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/MachineCodeForMethod.h"
10 #include "llvm/CodeGen/MachineInstr.h" // For debug output
11 #include "llvm/CodeGen/MachineCodeForBasicBlock.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/BasicBlock.h"
17 #include "llvm/iOther.h"
21 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
23 static AnnotationID MCFM_AID(
24 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
26 // The next two methods are used to construct and to retrieve
27 // the MachineCodeForFunction object for the given function.
28 // construct() -- Allocates and initializes for a given function and target
29 // get() -- Returns a handle to the object.
30 // This should not be called before "construct()"
31 // for a given Function.
34 MachineCodeForMethod::construct(const Function *M, const TargetMachine &Tar)
36 assert(M->getAnnotation(MCFM_AID) == 0 &&
37 "Object already exists for this function!");
38 MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
39 M->addAnnotation(mcInfo);
44 MachineCodeForMethod::destruct(const Function *M)
46 bool Deleted = M->deleteAnnotation(MCFM_AID);
47 assert(Deleted && "Machine code did not exist for function!");
51 MachineCodeForMethod::get(const Function *F)
53 MachineCodeForMethod *mc = (MachineCodeForMethod*)F->getAnnotation(MCFM_AID);
54 assert(mc && "Call construct() method first to allocate the object");
59 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
60 unsigned &maxOptionalNumArgs)
62 const MachineFrameInfo& frameInfo = target.getFrameInfo();
66 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
67 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
68 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
70 unsigned numOperands = callInst->getNumOperands() - 1;
71 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
75 unsigned int sizeForThisCall;
76 if (frameInfo.argsOnStackHaveFixedSize())
78 int argSize = frameInfo.getSizeOfEachArgOnStack();
79 sizeForThisCall = numExtra * (unsigned) argSize;
83 assert(0 && "UNTESTED CODE: Size per stack argument is not "
84 "fixed on this architecture: use actual arg sizes to "
85 "compute MaxOptionalArgsSize");
87 for (unsigned i = 0; i < numOperands; ++i)
88 sizeForThisCall += target.findOptimalStorageSize(callInst->
89 getOperand(i)->getType());
92 if (maxSize < sizeForThisCall)
93 maxSize = sizeForThisCall;
95 if ((int)maxOptionalNumArgs < numExtra)
96 maxOptionalNumArgs = (unsigned) numExtra;
102 // Align data larger than one L1 cache line on L1 cache line boundaries.
103 // Align all smaller data on the next higher 2^x boundary (4, 8, ...).
105 // THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
106 // SHOULD BE USED DIRECTLY THERE
109 SizeToAlignment(unsigned int size, const TargetMachine& target)
111 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
112 if (size > (unsigned) cacheLineSize / 2)
113 return cacheLineSize;
115 for (unsigned sz=1; /*no condition*/; sz *= 2)
123 MachineCodeForMethod::MachineCodeForMethod(const Function *F,
124 const TargetMachine& target)
125 : Annotation(MCFM_AID),
126 method(F), staticStackSize(0),
127 automaticVarsSize(0), regSpillsSize(0),
128 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
129 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
130 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
132 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
134 staticStackSize = maxOptionalArgsSize
135 + target.getFrameInfo().getMinStackFrameSize();
139 MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
141 unsigned int& getPaddedSize,
142 unsigned int sizeToUse = 0)
145 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
150 sizeToUse = target.findOptimalStorageSize(val->getType());
151 // align = target.DataLayout.getTypeAlignment(val->getType());
154 align = SizeToAlignment(sizeToUse, target);
156 int offset = getAutomaticVarsSize();
160 if (unsigned int mod = offset % align)
162 offset += align - mod;
163 getPaddedSize = sizeToUse + align - mod;
166 getPaddedSize = sizeToUse;
168 offset = growUp? firstOffset + offset
169 : firstOffset - offset;
175 MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
177 unsigned int sizeToUse = 0)
179 assert(! automaticVarsAreaFrozen &&
180 "Size of auto vars area has been used to compute an offset so "
181 "no more automatic vars should be allocated!");
183 // Check if we've allocated a stack slot for this value already
185 int offset = getOffset(val);
186 if (offset == INVALID_FRAME_OFFSET)
188 unsigned int getPaddedSize;
189 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
191 offsets[val] = offset;
192 incrementAutomaticVarsSize(getPaddedSize);
198 MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
201 assert(! spillsAreaFrozen &&
202 "Size of reg spills area has been used to compute an offset so "
203 "no more register spill slots should be allocated!");
205 unsigned int size = target.findOptimalStorageSize(type);
206 unsigned char align = target.DataLayout.getTypeAlignment(type);
209 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
211 int offset = getRegSpillsSize();
215 if (unsigned int mod = offset % align)
217 offset += align - mod;
221 offset = growUp? firstOffset + offset
222 : firstOffset - offset;
224 incrementRegSpillsSize(size);
230 MachineCodeForMethod::pushTempValue(const TargetMachine& target,
233 // Compute a power-of-2 alignment according to the possible sizes,
234 // but not greater than the alignment of the largest type we support
235 // (currently a double word -- see class TargetData).
236 unsigned char align = 1;
237 for (; align < size && align < target.DataLayout.getDoubleAlignment();
242 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
244 int offset = currentTmpValuesSize;
248 if (unsigned int mod = offset % align)
250 offset += align - mod;
254 offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
256 incrementTmpAreaSize(size);
261 MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
267 MachineCodeForMethod::getOffset(const Value* val) const
269 std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
270 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
274 MachineCodeForMethod::dump() const
276 std::cerr << "\n" << method->getReturnType()
277 << " \"" << method->getName() << "\"\n";
279 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
281 std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
282 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
283 for (unsigned i=0; i < mvec.size(); i++)
284 std::cerr << "\t" << *mvec[i];
286 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";