1d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//===-- X86FixupLEAs.cpp - use or replace LEA instructions -----------===//
2d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//
3d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//                     The LLVM Compiler Infrastructure
4d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//
5d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd// This file is distributed under the University of Illinois Open Source
6d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd// License. See LICENSE.TXT for details.
7d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//
8d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//===----------------------------------------------------------------------===//
9d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//
10d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd// This file defines the pass which will find  instructions  which
11d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd// can be re-written as LEA instructions in order to reduce pipeline
12d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd// delays for some models of the Intel Atom family.
13d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//
14d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd//===----------------------------------------------------------------------===//
15d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
16d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "X86.h"
17d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "X86InstrInfo.h"
18d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "X86Subtarget.h"
19d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/ADT/Statistic.h"
20d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/CodeGen/LiveVariables.h"
21d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/CodeGen/MachineFunctionPass.h"
22d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/CodeGen/MachineInstrBuilder.h"
23d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/CodeGen/MachineRegisterInfo.h"
24d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/CodeGen/Passes.h"
25d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/Support/Debug.h"
26d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/Support/raw_ostream.h"
27d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd#include "llvm/Target/TargetInstrInfo.h"
28d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurdusing namespace llvm;
29d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
30dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "x86-fixup-LEAs"
31dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
32d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston GurdSTATISTIC(NumLEAs, "Number of LEA instructions created");
33d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
34d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurdnamespace {
35cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesclass FixupLEAPass : public MachineFunctionPass {
36cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
37cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  static char ID;
38cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Loop over all of the instructions in the basic block
39cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// replacing applicable instructions with LEA instructions,
40cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// where appropriate.
41cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI);
42d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
43cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  const char *getPassName() const override { return "X86 Atom LEA Fixup"; }
447f8300b8f3f35167ce78628bc14d50beec203b8fPreston Gurd
45cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Given a machine register, look for the instruction
46cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// which writes it in the current basic block. If found,
47cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// try to replace it with an equivalent LEA instruction.
48cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// If replacement succeeds, then also process the the newly created
49cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// instruction.
50cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I,
51cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                    MachineFunction::iterator MFI);
527f8300b8f3f35167ce78628bc14d50beec203b8fPreston Gurd
53cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Given a memory access or LEA instruction
54cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// whose address mode uses a base and/or index register, look for
55cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// an opportunity to replace the instruction which sets the base or index
56cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// register with an equivalent LEA instruction.
57cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  void processInstruction(MachineBasicBlock::iterator &I,
58cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                          MachineFunction::iterator MFI);
597f8300b8f3f35167ce78628bc14d50beec203b8fPreston Gurd
60cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Given a LEA instruction which is unprofitable
61cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// on Silvermont try to replace it with an equivalent ADD instruction
62cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  void processInstructionForSLM(MachineBasicBlock::iterator &I,
63cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                MachineFunction::iterator MFI);
64dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
65cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Determine if an instruction references a machine register
66cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// and, if so, whether it reads or writes the register.
67cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
687f8300b8f3f35167ce78628bc14d50beec203b8fPreston Gurd
69cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Step backwards through a basic block, looking
70cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// for an instruction which writes a register within
71cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
72cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineBasicBlock::iterator searchBackwards(MachineOperand &p,
73cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                              MachineBasicBlock::iterator &I,
74cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                              MachineFunction::iterator MFI);
757f8300b8f3f35167ce78628bc14d50beec203b8fPreston Gurd
76cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief if an instruction can be converted to an
77cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// equivalent LEA, insert the new instruction into the basic block
78cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// and return a pointer to it. Otherwise, return zero.
79cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI,
80cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                   MachineBasicBlock::iterator &MBBI) const;
81d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
82cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinespublic:
83cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  FixupLEAPass() : MachineFunctionPass(ID) {}
84d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
85cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// \brief Loop over all of the basic blocks,
86cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// replacing instructions by equivalent LEA instructions
87cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  /// if needed and when possible.
88cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  bool runOnMachineFunction(MachineFunction &MF) override;
89d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
90cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesprivate:
91cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineFunction *MF;
92cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  const TargetMachine *TM;
93cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  const X86InstrInfo *TII; // Machine instruction info.
94cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines};
95cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hineschar FixupLEAPass::ID = 0;
96d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
97d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
98d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston GurdMachineInstr *
99d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston GurdFixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
1007f8300b8f3f35167ce78628bc14d50beec203b8fPreston Gurd                                 MachineBasicBlock::iterator &MBBI) const {
101cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineInstr *MI = MBBI;
102cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineInstr *NewMI;
103d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  switch (MI->getOpcode()) {
104dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case X86::MOV32rr:
105d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::MOV64rr: {
106cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    const MachineOperand &Src = MI->getOperand(1);
107cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    const MachineOperand &Dest = MI->getOperand(0);
108d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    NewMI = BuildMI(*MF, MI->getDebugLoc(),
109cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                    TII->get(MI->getOpcode() == X86::MOV32rr ? X86::LEA32r
110cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                                             : X86::LEA64r))
111cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                .addOperand(Dest)
112cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                .addOperand(Src)
113cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                .addImm(1)
114cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                .addReg(0)
115cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                .addImm(0)
116cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                .addReg(0);
117cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    MFI->insert(MBBI, NewMI); // Insert the new inst
118d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    return NewMI;
119d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
120d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD64ri32:
121d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD64ri8:
122d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD64ri32_DB:
123d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD64ri8_DB:
124d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD32ri:
125d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD32ri8:
126d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD32ri_DB:
127d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD32ri8_DB:
128d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD16ri:
129d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD16ri8:
130d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD16ri_DB:
131d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  case X86::ADD16ri8_DB:
132d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (!MI->getOperand(2).isImm()) {
133d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      // convertToThreeAddress will call getImm()
134d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      // which requires isImm() to be true
135dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return nullptr;
136d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    }
1377813a2fe9dff1d30efaaf84bec8eb02f8b78bd06Preston Gurd    break;
1382967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd  case X86::ADD16rr:
1392967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd  case X86::ADD16rr_DB:
1402967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd    if (MI->getOperand(1).getReg() != MI->getOperand(2).getReg()) {
1412967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd      // if src1 != src2, then convertToThreeAddress will
1422967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd      // need to create a Virtual register, which we cannot do
1432967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd      // after register allocation.
144dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return nullptr;
1452967a804126c6081a18d8b75adf2d9fd5a3c096ePreston Gurd    }
146d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
147dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return TII->convertToThreeAddress(MFI, MBBI, nullptr);
148d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
149d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
150cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesFunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); }
151d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
152d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurdbool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
153cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MF = &Func;
154dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  TM = &Func.getTarget();
155dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const X86Subtarget &ST = TM->getSubtarget<X86Subtarget>();
156dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!ST.LEAusesAG() && !ST.slowLEA())
157dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return false;
158dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
159cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  TII = static_cast<const X86InstrInfo *>(TM->getInstrInfo());
160d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
161d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  DEBUG(dbgs() << "Start X86FixupLEAs\n";);
162d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  // Process all basic blocks.
163d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
164d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    processBasicBlock(Func, I);
165d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  DEBUG(dbgs() << "End X86FixupLEAs\n";);
166d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
167d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  return true;
168d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
169d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
170cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesFixupLEAPass::RegUsageState
171cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesFixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) {
172d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  RegUsageState RegUsage = RU_NotUsed;
173cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineInstr *MI = I;
174d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
175d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  for (unsigned int i = 0; i < MI->getNumOperands(); ++i) {
176cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    MachineOperand &opnd = MI->getOperand(i);
177cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    if (opnd.isReg() && opnd.getReg() == p.getReg()) {
178d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      if (opnd.isDef())
179d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd        return RU_Write;
180d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      RegUsage = RU_Read;
181d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    }
182d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
183d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  return RegUsage;
184d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
185d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
186d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd/// getPreviousInstr - Given a reference to an instruction in a basic
187d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd/// block, return a reference to the previous instruction in the block,
188d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd/// wrapping around to the last instruction of the block if the block
189d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd/// branches to itself.
190cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesstatic inline bool getPreviousInstr(MachineBasicBlock::iterator &I,
191d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd                                    MachineFunction::iterator MFI) {
192d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  if (I == MFI->begin()) {
193d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (MFI->isPredecessor(MFI)) {
194d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      I = --MFI->end();
195d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      return true;
196cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    } else
197d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      return false;
198d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
199d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  --I;
200d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  return true;
201d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
202d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
203cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesMachineBasicBlock::iterator
204cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen HinesFixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I,
205cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                              MachineFunction::iterator MFI) {
206d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  int InstrDistance = 1;
207d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  MachineBasicBlock::iterator CurInst;
208d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  static const int INSTR_DISTANCE_THRESHOLD = 5;
209d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
210d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  CurInst = I;
211d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  bool Found;
212d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  Found = getPreviousInstr(CurInst, MFI);
213cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  while (Found && I != CurInst) {
214d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (CurInst->isCall() || CurInst->isInlineAsm())
215d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      break;
216d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
217d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      break; // too far back to make a difference
218cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    if (usesRegister(p, CurInst) == RU_Write) {
219d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      return CurInst;
220d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    }
221d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    InstrDistance += TII->getInstrLatency(TM->getInstrItineraryData(), CurInst);
222d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    Found = getPreviousInstr(CurInst, MFI);
223d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
224dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  return nullptr;
225d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
226d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
227cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesvoid FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I,
228d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd                                      MachineFunction::iterator MFI) {
229d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  // Process a load, store, or LEA instruction.
230d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  MachineInstr *MI = I;
231d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  int opcode = MI->getOpcode();
232cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  const MCInstrDesc &Desc = MI->getDesc();
233d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags, opcode);
234d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  if (AddrOffset >= 0) {
235d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    AddrOffset += X86II::getOperandBias(Desc);
236cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    MachineOperand &p = MI->getOperand(AddrOffset + X86::AddrBaseReg);
237d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (p.isReg() && p.getReg() != X86::ESP) {
238d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      seekLEAFixup(p, I, MFI);
239d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    }
240cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    MachineOperand &q = MI->getOperand(AddrOffset + X86::AddrIndexReg);
241d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (q.isReg() && q.getReg() != X86::ESP) {
242d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      seekLEAFixup(q, I, MFI);
243d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    }
244d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
245d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
246d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
247cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hinesvoid FixupLEAPass::seekLEAFixup(MachineOperand &p,
248cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines                                MachineBasicBlock::iterator &I,
249d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd                                MachineFunction::iterator MFI) {
250d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
251d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  if (MBI) {
252cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI);
253d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    if (NewMI) {
254d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      ++NumLEAs;
255dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump(););
256d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      // now to replace with an equivalent LEA...
257dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump(););
258d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      MFI->erase(MBI);
259d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      MachineBasicBlock::iterator J =
260cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines          static_cast<MachineBasicBlock::iterator>(NewMI);
261d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd      processInstruction(J, MFI);
262d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd    }
263d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  }
264d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
265d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
266dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinesvoid FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I,
267dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                                            MachineFunction::iterator MFI) {
268dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  MachineInstr *MI = I;
269dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const int opcode = MI->getOpcode();
270dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (opcode != X86::LEA16r && opcode != X86::LEA32r && opcode != X86::LEA64r &&
271dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      opcode != X86::LEA64_32r)
272dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return;
273dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (MI->getOperand(5).getReg() != 0 || !MI->getOperand(4).isImm() ||
274dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      !TII->isSafeToClobberEFLAGS(*MFI, I))
275dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return;
276dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const unsigned DstR = MI->getOperand(0).getReg();
277dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const unsigned SrcR1 = MI->getOperand(1).getReg();
278dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const unsigned SrcR2 = MI->getOperand(3).getReg();
279dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR))
280dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return;
281dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (MI->getOperand(2).getImm() > 1)
282dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return;
283dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  int addrr_opcode, addri_opcode;
284dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  switch (opcode) {
285dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case X86::LEA16r:
286dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addrr_opcode = X86::ADD16rr;
287dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addri_opcode = X86::ADD16ri;
288dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    break;
289dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case X86::LEA32r:
290dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addrr_opcode = X86::ADD32rr;
291dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addri_opcode = X86::ADD32ri;
292dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    break;
293dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case X86::LEA64_32r:
294dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  case X86::LEA64r:
295dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addrr_opcode = X86::ADD64rr;
296dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    addri_opcode = X86::ADD64ri32;
297dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    break;
298dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  default:
299dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    assert(false && "Unexpected LEA instruction");
300dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
301dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump(););
302dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  DEBUG(dbgs() << "FixLEA: Replaced by: ";);
303cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines  MachineInstr *NewMI = nullptr;
304dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  const MachineOperand &Dst = MI->getOperand(0);
305dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Make ADD instruction for two registers writing to LEA's destination
306dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (SrcR1 != 0 && SrcR2 != 0) {
307dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MachineOperand &Src1 = MI->getOperand(SrcR1 == DstR ? 1 : 3);
308dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MachineOperand &Src2 = MI->getOperand(SrcR1 == DstR ? 3 : 1);
309dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addrr_opcode))
310dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                .addOperand(Dst)
311dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                .addOperand(Src1)
312dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                .addOperand(Src2);
313dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    MFI->insert(I, NewMI);
314dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DEBUG(NewMI->dump(););
315dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
316dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Make ADD instruction for immediate
317dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (MI->getOperand(4).getImm() != 0) {
318dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    const MachineOperand &SrcR = MI->getOperand(SrcR1 == DstR ? 1 : 3);
319dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addri_opcode))
320dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                .addOperand(Dst)
321dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                .addOperand(SrcR)
322dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                .addImm(MI->getOperand(4).getImm());
323dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    MFI->insert(I, NewMI);
324dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    DEBUG(NewMI->dump(););
325dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
326dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (NewMI) {
327dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    MFI->erase(I);
328dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    I = static_cast<MachineBasicBlock::iterator>(NewMI);
329dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
330dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines}
331dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
332d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurdbool FixupLEAPass::processBasicBlock(MachineFunction &MF,
333d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd                                     MachineFunction::iterator MFI) {
334d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd
335dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) {
336dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    if (TM->getSubtarget<X86Subtarget>().isSLM())
337dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      processInstructionForSLM(I, MFI);
338dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    else
339dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      processInstruction(I, MFI);
340dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  }
341d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd  return false;
342d6ac8e9a03d8fa7115079d86192bc4529e8281aaPreston Gurd}
343