Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1 | //===-- X86OptimizeLEAs.cpp - optimize usage of LEA instructions ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the pass that performs some optimizations with LEA |
Andrey Turetskiy | 45b22a4 | 2016-05-19 10:18:29 +0000 | [diff] [blame] | 11 | // instructions in order to improve performance and code size. |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 12 | // Currently, it does two things: |
| 13 | // 1) If there are two LEA instructions calculating addresses which only differ |
| 14 | // by displacement inside a basic block, one of them is removed. |
| 15 | // 2) Address calculations in load and store instructions are replaced by |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 16 | // existing LEA def registers where possible. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "X86.h" |
| 21 | #include "X86InstrInfo.h" |
| 22 | #include "X86Subtarget.h" |
| 23 | #include "llvm/ADT/Statistic.h" |
| 24 | #include "llvm/CodeGen/LiveVariables.h" |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineDominators.h" |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineOperand.h" |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 30 | #include "llvm/CodeGen/Passes.h" |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 31 | #include "llvm/IR/DIBuilder.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 32 | #include "llvm/IR/DebugInfoMetadata.h" |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Function.h" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/raw_ostream.h" |
| 36 | #include "llvm/Target/TargetInstrInfo.h" |
| 37 | |
| 38 | using namespace llvm; |
| 39 | |
| 40 | #define DEBUG_TYPE "x86-optimize-LEAs" |
| 41 | |
Andrey Turetskiy | 9994b88 | 2016-02-20 11:11:55 +0000 | [diff] [blame] | 42 | static cl::opt<bool> |
| 43 | DisableX86LEAOpt("disable-x86-lea-opt", cl::Hidden, |
| 44 | cl::desc("X86: Disable LEA optimizations."), |
| 45 | cl::init(false)); |
Alexey Bataev | 7b72b65 | 2015-12-17 07:34:39 +0000 | [diff] [blame] | 46 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 47 | STATISTIC(NumSubstLEAs, "Number of LEA instruction substitutions"); |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 48 | STATISTIC(NumFactoredLEAs, "Number of LEAs factorized"); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 49 | STATISTIC(NumRedundantLEAs, "Number of redundant LEA instructions removed"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 50 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 51 | /// \brief Returns true if two machine operands are identical and they are not |
| 52 | /// physical registers. |
| 53 | static inline bool isIdenticalOp(const MachineOperand &MO1, |
| 54 | const MachineOperand &MO2); |
| 55 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 56 | /// \brief Returns true if two machine instructions have identical operands. |
| 57 | static bool isIdenticalMI(MachineRegisterInfo *MRI, const MachineOperand &MO1, |
| 58 | const MachineOperand &MO2); |
| 59 | |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 60 | /// \brief Returns true if two address displacement operands are of the same |
| 61 | /// type and use the same symbol/index/address regardless of the offset. |
| 62 | static bool isSimilarDispOp(const MachineOperand &MO1, |
| 63 | const MachineOperand &MO2); |
| 64 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 65 | /// \brief Returns true if the instruction is LEA. |
| 66 | static inline bool isLEA(const MachineInstr &MI); |
| 67 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 68 | /// \brief Returns true if Definition of Operand is a copylike instruction. |
| 69 | static bool isDefCopyLike(MachineRegisterInfo *MRI, const MachineOperand &Opr); |
| 70 | |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 71 | namespace { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 72 | /// A key based on instruction's memory operands. |
| 73 | class MemOpKey { |
| 74 | public: |
| 75 | MemOpKey(const MachineOperand *Base, const MachineOperand *Scale, |
| 76 | const MachineOperand *Index, const MachineOperand *Segment, |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 77 | const MachineOperand *Disp, bool DispCheck = false) |
| 78 | : Disp(Disp), DeepCheck(DispCheck) { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 79 | Operands[0] = Base; |
| 80 | Operands[1] = Scale; |
| 81 | Operands[2] = Index; |
| 82 | Operands[3] = Segment; |
| 83 | } |
| 84 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 85 | /// Checks operands of MemOpKey are identical, if Base or Index |
| 86 | /// operand definitions are of kind SUBREG_TO_REG then compare |
| 87 | /// operands of defining MI. |
| 88 | bool performDeepCheck(const MemOpKey &Other) const { |
| 89 | MachineInstr *MI = const_cast<MachineInstr *>(Operands[0]->getParent()); |
| 90 | MachineRegisterInfo *MRI = MI->getRegInfo(); |
| 91 | |
| 92 | for (int i = 0; i < 4; i++) { |
| 93 | bool copyLike = isDefCopyLike(MRI, *Operands[i]); |
| 94 | if (copyLike && !isIdenticalMI(MRI, *Operands[i], *Other.Operands[i])) |
| 95 | return false; |
| 96 | else if (!copyLike && !isIdenticalOp(*Operands[i], *Other.Operands[i])) |
| 97 | return false; |
| 98 | } |
| 99 | return isIdenticalOp(*Disp, *Other.Disp); |
| 100 | } |
| 101 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 102 | bool operator==(const MemOpKey &Other) const { |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 103 | if (DeepCheck) |
| 104 | return performDeepCheck(Other); |
| 105 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 106 | // Addresses' bases, scales, indices and segments must be identical. |
| 107 | for (int i = 0; i < 4; ++i) |
| 108 | if (!isIdenticalOp(*Operands[i], *Other.Operands[i])) |
| 109 | return false; |
| 110 | |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 111 | // Addresses' displacements don't have to be exactly the same. It only |
| 112 | // matters that they use the same symbol/index/address. Immediates' or |
| 113 | // offsets' differences will be taken care of during instruction |
| 114 | // substitution. |
| 115 | return isSimilarDispOp(*Disp, *Other.Disp); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Address' base, scale, index and segment operands. |
| 119 | const MachineOperand *Operands[4]; |
| 120 | |
| 121 | // Address' displacement operand. |
| 122 | const MachineOperand *Disp; |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 123 | |
| 124 | // If true checks Address' base, index, segment and |
| 125 | // displacement are identical, in additions if base/index |
| 126 | // are defined by copylike instruction then futher |
| 127 | // compare the operands of the defining instruction. |
| 128 | bool DeepCheck; |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 129 | }; |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 130 | } // end anonymous namespace |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 131 | |
| 132 | /// Provide DenseMapInfo for MemOpKey. |
| 133 | namespace llvm { |
| 134 | template <> struct DenseMapInfo<MemOpKey> { |
| 135 | typedef DenseMapInfo<const MachineOperand *> PtrInfo; |
| 136 | |
| 137 | static inline MemOpKey getEmptyKey() { |
| 138 | return MemOpKey(PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(), |
| 139 | PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(), |
| 140 | PtrInfo::getEmptyKey()); |
| 141 | } |
| 142 | |
| 143 | static inline MemOpKey getTombstoneKey() { |
| 144 | return MemOpKey(PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(), |
| 145 | PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(), |
| 146 | PtrInfo::getTombstoneKey()); |
| 147 | } |
| 148 | |
| 149 | static unsigned getHashValue(const MemOpKey &Val) { |
| 150 | // Checking any field of MemOpKey is enough to determine if the key is |
| 151 | // empty or tombstone. |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 152 | hash_code Hash(0); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 153 | assert(Val.Disp != PtrInfo::getEmptyKey() && "Cannot hash the empty key"); |
| 154 | assert(Val.Disp != PtrInfo::getTombstoneKey() && |
| 155 | "Cannot hash the tombstone key"); |
| 156 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 157 | auto getMIHash = [](MachineInstr *MI) -> hash_code { |
| 158 | hash_code h(0); |
| 159 | for (unsigned i = 1, e = MI->getNumOperands(); i < e; i++) |
| 160 | h = hash_combine(h, MI->getOperand(i)); |
| 161 | return h; |
| 162 | }; |
| 163 | |
| 164 | const MachineOperand &Base = *Val.Operands[0]; |
| 165 | const MachineOperand &Index = *Val.Operands[2]; |
| 166 | MachineInstr *MI = const_cast<MachineInstr *>(Base.getParent()); |
| 167 | MachineRegisterInfo *MRI = MI->getRegInfo(); |
| 168 | |
| 169 | if (isDefCopyLike(MRI, Base)) |
| 170 | Hash = getMIHash(MRI->getVRegDef(Base.getReg())); |
| 171 | else |
| 172 | Hash = hash_combine(Hash, Base); |
| 173 | |
| 174 | if (isDefCopyLike(MRI, Index)) |
| 175 | Hash = getMIHash(MRI->getVRegDef(Index.getReg())); |
| 176 | else |
| 177 | Hash = hash_combine(Hash, Index); |
| 178 | |
| 179 | Hash = hash_combine(Hash, *Val.Operands[1], *Val.Operands[3]); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 180 | |
| 181 | // If the address displacement is an immediate, it should not affect the |
| 182 | // hash so that memory operands which differ only be immediate displacement |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 183 | // would have the same hash. If the address displacement is something else, |
| 184 | // we should reflect symbol/index/address in the hash. |
| 185 | switch (Val.Disp->getType()) { |
| 186 | case MachineOperand::MO_Immediate: |
| 187 | break; |
| 188 | case MachineOperand::MO_ConstantPoolIndex: |
| 189 | case MachineOperand::MO_JumpTableIndex: |
| 190 | Hash = hash_combine(Hash, Val.Disp->getIndex()); |
| 191 | break; |
| 192 | case MachineOperand::MO_ExternalSymbol: |
| 193 | Hash = hash_combine(Hash, Val.Disp->getSymbolName()); |
| 194 | break; |
| 195 | case MachineOperand::MO_GlobalAddress: |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 196 | Hash = hash_combine(Hash, Val.Disp->getGlobal()); |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 197 | break; |
| 198 | case MachineOperand::MO_BlockAddress: |
| 199 | Hash = hash_combine(Hash, Val.Disp->getBlockAddress()); |
| 200 | break; |
| 201 | case MachineOperand::MO_MCSymbol: |
| 202 | Hash = hash_combine(Hash, Val.Disp->getMCSymbol()); |
| 203 | break; |
Andrey Turetskiy | b405606 | 2016-04-26 12:18:12 +0000 | [diff] [blame] | 204 | case MachineOperand::MO_MachineBasicBlock: |
| 205 | Hash = hash_combine(Hash, Val.Disp->getMBB()); |
| 206 | break; |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 207 | default: |
| 208 | llvm_unreachable("Invalid address displacement operand"); |
| 209 | } |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 210 | |
| 211 | return (unsigned)Hash; |
| 212 | } |
| 213 | |
| 214 | static bool isEqual(const MemOpKey &LHS, const MemOpKey &RHS) { |
| 215 | // Checking any field of MemOpKey is enough to determine if the key is |
| 216 | // empty or tombstone. |
| 217 | if (RHS.Disp == PtrInfo::getEmptyKey()) |
| 218 | return LHS.Disp == PtrInfo::getEmptyKey(); |
| 219 | if (RHS.Disp == PtrInfo::getTombstoneKey()) |
| 220 | return LHS.Disp == PtrInfo::getTombstoneKey(); |
| 221 | return LHS == RHS; |
| 222 | } |
| 223 | }; |
| 224 | } |
| 225 | |
Benjamin Kramer | b7d3311 | 2016-08-06 11:13:10 +0000 | [diff] [blame] | 226 | /// \brief Returns a hash table key based on memory operands of \p MI. The |
| 227 | /// number of the first memory operand of \p MI is specified through \p N. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 228 | static inline MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N) { |
| 229 | assert((isLEA(MI) || MI.mayLoadOrStore()) && |
| 230 | "The instruction must be a LEA, a load or a store"); |
| 231 | return MemOpKey(&MI.getOperand(N + X86::AddrBaseReg), |
| 232 | &MI.getOperand(N + X86::AddrScaleAmt), |
| 233 | &MI.getOperand(N + X86::AddrIndexReg), |
| 234 | &MI.getOperand(N + X86::AddrSegmentReg), |
| 235 | &MI.getOperand(N + X86::AddrDisp)); |
| 236 | } |
| 237 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 238 | static inline MemOpKey getMemOpCSEKey(const MachineInstr &MI, unsigned N) { |
| 239 | static MachineOperand DummyScale = MachineOperand::CreateImm(1); |
| 240 | assert((isLEA(MI) || MI.mayLoadOrStore()) && |
| 241 | "The instruction must be a LEA, a load or a store"); |
| 242 | return MemOpKey(&MI.getOperand(N + X86::AddrBaseReg), &DummyScale, |
| 243 | &MI.getOperand(N + X86::AddrIndexReg), |
| 244 | &MI.getOperand(N + X86::AddrSegmentReg), |
| 245 | &MI.getOperand(N + X86::AddrDisp), true); |
| 246 | } |
| 247 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 248 | static inline bool isIdenticalOp(const MachineOperand &MO1, |
| 249 | const MachineOperand &MO2) { |
| 250 | return MO1.isIdenticalTo(MO2) && |
| 251 | (!MO1.isReg() || |
| 252 | !TargetRegisterInfo::isPhysicalRegister(MO1.getReg())); |
| 253 | } |
| 254 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 255 | static bool isIdenticalMI(MachineRegisterInfo *MRI, const MachineOperand &MO1, |
| 256 | const MachineOperand &MO2) { |
| 257 | MachineInstr *MI1 = nullptr; |
| 258 | MachineInstr *MI2 = nullptr; |
| 259 | if (!MO1.isReg() || !MO2.isReg()) |
| 260 | return false; |
| 261 | |
| 262 | MI1 = MRI->getVRegDef(MO1.getReg()); |
| 263 | MI2 = MRI->getVRegDef(MO2.getReg()); |
| 264 | if (!MI1 || !MI2) |
| 265 | return false; |
| 266 | if (MI1->getOpcode() != MI2->getOpcode()) |
| 267 | return false; |
| 268 | if (MI1->getNumOperands() != MI2->getNumOperands()) |
| 269 | return false; |
| 270 | for (unsigned i = 1, e = MI1->getNumOperands(); i < e; ++i) |
| 271 | if (!isIdenticalOp(MI1->getOperand(i), MI2->getOperand(i))) |
| 272 | return false; |
| 273 | return true; |
| 274 | } |
| 275 | |
Justin Bogner | 38e5217 | 2016-02-24 07:58:02 +0000 | [diff] [blame] | 276 | #ifndef NDEBUG |
| 277 | static bool isValidDispOp(const MachineOperand &MO) { |
| 278 | return MO.isImm() || MO.isCPI() || MO.isJTI() || MO.isSymbol() || |
Andrey Turetskiy | b405606 | 2016-04-26 12:18:12 +0000 | [diff] [blame] | 279 | MO.isGlobal() || MO.isBlockAddress() || MO.isMCSymbol() || MO.isMBB(); |
Justin Bogner | 38e5217 | 2016-02-24 07:58:02 +0000 | [diff] [blame] | 280 | } |
| 281 | #endif |
| 282 | |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 283 | static bool isSimilarDispOp(const MachineOperand &MO1, |
| 284 | const MachineOperand &MO2) { |
| 285 | assert(isValidDispOp(MO1) && isValidDispOp(MO2) && |
| 286 | "Address displacement operand is not valid"); |
| 287 | return (MO1.isImm() && MO2.isImm()) || |
| 288 | (MO1.isCPI() && MO2.isCPI() && MO1.getIndex() == MO2.getIndex()) || |
| 289 | (MO1.isJTI() && MO2.isJTI() && MO1.getIndex() == MO2.getIndex()) || |
| 290 | (MO1.isSymbol() && MO2.isSymbol() && |
| 291 | MO1.getSymbolName() == MO2.getSymbolName()) || |
| 292 | (MO1.isGlobal() && MO2.isGlobal() && |
| 293 | MO1.getGlobal() == MO2.getGlobal()) || |
| 294 | (MO1.isBlockAddress() && MO2.isBlockAddress() && |
| 295 | MO1.getBlockAddress() == MO2.getBlockAddress()) || |
| 296 | (MO1.isMCSymbol() && MO2.isMCSymbol() && |
Andrey Turetskiy | b405606 | 2016-04-26 12:18:12 +0000 | [diff] [blame] | 297 | MO1.getMCSymbol() == MO2.getMCSymbol()) || |
| 298 | (MO1.isMBB() && MO2.isMBB() && MO1.getMBB() == MO2.getMBB()); |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 301 | static inline bool isLEA(const MachineInstr &MI) { |
| 302 | unsigned Opcode = MI.getOpcode(); |
| 303 | return Opcode == X86::LEA16r || Opcode == X86::LEA32r || |
| 304 | Opcode == X86::LEA64r || Opcode == X86::LEA64_32r; |
| 305 | } |
| 306 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 307 | static bool isDefCopyLike(MachineRegisterInfo *MRI, const MachineOperand &Opr) { |
| 308 | if (!Opr.isReg() || TargetRegisterInfo::isPhysicalRegister(Opr.getReg())) |
| 309 | return false; |
| 310 | MachineInstr *MI = MRI->getVRegDef(Opr.getReg()); |
| 311 | return MI && MI->isCopyLike(); |
| 312 | } |
| 313 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 314 | namespace { |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 315 | |
| 316 | /// This class captures the functions and attributes |
| 317 | /// needed to factorize LEA within and across basic |
| 318 | /// blocks.LEA instruction with same BASE,OFFSET and |
| 319 | /// INDEX are the candidates for factorization. |
| 320 | class FactorizeLEAOpt { |
| 321 | public: |
| 322 | using LEAListT = std::list<MachineInstr *>; |
| 323 | using LEAMapT = DenseMap<MemOpKey, LEAListT>; |
| 324 | using ValueT = DenseMap<MemOpKey, unsigned>; |
| 325 | using ScopeEntryT = std::pair<MachineBasicBlock *, ValueT>; |
| 326 | using ScopeStackT = std::vector<ScopeEntryT>; |
| 327 | |
| 328 | FactorizeLEAOpt() = default; |
| 329 | FactorizeLEAOpt(const FactorizeLEAOpt &) = delete; |
| 330 | FactorizeLEAOpt &operator=(const FactorizeLEAOpt &) = delete; |
| 331 | |
| 332 | void performCleanup() { |
| 333 | for (auto LEA : removedLEAs) |
| 334 | LEA->eraseFromParent(); |
| 335 | LEAs.clear(); |
| 336 | Stack.clear(); |
| 337 | removedLEAs.clear(); |
| 338 | } |
| 339 | |
| 340 | LEAMapT &getLEAMap() { return LEAs; } |
| 341 | ScopeEntryT *getTopScope() { return &Stack.back(); } |
| 342 | |
| 343 | void addForLazyRemoval(MachineInstr *Instr) { removedLEAs.insert(Instr); } |
| 344 | |
| 345 | bool checkIfScheduledForRemoval(MachineInstr *Instr) { |
| 346 | return removedLEAs.find(Instr) != removedLEAs.end(); |
| 347 | } |
| 348 | |
| 349 | /// Push the ScopeEntry for the BasicBlock over Stack. |
| 350 | /// Also traverses over list of instruction and update |
| 351 | /// LEAs Map and ScopeEntry for each LEA instruction |
| 352 | /// found using insertLEA(). |
| 353 | void pushScope(MachineBasicBlock *MBB); |
| 354 | |
| 355 | /// Stores the size of MachineInstr list corrosponding |
| 356 | /// to key K from LEAs MAP into the ScopeEntry of |
| 357 | /// the basic block, then insert the LEA at the beginning |
| 358 | /// of the list. |
| 359 | void insertLEA(MachineInstr *MI); |
| 360 | |
| 361 | /// Pops out ScopeEntry of top most BasicBlock from the stack |
| 362 | /// and remove the LEA instructions contained in the scope |
| 363 | /// from the LEAs Map. |
| 364 | void popScope(); |
| 365 | |
| 366 | /// If LEA contains Physical Registers then its not a candidate |
| 367 | /// for factorizations since physical registers may violate SSA |
| 368 | /// semantics of MI. |
| 369 | bool containsPhyReg(MachineInstr *MI, unsigned RecLevel); |
| 370 | |
| 371 | private: |
| 372 | ScopeStackT Stack; |
| 373 | LEAMapT LEAs; |
| 374 | std::set<MachineInstr *> removedLEAs; |
| 375 | }; |
| 376 | |
| 377 | void FactorizeLEAOpt::pushScope(MachineBasicBlock *MBB) { |
| 378 | ValueT EmptyMap; |
| 379 | ScopeEntryT SE = std::make_pair(MBB, EmptyMap); |
| 380 | Stack.push_back(SE); |
| 381 | for (auto &MI : *MBB) { |
| 382 | if (isLEA(MI)) |
| 383 | insertLEA(&MI); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | void FactorizeLEAOpt::popScope() { |
| 388 | ScopeEntryT &SE = Stack.back(); |
| 389 | for (auto MapEntry : SE.second) { |
| 390 | LEAMapT::iterator Itr = LEAs.find(MapEntry.first); |
| 391 | assert((Itr != LEAs.end()) && |
| 392 | "LEAs map must have a node corresponding to ScopeEntry's Key."); |
| 393 | |
| 394 | while (((*Itr).second.size() > MapEntry.second)) |
| 395 | (*Itr).second.pop_front(); |
| 396 | // If list goes empty remove entry from LEAs Map. |
| 397 | if ((*Itr).second.empty()) |
| 398 | LEAs.erase(Itr); |
| 399 | } |
| 400 | Stack.pop_back(); |
| 401 | } |
| 402 | |
| 403 | bool FactorizeLEAOpt::containsPhyReg(MachineInstr *MI, unsigned RecLevel) { |
| 404 | if (!MI || !RecLevel) |
| 405 | return false; |
| 406 | |
| 407 | MachineRegisterInfo *MRI = MI->getRegInfo(); |
| 408 | for (auto Operand : MI->operands()) { |
| 409 | if (!Operand.isReg()) |
| 410 | continue; |
| 411 | if (TargetRegisterInfo::isPhysicalRegister(Operand.getReg())) |
| 412 | return true; |
| 413 | MachineInstr *OperDefMI = MRI->getVRegDef(Operand.getReg()); |
| 414 | if (OperDefMI && (MI != OperDefMI) && OperDefMI->isCopyLike() && |
| 415 | containsPhyReg(OperDefMI, RecLevel - 1)) |
| 416 | return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | void FactorizeLEAOpt::insertLEA(MachineInstr *MI) { |
| 422 | unsigned lsize; |
| 423 | if (containsPhyReg(MI, 2)) |
| 424 | return; |
| 425 | |
| 426 | MemOpKey Key = getMemOpCSEKey(*MI, 1); |
| 427 | ScopeEntryT *TopScope = getTopScope(); |
| 428 | |
| 429 | LEAMapT::iterator Itr = LEAs.find(Key); |
| 430 | if (Itr == LEAs.end()) { |
| 431 | lsize = 0; |
| 432 | LEAs[Key].push_front(MI); |
| 433 | } else { |
| 434 | lsize = (*Itr).second.size(); |
| 435 | (*Itr).second.push_front(MI); |
| 436 | } |
| 437 | if (TopScope->second.find(Key) == TopScope->second.end()) |
| 438 | TopScope->second[Key] = lsize; |
| 439 | } |
| 440 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 441 | class OptimizeLEAPass : public MachineFunctionPass { |
| 442 | public: |
| 443 | OptimizeLEAPass() : MachineFunctionPass(ID) {} |
| 444 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 445 | StringRef getPassName() const override { return "X86 LEA Optimize"; } |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 446 | |
| 447 | /// \brief Loop over all of the basic blocks, replacing address |
| 448 | /// calculations in load and store instructions, if it's already |
| 449 | /// been calculated by LEA. Also, remove redundant LEAs. |
| 450 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 451 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 452 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 453 | AU.setPreservesCFG(); |
| 454 | MachineFunctionPass::getAnalysisUsage(AU); |
| 455 | AU.addRequired<MachineDominatorTree>(); |
| 456 | } |
| 457 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 458 | private: |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 459 | typedef DenseMap<MemOpKey, SmallVector<MachineInstr *, 16>> MemOpMap; |
| 460 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 461 | /// \brief Returns a distance between two instructions inside one basic block. |
| 462 | /// Negative result means, that instructions occur in reverse order. |
| 463 | int calcInstrDist(const MachineInstr &First, const MachineInstr &Last); |
| 464 | |
| 465 | /// \brief Choose the best \p LEA instruction from the \p List to replace |
| 466 | /// address calculation in \p MI instruction. Return the address displacement |
Simon Pilgrim | 9d15fb3 | 2016-11-17 19:03:05 +0000 | [diff] [blame] | 467 | /// and the distance between \p MI and the chosen \p BestLEA in |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 468 | /// \p AddrDispShift and \p Dist. |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 469 | bool chooseBestLEA(const SmallVectorImpl<MachineInstr *> &List, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 470 | const MachineInstr &MI, MachineInstr *&BestLEA, |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 471 | int64_t &AddrDispShift, int &Dist); |
| 472 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 473 | /// \brief Returns the difference between addresses' displacements of \p MI1 |
| 474 | /// and \p MI2. The numbers of the first memory operands for the instructions |
| 475 | /// are specified through \p N1 and \p N2. |
| 476 | int64_t getAddrDispShift(const MachineInstr &MI1, unsigned N1, |
| 477 | const MachineInstr &MI2, unsigned N2) const; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 478 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 479 | /// \brief Returns true if the \p Last LEA instruction can be replaced by the |
| 480 | /// \p First. The difference between displacements of the addresses calculated |
| 481 | /// by these LEAs is returned in \p AddrDispShift. It'll be used for proper |
| 482 | /// replacement of the \p Last LEA's uses with the \p First's def register. |
| 483 | bool isReplaceable(const MachineInstr &First, const MachineInstr &Last, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 484 | int64_t &AddrDispShift) const; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 485 | |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 486 | /// \brief Find all LEA instructions in the basic block. Also, assign position |
| 487 | /// numbers to all instructions in the basic block to speed up calculation of |
| 488 | /// distance between them. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 489 | void findLEAs(const MachineBasicBlock &MBB, MemOpMap &LEAs); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 490 | |
| 491 | /// \brief Removes redundant address calculations. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 492 | bool removeRedundantAddrCalc(MemOpMap &LEAs); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 493 | |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 494 | /// Replace debug value MI with a new debug value instruction using register |
| 495 | /// VReg with an appropriate offset and DIExpression to incorporate the |
| 496 | /// address displacement AddrDispShift. Return new debug value instruction. |
| 497 | MachineInstr *replaceDebugValue(MachineInstr &MI, unsigned VReg, |
| 498 | int64_t AddrDispShift); |
| 499 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 500 | /// \brief Removes LEAs which calculate similar addresses. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 501 | bool removeRedundantLEAs(MemOpMap &LEAs); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 502 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 503 | /// \brief Visit over basic blocks, collect LEAs in a scoped |
| 504 | /// hash map (FactorizeLEAOpt::LEAs) and try to factor them out. |
| 505 | bool FactorizeLEAsAllBasicBlocks(MachineFunction &MF); |
| 506 | |
| 507 | bool FactorizeLEAsBasicBlock(MachineDomTreeNode *DN); |
| 508 | |
| 509 | /// \brief Factor out LEAs which share Base,Index,Offset and Segment. |
| 510 | bool processBasicBlock(const MachineBasicBlock &MBB); |
| 511 | |
| 512 | /// \brief Try to replace LEA with a lower strength instruction |
| 513 | /// to improves latency and throughput. |
| 514 | bool strengthReduceLEAs(MemOpMap &LEAs, const MachineBasicBlock &MBB); |
| 515 | |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 516 | DenseMap<const MachineInstr *, unsigned> InstrPos; |
| 517 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 518 | FactorizeLEAOpt FactorOpt; |
| 519 | |
| 520 | MachineDominatorTree *DT; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 521 | MachineRegisterInfo *MRI; |
| 522 | const X86InstrInfo *TII; |
| 523 | const X86RegisterInfo *TRI; |
| 524 | |
| 525 | static char ID; |
| 526 | }; |
| 527 | char OptimizeLEAPass::ID = 0; |
| 528 | } |
| 529 | |
| 530 | FunctionPass *llvm::createX86OptimizeLEAs() { return new OptimizeLEAPass(); } |
| 531 | |
| 532 | int OptimizeLEAPass::calcInstrDist(const MachineInstr &First, |
| 533 | const MachineInstr &Last) { |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 534 | // Both instructions must be in the same basic block and they must be |
| 535 | // presented in InstrPos. |
| 536 | assert(Last.getParent() == First.getParent() && |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 537 | "Instructions are in different basic blocks"); |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 538 | assert(InstrPos.find(&First) != InstrPos.end() && |
| 539 | InstrPos.find(&Last) != InstrPos.end() && |
| 540 | "Instructions' positions are undefined"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 541 | |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 542 | return InstrPos[&Last] - InstrPos[&First]; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | // Find the best LEA instruction in the List to replace address recalculation in |
| 546 | // MI. Such LEA must meet these requirements: |
| 547 | // 1) The address calculated by the LEA differs only by the displacement from |
| 548 | // the address used in MI. |
| 549 | // 2) The register class of the definition of the LEA is compatible with the |
| 550 | // register class of the address base register of MI. |
| 551 | // 3) Displacement of the new memory operand should fit in 1 byte if possible. |
| 552 | // 4) The LEA should be as close to MI as possible, and prior to it if |
| 553 | // possible. |
| 554 | bool OptimizeLEAPass::chooseBestLEA(const SmallVectorImpl<MachineInstr *> &List, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 555 | const MachineInstr &MI, |
| 556 | MachineInstr *&BestLEA, |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 557 | int64_t &AddrDispShift, int &Dist) { |
| 558 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 559 | const MCInstrDesc &Desc = MI.getDesc(); |
Craig Topper | 477649a | 2016-04-28 05:58:46 +0000 | [diff] [blame] | 560 | int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags) + |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 561 | X86II::getOperandBias(Desc); |
| 562 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 563 | BestLEA = nullptr; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 564 | |
| 565 | // Loop over all LEA instructions. |
| 566 | for (auto DefMI : List) { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 567 | // Get new address displacement. |
| 568 | int64_t AddrDispShiftTemp = getAddrDispShift(MI, MemOpNo, *DefMI, 1); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 569 | |
| 570 | // Make sure address displacement fits 4 bytes. |
| 571 | if (!isInt<32>(AddrDispShiftTemp)) |
| 572 | continue; |
| 573 | |
| 574 | // Check that LEA def register can be used as MI address base. Some |
| 575 | // instructions can use a limited set of registers as address base, for |
| 576 | // example MOV8mr_NOREX. We could constrain the register class of the LEA |
| 577 | // def to suit MI, however since this case is very rare and hard to |
| 578 | // reproduce in a test it's just more reliable to skip the LEA. |
| 579 | if (TII->getRegClass(Desc, MemOpNo + X86::AddrBaseReg, TRI, *MF) != |
| 580 | MRI->getRegClass(DefMI->getOperand(0).getReg())) |
| 581 | continue; |
| 582 | |
| 583 | // Choose the closest LEA instruction from the list, prior to MI if |
| 584 | // possible. Note that we took into account resulting address displacement |
| 585 | // as well. Also note that the list is sorted by the order in which the LEAs |
| 586 | // occur, so the break condition is pretty simple. |
| 587 | int DistTemp = calcInstrDist(*DefMI, MI); |
| 588 | assert(DistTemp != 0 && |
| 589 | "The distance between two different instructions cannot be zero"); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 590 | if (DistTemp > 0 || BestLEA == nullptr) { |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 591 | // Do not update return LEA, if the current one provides a displacement |
| 592 | // which fits in 1 byte, while the new candidate does not. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 593 | if (BestLEA != nullptr && !isInt<8>(AddrDispShiftTemp) && |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 594 | isInt<8>(AddrDispShift)) |
| 595 | continue; |
| 596 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 597 | BestLEA = DefMI; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 598 | AddrDispShift = AddrDispShiftTemp; |
| 599 | Dist = DistTemp; |
| 600 | } |
| 601 | |
| 602 | // FIXME: Maybe we should not always stop at the first LEA after MI. |
| 603 | if (DistTemp < 0) |
| 604 | break; |
| 605 | } |
| 606 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 607 | return BestLEA != nullptr; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 610 | // Get the difference between the addresses' displacements of the two |
| 611 | // instructions \p MI1 and \p MI2. The numbers of the first memory operands are |
| 612 | // passed through \p N1 and \p N2. |
| 613 | int64_t OptimizeLEAPass::getAddrDispShift(const MachineInstr &MI1, unsigned N1, |
| 614 | const MachineInstr &MI2, |
| 615 | unsigned N2) const { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 616 | const MachineOperand &Op1 = MI1.getOperand(N1 + X86::AddrDisp); |
| 617 | const MachineOperand &Op2 = MI2.getOperand(N2 + X86::AddrDisp); |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 618 | |
| 619 | assert(isSimilarDispOp(Op1, Op2) && |
| 620 | "Address displacement operands are not compatible"); |
| 621 | |
| 622 | // After the assert above we can be sure that both operands are of the same |
| 623 | // valid type and use the same symbol/index/address, thus displacement shift |
| 624 | // calculation is rather simple. |
| 625 | if (Op1.isJTI()) |
| 626 | return 0; |
| 627 | return Op1.isImm() ? Op1.getImm() - Op2.getImm() |
| 628 | : Op1.getOffset() - Op2.getOffset(); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 631 | // Check that the Last LEA can be replaced by the First LEA. To be so, |
| 632 | // these requirements must be met: |
| 633 | // 1) Addresses calculated by LEAs differ only by displacement. |
| 634 | // 2) Def registers of LEAs belong to the same class. |
| 635 | // 3) All uses of the Last LEA def register are replaceable, thus the |
| 636 | // register is used only as address base. |
| 637 | bool OptimizeLEAPass::isReplaceable(const MachineInstr &First, |
| 638 | const MachineInstr &Last, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 639 | int64_t &AddrDispShift) const { |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 640 | assert(isLEA(First) && isLEA(Last) && |
| 641 | "The function works only with LEA instructions"); |
| 642 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 643 | // Make sure that LEA def registers belong to the same class. There may be |
| 644 | // instructions (like MOV8mr_NOREX) which allow a limited set of registers to |
| 645 | // be used as their operands, so we must be sure that replacing one LEA |
| 646 | // with another won't lead to putting a wrong register in the instruction. |
| 647 | if (MRI->getRegClass(First.getOperand(0).getReg()) != |
| 648 | MRI->getRegClass(Last.getOperand(0).getReg())) |
| 649 | return false; |
| 650 | |
Andrea Di Biagio | 7937be7 | 2017-03-21 11:36:21 +0000 | [diff] [blame] | 651 | // Get new address displacement. |
| 652 | AddrDispShift = getAddrDispShift(Last, 1, First, 1); |
| 653 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 654 | // Loop over all uses of the Last LEA to check that its def register is |
| 655 | // used only as address base for memory accesses. If so, it can be |
| 656 | // replaced, otherwise - no. |
Andrea Di Biagio | 7937be7 | 2017-03-21 11:36:21 +0000 | [diff] [blame] | 657 | for (auto &MO : MRI->use_nodbg_operands(Last.getOperand(0).getReg())) { |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 658 | MachineInstr &MI = *MO.getParent(); |
| 659 | |
| 660 | // Get the number of the first memory operand. |
| 661 | const MCInstrDesc &Desc = MI.getDesc(); |
Craig Topper | 477649a | 2016-04-28 05:58:46 +0000 | [diff] [blame] | 662 | int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 663 | |
| 664 | // If the use instruction has no memory operand - the LEA is not |
| 665 | // replaceable. |
| 666 | if (MemOpNo < 0) |
| 667 | return false; |
| 668 | |
| 669 | MemOpNo += X86II::getOperandBias(Desc); |
| 670 | |
| 671 | // If the address base of the use instruction is not the LEA def register - |
| 672 | // the LEA is not replaceable. |
| 673 | if (!isIdenticalOp(MI.getOperand(MemOpNo + X86::AddrBaseReg), MO)) |
| 674 | return false; |
| 675 | |
| 676 | // If the LEA def register is used as any other operand of the use |
| 677 | // instruction - the LEA is not replaceable. |
| 678 | for (unsigned i = 0; i < MI.getNumOperands(); i++) |
| 679 | if (i != (unsigned)(MemOpNo + X86::AddrBaseReg) && |
| 680 | isIdenticalOp(MI.getOperand(i), MO)) |
| 681 | return false; |
| 682 | |
| 683 | // Check that the new address displacement will fit 4 bytes. |
| 684 | if (MI.getOperand(MemOpNo + X86::AddrDisp).isImm() && |
| 685 | !isInt<32>(MI.getOperand(MemOpNo + X86::AddrDisp).getImm() + |
| 686 | AddrDispShift)) |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | return true; |
| 691 | } |
| 692 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 693 | void OptimizeLEAPass::findLEAs(const MachineBasicBlock &MBB, MemOpMap &LEAs) { |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 694 | unsigned Pos = 0; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 695 | for (auto &MI : MBB) { |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 696 | // Assign the position number to the instruction. Note that we are going to |
| 697 | // move some instructions during the optimization however there will never |
| 698 | // be a need to move two instructions before any selected instruction. So to |
| 699 | // avoid multiple positions' updates during moves we just increase position |
| 700 | // counter by two leaving a free space for instructions which will be moved. |
| 701 | InstrPos[&MI] = Pos += 2; |
| 702 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 703 | if (isLEA(MI)) |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 704 | LEAs[getMemOpKey(MI, 1)].push_back(const_cast<MachineInstr *>(&MI)); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | |
| 708 | // Try to find load and store instructions which recalculate addresses already |
| 709 | // calculated by some LEA and replace their memory operands with its def |
| 710 | // register. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 711 | bool OptimizeLEAPass::removeRedundantAddrCalc(MemOpMap &LEAs) { |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 712 | bool Changed = false; |
| 713 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 714 | assert(!LEAs.empty()); |
| 715 | MachineBasicBlock *MBB = (*LEAs.begin()->second.begin())->getParent(); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 716 | |
| 717 | // Process all instructions in basic block. |
| 718 | for (auto I = MBB->begin(), E = MBB->end(); I != E;) { |
| 719 | MachineInstr &MI = *I++; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 720 | |
| 721 | // Instruction must be load or store. |
| 722 | if (!MI.mayLoadOrStore()) |
| 723 | continue; |
| 724 | |
| 725 | // Get the number of the first memory operand. |
| 726 | const MCInstrDesc &Desc = MI.getDesc(); |
Craig Topper | 477649a | 2016-04-28 05:58:46 +0000 | [diff] [blame] | 727 | int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 728 | |
| 729 | // If instruction has no memory operand - skip it. |
| 730 | if (MemOpNo < 0) |
| 731 | continue; |
| 732 | |
| 733 | MemOpNo += X86II::getOperandBias(Desc); |
| 734 | |
| 735 | // Get the best LEA instruction to replace address calculation. |
| 736 | MachineInstr *DefMI; |
| 737 | int64_t AddrDispShift; |
| 738 | int Dist; |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 739 | if (!chooseBestLEA(LEAs[getMemOpKey(MI, MemOpNo)], MI, DefMI, AddrDispShift, |
| 740 | Dist)) |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 741 | continue; |
| 742 | |
| 743 | // If LEA occurs before current instruction, we can freely replace |
| 744 | // the instruction. If LEA occurs after, we can lift LEA above the |
| 745 | // instruction and this way to be able to replace it. Since LEA and the |
| 746 | // instruction have similar memory operands (thus, the same def |
| 747 | // instructions for these operands), we can always do that, without |
| 748 | // worries of using registers before their defs. |
| 749 | if (Dist < 0) { |
| 750 | DefMI->removeFromParent(); |
| 751 | MBB->insert(MachineBasicBlock::iterator(&MI), DefMI); |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 752 | InstrPos[DefMI] = InstrPos[&MI] - 1; |
| 753 | |
| 754 | // Make sure the instructions' position numbers are sane. |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 755 | assert(((InstrPos[DefMI] == 1 && |
| 756 | MachineBasicBlock::iterator(DefMI) == MBB->begin()) || |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 757 | InstrPos[DefMI] > |
Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 758 | InstrPos[&*std::prev(MachineBasicBlock::iterator(DefMI))]) && |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 759 | "Instruction positioning is broken"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | // Since we can possibly extend register lifetime, clear kill flags. |
| 763 | MRI->clearKillFlags(DefMI->getOperand(0).getReg()); |
| 764 | |
| 765 | ++NumSubstLEAs; |
| 766 | DEBUG(dbgs() << "OptimizeLEAs: Candidate to replace: "; MI.dump();); |
| 767 | |
| 768 | // Change instruction operands. |
| 769 | MI.getOperand(MemOpNo + X86::AddrBaseReg) |
| 770 | .ChangeToRegister(DefMI->getOperand(0).getReg(), false); |
| 771 | MI.getOperand(MemOpNo + X86::AddrScaleAmt).ChangeToImmediate(1); |
| 772 | MI.getOperand(MemOpNo + X86::AddrIndexReg) |
| 773 | .ChangeToRegister(X86::NoRegister, false); |
| 774 | MI.getOperand(MemOpNo + X86::AddrDisp).ChangeToImmediate(AddrDispShift); |
| 775 | MI.getOperand(MemOpNo + X86::AddrSegmentReg) |
| 776 | .ChangeToRegister(X86::NoRegister, false); |
| 777 | |
| 778 | DEBUG(dbgs() << "OptimizeLEAs: Replaced by: "; MI.dump();); |
| 779 | |
| 780 | Changed = true; |
| 781 | } |
| 782 | |
| 783 | return Changed; |
| 784 | } |
| 785 | |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 786 | MachineInstr *OptimizeLEAPass::replaceDebugValue(MachineInstr &MI, |
| 787 | unsigned VReg, |
| 788 | int64_t AddrDispShift) { |
| 789 | DIExpression *Expr = const_cast<DIExpression *>(MI.getDebugExpression()); |
| 790 | |
Adrian Prantl | 109b236 | 2017-04-28 17:51:05 +0000 | [diff] [blame] | 791 | if (AddrDispShift != 0) |
| 792 | Expr = DIExpression::prepend(Expr, DIExpression::NoDeref, AddrDispShift, |
| 793 | DIExpression::WithStackValue); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 794 | |
| 795 | // Replace DBG_VALUE instruction with modified version. |
| 796 | MachineBasicBlock *MBB = MI.getParent(); |
| 797 | DebugLoc DL = MI.getDebugLoc(); |
| 798 | bool IsIndirect = MI.isIndirectDebugValue(); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 799 | const MDNode *Var = MI.getDebugVariable(); |
Adrian Prantl | 8b9bb53 | 2017-07-28 23:00:45 +0000 | [diff] [blame] | 800 | if (IsIndirect) |
| 801 | assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset"); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 802 | return BuildMI(*MBB, MBB->erase(&MI), DL, TII->get(TargetOpcode::DBG_VALUE), |
Adrian Prantl | 8b9bb53 | 2017-07-28 23:00:45 +0000 | [diff] [blame] | 803 | IsIndirect, VReg, Var, Expr); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 806 | // Try to find similar LEAs in the list and replace one with another. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 807 | bool OptimizeLEAPass::removeRedundantLEAs(MemOpMap &LEAs) { |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 808 | bool Changed = false; |
| 809 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 810 | // Loop over all entries in the table. |
| 811 | for (auto &E : LEAs) { |
| 812 | auto &List = E.second; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 813 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 814 | // Loop over all LEA pairs. |
| 815 | auto I1 = List.begin(); |
| 816 | while (I1 != List.end()) { |
| 817 | MachineInstr &First = **I1; |
| 818 | auto I2 = std::next(I1); |
| 819 | while (I2 != List.end()) { |
| 820 | MachineInstr &Last = **I2; |
| 821 | int64_t AddrDispShift; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 822 | |
Simon Pilgrim | 9d15fb3 | 2016-11-17 19:03:05 +0000 | [diff] [blame] | 823 | // LEAs should be in occurrence order in the list, so we can freely |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 824 | // replace later LEAs with earlier ones. |
| 825 | assert(calcInstrDist(First, Last) > 0 && |
Simon Pilgrim | 9d15fb3 | 2016-11-17 19:03:05 +0000 | [diff] [blame] | 826 | "LEAs must be in occurrence order in the list"); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 827 | |
| 828 | // Check that the Last LEA instruction can be replaced by the First. |
| 829 | if (!isReplaceable(First, Last, AddrDispShift)) { |
| 830 | ++I2; |
| 831 | continue; |
| 832 | } |
| 833 | |
| 834 | // Loop over all uses of the Last LEA and update their operands. Note |
| 835 | // that the correctness of this has already been checked in the |
| 836 | // isReplaceable function. |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 837 | unsigned FirstVReg = First.getOperand(0).getReg(); |
Andrea Di Biagio | 7937be7 | 2017-03-21 11:36:21 +0000 | [diff] [blame] | 838 | unsigned LastVReg = Last.getOperand(0).getReg(); |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 839 | for (auto UI = MRI->use_begin(LastVReg), UE = MRI->use_end(); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 840 | UI != UE;) { |
| 841 | MachineOperand &MO = *UI++; |
| 842 | MachineInstr &MI = *MO.getParent(); |
| 843 | |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 844 | if (MI.isDebugValue()) { |
| 845 | // Replace DBG_VALUE instruction with modified version using the |
| 846 | // register from the replacing LEA and the address displacement |
| 847 | // between the LEA instructions. |
| 848 | replaceDebugValue(MI, FirstVReg, AddrDispShift); |
| 849 | continue; |
| 850 | } |
| 851 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 852 | // Get the number of the first memory operand. |
| 853 | const MCInstrDesc &Desc = MI.getDesc(); |
| 854 | int MemOpNo = |
Craig Topper | 477649a | 2016-04-28 05:58:46 +0000 | [diff] [blame] | 855 | X86II::getMemoryOperandNo(Desc.TSFlags) + |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 856 | X86II::getOperandBias(Desc); |
| 857 | |
| 858 | // Update address base. |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 859 | MO.setReg(FirstVReg); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 860 | |
| 861 | // Update address disp. |
Andrey Turetskiy | 0babd26 | 2016-02-20 10:58:28 +0000 | [diff] [blame] | 862 | MachineOperand &Op = MI.getOperand(MemOpNo + X86::AddrDisp); |
| 863 | if (Op.isImm()) |
| 864 | Op.setImm(Op.getImm() + AddrDispShift); |
| 865 | else if (!Op.isJTI()) |
| 866 | Op.setOffset(Op.getOffset() + AddrDispShift); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | // Since we can possibly extend register lifetime, clear kill flags. |
Andrew Ng | 03e35b6 | 2017-04-28 08:44:30 +0000 | [diff] [blame] | 870 | MRI->clearKillFlags(FirstVReg); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 871 | |
| 872 | ++NumRedundantLEAs; |
| 873 | DEBUG(dbgs() << "OptimizeLEAs: Remove redundant LEA: "; Last.dump();); |
| 874 | |
| 875 | // By this moment, all of the Last LEA's uses must be replaced. So we |
| 876 | // can freely remove it. |
Andrea Di Biagio | 7937be7 | 2017-03-21 11:36:21 +0000 | [diff] [blame] | 877 | assert(MRI->use_empty(LastVReg) && |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 878 | "The LEA's def register must have no uses"); |
| 879 | Last.eraseFromParent(); |
| 880 | |
| 881 | // Erase removed LEA from the list. |
| 882 | I2 = List.erase(I2); |
| 883 | |
| 884 | Changed = true; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 885 | } |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 886 | ++I1; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 887 | } |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | return Changed; |
| 891 | } |
| 892 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 893 | static inline int getADDrrFromLEA(int LEAOpcode) { |
| 894 | switch (LEAOpcode) { |
| 895 | default: |
| 896 | llvm_unreachable("Unexpected LEA instruction"); |
| 897 | case X86::LEA16r: |
| 898 | return X86::ADD16rr; |
| 899 | case X86::LEA32r: |
| 900 | return X86::ADD32rr; |
| 901 | case X86::LEA64_32r: |
| 902 | case X86::LEA64r: |
| 903 | return X86::ADD64rr; |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | bool OptimizeLEAPass::strengthReduceLEAs(MemOpMap &LEAs, |
| 908 | const MachineBasicBlock &BB) { |
| 909 | bool Changed = false; |
| 910 | |
| 911 | // Loop over all entries in the table. |
| 912 | for (auto &E : LEAs) { |
| 913 | auto &List = E.second; |
| 914 | |
| 915 | // Loop over all LEA pairs. |
| 916 | for (auto I1 = List.begin(); I1 != List.end(); I1++) { |
| 917 | MachineInstrBuilder NewMI; |
| 918 | MachineInstr &First = **I1; |
| 919 | MachineOperand &Res = First.getOperand(0); |
| 920 | MachineOperand &Base = First.getOperand(1); |
| 921 | MachineOperand &Scale = First.getOperand(2); |
| 922 | MachineOperand &Index = First.getOperand(3); |
| 923 | MachineOperand &Offset = First.getOperand(4); |
| 924 | |
| 925 | const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(First.getOpcode())); |
| 926 | const DebugLoc DL = First.getDebugLoc(); |
| 927 | |
| 928 | if (!Base.isReg() || !Index.isReg()) |
| 929 | continue; |
| 930 | if (TargetRegisterInfo::isPhysicalRegister(Res.getReg()) || |
| 931 | TargetRegisterInfo::isPhysicalRegister(Base.getReg()) || |
| 932 | TargetRegisterInfo::isPhysicalRegister(Index.getReg())) |
| 933 | continue; |
| 934 | |
| 935 | MachineBasicBlock &MBB = *(const_cast<MachineBasicBlock *>(&BB)); |
| 936 | if (Scale.isImm() && Scale.getImm() == 1) { |
| 937 | // R = B + I |
| 938 | if (Offset.isImm() && !Offset.getImm()) { |
| 939 | NewMI = BuildMI(MBB, &First, DL, ADDrr) |
| 940 | .addDef(Res.getReg()) |
| 941 | .addUse(Base.getReg()) |
| 942 | .addUse(Index.getReg()); |
| 943 | Changed = NewMI.getInstr() != nullptr; |
| 944 | First.eraseFromParent(); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | return Changed; |
| 950 | } |
| 951 | |
| 952 | bool OptimizeLEAPass::processBasicBlock(const MachineBasicBlock &MBB) { |
| 953 | bool cseDone = false; |
| 954 | |
| 955 | // Legal scale value (1,2,4 & 8) vector. |
| 956 | int LegalScale[9] = {0, 1, 1, 0, 1, 0, 0, 0, 1}; |
| 957 | |
| 958 | auto CompareFn = [](const MachineInstr *Arg1, |
| 959 | const MachineInstr *Arg2) -> bool { |
| 960 | if (Arg1->getOperand(2).getImm() < Arg2->getOperand(2).getImm()) |
| 961 | return false; |
| 962 | return true; |
| 963 | }; |
| 964 | |
| 965 | // Loop over all entries in the table. |
| 966 | for (auto &E : FactorOpt.getLEAMap()) { |
| 967 | auto &List = E.second; |
| 968 | if (List.size() > 1) |
| 969 | List.sort(CompareFn); |
| 970 | |
| 971 | // Loop over all LEA pairs. |
| 972 | for (auto Iter1 = List.begin(); Iter1 != List.end(); Iter1++) { |
| 973 | for (auto Iter2 = std::next(Iter1); Iter2 != List.end(); Iter2++) { |
| 974 | MachineInstr &LI1 = **Iter1; |
| 975 | MachineInstr &LI2 = **Iter2; |
| 976 | |
| 977 | if (!DT->dominates(&LI2, &LI1)) |
| 978 | continue; |
| 979 | |
| 980 | int Scale1 = LI1.getOperand(2).getImm(); |
| 981 | int Scale2 = LI2.getOperand(2).getImm(); |
| 982 | assert(LI2.getOperand(0).isReg() && "Result is a VirtualReg"); |
| 983 | DebugLoc DL = LI1.getDebugLoc(); |
| 984 | |
| 985 | if (FactorOpt.checkIfScheduledForRemoval(&LI1)) |
| 986 | continue; |
| 987 | |
| 988 | int Factor = Scale1 - Scale2; |
| 989 | if (Factor > 0 && LegalScale[Factor]) { |
| 990 | DEBUG(dbgs() << "CSE LEAs: Candidate to replace: "; LI1.dump();); |
| 991 | MachineInstrBuilder NewMI = |
| 992 | BuildMI(*(const_cast<MachineBasicBlock *>(&MBB)), &LI1, DL, |
| 993 | TII->get(LI1.getOpcode())) |
| 994 | .addDef(LI1.getOperand(0).getReg()) // Dst = Dst of LI1. |
| 995 | .addUse(LI2.getOperand(0).getReg()) // Base = Dst of LI2. |
| 996 | .addImm(Factor) // Scale = Diff b/w scales. |
| 997 | .addUse(LI1.getOperand(3).getReg()) // Index = Index of LI1. |
| 998 | .addImm(0) // Disp = 0 |
| 999 | .addUse( |
| 1000 | LI1.getOperand(5).getReg()); // Segment = Segmant of LI1. |
| 1001 | |
| 1002 | cseDone = NewMI.getInstr() != nullptr; |
| 1003 | |
| 1004 | LI1.getOperand(0).setIsDef(false); |
| 1005 | |
| 1006 | /// Lazy removal shall ensure that replaced LEA remains |
| 1007 | /// till we finish processing all the basic block. This shall |
| 1008 | /// provide opportunity for further factorization based on |
| 1009 | /// the replaced LEA which will be legal since it has same |
| 1010 | /// destination as newly formed LEA. |
| 1011 | FactorOpt.addForLazyRemoval(&LI1); |
| 1012 | |
| 1013 | NumFactoredLEAs++; |
| 1014 | DEBUG(dbgs() << "CSE LEAs: Replaced by: "; NewMI->dump();); |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | return cseDone; |
| 1020 | } |
| 1021 | |
| 1022 | bool OptimizeLEAPass::FactorizeLEAsBasicBlock(MachineDomTreeNode *DN) { |
| 1023 | bool Changed = false; |
| 1024 | using StackT = SmallVector<MachineDomTreeNode* , 16>; |
| 1025 | using ProcessedMapT = DenseMap<MachineDomTreeNode* , bool>; |
| 1026 | |
| 1027 | StackT WorkList; |
| 1028 | ProcessedMapT ProcessesMap; |
| 1029 | |
| 1030 | WorkList.push_back(DN); |
| 1031 | while(!WorkList.empty()) { |
| 1032 | MachineDomTreeNode * MDN = WorkList.back(); |
| 1033 | if (ProcessesMap.find(MDN) == ProcessesMap.end()) { |
| 1034 | ProcessesMap[MDN] = true; |
| 1035 | FactorOpt.pushScope(MDN->getBlock()); |
| 1036 | Changed |= processBasicBlock(*MDN->getBlock()); |
| 1037 | for (auto Child : MDN->getChildren()) |
| 1038 | WorkList.push_back(Child); |
| 1039 | } |
| 1040 | MachineDomTreeNode *TDM = WorkList.back(); |
| 1041 | if (MDN->getLevel() == TDM->getLevel()) { |
| 1042 | FactorOpt.popScope(); |
| 1043 | WorkList.pop_back(); |
| 1044 | } |
| 1045 | } |
| 1046 | return Changed; |
| 1047 | } |
| 1048 | |
| 1049 | bool OptimizeLEAPass::FactorizeLEAsAllBasicBlocks(MachineFunction &MF) { |
| 1050 | bool Changed = FactorizeLEAsBasicBlock(DT->getRootNode()); |
| 1051 | FactorOpt.performCleanup(); |
| 1052 | return Changed; |
| 1053 | } |
| 1054 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1055 | bool OptimizeLEAPass::runOnMachineFunction(MachineFunction &MF) { |
| 1056 | bool Changed = false; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1057 | |
Andrey Turetskiy | 45b22a4 | 2016-05-19 10:18:29 +0000 | [diff] [blame] | 1058 | if (DisableX86LEAOpt || skipFunction(*MF.getFunction())) |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1059 | return false; |
| 1060 | |
| 1061 | MRI = &MF.getRegInfo(); |
| 1062 | TII = MF.getSubtarget<X86Subtarget>().getInstrInfo(); |
| 1063 | TRI = MF.getSubtarget<X86Subtarget>().getRegisterInfo(); |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 1064 | DT = &getAnalysis<MachineDominatorTree>(); |
| 1065 | |
| 1066 | // Attempt factorizing LEAs. |
| 1067 | Changed |= FactorizeLEAsAllBasicBlocks(MF); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1068 | |
| 1069 | // Process all basic blocks. |
| 1070 | for (auto &MBB : MF) { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 1071 | MemOpMap LEAs; |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 1072 | InstrPos.clear(); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1073 | |
| 1074 | // Find all LEA instructions in basic block. |
| 1075 | findLEAs(MBB, LEAs); |
| 1076 | |
| 1077 | // If current basic block has no LEAs, move on to the next one. |
| 1078 | if (LEAs.empty()) |
| 1079 | continue; |
| 1080 | |
Andrey Turetskiy | 45b22a4 | 2016-05-19 10:18:29 +0000 | [diff] [blame] | 1081 | // Remove redundant LEA instructions. |
| 1082 | Changed |= removeRedundantLEAs(LEAs); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 1083 | |
Jatin Bhateja | 3c29bac | 2017-10-04 09:02:10 +0000 | [diff] [blame] | 1084 | // Strength reduce LEA instructions. |
| 1085 | Changed |= strengthReduceLEAs(LEAs, MBB); |
| 1086 | |
Andrey Turetskiy | 45b22a4 | 2016-05-19 10:18:29 +0000 | [diff] [blame] | 1087 | // Remove redundant address calculations. Do it only for -Os/-Oz since only |
| 1088 | // a code size gain is expected from this part of the pass. |
| 1089 | if (MF.getFunction()->optForSize()) |
| 1090 | Changed |= removeRedundantAddrCalc(LEAs); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | return Changed; |
| 1094 | } |