diff options
author | Yingwei Zheng <dtcxzyw2333@gmail.com> | 2025-04-30 11:53:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-04-30 11:53:38 +0800 |
commit | 830cf36bd4c491ef28d9bba74737e324639bb4cd (patch) | |
tree | 17fecbe6ab0f5223fb572a9a243b2ecbd22666e2 /llvm/lib/Analysis/ValueTracking.cpp | |
parent | 4075a3698a3fdda3a77ce2456e3bfefbc0f0c56e (diff) | |
download | llvm-830cf36bd4c491ef28d9bba74737e324639bb4cd.zip llvm-830cf36bd4c491ef28d9bba74737e324639bb4cd.tar.gz llvm-830cf36bd4c491ef28d9bba74737e324639bb4cd.tar.bz2 |
[LVI][ValueTracking] Take UB-implying attributes into account in `isSafeToSpeculativelyExecute` (#137604)
Closes https://github.com/llvm/llvm-project/issues/137582.
In the original case, LVI uses the edge information in `%entry ->
%if.end` to get a more precise result. However, since the call to `smin`
has an `noundef` return attribute, an immediate UB will be triggered
after optimization.
Currently, `isSafeToSpeculativelyExecuteWithOpcode(%min)` returns true
because
https://github.com/llvm/llvm-project/commit/6a288c1e32351d4be3b7630841af078fa1c3bb8b
only checks whether the function is speculatable. However, it is not
enough in this case.
This patch takes UB-implying attributes into account if
`IgnoreUBImplyingAttrs` is set to false. If it is set to true, the
caller is responsible for correctly propagating UB-implying attributes.
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 1d3f8b7..256e77b 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -7201,20 +7201,19 @@ bool llvm::isNotCrossLaneOperation(const Instruction *I) { !isa<CallBase, BitCastInst, ExtractElementInst>(I); } -bool llvm::isSafeToSpeculativelyExecute(const Instruction *Inst, - const Instruction *CtxI, - AssumptionCache *AC, - const DominatorTree *DT, - const TargetLibraryInfo *TLI, - bool UseVariableInfo) { +bool llvm::isSafeToSpeculativelyExecute( + const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC, + const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo, + bool IgnoreUBImplyingAttrs) { return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI, - AC, DT, TLI, UseVariableInfo); + AC, DT, TLI, UseVariableInfo, + IgnoreUBImplyingAttrs); } bool llvm::isSafeToSpeculativelyExecuteWithOpcode( unsigned Opcode, const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI, - bool UseVariableInfo) { + bool UseVariableInfo, bool IgnoreUBImplyingAttrs) { #ifndef NDEBUG if (Inst->getOpcode() != Opcode) { // Check that the operands are actually compatible with the Opcode override. @@ -7287,7 +7286,11 @@ bool llvm::isSafeToSpeculativelyExecuteWithOpcode( // The called function could have undefined behavior or side-effects, even // if marked readnone nounwind. - return Callee && Callee->isSpeculatable(); + if (!Callee || !Callee->isSpeculatable()) + return false; + // Since the operands may be changed after hoisting, undefined behavior may + // be triggered by some UB-implying attributes. + return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs(); } case Instruction::VAArg: case Instruction::Alloca: |