blob: 1f9a414883613d8fcd13840d31bb40e8b9b585c5 [file] [log] [blame]
Vikram S. Adve03d33bd2002-04-25 04:30:43 +00001//===-- MachineCodeForMethod.cpp -------------------------------------------=//
Chris Lattnerf2868ce2002-02-03 07:54:50 +00002//
3// Purpose:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00004// Collect native machine code information for a function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +00005// This allows target-specific information about the generated code
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00006// to be stored with each function.
Vikram S. Adve03d33bd2002-04-25 04:30:43 +00007//===---------------------------------------------------------------------===//
Chris Lattnerf2868ce2002-02-03 07:54:50 +00008
9#include "llvm/CodeGen/MachineCodeForMethod.h"
10#include "llvm/CodeGen/MachineInstr.h" // For debug output
Vikram S. Adve432e1ca2002-07-08 23:03:54 +000011#include "llvm/CodeGen/MachineCodeForBasicBlock.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 Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/BasicBlock.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000017#include "llvm/iOther.h"
18#include <limits.h>
Chris Lattnerb91ca1f2002-02-24 23:01:56 +000019#include <iostream>
Chris Lattnerf2868ce2002-02-03 07:54:50 +000020
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
26// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027// the MachineCodeForFunction object for the given function.
28// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +000029// get() -- Returns a handle to the object.
30// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000031// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +000032//
Vikram S. Adve89e2da02002-03-18 03:36:30 +000033MachineCodeForMethod&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000034MachineCodeForMethod::construct(const Function *M, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000035{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000036 assert(M->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000037 "Object already exists for this function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +000038 MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
39 M->addAnnotation(mcInfo);
40 return *mcInfo;
41}
42
Vikram S. Adve89e2da02002-03-18 03:36:30 +000043void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000044MachineCodeForMethod::destruct(const Function *M)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000045{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000046 bool Deleted = M->deleteAnnotation(MCFM_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000047 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +000048}
49
Vikram S. Adve89e2da02002-03-18 03:36:30 +000050MachineCodeForMethod&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000051MachineCodeForMethod::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000052{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000053 MachineCodeForMethod *mc = (MachineCodeForMethod*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +000054 assert(mc && "Call construct() method first to allocate the object");
55 return *mc;
56}
57
58static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +000059ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
60 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +000061{
62 const MachineFrameInfo& frameInfo = target.getFrameInfo();
63
Chris Lattner0b12b5f2002-06-25 16:13:21 +000064 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000065
Chris Lattner0b12b5f2002-06-25 16:13:21 +000066 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
67 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
68 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
69 {
70 unsigned numOperands = callInst->getNumOperands() - 1;
71 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
72 if (numExtra <= 0)
73 continue;
74
75 unsigned int sizeForThisCall;
76 if (frameInfo.argsOnStackHaveFixedSize())
77 {
78 int argSize = frameInfo.getSizeOfEachArgOnStack();
79 sizeForThisCall = numExtra * (unsigned) argSize;
80 }
81 else
82 {
83 assert(0 && "UNTESTED CODE: Size per stack argument is not "
84 "fixed on this architecture: use actual arg sizes to "
85 "compute MaxOptionalArgsSize");
86 sizeForThisCall = 0;
87 for (unsigned i = 0; i < numOperands; ++i)
88 sizeForThisCall += target.findOptimalStorageSize(callInst->
89 getOperand(i)->getType());
90 }
91
92 if (maxSize < sizeForThisCall)
93 maxSize = sizeForThisCall;
94
95 if ((int)maxOptionalNumArgs < numExtra)
96 maxOptionalNumArgs = (unsigned) numExtra;
97 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +000098
Chris Lattnerf2868ce2002-02-03 07:54:50 +000099 return maxSize;
100}
101
102// Align data larger than one L1 cache line on L1 cache line boundaries.
103// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
104//
105// THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
106// SHOULD BE USED DIRECTLY THERE
107//
108inline unsigned int
109SizeToAlignment(unsigned int size, const TargetMachine& target)
110{
111 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
112 if (size > (unsigned) cacheLineSize / 2)
113 return cacheLineSize;
114 else
115 for (unsigned sz=1; /*no condition*/; sz *= 2)
116 if (sz >= size)
117 return sz;
118}
119
120
121
122/*ctor*/
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000123MachineCodeForMethod::MachineCodeForMethod(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000124 const TargetMachine& target)
125 : Annotation(MCFM_AID),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000126 method(F), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000127 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000128 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
129 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
130 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000131{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000132 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
133 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000134 staticStackSize = maxOptionalArgsSize
135 + target.getFrameInfo().getMinStackFrameSize();
136}
137
138int
139MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
140 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000141 unsigned int& getPaddedSize,
142 unsigned int sizeToUse = 0)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000143{
144 bool growUp;
145 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
146 growUp);
147 unsigned char align;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000148 if (sizeToUse == 0)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000149 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000150 sizeToUse = target.findOptimalStorageSize(val->getType());
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000151 // align = target.DataLayout.getTypeAlignment(val->getType());
152 }
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000153
154 align = SizeToAlignment(sizeToUse, target);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000155
156 int offset = getAutomaticVarsSize();
157 if (! growUp)
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000158 offset += sizeToUse;
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000159
160 if (unsigned int mod = offset % align)
161 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000162 offset += align - mod;
163 getPaddedSize = sizeToUse + align - mod;
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000164 }
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000165 else
166 getPaddedSize = sizeToUse;
167
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000168 offset = growUp? firstOffset + offset
169 : firstOffset - offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000170
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000171 return offset;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000172}
173
174int
175MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
176 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000177 unsigned int sizeToUse = 0)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000178{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000179 assert(! automaticVarsAreaFrozen &&
180 "Size of auto vars area has been used to compute an offset so "
181 "no more automatic vars should be allocated!");
182
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000183 // Check if we've allocated a stack slot for this value already
184 //
185 int offset = getOffset(val);
186 if (offset == INVALID_FRAME_OFFSET)
187 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000188 unsigned int getPaddedSize;
189 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
190 sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000191 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000192 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000193 }
194 return offset;
195}
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000196
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000197int
198MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
199 const Type* type)
200{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000201 assert(! spillsAreaFrozen &&
202 "Size of reg spills area has been used to compute an offset so "
203 "no more register spill slots should be allocated!");
204
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000205 unsigned int size = target.findOptimalStorageSize(type);
206 unsigned char align = target.DataLayout.getTypeAlignment(type);
207
208 bool growUp;
209 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
210
211 int offset = getRegSpillsSize();
212 if (! growUp)
213 offset += size;
214
215 if (unsigned int mod = offset % align)
216 {
217 offset += align - mod;
218 size += align - mod;
219 }
220
221 offset = growUp? firstOffset + offset
222 : firstOffset - offset;
223
224 incrementRegSpillsSize(size);
225
226 return offset;
227}
228
229int
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000230MachineCodeForMethod::pushTempValue(const TargetMachine& target,
231 unsigned int size)
232{
233 // Compute a power-of-2 alignment according to the possible sizes,
234 // but not greater than the alignment of the largest type we support
235 // (currently a double word -- see class TargetData).
236 unsigned char align = 1;
237 for (; align < size && align < target.DataLayout.getDoubleAlignment();
238 align = 2*align)
239 ;
240
241 bool growUp;
242 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
243
244 int offset = currentTmpValuesSize;
245 if (! growUp)
246 offset += size;
247
248 if (unsigned int mod = offset % align)
249 {
250 offset += align - mod;
251 size += align - mod;
252 }
253
254 offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
255
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000256 incrementTmpAreaSize(size);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000257 return offset;
258}
259
260void
261MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
262{
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000263 resetTmpAreaSize();
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000264}
265
266int
267MachineCodeForMethod::getOffset(const Value* val) const
268{
269 std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
270 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
271}
272
273void
274MachineCodeForMethod::dump() const
275{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000276 std::cerr << "\n" << method->getReturnType()
277 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000278
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000279 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000280 {
Vikram S. Adve432e1ca2002-07-08 23:03:54 +0000281 std::cerr << "\n" << BB->getName() << " (" << (const void*) BB << ")" << ":\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000282
Vikram S. Adve432e1ca2002-07-08 23:03:54 +0000283 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000284 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000285 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000286 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000287 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000288}