blob: ef783d1741ffa54b1058270d5ffd14d9c2b89f9e [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 Lattner6b944532002-10-28 01:16:38 +00009#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000010#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner227c3d32002-10-28 01:12:41 +000011#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000012#include "llvm/CodeGen/SSARegMap.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
Chris Lattnere316efc2002-10-29 23:18:43 +000023static AnnotationID MF_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) {
Chris Lattner88726182002-10-29 23:40:03 +000042 MachineFunction::construct(&F, Target).CalculateArgSize();
Chris Lattner227c3d32002-10-28 01:12:41 +000043 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 };
Chris Lattner10491642002-10-30 00:48:05 +000065
66 struct Printer : public FunctionPass {
67 const char *getPassName() const { return "MachineFunction Printer"; }
68
69 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70 AU.setPreservesAll();
71 }
72
73 bool runOnFunction(Function &F) {
74 MachineFunction::get(&F).dump();
75 return false;
76 }
77 };
Chris Lattner227c3d32002-10-28 01:12:41 +000078}
79
80Pass *createMachineCodeConstructionPass(TargetMachine &Target) {
81 return new ConstructMachineFunction(Target);
82}
83
84Pass *createMachineCodeDestructionPass() {
85 return new DestroyMachineFunction();
86}
87
Chris Lattner10491642002-10-30 00:48:05 +000088Pass *createMachineFunctionPrinterPass() {
89 return new Printer();
90}
91
Chris Lattner227c3d32002-10-28 01:12:41 +000092
93//===---------------------------------------------------------------------===//
94// MachineFunction implementation
95//===---------------------------------------------------------------------===//
96
Chris Lattner10491642002-10-30 00:48:05 +000097MachineFunction::MachineFunction(const Function *F,
98 const TargetMachine& target)
Chris Lattner831fdcf2002-12-25 05:03:22 +000099 : Annotation(MF_AID), Fn(F), Target(target) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000100 SSARegMapping = new SSARegMap();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000101
102 // FIXME: move state into another class
103 staticStackSize = automaticVarsSize = regSpillsSize = 0;
104 maxOptionalArgsSize = maxOptionalNumArgs = currentTmpValuesSize = 0;
105 maxTmpValuesSize = 0;
106 compiledAsLeaf = spillsAreaFrozen = automaticVarsAreaFrozen = false;
107}
108
109MachineFunction::~MachineFunction() {
110 delete SSARegMapping;
Chris Lattner10491642002-10-30 00:48:05 +0000111}
112
113void MachineFunction::dump() const { print(std::cerr); }
114
115void MachineFunction::print(std::ostream &OS) const {
116 OS << "\n" << *(Value*)Fn->getReturnType() << " \"" << Fn->getName()<< "\"\n";
117
118 for (const_iterator BB = begin(); BB != end(); ++BB) {
119 BasicBlock *LBB = BB->getBasicBlock();
Chris Lattner2109f502002-12-15 20:35:25 +0000120 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
Chris Lattner10491642002-10-30 00:48:05 +0000121 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
122 OS << "\t";
123 (*I)->print(OS, Target);
124 }
125 }
126 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
127}
128
129
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000130// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000131// the MachineCodeForFunction object for the given function.
132// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000133// get() -- Returns a handle to the object.
134// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000135// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000136//
Misha Brukmanfce11432002-10-28 00:28:31 +0000137MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000138MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000139{
Chris Lattnere316efc2002-10-29 23:18:43 +0000140 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000141 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000142 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
143 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000144 return *mcInfo;
145}
146
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000147void
Chris Lattner335d5c32002-10-28 05:58:46 +0000148MachineFunction::destruct(const Function *Fn)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000149{
Chris Lattnere316efc2002-10-29 23:18:43 +0000150 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000151 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000152}
153
Chris Lattner335d5c32002-10-28 05:58:46 +0000154MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000155{
Chris Lattnere316efc2002-10-29 23:18:43 +0000156 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000157 assert(mc && "Call construct() method first to allocate the object");
158 return *mc;
159}
160
Chris Lattner831fdcf2002-12-25 05:03:22 +0000161void MachineFunction::clearSSARegMap() {
162 delete SSARegMapping;
163 SSARegMapping = 0;
164}
165
166
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000167static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000168ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
169 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000170{
171 const MachineFrameInfo& frameInfo = target.getFrameInfo();
172
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000173 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000174
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000175 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
176 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
177 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
178 {
179 unsigned numOperands = callInst->getNumOperands() - 1;
180 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
181 if (numExtra <= 0)
182 continue;
183
184 unsigned int sizeForThisCall;
185 if (frameInfo.argsOnStackHaveFixedSize())
186 {
187 int argSize = frameInfo.getSizeOfEachArgOnStack();
188 sizeForThisCall = numExtra * (unsigned) argSize;
189 }
190 else
191 {
192 assert(0 && "UNTESTED CODE: Size per stack argument is not "
193 "fixed on this architecture: use actual arg sizes to "
194 "compute MaxOptionalArgsSize");
195 sizeForThisCall = 0;
196 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +0000197 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000198 getOperand(i)->getType());
199 }
200
201 if (maxSize < sizeForThisCall)
202 maxSize = sizeForThisCall;
203
204 if ((int)maxOptionalNumArgs < numExtra)
205 maxOptionalNumArgs = (unsigned) numExtra;
206 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000207
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000208 return maxSize;
209}
210
211// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000212// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
213// but not higher than the alignment of the largest type we support
214// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000215//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000216// This function is similar to the corresponding function in EmitAssembly.cpp
217// but they are unrelated. This one does not align at more than a
218// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000219//
220inline unsigned int
221SizeToAlignment(unsigned int size, const TargetMachine& target)
222{
223 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
224 if (size > (unsigned) cacheLineSize / 2)
225 return cacheLineSize;
226 else
227 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000228 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000229 return sz;
230}
231
232
Chris Lattner88726182002-10-29 23:40:03 +0000233void MachineFunction::CalculateArgSize() {
234 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(Target, Fn,
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000235 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000236 staticStackSize = maxOptionalArgsSize
Chris Lattner88726182002-10-29 23:40:03 +0000237 + Target.getFrameInfo().getMinStackFrameSize();
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000238}
239
240int
Misha Brukmanfce11432002-10-28 00:28:31 +0000241MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000242 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000243 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000244 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000245{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000246 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000247 sizeToUse = target.findOptimalStorageSize(val->getType());
248 unsigned int align = SizeToAlignment(sizeToUse, target);
249
250 bool growUp;
251 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
252 growUp);
253 int offset = growUp? firstOffset + getAutomaticVarsSize()
254 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
255
256 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
257 getPaddedSize = sizeToUse + abs(aligned - offset);
258
259 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000260}
261
262int
Misha Brukmanfce11432002-10-28 00:28:31 +0000263MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000264 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000265 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000266{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000267 assert(! automaticVarsAreaFrozen &&
268 "Size of auto vars area has been used to compute an offset so "
269 "no more automatic vars should be allocated!");
270
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000271 // Check if we've allocated a stack slot for this value already
272 //
273 int offset = getOffset(val);
274 if (offset == INVALID_FRAME_OFFSET)
275 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000276 unsigned int getPaddedSize;
Chris Lattner6b944532002-10-28 01:16:38 +0000277 offset = computeOffsetforLocalVar(target, val, getPaddedSize, sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000278 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000279 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000280 }
281 return offset;
282}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000283
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000284int
Misha Brukmanfce11432002-10-28 00:28:31 +0000285MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000286 const Type* type)
287{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000288 assert(! spillsAreaFrozen &&
289 "Size of reg spills area has been used to compute an offset so "
290 "no more register spill slots should be allocated!");
291
Vikram S. Advecde39822002-10-11 16:10:53 +0000292 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000293 unsigned char align = target.DataLayout.getTypeAlignment(type);
294
295 bool growUp;
296 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
297
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000298 int offset = growUp? firstOffset + getRegSpillsSize()
299 : firstOffset - (getRegSpillsSize() + size);
300
301 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
302 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000303
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000304 incrementRegSpillsSize(size); // update size of reg. spills area
305
306 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000307}
308
309int
Misha Brukmanfce11432002-10-28 00:28:31 +0000310MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000311 unsigned int size)
312{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000313 unsigned int align = SizeToAlignment(size, target);
314
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000315 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000316 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
317
318 int offset = growUp? firstOffset + currentTmpValuesSize
319 : firstOffset - (currentTmpValuesSize + size);
320
321 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
322 size += abs(aligned - offset); // include alignment padding in size
323
324 incrementTmpAreaSize(size); // update "current" size of tmp area
325
326 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000327}
328
329void
Misha Brukmanfce11432002-10-28 00:28:31 +0000330MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000331{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000332 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000333}
334
335int
Misha Brukmanfce11432002-10-28 00:28:31 +0000336MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000337{
Chris Lattner09ff1122002-07-24 21:21:32 +0000338 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattner6b944532002-10-28 01:16:38 +0000339 return (pair == offsets.end()) ? INVALID_FRAME_OFFSET : pair->second;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000340}