Skip to content

Commit

Permalink
[InstCombine] Fix unexpected overwriting in foldSelectWithSRem (ll…
Browse files Browse the repository at this point in the history
…vm#89539)

Fixes llvm#89516

(cherry picked from commit 6309440)
  • Loading branch information
dtcxzyw authored and llvmbot committed Apr 21, 2024
1 parent e6c3289 commit 6aa164f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2606,7 +2606,7 @@ static Instruction *foldSelectWithSRem(SelectInst &SI, InstCombinerImpl &IC,
// %cnd = icmp slt i32 %rem, 0
// %add = add i32 %rem, %n
// %sel = select i1 %cnd, i32 %add, i32 %rem
if (match(TrueVal, m_Add(m_Value(RemRes), m_Value(Remainder))) &&
if (match(TrueVal, m_Add(m_Specific(RemRes), m_Value(Remainder))) &&
match(RemRes, m_SRem(m_Value(Op), m_Specific(Remainder))) &&
IC.isKnownToBeAPowerOfTwo(Remainder, /*OrZero*/ true) &&
FalseVal == RemRes)
Expand Down
17 changes: 17 additions & 0 deletions llvm/test/Transforms/InstCombine/select-divrem.ll
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,20 @@ define i32 @rem_euclid_pow2_false_arm_folded(i32 %n) {
%res = select i1 %nonneg, i32 %rem, i32 1
ret i32 %res
}

define i8 @pr89516(i8 %n, i8 %x) {
; CHECK-LABEL: @pr89516(
; CHECK-NEXT: [[COND:%.*]] = icmp slt i8 [[X:%.*]], 0
; CHECK-NEXT: [[POW2:%.*]] = shl nuw i8 1, [[N:%.*]]
; CHECK-NEXT: [[SREM:%.*]] = srem i8 1, [[POW2]]
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[COND]], i8 [[POW2]], i8 0
; CHECK-NEXT: [[RES:%.*]] = add nuw i8 [[SREM]], [[ADD]]
; CHECK-NEXT: ret i8 [[RES]]
;
%cond = icmp slt i8 %x, 0
%pow2 = shl nuw i8 1, %n
%srem = srem i8 1, %pow2
%add = add nuw i8 %srem, %pow2
%res = select i1 %cond, i8 %add, i8 %srem
ret i8 %res
}

0 comments on commit 6aa164f

Please sign in to comment.