Coverage Report

Created: 2022-05-21 09:15

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/utils/TableGen/MveEmitter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MveEmitter.cpp - Generate arm_mve.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 set of linked tablegen backends is responsible for emitting the bits
10
// and pieces that implement <arm_mve.h>, which is defined by the ACLE standard
11
// and provides a set of types and functions for (more or less) direct access
12
// to the MVE instruction set, including the scalar shifts as well as the
13
// vector instructions.
14
//
15
// MVE's standard intrinsic functions are unusual in that they have a system of
16
// polymorphism. For example, the function vaddq() can behave like vaddq_u16(),
17
// vaddq_f32(), vaddq_s8(), etc., depending on the types of the vector
18
// arguments you give it.
19
//
20
// This constrains the implementation strategies. The usual approach to making
21
// the user-facing functions polymorphic would be to either use
22
// __attribute__((overloadable)) to make a set of vaddq() functions that are
23
// all inline wrappers on the underlying clang builtins, or to define a single
24
// vaddq() macro which expands to an instance of _Generic.
25
//
26
// The inline-wrappers approach would work fine for most intrinsics, except for
27
// the ones that take an argument required to be a compile-time constant,
28
// because if you wrap an inline function around a call to a builtin, the
29
// constant nature of the argument is not passed through.
30
//
31
// The _Generic approach can be made to work with enough effort, but it takes a
32
// lot of machinery, because of the design feature of _Generic that even the
33
// untaken branches are required to pass all front-end validity checks such as
34
// type-correctness. You can work around that by nesting further _Generics all
35
// over the place to coerce things to the right type in untaken branches, but
36
// what you get out is complicated, hard to guarantee its correctness, and
37
// worst of all, gives _completely unreadable_ error messages if the user gets
38
// the types wrong for an intrinsic call.
39
//
40
// Therefore, my strategy is to introduce a new __attribute__ that allows a
41
// function to be mapped to a clang builtin even though it doesn't have the
42
// same name, and then declare all the user-facing MVE function names with that
43
// attribute, mapping each one directly to the clang builtin. And the
44
// polymorphic ones have __attribute__((overloadable)) as well. So once the
45
// compiler has resolved the overload, it knows the internal builtin ID of the
46
// selected function, and can check the immediate arguments against that; and
47
// if the user gets the types wrong in a call to a polymorphic intrinsic, they
48
// get a completely clear error message showing all the declarations of that
49
// function in the header file and explaining why each one doesn't fit their
50
// call.
51
//
52
// The downside of this is that if every clang builtin has to correspond
53
// exactly to a user-facing ACLE intrinsic, then you can't save work in the
54
// frontend by doing it in the header file: CGBuiltin.cpp has to do the entire
55
// job of converting an ACLE intrinsic call into LLVM IR. So the Tablegen
56
// description for an MVE intrinsic has to contain a full description of the
57
// sequence of IRBuilder calls that clang will need to make.
58
//
59
//===----------------------------------------------------------------------===//
60
61
#include "llvm/ADT/APInt.h"
62
#include "llvm/ADT/StringRef.h"
63
#include "llvm/ADT/StringSwitch.h"
64
#include "llvm/Support/Casting.h"
65
#include "llvm/Support/raw_ostream.h"
66
#include "llvm/TableGen/Error.h"
67
#include "llvm/TableGen/Record.h"
68
#include "llvm/TableGen/StringToOffsetTable.h"
69
#include <cassert>
70
#include <cstddef>
71
#include <cstdint>
72
#include <list>
73
#include <map>
74
#include <memory>
75
#include <set>
76
#include <string>
77
#include <vector>
78
79
using namespace llvm;
80
81
namespace {
82
83
class EmitterBase;
84
class Result;
85
86
// -----------------------------------------------------------------------------
87
// A system of classes to represent all the types we'll need to deal with in
88
// the prototypes of intrinsics.
89
//
90
// Query methods include finding out the C name of a type; the "LLVM name" in
91
// the sense of a C++ code snippet that can be used in the codegen function;
92
// the suffix that represents the type in the ACLE intrinsic naming scheme
93
// (e.g. 's32' represents int32_t in intrinsics such as vaddq_s32); whether the
94
// type is floating-point related (hence should be under #ifdef in the MVE
95
// header so that it isn't included in integer-only MVE mode); and the type's
96
// size in bits. Not all subtypes support all these queries.
97
98
class Type {
99
public:
100
  enum class TypeKind {
101
    // Void appears as a return type (for store intrinsics, which are pure
102
    // side-effect). It's also used as the parameter type in the Tablegen
103
    // when an intrinsic doesn't need to come in various suffixed forms like
104
    // vfooq_s8,vfooq_u16,vfooq_f32.
105
    Void,
106
107
    // Scalar is used for ordinary int and float types of all sizes.
108
    Scalar,
109
110
    // Vector is used for anything that occupies exactly one MVE vector
111
    // register, i.e. {uint,int,float}NxM_t.
112
    Vector,
113
114
    // MultiVector is used for the {uint,int,float}NxMxK_t types used by the
115
    // interleaving load/store intrinsics v{ld,st}{2,4}q.
116
    MultiVector,
117
118
    // Predicate is used by all the predicated intrinsics. Its C
119
    // representation is mve_pred16_t (which is just an alias for uint16_t).
120
    // But we give more detail here, by indicating that a given predicate
121
    // instruction is logically regarded as a vector of i1 containing the
122
    // same number of lanes as the input vector type. So our Predicate type
123
    // comes with a lane count, which we use to decide which kind of <n x i1>
124
    // we'll invoke the pred_i2v IR intrinsic to translate it into.
125
    Predicate,
126
127
    // Pointer is used for pointer types (obviously), and comes with a flag
128
    // indicating whether it's a pointer to a const or mutable instance of
129
    // the pointee type.
130
    Pointer,
131
  };
132
133
private:
134
  const TypeKind TKind;
135
136
protected:
137
0
  Type(TypeKind K) : TKind(K) {}
138
139
public:
140
0
  TypeKind typeKind() const { return TKind; }
141
0
  virtual ~Type() = default;
142
  virtual bool requiresFloat() const = 0;
143
  virtual bool requiresMVE() const = 0;
144
  virtual unsigned sizeInBits() const = 0;
145
  virtual std::string cName() const = 0;
146
0
  virtual std::string llvmName() const {
147
0
    PrintFatalError("no LLVM type name available for type " + cName());
148
0
  }
149
0
  virtual std::string acleSuffix(std::string) const {
150
0
    PrintFatalError("no ACLE suffix available for this type");
151
0
  }
152
};
153
154
enum class ScalarTypeKind { SignedInt, UnsignedInt, Float };
155
0
inline std::string toLetter(ScalarTypeKind kind) {
156
0
  switch (kind) {
157
0
  case ScalarTypeKind::SignedInt:
158
0
    return "s";
159
0
  case ScalarTypeKind::UnsignedInt:
160
0
    return "u";
161
0
  case ScalarTypeKind::Float:
162
0
    return "f";
163
0
  }
164
0
  llvm_unreachable("Unhandled ScalarTypeKind enum");
165
0
}
166
0
inline std::string toCPrefix(ScalarTypeKind kind) {
167
0
  switch (kind) {
168
0
  case ScalarTypeKind::SignedInt:
169
0
    return "int";
170
0
  case ScalarTypeKind::UnsignedInt:
171
0
    return "uint";
172
0
  case ScalarTypeKind::Float:
173
0
    return "float";
174
0
  }
175
0
  llvm_unreachable("Unhandled ScalarTypeKind enum");
176
0
}
177
178
class VoidType : public Type {
179
public:
180
0
  VoidType() : Type(TypeKind::Void) {}
181
0
  unsigned sizeInBits() const override { return 0; }
182
0
  bool requiresFloat() const override { return false; }
183
0
  bool requiresMVE() const override { return false; }
184
0
  std::string cName() const override { return "void"; }
185
186
0
  static bool classof(const Type *T) { return T->typeKind() == TypeKind::Void; }
187
0
  std::string acleSuffix(std::string) const override { return ""; }
188
};
189
190
class PointerType : public Type {
191
  const Type *Pointee;
192
  bool Const;
193
194
public:
195
  PointerType(const Type *Pointee, bool Const)
196
0
      : Type(TypeKind::Pointer), Pointee(Pointee), Const(Const) {}
197
0
  unsigned sizeInBits() const override { return 32; }
198
0
  bool requiresFloat() const override { return Pointee->requiresFloat(); }
199
0
  bool requiresMVE() const override { return Pointee->requiresMVE(); }
200
0
  std::string cName() const override {
201
0
    std::string Name = Pointee->cName();
202
203
    // The syntax for a pointer in C is different when the pointee is
204
    // itself a pointer. The MVE intrinsics don't contain any double
205
    // pointers, so we don't need to worry about that wrinkle.
206
0
    assert(!isa<PointerType>(Pointee) && "Pointer to pointer not supported");
207
208
0
    if (Const)
209
0
      Name = "const " + Name;
210
0
    return Name + " *";
211
0
  }
212
0
  std::string llvmName() const override {
213
0
    return "llvm::PointerType::getUnqual(" + Pointee->llvmName() + ")";
214
0
  }
215
0
  const Type *getPointeeType() const { return Pointee; }
216
217
0
  static bool classof(const Type *T) {
218
0
    return T->typeKind() == TypeKind::Pointer;
219
0
  }
220
};
221
222
// Base class for all the types that have a name of the form
223
// [prefix][numbers]_t, like int32_t, uint16x8_t, float32x4x2_t.
224
//
225
// For this sub-hierarchy we invent a cNameBase() method which returns the
226
// whole name except for the trailing "_t", so that Vector and MultiVector can
227
// append an extra "x2" or whatever to their element type's cNameBase(). Then
228
// the main cName() query method puts "_t" on the end for the final type name.
229
230
class CRegularNamedType : public Type {
231
  using Type::Type;
232
  virtual std::string cNameBase() const = 0;
233
234
public:
235
0
  std::string cName() const override { return cNameBase() + "_t"; }
236
};
237
238
class ScalarType : public CRegularNamedType {
239
  ScalarTypeKind Kind;
240
  unsigned Bits;
241
  std::string NameOverride;
242
243
public:
244
0
  ScalarType(const Record *Record) : CRegularNamedType(TypeKind::Scalar) {
245
0
    Kind = StringSwitch<ScalarTypeKind>(Record->getValueAsString("kind"))
246
0
               .Case("s", ScalarTypeKind::SignedInt)
247
0
               .Case("u", ScalarTypeKind::UnsignedInt)
248
0
               .Case("f", ScalarTypeKind::Float);
249
0
    Bits = Record->getValueAsInt("size");
250
0
    NameOverride = std::string(Record->getValueAsString("nameOverride"));
251
0
  }
252
0
  unsigned sizeInBits() const override { return Bits; }
253
0
  ScalarTypeKind kind() const { return Kind; }
254
0
  std::string suffix() const { return toLetter(Kind) + utostr(Bits); }
255
0
  std::string cNameBase() const override {
256
0
    return toCPrefix(Kind) + utostr(Bits);
257
0
  }
258
0
  std::string cName() const override {
259
0
    if (NameOverride.empty())
260
0
      return CRegularNamedType::cName();
261
0
    return NameOverride;
262
0
  }
263
0
  std::string llvmName() const override {
264
0
    if (Kind == ScalarTypeKind::Float) {
265
0
      if (Bits == 16)
266
0
        return "HalfTy";
267
0
      if (Bits == 32)
268
0
        return "FloatTy";
269
0
      if (Bits == 64)
270
0
        return "DoubleTy";
271
0
      PrintFatalError("bad size for floating type");
272
0
    }
273
0
    return "Int" + utostr(Bits) + "Ty";
274
0
  }
275
0
  std::string acleSuffix(std::string overrideLetter) const override {
276
0
    return "_" + (overrideLetter.size() ? overrideLetter : toLetter(Kind))
277
0
               + utostr(Bits);
278
0
  }
279
0
  bool isInteger() const { return Kind != ScalarTypeKind::Float; }
280
0
  bool requiresFloat() const override { return !isInteger(); }
281
0
  bool requiresMVE() const override { return false; }
282
0
  bool hasNonstandardName() const { return !NameOverride.empty(); }
283
284
0
  static bool classof(const Type *T) {
285
0
    return T->typeKind() == TypeKind::Scalar;
286
0
  }
287
};
288
289
class VectorType : public CRegularNamedType {
290
  const ScalarType *Element;
291
  unsigned Lanes;
292
293
public:
294
  VectorType(const ScalarType *Element, unsigned Lanes)
295
0
      : CRegularNamedType(TypeKind::Vector), Element(Element), Lanes(Lanes) {}
296
0
  unsigned sizeInBits() const override { return Lanes * Element->sizeInBits(); }
297
0
  unsigned lanes() const { return Lanes; }
298
0
  bool requiresFloat() const override { return Element->requiresFloat(); }
299
0
  bool requiresMVE() const override { return true; }
300
0
  std::string cNameBase() const override {
301
0
    return Element->cNameBase() + "x" + utostr(Lanes);
302
0
  }
303
0
  std::string llvmName() const override {
304
0
    return "llvm::FixedVectorType::get(" + Element->llvmName() + ", " +
305
0
           utostr(Lanes) + ")";
306
0
  }
307
308
0
  static bool classof(const Type *T) {
309
0
    return T->typeKind() == TypeKind::Vector;
310
0
  }
311
};
312
313
class MultiVectorType : public CRegularNamedType {
314
  const VectorType *Element;
315
  unsigned Registers;
316
317
public:
318
  MultiVectorType(unsigned Registers, const VectorType *Element)
319
      : CRegularNamedType(TypeKind::MultiVector), Element(Element),
320
0
        Registers(Registers) {}
321
0
  unsigned sizeInBits() const override {
322
0
    return Registers * Element->sizeInBits();
323
0
  }
324
0
  unsigned registers() const { return Registers; }
325
0
  bool requiresFloat() const override { return Element->requiresFloat(); }
326
0
  bool requiresMVE() const override { return true; }
327
0
  std::string cNameBase() const override {
328
0
    return Element->cNameBase() + "x" + utostr(Registers);
329
0
  }
330
331
  // MultiVectorType doesn't override llvmName, because we don't expect to do
332
  // automatic code generation for the MVE intrinsics that use it: the {vld2,
333
  // vld4, vst2, vst4} family are the only ones that use these types, so it was
334
  // easier to hand-write the codegen for dealing with these structs than to
335
  // build in lots of extra automatic machinery that would only be used once.
336
337
0
  static bool classof(const Type *T) {
338
0
    return T->typeKind() == TypeKind::MultiVector;
339
0
  }
340
};
341
342
class PredicateType : public CRegularNamedType {
343
  unsigned Lanes;
344
345
public:
346
  PredicateType(unsigned Lanes)
347
0
      : CRegularNamedType(TypeKind::Predicate), Lanes(Lanes) {}
348
0
  unsigned sizeInBits() const override { return 16; }
349
0
  std::string cNameBase() const override { return "mve_pred16"; }
350
0
  bool requiresFloat() const override { return false; };
351
0
  bool requiresMVE() const override { return true; }
352
0
  std::string llvmName() const override {
353
0
    return "llvm::FixedVectorType::get(Builder.getInt1Ty(), " + utostr(Lanes) +
354
0
           ")";
355
0
  }
356
357
0
  static bool classof(const Type *T) {
358
0
    return T->typeKind() == TypeKind::Predicate;
359
0
  }
360
};
361
362
// -----------------------------------------------------------------------------
363
// Class to facilitate merging together the code generation for many intrinsics
364
// by means of varying a few constant or type parameters.
365
//
366
// Most obviously, the intrinsics in a single parametrised family will have
367
// code generation sequences that only differ in a type or two, e.g. vaddq_s8
368
// and vaddq_u16 will look the same apart from putting a different vector type
369
// in the call to CGM.getIntrinsic(). But also, completely different intrinsics
370
// will often code-generate in the same way, with only a different choice of
371
// _which_ IR intrinsic they lower to (e.g. vaddq_m_s8 and vmulq_m_s8), but
372
// marshalling the arguments and return values of the IR intrinsic in exactly
373
// the same way. And others might differ only in some other kind of constant,
374
// such as a lane index.
375
//
376
// So, when we generate the IR-building code for all these intrinsics, we keep
377
// track of every value that could possibly be pulled out of the code and
378
// stored ahead of time in a local variable. Then we group together intrinsics
379
// by textual equivalence of the code that would result if _all_ those
380
// parameters were stored in local variables. That gives us maximal sets that
381
// can be implemented by a single piece of IR-building code by changing
382
// parameter values ahead of time.
383
//
384
// After we've done that, we do a second pass in which we only allocate _some_
385
// of the parameters into local variables, by tracking which ones have the same
386
// values as each other (so that a single variable can be reused) and which
387
// ones are the same across the whole set (so that no variable is needed at
388
// all).
389
//
390
// Hence the class below. Its allocParam method is invoked during code
391
// generation by every method of a Result subclass (see below) that wants to
392
// give it the opportunity to pull something out into a switchable parameter.
393
// It returns a variable name for the parameter, or (if it's being used in the
394
// second pass once we've decided that some parameters don't need to be stored
395
// in variables after all) it might just return the input expression unchanged.
396
397
struct CodeGenParamAllocator {
398
  // Accumulated during code generation
399
  std::vector<std::string> *ParamTypes = nullptr;
400
  std::vector<std::string> *ParamValues = nullptr;
401
402
  // Provided ahead of time in pass 2, to indicate which parameters are being
403
  // assigned to what. This vector contains an entry for each call to
404
  // allocParam expected during code gen (which we counted up in pass 1), and
405
  // indicates the number of the parameter variable that should be returned, or
406
  // -1 if this call shouldn't allocate a parameter variable at all.
407
  //
408
  // We rely on the recursive code generation working identically in passes 1
409
  // and 2, so that the same list of calls to allocParam happen in the same
410
  // order. That guarantees that the parameter numbers recorded in pass 1 will
411
  // match the entries in this vector that store what EmitterBase::EmitBuiltinCG
412
  // decided to do about each one in pass 2.
413
  std::vector<int> *ParamNumberMap = nullptr;
414
415
  // Internally track how many things we've allocated
416
  unsigned nparams = 0;
417
418
0
  std::string allocParam(StringRef Type, StringRef Value) {
419
0
    unsigned ParamNumber;
420
421
0
    if (!ParamNumberMap) {
422
      // In pass 1, unconditionally assign a new parameter variable to every
423
      // value we're asked to process.
424
0
      ParamNumber = nparams++;
425
0
    } else {
426
      // In pass 2, consult the map provided by the caller to find out which
427
      // variable we should be keeping things in.
428
0
      int MapValue = (*ParamNumberMap)[nparams++];
429
0
      if (MapValue < 0)
430
0
        return std::string(Value);
431
0
      ParamNumber = MapValue;
432
0
    }
433
434
    // If we've allocated a new parameter variable for the first time, store
435
    // its type and value to be retrieved after codegen.
436
0
    if (ParamTypes && ParamTypes->size() == ParamNumber)
437
0
      ParamTypes->push_back(std::string(Type));
438
0
    if (ParamValues && ParamValues->size() == ParamNumber)
439
0
      ParamValues->push_back(std::string(Value));
440
441
    // Unimaginative naming scheme for parameter variables.
442
0
    return "Param" + utostr(ParamNumber);
443
0
  }
444
};
445
446
// -----------------------------------------------------------------------------
447
// System of classes that represent all the intermediate values used during
448
// code-generation for an intrinsic.
449
//
450
// The base class 'Result' can represent a value of the LLVM type 'Value', or
451
// sometimes 'Address' (for loads/stores, including an alignment requirement).
452
//
453
// In the case where the Tablegen provides a value in the codegen dag as a
454
// plain integer literal, the Result object we construct here will be one that
455
// returns true from hasIntegerConstantValue(). This allows the generated C++
456
// code to use the constant directly in contexts which can take a literal
457
// integer, such as Builder.CreateExtractValue(thing, 1), without going to the
458
// effort of calling llvm::ConstantInt::get() and then pulling the constant
459
// back out of the resulting llvm:Value later.
460
461
class Result {
462
public:
463
  // Convenient shorthand for the pointer type we'll be using everywhere.
464
  using Ptr = std::shared_ptr<Result>;
465
466
private:
467
  Ptr Predecessor;
468
  std::string VarName;
469
  bool VarNameUsed = false;
470
  unsigned Visited = 0;
471
472
public:
473
0
  virtual ~Result() = default;
474
  using Scope = std::map<std::string, Ptr>;
475
  virtual void genCode(raw_ostream &OS, CodeGenParamAllocator &) const = 0;
476
0
  virtual bool hasIntegerConstantValue() const { return false; }
477
0
  virtual uint32_t integerConstantValue() const { return 0; }
478
0
  virtual bool hasIntegerValue() const { return false; }
479
0
  virtual std::string getIntegerValue(const std::string &) {
480
0
    llvm_unreachable("non-working Result::getIntegerValue called");
481
0
  }
482
0
  virtual std::string typeName() const { return "Value *"; }
483
484
  // Mostly, when a code-generation operation has a dependency on prior
485
  // operations, it's because it uses the output values of those operations as
486
  // inputs. But there's one exception, which is the use of 'seq' in Tablegen
487
  // to indicate that operations have to be performed in sequence regardless of
488
  // whether they use each others' output values.
489
  //
490
  // So, the actual generation of code is done by depth-first search, using the
491
  // prerequisites() method to get a list of all the other Results that have to
492
  // be computed before this one. That method divides into the 'predecessor',
493
  // set by setPredecessor() while processing a 'seq' dag node, and the list
494
  // returned by 'morePrerequisites', which each subclass implements to return
495
  // a list of the Results it uses as input to whatever its own computation is
496
  // doing.
497
498
0
  virtual void morePrerequisites(std::vector<Ptr> &output) const {}
499
0
  std::vector<Ptr> prerequisites() const {
500
0
    std::vector<Ptr> ToRet;
501
0
    if (Predecessor)
502
0
      ToRet.push_back(Predecessor);
503
0
    morePrerequisites(ToRet);
504
0
    return ToRet;
505
0
  }
506
507
0
  void setPredecessor(Ptr p) {
508
    // If the user has nested one 'seq' node inside another, and this
509
    // method is called on the return value of the inner 'seq' (i.e.
510
    // the final item inside it), then we can't link _this_ node to p,
511
    // because it already has a predecessor. Instead, walk the chain
512
    // until we find the first item in the inner seq, and link that to
513
    // p, so that nesting seqs has the obvious effect of linking
514
    // everything together into one long sequential chain.
515
0
    Result *r = this;
516
0
    while (r->Predecessor)
517
0
      r = r->Predecessor.get();
518
0
    r->Predecessor = p;
519
0
  }
520
521
  // Each Result will be assigned a variable name in the output code, but not
522
  // all those variable names will actually be used (e.g. the return value of
523
  // Builder.CreateStore has void type, so nobody will want to refer to it). To
524
  // prevent annoying compiler warnings, we track whether each Result's
525
  // variable name was ever actually mentioned in subsequent statements, so
526
  // that it can be left out of the final generated code.
527
0
  std::string varname() {
528
0
    VarNameUsed = true;
529
0
    return VarName;
530
0
  }
531
0
  void setVarname(const StringRef s) { VarName = std::string(s); }
532
0
  bool varnameUsed() const { return VarNameUsed; }
533
534
  // Emit code to generate this result as a Value *.
535
0
  virtual std::string asValue() {
536
0
    return varname();
537
0
  }
538
539
  // Code generation happens in multiple passes. This method tracks whether a
540
  // Result has yet been visited in a given pass, without the need for a
541
  // tedious loop in between passes that goes through and resets a 'visited'
542
  // flag back to false: you just set Pass=1 the first time round, and Pass=2
543
  // the second time.
544
0
  bool needsVisiting(unsigned Pass) {
545
0
    bool ToRet = Visited < Pass;
546
0
    Visited = Pass;
547
0
    return ToRet;
548
0
  }
549
};
550
551
// Result subclass that retrieves one of the arguments to the clang builtin
552
// function. In cases where the argument has pointer type, we call
553
// EmitPointerWithAlignment and store the result in a variable of type Address,
554
// so that load and store IR nodes can know the right alignment. Otherwise, we
555
// call EmitScalarExpr.
556
//
557
// There are aggregate parameters in the MVE intrinsics API, but we don't deal
558
// with them in this Tablegen back end: they only arise in the vld2q/vld4q and
559
// vst2q/vst4q family, which is few enough that we just write the code by hand
560
// for those in CGBuiltin.cpp.
561
class BuiltinArgResult : public Result {
562
public:
563
  unsigned ArgNum;
564
  bool AddressType;
565
  bool Immediate;
566
  BuiltinArgResult(unsigned ArgNum, bool AddressType, bool Immediate)
567
0
      : ArgNum(ArgNum), AddressType(AddressType), Immediate(Immediate) {}
568
0
  void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
569
0
    OS << (AddressType ? "EmitPointerWithAlignment" : "EmitScalarExpr")
570
0
       << "(E->getArg(" << ArgNum << "))";
571
0
  }
572
0
  std::string typeName() const override {
573
0
    return AddressType ? "Address" : Result::typeName();
574
0
  }
575
  // Emit code to generate this result as a Value *.
576
0
  std::string asValue() override {
577
0
    if (AddressType)
578
0
      return "(" + varname() + ".getPointer())";
579
0
    return Result::asValue();
580
0
  }
581
0
  bool hasIntegerValue() const override { return Immediate; }
582
0
  std::string getIntegerValue(const std::string &IntType) override {
583
0
    return "GetIntegerConstantValue<" + IntType + ">(E->getArg(" +
584
0
           utostr(ArgNum) + "), getContext())";
585
0
  }
586
};
587
588
// Result subclass for an integer literal appearing in Tablegen. This may need
589
// to be turned into an llvm::Result by means of llvm::ConstantInt::get(), or
590
// it may be used directly as an integer, depending on which IRBuilder method
591
// it's being passed to.
592
class IntLiteralResult : public Result {
593
public:
594
  const ScalarType *IntegerType;
595
  uint32_t IntegerValue;
596
  IntLiteralResult(const ScalarType *IntegerType, uint32_t IntegerValue)
597
0
      : IntegerType(IntegerType), IntegerValue(IntegerValue) {}
598
  void genCode(raw_ostream &OS,
599
0
               CodeGenParamAllocator &ParamAlloc) const override {
600
0
    OS << "llvm::ConstantInt::get("
601
0
       << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName())
602
0
       << ", ";
603
0
    OS << ParamAlloc.allocParam(IntegerType->cName(), utostr(IntegerValue))
604
0
       << ")";
605
0
  }
