Skip to content

Commit aea16f2

Browse files
committed
[CVP][ValueTracking] Take UB-implying attrs into account
1 parent 9e7d1ec commit aea16f2

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -539,31 +539,43 @@ bool isNotCrossLaneOperation(const Instruction *I);
539539
/// move the instruction as long as the correct dominance relationships for
540540
/// the operands and users hold.
541541
///
542+
/// If \p UseVariableInfo is true, the information from non-constant operands
543+
/// will be taken into account.
544+
///
545+
/// If \p AllowRefinement is true, UB-implying attributes and metadata will be
546+
/// ignored. The caller is responsible for correctly propagating them after
547+
/// hoisting.
548+
///
542549
/// This method can return true for instructions that read memory;
543550
/// for such instructions, moving them may change the resulting value.
544551
bool isSafeToSpeculativelyExecute(const Instruction *I,
545552
const Instruction *CtxI = nullptr,
546553
AssumptionCache *AC = nullptr,
547554
const DominatorTree *DT = nullptr,
548555
const TargetLibraryInfo *TLI = nullptr,
549-
bool UseVariableInfo = true);
556+
bool UseVariableInfo = true,
557+
bool AllowRefinement = true);
550558

551559
inline bool isSafeToSpeculativelyExecute(const Instruction *I,
552560
BasicBlock::iterator CtxI,
553561
AssumptionCache *AC = nullptr,
554562
const DominatorTree *DT = nullptr,
555563
const TargetLibraryInfo *TLI = nullptr,
556-
bool UseVariableInfo = true) {
564+
bool UseVariableInfo = true,
565+
bool AllowRefinement = true) {
557566
// Take an iterator, and unwrap it into an Instruction *.
558-
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseVariableInfo);
567+
return isSafeToSpeculativelyExecute(I, &*CtxI, AC, DT, TLI, UseVariableInfo,
568+
AllowRefinement);
559569
}
560570

561571
/// Don't use information from its non-constant operands. This helper is used
562572
/// when its operands are going to be replaced.
563573
inline bool
564-
isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I) {
574+
isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I,
575+
bool AllowRefinement = true) {
565576
return isSafeToSpeculativelyExecute(I, nullptr, nullptr, nullptr, nullptr,
566-
/*UseVariableInfo=*/false);
577+
/*UseVariableInfo=*/false,
578+
AllowRefinement);
567579
}
568580

569581
/// This returns the same result as isSafeToSpeculativelyExecute if Opcode is
@@ -586,7 +598,8 @@ isSafeToSpeculativelyExecuteWithVariableReplaced(const Instruction *I) {
586598
bool isSafeToSpeculativelyExecuteWithOpcode(
587599
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI = nullptr,
588600
AssumptionCache *AC = nullptr, const DominatorTree *DT = nullptr,
589-
const TargetLibraryInfo *TLI = nullptr, bool UseVariableInfo = true);
601+
const TargetLibraryInfo *TLI = nullptr, bool UseVariableInfo = true,
602+
bool AllowRefinement = true);
590603

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

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,8 @@ ValueLatticeElement LazyValueInfoImpl::getValueAtUse(const Use &U) {
17011701
// of a cycle, we might end up reasoning about values from different cycle
17021702
// iterations (PR60629).
17031703
if (!CurrI->hasOneUse() ||
1704-
!isSafeToSpeculativelyExecuteWithVariableReplaced(CurrI))
1704+
!isSafeToSpeculativelyExecuteWithVariableReplaced(
1705+
CurrI, /*AllowRefinement=*/false))
17051706
break;
17061707
CurrU = &*CurrI->use_begin();
17071708
}

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7201,20 +7201,19 @@ bool llvm::isNotCrossLaneOperation(const Instruction *I) {
72017201
!isa<CallBase, BitCastInst, ExtractElementInst>(I);
72027202
}
72037203

7204-
bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst,
7205-
const Instruction *CtxI,
7206-
AssumptionCache *AC,
7207-
const DominatorTree *DT,
7208-
const TargetLibraryInfo *TLI,
7209-
bool UseVariableInfo) {
7204+
bool llvm::isSafeToSpeculativelyExecute(
7205+
const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7206+
const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7207+
bool AllowRefinement) {
72107208
return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7211-
AC, DT, TLI, UseVariableInfo);
7209+
AC, DT, TLI, UseVariableInfo,
7210+
AllowRefinement);
72127211
}
72137212

72147213
bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
72157214
unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
72167215
AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7217-
bool UseVariableInfo) {
7216+
bool UseVariableInfo, bool AllowRefinement) {
72187217
#ifndef NDEBUG
72197218
if (Inst->getOpcode() != Opcode) {
72207219
// Check that the operands are actually compatible with the Opcode override.
@@ -7266,7 +7265,7 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
72667265
return false;
72677266
}
72687267
case Instruction::Load: {
7269-
if (!UseVariableInfo)
7268+
if (!UseVariableInfo || !AllowRefinement)
72707269
return false;
72717270

72727271
const LoadInst *LI = dyn_cast<LoadInst>(Inst);
@@ -7287,7 +7286,20 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode(
72877286

72887287
// The called function could have undefined behavior or side-effects, even
72897288
// if marked readnone nounwind.
7290-
return Callee && Callee->isSpeculatable();
7289+
if (!Callee || !Callee->isSpeculatable())
7290+
return false;
7291+
// Since the operands may be changed after hoisting, undefined behavior may
7292+
// be triggered by some UB-implying attributes.
7293+
if (!AllowRefinement) {
7294+
if (CI->hasRetAttr(Attribute::NoUndef) ||
7295+
CI->getRetDereferenceableBytes() > 0 ||
7296+
CI->getRetDereferenceableOrNullBytes() > 0 ||
7297+
any_of(CI->args(), [&](const Use &U) {
7298+
return CI->isPassingUndefUB(CI->getArgOperandNo(&U));
7299+
}))
7300+
return false;
7301+
}
7302+
return true;
72917303
}
72927304
case Instruction::VAArg:
72937305
case Instruction::Alloca:

llvm/test/Transforms/CorrelatedValuePropagation/pr137582.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ define i8 @test(i16 %x) {
88
; CHECK-SAME: i16 [[X:%.*]]) {
99
; CHECK-NEXT: [[ENTRY:.*]]:
1010
; CHECK-NEXT: [[OR:%.*]] = or i16 [[X]], 1
11-
; CHECK-NEXT: [[CONV:%.*]] = trunc nuw nsw i16 [[OR]] to i8
11+
; CHECK-NEXT: [[CONV:%.*]] = trunc i16 [[OR]] to i8
1212
; CHECK-NEXT: [[MIN:%.*]] = call noundef i8 @llvm.smin.i8(i8 [[CONV]], i8 0)
1313
; CHECK-NEXT: [[COND:%.*]] = icmp eq i16 [[X]], 0
1414
; CHECK-NEXT: br i1 [[COND]], label %[[IF_END:.*]], label %[[IF_THEN:.*]]

0 commit comments

Comments
 (0)