1 ; RUN: opt < %s -reassociate -constprop -instcombine -S | FileCheck %s
3 define float @test1(float %A, float %B) {
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]]
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
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]]
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
31 ; Check again using minimal subset of FMF.
32 ; Both 'reassoc' and 'nsz' are required.
33 define float @test2_minimal(float %A, float %B) {
34 ; CHECK-LABEL: @test2_minimal(
35 ; CHECK-NEXT: [[Z:%.*]] = fsub reassoc nsz float %A, %B
36 ; CHECK-NEXT: ret float [[Z]]
38 %W = fadd reassoc nsz float %B, 5.000000e+00
39 %X = fadd reassoc nsz float %A, -7.000000e+00
40 %Y = fsub reassoc nsz float %X, %W
41 %Z = fadd reassoc nsz float %Y, 1.200000e+01
45 ; Verify the fold is not done with only 'reassoc' ('nsz' is required).
46 define float @test2_reassoc(float %A, float %B) {
47 ; CHECK-LABEL: @test2_reassoc(
48 ; CHECK-NEXT: [[W:%.*]] = fadd reassoc float %B, 5.000000e+00
49 ; CHECK-NEXT: [[X:%.*]] = fadd reassoc float %A, -7.000000e+00
50 ; CHECK-NEXT: [[Y:%.*]] = fsub reassoc float [[X]], [[W]]
51 ; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[Y]], 1.200000e+01
52 ; CHECK-NEXT: ret float [[Z]]
54 %W = fadd reassoc float %B, 5.000000e+00
55 %X = fadd reassoc float %A, -7.000000e+00
56 %Y = fsub reassoc float %X, %W
57 %Z = fadd reassoc float %Y, 1.200000e+01
61 define float @test3(float %A, float %B, float %C, float %D) {
62 ; CHECK-LABEL: @test3(
63 ; CHECK-NEXT: [[M:%.*]] = fadd float %A, 1.200000e+01
64 ; CHECK-NEXT: [[N:%.*]] = fadd float [[M]], %B
65 ; CHECK-NEXT: [[O:%.*]] = fadd float [[N]], %C
66 ; CHECK-NEXT: [[P:%.*]] = fsub float %D, [[O]]
67 ; CHECK-NEXT: [[Q:%.*]] = fadd float [[P]], 1.200000e+01
68 ; CHECK-NEXT: ret float [[Q]]
70 %M = fadd float %A, 1.200000e+01
71 %N = fadd float %M, %B
72 %O = fadd float %N, %C
73 %P = fsub float %D, %O
74 %Q = fadd float %P, 1.200000e+01
78 ; With sub reassociation, constant folding can eliminate the two 12 constants.
80 define float @test4(float %A, float %B, float %C, float %D) {
81 ; FIXME: InstCombine should be able to get us to the following:
82 ; %sum = fadd fast float %B, %A
83 ; %sum1 = fadd fast float %sum, %C
84 ; %Q = fsub fast float %D, %sum1
86 ; CHECK-LABEL: @test4(
87 ; CHECK-NEXT: [[B_NEG:%.*]] = fsub fast float -0.000000e+00, %B
88 ; CHECK-NEXT: [[O_NEG:%.*]] = fsub fast float [[B_NEG]], %A
89 ; CHECK-NEXT: [[P:%.*]] = fsub fast float [[O_NEG]], %C
90 ; CHECK-NEXT: [[Q:%.*]] = fadd fast float [[P]], %D
91 ; CHECK-NEXT: ret float [[Q]]
93 %M = fadd fast float 1.200000e+01, %A
94 %N = fadd fast float %M, %B
95 %O = fadd fast float %N, %C
96 %P = fsub fast float %D, %O
97 %Q = fadd fast float 1.200000e+01, %P
101 ; Check again using minimal subset of FMF.
103 define float @test4_reassoc(float %A, float %B, float %C, float %D) {
104 ; CHECK-LABEL: @test4_reassoc(
105 ; CHECK-NEXT: [[M:%.*]] = fadd reassoc float %A, 1.200000e+01
106 ; CHECK-NEXT: [[N:%.*]] = fadd reassoc float [[M]], %B
107 ; CHECK-NEXT: [[O:%.*]] = fadd reassoc float [[N]], %C
108 ; CHECK-NEXT: [[P:%.*]] = fsub reassoc float %D, [[O]]
109 ; CHECK-NEXT: [[Q:%.*]] = fadd reassoc float [[P]], 1.200000e+01
110 ; CHECK-NEXT: ret float [[Q]]
112 %M = fadd reassoc float 1.200000e+01, %A
113 %N = fadd reassoc float %M, %B
114 %O = fadd reassoc float %N, %C
115 %P = fsub reassoc float %D, %O
116 %Q = fadd reassoc float 1.200000e+01, %P