Change MachineBasicBlock's to not be Annotations, instead they are kept as
[oota-llvm.git] / lib / Target / SparcV9 / InstrSelection / InstrSelection.cpp
index d8f8981362285b8ca64594154874002f0a68287e..677bef7d04ad14741819d5fcdae80f1a810379f1 100644 (file)
 #include "llvm/CodeGen/InstrSelectionSupport.h"
 #include "llvm/CodeGen/InstrForest.h"
 #include "llvm/CodeGen/MachineCodeForInstruction.h"
-#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
-#include "llvm/CodeGen/MachineCodeForMethod.h"
+#include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/Target/MachineRegInfo.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Function.h"
 #include "llvm/iPHINode.h"
 #include "llvm/Pass.h"
 #include "Support/CommandLine.h"
+#include "Support/LeakDetector.h"
 using std::cerr;
 using std::vector;
 
@@ -62,6 +62,10 @@ namespace {
                                        int ruleForNode, short* nts);
   public:
     InstructionSelection(TargetMachine &T) : Target(T) {}
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesCFG();
+    }
     
     bool runOnFunction(Function &F);
   };
@@ -71,6 +75,29 @@ namespace {
 static RegisterLLC<InstructionSelection>
 X("instselect", "Instruction Selection", createInstructionSelectionPass);
 
+TmpInstruction::TmpInstruction(Value *s1, Value *s2, const std::string &name)
+  : Instruction(s1->getType(), Instruction::UserOp1, name) {
+  Operands.push_back(Use(s1, this));  // s1 must be nonnull
+  if (s2) {
+    Operands.push_back(Use(s2, this));
+  }
+
+  // TmpInstructions should not be garbage checked.
+  LeakDetector::removeGarbageObject(this);
+}
+  
+// Constructor that requires the type of the temporary to be specified.
+// Both S1 and S2 may be NULL.(
+TmpInstruction::TmpInstruction(const Type *Ty, Value *s1, Value* s2,
+                               const std::string &name)
+  : Instruction(Ty, Instruction::UserOp1, name) {
+  if (s1) { Operands.push_back(Use(s1, this)); }
+  if (s2) { Operands.push_back(Use(s2, this)); }
+
+  // TmpInstructions should not be garbage checked.
+  LeakDetector::removeGarbageObject(this);
+}
+
 
 bool InstructionSelection::runOnFunction(Function &F)
 {
@@ -112,14 +139,20 @@ bool InstructionSelection::runOnFunction(Function &F)
     }
   
   //
-  // Record instructions in the vector for each basic block
+  // Create the MachineBasicBlock records and add all of the MachineInstrs
+  // defined in the MachineCodeForInstruction objects to also live in the
+  // MachineBasicBlock objects.
   // 
-  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI)
+  MachineFunction &MF = MachineFunction::get(&F);
+  for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
+    MachineBasicBlock *MCBB = new MachineBasicBlock(BI);
+    MF.getBasicBlockList().push_back(MCBB);
+
     for (BasicBlock::iterator II = BI->begin(); II != BI->end(); ++II) {
       MachineCodeForInstruction &mvec = MachineCodeForInstruction::get(II);
-      MachineCodeForBasicBlock &MCBB = MachineCodeForBasicBlock::get(BI);
-      MCBB.insert(MCBB.end(), mvec.begin(), mvec.end());
+      MCBB->insert(MCBB->end(), mvec.begin(), mvec.end());
     }
+  }
 
   // Insert phi elimination code
   InsertCodeForPhis(F);
@@ -127,7 +160,7 @@ bool InstructionSelection::runOnFunction(Function &F)
   if (SelectDebugLevel >= Select_PrintMachineCode)
     {
       cerr << "\n*** Machine instructions after INSTRUCTION SELECTION\n";
-      MachineCodeForMethod::get(&F).dump();
+      MachineFunction::get(&F).dump();
     }
   
   return true;
@@ -149,7 +182,12 @@ InstructionSelection::InsertCodeForPhis(Function &F)
          PHINode *PN = dyn_cast<PHINode>(&*IIt); ++IIt) {
       // FIXME: This is probably wrong...
       Value *PhiCpRes = new PHINode(PN->getType(), "PhiCp:");
-        
+
+      // The leak detector shouldn't track these nodes.  They are not garbage,
+      // even though their parent field is never filled in.
+      //
+      LeakDetector::removeGarbageObject(PhiCpRes);
+
       // for each incoming value of the phi, insert phi elimination
       //
       for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) {
@@ -172,7 +210,7 @@ InstructionSelection::InsertCodeForPhis(Function &F)
       Target.getRegInfo().cpValue2Value(PhiCpRes, PN, mvec);
       
       // get an iterator to machine instructions in the BB
-      MachineCodeForBasicBlock& bbMvec = MachineCodeForBasicBlock::get(BB);
+      MachineBasicBlock& bbMvec = MachineBasicBlock::get(BB);
       
       bbMvec.insert(bbMvec.begin(), mvec.begin(), mvec.end());
     }  // for each Phi Instr in BB
@@ -194,11 +232,11 @@ InstructionSelection::InsertPhiElimInstructions(BasicBlock *BB,
   
   assert (FirstMIOfTerm && "No Machine Instrs for terminator");
   
-  MachineCodeForBasicBlock &bbMvec = MachineCodeForBasicBlock::get(BB);
+  MachineBasicBlock &bbMvec = MachineBasicBlock::get(BB);
 
   // find the position of first machine instruction generated by the
   // terminator of this BB
-  MachineCodeForBasicBlock::iterator MCIt =
+  MachineBasicBlock::iterator MCIt =
     std::find(bbMvec.begin(), bbMvec.end(), FirstMIOfTerm);
 
   assert( MCIt != bbMvec.end() && "Start inst of terminator not found");