blob: d26b3258fe8fe6c0c7fc490605c01bb06caf4835 [file] [log] [blame]
Chris Lattner8494d082002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00002//
John Criswell482202a2003-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 Evlogimenos58350a72004-09-05 18:41:35 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00009//
Chris Lattner8494d082002-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 Lattnereda6bd72002-02-03 07:54:50 +000015
Chris Lattner4cbb97b2003-12-20 10:20:58 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner448fb452002-12-25 05:03:22 +000017#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner448fb452002-12-25 05:03:22 +000018#include "llvm/CodeGen/SSARegMap.h"
Chris Lattnerca4362f2002-12-28 21:08:26 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerc6807e82003-01-13 00:23:03 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner4cbb97b2003-12-20 10:20:58 +000022#include "llvm/CodeGen/Passes.h"
Owen Anderson8c2c1e92006-05-12 06:33:49 +000023#include "llvm/Target/TargetData.h"
Jim Laskey70323a82006-12-14 19:17:33 +000024#include "llvm/Target/TargetLowering.h"
Chris Lattnereda6bd72002-02-03 07:54:50 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattner871e5912002-12-28 21:00:25 +000026#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner62b7fd12002-04-07 20:49:59 +000027#include "llvm/Function.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000028#include "llvm/Instructions.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000029#include "llvm/Support/Compiler.h"
Chris Lattnerbd7286e2006-10-03 20:19:23 +000030#include "llvm/Support/GraphWriter.h"
31#include "llvm/Support/LeakDetector.h"
32#include "llvm/ADT/STLExtras.h"
Jim Laskeyd00db252005-10-12 12:09:05 +000033#include "llvm/Config/config.h"
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +000034#include <fstream>
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +000035#include <sstream>
Chris Lattner9a3478e2003-12-20 09:17:07 +000036using namespace llvm;
Chris Lattnereda6bd72002-02-03 07:54:50 +000037
Chris Lattnerbbd68ad2002-10-29 23:18:43 +000038static AnnotationID MF_AID(
Alkis Evlogimenos58350a72004-09-05 18:41:35 +000039 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnereda6bd72002-02-03 07:54:50 +000040
Chris Lattner436c2dd2006-07-14 23:08:47 +000041// Out of line virtual function to home classes.
42void MachineFunctionPass::virtfn() {}
Chris Lattner6d8a6c62002-10-28 01:12:41 +000043
Chris Lattner6d8a6c62002-10-28 01:12:41 +000044namespace {
Chris Lattnere097e6f2006-06-28 22:17:39 +000045 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Brian Gaeke845c0dd2004-01-30 21:53:46 +000046 std::ostream *OS;
Chris Lattnercae054f2004-02-01 05:25:07 +000047 const std::string Banner;
Brian Gaeke845c0dd2004-01-30 21:53:46 +000048
49 Printer (std::ostream *_OS, const std::string &_Banner) :
50 OS (_OS), Banner (_Banner) { }
51
Chris Lattner214808f2002-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 Lattner4cbb97b2003-12-20 10:20:58 +000058 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke845c0dd2004-01-30 21:53:46 +000059 (*OS) << Banner;
60 MF.print (*OS);
Chris Lattner214808f2002-10-30 00:48:05 +000061 return false;
62 }
63 };
Chris Lattner6d8a6c62002-10-28 01:12:41 +000064}
65
Brian Gaeke845c0dd2004-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 Lattner304053c2005-01-23 22:13:58 +000070 const std::string &Banner){
Brian Gaeke845c0dd2004-01-30 21:53:46 +000071 return new Printer(OS, Banner);
Chris Lattner214808f2002-10-30 00:48:05 +000072}
73
Alkis Evlogimenos6a355162004-02-15 00:03:15 +000074namespace {
Chris Lattnere097e6f2006-06-28 22:17:39 +000075 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Alkis Evlogimenos6a355162004-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 Lattner6d8a6c62002-10-28 01:12:41 +000095//===---------------------------------------------------------------------===//
96// MachineFunction implementation
97//===---------------------------------------------------------------------===//
Chris Lattnere6074aa2005-01-29 18:41:25 +000098
Chris Lattnerf6c93e32005-01-30 00:09:23 +000099MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000100 MachineBasicBlock* dummy = new MachineBasicBlock();
101 LeakDetector::removeGarbageObject(dummy);
102 return dummy;
Tanya Lattnera578cb72004-05-24 06:11:51 +0000103}
104
105void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000106 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
107 ilist_iterator<MachineBasicBlock> first,
Chris Lattnere6074aa2005-01-29 18:41:25 +0000108 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000109 if (Parent != toList.Parent)
110 for (; first != last; ++first)
111 first->Parent = toList.Parent;
Tanya Lattnera578cb72004-05-24 06:11:51 +0000112}
Chris Lattner6d8a6c62002-10-28 01:12:41 +0000113
Chris Lattner214808f2002-10-30 00:48:05 +0000114MachineFunction::MachineFunction(const Function *F,
Chris Lattner32525642002-12-28 20:37:16 +0000115 const TargetMachine &TM)
Jim Laskey57a5e0b2006-01-04 13:43:56 +0000116 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmand5b111a2002-11-20 18:55:27 +0000117 SSARegMapping = new SSARegMap();
Chris Lattnera1d78022004-08-16 22:36:34 +0000118 MFInfo = 0;
Chris Lattnerca4362f2002-12-28 21:08:26 +0000119 FrameInfo = new MachineFrameInfo();
Chris Lattnerf6190822006-02-09 04:46:04 +0000120 ConstantPool = new MachineConstantPool(TM.getTargetData());
Jim Laskey70323a82006-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();
126 unsigned Alignment = IsPic ? TD.getIntAlignment() : TD.getPointerAlignment();
127 JumpTableInfo = new MachineJumpTableInfo(EntrySize, Alignment);
128
Tanya Lattner91fa3a92004-05-24 07:14:35 +0000129 BasicBlocks.Parent = this;
Chris Lattner448fb452002-12-25 05:03:22 +0000130}
131
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000132MachineFunction::~MachineFunction() {
Chris Lattner9a60c532004-07-01 06:29:07 +0000133 BasicBlocks.clear();
Chris Lattner448fb452002-12-25 05:03:22 +0000134 delete SSARegMapping;
Chris Lattner32525642002-12-28 20:37:16 +0000135 delete MFInfo;
136 delete FrameInfo;
Chris Lattnerc6807e82003-01-13 00:23:03 +0000137 delete ConstantPool;
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000138 delete JumpTableInfo;
Chris Lattner304053c2005-01-23 22:13:58 +0000139 delete[] UsedPhysRegs;
Chris Lattner214808f2002-10-30 00:48:05 +0000140}
141
Chris Lattner64fd9482006-10-03 19:18:57 +0000142
143/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
144/// recomputes them. This guarantees that the MBB numbers are sequential,
145/// dense, and match the ordering of the blocks within the function. If a
146/// specific MachineBasicBlock is specified, only that block and those after
147/// it are renumbered.
148void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
149 if (empty()) { MBBNumbering.clear(); return; }
150 MachineFunction::iterator MBBI, E = end();
151 if (MBB == 0)
152 MBBI = begin();
153 else
154 MBBI = MBB;
155
156 // Figure out the block number this should have.
157 unsigned BlockNo = 0;
Chris Lattnerbd7286e2006-10-03 20:19:23 +0000158 if (MBBI != begin())
159 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattner64fd9482006-10-03 19:18:57 +0000160
161 for (; MBBI != E; ++MBBI, ++BlockNo) {
162 if (MBBI->getNumber() != (int)BlockNo) {
163 // Remove use of the old number.
164 if (MBBI->getNumber() != -1) {
165 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
166 "MBB number mismatch!");
167 MBBNumbering[MBBI->getNumber()] = 0;
168 }
169
170 // If BlockNo is already taken, set that block's number to -1.
171 if (MBBNumbering[BlockNo])
172 MBBNumbering[BlockNo]->setNumber(-1);
173
174 MBBNumbering[BlockNo] = MBBI;
175 MBBI->setNumber(BlockNo);
176 }
177 }
178
179 // Okay, all the blocks are renumbered. If we have compactified the block
180 // numbering, shrink MBBNumbering now.
181 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
182 MBBNumbering.resize(BlockNo);
183}
184
185
Bill Wendling355fc5a2006-12-07 20:28:15 +0000186void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner214808f2002-10-30 00:48:05 +0000187
188void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke2fe0ac92004-03-29 21:58:31 +0000189 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner32525642002-12-28 20:37:16 +0000190
191 // Print Frame Information
Chris Lattnereb45c982003-01-16 18:35:57 +0000192 getFrameInfo()->print(*this, OS);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000193
194 // Print JumpTable Information
195 getJumpTableInfo()->print(OS);
Chris Lattnerc6807e82003-01-13 00:23:03 +0000196
197 // Print Constant Pool
198 getConstantPool()->print(OS);
Chris Lattnerd4d10ff2005-08-31 22:34:59 +0000199
200 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
201
202 if (livein_begin() != livein_end()) {
203 OS << "Live Ins:";
204 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
205 if (MRI)
206 OS << " " << MRI->getName(I->first);
207 else
208 OS << " Reg #" << I->first;
Chris Lattner52d0c782006-05-16 05:55:30 +0000209
210 if (I->second)
211 OS << " in VR#" << I->second << " ";
Chris Lattnerd4d10ff2005-08-31 22:34:59 +0000212 }
213 OS << "\n";
214 }
215 if (liveout_begin() != liveout_end()) {
216 OS << "Live Outs:";
217 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
218 if (MRI)
219 OS << " " << MRI->getName(*I);
220 else
221 OS << " Reg #" << *I;
222 OS << "\n";
223 }
224
Brian Gaeke52440fd2004-02-13 04:39:55 +0000225 for (const_iterator BB = begin(); BB != end(); ++BB)
226 BB->print(OS);
Brian Gaeke2fe0ac92004-03-29 21:58:31 +0000227
228 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner214808f2002-10-30 00:48:05 +0000229}
230
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000231/// CFGOnly flag - This is used to control whether or not the CFG graph printer
232/// prints out the contents of basic blocks or not. This is acceptable because
233/// this code is only really used for debugging purposes.
234///
235static bool CFGOnly = false;
236
237namespace llvm {
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000238 template<>
239 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
240 static std::string getGraphName(const MachineFunction *F) {
241 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000242 }
243
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000244 static std::string getNodeLabel(const MachineBasicBlock *Node,
245 const MachineFunction *Graph) {
246 if (CFGOnly && Node->getBasicBlock() &&
247 !Node->getBasicBlock()->getName().empty())
248 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000249
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000250 std::ostringstream Out;
251 if (CFGOnly) {
252 Out << Node->getNumber() << ':';
253 return Out.str();
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000254 }
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000255
256 Node->print(Out);
257
258 std::string OutStr = Out.str();
259 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
260
261 // Process string output to make it nicer...
262 for (unsigned i = 0; i != OutStr.length(); ++i)
263 if (OutStr[i] == '\n') { // Left justify
264 OutStr[i] = '\\';
265 OutStr.insert(OutStr.begin()+i+1, 'l');
266 }
267 return OutStr;
268 }
269 };
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000270}
271
272void MachineFunction::viewCFG() const
273{
Jim Laskeyd00db252005-10-12 12:09:05 +0000274#ifndef NDEBUG
Reid Spenceree7eaa22006-06-27 16:49:46 +0000275 ViewGraph(this, "mf" + getFunction()->getName());
276#else
Bill Wendling355fc5a2006-12-07 20:28:15 +0000277 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
278 << "systems with Graphviz or gv!\n";
Reid Spenceree7eaa22006-06-27 16:49:46 +0000279#endif // NDEBUG
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000280}
281
282void MachineFunction::viewCFGOnly() const
283{
284 CFGOnly = true;
285 viewCFG();
286 CFGOnly = false;
287}
288
Chris Lattnereda6bd72002-02-03 07:54:50 +0000289// The next two methods are used to construct and to retrieve
Chris Lattner62b7fd12002-04-07 20:49:59 +0000290// the MachineCodeForFunction object for the given function.
291// construct() -- Allocates and initializes for a given function and target
Chris Lattnereda6bd72002-02-03 07:54:50 +0000292// get() -- Returns a handle to the object.
293// This should not be called before "construct()"
Chris Lattner62b7fd12002-04-07 20:49:59 +0000294// for a given Function.
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000295//
Misha Brukman7ae7f842002-10-28 00:28:31 +0000296MachineFunction&
Chris Lattnerba3a8062002-10-28 05:58:46 +0000297MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000298{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000299 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner62b7fd12002-04-07 20:49:59 +0000300 "Object already exists for this function!");
Chris Lattnerba3a8062002-10-28 05:58:46 +0000301 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
302 Fn->addAnnotation(mcInfo);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000303 return *mcInfo;
304}
305
Chris Lattner4cbb97b2003-12-20 10:20:58 +0000306void MachineFunction::destruct(const Function *Fn) {
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000307 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner62b7fd12002-04-07 20:49:59 +0000308 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnereda6bd72002-02-03 07:54:50 +0000309}
310
Chris Lattnerba3a8062002-10-28 05:58:46 +0000311MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve7446b322002-03-18 03:36:30 +0000312{
Chris Lattnerbbd68ad2002-10-29 23:18:43 +0000313 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnereda6bd72002-02-03 07:54:50 +0000314 assert(mc && "Call construct() method first to allocate the object");
315 return *mc;
316}
317
Chris Lattner448fb452002-12-25 05:03:22 +0000318void MachineFunction::clearSSARegMap() {
319 delete SSARegMapping;
320 SSARegMapping = 0;
321}
322
Chris Lattner32525642002-12-28 20:37:16 +0000323//===----------------------------------------------------------------------===//
Chris Lattnerca4362f2002-12-28 21:08:26 +0000324// MachineFrameInfo implementation
Chris Lattner32525642002-12-28 20:37:16 +0000325//===----------------------------------------------------------------------===//
326
Chris Lattnereb45c982003-01-16 18:35:57 +0000327void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner185fa542004-06-02 05:56:52 +0000328 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattnereb45c982003-01-16 18:35:57 +0000329
Chris Lattner32525642002-12-28 20:37:16 +0000330 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
331 const StackObject &SO = Objects[i];
Chris Lattner77b220f2005-05-13 22:54:44 +0000332 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner32525642002-12-28 20:37:16 +0000333 if (SO.Size == 0)
334 OS << "variable sized";
335 else
Chris Lattner77b220f2005-05-13 22:54:44 +0000336 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
337 OS << " alignment is " << SO.Alignment << " byte"
338 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000339
Chris Lattner32525642002-12-28 20:37:16 +0000340 if (i < NumFixedObjects)
341 OS << " fixed";
342 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner5888b5d2004-06-11 06:37:11 +0000343 int Off = SO.SPOffset - ValOffset;
Chris Lattner32525642002-12-28 20:37:16 +0000344 OS << " at location [SP";
Chris Lattnereb45c982003-01-16 18:35:57 +0000345 if (Off > 0)
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000346 OS << "+" << Off;
Chris Lattnereb45c982003-01-16 18:35:57 +0000347 else if (Off < 0)
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000348 OS << Off;
Chris Lattner32525642002-12-28 20:37:16 +0000349 OS << "]";
350 }
351 OS << "\n";
352 }
353
354 if (HasVarSizedObjects)
355 OS << " Stack frame contains variable sized objects\n";
356}
357
Chris Lattnereb45c982003-01-16 18:35:57 +0000358void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendling355fc5a2006-12-07 20:28:15 +0000359 print(MF, *cerr.stream());
Chris Lattnereb45c982003-01-16 18:35:57 +0000360}
Chris Lattner32525642002-12-28 20:37:16 +0000361
362
363//===----------------------------------------------------------------------===//
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000364// MachineJumpTableInfo implementation
365//===----------------------------------------------------------------------===//
366
367/// getJumpTableIndex - Create a new jump table entry in the jump table info
368/// or return an existing one.
369///
370unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnercde339c2006-10-28 18:17:09 +0000371 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattner28328f92006-10-28 18:11:20 +0000372 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000373 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
374 if (JumpTables[i].MBBs == DestBBs)
375 return i;
376
377 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
378 return JumpTables.size()-1;
379}
380
381
382void MachineJumpTableInfo::print(std::ostream &OS) const {
383 // FIXME: this is lame, maybe we could print out the MBB numbers or something
384 // like {1, 2, 4, 5, 3, 0}
385 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
386 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
387 << " entries\n";
388 }
389}
390
Bill Wendling355fc5a2006-12-07 20:28:15 +0000391void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000392
393
394//===----------------------------------------------------------------------===//
Chris Lattnerc6807e82003-01-13 00:23:03 +0000395// MachineConstantPool implementation
396//===----------------------------------------------------------------------===//
397
Evan Cheng4f9299552006-09-14 05:50:57 +0000398const Type *MachineConstantPoolEntry::getType() const {
399 if (isMachineConstantPoolEntry())
400 return Val.MachineCPVal->getType();
401 return Val.ConstVal->getType();
402}
403
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000404MachineConstantPool::~MachineConstantPool() {
405 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
406 if (Constants[i].isMachineConstantPoolEntry())
407 delete Constants[i].Val.MachineCPVal;
408}
409
Chris Lattnerf6190822006-02-09 04:46:04 +0000410/// getConstantPoolIndex - Create a new entry in the constant pool or return
411/// an existing one. User must specify an alignment in bytes for the object.
412///
413unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
414 unsigned Alignment) {
415 assert(Alignment && "Alignment must be specified!");
416 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
417
418 // Check to see if we already have this constant.
419 //
420 // FIXME, this could be made much more efficient for large constant pools.
421 unsigned AlignMask = (1 << Alignment)-1;
422 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000423 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattnerf6190822006-02-09 04:46:04 +0000424 return i;
425
426 unsigned Offset = 0;
427 if (!Constants.empty()) {
Evan Cheng616aa542006-09-14 07:41:12 +0000428 Offset = Constants.back().getOffset();
Evan Cheng4f9299552006-09-14 05:50:57 +0000429 Offset += TD->getTypeSize(Constants.back().getType());
Chris Lattnerf6190822006-02-09 04:46:04 +0000430 Offset = (Offset+AlignMask)&~AlignMask;
431 }
432
433 Constants.push_back(MachineConstantPoolEntry(C, Offset));
434 return Constants.size()-1;
435}
436
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000437unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
438 unsigned Alignment) {
439 assert(Alignment && "Alignment must be specified!");
440 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
441
442 // Check to see if we already have this constant.
443 //
444 // FIXME, this could be made much more efficient for large constant pools.
445 unsigned AlignMask = (1 << Alignment)-1;
446 int Idx = V->getExistingMachineCPValue(this, Alignment);
447 if (Idx != -1)
448 return (unsigned)Idx;
449
450 unsigned Offset = 0;
451 if (!Constants.empty()) {
Evan Cheng616aa542006-09-14 07:41:12 +0000452 Offset = Constants.back().getOffset();
Evan Cheng4f9299552006-09-14 05:50:57 +0000453 Offset += TD->getTypeSize(Constants.back().getType());
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000454 Offset = (Offset+AlignMask)&~AlignMask;
455 }
456
457 Constants.push_back(MachineConstantPoolEntry(V, Offset));
458 return Constants.size()-1;
459}
460
Chris Lattnerf6190822006-02-09 04:46:04 +0000461
Chris Lattnerc6807e82003-01-13 00:23:03 +0000462void MachineConstantPool::print(std::ostream &OS) const {
Evan Cheng32be2dc2006-01-31 22:23:14 +0000463 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000464 OS << " <cp #" << i << "> is";
465 if (Constants[i].isMachineConstantPoolEntry())
466 Constants[i].Val.MachineCPVal->print(OS);
467 else
468 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Cheng76355032006-12-22 02:04:05 +0000469 OS << " , offset=" << Constants[i].getOffset();
Evan Cheng32be2dc2006-01-31 22:23:14 +0000470 OS << "\n";
471 }
Chris Lattnerc6807e82003-01-13 00:23:03 +0000472}
473
Bill Wendling355fc5a2006-12-07 20:28:15 +0000474void MachineConstantPool::dump() const { print(*cerr.stream()); }