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 MF_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).CalculateArgSize();
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);
65 struct Printer : public FunctionPass {
66 const char *getPassName() const { return "MachineFunction Printer"; }
68 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
72 bool runOnFunction(Function &F) {
73 MachineFunction::get(&F).dump();
79 Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
80 return new ConstructMachineFunction(Target);
83 Pass *createMachineCodeDestructionPass() {
84 return new DestroyMachineFunction();
87 Pass *createMachineFunctionPrinterPass() {
92 //===---------------------------------------------------------------------===//
93 // MachineFunction implementation
94 //===---------------------------------------------------------------------===//
96 MachineFunction::MachineFunction(const Function *F,
97 const TargetMachine& target)
99 Fn(F), Target(target), staticStackSize(0),
100 automaticVarsSize(0), regSpillsSize(0),
101 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
102 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
103 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
107 void MachineFunction::dump() const { print(std::cerr); }
109 void MachineFunction::print(std::ostream &OS) const {
110 OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
112 for (const_iterator BB = begin(); BB != end(); ++BB) {
113 BasicBlock *LBB = BB->getBasicBlock();
114 OS << "\n" << LBB->getName() << " ("
115 << (const void*)BB->getBasicBlock() << "):\n";
116 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
118 (*I)->print(OS, Target);
121 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
125 // The next two methods are used to construct and to retrieve
126 // the MachineCodeForFunction object for the given function.
127 // construct() -- Allocates and initializes for a given function and target
128 // get() -- Returns a handle to the object.
129 // This should not be called before "construct()"
130 // for a given Function.
133 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
135 assert(Fn->getAnnotation(MF_AID) == 0 &&
136 "Object already exists for this function!");
137 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
138 Fn->addAnnotation(mcInfo);
143 MachineFunction::destruct(const Function *Fn)
145 bool Deleted = Fn->deleteAnnotation(MF_AID);
146 assert(Deleted && "Machine code did not exist for function!");
149 MachineFunction& MachineFunction::get(const Function *F)
151 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
152 assert(mc && "Call construct() method first to allocate the object");
157 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
158 unsigned &maxOptionalNumArgs)
160 const MachineFrameInfo& frameInfo = target.getFrameInfo();
162 unsigned maxSize = 0;
164 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
165 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
166 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
168 unsigned numOperands = callInst->getNumOperands() - 1;
169 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
173 unsigned int sizeForThisCall;
174 if (frameInfo.argsOnStackHaveFixedSize())
176 int argSize = frameInfo.getSizeOfEachArgOnStack();
177 sizeForThisCall = numExtra * (unsigned) argSize;
181 assert(0 && "UNTESTED CODE: Size per stack argument is not "
182 "fixed on this architecture: use actual arg sizes to "
183 "compute MaxOptionalArgsSize");
185 for (unsigned i = 0; i < numOperands; ++i)
186 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
187 getOperand(i)->getType());
190 if (maxSize < sizeForThisCall)
191 maxSize = sizeForThisCall;
193 if ((int)maxOptionalNumArgs < numExtra)
194 maxOptionalNumArgs = (unsigned) numExtra;
200 // Align data larger than one L1 cache line on L1 cache line boundaries.
201 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
202 // but not higher than the alignment of the largest type we support
203 // (currently a double word). -- see class TargetData).
205 // This function is similar to the corresponding function in EmitAssembly.cpp
206 // but they are unrelated. This one does not align at more than a
207 // double-word boundary whereas that one might.
210 SizeToAlignment(unsigned int size, const TargetMachine& target)
212 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
213 if (size > (unsigned) cacheLineSize / 2)
214 return cacheLineSize;
216 for (unsigned sz=1; /*no condition*/; sz *= 2)
217 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
222 void MachineFunction::CalculateArgSize() {
223 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
225 staticStackSize = maxOptionalArgsSize
226 + Target.getFrameInfo().getMinStackFrameSize();
230 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
232 unsigned int& getPaddedSize,
233 unsigned int sizeToUse)
236 sizeToUse = target.findOptimalStorageSize(val->getType());
237 unsigned int align = SizeToAlignment(sizeToUse, target);
240 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
242 int offset = growUp? firstOffset + getAutomaticVarsSize()
243 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
245 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
246 getPaddedSize = sizeToUse + abs(aligned - offset);
252 MachineFunction::allocateLocalVar(const TargetMachine& target,
254 unsigned int sizeToUse)
256 assert(! automaticVarsAreaFrozen &&
257 "Size of auto vars area has been used to compute an offset so "
258 "no more automatic vars should be allocated!");
260 // Check if we've allocated a stack slot for this value already
262 int offset = getOffset(val);
263 if (offset == INVALID_FRAME_OFFSET)
265 unsigned int getPaddedSize;
266 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
267 offsets[val] = offset;
268 incrementAutomaticVarsSize(getPaddedSize);
274 MachineFunction::allocateSpilledValue(const TargetMachine& target,
277 assert(! spillsAreaFrozen &&
278 "Size of reg spills area has been used to compute an offset so "
279 "no more register spill slots should be allocated!");
281 unsigned int size = target.DataLayout.getTypeSize(type);
282 unsigned char align = target.DataLayout.getTypeAlignment(type);
285 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
287 int offset = growUp? firstOffset + getRegSpillsSize()
288 : firstOffset - (getRegSpillsSize() + size);
290 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
291 size += abs(aligned - offset); // include alignment padding in size
293 incrementRegSpillsSize(size); // update size of reg. spills area
299 MachineFunction::pushTempValue(const TargetMachine& target,
302 unsigned int align = SizeToAlignment(size, target);
305 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
307 int offset = growUp? firstOffset + currentTmpValuesSize
308 : firstOffset - (currentTmpValuesSize + size);
310 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
311 size += abs(aligned - offset); // include alignment padding in size
313 incrementTmpAreaSize(size); // update "current" size of tmp area
319 MachineFunction::popAllTempValues(const TargetMachine& target)
321 resetTmpAreaSize(); // clear tmp area to reuse
325 MachineFunction::getOffset(const Value* val) const
327 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
328 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;