1//===- CodeGenIntrinsics.cpp - Intrinsic Class Wrapper --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines a wrapper class for the 'Intrinsic' TableGen class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CodeGenIntrinsics.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringSwitch.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/FormatVariadic.h"
20#include "llvm/Support/MathExtras.h"
21#include "llvm/TableGen/Error.h"
22#include "llvm/TableGen/Record.h"
23#include <algorithm>
24#include <cassert>
25#include <limits>
26using namespace llvm;
27
28// As the type of more than one return values is represented as an anonymous
29// struct, which is encoded with `IIT_STRUCT` followed by a byte specifying
30// the number of return values, starting from 2 (encoded as 0) to 257
31// (encoded as 255). So, the maximum number of values that an intrinsic can
32// return is 257.
33static constexpr unsigned MaxNumReturn = 257;
34
35using ImmArgRange = CodeGenIntrinsic::ImmArgRangeSet::Range;
36using ImmArgRangeList = CodeGenIntrinsic::ImmArgRangeSet::RangeList;
37
38static ImmArgRange getHalfOpenRange(const Record *R, const ListInit *Range,
39 StringRef AttrName) {
40 auto GetBound = [&](const Init *Bound) -> int64_t {
41 if (const auto *II = dyn_cast<IntInit>(Val: Bound))
42 return II->getValue();
43 PrintFatalError(ErrorLoc: R->getLoc(),
44 Msg: Twine(AttrName) +
45 " bound is not an integer: " + Bound->getAsString());
46 };
47
48 int64_t Lower = GetBound(Range->getElement(Idx: 0));
49 int64_t Upper = GetBound(Range->getElement(Idx: 1));
50 if (Upper == std::numeric_limits<int64_t>::max())
51 PrintFatalError(ErrorLoc: R->getLoc(),
52 Msg: Twine(AttrName) + " closed upper bound is too large");
53 return ImmArgRange(Lower, Upper + 1);
54}
55
56static void appendHalfOpenRange(SmallVectorImpl<ImmArgRange> &Ranges,
57 ImmArgRange Range) {
58 if (!Ranges.empty()) {
59 if (Range.first == Ranges.back().second) {
60 Ranges.back().second = Range.second;
61 return;
62 }
63 }
64 Ranges.push_back(Elt: Range);
65}
66
67//===----------------------------------------------------------------------===//
68// CodeGenIntrinsic Implementation
69//===----------------------------------------------------------------------===//
70
71CodeGenIntrinsicContext::CodeGenIntrinsicContext(const RecordKeeper &RC) {
72 for (const Record *Rec : RC.getAllDerivedDefinitions(ClassName: "IntrinsicProperty"))
73 if (Rec->getValueAsBit(FieldName: "IsDefault"))
74 DefaultProperties.push_back(x: Rec);
75 NoUnwindAttr = RC.getDef(Name: "NoUnwind");
76}
77
78CodeGenIntrinsicTable::CodeGenIntrinsicTable(const RecordKeeper &RC) {
79 CodeGenIntrinsicContext Ctx(RC);
80
81 ArrayRef<const Record *> Defs = RC.getAllDerivedDefinitions(ClassName: "Intrinsic");
82 Intrinsics.reserve(n: Defs.size());
83
84 for (const Record *Def : Defs)
85 Intrinsics.emplace_back(args: CodeGenIntrinsic(Def, Ctx));
86
87 llvm::sort(C&: Intrinsics,
88 Comp: [](const CodeGenIntrinsic &LHS, const CodeGenIntrinsic &RHS) {
89 // Order target independent intrinsics before target dependent
90 // ones.
91 bool LHSHasTarget = !LHS.TargetPrefix.empty();
92 bool RHSHasTarget = !RHS.TargetPrefix.empty();
93
94 // To ensure deterministic sorted order when duplicates are
95 // present, use record ID as a tie-breaker similar to
96 // sortAndReportDuplicates in Utils.cpp.
97 unsigned LhsID = LHS.TheDef->getID();
98 unsigned RhsID = RHS.TheDef->getID();
99
100 return std::tie(args&: LHSHasTarget, args: LHS.Name, args&: LhsID) <
101 std::tie(args&: RHSHasTarget, args: RHS.Name, args&: RhsID);
102 });
103
104 Targets.push_back(x: {.Name: "", .Offset: 0, .Count: 0});
105 for (size_t I = 0, E = Intrinsics.size(); I < E; ++I)
106 if (Intrinsics[I].TargetPrefix != Targets.back().Name) {
107 Targets.back().Count = I - Targets.back().Offset;
108 Targets.push_back(x: {.Name: Intrinsics[I].TargetPrefix, .Offset: I, .Count: 0});
109 }
110 Targets.back().Count = Intrinsics.size() - Targets.back().Offset;
111
112 CheckDuplicateIntrinsics();
113 CheckTargetIndependentIntrinsics();
114 CheckOverloadSuffixConflicts();
115}
116
117// Check for duplicate intrinsic names.
118void CodeGenIntrinsicTable::CheckDuplicateIntrinsics() const {
119 // Since the Intrinsics vector is already sorted by name, if there are 2 or
120 // more intrinsics with duplicate names, they will appear adjacent in sorted
121 // order. Note that if the intrinsic name was derived from the record name
122 // there cannot be be duplicate as TableGen parser would have flagged that.
123 // However, if the name was specified in the intrinsic definition, then its
124 // possible to have duplicate names.
125 auto I = std::adjacent_find(
126 first: Intrinsics.begin(), last: Intrinsics.end(),
127 binary_pred: [](const CodeGenIntrinsic &Int1, const CodeGenIntrinsic &Int2) {
128 return Int1.Name == Int2.Name;
129 });
130 if (I == Intrinsics.end())
131 return;
132
133 // Found a duplicate intrinsics.
134 const CodeGenIntrinsic &First = *I;
135 const CodeGenIntrinsic &Second = *(I + 1);
136 PrintError(Rec: Second.TheDef,
137 Msg: Twine("Intrinsic `") + First.Name + "` is already defined");
138 PrintFatalNote(Rec: First.TheDef, Msg: "Previous definition here");
139}
140
141// For target independent intrinsics, check that their second dotted component
142// does not match any target name.
143void CodeGenIntrinsicTable::CheckTargetIndependentIntrinsics() const {
144 SmallDenseSet<StringRef> TargetNames;
145 for (const auto &Target : ArrayRef(Targets).drop_front())
146 TargetNames.insert(V: Target.Name);
147
148 // Set of target independent intrinsics.
149 const auto &Set = Targets[0];
150 for (const auto &Int : ArrayRef(&Intrinsics[Set.Offset], Set.Count)) {
151 StringRef Name = Int.Name;
152 StringRef Prefix = Name.drop_front(N: 5).split(Separator: '.').first;
153 if (!TargetNames.contains(V: Prefix))
154 continue;
155 PrintFatalError(Rec: Int.TheDef,
156 Msg: "target independent intrinsic `" + Name +
157 "' has prefix `llvm." + Prefix +
158 "` that conflicts with intrinsics for target `" +
159 Prefix + "`");
160 }
161}
162
163// Return true if the given Suffix looks like a mangled type. Note that this
164// check is conservative, but allows all existing LLVM intrinsic suffixes to be
165// considered as not looking like a mangling suffix.
166static bool doesSuffixLookLikeMangledType(StringRef Suffix) {
167 // Try to match against possible mangling suffixes for various types.
168 // See getMangledTypeStr() for the mangling suffixes possible. It includes
169 // pointer : p[0-9]+
170 // array : a[0-9]+.+
171 // struct: : s_/sl_.+
172 // function : f_.+
173 // vector : v/nxv[0-9]+.+
174 // target type : t.+
175 // integer : i[0-9]+
176 // named types : See `NamedTypes` below.
177
178 // Match anything with an _, so match function and struct types.
179 if (Suffix.contains(C: '_'))
180 return true;
181
182 // [av][0-9]+.+, simplified to [av][0-9].+
183 if (Suffix.size() >= 2 && is_contained(Range: "av", Element: Suffix[0]) && isDigit(C: Suffix[1]))
184 return true;
185
186 // nxv[0-9]+.+, simplified to nxv[0-9].+
187 if (Suffix.size() >= 4 && Suffix.starts_with(Prefix: "nxv") && isDigit(C: Suffix[3]))
188 return true;
189
190 // t.+
191 if (Suffix.size() > 1 && Suffix.starts_with(Prefix: 't'))
192 return false;
193
194 // [pi][0-9]+
195 if (Suffix.size() > 1 && is_contained(Range: "pi", Element: Suffix[0]) &&
196 all_of(Range: Suffix.drop_front(), P: isDigit))
197 return true;
198
199 // Match one of the named types.
200 static constexpr StringLiteral NamedTypes[] = {
201 "isVoid", "Metadata", "f16", "f32", "f64",
202 "f80", "f128", "bf16", "ppcf128", "x86amx"};
203 return is_contained(Range: NamedTypes, Element: Suffix);
204}
205
206// Check for conflicts with overloaded intrinsics. If there exists an overloaded
207// intrinsic with base name `llvm.target.foo`, LLVM will add a mangling suffix
208// to it to encode the overload types. This mangling suffix is 1 or more .
209// prefixed mangled type string as defined in `getMangledTypeStr`. If there
210// exists another intrinsic `llvm.target.foo[.<suffixN>]+`, which has the same
211// prefix as the overloaded intrinsic, its possible that there may be a name
212// conflict with the overloaded intrinsic and either one may interfere with name
213// lookup for the other, leading to wrong intrinsic ID being assigned.
214//
215// The actual name lookup in the intrinsic name table is done by a search
216// on each successive '.' separted component of the intrinsic name (see
217// `lookupLLVMIntrinsicByName`). Consider first the case where there exists a
218// non-overloaded intrinsic `llvm.target.foo[.suffix]+`. For the non-overloaded
219// intrinsics, the name lookup is an exact match, so the presence of the
220// overloaded intrinsic with the same prefix will not interfere with the
221// search. However, a lookup intended to match the overloaded intrinsic might be
222// affected by the presence of another entry in the name table with the same
223// prefix.
224//
225// Since LLVM's name lookup first selects the target specific (or target
226// independent) slice of the name table to look into, intrinsics in 2 different
227// targets cannot conflict with each other. Within a specific target,
228// if we have an overloaded intrinsic with name `llvm.target.foo` and another
229// one with same prefix and one or more suffixes `llvm.target.foo[.<suffixN>]+`,
230// then the name search will try to first match against suffix0, then suffix1
231// etc. If suffix0 can match a mangled type, then the search for an
232// `llvm.target.foo` with a mangling suffix can match against suffix0,
233// preventing a match with `llvm.target.foo`. If suffix0 cannot match a mangled
234// type, then that cannot happen, so we do not need to check for later suffixes.
235//
236// Generalizing, the `llvm.target.foo[.suffixN]+` will cause a conflict if the
237// first suffix (.suffix0) can match a mangled type (and then we do not need to
238// check later suffixes) and will not cause a conflict if it cannot (and then
239// again, we do not need to check for later suffixes).
240void CodeGenIntrinsicTable::CheckOverloadSuffixConflicts() const {
241 for (const TargetSet &Set : Targets) {
242 const CodeGenIntrinsic *Overloaded = nullptr;
243 for (const CodeGenIntrinsic &Int : (*this)[Set]) {
244 // If we do not have an overloaded intrinsic to check against, nothing
245 // to do except potentially identifying this as a candidate for checking
246 // against in future iteration.
247 if (!Overloaded) {
248 if (Int.isOverloaded)
249 Overloaded = &Int;
250 continue;
251 }
252
253 StringRef Name = Int.Name;
254 StringRef OverloadName = Overloaded->Name;
255 // If we have an overloaded intrinsic to check again, check if its name is
256 // a proper prefix of this intrinsic.
257 if (Name.starts_with(Prefix: OverloadName) && Name[OverloadName.size()] == '.') {
258 // If yes, verify suffixes and flag an error.
259 StringRef Suffixes = Name.drop_front(N: OverloadName.size() + 1);
260
261 // Only need to look at the first suffix.
262 StringRef Suffix0 = Suffixes.split(Separator: '.').first;
263
264 if (!doesSuffixLookLikeMangledType(Suffix: Suffix0))
265 continue;
266
267 unsigned SuffixSize = OverloadName.size() + 1 + Suffix0.size();
268 // If suffix looks like mangling suffix, flag it as an error.
269 PrintError(ErrorLoc: Int.TheDef->getLoc(),
270 Msg: "intrinsic `" + Name + "` cannot share prefix `" +
271 Name.take_front(N: SuffixSize) +
272 "` with another overloaded intrinsic `" + OverloadName +
273 "`");
274 PrintNote(NoteLoc: Overloaded->TheDef->getLoc(),
275 Msg: "Overloaded intrinsic `" + OverloadName + "` defined here");
276 continue;
277 }
278
279 // If we find an intrinsic that is not a proper prefix, any later
280 // intrinsic is also not going to be a proper prefix, so invalidate the
281 // overloaded to check against.
282 Overloaded = nullptr;
283 }
284 }
285}
286
287const CodeGenIntrinsic &CodeGenIntrinsicMap::operator[](const Record *Record) {
288 if (!Record->isSubClassOf(Name: "Intrinsic"))
289 PrintFatalError(Msg: "Intrinsic defs should be subclass of 'Intrinsic' class");
290
291 auto [Iter, Inserted] = Map.try_emplace(Key: Record);
292 if (Inserted)
293 Iter->second = std::make_unique<CodeGenIntrinsic>(args&: Record, args: Ctx);
294 return *Iter->second;
295}
296
297CodeGenIntrinsic::CodeGenIntrinsic(const Record *R,
298 const CodeGenIntrinsicContext &Ctx)
299 : TheDef(R) {
300 StringRef DefName = TheDef->getName();
301 ArrayRef<SMLoc> DefLoc = R->getLoc();
302
303 if (!DefName.starts_with(Prefix: "int_"))
304 PrintFatalError(ErrorLoc: DefLoc,
305 Msg: "Intrinsic '" + DefName + "' does not start with 'int_'!");
306
307 EnumName = DefName.substr(Start: 4);
308
309 // Ignore a missing ClangBuiltinName field.
310 ClangBuiltinName =
311 R->getValueAsOptionalString(FieldName: "ClangBuiltinName").value_or(u: "");
312 // Ignore a missing MSBuiltinName field.
313 MSBuiltinName = R->getValueAsOptionalString(FieldName: "MSBuiltinName").value_or(u: "");
314 TargetFeatures = R->getValueAsString(FieldName: "TargetFeatures");
315
316 TargetPrefix = R->getValueAsString(FieldName: "TargetPrefix");
317 Name = R->getValueAsString(FieldName: "LLVMName").str();
318
319 std::string DefaultName = "llvm." + EnumName.str();
320 llvm::replace(Range&: DefaultName, OldValue: '_', NewValue: '.');
321
322 if (Name == "") {
323 // If an explicit name isn't specified, derive one from the DefName.
324 Name = std::move(DefaultName);
325 } else {
326 // Verify it starts with "llvm.".
327 if (!StringRef(Name).starts_with(Prefix: "llvm."))
328 PrintFatalError(ErrorLoc: DefLoc, Msg: "Intrinsic '" + DefName +
329 "'s name does not start with 'llvm.'!");
330
331 if (Name == DefaultName)
332 PrintNote(NoteLoc: DefLoc, Msg: "Explicitly specified name matches default name, "
333 "consider dropping it");
334 }
335
336 // If TargetPrefix is specified, make sure that Name starts with
337 // "llvm.<targetprefix>.".
338 if (!TargetPrefix.empty()) {
339 StringRef Prefix = StringRef(Name).drop_front(N: 5); // Drop llvm.
340 if (!Prefix.consume_front(Prefix: TargetPrefix) || !Prefix.starts_with(Prefix: '.'))
341 PrintFatalError(ErrorLoc: DefLoc, Msg: "Intrinsic '" + DefName +
342 "' does not start with 'llvm." +
343 TargetPrefix + ".'!");
344 }
345
346 unsigned NumRet = R->getValueAsListInit(FieldName: "RetTypes")->size();
347 unsigned NumParam = R->getValueAsListInit(FieldName: "ParamTypes")->size();
348
349 if (NumRet > MaxNumReturn)
350 PrintFatalError(ErrorLoc: DefLoc, Msg: "intrinsics can only return upto " +
351 Twine(MaxNumReturn) + " values, '" + DefName +
352 "' returns " + Twine(NumRet) + " values");
353
354 const Record *TypeInfo = R->getValueAsDef(FieldName: "TypeInfo");
355 if (!TypeInfo->isSubClassOf(Name: "TypeInfoGen"))
356 PrintFatalError(ErrorLoc: DefLoc, Msg: "TypeInfo field in " + DefName +
357 " should be of subclass of TypeInfoGen!");
358
359 isOverloaded = TypeInfo->getValueAsBit(FieldName: "isOverloaded");
360 std::vector<const Record *> AllTypes =
361 TypeInfo->getValueAsListOfDefs(FieldName: "AllTypes");
362
363 // Validate overload index values in dependent types.
364 if (isOverloaded) {
365 const ListInit *OverloadedTypes =
366 TypeInfo->getValueAsListInit(FieldName: "OverloadTypes");
367 unsigned NumOverloadedTypes = OverloadedTypes->size();
368 for (const auto &[Idx, Ty] : enumerate(First&: AllTypes)) {
369 if (!Ty->isSubClassOf(Name: "LLVMDependentType"))
370 continue;
371 unsigned OverloadIndex = Ty->getValueAsInt(FieldName: "OverloadIndex");
372 if (OverloadIndex >= NumOverloadedTypes)
373 PrintFatalError(
374 Rec: Ty, Msg: formatv(Fmt: "for intrinsic {} overload index {} is invalid, "
375 "intrinsic only has {} overloaded types",
376 Vals&: DefName, Vals&: OverloadIndex, Vals&: NumOverloadedTypes));
377 const Record *OTy = OverloadedTypes->getElementAsRecord(Idx: OverloadIndex);
378 if (!OTy->isSubClassOf(Name: "LLVMAnyType"))
379 PrintFatalError(Rec: Ty, Msg: formatv(Fmt: "for intrinsic {} overload index {} is "
380 "invalid, dependent types must reference "
381 "an overload index of an \'llvm_any\' type",
382 Vals&: DefName, Vals&: OverloadIndex));
383
384 // Replace the dependent type with the overloaded type it references.
385 AllTypes[Idx] = OTy;
386 }
387 }
388
389 ArrayRef<const Record *> AllTypesRef = AllTypes;
390
391 // Types field is a concatenation of Return types followed by Param types.
392 for (const Record *RetTy : AllTypesRef.take_front(N: NumRet)) {
393 if (RetTy->getName() == "llvm_vararg_ty")
394 PrintFatalError(ErrorLoc: DefLoc, Msg: "cannot use llvm_vararg_ty as a return type");
395 IS.RetTys.push_back(x: RetTy);
396 }
397
398 for (const auto &[Idx, ParamTy] : enumerate(First: AllTypesRef.drop_front(N: NumRet))) {
399 if (Idx != NumParam - 1 && ParamTy->getName() == "llvm_vararg_ty")
400 PrintFatalError(ErrorLoc: DefLoc,
401 Msg: "llvm_vararg_ty can only be the last parameter type");
402 IS.ParamTys.push_back(x: ParamTy);
403 }
404
405 // Parse the intrinsic properties.
406 const ListInit *PropList = R->getValueAsListInit(FieldName: "IntrProperties");
407 for (unsigned i = 0, e = PropList->size(); i != e; ++i) {
408 const Record *Property = PropList->getElementAsRecord(Idx: i);
409 assert(Property->isSubClassOf("IntrinsicProperty") &&
410 "Expected a property!");
411
412 setProperty(Property);
413 }
414
415 // Set default properties to true.
416 setDefaultProperties(Ctx.DefaultProperties);
417
418 if (!canThrow)
419 FnAttributes.push_back(Elt: Ctx.NoUnwindAttr);
420
421 if (hasFnAttribute(Name: "NoReturn")) {
422 llvm::erase_if(C&: FnAttributes, P: [](const Record *Attr) {
423 return Attr->getName() == "WillReturn";
424 });
425 }
426
427 llvm::sort(C&: FnAttributes, Comp: LessRecordByID());
428 FnAttributes.erase(CS: llvm::unique(R&: FnAttributes), CE: FnAttributes.end());
429
430 // Also record the SDPatternOperator Properties.
431 Properties = parseSDPatternOperatorProperties(R);
432
433 // Sort the argument attributes for later benefit.
434 for (auto &Attrs : ArgumentAttributes)
435 llvm::sort(C&: Attrs);
436
437 for (const ImmArgRangeSet &RangeSet : ImmArgRangeSets)
438 if (!isParamImmArg(ParamIdx: RangeSet.ArgNo))
439 PrintFatalError(
440 ErrorLoc: TheDef->getLoc(),
441 Msg: formatv(Fmt: "RangeSet requires ImmArg for argument {}", Vals: RangeSet.ArgNo));
442
443 unsigned NumParams = IS.ParamTys.size();
444 bool SeenDefault = false;
445 for (unsigned i = 0; i < NumParams; ++i) {
446 bool HasDefault =
447 (i < ParamDefaultValues.size() && ParamDefaultValues[i].has_value());
448 if (HasDefault) {
449 SeenDefault = true;
450 } else if (SeenDefault) {
451 PrintFatalError(ErrorLoc: TheDef->getLoc(),
452 Msg: "missing default argument on parameter " + Twine(i));
453 }
454 }
455
456 for (unsigned i = 0; i < ParamDefaultValues.size(); ++i) {
457 if (!ParamDefaultValues[i].has_value())
458 continue;
459 const Record *VT = IS.ParamTys[i]->getValueAsDef(FieldName: "VT");
460 if (!VT->getValueAsBit(FieldName: "isInteger")) {
461 PrintFatalError(ErrorLoc: TheDef->getLoc(),
462 Msg: "default argument on parameter " + Twine(i) +
463 " requires an integer parameter type");
464 }
465 unsigned Width = VT->getValueAsInt(FieldName: "Size");
466 uint64_t Value = *ParamDefaultValues[i];
467 if (!isUIntN(N: Width, x: Value)) {
468 PrintFatalError(ErrorLoc: TheDef->getLoc(),
469 Msg: "default argument value " + Twine(Value) +
470 " out of range for i" + Twine(Width) + " parameter " +
471 Twine(i));
472 }
473 }
474}
475
476void CodeGenIntrinsic::setDefaultProperties(
477 ArrayRef<const Record *> DefaultProperties) {
478 // opt-out of using default attributes.
479 if (TheDef->getValueAsBit(FieldName: "DisableDefaultAttributes"))
480 return;
481
482 for (const Record *Rec : DefaultProperties)
483 setProperty(Rec);
484}
485
486void CodeGenIntrinsic::setProperty(const Record *R) {
487 if (R->getName() == "IntrNoMem")
488 ME = MemoryEffects::none();
489 else if (R->getName() == "IntrReadMem") {
490 if (ME.onlyWritesMemory())
491 PrintFatalError(ErrorLoc: TheDef->getLoc(),
492 Msg: Twine("IntrReadMem cannot be used after IntrNoMem or "
493 "IntrWriteMem. Default is ReadWrite"));
494 ME &= MemoryEffects::readOnly();
495 } else if (R->getName() == "IntrWriteMem") {
496 if (ME.onlyReadsMemory())
497 PrintFatalError(ErrorLoc: TheDef->getLoc(),
498 Msg: Twine("IntrWriteMem cannot be used after IntrNoMem or "
499 "IntrReadMem. Default is ReadWrite"));
500 ME &= MemoryEffects::writeOnly();
501 } else if (R->getName() == "IntrArgMemOnly")
502 ME &= MemoryEffects::argMemOnly();
503 else if (R->getName() == "IntrInaccessibleMemOnly")
504 ME &= MemoryEffects::inaccessibleMemOnly();
505 else if (R->isSubClassOf(Name: "IntrRead")) {
506 MemoryEffects ReadMask = MemoryEffects::writeOnly();
507 for (const Record *RLoc : R->getValueAsListOfDefs(FieldName: "MemLoc"))
508 ReadMask = ReadMask.getWithModRef(Loc: getValueAsIRMemLocation(R: RLoc),
509 MR: ModRefInfo::ModRef);
510 ME &= ReadMask;
511 } else if (R->isSubClassOf(Name: "IntrWrite")) {
512 MemoryEffects WriteMask = MemoryEffects::readOnly();
513 for (const Record *WLoc : R->getValueAsListOfDefs(FieldName: "MemLoc"))
514 WriteMask = WriteMask.getWithModRef(Loc: getValueAsIRMemLocation(R: WLoc),
515 MR: ModRefInfo::ModRef);
516 ME &= WriteMask;
517 } else if (R->getName() == "IntrInaccessibleMemOrArgMemOnly")
518 ME &= MemoryEffects::inaccessibleOrArgMemOnly();
519 else if (R->getName() == "Commutative")
520 isCommutative = true;
521 else if (R->getName() == "Throws")
522 canThrow = true;
523 else if (R->getName() == "IntrHasSideEffects")
524 hasSideEffects = true;
525 else if (R->getName() == "IntrTriviallyScalarizable")
526 isTriviallyScalarizable = true;
527 else if (R->isSubClassOf(Name: "IntrinsicFnAttr")) {
528 const Record *Attr = R->getValueAsDef(FieldName: "Attr");
529 if (!is_contained(Range: Attr->getValueAsListOfDefs(FieldName: "Properties"),
530 Element: R->getRecords().getDef(Name: "FnAttr"))) {
531 PrintFatalError(ErrorLoc: R->getLoc(),
532 Msg: Attr->getName() + " is not a function attribute");
533 }
534
535 FnAttributes.push_back(Elt: Attr);
536 } else if (R->isSubClassOf(Name: "IntrinsicArgAttr")) {
537 unsigned ArgNo = R->getValueAsInt(FieldName: "ArgNo");
538 const Record *Attr = R->getValueAsDef(FieldName: "Attr");
539 StringRef Kind = ArgNo == 0 ? "RetAttr" : "ParamAttr";
540 if (!is_contained(Range: Attr->getValueAsListOfDefs(FieldName: "Properties"),
541 Element: R->getRecords().getDef(Name: Kind))) {
542 PrintFatalError(ErrorLoc: R->getLoc(), Msg: Attr->getName() + " is not a " + Kind);
543 }
544
545 if (Attr->isSubClassOf(Name: "ConstantRangeAttr")) {
546 addArgAttribute(Idx: ArgNo, Attr, V: R->getValueAsInt(FieldName: "Lower"),
547 V2: R->getValueAsInt(FieldName: "Upper"));
548 } else {
549 addArgAttribute(Idx: ArgNo, Attr, V: R->getValueAsInt(FieldName: "Value"));
550 }
551
552 if (R->isSubClassOf(Name: "ImmArg")) {
553 const Record *DefaultField = R->getValueAsDef(FieldName: "Default");
554 const RecordVal *ValueField = DefaultField->getValue(Name: "Value");
555 if (ValueField && !isa<UnsetInit>(Val: ValueField->getValue())) {
556 int64_t Value = DefaultField->getValueAsInt(FieldName: "Value");
557 if (Value < 0)
558 PrintFatalError(ErrorLoc: TheDef->getLoc(),
559 Msg: "default argument value " + Twine(Value) +
560 " on parameter " + Twine(ArgNo - 1) +
561 " must be non-negative");
562 addDefaultArgValue(ArgIdx: ArgNo - 1, Value);
563 }
564 }
565 } else if (R->isSubClassOf(Name: "RangeSet")) {
566 unsigned ArgNo = R->getValueAsInt(FieldName: "ArgNo");
567 const ListInit *RangeList = R->getValueAsListInit(FieldName: "Ranges");
568 ImmArgRangeList Ranges;
569
570 if (ArgNo < 1)
571 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "RangeSet should only apply to arguments");
572 unsigned ParamNo = ArgNo - 1;
573
574 for (const ImmArgRangeSet &RangeSet : ImmArgRangeSets)
575 if (RangeSet.ArgNo == ParamNo)
576 PrintFatalError(
577 ErrorLoc: R->getLoc(),
578 Msg: formatv(Fmt: "RangeSet for argument {} already specified", Vals&: ParamNo));
579
580 for (const Init *Range : RangeList->getElements())
581 appendHalfOpenRange(
582 Ranges, Range: getHalfOpenRange(R, Range: cast<ListInit>(Val: Range), AttrName: "RangeSet"));
583 addImmArgRangeSet(ArgNo: ParamNo, Ranges: std::move(Ranges));
584 } else if (R->isSubClassOf(Name: "ArgInfo")) {
585 unsigned ArgNo = R->getValueAsInt(FieldName: "ArgNo");
586 if (ArgNo < 1)
587 PrintFatalError(ErrorLoc: R->getLoc(),
588 Msg: "ArgInfo requires ArgNo >= 1 (0 is return value)");
589 const ListInit *Properties = R->getValueAsListInit(FieldName: "Properties");
590 StringRef ArgName;
591 StringRef FuncName;
592
593 for (const Init *PropInit : Properties->getElements()) {
594 if (const auto *PropDef = dyn_cast<DefInit>(Val: PropInit)) {
595 const Record *PropRec = PropDef->getDef();
596
597 if (PropRec->isSubClassOf(Name: "ArgName"))
598 ArgName = PropRec->getValueAsString(FieldName: "Name");
599 else if (PropRec->isSubClassOf(Name: "ImmArgPrinter"))
600 FuncName = PropRec->getValueAsString(FieldName: "FuncName");
601 else
602 PrintFatalError(ErrorLoc: PropRec->getLoc(),
603 Msg: "Unknown ArgProperty type: " + PropRec->getName());
604 }
605 }
606 addPrettyPrintFunction(ArgIdx: ArgNo - 1, ArgName, FuncName);
607 } else {
608 llvm_unreachable("Unknown property!");
609 }
610}
611
612llvm::IRMemLocation
613CodeGenIntrinsic::getValueAsIRMemLocation(const Record *R) const {
614 StringRef Name = R->getName();
615 IRMemLocation Loc =
616 StringSwitch<IRMemLocation>(Name)
617 .Case(S: "ArgMem", Value: IRMemLocation::ArgMem)
618 .Case(S: "TargetMem0", Value: IRMemLocation::TargetMem0)
619 .Case(S: "TargetMem1", Value: IRMemLocation::TargetMem1)
620 .Case(S: "InaccessibleMem", Value: IRMemLocation::InaccessibleMem)
621 .Default(Value: IRMemLocation::Other); // fallback enum
622
623 if (Loc == IRMemLocation::Other)
624 PrintFatalError(ErrorLoc: R->getLoc(), Msg: "unknown IRMemLocation: " + Name);
625
626 return Loc;
627}
628
629bool CodeGenIntrinsic::isParamAPointer(unsigned ParamIdx) const {
630 if (ParamIdx >= IS.ParamTys.size())
631 return false;
632 return IS.ParamTys[ParamIdx]->isSubClassOf(Name: "LLVMQualPointerType") ||
633 IS.ParamTys[ParamIdx]->isSubClassOf(Name: "LLVMAnyPointerType");
634}
635
636bool CodeGenIntrinsic::isParamImmArg(unsigned ParamIdx) const {
637 // Convert argument index to attribute index starting from `FirstArgIndex`.
638 ++ParamIdx;
639 if (ParamIdx >= ArgumentAttributes.size())
640 return false;
641 return llvm::any_of(Range: ArgumentAttributes[ParamIdx], P: [](const ArgAttribute &A) {
642 return A.Attr->getName() == "ImmArg";
643 });
644}
645
646bool CodeGenIntrinsic::hasFnAttribute(StringRef Name) const {
647 return llvm::any_of(Range: FnAttributes, P: [Name](const Record *Attr) {
648 return Attr->getName() == Name;
649 });
650}
651
652bool CodeGenIntrinsic::ArgAttribute::operator<(
653 const ArgAttribute &Other) const {
654 return std::make_tuple(args: Attr->getID(), args: Value, args: Value2) <
655 std::make_tuple(args: Other.Attr->getID(), args: Other.Value, args: Other.Value2);
656}
657
658void CodeGenIntrinsic::addArgAttribute(unsigned Idx, const Record *Attr,
659 uint64_t V, uint64_t V2) {
660 if (Idx >= ArgumentAttributes.size())
661 ArgumentAttributes.resize(N: Idx + 1);
662 ArgumentAttributes[Idx].emplace_back(Args&: Attr, Args&: V, Args&: V2);
663}
664
665void CodeGenIntrinsic::addImmArgRangeSet(unsigned ArgNo,
666 ImmArgRangeSet::RangeList Ranges) {
667 ImmArgRangeSets.push_back(Elt: {.ArgNo: ArgNo, .Ranges: std::move(Ranges)});
668}
669
670void CodeGenIntrinsic::addPrettyPrintFunction(unsigned ArgIdx,
671 StringRef ArgName,
672 StringRef FuncName) {
673 auto It = llvm::find_if(Range&: PrettyPrintFunctions, P: [ArgIdx](const auto &Info) {
674 return Info.ArgIdx == ArgIdx;
675 });
676 if (It != PrettyPrintFunctions.end())
677 PrintFatalError(ErrorLoc: TheDef->getLoc(), Msg: "ArgInfo for argument " + Twine(ArgIdx) +
678 " is already defined as '" +
679 It->FuncName + "'");
680 PrettyPrintFunctions.emplace_back(Args&: ArgIdx, Args&: ArgName, Args&: FuncName);
681}
682
683void CodeGenIntrinsic::addDefaultArgValue(unsigned ArgIdx, uint64_t Value) {
684 if (ArgIdx >= ParamDefaultValues.size())
685 ParamDefaultValues.resize(N: ArgIdx + 1, NV: std::nullopt);
686
687 if (ParamDefaultValues[ArgIdx].has_value())
688 PrintFatalError(ErrorLoc: TheDef->getLoc(), Msg: "Default value for argument " +
689 Twine(ArgIdx) +
690 " is already defined");
691
692 ParamDefaultValues[ArgIdx] = Value;
693}
694