Skip to content

Commit

Permalink
[ADT] Move FixedPoint.h from Clang to LLVM.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bevin-hansson committed Aug 20, 2020
1 parent 1e7ec48 commit 1a995a0
Show file tree
Hide file tree
Showing 19 changed files with 123 additions and 130 deletions.
4 changes: 3 additions & 1 deletion clang/include/clang/AST/APValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,15 @@

namespace llvm {

class APFixedPoint;
class FixedPointSemantics;
struct fltSemantics;
template <typename T, unsigned N> class SmallPtrSet;

} // namespace llvm

namespace clang {

class APFixedPoint;
class APValue;
class ASTMutationListener;
class ASTRecordLayout;
Expand All @@ -99,7 +100,6 @@ class ParentMapContext;
class DynTypedNode;
class DynTypedNodeList;
class Expr;
class FixedPointSemantics;
class GlobalDecl;
class MangleContext;
class MangleNumberingContext;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/AST/OptionalDiagnostic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 10 additions & 9 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion clang/lib/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ add_clang_library(clangBasic
ExpressionTraits.cpp
FileManager.cpp
FileSystemStatCache.cpp
FixedPoint.cpp
IdentifierTable.cpp
LangOptions.cpp
LangStandards.cpp
Expand Down
27 changes: 13 additions & 14 deletions clang/lib/CodeGen/CGExprScalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/SemaExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions clang/lib/Serialization/ASTReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++];
Expand All @@ -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 &
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
1 change: 0 additions & 1 deletion clang/unittests/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ add_clang_unittest(BasicTests
CharInfoTest.cpp
DiagnosticTest.cpp
FileManagerTest.cpp
FixedPointTest.cpp
SourceManagerTest.cpp
)

Expand Down
Loading

0 comments on commit 1a995a0

Please sign in to comment.