blob: c762ae567c8a928806f42b1321d47154f4efdfbe [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//
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.
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 Lattner16c45e92003-12-20 10:20:58 +000017#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000018#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000019#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000021#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000022#include "llvm/CodeGen/MachineJumpTableInfo.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(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000114 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
115 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000116 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000117 if (Parent != toList.Parent)
118 for (; first != last; ++first)
119 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000120}
Chris Lattner227c3d32002-10-28 01:12:41 +0000121
Chris Lattner10491642002-10-30 00:48:05 +0000122MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000123 const TargetMachine &TM)
Evan Cheng505e5512007-04-25 22:10:09 +0000124 : Annotation(MF_AID), Fn(F), Target(TM) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000125 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000126 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000127 FrameInfo = new MachineFrameInfo();
Chris Lattner3029f922006-02-09 04:46:04 +0000128 ConstantPool = new MachineConstantPool(TM.getTargetData());
Evan Cheng505e5512007-04-25 22:10:09 +0000129 UsedPhysRegs.resize(TM.getRegisterInfo()->getNumRegs());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000130
131 // Set up jump table.
132 const TargetData &TD = *TM.getTargetData();
133 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
134 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000135 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000136 : TD.getPointerABIAlignment();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000137 JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
138
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000139 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000140}
141
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000142MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000143 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000144 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000145 delete MFInfo;
146 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000147 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000148 delete JumpTableInfo;
Chris Lattner10491642002-10-30 00:48:05 +0000149}
150
Chris Lattnere70cab02006-10-03 19:18:57 +0000151
152/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
153/// recomputes them. This guarantees that the MBB numbers are sequential,
154/// dense, and match the ordering of the blocks within the function. If a
155/// specific MachineBasicBlock is specified, only that block and those after
156/// it are renumbered.
157void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
158 if (empty()) { MBBNumbering.clear(); return; }
159 MachineFunction::iterator MBBI, E = end();
160 if (MBB == 0)
161 MBBI = begin();
162 else
163 MBBI = MBB;
164
165 // Figure out the block number this should have.
166 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000167 if (MBBI != begin())
168 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000169
170 for (; MBBI != E; ++MBBI, ++BlockNo) {
171 if (MBBI->getNumber() != (int)BlockNo) {
172 // Remove use of the old number.
173 if (MBBI->getNumber() != -1) {
174 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
175 "MBB number mismatch!");
176 MBBNumbering[MBBI->getNumber()] = 0;
177 }
178
179 // If BlockNo is already taken, set that block's number to -1.
180 if (MBBNumbering[BlockNo])
181 MBBNumbering[BlockNo]->setNumber(-1);
182
183 MBBNumbering[BlockNo] = MBBI;
184 MBBI->setNumber(BlockNo);
185 }
186 }
187
188 // Okay, all the blocks are renumbered. If we have compactified the block
189 // numbering, shrink MBBNumbering now.
190 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
191 MBBNumbering.resize(BlockNo);
192}
193
194
Bill Wendlingbcd24982006-12-07 20:28:15 +0000195void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner10491642002-10-30 00:48:05 +0000196
197void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000198 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000199
200 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000201 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000202
203 // Print JumpTable Information
204 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000205
206 // Print Constant Pool
207 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000208
209 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
210
211 if (livein_begin() != livein_end()) {
212 OS << "Live Ins:";
213 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
214 if (MRI)
215 OS << " " << MRI->getName(I->first);
216 else
217 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000218
219 if (I->second)
220 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000221 }
222 OS << "\n";
223 }
224 if (liveout_begin() != liveout_end()) {
225 OS << "Live Outs:";
226 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
227 if (MRI)
228 OS << " " << MRI->getName(*I);
229 else
230 OS << " Reg #" << *I;
231 OS << "\n";
232 }
233
Brian Gaeke90421cd2004-02-13 04:39:55 +0000234 for (const_iterator BB = begin(); BB != end(); ++BB)
235 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000236
237 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000238}
239
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000240/// CFGOnly flag - This is used to control whether or not the CFG graph printer
241/// prints out the contents of basic blocks or not. This is acceptable because
242/// this code is only really used for debugging purposes.
243///
244static bool CFGOnly = false;
245
246namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000247 template<>
248 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
249 static std::string getGraphName(const MachineFunction *F) {
250 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000251 }
252
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000253 static std::string getNodeLabel(const MachineBasicBlock *Node,
254 const MachineFunction *Graph) {
255 if (CFGOnly && Node->getBasicBlock() &&
256 !Node->getBasicBlock()->getName().empty())
257 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000258
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000259 std::ostringstream Out;
260 if (CFGOnly) {
261 Out << Node->getNumber() << ':';
262 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000263 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000264
265 Node->print(Out);
266
267 std::string OutStr = Out.str();
268 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
269
270 // Process string output to make it nicer...
271 for (unsigned i = 0; i != OutStr.length(); ++i)
272 if (OutStr[i] == '\n') { // Left justify
273 OutStr[i] = '\\';
274 OutStr.insert(OutStr.begin()+i+1, 'l');
275 }
276 return OutStr;
277 }
278 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000279}
280
281void MachineFunction::viewCFG() const
282{
Jim Laskey851a22d2005-10-12 12:09:05 +0000283#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000284 ViewGraph(this, "mf" + getFunction()->getName());
285#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000286 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
287 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000288#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000289}
290
291void MachineFunction::viewCFGOnly() const
292{
293 CFGOnly = true;
294 viewCFG();
295 CFGOnly = false;
296}
297
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000298// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000299// the MachineCodeForFunction object for the given function.
300// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000301// get() -- Returns a handle to the object.
302// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000303// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000304//
Misha Brukmanfce11432002-10-28 00:28:31 +0000305MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000306MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000307{
Chris Lattnere316efc2002-10-29 23:18:43 +0000308 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000309 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000310 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
311 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000312 return *mcInfo;
313}
314
Chris Lattner16c45e92003-12-20 10:20:58 +0000315void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000316 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000317 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000318}
319
Chris Lattner335d5c32002-10-28 05:58:46 +0000320MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000321{
Chris Lattnere316efc2002-10-29 23:18:43 +0000322 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000323 assert(mc && "Call construct() method first to allocate the object");
324 return *mc;
325}
326
Chris Lattner831fdcf2002-12-25 05:03:22 +0000327void MachineFunction::clearSSARegMap() {
328 delete SSARegMapping;
329 SSARegMapping = 0;
330}
331
Chris Lattner955fad12002-12-28 20:37:16 +0000332//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000333// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000334//===----------------------------------------------------------------------===//
335
Chris Lattner9085d8a2003-01-16 18:35:57 +0000336void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000337 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000338
Chris Lattner955fad12002-12-28 20:37:16 +0000339 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
340 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000341 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000342 if (SO.Size == 0)
343 OS << "variable sized";
344 else
Chris Lattner0db07092005-05-13 22:54:44 +0000345 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
346 OS << " alignment is " << SO.Alignment << " byte"
347 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000348
Chris Lattner955fad12002-12-28 20:37:16 +0000349 if (i < NumFixedObjects)
350 OS << " fixed";
351 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000352 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000353 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000354 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000355 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000356 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000357 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000358 OS << "]";
359 }
360 OS << "\n";
361 }
362
363 if (HasVarSizedObjects)
364 OS << " Stack frame contains variable sized objects\n";
365}
366
Chris Lattner9085d8a2003-01-16 18:35:57 +0000367void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000368 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000369}
Chris Lattner955fad12002-12-28 20:37:16 +0000370
371
372//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000373// MachineJumpTableInfo implementation
374//===----------------------------------------------------------------------===//
375
376/// getJumpTableIndex - Create a new jump table entry in the jump table info
377/// or return an existing one.
378///
379unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000380 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000381 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000382 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
383 if (JumpTables[i].MBBs == DestBBs)
384 return i;
385
386 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
387 return JumpTables.size()-1;
388}
389
390
391void MachineJumpTableInfo::print(std::ostream &OS) const {
392 // FIXME: this is lame, maybe we could print out the MBB numbers or something
393 // like {1, 2, 4, 5, 3, 0}
394 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
395 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
396 << " entries\n";
397 }
398}
399
Bill Wendlingbcd24982006-12-07 20:28:15 +0000400void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000401
402
403//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000404// MachineConstantPool implementation
405//===----------------------------------------------------------------------===//
406
Evan Cheng9abd7c32006-09-14 05:50:57 +0000407const Type *MachineConstantPoolEntry::getType() const {
408 if (isMachineConstantPoolEntry())
409 return Val.MachineCPVal->getType();
410 return Val.ConstVal->getType();
411}
412
Evan Chengd6594ae2006-09-12 21:00:35 +0000413MachineConstantPool::~MachineConstantPool() {
414 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
415 if (Constants[i].isMachineConstantPoolEntry())
416 delete Constants[i].Val.MachineCPVal;
417}
418
Chris Lattner3029f922006-02-09 04:46:04 +0000419/// getConstantPoolIndex - Create a new entry in the constant pool or return
420/// an existing one. User must specify an alignment in bytes for the object.
421///
422unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
423 unsigned Alignment) {
424 assert(Alignment && "Alignment must be specified!");
425 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
426
427 // Check to see if we already have this constant.
428 //
429 // FIXME, this could be made much more efficient for large constant pools.
430 unsigned AlignMask = (1 << Alignment)-1;
431 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000432 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000433 return i;
434
435 unsigned Offset = 0;
436 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000437 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000438 Offset += TD->getTypeSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000439 Offset = (Offset+AlignMask)&~AlignMask;
440 }
441
442 Constants.push_back(MachineConstantPoolEntry(C, Offset));
443 return Constants.size()-1;
444}
445
Evan Chengd6594ae2006-09-12 21:00:35 +0000446unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
447 unsigned Alignment) {
448 assert(Alignment && "Alignment must be specified!");
449 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
450
451 // Check to see if we already have this constant.
452 //
453 // FIXME, this could be made much more efficient for large constant pools.
454 unsigned AlignMask = (1 << Alignment)-1;
455 int Idx = V->getExistingMachineCPValue(this, Alignment);
456 if (Idx != -1)
457 return (unsigned)Idx;
458
459 unsigned Offset = 0;
460 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000461 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000462 Offset += TD->getTypeSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000463 Offset = (Offset+AlignMask)&~AlignMask;
464 }
465
466 Constants.push_back(MachineConstantPoolEntry(V, Offset));
467 return Constants.size()-1;
468}
469
Chris Lattner3029f922006-02-09 04:46:04 +0000470
Chris Lattner4d149cd2003-01-13 00:23:03 +0000471void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000472 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000473 OS << " <cp #" << i << "> is";
474 if (Constants[i].isMachineConstantPoolEntry())
475 Constants[i].Val.MachineCPVal->print(OS);
476 else
477 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000478 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000479 OS << "\n";
480 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000481}
482
Bill Wendlingbcd24982006-12-07 20:28:15 +0000483void MachineConstantPool::dump() const { print(*cerr.stream()); }