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