1//===-- LoongArch.h - Declare LoongArch target feature support --*- 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// This file declares LoongArch TargetInfo objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
14#define LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
15
16#include "clang/Basic/TargetInfo.h"
17#include "clang/Basic/TargetOptions.h"
18#include "llvm/Support/Compiler.h"
19#include "llvm/TargetParser/Triple.h"
20
21namespace clang {
22namespace targets {
23
24class LLVM_LIBRARY_VISIBILITY LoongArchTargetInfo : public TargetInfo {
25protected:
26 std::string ABI;
27 std::string CPU;
28 bool HasFeature32S;
29 bool HasFeatureD;
30 bool HasFeatureF;
31 bool HasFeatureLSX;
32 bool HasFeatureLASX;
33 bool HasFeatureFrecipe;
34 bool HasFeatureLAM_BH;
35 bool HasFeatureLAMCAS;
36 bool HasFeatureLD_SEQ_SA;
37 bool HasFeatureDiv32;
38 bool HasFeatureSCQ;
39
40public:
41 LoongArchTargetInfo(const llvm::Triple &Triple, const TargetOptions &)
42 : TargetInfo(Triple) {
43 HasFeature32S = false;
44 HasFeatureD = false;
45 HasFeatureF = false;
46 HasFeatureLSX = false;
47 HasFeatureLASX = false;
48 HasFeatureFrecipe = false;
49 HasFeatureLAM_BH = false;
50 HasFeatureLAMCAS = false;
51 HasFeatureLD_SEQ_SA = false;
52 HasFeatureDiv32 = false;
53 HasFeatureSCQ = false;
54 BFloat16Width = 16;
55 BFloat16Align = 16;
56 BFloat16Format = &llvm::APFloat::BFloat();
57 LongDoubleWidth = 128;
58 LongDoubleAlign = 128;
59 LongDoubleFormat = &llvm::APFloat::IEEEquad();
60 MCountName = "_mcount";
61 HasFloat16 = true;
62 SuitableAlign = 128;
63 WCharType = SignedInt;
64 WIntType = UnsignedInt;
65 BitIntMaxAlign = 128;
66 }
67
68 bool setCPU(const std::string &Name) override {
69 if (!isValidCPUName(Name))
70 return false;
71 CPU = Name;
72 return true;
73 }
74
75 StringRef getCPU() const { return CPU; }
76
77 StringRef getABI() const override { return ABI; }
78
79 void getTargetDefines(const LangOptions &Opts,
80 MacroBuilder &Builder) const override;
81
82 llvm::SmallVector<Builtin::InfosShard> getTargetBuiltins() const override;
83
84 BuiltinVaListKind getBuiltinVaListKind() const override {
85 return TargetInfo::VoidPtrBuiltinVaList;
86 }
87
88 std::string_view getClobbers() const override { return ""; }
89
90 ArrayRef<const char *> getGCCRegNames() const override;
91
92 int getEHDataRegisterNumber(unsigned RegNo) const override {
93 if (RegNo == 0)
94 return 4;
95 if (RegNo == 1)
96 return 5;
97 return -1;
98 }
99
100 ArrayRef<TargetInfo::GCCRegAlias> getGCCRegAliases() const override;
101
102 bool validateAsmConstraint(const char *&Name,
103 TargetInfo::ConstraintInfo &Info) const override;
104 std::string convertConstraint(const char *&Constraint) const override;
105
106 bool hasBitIntType() const override { return true; }
107
108 bool hasBFloat16Type() const override { return true; }
109
110 bool useFP16ConversionIntrinsics() const override { return false; }
111
112 bool handleTargetFeatures(std::vector<std::string> &Features,
113 DiagnosticsEngine &Diags) override;
114
115 ParsedTargetAttr parseTargetAttr(StringRef Str) const override;
116 bool supportsTargetAttributeTune() const override { return true; }
117
118 bool
119 initFeatureMap(llvm::StringMap<bool> &Features, DiagnosticsEngine &Diags,
120 StringRef CPU,
121 const std::vector<std::string> &FeaturesVec) const override;
122
123 bool hasFeature(StringRef Feature) const override;
124
125 bool isValidCPUName(StringRef Name) const override;
126 void fillValidCPUList(SmallVectorImpl<StringRef> &Values) const override;
127 bool isValidFeatureName(StringRef Name) const override;
128};
129
130class LLVM_LIBRARY_VISIBILITY LoongArch32TargetInfo
131 : public LoongArchTargetInfo {
132public:
133 LoongArch32TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
134 : LoongArchTargetInfo(Triple, Opts) {
135 IntPtrType = SignedInt;
136 PtrDiffType = SignedInt;
137 SizeType = UnsignedInt;
138 // TODO: select appropriate ABI.
139 setABI("ilp32d");
140 resetDataLayout();
141 }
142
143 bool setABI(const std::string &Name) override {
144 if (Name == "ilp32d" || Name == "ilp32f" || Name == "ilp32s") {
145 ABI = Name;
146 return true;
147 }
148 return false;
149 }
150 void setMaxAtomicWidth() override {
151 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 32;
152 }
153};
154
155class LLVM_LIBRARY_VISIBILITY LoongArch64TargetInfo
156 : public LoongArchTargetInfo {
157public:
158 LoongArch64TargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts)
159 : LoongArchTargetInfo(Triple, Opts) {
160 LongWidth = LongAlign = PointerWidth = PointerAlign = 64;
161 IntMaxType = Int64Type = SignedLong;
162 HasUnalignedAccess = true;
163 // TODO: select appropriate ABI.
164 setABI("lp64d");
165 resetDataLayout();
166 }
167
168 bool setABI(const std::string &Name) override {
169 if (Name == "lp64d" || Name == "lp64f" || Name == "lp64s") {
170 ABI = Name;
171 return true;
172 }
173 return false;
174 }
175 void setMaxAtomicWidth() override {
176 MaxAtomicPromoteWidth = MaxAtomicInlineWidth = 64;
177 }
178};
179} // end namespace targets
180} // end namespace clang
181
182#endif // LLVM_CLANG_LIB_BASIC_TARGETS_LOONGARCH_H
183