f4a5180e32238d98f254f7f2c430b4222822beca
[oota-llvm.git] / include / llvm / CodeGen / MachineFunction.h
1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
2 // 
3 // Collect native machine code information for a method.  This allows
4 // target-specific information about the generated code to be stored with each
5 // method.
6 //   
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
10 #define LLVM_CODEGEN_MACHINEFUNCTION_H
11
12 #include "llvm/CodeGen/MachineBasicBlock.h"
13 #include "llvm/CodeGen/SSARegMap.h"
14 #include "llvm/Annotation.h"
15 #include "Support/HashExtras.h"
16 #include "Support/hash_set"
17 #include "Support/ilist"
18
19 class Value;
20 class Function;
21 class Constant;
22 class Type;
23 class TargetMachine;
24 class Pass;
25
26 Pass *createMachineCodeConstructionPass(TargetMachine &Target);
27 Pass *createMachineCodeDestructionPass();
28 Pass *createMachineFunctionPrinterPass();
29
30 class MachineFunction : private Annotation {
31   const Function *Fn;
32   const TargetMachine &Target;
33
34   // List of machine basic blocks in function
35   iplist<MachineBasicBlock> BasicBlocks;
36
37   // FIXME: State should be held elsewhere...
38   hash_set<const Constant*> constantsForConstPool;
39   hash_map<const Value*, int> offsets;
40   unsigned      staticStackSize;
41   unsigned      automaticVarsSize;
42   unsigned      regSpillsSize;
43   unsigned      maxOptionalArgsSize;
44   unsigned      maxOptionalNumArgs;
45   unsigned      currentTmpValuesSize;
46   unsigned      maxTmpValuesSize;
47   bool          compiledAsLeaf;
48   bool          spillsAreaFrozen;
49   bool          automaticVarsAreaFrozen;
50
51   // Keeping track of mapping from SSA values to registers
52   SSARegMap *SSARegMapping;
53   
54 public:
55   MachineFunction(const Function *Fn, const TargetMachine& target);
56
57   /// getFunction - Return the LLVM function that this machine code represents
58   ///
59   const Function *getFunction() const { return Fn; }
60
61   /// getTarget - Return the target machine this machine code is compiled with
62   ///
63   const TargetMachine &getTarget() const { return Target; }
64
65   /// print - Print out the MachineFunction in a format suitable for debugging
66   /// to the specified stream.
67   ///
68   void print(std::ostream &OS) const;
69
70   /// dump - Print the current MachineFunction to cerr, useful for debugger use.
71   ///
72   void dump() const;
73
74   /// CalculateArgSize - Call this method to fill in the maxOptionalArgsSize &
75   /// staticStackSize fields...
76   ///
77   void CalculateArgSize();
78
79   // The next three methods are used to construct, destruct, and retrieve the
80   // MachineFunction object for the given method.
81   //
82   // construct() -- Allocates and initializes for a given method and target
83   // get()       -- Returns a handle to the object.
84   //                This should not be called before "construct()"
85   //                for a given Method.
86   // destruct()  -- Destroy the MachineFunction object
87   // 
88   static MachineFunction& construct(const Function *Fn,
89                                     const TargetMachine &target);
90   static void destruct(const Function *F);
91   static MachineFunction& get(const Function *F);
92
93   // Getting and storing SSARegMap information
94   const TargetRegisterClass* getRegClass(unsigned Reg) { 
95     return SSARegMapping->getRegClass(Reg);
96   }
97   void addRegMap(unsigned Reg, const TargetRegisterClass *RegClass) {
98     SSARegMapping->addRegMap(Reg, RegClass);
99   }
100   void clearSSARegMap() { delete SSARegMapping; }
101
102   // Provide accessors for the MachineBasicBlock list...
103   typedef iplist<MachineBasicBlock> BasicBlockListType;
104   typedef BasicBlockListType::iterator iterator;
105   typedef BasicBlockListType::const_iterator const_iterator;
106   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
107   typedef std::reverse_iterator<iterator>             reverse_iterator;
108
109   // Provide accessors for basic blocks...
110   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
111         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
112  
113   //===--------------------------------------------------------------------===//
114   // BasicBlock iterator forwarding functions
115   //
116   iterator                 begin()       { return BasicBlocks.begin(); }
117   const_iterator           begin() const { return BasicBlocks.begin(); }
118   iterator                 end  ()       { return BasicBlocks.end();   }
119   const_iterator           end  () const { return BasicBlocks.end();   }
120
121   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
122   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
123   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
124   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
125
126   unsigned                  size() const { return BasicBlocks.size(); }
127   bool                     empty() const { return BasicBlocks.empty(); }
128   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
129         MachineBasicBlock &front()       { return BasicBlocks.front(); }
130   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
131         MachineBasicBlock & back()       { return BasicBlocks.back(); }
132
133   //===--------------------------------------------------------------------===//
134   //
135   // FIXME: Most of the following state should be moved out to passes that use
136   // it, instead of being put here.
137   //
138
139   //
140   // Accessors for global information about generated code for a method.
141   // 
142   inline bool     isCompiledAsLeafMethod() const { return compiledAsLeaf; }
143   inline unsigned getStaticStackSize()     const { return staticStackSize; }
144   inline unsigned getAutomaticVarsSize()   const { return automaticVarsSize; }
145   inline unsigned getRegSpillsSize()       const { return regSpillsSize; }
146   inline unsigned getMaxOptionalArgsSize() const { return maxOptionalArgsSize;}
147   inline unsigned getMaxOptionalNumArgs()  const { return maxOptionalNumArgs;}
148   inline const hash_set<const Constant*>&
149                   getConstantPoolValues() const {return constantsForConstPool;}
150   
151   //
152   // Modifiers used during code generation
153   // 
154   void            initializeFrameLayout    (const TargetMachine& target);
155   
156   void            addToConstantPool        (const Constant* constVal)
157                                     { constantsForConstPool.insert(constVal); }
158   
159   inline void     markAsLeafMethod()              { compiledAsLeaf = true; }
160   
161   int             computeOffsetforLocalVar (const TargetMachine& target,
162                                             const Value*  local,
163                                             unsigned int& getPaddedSize,
164                                             unsigned int  sizeToUse = 0);
165   int             allocateLocalVar         (const TargetMachine& target,
166                                             const Value* local,
167                                             unsigned int sizeToUse = 0);
168   
169   int             allocateSpilledValue     (const TargetMachine& target,
170                                             const Type* type);
171   
172   int             pushTempValue            (const TargetMachine& target,
173                                             unsigned int size);
174   
175   void            popAllTempValues         (const TargetMachine& target);
176   
177   void            freezeSpillsArea         () { spillsAreaFrozen = true; } 
178   void            freezeAutomaticVarsArea  () { automaticVarsAreaFrozen=true; }
179   
180   int             getOffset                (const Value* val) const;
181   
182   // int          getOffsetFromFP       (const Value* val) const;
183   
184 private:
185   inline void     incrementAutomaticVarsSize(int incr) {
186     automaticVarsSize+= incr;
187     staticStackSize += incr;
188   }
189   inline void     incrementRegSpillsSize(int incr) {
190     regSpillsSize+= incr;
191     staticStackSize += incr;
192   }
193   inline void     incrementTmpAreaSize(int incr) {
194     currentTmpValuesSize += incr;
195     if (maxTmpValuesSize < currentTmpValuesSize)
196       {
197         staticStackSize += currentTmpValuesSize - maxTmpValuesSize;
198         maxTmpValuesSize = currentTmpValuesSize;
199       }
200   }
201   inline void     resetTmpAreaSize() {
202     currentTmpValuesSize = 0;
203   }
204   int             allocateOptionalArg      (const TargetMachine& target,
205                                             const Type* type);
206 };
207
208 #endif