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 // get - This deprecated static method returns the MachineBasicBlock object
76 // for the specified BasicBlock.
78 MachineBasicBlock& MachineBasicBlock::get(const BasicBlock *BB) {
79 const Function *F = BB->getParent();
80 MachineFunction &MF = MachineFunction::get(F);
82 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
83 if (I->getBasicBlock() == BB)
85 assert(0 && "MachineBasicBlock object not found for specified block!");
90 //===---------------------------------------------------------------------===//
91 // MachineFunction implementation
92 //===---------------------------------------------------------------------===//
94 // The next two methods are used to construct and to retrieve
95 // the MachineCodeForFunction object for the given function.
96 // construct() -- Allocates and initializes for a given function and target
97 // get() -- Returns a handle to the object.
98 // This should not be called before "construct()"
99 // for a given Function.
102 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
104 assert(Fn->getAnnotation(MCFM_AID) == 0 &&
105 "Object already exists for this function!");
106 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
107 Fn->addAnnotation(mcInfo);
112 MachineFunction::destruct(const Function *Fn)
114 bool Deleted = Fn->deleteAnnotation(MCFM_AID);
115 assert(Deleted && "Machine code did not exist for function!");
118 MachineFunction& MachineFunction::get(const Function *F)
120 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
121 assert(mc && "Call construct() method first to allocate the object");
126 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
127 unsigned &maxOptionalNumArgs)
129 const MachineFrameInfo& frameInfo = target.getFrameInfo();
131 unsigned maxSize = 0;
133 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
134 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
135 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
137 unsigned numOperands = callInst->getNumOperands() - 1;
138 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
142 unsigned int sizeForThisCall;
143 if (frameInfo.argsOnStackHaveFixedSize())
145 int argSize = frameInfo.getSizeOfEachArgOnStack();
146 sizeForThisCall = numExtra * (unsigned) argSize;
150 assert(0 && "UNTESTED CODE: Size per stack argument is not "
151 "fixed on this architecture: use actual arg sizes to "
152 "compute MaxOptionalArgsSize");
154 for (unsigned i = 0; i < numOperands; ++i)
155 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
156 getOperand(i)->getType());
159 if (maxSize < sizeForThisCall)
160 maxSize = sizeForThisCall;
162 if ((int)maxOptionalNumArgs < numExtra)
163 maxOptionalNumArgs = (unsigned) numExtra;
169 // Align data larger than one L1 cache line on L1 cache line boundaries.
170 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
171 // but not higher than the alignment of the largest type we support
172 // (currently a double word). -- see class TargetData).
174 // This function is similar to the corresponding function in EmitAssembly.cpp
175 // but they are unrelated. This one does not align at more than a
176 // double-word boundary whereas that one might.
179 SizeToAlignment(unsigned int size, const TargetMachine& target)
181 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
182 if (size > (unsigned) cacheLineSize / 2)
183 return cacheLineSize;
185 for (unsigned sz=1; /*no condition*/; sz *= 2)
186 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
192 MachineFunction::MachineFunction(const Function *F,
193 const TargetMachine& target)
194 : Annotation(MCFM_AID),
195 Fn(F), Target(target), staticStackSize(0),
196 automaticVarsSize(0), regSpillsSize(0),
197 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
198 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
199 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
201 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, Fn,
203 staticStackSize = maxOptionalArgsSize
204 + target.getFrameInfo().getMinStackFrameSize();
208 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
210 unsigned int& getPaddedSize,
211 unsigned int sizeToUse)
214 sizeToUse = target.findOptimalStorageSize(val->getType());
215 unsigned int align = SizeToAlignment(sizeToUse, target);
218 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
220 int offset = growUp? firstOffset + getAutomaticVarsSize()
221 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
223 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
224 getPaddedSize = sizeToUse + abs(aligned - offset);
230 MachineFunction::allocateLocalVar(const TargetMachine& target,
232 unsigned int sizeToUse)
234 assert(! automaticVarsAreaFrozen &&
235 "Size of auto vars area has been used to compute an offset so "
236 "no more automatic vars should be allocated!");
238 // Check if we've allocated a stack slot for this value already
240 int offset = getOffset(val);
241 if (offset == INVALID_FRAME_OFFSET)
243 unsigned int getPaddedSize;
244 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
245 offsets[val] = offset;
246 incrementAutomaticVarsSize(getPaddedSize);
252 MachineFunction::allocateSpilledValue(const TargetMachine& target,
255 assert(! spillsAreaFrozen &&
256 "Size of reg spills area has been used to compute an offset so "
257 "no more register spill slots should be allocated!");
259 unsigned int size = target.DataLayout.getTypeSize(type);
260 unsigned char align = target.DataLayout.getTypeAlignment(type);
263 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
265 int offset = growUp? firstOffset + getRegSpillsSize()
266 : firstOffset - (getRegSpillsSize() + size);
268 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
269 size += abs(aligned - offset); // include alignment padding in size
271 incrementRegSpillsSize(size); // update size of reg. spills area
277 MachineFunction::pushTempValue(const TargetMachine& target,
280 unsigned int align = SizeToAlignment(size, target);
283 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
285 int offset = growUp? firstOffset + currentTmpValuesSize
286 : firstOffset - (currentTmpValuesSize + size);
288 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
289 size += abs(aligned - offset); // include alignment padding in size
291 incrementTmpAreaSize(size); // update "current" size of tmp area
297 MachineFunction::popAllTempValues(const TargetMachine& target)
299 resetTmpAreaSize(); // clear tmp area to reuse
303 MachineFunction::getOffset(const Value* val) const
305 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
306 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
310 MachineFunction::dump() const
312 std::cerr << "\n" << Fn->getReturnType()
313 << " \"" << Fn->getName() << "\"\n";
315 for (const_iterator BB = begin(); BB != end(); ++BB) {
316 std::cerr << "\n" << BB->getBasicBlock()->getName() << " ("
317 << (const void*)BB->getBasicBlock() << ")" << ":" << "\n";
318 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end(); ++I)
319 std::cerr << "\t" << *I;
321 std::cerr << "\nEnd function \"" << Fn->getName() << "\"\n\n";