blob: 595159b15f2d8a0f1b5f89bdf5f7d489723d176c [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(
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) {
Chris Lattner84bc5422007-12-31 04:13:23 +0000125 RegInfo = new MachineRegisterInfo(*TM.getRegisterInfo());
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());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000129
130 // Set up jump table.
131 const TargetData &TD = *TM.getTargetData();
132 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
133 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000134 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000135 : TD.getPointerABIAlignment();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000136 JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
137
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000138 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000139}
140
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000141MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000142 BasicBlocks.clear();
Chris Lattner84bc5422007-12-31 04:13:23 +0000143 delete RegInfo;
Chris Lattner955fad12002-12-28 20:37:16 +0000144 delete MFInfo;
145 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000146 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000147 delete JumpTableInfo;
Chris Lattner10491642002-10-30 00:48:05 +0000148}
149
Chris Lattnere70cab02006-10-03 19:18:57 +0000150
151/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
152/// recomputes them. This guarantees that the MBB numbers are sequential,
153/// dense, and match the ordering of the blocks within the function. If a
154/// specific MachineBasicBlock is specified, only that block and those after
155/// it are renumbered.
156void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
157 if (empty()) { MBBNumbering.clear(); return; }
158 MachineFunction::iterator MBBI, E = end();
159 if (MBB == 0)
160 MBBI = begin();
161 else
162 MBBI = MBB;
163
164 // Figure out the block number this should have.
165 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000166 if (MBBI != begin())
167 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000168
169 for (; MBBI != E; ++MBBI, ++BlockNo) {
170 if (MBBI->getNumber() != (int)BlockNo) {
171 // Remove use of the old number.
172 if (MBBI->getNumber() != -1) {
173 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
174 "MBB number mismatch!");
175 MBBNumbering[MBBI->getNumber()] = 0;
176 }
177
178 // If BlockNo is already taken, set that block's number to -1.
179 if (MBBNumbering[BlockNo])
180 MBBNumbering[BlockNo]->setNumber(-1);
181
182 MBBNumbering[BlockNo] = MBBI;
183 MBBI->setNumber(BlockNo);
184 }
185 }
186
187 // Okay, all the blocks are renumbered. If we have compactified the block
188 // numbering, shrink MBBNumbering now.
189 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
190 MBBNumbering.resize(BlockNo);
191}
192
193
Bill Wendlingbcd24982006-12-07 20:28:15 +0000194void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner10491642002-10-30 00:48:05 +0000195
196void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000197 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000198
199 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000200 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000201
202 // Print JumpTable Information
203 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000204
205 // Print Constant Pool
206 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000207
208 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
209
Chris Lattner84bc5422007-12-31 04:13:23 +0000210 if (!RegInfo->livein_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000211 OS << "Live Ins:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000212 for (MachineRegisterInfo::livein_iterator
213 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000214 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 }
Chris Lattner84bc5422007-12-31 04:13:23 +0000224 if (!RegInfo->liveout_empty()) {
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000225 OS << "Live Outs:";
Chris Lattner84bc5422007-12-31 04:13:23 +0000226 for (MachineRegisterInfo::liveout_iterator
227 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000228 if (MRI)
229 OS << " " << MRI->getName(*I);
230 else
231 OS << " Reg #" << *I;
232 OS << "\n";
233 }
234
Brian Gaeke90421cd2004-02-13 04:39:55 +0000235 for (const_iterator BB = begin(); BB != end(); ++BB)
236 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000237
238 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000239}
240
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000241/// CFGOnly flag - This is used to control whether or not the CFG graph printer
242/// prints out the contents of basic blocks or not. This is acceptable because
243/// this code is only really used for debugging purposes.
244///
245static bool CFGOnly = false;
246
247namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000248 template<>
249 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
250 static std::string getGraphName(const MachineFunction *F) {
251 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000252 }
253
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000254 static std::string getNodeLabel(const MachineBasicBlock *Node,
255 const MachineFunction *Graph) {
256 if (CFGOnly && Node->getBasicBlock() &&
257 !Node->getBasicBlock()->getName().empty())
258 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000259
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000260 std::ostringstream Out;
261 if (CFGOnly) {
262 Out << Node->getNumber() << ':';
263 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000264 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000265
266 Node->print(Out);
267
268 std::string OutStr = Out.str();
269 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
270
271 // Process string output to make it nicer...
272 for (unsigned i = 0; i != OutStr.length(); ++i)
273 if (OutStr[i] == '\n') { // Left justify
274 OutStr[i] = '\\';
275 OutStr.insert(OutStr.begin()+i+1, 'l');
276 }
277 return OutStr;
278 }
279 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000280}
281
282void MachineFunction::viewCFG() const
283{
Jim Laskey851a22d2005-10-12 12:09:05 +0000284#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000285 ViewGraph(this, "mf" + getFunction()->getName());
286#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000287 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
288 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000289#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000290}
291
292void MachineFunction::viewCFGOnly() const
293{
294 CFGOnly = true;
295 viewCFG();
296 CFGOnly = false;
297}
298
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000299// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000300// the MachineCodeForFunction object for the given function.
301// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000302// get() -- Returns a handle to the object.
303// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000304// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000305//
Misha Brukmanfce11432002-10-28 00:28:31 +0000306MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000307MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000308{
Chris Lattnere316efc2002-10-29 23:18:43 +0000309 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000310 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000311 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
312 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000313 return *mcInfo;
314}
315
Chris Lattner16c45e92003-12-20 10:20:58 +0000316void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000317 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000318 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000319}
320
Chris Lattner335d5c32002-10-28 05:58:46 +0000321MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000322{
Chris Lattnere316efc2002-10-29 23:18:43 +0000323 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000324 assert(mc && "Call construct() method first to allocate the object");
325 return *mc;
326}
327
Chris Lattner955fad12002-12-28 20:37:16 +0000328//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000329// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000330//===----------------------------------------------------------------------===//
331
Chris Lattner9085d8a2003-01-16 18:35:57 +0000332void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000333 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000334
Chris Lattner955fad12002-12-28 20:37:16 +0000335 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
336 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000337 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000338 if (SO.Size == 0)
339 OS << "variable sized";
340 else
Chris Lattner0db07092005-05-13 22:54:44 +0000341 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
342 OS << " alignment is " << SO.Alignment << " byte"
343 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000344
Chris Lattner955fad12002-12-28 20:37:16 +0000345 if (i < NumFixedObjects)
346 OS << " fixed";
347 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000348 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000349 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000350 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000351 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000352 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000353 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000354 OS << "]";
355 }
356 OS << "\n";
357 }
358
359 if (HasVarSizedObjects)
360 OS << " Stack frame contains variable sized objects\n";
361}
362
Chris Lattner9085d8a2003-01-16 18:35:57 +0000363void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000364 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000365}
Chris Lattner955fad12002-12-28 20:37:16 +0000366
367
368//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000369// MachineJumpTableInfo implementation
370//===----------------------------------------------------------------------===//
371
372/// getJumpTableIndex - Create a new jump table entry in the jump table info
373/// or return an existing one.
374///
375unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000376 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000377 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000378 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
379 if (JumpTables[i].MBBs == DestBBs)
380 return i;
381
382 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
383 return JumpTables.size()-1;
384}
385
386
387void MachineJumpTableInfo::print(std::ostream &OS) const {
388 // FIXME: this is lame, maybe we could print out the MBB numbers or something
389 // like {1, 2, 4, 5, 3, 0}
390 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
391 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
392 << " entries\n";
393 }
394}
395
Bill Wendlingbcd24982006-12-07 20:28:15 +0000396void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000397
398
399//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000400// MachineConstantPool implementation
401//===----------------------------------------------------------------------===//
402
Evan Cheng9abd7c32006-09-14 05:50:57 +0000403const Type *MachineConstantPoolEntry::getType() const {
404 if (isMachineConstantPoolEntry())
405 return Val.MachineCPVal->getType();
406 return Val.ConstVal->getType();
407}
408
Evan Chengd6594ae2006-09-12 21:00:35 +0000409MachineConstantPool::~MachineConstantPool() {
410 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
411 if (Constants[i].isMachineConstantPoolEntry())
412 delete Constants[i].Val.MachineCPVal;
413}
414
Chris Lattner3029f922006-02-09 04:46:04 +0000415/// getConstantPoolIndex - Create a new entry in the constant pool or return
416/// an existing one. User must specify an alignment in bytes for the object.
417///
418unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
419 unsigned Alignment) {
420 assert(Alignment && "Alignment must be specified!");
421 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
422
423 // Check to see if we already have this constant.
424 //
425 // FIXME, this could be made much more efficient for large constant pools.
426 unsigned AlignMask = (1 << Alignment)-1;
427 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000428 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000429 return i;
430
431 unsigned Offset = 0;
432 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000433 Offset = Constants.back().getOffset();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000434 Offset += TD->getABITypeSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000435 Offset = (Offset+AlignMask)&~AlignMask;
436 }
437
438 Constants.push_back(MachineConstantPoolEntry(C, Offset));
439 return Constants.size()-1;
440}
441
Evan Chengd6594ae2006-09-12 21:00:35 +0000442unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
443 unsigned Alignment) {
444 assert(Alignment && "Alignment must be specified!");
445 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
446
447 // Check to see if we already have this constant.
448 //
449 // FIXME, this could be made much more efficient for large constant pools.
450 unsigned AlignMask = (1 << Alignment)-1;
451 int Idx = V->getExistingMachineCPValue(this, Alignment);
452 if (Idx != -1)
453 return (unsigned)Idx;
454
455 unsigned Offset = 0;
456 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000457 Offset = Constants.back().getOffset();
Duncan Sandsca0ed742007-11-05 00:04:43 +0000458 Offset += TD->getABITypeSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000459 Offset = (Offset+AlignMask)&~AlignMask;
460 }
461
462 Constants.push_back(MachineConstantPoolEntry(V, Offset));
463 return Constants.size()-1;
464}
465
Chris Lattner3029f922006-02-09 04:46:04 +0000466
Chris Lattner4d149cd2003-01-13 00:23:03 +0000467void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000468 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000469 OS << " <cp #" << i << "> is";
470 if (Constants[i].isMachineConstantPoolEntry())
471 Constants[i].Val.MachineCPVal->print(OS);
472 else
473 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000474 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000475 OS << "\n";
476 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000477}
478
Bill Wendlingbcd24982006-12-07 20:28:15 +0000479void MachineConstantPool::dump() const { print(*cerr.stream()); }