LLVM 24.0.0git
LLParser.cpp
Go to the documentation of this file.
1//===-- LLParser.cpp - Parser Class ---------------------------------------===//
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 the parser class for .ll files.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/APSInt.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/ScopeExit.h"
22#include "llvm/IR/Argument.h"
23#include "llvm/IR/Attributes.h"
24#include "llvm/IR/AutoUpgrade.h"
25#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Comdat.h"
30#include "llvm/IR/Constants.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalIFunc.h"
36#include "llvm/IR/InlineAsm.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/Metadata.h"
43#include "llvm/IR/Module.h"
44#include "llvm/IR/Operator.h"
45#include "llvm/IR/Value.h"
51#include "llvm/Support/ModRef.h"
54#include <algorithm>
55#include <cassert>
56#include <cstring>
57#include <optional>
58#include <vector>
59
60using namespace llvm;
61
63 "allow-incomplete-ir", cl::init(false), cl::Hidden,
65 "Allow incomplete IR on a best effort basis (references to unknown "
66 "metadata will be dropped)"));
67
68static std::string getTypeString(Type *T) {
69 std::string Result;
70 raw_string_ostream Tmp(Result);
71 Tmp << *T;
72 return Tmp.str();
73}
74
75/// Return whether skipped trivia contains a block comment that crosses the
76/// boundary between two metadata definitions.
77static bool blockCommentCrossesBoundary(SMLoc BeginLoc, SMLoc EndLoc,
78 SMLoc BoundaryLoc) {
79 const char *Begin = BeginLoc.getPointer();
80 const char *End = EndLoc.getPointer();
81 const char *Boundary = BoundaryLoc.getPointer();
82 const char *BlockCommentStart = nullptr;
83 bool InLineComment = false;
84
85 for (const char *Ptr = Begin; Ptr < End;) {
86 if (BlockCommentStart) {
87 if (Ptr + 1 < End && Ptr[0] == '*' && Ptr[1] == '/') {
88 Ptr += 2;
89 if (BlockCommentStart < Boundary && Ptr > Boundary)
90 return true;
91 BlockCommentStart = nullptr;
92 continue;
93 }
94 ++Ptr;
95 continue;
96 }
97
98 if (InLineComment) {
99 if (*Ptr == '\n' || *Ptr == '\r')
100 InLineComment = false;
101 ++Ptr;
102 continue;
103 }
104
105 if (*Ptr == ';') {
106 InLineComment = true;
107 ++Ptr;
108 continue;
109 }
110 if (Ptr + 1 < End && Ptr[0] == '/' && Ptr[1] == '*') {
111 BlockCommentStart = Ptr;
112 Ptr += 2;
113 continue;
114 }
115 ++Ptr;
116 }
117
118 return BlockCommentStart && BlockCommentStart < Boundary && End > Boundary;
119}
120
121/// Run: module ::= toplevelentity*
122bool LLParser::Run(bool UpgradeDebugInfo,
123 DataLayoutCallbackTy DataLayoutCallback) {
124 // Prime the lexer.
125 Lex.Lex();
126
127 if (Context.shouldDiscardValueNames())
128 return error(
129 Lex.getLoc(),
130 "Can't read textual IR with a Context that discards named Values");
131
132 if (M) {
133 if (parseTargetDefinitions(DataLayoutCallback))
134 return true;
135 }
136
137 return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) ||
138 validateEndOfIndex();
139}
140
142 const SlotMapping *Slots) {
143 restoreParsingState(Slots);
144 Lex.Lex();
145
146 Type *Ty = nullptr;
147 if (parseType(Ty) || parseConstantValue(Ty, C))
148 return true;
149 if (Lex.getKind() != lltok::Eof)
150 return error(Lex.getLoc(), "expected end of string");
151 return false;
152}
153
155 const SlotMapping *Slots) {
156 restoreParsingState(Slots);
157 Lex.Lex();
158
159 Read = 0;
160 SMLoc Start = Lex.getLoc();
161 Ty = nullptr;
162 if (parseType(Ty))
163 return true;
164 SMLoc End = Lex.getLoc();
165 Read = End.getPointer() - Start.getPointer();
166
167 return false;
168}
169
171 const SlotMapping *Slots) {
172 restoreParsingState(Slots);
173 Lex.Lex();
174
175 Read = 0;
176 SMLoc Start = Lex.getLoc();
177 Result = nullptr;
178 bool Status = parseDIExpressionBody(Result, /*IsDistinct=*/false);
179 SMLoc End = Lex.getLoc();
180 Read = End.getPointer() - Start.getPointer();
181
182 return Status;
183}
184
186 ArrayRef<SMLoc> DefinitionEnds) {
187 restoreParsingState(&Slots);
188 Lex.Lex();
189
190 for (SMLoc End : DefinitionEnds) {
191 if (Lex.getLoc().getPointer() >= End.getPointer())
192 return error(End, "expected end of metadata definition");
193 if (Lex.getKind() != lltok::exclaim)
194 return tokError("expected a metadata definition");
195 if (parseStandaloneMetadata())
196 return true;
197 if (Lex.getPrevTokEndLoc().getPointer() > End.getPointer() ||
198 (Lex.getKind() != lltok::Eof &&
199 Lex.getLoc().getPointer() < End.getPointer()) ||
200 blockCommentCrossesBoundary(Lex.getPrevTokEndLoc(), Lex.getLoc(), End))
201 return error(End, "expected end of metadata definition");
202 }
203
204 if (Lex.getKind() != lltok::Eof)
205 return tokError("expected end of metadata definitions");
206
207 if (!ForwardRefMDNodes.empty())
208 return error(ForwardRefMDNodes.begin()->second.second,
209 "use of undefined metadata '!" +
210 Twine(ForwardRefMDNodes.begin()->first) + "'");
211
212 for (auto &[_, MD] : NumberedMetadata)
213 if (MD && !MD->isResolved())
214 MD->resolveCycles();
216 NewDistinctSPs.clear();
217
218 Slots.MetadataNodes = std::move(NumberedMetadata);
219 return false;
220}
221
222void LLParser::restoreParsingState(const SlotMapping *Slots) {
223 if (!Slots)
224 return;
225 NumberedVals = Slots->GlobalValues;
226 NumberedMetadata = Slots->MetadataNodes;
227 for (const auto &I : Slots->NamedTypes)
228 NamedTypes.insert(
229 std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
230 for (const auto &I : Slots->Types)
231 NumberedTypes.insert(
232 std::make_pair(I.first, std::make_pair(I.second, LocTy())));
233}
234
236 // White-list intrinsics that are safe to drop.
238 II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl)
239 return;
240
242 for (Value *V : II->args())
243 if (auto *MV = dyn_cast<MetadataAsValue>(V))
244 if (auto *MD = dyn_cast<MDNode>(MV->getMetadata()))
245 if (MD->isTemporary())
246 MVs.push_back(MV);
247
248 if (!MVs.empty()) {
249 assert(II->use_empty() && "Cannot have uses");
250 II->eraseFromParent();
251
252 // Also remove no longer used MetadataAsValue wrappers.
253 for (MetadataAsValue *MV : MVs)
254 if (MV->use_empty())
255 delete MV;
256 }
257}
258
259void LLParser::dropUnknownMetadataReferences() {
260 auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); };
261 for (Function &F : *M) {
262 F.eraseMetadataIf(Pred);
263 for (Instruction &I : make_early_inc_range(instructions(F))) {
264 I.eraseMetadataIf(Pred);
265
266 if (auto *II = dyn_cast<IntrinsicInst>(&I))
268 }
269 }
270
271 for (GlobalVariable &GV : M->globals())
272 GV.eraseMetadataIf(Pred);
273
274 llvm::erase_if(PendingDbgRecords,
275 [](const auto &E) { return std::get<2>(E)->isTemporary(); });
276 llvm::erase_if(PendingDbgInsts,
277 [](const auto &E) { return std::get<2>(E)->isTemporary(); });
278
279 for (const auto &[ID, Info] : make_early_inc_range(ForwardRefMDNodes)) {
280 // Check whether there is only a single use left, which would be in our
281 // own NumberedMetadata.
282 if (Info.first->getNumTemporaryUses() == 1) {
283 NumberedMetadata.erase(ID);
284 ForwardRefMDNodes.erase(ID);
285 }
286 }
287}
288
289/// validateEndOfModule - Do final validity and basic correctness checks at the
290/// end of the module.
291bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
292 if (!M)
293 return false;
294
295 // We should have already returned an error if we observed both intrinsics and
296 // records in this IR.
297 assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) &&
298 "Mixed debug intrinsics/records seen without a parsing error?");
299
300 // Handle any function attribute group forward references.
301 for (const auto &RAG : ForwardRefAttrGroups) {
302 Value *V = RAG.first;
303 const std::vector<unsigned> &Attrs = RAG.second;
304 AttrBuilder B(Context);
305
306 for (const auto &Attr : Attrs) {
307 auto R = NumberedAttrBuilders.find(Attr);
308 if (R != NumberedAttrBuilders.end())
309 B.merge(R->second);
310 }
311
312 if (Function *Fn = dyn_cast<Function>(V)) {
313 AttributeList AS = Fn->getAttributes();
314 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
315 AS = AS.removeFnAttributes(Context);
316
317 FnAttrs.merge(B);
318
319 // If the alignment was parsed as an attribute, move to the alignment
320 // field.
321 if (MaybeAlign A = FnAttrs.getAlignment()) {
322 Fn->setAlignment(*A);
323 FnAttrs.removeAttribute(Attribute::Alignment);
324 }
325
326 AS = AS.addFnAttributes(Context, FnAttrs);
327 Fn->setAttributes(AS);
328 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
329 AttributeList AS = CI->getAttributes();
330 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
331 AS = AS.removeFnAttributes(Context);
332 FnAttrs.merge(B);
333 AS = AS.addFnAttributes(Context, FnAttrs);
334 CI->setAttributes(AS);
335 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
336 AttributeList AS = II->getAttributes();
337 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
338 AS = AS.removeFnAttributes(Context);
339 FnAttrs.merge(B);
340 AS = AS.addFnAttributes(Context, FnAttrs);
341 II->setAttributes(AS);
342 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(V)) {
343 AttributeList AS = CBI->getAttributes();
344 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
345 AS = AS.removeFnAttributes(Context);
346 FnAttrs.merge(B);
347 AS = AS.addFnAttributes(Context, FnAttrs);
348 CBI->setAttributes(AS);
349 } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
350 AttrBuilder Attrs(M->getContext(), GV->getAttributes());
351 Attrs.merge(B);
352 GV->setAttributes(AttributeSet::get(Context,Attrs));
353 } else {
354 llvm_unreachable("invalid object with forward attribute group reference");
355 }
356 }
357
358 // If there are entries in ForwardRefBlockAddresses at this point, the
359 // function was never defined.
360 if (!ForwardRefBlockAddresses.empty())
361 return error(ForwardRefBlockAddresses.begin()->first.Loc,
362 "expected function name in blockaddress");
363
364 auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef,
365 GlobalValue *FwdRef) {
366 GlobalValue *GV = nullptr;
367 if (GVRef.Kind == ValID::t_GlobalName) {
368 GV = M->getNamedValue(GVRef.StrVal);
369 } else {
370 GV = NumberedVals.get(GVRef.UIntVal);
371 }
372
373 if (!GV)
374 return error(GVRef.Loc, "unknown function '" + GVRef.StrVal +
375 "' referenced by dso_local_equivalent");
376
377 if (!GV->getValueType()->isFunctionTy())
378 return error(GVRef.Loc,
379 "expected a function, alias to function, or ifunc "
380 "in dso_local_equivalent");
381
382 auto *Equiv = DSOLocalEquivalent::get(GV);
383 FwdRef->replaceAllUsesWith(Equiv);
384 FwdRef->eraseFromParent();
385 return false;
386 };
387
388 // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this
389 // point, they are references after the function was defined. Resolve those
390 // now.
391 for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) {
392 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
393 return true;
394 }
395 for (auto &Iter : ForwardRefDSOLocalEquivalentNames) {
396 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
397 return true;
398 }
399 ForwardRefDSOLocalEquivalentIDs.clear();
400 ForwardRefDSOLocalEquivalentNames.clear();
401
402 for (const auto &NT : NumberedTypes)
403 if (NT.second.second.isValid())
404 return error(NT.second.second,
405 "use of undefined type '%" + Twine(NT.first) + "'");
406
407 for (const auto &[Name, TypeInfo] : NamedTypes)
408 if (TypeInfo.second.isValid())
409 return error(TypeInfo.second,
410 "use of undefined type named '" + Name + "'");
411
412 if (!ForwardRefComdats.empty())
413 return error(ForwardRefComdats.begin()->second,
414 "use of undefined comdat '$" +
415 ForwardRefComdats.begin()->first + "'");
416
417 if (AllowIncompleteIR && !ForwardRefMDNodes.empty())
418 dropUnknownMetadataReferences();
419
420 if (!ForwardRefMDNodes.empty())
421 return error(ForwardRefMDNodes.begin()->second.second,
422 "use of undefined metadata '!" +
423 Twine(ForwardRefMDNodes.begin()->first) + "'");
424
425 // Set debug locations.
426 for (auto [Loc, DR, MD] : PendingDbgRecords) {
427 if (auto *DI = dyn_cast<DILocation>(MD))
428 DR->setDebugLoc(DebugLoc(DI));
429 else
430 return error(Loc, "invalid debug location");
431 }
432 PendingDbgRecords.clear();
433 for (auto [Loc, I, MD] : PendingDbgInsts) {
434 if (auto *DI = dyn_cast<DILocation>(MD))
435 I->setDebugLoc(DebugLoc(DI));
436 else
437 return error(Loc, "invalid !dbg metadata");
438 }
439 PendingDbgInsts.clear();
440
441 for (const auto &[Name, Info] : make_early_inc_range(ForwardRefVals)) {
442 if (StringRef(Name).starts_with("llvm.")) {
444 // Automatically create declarations for intrinsics. Intrinsics can only
445 // be called directly, so the call function type directly determines the
446 // declaration function type.
447 //
448 // Additionally, automatically add the required mangling suffix to the
449 // intrinsic name. This means that we may replace a single forward
450 // declaration with multiple functions here.
451 for (Use &U : make_early_inc_range(Info.first->uses())) {
452 auto *CB = dyn_cast<CallBase>(U.getUser());
453 if (!CB || !CB->isCallee(&U))
454 return error(Info.second, "intrinsic can only be used as callee");
455
456 std::string ErrorMsg;
457 raw_string_ostream ErrorOS(ErrorMsg);
458
459 SmallVector<Type *> OverloadTys;
460 if (IID != Intrinsic::not_intrinsic &&
461 Intrinsic::isSignatureValid(IID, CB->getFunctionType(), OverloadTys,
462 ErrorOS)) {
463 U.set(Intrinsic::getOrInsertDeclaration(M, IID, OverloadTys));
464 } else {
465 // Try to upgrade the intrinsic.
466 Function *TmpF = Function::Create(CB->getFunctionType(),
468 Function *NewF = nullptr;
469 if (!UpgradeIntrinsicFunction(TmpF, NewF)) {
470 if (IID == Intrinsic::not_intrinsic)
471 return error(Info.second, "unknown intrinsic '" + Name + "'");
472 return error(Info.second, ErrorMsg);
473 }
474
475 U.set(TmpF);
476 UpgradeIntrinsicCall(CB, NewF);
477 if (TmpF->use_empty())
478 TmpF->eraseFromParent();
479 }
480 }
481
482 Info.first->eraseFromParent();
483 ForwardRefVals.erase(Name);
484 continue;
485 }
486
487 // If incomplete IR is allowed, also add declarations for
488 // non-intrinsics.
490 continue;
491
492 auto GetCommonFunctionType = [](Value *V) -> FunctionType * {
493 FunctionType *FTy = nullptr;
494 for (Use &U : V->uses()) {
495 auto *CB = dyn_cast<CallBase>(U.getUser());
496 if (!CB || !CB->isCallee(&U) || (FTy && FTy != CB->getFunctionType()))
497 return nullptr;
498 FTy = CB->getFunctionType();
499 }
500 return FTy;
501 };
502
503 // First check whether this global is only used in calls with the same
504 // type, in which case we'll insert a function. Otherwise, fall back to
505 // using a dummy i8 type.
506 Type *Ty = GetCommonFunctionType(Info.first);
507 if (!Ty)
508 Ty = Type::getInt8Ty(Context);
509
510 GlobalValue *GV;
511 if (auto *FTy = dyn_cast<FunctionType>(Ty))
513 else
514 GV = new GlobalVariable(*M, Ty, /*isConstant*/ false,
516 /*Initializer*/ nullptr, Name);
517 Info.first->replaceAllUsesWith(GV);
518 Info.first->eraseFromParent();
519 ForwardRefVals.erase(Name);
520 }
521
522 if (!ForwardRefVals.empty())
523 return error(ForwardRefVals.begin()->second.second,
524 "use of undefined value '@" + ForwardRefVals.begin()->first +
525 "'");
526
527 if (!ForwardRefValIDs.empty())
528 return error(ForwardRefValIDs.begin()->second.second,
529 "use of undefined value '@" +
530 Twine(ForwardRefValIDs.begin()->first) + "'");
531
532 // Resolve metadata cycles.
533 for (auto &N : NumberedMetadata) {
534 if (N.second && !N.second->isResolved())
535 N.second->resolveCycles();
536 }
537
539 NewDistinctSPs.clear();
540
541 // Look for intrinsic functions and CallInst that need to be upgraded. We use
542 // make_early_inc_range here because we may remove some functions.
545
546 if (UpgradeDebugInfo)
548
554
555 if (!Slots)
556 return false;
557 // Initialize the slot mapping.
558 // Because by this point we've parsed and validated everything, we can "steal"
559 // the mapping from LLParser as it doesn't need it anymore.
560 Slots->GlobalValues = std::move(NumberedVals);
561 Slots->MetadataNodes = std::move(NumberedMetadata);
562 for (const auto &I : NamedTypes)
563 Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
564 for (const auto &I : NumberedTypes)
565 Slots->Types.insert(std::make_pair(I.first, I.second.first));
566
567 return false;
568}
569
570/// Do final validity and basic correctness checks at the end of the index.
571bool LLParser::validateEndOfIndex() {
572 if (!Index)
573 return false;
574
575 if (!ForwardRefValueInfos.empty())
576 return error(ForwardRefValueInfos.begin()->second.front().second,
577 "use of undefined summary '^" +
578 Twine(ForwardRefValueInfos.begin()->first) + "'");
579
580 if (!ForwardRefAliasees.empty())
581 return error(ForwardRefAliasees.begin()->second.front().second,
582 "use of undefined summary '^" +
583 Twine(ForwardRefAliasees.begin()->first) + "'");
584
585 if (!ForwardRefTypeIds.empty())
586 return error(ForwardRefTypeIds.begin()->second.front().second,
587 "use of undefined type id summary '^" +
588 Twine(ForwardRefTypeIds.begin()->first) + "'");
589
590 return false;
591}
592
593//===----------------------------------------------------------------------===//
594// Top-Level Entities
595//===----------------------------------------------------------------------===//
596
597bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) {
598 // Delay parsing of the data layout string until the target triple is known.
599 // Then, pass both the the target triple and the tentative data layout string
600 // to DataLayoutCallback, allowing to override the DL string.
601 // This enables importing modules with invalid DL strings.
602 std::string TentativeDLStr = M->getDataLayoutStr();
603 LocTy DLStrLoc;
604
605 bool Done = false;
606 while (!Done) {
607 switch (Lex.getKind()) {
608 case lltok::kw_target:
609 if (parseTargetDefinition(TentativeDLStr, DLStrLoc))
610 return true;
611 break;
613 if (parseSourceFileName())
614 return true;
615 break;
616 default:
617 Done = true;
618 }
619 }
620 // Run the override callback to potentially change the data layout string, and
621 // parse the data layout string.
622 if (auto LayoutOverride =
623 DataLayoutCallback(M->getTargetTriple().str(), TentativeDLStr)) {
624 TentativeDLStr = *LayoutOverride;
625 DLStrLoc = {};
626 }
627 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDLStr);
628 if (!MaybeDL)
629 return error(DLStrLoc, toString(MaybeDL.takeError()));
630 M->setDataLayout(MaybeDL.get());
631 return false;
632}
633
634bool LLParser::parseTopLevelEntities() {
635 // If there is no Module, then parse just the summary index entries.
636 if (!M) {
637 while (true) {
638 switch (Lex.getKind()) {
639 case lltok::Eof:
640 return false;
641 case lltok::SummaryID:
642 if (parseSummaryEntry())
643 return true;
644 break;
646 if (parseSourceFileName())
647 return true;
648 break;
649 default:
650 // Skip everything else
651 Lex.Lex();
652 }
653 }
654 }
655 while (true) {
656 switch (Lex.getKind()) {
657 default:
658 return tokError("expected top-level entity");
659 case lltok::Eof: return false;
661 if (parseDeclare())
662 return true;
663 break;
664 case lltok::kw_define:
665 if (parseDefine())
666 return true;
667 break;
668 case lltok::kw_module:
669 if (parseModuleAsm())
670 return true;
671 break;
673 if (parseUnnamedType())
674 return true;
675 break;
676 case lltok::LocalVar:
677 if (parseNamedType())
678 return true;
679 break;
680 case lltok::GlobalID:
681 if (parseUnnamedGlobal())
682 return true;
683 break;
684 case lltok::GlobalVar:
685 if (parseNamedGlobal())
686 return true;
687 break;
688 case lltok::ComdatVar: if (parseComdat()) return true; break;
689 case lltok::exclaim:
690 if (parseStandaloneMetadata())
691 return true;
692 break;
693 case lltok::SummaryID:
694 if (parseSummaryEntry())
695 return true;
696 break;
698 if (parseNamedMetadata())
699 return true;
700 break;
702 if (parseUnnamedAttrGrp())
703 return true;
704 break;
706 if (parseUseListOrder())
707 return true;
708 break;
709 }
710 }
711}
712
713/// toplevelentity
714/// ::= 'module' 'asm' STRINGCONSTANT
715/// ::= 'module' 'asm' '(' 'property_name1:' STRINGCONSTANT ','
716/// 'property_name2:' STRINGCONSTANT ')'
717/// STRINGCONSTANT
718bool LLParser::parseModuleAsm() {
719 assert(Lex.getKind() == lltok::kw_module);
720 Lex.Lex();
721
722 std::string AsmStr;
723 if (parseToken(lltok::kw_asm, "expected 'module asm'"))
724 return true;
725
726 Module::GlobalAsmProperties Props;
727 if (EatIfPresent(lltok::lparen)) {
728 while (true) {
729 std::string Key, Value;
730 SMLoc Loc = Lex.getLoc();
731 if (Lex.getKind() != lltok::LabelStr)
732 return error(Loc, "expected property name followed by ':'");
733
734 Key = Lex.getStrVal();
735 Lex.Lex();
736
737 if (parseStringConstant(Value))
738 return true;
739
740 if (!Props.set(Key, Value))
741 return error(Loc, "unknown property name");
742
743 if (EatIfPresent(lltok::rparen))
744 break;
745 if (parseToken(lltok::comma, "expected ',' or ')'"))
746 return true;
747 }
748 }
749
750 do {
751 std::string AsmStrPart;
752 if (parseStringConstant(AsmStrPart))
753 return true;
754 AsmStr += AsmStrPart + "\n";
755 } while (Lex.getKind() == lltok::StringConstant);
756
757 M->appendModuleInlineAsm({AsmStr, Props});
758 return false;
759}
760
761/// toplevelentity
762/// ::= 'target' 'triple' '=' STRINGCONSTANT
763/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
764bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
765 LocTy &DLStrLoc) {
766 assert(Lex.getKind() == lltok::kw_target);
767 std::string Str;
768 switch (Lex.Lex()) {
769 default:
770 return tokError("unknown target property");
771 case lltok::kw_triple:
772 Lex.Lex();
773 if (parseToken(lltok::equal, "expected '=' after target triple") ||
774 parseStringConstant(Str))
775 return true;
776 M->setTargetTriple(Triple(std::move(Str)));
777 return false;
779 Lex.Lex();
780 if (parseToken(lltok::equal, "expected '=' after target datalayout"))
781 return true;
782 DLStrLoc = Lex.getLoc();
783 if (parseStringConstant(TentativeDLStr))
784 return true;
785 return false;
786 }
787}
788
789/// toplevelentity
790/// ::= 'source_filename' '=' STRINGCONSTANT
791bool LLParser::parseSourceFileName() {
792 assert(Lex.getKind() == lltok::kw_source_filename);
793 Lex.Lex();
794 if (parseToken(lltok::equal, "expected '=' after source_filename") ||
795 parseStringConstant(SourceFileName))
796 return true;
797 if (M)
798 M->setSourceFileName(SourceFileName);
799 return false;
800}
801
802/// parseUnnamedType:
803/// ::= LocalVarID '=' 'type' type
804bool LLParser::parseUnnamedType() {
805 LocTy TypeLoc = Lex.getLoc();
806 unsigned TypeID = Lex.getUIntVal();
807 Lex.Lex(); // eat LocalVarID;
808
809 if (parseToken(lltok::equal, "expected '=' after name") ||
810 parseToken(lltok::kw_type, "expected 'type' after '='"))
811 return true;
812
813 Type *Result = nullptr;
814 if (parseStructDefinition(TypeLoc, "", NumberedTypes[TypeID], Result))
815 return true;
816
817 if (!isa<StructType>(Result)) {
818 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
819 if (Entry.first)
820 return error(TypeLoc, "non-struct types may not be recursive");
821 Entry.first = Result;
822 Entry.second = SMLoc();
823 }
824
825 return false;
826}
827
828/// toplevelentity
829/// ::= LocalVar '=' 'type' type
830bool LLParser::parseNamedType() {
831 std::string Name = Lex.getStrVal();
832 LocTy NameLoc = Lex.getLoc();
833 Lex.Lex(); // eat LocalVar.
834
835 if (parseToken(lltok::equal, "expected '=' after name") ||
836 parseToken(lltok::kw_type, "expected 'type' after name"))
837 return true;
838
839 Type *Result = nullptr;
840 if (parseStructDefinition(NameLoc, Name, NamedTypes[Name], Result))
841 return true;
842
843 if (!isa<StructType>(Result)) {
844 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
845 if (Entry.first)
846 return error(NameLoc, "non-struct types may not be recursive");
847 Entry.first = Result;
848 Entry.second = SMLoc();
849 }
850
851 return false;
852}
853
854/// toplevelentity
855/// ::= 'declare' FunctionHeader
856bool LLParser::parseDeclare() {
857 assert(Lex.getKind() == lltok::kw_declare);
858 Lex.Lex();
859
860 std::vector<std::pair<unsigned, MDNode *>> MDs;
861 while (Lex.getKind() == lltok::MetadataVar) {
862 unsigned MDK;
863 MDNode *N;
864 if (parseMetadataAttachment(MDK, N))
865 return true;
866 MDs.push_back({MDK, N});
867 }
868
869 Function *F;
870 unsigned FunctionNumber = -1;
871 SmallVector<unsigned> UnnamedArgNums;
872 if (parseFunctionHeader(F, false, FunctionNumber, UnnamedArgNums))
873 return true;
874 for (auto &MD : MDs)
875 F->addMetadata(MD.first, *MD.second);
876 return false;
877}
878
879/// toplevelentity
880/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
881bool LLParser::parseDefine() {
882 assert(Lex.getKind() == lltok::kw_define);
883
884 FileLoc FunctionStart = getTokLineColumnPos();
885 Lex.Lex();
886
887 Function *F;
888 unsigned FunctionNumber = -1;
889 SmallVector<unsigned> UnnamedArgNums;
890 bool RetValue =
891 parseFunctionHeader(F, true, FunctionNumber, UnnamedArgNums) ||
892 parseOptionalFunctionMetadata(*F) ||
893 parseFunctionBody(*F, FunctionNumber, UnnamedArgNums);
894 if (ParserContext)
895 ParserContext->addFunctionLocation(
896 F, FileLocRange(FunctionStart, getPrevTokEndLineColumnPos()));
897
898 return RetValue;
899}
900
901/// parseGlobalType
902/// ::= 'constant'
903/// ::= 'global'
904bool LLParser::parseGlobalType(bool &IsConstant) {
905 if (Lex.getKind() == lltok::kw_constant)
906 IsConstant = true;
907 else if (Lex.getKind() == lltok::kw_global)
908 IsConstant = false;
909 else {
910 IsConstant = false;
911 return tokError("expected 'global' or 'constant'");
912 }
913 Lex.Lex();
914 return false;
915}
916
917bool LLParser::parseOptionalUnnamedAddr(
918 GlobalVariable::UnnamedAddr &UnnamedAddr) {
919 if (EatIfPresent(lltok::kw_unnamed_addr))
921 else if (EatIfPresent(lltok::kw_local_unnamed_addr))
923 else
924 UnnamedAddr = GlobalValue::UnnamedAddr::None;
925 return false;
926}
927
928/// parseUnnamedGlobal:
929/// OptionalVisibility (ALIAS | IFUNC) ...
930/// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
931/// OptionalDLLStorageClass
932/// ... -> global variable
933/// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
934/// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier
935/// OptionalVisibility
936/// OptionalDLLStorageClass
937/// ... -> global variable
938bool LLParser::parseUnnamedGlobal() {
939 unsigned VarID;
940 std::string Name;
941 LocTy NameLoc = Lex.getLoc();
942
943 // Handle the GlobalID form.
944 if (Lex.getKind() == lltok::GlobalID) {
945 VarID = Lex.getUIntVal();
946 if (checkValueID(NameLoc, "global", "@", NumberedVals.getNext(), VarID))
947 return true;
948
949 Lex.Lex(); // eat GlobalID;
950 if (parseToken(lltok::equal, "expected '=' after name"))
951 return true;
952 } else {
953 VarID = NumberedVals.getNext();
954 }
955
956 bool HasLinkage;
957 unsigned Linkage, Visibility, DLLStorageClass;
958 bool DSOLocal;
960 GlobalVariable::UnnamedAddr UnnamedAddr;
961 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
962 DSOLocal) ||
963 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
964 return true;
965
966 switch (Lex.getKind()) {
967 default:
968 return parseGlobal(Name, VarID, NameLoc, Linkage, HasLinkage, Visibility,
969 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
970 case lltok::kw_alias:
971 case lltok::kw_ifunc:
972 return parseAliasOrIFunc(Name, VarID, NameLoc, Linkage, Visibility,
973 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
974 }
975}
976
977/// parseNamedGlobal:
978/// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
979/// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
980/// OptionalVisibility OptionalDLLStorageClass
981/// ... -> global variable
982bool LLParser::parseNamedGlobal() {
983 assert(Lex.getKind() == lltok::GlobalVar);
984 LocTy NameLoc = Lex.getLoc();
985 std::string Name = Lex.getStrVal();
986 Lex.Lex();
987
988 bool HasLinkage;
989 unsigned Linkage, Visibility, DLLStorageClass;
990 bool DSOLocal;
992 GlobalVariable::UnnamedAddr UnnamedAddr;
993 if (parseToken(lltok::equal, "expected '=' in global variable") ||
994 parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
995 DSOLocal) ||
996 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
997 return true;
998
999 switch (Lex.getKind()) {
1000 default:
1001 return parseGlobal(Name, -1, NameLoc, Linkage, HasLinkage, Visibility,
1002 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
1003 case lltok::kw_alias:
1004 case lltok::kw_ifunc:
1005 return parseAliasOrIFunc(Name, -1, NameLoc, Linkage, Visibility,
1006 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
1007 }
1008}
1009
1010bool LLParser::parseComdat() {
1011 assert(Lex.getKind() == lltok::ComdatVar);
1012 std::string Name = Lex.getStrVal();
1013 LocTy NameLoc = Lex.getLoc();
1014 Lex.Lex();
1015
1016 if (parseToken(lltok::equal, "expected '=' here"))
1017 return true;
1018
1019 if (parseToken(lltok::kw_comdat, "expected comdat keyword"))
1020 return tokError("expected comdat type");
1021
1023 switch (Lex.getKind()) {
1024 default:
1025 return tokError("unknown selection kind");
1026 case lltok::kw_any:
1027 SK = Comdat::Any;
1028 break;
1030 SK = Comdat::ExactMatch;
1031 break;
1032 case lltok::kw_largest:
1033 SK = Comdat::Largest;
1034 break;
1037 break;
1038 case lltok::kw_samesize:
1039 SK = Comdat::SameSize;
1040 break;
1041 }
1042 Lex.Lex();
1043
1044 // See if the comdat was forward referenced, if so, use the comdat.
1045 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1046 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1047 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
1048 return error(NameLoc, "redefinition of comdat '$" + Name + "'");
1049
1050 Comdat *C;
1051 if (I != ComdatSymTab.end())
1052 C = &I->second;
1053 else
1054 C = M->getOrInsertComdat(Name);
1055 C->setSelectionKind(SK);
1056
1057 return false;
1058}
1059
1060// MDString:
1061// ::= '!' STRINGCONSTANT
1062bool LLParser::parseMDString(MDString *&Result) {
1063 std::string Str;
1064 if (parseStringConstant(Str))
1065 return true;
1066 Result = MDString::get(Context, Str);
1067 return false;
1068}
1069
1070// MDNode:
1071// ::= '!' MDNodeNumber
1072bool LLParser::parseMDNodeID(MDNode *&Result) {
1073 // !{ ..., !42, ... }
1074 LocTy IDLoc = Lex.getLoc();
1075 unsigned MID = 0;
1076 if (parseUInt32(MID))
1077 return true;
1078
1079 // If not a forward reference, just return it now.
1080 auto [It, Inserted] = NumberedMetadata.try_emplace(MID);
1081 if (!Inserted) {
1082 Result = It->second;
1083 return false;
1084 }
1085
1086 // Otherwise, create MDNode forward reference.
1087 auto &FwdRef = ForwardRefMDNodes[MID];
1088 FwdRef = std::make_pair(MDTuple::getTemporary(Context, {}), IDLoc);
1089
1090 Result = FwdRef.first.get();
1091 It->second.reset(Result);
1092 return false;
1093}
1094
1095/// parseNamedMetadata:
1096/// !foo = !{ !1, !2 }
1097bool LLParser::parseNamedMetadata() {
1098 assert(Lex.getKind() == lltok::MetadataVar);
1099 std::string Name = Lex.getStrVal();
1100 Lex.Lex();
1101
1102 if (parseToken(lltok::equal, "expected '=' here") ||
1103 parseToken(lltok::exclaim, "Expected '!' here") ||
1104 parseToken(lltok::lbrace, "Expected '{' here"))
1105 return true;
1106
1107 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
1108 if (Lex.getKind() != lltok::rbrace)
1109 do {
1110 MDNode *N = nullptr;
1111 // parse DIExpressions inline as a special case. They are still MDNodes,
1112 // so they can still appear in named metadata. Remove this logic if they
1113 // become plain Metadata.
1114 if (Lex.getKind() == lltok::MetadataVar &&
1115 Lex.getStrVal() == "DIExpression") {
1116 if (parseDIExpression(N, /*IsDistinct=*/false))
1117 return true;
1118 // DIArgLists should only appear inline in a function, as they may
1119 // contain LocalAsMetadata arguments which require a function context.
1120 } else if (Lex.getKind() == lltok::MetadataVar &&
1121 Lex.getStrVal() == "DIArgList") {
1122 return tokError("found DIArgList outside of function");
1123 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
1124 parseMDNodeID(N)) {
1125 return true;
1126 }
1127 NMD->addOperand(N);
1128 } while (EatIfPresent(lltok::comma));
1129
1130 return parseToken(lltok::rbrace, "expected end of metadata node");
1131}
1132
1133/// parseStandaloneMetadata:
1134/// !42 = !{...}
1135bool LLParser::parseStandaloneMetadata() {
1136 assert(Lex.getKind() == lltok::exclaim);
1137 Lex.Lex();
1138 unsigned MetadataID = 0;
1139
1140 MDNode *Init;
1141 if (parseUInt32(MetadataID) || parseToken(lltok::equal, "expected '=' here"))
1142 return true;
1143
1144 // Detect common error, from old metadata syntax.
1145 if (Lex.getKind() == lltok::Type)
1146 return tokError("unexpected type in metadata definition");
1147
1148 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
1149 if (Lex.getKind() == lltok::MetadataVar) {
1150 if (parseSpecializedMDNode(Init, IsDistinct))
1151 return true;
1152 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
1153 parseMDTuple(Init, IsDistinct))
1154 return true;
1155
1156 // See if this was forward referenced, if so, handle it.
1157 auto FI = ForwardRefMDNodes.find(MetadataID);
1158 if (FI != ForwardRefMDNodes.end()) {
1159 auto *ToReplace = FI->second.first.get();
1160 // DIAssignID has its own special forward-reference "replacement" for
1161 // attachments (the temporary attachments are never actually attached).
1162 if (isa<DIAssignID>(Init)) {
1163 for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) {
1164 assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) &&
1165 "Inst unexpectedly already has DIAssignID attachment");
1166 Inst->setMetadata(LLVMContext::MD_DIAssignID, Init);
1167 }
1168 }
1169
1170 ToReplace->replaceAllUsesWith(Init);
1171 ForwardRefMDNodes.erase(FI);
1172
1173 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
1174 } else {
1175 auto [It, Inserted] = NumberedMetadata.try_emplace(MetadataID);
1176 if (!Inserted)
1177 return tokError("Metadata id is already used");
1178 It->second.reset(Init);
1179 }
1180
1181 return false;
1182}
1183
1184// Skips a single module summary entry.
1185bool LLParser::skipModuleSummaryEntry() {
1186 // Each module summary entry consists of a tag for the entry
1187 // type, followed by a colon, then the fields which may be surrounded by
1188 // nested sets of parentheses. The "tag:" looks like a Label. Once parsing
1189 // support is in place we will look for the tokens corresponding to the
1190 // expected tags.
1191 if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module &&
1192 Lex.getKind() != lltok::kw_typeid &&
1193 Lex.getKind() != lltok::kw_typeidCompatibleVTable &&
1194 Lex.getKind() != lltok::kw_flags && Lex.getKind() != lltok::kw_blockcount)
1195 return tokError("Expected 'gv', 'module', 'typeid', "
1196 "'typeidCompatibleVTable', 'flags' or 'blockcount' at the "
1197 "start of summary entry");
1198 if (Lex.getKind() == lltok::kw_flags)
1199 return parseSummaryIndexFlags();
1200 if (Lex.getKind() == lltok::kw_blockcount)
1201 return parseBlockCount();
1202 Lex.Lex();
1203 if (parseToken(lltok::colon, "expected ':' at start of summary entry") ||
1204 parseToken(lltok::lparen, "expected '(' at start of summary entry"))
1205 return true;
1206 // Now walk through the parenthesized entry, until the number of open
1207 // parentheses goes back down to 0 (the first '(' was parsed above).
1208 unsigned NumOpenParen = 1;
1209 do {
1210 switch (Lex.getKind()) {
1211 case lltok::lparen:
1212 NumOpenParen++;
1213 break;
1214 case lltok::rparen:
1215 NumOpenParen--;
1216 break;
1217 case lltok::Eof:
1218 return tokError("found end of file while parsing summary entry");
1219 default:
1220 // Skip everything in between parentheses.
1221 break;
1222 }
1223 Lex.Lex();
1224 } while (NumOpenParen > 0);
1225 return false;
1226}
1227
1228/// SummaryEntry
1229/// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
1230bool LLParser::parseSummaryEntry() {
1231 assert(Lex.getKind() == lltok::SummaryID);
1232 unsigned SummaryID = Lex.getUIntVal();
1233
1234 // For summary entries, colons should be treated as distinct tokens,
1235 // not an indication of the end of a label token.
1236 Lex.setIgnoreColonInIdentifiers(true);
1237
1238 Lex.Lex();
1239 if (parseToken(lltok::equal, "expected '=' here"))
1240 return true;
1241
1242 // If we don't have an index object, skip the summary entry.
1243 if (!Index)
1244 return skipModuleSummaryEntry();
1245
1246 bool result = false;
1247 switch (Lex.getKind()) {
1248 case lltok::kw_gv:
1249 result = parseGVEntry(SummaryID);
1250 break;
1251 case lltok::kw_module:
1252 result = parseModuleEntry(SummaryID);
1253 break;
1254 case lltok::kw_typeid:
1255 result = parseTypeIdEntry(SummaryID);
1256 break;
1258 result = parseTypeIdCompatibleVtableEntry(SummaryID);
1259 break;
1260 case lltok::kw_flags:
1261 result = parseSummaryIndexFlags();
1262 break;
1264 result = parseBlockCount();
1265 break;
1266 default:
1267 result = error(Lex.getLoc(), "unexpected summary kind");
1268 break;
1269 }
1270 Lex.setIgnoreColonInIdentifiers(false);
1271 return result;
1272}
1273
1282
1283// If there was an explicit dso_local, update GV. In the absence of an explicit
1284// dso_local we keep the default value.
1285static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
1286 if (DSOLocal)
1287 GV.setDSOLocal(true);
1288}
1289
1290/// parseAliasOrIFunc:
1291/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1292/// OptionalVisibility OptionalDLLStorageClass
1293/// OptionalThreadLocal OptionalUnnamedAddr
1294/// 'alias|ifunc' AliaseeOrResolver SymbolAttrs*
1295///
1296/// AliaseeOrResolver
1297/// ::= TypeAndValue
1298///
1299/// SymbolAttrs
1300/// ::= ',' 'partition' StringConstant
1301///
1302/// Everything through OptionalUnnamedAddr has already been parsed.
1303///
1304bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID,
1305 LocTy NameLoc, unsigned L, unsigned Visibility,
1306 unsigned DLLStorageClass, bool DSOLocal,
1308 GlobalVariable::UnnamedAddr UnnamedAddr) {
1309 bool IsAlias;
1310 if (Lex.getKind() == lltok::kw_alias)
1311 IsAlias = true;
1312 else if (Lex.getKind() == lltok::kw_ifunc)
1313 IsAlias = false;
1314 else
1315 llvm_unreachable("Not an alias or ifunc!");
1316 Lex.Lex();
1317
1319
1320 if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
1321 return error(NameLoc, "invalid linkage type for alias");
1322
1323 if (!isValidVisibilityForLinkage(Visibility, L))
1324 return error(NameLoc,
1325 "symbol with local linkage must have default visibility");
1326
1327 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, L))
1328 return error(NameLoc,
1329 "symbol with local linkage cannot have a DLL storage class");
1330
1331 Type *Ty;
1332 LocTy ExplicitTypeLoc = Lex.getLoc();
1333 if (parseType(Ty) ||
1334 parseToken(lltok::comma, "expected comma after alias or ifunc's type"))
1335 return true;
1336
1337 Constant *Aliasee;
1338 LocTy AliaseeLoc = Lex.getLoc();
1339 if (Lex.getKind() != lltok::kw_bitcast &&
1340 Lex.getKind() != lltok::kw_getelementptr &&
1341 Lex.getKind() != lltok::kw_addrspacecast &&
1342 Lex.getKind() != lltok::kw_inttoptr) {
1343 if (parseGlobalTypeAndValue(Aliasee))
1344 return true;
1345 } else {
1346 // The bitcast dest type is not present, it is implied by the dest type.
1347 ValID ID;
1348 if (parseValID(ID, /*PFS=*/nullptr))
1349 return true;
1350 if (ID.Kind != ValID::t_Constant)
1351 return error(AliaseeLoc, "invalid aliasee");
1352 Aliasee = ID.ConstantVal;
1353 }
1354
1355 Type *AliaseeType = Aliasee->getType();
1356 auto *PTy = dyn_cast<PointerType>(AliaseeType);
1357 if (!PTy)
1358 return error(AliaseeLoc, "An alias or ifunc must have pointer type");
1359 unsigned AddrSpace = PTy->getAddressSpace();
1360
1361 GlobalValue *GVal = nullptr;
1362
1363 // See if the alias was forward referenced, if so, prepare to replace the
1364 // forward reference.
1365 if (!Name.empty()) {
1366 auto I = ForwardRefVals.find(Name);
1367 if (I != ForwardRefVals.end()) {
1368 GVal = I->second.first;
1369 ForwardRefVals.erase(Name);
1370 } else if (M->getNamedValue(Name)) {
1371 return error(NameLoc, "redefinition of global '@" + Name + "'");
1372 }
1373 } else {
1374 auto I = ForwardRefValIDs.find(NameID);
1375 if (I != ForwardRefValIDs.end()) {
1376 GVal = I->second.first;
1377 ForwardRefValIDs.erase(I);
1378 }
1379 }
1380
1381 // Okay, create the alias/ifunc but do not insert it into the module yet.
1382 std::unique_ptr<GlobalAlias> GA;
1383 std::unique_ptr<GlobalIFunc> GI;
1384 GlobalValue *GV;
1385 if (IsAlias) {
1386 GA.reset(GlobalAlias::create(Ty, AddrSpace, Linkage, Name, Aliasee,
1387 /*Parent=*/nullptr));
1388 GV = GA.get();
1389 } else {
1390 GI.reset(GlobalIFunc::create(Ty, AddrSpace, Linkage, Name, Aliasee,
1391 /*Parent=*/nullptr));
1392 GV = GI.get();
1393 }
1394 GV->setThreadLocalMode(TLM);
1397 GV->setUnnamedAddr(UnnamedAddr);
1398 maybeSetDSOLocal(DSOLocal, *GV);
1399
1400 // At this point we've parsed everything except for the IndirectSymbolAttrs.
1401 // Now parse them if there are any.
1402 while (Lex.getKind() == lltok::comma) {
1403 Lex.Lex();
1404
1405 if (Lex.getKind() == lltok::kw_partition) {
1406 Lex.Lex();
1407 GV->setPartition(Lex.getStrVal());
1408 if (parseToken(lltok::StringConstant, "expected partition string"))
1409 return true;
1410 } else if (!IsAlias && Lex.getKind() == lltok::MetadataVar) {
1411 if (parseGlobalObjectMetadataAttachment(*GI))
1412 return true;
1413 } else {
1414 return tokError("unknown alias or ifunc property!");
1415 }
1416 }
1417
1418 if (Name.empty())
1419 NumberedVals.add(NameID, GV);
1420
1421 if (GVal) {
1422 // Verify that types agree.
1423 if (GVal->getType() != GV->getType())
1424 return error(
1425 ExplicitTypeLoc,
1426 "forward reference and definition of alias have different types");
1427
1428 // If they agree, just RAUW the old value with the alias and remove the
1429 // forward ref info.
1430 GVal->replaceAllUsesWith(GV);
1431 GVal->eraseFromParent();
1432 }
1433
1434 // Insert into the module, we know its name won't collide now.
1435 if (IsAlias)
1436 M->insertAlias(GA.release());
1437 else
1438 M->insertIFunc(GI.release());
1439 assert(GV->getName() == Name && "Should not be a name conflict!");
1440
1441 return false;
1442}
1443
1444static bool isSanitizer(lltok::Kind Kind) {
1445 switch (Kind) {
1448 case lltok::kw_sanitize_memtag:
1450 return true;
1451 default:
1452 return false;
1453 }
1454}
1455
1456bool LLParser::parseSanitizer(GlobalVariable *GV) {
1457 using SanitizerMetadata = GlobalValue::SanitizerMetadata;
1459 if (GV->hasSanitizerMetadata())
1460 Meta = GV->getSanitizerMetadata();
1461
1462 switch (Lex.getKind()) {
1464 Meta.NoAddress = true;
1465 break;
1467 Meta.NoHWAddress = true;
1468 break;
1469 case lltok::kw_sanitize_memtag:
1470 Meta.Memtag = true;
1471 break;
1473 Meta.IsDynInit = true;
1474 break;
1475 default:
1476 return tokError("non-sanitizer token passed to LLParser::parseSanitizer()");
1477 }
1478 GV->setSanitizerMetadata(Meta);
1479 Lex.Lex();
1480 return false;
1481}
1482
1483/// parseGlobal
1484/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1485/// OptionalVisibility OptionalDLLStorageClass
1486/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
1487/// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
1488/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
1489/// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
1490/// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
1491/// Const OptionalAttrs
1492///
1493/// Everything up to and including OptionalUnnamedAddr has been parsed
1494/// already.
1495///
1496bool LLParser::parseGlobal(const std::string &Name, unsigned NameID,
1497 LocTy NameLoc, unsigned Linkage, bool HasLinkage,
1498 unsigned Visibility, unsigned DLLStorageClass,
1499 bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
1500 GlobalVariable::UnnamedAddr UnnamedAddr) {
1501 if (!isValidVisibilityForLinkage(Visibility, Linkage))
1502 return error(NameLoc,
1503 "symbol with local linkage must have default visibility");
1504
1505 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
1506 return error(NameLoc,
1507 "symbol with local linkage cannot have a DLL storage class");
1508
1509 unsigned AddrSpace;
1510 bool IsConstant, IsExternallyInitialized;
1511 LocTy IsExternallyInitializedLoc;
1512 LocTy TyLoc;
1513
1514 Type *Ty = nullptr;
1515 if (parseOptionalAddrSpace(AddrSpace) ||
1516 parseOptionalToken(lltok::kw_externally_initialized,
1517 IsExternallyInitialized,
1518 &IsExternallyInitializedLoc) ||
1519 parseGlobalType(IsConstant) || parseType(Ty, TyLoc))
1520 return true;
1521
1522 // If the linkage is specified and is external, then no initializer is
1523 // present.
1524 Constant *Init = nullptr;
1525 if (!HasLinkage ||
1528 if (parseGlobalValue(Ty, Init))
1529 return true;
1530 }
1531
1533 return error(TyLoc, "invalid type for global variable");
1534
1535 GlobalValue *GVal = nullptr;
1536
1537 // See if the global was forward referenced, if so, use the global.
1538 if (!Name.empty()) {
1539 auto I = ForwardRefVals.find(Name);
1540 if (I != ForwardRefVals.end()) {
1541 GVal = I->second.first;
1542 ForwardRefVals.erase(I);
1543 } else if (M->getNamedValue(Name)) {
1544 return error(NameLoc, "redefinition of global '@" + Name + "'");
1545 }
1546 } else {
1547 // Handle @"", where a name is syntactically specified, but semantically
1548 // missing.
1549 if (NameID == (unsigned)-1)
1550 NameID = NumberedVals.getNext();
1551
1552 auto I = ForwardRefValIDs.find(NameID);
1553 if (I != ForwardRefValIDs.end()) {
1554 GVal = I->second.first;
1555 ForwardRefValIDs.erase(I);
1556 }
1557 }
1558
1559 GlobalVariable *GV = new GlobalVariable(
1560 *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr,
1562
1563 if (Name.empty())
1564 NumberedVals.add(NameID, GV);
1565
1566 // Set the parsed properties on the global.
1567 if (Init)
1568 GV->setInitializer(Init);
1569 GV->setConstant(IsConstant);
1571 maybeSetDSOLocal(DSOLocal, *GV);
1574 GV->setExternallyInitialized(IsExternallyInitialized);
1575 GV->setThreadLocalMode(TLM);
1576 GV->setUnnamedAddr(UnnamedAddr);
1577
1578 if (GVal) {
1579 if (GVal->getAddressSpace() != AddrSpace)
1580 return error(
1581 TyLoc,
1582 "forward reference and definition of global have different types");
1583
1584 GVal->replaceAllUsesWith(GV);
1585 GVal->eraseFromParent();
1586 }
1587
1588 // parse attributes on the global.
1589 while (Lex.getKind() == lltok::comma) {
1590 Lex.Lex();
1591
1592 if (Lex.getKind() == lltok::kw_section) {
1593 Lex.Lex();
1594 GV->setSection(Lex.getStrVal());
1595 if (parseToken(lltok::StringConstant, "expected global section string"))
1596 return true;
1597 } else if (Lex.getKind() == lltok::kw_partition) {
1598 Lex.Lex();
1599 GV->setPartition(Lex.getStrVal());
1600 if (parseToken(lltok::StringConstant, "expected partition string"))
1601 return true;
1602 } else if (Lex.getKind() == lltok::kw_align) {
1603 MaybeAlign Alignment;
1604 if (parseOptionalAlignment(Alignment))
1605 return true;
1606 if (Alignment)
1607 GV->setAlignment(*Alignment);
1608 } else if (Lex.getKind() == lltok::kw_code_model) {
1610 if (parseOptionalCodeModel(CodeModel))
1611 return true;
1612 GV->setCodeModel(CodeModel);
1613 } else if (Lex.getKind() == lltok::MetadataVar) {
1614 if (parseGlobalObjectMetadataAttachment(*GV))
1615 return true;
1616 } else if (isSanitizer(Lex.getKind())) {
1617 if (parseSanitizer(GV))
1618 return true;
1619 } else {
1620 Comdat *C;
1621 if (parseOptionalComdat(Name, C))
1622 return true;
1623 if (C)
1624 GV->setComdat(C);
1625 else
1626 return tokError("unknown global variable property!");
1627 }
1628 }
1629
1630 AttrBuilder Attrs(M->getContext());
1631 LocTy BuiltinLoc;
1632 std::vector<unsigned> FwdRefAttrGrps;
1633 if (parseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
1634 return true;
1635 if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
1636 GV->setAttributes(AttributeSet::get(Context, Attrs));
1637 ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
1638 }
1639
1640 return false;
1641}
1642
1643/// parseUnnamedAttrGrp
1644/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
1645bool LLParser::parseUnnamedAttrGrp() {
1646 assert(Lex.getKind() == lltok::kw_attributes);
1647 LocTy AttrGrpLoc = Lex.getLoc();
1648 Lex.Lex();
1649
1650 if (Lex.getKind() != lltok::AttrGrpID)
1651 return tokError("expected attribute group id");
1652
1653 unsigned VarID = Lex.getUIntVal();
1654 std::vector<unsigned> unused;
1655 LocTy BuiltinLoc;
1656 Lex.Lex();
1657
1658 if (parseToken(lltok::equal, "expected '=' here") ||
1659 parseToken(lltok::lbrace, "expected '{' here"))
1660 return true;
1661
1662 auto R = NumberedAttrBuilders.find(VarID);
1663 if (R == NumberedAttrBuilders.end())
1664 R = NumberedAttrBuilders.emplace(VarID, AttrBuilder(M->getContext())).first;
1665
1666 if (parseFnAttributeValuePairs(R->second, unused, true, BuiltinLoc) ||
1667 parseToken(lltok::rbrace, "expected end of attribute group"))
1668 return true;
1669
1670 if (!R->second.hasAttributes())
1671 return error(AttrGrpLoc, "attribute group has no attributes");
1672
1673 return false;
1674}
1675
1677 switch (Kind) {
1678#define GET_ATTR_NAMES
1679#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
1680 case lltok::kw_##DISPLAY_NAME: \
1681 return Attribute::ENUM_NAME;
1682#include "llvm/IR/Attributes.inc"
1683 default:
1684 return Attribute::None;
1685 }
1686}
1687
1688bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
1689 bool InAttrGroup) {
1690 if (Attribute::isTypeAttrKind(Attr))
1691 return parseRequiredTypeAttr(B, Lex.getKind(), Attr);
1692
1693 switch (Attr) {
1694 case Attribute::Alignment: {
1695 MaybeAlign Alignment;
1696 if (InAttrGroup) {
1697 uint32_t Value = 0;
1698 Lex.Lex();
1699 if (parseToken(lltok::equal, "expected '=' here") || parseUInt32(Value))
1700 return true;
1702 } else {
1703 if (parseOptionalAlignment(Alignment, true))
1704 return true;
1705 }
1706 B.addAlignmentAttr(Alignment);
1707 return false;
1708 }
1709 case Attribute::StackAlignment: {
1710 unsigned Alignment;
1711 if (InAttrGroup) {
1712 Lex.Lex();
1713 if (parseToken(lltok::equal, "expected '=' here") ||
1714 parseUInt32(Alignment))
1715 return true;
1716 } else {
1717 if (parseOptionalStackAlignment(Alignment))
1718 return true;
1719 }
1720 B.addStackAlignmentAttr(Alignment);
1721 return false;
1722 }
1723 case Attribute::AllocSize: {
1724 unsigned ElemSizeArg;
1725 std::optional<unsigned> NumElemsArg;
1726 if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
1727 return true;
1728 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1729 return false;
1730 }
1731 case Attribute::VScaleRange: {
1732 unsigned MinValue, MaxValue;
1733 if (parseVScaleRangeArguments(MinValue, MaxValue))
1734 return true;
1735 B.addVScaleRangeAttr(MinValue,
1736 MaxValue > 0 ? MaxValue : std::optional<unsigned>());
1737 return false;
1738 }
1739 case Attribute::Dereferenceable: {
1740 std::optional<uint64_t> Bytes;
1741 if (parseOptionalAttrBytes(lltok::kw_dereferenceable, Bytes))
1742 return true;
1743 assert(Bytes.has_value());
1744 B.addDereferenceableAttr(Bytes.value());
1745 return false;
1746 }
1747 case Attribute::DeadOnReturn: {
1748 std::optional<uint64_t> Bytes;
1749 if (parseOptionalAttrBytes(lltok::kw_dead_on_return, Bytes,
1750 /*ErrorNoBytes=*/false))
1751 return true;
1752 if (Bytes.has_value()) {
1753 B.addDeadOnReturnAttr(DeadOnReturnInfo(Bytes.value()));
1754 } else {
1755 B.addDeadOnReturnAttr(DeadOnReturnInfo());
1756 }
1757 return false;
1758 }
1759 case Attribute::DereferenceableOrNull: {
1760 std::optional<uint64_t> Bytes;
1761 if (parseOptionalAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1762 return true;
1763 assert(Bytes.has_value());
1764 B.addDereferenceableOrNullAttr(Bytes.value());
1765 return false;
1766 }
1767 case Attribute::UWTable: {
1769 if (parseOptionalUWTableKind(Kind))
1770 return true;
1771 B.addUWTableAttr(Kind);
1772 return false;
1773 }
1774 case Attribute::AllocKind: {
1776 if (parseAllocKind(Kind))
1777 return true;
1778 B.addAllocKindAttr(Kind);
1779 return false;
1780 }
1781 case Attribute::Memory: {
1782 std::optional<MemoryEffects> ME = parseMemoryAttr();
1783 if (!ME)
1784 return true;
1785 B.addMemoryAttr(*ME);
1786 return false;
1787 }
1788 case Attribute::DenormalFPEnv: {
1789 std::optional<DenormalFPEnv> Mode = parseDenormalFPEnvAttr();
1790 if (!Mode)
1791 return true;
1792
1793 B.addDenormalFPEnvAttr(*Mode);
1794 return false;
1795 }
1796 case Attribute::NoFPClass: {
1797 if (FPClassTest NoFPClass =
1798 static_cast<FPClassTest>(parseNoFPClassAttr())) {
1799 B.addNoFPClassAttr(NoFPClass);
1800 return false;
1801 }
1802
1803 return true;
1804 }
1805 case Attribute::Range:
1806 return parseRangeAttr(B);
1807 case Attribute::Initializes:
1808 return parseInitializesAttr(B);
1809 case Attribute::Captures:
1810 return parseCapturesAttr(B);
1811 default:
1812 B.addAttribute(Attr);
1813 Lex.Lex();
1814 return false;
1815 }
1816}
1817
1819 switch (Kind) {
1820 case lltok::kw_readnone:
1821 ME &= MemoryEffects::none();
1822 return true;
1823 case lltok::kw_readonly:
1825 return true;
1826 case lltok::kw_writeonly:
1828 return true;
1831 return true;
1834 return true;
1837 return true;
1838 default:
1839 return false;
1840 }
1841}
1842
1843/// parseFnAttributeValuePairs
1844/// ::= <attr> | <attr> '=' <value>
1845bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
1846 std::vector<unsigned> &FwdRefAttrGrps,
1847 bool InAttrGrp, LocTy &BuiltinLoc) {
1848 bool HaveError = false;
1849
1850 B.clear();
1851
1853 while (true) {
1854 lltok::Kind Token = Lex.getKind();
1855 if (Token == lltok::rbrace)
1856 break; // Finished.
1857
1858 if (Token == lltok::StringConstant) {
1859 if (parseStringAttribute(B))
1860 return true;
1861 continue;
1862 }
1863
1864 if (Token == lltok::AttrGrpID) {
1865 // Allow a function to reference an attribute group:
1866 //
1867 // define void @foo() #1 { ... }
1868 if (InAttrGrp) {
1869 HaveError |= error(
1870 Lex.getLoc(),
1871 "cannot have an attribute group reference in an attribute group");
1872 } else {
1873 // Save the reference to the attribute group. We'll fill it in later.
1874 FwdRefAttrGrps.push_back(Lex.getUIntVal());
1875 }
1876 Lex.Lex();
1877 continue;
1878 }
1879
1880 SMLoc Loc = Lex.getLoc();
1881 if (Token == lltok::kw_builtin)
1882 BuiltinLoc = Loc;
1883
1884 if (upgradeMemoryAttr(ME, Token)) {
1885 Lex.Lex();
1886 continue;
1887 }
1888
1890 if (Attr == Attribute::None) {
1891 if (!InAttrGrp)
1892 break;
1893 return error(Lex.getLoc(), "unterminated attribute group");
1894 }
1895
1896 if (parseEnumAttribute(Attr, B, InAttrGrp))
1897 return true;
1898
1899 // As a hack, we allow function alignment to be initially parsed as an
1900 // attribute on a function declaration/definition or added to an attribute
1901 // group and later moved to the alignment field.
1902 if (!Attribute::canUseAsFnAttr(Attr) && Attr != Attribute::Alignment)
1903 HaveError |= error(Loc, "this attribute does not apply to functions");
1904 }
1905
1906 if (ME != MemoryEffects::unknown())
1907 B.addMemoryAttr(ME);
1908 return HaveError;
1909}
1910
1911//===----------------------------------------------------------------------===//
1912// GlobalValue Reference/Resolution Routines.
1913//===----------------------------------------------------------------------===//
1914
1916 // The used global type does not matter. We will later RAUW it with a
1917 // global/function of the correct type.
1918 return new GlobalVariable(*M, Type::getInt8Ty(M->getContext()), false,
1921 PTy->getAddressSpace());
1922}
1923
1924Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
1925 Value *Val) {
1926 Type *ValTy = Val->getType();
1927 if (ValTy == Ty)
1928 return Val;
1929 if (Ty->isLabelTy())
1930 error(Loc, "'" + Name + "' is not a basic block");
1931 else
1932 error(Loc, "'" + Name + "' defined with type '" +
1933 getTypeString(Val->getType()) + "' but expected '" +
1934 getTypeString(Ty) + "'");
1935 return nullptr;
1936}
1937
1938/// getGlobalVal - Get a value with the specified name or ID, creating a
1939/// forward reference record if needed. This can return null if the value
1940/// exists but does not have the right type.
1941GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty,
1942 LocTy Loc) {
1944 if (!PTy) {
1945 error(Loc, "global variable reference must have pointer type");
1946 return nullptr;
1947 }
1948
1949 // Look this name up in the normal function symbol table.
1950 GlobalValue *Val =
1951 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
1952
1953 // If this is a forward reference for the value, see if we already created a
1954 // forward ref record.
1955 if (!Val) {
1956 auto I = ForwardRefVals.find(Name);
1957 if (I != ForwardRefVals.end())
1958 Val = I->second.first;
1959 }
1960
1961 // If we have the value in the symbol table or fwd-ref table, return it.
1962 if (Val)
1964 checkValidVariableType(Loc, "@" + Name, Ty, Val));
1965
1966 // Otherwise, create a new forward reference for this value and remember it.
1967 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1968 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1969 return FwdVal;
1970}
1971
1972GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1974 if (!PTy) {
1975 error(Loc, "global variable reference must have pointer type");
1976 return nullptr;
1977 }
1978
1979 GlobalValue *Val = NumberedVals.get(ID);
1980
1981 // If this is a forward reference for the value, see if we already created a
1982 // forward ref record.
1983 if (!Val) {
1984 auto I = ForwardRefValIDs.find(ID);
1985 if (I != ForwardRefValIDs.end())
1986 Val = I->second.first;
1987 }
1988
1989 // If we have the value in the symbol table or fwd-ref table, return it.
1990 if (Val)
1992 checkValidVariableType(Loc, "@" + Twine(ID), Ty, Val));
1993
1994 // Otherwise, create a new forward reference for this value and remember it.
1995 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1996 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1997 return FwdVal;
1998}
1999
2000//===----------------------------------------------------------------------===//
2001// Comdat Reference/Resolution Routines.
2002//===----------------------------------------------------------------------===//
2003
2004Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
2005 // Look this name up in the comdat symbol table.
2006 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
2007 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
2008 if (I != ComdatSymTab.end())
2009 return &I->second;
2010
2011 // Otherwise, create a new forward reference for this value and remember it.
2012 Comdat *C = M->getOrInsertComdat(Name);
2013 ForwardRefComdats[Name] = Loc;
2014 return C;
2015}
2016
2017//===----------------------------------------------------------------------===//
2018// Helper Routines.
2019//===----------------------------------------------------------------------===//
2020
2021/// parseToken - If the current token has the specified kind, eat it and return
2022/// success. Otherwise, emit the specified error and return failure.
2023bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) {
2024 if (Lex.getKind() != T)
2025 return tokError(ErrMsg);
2026 Lex.Lex();
2027 return false;
2028}
2029
2030/// parseStringConstant
2031/// ::= StringConstant
2032bool LLParser::parseStringConstant(std::string &Result) {
2033 if (Lex.getKind() != lltok::StringConstant)
2034 return tokError("expected string constant");
2035 Result = Lex.getStrVal();
2036 Lex.Lex();
2037 return false;
2038}
2039
2040/// parseUInt32
2041/// ::= uint32
2042bool LLParser::parseUInt32(uint32_t &Val) {
2043 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
2044 return tokError("expected integer");
2045 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
2046 if (Val64 != unsigned(Val64))
2047 return tokError("expected 32-bit integer (too large)");
2048 Val = Val64;
2049 Lex.Lex();
2050 return false;
2051}
2052
2053/// parseUInt64
2054/// ::= uint64
2055bool LLParser::parseUInt64(uint64_t &Val) {
2056 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
2057 return tokError("expected integer");
2058 Val = Lex.getAPSIntVal().getLimitedValue();
2059 Lex.Lex();
2060 return false;
2061}
2062
2063/// parseTLSModel
2064/// := 'localdynamic'
2065/// := 'initialexec'
2066/// := 'localexec'
2067bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
2068 switch (Lex.getKind()) {
2069 default:
2070 return tokError("expected localdynamic, initialexec or localexec");
2073 break;
2076 break;
2079 break;
2080 }
2081
2082 Lex.Lex();
2083 return false;
2084}
2085
2086/// parseOptionalThreadLocal
2087/// := /*empty*/
2088/// := 'thread_local'
2089/// := 'thread_local' '(' tlsmodel ')'
2090bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
2092 if (!EatIfPresent(lltok::kw_thread_local))
2093 return false;
2094
2096 if (Lex.getKind() == lltok::lparen) {
2097 Lex.Lex();
2098 return parseTLSModel(TLM) ||
2099 parseToken(lltok::rparen, "expected ')' after thread local model");
2100 }
2101 return false;
2102}
2103
2104/// parseOptionalAddrSpace
2105/// := /*empty*/
2106/// := 'addrspace' '(' uint32 ')'
2107bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
2108 AddrSpace = DefaultAS;
2109 if (!EatIfPresent(lltok::kw_addrspace))
2110 return false;
2111
2112 auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool {
2113 if (Lex.getKind() == lltok::StringConstant) {
2114 const std::string &AddrSpaceStr = Lex.getStrVal();
2115 if (AddrSpaceStr == "A") {
2116 AddrSpace = M->getDataLayout().getAllocaAddrSpace();
2117 } else if (AddrSpaceStr == "G") {
2118 AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace();
2119 } else if (AddrSpaceStr == "P") {
2120 AddrSpace = M->getDataLayout().getProgramAddressSpace();
2121 } else if (std::optional<unsigned> AS =
2122 M->getDataLayout().getNamedAddressSpace(AddrSpaceStr)) {
2123 AddrSpace = *AS;
2124 } else {
2125 return tokError("invalid symbolic addrspace '" + AddrSpaceStr + "'");
2126 }
2127 Lex.Lex();
2128 return false;
2129 }
2130 if (Lex.getKind() != lltok::APSInt)
2131 return tokError("expected integer or string constant");
2132 SMLoc Loc = Lex.getLoc();
2133 if (parseUInt32(AddrSpace))
2134 return true;
2135 if (!isUInt<24>(AddrSpace))
2136 return error(Loc, "invalid address space, must be a 24-bit integer");
2137 return false;
2138 };
2139
2140 return parseToken(lltok::lparen, "expected '(' in address space") ||
2141 ParseAddrspaceValue(AddrSpace) ||
2142 parseToken(lltok::rparen, "expected ')' in address space");
2143}
2144
2145/// parseStringAttribute
2146/// := StringConstant
2147/// := StringConstant '=' StringConstant
2148bool LLParser::parseStringAttribute(AttrBuilder &B) {
2149 std::string Attr = Lex.getStrVal();
2150 Lex.Lex();
2151 std::string Val;
2152 if (EatIfPresent(lltok::equal) && parseStringConstant(Val))
2153 return true;
2154 B.addAttribute(Attr, Val);
2155 return false;
2156}
2157
2158/// Parse a potentially empty list of parameter or return attributes.
2159bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) {
2160 bool HaveError = false;
2161
2162 B.clear();
2163
2164 while (true) {
2165 lltok::Kind Token = Lex.getKind();
2166 if (Token == lltok::StringConstant) {
2167 if (parseStringAttribute(B))
2168 return true;
2169 continue;
2170 }
2171
2172 if (Token == lltok::kw_nocapture) {
2173 Lex.Lex();
2174 B.addCapturesAttr(CaptureInfo::none());
2175 continue;
2176 }
2177
2178 SMLoc Loc = Lex.getLoc();
2180 if (Attr == Attribute::None)
2181 return HaveError;
2182
2183 if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false))
2184 return true;
2185
2186 if (IsParam && !Attribute::canUseAsParamAttr(Attr))
2187 HaveError |= error(Loc, "this attribute does not apply to parameters");
2188 if (!IsParam && !Attribute::canUseAsRetAttr(Attr))
2189 HaveError |= error(Loc, "this attribute does not apply to return values");
2190 }
2191}
2192
2193static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
2194 HasLinkage = true;
2195 switch (Kind) {
2196 default:
2197 HasLinkage = false;
2199 case lltok::kw_private:
2201 case lltok::kw_internal:
2203 case lltok::kw_weak:
2205 case lltok::kw_weak_odr:
2207 case lltok::kw_linkonce:
2215 case lltok::kw_common:
2219 case lltok::kw_external:
2221 }
2222}
2223
2224/// parseOptionalLinkage
2225/// ::= /*empty*/
2226/// ::= 'private'
2227/// ::= 'internal'
2228/// ::= 'weak'
2229/// ::= 'weak_odr'
2230/// ::= 'linkonce'
2231/// ::= 'linkonce_odr'
2232/// ::= 'available_externally'
2233/// ::= 'appending'
2234/// ::= 'common'
2235/// ::= 'extern_weak'
2236/// ::= 'external'
2237bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
2238 unsigned &Visibility,
2239 unsigned &DLLStorageClass, bool &DSOLocal) {
2240 Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
2241 if (HasLinkage)
2242 Lex.Lex();
2243 parseOptionalDSOLocal(DSOLocal);
2244 parseOptionalVisibility(Visibility);
2245 parseOptionalDLLStorageClass(DLLStorageClass);
2246
2247 if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
2248 return error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
2249 }
2250
2251 return false;
2252}
2253
2254void LLParser::parseOptionalDSOLocal(bool &DSOLocal) {
2255 switch (Lex.getKind()) {
2256 default:
2257 DSOLocal = false;
2258 break;
2260 DSOLocal = true;
2261 Lex.Lex();
2262 break;
2264 DSOLocal = false;
2265 Lex.Lex();
2266 break;
2267 }
2268}
2269
2270/// parseOptionalVisibility
2271/// ::= /*empty*/
2272/// ::= 'default'
2273/// ::= 'hidden'
2274/// ::= 'protected'
2275///
2276void LLParser::parseOptionalVisibility(unsigned &Res) {
2277 switch (Lex.getKind()) {
2278 default:
2280 return;
2281 case lltok::kw_default:
2283 break;
2284 case lltok::kw_hidden:
2286 break;
2289 break;
2290 }
2291 Lex.Lex();
2292}
2293
2294bool LLParser::parseOptionalImportType(lltok::Kind Kind,
2296 switch (Kind) {
2297 default:
2298 return tokError("unknown import kind. Expect definition or declaration.");
2301 return false;
2304 return false;
2305 }
2306}
2307
2308/// parseOptionalDLLStorageClass
2309/// ::= /*empty*/
2310/// ::= 'dllimport'
2311/// ::= 'dllexport'
2312///
2313void LLParser::parseOptionalDLLStorageClass(unsigned &Res) {
2314 switch (Lex.getKind()) {
2315 default:
2317 return;
2320 break;
2323 break;
2324 }
2325 Lex.Lex();
2326}
2327
2328/// parseOptionalCallingConv
2329/// ::= /*empty*/
2330/// ::= 'ccc'
2331/// ::= 'fastcc'
2332/// ::= 'intel_ocl_bicc'
2333/// ::= 'coldcc'
2334/// ::= 'cfguard_checkcc'
2335/// ::= 'x86_stdcallcc'
2336/// ::= 'x86_fastcallcc'
2337/// ::= 'x86_thiscallcc'
2338/// ::= 'x86_vectorcallcc'
2339/// ::= 'arm_apcscc'
2340/// ::= 'arm_aapcscc'
2341/// ::= 'arm_aapcs_vfpcc'
2342/// ::= 'aarch64_vector_pcs'
2343/// ::= 'aarch64_sve_vector_pcs'
2344/// ::= 'aarch64_sme_preservemost_from_x0'
2345/// ::= 'aarch64_sme_preservemost_from_x1'
2346/// ::= 'aarch64_sme_preservemost_from_x2'
2347/// ::= 'msp430_intrcc'
2348/// ::= 'avr_intrcc'
2349/// ::= 'avr_signalcc'
2350/// ::= 'ptx_kernel'
2351/// ::= 'ptx_device'
2352/// ::= 'spir_func'
2353/// ::= 'spir_kernel'
2354/// ::= 'x86_64_sysvcc'
2355/// ::= 'win64cc'
2356/// ::= 'anyregcc'
2357/// ::= 'preserve_mostcc'
2358/// ::= 'preserve_allcc'
2359/// ::= 'preserve_nonecc'
2360/// ::= 'ghccc'
2361/// ::= 'swiftcc'
2362/// ::= 'swifttailcc'
2363/// ::= 'x86_intrcc'
2364/// ::= 'hhvmcc'
2365/// ::= 'hhvm_ccc'
2366/// ::= 'cxx_fast_tlscc'
2367/// ::= 'amdgpu_vs'
2368/// ::= 'amdgpu_ls'
2369/// ::= 'amdgpu_hs'
2370/// ::= 'amdgpu_es'
2371/// ::= 'amdgpu_gs'
2372/// ::= 'amdgpu_ps'
2373/// ::= 'amdgpu_cs'
2374/// ::= 'amdgpu_cs_chain'
2375/// ::= 'amdgpu_cs_chain_preserve'
2376/// ::= 'amdgpu_kernel'
2377/// ::= 'tailcc'
2378/// ::= 'm68k_rtdcc'
2379/// ::= 'graalcc'
2380/// ::= 'riscv_vector_cc'
2381/// ::= 'riscv_vls_cc'
2382/// ::= 'cc' UINT
2383///
2384bool LLParser::parseOptionalCallingConv(unsigned &CC) {
2385 switch (Lex.getKind()) {
2386 default: CC = CallingConv::C; return false;
2387 case lltok::kw_ccc: CC = CallingConv::C; break;
2388 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
2389 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
2402 break;
2405 break;
2408 break;
2411 break;
2421 case lltok::kw_win64cc: CC = CallingConv::Win64; break;
2422 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
2426 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
2427 case lltok::kw_swiftcc: CC = CallingConv::Swift; break;
2430 case lltok::kw_hhvmcc:
2432 break;
2433 case lltok::kw_hhvm_ccc:
2435 break;
2447 break;
2450 break;
2454 break;
2455 case lltok::kw_tailcc: CC = CallingConv::Tail; break;
2457 case lltok::kw_graalcc: CC = CallingConv::GRAAL; break;
2460 break;
2462 // Default ABI_VLEN
2464 Lex.Lex();
2465 if (!EatIfPresent(lltok::lparen))
2466 break;
2467 uint32_t ABIVlen;
2468 if (parseUInt32(ABIVlen) || !EatIfPresent(lltok::rparen))
2469 return true;
2470 switch (ABIVlen) {
2471 default:
2472 return tokError("unknown RISC-V ABI VLEN");
2473#define CC_VLS_CASE(ABIVlen) \
2474 case ABIVlen: \
2475 CC = CallingConv::RISCV_VLSCall_##ABIVlen; \
2476 break;
2477 CC_VLS_CASE(32)
2478 CC_VLS_CASE(64)
2479 CC_VLS_CASE(128)
2480 CC_VLS_CASE(256)
2481 CC_VLS_CASE(512)
2482 CC_VLS_CASE(1024)
2483 CC_VLS_CASE(2048)
2484 CC_VLS_CASE(4096)
2485 CC_VLS_CASE(8192)
2486 CC_VLS_CASE(16384)
2487 CC_VLS_CASE(32768)
2488 CC_VLS_CASE(65536)
2489#undef CC_VLS_CASE
2490 }
2491 return false;
2494 break;
2497 break;
2500 break;
2501 case lltok::kw_cc: {
2502 Lex.Lex();
2503 return parseUInt32(CC);
2504 }
2505 }
2506
2507 Lex.Lex();
2508 return false;
2509}
2510
2511/// parseMetadataAttachment
2512/// ::= !dbg !42
2513bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
2514 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
2515
2516 std::string Name = Lex.getStrVal();
2517 Kind = M->getMDKindID(Name);
2518 Lex.Lex();
2519
2520 return parseMDNode(MD);
2521}
2522
2523/// parseInstructionMetadata
2524/// ::= !dbg !42 (',' !dbg !57)*
2525bool LLParser::parseInstructionMetadata(Instruction &Inst) {
2526 do {
2527 if (Lex.getKind() != lltok::MetadataVar)
2528 return tokError("expected metadata after comma");
2529
2530 unsigned MDK;
2531 MDNode *N;
2532 auto Loc = Lex.getLoc();
2533 if (parseMetadataAttachment(MDK, N))
2534 return true;
2535
2536 if (MDK == LLVMContext::MD_DIAssignID)
2537 TempDIAssignIDAttachments[N].push_back(&Inst);
2538 else if (MDK == LLVMContext::MD_dbg)
2539 PendingDbgInsts.emplace_back(Loc, &Inst, N);
2540 else
2541 Inst.setMetadata(MDK, N);
2542
2543 // If this is the end of the list, we're done.
2544 } while (EatIfPresent(lltok::comma));
2545 return false;
2546}
2547
2548/// parseGlobalObjectMetadataAttachment
2549/// ::= !dbg !57
2550bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) {
2551 unsigned MDK;
2552 MDNode *N;
2553 if (parseMetadataAttachment(MDK, N))
2554 return true;
2555
2556 GO.addMetadata(MDK, *N);
2557 return false;
2558}
2559
2560/// parseOptionalFunctionMetadata
2561/// ::= (!dbg !57)*
2562bool LLParser::parseOptionalFunctionMetadata(Function &F) {
2563 while (Lex.getKind() == lltok::MetadataVar)
2564 if (parseGlobalObjectMetadataAttachment(F))
2565 return true;
2566 return false;
2567}
2568
2569/// parseOptionalAlignment
2570/// ::= /* empty */
2571/// ::= 'align' 4
2572bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
2573 Alignment = std::nullopt;
2574 if (!EatIfPresent(lltok::kw_align))
2575 return false;
2576 LocTy AlignLoc = Lex.getLoc();
2577 uint64_t Value = 0;
2578
2579 LocTy ParenLoc = Lex.getLoc();
2580 bool HaveParens = false;
2581 if (AllowParens) {
2582 if (EatIfPresent(lltok::lparen))
2583 HaveParens = true;
2584 }
2585
2586 if (parseUInt64(Value))
2587 return true;
2588
2589 if (HaveParens && !EatIfPresent(lltok::rparen))
2590 return error(ParenLoc, "expected ')'");
2591
2592 if (!isPowerOf2_64(Value))
2593 return error(AlignLoc, "alignment is not a power of two");
2595 return error(AlignLoc, "huge alignments are not supported yet");
2597 return false;
2598}
2599
2600/// parseOptionalPrefAlignment
2601/// ::= /* empty */
2602/// ::= 'prefalign' '(' 4 ')'
2603bool LLParser::parseOptionalPrefAlignment(MaybeAlign &Alignment) {
2604 Alignment = std::nullopt;
2605 if (!EatIfPresent(lltok::kw_prefalign))
2606 return false;
2607 LocTy AlignLoc = Lex.getLoc();
2608 uint64_t Value = 0;
2609
2610 LocTy ParenLoc = Lex.getLoc();
2611 if (!EatIfPresent(lltok::lparen))
2612 return error(ParenLoc, "expected '('");
2613
2614 if (parseUInt64(Value))
2615 return true;
2616
2617 ParenLoc = Lex.getLoc();
2618 if (!EatIfPresent(lltok::rparen))
2619 return error(ParenLoc, "expected ')'");
2620
2621 if (!isPowerOf2_64(Value))
2622 return error(AlignLoc, "alignment is not a power of two");
2624 return error(AlignLoc, "huge alignments are not supported yet");
2626 return false;
2627}
2628
2629/// parseOptionalCodeModel
2630/// ::= /* empty */
2631/// ::= 'code_model' "large"
2632bool LLParser::parseOptionalCodeModel(CodeModel::Model &model) {
2633 Lex.Lex();
2634 auto StrVal = Lex.getStrVal();
2635 auto ErrMsg = "expected global code model string";
2636 if (StrVal == "tiny")
2637 model = CodeModel::Tiny;
2638 else if (StrVal == "small")
2639 model = CodeModel::Small;
2640 else if (StrVal == "kernel")
2641 model = CodeModel::Kernel;
2642 else if (StrVal == "medium")
2643 model = CodeModel::Medium;
2644 else if (StrVal == "large")
2645 model = CodeModel::Large;
2646 else
2647 return tokError(ErrMsg);
2648 if (parseToken(lltok::StringConstant, ErrMsg))
2649 return true;
2650 return false;
2651}
2652
2653/// parseOptionalAttrBytes
2654/// ::= /* empty */
2655/// ::= AttrKind '(' 4 ')'
2656///
2657/// where AttrKind is either 'dereferenceable', 'dereferenceable_or_null', or
2658/// 'dead_on_return'
2659bool LLParser::parseOptionalAttrBytes(lltok::Kind AttrKind,
2660 std::optional<uint64_t> &Bytes,
2661 bool ErrorNoBytes) {
2662 assert((AttrKind == lltok::kw_dereferenceable ||
2663 AttrKind == lltok::kw_dereferenceable_or_null ||
2664 AttrKind == lltok::kw_dead_on_return) &&
2665 "contract!");
2666
2667 Bytes = 0;
2668 if (!EatIfPresent(AttrKind))
2669 return false;
2670 LocTy ParenLoc = Lex.getLoc();
2671 if (!EatIfPresent(lltok::lparen)) {
2672 if (ErrorNoBytes)
2673 return error(ParenLoc, "expected '('");
2674 Bytes = std::nullopt;
2675 return false;
2676 }
2677 LocTy DerefLoc = Lex.getLoc();
2678 if (parseUInt64(Bytes.value()))
2679 return true;
2680 ParenLoc = Lex.getLoc();
2681 if (!EatIfPresent(lltok::rparen))
2682 return error(ParenLoc, "expected ')'");
2683 if (!Bytes.value())
2684 return error(DerefLoc, "byte count specified must be non-zero");
2685 return false;
2686}
2687
2688bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) {
2689 Lex.Lex();
2691 if (!EatIfPresent(lltok::lparen))
2692 return false;
2693 LocTy KindLoc = Lex.getLoc();
2694 if (Lex.getKind() == lltok::kw_sync)
2696 else if (Lex.getKind() == lltok::kw_async)
2698 else
2699 return error(KindLoc, "expected unwind table kind");
2700 Lex.Lex();
2701 return parseToken(lltok::rparen, "expected ')'");
2702}
2703
2704bool LLParser::parseAllocKind(AllocFnKind &Kind) {
2705 Lex.Lex();
2706 LocTy ParenLoc = Lex.getLoc();
2707 if (!EatIfPresent(lltok::lparen))
2708 return error(ParenLoc, "expected '('");
2709 LocTy KindLoc = Lex.getLoc();
2710 std::string Arg;
2711 if (parseStringConstant(Arg))
2712 return error(KindLoc, "expected allockind value");
2713 for (StringRef A : llvm::split(Arg, ",")) {
2714 if (A == "alloc") {
2716 } else if (A == "realloc") {
2718 } else if (A == "free") {
2720 } else if (A == "uninitialized") {
2722 } else if (A == "zeroed") {
2724 } else if (A == "aligned") {
2726 } else {
2727 return error(KindLoc, Twine("unknown allockind ") + A);
2728 }
2729 }
2730 ParenLoc = Lex.getLoc();
2731 if (!EatIfPresent(lltok::rparen))
2732 return error(ParenLoc, "expected ')'");
2733 if (Kind == AllocFnKind::Unknown)
2734 return error(KindLoc, "expected allockind value");
2735 return false;
2736}
2737
2739 using Loc = IRMemLocation;
2740
2741 switch (Tok) {
2742 case lltok::kw_argmem:
2743 return {Loc::ArgMem};
2745 return {Loc::InaccessibleMem};
2746 case lltok::kw_errnomem:
2747 return {Loc::ErrnoMem};
2749 return {Loc::TargetMem0};
2751 return {Loc::TargetMem1};
2752 case lltok::kw_target_mem: {
2755 Targets.push_back(Loc);
2756 return Targets;
2757 }
2758 default:
2759 return {};
2760 }
2761}
2762
2763static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) {
2764 switch (Tok) {
2765 case lltok::kw_none:
2766 return ModRefInfo::NoModRef;
2767 case lltok::kw_read:
2768 return ModRefInfo::Ref;
2769 case lltok::kw_write:
2770 return ModRefInfo::Mod;
2772 return ModRefInfo::ModRef;
2773 default:
2774 return std::nullopt;
2775 }
2776}
2777
2778static std::optional<DenormalMode::DenormalModeKind>
2780 switch (Tok) {
2781 case lltok::kw_ieee:
2782 return DenormalMode::IEEE;
2787 case lltok::kw_dynamic:
2788 return DenormalMode::Dynamic;
2789 default:
2790 return std::nullopt;
2791 }
2792}
2793
2794std::optional<MemoryEffects> LLParser::parseMemoryAttr() {
2796
2797 // We use syntax like memory(argmem: read), so the colon should not be
2798 // interpreted as a label terminator.
2799 Lex.setIgnoreColonInIdentifiers(true);
2800 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
2801
2802 Lex.Lex();
2803 if (!EatIfPresent(lltok::lparen)) {
2804 tokError("expected '('");
2805 return std::nullopt;
2806 }
2807
2808 bool SeenLoc = false;
2809 bool SeenTargetLoc = false;
2810 do {
2811 SmallVector<IRMemLocation, 2> Locs = keywordToLoc(Lex.getKind());
2812 if (!Locs.empty()) {
2813 Lex.Lex();
2814 if (!EatIfPresent(lltok::colon)) {
2815 tokError("expected ':' after location");
2816 return std::nullopt;
2817 }
2818 }
2819
2820 std::optional<ModRefInfo> MR = keywordToModRef(Lex.getKind());
2821 if (!MR) {
2822 if (Locs.empty())
2823 tokError("expected memory location (argmem, inaccessiblemem, errnomem) "
2824 "or access kind (none, read, write, readwrite)");
2825 else
2826 tokError("expected access kind (none, read, write, readwrite)");
2827 return std::nullopt;
2828 }
2829
2830 Lex.Lex();
2831 if (!Locs.empty()) {
2832 SeenLoc = true;
2833 for (IRMemLocation Loc : Locs) {
2834 ME = ME.getWithModRef(Loc, *MR);
2835 if (ME.isTargetMemLoc(Loc) && Locs.size() == 1)
2836 SeenTargetLoc = true;
2837 }
2838 if (Locs.size() > 1 && SeenTargetLoc) {
2839 tokError("target memory default access kind must be specified first");
2840 return std::nullopt;
2841 }
2842
2843 } else {
2844 if (SeenLoc) {
2845 tokError("default access kind must be specified first");
2846 return std::nullopt;
2847 }
2848 ME = MemoryEffects(*MR);
2849 }
2850
2851 if (EatIfPresent(lltok::rparen))
2852 return ME;
2853 } while (EatIfPresent(lltok::comma));
2854
2855 tokError("unterminated memory attribute");
2856 return std::nullopt;
2857}
2858
2859std::optional<DenormalMode> LLParser::parseDenormalFPEnvEntry() {
2860 std::optional<DenormalMode::DenormalModeKind> OutputMode =
2861 keywordToDenormalModeKind(Lex.getKind());
2862 if (!OutputMode) {
2863 tokError("expected denormal behavior kind (ieee, preservesign, "
2864 "positivezero, dynamic)");
2865 return {};
2866 }
2867
2868 Lex.Lex();
2869
2870 std::optional<DenormalMode::DenormalModeKind> InputMode;
2871 if (EatIfPresent(lltok::bar)) {
2872 InputMode = keywordToDenormalModeKind(Lex.getKind());
2873 if (!InputMode) {
2874 tokError("expected denormal behavior kind (ieee, preservesign, "
2875 "positivezero, dynamic)");
2876 return {};
2877 }
2878
2879 Lex.Lex();
2880 } else {
2881 // Single item, input == output mode
2882 InputMode = OutputMode;
2883 }
2884
2885 return DenormalMode(*OutputMode, *InputMode);
2886}
2887
2888std::optional<DenormalFPEnv> LLParser::parseDenormalFPEnvAttr() {
2889 // We use syntax like denormal_fpenv(float: preservesign), so the colon should
2890 // not be interpreted as a label terminator.
2891 Lex.setIgnoreColonInIdentifiers(true);
2892 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
2893
2894 Lex.Lex();
2895
2896 if (parseToken(lltok::lparen, "expected '('"))
2897 return {};
2898
2899 DenormalMode DefaultMode = DenormalMode::getIEEE();
2900 DenormalMode F32Mode = DenormalMode::getInvalid();
2901
2902 bool HasDefaultSection = false;
2903 if (Lex.getKind() != lltok::Type) {
2904 std::optional<DenormalMode> ParsedDefaultMode = parseDenormalFPEnvEntry();
2905 if (!ParsedDefaultMode)
2906 return {};
2907 DefaultMode = *ParsedDefaultMode;
2908 HasDefaultSection = true;
2909 }
2910
2911 bool HasComma = EatIfPresent(lltok::comma);
2912 if (Lex.getKind() == lltok::Type) {
2913 if (HasDefaultSection && !HasComma) {
2914 tokError("expected ',' before float:");
2915 return {};
2916 }
2917
2918 Type *Ty = nullptr;
2919 if (parseType(Ty) || !Ty->isFloatTy()) {
2920 tokError("expected float:");
2921 return {};
2922 }
2923
2924 if (parseToken(lltok::colon, "expected ':' before float denormal_fpenv"))
2925 return {};
2926
2927 std::optional<DenormalMode> ParsedF32Mode = parseDenormalFPEnvEntry();
2928 if (!ParsedF32Mode)
2929 return {};
2930
2931 F32Mode = *ParsedF32Mode;
2932 }
2933
2934 if (parseToken(lltok::rparen, "unterminated denormal_fpenv"))
2935 return {};
2936
2937 return DenormalFPEnv(DefaultMode, F32Mode);
2938}
2939
2940static unsigned keywordToFPClassTest(lltok::Kind Tok) {
2941 switch (Tok) {
2942 case lltok::kw_all:
2943 return fcAllFlags;
2944 case lltok::kw_nan:
2945 return fcNan;
2946 case lltok::kw_snan:
2947 return fcSNan;
2948 case lltok::kw_qnan:
2949 return fcQNan;
2950 case lltok::kw_inf:
2951 return fcInf;
2952 case lltok::kw_ninf:
2953 return fcNegInf;
2954 case lltok::kw_pinf:
2955 return fcPosInf;
2956 case lltok::kw_norm:
2957 return fcNormal;
2958 case lltok::kw_nnorm:
2959 return fcNegNormal;
2960 case lltok::kw_pnorm:
2961 return fcPosNormal;
2962 case lltok::kw_sub:
2963 return fcSubnormal;
2964 case lltok::kw_nsub:
2965 return fcNegSubnormal;
2966 case lltok::kw_psub:
2967 return fcPosSubnormal;
2968 case lltok::kw_zero:
2969 return fcZero;
2970 case lltok::kw_nzero:
2971 return fcNegZero;
2972 case lltok::kw_pzero:
2973 return fcPosZero;
2974 default:
2975 return 0;
2976 }
2977}
2978
2979unsigned LLParser::parseNoFPClassAttr() {
2980 unsigned Mask = fcNone;
2981
2982 Lex.Lex();
2983 if (!EatIfPresent(lltok::lparen)) {
2984 tokError("expected '('");
2985 return 0;
2986 }
2987
2988 do {
2989 uint64_t Value = 0;
2990 unsigned TestMask = keywordToFPClassTest(Lex.getKind());
2991 if (TestMask != 0) {
2992 Mask |= TestMask;
2993 // TODO: Disallow overlapping masks to avoid copy paste errors
2994 } else if (Mask == 0 && Lex.getKind() == lltok::APSInt &&
2995 !parseUInt64(Value)) {
2996 if (Value == 0 || (Value & ~static_cast<unsigned>(fcAllFlags)) != 0) {
2997 error(Lex.getLoc(), "invalid mask value for 'nofpclass'");
2998 return 0;
2999 }
3000
3001 if (!EatIfPresent(lltok::rparen)) {
3002 error(Lex.getLoc(), "expected ')'");
3003 return 0;
3004 }
3005
3006 return Value;
3007 } else {
3008 error(Lex.getLoc(), "expected nofpclass test mask");
3009 return 0;
3010 }
3011
3012 Lex.Lex();
3013 if (EatIfPresent(lltok::rparen))
3014 return Mask;
3015 } while (1);
3016
3017 llvm_unreachable("unterminated nofpclass attribute");
3018}
3019
3020/// parseOptionalCommaAlign
3021/// ::=
3022/// ::= ',' align 4
3023///
3024/// This returns with AteExtraComma set to true if it ate an excess comma at the
3025/// end.
3026bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment,
3027 bool &AteExtraComma) {
3028 AteExtraComma = false;
3029 while (EatIfPresent(lltok::comma)) {
3030 // Metadata at the end is an early exit.
3031 if (Lex.getKind() == lltok::MetadataVar) {
3032 AteExtraComma = true;
3033 return false;
3034 }
3035
3036 if (Lex.getKind() != lltok::kw_align)
3037 return error(Lex.getLoc(), "expected metadata or 'align'");
3038
3039 if (parseOptionalAlignment(Alignment))
3040 return true;
3041 }
3042
3043 return false;
3044}
3045
3046/// parseOptionalCommaAddrSpace
3047/// ::=
3048/// ::= ',' addrspace(1)
3049///
3050/// This returns with AteExtraComma set to true if it ate an excess comma at the
3051/// end.
3052bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
3053 bool &AteExtraComma) {
3054 AteExtraComma = false;
3055 while (EatIfPresent(lltok::comma)) {
3056 // Metadata at the end is an early exit.
3057 if (Lex.getKind() == lltok::MetadataVar) {
3058 AteExtraComma = true;
3059 return false;
3060 }
3061
3062 Loc = Lex.getLoc();
3063 if (Lex.getKind() != lltok::kw_addrspace)
3064 return error(Lex.getLoc(), "expected metadata or 'addrspace'");
3065
3066 if (parseOptionalAddrSpace(AddrSpace))
3067 return true;
3068 }
3069
3070 return false;
3071}
3072
3073bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
3074 std::optional<unsigned> &HowManyArg) {
3075 Lex.Lex();
3076
3077 auto StartParen = Lex.getLoc();
3078 if (!EatIfPresent(lltok::lparen))
3079 return error(StartParen, "expected '('");
3080
3081 if (parseUInt32(BaseSizeArg))
3082 return true;
3083
3084 if (EatIfPresent(lltok::comma)) {
3085 auto HowManyAt = Lex.getLoc();
3086 unsigned HowMany;
3087 if (parseUInt32(HowMany))
3088 return true;
3089 if (HowMany == BaseSizeArg)
3090 return error(HowManyAt,
3091 "'allocsize' indices can't refer to the same parameter");
3092 HowManyArg = HowMany;
3093 } else
3094 HowManyArg = std::nullopt;
3095
3096 auto EndParen = Lex.getLoc();
3097 if (!EatIfPresent(lltok::rparen))
3098 return error(EndParen, "expected ')'");
3099 return false;
3100}
3101
3102bool LLParser::parseVScaleRangeArguments(unsigned &MinValue,
3103 unsigned &MaxValue) {
3104 Lex.Lex();
3105
3106 auto StartParen = Lex.getLoc();
3107 if (!EatIfPresent(lltok::lparen))
3108 return error(StartParen, "expected '('");
3109
3110 if (parseUInt32(MinValue))
3111 return true;
3112
3113 if (EatIfPresent(lltok::comma)) {
3114 if (parseUInt32(MaxValue))
3115 return true;
3116 } else
3117 MaxValue = MinValue;
3118
3119 auto EndParen = Lex.getLoc();
3120 if (!EatIfPresent(lltok::rparen))
3121 return error(EndParen, "expected ')'");
3122 return false;
3123}
3124
3125/// parseScopeAndOrdering
3126/// if isAtomic: ::= SyncScope? AtomicOrdering
3127/// else: ::=
3128///
3129/// This sets Scope and Ordering to the parsed values.
3130bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
3131 AtomicOrdering &Ordering) {
3132 if (!IsAtomic)
3133 return false;
3134
3135 return parseScope(SSID) || parseOrdering(Ordering);
3136}
3137
3138/// parseScope
3139/// ::= syncscope("singlethread" | "<target scope>")?
3140///
3141/// This sets synchronization scope ID to the ID of the parsed value.
3142bool LLParser::parseScope(SyncScope::ID &SSID) {
3143 SSID = SyncScope::System;
3144 if (EatIfPresent(lltok::kw_syncscope)) {
3145 auto StartParenAt = Lex.getLoc();
3146 if (!EatIfPresent(lltok::lparen))
3147 return error(StartParenAt, "Expected '(' in syncscope");
3148
3149 std::string SSN;
3150 auto SSNAt = Lex.getLoc();
3151 if (parseStringConstant(SSN))
3152 return error(SSNAt, "Expected synchronization scope name");
3153
3154 auto EndParenAt = Lex.getLoc();
3155 if (!EatIfPresent(lltok::rparen))
3156 return error(EndParenAt, "Expected ')' in syncscope");
3157
3158 SSID = Context.getOrInsertSyncScopeID(SSN);
3159 }
3160
3161 return false;
3162}
3163
3164/// parseOrdering
3165/// ::= AtomicOrdering
3166///
3167/// This sets Ordering to the parsed value.
3168bool LLParser::parseOrdering(AtomicOrdering &Ordering) {
3169 switch (Lex.getKind()) {
3170 default:
3171 return tokError("Expected ordering on atomic instruction");
3174 // Not specified yet:
3175 // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
3179 case lltok::kw_seq_cst:
3181 break;
3182 }
3183 Lex.Lex();
3184 return false;
3185}
3186
3187/// parseOptionalStackAlignment
3188/// ::= /* empty */
3189/// ::= 'alignstack' '(' 4 ')'
3190bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) {
3191 Alignment = 0;
3192 if (!EatIfPresent(lltok::kw_alignstack))
3193 return false;
3194 LocTy ParenLoc = Lex.getLoc();
3195 if (!EatIfPresent(lltok::lparen))
3196 return error(ParenLoc, "expected '('");
3197 LocTy AlignLoc = Lex.getLoc();
3198 if (parseUInt32(Alignment))
3199 return true;
3200 ParenLoc = Lex.getLoc();
3201 if (!EatIfPresent(lltok::rparen))
3202 return error(ParenLoc, "expected ')'");
3203 if (!isPowerOf2_32(Alignment))
3204 return error(AlignLoc, "stack alignment is not a power of two");
3205 return false;
3206}
3207
3208/// parseIndexList - This parses the index list for an insert/extractvalue
3209/// instruction. This sets AteExtraComma in the case where we eat an extra
3210/// comma at the end of the line and find that it is followed by metadata.
3211/// Clients that don't allow metadata can call the version of this function that
3212/// only takes one argument.
3213///
3214/// parseIndexList
3215/// ::= (',' uint32)+
3216///
3217bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices,
3218 bool &AteExtraComma) {
3219 AteExtraComma = false;
3220
3221 if (Lex.getKind() != lltok::comma)
3222 return tokError("expected ',' as start of index list");
3223
3224 while (EatIfPresent(lltok::comma)) {
3225 if (Lex.getKind() == lltok::MetadataVar) {
3226 if (Indices.empty())
3227 return tokError("expected index");
3228 AteExtraComma = true;
3229 return false;
3230 }
3231 unsigned Idx = 0;
3232 if (parseUInt32(Idx))
3233 return true;
3234 Indices.push_back(Idx);
3235 }
3236
3237 return false;
3238}
3239
3240//===----------------------------------------------------------------------===//
3241// Type Parsing.
3242//===----------------------------------------------------------------------===//
3243
3244/// parseType - parse a type.
3245bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
3246 SMLoc TypeLoc = Lex.getLoc();
3247 switch (Lex.getKind()) {
3248 default:
3249 return tokError(Msg);
3250 case lltok::Type:
3251 // Type ::= 'float' | 'void' (etc)
3252 Result = Lex.getTyVal();
3253 Lex.Lex();
3254
3255 // Handle "ptr" opaque pointer type.
3256 //
3257 // Type ::= ptr ('addrspace' '(' uint32 ')')?
3258 if (Result->isPointerTy()) {
3259 unsigned AddrSpace;
3260 if (parseOptionalAddrSpace(AddrSpace))
3261 return true;
3262 Result = PointerType::get(getContext(), AddrSpace);
3263
3264 // Give a nice error for 'ptr*'.
3265 if (Lex.getKind() == lltok::star)
3266 return tokError("ptr* is invalid - use ptr instead");
3267
3268 // Fall through to parsing the type suffixes only if this 'ptr' is a
3269 // function return. Otherwise, return success, implicitly rejecting other
3270 // suffixes.
3271 if (Lex.getKind() != lltok::lparen)
3272 return false;
3273 }
3274 break;
3275 case lltok::kw_target: {
3276 // Type ::= TargetExtType
3277 if (parseTargetExtType(Result))
3278 return true;
3279 break;
3280 }
3281 case lltok::lbrace:
3282 // Type ::= StructType
3283 if (parseAnonStructType(Result, false))
3284 return true;
3285 break;
3286 case lltok::lsquare:
3287 // Type ::= '[' ... ']'
3288 Lex.Lex(); // eat the lsquare.
3289 if (parseArrayVectorType(Result, false))
3290 return true;
3291 break;
3292 case lltok::less: // Either vector or packed struct.
3293 // Type ::= '<' ... '>'
3294 Lex.Lex();
3295 if (Lex.getKind() == lltok::lbrace) {
3296 if (parseAnonStructType(Result, true) ||
3297 parseToken(lltok::greater, "expected '>' at end of packed struct"))
3298 return true;
3299 } else if (parseArrayVectorType(Result, true))
3300 return true;
3301 break;
3302 case lltok::LocalVar: {
3303 // Type ::= %foo
3304 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
3305
3306 // If the type hasn't been defined yet, create a forward definition and
3307 // remember where that forward def'n was seen (in case it never is defined).
3308 if (!Entry.first) {
3309 Entry.first = StructType::create(Context, Lex.getStrVal());
3310 Entry.second = Lex.getLoc();
3311 }
3312 Result = Entry.first;
3313 Lex.Lex();
3314 break;
3315 }
3316
3317 case lltok::LocalVarID: {
3318 // Type ::= %4
3319 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
3320
3321 // If the type hasn't been defined yet, create a forward definition and
3322 // remember where that forward def'n was seen (in case it never is defined).
3323 if (!Entry.first) {
3324 Entry.first = StructType::create(Context);
3325 Entry.second = Lex.getLoc();
3326 }
3327 Result = Entry.first;
3328 Lex.Lex();
3329 break;
3330 }
3331 }
3332
3333 // parse the type suffixes.
3334 while (true) {
3335 switch (Lex.getKind()) {
3336 // End of type.
3337 default:
3338 if (!AllowVoid && Result->isVoidTy())
3339 return error(TypeLoc, "void type only allowed for function results");
3340 return false;
3341
3342 // Type ::= Type '*'
3343 case lltok::star:
3344 if (Result->isLabelTy())
3345 return tokError("basic block pointers are invalid");
3346 if (Result->isVoidTy())
3347 return tokError("pointers to void are invalid - use i8* instead");
3349 return tokError("pointer to this type is invalid");
3350 Result = PointerType::getUnqual(Context);
3351 Lex.Lex();
3352 break;
3353
3354 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
3355 case lltok::kw_addrspace: {
3356 if (Result->isLabelTy())
3357 return tokError("basic block pointers are invalid");
3358 if (Result->isVoidTy())
3359 return tokError("pointers to void are invalid; use i8* instead");
3361 return tokError("pointer to this type is invalid");
3362 unsigned AddrSpace;
3363 if (parseOptionalAddrSpace(AddrSpace) ||
3364 parseToken(lltok::star, "expected '*' in address space"))
3365 return true;
3366
3367 Result = PointerType::get(Context, AddrSpace);
3368 break;
3369 }
3370
3371 /// Types '(' ArgTypeListI ')' OptFuncAttrs
3372 case lltok::lparen:
3373 if (parseFunctionType(Result))
3374 return true;
3375 break;
3376 }
3377 }
3378}
3379
3380/// parseParameterList
3381/// ::= '(' ')'
3382/// ::= '(' Arg (',' Arg)* ')'
3383/// Arg
3384/// ::= Type OptionalAttributes Value OptionalAttributes
3385bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
3386 PerFunctionState &PFS, bool IsMustTailCall,
3387 bool InVarArgsFunc) {
3388 if (parseToken(lltok::lparen, "expected '(' in call"))
3389 return true;
3390
3391 while (Lex.getKind() != lltok::rparen) {
3392 // If this isn't the first argument, we need a comma.
3393 if (!ArgList.empty() &&
3394 parseToken(lltok::comma, "expected ',' in argument list"))
3395 return true;
3396
3397 // parse an ellipsis if this is a musttail call in a variadic function.
3398 if (Lex.getKind() == lltok::dotdotdot) {
3399 const char *Msg = "unexpected ellipsis in argument list for ";
3400 if (!IsMustTailCall)
3401 return tokError(Twine(Msg) + "non-musttail call");
3402 if (!InVarArgsFunc)
3403 return tokError(Twine(Msg) + "musttail call in non-varargs function");
3404 Lex.Lex(); // Lex the '...', it is purely for readability.
3405 return parseToken(lltok::rparen, "expected ')' at end of argument list");
3406 }
3407
3408 // parse the argument.
3409 LocTy ArgLoc;
3410 Type *ArgTy = nullptr;
3411 Value *V;
3412 if (parseType(ArgTy, ArgLoc))
3413 return true;
3415 return error(ArgLoc, "invalid type for function argument");
3416
3417 AttrBuilder ArgAttrs(M->getContext());
3418
3419 if (ArgTy->isMetadataTy()) {
3420 if (parseMetadataAsValue(V, PFS))
3421 return true;
3422 } else {
3423 // Otherwise, handle normal operands.
3424 if (parseOptionalParamAttrs(ArgAttrs) || parseValue(ArgTy, V, PFS))
3425 return true;
3426 }
3427 ArgList.push_back(ParamInfo(
3428 ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
3429 }
3430
3431 if (IsMustTailCall && InVarArgsFunc)
3432 return tokError("expected '...' at end of argument list for musttail call "
3433 "in varargs function");
3434
3435 Lex.Lex(); // Lex the ')'.
3436 return false;
3437}
3438
3439/// parseRequiredTypeAttr
3440/// ::= attrname(<ty>)
3441bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
3442 Attribute::AttrKind AttrKind) {
3443 Type *Ty = nullptr;
3444 if (!EatIfPresent(AttrToken))
3445 return true;
3446 if (!EatIfPresent(lltok::lparen))
3447 return error(Lex.getLoc(), "expected '('");
3448 if (parseType(Ty))
3449 return true;
3450 if (!EatIfPresent(lltok::rparen))
3451 return error(Lex.getLoc(), "expected ')'");
3452
3453 B.addTypeAttr(AttrKind, Ty);
3454 return false;
3455}
3456
3457/// parseRangeAttr
3458/// ::= range(<ty> <n>,<n>)
3459bool LLParser::parseRangeAttr(AttrBuilder &B) {
3460 Lex.Lex();
3461
3462 APInt Lower;
3463 APInt Upper;
3464 Type *Ty = nullptr;
3465 LocTy TyLoc;
3466
3467 auto ParseAPSInt = [&](unsigned BitWidth, APInt &Val) {
3468 if (Lex.getKind() != lltok::APSInt)
3469 return tokError("expected integer");
3470 if (Lex.getAPSIntVal().getBitWidth() > BitWidth)
3471 return tokError(
3472 "integer is too large for the bit width of specified type");
3473 Val = Lex.getAPSIntVal().extend(BitWidth);
3474 Lex.Lex();
3475 return false;
3476 };
3477
3478 if (parseToken(lltok::lparen, "expected '('") || parseType(Ty, TyLoc))
3479 return true;
3480 if (!Ty->isIntegerTy())
3481 return error(TyLoc, "the range must have integer type!");
3482
3483 unsigned BitWidth = Ty->getPrimitiveSizeInBits();
3484
3485 if (ParseAPSInt(BitWidth, Lower) ||
3486 parseToken(lltok::comma, "expected ','") || ParseAPSInt(BitWidth, Upper))
3487 return true;
3488 if (Lower == Upper && !Lower.isZero())
3489 return tokError("the range represent the empty set but limits aren't 0!");
3490
3491 if (parseToken(lltok::rparen, "expected ')'"))
3492 return true;
3493
3494 B.addRangeAttr(ConstantRange(Lower, Upper));
3495 return false;
3496}
3497
3498/// parseInitializesAttr
3499/// ::= initializes((Lo1,Hi1),(Lo2,Hi2),...)
3500bool LLParser::parseInitializesAttr(AttrBuilder &B) {
3501 Lex.Lex();
3502
3503 auto ParseAPSInt = [&](APInt &Val) {
3504 if (Lex.getKind() != lltok::APSInt)
3505 return tokError("expected integer");
3506 Val = Lex.getAPSIntVal().extend(64);
3507 Lex.Lex();
3508 return false;
3509 };
3510
3511 if (parseToken(lltok::lparen, "expected '('"))
3512 return true;
3513
3515 // Parse each constant range.
3516 do {
3517 APInt Lower, Upper;
3518 if (parseToken(lltok::lparen, "expected '('"))
3519 return true;
3520
3521 if (ParseAPSInt(Lower) || parseToken(lltok::comma, "expected ','") ||
3522 ParseAPSInt(Upper))
3523 return true;
3524
3525 if (Lower == Upper)
3526 return tokError("the range should not represent the full or empty set!");
3527
3528 if (parseToken(lltok::rparen, "expected ')'"))
3529 return true;
3530
3531 RangeList.push_back(ConstantRange(Lower, Upper));
3532 } while (EatIfPresent(lltok::comma));
3533
3534 if (parseToken(lltok::rparen, "expected ')'"))
3535 return true;
3536
3537 auto CRLOrNull = ConstantRangeList::getConstantRangeList(RangeList);
3538 if (!CRLOrNull.has_value())
3539 return tokError("Invalid (unordered or overlapping) range list");
3540 B.addInitializesAttr(*CRLOrNull);
3541 return false;
3542}
3543
3544bool LLParser::parseCapturesAttr(AttrBuilder &B) {
3546 std::optional<CaptureComponents> Ret;
3547
3548 // We use syntax like captures(ret: address, provenance), so the colon
3549 // should not be interpreted as a label terminator.
3550 Lex.setIgnoreColonInIdentifiers(true);
3551 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
3552
3553 Lex.Lex();
3554 if (parseToken(lltok::lparen, "expected '('"))
3555 return true;
3556
3557 CaptureComponents *Current = &Other;
3558 bool SeenComponent = false;
3559 while (true) {
3560 if (EatIfPresent(lltok::kw_ret)) {
3561 if (parseToken(lltok::colon, "expected ':'"))
3562 return true;
3563 if (Ret)
3564 return tokError("duplicate 'ret' location");
3566 Current = &*Ret;
3567 SeenComponent = false;
3568 }
3569
3570 if (EatIfPresent(lltok::kw_none)) {
3571 if (SeenComponent)
3572 return tokError("cannot use 'none' with other component");
3573 *Current = CaptureComponents::None;
3574 } else {
3575 if (SeenComponent && capturesNothing(*Current))
3576 return tokError("cannot use 'none' with other component");
3577
3578 if (EatIfPresent(lltok::kw_address_is_null))
3580 else if (EatIfPresent(lltok::kw_address))
3581 *Current |= CaptureComponents::Address;
3582 else if (EatIfPresent(lltok::kw_provenance))
3584 else if (EatIfPresent(lltok::kw_read_provenance))
3586 else
3587 return tokError("expected one of 'none', 'address', 'address_is_null', "
3588 "'provenance' or 'read_provenance'");
3589 }
3590
3591 SeenComponent = true;
3592 if (EatIfPresent(lltok::rparen))
3593 break;
3594
3595 if (parseToken(lltok::comma, "expected ',' or ')'"))
3596 return true;
3597 }
3598
3599 B.addCapturesAttr(CaptureInfo(Other, Ret.value_or(Other)));
3600 return false;
3601}
3602
3603/// parseOptionalOperandBundles
3604/// ::= /*empty*/
3605/// ::= '[' OperandBundle [, OperandBundle ]* ']'
3606///
3607/// OperandBundle
3608/// ::= bundle-tag '(' ')'
3609/// ::= bundle-tag '(' Type Value [, Type Value ]* ')'
3610///
3611/// bundle-tag ::= String Constant
3612bool LLParser::parseOptionalOperandBundles(
3613 SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
3614 LocTy BeginLoc = Lex.getLoc();
3615 if (!EatIfPresent(lltok::lsquare))
3616 return false;
3617
3618 while (Lex.getKind() != lltok::rsquare) {
3619 // If this isn't the first operand bundle, we need a comma.
3620 if (!BundleList.empty() &&
3621 parseToken(lltok::comma, "expected ',' in input list"))
3622 return true;
3623
3624 std::string Tag;
3625 if (parseStringConstant(Tag))
3626 return true;
3627
3628 if (parseToken(lltok::lparen, "expected '(' in operand bundle"))
3629 return true;
3630
3631 std::vector<Value *> Inputs;
3632 while (Lex.getKind() != lltok::rparen) {
3633 // If this isn't the first input, we need a comma.
3634 if (!Inputs.empty() &&
3635 parseToken(lltok::comma, "expected ',' in input list"))
3636 return true;
3637
3638 Type *Ty = nullptr;
3639 Value *Input = nullptr;
3640 if (parseType(Ty))
3641 return true;
3642 if (Ty->isMetadataTy()) {
3643 if (parseMetadataAsValue(Input, PFS))
3644 return true;
3645 } else if (parseValue(Ty, Input, PFS)) {
3646 return true;
3647 }
3648 Inputs.push_back(Input);
3649 }
3650
3651 BundleList.emplace_back(std::move(Tag), std::move(Inputs));
3652
3653 Lex.Lex(); // Lex the ')'.
3654 }
3655
3656 if (BundleList.empty())
3657 return error(BeginLoc, "operand bundle set must not be empty");
3658
3659 Lex.Lex(); // Lex the ']'.
3660 return false;
3661}
3662
3663bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix,
3664 unsigned NextID, unsigned ID) {
3665 if (ID < NextID)
3666 return error(Loc, Kind + " expected to be numbered '" + Prefix +
3667 Twine(NextID) + "' or greater");
3668
3669 return false;
3670}
3671
3672/// parseArgumentList - parse the argument list for a function type or function
3673/// prototype.
3674/// ::= '(' ArgTypeListI ')'
3675/// ArgTypeListI
3676/// ::= /*empty*/
3677/// ::= '...'
3678/// ::= ArgTypeList ',' '...'
3679/// ::= ArgType (',' ArgType)*
3680///
3681bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
3682 SmallVectorImpl<unsigned> &UnnamedArgNums,
3683 bool &IsVarArg) {
3684 unsigned CurValID = 0;
3685 IsVarArg = false;
3686 assert(Lex.getKind() == lltok::lparen);
3687 Lex.Lex(); // eat the (.
3688
3689 if (Lex.getKind() != lltok::rparen) {
3690 do {
3691 // Handle ... at end of arg list.
3692 if (EatIfPresent(lltok::dotdotdot)) {
3693 IsVarArg = true;
3694 break;
3695 }
3696
3697 // Otherwise must be an argument type.
3698 LocTy TypeLoc = Lex.getLoc();
3699 Type *ArgTy = nullptr;
3700 AttrBuilder Attrs(M->getContext());
3701 if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
3702 return true;
3703
3704 if (ArgTy->isVoidTy())
3705 return error(TypeLoc, "argument can not have void type");
3706
3707 std::string Name;
3708 FileLoc IdentStart;
3709 FileLoc IdentEnd;
3710 bool Unnamed = false;
3711 if (Lex.getKind() == lltok::LocalVar) {
3712 Name = Lex.getStrVal();
3713 IdentStart = getTokLineColumnPos();
3714 Lex.Lex();
3715 IdentEnd = getPrevTokEndLineColumnPos();
3716 } else {
3717 unsigned ArgID;
3718 if (Lex.getKind() == lltok::LocalVarID) {
3719 ArgID = Lex.getUIntVal();
3720 IdentStart = getTokLineColumnPos();
3721 if (checkValueID(TypeLoc, "argument", "%", CurValID, ArgID))
3722 return true;
3723 Lex.Lex();
3724 IdentEnd = getPrevTokEndLineColumnPos();
3725 } else {
3726 ArgID = CurValID;
3727 Unnamed = true;
3728 }
3729 UnnamedArgNums.push_back(ArgID);
3730 CurValID = ArgID + 1;
3731 }
3732
3734 return error(TypeLoc, "invalid type for function argument");
3735
3736 ArgList.emplace_back(
3737 TypeLoc, ArgTy,
3738 Unnamed ? std::nullopt
3739 : std::make_optional(FileLocRange(IdentStart, IdentEnd)),
3740 AttributeSet::get(ArgTy->getContext(), Attrs), std::move(Name));
3741 } while (EatIfPresent(lltok::comma));
3742 }
3743
3744 return parseToken(lltok::rparen, "expected ')' at end of argument list");
3745}
3746
3747/// parseFunctionType
3748/// ::= Type ArgumentList OptionalAttrs
3749bool LLParser::parseFunctionType(Type *&Result) {
3750 assert(Lex.getKind() == lltok::lparen);
3751
3753 return tokError("invalid function return type");
3754
3756 bool IsVarArg;
3757 SmallVector<unsigned> UnnamedArgNums;
3758 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg))
3759 return true;
3760
3761 // Reject names on the arguments lists.
3762 for (const ArgInfo &Arg : ArgList) {
3763 if (!Arg.Name.empty())
3764 return error(Arg.Loc, "argument name invalid in function type");
3765 if (Arg.Attrs.hasAttributes())
3766 return error(Arg.Loc, "argument attributes invalid in function type");
3767 }
3768
3769 SmallVector<Type*, 16> ArgListTy;
3770 for (const ArgInfo &Arg : ArgList)
3771 ArgListTy.push_back(Arg.Ty);
3772
3773 Result = FunctionType::get(Result, ArgListTy, IsVarArg);
3774 return false;
3775}
3776
3777/// parseAnonStructType - parse an anonymous struct type, which is inlined into
3778/// other structs.
3779bool LLParser::parseAnonStructType(Type *&Result, bool Packed) {
3781 if (parseStructBody(Elts))
3782 return true;
3783
3784 Result = StructType::get(Context, Elts, Packed);
3785 return false;
3786}
3787
3788/// parseStructDefinition - parse a struct in a 'type' definition.
3789bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name,
3790 std::pair<Type *, LocTy> &Entry,
3791 Type *&ResultTy) {
3792 // If the type was already defined, diagnose the redefinition.
3793 if (Entry.first && !Entry.second.isValid())
3794 return error(TypeLoc, "redefinition of type");
3795
3796 // If we have opaque, just return without filling in the definition for the
3797 // struct. This counts as a definition as far as the .ll file goes.
3798 if (EatIfPresent(lltok::kw_opaque)) {
3799 // This type is being defined, so clear the location to indicate this.
3800 Entry.second = SMLoc();
3801
3802 // If this type number has never been uttered, create it.
3803 if (!Entry.first)
3804 Entry.first = StructType::create(Context, Name);
3805 ResultTy = Entry.first;
3806 return false;
3807 }
3808
3809 // If the type starts with '<', then it is either a packed struct or a vector.
3810 bool isPacked = EatIfPresent(lltok::less);
3811
3812 // If we don't have a struct, then we have a random type alias, which we
3813 // accept for compatibility with old files. These types are not allowed to be
3814 // forward referenced and not allowed to be recursive.
3815 if (Lex.getKind() != lltok::lbrace) {
3816 if (Entry.first)
3817 return error(TypeLoc, "forward references to non-struct type");
3818
3819 ResultTy = nullptr;
3820 if (isPacked)
3821 return parseArrayVectorType(ResultTy, true);
3822 return parseType(ResultTy);
3823 }
3824
3825 // This type is being defined, so clear the location to indicate this.
3826 Entry.second = SMLoc();
3827
3828 // If this type number has never been uttered, create it.
3829 if (!Entry.first)
3830 Entry.first = StructType::create(Context, Name);
3831
3832 StructType *STy = cast<StructType>(Entry.first);
3833
3835 if (parseStructBody(Body) ||
3836 (isPacked && parseToken(lltok::greater, "expected '>' in packed struct")))
3837 return true;
3838
3839 if (auto E = STy->setBodyOrError(Body, isPacked))
3840 return tokError(toString(std::move(E)));
3841
3842 ResultTy = STy;
3843 return false;
3844}
3845
3846/// parseStructType: Handles packed and unpacked types. </> parsed elsewhere.
3847/// StructType
3848/// ::= '{' '}'
3849/// ::= '{' Type (',' Type)* '}'
3850/// ::= '<' '{' '}' '>'
3851/// ::= '<' '{' Type (',' Type)* '}' '>'
3852bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) {
3853 assert(Lex.getKind() == lltok::lbrace);
3854 Lex.Lex(); // Consume the '{'
3855
3856 // Handle the empty struct.
3857 if (EatIfPresent(lltok::rbrace))
3858 return false;
3859
3860 LocTy EltTyLoc = Lex.getLoc();
3861 Type *Ty = nullptr;
3862 if (parseType(Ty))
3863 return true;
3864 Body.push_back(Ty);
3865
3867 return error(EltTyLoc, "invalid element type for struct");
3868
3869 while (EatIfPresent(lltok::comma)) {
3870 EltTyLoc = Lex.getLoc();
3871 if (parseType(Ty))
3872 return true;
3873
3875 return error(EltTyLoc, "invalid element type for struct");
3876
3877 Body.push_back(Ty);
3878 }
3879
3880 return parseToken(lltok::rbrace, "expected '}' at end of struct");
3881}
3882
3883/// parseArrayVectorType - parse an array or vector type, assuming the first
3884/// token has already been consumed.
3885/// Type
3886/// ::= '[' APSINTVAL 'x' Types ']'
3887/// ::= '<' APSINTVAL 'x' Types '>'
3888/// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
3889bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) {
3890 bool Scalable = false;
3891
3892 if (IsVector && Lex.getKind() == lltok::kw_vscale) {
3893 Lex.Lex(); // consume the 'vscale'
3894 if (parseToken(lltok::kw_x, "expected 'x' after vscale"))
3895 return true;
3896
3897 Scalable = true;
3898 }
3899
3900 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
3901 Lex.getAPSIntVal().getBitWidth() > 64)
3902 return tokError("expected number in address space");
3903
3904 LocTy SizeLoc = Lex.getLoc();
3905 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
3906 Lex.Lex();
3907
3908 if (parseToken(lltok::kw_x, "expected 'x' after element count"))
3909 return true;
3910
3911 LocTy TypeLoc = Lex.getLoc();
3912 Type *EltTy = nullptr;
3913 if (parseType(EltTy))
3914 return true;
3915
3916 if (parseToken(IsVector ? lltok::greater : lltok::rsquare,
3917 "expected end of sequential type"))
3918 return true;
3919
3920 if (IsVector) {
3921 if (Size == 0)
3922 return error(SizeLoc, "zero element vector is illegal");
3923 if ((unsigned)Size != Size)
3924 return error(SizeLoc, "size too large for vector");
3926 return error(TypeLoc, "invalid vector element type");
3927 Result = VectorType::get(EltTy, unsigned(Size), Scalable);
3928 } else {
3930 return error(TypeLoc, "invalid array element type");
3931 Result = ArrayType::get(EltTy, Size);
3932 }
3933 return false;
3934}
3935
3936/// parseTargetExtType - handle target extension type syntax
3937/// TargetExtType
3938/// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')'
3939///
3940/// TargetExtTypeParams
3941/// ::= /*empty*/
3942/// ::= ',' Type TargetExtTypeParams
3943///
3944/// TargetExtIntParams
3945/// ::= /*empty*/
3946/// ::= ',' uint32 TargetExtIntParams
3947bool LLParser::parseTargetExtType(Type *&Result) {
3948 Lex.Lex(); // Eat the 'target' keyword.
3949
3950 // Get the mandatory type name.
3951 std::string TypeName;
3952 if (parseToken(lltok::lparen, "expected '(' in target extension type") ||
3953 parseStringConstant(TypeName))
3954 return true;
3955
3956 // Parse all of the integer and type parameters at the same time; the use of
3957 // SeenInt will allow us to catch cases where type parameters follow integer
3958 // parameters.
3959 SmallVector<Type *> TypeParams;
3960 SmallVector<unsigned> IntParams;
3961 bool SeenInt = false;
3962 while (Lex.getKind() == lltok::comma) {
3963 Lex.Lex(); // Eat the comma.
3964
3965 if (Lex.getKind() == lltok::APSInt) {
3966 SeenInt = true;
3967 unsigned IntVal;
3968 if (parseUInt32(IntVal))
3969 return true;
3970 IntParams.push_back(IntVal);
3971 } else if (SeenInt) {
3972 // The only other kind of parameter we support is type parameters, which
3973 // must precede the integer parameters. This is therefore an error.
3974 return tokError("expected uint32 param");
3975 } else {
3976 Type *TypeParam;
3977 if (parseType(TypeParam, /*AllowVoid=*/true))
3978 return true;
3979 TypeParams.push_back(TypeParam);
3980 }
3981 }
3982
3983 if (parseToken(lltok::rparen, "expected ')' in target extension type"))
3984 return true;
3985
3986 auto TTy =
3987 TargetExtType::getOrError(Context, TypeName, TypeParams, IntParams);
3988 if (auto E = TTy.takeError())
3989 return tokError(toString(std::move(E)));
3990
3991 Result = *TTy;
3992 return false;
3993}
3994
3995//===----------------------------------------------------------------------===//
3996// Function Semantic Analysis.
3997//===----------------------------------------------------------------------===//
3998
3999LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
4000 int functionNumber,
4001 ArrayRef<unsigned> UnnamedArgNums)
4002 : P(p), F(f), FunctionNumber(functionNumber) {
4003
4004 // Insert unnamed arguments into the NumberedVals list.
4005 auto It = UnnamedArgNums.begin();
4006 for (Argument &A : F.args()) {
4007 if (!A.hasName()) {
4008 unsigned ArgNum = *It++;
4009 NumberedVals.add(ArgNum, &A);
4010 }
4011 }
4012}
4013
4014LLParser::PerFunctionState::~PerFunctionState() {
4015 // If there were any forward referenced non-basicblock values, delete them.
4016
4017 for (const auto &P : ForwardRefVals) {
4018 if (isa<BasicBlock>(P.second.first))
4019 continue;
4020 P.second.first->replaceAllUsesWith(
4021 PoisonValue::get(P.second.first->getType()));
4022 P.second.first->deleteValue();
4023 }
4024
4025 for (const auto &P : ForwardRefValIDs) {
4026 if (isa<BasicBlock>(P.second.first))
4027 continue;
4028 P.second.first->replaceAllUsesWith(
4029 PoisonValue::get(P.second.first->getType()));
4030 P.second.first->deleteValue();
4031 }
4032}
4033
4034bool LLParser::PerFunctionState::finishFunction() {
4035 if (!ForwardRefVals.empty())
4036 return P.error(ForwardRefVals.begin()->second.second,
4037 "use of undefined value '%" + ForwardRefVals.begin()->first +
4038 "'");
4039 if (!ForwardRefValIDs.empty())
4040 return P.error(ForwardRefValIDs.begin()->second.second,
4041 "use of undefined value '%" +
4042 Twine(ForwardRefValIDs.begin()->first) + "'");
4043 return false;
4044}
4045
4046/// getVal - Get a value with the specified name or ID, creating a
4047/// forward reference record if needed. This can return null if the value
4048/// exists but does not have the right type.
4049Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty,
4050 LocTy Loc) {
4051 // Look this name up in the normal function symbol table.
4052 Value *Val = F.getValueSymbolTable()->lookup(Name);
4053
4054 // If this is a forward reference for the value, see if we already created a
4055 // forward ref record.
4056 if (!Val) {
4057 auto I = ForwardRefVals.find(Name);
4058 if (I != ForwardRefVals.end())
4059 Val = I->second.first;
4060 }
4061
4062 // If we have the value in the symbol table or fwd-ref table, return it.
4063 if (Val)
4064 return P.checkValidVariableType(Loc, "%" + Name, Ty, Val);
4065
4066 // Don't make placeholders with invalid type.
4067 if (!Ty->isFirstClassType()) {
4068 P.error(Loc, "invalid use of a non-first-class type");
4069 return nullptr;
4070 }
4071
4072 // Otherwise, create a new forward reference for this value and remember it.
4073 Value *FwdVal;
4074 if (Ty->isLabelTy()) {
4075 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
4076 } else {
4077 FwdVal = new Argument(Ty, Name);
4078 }
4079 if (FwdVal->getName() != Name) {
4080 P.error(Loc, "name is too long which can result in name collisions, "
4081 "consider making the name shorter or "
4082 "increasing -non-global-value-max-name-size");
4083 return nullptr;
4084 }
4085
4086 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
4087 return FwdVal;
4088}
4089
4090Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) {
4091 // Look this name up in the normal function symbol table.
4092 Value *Val = NumberedVals.get(ID);
4093
4094 // If this is a forward reference for the value, see if we already created a
4095 // forward ref record.
4096 if (!Val) {
4097 auto I = ForwardRefValIDs.find(ID);
4098 if (I != ForwardRefValIDs.end())
4099 Val = I->second.first;
4100 }
4101
4102 // If we have the value in the symbol table or fwd-ref table, return it.
4103 if (Val)
4104 return P.checkValidVariableType(Loc, "%" + Twine(ID), Ty, Val);
4105
4106 if (!Ty->isFirstClassType()) {
4107 P.error(Loc, "invalid use of a non-first-class type");
4108 return nullptr;
4109 }
4110
4111 // Otherwise, create a new forward reference for this value and remember it.
4112 Value *FwdVal;
4113 if (Ty->isLabelTy()) {
4114 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
4115 } else {
4116 FwdVal = new Argument(Ty);
4117 }
4118
4119 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
4120 return FwdVal;
4121}
4122
4123/// setInstName - After an instruction is parsed and inserted into its
4124/// basic block, this installs its name.
4125bool LLParser::PerFunctionState::setInstName(int NameID,
4126 const std::string &NameStr,
4127 LocTy NameLoc, Instruction *Inst) {
4128 // If this instruction has void type, it cannot have a name or ID specified.
4129 if (Inst->getType()->isVoidTy()) {
4130 if (NameID != -1 || !NameStr.empty())
4131 return P.error(NameLoc, "instructions returning void cannot have a name");
4132 return false;
4133 }
4134
4135 // If this was a numbered instruction, verify that the instruction is the
4136 // expected value and resolve any forward references.
4137 if (NameStr.empty()) {
4138 // If neither a name nor an ID was specified, just use the next ID.
4139 if (NameID == -1)
4140 NameID = NumberedVals.getNext();
4141
4142 if (P.checkValueID(NameLoc, "instruction", "%", NumberedVals.getNext(),
4143 NameID))
4144 return true;
4145
4146 auto FI = ForwardRefValIDs.find(NameID);
4147 if (FI != ForwardRefValIDs.end()) {
4148 Value *Sentinel = FI->second.first;
4149 if (Sentinel->getType() != Inst->getType())
4150 return P.error(NameLoc, "instruction forward referenced with type '" +
4151 getTypeString(FI->second.first->getType()) +
4152 "'");
4153
4154 Sentinel->replaceAllUsesWith(Inst);
4155 Sentinel->deleteValue();
4156 ForwardRefValIDs.erase(FI);
4157 }
4158
4159 NumberedVals.add(NameID, Inst);
4160 return false;
4161 }
4162
4163 // Otherwise, the instruction had a name. Resolve forward refs and set it.
4164 auto FI = ForwardRefVals.find(NameStr);
4165 if (FI != ForwardRefVals.end()) {
4166 Value *Sentinel = FI->second.first;
4167 if (Sentinel->getType() != Inst->getType())
4168 return P.error(NameLoc, "instruction forward referenced with type '" +
4169 getTypeString(FI->second.first->getType()) +
4170 "'");
4171
4172 Sentinel->replaceAllUsesWith(Inst);
4173 Sentinel->deleteValue();
4174 ForwardRefVals.erase(FI);
4175 }
4176
4177 // Set the name on the instruction.
4178 Inst->setName(NameStr);
4179
4180 if (Inst->getName() != NameStr)
4181 return P.error(NameLoc, "multiple definition of local value named '" +
4182 NameStr + "'");
4183 return false;
4184}
4185
4186/// getBB - Get a basic block with the specified name or ID, creating a
4187/// forward reference record if needed.
4188BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name,
4189 LocTy Loc) {
4191 getVal(Name, Type::getLabelTy(F.getContext()), Loc));
4192}
4193
4194BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) {
4196 getVal(ID, Type::getLabelTy(F.getContext()), Loc));
4197}
4198
4199/// defineBB - Define the specified basic block, which is either named or
4200/// unnamed. If there is an error, this returns null otherwise it returns
4201/// the block being defined.
4202BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
4203 int NameID, LocTy Loc) {
4204 BasicBlock *BB;
4205 if (Name.empty()) {
4206 if (NameID != -1) {
4207 if (P.checkValueID(Loc, "label", "", NumberedVals.getNext(), NameID))
4208 return nullptr;
4209 } else {
4210 NameID = NumberedVals.getNext();
4211 }
4212 BB = getBB(NameID, Loc);
4213 if (!BB) {
4214 P.error(Loc, "unable to create block numbered '" + Twine(NameID) + "'");
4215 return nullptr;
4216 }
4217 } else {
4218 BB = getBB(Name, Loc);
4219 if (!BB) {
4220 P.error(Loc, "unable to create block named '" + Name + "'");
4221 return nullptr;
4222 }
4223 }
4224
4225 // Move the block to the end of the function. Forward ref'd blocks are
4226 // inserted wherever they happen to be referenced.
4227 F.splice(F.end(), &F, BB->getIterator());
4228
4229 // Remove the block from forward ref sets.
4230 if (Name.empty()) {
4231 ForwardRefValIDs.erase(NameID);
4232 NumberedVals.add(NameID, BB);
4233 } else {
4234 // BB forward references are already in the function symbol table.
4235 ForwardRefVals.erase(Name);
4236 }
4237
4238 return BB;
4239}
4240
4241//===----------------------------------------------------------------------===//
4242// Constants.
4243//===----------------------------------------------------------------------===//
4244
4245/// parseValID - parse an abstract value that doesn't necessarily have a
4246/// type implied. For example, if we parse "4" we don't know what integer type
4247/// it has. The value will later be combined with its type and checked for
4248/// basic correctness. PFS is used to convert function-local operands of
4249/// metadata (since metadata operands are not just parsed here but also
4250/// converted to values). PFS can be null when we are not parsing metadata
4251/// values inside a function.
4252bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
4253 ID.Loc = Lex.getLoc();
4254 switch (Lex.getKind()) {
4255 default:
4256 return tokError("expected value token");
4257 case lltok::GlobalID: // @42
4258 ID.UIntVal = Lex.getUIntVal();
4259 ID.Kind = ValID::t_GlobalID;
4260 break;
4261 case lltok::GlobalVar: // @foo
4262 ID.StrVal = Lex.getStrVal();
4263 ID.Kind = ValID::t_GlobalName;
4264 break;
4265 case lltok::LocalVarID: // %42
4266 ID.UIntVal = Lex.getUIntVal();
4267 ID.Kind = ValID::t_LocalID;
4268 break;
4269 case lltok::LocalVar: // %foo
4270 ID.StrVal = Lex.getStrVal();
4271 ID.Kind = ValID::t_LocalName;
4272 break;
4273 case lltok::APSInt:
4274 ID.APSIntVal = Lex.getAPSIntVal();
4275 ID.Kind = ValID::t_APSInt;
4276 break;
4277 case lltok::APFloat: {
4278 ID.APFloatVal = Lex.getAPFloatVal();
4279 ID.Kind = ValID::t_APFloat;
4280 break;
4281 }
4282 case lltok::FloatLiteral: {
4283 if (!ExpectedTy)
4284 return error(ID.Loc, "unexpected floating-point literal");
4285 if (!ExpectedTy->isFloatingPointTy())
4286 return error(ID.Loc, "floating-point constant invalid for type");
4287 ID.APFloatVal = APFloat(ExpectedTy->getFltSemantics());
4288 APFloat::opStatus Except =
4289 cantFail(ID.APFloatVal.convertFromString(
4290 Lex.getStrVal(), RoundingMode::NearestTiesToEven),
4291 "Invalid float strings should be caught by the lexer");
4292 // Forbid overflowing and underflowing literals, but permit inexact
4293 // literals. Underflow is thrown when the result is denormal, so to allow
4294 // denormals, only reject underflowing literals that resulted in a zero.
4295 if (Except & APFloat::opOverflow)
4296 return error(ID.Loc, "floating-point constant overflowed type");
4297 if ((Except & APFloat::opUnderflow) && ID.APFloatVal.isZero())
4298 return error(ID.Loc, "floating-point constant underflowed type");
4299 ID.Kind = ValID::t_APFloat;
4300 break;
4301 }
4303 if (!ExpectedTy)
4304 return error(ID.Loc, "unexpected floating-point literal");
4305 const auto &Semantics = ExpectedTy->getFltSemantics();
4306 const APInt &Bits = Lex.getAPSIntVal();
4307 if (APFloat::getSizeInBits(Semantics) != Bits.getBitWidth())
4308 return error(ID.Loc, "float hex literal has incorrect number of bits");
4309 ID.APFloatVal = APFloat(Semantics, Bits);
4310 ID.Kind = ValID::t_APFloat;
4311 break;
4312 }
4313 case lltok::kw_true:
4314 ID.ConstantVal = ConstantInt::getTrue(Context);
4315 ID.Kind = ValID::t_Constant;
4316 break;
4317 case lltok::kw_false:
4318 ID.ConstantVal = ConstantInt::getFalse(Context);
4319 ID.Kind = ValID::t_Constant;
4320 break;
4321 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
4322 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
4323 case lltok::kw_poison: ID.Kind = ValID::t_Poison; break;
4324 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
4325 case lltok::kw_none: ID.Kind = ValID::t_None; break;
4326
4327 case lltok::lbrace: {
4328 // ValID ::= '{' ConstVector '}'
4329 Lex.Lex();
4331 if (parseGlobalValueVector(Elts) ||
4332 parseToken(lltok::rbrace, "expected end of struct constant"))
4333 return true;
4334
4335 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
4336 ID.UIntVal = Elts.size();
4337 memcpy(ID.ConstantStructElts.get(), Elts.data(),
4338 Elts.size() * sizeof(Elts[0]));
4340 return false;
4341 }
4342 case lltok::less: {
4343 // ValID ::= '<' ConstVector '>' --> Vector.
4344 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
4345 Lex.Lex();
4346 bool isPackedStruct = EatIfPresent(lltok::lbrace);
4347
4349 LocTy FirstEltLoc = Lex.getLoc();
4350 if (parseGlobalValueVector(Elts) ||
4351 (isPackedStruct &&
4352 parseToken(lltok::rbrace, "expected end of packed struct")) ||
4353 parseToken(lltok::greater, "expected end of constant"))
4354 return true;
4355
4356 if (isPackedStruct) {
4357 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
4358 memcpy(ID.ConstantStructElts.get(), Elts.data(),
4359 Elts.size() * sizeof(Elts[0]));
4360 ID.UIntVal = Elts.size();
4362 return false;
4363 }
4364
4365 if (Elts.empty())
4366 return error(ID.Loc, "constant vector must not be empty");
4367
4368 if (!Elts[0]->getType()->isIntegerTy() && !Elts[0]->getType()->isByteTy() &&
4369 !Elts[0]->getType()->isFloatingPointTy() &&
4370 !Elts[0]->getType()->isPointerTy())
4371 return error(
4372 FirstEltLoc,
4373 "vector elements must have integer, byte, pointer or floating point "
4374 "type");
4375
4376 // Verify that all the vector elements have the same type.
4377 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
4378 if (Elts[i]->getType() != Elts[0]->getType())
4379 return error(FirstEltLoc, "vector element #" + Twine(i) +
4380 " is not of type '" +
4381 getTypeString(Elts[0]->getType()));
4382
4383 ID.ConstantVal = ConstantVector::get(Elts);
4384 ID.Kind = ValID::t_Constant;
4385 return false;
4386 }
4387 case lltok::lsquare: { // Array Constant
4388 Lex.Lex();
4390 LocTy FirstEltLoc = Lex.getLoc();
4391 if (parseGlobalValueVector(Elts) ||
4392 parseToken(lltok::rsquare, "expected end of array constant"))
4393 return true;
4394
4395 // Handle empty element.
4396 if (Elts.empty()) {
4397 // Use undef instead of an array because it's inconvenient to determine
4398 // the element type at this point, there being no elements to examine.
4399 ID.Kind = ValID::t_EmptyArray;
4400 return false;
4401 }
4402
4403 if (!Elts[0]->getType()->isFirstClassType())
4404 return error(FirstEltLoc, "invalid array element type: " +
4405 getTypeString(Elts[0]->getType()));
4406
4407 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
4408
4409 // Verify all elements are correct type!
4410 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
4411 if (Elts[i]->getType() != Elts[0]->getType())
4412 return error(FirstEltLoc, "array element #" + Twine(i) +
4413 " is not of type '" +
4414 getTypeString(Elts[0]->getType()));
4415 }
4416
4417 ID.ConstantVal = ConstantArray::get(ATy, Elts);
4418 ID.Kind = ValID::t_Constant;
4419 return false;
4420 }
4421 case lltok::kw_c: { // c "foo"
4422 Lex.Lex();
4423 ArrayType *ATy = cast<ArrayType>(ExpectedTy);
4424 ID.ConstantVal = ConstantDataArray::getString(
4425 Context, Lex.getStrVal(), false, ATy->getElementType()->isByteTy());
4426 if (parseToken(lltok::StringConstant, "expected string"))
4427 return true;
4428 ID.Kind = ValID::t_Constant;
4429 return false;
4430 }
4431 case lltok::kw_asm: {
4432 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
4433 // STRINGCONSTANT
4434 bool HasSideEffect, AlignStack, AsmDialect, CanThrow;
4435 Lex.Lex();
4436 if (parseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
4437 parseOptionalToken(lltok::kw_alignstack, AlignStack) ||
4438 parseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
4439 parseOptionalToken(lltok::kw_unwind, CanThrow) ||
4440 parseStringConstant(ID.StrVal) ||
4441 parseToken(lltok::comma, "expected comma in inline asm expression") ||
4442 parseToken(lltok::StringConstant, "expected constraint string"))
4443 return true;
4444 ID.StrVal2 = Lex.getStrVal();
4445 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) |
4446 (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3);
4447 ID.Kind = ValID::t_InlineAsm;
4448 return false;
4449 }
4450
4452 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
4453 Lex.Lex();
4454
4455 ValID Fn, Label;
4456
4457 if (parseToken(lltok::lparen, "expected '(' in block address expression") ||
4458 parseValID(Fn, PFS) ||
4459 parseToken(lltok::comma,
4460 "expected comma in block address expression") ||
4461 parseValID(Label, PFS) ||
4462 parseToken(lltok::rparen, "expected ')' in block address expression"))
4463 return true;
4464
4466 return error(Fn.Loc, "expected function name in blockaddress");
4467 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
4468 return error(Label.Loc, "expected basic block name in blockaddress");
4469
4470 // Try to find the function (but skip it if it's forward-referenced).
4471 GlobalValue *GV = nullptr;
4472 if (Fn.Kind == ValID::t_GlobalID) {
4473 GV = NumberedVals.get(Fn.UIntVal);
4474 } else if (!ForwardRefVals.count(Fn.StrVal)) {
4475 GV = M->getNamedValue(Fn.StrVal);
4476 }
4477 Function *F = nullptr;
4478 if (GV) {
4479 // Confirm that it's actually a function with a definition.
4480 if (!isa<Function>(GV))
4481 return error(Fn.Loc, "expected function name in blockaddress");
4482 F = cast<Function>(GV);
4483 if (F->isDeclaration())
4484 return error(Fn.Loc, "cannot take blockaddress inside a declaration");
4485 }
4486
4487 if (!F) {
4488 // Make a global variable as a placeholder for this reference.
4489 GlobalValue *&FwdRef =
4490 ForwardRefBlockAddresses[std::move(Fn)][std::move(Label)];
4491 if (!FwdRef) {
4492 unsigned FwdDeclAS;
4493 if (ExpectedTy) {
4494 // If we know the type that the blockaddress is being assigned to,
4495 // we can use the address space of that type.
4496 if (!ExpectedTy->isPointerTy())
4497 return error(ID.Loc,
4498 "type of blockaddress must be a pointer and not '" +
4499 getTypeString(ExpectedTy) + "'");
4500 FwdDeclAS = ExpectedTy->getPointerAddressSpace();
4501 } else if (PFS) {
4502 // Otherwise, we default the address space of the current function.
4503 FwdDeclAS = PFS->getFunction().getAddressSpace();
4504 } else {
4505 llvm_unreachable("Unknown address space for blockaddress");
4506 }
4507 FwdRef = new GlobalVariable(
4508 *M, Type::getInt8Ty(Context), false, GlobalValue::InternalLinkage,
4509 nullptr, "", nullptr, GlobalValue::NotThreadLocal, FwdDeclAS);
4510 }
4511
4512 ID.ConstantVal = FwdRef;
4513 ID.Kind = ValID::t_Constant;
4514 return false;
4515 }
4516
4517 // We found the function; now find the basic block. Don't use PFS, since we
4518 // might be inside a constant expression.
4519 BasicBlock *BB;
4520 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
4521 if (Label.Kind == ValID::t_LocalID)
4522 BB = BlockAddressPFS->getBB(Label.UIntVal, Label.Loc);
4523 else
4524 BB = BlockAddressPFS->getBB(Label.StrVal, Label.Loc);
4525 if (!BB)
4526 return error(Label.Loc, "referenced value is not a basic block");
4527 } else {
4528 if (Label.Kind == ValID::t_LocalID)
4529 return error(Label.Loc, "cannot take address of numeric label after "
4530 "the function is defined");
4532 F->getValueSymbolTable()->lookup(Label.StrVal));
4533 if (!BB)
4534 return error(Label.Loc, "referenced value is not a basic block");
4535 }
4536
4537 ID.ConstantVal = BlockAddress::get(F, BB);
4538 ID.Kind = ValID::t_Constant;
4539 return false;
4540 }
4541
4543 // ValID ::= 'dso_local_equivalent' @foo
4544 Lex.Lex();
4545
4546 ValID Fn;
4547
4548 if (parseValID(Fn, PFS))
4549 return true;
4550
4552 return error(Fn.Loc,
4553 "expected global value name in dso_local_equivalent");
4554
4555 // Try to find the function (but skip it if it's forward-referenced).
4556 GlobalValue *GV = nullptr;
4557 if (Fn.Kind == ValID::t_GlobalID) {
4558 GV = NumberedVals.get(Fn.UIntVal);
4559 } else if (!ForwardRefVals.count(Fn.StrVal)) {
4560 GV = M->getNamedValue(Fn.StrVal);
4561 }
4562
4563 if (!GV) {
4564 // Make a placeholder global variable as a placeholder for this reference.
4565 auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID)
4566 ? ForwardRefDSOLocalEquivalentIDs
4567 : ForwardRefDSOLocalEquivalentNames;
4568 GlobalValue *&FwdRef = FwdRefMap[Fn];
4569 if (!FwdRef) {
4570 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
4571 GlobalValue::InternalLinkage, nullptr, "",
4573 }
4574
4575 ID.ConstantVal = FwdRef;
4576 ID.Kind = ValID::t_Constant;
4577 return false;
4578 }
4579
4580 if (!GV->getValueType()->isFunctionTy())
4581 return error(Fn.Loc, "expected a function, alias to function, or ifunc "
4582 "in dso_local_equivalent");
4583
4584 ID.ConstantVal = DSOLocalEquivalent::get(GV);
4585 ID.Kind = ValID::t_Constant;
4586 return false;
4587 }
4588
4589 case lltok::kw_no_cfi: {
4590 // ValID ::= 'no_cfi' @foo
4591 Lex.Lex();
4592
4593 if (parseValID(ID, PFS))
4594 return true;
4595
4596 if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName)
4597 return error(ID.Loc, "expected global value name in no_cfi");
4598
4599 ID.NoCFI = true;
4600 return false;
4601 }
4602 case lltok::kw_ptrauth: {
4603 // ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key>
4604 // (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)?
4605 // )? )? ')'
4606 Lex.Lex();
4607
4608 Constant *Ptr, *Key;
4609 Constant *Disc = nullptr, *AddrDisc = nullptr,
4610 *DeactivationSymbol = nullptr;
4611
4612 if (parseToken(lltok::lparen,
4613 "expected '(' in constant ptrauth expression") ||
4614 parseGlobalTypeAndValue(Ptr) ||
4615 parseToken(lltok::comma,
4616 "expected comma in constant ptrauth expression") ||
4617 parseGlobalTypeAndValue(Key))
4618 return true;
4619 // If present, parse the optional disc/addrdisc/ds.
4620 if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(Disc))
4621 return true;
4622 if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc))
4623 return true;
4624 if (EatIfPresent(lltok::comma) &&
4625 parseGlobalTypeAndValue(DeactivationSymbol))
4626 return true;
4627 if (parseToken(lltok::rparen,
4628 "expected ')' in constant ptrauth expression"))
4629 return true;
4630
4631 if (!Ptr->getType()->isPointerTy())
4632 return error(ID.Loc, "constant ptrauth base pointer must be a pointer");
4633
4634 auto *KeyC = dyn_cast<ConstantInt>(Key);
4635 if (!KeyC || KeyC->getBitWidth() != 32)
4636 return error(ID.Loc, "constant ptrauth key must be i32 constant");
4637
4638 ConstantInt *DiscC = nullptr;
4639 if (Disc) {
4640 DiscC = dyn_cast<ConstantInt>(Disc);
4641 if (!DiscC || DiscC->getBitWidth() != 64)
4642 return error(
4643 ID.Loc,
4644 "constant ptrauth integer discriminator must be i64 constant");
4645 } else {
4646 DiscC = ConstantInt::get(Type::getInt64Ty(Context), 0);
4647 }
4648
4649 if (AddrDisc) {
4650 if (!AddrDisc->getType()->isPointerTy())
4651 return error(
4652 ID.Loc, "constant ptrauth address discriminator must be a pointer");
4653 } else {
4654 AddrDisc = ConstantPointerNull::get(PointerType::get(Context, 0));
4655 }
4656
4657 if (!DeactivationSymbol)
4658 DeactivationSymbol =
4660 if (!DeactivationSymbol->getType()->isPointerTy())
4661 return error(ID.Loc,
4662 "constant ptrauth deactivation symbol must be a pointer");
4663
4664 ID.ConstantVal =
4665 ConstantPtrAuth::get(Ptr, KeyC, DiscC, AddrDisc, DeactivationSymbol);
4666 ID.Kind = ValID::t_Constant;
4667 return false;
4668 }
4669
4670 case lltok::kw_trunc:
4671 case lltok::kw_bitcast:
4673 case lltok::kw_inttoptr:
4675 case lltok::kw_ptrtoint: {
4676 unsigned Opc = Lex.getUIntVal();
4677 Type *DestTy = nullptr;
4678 Constant *SrcVal;
4679 Lex.Lex();
4680 if (parseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
4681 parseGlobalTypeAndValue(SrcVal) ||
4682 parseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
4683 parseType(DestTy) ||
4684 parseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
4685 return true;
4686 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
4687 return error(ID.Loc, "invalid cast opcode for cast from '" +
4688 getTypeString(SrcVal->getType()) + "' to '" +
4689 getTypeString(DestTy) + "'");
4691 SrcVal, DestTy);
4692 ID.Kind = ValID::t_Constant;
4693 return false;
4694 }
4696 return error(ID.Loc, "extractvalue constexprs are no longer supported");
4698 return error(ID.Loc, "insertvalue constexprs are no longer supported");
4699 case lltok::kw_udiv:
4700 return error(ID.Loc, "udiv constexprs are no longer supported");
4701 case lltok::kw_sdiv:
4702 return error(ID.Loc, "sdiv constexprs are no longer supported");
4703 case lltok::kw_urem:
4704 return error(ID.Loc, "urem constexprs are no longer supported");
4705 case lltok::kw_srem:
4706 return error(ID.Loc, "srem constexprs are no longer supported");
4707 case lltok::kw_fadd:
4708 return error(ID.Loc, "fadd constexprs are no longer supported");
4709 case lltok::kw_fsub:
4710 return error(ID.Loc, "fsub constexprs are no longer supported");
4711 case lltok::kw_fmul:
4712 return error(ID.Loc, "fmul constexprs are no longer supported");
4713 case lltok::kw_fdiv:
4714 return error(ID.Loc, "fdiv constexprs are no longer supported");
4715 case lltok::kw_frem:
4716 return error(ID.Loc, "frem constexprs are no longer supported");
4717 case lltok::kw_and:
4718 return error(ID.Loc, "and constexprs are no longer supported");
4719 case lltok::kw_or:
4720 return error(ID.Loc, "or constexprs are no longer supported");
4721 case lltok::kw_lshr:
4722 return error(ID.Loc, "lshr constexprs are no longer supported");
4723 case lltok::kw_ashr:
4724 return error(ID.Loc, "ashr constexprs are no longer supported");
4725 case lltok::kw_shl:
4726 return error(ID.Loc, "shl constexprs are no longer supported");
4727 case lltok::kw_mul:
4728 return error(ID.Loc, "mul constexprs are no longer supported");
4729 case lltok::kw_fneg:
4730 return error(ID.Loc, "fneg constexprs are no longer supported");
4731 case lltok::kw_select:
4732 return error(ID.Loc, "select constexprs are no longer supported");
4733 case lltok::kw_zext:
4734 return error(ID.Loc, "zext constexprs are no longer supported");
4735 case lltok::kw_sext:
4736 return error(ID.Loc, "sext constexprs are no longer supported");
4737 case lltok::kw_fptrunc:
4738 return error(ID.Loc, "fptrunc constexprs are no longer supported");
4739 case lltok::kw_fpext:
4740 return error(ID.Loc, "fpext constexprs are no longer supported");
4741 case lltok::kw_uitofp:
4742 return error(ID.Loc, "uitofp constexprs are no longer supported");
4743 case lltok::kw_sitofp:
4744 return error(ID.Loc, "sitofp constexprs are no longer supported");
4745 case lltok::kw_fptoui:
4746 return error(ID.Loc, "fptoui constexprs are no longer supported");
4747 case lltok::kw_fptosi:
4748 return error(ID.Loc, "fptosi constexprs are no longer supported");
4749 case lltok::kw_icmp:
4750 return error(ID.Loc, "icmp constexprs are no longer supported");
4751 case lltok::kw_fcmp:
4752 return error(ID.Loc, "fcmp constexprs are no longer supported");
4753
4754 // Binary Operators.
4755 case lltok::kw_add:
4756 case lltok::kw_sub:
4757 case lltok::kw_xor: {
4758 bool NUW = false;
4759 bool NSW = false;
4760 unsigned Opc = Lex.getUIntVal();
4761 Constant *Val0, *Val1;
4762 Lex.Lex();
4763 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
4764 Opc == Instruction::Mul) {
4765 if (EatIfPresent(lltok::kw_nuw))
4766 NUW = true;
4767 if (EatIfPresent(lltok::kw_nsw)) {
4768 NSW = true;
4769 if (EatIfPresent(lltok::kw_nuw))
4770 NUW = true;
4771 }
4772 }
4773 if (parseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
4774 parseGlobalTypeAndValue(Val0) ||
4775 parseToken(lltok::comma, "expected comma in binary constantexpr") ||
4776 parseGlobalTypeAndValue(Val1) ||
4777 parseToken(lltok::rparen, "expected ')' in binary constantexpr"))
4778 return true;
4779 if (Val0->getType() != Val1->getType())
4780 return error(ID.Loc, "operands of constexpr must have same type");
4781 // Check that the type is valid for the operator.
4782 if (!Val0->getType()->isIntOrIntVectorTy())
4783 return error(ID.Loc,
4784 "constexpr requires integer or integer vector operands");
4785 unsigned Flags = 0;
4788 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1, Flags);
4789 ID.Kind = ValID::t_Constant;
4790 return false;
4791 }
4792
4793 case lltok::kw_splat: {
4794 Lex.Lex();
4795 if (parseToken(lltok::lparen, "expected '(' after vector splat"))
4796 return true;
4797 Constant *C;
4798 if (parseGlobalTypeAndValue(C))
4799 return true;
4800 if (parseToken(lltok::rparen, "expected ')' at end of vector splat"))
4801 return true;
4802
4803 ID.ConstantVal = C;
4805 return false;
4806 }
4807
4812 unsigned Opc = Lex.getUIntVal();
4814 GEPNoWrapFlags NW;
4815 bool HasInRange = false;
4816 APSInt InRangeStart;
4817 APSInt InRangeEnd;
4818 Type *Ty;
4819 Lex.Lex();
4820
4821 if (Opc == Instruction::GetElementPtr) {
4822 while (true) {
4823 if (EatIfPresent(lltok::kw_inbounds))
4825 else if (EatIfPresent(lltok::kw_nusw))
4827 else if (EatIfPresent(lltok::kw_nuw))
4829 else
4830 break;
4831 }
4832
4833 if (EatIfPresent(lltok::kw_inrange)) {
4834 if (parseToken(lltok::lparen, "expected '('"))
4835 return true;
4836 if (Lex.getKind() != lltok::APSInt)
4837 return tokError("expected integer");
4838 InRangeStart = Lex.getAPSIntVal();
4839 Lex.Lex();
4840 if (parseToken(lltok::comma, "expected ','"))
4841 return true;
4842 if (Lex.getKind() != lltok::APSInt)
4843 return tokError("expected integer");
4844 InRangeEnd = Lex.getAPSIntVal();
4845 Lex.Lex();
4846 if (parseToken(lltok::rparen, "expected ')'"))
4847 return true;
4848 HasInRange = true;
4849 }
4850 }
4851
4852 if (parseToken(lltok::lparen, "expected '(' in constantexpr"))
4853 return true;
4854
4855 if (Opc == Instruction::GetElementPtr) {
4856 if (parseType(Ty) ||
4857 parseToken(lltok::comma, "expected comma after getelementptr's type"))
4858 return true;
4859 }
4860
4861 if (parseGlobalValueVector(Elts) ||
4862 parseToken(lltok::rparen, "expected ')' in constantexpr"))
4863 return true;
4864
4865 if (Opc == Instruction::GetElementPtr) {
4866 if (Elts.size() == 0 ||
4867 !Elts[0]->getType()->isPtrOrPtrVectorTy())
4868 return error(ID.Loc, "base of getelementptr must be a pointer");
4869
4870 Type *BaseType = Elts[0]->getType();
4871 std::optional<ConstantRange> InRange;
4872 if (HasInRange) {
4873 unsigned IndexWidth =
4874 M->getDataLayout().getIndexTypeSizeInBits(BaseType);
4875 InRangeStart = InRangeStart.extOrTrunc(IndexWidth);
4876 InRangeEnd = InRangeEnd.extOrTrunc(IndexWidth);
4877 if (InRangeStart.sge(InRangeEnd))
4878 return error(ID.Loc, "expected end to be larger than start");
4879 InRange = ConstantRange::getNonEmpty(InRangeStart, InRangeEnd);
4880 }
4881
4882 unsigned GEPWidth =
4883 BaseType->isVectorTy()
4884 ? cast<FixedVectorType>(BaseType)->getNumElements()
4885 : 0;
4886
4887 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
4888 for (Constant *Val : Indices) {
4889 Type *ValTy = Val->getType();
4890 if (!ValTy->isIntOrIntVectorTy())
4891 return error(ID.Loc, "getelementptr index must be an integer");
4892 if (auto *ValVTy = dyn_cast<VectorType>(ValTy)) {
4893 unsigned ValNumEl = cast<FixedVectorType>(ValVTy)->getNumElements();
4894 if (GEPWidth && (ValNumEl != GEPWidth))
4895 return error(
4896 ID.Loc,
4897 "getelementptr vector index has a wrong number of elements");
4898 // GEPWidth may have been unknown because the base is a scalar,
4899 // but it is known now.
4900 GEPWidth = ValNumEl;
4901 }
4902 }
4903
4904 if (!Indices.empty() && !Ty->isSized())
4905 return error(ID.Loc, "base element of getelementptr must be sized");
4906
4908 return error(ID.Loc, "invalid base element for constant getelementptr");
4909
4910 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
4911 return error(ID.Loc, "invalid getelementptr indices");
4912
4914 ID.ConstantVal =
4915 ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices, NW, InRange);
4917 } else if (Opc == Instruction::ShuffleVector) {
4918 if (Elts.size() != 3)
4919 return error(ID.Loc, "expected three operands to shufflevector");
4920 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4921 return error(ID.Loc, "invalid operands to shufflevector");
4922 SmallVector<int, 16> Mask;
4924 ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1], Mask);
4925 } else if (Opc == Instruction::ExtractElement) {
4926 if (Elts.size() != 2)
4927 return error(ID.Loc, "expected two operands to extractelement");
4928 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
4929 return error(ID.Loc, "invalid extractelement operands");
4930 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
4931 } else {
4932 assert(Opc == Instruction::InsertElement && "Unknown opcode");
4933 if (Elts.size() != 3)
4934 return error(ID.Loc, "expected three operands to insertelement");
4935 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4936 return error(ID.Loc, "invalid insertelement operands");
4937 ID.ConstantVal =
4938 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
4939 }
4940
4941 ID.Kind = ValID::t_Constant;
4942 return false;
4943 }
4944 }
4945
4946 Lex.Lex();
4947 return false;
4948}
4949
4950/// parseGlobalValue - parse a global value with the specified type.
4951bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) {
4952 C = nullptr;
4953 ValID ID;
4954 Value *V = nullptr;
4955 bool Parsed = parseValID(ID, /*PFS=*/nullptr, Ty) ||
4956 convertValIDToValue(Ty, ID, V, nullptr);
4957 if (V && !(C = dyn_cast<Constant>(V)))
4958 return error(ID.Loc, "global values must be constants");
4959 return Parsed;
4960}
4961
4962bool LLParser::parseGlobalTypeAndValue(Constant *&V) {
4963 Type *Ty = nullptr;
4964 return parseType(Ty) || parseGlobalValue(Ty, V);
4965}
4966
4967bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
4968 C = nullptr;
4969
4970 LocTy KwLoc = Lex.getLoc();
4971 if (!EatIfPresent(lltok::kw_comdat))
4972 return false;
4973
4974 if (EatIfPresent(lltok::lparen)) {
4975 if (Lex.getKind() != lltok::ComdatVar)
4976 return tokError("expected comdat variable");
4977 C = getComdat(Lex.getStrVal(), Lex.getLoc());
4978 Lex.Lex();
4979 if (parseToken(lltok::rparen, "expected ')' after comdat var"))
4980 return true;
4981 } else {
4982 if (GlobalName.empty())
4983 return tokError("comdat cannot be unnamed");
4984 C = getComdat(std::string(GlobalName), KwLoc);
4985 }
4986
4987 return false;
4988}
4989
4990/// parseGlobalValueVector
4991/// ::= /*empty*/
4992/// ::= TypeAndValue (',' TypeAndValue)*
4993bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
4994 // Empty list.
4995 if (Lex.getKind() == lltok::rbrace ||
4996 Lex.getKind() == lltok::rsquare ||
4997 Lex.getKind() == lltok::greater ||
4998 Lex.getKind() == lltok::rparen)
4999 return false;
5000
5001 do {
5002 // Let the caller deal with inrange.
5003 if (Lex.getKind() == lltok::kw_inrange)
5004 return false;
5005
5006 Constant *C;
5007 if (parseGlobalTypeAndValue(C))
5008 return true;
5009 Elts.push_back(C);
5010 } while (EatIfPresent(lltok::comma));
5011
5012 return false;
5013}
5014
5015bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
5017 if (parseMDNodeVector(Elts))
5018 return true;
5019
5020 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
5021 return false;
5022}
5023
5024/// MDNode:
5025/// ::= !{ ... }
5026/// ::= !7
5027/// ::= !DILocation(...)
5028bool LLParser::parseMDNode(MDNode *&N) {
5029 if (Lex.getKind() == lltok::MetadataVar)
5030 return parseSpecializedMDNode(N);
5031
5032 return parseToken(lltok::exclaim, "expected '!' here") || parseMDNodeTail(N);
5033}
5034
5035bool LLParser::parseMDNodeTail(MDNode *&N) {
5036 // !{ ... }
5037 if (Lex.getKind() == lltok::lbrace)
5038 return parseMDTuple(N);
5039
5040 // !42
5041 return parseMDNodeID(N);
5042}
5043
5044namespace {
5045
5046/// Structure to represent an optional metadata field.
5047template <class FieldTy> struct MDFieldImpl {
5048 typedef MDFieldImpl ImplTy;
5049 FieldTy Val;
5050 bool Seen;
5051
5052 void assign(FieldTy Val) {
5053 Seen = true;
5054 this->Val = std::move(Val);
5055 }
5056
5057 explicit MDFieldImpl(FieldTy Default)
5058 : Val(std::move(Default)), Seen(false) {}
5059};
5060
5061/// Structure to represent an optional metadata field that
5062/// can be of either type (A or B) and encapsulates the
5063/// MD<typeofA>Field and MD<typeofB>Field structs, so not
5064/// to reimplement the specifics for representing each Field.
5065template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
5066 typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
5067 FieldTypeA A;
5068 FieldTypeB B;
5069 bool Seen;
5070
5071 enum {
5072 IsInvalid = 0,
5073 IsTypeA = 1,
5074 IsTypeB = 2
5075 } WhatIs;
5076
5077 void assign(FieldTypeA A) {
5078 Seen = true;
5079 this->A = std::move(A);
5080 WhatIs = IsTypeA;
5081 }
5082
5083 void assign(FieldTypeB B) {
5084 Seen = true;
5085 this->B = std::move(B);
5086 WhatIs = IsTypeB;
5087 }
5088
5089 explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
5090 : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
5091 WhatIs(IsInvalid) {}
5092};
5093
5094struct MDUnsignedField : public MDFieldImpl<uint64_t> {
5095 uint64_t Max;
5096
5097 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
5098 : ImplTy(Default), Max(Max) {}
5099};
5100
5101struct LineField : public MDUnsignedField {
5102 LineField() : MDUnsignedField(0, UINT32_MAX) {}
5103};
5104
5105struct ColumnField : public MDUnsignedField {
5106 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
5107};
5108
5109struct DwarfTagField : public MDUnsignedField {
5110 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
5111 DwarfTagField(dwarf::Tag DefaultTag)
5112 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
5113};
5114
5115struct DwarfMacinfoTypeField : public MDUnsignedField {
5116 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
5117 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
5118 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
5119};
5120
5121struct DwarfAttEncodingField : public MDUnsignedField {
5122 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
5123};
5124
5125struct DwarfVirtualityField : public MDUnsignedField {
5126 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
5127};
5128
5129struct DwarfLangField : public MDUnsignedField {
5130 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
5131};
5132
5133struct DwarfSourceLangNameField : public MDUnsignedField {
5134 DwarfSourceLangNameField() : MDUnsignedField(0, UINT32_MAX) {}
5135};
5136
5137struct DwarfLangDialectField : public MDUnsignedField {
5138 DwarfLangDialectField()
5139 : MDUnsignedField(0, dwarf::DW_LLVM_LANG_DIALECT_max) {}
5140};
5141
5142struct DwarfCCField : public MDUnsignedField {
5143 DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
5144};
5145
5146struct DwarfEnumKindField : public MDUnsignedField {
5147 DwarfEnumKindField()
5148 : MDUnsignedField(dwarf::DW_APPLE_ENUM_KIND_invalid,
5149 dwarf::DW_APPLE_ENUM_KIND_max) {}
5150};
5151
5152struct EmissionKindField : public MDUnsignedField {
5153 EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
5154};
5155
5156struct FixedPointKindField : public MDUnsignedField {
5157 FixedPointKindField()
5158 : MDUnsignedField(0, DIFixedPointType::LastFixedPointKind) {}
5159};
5160
5161struct NameTableKindField : public MDUnsignedField {
5162 NameTableKindField()
5163 : MDUnsignedField(
5164 0, (unsigned)
5165 DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
5166};
5167
5168struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
5169 DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
5170};
5171
5172struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> {
5173 DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {}
5174};
5175
5176struct MDAPSIntField : public MDFieldImpl<APSInt> {
5177 MDAPSIntField() : ImplTy(APSInt()) {}
5178};
5179
5180struct MDSignedField : public MDFieldImpl<int64_t> {
5181 int64_t Min = INT64_MIN;
5182 int64_t Max = INT64_MAX;
5183
5184 MDSignedField(int64_t Default = 0)
5185 : ImplTy(Default) {}
5186 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
5187 : ImplTy(Default), Min(Min), Max(Max) {}
5188};
5189
5190struct MDBoolField : public MDFieldImpl<bool> {
5191 MDBoolField(bool Default = false) : ImplTy(Default) {}
5192};
5193
5194struct MDField : public MDFieldImpl<Metadata *> {
5195 bool AllowNull;
5196
5197 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
5198};
5199
5200struct MDStringField : public MDFieldImpl<MDString *> {
5201 enum class EmptyIs {
5202 Null, //< Allow empty input string, map to nullptr
5203 Empty, //< Allow empty input string, map to an empty MDString
5204 Error, //< Disallow empty string, map to an error
5205 } EmptyIs;
5206 MDStringField(enum EmptyIs EmptyIs = EmptyIs::Null)
5207 : ImplTy(nullptr), EmptyIs(EmptyIs) {}
5208};
5209
5210struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
5211 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
5212};
5213
5214struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
5215 ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
5216};
5217
5218struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
5219 MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
5220 : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
5221
5222 MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
5223 bool AllowNull = true)
5224 : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
5225
5226 bool isMDSignedField() const { return WhatIs == IsTypeA; }
5227 bool isMDField() const { return WhatIs == IsTypeB; }
5228 int64_t getMDSignedValue() const {
5229 assert(isMDSignedField() && "Wrong field type");
5230 return A.Val;
5231 }
5232 Metadata *getMDFieldValue() const {
5233 assert(isMDField() && "Wrong field type");
5234 return B.Val;
5235 }
5236};
5237
5238struct MDUnsignedOrMDField : MDEitherFieldImpl<MDUnsignedField, MDField> {
5239 MDUnsignedOrMDField(uint64_t Default = 0, bool AllowNull = true)
5240 : ImplTy(MDUnsignedField(Default), MDField(AllowNull)) {}
5241
5242 MDUnsignedOrMDField(uint64_t Default, uint64_t Max, bool AllowNull = true)
5243 : ImplTy(MDUnsignedField(Default, Max), MDField(AllowNull)) {}
5244
5245 bool isMDUnsignedField() const { return WhatIs == IsTypeA; }
5246 bool isMDField() const { return WhatIs == IsTypeB; }
5247 uint64_t getMDUnsignedValue() const {
5248 assert(isMDUnsignedField() && "Wrong field type");
5249 return A.Val;
5250 }
5251 Metadata *getMDFieldValue() const {
5252 assert(isMDField() && "Wrong field type");
5253 return B.Val;
5254 }
5255
5256 Metadata *getValueAsMetadata(LLVMContext &Context) const {
5257 if (isMDUnsignedField())
5259 ConstantInt::get(Type::getInt64Ty(Context), getMDUnsignedValue()));
5260 if (isMDField())
5261 return getMDFieldValue();
5262 return nullptr;
5263 }
5264};
5265
5266} // end anonymous namespace
5267
5268namespace llvm {
5269
5270template <>
5271bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) {
5272 if (Lex.getKind() != lltok::APSInt)
5273 return tokError("expected integer");
5274
5275 Result.assign(Lex.getAPSIntVal());
5276 Lex.Lex();
5277 return false;
5278}
5279
5280template <>
5281bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5282 MDUnsignedField &Result) {
5283 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
5284 return tokError("expected unsigned integer");
5285
5286 auto &U = Lex.getAPSIntVal();
5287 if (U.ugt(Result.Max))
5288 return tokError("value for '" + Name + "' too large, limit is " +
5289 Twine(Result.Max));
5290 Result.assign(U.getZExtValue());
5291 assert(Result.Val <= Result.Max && "Expected value in range");
5292 Lex.Lex();
5293 return false;
5294}
5295
5296template <>
5297bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) {
5298 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5299}
5300template <>
5301bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
5302 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5303}
5304
5305template <>
5306bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
5307 if (Lex.getKind() == lltok::APSInt)
5308 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5309
5310 if (Lex.getKind() != lltok::DwarfTag)
5311 return tokError("expected DWARF tag");
5312
5313 unsigned Tag = dwarf::getTag(Lex.getStrVal());
5315 return tokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
5316 assert(Tag <= Result.Max && "Expected valid DWARF tag");
5317
5318 Result.assign(Tag);
5319 Lex.Lex();
5320 return false;
5321}
5322
5323template <>
5324bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5325 DwarfMacinfoTypeField &Result) {
5326 if (Lex.getKind() == lltok::APSInt)
5327 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5328
5329 if (Lex.getKind() != lltok::DwarfMacinfo)
5330 return tokError("expected DWARF macinfo type");
5331
5332 unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
5333 if (Macinfo == dwarf::DW_MACINFO_invalid)
5334 return tokError("invalid DWARF macinfo type" + Twine(" '") +
5335 Lex.getStrVal() + "'");
5336 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
5337
5338 Result.assign(Macinfo);
5339 Lex.Lex();
5340 return false;
5341}
5342
5343template <>
5344bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5345 DwarfVirtualityField &Result) {
5346 if (Lex.getKind() == lltok::APSInt)
5347 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5348
5349 if (Lex.getKind() != lltok::DwarfVirtuality)
5350 return tokError("expected DWARF virtuality code");
5351
5352 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
5353 if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
5354 return tokError("invalid DWARF virtuality code" + Twine(" '") +
5355 Lex.getStrVal() + "'");
5356 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
5357 Result.assign(Virtuality);
5358 Lex.Lex();
5359 return false;
5360}
5361
5362template <>
5363bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5364 DwarfEnumKindField &Result) {
5365 if (Lex.getKind() == lltok::APSInt)
5366 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5367
5368 if (Lex.getKind() != lltok::DwarfEnumKind)
5369 return tokError("expected DWARF enum kind code");
5370
5371 unsigned EnumKind = dwarf::getEnumKind(Lex.getStrVal());
5372 if (EnumKind == dwarf::DW_APPLE_ENUM_KIND_invalid)
5373 return tokError("invalid DWARF enum kind code" + Twine(" '") +
5374 Lex.getStrVal() + "'");
5375 assert(EnumKind <= Result.Max && "Expected valid DWARF enum kind code");
5376 Result.assign(EnumKind);
5377 Lex.Lex();
5378 return false;
5379}
5380
5381template <>
5382bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
5383 if (Lex.getKind() == lltok::APSInt)
5384 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5385
5386 if (Lex.getKind() != lltok::DwarfLang)
5387 return tokError("expected DWARF language");
5388
5389 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
5390 if (!Lang)
5391 return tokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
5392 "'");
5393 assert(Lang <= Result.Max && "Expected valid DWARF language");
5394 Result.assign(Lang);
5395 Lex.Lex();
5396 return false;
5397}
5398
5399template <>
5400bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5401 DwarfSourceLangNameField &Result) {
5402 if (Lex.getKind() == lltok::APSInt)
5403 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5404
5405 if (Lex.getKind() != lltok::DwarfSourceLangName)
5406 return tokError("expected DWARF source language name");
5407
5408 unsigned Lang = dwarf::getSourceLanguageName(Lex.getStrVal());
5409 if (!Lang)
5410 return tokError("invalid DWARF source language name" + Twine(" '") +
5411 Lex.getStrVal() + "'");
5412 assert(Lang <= Result.Max && "Expected valid DWARF source language name");
5413 Result.assign(Lang);
5414 Lex.Lex();
5415 return false;
5416}
5417
5418template <>
5419bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5420 DwarfLangDialectField &Result) {
5421 // Specifying the dialect field requires a recognized dialect: simt or
5422 // tile (numerically 1 or 2). Omitting the field is the only way to
5423 // express "no dialect specified".
5424 if (Lex.getKind() == lltok::APSInt) {
5425 if (Lex.getAPSIntVal() == 0)
5426 return tokError("value for 'dialect' must be a known DWARF language "
5427 "dialect (DW_LLVM_LANG_DIALECT_simt or "
5428 "DW_LLVM_LANG_DIALECT_tile)");
5429 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5430 }
5431
5432 if (Lex.getKind() != lltok::DwarfLangDialect)
5433 return tokError("expected DWARF language dialect");
5434
5435 StringRef DialectString = Lex.getStrVal();
5436 // getLanguageDialect returns a sentinel above Result.Max for unknown
5437 // spellings; only simt and tile are registered, so any unrecognized
5438 // DW_LLVM_LANG_DIALECT_* token is rejected here.
5439 unsigned Dialect = dwarf::getLanguageDialect(DialectString);
5440 if (Dialect > Result.Max)
5441 return tokError("invalid DWARF language dialect" + Twine(" '") +
5442 DialectString + "'");
5443 Result.assign(Dialect);
5444 Lex.Lex();
5445 return false;
5446}
5447
5448template <>
5449bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
5450 if (Lex.getKind() == lltok::APSInt)
5451 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5452
5453 if (Lex.getKind() != lltok::DwarfCC)
5454 return tokError("expected DWARF calling convention");
5455
5456 unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
5457 if (!CC)
5458 return tokError("invalid DWARF calling convention" + Twine(" '") +
5459 Lex.getStrVal() + "'");
5460 assert(CC <= Result.Max && "Expected valid DWARF calling convention");
5461 Result.assign(CC);
5462 Lex.Lex();
5463 return false;
5464}
5465
5466template <>
5467bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5468 EmissionKindField &Result) {
5469 if (Lex.getKind() == lltok::APSInt)
5470 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5471
5472 if (Lex.getKind() != lltok::EmissionKind)
5473 return tokError("expected emission kind");
5474
5475 auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
5476 if (!Kind)
5477 return tokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
5478 "'");
5479 assert(*Kind <= Result.Max && "Expected valid emission kind");
5480 Result.assign(*Kind);
5481 Lex.Lex();
5482 return false;
5483}
5484
5485template <>
5486bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5487 FixedPointKindField &Result) {
5488 if (Lex.getKind() == lltok::APSInt)
5489 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5490
5491 if (Lex.getKind() != lltok::FixedPointKind)
5492 return tokError("expected fixed-point kind");
5493
5494 auto Kind = DIFixedPointType::getFixedPointKind(Lex.getStrVal());
5495 if (!Kind)
5496 return tokError("invalid fixed-point kind" + Twine(" '") + Lex.getStrVal() +
5497 "'");
5498 assert(*Kind <= Result.Max && "Expected valid fixed-point kind");
5499 Result.assign(*Kind);
5500 Lex.Lex();
5501 return false;
5502}
5503
5504template <>
5505bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5506 NameTableKindField &Result) {
5507 if (Lex.getKind() == lltok::APSInt)
5508 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5509
5510 if (Lex.getKind() != lltok::NameTableKind)
5511 return tokError("expected nameTable kind");
5512
5513 auto Kind = DICompileUnit::getNameTableKind(Lex.getStrVal());
5514 if (!Kind)
5515 return tokError("invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
5516 "'");
5517 assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
5518 Result.assign((unsigned)*Kind);
5519 Lex.Lex();
5520 return false;
5521}
5522
5523template <>
5524bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5525 DwarfAttEncodingField &Result) {
5526 if (Lex.getKind() == lltok::APSInt)
5527 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5528
5529 if (Lex.getKind() != lltok::DwarfAttEncoding)
5530 return tokError("expected DWARF type attribute encoding");
5531
5532 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
5533 if (!Encoding)
5534 return tokError("invalid DWARF type attribute encoding" + Twine(" '") +
5535 Lex.getStrVal() + "'");
5536 assert(Encoding <= Result.Max && "Expected valid DWARF language");
5537 Result.assign(Encoding);
5538 Lex.Lex();
5539 return false;
5540}
5541
5542/// DIFlagField
5543/// ::= uint32
5544/// ::= DIFlagVector
5545/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
5546template <>
5547bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
5548
5549 // parser for a single flag.
5550 auto parseFlag = [&](DINode::DIFlags &Val) {
5551 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
5552 uint32_t TempVal = static_cast<uint32_t>(Val);
5553 bool Res = parseUInt32(TempVal);
5554 Val = static_cast<DINode::DIFlags>(TempVal);
5555 return Res;
5556 }
5557
5558 if (Lex.getKind() != lltok::DIFlag)
5559 return tokError("expected debug info flag");
5560
5561 Val = DINode::getFlag(Lex.getStrVal());
5562 if (!Val)
5563 return tokError(Twine("invalid debug info flag '") + Lex.getStrVal() +
5564 "'");
5565 Lex.Lex();
5566 return false;
5567 };
5568
5569 // parse the flags and combine them together.
5570 DINode::DIFlags Combined = DINode::FlagZero;
5571 do {
5572 DINode::DIFlags Val;
5573 if (parseFlag(Val))
5574 return true;
5575 Combined |= Val;
5576 } while (EatIfPresent(lltok::bar));
5577
5578 Result.assign(Combined);
5579 return false;
5580}
5581
5582/// DISPFlagField
5583/// ::= uint32
5584/// ::= DISPFlagVector
5585/// ::= DISPFlagVector '|' DISPFlag* '|' uint32
5586template <>
5587bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) {
5588
5589 // parser for a single flag.
5590 auto parseFlag = [&](DISubprogram::DISPFlags &Val) {
5591 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
5592 uint32_t TempVal = static_cast<uint32_t>(Val);
5593 bool Res = parseUInt32(TempVal);
5594 Val = static_cast<DISubprogram::DISPFlags>(TempVal);
5595 return Res;
5596 }
5597
5598 if (Lex.getKind() != lltok::DISPFlag)
5599 return tokError("expected debug info flag");
5600
5601 Val = DISubprogram::getFlag(Lex.getStrVal());
5602 if (!Val)
5603 return tokError(Twine("invalid subprogram debug info flag '") +
5604 Lex.getStrVal() + "'");
5605 Lex.Lex();
5606 return false;
5607 };
5608
5609 // parse the flags and combine them together.
5610 DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero;
5611 do {
5613 if (parseFlag(Val))
5614 return true;
5615 Combined |= Val;
5616 } while (EatIfPresent(lltok::bar));
5617
5618 Result.assign(Combined);
5619 return false;
5620}
5621
5622template <>
5623bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) {
5624 if (Lex.getKind() != lltok::APSInt)
5625 return tokError("expected signed integer");
5626
5627 auto &S = Lex.getAPSIntVal();
5628 if (S < Result.Min)
5629 return tokError("value for '" + Name + "' too small, limit is " +
5630 Twine(Result.Min));
5631 if (S > Result.Max)
5632 return tokError("value for '" + Name + "' too large, limit is " +
5633 Twine(Result.Max));
5634 Result.assign(S.getExtValue());
5635 assert(Result.Val >= Result.Min && "Expected value in range");
5636 assert(Result.Val <= Result.Max && "Expected value in range");
5637 Lex.Lex();
5638 return false;
5639}
5640
5641template <>
5642bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
5643 switch (Lex.getKind()) {
5644 default:
5645 return tokError("expected 'true' or 'false'");
5646 case lltok::kw_true:
5647 Result.assign(true);
5648 break;
5649 case lltok::kw_false:
5650 Result.assign(false);
5651 break;
5652 }
5653 Lex.Lex();
5654 return false;
5655}
5656
5657template <>
5658bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) {
5659 if (Lex.getKind() == lltok::kw_null) {
5660 if (!Result.AllowNull)
5661 return tokError("'" + Name + "' cannot be null");
5662 Lex.Lex();
5663 Result.assign(nullptr);
5664 return false;
5665 }
5666
5667 Metadata *MD;
5668 if (parseMetadata(MD, nullptr))
5669 return true;
5670
5671 Result.assign(MD);
5672 return false;
5673}
5674
5675template <>
5676bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5677 MDSignedOrMDField &Result) {
5678 // Try to parse a signed int.
5679 if (Lex.getKind() == lltok::APSInt) {
5680 MDSignedField Res = Result.A;
5681 if (!parseMDField(Loc, Name, Res)) {
5682 Result.assign(Res);
5683 return false;
5684 }
5685 return true;
5686 }
5687
5688 // Otherwise, try to parse as an MDField.
5689 MDField Res = Result.B;
5690 if (!parseMDField(Loc, Name, Res)) {
5691 Result.assign(Res);
5692 return false;
5693 }
5694
5695 return true;
5696}
5697
5698template <>
5699bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5700 MDUnsignedOrMDField &Result) {
5701 // Try to parse an unsigned int.
5702 if (Lex.getKind() == lltok::APSInt) {
5703 MDUnsignedField Res = Result.A;
5704 if (!parseMDField(Loc, Name, Res)) {
5705 Result.assign(Res);
5706 return false;
5707 }
5708 return true;
5709 }
5710
5711 // Otherwise, try to parse as an MDField.
5712 MDField Res = Result.B;
5713 if (!parseMDField(Loc, Name, Res)) {
5714 Result.assign(Res);
5715 return false;
5716 }
5717
5718 return true;
5719}
5720
5721template <>
5722bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
5723 LocTy ValueLoc = Lex.getLoc();
5724 std::string S;
5725 if (parseStringConstant(S))
5726 return true;
5727
5728 if (S.empty()) {
5729 switch (Result.EmptyIs) {
5730 case MDStringField::EmptyIs::Null:
5731 Result.assign(nullptr);
5732 return false;
5733 case MDStringField::EmptyIs::Empty:
5734 break;
5735 case MDStringField::EmptyIs::Error:
5736 return error(ValueLoc, "'" + Name + "' cannot be empty");
5737 }
5738 }
5739
5740 Result.assign(MDString::get(Context, S));
5741 return false;
5742}
5743
5744template <>
5745bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
5747 if (parseMDNodeVector(MDs))
5748 return true;
5749
5750 Result.assign(std::move(MDs));
5751 return false;
5752}
5753
5754template <>
5755bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5756 ChecksumKindField &Result) {
5757 std::optional<DIFile::ChecksumKind> CSKind =
5758 DIFile::getChecksumKind(Lex.getStrVal());
5759
5760 if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
5761 return tokError("invalid checksum kind" + Twine(" '") + Lex.getStrVal() +
5762 "'");
5763
5764 Result.assign(*CSKind);
5765 Lex.Lex();
5766 return false;
5767}
5768
5769} // end namespace llvm
5770
5771template <class ParserTy>
5772bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) {
5773 do {
5774 if (Lex.getKind() != lltok::LabelStr)
5775 return tokError("expected field label here");
5776
5777 if (ParseField())
5778 return true;
5779 } while (EatIfPresent(lltok::comma));
5780
5781 return false;
5782}
5783
5784template <class ParserTy>
5785bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) {
5786 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5787 Lex.Lex();
5788
5789 if (parseToken(lltok::lparen, "expected '(' here"))
5790 return true;
5791 if (Lex.getKind() != lltok::rparen)
5792 if (parseMDFieldsImplBody(ParseField))
5793 return true;
5794
5795 ClosingLoc = Lex.getLoc();
5796 return parseToken(lltok::rparen, "expected ')' here");
5797}
5798
5799template <class FieldTy>
5800bool LLParser::parseMDField(StringRef Name, FieldTy &Result) {
5801 if (Result.Seen)
5802 return tokError("field '" + Name + "' cannot be specified more than once");
5803
5804 LocTy Loc = Lex.getLoc();
5805 Lex.Lex();
5806 return parseMDField(Loc, Name, Result);
5807}
5808
5809bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
5810 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5811
5812#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
5813 if (Lex.getStrVal() == #CLASS) \
5814 return parse##CLASS(N, IsDistinct);
5815#include "llvm/IR/Metadata.def"
5816
5817 return tokError("expected metadata type");
5818}
5819
5820#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
5821#define NOP_FIELD(NAME, TYPE, INIT)
5822#define REQUIRE_FIELD(NAME, TYPE, INIT) \
5823 if (!NAME.Seen) \
5824 return error(ClosingLoc, "missing required field '" #NAME "'");
5825#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
5826 if (Lex.getStrVal() == #NAME) \
5827 return parseMDField(#NAME, NAME);
5828#define PARSE_MD_FIELDS() \
5829 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
5830 do { \
5831 LocTy ClosingLoc; \
5832 if (parseMDFieldsImpl( \
5833 [&]() -> bool { \
5834 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
5835 return tokError(Twine("invalid field '") + Lex.getStrVal() + \
5836 "'"); \
5837 }, \
5838 ClosingLoc)) \
5839 return true; \
5840 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
5841 } while (false)
5842#define GET_OR_DISTINCT(CLASS, ARGS) \
5843 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
5844
5845/// parseDILocationFields:
5846/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
5847/// isImplicitCode: true, atomGroup: 1, atomRank: 1)
5848bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) {
5849#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5850 OPTIONAL(line, LineField, ); \
5851 OPTIONAL(column, ColumnField, ); \
5852 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5853 OPTIONAL(inlinedAt, MDField, ); \
5854 OPTIONAL(isImplicitCode, MDBoolField, (false)); \
5855 OPTIONAL(atomGroup, MDUnsignedField, (0, UINT64_MAX)); \
5856 OPTIONAL(atomRank, MDUnsignedField, (0, UINT8_MAX)); \
5857 OPTIONAL(irlayers, MDField, );
5859#undef VISIT_MD_FIELDS
5860
5861 Result =
5862 GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val,
5863 inlinedAt.Val, isImplicitCode.Val,
5864 atomGroup.Val, atomRank.Val, irlayers.Val));
5865 return false;
5866}
5867
5868bool LLParser::parseDILayerLoc(MDNode *&Result, bool IsDistinct) {
5869#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5870 OPTIONAL(line, LineField, ); \
5871 OPTIONAL(column, ColumnField, ); \
5872 REQUIRED(file, MDField, (/* AllowNull */ false)); \
5873 REQUIRED(kind, MDStringField, );
5875#undef VISIT_MD_FIELDS
5876
5877 Result = GET_OR_DISTINCT(DILayerLoc,
5878 (Context, kind.Val, file.Val, line.Val, column.Val));
5879 return false;
5880}
5881
5882bool LLParser::parseDILayerLocList(MDNode *&Result, bool IsDistinct) {
5883 // ::= !DILayerLocList(!a, !b, ...)
5884 Lex.Lex(); // eat the '!DILayerLocList' type name
5885 if (parseToken(lltok::lparen, "expected '(' here"))
5886 return true;
5888 if (!EatIfPresent(lltok::rparen)) {
5889 do {
5890 Metadata *MD;
5891 if (parseMetadata(MD, nullptr))
5892 return true;
5893 Layers.push_back(MD);
5894 } while (EatIfPresent(lltok::comma));
5895 if (parseToken(lltok::rparen, "expected ')' here"))
5896 return true;
5897 }
5898 Result = GET_OR_DISTINCT(DILayerLocList, (Context, Layers));
5899 return false;
5900}
5901
5902/// parseDIAssignID:
5903/// ::= distinct !DIAssignID()
5904bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) {
5905 if (!IsDistinct)
5906 return tokError("missing 'distinct', required for !DIAssignID()");
5907
5908 Lex.Lex();
5909
5910 // Now eat the parens.
5911 if (parseToken(lltok::lparen, "expected '(' here"))
5912 return true;
5913 if (parseToken(lltok::rparen, "expected ')' here"))
5914 return true;
5915
5917 return false;
5918}
5919
5920/// parseGenericDINode:
5921/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
5922bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) {
5923#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5924 REQUIRED(tag, DwarfTagField, ); \
5925 OPTIONAL(header, MDStringField, ); \
5926 OPTIONAL(operands, MDFieldList, );
5928#undef VISIT_MD_FIELDS
5929
5930 Result = GET_OR_DISTINCT(GenericDINode,
5931 (Context, tag.Val, header.Val, operands.Val));
5932 return false;
5933}
5934
5935/// parseDISubrangeType:
5936/// ::= !DISubrangeType(name: "whatever", file: !0,
5937/// line: 7, scope: !1, baseType: !2, size: 32,
5938/// align: 32, flags: 0, lowerBound: !3
5939/// upperBound: !4, stride: !5, bias: !6)
5940bool LLParser::parseDISubrangeType(MDNode *&Result, bool IsDistinct) {
5941#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5942 OPTIONAL(name, MDStringField, ); \
5943 OPTIONAL(file, MDField, ); \
5944 OPTIONAL(line, LineField, ); \
5945 OPTIONAL(scope, MDField, ); \
5946 OPTIONAL(baseType, MDField, ); \
5947 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5948 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5949 OPTIONAL(flags, DIFlagField, ); \
5950 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5951 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5952 OPTIONAL(stride, MDSignedOrMDField, ); \
5953 OPTIONAL(bias, MDSignedOrMDField, );
5955#undef VISIT_MD_FIELDS
5956
5957 auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
5958 if (Bound.isMDSignedField())
5960 Type::getInt64Ty(Context), Bound.getMDSignedValue()));
5961 if (Bound.isMDField())
5962 return Bound.getMDFieldValue();
5963 return nullptr;
5964 };
5965
5966 Metadata *LowerBound = convToMetadata(lowerBound);
5967 Metadata *UpperBound = convToMetadata(upperBound);
5968 Metadata *Stride = convToMetadata(stride);
5969 Metadata *Bias = convToMetadata(bias);
5970
5972 DISubrangeType, (Context, name.Val, file.Val, line.Val, scope.Val,
5973 size.getValueAsMetadata(Context), align.Val, flags.Val,
5974 baseType.Val, LowerBound, UpperBound, Stride, Bias));
5975
5976 return false;
5977}
5978
5979/// parseDISubrange:
5980/// ::= !DISubrange(count: 30, lowerBound: 2)
5981/// ::= !DISubrange(count: !node, lowerBound: 2)
5982/// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3)
5983bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) {
5984#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5985 OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
5986 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5987 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5988 OPTIONAL(stride, MDSignedOrMDField, );
5990#undef VISIT_MD_FIELDS
5991
5992 Metadata *Count = nullptr;
5993 Metadata *LowerBound = nullptr;
5994 Metadata *UpperBound = nullptr;
5995 Metadata *Stride = nullptr;
5996
5997 auto convToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * {
5998 if (Bound.isMDSignedField())
6000 Type::getInt64Ty(Context), Bound.getMDSignedValue()));
6001 if (Bound.isMDField())
6002 return Bound.getMDFieldValue();
6003 return nullptr;
6004 };
6005
6006 Count = convToMetadata(count);
6007 LowerBound = convToMetadata(lowerBound);
6008 UpperBound = convToMetadata(upperBound);
6009 Stride = convToMetadata(stride);
6010
6011 Result = GET_OR_DISTINCT(DISubrange,
6012 (Context, Count, LowerBound, UpperBound, Stride));
6013
6014 return false;
6015}
6016
6017/// parseDIGenericSubrange:
6018/// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride:
6019/// !node3)
6020bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) {
6021#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6022 OPTIONAL(count, MDSignedOrMDField, ); \
6023 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
6024 OPTIONAL(upperBound, MDSignedOrMDField, ); \
6025 OPTIONAL(stride, MDSignedOrMDField, );
6027#undef VISIT_MD_FIELDS
6028
6029 auto ConvToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * {
6030 if (Bound.isMDSignedField())
6031 return DIExpression::get(
6032 Context, {dwarf::DW_OP_consts,
6033 static_cast<uint64_t>(Bound.getMDSignedValue())});
6034 if (Bound.isMDField())
6035 return Bound.getMDFieldValue();
6036 return nullptr;
6037 };
6038
6039 Metadata *Count = ConvToMetadata(count);
6040 Metadata *LowerBound = ConvToMetadata(lowerBound);
6041 Metadata *UpperBound = ConvToMetadata(upperBound);
6042 Metadata *Stride = ConvToMetadata(stride);
6043
6044 Result = GET_OR_DISTINCT(DIGenericSubrange,
6045 (Context, Count, LowerBound, UpperBound, Stride));
6046
6047 return false;
6048}
6049
6050/// parseDIEnumerator:
6051/// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
6052bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) {
6053#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6054 REQUIRED(name, MDStringField, ); \
6055 REQUIRED(value, MDAPSIntField, ); \
6056 OPTIONAL(isUnsigned, MDBoolField, (false));
6058#undef VISIT_MD_FIELDS
6059
6060 if (isUnsigned.Val && value.Val.isNegative())
6061 return tokError("unsigned enumerator with negative value");
6062
6063 APSInt Value(value.Val);
6064 // Add a leading zero so that unsigned values with the msb set are not
6065 // mistaken for negative values when used for signed enumerators.
6066 if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet())
6067 Value = Value.zext(Value.getBitWidth() + 1);
6068
6069 Result =
6070 GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
6071
6072 return false;
6073}
6074
6075/// parseDIBasicType:
6076/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
6077/// encoding: DW_ATE_encoding, flags: 0)
6078bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) {
6079#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6080 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
6081 OPTIONAL(name, MDStringField, ); \
6082 OPTIONAL(file, MDField, ); \
6083 OPTIONAL(line, LineField, ); \
6084 OPTIONAL(scope, MDField, ); \
6085 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6086 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6087 OPTIONAL(dataSize, MDUnsignedField, (0, UINT32_MAX)); \
6088 OPTIONAL(encoding, DwarfAttEncodingField, ); \
6089 OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
6090 OPTIONAL(flags, DIFlagField, );
6092#undef VISIT_MD_FIELDS
6093
6095 DIBasicType, (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val,
6096 size.getValueAsMetadata(Context), align.Val, encoding.Val,
6097 num_extra_inhabitants.Val, dataSize.Val, flags.Val));
6098 return false;
6099}
6100
6101/// parseDIFixedPointType:
6102/// ::= !DIFixedPointType(tag: DW_TAG_base_type, name: "xyz", size: 32,
6103/// align: 32, encoding: DW_ATE_signed_fixed,
6104/// flags: 0, kind: Rational, factor: 3, numerator: 1,
6105/// denominator: 8)
6106bool LLParser::parseDIFixedPointType(MDNode *&Result, bool IsDistinct) {
6107#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6108 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
6109 OPTIONAL(name, MDStringField, ); \
6110 OPTIONAL(file, MDField, ); \
6111 OPTIONAL(line, LineField, ); \
6112 OPTIONAL(scope, MDField, ); \
6113 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6114 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6115 OPTIONAL(encoding, DwarfAttEncodingField, ); \
6116 OPTIONAL(flags, DIFlagField, ); \
6117 OPTIONAL(kind, FixedPointKindField, ); \
6118 OPTIONAL(factor, MDSignedField, ); \
6119 OPTIONAL(numerator, MDAPSIntField, ); \
6120 OPTIONAL(denominator, MDAPSIntField, );
6122#undef VISIT_MD_FIELDS
6123
6124 Result = GET_OR_DISTINCT(DIFixedPointType,
6125 (Context, tag.Val, name.Val, file.Val, line.Val,
6126 scope.Val, size.getValueAsMetadata(Context),
6127 align.Val, encoding.Val, flags.Val, kind.Val,
6128 factor.Val, numerator.Val, denominator.Val));
6129 return false;
6130}
6131
6132/// parseDIStringType:
6133/// ::= !DIStringType(name: "character(4)", size: 32, align: 32)
6134bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) {
6135#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6136 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \
6137 OPTIONAL(name, MDStringField, ); \
6138 OPTIONAL(stringLength, MDField, ); \
6139 OPTIONAL(stringLengthExpression, MDField, ); \
6140 OPTIONAL(stringLocationExpression, MDField, ); \
6141 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6142 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6143 OPTIONAL(encoding, DwarfAttEncodingField, ); \
6144 OPTIONAL(charType, MDField, );
6146#undef VISIT_MD_FIELDS
6147
6149 DIStringType,
6150 (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val,
6151 stringLocationExpression.Val, size.getValueAsMetadata(Context),
6152 align.Val, encoding.Val, charType.Val));
6153 return false;
6154}
6155
6156/// parseDIDerivedType:
6157/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
6158/// line: 7, scope: !1, baseType: !2, size: 32,
6159/// align: 32, offset: 0, flags: 0, extraData: !3,
6160/// dwarfAddressSpace: 3, ptrAuthKey: 1,
6161/// ptrAuthIsAddressDiscriminated: true,
6162/// ptrAuthExtraDiscriminator: 0x1234,
6163/// ptrAuthIsaPointer: 1, ptrAuthAuthenticatesNullValues:1
6164/// )
6165bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) {
6166#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6167 REQUIRED(tag, DwarfTagField, ); \
6168 OPTIONAL(name, MDStringField, ); \
6169 OPTIONAL(file, MDField, ); \
6170 OPTIONAL(line, LineField, ); \
6171 OPTIONAL(scope, MDField, ); \
6172 REQUIRED(baseType, MDField, ); \
6173 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6174 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6175 OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6176 OPTIONAL(flags, DIFlagField, ); \
6177 OPTIONAL(extraData, MDField, ); \
6178 OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \
6179 OPTIONAL(annotations, MDField, ); \
6180 OPTIONAL(ptrAuthKey, MDUnsignedField, (0, 7)); \
6181 OPTIONAL(ptrAuthIsAddressDiscriminated, MDBoolField, ); \
6182 OPTIONAL(ptrAuthExtraDiscriminator, MDUnsignedField, (0, 0xffff)); \
6183 OPTIONAL(ptrAuthIsaPointer, MDBoolField, ); \
6184 OPTIONAL(ptrAuthAuthenticatesNullValues, MDBoolField, );
6186#undef VISIT_MD_FIELDS
6187
6188 std::optional<unsigned> DWARFAddressSpace;
6189 if (dwarfAddressSpace.Val != UINT32_MAX)
6190 DWARFAddressSpace = dwarfAddressSpace.Val;
6191 std::optional<DIDerivedType::PtrAuthData> PtrAuthData;
6192 if (ptrAuthKey.Val)
6193 PtrAuthData.emplace(
6194 (unsigned)ptrAuthKey.Val, ptrAuthIsAddressDiscriminated.Val,
6195 (unsigned)ptrAuthExtraDiscriminator.Val, ptrAuthIsaPointer.Val,
6196 ptrAuthAuthenticatesNullValues.Val);
6197
6199 DIDerivedType, (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val,
6200 baseType.Val, size.getValueAsMetadata(Context), align.Val,
6201 offset.getValueAsMetadata(Context), DWARFAddressSpace,
6202 PtrAuthData, flags.Val, extraData.Val, annotations.Val));
6203 return false;
6204}
6205
6206bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
6207#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6208 REQUIRED(tag, DwarfTagField, ); \
6209 OPTIONAL(name, MDStringField, ); \
6210 OPTIONAL(file, MDField, ); \
6211 OPTIONAL(line, LineField, ); \
6212 OPTIONAL(scope, MDField, ); \
6213 OPTIONAL(baseType, MDField, ); \
6214 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6215 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6216 OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \
6217 OPTIONAL(flags, DIFlagField, ); \
6218 OPTIONAL(elements, MDField, ); \
6219 OPTIONAL(runtimeLang, DwarfLangField, ); \
6220 OPTIONAL(enumKind, DwarfEnumKindField, ); \
6221 OPTIONAL(vtableHolder, MDField, ); \
6222 OPTIONAL(templateParams, MDField, ); \
6223 OPTIONAL(identifier, MDStringField, ); \
6224 OPTIONAL(discriminator, MDField, ); \
6225 OPTIONAL(dataLocation, MDField, ); \
6226 OPTIONAL(associated, MDField, ); \
6227 OPTIONAL(allocated, MDField, ); \
6228 OPTIONAL(rank, MDSignedOrMDField, ); \
6229 OPTIONAL(annotations, MDField, ); \
6230 OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
6231 OPTIONAL(specification, MDField, ); \
6232 OPTIONAL(bitStride, MDField, );
6234#undef VISIT_MD_FIELDS
6235
6236 Metadata *Rank = nullptr;
6237 if (rank.isMDSignedField())
6239 Type::getInt64Ty(Context), rank.getMDSignedValue()));
6240 else if (rank.isMDField())
6241 Rank = rank.getMDFieldValue();
6242
6243 std::optional<unsigned> EnumKind;
6244 if (enumKind.Val != dwarf::DW_APPLE_ENUM_KIND_invalid)
6245 EnumKind = enumKind.Val;
6246
6247 // If this has an identifier try to build an ODR type.
6248 if (identifier.Val)
6249 if (auto *CT = DICompositeType::buildODRType(
6250 Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
6251 scope.Val, baseType.Val, size.getValueAsMetadata(Context),
6252 align.Val, offset.getValueAsMetadata(Context), specification.Val,
6253 num_extra_inhabitants.Val, flags.Val, elements.Val, runtimeLang.Val,
6254 EnumKind, vtableHolder.Val, templateParams.Val, discriminator.Val,
6255 dataLocation.Val, associated.Val, allocated.Val, Rank,
6256 annotations.Val, bitStride.Val)) {
6257 Result = CT;
6258 return false;
6259 }
6260
6261 // Create a new node, and save it in the context if it belongs in the type
6262 // map.
6264 DICompositeType,
6265 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
6266 size.getValueAsMetadata(Context), align.Val,
6267 offset.getValueAsMetadata(Context), flags.Val, elements.Val,
6268 runtimeLang.Val, EnumKind, vtableHolder.Val, templateParams.Val,
6269 identifier.Val, discriminator.Val, dataLocation.Val, associated.Val,
6270 allocated.Val, Rank, annotations.Val, specification.Val,
6271 num_extra_inhabitants.Val, bitStride.Val));
6272 return false;
6273}
6274
6275bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) {
6276#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6277 OPTIONAL(flags, DIFlagField, ); \
6278 OPTIONAL(cc, DwarfCCField, ); \
6279 REQUIRED(types, MDField, );
6281#undef VISIT_MD_FIELDS
6282
6283 Result = GET_OR_DISTINCT(DISubroutineType,
6284 (Context, flags.Val, cc.Val, types.Val));
6285 return false;
6286}
6287
6288/// parseDIFileType:
6289/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
6290/// checksumkind: CSK_MD5,
6291/// checksum: "000102030405060708090a0b0c0d0e0f",
6292/// source: "source file contents")
6293bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) {
6294 // The default constructed value for checksumkind is required, but will never
6295 // be used, as the parser checks if the field was actually Seen before using
6296 // the Val.
6297#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6298 REQUIRED(filename, MDStringField, ); \
6299 REQUIRED(directory, MDStringField, ); \
6300 OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
6301 OPTIONAL(checksum, MDStringField, ); \
6302 OPTIONAL(source, MDStringField, (MDStringField::EmptyIs::Empty));
6304#undef VISIT_MD_FIELDS
6305
6306 std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
6307 if (checksumkind.Seen && checksum.Seen)
6308 OptChecksum.emplace(checksumkind.Val, checksum.Val);
6309 else if (checksumkind.Seen || checksum.Seen)
6310 return tokError("'checksumkind' and 'checksum' must be provided together");
6311
6312 MDString *Source = nullptr;
6313 if (source.Seen)
6314 Source = source.Val;
6316 DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source));
6317 return false;
6318}
6319
6320/// parseDICompileUnit:
6321/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
6322/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
6323/// splitDebugFilename: "abc.debug",
6324/// emissionKind: FullDebug, enums: !1, retainedTypes: !2,
6325/// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd,
6326/// sysroot: "/", sdk: "MacOSX.sdk",
6327/// dialect: DW_LLVM_LANG_DIALECT_simt)
6328bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
6329 if (!IsDistinct)
6330 return tokError("missing 'distinct', required for !DICompileUnit");
6331
6332 LocTy Loc = Lex.getLoc();
6333
6334#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6335 REQUIRED(file, MDField, (/* AllowNull */ false)); \
6336 OPTIONAL(language, DwarfLangField, ); \
6337 OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \
6338 OPTIONAL(sourceLanguageVersion, MDUnsignedField, (0, UINT32_MAX)); \
6339 OPTIONAL(producer, MDStringField, ); \
6340 OPTIONAL(isOptimized, MDBoolField, ); \
6341 OPTIONAL(flags, MDStringField, ); \
6342 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
6343 OPTIONAL(splitDebugFilename, MDStringField, ); \
6344 OPTIONAL(emissionKind, EmissionKindField, ); \
6345 OPTIONAL(enums, MDField, ); \
6346 OPTIONAL(retainedTypes, MDField, ); \
6347 OPTIONAL(globals, MDField, ); \
6348 OPTIONAL(imports, MDField, ); \
6349 OPTIONAL(macros, MDField, ); \
6350 OPTIONAL(dwoId, MDUnsignedField, ); \
6351 OPTIONAL(splitDebugInlining, MDBoolField, = true); \
6352 OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \
6353 OPTIONAL(nameTableKind, NameTableKindField, ); \
6354 OPTIONAL(rangesBaseAddress, MDBoolField, = false); \
6355 OPTIONAL(sysroot, MDStringField, ); \
6356 OPTIONAL(sdk, MDStringField, ); \
6357 OPTIONAL(dialect, DwarfLangDialectField, );
6359#undef VISIT_MD_FIELDS
6360
6361 if (!language.Seen && !sourceLanguageName.Seen)
6362 return error(Loc, "missing one of 'language' or 'sourceLanguageName', "
6363 "required for !DICompileUnit");
6364
6365 if (language.Seen && sourceLanguageName.Seen)
6366 return error(Loc, "can only specify one of 'language' and "
6367 "'sourceLanguageName' on !DICompileUnit");
6368
6369 if (sourceLanguageVersion.Seen && !sourceLanguageName.Seen)
6370 return error(Loc, "'sourceLanguageVersion' requires an associated "
6371 "'sourceLanguageName' on !DICompileUnit");
6372
6373 uint16_t Dialect = static_cast<uint16_t>(dialect.Val);
6374 DISourceLanguageName SourceLanguage =
6375 language.Seen
6376 ? DISourceLanguageName(static_cast<uint16_t>(language.Val), Dialect)
6377 : DISourceLanguageName(
6378 static_cast<uint16_t>(sourceLanguageName.Val),
6379 static_cast<uint32_t>(sourceLanguageVersion.Val), Dialect);
6380
6382 Context, SourceLanguage, file.Val, producer.Val, isOptimized.Val,
6383 flags.Val, runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val,
6384 enums.Val, retainedTypes.Val, globals.Val, imports.Val, macros.Val,
6385 dwoId.Val, splitDebugInlining.Val, debugInfoForProfiling.Val,
6386 nameTableKind.Val, rangesBaseAddress.Val, sysroot.Val, sdk.Val);
6387 return false;
6388}
6389
6390/// parseDISubprogram:
6391/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
6392/// file: !1, line: 7, type: !2, isLocal: false,
6393/// isDefinition: true, scopeLine: 8, containingType: !3,
6394/// virtuality: DW_VIRTUALTIY_pure_virtual,
6395/// virtualIndex: 10, thisAdjustment: 4, flags: 11,
6396/// spFlags: 10, isOptimized: false, templateParams: !4,
6397/// declaration: !5, retainedNodes: !6, thrownTypes: !7,
6398/// annotations: !8)
6399bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
6400 auto Loc = Lex.getLoc();
6401#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6402 OPTIONAL(scope, MDField, ); \
6403 OPTIONAL(name, MDStringField, ); \
6404 OPTIONAL(linkageName, MDStringField, ); \
6405 OPTIONAL(file, MDField, ); \
6406 OPTIONAL(line, LineField, ); \
6407 REQUIRED(type, MDField, (/* AllowNull */ false)); \
6408 OPTIONAL(isLocal, MDBoolField, ); \
6409 OPTIONAL(isDefinition, MDBoolField, (true)); \
6410 OPTIONAL(scopeLine, LineField, ); \
6411 OPTIONAL(containingType, MDField, ); \
6412 OPTIONAL(virtuality, DwarfVirtualityField, ); \
6413 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
6414 OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \
6415 OPTIONAL(flags, DIFlagField, ); \
6416 OPTIONAL(spFlags, DISPFlagField, ); \
6417 OPTIONAL(isOptimized, MDBoolField, ); \
6418 OPTIONAL(unit, MDField, ); \
6419 OPTIONAL(templateParams, MDField, ); \
6420 OPTIONAL(declaration, MDField, ); \
6421 OPTIONAL(retainedNodes, MDField, ); \
6422 OPTIONAL(thrownTypes, MDField, ); \
6423 OPTIONAL(annotations, MDField, ); \
6424 OPTIONAL(targetFuncName, MDStringField, ); \
6425 OPTIONAL(keyInstructions, MDBoolField, );
6427#undef VISIT_MD_FIELDS
6428
6429 // An explicit spFlags field takes precedence over individual fields in
6430 // older IR versions.
6431 DISubprogram::DISPFlags SPFlags =
6432 spFlags.Seen ? spFlags.Val
6433 : DISubprogram::toSPFlags(isLocal.Val, isDefinition.Val,
6434 isOptimized.Val, virtuality.Val);
6435 if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct)
6436 return error(
6437 Loc,
6438 "missing 'distinct', required for !DISubprogram that is a Definition");
6440 DISubprogram,
6441 (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
6442 type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
6443 thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
6444 declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val,
6445 targetFuncName.Val, keyInstructions.Val));
6446
6447 if (IsDistinct)
6448 NewDistinctSPs.push_back(cast<DISubprogram>(Result));
6449
6450 return false;
6451}
6452
6453/// parseDILexicalBlock:
6454/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
6455bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
6456#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6457 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6458 OPTIONAL(file, MDField, ); \
6459 OPTIONAL(line, LineField, ); \
6460 OPTIONAL(column, ColumnField, );
6462#undef VISIT_MD_FIELDS
6463
6465 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
6466 return false;
6467}
6468
6469/// parseDILexicalBlockFile:
6470/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
6471bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
6472#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6473 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6474 OPTIONAL(file, MDField, ); \
6475 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
6477#undef VISIT_MD_FIELDS
6478
6479 Result = GET_OR_DISTINCT(DILexicalBlockFile,
6480 (Context, scope.Val, file.Val, discriminator.Val));
6481 return false;
6482}
6483
6484/// parseDICommonBlock:
6485/// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9)
6486bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) {
6487#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6488 REQUIRED(scope, MDField, ); \
6489 OPTIONAL(declaration, MDField, ); \
6490 OPTIONAL(name, MDStringField, ); \
6491 OPTIONAL(file, MDField, ); \
6492 OPTIONAL(line, LineField, );
6494#undef VISIT_MD_FIELDS
6495
6496 Result = GET_OR_DISTINCT(DICommonBlock,
6497 (Context, scope.Val, declaration.Val, name.Val,
6498 file.Val, line.Val));
6499 return false;
6500}
6501
6502/// parseDINamespace:
6503/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
6504bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) {
6505#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6506 REQUIRED(scope, MDField, ); \
6507 OPTIONAL(name, MDStringField, ); \
6508 OPTIONAL(exportSymbols, MDBoolField, );
6510#undef VISIT_MD_FIELDS
6511
6512 Result = GET_OR_DISTINCT(DINamespace,
6513 (Context, scope.Val, name.Val, exportSymbols.Val));
6514 return false;
6515}
6516
6517/// parseDIMacro:
6518/// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value:
6519/// "SomeValue")
6520bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) {
6521#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6522 REQUIRED(type, DwarfMacinfoTypeField, ); \
6523 OPTIONAL(line, LineField, ); \
6524 REQUIRED(name, MDStringField, ); \
6525 OPTIONAL(value, MDStringField, );
6527#undef VISIT_MD_FIELDS
6528
6529 Result = GET_OR_DISTINCT(DIMacro,
6530 (Context, type.Val, line.Val, name.Val, value.Val));
6531 return false;
6532}
6533
6534/// parseDIMacroFile:
6535/// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
6536bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) {
6537#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6538 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
6539 OPTIONAL(line, LineField, ); \
6540 REQUIRED(file, MDField, ); \
6541 OPTIONAL(nodes, MDField, );
6543#undef VISIT_MD_FIELDS
6544
6545 Result = GET_OR_DISTINCT(DIMacroFile,
6546 (Context, type.Val, line.Val, file.Val, nodes.Val));
6547 return false;
6548}
6549
6550/// parseDIModule:
6551/// ::= !DIModule(scope: !0, name: "SomeModule", configMacros:
6552/// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes",
6553/// file: !1, line: 4, isDecl: false)
6554bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) {
6555#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6556 REQUIRED(scope, MDField, ); \
6557 REQUIRED(name, MDStringField, ); \
6558 OPTIONAL(configMacros, MDStringField, ); \
6559 OPTIONAL(includePath, MDStringField, ); \
6560 OPTIONAL(apinotes, MDStringField, ); \
6561 OPTIONAL(file, MDField, ); \
6562 OPTIONAL(line, LineField, ); \
6563 OPTIONAL(isDecl, MDBoolField, );
6565#undef VISIT_MD_FIELDS
6566
6567 Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val,
6568 configMacros.Val, includePath.Val,
6569 apinotes.Val, line.Val, isDecl.Val));
6570 return false;
6571}
6572
6573/// parseDITemplateTypeParameter:
6574/// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false)
6575bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
6576#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6577 OPTIONAL(name, MDStringField, ); \
6578 REQUIRED(type, MDField, ); \
6579 OPTIONAL(defaulted, MDBoolField, );
6581#undef VISIT_MD_FIELDS
6582
6583 Result = GET_OR_DISTINCT(DITemplateTypeParameter,
6584 (Context, name.Val, type.Val, defaulted.Val));
6585 return false;
6586}
6587
6588/// parseDITemplateValueParameter:
6589/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
6590/// name: "V", type: !1, defaulted: false,
6591/// value: i32 7)
6592bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
6593#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6594 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
6595 OPTIONAL(name, MDStringField, ); \
6596 OPTIONAL(type, MDField, ); \
6597 OPTIONAL(defaulted, MDBoolField, ); \
6598 REQUIRED(value, MDField, );
6599
6601#undef VISIT_MD_FIELDS
6602
6604 DITemplateValueParameter,
6605 (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val));
6606 return false;
6607}
6608
6609/// parseDIGlobalVariable:
6610/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
6611/// file: !1, line: 7, type: !2, isLocal: false,
6612/// isDefinition: true, templateParams: !3,
6613/// declaration: !4, align: 8)
6614bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
6615#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6616 OPTIONAL(name, MDStringField, (MDStringField::EmptyIs::Error)); \
6617 OPTIONAL(scope, MDField, ); \
6618 OPTIONAL(linkageName, MDStringField, ); \
6619 OPTIONAL(file, MDField, ); \
6620 OPTIONAL(line, LineField, ); \
6621 OPTIONAL(type, MDField, ); \
6622 OPTIONAL(isLocal, MDBoolField, ); \
6623 OPTIONAL(isDefinition, MDBoolField, (true)); \
6624 OPTIONAL(templateParams, MDField, ); \
6625 OPTIONAL(declaration, MDField, ); \
6626 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6627 OPTIONAL(annotations, MDField, );
6629#undef VISIT_MD_FIELDS
6630
6631 Result =
6632 GET_OR_DISTINCT(DIGlobalVariable,
6633 (Context, scope.Val, name.Val, linkageName.Val, file.Val,
6634 line.Val, type.Val, isLocal.Val, isDefinition.Val,
6635 declaration.Val, templateParams.Val, align.Val,
6636 annotations.Val));
6637 return false;
6638}
6639
6640/// parseDILocalVariable:
6641/// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
6642/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
6643/// align: 8)
6644/// ::= !DILocalVariable(scope: !0, name: "foo",
6645/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
6646/// align: 8)
6647bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) {
6648#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6649 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6650 OPTIONAL(name, MDStringField, ); \
6651 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
6652 OPTIONAL(file, MDField, ); \
6653 OPTIONAL(line, LineField, ); \
6654 OPTIONAL(type, MDField, ); \
6655 OPTIONAL(flags, DIFlagField, ); \
6656 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6657 OPTIONAL(annotations, MDField, );
6659#undef VISIT_MD_FIELDS
6660
6661 Result = GET_OR_DISTINCT(DILocalVariable,
6662 (Context, scope.Val, name.Val, file.Val, line.Val,
6663 type.Val, arg.Val, flags.Val, align.Val,
6664 annotations.Val));
6665 return false;
6666}
6667
6668/// parseDILabel:
6669/// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7, column: 4)
6670bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) {
6671#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6672 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6673 REQUIRED(name, MDStringField, ); \
6674 REQUIRED(file, MDField, ); \
6675 REQUIRED(line, LineField, ); \
6676 OPTIONAL(column, ColumnField, ); \
6677 OPTIONAL(isArtificial, MDBoolField, ); \
6678 OPTIONAL(coroSuspendIdx, MDUnsignedField, );
6680#undef VISIT_MD_FIELDS
6681
6682 std::optional<unsigned> CoroSuspendIdx =
6683 coroSuspendIdx.Seen ? std::optional<unsigned>(coroSuspendIdx.Val)
6684 : std::nullopt;
6685
6686 Result = GET_OR_DISTINCT(DILabel,
6687 (Context, scope.Val, name.Val, file.Val, line.Val,
6688 column.Val, isArtificial.Val, CoroSuspendIdx));
6689 return false;
6690}
6691
6692/// parseDIExpressionBody:
6693/// ::= (0, 7, -1)
6694bool LLParser::parseDIExpressionBody(MDNode *&Result, bool IsDistinct) {
6695 if (parseToken(lltok::lparen, "expected '(' here"))
6696 return true;
6697
6698 SmallVector<uint64_t, 8> Elements;
6699 if (Lex.getKind() != lltok::rparen)
6700 do {
6701 if (Lex.getKind() == lltok::DwarfOp) {
6702 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
6703 Lex.Lex();
6704 Elements.push_back(Op);
6705 continue;
6706 }
6707 return tokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
6708 }
6709
6710 if (Lex.getKind() == lltok::DwarfAttEncoding) {
6711 if (unsigned Op = dwarf::getAttributeEncoding(Lex.getStrVal())) {
6712 Lex.Lex();
6713 Elements.push_back(Op);
6714 continue;
6715 }
6716 return tokError(Twine("invalid DWARF attribute encoding '") +
6717 Lex.getStrVal() + "'");
6718 }
6719
6720 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
6721 return tokError("expected unsigned integer");
6722
6723 auto &U = Lex.getAPSIntVal();
6724 if (U.ugt(UINT64_MAX))
6725 return tokError("element too large, limit is " + Twine(UINT64_MAX));
6726 Elements.push_back(U.getZExtValue());
6727 Lex.Lex();
6728 } while (EatIfPresent(lltok::comma));
6729
6730 if (parseToken(lltok::rparen, "expected ')' here"))
6731 return true;
6732
6733 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
6734 return false;
6735}
6736
6737/// parseDIExpression:
6738/// ::= !DIExpression(0, 7, -1)
6739bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
6740 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
6741 assert(Lex.getStrVal() == "DIExpression" && "Expected '!DIExpression'");
6742 Lex.Lex();
6743
6744 return parseDIExpressionBody(Result, IsDistinct);
6745}
6746
6747/// ParseDIArgList:
6748/// ::= !DIArgList(i32 7, i64 %0)
6749bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) {
6750 assert(PFS && "Expected valid function state");
6751 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
6752 Lex.Lex();
6753
6754 if (parseToken(lltok::lparen, "expected '(' here"))
6755 return true;
6756
6758 if (Lex.getKind() != lltok::rparen)
6759 do {
6760 Metadata *MD;
6761 if (parseValueAsMetadata(MD, "expected value-as-metadata operand", PFS))
6762 return true;
6763 Args.push_back(dyn_cast<ValueAsMetadata>(MD));
6764 } while (EatIfPresent(lltok::comma));
6765
6766 if (parseToken(lltok::rparen, "expected ')' here"))
6767 return true;
6768
6769 MD = DIArgList::get(Context, Args);
6770 return false;
6771}
6772
6773/// parseDIGlobalVariableExpression:
6774/// ::= !DIGlobalVariableExpression(var: !0, expr: !1)
6775bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result,
6776 bool IsDistinct) {
6777#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6778 REQUIRED(var, MDField, ); \
6779 REQUIRED(expr, MDField, );
6781#undef VISIT_MD_FIELDS
6782
6783 Result =
6784 GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
6785 return false;
6786}
6787
6788/// parseDIObjCProperty:
6789/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
6790/// getter: "getFoo", attributes: 7, type: !2)
6791bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
6792#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6793 OPTIONAL(name, MDStringField, ); \
6794 OPTIONAL(file, MDField, ); \
6795 OPTIONAL(line, LineField, ); \
6796 OPTIONAL(setter, MDStringField, ); \
6797 OPTIONAL(getter, MDStringField, ); \
6798 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
6799 OPTIONAL(type, MDField, );
6801#undef VISIT_MD_FIELDS
6802
6803 Result = GET_OR_DISTINCT(DIObjCProperty,
6804 (Context, name.Val, file.Val, line.Val, getter.Val,
6805 setter.Val, attributes.Val, type.Val));
6806 return false;
6807}
6808
6809/// parseDIProperty:
6810/// ::= !DIProperty(name: "x", file: !1, line: 7, type: !2,
6811/// backing_storage: !3)
6812bool LLParser::parseDIProperty(MDNode *&Result, bool IsDistinct) {
6813#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6814 OPTIONAL(name, MDStringField, ); \
6815 OPTIONAL(file, MDField, ); \
6816 OPTIONAL(line, LineField, ); \
6817 OPTIONAL(type, MDField, ); \
6818 OPTIONAL(backing_storage, MDField, );
6820#undef VISIT_MD_FIELDS
6821
6822 Result = GET_OR_DISTINCT(DIProperty, (Context, name.Val, file.Val, line.Val,
6823 type.Val, backing_storage.Val));
6824 return false;
6825}
6826
6827/// parseDIImportedEntity:
6828/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
6829/// line: 7, name: "foo", elements: !2)
6830bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
6831#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6832 REQUIRED(tag, DwarfTagField, ); \
6833 REQUIRED(scope, MDField, ); \
6834 OPTIONAL(entity, MDField, ); \
6835 OPTIONAL(file, MDField, ); \
6836 OPTIONAL(line, LineField, ); \
6837 OPTIONAL(name, MDStringField, ); \
6838 OPTIONAL(elements, MDField, );
6840#undef VISIT_MD_FIELDS
6841
6842 Result = GET_OR_DISTINCT(DIImportedEntity,
6843 (Context, tag.Val, scope.Val, entity.Val, file.Val,
6844 line.Val, name.Val, elements.Val));
6845 return false;
6846}
6847
6848#undef PARSE_MD_FIELD
6849#undef NOP_FIELD
6850#undef REQUIRE_FIELD
6851#undef DECLARE_FIELD
6852
6853/// parseMetadataAsValue
6854/// ::= metadata i32 %local
6855/// ::= metadata i32 @global
6856/// ::= metadata i32 7
6857/// ::= metadata !0
6858/// ::= metadata !{...}
6859/// ::= metadata !"string"
6860bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
6861 // Note: the type 'metadata' has already been parsed.
6862 Metadata *MD;
6863 if (parseMetadata(MD, &PFS))
6864 return true;
6865
6866 V = MetadataAsValue::get(Context, MD);
6867 return false;
6868}
6869
6870/// parseValueAsMetadata
6871/// ::= i32 %local
6872/// ::= i32 @global
6873/// ::= i32 7
6874bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
6875 PerFunctionState *PFS) {
6876 Type *Ty;
6877 LocTy Loc;
6878 if (parseType(Ty, TypeMsg, Loc))
6879 return true;
6880 if (Ty->isMetadataTy())
6881 return error(Loc, "invalid metadata-value-metadata roundtrip");
6882
6883 Value *V;
6884 if (parseValue(Ty, V, PFS))
6885 return true;
6886
6887 MD = ValueAsMetadata::get(V);
6888 return false;
6889}
6890
6891/// parseMetadata
6892/// ::= i32 %local
6893/// ::= i32 @global
6894/// ::= i32 7
6895/// ::= !42
6896/// ::= !{...}
6897/// ::= !"string"
6898/// ::= !DILocation(...)
6899bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) {
6900 if (Lex.getKind() == lltok::MetadataVar) {
6901 // DIArgLists are a special case, as they are a list of ValueAsMetadata and
6902 // so parsing this requires a Function State.
6903 if (Lex.getStrVal() == "DIArgList") {
6904 Metadata *AL;
6905 if (parseDIArgList(AL, PFS))
6906 return true;
6907 MD = AL;
6908 return false;
6909 }
6910 MDNode *N;
6911 if (parseSpecializedMDNode(N)) {
6912 return true;
6913 }
6914 MD = N;
6915 return false;
6916 }
6917
6918 // ValueAsMetadata:
6919 // <type> <value>
6920 if (Lex.getKind() != lltok::exclaim)
6921 return parseValueAsMetadata(MD, "expected metadata operand", PFS);
6922
6923 // '!'.
6924 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
6925 Lex.Lex();
6926
6927 // MDString:
6928 // ::= '!' STRINGCONSTANT
6929 if (Lex.getKind() == lltok::StringConstant) {
6930 MDString *S;
6931 if (parseMDString(S))
6932 return true;
6933 MD = S;
6934 return false;
6935 }
6936
6937 // MDNode:
6938 // !{ ... }
6939 // !7
6940 MDNode *N;
6941 if (parseMDNodeTail(N))
6942 return true;
6943 MD = N;
6944 return false;
6945}
6946
6947//===----------------------------------------------------------------------===//
6948// Function Parsing.
6949//===----------------------------------------------------------------------===//
6950
6951bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
6952 PerFunctionState *PFS) {
6953 if (Ty->isFunctionTy())
6954 return error(ID.Loc, "functions are not values, refer to them as pointers");
6955
6956 switch (ID.Kind) {
6957 case ValID::t_LocalID:
6958 if (!PFS)
6959 return error(ID.Loc, "invalid use of function-local name");
6960 V = PFS->getVal(ID.UIntVal, Ty, ID.Loc);
6961 return V == nullptr;
6962 case ValID::t_LocalName:
6963 if (!PFS)
6964 return error(ID.Loc, "invalid use of function-local name");
6965 V = PFS->getVal(ID.StrVal, Ty, ID.Loc);
6966 return V == nullptr;
6967 case ValID::t_InlineAsm: {
6968 if (!ID.FTy)
6969 return error(ID.Loc, "invalid type for inline asm constraint string");
6970 if (Error Err = InlineAsm::verify(ID.FTy, ID.StrVal2))
6971 return error(ID.Loc, toString(std::move(Err)));
6972 V = InlineAsm::get(
6973 ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1, (ID.UIntVal >> 1) & 1,
6974 InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), (ID.UIntVal >> 3) & 1);
6975 return false;
6976 }
6978 V = getGlobalVal(ID.StrVal, Ty, ID.Loc);
6979 if (V && ID.NoCFI)
6981 return V == nullptr;
6982 case ValID::t_GlobalID:
6983 V = getGlobalVal(ID.UIntVal, Ty, ID.Loc);
6984 if (V && ID.NoCFI)
6986 return V == nullptr;
6987 case ValID::t_APSInt:
6988 if (!Ty->isIntegerTy() && !Ty->isByteTy())
6989 return error(ID.Loc, "integer/byte constant must have integer/byte type");
6990 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
6991 Ty->isIntegerTy() ? V = ConstantInt::get(Context, ID.APSIntVal)
6992 : V = ConstantByte::get(Context, ID.APSIntVal);
6993 return false;
6994 case ValID::t_APFloat:
6995 if (!Ty->isFloatingPointTy() ||
6996 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
6997 return error(ID.Loc, "floating point constant invalid for type");
6998
6999 // The lexer has no type info, so builds all half, bfloat, float, and double
7000 // FP constants as double. Fix this here. Long double does not need this.
7001 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
7002 // Check for signaling before potentially converting and losing that info.
7003 bool IsSNAN = ID.APFloatVal.isSignaling();
7004 bool Ignored;
7005 if (Ty->isHalfTy())
7006 ID.APFloatVal.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
7007 &Ignored);
7008 else if (Ty->isBFloatTy())
7009 ID.APFloatVal.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven,
7010 &Ignored);
7011 else if (Ty->isFloatTy())
7012 ID.APFloatVal.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
7013 &Ignored);
7014 if (IsSNAN) {
7015 // The convert call above may quiet an SNaN, so manufacture another
7016 // SNaN. The bitcast works because the payload (significand) parameter
7017 // is truncated to fit.
7018 APInt Payload = ID.APFloatVal.bitcastToAPInt();
7019 ID.APFloatVal = APFloat::getSNaN(ID.APFloatVal.getSemantics(),
7020 ID.APFloatVal.isNegative(), &Payload);
7021 }
7022 }
7023 V = ConstantFP::get(Context, ID.APFloatVal);
7024
7025 if (V->getType() != Ty)
7026 return error(ID.Loc, "floating point constant does not have type '" +
7027 getTypeString(Ty) + "'");
7028
7029 return false;
7030 case ValID::t_Null:
7031 if (!Ty->isPointerTy())
7032 return error(ID.Loc, "null must be a pointer type");
7034 return false;
7035 case ValID::t_Undef:
7036 // FIXME: LabelTy should not be a first-class type.
7037 if (!Ty->isFirstClassType() || Ty->isLabelTy())
7038 return error(ID.Loc, "invalid type for undef constant");
7039 V = UndefValue::get(Ty);
7040 return false;
7042 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
7043 return error(ID.Loc, "invalid empty array initializer");
7044 V = PoisonValue::get(Ty);
7045 return false;
7046 case ValID::t_Zero:
7047 // FIXME: LabelTy should not be a first-class type.
7048 if (!Ty->isFirstClassType() || Ty->isLabelTy())
7049 return error(ID.Loc, "invalid type for null constant");
7050 if (auto *TETy = dyn_cast<TargetExtType>(Ty))
7051 if (!TETy->hasProperty(TargetExtType::HasZeroInit))
7052 return error(ID.Loc, "invalid type for null constant");
7054 return false;
7055 case ValID::t_None:
7056 if (!Ty->isTokenTy())
7057 return error(ID.Loc, "invalid type for none constant");
7059 return false;
7060 case ValID::t_Poison:
7061 // FIXME: LabelTy should not be a first-class type.
7062 if (!Ty->isFirstClassType() || Ty->isLabelTy())
7063 return error(ID.Loc, "invalid type for poison constant");
7064 V = PoisonValue::get(Ty);
7065 return false;
7066 case ValID::t_Constant:
7067 if (ID.ConstantVal->getType() != Ty)
7068 return error(ID.Loc, "constant expression type mismatch: got type '" +
7069 getTypeString(ID.ConstantVal->getType()) +
7070 "' but expected '" + getTypeString(Ty) + "'");
7071 V = ID.ConstantVal;
7072 return false;
7074 if (!Ty->isVectorTy())
7075 return error(ID.Loc, "vector constant must have vector type");
7076 if (ID.ConstantVal->getType() != Ty->getScalarType())
7077 return error(ID.Loc, "constant expression type mismatch: got type '" +
7078 getTypeString(ID.ConstantVal->getType()) +
7079 "' but expected '" +
7080 getTypeString(Ty->getScalarType()) + "'");
7081 V = ConstantVector::getSplat(cast<VectorType>(Ty)->getElementCount(),
7082 ID.ConstantVal);
7083 return false;
7086 if (StructType *ST = dyn_cast<StructType>(Ty)) {
7087 if (ST->getNumElements() != ID.UIntVal)
7088 return error(ID.Loc,
7089 "initializer with struct type has wrong # elements");
7090 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
7091 return error(ID.Loc, "packed'ness of initializer and type don't match");
7092
7093 // Verify that the elements are compatible with the structtype.
7094 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
7095 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
7096 return error(
7097 ID.Loc,
7098 "element " + Twine(i) +
7099 " of struct initializer doesn't match struct element type");
7100
7102 ST, ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
7103 } else
7104 return error(ID.Loc, "constant expression type mismatch");
7105 return false;
7106 }
7107 llvm_unreachable("Invalid ValID");
7108}
7109
7110bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
7111 C = nullptr;
7112 ValID ID;
7113 auto Loc = Lex.getLoc();
7114 if (parseValID(ID, /*PFS=*/nullptr, /*ExpectedTy=*/Ty))
7115 return true;
7116 switch (ID.Kind) {
7117 case ValID::t_APSInt:
7118 case ValID::t_APFloat:
7119 case ValID::t_Undef:
7120 case ValID::t_Poison:
7121 case ValID::t_Zero:
7122 case ValID::t_Constant:
7126 Value *V;
7127 if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr))
7128 return true;
7129 assert(isa<Constant>(V) && "Expected a constant value");
7130 C = cast<Constant>(V);
7131 return false;
7132 }
7133 case ValID::t_Null:
7135 return false;
7136 default:
7137 return error(Loc, "expected a constant value");
7138 }
7139}
7140
7141bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
7142 V = nullptr;
7143 ValID ID;
7144
7145 FileLoc Start = getTokLineColumnPos();
7146 bool Ret = parseValID(ID, PFS, Ty) || convertValIDToValue(Ty, ID, V, PFS);
7147 if (!Ret && ParserContext) {
7148 FileLoc End = getPrevTokEndLineColumnPos();
7149 ParserContext->addValueReferenceAtLocation(V, FileLocRange(Start, End));
7150 }
7151 return Ret;
7152}
7153
7154bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) {
7155 Type *Ty = nullptr;
7156 return parseType(Ty) || parseValue(Ty, V, PFS);
7157}
7158
7159bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
7160 PerFunctionState &PFS) {
7161 Value *V;
7162 Loc = Lex.getLoc();
7163 if (parseTypeAndValue(V, PFS))
7164 return true;
7165 if (!isa<BasicBlock>(V))
7166 return error(Loc, "expected a basic block");
7167 BB = cast<BasicBlock>(V);
7168 return false;
7169}
7170
7172 // Exit early for the common (non-debug-intrinsic) case.
7173 // We can make this the only check when we begin supporting all "llvm.dbg"
7174 // intrinsics in the new debug info format.
7175 if (!Name.starts_with("llvm.dbg."))
7176 return false;
7178 return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value ||
7179 FnID == Intrinsic::dbg_assign;
7180}
7181
7182/// FunctionHeader
7183/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
7184/// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
7185/// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
7186/// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
7187bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
7188 unsigned &FunctionNumber,
7189 SmallVectorImpl<unsigned> &UnnamedArgNums) {
7190 // parse the linkage.
7191 LocTy LinkageLoc = Lex.getLoc();
7192 unsigned Linkage;
7193 unsigned Visibility;
7194 unsigned DLLStorageClass;
7195 bool DSOLocal;
7196 AttrBuilder RetAttrs(M->getContext());
7197 unsigned CC;
7198 bool HasLinkage;
7199 Type *RetType = nullptr;
7200 LocTy RetTypeLoc = Lex.getLoc();
7201 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
7202 DSOLocal) ||
7203 parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
7204 parseType(RetType, RetTypeLoc, true /*void allowed*/))
7205 return true;
7206
7207 // Verify that the linkage is ok.
7210 break; // always ok.
7212 if (IsDefine)
7213 return error(LinkageLoc, "invalid linkage for function definition");
7214 break;
7222 if (!IsDefine)
7223 return error(LinkageLoc, "invalid linkage for function declaration");
7224 break;
7227 return error(LinkageLoc, "invalid function linkage type");
7228 }
7229
7230 if (!isValidVisibilityForLinkage(Visibility, Linkage))
7231 return error(LinkageLoc,
7232 "symbol with local linkage must have default visibility");
7233
7234 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
7235 return error(LinkageLoc,
7236 "symbol with local linkage cannot have a DLL storage class");
7237
7238 if (!FunctionType::isValidReturnType(RetType))
7239 return error(RetTypeLoc, "invalid function return type");
7240
7241 LocTy NameLoc = Lex.getLoc();
7242
7243 std::string FunctionName;
7244 if (Lex.getKind() == lltok::GlobalVar) {
7245 FunctionName = Lex.getStrVal();
7246 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
7247 FunctionNumber = Lex.getUIntVal();
7248 if (checkValueID(NameLoc, "function", "@", NumberedVals.getNext(),
7249 FunctionNumber))
7250 return true;
7251 } else {
7252 return tokError("expected function name");
7253 }
7254
7255 Lex.Lex();
7256
7257 if (Lex.getKind() != lltok::lparen)
7258 return tokError("expected '(' in function argument list");
7259
7261 bool IsVarArg;
7262 AttrBuilder FuncAttrs(M->getContext());
7263 std::vector<unsigned> FwdRefAttrGrps;
7264 LocTy BuiltinLoc;
7265 std::string Section;
7266 std::string Partition;
7267 MaybeAlign Alignment, PrefAlignment;
7268 std::string GC;
7270 unsigned AddrSpace = 0;
7271 Constant *Prefix = nullptr;
7272 Constant *Prologue = nullptr;
7273 Constant *PersonalityFn = nullptr;
7274 Comdat *C;
7275
7276 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg) ||
7277 parseOptionalUnnamedAddr(UnnamedAddr) ||
7278 parseOptionalProgramAddrSpace(AddrSpace) ||
7279 parseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
7280 BuiltinLoc) ||
7281 (EatIfPresent(lltok::kw_section) && parseStringConstant(Section)) ||
7282 (EatIfPresent(lltok::kw_partition) && parseStringConstant(Partition)) ||
7283 parseOptionalComdat(FunctionName, C) ||
7284 parseOptionalAlignment(Alignment) ||
7285 parseOptionalPrefAlignment(PrefAlignment) ||
7286 (EatIfPresent(lltok::kw_gc) && parseStringConstant(GC)) ||
7287 (EatIfPresent(lltok::kw_prefix) && parseGlobalTypeAndValue(Prefix)) ||
7288 (EatIfPresent(lltok::kw_prologue) && parseGlobalTypeAndValue(Prologue)) ||
7289 (EatIfPresent(lltok::kw_personality) &&
7290 parseGlobalTypeAndValue(PersonalityFn)))
7291 return true;
7292
7293 if (FuncAttrs.contains(Attribute::Builtin))
7294 return error(BuiltinLoc, "'builtin' attribute not valid on function");
7295
7296 // If the alignment was parsed as an attribute, move to the alignment field.
7297 if (MaybeAlign A = FuncAttrs.getAlignment()) {
7298 Alignment = A;
7299 FuncAttrs.removeAttribute(Attribute::Alignment);
7300 }
7301
7302 // Okay, if we got here, the function is syntactically valid. Convert types
7303 // and do semantic checks.
7304 std::vector<Type*> ParamTypeList;
7306
7307 for (const ArgInfo &Arg : ArgList) {
7308 ParamTypeList.push_back(Arg.Ty);
7309 Attrs.push_back(Arg.Attrs);
7310 }
7311
7312 AttributeList PAL =
7313 AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
7314 AttributeSet::get(Context, RetAttrs), Attrs);
7315
7316 if (PAL.hasParamAttr(0, Attribute::StructRet) && !RetType->isVoidTy())
7317 return error(RetTypeLoc, "functions with 'sret' argument must return void");
7318
7319 FunctionType *FT = FunctionType::get(RetType, ParamTypeList, IsVarArg);
7320 PointerType *PFT = PointerType::get(Context, AddrSpace);
7321
7322 Fn = nullptr;
7323 GlobalValue *FwdFn = nullptr;
7324 if (!FunctionName.empty()) {
7325 // If this was a definition of a forward reference, remove the definition
7326 // from the forward reference table and fill in the forward ref.
7327 auto FRVI = ForwardRefVals.find(FunctionName);
7328 if (FRVI != ForwardRefVals.end()) {
7329 FwdFn = FRVI->second.first;
7330 if (FwdFn->getType() != PFT)
7331 return error(FRVI->second.second,
7332 "invalid forward reference to "
7333 "function '" +
7334 FunctionName +
7335 "' with wrong type: "
7336 "expected '" +
7337 getTypeString(PFT) + "' but was '" +
7338 getTypeString(FwdFn->getType()) + "'");
7339 ForwardRefVals.erase(FRVI);
7340 } else if ((Fn = M->getFunction(FunctionName))) {
7341 // Reject redefinitions.
7342 return error(NameLoc,
7343 "invalid redefinition of function '" + FunctionName + "'");
7344 } else if (M->getNamedValue(FunctionName)) {
7345 return error(NameLoc, "redefinition of function '@" + FunctionName + "'");
7346 }
7347
7348 } else {
7349 // Handle @"", where a name is syntactically specified, but semantically
7350 // missing.
7351 if (FunctionNumber == (unsigned)-1)
7352 FunctionNumber = NumberedVals.getNext();
7353
7354 // If this is a definition of a forward referenced function, make sure the
7355 // types agree.
7356 auto I = ForwardRefValIDs.find(FunctionNumber);
7357 if (I != ForwardRefValIDs.end()) {
7358 FwdFn = I->second.first;
7359 if (FwdFn->getType() != PFT)
7360 return error(NameLoc, "type of definition and forward reference of '@" +
7361 Twine(FunctionNumber) +
7362 "' disagree: "
7363 "expected '" +
7364 getTypeString(PFT) + "' but was '" +
7365 getTypeString(FwdFn->getType()) + "'");
7366 ForwardRefValIDs.erase(I);
7367 }
7368 }
7369
7371 FunctionName, M);
7372
7373 assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
7374
7375 if (FunctionName.empty())
7376 NumberedVals.add(FunctionNumber, Fn);
7377
7379 maybeSetDSOLocal(DSOLocal, *Fn);
7382 Fn->setCallingConv(CC);
7383 Fn->setAttributes(PAL);
7384 Fn->setUnnamedAddr(UnnamedAddr);
7385 if (Alignment)
7386 Fn->setAlignment(*Alignment);
7387 Fn->setPreferredAlignment(PrefAlignment);
7388 Fn->setSection(Section);
7389 Fn->setPartition(Partition);
7390 Fn->setComdat(C);
7391 Fn->setPersonalityFn(PersonalityFn);
7392 if (!GC.empty()) Fn->setGC(GC);
7393 Fn->setPrefixData(Prefix);
7394 Fn->setPrologueData(Prologue);
7395 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
7396
7397 // Add all of the arguments we parsed to the function.
7398 Function::arg_iterator ArgIt = Fn->arg_begin();
7399 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
7400 if (ParserContext && ArgList[i].IdentLoc)
7401 ParserContext->addInstructionOrArgumentLocation(
7402 &*ArgIt, ArgList[i].IdentLoc.value());
7403 // If the argument has a name, insert it into the argument symbol table.
7404 if (ArgList[i].Name.empty()) continue;
7405
7406 // Set the name, if it conflicted, it will be auto-renamed.
7407 ArgIt->setName(ArgList[i].Name);
7408
7409 if (ArgIt->getName() != ArgList[i].Name)
7410 return error(ArgList[i].Loc,
7411 "redefinition of argument '%" + ArgList[i].Name + "'");
7412 }
7413
7414 if (FwdFn) {
7415 FwdFn->replaceAllUsesWith(Fn);
7416 FwdFn->eraseFromParent();
7417 }
7418
7419 if (IsDefine)
7420 return false;
7421
7422 // Check the declaration has no block address forward references.
7423 ValID ID;
7424 if (FunctionName.empty()) {
7425 ID.Kind = ValID::t_GlobalID;
7426 ID.UIntVal = FunctionNumber;
7427 } else {
7428 ID.Kind = ValID::t_GlobalName;
7429 ID.StrVal = FunctionName;
7430 }
7431 auto Blocks = ForwardRefBlockAddresses.find(ID);
7432 if (Blocks != ForwardRefBlockAddresses.end())
7433 return error(Blocks->first.Loc,
7434 "cannot take blockaddress inside a declaration");
7435 return false;
7436}
7437
7438bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
7439 ValID ID;
7440 if (FunctionNumber == -1) {
7441 ID.Kind = ValID::t_GlobalName;
7442 ID.StrVal = std::string(F.getName());
7443 } else {
7444 ID.Kind = ValID::t_GlobalID;
7445 ID.UIntVal = FunctionNumber;
7446 }
7447
7448 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
7449 if (Blocks == P.ForwardRefBlockAddresses.end())
7450 return false;
7451
7452 for (const auto &I : Blocks->second) {
7453 const ValID &BBID = I.first;
7454 GlobalValue *GV = I.second;
7455
7456 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
7457 "Expected local id or name");
7458 BasicBlock *BB;
7459 if (BBID.Kind == ValID::t_LocalName)
7460 BB = getBB(BBID.StrVal, BBID.Loc);
7461 else
7462 BB = getBB(BBID.UIntVal, BBID.Loc);
7463 if (!BB)
7464 return P.error(BBID.Loc, "referenced value is not a basic block");
7465
7466 Value *ResolvedVal = BlockAddress::get(&F, BB);
7467 ResolvedVal = P.checkValidVariableType(BBID.Loc, BBID.StrVal, GV->getType(),
7468 ResolvedVal);
7469 if (!ResolvedVal)
7470 return true;
7471 GV->replaceAllUsesWith(ResolvedVal);
7472 GV->eraseFromParent();
7473 }
7474
7475 P.ForwardRefBlockAddresses.erase(Blocks);
7476 return false;
7477}
7478
7479/// parseFunctionBody
7480/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
7481bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber,
7482 ArrayRef<unsigned> UnnamedArgNums) {
7483 if (Lex.getKind() != lltok::lbrace)
7484 return tokError("expected '{' in function body");
7485 Lex.Lex(); // eat the {.
7486
7487 PerFunctionState PFS(*this, Fn, FunctionNumber, UnnamedArgNums);
7488
7489 // Resolve block addresses and allow basic blocks to be forward-declared
7490 // within this function.
7491 if (PFS.resolveForwardRefBlockAddresses())
7492 return true;
7493 SaveAndRestore ScopeExit(BlockAddressPFS, &PFS);
7494
7495 // We need at least one basic block.
7496 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
7497 return tokError("function body requires at least one basic block");
7498
7499 while (Lex.getKind() != lltok::rbrace &&
7500 Lex.getKind() != lltok::kw_uselistorder)
7501 if (parseBasicBlock(PFS))
7502 return true;
7503
7504 while (Lex.getKind() != lltok::rbrace)
7505 if (parseUseListOrder(&PFS))
7506 return true;
7507
7508 // Eat the }.
7509 Lex.Lex();
7510
7511 // Verify function is ok.
7512 return PFS.finishFunction();
7513}
7514
7515/// parseBasicBlock
7516/// ::= (LabelStr|LabelID)? Instruction*
7517bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
7518 FileLoc BBStart = getTokLineColumnPos();
7519
7520 // If this basic block starts out with a name, remember it.
7521 std::string Name;
7522 int NameID = -1;
7523 LocTy NameLoc = Lex.getLoc();
7524 if (Lex.getKind() == lltok::LabelStr) {
7525 Name = Lex.getStrVal();
7526 Lex.Lex();
7527 } else if (Lex.getKind() == lltok::LabelID) {
7528 NameID = Lex.getUIntVal();
7529 Lex.Lex();
7530 }
7531
7532 BasicBlock *BB = PFS.defineBB(Name, NameID, NameLoc);
7533 if (!BB)
7534 return true;
7535
7536 std::string NameStr;
7537
7538 // Parse the instructions and debug values in this block until we get a
7539 // terminator.
7540 Instruction *Inst;
7541 auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord(); };
7542 using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>;
7543 SmallVector<DbgRecordPtr> TrailingDbgRecord;
7544 do {
7545 // Handle debug records first - there should always be an instruction
7546 // following the debug records, i.e. they cannot appear after the block
7547 // terminator.
7548 while (Lex.getKind() == lltok::hash) {
7549 if (SeenOldDbgInfoFormat)
7550 return error(Lex.getLoc(), "debug record should not appear in a module "
7551 "containing debug info intrinsics");
7552 SeenNewDbgInfoFormat = true;
7553 Lex.Lex();
7554
7555 DbgRecord *DR;
7556 if (parseDebugRecord(DR, PFS))
7557 return true;
7558 TrailingDbgRecord.emplace_back(DR, DeleteDbgRecord);
7559 }
7560
7561 FileLoc InstStart = getTokLineColumnPos();
7562 // This instruction may have three possibilities for a name: a) none
7563 // specified, b) name specified "%foo =", c) number specified: "%4 =".
7564 LocTy NameLoc = Lex.getLoc();
7565 int NameID = -1;
7566 NameStr = "";
7567
7568 if (Lex.getKind() == lltok::LocalVarID) {
7569 NameID = Lex.getUIntVal();
7570 Lex.Lex();
7571 if (parseToken(lltok::equal, "expected '=' after instruction id"))
7572 return true;
7573 } else if (Lex.getKind() == lltok::LocalVar) {
7574 NameStr = Lex.getStrVal();
7575 Lex.Lex();
7576 if (parseToken(lltok::equal, "expected '=' after instruction name"))
7577 return true;
7578 }
7579
7580 switch (parseInstruction(Inst, BB, PFS)) {
7581 default:
7582 llvm_unreachable("Unknown parseInstruction result!");
7583 case InstError: return true;
7584 case InstNormal:
7585 Inst->insertInto(BB, BB->end());
7586
7587 // With a normal result, we check to see if the instruction is followed by
7588 // a comma and metadata.
7589 if (EatIfPresent(lltok::comma))
7590 if (parseInstructionMetadata(*Inst))
7591 return true;
7592 break;
7593 case InstExtraComma:
7594 Inst->insertInto(BB, BB->end());
7595
7596 // If the instruction parser ate an extra comma at the end of it, it
7597 // *must* be followed by metadata.
7598 if (parseInstructionMetadata(*Inst))
7599 return true;
7600 break;
7601 }
7602
7603 // Set the name on the instruction.
7604 if (PFS.setInstName(NameID, NameStr, NameLoc, Inst))
7605 return true;
7606
7607 // Attach any preceding debug values to this instruction.
7608 for (DbgRecordPtr &DR : TrailingDbgRecord)
7609 BB->insertDbgRecordBefore(DR.release(), Inst->getIterator());
7610 TrailingDbgRecord.clear();
7611 if (ParserContext) {
7612 ParserContext->addInstructionOrArgumentLocation(
7613 Inst, FileLocRange(InstStart, getPrevTokEndLineColumnPos()));
7614 }
7615 } while (!Inst->isTerminator());
7616
7617 if (ParserContext)
7618 ParserContext->addBlockLocation(
7619 BB, FileLocRange(BBStart, getPrevTokEndLineColumnPos()));
7620
7621 assert(TrailingDbgRecord.empty() &&
7622 "All debug values should have been attached to an instruction.");
7623
7624 return false;
7625}
7626
7627/// parseDebugRecord
7628/// ::= #dbg_label '(' MDNode ')'
7629/// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ','
7630/// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')'
7631bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
7632 using RecordKind = DbgRecord::Kind;
7633 using LocType = DbgVariableRecord::LocationType;
7634 LocTy DVRLoc = Lex.getLoc();
7635 if (Lex.getKind() != lltok::DbgRecordType)
7636 return error(DVRLoc, "expected debug record type here");
7637 RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal())
7638 .Case("declare", RecordKind::ValueKind)
7639 .Case("value", RecordKind::ValueKind)
7640 .Case("assign", RecordKind::ValueKind)
7641 .Case("label", RecordKind::LabelKind)
7642 .Case("declare_value", RecordKind::ValueKind);
7643
7644 // Parsing labels is trivial; parse here and early exit, otherwise go into the
7645 // full DbgVariableRecord processing stage.
7646 if (RecordType == RecordKind::LabelKind) {
7647 Lex.Lex();
7648 if (parseToken(lltok::lparen, "Expected '(' here"))
7649 return true;
7650 MDNode *Label;
7651 if (parseMDNode(Label))
7652 return true;
7653 if (parseToken(lltok::comma, "Expected ',' here"))
7654 return true;
7655 MDNode *DbgLoc;
7656 if (parseMDNode(DbgLoc))
7657 return true;
7658 if (parseToken(lltok::rparen, "Expected ')' here"))
7659 return true;
7661 PendingDbgRecords.emplace_back(DVRLoc, DR, DbgLoc);
7662 return false;
7663 }
7664
7665 LocType ValueType = StringSwitch<LocType>(Lex.getStrVal())
7666 .Case("declare", LocType::Declare)
7667 .Case("value", LocType::Value)
7668 .Case("assign", LocType::Assign)
7669 .Case("declare_value", LocType::DeclareValue);
7670
7671 Lex.Lex();
7672 if (parseToken(lltok::lparen, "Expected '(' here"))
7673 return true;
7674
7675 // Parse Value field.
7676 Metadata *ValLocMD;
7677 if (parseMetadata(ValLocMD, &PFS))
7678 return true;
7679 if (parseToken(lltok::comma, "Expected ',' here"))
7680 return true;
7681
7682 // Parse Variable field.
7683 MDNode *Variable;
7684 if (parseMDNode(Variable))
7685 return true;
7686 if (parseToken(lltok::comma, "Expected ',' here"))
7687 return true;
7688
7689 // Parse Expression field.
7690 MDNode *Expression;
7691 if (parseMDNode(Expression))
7692 return true;
7693 if (parseToken(lltok::comma, "Expected ',' here"))
7694 return true;
7695
7696 // Parse additional fields for #dbg_assign.
7697 MDNode *AssignID = nullptr;
7698 Metadata *AddressLocation = nullptr;
7699 MDNode *AddressExpression = nullptr;
7700 if (ValueType == LocType::Assign) {
7701 // Parse DIAssignID.
7702 if (parseMDNode(AssignID))
7703 return true;
7704 if (parseToken(lltok::comma, "Expected ',' here"))
7705 return true;
7706
7707 // Parse address ValueAsMetadata.
7708 if (parseMetadata(AddressLocation, &PFS))
7709 return true;
7710 if (parseToken(lltok::comma, "Expected ',' here"))
7711 return true;
7712
7713 // Parse address DIExpression.
7714 if (parseMDNode(AddressExpression))
7715 return true;
7716 if (parseToken(lltok::comma, "Expected ',' here"))
7717 return true;
7718 }
7719
7720 /// Parse DILocation.
7721 MDNode *DebugLoc;
7722 if (parseMDNode(DebugLoc))
7723 return true;
7724
7725 if (parseToken(lltok::rparen, "Expected ')' here"))
7726 return true;
7728 ValueType, ValLocMD, Variable, Expression, AssignID, AddressLocation,
7729 AddressExpression);
7730 PendingDbgRecords.emplace_back(DVRLoc, DR, DebugLoc);
7731 return false;
7732}
7733//===----------------------------------------------------------------------===//
7734// Instruction Parsing.
7735//===----------------------------------------------------------------------===//
7736
7737/// parseInstruction - parse one of the many different instructions.
7738///
7739int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
7740 PerFunctionState &PFS) {
7741 lltok::Kind Token = Lex.getKind();
7742 if (Token == lltok::Eof)
7743 return tokError("found end of file when expecting more instructions");
7744 LocTy Loc = Lex.getLoc();
7745 unsigned KeywordVal = Lex.getUIntVal();
7746 Lex.Lex(); // Eat the keyword.
7747
7748 switch (Token) {
7749 default:
7750 return error(Loc, "expected instruction opcode");
7751 // Terminator Instructions.
7752 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
7753 case lltok::kw_ret:
7754 return parseRet(Inst, BB, PFS);
7755 case lltok::kw_br:
7756 return parseBr(Inst, PFS);
7757 case lltok::kw_switch:
7758 return parseSwitch(Inst, PFS);
7760 return parseIndirectBr(Inst, PFS);
7761 case lltok::kw_invoke:
7762 return parseInvoke(Inst, PFS);
7763 case lltok::kw_resume:
7764 return parseResume(Inst, PFS);
7766 return parseCleanupRet(Inst, PFS);
7767 case lltok::kw_catchret:
7768 return parseCatchRet(Inst, PFS);
7770 return parseCatchSwitch(Inst, PFS);
7771 case lltok::kw_catchpad:
7772 return parseCatchPad(Inst, PFS);
7774 return parseCleanupPad(Inst, PFS);
7775 case lltok::kw_callbr:
7776 return parseCallBr(Inst, PFS);
7777 // Unary Operators.
7778 case lltok::kw_fneg: {
7779 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7780 int Res = parseUnaryOp(Inst, PFS, KeywordVal, /*IsFP*/ true);
7781 if (Res != 0)
7782 return Res;
7783 if (FMF.any())
7784 Inst->setFastMathFlags(FMF);
7785 return false;
7786 }
7787 // Binary Operators.
7788 case lltok::kw_add:
7789 case lltok::kw_sub:
7790 case lltok::kw_mul:
7791 case lltok::kw_shl: {
7792 bool NUW = EatIfPresent(lltok::kw_nuw);
7793 bool NSW = EatIfPresent(lltok::kw_nsw);
7794 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
7795
7796 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
7797 return true;
7798
7799 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
7800 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
7801 return false;
7802 }
7803 case lltok::kw_fadd:
7804 case lltok::kw_fsub:
7805 case lltok::kw_fmul:
7806 case lltok::kw_fdiv:
7807 case lltok::kw_frem: {
7808 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7809 int Res = parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ true);
7810 if (Res != 0)
7811 return Res;
7812 if (FMF.any())
7813 Inst->setFastMathFlags(FMF);
7814 return 0;
7815 }
7816
7817 case lltok::kw_sdiv:
7818 case lltok::kw_udiv:
7819 case lltok::kw_lshr:
7820 case lltok::kw_ashr: {
7821 bool Exact = EatIfPresent(lltok::kw_exact);
7822
7823 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
7824 return true;
7825 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
7826 return false;
7827 }
7828
7829 case lltok::kw_urem:
7830 case lltok::kw_srem:
7831 return parseArithmetic(Inst, PFS, KeywordVal,
7832 /*IsFP*/ false);
7833 case lltok::kw_or: {
7834 bool Disjoint = EatIfPresent(lltok::kw_disjoint);
7835 if (parseLogical(Inst, PFS, KeywordVal))
7836 return true;
7837 if (Disjoint)
7838 cast<PossiblyDisjointInst>(Inst)->setIsDisjoint(true);
7839 return false;
7840 }
7841 case lltok::kw_and:
7842 case lltok::kw_xor:
7843 return parseLogical(Inst, PFS, KeywordVal);
7844 case lltok::kw_icmp: {
7845 bool SameSign = EatIfPresent(lltok::kw_samesign);
7846 if (parseCompare(Inst, PFS, KeywordVal))
7847 return true;
7848 if (SameSign)
7849 cast<ICmpInst>(Inst)->setSameSign();
7850 return false;
7851 }
7852 case lltok::kw_fcmp: {
7853 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7854 int Res = parseCompare(Inst, PFS, KeywordVal);
7855 if (Res != 0)
7856 return Res;
7857 if (FMF.any())
7858 Inst->setFastMathFlags(FMF);
7859 return 0;
7860 }
7861
7862 // Casts.
7863 case lltok::kw_uitofp: {
7864 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7865 bool NonNeg = EatIfPresent(lltok::kw_nneg);
7866 bool Res = parseCast(Inst, PFS, KeywordVal);
7867 if (Res != 0)
7868 return Res;
7869 if (NonNeg)
7870 Inst->setNonNeg();
7871 Inst->setFastMathFlags(FMF);
7872 return 0;
7873 }
7874 case lltok::kw_zext: {
7875 bool NonNeg = EatIfPresent(lltok::kw_nneg);
7876 bool Res = parseCast(Inst, PFS, KeywordVal);
7877 if (Res != 0)
7878 return Res;
7879 if (NonNeg)
7880 Inst->setNonNeg();
7881 return 0;
7882 }
7883 case lltok::kw_trunc: {
7884 bool NUW = EatIfPresent(lltok::kw_nuw);
7885 bool NSW = EatIfPresent(lltok::kw_nsw);
7886 if (!NUW)
7887 NUW = EatIfPresent(lltok::kw_nuw);
7888 if (parseCast(Inst, PFS, KeywordVal))
7889 return true;
7890 if (NUW)
7891 cast<TruncInst>(Inst)->setHasNoUnsignedWrap(true);
7892 if (NSW)
7893 cast<TruncInst>(Inst)->setHasNoSignedWrap(true);
7894 return false;
7895 }
7897 bool NonNull = EatIfPresent(lltok::kw_nonnull);
7898 if (parseCast(Inst, PFS, KeywordVal))
7899 return true;
7900 if (NonNull)
7901 cast<AddrSpaceCastInst>(Inst)->setNonNull();
7902 return false;
7903 }
7904 case lltok::kw_sext:
7905 case lltok::kw_bitcast:
7906 case lltok::kw_fptoui:
7907 case lltok::kw_fptosi:
7908 case lltok::kw_inttoptr:
7910 case lltok::kw_ptrtoint:
7911 return parseCast(Inst, PFS, KeywordVal);
7912 case lltok::kw_fptrunc:
7913 case lltok::kw_fpext:
7914 case lltok::kw_sitofp: {
7915 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7916 if (parseCast(Inst, PFS, KeywordVal))
7917 return true;
7918 if (FMF.any())
7919 Inst->setFastMathFlags(FMF);
7920 return false;
7921 }
7922
7923 // Other.
7924 case lltok::kw_select: {
7925 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7926 int Res = parseSelect(Inst, PFS);
7927 if (Res != 0)
7928 return Res;
7929 if (FMF.any()) {
7930 if (!isa<FPMathOperator>(Inst)) {
7931 Inst->deleteValue();
7932 return error(Loc, "fast-math-flags specified for select without "
7933 "floating-point scalar or vector return type");
7934 }
7935 Inst->setFastMathFlags(FMF);
7936 }
7937 return 0;
7938 }
7939 case lltok::kw_va_arg:
7940 return parseVAArg(Inst, PFS);
7942 return parseExtractElement(Inst, PFS);
7944 return parseInsertElement(Inst, PFS);
7946 return parseShuffleVector(Inst, PFS);
7947 case lltok::kw_phi: {
7948 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7949 int Res = parsePHI(Inst, PFS);
7950 if (Res != 0)
7951 return Res;
7952 if (FMF.any()) {
7953 if (!isa<FPMathOperator>(Inst)) {
7954 Inst->deleteValue();
7955 return error(Loc, "fast-math-flags specified for phi without "
7956 "floating-point scalar or vector return type");
7957 }
7958 Inst->setFastMathFlags(FMF);
7959 }
7960 return 0;
7961 }
7963 return parseLandingPad(Inst, PFS);
7964 case lltok::kw_freeze:
7965 return parseFreeze(Inst, PFS);
7967 return parseBitInsert(Inst, PFS);
7969 return parseBitExtract(Inst, PFS);
7970 // Call.
7971 case lltok::kw_call:
7972 return parseCall(Inst, PFS, CallInst::TCK_None);
7973 case lltok::kw_tail:
7974 return parseCall(Inst, PFS, CallInst::TCK_Tail);
7975 case lltok::kw_musttail:
7976 return parseCall(Inst, PFS, CallInst::TCK_MustTail);
7977 case lltok::kw_notail:
7978 return parseCall(Inst, PFS, CallInst::TCK_NoTail);
7979 // Memory.
7980 case lltok::kw_alloca:
7981 return parseAlloc(Inst, PFS);
7982 case lltok::kw_load:
7983 return parseLoad(Inst, PFS);
7984 case lltok::kw_store:
7985 return parseStore(Inst, PFS);
7986 case lltok::kw_cmpxchg:
7987 return parseCmpXchg(Inst, PFS);
7989 return parseAtomicRMW(Inst, PFS);
7990 case lltok::kw_fence:
7991 return parseFence(Inst, PFS);
7993 return parseGetElementPtr(Inst, PFS);
7995 return parseExtractValue(Inst, PFS);
7997 return parseInsertValue(Inst, PFS);
7998 }
7999}
8000
8001/// parseCmpPredicate - parse an integer or fp predicate, based on Kind.
8002bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) {
8003 if (Opc == Instruction::FCmp) {
8004 switch (Lex.getKind()) {
8005 default:
8006 return tokError("expected fcmp predicate (e.g. 'oeq')");
8007 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
8008 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
8009 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
8010 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
8011 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
8012 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
8013 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
8014 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
8015 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
8016 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
8017 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
8018 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
8019 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
8020 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
8021 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
8022 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
8023 }
8024 } else {
8025 switch (Lex.getKind()) {
8026 default:
8027 return tokError("expected icmp predicate (e.g. 'eq')");
8028 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
8029 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
8030 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
8031 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
8032 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
8033 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
8034 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
8035 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
8036 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
8037 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
8038 }
8039 }
8040 Lex.Lex();
8041 return false;
8042}
8043
8044//===----------------------------------------------------------------------===//
8045// Terminator Instructions.
8046//===----------------------------------------------------------------------===//
8047
8048/// parseRet - parse a return instruction.
8049/// ::= 'ret' void (',' !dbg, !1)*
8050/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
8051bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB,
8052 PerFunctionState &PFS) {
8053 SMLoc TypeLoc = Lex.getLoc();
8054 Type *Ty = nullptr;
8055 if (parseType(Ty, true /*void allowed*/))
8056 return true;
8057
8058 Type *ResType = PFS.getFunction().getReturnType();
8059
8060 if (Ty->isVoidTy()) {
8061 if (!ResType->isVoidTy())
8062 return error(TypeLoc, "value doesn't match function result type '" +
8063 getTypeString(ResType) + "'");
8064
8065 Inst = ReturnInst::Create(Context);
8066 return false;
8067 }
8068
8069 Value *RV;
8070 if (parseValue(Ty, RV, PFS))
8071 return true;
8072
8073 if (ResType != RV->getType())
8074 return error(TypeLoc, "value doesn't match function result type '" +
8075 getTypeString(ResType) + "'");
8076
8077 Inst = ReturnInst::Create(Context, RV);
8078 return false;
8079}
8080
8081/// parseBr
8082/// ::= 'br' TypeAndValue
8083/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8084bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) {
8085 LocTy Loc, Loc2;
8086 Value *Op0;
8087 BasicBlock *Op1, *Op2;
8088 if (parseTypeAndValue(Op0, Loc, PFS))
8089 return true;
8090
8091 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
8092 Inst = UncondBrInst::Create(BB);
8093 return false;
8094 }
8095
8096 if (Op0->getType() != Type::getInt1Ty(Context))
8097 return error(Loc, "branch condition must have 'i1' type");
8098
8099 if (parseToken(lltok::comma, "expected ',' after branch condition") ||
8100 parseTypeAndBasicBlock(Op1, Loc, PFS) ||
8101 parseToken(lltok::comma, "expected ',' after true destination") ||
8102 parseTypeAndBasicBlock(Op2, Loc2, PFS))
8103 return true;
8104
8105 Inst = CondBrInst::Create(Op0, Op1, Op2);
8106 return false;
8107}
8108
8109/// parseSwitch
8110/// Instruction
8111/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
8112/// JumpTable
8113/// ::= (TypeAndValue ',' TypeAndValue)*
8114bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
8115 LocTy CondLoc, BBLoc;
8116 Value *Cond;
8117 BasicBlock *DefaultBB;
8118 if (parseTypeAndValue(Cond, CondLoc, PFS) ||
8119 parseToken(lltok::comma, "expected ',' after switch condition") ||
8120 parseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
8121 parseToken(lltok::lsquare, "expected '[' with switch table"))
8122 return true;
8123
8124 if (!Cond->getType()->isIntegerTy())
8125 return error(CondLoc, "switch condition must have integer type");
8126
8127 // parse the jump table pairs.
8128 SmallPtrSet<Value*, 32> SeenCases;
8130 while (Lex.getKind() != lltok::rsquare) {
8131 Value *Constant;
8132 BasicBlock *DestBB;
8133
8134 if (parseTypeAndValue(Constant, CondLoc, PFS) ||
8135 parseToken(lltok::comma, "expected ',' after case value") ||
8136 parseTypeAndBasicBlock(DestBB, PFS))
8137 return true;
8138
8139 if (!SeenCases.insert(Constant).second)
8140 return error(CondLoc, "duplicate case value in switch");
8141 if (!isa<ConstantInt>(Constant))
8142 return error(CondLoc, "case value is not a constant integer");
8143
8144 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
8145 }
8146
8147 Lex.Lex(); // Eat the ']'.
8148
8149 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
8150 for (const auto &[OnVal, Dest] : Table)
8151 SI->addCase(OnVal, Dest);
8152 Inst = SI;
8153 return false;
8154}
8155
8156/// parseIndirectBr
8157/// Instruction
8158/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
8159bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
8160 LocTy AddrLoc;
8161 Value *Address;
8162 if (parseTypeAndValue(Address, AddrLoc, PFS) ||
8163 parseToken(lltok::comma, "expected ',' after indirectbr address") ||
8164 parseToken(lltok::lsquare, "expected '[' with indirectbr"))
8165 return true;
8166
8167 if (!Address->getType()->isPointerTy())
8168 return error(AddrLoc, "indirectbr address must have pointer type");
8169
8170 // parse the destination list.
8171 SmallVector<BasicBlock*, 16> DestList;
8172
8173 if (Lex.getKind() != lltok::rsquare) {
8174 BasicBlock *DestBB;
8175 if (parseTypeAndBasicBlock(DestBB, PFS))
8176 return true;
8177 DestList.push_back(DestBB);
8178
8179 while (EatIfPresent(lltok::comma)) {
8180 if (parseTypeAndBasicBlock(DestBB, PFS))
8181 return true;
8182 DestList.push_back(DestBB);
8183 }
8184 }
8185
8186 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
8187 return true;
8188
8189 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
8190 for (BasicBlock *Dest : DestList)
8191 IBI->addDestination(Dest);
8192 Inst = IBI;
8193 return false;
8194}
8195
8196// If RetType is a non-function pointer type, then this is the short syntax
8197// for the call, which means that RetType is just the return type. Infer the
8198// rest of the function argument types from the arguments that are present.
8199bool LLParser::resolveFunctionType(Type *RetType, ArrayRef<ParamInfo> ArgList,
8200 FunctionType *&FuncTy) {
8201 FuncTy = dyn_cast<FunctionType>(RetType);
8202 if (!FuncTy) {
8203 // Pull out the types of all of the arguments...
8204 SmallVector<Type *, 8> ParamTypes;
8205 ParamTypes.reserve(ArgList.size());
8206 for (const ParamInfo &Arg : ArgList)
8207 ParamTypes.push_back(Arg.V->getType());
8208
8209 if (!FunctionType::isValidReturnType(RetType))
8210 return true;
8211
8212 FuncTy = FunctionType::get(RetType, ParamTypes, false);
8213 }
8214 return false;
8215}
8216
8217/// parseInvoke
8218/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
8219/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
8220bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
8221 LocTy CallLoc = Lex.getLoc();
8222 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8223 std::vector<unsigned> FwdRefAttrGrps;
8224 LocTy NoBuiltinLoc;
8225 unsigned CC;
8226 unsigned InvokeAddrSpace;
8227 Type *RetType = nullptr;
8228 LocTy RetTypeLoc;
8229 ValID CalleeID;
8232
8233 BasicBlock *NormalBB, *UnwindBB;
8234 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
8235 parseOptionalProgramAddrSpace(InvokeAddrSpace) ||
8236 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
8237 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
8238 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
8239 NoBuiltinLoc) ||
8240 parseOptionalOperandBundles(BundleList, PFS) ||
8241 parseToken(lltok::kw_to, "expected 'to' in invoke") ||
8242 parseTypeAndBasicBlock(NormalBB, PFS) ||
8243 parseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
8244 parseTypeAndBasicBlock(UnwindBB, PFS))
8245 return true;
8246
8247 // If RetType is a non-function pointer type, then this is the short syntax
8248 // for the call, which means that RetType is just the return type. Infer the
8249 // rest of the function argument types from the arguments that are present.
8250 FunctionType *Ty;
8251 if (resolveFunctionType(RetType, ArgList, Ty))
8252 return error(RetTypeLoc, "Invalid result type for LLVM function");
8253
8254 CalleeID.FTy = Ty;
8255
8256 // Look up the callee.
8257 Value *Callee;
8258 if (convertValIDToValue(PointerType::get(Context, InvokeAddrSpace), CalleeID,
8259 Callee, &PFS))
8260 return true;
8261
8262 // Set up the Attribute for the function.
8263 SmallVector<Value *, 8> Args;
8265
8266 // Loop through FunctionType's arguments and ensure they are specified
8267 // correctly. Also, gather any parameter attributes.
8268 FunctionType::param_iterator I = Ty->param_begin();
8269 FunctionType::param_iterator E = Ty->param_end();
8270 for (const ParamInfo &Arg : ArgList) {
8271 Type *ExpectedTy = nullptr;
8272 if (I != E) {
8273 ExpectedTy = *I++;
8274 } else if (!Ty->isVarArg()) {
8275 return error(Arg.Loc, "too many arguments specified");
8276 }
8277
8278 if (ExpectedTy && ExpectedTy != Arg.V->getType())
8279 return error(Arg.Loc, "argument is not of expected type '" +
8280 getTypeString(ExpectedTy) + "'");
8281 Args.push_back(Arg.V);
8282 ArgAttrs.push_back(Arg.Attrs);
8283 }
8284
8285 if (I != E)
8286 return error(CallLoc, "not enough parameters specified for call");
8287
8288 // Finish off the Attribute and check them
8289 AttributeList PAL =
8290 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
8291 AttributeSet::get(Context, RetAttrs), ArgAttrs);
8292
8293 InvokeInst *II =
8294 InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
8295 II->setCallingConv(CC);
8296 II->setAttributes(PAL);
8297 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
8298 Inst = II;
8299 return false;
8300}
8301
8302/// parseResume
8303/// ::= 'resume' TypeAndValue
8304bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) {
8305 Value *Exn; LocTy ExnLoc;
8306 if (parseTypeAndValue(Exn, ExnLoc, PFS))
8307 return true;
8308
8309 ResumeInst *RI = ResumeInst::Create(Exn);
8310 Inst = RI;
8311 return false;
8312}
8313
8314bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args,
8315 PerFunctionState &PFS) {
8316 if (parseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
8317 return true;
8318
8319 while (Lex.getKind() != lltok::rsquare) {
8320 // If this isn't the first argument, we need a comma.
8321 if (!Args.empty() &&
8322 parseToken(lltok::comma, "expected ',' in argument list"))
8323 return true;
8324
8325 // parse the argument.
8326 LocTy ArgLoc;
8327 Type *ArgTy = nullptr;
8328 if (parseType(ArgTy, ArgLoc))
8329 return true;
8330
8331 Value *V;
8332 if (ArgTy->isMetadataTy()) {
8333 if (parseMetadataAsValue(V, PFS))
8334 return true;
8335 } else {
8336 if (parseValue(ArgTy, V, PFS))
8337 return true;
8338 }
8339 Args.push_back(V);
8340 }
8341
8342 Lex.Lex(); // Lex the ']'.
8343 return false;
8344}
8345
8346/// parseCleanupRet
8347/// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
8348bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
8349 Value *CleanupPad = nullptr;
8350
8351 if (parseToken(lltok::kw_from, "expected 'from' after cleanupret"))
8352 return true;
8353
8354 if (parseValue(Type::getTokenTy(Context), CleanupPad, PFS))
8355 return true;
8356
8357 if (parseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
8358 return true;
8359
8360 BasicBlock *UnwindBB = nullptr;
8361 if (Lex.getKind() == lltok::kw_to) {
8362 Lex.Lex();
8363 if (parseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
8364 return true;
8365 } else {
8366 if (parseTypeAndBasicBlock(UnwindBB, PFS)) {
8367 return true;
8368 }
8369 }
8370
8371 Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
8372 return false;
8373}
8374
8375/// parseCatchRet
8376/// ::= 'catchret' from Parent Value 'to' TypeAndValue
8377bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
8378 Value *CatchPad = nullptr;
8379
8380 if (parseToken(lltok::kw_from, "expected 'from' after catchret"))
8381 return true;
8382
8383 if (parseValue(Type::getTokenTy(Context), CatchPad, PFS))
8384 return true;
8385
8386 BasicBlock *BB;
8387 if (parseToken(lltok::kw_to, "expected 'to' in catchret") ||
8388 parseTypeAndBasicBlock(BB, PFS))
8389 return true;
8390
8391 Inst = CatchReturnInst::Create(CatchPad, BB);
8392 return false;
8393}
8394
8395/// parseCatchSwitch
8396/// ::= 'catchswitch' within Parent
8397bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
8398 Value *ParentPad;
8399
8400 if (parseToken(lltok::kw_within, "expected 'within' after catchswitch"))
8401 return true;
8402
8403 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
8404 Lex.getKind() != lltok::LocalVarID)
8405 return tokError("expected scope value for catchswitch");
8406
8407 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
8408 return true;
8409
8410 if (parseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
8411 return true;
8412
8414 do {
8415 BasicBlock *DestBB;
8416 if (parseTypeAndBasicBlock(DestBB, PFS))
8417 return true;
8418 Table.push_back(DestBB);
8419 } while (EatIfPresent(lltok::comma));
8420
8421 if (parseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
8422 return true;
8423
8424 if (parseToken(lltok::kw_unwind, "expected 'unwind' after catchswitch scope"))
8425 return true;
8426
8427 BasicBlock *UnwindBB = nullptr;
8428 if (EatIfPresent(lltok::kw_to)) {
8429 if (parseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
8430 return true;
8431 } else {
8432 if (parseTypeAndBasicBlock(UnwindBB, PFS))
8433 return true;
8434 }
8435
8436 auto *CatchSwitch =
8437 CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
8438 for (BasicBlock *DestBB : Table)
8439 CatchSwitch->addHandler(DestBB);
8440 Inst = CatchSwitch;
8441 return false;
8442}
8443
8444/// parseCatchPad
8445/// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
8446bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
8447 Value *CatchSwitch = nullptr;
8448
8449 if (parseToken(lltok::kw_within, "expected 'within' after catchpad"))
8450 return true;
8451
8452 if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
8453 return tokError("expected scope value for catchpad");
8454
8455 if (parseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
8456 return true;
8457
8458 SmallVector<Value *, 8> Args;
8459 if (parseExceptionArgs(Args, PFS))
8460 return true;
8461
8462 Inst = CatchPadInst::Create(CatchSwitch, Args);
8463 return false;
8464}
8465
8466/// parseCleanupPad
8467/// ::= 'cleanuppad' within Parent ParamList
8468bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
8469 Value *ParentPad = nullptr;
8470
8471 if (parseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
8472 return true;
8473
8474 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
8475 Lex.getKind() != lltok::LocalVarID)
8476 return tokError("expected scope value for cleanuppad");
8477
8478 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
8479 return true;
8480
8481 SmallVector<Value *, 8> Args;
8482 if (parseExceptionArgs(Args, PFS))
8483 return true;
8484
8485 Inst = CleanupPadInst::Create(ParentPad, Args);
8486 return false;
8487}
8488
8489//===----------------------------------------------------------------------===//
8490// Unary Operators.
8491//===----------------------------------------------------------------------===//
8492
8493/// parseUnaryOp
8494/// ::= UnaryOp TypeAndValue ',' Value
8495///
8496/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
8497/// operand is allowed.
8498bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
8499 unsigned Opc, bool IsFP) {
8500 LocTy Loc; Value *LHS;
8501 if (parseTypeAndValue(LHS, Loc, PFS))
8502 return true;
8503
8504 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
8506
8507 if (!Valid)
8508 return error(Loc, "invalid operand type for instruction");
8509
8511 return false;
8512}
8513
8514/// parseCallBr
8515/// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList
8516/// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue
8517/// '[' LabelList ']'
8518bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
8519 LocTy CallLoc = Lex.getLoc();
8520 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8521 std::vector<unsigned> FwdRefAttrGrps;
8522 LocTy NoBuiltinLoc;
8523 unsigned CC;
8524 Type *RetType = nullptr;
8525 LocTy RetTypeLoc;
8526 ValID CalleeID;
8529
8530 BasicBlock *DefaultDest;
8531 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
8532 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
8533 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
8534 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
8535 NoBuiltinLoc) ||
8536 parseOptionalOperandBundles(BundleList, PFS) ||
8537 parseToken(lltok::kw_to, "expected 'to' in callbr") ||
8538 parseTypeAndBasicBlock(DefaultDest, PFS) ||
8539 parseToken(lltok::lsquare, "expected '[' in callbr"))
8540 return true;
8541
8542 // parse the destination list.
8543 SmallVector<BasicBlock *, 16> IndirectDests;
8544
8545 if (Lex.getKind() != lltok::rsquare) {
8546 BasicBlock *DestBB;
8547 if (parseTypeAndBasicBlock(DestBB, PFS))
8548 return true;
8549 IndirectDests.push_back(DestBB);
8550
8551 while (EatIfPresent(lltok::comma)) {
8552 if (parseTypeAndBasicBlock(DestBB, PFS))
8553 return true;
8554 IndirectDests.push_back(DestBB);
8555 }
8556 }
8557
8558 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
8559 return true;
8560
8561 // If RetType is a non-function pointer type, then this is the short syntax
8562 // for the call, which means that RetType is just the return type. Infer the
8563 // rest of the function argument types from the arguments that are present.
8564 FunctionType *Ty;
8565 if (resolveFunctionType(RetType, ArgList, Ty))
8566 return error(RetTypeLoc, "Invalid result type for LLVM function");
8567
8568 CalleeID.FTy = Ty;
8569
8570 // Look up the callee.
8571 Value *Callee;
8572 if (convertValIDToValue(PointerType::getUnqual(Context), CalleeID, Callee,
8573 &PFS))
8574 return true;
8575
8576 // Set up the Attribute for the function.
8577 SmallVector<Value *, 8> Args;
8579
8580 // Loop through FunctionType's arguments and ensure they are specified
8581 // correctly. Also, gather any parameter attributes.
8582 FunctionType::param_iterator I = Ty->param_begin();
8583 FunctionType::param_iterator E = Ty->param_end();
8584 for (const ParamInfo &Arg : ArgList) {
8585 Type *ExpectedTy = nullptr;
8586 if (I != E) {
8587 ExpectedTy = *I++;
8588 } else if (!Ty->isVarArg()) {
8589 return error(Arg.Loc, "too many arguments specified");
8590 }
8591
8592 if (ExpectedTy && ExpectedTy != Arg.V->getType())
8593 return error(Arg.Loc, "argument is not of expected type '" +
8594 getTypeString(ExpectedTy) + "'");
8595 Args.push_back(Arg.V);
8596 ArgAttrs.push_back(Arg.Attrs);
8597 }
8598
8599 if (I != E)
8600 return error(CallLoc, "not enough parameters specified for call");
8601
8602 // Finish off the Attribute and check them
8603 AttributeList PAL =
8604 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
8605 AttributeSet::get(Context, RetAttrs), ArgAttrs);
8606
8607 CallBrInst *CBI =
8608 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
8609 BundleList);
8610 CBI->setCallingConv(CC);
8611 CBI->setAttributes(PAL);
8612 ForwardRefAttrGroups[CBI] = FwdRefAttrGrps;
8613 Inst = CBI;
8614 return false;
8615}
8616
8617//===----------------------------------------------------------------------===//
8618// Binary Operators.
8619//===----------------------------------------------------------------------===//
8620
8621/// parseArithmetic
8622/// ::= ArithmeticOps TypeAndValue ',' Value
8623///
8624/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
8625/// operand is allowed.
8626bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
8627 unsigned Opc, bool IsFP) {
8628 LocTy Loc; Value *LHS, *RHS;
8629 if (parseTypeAndValue(LHS, Loc, PFS) ||
8630 parseToken(lltok::comma, "expected ',' in arithmetic operation") ||
8631 parseValue(LHS->getType(), RHS, PFS))
8632 return true;
8633
8634 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
8636
8637 if (!Valid)
8638 return error(Loc, "invalid operand type for instruction");
8639
8641 return false;
8642}
8643
8644/// parseLogical
8645/// ::= ArithmeticOps TypeAndValue ',' Value {
8646bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS,
8647 unsigned Opc) {
8648 LocTy Loc; Value *LHS, *RHS;
8649 if (parseTypeAndValue(LHS, Loc, PFS) ||
8650 parseToken(lltok::comma, "expected ',' in logical operation") ||
8651 parseValue(LHS->getType(), RHS, PFS))
8652 return true;
8653
8654 if (!LHS->getType()->isIntOrIntVectorTy())
8655 return error(Loc,
8656 "instruction requires integer or integer vector operands");
8657
8659 return false;
8660}
8661
8662/// parseCompare
8663/// ::= 'icmp' IPredicates TypeAndValue ',' Value
8664/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
8665bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS,
8666 unsigned Opc) {
8667 // parse the integer/fp comparison predicate.
8668 LocTy Loc;
8669 unsigned Pred;
8670 Value *LHS, *RHS;
8671 if (parseCmpPredicate(Pred, Opc) || parseTypeAndValue(LHS, Loc, PFS) ||
8672 parseToken(lltok::comma, "expected ',' after compare value") ||
8673 parseValue(LHS->getType(), RHS, PFS))
8674 return true;
8675
8676 if (Opc == Instruction::FCmp) {
8677 if (!LHS->getType()->isFPOrFPVectorTy())
8678 return error(Loc, "fcmp requires floating point operands");
8679 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
8680 } else {
8681 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
8682 if (!LHS->getType()->isIntOrIntVectorTy() &&
8684 return error(Loc, "icmp requires integer operands");
8685 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
8686 }
8687 return false;
8688}
8689
8690//===----------------------------------------------------------------------===//
8691// Other Instructions.
8692//===----------------------------------------------------------------------===//
8693
8694/// parseCast
8695/// ::= CastOpc TypeAndValue 'to' Type
8696bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS,
8697 unsigned Opc) {
8698 LocTy Loc;
8699 Value *Op;
8700 Type *DestTy = nullptr;
8701 if (parseTypeAndValue(Op, Loc, PFS) ||
8702 parseToken(lltok::kw_to, "expected 'to' after cast value") ||
8703 parseType(DestTy))
8704 return true;
8705
8707 return error(Loc, "invalid cast opcode for cast from '" +
8708 getTypeString(Op->getType()) + "' to '" +
8709 getTypeString(DestTy) + "'");
8710 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
8711 return false;
8712}
8713
8714/// parseSelect
8715/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8716bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) {
8717 LocTy Loc;
8718 Value *Op0, *Op1, *Op2;
8719 if (parseTypeAndValue(Op0, Loc, PFS) ||
8720 parseToken(lltok::comma, "expected ',' after select condition") ||
8721 parseTypeAndValue(Op1, PFS) ||
8722 parseToken(lltok::comma, "expected ',' after select value") ||
8723 parseTypeAndValue(Op2, PFS))
8724 return true;
8725
8726 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
8727 return error(Loc, Reason);
8728
8729 Inst = SelectInst::Create(Op0, Op1, Op2);
8730 return false;
8731}
8732
8733/// parseVAArg
8734/// ::= 'va_arg' TypeAndValue ',' Type
8735bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) {
8736 Value *Op;
8737 Type *EltTy = nullptr;
8738 LocTy TypeLoc;
8739 if (parseTypeAndValue(Op, PFS) ||
8740 parseToken(lltok::comma, "expected ',' after vaarg operand") ||
8741 parseType(EltTy, TypeLoc))
8742 return true;
8743
8744 if (!EltTy->isFirstClassType())
8745 return error(TypeLoc, "va_arg requires operand with first class type");
8746
8747 Inst = new VAArgInst(Op, EltTy);
8748 return false;
8749}
8750
8751/// parseExtractElement
8752/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
8753bool LLParser::parseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
8754 LocTy Loc;
8755 Value *Op0, *Op1;
8756 if (parseTypeAndValue(Op0, Loc, PFS) ||
8757 parseToken(lltok::comma, "expected ',' after extract value") ||
8758 parseTypeAndValue(Op1, PFS))
8759 return true;
8760
8762 return error(Loc, "invalid extractelement operands");
8763
8764 Inst = ExtractElementInst::Create(Op0, Op1);
8765 return false;
8766}
8767
8768/// parseInsertElement
8769/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8770bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
8771 LocTy Loc;
8772 Value *Op0, *Op1, *Op2;
8773 if (parseTypeAndValue(Op0, Loc, PFS) ||
8774 parseToken(lltok::comma, "expected ',' after insertelement value") ||
8775 parseTypeAndValue(Op1, PFS) ||
8776 parseToken(lltok::comma, "expected ',' after insertelement value") ||
8777 parseTypeAndValue(Op2, PFS))
8778 return true;
8779
8780 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
8781 return error(Loc, "invalid insertelement operands");
8782
8783 Inst = InsertElementInst::Create(Op0, Op1, Op2);
8784 return false;
8785}
8786
8787// parseBitExtract
8788// ::= 'bitextract' Type ',' TypeAndValue ',' TypeAndValue
8789bool LLParser::parseBitExtract(Instruction *&Inst, PerFunctionState &PFS) {
8790 LocTy Loc;
8791 Type *Ty = nullptr;
8792 Value *Op0, *Op1;
8793 if (parseType(Ty, Loc) ||
8794 parseToken(lltok::comma, "expected ',' after bitextract type") ||
8795 parseTypeAndValue(Op0, Loc, PFS) ||
8796 parseToken(lltok::comma, "expected ',' after bitextract source value") ||
8797 parseTypeAndValue(Op1, PFS))
8798 return true;
8799
8800 if (const char *Reason = BitExtractInst::areInvalidOperands(Ty, Op0, Op1))
8801 return error(Loc, Reason);
8802
8803 Inst = BitExtractInst::Create(Ty, Op0, Op1);
8804 return false;
8805}
8806
8807// parseBitInsert
8808// ::= 'bitinsert' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8809bool LLParser::parseBitInsert(Instruction *&Inst, PerFunctionState &PFS) {
8810 LocTy Loc;
8811 Value *Op0, *Op1, *Op2;
8812 if (parseTypeAndValue(Op0, Loc, PFS) ||
8813 parseToken(lltok::comma, "expected ',' after bitinsert source value") ||
8814 parseTypeAndValue(Op1, PFS) ||
8815 parseToken(lltok::comma, "expected ',' after bitinsert insert value") ||
8816 parseTypeAndValue(Op2, PFS))
8817 return true;
8818
8819 if (const char *Reason = BitInsertInst::areInvalidOperands(Op0, Op1, Op2))
8820 return error(Loc, Reason);
8821
8822 Inst = BitInsertInst::Create(Op0, Op1, Op2);
8823 return false;
8824}
8825
8826/// parseShuffleVector
8827/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8828bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
8829 LocTy Loc;
8830 Value *Op0, *Op1, *Op2;
8831 if (parseTypeAndValue(Op0, Loc, PFS) ||
8832 parseToken(lltok::comma, "expected ',' after shuffle mask") ||
8833 parseTypeAndValue(Op1, PFS) ||
8834 parseToken(lltok::comma, "expected ',' after shuffle value") ||
8835 parseTypeAndValue(Op2, PFS))
8836 return true;
8837
8838 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
8839 return error(Loc, "invalid shufflevector operands");
8840
8841 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
8842 return false;
8843}
8844
8845/// parsePHI
8846/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
8847int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) {
8848 Type *Ty = nullptr; LocTy TypeLoc;
8849 Value *Op0, *Op1;
8850
8851 if (parseType(Ty, TypeLoc))
8852 return true;
8853
8854 if (!Ty->isFirstClassType())
8855 return error(TypeLoc, "phi node must have first class type");
8856
8857 bool First = true;
8858 bool AteExtraComma = false;
8860
8861 while (true) {
8862 if (First) {
8863 if (Lex.getKind() != lltok::lsquare)
8864 break;
8865 First = false;
8866 } else if (!EatIfPresent(lltok::comma))
8867 break;
8868
8869 if (Lex.getKind() == lltok::MetadataVar) {
8870 AteExtraComma = true;
8871 break;
8872 }
8873
8874 if (parseToken(lltok::lsquare, "expected '[' in phi value list") ||
8875 parseValue(Ty, Op0, PFS) ||
8876 parseToken(lltok::comma, "expected ',' after insertelement value") ||
8877 parseValue(Type::getLabelTy(Context), Op1, PFS) ||
8878 parseToken(lltok::rsquare, "expected ']' in phi value list"))
8879 return true;
8880
8881 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
8882 }
8883
8884 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
8885 for (const auto &[Val, BB] : PHIVals)
8886 PN->addIncoming(Val, BB);
8887 Inst = PN;
8888 return AteExtraComma ? InstExtraComma : InstNormal;
8889}
8890
8891/// parseLandingPad
8892/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
8893/// Clause
8894/// ::= 'catch' TypeAndValue
8895/// ::= 'filter'
8896/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
8897bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
8898 Type *Ty = nullptr; LocTy TyLoc;
8899
8900 if (parseType(Ty, TyLoc))
8901 return true;
8902
8903 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
8904 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
8905
8906 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
8908 if (EatIfPresent(lltok::kw_catch))
8910 else if (EatIfPresent(lltok::kw_filter))
8912 else
8913 return tokError("expected 'catch' or 'filter' clause type");
8914
8915 Value *V;
8916 LocTy VLoc;
8917 if (parseTypeAndValue(V, VLoc, PFS))
8918 return true;
8919
8920 // A 'catch' type expects a non-array constant. A filter clause expects an
8921 // array constant.
8922 if (CT == LandingPadInst::Catch) {
8923 if (isa<ArrayType>(V->getType()))
8924 return error(VLoc, "'catch' clause has an invalid type");
8925 } else {
8926 if (!isa<ArrayType>(V->getType()))
8927 return error(VLoc, "'filter' clause has an invalid type");
8928 }
8929
8931 if (!CV)
8932 return error(VLoc, "clause argument must be a constant");
8933 LP->addClause(CV);
8934 }
8935
8936 Inst = LP.release();
8937 return false;
8938}
8939
8940/// parseFreeze
8941/// ::= 'freeze' Type Value
8942bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) {
8943 LocTy Loc;
8944 Value *Op;
8945 if (parseTypeAndValue(Op, Loc, PFS))
8946 return true;
8947
8948 Inst = new FreezeInst(Op);
8949 return false;
8950}
8951
8952/// parseCall
8953/// ::= 'call' OptionalFastMathFlags OptionalCallingConv
8954/// OptionalAttrs Type Value ParameterList OptionalAttrs
8955/// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
8956/// OptionalAttrs Type Value ParameterList OptionalAttrs
8957/// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
8958/// OptionalAttrs Type Value ParameterList OptionalAttrs
8959/// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv
8960/// OptionalAttrs Type Value ParameterList OptionalAttrs
8961bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
8963 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8964 std::vector<unsigned> FwdRefAttrGrps;
8965 LocTy BuiltinLoc;
8966 unsigned CallAddrSpace;
8967 unsigned CC;
8968 Type *RetType = nullptr;
8969 LocTy RetTypeLoc;
8970 ValID CalleeID;
8973 LocTy CallLoc = Lex.getLoc();
8974
8975 if (TCK != CallInst::TCK_None &&
8976 parseToken(lltok::kw_call,
8977 "expected 'tail call', 'musttail call', or 'notail call'"))
8978 return true;
8979
8980 FastMathFlags FMF = EatFastMathFlagsIfPresent();
8981
8982 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
8983 parseOptionalProgramAddrSpace(CallAddrSpace) ||
8984 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
8985 parseValID(CalleeID, &PFS) ||
8986 parseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
8987 PFS.getFunction().isVarArg()) ||
8988 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
8989 parseOptionalOperandBundles(BundleList, PFS))
8990 return true;
8991
8992 // If RetType is a non-function pointer type, then this is the short syntax
8993 // for the call, which means that RetType is just the return type. Infer the
8994 // rest of the function argument types from the arguments that are present.
8995 FunctionType *Ty;
8996 if (resolveFunctionType(RetType, ArgList, Ty))
8997 return error(RetTypeLoc, "Invalid result type for LLVM function");
8998
8999 CalleeID.FTy = Ty;
9000
9001 // Look up the callee.
9002 Value *Callee;
9003 if (convertValIDToValue(PointerType::get(Context, CallAddrSpace), CalleeID,
9004 Callee, &PFS))
9005 return true;
9006
9007 // Set up the Attribute for the function.
9009
9010 SmallVector<Value*, 8> Args;
9011
9012 // Loop through FunctionType's arguments and ensure they are specified
9013 // correctly. Also, gather any parameter attributes.
9014 FunctionType::param_iterator I = Ty->param_begin();
9015 FunctionType::param_iterator E = Ty->param_end();
9016 for (const ParamInfo &Arg : ArgList) {
9017 Type *ExpectedTy = nullptr;
9018 if (I != E) {
9019 ExpectedTy = *I++;
9020 } else if (!Ty->isVarArg()) {
9021 return error(Arg.Loc, "too many arguments specified");
9022 }
9023
9024 if (ExpectedTy && ExpectedTy != Arg.V->getType())
9025 return error(Arg.Loc, "argument is not of expected type '" +
9026 getTypeString(ExpectedTy) + "'");
9027 Args.push_back(Arg.V);
9028 Attrs.push_back(Arg.Attrs);
9029 }
9030
9031 if (I != E)
9032 return error(CallLoc, "not enough parameters specified for call");
9033
9034 // Finish off the Attribute and check them
9035 AttributeList PAL =
9036 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
9037 AttributeSet::get(Context, RetAttrs), Attrs);
9038
9039 CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
9040 CI->setTailCallKind(TCK);
9041 CI->setCallingConv(CC);
9042 if (FMF.any()) {
9043 if (!isa<FPMathOperator>(CI)) {
9044 CI->deleteValue();
9045 return error(CallLoc, "fast-math-flags specified for call without "
9046 "floating-point scalar or vector return type");
9047 }
9048 CI->setFastMathFlags(FMF);
9049 }
9050
9051 if (CalleeID.Kind == ValID::t_GlobalName &&
9052 isOldDbgFormatIntrinsic(CalleeID.StrVal)) {
9053 if (SeenNewDbgInfoFormat) {
9054 CI->deleteValue();
9055 return error(CallLoc, "llvm.dbg intrinsic should not appear in a module "
9056 "using non-intrinsic debug info");
9057 }
9058 SeenOldDbgInfoFormat = true;
9059 }
9060 CI->setAttributes(PAL);
9061 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
9062 Inst = CI;
9063 return false;
9064}
9065
9066//===----------------------------------------------------------------------===//
9067// Memory Instructions.
9068//===----------------------------------------------------------------------===//
9069
9070/// parseAlloc
9071/// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
9072/// (',' 'align' i32)? (',', 'addrspace(n))?
9073int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
9074 Value *Size = nullptr;
9075 LocTy SizeLoc, TyLoc, ASLoc;
9076 MaybeAlign Alignment;
9077 unsigned AddrSpace = 0;
9078 Type *Ty = nullptr;
9079
9080 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
9081 bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
9082
9083 if (parseType(Ty, TyLoc))
9084 return true;
9085
9087 return error(TyLoc, "invalid type for alloca");
9088
9089 bool AteExtraComma = false;
9090 if (EatIfPresent(lltok::comma)) {
9091 if (Lex.getKind() == lltok::kw_align) {
9092 if (parseOptionalAlignment(Alignment))
9093 return true;
9094 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
9095 return true;
9096 } else if (Lex.getKind() == lltok::kw_addrspace) {
9097 ASLoc = Lex.getLoc();
9098 if (parseOptionalAddrSpace(AddrSpace))
9099 return true;
9100 } else if (Lex.getKind() == lltok::MetadataVar) {
9101 AteExtraComma = true;
9102 } else {
9103 if (parseTypeAndValue(Size, SizeLoc, PFS))
9104 return true;
9105 if (EatIfPresent(lltok::comma)) {
9106 if (Lex.getKind() == lltok::kw_align) {
9107 if (parseOptionalAlignment(Alignment))
9108 return true;
9109 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
9110 return true;
9111 } else if (Lex.getKind() == lltok::kw_addrspace) {
9112 ASLoc = Lex.getLoc();
9113 if (parseOptionalAddrSpace(AddrSpace))
9114 return true;
9115 } else if (Lex.getKind() == lltok::MetadataVar) {
9116 AteExtraComma = true;
9117 }
9118 }
9119 }
9120 }
9121
9122 if (Size && !Size->getType()->isIntegerTy())
9123 return error(SizeLoc, "element count must have integer type");
9124
9125 if (!Alignment && !Ty->isSized())
9126 return error(TyLoc, "Cannot allocate unsized type");
9127 if (!Alignment)
9128 Alignment = M->getDataLayout().getPrefTypeAlign(Ty);
9129 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment);
9130 AI->setUsedWithInAlloca(IsInAlloca);
9131 AI->setSwiftError(IsSwiftError);
9132 Inst = AI;
9133 return AteExtraComma ? InstExtraComma : InstNormal;
9134}
9135
9136/// parseLoad
9137/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
9138/// ::= 'load' 'atomic' 'volatile'? 'elementwise'? TypeAndValue
9139/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
9140int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) {
9141 Value *Val; LocTy Loc;
9142 MaybeAlign Alignment;
9143 bool AteExtraComma = false;
9144 bool isAtomic = false;
9147
9148 if (Lex.getKind() == lltok::kw_atomic) {
9149 isAtomic = true;
9150 Lex.Lex();
9151 }
9152
9153 bool isVolatile = false;
9154 if (Lex.getKind() == lltok::kw_volatile) {
9155 isVolatile = true;
9156 Lex.Lex();
9157 }
9158
9159 bool IsElementwise = false;
9160 if (Lex.getKind() == lltok::kw_elementwise) {
9161 IsElementwise = true;
9162 Lex.Lex();
9163 }
9164
9165 Type *Ty;
9166 LocTy ExplicitTypeLoc = Lex.getLoc();
9167 if (parseType(Ty) ||
9168 parseToken(lltok::comma, "expected comma after load's type") ||
9169 parseTypeAndValue(Val, Loc, PFS) ||
9170 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
9171 parseOptionalCommaAlign(Alignment, AteExtraComma))
9172 return true;
9173
9174 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
9175 return error(Loc, "load operand must be a pointer to a first class type");
9176
9177 if (IsElementwise && !isAtomic)
9178 return error(Loc, "elementwise load must be atomic");
9179
9180 if (IsElementwise && !isa<FixedVectorType>(Ty))
9181 return error(ExplicitTypeLoc,
9182 "atomic elementwise load operand must have fixed vector type");
9183
9184 if (isAtomic && !Alignment)
9185 return error(Loc, "atomic load must have explicit non-zero alignment");
9186
9187 if (Ordering == AtomicOrdering::Release ||
9189 return error(Loc, "atomic load cannot use Release ordering");
9190 if (IsElementwise && Ordering == AtomicOrdering::SequentiallyConsistent)
9191 return error(Loc,
9192 "atomic elementwise load cannot be sequentially consistent");
9193
9194 if (!Alignment && !Ty->isSized())
9195 return error(ExplicitTypeLoc, "loading unsized types is not allowed");
9196 if (!Alignment)
9197 Alignment = M->getDataLayout().getABITypeAlign(Ty);
9198 Inst = new LoadInst(Ty, Val, "",
9199 LoadStoreInstProperties{isVolatile, *Alignment, Ordering,
9200 SSID, IsElementwise},
9201 /*InsertBefore=*/nullptr);
9202 return AteExtraComma ? InstExtraComma : InstNormal;
9203}
9204
9205/// parseStore
9206
9207/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
9208/// ::= 'store' 'atomic' 'volatile'? 'elementwise'? TypeAndValue ','
9209/// TypeAndValue 'singlethread'? AtomicOrdering (',' 'align' i32)?
9210int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) {
9211 Value *Val, *Ptr;
9212 LocTy Loc, PtrLoc;
9213 MaybeAlign Alignment;
9214 bool AteExtraComma = false;
9215 bool isAtomic = false;
9218
9219 if (Lex.getKind() == lltok::kw_atomic) {
9220 isAtomic = true;
9221 Lex.Lex();
9222 }
9223
9224 bool isVolatile = false;
9225 if (Lex.getKind() == lltok::kw_volatile) {
9226 isVolatile = true;
9227 Lex.Lex();
9228 }
9229
9230 bool IsElementwise = false;
9231 if (Lex.getKind() == lltok::kw_elementwise) {
9232 IsElementwise = true;
9233 Lex.Lex();
9234 }
9235
9236 if (parseTypeAndValue(Val, Loc, PFS) ||
9237 parseToken(lltok::comma, "expected ',' after store operand") ||
9238 parseTypeAndValue(Ptr, PtrLoc, PFS) ||
9239 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
9240 parseOptionalCommaAlign(Alignment, AteExtraComma))
9241 return true;
9242
9243 if (!Ptr->getType()->isPointerTy())
9244 return error(PtrLoc, "store operand must be a pointer");
9245 if (!Val->getType()->isFirstClassType())
9246 return error(Loc, "store operand must be a first class value");
9247 if (isAtomic && !Alignment)
9248 return error(Loc, "atomic store must have explicit non-zero alignment");
9249 if (Ordering == AtomicOrdering::Acquire ||
9251 return error(Loc, "atomic store cannot use Acquire ordering");
9252
9253 if (IsElementwise && !isAtomic)
9254 return error(Loc, "elementwise store must be atomic");
9255
9256 if (IsElementwise && !isa<FixedVectorType>(Val->getType()))
9257 return error(
9258 Loc, "atomic elementwise store operand must have fixed vector type");
9259
9260 if (IsElementwise && Ordering == AtomicOrdering::SequentiallyConsistent)
9261 return error(Loc,
9262 "atomic elementwise store cannot be sequentially consistent");
9263
9264 if (!Alignment && !Val->getType()->isSized())
9265 return error(Loc, "storing unsized types is not allowed");
9266 if (!Alignment)
9267 Alignment = M->getDataLayout().getABITypeAlign(Val->getType());
9268
9269 Inst = new StoreInst(Val, Ptr,
9270 LoadStoreInstProperties{isVolatile, *Alignment, Ordering,
9271 SSID, IsElementwise},
9272 /*InsertBefore=*/nullptr);
9273 return AteExtraComma ? InstExtraComma : InstNormal;
9274}
9275
9276/// parseCmpXchg
9277/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
9278/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ','
9279/// 'Align'?
9280int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
9281 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
9282 bool AteExtraComma = false;
9283 AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
9284 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
9286 bool isVolatile = false;
9287 bool isWeak = false;
9288 MaybeAlign Alignment;
9289
9290 if (EatIfPresent(lltok::kw_weak))
9291 isWeak = true;
9292
9293 if (EatIfPresent(lltok::kw_volatile))
9294 isVolatile = true;
9295
9296 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
9297 parseToken(lltok::comma, "expected ',' after cmpxchg address") ||
9298 parseTypeAndValue(Cmp, CmpLoc, PFS) ||
9299 parseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
9300 parseTypeAndValue(New, NewLoc, PFS) ||
9301 parseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
9302 parseOrdering(FailureOrdering) ||
9303 parseOptionalCommaAlign(Alignment, AteExtraComma))
9304 return true;
9305
9306 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering))
9307 return tokError("invalid cmpxchg success ordering");
9308 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering))
9309 return tokError("invalid cmpxchg failure ordering");
9310 if (!Ptr->getType()->isPointerTy())
9311 return error(PtrLoc, "cmpxchg operand must be a pointer");
9312 if (Cmp->getType() != New->getType())
9313 return error(NewLoc, "compare value and new value type do not match");
9314 if (!New->getType()->isFirstClassType())
9315 return error(NewLoc, "cmpxchg operand must be a first class value");
9316
9317 const Align DefaultAlignment(
9318 PFS.getFunction().getDataLayout().getTypeStoreSize(
9319 Cmp->getType()));
9320
9321 AtomicCmpXchgInst *CXI =
9322 new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(DefaultAlignment),
9323 SuccessOrdering, FailureOrdering, SSID);
9324 CXI->setVolatile(isVolatile);
9325 CXI->setWeak(isWeak);
9326
9327 Inst = CXI;
9328 return AteExtraComma ? InstExtraComma : InstNormal;
9329}
9330
9331/// parseAtomicRMW
9332/// ::= 'atomicrmw' 'volatile'? 'elementwise'? BinOp TypeAndValue ','
9333/// TypeAndValue
9334/// 'singlethread'? AtomicOrdering
9335int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
9336 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
9337 bool AteExtraComma = false;
9340 bool IsVolatile = false;
9341 bool IsElementwise = false;
9342 bool IsFP = false;
9344 MaybeAlign Alignment;
9345
9346 if (EatIfPresent(lltok::kw_volatile))
9347 IsVolatile = true;
9348 if (EatIfPresent(lltok::kw_elementwise))
9349 IsElementwise = true;
9350
9351 switch (Lex.getKind()) {
9352 default:
9353 return tokError("expected binary operation in atomicrmw");
9367 break;
9370 break;
9373 break;
9374 case lltok::kw_usub_sat:
9376 break;
9377 case lltok::kw_fadd:
9379 IsFP = true;
9380 break;
9381 case lltok::kw_fsub:
9383 IsFP = true;
9384 break;
9385 case lltok::kw_fmax:
9387 IsFP = true;
9388 break;
9389 case lltok::kw_fmin:
9391 IsFP = true;
9392 break;
9393 case lltok::kw_fmaximum:
9395 IsFP = true;
9396 break;
9397 case lltok::kw_fminimum:
9399 IsFP = true;
9400 break;
9403 IsFP = true;
9404 break;
9407 IsFP = true;
9408 break;
9409 }
9410 Lex.Lex(); // Eat the operation.
9411
9412 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
9413 parseToken(lltok::comma, "expected ',' after atomicrmw address") ||
9414 parseTypeAndValue(Val, ValLoc, PFS) ||
9415 parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering) ||
9416 parseOptionalCommaAlign(Alignment, AteExtraComma))
9417 return true;
9418
9419 if (Ordering == AtomicOrdering::Unordered)
9420 return tokError("atomicrmw cannot be unordered");
9421 if (IsElementwise && Ordering == AtomicOrdering::SequentiallyConsistent)
9422 return tokError("atomicrmw elementwise cannot be sequentially consistent");
9423 if (!Ptr->getType()->isPointerTy())
9424 return error(PtrLoc, "atomicrmw operand must be a pointer");
9425 if (Val->getType()->isScalableTy())
9426 return error(ValLoc, "atomicrmw operand may not be scalable");
9427
9428 Type *ValTy = Val->getType();
9429 if (IsElementwise) {
9430 if (!isa<FixedVectorType>(Val->getType()))
9431 return error(ValLoc,
9432 "atomicrmw elementwise operand must be a fixed vector type");
9433 }
9434
9436 if (!ValTy->isIntOrIntVectorTy() && !ValTy->isFPOrFPVectorTy() &&
9437 !ValTy->isPtrOrPtrVectorTy()) {
9438 return error(
9439 ValLoc,
9441 " operand must be an integer type, a floating-point type, a "
9442 "pointer type, or a fixed vector of any of these types");
9443 }
9444 } else if (IsFP) {
9445 if (!ValTy->isFPOrFPVectorTy()) {
9446 return error(ValLoc, "atomicrmw " +
9448 " operand must be a floating point or fixed "
9449 "vector of floating point type");
9450 }
9451 } else {
9452 if (!ValTy->isIntOrIntVectorTy()) {
9453 return error(
9454 ValLoc,
9456 " operand must be an integer or fixed vector of integer type");
9457 }
9458 }
9459
9460 unsigned Size =
9461 PFS.getFunction().getDataLayout().getTypeStoreSizeInBits(ValTy);
9462 if (Size < 8 || (Size & (Size - 1)))
9463 return error(ValLoc,
9464 "atomicrmw operand must have a power-of-two byte size");
9465 const Align DefaultAlignment(
9466 PFS.getFunction().getDataLayout().getTypeStoreSize(Val->getType()));
9467 AtomicRMWInst *RMWI = new AtomicRMWInst(Operation, Ptr, Val,
9468 Alignment.value_or(DefaultAlignment),
9469 Ordering, SSID, IsElementwise);
9470 RMWI->setVolatile(IsVolatile);
9471 Inst = RMWI;
9472 return AteExtraComma ? InstExtraComma : InstNormal;
9473}
9474
9475/// parseFence
9476/// ::= 'fence' 'singlethread'? AtomicOrdering
9477int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) {
9480 if (parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
9481 return true;
9482
9483 if (Ordering == AtomicOrdering::Unordered)
9484 return tokError("fence cannot be unordered");
9485 if (Ordering == AtomicOrdering::Monotonic)
9486 return tokError("fence cannot be monotonic");
9487
9488 Inst = new FenceInst(Context, Ordering, SSID);
9489 return InstNormal;
9490}
9491
9492/// parseGetElementPtr
9493/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
9494int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
9495 Value *Ptr = nullptr;
9496 Value *Val = nullptr;
9497 LocTy Loc, EltLoc;
9498 GEPNoWrapFlags NW;
9499
9500 while (true) {
9501 if (EatIfPresent(lltok::kw_inbounds))
9503 else if (EatIfPresent(lltok::kw_nusw))
9505 else if (EatIfPresent(lltok::kw_nuw))
9507 else
9508 break;
9509 }
9510
9511 Type *Ty = nullptr;
9512 if (parseType(Ty) ||
9513 parseToken(lltok::comma, "expected comma after getelementptr's type") ||
9514 parseTypeAndValue(Ptr, Loc, PFS))
9515 return true;
9516
9517 Type *BaseType = Ptr->getType();
9518 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
9519 if (!BasePointerType)
9520 return error(Loc, "base of getelementptr must be a pointer");
9521
9522 SmallVector<Value*, 16> Indices;
9523 bool AteExtraComma = false;
9524 // GEP returns a vector of pointers if at least one of parameters is a vector.
9525 // All vector parameters should have the same vector width.
9526 ElementCount GEPWidth = BaseType->isVectorTy()
9527 ? cast<VectorType>(BaseType)->getElementCount()
9529
9530 while (EatIfPresent(lltok::comma)) {
9531 if (Lex.getKind() == lltok::MetadataVar) {
9532 AteExtraComma = true;
9533 break;
9534 }
9535 if (parseTypeAndValue(Val, EltLoc, PFS))
9536 return true;
9537 if (!Val->getType()->isIntOrIntVectorTy())
9538 return error(EltLoc, "getelementptr index must be an integer");
9539
9540 if (auto *ValVTy = dyn_cast<VectorType>(Val->getType())) {
9541 ElementCount ValNumEl = ValVTy->getElementCount();
9542 if (GEPWidth != ElementCount::getFixed(0) && GEPWidth != ValNumEl)
9543 return error(
9544 EltLoc,
9545 "getelementptr vector index has a wrong number of elements");
9546 GEPWidth = ValNumEl;
9547 }
9548 Indices.push_back(Val);
9549 }
9550
9551 if (!Indices.empty() && !Ty->isSized())
9552 return error(Loc, "base element of getelementptr must be sized");
9553
9554 auto *STy = dyn_cast<StructType>(Ty);
9555 if (STy && STy->isScalableTy())
9556 return error(Loc, "getelementptr cannot target structure that contains "
9557 "scalable vector type");
9558
9559 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
9560 return error(Loc, "invalid getelementptr indices");
9561 GetElementPtrInst *GEP = GetElementPtrInst::Create(Ty, Ptr, Indices);
9562 Inst = GEP;
9563 GEP->setNoWrapFlags(NW);
9564 return AteExtraComma ? InstExtraComma : InstNormal;
9565}
9566
9567/// parseExtractValue
9568/// ::= 'extractvalue' TypeAndValue (',' uint32)+
9569int LLParser::parseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
9570 Value *Val; LocTy Loc;
9571 SmallVector<unsigned, 4> Indices;
9572 bool AteExtraComma;
9573 if (parseTypeAndValue(Val, Loc, PFS) ||
9574 parseIndexList(Indices, AteExtraComma))
9575 return true;
9576
9577 if (!Val->getType()->isAggregateType())
9578 return error(Loc, "extractvalue operand must be aggregate type");
9579
9580 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
9581 return error(Loc, "invalid indices for extractvalue");
9582 Inst = ExtractValueInst::Create(Val, Indices);
9583 return AteExtraComma ? InstExtraComma : InstNormal;
9584}
9585
9586/// parseInsertValue
9587/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
9588int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
9589 Value *Val0, *Val1; LocTy Loc0, Loc1;
9590 SmallVector<unsigned, 4> Indices;
9591 bool AteExtraComma;
9592 if (parseTypeAndValue(Val0, Loc0, PFS) ||
9593 parseToken(lltok::comma, "expected comma after insertvalue operand") ||
9594 parseTypeAndValue(Val1, Loc1, PFS) ||
9595 parseIndexList(Indices, AteExtraComma))
9596 return true;
9597
9598 if (!Val0->getType()->isAggregateType())
9599 return error(Loc0, "insertvalue operand must be aggregate type");
9600
9601 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
9602 if (!IndexedType)
9603 return error(Loc0, "invalid indices for insertvalue");
9604 if (IndexedType != Val1->getType())
9605 return error(Loc1, "insertvalue operand and field disagree in type: '" +
9606 getTypeString(Val1->getType()) + "' instead of '" +
9607 getTypeString(IndexedType) + "'");
9608 Inst = InsertValueInst::Create(Val0, Val1, Indices);
9609 return AteExtraComma ? InstExtraComma : InstNormal;
9610}
9611
9612//===----------------------------------------------------------------------===//
9613// Embedded metadata.
9614//===----------------------------------------------------------------------===//
9615
9616/// parseMDNodeVector
9617/// ::= { Element (',' Element)* }
9618/// Element
9619/// ::= 'null' | Metadata
9620bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
9621 if (parseToken(lltok::lbrace, "expected '{' here"))
9622 return true;
9623
9624 // Check for an empty list.
9625 if (EatIfPresent(lltok::rbrace))
9626 return false;
9627
9628 do {
9629 if (EatIfPresent(lltok::kw_null)) {
9630 Elts.push_back(nullptr);
9631 continue;
9632 }
9633
9634 Metadata *MD;
9635 if (parseMetadata(MD, nullptr))
9636 return true;
9637 Elts.push_back(MD);
9638 } while (EatIfPresent(lltok::comma));
9639
9640 return parseToken(lltok::rbrace, "expected end of metadata node");
9641}
9642
9643//===----------------------------------------------------------------------===//
9644// Use-list order directives.
9645//===----------------------------------------------------------------------===//
9646bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
9647 SMLoc Loc) {
9648 if (!V->hasUseList())
9649 return false;
9650 if (V->use_empty())
9651 return error(Loc, "value has no uses");
9652
9653 unsigned NumUses = 0;
9654 SmallDenseMap<const Use *, unsigned, 16> Order;
9655 for (const Use &U : V->uses()) {
9656 if (++NumUses > Indexes.size())
9657 break;
9658 Order[&U] = Indexes[NumUses - 1];
9659 }
9660 if (NumUses < 2)
9661 return error(Loc, "value only has one use");
9662 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
9663 return error(Loc,
9664 "wrong number of indexes, expected " + Twine(V->getNumUses()));
9665
9666 V->sortUseList([&](const Use &L, const Use &R) {
9667 return Order.lookup(&L) < Order.lookup(&R);
9668 });
9669 return false;
9670}
9671
9672/// parseUseListOrderIndexes
9673/// ::= '{' uint32 (',' uint32)+ '}'
9674bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
9675 SMLoc Loc = Lex.getLoc();
9676 if (parseToken(lltok::lbrace, "expected '{' here"))
9677 return true;
9678 if (Lex.getKind() == lltok::rbrace)
9679 return tokError("expected non-empty list of uselistorder indexes");
9680
9681 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
9682 // indexes should be distinct numbers in the range [0, size-1], and should
9683 // not be in order.
9684 unsigned Offset = 0;
9685 unsigned Max = 0;
9686 bool IsOrdered = true;
9687 assert(Indexes.empty() && "Expected empty order vector");
9688 do {
9689 unsigned Index;
9690 if (parseUInt32(Index))
9691 return true;
9692
9693 // Update consistency checks.
9694 Offset += Index - Indexes.size();
9695 Max = std::max(Max, Index);
9696 IsOrdered &= Index == Indexes.size();
9697
9698 Indexes.push_back(Index);
9699 } while (EatIfPresent(lltok::comma));
9700
9701 if (parseToken(lltok::rbrace, "expected '}' here"))
9702 return true;
9703
9704 if (Indexes.size() < 2)
9705 return error(Loc, "expected >= 2 uselistorder indexes");
9706 if (Offset != 0 || Max >= Indexes.size())
9707 return error(Loc,
9708 "expected distinct uselistorder indexes in range [0, size)");
9709 if (IsOrdered)
9710 return error(Loc, "expected uselistorder indexes to change the order");
9711
9712 return false;
9713}
9714
9715/// parseUseListOrder
9716/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
9717bool LLParser::parseUseListOrder(PerFunctionState *PFS) {
9718 SMLoc Loc = Lex.getLoc();
9719 if (parseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
9720 return true;
9721
9722 Value *V;
9723 SmallVector<unsigned, 16> Indexes;
9724 if (parseTypeAndValue(V, PFS) ||
9725 parseToken(lltok::comma, "expected comma in uselistorder directive") ||
9726 parseUseListOrderIndexes(Indexes))
9727 return true;
9728
9729 return sortUseListOrder(V, Indexes, Loc);
9730}
9731
9732/// ModuleEntry
9733/// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
9734/// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
9735bool LLParser::parseModuleEntry(unsigned ID) {
9736 assert(Lex.getKind() == lltok::kw_module);
9737 Lex.Lex();
9738
9739 std::string Path;
9740 if (parseToken(lltok::colon, "expected ':' here") ||
9741 parseToken(lltok::lparen, "expected '(' here") ||
9742 parseToken(lltok::kw_path, "expected 'path' here") ||
9743 parseToken(lltok::colon, "expected ':' here") ||
9744 parseStringConstant(Path) ||
9745 parseToken(lltok::comma, "expected ',' here") ||
9746 parseToken(lltok::kw_hash, "expected 'hash' here") ||
9747 parseToken(lltok::colon, "expected ':' here") ||
9748 parseToken(lltok::lparen, "expected '(' here"))
9749 return true;
9750
9751 ModuleHash Hash;
9752 if (parseUInt32(Hash[0]) || parseToken(lltok::comma, "expected ',' here") ||
9753 parseUInt32(Hash[1]) || parseToken(lltok::comma, "expected ',' here") ||
9754 parseUInt32(Hash[2]) || parseToken(lltok::comma, "expected ',' here") ||
9755 parseUInt32(Hash[3]) || parseToken(lltok::comma, "expected ',' here") ||
9756 parseUInt32(Hash[4]))
9757 return true;
9758
9759 if (parseToken(lltok::rparen, "expected ')' here") ||
9760 parseToken(lltok::rparen, "expected ')' here"))
9761 return true;
9762
9763 auto ModuleEntry = Index->addModule(Path, Hash);
9764 ModuleIdMap[ID] = ModuleEntry->first();
9765
9766 return false;
9767}
9768
9769/// TypeIdEntry
9770/// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
9771bool LLParser::parseTypeIdEntry(unsigned ID) {
9772 assert(Lex.getKind() == lltok::kw_typeid);
9773 Lex.Lex();
9774
9775 std::string Name;
9776 if (parseToken(lltok::colon, "expected ':' here") ||
9777 parseToken(lltok::lparen, "expected '(' here") ||
9778 parseToken(lltok::kw_name, "expected 'name' here") ||
9779 parseToken(lltok::colon, "expected ':' here") ||
9780 parseStringConstant(Name))
9781 return true;
9782
9783 TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(Name);
9784 if (parseToken(lltok::comma, "expected ',' here") ||
9785 parseTypeIdSummary(TIS) || parseToken(lltok::rparen, "expected ')' here"))
9786 return true;
9787
9788 // Check if this ID was forward referenced, and if so, update the
9789 // corresponding GUIDs.
9790 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
9791 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
9792 for (auto TIDRef : FwdRefTIDs->second) {
9793 assert(!*TIDRef.first &&
9794 "Forward referenced type id GUID expected to be 0");
9795 *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
9796 }
9797 ForwardRefTypeIds.erase(FwdRefTIDs);
9798 }
9799
9800 return false;
9801}
9802
9803/// TypeIdSummary
9804/// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
9805bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) {
9806 if (parseToken(lltok::kw_summary, "expected 'summary' here") ||
9807 parseToken(lltok::colon, "expected ':' here") ||
9808 parseToken(lltok::lparen, "expected '(' here") ||
9809 parseTypeTestResolution(TIS.TTRes))
9810 return true;
9811
9812 if (EatIfPresent(lltok::comma)) {
9813 // Expect optional wpdResolutions field
9814 if (parseOptionalWpdResolutions(TIS.WPDRes))
9815 return true;
9816 }
9817
9818 if (parseToken(lltok::rparen, "expected ')' here"))
9819 return true;
9820
9821 return false;
9822}
9823
9826
9827/// TypeIdCompatibleVtableEntry
9828/// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
9829/// TypeIdCompatibleVtableInfo
9830/// ')'
9831bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
9833 Lex.Lex();
9834
9835 std::string Name;
9836 if (parseToken(lltok::colon, "expected ':' here") ||
9837 parseToken(lltok::lparen, "expected '(' here") ||
9838 parseToken(lltok::kw_name, "expected 'name' here") ||
9839 parseToken(lltok::colon, "expected ':' here") ||
9840 parseStringConstant(Name))
9841 return true;
9842
9844 Index->getOrInsertTypeIdCompatibleVtableSummary(Name);
9845 if (parseToken(lltok::comma, "expected ',' here") ||
9846 parseToken(lltok::kw_summary, "expected 'summary' here") ||
9847 parseToken(lltok::colon, "expected ':' here") ||
9848 parseToken(lltok::lparen, "expected '(' here"))
9849 return true;
9850
9851 IdToIndexMapType IdToIndexMap;
9852 // parse each call edge
9853 do {
9855 if (parseToken(lltok::lparen, "expected '(' here") ||
9856 parseToken(lltok::kw_offset, "expected 'offset' here") ||
9857 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
9858 parseToken(lltok::comma, "expected ',' here"))
9859 return true;
9860
9861 LocTy Loc = Lex.getLoc();
9862 unsigned GVId;
9863 ValueInfo VI;
9864 if (parseGVReference(VI, GVId))
9865 return true;
9866
9867 // Keep track of the TypeIdCompatibleVtableInfo array index needing a
9868 // forward reference. We will save the location of the ValueInfo needing an
9869 // update, but can only do so once the std::vector is finalized.
9870 if (VI == EmptyVI)
9871 IdToIndexMap[GVId].push_back(std::make_pair(TI.size(), Loc));
9872 TI.push_back({Offset, VI});
9873
9874 if (parseToken(lltok::rparen, "expected ')' in call"))
9875 return true;
9876 } while (EatIfPresent(lltok::comma));
9877
9878 // Now that the TI vector is finalized, it is safe to save the locations
9879 // of any forward GV references that need updating later.
9880 for (auto I : IdToIndexMap) {
9881 auto &Infos = ForwardRefValueInfos[I.first];
9882 for (auto P : I.second) {
9883 assert(TI[P.first].VTableVI == EmptyVI &&
9884 "Forward referenced ValueInfo expected to be empty");
9885 Infos.emplace_back(&TI[P.first].VTableVI, P.second);
9886 }
9887 }
9888
9889 if (parseToken(lltok::rparen, "expected ')' here") ||
9890 parseToken(lltok::rparen, "expected ')' here"))
9891 return true;
9892
9893 // Check if this ID was forward referenced, and if so, update the
9894 // corresponding GUIDs.
9895 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
9896 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
9897 for (auto TIDRef : FwdRefTIDs->second) {
9898 assert(!*TIDRef.first &&
9899 "Forward referenced type id GUID expected to be 0");
9900 *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
9901 }
9902 ForwardRefTypeIds.erase(FwdRefTIDs);
9903 }
9904
9905 return false;
9906}
9907
9908/// TypeTestResolution
9909/// ::= 'typeTestRes' ':' '(' 'kind' ':'
9910/// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
9911/// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
9912/// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
9913/// [',' 'inlinesBits' ':' UInt64]? ')'
9914bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) {
9915 if (parseToken(lltok::kw_typeTestRes, "expected 'typeTestRes' here") ||
9916 parseToken(lltok::colon, "expected ':' here") ||
9917 parseToken(lltok::lparen, "expected '(' here") ||
9918 parseToken(lltok::kw_kind, "expected 'kind' here") ||
9919 parseToken(lltok::colon, "expected ':' here"))
9920 return true;
9921
9922 switch (Lex.getKind()) {
9923 case lltok::kw_unknown:
9925 break;
9926 case lltok::kw_unsat:
9928 break;
9931 break;
9932 case lltok::kw_inline:
9934 break;
9935 case lltok::kw_single:
9937 break;
9938 case lltok::kw_allOnes:
9940 break;
9941 default:
9942 return error(Lex.getLoc(), "unexpected TypeTestResolution kind");
9943 }
9944 Lex.Lex();
9945
9946 if (parseToken(lltok::comma, "expected ',' here") ||
9947 parseToken(lltok::kw_sizeM1BitWidth, "expected 'sizeM1BitWidth' here") ||
9948 parseToken(lltok::colon, "expected ':' here") ||
9949 parseUInt32(TTRes.SizeM1BitWidth))
9950 return true;
9951
9952 // parse optional fields
9953 while (EatIfPresent(lltok::comma)) {
9954 switch (Lex.getKind()) {
9956 Lex.Lex();
9957 if (parseToken(lltok::colon, "expected ':'") ||
9958 parseUInt64(TTRes.AlignLog2))
9959 return true;
9960 break;
9961 case lltok::kw_sizeM1:
9962 Lex.Lex();
9963 if (parseToken(lltok::colon, "expected ':'") || parseUInt64(TTRes.SizeM1))
9964 return true;
9965 break;
9966 case lltok::kw_bitMask: {
9967 unsigned Val;
9968 Lex.Lex();
9969 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(Val))
9970 return true;
9971 assert(Val <= 0xff);
9972 TTRes.BitMask = (uint8_t)Val;
9973 break;
9974 }
9976 Lex.Lex();
9977 if (parseToken(lltok::colon, "expected ':'") ||
9978 parseUInt64(TTRes.InlineBits))
9979 return true;
9980 break;
9981 default:
9982 return error(Lex.getLoc(), "expected optional TypeTestResolution field");
9983 }
9984 }
9985
9986 if (parseToken(lltok::rparen, "expected ')' here"))
9987 return true;
9988
9989 return false;
9990}
9991
9992/// OptionalWpdResolutions
9993/// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
9994/// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
9995bool LLParser::parseOptionalWpdResolutions(
9996 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
9997 if (parseToken(lltok::kw_wpdResolutions, "expected 'wpdResolutions' here") ||
9998 parseToken(lltok::colon, "expected ':' here") ||
9999 parseToken(lltok::lparen, "expected '(' here"))
10000 return true;
10001
10002 do {
10004 WholeProgramDevirtResolution WPDRes;
10005 if (parseToken(lltok::lparen, "expected '(' here") ||
10006 parseToken(lltok::kw_offset, "expected 'offset' here") ||
10007 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
10008 parseToken(lltok::comma, "expected ',' here") || parseWpdRes(WPDRes) ||
10009 parseToken(lltok::rparen, "expected ')' here"))
10010 return true;
10011 WPDResMap[Offset] = WPDRes;
10012 } while (EatIfPresent(lltok::comma));
10013
10014 if (parseToken(lltok::rparen, "expected ')' here"))
10015 return true;
10016
10017 return false;
10018}
10019
10020/// WpdRes
10021/// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
10022/// [',' OptionalResByArg]? ')'
10023/// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
10024/// ',' 'singleImplName' ':' STRINGCONSTANT ','
10025/// [',' OptionalResByArg]? ')'
10026/// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
10027/// [',' OptionalResByArg]? ')'
10028bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) {
10029 if (parseToken(lltok::kw_wpdRes, "expected 'wpdRes' here") ||
10030 parseToken(lltok::colon, "expected ':' here") ||
10031 parseToken(lltok::lparen, "expected '(' here") ||
10032 parseToken(lltok::kw_kind, "expected 'kind' here") ||
10033 parseToken(lltok::colon, "expected ':' here"))
10034 return true;
10035
10036 switch (Lex.getKind()) {
10037 case lltok::kw_indir:
10039 break;
10042 break;
10045 break;
10046 default:
10047 return error(Lex.getLoc(), "unexpected WholeProgramDevirtResolution kind");
10048 }
10049 Lex.Lex();
10050
10051 // parse optional fields
10052 while (EatIfPresent(lltok::comma)) {
10053 switch (Lex.getKind()) {
10055 Lex.Lex();
10056 if (parseToken(lltok::colon, "expected ':' here") ||
10057 parseStringConstant(WPDRes.SingleImplName))
10058 return true;
10059 break;
10060 case lltok::kw_resByArg:
10061 if (parseOptionalResByArg(WPDRes.ResByArg))
10062 return true;
10063 break;
10064 default:
10065 return error(Lex.getLoc(),
10066 "expected optional WholeProgramDevirtResolution field");
10067 }
10068 }
10069
10070 if (parseToken(lltok::rparen, "expected ')' here"))
10071 return true;
10072
10073 return false;
10074}
10075
10076/// OptionalResByArg
10077/// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
10078/// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
10079/// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
10080/// 'virtualConstProp' )
10081/// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
10082/// [',' 'bit' ':' UInt32]? ')'
10083bool LLParser::parseOptionalResByArg(
10084 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
10085 &ResByArg) {
10086 if (parseToken(lltok::kw_resByArg, "expected 'resByArg' here") ||
10087 parseToken(lltok::colon, "expected ':' here") ||
10088 parseToken(lltok::lparen, "expected '(' here"))
10089 return true;
10090
10091 do {
10092 std::vector<uint64_t> Args;
10093 if (parseArgs(Args) || parseToken(lltok::comma, "expected ',' here") ||
10094 parseToken(lltok::kw_byArg, "expected 'byArg here") ||
10095 parseToken(lltok::colon, "expected ':' here") ||
10096 parseToken(lltok::lparen, "expected '(' here") ||
10097 parseToken(lltok::kw_kind, "expected 'kind' here") ||
10098 parseToken(lltok::colon, "expected ':' here"))
10099 return true;
10100
10101 WholeProgramDevirtResolution::ByArg ByArg;
10102 switch (Lex.getKind()) {
10103 case lltok::kw_indir:
10105 break;
10108 break;
10111 break;
10114 break;
10115 default:
10116 return error(Lex.getLoc(),
10117 "unexpected WholeProgramDevirtResolution::ByArg kind");
10118 }
10119 Lex.Lex();
10120
10121 // parse optional fields
10122 while (EatIfPresent(lltok::comma)) {
10123 switch (Lex.getKind()) {
10124 case lltok::kw_info:
10125 Lex.Lex();
10126 if (parseToken(lltok::colon, "expected ':' here") ||
10127 parseUInt64(ByArg.Info))
10128 return true;
10129 break;
10130 case lltok::kw_byte:
10131 Lex.Lex();
10132 if (parseToken(lltok::colon, "expected ':' here") ||
10133 parseUInt32(ByArg.Byte))
10134 return true;
10135 break;
10136 case lltok::kw_bit:
10137 Lex.Lex();
10138 if (parseToken(lltok::colon, "expected ':' here") ||
10139 parseUInt32(ByArg.Bit))
10140 return true;
10141 break;
10142 default:
10143 return error(Lex.getLoc(),
10144 "expected optional whole program devirt field");
10145 }
10146 }
10147
10148 if (parseToken(lltok::rparen, "expected ')' here"))
10149 return true;
10150
10151 ResByArg[Args] = ByArg;
10152 } while (EatIfPresent(lltok::comma));
10153
10154 if (parseToken(lltok::rparen, "expected ')' here"))
10155 return true;
10156
10157 return false;
10158}
10159
10160/// OptionalResByArg
10161/// ::= 'args' ':' '(' UInt64[, UInt64]* ')'
10162bool LLParser::parseArgs(std::vector<uint64_t> &Args) {
10163 if (parseToken(lltok::kw_args, "expected 'args' here") ||
10164 parseToken(lltok::colon, "expected ':' here") ||
10165 parseToken(lltok::lparen, "expected '(' here"))
10166 return true;
10167
10168 do {
10169 uint64_t Val;
10170 if (parseUInt64(Val))
10171 return true;
10172 Args.push_back(Val);
10173 } while (EatIfPresent(lltok::comma));
10174
10175 if (parseToken(lltok::rparen, "expected ')' here"))
10176 return true;
10177
10178 return false;
10179}
10180
10182
10183static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
10184 bool ReadOnly = Fwd->isReadOnly();
10185 bool WriteOnly = Fwd->isWriteOnly();
10186 assert(!(ReadOnly && WriteOnly));
10187 *Fwd = Resolved;
10188 if (ReadOnly)
10189 Fwd->setReadOnly();
10190 if (WriteOnly)
10191 Fwd->setWriteOnly();
10192}
10193
10194/// Stores the given Name/GUID and associated summary into the Index.
10195/// Also updates any forward references to the associated entry ID.
10196bool LLParser::addGlobalValueToIndex(
10197 std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
10198 unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) {
10199 // First create the ValueInfo utilizing the Name or GUID.
10200 ValueInfo VI;
10201 if (GUID != 0) {
10202 assert(Name.empty());
10203 VI = Index->getOrInsertValueInfo(GUID);
10204 } else {
10205 assert(!Name.empty());
10206 if (M) {
10207 auto *GV = M->getNamedValue(Name);
10208 if (!GV)
10209 return error(Loc, "Reference to undefined global \"" + Name + "\"");
10210
10211 // Be a little lenient here, to accomodate older files without GUIDs
10212 // already computed and assigned as metadata.
10213 GUID = GV->getGUIDOrFallback();
10214
10215 VI = Index->getOrInsertValueInfo(GV, GUID);
10216 } else {
10217 assert(
10218 (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
10219 "Need a source_filename to compute GUID for local");
10221 GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
10222 VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
10223 }
10224 }
10225
10226 // Resolve forward references from calls/refs
10227 auto FwdRefVIs = ForwardRefValueInfos.find(ID);
10228 if (FwdRefVIs != ForwardRefValueInfos.end()) {
10229 for (auto VIRef : FwdRefVIs->second) {
10230 assert(VIRef.first->getRef() == FwdVIRef &&
10231 "Forward referenced ValueInfo expected to be empty");
10232 resolveFwdRef(VIRef.first, VI);
10233 }
10234 ForwardRefValueInfos.erase(FwdRefVIs);
10235 }
10236
10237 // Resolve forward references from aliases
10238 auto FwdRefAliasees = ForwardRefAliasees.find(ID);
10239 if (FwdRefAliasees != ForwardRefAliasees.end()) {
10240 for (auto AliaseeRef : FwdRefAliasees->second) {
10241 assert(!AliaseeRef.first->hasAliasee() &&
10242 "Forward referencing alias already has aliasee");
10243 assert(Summary && "Aliasee must be a definition");
10244 AliaseeRef.first->setAliasee(VI, Summary.get());
10245 }
10246 ForwardRefAliasees.erase(FwdRefAliasees);
10247 }
10248
10249 // Add the summary if one was provided.
10250 if (Summary)
10251 Index->addGlobalValueSummary(VI, std::move(Summary));
10252
10253 // Save the associated ValueInfo for use in later references by ID.
10254 if (ID == NumberedValueInfos.size())
10255 NumberedValueInfos.push_back(VI);
10256 else {
10257 // Handle non-continuous numbers (to make test simplification easier).
10258 if (ID > NumberedValueInfos.size())
10259 NumberedValueInfos.resize(ID + 1);
10260 NumberedValueInfos[ID] = VI;
10261 }
10262
10263 return false;
10264}
10265
10266/// parseSummaryIndexFlags
10267/// ::= 'flags' ':' UInt64
10268bool LLParser::parseSummaryIndexFlags() {
10269 assert(Lex.getKind() == lltok::kw_flags);
10270 Lex.Lex();
10271
10272 if (parseToken(lltok::colon, "expected ':' here"))
10273 return true;
10275 if (parseUInt64(Flags))
10276 return true;
10277 if (Index)
10278 Index->setFlags(Flags);
10279 return false;
10280}
10281
10282/// parseBlockCount
10283/// ::= 'blockcount' ':' UInt64
10284bool LLParser::parseBlockCount() {
10285 assert(Lex.getKind() == lltok::kw_blockcount);
10286 Lex.Lex();
10287
10288 if (parseToken(lltok::colon, "expected ':' here"))
10289 return true;
10290 uint64_t BlockCount;
10291 if (parseUInt64(BlockCount))
10292 return true;
10293 if (Index)
10294 Index->setBlockCount(BlockCount);
10295 return false;
10296}
10297
10298/// parseGVEntry
10299/// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
10300/// [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
10301/// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
10302bool LLParser::parseGVEntry(unsigned ID) {
10303 assert(Lex.getKind() == lltok::kw_gv);
10304 Lex.Lex();
10305
10306 if (parseToken(lltok::colon, "expected ':' here") ||
10307 parseToken(lltok::lparen, "expected '(' here"))
10308 return true;
10309
10310 LocTy Loc = Lex.getLoc();
10311 std::string Name;
10313 switch (Lex.getKind()) {
10314 case lltok::kw_name:
10315 Lex.Lex();
10316 if (parseToken(lltok::colon, "expected ':' here") ||
10317 parseStringConstant(Name))
10318 return true;
10319 // Can't create GUID/ValueInfo until we have the linkage.
10320 break;
10321 case lltok::kw_guid:
10322 Lex.Lex();
10323 if (parseToken(lltok::colon, "expected ':' here") || parseUInt64(GUID))
10324 return true;
10325 break;
10326 default:
10327 return error(Lex.getLoc(), "expected name or guid tag");
10328 }
10329
10330 if (!EatIfPresent(lltok::comma)) {
10331 // No summaries. Wrap up.
10332 if (parseToken(lltok::rparen, "expected ')' here"))
10333 return true;
10334 // This was created for a call to an external or indirect target.
10335 // A GUID with no summary came from a VALUE_GUID record, dummy GUID
10336 // created for indirect calls with VP. A Name with no GUID came from
10337 // an external definition. We pass ExternalLinkage since that is only
10338 // used when the GUID must be computed from Name, and in that case
10339 // the symbol must have external linkage.
10340 return addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
10341 nullptr, Loc);
10342 }
10343
10344 // Have a list of summaries
10345 if (parseToken(lltok::kw_summaries, "expected 'summaries' here") ||
10346 parseToken(lltok::colon, "expected ':' here") ||
10347 parseToken(lltok::lparen, "expected '(' here"))
10348 return true;
10349 do {
10350 switch (Lex.getKind()) {
10351 case lltok::kw_function:
10352 if (parseFunctionSummary(Name, GUID, ID))
10353 return true;
10354 break;
10355 case lltok::kw_variable:
10356 if (parseVariableSummary(Name, GUID, ID))
10357 return true;
10358 break;
10359 case lltok::kw_alias:
10360 if (parseAliasSummary(Name, GUID, ID))
10361 return true;
10362 break;
10363 default:
10364 return error(Lex.getLoc(), "expected summary type");
10365 }
10366 } while (EatIfPresent(lltok::comma));
10367
10368 if (parseToken(lltok::rparen, "expected ')' here") ||
10369 parseToken(lltok::rparen, "expected ')' here"))
10370 return true;
10371
10372 return false;
10373}
10374
10375/// FunctionSummary
10376/// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
10377/// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
10378/// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]?
10379/// [',' OptionalRefs]? ')'
10380bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
10381 unsigned ID) {
10382 LocTy Loc = Lex.getLoc();
10383 assert(Lex.getKind() == lltok::kw_function);
10384 Lex.Lex();
10385
10386 StringRef ModulePath;
10387 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10389 /*NotEligibleToImport=*/false,
10390 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10391 GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false);
10392 unsigned InstCount;
10394 FunctionSummary::TypeIdInfo TypeIdInfo;
10395 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
10397 std::vector<CallsiteInfo> Callsites;
10398 std::vector<AllocInfo> Allocs;
10399 // Default is all-zeros (conservative values).
10400 FunctionSummary::FFlags FFlags = {};
10401 if (parseToken(lltok::colon, "expected ':' here") ||
10402 parseToken(lltok::lparen, "expected '(' here") ||
10403 parseModuleReference(ModulePath) ||
10404 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
10405 parseToken(lltok::comma, "expected ',' here") ||
10406 parseToken(lltok::kw_insts, "expected 'insts' here") ||
10407 parseToken(lltok::colon, "expected ':' here") || parseUInt32(InstCount))
10408 return true;
10409
10410 // parse optional fields
10411 while (EatIfPresent(lltok::comma)) {
10412 switch (Lex.getKind()) {
10414 if (parseOptionalFFlags(FFlags))
10415 return true;
10416 break;
10417 case lltok::kw_calls:
10418 if (parseOptionalCalls(Calls))
10419 return true;
10420 break;
10422 if (parseOptionalTypeIdInfo(TypeIdInfo))
10423 return true;
10424 break;
10425 case lltok::kw_refs:
10426 if (parseOptionalRefs(Refs))
10427 return true;
10428 break;
10429 case lltok::kw_params:
10430 if (parseOptionalParamAccesses(ParamAccesses))
10431 return true;
10432 break;
10433 case lltok::kw_allocs:
10434 if (parseOptionalAllocs(Allocs))
10435 return true;
10436 break;
10438 if (parseOptionalCallsites(Callsites))
10439 return true;
10440 break;
10441 default:
10442 return error(Lex.getLoc(), "expected optional function summary field");
10443 }
10444 }
10445
10446 if (parseToken(lltok::rparen, "expected ')' here"))
10447 return true;
10448
10449 auto FS = std::make_unique<FunctionSummary>(
10450 GVFlags, InstCount, FFlags, std::move(Refs), std::move(Calls),
10451 std::move(TypeIdInfo.TypeTests),
10452 std::move(TypeIdInfo.TypeTestAssumeVCalls),
10453 std::move(TypeIdInfo.TypeCheckedLoadVCalls),
10454 std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
10455 std::move(TypeIdInfo.TypeCheckedLoadConstVCalls),
10456 std::move(ParamAccesses), std::move(Callsites), std::move(Allocs));
10457
10458 FS->setModulePath(ModulePath);
10459
10460 return addGlobalValueToIndex(Name, GUID,
10462 std::move(FS), Loc);
10463}
10464
10465/// VariableSummary
10466/// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
10467/// [',' OptionalRefs]? ')'
10468bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
10469 unsigned ID) {
10470 LocTy Loc = Lex.getLoc();
10471 assert(Lex.getKind() == lltok::kw_variable);
10472 Lex.Lex();
10473
10474 StringRef ModulePath;
10475 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10477 /*NotEligibleToImport=*/false,
10478 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10479 GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false);
10480 GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
10481 /* WriteOnly */ false,
10482 /* Constant */ false,
10485 VTableFuncList VTableFuncs;
10486 if (parseToken(lltok::colon, "expected ':' here") ||
10487 parseToken(lltok::lparen, "expected '(' here") ||
10488 parseModuleReference(ModulePath) ||
10489 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
10490 parseToken(lltok::comma, "expected ',' here") ||
10491 parseGVarFlags(GVarFlags))
10492 return true;
10493
10494 // parse optional fields
10495 while (EatIfPresent(lltok::comma)) {
10496 switch (Lex.getKind()) {
10498 if (parseOptionalVTableFuncs(VTableFuncs))
10499 return true;
10500 break;
10501 case lltok::kw_refs:
10502 if (parseOptionalRefs(Refs))
10503 return true;
10504 break;
10505 default:
10506 return error(Lex.getLoc(), "expected optional variable summary field");
10507 }
10508 }
10509
10510 if (parseToken(lltok::rparen, "expected ')' here"))
10511 return true;
10512
10513 auto GS =
10514 std::make_unique<GlobalVarSummary>(GVFlags, GVarFlags, std::move(Refs));
10515
10516 GS->setModulePath(ModulePath);
10517 GS->setVTableFuncs(std::move(VTableFuncs));
10518
10519 return addGlobalValueToIndex(Name, GUID,
10521 std::move(GS), Loc);
10522}
10523
10524/// AliasSummary
10525/// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
10526/// 'aliasee' ':' GVReference ')'
10527bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
10528 unsigned ID) {
10529 assert(Lex.getKind() == lltok::kw_alias);
10530 LocTy Loc = Lex.getLoc();
10531 Lex.Lex();
10532
10533 StringRef ModulePath;
10534 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10536 /*NotEligibleToImport=*/false,
10537 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10538 GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false);
10539 if (parseToken(lltok::colon, "expected ':' here") ||
10540 parseToken(lltok::lparen, "expected '(' here") ||
10541 parseModuleReference(ModulePath) ||
10542 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
10543 parseToken(lltok::comma, "expected ',' here") ||
10544 parseToken(lltok::kw_aliasee, "expected 'aliasee' here") ||
10545 parseToken(lltok::colon, "expected ':' here"))
10546 return true;
10547
10548 ValueInfo AliaseeVI;
10549 unsigned GVId;
10550 auto AS = std::make_unique<AliasSummary>(GVFlags);
10551 AS->setModulePath(ModulePath);
10552
10553 if (!EatIfPresent(lltok::kw_null)) {
10554 if (parseGVReference(AliaseeVI, GVId))
10555 return true;
10556
10557 // Record forward reference if the aliasee is not parsed yet.
10558 if (AliaseeVI.getRef() == FwdVIRef) {
10559 ForwardRefAliasees[GVId].emplace_back(AS.get(), Loc);
10560 } else {
10561 auto Summary = Index->findSummaryInModule(AliaseeVI, ModulePath);
10562 assert(Summary && "Aliasee must be a definition");
10563 AS->setAliasee(AliaseeVI, Summary);
10564 }
10565 }
10566
10567 if (parseToken(lltok::rparen, "expected ')' here"))
10568 return true;
10569
10570 return addGlobalValueToIndex(Name, GUID,
10572 std::move(AS), Loc);
10573}
10574
10575/// Flag
10576/// ::= [0|1]
10577bool LLParser::parseFlag(unsigned &Val) {
10578 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
10579 return tokError("expected integer");
10580 Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
10581 Lex.Lex();
10582 return false;
10583}
10584
10585/// OptionalFFlags
10586/// := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
10587/// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
10588/// [',' 'returnDoesNotAlias' ':' Flag]? ')'
10589/// [',' 'noInline' ':' Flag]? ')'
10590/// [',' 'alwaysInline' ':' Flag]? ')'
10591/// [',' 'noUnwind' ':' Flag]? ')'
10592/// [',' 'mayThrow' ':' Flag]? ')'
10593/// [',' 'hasUnknownCall' ':' Flag]? ')'
10594/// [',' 'mustBeUnreachable' ':' Flag]? ')'
10595
10596bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
10597 assert(Lex.getKind() == lltok::kw_funcFlags);
10598 Lex.Lex();
10599
10600 if (parseToken(lltok::colon, "expected ':' in funcFlags") ||
10601 parseToken(lltok::lparen, "expected '(' in funcFlags"))
10602 return true;
10603
10604 do {
10605 unsigned Val = 0;
10606 switch (Lex.getKind()) {
10607 case lltok::kw_readNone:
10608 Lex.Lex();
10609 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10610 return true;
10611 FFlags.ReadNone = Val;
10612 break;
10613 case lltok::kw_readOnly:
10614 Lex.Lex();
10615 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10616 return true;
10617 FFlags.ReadOnly = Val;
10618 break;
10620 Lex.Lex();
10621 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10622 return true;
10623 FFlags.NoRecurse = Val;
10624 break;
10626 Lex.Lex();
10627 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10628 return true;
10629 FFlags.ReturnDoesNotAlias = Val;
10630 break;
10631 case lltok::kw_noInline:
10632 Lex.Lex();
10633 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10634 return true;
10635 FFlags.NoInline = Val;
10636 break;
10638 Lex.Lex();
10639 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10640 return true;
10641 FFlags.AlwaysInline = Val;
10642 break;
10643 case lltok::kw_noUnwind:
10644 Lex.Lex();
10645 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10646 return true;
10647 FFlags.NoUnwind = Val;
10648 break;
10649 case lltok::kw_mayThrow:
10650 Lex.Lex();
10651 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10652 return true;
10653 FFlags.MayThrow = Val;
10654 break;
10656 Lex.Lex();
10657 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10658 return true;
10659 FFlags.HasUnknownCall = Val;
10660 break;
10662 Lex.Lex();
10663 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10664 return true;
10665 FFlags.MustBeUnreachable = Val;
10666 break;
10667 default:
10668 return error(Lex.getLoc(), "expected function flag type");
10669 }
10670 } while (EatIfPresent(lltok::comma));
10671
10672 if (parseToken(lltok::rparen, "expected ')' in funcFlags"))
10673 return true;
10674
10675 return false;
10676}
10677
10678/// OptionalCalls
10679/// := 'calls' ':' '(' Call [',' Call]* ')'
10680/// Call ::= '(' 'callee' ':' GVReference
10681/// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]?
10682/// [ ',' 'tail' ]? ')'
10683bool LLParser::parseOptionalCalls(
10684 SmallVectorImpl<FunctionSummary::EdgeTy> &Calls) {
10685 assert(Lex.getKind() == lltok::kw_calls);
10686 Lex.Lex();
10687
10688 if (parseToken(lltok::colon, "expected ':' in calls") ||
10689 parseToken(lltok::lparen, "expected '(' in calls"))
10690 return true;
10691
10692 IdToIndexMapType IdToIndexMap;
10693 // parse each call edge
10694 do {
10695 ValueInfo VI;
10696 if (parseToken(lltok::lparen, "expected '(' in call") ||
10697 parseToken(lltok::kw_callee, "expected 'callee' in call") ||
10698 parseToken(lltok::colon, "expected ':'"))
10699 return true;
10700
10701 LocTy Loc = Lex.getLoc();
10702 unsigned GVId;
10703 if (parseGVReference(VI, GVId))
10704 return true;
10705
10707 unsigned RelBF = 0;
10708 unsigned HasTailCall = false;
10709
10710 // parse optional fields
10711 while (EatIfPresent(lltok::comma)) {
10712 switch (Lex.getKind()) {
10713 case lltok::kw_hotness:
10714 Lex.Lex();
10715 if (parseToken(lltok::colon, "expected ':'") || parseHotness(Hotness))
10716 return true;
10717 break;
10718 // Deprecated, keep in order to support old files.
10719 case lltok::kw_relbf:
10720 Lex.Lex();
10721 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(RelBF))
10722 return true;
10723 break;
10724 case lltok::kw_tail:
10725 Lex.Lex();
10726 if (parseToken(lltok::colon, "expected ':'") || parseFlag(HasTailCall))
10727 return true;
10728 break;
10729 default:
10730 return error(Lex.getLoc(), "expected hotness, relbf, or tail");
10731 }
10732 }
10733 // Keep track of the Call array index needing a forward reference.
10734 // We will save the location of the ValueInfo needing an update, but
10735 // can only do so once the std::vector is finalized.
10736 if (VI.getRef() == FwdVIRef)
10737 IdToIndexMap[GVId].push_back(std::make_pair(Calls.size(), Loc));
10738 Calls.push_back(
10739 FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, HasTailCall)});
10740
10741 if (parseToken(lltok::rparen, "expected ')' in call"))
10742 return true;
10743 } while (EatIfPresent(lltok::comma));
10744
10745 // Now that the Calls vector is finalized, it is safe to save the locations
10746 // of any forward GV references that need updating later.
10747 for (auto I : IdToIndexMap) {
10748 auto &Infos = ForwardRefValueInfos[I.first];
10749 for (auto P : I.second) {
10750 assert(Calls[P.first].first.getRef() == FwdVIRef &&
10751 "Forward referenced ValueInfo expected to be empty");
10752 Infos.emplace_back(&Calls[P.first].first, P.second);
10753 }
10754 }
10755
10756 if (parseToken(lltok::rparen, "expected ')' in calls"))
10757 return true;
10758
10759 return false;
10760}
10761
10762/// Hotness
10763/// := ('unknown'|'cold'|'none'|'hot'|'critical')
10764bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) {
10765 switch (Lex.getKind()) {
10766 case lltok::kw_unknown:
10768 break;
10769 case lltok::kw_cold:
10771 break;
10772 case lltok::kw_none:
10774 break;
10775 case lltok::kw_hot:
10777 break;
10778 case lltok::kw_critical:
10780 break;
10781 default:
10782 return error(Lex.getLoc(), "invalid call edge hotness");
10783 }
10784 Lex.Lex();
10785 return false;
10786}
10787
10788/// OptionalVTableFuncs
10789/// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')'
10790/// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')'
10791bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) {
10792 assert(Lex.getKind() == lltok::kw_vTableFuncs);
10793 Lex.Lex();
10794
10795 if (parseToken(lltok::colon, "expected ':' in vTableFuncs") ||
10796 parseToken(lltok::lparen, "expected '(' in vTableFuncs"))
10797 return true;
10798
10799 IdToIndexMapType IdToIndexMap;
10800 // parse each virtual function pair
10801 do {
10802 ValueInfo VI;
10803 if (parseToken(lltok::lparen, "expected '(' in vTableFunc") ||
10804 parseToken(lltok::kw_virtFunc, "expected 'callee' in vTableFunc") ||
10805 parseToken(lltok::colon, "expected ':'"))
10806 return true;
10807
10808 LocTy Loc = Lex.getLoc();
10809 unsigned GVId;
10810 if (parseGVReference(VI, GVId))
10811 return true;
10812
10814 if (parseToken(lltok::comma, "expected comma") ||
10815 parseToken(lltok::kw_offset, "expected offset") ||
10816 parseToken(lltok::colon, "expected ':'") || parseUInt64(Offset))
10817 return true;
10818
10819 // Keep track of the VTableFuncs array index needing a forward reference.
10820 // We will save the location of the ValueInfo needing an update, but
10821 // can only do so once the std::vector is finalized.
10822 if (VI == EmptyVI)
10823 IdToIndexMap[GVId].push_back(std::make_pair(VTableFuncs.size(), Loc));
10824 VTableFuncs.push_back({VI, Offset});
10825
10826 if (parseToken(lltok::rparen, "expected ')' in vTableFunc"))
10827 return true;
10828 } while (EatIfPresent(lltok::comma));
10829
10830 // Now that the VTableFuncs vector is finalized, it is safe to save the
10831 // locations of any forward GV references that need updating later.
10832 for (auto I : IdToIndexMap) {
10833 auto &Infos = ForwardRefValueInfos[I.first];
10834 for (auto P : I.second) {
10835 assert(VTableFuncs[P.first].FuncVI == EmptyVI &&
10836 "Forward referenced ValueInfo expected to be empty");
10837 Infos.emplace_back(&VTableFuncs[P.first].FuncVI, P.second);
10838 }
10839 }
10840
10841 if (parseToken(lltok::rparen, "expected ')' in vTableFuncs"))
10842 return true;
10843
10844 return false;
10845}
10846
10847/// ParamNo := 'param' ':' UInt64
10848bool LLParser::parseParamNo(uint64_t &ParamNo) {
10849 if (parseToken(lltok::kw_param, "expected 'param' here") ||
10850 parseToken(lltok::colon, "expected ':' here") || parseUInt64(ParamNo))
10851 return true;
10852 return false;
10853}
10854
10855/// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']'
10856bool LLParser::parseParamAccessOffset(ConstantRange &Range) {
10857 APSInt Lower;
10858 APSInt Upper;
10859 auto ParseAPSInt = [&](APSInt &Val) {
10860 if (Lex.getKind() != lltok::APSInt)
10861 return tokError("expected integer");
10862 Val = Lex.getAPSIntVal();
10863 Val = Val.extOrTrunc(FunctionSummary::ParamAccess::RangeWidth);
10864 Val.setIsSigned(true);
10865 Lex.Lex();
10866 return false;
10867 };
10868 if (parseToken(lltok::kw_offset, "expected 'offset' here") ||
10869 parseToken(lltok::colon, "expected ':' here") ||
10870 parseToken(lltok::lsquare, "expected '[' here") || ParseAPSInt(Lower) ||
10871 parseToken(lltok::comma, "expected ',' here") || ParseAPSInt(Upper) ||
10872 parseToken(lltok::rsquare, "expected ']' here"))
10873 return true;
10874
10875 ++Upper;
10876 Range =
10877 (Lower == Upper && !Lower.isMaxValue())
10878 ? ConstantRange::getEmpty(FunctionSummary::ParamAccess::RangeWidth)
10879 : ConstantRange(Lower, Upper);
10880
10881 return false;
10882}
10883
10884/// ParamAccessCall
10885/// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')'
10886bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
10887 IdLocListType &IdLocList) {
10888 if (parseToken(lltok::lparen, "expected '(' here") ||
10889 parseToken(lltok::kw_callee, "expected 'callee' here") ||
10890 parseToken(lltok::colon, "expected ':' here"))
10891 return true;
10892
10893 unsigned GVId;
10894 ValueInfo VI;
10895 LocTy Loc = Lex.getLoc();
10896 if (parseGVReference(VI, GVId))
10897 return true;
10898
10899 Call.Callee = VI;
10900 IdLocList.emplace_back(GVId, Loc);
10901
10902 if (parseToken(lltok::comma, "expected ',' here") ||
10903 parseParamNo(Call.ParamNo) ||
10904 parseToken(lltok::comma, "expected ',' here") ||
10905 parseParamAccessOffset(Call.Offsets))
10906 return true;
10907
10908 if (parseToken(lltok::rparen, "expected ')' here"))
10909 return true;
10910
10911 return false;
10912}
10913
10914/// ParamAccess
10915/// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')'
10916/// OptionalParamAccessCalls := '(' Call [',' Call]* ')'
10917bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param,
10918 IdLocListType &IdLocList) {
10919 if (parseToken(lltok::lparen, "expected '(' here") ||
10920 parseParamNo(Param.ParamNo) ||
10921 parseToken(lltok::comma, "expected ',' here") ||
10922 parseParamAccessOffset(Param.Use))
10923 return true;
10924
10925 if (EatIfPresent(lltok::comma)) {
10926 if (parseToken(lltok::kw_calls, "expected 'calls' here") ||
10927 parseToken(lltok::colon, "expected ':' here") ||
10928 parseToken(lltok::lparen, "expected '(' here"))
10929 return true;
10930 do {
10931 FunctionSummary::ParamAccess::Call Call;
10932 if (parseParamAccessCall(Call, IdLocList))
10933 return true;
10934 Param.Calls.push_back(Call);
10935 } while (EatIfPresent(lltok::comma));
10936
10937 if (parseToken(lltok::rparen, "expected ')' here"))
10938 return true;
10939 }
10940
10941 if (parseToken(lltok::rparen, "expected ')' here"))
10942 return true;
10943
10944 return false;
10945}
10946
10947/// OptionalParamAccesses
10948/// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')'
10949bool LLParser::parseOptionalParamAccesses(
10950 std::vector<FunctionSummary::ParamAccess> &Params) {
10951 assert(Lex.getKind() == lltok::kw_params);
10952 Lex.Lex();
10953
10954 if (parseToken(lltok::colon, "expected ':' here") ||
10955 parseToken(lltok::lparen, "expected '(' here"))
10956 return true;
10957
10958 IdLocListType VContexts;
10959 size_t CallsNum = 0;
10960 do {
10961 FunctionSummary::ParamAccess ParamAccess;
10962 if (parseParamAccess(ParamAccess, VContexts))
10963 return true;
10964 CallsNum += ParamAccess.Calls.size();
10965 assert(VContexts.size() == CallsNum);
10966 (void)CallsNum;
10967 Params.emplace_back(std::move(ParamAccess));
10968 } while (EatIfPresent(lltok::comma));
10969
10970 if (parseToken(lltok::rparen, "expected ')' here"))
10971 return true;
10972
10973 // Now that the Params is finalized, it is safe to save the locations
10974 // of any forward GV references that need updating later.
10975 IdLocListType::const_iterator ItContext = VContexts.begin();
10976 for (auto &PA : Params) {
10977 for (auto &C : PA.Calls) {
10978 if (C.Callee.getRef() == FwdVIRef)
10979 ForwardRefValueInfos[ItContext->first].emplace_back(&C.Callee,
10980 ItContext->second);
10981 ++ItContext;
10982 }
10983 }
10984 assert(ItContext == VContexts.end());
10985
10986 return false;
10987}
10988
10989/// OptionalRefs
10990/// := 'refs' ':' '(' GVReference [',' GVReference]* ')'
10991bool LLParser::parseOptionalRefs(SmallVectorImpl<ValueInfo> &Refs) {
10992 assert(Lex.getKind() == lltok::kw_refs);
10993 Lex.Lex();
10994
10995 if (parseToken(lltok::colon, "expected ':' in refs") ||
10996 parseToken(lltok::lparen, "expected '(' in refs"))
10997 return true;
10998
10999 struct ValueContext {
11000 ValueInfo VI;
11001 unsigned GVId;
11002 LocTy Loc;
11003 };
11004 std::vector<ValueContext> VContexts;
11005 // parse each ref edge
11006 do {
11007 ValueContext VC;
11008 VC.Loc = Lex.getLoc();
11009 if (parseGVReference(VC.VI, VC.GVId))
11010 return true;
11011 VContexts.push_back(VC);
11012 } while (EatIfPresent(lltok::comma));
11013
11014 // Sort value contexts so that ones with writeonly
11015 // and readonly ValueInfo are at the end of VContexts vector.
11016 // See FunctionSummary::specialRefCounts()
11017 llvm::sort(VContexts, [](const ValueContext &VC1, const ValueContext &VC2) {
11018 return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier();
11019 });
11020
11021 IdToIndexMapType IdToIndexMap;
11022 for (auto &VC : VContexts) {
11023 // Keep track of the Refs array index needing a forward reference.
11024 // We will save the location of the ValueInfo needing an update, but
11025 // can only do so once the std::vector is finalized.
11026 if (VC.VI.getRef() == FwdVIRef)
11027 IdToIndexMap[VC.GVId].push_back(std::make_pair(Refs.size(), VC.Loc));
11028 Refs.push_back(VC.VI);
11029 }
11030
11031 // Now that the Refs vector is finalized, it is safe to save the locations
11032 // of any forward GV references that need updating later.
11033 for (auto I : IdToIndexMap) {
11034 auto &Infos = ForwardRefValueInfos[I.first];
11035 for (auto P : I.second) {
11036 assert(Refs[P.first].getRef() == FwdVIRef &&
11037 "Forward referenced ValueInfo expected to be empty");
11038 Infos.emplace_back(&Refs[P.first], P.second);
11039 }
11040 }
11041
11042 if (parseToken(lltok::rparen, "expected ')' in refs"))
11043 return true;
11044
11045 return false;
11046}
11047
11048/// OptionalTypeIdInfo
11049/// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
11050/// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]?
11051/// [',' TypeCheckedLoadConstVCalls]? ')'
11052bool LLParser::parseOptionalTypeIdInfo(
11053 FunctionSummary::TypeIdInfo &TypeIdInfo) {
11054 assert(Lex.getKind() == lltok::kw_typeIdInfo);
11055 Lex.Lex();
11056
11057 if (parseToken(lltok::colon, "expected ':' here") ||
11058 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
11059 return true;
11060
11061 do {
11062 switch (Lex.getKind()) {
11064 if (parseTypeTests(TypeIdInfo.TypeTests))
11065 return true;
11066 break;
11068 if (parseVFuncIdList(lltok::kw_typeTestAssumeVCalls,
11069 TypeIdInfo.TypeTestAssumeVCalls))
11070 return true;
11071 break;
11073 if (parseVFuncIdList(lltok::kw_typeCheckedLoadVCalls,
11074 TypeIdInfo.TypeCheckedLoadVCalls))
11075 return true;
11076 break;
11078 if (parseConstVCallList(lltok::kw_typeTestAssumeConstVCalls,
11079 TypeIdInfo.TypeTestAssumeConstVCalls))
11080 return true;
11081 break;
11083 if (parseConstVCallList(lltok::kw_typeCheckedLoadConstVCalls,
11084 TypeIdInfo.TypeCheckedLoadConstVCalls))
11085 return true;
11086 break;
11087 default:
11088 return error(Lex.getLoc(), "invalid typeIdInfo list type");
11089 }
11090 } while (EatIfPresent(lltok::comma));
11091
11092 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
11093 return true;
11094
11095 return false;
11096}
11097
11098/// TypeTests
11099/// ::= 'typeTests' ':' '(' (SummaryID | UInt64)
11100/// [',' (SummaryID | UInt64)]* ')'
11101bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
11102 assert(Lex.getKind() == lltok::kw_typeTests);
11103 Lex.Lex();
11104
11105 if (parseToken(lltok::colon, "expected ':' here") ||
11106 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
11107 return true;
11108
11109 IdToIndexMapType IdToIndexMap;
11110 do {
11112 if (Lex.getKind() == lltok::SummaryID) {
11113 unsigned ID = Lex.getUIntVal();
11114 LocTy Loc = Lex.getLoc();
11115 // Keep track of the TypeTests array index needing a forward reference.
11116 // We will save the location of the GUID needing an update, but
11117 // can only do so once the std::vector is finalized.
11118 IdToIndexMap[ID].push_back(std::make_pair(TypeTests.size(), Loc));
11119 Lex.Lex();
11120 } else if (parseUInt64(GUID))
11121 return true;
11122 TypeTests.push_back(GUID);
11123 } while (EatIfPresent(lltok::comma));
11124
11125 // Now that the TypeTests vector is finalized, it is safe to save the
11126 // locations of any forward GV references that need updating later.
11127 for (auto I : IdToIndexMap) {
11128 auto &Ids = ForwardRefTypeIds[I.first];
11129 for (auto P : I.second) {
11130 assert(TypeTests[P.first] == 0 &&
11131 "Forward referenced type id GUID expected to be 0");
11132 Ids.emplace_back(&TypeTests[P.first], P.second);
11133 }
11134 }
11135
11136 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
11137 return true;
11138
11139 return false;
11140}
11141
11142/// VFuncIdList
11143/// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
11144bool LLParser::parseVFuncIdList(
11145 lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
11146 assert(Lex.getKind() == Kind);
11147 Lex.Lex();
11148
11149 if (parseToken(lltok::colon, "expected ':' here") ||
11150 parseToken(lltok::lparen, "expected '(' here"))
11151 return true;
11152
11153 IdToIndexMapType IdToIndexMap;
11154 do {
11155 FunctionSummary::VFuncId VFuncId;
11156 if (parseVFuncId(VFuncId, IdToIndexMap, VFuncIdList.size()))
11157 return true;
11158 VFuncIdList.push_back(VFuncId);
11159 } while (EatIfPresent(lltok::comma));
11160
11161 if (parseToken(lltok::rparen, "expected ')' here"))
11162 return true;
11163
11164 // Now that the VFuncIdList vector is finalized, it is safe to save the
11165 // locations of any forward GV references that need updating later.
11166 for (auto I : IdToIndexMap) {
11167 auto &Ids = ForwardRefTypeIds[I.first];
11168 for (auto P : I.second) {
11169 assert(VFuncIdList[P.first].GUID == 0 &&
11170 "Forward referenced type id GUID expected to be 0");
11171 Ids.emplace_back(&VFuncIdList[P.first].GUID, P.second);
11172 }
11173 }
11174
11175 return false;
11176}
11177
11178/// ConstVCallList
11179/// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
11180bool LLParser::parseConstVCallList(
11181 lltok::Kind Kind,
11182 std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
11183 assert(Lex.getKind() == Kind);
11184 Lex.Lex();
11185
11186 if (parseToken(lltok::colon, "expected ':' here") ||
11187 parseToken(lltok::lparen, "expected '(' here"))
11188 return true;
11189
11190 IdToIndexMapType IdToIndexMap;
11191 do {
11192 FunctionSummary::ConstVCall ConstVCall;
11193 if (parseConstVCall(ConstVCall, IdToIndexMap, ConstVCallList.size()))
11194 return true;
11195 ConstVCallList.push_back(ConstVCall);
11196 } while (EatIfPresent(lltok::comma));
11197
11198 if (parseToken(lltok::rparen, "expected ')' here"))
11199 return true;
11200
11201 // Now that the ConstVCallList vector is finalized, it is safe to save the
11202 // locations of any forward GV references that need updating later.
11203 for (auto I : IdToIndexMap) {
11204 auto &Ids = ForwardRefTypeIds[I.first];
11205 for (auto P : I.second) {
11206 assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
11207 "Forward referenced type id GUID expected to be 0");
11208 Ids.emplace_back(&ConstVCallList[P.first].VFunc.GUID, P.second);
11209 }
11210 }
11211
11212 return false;
11213}
11214
11215/// ConstVCall
11216/// ::= '(' VFuncId ',' Args ')'
11217bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
11218 IdToIndexMapType &IdToIndexMap, unsigned Index) {
11219 if (parseToken(lltok::lparen, "expected '(' here") ||
11220 parseVFuncId(ConstVCall.VFunc, IdToIndexMap, Index))
11221 return true;
11222
11223 if (EatIfPresent(lltok::comma))
11224 if (parseArgs(ConstVCall.Args))
11225 return true;
11226
11227 if (parseToken(lltok::rparen, "expected ')' here"))
11228 return true;
11229
11230 return false;
11231}
11232
11233/// VFuncId
11234/// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
11235/// 'offset' ':' UInt64 ')'
11236bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId,
11237 IdToIndexMapType &IdToIndexMap, unsigned Index) {
11238 assert(Lex.getKind() == lltok::kw_vFuncId);
11239 Lex.Lex();
11240
11241 if (parseToken(lltok::colon, "expected ':' here") ||
11242 parseToken(lltok::lparen, "expected '(' here"))
11243 return true;
11244
11245 if (Lex.getKind() == lltok::SummaryID) {
11246 VFuncId.GUID = 0;
11247 unsigned ID = Lex.getUIntVal();
11248 LocTy Loc = Lex.getLoc();
11249 // Keep track of the array index needing a forward reference.
11250 // We will save the location of the GUID needing an update, but
11251 // can only do so once the caller's std::vector is finalized.
11252 IdToIndexMap[ID].push_back(std::make_pair(Index, Loc));
11253 Lex.Lex();
11254 } else if (parseToken(lltok::kw_guid, "expected 'guid' here") ||
11255 parseToken(lltok::colon, "expected ':' here") ||
11256 parseUInt64(VFuncId.GUID))
11257 return true;
11258
11259 if (parseToken(lltok::comma, "expected ',' here") ||
11260 parseToken(lltok::kw_offset, "expected 'offset' here") ||
11261 parseToken(lltok::colon, "expected ':' here") ||
11262 parseUInt64(VFuncId.Offset) ||
11263 parseToken(lltok::rparen, "expected ')' here"))
11264 return true;
11265
11266 return false;
11267}
11268
11269/// GVFlags
11270/// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
11271/// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ','
11272/// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ','
11273/// 'canAutoHide' ':' Flag ',' ')'
11274bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
11275 assert(Lex.getKind() == lltok::kw_flags);
11276 Lex.Lex();
11277
11278 if (parseToken(lltok::colon, "expected ':' here") ||
11279 parseToken(lltok::lparen, "expected '(' here"))
11280 return true;
11281
11282 do {
11283 unsigned Flag = 0;
11284 switch (Lex.getKind()) {
11285 case lltok::kw_linkage:
11286 Lex.Lex();
11287 if (parseToken(lltok::colon, "expected ':'"))
11288 return true;
11289 bool HasLinkage;
11290 GVFlags.Linkage = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
11291 assert(HasLinkage && "Linkage not optional in summary entry");
11292 Lex.Lex();
11293 break;
11295 Lex.Lex();
11296 if (parseToken(lltok::colon, "expected ':'"))
11297 return true;
11298 parseOptionalVisibility(Flag);
11299 GVFlags.Visibility = Flag;
11300 break;
11302 Lex.Lex();
11303 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
11304 return true;
11305 GVFlags.NotEligibleToImport = Flag;
11306 break;
11307 case lltok::kw_live:
11308 Lex.Lex();
11309 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
11310 return true;
11311 GVFlags.Live = Flag;
11312 break;
11313 case lltok::kw_dsoLocal:
11314 Lex.Lex();
11315 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
11316 return true;
11317 GVFlags.DSOLocal = Flag;
11318 break;
11320 Lex.Lex();
11321 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
11322 return true;
11323 GVFlags.CanAutoHide = Flag;
11324 break;
11326 Lex.Lex();
11327 if (parseToken(lltok::colon, "expected ':'"))
11328 return true;
11330 if (parseOptionalImportType(Lex.getKind(), IK))
11331 return true;
11332 GVFlags.ImportType = static_cast<unsigned>(IK);
11333 Lex.Lex();
11334 break;
11336 Lex.Lex();
11337 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
11338 return true;
11339 GVFlags.NoRenameOnPromotion = Flag;
11340 break;
11341 default:
11342 return error(Lex.getLoc(), "expected gv flag type");
11343 }
11344 } while (EatIfPresent(lltok::comma));
11345
11346 if (parseToken(lltok::rparen, "expected ')' here"))
11347 return true;
11348
11349 return false;
11350}
11351
11352/// GVarFlags
11353/// ::= 'varFlags' ':' '(' 'readonly' ':' Flag
11354/// ',' 'writeonly' ':' Flag
11355/// ',' 'constant' ':' Flag ')'
11356bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) {
11357 assert(Lex.getKind() == lltok::kw_varFlags);
11358 Lex.Lex();
11359
11360 if (parseToken(lltok::colon, "expected ':' here") ||
11361 parseToken(lltok::lparen, "expected '(' here"))
11362 return true;
11363
11364 auto ParseRest = [this](unsigned int &Val) {
11365 Lex.Lex();
11366 if (parseToken(lltok::colon, "expected ':'"))
11367 return true;
11368 return parseFlag(Val);
11369 };
11370
11371 do {
11372 unsigned Flag = 0;
11373 switch (Lex.getKind()) {
11374 case lltok::kw_readonly:
11375 if (ParseRest(Flag))
11376 return true;
11377 GVarFlags.MaybeReadOnly = Flag;
11378 break;
11379 case lltok::kw_writeonly:
11380 if (ParseRest(Flag))
11381 return true;
11382 GVarFlags.MaybeWriteOnly = Flag;
11383 break;
11384 case lltok::kw_constant:
11385 if (ParseRest(Flag))
11386 return true;
11387 GVarFlags.Constant = Flag;
11388 break;
11390 if (ParseRest(Flag))
11391 return true;
11392 GVarFlags.VCallVisibility = Flag;
11393 break;
11394 default:
11395 return error(Lex.getLoc(), "expected gvar flag type");
11396 }
11397 } while (EatIfPresent(lltok::comma));
11398 return parseToken(lltok::rparen, "expected ')' here");
11399}
11400
11401/// ModuleReference
11402/// ::= 'module' ':' UInt
11403bool LLParser::parseModuleReference(StringRef &ModulePath) {
11404 // parse module id.
11405 if (parseToken(lltok::kw_module, "expected 'module' here") ||
11406 parseToken(lltok::colon, "expected ':' here") ||
11407 parseToken(lltok::SummaryID, "expected module ID"))
11408 return true;
11409
11410 unsigned ModuleID = Lex.getUIntVal();
11411 auto I = ModuleIdMap.find(ModuleID);
11412 // We should have already parsed all module IDs
11413 assert(I != ModuleIdMap.end());
11414 ModulePath = I->second;
11415 return false;
11416}
11417
11418/// GVReference
11419/// ::= SummaryID
11420bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) {
11421 bool WriteOnly = false, ReadOnly = EatIfPresent(lltok::kw_readonly);
11422 if (!ReadOnly)
11423 WriteOnly = EatIfPresent(lltok::kw_writeonly);
11424 if (parseToken(lltok::SummaryID, "expected GV ID"))
11425 return true;
11426
11427 GVId = Lex.getUIntVal();
11428 // Check if we already have a VI for this GV
11429 if (GVId < NumberedValueInfos.size() && NumberedValueInfos[GVId]) {
11430 assert(NumberedValueInfos[GVId].getRef() != FwdVIRef);
11431 VI = NumberedValueInfos[GVId];
11432 } else
11433 // We will create a forward reference to the stored location.
11434 VI = ValueInfo(false, FwdVIRef);
11435
11436 if (ReadOnly)
11437 VI.setReadOnly();
11438 if (WriteOnly)
11439 VI.setWriteOnly();
11440 return false;
11441}
11442
11443/// OptionalAllocs
11444/// := 'allocs' ':' '(' Alloc [',' Alloc]* ')'
11445/// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')'
11446/// ',' MemProfs ')'
11447/// Version ::= UInt32
11448bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) {
11449 assert(Lex.getKind() == lltok::kw_allocs);
11450 Lex.Lex();
11451
11452 if (parseToken(lltok::colon, "expected ':' in allocs") ||
11453 parseToken(lltok::lparen, "expected '(' in allocs"))
11454 return true;
11455
11456 // parse each alloc
11457 do {
11458 if (parseToken(lltok::lparen, "expected '(' in alloc") ||
11459 parseToken(lltok::kw_versions, "expected 'versions' in alloc") ||
11460 parseToken(lltok::colon, "expected ':'") ||
11461 parseToken(lltok::lparen, "expected '(' in versions"))
11462 return true;
11463
11464 SmallVector<uint8_t> Versions;
11465 do {
11466 uint8_t V = 0;
11467 if (parseAllocType(V))
11468 return true;
11469 Versions.push_back(V);
11470 } while (EatIfPresent(lltok::comma));
11471
11472 if (parseToken(lltok::rparen, "expected ')' in versions") ||
11473 parseToken(lltok::comma, "expected ',' in alloc"))
11474 return true;
11475
11476 std::vector<MIBInfo> MIBs;
11477 if (parseMemProfs(MIBs))
11478 return true;
11479
11480 Allocs.push_back({Versions, MIBs});
11481
11482 if (parseToken(lltok::rparen, "expected ')' in alloc"))
11483 return true;
11484 } while (EatIfPresent(lltok::comma));
11485
11486 if (parseToken(lltok::rparen, "expected ')' in allocs"))
11487 return true;
11488
11489 return false;
11490}
11491
11492/// MemProfs
11493/// := 'memProf' ':' '(' MemProf [',' MemProf]* ')'
11494/// MemProf ::= '(' 'type' ':' AllocType
11495/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
11496/// StackId ::= UInt64
11497bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) {
11498 assert(Lex.getKind() == lltok::kw_memProf);
11499 Lex.Lex();
11500
11501 if (parseToken(lltok::colon, "expected ':' in memprof") ||
11502 parseToken(lltok::lparen, "expected '(' in memprof"))
11503 return true;
11504
11505 // parse each MIB
11506 do {
11507 if (parseToken(lltok::lparen, "expected '(' in memprof") ||
11508 parseToken(lltok::kw_type, "expected 'type' in memprof") ||
11509 parseToken(lltok::colon, "expected ':'"))
11510 return true;
11511
11512 uint8_t AllocType;
11513 if (parseAllocType(AllocType))
11514 return true;
11515
11516 if (parseToken(lltok::comma, "expected ',' in memprof") ||
11517 parseToken(lltok::kw_stackIds, "expected 'stackIds' in memprof") ||
11518 parseToken(lltok::colon, "expected ':'") ||
11519 parseToken(lltok::lparen, "expected '(' in stackIds"))
11520 return true;
11521
11522 SmallVector<unsigned> StackIdIndices;
11523 // Combined index alloc records may not have a stack id list.
11524 if (Lex.getKind() != lltok::rparen) {
11525 do {
11526 uint64_t StackId = 0;
11527 if (parseUInt64(StackId))
11528 return true;
11529 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
11530 } while (EatIfPresent(lltok::comma));
11531 }
11532
11533 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
11534 return true;
11535
11536 MIBs.push_back({(AllocationType)AllocType, StackIdIndices});
11537
11538 if (parseToken(lltok::rparen, "expected ')' in memprof"))
11539 return true;
11540 } while (EatIfPresent(lltok::comma));
11541
11542 if (parseToken(lltok::rparen, "expected ')' in memprof"))
11543 return true;
11544
11545 return false;
11546}
11547
11548/// AllocType
11549/// := ('none'|'notcold'|'cold'|'hot')
11550bool LLParser::parseAllocType(uint8_t &AllocType) {
11551 switch (Lex.getKind()) {
11552 case lltok::kw_none:
11554 break;
11555 case lltok::kw_notcold:
11557 break;
11558 case lltok::kw_cold:
11560 break;
11561 case lltok::kw_hot:
11562 AllocType = (uint8_t)AllocationType::Hot;
11563 break;
11564 default:
11565 return error(Lex.getLoc(), "invalid alloc type");
11566 }
11567 Lex.Lex();
11568 return false;
11569}
11570
11571/// OptionalCallsites
11572/// := 'callsites' ':' '(' Callsite [',' Callsite]* ')'
11573/// Callsite ::= '(' 'callee' ':' GVReference
11574/// ',' 'clones' ':' '(' Version [',' Version]* ')'
11575/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
11576/// Version ::= UInt32
11577/// StackId ::= UInt64
11578bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) {
11579 assert(Lex.getKind() == lltok::kw_callsites);
11580 Lex.Lex();
11581
11582 if (parseToken(lltok::colon, "expected ':' in callsites") ||
11583 parseToken(lltok::lparen, "expected '(' in callsites"))
11584 return true;
11585
11586 IdToIndexMapType IdToIndexMap;
11587 // parse each callsite
11588 do {
11589 if (parseToken(lltok::lparen, "expected '(' in callsite") ||
11590 parseToken(lltok::kw_callee, "expected 'callee' in callsite") ||
11591 parseToken(lltok::colon, "expected ':'"))
11592 return true;
11593
11594 ValueInfo VI;
11595 unsigned GVId = 0;
11596 LocTy Loc = Lex.getLoc();
11597 if (!EatIfPresent(lltok::kw_null)) {
11598 if (parseGVReference(VI, GVId))
11599 return true;
11600 }
11601
11602 if (parseToken(lltok::comma, "expected ',' in callsite") ||
11603 parseToken(lltok::kw_clones, "expected 'clones' in callsite") ||
11604 parseToken(lltok::colon, "expected ':'") ||
11605 parseToken(lltok::lparen, "expected '(' in clones"))
11606 return true;
11607
11608 SmallVector<unsigned> Clones;
11609 do {
11610 unsigned V = 0;
11611 if (parseUInt32(V))
11612 return true;
11613 Clones.push_back(V);
11614 } while (EatIfPresent(lltok::comma));
11615
11616 if (parseToken(lltok::rparen, "expected ')' in clones") ||
11617 parseToken(lltok::comma, "expected ',' in callsite") ||
11618 parseToken(lltok::kw_stackIds, "expected 'stackIds' in callsite") ||
11619 parseToken(lltok::colon, "expected ':'") ||
11620 parseToken(lltok::lparen, "expected '(' in stackIds"))
11621 return true;
11622
11623 SmallVector<unsigned> StackIdIndices;
11624 // Synthesized callsite records will not have a stack id list.
11625 if (Lex.getKind() != lltok::rparen) {
11626 do {
11627 uint64_t StackId = 0;
11628 if (parseUInt64(StackId))
11629 return true;
11630 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
11631 } while (EatIfPresent(lltok::comma));
11632 }
11633
11634 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
11635 return true;
11636
11637 // Keep track of the Callsites array index needing a forward reference.
11638 // We will save the location of the ValueInfo needing an update, but
11639 // can only do so once the SmallVector is finalized.
11640 if (VI.getRef() == FwdVIRef)
11641 IdToIndexMap[GVId].push_back(std::make_pair(Callsites.size(), Loc));
11642 Callsites.push_back({VI, Clones, StackIdIndices});
11643
11644 if (parseToken(lltok::rparen, "expected ')' in callsite"))
11645 return true;
11646 } while (EatIfPresent(lltok::comma));
11647
11648 // Now that the Callsites vector is finalized, it is safe to save the
11649 // locations of any forward GV references that need updating later.
11650 for (auto I : IdToIndexMap) {
11651 auto &Infos = ForwardRefValueInfos[I.first];
11652 for (auto P : I.second) {
11653 assert(Callsites[P.first].Callee.getRef() == FwdVIRef &&
11654 "Forward referenced ValueInfo expected to be empty");
11655 Infos.emplace_back(&Callsites[P.first].Callee, P.second);
11656 }
11657 }
11658
11659 if (parseToken(lltok::rparen, "expected ')' in callsites"))
11660 return true;
11661
11662 return false;
11663}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
Unify divergent function exit nodes
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
Function Alias Analysis false
Expand Atomic instructions
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH
Definition Compiler.h:280
#define LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP
Definition Compiler.h:281
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil globals
static uint64_t align(uint64_t Size)
DXIL Finalize Linkage
dxil translate DXIL Translate Metadata
This file defines the DenseMap class.
@ Default
This file contains constants used for implementing Dwarf debug support.
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
GlobalValue::SanitizerMetadata SanitizerMetadata
Definition Globals.cpp:317
Hexagon Common GEP
#define _
Module.h This file contains the declarations for the Module class.
static GlobalValue * createGlobalFwdRef(Module *M, PointerType *PTy)
static cl::opt< bool > AllowIncompleteIR("allow-incomplete-ir", cl::init(false), cl::Hidden, cl::desc("Allow incomplete IR on a best effort basis (references to unknown " "metadata will be dropped)"))
static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV)
static bool upgradeMemoryAttr(MemoryEffects &ME, lltok::Kind Kind)
static bool blockCommentCrossesBoundary(SMLoc BeginLoc, SMLoc EndLoc, SMLoc BoundaryLoc)
Return whether skipped trivia contains a block comment that crosses the boundary between two metadata...
Definition LLParser.cpp:77
static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved)
static SmallVector< MemoryEffects::Location, 2 > keywordToLoc(lltok::Kind Tok)
static std::optional< DenormalMode::DenormalModeKind > keywordToDenormalModeKind(lltok::Kind Tok)
static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage)
static unsigned keywordToFPClassTest(lltok::Kind Tok)
#define CC_VLS_CASE(ABIVlen)
static std::optional< ModRefInfo > keywordToModRef(lltok::Kind Tok)
static bool isSanitizer(lltok::Kind Kind)
static void dropIntrinsicWithUnknownMetadataArgument(IntrinsicInst *II)
Definition LLParser.cpp:235
#define PARSE_MD_FIELDS()
static Attribute::AttrKind tokenToAttribute(lltok::Kind Kind)
static ValueInfo EmptyVI
#define GET_OR_DISTINCT(CLASS, ARGS)
bool isOldDbgFormatIntrinsic(StringRef Name)
static bool isValidVisibilityForLinkage(unsigned V, unsigned L)
static std::string getTypeString(Type *T)
Definition LLParser.cpp:68
static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L)
static const auto FwdVIRef
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
AllocType
This file contains the declarations for metadata subclasses.
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
Type::TypeID TypeID
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
if(PassOpts->AAPipeline)
static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val)
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
dot regions Print regions of function to dot file(with no function bodies)"
const char * Msg
This file contains some templates that are useful if you are working with the STL at all.
static const char * name
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file provides utility classes that use RAII to save and restore values.
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the SmallPtrSet class.
FunctionLoweringInfo::StatepointRelocationRecord RecordType
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
#define error(X)
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
Value * RHS
Value * LHS
static const fltSemantics & IEEEdouble()
Definition APFloat.h:305
static LLVM_ABI unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
Definition APFloat.cpp:393
opStatus
IEEE-754R 7: Default exception handling.
Definition APFloat.h:377
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1241
APSInt extOrTrunc(uint32_t width) const
Definition APSInt.h:119
void setSwiftError(bool V)
Specify whether this alloca is used to represent a swifterror.
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition Type.cpp:805
void setWeak(bool IsWeak)
static bool isValidFailureOrdering(AtomicOrdering Ordering)
void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
static bool isValidSuccessOrdering(AtomicOrdering Ordering)
void setVolatile(bool V)
Specify whether this is a volatile RMW or not.
BinOp
This enumeration lists the possible modifications atomicrmw can make.
@ Add
*p = old + v
@ FAdd
*p = old + v
@ USubCond
Subtract only if no unsigned overflow.
@ FMinimum
*p = minimum(old, v) minimum matches the behavior of llvm.minimum.
@ Min
*p = old <signed v ? old : v
@ Sub
*p = old - v
@ And
*p = old & v
@ Xor
*p = old ^ v
@ USubSat
*p = usub.sat(old, v) usub.sat matches the behavior of llvm.usub.sat.
@ FMaximum
*p = maximum(old, v) maximum matches the behavior of llvm.maximum.
@ FSub
*p = old - v
@ UIncWrap
Increment one up to a maximum value.
@ Max
*p = old >signed v ? old : v
@ UMin
*p = old <unsigned v ? old : v
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
@ UMax
*p = old >unsigned v ? old : v
@ FMaximumNum
*p = maximumnum(old, v) maximumnum matches the behavior of llvm.maximumnum.
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ FMinimumNum
*p = minimumnum(old, v) minimumnum matches the behavior of llvm.minimumnum.
@ Nand
*p = ~(old & v)
static LLVM_ABI StringRef getOperationName(BinOp Op)
static LLVM_ABI AttributeSet get(LLVMContext &C, const AttrBuilder &B)
static LLVM_ABI bool canUseAsRetAttr(AttrKind Kind)
static bool isTypeAttrKind(AttrKind Kind)
Definition Attributes.h:145
static LLVM_ABI bool canUseAsFnAttr(AttrKind Kind)
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:125
@ None
No attributes have been set.
Definition Attributes.h:127
static LLVM_ABI bool canUseAsParamAttr(AttrKind Kind)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:459
LLVM_ABI void insertDbgRecordBefore(DbgRecord *DR, InstListType::iterator Here)
Insert a DbgRecord into a block at the position given by Here.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
static LLVM_ABI BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
static LLVM_ABI const char * areInvalidOperands(const Type *Ty, Value *Val, Value *Offset)
Return a string if the specified operands are invalid for a bitextract operation, otherwise return nu...
static BitExtractInst * Create(Type *Ty, Value *Src, Value *Offset, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI const char * areInvalidOperands(Value *Base, Value *Val, Value *Offset)
Return a string if the specified operands are invalid for a bitinsert operation, otherwise return nul...
static BitInsertInst * Create(Value *Base, Value *Val, Value *Offset, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
void setCallingConv(CallingConv::ID CC)
void setAttributes(AttributeList A)
Set the attributes for this call.
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
void setTailCallKind(TailCallKind TCK)
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static CaptureInfo none()
Create CaptureInfo that does not capture any components of the pointer.
Definition ModRef.h:427
static LLVM_ABI CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", InsertPosition InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static LLVM_ABI bool castIsValid(Instruction::CastOps op, Type *SrcTy, Type *DstTy)
This method can be used to determine if a cast from SrcTy to DstTy using Opcode op is valid or not.
static CatchPadInst * Create(Value *CatchSwitch, ArrayRef< Value * > Args, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static CatchReturnInst * Create(Value *CatchPad, BasicBlock *BB, InsertPosition InsertBefore=nullptr)
static CatchSwitchInst * Create(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumHandlers, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static CleanupPadInst * Create(Value *ParentPad, ArrayRef< Value * > Args={}, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static CleanupReturnInst * Create(Value *CleanupPad, BasicBlock *UnwindBB=nullptr, InsertPosition InsertBefore=nullptr)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:743
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:757
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:746
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:755
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:744
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:745
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:754
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:748
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:751
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:752
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:747
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:749
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:756
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:753
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:742
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
@ Largest
The linker will choose the largest COMDAT.
Definition Comdat.h:39
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition Comdat.h:41
@ Any
The linker may choose any COMDAT.
Definition Comdat.h:37
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition Comdat.h:38
static CondBrInst * Create(Value *Cond, BasicBlock *IfTrue, BasicBlock *IfFalse, InsertPosition InsertBefore=nullptr)
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:548
static LLVM_ABI Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true, bool ByteString=false)
This method constructs a CDS and initializes it with a text string.
static LLVM_ABI Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_ABI Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static LLVM_ABI Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
static LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition Constants.h:1477
static LLVM_ABI Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
static bool isSupportedGetElementPtr(const Type *SrcElemTy)
Whether creating a constant expression for this getelementptr type is supported.
Definition Constants.h:1624
static LLVM_ABI Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible.
static LLVM_ABI bool isValueValidForType(Type *Ty, const APFloat &V)
Return true if Ty is big enough to represent V.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static ConstantInt * getSigned(IntegerType *Ty, int64_t V, bool ImplicitTrunc=false)
Return a ConstantInt with the specified value for the specified type.
Definition Constants.h:135
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:162
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI ConstantPtrAuth * get(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc, Constant *AddrDisc, Constant *DeactivationSymbol)
Return a pointer signed with the specified parameters.
static LLVM_ABI std::optional< ConstantRangeList > getConstantRangeList(ArrayRef< ConstantRange > RangesRef)
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
static LLVM_ABI Constant * get(StructType *T, ArrayRef< Constant * > V)
static LLVM_ABI Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
static LLVM_ABI Constant * get(ArrayRef< Constant * > V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
static LLVM_ABI DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
static DIAssignID * getDistinct(LLVMContext &Context)
DebugEmissionKind getEmissionKind() const
DebugNameTableKind getNameTableKind() const
static LLVM_ABI DICompositeType * buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, Metadata *SizeInBits, uint32_t AlignInBits, Metadata *OffsetInBits, Metadata *Specification, uint32_t NumExtraInhabitants, DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, std::optional< uint32_t > EnumKind, Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, Metadata *Rank, Metadata *Annotations, Metadata *BitStride)
Build a DICompositeType with the given ODR identifier.
static LLVM_ABI std::optional< ChecksumKind > getChecksumKind(StringRef CSKindStr)
ChecksumKind
Which algorithm (e.g.
static LLVM_ABI std::optional< FixedPointKind > getFixedPointKind(StringRef Str)
static LLVM_ABI DIFlags getFlag(StringRef Flag)
DIFlags
Debug info flags.
LLVM_ABI void cleanupRetainedNodes()
When IR modules are merged, typically during LTO, the merged module may contain several types having ...
static LLVM_ABI DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized, unsigned Virtuality=SPFlagNonvirtual, bool IsMainSubprogram=false)
static LLVM_ABI DISPFlags getFlag(StringRef Flag)
DISPFlags
Debug info subprogram flags.
static LLVM_ABI DSOLocalEquivalent * get(GlobalValue *GV)
Return a DSOLocalEquivalent for the specified global value.
static LLVM_ABI Expected< DataLayout > parse(StringRef LayoutString)
Parse a data layout string and return the layout.
static LLVM_ABI DbgLabelRecord * createUnresolvedDbgLabelRecord(MDNode *Label)
For use during parsing; creates a DbgLabelRecord from as-of-yet unresolved MDNodes.
Kind
Subclass discriminator.
static LLVM_ABI DbgVariableRecord * createUnresolvedDbgVariableRecord(LocationType Type, Metadata *Val, MDNode *Variable, MDNode *Expression, MDNode *AssignID, Metadata *Address, MDNode *AddressExpression)
Used to create DbgVariableRecords during parsing, where some metadata references may still be unresol...
unsigned size() const
Definition DenseMap.h:718
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:794
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:305
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI bool isValidOperands(const Value *Vec, const Value *Idx)
Return true if an extractelement instruction can be formed with the specified operands.
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
bool any() const
Definition FMF.h:56
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
static LLVM_ABI bool isValidArgumentType(Type *ArgTy)
Return true if the specified type is valid as an argument type.
Definition Type.cpp:457
Type::subtype_iterator param_iterator
static LLVM_ABI bool isValidReturnType(Type *RetTy)
Return true if the specified type is valid as a return type.
Definition Type.cpp:452
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:169
Argument * arg_iterator
Definition Function.h:73
void setPrefixData(Constant *PrefixData)
void setGC(std::string Str)
Definition Function.cpp:829
void setPersonalityFn(Constant *Fn)
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Function.cpp:455
arg_iterator arg_begin()
Definition Function.h:853
void setAlignment(Align Align)
Sets the alignment attribute of the Function.
Definition Function.h:1025
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition Function.h:332
void setPreferredAlignment(MaybeAlign Align)
Sets the prefalign attribute of the Function.
Definition Function.h:1037
void setPrologueData(Constant *PrologueData)
void setCallingConv(CallingConv::ID CC)
Definition Function.h:277
static GEPNoWrapFlags inBounds()
static GEPNoWrapFlags noUnsignedWrap()
static GEPNoWrapFlags noUnsignedSignedWrap()
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
static bool isValidLinkage(LinkageTypes L)
Definition GlobalAlias.h:98
static LLVM_ABI GlobalAlias * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Aliasee, Module *Parent)
If a parent module is specified, the alias is automatically inserted into the end of the specified mo...
Definition Globals.cpp:692
static LLVM_ABI GlobalIFunc * create(Type *Ty, unsigned AddressSpace, LinkageTypes Linkage, const Twine &Name, Constant *Resolver, Module *Parent)
If a parent module is specified, the ifunc is automatically inserted into the end of the specified mo...
Definition Globals.cpp:749
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:287
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:348
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
std::pair< key_type, mapped_type > value_type
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
LLVM_ABI const SanitizerMetadata & getSanitizerMetadata() const
Definition Globals.cpp:318
static bool isLocalLinkage(LinkageTypes Linkage)
void setUnnamedAddr(UnnamedAddr Val)
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
LLVM_ABI GUID getGUIDOrFallback() const
Return the GUID for this value if it has been assigned, otherwise fall back to computing it based on ...
Definition Globals.cpp:110
void setDLLStorageClass(DLLStorageClassTypes C)
void setThreadLocalMode(ThreadLocalMode Val)
void setLinkage(LinkageTypes LT)
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition GlobalValue.h:74
@ DLLExportStorageClass
Function to be accessible from DLL.
Definition GlobalValue.h:77
@ DLLImportStorageClass
Function to be imported from DLL.
Definition GlobalValue.h:76
bool hasSanitizerMetadata() const
unsigned getAddressSpace() const
void setDSOLocal(bool Local)
LLVM_ABI void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition Globals.cpp:158
PointerType * getType() const
Global values are always pointers.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
@ ProtectedVisibility
The GV is protected.
Definition GlobalValue.h:70
static bool isValidDeclarationLinkage(LinkageTypes Linkage)
static LLVM_ABI std::string getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName)
Return the modified name for a global value suitable to be used as the key for a global lookup (e....
Definition Globals.cpp:234
void setVisibility(VisibilityTypes V)
LLVM_ABI void setSanitizerMetadata(SanitizerMetadata Meta)
Definition Globals.cpp:324
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ CommonLinkage
Tentative definitions.
Definition GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition GlobalValue.h:59
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
Type * getValueType() const
LLVM_ABI void setPartition(StringRef Part)
Definition Globals.cpp:301
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition Globals.cpp:613
void setAttributes(AttributeSet A)
Set attribute list for this global.
void setConstant(bool Val)
LLVM_ABI void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
Definition Globals.cpp:660
void setExternallyInitialized(bool Val)
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalVariable.
LLVM_ABI void addDestination(BasicBlock *Dest)
Add a destination.
static IndirectBrInst * Create(Value *Address, unsigned NumDests, InsertPosition InsertBefore=nullptr)
static LLVM_ABI InlineAsm * get(FunctionType *Ty, StringRef AsmString, StringRef Constraints, bool hasSideEffects, bool isAlignStack=false, AsmDialect asmDialect=AD_ATT, bool canThrow=false)
InlineAsm::get - Return the specified uniqued inline asm string.
Definition InlineAsm.cpp:43
static LLVM_ABI Error verify(FunctionType *Ty, StringRef Constraints)
This static method can be used by the parser to check to see if the specified constraint string is le...
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static LLVM_ABI bool isValidOperands(const Value *Vec, const Value *NewElt, const Value *Idx)
Return true if an insertelement instruction can be formed with the specified operands.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
LLVM_ABI void setNonNeg(bool b=true)
Set or clear the nneg flag on this instruction, which must be a zext instruction.
bool isTerminator() const
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
LLVM_ABI InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
A wrapper class for inspecting calls to intrinsic functions.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
lltok::Kind Lex()
Definition LLLexer.h:68
lltok::Kind getKind() const
Definition LLLexer.h:73
LocTy getLoc() const
Definition LLLexer.h:71
LLVM_ABI bool parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read, const SlotMapping *Slots)
Definition LLParser.cpp:170
LLLexer::LocTy LocTy
Definition LLParser.h:110
LLVMContext & getContext()
Definition LLParser.h:239
LLVM_ABI bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition LLParser.cpp:154
LLVM_ABI bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition LLParser.cpp:141
LLVM_ABI bool parseMetadataDefinitions(SlotMapping &Slots, ArrayRef< SMLoc > DefinitionEnds)
Definition LLParser.cpp:185
LLVM_ABI bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition LLParser.cpp:122
static LLVM_ABI LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
Metadata node.
Definition Metadata.h:1081
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1587
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1579
A single uniqued string.
Definition Metadata.h:733
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:597
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition Metadata.h:1536
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1525
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition Metadata.h:1545
static MemoryEffectsBase readOnly()
Definition ModRef.h:133
MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const
Get new MemoryEffectsBase with modified ModRefInfo for Loc.
Definition ModRef.h:224
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:143
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:149
bool isTargetMemLoc(IRMemLocation Loc) const
Whether location is target memory location.
Definition ModRef.h:279
static MemoryEffectsBase writeOnly()
Definition ModRef.h:138
static MemoryEffectsBase inaccessibleOrArgMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:166
static MemoryEffectsBase none()
Definition ModRef.h:128
static MemoryEffectsBase unknown()
Definition ModRef.h:123
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:184
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:107
Root of the metadata hierarchy.
Definition Metadata.h:64
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
StringMap< Comdat > ComdatSymTabType
The type of the comdat "symbol" table.
Definition Module.h:83
LLVM_ABI void addOperand(MDNode *M)
static LLVM_ABI NoCFIValue * get(GlobalValue *GV)
Return a NoCFIValue for the specified function.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PointerType * getUnqual(LLVMContext &C)
This constructs an opaque pointer to an object in the default address space (address space zero).
static LLVM_ABI PointerType * get(LLVMContext &C, unsigned AddressSpace)
This constructs an opaque pointer to an object in a numbered address space.
Definition Type.cpp:887
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition Type.cpp:904
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
static ResumeInst * Create(Value *Exn, InsertPosition InsertBefore=nullptr)
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
Represents a location in source code.
Definition SMLoc.h:22
constexpr const char * getPointer() const
Definition SMLoc.h:33
static LLVM_ABI const char * areInvalidOperands(Value *Cond, Value *True, Value *False)
Return a string if the specified operands are invalid for a select operation, otherwise return null.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, const Instruction *MDFrom=nullptr)
ArrayRef< int > getShuffleMask() const
static LLVM_ABI bool isValidOperands(const Value *V1, const Value *V2, const Value *Mask)
Return true if a shufflevector instruction can be formed with the specified operands.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
iterator end()
Definition StringMap.h:214
iterator find(StringRef Key)
Definition StringMap.h:227
StringMapIterBase< Comdat, false > iterator
Definition StringMap.h:209
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
Definition Type.cpp:467
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:662
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition Type.cpp:743
LLVM_ABI Error setBodyOrError(ArrayRef< Type * > Elements, bool isPacked=false)
Specify a body for an opaque identified type or return an error if it would make the type recursive.
Definition Type.cpp:581
LLVM_ABI bool isScalableTy() const
Returns true if this struct contains a scalable vector.
Definition Type.cpp:494
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, InsertPosition InsertBefore=nullptr)
@ HasZeroInit
zeroinitializer is valid for this target extension type.
static LLVM_ABI Expected< TargetExtType * > getOrError(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types={}, ArrayRef< unsigned > Ints={})
Return a target extension type having the specified name and optional type and integer parameters,...
Definition Type.cpp:942
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:300
bool isByteTy() const
True if this is an instance of ByteType.
Definition Type.h:237
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:283
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:274
static LLVM_ABI Type * getTokenTy(LLVMContext &C)
Definition Type.cpp:279
bool isLabelTy() const
Return true if this is 'label'.
Definition Type.h:225
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:258
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:155
static LLVM_ABI Type * getLabelTy(LLVMContext &C)
Definition Type.cpp:273
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isSized() const
Return true if it makes sense to take the size of this type.
Definition Type.h:321
LLVM_ABI bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition Type.cpp:241
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:297
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:363
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:314
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:296
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:280
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition Type.h:268
LLVM_ABI bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
Definition Type.cpp:61
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:231
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:222
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:96
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
bool isMetadataTy() const
Return true if this is 'metadata'.
Definition Type.h:228
static LLVM_ABI UnaryOperator * Create(UnaryOps Op, Value *S, const Twine &Name=Twine(), InsertPosition InsertBefore=nullptr)
Construct a unary instruction, given the opcode and an operand.
static UncondBrInst * Create(BasicBlock *Target, InsertPosition InsertBefore=nullptr)
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static LLVM_ABI ValueAsMetadata * get(Value *V)
Definition Metadata.cpp:514
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
static constexpr uint64_t MaximumAlignment
Definition Value.h:801
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:394
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVM_ABI void deleteValue()
Delete a pointer to a generic Value.
Definition Value.cpp:108
bool use_empty() const
Definition Value.h:348
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
self_iterator getIterator()
Definition ilist_node.h:123
A raw_ostream that writes to an std::string.
std::string & str()
Returns the string's reference.
CallInst * Call
LLVM_ABI unsigned getSourceLanguageName(StringRef SourceLanguageNameString)
Definition Dwarf.cpp:615
LLVM_ABI unsigned getOperationEncoding(StringRef OperationEncodingString)
Definition Dwarf.cpp:165
LLVM_ABI unsigned getAttributeEncoding(StringRef EncodingString)
Definition Dwarf.cpp:275
LLVM_ABI unsigned getLanguageDialect(StringRef LanguageDialectString)
Definition Dwarf.cpp:634
LLVM_ABI unsigned getTag(StringRef TagString)
Definition Dwarf.cpp:32
LLVM_ABI unsigned getCallingConvention(StringRef LanguageString)
Definition Dwarf.cpp:670
LLVM_ABI unsigned getLanguage(StringRef LanguageString)
Definition Dwarf.cpp:424
LLVM_ABI unsigned getVirtuality(StringRef VirtualityString)
Definition Dwarf.cpp:386
LLVM_ABI unsigned getEnumKind(StringRef EnumKindString)
Definition Dwarf.cpp:405
LLVM_ABI unsigned getMacinfo(StringRef MacinfoString)
Definition Dwarf.cpp:742
#define UINT64_MAX
Definition DataTypes.h:77
#define INT64_MIN
Definition DataTypes.h:74
#define INT64_MAX
Definition DataTypes.h:71
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char IsVolatile[]
Key for Kernel::Arg::Metadata::mIsVolatile.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ Entry
Definition COFF.h:862
@ AArch64_VectorCall
Used between AArch64 Advanced SIMD functions.
@ X86_64_SysV
The C convention as specified in the x86-64 supplement to the System V ABI, used on most non-Windows ...
@ RISCV_VectorCall
Calling convention used for RISC-V V-extension.
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
@ AMDGPU_VS
Used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (vertex shader if tess...
@ AVR_SIGNAL
Used for AVR signal routines.
@ Swift
Calling convention for Swift.
Definition CallingConv.h:69
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
@ AArch64_SVE_VectorCall
Used between AArch64 SVE functions.
@ ARM_APCS
ARM Procedure Calling Standard (obsolete, but still used on some targets).
@ CHERIoT_CompartmentCall
Calling convention used for CHERIoT when crossing a protection boundary.
@ CFGuard_Check
Special calling convention on Windows for calling the Control Guard Check ICall funtion.
Definition CallingConv.h:82
@ AVR_INTR
Used for AVR interrupt routines.
@ PreserveMost
Used for runtime calls that preserves most registers.
Definition CallingConv.h:63
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_Gfx
Used for AMD graphics targets.
@ DUMMY_HHVM
Placeholders for HHVM calling conventions (deprecated, removed).
@ AMDGPU_CS_ChainPreserve
Used on AMDGPUs to give the middle-end more control over argument placement.
@ AMDGPU_HS
Used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
@ ARM_AAPCS
ARM Architecture Procedure Calling Standard calling convention (aka EABI).
@ CHERIoT_CompartmentCallee
Calling convention used for the callee of CHERIoT_CompartmentCall.
@ AMDGPU_GS
Used for Mesa/AMDPAL geometry shaders.
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2
Preserve X2-X15, X19-X29, SP, Z0-Z31, P0-P15.
@ CHERIoT_LibraryCall
Calling convention used for CHERIoT for cross-library calls to a stateless compartment.
@ CXX_FAST_TLS
Used for access functions.
Definition CallingConv.h:72
@ X86_INTR
x86 hardware interrupt context.
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0
Preserve X0-X13, X19-X29, SP, Z0-Z31, P0-P15.
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition CallingConv.h:47
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1
Preserve X1-X15, X19-X29, SP, Z0-Z31, P0-P15.
@ X86_ThisCall
Similar to X86_StdCall.
@ PTX_Device
Call to a PTX device function.
@ SPIR_KERNEL
Used for SPIR kernel functions.
@ PreserveAll
Used for runtime calls that preserves (almost) all registers.
Definition CallingConv.h:66
@ X86_StdCall
stdcall is mostly used by the Win32 API.
Definition CallingConv.h:99
@ SPIR_FUNC
Used for SPIR non-kernel device functions.
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ MSP430_INTR
Used for MSP430 interrupt routines.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ Intel_OCL_BI
Used for Intel OpenCL built-ins.
@ PreserveNone
Used for runtime calls that preserves none general registers.
Definition CallingConv.h:90
@ AMDGPU_ES
Used for AMDPAL shader stage before geometry shader if geometry is in use.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition CallingConv.h:76
@ Win64
The C convention as implemented on Windows/x86-64 and AArch64.
@ PTX_Kernel
Call to a PTX kernel. Passes all arguments in parameter space.
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
Definition CallingConv.h:87
@ GRAAL
Used by GraalVM. Two additional registers are reserved.
@ AMDGPU_LS
Used for AMDPAL vertex shader if tessellation is in use.
@ ARM_AAPCS_VFP
Same as ARM_AAPCS, but uses hard floating point ABI.
@ X86_RegCall
Register calling convention used for parameters transfer optimization.
@ M68k_RTD
Used for M68k rtd-based CC (similar to X86's stdcall).
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ X86_FastCall
'fast' analog of X86_StdCall.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
LLVM_ABI ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
LLVM_ABI bool isSignatureValid(Intrinsic::ID ID, FunctionType *FT, SmallVectorImpl< Type * > &OverloadTys, raw_ostream &OS=nulls())
Returns true if FT is a valid function type for intrinsic ID.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
constexpr bool isAtomic(const T &...O)
Definition SIDefines.h:390
constexpr bool isPacked(const T &...O)
Definition SIDefines.h:337
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:58
@ Valid
The data is already valid.
initializer< Ty > init(const Ty &Val)
@ DW_CC_hi_user
Definition Dwarf.h:856
@ DW_ATE_hi_user
Definition Dwarf.h:163
@ DW_LLVM_LANG_DIALECT_max
Definition Dwarf.h:212
@ DW_APPLE_ENUM_KIND_max
Definition Dwarf.h:206
@ DW_LANG_hi_user
Definition Dwarf.h:226
MacinfoRecordType
Definition Dwarf.h:913
@ DW_MACINFO_vendor_ext
Definition Dwarf.h:919
@ DW_VIRTUALITY_max
Definition Dwarf.h:200
@ DW_TAG_hi_user
Definition Dwarf.h:109
@ DW_TAG_invalid
LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
Definition Dwarf.h:48
@ DW_MACINFO_invalid
Macinfo type for invalid results.
Definition Dwarf.h:50
@ DW_APPLE_ENUM_KIND_invalid
Enum kind for invalid results.
Definition Dwarf.h:51
@ DW_VIRTUALITY_invalid
Virtuality for invalid results.
Definition Dwarf.h:49
@ kw_msp430_intrcc
Definition LLToken.h:155
@ kw_riscv_vls_cc
Definition LLToken.h:191
@ kw_cxx_fast_tlscc
Definition LLToken.h:174
@ kw_extractvalue
Definition LLToken.h:377
@ kw_dso_preemptable
Definition LLToken.h:51
@ DwarfVirtuality
Definition LLToken.h:514
@ DwarfLangDialect
Definition LLToken.h:517
@ kw_arm_apcscc
Definition LLToken.h:147
@ kw_inteldialect
Definition LLToken.h:129
@ kw_x86_stdcallcc
Definition LLToken.h:142
@ kw_constant
Definition LLToken.h:48
@ kw_initialexec
Definition LLToken.h:74
@ kw_aarch64_sme_preservemost_from_x1
Definition LLToken.h:153
@ kw_provenance
Definition LLToken.h:225
@ kw_mustBeUnreachable
Definition LLToken.h:425
@ kw_internal
Definition LLToken.h:54
@ kw_target_mem
Definition LLToken.h:211
@ kw_no_sanitize_hwaddress
Definition LLToken.h:493
@ kw_datalayout
Definition LLToken.h:92
@ kw_wpdResolutions
Definition LLToken.h:464
@ kw_canAutoHide
Definition LLToken.h:408
@ kw_alwaysInline
Definition LLToken.h:421
@ kw_insertelement
Definition LLToken.h:374
@ kw_linkonce
Definition LLToken.h:55
@ kw_cheriot_librarycallcc
Definition LLToken.h:194
@ kw_fmaximumnum
Definition LLToken.h:296
@ kw_inaccessiblememonly
Definition LLToken.h:218
@ kw_amdgpu_gfx
Definition LLToken.h:185
@ kw_getelementptr
Definition LLToken.h:371
@ FloatHexLiteral
Definition LLToken.h:534
@ kw_m68k_rtdcc
Definition LLToken.h:188
@ kw_preserve_nonecc
Definition LLToken.h:169
@ kw_x86_fastcallcc
Definition LLToken.h:143
@ kw_visibility
Definition LLToken.h:404
@ kw_cheriot_compartmentcalleecc
Definition LLToken.h:193
@ kw_positivezero
Definition LLToken.h:231
@ kw_unordered
Definition LLToken.h:96
@ kw_singleImpl
Definition LLToken.h:467
@ kw_localexec
Definition LLToken.h:75
@ kw_cfguard_checkcc
Definition LLToken.h:141
@ kw_typeCheckedLoadConstVCalls
Definition LLToken.h:444
@ kw_aarch64_sve_vector_pcs
Definition LLToken.h:151
@ kw_amdgpu_kernel
Definition LLToken.h:184
@ kw_uselistorder
Definition LLToken.h:392
@ kw_blockcount
Definition LLToken.h:402
@ kw_notEligibleToImport
Definition LLToken.h:405
@ kw_linkonce_odr
Definition LLToken.h:56
@ kw_protected
Definition LLToken.h:66
@ kw_dllexport
Definition LLToken.h:61
@ kw_x86_vectorcallcc
Definition LLToken.h:145
@ kw_ptx_device
Definition LLToken.h:159
@ kw_personality
Definition LLToken.h:346
@ DwarfEnumKind
Definition LLToken.h:528
@ kw_declaration
Definition LLToken.h:411
@ kw_elementwise
Definition LLToken.h:94
@ DwarfAttEncoding
Definition LLToken.h:513
@ kw_external
Definition LLToken.h:71
@ kw_spir_kernel
Definition LLToken.h:160
@ kw_local_unnamed_addr
Definition LLToken.h:68
@ kw_hasUnknownCall
Definition LLToken.h:424
@ kw_x86_intrcc
Definition LLToken.h:171
@ kw_addrspacecast
Definition LLToken.h:341
@ kw_zeroinitializer
Definition LLToken.h:76
@ StringConstant
Definition LLToken.h:511
@ kw_x86_thiscallcc
Definition LLToken.h:144
@ kw_cheriot_compartmentcallcc
Definition LLToken.h:192
@ kw_unnamed_addr
Definition LLToken.h:67
@ NameTableKind
Definition LLToken.h:520
@ kw_inlineBits
Definition LLToken.h:462
@ kw_weak_odr
Definition LLToken.h:58
@ kw_dllimport
Definition LLToken.h:60
@ kw_argmemonly
Definition LLToken.h:217
@ kw_blockaddress
Definition LLToken.h:379
@ kw_amdgpu_gfx_whole_wave
Definition LLToken.h:186
@ kw_landingpad
Definition LLToken.h:345
@ kw_aarch64_vector_pcs
Definition LLToken.h:150
@ kw_source_filename
Definition LLToken.h:90
@ kw_typeTestAssumeConstVCalls
Definition LLToken.h:443
@ FixedPointKind
Definition LLToken.h:521
@ kw_target_mem1
Definition LLToken.h:213
@ kw_ptx_kernel
Definition LLToken.h:158
@ kw_extractelement
Definition LLToken.h:373
@ kw_branchFunnel
Definition LLToken.h:468
@ kw_typeidCompatibleVTable
Definition LLToken.h:449
@ kw_vTableFuncs
Definition LLToken.h:435
@ kw_volatile
Definition LLToken.h:93
@ kw_typeCheckedLoadVCalls
Definition LLToken.h:442
@ kw_no_sanitize_address
Definition LLToken.h:490
@ kw_inaccessiblemem_or_argmemonly
Definition LLToken.h:219
@ kw_externally_initialized
Definition LLToken.h:69
@ kw_sanitize_address_dyninit
Definition LLToken.h:496
@ DwarfSourceLangName
Definition LLToken.h:516
@ kw_noRenameOnPromotion
Definition LLToken.h:412
@ kw_amdgpu_cs_chain_preserve
Definition LLToken.h:183
@ kw_thread_local
Definition LLToken.h:72
@ kw_catchswitch
Definition LLToken.h:359
@ kw_extern_weak
Definition LLToken.h:70
@ kw_arm_aapcscc
Definition LLToken.h:148
@ kw_read_provenance
Definition LLToken.h:226
@ kw_cleanuppad
Definition LLToken.h:362
@ kw_available_externally
Definition LLToken.h:63
@ kw_singleImplName
Definition LLToken.h:469
@ kw_target_mem0
Definition LLToken.h:212
@ kw_swifttailcc
Definition LLToken.h:166
@ kw_monotonic
Definition LLToken.h:97
@ kw_typeTestAssumeVCalls
Definition LLToken.h:441
@ kw_preservesign
Definition LLToken.h:230
@ kw_attributes
Definition LLToken.h:197
@ kw_code_model
Definition LLToken.h:123
@ kw_localdynamic
Definition LLToken.h:73
@ kw_uniformRetVal
Definition LLToken.h:472
@ kw_sideeffect
Definition LLToken.h:128
@ kw_sizeM1BitWidth
Definition LLToken.h:458
@ kw_nodeduplicate
Definition LLToken.h:261
@ kw_avr_signalcc
Definition LLToken.h:157
@ kw_bitextract
Definition LLToken.h:384
@ kw_exactmatch
Definition LLToken.h:259
@ kw_fminimumnum
Definition LLToken.h:297
@ kw_unreachable
Definition LLToken.h:357
@ kw_intel_ocl_bicc
Definition LLToken.h:140
@ kw_dso_local
Definition LLToken.h:50
@ kw_returnDoesNotAlias
Definition LLToken.h:419
@ kw_aarch64_sme_preservemost_from_x0
Definition LLToken.h:152
@ kw_preserve_allcc
Definition LLToken.h:168
@ kw_importType
Definition LLToken.h:409
@ kw_cleanupret
Definition LLToken.h:358
@ kw_shufflevector
Definition LLToken.h:375
@ kw_riscv_vector_cc
Definition LLToken.h:190
@ kw_avr_intrcc
Definition LLToken.h:156
@ kw_definition
Definition LLToken.h:410
@ kw_virtualConstProp
Definition LLToken.h:474
@ kw_vcall_visibility
Definition LLToken.h:463
@ kw_appending
Definition LLToken.h:59
@ kw_inaccessiblemem
Definition LLToken.h:210
@ kw_preserve_mostcc
Definition LLToken.h:167
@ kw_arm_aapcs_vfpcc
Definition LLToken.h:149
@ kw_typeTestRes
Definition LLToken.h:451
@ kw_x86_regcallcc
Definition LLToken.h:146
@ kw_typeIdInfo
Definition LLToken.h:439
@ kw_amdgpu_cs_chain
Definition LLToken.h:182
@ kw_dso_local_equivalent
Definition LLToken.h:380
@ kw_x86_64_sysvcc
Definition LLToken.h:162
@ DbgRecordType
Definition LLToken.h:527
@ kw_address_is_null
Definition LLToken.h:224
@ kw_musttail
Definition LLToken.h:86
@ kw_aarch64_sme_preservemost_from_x2
Definition LLToken.h:154
@ kw_uniqueRetVal
Definition LLToken.h:473
@ kw_insertvalue
Definition LLToken.h:378
@ kw_indirectbr
Definition LLToken.h:354
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:594
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
std::tuple< const DIScope *, const DIScope *, const DILocalVariable * > VarID
A unique key that represents a debug variable.
LLVM_ABI void UpgradeIntrinsicCall(CallBase *CB, Function *NewFn)
This is the complement to the above, replacing a specific call to an intrinsic function with a call t...
LLVM_ABI void UpgradeSectionAttributes(Module &M)
std::vector< VirtFuncOffset > VTableFuncList
List of functions referenced by a particular vtable definition.
SaveAndRestore(T &) -> SaveAndRestore< T >
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Done
Definition Threading.h:60
AllocFnKind
Definition Attributes.h:54
scope_exit(Callable) -> scope_exit< Callable >
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
LLVM_ABI bool UpgradeIntrinsicFunction(Function *F, Function *&NewFn, bool CanUpgradeDebugIntrinsicsToRecords=true)
This is a more granular function that simply checks an intrinsic function for upgrading,...
LLVM_ABI void UpgradeCallsToIntrinsic(Function *F)
This is an auto-upgrade hook for any old intrinsic function syntaxes which need to have both the func...
LLVM_ABI void UpgradeNVVMAnnotations(Module &M)
Convert legacy nvvm.annotations metadata to appropriate function attributes.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
auto cast_or_null(const Y &Val)
Definition Casting.h:714
LLVM_ABI bool UpgradeModuleFlags(Module &M)
This checks for module flags which should be upgraded.
static void assign(DXContainerYAML::SourceInfo::SectionHeader &Dst, const dxbc::SourceInfo::SectionHeader &Src)
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
LLVM_ABI bool UpgradeCFIFunctionsMetadata(Module &M)
Upgrade the cfi.functions metadata node by calculating and inserting the GUID for each function entry...
LLVM_ABI void copyModuleAttrToFunctions(Module &M)
Copies module attributes to the functions in the module.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
UWTableKind
Definition CodeGen.h:299
@ Async
"Asynchronous" unwind tables (instr precise)
Definition CodeGen.h:302
@ Sync
"Synchronous" unwind tables
Definition CodeGen.h:301
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:383
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
CaptureComponents
Components of the pointer that may be captured.
Definition ModRef.h:365
iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)
Split the specified string over a separator and return a range-compatible iterable over its partition...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
@ NoModRef
The access neither references nor modifies the value stored in memory.
Definition ModRef.h:30
IRMemLocation
The locations at which a function might access memory.
Definition ModRef.h:60
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
llvm::function_ref< std::optional< std::string >(StringRef, StringRef)> DataLayoutCallbackTy
Definition Parser.h:37
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:2028
DWARFExpression::Operation Op
@ NearestTiesToEven
roundTiesToEven.
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2208
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
LLVM_ABI bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
static int64_t upperBound(StackOffset Size)
bool capturesNothing(CaptureComponents CC)
Definition ModRef.h:375
#define N
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getInvalid()
static constexpr DenormalMode getIEEE()
static constexpr uint32_t RangeWidth
std::vector< Call > Calls
In the per-module summary, it summarizes the byte offset applied to each pointer parameter before pas...
std::vector< ConstVCall > TypeCheckedLoadConstVCalls
std::vector< VFuncId > TypeCheckedLoadVCalls
std::vector< ConstVCall > TypeTestAssumeConstVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm....
std::vector< GlobalValue::GUID > TypeTests
List of type identifiers used by this function in llvm.type.test intrinsics referenced by something o...
std::vector< VFuncId > TypeTestAssumeVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm....
unsigned NoRenameOnPromotion
This field is written by the ThinLTO prelink stage to decide whether a particular static global value...
unsigned DSOLocal
Indicates that the linker resolved the symbol to a definition from within the same linkage unit.
unsigned CanAutoHide
In the per-module summary, indicates that the global value is linkonce_odr and global unnamed addr (s...
unsigned ImportType
This field is written by the ThinLTO indexing step to postlink combined summary.
unsigned NotEligibleToImport
Indicate if the global value cannot be imported (e.g.
unsigned Linkage
The linkage type of the associated global value.
unsigned Visibility
Indicates the visibility.
unsigned Live
In per-module summary, indicate that the global value must be considered a live root for index-based ...
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
LLVM_ABI bool set(StringRef Name, std::string Value)
Set a property using a string name.
Definition Module.cpp:1033
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition SlotMapping.h:32
std::map< unsigned, Type * > Types
Definition SlotMapping.h:36
StringMap< Type * > NamedTypes
Definition SlotMapping.h:35
std::map< unsigned, TrackingMDNodeRef > MetadataNodes
Definition SlotMapping.h:34
NumberedValues< GlobalValue * > GlobalValues
Definition SlotMapping.h:33
std::map< uint64_t, WholeProgramDevirtResolution > WPDRes
Mapping from byte offset to whole-program devirt resolution for that (typeid, byte offset) pair.
TypeTestResolution TTRes
@ Unknown
Unknown (analysis not performed, don't lower)
@ Single
Single element (last example in "Short Inline Bit Vectors")
@ Inline
Inlined bit vector ("Short Inline Bit Vectors")
@ Unsat
Unsatisfiable type (i.e. no global has this type metadata)
@ AllOnes
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors")
@ ByteArray
Test a byte array (first example)
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
enum llvm::TypeTestResolution::Kind TheKind
ValID - Represents a reference of a definition of some sort with no type.
Definition LLParser.h:54
@ t_PackedConstantStruct
Definition LLParser.h:72
@ t_ConstantStruct
Definition LLParser.h:71
@ t_ConstantSplat
Definition LLParser.h:69
enum llvm::ValID::@273232264270353276247031231016211363171152164072 Kind
unsigned UIntVal
Definition LLParser.h:76
FunctionType * FTy
Definition LLParser.h:77
LLLexer::LocTy Loc
Definition LLParser.h:75
std::string StrVal
Definition LLParser.h:78
Struct that holds a reference to a particular GUID in a global value summary.
const GlobalValueSummaryMapTy::value_type * getRef() const
bool isWriteOnly() const
bool isReadOnly() const
@ UniformRetVal
Uniform return value optimization.
@ VirtualConstProp
Virtual constant propagation.
@ UniqueRetVal
Unique return value optimization.
@ Indir
Just do a regular virtual call.
uint64_t Info
Additional information for the resolution:
enum llvm::WholeProgramDevirtResolution::ByArg::Kind TheKind
enum llvm::WholeProgramDevirtResolution::Kind TheKind
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument,...
@ SingleImpl
Single implementation devirtualization.
@ Indir
Just do a regular virtual call.
@ BranchFunnel
When retpoline mitigation is enabled, use a branch funnel that is defined in the merged module.