Turns out AnalyzeBranch can modify the mbb being analyzed. This is a nasty
[oota-llvm.git] / lib / CodeGen / StackSlotColoring.cpp
index b6a043c0daea9a06d2be8c4ad9d649542a25a792..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"
@@ -29,10 +30,6 @@ DisableSharing("no-stack-slot-sharing",
              cl::init(false), cl::Hidden,
              cl::desc("Surpress slot sharing during stack coloring"));
 
-static cl::opt<int>
-DeleteLimit("slot-delete-limit", cl::init(-1), cl::Hidden,
-             cl::desc("Stack coloring slot deletion limit"));
-
 STATISTIC(NumEliminated,   "Number of stack slots eliminated due to coloring");
 
 namespace {
@@ -66,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);
     }
 
@@ -156,8 +155,7 @@ StackSlotColoring::OverlapWithAssignments(LiveInterval *li, int Color) const {
 int StackSlotColoring::ColorSlot(LiveInterval *li) {
   int Color = -1;
   bool Share = false;
-  if (!DisableSharing &&
-      (DeleteLimit == -1 || (int)NumEliminated < DeleteLimit)) {
+  if (!DisableSharing) {
     // Check if it's possible to reuse any of the used colors.
     Color = UsedColors.find_first();
     while (Color != -1) {
@@ -182,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
@@ -223,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);
   }
@@ -247,7 +261,7 @@ bool StackSlotColoring::ColorSlots(MachineFunction &MF) {
 }
 
 bool StackSlotColoring::runOnMachineFunction(MachineFunction &MF) {
-  DOUT << "******** Stack Slot Coloring ********\n";
+  DOUT << "********** Stack Slot Coloring **********\n";
 
   MFI = MF.getFrameInfo();
   LS = &getAnalysis<LiveStacks>();