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/MachineFunction.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/iOther.h"
19 const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
21 static AnnotationID MCFM_AID(
22 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
24 // The next two methods are used to construct and to retrieve
25 // the MachineCodeForFunction object for the given function.
26 // construct() -- Allocates and initializes for a given function and target
27 // get() -- Returns a handle to the object.
28 // This should not be called before "construct()"
29 // for a given Function.
32 MachineFunction::construct(const Function *M, const TargetMachine &Tar)
34 assert(M->getAnnotation(MCFM_AID) == 0 &&
35 "Object already exists for this function!");
36 MachineFunction* mcInfo = new MachineFunction(M, Tar);
37 M->addAnnotation(mcInfo);
42 MachineFunction::destruct(const Function *M)
44 bool Deleted = M->deleteAnnotation(MCFM_AID);
45 assert(Deleted && "Machine code did not exist for function!");
49 MachineFunction::get(const Function *F)
51 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
52 assert(mc && "Call construct() method first to allocate the object");
57 ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
58 unsigned &maxOptionalNumArgs)
60 const MachineFrameInfo& frameInfo = target.getFrameInfo();
64 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
65 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
66 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
68 unsigned numOperands = callInst->getNumOperands() - 1;
69 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
73 unsigned int sizeForThisCall;
74 if (frameInfo.argsOnStackHaveFixedSize())
76 int argSize = frameInfo.getSizeOfEachArgOnStack();
77 sizeForThisCall = numExtra * (unsigned) argSize;
81 assert(0 && "UNTESTED CODE: Size per stack argument is not "
82 "fixed on this architecture: use actual arg sizes to "
83 "compute MaxOptionalArgsSize");
85 for (unsigned i = 0; i < numOperands; ++i)
86 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
87 getOperand(i)->getType());
90 if (maxSize < sizeForThisCall)
91 maxSize = sizeForThisCall;
93 if ((int)maxOptionalNumArgs < numExtra)
94 maxOptionalNumArgs = (unsigned) numExtra;
100 // Align data larger than one L1 cache line on L1 cache line boundaries.
101 // Align all smaller data on the next higher 2^x boundary (4, 8, ...),
102 // but not higher than the alignment of the largest type we support
103 // (currently a double word). -- see class TargetData).
105 // This function is similar to the corresponding function in EmitAssembly.cpp
106 // but they are unrelated. This one does not align at more than a
107 // double-word boundary whereas that one might.
110 SizeToAlignment(unsigned int size, const TargetMachine& target)
112 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
113 if (size > (unsigned) cacheLineSize / 2)
114 return cacheLineSize;
116 for (unsigned sz=1; /*no condition*/; sz *= 2)
117 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
123 MachineFunction::MachineFunction(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 MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
141 unsigned int& getPaddedSize,
142 unsigned int sizeToUse)
145 sizeToUse = target.findOptimalStorageSize(val->getType());
146 unsigned int align = SizeToAlignment(sizeToUse, target);
149 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
151 int offset = growUp? firstOffset + getAutomaticVarsSize()
152 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
154 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
155 getPaddedSize = sizeToUse + abs(aligned - offset);
161 MachineFunction::allocateLocalVar(const TargetMachine& target,
163 unsigned int sizeToUse)
165 assert(! automaticVarsAreaFrozen &&
166 "Size of auto vars area has been used to compute an offset so "
167 "no more automatic vars should be allocated!");
169 // Check if we've allocated a stack slot for this value already
171 int offset = getOffset(val);
172 if (offset == INVALID_FRAME_OFFSET)
174 unsigned int getPaddedSize;
175 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
177 offsets[val] = offset;
178 incrementAutomaticVarsSize(getPaddedSize);
184 MachineFunction::allocateSpilledValue(const TargetMachine& target,
187 assert(! spillsAreaFrozen &&
188 "Size of reg spills area has been used to compute an offset so "
189 "no more register spill slots should be allocated!");
191 unsigned int size = target.DataLayout.getTypeSize(type);
192 unsigned char align = target.DataLayout.getTypeAlignment(type);
195 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
197 int offset = growUp? firstOffset + getRegSpillsSize()
198 : firstOffset - (getRegSpillsSize() + size);
200 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
201 size += abs(aligned - offset); // include alignment padding in size
203 incrementRegSpillsSize(size); // update size of reg. spills area
209 MachineFunction::pushTempValue(const TargetMachine& target,
212 unsigned int align = SizeToAlignment(size, target);
215 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
217 int offset = growUp? firstOffset + currentTmpValuesSize
218 : firstOffset - (currentTmpValuesSize + size);
220 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
221 size += abs(aligned - offset); // include alignment padding in size
223 incrementTmpAreaSize(size); // update "current" size of tmp area
229 MachineFunction::popAllTempValues(const TargetMachine& target)
231 resetTmpAreaSize(); // clear tmp area to reuse
235 MachineFunction::getOffset(const Value* val) const
237 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
238 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
242 MachineFunction::dump() const
244 std::cerr << "\n" << method->getReturnType()
245 << " \"" << method->getName() << "\"\n";
247 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
249 std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
250 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
251 for (unsigned i=0; i < mvec.size(); i++)
252 std::cerr << "\t" << *mvec[i];
254 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";