Skip to content

Commit a37dbc1

Browse files
authored
[InstCombine] Drop noundef in foldSelectCttzCtlz (#121692)
Close #121428
1 parent b5f2167 commit a37dbc1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,8 +1225,12 @@ static Value *foldSelectCttzCtlz(ICmpInst *ICI, Value *TrueVal, Value *FalseVal,
12251225
// zext/trunc) have one use (ending at the select), the cttz/ctlz result will
12261226
// not be used if the input is zero. Relax to 'zero is poison' for that case.
12271227
if (II->hasOneUse() && SelectArg->hasOneUse() &&
1228-
!match(II->getArgOperand(1), m_One()))
1228+
!match(II->getArgOperand(1), m_One())) {
12291229
II->setArgOperand(1, ConstantInt::getTrue(II->getContext()));
1230+
// noundef attribute on the intrinsic may no longer be valid.
1231+
II->dropUBImplyingAttrsAndMetadata();
1232+
IC.addToWorklist(II);
1233+
}
12301234

12311235
return nullptr;
12321236
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,19 @@ define i32 @test_cttz_not_bw(i32 %x) {
495495
ret i32 %res
496496
}
497497

498+
define i32 @test_cttz_not_bw_noundef(i32 %x) {
499+
; CHECK-LABEL: @test_cttz_not_bw_noundef(
500+
; CHECK-NEXT: [[CT:%.*]] = tail call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
501+
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp eq i32 [[X]], 0
502+
; CHECK-NEXT: [[RES:%.*]] = select i1 [[CMP_NOT]], i32 123, i32 [[CT]]
503+
; CHECK-NEXT: ret i32 [[RES]]
504+
;
505+
%ct = tail call noundef i32 @llvm.cttz.i32(i32 %x, i1 false)
506+
%cmp = icmp ne i32 %x, 0
507+
%res = select i1 %cmp, i32 %ct, i32 123
508+
ret i32 %res
509+
}
510+
498511
define i32 @test_cttz_not_bw_multiuse(i32 %x) {
499512
; CHECK-LABEL: @test_cttz_not_bw_multiuse(
500513
; CHECK-NEXT: [[CT:%.*]] = tail call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false)

0 commit comments

Comments
 (0)