This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
NeonEmitter.cpp
2651 lines (2289 loc) · 78.4 KB
/
NeonEmitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This tablegen backend is responsible for emitting arm_neon.h, which includes
// a declaration and definition of each function specified by the ARM NEON
// compiler interface. See ARM document DUI0348B.
//
// Each NEON instruction is implemented in terms of 1 or more functions which
// are suffixed with the element type of the input vectors. Functions may be
// implemented in terms of generic vector operations such as +, *, -, etc. or
// by calling a __builtin_-prefixed function which will be handled by clang's
// CodeGen library.
//
// Additional validation code can be generated by this file when runHeader() is
// called, rather than the normal run() entry point.
//
// See also the documentation in include/clang/Basic/arm_neon.td.
//
//===----------------------------------------------------------------------===//
#include "TableGenBackends.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/SetTheory.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cstddef>
#include <cstdint>
#include <deque>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace llvm;
namespace {
// While globals are generally bad, this one allows us to perform assertions
// liberally and somehow still trace them back to the def they indirectly
// came from.
static Record *CurrentRecord = nullptr;
static void assert_with_loc(bool Assertion, const std::string &Str) {
if (!Assertion) {
if (CurrentRecord)
PrintFatalError(CurrentRecord->getLoc(), Str);
else
PrintFatalError(Str);
}
}
enum ClassKind {
ClassNone,
ClassI, // generic integer instruction, e.g., "i8" suffix
ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
ClassW, // width-specific instruction, e.g., "8" suffix
ClassB, // bitcast arguments with enum argument to specify type
ClassL, // Logical instructions which are op instructions
// but we need to not emit any suffix for in our
// tests.
ClassNoTest // Instructions which we do not test since they are
// not TRUE instructions.
};
/// NeonTypeFlags - Flags to identify the types for overloaded Neon
/// builtins. These must be kept in sync with the flags in
/// include/clang/Basic/TargetBuiltins.h.
namespace NeonTypeFlags {
enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 };
enum EltType {
Int8,
Int16,
Int32,
Int64,
Poly8,
Poly16,
Poly64,
Poly128,
Float16,
Float32,
Float64
};
} // end namespace NeonTypeFlags
class NeonEmitter;
//===----------------------------------------------------------------------===//
// TypeSpec
//===----------------------------------------------------------------------===//
/// A TypeSpec is just a simple wrapper around a string, but gets its own type
/// for strong typing purposes.
///
/// A TypeSpec can be used to create a type.
class TypeSpec : public std::string {
public:
static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) {
std::vector<TypeSpec> Ret;
TypeSpec Acc;
for (char I : Str.str()) {
if (islower(I)) {
Acc.push_back(I);
Ret.push_back(TypeSpec(Acc));
Acc.clear();
} else {
Acc.push_back(I);
}
}
return Ret;
}
};
//===----------------------------------------------------------------------===//
// Type
//===----------------------------------------------------------------------===//
/// A Type. Not much more to say here.
class Type {
private:
TypeSpec TS;
bool Float, Signed, Immediate, Void, Poly, Constant, Pointer;
// ScalarForMangling and NoManglingQ are really not suited to live here as
// they are not related to the type. But they live in the TypeSpec (not the
// prototype), so this is really the only place to store them.
bool ScalarForMangling, NoManglingQ;
unsigned Bitwidth, ElementBitwidth, NumVectors;
public:
Type()
: Float(false), Signed(false), Immediate(false), Void(true), Poly(false),
Constant(false), Pointer(false), ScalarForMangling(false),
NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {}
Type(TypeSpec TS, char CharMod)
: TS(std::move(TS)), Float(false), Signed(false), Immediate(false),
Void(false), Poly(false), Constant(false), Pointer(false),
ScalarForMangling(false), NoManglingQ(false), Bitwidth(0),
ElementBitwidth(0), NumVectors(0) {
applyModifier(CharMod);
}
/// Returns a type representing "void".
static Type getVoid() { return Type(); }
bool operator==(const Type &Other) const { return str() == Other.str(); }
bool operator!=(const Type &Other) const { return !operator==(Other); }
//
// Query functions
//
bool isScalarForMangling() const { return ScalarForMangling; }
bool noManglingQ() const { return NoManglingQ; }
bool isPointer() const { return Pointer; }
bool isFloating() const { return Float; }
bool isInteger() const { return !Float && !Poly; }
bool isSigned() const { return Signed; }
bool isImmediate() const { return Immediate; }
bool isScalar() const { return NumVectors == 0; }
bool isVector() const { return NumVectors > 0; }
bool isFloat() const { return Float && ElementBitwidth == 32; }
bool isDouble() const { return Float && ElementBitwidth == 64; }
bool isHalf() const { return Float && ElementBitwidth == 16; }
bool isPoly() const { return Poly; }
bool isChar() const { return ElementBitwidth == 8; }
bool isShort() const { return !Float && ElementBitwidth == 16; }
bool isInt() const { return !Float && ElementBitwidth == 32; }
bool isLong() const { return !Float && ElementBitwidth == 64; }
bool isVoid() const { return Void; }
unsigned getNumElements() const { return Bitwidth / ElementBitwidth; }
unsigned getSizeInBits() const { return Bitwidth; }
unsigned getElementSizeInBits() const { return ElementBitwidth; }
unsigned getNumVectors() const { return NumVectors; }
//
// Mutator functions
//
void makeUnsigned() { Signed = false; }
void makeSigned() { Signed = true; }
void makeInteger(unsigned ElemWidth, bool Sign) {
Float = false;
Poly = false;
Signed = Sign;
Immediate = false;
ElementBitwidth = ElemWidth;
}
void makeImmediate(unsigned ElemWidth) {
Float = false;
Poly = false;
Signed = true;
Immediate = true;
ElementBitwidth = ElemWidth;
}
void makeScalar() {
Bitwidth = ElementBitwidth;
NumVectors = 0;
}
void makeOneVector() {
assert(isVector());
NumVectors = 1;
}
void doubleLanes() {
assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!");
Bitwidth = 128;
}
void halveLanes() {
assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!");
Bitwidth = 64;
}
/// Return the C string representation of a type, which is the typename
/// defined in stdint.h or arm_neon.h.
std::string str() const;
/// Return the string representation of a type, which is an encoded
/// string for passing to the BUILTIN() macro in Builtins.def.
std::string builtin_str() const;
/// Return the value in NeonTypeFlags for this type.
unsigned getNeonEnum() const;
/// Parse a type from a stdint.h or arm_neon.h typedef name,
/// for example uint32x2_t or int64_t.
static Type fromTypedefName(StringRef Name);
private:
/// Creates the type based on the typespec string in TS.
/// Sets "Quad" to true if the "Q" or "H" modifiers were
/// seen. This is needed by applyModifier as some modifiers
/// only take effect if the type size was changed by "Q" or "H".
void applyTypespec(bool &Quad);
/// Applies a prototype modifier to the type.
void applyModifier(char Mod);
};
//===----------------------------------------------------------------------===//
// Variable
//===----------------------------------------------------------------------===//
/// A variable is a simple class that just has a type and a name.
class Variable {
Type T;
std::string N;
public:
Variable() : T(Type::getVoid()), N("") {}
Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {}
Type getType() const { return T; }
std::string getName() const { return "__" + N; }
};
//===----------------------------------------------------------------------===//
// Intrinsic
//===----------------------------------------------------------------------===//
/// The main grunt class. This represents an instantiation of an intrinsic with
/// a particular typespec and prototype.
class Intrinsic {
friend class DagEmitter;
/// The Record this intrinsic was created from.
Record *R;
/// The unmangled name and prototype.
std::string Name, Proto;
/// The input and output typespecs. InTS == OutTS except when
/// CartesianProductOfTypes is 1 - this is the case for vreinterpret.
TypeSpec OutTS, InTS;
/// The base class kind. Most intrinsics use ClassS, which has full type
/// info for integers (s32/u32). Some use ClassI, which doesn't care about
/// signedness (i32), while some (ClassB) have no type at all, only a width
/// (32).
ClassKind CK;
/// The list of DAGs for the body. May be empty, in which case we should
/// emit a builtin call.
ListInit *Body;
/// The architectural #ifdef guard.
std::string Guard;
/// Set if the Unavailable bit is 1. This means we don't generate a body,
/// just an "unavailable" attribute on a declaration.
bool IsUnavailable;
/// Is this intrinsic safe for big-endian? or does it need its arguments
/// reversing?
bool BigEndianSafe;
/// The types of return value [0] and parameters [1..].
std::vector<Type> Types;
/// The local variables defined.
std::map<std::string, Variable> Variables;
/// NeededEarly - set if any other intrinsic depends on this intrinsic.
bool NeededEarly;
/// UseMacro - set if we should implement using a macro or unset for a
/// function.
bool UseMacro;
/// The set of intrinsics that this intrinsic uses/requires.
std::set<Intrinsic *> Dependencies;
/// The "base type", which is Type('d', OutTS). InBaseType is only
/// different if CartesianProductOfTypes = 1 (for vreinterpret).
Type BaseType, InBaseType;
/// The return variable.
Variable RetVar;
/// A postfix to apply to every variable. Defaults to "".
std::string VariablePostfix;
NeonEmitter &Emitter;
std::stringstream OS;
bool isBigEndianSafe() const {
if (BigEndianSafe)
return true;
for (const auto &T : Types){
if (T.isVector() && T.getNumElements() > 1)
return false;
}
return true;
}
public:
Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS,
TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter,
StringRef Guard, bool IsUnavailable, bool BigEndianSafe)
: R(R), Name(Name.str()), Proto(Proto.str()), OutTS(OutTS), InTS(InTS),
CK(CK), Body(Body), Guard(Guard.str()), IsUnavailable(IsUnavailable),
BigEndianSafe(BigEndianSafe), NeededEarly(false), UseMacro(false),
BaseType(OutTS, 'd'), InBaseType(InTS, 'd'), Emitter(Emitter) {
// If this builtin takes an immediate argument, we need to #define it rather
// than use a standard declaration, so that SemaChecking can range check
// the immediate passed by the user.
if (Proto.find('i') != std::string::npos)
UseMacro = true;
// Pointer arguments need to use macros to avoid hiding aligned attributes
// from the pointer type.
if (Proto.find('p') != std::string::npos ||
Proto.find('c') != std::string::npos)
UseMacro = true;
// It is not permitted to pass or return an __fp16 by value, so intrinsics
// taking a scalar float16_t must be implemented as macros.
if (OutTS.find('h') != std::string::npos &&
Proto.find('s') != std::string::npos)
UseMacro = true;
// Modify the TypeSpec per-argument to get a concrete Type, and create
// known variables for each.
// Types[0] is the return value.
Types.emplace_back(OutTS, Proto[0]);
for (unsigned I = 1; I < Proto.size(); ++I)
Types.emplace_back(InTS, Proto[I]);
}
/// Get the Record that this intrinsic is based off.
Record *getRecord() const { return R; }
/// Get the set of Intrinsics that this intrinsic calls.
/// this is the set of immediate dependencies, NOT the
/// transitive closure.
const std::set<Intrinsic *> &getDependencies() const { return Dependencies; }
/// Get the architectural guard string (#ifdef).
std::string getGuard() const { return Guard; }
/// Get the non-mangled name.
std::string getName() const { return Name; }
/// Return true if the intrinsic takes an immediate operand.
bool hasImmediate() const {
return Proto.find('i') != std::string::npos;
}
/// Return the parameter index of the immediate operand.
unsigned getImmediateIdx() const {
assert(hasImmediate());
unsigned Idx = Proto.find('i');
assert(Idx > 0 && "Can't return an immediate!");
return Idx - 1;
}
/// Return true if the intrinsic takes an splat operand.
bool hasSplat() const { return Proto.find('a') != std::string::npos; }
/// Return the parameter index of the splat operand.
unsigned getSplatIdx() const {
assert(hasSplat());
unsigned Idx = Proto.find('a');
assert(Idx > 0 && "Can't return a splat!");
return Idx - 1;
}
unsigned getNumParams() const { return Proto.size() - 1; }
Type getReturnType() const { return Types[0]; }
Type getParamType(unsigned I) const { return Types[I + 1]; }
Type getBaseType() const { return BaseType; }
/// Return the raw prototype string.
std::string getProto() const { return Proto; }
/// Return true if the prototype has a scalar argument.
/// This does not return true for the "splat" code ('a').
bool protoHasScalar() const;
/// Return the index that parameter PIndex will sit at
/// in a generated function call. This is often just PIndex,
/// but may not be as things such as multiple-vector operands
/// and sret parameters need to be taken into accont.
unsigned getGeneratedParamIdx(unsigned PIndex) {
unsigned Idx = 0;
if (getReturnType().getNumVectors() > 1)
// Multiple vectors are passed as sret.
++Idx;
for (unsigned I = 0; I < PIndex; ++I)
Idx += std::max(1U, getParamType(I).getNumVectors());
return Idx;
}
bool hasBody() const { return Body && !Body->getValues().empty(); }
void setNeededEarly() { NeededEarly = true; }
bool operator<(const Intrinsic &Other) const {
// Sort lexicographically on a two-tuple (Guard, Name)
if (Guard != Other.Guard)
return Guard < Other.Guard;
return Name < Other.Name;
}
ClassKind getClassKind(bool UseClassBIfScalar = false) {
if (UseClassBIfScalar && !protoHasScalar())
return ClassB;
return CK;
}
/// Return the name, mangled with type information.
/// If ForceClassS is true, use ClassS (u32/s32) instead
/// of the intrinsic's own type class.
std::string getMangledName(bool ForceClassS = false) const;
/// Return the type code for a builtin function call.
std::string getInstTypeCode(Type T, ClassKind CK) const;
/// Return the type string for a BUILTIN() macro in Builtins.def.
std::string getBuiltinTypeStr();
/// Generate the intrinsic, returning code.
std::string generate();
/// Perform type checking and populate the dependency graph, but
/// don't generate code yet.
void indexBody();
private:
std::string mangleName(std::string Name, ClassKind CK) const;
void initVariables();
std::string replaceParamsIn(std::string S);
void emitBodyAsBuiltinCall();
void generateImpl(bool ReverseArguments,
StringRef NamePrefix, StringRef CallPrefix);
void emitReturn();
void emitBody(StringRef CallPrefix);
void emitShadowedArgs();
void emitArgumentReversal();
void emitReturnReversal();
void emitReverseVariable(Variable &Dest, Variable &Src);
void emitNewLine();
void emitClosingBrace();
void emitOpeningBrace();
void emitPrototype(StringRef NamePrefix);
class DagEmitter {
Intrinsic &Intr;
StringRef CallPrefix;
public:
DagEmitter(Intrinsic &Intr, StringRef CallPrefix) :
Intr(Intr), CallPrefix(CallPrefix) {
}
std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName);
std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI);
std::pair<Type, std::string> emitDagSplat(DagInit *DI);
std::pair<Type, std::string> emitDagDup(DagInit *DI);
std::pair<Type, std::string> emitDagDupTyped(DagInit *DI);
std::pair<Type, std::string> emitDagShuffle(DagInit *DI);
std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast);
std::pair<Type, std::string> emitDagCall(DagInit *DI);
std::pair<Type, std::string> emitDagNameReplace(DagInit *DI);
std::pair<Type, std::string> emitDagLiteral(DagInit *DI);
std::pair<Type, std::string> emitDagOp(DagInit *DI);
std::pair<Type, std::string> emitDag(DagInit *DI);
};
};
//===----------------------------------------------------------------------===//
// NeonEmitter
//===----------------------------------------------------------------------===//
class NeonEmitter {
RecordKeeper &Records;
DenseMap<Record *, ClassKind> ClassMap;
std::map<std::string, std::deque<Intrinsic>> IntrinsicMap;
unsigned UniqueNumber;
void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out);
void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs);
void genOverloadTypeCheckCode(raw_ostream &OS,
SmallVectorImpl<Intrinsic *> &Defs);
void genIntrinsicRangeCheckCode(raw_ostream &OS,
SmallVectorImpl<Intrinsic *> &Defs);
public:
/// Called by Intrinsic - this attempts to get an intrinsic that takes
/// the given types as arguments.
Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types);
/// Called by Intrinsic - returns a globally-unique number.
unsigned getUniqueNumber() { return UniqueNumber++; }
NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) {
Record *SI = R.getClass("SInst");
Record *II = R.getClass("IInst");
Record *WI = R.getClass("WInst");
Record *SOpI = R.getClass("SOpInst");
Record *IOpI = R.getClass("IOpInst");
Record *WOpI = R.getClass("WOpInst");
Record *LOpI = R.getClass("LOpInst");
Record *NoTestOpI = R.getClass("NoTestOpInst");
ClassMap[SI] = ClassS;
ClassMap[II] = ClassI;
ClassMap[WI] = ClassW;
ClassMap[SOpI] = ClassS;
ClassMap[IOpI] = ClassI;
ClassMap[WOpI] = ClassW;
ClassMap[LOpI] = ClassL;
ClassMap[NoTestOpI] = ClassNoTest;
}
// run - Emit arm_neon.h.inc
void run(raw_ostream &o);
// runFP16 - Emit arm_fp16.h.inc
void runFP16(raw_ostream &o);
// runHeader - Emit all the __builtin prototypes used in arm_neon.h
// and arm_fp16.h
void runHeader(raw_ostream &o);
// runTests - Emit tests for all the Neon intrinsics.
void runTests(raw_ostream &o);
};
} // end anonymous namespace
//===----------------------------------------------------------------------===//
// Type implementation
//===----------------------------------------------------------------------===//
std::string Type::str() const {
if (Void)
return "void";
std::string S;
if (!Signed && isInteger())
S += "u";
if (Poly)
S += "poly";
else if (Float)
S += "float";
else
S += "int";
S += utostr(ElementBitwidth);
if (isVector())
S += "x" + utostr(getNumElements());
if (NumVectors > 1)
S += "x" + utostr(NumVectors);
S += "_t";
if (Constant)
S += " const";
if (Pointer)
S += " *";
return S;
}
std::string Type::builtin_str() const {
std::string S;
if (isVoid())
return "v";
if (Pointer)
// All pointers are void pointers.
S += "v";
else if (isInteger())
switch (ElementBitwidth) {
case 8: S += "c"; break;
case 16: S += "s"; break;
case 32: S += "i"; break;
case 64: S += "Wi"; break;
case 128: S += "LLLi"; break;
default: llvm_unreachable("Unhandled case!");
}
else
switch (ElementBitwidth) {
case 16: S += "h"; break;
case 32: S += "f"; break;
case 64: S += "d"; break;
default: llvm_unreachable("Unhandled case!");
}
if (isChar() && !Pointer && Signed)
// Make chars explicitly signed.
S = "S" + S;
else if (isInteger() && !Pointer && !Signed)
S = "U" + S;
// Constant indices are "int", but have the "constant expression" modifier.
if (isImmediate()) {
assert(isInteger() && isSigned());
S = "I" + S;
}
if (isScalar()) {
if (Constant) S += "C";
if (Pointer) S += "*";
return S;
}
std::string Ret;
for (unsigned I = 0; I < NumVectors; ++I)
Ret += "V" + utostr(getNumElements()) + S;
return Ret;
}
unsigned Type::getNeonEnum() const {
unsigned Addend;
switch (ElementBitwidth) {
case 8: Addend = 0; break;
case 16: Addend = 1; break;
case 32: Addend = 2; break;
case 64: Addend = 3; break;
case 128: Addend = 4; break;
default: llvm_unreachable("Unhandled element bitwidth!");
}
unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend;
if (Poly) {
// Adjustment needed because Poly32 doesn't exist.
if (Addend >= 2)
--Addend;
Base = (unsigned)NeonTypeFlags::Poly8 + Addend;
}
if (Float) {
assert(Addend != 0 && "Float8 doesn't exist!");
Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1);
}
if (Bitwidth == 128)
Base |= (unsigned)NeonTypeFlags::QuadFlag;
if (isInteger() && !Signed)
Base |= (unsigned)NeonTypeFlags::UnsignedFlag;
return Base;
}
Type Type::fromTypedefName(StringRef Name) {
Type T;
T.Void = false;
T.Float = false;
T.Poly = false;
if (Name.front() == 'u') {
T.Signed = false;
Name = Name.drop_front();
} else {
T.Signed = true;
}
if (Name.startswith("float")) {
T.Float = true;
Name = Name.drop_front(5);
} else if (Name.startswith("poly")) {
T.Poly = true;
Name = Name.drop_front(4);
} else {
assert(Name.startswith("int"));
Name = Name.drop_front(3);
}
unsigned I = 0;
for (I = 0; I < Name.size(); ++I) {
if (!isdigit(Name[I]))
break;
}
Name.substr(0, I).getAsInteger(10, T.ElementBitwidth);
Name = Name.drop_front(I);
T.Bitwidth = T.ElementBitwidth;
T.NumVectors = 1;
if (Name.front() == 'x') {
Name = Name.drop_front();
unsigned I = 0;
for (I = 0; I < Name.size(); ++I) {
if (!isdigit(Name[I]))
break;
}
unsigned NumLanes;
Name.substr(0, I).getAsInteger(10, NumLanes);
Name = Name.drop_front(I);
T.Bitwidth = T.ElementBitwidth * NumLanes;
} else {
// Was scalar.
T.NumVectors = 0;
}
if (Name.front() == 'x') {
Name = Name.drop_front();
unsigned I = 0;
for (I = 0; I < Name.size(); ++I) {
if (!isdigit(Name[I]))
break;
}
Name.substr(0, I).getAsInteger(10, T.NumVectors);
Name = Name.drop_front(I);
}
assert(Name.startswith("_t") && "Malformed typedef!");
return T;
}
void Type::applyTypespec(bool &Quad) {
std::string S = TS;
ScalarForMangling = false;
Void = false;
Poly = Float = false;
ElementBitwidth = ~0U;
Signed = true;
NumVectors = 1;
for (char I : S) {
switch (I) {
case 'S':
ScalarForMangling = true;
break;
case 'H':
NoManglingQ = true;
Quad = true;
break;
case 'Q':
Quad = true;
break;
case 'P':
Poly = true;
break;
case 'U':
Signed = false;
break;
case 'c':
ElementBitwidth = 8;
break;
case 'h':
Float = true;
LLVM_FALLTHROUGH;
case 's':
ElementBitwidth = 16;
break;
case 'f':
Float = true;
LLVM_FALLTHROUGH;
case 'i':
ElementBitwidth = 32;
break;
case 'd':
Float = true;
LLVM_FALLTHROUGH;
case 'l':
ElementBitwidth = 64;
break;
case 'k':
ElementBitwidth = 128;
// Poly doesn't have a 128x1 type.
if (Poly)
NumVectors = 0;
break;
default:
llvm_unreachable("Unhandled type code!");
}
}
assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
Bitwidth = Quad ? 128 : 64;
}
void Type::applyModifier(char Mod) {
bool AppliedQuad = false;
applyTypespec(AppliedQuad);
switch (Mod) {
case 'v':
Void = true;
break;
case 't':
if (Poly) {
Poly = false;
Signed = false;
}
break;
case 'b':
Signed = false;
Float = false;
Poly = false;
NumVectors = 0;
Bitwidth = ElementBitwidth;
break;
case '$':
Signed = true;
Float = false;
Poly = false;
NumVectors = 0;
Bitwidth = ElementBitwidth;
break;
case 'u':
Signed = false;
Poly = false;
Float = false;
break;
case 'x':
Signed = true;
assert(!Poly && "'u' can't be used with poly types!");
Float = false;
break;
case 'o':
Bitwidth = ElementBitwidth = 64;
NumVectors = 0;
Float = true;
break;
case 'y':
Bitwidth = ElementBitwidth = 32;
NumVectors = 0;
Float = true;
break;
case 'Y':
Bitwidth = ElementBitwidth = 16;
NumVectors = 0;
Float = true;
break;
case 'I':
Bitwidth = ElementBitwidth = 32;
NumVectors = 0;
Float = false;
Signed = true;
break;
case 'L':
Bitwidth = ElementBitwidth = 64;
NumVectors = 0;
Float = false;
Signed = true;
break;
case 'U':
Bitwidth = ElementBitwidth = 32;
NumVectors = 0;
Float = false;
Signed = false;
break;
case 'O':
Bitwidth = ElementBitwidth = 64;
NumVectors = 0;
Float = false;
Signed = false;
break;
case 'f':
Float = true;
ElementBitwidth = 32;
break;
case 'F':
Float = true;
ElementBitwidth = 64;
break;
case 'H':
Float = true;
ElementBitwidth = 16;
break;
case '0':
Float = true;
if (AppliedQuad)
Bitwidth /= 2;
ElementBitwidth = 16;
break;
case '1':
Float = true;
if (!AppliedQuad)
Bitwidth *= 2;
ElementBitwidth = 16;
break;
case 'g':
if (AppliedQuad)
Bitwidth /= 2;
break;
case 'j':
if (!AppliedQuad)
Bitwidth *= 2;
break;
case 'w':
ElementBitwidth *= 2;
Bitwidth *= 2;
break;
case 'n':
ElementBitwidth *= 2;
break;
case 'i':
Float = false;
Poly = false;
ElementBitwidth = Bitwidth = 32;
NumVectors = 0;
Signed = true;
Immediate = true;
break;
case 'l':
Float = false;
Poly = false;
ElementBitwidth = Bitwidth = 64;
NumVectors = 0;
Signed = false;
Immediate = true;
break;
case 'z':
ElementBitwidth /= 2;
Bitwidth = ElementBitwidth;
NumVectors = 0;
break;
case 'r':
ElementBitwidth *= 2;
Bitwidth = ElementBitwidth;
NumVectors = 0;
break;
case 's':
case 'a':
Bitwidth = ElementBitwidth;
NumVectors = 0;
break;
case 'k':
Bitwidth *= 2;
break;
case 'c':
Constant = true;
LLVM_FALLTHROUGH;
case 'p':
Pointer = true;
Bitwidth = ElementBitwidth;
NumVectors = 0;
break;
case 'h':
ElementBitwidth /= 2;
break;
case 'q':
ElementBitwidth /= 2;
Bitwidth *= 2;
break;
case 'e':
ElementBitwidth /= 2;
Signed = false;
break;
case 'm':
ElementBitwidth /= 2;
Bitwidth /= 2;
break;
case 'd':
break;
case '2':
NumVectors = 2;
break;