606
0
  bool hasIntegerConstantValue() const override { return true; }
607
0
  uint32_t integerConstantValue() const override { return IntegerValue; }
608
};
609
610
// Result subclass representing a cast between different integer types. We use
611
// our own ScalarType abstraction as the representation of the target type,
612
// which gives both size and signedness.
613
class IntCastResult : public Result {
614
public:
615
  const ScalarType *IntegerType;
616
  Ptr V;
617
  IntCastResult(const ScalarType *IntegerType, Ptr V)
618
0
      : IntegerType(IntegerType), V(V) {}
619
  void genCode(raw_ostream &OS,
620
0
               CodeGenParamAllocator &ParamAlloc) const override {
621
0
    OS << "Builder.CreateIntCast(" << V->varname() << ", "
622
0
       << ParamAlloc.allocParam("llvm::Type *", IntegerType->llvmName()) << ", "
623
0
       << ParamAlloc.allocParam("bool",
624
0
                                IntegerType->kind() == ScalarTypeKind::SignedInt
625
0
                                    ? "true"
626
0
                                    : "false")
627
0
       << ")";
628
0
  }
629
0
  void morePrerequisites(std::vector<Ptr> &output) const override {
630
0
    output.push_back(V);
631
0
  }
632
};
633
634
// Result subclass representing a cast between different pointer types.
635
class PointerCastResult : public Result {
636
public:
637
  const PointerType *PtrType;
638
  Ptr V;
639
  PointerCastResult(const PointerType *PtrType, Ptr V)
640
0
      : PtrType(PtrType), V(V) {}
641
  void genCode(raw_ostream &OS,
642
0
               CodeGenParamAllocator &ParamAlloc) const override {
643
0
    OS << "Builder.CreatePointerCast(" << V->asValue() << ", "
644
0
       << ParamAlloc.allocParam("llvm::Type *", PtrType->llvmName()) << ")";
645
0
  }
646
0
  void morePrerequisites(std::vector<Ptr> &output) const override {
647
0
    output.push_back(V);
648
0
  }
649
};
650
651
// Result subclass representing a call to an IRBuilder method. Each IRBuilder
652
// method we want to use will have a Tablegen record giving the method name and
653
// describing any important details of how to call it, such as whether a
654
// particular argument should be an integer constant instead of an llvm::Value.
655
class IRBuilderResult : public Result {
656
public:
657
  StringRef CallPrefix;
658
  std::vector<Ptr> Args;
659
  std::set<unsigned> AddressArgs;
660
  std::map<unsigned, std::string> IntegerArgs;
661
  IRBuilderResult(StringRef CallPrefix, std::vector<Ptr> Args,
662
                  std::set<unsigned> AddressArgs,
663
                  std::map<unsigned, std::string> IntegerArgs)
664
      : CallPrefix(CallPrefix), Args(Args), AddressArgs(AddressArgs),
665
0
        IntegerArgs(IntegerArgs) {}
666
  void genCode(raw_ostream &OS,
667
0
               CodeGenParamAllocator &ParamAlloc) const override {
668
0
    OS << CallPrefix;
669
0
    const char *Sep = "";
670
0
    for (unsigned i = 0, e = Args.size(); i < e; ++i) {
671
0
      Ptr Arg = Args[i];
672
0
      auto it = IntegerArgs.find(i);
673
674
0
      OS << Sep;
675
0
      Sep = ", ";
676
677
0
      if (it != IntegerArgs.end()) {
678
0
        if (Arg->hasIntegerConstantValue())
679
0
          OS << "static_cast<" << it->second << ">("
680
0
             << ParamAlloc.allocParam(it->second,
681
0
                                      utostr(Arg->integerConstantValue()))
682
0
             << ")";
683
0
        else if (Arg->hasIntegerValue())
684
0
          OS << ParamAlloc.allocParam(it->second,
685
0
                                      Arg->getIntegerValue(it->second));
686
0
      } else {
687
0
        OS << Arg->varname();
688
0
      }
689
0
    }
690
0
    OS << ")";
691
0
  }
692
0
  void morePrerequisites(std::vector<Ptr> &output) const override {
693
0
    for (unsigned i = 0, e = Args.size(); i < e; ++i) {
694
0
      Ptr Arg = Args[i];
695
0
      if (IntegerArgs.find(i) != IntegerArgs.end())
696
0
        continue;
697
0
      output.push_back(Arg);
698
0
    }
699
0
  }
700
};
701
702
// Result subclass representing making an Address out of a Value.
703
class AddressResult : public Result {
704
public:
705
  Ptr Arg;
706
  const Type *Ty;
707
  unsigned Align;
708
  AddressResult(Ptr Arg, const Type *Ty, unsigned Align)
709
0
      : Arg(Arg), Ty(Ty), Align(Align) {}
710
  void genCode(raw_ostream &OS,
711
0
               CodeGenParamAllocator &ParamAlloc) const override {
712
0
    OS << "Address(" << Arg->varname() << ", " << Ty->llvmName()
713
0
       << ", CharUnits::fromQuantity(" << Align << "))";
714
0
  }
715
0
  std::string typeName() const override {
716
0
    return "Address";
717
0
  }
718
0
  void morePrerequisites(std::vector<Ptr> &output) const override {
719
0
    output.push_back(Arg);
720
0
  }
721
};
722
723
// Result subclass representing a call to an IR intrinsic, which we first have
724
// to look up using an Intrinsic::ID constant and an array of types.
725
class IRIntrinsicResult : public Result {
726
public:
727
  std::string IntrinsicID;
728
  std::vector<const Type *> ParamTypes;
729
  std::vector<Ptr> Args;
730
  IRIntrinsicResult(StringRef IntrinsicID, std::vector<const Type *> ParamTypes,
731
                    std::vector<Ptr> Args)
732
      : IntrinsicID(std::string(IntrinsicID)), ParamTypes(ParamTypes),
733
0
        Args(Args) {}
734
  void genCode(raw_ostream &OS,
735
0
               CodeGenParamAllocator &ParamAlloc) const override {
736
0
    std::string IntNo = ParamAlloc.allocParam(
737
0
        "Intrinsic::ID", "Intrinsic::" + IntrinsicID);
738
0
    OS << "Builder.CreateCall(CGM.getIntrinsic(" << IntNo;
739
0
    if (!ParamTypes.empty()) {
740
0
      OS << ", {";
741
0
      const char *Sep = "";
742
0
      for (auto T : ParamTypes) {
743
0
        OS << Sep << ParamAlloc.allocParam("llvm::Type *", T->llvmName());
744
0
        Sep = ", ";
745
0
      }
746
0
      OS << "}";
747
0
    }
748
0
    OS << "), {";
749
0
    const char *Sep = "";
750
0
    for (auto Arg : Args) {
751
0
      OS << Sep << Arg->asValue();
752
0
      Sep = ", ";
753
0
    }
754
0
    OS << "})";
755
0
  }
756
0
  void morePrerequisites(std::vector<Ptr> &output) const override {
757
0
    output.insert(output.end(), Args.begin(), Args.end());
758
0
  }
759
};
760
761
// Result subclass that specifies a type, for use in IRBuilder operations such
762
// as CreateBitCast that take a type argument.
763
class TypeResult : public Result {
764
public:
765
  const Type *T;
766
0
  TypeResult(const Type *T) : T(T) {}
767
0
  void genCode(raw_ostream &OS, CodeGenParamAllocator &) const override {
768
0
    OS << T->llvmName();
769
0
  }
770
0
  std::string typeName() const override {
771
0
    return "llvm::Type *";
772
0
  }
773
};
774
775
// -----------------------------------------------------------------------------
776
// Class that describes a single ACLE intrinsic.
777
//
778
// A Tablegen record will typically describe more than one ACLE intrinsic, by
779
// means of setting the 'list<Type> Params' field to a list of multiple
780
// parameter types, so as to define vaddq_{s8,u8,...,f16,f32} all in one go.
781
// We'll end up with one instance of ACLEIntrinsic for *each* parameter type,
782
// rather than a single one for all of them. Hence, the constructor takes both
783
// a Tablegen record and the current value of the parameter type.
784
785
class ACLEIntrinsic {
786
  // Structure documenting that one of the intrinsic's arguments is required to
787
  // be a compile-time constant integer, and what constraints there are on its
788
  // value. Used when generating Sema checking code.
789
  struct ImmediateArg {
790
    enum class BoundsType { ExplicitRange, UInt };
791
    BoundsType boundsType;
792
    int64_t i1, i2;
793
    StringRef ExtraCheckType, ExtraCheckArgs;
794
    const Type *ArgType;
795
  };
796
797
  // For polymorphic intrinsics, FullName is the explicit name that uniquely
798
  // identifies this variant of the intrinsic, and ShortName is the name it
799
  // shares with at least one other intrinsic.
800
  std::string ShortName, FullName;
801
802
  // Name of the architecture extension, used in the Clang builtin name
803
  StringRef BuiltinExtension;
804
805
  // A very small number of intrinsics _only_ have a polymorphic
806
  // variant (vuninitializedq taking an unevaluated argument).
807
  bool PolymorphicOnly;
808
809
  // Another rarely-used flag indicating that the builtin doesn't
810
  // evaluate its argument(s) at all.
811
  bool NonEvaluating;
812
813
  // True if the intrinsic needs only the C header part (no codegen, semantic
814
  // checks, etc). Used for redeclaring MVE intrinsics in the arm_cde.h header.
815
  bool HeaderOnly;
816
817
  const Type *ReturnType;
818
  std::vector<const Type *> ArgTypes;
819
  std::map<unsigned, ImmediateArg> ImmediateArgs;
820
  Result::Ptr Code;
821
822
  std::map<std::string, std::string> CustomCodeGenArgs;
823
824
  // Recursive function that does the internals of code generation.
825
  void genCodeDfs(Result::Ptr V, std::list<Result::Ptr> &Used,
826
0
                  unsigned Pass) const {
827
0
    if (!V->needsVisiting(Pass))
828
0
      return;
829
830
0
    for (Result::Ptr W : V->prerequisites())
831
0
      genCodeDfs(W, Used, Pass);
832
833
0
    Used.push_back(V);
834
0
  }
835
836
public:
837
0
  const std::string &shortName() const { return ShortName; }
838
0
  const std::string &fullName() const { return FullName; }
839
0
  StringRef builtinExtension() const { return BuiltinExtension; }
840
0
  const Type *returnType() const { return ReturnType; }
841
0
  const std::vector<const Type *> &argTypes() const { return ArgTypes; }
842
0
  bool requiresFloat() const {
843
0
    if (ReturnType->requiresFloat())
844
0
      return true;
845
0
    for (const Type *T : ArgTypes)
846
0
      if (T->requiresFloat())
847
0
        return true;
848
0
    return false;
849
0
  }
850
0
  bool requiresMVE() const {
851
0
    return ReturnType->requiresMVE() ||
852
0
           any_of(ArgTypes, [](const Type *T) { return T->requiresMVE(); });
853
0
  }
854
0
  bool polymorphic() const { return ShortName != FullName; }
855
0
  bool polymorphicOnly() const { return PolymorphicOnly; }
856
0
  bool nonEvaluating() const { return NonEvaluating; }
857
0
  bool headerOnly() const { return HeaderOnly; }
858
859
  // External entry point for code generation, called from EmitterBase.
860
  void genCode(raw_ostream &OS, CodeGenParamAllocator &ParamAlloc,
861
0
               unsigned Pass) const {
862
0
    assert(!headerOnly() && "Called genCode for header-only intrinsic");
863
0
    if (!hasCode()) {
864
0
      for (auto kv : CustomCodeGenArgs)
865
0
        OS << "  " << kv.first << " = " << kv.second << ";\n";
866
0
      OS << "  break; // custom code gen\n";
867
0
      return;
868
0
    }
869
0
    std::list<Result::Ptr> Used;
870
0
    genCodeDfs(Code, Used, Pass);
871
872
0
    unsigned varindex = 0;
873
0
    for (Result::Ptr V : Used)
874
0
      if (V->varnameUsed())
875
0
        V->setVarname("Val" + utostr(varindex++));
876
877
0
    for (Result::Ptr V : Used) {
878
0
      OS << "  ";
879
0
      if (V == Used.back()) {
880
0
        assert(!V->varnameUsed());
881
0
        OS << "return "; // FIXME: what if the top-level thing is void?
882
0
      } else if (V->varnameUsed()) {
883
0
        std::string Type = V->typeName();
884
0
        OS << V->typeName();
885
0
        if (!StringRef(Type).endswith("*"))
886
0
          OS << " ";
887
0
        OS << V->varname() << " = ";
888
0
      }
889
0
      V->genCode(OS, ParamAlloc);
890
0
      OS << ";\n";
891
0
    }
892
0
  }
893
0
  bool hasCode() const { return Code != nullptr; }
894
895
0
  static std::string signedHexLiteral(const llvm::APInt &iOrig) {
896
0
    llvm::APInt i = iOrig.trunc(64);
897
0
    SmallString<40> s;
898
0
    i.toString(s, 16, true, true);
899
0
    return std::string(s.str());
900
0
  }
901
902
0
  std::string genSema() const {
903
0
    assert(!headerOnly() && "Called genSema for header-only intrinsic");
904
0
    std::vector<std::string> SemaChecks;
905
906
0
    for (const auto &kv : ImmediateArgs) {
907
0
      const ImmediateArg &IA = kv.second;
908
909
0
      llvm::APInt lo(128, 0), hi(128, 0);
910
0
      switch (IA.boundsType) {
911
0
      case ImmediateArg::BoundsType::ExplicitRange:
912
0
        lo = IA.i1;
913
0
        hi = IA.i2;
914
0
        break;
915
0
      case ImmediateArg::BoundsType::UInt:
916
0
        lo = 0;
917
0
        hi = llvm::APInt::getMaxValue(IA.i1).zext(128);
918
0
        break;
919
0
      }
920
921
0
      std::string Index = utostr(kv.first);
922
923
      // Emit a range check if the legal range of values for the
924
      // immediate is smaller than the _possible_ range of values for
925
      // its type.
926
0
      unsigned ArgTypeBits = IA.ArgType->sizeInBits();
927
0
      llvm::APInt ArgTypeRange = llvm::APInt::getMaxValue(ArgTypeBits).zext(128);
928
0
      llvm::APInt ActualRange = (hi-lo).trunc(64).sext(128);
929
0
      if (ActualRange.ult(ArgTypeRange))
930
0
        SemaChecks.push_back("SemaBuiltinConstantArgRange(TheCall, " + Index +
931
0
                             ", " + signedHexLiteral(lo) + ", " +
932
0
                             signedHexLiteral(hi) + ")");
933
934
0
      if (!IA.ExtraCheckType.empty()) {
935
0
        std::string Suffix;
936
0
        if (!IA.ExtraCheckArgs.empty()) {
937
0
          std::string tmp;
938
0
          StringRef Arg = IA.ExtraCheckArgs;
939
0
          if (Arg == "!lanesize") {
940
0
            tmp = utostr(IA.ArgType->sizeInBits());
941
0
            Arg = tmp;
942
0
          }
943
0
          Suffix = (Twine(", ") + Arg).str();
944
0
        }
945
0
        SemaChecks.push_back((Twine("SemaBuiltinConstantArg") +
946
0
                              IA.ExtraCheckType + "(TheCall, " + Index +
947
0
                              Suffix + ")")
948
0
                                 .str());
949
0
      }
950
951
0
      assert(!SemaChecks.empty());
952
0
    }
953
0
    if (SemaChecks.empty())
954
0
      return "";
955
0
    return join(std::begin(SemaChecks), std::end(SemaChecks),
956
0
                " ||\n         ") +
957
0
           ";\n";
958
0
  }
959
960
  ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param);
