blob: 8a2b610948b06f5e76d36815c270d3328f99c0c6 [file] [log] [blame]
Chris Lattner8494d082002-10-28 01:16:38 +00001//===-- MachineFunction.cpp -----------------------------------------------===//
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Evlogimenos58350a72004-09-05 18:41:35 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Alkis Evlogimenos58350a72004-09-05 18:41:35 +00009//
Chris Lattner8494d082002-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 Lattnereda6bd72002-02-03 07:54:50 +000015
David Greene8230a932009-08-19 22:16:11 +000016#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/Analysis/ConstantFolding.h"
Chris Lattnere32fad42012-01-27 01:47:28 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman4ca2ea52006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner9f53dcd2010-04-05 05:49:50 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner4cbb97b2003-12-20 10:20:58 +000027#include "llvm/CodeGen/Passes.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Function.h"
Chris Lattner273735b2010-01-26 05:58:28 +000031#include "llvm/MC/MCAsmInfo.h"
32#include "llvm/MC/MCContext.h"
David Greene66710112010-01-04 23:39:17 +000033#include "llvm/Support/Debug.h"
Chris Lattnerbd7286e2006-10-03 20:19:23 +000034#include "llvm/Support/GraphWriter.h"
Chris Lattner0c19df42008-08-23 22:23:09 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000036#include "llvm/Target/TargetFrameLowering.h"
37#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetMachine.h"
Eric Christopherd9134482014-08-04 21:25:23 +000039#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner9a3478e2003-12-20 09:17:07 +000040using namespace llvm;
Chris Lattnereda6bd72002-02-03 07:54:50 +000041
Chandler Carruthe96dd892014-04-21 22:55:11 +000042#define DEBUG_TYPE "codegen"
43
Chris Lattner67159522010-01-26 04:35:26 +000044//===----------------------------------------------------------------------===//
Chris Lattner6d8a6c62002-10-28 01:12:41 +000045// MachineFunction implementation
Chris Lattner67159522010-01-26 04:35:26 +000046//===----------------------------------------------------------------------===//
Chris Lattnere6074aa2005-01-29 18:41:25 +000047
Chris Lattnerf2471ec2009-09-15 22:44:26 +000048// Out of line virtual method.
49MachineFunctionInfo::~MachineFunctionInfo() {}
50
Dan Gohman804c95d2008-07-28 21:51:04 +000051void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman3b460302008-07-07 23:14:23 +000052 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattnera578cb72004-05-24 06:11:51 +000053}
Chris Lattner6d8a6c62002-10-28 01:12:41 +000054
Dan Gohman913c9982010-04-15 04:33:49 +000055MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
Rafael Espindola76936eb2014-10-14 18:53:16 +000056 unsigned FunctionNum, MachineModuleInfo &mmi)
Eric Christopherfc6de422014-08-05 02:39:49 +000057 : Fn(F), Target(TM), STI(TM.getSubtargetImpl()), Ctx(mmi.getContext()),
Rafael Espindola76936eb2014-10-14 18:53:16 +000058 MMI(mmi) {
Eric Christopher51bedaf2014-10-08 07:51:41 +000059 if (STI->getRegisterInfo())
Eric Christopherce40dbc2014-08-12 08:00:56 +000060 RegInfo = new (Allocator) MachineRegisterInfo(this);
Matthijs Kooijmanc8d79882008-10-13 12:37:16 +000061 else
Craig Topperc0196b12014-04-14 00:51:57 +000062 RegInfo = nullptr;
Bill Wendling626c9912013-06-17 20:41:25 +000063
Craig Topperc0196b12014-04-14 00:51:57 +000064 MFInfo = nullptr;
Eric Christopher000ef032014-10-08 08:46:34 +000065 FrameInfo = new (Allocator)
66 MachineFrameInfo(STI->getFrameLowering()->getStackAlignment(),
67 STI->getFrameLowering()->isStackRealignable(),
68 !F->hasFnAttribute("no-realign-stack"));
Bill Wendling626c9912013-06-17 20:41:25 +000069
Bill Wendling698e84f2012-12-30 10:32:01 +000070 if (Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
71 Attribute::StackAlignment))
Bill Wendling9be77592012-09-21 15:26:31 +000072 FrameInfo->ensureMaxAlignment(Fn->getAttributes().
Bill Wendling698e84f2012-12-30 10:32:01 +000073 getStackAlignment(AttributeSet::FunctionIndex));
Bill Wendling626c9912013-06-17 20:41:25 +000074
75 ConstantPool = new (Allocator) MachineConstantPool(TM);
Eric Christopher51bedaf2014-10-08 07:51:41 +000076 Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
Bill Wendling626c9912013-06-17 20:41:25 +000077
Eli Friedman2518f832011-05-06 20:34:06 +000078 // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
Bill Wendling698e84f2012-12-30 10:32:01 +000079 if (!Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
80 Attribute::OptimizeForSize))
Eric Christopher51bedaf2014-10-08 07:51:41 +000081 Alignment = std::max(Alignment,
82 STI->getTargetLowering()->getPrefFunctionAlignment());
Bill Wendling626c9912013-06-17 20:41:25 +000083
Chris Lattner67159522010-01-26 04:35:26 +000084 FunctionNumber = FunctionNum;
Craig Topperc0196b12014-04-14 00:51:57 +000085 JumpTableInfo = nullptr;
Chris Lattner448fb452002-12-25 05:03:22 +000086}
87
Alkis Evlogimenos58350a72004-09-05 18:41:35 +000088MachineFunction::~MachineFunction() {
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +000089 // Don't call destructors on MachineInstr and MachineOperand. All of their
90 // memory comes from the BumpPtrAllocator which is about to be purged.
91 //
92 // Do call MachineBasicBlock destructors, it contains std::vectors.
93 for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
94 I->Insts.clearAndLeakNodesUnsafely();
95
Dan Gohman3b460302008-07-07 23:14:23 +000096 InstructionRecycler.clear(Allocator);
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +000097 OperandRecycler.clear(Allocator);
Dan Gohman3b460302008-07-07 23:14:23 +000098 BasicBlockRecycler.clear(Allocator);
Bill Wendling7f7eb8a2009-06-25 00:32:48 +000099 if (RegInfo) {
100 RegInfo->~MachineRegisterInfo();
101 Allocator.Deallocate(RegInfo);
102 }
Dan Gohman3b460302008-07-07 23:14:23 +0000103 if (MFInfo) {
Bill Wendling7f7eb8a2009-06-25 00:32:48 +0000104 MFInfo->~MachineFunctionInfo();
105 Allocator.Deallocate(MFInfo);
Dan Gohman3b460302008-07-07 23:14:23 +0000106 }
Chad Rosier651f9a42012-06-19 23:37:57 +0000107
108 FrameInfo->~MachineFrameInfo();
109 Allocator.Deallocate(FrameInfo);
110
111 ConstantPool->~MachineConstantPool();
112 Allocator.Deallocate(ConstantPool);
113
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000114 if (JumpTableInfo) {
115 JumpTableInfo->~MachineJumpTableInfo();
116 Allocator.Deallocate(JumpTableInfo);
117 }
Chris Lattner214808f2002-10-30 00:48:05 +0000118}
119
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000120/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
121/// does already exist, allocate one.
122MachineJumpTableInfo *MachineFunction::
123getOrCreateJumpTableInfo(unsigned EntryKind) {
124 if (JumpTableInfo) return JumpTableInfo;
Chad Rosier651f9a42012-06-19 23:37:57 +0000125
Dan Gohman01c65a22010-03-18 18:49:47 +0000126 JumpTableInfo = new (Allocator)
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000127 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
128 return JumpTableInfo;
129}
Chris Lattner64fd9482006-10-03 19:18:57 +0000130
Reid Kleckner9c658212014-04-10 22:58:43 +0000131/// Should we be emitting segmented stack stuff for the function
132bool MachineFunction::shouldSplitStack() {
133 return getFunction()->hasFnAttribute("split-stack");
134}
135
Chris Lattner64fd9482006-10-03 19:18:57 +0000136/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
137/// recomputes them. This guarantees that the MBB numbers are sequential,
138/// dense, and match the ordering of the blocks within the function. If a
139/// specific MachineBasicBlock is specified, only that block and those after
140/// it are renumbered.
141void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
142 if (empty()) { MBBNumbering.clear(); return; }
143 MachineFunction::iterator MBBI, E = end();
Craig Topperc0196b12014-04-14 00:51:57 +0000144 if (MBB == nullptr)
Chris Lattner64fd9482006-10-03 19:18:57 +0000145 MBBI = begin();
146 else
147 MBBI = MBB;
Chad Rosier651f9a42012-06-19 23:37:57 +0000148
Chris Lattner64fd9482006-10-03 19:18:57 +0000149 // Figure out the block number this should have.
150 unsigned BlockNo = 0;
Chris Lattnerbd7286e2006-10-03 20:19:23 +0000151 if (MBBI != begin())
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000152 BlockNo = std::prev(MBBI)->getNumber() + 1;
Chad Rosier651f9a42012-06-19 23:37:57 +0000153
Chris Lattner64fd9482006-10-03 19:18:57 +0000154 for (; MBBI != E; ++MBBI, ++BlockNo) {
155 if (MBBI->getNumber() != (int)BlockNo) {
156 // Remove use of the old number.
157 if (MBBI->getNumber() != -1) {
158 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
159 "MBB number mismatch!");
Craig Topperc0196b12014-04-14 00:51:57 +0000160 MBBNumbering[MBBI->getNumber()] = nullptr;
Chris Lattner64fd9482006-10-03 19:18:57 +0000161 }
Chad Rosier651f9a42012-06-19 23:37:57 +0000162
Chris Lattner64fd9482006-10-03 19:18:57 +0000163 // If BlockNo is already taken, set that block's number to -1.
164 if (MBBNumbering[BlockNo])
165 MBBNumbering[BlockNo]->setNumber(-1);
166
167 MBBNumbering[BlockNo] = MBBI;
168 MBBI->setNumber(BlockNo);
169 }
Chad Rosier651f9a42012-06-19 23:37:57 +0000170 }
Chris Lattner64fd9482006-10-03 19:18:57 +0000171
172 // Okay, all the blocks are renumbered. If we have compactified the block
173 // numbering, shrink MBBNumbering now.
174 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
175 MBBNumbering.resize(BlockNo);
176}
177
Dan Gohman3b460302008-07-07 23:14:23 +0000178/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
179/// of `new MachineInstr'.
180///
181MachineInstr *
Evan Cheng6cc775f2011-06-28 19:10:37 +0000182MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
Bill Wendlinge3c78362009-02-03 00:55:04 +0000183 DebugLoc DL, bool NoImp) {
Dan Gohman3b460302008-07-07 23:14:23 +0000184 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Jakob Stoklund Olesenac4210e2012-12-20 22:53:58 +0000185 MachineInstr(*this, MCID, DL, NoImp);
Dan Gohman3b460302008-07-07 23:14:23 +0000186}
Chris Lattner64fd9482006-10-03 19:18:57 +0000187
Dan Gohman3b460302008-07-07 23:14:23 +0000188/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
Dan Gohman4a618822010-02-10 16:03:48 +0000189/// 'Orig' instruction, identical in all ways except the instruction
Dan Gohman3b460302008-07-07 23:14:23 +0000190/// has no parent, prev, or next.
191///
192MachineInstr *
193MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
194 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
195 MachineInstr(*this, *Orig);
196}
197
198/// DeleteMachineInstr - Delete the given MachineInstr.
199///
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000200/// This function also serves as the MachineInstr destructor - the real
201/// ~MachineInstr() destructor must be empty.
Dan Gohman3b460302008-07-07 23:14:23 +0000202void
203MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
Jakob Stoklund Olesen1bfeecb2013-01-05 05:00:09 +0000204 // Strip it for parts. The operand array and the MI object itself are
205 // independently recyclable.
206 if (MI->Operands)
207 deallocateOperandArray(MI->CapOperands, MI->Operands);
Jakob Stoklund Olesendc5285f2013-01-05 05:05:51 +0000208 // Don't call ~MachineInstr() which must be trivial anyway because
209 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
210 // destructors.
Dan Gohman3b460302008-07-07 23:14:23 +0000211 InstructionRecycler.Deallocate(Allocator, MI);
212}
213
214/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
215/// instead of `new MachineBasicBlock'.
216///
217MachineBasicBlock *
218MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
219 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
220 MachineBasicBlock(*this, bb);
221}
222
223/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
224///
225void
226MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
227 assert(MBB->getParent() == this && "MBB parent mismatch!");
228 MBB->~MachineBasicBlock();
229 BasicBlockRecycler.Deallocate(Allocator, MBB);
230}
231
Dan Gohman48b185d2009-09-25 20:36:54 +0000232MachineMemOperand *
Chris Lattner00ca0b82010-09-21 04:32:08 +0000233MachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000234 uint64_t s, unsigned base_alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +0000235 const AAMDNodes &AAInfo,
Rafael Espindola80c540e2012-03-31 18:14:00 +0000236 const MDNode *Ranges) {
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000237 return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment,
Hal Finkelcc39b672014-07-24 12:16:19 +0000238 AAInfo, Ranges);
Dan Gohman48b185d2009-09-25 20:36:54 +0000239}
240
241MachineMemOperand *
242MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
243 int64_t Offset, uint64_t Size) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000244 if (MMO->getValue())
245 return new (Allocator)
246 MachineMemOperand(MachinePointerInfo(MMO->getValue(),
247 MMO->getOffset()+Offset),
248 MMO->getFlags(), Size,
Benjamin Kramer2e52f022014-10-04 22:44:29 +0000249 MMO->getBaseAlignment());
Dan Gohman01c65a22010-03-18 18:49:47 +0000250 return new (Allocator)
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000251 MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
Chris Lattner00ca0b82010-09-21 04:32:08 +0000252 MMO->getOffset()+Offset),
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000253 MMO->getFlags(), Size,
Benjamin Kramer2e52f022014-10-04 22:44:29 +0000254 MMO->getBaseAlignment());
Dan Gohman48b185d2009-09-25 20:36:54 +0000255}
256
257MachineInstr::mmo_iterator
258MachineFunction::allocateMemRefsArray(unsigned long Num) {
259 return Allocator.Allocate<MachineMemOperand *>(Num);
260}
261
Dan Gohmandd76bb22009-10-09 18:10:05 +0000262std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
263MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
264 MachineInstr::mmo_iterator End) {
265 // Count the number of load mem refs.
266 unsigned Num = 0;
267 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
268 if ((*I)->isLoad())
269 ++Num;
270
271 // Allocate a new array and populate it with the load information.
272 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
273 unsigned Index = 0;
274 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
275 if ((*I)->isLoad()) {
276 if (!(*I)->isStore())
277 // Reuse the MMO.
278 Result[Index] = *I;
279 else {
280 // Clone the MMO and unset the store flag.
281 MachineMemOperand *JustLoad =
Chris Lattnerb5f49202010-09-21 04:46:39 +0000282 getMachineMemOperand((*I)->getPointerInfo(),
Dan Gohmandd76bb22009-10-09 18:10:05 +0000283 (*I)->getFlags() & ~MachineMemOperand::MOStore,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000284 (*I)->getSize(), (*I)->getBaseAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000285 (*I)->getAAInfo());
Dan Gohmandd76bb22009-10-09 18:10:05 +0000286 Result[Index] = JustLoad;
287 }
288 ++Index;
289 }
290 }
291 return std::make_pair(Result, Result + Num);
292}
293
294std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
295MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
296 MachineInstr::mmo_iterator End) {
297 // Count the number of load mem refs.
298 unsigned Num = 0;
299 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
300 if ((*I)->isStore())
301 ++Num;
302
303 // Allocate a new array and populate it with the store information.
304 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
305 unsigned Index = 0;
306 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
307 if ((*I)->isStore()) {
308 if (!(*I)->isLoad())
309 // Reuse the MMO.
310 Result[Index] = *I;
311 else {
312 // Clone the MMO and unset the load flag.
313 MachineMemOperand *JustStore =
Chris Lattnerb5f49202010-09-21 04:46:39 +0000314 getMachineMemOperand((*I)->getPointerInfo(),
Dan Gohmandd76bb22009-10-09 18:10:05 +0000315 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
Dan Gohmana94cc6d2010-10-20 00:31:05 +0000316 (*I)->getSize(), (*I)->getBaseAlignment(),
Hal Finkelcc39b672014-07-24 12:16:19 +0000317 (*I)->getAAInfo());
Dan Gohmandd76bb22009-10-09 18:10:05 +0000318 Result[Index] = JustStore;
319 }
320 ++Index;
321 }
322 }
323 return std::make_pair(Result, Result + Num);
324}
325
Manman Ren19f49ac2012-09-11 22:23:19 +0000326#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman3b460302008-07-07 23:14:23 +0000327void MachineFunction::dump() const {
David Greene66710112010-01-04 23:39:17 +0000328 print(dbgs());
Dan Gohman3b460302008-07-07 23:14:23 +0000329}
Manman Ren742534c2012-09-06 19:06:06 +0000330#endif
Chris Lattner214808f2002-10-30 00:48:05 +0000331
Craig Toppera538d832012-08-22 06:07:19 +0000332StringRef MachineFunction::getName() const {
333 assert(getFunction() && "No function!");
334 return getFunction()->getName();
335}
336
Jakob Stoklund Olesenb7050232010-10-26 20:21:46 +0000337void MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const {
David Blaikiec8c29202012-08-22 17:18:53 +0000338 OS << "# Machine code for function " << getName() << ": ";
Jakob Stoklund Olesen6c085342012-03-27 17:17:16 +0000339 if (RegInfo) {
340 OS << (RegInfo->isSSA() ? "SSA" : "Post SSA");
341 if (!RegInfo->tracksLiveness())
342 OS << ", not tracking liveness";
343 }
344 OS << '\n';
Chris Lattner32525642002-12-28 20:37:16 +0000345
346 // Print Frame Information
Dan Gohman3b460302008-07-07 23:14:23 +0000347 FrameInfo->print(*this, OS);
Chad Rosier651f9a42012-06-19 23:37:57 +0000348
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000349 // Print JumpTable Information
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000350 if (JumpTableInfo)
351 JumpTableInfo->print(OS);
Chris Lattnerc6807e82003-01-13 00:23:03 +0000352
353 // Print Constant Pool
Chris Lattner22d4bfc2009-08-23 01:12:47 +0000354 ConstantPool->print(OS);
Chad Rosier651f9a42012-06-19 23:37:57 +0000355
Eric Christopherfc6de422014-08-05 02:39:49 +0000356 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
Chad Rosier651f9a42012-06-19 23:37:57 +0000357
Matthijs Kooijmanc8d79882008-10-13 12:37:16 +0000358 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman34341e62009-10-31 20:19:03 +0000359 OS << "Function Live Ins: ";
Chris Lattnera10fff52007-12-31 04:13:23 +0000360 for (MachineRegisterInfo::livein_iterator
361 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Jakob Stoklund Olesen6297a712011-05-02 20:06:30 +0000362 OS << PrintReg(I->first, TRI);
Chris Lattner52d0c782006-05-16 05:55:30 +0000363 if (I->second)
Jakob Stoklund Olesen6297a712011-05-02 20:06:30 +0000364 OS << " in " << PrintReg(I->second, TRI);
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000365 if (std::next(I) != E)
Dan Gohman34341e62009-10-31 20:19:03 +0000366 OS << ", ";
Chris Lattnerd4d10ff2005-08-31 22:34:59 +0000367 }
Chris Lattner22d4bfc2009-08-23 01:12:47 +0000368 OS << '\n';
Chris Lattnerd4d10ff2005-08-31 22:34:59 +0000369 }
Chad Rosier651f9a42012-06-19 23:37:57 +0000370
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000371 for (const auto &BB : *this) {
Dan Gohman34341e62009-10-31 20:19:03 +0000372 OS << '\n';
Alexey Samsonov41b977d2014-04-30 18:29:51 +0000373 BB.print(OS, Indexes);
Dan Gohman34341e62009-10-31 20:19:03 +0000374 }
Brian Gaeke2fe0ac92004-03-29 21:58:31 +0000375
David Blaikiec8c29202012-08-22 17:18:53 +0000376 OS << "\n# End machine code for function " << getName() << ".\n\n";
Chris Lattner214808f2002-10-30 00:48:05 +0000377}
378
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000379namespace llvm {
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000380 template<>
381 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grosser90d33402009-11-30 12:38:13 +0000382
383 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
384
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000385 static std::string getGraphName(const MachineFunction *F) {
Craig Toppera538d832012-08-22 06:07:19 +0000386 return "CFG for '" + F->getName().str() + "' function";
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000387 }
388
Tobias Grosserdd7f2e72009-11-30 12:38:47 +0000389 std::string getNodeLabel(const MachineBasicBlock *Node,
390 const MachineFunction *Graph) {
Chris Lattner565449d2009-08-23 03:13:20 +0000391 std::string OutStr;
392 {
393 raw_string_ostream OSS(OutStr);
Jakob Stoklund Olesen80717dd2010-10-30 01:26:19 +0000394
395 if (isSimple()) {
396 OSS << "BB#" << Node->getNumber();
397 if (const BasicBlock *BB = Node->getBasicBlock())
398 OSS << ": " << BB->getName();
399 } else
Chris Lattner565449d2009-08-23 03:13:20 +0000400 Node->print(OSS);
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000401 }
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000402
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000403 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
404
405 // Process string output to make it nicer...
406 for (unsigned i = 0; i != OutStr.length(); ++i)
407 if (OutStr[i] == '\n') { // Left justify
408 OutStr[i] = '\\';
409 OutStr.insert(OutStr.begin()+i+1, 'l');
410 }
411 return OutStr;
412 }
413 };
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000414}
415
416void MachineFunction::viewCFG() const
417{
Jim Laskeyd00db252005-10-12 12:09:05 +0000418#ifndef NDEBUG
David Blaikiec8c29202012-08-22 17:18:53 +0000419 ViewGraph(this, "mf" + getName());
Reid Spenceree7eaa22006-06-27 16:49:46 +0000420#else
Dan Gohman76920142010-07-07 17:28:45 +0000421 errs() << "MachineFunction::viewCFG is only available in debug builds on "
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000422 << "systems with Graphviz or gv!\n";
Reid Spenceree7eaa22006-06-27 16:49:46 +0000423#endif // NDEBUG
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000424}
425
426void MachineFunction::viewCFGOnly() const
427{
Owen Andersonb70adf22009-06-24 17:37:09 +0000428#ifndef NDEBUG
David Blaikiec8c29202012-08-22 17:18:53 +0000429 ViewGraph(this, "mf" + getName(), true);
Owen Andersonb70adf22009-06-24 17:37:09 +0000430#else
Dan Gohman76920142010-07-07 17:28:45 +0000431 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000432 << "systems with Graphviz or gv!\n";
Owen Andersonb70adf22009-06-24 17:37:09 +0000433#endif // NDEBUG
Alkis Evlogimenos2c422bb2004-07-08 00:47:58 +0000434}
435
Bob Wilsonf8b85472009-04-20 18:36:57 +0000436/// addLiveIn - Add the specified physical register as a live-in value and
437/// create a corresponding virtual register for it.
438unsigned MachineFunction::addLiveIn(unsigned PReg,
Devang Patelf3292b22011-02-21 23:21:26 +0000439 const TargetRegisterClass *RC) {
Evan Cheng1b79bab2010-05-24 21:33:37 +0000440 MachineRegisterInfo &MRI = getRegInfo();
441 unsigned VReg = MRI.getLiveInVirtReg(PReg);
442 if (VReg) {
Quentin Colombet18b779e2013-12-12 00:15:47 +0000443 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
444 (void)VRegRC;
445 // A physical register can be added several times.
446 // Between two calls, the register class of the related virtual register
447 // may have been constrained to match some operation constraints.
448 // In that case, check that the current register class includes the
449 // physical register and is a sub class of the specified RC.
450 assert((VRegRC == RC || (VRegRC->contains(PReg) &&
451 RC->hasSubClassEq(VRegRC))) &&
452 "Register class mismatch!");
Evan Cheng1b79bab2010-05-24 21:33:37 +0000453 return VReg;
454 }
455 VReg = MRI.createVirtualRegister(RC);
456 MRI.addLiveIn(PReg, VReg);
Bob Wilsonf8b85472009-04-20 18:36:57 +0000457 return VReg;
458}
459
Chris Lattner8a785d72010-01-26 06:28:43 +0000460/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
Bill Wendling36321712010-06-29 22:34:52 +0000461/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
462/// normal 'L' label is returned.
NAKAMURA Takumic403be12014-06-25 12:40:56 +0000463MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
Bill Wendling36321712010-06-29 22:34:52 +0000464 bool isLinkerPrivate) const {
Eric Christopherfc6de422014-08-05 02:39:49 +0000465 const DataLayout *DL = getSubtarget().getDataLayout();
Chris Lattner8a785d72010-01-26 06:28:43 +0000466 assert(JumpTableInfo && "No jump tables");
Chandler Carruthc07bd402010-01-27 10:27:10 +0000467 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
Chad Rosier651f9a42012-06-19 23:37:57 +0000468
Rafael Espindola58873562014-01-03 19:21:54 +0000469 const char *Prefix = isLinkerPrivate ? DL->getLinkerPrivateGlobalPrefix() :
470 DL->getPrivateGlobalPrefix();
Alp Tokere69170a2014-06-26 22:52:05 +0000471 SmallString<60> Name;
472 raw_svector_ostream(Name)
473 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
Chris Lattner98970432010-03-30 18:10:53 +0000474 return Ctx.GetOrCreateSymbol(Name.str());
Chris Lattner8a785d72010-01-26 06:28:43 +0000475}
476
Chris Lattner7077efe2010-11-14 22:48:15 +0000477/// getPICBaseSymbol - Return a function-local symbol to represent the PIC
478/// base.
479MCSymbol *MachineFunction::getPICBaseSymbol() const {
Eric Christopherfc6de422014-08-05 02:39:49 +0000480 const DataLayout *DL = getSubtarget().getDataLayout();
Rafael Espindola58873562014-01-03 19:21:54 +0000481 return Ctx.GetOrCreateSymbol(Twine(DL->getPrivateGlobalPrefix())+
Chris Lattner7077efe2010-11-14 22:48:15 +0000482 Twine(getFunctionNumber())+"$pb");
483}
Chris Lattner8a785d72010-01-26 06:28:43 +0000484
Chris Lattner32525642002-12-28 20:37:16 +0000485//===----------------------------------------------------------------------===//
Chris Lattnerca4362f2002-12-28 21:08:26 +0000486// MachineFrameInfo implementation
Chris Lattner32525642002-12-28 20:37:16 +0000487//===----------------------------------------------------------------------===//
488
Manman Ren26c73f92012-12-04 00:26:44 +0000489/// ensureMaxAlignment - Make sure the function is at least Align bytes
490/// aligned.
491void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
Eric Christopher000ef032014-10-08 08:46:34 +0000492 if (!StackRealignable || !RealignOption)
493 assert(Align <= StackAlignment &&
Manman Renf5639412012-12-04 00:52:33 +0000494 "For targets without stack realignment, Align is out of limit!");
Manman Ren26c73f92012-12-04 00:26:44 +0000495 if (MaxAlignment < Align) MaxAlignment = Align;
496}
497
Manman Renf5639412012-12-04 00:52:33 +0000498/// clampStackAlignment - Clamp the alignment if requested and emit a warning.
Bob Wilson67bbf3a2013-02-08 20:35:15 +0000499static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
500 unsigned StackAlign) {
501 if (!ShouldClamp || Align <= StackAlign)
502 return Align;
503 DEBUG(dbgs() << "Warning: requested alignment " << Align
504 << " exceeds the stack alignment " << StackAlign
505 << " when stack realignment is off" << '\n');
Manman Renf5639412012-12-04 00:52:33 +0000506 return StackAlign;
507}
508
Bob Wilson67bbf3a2013-02-08 20:35:15 +0000509/// CreateStackObject - Create a new statically sized stack object, returning
510/// a nonnegative identifier to represent it.
Manman Ren26c73f92012-12-04 00:26:44 +0000511///
Bob Wilson67bbf3a2013-02-08 20:35:15 +0000512int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
Josh Magee22b8ba22013-12-19 03:17:11 +0000513 bool isSS, const AllocaInst *Alloca) {
Manman Ren26c73f92012-12-04 00:26:44 +0000514 assert(Size != 0 && "Cannot allocate zero size stack objects!");
Eric Christopher000ef032014-10-08 08:46:34 +0000515 Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
516 Alignment, StackAlignment);
Hal Finkel0815a052014-08-16 00:17:02 +0000517 Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca,
518 !isSS));
Manman Ren26c73f92012-12-04 00:26:44 +0000519 int Index = (int)Objects.size() - NumFixedObjects - 1;
520 assert(Index >= 0 && "Bad frame index!");
521 ensureMaxAlignment(Alignment);
522 return Index;
523}
524
525/// CreateSpillStackObject - Create a new statically sized stack object that
526/// represents a spill slot, returning a nonnegative identifier to represent
527/// it.
528///
529int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
530 unsigned Alignment) {
Eric Christopher000ef032014-10-08 08:46:34 +0000531 Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
532 Alignment, StackAlignment);
Josh Magee22b8ba22013-12-19 03:17:11 +0000533 CreateStackObject(Size, Alignment, true);
Manman Ren26c73f92012-12-04 00:26:44 +0000534 int Index = (int)Objects.size() - NumFixedObjects - 1;
535 ensureMaxAlignment(Alignment);
536 return Index;
537}
538
539/// CreateVariableSizedObject - Notify the MachineFrameInfo object that a
540/// variable sized object has been created. This must be created whenever a
541/// variable sized object is created, whether or not the index returned is
542/// actually used.
543///
Josh Magee22b8ba22013-12-19 03:17:11 +0000544int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
545 const AllocaInst *Alloca) {
Manman Ren26c73f92012-12-04 00:26:44 +0000546 HasVarSizedObjects = true;
Eric Christopher000ef032014-10-08 08:46:34 +0000547 Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
548 Alignment, StackAlignment);
Hal Finkel0815a052014-08-16 00:17:02 +0000549 Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
Manman Ren26c73f92012-12-04 00:26:44 +0000550 ensureMaxAlignment(Alignment);
551 return (int)Objects.size()-NumFixedObjects-1;
552}
553
Chris Lattner60688322008-01-25 07:19:06 +0000554/// CreateFixedObject - Create a new object at a fixed location on the stack.
555/// All fixed objects should be created before other objects are created for
556/// efficiency. By default, fixed objects are immutable. This returns an
557/// index with a negative value.
558///
559int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
Hal Finkel0815a052014-08-16 00:17:02 +0000560 bool Immutable, bool isAliased) {
Chris Lattner60688322008-01-25 07:19:06 +0000561 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
Evan Chengf3aeb2c2010-07-04 18:52:05 +0000562 // The alignment of the frame index can be determined from its offset from
563 // the incoming frame position. If the frame object is at offset 32 and
564 // the stack is guaranteed to be 16-byte aligned, then we know that the
565 // object is 16-byte aligned.
Eric Christopher000ef032014-10-08 08:46:34 +0000566 unsigned Align = MinAlign(SPOffset, StackAlignment);
567 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
568 StackAlignment);
Evan Chengf3aeb2c2010-07-04 18:52:05 +0000569 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
Nadav Rotem7c277da2012-09-06 09:17:37 +0000570 /*isSS*/ false,
Hal Finkel0815a052014-08-16 00:17:02 +0000571 /*Alloca*/ nullptr, isAliased));
Chris Lattner60688322008-01-25 07:19:06 +0000572 return -++NumFixedObjects;
573}
574
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000575/// CreateFixedSpillStackObject - Create a spill slot at a fixed location
576/// on the stack. Returns an index with a negative value.
577int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
578 int64_t SPOffset) {
Eric Christopher000ef032014-10-08 08:46:34 +0000579 unsigned Align = MinAlign(SPOffset, StackAlignment);
580 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
581 StackAlignment);
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000582 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset,
583 /*Immutable*/ true,
584 /*isSS*/ true,
Hal Finkel0815a052014-08-16 00:17:02 +0000585 /*Alloca*/ nullptr,
586 /*isAliased*/ false));
NAKAMURA Takumi1db59952014-06-25 12:41:52 +0000587 return -++NumFixedObjects;
588}
Chris Lattner60688322008-01-25 07:19:06 +0000589
Jakob Stoklund Olesen3de4a602009-08-13 16:19:33 +0000590BitVector
591MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
592 assert(MBB && "MBB must be valid");
593 const MachineFunction *MF = MBB->getParent();
594 assert(MF && "MBB must be part of a MachineFunction");
595 const TargetMachine &TM = MF->getTarget();
Eric Christopherd9134482014-08-04 21:25:23 +0000596 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
Jakob Stoklund Olesen3de4a602009-08-13 16:19:33 +0000597 BitVector BV(TRI->getNumRegs());
598
599 // Before CSI is calculated, no registers are considered pristine. They can be
600 // freely used and PEI will make sure they are saved.
601 if (!isCalleeSavedInfoValid())
602 return BV;
603
Craig Topper840beec2014-04-04 05:16:06 +0000604 for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
Jakob Stoklund Olesen3de4a602009-08-13 16:19:33 +0000605 BV.set(*CSR);
606
607 // The entry MBB always has all CSRs pristine.
608 if (MBB == &MF->front())
609 return BV;
610
611 // On other MBBs the saved CSRs are not pristine.
612 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
613 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
614 E = CSI.end(); I != E; ++I)
615 BV.reset(I->getReg());
616
617 return BV;
618}
619
Hal Finkel628ba122013-03-14 21:15:20 +0000620unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
Eric Christopherfc6de422014-08-05 02:39:49 +0000621 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
622 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Hal Finkel628ba122013-03-14 21:15:20 +0000623 unsigned MaxAlign = getMaxAlignment();
624 int Offset = 0;
625
626 // This code is very, very similar to PEI::calculateFrameObjectOffsets().
627 // It really should be refactored to share code. Until then, changes
628 // should keep in mind that there's tight coupling between the two.
629
630 for (int i = getObjectIndexBegin(); i != 0; ++i) {
631 int FixedOff = -getObjectOffset(i);
632 if (FixedOff > Offset) Offset = FixedOff;
633 }
634 for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
635 if (isDeadObjectIndex(i))
636 continue;
637 Offset += getObjectSize(i);
638 unsigned Align = getObjectAlignment(i);
639 // Adjust to alignment boundary
640 Offset = (Offset+Align-1)/Align*Align;
641
642 MaxAlign = std::max(Align, MaxAlign);
643 }
644
645 if (adjustsStack() && TFI->hasReservedCallFrame(MF))
646 Offset += getMaxCallFrameSize();
647
648 // Round up the size to a multiple of the alignment. If the function has
649 // any calls or alloca's, align to the target's StackAlignment value to
650 // ensure that the callee's frame or the alloca data is suitably aligned;
651 // otherwise, for leaf functions, align to the TransientStackAlignment
652 // value.
653 unsigned StackAlign;
654 if (adjustsStack() || hasVarSizedObjects() ||
655 (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
656 StackAlign = TFI->getStackAlignment();
657 else
658 StackAlign = TFI->getTransientStackAlignment();
659
660 // If the frame pointer is eliminated, all frame offsets will be relative to
661 // SP not FP. Align to MaxAlign so this works.
662 StackAlign = std::max(StackAlign, MaxAlign);
663 unsigned AlignMask = StackAlign - 1;
664 Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
665
666 return (unsigned)Offset;
667}
Jakob Stoklund Olesen3de4a602009-08-13 16:19:33 +0000668
Chris Lattner22d4bfc2009-08-23 01:12:47 +0000669void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman34341e62009-10-31 20:19:03 +0000670 if (Objects.empty()) return;
671
Eric Christopherfc6de422014-08-05 02:39:49 +0000672 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
Matthijs Kooijman2530f5f2008-11-03 11:16:43 +0000673 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattnereb45c982003-01-16 18:35:57 +0000674
Dan Gohman34341e62009-10-31 20:19:03 +0000675 OS << "Frame Objects:\n";
676
Chris Lattner32525642002-12-28 20:37:16 +0000677 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
678 const StackObject &SO = Objects[i];
Dan Gohman34341e62009-10-31 20:19:03 +0000679 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Cheng6d563682008-02-27 03:04:06 +0000680 if (SO.Size == ~0ULL) {
681 OS << "dead\n";
682 continue;
683 }
Chris Lattner32525642002-12-28 20:37:16 +0000684 if (SO.Size == 0)
685 OS << "variable sized";
686 else
Dan Gohman34341e62009-10-31 20:19:03 +0000687 OS << "size=" << SO.Size;
688 OS << ", align=" << SO.Alignment;
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000689
Chris Lattner32525642002-12-28 20:37:16 +0000690 if (i < NumFixedObjects)
Dan Gohman34341e62009-10-31 20:19:03 +0000691 OS << ", fixed";
Chris Lattner32525642002-12-28 20:37:16 +0000692 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattner9bd98ea2007-04-25 04:20:54 +0000693 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman34341e62009-10-31 20:19:03 +0000694 OS << ", at location [SP";
Chris Lattnereb45c982003-01-16 18:35:57 +0000695 if (Off > 0)
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000696 OS << "+" << Off;
Chris Lattnereb45c982003-01-16 18:35:57 +0000697 else if (Off < 0)
Alkis Evlogimenos58350a72004-09-05 18:41:35 +0000698 OS << Off;
Chris Lattner32525642002-12-28 20:37:16 +0000699 OS << "]";
700 }
701 OS << "\n";
702 }
Chris Lattner32525642002-12-28 20:37:16 +0000703}
704
Manman Ren19f49ac2012-09-11 22:23:19 +0000705#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattnereb45c982003-01-16 18:35:57 +0000706void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greene66710112010-01-04 23:39:17 +0000707 print(MF, dbgs());
Chris Lattnereb45c982003-01-16 18:35:57 +0000708}
Manman Ren742534c2012-09-06 19:06:06 +0000709#endif
Chris Lattner32525642002-12-28 20:37:16 +0000710
Chris Lattner32525642002-12-28 20:37:16 +0000711//===----------------------------------------------------------------------===//
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000712// MachineJumpTableInfo implementation
713//===----------------------------------------------------------------------===//
714
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000715/// getEntrySize - Return the size of each entry in the jump table.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000716unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000717 // The size of a jump table entry is 4 bytes unless the entry is just the
718 // address of a block, in which case it is the pointer size.
719 switch (getEntryKind()) {
720 case MachineJumpTableInfo::EK_BlockAddress:
Chandler Carruth5da3f052012-11-01 09:14:31 +0000721 return TD.getPointerSize();
Akira Hatanakaf0b08442012-02-03 04:33:00 +0000722 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
723 return 8;
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000724 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
725 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner5fc41602010-01-26 04:05:28 +0000726 case MachineJumpTableInfo::EK_Custom32:
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000727 return 4;
Richard Osborne6d3e92d2010-03-11 14:58:16 +0000728 case MachineJumpTableInfo::EK_Inline:
729 return 0;
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000730 }
Craig Topperaccd3512012-02-06 08:17:43 +0000731 llvm_unreachable("Unknown jump table encoding!");
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000732}
733
734/// getEntryAlignment - Return the alignment of each entry in the jump table.
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000735unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000736 // The alignment of a jump table entry is the alignment of int32 unless the
737 // entry is just the address of a block, in which case it is the pointer
738 // alignment.
739 switch (getEntryKind()) {
740 case MachineJumpTableInfo::EK_BlockAddress:
Chandler Carruth5da3f052012-11-01 09:14:31 +0000741 return TD.getPointerABIAlignment();
Akira Hatanakaf0b08442012-02-03 04:33:00 +0000742 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
743 return TD.getABIIntegerTypeAlignment(64);
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000744 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
745 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner5fc41602010-01-26 04:05:28 +0000746 case MachineJumpTableInfo::EK_Custom32:
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000747 return TD.getABIIntegerTypeAlignment(32);
Richard Osborne6d3e92d2010-03-11 14:58:16 +0000748 case MachineJumpTableInfo::EK_Inline:
749 return 1;
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000750 }
Craig Topperaccd3512012-02-06 08:17:43 +0000751 llvm_unreachable("Unknown jump table encoding!");
Chris Lattnerb6db2c62010-01-25 23:26:13 +0000752}
753
Bob Wilson3c7cde42010-03-18 18:42:41 +0000754/// createJumpTableIndex - Create a new jump table entry in the jump table info.
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000755///
Bob Wilson3c7cde42010-03-18 18:42:41 +0000756unsigned MachineJumpTableInfo::createJumpTableIndex(
Chris Lattnercde339c2006-10-28 18:17:09 +0000757 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattner28328f92006-10-28 18:11:20 +0000758 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000759 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
760 return JumpTables.size()-1;
761}
762
Dan Gohman505065c2009-04-15 01:18:49 +0000763/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
764/// the jump tables to branch to New instead.
Chris Lattner273735b2010-01-26 05:58:28 +0000765bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
766 MachineBasicBlock *New) {
Dan Gohman505065c2009-04-15 01:18:49 +0000767 assert(Old != New && "Not making a change?");
768 bool MadeChange = false;
Jim Grosbach9c8609e2009-11-14 20:09:13 +0000769 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
770 ReplaceMBBInJumpTable(i, Old, New);
771 return MadeChange;
772}
773
774/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
775/// the jump table to branch to New instead.
Chris Lattner273735b2010-01-26 05:58:28 +0000776bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
777 MachineBasicBlock *Old,
778 MachineBasicBlock *New) {
Jim Grosbach9c8609e2009-11-14 20:09:13 +0000779 assert(Old != New && "Not making a change?");
780 bool MadeChange = false;
781 MachineJumpTableEntry &JTE = JumpTables[Idx];
782 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
783 if (JTE.MBBs[j] == Old) {
784 JTE.MBBs[j] = New;
785 MadeChange = true;
786 }
Dan Gohman505065c2009-04-15 01:18:49 +0000787 return MadeChange;
788}
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000789
Chris Lattner22d4bfc2009-08-23 01:12:47 +0000790void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman34341e62009-10-31 20:19:03 +0000791 if (JumpTables.empty()) return;
792
793 OS << "Jump Tables:\n";
794
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000795 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman34341e62009-10-31 20:19:03 +0000796 OS << " jt#" << i << ": ";
797 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
798 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000799 }
Dan Gohman34341e62009-10-31 20:19:03 +0000800
801 OS << '\n';
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000802}
803
Manman Ren19f49ac2012-09-11 22:23:19 +0000804#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene66710112010-01-04 23:39:17 +0000805void MachineJumpTableInfo::dump() const { print(dbgs()); }
Manman Ren742534c2012-09-06 19:06:06 +0000806#endif
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000807
808
809//===----------------------------------------------------------------------===//
Chris Lattnerc6807e82003-01-13 00:23:03 +0000810// MachineConstantPool implementation
811//===----------------------------------------------------------------------===//
812
David Blaikiea379b1812011-12-20 02:50:00 +0000813void MachineConstantPoolValue::anchor() { }
814
Bill Wendling626c9912013-06-17 20:41:25 +0000815const DataLayout *MachineConstantPool::getDataLayout() const {
Eric Christopherd9134482014-08-04 21:25:23 +0000816 return TM.getSubtargetImpl()->getDataLayout();
Bill Wendling626c9912013-06-17 20:41:25 +0000817}
818
Chris Lattner229907c2011-07-18 04:54:35 +0000819Type *MachineConstantPoolEntry::getType() const {
Evan Cheng4f9299552006-09-14 05:50:57 +0000820 if (isMachineConstantPoolEntry())
Chris Lattnercfb01e22009-07-21 23:34:23 +0000821 return Val.MachineCPVal->getType();
Evan Cheng4f9299552006-09-14 05:50:57 +0000822 return Val.ConstVal->getType();
823}
824
Chris Lattnercfb01e22009-07-21 23:34:23 +0000825
Chris Lattner9bd736e2009-07-21 23:36:01 +0000826unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattnercfb01e22009-07-21 23:34:23 +0000827 if (isMachineConstantPoolEntry())
Chris Lattner9bd736e2009-07-21 23:36:01 +0000828 return Val.MachineCPVal->getRelocationInfo();
Chris Lattner4565ef52009-07-22 00:05:44 +0000829 return Val.ConstVal->getRelocationInfo();
Chris Lattnercfb01e22009-07-21 23:34:23 +0000830}
831
David Majnemer5a1c4b82014-07-14 22:06:29 +0000832SectionKind
833MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
834 SectionKind Kind;
835 switch (getRelocationInfo()) {
836 default:
837 llvm_unreachable("Unknown section kind");
838 case 2:
839 Kind = SectionKind::getReadOnlyWithRel();
840 break;
841 case 1:
842 Kind = SectionKind::getReadOnlyWithRelLocal();
843 break;
844 case 0:
845 switch (DL->getTypeAllocSize(getType())) {
846 case 4:
847 Kind = SectionKind::getMergeableConst4();
848 break;
849 case 8:
850 Kind = SectionKind::getMergeableConst8();
851 break;
852 case 16:
853 Kind = SectionKind::getMergeableConst16();
854 break;
855 default:
856 Kind = SectionKind::getMergeableConst();
857 break;
858 }
859 }
860 return Kind;
861}
862
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000863MachineConstantPool::~MachineConstantPool() {
864 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
865 if (Constants[i].isMachineConstantPoolEntry())
866 delete Constants[i].Val.MachineCPVal;
Cameron Zwarich7cf88762011-02-22 08:54:30 +0000867 for (DenseSet<MachineConstantPoolValue*>::iterator I =
868 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
869 I != E; ++I)
870 delete *I;
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000871}
872
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000873/// CanShareConstantPoolEntry - Test whether the given two constants
874/// can be allocated the same constant pool entry.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000875static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000876 const DataLayout *TD) {
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000877 // Handle the trivial case quickly.
878 if (A == B) return true;
879
880 // If they have the same type but weren't the same constant, quickly
881 // reject them.
882 if (A->getType() == B->getType()) return false;
883
Chris Lattner7ba81832012-01-27 01:46:00 +0000884 // We can't handle structs or arrays.
885 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
886 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
887 return false;
NAKAMURA Takumic403be12014-06-25 12:40:56 +0000888
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000889 // For now, only support constants with the same size.
Chris Lattner61a1d6c2012-01-26 21:37:55 +0000890 uint64_t StoreSize = TD->getTypeStoreSize(A->getType());
NAKAMURA Takumic403be12014-06-25 12:40:56 +0000891 if (StoreSize != TD->getTypeStoreSize(B->getType()) || StoreSize > 128)
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000892 return false;
893
Chris Lattner7ba81832012-01-27 01:46:00 +0000894 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000895
Chris Lattner7ba81832012-01-27 01:46:00 +0000896 // Try constant folding a bitcast of both instructions to an integer. If we
897 // get two identical ConstantInt's, then we are good to share them. We use
898 // the constant folding APIs to do this so that we get the benefit of
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000899 // DataLayout.
Chris Lattner7ba81832012-01-27 01:46:00 +0000900 if (isa<PointerType>(A->getType()))
901 A = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
902 const_cast<Constant*>(A), TD);
903 else if (A->getType() != IntTy)
904 A = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
905 const_cast<Constant*>(A), TD);
906 if (isa<PointerType>(B->getType()))
907 B = ConstantFoldInstOperands(Instruction::PtrToInt, IntTy,
908 const_cast<Constant*>(B), TD);
909 else if (B->getType() != IntTy)
910 B = ConstantFoldInstOperands(Instruction::BitCast, IntTy,
911 const_cast<Constant*>(B), TD);
Chad Rosier651f9a42012-06-19 23:37:57 +0000912
Chris Lattner7ba81832012-01-27 01:46:00 +0000913 return A == B;
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000914}
915
Chris Lattnerf6190822006-02-09 04:46:04 +0000916/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman5cf61202008-09-16 20:45:53 +0000917/// an existing one. User must specify the log2 of the minimum required
918/// alignment for the object.
Chris Lattnerf6190822006-02-09 04:46:04 +0000919///
NAKAMURA Takumic403be12014-06-25 12:40:56 +0000920unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
Chris Lattnerf6190822006-02-09 04:46:04 +0000921 unsigned Alignment) {
922 assert(Alignment && "Alignment must be specified!");
923 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000924
Chris Lattnerf6190822006-02-09 04:46:04 +0000925 // Check to see if we already have this constant.
926 //
927 // FIXME, this could be made much more efficient for large constant pools.
Chris Lattnerf6190822006-02-09 04:46:04 +0000928 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000929 if (!Constants[i].isMachineConstantPoolEntry() &&
Bill Wendling626c9912013-06-17 20:41:25 +0000930 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C,
931 getDataLayout())) {
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000932 if ((unsigned)Constants[i].getAlignment() < Alignment)
933 Constants[i].Alignment = Alignment;
Chris Lattnerf6190822006-02-09 04:46:04 +0000934 return i;
Dan Gohman75d6a4a2009-10-28 01:12:16 +0000935 }
Chad Rosier651f9a42012-06-19 23:37:57 +0000936
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000937 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattnerf6190822006-02-09 04:46:04 +0000938 return Constants.size()-1;
939}
940
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000941unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
942 unsigned Alignment) {
943 assert(Alignment && "Alignment must be specified!");
944 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Chad Rosier651f9a42012-06-19 23:37:57 +0000945
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000946 // Check to see if we already have this constant.
947 //
948 // FIXME, this could be made much more efficient for large constant pools.
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000949 int Idx = V->getExistingMachineCPValue(this, Alignment);
Cameron Zwarich7cf88762011-02-22 08:54:30 +0000950 if (Idx != -1) {
951 MachineCPVsSharingEntries.insert(V);
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000952 return (unsigned)Idx;
Cameron Zwarich7cf88762011-02-22 08:54:30 +0000953 }
Evan Cheng1fb8aed2009-03-13 07:51:59 +0000954
955 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000956 return Constants.size()-1;
957}
958
Chris Lattner838aff32008-08-23 22:53:13 +0000959void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman34341e62009-10-31 20:19:03 +0000960 if (Constants.empty()) return;
961
962 OS << "Constant Pool:\n";
Evan Cheng32be2dc2006-01-31 22:23:14 +0000963 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman34341e62009-10-31 20:19:03 +0000964 OS << " cp#" << i << ": ";
Evan Cheng45fe3bc2006-09-12 21:00:35 +0000965 if (Constants[i].isMachineConstantPoolEntry())
966 Constants[i].Val.MachineCPVal->print(OS);
967 else
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000968 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
Dan Gohman34341e62009-10-31 20:19:03 +0000969 OS << ", align=" << Constants[i].getAlignment();
Evan Cheng32be2dc2006-01-31 22:23:14 +0000970 OS << "\n";
971 }
Chris Lattnerc6807e82003-01-13 00:23:03 +0000972}
973
Manman Ren19f49ac2012-09-11 22:23:19 +0000974#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greene66710112010-01-04 23:39:17 +0000975void MachineConstantPool::dump() const { print(dbgs()); }
Manman Ren742534c2012-09-06 19:06:06 +0000976#endif