blob: 66497d964a8162af89c23acaa9e3f4d3d2fa4c7c [file] [log] [blame]
Chris Lattner8494d082002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Chris Lattnereda6bd72002-02-03 07:54:50 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner8494d082002-10-28 01:16:38 +000010// Collect native machine code information for a function. This allows
11// target-specific information about the generated code to be stored with each
12// function.
13//
14//===----------------------------------------------------------------------===//
Chris Lattnereda6bd72002-02-03 07:54:50 +000015
Chris Lattner8494d082002-10-28 01:16:38 +000016#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner448fb452002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner6d8a6c62002-10-28 01:12:41 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner448fb452002-12-25 05:03:22 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner32525642002-12-28 20:37:16 +000020#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattnerca4362f2002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc6807e82003-01-13 00:23:03 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnereda6bd72002-02-03 07:54:50 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner871e5912002-12-28 21:00:25 +000024#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner42d59212002-12-29 02:50:35 +000025#include "llvm/Target/TargetCacheInfo.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000026#include "llvm/Function.h"
Chris Lattnereda6bd72002-02-03 07:54:50 +000027#include "llvm/iOther.h"
Chris Lattner6d8a6c62002-10-28 01:12:41 +000028#include "llvm/Pass.h"
Chris Lattner9a3478e2003-12-20 09:17:07 +000029using namespace llvm;
Chris Lattnereda6bd72002-02-03 07:54:50 +000030
Chris Lattnerbbd68ad2002-10-29 23:18:43 +000031static AnnotationID MF_AID(
Chris Lattner62b7fd12002-04-07 20:49:59 +000032 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnereda6bd72002-02-03 07:54:50 +000033
Chris Lattner6d8a6c62002-10-28 01:12:41 +000034
Chris Lattner6d8a6c62002-10-28 01:12:41 +000035namespace {
Chris Lattner214808f2002-10-30 00:48:05 +000036 struct Printer : public FunctionPass {
37 const char *getPassName() const { return "MachineFunction Printer"; }
38
39 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.setPreservesAll();
41 }
42
43 bool runOnFunction(Function &F) {
44 MachineFunction::get(&F).dump();
45 return false;
46 }
47 };
Chris Lattner6d8a6c62002-10-28 01:12:41 +000048}
49
Chris Lattner9a3478e2003-12-20 09:17:07 +000050FunctionPass *llvm::createMachineFunctionPrinterPass() {
Chris Lattner214808f2002-10-30 00:48:05 +000051 return new Printer();
52}
53
Chris Lattner6d8a6c62002-10-28 01:12:41 +000054
55//===---------------------------------------------------------------------===//
56// MachineFunction implementation
57//===---------------------------------------------------------------------===//
58
Chris Lattner214808f2002-10-30 00:48:05 +000059MachineFunction::MachineFunction(const Function *F,
Chris Lattner32525642002-12-28 20:37:16 +000060 const TargetMachine &TM)
61 : Annotation(MF_AID), Fn(F), Target(TM) {
Misha Brukmand5b111a2002-11-20 18:55:27 +000062 SSARegMapping = new SSARegMap();
Chris Lattner32525642002-12-28 20:37:16 +000063 MFInfo = new MachineFunctionInfo(*this);
Chris Lattnerca4362f2002-12-28 21:08:26 +000064 FrameInfo = new MachineFrameInfo();
Chris Lattnerc6807e82003-01-13 00:23:03 +000065 ConstantPool = new MachineConstantPool();
Chris Lattner448fb452002-12-25 05:03:22 +000066}
67
68MachineFunction::~MachineFunction() {
69 delete SSARegMapping;
Chris Lattner32525642002-12-28 20:37:16 +000070 delete MFInfo;
71 delete FrameInfo;
Chris Lattnerc6807e82003-01-13 00:23:03 +000072 delete ConstantPool;
Chris Lattner214808f2002-10-30 00:48:05 +000073}
74
75void MachineFunction::dump() const { print(std::cerr); }
76
77void MachineFunction::print(std::ostream &OS) const {
Chris Lattner32525642002-12-28 20:37:16 +000078 OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName()
79 << "\"\n";
80
81 // Print Frame Information
Chris Lattnereb45c982003-01-16 18:35:57 +000082 getFrameInfo()->print(*this, OS);
Chris Lattnerc6807e82003-01-13 00:23:03 +000083
84 // Print Constant Pool
85 getConstantPool()->print(OS);
Chris Lattner214808f2002-10-30 00:48:05 +000086
87 for (const_iterator BB = begin(); BB != end(); ++BB) {
Chris Lattner414832f2003-07-26 23:24:56 +000088 const BasicBlock *LBB = BB->getBasicBlock();
Chris Lattnerf8954182002-12-15 20:35:25 +000089 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
Chris Lattner214808f2002-10-30 00:48:05 +000090 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
91 OS << "\t";
92 (*I)->print(OS, Target);
93 }
94 }
95 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
96}
97
98
Chris Lattnereda6bd72002-02-03 07:54:50 +000099// The next two methods are used to construct and to retrieve
Chris Lattner62b7fd12002-04-07 20:49:59 +0000100// the MachineCodeForFunction object for the given function.
101// construct() -- Allocates and initializes for a given function and target
Chris Lattnereda6bd72002-02-03 07:54:50 +0000102// get() -- Returns a handle to the object.
103// This should not be called before "construct()"
Chris Lattner62b7fd12002-04-07 20:49:59 +0000104// for a given Function.
Chris Lattnereda6bd72002-02-03 07:54:50 +0000105//
Misha Brukman7ae7f842002-10-28 00:28:31 +0000106MachineFunction&
Chris Lattnerba3a8062002-10-28 05:58:46 +0000107MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000108{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000109 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner62b7fd12002-04-07 20:49:59 +0000110 "Object already exists for this function!");
Chris Lattnerba3a8062002-10-28 05:58:46 +0000111 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
112 Fn->addAnnotation(mcInfo);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000113 return *mcInfo;
114}
115
Vikram S. Adve7446b322002-03-18 03:36:30 +0000116void
Chris Lattnerba3a8062002-10-28 05:58:46 +0000117MachineFunction::destruct(const Function *Fn)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000118{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000119 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner62b7fd12002-04-07 20:49:59 +0000120 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnereda6bd72002-02-03 07:54:50 +0000121}
122
Chris Lattnerba3a8062002-10-28 05:58:46 +0000123MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000124{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000125 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000126 assert(mc && "Call construct() method first to allocate the object");
127 return *mc;
128}
129
Chris Lattner448fb452002-12-25 05:03:22 +0000130void MachineFunction::clearSSARegMap() {
131 delete SSARegMapping;
132 SSARegMapping = 0;
133}
134
Chris Lattner32525642002-12-28 20:37:16 +0000135//===----------------------------------------------------------------------===//
Chris Lattnerca4362f2002-12-28 21:08:26 +0000136// MachineFrameInfo implementation
Chris Lattner32525642002-12-28 20:37:16 +0000137//===----------------------------------------------------------------------===//
138
Chris Lattnerc6807e82003-01-13 00:23:03 +0000139/// CreateStackObject - Create a stack object for a value of the specified type.
140///
141int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
142 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
143}
144
145int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
146 return CreateStackObject(RC->getSize(), RC->getAlignment());
147}
148
149
Chris Lattnereb45c982003-01-16 18:35:57 +0000150void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
151 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
152
Chris Lattner32525642002-12-28 20:37:16 +0000153 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
154 const StackObject &SO = Objects[i];
Chris Lattnerc6807e82003-01-13 00:23:03 +0000155 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
Chris Lattner32525642002-12-28 20:37:16 +0000156 if (SO.Size == 0)
157 OS << "variable sized";
158 else
159 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
160
161 if (i < NumFixedObjects)
162 OS << " fixed";
163 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnereb45c982003-01-16 18:35:57 +0000164 int Off = SO.SPOffset + ValOffset;
Chris Lattner32525642002-12-28 20:37:16 +0000165 OS << " at location [SP";
Chris Lattnereb45c982003-01-16 18:35:57 +0000166 if (Off > 0)
167 OS << "+" << Off;
168 else if (Off < 0)
169 OS << Off;
Chris Lattner32525642002-12-28 20:37:16 +0000170 OS << "]";
171 }
172 OS << "\n";
173 }
174
175 if (HasVarSizedObjects)
176 OS << " Stack frame contains variable sized objects\n";
177}
178
Chris Lattnereb45c982003-01-16 18:35:57 +0000179void MachineFrameInfo::dump(const MachineFunction &MF) const {
180 print(MF, std::cerr);
181}
Chris Lattner32525642002-12-28 20:37:16 +0000182
183
184//===----------------------------------------------------------------------===//
Chris Lattnerc6807e82003-01-13 00:23:03 +0000185// MachineConstantPool implementation
186//===----------------------------------------------------------------------===//
187
188void MachineConstantPool::print(std::ostream &OS) const {
189 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
190 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
191}
192
193void MachineConstantPool::dump() const { print(std::cerr); }
194
195//===----------------------------------------------------------------------===//
Chris Lattner32525642002-12-28 20:37:16 +0000196// MachineFunctionInfo implementation
197//===----------------------------------------------------------------------===//
Chris Lattner448fb452002-12-25 05:03:22 +0000198
Chris Lattnereda6bd72002-02-03 07:54:50 +0000199static unsigned
Vikram S. Adve70927722002-04-25 04:30:43 +0000200ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
201 unsigned &maxOptionalNumArgs)
Chris Lattnereda6bd72002-02-03 07:54:50 +0000202{
Chris Lattner32525642002-12-28 20:37:16 +0000203 const TargetFrameInfo &frameInfo = target.getFrameInfo();
Chris Lattnereda6bd72002-02-03 07:54:50 +0000204
Chris Lattner7076ff22002-06-25 16:13:21 +0000205 unsigned maxSize = 0;
Chris Lattnereda6bd72002-02-03 07:54:50 +0000206
Chris Lattner7076ff22002-06-25 16:13:21 +0000207 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
208 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattner6ee2cf52003-04-23 16:36:11 +0000209 if (const CallInst *callInst = dyn_cast<CallInst>(I))
Chris Lattner7076ff22002-06-25 16:13:21 +0000210 {
211 unsigned numOperands = callInst->getNumOperands() - 1;
212 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
213 if (numExtra <= 0)
214 continue;
215
Chris Lattner32525642002-12-28 20:37:16 +0000216 unsigned sizeForThisCall;
Chris Lattner7076ff22002-06-25 16:13:21 +0000217 if (frameInfo.argsOnStackHaveFixedSize())
218 {
219 int argSize = frameInfo.getSizeOfEachArgOnStack();
220 sizeForThisCall = numExtra * (unsigned) argSize;
221 }
222 else
223 {
224 assert(0 && "UNTESTED CODE: Size per stack argument is not "
225 "fixed on this architecture: use actual arg sizes to "
226 "compute MaxOptionalArgsSize");
227 sizeForThisCall = 0;
228 for (unsigned i = 0; i < numOperands; ++i)
Chris Lattner32525642002-12-28 20:37:16 +0000229 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
Chris Lattner7076ff22002-06-25 16:13:21 +0000230 getOperand(i)->getType());
231 }
232
233 if (maxSize < sizeForThisCall)
234 maxSize = sizeForThisCall;
235
236 if ((int)maxOptionalNumArgs < numExtra)
237 maxOptionalNumArgs = (unsigned) numExtra;
238 }
Vikram S. Adve7446b322002-03-18 03:36:30 +0000239
Chris Lattnereda6bd72002-02-03 07:54:50 +0000240 return maxSize;
241}
242
243// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Advee5671792002-09-16 15:18:16 +0000244// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
245// but not higher than the alignment of the largest type we support
246// (currently a double word). -- see class TargetData).
Chris Lattnereda6bd72002-02-03 07:54:50 +0000247//
Vikram S. Advee5671792002-09-16 15:18:16 +0000248// This function is similar to the corresponding function in EmitAssembly.cpp
249// but they are unrelated. This one does not align at more than a
250// double-word boundary whereas that one might.
Chris Lattnereda6bd72002-02-03 07:54:50 +0000251//
Chris Lattner32525642002-12-28 20:37:16 +0000252inline unsigned
253SizeToAlignment(unsigned size, const TargetMachine& target)
Chris Lattnereda6bd72002-02-03 07:54:50 +0000254{
255 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
256 if (size > (unsigned) cacheLineSize / 2)
257 return cacheLineSize;
258 else
259 for (unsigned sz=1; /*no condition*/; sz *= 2)
Chris Lattner32525642002-12-28 20:37:16 +0000260 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
Chris Lattnereda6bd72002-02-03 07:54:50 +0000261 return sz;
262}
263
264
Chris Lattner32525642002-12-28 20:37:16 +0000265void MachineFunctionInfo::CalculateArgSize() {
266 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
267 MF.getFunction(),
Vikram S. Adve70927722002-04-25 04:30:43 +0000268 maxOptionalNumArgs);
Vikram S. Adve7446b322002-03-18 03:36:30 +0000269 staticStackSize = maxOptionalArgsSize
Chris Lattner32525642002-12-28 20:37:16 +0000270 + MF.getTarget().getFrameInfo().getMinStackFrameSize();
Vikram S. Adve7446b322002-03-18 03:36:30 +0000271}
272
273int
Chris Lattner32525642002-12-28 20:37:16 +0000274MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
275 unsigned &getPaddedSize,
276 unsigned sizeToUse)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000277{
Vikram S. Adve377646f2002-03-24 03:39:26 +0000278 if (sizeToUse == 0)
Chris Lattner32525642002-12-28 20:37:16 +0000279 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
280 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
Vikram S. Advee5671792002-09-16 15:18:16 +0000281
282 bool growUp;
Chris Lattner32525642002-12-28 20:37:16 +0000283 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
284 growUp);
Vikram S. Advee5671792002-09-16 15:18:16 +0000285 int offset = growUp? firstOffset + getAutomaticVarsSize()
286 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
287
Chris Lattner32525642002-12-28 20:37:16 +0000288 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
Vikram S. Advee5671792002-09-16 15:18:16 +0000289 getPaddedSize = sizeToUse + abs(aligned - offset);
290
291 return aligned;
Chris Lattnereda6bd72002-02-03 07:54:50 +0000292}
293
Chris Lattner9a3478e2003-12-20 09:17:07 +0000294
295int MachineFunctionInfo::allocateLocalVar(const Value* val,
296 unsigned sizeToUse) {
Vikram S. Adve70927722002-04-25 04:30:43 +0000297 assert(! automaticVarsAreaFrozen &&
298 "Size of auto vars area has been used to compute an offset so "
299 "no more automatic vars should be allocated!");
300
Chris Lattnereda6bd72002-02-03 07:54:50 +0000301 // Check if we've allocated a stack slot for this value already
302 //
Chris Lattner9a3478e2003-12-20 09:17:07 +0000303 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
304 if (pair != offsets.end())
305 return pair->second;
306
307 unsigned getPaddedSize;
308 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
309 offsets[val] = offset;
310 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000311 return offset;
312}
Vikram S. Advee5671792002-09-16 15:18:16 +0000313
Chris Lattnereda6bd72002-02-03 07:54:50 +0000314int
Chris Lattner32525642002-12-28 20:37:16 +0000315MachineFunctionInfo::allocateSpilledValue(const Type* type)
Chris Lattnereda6bd72002-02-03 07:54:50 +0000316{
Vikram S. Adve70927722002-04-25 04:30:43 +0000317 assert(! spillsAreaFrozen &&
318 "Size of reg spills area has been used to compute an offset so "
319 "no more register spill slots should be allocated!");
320
Chris Lattner32525642002-12-28 20:37:16 +0000321 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
322 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000323
324 bool growUp;
Chris Lattner32525642002-12-28 20:37:16 +0000325 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000326
Vikram S. Advee5671792002-09-16 15:18:16 +0000327 int offset = growUp? firstOffset + getRegSpillsSize()
328 : firstOffset - (getRegSpillsSize() + size);
329
Chris Lattner32525642002-12-28 20:37:16 +0000330 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
Vikram S. Advee5671792002-09-16 15:18:16 +0000331 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnereda6bd72002-02-03 07:54:50 +0000332
Vikram S. Advee5671792002-09-16 15:18:16 +0000333 incrementRegSpillsSize(size); // update size of reg. spills area
334
335 return aligned;
Chris Lattnereda6bd72002-02-03 07:54:50 +0000336}
337
338int
Chris Lattner32525642002-12-28 20:37:16 +0000339MachineFunctionInfo::pushTempValue(unsigned size)
Chris Lattnereda6bd72002-02-03 07:54:50 +0000340{
Chris Lattner32525642002-12-28 20:37:16 +0000341 unsigned align = SizeToAlignment(size, MF.getTarget());
Vikram S. Advee5671792002-09-16 15:18:16 +0000342
Chris Lattnereda6bd72002-02-03 07:54:50 +0000343 bool growUp;
Chris Lattner32525642002-12-28 20:37:16 +0000344 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
Vikram S. Advee5671792002-09-16 15:18:16 +0000345
346 int offset = growUp? firstOffset + currentTmpValuesSize
347 : firstOffset - (currentTmpValuesSize + size);
348
Chris Lattner32525642002-12-28 20:37:16 +0000349 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
350 align);
Vikram S. Advee5671792002-09-16 15:18:16 +0000351 size += abs(aligned - offset); // include alignment padding in size
352
353 incrementTmpAreaSize(size); // update "current" size of tmp area
354
355 return aligned;
Chris Lattnereda6bd72002-02-03 07:54:50 +0000356}
357
Chris Lattner32525642002-12-28 20:37:16 +0000358void MachineFunctionInfo::popAllTempValues() {
Vikram S. Advee5671792002-09-16 15:18:16 +0000359 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnereda6bd72002-02-03 07:54:50 +0000360}
361