blob: c06c008baded8bf9c2bb3892b63fa2696b1c9cad [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&
Chris Lattner335d5c32002-10-28 05:58:46 +0000102MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000103{
Chris Lattner335d5c32002-10-28 05:58:46 +0000104 assert(Fn->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000105 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000106 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
107 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000108 return *mcInfo;
109}
110
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000111void
Chris Lattner335d5c32002-10-28 05:58:46 +0000112MachineFunction::destruct(const Function *Fn)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000113{
Chris Lattner335d5c32002-10-28 05:58:46 +0000114 bool Deleted = Fn->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
Chris Lattner335d5c32002-10-28 05:58:46 +0000118MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000119{
Misha Brukmanfce11432002-10-28 00:28:31 +0000120 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000121 assert(mc && "Call construct() method first to allocate the object");
122 return *mc;
123}
124
125static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000126ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
127 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000128{
129 const MachineFrameInfo& frameInfo = target.getFrameInfo();
130
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000131 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000132
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000133 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
134 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
135 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
136 {
137 unsigned numOperands = callInst->getNumOperands() - 1;
138 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
139 if (numExtra <= 0)
140 continue;
141
142 unsigned int sizeForThisCall;
143 if (frameInfo.argsOnStackHaveFixedSize())
144 {
145 int argSize = frameInfo.getSizeOfEachArgOnStack();
146 sizeForThisCall = numExtra * (unsigned) argSize;
147 }
148 else
149 {
150 assert(0 && "UNTESTED CODE: Size per stack argument is not "
151 "fixed on this architecture: use actual arg sizes to "
152 "compute MaxOptionalArgsSize");
153 sizeForThisCall = 0;
154 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +0000155 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000156 getOperand(i)->getType());
157 }
158
159 if (maxSize < sizeForThisCall)
160 maxSize = sizeForThisCall;
161
162 if ((int)maxOptionalNumArgs < numExtra)
163 maxOptionalNumArgs = (unsigned) numExtra;
164 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000165
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000166 return maxSize;
167}
168
169// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000170// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
171// but not higher than the alignment of the largest type we support
172// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000173//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000174// This function is similar to the corresponding function in EmitAssembly.cpp
175// but they are unrelated. This one does not align at more than a
176// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000177//
178inline unsigned int
179SizeToAlignment(unsigned int size, const TargetMachine& target)
180{
181 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
182 if (size > (unsigned) cacheLineSize / 2)
183 return cacheLineSize;
184 else
185 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000186 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000187 return sz;
188}
189
190
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000191/*ctor*/
Misha Brukmanfce11432002-10-28 00:28:31 +0000192MachineFunction::MachineFunction(const Function *F,
Chris Lattner335d5c32002-10-28 05:58:46 +0000193 const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000194 : Annotation(MCFM_AID),
Chris Lattner335d5c32002-10-28 05:58:46 +0000195 Fn(F), Target(target), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000196 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000197 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
198 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
199 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000200{
Chris Lattner335d5c32002-10-28 05:58:46 +0000201 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, Fn,
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000202 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000203 staticStackSize = maxOptionalArgsSize
204 + target.getFrameInfo().getMinStackFrameSize();
205}
206
207int
Misha Brukmanfce11432002-10-28 00:28:31 +0000208MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000209 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000210 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000211 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000212{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000213 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000214 sizeToUse = target.findOptimalStorageSize(val->getType());
215 unsigned int align = SizeToAlignment(sizeToUse, target);
216
217 bool growUp;
218 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
219 growUp);
220 int offset = growUp? firstOffset + getAutomaticVarsSize()
221 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
222
223 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
224 getPaddedSize = sizeToUse + abs(aligned - offset);
225
226 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000227}
228
229int
Misha Brukmanfce11432002-10-28 00:28:31 +0000230MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000231 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000232 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000233{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000234 assert(! automaticVarsAreaFrozen &&
235 "Size of auto vars area has been used to compute an offset so "
236 "no more automatic vars should be allocated!");
237
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000238 // Check if we've allocated a stack slot for this value already
239 //
240 int offset = getOffset(val);
241 if (offset == INVALID_FRAME_OFFSET)
242 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000243 unsigned int getPaddedSize;
Chris Lattner6b944532002-10-28 01:16:38 +0000244 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000245 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000246 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000247 }
248 return offset;
249}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000250
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000251int
Misha Brukmanfce11432002-10-28 00:28:31 +0000252MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000253 const Type* type)
254{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000255 assert(! spillsAreaFrozen &&
256 "Size of reg spills area has been used to compute an offset so "
257 "no more register spill slots should be allocated!");
258
Vikram S. Advecde39822002-10-11 16:10:53 +0000259 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000260 unsigned char align = target.DataLayout.getTypeAlignment(type);
261
262 bool growUp;
263 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
264
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000265 int offset = growUp? firstOffset + getRegSpillsSize()
266 : firstOffset - (getRegSpillsSize() + size);
267
268 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
269 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000270
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000271 incrementRegSpillsSize(size); // update size of reg. spills area
272
273 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000274}
275
276int
Misha Brukmanfce11432002-10-28 00:28:31 +0000277MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000278 unsigned int size)
279{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000280 unsigned int align = SizeToAlignment(size, target);
281
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000282 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000283 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
284
285 int offset = growUp? firstOffset + currentTmpValuesSize
286 : firstOffset - (currentTmpValuesSize + size);
287
288 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
289 size += abs(aligned - offset); // include alignment padding in size
290
291 incrementTmpAreaSize(size); // update "current" size of tmp area
292
293 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000294}
295
296void
Misha Brukmanfce11432002-10-28 00:28:31 +0000297MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000298{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000299 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000300}
301
302int
Misha Brukmanfce11432002-10-28 00:28:31 +0000303MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000304{
Chris Lattner09ff1122002-07-24 21:21:32 +0000305 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattner6b944532002-10-28 01:16:38 +0000306 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000307}
308
309void
Misha Brukmanfce11432002-10-28 00:28:31 +0000310MachineFunction::dump() const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000311{
Chris Lattner335d5c32002-10-28 05:58:46 +0000312 std::cerr << "\n" << Fn->getReturnType()
313 << " \"" << Fn->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000314
Chris Lattner335d5c32002-10-28 05:58:46 +0000315 for (Function::const_iterator BB = Fn->begin(); BB != Fn->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000316 {
Chris Lattner6b944532002-10-28 01:16:38 +0000317 std::cerr << "\n" << BB->getName() << " (" << (const void*)BB
318 << ")" << ":" << "\n";
Chris Lattner55291ea2002-10-28 01:41:47 +0000319 MachineBasicBlock& mvec = MachineBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000320 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000321 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000322 }
Chris Lattner335d5c32002-10-28 05:58:46 +0000323 std::cerr << "\nEnd function \"" << Fn->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000324}