Revert "Reapply 91184 with fixes and an addition to the testcase to cover the
[oota-llvm.git] / lib / CodeGen / MachineSSAUpdater.cpp
index 31662b3494a9ebb127127e287be7c7052e6f970e..292096f00dd17e97579cc4dba7076810bd422564 100644 (file)
@@ -85,6 +85,36 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlock(MachineBasicBlock *BB) {
   return GetValueAtEndOfBlockInternal(BB);
 }
 
+static
+unsigned LookForIdenticalPHI(MachineBasicBlock *BB,
+          SmallVector<std::pair<MachineBasicBlock*, unsigned>, 8> &PredValues) {
+  if (BB->empty())
+    return 0;
+
+  MachineBasicBlock::iterator I = BB->front();
+  if (I->getOpcode() != TargetInstrInfo::PHI)
+    return 0;
+
+  AvailableValsTy AVals;
+  for (unsigned i = 0, e = PredValues.size(); i != e; ++i)
+    AVals[PredValues[i].first] = PredValues[i].second;
+  while (I != BB->end() && I->getOpcode() == TargetInstrInfo::PHI) {
+    bool Same = true;
+    for (unsigned i = 1, e = I->getNumOperands(); i != e; i += 2) {
+      unsigned SrcReg = I->getOperand(i).getReg();
+      MachineBasicBlock *SrcBB = I->getOperand(i+1).getMBB();
+      if (AVals[SrcBB] != SrcReg) {
+        Same = false;
+        break;
+      }
+    }
+    if (Same)
+      return I->getOperand(0).getReg();
+    ++I;
+  }
+  return 0;
+}
+
 /// InsertNewDef - Insert an empty PHI or IMPLICIT_DEF instruction which define
 /// a value of the given register class at the start of the specified basic
 /// block. It returns the virtual register defined by the instruction.
@@ -94,10 +124,9 @@ MachineInstr *InsertNewDef(unsigned Opcode,
                            const TargetRegisterClass *RC,
                            MachineRegisterInfo *MRI, const TargetInstrInfo *TII) {
   unsigned NewVR = MRI->createVirtualRegister(RC);
-  return BuildMI(*BB, I, I->getDebugLoc(), TII->get(Opcode), NewVR);
+  return BuildMI(*BB, I, DebugLoc::getUnknownLoc(), TII->get(Opcode), NewVR);
 }
                           
-
 /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
 /// is live in the middle of the specified block.
 ///
@@ -121,11 +150,16 @@ unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
   // If there is no definition of the renamed variable in this block, just use
   // GetValueAtEndOfBlock to do our work.
   if (!getAvailableVals(AV).count(BB))
-    return GetValueAtEndOfBlock(BB);
+    return GetValueAtEndOfBlockInternal(BB);
 
   // If there are no predecessors, just return undef.
-  if (BB->pred_empty()) 
-    return ~0U;  // Sentinel value representing undef.
+  if (BB->pred_empty()) {
+    // Insert an implicit_def to represent an undef value.
+    MachineInstr *NewDef = InsertNewDef(TargetInstrInfo::IMPLICIT_DEF,
+                                        BB, BB->getFirstTerminator(),
+                                        VRC, MRI, TII);
+    return NewDef->getOperand(0).getReg();
+  }
 
   // Otherwise, we have the hard case.  Get the live-in values for each
   // predecessor.
@@ -151,9 +185,15 @@ unsigned MachineSSAUpdater::GetValueInMiddleOfBlock(MachineBasicBlock *BB) {
   if (SingularValue != 0)
     return SingularValue;
 
+  // If an identical PHI is already in BB, just reuse it.
+  unsigned DupPHI = LookForIdenticalPHI(BB, PredValues);
+  if (DupPHI)
+    return DupPHI;
+
   // Otherwise, we do need a PHI: insert one now.
+  MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
   MachineInstr *InsertedPHI = InsertNewDef(TargetInstrInfo::PHI, BB,
-                                           BB->front(), VRC, MRI, TII);
+                                           Loc, VRC, MRI, TII);
 
   // Fill in all the predecessors of the PHI.
   MachineInstrBuilder MIB(InsertedPHI);
@@ -193,26 +233,24 @@ void MachineSSAUpdater::RewriteUse(MachineOperand &U) {
   unsigned NewVR = 0;
   if (UseMI->getOpcode() == TargetInstrInfo::PHI) {
     MachineBasicBlock *SourceBB = findCorrespondingPred(UseMI, &U);
-    NewVR = GetValueAtEndOfBlock(SourceBB);
-    // Insert an implicit_def to represent an undef value.
-    MachineInstr *NewDef = InsertNewDef(TargetInstrInfo::IMPLICIT_DEF,
-                                        SourceBB,SourceBB->getFirstTerminator(),
-                                        VRC, MRI, TII);
-    NewVR = NewDef->getOperand(0).getReg();
+    NewVR = GetValueAtEndOfBlockInternal(SourceBB);
   } else {
     NewVR = GetValueInMiddleOfBlock(UseMI->getParent());
-    if (NewVR == ~0U) {
-      // Insert an implicit_def to represent an undef value.
-      MachineInstr *NewDef = InsertNewDef(TargetInstrInfo::IMPLICIT_DEF,
-                                          UseMI->getParent(), UseMI,
-                                          VRC, MRI, TII);
-      NewVR = NewDef->getOperand(0).getReg();
-    }
   }
 
   U.setReg(NewVR);
 }
 
