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