blob: d9ea15a6f5a92e80930543bd62d1b20fdd407c34 [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
Chris Lattnere316efc2002-10-29 23:18:43 +000022static AnnotationID MF_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) {
Chris Lattner88726182002-10-29 23:40:03 +000041 MachineFunction::construct(&F, Target).CalculateArgSize();
Chris Lattner227c3d32002-10-28 01:12:41 +000042 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 };
Chris Lattner10491642002-10-30 00:48:05 +000064
65 struct Printer : public FunctionPass {
66 const char *getPassName() const { return "MachineFunction Printer"; }
67
68 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.setPreservesAll();
70 }
71
72 bool runOnFunction(Function &F) {
73 MachineFunction::get(&F).dump();
74 return false;
75 }
76 };
Chris Lattner227c3d32002-10-28 01:12:41 +000077}
78
79Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
80 return new ConstructMachineFunction(Target);
81}
82
83Pass *createMachineCodeDestructionPass() {
84 return new DestroyMachineFunction();
85}
86
Chris Lattner10491642002-10-30 00:48:05 +000087Pass *createMachineFunctionPrinterPass() {
88 return new Printer();
89}
90
Chris Lattner227c3d32002-10-28 01:12:41 +000091
92//===---------------------------------------------------------------------===//
93// MachineFunction implementation
94//===---------------------------------------------------------------------===//
95
Chris Lattner10491642002-10-30 00:48:05 +000096MachineFunction::MachineFunction(const Function *F,
97 const TargetMachine& target)
98 : Annotation(MF_AID),
99 Fn(F), Target(target), staticStackSize(0),
100 automaticVarsSize(0), regSpillsSize(0),
101 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
102 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
103 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
104{
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000105 SSARegMapping = new SSARegMap();
Chris Lattner10491642002-10-30 00:48:05 +0000106}
107
108void MachineFunction::dump() const { print(std::cerr); }
109
110void MachineFunction::print(std::ostream &OS) const {
111 OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
112
113 for (const_iterator BB = begin(); BB != end(); ++BB) {
114 BasicBlock *LBB = BB->getBasicBlock();
115 OS << "\n" << LBB->getName() << " ("
116 << (const void*)BB->getBasicBlock() << "):\n";
117 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
118 OS << "\t";
119 (*I)->print(OS, Target);
120 }
121 }
122 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
123}
124
125
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000126// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000127// the MachineCodeForFunction object for the given function.
128// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000129// get() -- Returns a handle to the object.
130// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000131// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000132//
Misha Brukmanfce11432002-10-28 00:28:31 +0000133MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000134MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000135{
Chris Lattnere316efc2002-10-29 23:18:43 +0000136 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000137 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000138 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
139 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000140 return *mcInfo;
141}
142
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000143void
Chris Lattner335d5c32002-10-28 05:58:46 +0000144MachineFunction::destruct(const Function *Fn)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000145{
Chris Lattnere316efc2002-10-29 23:18:43 +0000146 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000147 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000148}
149
Chris Lattner335d5c32002-10-28 05:58:46 +0000150MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000151{
Chris Lattnere316efc2002-10-29 23:18:43 +0000152 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000153 assert(mc && "Call construct() method first to allocate the object");
154 return *mc;
155}
156
157static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000158ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
159 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000160{
161 const MachineFrameInfo& frameInfo = target.getFrameInfo();
162
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000163 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000164
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000165 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
166 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
167 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
168 {
169 unsigned numOperands = callInst->getNumOperands() - 1;
170 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
171 if (numExtra <= 0)
172 continue;
173
174 unsigned int sizeForThisCall;
175 if (frameInfo.argsOnStackHaveFixedSize())
176 {
177 int argSize = frameInfo.getSizeOfEachArgOnStack();
178 sizeForThisCall = numExtra * (unsigned) argSize;
179 }
180 else
181 {
182 assert(0 && "UNTESTED CODE: Size per stack argument is not "
183 "fixed on this architecture: use actual arg sizes to "
184 "compute MaxOptionalArgsSize");
185 sizeForThisCall = 0;
186 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +0000187 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000188 getOperand(i)->getType());
189 }
190
191 if (maxSize < sizeForThisCall)
192 maxSize = sizeForThisCall;
193
194 if ((int)maxOptionalNumArgs < numExtra)
195 maxOptionalNumArgs = (unsigned) numExtra;
196 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000197
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000198 return maxSize;
199}
200
201// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000202// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
203// but not higher than the alignment of the largest type we support
204// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000205//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000206// This function is similar to the corresponding function in EmitAssembly.cpp
207// but they are unrelated. This one does not align at more than a
208// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000209//
210inline unsigned int
211SizeToAlignment(unsigned int size, const TargetMachine& target)
212{
213 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
214 if (size > (unsigned) cacheLineSize / 2)
215 return cacheLineSize;
216 else
217 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000218 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000219 return sz;
220}
221
222
Chris Lattner88726182002-10-29 23:40:03 +0000223void MachineFunction::CalculateArgSize() {
224 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000225 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000226 staticStackSize = maxOptionalArgsSize
Chris Lattner88726182002-10-29 23:40:03 +0000227 + Target.getFrameInfo().getMinStackFrameSize();
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000228}
229
230int
Misha Brukmanfce11432002-10-28 00:28:31 +0000231MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000232 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000233 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000234 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000235{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000236 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000237 sizeToUse = target.findOptimalStorageSize(val->getType());
238 unsigned int align = SizeToAlignment(sizeToUse, target);
239
240 bool growUp;
241 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
242 growUp);
243 int offset = growUp? firstOffset + getAutomaticVarsSize()
244 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
245
246 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
247 getPaddedSize = sizeToUse + abs(aligned - offset);
248
249 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000250}
251
252int
Misha Brukmanfce11432002-10-28 00:28:31 +0000253MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000254 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000255 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000256{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000257 assert(! automaticVarsAreaFrozen &&
258 "Size of auto vars area has been used to compute an offset so "
259 "no more automatic vars should be allocated!");
260
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000261 // Check if we've allocated a stack slot for this value already
262 //
263 int offset = getOffset(val);
264 if (offset == INVALID_FRAME_OFFSET)
265 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000266 unsigned int getPaddedSize;
Chris Lattner6b944532002-10-28 01:16:38 +0000267 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000268 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000269 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000270 }
271 return offset;
272}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000273
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000274int
Misha Brukmanfce11432002-10-28 00:28:31 +0000275MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000276 const Type* type)
277{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000278 assert(! spillsAreaFrozen &&
279 "Size of reg spills area has been used to compute an offset so "
280 "no more register spill slots should be allocated!");
281
Vikram S. Advecde39822002-10-11 16:10:53 +0000282 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000283 unsigned char align = target.DataLayout.getTypeAlignment(type);
284
285 bool growUp;
286 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
287
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000288 int offset = growUp? firstOffset + getRegSpillsSize()
289 : firstOffset - (getRegSpillsSize() + size);
290
291 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
292 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000293
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000294 incrementRegSpillsSize(size); // update size of reg. spills area
295
296 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000297}
298
299int
Misha Brukmanfce11432002-10-28 00:28:31 +0000300MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000301 unsigned int size)
302{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000303 unsigned int align = SizeToAlignment(size, target);
304
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000305 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000306 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
307
308 int offset = growUp? firstOffset + currentTmpValuesSize
309 : firstOffset - (currentTmpValuesSize + size);
310
311 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
312 size += abs(aligned - offset); // include alignment padding in size
313
314 incrementTmpAreaSize(size); // update "current" size of tmp area
315
316 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000317}
318
319void
Misha Brukmanfce11432002-10-28 00:28:31 +0000320MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000321{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000322 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000323}
324
325int
Misha Brukmanfce11432002-10-28 00:28:31 +0000326MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000327{
Chris Lattner09ff1122002-07-24 21:21:32 +0000328 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattner6b944532002-10-28 01:16:38 +0000329 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000330}