Skip to content

Commit e25e74f

Browse files
committed
[InstCombine] Fold selection between less than zero and one
1 parent 669bcbb commit e25e74f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,20 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
34153415
TrueVal);
34163416
}
34173417

3418+
// select (icmp eq a, 0), 1, (lshr a, 31) -> icmp sle a, 0,
3419+
// which is then converted to icmp sle a, 1
3420+
CmpInst::Predicate Pred;
3421+
Value *A;
3422+
const APInt *C;
3423+
if (match(CondVal, m_Cmp(Pred, m_Value(A), m_Zero())) &&
3424+
match(TrueVal, m_One()) &&
3425+
match(FalseVal, m_LShr(m_Specific(A), m_APInt(C))) &&
3426+
Pred == ICmpInst::ICMP_EQ && *C == 31) {
3427+
auto *Cond = Builder.CreateICmpSLE(A,
3428+
ConstantInt::getNullValue(A->getType()));
3429+
return new ZExtInst(Cond, A->getType());
3430+
}
3431+
34183432
if (Instruction *R = foldSelectOfBools(SI))
34193433
return R;
34203434

llvm/test/Transforms/InstCombine/icmp-select.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ declare i8 @llvm.umin.i8(i8, i8)
77

88
define i32 @test_icmp_select_lte_0(i32 %0) {
99
; CHECK-LABEL: @test_icmp_select_lte_0(
10-
; CHECK-NEXT: [[CML:%.*]] = icmp eq i32 [[TMP0:%.*]], 0
11-
; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[TMP0]], 31
12-
; CHECK-NEXT: [[RE:%.*]] = select i1 [[CML]], i32 1, i32 [[LSHR]]
10+
; CHECK-NEXT: [[TMP2:%.*]] = icmp slt i32 [[TMP0:%.*]], 1
11+
; CHECK-NEXT: [[RE:%.*]] = zext i1 [[TMP2]] to i32
1312
; CHECK-NEXT: ret i32 [[RE]]
1413
;
1514
%cml = icmp eq i32 %0, 0

0 commit comments

Comments
 (0)