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/NonCopyable.h"
15 #include "Support/HashExtras.h"
16 #include "Support/hash_set"
17 #include "Support/ilist"
26 Pass *createMachineCodeConstructionPass(TargetMachine &Target);
27 Pass *createMachineCodeDestructionPass();
29 class MachineFunction : private Annotation {
31 const TargetMachine &Target;
33 // List of machine basic blocks in function
34 iplist<MachineBasicBlock> BasicBlocks;
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;
47 bool spillsAreaFrozen;
48 bool automaticVarsAreaFrozen;
51 MachineFunction(const Function *Fn, const TargetMachine& target);
53 /// getFunction - Return the LLVM function that this machine code represents
55 const Function *getFunction() const { return Fn; }
57 /// getTarget - Return the target machine this machine code is compiled with
59 const TargetMachine &getTarget() const { return Target; }
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.
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);
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;
80 // Provide accessors for basic blocks...
81 const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; }
82 BasicBlockListType &getBasicBlockList() { return BasicBlocks; }
84 //===--------------------------------------------------------------------===//
85 // BasicBlock iterator forwarding functions
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(); }
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(); }
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(); }
104 //===--------------------------------------------------------------------===//
106 // FIXME: Most of the following state should be moved out to passes that use
107 // it, instead of being put here.
111 // Accessors for global information about generated code for a method.
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;}
123 // Modifiers used during code generation
125 void initializeFrameLayout (const TargetMachine& target);
127 void addToConstantPool (const Constant* constVal)
128 { constantsForConstPool.insert(constVal); }
130 inline void markAsLeafMethod() { compiledAsLeaf = true; }
132 int computeOffsetforLocalVar (const TargetMachine& target,
134 unsigned int& getPaddedSize,
135 unsigned int sizeToUse = 0);
136 int allocateLocalVar (const TargetMachine& target,
138 unsigned int sizeToUse = 0);
140 int allocateSpilledValue (const TargetMachine& target,
143 int pushTempValue (const TargetMachine& target,
146 void popAllTempValues (const TargetMachine& target);
148 void freezeSpillsArea () { spillsAreaFrozen = true; }
149 void freezeAutomaticVarsArea () { automaticVarsAreaFrozen=true; }
151 int getOffset (const Value* val) const;
153 // int getOffsetFromFP (const Value* val) const;
158 inline void incrementAutomaticVarsSize(int incr) {
159 automaticVarsSize+= incr;
160 staticStackSize += incr;
162 inline void incrementRegSpillsSize(int incr) {
163 regSpillsSize+= incr;
164 staticStackSize += incr;
166 inline void incrementTmpAreaSize(int incr) {
167 currentTmpValuesSize += incr;
168 if (maxTmpValuesSize < currentTmpValuesSize)
170 staticStackSize += currentTmpValuesSize - maxTmpValuesSize;
171 maxTmpValuesSize = currentTmpValuesSize;
174 inline void resetTmpAreaSize() {
175 currentTmpValuesSize = 0;
177 int allocateOptionalArg (const TargetMachine& target,