Skip to content

Commit

Permalink
[InstCombine] Drop noundef attributes in foldCttzCtlz (#116718)
Browse files Browse the repository at this point in the history
Closes #112068.

(cherry picked from commit a59976b)
  • Loading branch information
dtcxzyw authored and AZero13 committed Nov 19, 2024
1 parent 51dee6b commit 5151407
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,10 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {

// If ctlz/cttz is only used as a shift amount, set is_zero_poison to true.
if (II.hasOneUse() && match(Op1, m_Zero()) &&
match(II.user_back(), m_Shift(m_Value(), m_Specific(&II))))
match(II.user_back(), m_Shift(m_Value(), m_Specific(&II)))) {
II.dropUBImplyingAttrsAndMetadata();
return IC.replaceOperand(II, 1, IC.Builder.getTrue());
}

Constant *C;

Expand Down
16 changes: 16 additions & 0 deletions llvm/test/Transforms/InstCombine/shift-cttz-ctlz.ll
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ entry:
ret i32 %res
}

; Make sure that noundef is dropped.

define i32 @shl_cttz_false_noundef(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @shl_cttz_false_noundef(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CTTZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[Y]], i1 true)
; CHECK-NEXT: [[RES:%.*]] = shl i32 [[X]], [[CTTZ]]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
%cttz = call noundef i32 @llvm.cttz.i32(i32 %y, i1 false)
%res = shl i32 %x, %cttz
ret i32 %res
}

define i32 @shl_ctlz_false(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @shl_ctlz_false(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
Expand Down

0 comments on commit 5151407

Please sign in to comment.