Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 1 | //===-- X86FixupLEAs.cpp - use or replace 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 | // |
Sanjay Patel | 6360441 | 2014-07-16 20:18:49 +0000 | [diff] [blame] | 10 | // This file defines the pass that finds instructions that can be |
| 11 | // re-written as LEA instructions in order to reduce pipeline delays. |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 12 | // When optimizing for size it replaces suitable LEAs with INC or DEC. |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 16 | #include "X86.h" |
| 17 | #include "X86InstrInfo.h" |
| 18 | #include "X86Subtarget.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
| 20 | #include "llvm/CodeGen/LiveVariables.h" |
| 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 24 | #include "llvm/CodeGen/Passes.h" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/raw_ostream.h" |
| 27 | #include "llvm/Target/TargetInstrInfo.h" |
| 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "x86-fixup-LEAs" |
| 31 | |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 32 | STATISTIC(NumLEAs, "Number of LEA instructions created"); |
| 33 | |
| 34 | namespace { |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 35 | class FixupLEAPass : public MachineFunctionPass { |
| 36 | enum RegUsageState { RU_NotUsed, RU_Write, RU_Read }; |
| 37 | static char ID; |
| 38 | /// \brief Loop over all of the instructions in the basic block |
| 39 | /// replacing applicable instructions with LEA instructions, |
| 40 | /// where appropriate. |
| 41 | bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 42 | |
Sanjay Patel | 6360441 | 2014-07-16 20:18:49 +0000 | [diff] [blame] | 43 | const char *getPassName() const override { return "X86 LEA Fixup"; } |
Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 44 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 45 | /// \brief Given a machine register, look for the instruction |
| 46 | /// which writes it in the current basic block. If found, |
| 47 | /// try to replace it with an equivalent LEA instruction. |
Eric Christopher | 572e03a | 2015-06-19 01:53:21 +0000 | [diff] [blame] | 48 | /// If replacement succeeds, then also process the newly created |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 49 | /// instruction. |
| 50 | void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I, |
| 51 | MachineFunction::iterator MFI); |
Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 52 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 53 | /// \brief Given a memory access or LEA instruction |
| 54 | /// whose address mode uses a base and/or index register, look for |
| 55 | /// an opportunity to replace the instruction which sets the base or index |
| 56 | /// register with an equivalent LEA instruction. |
| 57 | void processInstruction(MachineBasicBlock::iterator &I, |
| 58 | MachineFunction::iterator MFI); |
Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 59 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 60 | /// \brief Given a LEA instruction which is unprofitable |
| 61 | /// on Silvermont try to replace it with an equivalent ADD instruction |
| 62 | void processInstructionForSLM(MachineBasicBlock::iterator &I, |
| 63 | MachineFunction::iterator MFI); |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 64 | |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 65 | /// \brief Look for LEAs that add 1 to reg or subtract 1 from reg |
| 66 | /// and convert them to INC or DEC respectively. |
| 67 | bool fixupIncDec(MachineBasicBlock::iterator &I, |
| 68 | MachineFunction::iterator MFI) const; |
| 69 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 70 | /// \brief Determine if an instruction references a machine register |
| 71 | /// and, if so, whether it reads or writes the register. |
| 72 | RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I); |
Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 73 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 74 | /// \brief Step backwards through a basic block, looking |
| 75 | /// for an instruction which writes a register within |
| 76 | /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles. |
| 77 | MachineBasicBlock::iterator searchBackwards(MachineOperand &p, |
| 78 | MachineBasicBlock::iterator &I, |
| 79 | MachineFunction::iterator MFI); |
Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 80 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 81 | /// \brief if an instruction can be converted to an |
| 82 | /// equivalent LEA, insert the new instruction into the basic block |
| 83 | /// and return a pointer to it. Otherwise, return zero. |
| 84 | MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI, |
| 85 | MachineBasicBlock::iterator &MBBI) const; |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 86 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 87 | public: |
| 88 | FixupLEAPass() : MachineFunctionPass(ID) {} |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 89 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 90 | /// \brief Loop over all of the basic blocks, |
| 91 | /// replacing instructions by equivalent LEA instructions |
| 92 | /// if needed and when possible. |
| 93 | bool runOnMachineFunction(MachineFunction &MF) override; |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 94 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 95 | // This pass runs after regalloc and doesn't support VReg operands. |
| 96 | MachineFunctionProperties getRequiredProperties() const override { |
| 97 | return MachineFunctionProperties().set( |
| 98 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 99 | } |
| 100 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 101 | private: |
| 102 | MachineFunction *MF; |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 103 | const X86InstrInfo *TII; // Machine instruction info. |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 104 | bool OptIncDec; |
| 105 | bool OptLEA; |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 106 | }; |
| 107 | char FixupLEAPass::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 108 | } |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 109 | |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 110 | MachineInstr * |
| 111 | FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI, |
Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 112 | MachineBasicBlock::iterator &MBBI) const { |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 113 | MachineInstr *MI = MBBI; |
| 114 | MachineInstr *NewMI; |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 115 | switch (MI->getOpcode()) { |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 116 | case X86::MOV32rr: |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 117 | case X86::MOV64rr: { |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 118 | const MachineOperand &Src = MI->getOperand(1); |
| 119 | const MachineOperand &Dest = MI->getOperand(0); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 120 | NewMI = BuildMI(*MF, MI->getDebugLoc(), |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 121 | TII->get(MI->getOpcode() == X86::MOV32rr ? X86::LEA32r |
| 122 | : X86::LEA64r)) |
| 123 | .addOperand(Dest) |
| 124 | .addOperand(Src) |
| 125 | .addImm(1) |
| 126 | .addReg(0) |
| 127 | .addImm(0) |
| 128 | .addReg(0); |
| 129 | MFI->insert(MBBI, NewMI); // Insert the new inst |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 130 | return NewMI; |
| 131 | } |
| 132 | case X86::ADD64ri32: |
| 133 | case X86::ADD64ri8: |
| 134 | case X86::ADD64ri32_DB: |
| 135 | case X86::ADD64ri8_DB: |
| 136 | case X86::ADD32ri: |
| 137 | case X86::ADD32ri8: |
| 138 | case X86::ADD32ri_DB: |
| 139 | case X86::ADD32ri8_DB: |
| 140 | case X86::ADD16ri: |
| 141 | case X86::ADD16ri8: |
| 142 | case X86::ADD16ri_DB: |
| 143 | case X86::ADD16ri8_DB: |
| 144 | if (!MI->getOperand(2).isImm()) { |
| 145 | // convertToThreeAddress will call getImm() |
| 146 | // which requires isImm() to be true |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 147 | return nullptr; |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 148 | } |
Preston Gurd | f03a6e7 | 2013-09-30 23:51:22 +0000 | [diff] [blame] | 149 | break; |
Preston Gurd | f0b6288 | 2013-09-30 23:18:42 +0000 | [diff] [blame] | 150 | case X86::ADD16rr: |
| 151 | case X86::ADD16rr_DB: |
| 152 | if (MI->getOperand(1).getReg() != MI->getOperand(2).getReg()) { |
| 153 | // if src1 != src2, then convertToThreeAddress will |
| 154 | // need to create a Virtual register, which we cannot do |
| 155 | // after register allocation. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 156 | return nullptr; |
Preston Gurd | f0b6288 | 2013-09-30 23:18:42 +0000 | [diff] [blame] | 157 | } |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 158 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 159 | return TII->convertToThreeAddress(MFI, MBBI, nullptr); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 162 | FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); } |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 163 | |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 164 | bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) { |
Andrew Kaylor | 2bee5ef | 2016-04-26 21:44:24 +0000 | [diff] [blame^] | 165 | if (skipFunction(*Func.getFunction())) |
| 166 | return false; |
| 167 | |
Eric Christopher | dd240fd | 2014-06-03 21:01:39 +0000 | [diff] [blame] | 168 | MF = &Func; |
Eric Christopher | 4369c9b | 2015-02-20 08:01:52 +0000 | [diff] [blame] | 169 | const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>(); |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 170 | OptIncDec = !ST.slowIncDec() || Func.getFunction()->optForMinSize(); |
| 171 | OptLEA = ST.LEAusesAG() || ST.slowLEA(); |
| 172 | |
| 173 | if (!OptLEA && !OptIncDec) |
Eric Christopher | 0d5c99e | 2014-05-22 01:46:02 +0000 | [diff] [blame] | 174 | return false; |
| 175 | |
Eric Christopher | d361ff8 | 2015-02-05 19:27:01 +0000 | [diff] [blame] | 176 | TII = ST.getInstrInfo(); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 177 | |
| 178 | DEBUG(dbgs() << "Start X86FixupLEAs\n";); |
| 179 | // Process all basic blocks. |
| 180 | for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I) |
| 181 | processBasicBlock(Func, I); |
| 182 | DEBUG(dbgs() << "End X86FixupLEAs\n";); |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 187 | FixupLEAPass::RegUsageState |
| 188 | FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) { |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 189 | RegUsageState RegUsage = RU_NotUsed; |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 190 | MachineInstr *MI = I; |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 191 | |
| 192 | for (unsigned int i = 0; i < MI->getNumOperands(); ++i) { |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 193 | MachineOperand &opnd = MI->getOperand(i); |
| 194 | if (opnd.isReg() && opnd.getReg() == p.getReg()) { |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 195 | if (opnd.isDef()) |
| 196 | return RU_Write; |
| 197 | RegUsage = RU_Read; |
| 198 | } |
| 199 | } |
| 200 | return RegUsage; |
| 201 | } |
| 202 | |
| 203 | /// getPreviousInstr - Given a reference to an instruction in a basic |
| 204 | /// block, return a reference to the previous instruction in the block, |
| 205 | /// wrapping around to the last instruction of the block if the block |
| 206 | /// branches to itself. |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 207 | static inline bool getPreviousInstr(MachineBasicBlock::iterator &I, |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 208 | MachineFunction::iterator MFI) { |
| 209 | if (I == MFI->begin()) { |
Duncan P. N. Exon Smith | d77de64 | 2015-10-19 21:48:29 +0000 | [diff] [blame] | 210 | if (MFI->isPredecessor(&*MFI)) { |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 211 | I = --MFI->end(); |
| 212 | return true; |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 213 | } else |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | --I; |
| 217 | return true; |
| 218 | } |
| 219 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 220 | MachineBasicBlock::iterator |
| 221 | FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I, |
| 222 | MachineFunction::iterator MFI) { |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 223 | int InstrDistance = 1; |
| 224 | MachineBasicBlock::iterator CurInst; |
| 225 | static const int INSTR_DISTANCE_THRESHOLD = 5; |
| 226 | |
| 227 | CurInst = I; |
| 228 | bool Found; |
| 229 | Found = getPreviousInstr(CurInst, MFI); |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 230 | while (Found && I != CurInst) { |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 231 | if (CurInst->isCall() || CurInst->isInlineAsm()) |
| 232 | break; |
| 233 | if (InstrDistance > INSTR_DISTANCE_THRESHOLD) |
| 234 | break; // too far back to make a difference |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 235 | if (usesRegister(p, CurInst) == RU_Write) { |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 236 | return CurInst; |
| 237 | } |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 238 | InstrDistance += TII->getInstrLatency( |
Eric Christopher | d361ff8 | 2015-02-05 19:27:01 +0000 | [diff] [blame] | 239 | MF->getSubtarget().getInstrItineraryData(), CurInst); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 240 | Found = getPreviousInstr(CurInst, MFI); |
| 241 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 242 | return nullptr; |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 245 | static inline bool isLEA(const int opcode) { |
| 246 | return opcode == X86::LEA16r || opcode == X86::LEA32r || |
| 247 | opcode == X86::LEA64r || opcode == X86::LEA64_32r; |
| 248 | } |
| 249 | |
| 250 | /// isLEASimpleIncOrDec - Does this LEA have one these forms: |
| 251 | /// lea %reg, 1(%reg) |
| 252 | /// lea %reg, -1(%reg) |
| 253 | static inline bool isLEASimpleIncOrDec(MachineInstr *LEA) { |
| 254 | unsigned SrcReg = LEA->getOperand(1 + X86::AddrBaseReg).getReg(); |
| 255 | unsigned DstReg = LEA->getOperand(0).getReg(); |
| 256 | unsigned AddrDispOp = 1 + X86::AddrDisp; |
| 257 | return SrcReg == DstReg && |
| 258 | LEA->getOperand(1 + X86::AddrIndexReg).getReg() == 0 && |
| 259 | LEA->getOperand(1 + X86::AddrSegmentReg).getReg() == 0 && |
| 260 | LEA->getOperand(AddrDispOp).isImm() && |
| 261 | (LEA->getOperand(AddrDispOp).getImm() == 1 || |
| 262 | LEA->getOperand(AddrDispOp).getImm() == -1); |
| 263 | } |
| 264 | |
| 265 | bool FixupLEAPass::fixupIncDec(MachineBasicBlock::iterator &I, |
| 266 | MachineFunction::iterator MFI) const { |
| 267 | MachineInstr *MI = I; |
| 268 | int Opcode = MI->getOpcode(); |
| 269 | if (!isLEA(Opcode)) |
| 270 | return false; |
| 271 | |
| 272 | if (isLEASimpleIncOrDec(MI) && TII->isSafeToClobberEFLAGS(*MFI, I)) { |
| 273 | int NewOpcode; |
| 274 | bool isINC = MI->getOperand(4).getImm() == 1; |
| 275 | switch (Opcode) { |
| 276 | case X86::LEA16r: |
| 277 | NewOpcode = isINC ? X86::INC16r : X86::DEC16r; |
| 278 | break; |
| 279 | case X86::LEA32r: |
| 280 | case X86::LEA64_32r: |
| 281 | NewOpcode = isINC ? X86::INC32r : X86::DEC32r; |
| 282 | break; |
| 283 | case X86::LEA64r: |
| 284 | NewOpcode = isINC ? X86::INC64r : X86::DEC64r; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | MachineInstr *NewMI = |
| 289 | BuildMI(*MFI, I, MI->getDebugLoc(), TII->get(NewOpcode)) |
| 290 | .addOperand(MI->getOperand(0)) |
| 291 | .addOperand(MI->getOperand(1)); |
| 292 | MFI->erase(I); |
| 293 | I = static_cast<MachineBasicBlock::iterator>(NewMI); |
| 294 | return true; |
| 295 | } |
| 296 | return false; |
| 297 | } |
| 298 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 299 | void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I, |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 300 | MachineFunction::iterator MFI) { |
| 301 | // Process a load, store, or LEA instruction. |
| 302 | MachineInstr *MI = I; |
| 303 | int opcode = MI->getOpcode(); |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 304 | const MCInstrDesc &Desc = MI->getDesc(); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 305 | int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags, opcode); |
| 306 | if (AddrOffset >= 0) { |
| 307 | AddrOffset += X86II::getOperandBias(Desc); |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 308 | MachineOperand &p = MI->getOperand(AddrOffset + X86::AddrBaseReg); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 309 | if (p.isReg() && p.getReg() != X86::ESP) { |
| 310 | seekLEAFixup(p, I, MFI); |
| 311 | } |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 312 | MachineOperand &q = MI->getOperand(AddrOffset + X86::AddrIndexReg); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 313 | if (q.isReg() && q.getReg() != X86::ESP) { |
| 314 | seekLEAFixup(q, I, MFI); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 319 | void FixupLEAPass::seekLEAFixup(MachineOperand &p, |
| 320 | MachineBasicBlock::iterator &I, |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 321 | MachineFunction::iterator MFI) { |
| 322 | MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI); |
| 323 | if (MBI) { |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 324 | MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 325 | if (NewMI) { |
| 326 | ++NumLEAs; |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 327 | DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump();); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 328 | // now to replace with an equivalent LEA... |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 329 | DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump();); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 330 | MFI->erase(MBI); |
| 331 | MachineBasicBlock::iterator J = |
Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 332 | static_cast<MachineBasicBlock::iterator>(NewMI); |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 333 | processInstruction(J, MFI); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 338 | void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I, |
| 339 | MachineFunction::iterator MFI) { |
| 340 | MachineInstr *MI = I; |
| 341 | const int opcode = MI->getOpcode(); |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 342 | if (!isLEA(opcode)) |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 343 | return; |
| 344 | if (MI->getOperand(5).getReg() != 0 || !MI->getOperand(4).isImm() || |
| 345 | !TII->isSafeToClobberEFLAGS(*MFI, I)) |
| 346 | return; |
| 347 | const unsigned DstR = MI->getOperand(0).getReg(); |
| 348 | const unsigned SrcR1 = MI->getOperand(1).getReg(); |
| 349 | const unsigned SrcR2 = MI->getOperand(3).getReg(); |
| 350 | if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR)) |
| 351 | return; |
| 352 | if (MI->getOperand(2).getImm() > 1) |
| 353 | return; |
| 354 | int addrr_opcode, addri_opcode; |
| 355 | switch (opcode) { |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 356 | default: |
| 357 | llvm_unreachable("Unexpected LEA instruction"); |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 358 | case X86::LEA16r: |
| 359 | addrr_opcode = X86::ADD16rr; |
| 360 | addri_opcode = X86::ADD16ri; |
| 361 | break; |
| 362 | case X86::LEA32r: |
| 363 | addrr_opcode = X86::ADD32rr; |
| 364 | addri_opcode = X86::ADD32ri; |
| 365 | break; |
| 366 | case X86::LEA64_32r: |
| 367 | case X86::LEA64r: |
| 368 | addrr_opcode = X86::ADD64rr; |
| 369 | addri_opcode = X86::ADD64ri32; |
| 370 | break; |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 371 | } |
| 372 | DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump();); |
| 373 | DEBUG(dbgs() << "FixLEA: Replaced by: ";); |
Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 374 | MachineInstr *NewMI = nullptr; |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 375 | const MachineOperand &Dst = MI->getOperand(0); |
| 376 | // Make ADD instruction for two registers writing to LEA's destination |
| 377 | if (SrcR1 != 0 && SrcR2 != 0) { |
| 378 | const MachineOperand &Src1 = MI->getOperand(SrcR1 == DstR ? 1 : 3); |
| 379 | const MachineOperand &Src2 = MI->getOperand(SrcR1 == DstR ? 3 : 1); |
| 380 | NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addrr_opcode)) |
| 381 | .addOperand(Dst) |
| 382 | .addOperand(Src1) |
| 383 | .addOperand(Src2); |
| 384 | MFI->insert(I, NewMI); |
| 385 | DEBUG(NewMI->dump();); |
| 386 | } |
| 387 | // Make ADD instruction for immediate |
| 388 | if (MI->getOperand(4).getImm() != 0) { |
| 389 | const MachineOperand &SrcR = MI->getOperand(SrcR1 == DstR ? 1 : 3); |
| 390 | NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addri_opcode)) |
| 391 | .addOperand(Dst) |
| 392 | .addOperand(SrcR) |
| 393 | .addImm(MI->getOperand(4).getImm()); |
| 394 | MFI->insert(I, NewMI); |
| 395 | DEBUG(NewMI->dump();); |
| 396 | } |
| 397 | if (NewMI) { |
| 398 | MFI->erase(I); |
| 399 | I = static_cast<MachineBasicBlock::iterator>(NewMI); |
| 400 | } |
| 401 | } |
| 402 | |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 403 | bool FixupLEAPass::processBasicBlock(MachineFunction &MF, |
| 404 | MachineFunction::iterator MFI) { |
| 405 | |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 406 | for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) { |
Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 407 | if (OptIncDec) |
| 408 | if (fixupIncDec(I, MFI)) |
| 409 | continue; |
| 410 | |
| 411 | if (OptLEA) { |
| 412 | if (MF.getSubtarget<X86Subtarget>().isSLM()) |
| 413 | processInstructionForSLM(I, MFI); |
| 414 | else |
| 415 | processInstruction(I, MFI); |
| 416 | } |
Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 417 | } |
Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 418 | return false; |
| 419 | } |