blob: 434034b888f04af14163def35df34cd275bc2edf [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// 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//===----------------------------------------------------------------------===//
15
16#include "llvm/DerivedTypes.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner1b989192007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineInstr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/Passes.h"
24#include "llvm/Target/TargetData.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Target/TargetMachine.h"
26#include "llvm/Target/TargetFrameInfo.h"
27#include "llvm/Function.h"
28#include "llvm/Instructions.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/GraphWriter.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000031#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/ADT/STLExtras.h"
33#include "llvm/Config/config.h"
34#include <fstream>
35#include <sstream>
36using namespace llvm;
37
38static AnnotationID MF_AID(
39 AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
40
41// Out of line virtual function to home classes.
42void MachineFunctionPass::virtfn() {}
43
44namespace {
45 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
46 static char ID;
47
48 std::ostream *OS;
49 const std::string Banner;
50
Dan Gohman4ae08282008-07-21 19:48:15 +000051 Printer (std::ostream *os, const std::string &banner)
Dan Gohman26f8c272008-09-04 17:05:41 +000052 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54 const char *getPassName() const { return "MachineFunction Printer"; }
55
56 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesAll();
58 }
59
60 bool runOnMachineFunction(MachineFunction &MF) {
61 (*OS) << Banner;
62 MF.print (*OS);
63 return false;
64 }
65 };
66 char Printer::ID = 0;
67}
68
69/// Returns a newly-created MachineFunction Printer pass. The default output
70/// stream is std::cerr; the default banner is empty.
71///
72FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
73 const std::string &Banner){
74 return new Printer(OS, Banner);
75}
76
77namespace {
78 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
79 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +000080 Deleter() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
82 const char *getPassName() const { return "Machine Code Deleter"; }
83
84 bool runOnMachineFunction(MachineFunction &MF) {
85 // Delete the annotation from the function now.
86 MachineFunction::destruct(MF.getFunction());
87 return true;
88 }
89 };
90 char Deleter::ID = 0;
91}
92
93/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
94/// the current function, which should happen after the function has been
95/// emitted to a .s file or to memory.
96FunctionPass *llvm::createMachineCodeDeleter() {
97 return new Deleter();
98}
99
100
101
102//===---------------------------------------------------------------------===//
103// MachineFunction implementation
104//===---------------------------------------------------------------------===//
105
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000106void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman221a4372008-07-07 23:14:23 +0000107 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108}
109
110MachineFunction::MachineFunction(const Function *F,
111 const TargetMachine &TM)
112 : Annotation(MF_AID), Fn(F), Target(TM) {
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000113 if (TM.getRegisterInfo())
114 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
115 MachineRegisterInfo(*TM.getRegisterInfo());
116 else
117 RegInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 MFInfo = 0;
Dan Gohman221a4372008-07-07 23:14:23 +0000119 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
120 MachineFrameInfo(*TM.getFrameInfo());
121 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
122 MachineConstantPool(TM.getTargetData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
124 // Set up jump table.
125 const TargetData &TD = *TM.getTargetData();
126 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
127 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
128 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
129 : TD.getPointerABIAlignment();
Dan Gohman221a4372008-07-07 23:14:23 +0000130 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
131 MachineJumpTableInfo(EntrySize, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132}
133
134MachineFunction::~MachineFunction() {
135 BasicBlocks.clear();
Dan Gohman221a4372008-07-07 23:14:23 +0000136 InstructionRecycler.clear(Allocator);
137 BasicBlockRecycler.clear(Allocator);
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000138 if (RegInfo)
139 RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
Dan Gohman221a4372008-07-07 23:14:23 +0000140 if (MFInfo) {
141 MFInfo->~MachineFunctionInfo(); Allocator.Deallocate(MFInfo);
142 }
143 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
144 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
145 JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146}
147
148
149/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
150/// recomputes them. This guarantees that the MBB numbers are sequential,
151/// dense, and match the ordering of the blocks within the function. If a
152/// specific MachineBasicBlock is specified, only that block and those after
153/// it are renumbered.
154void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
155 if (empty()) { MBBNumbering.clear(); return; }
156 MachineFunction::iterator MBBI, E = end();
157 if (MBB == 0)
158 MBBI = begin();
159 else
160 MBBI = MBB;
161
162 // Figure out the block number this should have.
163 unsigned BlockNo = 0;
164 if (MBBI != begin())
165 BlockNo = prior(MBBI)->getNumber()+1;
166
167 for (; MBBI != E; ++MBBI, ++BlockNo) {
168 if (MBBI->getNumber() != (int)BlockNo) {
169 // Remove use of the old number.
170 if (MBBI->getNumber() != -1) {
171 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
172 "MBB number mismatch!");
173 MBBNumbering[MBBI->getNumber()] = 0;
174 }
175
176 // If BlockNo is already taken, set that block's number to -1.
177 if (MBBNumbering[BlockNo])
178 MBBNumbering[BlockNo]->setNumber(-1);
179
180 MBBNumbering[BlockNo] = MBBI;
181 MBBI->setNumber(BlockNo);
182 }
183 }
184
185 // Okay, all the blocks are renumbered. If we have compactified the block
186 // numbering, shrink MBBNumbering now.
187 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
188 MBBNumbering.resize(BlockNo);
189}
190
Dan Gohman221a4372008-07-07 23:14:23 +0000191/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
192/// of `new MachineInstr'.
193///
194MachineInstr *
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000195MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
196 DebugLoc DL, bool NoImp) {
Dan Gohman221a4372008-07-07 23:14:23 +0000197 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000198 MachineInstr(TID, DL, NoImp);
Dan Gohman221a4372008-07-07 23:14:23 +0000199}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200
Dan Gohman221a4372008-07-07 23:14:23 +0000201/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
202/// 'Orig' instruction, identical in all ways except the the instruction
203/// has no parent, prev, or next.
204///
205MachineInstr *
206MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
207 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
208 MachineInstr(*this, *Orig);
209}
210
211/// DeleteMachineInstr - Delete the given MachineInstr.
212///
213void
214MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
215 // Clear the instructions memoperands. This must be done manually because
216 // the instruction's parent pointer is now null, so it can't properly
217 // deallocate them on its own.
218 MI->clearMemOperands(*this);
219
220 MI->~MachineInstr();
221 InstructionRecycler.Deallocate(Allocator, MI);
222}
223
224/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
225/// instead of `new MachineBasicBlock'.
226///
227MachineBasicBlock *
228MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
229 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
230 MachineBasicBlock(*this, bb);
231}
232
233/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
234///
235void
236MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
237 assert(MBB->getParent() == this && "MBB parent mismatch!");
238 MBB->~MachineBasicBlock();
239 BasicBlockRecycler.Deallocate(Allocator, MBB);
240}
241
Dan Gohman221a4372008-07-07 23:14:23 +0000242void MachineFunction::dump() const {
243 print(*cerr.stream());
244}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245
246void MachineFunction::print(std::ostream &OS) const {
247 OS << "# Machine code for " << Fn->getName () << "():\n";
248
249 // Print Frame Information
Dan Gohman221a4372008-07-07 23:14:23 +0000250 FrameInfo->print(*this, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251
252 // Print JumpTable Information
Dan Gohman221a4372008-07-07 23:14:23 +0000253 JumpTableInfo->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254
255 // Print Constant Pool
Chris Lattner491f7832008-08-23 22:53:13 +0000256 {
257 raw_os_ostream OSS(OS);
258 ConstantPool->print(OSS);
259 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260
Dan Gohman1e57df32008-02-10 18:45:23 +0000261 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000263 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 OS << "Live Ins:";
Chris Lattner1b989192007-12-31 04:13:23 +0000265 for (MachineRegisterInfo::livein_iterator
266 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000267 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000268 OS << " " << TRI->getName(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 else
270 OS << " Reg #" << I->first;
271
272 if (I->second)
273 OS << " in VR#" << I->second << " ";
274 }
275 OS << "\n";
276 }
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000277 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 OS << "Live Outs:";
Chris Lattner1b989192007-12-31 04:13:23 +0000279 for (MachineRegisterInfo::liveout_iterator
280 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000281 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000282 OS << " " << TRI->getName(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 else
284 OS << " Reg #" << *I;
285 OS << "\n";
286 }
287
288 for (const_iterator BB = begin(); BB != end(); ++BB)
289 BB->print(OS);
290
291 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
292}
293
294/// CFGOnly flag - This is used to control whether or not the CFG graph printer
295/// prints out the contents of basic blocks or not. This is acceptable because
296/// this code is only really used for debugging purposes.
297///
298static bool CFGOnly = false;
299
300namespace llvm {
301 template<>
302 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
303 static std::string getGraphName(const MachineFunction *F) {
304 return "CFG for '" + F->getFunction()->getName() + "' function";
305 }
306
307 static std::string getNodeLabel(const MachineBasicBlock *Node,
308 const MachineFunction *Graph) {
309 if (CFGOnly && Node->getBasicBlock() &&
310 !Node->getBasicBlock()->getName().empty())
311 return Node->getBasicBlock()->getName() + ":";
312
313 std::ostringstream Out;
314 if (CFGOnly) {
315 Out << Node->getNumber() << ':';
316 return Out.str();
317 }
318
319 Node->print(Out);
320
321 std::string OutStr = Out.str();
322 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
323
324 // Process string output to make it nicer...
325 for (unsigned i = 0; i != OutStr.length(); ++i)
326 if (OutStr[i] == '\n') { // Left justify
327 OutStr[i] = '\\';
328 OutStr.insert(OutStr.begin()+i+1, 'l');
329 }
330 return OutStr;
331 }
332 };
333}
334
335void MachineFunction::viewCFG() const
336{
337#ifndef NDEBUG
338 ViewGraph(this, "mf" + getFunction()->getName());
339#else
340 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
341 << "systems with Graphviz or gv!\n";
342#endif // NDEBUG
343}
344
345void MachineFunction::viewCFGOnly() const
346{
347 CFGOnly = true;
348 viewCFG();
349 CFGOnly = false;
350}
351
352// The next two methods are used to construct and to retrieve
353// the MachineCodeForFunction object for the given function.
354// construct() -- Allocates and initializes for a given function and target
355// get() -- Returns a handle to the object.
356// This should not be called before "construct()"
357// for a given Function.
358//
359MachineFunction&
360MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
361{
362 assert(Fn->getAnnotation(MF_AID) == 0 &&
363 "Object already exists for this function!");
364 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
365 Fn->addAnnotation(mcInfo);
366 return *mcInfo;
367}
368
369void MachineFunction::destruct(const Function *Fn) {
370 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner845e05c2008-04-06 21:46:45 +0000371 assert(Deleted && "Machine code did not exist for function!");
372 Deleted = Deleted; // silence warning when no assertions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000373}
374
375MachineFunction& MachineFunction::get(const Function *F)
376{
377 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
378 assert(mc && "Call construct() method first to allocate the object");
379 return *mc;
380}
381
evanchengc611f1d2009-01-27 21:15:07 +0000382/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
383/// source file, line, and column. If none currently exists, create add a new
384/// new DebugLocTuple and insert it into the DebugIdMap.
385unsigned MachineFunction::getOrCreateDebugLocID(unsigned Src, unsigned Line,
386 unsigned Col) {
evancheng4e4168f2009-01-26 07:53:42 +0000387 struct DebugLocTuple Tuple(Src, Line, Col);
evanchengc611f1d2009-01-27 21:15:07 +0000388 DenseMap<DebugLocTuple, unsigned>::iterator II
389 = DebugLocInfo.DebugIdMap.find(Tuple);
evancheng2dbde502009-01-26 07:41:49 +0000390 if (II != DebugLocInfo.DebugIdMap.end())
391 return II->second;
392 // Add a new tuple.
Evan Chenga40c3dd2009-01-26 23:47:30 +0000393 unsigned Id = DebugLocInfo.DebugLocations.size();
evancheng2dbde502009-01-26 07:41:49 +0000394 DebugLocInfo.DebugLocations.push_back(Tuple);
Evan Chenga40c3dd2009-01-26 23:47:30 +0000395 DebugLocInfo.DebugIdMap[Tuple] = Id;
396 return Id;
evancheng2dbde502009-01-26 07:41:49 +0000397}
398
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399//===----------------------------------------------------------------------===//
400// MachineFrameInfo implementation
401//===----------------------------------------------------------------------===//
402
Chris Lattner610b9eb2008-01-25 07:19:06 +0000403/// CreateFixedObject - Create a new object at a fixed location on the stack.
404/// All fixed objects should be created before other objects are created for
405/// efficiency. By default, fixed objects are immutable. This returns an
406/// index with a negative value.
407///
408int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
409 bool Immutable) {
410 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
411 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
412 return -++NumFixedObjects;
413}
414
415
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Matthijs Kooijman0e9ee532008-11-03 11:16:43 +0000417 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
418 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419
420 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
421 const StackObject &SO = Objects[i];
Dan Gohmand7430142008-10-15 02:57:38 +0000422 OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengda872532008-02-27 03:04:06 +0000423 if (SO.Size == ~0ULL) {
424 OS << "dead\n";
425 continue;
426 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 if (SO.Size == 0)
428 OS << "variable sized";
429 else
430 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
431 OS << " alignment is " << SO.Alignment << " byte"
432 << (SO.Alignment != 1 ? "s," : ",");
433
434 if (i < NumFixedObjects)
435 OS << " fixed";
436 if (i < NumFixedObjects || SO.SPOffset != -1) {
437 int64_t Off = SO.SPOffset - ValOffset;
438 OS << " at location [SP";
439 if (Off > 0)
440 OS << "+" << Off;
441 else if (Off < 0)
442 OS << Off;
443 OS << "]";
444 }
445 OS << "\n";
446 }
447
448 if (HasVarSizedObjects)
449 OS << " Stack frame contains variable sized objects\n";
450}
451
452void MachineFrameInfo::dump(const MachineFunction &MF) const {
453 print(MF, *cerr.stream());
454}
455
456
457//===----------------------------------------------------------------------===//
458// MachineJumpTableInfo implementation
459//===----------------------------------------------------------------------===//
460
461/// getJumpTableIndex - Create a new jump table entry in the jump table info
462/// or return an existing one.
463///
464unsigned MachineJumpTableInfo::getJumpTableIndex(
465 const std::vector<MachineBasicBlock*> &DestBBs) {
466 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
467 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
468 if (JumpTables[i].MBBs == DestBBs)
469 return i;
470
471 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
472 return JumpTables.size()-1;
473}
474
475
476void MachineJumpTableInfo::print(std::ostream &OS) const {
477 // FIXME: this is lame, maybe we could print out the MBB numbers or something
478 // like {1, 2, 4, 5, 3, 0}
479 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohmand7430142008-10-15 02:57:38 +0000480 OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000481 << " entries\n";
482 }
483}
484
485void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
486
487
488//===----------------------------------------------------------------------===//
489// MachineConstantPool implementation
490//===----------------------------------------------------------------------===//
491
492const Type *MachineConstantPoolEntry::getType() const {
493 if (isMachineConstantPoolEntry())
494 return Val.MachineCPVal->getType();
495 return Val.ConstVal->getType();
496}
497
498MachineConstantPool::~MachineConstantPool() {
499 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
500 if (Constants[i].isMachineConstantPoolEntry())
501 delete Constants[i].Val.MachineCPVal;
502}
503
504/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman62e7d9f2008-09-16 20:45:53 +0000505/// an existing one. User must specify the log2 of the minimum required
506/// alignment for the object.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000507///
508unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
509 unsigned Alignment) {
510 assert(Alignment && "Alignment must be specified!");
511 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
512
513 // Check to see if we already have this constant.
514 //
515 // FIXME, this could be made much more efficient for large constant pools.
516 unsigned AlignMask = (1 << Alignment)-1;
517 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
518 if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
519 return i;
520
521 unsigned Offset = 0;
522 if (!Constants.empty()) {
523 Offset = Constants.back().getOffset();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000524 Offset += TD->getTypePaddedSize(Constants.back().getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 Offset = (Offset+AlignMask)&~AlignMask;
526 }
527
528 Constants.push_back(MachineConstantPoolEntry(C, Offset));
529 return Constants.size()-1;
530}
531
532unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
533 unsigned Alignment) {
534 assert(Alignment && "Alignment must be specified!");
535 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
536
537 // Check to see if we already have this constant.
538 //
539 // FIXME, this could be made much more efficient for large constant pools.
540 unsigned AlignMask = (1 << Alignment)-1;
541 int Idx = V->getExistingMachineCPValue(this, Alignment);
542 if (Idx != -1)
543 return (unsigned)Idx;
544
545 unsigned Offset = 0;
546 if (!Constants.empty()) {
547 Offset = Constants.back().getOffset();
Duncan Sandsd68f13b2009-01-12 20:38:59 +0000548 Offset += TD->getTypePaddedSize(Constants.back().getType());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000549 Offset = (Offset+AlignMask)&~AlignMask;
550 }
551
552 Constants.push_back(MachineConstantPoolEntry(V, Offset));
553 return Constants.size()-1;
554}
555
Chris Lattner491f7832008-08-23 22:53:13 +0000556void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohmand7430142008-10-15 02:57:38 +0000558 OS << " <cp#" << i << "> is";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559 if (Constants[i].isMachineConstantPoolEntry())
560 Constants[i].Val.MachineCPVal->print(OS);
561 else
562 OS << *(Value*)Constants[i].Val.ConstVal;
563 OS << " , offset=" << Constants[i].getOffset();
564 OS << "\n";
565 }
566}
567
Chris Lattner491f7832008-08-23 22:53:13 +0000568void MachineConstantPool::dump() const { print(errs()); errs().flush(); }