1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -reassociate -instcombine -S | FileCheck %s 3 4define float @test1(float %A, float %B) { 5; CHECK-LABEL: @test1( 6; CHECK-NEXT: [[W:%.*]] = fadd float [[B:%.*]], 5.000000e+00 7; CHECK-NEXT: [[X:%.*]] = fadd float [[A:%.*]], -7.000000e+00 8; CHECK-NEXT: [[Y:%.*]] = fsub float [[X]], [[W]] 9; CHECK-NEXT: [[Z:%.*]] = fadd float [[Y]], 1.200000e+01 10; CHECK-NEXT: ret float [[Z]] 11; 12 %W = fadd float 5.0, %B 13 %X = fadd float -7.0, %A 14 %Y = fsub float %X, %W 15 %Z = fadd float %Y, 12.0 16 ret float %Z 17} 18 19; With sub reassociation, constant folding can eliminate all of the constants. 20define float @test2(float %A, float %B) { 21; CHECK-LABEL: @test2( 22; CHECK-NEXT: [[Z:%.*]] = fsub fast float [[A:%.*]], [[B:%.*]] 23; CHECK-NEXT: ret float [[Z]] 24; 25 %W = fadd fast float %B, 5.000000e+00 26 %X = fadd fast float %A, -7.000000e+00 27 %Y = fsub fast float %X, %W 28 %Z = fadd fast float %Y, 1.200000e+01 29 ret float %Z 30} 31 32; Check again using minimal subset of FMF. 33; Both 'reassoc' and 'nsz' are required. 34define float @test2_minimal(float %A, float %B) { 35; CHECK-LABEL: @test2_minimal( 36; CHECK-NEXT: [[TMP1:%.*]] = fsub reassoc nsz float [[A:%.*]], [[B:%.*]] 37; CHECK-NEXT: ret float [[TMP1]] 38; 39 %W = fadd reassoc nsz float %B, 5.000000e+00 40 %X = fadd reassoc nsz float %A, -7.000000e+00 41 %Y = fsub reassoc nsz float %X, %W 42 %Z = fadd reassoc nsz float %Y, 1.200000e+01 43 ret float %Z 44} 45 46; Verify the fold is not done with only 'reassoc' ('nsz' is required). 47define float @test2_reassoc(float %A, float %B) { 48; CHECK-LABEL: @test2_reassoc( 49; CHECK-NEXT: [[W:%.*]] = fadd reassoc float [[B:%.*]], 5.000000e+00 50; CHECK-NEXT: [[X:%.*]] = fadd reassoc float [[A:%.*]], -7.000000e+00 51; CHECK-NEXT: [[Y:%.*]] = fsub reassoc float [[X]], [[W]] 52; CHECK-NEXT: [[Z:%.*]] = fadd reassoc float [[Y]], 1.200000e+01 53; CHECK-NEXT: ret float [[Z]] 54; 55 %W = fadd reassoc float %B, 5.000000e+00 56 %X = fadd reassoc float %A, -7.000000e+00 57 %Y = fsub reassoc float %X, %W 58 %Z = fadd reassoc float %Y, 1.200000e+01 59 ret float %Z 60} 61 62define float @test3(float %A, float %B, float %C, float %D) { 63; CHECK-LABEL: @test3( 64; CHECK-NEXT: [[M:%.*]] = fadd float [[A:%.*]], 1.200000e+01 65; CHECK-NEXT: [[N:%.*]] = fadd float [[M]], [[B:%.*]] 66; CHECK-NEXT: [[O:%.*]] = fadd float [[N]], [[C:%.*]] 67; CHECK-NEXT: [[P:%.*]] = fsub float [[D:%.*]], [[O]] 68; CHECK-NEXT: [[Q:%.*]] = fadd float [[P]], 1.200000e+01 69; CHECK-NEXT: ret float [[Q]] 70; 71 %M = fadd float %A, 1.200000e+01 72 %N = fadd float %M, %B 73 %O = fadd float %N, %C 74 %P = fsub float %D, %O 75 %Q = fadd float %P, 1.200000e+01 76 ret float %Q 77} 78 79; With sub reassociation, constant folding can eliminate the two 12 constants. 80 81define float @test4(float %A, float %B, float %C, float %D) { 82; CHECK-LABEL: @test4( 83; CHECK-NEXT: [[TMP1:%.*]] = fadd fast float [[B:%.*]], [[A:%.*]] 84; CHECK-NEXT: [[TMP2:%.*]] = fadd fast float [[TMP1]], [[C:%.*]] 85; CHECK-NEXT: [[Q:%.*]] = fsub fast float [[D:%.*]], [[TMP2]] 86; CHECK-NEXT: ret float [[Q]] 87; 88 %M = fadd fast float 1.200000e+01, %A 89 %N = fadd fast float %M, %B 90 %O = fadd fast float %N, %C 91 %P = fsub fast float %D, %O 92 %Q = fadd fast float 1.200000e+01, %P 93 ret float %Q 94} 95 96; Check again using minimal subset of FMF. 97 98define float @test4_reassoc(float %A, float %B, float %C, float %D) { 99; CHECK-LABEL: @test4_reassoc( 100; CHECK-NEXT: [[M:%.*]] = fadd reassoc float [[A:%.*]], 1.200000e+01 101; CHECK-NEXT: [[N:%.*]] = fadd reassoc float [[M]], [[B:%.*]] 102; CHECK-NEXT: [[O:%.*]] = fadd reassoc float [[N]], [[C:%.*]] 103; CHECK-NEXT: [[P:%.*]] = fsub reassoc float [[D:%.*]], [[O]] 104; CHECK-NEXT: [[Q:%.*]] = fadd reassoc float [[P]], 1.200000e+01 105; CHECK-NEXT: ret float [[Q]] 106; 107 %M = fadd reassoc float 1.200000e+01, %A 108 %N = fadd reassoc float %M, %B 109 %O = fadd reassoc float %N, %C 110 %P = fsub reassoc float %D, %O 111 %Q = fadd reassoc float 1.200000e+01, %P 112 ret float %Q 113} 114 115