961
};
962
963
// -----------------------------------------------------------------------------
964
// The top-level class that holds all the state from analyzing the entire
965
// Tablegen input.
966
967
class EmitterBase {
968
protected:
969
  // EmitterBase holds a collection of all the types we've instantiated.
970
  VoidType Void;
971
  std::map<std::string, std::unique_ptr<ScalarType>> ScalarTypes;
972
  std::map<std::tuple<ScalarTypeKind, unsigned, unsigned>,
973
           std::unique_ptr<VectorType>>
974
      VectorTypes;
975
  std::map<std::pair<std::string, unsigned>, std::unique_ptr<MultiVectorType>>
976
      MultiVectorTypes;
977
  std::map<unsigned, std::unique_ptr<PredicateType>> PredicateTypes;
978
  std::map<std::string, std::unique_ptr<PointerType>> PointerTypes;
979
980
  // And all the ACLEIntrinsic instances we've created.
981
  std::map<std::string, std::unique_ptr<ACLEIntrinsic>> ACLEIntrinsics;
982
983
public:
984
  // Methods to create a Type object, or return the right existing one from the
985
  // maps stored in this object.
986
0
  const VoidType *getVoidType() { return &Void; }
987
0
  const ScalarType *getScalarType(StringRef Name) {
988
0
    return ScalarTypes[std::string(Name)].get();
989
0
  }
990
0
  const ScalarType *getScalarType(Record *R) {
991
0
    return getScalarType(R->getName());
992
0
  }
993
0
  const VectorType *getVectorType(const ScalarType *ST, unsigned Lanes) {
994
0
    std::tuple<ScalarTypeKind, unsigned, unsigned> key(ST->kind(),
995
0
                                                       ST->sizeInBits(), Lanes);
996
0
    if (VectorTypes.find(key) == VectorTypes.end())
997
0
      VectorTypes[key] = std::make_unique<VectorType>(ST, Lanes);
998
0
    return VectorTypes[key].get();
999
0
  }
1000
0
  const VectorType *getVectorType(const ScalarType *ST) {
1001
0
    return getVectorType(ST, 128 / ST->sizeInBits());
1002
0
  }
1003
  const MultiVectorType *getMultiVectorType(unsigned Registers,
1004
0
                                            const VectorType *VT) {
1005
0
    std::pair<std::string, unsigned> key(VT->cNameBase(), Registers);
1006
0
    if (MultiVectorTypes.find(key) == MultiVectorTypes.end())
1007
0
      MultiVectorTypes[key] = std::make_unique<MultiVectorType>(Registers, VT);
1008
0
    return MultiVectorTypes[key].get();
1009
0
  }
1010
0
  const PredicateType *getPredicateType(unsigned Lanes) {
1011
0
    unsigned key = Lanes;
1012
0
    if (PredicateTypes.find(key) == PredicateTypes.end())
1013
0
      PredicateTypes[key] = std::make_unique<PredicateType>(Lanes);
1014
0
    return PredicateTypes[key].get();
1015
0
  }
1016
0
  const PointerType *getPointerType(const Type *T, bool Const) {
1017
0
    PointerType PT(T, Const);
1018
0
    std::string key = PT.cName();
1019
0
    if (PointerTypes.find(key) == PointerTypes.end())
1020
0
      PointerTypes[key] = std::make_unique<PointerType>(PT);
1021
0
    return PointerTypes[key].get();
1022
0
  }
1023
1024
  // Methods to construct a type from various pieces of Tablegen. These are
1025
  // always called in the context of setting up a particular ACLEIntrinsic, so
1026
  // there's always an ambient parameter type (because we're iterating through
1027
  // the Params list in the Tablegen record for the intrinsic), which is used
1028
  // to expand Tablegen classes like 'Vector' which mean something different in
1029
  // each member of a parametric family.
1030
  const Type *getType(Record *R, const Type *Param);
1031
  const Type *getType(DagInit *D, const Type *Param);
1032
  const Type *getType(Init *I, const Type *Param);
1033
1034
  // Functions that translate the Tablegen representation of an intrinsic's
1035
  // code generation into a collection of Value objects (which will then be
1036
  // reprocessed to read out the actual C++ code included by CGBuiltin.cpp).
1037
  Result::Ptr getCodeForDag(DagInit *D, const Result::Scope &Scope,
1038
                            const Type *Param);
1039
  Result::Ptr getCodeForDagArg(DagInit *D, unsigned ArgNum,
1040
                               const Result::Scope &Scope, const Type *Param);
1041
  Result::Ptr getCodeForArg(unsigned ArgNum, const Type *ArgType, bool Promote,
1042
                            bool Immediate);
1043
1044
  void GroupSemaChecks(std::map<std::string, std::set<std::string>> &Checks);
1045
1046
  // Constructor and top-level functions.
1047
1048
  EmitterBase(RecordKeeper &Records);
1049
0
  virtual ~EmitterBase() = default;
1050
1051
  virtual void EmitHeader(raw_ostream &OS) = 0;
1052
  virtual void EmitBuiltinDef(raw_ostream &OS) = 0;
1053
  virtual void EmitBuiltinSema(raw_ostream &OS) = 0;
1054
  void EmitBuiltinCG(raw_ostream &OS);
1055
  void EmitBuiltinAliases(raw_ostream &OS);
1056
};
1057
1058
0
const Type *EmitterBase::getType(Init *I, const Type *Param) {
1059
0
  if (auto Dag = dyn_cast<DagInit>(I))
1060
0
    return getType(Dag, Param);
1061
0
  if (auto Def = dyn_cast<DefInit>(I))
1062
0
    return getType(Def->getDef(), Param);
1063
1064
0
  PrintFatalError("Could not convert this value into a type");
1065
0
}
1066
1067
0
const Type *EmitterBase::getType(Record *R, const Type *Param) {
1068
  // Pass to a subfield of any wrapper records. We don't expect more than one
1069
  // of these: immediate operands are used as plain numbers rather than as
1070
  // llvm::Value, so it's meaningless to promote their type anyway.
1071
0
  if (R->isSubClassOf("Immediate"))
1072
0
    R = R->getValueAsDef("type");
1073
0
  else if (R->isSubClassOf("unpromoted"))
1074
0
    R = R->getValueAsDef("underlying_type");
1075
1076
0
  if (R->getName() == "Void")
1077
0
    return getVoidType();
1078
0
  if (R->isSubClassOf("PrimitiveType"))
1079
0
    return getScalarType(R);
1080
0
  if (R->isSubClassOf("ComplexType"))
1081
0
    return getType(R->getValueAsDag("spec"), Param);
1082
1083
0
  PrintFatalError(R->getLoc(), "Could not convert this record into a type");
1084
0
}
1085
1086
0
const Type *EmitterBase::getType(DagInit *D, const Type *Param) {
1087
  // The meat of the getType system: types in the Tablegen are represented by a
1088
  // dag whose operators select sub-cases of this function.
1089
1090
0
  Record *Op = cast<DefInit>(D->getOperator())->getDef();
1091
0
  if (!Op->isSubClassOf("ComplexTypeOp"))
1092
0
    PrintFatalError(
1093
0
        "Expected ComplexTypeOp as dag operator in type expression");
1094
1095
0
  if (Op->getName() == "CTO_Parameter") {
1096
0
    if (isa<VoidType>(Param))
1097
0
      PrintFatalError("Parametric type in unparametrised context");
1098
0
    return Param;
1099
0
  }
1100
1101
0
  if (Op->getName() == "CTO_Vec") {
1102
0
    const Type *Element = getType(D->getArg(0), Param);
1103
0
    if (D->getNumArgs() == 1) {
1104
0
      return getVectorType(cast<ScalarType>(Element));
1105
0
    } else {
1106
0
      const Type *ExistingVector = getType(D->getArg(1), Param);
1107
0
      return getVectorType(cast<ScalarType>(Element),
1108
0
                           cast<VectorType>(ExistingVector)->lanes());
1109
0
    }
1110
0
  }
1111
1112
0
  if (Op->getName() == "CTO_Pred") {
1113
0
    const Type *Element = getType(D->getArg(0), Param);
1114
0
    return getPredicateType(128 / Element->sizeInBits());
1115
0
  }
1116
1117
0
  if (Op->isSubClassOf("CTO_Tuple")) {
1118
0
    unsigned Registers = Op->getValueAsInt("n");
1119
0
    const Type *Element = getType(D->getArg(0), Param);
1120
0
    return getMultiVectorType(Registers, cast<VectorType>(Element));
1121
0
  }
1122
1123
0
  if (Op->isSubClassOf("CTO_Pointer")) {
1124
0
    const Type *Pointee = getType(D->getArg(0), Param);
1125
0
    return getPointerType(Pointee, Op->getValueAsBit("const"));
1126
0
  }
1127
1128
0
  if (Op->getName() == "CTO_CopyKind") {
1129
0
    const ScalarType *STSize = cast<ScalarType>(getType(D->getArg(0), Param));
1130
0
    const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(1), Param));
