Turns out AnalyzeBranch can modify the mbb being analyzed. This is a nasty
[oota-llvm.git] / lib / CodeGen / StackSlotColoring.cpp
index 6d0e2148c59b1dc3f62e20048572e6546bf9a11c..fdd2336e78dadb8e36c63ba5de358d6b1db7a9e9 100644 (file)
@@ -15,6 +15,7 @@
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/CodeGen/LiveStackAnalysis.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
+#include "llvm/CodeGen/PseudoSourceValue.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
@@ -62,10 +63,12 @@ namespace {
 
   public:
     static char ID; // Pass identification
-    StackSlotColoring() : MachineFunctionPass((intptr_t)&ID), NextColor(-1) {}
+    StackSlotColoring() : MachineFunctionPass(&ID), NextColor(-1) {}
     
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.addRequired<LiveStacks>();
+      AU.addPreservedID(MachineLoopInfoID);
+      AU.addPreservedID(MachineDominatorsID);
       MachineFunctionPass::getAnalysisUsage(AU);
     }
 
@@ -177,7 +180,7 @@ int StackSlotColoring::ColorSlot(LiveInterval *li) {
   // Record the assignment.
   Assignments[Color].push_back(li);
   int FI = li->getStackSlotIndex();
-  DOUT << "Assigning fi #" << FI << " to fi #" << Color << "\n";
+  DOUT << "Assigning fi#" << FI << " to fi#" << Color << "\n";
 
   // Change size and alignment of the allocated slot. If there are multiple
   // objects sharing the same slot, then make sure the size and alignment
@@ -218,22 +221,38 @@ bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
       MachineInstr &MI = *MII;
       for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
         MachineOperand &MO = MI.getOperand(i);
-        if (!MO.isFrameIndex())
+        if (!MO.isFI())
           continue;
         int FI = MO.getIndex();
         if (FI < 0)
           continue;
-        FI = SlotMapping[FI];
-        if (FI == -1)
+        int NewFI = SlotMapping[FI];
+        if (NewFI == -1)
           continue;
-        MO.setIndex(FI);
+        MO.setIndex(NewFI);
+
+        // Update the MachineMemOperand for the new memory location.
+        // FIXME: We need a better method of managing these too.
+        SmallVector<MachineMemOperand, 2> MMOs(MI.memoperands_begin(),
+                                               MI.memoperands_end());
+        MI.clearMemOperands(MF);
+        const Value *OldSV = PseudoSourceValue::getFixedStack(FI);
+        for (unsigned i = 0, e = MMOs.size(); i != e; ++i) {
+          if (MMOs[i].getValue() == OldSV) {
+            MachineMemOperand MMO(PseudoSourceValue::getFixedStack(NewFI),
+                                  MMOs[i].getFlags(), MMOs[i].getOffset(),
+                                  MMOs[i].getSize(), MMOs[i].getAlignment());
+            MI.addMemOperand(MF, MMO);
+          } else
+            MI.addMemOperand(MF, MMOs[i]);
+        }
       }
     }
   }
 
   // Delete unused stack slots.
   while (NextColor != -1) {
-    DOUT << "Removing unused stack object fi #" << NextColor << "\n";
+    DOUT << "Removing unused stack object fi#" << NextColor << "\n";
     MFI->RemoveStackObject(NextColor);
     NextColor = AllColors.find_next(NextColor);
   }