blob: 4fb26ddc392289ffdc2c7b7bf2b6902e619d6318 [file] [log] [blame]
Alexey Bataev7cf32472015-12-04 10:53:15 +00001//===-- X86OptimizeLEAs.cpp - optimize usage of LEA instructions ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the pass that performs some optimizations with LEA
Andrey Turetskiy45b22a42016-05-19 10:18:29 +000011// instructions in order to improve performance and code size.
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +000012// Currently, it does two things:
13// 1) If there are two LEA instructions calculating addresses which only differ
14// by displacement inside a basic block, one of them is removed.
15// 2) Address calculations in load and store instructions are replaced by
Alexey Bataev7cf32472015-12-04 10:53:15 +000016// existing LEA def registers where possible.
17//
18//===----------------------------------------------------------------------===//
19
20#include "X86.h"
21#include "X86InstrInfo.h"
22#include "X86Subtarget.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/CodeGen/LiveVariables.h"
Jatin Bhateja3c29bac2017-10-04 09:02:10 +000025#include "llvm/CodeGen/MachineDominators.h"
Alexey Bataev7cf32472015-12-04 10:53:15 +000026#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Andrey Turetskiy0babd262016-02-20 10:58:28 +000028#include "llvm/CodeGen/MachineOperand.h"
Alexey Bataev7cf32472015-12-04 10:53:15 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
30#include "llvm/CodeGen/Passes.h"
Andrew Ng03e35b62017-04-28 08:44:30 +000031#include "llvm/IR/DIBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000032#include "llvm/IR/DebugInfoMetadata.h"
Alexey Bataev7cf32472015-12-04 10:53:15 +000033#include "llvm/IR/Function.h"
34#include "llvm/Support/Debug.h"
35#include "llvm/Support/raw_ostream.h"
36#include "llvm/Target/TargetInstrInfo.h"
37
38using namespace llvm;
39
40#define DEBUG_TYPE "x86-optimize-LEAs"
41
Andrey Turetskiy9994b882016-02-20 11:11:55 +000042static cl::opt<bool>
43 DisableX86LEAOpt("disable-x86-lea-opt", cl::Hidden,
44 cl::desc("X86: Disable LEA optimizations."),
45 cl::init(false));
Alexey Bataev7b72b652015-12-17 07:34:39 +000046
Alexey Bataev7cf32472015-12-04 10:53:15 +000047STATISTIC(NumSubstLEAs, "Number of LEA instruction substitutions");
Jatin Bhateja3c29bac2017-10-04 09:02:10 +000048STATISTIC(NumFactoredLEAs, "Number of LEAs factorized");
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +000049STATISTIC(NumRedundantLEAs, "Number of redundant LEA instructions removed");
Alexey Bataev7cf32472015-12-04 10:53:15 +000050
Andrey Turetskiybca0f992016-02-04 08:57:03 +000051/// \brief Returns true if two machine operands are identical and they are not
52/// physical registers.
53static inline bool isIdenticalOp(const MachineOperand &MO1,
54 const MachineOperand &MO2);
55
Jatin Bhateja3c29bac2017-10-04 09:02:10 +000056/// \brief Returns true if two machine instructions have identical operands.
57static bool isIdenticalMI(MachineRegisterInfo *MRI, const MachineOperand &MO1,
58 const MachineOperand &MO2);
59
Andrey Turetskiy0babd262016-02-20 10:58:28 +000060/// \brief Returns true if two address displacement operands are of the same
61/// type and use the same symbol/index/address regardless of the offset.
62static bool isSimilarDispOp(const MachineOperand &MO1,
63 const MachineOperand &MO2);
64
Andrey Turetskiybca0f992016-02-04 08:57:03 +000065/// \brief Returns true if the instruction is LEA.
66static inline bool isLEA(const MachineInstr &MI);
67
Jatin Bhateja3c29bac2017-10-04 09:02:10 +000068/// \brief Returns true if Definition of Operand is a copylike instruction.
69static bool isDefCopyLike(MachineRegisterInfo *MRI, const MachineOperand &Opr);
70
Benjamin Kramerb7d33112016-08-06 11:13:10 +000071namespace {
Andrey Turetskiybca0f992016-02-04 08:57:03 +000072/// A key based on instruction's memory operands.
73class MemOpKey {
74public:
75 MemOpKey(const MachineOperand *Base, const MachineOperand *Scale,
76 const MachineOperand *Index, const MachineOperand *Segment,
Jatin Bhateja3c29bac2017-10-04 09:02:10 +000077 const MachineOperand *Disp, bool DispCheck = false)
78 : Disp(Disp), DeepCheck(DispCheck) {
Andrey Turetskiybca0f992016-02-04 08:57:03 +000079 Operands[0] = Base;
80 Operands[1] = Scale;
81 Operands[2] = Index;
82 Operands[3] = Segment;
83 }
84
Jatin Bhateja3c29bac2017-10-04 09:02:10 +000085 /// Checks operands of MemOpKey are identical, if Base or Index
86 /// operand definitions are of kind SUBREG_TO_REG then compare
87 /// operands of defining MI.
88 bool performDeepCheck(const MemOpKey &Other) const {
89 MachineInstr *MI = const_cast<MachineInstr *>(Operands[0]->getParent());
90 MachineRegisterInfo *MRI = MI->getRegInfo();
91
92 for (int i = 0; i < 4; i++) {
93 bool copyLike = isDefCopyLike(MRI, *Operands[i]);
94 if (copyLike && !isIdenticalMI(MRI, *Operands[i], *Other.Operands[i]))
95 return false;
96 else if (!copyLike && !isIdenticalOp(*Operands[i], *Other.Operands[i]))
97 return false;
98 }
99 return isIdenticalOp(*Disp, *Other.Disp);
100 }
101
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000102 bool operator==(const MemOpKey &Other) const {
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000103 if (DeepCheck)
104 return performDeepCheck(Other);
105
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000106 // Addresses' bases, scales, indices and segments must be identical.
107 for (int i = 0; i < 4; ++i)
108 if (!isIdenticalOp(*Operands[i], *Other.Operands[i]))
109 return false;
110
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000111 // Addresses' displacements don't have to be exactly the same. It only
112 // matters that they use the same symbol/index/address. Immediates' or
113 // offsets' differences will be taken care of during instruction
114 // substitution.
115 return isSimilarDispOp(*Disp, *Other.Disp);
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000116 }
117
118 // Address' base, scale, index and segment operands.
119 const MachineOperand *Operands[4];
120
121 // Address' displacement operand.
122 const MachineOperand *Disp;
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000123
124 // If true checks Address' base, index, segment and
125 // displacement are identical, in additions if base/index
126 // are defined by copylike instruction then futher
127 // compare the operands of the defining instruction.
128 bool DeepCheck;
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000129};
Benjamin Kramerb7d33112016-08-06 11:13:10 +0000130} // end anonymous namespace
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000131
132/// Provide DenseMapInfo for MemOpKey.
133namespace llvm {
134template <> struct DenseMapInfo<MemOpKey> {
135 typedef DenseMapInfo<const MachineOperand *> PtrInfo;
136
137 static inline MemOpKey getEmptyKey() {
138 return MemOpKey(PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(),
139 PtrInfo::getEmptyKey(), PtrInfo::getEmptyKey(),
140 PtrInfo::getEmptyKey());
141 }
142
143 static inline MemOpKey getTombstoneKey() {
144 return MemOpKey(PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(),
145 PtrInfo::getTombstoneKey(), PtrInfo::getTombstoneKey(),
146 PtrInfo::getTombstoneKey());
147 }
148
149 static unsigned getHashValue(const MemOpKey &Val) {
150 // Checking any field of MemOpKey is enough to determine if the key is
151 // empty or tombstone.
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000152 hash_code Hash(0);
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000153 assert(Val.Disp != PtrInfo::getEmptyKey() && "Cannot hash the empty key");
154 assert(Val.Disp != PtrInfo::getTombstoneKey() &&
155 "Cannot hash the tombstone key");
156
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000157 auto getMIHash = [](MachineInstr *MI) -> hash_code {
158 hash_code h(0);
159 for (unsigned i = 1, e = MI->getNumOperands(); i < e; i++)
160 h = hash_combine(h, MI->getOperand(i));
161 return h;
162 };
163
164 const MachineOperand &Base = *Val.Operands[0];
165 const MachineOperand &Index = *Val.Operands[2];
166 MachineInstr *MI = const_cast<MachineInstr *>(Base.getParent());
167 MachineRegisterInfo *MRI = MI->getRegInfo();
168
169 if (isDefCopyLike(MRI, Base))
170 Hash = getMIHash(MRI->getVRegDef(Base.getReg()));
171 else
172 Hash = hash_combine(Hash, Base);
173
174 if (isDefCopyLike(MRI, Index))
175 Hash = getMIHash(MRI->getVRegDef(Index.getReg()));
176 else
177 Hash = hash_combine(Hash, Index);
178
179 Hash = hash_combine(Hash, *Val.Operands[1], *Val.Operands[3]);
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000180
181 // If the address displacement is an immediate, it should not affect the
182 // hash so that memory operands which differ only be immediate displacement
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000183 // would have the same hash. If the address displacement is something else,
184 // we should reflect symbol/index/address in the hash.
185 switch (Val.Disp->getType()) {
186 case MachineOperand::MO_Immediate:
187 break;
188 case MachineOperand::MO_ConstantPoolIndex:
189 case MachineOperand::MO_JumpTableIndex:
190 Hash = hash_combine(Hash, Val.Disp->getIndex());
191 break;
192 case MachineOperand::MO_ExternalSymbol:
193 Hash = hash_combine(Hash, Val.Disp->getSymbolName());
194 break;
195 case MachineOperand::MO_GlobalAddress:
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000196 Hash = hash_combine(Hash, Val.Disp->getGlobal());
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000197 break;
198 case MachineOperand::MO_BlockAddress:
199 Hash = hash_combine(Hash, Val.Disp->getBlockAddress());
200 break;
201 case MachineOperand::MO_MCSymbol:
202 Hash = hash_combine(Hash, Val.Disp->getMCSymbol());
203 break;
Andrey Turetskiyb4056062016-04-26 12:18:12 +0000204 case MachineOperand::MO_MachineBasicBlock:
205 Hash = hash_combine(Hash, Val.Disp->getMBB());
206 break;
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000207 default:
208 llvm_unreachable("Invalid address displacement operand");
209 }
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000210
211 return (unsigned)Hash;
212 }
213
214 static bool isEqual(const MemOpKey &LHS, const MemOpKey &RHS) {
215 // Checking any field of MemOpKey is enough to determine if the key is
216 // empty or tombstone.
217 if (RHS.Disp == PtrInfo::getEmptyKey())
218 return LHS.Disp == PtrInfo::getEmptyKey();
219 if (RHS.Disp == PtrInfo::getTombstoneKey())
220 return LHS.Disp == PtrInfo::getTombstoneKey();
221 return LHS == RHS;
222 }
223};
224}
225
Benjamin Kramerb7d33112016-08-06 11:13:10 +0000226/// \brief Returns a hash table key based on memory operands of \p MI. The
227/// number of the first memory operand of \p MI is specified through \p N.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000228static inline MemOpKey getMemOpKey(const MachineInstr &MI, unsigned N) {
229 assert((isLEA(MI) || MI.mayLoadOrStore()) &&
230 "The instruction must be a LEA, a load or a store");
231 return MemOpKey(&MI.getOperand(N + X86::AddrBaseReg),
232 &MI.getOperand(N + X86::AddrScaleAmt),
233 &MI.getOperand(N + X86::AddrIndexReg),
234 &MI.getOperand(N + X86::AddrSegmentReg),
235 &MI.getOperand(N + X86::AddrDisp));
236}
237
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000238static inline MemOpKey getMemOpCSEKey(const MachineInstr &MI, unsigned N) {
239 static MachineOperand DummyScale = MachineOperand::CreateImm(1);
240 assert((isLEA(MI) || MI.mayLoadOrStore()) &&
241 "The instruction must be a LEA, a load or a store");
242 return MemOpKey(&MI.getOperand(N + X86::AddrBaseReg), &DummyScale,
243 &MI.getOperand(N + X86::AddrIndexReg),
244 &MI.getOperand(N + X86::AddrSegmentReg),
245 &MI.getOperand(N + X86::AddrDisp), true);
246}
247
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000248static inline bool isIdenticalOp(const MachineOperand &MO1,
249 const MachineOperand &MO2) {
250 return MO1.isIdenticalTo(MO2) &&
251 (!MO1.isReg() ||
252 !TargetRegisterInfo::isPhysicalRegister(MO1.getReg()));
253}
254
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000255static bool isIdenticalMI(MachineRegisterInfo *MRI, const MachineOperand &MO1,
256 const MachineOperand &MO2) {
257 MachineInstr *MI1 = nullptr;
258 MachineInstr *MI2 = nullptr;
259 if (!MO1.isReg() || !MO2.isReg())
260 return false;
261
262 MI1 = MRI->getVRegDef(MO1.getReg());
263 MI2 = MRI->getVRegDef(MO2.getReg());
264 if (!MI1 || !MI2)
265 return false;
266 if (MI1->getOpcode() != MI2->getOpcode())
267 return false;
268 if (MI1->getNumOperands() != MI2->getNumOperands())
269 return false;
270 for (unsigned i = 1, e = MI1->getNumOperands(); i < e; ++i)
271 if (!isIdenticalOp(MI1->getOperand(i), MI2->getOperand(i)))
272 return false;
273 return true;
274}
275
Justin Bogner38e52172016-02-24 07:58:02 +0000276#ifndef NDEBUG
277static bool isValidDispOp(const MachineOperand &MO) {
278 return MO.isImm() || MO.isCPI() || MO.isJTI() || MO.isSymbol() ||
Andrey Turetskiyb4056062016-04-26 12:18:12 +0000279 MO.isGlobal() || MO.isBlockAddress() || MO.isMCSymbol() || MO.isMBB();
Justin Bogner38e52172016-02-24 07:58:02 +0000280}
281#endif
282
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000283static bool isSimilarDispOp(const MachineOperand &MO1,
284 const MachineOperand &MO2) {
285 assert(isValidDispOp(MO1) && isValidDispOp(MO2) &&
286 "Address displacement operand is not valid");
287 return (MO1.isImm() && MO2.isImm()) ||
288 (MO1.isCPI() && MO2.isCPI() && MO1.getIndex() == MO2.getIndex()) ||
289 (MO1.isJTI() && MO2.isJTI() && MO1.getIndex() == MO2.getIndex()) ||
290 (MO1.isSymbol() && MO2.isSymbol() &&
291 MO1.getSymbolName() == MO2.getSymbolName()) ||
292 (MO1.isGlobal() && MO2.isGlobal() &&
293 MO1.getGlobal() == MO2.getGlobal()) ||
294 (MO1.isBlockAddress() && MO2.isBlockAddress() &&
295 MO1.getBlockAddress() == MO2.getBlockAddress()) ||
296 (MO1.isMCSymbol() && MO2.isMCSymbol() &&
Andrey Turetskiyb4056062016-04-26 12:18:12 +0000297 MO1.getMCSymbol() == MO2.getMCSymbol()) ||
298 (MO1.isMBB() && MO2.isMBB() && MO1.getMBB() == MO2.getMBB());
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000299}
300
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000301static inline bool isLEA(const MachineInstr &MI) {
302 unsigned Opcode = MI.getOpcode();
303 return Opcode == X86::LEA16r || Opcode == X86::LEA32r ||
304 Opcode == X86::LEA64r || Opcode == X86::LEA64_32r;
305}
306
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000307static bool isDefCopyLike(MachineRegisterInfo *MRI, const MachineOperand &Opr) {
308 if (!Opr.isReg() || TargetRegisterInfo::isPhysicalRegister(Opr.getReg()))
309 return false;
310 MachineInstr *MI = MRI->getVRegDef(Opr.getReg());
311 return MI && MI->isCopyLike();
312}
313
Alexey Bataev7cf32472015-12-04 10:53:15 +0000314namespace {
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000315
316/// This class captures the functions and attributes
317/// needed to factorize LEA within and across basic
318/// blocks.LEA instruction with same BASE,OFFSET and
319/// INDEX are the candidates for factorization.
320class FactorizeLEAOpt {
321public:
322 using LEAListT = std::list<MachineInstr *>;
323 using LEAMapT = DenseMap<MemOpKey, LEAListT>;
324 using ValueT = DenseMap<MemOpKey, unsigned>;
325 using ScopeEntryT = std::pair<MachineBasicBlock *, ValueT>;
326 using ScopeStackT = std::vector<ScopeEntryT>;
327
328 FactorizeLEAOpt() = default;
329 FactorizeLEAOpt(const FactorizeLEAOpt &) = delete;
330 FactorizeLEAOpt &operator=(const FactorizeLEAOpt &) = delete;
331
332 void performCleanup() {
333 for (auto LEA : removedLEAs)
334 LEA->eraseFromParent();
335 LEAs.clear();
336 Stack.clear();
337 removedLEAs.clear();
338 }
339
340 LEAMapT &getLEAMap() { return LEAs; }
341 ScopeEntryT *getTopScope() { return &Stack.back(); }
342
343 void addForLazyRemoval(MachineInstr *Instr) { removedLEAs.insert(Instr); }
344
345 bool checkIfScheduledForRemoval(MachineInstr *Instr) {
346 return removedLEAs.find(Instr) != removedLEAs.end();
347 }
348
349 /// Push the ScopeEntry for the BasicBlock over Stack.
350 /// Also traverses over list of instruction and update
351 /// LEAs Map and ScopeEntry for each LEA instruction
352 /// found using insertLEA().
353 void pushScope(MachineBasicBlock *MBB);
354
355 /// Stores the size of MachineInstr list corrosponding
356 /// to key K from LEAs MAP into the ScopeEntry of
357 /// the basic block, then insert the LEA at the beginning
358 /// of the list.
359 void insertLEA(MachineInstr *MI);
360
361 /// Pops out ScopeEntry of top most BasicBlock from the stack
362 /// and remove the LEA instructions contained in the scope
363 /// from the LEAs Map.
364 void popScope();
365
366 /// If LEA contains Physical Registers then its not a candidate
367 /// for factorizations since physical registers may violate SSA
368 /// semantics of MI.
369 bool containsPhyReg(MachineInstr *MI, unsigned RecLevel);
370
371private:
372 ScopeStackT Stack;
373 LEAMapT LEAs;
374 std::set<MachineInstr *> removedLEAs;
375};
376
377void FactorizeLEAOpt::pushScope(MachineBasicBlock *MBB) {
378 ValueT EmptyMap;
379 ScopeEntryT SE = std::make_pair(MBB, EmptyMap);
380 Stack.push_back(SE);
381 for (auto &MI : *MBB) {
382 if (isLEA(MI))
383 insertLEA(&MI);
384 }
385}
386
387void FactorizeLEAOpt::popScope() {
388 ScopeEntryT &SE = Stack.back();
389 for (auto MapEntry : SE.second) {
390 LEAMapT::iterator Itr = LEAs.find(MapEntry.first);
391 assert((Itr != LEAs.end()) &&
392 "LEAs map must have a node corresponding to ScopeEntry's Key.");
393
394 while (((*Itr).second.size() > MapEntry.second))
395 (*Itr).second.pop_front();
396 // If list goes empty remove entry from LEAs Map.
397 if ((*Itr).second.empty())
398 LEAs.erase(Itr);
399 }
400 Stack.pop_back();
401}
402
403bool FactorizeLEAOpt::containsPhyReg(MachineInstr *MI, unsigned RecLevel) {
404 if (!MI || !RecLevel)
405 return false;
406
407 MachineRegisterInfo *MRI = MI->getRegInfo();
408 for (auto Operand : MI->operands()) {
409 if (!Operand.isReg())
410 continue;
411 if (TargetRegisterInfo::isPhysicalRegister(Operand.getReg()))
412 return true;
413 MachineInstr *OperDefMI = MRI->getVRegDef(Operand.getReg());
414 if (OperDefMI && (MI != OperDefMI) && OperDefMI->isCopyLike() &&
415 containsPhyReg(OperDefMI, RecLevel - 1))
416 return true;
417 }
418 return false;
419}
420
421void FactorizeLEAOpt::insertLEA(MachineInstr *MI) {
422 unsigned lsize;
423 if (containsPhyReg(MI, 2))
424 return;
425
426 MemOpKey Key = getMemOpCSEKey(*MI, 1);
427 ScopeEntryT *TopScope = getTopScope();
428
429 LEAMapT::iterator Itr = LEAs.find(Key);
430 if (Itr == LEAs.end()) {
431 lsize = 0;
432 LEAs[Key].push_front(MI);
433 } else {
434 lsize = (*Itr).second.size();
435 (*Itr).second.push_front(MI);
436 }
437 if (TopScope->second.find(Key) == TopScope->second.end())
438 TopScope->second[Key] = lsize;
439}
440
Alexey Bataev7cf32472015-12-04 10:53:15 +0000441class OptimizeLEAPass : public MachineFunctionPass {
442public:
443 OptimizeLEAPass() : MachineFunctionPass(ID) {}
444
Mehdi Amini117296c2016-10-01 02:56:57 +0000445 StringRef getPassName() const override { return "X86 LEA Optimize"; }
Alexey Bataev7cf32472015-12-04 10:53:15 +0000446
447 /// \brief Loop over all of the basic blocks, replacing address
448 /// calculations in load and store instructions, if it's already
449 /// been calculated by LEA. Also, remove redundant LEAs.
450 bool runOnMachineFunction(MachineFunction &MF) override;
451
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000452 void getAnalysisUsage(AnalysisUsage &AU) const override {
453 AU.setPreservesCFG();
454 MachineFunctionPass::getAnalysisUsage(AU);
455 AU.addRequired<MachineDominatorTree>();
456 }
457
Alexey Bataev7cf32472015-12-04 10:53:15 +0000458private:
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000459 typedef DenseMap<MemOpKey, SmallVector<MachineInstr *, 16>> MemOpMap;
460
Alexey Bataev7cf32472015-12-04 10:53:15 +0000461 /// \brief Returns a distance between two instructions inside one basic block.
462 /// Negative result means, that instructions occur in reverse order.
463 int calcInstrDist(const MachineInstr &First, const MachineInstr &Last);
464
465 /// \brief Choose the best \p LEA instruction from the \p List to replace
466 /// address calculation in \p MI instruction. Return the address displacement
Simon Pilgrim9d15fb32016-11-17 19:03:05 +0000467 /// and the distance between \p MI and the chosen \p BestLEA in
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000468 /// \p AddrDispShift and \p Dist.
Alexey Bataev7cf32472015-12-04 10:53:15 +0000469 bool chooseBestLEA(const SmallVectorImpl<MachineInstr *> &List,
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000470 const MachineInstr &MI, MachineInstr *&BestLEA,
Alexey Bataev7cf32472015-12-04 10:53:15 +0000471 int64_t &AddrDispShift, int &Dist);
472
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000473 /// \brief Returns the difference between addresses' displacements of \p MI1
474 /// and \p MI2. The numbers of the first memory operands for the instructions
475 /// are specified through \p N1 and \p N2.
476 int64_t getAddrDispShift(const MachineInstr &MI1, unsigned N1,
477 const MachineInstr &MI2, unsigned N2) const;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000478
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000479 /// \brief Returns true if the \p Last LEA instruction can be replaced by the
480 /// \p First. The difference between displacements of the addresses calculated
481 /// by these LEAs is returned in \p AddrDispShift. It'll be used for proper
482 /// replacement of the \p Last LEA's uses with the \p First's def register.
483 bool isReplaceable(const MachineInstr &First, const MachineInstr &Last,
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000484 int64_t &AddrDispShift) const;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000485
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000486 /// \brief Find all LEA instructions in the basic block. Also, assign position
487 /// numbers to all instructions in the basic block to speed up calculation of
488 /// distance between them.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000489 void findLEAs(const MachineBasicBlock &MBB, MemOpMap &LEAs);
Alexey Bataev7cf32472015-12-04 10:53:15 +0000490
491 /// \brief Removes redundant address calculations.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000492 bool removeRedundantAddrCalc(MemOpMap &LEAs);
Alexey Bataev7cf32472015-12-04 10:53:15 +0000493
Andrew Ng03e35b62017-04-28 08:44:30 +0000494 /// Replace debug value MI with a new debug value instruction using register
495 /// VReg with an appropriate offset and DIExpression to incorporate the
496 /// address displacement AddrDispShift. Return new debug value instruction.
497 MachineInstr *replaceDebugValue(MachineInstr &MI, unsigned VReg,
498 int64_t AddrDispShift);
499
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000500 /// \brief Removes LEAs which calculate similar addresses.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000501 bool removeRedundantLEAs(MemOpMap &LEAs);
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000502
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000503 /// \brief Visit over basic blocks, collect LEAs in a scoped
504 /// hash map (FactorizeLEAOpt::LEAs) and try to factor them out.
505 bool FactorizeLEAsAllBasicBlocks(MachineFunction &MF);
506
507 bool FactorizeLEAsBasicBlock(MachineDomTreeNode *DN);
508
509 /// \brief Factor out LEAs which share Base,Index,Offset and Segment.
510 bool processBasicBlock(const MachineBasicBlock &MBB);
511
512 /// \brief Try to replace LEA with a lower strength instruction
513 /// to improves latency and throughput.
514 bool strengthReduceLEAs(MemOpMap &LEAs, const MachineBasicBlock &MBB);
515
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000516 DenseMap<const MachineInstr *, unsigned> InstrPos;
517
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000518 FactorizeLEAOpt FactorOpt;
519
520 MachineDominatorTree *DT;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000521 MachineRegisterInfo *MRI;
522 const X86InstrInfo *TII;
523 const X86RegisterInfo *TRI;
524
525 static char ID;
526};
527char OptimizeLEAPass::ID = 0;
528}
529
530FunctionPass *llvm::createX86OptimizeLEAs() { return new OptimizeLEAPass(); }
531
532int OptimizeLEAPass::calcInstrDist(const MachineInstr &First,
533 const MachineInstr &Last) {
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000534 // Both instructions must be in the same basic block and they must be
535 // presented in InstrPos.
536 assert(Last.getParent() == First.getParent() &&
Alexey Bataev7cf32472015-12-04 10:53:15 +0000537 "Instructions are in different basic blocks");
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000538 assert(InstrPos.find(&First) != InstrPos.end() &&
539 InstrPos.find(&Last) != InstrPos.end() &&
540 "Instructions' positions are undefined");
Alexey Bataev7cf32472015-12-04 10:53:15 +0000541
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000542 return InstrPos[&Last] - InstrPos[&First];
Alexey Bataev7cf32472015-12-04 10:53:15 +0000543}
544
545// Find the best LEA instruction in the List to replace address recalculation in
546// MI. Such LEA must meet these requirements:
547// 1) The address calculated by the LEA differs only by the displacement from
548// the address used in MI.
549// 2) The register class of the definition of the LEA is compatible with the
550// register class of the address base register of MI.
551// 3) Displacement of the new memory operand should fit in 1 byte if possible.
552// 4) The LEA should be as close to MI as possible, and prior to it if
553// possible.
554bool OptimizeLEAPass::chooseBestLEA(const SmallVectorImpl<MachineInstr *> &List,
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000555 const MachineInstr &MI,
556 MachineInstr *&BestLEA,
Alexey Bataev7cf32472015-12-04 10:53:15 +0000557 int64_t &AddrDispShift, int &Dist) {
558 const MachineFunction *MF = MI.getParent()->getParent();
559 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000560 int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags) +
Alexey Bataev7cf32472015-12-04 10:53:15 +0000561 X86II::getOperandBias(Desc);
562
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000563 BestLEA = nullptr;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000564
565 // Loop over all LEA instructions.
566 for (auto DefMI : List) {
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000567 // Get new address displacement.
568 int64_t AddrDispShiftTemp = getAddrDispShift(MI, MemOpNo, *DefMI, 1);
Alexey Bataev7cf32472015-12-04 10:53:15 +0000569
570 // Make sure address displacement fits 4 bytes.
571 if (!isInt<32>(AddrDispShiftTemp))
572 continue;
573
574 // Check that LEA def register can be used as MI address base. Some
575 // instructions can use a limited set of registers as address base, for
576 // example MOV8mr_NOREX. We could constrain the register class of the LEA
577 // def to suit MI, however since this case is very rare and hard to
578 // reproduce in a test it's just more reliable to skip the LEA.
579 if (TII->getRegClass(Desc, MemOpNo + X86::AddrBaseReg, TRI, *MF) !=
580 MRI->getRegClass(DefMI->getOperand(0).getReg()))
581 continue;
582
583 // Choose the closest LEA instruction from the list, prior to MI if
584 // possible. Note that we took into account resulting address displacement
585 // as well. Also note that the list is sorted by the order in which the LEAs
586 // occur, so the break condition is pretty simple.
587 int DistTemp = calcInstrDist(*DefMI, MI);
588 assert(DistTemp != 0 &&
589 "The distance between two different instructions cannot be zero");
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000590 if (DistTemp > 0 || BestLEA == nullptr) {
Alexey Bataev7cf32472015-12-04 10:53:15 +0000591 // Do not update return LEA, if the current one provides a displacement
592 // which fits in 1 byte, while the new candidate does not.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000593 if (BestLEA != nullptr && !isInt<8>(AddrDispShiftTemp) &&
Alexey Bataev7cf32472015-12-04 10:53:15 +0000594 isInt<8>(AddrDispShift))
595 continue;
596
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000597 BestLEA = DefMI;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000598 AddrDispShift = AddrDispShiftTemp;
599 Dist = DistTemp;
600 }
601
602 // FIXME: Maybe we should not always stop at the first LEA after MI.
603 if (DistTemp < 0)
604 break;
605 }
606
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000607 return BestLEA != nullptr;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000608}
609
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000610// Get the difference between the addresses' displacements of the two
611// instructions \p MI1 and \p MI2. The numbers of the first memory operands are
612// passed through \p N1 and \p N2.
613int64_t OptimizeLEAPass::getAddrDispShift(const MachineInstr &MI1, unsigned N1,
614 const MachineInstr &MI2,
615 unsigned N2) const {
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000616 const MachineOperand &Op1 = MI1.getOperand(N1 + X86::AddrDisp);
617 const MachineOperand &Op2 = MI2.getOperand(N2 + X86::AddrDisp);
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000618
619 assert(isSimilarDispOp(Op1, Op2) &&
620 "Address displacement operands are not compatible");
621
622 // After the assert above we can be sure that both operands are of the same
623 // valid type and use the same symbol/index/address, thus displacement shift
624 // calculation is rather simple.
625 if (Op1.isJTI())
626 return 0;
627 return Op1.isImm() ? Op1.getImm() - Op2.getImm()
628 : Op1.getOffset() - Op2.getOffset();
Alexey Bataev7cf32472015-12-04 10:53:15 +0000629}
630
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000631// Check that the Last LEA can be replaced by the First LEA. To be so,
632// these requirements must be met:
633// 1) Addresses calculated by LEAs differ only by displacement.
634// 2) Def registers of LEAs belong to the same class.
635// 3) All uses of the Last LEA def register are replaceable, thus the
636// register is used only as address base.
637bool OptimizeLEAPass::isReplaceable(const MachineInstr &First,
638 const MachineInstr &Last,
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000639 int64_t &AddrDispShift) const {
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000640 assert(isLEA(First) && isLEA(Last) &&
641 "The function works only with LEA instructions");
642
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000643 // Make sure that LEA def registers belong to the same class. There may be
644 // instructions (like MOV8mr_NOREX) which allow a limited set of registers to
645 // be used as their operands, so we must be sure that replacing one LEA
646 // with another won't lead to putting a wrong register in the instruction.
647 if (MRI->getRegClass(First.getOperand(0).getReg()) !=
648 MRI->getRegClass(Last.getOperand(0).getReg()))
649 return false;
650
Andrea Di Biagio7937be72017-03-21 11:36:21 +0000651 // Get new address displacement.
652 AddrDispShift = getAddrDispShift(Last, 1, First, 1);
653
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000654 // Loop over all uses of the Last LEA to check that its def register is
655 // used only as address base for memory accesses. If so, it can be
656 // replaced, otherwise - no.
Andrea Di Biagio7937be72017-03-21 11:36:21 +0000657 for (auto &MO : MRI->use_nodbg_operands(Last.getOperand(0).getReg())) {
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000658 MachineInstr &MI = *MO.getParent();
659
660 // Get the number of the first memory operand.
661 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000662 int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags);
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000663
664 // If the use instruction has no memory operand - the LEA is not
665 // replaceable.
666 if (MemOpNo < 0)
667 return false;
668
669 MemOpNo += X86II::getOperandBias(Desc);
670
671 // If the address base of the use instruction is not the LEA def register -
672 // the LEA is not replaceable.
673 if (!isIdenticalOp(MI.getOperand(MemOpNo + X86::AddrBaseReg), MO))
674 return false;
675
676 // If the LEA def register is used as any other operand of the use
677 // instruction - the LEA is not replaceable.
678 for (unsigned i = 0; i < MI.getNumOperands(); i++)
679 if (i != (unsigned)(MemOpNo + X86::AddrBaseReg) &&
680 isIdenticalOp(MI.getOperand(i), MO))
681 return false;
682
683 // Check that the new address displacement will fit 4 bytes.
684 if (MI.getOperand(MemOpNo + X86::AddrDisp).isImm() &&
685 !isInt<32>(MI.getOperand(MemOpNo + X86::AddrDisp).getImm() +
686 AddrDispShift))
687 return false;
688 }
689
690 return true;
691}
692
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000693void OptimizeLEAPass::findLEAs(const MachineBasicBlock &MBB, MemOpMap &LEAs) {
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000694 unsigned Pos = 0;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000695 for (auto &MI : MBB) {
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000696 // Assign the position number to the instruction. Note that we are going to
697 // move some instructions during the optimization however there will never
698 // be a need to move two instructions before any selected instruction. So to
699 // avoid multiple positions' updates during moves we just increase position
700 // counter by two leaving a free space for instructions which will be moved.
701 InstrPos[&MI] = Pos += 2;
702
Alexey Bataev7cf32472015-12-04 10:53:15 +0000703 if (isLEA(MI))
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000704 LEAs[getMemOpKey(MI, 1)].push_back(const_cast<MachineInstr *>(&MI));
Alexey Bataev7cf32472015-12-04 10:53:15 +0000705 }
706}
707
708// Try to find load and store instructions which recalculate addresses already
709// calculated by some LEA and replace their memory operands with its def
710// register.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000711bool OptimizeLEAPass::removeRedundantAddrCalc(MemOpMap &LEAs) {
Alexey Bataev7cf32472015-12-04 10:53:15 +0000712 bool Changed = false;
713
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000714 assert(!LEAs.empty());
715 MachineBasicBlock *MBB = (*LEAs.begin()->second.begin())->getParent();
Alexey Bataev7cf32472015-12-04 10:53:15 +0000716
717 // Process all instructions in basic block.
718 for (auto I = MBB->begin(), E = MBB->end(); I != E;) {
719 MachineInstr &MI = *I++;
Alexey Bataev7cf32472015-12-04 10:53:15 +0000720
721 // Instruction must be load or store.
722 if (!MI.mayLoadOrStore())
723 continue;
724
725 // Get the number of the first memory operand.
726 const MCInstrDesc &Desc = MI.getDesc();
Craig Topper477649a2016-04-28 05:58:46 +0000727 int MemOpNo = X86II::getMemoryOperandNo(Desc.TSFlags);
Alexey Bataev7cf32472015-12-04 10:53:15 +0000728
729 // If instruction has no memory operand - skip it.
730 if (MemOpNo < 0)
731 continue;
732
733 MemOpNo += X86II::getOperandBias(Desc);
734
735 // Get the best LEA instruction to replace address calculation.
736 MachineInstr *DefMI;
737 int64_t AddrDispShift;
738 int Dist;
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000739 if (!chooseBestLEA(LEAs[getMemOpKey(MI, MemOpNo)], MI, DefMI, AddrDispShift,
740 Dist))
Alexey Bataev7cf32472015-12-04 10:53:15 +0000741 continue;
742
743 // If LEA occurs before current instruction, we can freely replace
744 // the instruction. If LEA occurs after, we can lift LEA above the
745 // instruction and this way to be able to replace it. Since LEA and the
746 // instruction have similar memory operands (thus, the same def
747 // instructions for these operands), we can always do that, without
748 // worries of using registers before their defs.
749 if (Dist < 0) {
750 DefMI->removeFromParent();
751 MBB->insert(MachineBasicBlock::iterator(&MI), DefMI);
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000752 InstrPos[DefMI] = InstrPos[&MI] - 1;
753
754 // Make sure the instructions' position numbers are sane.
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000755 assert(((InstrPos[DefMI] == 1 &&
756 MachineBasicBlock::iterator(DefMI) == MBB->begin()) ||
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000757 InstrPos[DefMI] >
Duncan P. N. Exon Smith7b4c18e2016-07-12 03:18:50 +0000758 InstrPos[&*std::prev(MachineBasicBlock::iterator(DefMI))]) &&
Alexey Bataev28f0c5e2016-01-11 11:52:29 +0000759 "Instruction positioning is broken");
Alexey Bataev7cf32472015-12-04 10:53:15 +0000760 }
761
762 // Since we can possibly extend register lifetime, clear kill flags.
763 MRI->clearKillFlags(DefMI->getOperand(0).getReg());
764
765 ++NumSubstLEAs;
766 DEBUG(dbgs() << "OptimizeLEAs: Candidate to replace: "; MI.dump(););
767
768 // Change instruction operands.
769 MI.getOperand(MemOpNo + X86::AddrBaseReg)
770 .ChangeToRegister(DefMI->getOperand(0).getReg(), false);
771 MI.getOperand(MemOpNo + X86::AddrScaleAmt).ChangeToImmediate(1);
772 MI.getOperand(MemOpNo + X86::AddrIndexReg)
773 .ChangeToRegister(X86::NoRegister, false);
774 MI.getOperand(MemOpNo + X86::AddrDisp).ChangeToImmediate(AddrDispShift);
775 MI.getOperand(MemOpNo + X86::AddrSegmentReg)
776 .ChangeToRegister(X86::NoRegister, false);
777
778 DEBUG(dbgs() << "OptimizeLEAs: Replaced by: "; MI.dump(););
779
780 Changed = true;
781 }
782
783 return Changed;
784}
785
Andrew Ng03e35b62017-04-28 08:44:30 +0000786MachineInstr *OptimizeLEAPass::replaceDebugValue(MachineInstr &MI,
787 unsigned VReg,
788 int64_t AddrDispShift) {
789 DIExpression *Expr = const_cast<DIExpression *>(MI.getDebugExpression());
790
Adrian Prantl109b2362017-04-28 17:51:05 +0000791 if (AddrDispShift != 0)
792 Expr = DIExpression::prepend(Expr, DIExpression::NoDeref, AddrDispShift,
793 DIExpression::WithStackValue);
Andrew Ng03e35b62017-04-28 08:44:30 +0000794
795 // Replace DBG_VALUE instruction with modified version.
796 MachineBasicBlock *MBB = MI.getParent();
797 DebugLoc DL = MI.getDebugLoc();
798 bool IsIndirect = MI.isIndirectDebugValue();
Andrew Ng03e35b62017-04-28 08:44:30 +0000799 const MDNode *Var = MI.getDebugVariable();
Adrian Prantl8b9bb532017-07-28 23:00:45 +0000800 if (IsIndirect)
801 assert(MI.getOperand(1).getImm() == 0 && "DBG_VALUE with nonzero offset");
Andrew Ng03e35b62017-04-28 08:44:30 +0000802 return BuildMI(*MBB, MBB->erase(&MI), DL, TII->get(TargetOpcode::DBG_VALUE),
Adrian Prantl8b9bb532017-07-28 23:00:45 +0000803 IsIndirect, VReg, Var, Expr);
Andrew Ng03e35b62017-04-28 08:44:30 +0000804}
805
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000806// Try to find similar LEAs in the list and replace one with another.
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000807bool OptimizeLEAPass::removeRedundantLEAs(MemOpMap &LEAs) {
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000808 bool Changed = false;
809
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000810 // Loop over all entries in the table.
811 for (auto &E : LEAs) {
812 auto &List = E.second;
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000813
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000814 // Loop over all LEA pairs.
815 auto I1 = List.begin();
816 while (I1 != List.end()) {
817 MachineInstr &First = **I1;
818 auto I2 = std::next(I1);
819 while (I2 != List.end()) {
820 MachineInstr &Last = **I2;
821 int64_t AddrDispShift;
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000822
Simon Pilgrim9d15fb32016-11-17 19:03:05 +0000823 // LEAs should be in occurrence order in the list, so we can freely
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000824 // replace later LEAs with earlier ones.
825 assert(calcInstrDist(First, Last) > 0 &&
Simon Pilgrim9d15fb32016-11-17 19:03:05 +0000826 "LEAs must be in occurrence order in the list");
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000827
828 // Check that the Last LEA instruction can be replaced by the First.
829 if (!isReplaceable(First, Last, AddrDispShift)) {
830 ++I2;
831 continue;
832 }
833
834 // Loop over all uses of the Last LEA and update their operands. Note
835 // that the correctness of this has already been checked in the
836 // isReplaceable function.
Andrew Ng03e35b62017-04-28 08:44:30 +0000837 unsigned FirstVReg = First.getOperand(0).getReg();
Andrea Di Biagio7937be72017-03-21 11:36:21 +0000838 unsigned LastVReg = Last.getOperand(0).getReg();
Andrew Ng03e35b62017-04-28 08:44:30 +0000839 for (auto UI = MRI->use_begin(LastVReg), UE = MRI->use_end();
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000840 UI != UE;) {
841 MachineOperand &MO = *UI++;
842 MachineInstr &MI = *MO.getParent();
843
Andrew Ng03e35b62017-04-28 08:44:30 +0000844 if (MI.isDebugValue()) {
845 // Replace DBG_VALUE instruction with modified version using the
846 // register from the replacing LEA and the address displacement
847 // between the LEA instructions.
848 replaceDebugValue(MI, FirstVReg, AddrDispShift);
849 continue;
850 }
851
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000852 // Get the number of the first memory operand.
853 const MCInstrDesc &Desc = MI.getDesc();
854 int MemOpNo =
Craig Topper477649a2016-04-28 05:58:46 +0000855 X86II::getMemoryOperandNo(Desc.TSFlags) +
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000856 X86II::getOperandBias(Desc);
857
858 // Update address base.
Andrew Ng03e35b62017-04-28 08:44:30 +0000859 MO.setReg(FirstVReg);
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000860
861 // Update address disp.
Andrey Turetskiy0babd262016-02-20 10:58:28 +0000862 MachineOperand &Op = MI.getOperand(MemOpNo + X86::AddrDisp);
863 if (Op.isImm())
864 Op.setImm(Op.getImm() + AddrDispShift);
865 else if (!Op.isJTI())
866 Op.setOffset(Op.getOffset() + AddrDispShift);
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000867 }
868
869 // Since we can possibly extend register lifetime, clear kill flags.
Andrew Ng03e35b62017-04-28 08:44:30 +0000870 MRI->clearKillFlags(FirstVReg);
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000871
872 ++NumRedundantLEAs;
873 DEBUG(dbgs() << "OptimizeLEAs: Remove redundant LEA: "; Last.dump(););
874
875 // By this moment, all of the Last LEA's uses must be replaced. So we
876 // can freely remove it.
Andrea Di Biagio7937be72017-03-21 11:36:21 +0000877 assert(MRI->use_empty(LastVReg) &&
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000878 "The LEA's def register must have no uses");
879 Last.eraseFromParent();
880
881 // Erase removed LEA from the list.
882 I2 = List.erase(I2);
883
884 Changed = true;
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000885 }
Andrey Turetskiybca0f992016-02-04 08:57:03 +0000886 ++I1;
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000887 }
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +0000888 }
889
890 return Changed;
891}
892
Jatin Bhateja3c29bac2017-10-04 09:02:10 +0000893static inline int getADDrrFromLEA(int LEAOpcode) {
894 switch (LEAOpcode) {
895 default:
896 llvm_unreachable("Unexpected LEA instruction");
897 case X86::LEA16r:
898 return X86::ADD16rr;
899 case X86::LEA32r:
900 return X86::ADD32rr;
901 case X86::LEA64_32r:
902 case X86::LEA64r:
903 return X86::ADD64rr;
904 }
905}
906
907bool OptimizeLEAPass::strengthReduceLEAs(MemOpMap &LEAs,
908 const MachineBasicBlock &BB) {
909 bool Changed = false;
910
911 // Loop over all entries in the table.
912 for (auto &E : LEAs) {
913 auto &List = E.second;
914
915 // Loop over all LEA pairs.
916 for (auto I1 = List.begin(); I1 != List.end(); I1++) {
917 MachineInstrBuilder NewMI;
918 MachineInstr &First = **I1;
919 MachineOperand &Res = First.getOperand(0);
920 MachineOperand &Base = First.getOperand(1);
921 MachineOperand &Scale = First.getOperand(2);
922 MachineOperand &Index = First.getOperand(3);
923 MachineOperand &Offset = First.getOperand(4);
924
925 const MCInstrDesc &ADDrr = TII->get(getADDrrFromLEA(First.getOpcode()));
926 const DebugLoc DL = First.getDebugLoc();
927
928 if (!Base.isReg() || !Index.isReg())
929 continue;
930 if (TargetRegisterInfo::isPhysicalRegister(Res.getReg()) ||
931 TargetRegisterInfo::isPhysicalRegister(Base.getReg()) ||
932 TargetRegisterInfo::isPhysicalRegister(Index.getReg()))
933 continue;
934
935 MachineBasicBlock &MBB = *(const_cast<MachineBasicBlock *>(&BB));
936 if (Scale.isImm() && Scale.getImm() == 1) {
937 // R = B + I
938 if (Offset.isImm() && !Offset.getImm()) {
939 NewMI = BuildMI(MBB, &First, DL, ADDrr)
940 .addDef(Res.getReg())
941 .addUse(Base.getReg())
942 .addUse(Index.getReg());
943 Changed = NewMI.getInstr() != nullptr;
944 First.eraseFromParent();
945 }
946 }
947 }
948 }
949 return Changed;
950}
951
952bool OptimizeLEAPass::processBasicBlock(const MachineBasicBlock &MBB) {
953 bool cseDone = false;
954
955 // Legal scale value (1,2,4 & 8) vector.
956 int LegalScale[9] = {0, 1, 1, 0, 1, 0, 0, 0, 1};
957
958 auto CompareFn = [](const MachineInstr *Arg1,
959 const MachineInstr *Arg2) -> bool {
960 if (Arg1->getOperand(2).getImm() < Arg2->getOperand(2).getImm())
961 return false;
962 return true;
963 };
964
965 // Loop over all entries in the table.
966 for (auto &E : FactorOpt.getLEAMap()) {
967 auto &List = E.second;
968 if (List.size() > 1)
969 List.sort(CompareFn);
970
971 // Loop over all LEA pairs.
972 for (auto Iter1 = List.begin(); Iter1 != List.end(); Iter1++) {
973 for (auto Iter2 = std::next(Iter1); Iter2 != List.end(); Iter2++) {
974 MachineInstr &LI1 = **Iter1;
975 MachineInstr &LI2 = **Iter2;
976
977 if (!DT->dominates(&LI2, &LI1))
978 continue;
979
980 int Scale1 = LI1.getOperand(2).getImm();
981 int Scale2 = LI2.getOperand(2).getImm();
982 assert(LI2.getOperand(0).isReg() && "Result is a VirtualReg");
983 DebugLoc DL = LI1.getDebugLoc();
984
985 if (FactorOpt.checkIfScheduledForRemoval(&LI1))
986 continue;
987
988 int Factor = Scale1 - Scale2;
989 if (Factor > 0 && LegalScale[Factor]) {
990 DEBUG(dbgs() << "CSE LEAs: Candidate to replace: "; LI1.dump(););
991 MachineInstrBuilder NewMI =
992 BuildMI(*(const_cast<MachineBasicBlock *>(&MBB)), &LI1, DL,
993 TII->get(LI1.getOpcode()))
994 .addDef(LI1.getOperand(0).getReg()) // Dst = Dst of LI1.
995 .addUse(LI2.getOperand(0).getReg()) // Base = Dst of LI2.
996 .addImm(Factor) // Scale = Diff b/w scales.
997 .addUse(LI1.getOperand(3).getReg()) // Index = Index of LI1.
998 .addImm(0) // Disp = 0
999 .addUse(
1000 LI1.getOperand(5).getReg()); // Segment = Segmant of LI1.
1001
1002 cseDone = NewMI.getInstr() != nullptr;
1003
1004 LI1.getOperand(0).setIsDef(false);
1005
1006 /// Lazy removal shall ensure that replaced LEA remains
1007 /// till we finish processing all the basic block. This shall
1008 /// provide opportunity for further factorization based on
1009 /// the replaced LEA which will be legal since it has same
1010 /// destination as newly formed LEA.
1011 FactorOpt.addForLazyRemoval(&LI1);
1012
1013 NumFactoredLEAs++;
1014 DEBUG(dbgs() << "CSE LEAs: Replaced by: "; NewMI->dump(););
1015 }
1016 }
1017 }
1018 }
1019 return cseDone;
1020}
1021
1022bool OptimizeLEAPass::FactorizeLEAsBasicBlock(MachineDomTreeNode *DN) {
1023 bool Changed = false;
1024 using StackT = SmallVector<MachineDomTreeNode* , 16>;
1025 using ProcessedMapT = DenseMap<MachineDomTreeNode* , bool>;
1026
1027 StackT WorkList;
1028 ProcessedMapT ProcessesMap;
1029
1030 WorkList.push_back(DN);
1031 while(!WorkList.empty()) {
1032 MachineDomTreeNode * MDN = WorkList.back();
1033 if (ProcessesMap.find(MDN) == ProcessesMap.end()) {
1034 ProcessesMap[MDN] = true;
1035 FactorOpt.pushScope(MDN->getBlock());
1036 Changed |= processBasicBlock(*MDN->getBlock());
1037 for (auto Child : MDN->getChildren())
1038 WorkList.push_back(Child);
1039 }
1040 MachineDomTreeNode *TDM = WorkList.back();
1041 if (MDN->getLevel() == TDM->getLevel()) {
1042 FactorOpt.popScope();
1043 WorkList.pop_back();
1044 }
1045 }
1046 return Changed;
1047}
1048
1049bool OptimizeLEAPass::FactorizeLEAsAllBasicBlocks(MachineFunction &MF) {
1050 bool Changed = FactorizeLEAsBasicBlock(DT->getRootNode());
1051 FactorOpt.performCleanup();
1052 return Changed;
1053}
1054
Alexey Bataev7cf32472015-12-04 10:53:15 +00001055bool OptimizeLEAPass::runOnMachineFunction(MachineFunction &MF) {
1056 bool Changed = false;
Alexey Bataev7cf32472015-12-04 10:53:15 +00001057
Andrey Turetskiy45b22a42016-05-19 10:18:29 +00001058 if (DisableX86LEAOpt || skipFunction(*MF.getFunction()))
Alexey Bataev7cf32472015-12-04 10:53:15 +00001059 return false;
1060
1061 MRI = &MF.getRegInfo();
1062 TII = MF.getSubtarget<X86Subtarget>().getInstrInfo();
1063 TRI = MF.getSubtarget<X86Subtarget>().getRegisterInfo();
Jatin Bhateja3c29bac2017-10-04 09:02:10 +00001064 DT = &getAnalysis<MachineDominatorTree>();
1065
1066 // Attempt factorizing LEAs.
1067 Changed |= FactorizeLEAsAllBasicBlocks(MF);
Alexey Bataev7cf32472015-12-04 10:53:15 +00001068
1069 // Process all basic blocks.
1070 for (auto &MBB : MF) {
Andrey Turetskiybca0f992016-02-04 08:57:03 +00001071 MemOpMap LEAs;
Alexey Bataev28f0c5e2016-01-11 11:52:29 +00001072 InstrPos.clear();
Alexey Bataev7cf32472015-12-04 10:53:15 +00001073
1074 // Find all LEA instructions in basic block.
1075 findLEAs(MBB, LEAs);
1076
1077 // If current basic block has no LEAs, move on to the next one.
1078 if (LEAs.empty())
1079 continue;
1080
Andrey Turetskiy45b22a42016-05-19 10:18:29 +00001081 // Remove redundant LEA instructions.
1082 Changed |= removeRedundantLEAs(LEAs);
Andrey Turetskiy1ce2c992016-01-13 11:30:44 +00001083
Jatin Bhateja3c29bac2017-10-04 09:02:10 +00001084 // Strength reduce LEA instructions.
1085 Changed |= strengthReduceLEAs(LEAs, MBB);
1086
Andrey Turetskiy45b22a42016-05-19 10:18:29 +00001087 // Remove redundant address calculations. Do it only for -Os/-Oz since only
1088 // a code size gain is expected from this part of the pass.
1089 if (MF.getFunction()->optForSize())
1090 Changed |= removeRedundantAddrCalc(LEAs);
Alexey Bataev7cf32472015-12-04 10:53:15 +00001091 }
1092
1093 return Changed;
1094}