1 //===-- MachineCodeForMethod.cpp -------------------------------------------=//
4 // Collect native machine code information for a function.
5 // This allows target-specific information about the generated code
6 // to be stored with each function.
7 //===---------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineFunction.h"
10 #include "llvm/CodeGen/MachineInstr.h" // For debug output
11 #include "llvm/CodeGen/MachineCodeForBasicBlock.h"
12 #include "llvm/CodeGen/MachineCodeForInstruction.h"
13 #include "llvm/Target/TargetMachine.h"
14 #include "llvm/Target/MachineFrameInfo.h"
15 #include "llvm/Target/MachineCacheInfo.h"
16 #include "llvm/Function.h"
17 #include "llvm/iOther.h"
18 #include "llvm/Pass.h"
21 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
23 static AnnotationID MCFM_AID(
24 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
27 //===---------------------------------------------------------------------===//
28 // Code generation/destruction passes
29 //===---------------------------------------------------------------------===//
32 class ConstructMachineFunction : public FunctionPass {
33 TargetMachine &Target;
35 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
37 const char *getPassName() const {
38 return "ConstructMachineFunction";
41 bool runOnFunction(Function &F) {
42 MachineFunction::construct(&F, Target);
47 struct DestroyMachineFunction : public FunctionPass {
48 const char *getPassName() const { return "FreeMachineFunction"; }
50 static void freeMachineCode(Instruction &I) {
51 MachineCodeForInstruction::destroy(&I);
54 bool runOnFunction(Function &F) {
55 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
56 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
57 MachineCodeForInstruction::get(I).dropAllReferences();
59 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
60 for_each(FI->begin(), FI->end(), freeMachineCode);
67 Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
68 return new ConstructMachineFunction(Target);
71 Pass *createMachineCodeDestructionPass() {
72 return new DestroyMachineFunction();
76 //===---------------------------------------------------------------------===//
77 // MachineFunction implementation
78 //===---------------------------------------------------------------------===//
80 // The next two methods are used to construct and to retrieve
81 // the MachineCodeForFunction object for the given function.
82 // construct() -- Allocates and initializes for a given function and target
83 // get() -- Returns a handle to the object.
84 // This should not be called before "construct()"
85 // for a given Function.
88 MachineFunction::construct(const Function *M, const TargetMachine &Tar)
90 assert(M->getAnnotation(MCFM_AID) == 0 &&
91 "Object already exists for this function!");
92 MachineFunction* mcInfo = new MachineFunction(M, Tar);
93 M->addAnnotation(mcInfo);
98 MachineFunction::destruct(const Function *M)
100 bool Deleted = M->deleteAnnotation(MCFM_AID);
101 assert(Deleted && "Machine code did not exist for function!");
105 MachineFunction::get(const Function *F)
107 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
108 assert(mc && "Call construct() method first to allocate the object");
113 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
114 unsigned &maxOptionalNumArgs)
116 const MachineFrameInfo& frameInfo = target.getFrameInfo();
118 unsigned maxSize = 0;
120 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
121 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
122 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
124 unsigned numOperands = callInst->getNumOperands() - 1;
125 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
129 unsigned int sizeForThisCall;
130 if (frameInfo.argsOnStackHaveFixedSize())
132 int argSize = frameInfo.getSizeOfEachArgOnStack();
133 sizeForThisCall = numExtra * (unsigned) argSize;
137 assert(0 && "UNTESTED CODE: Size per stack argument is not "
138 "fixed on this architecture: use actual arg sizes to "
139 "compute MaxOptionalArgsSize");
141 for (unsigned i = 0; i < numOperands; ++i)
142 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
143 getOperand(i)->getType());
146 if (maxSize < sizeForThisCall)
147 maxSize = sizeForThisCall;
149 if ((int)maxOptionalNumArgs < numExtra)
150 maxOptionalNumArgs = (unsigned) numExtra;
156 // Align data larger than one L1 cache line on L1 cache line boundaries.
157 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
158 // but not higher than the alignment of the largest type we support
159 // (currently a double word). -- see class TargetData).
161 // This function is similar to the corresponding function in EmitAssembly.cpp
162 // but they are unrelated. This one does not align at more than a
163 // double-word boundary whereas that one might.
166 SizeToAlignment(unsigned int size, const TargetMachine& target)
168 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
169 if (size > (unsigned) cacheLineSize / 2)
170 return cacheLineSize;
172 for (unsigned sz=1; /*no condition*/; sz *= 2)
173 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
179 MachineFunction::MachineFunction(const Function *F,
180 const TargetMachine& target)
181 : Annotation(MCFM_AID),
182 method(F), staticStackSize(0),
183 automaticVarsSize(0), regSpillsSize(0),
184 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
185 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
186 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
188 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
190 staticStackSize = maxOptionalArgsSize
191 + target.getFrameInfo().getMinStackFrameSize();
195 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
197 unsigned int& getPaddedSize,
198 unsigned int sizeToUse)
201 sizeToUse = target.findOptimalStorageSize(val->getType());
202 unsigned int align = SizeToAlignment(sizeToUse, target);
205 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
207 int offset = growUp? firstOffset + getAutomaticVarsSize()
208 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
210 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
211 getPaddedSize = sizeToUse + abs(aligned - offset);
217 MachineFunction::allocateLocalVar(const TargetMachine& target,
219 unsigned int sizeToUse)
221 assert(! automaticVarsAreaFrozen &&
222 "Size of auto vars area has been used to compute an offset so "
223 "no more automatic vars should be allocated!");
225 // Check if we've allocated a stack slot for this value already
227 int offset = getOffset(val);
228 if (offset == INVALID_FRAME_OFFSET)
230 unsigned int getPaddedSize;
231 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
233 offsets[val] = offset;
234 incrementAutomaticVarsSize(getPaddedSize);
240 MachineFunction::allocateSpilledValue(const TargetMachine& target,
243 assert(! spillsAreaFrozen &&
244 "Size of reg spills area has been used to compute an offset so "
245 "no more register spill slots should be allocated!");
247 unsigned int size = target.DataLayout.getTypeSize(type);
248 unsigned char align = target.DataLayout.getTypeAlignment(type);
251 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
253 int offset = growUp? firstOffset + getRegSpillsSize()
254 : firstOffset - (getRegSpillsSize() + size);
256 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
257 size += abs(aligned - offset); // include alignment padding in size
259 incrementRegSpillsSize(size); // update size of reg. spills area
265 MachineFunction::pushTempValue(const TargetMachine& target,
268 unsigned int align = SizeToAlignment(size, target);
271 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
273 int offset = growUp? firstOffset + currentTmpValuesSize
274 : firstOffset - (currentTmpValuesSize + size);
276 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
277 size += abs(aligned - offset); // include alignment padding in size
279 incrementTmpAreaSize(size); // update "current" size of tmp area
285 MachineFunction::popAllTempValues(const TargetMachine& target)
287 resetTmpAreaSize(); // clear tmp area to reuse
291 MachineFunction::getOffset(const Value* val) const
293 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
294 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
298 MachineFunction::dump() const
300 std::cerr << "\n" << method->getReturnType()
301 << " \"" << method->getName() << "\"\n";
303 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
305 std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
306 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
307 for (unsigned i=0; i < mvec.size(); i++)
308 std::cerr << "\t" << *mvec[i];
310 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";