[llvm] 98a3a34 - [ConstantExpr] Don't create fneg expressions

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 7 02:27:37 PDT 2022


Author: Nikita Popov
Date: 2022-09-07T11:27:25+02:00
New Revision: 98a3a340c3612c06c51d7bb36bfc5857ab06a951

URL: https://github.com/llvm/llvm-project/commit/98a3a340c3612c06c51d7bb36bfc5857ab06a951
DIFF: https://github.com/llvm/llvm-project/commit/98a3a340c3612c06c51d7bb36bfc5857ab06a951.diff

LOG: [ConstantExpr] Don't create fneg expressions

Don't create fneg expressions unless explicitly requested by IR or
bitcode.

Added: 
    

Modified: 
    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

Removed: 
    


################################################################################
diff  --git a/clang/test/CodeGen/constantexpr-fneg.c b/clang/test/CodeGen/constantexpr-fneg.c
index 17873345daee7..7fc78cfebf8f3 100644
--- a/clang/test/CodeGen/constantexpr-fneg.c
+++ b/clang/test/CodeGen/constantexpr-fneg.c
@@ -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;

diff  --git a/llvm/include/llvm/Analysis/TargetFolder.h b/llvm/include/llvm/Analysis/TargetFolder.h
index c42577330e9b4..db7eda54b5c45 100644
--- a/llvm/include/llvm/Analysis/TargetFolder.h
+++ b/llvm/include/llvm/Analysis/TargetFolder.h
@@ -110,7 +110,7 @@ class TargetFolder final : public IRBuilderFolder {
   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;
   }
 

diff  --git a/llvm/include/llvm/IR/ConstantFolder.h b/llvm/include/llvm/IR/ConstantFolder.h
index bd28ff87965dd..82c07d47a1930 100644
--- a/llvm/include/llvm/IR/ConstantFolder.h
+++ b/llvm/include/llvm/IR/ConstantFolder.h
@@ -91,7 +91,7 @@ class ConstantFolder final : public IRBuilderFolder {
   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;
   }
 

diff  --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 9187d49ca7a5a..df1a9bfa3da27 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -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,

diff  --git a/llvm/test/Transforms/InstCombine/fmul.ll b/llvm/test/Transforms/InstCombine/fmul.ll
index 10c8bdd532817..981e26c4912bf 100644
--- a/llvm/test/Transforms/InstCombine/fmul.ll
+++ b/llvm/test/Transforms/InstCombine/fmul.ll
@@ -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)

diff  --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index c08ac44e34130..08ced4018483b 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -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)

diff  --git a/llvm/test/Transforms/Reassociate/crash2.ll b/llvm/test/Transforms/Reassociate/crash2.ll
index f45fbefcd3992..92dcf6b7b3757 100644
--- a/llvm/test/Transforms/Reassociate/crash2.ll
+++ b/llvm/test/Transforms/Reassociate/crash2.ll
@@ -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


        


More information about the llvm-commits mailing list