@@ -226,3 +226,75 @@ define float @fabs_x_fabs(float %x, float %y) {
226226 %mul = fmul float %x.fabs , %y.fabs
227227 ret float %mul
228228}
229+
230+ ; (X*Y) * X => (X*X) * Y
231+
232+ define float @reassoc_common_operand1 (float %x , float %y ) {
233+ ; CHECK-LABEL: @reassoc_common_operand1(
234+ ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], [[X]]
235+ ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[TMP1]], [[Y:%.*]]
236+ ; CHECK-NEXT: ret float [[MUL2]]
237+ ;
238+ %mul1 = fmul float %x , %y
239+ %mul2 = fmul fast float %mul1 , %x
240+ ret float %mul2
241+ }
242+
243+ ; (Y*X) * X => (X*X) * Y
244+
245+ define float @reassoc_common_operand2 (float %x , float %y ) {
246+ ; CHECK-LABEL: @reassoc_common_operand2(
247+ ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X:%.*]], [[X]]
248+ ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[TMP1]], [[Y:%.*]]
249+ ; CHECK-NEXT: ret float [[MUL2]]
250+ ;
251+ %mul1 = fmul float %y , %x
252+ %mul2 = fmul fast float %mul1 , %x
253+ ret float %mul2
254+ }
255+
256+ ; X * (X*Y) => (X*X) * Y
257+
258+ define float @reassoc_common_operand3 (float %x1 , float %y ) {
259+ ; CHECK-LABEL: @reassoc_common_operand3(
260+ ; CHECK-NEXT: [[X:%.*]] = fdiv float [[X1:%.*]], 3.000000e+00
261+ ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X]], [[X]]
262+ ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[TMP1]], [[Y:%.*]]
263+ ; CHECK-NEXT: ret float [[MUL2]]
264+ ;
265+ %x = fdiv float %x1 , 3 .0 ; thwart complexity-based canonicalization
266+ %mul1 = fmul float %x , %y
267+ %mul2 = fmul fast float %x , %mul1
268+ ret float %mul2
269+ }
270+
271+ ; X * (Y*X) => (X*X) * Y
272+
273+ define float @reassoc_common_operand4 (float %x1 , float %y ) {
274+ ; CHECK-LABEL: @reassoc_common_operand4(
275+ ; CHECK-NEXT: [[X:%.*]] = fdiv float [[X1:%.*]], 3.000000e+00
276+ ; CHECK-NEXT: [[TMP1:%.*]] = fmul fast float [[X]], [[X]]
277+ ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[TMP1]], [[Y:%.*]]
278+ ; CHECK-NEXT: ret float [[MUL2]]
279+ ;
280+ %x = fdiv float %x1 , 3 .0 ; thwart complexity-based canonicalization
281+ %mul1 = fmul float %y , %x
282+ %mul2 = fmul fast float %x , %mul1
283+ ret float %mul2
284+ }
285+
286+ ; No change if the first fmul has another use.
287+
288+ define float @reassoc_common_operand_multi_use (float %x , float %y ) {
289+ ; CHECK-LABEL: @reassoc_common_operand_multi_use(
290+ ; CHECK-NEXT: [[MUL1:%.*]] = fmul float [[X:%.*]], [[Y:%.*]]
291+ ; CHECK-NEXT: [[MUL2:%.*]] = fmul fast float [[MUL1]], [[X]]
292+ ; CHECK-NEXT: call void @use_f32(float [[MUL1]])
293+ ; CHECK-NEXT: ret float [[MUL2]]
294+ ;
295+ %mul1 = fmul float %x , %y
296+ %mul2 = fmul fast float %mul1 , %x
297+ call void @use_f32 (float %mul1 )
298+ ret float %mul2
299+ }
300+
0 commit comments