blob: 0e940f88be1b1453efd4602a8cc62822f6bb5665 [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
Misha Brukmanfce11432002-10-28 00:28:31 +00009#include "llvm/CodeGen/MachineFunction.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000010#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 Lattnerf2868ce2002-02-03 07:54:50 +000016#include "llvm/iOther.h"
17#include <limits.h>
18
19const int INVALID_FRAME_OFFSET = INT_MAX; // std::numeric_limits<int>::max();
20
21static AnnotationID MCFM_AID(
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000022 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000023
24// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000025// the MachineCodeForFunction object for the given function.
26// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +000027// get() -- Returns a handle to the object.
28// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000029// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +000030//
Misha Brukmanfce11432002-10-28 00:28:31 +000031MachineFunction&
32MachineFunction::construct(const Function *M, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000033{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000034 assert(M->getAnnotation(MCFM_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000035 "Object already exists for this function!");
Misha Brukmanfce11432002-10-28 00:28:31 +000036 MachineFunction* mcInfo = new MachineFunction(M, Tar);
Chris Lattnerf2868ce2002-02-03 07:54:50 +000037 M->addAnnotation(mcInfo);
38 return *mcInfo;
39}
40
Vikram S. Adve89e2da02002-03-18 03:36:30 +000041void
Misha Brukmanfce11432002-10-28 00:28:31 +000042MachineFunction::destruct(const Function *M)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000043{
Chris Lattnerf2868ce2002-02-03 07:54:50 +000044 bool Deleted = M->deleteAnnotation(MCFM_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000045 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +000046}
47
Misha Brukmanfce11432002-10-28 00:28:31 +000048MachineFunction&
49MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +000050{
Misha Brukmanfce11432002-10-28 00:28:31 +000051 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MCFM_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +000052 assert(mc && "Call construct() method first to allocate the object");
53 return *mc;
54}
55
56static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +000057ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
58 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +000059{
60 const MachineFrameInfo& frameInfo = target.getFrameInfo();
61
Chris Lattner0b12b5f2002-06-25 16:13:21 +000062 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000063
Chris Lattner0b12b5f2002-06-25 16:13:21 +000064 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
65 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
66 if (const CallInst *callInst = dyn_cast<CallInst>(&*I))
67 {
68 unsigned numOperands = callInst->getNumOperands() - 1;
69 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
70 if (numExtra <= 0)
71 continue;
72
73 unsigned int sizeForThisCall;
74 if (frameInfo.argsOnStackHaveFixedSize())
75 {
76 int argSize = frameInfo.getSizeOfEachArgOnStack();
77 sizeForThisCall = numExtra * (unsigned) argSize;
78 }
79 else
80 {
81 assert(0 && "UNTESTED CODE: Size per stack argument is not "
82 "fixed on this architecture: use actual arg sizes to "
83 "compute MaxOptionalArgsSize");
84 sizeForThisCall = 0;
85 for (unsigned i = 0; i < numOperands; ++i)
Vikram S. Advecde39822002-10-11 16:10:53 +000086 sizeForThisCall += target.DataLayout.getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +000087 getOperand(i)->getType());
88 }
89
90 if (maxSize < sizeForThisCall)
91 maxSize = sizeForThisCall;
92
93 if ((int)maxOptionalNumArgs < numExtra)
94 maxOptionalNumArgs = (unsigned) numExtra;
95 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +000096
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.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000101// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
102// but not higher than the alignment of the largest type we support
103// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000104//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000105// This function is similar to the corresponding function in EmitAssembly.cpp
106// but they are unrelated. This one does not align at more than a
107// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000108//
109inline unsigned int
110SizeToAlignment(unsigned int size, const TargetMachine& target)
111{
112 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
113 if (size > (unsigned) cacheLineSize / 2)
114 return cacheLineSize;
115 else
116 for (unsigned sz=1; /*no condition*/; sz *= 2)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000117 if (sz >= size || sz >= target.DataLayout.getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000118 return sz;
119}
120
121
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000122/*ctor*/
Misha Brukmanfce11432002-10-28 00:28:31 +0000123MachineFunction::MachineFunction(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
Misha Brukmanfce11432002-10-28 00:28:31 +0000139MachineFunction::computeOffsetforLocalVar(const TargetMachine& target,
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000140 const Value* val,
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000141 unsigned int& getPaddedSize,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000142 unsigned int sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000143{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000144 if (sizeToUse == 0)
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000145 sizeToUse = target.findOptimalStorageSize(val->getType());
146 unsigned int align = SizeToAlignment(sizeToUse, target);
147
148 bool growUp;
149 int firstOffset = target.getFrameInfo().getFirstAutomaticVarOffset(*this,
150 growUp);
151 int offset = growUp? firstOffset + getAutomaticVarsSize()
152 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
153
154 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
155 getPaddedSize = sizeToUse + abs(aligned - offset);
156
157 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000158}
159
160int
Misha Brukmanfce11432002-10-28 00:28:31 +0000161MachineFunction::allocateLocalVar(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000162 const Value* val,
Chris Lattner0c0edf82002-07-25 06:17:51 +0000163 unsigned int sizeToUse)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000164{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000165 assert(! automaticVarsAreaFrozen &&
166 "Size of auto vars area has been used to compute an offset so "
167 "no more automatic vars should be allocated!");
168
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000169 // Check if we've allocated a stack slot for this value already
170 //
171 int offset = getOffset(val);
172 if (offset == INVALID_FRAME_OFFSET)
173 {
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000174 unsigned int getPaddedSize;
175 offset = this->computeOffsetforLocalVar(target, val, getPaddedSize,
176 sizeToUse);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000177 offsets[val] = offset;
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000178 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000179 }
180 return offset;
181}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000182
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000183int
Misha Brukmanfce11432002-10-28 00:28:31 +0000184MachineFunction::allocateSpilledValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000185 const Type* type)
186{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000187 assert(! spillsAreaFrozen &&
188 "Size of reg spills area has been used to compute an offset so "
189 "no more register spill slots should be allocated!");
190
Vikram S. Advecde39822002-10-11 16:10:53 +0000191 unsigned int size = target.DataLayout.getTypeSize(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000192 unsigned char align = target.DataLayout.getTypeAlignment(type);
193
194 bool growUp;
195 int firstOffset = target.getFrameInfo().getRegSpillAreaOffset(*this, growUp);
196
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000197 int offset = growUp? firstOffset + getRegSpillsSize()
198 : firstOffset - (getRegSpillsSize() + size);
199
200 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
201 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000202
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000203 incrementRegSpillsSize(size); // update size of reg. spills area
204
205 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000206}
207
208int
Misha Brukmanfce11432002-10-28 00:28:31 +0000209MachineFunction::pushTempValue(const TargetMachine& target,
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000210 unsigned int size)
211{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000212 unsigned int align = SizeToAlignment(size, target);
213
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000214 bool growUp;
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000215 int firstOffset = target.getFrameInfo().getTmpAreaOffset(*this, growUp);
216
217 int offset = growUp? firstOffset + currentTmpValuesSize
218 : firstOffset - (currentTmpValuesSize + size);
219
220 int aligned = target.getFrameInfo().adjustAlignment(offset, growUp, align);
221 size += abs(aligned - offset); // include alignment padding in size
222
223 incrementTmpAreaSize(size); // update "current" size of tmp area
224
225 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000226}
227
228void
Misha Brukmanfce11432002-10-28 00:28:31 +0000229MachineFunction::popAllTempValues(const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000230{
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000231 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000232}
233
234int
Misha Brukmanfce11432002-10-28 00:28:31 +0000235MachineFunction::getOffset(const Value* val) const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000236{
Chris Lattner09ff1122002-07-24 21:21:32 +0000237 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000238 return (pair == offsets.end())? INVALID_FRAME_OFFSET : pair->second;
239}
240
241void
Misha Brukmanfce11432002-10-28 00:28:31 +0000242MachineFunction::dump() const
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000243{
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000244 std::cerr << "\n" << method->getReturnType()
245 << " \"" << method->getName() << "\"\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000246
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000247 for (Function::const_iterator BB = method->begin(); BB != method->end(); ++BB)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000248 {
Anand Shuklae683f772002-07-11 00:17:17 +0000249 std::cerr << std::endl << (*BB).getName() << " (" << (const void*) BB << ")" << ":" << std::endl;
Vikram S. Adve432e1ca2002-07-08 23:03:54 +0000250 MachineCodeForBasicBlock& mvec = MachineCodeForBasicBlock::get(BB);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000251 for (unsigned i=0; i < mvec.size(); i++)
Chris Lattnerb91ca1f2002-02-24 23:01:56 +0000252 std::cerr << "\t" << *mvec[i];
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000253 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000254 std::cerr << "\nEnd function \"" << method->getName() << "\"\n\n";
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000255}