1131
0
    for (const auto &kv : ScalarTypes) {
1132
0
      const ScalarType *RT = kv.second.get();
1133
0
      if (RT->kind() == STKind->kind() && RT->sizeInBits() == STSize->sizeInBits())
1134
0
        return RT;
1135
0
    }
1136
0
    PrintFatalError("Cannot find a type to satisfy CopyKind");
1137
0
  }
1138
1139
0
  if (Op->isSubClassOf("CTO_ScaleSize")) {
1140
0
    const ScalarType *STKind = cast<ScalarType>(getType(D->getArg(0), Param));
1141
0
    int Num = Op->getValueAsInt("num"), Denom = Op->getValueAsInt("denom");
1142
0
    unsigned DesiredSize = STKind->sizeInBits() * Num / Denom;
1143
0
    for (const auto &kv : ScalarTypes) {
1144
0
      const ScalarType *RT = kv.second.get();
1145
0
      if (RT->kind() == STKind->kind() && RT->sizeInBits() == DesiredSize)
1146
0
        return RT;
1147
0
    }
1148
0
    PrintFatalError("Cannot find a type to satisfy ScaleSize");
1149
0
  }
1150
1151
0
  PrintFatalError("Bad operator in type dag expression");
1152
0
}
1153
1154
Result::Ptr EmitterBase::getCodeForDag(DagInit *D, const Result::Scope &Scope,
1155
0
                                       const Type *Param) {
1156
0
  Record *Op = cast<DefInit>(D->getOperator())->getDef();
1157
1158
0
  if (Op->getName() == "seq") {
1159
0
    Result::Scope SubScope = Scope;
1160
0
    Result::Ptr PrevV = nullptr;
1161
0
    for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i) {
1162
      // We don't use getCodeForDagArg here, because the argument name
1163
      // has different semantics in a seq
1164
0
      Result::Ptr V =
1165
0
          getCodeForDag(cast<DagInit>(D->getArg(i)), SubScope, Param);
1166
0
      StringRef ArgName = D->getArgNameStr(i);
1167
0
      if (!ArgName.empty())
1168
0
        SubScope[std::string(ArgName)] = V;
1169
0
      if (PrevV)
1170
0
        V->setPredecessor(PrevV);
1171
0
      PrevV = V;
1172
0
    }
1173
0
    return PrevV;
1174
0
  } else if (Op->isSubClassOf("Type")) {
1175
0
    if (D->getNumArgs() != 1)
1176
0
      PrintFatalError("Type casts should have exactly one argument");
1177
0
    const Type *CastType = getType(Op, Param);
1178
0
    Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
1179
0
    if (const auto *ST = dyn_cast<ScalarType>(CastType)) {
1180
0
      if (!ST->requiresFloat()) {
1181
0
        if (Arg->hasIntegerConstantValue())
1182
0
          return std::make_shared<IntLiteralResult>(
1183
0
              ST, Arg->integerConstantValue());
1184
0
        else
1185
0
          return std::make_shared<IntCastResult>(ST, Arg);
1186
0
      }
1187
0
    } else if (const auto *PT = dyn_cast<PointerType>(CastType)) {
1188
0
      return std::make_shared<PointerCastResult>(PT, Arg);
1189
0
    }
1190
0
    PrintFatalError("Unsupported type cast");
1191
0
  } else if (Op->getName() == "address") {
1192
0
    if (D->getNumArgs() != 2)
1193
0
      PrintFatalError("'address' should have two arguments");
1194
0
    Result::Ptr Arg = getCodeForDagArg(D, 0, Scope, Param);
1195
1196
0
    const Type *Ty = nullptr;
1197
0
    if (auto *DI = dyn_cast<DagInit>(D->getArg(0)))
1198
0
      if (auto *PTy = dyn_cast<PointerType>(getType(DI->getOperator(), Param)))
1199
0
        Ty = PTy->getPointeeType();
1200
0
    if (!Ty)
1201
0
      PrintFatalError("'address' pointer argument should be a pointer");
1202
1203
0
    unsigned Alignment;
1204
0
    if (auto *II = dyn_cast<IntInit>(D->getArg(1))) {
1205
0
      Alignment = II->getValue();
1206
0
    } else {
1207
0
      PrintFatalError("'address' alignment argument should be an integer");
1208
0
    }
1209
0
    return std::make_shared<AddressResult>(Arg, Ty, Alignment);
1210
0
  } else if (Op->getName() == "unsignedflag") {
1211
0
    if (D->getNumArgs() != 1)
1212
0
      PrintFatalError("unsignedflag should have exactly one argument");
1213
0
    Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
1214
0
    if (!TypeRec->isSubClassOf("Type"))
1215
0
      PrintFatalError("unsignedflag's argument should be a type");
1216
0
    if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
1217
0
      return std::make_shared<IntLiteralResult>(
1218
0
        getScalarType("u32"), ST->kind() == ScalarTypeKind::UnsignedInt);
1219
0
    } else {
1220
0
      PrintFatalError("unsignedflag's argument should be a scalar type");
1221
0
    }
