blob: 1e3e3145a3704a3e8397a752789682c4995fed22 [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
Jim Laskeyacd80ac2006-12-14 19:17:33 +000099 // Set up jump table.
100 const TargetData &TD = *TM.getTargetData();
101 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
102 unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
Owen Anderson1d0be152009-08-13 21:58:54 +0000103 unsigned TyAlignment = IsPic ?
104 TD.getABITypeAlignment(Type::getInt32Ty(F->getContext()))
Bill Wendlingdd37b362009-06-25 00:32:48 +0000105 : TD.getPointerABIAlignment();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000106 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
Bill Wendlingdd37b362009-06-25 00:32:48 +0000107 MachineJumpTableInfo(EntrySize, TyAlignment);
Chris Lattner831fdcf2002-12-25 05:03:22 +0000108}
109
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000110MachineFunction::~MachineFunction() {
Chris Lattner4b9a4002004-07-01 06:29:07 +0000111 BasicBlocks.clear();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000112 InstructionRecycler.clear(Allocator);
113 BasicBlockRecycler.clear(Allocator);
Bill Wendlingdd37b362009-06-25 00:32:48 +0000114 if (RegInfo) {
115 RegInfo->~MachineRegisterInfo();
116 Allocator.Deallocate(RegInfo);
117 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000118 if (MFInfo) {
Bill Wendlingdd37b362009-06-25 00:32:48 +0000119 MFInfo->~MachineFunctionInfo();
120 Allocator.Deallocate(MFInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000121 }
122 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
123 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
124 JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
Chris Lattner10491642002-10-30 00:48:05 +0000125}
126
Chris Lattnere70cab02006-10-03 19:18:57 +0000127
128/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
129/// recomputes them. This guarantees that the MBB numbers are sequential,
130/// dense, and match the ordering of the blocks within the function. If a
131/// specific MachineBasicBlock is specified, only that block and those after
132/// it are renumbered.
133void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
134 if (empty()) { MBBNumbering.clear(); return; }
135 MachineFunction::iterator MBBI, E = end();
136 if (MBB == 0)
137 MBBI = begin();
138 else
139 MBBI = MBB;
140
141 // Figure out the block number this should have.
142 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000143 if (MBBI != begin())
144 BlockNo = prior(MBBI)->getNumber()+1;
Chris Lattnere70cab02006-10-03 19:18:57 +0000145
146 for (; MBBI != E; ++MBBI, ++BlockNo) {
147 if (MBBI->getNumber() != (int)BlockNo) {
148 // Remove use of the old number.
149 if (MBBI->getNumber() != -1) {
150 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
151 "MBB number mismatch!");
152 MBBNumbering[MBBI->getNumber()] = 0;
153 }
154
155 // If BlockNo is already taken, set that block's number to -1.
156 if (MBBNumbering[BlockNo])
157 MBBNumbering[BlockNo]->setNumber(-1);
158
159 MBBNumbering[BlockNo] = MBBI;
160 MBBI->setNumber(BlockNo);
161 }
162 }
163
164 // Okay, all the blocks are renumbered. If we have compactified the block
165 // numbering, shrink MBBNumbering now.
166 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
167 MBBNumbering.resize(BlockNo);
168}
169
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000170/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
171/// of `new MachineInstr'.
172///
173MachineInstr *
Bill Wendling9bc96a52009-02-03 00:55:04 +0000174MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
175 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000176 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Bill Wendling9bc96a52009-02-03 00:55:04 +0000177 MachineInstr(TID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000178}
Chris Lattnere70cab02006-10-03 19:18:57 +0000179
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000180/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
181/// 'Orig' instruction, identical in all ways except the the instruction
182/// has no parent, prev, or next.
183///
184MachineInstr *
185MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
186 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
187 MachineInstr(*this, *Orig);
188}
189
190/// DeleteMachineInstr - Delete the given MachineInstr.
191///
192void
193MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000194 MI->~MachineInstr();
195 InstructionRecycler.Deallocate(Allocator, MI);
196}
197
198/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
199/// instead of `new MachineBasicBlock'.
200///
201MachineBasicBlock *
202MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
203 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
204 MachineBasicBlock(*this, bb);
205}
206
207/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
208///
209void
210MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
211 assert(MBB->getParent() == this && "MBB parent mismatch!");
212 MBB->~MachineBasicBlock();
213 BasicBlockRecycler.Deallocate(Allocator, MBB);
214}
215
Dan Gohmanc76909a2009-09-25 20:36:54 +0000216MachineMemOperand *
217MachineFunction::getMachineMemOperand(const Value *v, unsigned f,
218 int64_t o, uint64_t s,
219 unsigned base_alignment) {
220 return new (Allocator.Allocate<MachineMemOperand>())
221 MachineMemOperand(v, f, o, s, base_alignment);
222}
223
224MachineMemOperand *
225MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
226 int64_t Offset, uint64_t Size) {
227 return new (Allocator.Allocate<MachineMemOperand>())
228 MachineMemOperand(MMO->getValue(), MMO->getFlags(),
229 int64_t(uint64_t(MMO->getOffset()) +
230 uint64_t(Offset)),
231 Size, MMO->getBaseAlignment());
232}
233
234MachineInstr::mmo_iterator
235MachineFunction::allocateMemRefsArray(unsigned long Num) {
236 return Allocator.Allocate<MachineMemOperand *>(Num);
237}
238
Dan Gohman91e69c32009-10-09 18:10:05 +0000239std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
240MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
241 MachineInstr::mmo_iterator End) {
242 // Count the number of load mem refs.
243 unsigned Num = 0;
244 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
245 if ((*I)->isLoad())
246 ++Num;
247
248 // Allocate a new array and populate it with the load information.
249 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
250 unsigned Index = 0;
251 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
252 if ((*I)->isLoad()) {
253 if (!(*I)->isStore())
254 // Reuse the MMO.
255 Result[Index] = *I;
256 else {
257 // Clone the MMO and unset the store flag.
258 MachineMemOperand *JustLoad =
259 getMachineMemOperand((*I)->getValue(),
260 (*I)->getFlags() & ~MachineMemOperand::MOStore,
261 (*I)->getOffset(), (*I)->getSize(),
262 (*I)->getBaseAlignment());
263 Result[Index] = JustLoad;
264 }
265 ++Index;
266 }
267 }
268 return std::make_pair(Result, Result + Num);
269}
270
271std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
272MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
273 MachineInstr::mmo_iterator End) {
274 // Count the number of load mem refs.
275 unsigned Num = 0;
276 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
277 if ((*I)->isStore())
278 ++Num;
279
280 // Allocate a new array and populate it with the store information.
281 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
282 unsigned Index = 0;
283 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
284 if ((*I)->isStore()) {
285 if (!(*I)->isLoad())
286 // Reuse the MMO.
287 Result[Index] = *I;
288 else {
289 // Clone the MMO and unset the load flag.
290 MachineMemOperand *JustStore =
291 getMachineMemOperand((*I)->getValue(),
292 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
293 (*I)->getOffset(), (*I)->getSize(),
294 (*I)->getBaseAlignment());
295 Result[Index] = JustStore;
296 }
297 ++Index;
298 }
299 }
300 return std::make_pair(Result, Result + Num);
301}
302
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000303void MachineFunction::dump() const {
David Greenedc554812010-01-04 23:39:17 +0000304 print(dbgs());
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000305}
Chris Lattner10491642002-10-30 00:48:05 +0000306
Chris Lattnerd74c5562009-08-23 01:12:47 +0000307void MachineFunction::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000308 OS << "# Machine code for function " << Fn->getName() << ":\n";
Chris Lattner955fad12002-12-28 20:37:16 +0000309
310 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000311 FrameInfo->print(*this, OS);
Nate Begeman37efe672006-04-22 18:53:45 +0000312
313 // Print JumpTable Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000314 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000315
316 // Print Constant Pool
Chris Lattnerd74c5562009-08-23 01:12:47 +0000317 ConstantPool->print(OS);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000318
Dan Gohman6f0d0242008-02-10 18:45:23 +0000319 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000320
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000321 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000322 OS << "Function Live Ins: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000323 for (MachineRegisterInfo::livein_iterator
324 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Dan Gohman6f0d0242008-02-10 18:45:23 +0000325 if (TRI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000326 OS << "%" << TRI->getName(I->first);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000327 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000328 OS << " %physreg" << I->first;
Chris Lattner4e920272006-05-16 05:55:30 +0000329
330 if (I->second)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000331 OS << " in reg%" << I->second;
332
Chris Lattner7896c9f2009-12-03 00:50:42 +0000333 if (llvm::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000334 OS << ", ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000335 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000336 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000337 }
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000338 if (RegInfo && !RegInfo->liveout_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000339 OS << "Function Live Outs: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000340 for (MachineRegisterInfo::liveout_iterator
Dan Gohman0ba90f32009-10-31 20:19:03 +0000341 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
Dan Gohman6f0d0242008-02-10 18:45:23 +0000342 if (TRI)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000343 OS << '%' << TRI->getName(*I);
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000344 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000345 OS << "%physreg" << *I;
346
Chris Lattner7896c9f2009-12-03 00:50:42 +0000347 if (llvm::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000348 OS << " ";
349 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000350 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000351 }
352
Dan Gohman0ba90f32009-10-31 20:19:03 +0000353 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
354 OS << '\n';
Chris Lattner2d8e3d22009-08-23 00:47:04 +0000355 BB->print(OS);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000356 }
Brian Gaeke47b71642004-03-29 21:58:31 +0000357
Dan Gohman0ba90f32009-10-31 20:19:03 +0000358 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000359}
360
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000361namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000362 template<>
363 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +0000364
365 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
366
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000367 static std::string getGraphName(const MachineFunction *F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000368 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000369 }
370
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000371 std::string getNodeLabel(const MachineBasicBlock *Node,
372 const MachineFunction *Graph) {
373 if (isSimple () && Node->getBasicBlock() &&
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000374 !Node->getBasicBlock()->getName().empty())
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000375 return Node->getBasicBlock()->getNameStr() + ":";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000376
Chris Lattnercf143a42009-08-23 03:13:20 +0000377 std::string OutStr;
378 {
379 raw_string_ostream OSS(OutStr);
380
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000381 if (isSimple())
Chris Lattnercf143a42009-08-23 03:13:20 +0000382 OSS << Node->getNumber() << ':';
383 else
384 Node->print(OSS);
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000385 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000386
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000387 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
388
389 // Process string output to make it nicer...
390 for (unsigned i = 0; i != OutStr.length(); ++i)
391 if (OutStr[i] == '\n') { // Left justify
392 OutStr[i] = '\\';
393 OutStr.insert(OutStr.begin()+i+1, 'l');
394 }
395 return OutStr;
396 }
397 };
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000398}
399
400void MachineFunction::viewCFG() const
401{
Jim Laskey851a22d2005-10-12 12:09:05 +0000402#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000403 ViewGraph(this, "mf" + getFunction()->getNameStr());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000404#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000405 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
406 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000407#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000408}
409
410void MachineFunction::viewCFGOnly() const
411{
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000412#ifndef NDEBUG
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000413 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000414#else
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000415 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
416 << "systems with Graphviz or gv!\n";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000417#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000418}
419
Bob Wilson998e1252009-04-20 18:36:57 +0000420/// addLiveIn - Add the specified physical register as a live-in value and
421/// create a corresponding virtual register for it.
422unsigned MachineFunction::addLiveIn(unsigned PReg,
423 const TargetRegisterClass *RC) {
424 assert(RC->contains(PReg) && "Not the correct regclass!");
425 unsigned VReg = getRegInfo().createVirtualRegister(RC);
426 getRegInfo().addLiveIn(PReg, VReg);
427 return VReg;
428}
429
Devang Patel6b61f582010-01-16 06:09:35 +0000430/// getDILocation - Get the DILocation for a given DebugLoc object.
431DILocation MachineFunction::getDILocation(DebugLoc DL) const {
Bill Wendling44f6ac62009-02-03 22:55:54 +0000432 unsigned Idx = DL.getIndex();
Bill Wendling85e3af92009-02-03 22:49:58 +0000433 assert(Idx < DebugLocInfo.DebugLocations.size() &&
434 "Invalid index into debug locations!");
Devang Patel6b61f582010-01-16 06:09:35 +0000435 return DILocation(DebugLocInfo.DebugLocations[Idx]);
Bill Wendling85e3af92009-02-03 22:49:58 +0000436}
437
Chris Lattner955fad12002-12-28 20:37:16 +0000438//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000439// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000440//===----------------------------------------------------------------------===//
441
Chris Lattner1612faa2008-01-25 07:19:06 +0000442/// CreateFixedObject - Create a new object at a fixed location on the stack.
443/// All fixed objects should be created before other objects are created for
444/// efficiency. By default, fixed objects are immutable. This returns an
445/// index with a negative value.
446///
447int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
David Greene3f2bf852009-11-12 20:49:22 +0000448 bool Immutable, bool isSS) {
Chris Lattner1612faa2008-01-25 07:19:06 +0000449 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
David Greene3f2bf852009-11-12 20:49:22 +0000450 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
451 isSS));
Chris Lattner1612faa2008-01-25 07:19:06 +0000452 return -++NumFixedObjects;
453}
454
455
Jakob Stoklund Olesen4a0f08c2009-08-13 16:19:33 +0000456BitVector
457MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
458 assert(MBB && "MBB must be valid");
459 const MachineFunction *MF = MBB->getParent();
460 assert(MF && "MBB must be part of a MachineFunction");
461 const TargetMachine &TM = MF->getTarget();
462 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
463 BitVector BV(TRI->getNumRegs());
464
465 // Before CSI is calculated, no registers are considered pristine. They can be
466 // freely used and PEI will make sure they are saved.
467 if (!isCalleeSavedInfoValid())
468 return BV;
469
470 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
471 BV.set(*CSR);
472
473 // The entry MBB always has all CSRs pristine.
474 if (MBB == &MF->front())
475 return BV;
476
477 // On other MBBs the saved CSRs are not pristine.
478 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
479 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
480 E = CSI.end(); I != E; ++I)
481 BV.reset(I->getReg());
482
483 return BV;
484}
485
486
Chris Lattnerd74c5562009-08-23 01:12:47 +0000487void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman0ba90f32009-10-31 20:19:03 +0000488 if (Objects.empty()) return;
489
Matthijs Kooijman06140882008-11-03 11:16:43 +0000490 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
491 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000492
Dan Gohman0ba90f32009-10-31 20:19:03 +0000493 OS << "Frame Objects:\n";
494
Chris Lattner955fad12002-12-28 20:37:16 +0000495 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
496 const StackObject &SO = Objects[i];
Dan Gohman0ba90f32009-10-31 20:19:03 +0000497 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengd3653122008-02-27 03:04:06 +0000498 if (SO.Size == ~0ULL) {
499 OS << "dead\n";
500 continue;
501 }
Chris Lattner955fad12002-12-28 20:37:16 +0000502 if (SO.Size == 0)
503 OS << "variable sized";
504 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000505 OS << "size=" << SO.Size;
506 OS << ", align=" << SO.Alignment;
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000507
Chris Lattner955fad12002-12-28 20:37:16 +0000508 if (i < NumFixedObjects)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000509 OS << ", fixed";
Chris Lattner955fad12002-12-28 20:37:16 +0000510 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000511 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000512 OS << ", at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000513 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000514 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000515 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000516 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000517 OS << "]";
518 }
519 OS << "\n";
520 }
Chris Lattner955fad12002-12-28 20:37:16 +0000521}
522
Chris Lattner9085d8a2003-01-16 18:35:57 +0000523void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greenedc554812010-01-04 23:39:17 +0000524 print(MF, dbgs());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000525}
Chris Lattner955fad12002-12-28 20:37:16 +0000526
Chris Lattner955fad12002-12-28 20:37:16 +0000527//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000528// MachineJumpTableInfo implementation
529//===----------------------------------------------------------------------===//
530
531/// getJumpTableIndex - Create a new jump table entry in the jump table info
532/// or return an existing one.
533///
534unsigned MachineJumpTableInfo::getJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000535 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000536 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000537 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
538 return JumpTables.size()-1;
539}
540
Dan Gohman593ea052009-04-15 01:18:49 +0000541/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
542/// the jump tables to branch to New instead.
543bool
544MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
545 MachineBasicBlock *New) {
546 assert(Old != New && "Not making a change?");
547 bool MadeChange = false;
Jim Grosbach68bb60f2009-11-14 20:09:13 +0000548 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
549 ReplaceMBBInJumpTable(i, Old, New);
550 return MadeChange;
551}
552
553/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
554/// the jump table to branch to New instead.
555bool
556MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
557 MachineBasicBlock *Old,
558 MachineBasicBlock *New) {
559 assert(Old != New && "Not making a change?");
560 bool MadeChange = false;
561 MachineJumpTableEntry &JTE = JumpTables[Idx];
562 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
563 if (JTE.MBBs[j] == Old) {
564 JTE.MBBs[j] = New;
565 MadeChange = true;
566 }
Dan Gohman593ea052009-04-15 01:18:49 +0000567 return MadeChange;
568}
Nate Begeman37efe672006-04-22 18:53:45 +0000569
Chris Lattnerd74c5562009-08-23 01:12:47 +0000570void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000571 if (JumpTables.empty()) return;
572
573 OS << "Jump Tables:\n";
574
Nate Begeman37efe672006-04-22 18:53:45 +0000575 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000576 OS << " jt#" << i << ": ";
577 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
578 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Nate Begeman37efe672006-04-22 18:53:45 +0000579 }
Dan Gohman0ba90f32009-10-31 20:19:03 +0000580
581 OS << '\n';
Nate Begeman37efe672006-04-22 18:53:45 +0000582}
583
David Greenedc554812010-01-04 23:39:17 +0000584void MachineJumpTableInfo::dump() const { print(dbgs()); }
Nate Begeman37efe672006-04-22 18:53:45 +0000585
586
587//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000588// MachineConstantPool implementation
589//===----------------------------------------------------------------------===//
590
Evan Cheng9abd7c32006-09-14 05:50:57 +0000591const Type *MachineConstantPoolEntry::getType() const {
592 if (isMachineConstantPoolEntry())
Chris Lattnercb459632009-07-21 23:34:23 +0000593 return Val.MachineCPVal->getType();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000594 return Val.ConstVal->getType();
595}
596
Chris Lattnercb459632009-07-21 23:34:23 +0000597
Chris Lattner354c0162009-07-21 23:36:01 +0000598unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattnercb459632009-07-21 23:34:23 +0000599 if (isMachineConstantPoolEntry())
Chris Lattner354c0162009-07-21 23:36:01 +0000600 return Val.MachineCPVal->getRelocationInfo();
Chris Lattner7cf12c72009-07-22 00:05:44 +0000601 return Val.ConstVal->getRelocationInfo();
Chris Lattnercb459632009-07-21 23:34:23 +0000602}
603
Evan Chengd6594ae2006-09-12 21:00:35 +0000604MachineConstantPool::~MachineConstantPool() {
605 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
606 if (Constants[i].isMachineConstantPoolEntry())
607 delete Constants[i].Val.MachineCPVal;
608}
609
Dan Gohman83f61202009-10-28 01:12:16 +0000610/// CanShareConstantPoolEntry - Test whether the given two constants
611/// can be allocated the same constant pool entry.
612static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
613 const TargetData *TD) {
614 // Handle the trivial case quickly.
615 if (A == B) return true;
616
617 // If they have the same type but weren't the same constant, quickly
618 // reject them.
619 if (A->getType() == B->getType()) return false;
620
621 // For now, only support constants with the same size.
622 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
623 return false;
624
625 // If a floating-point value and an integer value have the same encoding,
626 // they can share a constant-pool entry.
627 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
628 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
629 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
630 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
631 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
632 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
633
634 // Two vectors can share an entry if each pair of corresponding
635 // elements could.
636 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
637 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
638 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
639 return false;
640 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
Dan Gohman60d686d2009-10-31 14:12:53 +0000641 if (!CanShareConstantPoolEntry(AV->getOperand(i),
642 BV->getOperand(i), TD))
Dan Gohman83f61202009-10-28 01:12:16 +0000643 return false;
644 return true;
645 }
646
647 // TODO: Handle other cases.
648
649 return false;
650}
651
Chris Lattner3029f922006-02-09 04:46:04 +0000652/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000653/// an existing one. User must specify the log2 of the minimum required
654/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000655///
656unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
657 unsigned Alignment) {
658 assert(Alignment && "Alignment must be specified!");
659 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman83f61202009-10-28 01:12:16 +0000660
Chris Lattner3029f922006-02-09 04:46:04 +0000661 // Check to see if we already have this constant.
662 //
663 // FIXME, this could be made much more efficient for large constant pools.
Chris Lattner3029f922006-02-09 04:46:04 +0000664 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman83f61202009-10-28 01:12:16 +0000665 if (!Constants[i].isMachineConstantPoolEntry() &&
666 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
667 if ((unsigned)Constants[i].getAlignment() < Alignment)
668 Constants[i].Alignment = Alignment;
Chris Lattner3029f922006-02-09 04:46:04 +0000669 return i;
Dan Gohman83f61202009-10-28 01:12:16 +0000670 }
Chris Lattner3029f922006-02-09 04:46:04 +0000671
Evan Cheng1606e8e2009-03-13 07:51:59 +0000672 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +0000673 return Constants.size()-1;
674}
675
Evan Chengd6594ae2006-09-12 21:00:35 +0000676unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
677 unsigned Alignment) {
678 assert(Alignment && "Alignment must be specified!");
679 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
680
681 // Check to see if we already have this constant.
682 //
683 // FIXME, this could be made much more efficient for large constant pools.
Evan Chengd6594ae2006-09-12 21:00:35 +0000684 int Idx = V->getExistingMachineCPValue(this, Alignment);
685 if (Idx != -1)
686 return (unsigned)Idx;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000687
688 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +0000689 return Constants.size()-1;
690}
691
Chris Lattner62ca3252008-08-23 22:53:13 +0000692void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000693 if (Constants.empty()) return;
694
695 OS << "Constant Pool:\n";
Evan Chengb8973bd2006-01-31 22:23:14 +0000696 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000697 OS << " cp#" << i << ": ";
Evan Chengd6594ae2006-09-12 21:00:35 +0000698 if (Constants[i].isMachineConstantPoolEntry())
699 Constants[i].Val.MachineCPVal->print(OS);
700 else
701 OS << *(Value*)Constants[i].Val.ConstVal;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000702 OS << ", align=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +0000703 OS << "\n";
704 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000705}
706
David Greenedc554812010-01-04 23:39:17 +0000707void MachineConstantPool::dump() const { print(dbgs()); }