LLVM API Documentation

TwoAddressInstructionPass.cpp

Go to the documentation of this file.
00001 //===-- TwoAddressInstructionPass.cpp - Two-Address instruction pass ------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the TwoAddress instruction pass which is used
00011 // by most register allocators. Two-Address instructions are rewritten
00012 // from:
00013 //
00014 //     A = B op C
00015 //
00016 // to:
00017 //
00018 //     A = B
00019 //     A op= C
00020 //
00021 // Note that if a register allocator chooses to use this pass, that it
00022 // has to be capable of handling the non-SSA nature of these rewritten
00023 // virtual registers.
00024 //
00025 // It is also worth noting that the duplicate operand of the two
00026 // address instruction is removed.
00027 //
00028 //===----------------------------------------------------------------------===//
00029 
00030 #define DEBUG_TYPE "twoaddrinstr"
00031 #include "llvm/CodeGen/Passes.h"
00032 #include "llvm/Function.h"
00033 #include "llvm/CodeGen/LiveVariables.h"
00034 #include "llvm/CodeGen/MachineFunctionPass.h"
00035 #include "llvm/CodeGen/MachineInstr.h"
00036 #include "llvm/CodeGen/MachineRegisterInfo.h"
00037 #include "llvm/Target/TargetRegisterInfo.h"
00038 #include "llvm/Target/TargetInstrInfo.h"
00039 #include "llvm/Target/TargetMachine.h"
00040 #include "llvm/Target/TargetOptions.h"
00041 #include "llvm/Support/Compiler.h"
00042 #include "llvm/Support/Debug.h"
00043 #include "llvm/ADT/BitVector.h"
00044 #include "llvm/ADT/DenseMap.h"
00045 #include "llvm/ADT/SmallPtrSet.h"
00046 #include "llvm/ADT/Statistic.h"
00047 #include "llvm/ADT/STLExtras.h"
00048 using namespace llvm;
00049 
00050 STATISTIC(NumTwoAddressInstrs, "Number of two-address instructions");
00051 STATISTIC(NumCommuted        , "Number of instructions commuted to coalesce");
00052 STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
00053 STATISTIC(Num3AddrSunk,        "Number of 3-address instructions sunk");
00054 STATISTIC(NumReMats,           "Number of instructions re-materialized");
00055 
00056 namespace {
00057   class VISIBILITY_HIDDEN TwoAddressInstructionPass
00058     : public MachineFunctionPass {
00059     const TargetInstrInfo *TII;
00060     const TargetRegisterInfo *TRI;
00061     MachineRegisterInfo *MRI;
00062     LiveVariables *LV;
00063 
00064     bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
00065                               unsigned Reg,
00066                               MachineBasicBlock::iterator OldPos);
00067 
00068     bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
00069                              MachineInstr *MI, MachineInstr *DefMI,
00070                              MachineBasicBlock *MBB, unsigned Loc,
00071                              DenseMap<MachineInstr*, unsigned> &DistanceMap);
00072   public:
00073     static char ID; // Pass identification, replacement for typeid
00074     TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
00075 
00076     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00077       AU.addPreserved<LiveVariables>();
00078       AU.addPreservedID(MachineLoopInfoID);
00079       AU.addPreservedID(MachineDominatorsID);
00080       if (StrongPHIElim)
00081         AU.addPreservedID(StrongPHIEliminationID);
00082       else
00083         AU.addPreservedID(PHIEliminationID);
00084       MachineFunctionPass::getAnalysisUsage(AU);
00085     }
00086 
00087     /// runOnMachineFunction - Pass entry point.
00088     bool runOnMachineFunction(MachineFunction&);
00089   };
00090 }
00091 
00092 char TwoAddressInstructionPass::ID = 0;
00093 static RegisterPass<TwoAddressInstructionPass>
00094 X("twoaddressinstruction", "Two-Address instruction pass");
00095 
00096 const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
00097 
00098 /// Sink3AddrInstruction - A two-address instruction has been converted to a
00099 /// three-address instruction to avoid clobbering a register. Try to sink it
00100 /// past the instruction that would kill the above mentioned register to reduce
00101 /// register pressure.
00102 bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
00103                                            MachineInstr *MI, unsigned SavedReg,
00104                                            MachineBasicBlock::iterator OldPos) {
00105   // Check if it's safe to move this instruction.
00106   bool SeenStore = true; // Be conservative.
00107   if (!MI->isSafeToMove(TII, SeenStore))
00108     return false;
00109 
00110   unsigned DefReg = 0;
00111   SmallSet<unsigned, 4> UseRegs;
00112 
00113   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00114     const MachineOperand &MO = MI->getOperand(i);
00115     if (!MO.isReg())
00116       continue;
00117     unsigned MOReg = MO.getReg();
00118     if (!MOReg)
00119       continue;
00120     if (MO.isUse() && MOReg != SavedReg)
00121       UseRegs.insert(MO.getReg());
00122     if (!MO.isDef())
00123       continue;
00124     if (MO.isImplicit())
00125       // Don't try to move it if it implicitly defines a register.
00126       return false;
00127     if (DefReg)
00128       // For now, don't move any instructions that define multiple registers.
00129       return false;
00130     DefReg = MO.getReg();
00131   }
00132 
00133   // Find the instruction that kills SavedReg.
00134   MachineInstr *KillMI = NULL;
00135   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
00136          UE = MRI->use_end(); UI != UE; ++UI) {
00137     MachineOperand &UseMO = UI.getOperand();
00138     if (!UseMO.isKill())
00139       continue;
00140     KillMI = UseMO.getParent();
00141     break;
00142   }
00143 
00144   if (!KillMI || KillMI->getParent() != MBB)
00145     return false;
00146 
00147   // If any of the definitions are used by another instruction between the
00148   // position and the kill use, then it's not safe to sink it.
00149   // 
00150   // FIXME: This can be sped up if there is an easy way to query whether an
00151   // instruction is before or after another instruction. Then we can use
00152   // MachineRegisterInfo def / use instead.
00153   MachineOperand *KillMO = NULL;
00154   MachineBasicBlock::iterator KillPos = KillMI;
00155   ++KillPos;
00156 
00157   unsigned NumVisited = 0;
00158   for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
00159     MachineInstr *OtherMI = I;
00160     if (NumVisited > 30)  // FIXME: Arbitrary limit to reduce compile time cost.
00161       return false;
00162     ++NumVisited;
00163     for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
00164       MachineOperand &MO = OtherMI->getOperand(i);
00165       if (!MO.isReg())
00166         continue;
00167       unsigned MOReg = MO.getReg();
00168       if (!MOReg)
00169         continue;
00170       if (DefReg == MOReg)
00171         return false;
00172 
00173       if (MO.isKill()) {
00174         if (OtherMI == KillMI && MOReg == SavedReg)
00175           // Save the operand that kills the register. We want to unset the kill
00176           // marker if we can sink MI past it.
00177           KillMO = &MO;
00178         else if (UseRegs.count(MOReg))
00179           // One of the uses is killed before the destination.
00180           return false;
00181       }
00182     }
00183   }
00184 
00185   // Update kill and LV information.
00186   KillMO->setIsKill(false);
00187   KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
00188   KillMO->setIsKill(true);
00189   
00190   if (LV)
00191     LV->replaceKillInstruction(SavedReg, KillMI, MI);
00192 
00193   // Move instruction to its destination.
00194   MBB->remove(MI);
00195   MBB->insert(KillPos, MI);
00196 
00197   ++Num3AddrSunk;
00198   return true;
00199 }
00200 
00201 /// isTwoAddrUse - Return true if the specified MI is using the specified
00202 /// register as a two-address operand.
00203 static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
00204   const TargetInstrDesc &TID = UseMI->getDesc();
00205   for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
00206     MachineOperand &MO = UseMI->getOperand(i);
00207     if (MO.isReg() && MO.getReg() == Reg &&
00208         (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
00209       // Earlier use is a two-address one.
00210       return true;
00211   }
00212   return false;
00213 }
00214 
00215 /// isProfitableToReMat - Return true if the heuristics determines it is likely
00216 /// to be profitable to re-materialize the definition of Reg rather than copy
00217 /// the register.
00218 bool
00219 TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
00220                                 const TargetRegisterClass *RC,
00221                                 MachineInstr *MI, MachineInstr *DefMI,
00222                                 MachineBasicBlock *MBB, unsigned Loc,
00223                                 DenseMap<MachineInstr*, unsigned> &DistanceMap){
00224   bool OtherUse = false;
00225   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
00226          UE = MRI->use_end(); UI != UE; ++UI) {
00227     MachineOperand &UseMO = UI.getOperand();
00228     if (!UseMO.isUse())
00229       continue;
00230     MachineInstr *UseMI = UseMO.getParent();
00231     MachineBasicBlock *UseMBB = UseMI->getParent();
00232     if (UseMBB == MBB) {
00233       DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
00234       if (DI != DistanceMap.end() && DI->second == Loc)
00235         continue;  // Current use.
00236       OtherUse = true;
00237       // There is at least one other use in the MBB that will clobber the
00238       // register. 
00239       if (isTwoAddrUse(UseMI, Reg))
00240         return true;
00241     }
00242   }
00243 
00244   // If other uses in MBB are not two-address uses, then don't remat.
00245   if (OtherUse)
00246     return false;
00247 
00248   // No other uses in the same block, remat if it's defined in the same
00249   // block so it does not unnecessarily extend the live range.
00250   return MBB == DefMI->getParent();
00251 }
00252 
00253 /// runOnMachineFunction - Reduce two-address instructions to two operands.
00254 ///
00255 bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
00256   DOUT << "Machine Function\n";
00257   const TargetMachine &TM = MF.getTarget();
00258   MRI = &MF.getRegInfo();
00259   TII = TM.getInstrInfo();
00260   TRI = TM.getRegisterInfo();
00261   LV = getAnalysisToUpdate<LiveVariables>();
00262 
00263   bool MadeChange = false;
00264 
00265   DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
00266   DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
00267 
00268   // ReMatRegs - Keep track of the registers whose def's are remat'ed.
00269   BitVector ReMatRegs;
00270   ReMatRegs.resize(MRI->getLastVirtReg()+1);
00271 
00272   // DistanceMap - Keep track the distance of a MI from the start of the
00273   // current basic block.
00274   DenseMap<MachineInstr*, unsigned> DistanceMap;
00275 
00276   for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
00277        mbbi != mbbe; ++mbbi) {
00278     unsigned Dist = 0;
00279     DistanceMap.clear();
00280     for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
00281          mi != me; ) {
00282       MachineBasicBlock::iterator nmi = next(mi);
00283       const TargetInstrDesc &TID = mi->getDesc();
00284       bool FirstTied = true;
00285 
00286       DistanceMap.insert(std::make_pair(mi, ++Dist));
00287       for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
00288         int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
00289         if (ti == -1)
00290           continue;
00291 
00292         if (FirstTied) {
00293           ++NumTwoAddressInstrs;
00294           DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
00295         }
00296 
00297         FirstTied = false;
00298 
00299         assert(mi->getOperand(si).isReg() && mi->getOperand(si).getReg() &&
00300                mi->getOperand(si).isUse() && "two address instruction invalid");
00301 
00302         // If the two operands are the same we just remove the use
00303         // and mark the def as def&use, otherwise we have to insert a copy.
00304         if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
00305           // Rewrite:
00306           //     a = b op c
00307           // to:
00308           //     a = b
00309           //     a = a op c
00310           unsigned regA = mi->getOperand(ti).getReg();
00311           unsigned regB = mi->getOperand(si).getReg();
00312 
00313           assert(TargetRegisterInfo::isVirtualRegister(regA) &&
00314                  TargetRegisterInfo::isVirtualRegister(regB) &&
00315                  "cannot update physical register live information");
00316 
00317 #ifndef NDEBUG
00318           // First, verify that we don't have a use of a in the instruction (a =
00319           // b + a for example) because our transformation will not work. This
00320           // should never occur because we are in SSA form.
00321           for (unsigned i = 0; i != mi->getNumOperands(); ++i)
00322             assert((int)i == ti ||
00323                    !mi->getOperand(i).isReg() ||
00324                    mi->getOperand(i).getReg() != regA);
00325 #endif
00326 
00327           // If this instruction is not the killing user of B, see if we can
00328           // rearrange the code to make it so.  Making it the killing user will
00329           // allow us to coalesce A and B together, eliminating the copy we are
00330           // about to insert.
00331           if (!mi->killsRegister(regB)) {
00332             // If this instruction is commutative, check to see if C dies.  If
00333             // so, swap the B and C operands.  This makes the live ranges of A
00334             // and C joinable.
00335             // FIXME: This code also works for A := B op C instructions.
00336             if (TID.isCommutable() && mi->getNumOperands() >= 3) {
00337               assert(mi->getOperand(3-si).isReg() &&
00338                      "Not a proper commutative instruction!");
00339               unsigned regC = mi->getOperand(3-si).getReg();
00340 
00341               if (mi->killsRegister(regC)) {
00342                 DOUT << "2addr: COMMUTING  : " << *mi;
00343                 MachineInstr *NewMI = TII->commuteInstruction(mi);
00344 
00345                 if (NewMI == 0) {
00346                   DOUT << "2addr: COMMUTING FAILED!\n";
00347                 } else {
00348                   DOUT << "2addr: COMMUTED TO: " << *NewMI;
00349                   // If the instruction changed to commute it, update livevar.
00350                   if (NewMI != mi) {
00351                     if (LV)
00352                       // Update live variables
00353                       LV->replaceKillInstruction(regC, mi, NewMI);
00354                     
00355                     mbbi->insert(mi, NewMI);           // Insert the new inst
00356                     mbbi->erase(mi);                   // Nuke the old inst.
00357                     mi = NewMI;
00358                     DistanceMap.insert(std::make_pair(NewMI, Dist));
00359                   }
00360 
00361                   ++NumCommuted;
00362                   regB = regC;
00363                   goto InstructionRearranged;
00364                 }
00365               }
00366             }
00367 
00368             // If this instruction is potentially convertible to a true
00369             // three-address instruction,
00370             if (TID.isConvertibleTo3Addr()) {
00371               // FIXME: This assumes there are no more operands which are tied
00372               // to another register.
00373 #ifndef NDEBUG
00374               for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
00375                 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
00376 #endif
00377 
00378               MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
00379               if (NewMI) {
00380                 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
00381                 DOUT << "2addr:         TO 3-ADDR: " << *NewMI;
00382                 bool Sunk = false;
00383 
00384                 if (NewMI->findRegisterUseOperand(regB, false, TRI))
00385                   // FIXME: Temporary workaround. If the new instruction doesn't
00386                   // uses regB, convertToThreeAddress must have created more
00387                   // then one instruction.
00388                   Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
00389 
00390                 mbbi->erase(mi); // Nuke the old inst.
00391 
00392                 if (!Sunk) {
00393                   DistanceMap.insert(std::make_pair(NewMI, Dist));
00394                   mi = NewMI;
00395                   nmi = next(mi);
00396                 }
00397 
00398                 ++NumConvertedTo3Addr;
00399                 break; // Done with this instruction.
00400               }
00401             }
00402           }
00403 
00404         InstructionRearranged:
00405           const TargetRegisterClass* rc = MRI->getRegClass(regA);
00406           MachineInstr *DefMI = MRI->getVRegDef(regB);
00407           // If it's safe and profitable, remat the definition instead of
00408           // copying it.
00409           if (DefMI &&
00410               DefMI->getDesc().isAsCheapAsAMove() &&
00411               DefMI->isSafeToReMat(TII, regB) &&
00412               isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){
00413             DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
00414             TII->reMaterialize(*mbbi, mi, regA, DefMI);
00415             ReMatRegs.set(regB);
00416             ++NumReMats;
00417           } else {
00418             TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
00419           }
00420 
00421           MachineBasicBlock::iterator prevMi = prior(mi);
00422           DOUT << "\t\tprepend:\t"; DEBUG(prevMi->print(*cerr.stream(), &TM));
00423 
00424           // Update live variables for regB.
00425           if (LV) {
00426             LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
00427 
00428             // regB is used in this BB.
00429             varInfoB.UsedBlocks[mbbi->getNumber()] = true;
00430 
00431             if (LV->removeVirtualRegisterKilled(regB,  mi))
00432               LV->addVirtualRegisterKilled(regB, prevMi);
00433 
00434             if (LV->removeVirtualRegisterDead(regB, mi))
00435               LV->addVirtualRegisterDead(regB, prevMi);
00436           }
00437           
00438           // Replace all occurences of regB with regA.
00439           for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
00440             if (mi->getOperand(i).isReg() &&
00441                 mi->getOperand(i).getReg() == regB)
00442               mi->getOperand(i).setReg(regA);
00443           }
00444         }
00445 
00446         assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
00447         mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
00448         MadeChange = true;
00449 
00450         DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
00451       }
00452 
00453       mi = nmi;
00454     }
00455   }
00456 
00457   // Some remat'ed instructions are dead.
00458   int VReg = ReMatRegs.find_first();
00459   while (VReg != -1) {
00460     if (MRI->use_empty(VReg)) {
00461       MachineInstr *DefMI = MRI->getVRegDef(VReg);
00462       DefMI->eraseFromParent();
00463     }
00464     VReg = ReMatRegs.find_next(VReg);
00465   }
00466 
00467   return MadeChange;
00468 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.