xref: /aosp_15_r20/external/llvm/lib/CodeGen/MachineFunction.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- MachineFunction.cpp -----------------------------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Collect native machine code information for a function.  This allows
11*9880d681SAndroid Build Coastguard Worker // target-specific information about the generated code to be stored with each
12*9880d681SAndroid Build Coastguard Worker // function.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker 
16*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallString.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ConstantFolding.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/EHPersonalities.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineConstantPool.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionInitializer.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstr.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineJumpTableInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineModuleInfo.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/PseudoSourceValue.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/WinEHFuncInfo.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DebugInfo.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ModuleSlotTracker.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCContext.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/GraphWriter.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetFrameLowering.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetLowering.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
46*9880d681SAndroid Build Coastguard Worker using namespace llvm;
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "codegen"
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker static cl::opt<unsigned>
51*9880d681SAndroid Build Coastguard Worker     AlignAllFunctions("align-all-functions",
52*9880d681SAndroid Build Coastguard Worker                       cl::desc("Force the alignment of all functions."),
53*9880d681SAndroid Build Coastguard Worker                       cl::init(0), cl::Hidden);
54*9880d681SAndroid Build Coastguard Worker 
anchor()55*9880d681SAndroid Build Coastguard Worker void MachineFunctionInitializer::anchor() {}
56*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & ROS,bool OnlySet) const57*9880d681SAndroid Build Coastguard Worker void MachineFunctionProperties::print(raw_ostream &ROS, bool OnlySet) const {
58*9880d681SAndroid Build Coastguard Worker   // Leave this function even in NDEBUG as an out-of-line anchor.
59*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60*9880d681SAndroid Build Coastguard Worker   for (BitVector::size_type i = 0; i < Properties.size(); ++i) {
61*9880d681SAndroid Build Coastguard Worker     bool HasProperty = Properties[i];
62*9880d681SAndroid Build Coastguard Worker     if (OnlySet && !HasProperty)
63*9880d681SAndroid Build Coastguard Worker       continue;
64*9880d681SAndroid Build Coastguard Worker     switch(static_cast<Property>(i)) {
65*9880d681SAndroid Build Coastguard Worker       case Property::IsSSA:
66*9880d681SAndroid Build Coastguard Worker         ROS << (HasProperty ? "SSA, " : "Post SSA, ");
67*9880d681SAndroid Build Coastguard Worker         break;
68*9880d681SAndroid Build Coastguard Worker       case Property::TracksLiveness:
69*9880d681SAndroid Build Coastguard Worker         ROS << (HasProperty ? "" : "not ") << "tracking liveness, ";
70*9880d681SAndroid Build Coastguard Worker         break;
71*9880d681SAndroid Build Coastguard Worker       case Property::AllVRegsAllocated:
72*9880d681SAndroid Build Coastguard Worker         ROS << (HasProperty ? "AllVRegsAllocated" : "HasVRegs");
73*9880d681SAndroid Build Coastguard Worker         break;
74*9880d681SAndroid Build Coastguard Worker       default:
75*9880d681SAndroid Build Coastguard Worker         break;
76*9880d681SAndroid Build Coastguard Worker     }
77*9880d681SAndroid Build Coastguard Worker   }
78*9880d681SAndroid Build Coastguard Worker #endif
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker 
81*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
82*9880d681SAndroid Build Coastguard Worker // MachineFunction implementation
83*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
84*9880d681SAndroid Build Coastguard Worker 
85*9880d681SAndroid Build Coastguard Worker // Out-of-line virtual method.
~MachineFunctionInfo()86*9880d681SAndroid Build Coastguard Worker MachineFunctionInfo::~MachineFunctionInfo() {}
87*9880d681SAndroid Build Coastguard Worker 
deleteNode(MachineBasicBlock * MBB)88*9880d681SAndroid Build Coastguard Worker void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
89*9880d681SAndroid Build Coastguard Worker   MBB->getParent()->DeleteMachineBasicBlock(MBB);
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker 
getFnStackAlignment(const TargetSubtargetInfo * STI,const Function * Fn)92*9880d681SAndroid Build Coastguard Worker static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
93*9880d681SAndroid Build Coastguard Worker                                            const Function *Fn) {
94*9880d681SAndroid Build Coastguard Worker   if (Fn->hasFnAttribute(Attribute::StackAlignment))
95*9880d681SAndroid Build Coastguard Worker     return Fn->getFnStackAlignment();
96*9880d681SAndroid Build Coastguard Worker   return STI->getFrameLowering()->getStackAlignment();
97*9880d681SAndroid Build Coastguard Worker }
98*9880d681SAndroid Build Coastguard Worker 
MachineFunction(const Function * F,const TargetMachine & TM,unsigned FunctionNum,MachineModuleInfo & mmi)99*9880d681SAndroid Build Coastguard Worker MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
100*9880d681SAndroid Build Coastguard Worker                                  unsigned FunctionNum, MachineModuleInfo &mmi)
101*9880d681SAndroid Build Coastguard Worker     : Fn(F), Target(TM), STI(TM.getSubtargetImpl(*F)), Ctx(mmi.getContext()),
102*9880d681SAndroid Build Coastguard Worker       MMI(mmi) {
103*9880d681SAndroid Build Coastguard Worker   // Assume the function starts in SSA form with correct liveness.
104*9880d681SAndroid Build Coastguard Worker   Properties.set(MachineFunctionProperties::Property::IsSSA);
105*9880d681SAndroid Build Coastguard Worker   Properties.set(MachineFunctionProperties::Property::TracksLiveness);
106*9880d681SAndroid Build Coastguard Worker   if (STI->getRegisterInfo())
107*9880d681SAndroid Build Coastguard Worker     RegInfo = new (Allocator) MachineRegisterInfo(this);
108*9880d681SAndroid Build Coastguard Worker   else
109*9880d681SAndroid Build Coastguard Worker     RegInfo = nullptr;
110*9880d681SAndroid Build Coastguard Worker 
111*9880d681SAndroid Build Coastguard Worker   MFInfo = nullptr;
112*9880d681SAndroid Build Coastguard Worker   // We can realign the stack if the target supports it and the user hasn't
113*9880d681SAndroid Build Coastguard Worker   // explicitly asked us not to.
114*9880d681SAndroid Build Coastguard Worker   bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
115*9880d681SAndroid Build Coastguard Worker                       !F->hasFnAttribute("no-realign-stack");
116*9880d681SAndroid Build Coastguard Worker   FrameInfo = new (Allocator) MachineFrameInfo(
117*9880d681SAndroid Build Coastguard Worker       getFnStackAlignment(STI, Fn), /*StackRealignable=*/CanRealignSP,
118*9880d681SAndroid Build Coastguard Worker       /*ForceRealign=*/CanRealignSP &&
119*9880d681SAndroid Build Coastguard Worker           F->hasFnAttribute(Attribute::StackAlignment));
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   if (Fn->hasFnAttribute(Attribute::StackAlignment))
122*9880d681SAndroid Build Coastguard Worker     FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment());
123*9880d681SAndroid Build Coastguard Worker 
124*9880d681SAndroid Build Coastguard Worker   ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
125*9880d681SAndroid Build Coastguard Worker   Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
126*9880d681SAndroid Build Coastguard Worker 
127*9880d681SAndroid Build Coastguard Worker   // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
128*9880d681SAndroid Build Coastguard Worker   // FIXME: Use Function::optForSize().
129*9880d681SAndroid Build Coastguard Worker   if (!Fn->hasFnAttribute(Attribute::OptimizeForSize))
130*9880d681SAndroid Build Coastguard Worker     Alignment = std::max(Alignment,
131*9880d681SAndroid Build Coastguard Worker                          STI->getTargetLowering()->getPrefFunctionAlignment());
132*9880d681SAndroid Build Coastguard Worker 
133*9880d681SAndroid Build Coastguard Worker   if (AlignAllFunctions)
134*9880d681SAndroid Build Coastguard Worker     Alignment = AlignAllFunctions;
135*9880d681SAndroid Build Coastguard Worker 
136*9880d681SAndroid Build Coastguard Worker   FunctionNumber = FunctionNum;
137*9880d681SAndroid Build Coastguard Worker   JumpTableInfo = nullptr;
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker   if (isFuncletEHPersonality(classifyEHPersonality(
140*9880d681SAndroid Build Coastguard Worker           F->hasPersonalityFn() ? F->getPersonalityFn() : nullptr))) {
141*9880d681SAndroid Build Coastguard Worker     WinEHInfo = new (Allocator) WinEHFuncInfo();
142*9880d681SAndroid Build Coastguard Worker   }
143*9880d681SAndroid Build Coastguard Worker 
144*9880d681SAndroid Build Coastguard Worker   assert(TM.isCompatibleDataLayout(getDataLayout()) &&
145*9880d681SAndroid Build Coastguard Worker          "Can't create a MachineFunction using a Module with a "
146*9880d681SAndroid Build Coastguard Worker          "Target-incompatible DataLayout attached\n");
147*9880d681SAndroid Build Coastguard Worker 
148*9880d681SAndroid Build Coastguard Worker   PSVManager = llvm::make_unique<PseudoSourceValueManager>();
149*9880d681SAndroid Build Coastguard Worker }
150*9880d681SAndroid Build Coastguard Worker 
~MachineFunction()151*9880d681SAndroid Build Coastguard Worker MachineFunction::~MachineFunction() {
152*9880d681SAndroid Build Coastguard Worker   // Don't call destructors on MachineInstr and MachineOperand. All of their
153*9880d681SAndroid Build Coastguard Worker   // memory comes from the BumpPtrAllocator which is about to be purged.
154*9880d681SAndroid Build Coastguard Worker   //
155*9880d681SAndroid Build Coastguard Worker   // Do call MachineBasicBlock destructors, it contains std::vectors.
156*9880d681SAndroid Build Coastguard Worker   for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
157*9880d681SAndroid Build Coastguard Worker     I->Insts.clearAndLeakNodesUnsafely();
158*9880d681SAndroid Build Coastguard Worker 
159*9880d681SAndroid Build Coastguard Worker   InstructionRecycler.clear(Allocator);
160*9880d681SAndroid Build Coastguard Worker   OperandRecycler.clear(Allocator);
161*9880d681SAndroid Build Coastguard Worker   BasicBlockRecycler.clear(Allocator);
162*9880d681SAndroid Build Coastguard Worker   if (RegInfo) {
163*9880d681SAndroid Build Coastguard Worker     RegInfo->~MachineRegisterInfo();
164*9880d681SAndroid Build Coastguard Worker     Allocator.Deallocate(RegInfo);
165*9880d681SAndroid Build Coastguard Worker   }
166*9880d681SAndroid Build Coastguard Worker   if (MFInfo) {
167*9880d681SAndroid Build Coastguard Worker     MFInfo->~MachineFunctionInfo();
168*9880d681SAndroid Build Coastguard Worker     Allocator.Deallocate(MFInfo);
169*9880d681SAndroid Build Coastguard Worker   }
170*9880d681SAndroid Build Coastguard Worker 
171*9880d681SAndroid Build Coastguard Worker   FrameInfo->~MachineFrameInfo();
172*9880d681SAndroid Build Coastguard Worker   Allocator.Deallocate(FrameInfo);
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker   ConstantPool->~MachineConstantPool();
175*9880d681SAndroid Build Coastguard Worker   Allocator.Deallocate(ConstantPool);
176*9880d681SAndroid Build Coastguard Worker 
177*9880d681SAndroid Build Coastguard Worker   if (JumpTableInfo) {
178*9880d681SAndroid Build Coastguard Worker     JumpTableInfo->~MachineJumpTableInfo();
179*9880d681SAndroid Build Coastguard Worker     Allocator.Deallocate(JumpTableInfo);
180*9880d681SAndroid Build Coastguard Worker   }
181*9880d681SAndroid Build Coastguard Worker 
182*9880d681SAndroid Build Coastguard Worker   if (WinEHInfo) {
183*9880d681SAndroid Build Coastguard Worker     WinEHInfo->~WinEHFuncInfo();
184*9880d681SAndroid Build Coastguard Worker     Allocator.Deallocate(WinEHInfo);
185*9880d681SAndroid Build Coastguard Worker   }
186*9880d681SAndroid Build Coastguard Worker }
187*9880d681SAndroid Build Coastguard Worker 
getDataLayout() const188*9880d681SAndroid Build Coastguard Worker const DataLayout &MachineFunction::getDataLayout() const {
189*9880d681SAndroid Build Coastguard Worker   return Fn->getParent()->getDataLayout();
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker /// Get the JumpTableInfo for this function.
193*9880d681SAndroid Build Coastguard Worker /// If it does not already exist, allocate one.
194*9880d681SAndroid Build Coastguard Worker MachineJumpTableInfo *MachineFunction::
getOrCreateJumpTableInfo(unsigned EntryKind)195*9880d681SAndroid Build Coastguard Worker getOrCreateJumpTableInfo(unsigned EntryKind) {
196*9880d681SAndroid Build Coastguard Worker   if (JumpTableInfo) return JumpTableInfo;
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker   JumpTableInfo = new (Allocator)
199*9880d681SAndroid Build Coastguard Worker     MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
200*9880d681SAndroid Build Coastguard Worker   return JumpTableInfo;
201*9880d681SAndroid Build Coastguard Worker }
202*9880d681SAndroid Build Coastguard Worker 
203*9880d681SAndroid Build Coastguard Worker /// Should we be emitting segmented stack stuff for the function
shouldSplitStack() const204*9880d681SAndroid Build Coastguard Worker bool MachineFunction::shouldSplitStack() const {
205*9880d681SAndroid Build Coastguard Worker   return getFunction()->hasFnAttribute("split-stack");
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker 
208*9880d681SAndroid Build Coastguard Worker /// This discards all of the MachineBasicBlock numbers and recomputes them.
209*9880d681SAndroid Build Coastguard Worker /// This guarantees that the MBB numbers are sequential, dense, and match the
210*9880d681SAndroid Build Coastguard Worker /// ordering of the blocks within the function.  If a specific MachineBasicBlock
211*9880d681SAndroid Build Coastguard Worker /// is specified, only that block and those after it are renumbered.
RenumberBlocks(MachineBasicBlock * MBB)212*9880d681SAndroid Build Coastguard Worker void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
213*9880d681SAndroid Build Coastguard Worker   if (empty()) { MBBNumbering.clear(); return; }
214*9880d681SAndroid Build Coastguard Worker   MachineFunction::iterator MBBI, E = end();
215*9880d681SAndroid Build Coastguard Worker   if (MBB == nullptr)
216*9880d681SAndroid Build Coastguard Worker     MBBI = begin();
217*9880d681SAndroid Build Coastguard Worker   else
218*9880d681SAndroid Build Coastguard Worker     MBBI = MBB->getIterator();
219*9880d681SAndroid Build Coastguard Worker 
220*9880d681SAndroid Build Coastguard Worker   // Figure out the block number this should have.
221*9880d681SAndroid Build Coastguard Worker   unsigned BlockNo = 0;
222*9880d681SAndroid Build Coastguard Worker   if (MBBI != begin())
223*9880d681SAndroid Build Coastguard Worker     BlockNo = std::prev(MBBI)->getNumber() + 1;
224*9880d681SAndroid Build Coastguard Worker 
225*9880d681SAndroid Build Coastguard Worker   for (; MBBI != E; ++MBBI, ++BlockNo) {
226*9880d681SAndroid Build Coastguard Worker     if (MBBI->getNumber() != (int)BlockNo) {
227*9880d681SAndroid Build Coastguard Worker       // Remove use of the old number.
228*9880d681SAndroid Build Coastguard Worker       if (MBBI->getNumber() != -1) {
229*9880d681SAndroid Build Coastguard Worker         assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
230*9880d681SAndroid Build Coastguard Worker                "MBB number mismatch!");
231*9880d681SAndroid Build Coastguard Worker         MBBNumbering[MBBI->getNumber()] = nullptr;
232*9880d681SAndroid Build Coastguard Worker       }
233*9880d681SAndroid Build Coastguard Worker 
234*9880d681SAndroid Build Coastguard Worker       // If BlockNo is already taken, set that block's number to -1.
235*9880d681SAndroid Build Coastguard Worker       if (MBBNumbering[BlockNo])
236*9880d681SAndroid Build Coastguard Worker         MBBNumbering[BlockNo]->setNumber(-1);
237*9880d681SAndroid Build Coastguard Worker 
238*9880d681SAndroid Build Coastguard Worker       MBBNumbering[BlockNo] = &*MBBI;
239*9880d681SAndroid Build Coastguard Worker       MBBI->setNumber(BlockNo);
240*9880d681SAndroid Build Coastguard Worker     }
241*9880d681SAndroid Build Coastguard Worker   }
242*9880d681SAndroid Build Coastguard Worker 
243*9880d681SAndroid Build Coastguard Worker   // Okay, all the blocks are renumbered.  If we have compactified the block
244*9880d681SAndroid Build Coastguard Worker   // numbering, shrink MBBNumbering now.
245*9880d681SAndroid Build Coastguard Worker   assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
246*9880d681SAndroid Build Coastguard Worker   MBBNumbering.resize(BlockNo);
247*9880d681SAndroid Build Coastguard Worker }
248*9880d681SAndroid Build Coastguard Worker 
249*9880d681SAndroid Build Coastguard Worker /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
CreateMachineInstr(const MCInstrDesc & MCID,const DebugLoc & DL,bool NoImp)250*9880d681SAndroid Build Coastguard Worker MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
251*9880d681SAndroid Build Coastguard Worker                                                   const DebugLoc &DL,
252*9880d681SAndroid Build Coastguard Worker                                                   bool NoImp) {
253*9880d681SAndroid Build Coastguard Worker   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
254*9880d681SAndroid Build Coastguard Worker     MachineInstr(*this, MCID, DL, NoImp);
255*9880d681SAndroid Build Coastguard Worker }
256*9880d681SAndroid Build Coastguard Worker 
257*9880d681SAndroid Build Coastguard Worker /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
258*9880d681SAndroid Build Coastguard Worker /// identical in all ways except the instruction has no parent, prev, or next.
259*9880d681SAndroid Build Coastguard Worker MachineInstr *
CloneMachineInstr(const MachineInstr * Orig)260*9880d681SAndroid Build Coastguard Worker MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
261*9880d681SAndroid Build Coastguard Worker   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
262*9880d681SAndroid Build Coastguard Worker              MachineInstr(*this, *Orig);
263*9880d681SAndroid Build Coastguard Worker }
264*9880d681SAndroid Build Coastguard Worker 
265*9880d681SAndroid Build Coastguard Worker /// Delete the given MachineInstr.
266*9880d681SAndroid Build Coastguard Worker ///
267*9880d681SAndroid Build Coastguard Worker /// This function also serves as the MachineInstr destructor - the real
268*9880d681SAndroid Build Coastguard Worker /// ~MachineInstr() destructor must be empty.
269*9880d681SAndroid Build Coastguard Worker void
DeleteMachineInstr(MachineInstr * MI)270*9880d681SAndroid Build Coastguard Worker MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
271*9880d681SAndroid Build Coastguard Worker   // Strip it for parts. The operand array and the MI object itself are
272*9880d681SAndroid Build Coastguard Worker   // independently recyclable.
273*9880d681SAndroid Build Coastguard Worker   if (MI->Operands)
274*9880d681SAndroid Build Coastguard Worker     deallocateOperandArray(MI->CapOperands, MI->Operands);
275*9880d681SAndroid Build Coastguard Worker   // Don't call ~MachineInstr() which must be trivial anyway because
276*9880d681SAndroid Build Coastguard Worker   // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
277*9880d681SAndroid Build Coastguard Worker   // destructors.
278*9880d681SAndroid Build Coastguard Worker   InstructionRecycler.Deallocate(Allocator, MI);
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker 
281*9880d681SAndroid Build Coastguard Worker /// Allocate a new MachineBasicBlock. Use this instead of
282*9880d681SAndroid Build Coastguard Worker /// `new MachineBasicBlock'.
283*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *
CreateMachineBasicBlock(const BasicBlock * bb)284*9880d681SAndroid Build Coastguard Worker MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
285*9880d681SAndroid Build Coastguard Worker   return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
286*9880d681SAndroid Build Coastguard Worker              MachineBasicBlock(*this, bb);
287*9880d681SAndroid Build Coastguard Worker }
288*9880d681SAndroid Build Coastguard Worker 
289*9880d681SAndroid Build Coastguard Worker /// Delete the given MachineBasicBlock.
290*9880d681SAndroid Build Coastguard Worker void
DeleteMachineBasicBlock(MachineBasicBlock * MBB)291*9880d681SAndroid Build Coastguard Worker MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
292*9880d681SAndroid Build Coastguard Worker   assert(MBB->getParent() == this && "MBB parent mismatch!");
293*9880d681SAndroid Build Coastguard Worker   MBB->~MachineBasicBlock();
294*9880d681SAndroid Build Coastguard Worker   BasicBlockRecycler.Deallocate(Allocator, MBB);
295*9880d681SAndroid Build Coastguard Worker }
296*9880d681SAndroid Build Coastguard Worker 
297*9880d681SAndroid Build Coastguard Worker MachineMemOperand *
getMachineMemOperand(MachinePointerInfo PtrInfo,unsigned f,uint64_t s,unsigned base_alignment,const AAMDNodes & AAInfo,const MDNode * Ranges)298*9880d681SAndroid Build Coastguard Worker MachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f,
299*9880d681SAndroid Build Coastguard Worker                                       uint64_t s, unsigned base_alignment,
300*9880d681SAndroid Build Coastguard Worker                                       const AAMDNodes &AAInfo,
301*9880d681SAndroid Build Coastguard Worker                                       const MDNode *Ranges) {
302*9880d681SAndroid Build Coastguard Worker   // FIXME: Get rid of this static_cast and make getMachineOperand take a
303*9880d681SAndroid Build Coastguard Worker   // MachineMemOperand::Flags param.
304*9880d681SAndroid Build Coastguard Worker   return new (Allocator)
305*9880d681SAndroid Build Coastguard Worker       MachineMemOperand(PtrInfo, static_cast<MachineMemOperand::Flags>(f), s,
306*9880d681SAndroid Build Coastguard Worker                         base_alignment, AAInfo, Ranges);
307*9880d681SAndroid Build Coastguard Worker }
308*9880d681SAndroid Build Coastguard Worker 
309*9880d681SAndroid Build Coastguard Worker MachineMemOperand *
getMachineMemOperand(const MachineMemOperand * MMO,int64_t Offset,uint64_t Size)310*9880d681SAndroid Build Coastguard Worker MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
311*9880d681SAndroid Build Coastguard Worker                                       int64_t Offset, uint64_t Size) {
312*9880d681SAndroid Build Coastguard Worker   if (MMO->getValue())
313*9880d681SAndroid Build Coastguard Worker     return new (Allocator)
314*9880d681SAndroid Build Coastguard Worker                MachineMemOperand(MachinePointerInfo(MMO->getValue(),
315*9880d681SAndroid Build Coastguard Worker                                                     MMO->getOffset()+Offset),
316*9880d681SAndroid Build Coastguard Worker                                  MMO->getFlags(), Size,
317*9880d681SAndroid Build Coastguard Worker                                  MMO->getBaseAlignment());
318*9880d681SAndroid Build Coastguard Worker   return new (Allocator)
319*9880d681SAndroid Build Coastguard Worker              MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
320*9880d681SAndroid Build Coastguard Worker                                                   MMO->getOffset()+Offset),
321*9880d681SAndroid Build Coastguard Worker                                MMO->getFlags(), Size,
322*9880d681SAndroid Build Coastguard Worker                                MMO->getBaseAlignment());
323*9880d681SAndroid Build Coastguard Worker }
324*9880d681SAndroid Build Coastguard Worker 
325*9880d681SAndroid Build Coastguard Worker MachineInstr::mmo_iterator
allocateMemRefsArray(unsigned long Num)326*9880d681SAndroid Build Coastguard Worker MachineFunction::allocateMemRefsArray(unsigned long Num) {
327*9880d681SAndroid Build Coastguard Worker   return Allocator.Allocate<MachineMemOperand *>(Num);
328*9880d681SAndroid Build Coastguard Worker }
329*9880d681SAndroid Build Coastguard Worker 
330*9880d681SAndroid Build Coastguard Worker std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
extractLoadMemRefs(MachineInstr::mmo_iterator Begin,MachineInstr::mmo_iterator End)331*9880d681SAndroid Build Coastguard Worker MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
332*9880d681SAndroid Build Coastguard Worker                                     MachineInstr::mmo_iterator End) {
333*9880d681SAndroid Build Coastguard Worker   // Count the number of load mem refs.
334*9880d681SAndroid Build Coastguard Worker   unsigned Num = 0;
335*9880d681SAndroid Build Coastguard Worker   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
336*9880d681SAndroid Build Coastguard Worker     if ((*I)->isLoad())
337*9880d681SAndroid Build Coastguard Worker       ++Num;
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker   // Allocate a new array and populate it with the load information.
340*9880d681SAndroid Build Coastguard Worker   MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
341*9880d681SAndroid Build Coastguard Worker   unsigned Index = 0;
342*9880d681SAndroid Build Coastguard Worker   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
343*9880d681SAndroid Build Coastguard Worker     if ((*I)->isLoad()) {
344*9880d681SAndroid Build Coastguard Worker       if (!(*I)->isStore())
345*9880d681SAndroid Build Coastguard Worker         // Reuse the MMO.
346*9880d681SAndroid Build Coastguard Worker         Result[Index] = *I;
347*9880d681SAndroid Build Coastguard Worker       else {
348*9880d681SAndroid Build Coastguard Worker         // Clone the MMO and unset the store flag.
349*9880d681SAndroid Build Coastguard Worker         MachineMemOperand *JustLoad =
350*9880d681SAndroid Build Coastguard Worker           getMachineMemOperand((*I)->getPointerInfo(),
351*9880d681SAndroid Build Coastguard Worker                                (*I)->getFlags() & ~MachineMemOperand::MOStore,
352*9880d681SAndroid Build Coastguard Worker                                (*I)->getSize(), (*I)->getBaseAlignment(),
353*9880d681SAndroid Build Coastguard Worker                                (*I)->getAAInfo());
354*9880d681SAndroid Build Coastguard Worker         Result[Index] = JustLoad;
355*9880d681SAndroid Build Coastguard Worker       }
356*9880d681SAndroid Build Coastguard Worker       ++Index;
357*9880d681SAndroid Build Coastguard Worker     }
358*9880d681SAndroid Build Coastguard Worker   }
359*9880d681SAndroid Build Coastguard Worker   return std::make_pair(Result, Result + Num);
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker 
362*9880d681SAndroid Build Coastguard Worker std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
extractStoreMemRefs(MachineInstr::mmo_iterator Begin,MachineInstr::mmo_iterator End)363*9880d681SAndroid Build Coastguard Worker MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
364*9880d681SAndroid Build Coastguard Worker                                      MachineInstr::mmo_iterator End) {
365*9880d681SAndroid Build Coastguard Worker   // Count the number of load mem refs.
366*9880d681SAndroid Build Coastguard Worker   unsigned Num = 0;
367*9880d681SAndroid Build Coastguard Worker   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
368*9880d681SAndroid Build Coastguard Worker     if ((*I)->isStore())
369*9880d681SAndroid Build Coastguard Worker       ++Num;
370*9880d681SAndroid Build Coastguard Worker 
371*9880d681SAndroid Build Coastguard Worker   // Allocate a new array and populate it with the store information.
372*9880d681SAndroid Build Coastguard Worker   MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
373*9880d681SAndroid Build Coastguard Worker   unsigned Index = 0;
374*9880d681SAndroid Build Coastguard Worker   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
375*9880d681SAndroid Build Coastguard Worker     if ((*I)->isStore()) {
376*9880d681SAndroid Build Coastguard Worker       if (!(*I)->isLoad())
377*9880d681SAndroid Build Coastguard Worker         // Reuse the MMO.
378*9880d681SAndroid Build Coastguard Worker         Result[Index] = *I;
379*9880d681SAndroid Build Coastguard Worker       else {
380*9880d681SAndroid Build Coastguard Worker         // Clone the MMO and unset the load flag.
381*9880d681SAndroid Build Coastguard Worker         MachineMemOperand *JustStore =
382*9880d681SAndroid Build Coastguard Worker           getMachineMemOperand((*I)->getPointerInfo(),
383*9880d681SAndroid Build Coastguard Worker                                (*I)->getFlags() & ~MachineMemOperand::MOLoad,
384*9880d681SAndroid Build Coastguard Worker                                (*I)->getSize(), (*I)->getBaseAlignment(),
385*9880d681SAndroid Build Coastguard Worker                                (*I)->getAAInfo());
386*9880d681SAndroid Build Coastguard Worker         Result[Index] = JustStore;
387*9880d681SAndroid Build Coastguard Worker       }
388*9880d681SAndroid Build Coastguard Worker       ++Index;
389*9880d681SAndroid Build Coastguard Worker     }
390*9880d681SAndroid Build Coastguard Worker   }
391*9880d681SAndroid Build Coastguard Worker   return std::make_pair(Result, Result + Num);
392*9880d681SAndroid Build Coastguard Worker }
393*9880d681SAndroid Build Coastguard Worker 
createExternalSymbolName(StringRef Name)394*9880d681SAndroid Build Coastguard Worker const char *MachineFunction::createExternalSymbolName(StringRef Name) {
395*9880d681SAndroid Build Coastguard Worker   char *Dest = Allocator.Allocate<char>(Name.size() + 1);
396*9880d681SAndroid Build Coastguard Worker   std::copy(Name.begin(), Name.end(), Dest);
397*9880d681SAndroid Build Coastguard Worker   Dest[Name.size()] = 0;
398*9880d681SAndroid Build Coastguard Worker   return Dest;
399*9880d681SAndroid Build Coastguard Worker }
400*9880d681SAndroid Build Coastguard Worker 
401*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const402*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void MachineFunction::dump() const {
403*9880d681SAndroid Build Coastguard Worker   print(dbgs());
404*9880d681SAndroid Build Coastguard Worker }
405*9880d681SAndroid Build Coastguard Worker #endif
406*9880d681SAndroid Build Coastguard Worker 
getName() const407*9880d681SAndroid Build Coastguard Worker StringRef MachineFunction::getName() const {
408*9880d681SAndroid Build Coastguard Worker   assert(getFunction() && "No function!");
409*9880d681SAndroid Build Coastguard Worker   return getFunction()->getName();
410*9880d681SAndroid Build Coastguard Worker }
411*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & OS,const SlotIndexes * Indexes) const412*9880d681SAndroid Build Coastguard Worker void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
413*9880d681SAndroid Build Coastguard Worker   OS << "# Machine code for function " << getName() << ": ";
414*9880d681SAndroid Build Coastguard Worker   OS << "Properties: <";
415*9880d681SAndroid Build Coastguard Worker   getProperties().print(OS);
416*9880d681SAndroid Build Coastguard Worker   OS << ">\n";
417*9880d681SAndroid Build Coastguard Worker 
418*9880d681SAndroid Build Coastguard Worker   // Print Frame Information
419*9880d681SAndroid Build Coastguard Worker   FrameInfo->print(*this, OS);
420*9880d681SAndroid Build Coastguard Worker 
421*9880d681SAndroid Build Coastguard Worker   // Print JumpTable Information
422*9880d681SAndroid Build Coastguard Worker   if (JumpTableInfo)
423*9880d681SAndroid Build Coastguard Worker     JumpTableInfo->print(OS);
424*9880d681SAndroid Build Coastguard Worker 
425*9880d681SAndroid Build Coastguard Worker   // Print Constant Pool
426*9880d681SAndroid Build Coastguard Worker   ConstantPool->print(OS);
427*9880d681SAndroid Build Coastguard Worker 
428*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
429*9880d681SAndroid Build Coastguard Worker 
430*9880d681SAndroid Build Coastguard Worker   if (RegInfo && !RegInfo->livein_empty()) {
431*9880d681SAndroid Build Coastguard Worker     OS << "Function Live Ins: ";
432*9880d681SAndroid Build Coastguard Worker     for (MachineRegisterInfo::livein_iterator
433*9880d681SAndroid Build Coastguard Worker          I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
434*9880d681SAndroid Build Coastguard Worker       OS << PrintReg(I->first, TRI);
435*9880d681SAndroid Build Coastguard Worker       if (I->second)
436*9880d681SAndroid Build Coastguard Worker         OS << " in " << PrintReg(I->second, TRI);
437*9880d681SAndroid Build Coastguard Worker       if (std::next(I) != E)
438*9880d681SAndroid Build Coastguard Worker         OS << ", ";
439*9880d681SAndroid Build Coastguard Worker     }
440*9880d681SAndroid Build Coastguard Worker     OS << '\n';
441*9880d681SAndroid Build Coastguard Worker   }
442*9880d681SAndroid Build Coastguard Worker 
443*9880d681SAndroid Build Coastguard Worker   ModuleSlotTracker MST(getFunction()->getParent());
444*9880d681SAndroid Build Coastguard Worker   MST.incorporateFunction(*getFunction());
445*9880d681SAndroid Build Coastguard Worker   for (const auto &BB : *this) {
446*9880d681SAndroid Build Coastguard Worker     OS << '\n';
447*9880d681SAndroid Build Coastguard Worker     BB.print(OS, MST, Indexes);
448*9880d681SAndroid Build Coastguard Worker   }
449*9880d681SAndroid Build Coastguard Worker 
450*9880d681SAndroid Build Coastguard Worker   OS << "\n# End machine code for function " << getName() << ".\n\n";
451*9880d681SAndroid Build Coastguard Worker }
452*9880d681SAndroid Build Coastguard Worker 
453*9880d681SAndroid Build Coastguard Worker namespace llvm {
454*9880d681SAndroid Build Coastguard Worker   template<>
455*9880d681SAndroid Build Coastguard Worker   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
456*9880d681SAndroid Build Coastguard Worker 
DOTGraphTraitsllvm::DOTGraphTraits457*9880d681SAndroid Build Coastguard Worker   DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
458*9880d681SAndroid Build Coastguard Worker 
getGraphNamellvm::DOTGraphTraits459*9880d681SAndroid Build Coastguard Worker     static std::string getGraphName(const MachineFunction *F) {
460*9880d681SAndroid Build Coastguard Worker       return ("CFG for '" + F->getName() + "' function").str();
461*9880d681SAndroid Build Coastguard Worker     }
462*9880d681SAndroid Build Coastguard Worker 
getNodeLabelllvm::DOTGraphTraits463*9880d681SAndroid Build Coastguard Worker     std::string getNodeLabel(const MachineBasicBlock *Node,
464*9880d681SAndroid Build Coastguard Worker                              const MachineFunction *Graph) {
465*9880d681SAndroid Build Coastguard Worker       std::string OutStr;
466*9880d681SAndroid Build Coastguard Worker       {
467*9880d681SAndroid Build Coastguard Worker         raw_string_ostream OSS(OutStr);
468*9880d681SAndroid Build Coastguard Worker 
469*9880d681SAndroid Build Coastguard Worker         if (isSimple()) {
470*9880d681SAndroid Build Coastguard Worker           OSS << "BB#" << Node->getNumber();
471*9880d681SAndroid Build Coastguard Worker           if (const BasicBlock *BB = Node->getBasicBlock())
472*9880d681SAndroid Build Coastguard Worker             OSS << ": " << BB->getName();
473*9880d681SAndroid Build Coastguard Worker         } else
474*9880d681SAndroid Build Coastguard Worker           Node->print(OSS);
475*9880d681SAndroid Build Coastguard Worker       }
476*9880d681SAndroid Build Coastguard Worker 
477*9880d681SAndroid Build Coastguard Worker       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
478*9880d681SAndroid Build Coastguard Worker 
479*9880d681SAndroid Build Coastguard Worker       // Process string output to make it nicer...
480*9880d681SAndroid Build Coastguard Worker       for (unsigned i = 0; i != OutStr.length(); ++i)
481*9880d681SAndroid Build Coastguard Worker         if (OutStr[i] == '\n') {                            // Left justify
482*9880d681SAndroid Build Coastguard Worker           OutStr[i] = '\\';
483*9880d681SAndroid Build Coastguard Worker           OutStr.insert(OutStr.begin()+i+1, 'l');
484*9880d681SAndroid Build Coastguard Worker         }
485*9880d681SAndroid Build Coastguard Worker       return OutStr;
486*9880d681SAndroid Build Coastguard Worker     }
487*9880d681SAndroid Build Coastguard Worker   };
488*9880d681SAndroid Build Coastguard Worker }
489*9880d681SAndroid Build Coastguard Worker 
viewCFG() const490*9880d681SAndroid Build Coastguard Worker void MachineFunction::viewCFG() const
491*9880d681SAndroid Build Coastguard Worker {
492*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
493*9880d681SAndroid Build Coastguard Worker   ViewGraph(this, "mf" + getName());
494*9880d681SAndroid Build Coastguard Worker #else
495*9880d681SAndroid Build Coastguard Worker   errs() << "MachineFunction::viewCFG is only available in debug builds on "
496*9880d681SAndroid Build Coastguard Worker          << "systems with Graphviz or gv!\n";
497*9880d681SAndroid Build Coastguard Worker #endif // NDEBUG
498*9880d681SAndroid Build Coastguard Worker }
499*9880d681SAndroid Build Coastguard Worker 
viewCFGOnly() const500*9880d681SAndroid Build Coastguard Worker void MachineFunction::viewCFGOnly() const
501*9880d681SAndroid Build Coastguard Worker {
502*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
503*9880d681SAndroid Build Coastguard Worker   ViewGraph(this, "mf" + getName(), true);
504*9880d681SAndroid Build Coastguard Worker #else
505*9880d681SAndroid Build Coastguard Worker   errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
506*9880d681SAndroid Build Coastguard Worker          << "systems with Graphviz or gv!\n";
507*9880d681SAndroid Build Coastguard Worker #endif // NDEBUG
508*9880d681SAndroid Build Coastguard Worker }
509*9880d681SAndroid Build Coastguard Worker 
510*9880d681SAndroid Build Coastguard Worker /// Add the specified physical register as a live-in value and
511*9880d681SAndroid Build Coastguard Worker /// create a corresponding virtual register for it.
addLiveIn(unsigned PReg,const TargetRegisterClass * RC)512*9880d681SAndroid Build Coastguard Worker unsigned MachineFunction::addLiveIn(unsigned PReg,
513*9880d681SAndroid Build Coastguard Worker                                     const TargetRegisterClass *RC) {
514*9880d681SAndroid Build Coastguard Worker   MachineRegisterInfo &MRI = getRegInfo();
515*9880d681SAndroid Build Coastguard Worker   unsigned VReg = MRI.getLiveInVirtReg(PReg);
516*9880d681SAndroid Build Coastguard Worker   if (VReg) {
517*9880d681SAndroid Build Coastguard Worker     const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
518*9880d681SAndroid Build Coastguard Worker     (void)VRegRC;
519*9880d681SAndroid Build Coastguard Worker     // A physical register can be added several times.
520*9880d681SAndroid Build Coastguard Worker     // Between two calls, the register class of the related virtual register
521*9880d681SAndroid Build Coastguard Worker     // may have been constrained to match some operation constraints.
522*9880d681SAndroid Build Coastguard Worker     // In that case, check that the current register class includes the
523*9880d681SAndroid Build Coastguard Worker     // physical register and is a sub class of the specified RC.
524*9880d681SAndroid Build Coastguard Worker     assert((VRegRC == RC || (VRegRC->contains(PReg) &&
525*9880d681SAndroid Build Coastguard Worker                              RC->hasSubClassEq(VRegRC))) &&
526*9880d681SAndroid Build Coastguard Worker             "Register class mismatch!");
527*9880d681SAndroid Build Coastguard Worker     return VReg;
528*9880d681SAndroid Build Coastguard Worker   }
529*9880d681SAndroid Build Coastguard Worker   VReg = MRI.createVirtualRegister(RC);
530*9880d681SAndroid Build Coastguard Worker   MRI.addLiveIn(PReg, VReg);
531*9880d681SAndroid Build Coastguard Worker   return VReg;
532*9880d681SAndroid Build Coastguard Worker }
533*9880d681SAndroid Build Coastguard Worker 
534*9880d681SAndroid Build Coastguard Worker /// Return the MCSymbol for the specified non-empty jump table.
535*9880d681SAndroid Build Coastguard Worker /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
536*9880d681SAndroid Build Coastguard Worker /// normal 'L' label is returned.
getJTISymbol(unsigned JTI,MCContext & Ctx,bool isLinkerPrivate) const537*9880d681SAndroid Build Coastguard Worker MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
538*9880d681SAndroid Build Coastguard Worker                                         bool isLinkerPrivate) const {
539*9880d681SAndroid Build Coastguard Worker   const DataLayout &DL = getDataLayout();
540*9880d681SAndroid Build Coastguard Worker   assert(JumpTableInfo && "No jump tables");
541*9880d681SAndroid Build Coastguard Worker   assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
542*9880d681SAndroid Build Coastguard Worker 
543*9880d681SAndroid Build Coastguard Worker   const char *Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
544*9880d681SAndroid Build Coastguard Worker                                        : DL.getPrivateGlobalPrefix();
545*9880d681SAndroid Build Coastguard Worker   SmallString<60> Name;
546*9880d681SAndroid Build Coastguard Worker   raw_svector_ostream(Name)
547*9880d681SAndroid Build Coastguard Worker     << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
548*9880d681SAndroid Build Coastguard Worker   return Ctx.getOrCreateSymbol(Name);
549*9880d681SAndroid Build Coastguard Worker }
550*9880d681SAndroid Build Coastguard Worker 
551*9880d681SAndroid Build Coastguard Worker /// Return a function-local symbol to represent the PIC base.
getPICBaseSymbol() const552*9880d681SAndroid Build Coastguard Worker MCSymbol *MachineFunction::getPICBaseSymbol() const {
553*9880d681SAndroid Build Coastguard Worker   const DataLayout &DL = getDataLayout();
554*9880d681SAndroid Build Coastguard Worker   return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
555*9880d681SAndroid Build Coastguard Worker                                Twine(getFunctionNumber()) + "$pb");
556*9880d681SAndroid Build Coastguard Worker }
557*9880d681SAndroid Build Coastguard Worker 
558*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
559*9880d681SAndroid Build Coastguard Worker //  MachineFrameInfo implementation
560*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
561*9880d681SAndroid Build Coastguard Worker 
562*9880d681SAndroid Build Coastguard Worker /// Make sure the function is at least Align bytes aligned.
ensureMaxAlignment(unsigned Align)563*9880d681SAndroid Build Coastguard Worker void MachineFrameInfo::ensureMaxAlignment(unsigned Align) {
564*9880d681SAndroid Build Coastguard Worker   if (!StackRealignable)
565*9880d681SAndroid Build Coastguard Worker     assert(Align <= StackAlignment &&
566*9880d681SAndroid Build Coastguard Worker            "For targets without stack realignment, Align is out of limit!");
567*9880d681SAndroid Build Coastguard Worker   if (MaxAlignment < Align) MaxAlignment = Align;
568*9880d681SAndroid Build Coastguard Worker }
569*9880d681SAndroid Build Coastguard Worker 
570*9880d681SAndroid Build Coastguard Worker /// Clamp the alignment if requested and emit a warning.
clampStackAlignment(bool ShouldClamp,unsigned Align,unsigned StackAlign)571*9880d681SAndroid Build Coastguard Worker static inline unsigned clampStackAlignment(bool ShouldClamp, unsigned Align,
572*9880d681SAndroid Build Coastguard Worker                                            unsigned StackAlign) {
573*9880d681SAndroid Build Coastguard Worker   if (!ShouldClamp || Align <= StackAlign)
574*9880d681SAndroid Build Coastguard Worker     return Align;
575*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Warning: requested alignment " << Align
576*9880d681SAndroid Build Coastguard Worker                << " exceeds the stack alignment " << StackAlign
577*9880d681SAndroid Build Coastguard Worker                << " when stack realignment is off" << '\n');
578*9880d681SAndroid Build Coastguard Worker   return StackAlign;
579*9880d681SAndroid Build Coastguard Worker }
580*9880d681SAndroid Build Coastguard Worker 
581*9880d681SAndroid Build Coastguard Worker /// Create a new statically sized stack object, returning a nonnegative
582*9880d681SAndroid Build Coastguard Worker /// identifier to represent it.
CreateStackObject(uint64_t Size,unsigned Alignment,bool isSS,const AllocaInst * Alloca)583*9880d681SAndroid Build Coastguard Worker int MachineFrameInfo::CreateStackObject(uint64_t Size, unsigned Alignment,
584*9880d681SAndroid Build Coastguard Worker                       bool isSS, const AllocaInst *Alloca) {
585*9880d681SAndroid Build Coastguard Worker   assert(Size != 0 && "Cannot allocate zero size stack objects!");
586*9880d681SAndroid Build Coastguard Worker   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
587*9880d681SAndroid Build Coastguard Worker   Objects.push_back(StackObject(Size, Alignment, 0, false, isSS, Alloca,
588*9880d681SAndroid Build Coastguard Worker                                 !isSS));
589*9880d681SAndroid Build Coastguard Worker   int Index = (int)Objects.size() - NumFixedObjects - 1;
590*9880d681SAndroid Build Coastguard Worker   assert(Index >= 0 && "Bad frame index!");
591*9880d681SAndroid Build Coastguard Worker   ensureMaxAlignment(Alignment);
592*9880d681SAndroid Build Coastguard Worker   return Index;
593*9880d681SAndroid Build Coastguard Worker }
594*9880d681SAndroid Build Coastguard Worker 
595*9880d681SAndroid Build Coastguard Worker /// Create a new statically sized stack object that represents a spill slot,
596*9880d681SAndroid Build Coastguard Worker /// returning a nonnegative identifier to represent it.
CreateSpillStackObject(uint64_t Size,unsigned Alignment)597*9880d681SAndroid Build Coastguard Worker int MachineFrameInfo::CreateSpillStackObject(uint64_t Size,
598*9880d681SAndroid Build Coastguard Worker                                              unsigned Alignment) {
599*9880d681SAndroid Build Coastguard Worker   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
600*9880d681SAndroid Build Coastguard Worker   CreateStackObject(Size, Alignment, true);
601*9880d681SAndroid Build Coastguard Worker   int Index = (int)Objects.size() - NumFixedObjects - 1;
602*9880d681SAndroid Build Coastguard Worker   ensureMaxAlignment(Alignment);
603*9880d681SAndroid Build Coastguard Worker   return Index;
604*9880d681SAndroid Build Coastguard Worker }
605*9880d681SAndroid Build Coastguard Worker 
606*9880d681SAndroid Build Coastguard Worker /// Notify the MachineFrameInfo object that a variable sized object has been
607*9880d681SAndroid Build Coastguard Worker /// created. This must be created whenever a variable sized object is created,
608*9880d681SAndroid Build Coastguard Worker /// whether or not the index returned is actually used.
CreateVariableSizedObject(unsigned Alignment,const AllocaInst * Alloca)609*9880d681SAndroid Build Coastguard Worker int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
610*9880d681SAndroid Build Coastguard Worker                                                 const AllocaInst *Alloca) {
611*9880d681SAndroid Build Coastguard Worker   HasVarSizedObjects = true;
612*9880d681SAndroid Build Coastguard Worker   Alignment = clampStackAlignment(!StackRealignable, Alignment, StackAlignment);
613*9880d681SAndroid Build Coastguard Worker   Objects.push_back(StackObject(0, Alignment, 0, false, false, Alloca, true));
614*9880d681SAndroid Build Coastguard Worker   ensureMaxAlignment(Alignment);
615*9880d681SAndroid Build Coastguard Worker   return (int)Objects.size()-NumFixedObjects-1;
616*9880d681SAndroid Build Coastguard Worker }
617*9880d681SAndroid Build Coastguard Worker 
618*9880d681SAndroid Build Coastguard Worker /// Create a new object at a fixed location on the stack.
619*9880d681SAndroid Build Coastguard Worker /// All fixed objects should be created before other objects are created for
620*9880d681SAndroid Build Coastguard Worker /// efficiency. By default, fixed objects are immutable. This returns an
621*9880d681SAndroid Build Coastguard Worker /// index with a negative value.
CreateFixedObject(uint64_t Size,int64_t SPOffset,bool Immutable,bool isAliased)622*9880d681SAndroid Build Coastguard Worker int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
623*9880d681SAndroid Build Coastguard Worker                                         bool Immutable, bool isAliased) {
624*9880d681SAndroid Build Coastguard Worker   assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
625*9880d681SAndroid Build Coastguard Worker   // The alignment of the frame index can be determined from its offset from
626*9880d681SAndroid Build Coastguard Worker   // the incoming frame position.  If the frame object is at offset 32 and
627*9880d681SAndroid Build Coastguard Worker   // the stack is guaranteed to be 16-byte aligned, then we know that the
628*9880d681SAndroid Build Coastguard Worker   // object is 16-byte aligned. Note that unlike the non-fixed case, if the
629*9880d681SAndroid Build Coastguard Worker   // stack needs realignment, we can't assume that the stack will in fact be
630*9880d681SAndroid Build Coastguard Worker   // aligned.
631*9880d681SAndroid Build Coastguard Worker   unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
632*9880d681SAndroid Build Coastguard Worker   Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
633*9880d681SAndroid Build Coastguard Worker   Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable,
634*9880d681SAndroid Build Coastguard Worker                                               /*isSS*/   false,
635*9880d681SAndroid Build Coastguard Worker                                               /*Alloca*/ nullptr, isAliased));
636*9880d681SAndroid Build Coastguard Worker   return -++NumFixedObjects;
637*9880d681SAndroid Build Coastguard Worker }
638*9880d681SAndroid Build Coastguard Worker 
639*9880d681SAndroid Build Coastguard Worker /// Create a spill slot at a fixed location on the stack.
640*9880d681SAndroid Build Coastguard Worker /// Returns an index with a negative value.
CreateFixedSpillStackObject(uint64_t Size,int64_t SPOffset)641*9880d681SAndroid Build Coastguard Worker int MachineFrameInfo::CreateFixedSpillStackObject(uint64_t Size,
642*9880d681SAndroid Build Coastguard Worker                                                   int64_t SPOffset) {
643*9880d681SAndroid Build Coastguard Worker   unsigned Align = MinAlign(SPOffset, ForcedRealign ? 1 : StackAlignment);
644*9880d681SAndroid Build Coastguard Worker   Align = clampStackAlignment(!StackRealignable, Align, StackAlignment);
645*9880d681SAndroid Build Coastguard Worker   Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset,
646*9880d681SAndroid Build Coastguard Worker                                               /*Immutable*/ true,
647*9880d681SAndroid Build Coastguard Worker                                               /*isSS*/ true,
648*9880d681SAndroid Build Coastguard Worker                                               /*Alloca*/ nullptr,
649*9880d681SAndroid Build Coastguard Worker                                               /*isAliased*/ false));
650*9880d681SAndroid Build Coastguard Worker   return -++NumFixedObjects;
651*9880d681SAndroid Build Coastguard Worker }
652*9880d681SAndroid Build Coastguard Worker 
getPristineRegs(const MachineFunction & MF) const653*9880d681SAndroid Build Coastguard Worker BitVector MachineFrameInfo::getPristineRegs(const MachineFunction &MF) const {
654*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
655*9880d681SAndroid Build Coastguard Worker   BitVector BV(TRI->getNumRegs());
656*9880d681SAndroid Build Coastguard Worker 
657*9880d681SAndroid Build Coastguard Worker   // Before CSI is calculated, no registers are considered pristine. They can be
658*9880d681SAndroid Build Coastguard Worker   // freely used and PEI will make sure they are saved.
659*9880d681SAndroid Build Coastguard Worker   if (!isCalleeSavedInfoValid())
660*9880d681SAndroid Build Coastguard Worker     return BV;
661*9880d681SAndroid Build Coastguard Worker 
662*9880d681SAndroid Build Coastguard Worker   for (const MCPhysReg *CSR = TRI->getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
663*9880d681SAndroid Build Coastguard Worker     BV.set(*CSR);
664*9880d681SAndroid Build Coastguard Worker 
665*9880d681SAndroid Build Coastguard Worker   // Saved CSRs are not pristine.
666*9880d681SAndroid Build Coastguard Worker   for (auto &I : getCalleeSavedInfo())
667*9880d681SAndroid Build Coastguard Worker     for (MCSubRegIterator S(I.getReg(), TRI, true); S.isValid(); ++S)
668*9880d681SAndroid Build Coastguard Worker       BV.reset(*S);
669*9880d681SAndroid Build Coastguard Worker 
670*9880d681SAndroid Build Coastguard Worker   return BV;
671*9880d681SAndroid Build Coastguard Worker }
672*9880d681SAndroid Build Coastguard Worker 
estimateStackSize(const MachineFunction & MF) const673*9880d681SAndroid Build Coastguard Worker unsigned MachineFrameInfo::estimateStackSize(const MachineFunction &MF) const {
674*9880d681SAndroid Build Coastguard Worker   const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
675*9880d681SAndroid Build Coastguard Worker   const TargetRegisterInfo *RegInfo = MF.getSubtarget().getRegisterInfo();
676*9880d681SAndroid Build Coastguard Worker   unsigned MaxAlign = getMaxAlignment();
677*9880d681SAndroid Build Coastguard Worker   int Offset = 0;
678*9880d681SAndroid Build Coastguard Worker 
679*9880d681SAndroid Build Coastguard Worker   // This code is very, very similar to PEI::calculateFrameObjectOffsets().
680*9880d681SAndroid Build Coastguard Worker   // It really should be refactored to share code. Until then, changes
681*9880d681SAndroid Build Coastguard Worker   // should keep in mind that there's tight coupling between the two.
682*9880d681SAndroid Build Coastguard Worker 
683*9880d681SAndroid Build Coastguard Worker   for (int i = getObjectIndexBegin(); i != 0; ++i) {
684*9880d681SAndroid Build Coastguard Worker     int FixedOff = -getObjectOffset(i);
685*9880d681SAndroid Build Coastguard Worker     if (FixedOff > Offset) Offset = FixedOff;
686*9880d681SAndroid Build Coastguard Worker   }
687*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = getObjectIndexEnd(); i != e; ++i) {
688*9880d681SAndroid Build Coastguard Worker     if (isDeadObjectIndex(i))
689*9880d681SAndroid Build Coastguard Worker       continue;
690*9880d681SAndroid Build Coastguard Worker     Offset += getObjectSize(i);
691*9880d681SAndroid Build Coastguard Worker     unsigned Align = getObjectAlignment(i);
692*9880d681SAndroid Build Coastguard Worker     // Adjust to alignment boundary
693*9880d681SAndroid Build Coastguard Worker     Offset = (Offset+Align-1)/Align*Align;
694*9880d681SAndroid Build Coastguard Worker 
695*9880d681SAndroid Build Coastguard Worker     MaxAlign = std::max(Align, MaxAlign);
696*9880d681SAndroid Build Coastguard Worker   }
697*9880d681SAndroid Build Coastguard Worker 
698*9880d681SAndroid Build Coastguard Worker   if (adjustsStack() && TFI->hasReservedCallFrame(MF))
699*9880d681SAndroid Build Coastguard Worker     Offset += getMaxCallFrameSize();
700*9880d681SAndroid Build Coastguard Worker 
701*9880d681SAndroid Build Coastguard Worker   // Round up the size to a multiple of the alignment.  If the function has
702*9880d681SAndroid Build Coastguard Worker   // any calls or alloca's, align to the target's StackAlignment value to
703*9880d681SAndroid Build Coastguard Worker   // ensure that the callee's frame or the alloca data is suitably aligned;
704*9880d681SAndroid Build Coastguard Worker   // otherwise, for leaf functions, align to the TransientStackAlignment
705*9880d681SAndroid Build Coastguard Worker   // value.
706*9880d681SAndroid Build Coastguard Worker   unsigned StackAlign;
707*9880d681SAndroid Build Coastguard Worker   if (adjustsStack() || hasVarSizedObjects() ||
708*9880d681SAndroid Build Coastguard Worker       (RegInfo->needsStackRealignment(MF) && getObjectIndexEnd() != 0))
709*9880d681SAndroid Build Coastguard Worker     StackAlign = TFI->getStackAlignment();
710*9880d681SAndroid Build Coastguard Worker   else
711*9880d681SAndroid Build Coastguard Worker     StackAlign = TFI->getTransientStackAlignment();
712*9880d681SAndroid Build Coastguard Worker 
713*9880d681SAndroid Build Coastguard Worker   // If the frame pointer is eliminated, all frame offsets will be relative to
714*9880d681SAndroid Build Coastguard Worker   // SP not FP. Align to MaxAlign so this works.
715*9880d681SAndroid Build Coastguard Worker   StackAlign = std::max(StackAlign, MaxAlign);
716*9880d681SAndroid Build Coastguard Worker   unsigned AlignMask = StackAlign - 1;
717*9880d681SAndroid Build Coastguard Worker   Offset = (Offset + AlignMask) & ~uint64_t(AlignMask);
718*9880d681SAndroid Build Coastguard Worker 
719*9880d681SAndroid Build Coastguard Worker   return (unsigned)Offset;
720*9880d681SAndroid Build Coastguard Worker }
721*9880d681SAndroid Build Coastguard Worker 
print(const MachineFunction & MF,raw_ostream & OS) const722*9880d681SAndroid Build Coastguard Worker void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
723*9880d681SAndroid Build Coastguard Worker   if (Objects.empty()) return;
724*9880d681SAndroid Build Coastguard Worker 
725*9880d681SAndroid Build Coastguard Worker   const TargetFrameLowering *FI = MF.getSubtarget().getFrameLowering();
726*9880d681SAndroid Build Coastguard Worker   int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
727*9880d681SAndroid Build Coastguard Worker 
728*9880d681SAndroid Build Coastguard Worker   OS << "Frame Objects:\n";
729*9880d681SAndroid Build Coastguard Worker 
730*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
731*9880d681SAndroid Build Coastguard Worker     const StackObject &SO = Objects[i];
732*9880d681SAndroid Build Coastguard Worker     OS << "  fi#" << (int)(i-NumFixedObjects) << ": ";
733*9880d681SAndroid Build Coastguard Worker     if (SO.Size == ~0ULL) {
734*9880d681SAndroid Build Coastguard Worker       OS << "dead\n";
735*9880d681SAndroid Build Coastguard Worker       continue;
736*9880d681SAndroid Build Coastguard Worker     }
737*9880d681SAndroid Build Coastguard Worker     if (SO.Size == 0)
738*9880d681SAndroid Build Coastguard Worker       OS << "variable sized";
739*9880d681SAndroid Build Coastguard Worker     else
740*9880d681SAndroid Build Coastguard Worker       OS << "size=" << SO.Size;
741*9880d681SAndroid Build Coastguard Worker     OS << ", align=" << SO.Alignment;
742*9880d681SAndroid Build Coastguard Worker 
743*9880d681SAndroid Build Coastguard Worker     if (i < NumFixedObjects)
744*9880d681SAndroid Build Coastguard Worker       OS << ", fixed";
745*9880d681SAndroid Build Coastguard Worker     if (i < NumFixedObjects || SO.SPOffset != -1) {
746*9880d681SAndroid Build Coastguard Worker       int64_t Off = SO.SPOffset - ValOffset;
747*9880d681SAndroid Build Coastguard Worker       OS << ", at location [SP";
748*9880d681SAndroid Build Coastguard Worker       if (Off > 0)
749*9880d681SAndroid Build Coastguard Worker         OS << "+" << Off;
750*9880d681SAndroid Build Coastguard Worker       else if (Off < 0)
751*9880d681SAndroid Build Coastguard Worker         OS << Off;
752*9880d681SAndroid Build Coastguard Worker       OS << "]";
753*9880d681SAndroid Build Coastguard Worker     }
754*9880d681SAndroid Build Coastguard Worker     OS << "\n";
755*9880d681SAndroid Build Coastguard Worker   }
756*9880d681SAndroid Build Coastguard Worker }
757*9880d681SAndroid Build Coastguard Worker 
758*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump(const MachineFunction & MF) const759*9880d681SAndroid Build Coastguard Worker void MachineFrameInfo::dump(const MachineFunction &MF) const {
760*9880d681SAndroid Build Coastguard Worker   print(MF, dbgs());
761*9880d681SAndroid Build Coastguard Worker }
762*9880d681SAndroid Build Coastguard Worker #endif
763*9880d681SAndroid Build Coastguard Worker 
764*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
765*9880d681SAndroid Build Coastguard Worker //  MachineJumpTableInfo implementation
766*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
767*9880d681SAndroid Build Coastguard Worker 
768*9880d681SAndroid Build Coastguard Worker /// Return the size of each entry in the jump table.
getEntrySize(const DataLayout & TD) const769*9880d681SAndroid Build Coastguard Worker unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
770*9880d681SAndroid Build Coastguard Worker   // The size of a jump table entry is 4 bytes unless the entry is just the
771*9880d681SAndroid Build Coastguard Worker   // address of a block, in which case it is the pointer size.
772*9880d681SAndroid Build Coastguard Worker   switch (getEntryKind()) {
773*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_BlockAddress:
774*9880d681SAndroid Build Coastguard Worker     return TD.getPointerSize();
775*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
776*9880d681SAndroid Build Coastguard Worker     return 8;
777*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
778*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_LabelDifference32:
779*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_Custom32:
780*9880d681SAndroid Build Coastguard Worker     return 4;
781*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_Inline:
782*9880d681SAndroid Build Coastguard Worker     return 0;
783*9880d681SAndroid Build Coastguard Worker   }
784*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Unknown jump table encoding!");
785*9880d681SAndroid Build Coastguard Worker }
786*9880d681SAndroid Build Coastguard Worker 
787*9880d681SAndroid Build Coastguard Worker /// Return the alignment of each entry in the jump table.
getEntryAlignment(const DataLayout & TD) const788*9880d681SAndroid Build Coastguard Worker unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
789*9880d681SAndroid Build Coastguard Worker   // The alignment of a jump table entry is the alignment of int32 unless the
790*9880d681SAndroid Build Coastguard Worker   // entry is just the address of a block, in which case it is the pointer
791*9880d681SAndroid Build Coastguard Worker   // alignment.
792*9880d681SAndroid Build Coastguard Worker   switch (getEntryKind()) {
793*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_BlockAddress:
794*9880d681SAndroid Build Coastguard Worker     return TD.getPointerABIAlignment();
795*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
796*9880d681SAndroid Build Coastguard Worker     return TD.getABIIntegerTypeAlignment(64);
797*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
798*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_LabelDifference32:
799*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_Custom32:
800*9880d681SAndroid Build Coastguard Worker     return TD.getABIIntegerTypeAlignment(32);
801*9880d681SAndroid Build Coastguard Worker   case MachineJumpTableInfo::EK_Inline:
802*9880d681SAndroid Build Coastguard Worker     return 1;
803*9880d681SAndroid Build Coastguard Worker   }
804*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("Unknown jump table encoding!");
805*9880d681SAndroid Build Coastguard Worker }
806*9880d681SAndroid Build Coastguard Worker 
807*9880d681SAndroid Build Coastguard Worker /// Create a new jump table entry in the jump table info.
createJumpTableIndex(const std::vector<MachineBasicBlock * > & DestBBs)808*9880d681SAndroid Build Coastguard Worker unsigned MachineJumpTableInfo::createJumpTableIndex(
809*9880d681SAndroid Build Coastguard Worker                                const std::vector<MachineBasicBlock*> &DestBBs) {
810*9880d681SAndroid Build Coastguard Worker   assert(!DestBBs.empty() && "Cannot create an empty jump table!");
811*9880d681SAndroid Build Coastguard Worker   JumpTables.push_back(MachineJumpTableEntry(DestBBs));
812*9880d681SAndroid Build Coastguard Worker   return JumpTables.size()-1;
813*9880d681SAndroid Build Coastguard Worker }
814*9880d681SAndroid Build Coastguard Worker 
815*9880d681SAndroid Build Coastguard Worker /// If Old is the target of any jump tables, update the jump tables to branch
816*9880d681SAndroid Build Coastguard Worker /// to New instead.
ReplaceMBBInJumpTables(MachineBasicBlock * Old,MachineBasicBlock * New)817*9880d681SAndroid Build Coastguard Worker bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
818*9880d681SAndroid Build Coastguard Worker                                                   MachineBasicBlock *New) {
819*9880d681SAndroid Build Coastguard Worker   assert(Old != New && "Not making a change?");
820*9880d681SAndroid Build Coastguard Worker   bool MadeChange = false;
821*9880d681SAndroid Build Coastguard Worker   for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
822*9880d681SAndroid Build Coastguard Worker     ReplaceMBBInJumpTable(i, Old, New);
823*9880d681SAndroid Build Coastguard Worker   return MadeChange;
824*9880d681SAndroid Build Coastguard Worker }
825*9880d681SAndroid Build Coastguard Worker 
826*9880d681SAndroid Build Coastguard Worker /// If Old is a target of the jump tables, update the jump table to branch to
827*9880d681SAndroid Build Coastguard Worker /// New instead.
ReplaceMBBInJumpTable(unsigned Idx,MachineBasicBlock * Old,MachineBasicBlock * New)828*9880d681SAndroid Build Coastguard Worker bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
829*9880d681SAndroid Build Coastguard Worker                                                  MachineBasicBlock *Old,
830*9880d681SAndroid Build Coastguard Worker                                                  MachineBasicBlock *New) {
831*9880d681SAndroid Build Coastguard Worker   assert(Old != New && "Not making a change?");
832*9880d681SAndroid Build Coastguard Worker   bool MadeChange = false;
833*9880d681SAndroid Build Coastguard Worker   MachineJumpTableEntry &JTE = JumpTables[Idx];
834*9880d681SAndroid Build Coastguard Worker   for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
835*9880d681SAndroid Build Coastguard Worker     if (JTE.MBBs[j] == Old) {
836*9880d681SAndroid Build Coastguard Worker       JTE.MBBs[j] = New;
837*9880d681SAndroid Build Coastguard Worker       MadeChange = true;
838*9880d681SAndroid Build Coastguard Worker     }
839*9880d681SAndroid Build Coastguard Worker   return MadeChange;
840*9880d681SAndroid Build Coastguard Worker }
841*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & OS) const842*9880d681SAndroid Build Coastguard Worker void MachineJumpTableInfo::print(raw_ostream &OS) const {
843*9880d681SAndroid Build Coastguard Worker   if (JumpTables.empty()) return;
844*9880d681SAndroid Build Coastguard Worker 
845*9880d681SAndroid Build Coastguard Worker   OS << "Jump Tables:\n";
846*9880d681SAndroid Build Coastguard Worker 
847*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
848*9880d681SAndroid Build Coastguard Worker     OS << "  jt#" << i << ": ";
849*9880d681SAndroid Build Coastguard Worker     for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
850*9880d681SAndroid Build Coastguard Worker       OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
851*9880d681SAndroid Build Coastguard Worker   }
852*9880d681SAndroid Build Coastguard Worker 
853*9880d681SAndroid Build Coastguard Worker   OS << '\n';
854*9880d681SAndroid Build Coastguard Worker }
855*9880d681SAndroid Build Coastguard Worker 
856*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const857*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
858*9880d681SAndroid Build Coastguard Worker #endif
859*9880d681SAndroid Build Coastguard Worker 
860*9880d681SAndroid Build Coastguard Worker 
861*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
862*9880d681SAndroid Build Coastguard Worker //  MachineConstantPool implementation
863*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
864*9880d681SAndroid Build Coastguard Worker 
anchor()865*9880d681SAndroid Build Coastguard Worker void MachineConstantPoolValue::anchor() { }
866*9880d681SAndroid Build Coastguard Worker 
getType() const867*9880d681SAndroid Build Coastguard Worker Type *MachineConstantPoolEntry::getType() const {
868*9880d681SAndroid Build Coastguard Worker   if (isMachineConstantPoolEntry())
869*9880d681SAndroid Build Coastguard Worker     return Val.MachineCPVal->getType();
870*9880d681SAndroid Build Coastguard Worker   return Val.ConstVal->getType();
871*9880d681SAndroid Build Coastguard Worker }
872*9880d681SAndroid Build Coastguard Worker 
needsRelocation() const873*9880d681SAndroid Build Coastguard Worker bool MachineConstantPoolEntry::needsRelocation() const {
874*9880d681SAndroid Build Coastguard Worker   if (isMachineConstantPoolEntry())
875*9880d681SAndroid Build Coastguard Worker     return true;
876*9880d681SAndroid Build Coastguard Worker   return Val.ConstVal->needsRelocation();
877*9880d681SAndroid Build Coastguard Worker }
878*9880d681SAndroid Build Coastguard Worker 
879*9880d681SAndroid Build Coastguard Worker SectionKind
getSectionKind(const DataLayout * DL) const880*9880d681SAndroid Build Coastguard Worker MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
881*9880d681SAndroid Build Coastguard Worker   if (needsRelocation())
882*9880d681SAndroid Build Coastguard Worker     return SectionKind::getReadOnlyWithRel();
883*9880d681SAndroid Build Coastguard Worker   switch (DL->getTypeAllocSize(getType())) {
884*9880d681SAndroid Build Coastguard Worker   case 4:
885*9880d681SAndroid Build Coastguard Worker     return SectionKind::getMergeableConst4();
886*9880d681SAndroid Build Coastguard Worker   case 8:
887*9880d681SAndroid Build Coastguard Worker     return SectionKind::getMergeableConst8();
888*9880d681SAndroid Build Coastguard Worker   case 16:
889*9880d681SAndroid Build Coastguard Worker     return SectionKind::getMergeableConst16();
890*9880d681SAndroid Build Coastguard Worker   case 32:
891*9880d681SAndroid Build Coastguard Worker     return SectionKind::getMergeableConst32();
892*9880d681SAndroid Build Coastguard Worker   default:
893*9880d681SAndroid Build Coastguard Worker     return SectionKind::getReadOnly();
894*9880d681SAndroid Build Coastguard Worker   }
895*9880d681SAndroid Build Coastguard Worker }
896*9880d681SAndroid Build Coastguard Worker 
~MachineConstantPool()897*9880d681SAndroid Build Coastguard Worker MachineConstantPool::~MachineConstantPool() {
898*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
899*9880d681SAndroid Build Coastguard Worker     if (Constants[i].isMachineConstantPoolEntry())
900*9880d681SAndroid Build Coastguard Worker       delete Constants[i].Val.MachineCPVal;
901*9880d681SAndroid Build Coastguard Worker   for (DenseSet<MachineConstantPoolValue*>::iterator I =
902*9880d681SAndroid Build Coastguard Worker        MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
903*9880d681SAndroid Build Coastguard Worker        I != E; ++I)
904*9880d681SAndroid Build Coastguard Worker     delete *I;
905*9880d681SAndroid Build Coastguard Worker }
906*9880d681SAndroid Build Coastguard Worker 
907*9880d681SAndroid Build Coastguard Worker /// Test whether the given two constants can be allocated the same constant pool
908*9880d681SAndroid Build Coastguard Worker /// entry.
CanShareConstantPoolEntry(const Constant * A,const Constant * B,const DataLayout & DL)909*9880d681SAndroid Build Coastguard Worker static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
910*9880d681SAndroid Build Coastguard Worker                                       const DataLayout &DL) {
911*9880d681SAndroid Build Coastguard Worker   // Handle the trivial case quickly.
912*9880d681SAndroid Build Coastguard Worker   if (A == B) return true;
913*9880d681SAndroid Build Coastguard Worker 
914*9880d681SAndroid Build Coastguard Worker   // If they have the same type but weren't the same constant, quickly
915*9880d681SAndroid Build Coastguard Worker   // reject them.
916*9880d681SAndroid Build Coastguard Worker   if (A->getType() == B->getType()) return false;
917*9880d681SAndroid Build Coastguard Worker 
918*9880d681SAndroid Build Coastguard Worker   // We can't handle structs or arrays.
919*9880d681SAndroid Build Coastguard Worker   if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
920*9880d681SAndroid Build Coastguard Worker       isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
921*9880d681SAndroid Build Coastguard Worker     return false;
922*9880d681SAndroid Build Coastguard Worker 
923*9880d681SAndroid Build Coastguard Worker   // For now, only support constants with the same size.
924*9880d681SAndroid Build Coastguard Worker   uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
925*9880d681SAndroid Build Coastguard Worker   if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
926*9880d681SAndroid Build Coastguard Worker     return false;
927*9880d681SAndroid Build Coastguard Worker 
928*9880d681SAndroid Build Coastguard Worker   Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
929*9880d681SAndroid Build Coastguard Worker 
930*9880d681SAndroid Build Coastguard Worker   // Try constant folding a bitcast of both instructions to an integer.  If we
931*9880d681SAndroid Build Coastguard Worker   // get two identical ConstantInt's, then we are good to share them.  We use
932*9880d681SAndroid Build Coastguard Worker   // the constant folding APIs to do this so that we get the benefit of
933*9880d681SAndroid Build Coastguard Worker   // DataLayout.
934*9880d681SAndroid Build Coastguard Worker   if (isa<PointerType>(A->getType()))
935*9880d681SAndroid Build Coastguard Worker     A = ConstantFoldCastOperand(Instruction::PtrToInt,
936*9880d681SAndroid Build Coastguard Worker                                 const_cast<Constant *>(A), IntTy, DL);
937*9880d681SAndroid Build Coastguard Worker   else if (A->getType() != IntTy)
938*9880d681SAndroid Build Coastguard Worker     A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
939*9880d681SAndroid Build Coastguard Worker                                 IntTy, DL);
940*9880d681SAndroid Build Coastguard Worker   if (isa<PointerType>(B->getType()))
941*9880d681SAndroid Build Coastguard Worker     B = ConstantFoldCastOperand(Instruction::PtrToInt,
942*9880d681SAndroid Build Coastguard Worker                                 const_cast<Constant *>(B), IntTy, DL);
943*9880d681SAndroid Build Coastguard Worker   else if (B->getType() != IntTy)
944*9880d681SAndroid Build Coastguard Worker     B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
945*9880d681SAndroid Build Coastguard Worker                                 IntTy, DL);
946*9880d681SAndroid Build Coastguard Worker 
947*9880d681SAndroid Build Coastguard Worker   return A == B;
948*9880d681SAndroid Build Coastguard Worker }
949*9880d681SAndroid Build Coastguard Worker 
950*9880d681SAndroid Build Coastguard Worker /// Create a new entry in the constant pool or return an existing one.
951*9880d681SAndroid Build Coastguard Worker /// User must specify the log2 of the minimum required alignment for the object.
getConstantPoolIndex(const Constant * C,unsigned Alignment)952*9880d681SAndroid Build Coastguard Worker unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
953*9880d681SAndroid Build Coastguard Worker                                                    unsigned Alignment) {
954*9880d681SAndroid Build Coastguard Worker   assert(Alignment && "Alignment must be specified!");
955*9880d681SAndroid Build Coastguard Worker   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
956*9880d681SAndroid Build Coastguard Worker 
957*9880d681SAndroid Build Coastguard Worker   // Check to see if we already have this constant.
958*9880d681SAndroid Build Coastguard Worker   //
959*9880d681SAndroid Build Coastguard Worker   // FIXME, this could be made much more efficient for large constant pools.
960*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
961*9880d681SAndroid Build Coastguard Worker     if (!Constants[i].isMachineConstantPoolEntry() &&
962*9880d681SAndroid Build Coastguard Worker         CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
963*9880d681SAndroid Build Coastguard Worker       if ((unsigned)Constants[i].getAlignment() < Alignment)
964*9880d681SAndroid Build Coastguard Worker         Constants[i].Alignment = Alignment;
965*9880d681SAndroid Build Coastguard Worker       return i;
966*9880d681SAndroid Build Coastguard Worker     }
967*9880d681SAndroid Build Coastguard Worker 
968*9880d681SAndroid Build Coastguard Worker   Constants.push_back(MachineConstantPoolEntry(C, Alignment));
969*9880d681SAndroid Build Coastguard Worker   return Constants.size()-1;
970*9880d681SAndroid Build Coastguard Worker }
971*9880d681SAndroid Build Coastguard Worker 
getConstantPoolIndex(MachineConstantPoolValue * V,unsigned Alignment)972*9880d681SAndroid Build Coastguard Worker unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
973*9880d681SAndroid Build Coastguard Worker                                                    unsigned Alignment) {
974*9880d681SAndroid Build Coastguard Worker   assert(Alignment && "Alignment must be specified!");
975*9880d681SAndroid Build Coastguard Worker   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
976*9880d681SAndroid Build Coastguard Worker 
977*9880d681SAndroid Build Coastguard Worker   // Check to see if we already have this constant.
978*9880d681SAndroid Build Coastguard Worker   //
979*9880d681SAndroid Build Coastguard Worker   // FIXME, this could be made much more efficient for large constant pools.
980*9880d681SAndroid Build Coastguard Worker   int Idx = V->getExistingMachineCPValue(this, Alignment);
981*9880d681SAndroid Build Coastguard Worker   if (Idx != -1) {
982*9880d681SAndroid Build Coastguard Worker     MachineCPVsSharingEntries.insert(V);
983*9880d681SAndroid Build Coastguard Worker     return (unsigned)Idx;
984*9880d681SAndroid Build Coastguard Worker   }
985*9880d681SAndroid Build Coastguard Worker 
986*9880d681SAndroid Build Coastguard Worker   Constants.push_back(MachineConstantPoolEntry(V, Alignment));
987*9880d681SAndroid Build Coastguard Worker   return Constants.size()-1;
988*9880d681SAndroid Build Coastguard Worker }
989*9880d681SAndroid Build Coastguard Worker 
print(raw_ostream & OS) const990*9880d681SAndroid Build Coastguard Worker void MachineConstantPool::print(raw_ostream &OS) const {
991*9880d681SAndroid Build Coastguard Worker   if (Constants.empty()) return;
992*9880d681SAndroid Build Coastguard Worker 
993*9880d681SAndroid Build Coastguard Worker   OS << "Constant Pool:\n";
994*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
995*9880d681SAndroid Build Coastguard Worker     OS << "  cp#" << i << ": ";
996*9880d681SAndroid Build Coastguard Worker     if (Constants[i].isMachineConstantPoolEntry())
997*9880d681SAndroid Build Coastguard Worker       Constants[i].Val.MachineCPVal->print(OS);
998*9880d681SAndroid Build Coastguard Worker     else
999*9880d681SAndroid Build Coastguard Worker       Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1000*9880d681SAndroid Build Coastguard Worker     OS << ", align=" << Constants[i].getAlignment();
1001*9880d681SAndroid Build Coastguard Worker     OS << "\n";
1002*9880d681SAndroid Build Coastguard Worker   }
1003*9880d681SAndroid Build Coastguard Worker }
1004*9880d681SAndroid Build Coastguard Worker 
1005*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1006*9880d681SAndroid Build Coastguard Worker LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1007*9880d681SAndroid Build Coastguard Worker #endif
1008