blob: 8a2b610948b06f5e76d36815c270d3328f99c0c6 [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
David Greene098612b2009-08-19 22:16:11 +000016#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/Analysis/ConstantFolding.h"
Chris Lattner6b276072012-01-27 01:47:28 +000020#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000023#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman37efe672006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner820e55e2010-04-05 05:49:50 +000025#include "llvm/CodeGen/MachineModuleInfo.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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070029#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/Function.h"
Chris Lattnerbeeb93e2010-01-26 05:58:28 +000031#include "llvm/MC/MCAsmInfo.h"
32#include "llvm/MC/MCContext.h"
David Greenedc554812010-01-04 23:39:17 +000033#include "llvm/Support/Debug.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000034#include "llvm/Support/GraphWriter.h"
Chris Lattner944fac72008-08-23 22:23:09 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000036#include "llvm/Target/TargetFrameLowering.h"
37#include "llvm/Target/TargetLowering.h"
38#include "llvm/Target/TargetMachine.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080039#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner07f32d42003-12-20 09:17:07 +000040using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000041
Stephen Hinesdce4a402014-05-29 02:49:00 -070042#define DEBUG_TYPE "codegen"
43
Chris Lattnerb84822f2010-01-26 04:35:26 +000044//===----------------------------------------------------------------------===//
Chris Lattner227c3d32002-10-28 01:12:41 +000045// MachineFunction implementation
Chris Lattnerb84822f2010-01-26 04:35:26 +000046//===----------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +000047
Chris Lattnera70e2e32009-09-15 22:44:26 +000048// Out of line virtual method.
49MachineFunctionInfo::~MachineFunctionInfo() {}
50
Dan Gohmanfed90b62008-07-28 21:51:04 +000051void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +000052 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +000053}
Chris Lattner227c3d32002-10-28 01:12:41 +000054
Dan Gohmanae541aa2010-04-15 04:33:49 +000055MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
Stephen Hines37ed9c12014-12-01 14:51:49 -080056 unsigned FunctionNum, MachineModuleInfo &mmi)
57 : Fn(F), Target(TM), STI(TM.getSubtargetImpl()), Ctx(mmi.getContext()),
58 MMI(mmi) {
59 if (STI->getRegisterInfo())
60 RegInfo = new (Allocator) MachineRegisterInfo(this);
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +000061 else
Stephen Hinesdce4a402014-05-29 02:49:00 -070062 RegInfo = nullptr;
Bill Wendlingd10fa8b2013-06-17 20:41:25 +000063
Stephen Hinesdce4a402014-05-29 02:49:00 -070064 MFInfo = nullptr;
Stephen Hines37ed9c12014-12-01 14:51:49 -080065 FrameInfo = new (Allocator)
66 MachineFrameInfo(STI->getFrameLowering()->getStackAlignment(),
67 STI->getFrameLowering()->isStackRealignable(),
68 !F->hasFnAttribute("no-realign-stack"));
Bill Wendlingd10fa8b2013-06-17 20:41:25 +000069
Bill Wendling831737d2012-12-30 10:32:01 +000070 if (Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
71 Attribute::StackAlignment))
Bill Wendlingef99fe82012-09-21 15:26:31 +000072 FrameInfo->ensureMaxAlignment(Fn->getAttributes().
Bill Wendling831737d2012-12-30 10:32:01 +000073 getStackAlignment(AttributeSet::FunctionIndex));
Bill Wendlingd10fa8b2013-06-17 20:41:25 +000074
75 ConstantPool = new (Allocator) MachineConstantPool(TM);
Stephen Hines37ed9c12014-12-01 14:51:49 -080076 Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
Bill Wendlingd10fa8b2013-06-17 20:41:25 +000077
Eli Friedmanfc5d3052011-05-06 20:34:06 +000078 // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
Bill Wendling831737d2012-12-30 10:32:01 +000079 if (!Fn->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
80 Attribute::OptimizeForSize))
Eli Friedmanfc5d3052011-05-06 20:34:06 +000081 Alignment = std::max(Alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -080082 STI->getTargetLowering()->getPrefFunctionAlignment());
Bill Wendlingd10fa8b2013-06-17 20:41:25 +000083
Chris Lattnerb84822f2010-01-26 04:35:26 +000084 FunctionNumber = FunctionNum;
Stephen Hinesdce4a402014-05-29 02:49:00 -070085 JumpTableInfo = nullptr;
Chris Lattner831fdcf2002-12-25 05:03:22 +000086}
87
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +000088MachineFunction::~MachineFunction() {
Jakob Stoklund Olesen84be3d52013-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 Gohman8e5f2c62008-07-07 23:14:23 +000096 InstructionRecycler.clear(Allocator);
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +000097 OperandRecycler.clear(Allocator);
Dan Gohman8e5f2c62008-07-07 23:14:23 +000098 BasicBlockRecycler.clear(Allocator);
Bill Wendlingdd37b362009-06-25 00:32:48 +000099 if (RegInfo) {
100 RegInfo->~MachineRegisterInfo();
101 Allocator.Deallocate(RegInfo);
102 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000103 if (MFInfo) {
Bill Wendlingdd37b362009-06-25 00:32:48 +0000104 MFInfo->~MachineFunctionInfo();
105 Allocator.Deallocate(MFInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000106 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000107
108 FrameInfo->~MachineFrameInfo();
109 Allocator.Deallocate(FrameInfo);
110
111 ConstantPool->~MachineConstantPool();
112 Allocator.Deallocate(ConstantPool);
113
Chris Lattner071c62f2010-01-25 23:26:13 +0000114 if (JumpTableInfo) {
115 JumpTableInfo->~MachineJumpTableInfo();
116 Allocator.Deallocate(JumpTableInfo);
117 }
Chris Lattner10491642002-10-30 00:48:05 +0000118}
119
Chris Lattner071c62f2010-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 Rosier0c1eac82012-06-19 23:37:57 +0000125
Dan Gohman95531882010-03-18 18:49:47 +0000126 JumpTableInfo = new (Allocator)
Chris Lattner071c62f2010-01-25 23:26:13 +0000127 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
128 return JumpTableInfo;
129}
Chris Lattnere70cab02006-10-03 19:18:57 +0000130
Stephen Hinesdce4a402014-05-29 02:49:00 -0700131/// Should we be emitting segmented stack stuff for the function
132bool MachineFunction::shouldSplitStack() {
133 return getFunction()->hasFnAttribute("split-stack");
134}
135
Chris Lattnere70cab02006-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();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700144 if (MBB == nullptr)
Chris Lattnere70cab02006-10-03 19:18:57 +0000145 MBBI = begin();
146 else
147 MBBI = MBB;
Chad Rosier0c1eac82012-06-19 23:37:57 +0000148
Chris Lattnere70cab02006-10-03 19:18:57 +0000149 // Figure out the block number this should have.
150 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000151 if (MBBI != begin())
Stephen Hines36b56882014-04-23 16:57:46 -0700152 BlockNo = std::prev(MBBI)->getNumber() + 1;
Chad Rosier0c1eac82012-06-19 23:37:57 +0000153
Chris Lattnere70cab02006-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!");
Stephen Hinesdce4a402014-05-29 02:49:00 -0700160 MBBNumbering[MBBI->getNumber()] = nullptr;
Chris Lattnere70cab02006-10-03 19:18:57 +0000161 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000162
Chris Lattnere70cab02006-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 Rosier0c1eac82012-06-19 23:37:57 +0000170 }
Chris Lattnere70cab02006-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 Gohman8e5f2c62008-07-07 23:14:23 +0000178/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
179/// of `new MachineInstr'.
180///
181MachineInstr *
Evan Chenge837dea2011-06-28 19:10:37 +0000182MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
Bill Wendling9bc96a52009-02-03 00:55:04 +0000183 DebugLoc DL, bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000184 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000185 MachineInstr(*this, MCID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000186}
Chris Lattnere70cab02006-10-03 19:18:57 +0000187
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000188/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
Dan Gohmanf451cb82010-02-10 16:03:48 +0000189/// 'Orig' instruction, identical in all ways except the instruction
Dan Gohman8e5f2c62008-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 Olesen84be3d52013-01-05 05:05:51 +0000200/// This function also serves as the MachineInstr destructor - the real
201/// ~MachineInstr() destructor must be empty.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000202void
203MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
Jakob Stoklund Olesenf1d015f2013-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 Olesen84be3d52013-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 Gohman8e5f2c62008-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 Gohmanc76909a2009-09-25 20:36:54 +0000232MachineMemOperand *
Chris Lattnerda39c392010-09-21 04:32:08 +0000233MachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000234 uint64_t s, unsigned base_alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800235 const AAMDNodes &AAInfo,
Rafael Espindola95d594c2012-03-31 18:14:00 +0000236 const MDNode *Ranges) {
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000237 return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800238 AAInfo, Ranges);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000239}
240
241MachineMemOperand *
242MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
243 int64_t Offset, uint64_t Size) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700244 if (MMO->getValue())
245 return new (Allocator)
246 MachineMemOperand(MachinePointerInfo(MMO->getValue(),
247 MMO->getOffset()+Offset),
248 MMO->getFlags(), Size,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800249 MMO->getBaseAlignment());
Dan Gohman95531882010-03-18 18:49:47 +0000250 return new (Allocator)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700251 MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
Chris Lattnerda39c392010-09-21 04:32:08 +0000252 MMO->getOffset()+Offset),
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000253 MMO->getFlags(), Size,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800254 MMO->getBaseAlignment());
Dan Gohmanc76909a2009-09-25 20:36:54 +0000255}
256
257MachineInstr::mmo_iterator
258MachineFunction::allocateMemRefsArray(unsigned long Num) {
259 return Allocator.Allocate<MachineMemOperand *>(Num);
260}
261
Dan Gohman91e69c32009-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 Lattner93a95ae2010-09-21 04:46:39 +0000282 getMachineMemOperand((*I)->getPointerInfo(),
Dan Gohman91e69c32009-10-09 18:10:05 +0000283 (*I)->getFlags() & ~MachineMemOperand::MOStore,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000284 (*I)->getSize(), (*I)->getBaseAlignment(),
Stephen Hines37ed9c12014-12-01 14:51:49 -0800285 (*I)->getAAInfo());
Dan Gohman91e69c32009-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 Lattner93a95ae2010-09-21 04:46:39 +0000314 getMachineMemOperand((*I)->getPointerInfo(),
Dan Gohman91e69c32009-10-09 18:10:05 +0000315 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
Dan Gohmanf96e4bd2010-10-20 00:31:05 +0000316 (*I)->getSize(), (*I)->getBaseAlignment(),
Stephen Hines37ed9c12014-12-01 14:51:49 -0800317 (*I)->getAAInfo());
Dan Gohman91e69c32009-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 Renb720be62012-09-11 22:23:19 +0000326#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000327void MachineFunction::dump() const {
David Greenedc554812010-01-04 23:39:17 +0000328 print(dbgs());
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000329}
Manman Ren77e300e2012-09-06 19:06:06 +0000330#endif
Chris Lattner10491642002-10-30 00:48:05 +0000331
Craig Topper96601ca2012-08-22 06:07:19 +0000332StringRef MachineFunction::getName() const {
333 assert(getFunction() && "No function!");
334 return getFunction()->getName();
335}
336
Jakob Stoklund Olesenf4a1e1a2010-10-26 20:21:46 +0000337void MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const {
David Blaikie986d76d2012-08-22 17:18:53 +0000338 OS << "# Machine code for function " << getName() << ": ";
Jakob Stoklund Olesen1f141682012-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 Lattner955fad12002-12-28 20:37:16 +0000345
346 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000347 FrameInfo->print(*this, OS);
Chad Rosier0c1eac82012-06-19 23:37:57 +0000348
Nate Begeman37efe672006-04-22 18:53:45 +0000349 // Print JumpTable Information
Chris Lattner071c62f2010-01-25 23:26:13 +0000350 if (JumpTableInfo)
351 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000352
353 // Print Constant Pool
Chris Lattnerd74c5562009-08-23 01:12:47 +0000354 ConstantPool->print(OS);
Chad Rosier0c1eac82012-06-19 23:37:57 +0000355
Stephen Hines37ed9c12014-12-01 14:51:49 -0800356 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
Chad Rosier0c1eac82012-06-19 23:37:57 +0000357
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000358 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000359 OS << "Function Live Ins: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000360 for (MachineRegisterInfo::livein_iterator
361 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Jakob Stoklund Olesene2f669f2011-05-02 20:06:30 +0000362 OS << PrintReg(I->first, TRI);
Chris Lattner4e920272006-05-16 05:55:30 +0000363 if (I->second)
Jakob Stoklund Olesene2f669f2011-05-02 20:06:30 +0000364 OS << " in " << PrintReg(I->second, TRI);
Stephen Hines36b56882014-04-23 16:57:46 -0700365 if (std::next(I) != E)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000366 OS << ", ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000367 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000368 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000369 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000370
Stephen Hinesdce4a402014-05-29 02:49:00 -0700371 for (const auto &BB : *this) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000372 OS << '\n';
Stephen Hinesdce4a402014-05-29 02:49:00 -0700373 BB.print(OS, Indexes);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000374 }
Brian Gaeke47b71642004-03-29 21:58:31 +0000375
David Blaikie986d76d2012-08-22 17:18:53 +0000376 OS << "\n# End machine code for function " << getName() << ".\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000377}
378
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000379namespace llvm {
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000380 template<>
381 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Tobias Grossera10d5982009-11-30 12:38:13 +0000382
383 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
384
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000385 static std::string getGraphName(const MachineFunction *F) {
Craig Topper96601ca2012-08-22 06:07:19 +0000386 return "CFG for '" + F->getName().str() + "' function";
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000387 }
388
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000389 std::string getNodeLabel(const MachineBasicBlock *Node,
390 const MachineFunction *Graph) {
Chris Lattnercf143a42009-08-23 03:13:20 +0000391 std::string OutStr;
392 {
393 raw_string_ostream OSS(OutStr);
Jakob Stoklund Olesene5f4e9f2010-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 Lattnercf143a42009-08-23 03:13:20 +0000400 Node->print(OSS);
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000401 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000402
Alkis Evlogimenos76d9dac2004-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 Evlogimenos71bf4042004-07-08 00:47:58 +0000414}
415
416void MachineFunction::viewCFG() const
417{
Jim Laskey851a22d2005-10-12 12:09:05 +0000418#ifndef NDEBUG
David Blaikie986d76d2012-08-22 17:18:53 +0000419 ViewGraph(this, "mf" + getName());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000420#else
Dan Gohman643fffe2010-07-07 17:28:45 +0000421 errs() << "MachineFunction::viewCFG is only available in debug builds on "
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000422 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000423#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000424}
425
426void MachineFunction::viewCFGOnly() const
427{
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000428#ifndef NDEBUG
David Blaikie986d76d2012-08-22 17:18:53 +0000429 ViewGraph(this, "mf" + getName(), true);
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000430#else
Dan Gohman643fffe2010-07-07 17:28:45 +0000431 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000432 << "systems with Graphviz or gv!\n";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000433#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000434}
435
Bob Wilson998e1252009-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 Patel68e6bee2011-02-21 23:21:26 +0000439 const TargetRegisterClass *RC) {
Evan Cheng39460432010-05-24 21:33:37 +0000440 MachineRegisterInfo &MRI = getRegInfo();
441 unsigned VReg = MRI.getLiveInVirtReg(PReg);
442 if (VReg) {
Stephen Hines36b56882014-04-23 16:57:46 -0700443 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 Cheng39460432010-05-24 21:33:37 +0000453 return VReg;
454 }
455 VReg = MRI.createVirtualRegister(RC);
456 MRI.addLiveIn(PReg, VReg);
Bob Wilson998e1252009-04-20 18:36:57 +0000457 return VReg;
458}
459
Chris Lattner589c6f62010-01-26 06:28:43 +0000460/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
Bill Wendling07d31772010-06-29 22:34:52 +0000461/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
462/// normal 'L' label is returned.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700463MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
Bill Wendling07d31772010-06-29 22:34:52 +0000464 bool isLinkerPrivate) const {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800465 const DataLayout *DL = getSubtarget().getDataLayout();
Chris Lattner589c6f62010-01-26 06:28:43 +0000466 assert(JumpTableInfo && "No jump tables");
Chandler Carruthde4c0802010-01-27 10:27:10 +0000467 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
Chad Rosier0c1eac82012-06-19 23:37:57 +0000468
Stephen Hines36b56882014-04-23 16:57:46 -0700469 const char *Prefix = isLinkerPrivate ? DL->getLinkerPrivateGlobalPrefix() :
470 DL->getPrivateGlobalPrefix();
Chris Lattner589c6f62010-01-26 06:28:43 +0000471 SmallString<60> Name;
472 raw_svector_ostream(Name)
473 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
Chris Lattner9b97a732010-03-30 18:10:53 +0000474 return Ctx.GetOrCreateSymbol(Name.str());
Chris Lattner589c6f62010-01-26 06:28:43 +0000475}
476
Chris Lattner142b5312010-11-14 22:48:15 +0000477/// getPICBaseSymbol - Return a function-local symbol to represent the PIC
478/// base.
479MCSymbol *MachineFunction::getPICBaseSymbol() const {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800480 const DataLayout *DL = getSubtarget().getDataLayout();
Stephen Hines36b56882014-04-23 16:57:46 -0700481 return Ctx.GetOrCreateSymbol(Twine(DL->getPrivateGlobalPrefix())+
Chris Lattner142b5312010-11-14 22:48:15 +0000482 Twine(getFunctionNumber())+"$pb");
483}
Chris Lattner589c6f62010-01-26 06:28:43 +0000484
Chris Lattner955fad12002-12-28 20:37:16 +0000485//===----------------------------------------------------------------------===//
Chris Lattnereb24db92002-12-28 21:08:26 +0000486// MachineFrameInfo implementation
Chris Lattner955fad12002-12-28 20:37:16 +0000487//===----------------------------------------------------------------------===//
488
Manman Rendc8126b2012-12-04 00:26:44 +0000489/// ensureMaxAlignment - Make sure the function is at least Align bytes
490/// aligned.
491void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800492 if (!StackRealignable || !RealignOption)
493 assert(Align <= StackAlignment &&
Manman Ren69261a62012-12-04 00:52:33 +0000494 "For targets without stack realignment, Align is out of limit!");
Manman Rendc8126b2012-12-04 00:26:44 +0000495 if (MaxAlignment < Align) MaxAlignment = Align;
496}
497
Manman Ren69261a62012-12-04 00:52:33 +0000498/// clampStackAlignment - Clamp the alignment if requested and emit a warning.
Bob Wilson8f637ad2013-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 Ren69261a62012-12-04 00:52:33 +0000506 return StackAlign;
507}
508
Bob Wilson8f637ad2013-02-08 20:35:15 +0000509/// CreateStackObject - Create a new statically sized stack object, returning
510/// a nonnegative identifier to represent it.
Manman Rendc8126b2012-12-04 00:26:44 +0000511///
Bob Wilson8f637ad2013-02-08 20:35:15 +0000512int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
Stephen Hines36b56882014-04-23 16:57:46 -0700513 bool isSS, const AllocaInst *Alloca) {
Manman Rendc8126b2012-12-04 00:26:44 +0000514 assert(Size != 0 && "Cannot allocate zero size stack objects!");
Stephen Hines37ed9c12014-12-01 14:51:49 -0800515 Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
516 Alignment, StackAlignment);
517 Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca,
518 !isSS));
Manman Rendc8126b2012-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) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800531 Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
532 Alignment, StackAlignment);
Stephen Hines36b56882014-04-23 16:57:46 -0700533 CreateStackObject(Size, Alignment, true);
Manman Rendc8126b2012-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///
Stephen Hines36b56882014-04-23 16:57:46 -0700544int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
545 const AllocaInst *Alloca) {
Manman Rendc8126b2012-12-04 00:26:44 +0000546 HasVarSizedObjects = true;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800547 Alignment = clampStackAlignment(!StackRealignable || !RealignOption,
548 Alignment, StackAlignment);
549 Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
Manman Rendc8126b2012-12-04 00:26:44 +0000550 ensureMaxAlignment(Alignment);
551 return (int)Objects.size()-NumFixedObjects-1;
552}
553
Chris Lattner1612faa2008-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,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800560 bool Immutable, bool isAliased) {
Chris Lattner1612faa2008-01-25 07:19:06 +0000561 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
Evan Chengf2f49032010-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.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800566 unsigned Align = MinAlign(SPOffset, StackAlignment);
567 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
568 StackAlignment);
Evan Chengf2f49032010-07-04 18:52:05 +0000569 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
Nadav Rotemc05d3062012-09-06 09:17:37 +0000570 /*isSS*/ false,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800571 /*Alloca*/ nullptr, isAliased));
Chris Lattner1612faa2008-01-25 07:19:06 +0000572 return -++NumFixedObjects;
573}
574
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700575/// 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) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800579 unsigned Align = MinAlign(SPOffset, StackAlignment);
580 Align = clampStackAlignment(!StackRealignable || !RealignOption, Align,
581 StackAlignment);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700582 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset,
583 /*Immutable*/ true,
584 /*isSS*/ true,
Stephen Hines37ed9c12014-12-01 14:51:49 -0800585 /*Alloca*/ nullptr,
586 /*isAliased*/ false));
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700587 return -++NumFixedObjects;
588}
Chris Lattner1612faa2008-01-25 07:19:06 +0000589
Jakob Stoklund Olesen4a0f08c2009-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();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800596 const TargetRegisterInfo *TRI = TM.getSubtargetImpl()->getRegisterInfo();
Jakob Stoklund Olesen4a0f08c2009-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
Stephen Hinesdce4a402014-05-29 02:49:00 -0700604 for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
Jakob Stoklund Olesen4a0f08c2009-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 Finkel0cc52c62013-03-14 21:15:20 +0000620unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800621 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
622 const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
Hal Finkel0cc52c62013-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 Olesen4a0f08c2009-08-13 16:19:33 +0000668
Chris Lattnerd74c5562009-08-23 01:12:47 +0000669void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
Dan Gohman0ba90f32009-10-31 20:19:03 +0000670 if (Objects.empty()) return;
671
Stephen Hines37ed9c12014-12-01 14:51:49 -0800672 const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
Matthijs Kooijman06140882008-11-03 11:16:43 +0000673 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
Chris Lattner9085d8a2003-01-16 18:35:57 +0000674
Dan Gohman0ba90f32009-10-31 20:19:03 +0000675 OS << "Frame Objects:\n";
676
Chris Lattner955fad12002-12-28 20:37:16 +0000677 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
678 const StackObject &SO = Objects[i];
Dan Gohman0ba90f32009-10-31 20:19:03 +0000679 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
Evan Chengd3653122008-02-27 03:04:06 +0000680 if (SO.Size == ~0ULL) {
681 OS << "dead\n";
682 continue;
683 }
Chris Lattner955fad12002-12-28 20:37:16 +0000684 if (SO.Size == 0)
685 OS << "variable sized";
686 else
Dan Gohman0ba90f32009-10-31 20:19:03 +0000687 OS << "size=" << SO.Size;
688 OS << ", align=" << SO.Alignment;
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000689
Chris Lattner955fad12002-12-28 20:37:16 +0000690 if (i < NumFixedObjects)
Dan Gohman0ba90f32009-10-31 20:19:03 +0000691 OS << ", fixed";
Chris Lattner955fad12002-12-28 20:37:16 +0000692 if (i < NumFixedObjects || SO.SPOffset != -1) {
Chris Lattnera401b1e2007-04-25 04:20:54 +0000693 int64_t Off = SO.SPOffset - ValOffset;
Dan Gohman0ba90f32009-10-31 20:19:03 +0000694 OS << ", at location [SP";
Chris Lattner9085d8a2003-01-16 18:35:57 +0000695 if (Off > 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000696 OS << "+" << Off;
Chris Lattner9085d8a2003-01-16 18:35:57 +0000697 else if (Off < 0)
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000698 OS << Off;
Chris Lattner955fad12002-12-28 20:37:16 +0000699 OS << "]";
700 }
701 OS << "\n";
702 }
Chris Lattner955fad12002-12-28 20:37:16 +0000703}
704
Manman Renb720be62012-09-11 22:23:19 +0000705#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chris Lattner9085d8a2003-01-16 18:35:57 +0000706void MachineFrameInfo::dump(const MachineFunction &MF) const {
David Greenedc554812010-01-04 23:39:17 +0000707 print(MF, dbgs());
Chris Lattner9085d8a2003-01-16 18:35:57 +0000708}
Manman Ren77e300e2012-09-06 19:06:06 +0000709#endif
Chris Lattner955fad12002-12-28 20:37:16 +0000710
Chris Lattner955fad12002-12-28 20:37:16 +0000711//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000712// MachineJumpTableInfo implementation
713//===----------------------------------------------------------------------===//
714
Chris Lattner071c62f2010-01-25 23:26:13 +0000715/// getEntrySize - Return the size of each entry in the jump table.
Micah Villmow3574eca2012-10-08 16:38:25 +0000716unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
Chris Lattner071c62f2010-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 Carruth426c2bf2012-11-01 09:14:31 +0000721 return TD.getPointerSize();
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +0000722 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
723 return 8;
Chris Lattner071c62f2010-01-25 23:26:13 +0000724 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
725 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000726 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000727 return 4;
Richard Osborne95da6052010-03-11 14:58:16 +0000728 case MachineJumpTableInfo::EK_Inline:
729 return 0;
Chris Lattner071c62f2010-01-25 23:26:13 +0000730 }
Craig Topperaae875c2012-02-06 08:17:43 +0000731 llvm_unreachable("Unknown jump table encoding!");
Chris Lattner071c62f2010-01-25 23:26:13 +0000732}
733
734/// getEntryAlignment - Return the alignment of each entry in the jump table.
Micah Villmow3574eca2012-10-08 16:38:25 +0000735unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
Chris Lattner071c62f2010-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 Carruth426c2bf2012-11-01 09:14:31 +0000741 return TD.getPointerABIAlignment();
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +0000742 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
743 return TD.getABIIntegerTypeAlignment(64);
Chris Lattner071c62f2010-01-25 23:26:13 +0000744 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
745 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000746 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000747 return TD.getABIIntegerTypeAlignment(32);
Richard Osborne95da6052010-03-11 14:58:16 +0000748 case MachineJumpTableInfo::EK_Inline:
749 return 1;
Chris Lattner071c62f2010-01-25 23:26:13 +0000750 }
Craig Topperaae875c2012-02-06 08:17:43 +0000751 llvm_unreachable("Unknown jump table encoding!");
Chris Lattner071c62f2010-01-25 23:26:13 +0000752}
753
Bob Wilsond1ec31d2010-03-18 18:42:41 +0000754/// createJumpTableIndex - Create a new jump table entry in the jump table info.
Nate Begeman37efe672006-04-22 18:53:45 +0000755///
Bob Wilsond1ec31d2010-03-18 18:42:41 +0000756unsigned MachineJumpTableInfo::createJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000757 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000758 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000759 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
760 return JumpTables.size()-1;
761}
762
Dan Gohman593ea052009-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 Lattnerbeeb93e2010-01-26 05:58:28 +0000765bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
766 MachineBasicBlock *New) {
Dan Gohman593ea052009-04-15 01:18:49 +0000767 assert(Old != New && "Not making a change?");
768 bool MadeChange = false;
Jim Grosbach68bb60f2009-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 Lattnerbeeb93e2010-01-26 05:58:28 +0000776bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
777 MachineBasicBlock *Old,
778 MachineBasicBlock *New) {
Jim Grosbach68bb60f2009-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 Gohman593ea052009-04-15 01:18:49 +0000787 return MadeChange;
788}
Nate Begeman37efe672006-04-22 18:53:45 +0000789
Chris Lattnerd74c5562009-08-23 01:12:47 +0000790void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000791 if (JumpTables.empty()) return;
792
793 OS << "Jump Tables:\n";
794
Nate Begeman37efe672006-04-22 18:53:45 +0000795 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Dan Gohman0ba90f32009-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 Begeman37efe672006-04-22 18:53:45 +0000799 }
Dan Gohman0ba90f32009-10-31 20:19:03 +0000800
801 OS << '\n';
Nate Begeman37efe672006-04-22 18:53:45 +0000802}
803
Manman Renb720be62012-09-11 22:23:19 +0000804#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greenedc554812010-01-04 23:39:17 +0000805void MachineJumpTableInfo::dump() const { print(dbgs()); }
Manman Ren77e300e2012-09-06 19:06:06 +0000806#endif
Nate Begeman37efe672006-04-22 18:53:45 +0000807
808
809//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000810// MachineConstantPool implementation
811//===----------------------------------------------------------------------===//
812
David Blaikie2d24e2a2011-12-20 02:50:00 +0000813void MachineConstantPoolValue::anchor() { }
814
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000815const DataLayout *MachineConstantPool::getDataLayout() const {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800816 return TM.getSubtargetImpl()->getDataLayout();
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000817}
818
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000819Type *MachineConstantPoolEntry::getType() const {
Evan Cheng9abd7c32006-09-14 05:50:57 +0000820 if (isMachineConstantPoolEntry())
Chris Lattnercb459632009-07-21 23:34:23 +0000821 return Val.MachineCPVal->getType();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000822 return Val.ConstVal->getType();
823}
824
Chris Lattnercb459632009-07-21 23:34:23 +0000825
Chris Lattner354c0162009-07-21 23:36:01 +0000826unsigned MachineConstantPoolEntry::getRelocationInfo() const {
Chris Lattnercb459632009-07-21 23:34:23 +0000827 if (isMachineConstantPoolEntry())
Chris Lattner354c0162009-07-21 23:36:01 +0000828 return Val.MachineCPVal->getRelocationInfo();
Chris Lattner7cf12c72009-07-22 00:05:44 +0000829 return Val.ConstVal->getRelocationInfo();
Chris Lattnercb459632009-07-21 23:34:23 +0000830}
831
Stephen Hines37ed9c12014-12-01 14:51:49 -0800832SectionKind
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 Chengd6594ae2006-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 Zwarich55678692011-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 Chengd6594ae2006-09-12 21:00:35 +0000871}
872
Dan Gohman83f61202009-10-28 01:12:16 +0000873/// CanShareConstantPoolEntry - Test whether the given two constants
874/// can be allocated the same constant pool entry.
Dan Gohman46510a72010-04-15 01:51:59 +0000875static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
Micah Villmow3574eca2012-10-08 16:38:25 +0000876 const DataLayout *TD) {
Dan Gohman83f61202009-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 Lattner73858d12012-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;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700888
Dan Gohman83f61202009-10-28 01:12:16 +0000889 // For now, only support constants with the same size.
Chris Lattner6b0dc922012-01-26 21:37:55 +0000890 uint64_t StoreSize = TD->getTypeStoreSize(A->getType());
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700891 if (StoreSize != TD->getTypeStoreSize(B->getType()) || StoreSize > 128)
Dan Gohman83f61202009-10-28 01:12:16 +0000892 return false;
893
Chris Lattner73858d12012-01-27 01:46:00 +0000894 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
Dan Gohman83f61202009-10-28 01:12:16 +0000895
Chris Lattner73858d12012-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 Villmow3574eca2012-10-08 16:38:25 +0000899 // DataLayout.
Chris Lattner73858d12012-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 Rosier0c1eac82012-06-19 23:37:57 +0000912
Chris Lattner73858d12012-01-27 01:46:00 +0000913 return A == B;
Dan Gohman83f61202009-10-28 01:12:16 +0000914}
915
Chris Lattner3029f922006-02-09 04:46:04 +0000916/// getConstantPoolIndex - Create a new entry in the constant pool or return
Dan Gohman05ae9832008-09-16 20:45:53 +0000917/// an existing one. User must specify the log2 of the minimum required
918/// alignment for the object.
Chris Lattner3029f922006-02-09 04:46:04 +0000919///
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700920unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
Chris Lattner3029f922006-02-09 04:46:04 +0000921 unsigned Alignment) {
922 assert(Alignment && "Alignment must be specified!");
923 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman83f61202009-10-28 01:12:16 +0000924
Chris Lattner3029f922006-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 Lattner3029f922006-02-09 04:46:04 +0000928 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman83f61202009-10-28 01:12:16 +0000929 if (!Constants[i].isMachineConstantPoolEntry() &&
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000930 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C,
931 getDataLayout())) {
Dan Gohman83f61202009-10-28 01:12:16 +0000932 if ((unsigned)Constants[i].getAlignment() < Alignment)
933 Constants[i].Alignment = Alignment;
Chris Lattner3029f922006-02-09 04:46:04 +0000934 return i;
Dan Gohman83f61202009-10-28 01:12:16 +0000935 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000936
Evan Cheng1606e8e2009-03-13 07:51:59 +0000937 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +0000938 return Constants.size()-1;
939}
940
Evan Chengd6594ae2006-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 Rosier0c1eac82012-06-19 23:37:57 +0000945
Evan Chengd6594ae2006-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 Chengd6594ae2006-09-12 21:00:35 +0000949 int Idx = V->getExistingMachineCPValue(this, Alignment);
Cameron Zwarich55678692011-02-22 08:54:30 +0000950 if (Idx != -1) {
951 MachineCPVsSharingEntries.insert(V);
Evan Chengd6594ae2006-09-12 21:00:35 +0000952 return (unsigned)Idx;
Cameron Zwarich55678692011-02-22 08:54:30 +0000953 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000954
955 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +0000956 return Constants.size()-1;
957}
958
Chris Lattner62ca3252008-08-23 22:53:13 +0000959void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000960 if (Constants.empty()) return;
961
962 OS << "Constant Pool:\n";
Evan Chengb8973bd2006-01-31 22:23:14 +0000963 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman0ba90f32009-10-31 20:19:03 +0000964 OS << " cp#" << i << ": ";
Evan Chengd6594ae2006-09-12 21:00:35 +0000965 if (Constants[i].isMachineConstantPoolEntry())
966 Constants[i].Val.MachineCPVal->print(OS);
967 else
Stephen Hines36b56882014-04-23 16:57:46 -0700968 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
Dan Gohman0ba90f32009-10-31 20:19:03 +0000969 OS << ", align=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +0000970 OS << "\n";
971 }
Chris Lattner4d149cd2003-01-13 00:23:03 +0000972}
973
Manman Renb720be62012-09-11 22:23:19 +0000974#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
David Greenedc554812010-01-04 23:39:17 +0000975void MachineConstantPool::dump() const { print(dbgs()); }
Manman Ren77e300e2012-09-06 19:06:06 +0000976#endif