1222
0
  } else if (Op->getName() == "bitsize") {
1223
0
    if (D->getNumArgs() != 1)
1224
0
      PrintFatalError("bitsize should have exactly one argument");
1225
0
    Record *TypeRec = cast<DefInit>(D->getArg(0))->getDef();
1226
0
    if (!TypeRec->isSubClassOf("Type"))
1227
0
      PrintFatalError("bitsize's argument should be a type");
1228
0
    if (const auto *ST = dyn_cast<ScalarType>(getType(TypeRec, Param))) {
1229
0
      return std::make_shared<IntLiteralResult>(getScalarType("u32"),
1230
0
                                                ST->sizeInBits());
1231
0
    } else {
1232
0
      PrintFatalError("bitsize's argument should be a scalar type");
1233
0
    }
1234
0
  } else {
1235
0
    std::vector<Result::Ptr> Args;
1236
0
    for (unsigned i = 0, e = D->getNumArgs(); i < e; ++i)
1237
0
      Args.push_back(getCodeForDagArg(D, i, Scope, Param));
1238
0
    if (Op->isSubClassOf("IRBuilderBase")) {
1239
0
      std::set<unsigned> AddressArgs;
1240
0
      std::map<unsigned, std::string> IntegerArgs;
1241
0
      for (Record *sp : Op->getValueAsListOfDefs("special_params")) {
1242
0
        unsigned Index = sp->getValueAsInt("index");
1243
0
        if (sp->isSubClassOf("IRBuilderAddrParam")) {
1244
0
          AddressArgs.insert(Index);
1245
0
        } else if (sp->isSubClassOf("IRBuilderIntParam")) {
1246
0
          IntegerArgs[Index] = std::string(sp->getValueAsString("type"));
1247
0
        }
1248
0
      }
1249
0
      return std::make_shared<IRBuilderResult>(Op->getValueAsString("prefix"),
1250
0
                                               Args, AddressArgs, IntegerArgs);
1251
0
    } else if (Op->isSubClassOf("IRIntBase")) {
1252
0
      std::vector<const Type *> ParamTypes;
1253
0
      for (Record *RParam : Op->getValueAsListOfDefs("params"))
1254
0
        ParamTypes.push_back(getType(RParam, Param));
1255
0
      std::string IntName = std::string(Op->getValueAsString("intname"));
1256
0
      if (Op->getValueAsBit("appendKind"))
1257
0
        IntName += "_" + toLetter(cast<ScalarType>(Param)->kind());
1258
0
      return std::make_shared<IRIntrinsicResult>(IntName, ParamTypes, Args);
1259
0
    } else {
1260
0
      PrintFatalError("Unsupported dag node " + Op->getName());
1261
0
    }
1262
0
  }
