xref: /llvm-project/llvm/test/Transforms/InstCombine/preserve-sminmax.ll (revision 2caaec65c04ea7d0e9568b7895b7a46d6100cb75)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3
4; Instcombine normally would fold the sdiv into the comparison,
5; making "icmp slt i32 %h, 2", but in this case the sdiv has
6; another use, so it wouldn't a big win, and it would also
7; obfuscate an otherise obvious smax pattern to the point where
8; other analyses wouldn't recognize it.
9
10define i32 @foo(i32 %h) {
11; CHECK-LABEL: @foo(
12; CHECK-NEXT:    [[SD:%.*]] = sdiv i32 [[H:%.*]], 2
13; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.smin.i32(i32 [[SD]], i32 1)
14; CHECK-NEXT:    ret i32 [[R]]
15;
16  %sd = sdiv i32 %h, 2
17  %t = icmp slt i32 %sd, 1
18  %r = select i1 %t, i32 %sd, i32 1
19  ret i32 %r
20}
21
22define i32 @bar(i32 %h) {
23; CHECK-LABEL: @bar(
24; CHECK-NEXT:    [[SD:%.*]] = sdiv i32 [[H:%.*]], 2
25; CHECK-NEXT:    [[R:%.*]] = call i32 @llvm.smax.i32(i32 [[SD]], i32 1)
26; CHECK-NEXT:    ret i32 [[R]]
27;
28  %sd = sdiv i32 %h, 2
29  %t = icmp sgt i32 %sd, 1
30  %r = select i1 %t, i32 %sd, i32 1
31  ret i32 %r
32}
33