1 ; RUN: opt < %s -reassociate -constprop -instcombine -S | FileCheck %s 2 3 define float @test1(float %A, float %B) { 4 ; CHECK-LABEL: test1 5 ; CHECK-NEXT: %W = fadd float %B, 5.000000e+00 6 ; CHECK-NEXT: %X = fadd float %A, -7.000000e+00 7 ; CHECK-NEXT: %Y = fsub float %X, %W 8 ; CHECK-NEXT: %Z = fadd float %Y, 1.200000e+01 9 ; CHECK-NEXT: ret float %Z 10 11 %W = fadd float 5.0, %B 12 %X = fadd float -7.0, %A 13 %Y = fsub float %X, %W 14 %Z = fadd float %Y, 12.0 15 ret float %Z 16 } 17 18 ; With sub reassociation, constant folding can eliminate all of the constants. 19 define float @test2(float %A, float %B) { 20 ; CHECK-LABEL: test2 21 ; CHECK-NEXT: %Z = fsub fast float %A, %B 22 ; CHECK-NEXT: ret float %Z 23 24 %W = fadd fast float %B, 5.000000e+00 25 %X = fadd fast float %A, -7.000000e+00 26 %Y = fsub fast float %X, %W 27 %Z = fadd fast float %Y, 1.200000e+01 28 ret float %Z 29 30 } 31 32 define float @test3(float %A, float %B, float %C, float %D) { 33 ; CHECK-LABEL: test3 34 ; CHECK-NEXT: %M = fadd float %A, 1.200000e+01 35 ; CHECK-NEXT: %N = fadd float %M, %B 36 ; CHECK-NEXT: %O = fadd float %N, %C 37 ; CHECK-NEXT: %P = fsub float %D, %O 38 ; CHECK-NEXT: %Q = fadd float %P, 1.200000e+01 39 ; CHECK-NEXT: ret float %Q 40 41 %M = fadd float %A, 1.200000e+01 42 %N = fadd float %M, %B 43 %O = fadd float %N, %C 44 %P = fsub float %D, %O 45 %Q = fadd float %P, 1.200000e+01 46 ret float %Q 47 } 48 49 ; With sub reassociation, constant folding can eliminate the two 12 constants. 50 define float @test4(float %A, float %B, float %C, float %D) { 51 ; CHECK-LABEL: test4 52 ; CHECK-NEXT: %B.neg = fsub fast float -0.000000e+00, %B 53 ; CHECK-NEXT: %O.neg = fsub fast float %B.neg, %A 54 ; CHECK-NEXT: %P = fsub fast float %O.neg, %C 55 ; CHECK-NEXT: %Q = fadd fast float %P, %D 56 ; CHECK-NEXT: ret float %Q 57 58 ; FIXME: InstCombine should be able to get us to the following: 59 ; %sum = fadd fast float %B, %A 60 ; %sum1 = fadd fast float %sum, %C 61 ; %Q = fsub fast float %D, %sum1 62 ; ret i32 %Q 63 64 %M = fadd fast float 1.200000e+01, %A 65 %N = fadd fast float %M, %B 66 %O = fadd fast float %N, %C 67 %P = fsub fast float %D, %O 68 %Q = fadd fast float 1.200000e+01, %P 69 ret float %Q 70 } 71