1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -instcombine -S | FileCheck %s
4 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
6 ; Tests folding constants from two similar selects that feed a add
8 define float @test1a(i1 zeroext %arg) #0 {
9 ; CHECK-LABEL: @test1a(
10 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[ARG:%.*]], float 6.000000e+00, float 1.500000e+01
11 ; CHECK-NEXT: ret float [[TMP2]]
13 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
14 %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
15 %tmp2 = fadd float %tmp, %tmp1
19 ; Tests folding multiple expression constants from similar selects that feed a adds
21 define float @test1b(i1 zeroext %arg) #0 {
22 ; CHECK-LABEL: @test1b(
23 ; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[ARG:%.*]], float 7.250000e+00, float 2.800000e+01
24 ; CHECK-NEXT: ret float [[TMP5]]
26 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
27 %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
28 %tmp2 = select i1 %arg, float 2.500000e-01, float 4.000000e+00
29 %tmp3 = fadd float %tmp, %tmp1
30 %tmp4 = fadd float %tmp2, %tmp1
31 %tmp5 = fadd float %tmp4, %tmp3
35 ; Tests folding constants from two similar selects that feed a sub
37 define float @test2(i1 zeroext %arg) #0 {
38 ; CHECK-LABEL: @test2(
39 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[ARG:%.*]], float 4.000000e+00, float -3.000000e+00
40 ; CHECK-NEXT: ret float [[TMP2]]
42 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
43 %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
44 %tmp2 = fsub float %tmp, %tmp1
48 ; Tests folding constants from two similar selects that feed a mul
50 define float @test3(i1 zeroext %arg) #0 {
51 ; CHECK-LABEL: @test3(
52 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[ARG:%.*]], float 5.000000e+00, float 5.400000e+01
53 ; CHECK-NEXT: ret float [[TMP2]]
55 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
56 %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
57 %tmp2 = fmul float %tmp, %tmp1
61 declare void @use_float(float)
63 ; Tests folding constants if the selects have multiple uses but
64 ; we can fold away the binary op with a select.
66 define float @test4(i1 zeroext %arg) #0 {
67 ; CHECK-LABEL: @test4(
68 ; CHECK-NEXT: [[TMP:%.*]] = select i1 [[ARG:%.*]], float 5.000000e+00, float 6.000000e+00
69 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[ARG]], float 5.000000e+00, float 5.400000e+01
70 ; CHECK-NEXT: call void @use_float(float [[TMP]])
71 ; CHECK-NEXT: ret float [[TMP2]]
73 %tmp = select i1 %arg, float 5.000000e+00, float 6.000000e+00
74 %tmp1 = select i1 %arg, float 1.000000e+00, float 9.000000e+00
75 %tmp2 = fmul float %tmp, %tmp1
76 call void @use_float(float %tmp)
80 ; Tests not folding constants if we cannot fold away any of the selects.
82 define float @test5(i1 zeroext %arg, float %div) {
83 ; CHECK-LABEL: @test5(
84 ; CHECK-NEXT: [[TMP:%.*]] = select i1 [[ARG:%.*]], float [[DIV:%.*]], float 5.000000e+00
85 ; CHECK-NEXT: [[MUL:%.*]] = fmul contract float [[TMP]], [[TMP]]
86 ; CHECK-NEXT: call void @use_float(float [[TMP]])
87 ; CHECK-NEXT: ret float [[MUL]]
89 %tmp = select i1 %arg, float %div, float 5.000000e+00
90 %mul = fmul contract float %tmp, %tmp
91 call void @use_float(float %tmp)
95 define float @fmul_nnan_nsz(i1 %cond, float %val) {
96 ; CHECK-LABEL: @fmul_nnan_nsz(
97 ; CHECK-NEXT: ret float 0.000000e+00
99 %lhs = select i1 %cond, float %val, float +0.0
100 %rhs = select i1 %cond, float -0.0, float %val
101 %mul = fmul nnan nsz float %lhs, %rhs
105 define <2 x float> @fadd_nsz(<2 x i1> %cond, <2 x float> %val) {
106 ; CHECK-LABEL: @fadd_nsz(
107 ; CHECK-NEXT: ret <2 x float> [[VAL:%.*]]
109 %lhs = select <2 x i1> %cond, <2 x float> %val, <2 x float> <float +0.0, float +0.0>
110 %rhs = select <2 x i1> %cond, <2 x float> <float +0.0, float +0.0>, <2 x float> %val
111 %add = fadd nsz <2 x float> %lhs, %rhs
115 define double @fsub_nnan(i1 %cond, double %val, double %val2) {
116 ; CHECK-LABEL: @fsub_nnan(
117 ; CHECK-NEXT: [[TMP1:%.*]] = fadd nnan double [[VAL2:%.*]], -7.000000e+00
118 ; CHECK-NEXT: [[ADD:%.*]] = select nnan i1 [[COND:%.*]], double 0.000000e+00, double [[TMP1]]
119 ; CHECK-NEXT: ret double [[ADD]]
121 %lhs = select i1 %cond, double %val, double %val2
122 %rhs = select i1 %cond, double %val, double 7.0
123 %add = fsub nnan double %lhs, %rhs
127 ; TODO combine selects feeding fdiv like we do for fmul, fadd and fsub
128 define double @fdiv_nnan_nsz(i1 %cond, double %val, double %val2) {
129 ; CHECK-LABEL: @fdiv_nnan_nsz(
130 ; CHECK-NEXT: [[LHS:%.*]] = select i1 [[COND:%.*]], double [[VAL2:%.*]], double 0.000000e+00
131 ; CHECK-NEXT: [[RHS:%.*]] = select i1 [[COND]], double 4.200000e+01, double [[VAL:%.*]]
132 ; CHECK-NEXT: [[ADD:%.*]] = fdiv nnan nsz double [[LHS]], [[RHS]]
133 ; CHECK-NEXT: ret double [[ADD]]
135 %lhs = select i1 %cond, double %val2, double 0.0
136 %rhs = select i1 %cond, double 42.0, double %val
137 %add = fdiv nnan nsz double %lhs, %rhs