1 //===-- MachineFunction.cpp -----------------------------------------------===//
3 // Collect native machine code information for a function. This allows
4 // target-specific information about the generated code to be stored with each
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineInstr.h" // For debug output
10 #include "llvm/CodeGen/MachineFunction.h"
11 #include "llvm/CodeGen/MachineCodeForInstruction.h"
12 #include "llvm/Target/TargetMachine.h"
13 #include "llvm/Target/MachineFrameInfo.h"
14 #include "llvm/Target/MachineCacheInfo.h"
15 #include "llvm/Function.h"
16 #include "llvm/iOther.h"
17 #include "llvm/Pass.h"
20 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
22 static AnnotationID MCFM_AID(
23 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
26 //===---------------------------------------------------------------------===//
27 // Code generation/destruction passes
28 //===---------------------------------------------------------------------===//
31 class ConstructMachineFunction : public FunctionPass {
32 TargetMachine &Target;
34 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
36 const char *getPassName() const {
37 return "ConstructMachineFunction";
40 bool runOnFunction(Function &F) {
41 MachineFunction::construct(&F, Target);
46 struct DestroyMachineFunction : public FunctionPass {
47 const char *getPassName() const { return "FreeMachineFunction"; }
49 static void freeMachineCode(Instruction &I) {
50 MachineCodeForInstruction::destroy(&I);
53 bool runOnFunction(Function &F) {
54 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
55 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
56 MachineCodeForInstruction::get(I).dropAllReferences();
58 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
59 for_each(FI->begin(), FI->end(), freeMachineCode);
66 Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
67 return new ConstructMachineFunction(Target);
70 Pass *createMachineCodeDestructionPass() {
71 return new DestroyMachineFunction();
75 //===---------------------------------------------------------------------===//
76 // MachineFunction implementation
77 //===---------------------------------------------------------------------===//
79 // The next two methods are used to construct and to retrieve
80 // the MachineCodeForFunction object for the given function.
81 // construct() -- Allocates and initializes for a given function and target
82 // get() -- Returns a handle to the object.
83 // This should not be called before "construct()"
84 // for a given Function.
87 MachineFunction::construct(const Function *M, const TargetMachine &Tar)
89 assert(M->getAnnotation(MCFM_AID) == 0 &&
90 "Object already exists for this function!");
91 MachineFunction* mcInfo = new MachineFunction(M, Tar);
92 M->addAnnotation(mcInfo);
97 MachineFunction::destruct(const Function *M)
99 bool Deleted = M->deleteAnnotation(MCFM_AID);
100 assert(Deleted && "Machine code did not exist for function!");
104 MachineFunction::get(const Function *F)
106 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
107 assert(mc && "Call construct() method first to allocate the object");
112 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
113 unsigned &maxOptionalNumArgs)
115 const MachineFrameInfo& frameInfo = target.getFrameInfo();
117 unsigned maxSize = 0;
119 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
120 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
121 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
123 unsigned numOperands = callInst->getNumOperands() - 1;
124 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
128 unsigned int sizeForThisCall;
129 if (frameInfo.argsOnStackHaveFixedSize())
131 int argSize = frameInfo.getSizeOfEachArgOnStack();
132 sizeForThisCall = numExtra * (unsigned) argSize;
136 assert(0 && "UNTESTED CODE: Size per stack argument is not "
137 "fixed on this architecture: use actual arg sizes to "
138 "compute MaxOptionalArgsSize");
140 for (unsigned i = 0; i < numOperands; ++i)
141 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
142 getOperand(i)->getType());
145 if (maxSize < sizeForThisCall)
146 maxSize = sizeForThisCall;
148 if ((int)maxOptionalNumArgs < numExtra)
149 maxOptionalNumArgs = (unsigned) numExtra;
155 // Align data larger than one L1 cache line on L1 cache line boundaries.
156 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
157 // but not higher than the alignment of the largest type we support
158 // (currently a double word). -- see class TargetData).
160 // This function is similar to the corresponding function in EmitAssembly.cpp
161 // but they are unrelated. This one does not align at more than a
162 // double-word boundary whereas that one might.
165 SizeToAlignment(unsigned int size, const TargetMachine& target)
167 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
168 if (size > (unsigned) cacheLineSize / 2)
169 return cacheLineSize;
171 for (unsigned sz=1; /*no condition*/; sz *= 2)
172 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
178 MachineFunction::MachineFunction(const Function *F,
179 const TargetMachine& target)
180 : Annotation(MCFM_AID),
181 method(F), staticStackSize(0),
182 automaticVarsSize(0), regSpillsSize(0),
183 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
184 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
185 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
187 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
189 staticStackSize = maxOptionalArgsSize
190 + target.getFrameInfo().getMinStackFrameSize();
194 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
196 unsigned int& getPaddedSize,
197 unsigned int sizeToUse)
200 sizeToUse = target.findOptimalStorageSize(val->getType());
201 unsigned int align = SizeToAlignment(sizeToUse, target);
204 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
206 int offset = growUp? firstOffset + getAutomaticVarsSize()
207 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
209 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
210 getPaddedSize = sizeToUse + abs(aligned - offset);
216 MachineFunction::allocateLocalVar(const TargetMachine& target,
218 unsigned int sizeToUse)
220 assert(! automaticVarsAreaFrozen &&
221 "Size of auto vars area has been used to compute an offset so "
222 "no more automatic vars should be allocated!");
224 // Check if we've allocated a stack slot for this value already
226 int offset = getOffset(val);
227 if (offset == INVALID_FRAME_OFFSET)
229 unsigned int getPaddedSize;
230 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
231 offsets[val] = offset;
232 incrementAutomaticVarsSize(getPaddedSize);
238 MachineFunction::allocateSpilledValue(const TargetMachine& target,
241 assert(! spillsAreaFrozen &&
242 "Size of reg spills area has been used to compute an offset so "
243 "no more register spill slots should be allocated!");
245 unsigned int size = target.DataLayout.getTypeSize(type);
246 unsigned char align = target.DataLayout.getTypeAlignment(type);
249 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
251 int offset = growUp? firstOffset + getRegSpillsSize()
252 : firstOffset - (getRegSpillsSize() + size);
254 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
255 size += abs(aligned - offset); // include alignment padding in size
257 incrementRegSpillsSize(size); // update size of reg. spills area
263 MachineFunction::pushTempValue(const TargetMachine& target,
266 unsigned int align = SizeToAlignment(size, target);
269 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
271 int offset = growUp? firstOffset + currentTmpValuesSize
272 : firstOffset - (currentTmpValuesSize + size);
274 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
275 size += abs(aligned - offset); // include alignment padding in size
277 incrementTmpAreaSize(size); // update "current" size of tmp area
283 MachineFunction::popAllTempValues(const TargetMachine& target)
285 resetTmpAreaSize(); // clear tmp area to reuse
289 MachineFunction::getOffset(const Value* val) const
291 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
292 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
296 MachineFunction::dump() const
298 std::cerr << "\n" << method->getReturnType()
299 << " \"" << method->getName() << "\"\n";
301 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
303 std::cerr << "\n" << BB->getName() << " (" << (const void*)BB
304 << ")" << ":" << "\n";
305 MachineBasicBlock& mvec = MachineBasicBlock::get(BB);
306 for (unsigned i=0; i < mvec.size(); i++)
307 std::cerr << "\t" << *mvec[i];
309 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";