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 *Fn, const TargetMachine &Tar)
89 assert(Fn->getAnnotation(MCFM_AID) == 0 &&
90 "Object already exists for this function!");
91 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
92 Fn->addAnnotation(mcInfo);
97 MachineFunction::destruct(const Function *Fn)
99 bool Deleted = Fn->deleteAnnotation(MCFM_AID);
100 assert(Deleted && "Machine code did not exist for function!");
103 MachineFunction& MachineFunction::get(const Function *F)
105 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
106 assert(mc && "Call construct() method first to allocate the object");
111 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
112 unsigned &maxOptionalNumArgs)
114 const MachineFrameInfo& frameInfo = target.getFrameInfo();
116 unsigned maxSize = 0;
118 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
119 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
120 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
122 unsigned numOperands = callInst->getNumOperands() - 1;
123 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
127 unsigned int sizeForThisCall;
128 if (frameInfo.argsOnStackHaveFixedSize())
130 int argSize = frameInfo.getSizeOfEachArgOnStack();
131 sizeForThisCall = numExtra * (unsigned) argSize;
135 assert(0 && "UNTESTED CODE: Size per stack argument is not "
136 "fixed on this architecture: use actual arg sizes to "
137 "compute MaxOptionalArgsSize");
139 for (unsigned i = 0; i < numOperands; ++i)
140 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
141 getOperand(i)->getType());
144 if (maxSize < sizeForThisCall)
145 maxSize = sizeForThisCall;
147 if ((int)maxOptionalNumArgs < numExtra)
148 maxOptionalNumArgs = (unsigned) numExtra;
154 // Align data larger than one L1 cache line on L1 cache line boundaries.
155 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
156 // but not higher than the alignment of the largest type we support
157 // (currently a double word). -- see class TargetData).
159 // This function is similar to the corresponding function in EmitAssembly.cpp
160 // but they are unrelated. This one does not align at more than a
161 // double-word boundary whereas that one might.
164 SizeToAlignment(unsigned int size, const TargetMachine& target)
166 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
167 if (size > (unsigned) cacheLineSize / 2)
168 return cacheLineSize;
170 for (unsigned sz=1; /*no condition*/; sz *= 2)
171 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
177 MachineFunction::MachineFunction(const Function *F,
178 const TargetMachine& target)
179 : Annotation(MCFM_AID),
180 Fn(F), Target(target), staticStackSize(0),
181 automaticVarsSize(0), regSpillsSize(0),
182 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
183 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
184 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
186 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, Fn,
188 staticStackSize = maxOptionalArgsSize
189 + target.getFrameInfo().getMinStackFrameSize();
193 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
195 unsigned int& getPaddedSize,
196 unsigned int sizeToUse)
199 sizeToUse = target.findOptimalStorageSize(val->getType());
200 unsigned int align = SizeToAlignment(sizeToUse, target);
203 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
205 int offset = growUp? firstOffset + getAutomaticVarsSize()
206 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
208 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
209 getPaddedSize = sizeToUse + abs(aligned - offset);
215 MachineFunction::allocateLocalVar(const TargetMachine& target,
217 unsigned int sizeToUse)
219 assert(! automaticVarsAreaFrozen &&
220 "Size of auto vars area has been used to compute an offset so "
221 "no more automatic vars should be allocated!");
223 // Check if we've allocated a stack slot for this value already
225 int offset = getOffset(val);
226 if (offset == INVALID_FRAME_OFFSET)
228 unsigned int getPaddedSize;
229 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
230 offsets[val] = offset;
231 incrementAutomaticVarsSize(getPaddedSize);
237 MachineFunction::allocateSpilledValue(const TargetMachine& target,
240 assert(! spillsAreaFrozen &&
241 "Size of reg spills area has been used to compute an offset so "
242 "no more register spill slots should be allocated!");
244 unsigned int size = target.DataLayout.getTypeSize(type);
245 unsigned char align = target.DataLayout.getTypeAlignment(type);
248 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
250 int offset = growUp? firstOffset + getRegSpillsSize()
251 : firstOffset - (getRegSpillsSize() + size);
253 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
254 size += abs(aligned - offset); // include alignment padding in size
256 incrementRegSpillsSize(size); // update size of reg. spills area
262 MachineFunction::pushTempValue(const TargetMachine& target,
265 unsigned int align = SizeToAlignment(size, target);
268 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
270 int offset = growUp? firstOffset + currentTmpValuesSize
271 : firstOffset - (currentTmpValuesSize + size);
273 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
274 size += abs(aligned - offset); // include alignment padding in size
276 incrementTmpAreaSize(size); // update "current" size of tmp area
282 MachineFunction::popAllTempValues(const TargetMachine& target)
284 resetTmpAreaSize(); // clear tmp area to reuse
288 MachineFunction::getOffset(const Value* val) const
290 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
291 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
295 MachineFunction::dump() const
297 std::cerr << "\n" << Fn->getReturnType()
298 << " \"" << Fn->getName() << "\"\n";
300 for (const_iterator BB = begin(); BB != end(); ++BB) {
301 std::cerr << "\n" << BB->getBasicBlock()->getName() << " ("
302 << (const void*)BB->getBasicBlock() << ")" << ":" << "\n";
303 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end(); ++I)
304 std::cerr << "\t" << *I;
306 std::cerr << "\nEnd function \"" << Fn->getName() << "\"\n\n";