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)
105 SSARegMapping = new SSARegMap();
108 void MachineFunction::dump() const { print(std::cerr); }
110 void MachineFunction::print(std::ostream &OS) const {
111 OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
113 for (const_iterator BB = begin(); BB != end(); ++BB) {
114 BasicBlock *LBB = BB->getBasicBlock();
115 OS << "\n" << LBB->getName() << " ("
116 << (const void*)BB->getBasicBlock() << "):\n";
117 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
119 (*I)->print(OS, Target);
122 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
126 // The next two methods are used to construct and to retrieve
127 // the MachineCodeForFunction object for the given function.
128 // construct() -- Allocates and initializes for a given function and target
129 // get() -- Returns a handle to the object.
130 // This should not be called before "construct()"
131 // for a given Function.
134 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
136 assert(Fn->getAnnotation(MF_AID) == 0 &&
137 "Object already exists for this function!");
138 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
139 Fn->addAnnotation(mcInfo);
144 MachineFunction::destruct(const Function *Fn)
146 bool Deleted = Fn->deleteAnnotation(MF_AID);
147 assert(Deleted && "Machine code did not exist for function!");
150 MachineFunction& MachineFunction::get(const Function *F)
152 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
153 assert(mc && "Call construct() method first to allocate the object");
158 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
159 unsigned &maxOptionalNumArgs)
161 const MachineFrameInfo& frameInfo = target.getFrameInfo();
163 unsigned maxSize = 0;
165 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
166 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
167 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
169 unsigned numOperands = callInst->getNumOperands() - 1;
170 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
174 unsigned int sizeForThisCall;
175 if (frameInfo.argsOnStackHaveFixedSize())
177 int argSize = frameInfo.getSizeOfEachArgOnStack();
178 sizeForThisCall = numExtra * (unsigned) argSize;
182 assert(0 && "UNTESTED CODE: Size per stack argument is not "
183 "fixed on this architecture: use actual arg sizes to "
184 "compute MaxOptionalArgsSize");
186 for (unsigned i = 0; i < numOperands; ++i)
187 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
188 getOperand(i)->getType());
191 if (maxSize < sizeForThisCall)
192 maxSize = sizeForThisCall;
194 if ((int)maxOptionalNumArgs < numExtra)
195 maxOptionalNumArgs = (unsigned) numExtra;
201 // Align data larger than one L1 cache line on L1 cache line boundaries.
202 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
203 // but not higher than the alignment of the largest type we support
204 // (currently a double word). -- see class TargetData).
206 // This function is similar to the corresponding function in EmitAssembly.cpp
207 // but they are unrelated. This one does not align at more than a
208 // double-word boundary whereas that one might.
211 SizeToAlignment(unsigned int size, const TargetMachine& target)
213 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
214 if (size > (unsigned) cacheLineSize / 2)
215 return cacheLineSize;
217 for (unsigned sz=1; /*no condition*/; sz *= 2)
218 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
223 void MachineFunction::CalculateArgSize() {
224 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
226 staticStackSize = maxOptionalArgsSize
227 + Target.getFrameInfo().getMinStackFrameSize();
231 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
233 unsigned int& getPaddedSize,
234 unsigned int sizeToUse)
237 sizeToUse = target.findOptimalStorageSize(val->getType());
238 unsigned int align = SizeToAlignment(sizeToUse, target);
241 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
243 int offset = growUp? firstOffset + getAutomaticVarsSize()
244 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
246 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
247 getPaddedSize = sizeToUse + abs(aligned - offset);
253 MachineFunction::allocateLocalVar(const TargetMachine& target,
255 unsigned int sizeToUse)
257 assert(! automaticVarsAreaFrozen &&
258 "Size of auto vars area has been used to compute an offset so "
259 "no more automatic vars should be allocated!");
261 // Check if we've allocated a stack slot for this value already
263 int offset = getOffset(val);
264 if (offset == INVALID_FRAME_OFFSET)
266 unsigned int getPaddedSize;
267 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
268 offsets[val] = offset;
269 incrementAutomaticVarsSize(getPaddedSize);
275 MachineFunction::allocateSpilledValue(const TargetMachine& target,
278 assert(! spillsAreaFrozen &&
279 "Size of reg spills area has been used to compute an offset so "
280 "no more register spill slots should be allocated!");
282 unsigned int size = target.DataLayout.getTypeSize(type);
283 unsigned char align = target.DataLayout.getTypeAlignment(type);
286 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
288 int offset = growUp? firstOffset + getRegSpillsSize()
289 : firstOffset - (getRegSpillsSize() + size);
291 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
292 size += abs(aligned - offset); // include alignment padding in size
294 incrementRegSpillsSize(size); // update size of reg. spills area
300 MachineFunction::pushTempValue(const TargetMachine& target,
303 unsigned int align = SizeToAlignment(size, target);
306 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
308 int offset = growUp? firstOffset + currentTmpValuesSize
309 : firstOffset - (currentTmpValuesSize + size);
311 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
312 size += abs(aligned - offset); // include alignment padding in size
314 incrementTmpAreaSize(size); // update "current" size of tmp area
320 MachineFunction::popAllTempValues(const TargetMachine& target)
322 resetTmpAreaSize(); // clear tmp area to reuse
326 MachineFunction::getOffset(const Value* val) const
328 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
329 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;