Browse Source

[ConstantFold] fp_binop undef, undef --> undef

These are uncontroversial and independent of a proposed LangRef edits (D44216).

I tried to fix tests that would fold away:
rL327004
rL327028
rL327030
rL327034

I'm not sure if the Reassociate tests are meaningless yet, but they probably will be 
as we add more folds, so if anyone has suggestions or wants to fix those, please do.

Differential Revision: https://reviews.llvm.org/D44258


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@327058 91177308-0d34-0410-b5e6-96231b3b80d8
Sanjay Patel 7 years ago
parent
commit
b70e54f12d

+ 4 - 1
lib/IR/ConstantFold.cpp

@@ -1009,7 +1009,10 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
     case Instruction::FMul:
     case Instruction::FMul:
     case Instruction::FDiv:
     case Instruction::FDiv:
     case Instruction::FRem:
     case Instruction::FRem:
-      // TODO: UNDEF handling for binary float instructions.
+      // [any flop] undef, undef -> undef
+      if (isa<UndefValue>(C1) && isa<UndefValue>(C2))
+        return C1;
+      // TODO: Handle one undef operand and some other constant.
       return nullptr;
       return nullptr;
     case Instruction::BinaryOpsEnd:
     case Instruction::BinaryOpsEnd:
       llvm_unreachable("Invalid BinaryOp");
       llvm_unreachable("Invalid BinaryOp");

+ 1 - 1
test/Transforms/InstCombine/X86/X86FsubCmpCombine.ll

