[llvm] [LLVM][IR] Add constant range support for floating-point types (PR #86483)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 09:07:23 PDT 2024


================
@@ -0,0 +1,231 @@
+//===- ConstantFPRange.cpp - ConstantFPRange implementation ---------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/IR/ConstantFPRange.h"
+#include "llvm/ADT/APFloat.h"
+#include "llvm/Analysis/ValueTracking.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+#include <cassert>
+
+using namespace llvm;
+
+void ConstantFPRange::makeEmpty() {
+  auto &Sem = Lower.getSemantics();
+  Lower = APFloat::getInf(Sem, /*Negative=*/false);
+  Upper = APFloat::getInf(Sem, /*Negative=*/true);
+  MayBeQNaN = false;
+  MayBeSNaN = false;
+}
+
+void ConstantFPRange::makeFull() {
+  auto &Sem = Lower.getSemantics();
+  Lower = APFloat::getInf(Sem, /*Negative=*/true);
+  Upper = APFloat::getInf(Sem, /*Negative=*/false);
+  MayBeQNaN = true;
+  MayBeSNaN = true;
+}
+
+bool ConstantFPRange::isNaNOnly() const {
+  return Lower.isPosInfinity() && Upper.isNegInfinity();
+}
+
+ConstantFPRange::ConstantFPRange(const fltSemantics &Sem, bool IsFullSet)
+    : Lower(Sem, APFloat::uninitialized), Upper(Sem, APFloat::uninitialized) {
+  Lower = APFloat::getInf(Sem, /*Negative=*/IsFullSet);
+  Upper = APFloat::getInf(Sem, /*Negative=*/!IsFullSet);
+  MayBeQNaN = IsFullSet;
+  MayBeSNaN = IsFullSet;
+}
+
+ConstantFPRange::ConstantFPRange(const APFloat &Value)
+    : Lower(Value.getSemantics(), APFloat::uninitialized),
+      Upper(Value.getSemantics(), APFloat::uninitialized) {
+  if (Value.isNaN()) {
+    makeEmpty();
+    bool IsSNaN = Value.isSignaling();
+    MayBeQNaN = !IsSNaN;
+    MayBeSNaN = IsSNaN;
+  } else {
+    Lower = Upper = Value;
+    MayBeQNaN = MayBeSNaN = false;
+  }
+}
+
+// We treat that -0 is less than 0 here.
+static APFloat::cmpResult strictCompare(const APFloat &LHS,
+                                        const APFloat &RHS) {
+  assert(!LHS.isNaN() && !RHS.isNaN() && "Unordered compare");
+  if (LHS.isZero() && RHS.isZero()) {
+    if (LHS.isNegative() == RHS.isNegative())
+      return APFloat::cmpEqual;
+    return LHS.isNegative() ? APFloat::cmpLessThan : APFloat::cmpGreaterThan;
+  }
+  return LHS.compare(RHS);
+}
+
+ConstantFPRange::ConstantFPRange(APFloat LowerVal, APFloat UpperVal,
+                                 bool MayBeQNaN, bool MayBeSNaN)
+    : Lower(std::move(LowerVal)), Upper(std::move(UpperVal)) {
+  // Canonicalize empty set into [Inf, -Inf].
+  if (strictCompare(Lower, Upper) == APFloat::cmpGreaterThan &&
+      !(Lower.isInfinity() && Upper.isInfinity()))
+    makeEmpty();
----------------
nikic wrote:

Can we make this an assertion instead? Passing upper < lower seems more likely to be a bug than an attempt to create a non-canonical empty range.

https://github.com/llvm/llvm-project/pull/86483


More information about the llvm-commits mailing list