blob: f6cc71f3a4376411ad4c607a5517b7b7f87b4900 [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,
Chris Lattnerf4853452010-03-13 20:55:24 +000088 unsigned FunctionNum, MCContext &ctx)
89 : Fn(F), Target(TM), Ctx(ctx) {
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +000090 if (TM.getRegisterInfo())
Dan Gohman7616a1e2010-03-18 18:49:47 +000091 RegInfo = new (Allocator) MachineRegisterInfo(*TM.getRegisterInfo());
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +000092 else
93 RegInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 MFInfo = 0;
Dan Gohman7616a1e2010-03-18 18:49:47 +000095 FrameInfo = new (Allocator) MachineFrameInfo(*TM.getFrameInfo());
Charles Davisde5c9572010-02-19 18:17:13 +000096 if (Fn->hasFnAttr(Attribute::StackAlignment))
97 FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs(
98 Fn->getAttributes().getFnAttributes()));
Dan Gohman7616a1e2010-03-18 18:49:47 +000099 ConstantPool = new (Allocator) 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
Dan Gohman7616a1e2010-03-18 18:49:47 +0000132 JumpTableInfo = new (Allocator)
Chris Lattner1d196bc2010-01-25 23:26:13 +0000133 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
Dan Gohmandf1a7ff2010-02-10 16:03:48 +0000190/// 'Orig' instruction, identical in all ways except the instruction
Dan Gohman221a4372008-07-07 23:14:23 +0000191/// 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) {
Dan Gohman7616a1e2010-03-18 18:49:47 +0000229 return new (Allocator) MachineMemOperand(v, f, o, s, base_alignment);
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000230}
231
232MachineMemOperand *
233MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
234 int64_t Offset, uint64_t Size) {
Dan Gohman7616a1e2010-03-18 18:49:47 +0000235 return new (Allocator)
Dan Gohman4e3bb1b2009-09-25 20:36:54 +0000236 MachineMemOperand(MMO->getValue(), MMO->getFlags(),
237 int64_t(uint64_t(MMO->getOffset()) +
238 uint64_t(Offset)),
239 Size, MMO->getBaseAlignment());
240}
241
242MachineInstr::mmo_iterator
243MachineFunction::allocateMemRefsArray(unsigned long Num) {
244 return Allocator.Allocate<MachineMemOperand *>(Num);
245}
246
Dan Gohmanc7973eb2009-10-09 18:10:05 +0000247std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
248MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
249 MachineInstr::mmo_iterator End) {
250 // Count the number of load mem refs.
251 unsigned Num = 0;
252 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
253 if ((*I)->isLoad())
254 ++Num;
255
256 // Allocate a new array and populate it with the load information.
257 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
258 unsigned Index = 0;
259 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
260 if ((*I)->isLoad()) {
261 if (!(*I)->isStore())
262 // Reuse the MMO.
263 Result[Index] = *I;
264 else {
265 // Clone the MMO and unset the store flag.
266 MachineMemOperand *JustLoad =
267 getMachineMemOperand((*I)->getValue(),
268 (*I)->getFlags() & ~MachineMemOperand::MOStore,
269 (*I)->getOffset(), (*I)->getSize(),
270 (*I)->getBaseAlignment());
271 Result[Index] = JustLoad;
272 }
273 ++Index;
274 }
275 }
276 return std::make_pair(Result, Result + Num);
277}
278
279std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
280MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
281 MachineInstr::mmo_iterator End) {
282 // Count the number of load mem refs.
283 unsigned Num = 0;
284 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
285 if ((*I)->isStore())
286 ++Num;
287
288 // Allocate a new array and populate it with the store information.
289 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
290 unsigned Index = 0;
291 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
292 if ((*I)->isStore()) {
293 if (!(*I)->isLoad())
294 // Reuse the MMO.
295 Result[Index] = *I;
296 else {
297 // Clone the MMO and unset the load flag.
298 MachineMemOperand *JustStore =
299 getMachineMemOperand((*I)->getValue(),
300 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
301 (*I)->getOffset(), (*I)->getSize(),
302 (*I)->getBaseAlignment());
303 Result[Index] = JustStore;
304 }
305 ++Index;
306 }
307 }
308 return std::make_pair(Result, Result + Num);
309}
310
Dan Gohman221a4372008-07-07 23:14:23 +0000311void MachineFunction::dump() const {
David Greene869dddf2010-01-04 23:39:17 +0000312 print(dbgs());
Dan Gohman221a4372008-07-07 23:14:23 +0000313}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314
Chris Lattnerd7397752009-08-23 01:12:47 +0000315void MachineFunction::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000316 OS << "# Machine code for function " << Fn->getName() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317
318 // Print Frame Information
Dan Gohman221a4372008-07-07 23:14:23 +0000319 FrameInfo->print(*this, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000320
321 // Print JumpTable Information
Chris Lattner1d196bc2010-01-25 23:26:13 +0000322 if (JumpTableInfo)
323 JumpTableInfo->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324
325 // Print Constant Pool
Chris Lattnerd7397752009-08-23 01:12:47 +0000326 ConstantPool->print(OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000327
Dan Gohman1e57df32008-02-10 18:45:23 +0000328 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000330 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +0000331 OS << "Function Live Ins: ";
Chris Lattner1b989192007-12-31 04:13:23 +0000332 for (MachineRegisterInfo::livein_iterator
333 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman1e57df32008-02-10 18:45:23 +0000334 if (TRI)
Dan Gohman57b31652009-10-31 20:19:03 +0000335 OS << "%" << TRI->getName(I->first);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336 else
Dan Gohman57b31652009-10-31 20:19:03 +0000337 OS << " %physreg" << I->first;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 if (I->second)
Dan Gohman57b31652009-10-31 20:19:03 +0000340 OS << " in reg%" << I->second;
341
Chris Lattnerb44b4292009-12-03 00:50:42 +0000342 if (llvm::next(I) != E)
Dan Gohman57b31652009-10-31 20:19:03 +0000343 OS << ", ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 }
Chris Lattnerd7397752009-08-23 01:12:47 +0000345 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 }
Matthijs Kooijman1cb065e2008-10-13 12:37:16 +0000347 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohman57b31652009-10-31 20:19:03 +0000348 OS << "Function Live Outs: ";
Chris Lattner1b989192007-12-31 04:13:23 +0000349 for (MachineRegisterInfo::liveout_iterator
Dan Gohman57b31652009-10-31 20:19:03 +0000350 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
Dan Gohman1e57df32008-02-10 18:45:23 +0000351 if (TRI)
Dan Gohman57b31652009-10-31 20:19:03 +0000352 OS << '%' << TRI->getName(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 else
Dan Gohman57b31652009-10-31 20:19:03 +0000354 OS << "%physreg" << *I;
355
Chris Lattnerb44b4292009-12-03 00:50:42 +0000356 if (llvm::next(I) != E)
Dan Gohman57b31652009-10-31 20:19:03 +0000357 OS << " ";
358 }
Chris Lattnerd7397752009-08-23 01:12:47 +0000359 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 }
361
Dan Gohman57b31652009-10-31 20:19:03 +0000362 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
363 OS << '\n';
Chris Lattnerb3fff702009-08-23 00:47:04 +0000364 BB->print(OS);
Dan Gohman57b31652009-10-31 20:19:03 +0000365 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000366
Dan Gohman57b31652009-10-31 20:19:03 +0000367 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368}
369
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370namespace llvm {
371 template<>
372 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossere2c3aec2009-11-30 12:38:13 +0000373
374 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
375
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 static std::string getGraphName(const MachineFunction *F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000377 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379
Tobias Grosser810b18c2009-11-30 12:38:47 +0000380 std::string getNodeLabel(const MachineBasicBlock *Node,
381 const MachineFunction *Graph) {
382 if (isSimple () && Node->getBasicBlock() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 !Node->getBasicBlock()->getName().empty())
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000384 return Node->getBasicBlock()->getNameStr() + ":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
Chris Lattner0bde4e32009-08-23 03:13:20 +0000386 std::string OutStr;
387 {
388 raw_string_ostream OSS(OutStr);
389
Tobias Grosser810b18c2009-11-30 12:38:47 +0000390 if (isSimple())
Chris Lattner0bde4e32009-08-23 03:13:20 +0000391 OSS << Node->getNumber() << ':';
392 else
393 Node->print(OSS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 }
395
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
397
398 // Process string output to make it nicer...
399 for (unsigned i = 0; i != OutStr.length(); ++i)
400 if (OutStr[i] == '\n') { // Left justify
401 OutStr[i] = '\\';
402 OutStr.insert(OutStr.begin()+i+1, 'l');
403 }
404 return OutStr;
405 }
406 };
407}
408
409void MachineFunction::viewCFG() const
410{
411#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000412 ViewGraph(this, "mf" + getFunction()->getNameStr());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000413#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000414 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
415 << "systems with Graphviz or gv!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416#endif // NDEBUG
417}
418
419void MachineFunction::viewCFGOnly() const
420{
Owen Andersonf4a15462009-06-24 17:37:09 +0000421#ifndef NDEBUG
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000422 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
Owen Andersonf4a15462009-06-24 17:37:09 +0000423#else
Daniel Dunbar33b26632009-08-23 08:50:52 +0000424 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
425 << "systems with Graphviz or gv!\n";
Owen Andersonf4a15462009-06-24 17:37:09 +0000426#endif // NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427}
428
Bob Wilsonb6737aa2009-04-20 18:36:57 +0000429/// addLiveIn - Add the specified physical register as a live-in value and
430/// create a corresponding virtual register for it.
431unsigned MachineFunction::addLiveIn(unsigned PReg,
432 const TargetRegisterClass *RC) {
433 assert(RC->contains(PReg) && "Not the correct regclass!");
434 unsigned VReg = getRegInfo().createVirtualRegister(RC);
435 getRegInfo().addLiveIn(PReg, VReg);
436 return VReg;
437}
438
Devang Patel042945f2010-01-16 06:09:35 +0000439/// getDILocation - Get the DILocation for a given DebugLoc object.
440DILocation MachineFunction::getDILocation(DebugLoc DL) const {
Bill Wendlingc6075092009-02-03 22:55:54 +0000441 unsigned Idx = DL.getIndex();
Bill Wendling4756f3b2009-02-03 22:49:58 +0000442 assert(Idx < DebugLocInfo.DebugLocations.size() &&
443 "Invalid index into debug locations!");
Devang Patel042945f2010-01-16 06:09:35 +0000444 return DILocation(DebugLocInfo.DebugLocations[Idx]);
Bill Wendling4756f3b2009-02-03 22:49:58 +0000445}
446
Chris Lattner541d8902010-01-26 06:28:43 +0000447
448/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
449/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
450/// normal 'L' label is returned.
451MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
452 bool isLinkerPrivate) const {
453 assert(JumpTableInfo && "No jump tables");
454
Chandler Carruth9f4185e2010-01-27 10:27:10 +0000455 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
Chris Lattner541d8902010-01-26 06:28:43 +0000456 const MCAsmInfo &MAI = *getTarget().getMCAsmInfo();
457
458 const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
459 MAI.getPrivateGlobalPrefix();
460 SmallString<60> Name;
461 raw_svector_ostream(Name)
462 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
Chris Lattner3b197832010-03-30 18:10:53 +0000463 return Ctx.GetOrCreateSymbol(Name.str());
Chris Lattner541d8902010-01-26 06:28:43 +0000464}
465
466
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467//===----------------------------------------------------------------------===//
468// MachineFrameInfo implementation
469//===----------------------------------------------------------------------===//
470
Chris Lattner610b9eb2008-01-25 07:19:06 +0000471/// CreateFixedObject - Create a new object at a fixed location on the stack.
472/// All fixed objects should be created before other objects are created for
473/// efficiency. By default, fixed objects are immutable. This returns an
474/// index with a negative value.
475///
476int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
David Greene6424ab92009-11-12 20:49:22 +0000477 bool Immutable, bool isSS) {
Chris Lattner610b9eb2008-01-25 07:19:06 +0000478 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
David Greene6424ab92009-11-12 20:49:22 +0000479 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
480 isSS));
Chris Lattner610b9eb2008-01-25 07:19:06 +0000481 return -++NumFixedObjects;
482}
483
484
Jakob Stoklund Olesene7b072a2009-08-13 16:19:33 +0000485BitVector
486MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
487 assert(MBB && "MBB must be valid");
488 const MachineFunction *MF = MBB->getParent();
489 assert(MF && "MBB must be part of a MachineFunction");
490 const TargetMachine &TM = MF->getTarget();
491 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
492 BitVector BV(TRI->getNumRegs());
493
494 // Before CSI is calculated, no registers are considered pristine. They can be
495 // freely used and PEI will make sure they are saved.
496 if (!isCalleeSavedInfoValid())
497 return BV;
498
499 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
500 BV.set(*CSR);
501
502 // The entry MBB always has all CSRs pristine.
503 if (MBB == &MF->front())
504 return BV;
505
506 // On other MBBs the saved CSRs are not pristine.
507 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
508 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
509 E = CSI.end(); I != E; ++I)
510 BV.reset(I->getReg());
511
512 return BV;
513}
514
515
Chris Lattnerd7397752009-08-23 01:12:47 +0000516void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman57b31652009-10-31 20:19:03 +0000517 if (Objects.empty()) return;
518
Matthijs Kooijman0e9ee532008-11-03 11:16:43 +0000519 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
520 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521
Dan Gohman57b31652009-10-31 20:19:03 +0000522 OS << "Frame Objects:\n";
523
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000524 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
525 const StackObject &SO = Objects[i];
Dan Gohman57b31652009-10-31 20:19:03 +0000526 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengda872532008-02-27 03:04:06 +0000527 if (SO.Size == ~0ULL) {
528 OS << "dead\n";
529 continue;
530 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 if (SO.Size == 0)
532 OS << "variable sized";
533 else
Dan Gohman57b31652009-10-31 20:19:03 +0000534 OS << "size=" << SO.Size;
535 OS << ", align=" << SO.Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536
537 if (i < NumFixedObjects)
Dan Gohman57b31652009-10-31 20:19:03 +0000538 OS << ", fixed";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000539 if (i < NumFixedObjects || SO.SPOffset != -1) {
540 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman57b31652009-10-31 20:19:03 +0000541 OS << ", at location [SP";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 if (Off > 0)
543 OS << "+" << Off;
544 else if (Off < 0)
545 OS << Off;
546 OS << "]";
547 }
548 OS << "\n";
549 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550}
551
552void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greene869dddf2010-01-04 23:39:17 +0000553 print(MF, dbgs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554}
555
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556//===----------------------------------------------------------------------===//
557// MachineJumpTableInfo implementation
558//===----------------------------------------------------------------------===//
559
Chris Lattner1d196bc2010-01-25 23:26:13 +0000560/// getEntrySize - Return the size of each entry in the jump table.
561unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
562 // The size of a jump table entry is 4 bytes unless the entry is just the
563 // address of a block, in which case it is the pointer size.
564 switch (getEntryKind()) {
565 case MachineJumpTableInfo::EK_BlockAddress:
566 return TD.getPointerSize();
567 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
568 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner8ef60fc2010-01-26 04:05:28 +0000569 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner1d196bc2010-01-25 23:26:13 +0000570 return 4;
Richard Osborne9f3cfe62010-03-11 14:58:16 +0000571 case MachineJumpTableInfo::EK_Inline:
572 return 0;
Chris Lattner1d196bc2010-01-25 23:26:13 +0000573 }
574 assert(0 && "Unknown jump table encoding!");
575 return ~0;
576}
577
578/// getEntryAlignment - Return the alignment of each entry in the jump table.
579unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
580 // The alignment of a jump table entry is the alignment of int32 unless the
581 // entry is just the address of a block, in which case it is the pointer
582 // alignment.
583 switch (getEntryKind()) {
584 case MachineJumpTableInfo::EK_BlockAddress:
585 return TD.getPointerABIAlignment();
586 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
587 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner8ef60fc2010-01-26 04:05:28 +0000588 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner1d196bc2010-01-25 23:26:13 +0000589 return TD.getABIIntegerTypeAlignment(32);
Richard Osborne9f3cfe62010-03-11 14:58:16 +0000590 case MachineJumpTableInfo::EK_Inline:
591 return 1;
Chris Lattner1d196bc2010-01-25 23:26:13 +0000592 }
593 assert(0 && "Unknown jump table encoding!");
594 return ~0;
595}
596
Bob Wilson1f8f5802010-03-18 18:42:41 +0000597/// createJumpTableIndex - Create a new jump table entry in the jump table info.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598///
Bob Wilson1f8f5802010-03-18 18:42:41 +0000599unsigned MachineJumpTableInfo::createJumpTableIndex(
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 const std::vector<MachineBasicBlock*> &DestBBs) {
601 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
603 return JumpTables.size()-1;
604}
605
Dan Gohman352406d2009-04-15 01:18:49 +0000606/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
607/// the jump tables to branch to New instead.
Chris Lattner978b9772010-01-26 05:58:28 +0000608bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
609 MachineBasicBlock *New) {
Dan Gohman352406d2009-04-15 01:18:49 +0000610 assert(Old != New && "Not making a change?");
611 bool MadeChange = false;
Jim Grosbach47387c52009-11-14 20:09:13 +0000612 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
613 ReplaceMBBInJumpTable(i, Old, New);
614 return MadeChange;
615}
616
617/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
618/// the jump table to branch to New instead.
Chris Lattner978b9772010-01-26 05:58:28 +0000619bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
620 MachineBasicBlock *Old,
621 MachineBasicBlock *New) {
Jim Grosbach47387c52009-11-14 20:09:13 +0000622 assert(Old != New && "Not making a change?");
623 bool MadeChange = false;
624 MachineJumpTableEntry &JTE = JumpTables[Idx];
625 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
626 if (JTE.MBBs[j] == Old) {
627 JTE.MBBs[j] = New;
628 MadeChange = true;
629 }
Dan Gohman352406d2009-04-15 01:18:49 +0000630 return MadeChange;
631}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000632
Chris Lattnerd7397752009-08-23 01:12:47 +0000633void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000634 if (JumpTables.empty()) return;
635
636 OS << "Jump Tables:\n";
637
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000638 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman57b31652009-10-31 20:19:03 +0000639 OS << " jt#" << i << ": ";
640 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
641 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000642 }
Dan Gohman57b31652009-10-31 20:19:03 +0000643
644 OS << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000645}
646
David Greene869dddf2010-01-04 23:39:17 +0000647void MachineJumpTableInfo::dump() const { print(dbgs()); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000648
649
650//===----------------------------------------------------------------------===//
651// MachineConstantPool implementation
652//===----------------------------------------------------------------------===//
653
654const Type *MachineConstantPoolEntry::getType() const {
655 if (isMachineConstantPoolEntry())
Chris Lattner8f3f8512009-07-21 23:34:23 +0000656 return Val.MachineCPVal->getType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000657 return Val.ConstVal->getType();
658}
659
Chris Lattner8f3f8512009-07-21 23:34:23 +0000660
Chris Lattner690b8d52009-07-21 23:36:01 +0000661unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattner8f3f8512009-07-21 23:34:23 +0000662 if (isMachineConstantPoolEntry())
Chris Lattner690b8d52009-07-21 23:36:01 +0000663 return Val.MachineCPVal->getRelocationInfo();
Chris Lattnerbdb23b32009-07-22 00:05:44 +0000664 return Val.ConstVal->getRelocationInfo();
Chris Lattner8f3f8512009-07-21 23:34:23 +0000665}
666
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667MachineConstantPool::~MachineConstantPool() {
668 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
669 if (Constants[i].isMachineConstantPoolEntry())
670 delete Constants[i].Val.MachineCPVal;
671}
672
Dan Gohman114924d2009-10-28 01:12:16 +0000673/// CanShareConstantPoolEntry - Test whether the given two constants
674/// can be allocated the same constant pool entry.
675static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
676 const TargetData *TD) {
677 // Handle the trivial case quickly.
678 if (A == B) return true;
679
680 // If they have the same type but weren't the same constant, quickly
681 // reject them.
682 if (A->getType() == B->getType()) return false;
683
684 // For now, only support constants with the same size.
685 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
686 return false;
687
688 // If a floating-point value and an integer value have the same encoding,
689 // they can share a constant-pool entry.
690 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
691 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
692 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
693 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
694 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
695 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
696
697 // Two vectors can share an entry if each pair of corresponding
698 // elements could.
699 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
700 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
701 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
702 return false;
703 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
Dan Gohman509fccf2009-10-31 14:12:53 +0000704 if (!CanShareConstantPoolEntry(AV->getOperand(i),
705 BV->getOperand(i), TD))
Dan Gohman114924d2009-10-28 01:12:16 +0000706 return false;
707 return true;
708 }
709
710 // TODO: Handle other cases.
711
712 return false;
713}
714
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000715/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman62e7d9f2008-09-16 20:45:53 +0000716/// an existing one. User must specify the log2 of the minimum required
717/// alignment for the object.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000718///
719unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
720 unsigned Alignment) {
721 assert(Alignment && "Alignment must be specified!");
722 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman114924d2009-10-28 01:12:16 +0000723
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000724 // Check to see if we already have this constant.
725 //
726 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman114924d2009-10-28 01:12:16 +0000728 if (!Constants[i].isMachineConstantPoolEntry() &&
729 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
730 if ((unsigned)Constants[i].getAlignment() < Alignment)
731 Constants[i].Alignment = Alignment;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000732 return i;
Dan Gohman114924d2009-10-28 01:12:16 +0000733 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000734
Evan Cheng68c18682009-03-13 07:51:59 +0000735 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 return Constants.size()-1;
737}
738
739unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
740 unsigned Alignment) {
741 assert(Alignment && "Alignment must be specified!");
742 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
743
744 // Check to see if we already have this constant.
745 //
746 // FIXME, this could be made much more efficient for large constant pools.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000747 int Idx = V->getExistingMachineCPValue(this, Alignment);
748 if (Idx != -1)
749 return (unsigned)Idx;
Evan Cheng68c18682009-03-13 07:51:59 +0000750
751 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752 return Constants.size()-1;
753}
754
Chris Lattner491f7832008-08-23 22:53:13 +0000755void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman57b31652009-10-31 20:19:03 +0000756 if (Constants.empty()) return;
757
758 OS << "Constant Pool:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman57b31652009-10-31 20:19:03 +0000760 OS << " cp#" << i << ": ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000761 if (Constants[i].isMachineConstantPoolEntry())
762 Constants[i].Val.MachineCPVal->print(OS);
763 else
764 OS << *(Value*)Constants[i].Val.ConstVal;
Dan Gohman57b31652009-10-31 20:19:03 +0000765 OS << ", align=" << Constants[i].getAlignment();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000766 OS << "\n";
767 }
768}
769
David Greene869dddf2010-01-04 23:39:17 +0000770void MachineConstantPool::dump() const { print(dbgs()); }