From 98a3a340c3612c06c51d7bb36bfc5857ab06a951 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 7 Sep 2022 10:56:36 +0200 Subject: [PATCH] [ConstantExpr] Don't create fneg expressions Don't create fneg expressions unless explicitly requested by IR or bitcode. --- clang/test/CodeGen/constantexpr-fneg.c | 7 ++++--- llvm/include/llvm/Analysis/TargetFolder.h | 2 +- llvm/include/llvm/IR/ConstantFolder.h | 2 +- llvm/lib/Analysis/ConstantFolding.cpp | 2 +- llvm/test/Transforms/InstCombine/fmul.ll | 3 ++- llvm/test/Transforms/InstCombine/fneg.ll | 6 ++++-- llvm/test/Transforms/Reassociate/crash2.ll | 5 ++++- 7 files changed, 17 insertions(+), 10 deletions(-) diff --git a/clang/test/CodeGen/constantexpr-fneg.c b/clang/test/CodeGen/constantexpr-fneg.c index 17873345..7fc78cf 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 c425773..db7eda5 100644 --- a/llvm/include/llvm/Analysis/TargetFolder.h +++ b/llvm/include/llvm/Analysis/TargetFolder.h @@ -110,7 +110,7 @@ public: Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override { if (Constant *C = dyn_cast(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 bd28ff8..82c07d4 100644 --- a/llvm/include/llvm/IR/ConstantFolder.h +++ b/llvm/include/llvm/IR/ConstantFolder.h @@ -91,7 +91,7 @@ public: Value *FoldUnOpFMF(Instruction::UnaryOps Opc, Value *V, FastMathFlags FMF) const override { if (Constant *C = dyn_cast(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 9187d49..df1a9bf 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 10c8bdd..981e26c 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 c08ac44..08ced40 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 f45fbef..92dcf6b 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 -- 2.7.4