1//===--- ExprConstShared.h - Shared consetxpr functionality ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Shared functionality between the new constant expression
10// interpreter (AST/ByteCode/) and the current one (ExprConstant.cpp).
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_AST_EXPRCONSTSHARED_H
15#define LLVM_CLANG_LIB_AST_EXPRCONSTSHARED_H
16
17#include "clang/Basic/TypeTraits.h"
18#include <cstdint>
19#include <optional>
20
21namespace llvm {
22class APFloat;
23class APSInt;
24class APInt;
25}
26namespace clang {
27class QualType;
28class LangOptions;
29class ASTContext;
30class CharUnits;
31class Expr;
32class CallExpr;
33} // namespace clang
34using namespace clang;
35/// Values returned by __builtin_classify_type, chosen to match the values
36/// produced by GCC's builtin.
37enum class GCCTypeClass {
38 None = -1,
39 Void = 0,
40 Integer = 1,
41 // GCC reserves 2 for character types, but instead classifies them as
42 // integers.
43 Enum = 3,
44 Bool = 4,
45 Pointer = 5,
46 // GCC reserves 6 for references, but appears to never use it (because
47 // expressions never have reference type, presumably).
48 PointerToDataMember = 7,
49 RealFloat = 8,
50 Complex = 9,
51 // GCC reserves 10 for functions, but does not use it since GCC version 6 due
52 // to decay to pointer. (Prior to version 6 it was only used in C++ mode).
53 // GCC claims to reserve 11 for pointers to member functions, but *actually*
54 // uses 12 for that purpose, same as for a class or struct. Maybe it
55 // internally implements a pointer to member as a struct? Who knows.
56 PointerToMemberFunction = 12, // Not a bug, see above.
57 ClassOrStruct = 12,
58 Union = 13,
59 // GCC reserves 14 for arrays, but does not use it since GCC version 6 due to
60 // decay to pointer. (Prior to version 6 it was only used in C++ mode).
61 // GCC reserves 15 for strings, but actually uses 5 (pointer) for string
62 // literals.
63 // Lang = 16,
64 // OpaqueType = 17,
65 BitInt = 18,
66 Vector = 19
67};
68
69GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
70 const LangOptions &LangOpts);
71
72void HandleComplexComplexMul(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C,
73 llvm::APFloat D, llvm::APFloat &ResR,
74 llvm::APFloat &ResI);
75void HandleComplexComplexDiv(llvm::APFloat A, llvm::APFloat B, llvm::APFloat C,
76 llvm::APFloat D, llvm::APFloat &ResR,
77 llvm::APFloat &ResI);
78
79CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
80 UnaryExprOrTypeTrait ExprKind);
81
82/// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
83/// dispatch on in their x86 target-specific cases.
84///
85/// Target-independent builtins are returned unchanged. An x86 target builtin
86/// (including an auxiliary-target x86 builtin, whose ID is shifted past the
87/// primary target's builtins) is translated to its canonical X86::BI* value.
88/// Any other target's builtin returns 0: the constant evaluators only fold x86
89/// target builtins, and target builtin IDs of different targets overlap (each
90/// numbers from Builtin::FirstTSBuiltin), so an unrelated target's ID must not
91/// be mistaken for an x86 one.
92///
93/// The ID-based overload performs no work beyond a single comparison for
94/// target-independent builtins, so it is suitable for hot paths (e.g. the
95/// bytecode interpreter's builtin dispatch) where re-deriving the ID from the
96/// call expression would be wasteful.
97unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
98 unsigned BuiltinID);
99unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
100 const CallExpr *E);
101
102uint8_t GFNIMultiplicativeInverse(uint8_t Byte);
103uint8_t GFNIMul(uint8_t AByte, uint8_t BByte);
104uint8_t GFNIAffine(uint8_t XByte, const llvm::APInt &AQword,
105 const llvm::APSInt &Imm, bool Inverse = false);
106llvm::APSInt NormalizeRotateAmount(const llvm::APSInt &Value,
107 const llvm::APSInt &Amount);
108
109std::optional<llvm::APFloat>
110EvalScalarMinMaxFp(const llvm::APFloat &A, const llvm::APFloat &B,
111 std::optional<llvm::APSInt> RoundingMode, bool IsMin);
112
113#endif
114