/*========================== 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 a7317169c80681f9128c22f5f071e206b7617c28 Mon Sep 17 00:00:00 2001 From: Artem Gindinson Date: Wed, 23 Aug 2023 15:49:30 +0200 Subject: [PATCH] [InstCombine] Check for NaN before folding `select` for FP operators --- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp index 881b00f2a..9cdde7433 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -455,6 +455,12 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, } if (OpToFold) { + FastMathFlags FMF; + if (isa(&SI)) { + FMF = SI.getFastMathFlags(); + if (!FMF.noNaNs()) + return nullptr; + } Constant *C = ConstantExpr::getBinOpIdentity(TVI->getOpcode(), TVI->getType(), true); Value *OOp = TVI->getOperand(2-OpToFold); @@ -487,6 +493,12 @@ Instruction *InstCombinerImpl::foldSelectIntoOp(SelectInst &SI, Value *TrueVal, } if (OpToFold) { + FastMathFlags FMF; + if (isa(&SI)) { + FMF = SI.getFastMathFlags(); + if (!FMF.noNaNs()) + return nullptr; + } Constant *C = ConstantExpr::getBinOpIdentity(FVI->getOpcode(), FVI->getType(), true); Value *OOp = FVI->getOperand(2-OpToFold);