xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MachineFunction.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===- MachineFunction.cpp ------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // Collect native machine code information for a function.  This allows
10*0b57cec5SDimitry Andric // target-specific information about the generated code to be stored with each
11*0b57cec5SDimitry Andric // function.
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
17*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
18*0b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
19*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
20*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
21*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
22*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
23*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
24*0b57cec5SDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
25*0b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h"
26*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h"
28*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
29*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
30*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
31*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
32*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h"
33*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
34*0b57cec5SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h"
35*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
36*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
37*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
38*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
39*0b57cec5SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h"
40*0b57cec5SDimitry Andric #include "llvm/CodeGen/WinEHFuncInfo.h"
41*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
42*0b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
43*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
44*0b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
45*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
46*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
47*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
48*0b57cec5SDimitry Andric #include "llvm/IR/Function.h"
49*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
50*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
51*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
52*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
53*0b57cec5SDimitry Andric #include "llvm/IR/Module.h"
54*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h"
55*0b57cec5SDimitry Andric #include "llvm/IR/Value.h"
56*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
57*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
58*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h"
59*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
60*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
61*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
62*0b57cec5SDimitry Andric #include "llvm/Support/DOTGraphTraits.h"
63*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
64*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
65*0b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h"
66*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
67*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
68*0b57cec5SDimitry Andric #include <algorithm>
69*0b57cec5SDimitry Andric #include <cassert>
70*0b57cec5SDimitry Andric #include <cstddef>
71*0b57cec5SDimitry Andric #include <cstdint>
72*0b57cec5SDimitry Andric #include <iterator>
73*0b57cec5SDimitry Andric #include <string>
74*0b57cec5SDimitry Andric #include <utility>
75*0b57cec5SDimitry Andric #include <vector>
76*0b57cec5SDimitry Andric 
77*0b57cec5SDimitry Andric using namespace llvm;
78*0b57cec5SDimitry Andric 
79*0b57cec5SDimitry Andric #define DEBUG_TYPE "codegen"
80*0b57cec5SDimitry Andric 
81*0b57cec5SDimitry Andric static cl::opt<unsigned>
82*0b57cec5SDimitry Andric AlignAllFunctions("align-all-functions",
83*0b57cec5SDimitry Andric                   cl::desc("Force the alignment of all functions."),
84*0b57cec5SDimitry Andric                   cl::init(0), cl::Hidden);
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
87*0b57cec5SDimitry Andric   using P = MachineFunctionProperties::Property;
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric   switch(Prop) {
90*0b57cec5SDimitry Andric   case P::FailedISel: return "FailedISel";
91*0b57cec5SDimitry Andric   case P::IsSSA: return "IsSSA";
92*0b57cec5SDimitry Andric   case P::Legalized: return "Legalized";
93*0b57cec5SDimitry Andric   case P::NoPHIs: return "NoPHIs";
94*0b57cec5SDimitry Andric   case P::NoVRegs: return "NoVRegs";
95*0b57cec5SDimitry Andric   case P::RegBankSelected: return "RegBankSelected";
96*0b57cec5SDimitry Andric   case P::Selected: return "Selected";
97*0b57cec5SDimitry Andric   case P::TracksLiveness: return "TracksLiveness";
98*0b57cec5SDimitry Andric   }
99*0b57cec5SDimitry Andric   llvm_unreachable("Invalid machine function property");
100*0b57cec5SDimitry Andric }
101*0b57cec5SDimitry Andric 
102*0b57cec5SDimitry Andric // Pin the vtable to this file.
103*0b57cec5SDimitry Andric void MachineFunction::Delegate::anchor() {}
104*0b57cec5SDimitry Andric 
105*0b57cec5SDimitry Andric void MachineFunctionProperties::print(raw_ostream &OS) const {
106*0b57cec5SDimitry Andric   const char *Separator = "";
107*0b57cec5SDimitry Andric   for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
108*0b57cec5SDimitry Andric     if (!Properties[I])
109*0b57cec5SDimitry Andric       continue;
110*0b57cec5SDimitry Andric     OS << Separator << getPropertyName(static_cast<Property>(I));
111*0b57cec5SDimitry Andric     Separator = ", ";
112*0b57cec5SDimitry Andric   }
113*0b57cec5SDimitry Andric }
114*0b57cec5SDimitry Andric 
115*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
116*0b57cec5SDimitry Andric // MachineFunction implementation
117*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
118*0b57cec5SDimitry Andric 
119*0b57cec5SDimitry Andric // Out-of-line virtual method.
120*0b57cec5SDimitry Andric MachineFunctionInfo::~MachineFunctionInfo() = default;
121*0b57cec5SDimitry Andric 
122*0b57cec5SDimitry Andric void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
123*0b57cec5SDimitry Andric   MBB->getParent()->DeleteMachineBasicBlock(MBB);
124*0b57cec5SDimitry Andric }
125*0b57cec5SDimitry Andric 
126*0b57cec5SDimitry Andric static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
127*0b57cec5SDimitry Andric                                            const Function &F) {
128*0b57cec5SDimitry Andric   if (F.hasFnAttribute(Attribute::StackAlignment))
129*0b57cec5SDimitry Andric     return F.getFnStackAlignment();
130*0b57cec5SDimitry Andric   return STI->getFrameLowering()->getStackAlignment();
131*0b57cec5SDimitry Andric }
132*0b57cec5SDimitry Andric 
133*0b57cec5SDimitry Andric MachineFunction::MachineFunction(const Function &F,
134*0b57cec5SDimitry Andric                                  const LLVMTargetMachine &Target,
135*0b57cec5SDimitry Andric                                  const TargetSubtargetInfo &STI,
136*0b57cec5SDimitry Andric                                  unsigned FunctionNum, MachineModuleInfo &mmi)
137*0b57cec5SDimitry Andric     : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
138*0b57cec5SDimitry Andric   FunctionNumber = FunctionNum;
139*0b57cec5SDimitry Andric   init();
140*0b57cec5SDimitry Andric }
141*0b57cec5SDimitry Andric 
142*0b57cec5SDimitry Andric void MachineFunction::handleInsertion(MachineInstr &MI) {
143*0b57cec5SDimitry Andric   if (TheDelegate)
144*0b57cec5SDimitry Andric     TheDelegate->MF_HandleInsertion(MI);
145*0b57cec5SDimitry Andric }
146*0b57cec5SDimitry Andric 
147*0b57cec5SDimitry Andric void MachineFunction::handleRemoval(MachineInstr &MI) {
148*0b57cec5SDimitry Andric   if (TheDelegate)
149*0b57cec5SDimitry Andric     TheDelegate->MF_HandleRemoval(MI);
150*0b57cec5SDimitry Andric }
151*0b57cec5SDimitry Andric 
152*0b57cec5SDimitry Andric void MachineFunction::init() {
153*0b57cec5SDimitry Andric   // Assume the function starts in SSA form with correct liveness.
154*0b57cec5SDimitry Andric   Properties.set(MachineFunctionProperties::Property::IsSSA);
155*0b57cec5SDimitry Andric   Properties.set(MachineFunctionProperties::Property::TracksLiveness);
156*0b57cec5SDimitry Andric   if (STI->getRegisterInfo())
157*0b57cec5SDimitry Andric     RegInfo = new (Allocator) MachineRegisterInfo(this);
158*0b57cec5SDimitry Andric   else
159*0b57cec5SDimitry Andric     RegInfo = nullptr;
160*0b57cec5SDimitry Andric 
161*0b57cec5SDimitry Andric   MFInfo = nullptr;
162*0b57cec5SDimitry Andric   // We can realign the stack if the target supports it and the user hasn't
163*0b57cec5SDimitry Andric   // explicitly asked us not to.
164*0b57cec5SDimitry Andric   bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
165*0b57cec5SDimitry Andric                       !F.hasFnAttribute("no-realign-stack");
166*0b57cec5SDimitry Andric   FrameInfo = new (Allocator) MachineFrameInfo(
167*0b57cec5SDimitry Andric       getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
168*0b57cec5SDimitry Andric       /*ForcedRealign=*/CanRealignSP &&
169*0b57cec5SDimitry Andric           F.hasFnAttribute(Attribute::StackAlignment));
170*0b57cec5SDimitry Andric 
171*0b57cec5SDimitry Andric   if (F.hasFnAttribute(Attribute::StackAlignment))
172*0b57cec5SDimitry Andric     FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
173*0b57cec5SDimitry Andric 
174*0b57cec5SDimitry Andric   ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
175*0b57cec5SDimitry Andric   Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
176*0b57cec5SDimitry Andric 
177*0b57cec5SDimitry Andric   // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
178*0b57cec5SDimitry Andric   // FIXME: Use Function::hasOptSize().
179*0b57cec5SDimitry Andric   if (!F.hasFnAttribute(Attribute::OptimizeForSize))
180*0b57cec5SDimitry Andric     Alignment = std::max(Alignment,
181*0b57cec5SDimitry Andric                          STI->getTargetLowering()->getPrefFunctionAlignment());
182*0b57cec5SDimitry Andric 
183*0b57cec5SDimitry Andric   if (AlignAllFunctions)
184*0b57cec5SDimitry Andric     Alignment = AlignAllFunctions;
185*0b57cec5SDimitry Andric 
186*0b57cec5SDimitry Andric   JumpTableInfo = nullptr;
187*0b57cec5SDimitry Andric 
188*0b57cec5SDimitry Andric   if (isFuncletEHPersonality(classifyEHPersonality(
189*0b57cec5SDimitry Andric           F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
190*0b57cec5SDimitry Andric     WinEHInfo = new (Allocator) WinEHFuncInfo();
191*0b57cec5SDimitry Andric   }
192*0b57cec5SDimitry Andric 
193*0b57cec5SDimitry Andric   if (isScopedEHPersonality(classifyEHPersonality(
194*0b57cec5SDimitry Andric           F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
195*0b57cec5SDimitry Andric     WasmEHInfo = new (Allocator) WasmEHFuncInfo();
196*0b57cec5SDimitry Andric   }
197*0b57cec5SDimitry Andric 
198*0b57cec5SDimitry Andric   assert(Target.isCompatibleDataLayout(getDataLayout()) &&
199*0b57cec5SDimitry Andric          "Can't create a MachineFunction using a Module with a "
200*0b57cec5SDimitry Andric          "Target-incompatible DataLayout attached\n");
201*0b57cec5SDimitry Andric 
202*0b57cec5SDimitry Andric   PSVManager =
203*0b57cec5SDimitry Andric     llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
204*0b57cec5SDimitry Andric                                                   getInstrInfo()));
205*0b57cec5SDimitry Andric }
206*0b57cec5SDimitry Andric 
207*0b57cec5SDimitry Andric MachineFunction::~MachineFunction() {
208*0b57cec5SDimitry Andric   clear();
209*0b57cec5SDimitry Andric }
210*0b57cec5SDimitry Andric 
211*0b57cec5SDimitry Andric void MachineFunction::clear() {
212*0b57cec5SDimitry Andric   Properties.reset();
213*0b57cec5SDimitry Andric   // Don't call destructors on MachineInstr and MachineOperand. All of their
214*0b57cec5SDimitry Andric   // memory comes from the BumpPtrAllocator which is about to be purged.
215*0b57cec5SDimitry Andric   //
216*0b57cec5SDimitry Andric   // Do call MachineBasicBlock destructors, it contains std::vectors.
217*0b57cec5SDimitry Andric   for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
218*0b57cec5SDimitry Andric     I->Insts.clearAndLeakNodesUnsafely();
219*0b57cec5SDimitry Andric   MBBNumbering.clear();
220*0b57cec5SDimitry Andric 
221*0b57cec5SDimitry Andric   InstructionRecycler.clear(Allocator);
222*0b57cec5SDimitry Andric   OperandRecycler.clear(Allocator);
223*0b57cec5SDimitry Andric   BasicBlockRecycler.clear(Allocator);
224*0b57cec5SDimitry Andric   CodeViewAnnotations.clear();
225*0b57cec5SDimitry Andric   VariableDbgInfos.clear();
226*0b57cec5SDimitry Andric   if (RegInfo) {
227*0b57cec5SDimitry Andric     RegInfo->~MachineRegisterInfo();
228*0b57cec5SDimitry Andric     Allocator.Deallocate(RegInfo);
229*0b57cec5SDimitry Andric   }
230*0b57cec5SDimitry Andric   if (MFInfo) {
231*0b57cec5SDimitry Andric     MFInfo->~MachineFunctionInfo();
232*0b57cec5SDimitry Andric     Allocator.Deallocate(MFInfo);
233*0b57cec5SDimitry Andric   }
234*0b57cec5SDimitry Andric 
235*0b57cec5SDimitry Andric   FrameInfo->~MachineFrameInfo();
236*0b57cec5SDimitry Andric   Allocator.Deallocate(FrameInfo);
237*0b57cec5SDimitry Andric 
238*0b57cec5SDimitry Andric   ConstantPool->~MachineConstantPool();
239*0b57cec5SDimitry Andric   Allocator.Deallocate(ConstantPool);
240*0b57cec5SDimitry Andric 
241*0b57cec5SDimitry Andric   if (JumpTableInfo) {
242*0b57cec5SDimitry Andric     JumpTableInfo->~MachineJumpTableInfo();
243*0b57cec5SDimitry Andric     Allocator.Deallocate(JumpTableInfo);
244*0b57cec5SDimitry Andric   }
245*0b57cec5SDimitry Andric 
246*0b57cec5SDimitry Andric   if (WinEHInfo) {
247*0b57cec5SDimitry Andric     WinEHInfo->~WinEHFuncInfo();
248*0b57cec5SDimitry Andric     Allocator.Deallocate(WinEHInfo);
249*0b57cec5SDimitry Andric   }
250*0b57cec5SDimitry Andric 
251*0b57cec5SDimitry Andric   if (WasmEHInfo) {
252*0b57cec5SDimitry Andric     WasmEHInfo->~WasmEHFuncInfo();
253*0b57cec5SDimitry Andric     Allocator.Deallocate(WasmEHInfo);
254*0b57cec5SDimitry Andric   }
255*0b57cec5SDimitry Andric }
256*0b57cec5SDimitry Andric 
257*0b57cec5SDimitry Andric const DataLayout &MachineFunction::getDataLayout() const {
258*0b57cec5SDimitry Andric   return F.getParent()->getDataLayout();
259*0b57cec5SDimitry Andric }
260*0b57cec5SDimitry Andric 
261*0b57cec5SDimitry Andric /// Get the JumpTableInfo for this function.
262*0b57cec5SDimitry Andric /// If it does not already exist, allocate one.
263*0b57cec5SDimitry Andric MachineJumpTableInfo *MachineFunction::
264*0b57cec5SDimitry Andric getOrCreateJumpTableInfo(unsigned EntryKind) {
265*0b57cec5SDimitry Andric   if (JumpTableInfo) return JumpTableInfo;
266*0b57cec5SDimitry Andric 
267*0b57cec5SDimitry Andric   JumpTableInfo = new (Allocator)
268*0b57cec5SDimitry Andric     MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
269*0b57cec5SDimitry Andric   return JumpTableInfo;
270*0b57cec5SDimitry Andric }
271*0b57cec5SDimitry Andric 
272*0b57cec5SDimitry Andric /// Should we be emitting segmented stack stuff for the function
273*0b57cec5SDimitry Andric bool MachineFunction::shouldSplitStack() const {
274*0b57cec5SDimitry Andric   return getFunction().hasFnAttribute("split-stack");
275*0b57cec5SDimitry Andric }
276*0b57cec5SDimitry Andric 
277*0b57cec5SDimitry Andric LLVM_NODISCARD unsigned
278*0b57cec5SDimitry Andric MachineFunction::addFrameInst(const MCCFIInstruction &Inst) {
279*0b57cec5SDimitry Andric   FrameInstructions.push_back(Inst);
280*0b57cec5SDimitry Andric   return FrameInstructions.size() - 1;
281*0b57cec5SDimitry Andric }
282*0b57cec5SDimitry Andric 
283*0b57cec5SDimitry Andric /// This discards all of the MachineBasicBlock numbers and recomputes them.
284*0b57cec5SDimitry Andric /// This guarantees that the MBB numbers are sequential, dense, and match the
285*0b57cec5SDimitry Andric /// ordering of the blocks within the function.  If a specific MachineBasicBlock
286*0b57cec5SDimitry Andric /// is specified, only that block and those after it are renumbered.
287*0b57cec5SDimitry Andric void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
288*0b57cec5SDimitry Andric   if (empty()) { MBBNumbering.clear(); return; }
289*0b57cec5SDimitry Andric   MachineFunction::iterator MBBI, E = end();
290*0b57cec5SDimitry Andric   if (MBB == nullptr)
291*0b57cec5SDimitry Andric     MBBI = begin();
292*0b57cec5SDimitry Andric   else
293*0b57cec5SDimitry Andric     MBBI = MBB->getIterator();
294*0b57cec5SDimitry Andric 
295*0b57cec5SDimitry Andric   // Figure out the block number this should have.
296*0b57cec5SDimitry Andric   unsigned BlockNo = 0;
297*0b57cec5SDimitry Andric   if (MBBI != begin())
298*0b57cec5SDimitry Andric     BlockNo = std::prev(MBBI)->getNumber() + 1;
299*0b57cec5SDimitry Andric 
300*0b57cec5SDimitry Andric   for (; MBBI != E; ++MBBI, ++BlockNo) {
301*0b57cec5SDimitry Andric     if (MBBI->getNumber() != (int)BlockNo) {
302*0b57cec5SDimitry Andric       // Remove use of the old number.
303*0b57cec5SDimitry Andric       if (MBBI->getNumber() != -1) {
304*0b57cec5SDimitry Andric         assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
305*0b57cec5SDimitry Andric                "MBB number mismatch!");
306*0b57cec5SDimitry Andric         MBBNumbering[MBBI->getNumber()] = nullptr;
307*0b57cec5SDimitry Andric       }
308*0b57cec5SDimitry Andric 
309*0b57cec5SDimitry Andric       // If BlockNo is already taken, set that block's number to -1.
310*0b57cec5SDimitry Andric       if (MBBNumbering[BlockNo])
311*0b57cec5SDimitry Andric         MBBNumbering[BlockNo]->setNumber(-1);
312*0b57cec5SDimitry Andric 
313*0b57cec5SDimitry Andric       MBBNumbering[BlockNo] = &*MBBI;
314*0b57cec5SDimitry Andric       MBBI->setNumber(BlockNo);
315*0b57cec5SDimitry Andric     }
316*0b57cec5SDimitry Andric   }
317*0b57cec5SDimitry Andric 
318*0b57cec5SDimitry Andric   // Okay, all the blocks are renumbered.  If we have compactified the block
319*0b57cec5SDimitry Andric   // numbering, shrink MBBNumbering now.
320*0b57cec5SDimitry Andric   assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
321*0b57cec5SDimitry Andric   MBBNumbering.resize(BlockNo);
322*0b57cec5SDimitry Andric }
323*0b57cec5SDimitry Andric 
324*0b57cec5SDimitry Andric /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
325*0b57cec5SDimitry Andric MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
326*0b57cec5SDimitry Andric                                                   const DebugLoc &DL,
327*0b57cec5SDimitry Andric                                                   bool NoImp) {
328*0b57cec5SDimitry Andric   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
329*0b57cec5SDimitry Andric     MachineInstr(*this, MCID, DL, NoImp);
330*0b57cec5SDimitry Andric }
331*0b57cec5SDimitry Andric 
332*0b57cec5SDimitry Andric /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
333*0b57cec5SDimitry Andric /// identical in all ways except the instruction has no parent, prev, or next.
334*0b57cec5SDimitry Andric MachineInstr *
335*0b57cec5SDimitry Andric MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
336*0b57cec5SDimitry Andric   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
337*0b57cec5SDimitry Andric              MachineInstr(*this, *Orig);
338*0b57cec5SDimitry Andric }
339*0b57cec5SDimitry Andric 
340*0b57cec5SDimitry Andric MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
341*0b57cec5SDimitry Andric     MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
342*0b57cec5SDimitry Andric   MachineInstr *FirstClone = nullptr;
343*0b57cec5SDimitry Andric   MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
344*0b57cec5SDimitry Andric   while (true) {
345*0b57cec5SDimitry Andric     MachineInstr *Cloned = CloneMachineInstr(&*I);
346*0b57cec5SDimitry Andric     MBB.insert(InsertBefore, Cloned);
347*0b57cec5SDimitry Andric     if (FirstClone == nullptr) {
348*0b57cec5SDimitry Andric       FirstClone = Cloned;
349*0b57cec5SDimitry Andric     } else {
350*0b57cec5SDimitry Andric       Cloned->bundleWithPred();
351*0b57cec5SDimitry Andric     }
352*0b57cec5SDimitry Andric 
353*0b57cec5SDimitry Andric     if (!I->isBundledWithSucc())
354*0b57cec5SDimitry Andric       break;
355*0b57cec5SDimitry Andric     ++I;
356*0b57cec5SDimitry Andric   }
357*0b57cec5SDimitry Andric   return *FirstClone;
358*0b57cec5SDimitry Andric }
359*0b57cec5SDimitry Andric 
360*0b57cec5SDimitry Andric /// Delete the given MachineInstr.
361*0b57cec5SDimitry Andric ///
362*0b57cec5SDimitry Andric /// This function also serves as the MachineInstr destructor - the real
363*0b57cec5SDimitry Andric /// ~MachineInstr() destructor must be empty.
364*0b57cec5SDimitry Andric void
365*0b57cec5SDimitry Andric MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
366*0b57cec5SDimitry Andric   // Verify that a call site info is at valid state. This assertion should
367*0b57cec5SDimitry Andric   // be triggered during the implementation of support for the
368*0b57cec5SDimitry Andric   // call site info of a new architecture. If the assertion is triggered,
369*0b57cec5SDimitry Andric   // back trace will tell where to insert a call to updateCallSiteInfo().
370*0b57cec5SDimitry Andric   assert((!MI->isCall(MachineInstr::IgnoreBundle) ||
371*0b57cec5SDimitry Andric           CallSitesInfo.find(MI) == CallSitesInfo.end()) &&
372*0b57cec5SDimitry Andric          "Call site info was not updated!");
373*0b57cec5SDimitry Andric   // Strip it for parts. The operand array and the MI object itself are
374*0b57cec5SDimitry Andric   // independently recyclable.
375*0b57cec5SDimitry Andric   if (MI->Operands)
376*0b57cec5SDimitry Andric     deallocateOperandArray(MI->CapOperands, MI->Operands);
377*0b57cec5SDimitry Andric   // Don't call ~MachineInstr() which must be trivial anyway because
378*0b57cec5SDimitry Andric   // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
379*0b57cec5SDimitry Andric   // destructors.
380*0b57cec5SDimitry Andric   InstructionRecycler.Deallocate(Allocator, MI);
381*0b57cec5SDimitry Andric }
382*0b57cec5SDimitry Andric 
383*0b57cec5SDimitry Andric /// Allocate a new MachineBasicBlock. Use this instead of
384*0b57cec5SDimitry Andric /// `new MachineBasicBlock'.
385*0b57cec5SDimitry Andric MachineBasicBlock *
386*0b57cec5SDimitry Andric MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
387*0b57cec5SDimitry Andric   return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
388*0b57cec5SDimitry Andric              MachineBasicBlock(*this, bb);
389*0b57cec5SDimitry Andric }
390*0b57cec5SDimitry Andric 
391*0b57cec5SDimitry Andric /// Delete the given MachineBasicBlock.
392*0b57cec5SDimitry Andric void
393*0b57cec5SDimitry Andric MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
394*0b57cec5SDimitry Andric   assert(MBB->getParent() == this && "MBB parent mismatch!");
395*0b57cec5SDimitry Andric   MBB->~MachineBasicBlock();
396*0b57cec5SDimitry Andric   BasicBlockRecycler.Deallocate(Allocator, MBB);
397*0b57cec5SDimitry Andric }
398*0b57cec5SDimitry Andric 
399*0b57cec5SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand(
400*0b57cec5SDimitry Andric     MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
401*0b57cec5SDimitry Andric     unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
402*0b57cec5SDimitry Andric     SyncScope::ID SSID, AtomicOrdering Ordering,
403*0b57cec5SDimitry Andric     AtomicOrdering FailureOrdering) {
404*0b57cec5SDimitry Andric   return new (Allocator)
405*0b57cec5SDimitry Andric       MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
406*0b57cec5SDimitry Andric                         SSID, Ordering, FailureOrdering);
407*0b57cec5SDimitry Andric }
408*0b57cec5SDimitry Andric 
409*0b57cec5SDimitry Andric MachineMemOperand *
410*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
411*0b57cec5SDimitry Andric                                       int64_t Offset, uint64_t Size) {
412*0b57cec5SDimitry Andric   const MachinePointerInfo &PtrInfo = MMO->getPointerInfo();
413*0b57cec5SDimitry Andric 
414*0b57cec5SDimitry Andric   // If there is no pointer value, the offset isn't tracked so we need to adjust
415*0b57cec5SDimitry Andric   // the base alignment.
416*0b57cec5SDimitry Andric   unsigned Align = PtrInfo.V.isNull()
417*0b57cec5SDimitry Andric                        ? MinAlign(MMO->getBaseAlignment(), Offset)
418*0b57cec5SDimitry Andric                        : MMO->getBaseAlignment();
419*0b57cec5SDimitry Andric 
420*0b57cec5SDimitry Andric   return new (Allocator)
421*0b57cec5SDimitry Andric       MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size,
422*0b57cec5SDimitry Andric                         Align, AAMDNodes(), nullptr, MMO->getSyncScopeID(),
423*0b57cec5SDimitry Andric                         MMO->getOrdering(), MMO->getFailureOrdering());
424*0b57cec5SDimitry Andric }
425*0b57cec5SDimitry Andric 
426*0b57cec5SDimitry Andric MachineMemOperand *
427*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
428*0b57cec5SDimitry Andric                                       const AAMDNodes &AAInfo) {
429*0b57cec5SDimitry Andric   MachinePointerInfo MPI = MMO->getValue() ?
430*0b57cec5SDimitry Andric              MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
431*0b57cec5SDimitry Andric              MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
432*0b57cec5SDimitry Andric 
433*0b57cec5SDimitry Andric   return new (Allocator)
434*0b57cec5SDimitry Andric              MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
435*0b57cec5SDimitry Andric                                MMO->getBaseAlignment(), AAInfo,
436*0b57cec5SDimitry Andric                                MMO->getRanges(), MMO->getSyncScopeID(),
437*0b57cec5SDimitry Andric                                MMO->getOrdering(), MMO->getFailureOrdering());
438*0b57cec5SDimitry Andric }
439*0b57cec5SDimitry Andric 
440*0b57cec5SDimitry Andric MachineMemOperand *
441*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
442*0b57cec5SDimitry Andric                                       MachineMemOperand::Flags Flags) {
443*0b57cec5SDimitry Andric   return new (Allocator) MachineMemOperand(
444*0b57cec5SDimitry Andric       MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlignment(),
445*0b57cec5SDimitry Andric       MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(),
446*0b57cec5SDimitry Andric       MMO->getOrdering(), MMO->getFailureOrdering());
447*0b57cec5SDimitry Andric }
448*0b57cec5SDimitry Andric 
449*0b57cec5SDimitry Andric MachineInstr::ExtraInfo *
450*0b57cec5SDimitry Andric MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
451*0b57cec5SDimitry Andric                                    MCSymbol *PreInstrSymbol,
452*0b57cec5SDimitry Andric                                    MCSymbol *PostInstrSymbol) {
453*0b57cec5SDimitry Andric   return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
454*0b57cec5SDimitry Andric                                          PostInstrSymbol);
455*0b57cec5SDimitry Andric }
456*0b57cec5SDimitry Andric 
457*0b57cec5SDimitry Andric const char *MachineFunction::createExternalSymbolName(StringRef Name) {
458*0b57cec5SDimitry Andric   char *Dest = Allocator.Allocate<char>(Name.size() + 1);
459*0b57cec5SDimitry Andric   llvm::copy(Name, Dest);
460*0b57cec5SDimitry Andric   Dest[Name.size()] = 0;
461*0b57cec5SDimitry Andric   return Dest;
462*0b57cec5SDimitry Andric }
463*0b57cec5SDimitry Andric 
464*0b57cec5SDimitry Andric uint32_t *MachineFunction::allocateRegMask() {
465*0b57cec5SDimitry Andric   unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
466*0b57cec5SDimitry Andric   unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
467*0b57cec5SDimitry Andric   uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
468*0b57cec5SDimitry Andric   memset(Mask, 0, Size * sizeof(Mask[0]));
469*0b57cec5SDimitry Andric   return Mask;
470*0b57cec5SDimitry Andric }
471*0b57cec5SDimitry Andric 
472*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
473*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineFunction::dump() const {
474*0b57cec5SDimitry Andric   print(dbgs());
475*0b57cec5SDimitry Andric }
476*0b57cec5SDimitry Andric #endif
477*0b57cec5SDimitry Andric 
478*0b57cec5SDimitry Andric StringRef MachineFunction::getName() const {
479*0b57cec5SDimitry Andric   return getFunction().getName();
480*0b57cec5SDimitry Andric }
481*0b57cec5SDimitry Andric 
482*0b57cec5SDimitry Andric void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
483*0b57cec5SDimitry Andric   OS << "# Machine code for function " << getName() << ": ";
484*0b57cec5SDimitry Andric   getProperties().print(OS);
485*0b57cec5SDimitry Andric   OS << '\n';
486*0b57cec5SDimitry Andric 
487*0b57cec5SDimitry Andric   // Print Frame Information
488*0b57cec5SDimitry Andric   FrameInfo->print(*this, OS);
489*0b57cec5SDimitry Andric 
490*0b57cec5SDimitry Andric   // Print JumpTable Information
491*0b57cec5SDimitry Andric   if (JumpTableInfo)
492*0b57cec5SDimitry Andric     JumpTableInfo->print(OS);
493*0b57cec5SDimitry Andric 
494*0b57cec5SDimitry Andric   // Print Constant Pool
495*0b57cec5SDimitry Andric   ConstantPool->print(OS);
496*0b57cec5SDimitry Andric 
497*0b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
498*0b57cec5SDimitry Andric 
499*0b57cec5SDimitry Andric   if (RegInfo && !RegInfo->livein_empty()) {
500*0b57cec5SDimitry Andric     OS << "Function Live Ins: ";
501*0b57cec5SDimitry Andric     for (MachineRegisterInfo::livein_iterator
502*0b57cec5SDimitry Andric          I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
503*0b57cec5SDimitry Andric       OS << printReg(I->first, TRI);
504*0b57cec5SDimitry Andric       if (I->second)
505*0b57cec5SDimitry Andric         OS << " in " << printReg(I->second, TRI);
506*0b57cec5SDimitry Andric       if (std::next(I) != E)
507*0b57cec5SDimitry Andric         OS << ", ";
508*0b57cec5SDimitry Andric     }
509*0b57cec5SDimitry Andric     OS << '\n';
510*0b57cec5SDimitry Andric   }
511*0b57cec5SDimitry Andric 
512*0b57cec5SDimitry Andric   ModuleSlotTracker MST(getFunction().getParent());
513*0b57cec5SDimitry Andric   MST.incorporateFunction(getFunction());
514*0b57cec5SDimitry Andric   for (const auto &BB : *this) {
515*0b57cec5SDimitry Andric     OS << '\n';
516*0b57cec5SDimitry Andric     // If we print the whole function, print it at its most verbose level.
517*0b57cec5SDimitry Andric     BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
518*0b57cec5SDimitry Andric   }
519*0b57cec5SDimitry Andric 
520*0b57cec5SDimitry Andric   OS << "\n# End machine code for function " << getName() << ".\n\n";
521*0b57cec5SDimitry Andric }
522*0b57cec5SDimitry Andric 
523*0b57cec5SDimitry Andric namespace llvm {
524*0b57cec5SDimitry Andric 
525*0b57cec5SDimitry Andric   template<>
526*0b57cec5SDimitry Andric   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
527*0b57cec5SDimitry Andric     DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
528*0b57cec5SDimitry Andric 
529*0b57cec5SDimitry Andric     static std::string getGraphName(const MachineFunction *F) {
530*0b57cec5SDimitry Andric       return ("CFG for '" + F->getName() + "' function").str();
531*0b57cec5SDimitry Andric     }
532*0b57cec5SDimitry Andric 
533*0b57cec5SDimitry Andric     std::string getNodeLabel(const MachineBasicBlock *Node,
534*0b57cec5SDimitry Andric                              const MachineFunction *Graph) {
535*0b57cec5SDimitry Andric       std::string OutStr;
536*0b57cec5SDimitry Andric       {
537*0b57cec5SDimitry Andric         raw_string_ostream OSS(OutStr);
538*0b57cec5SDimitry Andric 
539*0b57cec5SDimitry Andric         if (isSimple()) {
540*0b57cec5SDimitry Andric           OSS << printMBBReference(*Node);
541*0b57cec5SDimitry Andric           if (const BasicBlock *BB = Node->getBasicBlock())
542*0b57cec5SDimitry Andric             OSS << ": " << BB->getName();
543*0b57cec5SDimitry Andric         } else
544*0b57cec5SDimitry Andric           Node->print(OSS);
545*0b57cec5SDimitry Andric       }
546*0b57cec5SDimitry Andric 
547*0b57cec5SDimitry Andric       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
548*0b57cec5SDimitry Andric 
549*0b57cec5SDimitry Andric       // Process string output to make it nicer...
550*0b57cec5SDimitry Andric       for (unsigned i = 0; i != OutStr.length(); ++i)
551*0b57cec5SDimitry Andric         if (OutStr[i] == '\n') {                            // Left justify
552*0b57cec5SDimitry Andric           OutStr[i] = '\\';
553*0b57cec5SDimitry Andric           OutStr.insert(OutStr.begin()+i+1, 'l');
554*0b57cec5SDimitry Andric         }
555*0b57cec5SDimitry Andric       return OutStr;
556*0b57cec5SDimitry Andric     }
557*0b57cec5SDimitry Andric   };
558*0b57cec5SDimitry Andric 
559*0b57cec5SDimitry Andric } // end namespace llvm
560*0b57cec5SDimitry Andric 
561*0b57cec5SDimitry Andric void MachineFunction::viewCFG() const
562*0b57cec5SDimitry Andric {
563*0b57cec5SDimitry Andric #ifndef NDEBUG
564*0b57cec5SDimitry Andric   ViewGraph(this, "mf" + getName());
565*0b57cec5SDimitry Andric #else
566*0b57cec5SDimitry Andric   errs() << "MachineFunction::viewCFG is only available in debug builds on "
567*0b57cec5SDimitry Andric          << "systems with Graphviz or gv!\n";
568*0b57cec5SDimitry Andric #endif // NDEBUG
569*0b57cec5SDimitry Andric }
570*0b57cec5SDimitry Andric 
571*0b57cec5SDimitry Andric void MachineFunction::viewCFGOnly() const
572*0b57cec5SDimitry Andric {
573*0b57cec5SDimitry Andric #ifndef NDEBUG
574*0b57cec5SDimitry Andric   ViewGraph(this, "mf" + getName(), true);
575*0b57cec5SDimitry Andric #else
576*0b57cec5SDimitry Andric   errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
577*0b57cec5SDimitry Andric          << "systems with Graphviz or gv!\n";
578*0b57cec5SDimitry Andric #endif // NDEBUG
579*0b57cec5SDimitry Andric }
580*0b57cec5SDimitry Andric 
581*0b57cec5SDimitry Andric /// Add the specified physical register as a live-in value and
582*0b57cec5SDimitry Andric /// create a corresponding virtual register for it.
583*0b57cec5SDimitry Andric unsigned MachineFunction::addLiveIn(unsigned PReg,
584*0b57cec5SDimitry Andric                                     const TargetRegisterClass *RC) {
585*0b57cec5SDimitry Andric   MachineRegisterInfo &MRI = getRegInfo();
586*0b57cec5SDimitry Andric   unsigned VReg = MRI.getLiveInVirtReg(PReg);
587*0b57cec5SDimitry Andric   if (VReg) {
588*0b57cec5SDimitry Andric     const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
589*0b57cec5SDimitry Andric     (void)VRegRC;
590*0b57cec5SDimitry Andric     // A physical register can be added several times.
591*0b57cec5SDimitry Andric     // Between two calls, the register class of the related virtual register
592*0b57cec5SDimitry Andric     // may have been constrained to match some operation constraints.
593*0b57cec5SDimitry Andric     // In that case, check that the current register class includes the
594*0b57cec5SDimitry Andric     // physical register and is a sub class of the specified RC.
595*0b57cec5SDimitry Andric     assert((VRegRC == RC || (VRegRC->contains(PReg) &&
596*0b57cec5SDimitry Andric                              RC->hasSubClassEq(VRegRC))) &&
597*0b57cec5SDimitry Andric             "Register class mismatch!");
598*0b57cec5SDimitry Andric     return VReg;
599*0b57cec5SDimitry Andric   }
600*0b57cec5SDimitry Andric   VReg = MRI.createVirtualRegister(RC);
601*0b57cec5SDimitry Andric   MRI.addLiveIn(PReg, VReg);
602*0b57cec5SDimitry Andric   return VReg;
603*0b57cec5SDimitry Andric }
604*0b57cec5SDimitry Andric 
605*0b57cec5SDimitry Andric /// Return the MCSymbol for the specified non-empty jump table.
606*0b57cec5SDimitry Andric /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
607*0b57cec5SDimitry Andric /// normal 'L' label is returned.
608*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
609*0b57cec5SDimitry Andric                                         bool isLinkerPrivate) const {
610*0b57cec5SDimitry Andric   const DataLayout &DL = getDataLayout();
611*0b57cec5SDimitry Andric   assert(JumpTableInfo && "No jump tables");
612*0b57cec5SDimitry Andric   assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
613*0b57cec5SDimitry Andric 
614*0b57cec5SDimitry Andric   StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
615*0b57cec5SDimitry Andric                                      : DL.getPrivateGlobalPrefix();
616*0b57cec5SDimitry Andric   SmallString<60> Name;
617*0b57cec5SDimitry Andric   raw_svector_ostream(Name)
618*0b57cec5SDimitry Andric     << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
619*0b57cec5SDimitry Andric   return Ctx.getOrCreateSymbol(Name);
620*0b57cec5SDimitry Andric }
621*0b57cec5SDimitry Andric 
622*0b57cec5SDimitry Andric /// Return a function-local symbol to represent the PIC base.
623*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getPICBaseSymbol() const {
624*0b57cec5SDimitry Andric   const DataLayout &DL = getDataLayout();
625*0b57cec5SDimitry Andric   return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
626*0b57cec5SDimitry Andric                                Twine(getFunctionNumber()) + "$pb");
627*0b57cec5SDimitry Andric }
628*0b57cec5SDimitry Andric 
629*0b57cec5SDimitry Andric /// \name Exception Handling
630*0b57cec5SDimitry Andric /// \{
631*0b57cec5SDimitry Andric 
632*0b57cec5SDimitry Andric LandingPadInfo &
633*0b57cec5SDimitry Andric MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
634*0b57cec5SDimitry Andric   unsigned N = LandingPads.size();
635*0b57cec5SDimitry Andric   for (unsigned i = 0; i < N; ++i) {
636*0b57cec5SDimitry Andric     LandingPadInfo &LP = LandingPads[i];
637*0b57cec5SDimitry Andric     if (LP.LandingPadBlock == LandingPad)
638*0b57cec5SDimitry Andric       return LP;
639*0b57cec5SDimitry Andric   }
640*0b57cec5SDimitry Andric 
641*0b57cec5SDimitry Andric   LandingPads.push_back(LandingPadInfo(LandingPad));
642*0b57cec5SDimitry Andric   return LandingPads[N];
643*0b57cec5SDimitry Andric }
644*0b57cec5SDimitry Andric 
645*0b57cec5SDimitry Andric void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
646*0b57cec5SDimitry Andric                                 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
647*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
648*0b57cec5SDimitry Andric   LP.BeginLabels.push_back(BeginLabel);
649*0b57cec5SDimitry Andric   LP.EndLabels.push_back(EndLabel);
650*0b57cec5SDimitry Andric }
651*0b57cec5SDimitry Andric 
652*0b57cec5SDimitry Andric MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
653*0b57cec5SDimitry Andric   MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
654*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
655*0b57cec5SDimitry Andric   LP.LandingPadLabel = LandingPadLabel;
656*0b57cec5SDimitry Andric 
657*0b57cec5SDimitry Andric   const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
658*0b57cec5SDimitry Andric   if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
659*0b57cec5SDimitry Andric     if (const auto *PF =
660*0b57cec5SDimitry Andric             dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
661*0b57cec5SDimitry Andric       getMMI().addPersonality(PF);
662*0b57cec5SDimitry Andric 
663*0b57cec5SDimitry Andric     if (LPI->isCleanup())
664*0b57cec5SDimitry Andric       addCleanup(LandingPad);
665*0b57cec5SDimitry Andric 
666*0b57cec5SDimitry Andric     // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
667*0b57cec5SDimitry Andric     //        correct, but we need to do it this way because of how the DWARF EH
668*0b57cec5SDimitry Andric     //        emitter processes the clauses.
669*0b57cec5SDimitry Andric     for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
670*0b57cec5SDimitry Andric       Value *Val = LPI->getClause(I - 1);
671*0b57cec5SDimitry Andric       if (LPI->isCatch(I - 1)) {
672*0b57cec5SDimitry Andric         addCatchTypeInfo(LandingPad,
673*0b57cec5SDimitry Andric                          dyn_cast<GlobalValue>(Val->stripPointerCasts()));
674*0b57cec5SDimitry Andric       } else {
675*0b57cec5SDimitry Andric         // Add filters in a list.
676*0b57cec5SDimitry Andric         auto *CVal = cast<Constant>(Val);
677*0b57cec5SDimitry Andric         SmallVector<const GlobalValue *, 4> FilterList;
678*0b57cec5SDimitry Andric         for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
679*0b57cec5SDimitry Andric              II != IE; ++II)
680*0b57cec5SDimitry Andric           FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
681*0b57cec5SDimitry Andric 
682*0b57cec5SDimitry Andric         addFilterTypeInfo(LandingPad, FilterList);
683*0b57cec5SDimitry Andric       }
684*0b57cec5SDimitry Andric     }
685*0b57cec5SDimitry Andric 
686*0b57cec5SDimitry Andric   } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
687*0b57cec5SDimitry Andric     for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) {
688*0b57cec5SDimitry Andric       Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
689*0b57cec5SDimitry Andric       addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
690*0b57cec5SDimitry Andric     }
691*0b57cec5SDimitry Andric 
692*0b57cec5SDimitry Andric   } else {
693*0b57cec5SDimitry Andric     assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
694*0b57cec5SDimitry Andric   }
695*0b57cec5SDimitry Andric 
696*0b57cec5SDimitry Andric   return LandingPadLabel;
697*0b57cec5SDimitry Andric }
698*0b57cec5SDimitry Andric 
699*0b57cec5SDimitry Andric void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
700*0b57cec5SDimitry Andric                                        ArrayRef<const GlobalValue *> TyInfo) {
701*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
702*0b57cec5SDimitry Andric   for (unsigned N = TyInfo.size(); N; --N)
703*0b57cec5SDimitry Andric     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
704*0b57cec5SDimitry Andric }
705*0b57cec5SDimitry Andric 
706*0b57cec5SDimitry Andric void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
707*0b57cec5SDimitry Andric                                         ArrayRef<const GlobalValue *> TyInfo) {
708*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
709*0b57cec5SDimitry Andric   std::vector<unsigned> IdsInFilter(TyInfo.size());
710*0b57cec5SDimitry Andric   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
711*0b57cec5SDimitry Andric     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
712*0b57cec5SDimitry Andric   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
713*0b57cec5SDimitry Andric }
714*0b57cec5SDimitry Andric 
715*0b57cec5SDimitry Andric void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
716*0b57cec5SDimitry Andric                                       bool TidyIfNoBeginLabels) {
717*0b57cec5SDimitry Andric   for (unsigned i = 0; i != LandingPads.size(); ) {
718*0b57cec5SDimitry Andric     LandingPadInfo &LandingPad = LandingPads[i];
719*0b57cec5SDimitry Andric     if (LandingPad.LandingPadLabel &&
720*0b57cec5SDimitry Andric         !LandingPad.LandingPadLabel->isDefined() &&
721*0b57cec5SDimitry Andric         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
722*0b57cec5SDimitry Andric       LandingPad.LandingPadLabel = nullptr;
723*0b57cec5SDimitry Andric 
724*0b57cec5SDimitry Andric     // Special case: we *should* emit LPs with null LP MBB. This indicates
725*0b57cec5SDimitry Andric     // "nounwind" case.
726*0b57cec5SDimitry Andric     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
727*0b57cec5SDimitry Andric       LandingPads.erase(LandingPads.begin() + i);
728*0b57cec5SDimitry Andric       continue;
729*0b57cec5SDimitry Andric     }
730*0b57cec5SDimitry Andric 
731*0b57cec5SDimitry Andric     if (TidyIfNoBeginLabels) {
732*0b57cec5SDimitry Andric       for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
733*0b57cec5SDimitry Andric         MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
734*0b57cec5SDimitry Andric         MCSymbol *EndLabel = LandingPad.EndLabels[j];
735*0b57cec5SDimitry Andric         if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
736*0b57cec5SDimitry Andric             (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
737*0b57cec5SDimitry Andric           continue;
738*0b57cec5SDimitry Andric 
739*0b57cec5SDimitry Andric         LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
740*0b57cec5SDimitry Andric         LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
741*0b57cec5SDimitry Andric         --j;
742*0b57cec5SDimitry Andric         --e;
743*0b57cec5SDimitry Andric       }
744*0b57cec5SDimitry Andric 
745*0b57cec5SDimitry Andric       // Remove landing pads with no try-ranges.
746*0b57cec5SDimitry Andric       if (LandingPads[i].BeginLabels.empty()) {
747*0b57cec5SDimitry Andric         LandingPads.erase(LandingPads.begin() + i);
748*0b57cec5SDimitry Andric         continue;
749*0b57cec5SDimitry Andric       }
750*0b57cec5SDimitry Andric     }
751*0b57cec5SDimitry Andric 
752*0b57cec5SDimitry Andric     // If there is no landing pad, ensure that the list of typeids is empty.
753*0b57cec5SDimitry Andric     // If the only typeid is a cleanup, this is the same as having no typeids.
754*0b57cec5SDimitry Andric     if (!LandingPad.LandingPadBlock ||
755*0b57cec5SDimitry Andric         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
756*0b57cec5SDimitry Andric       LandingPad.TypeIds.clear();
757*0b57cec5SDimitry Andric     ++i;
758*0b57cec5SDimitry Andric   }
759*0b57cec5SDimitry Andric }
760*0b57cec5SDimitry Andric 
761*0b57cec5SDimitry Andric void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
762*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
763*0b57cec5SDimitry Andric   LP.TypeIds.push_back(0);
764*0b57cec5SDimitry Andric }
765*0b57cec5SDimitry Andric 
766*0b57cec5SDimitry Andric void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
767*0b57cec5SDimitry Andric                                          const Function *Filter,
768*0b57cec5SDimitry Andric                                          const BlockAddress *RecoverBA) {
769*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
770*0b57cec5SDimitry Andric   SEHHandler Handler;
771*0b57cec5SDimitry Andric   Handler.FilterOrFinally = Filter;
772*0b57cec5SDimitry Andric   Handler.RecoverBA = RecoverBA;
773*0b57cec5SDimitry Andric   LP.SEHHandlers.push_back(Handler);
774*0b57cec5SDimitry Andric }
775*0b57cec5SDimitry Andric 
776*0b57cec5SDimitry Andric void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
777*0b57cec5SDimitry Andric                                            const Function *Cleanup) {
778*0b57cec5SDimitry Andric   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
779*0b57cec5SDimitry Andric   SEHHandler Handler;
780*0b57cec5SDimitry Andric   Handler.FilterOrFinally = Cleanup;
781*0b57cec5SDimitry Andric   Handler.RecoverBA = nullptr;
782*0b57cec5SDimitry Andric   LP.SEHHandlers.push_back(Handler);
783*0b57cec5SDimitry Andric }
784*0b57cec5SDimitry Andric 
785*0b57cec5SDimitry Andric void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
786*0b57cec5SDimitry Andric                                             ArrayRef<unsigned> Sites) {
787*0b57cec5SDimitry Andric   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
788*0b57cec5SDimitry Andric }
789*0b57cec5SDimitry Andric 
790*0b57cec5SDimitry Andric unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
791*0b57cec5SDimitry Andric   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
792*0b57cec5SDimitry Andric     if (TypeInfos[i] == TI) return i + 1;
793*0b57cec5SDimitry Andric 
794*0b57cec5SDimitry Andric   TypeInfos.push_back(TI);
795*0b57cec5SDimitry Andric   return TypeInfos.size();
796*0b57cec5SDimitry Andric }
797*0b57cec5SDimitry Andric 
798*0b57cec5SDimitry Andric int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
799*0b57cec5SDimitry Andric   // If the new filter coincides with the tail of an existing filter, then
800*0b57cec5SDimitry Andric   // re-use the existing filter.  Folding filters more than this requires
801*0b57cec5SDimitry Andric   // re-ordering filters and/or their elements - probably not worth it.
802*0b57cec5SDimitry Andric   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
803*0b57cec5SDimitry Andric        E = FilterEnds.end(); I != E; ++I) {
804*0b57cec5SDimitry Andric     unsigned i = *I, j = TyIds.size();
805*0b57cec5SDimitry Andric 
806*0b57cec5SDimitry Andric     while (i && j)
807*0b57cec5SDimitry Andric       if (FilterIds[--i] != TyIds[--j])
808*0b57cec5SDimitry Andric         goto try_next;
809*0b57cec5SDimitry Andric 
810*0b57cec5SDimitry Andric     if (!j)
811*0b57cec5SDimitry Andric       // The new filter coincides with range [i, end) of the existing filter.
812*0b57cec5SDimitry Andric       return -(1 + i);
813*0b57cec5SDimitry Andric 
814*0b57cec5SDimitry Andric try_next:;
815*0b57cec5SDimitry Andric   }
816*0b57cec5SDimitry Andric 
817*0b57cec5SDimitry Andric   // Add the new filter.
818*0b57cec5SDimitry Andric   int FilterID = -(1 + FilterIds.size());
819*0b57cec5SDimitry Andric   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
820*0b57cec5SDimitry Andric   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
821*0b57cec5SDimitry Andric   FilterEnds.push_back(FilterIds.size());
822*0b57cec5SDimitry Andric   FilterIds.push_back(0); // terminator
823*0b57cec5SDimitry Andric   return FilterID;
824*0b57cec5SDimitry Andric }
825*0b57cec5SDimitry Andric 
826*0b57cec5SDimitry Andric void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I, MDNode *MD) {
827*0b57cec5SDimitry Andric   MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true);
828*0b57cec5SDimitry Andric   MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true);
829*0b57cec5SDimitry Andric   I->setPreInstrSymbol(*this, BeginLabel);
830*0b57cec5SDimitry Andric   I->setPostInstrSymbol(*this, EndLabel);
831*0b57cec5SDimitry Andric 
832*0b57cec5SDimitry Andric   DIType *DI = dyn_cast<DIType>(MD);
833*0b57cec5SDimitry Andric   CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI));
834*0b57cec5SDimitry Andric }
835*0b57cec5SDimitry Andric 
836*0b57cec5SDimitry Andric void MachineFunction::updateCallSiteInfo(const MachineInstr *Old,
837*0b57cec5SDimitry Andric                                          const MachineInstr *New) {
838*0b57cec5SDimitry Andric   if (!Target.Options.EnableDebugEntryValues || Old == New)
839*0b57cec5SDimitry Andric     return;
840*0b57cec5SDimitry Andric 
841*0b57cec5SDimitry Andric   assert(Old->isCall() && (!New || New->isCall()) &&
842*0b57cec5SDimitry Andric          "Call site info referes only to call instructions!");
843*0b57cec5SDimitry Andric   CallSiteInfoMap::iterator CSIt = CallSitesInfo.find(Old);
844*0b57cec5SDimitry Andric   if (CSIt == CallSitesInfo.end())
845*0b57cec5SDimitry Andric     return;
846*0b57cec5SDimitry Andric   CallSiteInfo CSInfo = std::move(CSIt->second);
847*0b57cec5SDimitry Andric   CallSitesInfo.erase(CSIt);
848*0b57cec5SDimitry Andric   if (New)
849*0b57cec5SDimitry Andric     CallSitesInfo[New] = CSInfo;
850*0b57cec5SDimitry Andric }
851*0b57cec5SDimitry Andric 
852*0b57cec5SDimitry Andric /// \}
853*0b57cec5SDimitry Andric 
854*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
855*0b57cec5SDimitry Andric //  MachineJumpTableInfo implementation
856*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
857*0b57cec5SDimitry Andric 
858*0b57cec5SDimitry Andric /// Return the size of each entry in the jump table.
859*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
860*0b57cec5SDimitry Andric   // The size of a jump table entry is 4 bytes unless the entry is just the
861*0b57cec5SDimitry Andric   // address of a block, in which case it is the pointer size.
862*0b57cec5SDimitry Andric   switch (getEntryKind()) {
863*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_BlockAddress:
864*0b57cec5SDimitry Andric     return TD.getPointerSize();
865*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
866*0b57cec5SDimitry Andric     return 8;
867*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
868*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_LabelDifference32:
869*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_Custom32:
870*0b57cec5SDimitry Andric     return 4;
871*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_Inline:
872*0b57cec5SDimitry Andric     return 0;
873*0b57cec5SDimitry Andric   }
874*0b57cec5SDimitry Andric   llvm_unreachable("Unknown jump table encoding!");
875*0b57cec5SDimitry Andric }
876*0b57cec5SDimitry Andric 
877*0b57cec5SDimitry Andric /// Return the alignment of each entry in the jump table.
878*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
879*0b57cec5SDimitry Andric   // The alignment of a jump table entry is the alignment of int32 unless the
880*0b57cec5SDimitry Andric   // entry is just the address of a block, in which case it is the pointer
881*0b57cec5SDimitry Andric   // alignment.
882*0b57cec5SDimitry Andric   switch (getEntryKind()) {
883*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_BlockAddress:
884*0b57cec5SDimitry Andric     return TD.getPointerABIAlignment(0);
885*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
886*0b57cec5SDimitry Andric     return TD.getABIIntegerTypeAlignment(64);
887*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
888*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_LabelDifference32:
889*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_Custom32:
890*0b57cec5SDimitry Andric     return TD.getABIIntegerTypeAlignment(32);
891*0b57cec5SDimitry Andric   case MachineJumpTableInfo::EK_Inline:
892*0b57cec5SDimitry Andric     return 1;
893*0b57cec5SDimitry Andric   }
894*0b57cec5SDimitry Andric   llvm_unreachable("Unknown jump table encoding!");
895*0b57cec5SDimitry Andric }
896*0b57cec5SDimitry Andric 
897*0b57cec5SDimitry Andric /// Create a new jump table entry in the jump table info.
898*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::createJumpTableIndex(
899*0b57cec5SDimitry Andric                                const std::vector<MachineBasicBlock*> &DestBBs) {
900*0b57cec5SDimitry Andric   assert(!DestBBs.empty() && "Cannot create an empty jump table!");
901*0b57cec5SDimitry Andric   JumpTables.push_back(MachineJumpTableEntry(DestBBs));
902*0b57cec5SDimitry Andric   return JumpTables.size()-1;
903*0b57cec5SDimitry Andric }
904*0b57cec5SDimitry Andric 
905*0b57cec5SDimitry Andric /// If Old is the target of any jump tables, update the jump tables to branch
906*0b57cec5SDimitry Andric /// to New instead.
907*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
908*0b57cec5SDimitry Andric                                                   MachineBasicBlock *New) {
909*0b57cec5SDimitry Andric   assert(Old != New && "Not making a change?");
910*0b57cec5SDimitry Andric   bool MadeChange = false;
911*0b57cec5SDimitry Andric   for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
912*0b57cec5SDimitry Andric     ReplaceMBBInJumpTable(i, Old, New);
913*0b57cec5SDimitry Andric   return MadeChange;
914*0b57cec5SDimitry Andric }
915*0b57cec5SDimitry Andric 
916*0b57cec5SDimitry Andric /// If Old is a target of the jump tables, update the jump table to branch to
917*0b57cec5SDimitry Andric /// New instead.
918*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
919*0b57cec5SDimitry Andric                                                  MachineBasicBlock *Old,
920*0b57cec5SDimitry Andric                                                  MachineBasicBlock *New) {
921*0b57cec5SDimitry Andric   assert(Old != New && "Not making a change?");
922*0b57cec5SDimitry Andric   bool MadeChange = false;
923*0b57cec5SDimitry Andric   MachineJumpTableEntry &JTE = JumpTables[Idx];
924*0b57cec5SDimitry Andric   for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
925*0b57cec5SDimitry Andric     if (JTE.MBBs[j] == Old) {
926*0b57cec5SDimitry Andric       JTE.MBBs[j] = New;
927*0b57cec5SDimitry Andric       MadeChange = true;
928*0b57cec5SDimitry Andric     }
929*0b57cec5SDimitry Andric   return MadeChange;
930*0b57cec5SDimitry Andric }
931*0b57cec5SDimitry Andric 
932*0b57cec5SDimitry Andric void MachineJumpTableInfo::print(raw_ostream &OS) const {
933*0b57cec5SDimitry Andric   if (JumpTables.empty()) return;
934*0b57cec5SDimitry Andric 
935*0b57cec5SDimitry Andric   OS << "Jump Tables:\n";
936*0b57cec5SDimitry Andric 
937*0b57cec5SDimitry Andric   for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
938*0b57cec5SDimitry Andric     OS << printJumpTableEntryReference(i) << ':';
939*0b57cec5SDimitry Andric     for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
940*0b57cec5SDimitry Andric       OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
941*0b57cec5SDimitry Andric     if (i != e)
942*0b57cec5SDimitry Andric       OS << '\n';
943*0b57cec5SDimitry Andric   }
944*0b57cec5SDimitry Andric 
945*0b57cec5SDimitry Andric   OS << '\n';
946*0b57cec5SDimitry Andric }
947*0b57cec5SDimitry Andric 
948*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
949*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
950*0b57cec5SDimitry Andric #endif
951*0b57cec5SDimitry Andric 
952*0b57cec5SDimitry Andric Printable llvm::printJumpTableEntryReference(unsigned Idx) {
953*0b57cec5SDimitry Andric   return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
954*0b57cec5SDimitry Andric }
955*0b57cec5SDimitry Andric 
956*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
957*0b57cec5SDimitry Andric //  MachineConstantPool implementation
958*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
959*0b57cec5SDimitry Andric 
960*0b57cec5SDimitry Andric void MachineConstantPoolValue::anchor() {}
961*0b57cec5SDimitry Andric 
962*0b57cec5SDimitry Andric Type *MachineConstantPoolEntry::getType() const {
963*0b57cec5SDimitry Andric   if (isMachineConstantPoolEntry())
964*0b57cec5SDimitry Andric     return Val.MachineCPVal->getType();
965*0b57cec5SDimitry Andric   return Val.ConstVal->getType();
966*0b57cec5SDimitry Andric }
967*0b57cec5SDimitry Andric 
968*0b57cec5SDimitry Andric bool MachineConstantPoolEntry::needsRelocation() const {
969*0b57cec5SDimitry Andric   if (isMachineConstantPoolEntry())
970*0b57cec5SDimitry Andric     return true;
971*0b57cec5SDimitry Andric   return Val.ConstVal->needsRelocation();
972*0b57cec5SDimitry Andric }
973*0b57cec5SDimitry Andric 
974*0b57cec5SDimitry Andric SectionKind
975*0b57cec5SDimitry Andric MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
976*0b57cec5SDimitry Andric   if (needsRelocation())
977*0b57cec5SDimitry Andric     return SectionKind::getReadOnlyWithRel();
978*0b57cec5SDimitry Andric   switch (DL->getTypeAllocSize(getType())) {
979*0b57cec5SDimitry Andric   case 4:
980*0b57cec5SDimitry Andric     return SectionKind::getMergeableConst4();
981*0b57cec5SDimitry Andric   case 8:
982*0b57cec5SDimitry Andric     return SectionKind::getMergeableConst8();
983*0b57cec5SDimitry Andric   case 16:
984*0b57cec5SDimitry Andric     return SectionKind::getMergeableConst16();
985*0b57cec5SDimitry Andric   case 32:
986*0b57cec5SDimitry Andric     return SectionKind::getMergeableConst32();
987*0b57cec5SDimitry Andric   default:
988*0b57cec5SDimitry Andric     return SectionKind::getReadOnly();
989*0b57cec5SDimitry Andric   }
990*0b57cec5SDimitry Andric }
991*0b57cec5SDimitry Andric 
992*0b57cec5SDimitry Andric MachineConstantPool::~MachineConstantPool() {
993*0b57cec5SDimitry Andric   // A constant may be a member of both Constants and MachineCPVsSharingEntries,
994*0b57cec5SDimitry Andric   // so keep track of which we've deleted to avoid double deletions.
995*0b57cec5SDimitry Andric   DenseSet<MachineConstantPoolValue*> Deleted;
996*0b57cec5SDimitry Andric   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
997*0b57cec5SDimitry Andric     if (Constants[i].isMachineConstantPoolEntry()) {
998*0b57cec5SDimitry Andric       Deleted.insert(Constants[i].Val.MachineCPVal);
999*0b57cec5SDimitry Andric       delete Constants[i].Val.MachineCPVal;
1000*0b57cec5SDimitry Andric     }
1001*0b57cec5SDimitry Andric   for (DenseSet<MachineConstantPoolValue*>::iterator I =
1002*0b57cec5SDimitry Andric        MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
1003*0b57cec5SDimitry Andric        I != E; ++I) {
1004*0b57cec5SDimitry Andric     if (Deleted.count(*I) == 0)
1005*0b57cec5SDimitry Andric       delete *I;
1006*0b57cec5SDimitry Andric   }
1007*0b57cec5SDimitry Andric }
1008*0b57cec5SDimitry Andric 
1009*0b57cec5SDimitry Andric /// Test whether the given two constants can be allocated the same constant pool
1010*0b57cec5SDimitry Andric /// entry.
1011*0b57cec5SDimitry Andric static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
1012*0b57cec5SDimitry Andric                                       const DataLayout &DL) {
1013*0b57cec5SDimitry Andric   // Handle the trivial case quickly.
1014*0b57cec5SDimitry Andric   if (A == B) return true;
1015*0b57cec5SDimitry Andric 
1016*0b57cec5SDimitry Andric   // If they have the same type but weren't the same constant, quickly
1017*0b57cec5SDimitry Andric   // reject them.
1018*0b57cec5SDimitry Andric   if (A->getType() == B->getType()) return false;
1019*0b57cec5SDimitry Andric 
1020*0b57cec5SDimitry Andric   // We can't handle structs or arrays.
1021*0b57cec5SDimitry Andric   if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
1022*0b57cec5SDimitry Andric       isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
1023*0b57cec5SDimitry Andric     return false;
1024*0b57cec5SDimitry Andric 
1025*0b57cec5SDimitry Andric   // For now, only support constants with the same size.
1026*0b57cec5SDimitry Andric   uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
1027*0b57cec5SDimitry Andric   if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
1028*0b57cec5SDimitry Andric     return false;
1029*0b57cec5SDimitry Andric 
1030*0b57cec5SDimitry Andric   Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
1031*0b57cec5SDimitry Andric 
1032*0b57cec5SDimitry Andric   // Try constant folding a bitcast of both instructions to an integer.  If we
1033*0b57cec5SDimitry Andric   // get two identical ConstantInt's, then we are good to share them.  We use
1034*0b57cec5SDimitry Andric   // the constant folding APIs to do this so that we get the benefit of
1035*0b57cec5SDimitry Andric   // DataLayout.
1036*0b57cec5SDimitry Andric   if (isa<PointerType>(A->getType()))
1037*0b57cec5SDimitry Andric     A = ConstantFoldCastOperand(Instruction::PtrToInt,
1038*0b57cec5SDimitry Andric                                 const_cast<Constant *>(A), IntTy, DL);
1039*0b57cec5SDimitry Andric   else if (A->getType() != IntTy)
1040*0b57cec5SDimitry Andric     A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
1041*0b57cec5SDimitry Andric                                 IntTy, DL);
1042*0b57cec5SDimitry Andric   if (isa<PointerType>(B->getType()))
1043*0b57cec5SDimitry Andric     B = ConstantFoldCastOperand(Instruction::PtrToInt,
1044*0b57cec5SDimitry Andric                                 const_cast<Constant *>(B), IntTy, DL);
1045*0b57cec5SDimitry Andric   else if (B->getType() != IntTy)
1046*0b57cec5SDimitry Andric     B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
1047*0b57cec5SDimitry Andric                                 IntTy, DL);
1048*0b57cec5SDimitry Andric 
1049*0b57cec5SDimitry Andric   return A == B;
1050*0b57cec5SDimitry Andric }
1051*0b57cec5SDimitry Andric 
1052*0b57cec5SDimitry Andric /// Create a new entry in the constant pool or return an existing one.
1053*0b57cec5SDimitry Andric /// User must specify the log2 of the minimum required alignment for the object.
1054*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
1055*0b57cec5SDimitry Andric                                                    unsigned Alignment) {
1056*0b57cec5SDimitry Andric   assert(Alignment && "Alignment must be specified!");
1057*0b57cec5SDimitry Andric   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1058*0b57cec5SDimitry Andric 
1059*0b57cec5SDimitry Andric   // Check to see if we already have this constant.
1060*0b57cec5SDimitry Andric   //
1061*0b57cec5SDimitry Andric   // FIXME, this could be made much more efficient for large constant pools.
1062*0b57cec5SDimitry Andric   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
1063*0b57cec5SDimitry Andric     if (!Constants[i].isMachineConstantPoolEntry() &&
1064*0b57cec5SDimitry Andric         CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
1065*0b57cec5SDimitry Andric       if ((unsigned)Constants[i].getAlignment() < Alignment)
1066*0b57cec5SDimitry Andric         Constants[i].Alignment = Alignment;
1067*0b57cec5SDimitry Andric       return i;
1068*0b57cec5SDimitry Andric     }
1069*0b57cec5SDimitry Andric 
1070*0b57cec5SDimitry Andric   Constants.push_back(MachineConstantPoolEntry(C, Alignment));
1071*0b57cec5SDimitry Andric   return Constants.size()-1;
1072*0b57cec5SDimitry Andric }
1073*0b57cec5SDimitry Andric 
1074*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1075*0b57cec5SDimitry Andric                                                    unsigned Alignment) {
1076*0b57cec5SDimitry Andric   assert(Alignment && "Alignment must be specified!");
1077*0b57cec5SDimitry Andric   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1078*0b57cec5SDimitry Andric 
1079*0b57cec5SDimitry Andric   // Check to see if we already have this constant.
1080*0b57cec5SDimitry Andric   //
1081*0b57cec5SDimitry Andric   // FIXME, this could be made much more efficient for large constant pools.
1082*0b57cec5SDimitry Andric   int Idx = V->getExistingMachineCPValue(this, Alignment);
1083*0b57cec5SDimitry Andric   if (Idx != -1) {
1084*0b57cec5SDimitry Andric     MachineCPVsSharingEntries.insert(V);
1085*0b57cec5SDimitry Andric     return (unsigned)Idx;
1086*0b57cec5SDimitry Andric   }
1087*0b57cec5SDimitry Andric 
1088*0b57cec5SDimitry Andric   Constants.push_back(MachineConstantPoolEntry(V, Alignment));
1089*0b57cec5SDimitry Andric   return Constants.size()-1;
1090*0b57cec5SDimitry Andric }
1091*0b57cec5SDimitry Andric 
1092*0b57cec5SDimitry Andric void MachineConstantPool::print(raw_ostream &OS) const {
1093*0b57cec5SDimitry Andric   if (Constants.empty()) return;
1094*0b57cec5SDimitry Andric 
1095*0b57cec5SDimitry Andric   OS << "Constant Pool:\n";
1096*0b57cec5SDimitry Andric   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
1097*0b57cec5SDimitry Andric     OS << "  cp#" << i << ": ";
1098*0b57cec5SDimitry Andric     if (Constants[i].isMachineConstantPoolEntry())
1099*0b57cec5SDimitry Andric       Constants[i].Val.MachineCPVal->print(OS);
1100*0b57cec5SDimitry Andric     else
1101*0b57cec5SDimitry Andric       Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1102*0b57cec5SDimitry Andric     OS << ", align=" << Constants[i].getAlignment();
1103*0b57cec5SDimitry Andric     OS << "\n";
1104*0b57cec5SDimitry Andric   }
1105*0b57cec5SDimitry Andric }
1106*0b57cec5SDimitry Andric 
1107*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1108*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1109*0b57cec5SDimitry Andric #endif
1110