[ConstantExpr] Don't create fneg expressions

Don't create fneg expressions unless explicitly requested by IR or
bitcode.
This commit is contained in:
Nikita Popov 2022-09-07 10:56:36 +02:00
parent da695de628
commit 98a3a340c3
7 changed files with 17 additions and 10 deletions

View 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;

View 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;
}

View 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;
}

View 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,

View 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)

View 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)

View 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