blob: 0cba1567e184697ae60c5dbd57aaf1ef952c68a0 [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 Lattner16c45e92003-12-20 10:20:58 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner831fdcf2002-12-25 05:03:22 +000018#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnereb24db92002-12-28 21:08:26 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman37efe672006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000022#include "llvm/CodeGen/Passes.h"
Owen Anderson07000c62006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000024#include "llvm/Target/TargetLowering.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000026#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000027#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000028#include "llvm/Instructions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000030#include "llvm/Support/GraphWriter.h"
31#include "llvm/Support/LeakDetector.h"
32#include "llvm/ADT/STLExtras.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000033#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000034#include <fstream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000035#include <sstream>
Chris Lattner07f32d42003-12-20 09:17:07 +000036using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000037
Chris Lattnere316efc2002-10-29 23:18:43 +000038static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000039 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000040
Chris Lattnerefb9b812006-07-14 23:08:47 +000041// Out of line virtual function to home classes.
42void MachineFunctionPass::virtfn() {}
Chris Lattner227c3d32002-10-28 01:12:41 +000043
Chris Lattner227c3d32002-10-28 01:12:41 +000044namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000045 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000046 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000047 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000048
49 Printer (std::ostream *_OS, const std::string &_Banner) :
50 OS (_OS), Banner (_Banner) { }
51
Chris Lattner10491642002-10-30 00:48:05 +000052 const char *getPassName() const { return "MachineFunction Printer"; }
53
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.setPreservesAll();
56 }
57
Chris Lattner16c45e92003-12-20 10:20:58 +000058 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000059 (*OS) << Banner;
60 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000061 return false;
62 }
63 };
Chris Lattner227c3d32002-10-28 01:12:41 +000064}
65
Brian Gaeke09caa372004-01-30 21:53:46 +000066/// Returns a newly-created MachineFunction Printer pass. The default output
67/// stream is std::cerr; the default banner is empty.
68///
69FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000070 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000071 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000072}
73
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000074namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000075 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000076 const char *getPassName() const { return "Machine Code Deleter"; }
77
78 bool runOnMachineFunction(MachineFunction &MF) {
79 // Delete the annotation from the function now.
80 MachineFunction::destruct(MF.getFunction());
81 return true;
82 }
83 };
84}
85
86/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
87/// the current function, which should happen after the function has been
88/// emitted to a .s file or to memory.
89FunctionPass *llvm::createMachineCodeDeleter() {
90 return new Deleter();
91}
92
93
94
Chris Lattner227c3d32002-10-28 01:12:41 +000095//===---------------------------------------------------------------------===//
96// MachineFunction implementation
97//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000098
Chris Lattnerbca81442005-01-30 00:09:23 +000099MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000100 MachineBasicBlock* dummy = new MachineBasicBlock();
101 LeakDetector::removeGarbageObject(dummy);
102 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +0000103}
104
105void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000106 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
107 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000108 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000109 if (Parent != toList.Parent)
110 for (; first != last; ++first)
111 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000112}
Chris Lattner227c3d32002-10-28 01:12:41 +0000113
Chris Lattner10491642002-10-30 00:48:05 +0000114MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000115 const TargetMachine &TM)
Jim Laskeyf99c2322006-01-04 13:43:56 +0000116 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000117 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000118 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000119 FrameInfo = new MachineFrameInfo();
Chris Lattner3029f922006-02-09 04:46:04 +0000120 ConstantPool = new MachineConstantPool(TM.getTargetData());
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000121
122 // Set up jump table.
123 const TargetData &TD = *TM.getTargetData();
124 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
125 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Chris Lattner58092e32007-01-20 22:35:55 +0000126 unsigned Alignment = IsPic ? TD.getIntABIAlignment()
127 : TD.getPointerABIAlignment();
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000128 JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
129
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000130 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000131}
132
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000133MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000134 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000135 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000136 delete MFInfo;
137 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000138 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000139 delete JumpTableInfo;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000140 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000141}
142
Chris Lattnere70cab02006-10-03 19:18:57 +0000143
144/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
145/// recomputes them. This guarantees that the MBB numbers are sequential,
146/// dense, and match the ordering of the blocks within the function. If a
147/// specific MachineBasicBlock is specified, only that block and those after
148/// it are renumbered.
149void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
150 if (empty()) { MBBNumbering.clear(); return; }
151 MachineFunction::iterator MBBI, E = end();
152 if (MBB == 0)
153 MBBI = begin();
154 else
155 MBBI = MBB;
156
157 // Figure out the block number this should have.
158 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000159 if (MBBI != begin())
160 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000161
162 for (; MBBI != E; ++MBBI, ++BlockNo) {
163 if (MBBI->getNumber() != (int)BlockNo) {
164 // Remove use of the old number.
165 if (MBBI->getNumber() != -1) {
166 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
167 "MBB number mismatch!");
168 MBBNumbering[MBBI->getNumber()] = 0;
169 }
170
171 // If BlockNo is already taken, set that block's number to -1.
172 if (MBBNumbering[BlockNo])
173 MBBNumbering[BlockNo]->setNumber(-1);
174
175 MBBNumbering[BlockNo] = MBBI;
176 MBBI->setNumber(BlockNo);
177 }
178 }
179
180 // Okay, all the blocks are renumbered. If we have compactified the block
181 // numbering, shrink MBBNumbering now.
182 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
183 MBBNumbering.resize(BlockNo);
184}
185
186
Bill Wendlingbcd24982006-12-07 20:28:15 +0000187void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner10491642002-10-30 00:48:05 +0000188
189void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000190 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000191
192 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000193 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000194
195 // Print JumpTable Information
196 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000197
198 // Print Constant Pool
199 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000200
201 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
202
203 if (livein_begin() != livein_end()) {
204 OS << "Live Ins:";
205 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
206 if (MRI)
207 OS << " " << MRI->getName(I->first);
208 else
209 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000210
211 if (I->second)
212 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000213 }
214 OS << "\n";
215 }
216 if (liveout_begin() != liveout_end()) {
217 OS << "Live Outs:";
218 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
219 if (MRI)
220 OS << " " << MRI->getName(*I);
221 else
222 OS << " Reg #" << *I;
223 OS << "\n";
224 }
225
Brian Gaeke90421cd2004-02-13 04:39:55 +0000226 for (const_iterator BB = begin(); BB != end(); ++BB)
227 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000228
229 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000230}
231
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000232/// CFGOnly flag - This is used to control whether or not the CFG graph printer
233/// prints out the contents of basic blocks or not. This is acceptable because
234/// this code is only really used for debugging purposes.
235///
236static bool CFGOnly = false;
237
238namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000239 template<>
240 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
241 static std::string getGraphName(const MachineFunction *F) {
242 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000243 }
244
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000245 static std::string getNodeLabel(const MachineBasicBlock *Node,
246 const MachineFunction *Graph) {
247 if (CFGOnly && Node->getBasicBlock() &&
248 !Node->getBasicBlock()->getName().empty())
249 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000250
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000251 std::ostringstream Out;
252 if (CFGOnly) {
253 Out << Node->getNumber() << ':';
254 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000255 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000256
257 Node->print(Out);
258
259 std::string OutStr = Out.str();
260 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
261
262 // Process string output to make it nicer...
263 for (unsigned i = 0; i != OutStr.length(); ++i)
264 if (OutStr[i] == '\n') { // Left justify
265 OutStr[i] = '\\';
266 OutStr.insert(OutStr.begin()+i+1, 'l');
267 }
268 return OutStr;
269 }
270 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000271}
272
273void MachineFunction::viewCFG() const
274{
Jim Laskey851a22d2005-10-12 12:09:05 +0000275#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000276 ViewGraph(this, "mf" + getFunction()->getName());
277#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000278 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
279 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000280#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000281}
282
283void MachineFunction::viewCFGOnly() const
284{
285 CFGOnly = true;
286 viewCFG();
287 CFGOnly = false;
288}
289
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000290// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000291// the MachineCodeForFunction object for the given function.
292// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000293// get() -- Returns a handle to the object.
294// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000295// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000296//
Misha Brukmanfce11432002-10-28 00:28:31 +0000297MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000298MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000299{
Chris Lattnere316efc2002-10-29 23:18:43 +0000300 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000301 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000302 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
303 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000304 return *mcInfo;
305}
306
Chris Lattner16c45e92003-12-20 10:20:58 +0000307void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000308 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000309 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000310}
311
Chris Lattner335d5c32002-10-28 05:58:46 +0000312MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000313{
Chris Lattnere316efc2002-10-29 23:18:43 +0000314 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000315 assert(mc && "Call construct() method first to allocate the object");
316 return *mc;
317}
318
Chris Lattner831fdcf2002-12-25 05:03:22 +0000319void MachineFunction::clearSSARegMap() {
320 delete SSARegMapping;
321 SSARegMapping = 0;
322}
323
Chris Lattner955fad12002-12-28 20:37:16 +0000324//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000325// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000326//===----------------------------------------------------------------------===//
327
Chris Lattner9085d8a2003-01-16 18:35:57 +0000328void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000329 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000330
Chris Lattner955fad12002-12-28 20:37:16 +0000331 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
332 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000333 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000334 if (SO.Size == 0)
335 OS << "variable sized";
336 else
Chris Lattner0db07092005-05-13 22:54:44 +0000337 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
338 OS << " alignment is " << SO.Alignment << " byte"
339 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000340
Chris Lattner955fad12002-12-28 20:37:16 +0000341 if (i < NumFixedObjects)
342 OS << " fixed";
343 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000344 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000345 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000346 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000347 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000348 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000349 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000350 OS << "]";
351 }
352 OS << "\n";
353 }
354
355 if (HasVarSizedObjects)
356 OS << " Stack frame contains variable sized objects\n";
357}
358
Chris Lattner9085d8a2003-01-16 18:35:57 +0000359void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000360 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000361}
Chris Lattner955fad12002-12-28 20:37:16 +0000362
363
364//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000365// MachineJumpTableInfo implementation
366//===----------------------------------------------------------------------===//
367
368/// getJumpTableIndex - Create a new jump table entry in the jump table info
369/// or return an existing one.
370///
371unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000372 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000373 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000374 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
375 if (JumpTables[i].MBBs == DestBBs)
376 return i;
377
378 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
379 return JumpTables.size()-1;
380}
381
382
383void MachineJumpTableInfo::print(std::ostream &OS) const {
384 // FIXME: this is lame, maybe we could print out the MBB numbers or something
385 // like {1, 2, 4, 5, 3, 0}
386 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
387 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
388 << " entries\n";
389 }
390}
391
Bill Wendlingbcd24982006-12-07 20:28:15 +0000392void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000393
394
395//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000396// MachineConstantPool implementation
397//===----------------------------------------------------------------------===//
398
Evan Cheng9abd7c32006-09-14 05:50:57 +0000399const Type *MachineConstantPoolEntry::getType() const {
400 if (isMachineConstantPoolEntry())
401 return Val.MachineCPVal->getType();
402 return Val.ConstVal->getType();
403}
404
Evan Chengd6594ae2006-09-12 21:00:35 +0000405MachineConstantPool::~MachineConstantPool() {
406 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
407 if (Constants[i].isMachineConstantPoolEntry())
408 delete Constants[i].Val.MachineCPVal;
409}
410
Chris Lattner3029f922006-02-09 04:46:04 +0000411/// getConstantPoolIndex - Create a new entry in the constant pool or return
412/// an existing one. User must specify an alignment in bytes for the object.
413///
414unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
415 unsigned Alignment) {
416 assert(Alignment && "Alignment must be specified!");
417 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
418
419 // Check to see if we already have this constant.
420 //
421 // FIXME, this could be made much more efficient for large constant pools.
422 unsigned AlignMask = (1 << Alignment)-1;
423 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Chengd6594ae2006-09-12 21:00:35 +0000424 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000425 return i;
426
427 unsigned Offset = 0;
428 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000429 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000430 Offset += TD->getTypeSize(Constants.back().getType());
Chris Lattner3029f922006-02-09 04:46:04 +0000431 Offset = (Offset+AlignMask)&~AlignMask;
432 }
433
434 Constants.push_back(MachineConstantPoolEntry(C, Offset));
435 return Constants.size()-1;
436}
437
Evan Chengd6594ae2006-09-12 21:00:35 +0000438unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
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 int Idx = V->getExistingMachineCPValue(this, Alignment);
448 if (Idx != -1)
449 return (unsigned)Idx;
450
451 unsigned Offset = 0;
452 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000453 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000454 Offset += TD->getTypeSize(Constants.back().getType());
Evan Chengd6594ae2006-09-12 21:00:35 +0000455 Offset = (Offset+AlignMask)&~AlignMask;
456 }
457
458 Constants.push_back(MachineConstantPoolEntry(V, Offset));
459 return Constants.size()-1;
460}
461
Chris Lattner3029f922006-02-09 04:46:04 +0000462
Chris Lattner4d149cd2003-01-13 00:23:03 +0000463void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000464 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-09-12 21:00:35 +0000465 OS << " <cp #" << i << "> is";
466 if (Constants[i].isMachineConstantPoolEntry())
467 Constants[i].Val.MachineCPVal->print(OS);
468 else
469 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Chengd472ad72006-12-22 02:04:05 +0000470 OS << " , offset=" << Constants[i].getOffset();
Evan Chengb8973bd2006-01-31 22:23:14 +0000471 OS << "\n";
472 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000473}
474
Bill Wendlingbcd24982006-12-07 20:28:15 +0000475void MachineConstantPool::dump() const { print(*cerr.stream()); }