Skip to content

Commit 3cd67ee

Browse files
committed
[InstCombine] Drop range attr in select of ctz fold
The range may no longer be valid after the select has been optimized away. This fixes the kernel miscompiles reported at ClangBuiltLinux/linux#2031.
1 parent 11725b5 commit 3cd67ee

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ static Instruction *foldSelectCtlzToCttz(ICmpInst *ICI, Value *TrueVal,
11271127
/// into:
11281128
/// %0 = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
11291129
static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
1130-
InstCombiner::BuilderTy &Builder) {
1130+
InstCombinerImpl &IC) {
11311131
ICmpInst::Predicate Pred = ICI->getPredicate();
11321132
Value *CmpLHS = ICI->getOperand(0);
11331133
Value *CmpRHS = ICI->getOperand(1);
@@ -1169,6 +1169,9 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
11691169
// Explicitly clear the 'is_zero_poison' flag. It's always valid to go from
11701170
// true to false on this flag, so we can replace it for all users.
11711171
II->setArgOperand(1, ConstantInt::getFalse(II->getContext()));
1172+
// A range annotation on the intrinsic may no longer be valid.
1173+
II->dropPoisonGeneratingAnnotations();
1174+
IC.addToWorklist(II);
11721175
return SelectArg;
11731176
}
11741177

@@ -1921,7 +1924,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19211924
if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))
19221925
return replaceInstUsesWith(SI, V);
19231926

1924-
if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, Builder))
1927+
if (Value *V = foldSelectCttzCtlz(ICI, TrueVal, FalseVal, *this))
19251928
return replaceInstUsesWith(SI, V);
19261929

19271930
if (Value *V = canonicalizeSaturatedSubtract(ICI, TrueVal, FalseVal, Builder))

llvm/test/Transforms/InstCombine/select-cmp-cttz-ctlz.ll

+2-4
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ define i16 @test4(i16 %x) {
4949
ret i16 %cond
5050
}
5151

52-
; FIXME: This is a miscompile.
5352
define i16 @test4_with_range(i16 %x) {
5453
; CHECK-LABEL: @test4_with_range(
55-
; CHECK-NEXT: [[CT:%.*]] = call range(i16 0, 16) i16 @llvm.ctlz.i16(i16 [[X:%.*]], i1 false)
54+
; CHECK-NEXT: [[CT:%.*]] = call range(i16 0, 17) i16 @llvm.ctlz.i16(i16 [[X:%.*]], i1 false)
5655
; CHECK-NEXT: ret i16 [[CT]]
5756
;
5857
%ct = call range(i16 0, 16) i16 @llvm.ctlz.i16(i16 %x, i1 true)
@@ -127,10 +126,9 @@ define i16 @test4b(i16 %x) {
127126
ret i16 %cond
128127
}
129128

130-
; FIXME: This is a miscompile.
131129
define i16 @test4b_with_range(i16 %x) {
132130
; CHECK-LABEL: @test4b_with_range(
133-
; CHECK-NEXT: [[CT:%.*]] = call range(i16 0, 16) i16 @llvm.cttz.i16(i16 [[X:%.*]], i1 false)
131+
; CHECK-NEXT: [[CT:%.*]] = call range(i16 0, 17) i16 @llvm.cttz.i16(i16 [[X:%.*]], i1 false)
134132
; CHECK-NEXT: ret i16 [[CT]]
135133
;
136134
%ct = call range(i16 0, 16) i16 @llvm.cttz.i16(i16 %x, i1 true)

0 commit comments

Comments
 (0)