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/SmallSet.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(NumAggrCommuted    , "Number of instructions aggressively commuted");
00053 STATISTIC(NumConvertedTo3Addr, "Number of instructions promoted to 3-address");
00054 STATISTIC(Num3AddrSunk,        "Number of 3-address instructions sunk");
00055 STATISTIC(NumReMats,           "Number of instructions re-materialized");
00056 
00057 namespace {
00058   class VISIBILITY_HIDDEN TwoAddressInstructionPass
00059     : public MachineFunctionPass {
00060     const TargetInstrInfo *TII;
00061     const TargetRegisterInfo *TRI;
00062     MachineRegisterInfo *MRI;
00063     LiveVariables *LV;
00064 
00065     bool Sink3AddrInstruction(MachineBasicBlock *MBB, MachineInstr *MI,
00066                               unsigned Reg,
00067                               MachineBasicBlock::iterator OldPos);
00068 
00069     bool isProfitableToReMat(unsigned Reg, const TargetRegisterClass *RC,
00070                              MachineInstr *MI, MachineInstr *DefMI,
00071                              MachineBasicBlock *MBB, unsigned Loc,
00072                              DenseMap<MachineInstr*, unsigned> &DistanceMap);
00073 
00074     bool NoUseAfterLastDef(unsigned Reg, MachineBasicBlock *MBB, unsigned Dist,
00075                            DenseMap<MachineInstr*, unsigned> &DistanceMap,
00076                            unsigned &LastDef);
00077 
00078     bool isProfitableToCommute(unsigned regB, unsigned regC,
00079                                MachineInstr *MI, MachineBasicBlock *MBB,
00080                                unsigned Dist,
00081                                DenseMap<MachineInstr*, unsigned> &DistanceMap);
00082 
00083     bool CommuteInstruction(MachineBasicBlock::iterator &mi,
00084                             MachineFunction::iterator &mbbi,
00085                             unsigned RegC, unsigned Dist,
00086                             DenseMap<MachineInstr*, unsigned> &DistanceMap);
00087   public:
00088     static char ID; // Pass identification, replacement for typeid
00089     TwoAddressInstructionPass() : MachineFunctionPass(&ID) {}
00090 
00091     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00092       AU.addPreserved<LiveVariables>();
00093       AU.addPreservedID(MachineLoopInfoID);
00094       AU.addPreservedID(MachineDominatorsID);
00095       if (StrongPHIElim)
00096         AU.addPreservedID(StrongPHIEliminationID);
00097       else
00098         AU.addPreservedID(PHIEliminationID);
00099       MachineFunctionPass::getAnalysisUsage(AU);
00100     }
00101 
00102     /// runOnMachineFunction - Pass entry point.
00103     bool runOnMachineFunction(MachineFunction&);
00104   };
00105 }
00106 
00107 char TwoAddressInstructionPass::ID = 0;
00108 static RegisterPass<TwoAddressInstructionPass>
00109 X("twoaddressinstruction", "Two-Address instruction pass");
00110 
00111 const PassInfo *const llvm::TwoAddressInstructionPassID = &X;
00112 
00113 /// Sink3AddrInstruction - A two-address instruction has been converted to a
00114 /// three-address instruction to avoid clobbering a register. Try to sink it
00115 /// past the instruction that would kill the above mentioned register to reduce
00116 /// register pressure.
00117 bool TwoAddressInstructionPass::Sink3AddrInstruction(MachineBasicBlock *MBB,
00118                                            MachineInstr *MI, unsigned SavedReg,
00119                                            MachineBasicBlock::iterator OldPos) {
00120   // Check if it's safe to move this instruction.
00121   bool SeenStore = true; // Be conservative.
00122   if (!MI->isSafeToMove(TII, SeenStore))
00123     return false;
00124 
00125   unsigned DefReg = 0;
00126   SmallSet<unsigned, 4> UseRegs;
00127 
00128   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00129     const MachineOperand &MO = MI->getOperand(i);
00130     if (!MO.isReg())
00131       continue;
00132     unsigned MOReg = MO.getReg();
00133     if (!MOReg)
00134       continue;
00135     if (MO.isUse() && MOReg != SavedReg)
00136       UseRegs.insert(MO.getReg());
00137     if (!MO.isDef())
00138       continue;
00139     if (MO.isImplicit())
00140       // Don't try to move it if it implicitly defines a register.
00141       return false;
00142     if (DefReg)
00143       // For now, don't move any instructions that define multiple registers.
00144       return false;
00145     DefReg = MO.getReg();
00146   }
00147 
00148   // Find the instruction that kills SavedReg.
00149   MachineInstr *KillMI = NULL;
00150   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(SavedReg),
00151          UE = MRI->use_end(); UI != UE; ++UI) {
00152     MachineOperand &UseMO = UI.getOperand();
00153     if (!UseMO.isKill())
00154       continue;
00155     KillMI = UseMO.getParent();
00156     break;
00157   }
00158 
00159   if (!KillMI || KillMI->getParent() != MBB)
00160     return false;
00161 
00162   // If any of the definitions are used by another instruction between the
00163   // position and the kill use, then it's not safe to sink it.
00164   // 
00165   // FIXME: This can be sped up if there is an easy way to query whether an
00166   // instruction is before or after another instruction. Then we can use
00167   // MachineRegisterInfo def / use instead.
00168   MachineOperand *KillMO = NULL;
00169   MachineBasicBlock::iterator KillPos = KillMI;
00170   ++KillPos;
00171 
00172   unsigned NumVisited = 0;
00173   for (MachineBasicBlock::iterator I = next(OldPos); I != KillPos; ++I) {
00174     MachineInstr *OtherMI = I;
00175     if (NumVisited > 30)  // FIXME: Arbitrary limit to reduce compile time cost.
00176       return false;
00177     ++NumVisited;
00178     for (unsigned i = 0, e = OtherMI->getNumOperands(); i != e; ++i) {
00179       MachineOperand &MO = OtherMI->getOperand(i);
00180       if (!MO.isReg())
00181         continue;
00182       unsigned MOReg = MO.getReg();
00183       if (!MOReg)
00184         continue;
00185       if (DefReg == MOReg)
00186         return false;
00187 
00188       if (MO.isKill()) {
00189         if (OtherMI == KillMI && MOReg == SavedReg)
00190           // Save the operand that kills the register. We want to unset the kill
00191           // marker if we can sink MI past it.
00192           KillMO = &MO;
00193         else if (UseRegs.count(MOReg))
00194           // One of the uses is killed before the destination.
00195           return false;
00196       }
00197     }
00198   }
00199 
00200   // Update kill and LV information.
00201   KillMO->setIsKill(false);
00202   KillMO = MI->findRegisterUseOperand(SavedReg, false, TRI);
00203   KillMO->setIsKill(true);
00204   
00205   if (LV)
00206     LV->replaceKillInstruction(SavedReg, KillMI, MI);
00207 
00208   // Move instruction to its destination.
00209   MBB->remove(MI);
00210   MBB->insert(KillPos, MI);
00211 
00212   ++Num3AddrSunk;
00213   return true;
00214 }
00215 
00216 /// isTwoAddrUse - Return true if the specified MI is using the specified
00217 /// register as a two-address operand.
00218 static bool isTwoAddrUse(MachineInstr *UseMI, unsigned Reg) {
00219   const TargetInstrDesc &TID = UseMI->getDesc();
00220   for (unsigned i = 0, e = TID.getNumOperands(); i != e; ++i) {
00221     MachineOperand &MO = UseMI->getOperand(i);
00222     if (MO.isReg() && MO.getReg() == Reg &&
00223         (MO.isDef() || TID.getOperandConstraint(i, TOI::TIED_TO) != -1))
00224       // Earlier use is a two-address one.
00225       return true;
00226   }
00227   return false;
00228 }
00229 
00230 /// isProfitableToReMat - Return true if the heuristics determines it is likely
00231 /// to be profitable to re-materialize the definition of Reg rather than copy
00232 /// the register.
00233 bool
00234 TwoAddressInstructionPass::isProfitableToReMat(unsigned Reg,
00235                                 const TargetRegisterClass *RC,
00236                                 MachineInstr *MI, MachineInstr *DefMI,
00237                                 MachineBasicBlock *MBB, unsigned Loc,
00238                                 DenseMap<MachineInstr*, unsigned> &DistanceMap){
00239   bool OtherUse = false;
00240   for (MachineRegisterInfo::use_iterator UI = MRI->use_begin(Reg),
00241          UE = MRI->use_end(); UI != UE; ++UI) {
00242     MachineOperand &UseMO = UI.getOperand();
00243     MachineInstr *UseMI = UseMO.getParent();
00244     MachineBasicBlock *UseMBB = UseMI->getParent();
00245     if (UseMBB == MBB) {
00246       DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(UseMI);
00247       if (DI != DistanceMap.end() && DI->second == Loc)
00248         continue;  // Current use.
00249       OtherUse = true;
00250       // There is at least one other use in the MBB that will clobber the
00251       // register. 
00252       if (isTwoAddrUse(UseMI, Reg))
00253         return true;
00254     }
00255   }
00256 
00257   // If other uses in MBB are not two-address uses, then don't remat.
00258   if (OtherUse)
00259     return false;
00260 
00261   // No other uses in the same block, remat if it's defined in the same
00262   // block so it does not unnecessarily extend the live range.
00263   return MBB == DefMI->getParent();
00264 }
00265 
00266 /// NoUseAfterLastDef - Return true if there are no intervening uses between the
00267 /// last instruction in the MBB that defines the specified register and the
00268 /// two-address instruction which is being processed. It also returns the last
00269 /// def location by reference
00270 bool TwoAddressInstructionPass::NoUseAfterLastDef(unsigned Reg,
00271                                  MachineBasicBlock *MBB, unsigned Dist,
00272                                  DenseMap<MachineInstr*, unsigned> &DistanceMap,
00273                                  unsigned &LastDef) {
00274   LastDef = 0;
00275   unsigned LastUse = Dist;
00276   for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Reg),
00277          E = MRI->reg_end(); I != E; ++I) {
00278     MachineOperand &MO = I.getOperand();
00279     MachineInstr *MI = MO.getParent();
00280     if (MI->getParent() != MBB)
00281       continue;
00282     DenseMap<MachineInstr*, unsigned>::iterator DI = DistanceMap.find(MI);
00283     if (DI == DistanceMap.end())
00284       continue;
00285     if (MO.isUse() && DI->second < LastUse)
00286       LastUse = DI->second;
00287     if (MO.isDef() && DI->second > LastDef)
00288       LastDef = DI->second;
00289   }
00290 
00291   return !(LastUse > LastDef && LastUse < Dist);
00292 }
00293 
00294 /// isProfitableToReMat - Return true if it's potentially profitable to commute
00295 /// the two-address instruction that's being processed.
00296 bool
00297 TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
00298                 MachineInstr *MI, MachineBasicBlock *MBB,
00299                 unsigned Dist, DenseMap<MachineInstr*, unsigned> &DistanceMap) {
00300   // Determine if it's profitable to commute this two address instruction. In
00301   // general, we want no uses between this instruction and the definition of
00302   // the two-address register.
00303   // e.g.
00304   // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
00305   // %reg1029<def> = MOV8rr %reg1028
00306   // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
00307   // insert => %reg1030<def> = MOV8rr %reg1028
00308   // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
00309   // In this case, it might not be possible to coalesce the second MOV8rr
00310   // instruction if the first one is coalesced. So it would be profitable to
00311   // commute it:
00312   // %reg1028<def> = EXTRACT_SUBREG %reg1027<kill>, 1
00313   // %reg1029<def> = MOV8rr %reg1028
00314   // %reg1029<def> = SHR8ri %reg1029, 7, %EFLAGS<imp-def,dead>
00315   // insert => %reg1030<def> = MOV8rr %reg1029
00316   // %reg1030<def> = ADD8rr %reg1029<kill>, %reg1028<kill>, %EFLAGS<imp-def,dead>  
00317 
00318   if (!MI->killsRegister(regC))
00319     return false;
00320 
00321   // Ok, we have something like:
00322   // %reg1030<def> = ADD8rr %reg1028<kill>, %reg1029<kill>, %EFLAGS<imp-def,dead>
00323   // let's see if it's worth commuting it.
00324 
00325   // If there is a use of regC between its last def (could be livein) and this
00326   // instruction, then bail.
00327   unsigned LastDefC = 0;
00328   if (!NoUseAfterLastDef(regC, MBB, Dist, DistanceMap, LastDefC))
00329     return false;
00330 
00331   // If there is a use of regB between its last def (could be livein) and this
00332   // instruction, then go ahead and make this transformation.
00333   unsigned LastDefB = 0;
00334   if (!NoUseAfterLastDef(regB, MBB, Dist, DistanceMap, LastDefB))
00335     return true;
00336 
00337   // Since there are no intervening uses for both registers, then commute
00338   // if the def of regC is closer. Its live interval is shorter.
00339   return LastDefB && LastDefC && LastDefC > LastDefB;
00340 }
00341 
00342 /// CommuteInstruction - Commute a two-address instruction and update the basic
00343 /// block, distance map, and live variables if needed. Return true if it is
00344 /// successful.
00345 bool
00346 TwoAddressInstructionPass::CommuteInstruction(MachineBasicBlock::iterator &mi,
00347                                               MachineFunction::iterator &mbbi,
00348                                               unsigned RegC, unsigned Dist,
00349                                DenseMap<MachineInstr*, unsigned> &DistanceMap) {
00350   MachineInstr *MI = mi;
00351   DOUT << "2addr: COMMUTING  : " << *MI;
00352   MachineInstr *NewMI = TII->commuteInstruction(MI);
00353 
00354   if (NewMI == 0) {
00355     DOUT << "2addr: COMMUTING FAILED!\n";
00356     return false;
00357   }
00358 
00359   DOUT << "2addr: COMMUTED TO: " << *NewMI;
00360   // If the instruction changed to commute it, update livevar.
00361   if (NewMI != MI) {
00362     if (LV)
00363       // Update live variables
00364       LV->replaceKillInstruction(RegC, MI, NewMI);
00365 
00366     mbbi->insert(mi, NewMI);           // Insert the new inst
00367     mbbi->erase(mi);                   // Nuke the old inst.
00368     mi = NewMI;
00369     DistanceMap.insert(std::make_pair(NewMI, Dist));
00370   }
00371   return true;
00372 }
00373 
00374 /// runOnMachineFunction - Reduce two-address instructions to two operands.
00375 ///
00376 bool TwoAddressInstructionPass::runOnMachineFunction(MachineFunction &MF) {
00377   DOUT << "Machine Function\n";
00378   const TargetMachine &TM = MF.getTarget();
00379   MRI = &MF.getRegInfo();
00380   TII = TM.getInstrInfo();
00381   TRI = TM.getRegisterInfo();
00382   LV = getAnalysisIfAvailable<LiveVariables>();
00383 
00384   bool MadeChange = false;
00385 
00386   DOUT << "********** REWRITING TWO-ADDR INSTRS **********\n";
00387   DOUT << "********** Function: " << MF.getFunction()->getName() << '\n';
00388 
00389   // ReMatRegs - Keep track of the registers whose def's are remat'ed.
00390   BitVector ReMatRegs;
00391   ReMatRegs.resize(MRI->getLastVirtReg()+1);
00392 
00393   // DistanceMap - Keep track the distance of a MI from the start of the
00394   // current basic block.
00395   DenseMap<MachineInstr*, unsigned> DistanceMap;
00396 
00397   for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
00398        mbbi != mbbe; ++mbbi) {
00399     unsigned Dist = 0;
00400     DistanceMap.clear();
00401     for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
00402          mi != me; ) {
00403       MachineBasicBlock::iterator nmi = next(mi);
00404       const TargetInstrDesc &TID = mi->getDesc();
00405       bool FirstTied = true;
00406 
00407       DistanceMap.insert(std::make_pair(mi, ++Dist));
00408       for (unsigned si = 1, e = TID.getNumOperands(); si < e; ++si) {
00409         int ti = TID.getOperandConstraint(si, TOI::TIED_TO);
00410         if (ti == -1)
00411           continue;
00412 
00413         if (FirstTied) {
00414           ++NumTwoAddressInstrs;
00415           DOUT << '\t'; DEBUG(mi->print(*cerr.stream(), &TM));
00416         }
00417 
00418         FirstTied = false;
00419 
00420         assert(mi->getOperand(si).isReg() && mi->getOperand(si).getReg() &&
00421                mi->getOperand(si).isUse() && "two address instruction invalid");
00422 
00423         // If the two operands are the same we just remove the use
00424         // and mark the def as def&use, otherwise we have to insert a copy.
00425         if (mi->getOperand(ti).getReg() != mi->getOperand(si).getReg()) {
00426           // Rewrite:
00427           //     a = b op c
00428           // to:
00429           //     a = b
00430           //     a = a op c
00431           unsigned regA = mi->getOperand(ti).getReg();
00432           unsigned regB = mi->getOperand(si).getReg();
00433 
00434           assert(TargetRegisterInfo::isVirtualRegister(regA) &&
00435                  TargetRegisterInfo::isVirtualRegister(regB) &&
00436                  "cannot update physical register live information");
00437 
00438 #ifndef NDEBUG
00439           // First, verify that we don't have a use of a in the instruction (a =
00440           // b + a for example) because our transformation will not work. This
00441           // should never occur because we are in SSA form.
00442           for (unsigned i = 0; i != mi->getNumOperands(); ++i)
00443             assert((int)i == ti ||
00444                    !mi->getOperand(i).isReg() ||
00445                    mi->getOperand(i).getReg() != regA);
00446 #endif
00447 
00448           // If this instruction is not the killing user of B, see if we can
00449           // rearrange the code to make it so.  Making it the killing user will
00450           // allow us to coalesce A and B together, eliminating the copy we are
00451           // about to insert.
00452           if (!mi->killsRegister(regB)) {
00453             // If this instruction is commutative, check to see if C dies.  If
00454             // so, swap the B and C operands.  This makes the live ranges of A
00455             // and C joinable.
00456             // FIXME: This code also works for A := B op C instructions.
00457             if (TID.isCommutable() && mi->getNumOperands() >= 3) {
00458               assert(mi->getOperand(3-si).isReg() &&
00459                      "Not a proper commutative instruction!");
00460               unsigned regC = mi->getOperand(3-si).getReg();
00461               if (mi->killsRegister(regC)) {
00462                 if (CommuteInstruction(mi, mbbi, regC, Dist, DistanceMap)) {
00463                   ++NumCommuted;
00464                   regB = regC;
00465                   goto InstructionRearranged;
00466                 }
00467               }
00468             }
00469 
00470             // If this instruction is potentially convertible to a true
00471             // three-address instruction,
00472             if (TID.isConvertibleTo3Addr()) {
00473               // FIXME: This assumes there are no more operands which are tied
00474               // to another register.
00475 #ifndef NDEBUG
00476               for (unsigned i = si + 1, e = TID.getNumOperands(); i < e; ++i)
00477                 assert(TID.getOperandConstraint(i, TOI::TIED_TO) == -1);
00478 #endif
00479 
00480               MachineInstr *NewMI = TII->convertToThreeAddress(mbbi, mi, LV);
00481               if (NewMI) {
00482                 DOUT << "2addr: CONVERTING 2-ADDR: " << *mi;
00483                 DOUT << "2addr:         TO 3-ADDR: " << *NewMI;
00484                 bool Sunk = false;
00485 
00486                 if (NewMI->findRegisterUseOperand(regB, false, TRI))
00487                   // FIXME: Temporary workaround. If the new instruction doesn't
00488                   // uses regB, convertToThreeAddress must have created more
00489                   // then one instruction.
00490                   Sunk = Sink3AddrInstruction(mbbi, NewMI, regB, mi);
00491 
00492                 mbbi->erase(mi); // Nuke the old inst.
00493 
00494                 if (!Sunk) {
00495                   DistanceMap.insert(std::make_pair(NewMI, Dist));
00496                   mi = NewMI;
00497                   nmi = next(mi);
00498                 }
00499 
00500                 ++NumConvertedTo3Addr;
00501                 break; // Done with this instruction.
00502               }
00503             }
00504           }
00505 
00506           // If it's profitable to commute the instruction, do so.
00507           if (TID.isCommutable() && mi->getNumOperands() >= 3) {
00508             unsigned regC = mi->getOperand(3-si).getReg();
00509             if (isProfitableToCommute(regB, regC, mi, mbbi, Dist, DistanceMap))
00510               if (CommuteInstruction(mi, mbbi, regC, Dist, DistanceMap)) {
00511                 ++NumAggrCommuted;
00512                 ++NumCommuted;
00513                 regB = regC;
00514               }
00515           }
00516 
00517         InstructionRearranged:
00518           const TargetRegisterClass* rc = MRI->getRegClass(regA);
00519           MachineInstr *DefMI = MRI->getVRegDef(regB);
00520           // If it's safe and profitable, remat the definition instead of
00521           // copying it.
00522           if (DefMI &&
00523               DefMI->getDesc().isAsCheapAsAMove() &&
00524               DefMI->isSafeToReMat(TII, regB) &&
00525               isProfitableToReMat(regB, rc, mi, DefMI, mbbi, Dist,DistanceMap)){
00526             DEBUG(cerr << "2addr: REMATTING : " << *DefMI << "\n");
00527             TII->reMaterialize(*mbbi, mi, regA, DefMI);
00528             ReMatRegs.set(regB);
00529             ++NumReMats;
00530           } else {
00531             TII->copyRegToReg(*mbbi, mi, regA, regB, rc, rc);
00532           }
00533 
00534           MachineBasicBlock::iterator prevMI = prior(mi);
00535           // Update DistanceMap.
00536           DistanceMap.insert(std::make_pair(prevMI, Dist));
00537           DistanceMap[mi] = ++Dist;
00538 
00539           // Update live variables for regB.
00540           if (LV) {
00541             LiveVariables::VarInfo& varInfoB = LV->getVarInfo(regB);
00542 
00543             // regB is used in this BB.
00544             varInfoB.UsedBlocks[mbbi->getNumber()] = true;
00545 
00546             if (LV->removeVirtualRegisterKilled(regB,  mi))
00547               LV->addVirtualRegisterKilled(regB, prevMI);
00548 
00549             if (LV->removeVirtualRegisterDead(regB, mi))
00550               LV->addVirtualRegisterDead(regB, prevMI);
00551           }
00552 
00553           DOUT << "\t\tprepend:\t"; DEBUG(prevMI->print(*cerr.stream(), &TM));
00554           
00555           // Replace all occurences of regB with regA.
00556           for (unsigned i = 0, e = mi->getNumOperands(); i != e; ++i) {
00557             if (mi->getOperand(i).isReg() &&
00558                 mi->getOperand(i).getReg() == regB)
00559               mi->getOperand(i).setReg(regA);
00560           }
00561         }
00562 
00563         assert(mi->getOperand(ti).isDef() && mi->getOperand(si).isUse());
00564         mi->getOperand(ti).setReg(mi->getOperand(si).getReg());
00565         MadeChange = true;
00566 
00567         DOUT << "\t\trewrite to:\t"; DEBUG(mi->print(*cerr.stream(), &TM));
00568       }
00569 
00570       mi = nmi;
00571     }
00572   }
00573 
00574   // Some remat'ed instructions are dead.
00575   int VReg = ReMatRegs.find_first();
00576   while (VReg != -1) {
00577     if (MRI->use_empty(VReg)) {
00578       MachineInstr *DefMI = MRI->getVRegDef(VReg);
00579       DefMI->eraseFromParent();
00580     }
00581     VReg = ReMatRegs.find_next(VReg);
00582   }
00583 
00584   return MadeChange;
00585 }