[clang] 1a995a0 - [ADT] Move FixedPoint.h from Clang to LLVM.

Bevin Hansson via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 20 01:31:13 PDT 2020


Author: Bevin Hansson
Date: 2020-08-20T10:29:45+02:00
New Revision: 1a995a0af3c4e97c1135b6c9c3a243072ee0463c

URL: https://github.com/llvm/llvm-project/commit/1a995a0af3c4e97c1135b6c9c3a243072ee0463c
DIFF: https://github.com/llvm/llvm-project/commit/1a995a0af3c4e97c1135b6c9c3a243072ee0463c.diff

LOG: [ADT] Move FixedPoint.h from Clang to LLVM.

This patch moves FixedPointSemantics and APFixedPoint
from Clang to LLVM ADT.

This will make it easier to use the fixed-point
classes in LLVM for constructing an IR builder for
fixed-point and for reusing the APFixedPoint class
for constant evaluation purposes.

RFC: http://lists.llvm.org/pipermail/llvm-dev/2020-August/144025.html

Reviewed By: leonardchan, rjmccall

Differential Revision: https://reviews.llvm.org/D85312

Added: 
    llvm/include/llvm/ADT/APFixedPoint.h
    llvm/lib/Support/APFixedPoint.cpp
    llvm/unittests/ADT/APFixedPointTest.cpp

Modified: 
    clang/include/clang/AST/APValue.h
    clang/include/clang/AST/ASTContext.h
    clang/include/clang/AST/Expr.h
    clang/include/clang/AST/OptionalDiagnostic.h
    clang/lib/AST/ASTContext.cpp
    clang/lib/AST/ExprConstant.cpp
    clang/lib/AST/Type.cpp
    clang/lib/Basic/CMakeLists.txt
    clang/lib/CodeGen/CGExprScalar.cpp
    clang/lib/Sema/SemaChecking.cpp
    clang/lib/Sema/SemaExpr.cpp
    clang/lib/Serialization/ASTReader.cpp
    clang/lib/Serialization/ASTWriter.cpp
    clang/unittests/Basic/CMakeLists.txt
    llvm/lib/Support/CMakeLists.txt
    llvm/unittests/ADT/CMakeLists.txt

Removed: 
    clang/include/clang/Basic/FixedPoint.h
    clang/lib/Basic/FixedPoint.cpp
    clang/unittests/Basic/FixedPointTest.cpp


################################################################################
diff  --git a/clang/include/clang/AST/APValue.h b/clang/include/clang/AST/APValue.h
index cca92b5f8235..87e4bd7f84c1 100644
--- a/clang/include/clang/AST/APValue.h
+++ b/clang/include/clang/AST/APValue.h
@@ -13,8 +13,8 @@
 #ifndef LLVM_CLANG_AST_APVALUE_H
 #define LLVM_CLANG_AST_APVALUE_H
 
-#include "clang/Basic/FixedPoint.h"
 #include "clang/Basic/LLVM.h"
