Start using the new function cloning header
[oota-llvm.git] / lib / CodeGen / MachineFunction.cpp
index 0e940f88be1b1453efd4602a8cc62822f6bb5665..3dcbabc2a684b5c445eeeece9ff3d4b871b5f3f9 100644 (file)
-//===-- MachineCodeForMethod.cpp -------------------------------------------=//
+//===-- MachineFunction.cpp -----------------------------------------------===//
 // 
-// Purpose:
-//   Collect native machine code information for a function.
-//   This allows target-specific information about the generated code
-//   to be stored with each function.
-//===---------------------------------------------------------------------===//
+// Collect native machine code information for a function.  This allows
+// target-specific information about the generated code to be stored with each
+// function.
+//
+//===----------------------------------------------------------------------===//
 
-#include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineInstr.h"  // For debug output
-#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineCodeForInstruction.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/MachineFrameInfo.h"
 #include "llvm/Target/MachineCacheInfo.h"
 #include "llvm/Function.h"
 #include "llvm/iOther.h"
+#include "llvm/Pass.h"
 #include <limits.h>
 
 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
 
-static AnnotationID MCFM_AID(
+static AnnotationID MF_AID(
                  AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
 
+
+//===---------------------------------------------------------------------===//
+// Code generation/destruction passes
+//===---------------------------------------------------------------------===//
+
+namespace {
+  class ConstructMachineFunction : public FunctionPass {
+    TargetMachine &Target;
+  public:
+    ConstructMachineFunction(TargetMachine &T) : Target(T) {}
+    
+    const char *getPassName() const {
+      return "ConstructMachineFunction";
+    }
+    
+    bool runOnFunction(Function &F) {
+      MachineFunction::construct(&F, Target).CalculateArgSize();
+      return false;
+    }
+  };
+
+  struct DestroyMachineFunction : public FunctionPass {
+    const char *getPassName() const { return "FreeMachineFunction"; }
+    
+    static void freeMachineCode(Instruction &I) {
+      MachineCodeForInstruction::destroy(&I);
+    }
+    
+    bool runOnFunction(Function &F) {
+      for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
+        for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
+          MachineCodeForInstruction::get(I).dropAllReferences();
+      
+      for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
+        for_each(FI->begin(), FI->end(), freeMachineCode);
+      
+      return false;
+    }
+  };
+
+  struct Printer : public FunctionPass {
+    const char *getPassName() const { return "MachineFunction Printer"; }
+
+    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+      AU.setPreservesAll();
+    }
+
+    bool runOnFunction(Function &F) {
+      MachineFunction::get(&F).dump();
+      return false;
+    }
+  };
+}
+
+Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
+  return new ConstructMachineFunction(Target);
+}
+
+Pass *createMachineCodeDestructionPass() {
+  return new DestroyMachineFunction();
+}
+
+Pass *createMachineFunctionPrinterPass() {
+  return new Printer();
+}
+
+
+//===---------------------------------------------------------------------===//
+// MachineFunction implementation
+//===---------------------------------------------------------------------===//
+
+MachineFunction::MachineFunction(const Function *F,
+                                 const TargetMachine& target)
+  : Annotation(MF_AID),
+    Fn(F), Target(target), staticStackSize(0),
+    automaticVarsSize(0), regSpillsSize(0),
+    maxOptionalArgsSize(0), maxOptionalNumArgs(0),
+    currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
+    spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
+{
+}
+
+void MachineFunction::dump() const { print(std::cerr); }
+
+void MachineFunction::print(std::ostream &OS) const {
+  OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
+  
+  for (const_iterator BB = begin(); BB != end(); ++BB) {
+    BasicBlock *LBB = BB->getBasicBlock();
+    OS << "\n" << LBB->getName() << " ("
+       << (const void*)BB->getBasicBlock() << "):\n";
+    for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
+      OS << "\t";
+      (*I)->print(OS, Target);
+    }
+  }
+  OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
+}
+
+
 // The next two methods are used to construct and to retrieve
 // the MachineCodeForFunction object for the given function.
 // construct() -- Allocates and initializes for a given function and target
@@ -29,26 +130,25 @@ static AnnotationID MCFM_AID(
 //                for a given Function.
 // 
 MachineFunction&
-MachineFunction::construct(const Function *M, const TargetMachine &Tar)
+MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
 {
-  assert(M->getAnnotation(MCFM_AID) == 0 &&
+  assert(Fn->getAnnotation(MF_AID) == 0 &&
          "Object already exists for this function!");
-  MachineFunction* mcInfo = new MachineFunction(M, Tar);
-  M->addAnnotation(mcInfo);
+  MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
+  Fn->addAnnotation(mcInfo);
   return *mcInfo;
 }
 
 void
-MachineFunction::destruct(const Function *M)
+MachineFunction::destruct(const Function *Fn)
 {
-  bool Deleted = M->deleteAnnotation(MCFM_AID);
+  bool Deleted = Fn->deleteAnnotation(MF_AID);
   assert(Deleted && "Machine code did not exist for function!");
 }
 
-MachineFunction&
-MachineFunction::get(const Function *F)
+MachineFunction& MachineFunction::get(const Function *F)
 {
-  MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
+  MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
   assert(mc && "Call construct() method first to allocate the object");
   return *mc;
 }
@@ -119,20 +219,11 @@ SizeToAlignment(unsigned int size, const TargetMachine& target)
 }
 
 
-/*ctor*/
-MachineFunction::MachineFunction(const Function *F,
-                                           const TargetMachine& target)
-  : Annotation(MCFM_AID),
-    method(F), staticStackSize(0),
-    automaticVarsSize(0), regSpillsSize(0),
-    maxOptionalArgsSize(0), maxOptionalNumArgs(0),
-    currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
-    spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
-{
-  maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
+void MachineFunction::CalculateArgSize() {
+  maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
                                                    maxOptionalNumArgs);
   staticStackSize = maxOptionalArgsSize
-                    + target.getFrameInfo().getMinStackFrameSize();
+    + Target.getFrameInfo().getMinStackFrameSize();
 }
 
 int
@@ -172,8 +263,7 @@ MachineFunction::allocateLocalVar(const TargetMachine& target,
   if (offset == INVALID_FRAME_OFFSET)
     {
       unsigned int getPaddedSize;
-      offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
-                                              sizeToUse);
+      offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
       offsets[val] = offset;
       incrementAutomaticVarsSize(getPaddedSize);
     }
@@ -235,21 +325,5 @@ int
 MachineFunction::getOffset(const Value* val) const
 {
   hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
-  return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
-}
-
-void
-MachineFunction::dump() const
-{
-  std::cerr << "\n" << method->getReturnType()
-            << " \"" << method->getName() << "\"\n";
-  
-  for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
-    {
-      std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
-      MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
-      for (unsigned i=0; i < mvec.size(); i++)
-       std::cerr << "\t" << *mvec[i];
-    } 
-  std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
+  return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
 }