1263
0
}
1264
1265
Result::Ptr EmitterBase::getCodeForDagArg(DagInit *D, unsigned ArgNum,
1266
                                          const Result::Scope &Scope,
1267
0
                                          const Type *Param) {
1268
0
  Init *Arg = D->getArg(ArgNum);
1269
0
  StringRef Name = D->getArgNameStr(ArgNum);
1270
1271
0
  if (!Name.empty()) {
1272
0
    if (!isa<UnsetInit>(Arg))
1273
0
      PrintFatalError(
1274
0
          "dag operator argument should not have both a value and a name");
1275
0
    auto it = Scope.find(std::string(Name));
1276
0
    if (it == Scope.end())
1277
0
      PrintFatalError("unrecognized variable name '" + Name + "'");
1278
0
    return it->second;
1279
0
  }
1280
1281
  // Sometimes the Arg is a bit. Prior to multiclass template argument
1282
  // checking, integers would sneak through the bit declaration,
1283
  // but now they really are bits.
1284
0
  if (auto *BI = dyn_cast<BitInit>(Arg))
1285
0
    return std::make_shared<IntLiteralResult>(getScalarType("u32"),
1286
0
                                              BI->getValue());
1287
1288
0
  if (auto *II = dyn_cast<IntInit>(Arg))
1289
0
    return std::make_shared<IntLiteralResult>(getScalarType("u32"),
1290
0
                                              II->getValue());
1291
1292
0
  if (auto *DI = dyn_cast<DagInit>(Arg))
1293
0
    return getCodeForDag(DI, Scope, Param);
1294
1295
0
  if (auto *DI = dyn_cast<DefInit>(Arg)) {
1296
0
    Record *Rec = DI->getDef();
1297
0
    if (Rec->isSubClassOf("Type")) {
1298
0
      const Type *T = getType(Rec, Param);
1299
0
      return std::make_shared<TypeResult>(T);
1300
0
    }
1301
0
  }
1302
1303
0
  PrintError("bad DAG argument type for code generation");
1304
0
  PrintNote("DAG: " + D->getAsString());
1305
0
  if (TypedInit *Typed = dyn_cast<TypedInit>(Arg))
1306
0
    PrintNote("argument type: " + Typed->getType()->getAsString());
1307
0
  PrintFatalNote("argument number " + Twine(ArgNum) + ": " + Arg->getAsString());
1308
0
}
1309
1310
Result::Ptr EmitterBase::getCodeForArg(unsigned ArgNum, const Type *ArgType,
1311
0
                                       bool Promote, bool Immediate) {
1312
0
  Result::Ptr V = std::make_shared<BuiltinArgResult>(
1313
0
      ArgNum, isa<PointerType>(ArgType), Immediate);
1314
1315
0
  if (Promote) {
1316
0
    if (const auto *ST = dyn_cast<ScalarType>(ArgType)) {
1317
0
      if (ST->isInteger() && ST->sizeInBits() < 32)
1318
0
        V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
1319
0
    } else if (const auto *PT = dyn_cast<PredicateType>(ArgType)) {
1320
0
      V = std::make_shared<IntCastResult>(getScalarType("u32"), V);
1321
0
      V = std::make_shared<IRIntrinsicResult>("arm_mve_pred_i2v",
1322
0
                                              std::vector<const Type *>{PT},
1323
0
                                              std::vector<Result::Ptr>{V});
1324
0
    }
1325
0
  }
1326
1327
0
  return V;
1328
0
}
1329
1330
ACLEIntrinsic::ACLEIntrinsic(EmitterBase &ME, Record *R, const Type *Param)
1331
0
    : ReturnType(ME.getType(R->getValueAsDef("ret"), Param)) {
1332
  // Derive the intrinsic's full name, by taking the name of the
1333
  // Tablegen record (or override) and appending the suffix from its
1334
  // parameter type. (If the intrinsic is unparametrised, its
1335
  // parameter type will be given as Void, which returns the empty
1336
  // string for acleSuffix.)
1337
0
  StringRef BaseName =
1338
0
      (R->isSubClassOf("NameOverride") ? R->getValueAsString("basename")
1339
0
                                       : R->getName());
1340
0
  StringRef overrideLetter = R->getValueAsString("overrideKindLetter");
1341
0
  FullName =
1342
0
      (Twine(BaseName) + Param->acleSuffix(std::string(overrideLetter))).str();
1343
1344
  // Derive the intrinsic's polymorphic name, by removing components from the
1345
  // full name as specified by its 'pnt' member ('polymorphic name type'),
1346
  // which indicates how many type suffixes to remove, and any other piece of
1347
  // the name that should be removed.
1348
0
  Record *PolymorphicNameType = R->getValueAsDef("pnt");
1349
0
  SmallVector<StringRef, 8> NameParts;
1350
0
  StringRef(FullName).split(NameParts, '_');
1351
0
  for (unsigned i = 0, e = PolymorphicNameType->getValueAsInt(
1352
0
                           "NumTypeSuffixesToDiscard");
1353
0
       i < e; ++i)
1354
0
    NameParts.pop_back();
1355
0
  if (!PolymorphicNameType->isValueUnset("ExtraSuffixToDiscard")) {
1356
0
    StringRef ExtraSuffix =
1357
0
        PolymorphicNameType->getValueAsString("ExtraSuffixToDiscard");
1358
0
    auto it = NameParts.end();
1359
0
    while (it != NameParts.begin()) {
1360
0
      --it;
1361
0
      if (*it == ExtraSuffix) {
1362
0
        NameParts.erase(it);
1363
0
        break;
1364
0
      }
1365
0
    }
1366
0
  }
1367
0
  ShortName = join(std::begin(NameParts), std::end(NameParts), "_");
1368
1369
0
  BuiltinExtension = R->getValueAsString("builtinExtension");
1370
1371
0
  PolymorphicOnly = R->getValueAsBit("polymorphicOnly");
1372
0
  NonEvaluating = R->getValueAsBit("nonEvaluating");
1373
0
  HeaderOnly = R->getValueAsBit("headerOnly");
1374
1375
  // Process the intrinsic's argument list.
1376
0
  DagInit *ArgsDag = R->getValueAsDag("args");
1377
0
  Result::Scope Scope;
1378
0
  for (unsigned i = 0, e = ArgsDag->getNumArgs(); i < e; ++i) {
1379
0
    Init *TypeInit = ArgsDag->getArg(i);
1380
1381
0
    bool Promote = true;
1382
0
    if (auto TypeDI = dyn_cast<DefInit>(TypeInit))
1383
0
      if (TypeDI->getDef()->isSubClassOf("unpromoted"))
1384
0
        Promote = false;
1385
1386
    // Work out the type of the argument, for use in the function prototype in
1387
    // the header file.
1388
0
    const Type *ArgType = ME.getType(TypeInit, Param);
1389
0
    ArgTypes.push_back(ArgType);
1390
1391
    // If the argument is a subclass of Immediate, record the details about
1392
    // what values it can take, for Sema checking.
1393
0
    bool Immediate = false;
1394
0
    if (auto TypeDI = dyn_cast<DefInit>(TypeInit)) {
1395
0
      Record *TypeRec = TypeDI->getDef();
1396
0
      if (TypeRec->isSubClassOf("Immediate")) {
1397
0
        Immediate = true;
1398
1399
0
        Record *Bounds = TypeRec->getValueAsDef("bounds");
1400
0
        ImmediateArg &IA = ImmediateArgs[i];
1401
0
        if (Bounds->isSubClassOf("IB_ConstRange")) {
1402
0
          IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
1403
0
          IA.i1 = Bounds->getValueAsInt("lo");
1404
0
          IA.i2 = Bounds->getValueAsInt("hi");
1405
0
        } else if (Bounds->getName() == "IB_UEltValue") {
1406
0
          IA.boundsType = ImmediateArg::BoundsType::UInt;
1407
0
          IA.i1 = Param->sizeInBits();
1408
0
        } else if (Bounds->getName() == "IB_LaneIndex") {
1409
0
          IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
1410
0
          IA.i1 = 0;
1411
0
          IA.i2 = 128 / Param->sizeInBits() - 1;
1412
0
        } else if (Bounds->isSubClassOf("IB_EltBit")) {
1413
0
          IA.boundsType = ImmediateArg::BoundsType::ExplicitRange;
1414
0
          IA.i1 = Bounds->getValueAsInt("base");
1415
0
          const Type *T = ME.getType(Bounds->getValueAsDef("type"), Param);
1416
0
          IA.i2 = IA.i1 + T->sizeInBits() - 1;
1417
0
        } else {
1418
0
          PrintFatalError("unrecognised ImmediateBounds subclass");
1419
0
        }
1420
1421
0
        IA.ArgType = ArgType;
1422
1423
0
        if (!TypeRec->isValueUnset("extra")) {
1424
0
          IA.ExtraCheckType = TypeRec->getValueAsString("extra");
1425
0
          if (!TypeRec->isValueUnset("extraarg"))
1426
0
            IA.ExtraCheckArgs = TypeRec->getValueAsString("extraarg");
1427
0
        }
1428
0
      }
1429
0
    }
1430
1431
    // The argument will usually have a name in the arguments dag, which goes
1432
    // into the variable-name scope that the code gen will refer to.
1433
0
    StringRef ArgName = ArgsDag->getArgNameStr(i);
1434
0
    if (!ArgName.empty())
1435
0
      Scope[std::string(ArgName)] =
1436
0
          ME.getCodeForArg(i, ArgType, Promote, Immediate);
1437
0
  }
1438
1439
  // Finally, go through the codegen dag and translate it into a Result object
1440
  // (with an arbitrary DAG of depended-on Results hanging off it).
1441
0
  DagInit *CodeDag = R->getValueAsDag("codegen");
1442
0
  Record *MainOp = cast<DefInit>(CodeDag->getOperator())->getDef();
1443
0
  if (MainOp->isSubClassOf("CustomCodegen")) {
1444
    // Or, if it's the special case of CustomCodegen, just accumulate
1445
    // a list of parameters we're going to assign to variables before
1446
    // breaking from the loop.
1447
0
    CustomCodeGenArgs["CustomCodeGenType"] =
1448
0
        (Twine("CustomCodeGen::") + MainOp->getValueAsString("type")).str();
1449
0
    for (unsigned i = 0, e = CodeDag->getNumArgs(); i < e; ++i) {
1450
0
      StringRef Name = CodeDag->getArgNameStr(i);
1451
0
      if (Name.empty()) {
1452
0
        PrintFatalError("Operands to CustomCodegen should have names");
1453
0
      } else if (auto *II = dyn_cast<IntInit>(CodeDag->getArg(i))) {
1454
0
        CustomCodeGenArgs[std::string(Name)] = itostr(II->getValue());
1455
0
      } else if (auto *SI = dyn_cast<StringInit>(CodeDag->getArg(i))) {
1456
0
        CustomCodeGenArgs[std::string(Name)] = std::string(SI->getValue());
1457
0
      } else {
1458
0
        PrintFatalError("Operands to CustomCodegen should be integers");
1459
0
      }
1460
0
    }
1461
0
  } else {
1462
0
    Code = ME.getCodeForDag(CodeDag, Scope, Param);
1463
0
  }
1464
0
}
1465
1466
0
EmitterBase::EmitterBase(RecordKeeper &Records) {
1467
  // Construct the whole EmitterBase.
1468
1469
  // First, look up all the instances of PrimitiveType. This gives us the list
1470
  // of vector typedefs we have to put in arm_mve.h, and also allows us to
1471
  // collect all the useful ScalarType instances into a big list so that we can
1472
  // use it for operations such as 'find the unsigned version of this signed
1473
  // integer type'.
1474
0
  for (Record *R : Records.getAllDerivedDefinitions("PrimitiveType"))
1475
0
    ScalarTypes[std::string(R->getName())] = std::make_unique<ScalarType>(R);
1476
1477
  // Now go through the instances of Intrinsic, and for each one, iterate
1478
  // through its list of type parameters making an ACLEIntrinsic for each one.
1479
0
  for (Record *R : Records.getAllDerivedDefinitions("Intrinsic")) {
1480
0
    for (Record *RParam : R->getValueAsListOfDefs("params")) {
1481
0
      const Type *Param = getType(RParam, getVoidType());
1482
0
      auto Intrinsic = std::make_unique<ACLEIntrinsic>(*this, R, Param);
1483
0
      ACLEIntrinsics[Intrinsic->fullName()] = std::move(Intrinsic);
1484
0
    }
1485
0
  }
1486
0
}
1487
1488
/// A wrapper on raw_string_ostream that contains its own buffer rather than
1489
/// having to point it at one elsewhere. (In other words, it works just like
1490
/// std::ostringstream; also, this makes it convenient to declare a whole array
1491
/// of them at once.)
1492
///
1493
/// We have to set this up using multiple inheritance, to ensure that the
1494
/// string member has been constructed before raw_string_ostream's constructor
1495
/// is given a pointer to it.
1496
class string_holder {
1497
protected:
1498
  std::string S;
1499
};
1500
class raw_self_contained_string_ostream : private string_holder,
1501
                                          public raw_string_ostream {
1502
public:
1503
0
  raw_self_contained_string_ostream() : raw_string_ostream(S) {}
1504
};
1505
1506
const char LLVMLicenseHeader[] =
1507
    " *\n"
1508
    " *\n"
1509
    " * Part of the LLVM Project, under the Apache License v2.0 with LLVM"
1510
    " Exceptions.\n"
1511
    " * See https://llvm.org/LICENSE.txt for license information.\n"
1512
    " * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception\n"
1513
    " *\n"
1514
    " *===-----------------------------------------------------------------"
1515
    "------===\n"
1516
    " */\n"
1517
    "\n";
1518
1519
// Machinery for the grouping of intrinsics by similar codegen.
1520
//
1521
// The general setup is that 'MergeableGroup' stores the things that a set of
1522
// similarly shaped intrinsics have in common: the text of their code
1523
// generation, and the number and type of their parameter variables.
1524
// MergeableGroup is the key in a std::map whose value is a set of
1525
// OutputIntrinsic, which stores the ways in which a particular intrinsic
1526
// specializes the MergeableGroup's generic description: the function name and
1527
// the _values_ of the parameter variables.
1528
1529
struct ComparableStringVector : std::vector<std::string> {
1530
  // Infrastructure: a derived class of vector<string> which comes with an
1531
  // ordering, so that it can be used as a key in maps and an element in sets.
1532
  // There's no requirement on the ordering beyond being deterministic.
1533
0
  bool operator<(const ComparableStringVector &rhs) const {
1534
0
    if (size() != rhs.size())
1535
0
      return size() < rhs.size();
1536
0
    for (size_t i = 0, e = size(); i < e; ++i)
1537
0
      if ((*this)[i] != rhs[i])
1538
0
        return (*this)[i] < rhs[i];
1539
0
    return false;
1540
0
  }
1541
};
1542
1543
struct OutputIntrinsic {
1544
  const ACLEIntrinsic *Int;
1545
  std::string Name;
1546
  ComparableStringVector ParamValues;
1547
0
  bool operator<(const OutputIntrinsic &rhs) const {
1548
0
    if (Name != rhs.Name)
1549
0
      return Name < rhs.Name;
1550
0
    return ParamValues < rhs.ParamValues;
1551
0
  }
1552
};
1553
struct MergeableGroup {
1554
  std::string Code;
1555
  ComparableStringVector ParamTypes;
1556
0
  bool operator<(const MergeableGroup &rhs) const {
1557
0
    if (Code != rhs.Code)
1558
0
      return Code < rhs.Code;
1559
0
    return ParamTypes < rhs.ParamTypes;
1560
0
  }
1561
};
1562
1563
0
void EmitterBase::EmitBuiltinCG(raw_ostream &OS) {
1564
  // Pass 1: generate code for all the intrinsics as if every type or constant
1565
  // that can possibly be abstracted out into a parameter variable will be.
1566
  // This identifies the sets of intrinsics we'll group together into a single
1567
  // piece of code generation.
1568
1569
0
  std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroupsPrelim;
1570
1571
0
  for (const auto &kv : ACLEIntrinsics) {
1572
0
    const ACLEIntrinsic &Int = *kv.second;
1573
0
    if (Int.headerOnly())
1574
0
      continue;
1575
1576
0
    MergeableGroup MG;
1577
0
    OutputIntrinsic OI;
1578
1579
0
    OI.Int = &Int;
1580
0
    OI.Name = Int.fullName();
1581
0
    CodeGenParamAllocator ParamAllocPrelim{&MG.ParamTypes, &OI.ParamValues};
1582
0
    raw_string_ostream OS(MG.Code);
1583
0
    Int.genCode(OS, ParamAllocPrelim, 1);
1584
0
    OS.flush();
1585
1586
0
    MergeableGroupsPrelim[MG].insert(OI);
1587
0
  }
1588
1589
  // Pass 2: for each of those groups, optimize the parameter variable set by
1590
  // eliminating 'parameters' that are the same for all intrinsics in the
1591
  // group, and merging together pairs of parameter variables that take the
1592
  // same values as each other for all intrinsics in the group.
1593
1594
0
  std::map<MergeableGroup, std::set<OutputIntrinsic>> MergeableGroups;
1595
1596
0
  for (const auto &kv : MergeableGroupsPrelim) {
1597
0
    const MergeableGroup &MG = kv.first;
1598
0
    std::vector<int> ParamNumbers;
1599
0
    std::map<ComparableStringVector, int> ParamNumberMap;
1600
1601
    // Loop over the parameters for this group.
1602
0
    for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
1603
      // Is this parameter the same for all intrinsics in the group?
1604
0
      const OutputIntrinsic &OI_first = *kv.second.begin();
1605
0
      bool Constant = all_of(kv.second, [&](const OutputIntrinsic &OI) {
1606
0
        return OI.ParamValues[i] == OI_first.ParamValues[i];
1607
0
      });
1608
1609
      // If so, record it as -1, meaning 'no parameter variable needed'. Then
1610
      // the corresponding call to allocParam in pass 2 will not generate a
1611
      // variable at all, and just use the value inline.
1612
0
      if (Constant) {
1613
0
        ParamNumbers.push_back(-1);
1614
0
        continue;
1615
0
      }
1616
1617
      // Otherwise, make a list of the values this parameter takes for each
1618
      // intrinsic, and see if that value vector matches anything we already
1619
      // have. We also record the parameter type, so that we don't accidentally
1620
      // match up two parameter variables with different types. (Not that
1621
      // there's much chance of them having textually equivalent values, but in
1622
      // _principle_ it could happen.)
1623
0
      ComparableStringVector key;
1624
0
      key.push_back(MG.ParamTypes[i]);
1625
0
      for (const auto &OI : kv.second)
1626
0
        key.push_back(OI.ParamValues[i]);
1627
1628
0
      auto Found = ParamNumberMap.find(key);
1629
0
      if (Found != ParamNumberMap.end()) {
1630
        // Yes, an existing parameter variable can be reused for this.
1631
0
        ParamNumbers.push_back(Found->second);
1632
0
        continue;
1633
0
      }
1634
1635
      // No, we need a new parameter variable.
1636
0
      int ExistingIndex = ParamNumberMap.size();
1637
0
      ParamNumberMap[key] = ExistingIndex;
1638
0
      ParamNumbers.push_back(ExistingIndex);
1639
0
    }
1640
1641
    // Now we're ready to do the pass 2 code generation, which will emit the
1642
    // reduced set of parameter variables we've just worked out.
1643
1644
0
    for (const auto &OI_prelim : kv.second) {
1645
0
      const ACLEIntrinsic *Int = OI_prelim.Int;
1646
1647
0
      MergeableGroup MG;
1648
0
      OutputIntrinsic OI;
1649
1650
0
      OI.Int = OI_prelim.Int;
1651
0
      OI.Name = OI_prelim.Name;
1652
0
      CodeGenParamAllocator ParamAlloc{&MG.ParamTypes, &OI.ParamValues,
1653
0
                                       &ParamNumbers};
1654
0
      raw_string_ostream OS(MG.Code);
1655
0
      Int->genCode(OS, ParamAlloc, 2);
1656
0
      OS.flush();
1657
1658
0
      MergeableGroups[MG].insert(OI);
1659
0
    }
1660
0
  }
