| 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" |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/Passes.h" |
| Simon Pilgrim | a3a9d812 | 2018-04-13 15:09:39 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/TargetSchedule.h" |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 28 | #define FIXUPLEA_DESC "X86 LEA Fixup" |
| 29 | #define FIXUPLEA_NAME "x86-fixup-LEAs" |
| 30 | |
| 31 | #define DEBUG_TYPE FIXUPLEA_NAME |
| Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 32 | |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 33 | STATISTIC(NumLEAs, "Number of LEA instructions created"); |
| 34 | |
| 35 | namespace { |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 36 | class FixupLEAPass : public MachineFunctionPass { |
| 37 | enum RegUsageState { RU_NotUsed, RU_Write, RU_Read }; |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 38 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 39 | /// Loop over all of the instructions in the basic block |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 40 | /// replacing applicable instructions with LEA instructions, |
| 41 | /// where appropriate. |
| 42 | bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 43 | |
| Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 44 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 45 | /// Given a machine register, look for the instruction |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 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 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 53 | /// Given a memory access or LEA instruction |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 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 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 60 | /// Given a LEA instruction which is unprofitable |
| Simon Pilgrim | d5d7224 | 2018-11-01 14:57:07 +0000 | [diff] [blame^] | 61 | /// on SlowLEA targets try to replace it with an equivalent ADD instruction. |
| 62 | void processInstructionForSlowLEA(MachineBasicBlock::iterator &I, |
| 63 | MachineFunction::iterator MFI); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 64 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 65 | /// Given a LEA instruction which is unprofitable |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 66 | /// on SNB+ try to replace it with other instructions. |
| 67 | /// According to Intel's Optimization Reference Manual: |
| 68 | /// " For LEA instructions with three source operands and some specific |
| 69 | /// situations, instruction latency has increased to 3 cycles, and must |
| 70 | /// dispatch via port 1: |
| 71 | /// - LEA that has all three source operands: base, index, and offset |
| 72 | /// - LEA that uses base and index registers where the base is EBP, RBP, |
| 73 | /// or R13 |
| 74 | /// - LEA that uses RIP relative addressing mode |
| 75 | /// - LEA that uses 16-bit addressing mode " |
| 76 | /// This function currently handles the first 2 cases only. |
| 77 | MachineInstr *processInstrForSlow3OpLEA(MachineInstr &MI, |
| 78 | MachineFunction::iterator MFI); |
| 79 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 80 | /// Look for LEAs that add 1 to reg or subtract 1 from reg |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 81 | /// and convert them to INC or DEC respectively. |
| 82 | bool fixupIncDec(MachineBasicBlock::iterator &I, |
| 83 | MachineFunction::iterator MFI) const; |
| 84 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 85 | /// Determine if an instruction references a machine register |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 86 | /// and, if so, whether it reads or writes the register. |
| 87 | RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I); |
| Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 88 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 89 | /// Step backwards through a basic block, looking |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 90 | /// for an instruction which writes a register within |
| 91 | /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles. |
| 92 | MachineBasicBlock::iterator searchBackwards(MachineOperand &p, |
| 93 | MachineBasicBlock::iterator &I, |
| 94 | MachineFunction::iterator MFI); |
| Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 95 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 96 | /// if an instruction can be converted to an |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 97 | /// equivalent LEA, insert the new instruction into the basic block |
| 98 | /// and return a pointer to it. Otherwise, return zero. |
| 99 | MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI, |
| 100 | MachineBasicBlock::iterator &MBBI) const; |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 101 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 102 | public: |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 103 | static char ID; |
| 104 | |
| 105 | StringRef getPassName() const override { return FIXUPLEA_DESC; } |
| 106 | |
| 107 | FixupLEAPass() : MachineFunctionPass(ID) { |
| 108 | initializeFixupLEAPassPass(*PassRegistry::getPassRegistry()); |
| 109 | } |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 110 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 111 | /// Loop over all of the basic blocks, |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 112 | /// replacing instructions by equivalent LEA instructions |
| 113 | /// if needed and when possible. |
| 114 | bool runOnMachineFunction(MachineFunction &MF) override; |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 115 | |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 116 | // This pass runs after regalloc and doesn't support VReg operands. |
| 117 | MachineFunctionProperties getRequiredProperties() const override { |
| 118 | return MachineFunctionProperties().set( |
| Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 119 | MachineFunctionProperties::Property::NoVRegs); |
| Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 122 | private: |
| Simon Pilgrim | a3a9d812 | 2018-04-13 15:09:39 +0000 | [diff] [blame] | 123 | TargetSchedModel TSM; |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 124 | MachineFunction *MF; |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 125 | const X86InstrInfo *TII; // Machine instruction info. |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 126 | bool OptIncDec; |
| 127 | bool OptLEA; |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 128 | }; |
| Reid Kleckner | 0ad69fc | 2017-05-16 19:55:03 +0000 | [diff] [blame] | 129 | } |
| Lama Saba | 52e8925 | 2017-05-16 16:01:36 +0000 | [diff] [blame] | 130 | |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 131 | char FixupLEAPass::ID = 0; |
| 132 | |
| 133 | INITIALIZE_PASS(FixupLEAPass, FIXUPLEA_NAME, FIXUPLEA_DESC, false, false) |
| 134 | |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 135 | MachineInstr * |
| 136 | FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI, |
| Preston Gurd | 128920d | 2013-04-25 21:31:33 +0000 | [diff] [blame] | 137 | MachineBasicBlock::iterator &MBBI) const { |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 138 | MachineInstr &MI = *MBBI; |
| 139 | switch (MI.getOpcode()) { |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 140 | case X86::MOV32rr: |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 141 | case X86::MOV64rr: { |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 142 | const MachineOperand &Src = MI.getOperand(1); |
| 143 | const MachineOperand &Dest = MI.getOperand(0); |
| 144 | MachineInstr *NewMI = |
| 145 | BuildMI(*MF, MI.getDebugLoc(), |
| 146 | TII->get(MI.getOpcode() == X86::MOV32rr ? X86::LEA32r |
| 147 | : X86::LEA64r)) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 148 | .add(Dest) |
| 149 | .add(Src) |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 150 | .addImm(1) |
| 151 | .addReg(0) |
| 152 | .addImm(0) |
| 153 | .addReg(0); |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 154 | MFI->insert(MBBI, NewMI); // Insert the new inst |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 155 | return NewMI; |
| 156 | } |
| 157 | case X86::ADD64ri32: |
| 158 | case X86::ADD64ri8: |
| 159 | case X86::ADD64ri32_DB: |
| 160 | case X86::ADD64ri8_DB: |
| 161 | case X86::ADD32ri: |
| 162 | case X86::ADD32ri8: |
| 163 | case X86::ADD32ri_DB: |
| 164 | case X86::ADD32ri8_DB: |
| 165 | case X86::ADD16ri: |
| 166 | case X86::ADD16ri8: |
| 167 | case X86::ADD16ri_DB: |
| 168 | case X86::ADD16ri8_DB: |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 169 | if (!MI.getOperand(2).isImm()) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 170 | // convertToThreeAddress will call getImm() |
| 171 | // which requires isImm() to be true |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 172 | return nullptr; |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 173 | } |
| Preston Gurd | f03a6e7 | 2013-09-30 23:51:22 +0000 | [diff] [blame] | 174 | break; |
| Preston Gurd | f0b6288 | 2013-09-30 23:18:42 +0000 | [diff] [blame] | 175 | case X86::ADD16rr: |
| 176 | case X86::ADD16rr_DB: |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 177 | if (MI.getOperand(1).getReg() != MI.getOperand(2).getReg()) { |
| Preston Gurd | f0b6288 | 2013-09-30 23:18:42 +0000 | [diff] [blame] | 178 | // if src1 != src2, then convertToThreeAddress will |
| 179 | // need to create a Virtual register, which we cannot do |
| 180 | // after register allocation. |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 181 | return nullptr; |
| Preston Gurd | f0b6288 | 2013-09-30 23:18:42 +0000 | [diff] [blame] | 182 | } |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 183 | } |
| Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 184 | return TII->convertToThreeAddress(MFI, MI, nullptr); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 187 | FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); } |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 188 | |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 189 | bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 190 | if (skipFunction(Func.getFunction())) |
| Andrew Kaylor | 2bee5ef | 2016-04-26 21:44:24 +0000 | [diff] [blame] | 191 | return false; |
| 192 | |
| Eric Christopher | dd240fd | 2014-06-03 21:01:39 +0000 | [diff] [blame] | 193 | MF = &Func; |
| Eric Christopher | 4369c9b | 2015-02-20 08:01:52 +0000 | [diff] [blame] | 194 | const X86Subtarget &ST = Func.getSubtarget<X86Subtarget>(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 195 | OptIncDec = !ST.slowIncDec() || Func.getFunction().optForMinSize(); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 196 | OptLEA = ST.LEAusesAG() || ST.slowLEA() || ST.slow3OpsLEA(); |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 197 | |
| 198 | if (!OptLEA && !OptIncDec) |
| Eric Christopher | 0d5c99e | 2014-05-22 01:46:02 +0000 | [diff] [blame] | 199 | return false; |
| 200 | |
| Simon Pilgrim | a3a9d812 | 2018-04-13 15:09:39 +0000 | [diff] [blame] | 201 | TSM.init(&Func.getSubtarget()); |
| Eric Christopher | d361ff8 | 2015-02-05 19:27:01 +0000 | [diff] [blame] | 202 | TII = ST.getInstrInfo(); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 203 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 204 | LLVM_DEBUG(dbgs() << "Start X86FixupLEAs\n";); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 205 | // Process all basic blocks. |
| 206 | for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I) |
| 207 | processBasicBlock(Func, I); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 208 | LLVM_DEBUG(dbgs() << "End X86FixupLEAs\n";); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 213 | FixupLEAPass::RegUsageState |
| 214 | FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 215 | RegUsageState RegUsage = RU_NotUsed; |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 216 | MachineInstr &MI = *I; |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 217 | |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 218 | for (unsigned int i = 0; i < MI.getNumOperands(); ++i) { |
| 219 | MachineOperand &opnd = MI.getOperand(i); |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 220 | if (opnd.isReg() && opnd.getReg() == p.getReg()) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 221 | if (opnd.isDef()) |
| 222 | return RU_Write; |
| 223 | RegUsage = RU_Read; |
| 224 | } |
| 225 | } |
| 226 | return RegUsage; |
| 227 | } |
| 228 | |
| 229 | /// getPreviousInstr - Given a reference to an instruction in a basic |
| 230 | /// block, return a reference to the previous instruction in the block, |
| 231 | /// wrapping around to the last instruction of the block if the block |
| 232 | /// branches to itself. |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 233 | static inline bool getPreviousInstr(MachineBasicBlock::iterator &I, |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 234 | MachineFunction::iterator MFI) { |
| 235 | if (I == MFI->begin()) { |
| Duncan P. N. Exon Smith | d77de64 | 2015-10-19 21:48:29 +0000 | [diff] [blame] | 236 | if (MFI->isPredecessor(&*MFI)) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 237 | I = --MFI->end(); |
| 238 | return true; |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 239 | } else |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | --I; |
| 243 | return true; |
| 244 | } |
| 245 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 246 | MachineBasicBlock::iterator |
| 247 | FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I, |
| 248 | MachineFunction::iterator MFI) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 249 | int InstrDistance = 1; |
| 250 | MachineBasicBlock::iterator CurInst; |
| 251 | static const int INSTR_DISTANCE_THRESHOLD = 5; |
| 252 | |
| 253 | CurInst = I; |
| 254 | bool Found; |
| 255 | Found = getPreviousInstr(CurInst, MFI); |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 256 | while (Found && I != CurInst) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 257 | if (CurInst->isCall() || CurInst->isInlineAsm()) |
| 258 | break; |
| 259 | if (InstrDistance > INSTR_DISTANCE_THRESHOLD) |
| 260 | break; // too far back to make a difference |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 261 | if (usesRegister(p, CurInst) == RU_Write) { |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 262 | return CurInst; |
| 263 | } |
| Simon Pilgrim | a3a9d812 | 2018-04-13 15:09:39 +0000 | [diff] [blame] | 264 | InstrDistance += TSM.computeInstrLatency(&*CurInst); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 265 | Found = getPreviousInstr(CurInst, MFI); |
| 266 | } |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 267 | return MachineBasicBlock::iterator(); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 270 | static inline bool isLEA(const int Opcode) { |
| 271 | return Opcode == X86::LEA16r || Opcode == X86::LEA32r || |
| 272 | Opcode == X86::LEA64r || Opcode == X86::LEA64_32r; |
| 273 | } |
| 274 | |
| 275 | static inline bool isInefficientLEAReg(unsigned int Reg) { |
| Craig Topper | b2cc9a1 | 2018-08-03 03:45:19 +0000 | [diff] [blame] | 276 | return Reg == X86::EBP || Reg == X86::RBP || |
| 277 | Reg == X86::R13D || Reg == X86::R13; |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static inline bool isRegOperand(const MachineOperand &Op) { |
| 281 | return Op.isReg() && Op.getReg() != X86::NoRegister; |
| 282 | } |
| 283 | /// hasIneffecientLEARegs - LEA that uses base and index registers |
| 284 | /// where the base is EBP, RBP, or R13 |
| Andrea Di Biagio | b6022aa | 2018-07-19 16:42:15 +0000 | [diff] [blame] | 285 | // TODO: use a variant scheduling class to model the latency profile |
| 286 | // of LEA instructions, and implement this logic as a scheduling predicate. |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 287 | static inline bool hasInefficientLEABaseReg(const MachineOperand &Base, |
| 288 | const MachineOperand &Index) { |
| 289 | return Base.isReg() && isInefficientLEAReg(Base.getReg()) && |
| 290 | isRegOperand(Index); |
| 291 | } |
| 292 | |
| 293 | static inline bool hasLEAOffset(const MachineOperand &Offset) { |
| 294 | return (Offset.isImm() && Offset.getImm() != 0) || Offset.isGlobal(); |
| 295 | } |
| 296 | |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 297 | static inline int getADDrrFromLEA(int LEAOpcode) { |
| 298 | switch (LEAOpcode) { |
| 299 | default: |
| 300 | llvm_unreachable("Unexpected LEA instruction"); |
| 301 | case X86::LEA16r: |
| 302 | return X86::ADD16rr; |
| 303 | case X86::LEA32r: |
| 304 | return X86::ADD32rr; |
| 305 | case X86::LEA64_32r: |
| 306 | case X86::LEA64r: |
| 307 | return X86::ADD64rr; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | static inline int getADDriFromLEA(int LEAOpcode, const MachineOperand &Offset) { |
| 312 | bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm()); |
| 313 | switch (LEAOpcode) { |
| 314 | default: |
| 315 | llvm_unreachable("Unexpected LEA instruction"); |
| 316 | case X86::LEA16r: |
| 317 | return IsInt8 ? X86::ADD16ri8 : X86::ADD16ri; |
| 318 | case X86::LEA32r: |
| 319 | case X86::LEA64_32r: |
| 320 | return IsInt8 ? X86::ADD32ri8 : X86::ADD32ri; |
| 321 | case X86::LEA64r: |
| 322 | return IsInt8 ? X86::ADD64ri8 : X86::ADD64ri32; |
| 323 | } |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// isLEASimpleIncOrDec - Does this LEA have one these forms: |
| 327 | /// lea %reg, 1(%reg) |
| 328 | /// lea %reg, -1(%reg) |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 329 | static inline bool isLEASimpleIncOrDec(MachineInstr &LEA) { |
| 330 | unsigned SrcReg = LEA.getOperand(1 + X86::AddrBaseReg).getReg(); |
| 331 | unsigned DstReg = LEA.getOperand(0).getReg(); |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 332 | unsigned AddrDispOp = 1 + X86::AddrDisp; |
| 333 | return SrcReg == DstReg && |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 334 | LEA.getOperand(1 + X86::AddrIndexReg).getReg() == 0 && |
| 335 | LEA.getOperand(1 + X86::AddrSegmentReg).getReg() == 0 && |
| 336 | LEA.getOperand(AddrDispOp).isImm() && |
| 337 | (LEA.getOperand(AddrDispOp).getImm() == 1 || |
| 338 | LEA.getOperand(AddrDispOp).getImm() == -1); |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | bool FixupLEAPass::fixupIncDec(MachineBasicBlock::iterator &I, |
| 342 | MachineFunction::iterator MFI) const { |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 343 | MachineInstr &MI = *I; |
| 344 | int Opcode = MI.getOpcode(); |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 345 | if (!isLEA(Opcode)) |
| 346 | return false; |
| 347 | |
| 348 | if (isLEASimpleIncOrDec(MI) && TII->isSafeToClobberEFLAGS(*MFI, I)) { |
| 349 | int NewOpcode; |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 350 | bool isINC = MI.getOperand(4).getImm() == 1; |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 351 | switch (Opcode) { |
| 352 | case X86::LEA16r: |
| 353 | NewOpcode = isINC ? X86::INC16r : X86::DEC16r; |
| 354 | break; |
| 355 | case X86::LEA32r: |
| 356 | case X86::LEA64_32r: |
| 357 | NewOpcode = isINC ? X86::INC32r : X86::DEC32r; |
| 358 | break; |
| 359 | case X86::LEA64r: |
| 360 | NewOpcode = isINC ? X86::INC64r : X86::DEC64r; |
| 361 | break; |
| 362 | } |
| 363 | |
| 364 | MachineInstr *NewMI = |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 365 | BuildMI(*MFI, I, MI.getDebugLoc(), TII->get(NewOpcode)) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 366 | .add(MI.getOperand(0)) |
| 367 | .add(MI.getOperand(1)); |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 368 | MFI->erase(I); |
| 369 | I = static_cast<MachineBasicBlock::iterator>(NewMI); |
| 370 | return true; |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 375 | void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I, |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 376 | MachineFunction::iterator MFI) { |
| 377 | // Process a load, store, or LEA instruction. |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 378 | MachineInstr &MI = *I; |
| 379 | const MCInstrDesc &Desc = MI.getDesc(); |
| Craig Topper | 477649a | 2016-04-28 05:58:46 +0000 | [diff] [blame] | 380 | int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 381 | if (AddrOffset >= 0) { |
| 382 | AddrOffset += X86II::getOperandBias(Desc); |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 383 | MachineOperand &p = MI.getOperand(AddrOffset + X86::AddrBaseReg); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 384 | if (p.isReg() && p.getReg() != X86::ESP) { |
| 385 | seekLEAFixup(p, I, MFI); |
| 386 | } |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 387 | MachineOperand &q = MI.getOperand(AddrOffset + X86::AddrIndexReg); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 388 | if (q.isReg() && q.getReg() != X86::ESP) { |
| 389 | seekLEAFixup(q, I, MFI); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 394 | void FixupLEAPass::seekLEAFixup(MachineOperand &p, |
| 395 | MachineBasicBlock::iterator &I, |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 396 | MachineFunction::iterator MFI) { |
| 397 | MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI); |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 398 | if (MBI != MachineBasicBlock::iterator()) { |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 399 | MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 400 | if (NewMI) { |
| 401 | ++NumLEAs; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 402 | LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump();); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 403 | // now to replace with an equivalent LEA... |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 404 | LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump();); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 405 | MFI->erase(MBI); |
| 406 | MachineBasicBlock::iterator J = |
| Eric Christopher | 31b81ce | 2014-06-03 21:01:35 +0000 | [diff] [blame] | 407 | static_cast<MachineBasicBlock::iterator>(NewMI); |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 408 | processInstruction(J, MFI); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| Simon Pilgrim | d5d7224 | 2018-11-01 14:57:07 +0000 | [diff] [blame^] | 413 | void FixupLEAPass::processInstructionForSlowLEA(MachineBasicBlock::iterator &I, |
| 414 | MachineFunction::iterator MFI) { |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 415 | MachineInstr &MI = *I; |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 416 | const int Opcode = MI.getOpcode(); |
| 417 | if (!isLEA(Opcode)) |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 418 | return; |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 419 | if (MI.getOperand(5).getReg() != 0 || !MI.getOperand(4).isImm() || |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 420 | !TII->isSafeToClobberEFLAGS(*MFI, I)) |
| 421 | return; |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 422 | const unsigned DstR = MI.getOperand(0).getReg(); |
| 423 | const unsigned SrcR1 = MI.getOperand(1).getReg(); |
| 424 | const unsigned SrcR2 = MI.getOperand(3).getReg(); |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 425 | if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR)) |
| 426 | return; |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 427 | if (MI.getOperand(2).getImm() > 1) |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 428 | return; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 429 | LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump();); |
| 430 | LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";); |
| Craig Topper | 66f09ad | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 431 | MachineInstr *NewMI = nullptr; |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 432 | // Make ADD instruction for two registers writing to LEA's destination |
| 433 | if (SrcR1 != 0 && SrcR2 != 0) { |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 434 | const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(Opcode)); |
| 435 | const MachineOperand &Src = MI.getOperand(SrcR1 == DstR ? 3 : 1); |
| 436 | NewMI = |
| 437 | BuildMI(*MFI, I, MI.getDebugLoc(), ADDrr, DstR).addReg(DstR).add(Src); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 438 | LLVM_DEBUG(NewMI->dump();); |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 439 | } |
| 440 | // Make ADD instruction for immediate |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 441 | if (MI.getOperand(4).getImm() != 0) { |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 442 | const MCInstrDesc &ADDri = |
| 443 | TII->get(getADDriFromLEA(Opcode, MI.getOperand(4))); |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 444 | const MachineOperand &SrcR = MI.getOperand(SrcR1 == DstR ? 1 : 3); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 445 | NewMI = BuildMI(*MFI, I, MI.getDebugLoc(), ADDri, DstR) |
| Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 446 | .add(SrcR) |
| Duncan P. N. Exon Smith | 7b4c18e | 2016-07-12 03:18:50 +0000 | [diff] [blame] | 447 | .addImm(MI.getOperand(4).getImm()); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 448 | LLVM_DEBUG(NewMI->dump();); |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 449 | } |
| 450 | if (NewMI) { |
| 451 | MFI->erase(I); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 452 | I = NewMI; |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 456 | MachineInstr * |
| 457 | FixupLEAPass::processInstrForSlow3OpLEA(MachineInstr &MI, |
| 458 | MachineFunction::iterator MFI) { |
| 459 | |
| 460 | const int LEAOpcode = MI.getOpcode(); |
| 461 | if (!isLEA(LEAOpcode)) |
| 462 | return nullptr; |
| 463 | |
| 464 | const MachineOperand &Dst = MI.getOperand(0); |
| 465 | const MachineOperand &Base = MI.getOperand(1); |
| 466 | const MachineOperand &Scale = MI.getOperand(2); |
| 467 | const MachineOperand &Index = MI.getOperand(3); |
| 468 | const MachineOperand &Offset = MI.getOperand(4); |
| 469 | const MachineOperand &Segment = MI.getOperand(5); |
| 470 | |
| Andrea Di Biagio | b6022aa | 2018-07-19 16:42:15 +0000 | [diff] [blame] | 471 | if (!(TII->isThreeOperandsLEA(MI) || |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 472 | hasInefficientLEABaseReg(Base, Index)) || |
| 473 | !TII->isSafeToClobberEFLAGS(*MFI, MI) || |
| 474 | Segment.getReg() != X86::NoRegister) |
| 475 | return nullptr; |
| 476 | |
| 477 | unsigned int DstR = Dst.getReg(); |
| 478 | unsigned int BaseR = Base.getReg(); |
| 479 | unsigned int IndexR = Index.getReg(); |
| 480 | unsigned SSDstR = |
| 481 | (LEAOpcode == X86::LEA64_32r) ? getX86SubSuperRegister(DstR, 64) : DstR; |
| 482 | bool IsScale1 = Scale.getImm() == 1; |
| 483 | bool IsInefficientBase = isInefficientLEAReg(BaseR); |
| 484 | bool IsInefficientIndex = isInefficientLEAReg(IndexR); |
| 485 | |
| 486 | // Skip these cases since it takes more than 2 instructions |
| 487 | // to replace the LEA instruction. |
| 488 | if (IsInefficientBase && SSDstR == BaseR && !IsScale1) |
| 489 | return nullptr; |
| 490 | if (LEAOpcode == X86::LEA64_32r && IsInefficientBase && |
| 491 | (IsInefficientIndex || !IsScale1)) |
| 492 | return nullptr; |
| 493 | |
| 494 | const DebugLoc DL = MI.getDebugLoc(); |
| 495 | const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(LEAOpcode)); |
| 496 | const MCInstrDesc &ADDri = TII->get(getADDriFromLEA(LEAOpcode, Offset)); |
| 497 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 498 | LLVM_DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MI.dump();); |
| 499 | LLVM_DEBUG(dbgs() << "FixLEA: Replaced by: ";); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 500 | |
| 501 | // First try to replace LEA with one or two (for the 3-op LEA case) |
| 502 | // add instructions: |
| 503 | // 1.lea (%base,%index,1), %base => add %index,%base |
| 504 | // 2.lea (%base,%index,1), %index => add %base,%index |
| 505 | if (IsScale1 && (DstR == BaseR || DstR == IndexR)) { |
| 506 | const MachineOperand &Src = DstR == BaseR ? Index : Base; |
| 507 | MachineInstr *NewMI = |
| 508 | BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Src); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 509 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 510 | // Create ADD instruction for the Offset in case of 3-Ops LEA. |
| 511 | if (hasLEAOffset(Offset)) { |
| 512 | NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 513 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 514 | } |
| 515 | return NewMI; |
| 516 | } |
| 517 | // If the base is inefficient try switching the index and base operands, |
| 518 | // otherwise just break the 3-Ops LEA inst into 2-Ops LEA + ADD instruction: |
| 519 | // lea offset(%base,%index,scale),%dst => |
| 520 | // lea (%base,%index,scale); add offset,%dst |
| 521 | if (!IsInefficientBase || (!IsInefficientIndex && IsScale1)) { |
| 522 | MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode)) |
| 523 | .add(Dst) |
| 524 | .add(IsInefficientBase ? Index : Base) |
| 525 | .add(Scale) |
| 526 | .add(IsInefficientBase ? Base : Index) |
| 527 | .addImm(0) |
| 528 | .add(Segment); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 529 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 530 | // Create ADD instruction for the Offset in case of 3-Ops LEA. |
| 531 | if (hasLEAOffset(Offset)) { |
| 532 | NewMI = BuildMI(*MFI, MI, DL, ADDri, DstR).addReg(DstR).add(Offset); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 533 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 534 | } |
| 535 | return NewMI; |
| 536 | } |
| 537 | // Handle the rest of the cases with inefficient base register: |
| 538 | assert(SSDstR != BaseR && "SSDstR == BaseR should be handled already!"); |
| 539 | assert(IsInefficientBase && "efficient base should be handled already!"); |
| 540 | |
| 541 | // lea (%base,%index,1), %dst => mov %base,%dst; add %index,%dst |
| 542 | if (IsScale1 && !hasLEAOffset(Offset)) { |
| Serguei Katkov | 1ce7137 | 2018-01-26 04:49:26 +0000 | [diff] [blame] | 543 | bool BIK = Base.isKill() && BaseR != IndexR; |
| 544 | TII->copyPhysReg(*MFI, MI, DL, DstR, BaseR, BIK); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 545 | LLVM_DEBUG(MI.getPrevNode()->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 546 | |
| 547 | MachineInstr *NewMI = |
| 548 | BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Index); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 549 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 550 | return NewMI; |
| 551 | } |
| 552 | // lea offset(%base,%index,scale), %dst => |
| 553 | // lea offset( ,%index,scale), %dst; add %base,%dst |
| 554 | MachineInstr *NewMI = BuildMI(*MFI, MI, DL, TII->get(LEAOpcode)) |
| 555 | .add(Dst) |
| 556 | .addReg(0) |
| 557 | .add(Scale) |
| 558 | .add(Index) |
| 559 | .add(Offset) |
| 560 | .add(Segment); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 561 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 562 | |
| 563 | NewMI = BuildMI(*MFI, MI, DL, ADDrr, DstR).addReg(DstR).add(Base); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 564 | LLVM_DEBUG(NewMI->dump();); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 565 | return NewMI; |
| 566 | } |
| 567 | |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 568 | bool FixupLEAPass::processBasicBlock(MachineFunction &MF, |
| 569 | MachineFunction::iterator MFI) { |
| 570 | |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 571 | for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) { |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 572 | if (OptIncDec) |
| 573 | if (fixupIncDec(I, MFI)) |
| 574 | continue; |
| 575 | |
| 576 | if (OptLEA) { |
| Craig Topper | 9164b9b | 2018-07-31 00:43:54 +0000 | [diff] [blame] | 577 | if (MF.getSubtarget<X86Subtarget>().slowLEA()) |
| Simon Pilgrim | d5d7224 | 2018-11-01 14:57:07 +0000 | [diff] [blame^] | 578 | processInstructionForSlowLEA(I, MFI); |
| Lama Saba | 2ea271b | 2017-05-18 08:11:50 +0000 | [diff] [blame] | 579 | |
| 580 | else { |
| 581 | if (MF.getSubtarget<X86Subtarget>().slow3OpsLEA()) { |
| 582 | if (auto *NewMI = processInstrForSlow3OpLEA(*I, MFI)) { |
| 583 | MFI->erase(I); |
| 584 | I = NewMI; |
| 585 | } |
| 586 | } else |
| 587 | processInstruction(I, MFI); |
| 588 | } |
| Michael Kuperstein | 12982a8 | 2015-11-11 11:44:31 +0000 | [diff] [blame] | 589 | } |
| Alexey Volkov | 6226de6 | 2014-05-20 08:55:50 +0000 | [diff] [blame] | 590 | } |
| Preston Gurd | 8b7ab4b | 2013-04-25 20:29:37 +0000 | [diff] [blame] | 591 | return false; |
| 592 | } |