blob: 4453be80c1c0aa21d405e80734304a371604a0a3 [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 {
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
50 Printer (std::ostream *_OS, const std::string &_Banner) :
51 OS (_OS), Banner (_Banner) { }
52
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 };
Chris Lattner227c3d32002-10-28 01:12:41 +000065}
66
Brian Gaeke09caa372004-01-30 21:53:46 +000067/// Returns a newly-created MachineFunction Printer pass. The default output
68/// stream is std::cerr; the default banner is empty.
69///
70FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000071 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000072 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000073}
74
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000075namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000076 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000077 const char *getPassName() const { return "Machine Code Deleter"; }
78
79 bool runOnMachineFunction(MachineFunction &MF) {
80 // Delete the annotation from the function now.
81 MachineFunction::destruct(MF.getFunction());
82 return true;
83 }
84 };
85}
86
87/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
88/// the current function, which should happen after the function has been
89/// emitted to a .s file or to memory.
90FunctionPass *llvm::createMachineCodeDeleter() {
91 return new Deleter();
92}
93
94
95
Chris Lattner227c3d32002-10-28 01:12:41 +000096//===---------------------------------------------------------------------===//
97// MachineFunction implementation
98//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000099
Chris Lattnerbca81442005-01-30 00:09:23 +0000100MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000101 MachineBasicBlock* dummy = new MachineBasicBlock();
102 LeakDetector::removeGarbageObject(dummy);
103 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +0000104}
105
106void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000107 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
108 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000109 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000110 if (Parent != toList.Parent)
111 for (; first != last; ++first)
112 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000113}
Chris Lattner227c3d32002-10-28 01:12:41 +0000114
Chris Lattner10491642002-10-30 00:48:05 +0000115MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000116 const TargetMachine &TM)
Jim Laskeyf99c2322006-01-04 13:43:56 +0000117 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000118 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000119 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000120 FrameInfo = new MachineFrameInfo();
Chris Lattner3029f922006-02-09 04:46:04 +0000121 ConstantPool = new MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000122
123 // Set up jump table.
124 const TargetData &TD = *TM.getTargetData();
125 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
126 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattnerd2b7cec2007-02-14 05:52:17 +0000127 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
Chris Lattner58092e32007-01-20 22:35:55 +0000128 : TD.getPointerABIAlignment();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000129 JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
130
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000131 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000132}
133
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000134MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000135 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000136 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000137 delete MFInfo;
138 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000139 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000140 delete JumpTableInfo;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000141 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000142}
143
Chris Lattnere70cab02006-10-03 19:18:57 +0000144
145/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
146/// recomputes them. This guarantees that the MBB numbers are sequential,
147/// dense, and match the ordering of the blocks within the function. If a
148/// specific MachineBasicBlock is specified, only that block and those after
149/// it are renumbered.
150void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
151 if (empty()) { MBBNumbering.clear(); return; }
152 MachineFunction::iterator MBBI, E = end();
153 if (MBB == 0)
154 MBBI = begin();
155 else
156 MBBI = MBB;
157
158 // Figure out the block number this should have.
159 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000160 if (MBBI != begin())
161 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000162
163 for (; MBBI != E; ++MBBI, ++BlockNo) {
164 if (MBBI->getNumber() != (int)BlockNo) {
165 // Remove use of the old number.
166 if (MBBI->getNumber() != -1) {
167 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
168 "MBB number mismatch!");
169 MBBNumbering[MBBI->getNumber()] = 0;
170 }
171
172 // If BlockNo is already taken, set that block's number to -1.
173 if (MBBNumbering[BlockNo])
174 MBBNumbering[BlockNo]->setNumber(-1);
175
176 MBBNumbering[BlockNo] = MBBI;
177 MBBI->setNumber(BlockNo);
178 }
179 }
180
181 // Okay, all the blocks are renumbered. If we have compactified the block
182 // numbering, shrink MBBNumbering now.
183 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
184 MBBNumbering.resize(BlockNo);
185}
186
187
Bill Wendlingbcd24982006-12-07 20:28:15 +0000188void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner10491642002-10-30 00:48:05 +0000189
190void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000191 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000192
193 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000194 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000195
196 // Print JumpTable Information
197 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000198
199 // Print Constant Pool
200 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000201
202 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
203
204 if (livein_begin() != livein_end()) {
205 OS << "Live Ins:";
206 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
207 if (MRI)
208 OS << " " << MRI->getName(I->first);
209 else
210 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000211
212 if (I->second)
213 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000214 }
215 OS << "\n";
216 }
217 if (liveout_begin() != liveout_end()) {
218 OS << "Live Outs:";
219 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
220 if (MRI)
221 OS << " " << MRI->getName(*I);
222 else
223 OS << " Reg #" << *I;
224 OS << "\n";
225 }
226
Brian Gaeke90421cd2004-02-13 04:39:55 +0000227 for (const_iterator BB = begin(); BB != end(); ++BB)
228 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000229
230 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000231}
232
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000233/// CFGOnly flag - This is used to control whether or not the CFG graph printer
234/// prints out the contents of basic blocks or not. This is acceptable because
235/// this code is only really used for debugging purposes.
236///
237static bool CFGOnly = false;
238
239namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000240 template<>
241 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
242 static std::string getGraphName(const MachineFunction *F) {
243 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000244 }
245
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000246 static std::string getNodeLabel(const MachineBasicBlock *Node,
247 const MachineFunction *Graph) {
248 if (CFGOnly && Node->getBasicBlock() &&
249 !Node->getBasicBlock()->getName().empty())
250 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000251
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000252 std::ostringstream Out;
253 if (CFGOnly) {
254 Out << Node->getNumber() << ':';
255 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000256 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000257
258 Node->print(Out);
259
260 std::string OutStr = Out.str();
261 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
262
263 // Process string output to make it nicer...
264 for (unsigned i = 0; i != OutStr.length(); ++i)
265 if (OutStr[i] == '\n') { // Left justify
266 OutStr[i] = '\\';
267 OutStr.insert(OutStr.begin()+i+1, 'l');
268 }
269 return OutStr;
270 }
271 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000272}
273
274void MachineFunction::viewCFG() const
275{
Jim Laskey851a22d2005-10-12 12:09:05 +0000276#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000277 ViewGraph(this, "mf" + getFunction()->getName());
278#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000279 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
280 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000281#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000282}
283
284void MachineFunction::viewCFGOnly() const
285{
286 CFGOnly = true;
287 viewCFG();
288 CFGOnly = false;
289}
290
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000291// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000292// the MachineCodeForFunction object for the given function.
293// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000294// get() -- Returns a handle to the object.
295// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000296// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000297//
Misha Brukmanfce11432002-10-28 00:28:31 +0000298MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000299MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000300{
Chris Lattnere316efc2002-10-29 23:18:43 +0000301 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000302 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000303 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
304 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000305 return *mcInfo;
306}
307
Chris Lattner16c45e92003-12-20 10:20:58 +0000308void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000309 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000310 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000311}
312
Chris Lattner335d5c32002-10-28 05:58:46 +0000313MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000314{
Chris Lattnere316efc2002-10-29 23:18:43 +0000315 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000316 assert(mc && "Call construct() method first to allocate the object");
317 return *mc;
318}
319
Chris Lattner831fdcf2002-12-25 05:03:22 +0000320void MachineFunction::clearSSARegMap() {
321 delete SSARegMapping;
322 SSARegMapping = 0;
323}
324
Chris Lattner955fad12002-12-28 20:37:16 +0000325//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000326// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000327//===----------------------------------------------------------------------===//
328
Chris Lattner9085d8a2003-01-16 18:35:57 +0000329void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000330 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000331
Chris Lattner955fad12002-12-28 20:37:16 +0000332 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
333 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000334 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000335 if (SO.Size == 0)
336 OS << "variable sized";
337 else
Chris Lattner0db07092005-05-13 22:54:44 +0000338 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
339 OS << " alignment is " << SO.Alignment << " byte"
340 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000341
Chris Lattner955fad12002-12-28 20:37:16 +0000342 if (i < NumFixedObjects)
343 OS << " fixed";
344 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000345 int64_t Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000346 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000347 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000348 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000349 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000350 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000351 OS << "]";
352 }
353 OS << "\n";
354 }
355
356 if (HasVarSizedObjects)
357 OS << " Stack frame contains variable sized objects\n";
358}
359
Chris Lattner9085d8a2003-01-16 18:35:57 +0000360void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000361 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000362}
Chris Lattner955fad12002-12-28 20:37:16 +0000363
364
365//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000366// MachineJumpTableInfo implementation
367//===----------------------------------------------------------------------===//
368
369/// getJumpTableIndex - Create a new jump table entry in the jump table info
370/// or return an existing one.
371///
372unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000373 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000374 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000375 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
376 if (JumpTables[i].MBBs == DestBBs)
377 return i;
378
379 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
380 return JumpTables.size()-1;
381}
382
383
384void MachineJumpTableInfo::print(std::ostream &OS) const {
385 // FIXME: this is lame, maybe we could print out the MBB numbers or something
386 // like {1, 2, 4, 5, 3, 0}
387 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
388 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
389 << " entries\n";
390 }
391}
392
Bill Wendlingbcd24982006-12-07 20:28:15 +0000393void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000394
395
396//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000397// MachineConstantPool implementation
398//===----------------------------------------------------------------------===//
399
Evan Cheng9abd7c32006-09-14 05:50:57 +0000400const Type *MachineConstantPoolEntry::getType() const {
401 if (isMachineConstantPoolEntry())
402 return Val.MachineCPVal->getType();
403 return Val.ConstVal->getType();
404}
405
Evan Chengd6594ae2006-09-12 21:00:35 +0000406MachineConstantPool::~MachineConstantPool() {
407 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
408 if (Constants[i].isMachineConstantPoolEntry())
409 delete Constants[i].Val.MachineCPVal;
410}
411
Chris Lattner3029f922006-02-09 04:46:04 +0000412/// getConstantPoolIndex - Create a new entry in the constant pool or return
413/// an existing one. User must specify an alignment in bytes for the object.
414///
415unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
416 unsigned Alignment) {
417 assert(Alignment && "Alignment must be specified!");
418 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
419
420 // Check to see if we already have this constant.
421 //
422 // FIXME, this could be made much more efficient for large constant pools.
423 unsigned AlignMask = (1 << Alignment)-1;
424 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000425 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000426 return i;
427
428 unsigned Offset = 0;
429 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000430 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000431 Offset += TD->getTypeSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000432 Offset = (Offset+AlignMask)&~AlignMask;
433 }
434
435 Constants.push_back(MachineConstantPoolEntry(C, Offset));
436 return Constants.size()-1;
437}
438
Evan Chengd6594ae2006-09-12 21:00:35 +0000439unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
440 unsigned Alignment) {
441 assert(Alignment && "Alignment must be specified!");
442 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
443
444 // Check to see if we already have this constant.
445 //
446 // FIXME, this could be made much more efficient for large constant pools.
447 unsigned AlignMask = (1 << Alignment)-1;
448 int Idx = V->getExistingMachineCPValue(this, Alignment);
449 if (Idx != -1)
450 return (unsigned)Idx;
451
452 unsigned Offset = 0;
453 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000454 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000455 Offset += TD->getTypeSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000456 Offset = (Offset+AlignMask)&~AlignMask;
457 }
458
459 Constants.push_back(MachineConstantPoolEntry(V, Offset));
460 return Constants.size()-1;
461}
462
Chris Lattner3029f922006-02-09 04:46:04 +0000463
Chris Lattner4d149cd2003-01-13 00:23:03 +0000464void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000465 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000466 OS << " <cp #" << i << "> is";
467 if (Constants[i].isMachineConstantPoolEntry())
468 Constants[i].Val.MachineCPVal->print(OS);
469 else
470 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000471 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000472 OS << "\n";
473 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000474}
475
Bill Wendlingbcd24982006-12-07 20:28:15 +0000476void MachineConstantPool::dump() const { print(*cerr.stream()); }