blob: 4de55f4596e9dfc436b711fee273a6056b9c6c87 [file] [log] [blame]
Chris Lattner6b944532002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Chris Lattnerf2868ce2002-02-03 07:54:50 +00002//
John Criswellb576c942003-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 Lattner6b944532002-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 Lattnerf2868ce2002-02-03 07:54:50 +000015
Chris Lattner16c45e92003-12-20 10:20:58 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner227c3d32002-10-28 01:12:41 +000018#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner955fad12002-12-28 20:37:16 +000020#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000022#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000023#include "llvm/CodeGen/Passes.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000025#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf27eeea2002-12-29 02:50:35 +000026#include "llvm/Target/TargetCacheInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027#include "llvm/Function.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000028#include "llvm/iOther.h"
Chris Lattner07f32d42003-12-20 09:17:07 +000029using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000030
Chris Lattnere316efc2002-10-29 23:18:43 +000031static AnnotationID MF_AID(
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000032 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000033
Chris Lattner227c3d32002-10-28 01:12:41 +000034
Chris Lattner227c3d32002-10-28 01:12:41 +000035namespace {
Chris Lattner16c45e92003-12-20 10:20:58 +000036 struct Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000037 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000038 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000039
40 Printer (std::ostream *_OS, const std::string &_Banner) :
41 OS (_OS), Banner (_Banner) { }
42
Chris Lattner10491642002-10-30 00:48:05 +000043 const char *getPassName() const { return "MachineFunction Printer"; }
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 }
48
Chris Lattner16c45e92003-12-20 10:20:58 +000049 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000050 (*OS) << Banner;
51 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000052 return false;
53 }
54 };
Chris Lattner227c3d32002-10-28 01:12:41 +000055}
56
Brian Gaeke09caa372004-01-30 21:53:46 +000057/// Returns a newly-created MachineFunction Printer pass. The default output
58/// stream is std::cerr; the default banner is empty.
59///
60FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
61 const std::string &Banner) {
62 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000063}
64
Chris Lattner227c3d32002-10-28 01:12:41 +000065//===---------------------------------------------------------------------===//
66// MachineFunction implementation
67//===---------------------------------------------------------------------===//
68
Chris Lattner10491642002-10-30 00:48:05 +000069MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +000070 const TargetMachine &TM)
71 : Annotation(MF_AID), Fn(F), Target(TM) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +000072 SSARegMapping = new SSARegMap();
Chris Lattner955fad12002-12-28 20:37:16 +000073 MFInfo = new MachineFunctionInfo(*this);
Chris Lattnereb24db92002-12-28 21:08:26 +000074 FrameInfo = new MachineFrameInfo();
Chris Lattner4d149cd2003-01-13 00:23:03 +000075 ConstantPool = new MachineConstantPool();
Chris Lattner831fdcf2002-12-25 05:03:22 +000076}
77
78MachineFunction::~MachineFunction() {
79 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +000080 delete MFInfo;
81 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +000082 delete ConstantPool;
Chris Lattner10491642002-10-30 00:48:05 +000083}
84
85void MachineFunction::dump() const { print(std::cerr); }
86
87void MachineFunction::print(std::ostream &OS) const {
Chris Lattner955fad12002-12-28 20:37:16 +000088 OS << "\n" << *(Value*)Fn->getFunctionType() << " \"" << Fn->getName()
89 << "\"\n";
90
91 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +000092 getFrameInfo()->print(*this, OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +000093
94 // Print Constant Pool
95 getConstantPool()->print(OS);
Chris Lattner10491642002-10-30 00:48:05 +000096
97 for (const_iterator BB = begin(); BB != end(); ++BB) {
Chris Lattner9e2dd8f2003-07-26 23:24:56 +000098 const BasicBlock *LBB = BB->getBasicBlock();
Chris Lattner2109f502002-12-15 20:35:25 +000099 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
Chris Lattner10491642002-10-30 00:48:05 +0000100 for (MachineBasicBlock::const_iterator I = BB->begin(); I != BB->end();++I){
101 OS << "\t";
Alkis Evlogimenosc0b9dc52004-02-12 02:27:10 +0000102 I->print(OS, Target);
Chris Lattner10491642002-10-30 00:48:05 +0000103 }
104 }
105 OS << "\nEnd function \"" << Fn->getName() << "\"\n\n";
106}
107
108
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000109// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000110// the MachineCodeForFunction object for the given function.
111// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000112// get() -- Returns a handle to the object.
113// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000114// for a given Function.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000115//
Misha Brukmanfce11432002-10-28 00:28:31 +0000116MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000117MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000118{
Chris Lattnere316efc2002-10-29 23:18:43 +0000119 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000120 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000121 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
122 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000123 return *mcInfo;
124}
125
Chris Lattner16c45e92003-12-20 10:20:58 +0000126void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000127 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000128 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000129}
130
Chris Lattner335d5c32002-10-28 05:58:46 +0000131MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000132{
Chris Lattnere316efc2002-10-29 23:18:43 +0000133 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000134 assert(mc && "Call construct() method first to allocate the object");
135 return *mc;
136}
137
Chris Lattner831fdcf2002-12-25 05:03:22 +0000138void MachineFunction::clearSSARegMap() {
139 delete SSARegMapping;
140 SSARegMapping = 0;
141}
142
Chris Lattner955fad12002-12-28 20:37:16 +0000143//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000144// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000145//===----------------------------------------------------------------------===//
146
Chris Lattner4d149cd2003-01-13 00:23:03 +0000147/// CreateStackObject - Create a stack object for a value of the specified type.
148///
149int MachineFrameInfo::CreateStackObject(const Type *Ty, const TargetData &TD) {
150 return CreateStackObject(TD.getTypeSize(Ty), TD.getTypeAlignment(Ty));
151}
152
153int MachineFrameInfo::CreateStackObject(const TargetRegisterClass *RC) {
154 return CreateStackObject(RC->getSize(), RC->getAlignment());
155}
156
157
Chris Lattner9085d8a2003-01-16 18:35:57 +0000158void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
159 int ValOffset = MF.getTarget().getFrameInfo().getOffsetOfLocalArea();
160
Chris Lattner955fad12002-12-28 20:37:16 +0000161 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
162 const StackObject &SO = Objects[i];
Chris Lattner4d149cd2003-01-13 00:23:03 +0000163 OS << " <fi #" << (int)(i-NumFixedObjects) << "> is ";
Chris Lattner955fad12002-12-28 20:37:16 +0000164 if (SO.Size == 0)
165 OS << "variable sized";
166 else
167 OS << SO.Size << " byte" << (SO.Size != 1 ? "s" : " ");
168
169 if (i < NumFixedObjects)
170 OS << " fixed";
171 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner9085d8a2003-01-16 18:35:57 +0000172 int Off = SO.SPOffset + ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000173 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000174 if (Off > 0)
175 OS << "+" << Off;
176 else if (Off < 0)
177 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000178 OS << "]";
179 }
180 OS << "\n";
181 }
182
183 if (HasVarSizedObjects)
184 OS << " Stack frame contains variable sized objects\n";
185}
186
Chris Lattner9085d8a2003-01-16 18:35:57 +0000187void MachineFrameInfo::dump(const MachineFunction &MF) const {
188 print(MF, std::cerr);
189}
Chris Lattner955fad12002-12-28 20:37:16 +0000190
191
192//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000193// MachineConstantPool implementation
194//===----------------------------------------------------------------------===//
195
196void MachineConstantPool::print(std::ostream &OS) const {
197 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
198 OS << " <cp #" << i << "> is" << *(Value*)Constants[i] << "\n";
199}
200
201void MachineConstantPool::dump() const { print(std::cerr); }
202
203//===----------------------------------------------------------------------===//
Chris Lattner955fad12002-12-28 20:37:16 +0000204// MachineFunctionInfo implementation
205//===----------------------------------------------------------------------===//
Chris Lattner831fdcf2002-12-25 05:03:22 +0000206
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000207static unsigned
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000208ComputeMaxOptionalArgsSize(const TargetMachine& target, const Function *F,
209 unsigned &maxOptionalNumArgs)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000210{
Chris Lattner955fad12002-12-28 20:37:16 +0000211 const TargetFrameInfo &frameInfo = target.getFrameInfo();
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000212
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000213 unsigned maxSize = 0;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000214
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000215 for (Function::const_iterator BB = F->begin(), BBE = F->end(); BB !=BBE; ++BB)
216 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Chris Lattner2ee82e02003-04-23 16:36:11 +0000217 if (const CallInst *callInst = dyn_cast<CallInst>(I))
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000218 {
219 unsigned numOperands = callInst->getNumOperands() - 1;
220 int numExtra = (int)numOperands-frameInfo.getNumFixedOutgoingArgs();
221 if (numExtra <= 0)
222 continue;
223
Chris Lattner955fad12002-12-28 20:37:16 +0000224 unsigned sizeForThisCall;
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000225 if (frameInfo.argsOnStackHaveFixedSize())
226 {
227 int argSize = frameInfo.getSizeOfEachArgOnStack();
228 sizeForThisCall = numExtra * (unsigned) argSize;
229 }
230 else
231 {
232 assert(0 && "UNTESTED CODE: Size per stack argument is not "
233 "fixed on this architecture: use actual arg sizes to "
234 "compute MaxOptionalArgsSize");
235 sizeForThisCall = 0;
236 for (unsigned i = 0; i < numOperands; ++i)
Chris Lattner955fad12002-12-28 20:37:16 +0000237 sizeForThisCall += target.getTargetData().getTypeSize(callInst->
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000238 getOperand(i)->getType());
239 }
240
241 if (maxSize < sizeForThisCall)
242 maxSize = sizeForThisCall;
243
244 if ((int)maxOptionalNumArgs < numExtra)
245 maxOptionalNumArgs = (unsigned) numExtra;
246 }
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000247
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000248 return maxSize;
249}
250
251// Align data larger than one L1 cache line on L1 cache line boundaries.
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000252// Align all smaller data on the next higher 2^x boundary (4, 8, ...),
253// but not higher than the alignment of the largest type we support
254// (currently a double word). -- see class TargetData).
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000255//
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000256// This function is similar to the corresponding function in EmitAssembly.cpp
257// but they are unrelated. This one does not align at more than a
258// double-word boundary whereas that one might.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000259//
Chris Lattner955fad12002-12-28 20:37:16 +0000260inline unsigned
261SizeToAlignment(unsigned size, const TargetMachine& target)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000262{
263 unsigned short cacheLineSize = target.getCacheInfo().getCacheLineSize(1);
264 if (size > (unsigned) cacheLineSize / 2)
265 return cacheLineSize;
266 else
267 for (unsigned sz=1; /*no condition*/; sz *= 2)
Chris Lattner955fad12002-12-28 20:37:16 +0000268 if (sz >= size || sz >= target.getTargetData().getDoubleAlignment())
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000269 return sz;
270}
271
272
Chris Lattner955fad12002-12-28 20:37:16 +0000273void MachineFunctionInfo::CalculateArgSize() {
274 maxOptionalArgsSize = ComputeMaxOptionalArgsSize(MF.getTarget(),
275 MF.getFunction(),
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000276 maxOptionalNumArgs);
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000277 staticStackSize = maxOptionalArgsSize
Chris Lattner955fad12002-12-28 20:37:16 +0000278 + MF.getTarget().getFrameInfo().getMinStackFrameSize();
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000279}
280
281int
Chris Lattner955fad12002-12-28 20:37:16 +0000282MachineFunctionInfo::computeOffsetforLocalVar(const Value* val,
283 unsigned &getPaddedSize,
284 unsigned sizeToUse)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000285{
Vikram S. Advee4e4d4e2002-03-24 03:39:26 +0000286 if (sizeToUse == 0)
Chris Lattner955fad12002-12-28 20:37:16 +0000287 sizeToUse = MF.getTarget().findOptimalStorageSize(val->getType());
288 unsigned align = SizeToAlignment(sizeToUse, MF.getTarget());
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000289
290 bool growUp;
Chris Lattner955fad12002-12-28 20:37:16 +0000291 int firstOffset = MF.getTarget().getFrameInfo().getFirstAutomaticVarOffset(MF,
292 growUp);
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000293 int offset = growUp? firstOffset + getAutomaticVarsSize()
294 : firstOffset - (getAutomaticVarsSize() + sizeToUse);
295
Chris Lattner955fad12002-12-28 20:37:16 +0000296 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000297 getPaddedSize = sizeToUse + abs(aligned - offset);
298
299 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000300}
301
Chris Lattner07f32d42003-12-20 09:17:07 +0000302
303int MachineFunctionInfo::allocateLocalVar(const Value* val,
304 unsigned sizeToUse) {
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000305 assert(! automaticVarsAreaFrozen &&
306 "Size of auto vars area has been used to compute an offset so "
307 "no more automatic vars should be allocated!");
308
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000309 // Check if we've allocated a stack slot for this value already
310 //
Chris Lattner07f32d42003-12-20 09:17:07 +0000311 hash_map<const Value*, int>::const_iterator pair = offsets.find(val);
312 if (pair != offsets.end())
313 return pair->second;
314
315 unsigned getPaddedSize;
316 unsigned offset = computeOffsetforLocalVar(val, getPaddedSize, sizeToUse);
317 offsets[val] = offset;
318 incrementAutomaticVarsSize(getPaddedSize);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000319 return offset;
320}
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000321
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000322int
Chris Lattner955fad12002-12-28 20:37:16 +0000323MachineFunctionInfo::allocateSpilledValue(const Type* type)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000324{
Vikram S. Adve03d33bd2002-04-25 04:30:43 +0000325 assert(! spillsAreaFrozen &&
326 "Size of reg spills area has been used to compute an offset so "
327 "no more register spill slots should be allocated!");
328
Chris Lattner955fad12002-12-28 20:37:16 +0000329 unsigned size = MF.getTarget().getTargetData().getTypeSize(type);
330 unsigned char align = MF.getTarget().getTargetData().getTypeAlignment(type);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000331
332 bool growUp;
Chris Lattner955fad12002-12-28 20:37:16 +0000333 int firstOffset = MF.getTarget().getFrameInfo().getRegSpillAreaOffset(MF, growUp);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000334
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000335 int offset = growUp? firstOffset + getRegSpillsSize()
336 : firstOffset - (getRegSpillsSize() + size);
337
Chris Lattner955fad12002-12-28 20:37:16 +0000338 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp, align);
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000339 size += abs(aligned - offset); // include alignment padding in size
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000340
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000341 incrementRegSpillsSize(size); // update size of reg. spills area
342
343 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000344}
345
346int
Chris Lattner955fad12002-12-28 20:37:16 +0000347MachineFunctionInfo::pushTempValue(unsigned size)
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000348{
Chris Lattner955fad12002-12-28 20:37:16 +0000349 unsigned align = SizeToAlignment(size, MF.getTarget());
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000350
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000351 bool growUp;
Chris Lattner955fad12002-12-28 20:37:16 +0000352 int firstOffset = MF.getTarget().getFrameInfo().getTmpAreaOffset(MF, growUp);
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000353
354 int offset = growUp? firstOffset + currentTmpValuesSize
355 : firstOffset - (currentTmpValuesSize + size);
356
Chris Lattner955fad12002-12-28 20:37:16 +0000357 int aligned = MF.getTarget().getFrameInfo().adjustAlignment(offset, growUp,
358 align);
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000359 size += abs(aligned - offset); // include alignment padding in size
360
361 incrementTmpAreaSize(size); // update "current" size of tmp area
362
363 return aligned;
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000364}
365
Chris Lattner955fad12002-12-28 20:37:16 +0000366void MachineFunctionInfo::popAllTempValues() {
Vikram S. Adve1318bed2002-09-16 15:18:16 +0000367 resetTmpAreaSize(); // clear tmp area to reuse
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000368}
369