blob: 62fcccbacb558f71cce8efc3262110ec7d133402 [file] [log] [blame]
Peter Collingbournebee583f2011-10-06 13:03:08 +00001//===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend is responsible for emitting arm_neon.h, which includes
11// a declaration and definition of each function specified by the ARM NEON
12// compiler interface. See ARM document DUI0348B.
13//
14// Each NEON instruction is implemented in terms of 1 or more functions which
15// are suffixed with the element type of the input vectors. Functions may be
16// implemented in terms of generic vector operations such as +, *, -, etc. or
17// by calling a __builtin_-prefixed function which will be handled by clang's
18// CodeGen library.
19//
20// Additional validation code can be generated by this file when runHeader() is
James Molloydee4ab02014-06-17 13:11:27 +000021// called, rather than the normal run() entry point.
22//
23// See also the documentation in include/clang/Basic/arm_neon.td.
Peter Collingbournebee583f2011-10-06 13:03:08 +000024//
25//===----------------------------------------------------------------------===//
26
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000027#include "llvm/ADT/ArrayRef.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000028#include "llvm/ADT/DenseMap.h"
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000029#include "llvm/ADT/None.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000030#include "llvm/ADT/SmallVector.h"
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000031#include "llvm/ADT/STLExtras.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000032#include "llvm/ADT/StringExtras.h"
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000033#include "llvm/ADT/StringRef.h"
34#include "llvm/Support/Casting.h"
David Blaikie8a40f702012-01-17 06:56:22 +000035#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000036#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000037#include "llvm/TableGen/Error.h"
38#include "llvm/TableGen/Record.h"
James Molloydee4ab02014-06-17 13:11:27 +000039#include "llvm/TableGen/SetTheory.h"
James Molloydee4ab02014-06-17 13:11:27 +000040#include <algorithm>
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000041#include <cassert>
42#include <cctype>
43#include <cstddef>
44#include <cstdint>
David Blaikie4c96a5e2015-08-06 18:29:32 +000045#include <deque>
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000046#include <map>
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000047#include <set>
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000048#include <sstream>
49#include <string>
Benjamin Kramercfeacf52016-05-27 14:27:13 +000050#include <utility>
Chandler Carruth575bc3ba2015-01-14 11:23:58 +000051#include <vector>
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000052
Peter Collingbournebee583f2011-10-06 13:03:08 +000053using namespace llvm;
54
James Molloydee4ab02014-06-17 13:11:27 +000055namespace {
56
57// While globals are generally bad, this one allows us to perform assertions
58// liberally and somehow still trace them back to the def they indirectly
59// came from.
60static Record *CurrentRecord = nullptr;
61static void assert_with_loc(bool Assertion, const std::string &Str) {
62 if (!Assertion) {
63 if (CurrentRecord)
64 PrintFatalError(CurrentRecord->getLoc(), Str);
65 else
66 PrintFatalError(Str);
67 }
68}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000069
70enum ClassKind {
71 ClassNone,
James Molloydee4ab02014-06-17 13:11:27 +000072 ClassI, // generic integer instruction, e.g., "i8" suffix
73 ClassS, // signed/unsigned/poly, e.g., "s8", "u8" or "p8" suffix
74 ClassW, // width-specific instruction, e.g., "8" suffix
75 ClassB, // bitcast arguments with enum argument to specify type
76 ClassL, // Logical instructions which are op instructions
77 // but we need to not emit any suffix for in our
78 // tests.
79 ClassNoTest // Instructions which we do not test since they are
80 // not TRUE instructions.
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000081};
82
83/// NeonTypeFlags - Flags to identify the types for overloaded Neon
84/// builtins. These must be kept in sync with the flags in
85/// include/clang/Basic/TargetBuiltins.h.
James Molloydee4ab02014-06-17 13:11:27 +000086namespace NeonTypeFlags {
Eugene Zelenko58ab22f2016-11-29 22:44:24 +000087
James Molloydee4ab02014-06-17 13:11:27 +000088enum { EltTypeMask = 0xf, UnsignedFlag = 0x10, QuadFlag = 0x20 };
89
90enum EltType {
91 Int8,
92 Int16,
93 Int32,
94 Int64,
95 Poly8,
96 Poly16,
97 Poly64,
98 Poly128,
99 Float16,
100 Float32,
101 Float64
102};
James Molloydee4ab02014-06-17 13:11:27 +0000103
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000104} // end namespace NeonTypeFlags
105
James Molloydee4ab02014-06-17 13:11:27 +0000106class NeonEmitter;
James Molloydee4ab02014-06-17 13:11:27 +0000107
108//===----------------------------------------------------------------------===//
109// TypeSpec
110//===----------------------------------------------------------------------===//
111
112/// A TypeSpec is just a simple wrapper around a string, but gets its own type
113/// for strong typing purposes.
114///
115/// A TypeSpec can be used to create a type.
116class TypeSpec : public std::string {
117public:
118 static std::vector<TypeSpec> fromTypeSpecs(StringRef Str) {
119 std::vector<TypeSpec> Ret;
120 TypeSpec Acc;
121 for (char I : Str.str()) {
122 if (islower(I)) {
123 Acc.push_back(I);
124 Ret.push_back(TypeSpec(Acc));
125 Acc.clear();
126 } else {
127 Acc.push_back(I);
128 }
129 }
130 return Ret;
131 }
132};
133
134//===----------------------------------------------------------------------===//
135// Type
136//===----------------------------------------------------------------------===//
137
138/// A Type. Not much more to say here.
139class Type {
140private:
141 TypeSpec TS;
142
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000143 bool Float, Signed, Immediate, Void, Poly, Constant, Pointer;
James Molloydee4ab02014-06-17 13:11:27 +0000144 // ScalarForMangling and NoManglingQ are really not suited to live here as
145 // they are not related to the type. But they live in the TypeSpec (not the
146 // prototype), so this is really the only place to store them.
147 bool ScalarForMangling, NoManglingQ;
148 unsigned Bitwidth, ElementBitwidth, NumVectors;
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000149
150public:
James Molloydee4ab02014-06-17 13:11:27 +0000151 Type()
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000152 : Float(false), Signed(false), Immediate(false), Void(true), Poly(false),
153 Constant(false), Pointer(false), ScalarForMangling(false),
154 NoManglingQ(false), Bitwidth(0), ElementBitwidth(0), NumVectors(0) {}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000155
James Molloydee4ab02014-06-17 13:11:27 +0000156 Type(TypeSpec TS, char CharMod)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000157 : TS(std::move(TS)), Float(false), Signed(false), Immediate(false),
158 Void(false), Poly(false), Constant(false), Pointer(false),
159 ScalarForMangling(false), NoManglingQ(false), Bitwidth(0),
160 ElementBitwidth(0), NumVectors(0) {
James Molloydee4ab02014-06-17 13:11:27 +0000161 applyModifier(CharMod);
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000162 }
163
James Molloydee4ab02014-06-17 13:11:27 +0000164 /// Returns a type representing "void".
165 static Type getVoid() { return Type(); }
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000166
James Molloydee4ab02014-06-17 13:11:27 +0000167 bool operator==(const Type &Other) const { return str() == Other.str(); }
168 bool operator!=(const Type &Other) const { return !operator==(Other); }
169
170 //
171 // Query functions
172 //
173 bool isScalarForMangling() const { return ScalarForMangling; }
174 bool noManglingQ() const { return NoManglingQ; }
175
176 bool isPointer() const { return Pointer; }
177 bool isFloating() const { return Float; }
178 bool isInteger() const { return !Float && !Poly; }
179 bool isSigned() const { return Signed; }
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000180 bool isImmediate() const { return Immediate; }
James Molloydee4ab02014-06-17 13:11:27 +0000181 bool isScalar() const { return NumVectors == 0; }
182 bool isVector() const { return NumVectors > 0; }
183 bool isFloat() const { return Float && ElementBitwidth == 32; }
184 bool isDouble() const { return Float && ElementBitwidth == 64; }
185 bool isHalf() const { return Float && ElementBitwidth == 16; }
186 bool isPoly() const { return Poly; }
187 bool isChar() const { return ElementBitwidth == 8; }
188 bool isShort() const { return !Float && ElementBitwidth == 16; }
189 bool isInt() const { return !Float && ElementBitwidth == 32; }
190 bool isLong() const { return !Float && ElementBitwidth == 64; }
191 bool isVoid() const { return Void; }
192 unsigned getNumElements() const { return Bitwidth / ElementBitwidth; }
193 unsigned getSizeInBits() const { return Bitwidth; }
194 unsigned getElementSizeInBits() const { return ElementBitwidth; }
195 unsigned getNumVectors() const { return NumVectors; }
196
197 //
198 // Mutator functions
199 //
200 void makeUnsigned() { Signed = false; }
201 void makeSigned() { Signed = true; }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000202
James Molloydee4ab02014-06-17 13:11:27 +0000203 void makeInteger(unsigned ElemWidth, bool Sign) {
204 Float = false;
205 Poly = false;
206 Signed = Sign;
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000207 Immediate = false;
208 ElementBitwidth = ElemWidth;
209 }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000210
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000211 void makeImmediate(unsigned ElemWidth) {
212 Float = false;
213 Poly = false;
214 Signed = true;
215 Immediate = true;
James Molloydee4ab02014-06-17 13:11:27 +0000216 ElementBitwidth = ElemWidth;
217 }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000218
James Molloydee4ab02014-06-17 13:11:27 +0000219 void makeScalar() {
220 Bitwidth = ElementBitwidth;
221 NumVectors = 0;
222 }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000223
James Molloydee4ab02014-06-17 13:11:27 +0000224 void makeOneVector() {
225 assert(isVector());
226 NumVectors = 1;
227 }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000228
James Molloydee4ab02014-06-17 13:11:27 +0000229 void doubleLanes() {
230 assert_with_loc(Bitwidth != 128, "Can't get bigger than 128!");
231 Bitwidth = 128;
232 }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000233
James Molloydee4ab02014-06-17 13:11:27 +0000234 void halveLanes() {
235 assert_with_loc(Bitwidth != 64, "Can't get smaller than 64!");
236 Bitwidth = 64;
237 }
238
239 /// Return the C string representation of a type, which is the typename
240 /// defined in stdint.h or arm_neon.h.
241 std::string str() const;
242
243 /// Return the string representation of a type, which is an encoded
244 /// string for passing to the BUILTIN() macro in Builtins.def.
245 std::string builtin_str() const;
246
247 /// Return the value in NeonTypeFlags for this type.
248 unsigned getNeonEnum() const;
249
250 /// Parse a type from a stdint.h or arm_neon.h typedef name,
251 /// for example uint32x2_t or int64_t.
252 static Type fromTypedefName(StringRef Name);
253
254private:
255 /// Creates the type based on the typespec string in TS.
256 /// Sets "Quad" to true if the "Q" or "H" modifiers were
257 /// seen. This is needed by applyModifier as some modifiers
258 /// only take effect if the type size was changed by "Q" or "H".
259 void applyTypespec(bool &Quad);
260 /// Applies a prototype modifier to the type.
261 void applyModifier(char Mod);
262};
263
264//===----------------------------------------------------------------------===//
265// Variable
266//===----------------------------------------------------------------------===//
267
268/// A variable is a simple class that just has a type and a name.
269class Variable {
270 Type T;
271 std::string N;
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000272
273public:
James Molloydee4ab02014-06-17 13:11:27 +0000274 Variable() : T(Type::getVoid()), N("") {}
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000275 Variable(Type T, std::string N) : T(std::move(T)), N(std::move(N)) {}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000276
James Molloydee4ab02014-06-17 13:11:27 +0000277 Type getType() const { return T; }
278 std::string getName() const { return "__" + N; }
279};
280
281//===----------------------------------------------------------------------===//
282// Intrinsic
283//===----------------------------------------------------------------------===//
284
285/// The main grunt class. This represents an instantiation of an intrinsic with
286/// a particular typespec and prototype.
287class Intrinsic {
James Molloyb452f782014-06-27 11:53:35 +0000288 friend class DagEmitter;
289
James Molloydee4ab02014-06-17 13:11:27 +0000290 /// The Record this intrinsic was created from.
291 Record *R;
292 /// The unmangled name and prototype.
293 std::string Name, Proto;
294 /// The input and output typespecs. InTS == OutTS except when
295 /// CartesianProductOfTypes is 1 - this is the case for vreinterpret.
296 TypeSpec OutTS, InTS;
297 /// The base class kind. Most intrinsics use ClassS, which has full type
298 /// info for integers (s32/u32). Some use ClassI, which doesn't care about
299 /// signedness (i32), while some (ClassB) have no type at all, only a width
300 /// (32).
301 ClassKind CK;
302 /// The list of DAGs for the body. May be empty, in which case we should
303 /// emit a builtin call.
304 ListInit *Body;
305 /// The architectural #ifdef guard.
306 std::string Guard;
307 /// Set if the Unvailable bit is 1. This means we don't generate a body,
308 /// just an "unavailable" attribute on a declaration.
309 bool IsUnavailable;
James Molloyb452f782014-06-27 11:53:35 +0000310 /// Is this intrinsic safe for big-endian? or does it need its arguments
311 /// reversing?
312 bool BigEndianSafe;
James Molloydee4ab02014-06-17 13:11:27 +0000313
314 /// The types of return value [0] and parameters [1..].
315 std::vector<Type> Types;
316 /// The local variables defined.
317 std::map<std::string, Variable> Variables;
318 /// NeededEarly - set if any other intrinsic depends on this intrinsic.
319 bool NeededEarly;
320 /// UseMacro - set if we should implement using a macro or unset for a
321 /// function.
322 bool UseMacro;
323 /// The set of intrinsics that this intrinsic uses/requires.
324 std::set<Intrinsic *> Dependencies;
325 /// The "base type", which is Type('d', OutTS). InBaseType is only
326 /// different if CartesianProductOfTypes = 1 (for vreinterpret).
327 Type BaseType, InBaseType;
328 /// The return variable.
329 Variable RetVar;
330 /// A postfix to apply to every variable. Defaults to "".
331 std::string VariablePostfix;
332
333 NeonEmitter &Emitter;
334 std::stringstream OS;
335
336public:
337 Intrinsic(Record *R, StringRef Name, StringRef Proto, TypeSpec OutTS,
338 TypeSpec InTS, ClassKind CK, ListInit *Body, NeonEmitter &Emitter,
James Molloyb452f782014-06-27 11:53:35 +0000339 StringRef Guard, bool IsUnavailable, bool BigEndianSafe)
James Molloydee4ab02014-06-17 13:11:27 +0000340 : R(R), Name(Name.str()), Proto(Proto.str()), OutTS(OutTS), InTS(InTS),
341 CK(CK), Body(Body), Guard(Guard.str()), IsUnavailable(IsUnavailable),
James Molloyb452f782014-06-27 11:53:35 +0000342 BigEndianSafe(BigEndianSafe), NeededEarly(false), UseMacro(false),
343 BaseType(OutTS, 'd'), InBaseType(InTS, 'd'), Emitter(Emitter) {
James Molloydee4ab02014-06-17 13:11:27 +0000344 // If this builtin takes an immediate argument, we need to #define it rather
345 // than use a standard declaration, so that SemaChecking can range check
346 // the immediate passed by the user.
347 if (Proto.find('i') != std::string::npos)
348 UseMacro = true;
349
350 // Pointer arguments need to use macros to avoid hiding aligned attributes
351 // from the pointer type.
352 if (Proto.find('p') != std::string::npos ||
353 Proto.find('c') != std::string::npos)
354 UseMacro = true;
355
356 // It is not permitted to pass or return an __fp16 by value, so intrinsics
357 // taking a scalar float16_t must be implemented as macros.
358 if (OutTS.find('h') != std::string::npos &&
359 Proto.find('s') != std::string::npos)
360 UseMacro = true;
361
362 // Modify the TypeSpec per-argument to get a concrete Type, and create
363 // known variables for each.
364 // Types[0] is the return value.
Benjamin Kramer3204b152015-05-29 19:42:19 +0000365 Types.emplace_back(OutTS, Proto[0]);
James Molloydee4ab02014-06-17 13:11:27 +0000366 for (unsigned I = 1; I < Proto.size(); ++I)
Benjamin Kramer3204b152015-05-29 19:42:19 +0000367 Types.emplace_back(InTS, Proto[I]);
James Molloydee4ab02014-06-17 13:11:27 +0000368 }
369
370 /// Get the Record that this intrinsic is based off.
371 Record *getRecord() const { return R; }
372 /// Get the set of Intrinsics that this intrinsic calls.
373 /// this is the set of immediate dependencies, NOT the
374 /// transitive closure.
375 const std::set<Intrinsic *> &getDependencies() const { return Dependencies; }
376 /// Get the architectural guard string (#ifdef).
377 std::string getGuard() const { return Guard; }
378 /// Get the non-mangled name.
379 std::string getName() const { return Name; }
380
381 /// Return true if the intrinsic takes an immediate operand.
382 bool hasImmediate() const {
383 return Proto.find('i') != std::string::npos;
384 }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000385
James Molloydee4ab02014-06-17 13:11:27 +0000386 /// Return the parameter index of the immediate operand.
387 unsigned getImmediateIdx() const {
388 assert(hasImmediate());
389 unsigned Idx = Proto.find('i');
390 assert(Idx > 0 && "Can't return an immediate!");
391 return Idx - 1;
392 }
393
394 /// Return true if the intrinsic takes an splat operand.
395 bool hasSplat() const { return Proto.find('a') != std::string::npos; }
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000396
James Molloydee4ab02014-06-17 13:11:27 +0000397 /// Return the parameter index of the splat operand.
398 unsigned getSplatIdx() const {
399 assert(hasSplat());
400 unsigned Idx = Proto.find('a');
401 assert(Idx > 0 && "Can't return a splat!");
402 return Idx - 1;
403 }
404
405 unsigned getNumParams() const { return Proto.size() - 1; }
406 Type getReturnType() const { return Types[0]; }
407 Type getParamType(unsigned I) const { return Types[I + 1]; }
408 Type getBaseType() const { return BaseType; }
409 /// Return the raw prototype string.
410 std::string getProto() const { return Proto; }
411
412 /// Return true if the prototype has a scalar argument.
413 /// This does not return true for the "splat" code ('a').
David Blaikie4c96a5e2015-08-06 18:29:32 +0000414 bool protoHasScalar() const;
James Molloydee4ab02014-06-17 13:11:27 +0000415
416 /// Return the index that parameter PIndex will sit at
417 /// in a generated function call. This is often just PIndex,
418 /// but may not be as things such as multiple-vector operands
419 /// and sret parameters need to be taken into accont.
420 unsigned getGeneratedParamIdx(unsigned PIndex) {
421 unsigned Idx = 0;
422 if (getReturnType().getNumVectors() > 1)
423 // Multiple vectors are passed as sret.
424 ++Idx;
425
426 for (unsigned I = 0; I < PIndex; ++I)
427 Idx += std::max(1U, getParamType(I).getNumVectors());
428
429 return Idx;
430 }
431
Eugene Zelenko58ab22f2016-11-29 22:44:24 +0000432 bool hasBody() const { return Body && !Body->getValues().empty(); }
James Molloydee4ab02014-06-17 13:11:27 +0000433
434 void setNeededEarly() { NeededEarly = true; }
435
436 bool operator<(const Intrinsic &Other) const {
437 // Sort lexicographically on a two-tuple (Guard, Name)
438 if (Guard != Other.Guard)
439 return Guard < Other.Guard;
440 return Name < Other.Name;
441 }
442
443 ClassKind getClassKind(bool UseClassBIfScalar = false) {
444 if (UseClassBIfScalar && !protoHasScalar())
445 return ClassB;
446 return CK;
447 }
448
449 /// Return the name, mangled with type information.
450 /// If ForceClassS is true, use ClassS (u32/s32) instead
451 /// of the intrinsic's own type class.
David Blaikie4c96a5e2015-08-06 18:29:32 +0000452 std::string getMangledName(bool ForceClassS = false) const;
James Molloydee4ab02014-06-17 13:11:27 +0000453 /// Return the type code for a builtin function call.
David Blaikie4c96a5e2015-08-06 18:29:32 +0000454 std::string getInstTypeCode(Type T, ClassKind CK) const;
James Molloydee4ab02014-06-17 13:11:27 +0000455 /// Return the type string for a BUILTIN() macro in Builtins.def.
456 std::string getBuiltinTypeStr();
457
458 /// Generate the intrinsic, returning code.
459 std::string generate();
460 /// Perform type checking and populate the dependency graph, but
461 /// don't generate code yet.
462 void indexBody();
463
464private:
David Blaikie4c96a5e2015-08-06 18:29:32 +0000465 std::string mangleName(std::string Name, ClassKind CK) const;
James Molloydee4ab02014-06-17 13:11:27 +0000466
467 void initVariables();
468 std::string replaceParamsIn(std::string S);
469
470 void emitBodyAsBuiltinCall();
James Molloydee4ab02014-06-17 13:11:27 +0000471
James Molloyb452f782014-06-27 11:53:35 +0000472 void generateImpl(bool ReverseArguments,
473 StringRef NamePrefix, StringRef CallPrefix);
James Molloydee4ab02014-06-17 13:11:27 +0000474 void emitReturn();
James Molloyb452f782014-06-27 11:53:35 +0000475 void emitBody(StringRef CallPrefix);
James Molloydee4ab02014-06-17 13:11:27 +0000476 void emitShadowedArgs();
James Molloyb452f782014-06-27 11:53:35 +0000477 void emitArgumentReversal();
478 void emitReturnReversal();
479 void emitReverseVariable(Variable &Dest, Variable &Src);
James Molloydee4ab02014-06-17 13:11:27 +0000480 void emitNewLine();
481 void emitClosingBrace();
482 void emitOpeningBrace();
James Molloyb452f782014-06-27 11:53:35 +0000483 void emitPrototype(StringRef NamePrefix);
484
485 class DagEmitter {
486 Intrinsic &Intr;
487 StringRef CallPrefix;
488
489 public:
490 DagEmitter(Intrinsic &Intr, StringRef CallPrefix) :
491 Intr(Intr), CallPrefix(CallPrefix) {
492 }
493 std::pair<Type, std::string> emitDagArg(Init *Arg, std::string ArgName);
494 std::pair<Type, std::string> emitDagSaveTemp(DagInit *DI);
495 std::pair<Type, std::string> emitDagSplat(DagInit *DI);
496 std::pair<Type, std::string> emitDagDup(DagInit *DI);
497 std::pair<Type, std::string> emitDagShuffle(DagInit *DI);
498 std::pair<Type, std::string> emitDagCast(DagInit *DI, bool IsBitCast);
499 std::pair<Type, std::string> emitDagCall(DagInit *DI);
500 std::pair<Type, std::string> emitDagNameReplace(DagInit *DI);
501 std::pair<Type, std::string> emitDagLiteral(DagInit *DI);
502 std::pair<Type, std::string> emitDagOp(DagInit *DI);
503 std::pair<Type, std::string> emitDag(DagInit *DI);
504 };
James Molloydee4ab02014-06-17 13:11:27 +0000505};
506
507//===----------------------------------------------------------------------===//
508// NeonEmitter
509//===----------------------------------------------------------------------===//
510
511class NeonEmitter {
512 RecordKeeper &Records;
513 DenseMap<Record *, ClassKind> ClassMap;
David Blaikie4c96a5e2015-08-06 18:29:32 +0000514 std::map<std::string, std::deque<Intrinsic>> IntrinsicMap;
James Molloydee4ab02014-06-17 13:11:27 +0000515 unsigned UniqueNumber;
516
517 void createIntrinsic(Record *R, SmallVectorImpl<Intrinsic *> &Out);
518 void genBuiltinsDef(raw_ostream &OS, SmallVectorImpl<Intrinsic *> &Defs);
519 void genOverloadTypeCheckCode(raw_ostream &OS,
520 SmallVectorImpl<Intrinsic *> &Defs);
521 void genIntrinsicRangeCheckCode(raw_ostream &OS,
522 SmallVectorImpl<Intrinsic *> &Defs);
523
524public:
525 /// Called by Intrinsic - this attempts to get an intrinsic that takes
526 /// the given types as arguments.
David Blaikie4c96a5e2015-08-06 18:29:32 +0000527 Intrinsic &getIntrinsic(StringRef Name, ArrayRef<Type> Types);
James Molloydee4ab02014-06-17 13:11:27 +0000528
529 /// Called by Intrinsic - returns a globally-unique number.
530 unsigned getUniqueNumber() { return UniqueNumber++; }
531
532 NeonEmitter(RecordKeeper &R) : Records(R), UniqueNumber(0) {
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000533 Record *SI = R.getClass("SInst");
534 Record *II = R.getClass("IInst");
535 Record *WI = R.getClass("WInst");
Michael Gottesmanfc89cc22013-04-16 21:18:42 +0000536 Record *SOpI = R.getClass("SOpInst");
537 Record *IOpI = R.getClass("IOpInst");
538 Record *WOpI = R.getClass("WOpInst");
539 Record *LOpI = R.getClass("LOpInst");
540 Record *NoTestOpI = R.getClass("NoTestOpInst");
541
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000542 ClassMap[SI] = ClassS;
543 ClassMap[II] = ClassI;
544 ClassMap[WI] = ClassW;
Michael Gottesmanfc89cc22013-04-16 21:18:42 +0000545 ClassMap[SOpI] = ClassS;
546 ClassMap[IOpI] = ClassI;
547 ClassMap[WOpI] = ClassW;
548 ClassMap[LOpI] = ClassL;
549 ClassMap[NoTestOpI] = ClassNoTest;
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000550 }
551
552 // run - Emit arm_neon.h.inc
553 void run(raw_ostream &o);
554
555 // runHeader - Emit all the __builtin prototypes used in arm_neon.h
556 void runHeader(raw_ostream &o);
557
558 // runTests - Emit tests for all the Neon intrinsics.
559 void runTests(raw_ostream &o);
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000560};
James Molloydee4ab02014-06-17 13:11:27 +0000561
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +0000562} // end anonymous namespace
563
James Molloydee4ab02014-06-17 13:11:27 +0000564//===----------------------------------------------------------------------===//
565// Type implementation
566//===----------------------------------------------------------------------===//
Peter Collingbournebee583f2011-10-06 13:03:08 +0000567
James Molloydee4ab02014-06-17 13:11:27 +0000568std::string Type::str() const {
569 if (Void)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000570 return "void";
James Molloydee4ab02014-06-17 13:11:27 +0000571 std::string S;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000572
James Molloydee4ab02014-06-17 13:11:27 +0000573 if (!Signed && isInteger())
574 S += "u";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000575
James Molloydee4ab02014-06-17 13:11:27 +0000576 if (Poly)
577 S += "poly";
578 else if (Float)
579 S += "float";
580 else
581 S += "int";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000582
James Molloydee4ab02014-06-17 13:11:27 +0000583 S += utostr(ElementBitwidth);
584 if (isVector())
585 S += "x" + utostr(getNumElements());
586 if (NumVectors > 1)
587 S += "x" + utostr(NumVectors);
588 S += "_t";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000589
James Molloydee4ab02014-06-17 13:11:27 +0000590 if (Constant)
591 S += " const";
592 if (Pointer)
593 S += " *";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000594
James Molloydee4ab02014-06-17 13:11:27 +0000595 return S;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000596}
597
James Molloydee4ab02014-06-17 13:11:27 +0000598std::string Type::builtin_str() const {
599 std::string S;
600 if (isVoid())
601 return "v";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000602
James Molloydee4ab02014-06-17 13:11:27 +0000603 if (Pointer)
604 // All pointers are void pointers.
605 S += "v";
606 else if (isInteger())
607 switch (ElementBitwidth) {
608 case 8: S += "c"; break;
609 case 16: S += "s"; break;
610 case 32: S += "i"; break;
611 case 64: S += "Wi"; break;
612 case 128: S += "LLLi"; break;
Craig Topper0039f3f2014-06-18 03:57:25 +0000613 default: llvm_unreachable("Unhandled case!");
James Molloydee4ab02014-06-17 13:11:27 +0000614 }
615 else
616 switch (ElementBitwidth) {
617 case 16: S += "h"; break;
618 case 32: S += "f"; break;
619 case 64: S += "d"; break;
Craig Topper0039f3f2014-06-18 03:57:25 +0000620 default: llvm_unreachable("Unhandled case!");
James Molloydee4ab02014-06-17 13:11:27 +0000621 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000622
James Molloydee4ab02014-06-17 13:11:27 +0000623 if (isChar() && !Pointer)
624 // Make chars explicitly signed.
625 S = "S" + S;
626 else if (isInteger() && !Pointer && !Signed)
627 S = "U" + S;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000628
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000629 // Constant indices are "int", but have the "constant expression" modifier.
630 if (isImmediate()) {
631 assert(isInteger() && isSigned());
632 S = "I" + S;
633 }
634
James Molloydee4ab02014-06-17 13:11:27 +0000635 if (isScalar()) {
636 if (Constant) S += "C";
637 if (Pointer) S += "*";
638 return S;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000639 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000640
James Molloydee4ab02014-06-17 13:11:27 +0000641 std::string Ret;
642 for (unsigned I = 0; I < NumVectors; ++I)
643 Ret += "V" + utostr(getNumElements()) + S;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000644
James Molloydee4ab02014-06-17 13:11:27 +0000645 return Ret;
646}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000647
James Molloydee4ab02014-06-17 13:11:27 +0000648unsigned Type::getNeonEnum() const {
649 unsigned Addend;
650 switch (ElementBitwidth) {
651 case 8: Addend = 0; break;
652 case 16: Addend = 1; break;
653 case 32: Addend = 2; break;
654 case 64: Addend = 3; break;
655 case 128: Addend = 4; break;
Craig Topperc7193c42014-06-18 03:13:41 +0000656 default: llvm_unreachable("Unhandled element bitwidth!");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000657 }
658
James Molloydee4ab02014-06-17 13:11:27 +0000659 unsigned Base = (unsigned)NeonTypeFlags::Int8 + Addend;
660 if (Poly) {
661 // Adjustment needed because Poly32 doesn't exist.
662 if (Addend >= 2)
663 --Addend;
664 Base = (unsigned)NeonTypeFlags::Poly8 + Addend;
665 }
666 if (Float) {
667 assert(Addend != 0 && "Float8 doesn't exist!");
668 Base = (unsigned)NeonTypeFlags::Float16 + (Addend - 1);
669 }
670
671 if (Bitwidth == 128)
672 Base |= (unsigned)NeonTypeFlags::QuadFlag;
673 if (isInteger() && !Signed)
674 Base |= (unsigned)NeonTypeFlags::UnsignedFlag;
675
676 return Base;
677}
678
679Type Type::fromTypedefName(StringRef Name) {
680 Type T;
681 T.Void = false;
682 T.Float = false;
683 T.Poly = false;
684
685 if (Name.front() == 'u') {
686 T.Signed = false;
687 Name = Name.drop_front();
688 } else {
689 T.Signed = true;
690 }
691
692 if (Name.startswith("float")) {
693 T.Float = true;
694 Name = Name.drop_front(5);
695 } else if (Name.startswith("poly")) {
696 T.Poly = true;
697 Name = Name.drop_front(4);
698 } else {
699 assert(Name.startswith("int"));
700 Name = Name.drop_front(3);
701 }
702
703 unsigned I = 0;
704 for (I = 0; I < Name.size(); ++I) {
705 if (!isdigit(Name[I]))
706 break;
707 }
708 Name.substr(0, I).getAsInteger(10, T.ElementBitwidth);
709 Name = Name.drop_front(I);
710
711 T.Bitwidth = T.ElementBitwidth;
712 T.NumVectors = 1;
713
714 if (Name.front() == 'x') {
715 Name = Name.drop_front();
716 unsigned I = 0;
717 for (I = 0; I < Name.size(); ++I) {
718 if (!isdigit(Name[I]))
719 break;
720 }
721 unsigned NumLanes;
722 Name.substr(0, I).getAsInteger(10, NumLanes);
723 Name = Name.drop_front(I);
724 T.Bitwidth = T.ElementBitwidth * NumLanes;
725 } else {
726 // Was scalar.
727 T.NumVectors = 0;
728 }
729 if (Name.front() == 'x') {
730 Name = Name.drop_front();
731 unsigned I = 0;
732 for (I = 0; I < Name.size(); ++I) {
733 if (!isdigit(Name[I]))
734 break;
735 }
736 Name.substr(0, I).getAsInteger(10, T.NumVectors);
737 Name = Name.drop_front(I);
738 }
739
740 assert(Name.startswith("_t") && "Malformed typedef!");
741 return T;
742}
743
744void Type::applyTypespec(bool &Quad) {
745 std::string S = TS;
746 ScalarForMangling = false;
747 Void = false;
748 Poly = Float = false;
749 ElementBitwidth = ~0U;
750 Signed = true;
751 NumVectors = 1;
752
753 for (char I : S) {
754 switch (I) {
755 case 'S':
756 ScalarForMangling = true;
757 break;
758 case 'H':
759 NoManglingQ = true;
760 Quad = true;
761 break;
762 case 'Q':
763 Quad = true;
764 break;
765 case 'P':
766 Poly = true;
767 break;
768 case 'U':
769 Signed = false;
770 break;
771 case 'c':
772 ElementBitwidth = 8;
773 break;
774 case 'h':
775 Float = true;
776 // Fall through
777 case 's':
778 ElementBitwidth = 16;
779 break;
780 case 'f':
781 Float = true;
782 // Fall through
783 case 'i':
784 ElementBitwidth = 32;
785 break;
786 case 'd':
787 Float = true;
788 // Fall through
789 case 'l':
790 ElementBitwidth = 64;
791 break;
792 case 'k':
793 ElementBitwidth = 128;
794 // Poly doesn't have a 128x1 type.
795 if (Poly)
796 NumVectors = 0;
797 break;
798 default:
Craig Topper0039f3f2014-06-18 03:57:25 +0000799 llvm_unreachable("Unhandled type code!");
James Molloydee4ab02014-06-17 13:11:27 +0000800 }
801 }
802 assert(ElementBitwidth != ~0U && "Bad element bitwidth!");
803
804 Bitwidth = Quad ? 128 : 64;
805}
806
807void Type::applyModifier(char Mod) {
808 bool AppliedQuad = false;
809 applyTypespec(AppliedQuad);
810
811 switch (Mod) {
812 case 'v':
813 Void = true;
814 break;
815 case 't':
816 if (Poly) {
817 Poly = false;
818 Signed = false;
819 }
820 break;
821 case 'b':
822 Signed = false;
823 Float = false;
824 Poly = false;
825 NumVectors = 0;
826 Bitwidth = ElementBitwidth;
827 break;
828 case '$':
829 Signed = true;
830 Float = false;
831 Poly = false;
832 NumVectors = 0;
833 Bitwidth = ElementBitwidth;
834 break;
835 case 'u':
836 Signed = false;
837 Poly = false;
838 Float = false;
839 break;
840 case 'x':
841 Signed = true;
842 assert(!Poly && "'u' can't be used with poly types!");
843 Float = false;
844 break;
845 case 'o':
846 Bitwidth = ElementBitwidth = 64;
847 NumVectors = 0;
848 Float = true;
849 break;
850 case 'y':
851 Bitwidth = ElementBitwidth = 32;
852 NumVectors = 0;
853 Float = true;
854 break;
855 case 'f':
James Molloydee4ab02014-06-17 13:11:27 +0000856 Float = true;
857 ElementBitwidth = 32;
858 break;
859 case 'F':
860 Float = true;
861 ElementBitwidth = 64;
862 break;
Abderrazek Zaafrania44e5f62017-06-01 23:22:29 +0000863 case 'H':
864 Float = true;
865 ElementBitwidth = 16;
866 break;
James Molloydee4ab02014-06-17 13:11:27 +0000867 case 'g':
868 if (AppliedQuad)
869 Bitwidth /= 2;
870 break;
871 case 'j':
872 if (!AppliedQuad)
873 Bitwidth *= 2;
874 break;
875 case 'w':
876 ElementBitwidth *= 2;
877 Bitwidth *= 2;
878 break;
879 case 'n':
880 ElementBitwidth *= 2;
881 break;
882 case 'i':
883 Float = false;
884 Poly = false;
885 ElementBitwidth = Bitwidth = 32;
886 NumVectors = 0;
887 Signed = true;
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000888 Immediate = true;
James Molloydee4ab02014-06-17 13:11:27 +0000889 break;
890 case 'l':
891 Float = false;
892 Poly = false;
893 ElementBitwidth = Bitwidth = 64;
894 NumVectors = 0;
895 Signed = false;
Ahmed Bougacha94df7302015-06-04 01:43:41 +0000896 Immediate = true;
James Molloydee4ab02014-06-17 13:11:27 +0000897 break;
898 case 'z':
899 ElementBitwidth /= 2;
900 Bitwidth = ElementBitwidth;
901 NumVectors = 0;
902 break;
903 case 'r':
904 ElementBitwidth *= 2;
905 Bitwidth = ElementBitwidth;
906 NumVectors = 0;
907 break;
908 case 's':
909 case 'a':
910 Bitwidth = ElementBitwidth;
911 NumVectors = 0;
912 break;
913 case 'k':
914 Bitwidth *= 2;
915 break;
916 case 'c':
917 Constant = true;
918 // Fall through
919 case 'p':
920 Pointer = true;
921 Bitwidth = ElementBitwidth;
922 NumVectors = 0;
923 break;
924 case 'h':
925 ElementBitwidth /= 2;
926 break;
927 case 'q':
928 ElementBitwidth /= 2;
929 Bitwidth *= 2;
930 break;
931 case 'e':
932 ElementBitwidth /= 2;
933 Signed = false;
934 break;
935 case 'm':
936 ElementBitwidth /= 2;
937 Bitwidth /= 2;
938 break;
939 case 'd':
940 break;
941 case '2':
942 NumVectors = 2;
943 break;
944 case '3':
945 NumVectors = 3;
946 break;
947 case '4':
948 NumVectors = 4;
949 break;
950 case 'B':
951 NumVectors = 2;
952 if (!AppliedQuad)
953 Bitwidth *= 2;
954 break;
955 case 'C':
956 NumVectors = 3;
957 if (!AppliedQuad)
958 Bitwidth *= 2;
959 break;
960 case 'D':
961 NumVectors = 4;
962 if (!AppliedQuad)
963 Bitwidth *= 2;
964 break;
965 default:
Craig Topper0039f3f2014-06-18 03:57:25 +0000966 llvm_unreachable("Unhandled character!");
James Molloydee4ab02014-06-17 13:11:27 +0000967 }
968}
969
970//===----------------------------------------------------------------------===//
971// Intrinsic implementation
972//===----------------------------------------------------------------------===//
973
David Blaikie4c96a5e2015-08-06 18:29:32 +0000974std::string Intrinsic::getInstTypeCode(Type T, ClassKind CK) const {
James Molloydee4ab02014-06-17 13:11:27 +0000975 char typeCode = '\0';
976 bool printNumber = true;
977
978 if (CK == ClassB)
979 return "";
980
981 if (T.isPoly())
982 typeCode = 'p';
983 else if (T.isInteger())
984 typeCode = T.isSigned() ? 's' : 'u';
985 else
986 typeCode = 'f';
987
988 if (CK == ClassI) {
989 switch (typeCode) {
990 default:
991 break;
992 case 's':
993 case 'u':
994 case 'p':
995 typeCode = 'i';
996 break;
997 }
998 }
999 if (CK == ClassB) {
1000 typeCode = '\0';
1001 }
1002
1003 std::string S;
1004 if (typeCode != '\0')
1005 S.push_back(typeCode);
1006 if (printNumber)
1007 S += utostr(T.getElementSizeInBits());
1008
1009 return S;
1010}
1011
Ahmed Bougacha22a16962015-08-21 23:24:18 +00001012static bool isFloatingPointProtoModifier(char Mod) {
Abderrazek Zaafrania44e5f62017-06-01 23:22:29 +00001013 return Mod == 'F' || Mod == 'f' || Mod == 'H';
Ahmed Bougacha22a16962015-08-21 23:24:18 +00001014}
1015
James Molloydee4ab02014-06-17 13:11:27 +00001016std::string Intrinsic::getBuiltinTypeStr() {
1017 ClassKind LocalCK = getClassKind(true);
1018 std::string S;
1019
1020 Type RetT = getReturnType();
1021 if ((LocalCK == ClassI || LocalCK == ClassW) && RetT.isScalar() &&
1022 !RetT.isFloating())
1023 RetT.makeInteger(RetT.getElementSizeInBits(), false);
1024
Peter Collingbournebee583f2011-10-06 13:03:08 +00001025 // Since the return value must be one type, return a vector type of the
1026 // appropriate width which we will bitcast. An exception is made for
1027 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
1028 // fashion, storing them to a pointer arg.
James Molloydee4ab02014-06-17 13:11:27 +00001029 if (RetT.getNumVectors() > 1) {
1030 S += "vv*"; // void result with void* first argument
1031 } else {
1032 if (RetT.isPoly())
1033 RetT.makeInteger(RetT.getElementSizeInBits(), false);
1034 if (!RetT.isScalar() && !RetT.isSigned())
1035 RetT.makeSigned();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001036
Ahmed Bougacha22a16962015-08-21 23:24:18 +00001037 bool ForcedVectorFloatingType = isFloatingPointProtoModifier(Proto[0]);
James Molloydee4ab02014-06-17 13:11:27 +00001038 if (LocalCK == ClassB && !RetT.isScalar() && !ForcedVectorFloatingType)
1039 // Cast to vector of 8-bit elements.
1040 RetT.makeInteger(8, true);
1041
1042 S += RetT.builtin_str();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001043 }
1044
James Molloydee4ab02014-06-17 13:11:27 +00001045 for (unsigned I = 0; I < getNumParams(); ++I) {
1046 Type T = getParamType(I);
1047 if (T.isPoly())
1048 T.makeInteger(T.getElementSizeInBits(), false);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001049
Ahmed Bougacha22a16962015-08-21 23:24:18 +00001050 bool ForcedFloatingType = isFloatingPointProtoModifier(Proto[I + 1]);
James Molloydee4ab02014-06-17 13:11:27 +00001051 if (LocalCK == ClassB && !T.isScalar() && !ForcedFloatingType)
1052 T.makeInteger(8, true);
1053 // Halves always get converted to 8-bit elements.
1054 if (T.isHalf() && T.isVector() && !T.isScalarForMangling())
1055 T.makeInteger(8, true);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001056
James Molloydee4ab02014-06-17 13:11:27 +00001057 if (LocalCK == ClassI)
1058 T.makeSigned();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001059
James Molloydee4ab02014-06-17 13:11:27 +00001060 if (hasImmediate() && getImmediateIdx() == I)
Ahmed Bougacha94df7302015-06-04 01:43:41 +00001061 T.makeImmediate(32);
Michael Gottesman095c58f2013-04-16 22:07:30 +00001062
James Molloydee4ab02014-06-17 13:11:27 +00001063 S += T.builtin_str();
Michael Gottesman095c58f2013-04-16 22:07:30 +00001064 }
James Molloydee4ab02014-06-17 13:11:27 +00001065
1066 // Extra constant integer to hold type class enum for this function, e.g. s8
1067 if (LocalCK == ClassB)
1068 S += "i";
1069
1070 return S;
Michael Gottesman095c58f2013-04-16 22:07:30 +00001071}
1072
David Blaikie4c96a5e2015-08-06 18:29:32 +00001073std::string Intrinsic::getMangledName(bool ForceClassS) const {
James Molloydee4ab02014-06-17 13:11:27 +00001074 // Check if the prototype has a scalar operand with the type of the vector
1075 // elements. If not, bitcasting the args will take care of arg checking.
1076 // The actual signedness etc. will be taken care of with special enums.
1077 ClassKind LocalCK = CK;
1078 if (!protoHasScalar())
1079 LocalCK = ClassB;
1080
1081 return mangleName(Name, ForceClassS ? ClassS : LocalCK);
Kevin Qinc076d062013-08-29 07:55:15 +00001082}
1083
David Blaikie4c96a5e2015-08-06 18:29:32 +00001084std::string Intrinsic::mangleName(std::string Name, ClassKind LocalCK) const {
James Molloydee4ab02014-06-17 13:11:27 +00001085 std::string typeCode = getInstTypeCode(BaseType, LocalCK);
1086 std::string S = Name;
Hao Liu5e4ce1a2013-11-18 06:33:43 +00001087
Ahmed Bougachacd5b8a02015-08-21 23:34:20 +00001088 if (Name == "vcvt_f16_f32" || Name == "vcvt_f32_f16" ||
1089 Name == "vcvt_f32_f64" || Name == "vcvt_f64_f32")
James Molloydee4ab02014-06-17 13:11:27 +00001090 return Name;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001091
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001092 if (!typeCode.empty()) {
James Molloydee4ab02014-06-17 13:11:27 +00001093 // If the name ends with _xN (N = 2,3,4), insert the typeCode before _xN.
1094 if (Name.size() >= 3 && isdigit(Name.back()) &&
1095 Name[Name.length() - 2] == 'x' && Name[Name.length() - 3] == '_')
1096 S.insert(S.length() - 3, "_" + typeCode);
Hao Liu5e4ce1a2013-11-18 06:33:43 +00001097 else
James Molloydee4ab02014-06-17 13:11:27 +00001098 S += "_" + typeCode;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001099 }
Michael Gottesman095c58f2013-04-16 22:07:30 +00001100
James Molloydee4ab02014-06-17 13:11:27 +00001101 if (BaseType != InBaseType) {
1102 // A reinterpret - out the input base type at the end.
1103 S += "_" + getInstTypeCode(InBaseType, LocalCK);
1104 }
1105
1106 if (LocalCK == ClassB)
1107 S += "_v";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001108
1109 // Insert a 'q' before the first '_' character so that it ends up before
1110 // _lane or _n on vector-scalar operations.
James Molloydee4ab02014-06-17 13:11:27 +00001111 if (BaseType.getSizeInBits() == 128 && !BaseType.noManglingQ()) {
1112 size_t Pos = S.find('_');
1113 S.insert(Pos, "q");
Kevin Qinc076d062013-08-29 07:55:15 +00001114 }
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001115
James Molloydee4ab02014-06-17 13:11:27 +00001116 char Suffix = '\0';
1117 if (BaseType.isScalarForMangling()) {
1118 switch (BaseType.getElementSizeInBits()) {
1119 case 8: Suffix = 'b'; break;
1120 case 16: Suffix = 'h'; break;
1121 case 32: Suffix = 's'; break;
1122 case 64: Suffix = 'd'; break;
Craig Topper0039f3f2014-06-18 03:57:25 +00001123 default: llvm_unreachable("Bad suffix!");
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001124 }
1125 }
James Molloydee4ab02014-06-17 13:11:27 +00001126 if (Suffix != '\0') {
1127 size_t Pos = S.find('_');
1128 S.insert(Pos, &Suffix, 1);
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001129 }
James Molloydee4ab02014-06-17 13:11:27 +00001130
1131 return S;
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001132}
1133
James Molloydee4ab02014-06-17 13:11:27 +00001134std::string Intrinsic::replaceParamsIn(std::string S) {
1135 while (S.find('$') != std::string::npos) {
1136 size_t Pos = S.find('$');
1137 size_t End = Pos + 1;
1138 while (isalpha(S[End]))
1139 ++End;
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001140
James Molloydee4ab02014-06-17 13:11:27 +00001141 std::string VarName = S.substr(Pos + 1, End - Pos - 1);
1142 assert_with_loc(Variables.find(VarName) != Variables.end(),
1143 "Variable not defined!");
1144 S.replace(Pos, End - Pos, Variables.find(VarName)->second.getName());
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001145 }
1146
James Molloydee4ab02014-06-17 13:11:27 +00001147 return S;
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001148}
1149
James Molloydee4ab02014-06-17 13:11:27 +00001150void Intrinsic::initVariables() {
1151 Variables.clear();
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001152
James Molloydee4ab02014-06-17 13:11:27 +00001153 // Modify the TypeSpec per-argument to get a concrete Type, and create
1154 // known variables for each.
1155 for (unsigned I = 1; I < Proto.size(); ++I) {
1156 char NameC = '0' + (I - 1);
1157 std::string Name = "p";
1158 Name.push_back(NameC);
1159
1160 Variables[Name] = Variable(Types[I], Name + VariablePostfix);
1161 }
1162 RetVar = Variable(Types[0], "ret" + VariablePostfix);
1163}
1164
James Molloyb452f782014-06-27 11:53:35 +00001165void Intrinsic::emitPrototype(StringRef NamePrefix) {
James Molloydee4ab02014-06-17 13:11:27 +00001166 if (UseMacro)
1167 OS << "#define ";
1168 else
1169 OS << "__ai " << Types[0].str() << " ";
1170
James Molloyb452f782014-06-27 11:53:35 +00001171 OS << NamePrefix.str() << mangleName(Name, ClassS) << "(";
James Molloydee4ab02014-06-17 13:11:27 +00001172
1173 for (unsigned I = 0; I < getNumParams(); ++I) {
1174 if (I != 0)
1175 OS << ", ";
1176
1177 char NameC = '0' + I;
1178 std::string Name = "p";
1179 Name.push_back(NameC);
1180 assert(Variables.find(Name) != Variables.end());
1181 Variable &V = Variables[Name];
1182
1183 if (!UseMacro)
1184 OS << V.getType().str() << " ";
1185 OS << V.getName();
1186 }
1187
1188 OS << ")";
1189}
1190
1191void Intrinsic::emitOpeningBrace() {
1192 if (UseMacro)
1193 OS << " __extension__ ({";
1194 else
1195 OS << " {";
1196 emitNewLine();
1197}
1198
1199void Intrinsic::emitClosingBrace() {
1200 if (UseMacro)
1201 OS << "})";
1202 else
1203 OS << "}";
1204}
1205
1206void Intrinsic::emitNewLine() {
1207 if (UseMacro)
1208 OS << " \\\n";
1209 else
1210 OS << "\n";
1211}
1212
James Molloyb452f782014-06-27 11:53:35 +00001213void Intrinsic::emitReverseVariable(Variable &Dest, Variable &Src) {
1214 if (Dest.getType().getNumVectors() > 1) {
1215 emitNewLine();
1216
1217 for (unsigned K = 0; K < Dest.getType().getNumVectors(); ++K) {
Craig Topper054b3912016-01-31 00:20:26 +00001218 OS << " " << Dest.getName() << ".val[" << K << "] = "
James Molloyb452f782014-06-27 11:53:35 +00001219 << "__builtin_shufflevector("
Craig Topper054b3912016-01-31 00:20:26 +00001220 << Src.getName() << ".val[" << K << "], "
1221 << Src.getName() << ".val[" << K << "]";
James Molloyb452f782014-06-27 11:53:35 +00001222 for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
Craig Topper054b3912016-01-31 00:20:26 +00001223 OS << ", " << J;
James Molloyb452f782014-06-27 11:53:35 +00001224 OS << ");";
1225 emitNewLine();
1226 }
1227 } else {
1228 OS << " " << Dest.getName()
1229 << " = __builtin_shufflevector(" << Src.getName() << ", " << Src.getName();
1230 for (int J = Dest.getType().getNumElements() - 1; J >= 0; --J)
Craig Topper054b3912016-01-31 00:20:26 +00001231 OS << ", " << J;
James Molloyb452f782014-06-27 11:53:35 +00001232 OS << ");";
1233 emitNewLine();
1234 }
1235}
1236
1237void Intrinsic::emitArgumentReversal() {
1238 if (BigEndianSafe)
1239 return;
1240
1241 // Reverse all vector arguments.
1242 for (unsigned I = 0; I < getNumParams(); ++I) {
1243 std::string Name = "p" + utostr(I);
1244 std::string NewName = "rev" + utostr(I);
1245
1246 Variable &V = Variables[Name];
1247 Variable NewV(V.getType(), NewName + VariablePostfix);
1248
1249 if (!NewV.getType().isVector() || NewV.getType().getNumElements() == 1)
1250 continue;
1251
1252 OS << " " << NewV.getType().str() << " " << NewV.getName() << ";";
1253 emitReverseVariable(NewV, V);
1254 V = NewV;
1255 }
1256}
1257
1258void Intrinsic::emitReturnReversal() {
1259 if (BigEndianSafe)
1260 return;
1261 if (!getReturnType().isVector() || getReturnType().isVoid() ||
1262 getReturnType().getNumElements() == 1)
1263 return;
1264 emitReverseVariable(RetVar, RetVar);
1265}
1266
James Molloydee4ab02014-06-17 13:11:27 +00001267void Intrinsic::emitShadowedArgs() {
1268 // Macro arguments are not type-checked like inline function arguments,
1269 // so assign them to local temporaries to get the right type checking.
1270 if (!UseMacro)
Michael Gottesman6cd3e562013-04-16 23:00:26 +00001271 return;
1272
James Molloydee4ab02014-06-17 13:11:27 +00001273 for (unsigned I = 0; I < getNumParams(); ++I) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001274 // Do not create a temporary for an immediate argument.
1275 // That would defeat the whole point of using a macro!
James Molloydee4ab02014-06-17 13:11:27 +00001276 if (hasImmediate() && Proto[I+1] == 'i')
Peter Collingbournebee583f2011-10-06 13:03:08 +00001277 continue;
James Molloydee4ab02014-06-17 13:11:27 +00001278 // Do not create a temporary for pointer arguments. The input
1279 // pointer may have an alignment hint.
1280 if (getParamType(I).isPointer())
1281 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001282
James Molloyb452f782014-06-27 11:53:35 +00001283 std::string Name = "p" + utostr(I);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001284
James Molloydee4ab02014-06-17 13:11:27 +00001285 assert(Variables.find(Name) != Variables.end());
1286 Variable &V = Variables[Name];
Peter Collingbournebee583f2011-10-06 13:03:08 +00001287
James Molloydee4ab02014-06-17 13:11:27 +00001288 std::string NewName = "s" + utostr(I);
1289 Variable V2(V.getType(), NewName + VariablePostfix);
Jiangning Liu1bda93a2013-09-09 02:21:08 +00001290
James Molloydee4ab02014-06-17 13:11:27 +00001291 OS << " " << V2.getType().str() << " " << V2.getName() << " = "
1292 << V.getName() << ";";
1293 emitNewLine();
Jiangning Liu1bda93a2013-09-09 02:21:08 +00001294
James Molloydee4ab02014-06-17 13:11:27 +00001295 V = V2;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001296 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001297}
1298
Jiangning Liuee3e0872013-11-27 14:02:55 +00001299// We don't check 'a' in this function, because for builtin function the
1300// argument matching to 'a' uses a vector type splatted from a scalar type.
David Blaikie4c96a5e2015-08-06 18:29:32 +00001301bool Intrinsic::protoHasScalar() const {
James Molloydee4ab02014-06-17 13:11:27 +00001302 return (Proto.find('s') != std::string::npos ||
1303 Proto.find('z') != std::string::npos ||
1304 Proto.find('r') != std::string::npos ||
1305 Proto.find('b') != std::string::npos ||
1306 Proto.find('$') != std::string::npos ||
1307 Proto.find('y') != std::string::npos ||
1308 Proto.find('o') != std::string::npos);
Jiangning Liub96ebac2013-10-05 08:22:55 +00001309}
1310
James Molloydee4ab02014-06-17 13:11:27 +00001311void Intrinsic::emitBodyAsBuiltinCall() {
1312 std::string S;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001313
1314 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
1315 // sret-like argument.
James Molloydee4ab02014-06-17 13:11:27 +00001316 bool SRet = getReturnType().getNumVectors() >= 2;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001317
James Molloydee4ab02014-06-17 13:11:27 +00001318 StringRef N = Name;
1319 if (hasSplat()) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001320 // Call the non-splat builtin: chop off the "_n" suffix from the name.
James Molloydee4ab02014-06-17 13:11:27 +00001321 assert(N.endswith("_n"));
1322 N = N.drop_back(2);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001323 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001324
James Molloydee4ab02014-06-17 13:11:27 +00001325 ClassKind LocalCK = CK;
1326 if (!protoHasScalar())
1327 LocalCK = ClassB;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001328
James Molloydee4ab02014-06-17 13:11:27 +00001329 if (!getReturnType().isVoid() && !SRet)
1330 S += "(" + RetVar.getType().str() + ") ";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001331
James Molloydee4ab02014-06-17 13:11:27 +00001332 S += "__builtin_neon_" + mangleName(N, LocalCK) + "(";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001333
James Molloydee4ab02014-06-17 13:11:27 +00001334 if (SRet)
1335 S += "&" + RetVar.getName() + ", ";
1336
1337 for (unsigned I = 0; I < getNumParams(); ++I) {
1338 Variable &V = Variables["p" + utostr(I)];
1339 Type T = V.getType();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001340
1341 // Handle multiple-vector values specially, emitting each subvector as an
James Molloydee4ab02014-06-17 13:11:27 +00001342 // argument to the builtin.
1343 if (T.getNumVectors() > 1) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001344 // Check if an explicit cast is needed.
James Molloydee4ab02014-06-17 13:11:27 +00001345 std::string Cast;
1346 if (T.isChar() || T.isPoly() || !T.isSigned()) {
1347 Type T2 = T;
1348 T2.makeOneVector();
1349 T2.makeInteger(8, /*Signed=*/true);
1350 Cast = "(" + T2.str() + ")";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001351 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001352
James Molloydee4ab02014-06-17 13:11:27 +00001353 for (unsigned J = 0; J < T.getNumVectors(); ++J)
1354 S += Cast + V.getName() + ".val[" + utostr(J) + "], ";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001355 continue;
1356 }
1357
James Molloydee4ab02014-06-17 13:11:27 +00001358 std::string Arg;
1359 Type CastToType = T;
1360 if (hasSplat() && I == getSplatIdx()) {
1361 Arg = "(" + BaseType.str() + ") {";
1362 for (unsigned J = 0; J < BaseType.getNumElements(); ++J) {
1363 if (J != 0)
1364 Arg += ", ";
1365 Arg += V.getName();
1366 }
1367 Arg += "}";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001368
James Molloydee4ab02014-06-17 13:11:27 +00001369 CastToType = BaseType;
1370 } else {
1371 Arg = V.getName();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001372 }
1373
James Molloydee4ab02014-06-17 13:11:27 +00001374 // Check if an explicit cast is needed.
1375 if (CastToType.isVector()) {
1376 CastToType.makeInteger(8, true);
1377 Arg = "(" + CastToType.str() + ")" + Arg;
1378 }
1379
1380 S += Arg + ", ";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001381 }
1382
1383 // Extra constant integer to hold type class enum for this function, e.g. s8
James Molloydee4ab02014-06-17 13:11:27 +00001384 if (getClassKind(true) == ClassB) {
1385 Type ThisTy = getReturnType();
Ahmed Bougacha22a16962015-08-21 23:24:18 +00001386 if (Proto[0] == 'v' || isFloatingPointProtoModifier(Proto[0]))
James Molloydee4ab02014-06-17 13:11:27 +00001387 ThisTy = getParamType(0);
1388 if (ThisTy.isPointer())
1389 ThisTy = getParamType(1);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001390
James Molloydee4ab02014-06-17 13:11:27 +00001391 S += utostr(ThisTy.getNeonEnum());
1392 } else {
1393 // Remove extraneous ", ".
1394 S.pop_back();
1395 S.pop_back();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001396 }
James Molloydee4ab02014-06-17 13:11:27 +00001397 S += ");";
1398
1399 std::string RetExpr;
1400 if (!SRet && !RetVar.getType().isVoid())
1401 RetExpr = RetVar.getName() + " = ";
1402
1403 OS << " " << RetExpr << S;
1404 emitNewLine();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001405}
1406
James Molloyb452f782014-06-27 11:53:35 +00001407void Intrinsic::emitBody(StringRef CallPrefix) {
James Molloydee4ab02014-06-17 13:11:27 +00001408 std::vector<std::string> Lines;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001409
James Molloydee4ab02014-06-17 13:11:27 +00001410 assert(RetVar.getType() == Types[0]);
1411 // Create a return variable, if we're not void.
1412 if (!RetVar.getType().isVoid()) {
1413 OS << " " << RetVar.getType().str() << " " << RetVar.getName() << ";";
1414 emitNewLine();
1415 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001416
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001417 if (!Body || Body->getValues().empty()) {
James Molloydee4ab02014-06-17 13:11:27 +00001418 // Nothing specific to output - must output a builtin.
1419 emitBodyAsBuiltinCall();
1420 return;
1421 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001422
James Molloydee4ab02014-06-17 13:11:27 +00001423 // We have a list of "things to output". The last should be returned.
1424 for (auto *I : Body->getValues()) {
1425 if (StringInit *SI = dyn_cast<StringInit>(I)) {
1426 Lines.push_back(replaceParamsIn(SI->getAsString()));
1427 } else if (DagInit *DI = dyn_cast<DagInit>(I)) {
James Molloyb452f782014-06-27 11:53:35 +00001428 DagEmitter DE(*this, CallPrefix);
1429 Lines.push_back(DE.emitDag(DI).second + ";");
James Molloydee4ab02014-06-17 13:11:27 +00001430 }
1431 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001432
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001433 assert(!Lines.empty() && "Empty def?");
James Molloydee4ab02014-06-17 13:11:27 +00001434 if (!RetVar.getType().isVoid())
1435 Lines.back().insert(0, RetVar.getName() + " = ");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001436
James Molloydee4ab02014-06-17 13:11:27 +00001437 for (auto &L : Lines) {
1438 OS << " " << L;
1439 emitNewLine();
1440 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001441}
1442
James Molloydee4ab02014-06-17 13:11:27 +00001443void Intrinsic::emitReturn() {
1444 if (RetVar.getType().isVoid())
1445 return;
1446 if (UseMacro)
1447 OS << " " << RetVar.getName() << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001448 else
James Molloydee4ab02014-06-17 13:11:27 +00001449 OS << " return " << RetVar.getName() << ";";
1450 emitNewLine();
1451}
Peter Collingbournebee583f2011-10-06 13:03:08 +00001452
James Molloyb452f782014-06-27 11:53:35 +00001453std::pair<Type, std::string> Intrinsic::DagEmitter::emitDag(DagInit *DI) {
James Molloydee4ab02014-06-17 13:11:27 +00001454 // At this point we should only be seeing a def.
1455 DefInit *DefI = cast<DefInit>(DI->getOperator());
1456 std::string Op = DefI->getAsString();
1457
1458 if (Op == "cast" || Op == "bitcast")
1459 return emitDagCast(DI, Op == "bitcast");
1460 if (Op == "shuffle")
1461 return emitDagShuffle(DI);
1462 if (Op == "dup")
1463 return emitDagDup(DI);
1464 if (Op == "splat")
1465 return emitDagSplat(DI);
1466 if (Op == "save_temp")
1467 return emitDagSaveTemp(DI);
1468 if (Op == "op")
1469 return emitDagOp(DI);
1470 if (Op == "call")
1471 return emitDagCall(DI);
1472 if (Op == "name_replace")
1473 return emitDagNameReplace(DI);
1474 if (Op == "literal")
1475 return emitDagLiteral(DI);
1476 assert_with_loc(false, "Unknown operation!");
1477 return std::make_pair(Type::getVoid(), "");
1478}
1479
James Molloyb452f782014-06-27 11:53:35 +00001480std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagOp(DagInit *DI) {
James Molloydee4ab02014-06-17 13:11:27 +00001481 std::string Op = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
1482 if (DI->getNumArgs() == 2) {
1483 // Unary op.
1484 std::pair<Type, std::string> R =
Matthias Braunf1b01992016-12-05 06:00:51 +00001485 emitDagArg(DI->getArg(1), DI->getArgNameStr(1));
James Molloydee4ab02014-06-17 13:11:27 +00001486 return std::make_pair(R.first, Op + R.second);
1487 } else {
1488 assert(DI->getNumArgs() == 3 && "Can only handle unary and binary ops!");
1489 std::pair<Type, std::string> R1 =
Matthias Braunf1b01992016-12-05 06:00:51 +00001490 emitDagArg(DI->getArg(1), DI->getArgNameStr(1));
James Molloydee4ab02014-06-17 13:11:27 +00001491 std::pair<Type, std::string> R2 =
Matthias Braunf1b01992016-12-05 06:00:51 +00001492 emitDagArg(DI->getArg(2), DI->getArgNameStr(2));
James Molloydee4ab02014-06-17 13:11:27 +00001493 assert_with_loc(R1.first == R2.first, "Argument type mismatch!");
1494 return std::make_pair(R1.first, R1.second + " " + Op + " " + R2.second);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001495 }
James Molloydee4ab02014-06-17 13:11:27 +00001496}
Peter Collingbournebee583f2011-10-06 13:03:08 +00001497
James Molloyb452f782014-06-27 11:53:35 +00001498std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCall(DagInit *DI) {
James Molloydee4ab02014-06-17 13:11:27 +00001499 std::vector<Type> Types;
1500 std::vector<std::string> Values;
1501 for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
1502 std::pair<Type, std::string> R =
Matthias Braunf1b01992016-12-05 06:00:51 +00001503 emitDagArg(DI->getArg(I + 1), DI->getArgNameStr(I + 1));
James Molloydee4ab02014-06-17 13:11:27 +00001504 Types.push_back(R.first);
1505 Values.push_back(R.second);
1506 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001507
James Molloydee4ab02014-06-17 13:11:27 +00001508 // Look up the called intrinsic.
1509 std::string N;
1510 if (StringInit *SI = dyn_cast<StringInit>(DI->getArg(0)))
1511 N = SI->getAsUnquotedString();
Peter Collingbournebee583f2011-10-06 13:03:08 +00001512 else
James Molloydee4ab02014-06-17 13:11:27 +00001513 N = emitDagArg(DI->getArg(0), "").second;
David Blaikie4c96a5e2015-08-06 18:29:32 +00001514 Intrinsic &Callee = Intr.Emitter.getIntrinsic(N, Types);
James Molloydee4ab02014-06-17 13:11:27 +00001515
1516 // Make sure the callee is known as an early def.
David Blaikie4c96a5e2015-08-06 18:29:32 +00001517 Callee.setNeededEarly();
1518 Intr.Dependencies.insert(&Callee);
James Molloydee4ab02014-06-17 13:11:27 +00001519
1520 // Now create the call itself.
David Blaikie4c96a5e2015-08-06 18:29:32 +00001521 std::string S = CallPrefix.str() + Callee.getMangledName(true) + "(";
James Molloydee4ab02014-06-17 13:11:27 +00001522 for (unsigned I = 0; I < DI->getNumArgs() - 1; ++I) {
1523 if (I != 0)
1524 S += ", ";
1525 S += Values[I];
1526 }
1527 S += ")";
1528
David Blaikie4c96a5e2015-08-06 18:29:32 +00001529 return std::make_pair(Callee.getReturnType(), S);
James Molloydee4ab02014-06-17 13:11:27 +00001530}
1531
James Molloyb452f782014-06-27 11:53:35 +00001532std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagCast(DagInit *DI,
1533 bool IsBitCast){
James Molloydee4ab02014-06-17 13:11:27 +00001534 // (cast MOD* VAL) -> cast VAL to type given by MOD.
1535 std::pair<Type, std::string> R = emitDagArg(
Matthias Braunf1b01992016-12-05 06:00:51 +00001536 DI->getArg(DI->getNumArgs() - 1),
1537 DI->getArgNameStr(DI->getNumArgs() - 1));
James Molloydee4ab02014-06-17 13:11:27 +00001538 Type castToType = R.first;
1539 for (unsigned ArgIdx = 0; ArgIdx < DI->getNumArgs() - 1; ++ArgIdx) {
1540
1541 // MOD can take several forms:
1542 // 1. $X - take the type of parameter / variable X.
1543 // 2. The value "R" - take the type of the return type.
1544 // 3. a type string
1545 // 4. The value "U" or "S" to switch the signedness.
1546 // 5. The value "H" or "D" to half or double the bitwidth.
1547 // 6. The value "8" to convert to 8-bit (signed) integer lanes.
Matthias Braunf1b01992016-12-05 06:00:51 +00001548 if (!DI->getArgNameStr(ArgIdx).empty()) {
1549 assert_with_loc(Intr.Variables.find(DI->getArgNameStr(ArgIdx)) !=
James Molloyb452f782014-06-27 11:53:35 +00001550 Intr.Variables.end(),
James Molloydee4ab02014-06-17 13:11:27 +00001551 "Variable not found");
Matthias Braunf1b01992016-12-05 06:00:51 +00001552 castToType = Intr.Variables[DI->getArgNameStr(ArgIdx)].getType();
James Molloydee4ab02014-06-17 13:11:27 +00001553 } else {
1554 StringInit *SI = dyn_cast<StringInit>(DI->getArg(ArgIdx));
1555 assert_with_loc(SI, "Expected string type or $Name for cast type");
1556
1557 if (SI->getAsUnquotedString() == "R") {
James Molloyb452f782014-06-27 11:53:35 +00001558 castToType = Intr.getReturnType();
James Molloydee4ab02014-06-17 13:11:27 +00001559 } else if (SI->getAsUnquotedString() == "U") {
1560 castToType.makeUnsigned();
1561 } else if (SI->getAsUnquotedString() == "S") {
1562 castToType.makeSigned();
1563 } else if (SI->getAsUnquotedString() == "H") {
1564 castToType.halveLanes();
1565 } else if (SI->getAsUnquotedString() == "D") {
1566 castToType.doubleLanes();
1567 } else if (SI->getAsUnquotedString() == "8") {
1568 castToType.makeInteger(8, true);
1569 } else {
1570 castToType = Type::fromTypedefName(SI->getAsUnquotedString());
1571 assert_with_loc(!castToType.isVoid(), "Unknown typedef");
1572 }
1573 }
1574 }
1575
1576 std::string S;
1577 if (IsBitCast) {
1578 // Emit a reinterpret cast. The second operand must be an lvalue, so create
1579 // a temporary.
1580 std::string N = "reint";
1581 unsigned I = 0;
James Molloyb452f782014-06-27 11:53:35 +00001582 while (Intr.Variables.find(N) != Intr.Variables.end())
James Molloydee4ab02014-06-17 13:11:27 +00001583 N = "reint" + utostr(++I);
James Molloyb452f782014-06-27 11:53:35 +00001584 Intr.Variables[N] = Variable(R.first, N + Intr.VariablePostfix);
James Molloydee4ab02014-06-17 13:11:27 +00001585
James Molloyb452f782014-06-27 11:53:35 +00001586 Intr.OS << R.first.str() << " " << Intr.Variables[N].getName() << " = "
1587 << R.second << ";";
1588 Intr.emitNewLine();
James Molloydee4ab02014-06-17 13:11:27 +00001589
James Molloyb452f782014-06-27 11:53:35 +00001590 S = "*(" + castToType.str() + " *) &" + Intr.Variables[N].getName() + "";
James Molloydee4ab02014-06-17 13:11:27 +00001591 } else {
1592 // Emit a normal (static) cast.
1593 S = "(" + castToType.str() + ")(" + R.second + ")";
1594 }
1595
1596 return std::make_pair(castToType, S);
1597}
1598
James Molloyb452f782014-06-27 11:53:35 +00001599std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagShuffle(DagInit *DI){
James Molloydee4ab02014-06-17 13:11:27 +00001600 // See the documentation in arm_neon.td for a description of these operators.
1601 class LowHalf : public SetTheory::Operator {
1602 public:
Alexander Kornienko34eb2072015-04-11 02:00:23 +00001603 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1604 ArrayRef<SMLoc> Loc) override {
James Molloydee4ab02014-06-17 13:11:27 +00001605 SetTheory::RecSet Elts2;
1606 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
1607 Elts.insert(Elts2.begin(), Elts2.begin() + (Elts2.size() / 2));
1608 }
1609 };
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001610
James Molloydee4ab02014-06-17 13:11:27 +00001611 class HighHalf : public SetTheory::Operator {
1612 public:
Alexander Kornienko34eb2072015-04-11 02:00:23 +00001613 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1614 ArrayRef<SMLoc> Loc) override {
James Molloydee4ab02014-06-17 13:11:27 +00001615 SetTheory::RecSet Elts2;
1616 ST.evaluate(Expr->arg_begin(), Expr->arg_end(), Elts2, Loc);
1617 Elts.insert(Elts2.begin() + (Elts2.size() / 2), Elts2.end());
1618 }
1619 };
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001620
James Molloydee4ab02014-06-17 13:11:27 +00001621 class Rev : public SetTheory::Operator {
1622 unsigned ElementSize;
1623
1624 public:
1625 Rev(unsigned ElementSize) : ElementSize(ElementSize) {}
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001626
Alexander Kornienko34eb2072015-04-11 02:00:23 +00001627 void apply(SetTheory &ST, DagInit *Expr, SetTheory::RecSet &Elts,
1628 ArrayRef<SMLoc> Loc) override {
James Molloydee4ab02014-06-17 13:11:27 +00001629 SetTheory::RecSet Elts2;
1630 ST.evaluate(Expr->arg_begin() + 1, Expr->arg_end(), Elts2, Loc);
1631
1632 int64_t VectorSize = cast<IntInit>(Expr->getArg(0))->getValue();
1633 VectorSize /= ElementSize;
1634
1635 std::vector<Record *> Revved;
1636 for (unsigned VI = 0; VI < Elts2.size(); VI += VectorSize) {
1637 for (int LI = VectorSize - 1; LI >= 0; --LI) {
1638 Revved.push_back(Elts2[VI + LI]);
1639 }
1640 }
1641
1642 Elts.insert(Revved.begin(), Revved.end());
1643 }
1644 };
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001645
James Molloydee4ab02014-06-17 13:11:27 +00001646 class MaskExpander : public SetTheory::Expander {
1647 unsigned N;
1648
1649 public:
1650 MaskExpander(unsigned N) : N(N) {}
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001651
Alexander Kornienko34eb2072015-04-11 02:00:23 +00001652 void expand(SetTheory &ST, Record *R, SetTheory::RecSet &Elts) override {
James Molloydee4ab02014-06-17 13:11:27 +00001653 unsigned Addend = 0;
1654 if (R->getName() == "mask0")
1655 Addend = 0;
1656 else if (R->getName() == "mask1")
1657 Addend = N;
1658 else
1659 return;
1660 for (unsigned I = 0; I < N; ++I)
1661 Elts.insert(R->getRecords().getDef("sv" + utostr(I + Addend)));
1662 }
1663 };
1664
1665 // (shuffle arg1, arg2, sequence)
1666 std::pair<Type, std::string> Arg1 =
Matthias Braunf1b01992016-12-05 06:00:51 +00001667 emitDagArg(DI->getArg(0), DI->getArgNameStr(0));
James Molloydee4ab02014-06-17 13:11:27 +00001668 std::pair<Type, std::string> Arg2 =
Matthias Braunf1b01992016-12-05 06:00:51 +00001669 emitDagArg(DI->getArg(1), DI->getArgNameStr(1));
James Molloydee4ab02014-06-17 13:11:27 +00001670 assert_with_loc(Arg1.first == Arg2.first,
1671 "Different types in arguments to shuffle!");
1672
1673 SetTheory ST;
James Molloydee4ab02014-06-17 13:11:27 +00001674 SetTheory::RecSet Elts;
Craig Topperbccb7732015-04-24 06:53:50 +00001675 ST.addOperator("lowhalf", llvm::make_unique<LowHalf>());
1676 ST.addOperator("highhalf", llvm::make_unique<HighHalf>());
1677 ST.addOperator("rev",
1678 llvm::make_unique<Rev>(Arg1.first.getElementSizeInBits()));
1679 ST.addExpander("MaskExpand",
1680 llvm::make_unique<MaskExpander>(Arg1.first.getNumElements()));
Craig Topper5fc8fc22014-08-27 06:28:36 +00001681 ST.evaluate(DI->getArg(2), Elts, None);
James Molloydee4ab02014-06-17 13:11:27 +00001682
1683 std::string S = "__builtin_shufflevector(" + Arg1.second + ", " + Arg2.second;
1684 for (auto &E : Elts) {
1685 StringRef Name = E->getName();
1686 assert_with_loc(Name.startswith("sv"),
1687 "Incorrect element kind in shuffle mask!");
1688 S += ", " + Name.drop_front(2).str();
1689 }
1690 S += ")";
1691
1692 // Recalculate the return type - the shuffle may have halved or doubled it.
1693 Type T(Arg1.first);
1694 if (Elts.size() > T.getNumElements()) {
1695 assert_with_loc(
1696 Elts.size() == T.getNumElements() * 2,
1697 "Can only double or half the number of elements in a shuffle!");
1698 T.doubleLanes();
1699 } else if (Elts.size() < T.getNumElements()) {
1700 assert_with_loc(
1701 Elts.size() == T.getNumElements() / 2,
1702 "Can only double or half the number of elements in a shuffle!");
1703 T.halveLanes();
1704 }
1705
1706 return std::make_pair(T, S);
1707}
1708
James Molloyb452f782014-06-27 11:53:35 +00001709std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagDup(DagInit *DI) {
James Molloydee4ab02014-06-17 13:11:27 +00001710 assert_with_loc(DI->getNumArgs() == 1, "dup() expects one argument");
Matthias Braunf1b01992016-12-05 06:00:51 +00001711 std::pair<Type, std::string> A = emitDagArg(DI->getArg(0),
1712 DI->getArgNameStr(0));
James Molloydee4ab02014-06-17 13:11:27 +00001713 assert_with_loc(A.first.isScalar(), "dup() expects a scalar argument");
1714
James Molloyb452f782014-06-27 11:53:35 +00001715 Type T = Intr.getBaseType();
James Molloydee4ab02014-06-17 13:11:27 +00001716 assert_with_loc(T.isVector(), "dup() used but default type is scalar!");
1717 std::string S = "(" + T.str() + ") {";
1718 for (unsigned I = 0; I < T.getNumElements(); ++I) {
1719 if (I != 0)
1720 S += ", ";
1721 S += A.second;
1722 }
1723 S += "}";
1724
1725 return std::make_pair(T, S);
1726}
1727
James Molloyb452f782014-06-27 11:53:35 +00001728std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSplat(DagInit *DI) {
James Molloydee4ab02014-06-17 13:11:27 +00001729 assert_with_loc(DI->getNumArgs() == 2, "splat() expects two arguments");
Matthias Braunf1b01992016-12-05 06:00:51 +00001730 std::pair<Type, std::string> A = emitDagArg(DI->getArg(0),
1731 DI->getArgNameStr(0));
1732 std::pair<Type, std::string> B = emitDagArg(DI->getArg(1),
1733 DI->getArgNameStr(1));
James Molloydee4ab02014-06-17 13:11:27 +00001734
1735 assert_with_loc(B.first.isScalar(),
1736 "splat() requires a scalar int as the second argument");
1737
1738 std::string S = "__builtin_shufflevector(" + A.second + ", " + A.second;
James Molloyb452f782014-06-27 11:53:35 +00001739 for (unsigned I = 0; I < Intr.getBaseType().getNumElements(); ++I) {
James Molloydee4ab02014-06-17 13:11:27 +00001740 S += ", " + B.second;
1741 }
1742 S += ")";
1743
James Molloyb452f782014-06-27 11:53:35 +00001744 return std::make_pair(Intr.getBaseType(), S);
James Molloydee4ab02014-06-17 13:11:27 +00001745}
1746
James Molloyb452f782014-06-27 11:53:35 +00001747std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagSaveTemp(DagInit *DI) {
James Molloydee4ab02014-06-17 13:11:27 +00001748 assert_with_loc(DI->getNumArgs() == 2, "save_temp() expects two arguments");
Matthias Braunf1b01992016-12-05 06:00:51 +00001749 std::pair<Type, std::string> A = emitDagArg(DI->getArg(1),
1750 DI->getArgNameStr(1));
James Molloydee4ab02014-06-17 13:11:27 +00001751
1752 assert_with_loc(!A.first.isVoid(),
1753 "Argument to save_temp() must have non-void type!");
1754
Matthias Braunf1b01992016-12-05 06:00:51 +00001755 std::string N = DI->getArgNameStr(0);
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001756 assert_with_loc(!N.empty(),
1757 "save_temp() expects a name as the first argument");
James Molloydee4ab02014-06-17 13:11:27 +00001758
James Molloyb452f782014-06-27 11:53:35 +00001759 assert_with_loc(Intr.Variables.find(N) == Intr.Variables.end(),
James Molloydee4ab02014-06-17 13:11:27 +00001760 "Variable already defined!");
James Molloyb452f782014-06-27 11:53:35 +00001761 Intr.Variables[N] = Variable(A.first, N + Intr.VariablePostfix);
James Molloydee4ab02014-06-17 13:11:27 +00001762
1763 std::string S =
James Molloyb452f782014-06-27 11:53:35 +00001764 A.first.str() + " " + Intr.Variables[N].getName() + " = " + A.second;
James Molloydee4ab02014-06-17 13:11:27 +00001765
1766 return std::make_pair(Type::getVoid(), S);
1767}
1768
James Molloyb452f782014-06-27 11:53:35 +00001769std::pair<Type, std::string>
1770Intrinsic::DagEmitter::emitDagNameReplace(DagInit *DI) {
1771 std::string S = Intr.Name;
James Molloydee4ab02014-06-17 13:11:27 +00001772
1773 assert_with_loc(DI->getNumArgs() == 2, "name_replace requires 2 arguments!");
1774 std::string ToReplace = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
1775 std::string ReplaceWith = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
1776
1777 size_t Idx = S.find(ToReplace);
1778
1779 assert_with_loc(Idx != std::string::npos, "name should contain '" + ToReplace + "'!");
1780 S.replace(Idx, ToReplace.size(), ReplaceWith);
1781
1782 return std::make_pair(Type::getVoid(), S);
1783}
1784
James Molloyb452f782014-06-27 11:53:35 +00001785std::pair<Type, std::string> Intrinsic::DagEmitter::emitDagLiteral(DagInit *DI){
James Molloydee4ab02014-06-17 13:11:27 +00001786 std::string Ty = cast<StringInit>(DI->getArg(0))->getAsUnquotedString();
1787 std::string Value = cast<StringInit>(DI->getArg(1))->getAsUnquotedString();
1788 return std::make_pair(Type::fromTypedefName(Ty), Value);
1789}
1790
James Molloyb452f782014-06-27 11:53:35 +00001791std::pair<Type, std::string>
1792Intrinsic::DagEmitter::emitDagArg(Init *Arg, std::string ArgName) {
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001793 if (!ArgName.empty()) {
James Molloydee4ab02014-06-17 13:11:27 +00001794 assert_with_loc(!Arg->isComplete(),
1795 "Arguments must either be DAGs or names, not both!");
James Molloyb452f782014-06-27 11:53:35 +00001796 assert_with_loc(Intr.Variables.find(ArgName) != Intr.Variables.end(),
James Molloydee4ab02014-06-17 13:11:27 +00001797 "Variable not defined!");
James Molloyb452f782014-06-27 11:53:35 +00001798 Variable &V = Intr.Variables[ArgName];
James Molloydee4ab02014-06-17 13:11:27 +00001799 return std::make_pair(V.getType(), V.getName());
1800 }
1801
1802 assert(Arg && "Neither ArgName nor Arg?!");
1803 DagInit *DI = dyn_cast<DagInit>(Arg);
1804 assert_with_loc(DI, "Arguments must either be DAGs or names!");
1805
1806 return emitDag(DI);
1807}
1808
1809std::string Intrinsic::generate() {
James Molloyb452f782014-06-27 11:53:35 +00001810 // Little endian intrinsics are simple and don't require any argument
1811 // swapping.
1812 OS << "#ifdef __LITTLE_ENDIAN__\n";
1813
1814 generateImpl(false, "", "");
1815
1816 OS << "#else\n";
1817
1818 // Big endian intrinsics are more complex. The user intended these
1819 // intrinsics to operate on a vector "as-if" loaded by (V)LDR,
1820 // but we load as-if (V)LD1. So we should swap all arguments and
1821 // swap the return value too.
1822 //
1823 // If we call sub-intrinsics, we should call a version that does
1824 // not re-swap the arguments!
1825 generateImpl(true, "", "__noswap_");
1826
1827 // If we're needed early, create a non-swapping variant for
1828 // big-endian.
1829 if (NeededEarly) {
1830 generateImpl(false, "__noswap_", "__noswap_");
1831 }
1832 OS << "#endif\n\n";
1833
1834 return OS.str();
1835}
1836
1837void Intrinsic::generateImpl(bool ReverseArguments,
1838 StringRef NamePrefix, StringRef CallPrefix) {
James Molloydee4ab02014-06-17 13:11:27 +00001839 CurrentRecord = R;
1840
1841 // If we call a macro, our local variables may be corrupted due to
1842 // lack of proper lexical scoping. So, add a globally unique postfix
1843 // to every variable.
1844 //
1845 // indexBody() should have set up the Dependencies set by now.
1846 for (auto *I : Dependencies)
1847 if (I->UseMacro) {
1848 VariablePostfix = "_" + utostr(Emitter.getUniqueNumber());
1849 break;
1850 }
1851
1852 initVariables();
1853
James Molloyb452f782014-06-27 11:53:35 +00001854 emitPrototype(NamePrefix);
James Molloydee4ab02014-06-17 13:11:27 +00001855
1856 if (IsUnavailable) {
1857 OS << " __attribute__((unavailable));";
1858 } else {
1859 emitOpeningBrace();
1860 emitShadowedArgs();
James Molloyb452f782014-06-27 11:53:35 +00001861 if (ReverseArguments)
1862 emitArgumentReversal();
1863 emitBody(CallPrefix);
1864 if (ReverseArguments)
1865 emitReturnReversal();
James Molloydee4ab02014-06-17 13:11:27 +00001866 emitReturn();
1867 emitClosingBrace();
1868 }
1869 OS << "\n";
1870
1871 CurrentRecord = nullptr;
James Molloydee4ab02014-06-17 13:11:27 +00001872}
1873
1874void Intrinsic::indexBody() {
1875 CurrentRecord = R;
1876
1877 initVariables();
James Molloyb452f782014-06-27 11:53:35 +00001878 emitBody("");
James Molloydee4ab02014-06-17 13:11:27 +00001879 OS.str("");
1880
1881 CurrentRecord = nullptr;
1882}
1883
1884//===----------------------------------------------------------------------===//
1885// NeonEmitter implementation
1886//===----------------------------------------------------------------------===//
1887
David Blaikie4c96a5e2015-08-06 18:29:32 +00001888Intrinsic &NeonEmitter::getIntrinsic(StringRef Name, ArrayRef<Type> Types) {
James Molloydee4ab02014-06-17 13:11:27 +00001889 // First, look up the name in the intrinsic map.
1890 assert_with_loc(IntrinsicMap.find(Name.str()) != IntrinsicMap.end(),
1891 ("Intrinsic '" + Name + "' not found!").str());
David Blaikie4c96a5e2015-08-06 18:29:32 +00001892 auto &V = IntrinsicMap.find(Name.str())->second;
James Molloydee4ab02014-06-17 13:11:27 +00001893 std::vector<Intrinsic *> GoodVec;
1894
1895 // Create a string to print if we end up failing.
1896 std::string ErrMsg = "looking up intrinsic '" + Name.str() + "(";
1897 for (unsigned I = 0; I < Types.size(); ++I) {
1898 if (I != 0)
1899 ErrMsg += ", ";
1900 ErrMsg += Types[I].str();
1901 }
1902 ErrMsg += ")'\n";
1903 ErrMsg += "Available overloads:\n";
1904
1905 // Now, look through each intrinsic implementation and see if the types are
1906 // compatible.
David Blaikie4c96a5e2015-08-06 18:29:32 +00001907 for (auto &I : V) {
1908 ErrMsg += " - " + I.getReturnType().str() + " " + I.getMangledName();
James Molloydee4ab02014-06-17 13:11:27 +00001909 ErrMsg += "(";
David Blaikie4c96a5e2015-08-06 18:29:32 +00001910 for (unsigned A = 0; A < I.getNumParams(); ++A) {
James Molloydee4ab02014-06-17 13:11:27 +00001911 if (A != 0)
1912 ErrMsg += ", ";
David Blaikie4c96a5e2015-08-06 18:29:32 +00001913 ErrMsg += I.getParamType(A).str();
James Molloydee4ab02014-06-17 13:11:27 +00001914 }
1915 ErrMsg += ")\n";
1916
David Blaikie4c96a5e2015-08-06 18:29:32 +00001917 if (I.getNumParams() != Types.size())
James Molloydee4ab02014-06-17 13:11:27 +00001918 continue;
1919
1920 bool Good = true;
1921 for (unsigned Arg = 0; Arg < Types.size(); ++Arg) {
David Blaikie4c96a5e2015-08-06 18:29:32 +00001922 if (I.getParamType(Arg) != Types[Arg]) {
James Molloydee4ab02014-06-17 13:11:27 +00001923 Good = false;
1924 break;
1925 }
1926 }
1927 if (Good)
David Blaikie4c96a5e2015-08-06 18:29:32 +00001928 GoodVec.push_back(&I);
James Molloydee4ab02014-06-17 13:11:27 +00001929 }
1930
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00001931 assert_with_loc(!GoodVec.empty(),
James Molloydee4ab02014-06-17 13:11:27 +00001932 "No compatible intrinsic found - " + ErrMsg);
1933 assert_with_loc(GoodVec.size() == 1, "Multiple overloads found - " + ErrMsg);
1934
David Blaikie4c96a5e2015-08-06 18:29:32 +00001935 return *GoodVec.front();
James Molloydee4ab02014-06-17 13:11:27 +00001936}
1937
1938void NeonEmitter::createIntrinsic(Record *R,
1939 SmallVectorImpl<Intrinsic *> &Out) {
1940 std::string Name = R->getValueAsString("Name");
1941 std::string Proto = R->getValueAsString("Prototype");
1942 std::string Types = R->getValueAsString("Types");
1943 Record *OperationRec = R->getValueAsDef("Operation");
1944 bool CartesianProductOfTypes = R->getValueAsBit("CartesianProductOfTypes");
James Molloyb452f782014-06-27 11:53:35 +00001945 bool BigEndianSafe = R->getValueAsBit("BigEndianSafe");
James Molloydee4ab02014-06-17 13:11:27 +00001946 std::string Guard = R->getValueAsString("ArchGuard");
1947 bool IsUnavailable = OperationRec->getValueAsBit("Unavailable");
1948
1949 // Set the global current record. This allows assert_with_loc to produce
1950 // decent location information even when highly nested.
1951 CurrentRecord = R;
1952
1953 ListInit *Body = OperationRec->getValueAsListInit("Ops");
1954
1955 std::vector<TypeSpec> TypeSpecs = TypeSpec::fromTypeSpecs(Types);
1956
1957 ClassKind CK = ClassNone;
1958 if (R->getSuperClasses().size() >= 2)
Craig Topper25761242016-01-18 19:52:54 +00001959 CK = ClassMap[R->getSuperClasses()[1].first];
James Molloydee4ab02014-06-17 13:11:27 +00001960
1961 std::vector<std::pair<TypeSpec, TypeSpec>> NewTypeSpecs;
1962 for (auto TS : TypeSpecs) {
1963 if (CartesianProductOfTypes) {
1964 Type DefaultT(TS, 'd');
1965 for (auto SrcTS : TypeSpecs) {
1966 Type DefaultSrcT(SrcTS, 'd');
1967 if (TS == SrcTS ||
1968 DefaultSrcT.getSizeInBits() != DefaultT.getSizeInBits())
1969 continue;
1970 NewTypeSpecs.push_back(std::make_pair(TS, SrcTS));
1971 }
1972 } else {
1973 NewTypeSpecs.push_back(std::make_pair(TS, TS));
1974 }
1975 }
1976
1977 std::sort(NewTypeSpecs.begin(), NewTypeSpecs.end());
James Dennettfa245492015-04-06 21:09:24 +00001978 NewTypeSpecs.erase(std::unique(NewTypeSpecs.begin(), NewTypeSpecs.end()),
1979 NewTypeSpecs.end());
David Blaikie4c96a5e2015-08-06 18:29:32 +00001980 auto &Entry = IntrinsicMap[Name];
James Molloydee4ab02014-06-17 13:11:27 +00001981
1982 for (auto &I : NewTypeSpecs) {
David Blaikie4c96a5e2015-08-06 18:29:32 +00001983 Entry.emplace_back(R, Name, Proto, I.first, I.second, CK, Body, *this,
1984 Guard, IsUnavailable, BigEndianSafe);
1985 Out.push_back(&Entry.back());
James Molloydee4ab02014-06-17 13:11:27 +00001986 }
1987
1988 CurrentRecord = nullptr;
1989}
1990
1991/// genBuiltinsDef: Generate the BuiltinsARM.def and BuiltinsAArch64.def
1992/// declaration of builtins, checking for unique builtin declarations.
1993void NeonEmitter::genBuiltinsDef(raw_ostream &OS,
1994 SmallVectorImpl<Intrinsic *> &Defs) {
1995 OS << "#ifdef GET_NEON_BUILTINS\n";
1996
1997 // We only want to emit a builtin once, and we want to emit them in
1998 // alphabetical order, so use a std::set.
1999 std::set<std::string> Builtins;
2000
2001 for (auto *Def : Defs) {
2002 if (Def->hasBody())
2003 continue;
2004 // Functions with 'a' (the splat code) in the type prototype should not get
2005 // their own builtin as they use the non-splat variant.
2006 if (Def->hasSplat())
2007 continue;
2008
2009 std::string S = "BUILTIN(__builtin_neon_" + Def->getMangledName() + ", \"";
2010
2011 S += Def->getBuiltinTypeStr();
2012 S += "\", \"n\")";
2013
2014 Builtins.insert(S);
2015 }
2016
2017 for (auto &S : Builtins)
2018 OS << S << "\n";
2019 OS << "#endif\n\n";
2020}
2021
2022/// Generate the ARM and AArch64 overloaded type checking code for
2023/// SemaChecking.cpp, checking for unique builtin declarations.
2024void NeonEmitter::genOverloadTypeCheckCode(raw_ostream &OS,
2025 SmallVectorImpl<Intrinsic *> &Defs) {
2026 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
2027
2028 // We record each overload check line before emitting because subsequent Inst
2029 // definitions may extend the number of permitted types (i.e. augment the
2030 // Mask). Use std::map to avoid sorting the table by hash number.
2031 struct OverloadInfo {
2032 uint64_t Mask;
2033 int PtrArgNum;
2034 bool HasConstPtr;
2035 OverloadInfo() : Mask(0ULL), PtrArgNum(0), HasConstPtr(false) {}
2036 };
2037 std::map<std::string, OverloadInfo> OverloadMap;
2038
2039 for (auto *Def : Defs) {
2040 // If the def has a body (that is, it has Operation DAGs), it won't call
2041 // __builtin_neon_* so we don't need to generate a definition for it.
2042 if (Def->hasBody())
2043 continue;
2044 // Functions with 'a' (the splat code) in the type prototype should not get
2045 // their own builtin as they use the non-splat variant.
2046 if (Def->hasSplat())
2047 continue;
2048 // Functions which have a scalar argument cannot be overloaded, no need to
2049 // check them if we are emitting the type checking code.
2050 if (Def->protoHasScalar())
2051 continue;
2052
2053 uint64_t Mask = 0ULL;
2054 Type Ty = Def->getReturnType();
Ahmed Bougacha22a16962015-08-21 23:24:18 +00002055 if (Def->getProto()[0] == 'v' ||
2056 isFloatingPointProtoModifier(Def->getProto()[0]))
James Molloydee4ab02014-06-17 13:11:27 +00002057 Ty = Def->getParamType(0);
2058 if (Ty.isPointer())
2059 Ty = Def->getParamType(1);
2060
2061 Mask |= 1ULL << Ty.getNeonEnum();
2062
2063 // Check if the function has a pointer or const pointer argument.
2064 std::string Proto = Def->getProto();
2065 int PtrArgNum = -1;
2066 bool HasConstPtr = false;
2067 for (unsigned I = 0; I < Def->getNumParams(); ++I) {
2068 char ArgType = Proto[I + 1];
2069 if (ArgType == 'c') {
2070 HasConstPtr = true;
2071 PtrArgNum = I;
2072 break;
2073 }
2074 if (ArgType == 'p') {
2075 PtrArgNum = I;
2076 break;
2077 }
2078 }
2079 // For sret builtins, adjust the pointer argument index.
2080 if (PtrArgNum >= 0 && Def->getReturnType().getNumVectors() > 1)
2081 PtrArgNum += 1;
2082
2083 std::string Name = Def->getName();
2084 // Omit type checking for the pointer arguments of vld1_lane, vld1_dup,
2085 // and vst1_lane intrinsics. Using a pointer to the vector element
2086 // type with one of those operations causes codegen to select an aligned
2087 // load/store instruction. If you want an unaligned operation,
2088 // the pointer argument needs to have less alignment than element type,
2089 // so just accept any pointer type.
2090 if (Name == "vld1_lane" || Name == "vld1_dup" || Name == "vst1_lane") {
2091 PtrArgNum = -1;
2092 HasConstPtr = false;
2093 }
2094
2095 if (Mask) {
2096 std::string Name = Def->getMangledName();
2097 OverloadMap.insert(std::make_pair(Name, OverloadInfo()));
2098 OverloadInfo &OI = OverloadMap[Name];
2099 OI.Mask |= Mask;
2100 OI.PtrArgNum |= PtrArgNum;
2101 OI.HasConstPtr = HasConstPtr;
2102 }
2103 }
2104
2105 for (auto &I : OverloadMap) {
2106 OverloadInfo &OI = I.second;
2107
2108 OS << "case NEON::BI__builtin_neon_" << I.first << ": ";
2109 OS << "mask = 0x" << utohexstr(OI.Mask) << "ULL";
2110 if (OI.PtrArgNum >= 0)
2111 OS << "; PtrArgNum = " << OI.PtrArgNum;
2112 if (OI.HasConstPtr)
2113 OS << "; HasConstPtr = true";
2114 OS << "; break;\n";
2115 }
2116 OS << "#endif\n\n";
2117}
2118
2119void
2120NeonEmitter::genIntrinsicRangeCheckCode(raw_ostream &OS,
2121 SmallVectorImpl<Intrinsic *> &Defs) {
2122 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
2123
2124 std::set<std::string> Emitted;
2125
2126 for (auto *Def : Defs) {
2127 if (Def->hasBody())
2128 continue;
2129 // Functions with 'a' (the splat code) in the type prototype should not get
2130 // their own builtin as they use the non-splat variant.
2131 if (Def->hasSplat())
2132 continue;
Alp Toker958027b2014-07-14 19:42:55 +00002133 // Functions which do not have an immediate do not need to have range
2134 // checking code emitted.
James Molloydee4ab02014-06-17 13:11:27 +00002135 if (!Def->hasImmediate())
2136 continue;
2137 if (Emitted.find(Def->getMangledName()) != Emitted.end())
2138 continue;
2139
2140 std::string LowerBound, UpperBound;
2141
2142 Record *R = Def->getRecord();
2143 if (R->getValueAsBit("isVCVT_N")) {
2144 // VCVT between floating- and fixed-point values takes an immediate
2145 // in the range [1, 32) for f32 or [1, 64) for f64.
2146 LowerBound = "1";
2147 if (Def->getBaseType().getElementSizeInBits() == 32)
2148 UpperBound = "31";
2149 else
2150 UpperBound = "63";
2151 } else if (R->getValueAsBit("isScalarShift")) {
2152 // Right shifts have an 'r' in the name, left shifts do not. Convert
2153 // instructions have the same bounds and right shifts.
2154 if (Def->getName().find('r') != std::string::npos ||
2155 Def->getName().find("cvt") != std::string::npos)
2156 LowerBound = "1";
2157
2158 UpperBound = utostr(Def->getReturnType().getElementSizeInBits() - 1);
2159 } else if (R->getValueAsBit("isShift")) {
Alp Toker958027b2014-07-14 19:42:55 +00002160 // Builtins which are overloaded by type will need to have their upper
James Molloydee4ab02014-06-17 13:11:27 +00002161 // bound computed at Sema time based on the type constant.
2162
2163 // Right shifts have an 'r' in the name, left shifts do not.
2164 if (Def->getName().find('r') != std::string::npos)
2165 LowerBound = "1";
2166 UpperBound = "RFT(TV, true)";
2167 } else if (Def->getClassKind(true) == ClassB) {
2168 // ClassB intrinsics have a type (and hence lane number) that is only
2169 // known at runtime.
2170 if (R->getValueAsBit("isLaneQ"))
2171 UpperBound = "RFT(TV, false, true)";
2172 else
2173 UpperBound = "RFT(TV, false, false)";
2174 } else {
2175 // The immediate generally refers to a lane in the preceding argument.
2176 assert(Def->getImmediateIdx() > 0);
2177 Type T = Def->getParamType(Def->getImmediateIdx() - 1);
2178 UpperBound = utostr(T.getNumElements() - 1);
2179 }
2180
2181 // Calculate the index of the immediate that should be range checked.
2182 unsigned Idx = Def->getNumParams();
2183 if (Def->hasImmediate())
2184 Idx = Def->getGeneratedParamIdx(Def->getImmediateIdx());
2185
2186 OS << "case NEON::BI__builtin_neon_" << Def->getMangledName() << ": "
2187 << "i = " << Idx << ";";
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002188 if (!LowerBound.empty())
James Molloydee4ab02014-06-17 13:11:27 +00002189 OS << " l = " << LowerBound << ";";
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002190 if (!UpperBound.empty())
James Molloydee4ab02014-06-17 13:11:27 +00002191 OS << " u = " << UpperBound << ";";
2192 OS << " break;\n";
2193
2194 Emitted.insert(Def->getMangledName());
2195 }
2196
2197 OS << "#endif\n\n";
2198}
2199
2200/// runHeader - Emit a file with sections defining:
2201/// 1. the NEON section of BuiltinsARM.def and BuiltinsAArch64.def.
2202/// 2. the SemaChecking code for the type overload checking.
2203/// 3. the SemaChecking code for validation of intrinsic immediate arguments.
2204void NeonEmitter::runHeader(raw_ostream &OS) {
2205 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
2206
2207 SmallVector<Intrinsic *, 128> Defs;
2208 for (auto *R : RV)
2209 createIntrinsic(R, Defs);
2210
2211 // Generate shared BuiltinsXXX.def
2212 genBuiltinsDef(OS, Defs);
2213
2214 // Generate ARM overloaded type checking code for SemaChecking.cpp
2215 genOverloadTypeCheckCode(OS, Defs);
2216
2217 // Generate ARM range checking code for shift/lane immediates.
2218 genIntrinsicRangeCheckCode(OS, Defs);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002219}
2220
2221/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
2222/// is comprised of type definitions and function declarations.
2223void NeonEmitter::run(raw_ostream &OS) {
James Molloydee4ab02014-06-17 13:11:27 +00002224 OS << "/*===---- arm_neon.h - ARM Neon intrinsics "
2225 "------------------------------"
2226 "---===\n"
2227 " *\n"
2228 " * Permission is hereby granted, free of charge, to any person "
2229 "obtaining "
2230 "a copy\n"
2231 " * of this software and associated documentation files (the "
2232 "\"Software\"),"
2233 " to deal\n"
2234 " * in the Software without restriction, including without limitation "
2235 "the "
2236 "rights\n"
2237 " * to use, copy, modify, merge, publish, distribute, sublicense, "
2238 "and/or sell\n"
2239 " * copies of the Software, and to permit persons to whom the Software "
2240 "is\n"
2241 " * furnished to do so, subject to the following conditions:\n"
2242 " *\n"
2243 " * The above copyright notice and this permission notice shall be "
2244 "included in\n"
2245 " * all copies or substantial portions of the Software.\n"
2246 " *\n"
2247 " * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, "
2248 "EXPRESS OR\n"
2249 " * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF "
2250 "MERCHANTABILITY,\n"
2251 " * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT "
2252 "SHALL THE\n"
2253 " * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR "
2254 "OTHER\n"
2255 " * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, "
2256 "ARISING FROM,\n"
2257 " * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER "
2258 "DEALINGS IN\n"
2259 " * THE SOFTWARE.\n"
2260 " *\n"
2261 " *===-----------------------------------------------------------------"
2262 "---"
2263 "---===\n"
2264 " */\n\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002265
2266 OS << "#ifndef __ARM_NEON_H\n";
2267 OS << "#define __ARM_NEON_H\n\n";
2268
Tim Northover5bb34ca2013-11-21 12:36:34 +00002269 OS << "#if !defined(__ARM_NEON)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002270 OS << "#error \"NEON support not enabled\"\n";
2271 OS << "#endif\n\n";
2272
2273 OS << "#include <stdint.h>\n\n";
2274
2275 // Emit NEON-specific scalar typedefs.
2276 OS << "typedef float float32_t;\n";
Tim Northover2fe823a2013-08-01 09:23:19 +00002277 OS << "typedef __fp16 float16_t;\n";
2278
2279 OS << "#ifdef __aarch64__\n";
2280 OS << "typedef double float64_t;\n";
2281 OS << "#endif\n\n";
2282
2283 // For now, signedness of polynomial types depends on target
2284 OS << "#ifdef __aarch64__\n";
2285 OS << "typedef uint8_t poly8_t;\n";
2286 OS << "typedef uint16_t poly16_t;\n";
Kevin Qincaac85e2013-11-14 03:29:16 +00002287 OS << "typedef uint64_t poly64_t;\n";
Kevin Qinfb79d7f2013-12-10 06:49:01 +00002288 OS << "typedef __uint128_t poly128_t;\n";
Tim Northover2fe823a2013-08-01 09:23:19 +00002289 OS << "#else\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002290 OS << "typedef int8_t poly8_t;\n";
2291 OS << "typedef int16_t poly16_t;\n";
Tim Northover2fe823a2013-08-01 09:23:19 +00002292 OS << "#endif\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002293
2294 // Emit Neon vector typedefs.
Tim Northover2fe823a2013-08-01 09:23:19 +00002295 std::string TypedefTypes(
Kevin Qincaac85e2013-11-14 03:29:16 +00002296 "cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfdQdPcQPcPsQPsPlQPl");
James Molloydee4ab02014-06-17 13:11:27 +00002297 std::vector<TypeSpec> TDTypeVec = TypeSpec::fromTypeSpecs(TypedefTypes);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002298
2299 // Emit vector typedefs.
James Molloydee4ab02014-06-17 13:11:27 +00002300 bool InIfdef = false;
2301 for (auto &TS : TDTypeVec) {
2302 bool IsA64 = false;
2303 Type T(TS, 'd');
2304 if (T.isDouble() || (T.isPoly() && T.isLong()))
2305 IsA64 = true;
Tim Northover2fe823a2013-08-01 09:23:19 +00002306
James Molloydee4ab02014-06-17 13:11:27 +00002307 if (InIfdef && !IsA64) {
Jiangning Liu4617e9d2013-10-04 09:21:17 +00002308 OS << "#endif\n";
James Molloydee4ab02014-06-17 13:11:27 +00002309 InIfdef = false;
2310 }
2311 if (!InIfdef && IsA64) {
Tim Northover2fe823a2013-08-01 09:23:19 +00002312 OS << "#ifdef __aarch64__\n";
James Molloydee4ab02014-06-17 13:11:27 +00002313 InIfdef = true;
2314 }
Tim Northover2fe823a2013-08-01 09:23:19 +00002315
James Molloydee4ab02014-06-17 13:11:27 +00002316 if (T.isPoly())
Peter Collingbournebee583f2011-10-06 13:03:08 +00002317 OS << "typedef __attribute__((neon_polyvector_type(";
2318 else
2319 OS << "typedef __attribute__((neon_vector_type(";
2320
James Molloydee4ab02014-06-17 13:11:27 +00002321 Type T2 = T;
2322 T2.makeScalar();
2323 OS << utostr(T.getNumElements()) << "))) ";
2324 OS << T2.str();
2325 OS << " " << T.str() << ";\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002326 }
James Molloydee4ab02014-06-17 13:11:27 +00002327 if (InIfdef)
Kevin Qincaac85e2013-11-14 03:29:16 +00002328 OS << "#endif\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002329 OS << "\n";
2330
2331 // Emit struct typedefs.
James Molloydee4ab02014-06-17 13:11:27 +00002332 InIfdef = false;
2333 for (unsigned NumMembers = 2; NumMembers <= 4; ++NumMembers) {
2334 for (auto &TS : TDTypeVec) {
2335 bool IsA64 = false;
2336 Type T(TS, 'd');
2337 if (T.isDouble() || (T.isPoly() && T.isLong()))
2338 IsA64 = true;
Tim Northover2fe823a2013-08-01 09:23:19 +00002339
James Molloydee4ab02014-06-17 13:11:27 +00002340 if (InIfdef && !IsA64) {
Jiangning Liu4617e9d2013-10-04 09:21:17 +00002341 OS << "#endif\n";
James Molloydee4ab02014-06-17 13:11:27 +00002342 InIfdef = false;
2343 }
2344 if (!InIfdef && IsA64) {
Tim Northover2fe823a2013-08-01 09:23:19 +00002345 OS << "#ifdef __aarch64__\n";
James Molloydee4ab02014-06-17 13:11:27 +00002346 InIfdef = true;
2347 }
Tim Northover2fe823a2013-08-01 09:23:19 +00002348
James Molloydee4ab02014-06-17 13:11:27 +00002349 char M = '2' + (NumMembers - 2);
2350 Type VT(TS, M);
2351 OS << "typedef struct " << VT.str() << " {\n";
2352 OS << " " << T.str() << " val";
2353 OS << "[" << utostr(NumMembers) << "]";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002354 OS << ";\n} ";
James Molloydee4ab02014-06-17 13:11:27 +00002355 OS << VT.str() << ";\n";
Tim Northover2fe823a2013-08-01 09:23:19 +00002356 OS << "\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002357 }
2358 }
James Molloydee4ab02014-06-17 13:11:27 +00002359 if (InIfdef)
Kevin Qincaac85e2013-11-14 03:29:16 +00002360 OS << "#endif\n";
2361 OS << "\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002362
James Molloydee4ab02014-06-17 13:11:27 +00002363 OS << "#define __ai static inline __attribute__((__always_inline__, "
2364 "__nodebug__))\n\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002365
James Molloydee4ab02014-06-17 13:11:27 +00002366 SmallVector<Intrinsic *, 128> Defs;
2367 std::vector<Record *> RV = Records.getAllDerivedDefinitions("Inst");
2368 for (auto *R : RV)
2369 createIntrinsic(R, Defs);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002370
James Molloydee4ab02014-06-17 13:11:27 +00002371 for (auto *I : Defs)
2372 I->indexBody();
Tim Northover2fe823a2013-08-01 09:23:19 +00002373
James Molloydee4ab02014-06-17 13:11:27 +00002374 std::stable_sort(
2375 Defs.begin(), Defs.end(),
2376 [](const Intrinsic *A, const Intrinsic *B) { return *A < *B; });
Tim Northover2fe823a2013-08-01 09:23:19 +00002377
James Molloydee4ab02014-06-17 13:11:27 +00002378 // Only emit a def when its requirements have been met.
2379 // FIXME: This loop could be made faster, but it's fast enough for now.
2380 bool MadeProgress = true;
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002381 std::string InGuard;
James Molloydee4ab02014-06-17 13:11:27 +00002382 while (!Defs.empty() && MadeProgress) {
2383 MadeProgress = false;
2384
2385 for (SmallVector<Intrinsic *, 128>::iterator I = Defs.begin();
2386 I != Defs.end(); /*No step*/) {
2387 bool DependenciesSatisfied = true;
2388 for (auto *II : (*I)->getDependencies()) {
2389 if (std::find(Defs.begin(), Defs.end(), II) != Defs.end())
2390 DependenciesSatisfied = false;
2391 }
2392 if (!DependenciesSatisfied) {
2393 // Try the next one.
2394 ++I;
2395 continue;
2396 }
2397
2398 // Emit #endif/#if pair if needed.
2399 if ((*I)->getGuard() != InGuard) {
2400 if (!InGuard.empty())
2401 OS << "#endif\n";
2402 InGuard = (*I)->getGuard();
2403 if (!InGuard.empty())
2404 OS << "#if " << InGuard << "\n";
2405 }
2406
2407 // Actually generate the intrinsic code.
2408 OS << (*I)->generate();
2409
2410 MadeProgress = true;
2411 I = Defs.erase(I);
2412 }
Tim Northover2fe823a2013-08-01 09:23:19 +00002413 }
James Molloydee4ab02014-06-17 13:11:27 +00002414 assert(Defs.empty() && "Some requirements were not satisfied!");
2415 if (!InGuard.empty())
2416 OS << "#endif\n";
Tim Northover2fe823a2013-08-01 09:23:19 +00002417
James Molloydee4ab02014-06-17 13:11:27 +00002418 OS << "\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002419 OS << "#undef __ai\n\n";
2420 OS << "#endif /* __ARM_NEON_H */\n";
2421}
2422
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002423namespace clang {
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002424
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002425void EmitNeon(RecordKeeper &Records, raw_ostream &OS) {
2426 NeonEmitter(Records).run(OS);
2427}
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002428
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002429void EmitNeonSema(RecordKeeper &Records, raw_ostream &OS) {
2430 NeonEmitter(Records).runHeader(OS);
2431}
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002432
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002433void EmitNeonTest(RecordKeeper &Records, raw_ostream &OS) {
Craig Topper0039f3f2014-06-18 03:57:25 +00002434 llvm_unreachable("Neon test generation no longer implemented!");
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002435}
Eugene Zelenko58ab22f2016-11-29 22:44:24 +00002436
2437} // end namespace clang