Turns out AnalyzeBranch can modify the mbb being analyzed. This is a nasty
[oota-llvm.git] / lib / CodeGen / StackSlotColoring.cpp
index fe04657e297a6dfed775053847c12c4609d549cc..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"
@@ -179,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
@@ -225,17 +226,33 @@ bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
         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);
   }