Set SSARegMap to NULL after deleting it.
[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() { 
101     delete SSARegMapping;
102     SSARegMapping = NULL;
103   }
104
105   // Provide accessors for the MachineBasicBlock list...
106   typedef iplist<MachineBasicBlock> BasicBlockListType;
107   typedef BasicBlockListType::iterator iterator;
108   typedef BasicBlockListType::const_iterator const_iterator;
109   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
110   typedef std::reverse_iterator<iterator>             reverse_iterator;
111
112   // Provide accessors for basic blocks...
113   const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
114         BasicBlockListType &getBasicBlockList()       { return BasicBlocks; }
115  
116   //===--------------------------------------------------------------------===//
117   // BasicBlock iterator forwarding functions
118   //
119   iterator                 begin()       { return BasicBlocks.begin(); }
120   const_iterator           begin() const { return BasicBlocks.begin(); }
121   iterator                 end  ()       { return BasicBlocks.end();   }
122   const_iterator           end  () const { return BasicBlocks.end();   }
123
124   reverse_iterator        rbegin()       { return BasicBlocks.rbegin(); }
125   const_reverse_iterator  rbegin() const { return BasicBlocks.rbegin(); }
126   reverse_iterator        rend  ()       { return BasicBlocks.rend();   }
127   const_reverse_iterator  rend  () const { return BasicBlocks.rend();   }
128
129   unsigned                  size() const { return BasicBlocks.size(); }
130   bool                     empty() const { return BasicBlocks.empty(); }
131   const MachineBasicBlock &front() const { return BasicBlocks.front(); }
132         MachineBasicBlock &front()       { return BasicBlocks.front(); }
133   const MachineBasicBlock & back() const { return BasicBlocks.back(); }
134         MachineBasicBlock & back()       { return BasicBlocks.back(); }
135
136   //===--------------------------------------------------------------------===//
137   //
138   // FIXME: Most of the following state should be moved out to passes that use
139   // it, instead of being put here.
140   //
141
142   //
143   // Accessors for global information about generated code for a method.
144   // 
145   inline bool     isCompiledAsLeafMethod() const { return compiledAsLeaf; }
146   inline unsigned getStaticStackSize()     const { return staticStackSize; }
147   inline unsigned getAutomaticVarsSize()   const { return automaticVarsSize; }
148   inline unsigned getRegSpillsSize()       const { return regSpillsSize; }
149   inline unsigned getMaxOptionalArgsSize() const { return maxOptionalArgsSize;}
150   inline unsigned getMaxOptionalNumArgs()  const { return maxOptionalNumArgs;}
151   inline const hash_set<const Constant*>&
152                   getConstantPoolValues() const {return constantsForConstPool;}
153   
154   //
155   // Modifiers used during code generation
156   // 
157   void            initializeFrameLayout    (const TargetMachine& target);
158   
159   void            addToConstantPool        (const Constant* constVal)
160                                     { constantsForConstPool.insert(constVal); }
161   
162   inline void     markAsLeafMethod()              { compiledAsLeaf = true; }
163   
164   int             computeOffsetforLocalVar (const TargetMachine& target,
165                                             const Value*  local,
166                                             unsigned int& getPaddedSize,
167                                             unsigned int  sizeToUse = 0);
168   int             allocateLocalVar         (const TargetMachine& target,
169                                             const Value* local,
170                                             unsigned int sizeToUse = 0);
171   
172   int             allocateSpilledValue     (const TargetMachine& target,
173                                             const Type* type);
174   
175   int             pushTempValue            (const TargetMachine& target,
176                                             unsigned int size);
177   
178   void            popAllTempValues         (const TargetMachine& target);
179   
180   void            freezeSpillsArea         () { spillsAreaFrozen = true; } 
181   void            freezeAutomaticVarsArea  () { automaticVarsAreaFrozen=true; }
182   
183   int             getOffset                (const Value* val) const;
184   
185   // int          getOffsetFromFP       (const Value* val) const;
186   
187 private:
188   inline void     incrementAutomaticVarsSize(int incr) {
189     automaticVarsSize+= incr;
190     staticStackSize += incr;
191   }
192   inline void     incrementRegSpillsSize(int incr) {
193     regSpillsSize+= incr;
194     staticStackSize += incr;
195   }
196   inline void     incrementTmpAreaSize(int incr) {
197     currentTmpValuesSize += incr;
198     if (maxTmpValuesSize < currentTmpValuesSize)
199       {
200         staticStackSize += currentTmpValuesSize - maxTmpValuesSize;
201         maxTmpValuesSize = currentTmpValuesSize;
202       }
203   }
204   inline void     resetTmpAreaSize() {
205     currentTmpValuesSize = 0;
206   }
207   int             allocateOptionalArg      (const TargetMachine& target,
208                                             const Type* type);
209 };
210
211 #endif