blob: 0a7ffb101536a0a438091aaf0b4b7e9fa86c982b [file] [log] [blame]
Akira Hatanaka1083eb12013-02-14 23:20:15 +00001//===-- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler ------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00007//
Akira Hatanakae2489122011-04-15 21:51:11 +00008//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +00009//
Akira Hatanaka1083eb12013-02-14 23:20:15 +000010// Simple pass to fill delay slots with useful instructions.
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000011//
Akira Hatanakae2489122011-04-15 21:51:11 +000012//===----------------------------------------------------------------------===//
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000013
Sasa Stankovic5fddf612014-03-10 20:34:23 +000014#include "MCTargetDesc/MipsMCNaCl.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000015#include "Mips.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000016#include "MipsInstrInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000017#include "MipsTargetMachine.h"
Akira Hatanaka06bd1382013-02-14 23:40:57 +000018#include "llvm/ADT/BitVector.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000019#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000021#include "llvm/Analysis/AliasAnalysis.h"
22#include "llvm/Analysis/ValueTracking.h"
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000023#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000024#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
Daniel Sanders308181e2014-06-12 10:44:10 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Akira Hatanakaeb33ced2013-03-01 00:16:31 +000027#include "llvm/CodeGen/PseudoSourceValue.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000028#include "llvm/Support/CommandLine.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000029#include "llvm/Target/TargetInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Target/TargetMachine.h"
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000031#include "llvm/Target/TargetRegisterInfo.h"
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000032
33using namespace llvm;
34
Chandler Carruth84e68b22014-04-22 02:41:26 +000035#define DEBUG_TYPE "delay-slot-filler"
36
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000037STATISTIC(FilledSlots, "Number of delay slots filled");
Akira Hatanaka9e603442011-10-05 01:19:13 +000038STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that"
Akira Hatanaka02e760a2011-10-05 02:22:49 +000039 " are not NOP.");
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000040
Akira Hatanaka9d957842012-08-22 02:51:28 +000041static cl::opt<bool> DisableDelaySlotFiller(
42 "disable-mips-delay-filler",
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000043 cl::init(false),
Akira Hatanaka1083eb12013-02-14 23:20:15 +000044 cl::desc("Fill all delay slots with NOPs."),
Akira Hatanakaf2619ee2011-09-29 23:52:13 +000045 cl::Hidden);
46
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000047static cl::opt<bool> DisableForwardSearch(
48 "disable-mips-df-forward-search",
49 cl::init(true),
50 cl::desc("Disallow MIPS delay filler to search forward."),
51 cl::Hidden);
52
Akira Hatanakae44e30c2013-03-01 01:02:36 +000053static cl::opt<bool> DisableSuccBBSearch(
54 "disable-mips-df-succbb-search",
55 cl::init(true),
56 cl::desc("Disallow MIPS delay filler to search successor basic blocks."),
57 cl::Hidden);
58
59static cl::opt<bool> DisableBackwardSearch(
60 "disable-mips-df-backward-search",
61 cl::init(false),
62 cl::desc("Disallow MIPS delay filler to search backward."),
63 cl::Hidden);
64
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +000065namespace {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000066 typedef MachineBasicBlock::iterator Iter;
67 typedef MachineBasicBlock::reverse_iterator ReverseIter;
68 typedef SmallDenseMap<MachineBasicBlock*, MachineInstr*, 2> BB2BrMap;
69
Akira Hatanaka979899e2013-02-26 01:30:05 +000070 class RegDefsUses {
71 public:
Eric Christopher96e72c62015-01-29 23:27:36 +000072 RegDefsUses(const TargetRegisterInfo &TRI);
Akira Hatanaka979899e2013-02-26 01:30:05 +000073 void init(const MachineInstr &MI);
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000074
75 /// This function sets all caller-saved registers in Defs.
76 void setCallerSaved(const MachineInstr &MI);
77
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +000078 /// This function sets all unallocatable registers in Defs.
79 void setUnallocatableRegs(const MachineFunction &MF);
80
81 /// Set bits in Uses corresponding to MBB's live-out registers except for
82 /// the registers that are live-in to SuccBB.
83 void addLiveOut(const MachineBasicBlock &MBB,
84 const MachineBasicBlock &SuccBB);
85
Akira Hatanaka979899e2013-02-26 01:30:05 +000086 bool update(const MachineInstr &MI, unsigned Begin, unsigned End);
87
88 private:
89 bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg,
90 bool IsDef) const;
91
92 /// Returns true if Reg or its alias is in RegSet.
93 bool isRegInSet(const BitVector &RegSet, unsigned Reg) const;
94
95 const TargetRegisterInfo &TRI;
96 BitVector Defs, Uses;
97 };
98
Akira Hatanakae01ff9d2013-03-01 00:50:52 +000099 /// Base class for inspecting loads and stores.
100 class InspectMemInstr {
101 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000102 InspectMemInstr(bool ForbidMemInstr_)
103 : OrigSeenLoad(false), OrigSeenStore(false), SeenLoad(false),
104 SeenStore(false), ForbidMemInstr(ForbidMemInstr_) {}
105
106 /// Return true if MI cannot be moved to delay slot.
107 bool hasHazard(const MachineInstr &MI);
108
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000109 virtual ~InspectMemInstr() {}
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000110
111 protected:
112 /// Flags indicating whether loads or stores have been seen.
113 bool OrigSeenLoad, OrigSeenStore, SeenLoad, SeenStore;
114
115 /// Memory instructions are not allowed to move to delay slot if this flag
116 /// is true.
117 bool ForbidMemInstr;
118
119 private:
120 virtual bool hasHazard_(const MachineInstr &MI) = 0;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000121 };
122
123 /// This subclass rejects any memory instructions.
124 class NoMemInstr : public InspectMemInstr {
125 public:
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000126 NoMemInstr() : InspectMemInstr(true) {}
127 private:
Craig Topper56c590a2014-04-29 07:58:02 +0000128 bool hasHazard_(const MachineInstr &MI) override { return true; }
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000129 };
130
131 /// This subclass accepts loads from stacks and constant loads.
132 class LoadFromStackOrConst : public InspectMemInstr {
133 public:
134 LoadFromStackOrConst() : InspectMemInstr(false) {}
135 private:
Craig Topper56c590a2014-04-29 07:58:02 +0000136 bool hasHazard_(const MachineInstr &MI) override;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000137 };
138
139 /// This subclass uses memory dependence information to determine whether a
140 /// memory instruction can be moved to a delay slot.
141 class MemDefsUses : public InspectMemInstr {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000142 public:
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000143 MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI);
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000144
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000145 private:
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000146 typedef PointerUnion<const Value *, const PseudoSourceValue *> ValueType;
147
Craig Topper56c590a2014-04-29 07:58:02 +0000148 bool hasHazard_(const MachineInstr &MI) override;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000149
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000150 /// Update Defs and Uses. Return true if there exist dependences that
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000151 /// disqualify the delay slot candidate between V and values in Uses and
152 /// Defs.
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000153 bool updateDefsUses(ValueType V, bool MayStore);
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000154
155 /// Get the list of underlying objects of MI's memory operand.
156 bool getUnderlyingObjects(const MachineInstr &MI,
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000157 SmallVectorImpl<ValueType> &Objects) const;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000158
159 const MachineFrameInfo *MFI;
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000160 SmallPtrSet<ValueType, 4> Uses, Defs;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000161 const DataLayout &DL;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000162
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000163 /// Flags indicating whether loads or stores with no underlying objects have
164 /// been seen.
165 bool SeenNoObjLoad, SeenNoObjStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000166 };
167
Akira Hatanakaa0612812013-02-07 21:32:32 +0000168 class Filler : public MachineFunctionPass {
169 public:
Bruno Cardoso Lopesfde21cf2010-12-09 17:31:11 +0000170 Filler(TargetMachine &tm)
Bill Wendlingead89ef2013-06-07 07:04:14 +0000171 : MachineFunctionPass(ID), TM(tm) { }
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000172
Craig Topper56c590a2014-04-29 07:58:02 +0000173 const char *getPassName() const override {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000174 return "Mips Delay Slot Filler";
175 }
176
Craig Topper56c590a2014-04-29 07:58:02 +0000177 bool runOnMachineFunction(MachineFunction &F) override {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000178 bool Changed = false;
179 for (MachineFunction::iterator FI = F.begin(), FE = F.end();
180 FI != FE; ++FI)
181 Changed |= runOnMachineBasicBlock(*FI);
Daniel Sanders308181e2014-06-12 10:44:10 +0000182
183 // This pass invalidates liveness information when it reorders
184 // instructions to fill delay slot. Without this, -verify-machineinstrs
185 // will fail.
186 if (Changed)
187 F.getRegInfo().invalidateLiveness();
188
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000189 return Changed;
190 }
191
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000192 MachineFunctionProperties getRequiredProperties() const override {
193 return MachineFunctionProperties().set(
194 MachineFunctionProperties::Property::AllVRegsAllocated);
195 }
196
Craig Topper56c590a2014-04-29 07:58:02 +0000197 void getAnalysisUsage(AnalysisUsage &AU) const override {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000198 AU.addRequired<MachineBranchProbabilityInfo>();
199 MachineFunctionPass::getAnalysisUsage(AU);
200 }
Akira Hatanakaa0612812013-02-07 21:32:32 +0000201
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000202 private:
Akira Hatanakaa0612812013-02-07 21:32:32 +0000203 bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
204
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000205 Iter replaceWithCompactBranch(MachineBasicBlock &MBB,
206 Iter Branch, DebugLoc DL);
207
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000208 /// This function checks if it is valid to move Candidate to the delay slot
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000209 /// and returns true if it isn't. It also updates memory and register
210 /// dependence information.
211 bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000212 InspectMemInstr &IM) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000213
Akira Hatanakaf815db52013-03-01 00:26:14 +0000214 /// This function searches range [Begin, End) for an instruction that can be
215 /// moved to the delay slot. Returns true on success.
216 template<typename IterTy>
217 bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000218 RegDefsUses &RegDU, InspectMemInstr &IM, Iter Slot,
219 IterTy &Filler) const;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000220
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000221 /// This function searches in the backward direction for an instruction that
222 /// can be moved to the delay slot. Returns true on success.
223 bool searchBackward(MachineBasicBlock &MBB, Iter Slot) const;
224
225 /// This function searches MBB in the forward direction for an instruction
226 /// that can be moved to the delay slot. Returns true on success.
227 bool searchForward(MachineBasicBlock &MBB, Iter Slot) const;
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000228
Akira Hatanaka1ff803f2013-03-25 20:11:16 +0000229 /// This function searches one of MBB's successor blocks for an instruction
230 /// that can be moved to the delay slot and inserts clones of the
231 /// instruction into the successor's predecessor blocks.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000232 bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const;
233
Akira Hatanakae9e588d2013-03-01 02:17:02 +0000234 /// Pick a successor block of MBB. Return NULL if MBB doesn't have a
235 /// successor block that is not a landing pad.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000236 MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const;
237
238 /// This function analyzes MBB and returns an instruction with an unoccupied
239 /// slot that branches to Dst.
240 std::pair<MipsInstrInfo::BranchType, MachineInstr *>
241 getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const;
242
243 /// Examine Pred and see if it is possible to insert an instruction into
244 /// one of its branches delay slot or its end.
245 bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
246 RegDefsUses &RegDU, bool &HasMultipleSuccs,
247 BB2BrMap &BrMap) const;
248
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000249 bool terminateSearch(const MachineInstr &Candidate) const;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000250
Akira Hatanakaa0612812013-02-07 21:32:32 +0000251 TargetMachine &TM;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000252
Akira Hatanakaa0612812013-02-07 21:32:32 +0000253 static char ID;
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000254 };
255 char Filler::ID = 0;
256} // end of anonymous namespace
257
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000258static bool hasUnoccupiedSlot(const MachineInstr *MI) {
259 return MI->hasDelaySlot() && !MI->isBundledWithSucc();
260}
261
262/// This function inserts clones of Filler into predecessor blocks.
263static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) {
264 MachineFunction *MF = Filler->getParent()->getParent();
265
266 for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) {
267 if (I->second) {
268 MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler));
269 ++UsefulSlots;
270 } else {
271 I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler));
272 }
273 }
274}
275
276/// This function adds registers Filler defines to MBB's live-in register list.
277static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) {
278 for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) {
279 const MachineOperand &MO = Filler->getOperand(I);
280 unsigned R;
281
282 if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg()))
283 continue;
284
285#ifndef NDEBUG
286 const MachineFunction &MF = *MBB.getParent();
Eric Christopher96e72c62015-01-29 23:27:36 +0000287 assert(MF.getSubtarget().getRegisterInfo()->getAllocatableSet(MF).test(R) &&
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000288 "Shouldn't move an instruction with unallocatable registers across "
289 "basic block boundaries.");
290#endif
291
292 if (!MBB.isLiveIn(R))
293 MBB.addLiveIn(R);
294 }
295}
296
Eric Christopher96e72c62015-01-29 23:27:36 +0000297RegDefsUses::RegDefsUses(const TargetRegisterInfo &TRI)
298 : TRI(TRI), Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {}
Akira Hatanaka979899e2013-02-26 01:30:05 +0000299
300void RegDefsUses::init(const MachineInstr &MI) {
301 // Add all register operands which are explicit and non-variadic.
302 update(MI, 0, MI.getDesc().getNumOperands());
303
304 // If MI is a call, add RA to Defs to prevent users of RA from going into
305 // delay slot.
306 if (MI.isCall())
307 Defs.set(Mips::RA);
308
309 // Add all implicit register operands of branch instructions except
310 // register AT.
311 if (MI.isBranch()) {
312 update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands());
313 Defs.reset(Mips::AT);
314 }
315}
316
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000317void RegDefsUses::setCallerSaved(const MachineInstr &MI) {
318 assert(MI.isCall());
319
Vasileios Kalintiris70b744e2015-05-14 13:17:56 +0000320 // Add RA/RA_64 to Defs to prevent users of RA/RA_64 from going into
321 // the delay slot. The reason is that RA/RA_64 must not be changed
322 // in the delay slot so that the callee can return to the caller.
323 if (MI.definesRegister(Mips::RA) || MI.definesRegister(Mips::RA_64)) {
324 Defs.set(Mips::RA);
325 Defs.set(Mips::RA_64);
326 }
327
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000328 // If MI is a call, add all caller-saved registers to Defs.
329 BitVector CallerSavedRegs(TRI.getNumRegs(), true);
330
331 CallerSavedRegs.reset(Mips::ZERO);
332 CallerSavedRegs.reset(Mips::ZERO_64);
333
Eric Christopher7af952872015-03-11 21:41:28 +0000334 for (const MCPhysReg *R = TRI.getCalleeSavedRegs(MI.getParent()->getParent());
335 *R; ++R)
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000336 for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI)
337 CallerSavedRegs.reset(*AI);
338
339 Defs |= CallerSavedRegs;
340}
341
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000342void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) {
343 BitVector AllocSet = TRI.getAllocatableSet(MF);
344
345 for (int R = AllocSet.find_first(); R != -1; R = AllocSet.find_next(R))
346 for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI)
347 AllocSet.set(*AI);
348
349 AllocSet.set(Mips::ZERO);
350 AllocSet.set(Mips::ZERO_64);
351
352 Defs |= AllocSet.flip();
353}
354
355void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB,
356 const MachineBasicBlock &SuccBB) {
357 for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(),
358 SE = MBB.succ_end(); SI != SE; ++SI)
359 if (*SI != &SuccBB)
Matthias Braund9da1622015-09-09 18:08:03 +0000360 for (const auto &LI : (*SI)->liveins())
361 Uses.set(LI.PhysReg);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000362}
363
Akira Hatanaka979899e2013-02-26 01:30:05 +0000364bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) {
365 BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs());
366 bool HasHazard = false;
367
368 for (unsigned I = Begin; I != End; ++I) {
369 const MachineOperand &MO = MI.getOperand(I);
370
371 if (MO.isReg() && MO.getReg())
372 HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef());
373 }
374
375 Defs |= NewDefs;
376 Uses |= NewUses;
377
378 return HasHazard;
379}
380
381bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses,
382 unsigned Reg, bool IsDef) const {
383 if (IsDef) {
384 NewDefs.set(Reg);
385 // check whether Reg has already been defined or used.
386 return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg));
387 }
388
389 NewUses.set(Reg);
390 // check whether Reg has already been defined.
391 return isRegInSet(Defs, Reg);
392}
393
394bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const {
395 // Check Reg and all aliased Registers.
396 for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI)
397 if (RegSet.test(*AI))
398 return true;
399 return false;
400}
401
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000402bool InspectMemInstr::hasHazard(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000403 if (!MI.mayStore() && !MI.mayLoad())
404 return false;
405
406 if (ForbidMemInstr)
407 return true;
408
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000409 OrigSeenLoad = SeenLoad;
410 OrigSeenStore = SeenStore;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000411 SeenLoad |= MI.mayLoad();
412 SeenStore |= MI.mayStore();
413
414 // If MI is an ordered or volatile memory reference, disallow moving
415 // subsequent loads and stores to delay slot.
416 if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) {
417 ForbidMemInstr = true;
418 return true;
419 }
420
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000421 return hasHazard_(MI);
422}
423
424bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) {
425 if (MI.mayStore())
426 return true;
427
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000428 if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue())
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000429 return true;
430
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000431 if (const PseudoSourceValue *PSV =
432 (*MI.memoperands_begin())->getPseudoValue()) {
433 if (isa<FixedStackPseudoSourceValue>(PSV))
434 return false;
Alex Lorenze40c8a22015-08-11 23:09:45 +0000435 return !PSV->isConstant(nullptr) && !PSV->isStack();
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000436 }
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000437
438 return true;
439}
440
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000441MemDefsUses::MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI_)
442 : InspectMemInstr(false), MFI(MFI_), DL(DL), SeenNoObjLoad(false),
443 SeenNoObjStore(false) {}
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000444
445bool MemDefsUses::hasHazard_(const MachineInstr &MI) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000446 bool HasHazard = false;
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000447 SmallVector<ValueType, 4> Objs;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000448
449 // Check underlying object list.
450 if (getUnderlyingObjects(MI, Objs)) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000451 for (SmallVectorImpl<ValueType>::const_iterator I = Objs.begin();
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000452 I != Objs.end(); ++I)
453 HasHazard |= updateDefsUses(*I, MI.mayStore());
454
455 return HasHazard;
456 }
457
458 // No underlying objects found.
459 HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore);
460 HasHazard |= MI.mayLoad() || OrigSeenStore;
461
462 SeenNoObjLoad |= MI.mayLoad();
463 SeenNoObjStore |= MI.mayStore();
464
465 return HasHazard;
466}
467
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000468bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) {
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000469 if (MayStore)
David Blaikie70573dc2014-11-19 07:49:26 +0000470 return !Defs.insert(V).second || Uses.count(V) || SeenNoObjStore ||
471 SeenNoObjLoad;
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000472
473 Uses.insert(V);
474 return Defs.count(V) || SeenNoObjStore;
475}
476
477bool MemDefsUses::
478getUnderlyingObjects(const MachineInstr &MI,
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000479 SmallVectorImpl<ValueType> &Objects) const {
480 if (!MI.hasOneMemOperand() ||
481 (!(*MI.memoperands_begin())->getValue() &&
482 !(*MI.memoperands_begin())->getPseudoValue()))
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000483 return false;
484
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000485 if (const PseudoSourceValue *PSV =
486 (*MI.memoperands_begin())->getPseudoValue()) {
487 if (!PSV->isAliased(MFI))
488 return false;
489 Objects.push_back(PSV);
490 return true;
491 }
492
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000493 const Value *V = (*MI.memoperands_begin())->getValue();
494
495 SmallVector<Value *, 4> Objs;
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000496 GetUnderlyingObjects(const_cast<Value *>(V), Objs, DL);
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000497
Craig Topper31ee5862013-07-03 15:07:05 +0000498 for (SmallVectorImpl<Value *>::iterator I = Objs.begin(), E = Objs.end();
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000499 I != E; ++I) {
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000500 if (!isIdentifiedObject(V))
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000501 return false;
502
503 Objects.push_back(*I);
504 }
505
506 return true;
507}
508
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000509// Replace Branch with the compact branch instruction.
510Iter Filler::replaceWithCompactBranch(MachineBasicBlock &MBB,
511 Iter Branch, DebugLoc DL) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000512 const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
513 const MipsInstrInfo *TII = STI.getInstrInfo();
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000514
Daniel Sanderse8efff32016-03-14 16:24:05 +0000515 unsigned NewOpcode = TII->getEquivalentCompactForm(Branch);
516 Branch = TII->genInstrWithNewOpc(NewOpcode, Branch);
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000517
Daniel Sanderse8efff32016-03-14 16:24:05 +0000518 std::next(Branch)->eraseFromParent();
Jozef Kolek3b8ddb62014-11-21 22:04:35 +0000519 return Branch;
520}
521
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000522// For given opcode returns opcode of corresponding instruction with short
523// delay slot.
524static int getEquivalentCallShort(int Opcode) {
525 switch (Opcode) {
526 case Mips::BGEZAL:
527 return Mips::BGEZALS_MM;
528 case Mips::BLTZAL:
529 return Mips::BLTZALS_MM;
530 case Mips::JAL:
531 return Mips::JALS_MM;
532 case Mips::JALR:
533 return Mips::JALRS_MM;
534 case Mips::JALR16_MM:
535 return Mips::JALRS16_MM;
536 default:
537 llvm_unreachable("Unexpected call instruction for microMIPS.");
538 }
539}
540
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000541/// runOnMachineBasicBlock - Fill in delay slots for the given basic block.
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000542/// We assume there is only one delay slot per delayed instruction.
Akira Hatanaka1083eb12013-02-14 23:20:15 +0000543bool Filler::runOnMachineBasicBlock(MachineBasicBlock &MBB) {
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000544 bool Changed = false;
Eric Christopher6b6db772015-02-02 23:03:43 +0000545 const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
Eric Christopher96e72c62015-01-29 23:27:36 +0000546 bool InMicroMipsMode = STI.inMicroMipsMode();
547 const MipsInstrInfo *TII = STI.getInstrInfo();
Akira Hatanakae7b06972011-10-05 01:30:09 +0000548
Hrvoje Vargac45baf22016-03-23 10:29:38 +0000549 if (InMicroMipsMode && STI.hasMips32r6()) {
550 // This is microMIPS32r6 or microMIPS64r6 processor. Delay slot for
551 // branching instructions is not needed.
552 return Changed;
553 }
554
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000555 for (Iter I = MBB.begin(); I != MBB.end(); ++I) {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000556 if (!hasUnoccupiedSlot(&*I))
Akira Hatanakaa0612812013-02-07 21:32:32 +0000557 continue;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000558
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000559 ++FilledSlots;
560 Changed = true;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000561
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000562 // Delay slot filling is disabled at -O0.
563 if (!DisableDelaySlotFiller && (TM.getOptLevel() != CodeGenOpt::None)) {
564 bool Filled = false;
Zoran Jovanovic37bca102014-11-10 17:27:56 +0000565
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000566 if (searchBackward(MBB, I)) {
567 Filled = true;
568 } else if (I->isTerminator()) {
569 if (searchSuccBBs(MBB, I)) {
570 Filled = true;
Zoran Jovanovic37bca102014-11-10 17:27:56 +0000571 }
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000572 } else if (searchForward(MBB, I)) {
573 Filled = true;
574 }
575
576 if (Filled) {
577 // Get instruction with delay slot.
578 MachineBasicBlock::instr_iterator DSI(I);
579
Duncan P. N. Exon Smith78691482015-10-20 00:15:20 +0000580 if (InMicroMipsMode && TII->GetInstSizeInBytes(&*std::next(DSI)) == 2 &&
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000581 DSI->isCall()) {
582 // If instruction in delay slot is 16b change opcode to
583 // corresponding instruction with short delay slot.
584 DSI->setDesc(TII->get(getEquivalentCallShort(DSI->getOpcode())));
585 }
Zoran Jovanovicb554bba2014-11-25 10:50:00 +0000586 continue;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000587 }
588 }
Akira Hatanaka5ac78682012-06-13 23:25:52 +0000589
Simon Dardisd9d41f52016-04-05 12:50:29 +0000590 // For microMIPS if instruction is BEQ or BNE with one ZERO register, then
591 // instead of adding NOP replace this instruction with the corresponding
592 // compact branch instruction, i.e. BEQZC or BNEZC. Additionally
593 // PseudoReturn and PseudoIndirectBranch are expanded to JR_MM, so they can
594 // be replaced with JRC16_MM.
Daniel Sanderse8efff32016-03-14 16:24:05 +0000595
596 // For MIPSR6 attempt to produce the corresponding compact (no delay slot)
Simon Dardisd9d41f52016-04-05 12:50:29 +0000597 // form of the CTI. For indirect jumps this will not require inserting a
598 // NOP and for branches will hopefully avoid requiring a NOP.
599 if ((InMicroMipsMode || STI.hasMips32r6()) &&
600 TII->getEquivalentCompactForm(I)) {
Daniel Sanderse8efff32016-03-14 16:24:05 +0000601 I = replaceWithCompactBranch(MBB, I, I->getDebugLoc());
602 continue;
603 }
604
Jozef Kolek650a61a2015-02-13 17:51:27 +0000605 // Bundle the NOP to the instruction with the delay slot.
606 BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP));
607 MIBundleBuilder(MBB, I, std::next(I, 2));
Akira Hatanakaa0612812013-02-07 21:32:32 +0000608 }
609
Bruno Cardoso Lopes0b97ce72007-08-18 01:50:47 +0000610 return Changed;
611}
612
613/// createMipsDelaySlotFillerPass - Returns a pass that fills in delay
614/// slots in Mips MachineFunctions
615FunctionPass *llvm::createMipsDelaySlotFillerPass(MipsTargetMachine &tm) {
616 return new Filler(tm);
617}
618
Akira Hatanakaf815db52013-03-01 00:26:14 +0000619template<typename IterTy>
620bool Filler::searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End,
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000621 RegDefsUses &RegDU, InspectMemInstr& IM, Iter Slot,
622 IterTy &Filler) const {
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000623 bool IsReverseIter = std::is_convertible<IterTy, ReverseIter>::value;
624
625 for (IterTy I = Begin; I != End;) {
626 IterTy CurrI = I;
627 ++I;
628
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000629 // skip debug value
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000630 if (CurrI->isDebugValue())
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000631 continue;
632
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000633 if (terminateSearch(*CurrI))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000634 break;
635
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000636 assert((!CurrI->isCall() && !CurrI->isReturn() && !CurrI->isBranch()) &&
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000637 "Cannot put calls, returns or branches in delay slot.");
638
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000639 if (CurrI->isKill()) {
640 CurrI->eraseFromParent();
641
642 // This special case is needed for reverse iterators, because when we
643 // erase an instruction, the iterators are updated to point to the next
644 // instruction.
645 if (IsReverseIter && I != End)
646 I = CurrI;
647 continue;
648 }
649
650 if (delayHasHazard(*CurrI, RegDU, IM))
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000651 continue;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000652
Eric Christopher6b6db772015-02-02 23:03:43 +0000653 const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>();
654 if (STI.isTargetNaCl()) {
Sasa Stankovic5fddf612014-03-10 20:34:23 +0000655 // In NaCl, instructions that must be masked are forbidden in delay slots.
656 // We only check for loads, stores and SP changes. Calls, returns and
657 // branches are not checked because non-NaCl targets never put them in
658 // delay slots.
659 unsigned AddrIdx;
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000660 if ((isBasePlusOffsetMemoryAccess(CurrI->getOpcode(), &AddrIdx) &&
661 baseRegNeedsLoadStoreMask(CurrI->getOperand(AddrIdx).getReg())) ||
662 CurrI->modifiesRegister(Mips::SP, STI.getRegisterInfo()))
Sasa Stankovic5fddf612014-03-10 20:34:23 +0000663 continue;
664 }
665
Eric Christopher6b6db772015-02-02 23:03:43 +0000666 bool InMicroMipsMode = STI.inMicroMipsMode();
667 const MipsInstrInfo *TII = STI.getInstrInfo();
Jozef Koleke7cad7a2015-01-13 15:59:17 +0000668 unsigned Opcode = (*Slot).getOpcode();
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000669 if (InMicroMipsMode && TII->GetInstSizeInBytes(&(*CurrI)) == 2 &&
Jozef Koleke7cad7a2015-01-13 15:59:17 +0000670 (Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch ||
671 Opcode == Mips::PseudoReturn))
672 continue;
673
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000674 Filler = CurrI;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000675 return true;
676 }
677
678 return false;
679}
680
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000681bool Filler::searchBackward(MachineBasicBlock &MBB, Iter Slot) const {
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000682 if (DisableBackwardSearch)
683 return false;
684
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000685 auto *Fn = MBB.getParent();
686 RegDefsUses RegDU(*Fn->getSubtarget().getRegisterInfo());
687 MemDefsUses MemDU(Fn->getDataLayout(), Fn->getFrameInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000688 ReverseIter Filler;
Akira Hatanakaf815db52013-03-01 00:26:14 +0000689
690 RegDU.init(*Slot);
691
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000692 if (!searchRange(MBB, ReverseIter(Slot), MBB.rend(), RegDU, MemDU, Slot,
693 Filler))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000694 return false;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000695
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000696 MBB.splice(std::next(Slot), &MBB, std::next(Filler).base());
697 MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000698 ++UsefulSlots;
699 return true;
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000700}
701
702bool Filler::searchForward(MachineBasicBlock &MBB, Iter Slot) const {
703 // Can handle only calls.
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000704 if (DisableForwardSearch || !Slot->isCall())
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000705 return false;
706
Eric Christopher96e72c62015-01-29 23:27:36 +0000707 RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000708 NoMemInstr NM;
709 Iter Filler;
710
711 RegDU.setCallerSaved(*Slot);
712
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000713 if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Slot, Filler))
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000714 return false;
Akira Hatanaka5d4e4ea2011-10-05 01:23:39 +0000715
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000716 MBB.splice(std::next(Slot), &MBB, Filler);
717 MIBundleBuilder(MBB, Slot, std::next(Slot, 2));
Akira Hatanaka4c0a7122013-10-07 19:33:02 +0000718 ++UsefulSlots;
719 return true;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000720}
721
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000722bool Filler::searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const {
723 if (DisableSuccBBSearch)
724 return false;
725
726 MachineBasicBlock *SuccBB = selectSuccBB(MBB);
727
728 if (!SuccBB)
729 return false;
730
Eric Christopher96e72c62015-01-29 23:27:36 +0000731 RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo());
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000732 bool HasMultipleSuccs = false;
733 BB2BrMap BrMap;
Benjamin Kramerd2da7202014-04-21 09:34:48 +0000734 std::unique_ptr<InspectMemInstr> IM;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000735 Iter Filler;
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000736 auto *Fn = MBB.getParent();
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000737
738 // Iterate over SuccBB's predecessor list.
739 for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(),
740 PE = SuccBB->pred_end(); PI != PE; ++PI)
741 if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap))
742 return false;
743
744 // Do not allow moving instructions which have unallocatable register operands
745 // across basic block boundaries.
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000746 RegDU.setUnallocatableRegs(*Fn);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000747
748 // Only allow moving loads from stack or constants if any of the SuccBB's
749 // predecessors have multiple successors.
750 if (HasMultipleSuccs) {
751 IM.reset(new LoadFromStackOrConst());
752 } else {
Mehdi Aminibd7287e2015-07-16 06:11:10 +0000753 const MachineFrameInfo *MFI = Fn->getFrameInfo();
754 IM.reset(new MemDefsUses(Fn->getDataLayout(), MFI));
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000755 }
756
Vasileios Kalintiris87614902015-03-04 12:37:58 +0000757 if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Slot,
758 Filler))
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000759 return false;
760
761 insertDelayFiller(Filler, BrMap);
762 addLiveInRegs(Filler, *SuccBB);
763 Filler->eraseFromParent();
764
765 return true;
766}
767
768MachineBasicBlock *Filler::selectSuccBB(MachineBasicBlock &B) const {
769 if (B.succ_empty())
Craig Topper062a2ba2014-04-25 05:30:21 +0000770 return nullptr;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000771
772 // Select the successor with the larget edge weight.
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000773 auto &Prob = getAnalysis<MachineBranchProbabilityInfo>();
Cong Hou1938f2e2015-11-24 08:51:23 +0000774 MachineBasicBlock *S = *std::max_element(
775 B.succ_begin(), B.succ_end(),
776 [&](const MachineBasicBlock *Dst0, const MachineBasicBlock *Dst1) {
777 return Prob.getEdgeProbability(&B, Dst0) <
778 Prob.getEdgeProbability(&B, Dst1);
779 });
Reid Kleckner0e288232015-08-27 23:27:47 +0000780 return S->isEHPad() ? nullptr : S;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000781}
782
783std::pair<MipsInstrInfo::BranchType, MachineInstr *>
784Filler::getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const {
Eric Christopher6b6db772015-02-02 23:03:43 +0000785 const MipsInstrInfo *TII =
786 MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo();
Craig Topper062a2ba2014-04-25 05:30:21 +0000787 MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr;
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000788 SmallVector<MachineInstr*, 2> BranchInstrs;
789 SmallVector<MachineOperand, 2> Cond;
790
791 MipsInstrInfo::BranchType R =
792 TII->AnalyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs);
793
794 if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch))
Craig Topper062a2ba2014-04-25 05:30:21 +0000795 return std::make_pair(R, nullptr);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000796
797 if (R != MipsInstrInfo::BT_CondUncond) {
798 if (!hasUnoccupiedSlot(BranchInstrs[0]))
Craig Topper062a2ba2014-04-25 05:30:21 +0000799 return std::make_pair(MipsInstrInfo::BT_None, nullptr);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000800
801 assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst)));
802
803 return std::make_pair(R, BranchInstrs[0]);
804 }
805
806 assert((TrueBB == &Dst) || (FalseBB == &Dst));
807
808 // Examine the conditional branch. See if its slot is occupied.
809 if (hasUnoccupiedSlot(BranchInstrs[0]))
810 return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]);
811
812 // If that fails, try the unconditional branch.
813 if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst))
814 return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]);
815
Craig Topper062a2ba2014-04-25 05:30:21 +0000816 return std::make_pair(MipsInstrInfo::BT_None, nullptr);
Akira Hatanaka8f7bfb32013-03-01 02:03:51 +0000817}
818
819bool Filler::examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ,
820 RegDefsUses &RegDU, bool &HasMultipleSuccs,
821 BB2BrMap &BrMap) const {
822 std::pair<MipsInstrInfo::BranchType, MachineInstr *> P =
823 getBranch(Pred, Succ);
824
825 // Return if either getBranch wasn't able to analyze the branches or there
826 // were no branches with unoccupied slots.
827 if (P.first == MipsInstrInfo::BT_None)
828 return false;
829
830 if ((P.first != MipsInstrInfo::BT_Uncond) &&
831 (P.first != MipsInstrInfo::BT_NoBranch)) {
832 HasMultipleSuccs = true;
833 RegDU.addLiveOut(Pred, Succ);
834 }
835
836 BrMap[&Pred] = P.second;
837 return true;
838}
839
Akira Hatanakaeb33ced2013-03-01 00:16:31 +0000840bool Filler::delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU,
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000841 InspectMemInstr &IM) const {
Vasileios Kalintirisbb60cfb2015-04-17 12:01:02 +0000842 assert(!Candidate.isKill() &&
843 "KILL instructions should have been eliminated at this point.");
844
845 bool HasHazard = Candidate.isImplicitDef();
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000846
Akira Hatanakae01ff9d2013-03-01 00:50:52 +0000847 HasHazard |= IM.hasHazard(Candidate);
Akira Hatanaka979899e2013-02-26 01:30:05 +0000848 HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands());
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000849
Akira Hatanaka06bd1382013-02-14 23:40:57 +0000850 return HasHazard;
Akira Hatanakaf2619ee2011-09-29 23:52:13 +0000851}
852
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000853bool Filler::terminateSearch(const MachineInstr &Candidate) const {
854 return (Candidate.isTerminator() || Candidate.isCall() ||
Rafael Espindolab1f25f12014-03-07 06:08:31 +0000855 Candidate.isPosition() || Candidate.isInlineAsm() ||
Akira Hatanakadfd2f242013-02-14 23:11:24 +0000856 Candidate.hasUnmodeledSideEffects());
857}