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/iOther.h"
18 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
20 static AnnotationID MCFM_AID(
21 AnnotationManager::getID("CodeGen::MachineCodeForMethod"));
23 // The next two methods are used to construct and to retrieve
24 // the MachineCodeForMethod object for the given method.
25 // construct() -- Allocates and initializes for a given method and target
26 // get() -- Returns a handle to the object.
27 // This should not be called before "construct()"
28 // for a given Method.
30 MachineCodeForMethod &MachineCodeForMethod::construct(const Method *M,
31 const TargetMachine &Tar){
32 assert(M->getAnnotation(MCFM_AID) == 0 &&
33 "Object already exists for this method!");
34 MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
35 M->addAnnotation(mcInfo);
39 void MachineCodeForMethod::destruct(const Method *M) {
40 bool Deleted = M->deleteAnnotation(MCFM_AID);
41 assert(Deleted && "Machine code did not exist for method!");
45 MachineCodeForMethod &MachineCodeForMethod::get(const Method* method) {
46 MachineCodeForMethod* mc = (MachineCodeForMethod*)
47 method->getAnnotation(MCFM_AID);
48 assert(mc && "Call construct() method first to allocate the object");
53 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Method* method)
55 const MachineFrameInfo& frameInfo = target.getFrameInfo();
57 unsigned int maxSize = 0;
59 for (Method::const_inst_iterator I=method->inst_begin(),E=method->inst_end();
61 if ((*I)->getOpcode() == Instruction::Call)
63 CallInst* callInst = cast<CallInst>(*I);
64 unsigned int numOperands = callInst->getNumOperands() - 1;
65 int numExtra = (int) numOperands - frameInfo.getNumFixedOutgoingArgs();
69 unsigned int sizeForThisCall;
70 if (frameInfo.argsOnStackHaveFixedSize())
72 int argSize = frameInfo.getSizeOfEachArgOnStack();
73 sizeForThisCall = numExtra * (unsigned) argSize;
77 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual arg sizes to compute MaxOptionalArgsSize");
79 for (unsigned i=0; i < numOperands; ++i)
80 sizeForThisCall += target.findOptimalStorageSize(callInst->
81 getOperand(i)->getType());
84 if (maxSize < sizeForThisCall)
85 maxSize = sizeForThisCall;
91 // Align data larger than one L1 cache line on L1 cache line boundaries.
92 // Align all smaller data on the next higher 2^x boundary (4, 8, ...).
94 // THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
95 // SHOULD BE USED DIRECTLY THERE
98 SizeToAlignment(unsigned int size, const TargetMachine& target)
100 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
101 if (size > (unsigned) cacheLineSize / 2)
102 return cacheLineSize;
104 for (unsigned sz=1; /*no condition*/; sz *= 2)
112 MachineCodeForMethod::MachineCodeForMethod(const Method* _M,
113 const TargetMachine& target)
114 : Annotation(MCFM_AID),
115 method(_M), compiledAsLeaf(false), staticStackSize(0),
116 automaticVarsSize(0), regSpillsSize(0),
117 currentOptionalArgsSize(0), maxOptionalArgsSize(0),
118 currentTmpValuesSize(0)
120 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method);
121 staticStackSize = maxOptionalArgsSize +
122 target.getFrameInfo().getMinStackFrameSize();
126 MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
130 // Check if we've allocated a stack slot for this value already
132 int offset = getOffset(val);
133 if (offset == INVALID_FRAME_OFFSET)
136 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
141 size = target.findOptimalStorageSize(val->getType());
142 // align = target.DataLayout.getTypeAlignment(val->getType());
145 align = SizeToAlignment(size, target);
147 offset = getAutomaticVarsSize();
151 if (unsigned int mod = offset % align)
153 offset += align - mod;
157 offset = growUp? firstOffset + offset
158 : firstOffset - offset;
160 offsets[val] = offset;
162 incrementAutomaticVarsSize(size);
168 MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
171 unsigned int size = target.findOptimalStorageSize(type);
172 unsigned char align = target.DataLayout.getTypeAlignment(type);
175 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
177 int offset = getRegSpillsSize();
181 if (unsigned int mod = offset % align)
183 offset += align - mod;
187 offset = growUp? firstOffset + offset
188 : firstOffset - offset;
190 incrementRegSpillsSize(size);
196 MachineCodeForMethod::allocateOptionalArg(const TargetMachine& target,
199 const MachineFrameInfo& frameInfo = target.getFrameInfo();
202 if (frameInfo.argsOnStackHaveFixedSize())
203 size = frameInfo.getSizeOfEachArgOnStack();
206 size = target.findOptimalStorageSize(type);
207 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual argument sizes for computing optional arg offsets");
209 unsigned char align = target.DataLayout.getTypeAlignment(type);
212 int firstOffset = frameInfo.getFirstOptionalOutgoingArgOffset(*this, growUp);
214 int offset = getCurrentOptionalArgsSize();
218 if (unsigned int mod = offset % align)
220 offset += align - mod;
224 offset = growUp? firstOffset + offset
225 : firstOffset - offset;
227 incrementCurrentOptionalArgsSize(size);
233 MachineCodeForMethod::resetOptionalArgs(const TargetMachine& target)
235 currentOptionalArgsSize = 0;
239 MachineCodeForMethod::pushTempValue(const TargetMachine& target,
242 // Compute a power-of-2 alignment according to the possible sizes,
243 // but not greater than the alignment of the largest type we support
244 // (currently a double word -- see class TargetData).
245 unsigned char align = 1;
246 for (; align < size && align < target.DataLayout.getDoubleAlignment();
251 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
253 int offset = currentTmpValuesSize;
257 if (unsigned int mod = offset % align)
259 offset += align - mod;
263 offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
265 currentTmpValuesSize += size;
270 MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
272 currentTmpValuesSize = 0;
276 MachineCodeForMethod::getOffset(const Value* val) const
278 std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
279 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
283 MachineCodeForMethod::dump() const
285 cerr << "\n" << method->getReturnType()
286 << " \"" << method->getName() << "\"\n";
288 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
290 BasicBlock* bb = *BI;
292 << (bb->hasName()? bb->getName() : "Label")
293 << " (" << bb << ")" << ":\n";
295 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
296 for (unsigned i=0; i < mvec.size(); i++)
297 cerr << "\t" << *mvec[i];
299 cerr << "\nEnd method \"" << method->getName() << "\"\n\n";