Skip to content

Commit 1a995a0

Browse files
committed
[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
1 parent 1e7ec48 commit 1a995a0

File tree

19 files changed

+123
-130
lines changed

19 files changed

+123
-130
lines changed

clang/include/clang/AST/APValue.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#ifndef LLVM_CLANG_AST_APVALUE_H
1414
#define LLVM_CLANG_AST_APVALUE_H
1515

16-
#include "clang/Basic/FixedPoint.h"
1716
#include "clang/Basic/LLVM.h"
17+
#include "llvm/ADT/APFixedPoint.h"
1818
#include "llvm/ADT/APFloat.h"
1919
#include "llvm/ADT/APSInt.h"
2020
#include "llvm/ADT/PointerIntPair.h"
@@ -32,6 +32,7 @@ namespace clang {
3232
struct PrintingPolicy;
3333
class Type;
3434
class ValueDecl;
35+
class QualType;
3536

3637
/// Symbolic representation of typeid(T) for some type T.
3738
class TypeInfoLValue {
@@ -113,6 +114,7 @@ namespace clang {
113114
/// [APSInt] [APFloat], [Complex APSInt] [Complex APFloat], [Expr + Offset],
114115
/// [Vector: N * APValue], [Array: N * APValue]
115116
class APValue {
117+
typedef llvm::APFixedPoint APFixedPoint;
116118
typedef llvm::APSInt APSInt;
117119
typedef llvm::APFloat APFloat;
118120
public:

clang/include/clang/AST/ASTContext.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@
7474

7575
namespace llvm {
7676

77+
class APFixedPoint;
78+
class FixedPointSemantics;
7779
struct fltSemantics;
7880
template <typename T, unsigned N> class SmallPtrSet;
7981

8082
} // namespace llvm
8183

8284
namespace clang {
8385

84-
class APFixedPoint;
8586
class APValue;
8687
class ASTMutationListener;
8788
class ASTRecordLayout;
@@ -99,7 +100,6 @@ class ParentMapContext;
99100
class DynTypedNode;
100101
class DynTypedNodeList;
101102
class Expr;
102-
class FixedPointSemantics;
103103
class GlobalDecl;
104104
class MangleContext;
105105
class MangleNumberingContext;
@@ -1985,9 +1985,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
19851985

19861986
unsigned char getFixedPointScale(QualType Ty) const;
19871987
unsigned char getFixedPointIBits(QualType Ty) const;
1988-
FixedPointSemantics getFixedPointSemantics(QualType Ty) const;
1989-
APFixedPoint getFixedPointMax(QualType Ty) const;
1990-
APFixedPoint getFixedPointMin(QualType Ty) const;
1988+
llvm::FixedPointSemantics getFixedPointSemantics(QualType Ty) const;
1989+
llvm::APFixedPoint getFixedPointMax(QualType Ty) const;
1990+
llvm::APFixedPoint getFixedPointMin(QualType Ty) const;
19911991

19921992
DeclarationNameInfo getNameForTemplate(TemplateName Name,
19931993
SourceLocation NameLoc) const;

clang/include/clang/AST/Expr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "clang/AST/TemplateBase.h"
2525
#include "clang/AST/Type.h"
2626
#include "clang/Basic/CharInfo.h"
27-
#include "clang/Basic/FixedPoint.h"
2827
#include "clang/Basic/LangOptions.h"
2928
#include "clang/Basic/SyncScope.h"
3029
#include "clang/Basic/TypeTraits.h"

clang/include/clang/AST/OptionalDiagnostic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class OptionalDiagnostic {
6363
return *this;
6464
}
6565

66-
OptionalDiagnostic &operator<<(const APFixedPoint &FX) {
66+
OptionalDiagnostic &operator<<(const llvm::APFixedPoint &FX) {
6767
if (Diag) {
6868
SmallVector<char, 32> Buffer;
6969
FX.toString(Buffer);

clang/lib/AST/ASTContext.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include "clang/Basic/Builtins.h"
5252
#include "clang/Basic/CommentOptions.h"
5353
#include "clang/Basic/ExceptionSpecificationType.h"
54-
#include "clang/Basic/FixedPoint.h"
5554
#include "clang/Basic/IdentifierTable.h"
5655
#include "clang/Basic/LLVM.h"
5756
#include "clang/Basic/LangOptions.h"
@@ -65,6 +64,7 @@
6564
#include "clang/Basic/TargetCXXABI.h"
6665
#include "clang/Basic/TargetInfo.h"
6766
#include "clang/Basic/XRayLists.h"
67+
#include "llvm/ADT/APFixedPoint.h"
6868
#include "llvm/ADT/APInt.h"
6969
#include "llvm/ADT/APSInt.h"
7070
#include "llvm/ADT/ArrayRef.h"
@@ -11182,29 +11182,30 @@ unsigned char ASTContext::getFixedPointIBits(QualType Ty) const {
1118211182
}
1118311183
}
1118411184

11185-
FixedPointSemantics ASTContext::getFixedPointSemantics(QualType Ty) const {
11185+
llvm::FixedPointSemantics
11186+
ASTContext::getFixedPointSemantics(QualType Ty) const {
1118611187
assert((Ty->isFixedPointType() || Ty->isIntegerType()) &&
1118711188
"Can only get the fixed point semantics for a "
1118811189
"fixed point or integer type.");
1118911190
if (Ty->isIntegerType())
11190-
return FixedPointSemantics::GetIntegerSemantics(getIntWidth(Ty),
11191-
Ty->isSignedIntegerType());
11191+
return llvm::FixedPointSemantics::GetIntegerSemantics(
11192+
getIntWidth(Ty), Ty->isSignedIntegerType());
1119211193

1119311194
bool isSigned = Ty->isSignedFixedPointType();
11194-
return FixedPointSemantics(
11195+
return llvm::FixedPointSemantics(
1119511196
static_cast<unsigned>(getTypeSize(Ty)), getFixedPointScale(Ty), isSigned,
1119611197
Ty->isSaturatedFixedPointType(),
1119711198
!isSigned && getTargetInfo().doUnsignedFixedPointTypesHavePadding());
1119811199
}
1119911200

11200-
APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const {
11201+
llvm::APFixedPoint ASTContext::getFixedPointMax(QualType Ty) const {
1120111202
assert(Ty->isFixedPointType());
11202-
return APFixedPoint::getMax(getFixedPointSemantics(Ty));
11203+
return llvm::APFixedPoint::getMax(getFixedPointSemantics(Ty));
1120311204
}
1120411205

11205-
APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const {
11206+
llvm::APFixedPoint ASTContext::getFixedPointMin(QualType Ty) const {
1120611207
assert(Ty->isFixedPointType());
11207-
return APFixedPoint::getMin(getFixedPointSemantics(Ty));
11208+
return llvm::APFixedPoint::getMin(getFixedPointSemantics(Ty));
1120811209
}
1120911210

1121011211
QualType ASTContext::getCorrespondingSignedFixedPointType(QualType Ty) const {

clang/lib/AST/ExprConstant.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
#include "clang/AST/StmtVisitor.h"
5151
#include "clang/AST/TypeLoc.h"
5252
#include "clang/Basic/Builtins.h"
53-
#include "clang/Basic/FixedPoint.h"
5453
#include "clang/Basic/TargetInfo.h"
54+
#include "llvm/ADT/APFixedPoint.h"
5555
#include "llvm/ADT/Optional.h"
5656
#include "llvm/ADT/SmallBitVector.h"
5757
#include "llvm/Support/Debug.h"
@@ -63,9 +63,11 @@
6363
#define DEBUG_TYPE "exprconstant"
6464

6565
using namespace clang;
66+
using llvm::APFixedPoint;
6667
using llvm::APInt;
6768
using llvm::APSInt;
6869
using llvm::APFloat;
70+
using llvm::FixedPointSemantics;
6971
using llvm::Optional;
7072

7173
namespace {

clang/lib/AST/Type.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4337,10 +4337,10 @@ CXXRecordDecl *MemberPointerType::getMostRecentCXXRecordDecl() const {
43374337

43384338
void clang::FixedPointValueToString(SmallVectorImpl<char> &Str,
43394339
llvm::APSInt Val, unsigned Scale) {
4340-
FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(),
4341-
/*IsSaturated=*/false,
4342-
/*HasUnsignedPadding=*/false);
4343-
APFixedPoint(Val, FXSema).toString(Str);
4340+
llvm::FixedPointSemantics FXSema(Val.getBitWidth(), Scale, Val.isSigned(),
4341+
/*IsSaturated=*/false,
4342+
/*HasUnsignedPadding=*/false);
4343+
llvm::APFixedPoint(Val, FXSema).toString(Str);
43444344
}
43454345

43464346
AutoType::AutoType(QualType DeducedAsType, AutoTypeKeyword Keyword,

clang/lib/Basic/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ add_clang_library(clangBasic
4848
ExpressionTraits.cpp
4949
FileManager.cpp
5050
FileSystemStatCache.cpp
51-
FixedPoint.cpp
5251
IdentifierTable.cpp
5352
LangOptions.cpp
5453
LangStandards.cpp

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
#include "clang/AST/RecordLayout.h"
2727
#include "clang/AST/StmtVisitor.h"
2828
#include "clang/Basic/CodeGenOptions.h"
29-
#include "clang/Basic/FixedPoint.h"
3029
#include "clang/Basic/TargetInfo.h"
30+
#include "llvm/ADT/APFixedPoint.h"
3131
#include "llvm/ADT/Optional.h"
3232
#include "llvm/IR/CFG.h"
3333
#include "llvm/IR/Constants.h"
@@ -356,8 +356,9 @@ class ScalarExprEmitter
356356
/// and an integer.
357357
Value *EmitFixedPointConversion(Value *Src, QualType SrcTy, QualType DstTy,
358358
SourceLocation Loc);
359-
Value *EmitFixedPointConversion(Value *Src, FixedPointSemantics &SrcFixedSema,
360-
FixedPointSemantics &DstFixedSema,
359+
Value *EmitFixedPointConversion(Value *Src,
360+
llvm::FixedPointSemantics &SrcFixedSema,
361+
llvm::FixedPointSemantics &DstFixedSema,
361362
SourceLocation Loc,
362363
bool DstIsInteger = false);
363364

@@ -1444,17 +1445,17 @@ Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
14441445
Value *ScalarExprEmitter::EmitFixedPointConversion(Value *Src, QualType SrcTy,
14451446
QualType DstTy,
14461447
SourceLocation Loc) {
1447-
FixedPointSemantics SrcFPSema =
1448-
CGF.getContext().getFixedPointSemantics(SrcTy);
1449-
FixedPointSemantics DstFPSema =
1450-
CGF.getContext().getFixedPointSemantics(DstTy);
1448+
auto SrcFPSema = CGF.getContext().getFixedPointSemantics(SrcTy);
1449+
auto DstFPSema = CGF.getContext().getFixedPointSemantics(DstTy);
14511450
return EmitFixedPointConversion(Src, SrcFPSema, DstFPSema, Loc,
14521451
DstTy->isIntegerType());
14531452
}
14541453

14551454
Value *ScalarExprEmitter::EmitFixedPointConversion(
1456-
Value *Src, FixedPointSemantics &SrcFPSema, FixedPointSemantics &DstFPSema,
1455+
Value *Src, llvm::FixedPointSemantics &SrcFPSema,
1456+
llvm::FixedPointSemantics &DstFPSema,
14571457
SourceLocation Loc, bool DstIsInteger) {
1458+
using llvm::APFixedPoint;
14581459
using llvm::APInt;
14591460
using llvm::ConstantInt;
14601461
using llvm::Value;
@@ -2667,12 +2668,10 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
26672668
// Now, convert from our invented integer literal to the type of the unary
26682669
// op. This will upscale and saturate if necessary. This value can become
26692670
// undef in some cases.
2670-
FixedPointSemantics SrcSema =
2671-
FixedPointSemantics::GetIntegerSemantics(value->getType()
2672-
->getScalarSizeInBits(),
2673-
/*IsSigned=*/true);
2674-
FixedPointSemantics DstSema =
2675-
CGF.getContext().getFixedPointSemantics(Info.Ty);
2671+
auto SrcSema =
2672+
llvm::FixedPointSemantics::GetIntegerSemantics(
2673+
value->getType()->getScalarSizeInBits(), /*IsSigned=*/true);
2674+
auto DstSema = CGF.getContext().getFixedPointSemantics(Info.Ty);
26762675
Info.RHS = EmitFixedPointConversion(Info.RHS, SrcSema, DstSema,
26772676
E->getExprLoc());
26782677
value = EmitFixedPointBinOp(Info);

clang/lib/Sema/SemaChecking.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11843,9 +11843,9 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
1184311843
Expr::EvalResult Result;
1184411844
if (E->EvaluateAsFixedPoint(Result, S.Context, Expr::SE_AllowSideEffects,
1184511845
S.isConstantEvaluated())) {
11846-
APFixedPoint Value = Result.Val.getFixedPoint();
11847-
APFixedPoint MaxVal = S.Context.getFixedPointMax(T);
11848-
APFixedPoint MinVal = S.Context.getFixedPointMin(T);
11846+
llvm::APFixedPoint Value = Result.Val.getFixedPoint();
11847+
llvm::APFixedPoint MaxVal = S.Context.getFixedPointMax(T);
11848+
llvm::APFixedPoint MinVal = S.Context.getFixedPointMin(T);
1184911849
if (Value > MaxVal || Value < MinVal) {
1185011850
S.DiagRuntimeBehavior(E->getExprLoc(), E,
1185111851
S.PDiag(diag::warn_impcast_fixed_point_range)
@@ -11860,7 +11860,7 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
1186011860
if (!S.isConstantEvaluated() &&
1186111861
E->EvaluateAsFixedPoint(Result, S.Context,
1186211862
Expr::SE_AllowSideEffects)) {
11863-
APFixedPoint FXResult = Result.Val.getFixedPoint();
11863+
llvm::APFixedPoint FXResult = Result.Val.getFixedPoint();
1186411864

1186511865
bool Overflowed;
1186611866
llvm::APSInt IntResult = FXResult.convertToInt(
@@ -11885,7 +11885,7 @@ static void CheckImplicitConversion(Sema &S, Expr *E, QualType T,
1188511885
llvm::APSInt Value = Result.Val.getInt();
1188611886

1188711887
bool Overflowed;
11888-
APFixedPoint IntResult = APFixedPoint::getFromIntValue(
11888+
llvm::APFixedPoint IntResult = llvm::APFixedPoint::getFromIntValue(
1188911889
Value, S.Context.getFixedPointSemantics(T), &Overflowed);
1189011890

1189111891
if (Overflowed) {

clang/lib/Sema/SemaExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "clang/AST/RecursiveASTVisitor.h"
2828
#include "clang/AST/TypeLoc.h"
2929
#include "clang/Basic/Builtins.h"
30-
#include "clang/Basic/FixedPoint.h"
3130
#include "clang/Basic/PartialDiagnostic.h"
3231
#include "clang/Basic/SourceManager.h"
3332
#include "clang/Basic/TargetInfo.h"
@@ -10610,7 +10609,7 @@ static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS,
1061010609
if (LHSExprType->isExtIntType())
1061110610
LeftSize = S.Context.getIntWidth(LHSExprType);
1061210611
else if (LHSExprType->isFixedPointType()) {
10613-
FixedPointSemantics FXSema = S.Context.getFixedPointSemantics(LHSExprType);
10612+
auto FXSema = S.Context.getFixedPointSemantics(LHSExprType);
1061410613
LeftSize = FXSema.getWidth() - (unsigned)FXSema.hasUnsignedPadding();
1061510614
}
1061610615
llvm::APInt LeftBits(Right.getBitWidth(), LeftSize);

clang/lib/Serialization/ASTReader.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8941,7 +8941,7 @@ ASTReader::ReadSourceRange(ModuleFile &F, const RecordData &Record,
89418941
return SourceRange(beg, end);
89428942
}
89438943

8944-
static FixedPointSemantics
8944+
static llvm::FixedPointSemantics
89458945
ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
89468946
unsigned &Idx) {
89478947
unsigned Width = Record[Idx++];
@@ -8950,8 +8950,8 @@ ReadFixedPointSemantics(const SmallVectorImpl<uint64_t> &Record,
89508950
bool IsSigned = Tmp & 0x1;
89518951
bool IsSaturated = Tmp & 0x2;
89528952
bool HasUnsignedPadding = Tmp & 0x4;
8953-
return FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8954-
HasUnsignedPadding);
8953+
return llvm::FixedPointSemantics(Width, Scale, IsSigned, IsSaturated,
8954+
HasUnsignedPadding);
89558955
}
89568956

89578957
static const llvm::fltSemantics &
@@ -8974,8 +8974,8 @@ APValue ASTRecordReader::readAPValue() {
89748974
return APValue(readAPFloat(FloatSema));
89758975
}
89768976
case APValue::FixedPoint: {
8977-
FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8978-
return APValue(APFixedPoint(readAPInt(), FPSema));
8977+
llvm::FixedPointSemantics FPSema = ReadFixedPointSemantics(Record, Idx);
8978+
return APValue(llvm::APFixedPoint(readAPInt(), FPSema));
89798979
}
89808980
case APValue::ComplexInt: {
89818981
llvm::APSInt First = readAPSInt();

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5127,7 +5127,7 @@ void ASTRecordWriter::AddAPFloat(const llvm::APFloat &Value) {
51275127
}
51285128

51295129
static void WriteFixedPointSemantics(ASTRecordWriter &Record,
5130-
FixedPointSemantics FPSema) {
5130+
llvm::FixedPointSemantics FPSema) {
51315131
Record.push_back(FPSema.getWidth());
51325132
Record.push_back(FPSema.getScale());
51335133
Record.push_back(FPSema.isSigned() | FPSema.isSaturated() << 1 |

clang/unittests/Basic/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_clang_unittest(BasicTests
66
CharInfoTest.cpp
77
DiagnosticTest.cpp
88
FileManagerTest.cpp
9-
FixedPointTest.cpp
109
SourceManagerTest.cpp
1110
)
1211

0 commit comments

Comments
 (0)