blob: 3495319670a5d0e7d0164d5e7f292411cfbc5998 [file] [log] [blame]
Eugene Zelenko887aef72017-10-10 22:33:29 +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"
Eugene Zelenko887aef72017-10-10 22:33:29 +000017#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/DenseSet.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SmallString.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000022#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Analysis/ConstantFolding.h"
David Majnemer1114aa22015-12-02 23:06:39 +000026#include "llvm/Analysis/EHPersonalities.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000027#include "llvm/CodeGen/MachineBasicBlock.h"
Chris Lattner6b276072012-01-27 01:47:28 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
30#include "llvm/CodeGen/MachineInstr.h"
Nate Begeman37efe672006-04-22 18:53:45 +000031#include "llvm/CodeGen/MachineJumpTableInfo.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000032#include "llvm/CodeGen/MachineMemOperand.h"
Chris Lattner820e55e2010-04-05 05:49:50 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenzde0129a2015-08-11 23:09:45 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000036#include "llvm/CodeGen/TargetFrameLowering.h"
37#include "llvm/CodeGen/TargetLowering.h"
38#include "llvm/CodeGen/TargetRegisterInfo.h"
39#include "llvm/CodeGen/TargetSubtargetInfo.h"
Heejin Ahnc430c832018-06-19 00:26:39 +000040#include "llvm/CodeGen/WasmEHFuncInfo.h"
Reid Kleckner595419d2015-11-17 21:10:25 +000041#include "llvm/CodeGen/WinEHFuncInfo.h"
Nico Weber0f38c602018-04-30 14:59:11 +000042#include "llvm/Config/llvm-config.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000043#include "llvm/IR/Attributes.h"
44#include "llvm/IR/BasicBlock.h"
45#include "llvm/IR/Constant.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000046#include "llvm/IR/DataLayout.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000047#include "llvm/IR/DerivedTypes.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000048#include "llvm/IR/Function.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000049#include "llvm/IR/GlobalValue.h"
50#include "llvm/IR/Instruction.h"
51#include "llvm/IR/Instructions.h"
52#include "llvm/IR/Metadata.h"
Mehdi Amini33c4d682015-07-07 18:20:57 +000053#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith9a61a422015-06-26 22:04:20 +000054#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000055#include "llvm/IR/Value.h"
Chris Lattnerbeeb93e2010-01-26 05:58:28 +000056#include "llvm/MC/MCContext.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000057#include "llvm/MC/MCSymbol.h"
58#include "llvm/MC/SectionKind.h"
59#include "llvm/Support/Casting.h"
60#include "llvm/Support/CommandLine.h"
61#include "llvm/Support/Compiler.h"
62#include "llvm/Support/DOTGraphTraits.h"
David Greenedc554812010-01-04 23:39:17 +000063#include "llvm/Support/Debug.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000064#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf28bbda2006-10-03 20:19:23 +000065#include "llvm/Support/GraphWriter.h"
Chris Lattner944fac72008-08-23 22:23:09 +000066#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000067#include "llvm/Target/TargetMachine.h"
Eugene Zelenko887aef72017-10-10 22:33:29 +000068#include <algorithm>
69#include <cassert>
70#include <cstddef>
71#include <cstdint>
72#include <iterator>
73#include <string>
74#include <utility>
75#include <vector>
76
Chris Lattner07f32d42003-12-20 09:17:07 +000077using namespace llvm;
Chris Lattnerf2868ce2002-02-03 07:54:50 +000078
Chandler Carruth283b3992014-04-21 22:55:11 +000079#define DEBUG_TYPE "codegen"
80
Chad Rosier8b2e79c2015-12-29 18:18:07 +000081static cl::opt<unsigned>
Eugene Zelenko887aef72017-10-10 22:33:29 +000082AlignAllFunctions("align-all-functions",
83 cl::desc("Force the alignment of all functions."),
84 cl::init(0), cl::Hidden);
Chad Rosier8b2e79c2015-12-29 18:18:07 +000085
Matthias Braun78efd692016-08-19 22:31:45 +000086static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
Eugene Zelenko887aef72017-10-10 22:33:29 +000087 using P = MachineFunctionProperties::Property;
88
Matthias Braun78efd692016-08-19 22:31:45 +000089 switch(Prop) {
Quentin Colombet6dff9d72016-08-26 23:49:01 +000090 case P::FailedISel: return "FailedISel";
Matthias Braun78efd692016-08-19 22:31:45 +000091 case P::IsSSA: return "IsSSA";
92 case P::Legalized: return "Legalized";
Matthias Braundb9ce2f2016-08-23 21:19:49 +000093 case P::NoPHIs: return "NoPHIs";
Matthias Braun690a3cb2016-08-25 01:27:13 +000094 case P::NoVRegs: return "NoVRegs";
Matthias Braun78efd692016-08-19 22:31:45 +000095 case P::RegBankSelected: return "RegBankSelected";
96 case P::Selected: return "Selected";
97 case P::TracksLiveness: return "TracksLiveness";
Derek Schuffd239bc52016-03-29 20:28:20 +000098 }
Matthias Braun8b3f7542016-08-19 23:03:28 +000099 llvm_unreachable("Invalid machine function property");
Matthias Braun78efd692016-08-19 22:31:45 +0000100}
101
Aditya Nandakumar3b417742018-09-20 23:01:56 +0000102// Pin the vtable to this file.
103void MachineFunction::Delegate::anchor() {}
104
Matthias Braun78efd692016-08-19 22:31:45 +0000105void MachineFunctionProperties::print(raw_ostream &OS) const {
106 const char *Separator = "";
107 for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
108 if (!Properties[I])
109 continue;
110 OS << Separator << getPropertyName(static_cast<Property>(I));
111 Separator = ", ";
112 }
Derek Schuffd239bc52016-03-29 20:28:20 +0000113}
114
Chris Lattnerb84822f2010-01-26 04:35:26 +0000115//===----------------------------------------------------------------------===//
Chris Lattner227c3d32002-10-28 01:12:41 +0000116// MachineFunction implementation
Chris Lattnerb84822f2010-01-26 04:35:26 +0000117//===----------------------------------------------------------------------===//
Chris Lattner9d5d7592005-01-29 18:41:25 +0000118
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000119// Out-of-line virtual method.
Eugene Zelenko887aef72017-10-10 22:33:29 +0000120MachineFunctionInfo::~MachineFunctionInfo() = default;
Chris Lattnera70e2e32009-09-15 22:44:26 +0000121
Duncan P. N. Exon Smith9c80b352016-08-30 18:40:47 +0000122void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000123 MBB->getParent()->DeleteMachineBasicBlock(MBB);
Tanya Lattner792699c2004-05-24 06:11:51 +0000124}
Chris Lattner227c3d32002-10-28 01:12:41 +0000125
Charles Davisc15a9072016-04-09 23:34:42 +0000126static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
Matthias Braundfcb4f52017-12-15 22:22:46 +0000127 const Function &F) {
128 if (F.hasFnAttribute(Attribute::StackAlignment))
129 return F.getFnStackAlignment();
Charles Davisc15a9072016-04-09 23:34:42 +0000130 return STI->getFrameLowering()->getStackAlignment();
131}
132
Matthias Braundad0df62018-11-05 23:49:14 +0000133MachineFunction::MachineFunction(const Function &F,
134 const LLVMTargetMachine &Target,
Matthias Braundfcb4f52017-12-15 22:22:46 +0000135 const TargetSubtargetInfo &STI,
Rafael Espindolad1494d52014-10-14 18:53:16 +0000136 unsigned FunctionNum, MachineModuleInfo &mmi)
Matthias Braundfcb4f52017-12-15 22:22:46 +0000137 : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
Quentin Colombetac0c6d32016-08-26 22:32:53 +0000138 FunctionNumber = FunctionNum;
139 init();
140}
141
Aditya Nandakumar66f23702019-01-16 00:40:37 +0000142void MachineFunction::handleInsertion(MachineInstr &MI) {
Aditya Nandakumar3b417742018-09-20 23:01:56 +0000143 if (TheDelegate)
144 TheDelegate->MF_HandleInsertion(MI);
145}
146
Aditya Nandakumar66f23702019-01-16 00:40:37 +0000147void MachineFunction::handleRemoval(MachineInstr &MI) {
Aditya Nandakumar3b417742018-09-20 23:01:56 +0000148 if (TheDelegate)
149 TheDelegate->MF_HandleRemoval(MI);
150}
151
Quentin Colombetac0c6d32016-08-26 22:32:53 +0000152void MachineFunction::init() {
Derek Schuff40e099b2016-04-11 23:32:13 +0000153 // Assume the function starts in SSA form with correct liveness.
Derek Schuffcfaa4e02016-04-04 18:03:29 +0000154 Properties.set(MachineFunctionProperties::Property::IsSSA);
Derek Schuff40e099b2016-04-11 23:32:13 +0000155 Properties.set(MachineFunctionProperties::Property::TracksLiveness);
Eric Christopherc7204052014-10-08 07:51:41 +0000156 if (STI->getRegisterInfo())
Eric Christopher99cd10f2014-08-12 08:00:56 +0000157 RegInfo = new (Allocator) MachineRegisterInfo(this);
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000158 else
Craig Topper4ba84432014-04-14 00:51:57 +0000159 RegInfo = nullptr;
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000160
Craig Topper4ba84432014-04-14 00:51:57 +0000161 MFInfo = nullptr;
Reid Kleckner67e74632016-04-11 17:54:03 +0000162 // We can realign the stack if the target supports it and the user hasn't
163 // explicitly asked us not to.
164 bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
Matthias Braundfcb4f52017-12-15 22:22:46 +0000165 !F.hasFnAttribute("no-realign-stack");
Reid Kleckner67e74632016-04-11 17:54:03 +0000166 FrameInfo = new (Allocator) MachineFrameInfo(
Matthias Braundfcb4f52017-12-15 22:22:46 +0000167 getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
Reid Kleckner67e74632016-04-11 17:54:03 +0000168 /*ForceRealign=*/CanRealignSP &&
Matthias Braundfcb4f52017-12-15 22:22:46 +0000169 F.hasFnAttribute(Attribute::StackAlignment));
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000170
Matthias Braundfcb4f52017-12-15 22:22:46 +0000171 if (F.hasFnAttribute(Attribute::StackAlignment))
172 FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000173
Mehdi Amini33c4d682015-07-07 18:20:57 +0000174 ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
Eric Christopherc7204052014-10-08 07:51:41 +0000175 Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000176
Matthias Braundfcb4f52017-12-15 22:22:46 +0000177 // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
Sanjay Patelf3609832015-08-04 15:49:57 +0000178 // FIXME: Use Function::optForSize().
Matthias Braundfcb4f52017-12-15 22:22:46 +0000179 if (!F.hasFnAttribute(Attribute::OptimizeForSize))
Eric Christopherc7204052014-10-08 07:51:41 +0000180 Alignment = std::max(Alignment,
181 STI->getTargetLowering()->getPrefFunctionAlignment());
Bill Wendlingd10fa8b2013-06-17 20:41:25 +0000182
Chad Rosier8b2e79c2015-12-29 18:18:07 +0000183 if (AlignAllFunctions)
184 Alignment = AlignAllFunctions;
185
Craig Topper4ba84432014-04-14 00:51:57 +0000186 JumpTableInfo = nullptr;
Tobias Grosser378e97e2015-08-17 10:58:03 +0000187
Reid Kleckner595419d2015-11-17 21:10:25 +0000188 if (isFuncletEHPersonality(classifyEHPersonality(
Matthias Braundfcb4f52017-12-15 22:22:46 +0000189 F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
Reid Kleckner595419d2015-11-17 21:10:25 +0000190 WinEHInfo = new (Allocator) WinEHFuncInfo();
191 }
192
Heejin Ahnc430c832018-06-19 00:26:39 +0000193 if (isScopedEHPersonality(classifyEHPersonality(
194 F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
195 WasmEHInfo = new (Allocator) WasmEHFuncInfo();
196 }
197
Quentin Colombetac0c6d32016-08-26 22:32:53 +0000198 assert(Target.isCompatibleDataLayout(getDataLayout()) &&
Tobias Grosser378e97e2015-08-17 10:58:03 +0000199 "Can't create a MachineFunction using a Module with a "
200 "Target-incompatible DataLayout attached\n");
201
Jan Sjodin028255f2017-09-14 20:53:51 +0000202 PSVManager =
203 llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
204 getInstrInfo()));
Chris Lattner831fdcf2002-12-25 05:03:22 +0000205}
206
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000207MachineFunction::~MachineFunction() {
Quentin Colombetac0c6d32016-08-26 22:32:53 +0000208 clear();
209}
210
211void MachineFunction::clear() {
212 Properties.reset();
Jakob Stoklund Olesen84be3d52013-01-05 05:05:51 +0000213 // Don't call destructors on MachineInstr and MachineOperand. All of their
214 // memory comes from the BumpPtrAllocator which is about to be purged.
215 //
216 // Do call MachineBasicBlock destructors, it contains std::vectors.
217 for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
218 I->Insts.clearAndLeakNodesUnsafely();
Roman Tereshinbe8f7c92018-04-30 18:58:57 +0000219 MBBNumbering.clear();
Jakob Stoklund Olesen84be3d52013-01-05 05:05:51 +0000220
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000221 InstructionRecycler.clear(Allocator);
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000222 OperandRecycler.clear(Allocator);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000223 BasicBlockRecycler.clear(Allocator);
Reid Klecknerc86178e2017-09-05 20:14:58 +0000224 CodeViewAnnotations.clear();
Tim Northoverdd48fdc2017-03-09 21:12:06 +0000225 VariableDbgInfos.clear();
Bill Wendlingdd37b362009-06-25 00:32:48 +0000226 if (RegInfo) {
227 RegInfo->~MachineRegisterInfo();
228 Allocator.Deallocate(RegInfo);
229 }
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000230 if (MFInfo) {
Bill Wendlingdd37b362009-06-25 00:32:48 +0000231 MFInfo->~MachineFunctionInfo();
232 Allocator.Deallocate(MFInfo);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000233 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000234
235 FrameInfo->~MachineFrameInfo();
236 Allocator.Deallocate(FrameInfo);
237
238 ConstantPool->~MachineConstantPool();
239 Allocator.Deallocate(ConstantPool);
240
Chris Lattner071c62f2010-01-25 23:26:13 +0000241 if (JumpTableInfo) {
242 JumpTableInfo->~MachineJumpTableInfo();
243 Allocator.Deallocate(JumpTableInfo);
244 }
Reid Kleckner595419d2015-11-17 21:10:25 +0000245
246 if (WinEHInfo) {
247 WinEHInfo->~WinEHFuncInfo();
248 Allocator.Deallocate(WinEHInfo);
249 }
Heejin Ahn55297112018-09-28 20:54:04 +0000250
251 if (WasmEHInfo) {
252 WasmEHInfo->~WasmEHFuncInfo();
253 Allocator.Deallocate(WasmEHInfo);
254 }
Chris Lattner10491642002-10-30 00:48:05 +0000255}
256
Mehdi Amini33c4d682015-07-07 18:20:57 +0000257const DataLayout &MachineFunction::getDataLayout() const {
Matthias Braundfcb4f52017-12-15 22:22:46 +0000258 return F.getParent()->getDataLayout();
Mehdi Amini33c4d682015-07-07 18:20:57 +0000259}
260
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000261/// Get the JumpTableInfo for this function.
262/// If it does not already exist, allocate one.
Chris Lattner071c62f2010-01-25 23:26:13 +0000263MachineJumpTableInfo *MachineFunction::
264getOrCreateJumpTableInfo(unsigned EntryKind) {
265 if (JumpTableInfo) return JumpTableInfo;
Chad Rosier0c1eac82012-06-19 23:37:57 +0000266
Dan Gohman95531882010-03-18 18:49:47 +0000267 JumpTableInfo = new (Allocator)
Chris Lattner071c62f2010-01-25 23:26:13 +0000268 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
269 return JumpTableInfo;
270}
Chris Lattnere70cab02006-10-03 19:18:57 +0000271
Reid Klecknerbc1fd912014-04-10 22:58:43 +0000272/// Should we be emitting segmented stack stuff for the function
Quentin Colombet871b3df2016-01-19 22:31:12 +0000273bool MachineFunction::shouldSplitStack() const {
Matthias Braund3181392017-12-15 22:22:58 +0000274 return getFunction().hasFnAttribute("split-stack");
Reid Klecknerbc1fd912014-04-10 22:58:43 +0000275}
276
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000277/// This discards all of the MachineBasicBlock numbers and recomputes them.
278/// This guarantees that the MBB numbers are sequential, dense, and match the
279/// ordering of the blocks within the function. If a specific MachineBasicBlock
280/// is specified, only that block and those after it are renumbered.
Chris Lattnere70cab02006-10-03 19:18:57 +0000281void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
282 if (empty()) { MBBNumbering.clear(); return; }
283 MachineFunction::iterator MBBI, E = end();
Craig Topper4ba84432014-04-14 00:51:57 +0000284 if (MBB == nullptr)
Chris Lattnere70cab02006-10-03 19:18:57 +0000285 MBBI = begin();
286 else
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +0000287 MBBI = MBB->getIterator();
Chad Rosier0c1eac82012-06-19 23:37:57 +0000288
Chris Lattnere70cab02006-10-03 19:18:57 +0000289 // Figure out the block number this should have.
290 unsigned BlockNo = 0;
Chris Lattnerf28bbda2006-10-03 20:19:23 +0000291 if (MBBI != begin())
Benjamin Kramerd628f192014-03-02 12:27:27 +0000292 BlockNo = std::prev(MBBI)->getNumber() + 1;
Chad Rosier0c1eac82012-06-19 23:37:57 +0000293
Chris Lattnere70cab02006-10-03 19:18:57 +0000294 for (; MBBI != E; ++MBBI, ++BlockNo) {
295 if (MBBI->getNumber() != (int)BlockNo) {
296 // Remove use of the old number.
297 if (MBBI->getNumber() != -1) {
298 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
299 "MBB number mismatch!");
Craig Topper4ba84432014-04-14 00:51:57 +0000300 MBBNumbering[MBBI->getNumber()] = nullptr;
Chris Lattnere70cab02006-10-03 19:18:57 +0000301 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000302
Chris Lattnere70cab02006-10-03 19:18:57 +0000303 // If BlockNo is already taken, set that block's number to -1.
304 if (MBBNumbering[BlockNo])
305 MBBNumbering[BlockNo]->setNumber(-1);
306
Duncan P. N. Exon Smith9731c602015-10-09 19:40:45 +0000307 MBBNumbering[BlockNo] = &*MBBI;
Chris Lattnere70cab02006-10-03 19:18:57 +0000308 MBBI->setNumber(BlockNo);
309 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000310 }
Chris Lattnere70cab02006-10-03 19:18:57 +0000311
312 // Okay, all the blocks are renumbered. If we have compactified the block
313 // numbering, shrink MBBNumbering now.
314 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
315 MBBNumbering.resize(BlockNo);
316}
317
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000318/// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
Benjamin Krameraf18e012016-06-12 15:39:02 +0000319MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
320 const DebugLoc &DL,
321 bool NoImp) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000322 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
Jakob Stoklund Olesen9500e5d2012-12-20 22:53:58 +0000323 MachineInstr(*this, MCID, DL, NoImp);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000324}
Chris Lattnere70cab02006-10-03 19:18:57 +0000325
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000326/// Create a new MachineInstr which is a copy of the 'Orig' instruction,
327/// identical in all ways except the instruction has no parent, prev, or next.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000328MachineInstr *
329MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
330 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
331 MachineInstr(*this, *Orig);
332}
333
Matthias Braun18f24452017-08-22 23:56:30 +0000334MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
335 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
336 MachineInstr *FirstClone = nullptr;
337 MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
Eugene Zelenko887aef72017-10-10 22:33:29 +0000338 while (true) {
Matthias Braun18f24452017-08-22 23:56:30 +0000339 MachineInstr *Cloned = CloneMachineInstr(&*I);
340 MBB.insert(InsertBefore, Cloned);
341 if (FirstClone == nullptr) {
342 FirstClone = Cloned;
343 } else {
344 Cloned->bundleWithPred();
345 }
346
347 if (!I->isBundledWithSucc())
348 break;
349 ++I;
350 }
351 return *FirstClone;
352}
353
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000354/// Delete the given MachineInstr.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000355///
Jakob Stoklund Olesen84be3d52013-01-05 05:05:51 +0000356/// This function also serves as the MachineInstr destructor - the real
357/// ~MachineInstr() destructor must be empty.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000358void
359MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
Jakob Stoklund Olesenf1d015f2013-01-05 05:00:09 +0000360 // Strip it for parts. The operand array and the MI object itself are
361 // independently recyclable.
362 if (MI->Operands)
363 deallocateOperandArray(MI->CapOperands, MI->Operands);
Jakob Stoklund Olesen84be3d52013-01-05 05:05:51 +0000364 // Don't call ~MachineInstr() which must be trivial anyway because
365 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
366 // destructors.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000367 InstructionRecycler.Deallocate(Allocator, MI);
368}
369
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000370/// Allocate a new MachineBasicBlock. Use this instead of
371/// `new MachineBasicBlock'.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000372MachineBasicBlock *
373MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
374 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
375 MachineBasicBlock(*this, bb);
376}
377
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000378/// Delete the given MachineBasicBlock.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000379void
380MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
381 assert(MBB->getParent() == this && "MBB parent mismatch!");
382 MBB->~MachineBasicBlock();
383 BasicBlockRecycler.Deallocate(Allocator, MBB);
384}
385
Justin Lebar14fc45e2016-07-15 18:26:59 +0000386MachineMemOperand *MachineFunction::getMachineMemOperand(
387 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000388 unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +0000389 SyncScope::ID SSID, AtomicOrdering Ordering,
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000390 AtomicOrdering FailureOrdering) {
Justin Lebarf66cee62016-07-14 17:07:44 +0000391 return new (Allocator)
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000392 MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +0000393 SSID, Ordering, FailureOrdering);
Dan Gohmanc76909a2009-09-25 20:36:54 +0000394}
395
396MachineMemOperand *
397MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
398 int64_t Offset, uint64_t Size) {
Nick Lewyckyd63390c2014-04-15 07:22:52 +0000399 if (MMO->getValue())
400 return new (Allocator)
401 MachineMemOperand(MachinePointerInfo(MMO->getValue(),
402 MMO->getOffset()+Offset),
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000403 MMO->getFlags(), Size, MMO->getBaseAlignment(),
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +0000404 AAMDNodes(), nullptr, MMO->getSyncScopeID(),
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000405 MMO->getOrdering(), MMO->getFailureOrdering());
Dan Gohman95531882010-03-18 18:49:47 +0000406 return new (Allocator)
Nick Lewyckyd63390c2014-04-15 07:22:52 +0000407 MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
Chris Lattnerda39c392010-09-21 04:32:08 +0000408 MMO->getOffset()+Offset),
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000409 MMO->getFlags(), Size, MMO->getBaseAlignment(),
Konstantin Zhuravlyov8f856852017-07-11 22:23:00 +0000410 AAMDNodes(), nullptr, MMO->getSyncScopeID(),
Konstantin Zhuravlyova7e2c082016-10-15 22:01:18 +0000411 MMO->getOrdering(), MMO->getFailureOrdering());
Dan Gohmanc76909a2009-09-25 20:36:54 +0000412}
413
Hiroshi Inoueeeca49d2017-08-01 03:32:15 +0000414MachineMemOperand *
415MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
416 const AAMDNodes &AAInfo) {
417 MachinePointerInfo MPI = MMO->getValue() ?
418 MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
419 MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
420
421 return new (Allocator)
422 MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
423 MMO->getBaseAlignment(), AAInfo,
424 MMO->getRanges(), MMO->getSyncScopeID(),
425 MMO->getOrdering(), MMO->getFailureOrdering());
426}
427
Chandler Carruth2a752bf2018-08-16 21:30:05 +0000428MachineInstr::ExtraInfo *
429MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
430 MCSymbol *PreInstrSymbol,
431 MCSymbol *PostInstrSymbol) {
432 return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
433 PostInstrSymbol);
Dan Gohman91e69c32009-10-09 18:10:05 +0000434}
435
Alex Lorenz65671bf2015-07-21 16:59:53 +0000436const char *MachineFunction::createExternalSymbolName(StringRef Name) {
437 char *Dest = Allocator.Allocate<char>(Name.size() + 1);
Fangrui Song53a62242018-11-17 01:44:25 +0000438 llvm::copy(Name, Dest);
Alex Lorenz65671bf2015-07-21 16:59:53 +0000439 Dest[Name.size()] = 0;
440 return Dest;
441}
442
Matthias Braunee571f22018-07-26 00:27:47 +0000443uint32_t *MachineFunction::allocateRegMask() {
444 unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
445 unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
446 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
447 memset(Mask, 0, Size * sizeof(Mask[0]));
448 return Mask;
449}
450
Aaron Ballman1d03d382017-10-15 14:32:27 +0000451#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000452LLVM_DUMP_METHOD void MachineFunction::dump() const {
David Greenedc554812010-01-04 23:39:17 +0000453 print(dbgs());
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000454}
Manman Ren77e300e2012-09-06 19:06:06 +0000455#endif
Chris Lattner10491642002-10-30 00:48:05 +0000456
Craig Topper96601ca2012-08-22 06:07:19 +0000457StringRef MachineFunction::getName() const {
Matthias Braund3181392017-12-15 22:22:58 +0000458 return getFunction().getName();
Craig Topper96601ca2012-08-22 06:07:19 +0000459}
460
Matthias Braund170aea2016-05-05 18:14:43 +0000461void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
David Blaikie986d76d2012-08-22 17:18:53 +0000462 OS << "# Machine code for function " << getName() << ": ";
Derek Schuffd239bc52016-03-29 20:28:20 +0000463 getProperties().print(OS);
Matthias Braun2b727382016-09-29 01:47:42 +0000464 OS << '\n';
Chris Lattner955fad12002-12-28 20:37:16 +0000465
466 // Print Frame Information
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000467 FrameInfo->print(*this, OS);
Chad Rosier0c1eac82012-06-19 23:37:57 +0000468
Nate Begeman37efe672006-04-22 18:53:45 +0000469 // Print JumpTable Information
Chris Lattner071c62f2010-01-25 23:26:13 +0000470 if (JumpTableInfo)
471 JumpTableInfo->print(OS);
Chris Lattner4d149cd2003-01-13 00:23:03 +0000472
473 // Print Constant Pool
Chris Lattnerd74c5562009-08-23 01:12:47 +0000474 ConstantPool->print(OS);
Chad Rosier0c1eac82012-06-19 23:37:57 +0000475
Eric Christopher60355182014-08-05 02:39:49 +0000476 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
Chad Rosier0c1eac82012-06-19 23:37:57 +0000477
Matthijs Kooijmane2b997b2008-10-13 12:37:16 +0000478 if (RegInfo && !RegInfo->livein_empty()) {
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000479 OS << "Function Live Ins: ";
Chris Lattner84bc5422007-12-31 04:13:23 +0000480 for (MachineRegisterInfo::livein_iterator
481 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +0000482 OS << printReg(I->first, TRI);
Chris Lattner4e920272006-05-16 05:55:30 +0000483 if (I->second)
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +0000484 OS << " in " << printReg(I->second, TRI);
Benjamin Kramerd628f192014-03-02 12:27:27 +0000485 if (std::next(I) != E)
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000486 OS << ", ";
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000487 }
Chris Lattnerd74c5562009-08-23 01:12:47 +0000488 OS << '\n';
Chris Lattnera1f68ca2005-08-31 22:34:59 +0000489 }
Chad Rosier0c1eac82012-06-19 23:37:57 +0000490
Matthias Braund3181392017-12-15 22:22:58 +0000491 ModuleSlotTracker MST(getFunction().getParent());
492 MST.incorporateFunction(getFunction());
Alexey Samsonov4aef7272014-04-30 18:29:51 +0000493 for (const auto &BB : *this) {
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000494 OS << '\n';
Francis Visoiu Mistrih454e6dc2018-02-26 15:23:42 +0000495 // If we print the whole function, print it at its most verbose level.
496 BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000497 }
Brian Gaeke47b71642004-03-29 21:58:31 +0000498
David Blaikie986d76d2012-08-22 17:18:53 +0000499 OS << "\n# End machine code for function " << getName() << ".\n\n";
Chris Lattner10491642002-10-30 00:48:05 +0000500}
501
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000502namespace llvm {
Eugene Zelenko887aef72017-10-10 22:33:29 +0000503
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000504 template<>
505 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
Eugene Zelenko887aef72017-10-10 22:33:29 +0000506 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
Tobias Grossera10d5982009-11-30 12:38:13 +0000507
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000508 static std::string getGraphName(const MachineFunction *F) {
Yaron Keren2a9bbcb2015-03-27 17:51:30 +0000509 return ("CFG for '" + F->getName() + "' function").str();
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000510 }
511
Tobias Grosser56f4ef32009-11-30 12:38:47 +0000512 std::string getNodeLabel(const MachineBasicBlock *Node,
513 const MachineFunction *Graph) {
Chris Lattnercf143a42009-08-23 03:13:20 +0000514 std::string OutStr;
515 {
516 raw_string_ostream OSS(OutStr);
Jakob Stoklund Olesene5f4e9f2010-10-30 01:26:19 +0000517
518 if (isSimple()) {
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +0000519 OSS << printMBBReference(*Node);
Jakob Stoklund Olesene5f4e9f2010-10-30 01:26:19 +0000520 if (const BasicBlock *BB = Node->getBasicBlock())
521 OSS << ": " << BB->getName();
522 } else
Chris Lattnercf143a42009-08-23 03:13:20 +0000523 Node->print(OSS);
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000524 }
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000525
Alkis Evlogimenos76d9dac2004-09-05 18:41:35 +0000526 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
527
528 // Process string output to make it nicer...
529 for (unsigned i = 0; i != OutStr.length(); ++i)
530 if (OutStr[i] == '\n') { // Left justify
531 OutStr[i] = '\\';
532 OutStr.insert(OutStr.begin()+i+1, 'l');
533 }
534 return OutStr;
535 }
536 };
Eugene Zelenko887aef72017-10-10 22:33:29 +0000537
538} // end namespace llvm
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000539
540void MachineFunction::viewCFG() const
541{
Jim Laskey851a22d2005-10-12 12:09:05 +0000542#ifndef NDEBUG
David Blaikie986d76d2012-08-22 17:18:53 +0000543 ViewGraph(this, "mf" + getName());
Reid Spencer9d5b5322006-06-27 16:49:46 +0000544#else
Dan Gohman643fffe2010-07-07 17:28:45 +0000545 errs() << "MachineFunction::viewCFG is only available in debug builds on "
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000546 << "systems with Graphviz or gv!\n";
Reid Spencer9d5b5322006-06-27 16:49:46 +0000547#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000548}
549
550void MachineFunction::viewCFGOnly() const
551{
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000552#ifndef NDEBUG
David Blaikie986d76d2012-08-22 17:18:53 +0000553 ViewGraph(this, "mf" + getName(), true);
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000554#else
Dan Gohman643fffe2010-07-07 17:28:45 +0000555 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
Daniel Dunbar43ed2672009-08-23 08:50:52 +0000556 << "systems with Graphviz or gv!\n";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000557#endif // NDEBUG
Alkis Evlogimenos71bf4042004-07-08 00:47:58 +0000558}
559
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000560/// Add the specified physical register as a live-in value and
Bob Wilson998e1252009-04-20 18:36:57 +0000561/// create a corresponding virtual register for it.
562unsigned MachineFunction::addLiveIn(unsigned PReg,
Devang Patel68e6bee2011-02-21 23:21:26 +0000563 const TargetRegisterClass *RC) {
Evan Cheng39460432010-05-24 21:33:37 +0000564 MachineRegisterInfo &MRI = getRegInfo();
565 unsigned VReg = MRI.getLiveInVirtReg(PReg);
566 if (VReg) {
Quentin Colombetb0f8afd2013-12-12 00:15:47 +0000567 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
568 (void)VRegRC;
569 // A physical register can be added several times.
570 // Between two calls, the register class of the related virtual register
571 // may have been constrained to match some operation constraints.
572 // In that case, check that the current register class includes the
573 // physical register and is a sub class of the specified RC.
574 assert((VRegRC == RC || (VRegRC->contains(PReg) &&
575 RC->hasSubClassEq(VRegRC))) &&
576 "Register class mismatch!");
Evan Cheng39460432010-05-24 21:33:37 +0000577 return VReg;
578 }
579 VReg = MRI.createVirtualRegister(RC);
580 MRI.addLiveIn(PReg, VReg);
Bob Wilson998e1252009-04-20 18:36:57 +0000581 return VReg;
582}
583
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000584/// Return the MCSymbol for the specified non-empty jump table.
Bill Wendling07d31772010-06-29 22:34:52 +0000585/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
586/// normal 'L' label is returned.
NAKAMURA Takumi2fc4d992014-06-25 12:40:56 +0000587MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
Bill Wendling07d31772010-06-29 22:34:52 +0000588 bool isLinkerPrivate) const {
Mehdi Amini33c4d682015-07-07 18:20:57 +0000589 const DataLayout &DL = getDataLayout();
Chris Lattner589c6f62010-01-26 06:28:43 +0000590 assert(JumpTableInfo && "No jump tables");
Chandler Carruthde4c0802010-01-27 10:27:10 +0000591 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
Chad Rosier0c1eac82012-06-19 23:37:57 +0000592
Mehdi Amini0efcb402016-10-01 05:57:55 +0000593 StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
594 : DL.getPrivateGlobalPrefix();
Alp Toker8dd8d5c2014-06-26 22:52:05 +0000595 SmallString<60> Name;
596 raw_svector_ostream(Name)
597 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
Jim Grosbach19696da2015-05-18 18:43:14 +0000598 return Ctx.getOrCreateSymbol(Name);
Chris Lattner589c6f62010-01-26 06:28:43 +0000599}
600
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000601/// Return a function-local symbol to represent the PIC base.
Chris Lattner142b5312010-11-14 22:48:15 +0000602MCSymbol *MachineFunction::getPICBaseSymbol() const {
Mehdi Amini33c4d682015-07-07 18:20:57 +0000603 const DataLayout &DL = getDataLayout();
604 return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
605 Twine(getFunctionNumber()) + "$pb");
Chris Lattner142b5312010-11-14 22:48:15 +0000606}
Chris Lattner589c6f62010-01-26 06:28:43 +0000607
Matthias Braun347847b2016-12-01 19:32:15 +0000608/// \name Exception Handling
609/// \{
610
611LandingPadInfo &
612MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
613 unsigned N = LandingPads.size();
614 for (unsigned i = 0; i < N; ++i) {
615 LandingPadInfo &LP = LandingPads[i];
616 if (LP.LandingPadBlock == LandingPad)
617 return LP;
618 }
619
620 LandingPads.push_back(LandingPadInfo(LandingPad));
621 return LandingPads[N];
622}
623
624void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
625 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
626 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
627 LP.BeginLabels.push_back(BeginLabel);
628 LP.EndLabels.push_back(EndLabel);
629}
630
631MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
632 MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
633 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
634 LP.LandingPadLabel = LandingPadLabel;
Heejin Ahnf384f742018-09-25 19:56:44 +0000635
636 const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
637 if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
638 if (const auto *PF =
639 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
640 getMMI().addPersonality(PF);
641
642 if (LPI->isCleanup())
643 addCleanup(LandingPad);
644
645 // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
Heejin Ahncde69b22018-09-29 09:22:25 +0000646 // correct, but we need to do it this way because of how the DWARF EH
647 // emitter processes the clauses.
Heejin Ahnf384f742018-09-25 19:56:44 +0000648 for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
649 Value *Val = LPI->getClause(I - 1);
650 if (LPI->isCatch(I - 1)) {
651 addCatchTypeInfo(LandingPad,
652 dyn_cast<GlobalValue>(Val->stripPointerCasts()));
653 } else {
654 // Add filters in a list.
655 auto *CVal = cast<Constant>(Val);
656 SmallVector<const GlobalValue *, 4> FilterList;
657 for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
658 II != IE; ++II)
659 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
660
661 addFilterTypeInfo(LandingPad, FilterList);
662 }
663 }
664
Heejin Ahn397841e2018-10-25 23:55:10 +0000665 } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
666 for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) {
667 Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
668 addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
669 }
Heejin Ahnf384f742018-09-25 19:56:44 +0000670
671 } else {
672 assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
673 }
674
Matthias Braun347847b2016-12-01 19:32:15 +0000675 return LandingPadLabel;
676}
677
678void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
679 ArrayRef<const GlobalValue *> TyInfo) {
680 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
681 for (unsigned N = TyInfo.size(); N; --N)
682 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
683}
684
685void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
686 ArrayRef<const GlobalValue *> TyInfo) {
687 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
688 std::vector<unsigned> IdsInFilter(TyInfo.size());
689 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
690 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
691 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
692}
693
Heejin Ahn397841e2018-10-25 23:55:10 +0000694void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
695 bool TidyIfNoBeginLabels) {
Matthias Braun347847b2016-12-01 19:32:15 +0000696 for (unsigned i = 0; i != LandingPads.size(); ) {
697 LandingPadInfo &LandingPad = LandingPads[i];
698 if (LandingPad.LandingPadLabel &&
699 !LandingPad.LandingPadLabel->isDefined() &&
700 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
701 LandingPad.LandingPadLabel = nullptr;
702
703 // Special case: we *should* emit LPs with null LP MBB. This indicates
704 // "nounwind" case.
705 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
706 LandingPads.erase(LandingPads.begin() + i);
707 continue;
708 }
709
Heejin Ahn397841e2018-10-25 23:55:10 +0000710 if (TidyIfNoBeginLabels) {
711 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
712 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
713 MCSymbol *EndLabel = LandingPad.EndLabels[j];
714 if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
715 (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
716 continue;
Matthias Braun347847b2016-12-01 19:32:15 +0000717
Heejin Ahn397841e2018-10-25 23:55:10 +0000718 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
719 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
720 --j;
721 --e;
722 }
Matthias Braun347847b2016-12-01 19:32:15 +0000723
Heejin Ahn397841e2018-10-25 23:55:10 +0000724 // Remove landing pads with no try-ranges.
725 if (LandingPads[i].BeginLabels.empty()) {
726 LandingPads.erase(LandingPads.begin() + i);
727 continue;
728 }
Matthias Braun347847b2016-12-01 19:32:15 +0000729 }
730
731 // If there is no landing pad, ensure that the list of typeids is empty.
732 // If the only typeid is a cleanup, this is the same as having no typeids.
733 if (!LandingPad.LandingPadBlock ||
734 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
735 LandingPad.TypeIds.clear();
736 ++i;
737 }
738}
739
740void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
741 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
742 LP.TypeIds.push_back(0);
743}
744
745void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
746 const Function *Filter,
747 const BlockAddress *RecoverBA) {
748 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
749 SEHHandler Handler;
750 Handler.FilterOrFinally = Filter;
751 Handler.RecoverBA = RecoverBA;
752 LP.SEHHandlers.push_back(Handler);
753}
754
755void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
756 const Function *Cleanup) {
757 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
758 SEHHandler Handler;
759 Handler.FilterOrFinally = Cleanup;
760 Handler.RecoverBA = nullptr;
761 LP.SEHHandlers.push_back(Handler);
762}
763
764void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
765 ArrayRef<unsigned> Sites) {
766 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
767}
768
769unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
770 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
771 if (TypeInfos[i] == TI) return i + 1;
772
773 TypeInfos.push_back(TI);
774 return TypeInfos.size();
775}
776
777int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
778 // If the new filter coincides with the tail of an existing filter, then
779 // re-use the existing filter. Folding filters more than this requires
780 // re-ordering filters and/or their elements - probably not worth it.
781 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
782 E = FilterEnds.end(); I != E; ++I) {
783 unsigned i = *I, j = TyIds.size();
784
785 while (i && j)
786 if (FilterIds[--i] != TyIds[--j])
787 goto try_next;
788
789 if (!j)
790 // The new filter coincides with range [i, end) of the existing filter.
791 return -(1 + i);
792
793try_next:;
794 }
795
796 // Add the new filter.
797 int FilterID = -(1 + FilterIds.size());
798 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
799 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
800 FilterEnds.push_back(FilterIds.size());
801 FilterIds.push_back(0); // terminator
802 return FilterID;
803}
804
Matthias Braun347847b2016-12-01 19:32:15 +0000805/// \}
806
Chris Lattner955fad12002-12-28 20:37:16 +0000807//===----------------------------------------------------------------------===//
Nate Begeman37efe672006-04-22 18:53:45 +0000808// MachineJumpTableInfo implementation
809//===----------------------------------------------------------------------===//
810
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000811/// Return the size of each entry in the jump table.
Micah Villmow3574eca2012-10-08 16:38:25 +0000812unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
Chris Lattner071c62f2010-01-25 23:26:13 +0000813 // The size of a jump table entry is 4 bytes unless the entry is just the
814 // address of a block, in which case it is the pointer size.
815 switch (getEntryKind()) {
816 case MachineJumpTableInfo::EK_BlockAddress:
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000817 return TD.getPointerSize();
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +0000818 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
819 return 8;
Chris Lattner071c62f2010-01-25 23:26:13 +0000820 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
821 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000822 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000823 return 4;
Richard Osborne95da6052010-03-11 14:58:16 +0000824 case MachineJumpTableInfo::EK_Inline:
825 return 0;
Chris Lattner071c62f2010-01-25 23:26:13 +0000826 }
Craig Topperaae875c2012-02-06 08:17:43 +0000827 llvm_unreachable("Unknown jump table encoding!");
Chris Lattner071c62f2010-01-25 23:26:13 +0000828}
829
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000830/// Return the alignment of each entry in the jump table.
Micah Villmow3574eca2012-10-08 16:38:25 +0000831unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
Chris Lattner071c62f2010-01-25 23:26:13 +0000832 // The alignment of a jump table entry is the alignment of int32 unless the
833 // entry is just the address of a block, in which case it is the pointer
834 // alignment.
835 switch (getEntryKind()) {
836 case MachineJumpTableInfo::EK_BlockAddress:
Fangrui Song0b5d88f2017-11-15 06:17:32 +0000837 return TD.getPointerABIAlignment(0);
Akira Hatanaka6c2cf8b2012-02-03 04:33:00 +0000838 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
839 return TD.getABIIntegerTypeAlignment(64);
Chris Lattner071c62f2010-01-25 23:26:13 +0000840 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
841 case MachineJumpTableInfo::EK_LabelDifference32:
Chris Lattner85fe0782010-01-26 04:05:28 +0000842 case MachineJumpTableInfo::EK_Custom32:
Chris Lattner071c62f2010-01-25 23:26:13 +0000843 return TD.getABIIntegerTypeAlignment(32);
Richard Osborne95da6052010-03-11 14:58:16 +0000844 case MachineJumpTableInfo::EK_Inline:
845 return 1;
Chris Lattner071c62f2010-01-25 23:26:13 +0000846 }
Craig Topperaae875c2012-02-06 08:17:43 +0000847 llvm_unreachable("Unknown jump table encoding!");
Chris Lattner071c62f2010-01-25 23:26:13 +0000848}
849
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000850/// Create a new jump table entry in the jump table info.
Bob Wilsond1ec31d2010-03-18 18:42:41 +0000851unsigned MachineJumpTableInfo::createJumpTableIndex(
Chris Lattnera4eb44a2006-10-28 18:17:09 +0000852 const std::vector<MachineBasicBlock*> &DestBBs) {
Chris Lattnere7251a02006-10-28 18:11:20 +0000853 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
Nate Begeman37efe672006-04-22 18:53:45 +0000854 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
855 return JumpTables.size()-1;
856}
857
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000858/// If Old is the target of any jump tables, update the jump tables to branch
859/// to New instead.
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000860bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
861 MachineBasicBlock *New) {
Dan Gohman593ea052009-04-15 01:18:49 +0000862 assert(Old != New && "Not making a change?");
863 bool MadeChange = false;
Jim Grosbach68bb60f2009-11-14 20:09:13 +0000864 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
865 ReplaceMBBInJumpTable(i, Old, New);
866 return MadeChange;
867}
868
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000869/// If Old is a target of the jump tables, update the jump table to branch to
870/// New instead.
Chris Lattnerbeeb93e2010-01-26 05:58:28 +0000871bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
872 MachineBasicBlock *Old,
873 MachineBasicBlock *New) {
Jim Grosbach68bb60f2009-11-14 20:09:13 +0000874 assert(Old != New && "Not making a change?");
875 bool MadeChange = false;
876 MachineJumpTableEntry &JTE = JumpTables[Idx];
877 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
878 if (JTE.MBBs[j] == Old) {
879 JTE.MBBs[j] = New;
880 MadeChange = true;
881 }
Dan Gohman593ea052009-04-15 01:18:49 +0000882 return MadeChange;
883}
Nate Begeman37efe672006-04-22 18:53:45 +0000884
Chris Lattnerd74c5562009-08-23 01:12:47 +0000885void MachineJumpTableInfo::print(raw_ostream &OS) const {
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000886 if (JumpTables.empty()) return;
887
888 OS << "Jump Tables:\n";
889
Nate Begeman37efe672006-04-22 18:53:45 +0000890 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
Francis Visoiu Mistrihd347e972017-12-13 10:30:59 +0000891 OS << printJumpTableEntryReference(i) << ": ";
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000892 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
Francis Visoiu Mistrihca0df552017-12-04 17:18:51 +0000893 OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
Nate Begeman37efe672006-04-22 18:53:45 +0000894 }
Dan Gohman0ba90f3e2009-10-31 20:19:03 +0000895
896 OS << '\n';
Nate Begeman37efe672006-04-22 18:53:45 +0000897}
898
Aaron Ballman1d03d382017-10-15 14:32:27 +0000899#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000900LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
Manman Ren77e300e2012-09-06 19:06:06 +0000901#endif
Nate Begeman37efe672006-04-22 18:53:45 +0000902
Francis Visoiu Mistrihd347e972017-12-13 10:30:59 +0000903Printable llvm::printJumpTableEntryReference(unsigned Idx) {
904 return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
905}
906
Nate Begeman37efe672006-04-22 18:53:45 +0000907//===----------------------------------------------------------------------===//
Chris Lattner4d149cd2003-01-13 00:23:03 +0000908// MachineConstantPool implementation
909//===----------------------------------------------------------------------===//
910
Eugene Zelenko887aef72017-10-10 22:33:29 +0000911void MachineConstantPoolValue::anchor() {}
David Blaikie2d24e2a2011-12-20 02:50:00 +0000912
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000913Type *MachineConstantPoolEntry::getType() const {
Evan Cheng9abd7c32006-09-14 05:50:57 +0000914 if (isMachineConstantPoolEntry())
Chris Lattnercb459632009-07-21 23:34:23 +0000915 return Val.MachineCPVal->getType();
Evan Cheng9abd7c32006-09-14 05:50:57 +0000916 return Val.ConstVal->getType();
917}
918
Rafael Espindola8dcaa9f2015-11-17 00:51:23 +0000919bool MachineConstantPoolEntry::needsRelocation() const {
Chris Lattnercb459632009-07-21 23:34:23 +0000920 if (isMachineConstantPoolEntry())
Rafael Espindola8dcaa9f2015-11-17 00:51:23 +0000921 return true;
922 return Val.ConstVal->needsRelocation();
Chris Lattnercb459632009-07-21 23:34:23 +0000923}
924
David Majnemera539d7b2014-07-14 22:06:29 +0000925SectionKind
926MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
Rafael Espindola8dcaa9f2015-11-17 00:51:23 +0000927 if (needsRelocation())
928 return SectionKind::getReadOnlyWithRel();
929 switch (DL->getTypeAllocSize(getType())) {
930 case 4:
931 return SectionKind::getMergeableConst4();
932 case 8:
933 return SectionKind::getMergeableConst8();
934 case 16:
935 return SectionKind::getMergeableConst16();
David Majnemerb23b0ff2016-02-22 22:23:11 +0000936 case 32:
937 return SectionKind::getMergeableConst32();
David Majnemera539d7b2014-07-14 22:06:29 +0000938 default:
Rafael Espindola8dcaa9f2015-11-17 00:51:23 +0000939 return SectionKind::getReadOnly();
David Majnemera539d7b2014-07-14 22:06:29 +0000940 }
David Majnemera539d7b2014-07-14 22:06:29 +0000941}
942
Evan Chengd6594ae2006-09-12 21:00:35 +0000943MachineConstantPool::~MachineConstantPool() {
James Molloyba54dc22016-09-26 07:26:24 +0000944 // A constant may be a member of both Constants and MachineCPVsSharingEntries,
945 // so keep track of which we've deleted to avoid double deletions.
946 DenseSet<MachineConstantPoolValue*> Deleted;
Evan Chengd6594ae2006-09-12 21:00:35 +0000947 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
James Molloyba54dc22016-09-26 07:26:24 +0000948 if (Constants[i].isMachineConstantPoolEntry()) {
949 Deleted.insert(Constants[i].Val.MachineCPVal);
Evan Chengd6594ae2006-09-12 21:00:35 +0000950 delete Constants[i].Val.MachineCPVal;
James Molloyba54dc22016-09-26 07:26:24 +0000951 }
Cameron Zwarich55678692011-02-22 08:54:30 +0000952 for (DenseSet<MachineConstantPoolValue*>::iterator I =
953 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
James Molloyba54dc22016-09-26 07:26:24 +0000954 I != E; ++I) {
955 if (Deleted.count(*I) == 0)
956 delete *I;
957 }
Evan Chengd6594ae2006-09-12 21:00:35 +0000958}
959
Sanjay Patelea58c7d2015-06-13 15:32:45 +0000960/// Test whether the given two constants can be allocated the same constant pool
961/// entry.
Dan Gohman46510a72010-04-15 01:51:59 +0000962static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
Mehdi Amini33c4d682015-07-07 18:20:57 +0000963 const DataLayout &DL) {
Dan Gohman83f61202009-10-28 01:12:16 +0000964 // Handle the trivial case quickly.
965 if (A == B) return true;
966
967 // If they have the same type but weren't the same constant, quickly
968 // reject them.
969 if (A->getType() == B->getType()) return false;
970
Chris Lattner73858d12012-01-27 01:46:00 +0000971 // We can't handle structs or arrays.
972 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
973 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
974 return false;
NAKAMURA Takumi2fc4d992014-06-25 12:40:56 +0000975
Dan Gohman83f61202009-10-28 01:12:16 +0000976 // For now, only support constants with the same size.
Mehdi Amini33c4d682015-07-07 18:20:57 +0000977 uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
978 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
Dan Gohman83f61202009-10-28 01:12:16 +0000979 return false;
980
Chris Lattner73858d12012-01-27 01:46:00 +0000981 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
Dan Gohman83f61202009-10-28 01:12:16 +0000982
Chris Lattner73858d12012-01-27 01:46:00 +0000983 // Try constant folding a bitcast of both instructions to an integer. If we
984 // get two identical ConstantInt's, then we are good to share them. We use
985 // the constant folding APIs to do this so that we get the benefit of
Micah Villmow3574eca2012-10-08 16:38:25 +0000986 // DataLayout.
Chris Lattner73858d12012-01-27 01:46:00 +0000987 if (isa<PointerType>(A->getType()))
Manuel Jacob9b455a82016-01-21 06:31:08 +0000988 A = ConstantFoldCastOperand(Instruction::PtrToInt,
989 const_cast<Constant *>(A), IntTy, DL);
Chris Lattner73858d12012-01-27 01:46:00 +0000990 else if (A->getType() != IntTy)
Manuel Jacob9b455a82016-01-21 06:31:08 +0000991 A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
992 IntTy, DL);
Chris Lattner73858d12012-01-27 01:46:00 +0000993 if (isa<PointerType>(B->getType()))
Manuel Jacob9b455a82016-01-21 06:31:08 +0000994 B = ConstantFoldCastOperand(Instruction::PtrToInt,
995 const_cast<Constant *>(B), IntTy, DL);
Chris Lattner73858d12012-01-27 01:46:00 +0000996 else if (B->getType() != IntTy)
Manuel Jacob9b455a82016-01-21 06:31:08 +0000997 B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
998 IntTy, DL);
Chad Rosier0c1eac82012-06-19 23:37:57 +0000999
Chris Lattner73858d12012-01-27 01:46:00 +00001000 return A == B;
Dan Gohman83f61202009-10-28 01:12:16 +00001001}
1002
Sanjay Patelea58c7d2015-06-13 15:32:45 +00001003/// Create a new entry in the constant pool or return an existing one.
1004/// User must specify the log2 of the minimum required alignment for the object.
NAKAMURA Takumi2fc4d992014-06-25 12:40:56 +00001005unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
Chris Lattner3029f922006-02-09 04:46:04 +00001006 unsigned Alignment) {
1007 assert(Alignment && "Alignment must be specified!");
1008 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Dan Gohman83f61202009-10-28 01:12:16 +00001009
Chris Lattner3029f922006-02-09 04:46:04 +00001010 // Check to see if we already have this constant.
1011 //
1012 // FIXME, this could be made much more efficient for large constant pools.
Chris Lattner3029f922006-02-09 04:46:04 +00001013 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
Dan Gohman83f61202009-10-28 01:12:16 +00001014 if (!Constants[i].isMachineConstantPoolEntry() &&
Mehdi Amini33c4d682015-07-07 18:20:57 +00001015 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
Dan Gohman83f61202009-10-28 01:12:16 +00001016 if ((unsigned)Constants[i].getAlignment() < Alignment)
1017 Constants[i].Alignment = Alignment;
Chris Lattner3029f922006-02-09 04:46:04 +00001018 return i;
Dan Gohman83f61202009-10-28 01:12:16 +00001019 }
Chad Rosier0c1eac82012-06-19 23:37:57 +00001020
Evan Cheng1606e8e2009-03-13 07:51:59 +00001021 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
Chris Lattner3029f922006-02-09 04:46:04 +00001022 return Constants.size()-1;
1023}
1024
Evan Chengd6594ae2006-09-12 21:00:35 +00001025unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1026 unsigned Alignment) {
1027 assert(Alignment && "Alignment must be specified!");
1028 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
Chad Rosier0c1eac82012-06-19 23:37:57 +00001029
Evan Chengd6594ae2006-09-12 21:00:35 +00001030 // Check to see if we already have this constant.
1031 //
1032 // FIXME, this could be made much more efficient for large constant pools.
Evan Chengd6594ae2006-09-12 21:00:35 +00001033 int Idx = V->getExistingMachineCPValue(this, Alignment);
Cameron Zwarich55678692011-02-22 08:54:30 +00001034 if (Idx != -1) {
1035 MachineCPVsSharingEntries.insert(V);
Evan Chengd6594ae2006-09-12 21:00:35 +00001036 return (unsigned)Idx;
Cameron Zwarich55678692011-02-22 08:54:30 +00001037 }
Evan Cheng1606e8e2009-03-13 07:51:59 +00001038
1039 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
Evan Chengd6594ae2006-09-12 21:00:35 +00001040 return Constants.size()-1;
1041}
1042
Chris Lattner62ca3252008-08-23 22:53:13 +00001043void MachineConstantPool::print(raw_ostream &OS) const {
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001044 if (Constants.empty()) return;
1045
1046 OS << "Constant Pool:\n";
Evan Chengb8973bd2006-01-31 22:23:14 +00001047 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001048 OS << " cp#" << i << ": ";
Evan Chengd6594ae2006-09-12 21:00:35 +00001049 if (Constants[i].isMachineConstantPoolEntry())
1050 Constants[i].Val.MachineCPVal->print(OS);
1051 else
Chandler Carruth560e3952014-01-09 02:29:41 +00001052 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
Dan Gohman0ba90f3e2009-10-31 20:19:03 +00001053 OS << ", align=" << Constants[i].getAlignment();
Evan Chengb8973bd2006-01-31 22:23:14 +00001054 OS << "\n";
1055 }
Chris Lattner4d149cd2003-01-13 00:23:03 +00001056}
1057
Aaron Ballman1d03d382017-10-15 14:32:27 +00001058#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +00001059LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
Manman Ren77e300e2012-09-06 19:06:06 +00001060#endif