blob: a9501259d24bbf2b8274c1f333b5ae5bd41a2bef [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"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000025#include "llvm/Target/TargetLowering.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000026#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000027#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000028#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000029#include "llvm/Instructions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000031#include "llvm/Support/GraphWriter.h"
32#include "llvm/Support/LeakDetector.h"
33#include "llvm/ADT/STLExtras.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000034#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000035#include <fstream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000036#include <sstream>
Chris Lattner07f32d42003-12-20 09:17:07 +000037using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000038
Chris Lattnere316efc2002-10-29 23:18:43 +000039static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000040 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000041
Chris Lattnerefb9b812006-07-14 23:08:47 +000042// Out of line virtual function to home classes.
43void MachineFunctionPass::virtfn() {}
Chris Lattner227c3d32002-10-28 01:12:41 +000044
Chris Lattner227c3d32002-10-28 01:12:41 +000045namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000046 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000047 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000048
Brian Gaeke09caa372004-01-30 21:53:46 +000049 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000050 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000051
Devang Patel794fd752007-05-01 21:15:47 +000052 Printer (std::ostream *_OS, const std::string &_Banner)
53 : MachineFunctionPass((intptr_t)&ID), OS (_OS), Banner (_Banner) { }
Brian Gaeke09caa372004-01-30 21:53:46 +000054
Chris Lattner10491642002-10-30 00:48:05 +000055 const char *getPassName() const { return "MachineFunction Printer"; }
56
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesAll();
59 }
60
Chris Lattner16c45e92003-12-20 10:20:58 +000061 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000062 (*OS) << Banner;
63 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000064 return false;
65 }
66 };
Devang Patel19974732007-05-03 01:11:54 +000067 char Printer::ID = 0;
Chris Lattner227c3d32002-10-28 01:12:41 +000068}
69
Brian Gaeke09caa372004-01-30 21:53:46 +000070/// Returns a newly-created MachineFunction Printer pass. The default output
71/// stream is std::cerr; the default banner is empty.
72///
73FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000074 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000075 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000076}
77
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000078namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000079 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000080 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000081 Deleter() : MachineFunctionPass((intptr_t)&ID) {}
82
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000083 const char *getPassName() const { return "Machine Code Deleter"; }
84
85 bool runOnMachineFunction(MachineFunction &MF) {
86 // Delete the annotation from the function now.
87 MachineFunction::destruct(MF.getFunction());
88 return true;
89 }
90 };
Devang Patel19974732007-05-03 01:11:54 +000091 char Deleter::ID = 0;
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000092}
93
94/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
95/// the current function, which should happen after the function has been
96/// emitted to a .s file or to memory.
97FunctionPass *llvm::createMachineCodeDeleter() {
98 return new Deleter();
99}
100
101
102
Chris Lattner227c3d32002-10-28 01:12:41 +0000103//===---------------------------------------------------------------------===//
104// MachineFunction implementation
105//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +0000106
Chris Lattnerbca81442005-01-30 00:09:23 +0000107MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000108 MachineBasicBlock* dummy = new MachineBasicBlock();
109 LeakDetector::removeGarbageObject(dummy);
110 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +0000111}
112
113void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Chris Lattnerf20c1a42007-12-31 04:56:33 +0000114 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
115 ilist_iterator<MachineBasicBlock> first,
116 ilist_iterator<MachineBasicBlock> last) {
117 // If splicing withing the same function, no change.
118 if (Parent == toList.Parent) return;
119
120 for (; first != last; ++first)
121 first->setParent(toList.Parent);
Tanya Lattner792699c2004-05-24 06:11:51 +0000122}
Chris Lattner227c3d32002-10-28 01:12:41 +0000123
Chris Lattner10491642002-10-30 00:48:05 +0000124MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000125 const TargetMachine &TM)
Evan Cheng505e5512007-04-25 22:10:09 +0000126 : Annotation(MF_AID), Fn(F), Target(TM) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000127 RegInfo = new MachineRegisterInfo(*TM.getRegisterInfo());
Chris Lattnerad828162004-08-16 22:36:34 +0000128 MFInfo = 0;
Chris Lattner1612faa2008-01-25 07:19:06 +0000129 FrameInfo = new MachineFrameInfo(*TM.getFrameInfo());
Chris Lattner3029f922006-02-09 04:46:04 +0000130 ConstantPool = new MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000131
132 // Set up jump table.
133 const TargetData &TD = *TM.getTargetData();
134 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
135 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000136 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000137 : TD.getPointerABIAlignment();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000138 JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
139
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000140 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000141}
142
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000143MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000144 BasicBlocks.clear();
Chris Lattner84bc5422007-12-31 04:13:23 +0000145 delete RegInfo;
Chris Lattner955fad12002-12-28 20:37:16 +0000146 delete MFInfo;
147 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000148 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000149 delete JumpTableInfo;
Chris Lattner10491642002-10-30 00:48:05 +0000150}
151
Chris Lattnere70cab02006-10-03 19:18:57 +0000152
153/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
154/// recomputes them. This guarantees that the MBB numbers are sequential,
155/// dense, and match the ordering of the blocks within the function. If a
156/// specific MachineBasicBlock is specified, only that block and those after
157/// it are renumbered.
158void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
159 if (empty()) { MBBNumbering.clear(); return; }
160 MachineFunction::iterator MBBI, E = end();
161 if (MBB == 0)
162 MBBI = begin();
163 else
164 MBBI = MBB;
165
166 // Figure out the block number this should have.
167 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000168 if (MBBI != begin())
169 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000170
171 for (; MBBI != E; ++MBBI, ++BlockNo) {
172 if (MBBI->getNumber() != (int)BlockNo) {
173 // Remove use of the old number.
174 if (MBBI->getNumber() != -1) {
175 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
176 "MBB number mismatch!");
177 MBBNumbering[MBBI->getNumber()] = 0;
178 }
179
180 // If BlockNo is already taken, set that block's number to -1.
181 if (MBBNumbering[BlockNo])
182 MBBNumbering[BlockNo]->setNumber(-1);
183
184 MBBNumbering[BlockNo] = MBBI;
185 MBBI->setNumber(BlockNo);
186 }
187 }
188
189 // Okay, all the blocks are renumbered. If we have compactified the block
190 // numbering, shrink MBBNumbering now.
191 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
192 MBBNumbering.resize(BlockNo);
193}
194
195
Bill Wendlingbcd24982006-12-07 20:28:15 +0000196void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner10491642002-10-30 00:48:05 +0000197
198void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000199 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000200
201 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000202 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000203
204 // Print JumpTable Information
205 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000206
207 // Print Constant Pool
208 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000209
Dan Gohman6f0d0242008-02-10 18:45:23 +0000210 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000211
Chris Lattner84bc5422007-12-31 04:13:23 +0000212 if (!RegInfo->livein_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000213 OS << "Live Ins:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000214 for (MachineRegisterInfo::livein_iterator
215 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000216 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000217 OS << " " << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000218 else
219 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000220
221 if (I->second)
222 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000223 }
224 OS << "\n";
225 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000226 if (!RegInfo->liveout_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000227 OS << "Live Outs:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000228 for (MachineRegisterInfo::liveout_iterator
229 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman6f0d0242008-02-10 18:45:23 +0000230 if (TRI)
Bill Wendlinge6d088a2008-02-26 21:47:57 +0000231 OS << " " << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000232 else
233 OS << " Reg #" << *I;
234 OS << "\n";
235 }
236
Brian Gaeke90421cd2004-02-13 04:39:55 +0000237 for (const_iterator BB = begin(); BB != end(); ++BB)
238 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000239
240 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000241}
242
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000243/// CFGOnly flag - This is used to control whether or not the CFG graph printer
244/// prints out the contents of basic blocks or not. This is acceptable because
245/// this code is only really used for debugging purposes.
246///
247static bool CFGOnly = false;
248
249namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000250 template<>
251 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
252 static std::string getGraphName(const MachineFunction *F) {
253 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000254 }
255
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000256 static std::string getNodeLabel(const MachineBasicBlock *Node,
257 const MachineFunction *Graph) {
258 if (CFGOnly && Node->getBasicBlock() &&
259 !Node->getBasicBlock()->getName().empty())
260 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000261
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000262 std::ostringstream Out;
263 if (CFGOnly) {
264 Out << Node->getNumber() << ':';
265 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000266 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000267
268 Node->print(Out);
269
270 std::string OutStr = Out.str();
271 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
272
273 // Process string output to make it nicer...
274 for (unsigned i = 0; i != OutStr.length(); ++i)
275 if (OutStr[i] == '\n') { // Left justify
276 OutStr[i] = '\\';
277 OutStr.insert(OutStr.begin()+i+1, 'l');
278 }
279 return OutStr;
280 }
281 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000282}
283
284void MachineFunction::viewCFG() const
285{
Jim Laskey851a22d2005-10-12 12:09:05 +0000286#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000287 ViewGraph(this, "mf" + getFunction()->getName());
288#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000289 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
290 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000291#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000292}
293
294void MachineFunction::viewCFGOnly() const
295{
296 CFGOnly = true;
297 viewCFG();
298 CFGOnly = false;
299}
300
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000301// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000302// the MachineCodeForFunction object for the given function.
303// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000304// get() -- Returns a handle to the object.
305// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000306// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000307//
Misha Brukmanfce11432002-10-28 00:28:31 +0000308MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000309MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000310{
Chris Lattnere316efc2002-10-29 23:18:43 +0000311 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000312 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000313 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
314 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000315 return *mcInfo;
316}
317
Chris Lattner16c45e92003-12-20 10:20:58 +0000318void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000319 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner409a3d02008-04-06 21:46:45 +0000320 assert(Deleted && "Machine code did not exist for function!");
321 Deleted = Deleted; // silence warning when no assertions.
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000322}
323
Chris Lattner335d5c32002-10-28 05:58:46 +0000324MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000325{
Chris Lattnere316efc2002-10-29 23:18:43 +0000326 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000327 assert(mc && "Call construct() method first to allocate the object");
328 return *mc;
329}
330
Chris Lattner955fad12002-12-28 20:37:16 +0000331//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000332// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000333//===----------------------------------------------------------------------===//
334
Chris Lattner1612faa2008-01-25 07:19:06 +0000335/// CreateFixedObject - Create a new object at a fixed location on the stack.
336/// All fixed objects should be created before other objects are created for
337/// efficiency. By default, fixed objects are immutable. This returns an
338/// index with a negative value.
339///
340int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
341 bool Immutable) {
342 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
343 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
344 return -++NumFixedObjects;
345}
346
347
Chris Lattner9085d8a2003-01-16 18:35:57 +0000348void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000349 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000350
Chris Lattner955fad12002-12-28 20:37:16 +0000351 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
352 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000353 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengd3653122008-02-27 03:04:06 +0000354 if (SO.Size == ~0ULL) {
355 OS << "dead\n";
356 continue;
357 }
Chris Lattner955fad12002-12-28 20:37:16 +0000358 if (SO.Size == 0)
359 OS << "variable sized";
360 else
Chris Lattner0db07092005-05-13 22:54:44 +0000361 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
362 OS << " alignment is " << SO.Alignment << " byte"
363 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000364
Chris Lattner955fad12002-12-28 20:37:16 +0000365 if (i < NumFixedObjects)
366 OS << " fixed";
367 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000368 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000369 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000370 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000371 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000372 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000373 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000374 OS << "]";
375 }
376 OS << "\n";
377 }
378
379 if (HasVarSizedObjects)
380 OS << " Stack frame contains variable sized objects\n";
381}
382
Chris Lattner9085d8a2003-01-16 18:35:57 +0000383void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000384 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000385}
Chris Lattner955fad12002-12-28 20:37:16 +0000386
387
388//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000389// MachineJumpTableInfo implementation
390//===----------------------------------------------------------------------===//
391
392/// getJumpTableIndex - Create a new jump table entry in the jump table info
393/// or return an existing one.
394///
395unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000396 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000397 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000398 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
399 if (JumpTables[i].MBBs == DestBBs)
400 return i;
401
402 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
403 return JumpTables.size()-1;
404}
405
406
407void MachineJumpTableInfo::print(std::ostream &OS) const {
408 // FIXME: this is lame, maybe we could print out the MBB numbers or something
409 // like {1, 2, 4, 5, 3, 0}
410 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
411 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
412 << " entries\n";
413 }
414}
415
Bill Wendlingbcd24982006-12-07 20:28:15 +0000416void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000417
418
419//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000420// MachineConstantPool implementation
421//===----------------------------------------------------------------------===//
422
Evan Cheng9abd7c32006-09-14 05:50:57 +0000423const Type *MachineConstantPoolEntry::getType() const {
424 if (isMachineConstantPoolEntry())
425 return Val.MachineCPVal->getType();
426 return Val.ConstVal->getType();
427}
428
Evan Chengd6594ae2006-09-12 21:00:35 +0000429MachineConstantPool::~MachineConstantPool() {
430 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
431 if (Constants[i].isMachineConstantPoolEntry())
432 delete Constants[i].Val.MachineCPVal;
433}
434
Chris Lattner3029f922006-02-09 04:46:04 +0000435/// getConstantPoolIndex - Create a new entry in the constant pool or return
436/// an existing one. User must specify an alignment in bytes for the object.
437///
438unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
439 unsigned Alignment) {
440 assert(Alignment && "Alignment must be specified!");
441 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
442
443 // Check to see if we already have this constant.
444 //
445 // FIXME, this could be made much more efficient for large constant pools.
446 unsigned AlignMask = (1 << Alignment)-1;
447 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000448 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000449 return i;
450
451 unsigned Offset = 0;
452 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000453 Offset = Constants.back().getOffset();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000454 Offset += TD->getABITypeSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000455 Offset = (Offset+AlignMask)&~AlignMask;
456 }
457
458 Constants.push_back(MachineConstantPoolEntry(C, Offset));
459 return Constants.size()-1;
460}
461
Evan Chengd6594ae2006-09-12 21:00:35 +0000462unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
463 unsigned Alignment) {
464 assert(Alignment && "Alignment must be specified!");
465 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
466
467 // Check to see if we already have this constant.
468 //
469 // FIXME, this could be made much more efficient for large constant pools.
470 unsigned AlignMask = (1 << Alignment)-1;
471 int Idx = V->getExistingMachineCPValue(this, Alignment);
472 if (Idx != -1)
473 return (unsigned)Idx;
474
475 unsigned Offset = 0;
476 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000477 Offset = Constants.back().getOffset();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000478 Offset += TD->getABITypeSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000479 Offset = (Offset+AlignMask)&~AlignMask;
480 }
481
482 Constants.push_back(MachineConstantPoolEntry(V, Offset));
483 return Constants.size()-1;
484}
485
Chris Lattner3029f922006-02-09 04:46:04 +0000486
Chris Lattner4d149cd2003-01-13 00:23:03 +0000487void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000488 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000489 OS << " <cp #" << i << "> is";
490 if (Constants[i].isMachineConstantPoolEntry())
491 Constants[i].Val.MachineCPVal->print(OS);
492 else
493 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000494 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000495 OS << "\n";
496 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000497}
498
Bill Wendlingbcd24982006-12-07 20:28:15 +0000499void MachineConstantPool::dump() const { print(*cerr.stream()); }