blob: cf3b8f056ad1318d3ac580c786d53809761ac579 [file] [log] [blame]
Chris Lattner6b944532002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +00009//
Chris Lattner6b944532002-10-28 01:16:38 +000010// 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//===----------------------------------------------------------------------===//
Chris Lattnerf2868ce2002-02-03 07:54:50 +000015
Chris Lattnerd2b7cec2007-02-14 05:52:17 +000016#include "llvm/DerivedTypes.h"
Bill Wendling20c568f2009-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"
Chris Lattner4d149cd2003-01-13 00:23:03 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greene098612b2009-08-19 22:16:11 +000022#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman37efe672006-04-22 18:53:45 +000026#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000028#include "llvm/CodeGen/Passes.h"
Devang Patelc99fd872010-01-19 06:09:04 +000029#include "llvm/Analysis/DebugInfo.h"
David Greenedc554812010-01-04 23:39:17 +000030#include "llvm/Support/Debug.h"
Owen Anderson07000c62006-05-12 06:33:49 +000031#include "llvm/Target/TargetData.h"
Bill Wendling20c568f2009-06-30 22:38:32 +000032#include "llvm/Target/TargetLowering.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000033#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000034#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000035#include "llvm/Support/GraphWriter.h"
Chris Lattner944fac72008-08-23 22:23:09 +000036#include "llvm/Support/raw_ostream.h"
Chris Lattner07f32d42003-12-20 09:17:07 +000037using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000038
Chris Lattner227c3d32002-10-28 01:12:41 +000039namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000040 struct Printer : public MachineFunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000041 static char ID;
Devang Patel794fd752007-05-01 21:15:47 +000042
Chris Lattnercf143a42009-08-23 03:13:20 +000043 raw_ostream &OS;
Chris Lattnerd4baf0f2004-02-01 05:25:07 +000044 const std::string Banner;
Brian Gaeke09caa372004-01-30 21:53:46 +000045
Chris Lattnercf143a42009-08-23 03:13:20 +000046 Printer(raw_ostream &os, const std::string &banner)
Dan Gohmanae73dc12008-09-04 17:05:41 +000047 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
Brian Gaeke09caa372004-01-30 21:53:46 +000048
Chris Lattner10491642002-10-30 00:48:05 +000049 const char *getPassName() const { return "MachineFunction Printer"; }
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesAll();
Dan Gohmanad2afc22009-07-31 18:16:33 +000053 MachineFunctionPass::getAnalysisUsage(AU);
Chris Lattner10491642002-10-30 00:48:05 +000054 }
55
Chris Lattner16c45e92003-12-20 10:20:58 +000056 bool runOnMachineFunction(MachineFunction &MF) {
Dan Gohman0ba90f32009-10-31 20:19:03 +000057 OS << "# " << Banner << ":\n";
Chris Lattnercf143a42009-08-23 03:13:20 +000058 MF.print(OS);
Chris Lattner10491642002-10-30 00:48:05 +000059 return false;
60 }
61 };
Devang Patel19974732007-05-03 01:11:54 +000062 char Printer::ID = 0;
Chris Lattner227c3d32002-10-28 01:12:41 +000063}
64
Daniel Dunbar275872e2009-08-03 01:02:24 +000065/// Returns a newly-created MachineFunction Printer pass. The default banner is
66/// empty.
Brian Gaeke09caa372004-01-30 21:53:46 +000067///
Chris Lattnercf143a42009-08-23 03:13:20 +000068FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
Chris Lattnerce9c41e2005-01-23 22:13:58 +000069 const std::string &Banner){
Brian Gaeke09caa372004-01-30 21:53:46 +000070 return new Printer(OS, Banner);
Chris Lattner10491642002-10-30 00:48:05 +000071}
72
Chris Lattner227c3d32002-10-28 01:12:41 +000073//===---------------------------------------------------------------------===//
74// MachineFunction implementation
75//===---------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000076
Chris Lattnera70e2e32009-09-15 22:44:26 +000077// Out of line virtual method.
78MachineFunctionInfo::~MachineFunctionInfo() {}
79
Dan Gohmanfed90b62008-07-28 21:51:04 +000080void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +000081 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +000082}
Chris Lattner227c3d32002-10-28 01:12:41 +000083
Dan Gohmanad2afc22009-07-31 18:16:33 +000084MachineFunction::MachineFunction(Function *F,
Chris Lattner955fad12002-12-28 20:37:16 +000085 const TargetMachine &TM)
Dan Gohmanf266f892009-07-31 18:35:51 +000086 : Fn(F), Target(TM) {
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +000087 if (TM.getRegisterInfo())
88 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
89 MachineRegisterInfo(*TM.getRegisterInfo());
90 else
91 RegInfo = 0;
Chris Lattnerad828162004-08-16 22:36:34 +000092 MFInfo = 0;
Dan Gohman8e5f2c62008-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 Wendling20c568f2009-06-30 22:38:32 +000097 Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
Bill Wendlingdd37b362009-06-25 00:32:48 +000098
Chris Lattner071c62f2010-01-25 23:26:13 +000099 JumpTableInfo = 0;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000100}
101
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000102MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000103 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000104 InstructionRecycler.clear(Allocator);
105 BasicBlockRecycler.clear(Allocator);
Bill Wendlingdd37b362009-06-25 00:32:48 +0000106 if (RegInfo) {
107 RegInfo->~MachineRegisterInfo();
108 Allocator.Deallocate(RegInfo);
109 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000110 if (MFInfo) {
Bill Wendlingdd37b362009-06-25 00:32:48 +0000111 MFInfo->~MachineFunctionInfo();
112 Allocator.Deallocate(MFInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000113 }
114 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
115 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
Chris Lattner071c62f2010-01-25 23:26:13 +0000116
117 if (JumpTableInfo) {
118 JumpTableInfo->~MachineJumpTableInfo();
119 Allocator.Deallocate(JumpTableInfo);
120 }
Chris Lattner10491642002-10-30 00:48:05 +0000121}
122
Chris Lattner071c62f2010-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}
Chris Lattnere70cab02006-10-03 19:18:57 +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;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000149 if (MBBI != begin())
150 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000151
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 Gohman8e5f2c62008-07-07 23:14:23 +0000176/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
177/// of `new MachineInstr'.
178///
179MachineInstr *
Bill Wendling9bc96a52009-02-03 00:55:04 +0000180MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
181 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000182 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling9bc96a52009-02-03 00:55:04 +0000183 MachineInstr(TID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000184}
Chris Lattnere70cab02006-10-03 19:18:57 +0000185
Dan Gohman8e5f2c62008-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 Gohman8e5f2c62008-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 Gohmanc76909a2009-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 Gohman91e69c32009-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 Gohman8e5f2c62008-07-07 23:14:23 +0000309void MachineFunction::dump() const {
David Greenedc554812010-01-04 23:39:17 +0000310 print(dbgs());
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000311}
Chris Lattner10491642002-10-30 00:48:05 +0000312
Chris Lattnerd74c5562009-08-23 01:12:47 +0000313void MachineFunction::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000314 OS << "# Machine code for function " << Fn->getName() << ":\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000315
316 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000317 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000318
319 // Print JumpTable Information
Chris Lattner071c62f2010-01-25 23:26:13 +0000320 if (JumpTableInfo)
321 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000322
323 // Print Constant Pool
Chris Lattnerd74c5562009-08-23 01:12:47 +0000324 ConstantPool->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000325
Dan Gohman6f0d0242008-02-10 18:45:23 +0000326 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000327
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000328 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000329 OS << "Function Live Ins: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000330 for (MachineRegisterInfo::livein_iterator
331 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000332 if (TRI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000333 OS << "%" << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000334 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000335 OS << " %physreg" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000336
337 if (I->second)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000338 OS << " in reg%" << I->second;
339
Chris Lattner7896c9f2009-12-03 00:50:42 +0000340 if (llvm::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000341 OS << ", ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000342 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000343 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000344 }
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000345 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000346 OS << "Function Live Outs: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000347 for (MachineRegisterInfo::liveout_iterator
Dan Gohman0ba90f32009-10-31 20:19:03 +0000348 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
Dan Gohman6f0d0242008-02-10 18:45:23 +0000349 if (TRI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000350 OS << '%' << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000351 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000352 OS << "%physreg" << *I;
353
Chris Lattner7896c9f2009-12-03 00:50:42 +0000354 if (llvm::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000355 OS << " ";
356 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000357 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000358 }
359
Dan Gohman0ba90f32009-10-31 20:19:03 +0000360 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
361 OS << '\n';
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000362 BB->print(OS);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000363 }
Brian Gaeke47b71642004-03-29 21:58:31 +0000364
Dan Gohman0ba90f32009-10-31 20:19:03 +0000365 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000366}
367
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000368namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000369 template<>
370 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +0000371
372 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
373
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000374 static std::string getGraphName(const MachineFunction *F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000375 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000376 }
377
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000378 std::string getNodeLabel(const MachineBasicBlock *Node,
379 const MachineFunction *Graph) {
380 if (isSimple () && Node->getBasicBlock() &&
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000381 !Node->getBasicBlock()->getName().empty())
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000382 return Node->getBasicBlock()->getNameStr() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000383
Chris Lattnercf143a42009-08-23 03:13:20 +0000384 std::string OutStr;
385 {
386 raw_string_ostream OSS(OutStr);
387
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000388 if (isSimple())
Chris Lattnercf143a42009-08-23 03:13:20 +0000389 OSS << Node->getNumber() << ':';
390 else
391 Node->print(OSS);
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000392 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000393
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +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 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000405}
406
407void MachineFunction::viewCFG() const
408{
Jim Laskey851a22d2005-10-12 12:09:05 +0000409#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000410 ViewGraph(this, "mf" + getFunction()->getNameStr());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000411#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000412 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
413 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000414#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000415}
416
417void MachineFunction::viewCFGOnly() const
418{
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000419#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000420 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000421#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000422 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
423 << "systems with Graphviz or gv!\n";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000424#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000425}
426
Bob Wilson998e1252009-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 Patel6b61f582010-01-16 06:09:35 +0000437/// getDILocation - Get the DILocation for a given DebugLoc object.
438DILocation MachineFunction::getDILocation(DebugLoc DL) const {
Bill Wendling44f6ac62009-02-03 22:55:54 +0000439 unsigned Idx = DL.getIndex();
Bill Wendling85e3af92009-02-03 22:49:58 +0000440 assert(Idx < DebugLocInfo.DebugLocations.size() &&
441 "Invalid index into debug locations!");
Devang Patel6b61f582010-01-16 06:09:35 +0000442 return DILocation(DebugLocInfo.DebugLocations[Idx]);
Bill Wendling85e3af92009-02-03 22:49:58 +0000443}
444
Chris Lattner955fad12002-12-28 20:37:16 +0000445//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000446// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000447//===----------------------------------------------------------------------===//
448
Chris Lattner1612faa2008-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 Greene3f2bf852009-11-12 20:49:22 +0000455 bool Immutable, bool isSS) {
Chris Lattner1612faa2008-01-25 07:19:06 +0000456 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
David Greene3f2bf852009-11-12 20:49:22 +0000457 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
458 isSS));
Chris Lattner1612faa2008-01-25 07:19:06 +0000459 return -++NumFixedObjects;
460}
461
462
Jakob Stoklund Olesen4a0f08c2009-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 Lattnerd74c5562009-08-23 01:12:47 +0000494void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman0ba90f32009-10-31 20:19:03 +0000495 if (Objects.empty()) return;
496
Matthijs Kooijman06140882008-11-03 11:16:43 +0000497 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
498 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000499
Dan Gohman0ba90f32009-10-31 20:19:03 +0000500 OS << "Frame Objects:\n";
501
Chris Lattner955fad12002-12-28 20:37:16 +0000502 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
503 const StackObject &SO = Objects[i];
Dan Gohman0ba90f32009-10-31 20:19:03 +0000504 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengd3653122008-02-27 03:04:06 +0000505 if (SO.Size == ~0ULL) {
506 OS << "dead\n";
507 continue;
508 }
Chris Lattner955fad12002-12-28 20:37:16 +0000509 if (SO.Size == 0)
510 OS << "variable sized";
511 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000512 OS << "size=" << SO.Size;
513 OS << ", align=" << SO.Alignment;
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000514
Chris Lattner955fad12002-12-28 20:37:16 +0000515 if (i < NumFixedObjects)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000516 OS << ", fixed";
Chris Lattner955fad12002-12-28 20:37:16 +0000517 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000518 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000519 OS << ", at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000520 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000521 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000522 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000523 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000524 OS << "]";
525 }
526 OS << "\n";
527 }
Chris Lattner955fad12002-12-28 20:37:16 +0000528}
529
Chris Lattner9085d8a2003-01-16 18:35:57 +0000530void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greenedc554812010-01-04 23:39:17 +0000531 print(MF, dbgs());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000532}
Chris Lattner955fad12002-12-28 20:37:16 +0000533
Chris Lattner955fad12002-12-28 20:37:16 +0000534//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000535// MachineJumpTableInfo implementation
536//===----------------------------------------------------------------------===//
537
Chris Lattner071c62f2010-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:
Chris Lattner85fe0782010-01-26 04:05:28 +0000547 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000548 return 4;
549 }
550 assert(0 && "Unknown jump table encoding!");
551 return ~0;
552}
553
554/// getEntryAlignment - Return the alignment of each entry in the jump table.
555unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
556 // The alignment of a jump table entry is the alignment of int32 unless the
557 // entry is just the address of a block, in which case it is the pointer
558 // alignment.
559 switch (getEntryKind()) {
560 case MachineJumpTableInfo::EK_BlockAddress:
561 return TD.getPointerABIAlignment();
562 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
563 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000564 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000565 return TD.getABIIntegerTypeAlignment(32);
566 }
567 assert(0 && "Unknown jump table encoding!");
568 return ~0;
569}
570
Nate Begeman37efe672006-04-22 18:53:45 +0000571/// getJumpTableIndex - Create a new jump table entry in the jump table info
572/// or return an existing one.
573///
574unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000575 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000576 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000577 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
578 return JumpTables.size()-1;
579}
580
Dan Gohman593ea052009-04-15 01:18:49 +0000581/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
582/// the jump tables to branch to New instead.
583bool
584MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
585 MachineBasicBlock *New) {
586 assert(Old != New && "Not making a change?");
587 bool MadeChange = false;
Jim Grosbach68bb60f2009-11-14 20:09:13 +0000588 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
589 ReplaceMBBInJumpTable(i, Old, New);
590 return MadeChange;
591}
592
593/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
594/// the jump table to branch to New instead.
595bool
596MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
597 MachineBasicBlock *Old,
598 MachineBasicBlock *New) {
599 assert(Old != New && "Not making a change?");
600 bool MadeChange = false;
601 MachineJumpTableEntry &JTE = JumpTables[Idx];
602 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
603 if (JTE.MBBs[j] == Old) {
604 JTE.MBBs[j] = New;
605 MadeChange = true;
606 }
Dan Gohman593ea052009-04-15 01:18:49 +0000607 return MadeChange;
608}
Nate Begeman37efe672006-04-22 18:53:45 +0000609
Chris Lattnerd74c5562009-08-23 01:12:47 +0000610void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000611 if (JumpTables.empty()) return;
612
613 OS << "Jump Tables:\n";
614
Nate Begeman37efe672006-04-22 18:53:45 +0000615 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000616 OS << " jt#" << i << ": ";
617 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
618 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Nate Begeman37efe672006-04-22 18:53:45 +0000619 }
Dan Gohman0ba90f32009-10-31 20:19:03 +0000620
621 OS << '\n';
Nate Begeman37efe672006-04-22 18:53:45 +0000622}
623
David Greenedc554812010-01-04 23:39:17 +0000624void MachineJumpTableInfo::dump() const { print(dbgs()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000625
626
627//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000628// MachineConstantPool implementation
629//===----------------------------------------------------------------------===//
630
Evan Cheng9abd7c32006-09-14 05:50:57 +0000631const Type *MachineConstantPoolEntry::getType() const {
632 if (isMachineConstantPoolEntry())
Chris Lattnercb459632009-07-21 23:34:23 +0000633 return Val.MachineCPVal->getType();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000634 return Val.ConstVal->getType();
635}
636
Chris Lattnercb459632009-07-21 23:34:23 +0000637
Chris Lattner354c0162009-07-21 23:36:01 +0000638unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattnercb459632009-07-21 23:34:23 +0000639 if (isMachineConstantPoolEntry())
Chris Lattner354c0162009-07-21 23:36:01 +0000640 return Val.MachineCPVal->getRelocationInfo();
Chris Lattner7cf12c72009-07-22 00:05:44 +0000641 return Val.ConstVal->getRelocationInfo();
Chris Lattnercb459632009-07-21 23:34:23 +0000642}
643
Evan Chengd6594ae2006-09-12 21:00:35 +0000644MachineConstantPool::~MachineConstantPool() {
645 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
646 if (Constants[i].isMachineConstantPoolEntry())
647 delete Constants[i].Val.MachineCPVal;
648}
649
Dan Gohman83f61202009-10-28 01:12:16 +0000650/// CanShareConstantPoolEntry - Test whether the given two constants
651/// can be allocated the same constant pool entry.
652static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
653 const TargetData *TD) {
654 // Handle the trivial case quickly.
655 if (A == B) return true;
656
657 // If they have the same type but weren't the same constant, quickly
658 // reject them.
659 if (A->getType() == B->getType()) return false;
660
661 // For now, only support constants with the same size.
662 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
663 return false;
664
665 // If a floating-point value and an integer value have the same encoding,
666 // they can share a constant-pool entry.
667 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
668 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
669 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
670 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
671 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
672 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
673
674 // Two vectors can share an entry if each pair of corresponding
675 // elements could.
676 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
677 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
678 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
679 return false;
680 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
Dan Gohman60d686d2009-10-31 14:12:53 +0000681 if (!CanShareConstantPoolEntry(AV->getOperand(i),
682 BV->getOperand(i), TD))
Dan Gohman83f61202009-10-28 01:12:16 +0000683 return false;
684 return true;
685 }
686
687 // TODO: Handle other cases.
688
689 return false;
690}
691
Chris Lattner3029f922006-02-09 04:46:04 +0000692/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000693/// an existing one. User must specify the log2 of the minimum required
694/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000695///
696unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
697 unsigned Alignment) {
698 assert(Alignment && "Alignment must be specified!");
699 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman83f61202009-10-28 01:12:16 +0000700
Chris Lattner3029f922006-02-09 04:46:04 +0000701 // Check to see if we already have this constant.
702 //
703 // FIXME, this could be made much more efficient for large constant pools.
Chris Lattner3029f922006-02-09 04:46:04 +0000704 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman83f61202009-10-28 01:12:16 +0000705 if (!Constants[i].isMachineConstantPoolEntry() &&
706 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
707 if ((unsigned)Constants[i].getAlignment() < Alignment)
708 Constants[i].Alignment = Alignment;
Chris Lattner3029f922006-02-09 04:46:04 +0000709 return i;
Dan Gohman83f61202009-10-28 01:12:16 +0000710 }
Chris Lattner3029f922006-02-09 04:46:04 +0000711
Evan Cheng1606e8e2009-03-13 07:51:59 +0000712 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +0000713 return Constants.size()-1;
714}
715
Evan Chengd6594ae2006-09-12 21:00:35 +0000716unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
717 unsigned Alignment) {
718 assert(Alignment && "Alignment must be specified!");
719 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
720
721 // Check to see if we already have this constant.
722 //
723 // FIXME, this could be made much more efficient for large constant pools.
Evan Chengd6594ae2006-09-12 21:00:35 +0000724 int Idx = V->getExistingMachineCPValue(this, Alignment);
725 if (Idx != -1)
726 return (unsigned)Idx;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000727
728 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +0000729 return Constants.size()-1;
730}
731
Chris Lattner62ca3252008-08-23 22:53:13 +0000732void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000733 if (Constants.empty()) return;
734
735 OS << "Constant Pool:\n";
Evan Chengb8973bd2006-01-31 22:23:14 +0000736 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000737 OS << " cp#" << i << ": ";
Evan Chengd6594ae2006-09-12 21:00:35 +0000738 if (Constants[i].isMachineConstantPoolEntry())
739 Constants[i].Val.MachineCPVal->print(OS);
740 else
741 OS << *(Value*)Constants[i].Val.ConstVal;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000742 OS << ", align=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +0000743 OS << "\n";
744 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000745}
746
David Greenedc554812010-01-04 23:39:17 +0000747void MachineConstantPool::dump() const { print(dbgs()); }