Skip to content

Commit 707fc2e

Browse files
committed
Revert rG528bc10e95d5f9d6a338f9bab5e91d7265d1cf05 : "[X86FixupLEAs] Transform the sequence LEA/SUB to SUB/SUB"
Reports on D101970 indicate this is causing failures on multi-stage compiles.
1 parent d131081 commit 707fc2e

File tree

8 files changed

+76
-308
lines changed

8 files changed

+76
-308
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,6 @@ class TargetInstrInfo : public MCInstrInfo {
459459
unsigned &SrcOpIdx1,
460460
unsigned &SrcOpIdx2) const;
461461

462-
/// Returns true if the target has a preference on the operands order of
463-
/// the given machine instruction. And specify if \p Commute is required to
464-
/// get the desired operands order.
465-
virtual bool hasCommutePreference(MachineInstr &MI, bool &Commute) const {
466-
return false;
467-
}
468-
469462
/// A pair composed of a register and a sub-register index.
470463
/// Used to give some type checking when modeling Reg:SubReg.
471464
struct RegSubRegPair {

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,6 @@ bool TwoAddressInstructionPass::isProfitableToCommute(Register RegA,
527527
if (isRevCopyChain(RegB, RegA, MaxDataFlowEdge))
528528
return false;
529529

530-
// Look for other target specific commute preference.
531-
bool Commute;
532-
if (TII->hasCommutePreference(*MI, Commute))
533-
return Commute;
534-
535530
// Since there are no intervening uses for both registers, then commute
536531
// if the def of RegC is closer. Its live interval is shorter.
537532
return LastDefB && LastDefC && LastDefC > LastDefB;

llvm/lib/Target/X86/X86FixupLEAs.cpp

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,6 @@ class FixupLEAPass : public MachineFunctionPass {
7979
MachineBasicBlock &MBB, bool OptIncDec,
8080
bool UseLEAForSP) const;
8181

82-
/// Look for and transform the sequence
83-
/// lea (reg1, reg2), reg3
84-
/// sub reg3, reg4
85-
/// to
86-
/// sub reg1, reg4
87-
/// sub reg2, reg4
88-
/// It can also optimize the sequence lea/add similarly.
89-
bool optLEAALU(MachineBasicBlock::iterator &I, MachineBasicBlock &MBB) const;
90-
91-
/// Step forwards in MBB, looking for an ADD/SUB instruction which uses
92-
/// the dest register of LEA instruction I.
93-
MachineBasicBlock::iterator searchALUInst(MachineBasicBlock::iterator &I,
94-
MachineBasicBlock &MBB) const;
95-
96-
/// Check instructions between LeaI and AluI (exclusively).
97-
/// Set BaseIndexDef to true if base or index register from LeaI is defined.
98-
/// Set AluDestRef to true if the dest register of AluI is used or defined.
99-
void checkRegUsage(MachineBasicBlock::iterator &LeaI,
100-
MachineBasicBlock::iterator &AluI, bool &BaseIndexDef,
101-
bool &AluDestRef) const;
102-
10382
/// Determine if an instruction references a machine register
10483
/// and, if so, whether it reads or writes the register.
10584
RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I);
@@ -359,18 +338,6 @@ static inline unsigned getADDrrFromLEA(unsigned LEAOpcode) {
359338
}
360339
}
361340

362-
static inline unsigned getSUBrrFromLEA(unsigned LEAOpcode) {
363-
switch (LEAOpcode) {
364-
default:
365-
llvm_unreachable("Unexpected LEA instruction");
366-
case X86::LEA32r:
367-
case X86::LEA64_32r:
368-
return X86::SUB32rr;
369-
case X86::LEA64r:
370-
return X86::SUB64rr;
371-
}
372-
}
373-
374341
static inline unsigned getADDriFromLEA(unsigned LEAOpcode,
375342
const MachineOperand &Offset) {
376343
bool IsInt8 = Offset.isImm() && isInt<8>(Offset.getImm());
@@ -397,133 +364,6 @@ static inline unsigned getINCDECFromLEA(unsigned LEAOpcode, bool IsINC) {
397364
}
398365
}
399366

