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