blob: b9c017a4591b267e2e8e06d2fbf3d917980b12d0 [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
75//===---------------------------------------------------------------------===//
76// MachineFunction implementation
77//===---------------------------------------------------------------------===//
78
Chris Lattnerf2868ce2002-02-03 07:54:50 +000079// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000080// the MachineCodeForFunction object for the given function.
81// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +000082// get() -- Returns a handle to the object.
83// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000084// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +000085//
Misha Brukmanfce11432002-10-28 00:28:31 +000086MachineFunction&
87MachineFunction::construct(const Function *M, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000088{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000089 assert(M->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000090 "Object already exists for this function!");
Misha Brukmanfce11432002-10-28 00:28:31 +000091 MachineFunction* mcInfo = new MachineFunction(M, Tar);
Chris Lattnerf2868ce2002-02-03 07:54:50 +000092 M->addAnnotation(mcInfo);
93 return *mcInfo;
94}
95
Vikram S. Adve89e2da02002-03-18 03:36:30 +000096void
Misha Brukmanfce11432002-10-28 00:28:31 +000097MachineFunction::destruct(const Function *M)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000098{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000099 bool Deleted = M->deleteAnnotation(MCFM_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000100 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000101}
102
Misha Brukmanfce11432002-10-28 00:28:31 +0000103MachineFunction&
104MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000105{
Misha Brukmanfce11432002-10-28 00:28:31 +0000106 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000107 assert(mc && "Call construct() method first to allocate the object");
108 return *mc;
109}
110
111static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000112ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
113 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000114{
115 const MachineFrameInfo& frameInfo = target.getFrameInfo();
116
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000117 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000118
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000119 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
120 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
121 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
122 {
123 unsigned numOperands = callInst->getNumOperands() - 1;
124 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
125 if (numExtra <= 0)
126 continue;
127
128 unsigned int sizeForThisCall;
129 if (frameInfo.argsOnStackHaveFixedSize())
130 {
131 int argSize = frameInfo.getSizeOfEachArgOnStack();
132 sizeForThisCall = numExtra * (unsigned) argSize;
133 }
134 else
135 {
136 assert(0 && "UNTESTED CODE: Size per stack argument is not "
137 "fixed on this architecture: use actual arg sizes to "
138 "compute MaxOptionalArgsSize");
139 sizeForThisCall = 0;
140 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +0000141 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000142 getOperand(i)->getType());
143 }
144
145 if (maxSize < sizeForThisCall)
146 maxSize = sizeForThisCall;
147
148 if ((int)maxOptionalNumArgs < numExtra)
149 maxOptionalNumArgs = (unsigned) numExtra;
150 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000151
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000152 return maxSize;
153}
154
155// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000156// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
157// but not higher than the alignment of the largest type we support
158// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000159//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000160// This function is similar to the corresponding function in EmitAssembly.cpp
161// but they are unrelated. This one does not align at more than a
162// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000163//
164inline unsigned int
165SizeToAlignment(unsigned int size, const TargetMachine& target)
166{
167 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
168 if (size > (unsigned) cacheLineSize / 2)
169 return cacheLineSize;
170 else
171 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000172 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000173 return sz;
174}
175
176
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000177/*ctor*/
Misha Brukmanfce11432002-10-28 00:28:31 +0000178MachineFunction::MachineFunction(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000179 const TargetMachine& target)
180 : Annotation(MCFM_AID),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000181 method(F), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000182 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000183 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
184 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
185 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000186{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000187 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
188 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000189 staticStackSize = maxOptionalArgsSize
190 + target.getFrameInfo().getMinStackFrameSize();
191}
192
193int
Misha Brukmanfce11432002-10-28 00:28:31 +0000194MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000195 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000196 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000197 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000198{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000199 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000200 sizeToUse = target.findOptimalStorageSize(val->getType());
201 unsigned int align = SizeToAlignment(sizeToUse, target);
202
203 bool growUp;
204 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
205 growUp);
206 int offset = growUp? firstOffset + getAutomaticVarsSize()
207 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
208
209 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
210 getPaddedSize = sizeToUse + abs(aligned - offset);
211
212 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000213}
214
215int
Misha Brukmanfce11432002-10-28 00:28:31 +0000216MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000217 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000218 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000219{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000220 assert(! automaticVarsAreaFrozen &&
221 "Size of auto vars area has been used to compute an offset so "
222 "no more automatic vars should be allocated!");
223
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000224 // Check if we've allocated a stack slot for this value already
225 //
226 int offset = getOffset(val);
227 if (offset == INVALID_FRAME_OFFSET)
228 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000229 unsigned int getPaddedSize;
Chris Lattner6b944532002-10-28 01:16:38 +0000230 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000231 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000232 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000233 }
234 return offset;
235}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000236
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000237int
Misha Brukmanfce11432002-10-28 00:28:31 +0000238MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000239 const Type* type)
240{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000241 assert(! spillsAreaFrozen &&
242 "Size of reg spills area has been used to compute an offset so "
243 "no more register spill slots should be allocated!");
244
Vikram S. Advecde39822002-10-11 16:10:53 +0000245 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000246 unsigned char align = target.DataLayout.getTypeAlignment(type);
247
248 bool growUp;
249 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
250
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000251 int offset = growUp? firstOffset + getRegSpillsSize()
252 : firstOffset - (getRegSpillsSize() + size);
253
254 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
255 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000256
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000257 incrementRegSpillsSize(size); // update size of reg. spills area
258
259 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000260}
261
262int
Misha Brukmanfce11432002-10-28 00:28:31 +0000263MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000264 unsigned int size)
265{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000266 unsigned int align = SizeToAlignment(size, target);
267
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000268 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000269 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
270
271 int offset = growUp? firstOffset + currentTmpValuesSize
272 : firstOffset - (currentTmpValuesSize + size);
273
274 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
275 size += abs(aligned - offset); // include alignment padding in size
276
277 incrementTmpAreaSize(size); // update "current" size of tmp area
278
279 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000280}
281
282void
Misha Brukmanfce11432002-10-28 00:28:31 +0000283MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000284{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000285 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000286}
287
288int
Misha Brukmanfce11432002-10-28 00:28:31 +0000289MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000290{
Chris Lattner09ff1122002-07-24 21:21:32 +0000291 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattner6b944532002-10-28 01:16:38 +0000292 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000293}
294
295void
Misha Brukmanfce11432002-10-28 00:28:31 +0000296MachineFunction::dump() const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000297{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000298 std::cerr << "\n" << method->getReturnType()
299 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000300
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000301 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000302 {
Chris Lattner6b944532002-10-28 01:16:38 +0000303 std::cerr << "\n" << BB->getName() << " (" << (const void*)BB
304 << ")" << ":" << "\n";
Chris Lattner55291ea2002-10-28 01:41:47 +0000305 MachineBasicBlock& mvec = MachineBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000306 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000307 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000308 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000309 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000310}