blob: e0b6ac63bb72551ec8e6a27ef4a939e9bb4ce604 [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.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000103// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
104// but not higher than the alignment of the largest type we support
105// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000106//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000107// This function is similar to the corresponding function in EmitAssembly.cpp
108// but they are unrelated. This one does not align at more than a
109// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000110//
111inline unsigned int
112SizeToAlignment(unsigned int size, const TargetMachine& target)
113{
114 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
115 if (size > (unsigned) cacheLineSize / 2)
116 return cacheLineSize;
117 else
118 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000119 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000120 return sz;
121}
122
123
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000124/*ctor*/
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000125MachineCodeForMethod::MachineCodeForMethod(const Function *F,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000126 const TargetMachine& target)
127 : Annotation(MCFM_AID),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000128 method(F), staticStackSize(0),
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000129 automaticVarsSize(0), regSpillsSize(0),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000130 maxOptionalArgsSize(0), maxOptionalNumArgs(0),
131 currentTmpValuesSize(0), maxTmpValuesSize(0), compiledAsLeaf(false),
132 spillsAreaFrozen(false), automaticVarsAreaFrozen(false)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000133{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000134 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(target, method,
135 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000136 staticStackSize = maxOptionalArgsSize
137 + target.getFrameInfo().getMinStackFrameSize();
138}
139
140int
141MachineCodeForMethod::computeOffsetforLocalVar(const TargetMachine& target,
142 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000143 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000144 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000145{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000146 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000147 sizeToUse = target.findOptimalStorageSize(val->getType());
148 unsigned int align = SizeToAlignment(sizeToUse, target);
149
150 bool growUp;
151 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
152 growUp);
153 int offset = growUp? firstOffset + getAutomaticVarsSize()
154 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
155
156 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
157 getPaddedSize = sizeToUse + abs(aligned - offset);
158
159 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000160}
161
162int
163MachineCodeForMethod::allocateLocalVar(const TargetMachine& target,
164 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000165 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000166{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000167 assert(! automaticVarsAreaFrozen &&
168 "Size of auto vars area has been used to compute an offset so "
169 "no more automatic vars should be allocated!");
170
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000171 // Check if we've allocated a stack slot for this value already
172 //
173 int offset = getOffset(val);
174 if (offset == INVALID_FRAME_OFFSET)
175 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000176 unsigned int getPaddedSize;
177 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
178 sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000179 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000180 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000181 }
182 return offset;
183}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000184
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000185int
186MachineCodeForMethod::allocateSpilledValue(const TargetMachine& target,
187 const Type* type)
188{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000189 assert(! spillsAreaFrozen &&
190 "Size of reg spills area has been used to compute an offset so "
191 "no more register spill slots should be allocated!");
192
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000193 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
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000199 int offset = growUp? firstOffset + getRegSpillsSize()
200 : firstOffset - (getRegSpillsSize() + size);
201
202 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
203 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000204
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000205 incrementRegSpillsSize(size); // update size of reg. spills area
206
207 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000208}
209
210int
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000211MachineCodeForMethod::pushTempValue(const TargetMachine& target,
212 unsigned int size)
213{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000214 unsigned int align = SizeToAlignment(size, target);
215
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000216 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000217 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
218
219 int offset = growUp? firstOffset + currentTmpValuesSize
220 : firstOffset - (currentTmpValuesSize + size);
221
222 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
223 size += abs(aligned - offset); // include alignment padding in size
224
225 incrementTmpAreaSize(size); // update "current" size of tmp area
226
227 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000228}
229
230void
231MachineCodeForMethod::popAllTempValues(const TargetMachine& target)
232{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000233 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000234}
235
236int
237MachineCodeForMethod::getOffset(const Value* val) const
238{
Chris Lattner09ff1122002-07-24 21:21:32 +0000239 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000240 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
241}
242
243void
244MachineCodeForMethod::dump() const
245{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000246 std::cerr << "\n" << method->getReturnType()
247 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000248
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000249 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000250 {
Anand Shuklae683f772002-07-11 00:17:17 +0000251 std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
Vikram S. Adve432e1ca2002-07-08 23:03:54 +0000252 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000253 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000254 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000255 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000256 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000257}