Skip to content

Commit 4d557fd

Browse files
committed
[InstCombine] Do not use operand info in replaceInInstruction
1 parent 3ecf076 commit 4d557fd

File tree

5 files changed

+37
-17
lines changed

5 files changed

+37
-17
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -822,15 +822,25 @@ bool isSafeToSpeculativelyExecute(const Instruction *I,
822822
const Instruction *CtxI = nullptr,
823823
AssumptionCache *AC = nullptr,
824824
const DominatorTree *DT = nullptr,
825-
const TargetLibraryInfo *TLI = nullptr);
825+
const TargetLibraryInfo *TLI = nullptr,
826+
bool UseOperandInfo = true);
827+
828+
inline bool isSafeToSpeculativelyExecute(const Instruction *I,
829+
BasicBlock::iterator CtxI,
830+
AssumptionCache *AC = nullptr,
831+
const DominatorTree *DT = nullptr,
832+
const TargetLibraryInfo *TLI = nullptr,
833+
bool UseOperandInfo = true) {
834+
// Take an iterator, and unwrap it into an Instruction *.
835+
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseOperandInfo);
836+
}
826837

838+
/// Don't use information from its operands. This helper is used when its
839+
/// operands are going to be replaced.
827840
inline bool
828-
isSafeToSpeculativelyExecute(const Instruction *I, BasicBlock::iterator CtxI,
829-
AssumptionCache *AC = nullptr,
830-
const DominatorTree *DT = nullptr,
831-
const TargetLibraryInfo *TLI = nullptr) {
832-
// Take an iterator, and unwrap it into an Instruction *.
833-
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI);
841+
isSafeToSpeculativelyExecuteWithOperandsReplaced(const Instruction *I) {
842+
return isSafeToSpeculativelyExecute(I, nullptr, nullptr, nullptr, nullptr,
843+
/*UseOperandInfo=*/false);
834844
}
835845

836846
/// This returns the same result as isSafeToSpeculativelyExecute if Opcode is
@@ -853,7 +863,7 @@ isSafeToSpeculativelyExecute(const Instruction *I, BasicBlock::iterator CtxI,
853863
bool isSafeToSpeculativelyExecuteWithOpcode(
854864
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI = nullptr,
855865
AssumptionCache *AC = nullptr, const DominatorTree *DT = nullptr,
856-
const TargetLibraryInfo *TLI = nullptr);
866+
const TargetLibraryInfo *TLI = nullptr, bool UseInstrInfo = true);
857867

858868
/// Returns true if the result or effects of the given instructions \p I
859869
/// depend values not reachable through the def use graph.

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6760,15 +6760,16 @@ bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
67606760
const Instruction *CtxI,
67616761
AssumptionCache *AC,
67626762
const DominatorTree *DT,
6763-
const TargetLibraryInfo *TLI) {
6763+
const TargetLibraryInfo *TLI,
6764+
bool UseOperandInfo) {
67646765
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6765-
AC, DT, TLI);
6766+
AC, DT, TLI, UseOperandInfo);
67666767
}
67676768

67686769
bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
67696770
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6770-
AssumptionCache *AC, const DominatorTree *DT,
6771-
const TargetLibraryInfo *TLI) {
6771+
AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
6772+
bool UseOperandInfo) {
67726773
#ifndef NDEBUG
67736774
if (Inst->getOpcode() != Opcode) {
67746775
// Check that the operands are actually compatible with the Opcode override.
@@ -6796,12 +6797,15 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
67966797
case Instruction::URem: {
67976798
// x / y is undefined if y == 0.
67986799
const APInt *V;
6799-
if (match(Inst->getOperand(1), m_APInt(V)))
6800+
if (UseOperandInfo && match(Inst->getOperand(1), m_APInt(V)))
68006801
return *V != 0;
68016802
return false;
68026803
}
68036804
case Instruction::SDiv:
68046805
case Instruction::SRem: {
6806+
if (!UseOperandInfo)
6807+
return false;
6808+
68056809
// x / y is undefined if y == 0 or x == INT_MIN and y == -1
68066810
const APInt *Numerator, *Denominator;
68076811
if (!match(Inst->getOperand(1), m_APInt(Denominator)))
@@ -6820,6 +6824,9 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
68206824
return false;
68216825
}
68226826
case Instruction::Load: {
6827+
if (!UseOperandInfo)
6828+
return false;
6829+
68236830
const LoadInst *LI = dyn_cast<LoadInst>(Inst);
68246831
if (!LI)
68256832
return false;
@@ -6838,7 +6845,9 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
68386845

68396846
// The called function could have undefined behavior or side-effects, even
68406847
// if marked readnone nounwind.
6841-
return Callee && Callee->isSpeculatable();
6848+
// NOTE: Intrinsic cannot be replaced.
6849+
return Callee && Callee->isSpeculatable() &&
6850+
(UseOperandInfo || Callee->isIntrinsic());
68426851
}
68436852
case Instruction::VAArg:
68446853
case Instruction::Alloca:

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,8 @@ bool InstCombinerImpl::replaceInInstruction(Value *V, Value *Old, Value *New,
12481248
return false;
12491249

12501250
auto *I = dyn_cast<Instruction>(V);
1251-
if (!I || !I->hasOneUse() || !isSafeToSpeculativelyExecute(I))
1251+
if (!I || !I->hasOneUse() ||
1252+
!isSafeToSpeculativelyExecuteWithOperandsReplaced(I))
12521253
return false;
12531254

12541255
bool Changed = false;

llvm/test/Transforms/InstCombine/select-binop-cmp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ define <2 x i32> @select_replace_undef(<2 x i32> %x, <2 x i32> %y) {
13051305
define i32 @select_replace_call_speculatable(i32 %x, i32 %y) {
13061306
; CHECK-LABEL: @select_replace_call_speculatable(
13071307
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
1308-
; CHECK-NEXT: [[CALL:%.*]] = call i32 @call_speculatable(i32 0, i32 0)
1308+
; CHECK-NEXT: [[CALL:%.*]] = call i32 @call_speculatable(i32 [[X]], i32 [[X]])
13091309
; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i32 [[CALL]], i32 [[Y:%.*]]
13101310
; CHECK-NEXT: ret i32 [[S]]
13111311
;

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4720,7 +4720,7 @@ define i8 @select_knownbits_simplify_missing_noundef(i8 %x) {
47204720
define i32 @pr99436(ptr align 4 dereferenceable(4) %ptr) {
47214721
; CHECK-LABEL: @pr99436(
47224722
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[PTR:%.*]], @g_ext
4723-
; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr @g_ext, align 4
4723+
; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[PTR]], align 4
47244724
; CHECK-NEXT: [[RET:%.*]] = select i1 [[CMP]], i32 [[VAL]], i32 0
47254725
; CHECK-NEXT: ret i32 [[RET]]
47264726
;

0 commit comments

Comments
 (0)