Skip to content

Commit 2996f30

Browse files
mingmingl-llvmmemfrob
authored andcommitted
Revert "Enhance peephole optimization."
This reverts commit d84ca05ef7f897fdd51900ea07e3c5344632130a. Will revert, update commit message and re-commit.
1 parent ff2a895 commit 2996f30

File tree

2 files changed

+0
-311
lines changed

2 files changed

+0
-311
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525
#include "llvm/CodeGen/MachineConstantPool.h"
2626
#include "llvm/CodeGen/MachineDominators.h"
2727
#include "llvm/CodeGen/MachineFrameInfo.h"
28-
#include "llvm/CodeGen/MachineInstr.h"
2928
#include "llvm/CodeGen/MachineInstrBuilder.h"
3029
#include "llvm/CodeGen/MachineModuleInfo.h"
31-
#include "llvm/CodeGen/MachineOperand.h"
3230
#include "llvm/CodeGen/MachineRegisterInfo.h"
3331
#include "llvm/CodeGen/StackMaps.h"
3432
#include "llvm/IR/DebugInfoMetadata.h"
3533
#include "llvm/IR/DerivedTypes.h"
3634
#include "llvm/IR/Function.h"
37-
#include "llvm/IR/InstrTypes.h"
3835
#include "llvm/MC/MCAsmInfo.h"
3936
#include "llvm/MC/MCExpr.h"
4037
#include "llvm/MC/MCInst.h"
@@ -967,101 +964,6 @@ inline static bool isTruncatedShiftCountForLEA(unsigned ShAmt) {
967964
return ShAmt < 4 && ShAmt > 0;
968965
}
969966