1661
1662
  // Output the actual C++ code.
1663
1664
0
  for (const auto &kv : MergeableGroups) {
1665
0
    const MergeableGroup &MG = kv.first;
1666
1667
    // List of case statements in the main switch on BuiltinID, and an open
1668
    // brace.
1669
0
    const char *prefix = "";
1670
0
    for (const auto &OI : kv.second) {
1671
0
      OS << prefix << "case ARM::BI__builtin_arm_" << OI.Int->builtinExtension()
1672
0
         << "_" << OI.Name << ":";
1673
1674
0
      prefix = "\n";
1675
0
    }
1676
0
    OS << " {\n";
1677
1678
0
    if (!MG.ParamTypes.empty()) {
1679
      // If we've got some parameter variables, then emit their declarations...
1680
0
      for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i) {
1681
0
        StringRef Type = MG.ParamTypes[i];
1682
0
        OS << "  " << Type;
1683
0
        if (!Type.endswith("*"))
1684
0
          OS << " ";
1685
0
        OS << " Param" << utostr(i) << ";\n";
1686
0
      }
1687
1688
      // ... and an inner switch on BuiltinID that will fill them in with each
1689
      // individual intrinsic's values.
1690
0
      OS << "  switch (BuiltinID) {\n";
1691
0
      for (const auto &OI : kv.second) {
1692
0
        OS << "  case ARM::BI__builtin_arm_" << OI.Int->builtinExtension()
1693
0
           << "_" << OI.Name << ":\n";
1694
0
        for (size_t i = 0, e = MG.ParamTypes.size(); i < e; ++i)
1695
0
          OS << "    Param" << utostr(i) << " = " << OI.ParamValues[i] << ";\n";
1696
0
        OS << "    break;\n";
1697
0
      }
1698
0
      OS << "  }\n";
1699
0
    }
1700
1701
    // And finally, output the code, and close the outer pair of braces. (The
1702
    // code will always end with a 'return' statement, so we need not insert a
1703
    // 'break' here.)
1704
0
    OS << MG.Code << "}\n";
1705
0
  }
