1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===//
3 // Collect native machine code information for a method. This allows
4 // target-specific information about the generated code to be stored with each
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H
10 #define LLVM_CODEGEN_MACHINEFUNCTION_H
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"
26 Pass *createMachineCodeConstructionPass(TargetMachine &Target);
27 Pass *createMachineCodeDestructionPass();
28 Pass *createMachineFunctionPrinterPass();
30 class MachineFunction : private Annotation {
32 const TargetMachine &Target;
34 // List of machine basic blocks in function
35 iplist<MachineBasicBlock> BasicBlocks;
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;
48 bool spillsAreaFrozen;
49 bool automaticVarsAreaFrozen;
51 // Keeping track of mapping from SSA values to registers
52 SSARegMap *SSARegMapping;
55 MachineFunction(const Function *Fn, const TargetMachine& target);
57 /// getFunction - Return the LLVM function that this machine code represents
59 const Function *getFunction() const { return Fn; }
61 /// getTarget - Return the target machine this machine code is compiled with
63 const TargetMachine &getTarget() const { return Target; }
65 /// print - Print out the MachineFunction in a format suitable for debugging
66 /// to the specified stream.
68 void print(std::ostream &OS) const;
70 /// dump - Print the current MachineFunction to cerr, useful for debugger use.
74 /// CalculateArgSize - Call this method to fill in the maxOptionalArgsSize &
75 /// staticStackSize fields...
77 void CalculateArgSize();
79 // The next three methods are used to construct, destruct, and retrieve the
80 // MachineFunction object for the given method.
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
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);
93 // Getting and storing SSARegMap information
94 const TargetRegisterClass* getRegClass(unsigned Reg) {
95 return SSARegMapping->getRegClass(Reg);
97 void addRegMap(unsigned Reg, const TargetRegisterClass *RegClass) {
98 SSARegMapping->addRegMap(Reg, RegClass);
100 void clearSSARegMap() {
101 delete SSARegMapping;
102 SSARegMapping = NULL;
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;
112 // Provide accessors for basic blocks...
113 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
114 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
116 //===--------------------------------------------------------------------===//
117 // BasicBlock iterator forwarding functions
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(); }
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(); }
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(); }
136 //===--------------------------------------------------------------------===//
138 // FIXME: Most of the following state should be moved out to passes that use
139 // it, instead of being put here.
143 // Accessors for global information about generated code for a method.
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;}
155 // Modifiers used during code generation
157 void initializeFrameLayout (const TargetMachine& target);
159 void addToConstantPool (const Constant* constVal)
160 { constantsForConstPool.insert(constVal); }
162 inline void markAsLeafMethod() { compiledAsLeaf = true; }
164 int computeOffsetforLocalVar (const TargetMachine& target,
166 unsigned int& getPaddedSize,
167 unsigned int sizeToUse = 0);
168 int allocateLocalVar (const TargetMachine& target,
170 unsigned int sizeToUse = 0);
172 int allocateSpilledValue (const TargetMachine& target,
175 int pushTempValue (const TargetMachine& target,
178 void popAllTempValues (const TargetMachine& target);
180 void freezeSpillsArea () { spillsAreaFrozen = true; }
181 void freezeAutomaticVarsArea () { automaticVarsAreaFrozen=true; }
183 int getOffset (const Value* val) const;
185 // int getOffsetFromFP (const Value* val) const;
188 inline void incrementAutomaticVarsSize(int incr) {
189 automaticVarsSize+= incr;
190 staticStackSize += incr;
192 inline void incrementRegSpillsSize(int incr) {
193 regSpillsSize+= incr;
194 staticStackSize += incr;
196 inline void incrementTmpAreaSize(int incr) {
197 currentTmpValuesSize += incr;
198 if (maxTmpValuesSize < currentTmpValuesSize)
200 staticStackSize += currentTmpValuesSize - maxTmpValuesSize;
201 maxTmpValuesSize = currentTmpValuesSize;
204 inline void resetTmpAreaSize() {
205 currentTmpValuesSize = 0;
207 int allocateOptionalArg (const TargetMachine& target,