970-
static bool findRedundantFlagInstr(MachineInstr &CmpInstr,
971-
MachineInstr &CmpValDefInstr,
972-
const MachineRegisterInfo *MRI,
973-
MachineInstr **AndInstr,
974-
const TargetRegisterInfo *TRI,
975-
bool &NoSignFlag, bool &ClearsOverflowFlag) {
976-
if (CmpValDefInstr.getOpcode() != X86::SUBREG_TO_REG)
977-
return false;
978-
979-
if (CmpInstr.getOpcode() != X86::TEST64rr)
980-
return false;
981-
982-
// CmpInstr is a TEST64rr instruction, and `X86InstrInfo::analyzeCompare`
983-
// guarantees that it's analyzable only if two registers are identical.
984-
assert(
985-
(CmpInstr.getOperand(0).getReg() == CmpInstr.getOperand(1).getReg()) &&
986-
"CmpInstr is an analyzable TEST64rr, and `X86InstrInfo::analyzeCompare` "
987-
"requires two reg operands are the same.");
988-
989-
// Caller (`X86InstrInfo::optimizeCompareInstr`) guarantees that
990-
// `CmpValDefInstr` defines the value that's used by `CmpInstr`; in this case
991-
// if `CmpValDefInstr` sets the EFLAGS, it is likely that `CmpInstr` is
992-
// redundant.
993-
assert(
994-
(MRI->getVRegDef(CmpInstr.getOperand(0).getReg()) == &CmpValDefInstr) &&
995-
"Caller guarantees that TEST64rr is a user of SUBREG_TO_REG.");
996-
997-
// As seen in X86 td files, CmpValDefInstr.getOperand(1).getImm() is typically
998-
// 0.
999-
if (CmpValDefInstr.getOperand(1).getImm() != 0)
1000-
return false;
1001-
1002-
// As seen in X86 td files, CmpValDefInstr.getOperand(3) is typically
1003-
// sub_32bit or sub_xmm.
1004-
if (CmpValDefInstr.getOperand(3).getImm() != X86::sub_32bit)
1005-
return false;
1006-
1007-
MachineInstr *VregDefInstr =
1008-
MRI->getVRegDef(CmpValDefInstr.getOperand(2).getReg());
1009-
1010-
assert(VregDefInstr && "Must have a definition (SSA)");
1011-
1012-
// Requires `CmpValDefInstr` and `VregDefInstr` are from the same MBB
1013-
// to simplify the subsequent analysis.
1014-
//
1015-
// FIXME: If `VregDefInstr->getParent()` is the only predecessor of
1016-
// `CmpValDefInstr.getParent()`, this could be handled.
1017-
if (VregDefInstr->getParent() != CmpValDefInstr.getParent())
1018-
return false;
1019-
1020-
if (X86::isAND(VregDefInstr->getOpcode())) {
1021-
// Get a sequence of instructions like
1022-
// %reg = and* ... // Set EFLAGS
1023-
// ... // EFLAGS not changed
1024-
// %extended_reg = subreg_to_reg 0, %reg, %subreg.sub_32bit
1025-
// test64rr %extended_reg, %extended_reg, implicit-def $eflags
1026-
//
1027-
// If subsequent readers use a subset of bits that don't change
1028-
// after `and*` instructions, it's likely that the test64rr could
1029-
// be optimized away.
1030-
for (const MachineInstr &Instr :
1031-
make_range(std::next(MachineBasicBlock::iterator(VregDefInstr)),
1032-
MachineBasicBlock::iterator(CmpValDefInstr))) {
1033-
// There are instructions between 'VregDefInstr' and
1034-
// 'CmpValDefInstr' that modifies EFLAGS.
1035-
if (Instr.modifiesRegister(X86::EFLAGS, TRI))
1036-
return false;
1037-
}
1038-
1039-
*AndInstr = VregDefInstr;
1040-
1041-
// AND instruction will essentially update SF and clear OF, so
1042-
// NoSignFlag should be false in the sense that SF is modified by `AND`.
1043-
//
1044-
// However, the implementation artifically sets `NoSignFlag` to true
1045-
// to poison the SF bit; that is to say, if SF is looked at later, the
1046-
// optimization (to erase TEST64rr) will be disabled.
1047-
//
1048-
// The reason to poison SF bit is that SF bit value could be different
1049-
// in the `AND` and `TEST` operation; signed bit is not known for `AND`,
1050-
// and is known to be 0 as a result of `TEST64rr`.
1051-
//
1052-
// FIXME: As opposed to poisoning the SF bit direclty, consider peeking into
1053-
// the AND instruction and using the static information to guide peephole optimization if possible.
1054-
// For example, it's possible to fold a conditional move into a copy
1055-
// if the relevant EFLAG bits could be deduced from an immediate operand of and operation.
1056-
//
1057-
NoSignFlag = true;
1058-
// ClearsOverflowFlag is true for AND operation (no surprise).
1059-
ClearsOverflowFlag = true;
1060-
return true;
1061-
}
1062-
return false;
1063-
}
1064-
1065967
bool X86InstrInfo::classifyLEAReg(MachineInstr &MI, const MachineOperand &Src,
1066968
unsigned Opc, bool AllowSP, Register &NewSrc,
1067969
bool &isKill, MachineOperand &ImplicitOp,
@@ -4324,23 +4226,6 @@ bool X86InstrInfo::optimizeCompareInstr(MachineInstr &CmpInstr, Register SrcReg,
43244226
MI = &Inst;
43254227
break;
43264228
}
4327-
4328-
// Look back for the following pattern, in which case the test64rr
4329-
// instruction could be erased.
4330-
//
4331-
// Example:
4332-
// %reg = and32ri %in_reg, 5
4333-
// ... // EFLAGS not changed.
4334-
// %src_reg = subreg_to_reg 0, %reg, %subreg.sub_index
4335-
// test64rr %src_reg, %src_reg, implicit-def $eflags
4336-
MachineInstr *AndInstr = nullptr;
4337-
if (IsCmpZero &&
4338-
findRedundantFlagInstr(CmpInstr, Inst, MRI, &AndInstr, TRI,
4339-
NoSignFlag, ClearsOverflowFlag)) {
4340-
assert(AndInstr != nullptr && X86::isAND(AndInstr->getOpcode()));
4341-
MI = AndInstr;
4342-
break;
4343-
}
43444229
// Cannot find other candidates before definition of SrcReg.
43454230
return false;
43464231
}

llvm/test/CodeGen/X86/peephole-test-after-add.mir

Lines changed: 0 additions & 196 deletions
This file was deleted.

0 commit comments

Comments
 (0)