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 |
| 11 | // instructions in order to improve 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" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 27 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 28 | #include "llvm/CodeGen/Passes.h" |
| 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
| 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | |
| 34 | using namespace llvm; |
| 35 | |
| 36 | #define DEBUG_TYPE "x86-optimize-LEAs" |
| 37 | |
Hans Wennborg | 8404789 | 2016-02-17 02:49:59 +0000 | [diff] [blame^] | 38 | static cl::opt<bool> EnableX86LEAOpt("enable-x86-lea-opt", cl::Hidden, |
| 39 | cl::desc("X86: Enable LEA optimizations."), |
| 40 | cl::init(false)); |
Alexey Bataev | 7b72b65 | 2015-12-17 07:34:39 +0000 | [diff] [blame] | 41 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 42 | STATISTIC(NumSubstLEAs, "Number of LEA instruction substitutions"); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 43 | STATISTIC(NumRedundantLEAs, "Number of redundant LEA instructions removed"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 44 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 45 | class MemOpKey; |
| 46 | |
| 47 | /// \brief Returns a hash table key based on memory operands of \p MI. The |
| 48 | /// number of the first memory operand of \p MI is specified through \p N. |
| 49 | static inline MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N); |
| 50 | |
| 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 | |
| 56 | /// \brief Returns true if the instruction is LEA. |
| 57 | static inline bool isLEA(const MachineInstr &MI); |
| 58 | |
| 59 | /// A key based on instruction's memory operands. |
| 60 | class MemOpKey { |
| 61 | public: |
| 62 | MemOpKey(const MachineOperand *Base, const MachineOperand *Scale, |
| 63 | const MachineOperand *Index, const MachineOperand *Segment, |
| 64 | const MachineOperand *Disp) |
| 65 | : Disp(Disp) { |
| 66 | Operands[0] = Base; |
| 67 | Operands[1] = Scale; |
| 68 | Operands[2] = Index; |
| 69 | Operands[3] = Segment; |
| 70 | } |
| 71 | |
| 72 | bool operator==(const MemOpKey &Other) const { |
| 73 | // Addresses' bases, scales, indices and segments must be identical. |
| 74 | for (int i = 0; i < 4; ++i) |
| 75 | if (!isIdenticalOp(*Operands[i], *Other.Operands[i])) |
| 76 | return false; |
| 77 | |
| 78 | assert((Disp->isImm() || Disp->isGlobal()) && |
| 79 | (Other.Disp->isImm() || Other.Disp->isGlobal()) && |
| 80 | "Address displacement operand is always an immediate or a global"); |
| 81 | |
| 82 | // Addresses' displacements must be either immediates or the same global. |
| 83 | // Immediates' and offsets' values don't matter for the operator since the |
| 84 | // difference will be taken care of during instruction substitution. |
| 85 | if ((Disp->isImm() && Other.Disp->isImm()) || |
| 86 | (Disp->isGlobal() && Other.Disp->isGlobal() && |
| 87 | Disp->getGlobal() == Other.Disp->getGlobal())) |
| 88 | return true; |
| 89 | |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | // Address' base, scale, index and segment operands. |
| 94 | const MachineOperand *Operands[4]; |
| 95 | |
| 96 | // Address' displacement operand. |
| 97 | const MachineOperand *Disp; |
| 98 | }; |
| 99 | |
| 100 | /// Provide DenseMapInfo for MemOpKey. |
| 101 | namespace llvm { |
| 102 | template <> struct DenseMapInfo<MemOpKey> { |
| 103 | typedef DenseMapInfo<const MachineOperand *> PtrInfo; |
| 104 | |
| 105 | static inline MemOpKey getEmptyKey() { |
| 106 | return MemOpKey(PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(), |
| 107 | PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(), |
| 108 | PtrInfo::getEmptyKey()); |
| 109 | } |
| 110 | |
| 111 | static inline MemOpKey getTombstoneKey() { |
| 112 | return MemOpKey(PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(), |
| 113 | PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(), |
| 114 | PtrInfo::getTombstoneKey()); |
| 115 | } |
| 116 | |
| 117 | static unsigned getHashValue(const MemOpKey &Val) { |
| 118 | // Checking any field of MemOpKey is enough to determine if the key is |
| 119 | // empty or tombstone. |
| 120 | assert(Val.Disp != PtrInfo::getEmptyKey() && "Cannot hash the empty key"); |
| 121 | assert(Val.Disp != PtrInfo::getTombstoneKey() && |
| 122 | "Cannot hash the tombstone key"); |
| 123 | |
| 124 | hash_code Hash = hash_combine(*Val.Operands[0], *Val.Operands[1], |
| 125 | *Val.Operands[2], *Val.Operands[3]); |
| 126 | |
| 127 | // If the address displacement is an immediate, it should not affect the |
| 128 | // hash so that memory operands which differ only be immediate displacement |
| 129 | // would have the same hash. If the address displacement is a global, we |
| 130 | // should reflect this global in the hash. |
| 131 | if (Val.Disp->isGlobal()) |
| 132 | Hash = hash_combine(Hash, Val.Disp->getGlobal()); |
| 133 | |
| 134 | return (unsigned)Hash; |
| 135 | } |
| 136 | |
| 137 | static bool isEqual(const MemOpKey &LHS, const MemOpKey &RHS) { |
| 138 | // Checking any field of MemOpKey is enough to determine if the key is |
| 139 | // empty or tombstone. |
| 140 | if (RHS.Disp == PtrInfo::getEmptyKey()) |
| 141 | return LHS.Disp == PtrInfo::getEmptyKey(); |
| 142 | if (RHS.Disp == PtrInfo::getTombstoneKey()) |
| 143 | return LHS.Disp == PtrInfo::getTombstoneKey(); |
| 144 | return LHS == RHS; |
| 145 | } |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | static inline MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N) { |
| 150 | assert((isLEA(MI) || MI.mayLoadOrStore()) && |
| 151 | "The instruction must be a LEA, a load or a store"); |
| 152 | return MemOpKey(&MI.getOperand(N + X86::AddrBaseReg), |
| 153 | &MI.getOperand(N + X86::AddrScaleAmt), |
| 154 | &MI.getOperand(N + X86::AddrIndexReg), |
| 155 | &MI.getOperand(N + X86::AddrSegmentReg), |
| 156 | &MI.getOperand(N + X86::AddrDisp)); |
| 157 | } |
| 158 | |
| 159 | static inline bool isIdenticalOp(const MachineOperand &MO1, |
| 160 | const MachineOperand &MO2) { |
| 161 | return MO1.isIdenticalTo(MO2) && |
| 162 | (!MO1.isReg() || |
| 163 | !TargetRegisterInfo::isPhysicalRegister(MO1.getReg())); |
| 164 | } |
| 165 | |
| 166 | static inline bool isLEA(const MachineInstr &MI) { |
| 167 | unsigned Opcode = MI.getOpcode(); |
| 168 | return Opcode == X86::LEA16r || Opcode == X86::LEA32r || |
| 169 | Opcode == X86::LEA64r || Opcode == X86::LEA64_32r; |
| 170 | } |
| 171 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 172 | namespace { |
| 173 | class OptimizeLEAPass : public MachineFunctionPass { |
| 174 | public: |
| 175 | OptimizeLEAPass() : MachineFunctionPass(ID) {} |
| 176 | |
| 177 | const char *getPassName() const override { return "X86 LEA Optimize"; } |
| 178 | |
| 179 | /// \brief Loop over all of the basic blocks, replacing address |
| 180 | /// calculations in load and store instructions, if it's already |
| 181 | /// been calculated by LEA. Also, remove redundant LEAs. |
| 182 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 183 | |
| 184 | private: |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 185 | typedef DenseMap<MemOpKey, SmallVector<MachineInstr *, 16>> MemOpMap; |
| 186 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 187 | /// \brief Returns a distance between two instructions inside one basic block. |
| 188 | /// Negative result means, that instructions occur in reverse order. |
| 189 | int calcInstrDist(const MachineInstr &First, const MachineInstr &Last); |
| 190 | |
| 191 | /// \brief Choose the best \p LEA instruction from the \p List to replace |
| 192 | /// address calculation in \p MI instruction. Return the address displacement |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 193 | /// and the distance between \p MI and the choosen \p BestLEA in |
| 194 | /// \p AddrDispShift and \p Dist. |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 195 | bool chooseBestLEA(const SmallVectorImpl<MachineInstr *> &List, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 196 | const MachineInstr &MI, MachineInstr *&BestLEA, |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 197 | int64_t &AddrDispShift, int &Dist); |
| 198 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 199 | /// \brief Returns the difference between addresses' displacements of \p MI1 |
| 200 | /// and \p MI2. The numbers of the first memory operands for the instructions |
| 201 | /// are specified through \p N1 and \p N2. |
| 202 | int64_t getAddrDispShift(const MachineInstr &MI1, unsigned N1, |
| 203 | const MachineInstr &MI2, unsigned N2) const; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 204 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 205 | /// \brief Returns true if the \p Last LEA instruction can be replaced by the |
| 206 | /// \p First. The difference between displacements of the addresses calculated |
| 207 | /// by these LEAs is returned in \p AddrDispShift. It'll be used for proper |
| 208 | /// replacement of the \p Last LEA's uses with the \p First's def register. |
| 209 | bool isReplaceable(const MachineInstr &First, const MachineInstr &Last, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 210 | int64_t &AddrDispShift) const; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 211 | |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 212 | /// \brief Find all LEA instructions in the basic block. Also, assign position |
| 213 | /// numbers to all instructions in the basic block to speed up calculation of |
| 214 | /// distance between them. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 215 | void findLEAs(const MachineBasicBlock &MBB, MemOpMap &LEAs); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 216 | |
| 217 | /// \brief Removes redundant address calculations. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 218 | bool removeRedundantAddrCalc(MemOpMap &LEAs); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 219 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 220 | /// \brief Removes LEAs which calculate similar addresses. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 221 | bool removeRedundantLEAs(MemOpMap &LEAs); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 222 | |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 223 | DenseMap<const MachineInstr *, unsigned> InstrPos; |
| 224 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 225 | MachineRegisterInfo *MRI; |
| 226 | const X86InstrInfo *TII; |
| 227 | const X86RegisterInfo *TRI; |
| 228 | |
| 229 | static char ID; |
| 230 | }; |
| 231 | char OptimizeLEAPass::ID = 0; |
| 232 | } |
| 233 | |
| 234 | FunctionPass *llvm::createX86OptimizeLEAs() { return new OptimizeLEAPass(); } |
| 235 | |
| 236 | int OptimizeLEAPass::calcInstrDist(const MachineInstr &First, |
| 237 | const MachineInstr &Last) { |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 238 | // Both instructions must be in the same basic block and they must be |
| 239 | // presented in InstrPos. |
| 240 | assert(Last.getParent() == First.getParent() && |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 241 | "Instructions are in different basic blocks"); |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 242 | assert(InstrPos.find(&First) != InstrPos.end() && |
| 243 | InstrPos.find(&Last) != InstrPos.end() && |
| 244 | "Instructions' positions are undefined"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 245 | |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 246 | return InstrPos[&Last] - InstrPos[&First]; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // Find the best LEA instruction in the List to replace address recalculation in |
| 250 | // MI. Such LEA must meet these requirements: |
| 251 | // 1) The address calculated by the LEA differs only by the displacement from |
| 252 | // the address used in MI. |
| 253 | // 2) The register class of the definition of the LEA is compatible with the |
| 254 | // register class of the address base register of MI. |
| 255 | // 3) Displacement of the new memory operand should fit in 1 byte if possible. |
| 256 | // 4) The LEA should be as close to MI as possible, and prior to it if |
| 257 | // possible. |
| 258 | bool OptimizeLEAPass::chooseBestLEA(const SmallVectorImpl<MachineInstr *> &List, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 259 | const MachineInstr &MI, |
| 260 | MachineInstr *&BestLEA, |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 261 | int64_t &AddrDispShift, int &Dist) { |
| 262 | const MachineFunction *MF = MI.getParent()->getParent(); |
| 263 | const MCInstrDesc &Desc = MI.getDesc(); |
| 264 | int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags, MI.getOpcode()) + |
| 265 | X86II::getOperandBias(Desc); |
| 266 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 267 | BestLEA = nullptr; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 268 | |
| 269 | // Loop over all LEA instructions. |
| 270 | for (auto DefMI : List) { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 271 | // Get new address displacement. |
| 272 | int64_t AddrDispShiftTemp = getAddrDispShift(MI, MemOpNo, *DefMI, 1); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 273 | |
| 274 | // Make sure address displacement fits 4 bytes. |
| 275 | if (!isInt<32>(AddrDispShiftTemp)) |
| 276 | continue; |
| 277 | |
| 278 | // Check that LEA def register can be used as MI address base. Some |
| 279 | // instructions can use a limited set of registers as address base, for |
| 280 | // example MOV8mr_NOREX. We could constrain the register class of the LEA |
| 281 | // def to suit MI, however since this case is very rare and hard to |
| 282 | // reproduce in a test it's just more reliable to skip the LEA. |
| 283 | if (TII->getRegClass(Desc, MemOpNo + X86::AddrBaseReg, TRI, *MF) != |
| 284 | MRI->getRegClass(DefMI->getOperand(0).getReg())) |
| 285 | continue; |
| 286 | |
| 287 | // Choose the closest LEA instruction from the list, prior to MI if |
| 288 | // possible. Note that we took into account resulting address displacement |
| 289 | // as well. Also note that the list is sorted by the order in which the LEAs |
| 290 | // occur, so the break condition is pretty simple. |
| 291 | int DistTemp = calcInstrDist(*DefMI, MI); |
| 292 | assert(DistTemp != 0 && |
| 293 | "The distance between two different instructions cannot be zero"); |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 294 | if (DistTemp > 0 || BestLEA == nullptr) { |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 295 | // Do not update return LEA, if the current one provides a displacement |
| 296 | // which fits in 1 byte, while the new candidate does not. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 297 | if (BestLEA != nullptr && !isInt<8>(AddrDispShiftTemp) && |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 298 | isInt<8>(AddrDispShift)) |
| 299 | continue; |
| 300 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 301 | BestLEA = DefMI; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 302 | AddrDispShift = AddrDispShiftTemp; |
| 303 | Dist = DistTemp; |
| 304 | } |
| 305 | |
| 306 | // FIXME: Maybe we should not always stop at the first LEA after MI. |
| 307 | if (DistTemp < 0) |
| 308 | break; |
| 309 | } |
| 310 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 311 | return BestLEA != nullptr; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 314 | // Get the difference between the addresses' displacements of the two |
| 315 | // instructions \p MI1 and \p MI2. The numbers of the first memory operands are |
| 316 | // passed through \p N1 and \p N2. |
| 317 | int64_t OptimizeLEAPass::getAddrDispShift(const MachineInstr &MI1, unsigned N1, |
| 318 | const MachineInstr &MI2, |
| 319 | unsigned N2) const { |
| 320 | // Address displacement operands may differ by a constant. |
| 321 | const MachineOperand &Op1 = MI1.getOperand(N1 + X86::AddrDisp); |
| 322 | const MachineOperand &Op2 = MI2.getOperand(N2 + X86::AddrDisp); |
| 323 | if (Op1.isImm() && Op2.isImm()) |
| 324 | return Op1.getImm() - Op2.getImm(); |
| 325 | else if (Op1.isGlobal() && Op2.isGlobal() && |
| 326 | Op1.getGlobal() == Op2.getGlobal()) |
| 327 | return Op1.getOffset() - Op2.getOffset(); |
| 328 | else |
| 329 | llvm_unreachable("Invalid address displacement operand"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 332 | // Check that the Last LEA can be replaced by the First LEA. To be so, |
| 333 | // these requirements must be met: |
| 334 | // 1) Addresses calculated by LEAs differ only by displacement. |
| 335 | // 2) Def registers of LEAs belong to the same class. |
| 336 | // 3) All uses of the Last LEA def register are replaceable, thus the |
| 337 | // register is used only as address base. |
| 338 | bool OptimizeLEAPass::isReplaceable(const MachineInstr &First, |
| 339 | const MachineInstr &Last, |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 340 | int64_t &AddrDispShift) const { |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 341 | assert(isLEA(First) && isLEA(Last) && |
| 342 | "The function works only with LEA instructions"); |
| 343 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 344 | // Get new address displacement. |
| 345 | AddrDispShift = getAddrDispShift(Last, 1, First, 1); |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 346 | |
| 347 | // Make sure that LEA def registers belong to the same class. There may be |
| 348 | // instructions (like MOV8mr_NOREX) which allow a limited set of registers to |
| 349 | // be used as their operands, so we must be sure that replacing one LEA |
| 350 | // with another won't lead to putting a wrong register in the instruction. |
| 351 | if (MRI->getRegClass(First.getOperand(0).getReg()) != |
| 352 | MRI->getRegClass(Last.getOperand(0).getReg())) |
| 353 | return false; |
| 354 | |
| 355 | // Loop over all uses of the Last LEA to check that its def register is |
| 356 | // used only as address base for memory accesses. If so, it can be |
| 357 | // replaced, otherwise - no. |
| 358 | for (auto &MO : MRI->use_operands(Last.getOperand(0).getReg())) { |
| 359 | MachineInstr &MI = *MO.getParent(); |
| 360 | |
| 361 | // Get the number of the first memory operand. |
| 362 | const MCInstrDesc &Desc = MI.getDesc(); |
| 363 | int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags, MI.getOpcode()); |
| 364 | |
| 365 | // If the use instruction has no memory operand - the LEA is not |
| 366 | // replaceable. |
| 367 | if (MemOpNo < 0) |
| 368 | return false; |
| 369 | |
| 370 | MemOpNo += X86II::getOperandBias(Desc); |
| 371 | |
| 372 | // If the address base of the use instruction is not the LEA def register - |
| 373 | // the LEA is not replaceable. |
| 374 | if (!isIdenticalOp(MI.getOperand(MemOpNo + X86::AddrBaseReg), MO)) |
| 375 | return false; |
| 376 | |
| 377 | // If the LEA def register is used as any other operand of the use |
| 378 | // instruction - the LEA is not replaceable. |
| 379 | for (unsigned i = 0; i < MI.getNumOperands(); i++) |
| 380 | if (i != (unsigned)(MemOpNo + X86::AddrBaseReg) && |
| 381 | isIdenticalOp(MI.getOperand(i), MO)) |
| 382 | return false; |
| 383 | |
| 384 | // Check that the new address displacement will fit 4 bytes. |
| 385 | if (MI.getOperand(MemOpNo + X86::AddrDisp).isImm() && |
| 386 | !isInt<32>(MI.getOperand(MemOpNo + X86::AddrDisp).getImm() + |
| 387 | AddrDispShift)) |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 394 | void OptimizeLEAPass::findLEAs(const MachineBasicBlock &MBB, MemOpMap &LEAs) { |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 395 | unsigned Pos = 0; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 396 | for (auto &MI : MBB) { |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 397 | // Assign the position number to the instruction. Note that we are going to |
| 398 | // move some instructions during the optimization however there will never |
| 399 | // be a need to move two instructions before any selected instruction. So to |
| 400 | // avoid multiple positions' updates during moves we just increase position |
| 401 | // counter by two leaving a free space for instructions which will be moved. |
| 402 | InstrPos[&MI] = Pos += 2; |
| 403 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 404 | if (isLEA(MI)) |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 405 | LEAs[getMemOpKey(MI, 1)].push_back(const_cast<MachineInstr *>(&MI)); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | // Try to find load and store instructions which recalculate addresses already |
| 410 | // calculated by some LEA and replace their memory operands with its def |
| 411 | // register. |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 412 | bool OptimizeLEAPass::removeRedundantAddrCalc(MemOpMap &LEAs) { |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 413 | bool Changed = false; |
| 414 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 415 | assert(!LEAs.empty()); |
| 416 | MachineBasicBlock *MBB = (*LEAs.begin()->second.begin())->getParent(); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 417 | |
| 418 | // Process all instructions in basic block. |
| 419 | for (auto I = MBB->begin(), E = MBB->end(); I != E;) { |
| 420 | MachineInstr &MI = *I++; |
| 421 | unsigned Opcode = MI.getOpcode(); |
| 422 | |
| 423 | // Instruction must be load or store. |
| 424 | if (!MI.mayLoadOrStore()) |
| 425 | continue; |
| 426 | |
| 427 | // Get the number of the first memory operand. |
| 428 | const MCInstrDesc &Desc = MI.getDesc(); |
| 429 | int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags, Opcode); |
| 430 | |
| 431 | // If instruction has no memory operand - skip it. |
| 432 | if (MemOpNo < 0) |
| 433 | continue; |
| 434 | |
| 435 | MemOpNo += X86II::getOperandBias(Desc); |
| 436 | |
Andrey Turetskiy | 1052ac2 | 2016-02-16 12:47:45 +0000 | [diff] [blame] | 437 | // Address displacement must be an immediate or a global. |
| 438 | MachineOperand &Disp = MI.getOperand(MemOpNo + X86::AddrDisp); |
| 439 | if (!Disp.isImm() && !Disp.isGlobal()) |
| 440 | continue; |
| 441 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 442 | // Get the best LEA instruction to replace address calculation. |
| 443 | MachineInstr *DefMI; |
| 444 | int64_t AddrDispShift; |
| 445 | int Dist; |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 446 | if (!chooseBestLEA(LEAs[getMemOpKey(MI, MemOpNo)], MI, DefMI, AddrDispShift, |
| 447 | Dist)) |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 448 | continue; |
| 449 | |
| 450 | // If LEA occurs before current instruction, we can freely replace |
| 451 | // the instruction. If LEA occurs after, we can lift LEA above the |
| 452 | // instruction and this way to be able to replace it. Since LEA and the |
| 453 | // instruction have similar memory operands (thus, the same def |
| 454 | // instructions for these operands), we can always do that, without |
| 455 | // worries of using registers before their defs. |
| 456 | if (Dist < 0) { |
| 457 | DefMI->removeFromParent(); |
| 458 | MBB->insert(MachineBasicBlock::iterator(&MI), DefMI); |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 459 | InstrPos[DefMI] = InstrPos[&MI] - 1; |
| 460 | |
| 461 | // Make sure the instructions' position numbers are sane. |
| 462 | assert(((InstrPos[DefMI] == 1 && DefMI == MBB->begin()) || |
| 463 | InstrPos[DefMI] > |
| 464 | InstrPos[std::prev(MachineBasicBlock::iterator(DefMI))]) && |
| 465 | "Instruction positioning is broken"); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // Since we can possibly extend register lifetime, clear kill flags. |
| 469 | MRI->clearKillFlags(DefMI->getOperand(0).getReg()); |
| 470 | |
| 471 | ++NumSubstLEAs; |
| 472 | DEBUG(dbgs() << "OptimizeLEAs: Candidate to replace: "; MI.dump();); |
| 473 | |
| 474 | // Change instruction operands. |
| 475 | MI.getOperand(MemOpNo + X86::AddrBaseReg) |
| 476 | .ChangeToRegister(DefMI->getOperand(0).getReg(), false); |
| 477 | MI.getOperand(MemOpNo + X86::AddrScaleAmt).ChangeToImmediate(1); |
| 478 | MI.getOperand(MemOpNo + X86::AddrIndexReg) |
| 479 | .ChangeToRegister(X86::NoRegister, false); |
| 480 | MI.getOperand(MemOpNo + X86::AddrDisp).ChangeToImmediate(AddrDispShift); |
| 481 | MI.getOperand(MemOpNo + X86::AddrSegmentReg) |
| 482 | .ChangeToRegister(X86::NoRegister, false); |
| 483 | |
| 484 | DEBUG(dbgs() << "OptimizeLEAs: Replaced by: "; MI.dump();); |
| 485 | |
| 486 | Changed = true; |
| 487 | } |
| 488 | |
| 489 | return Changed; |
| 490 | } |
| 491 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 492 | // 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] | 493 | bool OptimizeLEAPass::removeRedundantLEAs(MemOpMap &LEAs) { |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 494 | bool Changed = false; |
| 495 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 496 | // Loop over all entries in the table. |
| 497 | for (auto &E : LEAs) { |
| 498 | auto &List = E.second; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 499 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 500 | // Loop over all LEA pairs. |
| 501 | auto I1 = List.begin(); |
| 502 | while (I1 != List.end()) { |
| 503 | MachineInstr &First = **I1; |
| 504 | auto I2 = std::next(I1); |
| 505 | while (I2 != List.end()) { |
| 506 | MachineInstr &Last = **I2; |
| 507 | int64_t AddrDispShift; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 508 | |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 509 | // LEAs should be in occurence order in the list, so we can freely |
| 510 | // replace later LEAs with earlier ones. |
| 511 | assert(calcInstrDist(First, Last) > 0 && |
| 512 | "LEAs must be in occurence order in the list"); |
| 513 | |
| 514 | // Check that the Last LEA instruction can be replaced by the First. |
| 515 | if (!isReplaceable(First, Last, AddrDispShift)) { |
| 516 | ++I2; |
| 517 | continue; |
| 518 | } |
| 519 | |
| 520 | // Loop over all uses of the Last LEA and update their operands. Note |
| 521 | // that the correctness of this has already been checked in the |
| 522 | // isReplaceable function. |
| 523 | for (auto UI = MRI->use_begin(Last.getOperand(0).getReg()), |
| 524 | UE = MRI->use_end(); |
| 525 | UI != UE;) { |
| 526 | MachineOperand &MO = *UI++; |
| 527 | MachineInstr &MI = *MO.getParent(); |
| 528 | |
| 529 | // Get the number of the first memory operand. |
| 530 | const MCInstrDesc &Desc = MI.getDesc(); |
| 531 | int MemOpNo = |
| 532 | X86II::getMemoryOperandNo(Desc.TSFlags, MI.getOpcode()) + |
| 533 | X86II::getOperandBias(Desc); |
| 534 | |
| 535 | // Update address base. |
| 536 | MO.setReg(First.getOperand(0).getReg()); |
| 537 | |
| 538 | // Update address disp. |
| 539 | MachineOperand *Op = &MI.getOperand(MemOpNo + X86::AddrDisp); |
| 540 | if (Op->isImm()) |
| 541 | Op->setImm(Op->getImm() + AddrDispShift); |
| 542 | else if (Op->isGlobal()) |
| 543 | Op->setOffset(Op->getOffset() + AddrDispShift); |
| 544 | else |
| 545 | llvm_unreachable("Invalid address displacement operand"); |
| 546 | } |
| 547 | |
| 548 | // Since we can possibly extend register lifetime, clear kill flags. |
| 549 | MRI->clearKillFlags(First.getOperand(0).getReg()); |
| 550 | |
| 551 | ++NumRedundantLEAs; |
| 552 | DEBUG(dbgs() << "OptimizeLEAs: Remove redundant LEA: "; Last.dump();); |
| 553 | |
| 554 | // By this moment, all of the Last LEA's uses must be replaced. So we |
| 555 | // can freely remove it. |
| 556 | assert(MRI->use_empty(Last.getOperand(0).getReg()) && |
| 557 | "The LEA's def register must have no uses"); |
| 558 | Last.eraseFromParent(); |
| 559 | |
| 560 | // Erase removed LEA from the list. |
| 561 | I2 = List.erase(I2); |
| 562 | |
| 563 | Changed = true; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 564 | } |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 565 | ++I1; |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 566 | } |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return Changed; |
| 570 | } |
| 571 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 572 | bool OptimizeLEAPass::runOnMachineFunction(MachineFunction &MF) { |
| 573 | bool Changed = false; |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 574 | |
| 575 | // Perform this optimization only if we care about code size. |
Hans Wennborg | 8404789 | 2016-02-17 02:49:59 +0000 | [diff] [blame^] | 576 | if (!EnableX86LEAOpt || !MF.getFunction()->optForSize()) |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 577 | return false; |
| 578 | |
| 579 | MRI = &MF.getRegInfo(); |
| 580 | TII = MF.getSubtarget<X86Subtarget>().getInstrInfo(); |
| 581 | TRI = MF.getSubtarget<X86Subtarget>().getRegisterInfo(); |
| 582 | |
| 583 | // Process all basic blocks. |
| 584 | for (auto &MBB : MF) { |
Andrey Turetskiy | bca0f99 | 2016-02-04 08:57:03 +0000 | [diff] [blame] | 585 | MemOpMap LEAs; |
Alexey Bataev | 28f0c5e | 2016-01-11 11:52:29 +0000 | [diff] [blame] | 586 | InstrPos.clear(); |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 587 | |
| 588 | // Find all LEA instructions in basic block. |
| 589 | findLEAs(MBB, LEAs); |
| 590 | |
| 591 | // If current basic block has no LEAs, move on to the next one. |
| 592 | if (LEAs.empty()) |
| 593 | continue; |
| 594 | |
Andrey Turetskiy | 1ce2c99 | 2016-01-13 11:30:44 +0000 | [diff] [blame] | 595 | // Remove redundant LEA instructions. The optimization may have a negative |
| 596 | // effect on performance, so do it only for -Oz. |
| 597 | if (MF.getFunction()->optForMinSize()) |
| 598 | Changed |= removeRedundantLEAs(LEAs); |
| 599 | |
Alexey Bataev | 7cf3247 | 2015-12-04 10:53:15 +0000 | [diff] [blame] | 600 | // Remove redundant address calculations. |
| 601 | Changed |= removeRedundantAddrCalc(LEAs); |
| 602 | } |
| 603 | |
| 604 | return Changed; |
| 605 | } |