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