1; RUN: opt -reassociate -gvn -S < %s | FileCheck %s 2 3; (x + 0.1234 * y) * (x + -0.1234 * y) -> (x + 0.1234 * y) * (x - 0.1234 * y) 4define double @test1(double %x, double %y) { 5; CHECK-LABEL: @test1( 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]] 11; 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 17 ret double %mul3 18} 19 20; (x + -0.1234 * y) * (x + -0.1234 * y) -> (x - 0.1234 * y) * (x - 0.1234 * y) 21define 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]] 27; 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 33 ret double %mul3 34} 35 36; (x + 0.1234 * y) * (x - -0.1234 * y) -> (x + 0.1234 * y) * (x + 0.1234 * y) 37define 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]] 43; 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 49 ret double %mul3 50} 51 52; Canonicalize (x - -0.1234 * y) 53define 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]] 58; 59 %mul = fmul double -1.234000e-01, %y 60 %sub = fsub double %x, %mul 61 ret double %sub 62} 63 64; Don't modify (-0.1234 * y - x) 65define 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]] 70; 71 %mul = fmul double -1.234000e-01, %y 72 %sub = fsub double %mul, %x 73 ret double %sub 74} 75 76; Canonicalize (-0.1234 * y + x) -> (x - 0.1234 * y) 77define 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]] 82; 83 %mul = fmul double -1.234000e-01, %y 84 %add = fadd double %mul, %x 85 ret double %add 86} 87 88; Canonicalize (y * -0.1234 + x) -> (x - 0.1234 * y) 89define 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]] 94; 95 %mul = fmul double %y, -1.234000e-01 96 %add = fadd double %mul, %x 97 ret double %add 98} 99 100; Canonicalize (x - -0.1234 / y) 101define 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]] 106; 107 %div = fdiv double -1.234000e-01, %y 108 %sub = fsub double %x, %div 109 ret double %sub 110} 111 112; Don't modify (-0.1234 / y - x) 113define 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]] 118; 119 %div = fdiv double -1.234000e-01, %y 120 %sub = fsub double %div, %x 121 ret double %sub 122} 123 124; Canonicalize (-0.1234 / y + x) -> (x - 0.1234 / y) 125define 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]] 130; 131 %div = fdiv double -1.234000e-01, %y 132 %add = fadd double %div, %x 133 ret double %add 134} 135 136; Canonicalize (y / -0.1234 + x) -> (x - y / 0.1234) 137define 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]] 142; 143 %div = fdiv double %y, -1.234000e-01 144 %add = fadd double %div, %x 145 ret double %add 146} 147 148; Don't create an NSW violation 149define 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]] 154; 155 %mul = mul nsw i4 %x, -2 156 %add = add i4 %mul, 3 157 ret i4 %add 158} 159 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. 165; 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. 169 170define 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]] 178; 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 184 ret double %add 185} 186