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