1706
0
}
1707
1708
0
void EmitterBase::EmitBuiltinAliases(raw_ostream &OS) {
1709
  // Build a sorted table of:
1710
  // - intrinsic id number
1711
  // - full name
1712
  // - polymorphic name or -1
1713
0
  StringToOffsetTable StringTable;
1714
0
  OS << "static const IntrinToName MapData[] = {\n";
1715
0
  for (const auto &kv : ACLEIntrinsics) {
1716
0
    const ACLEIntrinsic &Int = *kv.second;
1717
0
    if (Int.headerOnly())
1718
0
      continue;
1719
0
    int32_t ShortNameOffset =
1720
0
        Int.polymorphic() ? StringTable.GetOrAddStringOffset(Int.shortName())
1721
0
                          : -1;
1722
0
    OS << "  { ARM::BI__builtin_arm_" << Int.builtinExtension() << "_"
1723
0
       << Int.fullName() << ", "
1724
0
       << StringTable.GetOrAddStringOffset(Int.fullName()) << ", "
1725
0
       << ShortNameOffset << "},\n";
1726
0
  }
1727
0
  OS << "};\n\n";
1728
1729
0
  OS << "ArrayRef<IntrinToName> Map(MapData);\n\n";
1730
1731
0
  OS << "static const char IntrinNames[] = {\n";
1732
0
  StringTable.EmitString(OS);
1733
0
  OS << "};\n\n";
1734
0
}
1735
1736
void EmitterBase::GroupSemaChecks(
1737
0
    std::map<std::string, std::set<std::string>> &Checks) {
1738
0
  for (const auto &kv : ACLEIntrinsics) {
1739
0
    const ACLEIntrinsic &Int = *kv.second;
1740
0
    if (Int.headerOnly())
1741
0
      continue;
1742
0
    std::string Check = Int.genSema();
1743
0
    if (!Check.empty())
1744
0
      Checks[Check].insert(Int.fullName());
1745
0
  }
1746
0
}
1747
1748
// -----------------------------------------------------------------------------
1749
// The class used for generating arm_mve.h and related Clang bits
1750
//
1751
1752
class MveEmitter : public EmitterBase {
1753
public:
1754
0
  MveEmitter(RecordKeeper &Records) : EmitterBase(Records){};
1755
  void EmitHeader(raw_ostream &OS) override;
1756
  void EmitBuiltinDef(raw_ostream &OS) override;
1757
  void EmitBuiltinSema(raw_ostream &OS) override;
1758
};
1759
1760
0
void MveEmitter::EmitHeader(raw_ostream &OS) {
1761
  // Accumulate pieces of the header file that will be enabled under various
1762
  // different combinations of #ifdef. The index into parts[] is made up of
1763
  // the following bit flags.
1764
0
  constexpr unsigned Float = 1;
1765
0
  constexpr unsigned UseUserNamespace = 2;
1766
1767
0
  constexpr unsigned NumParts = 4;
1768
0
  raw_self_contained_string_ostream parts[NumParts];
1769
1770
  // Write typedefs for all the required vector types, and a few scalar
1771
  // types that don't already have the name we want them to have.
1772
1773
0
  parts[0] << "typedef uint16_t mve_pred16_t;\n";
1774
0
  parts[Float] << "typedef __fp16 float16_t;\n"
1775
0
                  "typedef float float32_t;\n";
1776
0
  for (const auto &kv : ScalarTypes) {
1777
0
    const ScalarType *ST = kv.second.get();
1778
0
    if (ST->hasNonstandardName())
1779
0
      continue;
1780
0
    raw_ostream &OS = parts[ST->requiresFloat() ? Float : 0];
1781
0
    const VectorType *VT = getVectorType(ST);
1782
1783
0
    OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
1784
0
       << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
1785
0
       << VT->cName() << ";\n";
1786
1787
    // Every vector type also comes with a pair of multi-vector types for
1788
    // the VLD2 and VLD4 instructions.
1789
0
    for (unsigned n = 2; n <= 4; n += 2) {
1790
0
      const MultiVectorType *MT = getMultiVectorType(n, VT);
1791
0
      OS << "typedef struct { " << VT->cName() << " val[" << n << "]; } "
1792
0
         << MT->cName() << ";\n";
1793
0
    }
1794
0
  }
1795
0
  parts[0] << "\n";
1796
0
  parts[Float] << "\n";
1797
1798
  // Write declarations for all the intrinsics.
1799
1800
0
  for (const auto &kv : ACLEIntrinsics) {
1801
0
    const ACLEIntrinsic &Int = *kv.second;
1802
1803
    // We generate each intrinsic twice, under its full unambiguous
1804
    // name and its shorter polymorphic name (if the latter exists).
1805
0
    for (bool Polymorphic : {false, true}) {
1806
0
      if (Polymorphic && !Int.polymorphic())
1807
0
        continue;
1808
0
      if (!Polymorphic && Int.polymorphicOnly())
1809
0
        continue;
1810
1811
      // We also generate each intrinsic under a name like __arm_vfooq
1812
      // (which is in C language implementation namespace, so it's
1813
      // safe to define in any conforming user program) and a shorter
1814
      // one like vfooq (which is in user namespace, so a user might
1815
      // reasonably have used it for something already). If so, they
1816
      // can #define __ARM_MVE_PRESERVE_USER_NAMESPACE before
1817
      // including the header, which will suppress the shorter names
1818
      // and leave only the implementation-namespace ones. Then they
1819
      // have to write __arm_vfooq everywhere, of course.
1820
1821
0
      for (bool UserNamespace : {false, true}) {
1822
0
        raw_ostream &OS = parts[(Int.requiresFloat() ? Float : 0) |
1823
0
                                (UserNamespace ? UseUserNamespace : 0)];
1824
1825
        // Make the name of the function in this declaration.
1826
1827
0
        std::string FunctionName =
1828
0
            Polymorphic ? Int.shortName() : Int.fullName();
1829
0
        if (!UserNamespace)
1830
0
          FunctionName = "__arm_" + FunctionName;
1831
1832
        // Make strings for the types involved in the function's
1833
        // prototype.
1834
1835
0
        std::string RetTypeName = Int.returnType()->cName();
1836
0
        if (!StringRef(RetTypeName).endswith("*"))
1837
0
          RetTypeName += " ";
1838
1839
0
        std::vector<std::string> ArgTypeNames;
1840
0
        for (const Type *ArgTypePtr : Int.argTypes())
1841
0
          ArgTypeNames.push_back(ArgTypePtr->cName());
1842
0
        std::string ArgTypesString =
1843
0
            join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
1844
1845
        // Emit the actual declaration. All these functions are
1846
        // declared 'static inline' without a body, which is fine
1847
        // provided clang recognizes them as builtins, and has the
1848
        // effect that this type signature is used in place of the one
1849
        // that Builtins.def didn't provide. That's how we can get
1850
        // structure types that weren't defined until this header was
1851
        // included to be part of the type signature of a builtin that
1852
        // was known to clang already.
1853
        //
1854
        // The declarations use __attribute__(__clang_arm_builtin_alias),
1855
        // so that each function declared will be recognized as the
1856
        // appropriate MVE builtin in spite of its user-facing name.
1857
        //
1858
        // (That's better than making them all wrapper functions,
1859
        // partly because it avoids any compiler error message citing
1860
        // the wrapper function definition instead of the user's code,
1861
        // and mostly because some MVE intrinsics have arguments
1862
        // required to be compile-time constants, and that property
1863
        // can't be propagated through a wrapper function. It can be
1864
        // propagated through a macro, but macros can't be overloaded
1865
        // on argument types very easily - you have to use _Generic,
1866
        // which makes error messages very confusing when the user
1867
        // gets it wrong.)
1868
        //
1869
        // Finally, the polymorphic versions of the intrinsics are
1870
        // also defined with __attribute__(overloadable), so that when
1871
        // the same name is defined with several type signatures, the
1872
        // right thing happens. Each one of the overloaded
1873
        // declarations is given a different builtin id, which
1874
        // has exactly the effect we want: first clang resolves the
1875
        // overload to the right function, then it knows which builtin
1876
        // it's referring to, and then the Sema checking for that
1877
        // builtin can check further things like the constant
1878
        // arguments.
1879
        //
1880
        // One more subtlety is the newline just before the return
1881
        // type name. That's a cosmetic tweak to make the error
1882
        // messages legible if the user gets the types wrong in a call
1883
        // to a polymorphic function: this way, clang will print just
1884
        // the _final_ line of each declaration in the header, to show
1885
        // the type signatures that would have been legal. So all the
1886
        // confusing machinery with __attribute__ is left out of the
1887
        // error message, and the user sees something that's more or
1888
        // less self-documenting: "here's a list of actually readable
1889
        // type signatures for vfooq(), and here's why each one didn't
1890
        // match your call".
1891
1892
0
        OS << "static __inline__ __attribute__(("
1893
0
           << (Polymorphic ? "__overloadable__, " : "")
1894
0
           << "__clang_arm_builtin_alias(__builtin_arm_mve_" << Int.fullName()
1895
0
           << ")))\n"
1896
0
           << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
1897
0
      }
1898
0
    }
1899
0
  }
1900
0
  for (auto &part : parts)
1901
0
    part << "\n";
1902
1903
  // Now we've finished accumulating bits and pieces into the parts[] array.
1904
  // Put it all together to write the final output file.
1905
1906
0
  OS << "/*===---- arm_mve.h - ARM MVE intrinsics "
1907
0
        "-----------------------------------===\n"
1908
0
     << LLVMLicenseHeader
1909
0
     << "#ifndef __ARM_MVE_H\n"
1910
0
        "#define __ARM_MVE_H\n"
1911
0
        "\n"
1912
0
        "#if !__ARM_FEATURE_MVE\n"
1913
0
        "#error \"MVE support not enabled\"\n"
1914
0
        "#endif\n"
1915
0
        "\n"
1916
0
        "#include <stdint.h>\n"
1917
0
        "\n"
1918
0
        "#ifdef __cplusplus\n"
1919
0
        "extern \"C\" {\n"
1920
0
        "#endif\n"
1921
0
        "\n";
1922
1923
0
  for (size_t i = 0; i < NumParts; ++i) {
1924
0
    std::vector<std::string> conditions;
1925
0
    if (i & Float)
1926
0
      conditions.push_back("(__ARM_FEATURE_MVE & 2)");
1927
0
    if (i & UseUserNamespace)
1928
0
      conditions.push_back("(!defined __ARM_MVE_PRESERVE_USER_NAMESPACE)");
1929
1930
0
    std::string condition =
1931
0
        join(std::begin(conditions), std::end(conditions), " && ");
1932
0
    if (!condition.empty())
1933
0
      OS << "#if " << condition << "\n\n";
1934
0
    OS << parts[i].str();
1935
0
    if (!condition.empty())
1936
0
      OS << "#endif /* " << condition << " */\n\n";
1937
0
  }
1938
1939
0
  OS << "#ifdef __cplusplus\n"
1940
0
        "} /* extern \"C\" */\n"
1941
0
        "#endif\n"
1942
0
        "\n"
1943
0
        "#endif /* __ARM_MVE_H */\n";
1944
0
}
1945
1946
0
void MveEmitter::EmitBuiltinDef(raw_ostream &OS) {
1947
0
  for (const auto &kv : ACLEIntrinsics) {
1948
0
    const ACLEIntrinsic &Int = *kv.second;
1949
0
    OS << "BUILTIN(__builtin_arm_mve_" << Int.fullName()
1950
0
       << ", \"\", \"n\")\n";
1951
0
  }
1952
1953
0
  std::set<std::string> ShortNamesSeen;
1954
1955
0
  for (const auto &kv : ACLEIntrinsics) {
1956
0
    const ACLEIntrinsic &Int = *kv.second;
1957
0
    if (Int.polymorphic()) {
1958
0
      StringRef Name = Int.shortName();
1959
0
      if (ShortNamesSeen.find(std::string(Name)) == ShortNamesSeen.end()) {
1960
0
        OS << "BUILTIN(__builtin_arm_mve_" << Name << ", \"vi.\", \"nt";
1961
0
        if (Int.nonEvaluating())
1962
0
          OS << "u"; // indicate that this builtin doesn't evaluate its args
1963
0
        OS << "\")\n";
1964
0
        ShortNamesSeen.insert(std::string(Name));
1965
0
      }
1966
0
    }
1967
0
  }
1968
0
}
1969
1970
0
void MveEmitter::EmitBuiltinSema(raw_ostream &OS) {
1971
0
  std::map<std::string, std::set<std::string>> Checks;
1972
0
  GroupSemaChecks(Checks);
1973
1974
0
  for (const auto &kv : Checks) {
1975
0
    for (StringRef Name : kv.second)
1976
0
      OS << "case ARM::BI__builtin_arm_mve_" << Name << ":\n";
1977
0
    OS << "  return " << kv.first;
1978
0
  }
1979
0
}
1980
1981
// -----------------------------------------------------------------------------
1982
// Class that describes an ACLE intrinsic implemented as a macro.
1983
//
1984
// This class is used when the intrinsic is polymorphic in 2 or 3 types, but we
1985
// want to avoid a combinatorial explosion by reinterpreting the arguments to
1986
// fixed types.
1987
1988
class FunctionMacro {
1989
  std::vector<StringRef> Params;
1990
  StringRef Definition;
1991
1992
public:
1993
  FunctionMacro(const Record &R);
1994
1995
0
  const std::vector<StringRef> &getParams() const { return Params; }
1996
0
  StringRef getDefinition() const { return Definition; }
1997
};
1998
1999
0
FunctionMacro::FunctionMacro(const Record &R) {
2000
0
  Params = R.getValueAsListOfStrings("params");
2001
0
  Definition = R.getValueAsString("definition");
2002
0
}
2003
2004
// -----------------------------------------------------------------------------
2005
// The class used for generating arm_cde.h and related Clang bits
2006
//
2007
2008
class CdeEmitter : public EmitterBase {
2009
  std::map<StringRef, FunctionMacro> FunctionMacros;
2010
2011
public:
2012
  CdeEmitter(RecordKeeper &Records);
2013
  void EmitHeader(raw_ostream &OS) override;
2014
  void EmitBuiltinDef(raw_ostream &OS) override;
2015
  void EmitBuiltinSema(raw_ostream &OS) override;
2016
};
2017
2018
0
CdeEmitter::CdeEmitter(RecordKeeper &Records) : EmitterBase(Records) {
2019
0
  for (Record *R : Records.getAllDerivedDefinitions("FunctionMacro"))
2020
0
    FunctionMacros.emplace(R->getName(), FunctionMacro(*R));
2021
0
}
2022
2023
0
void CdeEmitter::EmitHeader(raw_ostream &OS) {
2024
  // Accumulate pieces of the header file that will be enabled under various
2025
  // different combinations of #ifdef. The index into parts[] is one of the
2026
  // following:
2027
0
  constexpr unsigned None = 0;
2028
0
  constexpr unsigned MVE = 1;
2029
0
  constexpr unsigned MVEFloat = 2;
2030
2031
0
  constexpr unsigned NumParts = 3;
2032
0
  raw_self_contained_string_ostream parts[NumParts];
2033
2034
  // Write typedefs for all the required vector types, and a few scalar
2035
  // types that don't already have the name we want them to have.
2036
2037
0
  parts[MVE] << "typedef uint16_t mve_pred16_t;\n";
2038
0
  parts[MVEFloat] << "typedef __fp16 float16_t;\n"
2039
0
                     "typedef float float32_t;\n";
2040
0
  for (const auto &kv : ScalarTypes) {
2041
0
    const ScalarType *ST = kv.second.get();
2042
0
    if (ST->hasNonstandardName())
2043
0
      continue;
2044
    // We don't have float64x2_t
2045
0
    if (ST->kind() == ScalarTypeKind::Float && ST->sizeInBits() == 64)
2046
0
      continue;
2047
0
    raw_ostream &OS = parts[ST->requiresFloat() ? MVEFloat : MVE];
2048
0
    const VectorType *VT = getVectorType(ST);
2049
2050
0
    OS << "typedef __attribute__((__neon_vector_type__(" << VT->lanes()
2051
0
       << "), __clang_arm_mve_strict_polymorphism)) " << ST->cName() << " "
2052
0
       << VT->cName() << ";\n";
2053
0
  }
2054
0
  parts[MVE] << "\n";
2055
0
  parts[MVEFloat] << "\n";
2056
2057
  // Write declarations for all the intrinsics.
2058
2059
0
  for (const auto &kv : ACLEIntrinsics) {
2060
0
    const ACLEIntrinsic &Int = *kv.second;
2061
2062
    // We generate each intrinsic twice, under its full unambiguous
2063
    // name and its shorter polymorphic name (if the latter exists).
2064
0
    for (bool Polymorphic : {false, true}) {
2065
0
      if (Polymorphic && !Int.polymorphic())
2066
0
        continue;
2067
0
      if (!Polymorphic && Int.polymorphicOnly())
2068
0
        continue;
2069
2070
0
      raw_ostream &OS =
2071
0
          parts[Int.requiresFloat() ? MVEFloat
2072
0
                                    : Int.requiresMVE() ? MVE : None];
2073
2074
      // Make the name of the function in this declaration.
2075
0
      std::string FunctionName =
2076
0
          "__arm_" + (Polymorphic ? Int.shortName() : Int.fullName());
2077
2078
      // Make strings for the types involved in the function's
2079
      // prototype.
2080
0
      std::string RetTypeName = Int.returnType()->cName();
2081
0
      if (!StringRef(RetTypeName).endswith("*"))
2082
0
        RetTypeName += " ";
2083
2084
0
      std::vector<std::string> ArgTypeNames;
2085
0
      for (const Type *ArgTypePtr : Int.argTypes())
2086
0
        ArgTypeNames.push_back(ArgTypePtr->cName());
2087
0
      std::string ArgTypesString =
2088
0
          join(std::begin(ArgTypeNames), std::end(ArgTypeNames), ", ");
2089
2090
      // Emit the actual declaration. See MveEmitter::EmitHeader for detailed
2091
      // comments
2092
0
      OS << "static __inline__ __attribute__(("
2093
0
         << (Polymorphic ? "__overloadable__, " : "")
2094
0
         << "__clang_arm_builtin_alias(__builtin_arm_" << Int.builtinExtension()
2095
0
         << "_" << Int.fullName() << ")))\n"
2096
0
         << RetTypeName << FunctionName << "(" << ArgTypesString << ");\n";
2097
0
    }
2098
0
  }
2099
2100
0
  for (const auto &kv : FunctionMacros) {
2101
0
    StringRef Name = kv.first;
2102
0
    const FunctionMacro &FM = kv.second;
2103
2104
0
    raw_ostream &OS = parts[MVE];
2105
0
    OS << "#define "
2106
0
       << "__arm_" << Name << "(" << join(FM.getParams(), ", ") << ") "
2107
0
       << FM.getDefinition() << "\n";
2108
0
  }
2109
2110
0
  for (auto &part : parts)
2111
0
    part << "\n";
2112
2113
  // Now we've finished accumulating bits and pieces into the parts[] array.
2114
  // Put it all together to write the final output file.
2115
2116
0
  OS << "/*===---- arm_cde.h - ARM CDE intrinsics "
2117
0
        "-----------------------------------===\n"
2118
0
     << LLVMLicenseHeader
2119
0
     << "#ifndef __ARM_CDE_H\n"
2120
0
        "#define __ARM_CDE_H\n"
2121
0
        "\n"
2122
0
        "#if !__ARM_FEATURE_CDE\n"
2123
0
        "#error \"CDE support not enabled\"\n"
2124
0
        "#endif\n"
2125
0
        "\n"
2126
0
        "#include <stdint.h>\n"
2127
0
        "\n"
2128
0
        "#ifdef __cplusplus\n"
2129
0
        "extern \"C\" {\n"
2130
0
        "#endif\n"
2131
0
        "\n";
2132
2133
0
  for (size_t i = 0; i < NumParts; ++i) {
2134
0
    std::string condition;
2135
0
    if (i == MVEFloat)
2136
0
      condition = "__ARM_FEATURE_MVE & 2";
2137
0
    else if (i == MVE)
2138
0
      condition = "__ARM_FEATURE_MVE";
2139
2140
0
    if (!condition.empty())
2141
0
      OS << "#if " << condition << "\n\n";
2142
0
    OS << parts[i].str();
2143
0
    if (!condition.empty())
2144
0
      OS << "#endif /* " << condition << " */\n\n";
2145
0
  }
2146
2147
0
  OS << "#ifdef __cplusplus\n"
2148
0
        "} /* extern \"C\" */\n"
2149
0
        "#endif\n"
2150
0
        "\n"
2151
0
        "#endif /* __ARM_CDE_H */\n";
2152
0
}
2153
2154
0
void CdeEmitter::EmitBuiltinDef(raw_ostream &OS) {
2155
0
  for (const auto &kv : ACLEIntrinsics) {
2156
0
    if (kv.second->headerOnly())
2157
0
      continue;
2158
0
    const ACLEIntrinsic &Int = *kv.second;
2159
0
    OS << "BUILTIN(__builtin_arm_cde_" << Int.fullName()
2160
0
       << ", \"\", \"ncU\")\n";
2161
0
  }
2162
0
}
2163
2164
0
void CdeEmitter::EmitBuiltinSema(raw_ostream &OS) {
2165
0
  std::map<std::string, std::set<std::string>> Checks;
2166
0
  GroupSemaChecks(Checks);
2167
2168
0
  for (const auto &kv : Checks) {
2169
0
    for (StringRef Name : kv.second)
2170
0
      OS << "case ARM::BI__builtin_arm_cde_" << Name << ":\n";
2171
0
    OS << "  Err = " << kv.first << "  break;\n";
2172
0
  }
2173
0
}
2174
2175
} // namespace
2176
2177
namespace clang {
2178
2179
// MVE
2180
2181
0
void EmitMveHeader(RecordKeeper &Records, raw_ostream &OS) {
2182
0
  MveEmitter(Records).EmitHeader(OS);
2183
0
}
2184
2185
0
void EmitMveBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
2186
0
  MveEmitter(Records).EmitBuiltinDef(OS);
2187
0
}
2188
2189
0
void EmitMveBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
2190
0
  MveEmitter(Records).EmitBuiltinSema(OS);
2191
0
}
2192
2193
0
void EmitMveBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
2194
0
  MveEmitter(Records).EmitBuiltinCG(OS);
2195
0
}
2196
2197
0
void EmitMveBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
2198
0
  MveEmitter(Records).EmitBuiltinAliases(OS);
2199
0
}
2200
2201
// CDE
2202
2203
0
void EmitCdeHeader(RecordKeeper &Records, raw_ostream &OS) {
2204
0
  CdeEmitter(Records).EmitHeader(OS);
2205
0
}
2206
2207
0
void EmitCdeBuiltinDef(RecordKeeper &Records, raw_ostream &OS) {
2208
0
  CdeEmitter(Records).EmitBuiltinDef(OS);
2209
0
}
2210
2211
0
void EmitCdeBuiltinSema(RecordKeeper &Records, raw_ostream &OS) {
2212
0
  CdeEmitter(Records).EmitBuiltinSema(OS);
2213
0
}
2214
2215
0
void EmitCdeBuiltinCG(RecordKeeper &Records, raw_ostream &OS) {
2216
0
  CdeEmitter(Records).EmitBuiltinCG(OS);
2217
0
}
2218
2219
0
void EmitCdeBuiltinAliases(RecordKeeper &Records, raw_ostream &OS) {
2220
0
  CdeEmitter(Records).EmitBuiltinAliases(OS);
2221
0
}
2222
2223
} // end namespace clang