+#include "llvm/ADT/APFixedPoint.h"
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/PointerIntPair.h"
@@ -32,6 +32,7 @@ namespace clang {
   struct PrintingPolicy;
   class Type;
   class ValueDecl;
+  class QualType;
 
 /// Symbolic representation of typeid(T) for some type T.
 class TypeInfoLValue {
@@ -113,6 +114,7 @@ namespace clang {
 /// [APSInt] [APFloat], [Complex APSInt] [Complex APFloat], [Expr + Offset],
 /// [Vector: N * APValue], [Array: N * APValue]
 class APValue {
+  typedef llvm::APFixedPoint APFixedPoint;
   typedef llvm::APSInt APSInt;
   typedef llvm::APFloat APFloat;
 public:

diff  --git a/clang/include/clang/AST/ASTContext.h b/clang/include/clang/AST/ASTContext.h
index 3996644c9c85..b71f144d22a3 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -74,6 +74,8 @@
 
 namespace llvm {
 
+class APFixedPoint;
+class FixedPointSemantics;
 struct fltSemantics;
 template <typename T, unsigned N> class SmallPtrSet;
 
@@ -81,7 +83,6 @@ template <typename T, unsigned N> class SmallPtrSet;
 
 namespace clang {
 
-class APFixedPoint;
 class APValue;
 class ASTMutationListener;
 class ASTRecordLayout;
@@ -99,7 +100,6 @@ class ParentMapContext;
 class DynTypedNode;
 class DynTypedNodeList;
 class Expr;
-class FixedPointSemantics;
 class GlobalDecl;
 class MangleContext;
 class MangleNumberingContext;
@@ -1985,9 +1985,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
 
   unsigned char getFixedPointScale(QualType Ty) const;
   unsigned char getFixedPointIBits(QualType Ty) const;
-  FixedPointSemantics getFixedPointSemantics(QualType Ty) const;
-  APFixedPoint getFixedPointMax(QualType Ty) const;
-  APFixedPoint getFixedPointMin(QualType Ty) const;
+  llvm::FixedPointSemantics getFixedPointSemantics(QualType Ty) const;
+  llvm::APFixedPoint getFixedPointMax(QualType Ty) const;
+  llvm::APFixedPoint getFixedPointMin(QualType Ty) const;
 
   DeclarationNameInfo getNameForTemplate(TemplateName Name,
                                          SourceLocation NameLoc) const;

diff  --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index 96db7bc3be29..5edca2593789 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -24,7 +24,6 @@
 #include "clang/AST/TemplateBase.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/CharInfo.h"
-#include "clang/Basic/FixedPoint.h"
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/SyncScope.h"
 #include "clang/Basic/TypeTraits.h"

diff  --git a/clang/include/clang/AST/OptionalDiagnostic.h b/clang/include/clang/AST/OptionalDiagnostic.h
index c57199f0fdf1..c9a2d19f4ebc 100644
--- a/clang/include/clang/AST/OptionalDiagnostic.h
+++ b/clang/include/clang/AST/OptionalDiagnostic.h
@@ -63,7 +63,7 @@ class OptionalDiagnostic {
     return *this;
   }
 
-  OptionalDiagnostic &operator<<(const APFixedPoint &FX) {
+  OptionalDiagnostic &operator<<(const llvm::APFixedPoint &FX) {
     if (Diag) {
       SmallVector<char, 32> Buffer;
       FX.toString(Buffer);

diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index a9cab40247df..172a9018921f 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -51,7 +51,6 @@
 #include "clang/Basic/Builtins.h"
 #include "clang/Basic/CommentOptions.h"
 #include "clang/Basic/ExceptionSpecificationType.h"
-#include "clang/Basic/FixedPoint.h"
 #include "clang/Basic/IdentifierTable.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/LangOptions.h"
@@ -65,6 +64,7 @@
 #include "clang/Basic/TargetCXXABI.h"
 #include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/XRayLists.h"
+#include "llvm/ADT/APFixedPoint.h"
 #include "llvm/ADT/APInt.h"
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/ArrayRef.h"
@@ -11182,29 +11182,30 @@ unsigned char ASTContext::getFixedPointIBits(QualType Ty) const {
   }
 }
 
-FixedPointSemantics ASTContext::getFixedPointSemantics(QualType Ty) const {
+llvm::FixedPointSemantics
+ASTContext::getFixedPointSemantics(QualType Ty) const {
   assert((Ty->isFixedPointType() || Ty->isIntegerType()) &&
          "Can only get the fixed point semantics for a "
          "fixed point or integer type.");
   if (Ty->isIntegerType())
-    return FixedPointSemantics::GetIntegerSemantics(getIntWidth(Ty),
-                                                    Ty->isSignedIntegerType());
+    return llvm::FixedPointSemantics::GetIntegerSemantics(
+        getIntWidth(Ty), Ty->isSignedIntegerType());
 
   bool isSigned = Ty->isSignedFixedPointType();
-  return FixedPointSemantics(
+  return llvm::FixedPointSemantics(
       static_cast<unsigned>(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned,
       Ty->isSaturatedFixedPointType(),
       !isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding());
 }
 
-APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const {
+llvm::APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const {
   assert(Ty->isFixedPointType());
-  return APFixedPoint::getMax(getFixedPointSemantics(Ty));
+  return llvm::APFixedPoint::getMax(getFixedPointSemantics(Ty));
 }
 
-APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const {
+llvm::APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const {
   assert(Ty->isFixedPointType());
-  return APFixedPoint::getMin(getFixedPointSemantics(Ty));
+  return llvm::APFixedPoint::getMin(getFixedPointSemantics(Ty));
 }
 
 QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const {

diff  --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 760e5621e0ef..2c2dd6a3ef3a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -50,8 +50,8 @@
 #include "clang/AST/StmtVisitor.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/Builtins.h"
-#include "clang/Basic/FixedPoint.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/APFixedPoint.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/Support/Debug.h"
@@ -63,9 +63,11 @@
 #define DEBUG_TYPE "exprconstant"
 
 using namespace clang;
+using llvm::APFixedPoint;
 using llvm::APInt;
 using llvm::APSInt;
 using llvm::APFloat;
+using llvm::FixedPointSemantics;
 using llvm::Optional;
 
 namespace {

diff  --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index b94e12d65d7f..71e6db0cad7d 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -4337,10 +4337,10 @@ CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const {
 
 void clang::FixedPointValueToString(SmallVectorImpl<char> &Str,
                                     llvm::APSInt Val, unsigned Scale) {
-  FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(),
-                             /*IsSaturated=*/false,
-                             /*HasUnsignedPadding=*/false);
-  APFixedPoint(Val, FXSema).toString(Str);
+  llvm::FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(),
+                                   /*IsSaturated=*/false,
+                                   /*HasUnsignedPadding=*/false);
+  llvm::APFixedPoint(Val, FXSema).toString(Str);
 }
 
 AutoType::AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,

diff  --git a/clang/lib/Basic/CMakeLists.txt b/clang/lib/Basic/CMakeLists.txt
index 5a941e15eff2..9d78409483d3 100644
--- a/clang/lib/Basic/CMakeLists.txt
+++ b/clang/lib/Basic/CMakeLists.txt
@@ -48,7 +48,6 @@ add_clang_library(clangBasic
   ExpressionTraits.cpp
   FileManager.cpp
   FileSystemStatCache.cpp
-  FixedPoint.cpp
   IdentifierTable.cpp
   LangOptions.cpp
   LangStandards.cpp

diff  --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 6131f97995dc..aad4d2d2a674 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -26,8 +26,8 @@
 #include "clang/AST/RecordLayout.h"
 #include "clang/AST/StmtVisitor.h"
 #include "clang/Basic/CodeGenOptions.h"
-#include "clang/Basic/FixedPoint.h"
 #include "clang/Basic/TargetInfo.h"
+#include "llvm/ADT/APFixedPoint.h"
 #include "llvm/ADT/Optional.h"
 #include "llvm/IR/CFG.h"
 #include "llvm/IR/Constants.h"
@@ -356,8 +356,9 @@ class ScalarExprEmitter
   /// and an integer.
   Value *EmitFixedPointConversion(Value *Src, QualType SrcTy, QualType DstTy,
                                   SourceLocation Loc);
-  Value *EmitFixedPointConversion(Value *Src, FixedPointSemantics &SrcFixedSema,
-                                  FixedPointSemantics &DstFixedSema,
+  Value *EmitFixedPointConversion(Value *Src,
+                                  llvm::FixedPointSemantics &SrcFixedSema,
+                                  llvm::FixedPointSemantics &DstFixedSema,
                                   SourceLocation Loc,
                                   bool DstIsInteger = false);
 
@@ -1444,17 +1445,17 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
 Value *ScalarExprEmitter::EmitFixedPointConversion(Value *Src, QualType SrcTy,
                                                    QualType DstTy,
                                                    SourceLocation Loc) {
-  FixedPointSemantics SrcFPSema =
-      CGF.getContext().getFixedPointSemantics(SrcTy);
-  FixedPointSemantics DstFPSema =
-      CGF.getContext().getFixedPointSemantics(DstTy);
+  auto SrcFPSema = CGF.getContext().getFixedPointSemantics(SrcTy);
+  auto DstFPSema = CGF.getContext().getFixedPointSemantics(DstTy);
   return EmitFixedPointConversion(Src, SrcFPSema, DstFPSema, Loc,
                                   DstTy->isIntegerType());
 }
 
 Value *ScalarExprEmitter::EmitFixedPointConversion(
-    Value *Src, FixedPointSemantics &SrcFPSema, FixedPointSemantics &DstFPSema,
+    Value *Src, llvm::FixedPointSemantics &SrcFPSema,
+    llvm::FixedPointSemantics &DstFPSema,
     SourceLocation Loc, bool DstIsInteger) {
+  using llvm::APFixedPoint;
   using llvm::APInt;
   using llvm::ConstantInt;
   using llvm::Value;
@@ -2667,12 +2668,10 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
     // Now, convert from our invented integer literal to the type of the unary
     // op. This will upscale and saturate if necessary. This value can become
     // undef in some cases.
-    FixedPointSemantics SrcSema =
-        FixedPointSemantics::GetIntegerSemantics(value->getType()
-                                                      ->getScalarSizeInBits(),
-                                                 /*IsSigned=*/true);
-    FixedPointSemantics DstSema =
-        CGF.getContext().getFixedPointSemantics(Info.Ty);
+    auto SrcSema =
+        llvm::FixedPointSemantics::GetIntegerSemantics(
+            value->getType()->getScalarSizeInBits(), /*IsSigned=*/true);
+    auto DstSema = CGF.getContext().getFixedPointSemantics(Info.Ty);
     Info.RHS = EmitFixedPointConversion(Info.RHS, SrcSema, DstSema,
                                         E->getExprLoc());
     value = EmitFixedPointBinOp(Info);

diff  --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index deceffdb0ba5..df24931d058b 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11843,9 +11843,9 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
       Expr::EvalResult Result;
       if (E->EvaluateAsFixedPoint(Result, S.Context, Expr::SE_AllowSideEffects,
                                   S.isConstantEvaluated())) {
-        APFixedPoint Value = Result.Val.getFixedPoint();
-        APFixedPoint MaxVal = S.Context.getFixedPointMax(T);
-        APFixedPoint MinVal = S.Context.getFixedPointMin(T);
+        llvm::APFixedPoint Value = Result.Val.getFixedPoint();
+        llvm::APFixedPoint MaxVal = S.Context.getFixedPointMax(T);
+        llvm::APFixedPoint MinVal = S.Context.getFixedPointMin(T);
         if (Value > MaxVal || Value < MinVal) {
           S.DiagRuntimeBehavior(E->getExprLoc(), E,
                                 S.PDiag(diag::warn_impcast_fixed_point_range)
@@ -11860,7 +11860,7 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
       if (!S.isConstantEvaluated() &&
           E->EvaluateAsFixedPoint(Result, S.Context,
                                   Expr::SE_AllowSideEffects)) {
-        APFixedPoint FXResult = Result.Val.getFixedPoint();
+        llvm::APFixedPoint FXResult = Result.Val.getFixedPoint();
 
         bool Overflowed;
         llvm::APSInt IntResult = FXResult.convertToInt(
@@ -11885,7 +11885,7 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
         llvm::APSInt Value = Result.Val.getInt();
 
         bool Overflowed;
-        APFixedPoint IntResult = APFixedPoint::getFromIntValue(
+        llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue(
             Value, S.Context.getFixedPointSemantics(T), &Overflowed);
 
         if (Overflowed) {

diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 39ed25f3ef96..0df54c0ef985 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -27,7 +27,6 @@
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/TypeLoc.h"
 #include "clang/Basic/Builtins.h"
-#include "clang/Basic/FixedPoint.h"
 #include "clang/Basic/PartialDiagnostic.h"
 #include "clang/Basic/SourceManager.h"
 #include "clang/Basic/TargetInfo.h"
@@ -10610,7 +10609,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
   if (LHSExprType->isExtIntType())
     LeftSize = S.Context.getIntWidth(LHSExprType);
   else if (LHSExprType->isFixedPointType()) {
-    FixedPointSemantics FXSema = S.Context.getFixedPointSemantics(LHSExprType);
+    auto FXSema = S.Context.getFixedPointSemantics(LHSExprType);
     LeftSize = FXSema.getWidth() - (unsigned)FXSema.hasUnsignedPadding();
   }
   llvm::APInt LeftBits(Right.getBitWidth(), LeftSize);

diff  --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp
index 2b4679b79d11..21cdde9e8455 100644
--- a/clang/lib/Serialization/ASTReader.cpp
+++ b/clang/lib/Serialization/ASTReader.cpp
@@ -8941,7 +8941,7 @@ ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
   return SourceRange(beg, end);
 }
 
-static FixedPointSemantics
+static llvm::FixedPointSemantics
 ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
                         unsigned &Idx) {
   unsigned Width = Record[Idx++];
@@ -8950,8 +8950,8 @@ ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
   bool IsSigned = Tmp & 0x1;
   bool IsSaturated = Tmp & 0x2;
   bool HasUnsignedPadding = Tmp & 0x4;
-  return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
-                             HasUnsignedPadding);
+  return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
+                                   HasUnsignedPadding);
 }
 
 static const llvm::fltSemantics &
@@ -8974,8 +8974,8 @@ APValue ASTRecordReader::readAPValue() {
     return APValue(readAPFloat(FloatSema));
   }
   case APValue::FixedPoint: {
-    FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
-    return APValue(APFixedPoint(readAPInt(), FPSema));
+    llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
+    return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
   }
   case APValue::ComplexInt: {
     llvm::APSInt First = readAPSInt();

diff  --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp
index a896df2e8358..21dd8534a6f7 100644
--- a/clang/lib/Serialization/ASTWriter.cpp
+++ b/clang/lib/Serialization/ASTWriter.cpp
@@ -5127,7 +5127,7 @@ void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
 }
 
 static void WriteFixedPointSemantics(ASTRecordWriter &Record,
-                                     FixedPointSemantics FPSema) {
+                                     llvm::FixedPointSemantics FPSema) {
   Record.push_back(FPSema.getWidth());
   Record.push_back(FPSema.getScale());
   Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |

diff  --git a/clang/unittests/Basic/CMakeLists.txt b/clang/unittests/Basic/CMakeLists.txt
index a54e9397ad17..27e49ff6e0b9 100644
--- a/clang/unittests/Basic/CMakeLists.txt
+++ b/clang/unittests/Basic/CMakeLists.txt
@@ -6,7 +6,6 @@ add_clang_unittest(BasicTests
   CharInfoTest.cpp
   DiagnosticTest.cpp
   FileManagerTest.cpp
-  FixedPointTest.cpp
   SourceManagerTest.cpp
   )
 

diff  --git a/clang/include/clang/Basic/FixedPoint.h b/llvm/include/llvm/ADT/APFixedPoint.h
similarity index 86%
rename from clang/include/clang/Basic/FixedPoint.h
rename to llvm/include/llvm/ADT/APFixedPoint.h
index ee52a2b0a615..f63ff15cdf7d 100644
--- a/clang/include/clang/Basic/FixedPoint.h
+++ b/llvm/include/llvm/ADT/APFixedPoint.h
@@ -1,4 +1,4 @@
-//===- FixedPoint.h - Fixed point constant handling -------------*- C++ -*-===//
+//===- APFixedPoint.h - Fixed point constant handling -----------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -9,23 +9,20 @@
 /// \file
 /// Defines the fixed point number interface.
 /// This is a class for abstracting various operations performed on fixed point
-/// types described in ISO/IEC JTC1 SC22 WG14 N1169 starting at clause 4.
+/// types.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_BASIC_FIXEDPOINT_H
-#define LLVM_CLANG_BASIC_FIXEDPOINT_H
+#ifndef LLVM_ADT_APFIXEDPOINT_H
+#define LLVM_ADT_APFIXEDPOINT_H
 
 #include "llvm/ADT/APSInt.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/raw_ostream.h"
 
-namespace clang {
-
-class ASTContext;
-class QualType;
+namespace llvm {
 
-/// The fixed point semantics work similarly to llvm::fltSemantics. The width
+/// The fixed point semantics work similarly to fltSemantics. The width
 /// specifies the whole bit width of the underlying scaled integer (with padding
 /// if any). The scale represents the number of fractional bits in this type.
 /// When HasUnsignedPadding is true and this type is unsigned, the first bit
@@ -88,26 +85,21 @@ class FixedPointSemantics {
 /// info about the fixed point type's width, sign, scale, and saturation, and
 /// provides 
diff erent operations that would normally be performed on fixed point
 /// types.
-///
-/// Semantically this does not represent any existing C type other than fixed
-/// point types and should eventually be moved to LLVM if fixed point types gain
-/// native IR support.
 class APFixedPoint {
 public:
-  APFixedPoint(const llvm::APInt &Val, const FixedPointSemantics &Sema)
+  APFixedPoint(const APInt &Val, const FixedPointSemantics &Sema)
       : Val(Val, !Sema.isSigned()), Sema(Sema) {
     assert(Val.getBitWidth() == Sema.getWidth() &&
            "The value should have a bit width that matches the Sema width");
   }
 
   APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema)
-      : APFixedPoint(llvm::APInt(Sema.getWidth(), Val, Sema.isSigned()),
-                     Sema) {}
+      : APFixedPoint(APInt(Sema.getWidth(), Val, Sema.isSigned()), Sema) {}
 
   // Zero initialization.
   APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}
 
-  llvm::APSInt getValue() const { return llvm::APSInt(Val, !Sema.isSigned()); }
+  APSInt getValue() const { return APSInt(Val, !Sema.isSigned()); }
   inline unsigned getWidth() const { return Sema.getWidth(); }
   inline unsigned getScale() const { return Sema.getScale(); }
   inline bool isSaturated() const { return Sema.isSaturated(); }
@@ -149,7 +141,7 @@ class APFixedPoint {
 
   /// Return the integral part of this fixed point number, rounded towards
   /// zero. (-2.5k -> -2)
-  llvm::APSInt getIntPart() const {
+  APSInt getIntPart() const {
     if (Val < 0 && Val != -Val) // Cover the case when we have the min val
       return -(-Val >> getScale());
     else
@@ -164,12 +156,12 @@ class APFixedPoint {
   ///
   /// If the overflow parameter is provided, set this value to true or false to
   /// indicate if this operation results in an overflow.
-  llvm::APSInt convertToInt(unsigned DstWidth, bool DstSign,
-                            bool *Overflow = nullptr) const;
+  APSInt convertToInt(unsigned DstWidth, bool DstSign,
+                      bool *Overflow = nullptr) const;
 
-  void toString(llvm::SmallVectorImpl<char> &Str) const;
+  void toString(SmallVectorImpl<char> &Str) const;
   std::string toString() const {
-    llvm::SmallString<40> S;
+    SmallString<40> S;
     toString(S);
     return std::string(S.str());
   }
@@ -198,21 +190,20 @@ class APFixedPoint {
   /// and in the same semantics as the provided target semantics. If the value
   /// is not able to fit in the specified fixed point semantics, and the
   /// overflow parameter is provided, it is set to true.
-  static APFixedPoint getFromIntValue(const llvm::APSInt &Value,
+  static APFixedPoint getFromIntValue(const APSInt &Value,
                                       const FixedPointSemantics &DstFXSema,
                                       bool *Overflow = nullptr);
 
 private:
-  llvm::APSInt Val;
+  APSInt Val;
   FixedPointSemantics Sema;
 };
 
-inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
-                                     const APFixedPoint &FX) {
+inline raw_ostream &operator<<(raw_ostream &OS, const APFixedPoint &FX) {
   OS << FX.toString();
   return OS;
 }
 
-}  // namespace clang
+}  // namespace llvm
 
 #endif

diff  --git a/clang/lib/Basic/FixedPoint.cpp b/llvm/lib/Support/APFixedPoint.cpp
similarity index 82%
rename from clang/lib/Basic/FixedPoint.cpp
rename to llvm/lib/Support/APFixedPoint.cpp
index 892b8c19fd79..ebb434276634 100644
--- a/clang/lib/Basic/FixedPoint.cpp
+++ b/llvm/lib/Support/APFixedPoint.cpp
@@ -1,4 +1,4 @@
-//===- FixedPoint.cpp - Fixed point constant handling -----------*- C++ -*-===//
+//===- APFixedPoint.cpp - Fixed point constant handling ---------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -11,13 +11,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Basic/FixedPoint.h"
+#include "llvm/ADT/APFixedPoint.h"
 
-namespace clang {
+namespace llvm {
 
 APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema,
                                    bool *Overflow) const {
-  llvm::APSInt NewVal = Val;
+  APSInt NewVal = Val;
   unsigned DstWidth = DstSema.getWidth();
   unsigned DstScale = DstSema.getScale();
   bool Upscaling = DstScale > getScale();
@@ -31,10 +31,10 @@ APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema,
     NewVal >>= (getScale() - DstScale);
   }
 
-  auto Mask = llvm::APInt::getBitsSetFrom(
+  auto Mask = APInt::getBitsSetFrom(
       NewVal.getBitWidth(),
       std::min(DstScale + DstSema.getIntegralBits(), NewVal.getBitWidth()));
-  llvm::APInt Masked(NewVal & Mask);
+  APInt Masked(NewVal & Mask);
 
   // Change in the bits above the sign
   if (!(Masked == Mask || Masked == 0)) {
@@ -61,8 +61,8 @@ APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema,
 }
 
 int APFixedPoint::compare(const APFixedPoint &Other) const {
-  llvm::APSInt ThisVal = getValue();
-  llvm::APSInt OtherVal = Other.getValue();
+  APSInt ThisVal = getValue();
+  APSInt OtherVal = Other.getValue();
   bool ThisSigned = Val.isSigned();
   bool OtherSigned = OtherVal.isSigned();
   unsigned OtherScale = Other.getScale();
@@ -113,14 +113,14 @@ int APFixedPoint::compare(const APFixedPoint &Other) const {
 
 APFixedPoint APFixedPoint::getMax(const FixedPointSemantics &Sema) {
   bool IsUnsigned = !Sema.isSigned();
-  auto Val = llvm::APSInt::getMaxValue(Sema.getWidth(), IsUnsigned);
+  auto Val = APSInt::getMaxValue(Sema.getWidth(), IsUnsigned);
   if (IsUnsigned && Sema.hasUnsignedPadding())
     Val = Val.lshr(1);
   return APFixedPoint(Val, Sema);
 }
 
 APFixedPoint APFixedPoint::getMin(const FixedPointSemantics &Sema) {
-  auto Val = llvm::APSInt::getMinValue(Sema.getWidth(), !Sema.isSigned());
+  auto Val = APSInt::getMinValue(Sema.getWidth(), !Sema.isSigned());
   return APFixedPoint(Val, Sema);
 }
 
@@ -154,11 +154,11 @@ APFixedPoint APFixedPoint::add(const APFixedPoint &Other,
   auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics());
   APFixedPoint ConvertedThis = convert(CommonFXSema);
   APFixedPoint ConvertedOther = Other.convert(CommonFXSema);
-  llvm::APSInt ThisVal = ConvertedThis.getValue();
-  llvm::APSInt OtherVal = ConvertedOther.getValue();
+  APSInt ThisVal = ConvertedThis.getValue();
+  APSInt OtherVal = ConvertedOther.getValue();
   bool Overflowed = false;
 
-  llvm::APSInt Result;
+  APSInt Result;
   if (CommonFXSema.isSaturated()) {
     Result = CommonFXSema.isSigned() ? ThisVal.sadd_sat(OtherVal)
                                      : ThisVal.uadd_sat(OtherVal);
@@ -178,11 +178,11 @@ APFixedPoint APFixedPoint::sub(const APFixedPoint &Other,
   auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics());
   APFixedPoint ConvertedThis = convert(CommonFXSema);
   APFixedPoint ConvertedOther = Other.convert(CommonFXSema);
-  llvm::APSInt ThisVal = ConvertedThis.getValue();
-  llvm::APSInt OtherVal = ConvertedOther.getValue();
+  APSInt ThisVal = ConvertedThis.getValue();
+  APSInt OtherVal = ConvertedOther.getValue();
   bool Overflowed = false;
 
-  llvm::APSInt Result;
+  APSInt Result;
   if (CommonFXSema.isSaturated()) {
     Result = CommonFXSema.isSigned() ? ThisVal.ssub_sat(OtherVal)
                                      : ThisVal.usub_sat(OtherVal);
@@ -202,8 +202,8 @@ APFixedPoint APFixedPoint::mul(const APFixedPoint &Other,
   auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics());
   APFixedPoint ConvertedThis = convert(CommonFXSema);
   APFixedPoint ConvertedOther = Other.convert(CommonFXSema);
-  llvm::APSInt ThisVal = ConvertedThis.getValue();
-  llvm::APSInt OtherVal = ConvertedOther.getValue();
+  APSInt ThisVal = ConvertedThis.getValue();
+  APSInt OtherVal = ConvertedOther.getValue();
   bool Overflowed = false;
 
   // Widen the LHS and RHS so we can perform a full multiplication.
@@ -223,7 +223,7 @@ APFixedPoint APFixedPoint::mul(const APFixedPoint &Other,
   // outside the representable range. We interpret the spec as allowing us to
   // perform the rounding step first, avoiding the overflow case that would
   // arise.
-  llvm::APSInt Result;
+  APSInt Result;
   if (CommonFXSema.isSigned())
     Result = ThisVal.smul_ov(OtherVal, Overflowed)
                     .ashr(CommonFXSema.getScale());
@@ -235,10 +235,10 @@ APFixedPoint APFixedPoint::mul(const APFixedPoint &Other,
 
   // If our result lies outside of the representative range of the common
   // semantic, we either have overflow or saturation.
-  llvm::APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue()
-                                                       .extOrTrunc(Wide);
-  llvm::APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue()
-                                                       .extOrTrunc(Wide);
+  APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue()
+                                                 .extOrTrunc(Wide);
+  APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue()
+                                                 .extOrTrunc(Wide);
   if (CommonFXSema.isSaturated()) {
     if (Result < Min)
       Result = Min;
@@ -259,8 +259,8 @@ APFixedPoint APFixedPoint::div(const APFixedPoint &Other,
   auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics());
   APFixedPoint ConvertedThis = convert(CommonFXSema);
   APFixedPoint ConvertedOther = Other.convert(CommonFXSema);
-  llvm::APSInt ThisVal = ConvertedThis.getValue();
-  llvm::APSInt OtherVal = ConvertedOther.getValue();
+  APSInt ThisVal = ConvertedThis.getValue();
+  APSInt OtherVal = ConvertedOther.getValue();
   bool Overflowed = false;
 
   // Widen the LHS and RHS so we can perform a full division.
@@ -276,10 +276,10 @@ APFixedPoint APFixedPoint::div(const APFixedPoint &Other,
   // Upscale to compensate for the loss of precision from division, and
   // perform the full division.
   ThisVal = ThisVal.shl(CommonFXSema.getScale());
-  llvm::APSInt Result;
+  APSInt Result;
   if (CommonFXSema.isSigned()) {
-    llvm::APInt Rem;
-    llvm::APInt::sdivrem(ThisVal, OtherVal, Result, Rem);
+    APInt Rem;
+    APInt::sdivrem(ThisVal, OtherVal, Result, Rem);
     // If the quotient is negative and the remainder is nonzero, round
     // towards negative infinity by subtracting epsilon from the result.
     if (ThisVal.isNegative() != OtherVal.isNegative() && !Rem.isNullValue())
@@ -290,10 +290,10 @@ APFixedPoint APFixedPoint::div(const APFixedPoint &Other,
 
   // If our result lies outside of the representative range of the common
   // semantic, we either have overflow or saturation.
-  llvm::APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue()
-                                                       .extOrTrunc(Wide);
-  llvm::APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue()
-                                                       .extOrTrunc(Wide);
+  APSInt Max = APFixedPoint::getMax(CommonFXSema).getValue()
+                                                 .extOrTrunc(Wide);
+  APSInt Min = APFixedPoint::getMin(CommonFXSema).getValue()
+                                                 .extOrTrunc(Wide);
   if (CommonFXSema.isSaturated()) {
     if (Result < Min)
       Result = Min;
@@ -310,7 +310,7 @@ APFixedPoint APFixedPoint::div(const APFixedPoint &Other,
 }
 
 APFixedPoint APFixedPoint::shl(unsigned Amt, bool *Overflow) const {
-  llvm::APSInt ThisVal = Val;
+  APSInt ThisVal = Val;
   bool Overflowed = false;
 
   // Widen the LHS.
@@ -322,13 +322,13 @@ APFixedPoint APFixedPoint::shl(unsigned Amt, bool *Overflow) const {
 
   // Clamp the shift amount at the original width, and perform the shift.
   Amt = std::min(Amt, ThisVal.getBitWidth());
-  llvm::APSInt Result = ThisVal << Amt;
+  APSInt Result = ThisVal << Amt;
   Result.setIsSigned(Sema.isSigned());
 
   // If our result lies outside of the representative range of the
   // semantic, we either have overflow or saturation.
-  llvm::APSInt Max = APFixedPoint::getMax(Sema).getValue().extOrTrunc(Wide);
-  llvm::APSInt Min = APFixedPoint::getMin(Sema).getValue().extOrTrunc(Wide);
+  APSInt Max = APFixedPoint::getMax(Sema).getValue().extOrTrunc(Wide);
+  APSInt Min = APFixedPoint::getMin(Sema).getValue().extOrTrunc(Wide);
   if (Sema.isSaturated()) {
     if (Result < Min)
       Result = Min;
@@ -343,8 +343,8 @@ APFixedPoint APFixedPoint::shl(unsigned Amt, bool *Overflow) const {
   return APFixedPoint(Result.sextOrTrunc(Sema.getWidth()), Sema);
 }
 
-void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const {
-  llvm::APSInt Val = getValue();
+void APFixedPoint::toString(SmallVectorImpl<char> &Str) const {
+  APSInt Val = getValue();
   unsigned Scale = getScale();
 
   if (Val.isSigned() && Val.isNegative() && Val != -Val) {
@@ -352,13 +352,13 @@ void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const {
     Str.push_back('-');
   }
 
-  llvm::APSInt IntPart = Val >> Scale;
+  APSInt IntPart = Val >> Scale;
 
   // Add 4 digits to hold the value after multiplying 10 (the radix)
   unsigned Width = Val.getBitWidth() + 4;
-  llvm::APInt FractPart = Val.zextOrTrunc(Scale).zext(Width);
-  llvm::APInt FractPartMask = llvm::APInt::getAllOnesValue(Scale).zext(Width);
-  llvm::APInt RadixInt = llvm::APInt(Width, 10);
+  APInt FractPart = Val.zextOrTrunc(Scale).zext(Width);
+  APInt FractPartMask = APInt::getAllOnesValue(Scale).zext(Width);
+  APInt RadixInt = APInt(Width, 10);
 
   IntPart.toString(Str, /*Radix=*/10);
   Str.push_back('.');
@@ -388,13 +388,13 @@ APFixedPoint APFixedPoint::negate(bool *Overflow) const {
     return APFixedPoint(Sema);
 }
 
-llvm::APSInt APFixedPoint::convertToInt(unsigned DstWidth, bool DstSign,
-                                        bool *Overflow) const {
-  llvm::APSInt Result = getIntPart();
+APSInt APFixedPoint::convertToInt(unsigned DstWidth, bool DstSign,
+                                  bool *Overflow) const {
+  APSInt Result = getIntPart();
   unsigned SrcWidth = getWidth();
 
-  llvm::APSInt DstMin = llvm::APSInt::getMinValue(DstWidth, !DstSign);
-  llvm::APSInt DstMax = llvm::APSInt::getMaxValue(DstWidth, !DstSign);
+  APSInt DstMin = APSInt::getMinValue(DstWidth, !DstSign);
+  APSInt DstMax = APSInt::getMaxValue(DstWidth, !DstSign);
 
   if (SrcWidth < DstWidth) {
     Result = Result.extend(DstWidth);
@@ -417,7 +417,7 @@ llvm::APSInt APFixedPoint::convertToInt(unsigned DstWidth, bool DstSign,
   return Result.extOrTrunc(DstWidth);
 }
 
-APFixedPoint APFixedPoint::getFromIntValue(const llvm::APSInt &Value,
+APFixedPoint APFixedPoint::getFromIntValue(const APSInt &Value,
                                            const FixedPointSemantics &DstFXSema,
                                            bool *Overflow) {
   FixedPointSemantics IntFXSema = FixedPointSemantics::GetIntegerSemantics(

diff  --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt
index e6ff814590d9..a571d269b39f 100644
--- a/llvm/lib/Support/CMakeLists.txt
+++ b/llvm/lib/Support/CMakeLists.txt
@@ -67,6 +67,7 @@ add_llvm_component_library(LLVMSupport
   ABIBreak.cpp
   ARMTargetParser.cpp
   AMDGPUMetadata.cpp
+  APFixedPoint.cpp
   APFloat.cpp
   APInt.cpp
   APSInt.cpp

diff  --git a/clang/unittests/Basic/FixedPointTest.cpp b/llvm/unittests/ADT/APFixedPointTest.cpp
similarity index 99%
rename from clang/unittests/Basic/FixedPointTest.cpp
rename to llvm/unittests/ADT/APFixedPointTest.cpp
index 5d991c072035..02e80ca7380d 100644
--- a/clang/unittests/Basic/FixedPointTest.cpp
+++ b/llvm/unittests/ADT/APFixedPointTest.cpp
@@ -1,4 +1,4 @@
-//===- unittests/Basic/FixedPointTest.cpp -- fixed point number tests -----===//
+//===- unittests/ADT/FixedPointTest.cpp -- fixed point number tests -----===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Basic/FixedPoint.h"
+#include "llvm/ADT/APFixedPoint.h"
 #include "llvm/ADT/APSInt.h"
 #include "gtest/gtest.h"
 
-using clang::APFixedPoint;
-using clang::FixedPointSemantics;
+using llvm::APFixedPoint;
+using llvm::FixedPointSemantics;
 using llvm::APInt;
 using llvm::APSInt;
 

diff  --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt
index cff3911e339f..51f1b114f5cb 100644
--- a/llvm/unittests/ADT/CMakeLists.txt
+++ b/llvm/unittests/ADT/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_llvm_unittest(ADTTests
   AnyTest.cpp
+  APFixedPointTest.cpp
   APFloatTest.cpp
   APIntTest.cpp
   APSIntTest.cpp


        


More information about the cfe-commits mailing list