+void MachineSSAUpdater::ReplaceRegWith(unsigned OldReg, unsigned NewReg) {
+  MRI->replaceRegWith(OldReg, NewReg);
+
+  AvailableValsTy &AvailableVals = getAvailableVals(AV);
+  for (DenseMap<MachineBasicBlock*, unsigned>::iterator
+         I = AvailableVals.begin(), E = AvailableVals.end(); I != E; ++I)
+    if (I->second == OldReg)
+      I->second = NewReg;
+}
+
 /// GetValueAtEndOfBlockInternal - Check to see if AvailableVals has an entry
 /// for the specified BB and if so, return it.  If not, construct SSA form by
 /// walking predecessors inserting PHI nodes as needed until we get to a block
@@ -238,7 +276,8 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
     // that we have a cycle.  Handle this by inserting a PHI node and returning
     // it.  When we get back to the first instance of the recursion we will fill
     // in the PHI node.
-    MachineInstr *NewPHI = InsertNewDef(TargetInstrInfo::PHI, BB, BB->front(),
+    MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
+    MachineInstr *NewPHI = InsertNewDef(TargetInstrInfo::PHI, BB, Loc,
                                         VRC, MRI,TII);
     unsigned NewVR = NewPHI->getOperand(0).getReg();
     InsertRes.first->second = NewVR;
@@ -248,8 +287,13 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
   // If there are no predecessors, then we must have found an unreachable block
   // just return 'undef'.  Since there are no predecessors, InsertRes must not
   // be invalidated.
-  if (BB->pred_empty())
-    return InsertRes.first->second = ~0U;  // Sentinel value representing undef.
+  if (BB->pred_empty()) {
+    // Insert an implicit_def to represent an undef value.
+    MachineInstr *NewDef = InsertNewDef(TargetInstrInfo::IMPLICIT_DEF,
+                                        BB, BB->getFirstTerminator(),
+                                        VRC, MRI, TII);
+    return InsertRes.first->second = NewDef->getOperand(0).getReg();
+  }
 
   // Okay, the value isn't in the map and we just inserted a null in the entry
   // to indicate that we're processing the block.  Since we have no idea what
@@ -297,7 +341,7 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
       MachineInstr *OldVal = MRI->getVRegDef(InsertedVal);
       // Be careful about dead loops.  These RAUW's also update InsertedVal.
       assert(InsertedVal != SingularValue && "Dead loop?");
-      MRI->replaceRegWith(InsertedVal, SingularValue);
+      ReplaceRegWith(InsertedVal, SingularValue);
       OldVal->eraseFromParent();
     }
 
@@ -313,7 +357,8 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
   // Otherwise, we do need a PHI: insert one now if we don't already have one.
   MachineInstr *InsertedPHI;
   if (InsertedVal == 0) {
-    InsertedPHI = InsertNewDef(TargetInstrInfo::PHI, BB, BB->front(),
+    MachineBasicBlock::iterator Loc = BB->empty() ? BB->end() : BB->front();
+    InsertedPHI = InsertNewDef(TargetInstrInfo::PHI, BB, Loc,
                                VRC, MRI, TII);
     InsertedVal = InsertedPHI->getOperand(0).getReg();
   } else {
@@ -321,16 +366,11 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
   }
 
   // Fill in all the predecessors of the PHI.
-  bool IsUndef = true;
   MachineInstrBuilder MIB(InsertedPHI);
   for (IncomingPredInfoTy::iterator I =
          IncomingPredInfo.begin()+FirstPredInfoEntry,
-         E = IncomingPredInfo.end(); I != E; ++I) {
-    if (I->second == ~0U)
-      continue;
-    IsUndef = false;
+         E = IncomingPredInfo.end(); I != E; ++I)
     MIB.addReg(I->second).addMBB(I->first);
-  }
 
   // Drop the entries we added in IncomingPredInfo to restore the stack.
   IncomingPredInfo.erase(IncomingPredInfo.begin()+FirstPredInfoEntry,
@@ -338,10 +378,7 @@ unsigned MachineSSAUpdater::GetValueAtEndOfBlockInternal(MachineBasicBlock *BB){
 
   // See if the PHI node can be merged to a single value.  This can happen in
   // loop cases when we get a PHI of itself and one other value.
-  if (IsUndef) {
-    InsertedPHI->eraseFromParent();
-    InsertedVal = ~0U;
-  } else if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
+  if (unsigned ConstVal = InsertedPHI->isConstantValuePHI()) {
     MRI->replaceRegWith(InsertedVal, ConstVal);
     InsertedPHI->eraseFromParent();
     InsertedVal = ConstVal;