@@ -155,7 +155,7 @@ entry:
 define i8 @sub_compare_folding_swapPD256_undef(<4 x double> %a, <4 x double> %b) {
 define i8 @sub_compare_folding_swapPD256_undef(<4 x double> %a, <4 x double> %b) {
 ; CHECK-LABEL: @sub_compare_folding_swapPD256_undef(
 ; CHECK-LABEL: @sub_compare_folding_swapPD256_undef(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[TMP:%.*]] = call <4 x i1> @llvm.x86.avx512.mask.cmp.pd.256(<4 x double> fsub (<4 x double> undef, <4 x double> undef), <4 x double> zeroinitializer, i32 5)
+; CHECK-NEXT:    [[TMP:%.*]] = call <4 x i1> @llvm.x86.avx512.mask.cmp.pd.256(<4 x double> undef, <4 x double> zeroinitializer, i32 5)
 ; CHECK-NEXT:    [[TMP0:%.*]] = shufflevector <4 x i1> [[TMP]], <4 x i1> zeroinitializer, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
 ; CHECK-NEXT:    [[TMP0:%.*]] = shufflevector <4 x i1> [[TMP]], <4 x i1> zeroinitializer, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <8 x i1> [[TMP0]] to i8
 ; CHECK-NEXT:    [[TMP1:%.*]] = bitcast <8 x i1> [[TMP0]] to i8
 ; CHECK-NEXT:    ret i8 [[TMP1]]
 ; CHECK-NEXT:    ret i8 [[TMP1]]

+ 5 - 5
test/Transforms/InstSimplify/fp-undef.ll

@@ -179,7 +179,7 @@ define float @frem_undef_op1_fast(float %x) {
 
 
 define double @fadd_undef_undef(double %x) {
 define double @fadd_undef_undef(double %x) {
 ; CHECK-LABEL: @fadd_undef_undef(
 ; CHECK-LABEL: @fadd_undef_undef(
-; CHECK-NEXT:    ret double fadd (double undef, double undef)
+; CHECK-NEXT:    ret double undef
 ;
 ;
   %r = fadd double undef, undef
   %r = fadd double undef, undef
   ret double %r
   ret double %r
@@ -187,7 +187,7 @@ define double @fadd_undef_undef(double %x) {
 
 
 define double @fsub_undef_undef(double %x) {
 define double @fsub_undef_undef(double %x) {
 ; CHECK-LABEL: @fsub_undef_undef(
 ; CHECK-LABEL: @fsub_undef_undef(
-; CHECK-NEXT:    ret double fsub (double undef, double undef)
+; CHECK-NEXT:    ret double undef
 ;
 ;
   %r = fsub double undef, undef
   %r = fsub double undef, undef
   ret double %r
   ret double %r
@@ -195,7 +195,7 @@ define double @fsub_undef_undef(double %x) {
 
 
 define double @fmul_undef_undef(double %x) {
 define double @fmul_undef_undef(double %x) {
 ; CHECK-LABEL: @fmul_undef_undef(
 ; CHECK-LABEL: @fmul_undef_undef(
-; CHECK-NEXT:    ret double fmul (double undef, double undef)
+; CHECK-NEXT:    ret double undef
 ;
 ;
   %r = fmul double undef, undef
   %r = fmul double undef, undef
   ret double %r
   ret double %r
@@ -203,7 +203,7 @@ define double @fmul_undef_undef(double %x) {
 
 
 define double @fdiv_undef_undef(double %x) {
 define double @fdiv_undef_undef(double %x) {
 ; CHECK-LABEL: @fdiv_undef_undef(
 ; CHECK-LABEL: @fdiv_undef_undef(
-; CHECK-NEXT:    ret double fdiv (double undef, double undef)
+; CHECK-NEXT:    ret double undef
 ;
 ;
   %r = fdiv double undef, undef
   %r = fdiv double undef, undef
   ret double %r
   ret double %r
@@ -211,7 +211,7 @@ define double @fdiv_undef_undef(double %x) {
 
 
 define double @frem_undef_undef(double %x) {
 define double @frem_undef_undef(double %x) {
 ; CHECK-LABEL: @frem_undef_undef(
 ; CHECK-LABEL: @frem_undef_undef(
-; CHECK-NEXT:    ret double frem (double undef, double undef)
+; CHECK-NEXT:    ret double undef
 ;
 ;
   %r = frem double undef, undef
   %r = frem double undef, undef
   ret double %r
   ret double %r

+ 6 - 5
test/Transforms/Reassociate/factorize-again.ll

@@ -1,15 +1,16 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -reassociate < %s | FileCheck %s
 ; RUN: opt -S -reassociate < %s | FileCheck %s
 
 
 define void @main(float, float) {
 define void @main(float, float) {
 ; CHECK-LABEL: @main(
 ; CHECK-LABEL: @main(
 ; CHECK-NEXT:  wrapper_entry:
 ; CHECK-NEXT:  wrapper_entry:
-; CHECK-NEXT:    [[TMP2:%.*]] = fsub float undef, %0
-; CHECK-NEXT:    [[TMP3:%.*]] = fsub float undef, %1
+; CHECK-NEXT:    [[TMP2:%.*]] = fsub float undef, [[TMP0:%.*]]
+; CHECK-NEXT:    [[TMP3:%.*]] = fsub float undef, [[TMP1:%.*]]
 ; CHECK-NEXT:    [[TMP4:%.*]] = call float @llvm.rsqrt.f32(float undef)
 ; CHECK-NEXT:    [[TMP4:%.*]] = call float @llvm.rsqrt.f32(float undef)
 ; CHECK-NEXT:    [[REASS_ADD2:%.*]] = fadd fast float [[TMP3]], [[TMP2]]
 ; CHECK-NEXT:    [[REASS_ADD2:%.*]] = fadd fast float [[TMP3]], [[TMP2]]
-; CHECK-NEXT:    [[REASS_MUL3:%.*]] = fmul fast float [[REASS_ADD2]], undef
-; CHECK-NEXT:    [[REASS_ADD1:%.*]] = fadd fast float [[REASS_MUL3]], fmul (float undef, float undef)
-; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast float [[REASS_ADD1]], [[TMP4]]
+; CHECK-NEXT:    [[REASS_MUL3:%.*]] = fmul fast float [[TMP4]], [[REASS_ADD2]]
+; CHECK-NEXT:    [[REASS_ADD1:%.*]] = fadd fast float [[REASS_MUL3]], [[TMP4]]
+; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast float [[REASS_ADD1]], undef
 ; CHECK-NEXT:    [[TMP5:%.*]] = call float @foo2(float [[REASS_MUL]], float 0.000000e+00)
 ; CHECK-NEXT:    [[TMP5:%.*]] = call float @foo2(float [[REASS_MUL]], float 0.000000e+00)
 ; CHECK-NEXT:    [[MUL36:%.*]] = fmul fast float [[TMP5]], 1.500000e+00
 ; CHECK-NEXT:    [[MUL36:%.*]] = fmul fast float [[TMP5]], 1.500000e+00
 ; CHECK-NEXT:    call void @foo1(i32 4, float [[MUL36]])
 ; CHECK-NEXT:    call void @foo1(i32 4, float [[MUL36]])

+ 3 - 2
test/Transforms/Reassociate/fp-expr.ll

@@ -1,10 +1,11 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -reassociate < %s | FileCheck %s
 ; RUN: opt -S -reassociate < %s | FileCheck %s
 
 
 define void @test1() {
 define void @test1() {
 ; CHECK-LABEL: @test1(
 ; CHECK-LABEL: @test1(
 ; CHECK-NEXT:    [[T1:%.*]] = tail call <4 x float> @blam()
 ; CHECK-NEXT:    [[T1:%.*]] = tail call <4 x float> @blam()
 ; CHECK-NEXT:    [[T1_NEG:%.*]] = fsub fast <4 x float> <float -0.000000e+00, float -0.000000e+00, float -0.000000e+00, float -0.000000e+00>, [[T1]]
 ; CHECK-NEXT:    [[T1_NEG:%.*]] = fsub fast <4 x float> <float -0.000000e+00, float -0.000000e+00, float -0.000000e+00, float -0.000000e+00>, [[T1]]
-; CHECK-NEXT:    [[T24:%.*]] = fadd fast <4 x float> [[T1_NEG]], fadd (<4 x float> undef, <4 x float> undef)
+; CHECK-NEXT:    [[T24:%.*]] = fadd fast <4 x float> [[T1_NEG]], undef
 ; CHECK-NEXT:    tail call void @wombat(<4 x float> [[T24]])
 ; CHECK-NEXT:    tail call void @wombat(<4 x float> [[T24]])
 ; CHECK-NEXT:    ret void
 ; CHECK-NEXT:    ret void
 ;
 ;
@@ -19,7 +20,7 @@ define half @test2() {
 ; CHECK-LABEL: @test2(
 ; CHECK-LABEL: @test2(
 ; CHECK-NEXT:    [[T15:%.*]] = fsub fast half undef, undef
 ; CHECK-NEXT:    [[T15:%.*]] = fsub fast half undef, undef
 ; CHECK-NEXT:    [[T15_NEG:%.*]] = fsub fast half 0xH8000, [[T15]]
 ; CHECK-NEXT:    [[T15_NEG:%.*]] = fsub fast half 0xH8000, [[T15]]
-; CHECK-NEXT:    [[T18:%.*]] = fadd fast half [[T15_NEG]], fadd (half undef, half undef)
+; CHECK-NEXT:    [[T18:%.*]] = fadd fast half [[T15_NEG]], undef
 ; CHECK-NEXT:    ret half [[T18]]
 ; CHECK-NEXT:    ret half [[T18]]
 ;
 ;
   %t15 = fsub fast half undef, undef
   %t15 = fsub fast half undef, undef