blob: edc6d8dc6fa998d99f1abbedd6c34295d9f66cdc [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"
Bill Wendling20c568f2009-06-30 22:38:32 +000019#include "llvm/Config/config.h"
Chris Lattner4d149cd2003-01-13 00:23:03 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greene098612b2009-08-19 22:16:11 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman37efe672006-04-22 18:53:45 +000025#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner16c45e92003-12-20 10:20:58 +000027#include "llvm/CodeGen/Passes.h"
Chris Lattnerbeeb93e2010-01-26 05:58:28 +000028#include "llvm/MC/MCAsmInfo.h"
29#include "llvm/MC/MCContext.h"
Devang Patelc99fd872010-01-19 06:09:04 +000030#include "llvm/Analysis/DebugInfo.h"
David Greenedc554812010-01-04 23:39:17 +000031#include "llvm/Support/Debug.h"
Owen Anderson07000c62006-05-12 06:33:49 +000032#include "llvm/Target/TargetData.h"
Bill Wendling20c568f2009-06-30 22:38:32 +000033#include "llvm/Target/TargetLowering.h"
Chris Lattnerf2868ce2002-02-03 07:54:50 +000034#include "llvm/Target/TargetMachine.h"
Chris Lattner8bd66e62002-12-28 21:00:25 +000035#include "llvm/Target/TargetFrameInfo.h"
Chris Lattnerbeeb93e2010-01-26 05:58:28 +000036#include "llvm/ADT/SmallString.h"
37#include "llvm/ADT/STLExtras.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000038#include "llvm/Support/GraphWriter.h"
Chris Lattner944fac72008-08-23 22:23:09 +000039#include "llvm/Support/raw_ostream.h"
Chris Lattner07f32d42003-12-20 09:17:07 +000040using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000041
Evan Chengcf5862d2010-04-02 19:29:15 +000042namespace {
43 struct Printer : public MachineFunctionPass {
44 static char ID;
45
46 raw_ostream &OS;
47 const std::string Banner;
48
49 Printer(raw_ostream &os, const std::string &banner)
50 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
51
52 const char *getPassName() const { return "MachineFunction Printer"; }
53
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.setPreservesAll();
56 MachineFunctionPass::getAnalysisUsage(AU);
57 }
58
59 bool runOnMachineFunction(MachineFunction &MF) {
60 OS << "# " << Banner << ":\n";
61 MF.print(OS);
62 return false;
63 }
64 };
65 char Printer::ID = 0;
66}
67
68/// Returns a newly-created MachineFunction Printer pass. The default banner is
69/// empty.
70///
71FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
72 const std::string &Banner){
73 return new Printer(OS, Banner);
74}
75
Chris Lattnerb84822f2010-01-26 04:35:26 +000076//===----------------------------------------------------------------------===//
Chris Lattner227c3d32002-10-28 01:12:41 +000077// MachineFunction implementation
Chris Lattnerb84822f2010-01-26 04:35:26 +000078//===----------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000079
Chris Lattnera70e2e32009-09-15 22:44:26 +000080// Out of line virtual method.
81MachineFunctionInfo::~MachineFunctionInfo() {}
82
Dan Gohmanfed90b62008-07-28 21:51:04 +000083void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +000084 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +000085}
Chris Lattner227c3d32002-10-28 01:12:41 +000086
Chris Lattnerb84822f2010-01-26 04:35:26 +000087MachineFunction::MachineFunction(Function *F, const TargetMachine &TM,
Chris Lattner11d53c12010-03-13 20:55:24 +000088 unsigned FunctionNum, MCContext &ctx)
89 : Fn(F), Target(TM), Ctx(ctx) {
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +000090 if (TM.getRegisterInfo())
Dan Gohman95531882010-03-18 18:49:47 +000091 RegInfo = new (Allocator) MachineRegisterInfo(*TM.getRegisterInfo());
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +000092 else
93 RegInfo = 0;
Chris Lattnerad828162004-08-16 22:36:34 +000094 MFInfo = 0;
Dan Gohman95531882010-03-18 18:49:47 +000095 FrameInfo = new (Allocator) MachineFrameInfo(*TM.getFrameInfo());
Charles Davis5dfa2672010-02-19 18:17:13 +000096 if (Fn->hasFnAttr(Attribute::StackAlignment))
97 FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs(
98 Fn->getAttributes().getFnAttributes()));
Dan Gohman95531882010-03-18 18:49:47 +000099 ConstantPool = new (Allocator) MachineConstantPool(TM.getTargetData());
Bill Wendling20c568f2009-06-30 22:38:32 +0000100 Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
Chris Lattnerb84822f2010-01-26 04:35:26 +0000101 FunctionNumber = FunctionNum;
Chris Lattner071c62f2010-01-25 23:26:13 +0000102 JumpTableInfo = 0;
Chris Lattner831fdcf2002-12-25 05:03:22 +0000103}
104
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000105MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000106 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000107 InstructionRecycler.clear(Allocator);
108 BasicBlockRecycler.clear(Allocator);
Bill Wendlingdd37b362009-06-25 00:32:48 +0000109 if (RegInfo) {
110 RegInfo->~MachineRegisterInfo();
111 Allocator.Deallocate(RegInfo);
112 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000113 if (MFInfo) {
Bill Wendlingdd37b362009-06-25 00:32:48 +0000114 MFInfo->~MachineFunctionInfo();
115 Allocator.Deallocate(MFInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000116 }
117 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
118 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
Chris Lattner071c62f2010-01-25 23:26:13 +0000119
120 if (JumpTableInfo) {
121 JumpTableInfo->~MachineJumpTableInfo();
122 Allocator.Deallocate(JumpTableInfo);
123 }
Chris Lattner10491642002-10-30 00:48:05 +0000124}
125
Chris Lattner071c62f2010-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 Gohman95531882010-03-18 18:49:47 +0000132 JumpTableInfo = new (Allocator)
Chris Lattner071c62f2010-01-25 23:26:13 +0000133 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
134 return JumpTableInfo;
135}
Chris Lattnere70cab02006-10-03 19:18:57 +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;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000152 if (MBBI != begin())
153 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000154
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 Gohman8e5f2c62008-07-07 23:14:23 +0000179/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
180/// of `new MachineInstr'.
181///
182MachineInstr *
Bill Wendling9bc96a52009-02-03 00:55:04 +0000183MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
184 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000185 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling9bc96a52009-02-03 00:55:04 +0000186 MachineInstr(TID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000187}
Chris Lattnere70cab02006-10-03 19:18:57 +0000188
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000189/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
Dan Gohmanf451cb82010-02-10 16:03:48 +0000190/// 'Orig' instruction, identical in all ways except the instruction
Dan Gohman8e5f2c62008-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 Gohman8e5f2c62008-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 Gohmanc76909a2009-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 Gohman95531882010-03-18 18:49:47 +0000229 return new (Allocator) MachineMemOperand(v, f, o, s, base_alignment);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000230}
231
232MachineMemOperand *
233MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
234 int64_t Offset, uint64_t Size) {
Dan Gohman95531882010-03-18 18:49:47 +0000235 return new (Allocator)
Dan Gohmanc76909a2009-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 Gohman91e69c32009-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 Gohman8e5f2c62008-07-07 23:14:23 +0000311void MachineFunction::dump() const {
David Greenedc554812010-01-04 23:39:17 +0000312 print(dbgs());
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000313}
Chris Lattner10491642002-10-30 00:48:05 +0000314
Chris Lattnerd74c5562009-08-23 01:12:47 +0000315void MachineFunction::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000316 OS << "# Machine code for function " << Fn->getName() << ":\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000317
318 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000319 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000320
321 // Print JumpTable Information
Chris Lattner071c62f2010-01-25 23:26:13 +0000322 if (JumpTableInfo)
323 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000324
325 // Print Constant Pool
Chris Lattnerd74c5562009-08-23 01:12:47 +0000326 ConstantPool->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000327
Dan Gohman6f0d0242008-02-10 18:45:23 +0000328 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000329
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000330 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000331 OS << "Function Live Ins: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000332 for (MachineRegisterInfo::livein_iterator
333 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000334 if (TRI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000335 OS << "%" << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000336 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000337 OS << " %physreg" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000338
339 if (I->second)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000340 OS << " in reg%" << I->second;
341
Chris Lattner7896c9f2009-12-03 00:50:42 +0000342 if (llvm::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000343 OS << ", ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000344 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000345 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000346 }
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000347 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000348 OS << "Function Live Outs: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000349 for (MachineRegisterInfo::liveout_iterator
Dan Gohman0ba90f32009-10-31 20:19:03 +0000350 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
Dan Gohman6f0d0242008-02-10 18:45:23 +0000351 if (TRI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000352 OS << '%' << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000353 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000354 OS << "%physreg" << *I;
355
Chris Lattner7896c9f2009-12-03 00:50:42 +0000356 if (llvm::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000357 OS << " ";
358 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000359 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000360 }
361
Dan Gohman0ba90f32009-10-31 20:19:03 +0000362 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
363 OS << '\n';
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000364 BB->print(OS);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000365 }
Brian Gaeke47b71642004-03-29 21:58:31 +0000366
Dan Gohman0ba90f32009-10-31 20:19:03 +0000367 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000368}
369
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000370namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000371 template<>
372 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +0000373
374 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
375
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000376 static std::string getGraphName(const MachineFunction *F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000377 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000378 }
379
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000380 std::string getNodeLabel(const MachineBasicBlock *Node,
381 const MachineFunction *Graph) {
382 if (isSimple () && Node->getBasicBlock() &&
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000383 !Node->getBasicBlock()->getName().empty())
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000384 return Node->getBasicBlock()->getNameStr() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000385
Chris Lattnercf143a42009-08-23 03:13:20 +0000386 std::string OutStr;
387 {
388 raw_string_ostream OSS(OutStr);
389
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000390 if (isSimple())
Chris Lattnercf143a42009-08-23 03:13:20 +0000391 OSS << Node->getNumber() << ':';
392 else
393 Node->print(OSS);
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000394 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000395
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +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 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000407}
408
409void MachineFunction::viewCFG() const
410{
Jim Laskey851a22d2005-10-12 12:09:05 +0000411#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000412 ViewGraph(this, "mf" + getFunction()->getNameStr());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000413#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000414 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
415 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000416#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000417}
418
419void MachineFunction::viewCFGOnly() const
420{
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000421#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000422 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000423#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000424 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
425 << "systems with Graphviz or gv!\n";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000426#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000427}
428
Bob Wilson998e1252009-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
Chris Lattner589c6f62010-01-26 06:28:43 +0000439/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
440/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
441/// normal 'L' label is returned.
442MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
443 bool isLinkerPrivate) const {
444 assert(JumpTableInfo && "No jump tables");
445
Chandler Carruthde4c0802010-01-27 10:27:10 +0000446 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
Chris Lattner589c6f62010-01-26 06:28:43 +0000447 const MCAsmInfo &MAI = *getTarget().getMCAsmInfo();
448
449 const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
450 MAI.getPrivateGlobalPrefix();
451 SmallString<60> Name;
452 raw_svector_ostream(Name)
453 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
Chris Lattner9b97a732010-03-30 18:10:53 +0000454 return Ctx.GetOrCreateSymbol(Name.str());
Chris Lattner589c6f62010-01-26 06:28:43 +0000455}
456
457
Chris Lattner955fad12002-12-28 20:37:16 +0000458//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000459// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000460//===----------------------------------------------------------------------===//
461
Chris Lattner1612faa2008-01-25 07:19:06 +0000462/// CreateFixedObject - Create a new object at a fixed location on the stack.
463/// All fixed objects should be created before other objects are created for
464/// efficiency. By default, fixed objects are immutable. This returns an
465/// index with a negative value.
466///
467int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
David Greene3f2bf852009-11-12 20:49:22 +0000468 bool Immutable, bool isSS) {
Chris Lattner1612faa2008-01-25 07:19:06 +0000469 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
David Greene3f2bf852009-11-12 20:49:22 +0000470 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
471 isSS));
Chris Lattner1612faa2008-01-25 07:19:06 +0000472 return -++NumFixedObjects;
473}
474
475
Jakob Stoklund Olesen4a0f08c2009-08-13 16:19:33 +0000476BitVector
477MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
478 assert(MBB && "MBB must be valid");
479 const MachineFunction *MF = MBB->getParent();
480 assert(MF && "MBB must be part of a MachineFunction");
481 const TargetMachine &TM = MF->getTarget();
482 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
483 BitVector BV(TRI->getNumRegs());
484
485 // Before CSI is calculated, no registers are considered pristine. They can be
486 // freely used and PEI will make sure they are saved.
487 if (!isCalleeSavedInfoValid())
488 return BV;
489
490 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
491 BV.set(*CSR);
492
493 // The entry MBB always has all CSRs pristine.
494 if (MBB == &MF->front())
495 return BV;
496
497 // On other MBBs the saved CSRs are not pristine.
498 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
499 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
500 E = CSI.end(); I != E; ++I)
501 BV.reset(I->getReg());
502
503 return BV;
504}
505
506
Chris Lattnerd74c5562009-08-23 01:12:47 +0000507void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman0ba90f32009-10-31 20:19:03 +0000508 if (Objects.empty()) return;
509
Matthijs Kooijman06140882008-11-03 11:16:43 +0000510 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
511 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000512
Dan Gohman0ba90f32009-10-31 20:19:03 +0000513 OS << "Frame Objects:\n";
514
Chris Lattner955fad12002-12-28 20:37:16 +0000515 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
516 const StackObject &SO = Objects[i];
Dan Gohman0ba90f32009-10-31 20:19:03 +0000517 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengd3653122008-02-27 03:04:06 +0000518 if (SO.Size == ~0ULL) {
519 OS << "dead\n";
520 continue;
521 }
Chris Lattner955fad12002-12-28 20:37:16 +0000522 if (SO.Size == 0)
523 OS << "variable sized";
524 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000525 OS << "size=" << SO.Size;
526 OS << ", align=" << SO.Alignment;
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000527
Chris Lattner955fad12002-12-28 20:37:16 +0000528 if (i < NumFixedObjects)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000529 OS << ", fixed";
Chris Lattner955fad12002-12-28 20:37:16 +0000530 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000531 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000532 OS << ", at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000533 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000534 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000535 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000536 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000537 OS << "]";
538 }
539 OS << "\n";
540 }
Chris Lattner955fad12002-12-28 20:37:16 +0000541}
542
Chris Lattner9085d8a2003-01-16 18:35:57 +0000543void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greenedc554812010-01-04 23:39:17 +0000544 print(MF, dbgs());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000545}
Chris Lattner955fad12002-12-28 20:37:16 +0000546
Chris Lattner955fad12002-12-28 20:37:16 +0000547//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000548// MachineJumpTableInfo implementation
549//===----------------------------------------------------------------------===//
550
Chris Lattner071c62f2010-01-25 23:26:13 +0000551/// getEntrySize - Return the size of each entry in the jump table.
552unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
553 // The size of a jump table entry is 4 bytes unless the entry is just the
554 // address of a block, in which case it is the pointer size.
555 switch (getEntryKind()) {
556 case MachineJumpTableInfo::EK_BlockAddress:
557 return TD.getPointerSize();
558 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
559 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000560 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000561 return 4;
Richard Osborne95da6052010-03-11 14:58:16 +0000562 case MachineJumpTableInfo::EK_Inline:
563 return 0;
Chris Lattner071c62f2010-01-25 23:26:13 +0000564 }
565 assert(0 && "Unknown jump table encoding!");
566 return ~0;
567}
568
569/// getEntryAlignment - Return the alignment of each entry in the jump table.
570unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
571 // The alignment of a jump table entry is the alignment of int32 unless the
572 // entry is just the address of a block, in which case it is the pointer
573 // alignment.
574 switch (getEntryKind()) {
575 case MachineJumpTableInfo::EK_BlockAddress:
576 return TD.getPointerABIAlignment();
577 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
578 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000579 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000580 return TD.getABIIntegerTypeAlignment(32);
Richard Osborne95da6052010-03-11 14:58:16 +0000581 case MachineJumpTableInfo::EK_Inline:
582 return 1;
Chris Lattner071c62f2010-01-25 23:26:13 +0000583 }
584 assert(0 && "Unknown jump table encoding!");
585 return ~0;
586}
587
Bob Wilsond1ec31d2010-03-18 18:42:41 +0000588/// createJumpTableIndex - Create a new jump table entry in the jump table info.
Nate Begeman37efe672006-04-22 18:53:45 +0000589///
Bob Wilsond1ec31d2010-03-18 18:42:41 +0000590unsigned MachineJumpTableInfo::createJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000591 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000592 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000593 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
594 return JumpTables.size()-1;
595}
596
Dan Gohman593ea052009-04-15 01:18:49 +0000597/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
598/// the jump tables to branch to New instead.
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000599bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
600 MachineBasicBlock *New) {
Dan Gohman593ea052009-04-15 01:18:49 +0000601 assert(Old != New && "Not making a change?");
602 bool MadeChange = false;
Jim Grosbach68bb60f2009-11-14 20:09:13 +0000603 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
604 ReplaceMBBInJumpTable(i, Old, New);
605 return MadeChange;
606}
607
608/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
609/// the jump table to branch to New instead.
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000610bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
611 MachineBasicBlock *Old,
612 MachineBasicBlock *New) {
Jim Grosbach68bb60f2009-11-14 20:09:13 +0000613 assert(Old != New && "Not making a change?");
614 bool MadeChange = false;
615 MachineJumpTableEntry &JTE = JumpTables[Idx];
616 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
617 if (JTE.MBBs[j] == Old) {
618 JTE.MBBs[j] = New;
619 MadeChange = true;
620 }
Dan Gohman593ea052009-04-15 01:18:49 +0000621 return MadeChange;
622}
Nate Begeman37efe672006-04-22 18:53:45 +0000623
Chris Lattnerd74c5562009-08-23 01:12:47 +0000624void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000625 if (JumpTables.empty()) return;
626
627 OS << "Jump Tables:\n";
628
Nate Begeman37efe672006-04-22 18:53:45 +0000629 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000630 OS << " jt#" << i << ": ";
631 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
632 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Nate Begeman37efe672006-04-22 18:53:45 +0000633 }
Dan Gohman0ba90f32009-10-31 20:19:03 +0000634
635 OS << '\n';
Nate Begeman37efe672006-04-22 18:53:45 +0000636}
637
David Greenedc554812010-01-04 23:39:17 +0000638void MachineJumpTableInfo::dump() const { print(dbgs()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000639
640
641//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000642// MachineConstantPool implementation
643//===----------------------------------------------------------------------===//
644
Evan Cheng9abd7c32006-09-14 05:50:57 +0000645const Type *MachineConstantPoolEntry::getType() const {
646 if (isMachineConstantPoolEntry())
Chris Lattnercb459632009-07-21 23:34:23 +0000647 return Val.MachineCPVal->getType();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000648 return Val.ConstVal->getType();
649}
650
Chris Lattnercb459632009-07-21 23:34:23 +0000651
Chris Lattner354c0162009-07-21 23:36:01 +0000652unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattnercb459632009-07-21 23:34:23 +0000653 if (isMachineConstantPoolEntry())
Chris Lattner354c0162009-07-21 23:36:01 +0000654 return Val.MachineCPVal->getRelocationInfo();
Chris Lattner7cf12c72009-07-22 00:05:44 +0000655 return Val.ConstVal->getRelocationInfo();
Chris Lattnercb459632009-07-21 23:34:23 +0000656}
657
Evan Chengd6594ae2006-09-12 21:00:35 +0000658MachineConstantPool::~MachineConstantPool() {
659 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
660 if (Constants[i].isMachineConstantPoolEntry())
661 delete Constants[i].Val.MachineCPVal;
662}
663
Dan Gohman83f61202009-10-28 01:12:16 +0000664/// CanShareConstantPoolEntry - Test whether the given two constants
665/// can be allocated the same constant pool entry.
666static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
667 const TargetData *TD) {
668 // Handle the trivial case quickly.
669 if (A == B) return true;
670
671 // If they have the same type but weren't the same constant, quickly
672 // reject them.
673 if (A->getType() == B->getType()) return false;
674
675 // For now, only support constants with the same size.
676 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
677 return false;
678
679 // If a floating-point value and an integer value have the same encoding,
680 // they can share a constant-pool entry.
681 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
682 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
683 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
684 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
685 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
686 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
687
688 // Two vectors can share an entry if each pair of corresponding
689 // elements could.
690 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
691 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
692 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
693 return false;
694 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
Dan Gohman60d686d2009-10-31 14:12:53 +0000695 if (!CanShareConstantPoolEntry(AV->getOperand(i),
696 BV->getOperand(i), TD))
Dan Gohman83f61202009-10-28 01:12:16 +0000697 return false;
698 return true;
699 }
700
701 // TODO: Handle other cases.
702
703 return false;
704}
705
Chris Lattner3029f922006-02-09 04:46:04 +0000706/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000707/// an existing one. User must specify the log2 of the minimum required
708/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000709///
710unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
711 unsigned Alignment) {
712 assert(Alignment && "Alignment must be specified!");
713 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman83f61202009-10-28 01:12:16 +0000714
Chris Lattner3029f922006-02-09 04:46:04 +0000715 // Check to see if we already have this constant.
716 //
717 // FIXME, this could be made much more efficient for large constant pools.
Chris Lattner3029f922006-02-09 04:46:04 +0000718 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman83f61202009-10-28 01:12:16 +0000719 if (!Constants[i].isMachineConstantPoolEntry() &&
720 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
721 if ((unsigned)Constants[i].getAlignment() < Alignment)
722 Constants[i].Alignment = Alignment;
Chris Lattner3029f922006-02-09 04:46:04 +0000723 return i;
Dan Gohman83f61202009-10-28 01:12:16 +0000724 }
Chris Lattner3029f922006-02-09 04:46:04 +0000725
Evan Cheng1606e8e2009-03-13 07:51:59 +0000726 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +0000727 return Constants.size()-1;
728}
729
Evan Chengd6594ae2006-09-12 21:00:35 +0000730unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
731 unsigned Alignment) {
732 assert(Alignment && "Alignment must be specified!");
733 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
734
735 // Check to see if we already have this constant.
736 //
737 // FIXME, this could be made much more efficient for large constant pools.
Evan Chengd6594ae2006-09-12 21:00:35 +0000738 int Idx = V->getExistingMachineCPValue(this, Alignment);
739 if (Idx != -1)
740 return (unsigned)Idx;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000741
742 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +0000743 return Constants.size()-1;
744}
745
Chris Lattner62ca3252008-08-23 22:53:13 +0000746void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000747 if (Constants.empty()) return;
748
749 OS << "Constant Pool:\n";
Evan Chengb8973bd2006-01-31 22:23:14 +0000750 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000751 OS << " cp#" << i << ": ";
Evan Chengd6594ae2006-09-12 21:00:35 +0000752 if (Constants[i].isMachineConstantPoolEntry())
753 Constants[i].Val.MachineCPVal->print(OS);
754 else
755 OS << *(Value*)Constants[i].Val.ConstVal;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000756 OS << ", align=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +0000757 OS << "\n";
758 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000759}
760
David Greenedc554812010-01-04 23:39:17 +0000761void MachineConstantPool::dump() const { print(dbgs()); }