/*========================== begin_copyright_notice ============================ Copyright (C) 2023 Intel Corporation SPDX-License-Identifier: MIT ============================= end_copyright_notice ===========================*/ /*========================== begin_copyright_notice ============================ Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. See https://llvm.org/LICENSE.txt for license information. SPDX-License-Identifier: Apache-2.0 with LLVM-exception ============================= end_copyright_notice ===========================*/ # TODO: Once upstreamed, update with LLORG revision & adjust per community review From 492a1c879f338c3f12ef4d2f619ca2c8f2467da8 Mon Sep 17 00:00:00 2001 From: Artem Gindinson Date: Wed, 23 Aug 2023 15:41:51 +0200 Subject: [PATCH] [InstCombine] Check for NaN before folding `select` for FP operators --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index ad96a5f47..88e8e7382 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -456,8 +456,12 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, // TODO: We probably ought to revisit cases where the select and FP // instructions have different flags and add tests to ensure the // behaviour is correct. - if (isa(&SI)) + if (isa(&SI)) { FMF = SI.getFastMathFlags(); + // Avoid folding on NaN inputs + if (!FMF.noNaNs()) + return nullptr; + } Constant *C = ConstantExpr::getBinOpIdentity( TVI->getOpcode(), TVI->getType(), true, FMF.noSignedZeros()); Value *OOp = TVI->getOperand(2 - OpToFold);