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);
52 /// getFunction - Return the LLVM function that this machine code represents
54 const Function *getFunction() const { return Fn; }
56 /// getTarget - Return the target machine this machine code is compiled with
58 const TargetMachine &getTarget() const { return Target; }
60 // The next two methods are used to construct and to retrieve
61 // the MachineFunction object for the given method.
62 // construct() -- Allocates and initializes for a given method and target
63 // get() -- Returns a handle to the object.
64 // This should not be called before "construct()"
65 // for a given Method.
67 static MachineFunction& construct(const Function *Fn,
68 const TargetMachine &target);
69 static void destruct(const Function *F);
70 static MachineFunction& get(const Function *F);
72 // Provide accessors for the MachineBasicBlock list...
73 typedef iplist<MachineBasicBlock> BasicBlockListType;
74 typedef BasicBlockListType::iterator iterator;
75 typedef BasicBlockListType::const_iterator const_iterator;
76 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
77 typedef std::reverse_iterator<iterator> reverse_iterator;
79 // Provide accessors for basic blocks...
80 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
81 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
83 //===--------------------------------------------------------------------===//
84 // BasicBlock iterator forwarding functions
86 iterator begin() { return BasicBlocks.begin(); }
87 const_iterator begin() const { return BasicBlocks.begin(); }
88 iterator end () { return BasicBlocks.end(); }
89 const_iterator end () const { return BasicBlocks.end(); }
91 reverse_iterator rbegin() { return BasicBlocks.rbegin(); }
92 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
93 reverse_iterator rend () { return BasicBlocks.rend(); }
94 const_reverse_iterator rend () const { return BasicBlocks.rend(); }
96 unsigned size() const { return BasicBlocks.size(); }
97 bool empty() const { return BasicBlocks.empty(); }
98 const MachineBasicBlock &front() const { return BasicBlocks.front(); }
99 MachineBasicBlock &front() { return BasicBlocks.front(); }
100 const MachineBasicBlock & back() const { return BasicBlocks.back(); }
101 MachineBasicBlock & back() { return BasicBlocks.back(); }
103 //===--------------------------------------------------------------------===//
105 // FIXME: Most of the following state should be moved out to passes that use
106 // it, instead of being put here.
110 // Accessors for global information about generated code for a method.
112 inline bool isCompiledAsLeafMethod() const { return compiledAsLeaf; }
113 inline unsigned getStaticStackSize() const { return staticStackSize; }
114 inline unsigned getAutomaticVarsSize() const { return automaticVarsSize; }
115 inline unsigned getRegSpillsSize() const { return regSpillsSize; }
116 inline unsigned getMaxOptionalArgsSize() const { return maxOptionalArgsSize;}
117 inline unsigned getMaxOptionalNumArgs() const { return maxOptionalNumArgs;}
118 inline const hash_set<const Constant*>&
119 getConstantPoolValues() const {return constantsForConstPool;}
122 // Modifiers used during code generation
124 void initializeFrameLayout (const TargetMachine& target);
126 void addToConstantPool (const Constant* constVal)
127 { constantsForConstPool.insert(constVal); }
129 inline void markAsLeafMethod() { compiledAsLeaf = true; }
131 int computeOffsetforLocalVar (const TargetMachine& target,
133 unsigned int& getPaddedSize,
134 unsigned int sizeToUse = 0);
135 int allocateLocalVar (const TargetMachine& target,
137 unsigned int sizeToUse = 0);
139 int allocateSpilledValue (const TargetMachine& target,
142 int pushTempValue (const TargetMachine& target,
145 void popAllTempValues (const TargetMachine& target);
147 void freezeSpillsArea () { spillsAreaFrozen = true; }
148 void freezeAutomaticVarsArea () { automaticVarsAreaFrozen=true; }
150 int getOffset (const Value* val) const;
152 // int getOffsetFromFP (const Value* val) const;
157 inline void incrementAutomaticVarsSize(int incr) {
158 automaticVarsSize+= incr;
159 staticStackSize += incr;
161 inline void incrementRegSpillsSize(int incr) {
162 regSpillsSize+= incr;
163 staticStackSize += incr;
165 inline void incrementTmpAreaSize(int incr) {
166 currentTmpValuesSize += incr;
167 if (maxTmpValuesSize < currentTmpValuesSize)
169 staticStackSize += currentTmpValuesSize - maxTmpValuesSize;
170 maxTmpValuesSize = currentTmpValuesSize;
173 inline void resetTmpAreaSize() {
174 currentTmpValuesSize = 0;
176 int allocateOptionalArg (const TargetMachine& target,