blob: 5aaae6deb510b36af5830befa17ad1a696cb563f [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"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000025#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000026#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000027#include "llvm/Instructions.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000029#include "llvm/Support/GraphWriter.h"
30#include "llvm/Support/LeakDetector.h"
31#include "llvm/ADT/STLExtras.h"
Jim Laskey851a22d2005-10-12 12:09:05 +000032#include "llvm/Config/config.h"
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000033#include <fstream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000034#include <sstream>
Chris Lattner07f32d42003-12-20 09:17:07 +000035using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000036
Chris Lattnere316efc2002-10-29 23:18:43 +000037static AnnotationID MF_AID(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000038 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
Chris Lattnerf2868ce2002-02-03 07:54:50 +000039
Chris Lattnerefb9b812006-07-14 23:08:47 +000040// Out of line virtual function to home classes.
41void MachineFunctionPass::virtfn() {}
Chris Lattner227c3d32002-10-28 01:12:41 +000042
Chris Lattner227c3d32002-10-28 01:12:41 +000043namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000044 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
Brian Gaeke09caa372004-01-30 21:53:46 +000045 std::ostream *OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000046 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000047
48 Printer (std::ostream *_OS, const std::string &_Banner) :
49 OS (_OS), Banner (_Banner) { }
50
Chris Lattner10491642002-10-30 00:48:05 +000051 const char *getPassName() const { return "MachineFunction Printer"; }
52
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.setPreservesAll();
55 }
56
Chris Lattner16c45e92003-12-20 10:20:58 +000057 bool runOnMachineFunction(MachineFunction &MF) {
Brian Gaeke09caa372004-01-30 21:53:46 +000058 (*OS) << Banner;
59 MF.print (*OS);
Chris Lattner10491642002-10-30 00:48:05 +000060 return false;
61 }
62 };
Chris Lattner227c3d32002-10-28 01:12:41 +000063}
64
Brian Gaeke09caa372004-01-30 21:53:46 +000065/// Returns a newly-created MachineFunction Printer pass. The default output
66/// stream is std::cerr; the default banner is empty.
67///
68FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000069 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000070 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000071}
72
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000073namespace {
Chris Lattnerf8c68f62006-06-28 22:17:39 +000074 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
Alkis Evlogimenosc81efdc2004-02-15 00:03:15 +000075 const char *getPassName() const { return "Machine Code Deleter"; }
76
77 bool runOnMachineFunction(MachineFunction &MF) {
78 // Delete the annotation from the function now.
79 MachineFunction::destruct(MF.getFunction());
80 return true;
81 }
82 };
83}
84
85/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
86/// the current function, which should happen after the function has been
87/// emitted to a .s file or to memory.
88FunctionPass *llvm::createMachineCodeDeleter() {
89 return new Deleter();
90}
91
92
93
Chris Lattner227c3d32002-10-28 01:12:41 +000094//===---------------------------------------------------------------------===//
95// MachineFunction implementation
96//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000097
Chris Lattnerbca81442005-01-30 00:09:23 +000098MachineBasicBlock* ilist_traits<MachineBasicBlock>::createSentinel() {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000099 MachineBasicBlock* dummy = new MachineBasicBlock();
100 LeakDetector::removeGarbageObject(dummy);
101 return dummy;
Tanya Lattner792699c2004-05-24 06:11:51 +0000102}
103
104void ilist_traits<MachineBasicBlock>::transferNodesFromList(
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000105 iplist<MachineBasicBlock, ilist_traits<MachineBasicBlock> >& toList,
106 ilist_iterator<MachineBasicBlock> first,
Chris Lattner9d5d7592005-01-29 18:41:25 +0000107 ilist_iterator<MachineBasicBlock> last) {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000108 if (Parent != toList.Parent)
109 for (; first != last; ++first)
110 first->Parent = toList.Parent;
Tanya Lattner792699c2004-05-24 06:11:51 +0000111}
Chris Lattner227c3d32002-10-28 01:12:41 +0000112
Chris Lattner10491642002-10-30 00:48:05 +0000113MachineFunction::MachineFunction(const Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +0000114 const TargetMachine &TM)
Jim Laskeyf99c2322006-01-04 13:43:56 +0000115 : Annotation(MF_AID), Fn(F), Target(TM), UsedPhysRegs(0) {
Misha Brukmanb7825bc2002-11-20 18:55:27 +0000116 SSARegMapping = new SSARegMap();
Chris Lattnerad828162004-08-16 22:36:34 +0000117 MFInfo = 0;
Chris Lattnereb24db92002-12-28 21:08:26 +0000118 FrameInfo = new MachineFrameInfo();
Chris Lattner3029f922006-02-09 04:46:04 +0000119 ConstantPool = new MachineConstantPool(TM.getTargetData());
Nate Begeman37efe672006-04-22 18:53:45 +0000120 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000121 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000122}
123
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000124MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000125 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000126 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000127 delete MFInfo;
128 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000129 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000130 delete JumpTableInfo;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000131 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000132}
133
Chris Lattnere70cab02006-10-03 19:18:57 +0000134
135/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
136/// recomputes them. This guarantees that the MBB numbers are sequential,
137/// dense, and match the ordering of the blocks within the function. If a
138/// specific MachineBasicBlock is specified, only that block and those after
139/// it are renumbered.
140void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
141 if (empty()) { MBBNumbering.clear(); return; }
142 MachineFunction::iterator MBBI, E = end();
143 if (MBB == 0)
144 MBBI = begin();
145 else
146 MBBI = MBB;
147
148 // Figure out the block number this should have.
149 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000150 if (MBBI != begin())
151 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000152
153 for (; MBBI != E; ++MBBI, ++BlockNo) {
154 if (MBBI->getNumber() != (int)BlockNo) {
155 // Remove use of the old number.
156 if (MBBI->getNumber() != -1) {
157 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
158 "MBB number mismatch!");
159 MBBNumbering[MBBI->getNumber()] = 0;
160 }
161
162 // If BlockNo is already taken, set that block's number to -1.
163 if (MBBNumbering[BlockNo])
164 MBBNumbering[BlockNo]->setNumber(-1);
165
166 MBBNumbering[BlockNo] = MBBI;
167 MBBI->setNumber(BlockNo);
168 }
169 }
170
171 // Okay, all the blocks are renumbered. If we have compactified the block
172 // numbering, shrink MBBNumbering now.
173 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
174 MBBNumbering.resize(BlockNo);
175}
176
177
Bill Wendlingbcd24982006-12-07 20:28:15 +0000178void MachineFunction::dump() const { print(*cerr.stream()); }
Chris Lattner10491642002-10-30 00:48:05 +0000179
180void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000181 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000182
183 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000184 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000185
186 // Print JumpTable Information
187 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000188
189 // Print Constant Pool
190 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000191
192 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
193
194 if (livein_begin() != livein_end()) {
195 OS << "Live Ins:";
196 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
197 if (MRI)
198 OS << " " << MRI->getName(I->first);
199 else
200 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000201
202 if (I->second)
203 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000204 }
205 OS << "\n";
206 }
207 if (liveout_begin() != liveout_end()) {
208 OS << "Live Outs:";
209 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
210 if (MRI)
211 OS << " " << MRI->getName(*I);
212 else
213 OS << " Reg #" << *I;
214 OS << "\n";
215 }
216
Brian Gaeke90421cd2004-02-13 04:39:55 +0000217 for (const_iterator BB = begin(); BB != end(); ++BB)
218 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000219
220 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000221}
222
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000223/// CFGOnly flag - This is used to control whether or not the CFG graph printer
224/// prints out the contents of basic blocks or not. This is acceptable because
225/// this code is only really used for debugging purposes.
226///
227static bool CFGOnly = false;
228
229namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000230 template<>
231 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
232 static std::string getGraphName(const MachineFunction *F) {
233 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000234 }
235
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000236 static std::string getNodeLabel(const MachineBasicBlock *Node,
237 const MachineFunction *Graph) {
238 if (CFGOnly && Node->getBasicBlock() &&
239 !Node->getBasicBlock()->getName().empty())
240 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000241
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000242 std::ostringstream Out;
243 if (CFGOnly) {
244 Out << Node->getNumber() << ':';
245 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000246 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000247
248 Node->print(Out);
249
250 std::string OutStr = Out.str();
251 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
252
253 // Process string output to make it nicer...
254 for (unsigned i = 0; i != OutStr.length(); ++i)
255 if (OutStr[i] == '\n') { // Left justify
256 OutStr[i] = '\\';
257 OutStr.insert(OutStr.begin()+i+1, 'l');
258 }
259 return OutStr;
260 }
261 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000262}
263
264void MachineFunction::viewCFG() const
265{
Jim Laskey851a22d2005-10-12 12:09:05 +0000266#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000267 ViewGraph(this, "mf" + getFunction()->getName());
268#else
Bill Wendlingbcd24982006-12-07 20:28:15 +0000269 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
270 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000271#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000272}
273
274void MachineFunction::viewCFGOnly() const
275{
276 CFGOnly = true;
277 viewCFG();
278 CFGOnly = false;
279}
280
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000281// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000282// the MachineCodeForFunction object for the given function.
283// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000284// get() -- Returns a handle to the object.
285// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000286// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000287//
Misha Brukmanfce11432002-10-28 00:28:31 +0000288MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000289MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000290{
Chris Lattnere316efc2002-10-29 23:18:43 +0000291 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000292 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000293 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
294 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000295 return *mcInfo;
296}
297
Chris Lattner16c45e92003-12-20 10:20:58 +0000298void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000299 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000300 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000301}
302
Chris Lattner335d5c32002-10-28 05:58:46 +0000303MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000304{
Chris Lattnere316efc2002-10-29 23:18:43 +0000305 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000306 assert(mc && "Call construct() method first to allocate the object");
307 return *mc;
308}
309
Chris Lattner831fdcf2002-12-25 05:03:22 +0000310void MachineFunction::clearSSARegMap() {
311 delete SSARegMapping;
312 SSARegMapping = 0;
313}
314
Chris Lattner955fad12002-12-28 20:37:16 +0000315//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000316// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000317//===----------------------------------------------------------------------===//
318
Chris Lattner9085d8a2003-01-16 18:35:57 +0000319void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000320 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000321
Chris Lattner955fad12002-12-28 20:37:16 +0000322 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
323 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000324 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000325 if (SO.Size == 0)
326 OS << "variable sized";
327 else
Chris Lattner0db07092005-05-13 22:54:44 +0000328 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
329 OS << " alignment is " << SO.Alignment << " byte"
330 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000331
Chris Lattner955fad12002-12-28 20:37:16 +0000332 if (i < NumFixedObjects)
333 OS << " fixed";
334 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000335 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000336 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000337 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000338 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000339 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000340 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000341 OS << "]";
342 }
343 OS << "\n";
344 }
345
346 if (HasVarSizedObjects)
347 OS << " Stack frame contains variable sized objects\n";
348}
349
Chris Lattner9085d8a2003-01-16 18:35:57 +0000350void MachineFrameInfo::dump(const MachineFunction &MF) const {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000351 print(MF, *cerr.stream());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000352}
Chris Lattner955fad12002-12-28 20:37:16 +0000353
354
355//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000356// MachineJumpTableInfo implementation
357//===----------------------------------------------------------------------===//
358
359/// getJumpTableIndex - Create a new jump table entry in the jump table info
360/// or return an existing one.
361///
362unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000363 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000364 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000365 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
366 if (JumpTables[i].MBBs == DestBBs)
367 return i;
368
369 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
370 return JumpTables.size()-1;
371}
372
373
374void MachineJumpTableInfo::print(std::ostream &OS) const {
375 // FIXME: this is lame, maybe we could print out the MBB numbers or something
376 // like {1, 2, 4, 5, 3, 0}
377 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
378 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
379 << " entries\n";
380 }
381}
382
Nate Begeman3700a4d2006-04-22 23:52:35 +0000383unsigned MachineJumpTableInfo::getEntrySize() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000384 return TD->getPointerSize();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000385}
386
387unsigned MachineJumpTableInfo::getAlignment() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000388 return TD->getPointerAlignment();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000389}
390
Bill Wendlingbcd24982006-12-07 20:28:15 +0000391void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000392
393
394//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000395// MachineConstantPool implementation
396//===----------------------------------------------------------------------===//
397
Evan Cheng9abd7c32006-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 Chengd6594ae2006-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 Lattner3029f922006-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 Chengd6594ae2006-09-12 21:00:35 +0000423 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
Chris Lattner3029f922006-02-09 04:46:04 +0000424 return i;
425
426 unsigned Offset = 0;
427 if (!Constants.empty()) {
Evan Chenga17cf0a2006-09-14 07:41:12 +0000428 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000429 Offset += TD->getTypeSize(Constants.back().getType());
Chris Lattner3029f922006-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 Chengd6594ae2006-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 Chenga17cf0a2006-09-14 07:41:12 +0000452 Offset = Constants.back().getOffset();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000453 Offset += TD->getTypeSize(Constants.back().getType());
Evan Chengd6594ae2006-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 Lattner3029f922006-02-09 04:46:04 +0000461
Chris Lattner4d149cd2003-01-13 00:23:03 +0000462void MachineConstantPool::print(std::ostream &OS) const {
Evan Chengb8973bd2006-01-31 22:23:14 +0000463 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Evan Chengd6594ae2006-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;
Chris Lattner3029f922006-02-09 04:46:04 +0000469 OS << " , offset=" << Constants[i].Offset;
Evan Chengb8973bd2006-01-31 22:23:14 +0000470 OS << "\n";
471 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000472}
473
Bill Wendlingbcd24982006-12-07 20:28:15 +0000474void MachineConstantPool::dump() const { print(*cerr.stream()); }