[ConstantExpr] Don't create fneg expressions
authorNikita Popov <npopov@redhat.com>
Wed, 7 Sep 2022 08:56:36 +0000 (10:56 +0200)
committerNikita Popov <npopov@redhat.com>
Wed, 7 Sep 2022 09:27:25 +0000 (11:27 +0200)
Don't create fneg expressions unless explicitly requested by IR or
bitcode.

clang/test/CodeGen/constantexpr-fneg.c
llvm/include/llvm/Analysis/TargetFolder.h
llvm/include/llvm/IR/ConstantFolder.h
llvm/lib/Analysis/ConstantFolding.cpp
llvm/test/Transforms/InstCombine/fmul.ll
llvm/test/Transforms/InstCombine/fneg.ll
llvm/test/Transforms/Reassociate/crash2.ll

index 1787334..7fc78cf 100644 (file)
@@ -2,8 +2,7 @@
 // RUN: llvm-dis %t.bc -o - | FileCheck %s
 
 // Test case for PR45426. Make sure we do not crash while writing bitcode
-// containing a simplify-able fneg constant expression. Check that the created
-// bitcode file can be disassembled and has the constant expressions simplified.
+// containing a simplify-able fneg constant expression.
 //
 // CHECK-LABEL define i32 @main()
 // CHECK:      entry:
@@ -11,7 +10,9 @@
 // CHECK-NEXT:   store i32 0, i32* %retval
 // CHECK-NEXT:   [[LV:%.*]] = load float*, float** @c
 // CHECK-NEXT:   store float 1.000000e+00, float* [[LV]], align 4
-// CHECK-NEXT:   ret i32 -1
+// CHECK-NEXT:   [[FNEG:%.*]] = fneg float 1.000000e+00
+// CHECK-NEXT:   [[CONV:%.*]] = fptosi float [[FNEG]] to i32
+// CHECK-NEXT:   ret i32 [[CONV]]
 
 int a[], b;
 float *c;
index c425773..db7eda5 100644 (file)
@@ -110,7 +110,7 @@ public:
   Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
                       FastMathFlags FMF) const override {
     if (Constant *C = dyn_cast<Constant>(V))
-      return Fold(ConstantExpr::get(Opc, C));
+      return ConstantFoldUnaryOpOperand(Opc, C, DL);
     return nullptr;
   }
 
index bd28ff8..82c07d4 100644 (file)
@@ -91,7 +91,7 @@ public:
   Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V,
                       FastMathFlags FMF) const override {
     if (Constant *C = dyn_cast<Constant>(V))
-      return ConstantExpr::get(Opc, C);
+      return ConstantFoldUnaryInstruction(Opc, C);
     return nullptr;
   }
 
index 9187d49..df1a9bf 100644 (file)
@@ -1333,7 +1333,7 @@ Constant *llvm::ConstantFoldUnaryOpOperand(unsigned Opcode, Constant *Op,
                                            const DataLayout &DL) {
   assert(Instruction::isUnaryOp(Opcode));
 
-  return ConstantExpr::get(Opcode, Op);
+  return ConstantFoldUnaryInstruction(Opcode, Op);
 }
 
 Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
index 10c8bdd..981e26c 100644 (file)
@@ -1057,7 +1057,8 @@ define float @fmul_fdiv_factor_extra_use(float %x, float %y) {
 
 define double @fmul_negated_constant_expression(double %x) {
 ; CHECK-LABEL: @fmul_negated_constant_expression(
-; CHECK-NEXT:    [[R:%.*]] = fmul double [[X:%.*]], fneg (double bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double))
+; CHECK-NEXT:    [[FSUB:%.*]] = fneg double bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double)
+; CHECK-NEXT:    [[R:%.*]] = fmul double [[FSUB]], [[X:%.*]]
 ; CHECK-NEXT:    ret double [[R]]
 ;
   %fsub = fsub double -0.000000e+00, bitcast (i64 ptrtoint (i8** getelementptr inbounds ({ [2 x i8*] }, { [2 x i8*] }* @g, i64 0, inrange i32 0, i64 2) to i64) to double)
index c08ac44..08ced40 100644 (file)
@@ -594,7 +594,8 @@ define <2 x float> @fake_fneg_nsz_fadd_constant_vec(<2 x float> %x) {
 
 define float @fneg_nsz_fadd_constant_expr(float %x) {
 ; CHECK-LABEL: @fneg_nsz_fadd_constant_expr(
-; CHECK-NEXT:    [[R:%.*]] = fsub nsz float fneg (float bitcast (i32 ptrtoint (i16* @g to i32) to float)), [[X:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = fadd float [[X:%.*]], bitcast (i32 ptrtoint (i16* @g to i32) to float)
+; CHECK-NEXT:    [[R:%.*]] = fneg nsz float [[A]]
 ; CHECK-NEXT:    ret float [[R]]
 ;
   %a = fadd float %x, bitcast (i32 ptrtoint (i16* @g to i32) to float)
@@ -604,7 +605,8 @@ define float @fneg_nsz_fadd_constant_expr(float %x) {
 
 define float @fake_fneg_nsz_fadd_constant_expr(float %x) {
 ; CHECK-LABEL: @fake_fneg_nsz_fadd_constant_expr(
-; CHECK-NEXT:    [[R:%.*]] = fsub nsz float fneg (float bitcast (i32 ptrtoint (i16* @g to i32) to float)), [[X:%.*]]
+; CHECK-NEXT:    [[A:%.*]] = fadd float [[X:%.*]], bitcast (i32 ptrtoint (i16* @g to i32) to float)
+; CHECK-NEXT:    [[R:%.*]] = fneg nsz float [[A]]
 ; CHECK-NEXT:    ret float [[R]]
 ;
   %a = fadd float %x, bitcast (i32 ptrtoint (i16* @g to i32) to float)
index f45fbef..92dcf6b 100644 (file)
@@ -7,7 +7,10 @@
 
 define float @undef1() {
 ; CHECK-LABEL: @undef1(
-; CHECK-NEXT:    ret float 0.000000e+00
+; CHECK-NEXT:    [[FACTOR1:%.*]] = fmul fast float -1.000000e+00, bitcast (i32 ptrtoint (i32* @g to i32) to float)
+; CHECK-NEXT:    [[REASS_ADD:%.*]] = fadd fast float [[FACTOR1]], bitcast (i32 ptrtoint (i32* @g to i32) to float)
+; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast float [[REASS_ADD]], 2.000000e+00
+; CHECK-NEXT:    ret float [[REASS_MUL]]
 ;
   %t0 = fadd fast float bitcast (i32 ptrtoint (i32* @g to i32) to float), bitcast (i32 ptrtoint (i32* @g to i32) to float)
   %t1 = fsub fast float bitcast (i32 ptrtoint (i32* @g to i32) to float), %t0