blob: 8c8b415df91431811f3195c5ceb39b43f3f9e17d [file] [log] [blame]
Sander de Smalen5087ace2020-03-15 14:29:45 +00001//===- SveEmitter.cpp - Generate arm_sve.h for use with clang -*- 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 tablegen backend is responsible for emitting arm_sve.h, which includes
10// a declaration and definition of each function specified by the ARM C/C++
11// Language Extensions (ACLE).
12//
13// For details, visit:
14// https://developer.arm.com/architectures/system-architectures/software-standards/acle
15//
16// Each SVE instruction is implemented in terms of 1 or more functions which
17// are suffixed with the element type of the input vectors. Functions may be
18// implemented in terms of generic vector operations such as +, *, -, etc. or
19// by calling a __builtin_-prefixed function which will be handled by clang's
20// CodeGen library.
21//
22// See also the documentation in include/clang/Basic/arm_sve.td.
23//
24//===----------------------------------------------------------------------===//
25
26#include "llvm/ADT/STLExtras.h"
Sander de Smalenc5b81462020-03-18 11:07:20 +000027#include "llvm/ADT/StringMap.h"
Sander de Smalen5087ace2020-03-15 14:29:45 +000028#include "llvm/ADT/ArrayRef.h"
29#include "llvm/ADT/StringExtras.h"
30#include "llvm/TableGen/Record.h"
31#include "llvm/TableGen/Error.h"
32#include <string>
33#include <sstream>
34#include <set>
35#include <cctype>
Eric Fiselieraf2968e2020-04-16 18:35:31 -040036#include <tuple>
Sander de Smalen5087ace2020-03-15 14:29:45 +000037
38using namespace llvm;
39
Sander de Smalenc5b81462020-03-18 11:07:20 +000040enum ClassKind {
41 ClassNone,
42 ClassS, // signed/unsigned, e.g., "_s8", "_u8" suffix
43 ClassG, // Overloaded name without type suffix
44};
45
46using TypeSpec = std::string;
Sander de Smalen5087ace2020-03-15 14:29:45 +000047
48namespace {
49
Sander de Smalenc8a5b302020-04-14 15:56:36 +010050class ImmCheck {
51 unsigned Arg;
52 unsigned Kind;
53 unsigned ElementSizeInBits;
54
55public:
56 ImmCheck(unsigned Arg, unsigned Kind, unsigned ElementSizeInBits = 0)
57 : Arg(Arg), Kind(Kind), ElementSizeInBits(ElementSizeInBits) {}
58 ImmCheck(const ImmCheck &Other) = default;
59 ~ImmCheck() = default;
60
61 unsigned getArg() const { return Arg; }
62 unsigned getKind() const { return Kind; }
63 unsigned getElementSizeInBits() const { return ElementSizeInBits; }
64};
65
Sander de Smalenc5b81462020-03-18 11:07:20 +000066class SVEType {
67 TypeSpec TS;
68 bool Float, Signed, Immediate, Void, Constant, Pointer;
69 bool DefaultType, IsScalable, Predicate, PredicatePattern, PrefetchOp;
70 unsigned Bitwidth, ElementBitwidth, NumVectors;
71
Sander de Smalen8b409ea2020-03-16 10:14:05 +000072public:
Sander de Smalenc5b81462020-03-18 11:07:20 +000073 SVEType() : SVEType(TypeSpec(), 'v') {}
74
75 SVEType(TypeSpec TS, char CharMod)
76 : TS(TS), Float(false), Signed(true), Immediate(false), Void(false),
77 Constant(false), Pointer(false), DefaultType(false), IsScalable(true),
78 Predicate(false), PredicatePattern(false), PrefetchOp(false),
79 Bitwidth(128), ElementBitwidth(~0U), NumVectors(1) {
80 if (!TS.empty())
81 applyTypespec();
82 applyModifier(CharMod);
83 }
84
Sander de Smalenc5b81462020-03-18 11:07:20 +000085 bool isPointer() const { return Pointer; }
86 bool isVoidPointer() const { return Pointer && Void; }
87 bool isSigned() const { return Signed; }
88 bool isImmediate() const { return Immediate; }
89 bool isScalar() const { return NumVectors == 0; }
90 bool isVector() const { return NumVectors > 0; }
91 bool isScalableVector() const { return isVector() && IsScalable; }
92 bool isChar() const { return ElementBitwidth == 8; }
93 bool isVoid() const { return Void & !Pointer; }
94 bool isDefault() const { return DefaultType; }
95 bool isFloat() const { return Float; }
96 bool isInteger() const { return !Float && !Predicate; }
97 bool isScalarPredicate() const { return !Float && ElementBitwidth == 1; }
98 bool isPredicateVector() const { return Predicate; }
99 bool isPredicatePattern() const { return PredicatePattern; }
100 bool isPrefetchOp() const { return PrefetchOp; }
101 bool isConstant() const { return Constant; }
102 unsigned getElementSizeInBits() const { return ElementBitwidth; }
103 unsigned getNumVectors() const { return NumVectors; }
104
105 unsigned getNumElements() const {
106 assert(ElementBitwidth != ~0U);
107 return Bitwidth / ElementBitwidth;
108 }
109 unsigned getSizeInBits() const {
110 return Bitwidth;
111 }
112
113 /// Return the string representation of a type, which is an encoded
114 /// string for passing to the BUILTIN() macro in Builtins.def.
115 std::string builtin_str() const;
116
Sander de Smalen981f0802020-03-18 15:05:08 +0000117 /// Return the C/C++ string representation of a type for use in the
118 /// arm_sve.h header file.
119 std::string str() const;
120
Sander de Smalenc5b81462020-03-18 11:07:20 +0000121private:
122 /// Creates the type based on the typespec string in TS.
123 void applyTypespec();
124
125 /// Applies a prototype modifier to the type.
126 void applyModifier(char Mod);
127};
128
129
130class SVEEmitter;
131
132/// The main grunt class. This represents an instantiation of an intrinsic with
133/// a particular typespec and prototype.
134class Intrinsic {
135 /// The unmangled name.
136 std::string Name;
137
138 /// The name of the corresponding LLVM IR intrinsic.
139 std::string LLVMName;
140
141 /// Intrinsic prototype.
142 std::string Proto;
143
144 /// The base type spec for this intrinsic.
145 TypeSpec BaseTypeSpec;
146
147 /// The base class kind. Most intrinsics use ClassS, which has full type
148 /// info for integers (_s32/_u32), or ClassG which is used for overloaded
149 /// intrinsics.
150 ClassKind Class;
151
152 /// The architectural #ifdef guard.
153 std::string Guard;
154
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100155 // The merge suffix such as _m, _x or _z.
156 std::string MergeSuffix;
157
Sander de Smalenc5b81462020-03-18 11:07:20 +0000158 /// The types of return value [0] and parameters [1..].
159 std::vector<SVEType> Types;
160
161 /// The "base type", which is VarType('d', BaseTypeSpec).
162 SVEType BaseType;
163
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100164 uint64_t Flags;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000165
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100166 SmallVector<ImmCheck, 2> ImmChecks;
167
Sander de Smalenc5b81462020-03-18 11:07:20 +0000168public:
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100169 Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
170 StringRef MergeSuffix, uint64_t MemoryElementTy, StringRef LLVMName,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100171 uint64_t Flags, ArrayRef<ImmCheck> ImmChecks, TypeSpec BT,
172 ClassKind Class, SVEEmitter &Emitter, StringRef Guard);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000173
174 ~Intrinsic()=default;
175
176 std::string getName() const { return Name; }
177 std::string getLLVMName() const { return LLVMName; }
178 std::string getProto() const { return Proto; }
179 TypeSpec getBaseTypeSpec() const { return BaseTypeSpec; }
180 SVEType getBaseType() const { return BaseType; }
181
182 StringRef getGuard() const { return Guard; }
183 ClassKind getClassKind() const { return Class; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000184
185 SVEType getReturnType() const { return Types[0]; }
186 ArrayRef<SVEType> getTypes() const { return Types; }
187 SVEType getParamType(unsigned I) const { return Types[I + 1]; }
188 unsigned getNumParams() const { return Proto.size() - 1; }
189
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100190 uint64_t getFlags() const { return Flags; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000191 bool isFlagSet(uint64_t Flag) const { return Flags & Flag;}
192
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100193 ArrayRef<ImmCheck> getImmChecks() const { return ImmChecks; }
194
Sander de Smalenc5b81462020-03-18 11:07:20 +0000195 /// Return the type string for a BUILTIN() macro in Builtins.def.
196 std::string getBuiltinTypeStr();
197
198 /// Return the name, mangled with type information. The name is mangled for
199 /// ClassS, so will add type suffixes such as _u32/_s32.
200 std::string getMangledName() const { return mangleName(ClassS); }
201
202 /// Returns true if the intrinsic is overloaded, in that it should also generate
203 /// a short form without the type-specifiers, e.g. 'svld1(..)' instead of
204 /// 'svld1_u32(..)'.
205 static bool isOverloadedIntrinsic(StringRef Name) {
206 auto BrOpen = Name.find("[");
207 auto BrClose = Name.find(']');
208 return BrOpen != std::string::npos && BrClose != std::string::npos;
209 }
210
Sander de Smalen41d52662020-04-22 13:58:35 +0100211 /// Return true if the intrinsic takes a splat operand.
212 bool hasSplat() const {
213 // These prototype modifiers are described in arm_sve.td.
214 return Proto.find_first_of("ajfrKLR") != std::string::npos;
215 }
216
217 /// Return the parameter index of the splat operand.
218 unsigned getSplatIdx() const {
219 // These prototype modifiers are described in arm_sve.td.
220 auto Idx = Proto.find_first_of("ajfrKLR");
221 assert(Idx != std::string::npos && Idx > 0 &&
222 "Prototype has no splat operand");
223 return Idx - 1;
224 }
225
Sander de Smalenc5b81462020-03-18 11:07:20 +0000226 /// Emits the intrinsic declaration to the ostream.
227 void emitIntrinsic(raw_ostream &OS) const;
228
229private:
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100230 std::string getMergeSuffix() const { return MergeSuffix; }
Sander de Smalenc5b81462020-03-18 11:07:20 +0000231 std::string mangleName(ClassKind LocalCK) const;
232 std::string replaceTemplatedArgs(std::string Name, TypeSpec TS,
233 std::string Proto) const;
234};
235
236class SVEEmitter {
237private:
238 RecordKeeper &Records;
239 llvm::StringMap<uint64_t> EltTypes;
240 llvm::StringMap<uint64_t> MemEltTypes;
241 llvm::StringMap<uint64_t> FlagTypes;
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100242 llvm::StringMap<uint64_t> MergeTypes;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100243 llvm::StringMap<uint64_t> ImmCheckTypes;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000244
Sander de Smalenc5b81462020-03-18 11:07:20 +0000245public:
246 SVEEmitter(RecordKeeper &R) : Records(R) {
247 for (auto *RV : Records.getAllDerivedDefinitions("EltType"))
248 EltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
249 for (auto *RV : Records.getAllDerivedDefinitions("MemEltType"))
250 MemEltTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
251 for (auto *RV : Records.getAllDerivedDefinitions("FlagType"))
252 FlagTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100253 for (auto *RV : Records.getAllDerivedDefinitions("MergeType"))
254 MergeTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100255 for (auto *RV : Records.getAllDerivedDefinitions("ImmCheckType"))
256 ImmCheckTypes[RV->getNameInitAsString()] = RV->getValueAsInt("Value");
257 }
258
259 /// Returns the enum value for the immcheck type
260 unsigned getEnumValueForImmCheck(StringRef C) const {
261 auto It = ImmCheckTypes.find(C);
262 if (It != ImmCheckTypes.end())
263 return It->getValue();
264 llvm_unreachable("Unsupported imm check");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000265 }
266
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100267 /// Returns the enum value for the flag type
268 uint64_t getEnumValueForFlag(StringRef C) const {
269 auto Res = FlagTypes.find(C);
270 if (Res != FlagTypes.end())
271 return Res->getValue();
272 llvm_unreachable("Unsupported flag");
273 }
274
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100275 // Returns the SVETypeFlags for a given value and mask.
276 uint64_t encodeFlag(uint64_t V, StringRef MaskName) const {
277 auto It = FlagTypes.find(MaskName);
278 if (It != FlagTypes.end()) {
279 uint64_t Mask = It->getValue();
280 unsigned Shift = llvm::countTrailingZeros(Mask);
281 return (V << Shift) & Mask;
282 }
283 llvm_unreachable("Unsupported flag");
284 }
285
286 // Returns the SVETypeFlags for the given element type.
287 uint64_t encodeEltType(StringRef EltName) {
288 auto It = EltTypes.find(EltName);
289 if (It != EltTypes.end())
290 return encodeFlag(It->getValue(), "EltTypeMask");
291 llvm_unreachable("Unsupported EltType");
292 }
293
294 // Returns the SVETypeFlags for the given memory element type.
295 uint64_t encodeMemoryElementType(uint64_t MT) {
296 return encodeFlag(MT, "MemEltTypeMask");
297 }
298
299 // Returns the SVETypeFlags for the given merge type.
300 uint64_t encodeMergeType(uint64_t MT) {
301 return encodeFlag(MT, "MergeTypeMask");
302 }
303
Sander de Smalen41d52662020-04-22 13:58:35 +0100304 // Returns the SVETypeFlags for the given splat operand.
305 unsigned encodeSplatOperand(unsigned SplatIdx) {
306 assert(SplatIdx < 7 && "SplatIdx out of encodable range");
307 return encodeFlag(SplatIdx + 1, "SplatOperandMask");
308 }
309
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100310 // Returns the SVETypeFlags value for the given SVEType.
311 uint64_t encodeTypeFlags(const SVEType &T);
312
Sander de Smalenc5b81462020-03-18 11:07:20 +0000313 /// Emit arm_sve.h.
314 void createHeader(raw_ostream &o);
315
316 /// Emit all the __builtin prototypes and code needed by Sema.
317 void createBuiltins(raw_ostream &o);
318
319 /// Emit all the information needed to map builtin -> LLVM IR intrinsic.
320 void createCodeGenMap(raw_ostream &o);
321
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100322 /// Emit all the range checks for the immediates.
323 void createRangeChecks(raw_ostream &o);
324
Sander de Smalenc5b81462020-03-18 11:07:20 +0000325 /// Create the SVETypeFlags used in CGBuiltins
326 void createTypeFlags(raw_ostream &o);
327
328 /// Create intrinsic and add it to \p Out
329 void createIntrinsic(Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out);
Sander de Smalen5087ace2020-03-15 14:29:45 +0000330};
331
332} // end anonymous namespace
333
334
335//===----------------------------------------------------------------------===//
Sander de Smalenc5b81462020-03-18 11:07:20 +0000336// Type implementation
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000337//===----------------------------------------------------------------------===//
Sander de Smalen8b409ea2020-03-16 10:14:05 +0000338
Sander de Smalenc5b81462020-03-18 11:07:20 +0000339std::string SVEType::builtin_str() const {
340 std::string S;
341 if (isVoid())
342 return "v";
343
344 if (isVoidPointer())
345 S += "v";
346 else if (!Float)
347 switch (ElementBitwidth) {
348 case 1: S += "b"; break;
349 case 8: S += "c"; break;
350 case 16: S += "s"; break;
351 case 32: S += "i"; break;
352 case 64: S += "Wi"; break;
353 case 128: S += "LLLi"; break;
354 default: llvm_unreachable("Unhandled case!");
355 }
356 else
357 switch (ElementBitwidth) {
358 case 16: S += "h"; break;
359 case 32: S += "f"; break;
360 case 64: S += "d"; break;
361 default: llvm_unreachable("Unhandled case!");
362 }
363
364 if (!isFloat()) {
365 if ((isChar() || isPointer()) && !isVoidPointer()) {
366 // Make chars and typed pointers explicitly signed.
367 if (Signed)
368 S = "S" + S;
369 else if (!Signed)
370 S = "U" + S;
371 } else if (!isVoidPointer() && !Signed) {
372 S = "U" + S;
373 }
374 }
375
376 // Constant indices are "int", but have the "constant expression" modifier.
377 if (isImmediate()) {
378 assert(!isFloat() && "fp immediates are not supported");
379 S = "I" + S;
380 }
381
382 if (isScalar()) {
383 if (Constant) S += "C";
384 if (Pointer) S += "*";
385 return S;
386 }
387
388 assert(isScalableVector() && "Unsupported type");
389 return "q" + utostr(getNumElements() * NumVectors) + S;
390}
391
Sander de Smalen981f0802020-03-18 15:05:08 +0000392std::string SVEType::str() const {
393 if (isPredicatePattern())
394 return "sv_pattern";
395
396 if (isPrefetchOp())
397 return "sv_prfop";
398
399 std::string S;
400 if (Void)
401 S += "void";
402 else {
403 if (isScalableVector())
404 S += "sv";
405 if (!Signed && !Float)
406 S += "u";
407
408 if (Float)
409 S += "float";
410 else if (isScalarPredicate())
411 S += "bool";
412 else
413 S += "int";
414
415 if (!isScalarPredicate())
416 S += utostr(ElementBitwidth);
417 if (!isScalableVector() && isVector())
418 S += "x" + utostr(getNumElements());
419 if (NumVectors > 1)
420 S += "x" + utostr(NumVectors);
421 S += "_t";
422 }
423
424 if (Constant)
425 S += " const";
426 if (Pointer)
427 S += " *";
428
429 return S;
430}
Sander de Smalenc5b81462020-03-18 11:07:20 +0000431void SVEType::applyTypespec() {
432 for (char I : TS) {
433 switch (I) {
434 case 'P':
435 Predicate = true;
436 ElementBitwidth = 1;
437 break;
438 case 'U':
439 Signed = false;
440 break;
441 case 'c':
442 ElementBitwidth = 8;
443 break;
444 case 's':
445 ElementBitwidth = 16;
446 break;
447 case 'i':
448 ElementBitwidth = 32;
449 break;
450 case 'l':
451 ElementBitwidth = 64;
452 break;
453 case 'h':
454 Float = true;
455 ElementBitwidth = 16;
456 break;
457 case 'f':
458 Float = true;
459 ElementBitwidth = 32;
460 break;
461 case 'd':
462 Float = true;
463 ElementBitwidth = 64;
464 break;
465 default:
466 llvm_unreachable("Unhandled type code!");
467 }
468 }
469 assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
470}
471
472void SVEType::applyModifier(char Mod) {
473 switch (Mod) {
474 case 'v':
475 Void = true;
476 break;
477 case 'd':
478 DefaultType = true;
479 break;
480 case 'c':
481 Constant = true;
482 LLVM_FALLTHROUGH;
483 case 'p':
484 Pointer = true;
485 Bitwidth = ElementBitwidth;
486 NumVectors = 0;
487 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100488 case 'e':
489 Signed = false;
490 ElementBitwidth /= 2;
491 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100492 case 'h':
493 ElementBitwidth /= 2;
494 break;
Sander de Smalenfc645392020-04-20 14:57:13 +0100495 case 'q':
496 ElementBitwidth /= 4;
497 break;
498 case 'o':
499 ElementBitwidth *= 4;
500 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000501 case 'P':
502 Signed = true;
503 Float = false;
504 Predicate = true;
505 Bitwidth = 16;
506 ElementBitwidth = 1;
507 break;
Sander de Smalen41d52662020-04-22 13:58:35 +0100508 case 'a':
509 Bitwidth = ElementBitwidth;
510 NumVectors = 0;
511 break;
Sander de Smalen515020c2020-04-20 14:41:58 +0100512 case 'u':
513 Predicate = false;
514 Signed = false;
515 Float = false;
516 break;
Andrzej Warzynski72f56582020-04-07 11:09:01 +0100517 case 'x':
518 Predicate = false;
519 Signed = true;
520 Float = false;
521 break;
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100522 case 'i':
523 Predicate = false;
524 Float = false;
525 ElementBitwidth = Bitwidth = 64;
526 NumVectors = 0;
527 Signed = false;
528 Immediate = true;
529 break;
530 case 'I':
531 Predicate = false;
532 Float = false;
533 ElementBitwidth = Bitwidth = 32;
534 NumVectors = 0;
535 Signed = true;
536 Immediate = true;
537 PredicatePattern = true;
538 break;
Sander de Smalen823e2a62020-04-24 11:31:34 +0100539 case 'J':
540 Predicate = false;
541 Float = false;
542 ElementBitwidth = Bitwidth = 32;
543 NumVectors = 0;
544 Signed = true;
545 Immediate = true;
546 PrefetchOp = true;
547 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100548 case 'k':
549 Predicate = false;
550 Signed = true;
551 Float = false;
552 ElementBitwidth = Bitwidth = 32;
553 NumVectors = 0;
554 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100555 case 'l':
556 Predicate = false;
557 Signed = true;
558 Float = false;
559 ElementBitwidth = Bitwidth = 64;
560 NumVectors = 0;
561 break;
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100562 case 'm':
563 Predicate = false;
564 Signed = false;
565 Float = false;
566 ElementBitwidth = Bitwidth = 32;
567 NumVectors = 0;
568 break;
569 case 'n':
570 Predicate = false;
571 Signed = false;
572 Float = false;
573 ElementBitwidth = Bitwidth = 64;
574 NumVectors = 0;
575 break;
Sander de Smalena5e03892020-04-23 10:53:23 +0100576 case 't':
577 Signed = true;
578 Float = false;
579 ElementBitwidth = 32;
580 break;
581 case 'z':
582 Signed = false;
583 Float = false;
584 ElementBitwidth = 32;
585 break;
Sander de Smalen00216442020-04-23 10:45:13 +0100586 case 'O':
587 Predicate = false;
588 Float = true;
589 ElementBitwidth = 16;
590 break;
591 case 'M':
592 Predicate = false;
593 Float = true;
594 ElementBitwidth = 32;
595 break;
596 case 'N':
597 Predicate = false;
598 Float = true;
599 ElementBitwidth = 64;
600 break;
Sander de Smalen17a68c62020-04-14 13:17:52 +0100601 case 'S':
602 Constant = true;
603 Pointer = true;
604 ElementBitwidth = Bitwidth = 8;
605 NumVectors = 0;
606 Signed = true;
607 break;
608 case 'W':
609 Constant = true;
610 Pointer = true;
611 ElementBitwidth = Bitwidth = 8;
612 NumVectors = 0;
613 Signed = false;
614 break;
615 case 'T':
616 Constant = true;
617 Pointer = true;
618 ElementBitwidth = Bitwidth = 16;
619 NumVectors = 0;
620 Signed = true;
621 break;
622 case 'X':
623 Constant = true;
624 Pointer = true;
625 ElementBitwidth = Bitwidth = 16;
626 NumVectors = 0;
627 Signed = false;
628 break;
629 case 'Y':
630 Constant = true;
631 Pointer = true;
632 ElementBitwidth = Bitwidth = 32;
633 NumVectors = 0;
634 Signed = false;
635 break;
636 case 'U':
637 Constant = true;
638 Pointer = true;
639 ElementBitwidth = Bitwidth = 32;
640 NumVectors = 0;
641 Signed = true;
642 break;
643 case 'A':
644 Pointer = true;
645 ElementBitwidth = Bitwidth = 8;
646 NumVectors = 0;
647 Signed = true;
648 break;
649 case 'B':
650 Pointer = true;
651 ElementBitwidth = Bitwidth = 16;
652 NumVectors = 0;
653 Signed = true;
654 break;
655 case 'C':
656 Pointer = true;
657 ElementBitwidth = Bitwidth = 32;
658 NumVectors = 0;
659 Signed = true;
660 break;
661 case 'D':
662 Pointer = true;
663 ElementBitwidth = Bitwidth = 64;
664 NumVectors = 0;
665 Signed = true;
666 break;
667 case 'E':
668 Pointer = true;
669 ElementBitwidth = Bitwidth = 8;
670 NumVectors = 0;
671 Signed = false;
672 break;
673 case 'F':
674 Pointer = true;
675 ElementBitwidth = Bitwidth = 16;
676 NumVectors = 0;
677 Signed = false;
678 break;
679 case 'G':
680 Pointer = true;
681 ElementBitwidth = Bitwidth = 32;
682 NumVectors = 0;
683 Signed = false;
684 break;
Sander de Smalenc5b81462020-03-18 11:07:20 +0000685 default:
686 llvm_unreachable("Unhandled character!");
687 }
688}
689
690
691//===----------------------------------------------------------------------===//
692// Intrinsic implementation
693//===----------------------------------------------------------------------===//
694
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100695Intrinsic::Intrinsic(StringRef Name, StringRef Proto, uint64_t MergeTy,
696 StringRef MergeSuffix, uint64_t MemoryElementTy,
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100697 StringRef LLVMName, uint64_t Flags,
698 ArrayRef<ImmCheck> Checks, TypeSpec BT, ClassKind Class,
699 SVEEmitter &Emitter, StringRef Guard)
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100700 : Name(Name.str()), LLVMName(LLVMName), Proto(Proto.str()),
701 BaseTypeSpec(BT), Class(Class), Guard(Guard.str()),
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100702 MergeSuffix(MergeSuffix.str()), BaseType(BT, 'd'), Flags(Flags),
703 ImmChecks(Checks.begin(), Checks.end()) {
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100704
705 // Types[0] is the return value.
706 for (unsigned I = 0; I < Proto.size(); ++I) {
707 SVEType T(BaseTypeSpec, Proto[I]);
708 Types.push_back(T);
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100709
710 // Add range checks for immediates
711 if (I > 0) {
712 if (T.isPredicatePattern())
713 ImmChecks.emplace_back(
714 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_31"));
Sander de Smalen823e2a62020-04-24 11:31:34 +0100715 else if (T.isPrefetchOp())
716 ImmChecks.emplace_back(
717 I - 1, Emitter.getEnumValueForImmCheck("ImmCheck0_13"));
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100718 }
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100719 }
720
721 // Set flags based on properties
722 this->Flags |= Emitter.encodeTypeFlags(BaseType);
723 this->Flags |= Emitter.encodeMemoryElementType(MemoryElementTy);
724 this->Flags |= Emitter.encodeMergeType(MergeTy);
Sander de Smalen41d52662020-04-22 13:58:35 +0100725 if (hasSplat())
726 this->Flags |= Emitter.encodeSplatOperand(getSplatIdx());
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100727}
728
Sander de Smalenc5b81462020-03-18 11:07:20 +0000729std::string Intrinsic::getBuiltinTypeStr() {
730 std::string S;
731
732 SVEType RetT = getReturnType();
733 // Since the return value must be one type, return a vector type of the
734 // appropriate width which we will bitcast. An exception is made for
735 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
736 // fashion, storing them to a pointer arg.
737 if (RetT.getNumVectors() > 1) {
738 S += "vv*"; // void result with void* first argument
739 } else
740 S += RetT.builtin_str();
741
742 for (unsigned I = 0; I < getNumParams(); ++I)
743 S += getParamType(I).builtin_str();
744
745 return S;
746}
747
748std::string Intrinsic::replaceTemplatedArgs(std::string Name, TypeSpec TS,
749 std::string Proto) const {
750 std::string Ret = Name;
751 while (Ret.find('{') != std::string::npos) {
752 size_t Pos = Ret.find('{');
753 size_t End = Ret.find('}');
754 unsigned NumChars = End - Pos + 1;
755 assert(NumChars == 3 && "Unexpected template argument");
756
757 SVEType T;
758 char C = Ret[Pos+1];
759 switch(C) {
760 default:
761 llvm_unreachable("Unknown predication specifier");
762 case 'd':
763 T = SVEType(TS, 'd');
764 break;
765 case '0':
766 case '1':
767 case '2':
768 case '3':
769 T = SVEType(TS, Proto[C - '0']);
770 break;
771 }
772
773 // Replace templated arg with the right suffix (e.g. u32)
774 std::string TypeCode;
775 if (T.isInteger())
776 TypeCode = T.isSigned() ? 's' : 'u';
777 else if (T.isPredicateVector())
778 TypeCode = 'b';
779 else
780 TypeCode = 'f';
781 Ret.replace(Pos, NumChars, TypeCode + utostr(T.getElementSizeInBits()));
782 }
783
784 return Ret;
785}
786
Sander de Smalenc5b81462020-03-18 11:07:20 +0000787std::string Intrinsic::mangleName(ClassKind LocalCK) const {
788 std::string S = getName();
789
790 if (LocalCK == ClassG) {
791 // Remove the square brackets and everything in between.
792 while (S.find("[") != std::string::npos) {
793 auto Start = S.find("[");
794 auto End = S.find(']');
795 S.erase(Start, (End-Start)+1);
796 }
797 } else {
798 // Remove the square brackets.
799 while (S.find("[") != std::string::npos) {
800 auto BrPos = S.find('[');
801 if (BrPos != std::string::npos)
802 S.erase(BrPos, 1);
803 BrPos = S.find(']');
804 if (BrPos != std::string::npos)
805 S.erase(BrPos, 1);
806 }
807 }
808
809 // Replace all {d} like expressions with e.g. 'u32'
810 return replaceTemplatedArgs(S, getBaseTypeSpec(), getProto()) +
811 getMergeSuffix();
812}
813
814void Intrinsic::emitIntrinsic(raw_ostream &OS) const {
815 // Use the preprocessor to
816 if (getClassKind() != ClassG || getProto().size() <= 1) {
817 OS << "#define " << mangleName(getClassKind())
818 << "(...) __builtin_sve_" << mangleName(ClassS)
819 << "(__VA_ARGS__)\n";
820 } else {
Sander de Smalen981f0802020-03-18 15:05:08 +0000821 std::string FullName = mangleName(ClassS);
822 std::string ProtoName = mangleName(ClassG);
823
824 OS << "__aio __attribute__((__clang_arm_builtin_alias("
825 << "__builtin_sve_" << FullName << ")))\n";
826
827 OS << getTypes()[0].str() << " " << ProtoName << "(";
828 for (unsigned I = 0; I < getTypes().size() - 1; ++I) {
829 if (I != 0)
830 OS << ", ";
831 OS << getTypes()[I + 1].str();
832 }
833 OS << ");\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000834 }
835}
836
837//===----------------------------------------------------------------------===//
838// SVEEmitter implementation
839//===----------------------------------------------------------------------===//
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100840uint64_t SVEEmitter::encodeTypeFlags(const SVEType &T) {
841 if (T.isFloat()) {
842 switch (T.getElementSizeInBits()) {
843 case 16:
844 return encodeEltType("EltTyFloat16");
845 case 32:
846 return encodeEltType("EltTyFloat32");
847 case 64:
848 return encodeEltType("EltTyFloat64");
849 default:
850 llvm_unreachable("Unhandled float element bitwidth!");
851 }
852 }
853
854 if (T.isPredicateVector()) {
855 switch (T.getElementSizeInBits()) {
856 case 8:
857 return encodeEltType("EltTyBool8");
858 case 16:
859 return encodeEltType("EltTyBool16");
860 case 32:
861 return encodeEltType("EltTyBool32");
862 case 64:
863 return encodeEltType("EltTyBool64");
864 default:
865 llvm_unreachable("Unhandled predicate element bitwidth!");
866 }
867 }
868
869 switch (T.getElementSizeInBits()) {
870 case 8:
871 return encodeEltType("EltTyInt8");
872 case 16:
873 return encodeEltType("EltTyInt16");
874 case 32:
875 return encodeEltType("EltTyInt32");
876 case 64:
877 return encodeEltType("EltTyInt64");
878 default:
879 llvm_unreachable("Unhandled integer element bitwidth!");
880 }
881}
882
Sander de Smalenc5b81462020-03-18 11:07:20 +0000883void SVEEmitter::createIntrinsic(
884 Record *R, SmallVectorImpl<std::unique_ptr<Intrinsic>> &Out) {
885 StringRef Name = R->getValueAsString("Name");
886 StringRef Proto = R->getValueAsString("Prototype");
887 StringRef Types = R->getValueAsString("Types");
888 StringRef Guard = R->getValueAsString("ArchGuard");
889 StringRef LLVMName = R->getValueAsString("LLVMIntrinsic");
Sander de Smalenf6ea0262020-04-14 15:31:20 +0100890 uint64_t Merge = R->getValueAsInt("Merge");
891 StringRef MergeSuffix = R->getValueAsString("MergeSuffix");
892 uint64_t MemEltType = R->getValueAsInt("MemEltType");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000893 std::vector<Record*> FlagsList = R->getValueAsListOfDefs("Flags");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100894 std::vector<Record*> ImmCheckList = R->getValueAsListOfDefs("ImmChecks");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000895
896 int64_t Flags = 0;
897 for (auto FlagRec : FlagsList)
898 Flags |= FlagRec->getValueAsInt("Value");
Sander de Smalenc5b81462020-03-18 11:07:20 +0000899
Sander de Smalen662cbaf2020-04-22 15:00:01 +0100900 // Create a dummy TypeSpec for non-overloaded builtins.
901 if (Types.empty()) {
902 assert((Flags & getEnumValueForFlag("IsOverloadNone")) &&
903 "Expect TypeSpec for overloaded builtin!");
904 Types = "i";
905 }
906
Sander de Smalenc5b81462020-03-18 11:07:20 +0000907 // Extract type specs from string
908 SmallVector<TypeSpec, 8> TypeSpecs;
909 TypeSpec Acc;
910 for (char I : Types) {
911 Acc.push_back(I);
912 if (islower(I)) {
913 TypeSpecs.push_back(TypeSpec(Acc));
914 Acc.clear();
915 }
916 }
917
918 // Remove duplicate type specs.
Benjamin Kramer4065e922020-03-28 19:19:55 +0100919 llvm::sort(TypeSpecs);
Sander de Smalenc5b81462020-03-18 11:07:20 +0000920 TypeSpecs.erase(std::unique(TypeSpecs.begin(), TypeSpecs.end()),
921 TypeSpecs.end());
922
923 // Create an Intrinsic for each type spec.
924 for (auto TS : TypeSpecs) {
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100925 // Collate a list of range/option checks for the immediates.
926 SmallVector<ImmCheck, 2> ImmChecks;
927 for (auto *R : ImmCheckList) {
Christopher Tetreault464a0692020-04-15 15:16:17 -0700928 int64_t Arg = R->getValueAsInt("Arg");
929 int64_t EltSizeArg = R->getValueAsInt("EltSizeArg");
930 int64_t Kind = R->getValueAsDef("Kind")->getValueAsInt("Value");
931 assert(Arg >= 0 && Kind >= 0 && "Arg and Kind must be nonnegative");
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100932
933 unsigned ElementSizeInBits = 0;
934 if (EltSizeArg >= 0)
935 ElementSizeInBits =
936 SVEType(TS, Proto[EltSizeArg + /* offset by return arg */ 1])
937 .getElementSizeInBits();
938 ImmChecks.push_back(ImmCheck(Arg, Kind, ElementSizeInBits));
939 }
940
941 Out.push_back(std::make_unique<Intrinsic>(
942 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags, ImmChecks,
943 TS, ClassS, *this, Guard));
Sander de Smalen981f0802020-03-18 15:05:08 +0000944
945 // Also generate the short-form (e.g. svadd_m) for the given type-spec.
946 if (Intrinsic::isOverloadedIntrinsic(Name))
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100947 Out.push_back(std::make_unique<Intrinsic>(
948 Name, Proto, Merge, MergeSuffix, MemEltType, LLVMName, Flags,
949 ImmChecks, TS, ClassG, *this, Guard));
Sander de Smalenc5b81462020-03-18 11:07:20 +0000950 }
951}
952
953void SVEEmitter::createHeader(raw_ostream &OS) {
Sander de Smalen5087ace2020-03-15 14:29:45 +0000954 OS << "/*===---- arm_sve.h - ARM SVE intrinsics "
955 "-----------------------------------===\n"
956 " *\n"
957 " *\n"
958 " * Part of the LLVM Project, under the Apache License v2.0 with LLVM "
959 "Exceptions.\n"
960 " * See https://llvm.org/LICENSE.txt for license information.\n"
961 " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
962 " *\n"
963 " *===-----------------------------------------------------------------"
964 "------===\n"
965 " */\n\n";
966
967 OS << "#ifndef __ARM_SVE_H\n";
968 OS << "#define __ARM_SVE_H\n\n";
969
970 OS << "#if !defined(__ARM_FEATURE_SVE)\n";
971 OS << "#error \"SVE support not enabled\"\n";
972 OS << "#else\n\n";
973
974 OS << "#include <stdint.h>\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +0000975 OS << "#ifdef __cplusplus\n";
976 OS << "extern \"C\" {\n";
977 OS << "#else\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +0000978 OS << "#include <stdbool.h>\n";
979 OS << "#endif\n\n";
980
981 OS << "typedef __fp16 float16_t;\n";
982 OS << "typedef float float32_t;\n";
983 OS << "typedef double float64_t;\n";
984 OS << "typedef bool bool_t;\n\n";
985
986 OS << "typedef __SVInt8_t svint8_t;\n";
987 OS << "typedef __SVInt16_t svint16_t;\n";
988 OS << "typedef __SVInt32_t svint32_t;\n";
989 OS << "typedef __SVInt64_t svint64_t;\n";
990 OS << "typedef __SVUint8_t svuint8_t;\n";
991 OS << "typedef __SVUint16_t svuint16_t;\n";
992 OS << "typedef __SVUint32_t svuint32_t;\n";
993 OS << "typedef __SVUint64_t svuint64_t;\n";
994 OS << "typedef __SVFloat16_t svfloat16_t;\n";
995 OS << "typedef __SVFloat32_t svfloat32_t;\n";
996 OS << "typedef __SVFloat64_t svfloat64_t;\n";
997 OS << "typedef __SVBool_t svbool_t;\n\n";
998
Sander de Smalenc8a5b302020-04-14 15:56:36 +0100999 OS << "typedef enum\n";
1000 OS << "{\n";
1001 OS << " SV_POW2 = 0,\n";
1002 OS << " SV_VL1 = 1,\n";
1003 OS << " SV_VL2 = 2,\n";
1004 OS << " SV_VL3 = 3,\n";
1005 OS << " SV_VL4 = 4,\n";
1006 OS << " SV_VL5 = 5,\n";
1007 OS << " SV_VL6 = 6,\n";
1008 OS << " SV_VL7 = 7,\n";
1009 OS << " SV_VL8 = 8,\n";
1010 OS << " SV_VL16 = 9,\n";
1011 OS << " SV_VL32 = 10,\n";
1012 OS << " SV_VL64 = 11,\n";
1013 OS << " SV_VL128 = 12,\n";
1014 OS << " SV_VL256 = 13,\n";
1015 OS << " SV_MUL4 = 29,\n";
1016 OS << " SV_MUL3 = 30,\n";
1017 OS << " SV_ALL = 31\n";
1018 OS << "} sv_pattern;\n\n";
1019
Sander de Smalen823e2a62020-04-24 11:31:34 +01001020 OS << "typedef enum\n";
1021 OS << "{\n";
1022 OS << " SV_PLDL1KEEP = 0,\n";
1023 OS << " SV_PLDL1STRM = 1,\n";
1024 OS << " SV_PLDL2KEEP = 2,\n";
1025 OS << " SV_PLDL2STRM = 3,\n";
1026 OS << " SV_PLDL3KEEP = 4,\n";
1027 OS << " SV_PLDL3STRM = 5,\n";
1028 OS << " SV_PSTL1KEEP = 8,\n";
1029 OS << " SV_PSTL1STRM = 9,\n";
1030 OS << " SV_PSTL2KEEP = 10,\n";
1031 OS << " SV_PSTL2STRM = 11,\n";
1032 OS << " SV_PSTL3KEEP = 12,\n";
1033 OS << " SV_PSTL3STRM = 13\n";
1034 OS << "} sv_prfop;\n\n";
1035
Sander de Smalen981f0802020-03-18 15:05:08 +00001036 OS << "/* Function attributes */\n";
1037 OS << "#define __aio static inline __attribute__((__always_inline__, "
1038 "__nodebug__, __overloadable__))\n\n";
1039
Sander de Smalenc5b81462020-03-18 11:07:20 +00001040 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1041 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1042 for (auto *R : RV)
1043 createIntrinsic(R, Defs);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001044
Sander de Smalenc5b81462020-03-18 11:07:20 +00001045 // Sort intrinsics in header file by following order/priority:
1046 // - Architectural guard (i.e. does it require SVE2 or SVE2_AES)
1047 // - Class (is intrinsic overloaded or not)
1048 // - Intrinsic name
1049 std::stable_sort(
1050 Defs.begin(), Defs.end(), [](const std::unique_ptr<Intrinsic> &A,
1051 const std::unique_ptr<Intrinsic> &B) {
Eric Fiselieraf2968e2020-04-16 18:35:31 -04001052 auto ToTuple = [](const std::unique_ptr<Intrinsic> &I) {
1053 return std::make_tuple(I->getGuard(), (unsigned)I->getClassKind(), I->getName());
1054 };
1055 return ToTuple(A) < ToTuple(B);
Sander de Smalenc5b81462020-03-18 11:07:20 +00001056 });
1057
1058 StringRef InGuard = "";
1059 for (auto &I : Defs) {
1060 // Emit #endif/#if pair if needed.
1061 if (I->getGuard() != InGuard) {
1062 if (!InGuard.empty())
1063 OS << "#endif //" << InGuard << "\n";
1064 InGuard = I->getGuard();
1065 if (!InGuard.empty())
1066 OS << "\n#if " << InGuard << "\n";
1067 }
1068
1069 // Actually emit the intrinsic declaration.
1070 I->emitIntrinsic(OS);
1071 }
1072
1073 if (!InGuard.empty())
1074 OS << "#endif //" << InGuard << "\n";
1075
Sander de Smalen00216442020-04-23 10:45:13 +01001076 OS << "#if defined(__ARM_FEATURE_SVE2)\n";
1077 OS << "#define svcvtnt_f16_x svcvtnt_f16_m\n";
1078 OS << "#define svcvtnt_f16_f32_x svcvtnt_f16_f32_m\n";
1079 OS << "#define svcvtnt_f32_x svcvtnt_f32_m\n";
1080 OS << "#define svcvtnt_f32_f64_x svcvtnt_f32_f64_m\n\n";
1081
1082 OS << "#define svcvtxnt_f32_x svcvtxnt_f32_m\n";
1083 OS << "#define svcvtxnt_f32_f64_x svcvtxnt_f32_f64_m\n\n";
1084
1085 OS << "#endif /*__ARM_FEATURE_SVE2 */\n\n";
1086
Sander de Smalenc5b81462020-03-18 11:07:20 +00001087 OS << "#ifdef __cplusplus\n";
1088 OS << "} // extern \"C\"\n";
1089 OS << "#endif\n\n";
1090 OS << "#endif /*__ARM_FEATURE_SVE */\n\n";
Sander de Smalen5087ace2020-03-15 14:29:45 +00001091 OS << "#endif /* __ARM_SVE_H */\n";
1092}
1093
Sander de Smalenc5b81462020-03-18 11:07:20 +00001094void SVEEmitter::createBuiltins(raw_ostream &OS) {
1095 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1096 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1097 for (auto *R : RV)
1098 createIntrinsic(R, Defs);
1099
1100 // The mappings must be sorted based on BuiltinID.
1101 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1102 const std::unique_ptr<Intrinsic> &B) {
1103 return A->getMangledName() < B->getMangledName();
1104 });
1105
1106 OS << "#ifdef GET_SVE_BUILTINS\n";
1107 for (auto &Def : Defs) {
1108 // Only create BUILTINs for non-overloaded intrinsics, as overloaded
1109 // declarations only live in the header file.
1110 if (Def->getClassKind() != ClassG)
1111 OS << "BUILTIN(__builtin_sve_" << Def->getMangledName() << ", \""
1112 << Def->getBuiltinTypeStr() << "\", \"n\")\n";
1113 }
1114 OS << "#endif\n\n";
1115}
1116
1117void SVEEmitter::createCodeGenMap(raw_ostream &OS) {
1118 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1119 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1120 for (auto *R : RV)
1121 createIntrinsic(R, Defs);
1122
1123 // The mappings must be sorted based on BuiltinID.
1124 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1125 const std::unique_ptr<Intrinsic> &B) {
1126 return A->getMangledName() < B->getMangledName();
1127 });
1128
1129 OS << "#ifdef GET_SVE_LLVM_INTRINSIC_MAP\n";
1130 for (auto &Def : Defs) {
1131 // Builtins only exist for non-overloaded intrinsics, overloaded
1132 // declarations only live in the header file.
1133 if (Def->getClassKind() == ClassG)
1134 continue;
1135
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001136 uint64_t Flags = Def->getFlags();
Sander de Smalenc5b81462020-03-18 11:07:20 +00001137 auto FlagString = std::to_string(Flags);
1138
1139 std::string LLVMName = Def->getLLVMName();
1140 std::string Builtin = Def->getMangledName();
1141 if (!LLVMName.empty())
1142 OS << "SVEMAP1(" << Builtin << ", " << LLVMName << ", " << FlagString
1143 << "),\n";
1144 else
1145 OS << "SVEMAP2(" << Builtin << ", " << FlagString << "),\n";
1146 }
1147 OS << "#endif\n\n";
1148}
1149
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001150void SVEEmitter::createRangeChecks(raw_ostream &OS) {
1151 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
1152 SmallVector<std::unique_ptr<Intrinsic>, 128> Defs;
1153 for (auto *R : RV)
1154 createIntrinsic(R, Defs);
1155
1156 // The mappings must be sorted based on BuiltinID.
1157 llvm::sort(Defs, [](const std::unique_ptr<Intrinsic> &A,
1158 const std::unique_ptr<Intrinsic> &B) {
1159 return A->getMangledName() < B->getMangledName();
1160 });
1161
1162
1163 OS << "#ifdef GET_SVE_IMMEDIATE_CHECK\n";
1164
1165 // Ensure these are only emitted once.
1166 std::set<std::string> Emitted;
1167
1168 for (auto &Def : Defs) {
1169 if (Emitted.find(Def->getMangledName()) != Emitted.end() ||
1170 Def->getImmChecks().empty())
1171 continue;
1172
1173 OS << "case SVE::BI__builtin_sve_" << Def->getMangledName() << ":\n";
1174 for (auto &Check : Def->getImmChecks())
1175 OS << "ImmChecks.push_back(std::make_tuple(" << Check.getArg() << ", "
1176 << Check.getKind() << ", " << Check.getElementSizeInBits() << "));\n";
1177 OS << " break;\n";
1178
1179 Emitted.insert(Def->getMangledName());
1180 }
1181
1182 OS << "#endif\n\n";
1183}
1184
Sander de Smalenc5b81462020-03-18 11:07:20 +00001185/// Create the SVETypeFlags used in CGBuiltins
1186void SVEEmitter::createTypeFlags(raw_ostream &OS) {
1187 OS << "#ifdef LLVM_GET_SVE_TYPEFLAGS\n";
1188 for (auto &KV : FlagTypes)
1189 OS << "const uint64_t " << KV.getKey() << " = " << KV.getValue() << ";\n";
1190 OS << "#endif\n\n";
1191
1192 OS << "#ifdef LLVM_GET_SVE_ELTTYPES\n";
1193 for (auto &KV : EltTypes)
1194 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1195 OS << "#endif\n\n";
1196
1197 OS << "#ifdef LLVM_GET_SVE_MEMELTTYPES\n";
1198 for (auto &KV : MemEltTypes)
1199 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1200 OS << "#endif\n\n";
Sander de Smalenf6ea0262020-04-14 15:31:20 +01001201
1202 OS << "#ifdef LLVM_GET_SVE_MERGETYPES\n";
1203 for (auto &KV : MergeTypes)
1204 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1205 OS << "#endif\n\n";
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001206
1207 OS << "#ifdef LLVM_GET_SVE_IMMCHECKTYPES\n";
1208 for (auto &KV : ImmCheckTypes)
1209 OS << " " << KV.getKey() << " = " << KV.getValue() << ",\n";
1210 OS << "#endif\n\n";
Sander de Smalenc5b81462020-03-18 11:07:20 +00001211}
1212
Sander de Smalen5087ace2020-03-15 14:29:45 +00001213namespace clang {
1214void EmitSveHeader(RecordKeeper &Records, raw_ostream &OS) {
Sander de Smalenc5b81462020-03-18 11:07:20 +00001215 SVEEmitter(Records).createHeader(OS);
1216}
1217
1218void EmitSveBuiltins(RecordKeeper &Records, raw_ostream &OS) {
1219 SVEEmitter(Records).createBuiltins(OS);
1220}
1221
1222void EmitSveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
1223 SVEEmitter(Records).createCodeGenMap(OS);
1224}
Sander de Smalenc8a5b302020-04-14 15:56:36 +01001225
1226void EmitSveRangeChecks(RecordKeeper &Records, raw_ostream &OS) {
1227 SVEEmitter(Records).createRangeChecks(OS);
1228}
1229
Sander de Smalenc5b81462020-03-18 11:07:20 +00001230void EmitSveTypeFlags(RecordKeeper &Records, raw_ostream &OS) {
1231 SVEEmitter(Records).createTypeFlags(OS);
Sander de Smalen5087ace2020-03-15 14:29:45 +00001232}
1233
1234} // End namespace clang