We need to verify that the machine instruction we're using as a replacement for
[oota-llvm.git] / lib / CodeGen / MachineCSE.cpp
index 5d79e96097e31bfb1ebd810e1e348898dbc08364..7eda8c129dc47dccb9b87ece8c0aa21d1ac58bce 100644 (file)
@@ -33,8 +33,6 @@ STATISTIC(NumCoalesces, "Number of copies coalesced");
 STATISTIC(NumCSEs,      "Number of common subexpression eliminated");
 STATISTIC(NumPhysCSEs,
           "Number of physreg referencing common subexpr eliminated");
-STATISTIC(NumCrossBlockPhysCSEs,
-          "Number of physreg common subexprs cross-block eliminated");
 STATISTIC(NumCommutes,  "Number of copies coalesced after commuting");
 
 namespace {
@@ -84,8 +82,7 @@ namespace {
                                 MachineBasicBlock::const_iterator E) const ;
     bool hasLivePhysRegDefUses(const MachineInstr *MI,
                                const MachineBasicBlock *MBB,
-                               SmallSet<unsigned,8> &PhysRefs,
-                               SmallVector<unsigned,8> &PhysDefs) const;
+                               SmallSet<unsigned,8> &PhysRefs) const;
     bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
                           SmallSet<unsigned,8> &PhysRefs) const;
     bool isCSECandidate(MachineInstr *MI);
@@ -192,8 +189,7 @@ MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
 /// instruction does not uses a physical register.
 bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
                                        const MachineBasicBlock *MBB,
-                                       SmallSet<unsigned,8> &PhysRefs,
-                                       SmallVector<unsigned,8> &PhysDefs) const{
+                                       SmallSet<unsigned,8> &PhysRefs) const {
   MachineBasicBlock::const_iterator I = MI; I = llvm::next(I);
   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
     const MachineOperand &MO = MI->getOperand(i);
@@ -210,7 +206,6 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
     if (MO.isDef() &&
         (MO.isDead() || isPhysDefTriviallyDead(Reg, I, MBB->end())))
       continue;
-    PhysDefs.push_back(Reg);
     PhysRefs.insert(Reg);
     for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
       PhysRefs.insert(*Alias);
@@ -221,43 +216,35 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
 
 bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
                                   SmallSet<unsigned,8> &PhysRefs) const {
-  // Look backward from MI to find CSMI.
+  // For now conservatively returns false if the common subexpression is
+  // not in the same basic block as the given instruction.
+  MachineBasicBlock *MBB = MI->getParent();
+  if (CSMI->getParent() != MBB)
+    return false;
+  MachineBasicBlock::const_iterator I = CSMI; I = llvm::next(I);
+  MachineBasicBlock::const_iterator E = MI;
   unsigned LookAheadLeft = LookAheadLimit;
-  MachineBasicBlock *CurBB = MI->getParent();
-  MachineBasicBlock::const_reverse_iterator I(MI);
-  MachineBasicBlock::const_reverse_iterator E(CurBB->rend());
   while (LookAheadLeft) {
-    while (LookAheadLeft && I != E) {
-      // Skip over dbg_value's.
-      while (I != E && I->isDebugValue())
-        ++I;
-
-      if (I == E) break;
-
-      if (&*I == CSMI)
-        return true;
+    // Skip over dbg_value's.
+    while (I != E && I->isDebugValue())
+      ++I;
 
-      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
-        const MachineOperand &MO = I->getOperand(i);
-        if (!MO.isReg() || !MO.isDef())
-          continue;
-        unsigned MOReg = MO.getReg();
-        if (TargetRegisterInfo::isVirtualRegister(MOReg))
-          continue;
-        if (PhysRefs.count(MOReg))
-          return false;
-      }
+    if (I == E)
+      return true;
 
-      --LookAheadLeft;
-      ++I;
+    for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
+      const MachineOperand &MO = I->getOperand(i);
+      if (!MO.isReg() || !MO.isDef())
+        continue;
+      unsigned MOReg = MO.getReg();
+      if (TargetRegisterInfo::isVirtualRegister(MOReg))
+        continue;
+      if (PhysRefs.count(MOReg))
+        return false;
     }
-    // Go back another BB; for now, only go back at most one BB.
-    MachineBasicBlock *CSBB = CSMI->getParent();
-    if (!CSBB->isSuccessor(CurBB) || CurBB->pred_size() != 1)
-      return false;
-    CurBB = CSBB;
-    I = CSBB->rbegin();
-    E = CSBB->rend();
+
+    --LookAheadLeft;
+    ++I;
   }
 
   return false;
@@ -273,12 +260,12 @@ bool MachineCSE::isCSECandidate(MachineInstr *MI) {
     return false;
 
   // Ignore stuff that we obviously can't move.
-  const TargetInstrDesc &TID = MI->getDesc();  
-  if (TID.mayStore() || TID.isCall() || TID.isTerminator() ||
+  const MCInstrDesc &MCID = MI->getDesc();  
+  if (MCID.mayStore() || MCID.isCall() || MCID.isTerminator() ||
       MI->hasUnmodeledSideEffects())
     return false;
 
-  if (TID.mayLoad()) {
+  if (MCID.mayLoad()) {
     // Okay, this instruction does a load. As a refinement, we allow the target
     // to decide whether the loaded value is actually a constant. If so, we can
     // actually use it as a load.
@@ -408,8 +395,7 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
     // used, then it's not safe to replace it with a common subexpression.
     // It's also not safe if the instruction uses physical registers.
     SmallSet<unsigned,8> PhysRefs;
-    SmallVector<unsigned,8> DirectPhysRefs;
-    if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs, DirectPhysRefs)) {
+    if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs)) {
       FoundCSE = false;
 
       // ... Unless the CS is local and it also defines the physical register
@@ -444,13 +430,24 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
       unsigned NewReg = CSMI->getOperand(i).getReg();
       if (OldReg == NewReg)
         continue;
+
       assert(TargetRegisterInfo::isVirtualRegister(OldReg) &&
              TargetRegisterInfo::isVirtualRegister(NewReg) &&
              "Do not CSE physical register defs!");
+
       if (!isProfitableToCSE(NewReg, OldReg, CSMI, MI)) {
         DoCSE = false;
         break;
       }
+
+      // Don't perform CSE if the result of the old instruction cannot exist
+      // within the register class of the new instruction.
+      const TargetRegisterClass *OldRC = MRI->getRegClass(OldReg);
+      if (!MRI->constrainRegClass(NewReg, OldRC)) {
+        DoCSE = false;
+        break;
+      }
+
       CSEPairs.push_back(std::make_pair(OldReg, NewReg));
       --NumDefs;
     }
@@ -462,14 +459,6 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
         MRI->clearKillFlags(CSEPairs[i].second);
       }
       MI->eraseFromParent();
-      if (!DirectPhysRefs.empty() && CSMI->getParent() != MBB) {
-        assert(CSMI->getParent()->isSuccessor(MBB));
-        ++NumCrossBlockPhysCSEs;
-        SmallVector<unsigned,8>::iterator PI = DirectPhysRefs.begin(),
-                                          PE = DirectPhysRefs.end();
-        for (; PI != PE; ++PI)
-          MBB->addLiveIn(*PI);
-      }
       ++NumCSEs;
       if (!PhysRefs.empty())
         ++NumPhysCSEs;