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, ...),
104 // but not higher than the alignment of the largest type we support
105 // (currently a double word). -- see class TargetData).
107 // This function is similar to the corresponding function in EmitAssembly.cpp
108 // but they are unrelated. This one does not align at more than a
109 // double-word boundary whereas that one might.
112 SizeToAlignment(unsigned int size, const TargetMachine& target)
114 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
115 if (size > (unsigned) cacheLineSize / 2)
116 return cacheLineSize;
118 for (unsigned sz=1; /*no condition*/; sz *= 2)
119 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
125 MachineCodeForMethod::MachineCodeForMethod(const Function *F,
126 const TargetMachine& target)
127 : Annotation(MCFM_AID),
128 method(F), staticStackSize(0),
129 automaticVarsSize(0), regSpillsSize(0),
130 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
131 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
132 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
134 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
136 staticStackSize = maxOptionalArgsSize
137 + target.getFrameInfo().getMinStackFrameSize();
141 MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
143 unsigned int& getPaddedSize,
144 unsigned int sizeToUse)
147 sizeToUse = target.findOptimalStorageSize(val->getType());
148 unsigned int align = SizeToAlignment(sizeToUse, target);
151 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
153 int offset = growUp? firstOffset + getAutomaticVarsSize()
154 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
156 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
157 getPaddedSize = sizeToUse + abs(aligned - offset);
163 MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
165 unsigned int sizeToUse)
167 assert(! automaticVarsAreaFrozen &&
168 "Size of auto vars area has been used to compute an offset so "
169 "no more automatic vars should be allocated!");
171 // Check if we've allocated a stack slot for this value already
173 int offset = getOffset(val);
174 if (offset == INVALID_FRAME_OFFSET)
176 unsigned int getPaddedSize;
177 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
179 offsets[val] = offset;
180 incrementAutomaticVarsSize(getPaddedSize);
186 MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
189 assert(! spillsAreaFrozen &&
190 "Size of reg spills area has been used to compute an offset so "
191 "no more register spill slots should be allocated!");
193 unsigned int size = target.findOptimalStorageSize(type);
194 unsigned char align = target.DataLayout.getTypeAlignment(type);
197 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
199 int offset = growUp? firstOffset + getRegSpillsSize()
200 : firstOffset - (getRegSpillsSize() + size);
202 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
203 size += abs(aligned - offset); // include alignment padding in size
205 incrementRegSpillsSize(size); // update size of reg. spills area
211 MachineCodeForMethod::pushTempValue(const TargetMachine& target,
214 unsigned int align = SizeToAlignment(size, target);
217 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
219 int offset = growUp? firstOffset + currentTmpValuesSize
220 : firstOffset - (currentTmpValuesSize + size);
222 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
223 size += abs(aligned - offset); // include alignment padding in size
225 incrementTmpAreaSize(size); // update "current" size of tmp area
231 MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
233 resetTmpAreaSize(); // clear tmp area to reuse
237 MachineCodeForMethod::getOffset(const Value* val) const
239 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
240 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
244 MachineCodeForMethod::dump() const
246 std::cerr << "\n" << method->getReturnType()
247 << " \"" << method->getName() << "\"\n";
249 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
251 std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
252 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
253 for (unsigned i=0; i < mvec.size(); i++)
254 std::cerr << "\t" << *mvec[i];
256 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";