1 //===-- MachineCodeForMethod.cpp --------------------------------------------=//
4 // Collect native machine code information for a method.
5 // This allows target-specific information about the generated code
6 // to be stored with each method.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/CodeGen/MachineCodeForMethod.h"
10 #include "llvm/CodeGen/MachineInstr.h" // For debug output
11 #include "llvm/Target/TargetMachine.h"
12 #include "llvm/Target/MachineFrameInfo.h"
13 #include "llvm/Target/MachineCacheInfo.h"
14 #include "llvm/Method.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/iOther.h"
19 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
21 static AnnotationID MCFM_AID(
22 AnnotationManager::getID("CodeGen::MachineCodeForMethod"));
24 // The next two methods are used to construct and to retrieve
25 // the MachineCodeForMethod object for the given method.
26 // construct() -- Allocates and initializes for a given method and target
27 // get() -- Returns a handle to the object.
28 // This should not be called before "construct()"
29 // for a given Method.
31 MachineCodeForMethod &MachineCodeForMethod::construct(const Method *M,
32 const TargetMachine &Tar){
33 assert(M->getAnnotation(MCFM_AID) == 0 &&
34 "Object already exists for this method!");
35 MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
36 M->addAnnotation(mcInfo);
40 void MachineCodeForMethod::destruct(const Method *M) {
41 bool Deleted = M->deleteAnnotation(MCFM_AID);
42 assert(Deleted && "Machine code did not exist for method!");
46 MachineCodeForMethod &MachineCodeForMethod::get(const Method* method) {
47 MachineCodeForMethod* mc = (MachineCodeForMethod*)
48 method->getAnnotation(MCFM_AID);
49 assert(mc && "Call construct() method first to allocate the object");
54 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Method* method)
56 const MachineFrameInfo& frameInfo = target.getFrameInfo();
58 unsigned int maxSize = 0;
60 for (Method::const_iterator MI=method->begin(), ME=method->end();
62 const BasicBlock *BB = *MI;
63 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
64 if (CallInst *callInst = dyn_cast<CallInst>(*I)) {
65 unsigned int numOperands = callInst->getNumOperands() - 1;
66 int numExtra = (int) numOperands - frameInfo.getNumFixedOutgoingArgs();
70 unsigned int sizeForThisCall;
71 if (frameInfo.argsOnStackHaveFixedSize())
73 int argSize = frameInfo.getSizeOfEachArgOnStack();
74 sizeForThisCall = numExtra * (unsigned) argSize;
78 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual arg sizes to compute MaxOptionalArgsSize");
80 for (unsigned i=0; i < numOperands; ++i)
81 sizeForThisCall += target.findOptimalStorageSize(callInst->
82 getOperand(i)->getType());
85 if (maxSize < sizeForThisCall)
86 maxSize = sizeForThisCall;
92 // Align data larger than one L1 cache line on L1 cache line boundaries.
93 // Align all smaller data on the next higher 2^x boundary (4, 8, ...).
95 // THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
96 // SHOULD BE USED DIRECTLY THERE
99 SizeToAlignment(unsigned int size, const TargetMachine& target)
101 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
102 if (size > (unsigned) cacheLineSize / 2)
103 return cacheLineSize;
105 for (unsigned sz=1; /*no condition*/; sz *= 2)
113 MachineCodeForMethod::MachineCodeForMethod(const Method* _M,
114 const TargetMachine& target)
115 : Annotation(MCFM_AID),
116 method(_M), compiledAsLeaf(false), staticStackSize(0),
117 automaticVarsSize(0), regSpillsSize(0),
118 currentOptionalArgsSize(0), maxOptionalArgsSize(0),
119 currentTmpValuesSize(0)
121 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method);
122 staticStackSize = maxOptionalArgsSize +
123 target.getFrameInfo().getMinStackFrameSize();
127 MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
131 // Check if we've allocated a stack slot for this value already
133 int offset = getOffset(val);
134 if (offset == INVALID_FRAME_OFFSET)
137 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
142 size = target.findOptimalStorageSize(val->getType());
143 // align = target.DataLayout.getTypeAlignment(val->getType());
146 align = SizeToAlignment(size, target);
148 offset = getAutomaticVarsSize();
152 if (unsigned int mod = offset % align)
154 offset += align - mod;
158 offset = growUp? firstOffset + offset
159 : firstOffset - offset;
161 offsets[val] = offset;
163 incrementAutomaticVarsSize(size);
169 MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
172 unsigned int size = target.findOptimalStorageSize(type);
173 unsigned char align = target.DataLayout.getTypeAlignment(type);
176 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
178 int offset = getRegSpillsSize();
182 if (unsigned int mod = offset % align)
184 offset += align - mod;
188 offset = growUp? firstOffset + offset
189 : firstOffset - offset;
191 incrementRegSpillsSize(size);
197 MachineCodeForMethod::allocateOptionalArg(const TargetMachine& target,
200 const MachineFrameInfo& frameInfo = target.getFrameInfo();
203 if (frameInfo.argsOnStackHaveFixedSize())
204 size = frameInfo.getSizeOfEachArgOnStack();
207 size = target.findOptimalStorageSize(type);
208 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual argument sizes for computing optional arg offsets");
210 unsigned char align = target.DataLayout.getTypeAlignment(type);
213 int firstOffset = frameInfo.getFirstOptionalOutgoingArgOffset(*this, growUp);
215 int offset = getCurrentOptionalArgsSize();
219 if (unsigned int mod = offset % align)
221 offset += align - mod;
225 offset = growUp? firstOffset + offset
226 : firstOffset - offset;
228 incrementCurrentOptionalArgsSize(size);
234 MachineCodeForMethod::resetOptionalArgs(const TargetMachine& target)
236 currentOptionalArgsSize = 0;
240 MachineCodeForMethod::pushTempValue(const TargetMachine& target,
243 // Compute a power-of-2 alignment according to the possible sizes,
244 // but not greater than the alignment of the largest type we support
245 // (currently a double word -- see class TargetData).
246 unsigned char align = 1;
247 for (; align < size && align < target.DataLayout.getDoubleAlignment();
252 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
254 int offset = currentTmpValuesSize;
258 if (unsigned int mod = offset % align)
260 offset += align - mod;
264 offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
266 currentTmpValuesSize += size;
271 MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
273 currentTmpValuesSize = 0;
277 MachineCodeForMethod::getOffset(const Value* val) const
279 std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
280 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
284 MachineCodeForMethod::dump() const
286 cerr << "\n" << method->getReturnType()
287 << " \"" << method->getName() << "\"\n";
289 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
291 BasicBlock* bb = *BI;
293 << (bb->hasName()? bb->getName() : "Label")
294 << " (" << bb << ")" << ":\n";
296 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
297 for (unsigned i=0; i < mvec.size(); i++)
298 cerr << "\t" << *mvec[i];
300 cerr << "\nEnd method \"" << method->getName() << "\"\n\n";