blob: bea0445b154f45f200c22614d900a8af5e14df53 [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"
Bill Wendling25a8ae32009-06-30 22:38:32 +000017#include "llvm/Function.h"
18#include "llvm/Instructions.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/Config/config.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greeneb214eb92009-08-19 22:16:11 +000022#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b989192007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineInstr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/Passes.h"
Devang Patelc2103b12010-01-19 06:09:04 +000029#include "llvm/Analysis/DebugInfo.h"
David Greene869dddf2010-01-04 23:39:17 +000030#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Target/TargetData.h"
Bill Wendling25a8ae32009-06-30 22:38:32 +000032#include "llvm/Target/TargetLowering.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035#include "llvm/Support/GraphWriter.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000036#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037using namespace llvm;
38
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000040 struct Printer : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 static char ID;
42
Chris Lattner0bde4e32009-08-23 03:13:20 +000043 raw_ostream &OS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 const std::string Banner;
45
Chris Lattner0bde4e32009-08-23 03:13:20 +000046 Printer(raw_ostream &os, const std::string &banner)
Dan Gohman26f8c272008-09-04 17:05:41 +000047 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048
49 const char *getPassName() const { return "MachineFunction Printer"; }
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000053 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 }
55
56 bool runOnMachineFunction(MachineFunction &MF) {
Dan Gohman57b31652009-10-31 20:19:03 +000057 OS << "# " << Banner << ":\n";
Chris Lattner0bde4e32009-08-23 03:13:20 +000058 MF.print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 return false;
60 }
61 };
62 char Printer::ID = 0;
63}
64
Daniel Dunbar0a17f3d2009-08-03 01:02:24 +000065/// Returns a newly-created MachineFunction Printer pass. The default banner is
66/// empty.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067///
Chris Lattner0bde4e32009-08-23 03:13:20 +000068FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 const std::string &Banner){
70 return new Printer(OS, Banner);
71}
72
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073//===---------------------------------------------------------------------===//
74// MachineFunction implementation
75//===---------------------------------------------------------------------===//
76
Chris Lattner72ba6722009-09-15 22:44:26 +000077// Out of line virtual method.
78MachineFunctionInfo::~MachineFunctionInfo() {}
79
Dan Gohman2fcbc7e2008-07-28 21:51:04 +000080void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman221a4372008-07-07 23:14:23 +000081 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082}
83
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000084MachineFunction::MachineFunction(Function *F,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 const TargetMachine &TM)
Dan Gohman58ccf8d2009-07-31 18:35:51 +000086 : Fn(F), Target(TM) {
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +000087 if (TM.getRegisterInfo())
88 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
89 MachineRegisterInfo(*TM.getRegisterInfo());
90 else
91 RegInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 MFInfo = 0;
Dan Gohman221a4372008-07-07 23:14:23 +000093 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
94 MachineFrameInfo(*TM.getFrameInfo());
95 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
96 MachineConstantPool(TM.getTargetData());
Bill Wendling25a8ae32009-06-30 22:38:32 +000097 Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
Bill Wendling9624c612009-06-25 00:32:48 +000098
Chris Lattner1d196bc2010-01-25 23:26:13 +000099 JumpTableInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
102MachineFunction::~MachineFunction() {
103 BasicBlocks.clear();
Dan Gohman221a4372008-07-07 23:14:23 +0000104 InstructionRecycler.clear(Allocator);
105 BasicBlockRecycler.clear(Allocator);
Bill Wendling9624c612009-06-25 00:32:48 +0000106 if (RegInfo) {
107 RegInfo->~MachineRegisterInfo();
108 Allocator.Deallocate(RegInfo);
109 }
Dan Gohman221a4372008-07-07 23:14:23 +0000110 if (MFInfo) {
Bill Wendling9624c612009-06-25 00:32:48 +0000111 MFInfo->~MachineFunctionInfo();
112 Allocator.Deallocate(MFInfo);
Dan Gohman221a4372008-07-07 23:14:23 +0000113 }
114 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
115 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
Chris Lattner1d196bc2010-01-25 23:26:13 +0000116
117 if (JumpTableInfo) {
118 JumpTableInfo->~MachineJumpTableInfo();
119 Allocator.Deallocate(JumpTableInfo);
120 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121}
122
Chris Lattner1d196bc2010-01-25 23:26:13 +0000123/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
124/// does already exist, allocate one.
125MachineJumpTableInfo *MachineFunction::
126getOrCreateJumpTableInfo(unsigned EntryKind) {
127 if (JumpTableInfo) return JumpTableInfo;
128
129 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
130 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
131 return JumpTableInfo;
132}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
134/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
135/// recomputes them. This guarantees that the MBB numbers are sequential,
136/// dense, and match the ordering of the blocks within the function. If a
137/// specific MachineBasicBlock is specified, only that block and those after
138/// it are renumbered.
139void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
140 if (empty()) { MBBNumbering.clear(); return; }
141 MachineFunction::iterator MBBI, E = end();
142 if (MBB == 0)
143 MBBI = begin();
144 else
145 MBBI = MBB;
146
147 // Figure out the block number this should have.
148 unsigned BlockNo = 0;
149 if (MBBI != begin())
150 BlockNo = prior(MBBI)->getNumber()+1;
151
152 for (; MBBI != E; ++MBBI, ++BlockNo) {
153 if (MBBI->getNumber() != (int)BlockNo) {
154 // Remove use of the old number.
155 if (MBBI->getNumber() != -1) {
156 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
157 "MBB number mismatch!");
158 MBBNumbering[MBBI->getNumber()] = 0;
159 }
160
161 // If BlockNo is already taken, set that block's number to -1.
162 if (MBBNumbering[BlockNo])
163 MBBNumbering[BlockNo]->setNumber(-1);
164
165 MBBNumbering[BlockNo] = MBBI;
166 MBBI->setNumber(BlockNo);
167 }
168 }
169
170 // Okay, all the blocks are renumbered. If we have compactified the block
171 // numbering, shrink MBBNumbering now.
172 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
173 MBBNumbering.resize(BlockNo);
174}
175
Dan Gohman221a4372008-07-07 23:14:23 +0000176/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
177/// of `new MachineInstr'.
178///
179MachineInstr *
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000180MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
181 DebugLoc DL, bool NoImp) {
Dan Gohman221a4372008-07-07 23:14:23 +0000182 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000183 MachineInstr(TID, DL, NoImp);
Dan Gohman221a4372008-07-07 23:14:23 +0000184}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
Dan Gohman221a4372008-07-07 23:14:23 +0000186/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
187/// 'Orig' instruction, identical in all ways except the the instruction
188/// has no parent, prev, or next.
189///
190MachineInstr *
191MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
192 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
193 MachineInstr(*this, *Orig);
194}
195
196/// DeleteMachineInstr - Delete the given MachineInstr.
197///
198void
199MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
Dan Gohman221a4372008-07-07 23:14:23 +0000200 MI->~MachineInstr();
201 InstructionRecycler.Deallocate(Allocator, MI);
202}
203
204/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
205/// instead of `new MachineBasicBlock'.
206///
207MachineBasicBlock *
208MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
209 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
210 MachineBasicBlock(*this, bb);
211}
212
213/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
214///
215void
216MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
217 assert(MBB->getParent() == this && "MBB parent mismatch!");
218 MBB->~MachineBasicBlock();
219 BasicBlockRecycler.Deallocate(Allocator, MBB);
220}
221
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000222MachineMemOperand *
223MachineFunction::getMachineMemOperand(const Value *v, unsigned f,
224 int64_t o, uint64_t s,
225 unsigned base_alignment) {
226 return new (Allocator.Allocate<MachineMemOperand>())
227 MachineMemOperand(v, f, o, s, base_alignment);
228}
229
230MachineMemOperand *
231MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
232 int64_t Offset, uint64_t Size) {
233 return new (Allocator.Allocate<MachineMemOperand>())
234 MachineMemOperand(MMO->getValue(), MMO->getFlags(),
235 int64_t(uint64_t(MMO->getOffset()) +
236 uint64_t(Offset)),
237 Size, MMO->getBaseAlignment());
238}
239
240MachineInstr::mmo_iterator
241MachineFunction::allocateMemRefsArray(unsigned long Num) {
242 return Allocator.Allocate<MachineMemOperand *>(Num);
243}
244
Dan Gohmanc7973eb2009-10-09 18:10:05 +0000245std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
246MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
247 MachineInstr::mmo_iterator End) {
248 // Count the number of load mem refs.
249 unsigned Num = 0;
250 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
251 if ((*I)->isLoad())
252 ++Num;
253
254 // Allocate a new array and populate it with the load information.
255 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
256 unsigned Index = 0;
257 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
258 if ((*I)->isLoad()) {
259 if (!(*I)->isStore())
260 // Reuse the MMO.
261 Result[Index] = *I;
262 else {
263 // Clone the MMO and unset the store flag.
264 MachineMemOperand *JustLoad =
265 getMachineMemOperand((*I)->getValue(),
266 (*I)->getFlags() & ~MachineMemOperand::MOStore,
267 (*I)->getOffset(), (*I)->getSize(),
268 (*I)->getBaseAlignment());
269 Result[Index] = JustLoad;
270 }
271 ++Index;
272 }
273 }
274 return std::make_pair(Result, Result + Num);
275}
276
277std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
278MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
279 MachineInstr::mmo_iterator End) {
280 // Count the number of load mem refs.
281 unsigned Num = 0;
282 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
283 if ((*I)->isStore())
284 ++Num;
285
286 // Allocate a new array and populate it with the store information.
287 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
288 unsigned Index = 0;
289 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
290 if ((*I)->isStore()) {
291 if (!(*I)->isLoad())
292 // Reuse the MMO.
293 Result[Index] = *I;
294 else {
295 // Clone the MMO and unset the load flag.
296 MachineMemOperand *JustStore =
297 getMachineMemOperand((*I)->getValue(),
298 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
299 (*I)->getOffset(), (*I)->getSize(),
300 (*I)->getBaseAlignment());
301 Result[Index] = JustStore;
302 }
303 ++Index;
304 }
305 }
306 return std::make_pair(Result, Result + Num);
307}
308
Dan Gohman221a4372008-07-07 23:14:23 +0000309void MachineFunction::dump() const {
David Greene869dddf2010-01-04 23:39:17 +0000310 print(dbgs());
Dan Gohman221a4372008-07-07 23:14:23 +0000311}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312
Chris Lattnerd7397752009-08-23 01:12:47 +0000313void MachineFunction::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000314 OS << "# Machine code for function " << Fn->getName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
316 // Print Frame Information
Dan Gohman221a4372008-07-07 23:14:23 +0000317 FrameInfo->print(*this, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318
319 // Print JumpTable Information
Chris Lattner1d196bc2010-01-25 23:26:13 +0000320 if (JumpTableInfo)
321 JumpTableInfo->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
323 // Print Constant Pool
Chris Lattnerd7397752009-08-23 01:12:47 +0000324 ConstantPool->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
Dan Gohman1e57df32008-02-10 18:45:23 +0000326 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000328 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +0000329 OS << "Function Live Ins: ";
Chris Lattner1b989192007-12-31 04:13:23 +0000330 for (MachineRegisterInfo::livein_iterator
331 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000332 if (TRI)
Dan Gohman57b31652009-10-31 20:19:03 +0000333 OS << "%" << TRI->getName(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 else
Dan Gohman57b31652009-10-31 20:19:03 +0000335 OS << " %physreg" << I->first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336
337 if (I->second)
Dan Gohman57b31652009-10-31 20:19:03 +0000338 OS << " in reg%" << I->second;
339
Chris Lattnerb44b4292009-12-03 00:50:42 +0000340 if (llvm::next(I) != E)
Dan Gohman57b31652009-10-31 20:19:03 +0000341 OS << ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 }
Chris Lattnerd7397752009-08-23 01:12:47 +0000343 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 }
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000345 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +0000346 OS << "Function Live Outs: ";
Chris Lattner1b989192007-12-31 04:13:23 +0000347 for (MachineRegisterInfo::liveout_iterator
Dan Gohman57b31652009-10-31 20:19:03 +0000348 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
Dan Gohman1e57df32008-02-10 18:45:23 +0000349 if (TRI)
Dan Gohman57b31652009-10-31 20:19:03 +0000350 OS << '%' << TRI->getName(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 else
Dan Gohman57b31652009-10-31 20:19:03 +0000352 OS << "%physreg" << *I;
353
Chris Lattnerb44b4292009-12-03 00:50:42 +0000354 if (llvm::next(I) != E)
Dan Gohman57b31652009-10-31 20:19:03 +0000355 OS << " ";
356 }
Chris Lattnerd7397752009-08-23 01:12:47 +0000357 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 }
359
Dan Gohman57b31652009-10-31 20:19:03 +0000360 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
361 OS << '\n';
Chris Lattnerb3fff702009-08-23 00:47:04 +0000362 BB->print(OS);
Dan Gohman57b31652009-10-31 20:19:03 +0000363 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364
Dan Gohman57b31652009-10-31 20:19:03 +0000365 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366}
367
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368namespace llvm {
369 template<>
370 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossere2c3aec2009-11-30 12:38:13 +0000371
372 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 static std::string getGraphName(const MachineFunction *F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000375 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 }
377
Tobias Grosser810b18c2009-11-30 12:38:47 +0000378 std::string getNodeLabel(const MachineBasicBlock *Node,
379 const MachineFunction *Graph) {
380 if (isSimple () && Node->getBasicBlock() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 !Node->getBasicBlock()->getName().empty())
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000382 return Node->getBasicBlock()->getNameStr() + ":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383
Chris Lattner0bde4e32009-08-23 03:13:20 +0000384 std::string OutStr;
385 {
386 raw_string_ostream OSS(OutStr);
387
Tobias Grosser810b18c2009-11-30 12:38:47 +0000388 if (isSimple())
Chris Lattner0bde4e32009-08-23 03:13:20 +0000389 OSS << Node->getNumber() << ':';
390 else
391 Node->print(OSS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 }
393
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
395
396 // Process string output to make it nicer...
397 for (unsigned i = 0; i != OutStr.length(); ++i)
398 if (OutStr[i] == '\n') { // Left justify
399 OutStr[i] = '\\';
400 OutStr.insert(OutStr.begin()+i+1, 'l');
401 }
402 return OutStr;
403 }
404 };
405}
406
407void MachineFunction::viewCFG() const
408{
409#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000410 ViewGraph(this, "mf" + getFunction()->getNameStr());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000412 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
413 << "systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414#endif // NDEBUG
415}
416
417void MachineFunction::viewCFGOnly() const
418{
Owen Andersonf4a15462009-06-24 17:37:09 +0000419#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000420 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
Owen Andersonf4a15462009-06-24 17:37:09 +0000421#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000422 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
423 << "systems with Graphviz or gv!\n";
Owen Andersonf4a15462009-06-24 17:37:09 +0000424#endif // NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000425}
426
Bob Wilsonb6737aa2009-04-20 18:36:57 +0000427/// addLiveIn - Add the specified physical register as a live-in value and
428/// create a corresponding virtual register for it.
429unsigned MachineFunction::addLiveIn(unsigned PReg,
430 const TargetRegisterClass *RC) {
431 assert(RC->contains(PReg) && "Not the correct regclass!");
432 unsigned VReg = getRegInfo().createVirtualRegister(RC);
433 getRegInfo().addLiveIn(PReg, VReg);
434 return VReg;
435}
436
Devang Patel042945f2010-01-16 06:09:35 +0000437/// getDILocation - Get the DILocation for a given DebugLoc object.
438DILocation MachineFunction::getDILocation(DebugLoc DL) const {
Bill Wendlingc6075092009-02-03 22:55:54 +0000439 unsigned Idx = DL.getIndex();
Bill Wendling4756f3b2009-02-03 22:49:58 +0000440 assert(Idx < DebugLocInfo.DebugLocations.size() &&
441 "Invalid index into debug locations!");
Devang Patel042945f2010-01-16 06:09:35 +0000442 return DILocation(DebugLocInfo.DebugLocations[Idx]);
Bill Wendling4756f3b2009-02-03 22:49:58 +0000443}
444
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000445//===----------------------------------------------------------------------===//
446// MachineFrameInfo implementation
447//===----------------------------------------------------------------------===//
448
Chris Lattner610b9eb2008-01-25 07:19:06 +0000449/// CreateFixedObject - Create a new object at a fixed location on the stack.
450/// All fixed objects should be created before other objects are created for
451/// efficiency. By default, fixed objects are immutable. This returns an
452/// index with a negative value.
453///
454int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
David Greene6424ab92009-11-12 20:49:22 +0000455 bool Immutable, bool isSS) {
Chris Lattner610b9eb2008-01-25 07:19:06 +0000456 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
David Greene6424ab92009-11-12 20:49:22 +0000457 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
458 isSS));
Chris Lattner610b9eb2008-01-25 07:19:06 +0000459 return -++NumFixedObjects;
460}
461
462
Jakob Stoklund Olesene7b072a2009-08-13 16:19:33 +0000463BitVector
464MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
465 assert(MBB && "MBB must be valid");
466 const MachineFunction *MF = MBB->getParent();
467 assert(MF && "MBB must be part of a MachineFunction");
468 const TargetMachine &TM = MF->getTarget();
469 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
470 BitVector BV(TRI->getNumRegs());
471
472 // Before CSI is calculated, no registers are considered pristine. They can be
473 // freely used and PEI will make sure they are saved.
474 if (!isCalleeSavedInfoValid())
475 return BV;
476
477 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
478 BV.set(*CSR);
479
480 // The entry MBB always has all CSRs pristine.
481 if (MBB == &MF->front())
482 return BV;
483
484 // On other MBBs the saved CSRs are not pristine.
485 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
486 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
487 E = CSI.end(); I != E; ++I)
488 BV.reset(I->getReg());
489
490 return BV;
491}
492
493
Chris Lattnerd7397752009-08-23 01:12:47 +0000494void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman57b31652009-10-31 20:19:03 +0000495 if (Objects.empty()) return;
496
Matthijs Kooijman0e9ee532008-11-03 11:16:43 +0000497 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
498 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499
Dan Gohman57b31652009-10-31 20:19:03 +0000500 OS << "Frame Objects:\n";
501
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
503 const StackObject &SO = Objects[i];
Dan Gohman57b31652009-10-31 20:19:03 +0000504 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengda872532008-02-27 03:04:06 +0000505 if (SO.Size == ~0ULL) {
506 OS << "dead\n";
507 continue;
508 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 if (SO.Size == 0)
510 OS << "variable sized";
511 else
Dan Gohman57b31652009-10-31 20:19:03 +0000512 OS << "size=" << SO.Size;
513 OS << ", align=" << SO.Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000514
515 if (i < NumFixedObjects)
Dan Gohman57b31652009-10-31 20:19:03 +0000516 OS << ", fixed";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 if (i < NumFixedObjects || SO.SPOffset != -1) {
518 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman57b31652009-10-31 20:19:03 +0000519 OS << ", at location [SP";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 if (Off > 0)
521 OS << "+" << Off;
522 else if (Off < 0)
523 OS << Off;
524 OS << "]";
525 }
526 OS << "\n";
527 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528}
529
530void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greene869dddf2010-01-04 23:39:17 +0000531 print(MF, dbgs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532}
533
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534//===----------------------------------------------------------------------===//
535// MachineJumpTableInfo implementation
536//===----------------------------------------------------------------------===//
537
Chris Lattner1d196bc2010-01-25 23:26:13 +0000538/// getEntrySize - Return the size of each entry in the jump table.
539unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
540 // The size of a jump table entry is 4 bytes unless the entry is just the
541 // address of a block, in which case it is the pointer size.
542 switch (getEntryKind()) {
543 case MachineJumpTableInfo::EK_BlockAddress:
544 return TD.getPointerSize();
545 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
546 case MachineJumpTableInfo::EK_LabelDifference32:
547 return 4;
548 }
549 assert(0 && "Unknown jump table encoding!");
550 return ~0;
551}
552
553/// getEntryAlignment - Return the alignment of each entry in the jump table.
554unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
555 // The alignment of a jump table entry is the alignment of int32 unless the
556 // entry is just the address of a block, in which case it is the pointer
557 // alignment.
558 switch (getEntryKind()) {
559 case MachineJumpTableInfo::EK_BlockAddress:
560 return TD.getPointerABIAlignment();
561 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
562 case MachineJumpTableInfo::EK_LabelDifference32:
563 return TD.getABIIntegerTypeAlignment(32);
564 }
565 assert(0 && "Unknown jump table encoding!");
566 return ~0;
567}
568
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000569/// getJumpTableIndex - Create a new jump table entry in the jump table info
570/// or return an existing one.
571///
572unsigned MachineJumpTableInfo::getJumpTableIndex(
573 const std::vector<MachineBasicBlock*> &DestBBs) {
574 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000575 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
576 return JumpTables.size()-1;
577}
578
Dan Gohman352406d2009-04-15 01:18:49 +0000579/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
580/// the jump tables to branch to New instead.
581bool
582MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
583 MachineBasicBlock *New) {
584 assert(Old != New && "Not making a change?");
585 bool MadeChange = false;
Jim Grosbach47387c52009-11-14 20:09:13 +0000586 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
587 ReplaceMBBInJumpTable(i, Old, New);
588 return MadeChange;
589}
590
591/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
592/// the jump table to branch to New instead.
593bool
594MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
595 MachineBasicBlock *Old,
596 MachineBasicBlock *New) {
597 assert(Old != New && "Not making a change?");
598 bool MadeChange = false;
599 MachineJumpTableEntry &JTE = JumpTables[Idx];
600 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
601 if (JTE.MBBs[j] == Old) {
602 JTE.MBBs[j] = New;
603 MadeChange = true;
604 }
Dan Gohman352406d2009-04-15 01:18:49 +0000605 return MadeChange;
606}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000607
Chris Lattnerd7397752009-08-23 01:12:47 +0000608void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000609 if (JumpTables.empty()) return;
610
611 OS << "Jump Tables:\n";
612
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000613 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman57b31652009-10-31 20:19:03 +0000614 OS << " jt#" << i << ": ";
615 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
616 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000617 }
Dan Gohman57b31652009-10-31 20:19:03 +0000618
619 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000620}
621
David Greene869dddf2010-01-04 23:39:17 +0000622void MachineJumpTableInfo::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000623
624
625//===----------------------------------------------------------------------===//
626// MachineConstantPool implementation
627//===----------------------------------------------------------------------===//
628
629const Type *MachineConstantPoolEntry::getType() const {
630 if (isMachineConstantPoolEntry())
Chris Lattner8f3f8512009-07-21 23:34:23 +0000631 return Val.MachineCPVal->getType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632 return Val.ConstVal->getType();
633}
634
Chris Lattner8f3f8512009-07-21 23:34:23 +0000635
Chris Lattner690b8d52009-07-21 23:36:01 +0000636unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattner8f3f8512009-07-21 23:34:23 +0000637 if (isMachineConstantPoolEntry())
Chris Lattner690b8d52009-07-21 23:36:01 +0000638 return Val.MachineCPVal->getRelocationInfo();
Chris Lattnerbdb23b32009-07-22 00:05:44 +0000639 return Val.ConstVal->getRelocationInfo();
Chris Lattner8f3f8512009-07-21 23:34:23 +0000640}
641
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642MachineConstantPool::~MachineConstantPool() {
643 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
644 if (Constants[i].isMachineConstantPoolEntry())
645 delete Constants[i].Val.MachineCPVal;
646}
647
Dan Gohman114924d2009-10-28 01:12:16 +0000648/// CanShareConstantPoolEntry - Test whether the given two constants
649/// can be allocated the same constant pool entry.
650static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
651 const TargetData *TD) {
652 // Handle the trivial case quickly.
653 if (A == B) return true;
654
655 // If they have the same type but weren't the same constant, quickly
656 // reject them.
657 if (A->getType() == B->getType()) return false;
658
659 // For now, only support constants with the same size.
660 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
661 return false;
662
663 // If a floating-point value and an integer value have the same encoding,
664 // they can share a constant-pool entry.
665 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
666 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
667 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
668 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
669 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
670 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
671
672 // Two vectors can share an entry if each pair of corresponding
673 // elements could.
674 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
675 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
676 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
677 return false;
678 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
Dan Gohman509fccf2009-10-31 14:12:53 +0000679 if (!CanShareConstantPoolEntry(AV->getOperand(i),
680 BV->getOperand(i), TD))
Dan Gohman114924d2009-10-28 01:12:16 +0000681 return false;
682 return true;
683 }
684
685 // TODO: Handle other cases.
686
687 return false;
688}
689
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000690/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman62e7d9f2008-09-16 20:45:53 +0000691/// an existing one. User must specify the log2 of the minimum required
692/// alignment for the object.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000693///
694unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
695 unsigned Alignment) {
696 assert(Alignment && "Alignment must be specified!");
697 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman114924d2009-10-28 01:12:16 +0000698
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000699 // Check to see if we already have this constant.
700 //
701 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman114924d2009-10-28 01:12:16 +0000703 if (!Constants[i].isMachineConstantPoolEntry() &&
704 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
705 if ((unsigned)Constants[i].getAlignment() < Alignment)
706 Constants[i].Alignment = Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 return i;
Dan Gohman114924d2009-10-28 01:12:16 +0000708 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000709
Evan Cheng68c18682009-03-13 07:51:59 +0000710 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000711 return Constants.size()-1;
712}
713
714unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
715 unsigned Alignment) {
716 assert(Alignment && "Alignment must be specified!");
717 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
718
719 // Check to see if we already have this constant.
720 //
721 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000722 int Idx = V->getExistingMachineCPValue(this, Alignment);
723 if (Idx != -1)
724 return (unsigned)Idx;
Evan Cheng68c18682009-03-13 07:51:59 +0000725
726 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 return Constants.size()-1;
728}
729
Chris Lattner491f7832008-08-23 22:53:13 +0000730void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000731 if (Constants.empty()) return;
732
733 OS << "Constant Pool:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman57b31652009-10-31 20:19:03 +0000735 OS << " cp#" << i << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 if (Constants[i].isMachineConstantPoolEntry())
737 Constants[i].Val.MachineCPVal->print(OS);
738 else
739 OS << *(Value*)Constants[i].Val.ConstVal;
Dan Gohman57b31652009-10-31 20:19:03 +0000740 OS << ", align=" << Constants[i].getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741 OS << "\n";
742 }
743}
744
David Greene869dddf2010-01-04 23:39:17 +0000745void MachineConstantPool::dump() const { print(dbgs()); }