blob: cacfed1d9f7b576fe833a82bcf18673ed3b8559b [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
Chris Lattner68433442009-04-13 05:44:34 +000038bool MachineFunctionPass::runOnFunction(Function &F) {
39 // Do not codegen any 'available_externally' functions at all, they have
40 // definitions outside the translation unit.
41 if (F.hasAvailableExternallyLinkage())
42 return false;
43
44 return runOnMachineFunction(MachineFunction::get(&F));
45}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47namespace {
48 struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
49 static char ID;
50
51 std::ostream *OS;
52 const std::string Banner;
53
Dan Gohman4ae08282008-07-21 19:48:15 +000054 Printer (std::ostream *os, const std::string &banner)
Dan Gohman26f8c272008-09-04 17:05:41 +000055 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
57 const char *getPassName() const { return "MachineFunction Printer"; }
58
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 }
62
63 bool runOnMachineFunction(MachineFunction &MF) {
64 (*OS) << Banner;
65 MF.print (*OS);
66 return false;
67 }
68 };
69 char Printer::ID = 0;
70}
71
72/// Returns a newly-created MachineFunction Printer pass. The default output
73/// stream is std::cerr; the default banner is empty.
74///
75FunctionPass *llvm::createMachineFunctionPrinterPass(std::ostream *OS,
76 const std::string &Banner){
77 return new Printer(OS, Banner);
78}
79
80namespace {
81 struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
82 static char ID;
Dan Gohman26f8c272008-09-04 17:05:41 +000083 Deleter() : MachineFunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084
85 const char *getPassName() const { return "Machine Code Deleter"; }
86
87 bool runOnMachineFunction(MachineFunction &MF) {
88 // Delete the annotation from the function now.
89 MachineFunction::destruct(MF.getFunction());
90 return true;
91 }
92 };
93 char Deleter::ID = 0;
94}
95
96/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
97/// the current function, which should happen after the function has been
98/// emitted to a .s file or to memory.
99FunctionPass *llvm::createMachineCodeDeleter() {
100 return new Deleter();
101}
102
103
104
105//===---------------------------------------------------------------------===//
106// MachineFunction implementation
107//===---------------------------------------------------------------------===//
108
Dan Gohman2fcbc7e2008-07-28 21:51:04 +0000109void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman221a4372008-07-07 23:14:23 +0000110 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111}
112
113MachineFunction::MachineFunction(const Function *F,
114 const TargetMachine &TM)
Owen Andersonf6064242009-05-14 19:17:24 +0000115 : Annotation(AnnotationManager::getID("CodeGen::MachineCodeForFunction")),
116 Fn(F), Target(TM) {
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000117 if (TM.getRegisterInfo())
118 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
119 MachineRegisterInfo(*TM.getRegisterInfo());
120 else
121 RegInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 MFInfo = 0;
Dan Gohman221a4372008-07-07 23:14:23 +0000123 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
124 MachineFrameInfo(*TM.getFrameInfo());
125 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
126 MachineConstantPool(TM.getTargetData());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127
128 // Set up jump table.
129 const TargetData &TD = *TM.getTargetData();
130 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
131 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
132 unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
133 : TD.getPointerABIAlignment();
Dan Gohman221a4372008-07-07 23:14:23 +0000134 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
135 MachineJumpTableInfo(EntrySize, Alignment);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136}
137
138MachineFunction::~MachineFunction() {
139 BasicBlocks.clear();
Dan Gohman221a4372008-07-07 23:14:23 +0000140 InstructionRecycler.clear(Allocator);
141 BasicBlockRecycler.clear(Allocator);
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000142 if (RegInfo)
143 RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
Dan Gohman221a4372008-07-07 23:14:23 +0000144 if (MFInfo) {
145 MFInfo->~MachineFunctionInfo(); Allocator.Deallocate(MFInfo);
146 }
147 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
148 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
149 JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150}
151
152
153/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
154/// recomputes them. This guarantees that the MBB numbers are sequential,
155/// dense, and match the ordering of the blocks within the function. If a
156/// specific MachineBasicBlock is specified, only that block and those after
157/// it are renumbered.
158void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
159 if (empty()) { MBBNumbering.clear(); return; }
160 MachineFunction::iterator MBBI, E = end();
161 if (MBB == 0)
162 MBBI = begin();
163 else
164 MBBI = MBB;
165
166 // Figure out the block number this should have.
167 unsigned BlockNo = 0;
168 if (MBBI != begin())
169 BlockNo = prior(MBBI)->getNumber()+1;
170
171 for (; MBBI != E; ++MBBI, ++BlockNo) {
172 if (MBBI->getNumber() != (int)BlockNo) {
173 // Remove use of the old number.
174 if (MBBI->getNumber() != -1) {
175 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
176 "MBB number mismatch!");
177 MBBNumbering[MBBI->getNumber()] = 0;
178 }
179
180 // If BlockNo is already taken, set that block's number to -1.
181 if (MBBNumbering[BlockNo])
182 MBBNumbering[BlockNo]->setNumber(-1);
183
184 MBBNumbering[BlockNo] = MBBI;
185 MBBI->setNumber(BlockNo);
186 }
187 }
188
189 // Okay, all the blocks are renumbered. If we have compactified the block
190 // numbering, shrink MBBNumbering now.
191 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
192 MBBNumbering.resize(BlockNo);
193}
194
Dan Gohman221a4372008-07-07 23:14:23 +0000195/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
196/// of `new MachineInstr'.
197///
198MachineInstr *
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000199MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
200 DebugLoc DL, bool NoImp) {
Dan Gohman221a4372008-07-07 23:14:23 +0000201 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000202 MachineInstr(TID, DL, NoImp);
Dan Gohman221a4372008-07-07 23:14:23 +0000203}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204
Dan Gohman221a4372008-07-07 23:14:23 +0000205/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
206/// 'Orig' instruction, identical in all ways except the the instruction
207/// has no parent, prev, or next.
208///
209MachineInstr *
210MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
211 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
212 MachineInstr(*this, *Orig);
213}
214
215/// DeleteMachineInstr - Delete the given MachineInstr.
216///
217void
218MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
219 // Clear the instructions memoperands. This must be done manually because
220 // the instruction's parent pointer is now null, so it can't properly
221 // deallocate them on its own.
222 MI->clearMemOperands(*this);
223
224 MI->~MachineInstr();
225 InstructionRecycler.Deallocate(Allocator, MI);
226}
227
228/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
229/// instead of `new MachineBasicBlock'.
230///
231MachineBasicBlock *
232MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
233 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
234 MachineBasicBlock(*this, bb);
235}
236
237/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
238///
239void
240MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
241 assert(MBB->getParent() == this && "MBB parent mismatch!");
242 MBB->~MachineBasicBlock();
243 BasicBlockRecycler.Deallocate(Allocator, MBB);
244}
245
Dan Gohman221a4372008-07-07 23:14:23 +0000246void MachineFunction::dump() const {
247 print(*cerr.stream());
248}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249
250void MachineFunction::print(std::ostream &OS) const {
251 OS << "# Machine code for " << Fn->getName () << "():\n";
252
253 // Print Frame Information
Dan Gohman221a4372008-07-07 23:14:23 +0000254 FrameInfo->print(*this, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255
256 // Print JumpTable Information
Dan Gohman221a4372008-07-07 23:14:23 +0000257 JumpTableInfo->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258
259 // Print Constant Pool
Chris Lattner491f7832008-08-23 22:53:13 +0000260 {
261 raw_os_ostream OSS(OS);
262 ConstantPool->print(OSS);
263 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
Dan Gohman1e57df32008-02-10 18:45:23 +0000265 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000267 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 OS << "Live Ins:";
Chris Lattner1b989192007-12-31 04:13:23 +0000269 for (MachineRegisterInfo::livein_iterator
270 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000271 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000272 OS << " " << TRI->getName(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 else
274 OS << " Reg #" << I->first;
275
276 if (I->second)
277 OS << " in VR#" << I->second << " ";
278 }
279 OS << "\n";
280 }
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000281 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 OS << "Live Outs:";
Chris Lattner1b989192007-12-31 04:13:23 +0000283 for (MachineRegisterInfo::liveout_iterator
284 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000285 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000286 OS << " " << TRI->getName(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 else
288 OS << " Reg #" << *I;
289 OS << "\n";
290 }
291
292 for (const_iterator BB = begin(); BB != end(); ++BB)
293 BB->print(OS);
294
295 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
296}
297
298/// CFGOnly flag - This is used to control whether or not the CFG graph printer
299/// prints out the contents of basic blocks or not. This is acceptable because
300/// this code is only really used for debugging purposes.
301///
302static bool CFGOnly = false;
303
304namespace llvm {
305 template<>
306 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
307 static std::string getGraphName(const MachineFunction *F) {
308 return "CFG for '" + F->getFunction()->getName() + "' function";
309 }
310
311 static std::string getNodeLabel(const MachineBasicBlock *Node,
312 const MachineFunction *Graph) {
313 if (CFGOnly && Node->getBasicBlock() &&
314 !Node->getBasicBlock()->getName().empty())
315 return Node->getBasicBlock()->getName() + ":";
316
317 std::ostringstream Out;
318 if (CFGOnly) {
319 Out << Node->getNumber() << ':';
320 return Out.str();
321 }
322
323 Node->print(Out);
324
325 std::string OutStr = Out.str();
326 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
327
328 // Process string output to make it nicer...
329 for (unsigned i = 0; i != OutStr.length(); ++i)
330 if (OutStr[i] == '\n') { // Left justify
331 OutStr[i] = '\\';
332 OutStr.insert(OutStr.begin()+i+1, 'l');
333 }
334 return OutStr;
335 }
336 };
337}
338
339void MachineFunction::viewCFG() const
340{
341#ifndef NDEBUG
342 ViewGraph(this, "mf" + getFunction()->getName());
343#else
344 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
345 << "systems with Graphviz or gv!\n";
346#endif // NDEBUG
347}
348
349void MachineFunction::viewCFGOnly() const
350{
351 CFGOnly = true;
352 viewCFG();
353 CFGOnly = false;
354}
355
356// The next two methods are used to construct and to retrieve
357// the MachineCodeForFunction object for the given function.
358// construct() -- Allocates and initializes for a given function and target
359// get() -- Returns a handle to the object.
360// This should not be called before "construct()"
361// for a given Function.
362//
363MachineFunction&
364MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
365{
Owen Andersonf6064242009-05-14 19:17:24 +0000366 AnnotationID MF_AID =
367 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 assert(Fn->getAnnotation(MF_AID) == 0 &&
369 "Object already exists for this function!");
370 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
371 Fn->addAnnotation(mcInfo);
372 return *mcInfo;
373}
374
375void MachineFunction::destruct(const Function *Fn) {
Owen Andersonf6064242009-05-14 19:17:24 +0000376 AnnotationID MF_AID =
377 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner845e05c2008-04-06 21:46:45 +0000379 assert(Deleted && "Machine code did not exist for function!");
380 Deleted = Deleted; // silence warning when no assertions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381}
382
383MachineFunction& MachineFunction::get(const Function *F)
384{
Owen Andersonf6064242009-05-14 19:17:24 +0000385 AnnotationID MF_AID =
386 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
388 assert(mc && "Call construct() method first to allocate the object");
389 return *mc;
390}
391
Bob Wilsonb6737aa2009-04-20 18:36:57 +0000392/// addLiveIn - Add the specified physical register as a live-in value and
393/// create a corresponding virtual register for it.
394unsigned MachineFunction::addLiveIn(unsigned PReg,
395 const TargetRegisterClass *RC) {
396 assert(RC->contains(PReg) && "Not the correct regclass!");
397 unsigned VReg = getRegInfo().createVirtualRegister(RC);
398 getRegInfo().addLiveIn(PReg, VReg);
399 return VReg;
400}
401
evanchengc611f1d2009-01-27 21:15:07 +0000402/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
Bill Wendling4756f3b2009-02-03 22:49:58 +0000403/// source file, line, and column. If none currently exists, create a new
404/// DebugLocTuple, and insert it into the DebugIdMap.
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +0000405unsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
406 unsigned Line, unsigned Col) {
Bill Wendlingbc121cc2009-05-21 00:04:55 +0000407 DebugLocTuple Tuple(CompileUnit, Line, Col);
evanchengc611f1d2009-01-27 21:15:07 +0000408 DenseMap<DebugLocTuple, unsigned>::iterator II
409 = DebugLocInfo.DebugIdMap.find(Tuple);
evancheng2dbde502009-01-26 07:41:49 +0000410 if (II != DebugLocInfo.DebugIdMap.end())
411 return II->second;
412 // Add a new tuple.
Evan Chenga40c3dd2009-01-26 23:47:30 +0000413 unsigned Id = DebugLocInfo.DebugLocations.size();
evancheng2dbde502009-01-26 07:41:49 +0000414 DebugLocInfo.DebugLocations.push_back(Tuple);
Evan Chenga40c3dd2009-01-26 23:47:30 +0000415 DebugLocInfo.DebugIdMap[Tuple] = Id;
416 return Id;
evancheng2dbde502009-01-26 07:41:49 +0000417}
418
Bill Wendling4756f3b2009-02-03 22:49:58 +0000419/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
Bill Wendlingacf95742009-02-04 00:05:34 +0000420DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
Bill Wendlingc6075092009-02-03 22:55:54 +0000421 unsigned Idx = DL.getIndex();
Bill Wendling4756f3b2009-02-03 22:49:58 +0000422 assert(Idx < DebugLocInfo.DebugLocations.size() &&
423 "Invalid index into debug locations!");
424 return DebugLocInfo.DebugLocations[Idx];
425}
426
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427//===----------------------------------------------------------------------===//
428// MachineFrameInfo implementation
429//===----------------------------------------------------------------------===//
430
Chris Lattner610b9eb2008-01-25 07:19:06 +0000431/// CreateFixedObject - Create a new object at a fixed location on the stack.
432/// All fixed objects should be created before other objects are created for
433/// efficiency. By default, fixed objects are immutable. This returns an
434/// index with a negative value.
435///
436int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
437 bool Immutable) {
438 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
439 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
440 return -++NumFixedObjects;
441}
442
443
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Matthijs Kooijman0e9ee532008-11-03 11:16:43 +0000445 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
446 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447
448 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
449 const StackObject &SO = Objects[i];
Dan Gohmand7430142008-10-15 02:57:38 +0000450 OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengda872532008-02-27 03:04:06 +0000451 if (SO.Size == ~0ULL) {
452 OS << "dead\n";
453 continue;
454 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 if (SO.Size == 0)
456 OS << "variable sized";
457 else
458 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
459 OS << " alignment is " << SO.Alignment << " byte"
460 << (SO.Alignment != 1 ? "s," : ",");
461
462 if (i < NumFixedObjects)
463 OS << " fixed";
464 if (i < NumFixedObjects || SO.SPOffset != -1) {
465 int64_t Off = SO.SPOffset - ValOffset;
466 OS << " at location [SP";
467 if (Off > 0)
468 OS << "+" << Off;
469 else if (Off < 0)
470 OS << Off;
471 OS << "]";
472 }
473 OS << "\n";
474 }
475
476 if (HasVarSizedObjects)
477 OS << " Stack frame contains variable sized objects\n";
478}
479
480void MachineFrameInfo::dump(const MachineFunction &MF) const {
481 print(MF, *cerr.stream());
482}
483
484
485//===----------------------------------------------------------------------===//
486// MachineJumpTableInfo implementation
487//===----------------------------------------------------------------------===//
488
489/// getJumpTableIndex - Create a new jump table entry in the jump table info
490/// or return an existing one.
491///
492unsigned MachineJumpTableInfo::getJumpTableIndex(
493 const std::vector<MachineBasicBlock*> &DestBBs) {
494 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
495 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
496 if (JumpTables[i].MBBs == DestBBs)
497 return i;
498
499 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
500 return JumpTables.size()-1;
501}
502
Dan Gohman352406d2009-04-15 01:18:49 +0000503/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
504/// the jump tables to branch to New instead.
505bool
506MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
507 MachineBasicBlock *New) {
508 assert(Old != New && "Not making a change?");
509 bool MadeChange = false;
510 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) {
511 MachineJumpTableEntry &JTE = JumpTables[i];
512 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
513 if (JTE.MBBs[j] == Old) {
514 JTE.MBBs[j] = New;
515 MadeChange = true;
516 }
517 }
518 return MadeChange;
519}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520
521void MachineJumpTableInfo::print(std::ostream &OS) const {
522 // FIXME: this is lame, maybe we could print out the MBB numbers or something
523 // like {1, 2, 4, 5, 3, 0}
524 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohmand7430142008-10-15 02:57:38 +0000525 OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 << " entries\n";
527 }
528}
529
530void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
531
532
533//===----------------------------------------------------------------------===//
534// MachineConstantPool implementation
535//===----------------------------------------------------------------------===//
536
537const Type *MachineConstantPoolEntry::getType() const {
538 if (isMachineConstantPoolEntry())
539 return Val.MachineCPVal->getType();
540 return Val.ConstVal->getType();
541}
542
543MachineConstantPool::~MachineConstantPool() {
544 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
545 if (Constants[i].isMachineConstantPoolEntry())
546 delete Constants[i].Val.MachineCPVal;
547}
548
549/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman62e7d9f2008-09-16 20:45:53 +0000550/// an existing one. User must specify the log2 of the minimum required
551/// alignment for the object.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552///
553unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
554 unsigned Alignment) {
555 assert(Alignment && "Alignment must be specified!");
556 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
557
558 // Check to see if we already have this constant.
559 //
560 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Cheng68c18682009-03-13 07:51:59 +0000562 if (Constants[i].Val.ConstVal == C &&
563 (Constants[i].getAlignment() & (Alignment - 1)) == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 return i;
565
Evan Cheng68c18682009-03-13 07:51:59 +0000566 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000567 return Constants.size()-1;
568}
569
570unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
571 unsigned Alignment) {
572 assert(Alignment && "Alignment must be specified!");
573 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
574
575 // Check to see if we already have this constant.
576 //
577 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578 int Idx = V->getExistingMachineCPValue(this, Alignment);
579 if (Idx != -1)
580 return (unsigned)Idx;
Evan Cheng68c18682009-03-13 07:51:59 +0000581
582 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583 return Constants.size()-1;
584}
585
Chris Lattner491f7832008-08-23 22:53:13 +0000586void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohmand7430142008-10-15 02:57:38 +0000588 OS << " <cp#" << i << "> is";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589 if (Constants[i].isMachineConstantPoolEntry())
590 Constants[i].Val.MachineCPVal->print(OS);
591 else
592 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Cheng68c18682009-03-13 07:51:59 +0000593 OS << " , alignment=" << Constants[i].getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000594 OS << "\n";
595 }
596}
597
Dan Gohmane7d7b0f2009-03-23 15:57:19 +0000598void MachineConstantPool::dump() const { print(errs()); }