blob: 2d2b59e2eced498e0847e70c70586b1cdf6d6858 [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());
Bill Wendling9624c612009-06-25 00:32:48 +0000127
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 // Set up jump table.
129 const TargetData &TD = *TM.getTargetData();
130 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
131 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Bill Wendling9624c612009-06-25 00:32:48 +0000132 unsigned TyAlignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
133 : TD.getPointerABIAlignment();
Dan Gohman221a4372008-07-07 23:14:23 +0000134 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
Bill Wendling9624c612009-06-25 00:32:48 +0000135 MachineJumpTableInfo(EntrySize, TyAlignment);
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);
Bill Wendling9624c612009-06-25 00:32:48 +0000142 if (RegInfo) {
143 RegInfo->~MachineRegisterInfo();
144 Allocator.Deallocate(RegInfo);
145 }
Dan Gohman221a4372008-07-07 23:14:23 +0000146 if (MFInfo) {
Bill Wendling9624c612009-06-25 00:32:48 +0000147 MFInfo->~MachineFunctionInfo();
148 Allocator.Deallocate(MFInfo);
Dan Gohman221a4372008-07-07 23:14:23 +0000149 }
150 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
151 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
152 JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153}
154
155
156/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
157/// recomputes them. This guarantees that the MBB numbers are sequential,
158/// dense, and match the ordering of the blocks within the function. If a
159/// specific MachineBasicBlock is specified, only that block and those after
160/// it are renumbered.
161void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
162 if (empty()) { MBBNumbering.clear(); return; }
163 MachineFunction::iterator MBBI, E = end();
164 if (MBB == 0)
165 MBBI = begin();
166 else
167 MBBI = MBB;
168
169 // Figure out the block number this should have.
170 unsigned BlockNo = 0;
171 if (MBBI != begin())
172 BlockNo = prior(MBBI)->getNumber()+1;
173
174 for (; MBBI != E; ++MBBI, ++BlockNo) {
175 if (MBBI->getNumber() != (int)BlockNo) {
176 // Remove use of the old number.
177 if (MBBI->getNumber() != -1) {
178 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
179 "MBB number mismatch!");
180 MBBNumbering[MBBI->getNumber()] = 0;
181 }
182
183 // If BlockNo is already taken, set that block's number to -1.
184 if (MBBNumbering[BlockNo])
185 MBBNumbering[BlockNo]->setNumber(-1);
186
187 MBBNumbering[BlockNo] = MBBI;
188 MBBI->setNumber(BlockNo);
189 }
190 }
191
192 // Okay, all the blocks are renumbered. If we have compactified the block
193 // numbering, shrink MBBNumbering now.
194 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
195 MBBNumbering.resize(BlockNo);
196}
197
Dan Gohman221a4372008-07-07 23:14:23 +0000198/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
199/// of `new MachineInstr'.
200///
201MachineInstr *
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000202MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
203 DebugLoc DL, bool NoImp) {
Dan Gohman221a4372008-07-07 23:14:23 +0000204 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000205 MachineInstr(TID, DL, NoImp);
Dan Gohman221a4372008-07-07 23:14:23 +0000206}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
Dan Gohman221a4372008-07-07 23:14:23 +0000208/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
209/// 'Orig' instruction, identical in all ways except the the instruction
210/// has no parent, prev, or next.
211///
212MachineInstr *
213MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
214 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
215 MachineInstr(*this, *Orig);
216}
217
218/// DeleteMachineInstr - Delete the given MachineInstr.
219///
220void
221MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
222 // Clear the instructions memoperands. This must be done manually because
223 // the instruction's parent pointer is now null, so it can't properly
224 // deallocate them on its own.
225 MI->clearMemOperands(*this);
226
227 MI->~MachineInstr();
228 InstructionRecycler.Deallocate(Allocator, MI);
229}
230
231/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
232/// instead of `new MachineBasicBlock'.
233///
234MachineBasicBlock *
235MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
236 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
237 MachineBasicBlock(*this, bb);
238}
239
240/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
241///
242void
243MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
244 assert(MBB->getParent() == this && "MBB parent mismatch!");
245 MBB->~MachineBasicBlock();
246 BasicBlockRecycler.Deallocate(Allocator, MBB);
247}
248
Dan Gohman221a4372008-07-07 23:14:23 +0000249void MachineFunction::dump() const {
250 print(*cerr.stream());
251}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252
253void MachineFunction::print(std::ostream &OS) const {
254 OS << "# Machine code for " << Fn->getName () << "():\n";
255
256 // Print Frame Information
Dan Gohman221a4372008-07-07 23:14:23 +0000257 FrameInfo->print(*this, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258
259 // Print JumpTable Information
Dan Gohman221a4372008-07-07 23:14:23 +0000260 JumpTableInfo->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261
262 // Print Constant Pool
Chris Lattner491f7832008-08-23 22:53:13 +0000263 {
264 raw_os_ostream OSS(OS);
265 ConstantPool->print(OSS);
266 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267
Dan Gohman1e57df32008-02-10 18:45:23 +0000268 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000270 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 OS << "Live Ins:";
Chris Lattner1b989192007-12-31 04:13:23 +0000272 for (MachineRegisterInfo::livein_iterator
273 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000274 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000275 OS << " " << TRI->getName(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 else
277 OS << " Reg #" << I->first;
278
279 if (I->second)
280 OS << " in VR#" << I->second << " ";
281 }
282 OS << "\n";
283 }
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000284 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 OS << "Live Outs:";
Chris Lattner1b989192007-12-31 04:13:23 +0000286 for (MachineRegisterInfo::liveout_iterator
287 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
Dan Gohman1e57df32008-02-10 18:45:23 +0000288 if (TRI)
Bill Wendling9b0baeb2008-02-26 21:47:57 +0000289 OS << " " << TRI->getName(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 else
291 OS << " Reg #" << *I;
292 OS << "\n";
293 }
294
295 for (const_iterator BB = begin(); BB != end(); ++BB)
296 BB->print(OS);
297
298 OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
299}
300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301namespace llvm {
302 template<>
303 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
304 static std::string getGraphName(const MachineFunction *F) {
305 return "CFG for '" + F->getFunction()->getName() + "' function";
306 }
307
308 static std::string getNodeLabel(const MachineBasicBlock *Node,
Owen Andersonf4a15462009-06-24 17:37:09 +0000309 const MachineFunction *Graph,
310 bool ShortNames) {
311 if (ShortNames && Node->getBasicBlock() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 !Node->getBasicBlock()->getName().empty())
313 return Node->getBasicBlock()->getName() + ":";
314
315 std::ostringstream Out;
Owen Andersonf4a15462009-06-24 17:37:09 +0000316 if (ShortNames) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317 Out << Node->getNumber() << ':';
318 return Out.str();
319 }
320
321 Node->print(Out);
322
323 std::string OutStr = Out.str();
324 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
325
326 // Process string output to make it nicer...
327 for (unsigned i = 0; i != OutStr.length(); ++i)
328 if (OutStr[i] == '\n') { // Left justify
329 OutStr[i] = '\\';
330 OutStr.insert(OutStr.begin()+i+1, 'l');
331 }
332 return OutStr;
333 }
334 };
335}
336
337void MachineFunction::viewCFG() const
338{
339#ifndef NDEBUG
340 ViewGraph(this, "mf" + getFunction()->getName());
341#else
342 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
343 << "systems with Graphviz or gv!\n";
344#endif // NDEBUG
345}
346
347void MachineFunction::viewCFGOnly() const
348{
Owen Andersonf4a15462009-06-24 17:37:09 +0000349#ifndef NDEBUG
350 ViewGraph(this, "mf" + getFunction()->getName(), true);
351#else
352 cerr << "SelectionDAG::viewGraph is only available in debug builds on "
353 << "systems with Graphviz or gv!\n";
354#endif // NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355}
356
357// The next two methods are used to construct and to retrieve
358// the MachineCodeForFunction object for the given function.
359// construct() -- Allocates and initializes for a given function and target
360// get() -- Returns a handle to the object.
361// This should not be called before "construct()"
362// for a given Function.
363//
364MachineFunction&
365MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
366{
Owen Andersonf6064242009-05-14 19:17:24 +0000367 AnnotationID MF_AID =
368 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 assert(Fn->getAnnotation(MF_AID) == 0 &&
370 "Object already exists for this function!");
371 MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
372 Fn->addAnnotation(mcInfo);
373 return *mcInfo;
374}
375
376void MachineFunction::destruct(const Function *Fn) {
Owen Andersonf6064242009-05-14 19:17:24 +0000377 AnnotationID MF_AID =
378 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 bool Deleted = Fn->deleteAnnotation(MF_AID);
Chris Lattner845e05c2008-04-06 21:46:45 +0000380 assert(Deleted && "Machine code did not exist for function!");
381 Deleted = Deleted; // silence warning when no assertions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382}
383
384MachineFunction& MachineFunction::get(const Function *F)
385{
Owen Andersonf6064242009-05-14 19:17:24 +0000386 AnnotationID MF_AID =
387 AnnotationManager::getID("CodeGen::MachineCodeForFunction");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
389 assert(mc && "Call construct() method first to allocate the object");
390 return *mc;
391}
392
Bob Wilsonb6737aa2009-04-20 18:36:57 +0000393/// addLiveIn - Add the specified physical register as a live-in value and
394/// create a corresponding virtual register for it.
395unsigned MachineFunction::addLiveIn(unsigned PReg,
396 const TargetRegisterClass *RC) {
397 assert(RC->contains(PReg) && "Not the correct regclass!");
398 unsigned VReg = getRegInfo().createVirtualRegister(RC);
399 getRegInfo().addLiveIn(PReg, VReg);
400 return VReg;
401}
402
evanchengc611f1d2009-01-27 21:15:07 +0000403/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
Bill Wendling4756f3b2009-02-03 22:49:58 +0000404/// source file, line, and column. If none currently exists, create a new
405/// DebugLocTuple, and insert it into the DebugIdMap.
Argiris Kirtzidis5b02f4c2009-04-30 23:22:31 +0000406unsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
407 unsigned Line, unsigned Col) {
Bill Wendlingbc121cc2009-05-21 00:04:55 +0000408 DebugLocTuple Tuple(CompileUnit, Line, Col);
evanchengc611f1d2009-01-27 21:15:07 +0000409 DenseMap<DebugLocTuple, unsigned>::iterator II
410 = DebugLocInfo.DebugIdMap.find(Tuple);
evancheng2dbde502009-01-26 07:41:49 +0000411 if (II != DebugLocInfo.DebugIdMap.end())
412 return II->second;
413 // Add a new tuple.
Evan Chenga40c3dd2009-01-26 23:47:30 +0000414 unsigned Id = DebugLocInfo.DebugLocations.size();
evancheng2dbde502009-01-26 07:41:49 +0000415 DebugLocInfo.DebugLocations.push_back(Tuple);
Evan Chenga40c3dd2009-01-26 23:47:30 +0000416 DebugLocInfo.DebugIdMap[Tuple] = Id;
417 return Id;
evancheng2dbde502009-01-26 07:41:49 +0000418}
419
Bill Wendling4756f3b2009-02-03 22:49:58 +0000420/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
Bill Wendlingacf95742009-02-04 00:05:34 +0000421DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
Bill Wendlingc6075092009-02-03 22:55:54 +0000422 unsigned Idx = DL.getIndex();
Bill Wendling4756f3b2009-02-03 22:49:58 +0000423 assert(Idx < DebugLocInfo.DebugLocations.size() &&
424 "Invalid index into debug locations!");
425 return DebugLocInfo.DebugLocations[Idx];
426}
427
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428//===----------------------------------------------------------------------===//
429// MachineFrameInfo implementation
430//===----------------------------------------------------------------------===//
431
Chris Lattner610b9eb2008-01-25 07:19:06 +0000432/// CreateFixedObject - Create a new object at a fixed location on the stack.
433/// All fixed objects should be created before other objects are created for
434/// efficiency. By default, fixed objects are immutable. This returns an
435/// index with a negative value.
436///
437int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
438 bool Immutable) {
439 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
440 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
441 return -++NumFixedObjects;
442}
443
444
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
Matthijs Kooijman0e9ee532008-11-03 11:16:43 +0000446 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
447 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448
449 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
450 const StackObject &SO = Objects[i];
Dan Gohmand7430142008-10-15 02:57:38 +0000451 OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
Evan Chengda872532008-02-27 03:04:06 +0000452 if (SO.Size == ~0ULL) {
453 OS << "dead\n";
454 continue;
455 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 if (SO.Size == 0)
457 OS << "variable sized";
458 else
459 OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
460 OS << " alignment is " << SO.Alignment << " byte"
461 << (SO.Alignment != 1 ? "s," : ",");
462
463 if (i < NumFixedObjects)
464 OS << " fixed";
465 if (i < NumFixedObjects || SO.SPOffset != -1) {
466 int64_t Off = SO.SPOffset - ValOffset;
467 OS << " at location [SP";
468 if (Off > 0)
469 OS << "+" << Off;
470 else if (Off < 0)
471 OS << Off;
472 OS << "]";
473 }
474 OS << "\n";
475 }
476
477 if (HasVarSizedObjects)
478 OS << " Stack frame contains variable sized objects\n";
479}
480
481void MachineFrameInfo::dump(const MachineFunction &MF) const {
482 print(MF, *cerr.stream());
483}
484
485
486//===----------------------------------------------------------------------===//
487// MachineJumpTableInfo implementation
488//===----------------------------------------------------------------------===//
489
490/// getJumpTableIndex - Create a new jump table entry in the jump table info
491/// or return an existing one.
492///
493unsigned MachineJumpTableInfo::getJumpTableIndex(
494 const std::vector<MachineBasicBlock*> &DestBBs) {
495 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
496 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
497 if (JumpTables[i].MBBs == DestBBs)
498 return i;
499
500 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
501 return JumpTables.size()-1;
502}
503
Dan Gohman352406d2009-04-15 01:18:49 +0000504/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
505/// the jump tables to branch to New instead.
506bool
507MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
508 MachineBasicBlock *New) {
509 assert(Old != New && "Not making a change?");
510 bool MadeChange = false;
511 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) {
512 MachineJumpTableEntry &JTE = JumpTables[i];
513 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
514 if (JTE.MBBs[j] == Old) {
515 JTE.MBBs[j] = New;
516 MadeChange = true;
517 }
518 }
519 return MadeChange;
520}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521
522void MachineJumpTableInfo::print(std::ostream &OS) const {
523 // FIXME: this is lame, maybe we could print out the MBB numbers or something
524 // like {1, 2, 4, 5, 3, 0}
525 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohmand7430142008-10-15 02:57:38 +0000526 OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000527 << " entries\n";
528 }
529}
530
531void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
532
533
534//===----------------------------------------------------------------------===//
535// MachineConstantPool implementation
536//===----------------------------------------------------------------------===//
537
538const Type *MachineConstantPoolEntry::getType() const {
539 if (isMachineConstantPoolEntry())
540 return Val.MachineCPVal->getType();
541 return Val.ConstVal->getType();
542}
543
544MachineConstantPool::~MachineConstantPool() {
545 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
546 if (Constants[i].isMachineConstantPoolEntry())
547 delete Constants[i].Val.MachineCPVal;
548}
549
550/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman62e7d9f2008-09-16 20:45:53 +0000551/// an existing one. User must specify the log2 of the minimum required
552/// alignment for the object.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000553///
554unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
555 unsigned Alignment) {
556 assert(Alignment && "Alignment must be specified!");
557 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
558
559 // Check to see if we already have this constant.
560 //
561 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000562 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Evan Cheng68c18682009-03-13 07:51:59 +0000563 if (Constants[i].Val.ConstVal == C &&
564 (Constants[i].getAlignment() & (Alignment - 1)) == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 return i;
566
Evan Cheng68c18682009-03-13 07:51:59 +0000567 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 return Constants.size()-1;
569}
570
571unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
572 unsigned Alignment) {
573 assert(Alignment && "Alignment must be specified!");
574 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
575
576 // Check to see if we already have this constant.
577 //
578 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 int Idx = V->getExistingMachineCPValue(this, Alignment);
580 if (Idx != -1)
581 return (unsigned)Idx;
Evan Cheng68c18682009-03-13 07:51:59 +0000582
583 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 return Constants.size()-1;
585}
586
Chris Lattner491f7832008-08-23 22:53:13 +0000587void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohmand7430142008-10-15 02:57:38 +0000589 OS << " <cp#" << i << "> is";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590 if (Constants[i].isMachineConstantPoolEntry())
591 Constants[i].Val.MachineCPVal->print(OS);
592 else
593 OS << *(Value*)Constants[i].Val.ConstVal;
Evan Cheng68c18682009-03-13 07:51:59 +0000594 OS << " , alignment=" << Constants[i].getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 OS << "\n";
596 }
597}
598
Dan Gohmane7d7b0f2009-03-23 15:57:19 +0000599void MachineConstantPool::dump() const { print(errs()); }