1*9880d681SAndroid Build Coastguard Worker //===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
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 // Simple pass to fill delay slots with useful instructions.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker
14*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsMCNaCl.h"
15*9880d681SAndroid Build Coastguard Worker #include "Mips.h"
16*9880d681SAndroid Build Coastguard Worker #include "MipsInstrInfo.h"
17*9880d681SAndroid Build Coastguard Worker #include "MipsTargetMachine.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/BitVector.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallPtrSet.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/AliasAnalysis.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ValueTracking.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/PseudoSourceValue.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker using namespace llvm;
34*9880d681SAndroid Build Coastguard Worker
35*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "delay-slot-filler"
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker STATISTIC(FilledSlots, "Number of delay slots filled");
38*9880d681SAndroid Build Coastguard Worker STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
39*9880d681SAndroid Build Coastguard Worker " are not NOP.");
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableDelaySlotFiller(
42*9880d681SAndroid Build Coastguard Worker "disable-mips-delay-filler",
43*9880d681SAndroid Build Coastguard Worker cl::init(false),
44*9880d681SAndroid Build Coastguard Worker cl::desc("Fill all delay slots with NOPs."),
45*9880d681SAndroid Build Coastguard Worker cl::Hidden);
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableForwardSearch(
48*9880d681SAndroid Build Coastguard Worker "disable-mips-df-forward-search",
49*9880d681SAndroid Build Coastguard Worker cl::init(true),
50*9880d681SAndroid Build Coastguard Worker cl::desc("Disallow MIPS delay filler to search forward."),
51*9880d681SAndroid Build Coastguard Worker cl::Hidden);
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableSuccBBSearch(
54*9880d681SAndroid Build Coastguard Worker "disable-mips-df-succbb-search",
55*9880d681SAndroid Build Coastguard Worker cl::init(true),
56*9880d681SAndroid Build Coastguard Worker cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
57*9880d681SAndroid Build Coastguard Worker cl::Hidden);
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker static cl::opt<bool> DisableBackwardSearch(
60*9880d681SAndroid Build Coastguard Worker "disable-mips-df-backward-search",
61*9880d681SAndroid Build Coastguard Worker cl::init(false),
62*9880d681SAndroid Build Coastguard Worker cl::desc("Disallow MIPS delay filler to search backward."),
63*9880d681SAndroid Build Coastguard Worker cl::Hidden);
64*9880d681SAndroid Build Coastguard Worker
65*9880d681SAndroid Build Coastguard Worker enum CompactBranchPolicy {
66*9880d681SAndroid Build Coastguard Worker CB_Never, ///< The policy 'never' may in some circumstances or for some
67*9880d681SAndroid Build Coastguard Worker ///< ISAs not be absolutely adhered to.
68*9880d681SAndroid Build Coastguard Worker CB_Optimal, ///< Optimal is the default and will produce compact branches
69*9880d681SAndroid Build Coastguard Worker ///< when delay slots cannot be filled.
70*9880d681SAndroid Build Coastguard Worker CB_Always ///< 'always' may in some circumstances may not be
71*9880d681SAndroid Build Coastguard Worker ///< absolutely adhered to there may not be a corresponding
72*9880d681SAndroid Build Coastguard Worker ///< compact form of a branch.
73*9880d681SAndroid Build Coastguard Worker };
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker static cl::opt<CompactBranchPolicy> MipsCompactBranchPolicy(
76*9880d681SAndroid Build Coastguard Worker "mips-compact-branches",cl::Optional,
77*9880d681SAndroid Build Coastguard Worker cl::init(CB_Optimal),
78*9880d681SAndroid Build Coastguard Worker cl::desc("MIPS Specific: Compact branch policy."),
79*9880d681SAndroid Build Coastguard Worker cl::values(
80*9880d681SAndroid Build Coastguard Worker clEnumValN(CB_Never, "never", "Do not use compact branches if possible."),
81*9880d681SAndroid Build Coastguard Worker clEnumValN(CB_Optimal, "optimal", "Use compact branches where appropiate (default)."),
82*9880d681SAndroid Build Coastguard Worker clEnumValN(CB_Always, "always", "Always use compact branches if possible."),
83*9880d681SAndroid Build Coastguard Worker clEnumValEnd
84*9880d681SAndroid Build Coastguard Worker )
85*9880d681SAndroid Build Coastguard Worker );
86*9880d681SAndroid Build Coastguard Worker
87*9880d681SAndroid Build Coastguard Worker namespace {
88*9880d681SAndroid Build Coastguard Worker typedef MachineBasicBlock::iterator Iter;
89*9880d681SAndroid Build Coastguard Worker typedef MachineBasicBlock::reverse_iterator ReverseIter;
90*9880d681SAndroid Build Coastguard Worker typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker class RegDefsUses {
93*9880d681SAndroid Build Coastguard Worker public:
94*9880d681SAndroid Build Coastguard Worker RegDefsUses(const TargetRegisterInfo &TRI);
95*9880d681SAndroid Build Coastguard Worker void init(const MachineInstr &MI);
96*9880d681SAndroid Build Coastguard Worker
97*9880d681SAndroid Build Coastguard Worker /// This function sets all caller-saved registers in Defs.
98*9880d681SAndroid Build Coastguard Worker void setCallerSaved(const MachineInstr &MI);
99*9880d681SAndroid Build Coastguard Worker
100*9880d681SAndroid Build Coastguard Worker /// This function sets all unallocatable registers in Defs.
101*9880d681SAndroid Build Coastguard Worker void setUnallocatableRegs(const MachineFunction &MF);
102*9880d681SAndroid Build Coastguard Worker
103*9880d681SAndroid Build Coastguard Worker /// Set bits in Uses corresponding to MBB's live-out registers except for
104*9880d681SAndroid Build Coastguard Worker /// the registers that are live-in to SuccBB.
105*9880d681SAndroid Build Coastguard Worker void addLiveOut(const MachineBasicBlock &MBB,
106*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock &SuccBB);
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker private:
111*9880d681SAndroid Build Coastguard Worker bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
112*9880d681SAndroid Build Coastguard Worker bool IsDef) const;
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker /// Returns true if Reg or its alias is in RegSet.
115*9880d681SAndroid Build Coastguard Worker bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo &TRI;
118*9880d681SAndroid Build Coastguard Worker BitVector Defs, Uses;
119*9880d681SAndroid Build Coastguard Worker };
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker /// Base class for inspecting loads and stores.
122*9880d681SAndroid Build Coastguard Worker class InspectMemInstr {
123*9880d681SAndroid Build Coastguard Worker public:
InspectMemInstr(bool ForbidMemInstr_)124*9880d681SAndroid Build Coastguard Worker InspectMemInstr(bool ForbidMemInstr_)
125*9880d681SAndroid Build Coastguard Worker : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
126*9880d681SAndroid Build Coastguard Worker SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
127*9880d681SAndroid Build Coastguard Worker
128*9880d681SAndroid Build Coastguard Worker /// Return true if MI cannot be moved to delay slot.
129*9880d681SAndroid Build Coastguard Worker bool hasHazard(const MachineInstr &MI);
130*9880d681SAndroid Build Coastguard Worker
~InspectMemInstr()131*9880d681SAndroid Build Coastguard Worker virtual ~InspectMemInstr() {}
132*9880d681SAndroid Build Coastguard Worker
133*9880d681SAndroid Build Coastguard Worker protected:
134*9880d681SAndroid Build Coastguard Worker /// Flags indicating whether loads or stores have been seen.
135*9880d681SAndroid Build Coastguard Worker bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker /// Memory instructions are not allowed to move to delay slot if this flag
138*9880d681SAndroid Build Coastguard Worker /// is true.
139*9880d681SAndroid Build Coastguard Worker bool ForbidMemInstr;
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker private:
142*9880d681SAndroid Build Coastguard Worker virtual bool hasHazard_(const MachineInstr &MI) = 0;
143*9880d681SAndroid Build Coastguard Worker };
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker /// This subclass rejects any memory instructions.
146*9880d681SAndroid Build Coastguard Worker class NoMemInstr : public InspectMemInstr {
147*9880d681SAndroid Build Coastguard Worker public:
NoMemInstr()148*9880d681SAndroid Build Coastguard Worker NoMemInstr() : InspectMemInstr(true) {}
149*9880d681SAndroid Build Coastguard Worker private:
hasHazard_(const MachineInstr & MI)150*9880d681SAndroid Build Coastguard Worker bool hasHazard_(const MachineInstr &MI) override { return true; }
151*9880d681SAndroid Build Coastguard Worker };
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker /// This subclass accepts loads from stacks and constant loads.
154*9880d681SAndroid Build Coastguard Worker class LoadFromStackOrConst : public InspectMemInstr {
155*9880d681SAndroid Build Coastguard Worker public:
LoadFromStackOrConst()156*9880d681SAndroid Build Coastguard Worker LoadFromStackOrConst() : InspectMemInstr(false) {}
157*9880d681SAndroid Build Coastguard Worker private:
158*9880d681SAndroid Build Coastguard Worker bool hasHazard_(const MachineInstr &MI) override;
159*9880d681SAndroid Build Coastguard Worker };
160*9880d681SAndroid Build Coastguard Worker
161*9880d681SAndroid Build Coastguard Worker /// This subclass uses memory dependence information to determine whether a
162*9880d681SAndroid Build Coastguard Worker /// memory instruction can be moved to a delay slot.
163*9880d681SAndroid Build Coastguard Worker class MemDefsUses : public InspectMemInstr {
164*9880d681SAndroid Build Coastguard Worker public:
165*9880d681SAndroid Build Coastguard Worker MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI);
166*9880d681SAndroid Build Coastguard Worker
167*9880d681SAndroid Build Coastguard Worker private:
168*9880d681SAndroid Build Coastguard Worker typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
169*9880d681SAndroid Build Coastguard Worker
170*9880d681SAndroid Build Coastguard Worker bool hasHazard_(const MachineInstr &MI) override;
171*9880d681SAndroid Build Coastguard Worker
172*9880d681SAndroid Build Coastguard Worker /// Update Defs and Uses. Return true if there exist dependences that
173*9880d681SAndroid Build Coastguard Worker /// disqualify the delay slot candidate between V and values in Uses and
174*9880d681SAndroid Build Coastguard Worker /// Defs.
175*9880d681SAndroid Build Coastguard Worker bool updateDefsUses(ValueType V, bool MayStore);
176*9880d681SAndroid Build Coastguard Worker
177*9880d681SAndroid Build Coastguard Worker /// Get the list of underlying objects of MI's memory operand.
178*9880d681SAndroid Build Coastguard Worker bool getUnderlyingObjects(const MachineInstr &MI,
179*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<ValueType> &Objects) const;
180*9880d681SAndroid Build Coastguard Worker
181*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI;
182*9880d681SAndroid Build Coastguard Worker SmallPtrSet<ValueType, 4> Uses, Defs;
183*9880d681SAndroid Build Coastguard Worker const DataLayout &DL;
184*9880d681SAndroid Build Coastguard Worker
185*9880d681SAndroid Build Coastguard Worker /// Flags indicating whether loads or stores with no underlying objects have
186*9880d681SAndroid Build Coastguard Worker /// been seen.
187*9880d681SAndroid Build Coastguard Worker bool SeenNoObjLoad, SeenNoObjStore;
188*9880d681SAndroid Build Coastguard Worker };
189*9880d681SAndroid Build Coastguard Worker
190*9880d681SAndroid Build Coastguard Worker class Filler : public MachineFunctionPass {
191*9880d681SAndroid Build Coastguard Worker public:
Filler(TargetMachine & tm)192*9880d681SAndroid Build Coastguard Worker Filler(TargetMachine &tm)
193*9880d681SAndroid Build Coastguard Worker : MachineFunctionPass(ID), TM(tm) { }
194*9880d681SAndroid Build Coastguard Worker
getPassName() const195*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
196*9880d681SAndroid Build Coastguard Worker return "Mips Delay Slot Filler";
197*9880d681SAndroid Build Coastguard Worker }
198*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & F)199*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &F) override {
200*9880d681SAndroid Build Coastguard Worker bool Changed = false;
201*9880d681SAndroid Build Coastguard Worker for (MachineFunction::iterator FI = F.begin(), FE = F.end();
202*9880d681SAndroid Build Coastguard Worker FI != FE; ++FI)
203*9880d681SAndroid Build Coastguard Worker Changed |= runOnMachineBasicBlock(*FI);
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker // This pass invalidates liveness information when it reorders
206*9880d681SAndroid Build Coastguard Worker // instructions to fill delay slot. Without this, -verify-machineinstrs
207*9880d681SAndroid Build Coastguard Worker // will fail.
208*9880d681SAndroid Build Coastguard Worker if (Changed)
209*9880d681SAndroid Build Coastguard Worker F.getRegInfo().invalidateLiveness();
210*9880d681SAndroid Build Coastguard Worker
211*9880d681SAndroid Build Coastguard Worker return Changed;
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker
getRequiredProperties() const214*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties getRequiredProperties() const override {
215*9880d681SAndroid Build Coastguard Worker return MachineFunctionProperties().set(
216*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties::Property::AllVRegsAllocated);
217*9880d681SAndroid Build Coastguard Worker }
218*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const219*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
220*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineBranchProbabilityInfo>();
221*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
222*9880d681SAndroid Build Coastguard Worker }
223*9880d681SAndroid Build Coastguard Worker
224*9880d681SAndroid Build Coastguard Worker private:
225*9880d681SAndroid Build Coastguard Worker bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
226*9880d681SAndroid Build Coastguard Worker
227*9880d681SAndroid Build Coastguard Worker Iter replaceWithCompactBranch(MachineBasicBlock &MBB, Iter Branch,
228*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL);
229*9880d681SAndroid Build Coastguard Worker
230*9880d681SAndroid Build Coastguard Worker /// This function checks if it is valid to move Candidate to the delay slot
231*9880d681SAndroid Build Coastguard Worker /// and returns true if it isn't. It also updates memory and register
232*9880d681SAndroid Build Coastguard Worker /// dependence information.
233*9880d681SAndroid Build Coastguard Worker bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
234*9880d681SAndroid Build Coastguard Worker InspectMemInstr &IM) const;
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker /// This function searches range [Begin, End) for an instruction that can be
237*9880d681SAndroid Build Coastguard Worker /// moved to the delay slot. Returns true on success.
238*9880d681SAndroid Build Coastguard Worker template<typename IterTy>
239*9880d681SAndroid Build Coastguard Worker bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
240*9880d681SAndroid Build Coastguard Worker RegDefsUses &RegDU, InspectMemInstr &IM, Iter Slot,
241*9880d681SAndroid Build Coastguard Worker IterTy &Filler) const;
242*9880d681SAndroid Build Coastguard Worker
243*9880d681SAndroid Build Coastguard Worker /// This function searches in the backward direction for an instruction that
244*9880d681SAndroid Build Coastguard Worker /// can be moved to the delay slot. Returns true on success.
245*9880d681SAndroid Build Coastguard Worker bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
246*9880d681SAndroid Build Coastguard Worker
247*9880d681SAndroid Build Coastguard Worker /// This function searches MBB in the forward direction for an instruction
248*9880d681SAndroid Build Coastguard Worker /// that can be moved to the delay slot. Returns true on success.
249*9880d681SAndroid Build Coastguard Worker bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker /// This function searches one of MBB's successor blocks for an instruction
252*9880d681SAndroid Build Coastguard Worker /// that can be moved to the delay slot and inserts clones of the
253*9880d681SAndroid Build Coastguard Worker /// instruction into the successor's predecessor blocks.
254*9880d681SAndroid Build Coastguard Worker bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
255*9880d681SAndroid Build Coastguard Worker
256*9880d681SAndroid Build Coastguard Worker /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
257*9880d681SAndroid Build Coastguard Worker /// successor block that is not a landing pad.
258*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
259*9880d681SAndroid Build Coastguard Worker
260*9880d681SAndroid Build Coastguard Worker /// This function analyzes MBB and returns an instruction with an unoccupied
261*9880d681SAndroid Build Coastguard Worker /// slot that branches to Dst.
262*9880d681SAndroid Build Coastguard Worker std::pair<MipsInstrInfo::BranchType, MachineInstr *>
263*9880d681SAndroid Build Coastguard Worker getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker /// Examine Pred and see if it is possible to insert an instruction into
266*9880d681SAndroid Build Coastguard Worker /// one of its branches delay slot or its end.
267*9880d681SAndroid Build Coastguard Worker bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
268*9880d681SAndroid Build Coastguard Worker RegDefsUses &RegDU, bool &HasMultipleSuccs,
269*9880d681SAndroid Build Coastguard Worker BB2BrMap &BrMap) const;
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker bool terminateSearch(const MachineInstr &Candidate) const;
272*9880d681SAndroid Build Coastguard Worker
273*9880d681SAndroid Build Coastguard Worker TargetMachine &TM;
274*9880d681SAndroid Build Coastguard Worker
275*9880d681SAndroid Build Coastguard Worker static char ID;
276*9880d681SAndroid Build Coastguard Worker };
277*9880d681SAndroid Build Coastguard Worker char Filler::ID = 0;
278*9880d681SAndroid Build Coastguard Worker } // end of anonymous namespace
279*9880d681SAndroid Build Coastguard Worker
hasUnoccupiedSlot(const MachineInstr * MI)280*9880d681SAndroid Build Coastguard Worker static bool hasUnoccupiedSlot(const MachineInstr *MI) {
281*9880d681SAndroid Build Coastguard Worker return MI->hasDelaySlot() && !MI->isBundledWithSucc();
282*9880d681SAndroid Build Coastguard Worker }
283*9880d681SAndroid Build Coastguard Worker
284*9880d681SAndroid Build Coastguard Worker /// This function inserts clones of Filler into predecessor blocks.
insertDelayFiller(Iter Filler,const BB2BrMap & BrMap)285*9880d681SAndroid Build Coastguard Worker static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
286*9880d681SAndroid Build Coastguard Worker MachineFunction *MF = Filler->getParent()->getParent();
287*9880d681SAndroid Build Coastguard Worker
288*9880d681SAndroid Build Coastguard Worker for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
289*9880d681SAndroid Build Coastguard Worker if (I->second) {
290*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
291*9880d681SAndroid Build Coastguard Worker ++UsefulSlots;
292*9880d681SAndroid Build Coastguard Worker } else {
293*9880d681SAndroid Build Coastguard Worker I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
294*9880d681SAndroid Build Coastguard Worker }
295*9880d681SAndroid Build Coastguard Worker }
296*9880d681SAndroid Build Coastguard Worker }
297*9880d681SAndroid Build Coastguard Worker
298*9880d681SAndroid Build Coastguard Worker /// This function adds registers Filler defines to MBB's live-in register list.
addLiveInRegs(Iter Filler,MachineBasicBlock & MBB)299*9880d681SAndroid Build Coastguard Worker static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
300*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
301*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = Filler->getOperand(I);
302*9880d681SAndroid Build Coastguard Worker unsigned R;
303*9880d681SAndroid Build Coastguard Worker
304*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
305*9880d681SAndroid Build Coastguard Worker continue;
306*9880d681SAndroid Build Coastguard Worker
307*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
308*9880d681SAndroid Build Coastguard Worker const MachineFunction &MF = *MBB.getParent();
309*9880d681SAndroid Build Coastguard Worker assert(MF.getSubtarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
310*9880d681SAndroid Build Coastguard Worker "Shouldn't move an instruction with unallocatable registers across "
311*9880d681SAndroid Build Coastguard Worker "basic block boundaries.");
312*9880d681SAndroid Build Coastguard Worker #endif
313*9880d681SAndroid Build Coastguard Worker
314*9880d681SAndroid Build Coastguard Worker if (!MBB.isLiveIn(R))
315*9880d681SAndroid Build Coastguard Worker MBB.addLiveIn(R);
316*9880d681SAndroid Build Coastguard Worker }
317*9880d681SAndroid Build Coastguard Worker }
318*9880d681SAndroid Build Coastguard Worker
RegDefsUses(const TargetRegisterInfo & TRI)319*9880d681SAndroid Build Coastguard Worker RegDefsUses::RegDefsUses(const TargetRegisterInfo &TRI)
320*9880d681SAndroid Build Coastguard Worker : TRI(TRI), Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {}
321*9880d681SAndroid Build Coastguard Worker
init(const MachineInstr & MI)322*9880d681SAndroid Build Coastguard Worker void RegDefsUses::init(const MachineInstr &MI) {
323*9880d681SAndroid Build Coastguard Worker // Add all register operands which are explicit and non-variadic.
324*9880d681SAndroid Build Coastguard Worker update(MI, 0, MI.getDesc().getNumOperands());
325*9880d681SAndroid Build Coastguard Worker
326*9880d681SAndroid Build Coastguard Worker // If MI is a call, add RA to Defs to prevent users of RA from going into
327*9880d681SAndroid Build Coastguard Worker // delay slot.
328*9880d681SAndroid Build Coastguard Worker if (MI.isCall())
329*9880d681SAndroid Build Coastguard Worker Defs.set(Mips::RA);
330*9880d681SAndroid Build Coastguard Worker
331*9880d681SAndroid Build Coastguard Worker // Add all implicit register operands of branch instructions except
332*9880d681SAndroid Build Coastguard Worker // register AT.
333*9880d681SAndroid Build Coastguard Worker if (MI.isBranch()) {
334*9880d681SAndroid Build Coastguard Worker update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
335*9880d681SAndroid Build Coastguard Worker Defs.reset(Mips::AT);
336*9880d681SAndroid Build Coastguard Worker }
337*9880d681SAndroid Build Coastguard Worker }
338*9880d681SAndroid Build Coastguard Worker
setCallerSaved(const MachineInstr & MI)339*9880d681SAndroid Build Coastguard Worker void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
340*9880d681SAndroid Build Coastguard Worker assert(MI.isCall());
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard Worker // Add RA/RA_64 to Defs to prevent users of RA/RA_64 from going into
343*9880d681SAndroid Build Coastguard Worker // the delay slot. The reason is that RA/RA_64 must not be changed
344*9880d681SAndroid Build Coastguard Worker // in the delay slot so that the callee can return to the caller.
345*9880d681SAndroid Build Coastguard Worker if (MI.definesRegister(Mips::RA) || MI.definesRegister(Mips::RA_64)) {
346*9880d681SAndroid Build Coastguard Worker Defs.set(Mips::RA);
347*9880d681SAndroid Build Coastguard Worker Defs.set(Mips::RA_64);
348*9880d681SAndroid Build Coastguard Worker }
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker // If MI is a call, add all caller-saved registers to Defs.
351*9880d681SAndroid Build Coastguard Worker BitVector CallerSavedRegs(TRI.getNumRegs(), true);
352*9880d681SAndroid Build Coastguard Worker
353*9880d681SAndroid Build Coastguard Worker CallerSavedRegs.reset(Mips::ZERO);
354*9880d681SAndroid Build Coastguard Worker CallerSavedRegs.reset(Mips::ZERO_64);
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard Worker for (const MCPhysReg *R = TRI.getCalleeSavedRegs(MI.getParent()->getParent());
357*9880d681SAndroid Build Coastguard Worker *R; ++R)
358*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
359*9880d681SAndroid Build Coastguard Worker CallerSavedRegs.reset(*AI);
360*9880d681SAndroid Build Coastguard Worker
361*9880d681SAndroid Build Coastguard Worker Defs |= CallerSavedRegs;
362*9880d681SAndroid Build Coastguard Worker }
363*9880d681SAndroid Build Coastguard Worker
setUnallocatableRegs(const MachineFunction & MF)364*9880d681SAndroid Build Coastguard Worker void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
365*9880d681SAndroid Build Coastguard Worker BitVector AllocSet = TRI.getAllocatableSet(MF);
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
368*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
369*9880d681SAndroid Build Coastguard Worker AllocSet.set(*AI);
370*9880d681SAndroid Build Coastguard Worker
371*9880d681SAndroid Build Coastguard Worker AllocSet.set(Mips::ZERO);
372*9880d681SAndroid Build Coastguard Worker AllocSet.set(Mips::ZERO_64);
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker Defs |= AllocSet.flip();
375*9880d681SAndroid Build Coastguard Worker }
376*9880d681SAndroid Build Coastguard Worker
addLiveOut(const MachineBasicBlock & MBB,const MachineBasicBlock & SuccBB)377*9880d681SAndroid Build Coastguard Worker void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
378*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock &SuccBB) {
379*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
380*9880d681SAndroid Build Coastguard Worker SE = MBB.succ_end(); SI != SE; ++SI)
381*9880d681SAndroid Build Coastguard Worker if (*SI != &SuccBB)
382*9880d681SAndroid Build Coastguard Worker for (const auto &LI : (*SI)->liveins())
383*9880d681SAndroid Build Coastguard Worker Uses.set(LI.PhysReg);
384*9880d681SAndroid Build Coastguard Worker }
385*9880d681SAndroid Build Coastguard Worker
update(const MachineInstr & MI,unsigned Begin,unsigned End)386*9880d681SAndroid Build Coastguard Worker bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
387*9880d681SAndroid Build Coastguard Worker BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
388*9880d681SAndroid Build Coastguard Worker bool HasHazard = false;
389*9880d681SAndroid Build Coastguard Worker
390*9880d681SAndroid Build Coastguard Worker for (unsigned I = Begin; I != End; ++I) {
391*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI.getOperand(I);
392*9880d681SAndroid Build Coastguard Worker
393*9880d681SAndroid Build Coastguard Worker if (MO.isReg() && MO.getReg())
394*9880d681SAndroid Build Coastguard Worker HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
395*9880d681SAndroid Build Coastguard Worker }
396*9880d681SAndroid Build Coastguard Worker
397*9880d681SAndroid Build Coastguard Worker Defs |= NewDefs;
398*9880d681SAndroid Build Coastguard Worker Uses |= NewUses;
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker return HasHazard;
401*9880d681SAndroid Build Coastguard Worker }
402*9880d681SAndroid Build Coastguard Worker
checkRegDefsUses(BitVector & NewDefs,BitVector & NewUses,unsigned Reg,bool IsDef) const403*9880d681SAndroid Build Coastguard Worker bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
404*9880d681SAndroid Build Coastguard Worker unsigned Reg, bool IsDef) const {
405*9880d681SAndroid Build Coastguard Worker if (IsDef) {
406*9880d681SAndroid Build Coastguard Worker NewDefs.set(Reg);
407*9880d681SAndroid Build Coastguard Worker // check whether Reg has already been defined or used.
408*9880d681SAndroid Build Coastguard Worker return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
409*9880d681SAndroid Build Coastguard Worker }
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker NewUses.set(Reg);
412*9880d681SAndroid Build Coastguard Worker // check whether Reg has already been defined.
413*9880d681SAndroid Build Coastguard Worker return isRegInSet(Defs, Reg);
414*9880d681SAndroid Build Coastguard Worker }
415*9880d681SAndroid Build Coastguard Worker
isRegInSet(const BitVector & RegSet,unsigned Reg) const416*9880d681SAndroid Build Coastguard Worker bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
417*9880d681SAndroid Build Coastguard Worker // Check Reg and all aliased Registers.
418*9880d681SAndroid Build Coastguard Worker for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
419*9880d681SAndroid Build Coastguard Worker if (RegSet.test(*AI))
420*9880d681SAndroid Build Coastguard Worker return true;
421*9880d681SAndroid Build Coastguard Worker return false;
422*9880d681SAndroid Build Coastguard Worker }
423*9880d681SAndroid Build Coastguard Worker
hasHazard(const MachineInstr & MI)424*9880d681SAndroid Build Coastguard Worker bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
425*9880d681SAndroid Build Coastguard Worker if (!MI.mayStore() && !MI.mayLoad())
426*9880d681SAndroid Build Coastguard Worker return false;
427*9880d681SAndroid Build Coastguard Worker
428*9880d681SAndroid Build Coastguard Worker if (ForbidMemInstr)
429*9880d681SAndroid Build Coastguard Worker return true;
430*9880d681SAndroid Build Coastguard Worker
431*9880d681SAndroid Build Coastguard Worker OrigSeenLoad = SeenLoad;
432*9880d681SAndroid Build Coastguard Worker OrigSeenStore = SeenStore;
433*9880d681SAndroid Build Coastguard Worker SeenLoad |= MI.mayLoad();
434*9880d681SAndroid Build Coastguard Worker SeenStore |= MI.mayStore();
435*9880d681SAndroid Build Coastguard Worker
436*9880d681SAndroid Build Coastguard Worker // If MI is an ordered or volatile memory reference, disallow moving
437*9880d681SAndroid Build Coastguard Worker // subsequent loads and stores to delay slot.
438*9880d681SAndroid Build Coastguard Worker if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
439*9880d681SAndroid Build Coastguard Worker ForbidMemInstr = true;
440*9880d681SAndroid Build Coastguard Worker return true;
441*9880d681SAndroid Build Coastguard Worker }
442*9880d681SAndroid Build Coastguard Worker
443*9880d681SAndroid Build Coastguard Worker return hasHazard_(MI);
444*9880d681SAndroid Build Coastguard Worker }
445*9880d681SAndroid Build Coastguard Worker
hasHazard_(const MachineInstr & MI)446*9880d681SAndroid Build Coastguard Worker bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
447*9880d681SAndroid Build Coastguard Worker if (MI.mayStore())
448*9880d681SAndroid Build Coastguard Worker return true;
449*9880d681SAndroid Build Coastguard Worker
450*9880d681SAndroid Build Coastguard Worker if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue())
451*9880d681SAndroid Build Coastguard Worker return true;
452*9880d681SAndroid Build Coastguard Worker
453*9880d681SAndroid Build Coastguard Worker if (const PseudoSourceValue *PSV =
454*9880d681SAndroid Build Coastguard Worker (*MI.memoperands_begin())->getPseudoValue()) {
455*9880d681SAndroid Build Coastguard Worker if (isa<FixedStackPseudoSourceValue>(PSV))
456*9880d681SAndroid Build Coastguard Worker return false;
457*9880d681SAndroid Build Coastguard Worker return !PSV->isConstant(nullptr) && !PSV->isStack();
458*9880d681SAndroid Build Coastguard Worker }
459*9880d681SAndroid Build Coastguard Worker
460*9880d681SAndroid Build Coastguard Worker return true;
461*9880d681SAndroid Build Coastguard Worker }
462*9880d681SAndroid Build Coastguard Worker
MemDefsUses(const DataLayout & DL,const MachineFrameInfo * MFI_)463*9880d681SAndroid Build Coastguard Worker MemDefsUses::MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI_)
464*9880d681SAndroid Build Coastguard Worker : InspectMemInstr(false), MFI(MFI_), DL(DL), SeenNoObjLoad(false),
465*9880d681SAndroid Build Coastguard Worker SeenNoObjStore(false) {}
466*9880d681SAndroid Build Coastguard Worker
hasHazard_(const MachineInstr & MI)467*9880d681SAndroid Build Coastguard Worker bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
468*9880d681SAndroid Build Coastguard Worker bool HasHazard = false;
469*9880d681SAndroid Build Coastguard Worker SmallVector<ValueType, 4> Objs;
470*9880d681SAndroid Build Coastguard Worker
471*9880d681SAndroid Build Coastguard Worker // Check underlying object list.
472*9880d681SAndroid Build Coastguard Worker if (getUnderlyingObjects(MI, Objs)) {
473*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin();
474*9880d681SAndroid Build Coastguard Worker I != Objs.end(); ++I)
475*9880d681SAndroid Build Coastguard Worker HasHazard |= updateDefsUses(*I, MI.mayStore());
476*9880d681SAndroid Build Coastguard Worker
477*9880d681SAndroid Build Coastguard Worker return HasHazard;
478*9880d681SAndroid Build Coastguard Worker }
479*9880d681SAndroid Build Coastguard Worker
480*9880d681SAndroid Build Coastguard Worker // No underlying objects found.
481*9880d681SAndroid Build Coastguard Worker HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
482*9880d681SAndroid Build Coastguard Worker HasHazard |= MI.mayLoad() || OrigSeenStore;
483*9880d681SAndroid Build Coastguard Worker
484*9880d681SAndroid Build Coastguard Worker SeenNoObjLoad |= MI.mayLoad();
485*9880d681SAndroid Build Coastguard Worker SeenNoObjStore |= MI.mayStore();
486*9880d681SAndroid Build Coastguard Worker
487*9880d681SAndroid Build Coastguard Worker return HasHazard;
488*9880d681SAndroid Build Coastguard Worker }
489*9880d681SAndroid Build Coastguard Worker
updateDefsUses(ValueType V,bool MayStore)490*9880d681SAndroid Build Coastguard Worker bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) {
491*9880d681SAndroid Build Coastguard Worker if (MayStore)
492*9880d681SAndroid Build Coastguard Worker return !Defs.insert(V).second || Uses.count(V) || SeenNoObjStore ||
493*9880d681SAndroid Build Coastguard Worker SeenNoObjLoad;
494*9880d681SAndroid Build Coastguard Worker
495*9880d681SAndroid Build Coastguard Worker Uses.insert(V);
496*9880d681SAndroid Build Coastguard Worker return Defs.count(V) || SeenNoObjStore;
497*9880d681SAndroid Build Coastguard Worker }
498*9880d681SAndroid Build Coastguard Worker
499*9880d681SAndroid Build Coastguard Worker bool MemDefsUses::
getUnderlyingObjects(const MachineInstr & MI,SmallVectorImpl<ValueType> & Objects) const500*9880d681SAndroid Build Coastguard Worker getUnderlyingObjects(const MachineInstr &MI,
501*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<ValueType> &Objects) const {
502*9880d681SAndroid Build Coastguard Worker if (!MI.hasOneMemOperand() ||
503*9880d681SAndroid Build Coastguard Worker (!(*MI.memoperands_begin())->getValue() &&
504*9880d681SAndroid Build Coastguard Worker !(*MI.memoperands_begin())->getPseudoValue()))
505*9880d681SAndroid Build Coastguard Worker return false;
506*9880d681SAndroid Build Coastguard Worker
507*9880d681SAndroid Build Coastguard Worker if (const PseudoSourceValue *PSV =
508*9880d681SAndroid Build Coastguard Worker (*MI.memoperands_begin())->getPseudoValue()) {
509*9880d681SAndroid Build Coastguard Worker if (!PSV->isAliased(MFI))
510*9880d681SAndroid Build Coastguard Worker return false;
511*9880d681SAndroid Build Coastguard Worker Objects.push_back(PSV);
512*9880d681SAndroid Build Coastguard Worker return true;
513*9880d681SAndroid Build Coastguard Worker }
514*9880d681SAndroid Build Coastguard Worker
515*9880d681SAndroid Build Coastguard Worker const Value *V = (*MI.memoperands_begin())->getValue();
516*9880d681SAndroid Build Coastguard Worker
517*9880d681SAndroid Build Coastguard Worker SmallVector<Value *, 4> Objs;
518*9880d681SAndroid Build Coastguard Worker GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
519*9880d681SAndroid Build Coastguard Worker
520*9880d681SAndroid Build Coastguard Worker for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
521*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
522*9880d681SAndroid Build Coastguard Worker if (!isIdentifiedObject(V))
523*9880d681SAndroid Build Coastguard Worker return false;
524*9880d681SAndroid Build Coastguard Worker
525*9880d681SAndroid Build Coastguard Worker Objects.push_back(*I);
526*9880d681SAndroid Build Coastguard Worker }
527*9880d681SAndroid Build Coastguard Worker
528*9880d681SAndroid Build Coastguard Worker return true;
529*9880d681SAndroid Build Coastguard Worker }
530*9880d681SAndroid Build Coastguard Worker
531*9880d681SAndroid Build Coastguard Worker // Replace Branch with the compact branch instruction.
replaceWithCompactBranch(MachineBasicBlock & MBB,Iter Branch,const DebugLoc & DL)532*9880d681SAndroid Build Coastguard Worker Iter Filler::replaceWithCompactBranch(MachineBasicBlock &MBB, Iter Branch,
533*9880d681SAndroid Build Coastguard Worker const DebugLoc &DL) {
534*9880d681SAndroid Build Coastguard Worker const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
535*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII = STI.getInstrInfo();
536*9880d681SAndroid Build Coastguard Worker
537*9880d681SAndroid Build Coastguard Worker unsigned NewOpcode = TII->getEquivalentCompactForm(Branch);
538*9880d681SAndroid Build Coastguard Worker Branch = TII->genInstrWithNewOpc(NewOpcode, Branch);
539*9880d681SAndroid Build Coastguard Worker
540*9880d681SAndroid Build Coastguard Worker std::next(Branch)->eraseFromParent();
541*9880d681SAndroid Build Coastguard Worker return Branch;
542*9880d681SAndroid Build Coastguard Worker }
543*9880d681SAndroid Build Coastguard Worker
544*9880d681SAndroid Build Coastguard Worker // For given opcode returns opcode of corresponding instruction with short
545*9880d681SAndroid Build Coastguard Worker // delay slot.
getEquivalentCallShort(int Opcode)546*9880d681SAndroid Build Coastguard Worker static int getEquivalentCallShort(int Opcode) {
547*9880d681SAndroid Build Coastguard Worker switch (Opcode) {
548*9880d681SAndroid Build Coastguard Worker case Mips::BGEZAL:
549*9880d681SAndroid Build Coastguard Worker return Mips::BGEZALS_MM;
550*9880d681SAndroid Build Coastguard Worker case Mips::BLTZAL:
551*9880d681SAndroid Build Coastguard Worker return Mips::BLTZALS_MM;
552*9880d681SAndroid Build Coastguard Worker case Mips::JAL:
553*9880d681SAndroid Build Coastguard Worker return Mips::JALS_MM;
554*9880d681SAndroid Build Coastguard Worker case Mips::JALR:
555*9880d681SAndroid Build Coastguard Worker return Mips::JALRS_MM;
556*9880d681SAndroid Build Coastguard Worker case Mips::JALR16_MM:
557*9880d681SAndroid Build Coastguard Worker return Mips::JALRS16_MM;
558*9880d681SAndroid Build Coastguard Worker default:
559*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unexpected call instruction for microMIPS.");
560*9880d681SAndroid Build Coastguard Worker }
561*9880d681SAndroid Build Coastguard Worker }
562*9880d681SAndroid Build Coastguard Worker
563*9880d681SAndroid Build Coastguard Worker /// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
564*9880d681SAndroid Build Coastguard Worker /// We assume there is only one delay slot per delayed instruction.
runOnMachineBasicBlock(MachineBasicBlock & MBB)565*9880d681SAndroid Build Coastguard Worker bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
566*9880d681SAndroid Build Coastguard Worker bool Changed = false;
567*9880d681SAndroid Build Coastguard Worker const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
568*9880d681SAndroid Build Coastguard Worker bool InMicroMipsMode = STI.inMicroMipsMode();
569*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII = STI.getInstrInfo();
570*9880d681SAndroid Build Coastguard Worker
571*9880d681SAndroid Build Coastguard Worker if (InMicroMipsMode && STI.hasMips32r6()) {
572*9880d681SAndroid Build Coastguard Worker // This is microMIPS32r6 or microMIPS64r6 processor. Delay slot for
573*9880d681SAndroid Build Coastguard Worker // branching instructions is not needed.
574*9880d681SAndroid Build Coastguard Worker return Changed;
575*9880d681SAndroid Build Coastguard Worker }
576*9880d681SAndroid Build Coastguard Worker
577*9880d681SAndroid Build Coastguard Worker for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
578*9880d681SAndroid Build Coastguard Worker if (!hasUnoccupiedSlot(&*I))
579*9880d681SAndroid Build Coastguard Worker continue;
580*9880d681SAndroid Build Coastguard Worker
581*9880d681SAndroid Build Coastguard Worker ++FilledSlots;
582*9880d681SAndroid Build Coastguard Worker Changed = true;
583*9880d681SAndroid Build Coastguard Worker
584*9880d681SAndroid Build Coastguard Worker // Delay slot filling is disabled at -O0.
585*9880d681SAndroid Build Coastguard Worker if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
586*9880d681SAndroid Build Coastguard Worker bool Filled = false;
587*9880d681SAndroid Build Coastguard Worker
588*9880d681SAndroid Build Coastguard Worker if (MipsCompactBranchPolicy.getValue() != CB_Always ||
589*9880d681SAndroid Build Coastguard Worker !TII->getEquivalentCompactForm(I)) {
590*9880d681SAndroid Build Coastguard Worker if (searchBackward(MBB, I)) {
591*9880d681SAndroid Build Coastguard Worker Filled = true;
592*9880d681SAndroid Build Coastguard Worker } else if (I->isTerminator()) {
593*9880d681SAndroid Build Coastguard Worker if (searchSuccBBs(MBB, I)) {
594*9880d681SAndroid Build Coastguard Worker Filled = true;
595*9880d681SAndroid Build Coastguard Worker }
596*9880d681SAndroid Build Coastguard Worker } else if (searchForward(MBB, I)) {
597*9880d681SAndroid Build Coastguard Worker Filled = true;
598*9880d681SAndroid Build Coastguard Worker }
599*9880d681SAndroid Build Coastguard Worker }
600*9880d681SAndroid Build Coastguard Worker
601*9880d681SAndroid Build Coastguard Worker if (Filled) {
602*9880d681SAndroid Build Coastguard Worker // Get instruction with delay slot.
603*9880d681SAndroid Build Coastguard Worker MachineBasicBlock::instr_iterator DSI = I.getInstrIterator();
604*9880d681SAndroid Build Coastguard Worker
605*9880d681SAndroid Build Coastguard Worker if (InMicroMipsMode && TII->GetInstSizeInBytes(*std::next(DSI)) == 2 &&
606*9880d681SAndroid Build Coastguard Worker DSI->isCall()) {
607*9880d681SAndroid Build Coastguard Worker // If instruction in delay slot is 16b change opcode to
608*9880d681SAndroid Build Coastguard Worker // corresponding instruction with short delay slot.
609*9880d681SAndroid Build Coastguard Worker DSI->setDesc(TII->get(getEquivalentCallShort(DSI->getOpcode())));
610*9880d681SAndroid Build Coastguard Worker }
611*9880d681SAndroid Build Coastguard Worker continue;
612*9880d681SAndroid Build Coastguard Worker }
613*9880d681SAndroid Build Coastguard Worker }
614*9880d681SAndroid Build Coastguard Worker
615*9880d681SAndroid Build Coastguard Worker // For microMIPS if instruction is BEQ or BNE with one ZERO register, then
616*9880d681SAndroid Build Coastguard Worker // instead of adding NOP replace this instruction with the corresponding
617*9880d681SAndroid Build Coastguard Worker // compact branch instruction, i.e. BEQZC or BNEZC. Additionally
618*9880d681SAndroid Build Coastguard Worker // PseudoReturn and PseudoIndirectBranch are expanded to JR_MM, so they can
619*9880d681SAndroid Build Coastguard Worker // be replaced with JRC16_MM.
620*9880d681SAndroid Build Coastguard Worker
621*9880d681SAndroid Build Coastguard Worker // For MIPSR6 attempt to produce the corresponding compact (no delay slot)
622*9880d681SAndroid Build Coastguard Worker // form of the CTI. For indirect jumps this will not require inserting a
623*9880d681SAndroid Build Coastguard Worker // NOP and for branches will hopefully avoid requiring a NOP.
624*9880d681SAndroid Build Coastguard Worker if ((InMicroMipsMode ||
625*9880d681SAndroid Build Coastguard Worker (STI.hasMips32r6() && MipsCompactBranchPolicy != CB_Never)) &&
626*9880d681SAndroid Build Coastguard Worker TII->getEquivalentCompactForm(I)) {
627*9880d681SAndroid Build Coastguard Worker I = replaceWithCompactBranch(MBB, I, I->getDebugLoc());
628*9880d681SAndroid Build Coastguard Worker continue;
629*9880d681SAndroid Build Coastguard Worker }
630*9880d681SAndroid Build Coastguard Worker
631*9880d681SAndroid Build Coastguard Worker // Bundle the NOP to the instruction with the delay slot.
632*9880d681SAndroid Build Coastguard Worker BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
633*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(MBB, I, std::next(I, 2));
634*9880d681SAndroid Build Coastguard Worker }
635*9880d681SAndroid Build Coastguard Worker
636*9880d681SAndroid Build Coastguard Worker return Changed;
637*9880d681SAndroid Build Coastguard Worker }
638*9880d681SAndroid Build Coastguard Worker
639*9880d681SAndroid Build Coastguard Worker /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
640*9880d681SAndroid Build Coastguard Worker /// slots in Mips MachineFunctions
createMipsDelaySlotFillerPass(MipsTargetMachine & tm)641*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
642*9880d681SAndroid Build Coastguard Worker return new Filler(tm);
643*9880d681SAndroid Build Coastguard Worker }
644*9880d681SAndroid Build Coastguard Worker
645*9880d681SAndroid Build Coastguard Worker template<typename IterTy>
searchRange(MachineBasicBlock & MBB,IterTy Begin,IterTy End,RegDefsUses & RegDU,InspectMemInstr & IM,Iter Slot,IterTy & Filler) const646*9880d681SAndroid Build Coastguard Worker bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
647*9880d681SAndroid Build Coastguard Worker RegDefsUses &RegDU, InspectMemInstr& IM, Iter Slot,
648*9880d681SAndroid Build Coastguard Worker IterTy &Filler) const {
649*9880d681SAndroid Build Coastguard Worker bool IsReverseIter = std::is_convertible<IterTy, ReverseIter>::value;
650*9880d681SAndroid Build Coastguard Worker
651*9880d681SAndroid Build Coastguard Worker for (IterTy I = Begin; I != End;) {
652*9880d681SAndroid Build Coastguard Worker IterTy CurrI = I;
653*9880d681SAndroid Build Coastguard Worker ++I;
654*9880d681SAndroid Build Coastguard Worker
655*9880d681SAndroid Build Coastguard Worker // skip debug value
656*9880d681SAndroid Build Coastguard Worker if (CurrI->isDebugValue())
657*9880d681SAndroid Build Coastguard Worker continue;
658*9880d681SAndroid Build Coastguard Worker
659*9880d681SAndroid Build Coastguard Worker if (terminateSearch(*CurrI))
660*9880d681SAndroid Build Coastguard Worker break;
661*9880d681SAndroid Build Coastguard Worker
662*9880d681SAndroid Build Coastguard Worker assert((!CurrI->isCall() && !CurrI->isReturn() && !CurrI->isBranch()) &&
663*9880d681SAndroid Build Coastguard Worker "Cannot put calls, returns or branches in delay slot.");
664*9880d681SAndroid Build Coastguard Worker
665*9880d681SAndroid Build Coastguard Worker if (CurrI->isKill()) {
666*9880d681SAndroid Build Coastguard Worker CurrI->eraseFromParent();
667*9880d681SAndroid Build Coastguard Worker
668*9880d681SAndroid Build Coastguard Worker // This special case is needed for reverse iterators, because when we
669*9880d681SAndroid Build Coastguard Worker // erase an instruction, the iterators are updated to point to the next
670*9880d681SAndroid Build Coastguard Worker // instruction.
671*9880d681SAndroid Build Coastguard Worker if (IsReverseIter && I != End)
672*9880d681SAndroid Build Coastguard Worker I = CurrI;
673*9880d681SAndroid Build Coastguard Worker continue;
674*9880d681SAndroid Build Coastguard Worker }
675*9880d681SAndroid Build Coastguard Worker
676*9880d681SAndroid Build Coastguard Worker if (delayHasHazard(*CurrI, RegDU, IM))
677*9880d681SAndroid Build Coastguard Worker continue;
678*9880d681SAndroid Build Coastguard Worker
679*9880d681SAndroid Build Coastguard Worker const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
680*9880d681SAndroid Build Coastguard Worker if (STI.isTargetNaCl()) {
681*9880d681SAndroid Build Coastguard Worker // In NaCl, instructions that must be masked are forbidden in delay slots.
682*9880d681SAndroid Build Coastguard Worker // We only check for loads, stores and SP changes. Calls, returns and
683*9880d681SAndroid Build Coastguard Worker // branches are not checked because non-NaCl targets never put them in
684*9880d681SAndroid Build Coastguard Worker // delay slots.
685*9880d681SAndroid Build Coastguard Worker unsigned AddrIdx;
686*9880d681SAndroid Build Coastguard Worker if ((isBasePlusOffsetMemoryAccess(CurrI->getOpcode(), &AddrIdx) &&
687*9880d681SAndroid Build Coastguard Worker baseRegNeedsLoadStoreMask(CurrI->getOperand(AddrIdx).getReg())) ||
688*9880d681SAndroid Build Coastguard Worker CurrI->modifiesRegister(Mips::SP, STI.getRegisterInfo()))
689*9880d681SAndroid Build Coastguard Worker continue;
690*9880d681SAndroid Build Coastguard Worker }
691*9880d681SAndroid Build Coastguard Worker
692*9880d681SAndroid Build Coastguard Worker bool InMicroMipsMode = STI.inMicroMipsMode();
693*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII = STI.getInstrInfo();
694*9880d681SAndroid Build Coastguard Worker unsigned Opcode = (*Slot).getOpcode();
695*9880d681SAndroid Build Coastguard Worker if (InMicroMipsMode && TII->GetInstSizeInBytes(*CurrI) == 2 &&
696*9880d681SAndroid Build Coastguard Worker (Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch ||
697*9880d681SAndroid Build Coastguard Worker Opcode == Mips::PseudoReturn))
698*9880d681SAndroid Build Coastguard Worker continue;
699*9880d681SAndroid Build Coastguard Worker
700*9880d681SAndroid Build Coastguard Worker Filler = CurrI;
701*9880d681SAndroid Build Coastguard Worker return true;
702*9880d681SAndroid Build Coastguard Worker }
703*9880d681SAndroid Build Coastguard Worker
704*9880d681SAndroid Build Coastguard Worker return false;
705*9880d681SAndroid Build Coastguard Worker }
706*9880d681SAndroid Build Coastguard Worker
searchBackward(MachineBasicBlock & MBB,Iter Slot) const707*9880d681SAndroid Build Coastguard Worker bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
708*9880d681SAndroid Build Coastguard Worker if (DisableBackwardSearch)
709*9880d681SAndroid Build Coastguard Worker return false;
710*9880d681SAndroid Build Coastguard Worker
711*9880d681SAndroid Build Coastguard Worker auto *Fn = MBB.getParent();
712*9880d681SAndroid Build Coastguard Worker RegDefsUses RegDU(*Fn->getSubtarget().getRegisterInfo());
713*9880d681SAndroid Build Coastguard Worker MemDefsUses MemDU(Fn->getDataLayout(), Fn->getFrameInfo());
714*9880d681SAndroid Build Coastguard Worker ReverseIter Filler;
715*9880d681SAndroid Build Coastguard Worker
716*9880d681SAndroid Build Coastguard Worker RegDU.init(*Slot);
717*9880d681SAndroid Build Coastguard Worker
718*9880d681SAndroid Build Coastguard Worker if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Slot,
719*9880d681SAndroid Build Coastguard Worker Filler))
720*9880d681SAndroid Build Coastguard Worker return false;
721*9880d681SAndroid Build Coastguard Worker
722*9880d681SAndroid Build Coastguard Worker MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
723*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
724*9880d681SAndroid Build Coastguard Worker ++UsefulSlots;
725*9880d681SAndroid Build Coastguard Worker return true;
726*9880d681SAndroid Build Coastguard Worker }
727*9880d681SAndroid Build Coastguard Worker
searchForward(MachineBasicBlock & MBB,Iter Slot) const728*9880d681SAndroid Build Coastguard Worker bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
729*9880d681SAndroid Build Coastguard Worker // Can handle only calls.
730*9880d681SAndroid Build Coastguard Worker if (DisableForwardSearch || !Slot->isCall())
731*9880d681SAndroid Build Coastguard Worker return false;
732*9880d681SAndroid Build Coastguard Worker
733*9880d681SAndroid Build Coastguard Worker RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
734*9880d681SAndroid Build Coastguard Worker NoMemInstr NM;
735*9880d681SAndroid Build Coastguard Worker Iter Filler;
736*9880d681SAndroid Build Coastguard Worker
737*9880d681SAndroid Build Coastguard Worker RegDU.setCallerSaved(*Slot);
738*9880d681SAndroid Build Coastguard Worker
739*9880d681SAndroid Build Coastguard Worker if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Slot, Filler))
740*9880d681SAndroid Build Coastguard Worker return false;
741*9880d681SAndroid Build Coastguard Worker
742*9880d681SAndroid Build Coastguard Worker MBB.splice(std::next(Slot), &MBB, Filler);
743*9880d681SAndroid Build Coastguard Worker MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
744*9880d681SAndroid Build Coastguard Worker ++UsefulSlots;
745*9880d681SAndroid Build Coastguard Worker return true;
746*9880d681SAndroid Build Coastguard Worker }
747*9880d681SAndroid Build Coastguard Worker
searchSuccBBs(MachineBasicBlock & MBB,Iter Slot) const748*9880d681SAndroid Build Coastguard Worker bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
749*9880d681SAndroid Build Coastguard Worker if (DisableSuccBBSearch)
750*9880d681SAndroid Build Coastguard Worker return false;
751*9880d681SAndroid Build Coastguard Worker
752*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *SuccBB = selectSuccBB(MBB);
753*9880d681SAndroid Build Coastguard Worker
754*9880d681SAndroid Build Coastguard Worker if (!SuccBB)
755*9880d681SAndroid Build Coastguard Worker return false;
756*9880d681SAndroid Build Coastguard Worker
757*9880d681SAndroid Build Coastguard Worker RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
758*9880d681SAndroid Build Coastguard Worker bool HasMultipleSuccs = false;
759*9880d681SAndroid Build Coastguard Worker BB2BrMap BrMap;
760*9880d681SAndroid Build Coastguard Worker std::unique_ptr<InspectMemInstr> IM;
761*9880d681SAndroid Build Coastguard Worker Iter Filler;
762*9880d681SAndroid Build Coastguard Worker auto *Fn = MBB.getParent();
763*9880d681SAndroid Build Coastguard Worker
764*9880d681SAndroid Build Coastguard Worker // Iterate over SuccBB's predecessor list.
765*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
766*9880d681SAndroid Build Coastguard Worker PE = SuccBB->pred_end(); PI != PE; ++PI)
767*9880d681SAndroid Build Coastguard Worker if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
768*9880d681SAndroid Build Coastguard Worker return false;
769*9880d681SAndroid Build Coastguard Worker
770*9880d681SAndroid Build Coastguard Worker // Do not allow moving instructions which have unallocatable register operands
771*9880d681SAndroid Build Coastguard Worker // across basic block boundaries.
772*9880d681SAndroid Build Coastguard Worker RegDU.setUnallocatableRegs(*Fn);
773*9880d681SAndroid Build Coastguard Worker
774*9880d681SAndroid Build Coastguard Worker // Only allow moving loads from stack or constants if any of the SuccBB's
775*9880d681SAndroid Build Coastguard Worker // predecessors have multiple successors.
776*9880d681SAndroid Build Coastguard Worker if (HasMultipleSuccs) {
777*9880d681SAndroid Build Coastguard Worker IM.reset(new LoadFromStackOrConst());
778*9880d681SAndroid Build Coastguard Worker } else {
779*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI = Fn->getFrameInfo();
780*9880d681SAndroid Build Coastguard Worker IM.reset(new MemDefsUses(Fn->getDataLayout(), MFI));
781*9880d681SAndroid Build Coastguard Worker }
782*9880d681SAndroid Build Coastguard Worker
783*9880d681SAndroid Build Coastguard Worker if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Slot,
784*9880d681SAndroid Build Coastguard Worker Filler))
785*9880d681SAndroid Build Coastguard Worker return false;
786*9880d681SAndroid Build Coastguard Worker
787*9880d681SAndroid Build Coastguard Worker insertDelayFiller(Filler, BrMap);
788*9880d681SAndroid Build Coastguard Worker addLiveInRegs(Filler, *SuccBB);
789*9880d681SAndroid Build Coastguard Worker Filler->eraseFromParent();
790*9880d681SAndroid Build Coastguard Worker
791*9880d681SAndroid Build Coastguard Worker return true;
792*9880d681SAndroid Build Coastguard Worker }
793*9880d681SAndroid Build Coastguard Worker
selectSuccBB(MachineBasicBlock & B) const794*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
795*9880d681SAndroid Build Coastguard Worker if (B.succ_empty())
796*9880d681SAndroid Build Coastguard Worker return nullptr;
797*9880d681SAndroid Build Coastguard Worker
798*9880d681SAndroid Build Coastguard Worker // Select the successor with the larget edge weight.
799*9880d681SAndroid Build Coastguard Worker auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
800*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *S = *std::max_element(
801*9880d681SAndroid Build Coastguard Worker B.succ_begin(), B.succ_end(),
802*9880d681SAndroid Build Coastguard Worker [&](const MachineBasicBlock *Dst0, const MachineBasicBlock *Dst1) {
803*9880d681SAndroid Build Coastguard Worker return Prob.getEdgeProbability(&B, Dst0) <
804*9880d681SAndroid Build Coastguard Worker Prob.getEdgeProbability(&B, Dst1);
805*9880d681SAndroid Build Coastguard Worker });
806*9880d681SAndroid Build Coastguard Worker return S->isEHPad() ? nullptr : S;
807*9880d681SAndroid Build Coastguard Worker }
808*9880d681SAndroid Build Coastguard Worker
809*9880d681SAndroid Build Coastguard Worker std::pair<MipsInstrInfo::BranchType, MachineInstr *>
getBranch(MachineBasicBlock & MBB,const MachineBasicBlock & Dst) const810*9880d681SAndroid Build Coastguard Worker Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
811*9880d681SAndroid Build Coastguard Worker const MipsInstrInfo *TII =
812*9880d681SAndroid Build Coastguard Worker MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
813*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr;
814*9880d681SAndroid Build Coastguard Worker SmallVector<MachineInstr*, 2> BranchInstrs;
815*9880d681SAndroid Build Coastguard Worker SmallVector<MachineOperand, 2> Cond;
816*9880d681SAndroid Build Coastguard Worker
817*9880d681SAndroid Build Coastguard Worker MipsInstrInfo::BranchType R =
818*9880d681SAndroid Build Coastguard Worker TII->analyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
819*9880d681SAndroid Build Coastguard Worker
820*9880d681SAndroid Build Coastguard Worker if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
821*9880d681SAndroid Build Coastguard Worker return std::make_pair(R, nullptr);
822*9880d681SAndroid Build Coastguard Worker
823*9880d681SAndroid Build Coastguard Worker if (R != MipsInstrInfo::BT_CondUncond) {
824*9880d681SAndroid Build Coastguard Worker if (!hasUnoccupiedSlot(BranchInstrs[0]))
825*9880d681SAndroid Build Coastguard Worker return std::make_pair(MipsInstrInfo::BT_None, nullptr);
826*9880d681SAndroid Build Coastguard Worker
827*9880d681SAndroid Build Coastguard Worker assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
828*9880d681SAndroid Build Coastguard Worker
829*9880d681SAndroid Build Coastguard Worker return std::make_pair(R, BranchInstrs[0]);
830*9880d681SAndroid Build Coastguard Worker }
831*9880d681SAndroid Build Coastguard Worker
832*9880d681SAndroid Build Coastguard Worker assert((TrueBB == &Dst) || (FalseBB == &Dst));
833*9880d681SAndroid Build Coastguard Worker
834*9880d681SAndroid Build Coastguard Worker // Examine the conditional branch. See if its slot is occupied.
835*9880d681SAndroid Build Coastguard Worker if (hasUnoccupiedSlot(BranchInstrs[0]))
836*9880d681SAndroid Build Coastguard Worker return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
837*9880d681SAndroid Build Coastguard Worker
838*9880d681SAndroid Build Coastguard Worker // If that fails, try the unconditional branch.
839*9880d681SAndroid Build Coastguard Worker if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
840*9880d681SAndroid Build Coastguard Worker return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
841*9880d681SAndroid Build Coastguard Worker
842*9880d681SAndroid Build Coastguard Worker return std::make_pair(MipsInstrInfo::BT_None, nullptr);
843*9880d681SAndroid Build Coastguard Worker }
844*9880d681SAndroid Build Coastguard Worker
examinePred(MachineBasicBlock & Pred,const MachineBasicBlock & Succ,RegDefsUses & RegDU,bool & HasMultipleSuccs,BB2BrMap & BrMap) const845*9880d681SAndroid Build Coastguard Worker bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
846*9880d681SAndroid Build Coastguard Worker RegDefsUses &RegDU, bool &HasMultipleSuccs,
847*9880d681SAndroid Build Coastguard Worker BB2BrMap &BrMap) const {
848*9880d681SAndroid Build Coastguard Worker std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
849*9880d681SAndroid Build Coastguard Worker getBranch(Pred, Succ);
850*9880d681SAndroid Build Coastguard Worker
851*9880d681SAndroid Build Coastguard Worker // Return if either getBranch wasn't able to analyze the branches or there
852*9880d681SAndroid Build Coastguard Worker // were no branches with unoccupied slots.
853*9880d681SAndroid Build Coastguard Worker if (P.first == MipsInstrInfo::BT_None)
854*9880d681SAndroid Build Coastguard Worker return false;
855*9880d681SAndroid Build Coastguard Worker
856*9880d681SAndroid Build Coastguard Worker if ((P.first != MipsInstrInfo::BT_Uncond) &&
857*9880d681SAndroid Build Coastguard Worker (P.first != MipsInstrInfo::BT_NoBranch)) {
858*9880d681SAndroid Build Coastguard Worker HasMultipleSuccs = true;
859*9880d681SAndroid Build Coastguard Worker RegDU.addLiveOut(Pred, Succ);
860*9880d681SAndroid Build Coastguard Worker }
861*9880d681SAndroid Build Coastguard Worker
862*9880d681SAndroid Build Coastguard Worker BrMap[&Pred] = P.second;
863*9880d681SAndroid Build Coastguard Worker return true;
864*9880d681SAndroid Build Coastguard Worker }
865*9880d681SAndroid Build Coastguard Worker
delayHasHazard(const MachineInstr & Candidate,RegDefsUses & RegDU,InspectMemInstr & IM) const866*9880d681SAndroid Build Coastguard Worker bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
867*9880d681SAndroid Build Coastguard Worker InspectMemInstr &IM) const {
868*9880d681SAndroid Build Coastguard Worker assert(!Candidate.isKill() &&
869*9880d681SAndroid Build Coastguard Worker "KILL instructions should have been eliminated at this point.");
870*9880d681SAndroid Build Coastguard Worker
871*9880d681SAndroid Build Coastguard Worker bool HasHazard = Candidate.isImplicitDef();
872*9880d681SAndroid Build Coastguard Worker
873*9880d681SAndroid Build Coastguard Worker HasHazard |= IM.hasHazard(Candidate);
874*9880d681SAndroid Build Coastguard Worker HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
875*9880d681SAndroid Build Coastguard Worker
876*9880d681SAndroid Build Coastguard Worker return HasHazard;
877*9880d681SAndroid Build Coastguard Worker }
878*9880d681SAndroid Build Coastguard Worker
terminateSearch(const MachineInstr & Candidate) const879*9880d681SAndroid Build Coastguard Worker bool Filler::terminateSearch(const MachineInstr &Candidate) const {
880*9880d681SAndroid Build Coastguard Worker return (Candidate.isTerminator() || Candidate.isCall() ||
881*9880d681SAndroid Build Coastguard Worker Candidate.isPosition() || Candidate.isInlineAsm() ||
882*9880d681SAndroid Build Coastguard Worker Candidate.hasUnmodeledSideEffects());
883*9880d681SAndroid Build Coastguard Worker }
884