From d686ea32b1af5f0a00fe577f530817c4214e16d0 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 8 Jul 2022 17:43:55 +0200 Subject: [PATCH] [ConstantFolding] Guard against unfolded FP binop Check that the operation actually folded before trying to flush denormals. A minor variation of the pr33453 test exposed this with the FP binops marked as undesirable. --- llvm/lib/Analysis/ConstantFolding.cpp | 2 ++ llvm/test/Transforms/InstCombine/pr33453.ll | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 85d9385236dc..aa4da27be4e5 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -1397,6 +1397,8 @@ Constant *llvm::ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS, // Calculate constant result. Constant *C = ConstantFoldBinaryOpOperands(Opcode, Op0, Op1, DL); + if (!C) + return nullptr; // Flush denormal output if needed. return FlushFPConstant(C, I, /* IsOutput */ true); diff --git a/llvm/test/Transforms/InstCombine/pr33453.ll b/llvm/test/Transforms/InstCombine/pr33453.ll index 5d496e76186a..055ae274257c 100644 --- a/llvm/test/Transforms/InstCombine/pr33453.ll +++ b/llvm/test/Transforms/InstCombine/pr33453.ll @@ -6,9 +6,11 @@ define float @patatino() { ; CHECK-LABEL: @patatino( -; CHECK-NEXT: ret float fmul (float uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float), float uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float)) +; CHECK-NEXT: [[FMUL:%.*]] = fmul float uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float), uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float) +; CHECK-NEXT: ret float [[FMUL]] ; - %call = call float @fabsf(float fmul (float uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float), float uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float))) + %fmul = fmul float uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float), uitofp (i1 icmp eq (i16* getelementptr inbounds (i16, i16* @g2, i64 1), i16* @g1) to float) + %call = call float @fabsf(float %fmul) ret float %call }