Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 1 | //===- MipsDelaySlotFiller.cpp - Mips Delay Slot Filler -------------------===// |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 6 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 8 | // |
Akira Hatanaka | 1083eb1 | 2013-02-14 23:20:15 +0000 | [diff] [blame] | 9 | // Simple pass to fill delay slots with useful instructions. |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 10 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 11 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 12 | |
Sasa Stankovic | 5fddf61 | 2014-03-10 20:34:23 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/MipsMCNaCl.h" |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 14 | #include "Mips.h" |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 15 | #include "MipsInstrInfo.h" |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 16 | #include "MipsRegisterInfo.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 17 | #include "MipsSubtarget.h" |
Akira Hatanaka | 06bd138 | 2013-02-14 23:40:57 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/BitVector.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/PointerUnion.h" |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallPtrSet.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringRef.h" |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/AliasAnalysis.h" |
| 26 | #include "llvm/Analysis/ValueTracking.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunction.h" |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstr.h" |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineOperand.h" |
Daniel Sanders | 308181e | 2014-06-12 10:44:10 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/PseudoSourceValue.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 37 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCInstrDesc.h" |
| 39 | #include "llvm/MC/MCRegisterInfo.h" |
| 40 | #include "llvm/Support/Casting.h" |
| 41 | #include "llvm/Support/CodeGen.h" |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 45 | #include <algorithm> |
| 46 | #include <cassert> |
| 47 | #include <iterator> |
| 48 | #include <memory> |
| 49 | #include <utility> |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 50 | |
| 51 | using namespace llvm; |
| 52 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 53 | #define DEBUG_TYPE "mips-delay-slot-filler" |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 54 | |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 55 | STATISTIC(FilledSlots, "Number of delay slots filled"); |
Akira Hatanaka | 9e60344 | 2011-10-05 01:19:13 +0000 | [diff] [blame] | 56 | STATISTIC(UsefulSlots, "Number of delay slots filled with instructions that" |
Akira Hatanaka | 02e760a | 2011-10-05 02:22:49 +0000 | [diff] [blame] | 57 | " are not NOP."); |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 58 | |
Akira Hatanaka | 9d95784 | 2012-08-22 02:51:28 +0000 | [diff] [blame] | 59 | static cl::opt<bool> DisableDelaySlotFiller( |
| 60 | "disable-mips-delay-filler", |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 61 | cl::init(false), |
Akira Hatanaka | 1083eb1 | 2013-02-14 23:20:15 +0000 | [diff] [blame] | 62 | cl::desc("Fill all delay slots with NOPs."), |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 63 | cl::Hidden); |
| 64 | |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 65 | static cl::opt<bool> DisableForwardSearch( |
| 66 | "disable-mips-df-forward-search", |
| 67 | cl::init(true), |
| 68 | cl::desc("Disallow MIPS delay filler to search forward."), |
| 69 | cl::Hidden); |
| 70 | |
Akira Hatanaka | e44e30c | 2013-03-01 01:02:36 +0000 | [diff] [blame] | 71 | static cl::opt<bool> DisableSuccBBSearch( |
| 72 | "disable-mips-df-succbb-search", |
| 73 | cl::init(true), |
| 74 | cl::desc("Disallow MIPS delay filler to search successor basic blocks."), |
| 75 | cl::Hidden); |
| 76 | |
| 77 | static cl::opt<bool> DisableBackwardSearch( |
| 78 | "disable-mips-df-backward-search", |
| 79 | cl::init(false), |
| 80 | cl::desc("Disallow MIPS delay filler to search backward."), |
| 81 | cl::Hidden); |
| 82 | |
Simon Dardis | 8d8f2f8 | 2016-05-17 10:21:43 +0000 | [diff] [blame] | 83 | enum CompactBranchPolicy { |
| 84 | CB_Never, ///< The policy 'never' may in some circumstances or for some |
| 85 | ///< ISAs not be absolutely adhered to. |
| 86 | CB_Optimal, ///< Optimal is the default and will produce compact branches |
| 87 | ///< when delay slots cannot be filled. |
| 88 | CB_Always ///< 'always' may in some circumstances may not be |
| 89 | ///< absolutely adhered to there may not be a corresponding |
| 90 | ///< compact form of a branch. |
| 91 | }; |
| 92 | |
| 93 | static cl::opt<CompactBranchPolicy> MipsCompactBranchPolicy( |
| 94 | "mips-compact-branches",cl::Optional, |
| 95 | cl::init(CB_Optimal), |
| 96 | cl::desc("MIPS Specific: Compact branch policy."), |
| 97 | cl::values( |
| 98 | clEnumValN(CB_Never, "never", "Do not use compact branches if possible."), |
| 99 | clEnumValN(CB_Optimal, "optimal", "Use compact branches where appropiate (default)."), |
Mehdi Amini | 732afdd | 2016-10-08 19:41:06 +0000 | [diff] [blame] | 100 | clEnumValN(CB_Always, "always", "Always use compact branches if possible.") |
Simon Dardis | 8d8f2f8 | 2016-05-17 10:21:43 +0000 | [diff] [blame] | 101 | ) |
| 102 | ); |
| 103 | |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 104 | namespace { |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 105 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 106 | using Iter = MachineBasicBlock::iterator; |
| 107 | using ReverseIter = MachineBasicBlock::reverse_iterator; |
| 108 | using BB2BrMap = SmallDenseMap<MachineBasicBlock *, MachineInstr *, 2>; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 109 | |
Akira Hatanaka | 979899e | 2013-02-26 01:30:05 +0000 | [diff] [blame] | 110 | class RegDefsUses { |
| 111 | public: |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 112 | RegDefsUses(const TargetRegisterInfo &TRI); |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 113 | |
Akira Hatanaka | 979899e | 2013-02-26 01:30:05 +0000 | [diff] [blame] | 114 | void init(const MachineInstr &MI); |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 115 | |
| 116 | /// This function sets all caller-saved registers in Defs. |
| 117 | void setCallerSaved(const MachineInstr &MI); |
| 118 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 119 | /// This function sets all unallocatable registers in Defs. |
| 120 | void setUnallocatableRegs(const MachineFunction &MF); |
| 121 | |
| 122 | /// Set bits in Uses corresponding to MBB's live-out registers except for |
| 123 | /// the registers that are live-in to SuccBB. |
| 124 | void addLiveOut(const MachineBasicBlock &MBB, |
| 125 | const MachineBasicBlock &SuccBB); |
| 126 | |
Akira Hatanaka | 979899e | 2013-02-26 01:30:05 +0000 | [diff] [blame] | 127 | bool update(const MachineInstr &MI, unsigned Begin, unsigned End); |
| 128 | |
| 129 | private: |
| 130 | bool checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, unsigned Reg, |
| 131 | bool IsDef) const; |
| 132 | |
| 133 | /// Returns true if Reg or its alias is in RegSet. |
| 134 | bool isRegInSet(const BitVector &RegSet, unsigned Reg) const; |
| 135 | |
| 136 | const TargetRegisterInfo &TRI; |
| 137 | BitVector Defs, Uses; |
| 138 | }; |
| 139 | |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 140 | /// Base class for inspecting loads and stores. |
| 141 | class InspectMemInstr { |
| 142 | public: |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 143 | InspectMemInstr(bool ForbidMemInstr_) : ForbidMemInstr(ForbidMemInstr_) {} |
| 144 | virtual ~InspectMemInstr() = default; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 145 | |
| 146 | /// Return true if MI cannot be moved to delay slot. |
| 147 | bool hasHazard(const MachineInstr &MI); |
| 148 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 149 | protected: |
| 150 | /// Flags indicating whether loads or stores have been seen. |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 151 | bool OrigSeenLoad = false; |
| 152 | bool OrigSeenStore = false; |
| 153 | bool SeenLoad = false; |
| 154 | bool SeenStore = false; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 155 | |
| 156 | /// Memory instructions are not allowed to move to delay slot if this flag |
| 157 | /// is true. |
| 158 | bool ForbidMemInstr; |
| 159 | |
| 160 | private: |
| 161 | virtual bool hasHazard_(const MachineInstr &MI) = 0; |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | /// This subclass rejects any memory instructions. |
| 165 | class NoMemInstr : public InspectMemInstr { |
| 166 | public: |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 167 | NoMemInstr() : InspectMemInstr(true) {} |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 168 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 169 | private: |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 170 | bool hasHazard_(const MachineInstr &MI) override { return true; } |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | /// This subclass accepts loads from stacks and constant loads. |
| 174 | class LoadFromStackOrConst : public InspectMemInstr { |
| 175 | public: |
| 176 | LoadFromStackOrConst() : InspectMemInstr(false) {} |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 177 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 178 | private: |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 179 | bool hasHazard_(const MachineInstr &MI) override; |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | /// This subclass uses memory dependence information to determine whether a |
| 183 | /// memory instruction can be moved to a delay slot. |
| 184 | class MemDefsUses : public InspectMemInstr { |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 185 | public: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 186 | MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI); |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 187 | |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 188 | private: |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 189 | using ValueType = PointerUnion<const Value *, const PseudoSourceValue *>; |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 190 | |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 191 | bool hasHazard_(const MachineInstr &MI) override; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 192 | |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 193 | /// Update Defs and Uses. Return true if there exist dependences that |
Akira Hatanaka | e9e588d | 2013-03-01 02:17:02 +0000 | [diff] [blame] | 194 | /// disqualify the delay slot candidate between V and values in Uses and |
| 195 | /// Defs. |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 196 | bool updateDefsUses(ValueType V, bool MayStore); |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 197 | |
| 198 | /// Get the list of underlying objects of MI's memory operand. |
| 199 | bool getUnderlyingObjects(const MachineInstr &MI, |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 200 | SmallVectorImpl<ValueType> &Objects) const; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 201 | |
| 202 | const MachineFrameInfo *MFI; |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 203 | SmallPtrSet<ValueType, 4> Uses, Defs; |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 204 | const DataLayout &DL; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 205 | |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 206 | /// Flags indicating whether loads or stores with no underlying objects have |
| 207 | /// been seen. |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 208 | bool SeenNoObjLoad = false; |
| 209 | bool SeenNoObjStore = false; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 212 | class MipsDelaySlotFiller : public MachineFunctionPass { |
Akira Hatanaka | a061281 | 2013-02-07 21:32:32 +0000 | [diff] [blame] | 213 | public: |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 214 | MipsDelaySlotFiller() : MachineFunctionPass(ID) { |
| 215 | initializeMipsDelaySlotFillerPass(*PassRegistry::getPassRegistry()); |
| 216 | } |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 217 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 218 | StringRef getPassName() const override { return "Mips Delay Slot Filler"; } |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 219 | |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 220 | bool runOnMachineFunction(MachineFunction &F) override { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 221 | TM = &F.getTarget(); |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 222 | bool Changed = false; |
| 223 | for (MachineFunction::iterator FI = F.begin(), FE = F.end(); |
| 224 | FI != FE; ++FI) |
| 225 | Changed |= runOnMachineBasicBlock(*FI); |
Daniel Sanders | 308181e | 2014-06-12 10:44:10 +0000 | [diff] [blame] | 226 | |
| 227 | // This pass invalidates liveness information when it reorders |
| 228 | // instructions to fill delay slot. Without this, -verify-machineinstrs |
| 229 | // will fail. |
| 230 | if (Changed) |
| 231 | F.getRegInfo().invalidateLiveness(); |
| 232 | |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 233 | return Changed; |
| 234 | } |
| 235 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 236 | MachineFunctionProperties getRequiredProperties() const override { |
| 237 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 238 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 241 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 242 | AU.addRequired<MachineBranchProbabilityInfo>(); |
| 243 | MachineFunctionPass::getAnalysisUsage(AU); |
| 244 | } |
Akira Hatanaka | a061281 | 2013-02-07 21:32:32 +0000 | [diff] [blame] | 245 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 246 | static char ID; |
| 247 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 248 | private: |
Akira Hatanaka | a061281 | 2013-02-07 21:32:32 +0000 | [diff] [blame] | 249 | bool runOnMachineBasicBlock(MachineBasicBlock &MBB); |
| 250 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 251 | Iter replaceWithCompactBranch(MachineBasicBlock &MBB, Iter Branch, |
| 252 | const DebugLoc &DL); |
Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 253 | |
Akira Hatanaka | 06bd138 | 2013-02-14 23:40:57 +0000 | [diff] [blame] | 254 | /// This function checks if it is valid to move Candidate to the delay slot |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 255 | /// and returns true if it isn't. It also updates memory and register |
| 256 | /// dependence information. |
| 257 | bool delayHasHazard(const MachineInstr &Candidate, RegDefsUses &RegDU, |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 258 | InspectMemInstr &IM) const; |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 259 | |
Akira Hatanaka | f815db5 | 2013-03-01 00:26:14 +0000 | [diff] [blame] | 260 | /// This function searches range [Begin, End) for an instruction that can be |
| 261 | /// moved to the delay slot. Returns true on success. |
| 262 | template<typename IterTy> |
| 263 | bool searchRange(MachineBasicBlock &MBB, IterTy Begin, IterTy End, |
Vasileios Kalintiris | 8761490 | 2015-03-04 12:37:58 +0000 | [diff] [blame] | 264 | RegDefsUses &RegDU, InspectMemInstr &IM, Iter Slot, |
| 265 | IterTy &Filler) const; |
Akira Hatanaka | f815db5 | 2013-03-01 00:26:14 +0000 | [diff] [blame] | 266 | |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 267 | /// This function searches in the backward direction for an instruction that |
| 268 | /// can be moved to the delay slot. Returns true on success. |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 269 | bool searchBackward(MachineBasicBlock &MBB, MachineInstr &Slot) const; |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 270 | |
| 271 | /// This function searches MBB in the forward direction for an instruction |
| 272 | /// that can be moved to the delay slot. Returns true on success. |
| 273 | bool searchForward(MachineBasicBlock &MBB, Iter Slot) const; |
Akira Hatanaka | dfd2f24 | 2013-02-14 23:11:24 +0000 | [diff] [blame] | 274 | |
Akira Hatanaka | 1ff803f | 2013-03-25 20:11:16 +0000 | [diff] [blame] | 275 | /// This function searches one of MBB's successor blocks for an instruction |
| 276 | /// that can be moved to the delay slot and inserts clones of the |
| 277 | /// instruction into the successor's predecessor blocks. |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 278 | bool searchSuccBBs(MachineBasicBlock &MBB, Iter Slot) const; |
| 279 | |
Akira Hatanaka | e9e588d | 2013-03-01 02:17:02 +0000 | [diff] [blame] | 280 | /// Pick a successor block of MBB. Return NULL if MBB doesn't have a |
| 281 | /// successor block that is not a landing pad. |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 282 | MachineBasicBlock *selectSuccBB(MachineBasicBlock &B) const; |
| 283 | |
| 284 | /// This function analyzes MBB and returns an instruction with an unoccupied |
| 285 | /// slot that branches to Dst. |
| 286 | std::pair<MipsInstrInfo::BranchType, MachineInstr *> |
| 287 | getBranch(MachineBasicBlock &MBB, const MachineBasicBlock &Dst) const; |
| 288 | |
| 289 | /// Examine Pred and see if it is possible to insert an instruction into |
| 290 | /// one of its branches delay slot or its end. |
| 291 | bool examinePred(MachineBasicBlock &Pred, const MachineBasicBlock &Succ, |
| 292 | RegDefsUses &RegDU, bool &HasMultipleSuccs, |
| 293 | BB2BrMap &BrMap) const; |
| 294 | |
Akira Hatanaka | dfd2f24 | 2013-02-14 23:11:24 +0000 | [diff] [blame] | 295 | bool terminateSearch(const MachineInstr &Candidate) const; |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 296 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 297 | const TargetMachine *TM = nullptr; |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 298 | }; |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 299 | |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 300 | } // end anonymous namespace |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 301 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 302 | char MipsDelaySlotFiller::ID = 0; |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 303 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 304 | static bool hasUnoccupiedSlot(const MachineInstr *MI) { |
| 305 | return MI->hasDelaySlot() && !MI->isBundledWithSucc(); |
| 306 | } |
| 307 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 308 | INITIALIZE_PASS(MipsDelaySlotFiller, DEBUG_TYPE, |
| 309 | "Fill delay slot for MIPS", false, false) |
| 310 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 311 | /// This function inserts clones of Filler into predecessor blocks. |
| 312 | static void insertDelayFiller(Iter Filler, const BB2BrMap &BrMap) { |
| 313 | MachineFunction *MF = Filler->getParent()->getParent(); |
| 314 | |
| 315 | for (BB2BrMap::const_iterator I = BrMap.begin(); I != BrMap.end(); ++I) { |
| 316 | if (I->second) { |
| 317 | MIBundleBuilder(I->second).append(MF->CloneMachineInstr(&*Filler)); |
| 318 | ++UsefulSlots; |
| 319 | } else { |
| 320 | I->first->insert(I->first->end(), MF->CloneMachineInstr(&*Filler)); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /// This function adds registers Filler defines to MBB's live-in register list. |
| 326 | static void addLiveInRegs(Iter Filler, MachineBasicBlock &MBB) { |
| 327 | for (unsigned I = 0, E = Filler->getNumOperands(); I != E; ++I) { |
| 328 | const MachineOperand &MO = Filler->getOperand(I); |
| 329 | unsigned R; |
| 330 | |
| 331 | if (!MO.isReg() || !MO.isDef() || !(R = MO.getReg())) |
| 332 | continue; |
| 333 | |
| 334 | #ifndef NDEBUG |
| 335 | const MachineFunction &MF = *MBB.getParent(); |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 336 | assert(MF.getSubtarget().getRegisterInfo()->getAllocatableSet(MF).test(R) && |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 337 | "Shouldn't move an instruction with unallocatable registers across " |
| 338 | "basic block boundaries."); |
| 339 | #endif |
| 340 | |
| 341 | if (!MBB.isLiveIn(R)) |
| 342 | MBB.addLiveIn(R); |
| 343 | } |
| 344 | } |
| 345 | |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 346 | RegDefsUses::RegDefsUses(const TargetRegisterInfo &TRI) |
| 347 | : TRI(TRI), Defs(TRI.getNumRegs(), false), Uses(TRI.getNumRegs(), false) {} |
Akira Hatanaka | 979899e | 2013-02-26 01:30:05 +0000 | [diff] [blame] | 348 | |
| 349 | void RegDefsUses::init(const MachineInstr &MI) { |
| 350 | // Add all register operands which are explicit and non-variadic. |
| 351 | update(MI, 0, MI.getDesc().getNumOperands()); |
| 352 | |
| 353 | // If MI is a call, add RA to Defs to prevent users of RA from going into |
| 354 | // delay slot. |
| 355 | if (MI.isCall()) |
| 356 | Defs.set(Mips::RA); |
| 357 | |
| 358 | // Add all implicit register operands of branch instructions except |
| 359 | // register AT. |
| 360 | if (MI.isBranch()) { |
| 361 | update(MI, MI.getDesc().getNumOperands(), MI.getNumOperands()); |
| 362 | Defs.reset(Mips::AT); |
| 363 | } |
| 364 | } |
| 365 | |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 366 | void RegDefsUses::setCallerSaved(const MachineInstr &MI) { |
| 367 | assert(MI.isCall()); |
| 368 | |
Vasileios Kalintiris | 70b744e | 2015-05-14 13:17:56 +0000 | [diff] [blame] | 369 | // Add RA/RA_64 to Defs to prevent users of RA/RA_64 from going into |
| 370 | // the delay slot. The reason is that RA/RA_64 must not be changed |
| 371 | // in the delay slot so that the callee can return to the caller. |
| 372 | if (MI.definesRegister(Mips::RA) || MI.definesRegister(Mips::RA_64)) { |
| 373 | Defs.set(Mips::RA); |
| 374 | Defs.set(Mips::RA_64); |
| 375 | } |
| 376 | |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 377 | // If MI is a call, add all caller-saved registers to Defs. |
| 378 | BitVector CallerSavedRegs(TRI.getNumRegs(), true); |
| 379 | |
| 380 | CallerSavedRegs.reset(Mips::ZERO); |
| 381 | CallerSavedRegs.reset(Mips::ZERO_64); |
| 382 | |
Eric Christopher | 7af95287 | 2015-03-11 21:41:28 +0000 | [diff] [blame] | 383 | for (const MCPhysReg *R = TRI.getCalleeSavedRegs(MI.getParent()->getParent()); |
| 384 | *R; ++R) |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 385 | for (MCRegAliasIterator AI(*R, &TRI, true); AI.isValid(); ++AI) |
| 386 | CallerSavedRegs.reset(*AI); |
| 387 | |
| 388 | Defs |= CallerSavedRegs; |
| 389 | } |
| 390 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 391 | void RegDefsUses::setUnallocatableRegs(const MachineFunction &MF) { |
| 392 | BitVector AllocSet = TRI.getAllocatableSet(MF); |
| 393 | |
Francis Visoiu Mistrih | b52e036 | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 394 | for (unsigned R : AllocSet.set_bits()) |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 395 | for (MCRegAliasIterator AI(R, &TRI, false); AI.isValid(); ++AI) |
| 396 | AllocSet.set(*AI); |
| 397 | |
| 398 | AllocSet.set(Mips::ZERO); |
| 399 | AllocSet.set(Mips::ZERO_64); |
| 400 | |
| 401 | Defs |= AllocSet.flip(); |
| 402 | } |
| 403 | |
| 404 | void RegDefsUses::addLiveOut(const MachineBasicBlock &MBB, |
| 405 | const MachineBasicBlock &SuccBB) { |
| 406 | for (MachineBasicBlock::const_succ_iterator SI = MBB.succ_begin(), |
| 407 | SE = MBB.succ_end(); SI != SE; ++SI) |
| 408 | if (*SI != &SuccBB) |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 409 | for (const auto &LI : (*SI)->liveins()) |
| 410 | Uses.set(LI.PhysReg); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Akira Hatanaka | 979899e | 2013-02-26 01:30:05 +0000 | [diff] [blame] | 413 | bool RegDefsUses::update(const MachineInstr &MI, unsigned Begin, unsigned End) { |
| 414 | BitVector NewDefs(TRI.getNumRegs()), NewUses(TRI.getNumRegs()); |
| 415 | bool HasHazard = false; |
| 416 | |
| 417 | for (unsigned I = Begin; I != End; ++I) { |
| 418 | const MachineOperand &MO = MI.getOperand(I); |
| 419 | |
| 420 | if (MO.isReg() && MO.getReg()) |
| 421 | HasHazard |= checkRegDefsUses(NewDefs, NewUses, MO.getReg(), MO.isDef()); |
| 422 | } |
| 423 | |
| 424 | Defs |= NewDefs; |
| 425 | Uses |= NewUses; |
| 426 | |
| 427 | return HasHazard; |
| 428 | } |
| 429 | |
| 430 | bool RegDefsUses::checkRegDefsUses(BitVector &NewDefs, BitVector &NewUses, |
| 431 | unsigned Reg, bool IsDef) const { |
| 432 | if (IsDef) { |
| 433 | NewDefs.set(Reg); |
| 434 | // check whether Reg has already been defined or used. |
| 435 | return (isRegInSet(Defs, Reg) || isRegInSet(Uses, Reg)); |
| 436 | } |
| 437 | |
| 438 | NewUses.set(Reg); |
| 439 | // check whether Reg has already been defined. |
| 440 | return isRegInSet(Defs, Reg); |
| 441 | } |
| 442 | |
| 443 | bool RegDefsUses::isRegInSet(const BitVector &RegSet, unsigned Reg) const { |
| 444 | // Check Reg and all aliased Registers. |
| 445 | for (MCRegAliasIterator AI(Reg, &TRI, true); AI.isValid(); ++AI) |
| 446 | if (RegSet.test(*AI)) |
| 447 | return true; |
| 448 | return false; |
| 449 | } |
| 450 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 451 | bool InspectMemInstr::hasHazard(const MachineInstr &MI) { |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 452 | if (!MI.mayStore() && !MI.mayLoad()) |
| 453 | return false; |
| 454 | |
| 455 | if (ForbidMemInstr) |
| 456 | return true; |
| 457 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 458 | OrigSeenLoad = SeenLoad; |
| 459 | OrigSeenStore = SeenStore; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 460 | SeenLoad |= MI.mayLoad(); |
| 461 | SeenStore |= MI.mayStore(); |
| 462 | |
| 463 | // If MI is an ordered or volatile memory reference, disallow moving |
| 464 | // subsequent loads and stores to delay slot. |
| 465 | if (MI.hasOrderedMemoryRef() && (OrigSeenLoad || OrigSeenStore)) { |
| 466 | ForbidMemInstr = true; |
| 467 | return true; |
| 468 | } |
| 469 | |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 470 | return hasHazard_(MI); |
| 471 | } |
| 472 | |
| 473 | bool LoadFromStackOrConst::hasHazard_(const MachineInstr &MI) { |
| 474 | if (MI.mayStore()) |
| 475 | return true; |
| 476 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 477 | if (!MI.hasOneMemOperand() || !(*MI.memoperands_begin())->getPseudoValue()) |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 478 | return true; |
| 479 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 480 | if (const PseudoSourceValue *PSV = |
| 481 | (*MI.memoperands_begin())->getPseudoValue()) { |
| 482 | if (isa<FixedStackPseudoSourceValue>(PSV)) |
| 483 | return false; |
Alex Lorenz | e40c8a2 | 2015-08-11 23:09:45 +0000 | [diff] [blame] | 484 | return !PSV->isConstant(nullptr) && !PSV->isStack(); |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 485 | } |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 486 | |
| 487 | return true; |
| 488 | } |
| 489 | |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 490 | MemDefsUses::MemDefsUses(const DataLayout &DL, const MachineFrameInfo *MFI_) |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 491 | : InspectMemInstr(false), MFI(MFI_), DL(DL) {} |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 492 | |
| 493 | bool MemDefsUses::hasHazard_(const MachineInstr &MI) { |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 494 | bool HasHazard = false; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 495 | |
| 496 | // Check underlying object list. |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 497 | SmallVector<ValueType, 4> Objs; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 498 | if (getUnderlyingObjects(MI, Objs)) { |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 499 | for (ValueType VT : Objs) |
| 500 | HasHazard |= updateDefsUses(VT, MI.mayStore()); |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 501 | return HasHazard; |
| 502 | } |
| 503 | |
| 504 | // No underlying objects found. |
| 505 | HasHazard = MI.mayStore() && (OrigSeenLoad || OrigSeenStore); |
| 506 | HasHazard |= MI.mayLoad() || OrigSeenStore; |
| 507 | |
| 508 | SeenNoObjLoad |= MI.mayLoad(); |
| 509 | SeenNoObjStore |= MI.mayStore(); |
| 510 | |
| 511 | return HasHazard; |
| 512 | } |
| 513 | |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 514 | bool MemDefsUses::updateDefsUses(ValueType V, bool MayStore) { |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 515 | if (MayStore) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 516 | return !Defs.insert(V).second || Uses.count(V) || SeenNoObjStore || |
| 517 | SeenNoObjLoad; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 518 | |
| 519 | Uses.insert(V); |
| 520 | return Defs.count(V) || SeenNoObjStore; |
| 521 | } |
| 522 | |
| 523 | bool MemDefsUses:: |
| 524 | getUnderlyingObjects(const MachineInstr &MI, |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 525 | SmallVectorImpl<ValueType> &Objects) const { |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 526 | if (!MI.hasOneMemOperand()) |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 527 | return false; |
| 528 | |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 529 | auto & MMO = **MI.memoperands_begin(); |
| 530 | |
| 531 | if (const PseudoSourceValue *PSV = MMO.getPseudoValue()) { |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 532 | if (!PSV->isAliased(MFI)) |
| 533 | return false; |
| 534 | Objects.push_back(PSV); |
| 535 | return true; |
| 536 | } |
| 537 | |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 538 | if (const Value *V = MMO.getValue()) { |
| 539 | SmallVector<const Value *, 4> Objs; |
| 540 | GetUnderlyingObjects(V, Objs, DL); |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 541 | |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 542 | for (const Value *UValue : Objs) { |
| 543 | if (!isIdentifiedObject(V)) |
| 544 | return false; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 545 | |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 546 | Objects.push_back(UValue); |
| 547 | } |
| 548 | return true; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Simon Atanasyan | 0b0cc23 | 2019-05-15 21:26:25 +0000 | [diff] [blame] | 551 | return false; |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 554 | // Replace Branch with the compact branch instruction. |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 555 | Iter MipsDelaySlotFiller::replaceWithCompactBranch(MachineBasicBlock &MBB, |
| 556 | Iter Branch, |
| 557 | const DebugLoc &DL) { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 558 | const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>(); |
| 559 | const MipsInstrInfo *TII = STI.getInstrInfo(); |
Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 560 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 561 | unsigned NewOpcode = TII->getEquivalentCompactForm(Branch); |
| 562 | Branch = TII->genInstrWithNewOpc(NewOpcode, Branch); |
Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 563 | |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 564 | std::next(Branch)->eraseFromParent(); |
Jozef Kolek | 3b8ddb6 | 2014-11-21 22:04:35 +0000 | [diff] [blame] | 565 | return Branch; |
| 566 | } |
| 567 | |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 568 | // For given opcode returns opcode of corresponding instruction with short |
| 569 | // delay slot. |
Hiroshi Inoue | a89d4b5 | 2017-06-30 09:11:50 +0000 | [diff] [blame] | 570 | // For the pseudo TAILCALL*_MM instructions return the short delay slot |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 571 | // form. Unfortunately, TAILCALL<->b16 is denied as b16 has a limited range |
| 572 | // that is too short to make use of for tail calls. |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 573 | static int getEquivalentCallShort(int Opcode) { |
| 574 | switch (Opcode) { |
| 575 | case Mips::BGEZAL: |
| 576 | return Mips::BGEZALS_MM; |
| 577 | case Mips::BLTZAL: |
| 578 | return Mips::BLTZALS_MM; |
| 579 | case Mips::JAL: |
Simon Dardis | 0f2f597 | 2018-04-25 14:12:57 +0000 | [diff] [blame] | 580 | case Mips::JAL_MM: |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 581 | return Mips::JALS_MM; |
| 582 | case Mips::JALR: |
| 583 | return Mips::JALRS_MM; |
| 584 | case Mips::JALR16_MM: |
| 585 | return Mips::JALRS16_MM; |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 586 | case Mips::TAILCALL_MM: |
| 587 | llvm_unreachable("Attempting to shorten the TAILCALL_MM pseudo!"); |
Simon Dardis | ea34315 | 2016-08-18 13:22:43 +0000 | [diff] [blame] | 588 | case Mips::TAILCALLREG: |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 589 | return Mips::JR16_MM; |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 590 | default: |
| 591 | llvm_unreachable("Unexpected call instruction for microMIPS."); |
| 592 | } |
| 593 | } |
| 594 | |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 595 | /// runOnMachineBasicBlock - Fill in delay slots for the given basic block. |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 596 | /// We assume there is only one delay slot per delayed instruction. |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 597 | bool MipsDelaySlotFiller::runOnMachineBasicBlock(MachineBasicBlock &MBB) { |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 598 | bool Changed = false; |
Eric Christopher | 6b6db77 | 2015-02-02 23:03:43 +0000 | [diff] [blame] | 599 | const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>(); |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 600 | bool InMicroMipsMode = STI.inMicroMipsMode(); |
| 601 | const MipsInstrInfo *TII = STI.getInstrInfo(); |
Akira Hatanaka | e7b0697 | 2011-10-05 01:30:09 +0000 | [diff] [blame] | 602 | |
Akira Hatanaka | dfd2f24 | 2013-02-14 23:11:24 +0000 | [diff] [blame] | 603 | for (Iter I = MBB.begin(); I != MBB.end(); ++I) { |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 604 | if (!hasUnoccupiedSlot(&*I)) |
Akira Hatanaka | a061281 | 2013-02-07 21:32:32 +0000 | [diff] [blame] | 605 | continue; |
Akira Hatanaka | 5d4e4ea | 2011-10-05 01:23:39 +0000 | [diff] [blame] | 606 | |
Simon Dardis | eb5bfd9 | 2017-11-23 12:38:04 +0000 | [diff] [blame] | 607 | // Delay slot filling is disabled at -O0, or in microMIPS32R6. |
| 608 | if (!DisableDelaySlotFiller && (TM->getOptLevel() != CodeGenOpt::None) && |
| 609 | !(InMicroMipsMode && STI.hasMips32r6())) { |
Akira Hatanaka | 5d4e4ea | 2011-10-05 01:23:39 +0000 | [diff] [blame] | 610 | |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 611 | bool Filled = false; |
Zoran Jovanovic | 37bca10 | 2014-11-10 17:27:56 +0000 | [diff] [blame] | 612 | |
Simon Dardis | 8d8f2f8 | 2016-05-17 10:21:43 +0000 | [diff] [blame] | 613 | if (MipsCompactBranchPolicy.getValue() != CB_Always || |
| 614 | !TII->getEquivalentCompactForm(I)) { |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 615 | if (searchBackward(MBB, *I)) { |
Simon Dardis | 8d8f2f8 | 2016-05-17 10:21:43 +0000 | [diff] [blame] | 616 | Filled = true; |
| 617 | } else if (I->isTerminator()) { |
| 618 | if (searchSuccBBs(MBB, I)) { |
| 619 | Filled = true; |
| 620 | } |
| 621 | } else if (searchForward(MBB, I)) { |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 622 | Filled = true; |
Zoran Jovanovic | 37bca10 | 2014-11-10 17:27:56 +0000 | [diff] [blame] | 623 | } |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | if (Filled) { |
| 627 | // Get instruction with delay slot. |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 628 | MachineBasicBlock::instr_iterator DSI = I.getInstrIterator(); |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 629 | |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 630 | if (InMicroMipsMode && TII->getInstSizeInBytes(*std::next(DSI)) == 2 && |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 631 | DSI->isCall()) { |
| 632 | // If instruction in delay slot is 16b change opcode to |
| 633 | // corresponding instruction with short delay slot. |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 634 | |
| 635 | // TODO: Implement an instruction mapping table of 16bit opcodes to |
| 636 | // 32bit opcodes so that an instruction can be expanded. This would |
| 637 | // save 16 bits as a TAILCALL_MM pseudo requires a fullsized nop. |
Hiroshi Inoue | 290adb3 | 2018-01-22 05:54:46 +0000 | [diff] [blame] | 638 | // TODO: Permit b16 when branching backwards to the same function |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 639 | // if it is in range. |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 640 | DSI->setDesc(TII->get(getEquivalentCallShort(DSI->getOpcode()))); |
| 641 | } |
Simon Dardis | eb5bfd9 | 2017-11-23 12:38:04 +0000 | [diff] [blame] | 642 | ++FilledSlots; |
| 643 | Changed = true; |
Zoran Jovanovic | b554bba | 2014-11-25 10:50:00 +0000 | [diff] [blame] | 644 | continue; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
Akira Hatanaka | 5ac7868 | 2012-06-13 23:25:52 +0000 | [diff] [blame] | 647 | |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 648 | // For microMIPS if instruction is BEQ or BNE with one ZERO register, then |
| 649 | // instead of adding NOP replace this instruction with the corresponding |
| 650 | // compact branch instruction, i.e. BEQZC or BNEZC. Additionally |
| 651 | // PseudoReturn and PseudoIndirectBranch are expanded to JR_MM, so they can |
| 652 | // be replaced with JRC16_MM. |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 653 | |
| 654 | // For MIPSR6 attempt to produce the corresponding compact (no delay slot) |
Simon Dardis | d9d41f5 | 2016-04-05 12:50:29 +0000 | [diff] [blame] | 655 | // form of the CTI. For indirect jumps this will not require inserting a |
| 656 | // NOP and for branches will hopefully avoid requiring a NOP. |
Simon Dardis | 8d8f2f8 | 2016-05-17 10:21:43 +0000 | [diff] [blame] | 657 | if ((InMicroMipsMode || |
| 658 | (STI.hasMips32r6() && MipsCompactBranchPolicy != CB_Never)) && |
| 659 | TII->getEquivalentCompactForm(I)) { |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 660 | I = replaceWithCompactBranch(MBB, I, I->getDebugLoc()); |
Simon Dardis | eb5bfd9 | 2017-11-23 12:38:04 +0000 | [diff] [blame] | 661 | Changed = true; |
Daniel Sanders | e8efff3 | 2016-03-14 16:24:05 +0000 | [diff] [blame] | 662 | continue; |
| 663 | } |
| 664 | |
Jozef Kolek | 650a61a | 2015-02-13 17:51:27 +0000 | [diff] [blame] | 665 | // Bundle the NOP to the instruction with the delay slot. |
| 666 | BuildMI(MBB, std::next(I), I->getDebugLoc(), TII->get(Mips::NOP)); |
| 667 | MIBundleBuilder(MBB, I, std::next(I, 2)); |
Simon Dardis | eb5bfd9 | 2017-11-23 12:38:04 +0000 | [diff] [blame] | 668 | ++FilledSlots; |
| 669 | Changed = true; |
Akira Hatanaka | a061281 | 2013-02-07 21:32:32 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Bruno Cardoso Lopes | 0b97ce7 | 2007-08-18 01:50:47 +0000 | [diff] [blame] | 672 | return Changed; |
| 673 | } |
| 674 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 675 | template <typename IterTy> |
| 676 | bool MipsDelaySlotFiller::searchRange(MachineBasicBlock &MBB, IterTy Begin, |
| 677 | IterTy End, RegDefsUses &RegDU, |
| 678 | InspectMemInstr &IM, Iter Slot, |
| 679 | IterTy &Filler) const { |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 680 | for (IterTy I = Begin; I != End;) { |
| 681 | IterTy CurrI = I; |
| 682 | ++I; |
| 683 | |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 684 | // skip debug value |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 685 | if (CurrI->isDebugInstr()) |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 686 | continue; |
| 687 | |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 688 | if (terminateSearch(*CurrI)) |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 689 | break; |
| 690 | |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 691 | assert((!CurrI->isCall() && !CurrI->isReturn() && !CurrI->isBranch()) && |
Akira Hatanaka | eb33ced | 2013-03-01 00:16:31 +0000 | [diff] [blame] | 692 | "Cannot put calls, returns or branches in delay slot."); |
| 693 | |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 694 | if (CurrI->isKill()) { |
| 695 | CurrI->eraseFromParent(); |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 696 | continue; |
| 697 | } |
| 698 | |
| 699 | if (delayHasHazard(*CurrI, RegDU, IM)) |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 700 | continue; |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 701 | |
Eric Christopher | 6b6db77 | 2015-02-02 23:03:43 +0000 | [diff] [blame] | 702 | const MipsSubtarget &STI = MBB.getParent()->getSubtarget<MipsSubtarget>(); |
| 703 | if (STI.isTargetNaCl()) { |
Sasa Stankovic | 5fddf61 | 2014-03-10 20:34:23 +0000 | [diff] [blame] | 704 | // In NaCl, instructions that must be masked are forbidden in delay slots. |
| 705 | // We only check for loads, stores and SP changes. Calls, returns and |
| 706 | // branches are not checked because non-NaCl targets never put them in |
| 707 | // delay slots. |
| 708 | unsigned AddrIdx; |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 709 | if ((isBasePlusOffsetMemoryAccess(CurrI->getOpcode(), &AddrIdx) && |
| 710 | baseRegNeedsLoadStoreMask(CurrI->getOperand(AddrIdx).getReg())) || |
| 711 | CurrI->modifiesRegister(Mips::SP, STI.getRegisterInfo())) |
Sasa Stankovic | 5fddf61 | 2014-03-10 20:34:23 +0000 | [diff] [blame] | 712 | continue; |
| 713 | } |
| 714 | |
Eric Christopher | 6b6db77 | 2015-02-02 23:03:43 +0000 | [diff] [blame] | 715 | bool InMicroMipsMode = STI.inMicroMipsMode(); |
| 716 | const MipsInstrInfo *TII = STI.getInstrInfo(); |
Jozef Kolek | e7cad7a | 2015-01-13 15:59:17 +0000 | [diff] [blame] | 717 | unsigned Opcode = (*Slot).getOpcode(); |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 718 | // This is complicated by the tail call optimization. For non-PIC code |
| 719 | // there is only a 32bit sized unconditional branch which can be assumed |
| 720 | // to be able to reach the target. b16 only has a range of +/- 1 KB. |
| 721 | // It's entirely possible that the target function is reachable with b16 |
| 722 | // but we don't have enough information to make that decision. |
| 723 | if (InMicroMipsMode && TII->getInstSizeInBytes(*CurrI) == 2 && |
Jozef Kolek | e7cad7a | 2015-01-13 15:59:17 +0000 | [diff] [blame] | 724 | (Opcode == Mips::JR || Opcode == Mips::PseudoIndirectBranch || |
Petar Jovanovic | 6083106 | 2019-02-22 14:53:58 +0000 | [diff] [blame] | 725 | Opcode == Mips::PseudoIndirectBranch_MM || |
Simon Dardis | 57f4ae4 | 2016-08-04 09:17:07 +0000 | [diff] [blame] | 726 | Opcode == Mips::PseudoReturn || Opcode == Mips::TAILCALL)) |
Jozef Kolek | e7cad7a | 2015-01-13 15:59:17 +0000 | [diff] [blame] | 727 | continue; |
Simon Atanasyan | a9e8765 | 2018-09-19 18:46:29 +0000 | [diff] [blame] | 728 | // Instructions LWP/SWP and MOVEP should not be in a delay slot as that |
Zoran Jovanovic | 3a7654c | 2018-06-13 12:51:37 +0000 | [diff] [blame] | 729 | // results in unpredictable behaviour |
Simon Atanasyan | a9e8765 | 2018-09-19 18:46:29 +0000 | [diff] [blame] | 730 | if (InMicroMipsMode && (Opcode == Mips::LWP_MM || Opcode == Mips::SWP_MM || |
| 731 | Opcode == Mips::MOVEP_MM)) |
Zoran Jovanovic | 3a7654c | 2018-06-13 12:51:37 +0000 | [diff] [blame] | 732 | continue; |
Jozef Kolek | e7cad7a | 2015-01-13 15:59:17 +0000 | [diff] [blame] | 733 | |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 734 | Filler = CurrI; |
Akira Hatanaka | f815db5 | 2013-03-01 00:26:14 +0000 | [diff] [blame] | 735 | return true; |
| 736 | } |
| 737 | |
| 738 | return false; |
| 739 | } |
| 740 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 741 | bool MipsDelaySlotFiller::searchBackward(MachineBasicBlock &MBB, |
| 742 | MachineInstr &Slot) const { |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 743 | if (DisableBackwardSearch) |
| 744 | return false; |
| 745 | |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 746 | auto *Fn = MBB.getParent(); |
| 747 | RegDefsUses RegDU(*Fn->getSubtarget().getRegisterInfo()); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 748 | MemDefsUses MemDU(Fn->getDataLayout(), &Fn->getFrameInfo()); |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 749 | ReverseIter Filler; |
Akira Hatanaka | f815db5 | 2013-03-01 00:26:14 +0000 | [diff] [blame] | 750 | |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 751 | RegDU.init(Slot); |
Akira Hatanaka | f815db5 | 2013-03-01 00:26:14 +0000 | [diff] [blame] | 752 | |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 753 | MachineBasicBlock::iterator SlotI = Slot; |
| 754 | if (!searchRange(MBB, ++SlotI.getReverse(), MBB.rend(), RegDU, MemDU, Slot, |
Vasileios Kalintiris | 8761490 | 2015-03-04 12:37:58 +0000 | [diff] [blame] | 755 | Filler)) |
Akira Hatanaka | 4c0a712 | 2013-10-07 19:33:02 +0000 | [diff] [blame] | 756 | return false; |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 757 | |
Duncan P. N. Exon Smith | 1872096 | 2016-09-11 18:51:28 +0000 | [diff] [blame] | 758 | MBB.splice(std::next(SlotI), &MBB, Filler.getReverse()); |
| 759 | MIBundleBuilder(MBB, SlotI, std::next(SlotI, 2)); |
Akira Hatanaka | 4c0a712 | 2013-10-07 19:33:02 +0000 | [diff] [blame] | 760 | ++UsefulSlots; |
| 761 | return true; |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 764 | bool MipsDelaySlotFiller::searchForward(MachineBasicBlock &MBB, |
| 765 | Iter Slot) const { |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 766 | // Can handle only calls. |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 767 | if (DisableForwardSearch || !Slot->isCall()) |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 768 | return false; |
| 769 | |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 770 | RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo()); |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 771 | NoMemInstr NM; |
| 772 | Iter Filler; |
| 773 | |
| 774 | RegDU.setCallerSaved(*Slot); |
| 775 | |
Vasileios Kalintiris | 8761490 | 2015-03-04 12:37:58 +0000 | [diff] [blame] | 776 | if (!searchRange(MBB, std::next(Slot), MBB.end(), RegDU, NM, Slot, Filler)) |
Akira Hatanaka | 4c0a712 | 2013-10-07 19:33:02 +0000 | [diff] [blame] | 777 | return false; |
Akira Hatanaka | 5d4e4ea | 2011-10-05 01:23:39 +0000 | [diff] [blame] | 778 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 779 | MBB.splice(std::next(Slot), &MBB, Filler); |
| 780 | MIBundleBuilder(MBB, Slot, std::next(Slot, 2)); |
Akira Hatanaka | 4c0a712 | 2013-10-07 19:33:02 +0000 | [diff] [blame] | 781 | ++UsefulSlots; |
| 782 | return true; |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 785 | bool MipsDelaySlotFiller::searchSuccBBs(MachineBasicBlock &MBB, |
| 786 | Iter Slot) const { |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 787 | if (DisableSuccBBSearch) |
| 788 | return false; |
| 789 | |
| 790 | MachineBasicBlock *SuccBB = selectSuccBB(MBB); |
| 791 | |
| 792 | if (!SuccBB) |
| 793 | return false; |
| 794 | |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 795 | RegDefsUses RegDU(*MBB.getParent()->getSubtarget().getRegisterInfo()); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 796 | bool HasMultipleSuccs = false; |
| 797 | BB2BrMap BrMap; |
Benjamin Kramer | d2da720 | 2014-04-21 09:34:48 +0000 | [diff] [blame] | 798 | std::unique_ptr<InspectMemInstr> IM; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 799 | Iter Filler; |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 800 | auto *Fn = MBB.getParent(); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 801 | |
| 802 | // Iterate over SuccBB's predecessor list. |
| 803 | for (MachineBasicBlock::pred_iterator PI = SuccBB->pred_begin(), |
| 804 | PE = SuccBB->pred_end(); PI != PE; ++PI) |
| 805 | if (!examinePred(**PI, *SuccBB, RegDU, HasMultipleSuccs, BrMap)) |
| 806 | return false; |
| 807 | |
| 808 | // Do not allow moving instructions which have unallocatable register operands |
| 809 | // across basic block boundaries. |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 810 | RegDU.setUnallocatableRegs(*Fn); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 811 | |
| 812 | // Only allow moving loads from stack or constants if any of the SuccBB's |
| 813 | // predecessors have multiple successors. |
| 814 | if (HasMultipleSuccs) { |
| 815 | IM.reset(new LoadFromStackOrConst()); |
| 816 | } else { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 817 | const MachineFrameInfo &MFI = Fn->getFrameInfo(); |
| 818 | IM.reset(new MemDefsUses(Fn->getDataLayout(), &MFI)); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Vasileios Kalintiris | 8761490 | 2015-03-04 12:37:58 +0000 | [diff] [blame] | 821 | if (!searchRange(MBB, SuccBB->begin(), SuccBB->end(), RegDU, *IM, Slot, |
| 822 | Filler)) |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 823 | return false; |
| 824 | |
| 825 | insertDelayFiller(Filler, BrMap); |
| 826 | addLiveInRegs(Filler, *SuccBB); |
| 827 | Filler->eraseFromParent(); |
| 828 | |
| 829 | return true; |
| 830 | } |
| 831 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 832 | MachineBasicBlock * |
| 833 | MipsDelaySlotFiller::selectSuccBB(MachineBasicBlock &B) const { |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 834 | if (B.succ_empty()) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 835 | return nullptr; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 836 | |
| 837 | // Select the successor with the larget edge weight. |
Benjamin Kramer | 3a377bc | 2014-03-01 11:47:00 +0000 | [diff] [blame] | 838 | auto &Prob = getAnalysis<MachineBranchProbabilityInfo>(); |
Cong Hou | 1938f2e | 2015-11-24 08:51:23 +0000 | [diff] [blame] | 839 | MachineBasicBlock *S = *std::max_element( |
| 840 | B.succ_begin(), B.succ_end(), |
| 841 | [&](const MachineBasicBlock *Dst0, const MachineBasicBlock *Dst1) { |
| 842 | return Prob.getEdgeProbability(&B, Dst0) < |
| 843 | Prob.getEdgeProbability(&B, Dst1); |
| 844 | }); |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 845 | return S->isEHPad() ? nullptr : S; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | std::pair<MipsInstrInfo::BranchType, MachineInstr *> |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 849 | MipsDelaySlotFiller::getBranch(MachineBasicBlock &MBB, |
| 850 | const MachineBasicBlock &Dst) const { |
Eric Christopher | 6b6db77 | 2015-02-02 23:03:43 +0000 | [diff] [blame] | 851 | const MipsInstrInfo *TII = |
| 852 | MBB.getParent()->getSubtarget<MipsSubtarget>().getInstrInfo(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 853 | MachineBasicBlock *TrueBB = nullptr, *FalseBB = nullptr; |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 854 | SmallVector<MachineInstr*, 2> BranchInstrs; |
| 855 | SmallVector<MachineOperand, 2> Cond; |
| 856 | |
| 857 | MipsInstrInfo::BranchType R = |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 858 | TII->analyzeBranch(MBB, TrueBB, FalseBB, Cond, false, BranchInstrs); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 859 | |
| 860 | if ((R == MipsInstrInfo::BT_None) || (R == MipsInstrInfo::BT_NoBranch)) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 861 | return std::make_pair(R, nullptr); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 862 | |
| 863 | if (R != MipsInstrInfo::BT_CondUncond) { |
| 864 | if (!hasUnoccupiedSlot(BranchInstrs[0])) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 865 | return std::make_pair(MipsInstrInfo::BT_None, nullptr); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 866 | |
| 867 | assert(((R != MipsInstrInfo::BT_Uncond) || (TrueBB == &Dst))); |
| 868 | |
| 869 | return std::make_pair(R, BranchInstrs[0]); |
| 870 | } |
| 871 | |
| 872 | assert((TrueBB == &Dst) || (FalseBB == &Dst)); |
| 873 | |
| 874 | // Examine the conditional branch. See if its slot is occupied. |
| 875 | if (hasUnoccupiedSlot(BranchInstrs[0])) |
| 876 | return std::make_pair(MipsInstrInfo::BT_Cond, BranchInstrs[0]); |
| 877 | |
| 878 | // If that fails, try the unconditional branch. |
| 879 | if (hasUnoccupiedSlot(BranchInstrs[1]) && (FalseBB == &Dst)) |
| 880 | return std::make_pair(MipsInstrInfo::BT_Uncond, BranchInstrs[1]); |
| 881 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 882 | return std::make_pair(MipsInstrInfo::BT_None, nullptr); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 885 | bool MipsDelaySlotFiller::examinePred(MachineBasicBlock &Pred, |
| 886 | const MachineBasicBlock &Succ, |
| 887 | RegDefsUses &RegDU, |
| 888 | bool &HasMultipleSuccs, |
| 889 | BB2BrMap &BrMap) const { |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 890 | std::pair<MipsInstrInfo::BranchType, MachineInstr *> P = |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 891 | getBranch(Pred, Succ); |
Akira Hatanaka | 8f7bfb3 | 2013-03-01 02:03:51 +0000 | [diff] [blame] | 892 | |
| 893 | // Return if either getBranch wasn't able to analyze the branches or there |
| 894 | // were no branches with unoccupied slots. |
| 895 | if (P.first == MipsInstrInfo::BT_None) |
| 896 | return false; |
| 897 | |
| 898 | if ((P.first != MipsInstrInfo::BT_Uncond) && |
| 899 | (P.first != MipsInstrInfo::BT_NoBranch)) { |
| 900 | HasMultipleSuccs = true; |
| 901 | RegDU.addLiveOut(Pred, Succ); |
| 902 | } |
| 903 | |
| 904 | BrMap[&Pred] = P.second; |
| 905 | return true; |
| 906 | } |
| 907 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 908 | bool MipsDelaySlotFiller::delayHasHazard(const MachineInstr &Candidate, |
| 909 | RegDefsUses &RegDU, |
| 910 | InspectMemInstr &IM) const { |
Vasileios Kalintiris | bb60cfb | 2015-04-17 12:01:02 +0000 | [diff] [blame] | 911 | assert(!Candidate.isKill() && |
| 912 | "KILL instructions should have been eliminated at this point."); |
| 913 | |
| 914 | bool HasHazard = Candidate.isImplicitDef(); |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 915 | |
Akira Hatanaka | e01ff9d | 2013-03-01 00:50:52 +0000 | [diff] [blame] | 916 | HasHazard |= IM.hasHazard(Candidate); |
Akira Hatanaka | 979899e | 2013-02-26 01:30:05 +0000 | [diff] [blame] | 917 | HasHazard |= RegDU.update(Candidate, 0, Candidate.getNumOperands()); |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 918 | |
Akira Hatanaka | 06bd138 | 2013-02-14 23:40:57 +0000 | [diff] [blame] | 919 | return HasHazard; |
Akira Hatanaka | f2619ee | 2011-09-29 23:52:13 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 922 | bool MipsDelaySlotFiller::terminateSearch(const MachineInstr &Candidate) const { |
Akira Hatanaka | dfd2f24 | 2013-02-14 23:11:24 +0000 | [diff] [blame] | 923 | return (Candidate.isTerminator() || Candidate.isCall() || |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 924 | Candidate.isPosition() || Candidate.isInlineAsm() || |
Akira Hatanaka | dfd2f24 | 2013-02-14 23:11:24 +0000 | [diff] [blame] | 925 | Candidate.hasUnmodeledSideEffects()); |
| 926 | } |
Eugene Zelenko | 926883e | 2017-02-01 01:22:51 +0000 | [diff] [blame] | 927 | |
| 928 | /// createMipsDelaySlotFillerPass - Returns a pass that fills in delay |
| 929 | /// slots in Mips MachineFunctions |
Simon Dardis | ae20108 | 2018-05-11 16:13:53 +0000 | [diff] [blame] | 930 | FunctionPass *llvm::createMipsDelaySlotFillerPass() { return new MipsDelaySlotFiller(); } |