blob: 4d50f89729f594fbdd353c64c23d2624124ec7e5 [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
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
Vikram S. Adve03d33bd2002-04-25 04:30:43 +000058ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
59 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +000060{
61 const MachineFrameInfo& frameInfo = target.getFrameInfo();
62
Chris Lattner0b12b5f2002-06-25 16:13:21 +000063 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000064
Chris Lattner0b12b5f2002-06-25 16:13:21 +000065 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
66 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
67 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
68 {
69 unsigned numOperands = callInst->getNumOperands() - 1;
70 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
71 if (numExtra <= 0)
72 continue;
73
74 unsigned int sizeForThisCall;
75 if (frameInfo.argsOnStackHaveFixedSize())
76 {
77 int argSize = frameInfo.getSizeOfEachArgOnStack();
78 sizeForThisCall = numExtra * (unsigned) argSize;
79 }
80 else
81 {
82 assert(0 && "UNTESTED CODE: Size per stack argument is not "
83 "fixed on this architecture: use actual arg sizes to "
84 "compute MaxOptionalArgsSize");
85 sizeForThisCall = 0;
86 for (unsigned i = 0; i < numOperands; ++i)
87 sizeForThisCall += target.findOptimalStorageSize(callInst->
88 getOperand(i)->getType());
89 }
90
91 if (maxSize < sizeForThisCall)
92 maxSize = sizeForThisCall;
93
94 if ((int)maxOptionalNumArgs < numExtra)
95 maxOptionalNumArgs = (unsigned) numExtra;
96 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +000097
Chris Lattnerf2868ce2002-02-03 07:54:50 +000098 return maxSize;
99}
100
101// Align data larger than one L1 cache line on L1 cache line boundaries.
102// Align all smaller data on the next higher 2^x boundary (4, 8, ...).
103//
104// THIS FUNCTION HAS BEEN COPIED FROM EMITASSEMBLY.CPP AND
105// SHOULD BE USED DIRECTLY THERE
106//
107inline unsigned int
108SizeToAlignment(unsigned int size, const TargetMachine& target)
109{
110 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
111 if (size > (unsigned) cacheLineSize / 2)
112 return cacheLineSize;
113 else
114 for (unsigned sz=1; /*no condition*/; sz *= 2)
115 if (sz >= size)
116 return sz;
117}
118
119
120
121/*ctor*/
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000122MachineCodeForMethod::MachineCodeForMethod(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000123 const TargetMachine& target)
124 : Annotation(MCFM_AID),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000125 method(F), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000126 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000127 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
128 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
129 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000130{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000131 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
132 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000133 staticStackSize = maxOptionalArgsSize
134 + target.getFrameInfo().getMinStackFrameSize();
135}
136
137int
138MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
139 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000140 unsigned int& getPaddedSize,
141 unsigned int sizeToUse = 0)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000142{
143 bool growUp;
144 int firstOffset =target.getFrameInfo().getFirstAutomaticVarOffset(*this,
145 growUp);
146 unsigned char align;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000147 if (sizeToUse == 0)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000148 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000149 sizeToUse = target.findOptimalStorageSize(val->getType());
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000150 // align = target.DataLayout.getTypeAlignment(val->getType());
151 }
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000152
153 align = SizeToAlignment(sizeToUse, target);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000154
155 int offset = getAutomaticVarsSize();
156 if (! growUp)
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000157 offset += sizeToUse;
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000158
159 if (unsigned int mod = offset % align)
160 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000161 offset += align - mod;
162 getPaddedSize = sizeToUse + align - mod;
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000163 }
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000164 else
165 getPaddedSize = sizeToUse;
166
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000167 offset = growUp? firstOffset + offset
168 : firstOffset - offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000169
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000170 return offset;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000171}
172
173int
174MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
175 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000176 unsigned int sizeToUse = 0)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000177{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000178 assert(! automaticVarsAreaFrozen &&
179 "Size of auto vars area has been used to compute an offset so "
180 "no more automatic vars should be allocated!");
181
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000182 // Check if we've allocated a stack slot for this value already
183 //
184 int offset = getOffset(val);
185 if (offset == INVALID_FRAME_OFFSET)
186 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000187 unsigned int getPaddedSize;
188 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
189 sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000190 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000191 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000192 }
193 return offset;
194}
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000195
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000196int
197MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
198 const Type* type)
199{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000200 assert(! spillsAreaFrozen &&
201 "Size of reg spills area has been used to compute an offset so "
202 "no more register spill slots should be allocated!");
203
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000204 unsigned int size = target.findOptimalStorageSize(type);
205 unsigned char align = target.DataLayout.getTypeAlignment(type);
206
207 bool growUp;
208 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
209
210 int offset = getRegSpillsSize();
211 if (! growUp)
212 offset += size;
213
214 if (unsigned int mod = offset % align)
215 {
216 offset += align - mod;
217 size += align - mod;
218 }
219
220 offset = growUp? firstOffset + offset
221 : firstOffset - offset;
222
223 incrementRegSpillsSize(size);
224
225 return offset;
226}
227
228int
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000229MachineCodeForMethod::pushTempValue(const TargetMachine& target,
230 unsigned int size)
231{
232 // Compute a power-of-2 alignment according to the possible sizes,
233 // but not greater than the alignment of the largest type we support
234 // (currently a double word -- see class TargetData).
235 unsigned char align = 1;
236 for (; align < size && align < target.DataLayout.getDoubleAlignment();
237 align = 2*align)
238 ;
239
240 bool growUp;
241 int firstTmpOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
242
243 int offset = currentTmpValuesSize;
244 if (! growUp)
245 offset += size;
246
247 if (unsigned int mod = offset % align)
248 {
249 offset += align - mod;
250 size += align - mod;
251 }
252
253 offset = growUp ? firstTmpOffset + offset : firstTmpOffset - offset;
254
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000255 incrementTmpAreaSize(size);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000256 return offset;
257}
258
259void
260MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
261{
Vikram S. Advefa79e6e2002-03-31 18:57:49 +0000262 resetTmpAreaSize();
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000263}
264
265int
266MachineCodeForMethod::getOffset(const Value* val) const
267{
268 std::hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
269 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
270}
271
272void
273MachineCodeForMethod::dump() const
274{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000275 std::cerr << "\n" << method->getReturnType()
276 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000277
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000278 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000279 {
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000280 std::cerr << "\n" << BB->getName() << " (" << *BB << ")" << ":\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000281
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000282 MachineCodeForBasicBlock& mvec = BB->getMachineInstrVec();
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000283 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000284 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000285 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000286 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000287}