blob: 4d680754d0ff216d01a6ccc1fe8cad284b11410b [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"
Vikram S. Adve432e1ca2002-07-08 23:03:54 +000011#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattner227c3d32002-10-28 01:12:41 +000012#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000013#include "llvm/Target/TargetMachine.h"
14#include "llvm/Target/MachineFrameInfo.h"
15#include "llvm/Target/MachineCacheInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000016#include "llvm/Function.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000017#include "llvm/iOther.h"
Chris Lattner227c3d32002-10-28 01:12:41 +000018#include "llvm/Pass.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000019#include <limits.h>
20
21const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
22
23static AnnotationID MCFM_AID(
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000024 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000025
Chris Lattner227c3d32002-10-28 01:12:41 +000026
27//===---------------------------------------------------------------------===//
28// Code generation/destruction passes
29//===---------------------------------------------------------------------===//
30
31namespace {
32 class ConstructMachineFunction : public FunctionPass {
33 TargetMachine &Target;
34 public:
35 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
36
37 const char *getPassName() const {
38 return "ConstructMachineFunction";
39 }
40
41 bool runOnFunction(Function &F) {
42 MachineFunction::construct(&F, Target);
43 return false;
44 }
45 };
46
47 struct DestroyMachineFunction : public FunctionPass {
48 const char *getPassName() const { return "FreeMachineFunction"; }
49
50 static void freeMachineCode(Instruction &I) {
51 MachineCodeForInstruction::destroy(&I);
52 }
53
54 bool runOnFunction(Function &F) {
55 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
56 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
57 MachineCodeForInstruction::get(I).dropAllReferences();
58
59 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
60 for_each(FI->begin(), FI->end(), freeMachineCode);
61
62 return false;
63 }
64 };
65}
66
67Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
68 return new ConstructMachineFunction(Target);
69}
70
71Pass *createMachineCodeDestructionPass() {
72 return new DestroyMachineFunction();
73}
74
75
76//===---------------------------------------------------------------------===//
77// MachineFunction implementation
78//===---------------------------------------------------------------------===//
79
Chris Lattnerf2868ce2002-02-03 07:54:50 +000080// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000081// the MachineCodeForFunction object for the given function.
82// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +000083// get() -- Returns a handle to the object.
84// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000085// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +000086//
Misha Brukmanfce11432002-10-28 00:28:31 +000087MachineFunction&
88MachineFunction::construct(const Function *M, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000089{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000090 assert(M->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000091 "Object already exists for this function!");
Misha Brukmanfce11432002-10-28 00:28:31 +000092 MachineFunction* mcInfo = new MachineFunction(M, Tar);
Chris Lattnerf2868ce2002-02-03 07:54:50 +000093 M->addAnnotation(mcInfo);
94 return *mcInfo;
95}
96
Vikram S. Adve89e2da02002-03-18 03:36:30 +000097void
Misha Brukmanfce11432002-10-28 00:28:31 +000098MachineFunction::destruct(const Function *M)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000099{
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000100 bool Deleted = M->deleteAnnotation(MCFM_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000101 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000102}
103
Misha Brukmanfce11432002-10-28 00:28:31 +0000104MachineFunction&
105MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000106{
Misha Brukmanfce11432002-10-28 00:28:31 +0000107 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000108 assert(mc && "Call construct() method first to allocate the object");
109 return *mc;
110}
111
112static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000113ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
114 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000115{
116 const MachineFrameInfo& frameInfo = target.getFrameInfo();
117
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000118 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000119
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000120 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
121 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
122 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
123 {
124 unsigned numOperands = callInst->getNumOperands() - 1;
125 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
126 if (numExtra <= 0)
127 continue;
128
129 unsigned int sizeForThisCall;
130 if (frameInfo.argsOnStackHaveFixedSize())
131 {
132 int argSize = frameInfo.getSizeOfEachArgOnStack();
133 sizeForThisCall = numExtra * (unsigned) argSize;
134 }
135 else
136 {
137 assert(0 && "UNTESTED CODE: Size per stack argument is not "
138 "fixed on this architecture: use actual arg sizes to "
139 "compute MaxOptionalArgsSize");
140 sizeForThisCall = 0;
141 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +0000142 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000143 getOperand(i)->getType());
144 }
145
146 if (maxSize < sizeForThisCall)
147 maxSize = sizeForThisCall;
148
149 if ((int)maxOptionalNumArgs < numExtra)
150 maxOptionalNumArgs = (unsigned) numExtra;
151 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000152
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000153 return maxSize;
154}
155
156// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000157// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
158// but not higher than the alignment of the largest type we support
159// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000160//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000161// This function is similar to the corresponding function in EmitAssembly.cpp
162// but they are unrelated. This one does not align at more than a
163// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000164//
165inline unsigned int
166SizeToAlignment(unsigned int size, const TargetMachine& target)
167{
168 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
169 if (size > (unsigned) cacheLineSize / 2)
170 return cacheLineSize;
171 else
172 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000173 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000174 return sz;
175}
176
177
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000178/*ctor*/
Misha Brukmanfce11432002-10-28 00:28:31 +0000179MachineFunction::MachineFunction(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000180 const TargetMachine& target)
181 : Annotation(MCFM_AID),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000182 method(F), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000183 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000184 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
185 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
186 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000187{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000188 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
189 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000190 staticStackSize = maxOptionalArgsSize
191 + target.getFrameInfo().getMinStackFrameSize();
192}
193
194int
Misha Brukmanfce11432002-10-28 00:28:31 +0000195MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000196 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000197 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000198 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000199{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000200 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000201 sizeToUse = target.findOptimalStorageSize(val->getType());
202 unsigned int align = SizeToAlignment(sizeToUse, target);
203
204 bool growUp;
205 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
206 growUp);
207 int offset = growUp? firstOffset + getAutomaticVarsSize()
208 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
209
210 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
211 getPaddedSize = sizeToUse + abs(aligned - offset);
212
213 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000214}
215
216int
Misha Brukmanfce11432002-10-28 00:28:31 +0000217MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000218 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000219 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000220{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000221 assert(! automaticVarsAreaFrozen &&
222 "Size of auto vars area has been used to compute an offset so "
223 "no more automatic vars should be allocated!");
224
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000225 // Check if we've allocated a stack slot for this value already
226 //
227 int offset = getOffset(val);
228 if (offset == INVALID_FRAME_OFFSET)
229 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000230 unsigned int getPaddedSize;
Chris Lattner6b944532002-10-28 01:16:38 +0000231 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000232 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000233 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000234 }
235 return offset;
236}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000237
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000238int
Misha Brukmanfce11432002-10-28 00:28:31 +0000239MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000240 const Type* type)
241{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000242 assert(! spillsAreaFrozen &&
243 "Size of reg spills area has been used to compute an offset so "
244 "no more register spill slots should be allocated!");
245
Vikram S. Advecde39822002-10-11 16:10:53 +0000246 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000247 unsigned char align = target.DataLayout.getTypeAlignment(type);
248
249 bool growUp;
250 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
251
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000252 int offset = growUp? firstOffset + getRegSpillsSize()
253 : firstOffset - (getRegSpillsSize() + size);
254
255 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
256 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000257
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000258 incrementRegSpillsSize(size); // update size of reg. spills area
259
260 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000261}
262
263int
Misha Brukmanfce11432002-10-28 00:28:31 +0000264MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000265 unsigned int size)
266{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000267 unsigned int align = SizeToAlignment(size, target);
268
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000269 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000270 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
271
272 int offset = growUp? firstOffset + currentTmpValuesSize
273 : firstOffset - (currentTmpValuesSize + size);
274
275 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
276 size += abs(aligned - offset); // include alignment padding in size
277
278 incrementTmpAreaSize(size); // update "current" size of tmp area
279
280 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000281}
282
283void
Misha Brukmanfce11432002-10-28 00:28:31 +0000284MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000285{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000286 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000287}
288
289int
Misha Brukmanfce11432002-10-28 00:28:31 +0000290MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000291{
Chris Lattner09ff1122002-07-24 21:21:32 +0000292 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattner6b944532002-10-28 01:16:38 +0000293 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000294}
295
296void
Misha Brukmanfce11432002-10-28 00:28:31 +0000297MachineFunction::dump() const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000298{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000299 std::cerr << "\n" << method->getReturnType()
300 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000301
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000302 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000303 {
Chris Lattner6b944532002-10-28 01:16:38 +0000304 std::cerr << "\n" << BB->getName() << " (" << (const void*)BB
305 << ")" << ":" << "\n";
Vikram S. Adve432e1ca2002-07-08 23:03:54 +0000306 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000307 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000308 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000309 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000310 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000311}