1 ; RUN: opt -reassociate -gvn -S < %s | FileCheck %s
3 ; (x + 0.1234 * y) * (x + -0.1234 * y) -> (x + 0.1234 * y) * (x - 0.1234 * y)
4 define double @test1(double %x, double %y) {
6 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, 1.234000e-01
7 ; CHECK-NEXT: [[ADD:%.*]] = fadd double %x, [[MUL]]
8 ; CHECK-NEXT: [[ADD21:%.*]] = fsub double %x, [[MUL]]
9 ; CHECK-NEXT: [[MUL3:%.*]] = fmul double [[ADD]], [[ADD21]]
10 ; CHECK-NEXT: ret double [[MUL3]]
12 %mul = fmul double 1.234000e-01, %y
13 %add = fadd double %mul, %x
14 %mul1 = fmul double -1.234000e-01, %y
15 %add2 = fadd double %mul1, %x
16 %mul3 = fmul double %add, %add2
20 ; (x + -0.1234 * y) * (x + -0.1234 * y) -> (x - 0.1234 * y) * (x - 0.1234 * y)
21 define double @test2(double %x, double %y) {
22 ; CHECK-LABEL: @test2(
23 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, 1.234000e-01
24 ; CHECK-NEXT: [[ADD1:%.*]] = fsub double %x, [[MUL]]
25 ; CHECK-NEXT: [[MUL3:%.*]] = fmul double [[ADD1]], [[ADD1]]
26 ; CHECK-NEXT: ret double [[MUL3]]
28 %mul = fmul double %y, -1.234000e-01
29 %add = fadd double %mul, %x
30 %mul1 = fmul double %y, -1.234000e-01
31 %add2 = fadd double %mul1, %x
32 %mul3 = fmul double %add, %add2
36 ; (x + 0.1234 * y) * (x - -0.1234 * y) -> (x + 0.1234 * y) * (x + 0.1234 * y)
37 define double @test3(double %x, double %y) {
38 ; CHECK-LABEL: @test3(
39 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, 1.234000e-01
40 ; CHECK-NEXT: [[ADD:%.*]] = fadd double %x, [[MUL]]
41 ; CHECK-NEXT: [[MUL3:%.*]] = fmul double [[ADD]], [[ADD]]
42 ; CHECK-NEXT: ret double [[MUL3]]
44 %mul = fmul double %y, 1.234000e-01
45 %add = fadd double %mul, %x
46 %mul1 = fmul double %y, -1.234000e-01
47 %add2 = fsub double %x, %mul1
48 %mul3 = fmul double %add, %add2
52 ; Canonicalize (x - -0.1234 * y)
53 define double @test5(double %x, double %y) {
54 ; CHECK-LABEL: @test5(
55 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, 1.234000e-01
56 ; CHECK-NEXT: [[SUB1:%.*]] = fadd double %x, [[MUL]]
57 ; CHECK-NEXT: ret double [[SUB1]]
59 %mul = fmul double -1.234000e-01, %y
60 %sub = fsub double %x, %mul
64 ; Don't modify (-0.1234 * y - x)
65 define double @test6(double %x, double %y) {
66 ; CHECK-LABEL: @test6(
67 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, -1.234000e-01
68 ; CHECK-NEXT: [[SUB:%.*]] = fsub double [[MUL]], %x
69 ; CHECK-NEXT: ret double [[SUB]]
71 %mul = fmul double -1.234000e-01, %y
72 %sub = fsub double %mul, %x
76 ; Canonicalize (-0.1234 * y + x) -> (x - 0.1234 * y)
77 define double @test7(double %x, double %y) {
78 ; CHECK-LABEL: @test7(
79 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, 1.234000e-01
80 ; CHECK-NEXT: [[ADD1:%.*]] = fsub double %x, [[MUL]]
81 ; CHECK-NEXT: ret double [[ADD1]]
83 %mul = fmul double -1.234000e-01, %y
84 %add = fadd double %mul, %x
88 ; Canonicalize (y * -0.1234 + x) -> (x - 0.1234 * y)
89 define double @test8(double %x, double %y) {
90 ; CHECK-LABEL: @test8(
91 ; CHECK-NEXT: [[MUL:%.*]] = fmul double %y, 1.234000e-01
92 ; CHECK-NEXT: [[ADD1:%.*]] = fsub double %x, [[MUL]]
93 ; CHECK-NEXT: ret double [[ADD1]]
95 %mul = fmul double %y, -1.234000e-01
96 %add = fadd double %mul, %x
100 ; Canonicalize (x - -0.1234 / y)
101 define double @test9(double %x, double %y) {
102 ; CHECK-LABEL: @test9(
103 ; CHECK-NEXT: [[DIV:%.*]] = fdiv double 1.234000e-01, %y
104 ; CHECK-NEXT: [[SUB1:%.*]] = fadd double %x, [[DIV]]
105 ; CHECK-NEXT: ret double [[SUB1]]
107 %div = fdiv double -1.234000e-01, %y
108 %sub = fsub double %x, %div
112 ; Don't modify (-0.1234 / y - x)
113 define double @test10(double %x, double %y) {
114 ; CHECK-LABEL: @test10(
115 ; CHECK-NEXT: [[DIV:%.*]] = fdiv double -1.234000e-01, %y
116 ; CHECK-NEXT: [[SUB:%.*]] = fsub double [[DIV]], %x
117 ; CHECK-NEXT: ret double [[SUB]]
119 %div = fdiv double -1.234000e-01, %y
120 %sub = fsub double %div, %x
124 ; Canonicalize (-0.1234 / y + x) -> (x - 0.1234 / y)
125 define double @test11(double %x, double %y) {
126 ; CHECK-LABEL: @test11(
127 ; CHECK-NEXT: [[DIV:%.*]] = fdiv double 1.234000e-01, %y
128 ; CHECK-NEXT: [[ADD1:%.*]] = fsub double %x, [[DIV]]
129 ; CHECK-NEXT: ret double [[ADD1]]
131 %div = fdiv double -1.234000e-01, %y
132 %add = fadd double %div, %x
136 ; Canonicalize (y / -0.1234 + x) -> (x - y / 0.1234)
137 define double @test12(double %x, double %y) {
138 ; CHECK-LABEL: @test12(
139 ; CHECK-NEXT: [[DIV:%.*]] = fdiv double %y, 1.234000e-01
140 ; CHECK-NEXT: [[ADD1:%.*]] = fsub double %x, [[DIV]]
141 ; CHECK-NEXT: ret double [[ADD1]]
143 %div = fdiv double %y, -1.234000e-01
144 %add = fadd double %div, %x
148 ; Don't create an NSW violation
149 define i4 @test13(i4 %x) {
150 ; CHECK-LABEL: @test13(
151 ; CHECK-NEXT: [[MUL:%.*]] = mul nsw i4 %x, -2
152 ; CHECK-NEXT: [[ADD:%.*]] = add i4 [[MUL]], 3
153 ; CHECK-NEXT: ret i4 [[ADD]]
155 %mul = mul nsw i4 %x, -2
156 %add = add i4 %mul, 3
160 ; This tests used to cause an infinite loop where we would loop between
161 ; canonicalizing the negated constant (i.e., (X + Y*-5.0) -> (X - Y*5.0)) and
162 ; breaking up a subtract (i.e., (X - Y*5.0) -> X + (0 - Y*5.0)). To break the
163 ; cycle, we don't canonicalize the negative constant if we're going to later
164 ; break up the subtract.
166 ; Check to make sure we don't canonicalize
167 ; (%pow2*-5.0 + %sub) -> (%sub - %pow2*5.0)
168 ; as we would later break up this subtract causing a cycle.
170 define double @pr34078(double %A) {
171 ; CHECK-LABEL: @pr34078(
172 ; CHECK-NEXT: [[SUB:%.*]] = fsub fast double 1.000000e+00, %A
173 ; CHECK-NEXT: [[POW2:%.*]] = fmul double %A, %A
174 ; CHECK-NEXT: [[MUL5_NEG:%.*]] = fmul fast double [[POW2]], -5.000000e-01
175 ; CHECK-NEXT: [[SUB1:%.*]] = fadd fast double [[MUL5_NEG]], [[SUB]]
176 ; CHECK-NEXT: [[FACTOR:%.*]] = fmul fast double [[SUB1]], 2.000000e+00
177 ; CHECK-NEXT: ret double [[FACTOR]]
179 %sub = fsub fast double 1.000000e+00, %A
180 %pow2 = fmul double %A, %A
181 %mul5 = fmul fast double %pow2, 5.000000e-01
182 %sub1 = fsub fast double %sub, %mul5
183 %add = fadd fast double %sub1, %sub1