blob: b243297253fbae382751ef62ba05bb443cab9df4 [file] [log] [blame]
Chris Lattner6b944532002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00009//
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 Lattnerd2b7cec2007-02-14 05:52:17 +000016#include "llvm/DerivedTypes.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman37efe672006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000023#include "llvm/CodeGen/Passes.h"
Owen Anderson07000c62006-05-12 06:33:49 +000024#include "llvm/Target/TargetData.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000026#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000028#include "llvm/Instructions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000030#include "llvm/Support/GraphWriter.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000031#include "llvm/ADT/STLExtras.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000032#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000033#include <fstream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000034#include <sstream>
Chris Lattner07f32d42003-12-20 09:17:07 +000035using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000036
Chris Lattnere316efc2002-10-29 23:18:43 +000037static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000038 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000039
Chris Lattnerefb9b812006-07-14 23:08:47 +000040// Out of line virtual function to home classes.
41void MachineFunctionPass::virtfn() {}
Chris Lattner227c3d32002-10-28 01:12:41 +000042
Chris Lattner227c3d32002-10-28 01:12:41 +000043namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000044 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000045 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000046
Brian Gaeke09caa372004-01-30 21:53:46 +000047 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000048 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000049
Dan Gohman80f3d462008-07-21 19:48:15 +000050 Printer (std::ostream *os, const std::string &banner)
51 : MachineFunctionPass((intptr_t)&ID), OS(os), Banner(banner) {}
Brian Gaeke09caa372004-01-30 21:53:46 +000052
Chris Lattner10491642002-10-30 00:48:05 +000053 const char *getPassName() const { return "MachineFunction Printer"; }
54
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesAll();
57 }
58
Chris Lattner16c45e92003-12-20 10:20:58 +000059 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000060 (*OS) << Banner;
61 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000062 return false;
63 }
64 };
Devang Patel19974732007-05-03 01:11:54 +000065 char Printer::ID = 0;
Chris Lattner227c3d32002-10-28 01:12:41 +000066}
67
Brian Gaeke09caa372004-01-30 21:53:46 +000068/// Returns a newly-created MachineFunction Printer pass. The default output
69/// stream is std::cerr; the default banner is empty.
70///
71FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000072 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000073 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000074}
75
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000076namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000077 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000078 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000079 Deleter() : MachineFunctionPass((intptr_t)&ID) {}
80
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000081 const char *getPassName() const { return "Machine Code Deleter"; }
82
83 bool runOnMachineFunction(MachineFunction &MF) {
84 // Delete the annotation from the function now.
85 MachineFunction::destruct(MF.getFunction());
86 return true;
87 }
88 };
Devang Patel19974732007-05-03 01:11:54 +000089 char Deleter::ID = 0;
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000090}
91
92/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
93/// the current function, which should happen after the function has been
94/// emitted to a .s file or to memory.
95FunctionPass *llvm::createMachineCodeDeleter() {
96 return new Deleter();
97}
98
99
100
Chris Lattner227c3d32002-10-28 01:12:41 +0000101//===---------------------------------------------------------------------===//
102// MachineFunction implementation
103//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +0000104
Dan Gohmanfed90b62008-07-28 21:51:04 +0000105void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000106 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +0000107}
Chris Lattner227c3d32002-10-28 01:12:41 +0000108
Chris Lattner10491642002-10-30 00:48:05 +0000109MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000110 const TargetMachine &TM)
Evan Cheng505e5512007-04-25 22:10:09 +0000111 : Annotation(MF_AID), Fn(F), Target(TM) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000112 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
113 MachineRegisterInfo(*TM.getRegisterInfo());
Chris Lattnerad828162004-08-16 22:36:34 +0000114 MFInfo = 0;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000115 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
116 MachineFrameInfo(*TM.getFrameInfo());
117 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
118 MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000119
120 // Set up jump table.
121 const TargetData &TD = *TM.getTargetData();
122 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
123 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000124 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000125 : TD.getPointerABIAlignment();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000126 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
127 MachineJumpTableInfo(EntrySize, Alignment);
Chris Lattner831fdcf2002-12-25 05:03:22 +0000128}
129
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000130MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000131 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000132 InstructionRecycler.clear(Allocator);
133 BasicBlockRecycler.clear(Allocator);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000134 RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
135 if (MFInfo) {
136 MFInfo->~MachineFunctionInfo(); Allocator.Deallocate(MFInfo);
137 }
138 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
139 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
140 JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
Chris Lattner10491642002-10-30 00:48:05 +0000141}
142
Chris Lattnere70cab02006-10-03 19:18:57 +0000143
144/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
145/// recomputes them. This guarantees that the MBB numbers are sequential,
146/// dense, and match the ordering of the blocks within the function. If a
147/// specific MachineBasicBlock is specified, only that block and those after
148/// it are renumbered.
149void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
150 if (empty()) { MBBNumbering.clear(); return; }
151 MachineFunction::iterator MBBI, E = end();
152 if (MBB == 0)
153 MBBI = begin();
154 else
155 MBBI = MBB;
156
157 // Figure out the block number this should have.
158 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000159 if (MBBI != begin())
160 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000161
162 for (; MBBI != E; ++MBBI, ++BlockNo) {
163 if (MBBI->getNumber() != (int)BlockNo) {
164 // Remove use of the old number.
165 if (MBBI->getNumber() != -1) {
166 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
167 "MBB number mismatch!");
168 MBBNumbering[MBBI->getNumber()] = 0;
169 }
170
171 // If BlockNo is already taken, set that block's number to -1.
172 if (MBBNumbering[BlockNo])
173 MBBNumbering[BlockNo]->setNumber(-1);
174
175 MBBNumbering[BlockNo] = MBBI;
176 MBBI->setNumber(BlockNo);
177 }
178 }
179
180 // Okay, all the blocks are renumbered. If we have compactified the block
181 // numbering, shrink MBBNumbering now.
182 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
183 MBBNumbering.resize(BlockNo);
184}
185
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000186/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
187/// of `new MachineInstr'.
188///
189MachineInstr *
190MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, bool NoImp) {
191 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
192 MachineInstr(TID, NoImp);
193}
Chris Lattnere70cab02006-10-03 19:18:57 +0000194
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000195/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
196/// 'Orig' instruction, identical in all ways except the the instruction
197/// has no parent, prev, or next.
198///
199MachineInstr *
200MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
201 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
202 MachineInstr(*this, *Orig);
203}
204
205/// DeleteMachineInstr - Delete the given MachineInstr.
206///
207void
208MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
209 // Clear the instructions memoperands. This must be done manually because
210 // the instruction's parent pointer is now null, so it can't properly
211 // deallocate them on its own.
212 MI->clearMemOperands(*this);
213
214 MI->~MachineInstr();
215 InstructionRecycler.Deallocate(Allocator, MI);
216}
217
218/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
219/// instead of `new MachineBasicBlock'.
220///
221MachineBasicBlock *
222MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
223 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
224 MachineBasicBlock(*this, bb);
225}
226
227/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
228///
229void
230MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
231 assert(MBB->getParent() == this && "MBB parent mismatch!");
232 MBB->~MachineBasicBlock();
233 BasicBlockRecycler.Deallocate(Allocator, MBB);
234}
235
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000236void MachineFunction::dump() const {
237 print(*cerr.stream());
238}
Chris Lattner10491642002-10-30 00:48:05 +0000239
240void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000241 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000242
243 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000244 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000245
246 // Print JumpTable Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000247 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000248
249 // Print Constant Pool
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000250 ConstantPool->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000251
Dan Gohman6f0d0242008-02-10 18:45:23 +0000252 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000253
Chris Lattner84bc5422007-12-31 04:13:23 +0000254 if (!RegInfo->livein_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000255 OS << "Live Ins:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000256 for (MachineRegisterInfo::livein_iterator
257 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000258 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000259 OS << " " << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000260 else
261 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000262
263 if (I->second)
264 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000265 }
266 OS << "\n";
267 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000268 if (!RegInfo->liveout_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000269 OS << "Live Outs:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000270 for (MachineRegisterInfo::liveout_iterator
271 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman6f0d0242008-02-10 18:45:23 +0000272 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000273 OS << " " << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000274 else
275 OS << " Reg #" << *I;
276 OS << "\n";
277 }
278
Brian Gaeke90421cd2004-02-13 04:39:55 +0000279 for (const_iterator BB = begin(); BB != end(); ++BB)
280 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000281
282 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000283}
284
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000285/// CFGOnly flag - This is used to control whether or not the CFG graph printer
286/// prints out the contents of basic blocks or not. This is acceptable because
287/// this code is only really used for debugging purposes.
288///
289static bool CFGOnly = false;
290
291namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000292 template<>
293 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
294 static std::string getGraphName(const MachineFunction *F) {
295 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000296 }
297
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000298 static std::string getNodeLabel(const MachineBasicBlock *Node,
299 const MachineFunction *Graph) {
300 if (CFGOnly && Node->getBasicBlock() &&
301 !Node->getBasicBlock()->getName().empty())
302 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000303
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000304 std::ostringstream Out;
305 if (CFGOnly) {
306 Out << Node->getNumber() << ':';
307 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000308 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000309
310 Node->print(Out);
311
312 std::string OutStr = Out.str();
313 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
314
315 // Process string output to make it nicer...
316 for (unsigned i = 0; i != OutStr.length(); ++i)
317 if (OutStr[i] == '\n') { // Left justify
318 OutStr[i] = '\\';
319 OutStr.insert(OutStr.begin()+i+1, 'l');
320 }
321 return OutStr;
322 }
323 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000324}
325
326void MachineFunction::viewCFG() const
327{
Jim Laskey851a22d2005-10-12 12:09:05 +0000328#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000329 ViewGraph(this, "mf" + getFunction()->getName());
330#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000331 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
332 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000333#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000334}
335
336void MachineFunction::viewCFGOnly() const
337{
338 CFGOnly = true;
339 viewCFG();
340 CFGOnly = false;
341}
342
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000343// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000344// the MachineCodeForFunction object for the given function.
345// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000346// get() -- Returns a handle to the object.
347// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000348// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000349//
Misha Brukmanfce11432002-10-28 00:28:31 +0000350MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000351MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000352{
Chris Lattnere316efc2002-10-29 23:18:43 +0000353 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000354 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000355 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
356 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000357 return *mcInfo;
358}
359
Chris Lattner16c45e92003-12-20 10:20:58 +0000360void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000361 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner409a3d02008-04-06 21:46:45 +0000362 assert(Deleted && "Machine code did not exist for function!");
363 Deleted = Deleted; // silence warning when no assertions.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000364}
365
Chris Lattner335d5c32002-10-28 05:58:46 +0000366MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000367{
Chris Lattnere316efc2002-10-29 23:18:43 +0000368 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000369 assert(mc && "Call construct() method first to allocate the object");
370 return *mc;
371}
372
Chris Lattner955fad12002-12-28 20:37:16 +0000373//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000374// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000375//===----------------------------------------------------------------------===//
376
Chris Lattner1612faa2008-01-25 07:19:06 +0000377/// CreateFixedObject - Create a new object at a fixed location on the stack.
378/// All fixed objects should be created before other objects are created for
379/// efficiency. By default, fixed objects are immutable. This returns an
380/// index with a negative value.
381///
382int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
383 bool Immutable) {
384 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
385 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
386 return -++NumFixedObjects;
387}
388
389
Chris Lattner9085d8a2003-01-16 18:35:57 +0000390void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000391 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000392
Chris Lattner955fad12002-12-28 20:37:16 +0000393 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
394 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000395 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengd3653122008-02-27 03:04:06 +0000396 if (SO.Size == ~0ULL) {
397 OS << "dead\n";
398 continue;
399 }
Chris Lattner955fad12002-12-28 20:37:16 +0000400 if (SO.Size == 0)
401 OS << "variable sized";
402 else
Chris Lattner0db07092005-05-13 22:54:44 +0000403 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
404 OS << " alignment is " << SO.Alignment << " byte"
405 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000406
Chris Lattner955fad12002-12-28 20:37:16 +0000407 if (i < NumFixedObjects)
408 OS << " fixed";
409 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000410 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000411 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000412 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000413 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000414 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000415 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000416 OS << "]";
417 }
418 OS << "\n";
419 }
420
421 if (HasVarSizedObjects)
422 OS << " Stack frame contains variable sized objects\n";
423}
424
Chris Lattner9085d8a2003-01-16 18:35:57 +0000425void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000426 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000427}
Chris Lattner955fad12002-12-28 20:37:16 +0000428
429
430//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000431// MachineJumpTableInfo implementation
432//===----------------------------------------------------------------------===//
433
434/// getJumpTableIndex - Create a new jump table entry in the jump table info
435/// or return an existing one.
436///
437unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000438 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000439 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000440 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
441 if (JumpTables[i].MBBs == DestBBs)
442 return i;
443
444 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
445 return JumpTables.size()-1;
446}
447
448
449void MachineJumpTableInfo::print(std::ostream &OS) const {
450 // FIXME: this is lame, maybe we could print out the MBB numbers or something
451 // like {1, 2, 4, 5, 3, 0}
452 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
453 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
454 << " entries\n";
455 }
456}
457
Bill Wendlingbcd24982006-12-07 20:28:15 +0000458void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000459
460
461//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000462// MachineConstantPool implementation
463//===----------------------------------------------------------------------===//
464
Evan Cheng9abd7c32006-09-14 05:50:57 +0000465const Type *MachineConstantPoolEntry::getType() const {
466 if (isMachineConstantPoolEntry())
467 return Val.MachineCPVal->getType();
468 return Val.ConstVal->getType();
469}
470
Evan Chengd6594ae2006-09-12 21:00:35 +0000471MachineConstantPool::~MachineConstantPool() {
472 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
473 if (Constants[i].isMachineConstantPoolEntry())
474 delete Constants[i].Val.MachineCPVal;
475}
476
Chris Lattner3029f922006-02-09 04:46:04 +0000477/// getConstantPoolIndex - Create a new entry in the constant pool or return
478/// an existing one. User must specify an alignment in bytes for the object.
479///
480unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
481 unsigned Alignment) {
482 assert(Alignment && "Alignment must be specified!");
483 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
484
485 // Check to see if we already have this constant.
486 //
487 // FIXME, this could be made much more efficient for large constant pools.
488 unsigned AlignMask = (1 << Alignment)-1;
489 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000490 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000491 return i;
492
493 unsigned Offset = 0;
494 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000495 Offset = Constants.back().getOffset();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000496 Offset += TD->getABITypeSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000497 Offset = (Offset+AlignMask)&~AlignMask;
498 }
499
500 Constants.push_back(MachineConstantPoolEntry(C, Offset));
501 return Constants.size()-1;
502}
503
Evan Chengd6594ae2006-09-12 21:00:35 +0000504unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
505 unsigned Alignment) {
506 assert(Alignment && "Alignment must be specified!");
507 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
508
509 // Check to see if we already have this constant.
510 //
511 // FIXME, this could be made much more efficient for large constant pools.
512 unsigned AlignMask = (1 << Alignment)-1;
513 int Idx = V->getExistingMachineCPValue(this, Alignment);
514 if (Idx != -1)
515 return (unsigned)Idx;
516
517 unsigned Offset = 0;
518 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000519 Offset = Constants.back().getOffset();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000520 Offset += TD->getABITypeSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000521 Offset = (Offset+AlignMask)&~AlignMask;
522 }
523
524 Constants.push_back(MachineConstantPoolEntry(V, Offset));
525 return Constants.size()-1;
526}
527
Chris Lattner3029f922006-02-09 04:46:04 +0000528
Chris Lattner4d149cd2003-01-13 00:23:03 +0000529void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000530 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000531 OS << " <cp #" << i << "> is";
532 if (Constants[i].isMachineConstantPoolEntry())
533 Constants[i].Val.MachineCPVal->print(OS);
534 else
535 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000536 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000537 OS << "\n";
538 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000539}
540
Bill Wendlingbcd24982006-12-07 20:28:15 +0000541void MachineConstantPool::dump() const { print(*cerr.stream()); }