blob: 7617de962d733d72aff91608162beccb7ab88d23 [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>
Reid Spencer954da372004-07-04 12:19:56 +000034#include <iostream>
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +000035#include <sstream>
Tanya Lattner792699c2004-05-24 06:11:51 +000036
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());
Nate Begeman37efe672006-04-22 18:53:45 +0000122 JumpTableInfo = new MachineJumpTableInfo(TM.getTargetData());
Tanya Lattner17fb34b2004-05-24 07:14:35 +0000123 BasicBlocks.Parent = this;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000124}
125
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000126MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000127 BasicBlocks.clear();
Chris Lattner831fdcf2002-12-25 05:03:22 +0000128 delete SSARegMapping;
Chris Lattner955fad12002-12-28 20:37:16 +0000129 delete MFInfo;
130 delete FrameInfo;
Chris Lattner4d149cd2003-01-13 00:23:03 +0000131 delete ConstantPool;
Nate Begeman37efe672006-04-22 18:53:45 +0000132 delete JumpTableInfo;
Chris Lattnerce9c41e2005-01-23 22:13:58 +0000133 delete[] UsedPhysRegs;
Chris Lattner10491642002-10-30 00:48:05 +0000134}
135
Chris Lattnere70cab02006-10-03 19:18:57 +0000136
137/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
138/// recomputes them. This guarantees that the MBB numbers are sequential,
139/// dense, and match the ordering of the blocks within the function. If a
140/// specific MachineBasicBlock is specified, only that block and those after
141/// it are renumbered.
142void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
143 if (empty()) { MBBNumbering.clear(); return; }
144 MachineFunction::iterator MBBI, E = end();
145 if (MBB == 0)
146 MBBI = begin();
147 else
148 MBBI = MBB;
149
150 // Figure out the block number this should have.
151 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000152 if (MBBI != begin())
153 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000154
155 for (; MBBI != E; ++MBBI, ++BlockNo) {
156 if (MBBI->getNumber() != (int)BlockNo) {
157 // Remove use of the old number.
158 if (MBBI->getNumber() != -1) {
159 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
160 "MBB number mismatch!");
161 MBBNumbering[MBBI->getNumber()] = 0;
162 }
163
164 // If BlockNo is already taken, set that block's number to -1.
165 if (MBBNumbering[BlockNo])
166 MBBNumbering[BlockNo]->setNumber(-1);
167
168 MBBNumbering[BlockNo] = MBBI;
169 MBBI->setNumber(BlockNo);
170 }
171 }
172
173 // Okay, all the blocks are renumbered. If we have compactified the block
174 // numbering, shrink MBBNumbering now.
175 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
176 MBBNumbering.resize(BlockNo);
177}
178
179
Chris Lattner10491642002-10-30 00:48:05 +0000180void MachineFunction::dump() const { print(std::cerr); }
181
182void MachineFunction::print(std::ostream &OS) const {
Brian Gaeke47b71642004-03-29 21:58:31 +0000183 OS << "# Machine code for " << Fn->getName () << "():\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000184
185 // Print Frame Information
Chris Lattner9085d8a2003-01-16 18:35:57 +0000186 getFrameInfo()->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000187
188 // Print JumpTable Information
189 getJumpTableInfo()->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000190
191 // Print Constant Pool
192 getConstantPool()->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000193
194 const MRegisterInfo *MRI = getTarget().getRegisterInfo();
195
196 if (livein_begin() != livein_end()) {
197 OS << "Live Ins:";
198 for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I) {
199 if (MRI)
200 OS << " " << MRI->getName(I->first);
201 else
202 OS << " Reg #" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000203
204 if (I->second)
205 OS << " in VR#" << I->second << " ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000206 }
207 OS << "\n";
208 }
209 if (liveout_begin() != liveout_end()) {
210 OS << "Live Outs:";
211 for (liveout_iterator I = liveout_begin(), E = liveout_end(); I != E; ++I)
212 if (MRI)
213 OS << " " << MRI->getName(*I);
214 else
215 OS << " Reg #" << *I;
216 OS << "\n";
217 }
218
Brian Gaeke90421cd2004-02-13 04:39:55 +0000219 for (const_iterator BB = begin(); BB != end(); ++BB)
220 BB->print(OS);
Brian Gaeke47b71642004-03-29 21:58:31 +0000221
222 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000223}
224
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000225/// CFGOnly flag - This is used to control whether or not the CFG graph printer
226/// prints out the contents of basic blocks or not. This is acceptable because
227/// this code is only really used for debugging purposes.
228///
229static bool CFGOnly = false;
230
231namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000232 template<>
233 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
234 static std::string getGraphName(const MachineFunction *F) {
235 return "CFG for '" + F->getFunction()->getName() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000236 }
237
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000238 static std::string getNodeLabel(const MachineBasicBlock *Node,
239 const MachineFunction *Graph) {
240 if (CFGOnly && Node->getBasicBlock() &&
241 !Node->getBasicBlock()->getName().empty())
242 return Node->getBasicBlock()->getName() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000243
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000244 std::ostringstream Out;
245 if (CFGOnly) {
246 Out << Node->getNumber() << ':';
247 return Out.str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000248 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000249
250 Node->print(Out);
251
252 std::string OutStr = Out.str();
253 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
254
255 // Process string output to make it nicer...
256 for (unsigned i = 0; i != OutStr.length(); ++i)
257 if (OutStr[i] == '\n') { // Left justify
258 OutStr[i] = '\\';
259 OutStr.insert(OutStr.begin()+i+1, 'l');
260 }
261 return OutStr;
262 }
263 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000264}
265
266void MachineFunction::viewCFG() const
267{
Jim Laskey851a22d2005-10-12 12:09:05 +0000268#ifndef NDEBUG
Reid Spencer9d5b5322006-06-27 16:49:46 +0000269 ViewGraph(this, "mf" + getFunction()->getName());
270#else
271 std::cerr << "SelectionDAG::viewGraph is only available in debug builds on "
Jim Laskey851a22d2005-10-12 12:09:05 +0000272 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000273#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000274}
275
276void MachineFunction::viewCFGOnly() const
277{
278 CFGOnly = true;
279 viewCFG();
280 CFGOnly = false;
281}
282
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000283// The next two methods are used to construct and to retrieve
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000284// the MachineCodeForFunction object for the given function.
285// construct() -- Allocates and initializes for a given function and target
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000286// get() -- Returns a handle to the object.
287// This should not be called before "construct()"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000288// for a given Function.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000289//
Misha Brukmanfce11432002-10-28 00:28:31 +0000290MachineFunction&
Chris Lattner335d5c32002-10-28 05:58:46 +0000291MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000292{
Chris Lattnere316efc2002-10-29 23:18:43 +0000293 assert(Fn->getAnnotation(MF_AID) == 0 &&
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000294 "Object already exists for this function!");
Chris Lattner335d5c32002-10-28 05:58:46 +0000295 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
296 Fn->addAnnotation(mcInfo);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000297 return *mcInfo;
298}
299
Chris Lattner16c45e92003-12-20 10:20:58 +0000300void MachineFunction::destruct(const Function *Fn) {
Chris Lattnere316efc2002-10-29 23:18:43 +0000301 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000302 assert(Deleted && "Machine code did not exist for function!");
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000303}
304
Chris Lattner335d5c32002-10-28 05:58:46 +0000305MachineFunction& MachineFunction::get(const Function *F)
Vikram S. Adve89e2da02002-03-18 03:36:30 +0000306{
Chris Lattnere316efc2002-10-29 23:18:43 +0000307 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
Chris Lattnerf2868ce2002-02-03 07:54:50 +0000308 assert(mc && "Call construct() method first to allocate the object");
309 return *mc;
310}
311
Chris Lattner831fdcf2002-12-25 05:03:22 +0000312void MachineFunction::clearSSARegMap() {
313 delete SSARegMapping;
314 SSARegMapping = 0;
315}
316
Chris Lattner955fad12002-12-28 20:37:16 +0000317//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000318// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000319//===----------------------------------------------------------------------===//
320
Chris Lattner9085d8a2003-01-16 18:35:57 +0000321void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Chris Lattner62d6ad22004-06-02 05:56:52 +0000322 int ValOffset = MF.getTarget().getFrameInfo()->getOffsetOfLocalArea();
Chris Lattner9085d8a2003-01-16 18:35:57 +0000323
Chris Lattner955fad12002-12-28 20:37:16 +0000324 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
325 const StackObject &SO = Objects[i];
Chris Lattner0db07092005-05-13 22:54:44 +0000326 OS << " <fi #" << (int)(i-NumFixedObjects) << ">: ";
Chris Lattner955fad12002-12-28 20:37:16 +0000327 if (SO.Size == 0)
328 OS << "variable sized";
329 else
Chris Lattner0db07092005-05-13 22:54:44 +0000330 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
331 OS << " alignment is " << SO.Alignment << " byte"
332 << (SO.Alignment != 1 ? "s," : ",");
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000333
Chris Lattner955fad12002-12-28 20:37:16 +0000334 if (i < NumFixedObjects)
335 OS << " fixed";
336 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner7f7bbc22004-06-11 06:37:11 +0000337 int Off = SO.SPOffset - ValOffset;
Chris Lattner955fad12002-12-28 20:37:16 +0000338 OS << " at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000339 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000340 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000341 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000342 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000343 OS << "]";
344 }
345 OS << "\n";
346 }
347
348 if (HasVarSizedObjects)
349 OS << " Stack frame contains variable sized objects\n";
350}
351
Chris Lattner9085d8a2003-01-16 18:35:57 +0000352void MachineFrameInfo::dump(const MachineFunction &MF) const {
353 print(MF, std::cerr);
354}
Chris Lattner955fad12002-12-28 20:37:16 +0000355
356
357//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000358// MachineJumpTableInfo implementation
359//===----------------------------------------------------------------------===//
360
361/// getJumpTableIndex - Create a new jump table entry in the jump table info
362/// or return an existing one.
363///
364unsigned MachineJumpTableInfo::getJumpTableIndex(
365 std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000366 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000367 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
368 if (JumpTables[i].MBBs == DestBBs)
369 return i;
370
371 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
372 return JumpTables.size()-1;
373}
374
375
376void MachineJumpTableInfo::print(std::ostream &OS) const {
377 // FIXME: this is lame, maybe we could print out the MBB numbers or something
378 // like {1, 2, 4, 5, 3, 0}
379 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
380 OS << " <jt #" << i << "> has " << JumpTables[i].MBBs.size()
381 << " entries\n";
382 }
383}
384
Nate Begeman3700a4d2006-04-22 23:52:35 +0000385unsigned MachineJumpTableInfo::getEntrySize() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000386 return TD->getPointerSize();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000387}
388
389unsigned MachineJumpTableInfo::getAlignment() const {
Owen Andersona69571c2006-05-03 01:29:57 +0000390 return TD->getPointerAlignment();
Nate Begeman3700a4d2006-04-22 23:52:35 +0000391}
392
Nate Begeman37efe672006-04-22 18:53:45 +0000393void MachineJumpTableInfo::dump() const { print(std::cerr); }
394
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;
Chris Lattner3029f922006-02-09 04:46:04 +0000471 OS << " , offset=" << Constants[i].Offset;
Evan Chengb8973bd2006-01-31 22:23:14 +0000472 OS << "\n";
473 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000474}
475
476void MachineConstantPool::dump() const { print(std::cerr); }