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/MachineFunction.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/CodeGen/MachineCodeForInstruction.h"
12 #include "llvm/CodeGen/SSARegMap.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 MF_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).CalculateArgSize();
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);
66 struct Printer : public FunctionPass {
67 const char *getPassName() const { return "MachineFunction Printer"; }
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73 bool runOnFunction(Function &F) {
74 MachineFunction::get(&F).dump();
80 Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
81 return new ConstructMachineFunction(Target);
84 Pass *createMachineCodeDestructionPass() {
85 return new DestroyMachineFunction();
88 Pass *createMachineFunctionPrinterPass() {
93 //===---------------------------------------------------------------------===//
94 // MachineFunction implementation
95 //===---------------------------------------------------------------------===//
97 MachineFunction::MachineFunction(const Function *F,
98 const TargetMachine& target)
99 : Annotation(MF_AID), Fn(F), Target(target) {
100 SSARegMapping = new SSARegMap();
102 // FIXME: move state into another class
103 staticStackSize = automaticVarsSize = regSpillsSize = 0;
104 maxOptionalArgsSize = maxOptionalNumArgs = currentTmpValuesSize = 0;
105 maxTmpValuesSize = 0;
106 compiledAsLeaf = spillsAreaFrozen = automaticVarsAreaFrozen = false;
109 MachineFunction::~MachineFunction() {
110 delete SSARegMapping;
113 void MachineFunction::dump() const { print(std::cerr); }
115 void MachineFunction::print(std::ostream &OS) const {
116 OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
118 for (const_iterator BB = begin(); BB != end(); ++BB) {
119 BasicBlock *LBB = BB->getBasicBlock();
120 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
121 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
123 (*I)->print(OS, Target);
126 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
130 // The next two methods are used to construct and to retrieve
131 // the MachineCodeForFunction object for the given function.
132 // construct() -- Allocates and initializes for a given function and target
133 // get() -- Returns a handle to the object.
134 // This should not be called before "construct()"
135 // for a given Function.
138 MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
140 assert(Fn->getAnnotation(MF_AID) == 0 &&
141 "Object already exists for this function!");
142 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
143 Fn->addAnnotation(mcInfo);
148 MachineFunction::destruct(const Function *Fn)
150 bool Deleted = Fn->deleteAnnotation(MF_AID);
151 assert(Deleted && "Machine code did not exist for function!");
154 MachineFunction& MachineFunction::get(const Function *F)
156 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
157 assert(mc && "Call construct() method first to allocate the object");
161 void MachineFunction::clearSSARegMap() {
162 delete SSARegMapping;
168 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
169 unsigned &maxOptionalNumArgs)
171 const MachineFrameInfo& frameInfo = target.getFrameInfo();
173 unsigned maxSize = 0;
175 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
176 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
177 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
179 unsigned numOperands = callInst->getNumOperands() - 1;
180 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
184 unsigned int sizeForThisCall;
185 if (frameInfo.argsOnStackHaveFixedSize())
187 int argSize = frameInfo.getSizeOfEachArgOnStack();
188 sizeForThisCall = numExtra * (unsigned) argSize;
192 assert(0 && "UNTESTED CODE: Size per stack argument is not "
193 "fixed on this architecture: use actual arg sizes to "
194 "compute MaxOptionalArgsSize");
196 for (unsigned i = 0; i < numOperands; ++i)
197 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
198 getOperand(i)->getType());
201 if (maxSize < sizeForThisCall)
202 maxSize = sizeForThisCall;
204 if ((int)maxOptionalNumArgs < numExtra)
205 maxOptionalNumArgs = (unsigned) numExtra;
211 // Align data larger than one L1 cache line on L1 cache line boundaries.
212 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
213 // but not higher than the alignment of the largest type we support
214 // (currently a double word). -- see class TargetData).
216 // This function is similar to the corresponding function in EmitAssembly.cpp
217 // but they are unrelated. This one does not align at more than a
218 // double-word boundary whereas that one might.
221 SizeToAlignment(unsigned int size, const TargetMachine& target)
223 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
224 if (size > (unsigned) cacheLineSize / 2)
225 return cacheLineSize;
227 for (unsigned sz=1; /*no condition*/; sz *= 2)
228 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
233 void MachineFunction::CalculateArgSize() {
234 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
236 staticStackSize = maxOptionalArgsSize
237 + Target.getFrameInfo().getMinStackFrameSize();
241 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
243 unsigned int& getPaddedSize,
244 unsigned int sizeToUse)
247 sizeToUse = target.findOptimalStorageSize(val->getType());
248 unsigned int align = SizeToAlignment(sizeToUse, target);
251 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
253 int offset = growUp? firstOffset + getAutomaticVarsSize()
254 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
256 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
257 getPaddedSize = sizeToUse + abs(aligned - offset);
263 MachineFunction::allocateLocalVar(const TargetMachine& target,
265 unsigned int sizeToUse)
267 assert(! automaticVarsAreaFrozen &&
268 "Size of auto vars area has been used to compute an offset so "
269 "no more automatic vars should be allocated!");
271 // Check if we've allocated a stack slot for this value already
273 int offset = getOffset(val);
274 if (offset == INVALID_FRAME_OFFSET)
276 unsigned int getPaddedSize;
277 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
278 offsets[val] = offset;
279 incrementAutomaticVarsSize(getPaddedSize);
285 MachineFunction::allocateSpilledValue(const TargetMachine& target,
288 assert(! spillsAreaFrozen &&
289 "Size of reg spills area has been used to compute an offset so "
290 "no more register spill slots should be allocated!");
292 unsigned int size = target.DataLayout.getTypeSize(type);
293 unsigned char align = target.DataLayout.getTypeAlignment(type);
296 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
298 int offset = growUp? firstOffset + getRegSpillsSize()
299 : firstOffset - (getRegSpillsSize() + size);
301 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
302 size += abs(aligned - offset); // include alignment padding in size
304 incrementRegSpillsSize(size); // update size of reg. spills area
310 MachineFunction::pushTempValue(const TargetMachine& target,
313 unsigned int align = SizeToAlignment(size, target);
316 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
318 int offset = growUp? firstOffset + currentTmpValuesSize
319 : firstOffset - (currentTmpValuesSize + size);
321 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
322 size += abs(aligned - offset); // include alignment padding in size
324 incrementTmpAreaSize(size); // update "current" size of tmp area
330 MachineFunction::popAllTempValues(const TargetMachine& target)
332 resetTmpAreaSize(); // clear tmp area to reuse
336 MachineFunction::getOffset(const Value* val) const
338 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
339 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;