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/Annotation.h"
14 #include "Support/HashExtras.h"
15 #include "Support/hash_set"
16 #include "Support/ilist"
25 Pass *createMachineCodeConstructionPass(TargetMachine &Target);
26 Pass *createMachineCodeDestructionPass();
28 class MachineFunction : private Annotation {
30 const TargetMachine &Target;
32 // List of machine basic blocks in function
33 iplist<MachineBasicBlock> BasicBlocks;
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;
46 bool spillsAreaFrozen;
47 bool automaticVarsAreaFrozen;
50 MachineFunction(const Function *Fn, const TargetMachine& target);
53 /// CalculateArgSize - Call this method to fill in the maxOptionalArgsSize &
54 /// staticStackSize fields...
56 void CalculateArgSize();
58 /// getFunction - Return the LLVM function that this machine code represents
60 const Function *getFunction() const { return Fn; }
62 /// getTarget - Return the target machine this machine code is compiled with
64 const TargetMachine &getTarget() const { return Target; }
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.
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);
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;
85 // Provide accessors for basic blocks...
86 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
87 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
89 //===--------------------------------------------------------------------===//
90 // BasicBlock iterator forwarding functions
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(); }
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(); }
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(); }
109 //===--------------------------------------------------------------------===//
111 // FIXME: Most of the following state should be moved out to passes that use
112 // it, instead of being put here.
116 // Accessors for global information about generated code for a method.
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;}
128 // Modifiers used during code generation
130 void initializeFrameLayout (const TargetMachine& target);
132 void addToConstantPool (const Constant* constVal)
133 { constantsForConstPool.insert(constVal); }
135 inline void markAsLeafMethod() { compiledAsLeaf = true; }
137 int computeOffsetforLocalVar (const TargetMachine& target,
139 unsigned int& getPaddedSize,
140 unsigned int sizeToUse = 0);
141 int allocateLocalVar (const TargetMachine& target,
143 unsigned int sizeToUse = 0);
145 int allocateSpilledValue (const TargetMachine& target,
148 int pushTempValue (const TargetMachine& target,
151 void popAllTempValues (const TargetMachine& target);
153 void freezeSpillsArea () { spillsAreaFrozen = true; }
154 void freezeAutomaticVarsArea () { automaticVarsAreaFrozen=true; }
156 int getOffset (const Value* val) const;
158 // int getOffsetFromFP (const Value* val) const;
163 inline void incrementAutomaticVarsSize(int incr) {
164 automaticVarsSize+= incr;
165 staticStackSize += incr;
167 inline void incrementRegSpillsSize(int incr) {
168 regSpillsSize+= incr;
169 staticStackSize += incr;
171 inline void incrementTmpAreaSize(int incr) {
172 currentTmpValuesSize += incr;
173 if (maxTmpValuesSize < currentTmpValuesSize)
175 staticStackSize += currentTmpValuesSize - maxTmpValuesSize;
176 maxTmpValuesSize = currentTmpValuesSize;
179 inline void resetTmpAreaSize() {
180 currentTmpValuesSize = 0;
182 int allocateOptionalArg (const TargetMachine& target,