Bug type: miscompilation
Component: ConstantFold
Bug location: llvm/lib/Analysis/ConstantFolding.cpp: llvm::ConstantFoldBinaryOpOperands
llvm::ConstantFoldCompareInstOperands
llvm::ConstantFoldFPInstOperands
llvm::FlushFPConstant
Issue title: `fcmp` miscompiled to a constant
Labels: miscompilation
floating-point
llvm:analysis
Reported by: bongjunj

Alive2 report: https://alive2.llvm.org/ce/z/324E7x

```llvm
----------------------------------------
define i1 @fcmp_double_dynamic_dynamic.2() denormal-fp-math=dynamic,dynamic {
#0:
  %cmp = fcmp une double 0x0008000000000000, -0.000000
  ret i1 %cmp
}
=>
define i1 @fcmp_double_dynamic_dynamic.2() nofree noundef willreturn denormal-fp-math=dynamic,dynamic memory(none) {
#0:
  ret i1 1
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:

Source:
i1 %cmp = #x0 (0)

Target:
Source value: #x0 (0)
Target value: #x1 (1)

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
```


Comments: author: arsenm
body: I don't see the issue? Those values are in fact not equal, so this is true. Alive bug? 
author: jcranmer-intel
body: it's comparing a denormal to 0, and the `denormal-fp-mode` is set to `dynamic`, so `fcmp` may or may not be flushing denormals to 0.
author: arsenm
body: InstCombine doesn't fold this, IPSCCP does 
author: nikic
body: I expect this is because we got through ValueLattice comparison, which does not have access to the denormal mode: https://github.com/llvm/llvm-project/blob/9501af5f92bb31fa9527d12a4b17b88d9fd651c4/llvm/lib/Analysis/ValueLattice.cpp#L28-L29

Really, the constant folding implementation should be defaulting to the conservatively correct behavior if the denormal mode is unknown.

Fix patch:
commit cd88bfcb5906049e1387b856fc7256e5fae22e5f
Author: Matt Arsenault <Matthew.Arsenault@amd.com>
Date:   Wed Nov 13 07:43:50 2024 -0800

    ConstantFolding: Do not fold fcmp of denormal without known mode (#115407)
    
    Fixes #114947

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 88db315ffd0b..1971c28fc4c4 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1266,14 +1266,16 @@ Constant *llvm::ConstantFoldCompareInstOperands(
     return ConstantFoldCompareInstOperands(Predicate, Ops1, Ops0, DL, TLI);
   }
 
-  // Flush any denormal constant float input according to denormal handling
-  // mode.
-  Ops0 = FlushFPConstant(Ops0, I, /* IsOutput */ false);
-  if (!Ops0)
-    return nullptr;
-  Ops1 = FlushFPConstant(Ops1, I, /* IsOutput */ false);
-  if (!Ops1)
-    return nullptr;
+  if (CmpInst::isFPPredicate(Predicate)) {
+    // Flush any denormal constant float input according to denormal handling
+    // mode.
+    Ops0 = FlushFPConstant(Ops0, I, /*IsOutput=*/false);
+    if (!Ops0)
+      return nullptr;
+    Ops1 = FlushFPConstant(Ops1, I, /*IsOutput=*/false);
+    if (!Ops1)
+      return nullptr;
+  }
 
   return ConstantFoldCompareInstruction(Predicate, Ops0, Ops1);
 }
@@ -1298,47 +1300,110 @@ Constant *llvm::ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS,
   return ConstantFoldBinaryInstruction(Opcode, LHS, RHS);
 }
 
-Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *I,
-                                bool IsOutput) {
-  if (!I || !I->getParent() || !I->getFunction())
-    return Operand;
+static ConstantFP *flushDenormalConstant(Type *Ty, const APFloat &APF,
+                                         DenormalMode::DenormalModeKind Mode) {
+  switch (Mode) {
+  case DenormalMode::Dynamic:
+    return nullptr;
+  case DenormalMode::IEEE:
+    return ConstantFP::get(Ty->getContext(), APF);
+  case DenormalMode::PreserveSign:
+    return ConstantFP::get(
+        Ty->getContext(),
+        APFloat::getZero(APF.getSemantics(), APF.isNegative()));
+  case DenormalMode::PositiveZero:
+    return ConstantFP::get(Ty->getContext(),
+                           APFloat::getZero(APF.getSemantics(), false));
+  default:
+    break;
+  }
 
-  ConstantFP *CFP = dyn_cast<ConstantFP>(Operand);
-  if (!CFP)
-    return Operand;
+  llvm_unreachable("unknown denormal mode");
+}
+
+/// Return the denormal mode that can be assumed when executing a floating point
+/// operation at \p CtxI.
+static DenormalMode getInstrDenormalMode(const Instruction *CtxI, Type *Ty) {
+  if (!CtxI || !CtxI->getParent() || !CtxI->getFunction())
+    return DenormalMode::getDynamic();
+  return CtxI->getFunction()->getDenormalMode(Ty->getFltSemantics());
+}
 
+static ConstantFP *flushDenormalConstantFP(ConstantFP *CFP,
+                                           const Instruction *Inst,
+                                           bool IsOutput) {
   const APFloat &APF = CFP->getValueAPF();
-  // TODO: Should this canonicalize nans?
   if (!APF.isDenormal())
-    return Operand;
+    return CFP;
 
-  Type *Ty = CFP->getType();
-  DenormalMode DenormMode =
-      I->getFunction()->getDenormalMode(Ty->getFltSemantics());
-  DenormalMode::DenormalModeKind Mode =
-      IsOutput ? DenormMode.Output : DenormMode.Input;
-  switch (Mode) {
-  default:
-    llvm_unreachable("unknown denormal mode");
-  case DenormalMode::Dynamic:
-    return nullptr;
-  case DenormalMode::IEEE:
+  DenormalMode Mode = getInstrDenormalMode(Inst, CFP->getType());
+  return flushDenormalConstant(CFP->getType(), APF,
+                               IsOutput ? Mode.Output : Mode.Input);
+}
+
+Constant *llvm::FlushFPConstant(Constant *Operand, const Instruction *Inst,
+                                bool IsOutput) {
+  if (ConstantFP *CFP = dyn_cast<ConstantFP>(Operand))
+    return flushDenormalConstantFP(CFP, Inst, IsOutput);
+
+  if (isa<ConstantAggregateZero, UndefValue, ConstantExpr>(Operand))
     return Operand;
-  case DenormalMode::PreserveSign:
-    if (APF.isDenormal()) {
-      return ConstantFP::get(
-          Ty->getContext(),
-          APFloat::getZero(Ty->getFltSemantics(), APF.isNegative()));
+
+  Type *Ty = Operand->getType();
+  VectorType *VecTy = dyn_cast<VectorType>(Ty);
+  if (VecTy) {
+    if (auto *Splat = dyn_cast_or_null<ConstantFP>(Operand->getSplatValue())) {
+      ConstantFP *Folded = flushDenormalConstantFP(Splat, Inst, IsOutput);
+      if (!Folded)
+        return nullptr;
+      return ConstantVector::getSplat(VecTy->getElementCount(), Folded);
     }
-    return Operand;
-  case DenormalMode::PositiveZero:
-    if (APF.isDenormal()) {
-      return ConstantFP::get(Ty->getContext(),
-                             APFloat::getZero(Ty->getFltSemantics(), false));
+
+    Ty = VecTy->getElementType();
+  }
+
+  if (const auto *CV = dyn_cast<ConstantVector>(Operand)) {
+    SmallVector<Constant *, 16> NewElts;
+    for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
+      Constant *Element = CV->getAggregateElement(i);
+      if (isa<UndefValue>(Element)) {
+        NewElts.push_back(Element);
+        continue;
+      }
+
+      ConstantFP *CFP = dyn_cast<ConstantFP>(Element);
+      if (!CFP)
+        return nullptr;
+
+      ConstantFP *Folded = flushDenormalConstantFP(CFP, Inst, IsOutput);
+      if (!Folded)
+        return nullptr;
+      NewElts.push_back(Folded);
     }
-    return Operand;
+
+    return ConstantVector::get(NewElts);
+  }
+
+  if (const auto *CDV = dyn_cast<ConstantDataVector>(Operand)) {
+    SmallVector<Constant *, 16> NewElts;
+    for (unsigned I = 0, E = CDV->getNumElements(); I < E; ++I) {
+      const APFloat &Elt = CDV->getElementAsAPFloat(I);
+      if (!Elt.isDenormal()) {
+        NewElts.push_back(ConstantFP::get(Ty, Elt));
+      } else {
+        DenormalMode Mode = getInstrDenormalMode(Inst, Ty);
+        ConstantFP *Folded =
+            flushDenormalConstant(Ty, Elt, IsOutput ? Mode.Output : Mode.Input);
+        if (!Folded)
+          return nullptr;
+        NewElts.push_back(Folded);
+      }
+    }
+
+    return ConstantVector::get(NewElts);
   }
-  return Operand;
+
+  return nullptr;
 }
 
 Constant *llvm::ConstantFoldFPInstOperands(unsigned Opcode, Constant *LHS,


Regression tests:
file: llvm/test/Transforms/SCCP/no-fold-fcmp-dynamic-denormal-mode-issue114947.ll
commands: opt -S -passes=ipsccp < %s
tests: test_name: no_fold_fcmp_denormal_double_ieee_dynamic_vector_nonsplat
test_body: define <2 x i1> @no_fold_fcmp_denormal_double_ieee_dynamic_vector_nonsplat() #0 {
  %cmp = fcmp une <2 x double> <double 0x8000000000000, double 1.000000e+00>, zeroinitializer
  ret <2 x i1> %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: fold_fcmp_nondenormal_double_ieee_dynamic_vector_nonsplat_undef
test_body: define <3 x i1> @fold_fcmp_nondenormal_double_ieee_dynamic_vector_nonsplat_undef() #0 {
  %cmp = fcmp une <3 x double> <double 2.000000e+00, double undef, double 4.000000e+00>, <double 1.000000e+00, double undef, double 4.000000e+00>
  ret <3 x i1> %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: no_fold_fcmp_denormal_double_ieee_dynamic_denormal_undef
test_body: define i1 @no_fold_fcmp_denormal_double_ieee_dynamic_denormal_undef() #0 {
  %cmp = fcmp une double 0x8000000000000, undef
  ret i1 %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: fold_fcmp_nondenormal_double_ieee_dynamic_vector_nonsplat
test_body: define <2 x i1> @fold_fcmp_nondenormal_double_ieee_dynamic_vector_nonsplat() #0 {
  %cmp = fcmp une <2 x double> <double 2.000000e+00, double 4.000000e+00>, <double 2.000000e+00, double 8.000000e+00>
  ret <2 x i1> %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: fold_fcmp_nondenormal_double_ieee_dynamic_vector_splat
test_body: define <2 x i1> @fold_fcmp_nondenormal_double_ieee_dynamic_vector_splat() #0 {
  %cmp = fcmp une <2 x double> splat (double 2.000000e+00), zeroinitializer
  ret <2 x i1> %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: fold_fcmp_nondenormal_double_ieee_dynamic
test_body: define i1 @fold_fcmp_nondenormal_double_ieee_dynamic() #0 {
  %cmp = fcmp une double 2.000000e+00, 0.000000e+00
  ret i1 %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: fold_fcmp_nondenormal_double_ieee_dynamic_zero
test_body: define <2 x i1> @fold_fcmp_nondenormal_double_ieee_dynamic_zero() #0 {
  %cmp = fcmp une <2 x double> zeroinitializer, zeroinitializer
  ret <2 x i1> %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: no_fold_fcmp_denormal_double_ieee_dynamic_denormal_poison
test_body: define i1 @no_fold_fcmp_denormal_double_ieee_dynamic_denormal_poison() #0 {
  %cmp = fcmp une double 0x8000000000000, poison
  ret i1 %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: no_fold_fcmp_denormal_double_ieee_dynamic_vector_splat
test_body: define <2 x i1> @no_fold_fcmp_denormal_double_ieee_dynamic_vector_splat() #0 {
  %cmp = fcmp une <2 x double> splat (double 0x8000000000000), zeroinitializer
  ret <2 x i1> %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }

test_name: no_fold_fcmp_denormal_double_ieee_dynamic
test_body: define i1 @no_fold_fcmp_denormal_double_ieee_dynamic() #0 {
  %cmp = fcmp une double 0x8000000000000, 0.000000e+00
  ret i1 %cmp
}

attributes #0 = { "denormal-fp-math"="ieee,dynamic" }