add some helpers
[oota-llvm.git] / lib / CodeGen / MachineFunction.cpp
index 205414e7264cc3c4b0d0b7b614a547e2be40efa3..29eee3612f64dad64499983cfa14cf017696ea19 100644 (file)
@@ -13,6 +13,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/DerivedTypes.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/SSARegMap.h"
@@ -21,6 +22,7 @@
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetLowering.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Target/TargetFrameInfo.h"
 #include "llvm/Function.h"
@@ -31,9 +33,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Config/config.h"
 #include <fstream>
-#include <iostream>
 #include <sstream>
-
 using namespace llvm;
 
 static AnnotationID MF_AID(
@@ -114,12 +114,21 @@ void ilist_traits<MachineBasicBlock>::transferNodesFromList(
 
 MachineFunction::MachineFunction(const Function *F,
                                  const TargetMachine &TM)
-  : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
+  : Annotation(MF_AID), Fn(F), Target(TM) {
   SSARegMapping = new SSARegMap();
   MFInfo = 0;
   FrameInfo = new MachineFrameInfo();
   ConstantPool = new MachineConstantPool(TM.getTargetData());
-  JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
+  UsedPhysRegs.resize(TM.getRegisterInfo()->getNumRegs());
+  
+  // Set up jump table.
+  const TargetData &TD = *TM.getTargetData();
+  bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
+  unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
+  unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
+                             : TD.getPointerABIAlignment();
+  JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
+  
   BasicBlocks.Parent = this;
 }
 
@@ -130,7 +139,6 @@ MachineFunction::~MachineFunction() {
   delete FrameInfo;
   delete ConstantPool;
   delete JumpTableInfo;
-  delete[] UsedPhysRegs;
 }
 
 
@@ -177,7 +185,7 @@ void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
 }
 
 
-void MachineFunction::dump() const { print(std::cerr); }
+void MachineFunction::dump() const { print(*cerr.stream()); }
 
 void MachineFunction::print(std::ostream &OS) const {
   OS << "# Machine code for " << Fn->getName () << "():\n";
@@ -268,8 +276,8 @@ void MachineFunction::viewCFG() const
 #ifndef NDEBUG
   ViewGraph(this, "mf" + getFunction()->getName());
 #else
-  std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
-            << "systems with Graphviz or gv!\n";
+  cerr << "SelectionDAG::viewGraph is only available in debug builds on "
+       << "systems with Graphviz or gv!\n";
 #endif // NDEBUG
 }
 
@@ -334,7 +342,7 @@ void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
     if (i < NumFixedObjects)
       OS << " fixed";
     if (i < NumFixedObjects || SO.SPOffset != -1) {
-      int Off = SO.SPOffset - ValOffset;
+      int64_t Off = SO.SPOffset - ValOffset;
       OS << " at location [SP";
       if (Off > 0)
         OS << "+" << Off;
@@ -350,7 +358,7 @@ void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
 }
 
 void MachineFrameInfo::dump(const MachineFunction &MF) const {
-  print(MF, std::cerr);
+  print(MF, *cerr.stream());
 }
 
 
@@ -362,7 +370,8 @@ void MachineFrameInfo::dump(const MachineFunction &MF) const {
 /// or return an existing one.
 ///
 unsigned MachineJumpTableInfo::getJumpTableIndex(
-                                     std::vector<MachineBasicBlock*> &DestBBs) {
+                               const std::vector<MachineBasicBlock*> &DestBBs) {
+  assert(!DestBBs.empty() && "Cannot create an empty jump table!");
   for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
     if (JumpTables[i].MBBs == DestBBs)
       return i;
@@ -381,15 +390,7 @@ void MachineJumpTableInfo::print(std::ostream &OS) const {
   }
 }
 
-unsigned MachineJumpTableInfo::getEntrySize() const { 
-  return TD->getPointerSize(); 
-}
-
-unsigned MachineJumpTableInfo::getAlignment() const { 
-  return TD->getPointerAlignment(); 
-}
-
-void MachineJumpTableInfo::dump() const { print(std::cerr); }
+void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
 
 
 //===----------------------------------------------------------------------===//
@@ -467,9 +468,9 @@ void MachineConstantPool::print(std::ostream &OS) const {
       Constants[i].Val.MachineCPVal->print(OS);
     else
       OS << *(Value*)Constants[i].Val.ConstVal;
-    OS << " , offset=" << Constants[i].Offset;
+    OS << " , offset=" << Constants[i].getOffset();
     OS << "\n";
   }
 }
 
-void MachineConstantPool::dump() const { print(std::cerr); }
+void MachineConstantPool::dump() const { print(*cerr.stream()); }