blob: deb639d542fbfd95e618584807a9d6c60c1a8aa3 [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"
Bill Wendling25a8ae32009-06-30 22:38:32 +000019#include "llvm/Config/config.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greeneb214eb92009-08-19 22:16:11 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineInstr.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027#include "llvm/CodeGen/Passes.h"
Chris Lattner978b9772010-01-26 05:58:28 +000028#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCContext.h"
Devang Patelc2103b12010-01-19 06:09:04 +000030#include "llvm/Analysis/DebugInfo.h"
David Greene869dddf2010-01-04 23:39:17 +000031#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Target/TargetData.h"
Bill Wendling25a8ae32009-06-30 22:38:32 +000033#include "llvm/Target/TargetLowering.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Target/TargetMachine.h"
35#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner978b9772010-01-26 05:58:28 +000036#include "llvm/ADT/SmallString.h"
37#include "llvm/ADT/STLExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Support/GraphWriter.h"
Chris Lattner1fefaac2008-08-23 22:23:09 +000039#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040using namespace llvm;
41
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000043 struct Printer : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 static char ID;
45
Chris Lattner0bde4e32009-08-23 03:13:20 +000046 raw_ostream &OS;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047 const std::string Banner;
48
Chris Lattner0bde4e32009-08-23 03:13:20 +000049 Printer(raw_ostream &os, const std::string &banner)
Dan Gohman26f8c272008-09-04 17:05:41 +000050 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 const char *getPassName() const { return "MachineFunction Printer"; }
53
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.setPreservesAll();
Dan Gohmanfdf9ee22009-07-31 18:16:33 +000056 MachineFunctionPass::getAnalysisUsage(AU);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 }
58
59 bool runOnMachineFunction(MachineFunction &MF) {
Dan Gohman57b31652009-10-31 20:19:03 +000060 OS << "# " << Banner << ":\n";
Chris Lattner0bde4e32009-08-23 03:13:20 +000061 MF.print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 return false;
63 }
64 };
65 char Printer::ID = 0;
66}
67
Daniel Dunbar0a17f3d2009-08-03 01:02:24 +000068/// Returns a newly-created MachineFunction Printer pass. The default banner is
69/// empty.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070///
Chris Lattner0bde4e32009-08-23 03:13:20 +000071FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 const std::string &Banner){
73 return new Printer(OS, Banner);
74}
75
Chris Lattner01462ba2010-01-26 04:35:26 +000076//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077// MachineFunction implementation
Chris Lattner01462ba2010-01-26 04:35:26 +000078//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079
Chris Lattner72ba6722009-09-15 22:44:26 +000080// Out of line virtual method.
81MachineFunctionInfo::~MachineFunctionInfo() {}
82
Dan Gohman2fcbc7e2008-07-28 21:51:04 +000083void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman221a4372008-07-07 23:14:23 +000084 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085}
86
Chris Lattner01462ba2010-01-26 04:35:26 +000087MachineFunction::MachineFunction(Function *F, const TargetMachine &TM,
88 unsigned FunctionNum)
Dan Gohman58ccf8d2009-07-31 18:35:51 +000089 : Fn(F), Target(TM) {
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +000090 if (TM.getRegisterInfo())
91 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
92 MachineRegisterInfo(*TM.getRegisterInfo());
93 else
94 RegInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 MFInfo = 0;
Dan Gohman221a4372008-07-07 23:14:23 +000096 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
97 MachineFrameInfo(*TM.getFrameInfo());
98 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
99 MachineConstantPool(TM.getTargetData());
Bill Wendling25a8ae32009-06-30 22:38:32 +0000100 Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
Chris Lattner01462ba2010-01-26 04:35:26 +0000101 FunctionNumber = FunctionNum;
Chris Lattner1d196bc2010-01-25 23:26:13 +0000102 JumpTableInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103}
104
105MachineFunction::~MachineFunction() {
106 BasicBlocks.clear();
Dan Gohman221a4372008-07-07 23:14:23 +0000107 InstructionRecycler.clear(Allocator);
108 BasicBlockRecycler.clear(Allocator);
Bill Wendling9624c612009-06-25 00:32:48 +0000109 if (RegInfo) {
110 RegInfo->~MachineRegisterInfo();
111 Allocator.Deallocate(RegInfo);
112 }
Dan Gohman221a4372008-07-07 23:14:23 +0000113 if (MFInfo) {
Bill Wendling9624c612009-06-25 00:32:48 +0000114 MFInfo->~MachineFunctionInfo();
115 Allocator.Deallocate(MFInfo);
Dan Gohman221a4372008-07-07 23:14:23 +0000116 }
117 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
118 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
Chris Lattner1d196bc2010-01-25 23:26:13 +0000119
120 if (JumpTableInfo) {
121 JumpTableInfo->~MachineJumpTableInfo();
122 Allocator.Deallocate(JumpTableInfo);
123 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124}
125
Chris Lattner1d196bc2010-01-25 23:26:13 +0000126/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
127/// does already exist, allocate one.
128MachineJumpTableInfo *MachineFunction::
129getOrCreateJumpTableInfo(unsigned EntryKind) {
130 if (JumpTableInfo) return JumpTableInfo;
131
132 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
133 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
134 return JumpTableInfo;
135}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136
137/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
138/// recomputes them. This guarantees that the MBB numbers are sequential,
139/// dense, and match the ordering of the blocks within the function. If a
140/// specific MachineBasicBlock is specified, only that block and those after
141/// it are renumbered.
142void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
143 if (empty()) { MBBNumbering.clear(); return; }
144 MachineFunction::iterator MBBI, E = end();
145 if (MBB == 0)
146 MBBI = begin();
147 else
148 MBBI = MBB;
149
150 // Figure out the block number this should have.
151 unsigned BlockNo = 0;
152 if (MBBI != begin())
153 BlockNo = prior(MBBI)->getNumber()+1;
154
155 for (; MBBI != E; ++MBBI, ++BlockNo) {
156 if (MBBI->getNumber() != (int)BlockNo) {
157 // Remove use of the old number.
158 if (MBBI->getNumber() != -1) {
159 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
160 "MBB number mismatch!");
161 MBBNumbering[MBBI->getNumber()] = 0;
162 }
163
164 // If BlockNo is already taken, set that block's number to -1.
165 if (MBBNumbering[BlockNo])
166 MBBNumbering[BlockNo]->setNumber(-1);
167
168 MBBNumbering[BlockNo] = MBBI;
169 MBBI->setNumber(BlockNo);
170 }
171 }
172
173 // Okay, all the blocks are renumbered. If we have compactified the block
174 // numbering, shrink MBBNumbering now.
175 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
176 MBBNumbering.resize(BlockNo);
177}
178
Dan Gohman221a4372008-07-07 23:14:23 +0000179/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
180/// of `new MachineInstr'.
181///
182MachineInstr *
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000183MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
184 DebugLoc DL, bool NoImp) {
Dan Gohman221a4372008-07-07 23:14:23 +0000185 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling5aa0ddb2009-02-03 00:55:04 +0000186 MachineInstr(TID, DL, NoImp);
Dan Gohman221a4372008-07-07 23:14:23 +0000187}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188
Dan Gohman221a4372008-07-07 23:14:23 +0000189/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
190/// 'Orig' instruction, identical in all ways except the the instruction
191/// has no parent, prev, or next.
192///
193MachineInstr *
194MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
195 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
196 MachineInstr(*this, *Orig);
197}
198
199/// DeleteMachineInstr - Delete the given MachineInstr.
200///
201void
202MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
Dan Gohman221a4372008-07-07 23:14:23 +0000203 MI->~MachineInstr();
204 InstructionRecycler.Deallocate(Allocator, MI);
205}
206
207/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
208/// instead of `new MachineBasicBlock'.
209///
210MachineBasicBlock *
211MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
212 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
213 MachineBasicBlock(*this, bb);
214}
215
216/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
217///
218void
219MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
220 assert(MBB->getParent() == this && "MBB parent mismatch!");
221 MBB->~MachineBasicBlock();
222 BasicBlockRecycler.Deallocate(Allocator, MBB);
223}
224
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000225MachineMemOperand *
226MachineFunction::getMachineMemOperand(const Value *v, unsigned f,
227 int64_t o, uint64_t s,
228 unsigned base_alignment) {
229 return new (Allocator.Allocate<MachineMemOperand>())
230 MachineMemOperand(v, f, o, s, base_alignment);
231}
232
233MachineMemOperand *
234MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
235 int64_t Offset, uint64_t Size) {
236 return new (Allocator.Allocate<MachineMemOperand>())
237 MachineMemOperand(MMO->getValue(), MMO->getFlags(),
238 int64_t(uint64_t(MMO->getOffset()) +
239 uint64_t(Offset)),
240 Size, MMO->getBaseAlignment());
241}
242
243MachineInstr::mmo_iterator
244MachineFunction::allocateMemRefsArray(unsigned long Num) {
245 return Allocator.Allocate<MachineMemOperand *>(Num);
246}
247
Dan Gohmanc7973eb2009-10-09 18:10:05 +0000248std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
249MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
250 MachineInstr::mmo_iterator End) {
251 // Count the number of load mem refs.
252 unsigned Num = 0;
253 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
254 if ((*I)->isLoad())
255 ++Num;
256
257 // Allocate a new array and populate it with the load information.
258 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
259 unsigned Index = 0;
260 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
261 if ((*I)->isLoad()) {
262 if (!(*I)->isStore())
263 // Reuse the MMO.
264 Result[Index] = *I;
265 else {
266 // Clone the MMO and unset the store flag.
267 MachineMemOperand *JustLoad =
268 getMachineMemOperand((*I)->getValue(),
269 (*I)->getFlags() & ~MachineMemOperand::MOStore,
270 (*I)->getOffset(), (*I)->getSize(),
271 (*I)->getBaseAlignment());
272 Result[Index] = JustLoad;
273 }
274 ++Index;
275 }
276 }
277 return std::make_pair(Result, Result + Num);
278}
279
280std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
281MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
282 MachineInstr::mmo_iterator End) {
283 // Count the number of load mem refs.
284 unsigned Num = 0;
285 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
286 if ((*I)->isStore())
287 ++Num;
288
289 // Allocate a new array and populate it with the store information.
290 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
291 unsigned Index = 0;
292 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
293 if ((*I)->isStore()) {
294 if (!(*I)->isLoad())
295 // Reuse the MMO.
296 Result[Index] = *I;
297 else {
298 // Clone the MMO and unset the load flag.
299 MachineMemOperand *JustStore =
300 getMachineMemOperand((*I)->getValue(),
301 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
302 (*I)->getOffset(), (*I)->getSize(),
303 (*I)->getBaseAlignment());
304 Result[Index] = JustStore;
305 }
306 ++Index;
307 }
308 }
309 return std::make_pair(Result, Result + Num);
310}
311
Dan Gohman221a4372008-07-07 23:14:23 +0000312void MachineFunction::dump() const {
David Greene869dddf2010-01-04 23:39:17 +0000313 print(dbgs());
Dan Gohman221a4372008-07-07 23:14:23 +0000314}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
Chris Lattnerd7397752009-08-23 01:12:47 +0000316void MachineFunction::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000317 OS << "# Machine code for function " << Fn->getName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318
319 // Print Frame Information
Dan Gohman221a4372008-07-07 23:14:23 +0000320 FrameInfo->print(*this, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000321
322 // Print JumpTable Information
Chris Lattner1d196bc2010-01-25 23:26:13 +0000323 if (JumpTableInfo)
324 JumpTableInfo->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 // Print Constant Pool
Chris Lattnerd7397752009-08-23 01:12:47 +0000327 ConstantPool->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328
Dan Gohman1e57df32008-02-10 18:45:23 +0000329 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000331 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +0000332 OS << "Function Live Ins: ";
Chris Lattner1b989192007-12-31 04:13:23 +0000333 for (MachineRegisterInfo::livein_iterator
334 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000335 if (TRI)
Dan Gohman57b31652009-10-31 20:19:03 +0000336 OS << "%" << TRI->getName(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 else
Dan Gohman57b31652009-10-31 20:19:03 +0000338 OS << " %physreg" << I->first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
340 if (I->second)
Dan Gohman57b31652009-10-31 20:19:03 +0000341 OS << " in reg%" << I->second;
342
Chris Lattnerb44b4292009-12-03 00:50:42 +0000343 if (llvm::next(I) != E)
Dan Gohman57b31652009-10-31 20:19:03 +0000344 OS << ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 }
Chris Lattnerd7397752009-08-23 01:12:47 +0000346 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 }
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000348 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +0000349 OS << "Function Live Outs: ";
Chris Lattner1b989192007-12-31 04:13:23 +0000350 for (MachineRegisterInfo::liveout_iterator
Dan Gohman57b31652009-10-31 20:19:03 +0000351 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
Dan Gohman1e57df32008-02-10 18:45:23 +0000352 if (TRI)
Dan Gohman57b31652009-10-31 20:19:03 +0000353 OS << '%' << TRI->getName(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 else
Dan Gohman57b31652009-10-31 20:19:03 +0000355 OS << "%physreg" << *I;
356
Chris Lattnerb44b4292009-12-03 00:50:42 +0000357 if (llvm::next(I) != E)
Dan Gohman57b31652009-10-31 20:19:03 +0000358 OS << " ";
359 }
Chris Lattnerd7397752009-08-23 01:12:47 +0000360 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 }
362
Dan Gohman57b31652009-10-31 20:19:03 +0000363 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
364 OS << '\n';
Chris Lattnerb3fff702009-08-23 00:47:04 +0000365 BB->print(OS);
Dan Gohman57b31652009-10-31 20:19:03 +0000366 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000367
Dan Gohman57b31652009-10-31 20:19:03 +0000368 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369}
370
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371namespace llvm {
372 template<>
373 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossere2c3aec2009-11-30 12:38:13 +0000374
375 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 static std::string getGraphName(const MachineFunction *F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000378 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379 }
380
Tobias Grosser810b18c2009-11-30 12:38:47 +0000381 std::string getNodeLabel(const MachineBasicBlock *Node,
382 const MachineFunction *Graph) {
383 if (isSimple () && Node->getBasicBlock() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 !Node->getBasicBlock()->getName().empty())
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000385 return Node->getBasicBlock()->getNameStr() + ":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386
Chris Lattner0bde4e32009-08-23 03:13:20 +0000387 std::string OutStr;
388 {
389 raw_string_ostream OSS(OutStr);
390
Tobias Grosser810b18c2009-11-30 12:38:47 +0000391 if (isSimple())
Chris Lattner0bde4e32009-08-23 03:13:20 +0000392 OSS << Node->getNumber() << ':';
393 else
394 Node->print(OSS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 }
396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
398
399 // Process string output to make it nicer...
400 for (unsigned i = 0; i != OutStr.length(); ++i)
401 if (OutStr[i] == '\n') { // Left justify
402 OutStr[i] = '\\';
403 OutStr.insert(OutStr.begin()+i+1, 'l');
404 }
405 return OutStr;
406 }
407 };
408}
409
410void MachineFunction::viewCFG() const
411{
412#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000413 ViewGraph(this, "mf" + getFunction()->getNameStr());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000415 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
416 << "systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000417#endif // NDEBUG
418}
419
420void MachineFunction::viewCFGOnly() const
421{
Owen Andersonf4a15462009-06-24 17:37:09 +0000422#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000423 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
Owen Andersonf4a15462009-06-24 17:37:09 +0000424#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000425 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
426 << "systems with Graphviz or gv!\n";
Owen Andersonf4a15462009-06-24 17:37:09 +0000427#endif // NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428}
429
Bob Wilsonb6737aa2009-04-20 18:36:57 +0000430/// addLiveIn - Add the specified physical register as a live-in value and
431/// create a corresponding virtual register for it.
432unsigned MachineFunction::addLiveIn(unsigned PReg,
433 const TargetRegisterClass *RC) {
434 assert(RC->contains(PReg) && "Not the correct regclass!");
435 unsigned VReg = getRegInfo().createVirtualRegister(RC);
436 getRegInfo().addLiveIn(PReg, VReg);
437 return VReg;
438}
439
Devang Patel042945f2010-01-16 06:09:35 +0000440/// getDILocation - Get the DILocation for a given DebugLoc object.
441DILocation MachineFunction::getDILocation(DebugLoc DL) const {
Bill Wendlingc6075092009-02-03 22:55:54 +0000442 unsigned Idx = DL.getIndex();
Bill Wendling4756f3b2009-02-03 22:49:58 +0000443 assert(Idx < DebugLocInfo.DebugLocations.size() &&
444 "Invalid index into debug locations!");
Devang Patel042945f2010-01-16 06:09:35 +0000445 return DILocation(DebugLocInfo.DebugLocations[Idx]);
Bill Wendling4756f3b2009-02-03 22:49:58 +0000446}
447
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448//===----------------------------------------------------------------------===//
449// MachineFrameInfo implementation
450//===----------------------------------------------------------------------===//
451
Chris Lattner610b9eb2008-01-25 07:19:06 +0000452/// CreateFixedObject - Create a new object at a fixed location on the stack.
453/// All fixed objects should be created before other objects are created for
454/// efficiency. By default, fixed objects are immutable. This returns an
455/// index with a negative value.
456///
457int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
David Greene6424ab92009-11-12 20:49:22 +0000458 bool Immutable, bool isSS) {
Chris Lattner610b9eb2008-01-25 07:19:06 +0000459 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
David Greene6424ab92009-11-12 20:49:22 +0000460 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
461 isSS));
Chris Lattner610b9eb2008-01-25 07:19:06 +0000462 return -++NumFixedObjects;
463}
464
465
Jakob Stoklund Olesene7b072a2009-08-13 16:19:33 +0000466BitVector
467MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
468 assert(MBB && "MBB must be valid");
469 const MachineFunction *MF = MBB->getParent();
470 assert(MF && "MBB must be part of a MachineFunction");
471 const TargetMachine &TM = MF->getTarget();
472 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
473 BitVector BV(TRI->getNumRegs());
474
475 // Before CSI is calculated, no registers are considered pristine. They can be
476 // freely used and PEI will make sure they are saved.
477 if (!isCalleeSavedInfoValid())
478 return BV;
479
480 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
481 BV.set(*CSR);
482
483 // The entry MBB always has all CSRs pristine.
484 if (MBB == &MF->front())
485 return BV;
486
487 // On other MBBs the saved CSRs are not pristine.
488 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
489 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
490 E = CSI.end(); I != E; ++I)
491 BV.reset(I->getReg());
492
493 return BV;
494}
495
496
Chris Lattnerd7397752009-08-23 01:12:47 +0000497void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman57b31652009-10-31 20:19:03 +0000498 if (Objects.empty()) return;
499
Matthijs Kooijman0e9ee532008-11-03 11:16:43 +0000500 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
501 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000502
Dan Gohman57b31652009-10-31 20:19:03 +0000503 OS << "Frame Objects:\n";
504
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
506 const StackObject &SO = Objects[i];
Dan Gohman57b31652009-10-31 20:19:03 +0000507 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengda872532008-02-27 03:04:06 +0000508 if (SO.Size == ~0ULL) {
509 OS << "dead\n";
510 continue;
511 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 if (SO.Size == 0)
513 OS << "variable sized";
514 else
Dan Gohman57b31652009-10-31 20:19:03 +0000515 OS << "size=" << SO.Size;
516 OS << ", align=" << SO.Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517
518 if (i < NumFixedObjects)
Dan Gohman57b31652009-10-31 20:19:03 +0000519 OS << ", fixed";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 if (i < NumFixedObjects || SO.SPOffset != -1) {
521 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman57b31652009-10-31 20:19:03 +0000522 OS << ", at location [SP";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 if (Off > 0)
524 OS << "+" << Off;
525 else if (Off < 0)
526 OS << Off;
527 OS << "]";
528 }
529 OS << "\n";
530 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531}
532
533void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greene869dddf2010-01-04 23:39:17 +0000534 print(MF, dbgs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535}
536
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537//===----------------------------------------------------------------------===//
538// MachineJumpTableInfo implementation
539//===----------------------------------------------------------------------===//
540
Chris Lattner1d196bc2010-01-25 23:26:13 +0000541/// getEntrySize - Return the size of each entry in the jump table.
542unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
543 // The size of a jump table entry is 4 bytes unless the entry is just the
544 // address of a block, in which case it is the pointer size.
545 switch (getEntryKind()) {
546 case MachineJumpTableInfo::EK_BlockAddress:
547 return TD.getPointerSize();
548 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
549 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner8ef60fc2010-01-26 04:05:28 +0000550 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner1d196bc2010-01-25 23:26:13 +0000551 return 4;
552 }
553 assert(0 && "Unknown jump table encoding!");
554 return ~0;
555}
556
557/// getEntryAlignment - Return the alignment of each entry in the jump table.
558unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
559 // The alignment of a jump table entry is the alignment of int32 unless the
560 // entry is just the address of a block, in which case it is the pointer
561 // alignment.
562 switch (getEntryKind()) {
563 case MachineJumpTableInfo::EK_BlockAddress:
564 return TD.getPointerABIAlignment();
565 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
566 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner8ef60fc2010-01-26 04:05:28 +0000567 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner1d196bc2010-01-25 23:26:13 +0000568 return TD.getABIIntegerTypeAlignment(32);
569 }
570 assert(0 && "Unknown jump table encoding!");
571 return ~0;
572}
573
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574/// getJumpTableIndex - Create a new jump table entry in the jump table info
575/// or return an existing one.
576///
577unsigned MachineJumpTableInfo::getJumpTableIndex(
578 const std::vector<MachineBasicBlock*> &DestBBs) {
579 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000580 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
581 return JumpTables.size()-1;
582}
583
Chris Lattner978b9772010-01-26 05:58:28 +0000584/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
585/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
586/// normal 'L' label is returned.
587MCSymbol *MachineJumpTableInfo::getJTISymbol(unsigned JTI, MCContext &Ctx,
588 bool isLinkerPrivate) const {
589 assert(JTI < JumpTables.size() && !JumpTables[JTI].MBBs.empty() &&
590 "Invalid JTI!");
591 const MachineFunction *MF = JumpTables[JTI].MBBs[0]->getParent();
592 const MCAsmInfo &MAI = *MF->getTarget().getMCAsmInfo();
593
594 const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
595 MAI.getPrivateGlobalPrefix();
596 SmallString<60> Name;
597 raw_svector_ostream(Name)
598 << Prefix << "JTI" << MF->getFunctionNumber() << '_' << JTI;
599 return Ctx.GetOrCreateSymbol(Name.str());
600}
601
602
Dan Gohman352406d2009-04-15 01:18:49 +0000603/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
604/// the jump tables to branch to New instead.
Chris Lattner978b9772010-01-26 05:58:28 +0000605bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
606 MachineBasicBlock *New) {
Dan Gohman352406d2009-04-15 01:18:49 +0000607 assert(Old != New && "Not making a change?");
608 bool MadeChange = false;
Jim Grosbach47387c52009-11-14 20:09:13 +0000609 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
610 ReplaceMBBInJumpTable(i, Old, New);
611 return MadeChange;
612}
613
614/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
615/// the jump table to branch to New instead.
Chris Lattner978b9772010-01-26 05:58:28 +0000616bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
617 MachineBasicBlock *Old,
618 MachineBasicBlock *New) {
Jim Grosbach47387c52009-11-14 20:09:13 +0000619 assert(Old != New && "Not making a change?");
620 bool MadeChange = false;
621 MachineJumpTableEntry &JTE = JumpTables[Idx];
622 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
623 if (JTE.MBBs[j] == Old) {
624 JTE.MBBs[j] = New;
625 MadeChange = true;
626 }
Dan Gohman352406d2009-04-15 01:18:49 +0000627 return MadeChange;
628}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629
Chris Lattnerd7397752009-08-23 01:12:47 +0000630void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000631 if (JumpTables.empty()) return;
632
633 OS << "Jump Tables:\n";
634
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000635 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman57b31652009-10-31 20:19:03 +0000636 OS << " jt#" << i << ": ";
637 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
638 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000639 }
Dan Gohman57b31652009-10-31 20:19:03 +0000640
641 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642}
643
David Greene869dddf2010-01-04 23:39:17 +0000644void MachineJumpTableInfo::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645
646
647//===----------------------------------------------------------------------===//
648// MachineConstantPool implementation
649//===----------------------------------------------------------------------===//
650
651const Type *MachineConstantPoolEntry::getType() const {
652 if (isMachineConstantPoolEntry())
Chris Lattner8f3f8512009-07-21 23:34:23 +0000653 return Val.MachineCPVal->getType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000654 return Val.ConstVal->getType();
655}
656
Chris Lattner8f3f8512009-07-21 23:34:23 +0000657
Chris Lattner690b8d52009-07-21 23:36:01 +0000658unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattner8f3f8512009-07-21 23:34:23 +0000659 if (isMachineConstantPoolEntry())
Chris Lattner690b8d52009-07-21 23:36:01 +0000660 return Val.MachineCPVal->getRelocationInfo();
Chris Lattnerbdb23b32009-07-22 00:05:44 +0000661 return Val.ConstVal->getRelocationInfo();
Chris Lattner8f3f8512009-07-21 23:34:23 +0000662}
663
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000664MachineConstantPool::~MachineConstantPool() {
665 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
666 if (Constants[i].isMachineConstantPoolEntry())
667 delete Constants[i].Val.MachineCPVal;
668}
669
Dan Gohman114924d2009-10-28 01:12:16 +0000670/// CanShareConstantPoolEntry - Test whether the given two constants
671/// can be allocated the same constant pool entry.
672static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
673 const TargetData *TD) {
674 // Handle the trivial case quickly.
675 if (A == B) return true;
676
677 // If they have the same type but weren't the same constant, quickly
678 // reject them.
679 if (A->getType() == B->getType()) return false;
680
681 // For now, only support constants with the same size.
682 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
683 return false;
684
685 // If a floating-point value and an integer value have the same encoding,
686 // they can share a constant-pool entry.
687 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
688 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
689 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
690 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
691 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
692 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
693
694 // Two vectors can share an entry if each pair of corresponding
695 // elements could.
696 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
697 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
698 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
699 return false;
700 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
Dan Gohman509fccf2009-10-31 14:12:53 +0000701 if (!CanShareConstantPoolEntry(AV->getOperand(i),
702 BV->getOperand(i), TD))
Dan Gohman114924d2009-10-28 01:12:16 +0000703 return false;
704 return true;
705 }
706
707 // TODO: Handle other cases.
708
709 return false;
710}
711
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000712/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman62e7d9f2008-09-16 20:45:53 +0000713/// an existing one. User must specify the log2 of the minimum required
714/// alignment for the object.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715///
716unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
717 unsigned Alignment) {
718 assert(Alignment && "Alignment must be specified!");
719 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman114924d2009-10-28 01:12:16 +0000720
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000721 // Check to see if we already have this constant.
722 //
723 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman114924d2009-10-28 01:12:16 +0000725 if (!Constants[i].isMachineConstantPoolEntry() &&
726 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
727 if ((unsigned)Constants[i].getAlignment() < Alignment)
728 Constants[i].Alignment = Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000729 return i;
Dan Gohman114924d2009-10-28 01:12:16 +0000730 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000731
Evan Cheng68c18682009-03-13 07:51:59 +0000732 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000733 return Constants.size()-1;
734}
735
736unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
737 unsigned Alignment) {
738 assert(Alignment && "Alignment must be specified!");
739 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
740
741 // Check to see if we already have this constant.
742 //
743 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000744 int Idx = V->getExistingMachineCPValue(this, Alignment);
745 if (Idx != -1)
746 return (unsigned)Idx;
Evan Cheng68c18682009-03-13 07:51:59 +0000747
748 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000749 return Constants.size()-1;
750}
751
Chris Lattner491f7832008-08-23 22:53:13 +0000752void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000753 if (Constants.empty()) return;
754
755 OS << "Constant Pool:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000756 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman57b31652009-10-31 20:19:03 +0000757 OS << " cp#" << i << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000758 if (Constants[i].isMachineConstantPoolEntry())
759 Constants[i].Val.MachineCPVal->print(OS);
760 else
761 OS << *(Value*)Constants[i].Val.ConstVal;
Dan Gohman57b31652009-10-31 20:19:03 +0000762 OS << ", align=" << Constants[i].getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 OS << "\n";
764 }
765}
766
David Greene869dddf2010-01-04 23:39:17 +0000767void MachineConstantPool::dump() const { print(dbgs()); }