blob: fd42ecd3fac1df79a4bc3829dc80bf9a3fc13700 [file] [log] [blame]
Preston Gurd8b7ab4b2013-04-25 20:29:37 +00001//===-- 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//
10// This file defines the pass which will find instructions which
11// can be re-written as LEA instructions in order to reduce pipeline
12// delays for some models of the Intel Atom family.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "x86-fixup-LEAs"
17#include "X86.h"
18#include "X86InstrInfo.h"
19#include "X86Subtarget.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/CodeGen/LiveVariables.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/Passes.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28#include "llvm/Target/TargetInstrInfo.h"
29using namespace llvm;
30
31STATISTIC(NumLEAs, "Number of LEA instructions created");
32
33namespace {
34 class FixupLEAPass : public MachineFunctionPass {
35 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read };
36 static char ID;
Preston Gurd128920d2013-04-25 21:31:33 +000037 /// \brief Loop over all of the instructions in the basic block
38 /// replacing applicable instructions with LEA instructions,
39 /// where appropriate.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000040 bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI);
41
42 virtual const char *getPassName() const { return "X86 Atom LEA Fixup";}
Preston Gurd128920d2013-04-25 21:31:33 +000043
44 /// \brief Given a machine register, look for the instruction
45 /// which writes it in the current basic block. If found,
46 /// try to replace it with an equivalent LEA instruction.
47 /// If replacement succeeds, then also process the the newly created
48 /// instruction.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000049 void seekLEAFixup(MachineOperand& p, MachineBasicBlock::iterator& I,
50 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000051
52 /// \brief Given a memory access or LEA instruction
53 /// whose address mode uses a base and/or index register, look for
54 /// an opportunity to replace the instruction which sets the base or index
55 /// register with an equivalent LEA instruction.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000056 void processInstruction(MachineBasicBlock::iterator& I,
57 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000058
59 /// \brief Determine if an instruction references a machine register
60 /// and, if so, whether it reads or writes the register.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000061 RegUsageState usesRegister(MachineOperand& p,
62 MachineBasicBlock::iterator I);
Preston Gurd128920d2013-04-25 21:31:33 +000063
64 /// \brief Step backwards through a basic block, looking
65 /// for an instruction which writes a register within
66 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000067 MachineBasicBlock::iterator searchBackwards(MachineOperand& p,
68 MachineBasicBlock::iterator& I,
69 MachineFunction::iterator MFI);
Preston Gurd128920d2013-04-25 21:31:33 +000070
71 /// \brief if an instruction can be converted to an
72 /// equivalent LEA, insert the new instruction into the basic block
73 /// and return a pointer to it. Otherwise, return zero.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000074 MachineInstr* postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +000075 MachineBasicBlock::iterator &MBBI) const;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000076
77 public:
78 FixupLEAPass() : MachineFunctionPass(ID) {}
79
Preston Gurd128920d2013-04-25 21:31:33 +000080 /// \brief Loop over all of the basic blocks,
81 /// replacing instructions by equivalent LEA instructions
82 /// if needed and when possible.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000083 virtual bool runOnMachineFunction(MachineFunction &MF);
84
85 private:
86 MachineFunction *MF;
87 const TargetMachine *TM;
88 const TargetInstrInfo *TII; // Machine instruction info.
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000089
90 };
91 char FixupLEAPass::ID = 0;
92}
93
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000094MachineInstr *
95FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI,
Preston Gurd128920d2013-04-25 21:31:33 +000096 MachineBasicBlock::iterator &MBBI) const {
Preston Gurd8b7ab4b2013-04-25 20:29:37 +000097 MachineInstr* MI = MBBI;
98 MachineInstr* NewMI;
99 switch (MI->getOpcode()) {
100 case X86::MOV32rr:
101 case X86::MOV64rr: {
102 const MachineOperand& Src = MI->getOperand(1);
103 const MachineOperand& Dest = MI->getOperand(0);
104 NewMI = BuildMI(*MF, MI->getDebugLoc(),
105 TII->get( MI->getOpcode() == X86::MOV32rr ? X86::LEA32r : X86::LEA64r))
106 .addOperand(Dest)
107 .addOperand(Src).addImm(1).addReg(0).addImm(0).addReg(0);
108 MFI->insert(MBBI, NewMI); // Insert the new inst
109 return NewMI;
110 }
111 case X86::ADD64ri32:
112 case X86::ADD64ri8:
113 case X86::ADD64ri32_DB:
114 case X86::ADD64ri8_DB:
115 case X86::ADD32ri:
116 case X86::ADD32ri8:
117 case X86::ADD32ri_DB:
118 case X86::ADD32ri8_DB:
119 case X86::ADD16ri:
120 case X86::ADD16ri8:
121 case X86::ADD16ri_DB:
122 case X86::ADD16ri8_DB:
123 if (!MI->getOperand(2).isImm()) {
124 // convertToThreeAddress will call getImm()
125 // which requires isImm() to be true
126 return 0;
127 }
Preston Gurdf0b62882013-09-30 23:18:42 +0000128 case X86::ADD16rr:
129 case X86::ADD16rr_DB:
130 if (MI->getOperand(1).getReg() != MI->getOperand(2).getReg()) {
131 // if src1 != src2, then convertToThreeAddress will
132 // need to create a Virtual register, which we cannot do
133 // after register allocation.
134 return 0;
135 }
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000136 }
Preston Gurd128920d2013-04-25 21:31:33 +0000137 return TII->convertToThreeAddress(MFI, MBBI, 0);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000138}
139
140FunctionPass *llvm::createX86FixupLEAs() {
141 return new FixupLEAPass();
142}
143
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000144bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) {
145 MF = &Func;
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000146 TM = &MF->getTarget();
Bill Wendling8f268402013-06-07 21:00:34 +0000147 TII = TM->getInstrInfo();
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000148
149 DEBUG(dbgs() << "Start X86FixupLEAs\n";);
150 // Process all basic blocks.
151 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I)
152 processBasicBlock(Func, I);
153 DEBUG(dbgs() << "End X86FixupLEAs\n";);
154
155 return true;
156}
157
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000158FixupLEAPass::RegUsageState FixupLEAPass::usesRegister(MachineOperand& p,
159 MachineBasicBlock::iterator I) {
160 RegUsageState RegUsage = RU_NotUsed;
161 MachineInstr* MI = I;
162
163 for (unsigned int i = 0; i < MI->getNumOperands(); ++i) {
164 MachineOperand& opnd = MI->getOperand(i);
165 if (opnd.isReg() && opnd.getReg() == p.getReg()){
166 if (opnd.isDef())
167 return RU_Write;
168 RegUsage = RU_Read;
169 }
170 }
171 return RegUsage;
172}
173
174/// getPreviousInstr - Given a reference to an instruction in a basic
175/// block, return a reference to the previous instruction in the block,
176/// wrapping around to the last instruction of the block if the block
177/// branches to itself.
178static inline bool getPreviousInstr(MachineBasicBlock::iterator& I,
179 MachineFunction::iterator MFI) {
180 if (I == MFI->begin()) {
181 if (MFI->isPredecessor(MFI)) {
182 I = --MFI->end();
183 return true;
184 }
185 else
186 return false;
187 }
188 --I;
189 return true;
190}
191
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000192MachineBasicBlock::iterator FixupLEAPass::searchBackwards(MachineOperand& p,
193 MachineBasicBlock::iterator& I,
194 MachineFunction::iterator MFI) {
195 int InstrDistance = 1;
196 MachineBasicBlock::iterator CurInst;
197 static const int INSTR_DISTANCE_THRESHOLD = 5;
198
199 CurInst = I;
200 bool Found;
201 Found = getPreviousInstr(CurInst, MFI);
202 while( Found && I != CurInst) {
203 if (CurInst->isCall() || CurInst->isInlineAsm())
204 break;
205 if (InstrDistance > INSTR_DISTANCE_THRESHOLD)
206 break; // too far back to make a difference
207 if (usesRegister(p, CurInst) == RU_Write){
208 return CurInst;
209 }
210 InstrDistance += TII->getInstrLatency(TM->getInstrItineraryData(), CurInst);
211 Found = getPreviousInstr(CurInst, MFI);
212 }
213 return 0;
214}
215
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000216void FixupLEAPass::processInstruction(MachineBasicBlock::iterator& I,
217 MachineFunction::iterator MFI) {
218 // Process a load, store, or LEA instruction.
219 MachineInstr *MI = I;
220 int opcode = MI->getOpcode();
221 const MCInstrDesc& Desc = MI->getDesc();
222 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags, opcode);
223 if (AddrOffset >= 0) {
224 AddrOffset += X86II::getOperandBias(Desc);
225 MachineOperand& p = MI->getOperand(AddrOffset + X86::AddrBaseReg);
226 if (p.isReg() && p.getReg() != X86::ESP) {
227 seekLEAFixup(p, I, MFI);
228 }
229 MachineOperand& q = MI->getOperand(AddrOffset + X86::AddrIndexReg);
230 if (q.isReg() && q.getReg() != X86::ESP) {
231 seekLEAFixup(q, I, MFI);
232 }
233 }
234}
235
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000236void FixupLEAPass::seekLEAFixup(MachineOperand& p,
237 MachineBasicBlock::iterator& I,
238 MachineFunction::iterator MFI) {
239 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI);
240 if (MBI) {
Preston Gurd128920d2013-04-25 21:31:33 +0000241 MachineInstr* NewMI = postRAConvertToLEA(MFI, MBI);
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000242 if (NewMI) {
243 ++NumLEAs;
244 DEBUG(dbgs() << "Candidate to replace:"; MBI->dump(););
245 // now to replace with an equivalent LEA...
246 DEBUG(dbgs() << "Replaced by: "; NewMI->dump(););
247 MFI->erase(MBI);
248 MachineBasicBlock::iterator J =
249 static_cast<MachineBasicBlock::iterator> (NewMI);
250 processInstruction(J, MFI);
251 }
252 }
253}
254
Preston Gurd8b7ab4b2013-04-25 20:29:37 +0000255bool FixupLEAPass::processBasicBlock(MachineFunction &MF,
256 MachineFunction::iterator MFI) {
257
258 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I)
259 processInstruction(I, MFI);
260 return false;
261}