400-
MachineBasicBlock::iterator
401-
FixupLEAPass::searchALUInst(MachineBasicBlock::iterator &I,
402-
MachineBasicBlock &MBB) const {
403-
const int InstrDistanceThreshold = 5;
404-
int InstrDistance = 1;
405-
MachineBasicBlock::iterator CurInst = std::next(I);
406-
407-
unsigned LEAOpcode = I->getOpcode();
408-
unsigned AddOpcode = getADDrrFromLEA(LEAOpcode);
409-
unsigned SubOpcode = getSUBrrFromLEA(LEAOpcode);
410-
Register DestReg = I->getOperand(0).getReg();
411-
412-
while (CurInst != MBB.end()) {
413-
if (CurInst->isCall() || CurInst->isInlineAsm())
414-
break;
415-
if (InstrDistance > InstrDistanceThreshold)
416-
break;
417-
418-
// Check if the lea dest register is used in an add/sub instruction only.
419-
for (unsigned I = 0, E = CurInst->getNumOperands(); I != E; ++I) {
420-
MachineOperand &Opnd = CurInst->getOperand(I);
421-
if (Opnd.isReg() && Opnd.getReg() == DestReg) {
422-
if (Opnd.isDef() || !Opnd.isKill())
423-
return MachineBasicBlock::iterator();
424-
425-
unsigned AluOpcode = CurInst->getOpcode();
426-
if (AluOpcode != AddOpcode && AluOpcode != SubOpcode)
427-
return MachineBasicBlock::iterator();
428-
429-
MachineOperand &Opnd2 = CurInst->getOperand(3 - I);
430-
MachineOperand AluDest = CurInst->getOperand(0);
431-
if (Opnd2.getReg() != AluDest.getReg())
432-
return MachineBasicBlock::iterator();
433-
434-
return CurInst;
435-
}
436-
}
437-
438-
InstrDistance++;
439-
++CurInst;
440-
}
441-
return MachineBasicBlock::iterator();
442-
}
443-
444-
void FixupLEAPass::checkRegUsage(MachineBasicBlock::iterator &LeaI,
445-
MachineBasicBlock::iterator &AluI,
446-
bool &BaseIndexDef, bool &AluDestRef) const {
447-
BaseIndexDef = AluDestRef = false;
448-
Register BaseReg = LeaI->getOperand(1 + X86::AddrBaseReg).getReg();
449-
Register IndexReg = LeaI->getOperand(1 + X86::AddrIndexReg).getReg();
450-
Register AluDestReg = AluI->getOperand(0).getReg();
451-
452-
MachineBasicBlock::iterator CurInst = std::next(LeaI);
453-
while (CurInst != AluI) {
454-
for (unsigned I = 0, E = CurInst->getNumOperands(); I != E; ++I) {
455-
MachineOperand &Opnd = CurInst->getOperand(I);
456-
if (!Opnd.isReg())
457-
continue;
458-
Register Reg = Opnd.getReg();
459-
if (TRI->regsOverlap(Reg, AluDestReg))
460-
AluDestRef = true;
461-
if (Opnd.isDef() &&
462-
(TRI->regsOverlap(Reg, BaseReg) || TRI->regsOverlap(Reg, IndexReg))) {
463-
BaseIndexDef = true;
464-
}
465-
}
466-
++CurInst;
467-
}
468-
}
469-
470-
bool FixupLEAPass::optLEAALU(MachineBasicBlock::iterator &I,
471-
MachineBasicBlock &MBB) const {
472-
// Look for an add/sub instruction which uses the result of lea.
473-
MachineBasicBlock::iterator AluI = searchALUInst(I, MBB);
474-
if (AluI == MachineBasicBlock::iterator())
475-
return false;
476-
477-
// Check if there are any related register usage between lea and alu.
478-
bool BaseIndexDef, AluDestRef;
479-
checkRegUsage(I, AluI, BaseIndexDef, AluDestRef);
480-
481-
MachineBasicBlock::iterator InsertPos = AluI;
482-
if (BaseIndexDef) {
483-
if (AluDestRef)
484-
return false;
485-
MachineBasicBlock::iterator AfterAluI = std::next(AluI);
486-
if (MBB.computeRegisterLiveness(TRI, X86::EFLAGS, AfterAluI) !=
487-
MachineBasicBlock::LQR_Dead)
488-
return false;
489-
490-
InsertPos = I;
491-
}
492-
493-
// Check if there are same registers.
494-
Register AluDestReg = AluI->getOperand(0).getReg();
495-
Register BaseReg = I->getOperand(1 + X86::AddrBaseReg).getReg();
496-
Register IndexReg = I->getOperand(1 + X86::AddrIndexReg).getReg();
497-
if (I->getOpcode() == X86::LEA64_32r) {
498-
BaseReg = TRI->getSubReg(BaseReg, X86::sub_32bit);
499-
IndexReg = TRI->getSubReg(IndexReg, X86::sub_32bit);
500-
}
501-
if (AluDestReg == IndexReg) {
502-
if (BaseReg == IndexReg)
503-
return false;
504-
std::swap(BaseReg, IndexReg);
505-
}
506-
507-
// Now it's safe to change instructions.
508-
MachineInstr *NewMI1, *NewMI2;
509-
unsigned NewOpcode = AluI->getOpcode();
510-
NewMI1 = BuildMI(MBB, InsertPos, AluI->getDebugLoc(), TII->get(NewOpcode),
511-
AluDestReg)
512-
.addReg(AluDestReg)
513-
.addReg(BaseReg);
514-
NewMI2 = BuildMI(MBB, InsertPos, AluI->getDebugLoc(), TII->get(NewOpcode),
515-
AluDestReg)
516-
.addReg(AluDestReg)
517-
.addReg(IndexReg);
518-
519-
MBB.getParent()->substituteDebugValuesForInst(*AluI, *NewMI1, 1);
520-
MBB.getParent()->substituteDebugValuesForInst(*AluI, *NewMI2, 1);
521-
MBB.erase(I);
522-
MBB.erase(AluI);
523-
I = NewMI1;
524-
return true;
525-
}
526-
527367
bool FixupLEAPass::optTwoAddrLEA(MachineBasicBlock::iterator &I,
528368
MachineBasicBlock &MBB, bool OptIncDec,
529369
bool UseLEAForSP) const {
@@ -558,7 +398,6 @@ bool FixupLEAPass::optTwoAddrLEA(MachineBasicBlock::iterator &I,
558398

559399
MachineInstr *NewMI = nullptr;
560400

561-
// Case 1.
562401
// Look for lea(%reg1, %reg2), %reg1 or lea(%reg2, %reg1), %reg1
563402
// which can be turned into add %reg2, %reg1
564403
if (BaseReg != 0 && IndexReg != 0 && Disp.getImm() == 0 &&
@@ -578,7 +417,6 @@ bool FixupLEAPass::optTwoAddrLEA(MachineBasicBlock::iterator &I,
578417
.addReg(BaseReg).addReg(IndexReg);
579418
}
580419
} else if (DestReg == BaseReg && IndexReg == 0) {
581-
// Case 2.
582420
// This is an LEA with only a base register and a displacement,
583421
// We can use ADDri or INC/DEC.
584422

@@ -609,12 +447,6 @@ bool FixupLEAPass::optTwoAddrLEA(MachineBasicBlock::iterator &I,
609447
.addReg(BaseReg).addImm(Disp.getImm());
610448
}
611449
}
612-
} else if (BaseReg != 0 && IndexReg != 0 && Disp.getImm() == 0) {
613-
// Case 3.
614-
// Look for and transform the sequence
615-
// lea (reg1, reg2), reg3
616-
// sub reg3, reg4
617-
return optLEAALU(I, MBB);
618450
} else
619451
return false;
620452

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,60 +2670,6 @@ bool X86InstrInfo::findCommutedOpIndices(const MachineInstr &MI,
26702670
return false;
26712671
}
26722672

2673-
static bool isConvertibleLEA(MachineInstr *MI) {
2674-
unsigned Opcode = MI->getOpcode();
2675-
if (Opcode != X86::LEA32r && Opcode != X86::LEA64r &&
2676-
Opcode != X86::LEA64_32r)
2677-
return false;
2678-
2679-
const MachineOperand &Scale = MI->getOperand(1 + X86::AddrScaleAmt);
2680-
const MachineOperand &Disp = MI->getOperand(1 + X86::AddrDisp);
2681-
const MachineOperand &Segment = MI->getOperand(1 + X86::AddrSegmentReg);
2682-
2683-
if (Segment.getReg() != 0 || !Disp.isImm() || Disp.getImm() != 0 ||
2684-
Scale.getImm() > 1)
2685-
return false;
2686-
2687-
return true;
2688-
}
2689-
2690-
bool X86InstrInfo::hasCommutePreference(MachineInstr &MI, bool &Commute) const {
2691-
// Currently we're interested in following sequence only.
2692-
// r3 = lea r1, r2
2693-
// r5 = add r3, r4
2694-
// Both r3 and r4 are killed in add, we hope the add instruction has the
2695-
// operand order
2696-
// r5 = add r4, r3
2697-
// So later in X86FixupLEAs the lea instruction can be rewritten as add.
2698-
unsigned Opcode = MI.getOpcode();
2699-
if (Opcode != X86::ADD32rr && Opcode != X86::ADD64rr)
2700-
return false;
2701-
2702-
const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
2703-
Register Reg1 = MI.getOperand(1).getReg();
2704-
Register Reg2 = MI.getOperand(2).getReg();
2705-
2706-
// Check if Reg1 comes from LEA in the same MBB.
2707-
if (MachineOperand *Op = MRI.getOneDef(Reg1)) {
2708-
MachineInstr *Inst = Op->getParent();
2709-
if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) {
2710-
Commute = true;
2711-
return true;
2712-
}
2713-
}
2714-
2715-
// Check if Reg2 comes from LEA in the same MBB.
2716-
if (MachineOperand *Op = MRI.getOneDef(Reg2)) {
2717-
MachineInstr *Inst = Op->getParent();
2718-
if (isConvertibleLEA(Inst) && Inst->getParent() == MI.getParent()) {
2719-
Commute = false;
2720-
return true;
2721-
}
2722-
}
2723-
2724-
return false;
2725-
}
2726-
27272673
X86::CondCode X86::getCondFromBranch(const MachineInstr &MI) {
27282674
switch (MI.getOpcode()) {
27292675
default: return X86::COND_INVALID;

llvm/lib/Target/X86/X86InstrInfo.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,6 @@ class X86InstrInfo final : public X86GenInstrInfo {
284284
bool findCommutedOpIndices(const MachineInstr &MI, unsigned &SrcOpIdx1,
285285
unsigned &SrcOpIdx2) const override;
286286

287-
/// Returns true if we have preference on the operands order in MI, the
288-
/// commute decision is returned in Commute.
289-
bool hasCommutePreference(MachineInstr &MI, bool &Commute) const override;
290-
291287
/// Returns an adjusted FMA opcode that must be used in FMA instruction that
292288
/// performs the same computations as the given \p MI but which has the
293289
/// operands \p SrcOpIdx1 and \p SrcOpIdx2 commuted.

0 commit comments

Comments
 (0)