blob: bb52ef298e5ab4bf4cf65c6870c0716cd2699f04 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===-- MachineCodeForFunction.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.
Chris Lattnerf2868ce2002-02-03 07:54:50 +00007//===----------------------------------------------------------------------===//
8
9#include "llvm/CodeGen/MachineCodeForMethod.h"
10#include "llvm/CodeGen/MachineInstr.h" // For debug output
11#include "llvm/Target/TargetMachine.h"
12#include "llvm/Target/MachineFrameInfo.h"
13#include "llvm/Target/MachineCacheInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000014#include "llvm/Function.h"
Chris Lattner221d6882002-02-12 21:07:25 +000015#include "llvm/BasicBlock.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000016#include "llvm/iOther.h"
17#include <limits.h>
Chris Lattnerb91ca1f2002-02-24 23:01:56 +000018#include <iostream>
Chris Lattnerf2868ce2002-02-03 07:54:50 +000019
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
25// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026// the MachineCodeForFunction object for the given function.
27// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +000028// get() -- Returns a handle to the object.
29// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000030// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +000031//
Vikram S. Adve89e2da02002-03-18 03:36:30 +000032MachineCodeForMethod&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000033MachineCodeForMethod::construct(const Function *M, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000034{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000035 assert(M->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000036 "Object already exists for this function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +000037 MachineCodeForMethod* mcInfo = new MachineCodeForMethod(M, Tar);
38 M->addAnnotation(mcInfo);
39 return *mcInfo;
40}
41
Vikram S. Adve89e2da02002-03-18 03:36:30 +000042void
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000043MachineCodeForMethod::destruct(const Function *M)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000044{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000045 bool Deleted = M->deleteAnnotation(MCFM_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000046 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +000047}
48
Vikram S. Adve89e2da02002-03-18 03:36:30 +000049MachineCodeForMethod&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000050MachineCodeForMethod::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000051{
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000052 MachineCodeForMethod *mc = (MachineCodeForMethod*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +000053 assert(mc && "Call construct() method first to allocate the object");
54 return *mc;
55}
56
57static unsigned
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000058ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F)
Chris Lattnerf2868ce2002-02-03 07:54:50 +000059{
60 const MachineFrameInfo& frameInfo = target.getFrameInfo();
61
62 unsigned int maxSize = 0;
63
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000064 for (Function::const_iterator MI = F->begin(), ME = F->end(); MI != ME; ++MI)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000065 {
66 const BasicBlock *BB = *MI;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000067 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000068 if (CallInst *callInst = dyn_cast<CallInst>(*I))
Chris Lattnerf2868ce2002-02-03 07:54:50 +000069 {
Vikram S. Adve89e2da02002-03-18 03:36:30 +000070 unsigned int numOperands = callInst->getNumOperands() - 1;
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000071 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
Vikram S. Adve89e2da02002-03-18 03:36:30 +000072 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 {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000083 assert(0 && "UNTESTED CODE: Size per stack argument is not "
84 "fixed on this architecture: use actual arg sizes to "
85 "compute MaxOptionalArgsSize");
Vikram S. Adve89e2da02002-03-18 03:36:30 +000086 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;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000094 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +000095 }
96
Chris Lattnerf2868ce2002-02-03 07:54:50 +000097 return maxSize;
98}
99
100// Align data larger than one L1 cache line on L1 cache line boundaries.
101// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
102//
103// THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
104// SHOULD BE USED DIRECTLY THERE
105//
106inline unsigned int
107SizeToAlignment(unsigned int size, const TargetMachine& target)
108{
109 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
110 if (size > (unsigned) cacheLineSize / 2)
111 return cacheLineSize;
112 else
113 for (unsigned sz=1; /*no condition*/; sz *= 2)
114 if (sz >= size)
115 return sz;
116}
117
118
119
120/*ctor*/
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000121MachineCodeForMethod::MachineCodeForMethod(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000122 const TargetMachine& target)
123 : Annotation(MCFM_AID),
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000124 method(F), compiledAsLeaf(false), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000125 automaticVarsSize(0), regSpillsSize(0),
126 currentOptionalArgsSize(0), maxOptionalArgsSize(0),
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000127 currentTmpValuesSize(0), maxTmpValuesSize(0)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000128{
129 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000130 staticStackSize = maxOptionalArgsSize
131 + target.getFrameInfo().getMinStackFrameSize();
132}
133
134int
135MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
136 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000137 unsigned int& getPaddedSize,
138 unsigned int sizeToUse = 0)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000139{
140 bool growUp;
141 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
142 growUp);
143 unsigned char align;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000144 if (sizeToUse == 0)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000145 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000146 sizeToUse = target.findOptimalStorageSize(val->getType());
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000147 // align = target.DataLayout.getTypeAlignment(val->getType());
148 }
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000149
150 align = SizeToAlignment(sizeToUse, target);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000151
152 int offset = getAutomaticVarsSize();
153 if (! growUp)
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000154 offset += sizeToUse;
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000155
156 if (unsigned int mod = offset % align)
157 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000158 offset += align - mod;
159 getPaddedSize = sizeToUse + align - mod;
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000160 }
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000161 else
162 getPaddedSize = sizeToUse;
163
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000164 offset = growUp? firstOffset + offset
165 : firstOffset - offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000166
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000167 return offset;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000168}
169
170int
171MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
172 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000173 unsigned int sizeToUse = 0)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000174{
175 // Check if we've allocated a stack slot for this value already
176 //
177 int offset = getOffset(val);
178 if (offset == INVALID_FRAME_OFFSET)
179 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000180 unsigned int getPaddedSize;
181 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
182 sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000183 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000184 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000185 }
186 return offset;
187}
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000188
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000189int
190MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
191 const Type* type)
192{
193 unsigned int size = target.findOptimalStorageSize(type);
194 unsigned char align = target.DataLayout.getTypeAlignment(type);
195
196 bool growUp;
197 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
198
199 int offset = getRegSpillsSize();
200 if (! growUp)
201 offset += size;
202
203 if (unsigned int mod = offset % align)
204 {
205 offset += align - mod;
206 size += align - mod;
207 }
208
209 offset = growUp? firstOffset + offset
210 : firstOffset - offset;
211
212 incrementRegSpillsSize(size);
213
214 return offset;
215}
216
217int
218MachineCodeForMethod::allocateOptionalArg(const TargetMachine& target,
219 const Type* type)
220{
221 const MachineFrameInfo& frameInfo = target.getFrameInfo();
222
223 int size = INT_MAX;
224 if (frameInfo.argsOnStackHaveFixedSize())
225 size = frameInfo.getSizeOfEachArgOnStack();
226 else
227 {
228 size = target.findOptimalStorageSize(type);
229 assert(0 && "UNTESTED CODE: Size per stack argument is not fixed on this architecture: use actual argument sizes for computing optional arg offsets");
230 }
231 unsigned char align = target.DataLayout.getTypeAlignment(type);
232
233 bool growUp;
234 int firstOffset = frameInfo.getFirstOptionalOutgoingArgOffset(*this, growUp);
235
236 int offset = getCurrentOptionalArgsSize();
237 if (! growUp)
238 offset += size;
239
240 if (unsigned int mod = offset % align)
241 {
242 offset += align - mod;
243 size += align - mod;
244 }
245
246 offset = growUp? firstOffset + offset
247 : firstOffset - offset;
248
249 incrementCurrentOptionalArgsSize(size);
250
251 return offset;
252}
253
254void
255MachineCodeForMethod::resetOptionalArgs(const TargetMachine& target)
256{
257 currentOptionalArgsSize = 0;
258}
259
260int
261MachineCodeForMethod::pushTempValue(const TargetMachine& target,
262 unsigned int size)
263{
264 // Compute a power-of-2 alignment according to the possible sizes,
265 // but not greater than the alignment of the largest type we support
266 // (currently a double word -- see class TargetData).
267 unsigned char align = 1;
268 for (; align < size && align < target.DataLayout.getDoubleAlignment();
269 align = 2*align)
270 ;
271
272 bool growUp;
273 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
274
275 int offset = currentTmpValuesSize;
276 if (! growUp)
277 offset += size;
278
279 if (unsigned int mod = offset % align)
280 {
281 offset += align - mod;
282 size += align - mod;
283 }
284
285 offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
286
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000287 incrementTmpAreaSize(size);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000288 return offset;
289}
290
291void
292MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
293{
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000294 resetTmpAreaSize();
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000295}
296
297int
298MachineCodeForMethod::getOffset(const Value* val) const
299{
300 std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
301 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
302}
303
304void
305MachineCodeForMethod::dump() const
306{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000307 std::cerr << "\n" << method->getReturnType()
308 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000309
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000310 for (Function::const_iterator BI = method->begin(); BI != method->end(); ++BI)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000311 {
312 BasicBlock* bb = *BI;
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000313 std::cerr << "\n" << bb->getName() << " (" << bb << ")" << ":\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000314
315 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
316 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000317 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000318 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000319 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000320}