blob: 15036a4d077ed9531910bf69fb0fb237a26be9d4 [file] [log] [blame]
Chris Lattner6b944532002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Chris Lattnerf2868ce2002-02-03 07:54:50 +00002//
Chris Lattner6b944532002-10-28 01:16:38 +00003// Collect native machine code information for a function. This allows
4// target-specific information about the generated code to be stored with each
5// function.
6//
7//===----------------------------------------------------------------------===//
Chris Lattnerf2868ce2002-02-03 07:54:50 +00008
Chris Lattnerf2868ce2002-02-03 07:54:50 +00009#include "llvm/CodeGen/MachineInstr.h" // For debug output
Chris Lattner6b944532002-10-28 01:16:38 +000010#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner227c3d32002-10-28 01:12:41 +000011#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000012#include "llvm/Target/TargetMachine.h"
13#include "llvm/Target/MachineFrameInfo.h"
14#include "llvm/Target/MachineCacheInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000015#include "llvm/Function.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000016#include "llvm/iOther.h"
Chris Lattner227c3d32002-10-28 01:12:41 +000017#include "llvm/Pass.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000018#include <limits.h>
19
20const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
21
22static AnnotationID MCFM_AID(
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000023 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000024
Chris Lattner227c3d32002-10-28 01:12:41 +000025
26//===---------------------------------------------------------------------===//
27// Code generation/destruction passes
28//===---------------------------------------------------------------------===//
29
30namespace {
31 class ConstructMachineFunction : public FunctionPass {
32 TargetMachine &Target;
33 public:
34 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
35
36 const char *getPassName() const {
37 return "ConstructMachineFunction";
38 }
39
40 bool runOnFunction(Function &F) {
41 MachineFunction::construct(&F, Target);
42 return false;
43 }
44 };
45
46 struct DestroyMachineFunction : public FunctionPass {
47 const char *getPassName() const { return "FreeMachineFunction"; }
48
49 static void freeMachineCode(Instruction &I) {
50 MachineCodeForInstruction::destroy(&I);
51 }
52
53 bool runOnFunction(Function &F) {
54 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
55 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
56 MachineCodeForInstruction::get(I).dropAllReferences();
57
58 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
59 for_each(FI->begin(), FI->end(), freeMachineCode);
60
61 return false;
62 }
63 };
64}
65
66Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
67 return new ConstructMachineFunction(Target);
68}
69
70Pass *createMachineCodeDestructionPass() {
71 return new DestroyMachineFunction();
72}
73
74
Chris Lattnerd0aa0cd2002-10-28 05:30:46 +000075// get - This deprecated static method returns the MachineBasicBlock object
76// for the specified BasicBlock.
77//
78MachineBasicBlock& MachineBasicBlock::get(const BasicBlock *BB) {
79 const Function *F = BB->getParent();
80 MachineFunction &MF = MachineFunction::get(F);
81
82 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
83 if (I->getBasicBlock() == BB)
84 return *I;
85 assert(0 && "MachineBasicBlock object not found for specified block!");
86 return get(BB);
87}
88
89
Chris Lattner227c3d32002-10-28 01:12:41 +000090//===---------------------------------------------------------------------===//
91// MachineFunction implementation
92//===---------------------------------------------------------------------===//
93
Chris Lattnerf2868ce2002-02-03 07:54:50 +000094// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000095// the MachineCodeForFunction object for the given function.
96// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +000097// get() -- Returns a handle to the object.
98// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000099// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000100//
Misha Brukmanfce11432002-10-28 00:28:31 +0000101MachineFunction&
102MachineFunction::construct(const Function *M, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000103{
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000104 assert(M->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000105 "Object already exists for this function!");
Misha Brukmanfce11432002-10-28 00:28:31 +0000106 MachineFunction* mcInfo = new MachineFunction(M, Tar);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000107 M->addAnnotation(mcInfo);
108 return *mcInfo;
109}
110
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000111void
Misha Brukmanfce11432002-10-28 00:28:31 +0000112MachineFunction::destruct(const Function *M)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000113{
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000114 bool Deleted = M->deleteAnnotation(MCFM_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000115 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000116}
117
Misha Brukmanfce11432002-10-28 00:28:31 +0000118MachineFunction&
119MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000120{
Misha Brukmanfce11432002-10-28 00:28:31 +0000121 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000122 assert(mc && "Call construct() method first to allocate the object");
123 return *mc;
124}
125
126static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000127ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
128 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000129{
130 const MachineFrameInfo& frameInfo = target.getFrameInfo();
131
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000132 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000133
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000134 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
135 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
136 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
137 {
138 unsigned numOperands = callInst->getNumOperands() - 1;
139 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
140 if (numExtra <= 0)
141 continue;
142
143 unsigned int sizeForThisCall;
144 if (frameInfo.argsOnStackHaveFixedSize())
145 {
146 int argSize = frameInfo.getSizeOfEachArgOnStack();
147 sizeForThisCall = numExtra * (unsigned) argSize;
148 }
149 else
150 {
151 assert(0 && "UNTESTED CODE: Size per stack argument is not "
152 "fixed on this architecture: use actual arg sizes to "
153 "compute MaxOptionalArgsSize");
154 sizeForThisCall = 0;
155 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +0000156 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000157 getOperand(i)->getType());
158 }
159
160 if (maxSize < sizeForThisCall)
161 maxSize = sizeForThisCall;
162
163 if ((int)maxOptionalNumArgs < numExtra)
164 maxOptionalNumArgs = (unsigned) numExtra;
165 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000166
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000167 return maxSize;
168}
169
170// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000171// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
172// but not higher than the alignment of the largest type we support
173// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000174//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000175// This function is similar to the corresponding function in EmitAssembly.cpp
176// but they are unrelated. This one does not align at more than a
177// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000178//
179inline unsigned int
180SizeToAlignment(unsigned int size, const TargetMachine& target)
181{
182 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
183 if (size > (unsigned) cacheLineSize / 2)
184 return cacheLineSize;
185 else
186 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000187 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000188 return sz;
189}
190
191
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000192/*ctor*/
Misha Brukmanfce11432002-10-28 00:28:31 +0000193MachineFunction::MachineFunction(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000194 const TargetMachine& target)
195 : Annotation(MCFM_AID),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000196 method(F), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000197 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000198 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
199 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
200 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000201{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000202 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
203 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000204 staticStackSize = maxOptionalArgsSize
205 + target.getFrameInfo().getMinStackFrameSize();
206}
207
208int
Misha Brukmanfce11432002-10-28 00:28:31 +0000209MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000210 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000211 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000212 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000213{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000214 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000215 sizeToUse = target.findOptimalStorageSize(val->getType());
216 unsigned int align = SizeToAlignment(sizeToUse, target);
217
218 bool growUp;
219 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
220 growUp);
221 int offset = growUp? firstOffset + getAutomaticVarsSize()
222 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
223
224 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
225 getPaddedSize = sizeToUse + abs(aligned - offset);
226
227 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000228}
229
230int
Misha Brukmanfce11432002-10-28 00:28:31 +0000231MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000232 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000233 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000234{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000235 assert(! automaticVarsAreaFrozen &&
236 "Size of auto vars area has been used to compute an offset so "
237 "no more automatic vars should be allocated!");
238
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000239 // Check if we've allocated a stack slot for this value already
240 //
241 int offset = getOffset(val);
242 if (offset == INVALID_FRAME_OFFSET)
243 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000244 unsigned int getPaddedSize;
Chris Lattner6b944532002-10-28 01:16:38 +0000245 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000246 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000247 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000248 }
249 return offset;
250}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000251
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000252int
Misha Brukmanfce11432002-10-28 00:28:31 +0000253MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000254 const Type* type)
255{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000256 assert(! spillsAreaFrozen &&
257 "Size of reg spills area has been used to compute an offset so "
258 "no more register spill slots should be allocated!");
259
Vikram S. Advecde39822002-10-11 16:10:53 +0000260 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000261 unsigned char align = target.DataLayout.getTypeAlignment(type);
262
263 bool growUp;
264 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
265
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000266 int offset = growUp? firstOffset + getRegSpillsSize()
267 : firstOffset - (getRegSpillsSize() + size);
268
269 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
270 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000271
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000272 incrementRegSpillsSize(size); // update size of reg. spills area
273
274 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000275}
276
277int
Misha Brukmanfce11432002-10-28 00:28:31 +0000278MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000279 unsigned int size)
280{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000281 unsigned int align = SizeToAlignment(size, target);
282
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000283 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000284 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
285
286 int offset = growUp? firstOffset + currentTmpValuesSize
287 : firstOffset - (currentTmpValuesSize + size);
288
289 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
290 size += abs(aligned - offset); // include alignment padding in size
291
292 incrementTmpAreaSize(size); // update "current" size of tmp area
293
294 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000295}
296
297void
Misha Brukmanfce11432002-10-28 00:28:31 +0000298MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000299{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000300 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000301}
302
303int
Misha Brukmanfce11432002-10-28 00:28:31 +0000304MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000305{
Chris Lattner09ff1122002-07-24 21:21:32 +0000306 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattner6b944532002-10-28 01:16:38 +0000307 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000308}
309
310void
Misha Brukmanfce11432002-10-28 00:28:31 +0000311MachineFunction::dump() const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000312{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000313 std::cerr << "\n" << method->getReturnType()
314 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000315
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000316 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000317 {
Chris Lattner6b944532002-10-28 01:16:38 +0000318 std::cerr << "\n" << BB->getName() << " (" << (const void*)BB
319 << ")" << ":" << "\n";
Chris Lattner55291ea2002-10-28 01:41:47 +0000320 MachineBasicBlock& mvec = MachineBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000321 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000322 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000323 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000324 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000325}