Skip to content

[InstCombine] Do not use operand info in replaceInInstruction #99492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions llvm/include/llvm/Analysis/ValueTracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -822,15 +822,25 @@ bool isSafeToSpeculativelyExecute(const Instruction *I,
const Instruction *CtxI = nullptr,
AssumptionCache *AC = nullptr,
const DominatorTree *DT = nullptr,
const TargetLibraryInfo *TLI = nullptr);
const TargetLibraryInfo *TLI = nullptr,
bool UseVariableInfo = true);

inline bool isSafeToSpeculativelyExecute(const Instruction *I,
BasicBlock::iterator CtxI,
AssumptionCache *AC = nullptr,
const DominatorTree *DT = nullptr,
const TargetLibraryInfo *TLI = nullptr,
bool UseVariableInfo = true) {
// Take an iterator, and unwrap it into an Instruction *.
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseVariableInfo);
}

/// Don't use information from its non-constant operands. This helper is used
/// when its operands are going to be replaced.
inline bool
isSafeToSpeculativelyExecute(const Instruction *I, BasicBlock::iterator CtxI,
AssumptionCache *AC = nullptr,
const DominatorTree *DT = nullptr,
const TargetLibraryInfo *TLI = nullptr) {
// Take an iterator, and unwrap it into an Instruction *.
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI);
isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I) {
return isSafeToSpeculativelyExecute(I, nullptr, nullptr, nullptr, nullptr,
/*UseVariableInfo=*/false);
}

/// This returns the same result as isSafeToSpeculativelyExecute if Opcode is
Expand All @@ -853,7 +863,7 @@ isSafeToSpeculativelyExecute(const Instruction *I, BasicBlock::iterator CtxI,
bool isSafeToSpeculativelyExecuteWithOpcode(
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI = nullptr,
AssumptionCache *AC = nullptr, const DominatorTree *DT = nullptr,
const TargetLibraryInfo *TLI = nullptr);
const TargetLibraryInfo *TLI = nullptr, bool UseVariableInfo = true);

/// Returns true if the result or effects of the given instructions \p I
/// depend values not reachable through the def use graph.
Expand Down
12 changes: 8 additions & 4 deletions llvm/lib/Analysis/ValueTracking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6760,15 +6760,16 @@ bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
const Instruction *CtxI,
AssumptionCache *AC,
const DominatorTree *DT,
const TargetLibraryInfo *TLI) {
const TargetLibraryInfo *TLI,
bool UseVariableInfo) {
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
AC, DT, TLI);
AC, DT, TLI, UseVariableInfo);
}

bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
AssumptionCache *AC, const DominatorTree *DT,
const TargetLibraryInfo *TLI) {
AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
bool UseVariableInfo) {
#ifndef NDEBUG
if (Inst->getOpcode() != Opcode) {
// Check that the operands are actually compatible with the Opcode override.
Expand Down Expand Up @@ -6820,6 +6821,9 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
return false;
}
case Instruction::Load: {
if (!UseVariableInfo)
return false;

const LoadInst *LI = dyn_cast<LoadInst>(Inst);
if (!LI)
return false;
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1247,8 +1247,11 @@ bool InstCombinerImpl::replaceInInstruction(Value *V, Value *Old, Value *New,
if (Depth == 2)
return false;

assert(!isa<Constant>(Old) && "Only replace non-constant values");

auto *I = dyn_cast<Instruction>(V);
if (!I || !I->hasOneUse() || !isSafeToSpeculativelyExecute(I))
if (!I || !I->hasOneUse() ||
!isSafeToSpeculativelyExecuteWithVariableReplaced(I))
return false;

bool Changed = false;
Expand Down Expand Up @@ -1331,7 +1334,7 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
// profitability is not clear for other cases.
// FIXME: Support vectors.
if (OldOp == CmpLHS && match(NewOp, m_ImmConstant()) &&
!match(OldOp, m_ImmConstant()) && !Cmp.getType()->isVectorTy() &&
!match(OldOp, m_Constant()) && !Cmp.getType()->isVectorTy() &&
isGuaranteedNotToBeUndef(NewOp, SQ.AC, &Sel, &DT))
if (replaceInInstruction(TrueVal, OldOp, NewOp))
return &Sel;
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Transforms/InstCombine/select-binop-cmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,19 @@ define i32 @select_replace_call_speculatable(i32 %x, i32 %y) {
ret i32 %s
}

define i32 @select_replace_call_speculatable_intrinsic(i32 %x, i32 %y) {
; CHECK-LABEL: @select_replace_call_speculatable_intrinsic(
; CHECK-NEXT: [[C:%.*]] = icmp eq i32 [[X:%.*]], 0
; CHECK-NEXT: [[CALL:%.*]] = call i32 @llvm.smax.i32(i32 [[Y:%.*]], i32 0)
; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i32 [[CALL]], i32 [[Y]]
; CHECK-NEXT: ret i32 [[S]]
;
%c = icmp eq i32 %x, 0
%call = call i32 @llvm.smax.i32(i32 %x, i32 %y)
%s = select i1 %c, i32 %call, i32 %y
ret i32 %s
}

; We can't replace the call arguments, as the call is not speculatable. We
; may end up changing side-effects or causing undefined behavior.
define i32 @select_replace_call_non_speculatable(i32 %x, i32 %y) {
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/Transforms/InstCombine/select.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4713,3 +4713,19 @@ define i8 @select_knownbits_simplify_missing_noundef(i8 %x) {
%res = select i1 %cmp, i8 %and, i8 0
ret i8 %res
}

@g_ext = external global i8

; Make sure we don't replace %ptr with @g_ext, which may cause the load to trigger UB.
define i32 @pr99436(ptr align 4 dereferenceable(4) %ptr) {
; CHECK-LABEL: @pr99436(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[PTR:%.*]], @g_ext
; CHECK-NEXT: [[VAL:%.*]] = load i32, ptr [[PTR]], align 4
; CHECK-NEXT: [[RET:%.*]] = select i1 [[CMP]], i32 [[VAL]], i32 0
; CHECK-NEXT: ret i32 [[RET]]
;
%cmp = icmp eq ptr %ptr, @g_ext
%val = load i32, ptr %ptr, align 4
%ret = select i1 %cmp, i32 %val, i32 0
ret i32 %ret
}
Loading