LLVM 23.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/// Run: module ::= toplevelentity*
76bool LLParser::Run(bool UpgradeDebugInfo,
77 DataLayoutCallbackTy DataLayoutCallback) {
78 // Prime the lexer.
79 Lex.Lex();
80
81 if (Context.shouldDiscardValueNames())
82 return error(
83 Lex.getLoc(),
84 "Can't read textual IR with a Context that discards named Values");
85
86 if (M) {
87 if (parseTargetDefinitions(DataLayoutCallback))
88 return true;
89 }
90
91 return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) ||
92 validateEndOfIndex();
93}
94
96 const SlotMapping *Slots) {
97 restoreParsingState(Slots);
98 Lex.Lex();
99
100 Type *Ty = nullptr;
101 if (parseType(Ty) || parseConstantValue(Ty, C))
102 return true;
103 if (Lex.getKind() != lltok::Eof)
104 return error(Lex.getLoc(), "expected end of string");
105 return false;
106}
107
109 const SlotMapping *Slots) {
110 restoreParsingState(Slots);
111 Lex.Lex();
112
113 Read = 0;
114 SMLoc Start = Lex.getLoc();
115 Ty = nullptr;
116 if (parseType(Ty))
117 return true;
118 SMLoc End = Lex.getLoc();
119 Read = End.getPointer() - Start.getPointer();
120
121 return false;
122}
123
125 const SlotMapping *Slots) {
126 restoreParsingState(Slots);
127 Lex.Lex();
128
129 Read = 0;
130 SMLoc Start = Lex.getLoc();
131 Result = nullptr;
132 bool Status = parseDIExpressionBody(Result, /*IsDistinct=*/false);
133 SMLoc End = Lex.getLoc();
134 Read = End.getPointer() - Start.getPointer();
135
136 return Status;
137}
138
139void LLParser::restoreParsingState(const SlotMapping *Slots) {
140 if (!Slots)
141 return;
142 NumberedVals = Slots->GlobalValues;
143 NumberedMetadata = Slots->MetadataNodes;
144 for (const auto &I : Slots->NamedTypes)
145 NamedTypes.insert(
146 std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
147 for (const auto &I : Slots->Types)
148 NumberedTypes.insert(
149 std::make_pair(I.first, std::make_pair(I.second, LocTy())));
150}
151
153 // White-list intrinsics that are safe to drop.
155 II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl)
156 return;
157
159 for (Value *V : II->args())
160 if (auto *MV = dyn_cast<MetadataAsValue>(V))
161 if (auto *MD = dyn_cast<MDNode>(MV->getMetadata()))
162 if (MD->isTemporary())
163 MVs.push_back(MV);
164
165 if (!MVs.empty()) {
166 assert(II->use_empty() && "Cannot have uses");
167 II->eraseFromParent();
168
169 // Also remove no longer used MetadataAsValue wrappers.
170 for (MetadataAsValue *MV : MVs)
171 if (MV->use_empty())
172 delete MV;
173 }
174}
175
176void LLParser::dropUnknownMetadataReferences() {
177 auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); };
178 for (Function &F : *M) {
179 F.eraseMetadataIf(Pred);
180 for (Instruction &I : make_early_inc_range(instructions(F))) {
181 I.eraseMetadataIf(Pred);
182
183 if (auto *II = dyn_cast<IntrinsicInst>(&I))
185 }
186 }
187
188 for (GlobalVariable &GV : M->globals())
189 GV.eraseMetadataIf(Pred);
190
191 for (const auto &[ID, Info] : make_early_inc_range(ForwardRefMDNodes)) {
192 // Check whether there is only a single use left, which would be in our
193 // own NumberedMetadata.
194 if (Info.first->getNumTemporaryUses() == 1) {
195 NumberedMetadata.erase(ID);
196 ForwardRefMDNodes.erase(ID);
197 }
198 }
199}
200
201/// validateEndOfModule - Do final validity and basic correctness checks at the
202/// end of the module.
203bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
204 if (!M)
205 return false;
206
207 // We should have already returned an error if we observed both intrinsics and
208 // records in this IR.
209 assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) &&
210 "Mixed debug intrinsics/records seen without a parsing error?");
211
212 // Handle any function attribute group forward references.
213 for (const auto &RAG : ForwardRefAttrGroups) {
214 Value *V = RAG.first;
215 const std::vector<unsigned> &Attrs = RAG.second;
216 AttrBuilder B(Context);
217
218 for (const auto &Attr : Attrs) {
219 auto R = NumberedAttrBuilders.find(Attr);
220 if (R != NumberedAttrBuilders.end())
221 B.merge(R->second);
222 }
223
224 if (Function *Fn = dyn_cast<Function>(V)) {
225 AttributeList AS = Fn->getAttributes();
226 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
227 AS = AS.removeFnAttributes(Context);
228
229 FnAttrs.merge(B);
230
231 // If the alignment was parsed as an attribute, move to the alignment
232 // field.
233 if (MaybeAlign A = FnAttrs.getAlignment()) {
234 Fn->setAlignment(*A);
235 FnAttrs.removeAttribute(Attribute::Alignment);
236 }
237
238 AS = AS.addFnAttributes(Context, FnAttrs);
239 Fn->setAttributes(AS);
240 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
241 AttributeList AS = CI->getAttributes();
242 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
243 AS = AS.removeFnAttributes(Context);
244 FnAttrs.merge(B);
245 AS = AS.addFnAttributes(Context, FnAttrs);
246 CI->setAttributes(AS);
247 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
248 AttributeList AS = II->getAttributes();
249 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
250 AS = AS.removeFnAttributes(Context);
251 FnAttrs.merge(B);
252 AS = AS.addFnAttributes(Context, FnAttrs);
253 II->setAttributes(AS);
254 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(V)) {
255 AttributeList AS = CBI->getAttributes();
256 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
257 AS = AS.removeFnAttributes(Context);
258 FnAttrs.merge(B);
259 AS = AS.addFnAttributes(Context, FnAttrs);
260 CBI->setAttributes(AS);
261 } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
262 AttrBuilder Attrs(M->getContext(), GV->getAttributes());
263 Attrs.merge(B);
264 GV->setAttributes(AttributeSet::get(Context,Attrs));
265 } else {
266 llvm_unreachable("invalid object with forward attribute group reference");
267 }
268 }
269
270 // If there are entries in ForwardRefBlockAddresses at this point, the
271 // function was never defined.
272 if (!ForwardRefBlockAddresses.empty())
273 return error(ForwardRefBlockAddresses.begin()->first.Loc,
274 "expected function name in blockaddress");
275
276 auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef,
277 GlobalValue *FwdRef) {
278 GlobalValue *GV = nullptr;
279 if (GVRef.Kind == ValID::t_GlobalName) {
280 GV = M->getNamedValue(GVRef.StrVal);
281 } else {
282 GV = NumberedVals.get(GVRef.UIntVal);
283 }
284
285 if (!GV)
286 return error(GVRef.Loc, "unknown function '" + GVRef.StrVal +
287 "' referenced by dso_local_equivalent");
288
289 if (!GV->getValueType()->isFunctionTy())
290 return error(GVRef.Loc,
291 "expected a function, alias to function, or ifunc "
292 "in dso_local_equivalent");
293
294 auto *Equiv = DSOLocalEquivalent::get(GV);
295 FwdRef->replaceAllUsesWith(Equiv);
296 FwdRef->eraseFromParent();
297 return false;
298 };
299
300 // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this
301 // point, they are references after the function was defined. Resolve those
302 // now.
303 for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) {
304 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
305 return true;
306 }
307 for (auto &Iter : ForwardRefDSOLocalEquivalentNames) {
308 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
309 return true;
310 }
311 ForwardRefDSOLocalEquivalentIDs.clear();
312 ForwardRefDSOLocalEquivalentNames.clear();
313
314 for (const auto &NT : NumberedTypes)
315 if (NT.second.second.isValid())
316 return error(NT.second.second,
317 "use of undefined type '%" + Twine(NT.first) + "'");
318
319 for (const auto &[Name, TypeInfo] : NamedTypes)
320 if (TypeInfo.second.isValid())
321 return error(TypeInfo.second,
322 "use of undefined type named '" + Name + "'");
323
324 if (!ForwardRefComdats.empty())
325 return error(ForwardRefComdats.begin()->second,
326 "use of undefined comdat '$" +
327 ForwardRefComdats.begin()->first + "'");
328
329 for (const auto &[Name, Info] : make_early_inc_range(ForwardRefVals)) {
330 if (StringRef(Name).starts_with("llvm.")) {
332 // Automatically create declarations for intrinsics. Intrinsics can only
333 // be called directly, so the call function type directly determines the
334 // declaration function type.
335 //
336 // Additionally, automatically add the required mangling suffix to the
337 // intrinsic name. This means that we may replace a single forward
338 // declaration with multiple functions here.
339 for (Use &U : make_early_inc_range(Info.first->uses())) {
340 auto *CB = dyn_cast<CallBase>(U.getUser());
341 if (!CB || !CB->isCallee(&U))
342 return error(Info.second, "intrinsic can only be used as callee");
343
344 SmallVector<Type *> OverloadTys;
345 if (IID != Intrinsic::not_intrinsic &&
346 Intrinsic::getIntrinsicSignature(IID, CB->getFunctionType(),
347 OverloadTys)) {
348 U.set(Intrinsic::getOrInsertDeclaration(M, IID, OverloadTys));
349 } else {
350 // Try to upgrade the intrinsic.
351 Function *TmpF = Function::Create(CB->getFunctionType(),
353 Function *NewF = nullptr;
354 if (!UpgradeIntrinsicFunction(TmpF, NewF)) {
355 if (IID == Intrinsic::not_intrinsic)
356 return error(Info.second, "unknown intrinsic '" + Name + "'");
357 return error(Info.second, "invalid intrinsic signature");
358 }
359
360 U.set(TmpF);
361 UpgradeIntrinsicCall(CB, NewF);
362 if (TmpF->use_empty())
363 TmpF->eraseFromParent();
364 }
365 }
366
367 Info.first->eraseFromParent();
368 ForwardRefVals.erase(Name);
369 continue;
370 }
371
372 // If incomplete IR is allowed, also add declarations for
373 // non-intrinsics.
375 continue;
376
377 auto GetCommonFunctionType = [](Value *V) -> FunctionType * {
378 FunctionType *FTy = nullptr;
379 for (Use &U : V->uses()) {
380 auto *CB = dyn_cast<CallBase>(U.getUser());
381 if (!CB || !CB->isCallee(&U) || (FTy && FTy != CB->getFunctionType()))
382 return nullptr;
383 FTy = CB->getFunctionType();
384 }
385 return FTy;
386 };
387
388 // First check whether this global is only used in calls with the same
389 // type, in which case we'll insert a function. Otherwise, fall back to
390 // using a dummy i8 type.
391 Type *Ty = GetCommonFunctionType(Info.first);
392 if (!Ty)
393 Ty = Type::getInt8Ty(Context);
394
395 GlobalValue *GV;
396 if (auto *FTy = dyn_cast<FunctionType>(Ty))
398 else
399 GV = new GlobalVariable(*M, Ty, /*isConstant*/ false,
401 /*Initializer*/ nullptr, Name);
402 Info.first->replaceAllUsesWith(GV);
403 Info.first->eraseFromParent();
404 ForwardRefVals.erase(Name);
405 }
406
407 if (!ForwardRefVals.empty())
408 return error(ForwardRefVals.begin()->second.second,
409 "use of undefined value '@" + ForwardRefVals.begin()->first +
410 "'");
411
412 if (!ForwardRefValIDs.empty())
413 return error(ForwardRefValIDs.begin()->second.second,
414 "use of undefined value '@" +
415 Twine(ForwardRefValIDs.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 // Resolve metadata cycles.
426 for (auto &N : NumberedMetadata) {
427 if (N.second && !N.second->isResolved())
428 N.second->resolveCycles();
429 }
430
432 NewDistinctSPs.clear();
433
434 for (auto *Inst : InstsWithTBAATag) {
435 MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
436 // With incomplete IR, the tbaa metadata may have been dropped.
438 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
439 if (MD) {
440 auto *UpgradedMD = UpgradeTBAANode(*MD);
441 if (MD != UpgradedMD)
442 Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
443 }
444 }
445
446 // Look for intrinsic functions and CallInst that need to be upgraded. We use
447 // make_early_inc_range here because we may remove some functions.
448 for (Function &F : llvm::make_early_inc_range(*M))
450
451 if (UpgradeDebugInfo)
453
458
459 if (!Slots)
460 return false;
461 // Initialize the slot mapping.
462 // Because by this point we've parsed and validated everything, we can "steal"
463 // the mapping from LLParser as it doesn't need it anymore.
464 Slots->GlobalValues = std::move(NumberedVals);
465 Slots->MetadataNodes = std::move(NumberedMetadata);
466 for (const auto &I : NamedTypes)
467 Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
468 for (const auto &I : NumberedTypes)
469 Slots->Types.insert(std::make_pair(I.first, I.second.first));
470
471 return false;
472}
473
474/// Do final validity and basic correctness checks at the end of the index.
475bool LLParser::validateEndOfIndex() {
476 if (!Index)
477 return false;
478
479 if (!ForwardRefValueInfos.empty())
480 return error(ForwardRefValueInfos.begin()->second.front().second,
481 "use of undefined summary '^" +
482 Twine(ForwardRefValueInfos.begin()->first) + "'");
483
484 if (!ForwardRefAliasees.empty())
485 return error(ForwardRefAliasees.begin()->second.front().second,
486 "use of undefined summary '^" +
487 Twine(ForwardRefAliasees.begin()->first) + "'");
488
489 if (!ForwardRefTypeIds.empty())
490 return error(ForwardRefTypeIds.begin()->second.front().second,
491 "use of undefined type id summary '^" +
492 Twine(ForwardRefTypeIds.begin()->first) + "'");
493
494 return false;
495}
496
497//===----------------------------------------------------------------------===//
498// Top-Level Entities
499//===----------------------------------------------------------------------===//
500
501bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) {
502 // Delay parsing of the data layout string until the target triple is known.
503 // Then, pass both the the target triple and the tentative data layout string
504 // to DataLayoutCallback, allowing to override the DL string.
505 // This enables importing modules with invalid DL strings.
506 std::string TentativeDLStr = M->getDataLayoutStr();
507 LocTy DLStrLoc;
508
509 bool Done = false;
510 while (!Done) {
511 switch (Lex.getKind()) {
512 case lltok::kw_target:
513 if (parseTargetDefinition(TentativeDLStr, DLStrLoc))
514 return true;
515 break;
517 if (parseSourceFileName())
518 return true;
519 break;
520 default:
521 Done = true;
522 }
523 }
524 // Run the override callback to potentially change the data layout string, and
525 // parse the data layout string.
526 if (auto LayoutOverride =
527 DataLayoutCallback(M->getTargetTriple().str(), TentativeDLStr)) {
528 TentativeDLStr = *LayoutOverride;
529 DLStrLoc = {};
530 }
531 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDLStr);
532 if (!MaybeDL)
533 return error(DLStrLoc, toString(MaybeDL.takeError()));
534 M->setDataLayout(MaybeDL.get());
535 return false;
536}
537
538bool LLParser::parseTopLevelEntities() {
539 // If there is no Module, then parse just the summary index entries.
540 if (!M) {
541 while (true) {
542 switch (Lex.getKind()) {
543 case lltok::Eof:
544 return false;
545 case lltok::SummaryID:
546 if (parseSummaryEntry())
547 return true;
548 break;
550 if (parseSourceFileName())
551 return true;
552 break;
553 default:
554 // Skip everything else
555 Lex.Lex();
556 }
557 }
558 }
559 while (true) {
560 switch (Lex.getKind()) {
561 default:
562 return tokError("expected top-level entity");
563 case lltok::Eof: return false;
565 if (parseDeclare())
566 return true;
567 break;
568 case lltok::kw_define:
569 if (parseDefine())
570 return true;
571 break;
572 case lltok::kw_module:
573 if (parseModuleAsm())
574 return true;
575 break;
577 if (parseUnnamedType())
578 return true;
579 break;
580 case lltok::LocalVar:
581 if (parseNamedType())
582 return true;
583 break;
584 case lltok::GlobalID:
585 if (parseUnnamedGlobal())
586 return true;
587 break;
588 case lltok::GlobalVar:
589 if (parseNamedGlobal())
590 return true;
591 break;
592 case lltok::ComdatVar: if (parseComdat()) return true; break;
593 case lltok::exclaim:
594 if (parseStandaloneMetadata())
595 return true;
596 break;
597 case lltok::SummaryID:
598 if (parseSummaryEntry())
599 return true;
600 break;
602 if (parseNamedMetadata())
603 return true;
604 break;
606 if (parseUnnamedAttrGrp())
607 return true;
608 break;
610 if (parseUseListOrder())
611 return true;
612 break;
614 if (parseUseListOrderBB())
615 return true;
616 break;
617 }
618 }
619}
620
621/// toplevelentity
622/// ::= 'module' 'asm' STRINGCONSTANT
623bool LLParser::parseModuleAsm() {
624 assert(Lex.getKind() == lltok::kw_module);
625 Lex.Lex();
626
627 std::string AsmStr;
628 if (parseToken(lltok::kw_asm, "expected 'module asm'") ||
629 parseStringConstant(AsmStr))
630 return true;
631
632 M->appendModuleInlineAsm(AsmStr);
633 return false;
634}
635
636/// toplevelentity
637/// ::= 'target' 'triple' '=' STRINGCONSTANT
638/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
639bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
640 LocTy &DLStrLoc) {
641 assert(Lex.getKind() == lltok::kw_target);
642 std::string Str;
643 switch (Lex.Lex()) {
644 default:
645 return tokError("unknown target property");
646 case lltok::kw_triple:
647 Lex.Lex();
648 if (parseToken(lltok::equal, "expected '=' after target triple") ||
649 parseStringConstant(Str))
650 return true;
651 M->setTargetTriple(Triple(std::move(Str)));
652 return false;
654 Lex.Lex();
655 if (parseToken(lltok::equal, "expected '=' after target datalayout"))
656 return true;
657 DLStrLoc = Lex.getLoc();
658 if (parseStringConstant(TentativeDLStr))
659 return true;
660 return false;
661 }
662}
663
664/// toplevelentity
665/// ::= 'source_filename' '=' STRINGCONSTANT
666bool LLParser::parseSourceFileName() {
667 assert(Lex.getKind() == lltok::kw_source_filename);
668 Lex.Lex();
669 if (parseToken(lltok::equal, "expected '=' after source_filename") ||
670 parseStringConstant(SourceFileName))
671 return true;
672 if (M)
673 M->setSourceFileName(SourceFileName);
674 return false;
675}
676
677/// parseUnnamedType:
678/// ::= LocalVarID '=' 'type' type
679bool LLParser::parseUnnamedType() {
680 LocTy TypeLoc = Lex.getLoc();
681 unsigned TypeID = Lex.getUIntVal();
682 Lex.Lex(); // eat LocalVarID;
683
684 if (parseToken(lltok::equal, "expected '=' after name") ||
685 parseToken(lltok::kw_type, "expected 'type' after '='"))
686 return true;
687
688 Type *Result = nullptr;
689 if (parseStructDefinition(TypeLoc, "", NumberedTypes[TypeID], Result))
690 return true;
691
692 if (!isa<StructType>(Result)) {
693 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
694 if (Entry.first)
695 return error(TypeLoc, "non-struct types may not be recursive");
696 Entry.first = Result;
697 Entry.second = SMLoc();
698 }
699
700 return false;
701}
702
703/// toplevelentity
704/// ::= LocalVar '=' 'type' type
705bool LLParser::parseNamedType() {
706 std::string Name = Lex.getStrVal();
707 LocTy NameLoc = Lex.getLoc();
708 Lex.Lex(); // eat LocalVar.
709
710 if (parseToken(lltok::equal, "expected '=' after name") ||
711 parseToken(lltok::kw_type, "expected 'type' after name"))
712 return true;
713
714 Type *Result = nullptr;
715 if (parseStructDefinition(NameLoc, Name, NamedTypes[Name], Result))
716 return true;
717
718 if (!isa<StructType>(Result)) {
719 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
720 if (Entry.first)
721 return error(NameLoc, "non-struct types may not be recursive");
722 Entry.first = Result;
723 Entry.second = SMLoc();
724 }
725
726 return false;
727}
728
729/// toplevelentity
730/// ::= 'declare' FunctionHeader
731bool LLParser::parseDeclare() {
732 assert(Lex.getKind() == lltok::kw_declare);
733 Lex.Lex();
734
735 std::vector<std::pair<unsigned, MDNode *>> MDs;
736 while (Lex.getKind() == lltok::MetadataVar) {
737 unsigned MDK;
738 MDNode *N;
739 if (parseMetadataAttachment(MDK, N))
740 return true;
741 MDs.push_back({MDK, N});
742 }
743
744 Function *F;
745 unsigned FunctionNumber = -1;
746 SmallVector<unsigned> UnnamedArgNums;
747 if (parseFunctionHeader(F, false, FunctionNumber, UnnamedArgNums))
748 return true;
749 for (auto &MD : MDs)
750 F->addMetadata(MD.first, *MD.second);
751 return false;
752}
753
754/// toplevelentity
755/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
756bool LLParser::parseDefine() {
757 assert(Lex.getKind() == lltok::kw_define);
758
759 FileLoc FunctionStart = getTokLineColumnPos();
760 Lex.Lex();
761
762 Function *F;
763 unsigned FunctionNumber = -1;
764 SmallVector<unsigned> UnnamedArgNums;
765 bool RetValue =
766 parseFunctionHeader(F, true, FunctionNumber, UnnamedArgNums) ||
767 parseOptionalFunctionMetadata(*F) ||
768 parseFunctionBody(*F, FunctionNumber, UnnamedArgNums);
769 if (ParserContext)
770 ParserContext->addFunctionLocation(
771 F, FileLocRange(FunctionStart, getPrevTokEndLineColumnPos()));
772
773 return RetValue;
774}
775
776/// parseGlobalType
777/// ::= 'constant'
778/// ::= 'global'
779bool LLParser::parseGlobalType(bool &IsConstant) {
780 if (Lex.getKind() == lltok::kw_constant)
781 IsConstant = true;
782 else if (Lex.getKind() == lltok::kw_global)
783 IsConstant = false;
784 else {
785 IsConstant = false;
786 return tokError("expected 'global' or 'constant'");
787 }
788 Lex.Lex();
789 return false;
790}
791
792bool LLParser::parseOptionalUnnamedAddr(
793 GlobalVariable::UnnamedAddr &UnnamedAddr) {
794 if (EatIfPresent(lltok::kw_unnamed_addr))
796 else if (EatIfPresent(lltok::kw_local_unnamed_addr))
798 else
799 UnnamedAddr = GlobalValue::UnnamedAddr::None;
800 return false;
801}
802
803/// parseUnnamedGlobal:
804/// OptionalVisibility (ALIAS | IFUNC) ...
805/// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
806/// OptionalDLLStorageClass
807/// ... -> global variable
808/// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
809/// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier
810/// OptionalVisibility
811/// OptionalDLLStorageClass
812/// ... -> global variable
813bool LLParser::parseUnnamedGlobal() {
814 unsigned VarID;
815 std::string Name;
816 LocTy NameLoc = Lex.getLoc();
817
818 // Handle the GlobalID form.
819 if (Lex.getKind() == lltok::GlobalID) {
820 VarID = Lex.getUIntVal();
821 if (checkValueID(NameLoc, "global", "@", NumberedVals.getNext(), VarID))
822 return true;
823
824 Lex.Lex(); // eat GlobalID;
825 if (parseToken(lltok::equal, "expected '=' after name"))
826 return true;
827 } else {
828 VarID = NumberedVals.getNext();
829 }
830
831 bool HasLinkage;
832 unsigned Linkage, Visibility, DLLStorageClass;
833 bool DSOLocal;
835 GlobalVariable::UnnamedAddr UnnamedAddr;
836 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
837 DSOLocal) ||
838 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
839 return true;
840
841 switch (Lex.getKind()) {
842 default:
843 return parseGlobal(Name, VarID, NameLoc, Linkage, HasLinkage, Visibility,
844 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
845 case lltok::kw_alias:
846 case lltok::kw_ifunc:
847 return parseAliasOrIFunc(Name, VarID, NameLoc, Linkage, Visibility,
848 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
849 }
850}
851
852/// parseNamedGlobal:
853/// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
854/// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
855/// OptionalVisibility OptionalDLLStorageClass
856/// ... -> global variable
857bool LLParser::parseNamedGlobal() {
858 assert(Lex.getKind() == lltok::GlobalVar);
859 LocTy NameLoc = Lex.getLoc();
860 std::string Name = Lex.getStrVal();
861 Lex.Lex();
862
863 bool HasLinkage;
864 unsigned Linkage, Visibility, DLLStorageClass;
865 bool DSOLocal;
867 GlobalVariable::UnnamedAddr UnnamedAddr;
868 if (parseToken(lltok::equal, "expected '=' in global variable") ||
869 parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
870 DSOLocal) ||
871 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
872 return true;
873
874 switch (Lex.getKind()) {
875 default:
876 return parseGlobal(Name, -1, NameLoc, Linkage, HasLinkage, Visibility,
877 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
878 case lltok::kw_alias:
879 case lltok::kw_ifunc:
880 return parseAliasOrIFunc(Name, -1, NameLoc, Linkage, Visibility,
881 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
882 }
883}
884
885bool LLParser::parseComdat() {
886 assert(Lex.getKind() == lltok::ComdatVar);
887 std::string Name = Lex.getStrVal();
888 LocTy NameLoc = Lex.getLoc();
889 Lex.Lex();
890
891 if (parseToken(lltok::equal, "expected '=' here"))
892 return true;
893
894 if (parseToken(lltok::kw_comdat, "expected comdat keyword"))
895 return tokError("expected comdat type");
896
898 switch (Lex.getKind()) {
899 default:
900 return tokError("unknown selection kind");
901 case lltok::kw_any:
902 SK = Comdat::Any;
903 break;
906 break;
908 SK = Comdat::Largest;
909 break;
912 break;
914 SK = Comdat::SameSize;
915 break;
916 }
917 Lex.Lex();
918
919 // See if the comdat was forward referenced, if so, use the comdat.
920 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
921 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
922 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
923 return error(NameLoc, "redefinition of comdat '$" + Name + "'");
924
925 Comdat *C;
926 if (I != ComdatSymTab.end())
927 C = &I->second;
928 else
929 C = M->getOrInsertComdat(Name);
930 C->setSelectionKind(SK);
931
932 return false;
933}
934
935// MDString:
936// ::= '!' STRINGCONSTANT
937bool LLParser::parseMDString(MDString *&Result) {
938 std::string Str;
939 if (parseStringConstant(Str))
940 return true;
941 Result = MDString::get(Context, Str);
942 return false;
943}
944
945// MDNode:
946// ::= '!' MDNodeNumber
947bool LLParser::parseMDNodeID(MDNode *&Result) {
948 // !{ ..., !42, ... }
949 LocTy IDLoc = Lex.getLoc();
950 unsigned MID = 0;
951 if (parseUInt32(MID))
952 return true;
953
954 // If not a forward reference, just return it now.
955 auto [It, Inserted] = NumberedMetadata.try_emplace(MID);
956 if (!Inserted) {
957 Result = It->second;
958 return false;
959 }
960
961 // Otherwise, create MDNode forward reference.
962 auto &FwdRef = ForwardRefMDNodes[MID];
963 FwdRef = std::make_pair(MDTuple::getTemporary(Context, {}), IDLoc);
964
965 Result = FwdRef.first.get();
966 It->second.reset(Result);
967 return false;
968}
969
970/// parseNamedMetadata:
971/// !foo = !{ !1, !2 }
972bool LLParser::parseNamedMetadata() {
973 assert(Lex.getKind() == lltok::MetadataVar);
974 std::string Name = Lex.getStrVal();
975 Lex.Lex();
976
977 if (parseToken(lltok::equal, "expected '=' here") ||
978 parseToken(lltok::exclaim, "Expected '!' here") ||
979 parseToken(lltok::lbrace, "Expected '{' here"))
980 return true;
981
982 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
983 if (Lex.getKind() != lltok::rbrace)
984 do {
985 MDNode *N = nullptr;
986 // parse DIExpressions inline as a special case. They are still MDNodes,
987 // so they can still appear in named metadata. Remove this logic if they
988 // become plain Metadata.
989 if (Lex.getKind() == lltok::MetadataVar &&
990 Lex.getStrVal() == "DIExpression") {
991 if (parseDIExpression(N, /*IsDistinct=*/false))
992 return true;
993 // DIArgLists should only appear inline in a function, as they may
994 // contain LocalAsMetadata arguments which require a function context.
995 } else if (Lex.getKind() == lltok::MetadataVar &&
996 Lex.getStrVal() == "DIArgList") {
997 return tokError("found DIArgList outside of function");
998 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
999 parseMDNodeID(N)) {
1000 return true;
1001 }
1002 NMD->addOperand(N);
1003 } while (EatIfPresent(lltok::comma));
1004
1005 return parseToken(lltok::rbrace, "expected end of metadata node");
1006}
1007
1008/// parseStandaloneMetadata:
1009/// !42 = !{...}
1010bool LLParser::parseStandaloneMetadata() {
1011 assert(Lex.getKind() == lltok::exclaim);
1012 Lex.Lex();
1013 unsigned MetadataID = 0;
1014
1015 MDNode *Init;
1016 if (parseUInt32(MetadataID) || parseToken(lltok::equal, "expected '=' here"))
1017 return true;
1018
1019 // Detect common error, from old metadata syntax.
1020 if (Lex.getKind() == lltok::Type)
1021 return tokError("unexpected type in metadata definition");
1022
1023 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
1024 if (Lex.getKind() == lltok::MetadataVar) {
1025 if (parseSpecializedMDNode(Init, IsDistinct))
1026 return true;
1027 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
1028 parseMDTuple(Init, IsDistinct))
1029 return true;
1030
1031 // See if this was forward referenced, if so, handle it.
1032 auto FI = ForwardRefMDNodes.find(MetadataID);
1033 if (FI != ForwardRefMDNodes.end()) {
1034 auto *ToReplace = FI->second.first.get();
1035 // DIAssignID has its own special forward-reference "replacement" for
1036 // attachments (the temporary attachments are never actually attached).
1037 if (isa<DIAssignID>(Init)) {
1038 for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) {
1039 assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) &&
1040 "Inst unexpectedly already has DIAssignID attachment");
1041 Inst->setMetadata(LLVMContext::MD_DIAssignID, Init);
1042 }
1043 }
1044
1045 ToReplace->replaceAllUsesWith(Init);
1046 ForwardRefMDNodes.erase(FI);
1047
1048 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
1049 } else {
1050 auto [It, Inserted] = NumberedMetadata.try_emplace(MetadataID);
1051 if (!Inserted)
1052 return tokError("Metadata id is already used");
1053 It->second.reset(Init);
1054 }
1055
1056 return false;
1057}
1058
1059// Skips a single module summary entry.
1060bool LLParser::skipModuleSummaryEntry() {
1061 // Each module summary entry consists of a tag for the entry
1062 // type, followed by a colon, then the fields which may be surrounded by
1063 // nested sets of parentheses. The "tag:" looks like a Label. Once parsing
1064 // support is in place we will look for the tokens corresponding to the
1065 // expected tags.
1066 if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module &&
1067 Lex.getKind() != lltok::kw_typeid && Lex.getKind() != lltok::kw_flags &&
1068 Lex.getKind() != lltok::kw_blockcount)
1069 return tokError(
1070 "Expected 'gv', 'module', 'typeid', 'flags' or 'blockcount' at the "
1071 "start of summary entry");
1072 if (Lex.getKind() == lltok::kw_flags)
1073 return parseSummaryIndexFlags();
1074 if (Lex.getKind() == lltok::kw_blockcount)
1075 return parseBlockCount();
1076 Lex.Lex();
1077 if (parseToken(lltok::colon, "expected ':' at start of summary entry") ||
1078 parseToken(lltok::lparen, "expected '(' at start of summary entry"))
1079 return true;
1080 // Now walk through the parenthesized entry, until the number of open
1081 // parentheses goes back down to 0 (the first '(' was parsed above).
1082 unsigned NumOpenParen = 1;
1083 do {
1084 switch (Lex.getKind()) {
1085 case lltok::lparen:
1086 NumOpenParen++;
1087 break;
1088 case lltok::rparen:
1089 NumOpenParen--;
1090 break;
1091 case lltok::Eof:
1092 return tokError("found end of file while parsing summary entry");
1093 default:
1094 // Skip everything in between parentheses.
1095 break;
1096 }
1097 Lex.Lex();
1098 } while (NumOpenParen > 0);
1099 return false;
1100}
1101
1102/// SummaryEntry
1103/// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
1104bool LLParser::parseSummaryEntry() {
1105 assert(Lex.getKind() == lltok::SummaryID);
1106 unsigned SummaryID = Lex.getUIntVal();
1107
1108 // For summary entries, colons should be treated as distinct tokens,
1109 // not an indication of the end of a label token.
1110 Lex.setIgnoreColonInIdentifiers(true);
1111
1112 Lex.Lex();
1113 if (parseToken(lltok::equal, "expected '=' here"))
1114 return true;
1115
1116 // If we don't have an index object, skip the summary entry.
1117 if (!Index)
1118 return skipModuleSummaryEntry();
1119
1120 bool result = false;
1121 switch (Lex.getKind()) {
1122 case lltok::kw_gv:
1123 result = parseGVEntry(SummaryID);
1124 break;
1125 case lltok::kw_module:
1126 result = parseModuleEntry(SummaryID);
1127 break;
1128 case lltok::kw_typeid:
1129 result = parseTypeIdEntry(SummaryID);
1130 break;
1132 result = parseTypeIdCompatibleVtableEntry(SummaryID);
1133 break;
1134 case lltok::kw_flags:
1135 result = parseSummaryIndexFlags();
1136 break;
1138 result = parseBlockCount();
1139 break;
1140 default:
1141 result = error(Lex.getLoc(), "unexpected summary kind");
1142 break;
1143 }
1144 Lex.setIgnoreColonInIdentifiers(false);
1145 return result;
1146}
1147
1156
1157// If there was an explicit dso_local, update GV. In the absence of an explicit
1158// dso_local we keep the default value.
1159static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
1160 if (DSOLocal)
1161 GV.setDSOLocal(true);
1162}
1163
1164/// parseAliasOrIFunc:
1165/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1166/// OptionalVisibility OptionalDLLStorageClass
1167/// OptionalThreadLocal OptionalUnnamedAddr
1168/// 'alias|ifunc' AliaseeOrResolver SymbolAttrs*
1169///
1170/// AliaseeOrResolver
1171/// ::= TypeAndValue
1172///
1173/// SymbolAttrs
1174/// ::= ',' 'partition' StringConstant
1175///
1176/// Everything through OptionalUnnamedAddr has already been parsed.
1177///
1178bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID,
1179 LocTy NameLoc, unsigned L, unsigned Visibility,
1180 unsigned DLLStorageClass, bool DSOLocal,
1182 GlobalVariable::UnnamedAddr UnnamedAddr) {
1183 bool IsAlias;
1184 if (Lex.getKind() == lltok::kw_alias)
1185 IsAlias = true;
1186 else if (Lex.getKind() == lltok::kw_ifunc)
1187 IsAlias = false;
1188 else
1189 llvm_unreachable("Not an alias or ifunc!");
1190 Lex.Lex();
1191
1193
1194 if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
1195 return error(NameLoc, "invalid linkage type for alias");
1196
1197 if (!isValidVisibilityForLinkage(Visibility, L))
1198 return error(NameLoc,
1199 "symbol with local linkage must have default visibility");
1200
1201 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, L))
1202 return error(NameLoc,
1203 "symbol with local linkage cannot have a DLL storage class");
1204
1205 Type *Ty;
1206 LocTy ExplicitTypeLoc = Lex.getLoc();
1207 if (parseType(Ty) ||
1208 parseToken(lltok::comma, "expected comma after alias or ifunc's type"))
1209 return true;
1210
1211 Constant *Aliasee;
1212 LocTy AliaseeLoc = Lex.getLoc();
1213 if (Lex.getKind() != lltok::kw_bitcast &&
1214 Lex.getKind() != lltok::kw_getelementptr &&
1215 Lex.getKind() != lltok::kw_addrspacecast &&
1216 Lex.getKind() != lltok::kw_inttoptr) {
1217 if (parseGlobalTypeAndValue(Aliasee))
1218 return true;
1219 } else {
1220 // The bitcast dest type is not present, it is implied by the dest type.
1221 ValID ID;
1222 if (parseValID(ID, /*PFS=*/nullptr))
1223 return true;
1224 if (ID.Kind != ValID::t_Constant)
1225 return error(AliaseeLoc, "invalid aliasee");
1226 Aliasee = ID.ConstantVal;
1227 }
1228
1229 Type *AliaseeType = Aliasee->getType();
1230 auto *PTy = dyn_cast<PointerType>(AliaseeType);
1231 if (!PTy)
1232 return error(AliaseeLoc, "An alias or ifunc must have pointer type");
1233 unsigned AddrSpace = PTy->getAddressSpace();
1234
1235 GlobalValue *GVal = nullptr;
1236
1237 // See if the alias was forward referenced, if so, prepare to replace the
1238 // forward reference.
1239 if (!Name.empty()) {
1240 auto I = ForwardRefVals.find(Name);
1241 if (I != ForwardRefVals.end()) {
1242 GVal = I->second.first;
1243 ForwardRefVals.erase(Name);
1244 } else if (M->getNamedValue(Name)) {
1245 return error(NameLoc, "redefinition of global '@" + Name + "'");
1246 }
1247 } else {
1248 auto I = ForwardRefValIDs.find(NameID);
1249 if (I != ForwardRefValIDs.end()) {
1250 GVal = I->second.first;
1251 ForwardRefValIDs.erase(I);
1252 }
1253 }
1254
1255 // Okay, create the alias/ifunc but do not insert it into the module yet.
1256 std::unique_ptr<GlobalAlias> GA;
1257 std::unique_ptr<GlobalIFunc> GI;
1258 GlobalValue *GV;
1259 if (IsAlias) {
1260 GA.reset(GlobalAlias::create(Ty, AddrSpace, Linkage, Name, Aliasee,
1261 /*Parent=*/nullptr));
1262 GV = GA.get();
1263 } else {
1264 GI.reset(GlobalIFunc::create(Ty, AddrSpace, Linkage, Name, Aliasee,
1265 /*Parent=*/nullptr));
1266 GV = GI.get();
1267 }
1268 GV->setThreadLocalMode(TLM);
1271 GV->setUnnamedAddr(UnnamedAddr);
1272 maybeSetDSOLocal(DSOLocal, *GV);
1273
1274 // At this point we've parsed everything except for the IndirectSymbolAttrs.
1275 // Now parse them if there are any.
1276 while (Lex.getKind() == lltok::comma) {
1277 Lex.Lex();
1278
1279 if (Lex.getKind() == lltok::kw_partition) {
1280 Lex.Lex();
1281 GV->setPartition(Lex.getStrVal());
1282 if (parseToken(lltok::StringConstant, "expected partition string"))
1283 return true;
1284 } else if (!IsAlias && Lex.getKind() == lltok::MetadataVar) {
1285 if (parseGlobalObjectMetadataAttachment(*GI))
1286 return true;
1287 } else {
1288 return tokError("unknown alias or ifunc property!");
1289 }
1290 }
1291
1292 if (Name.empty())
1293 NumberedVals.add(NameID, GV);
1294
1295 if (GVal) {
1296 // Verify that types agree.
1297 if (GVal->getType() != GV->getType())
1298 return error(
1299 ExplicitTypeLoc,
1300 "forward reference and definition of alias have different types");
1301
1302 // If they agree, just RAUW the old value with the alias and remove the
1303 // forward ref info.
1304 GVal->replaceAllUsesWith(GV);
1305 GVal->eraseFromParent();
1306 }
1307
1308 // Insert into the module, we know its name won't collide now.
1309 if (IsAlias)
1310 M->insertAlias(GA.release());
1311 else
1312 M->insertIFunc(GI.release());
1313 assert(GV->getName() == Name && "Should not be a name conflict!");
1314
1315 return false;
1316}
1317
1318static bool isSanitizer(lltok::Kind Kind) {
1319 switch (Kind) {
1322 case lltok::kw_sanitize_memtag:
1324 return true;
1325 default:
1326 return false;
1327 }
1328}
1329
1330bool LLParser::parseSanitizer(GlobalVariable *GV) {
1331 using SanitizerMetadata = GlobalValue::SanitizerMetadata;
1333 if (GV->hasSanitizerMetadata())
1334 Meta = GV->getSanitizerMetadata();
1335
1336 switch (Lex.getKind()) {
1338 Meta.NoAddress = true;
1339 break;
1341 Meta.NoHWAddress = true;
1342 break;
1343 case lltok::kw_sanitize_memtag:
1344 Meta.Memtag = true;
1345 break;
1347 Meta.IsDynInit = true;
1348 break;
1349 default:
1350 return tokError("non-sanitizer token passed to LLParser::parseSanitizer()");
1351 }
1352 GV->setSanitizerMetadata(Meta);
1353 Lex.Lex();
1354 return false;
1355}
1356
1357/// parseGlobal
1358/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1359/// OptionalVisibility OptionalDLLStorageClass
1360/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
1361/// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
1362/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
1363/// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
1364/// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
1365/// Const OptionalAttrs
1366///
1367/// Everything up to and including OptionalUnnamedAddr has been parsed
1368/// already.
1369///
1370bool LLParser::parseGlobal(const std::string &Name, unsigned NameID,
1371 LocTy NameLoc, unsigned Linkage, bool HasLinkage,
1372 unsigned Visibility, unsigned DLLStorageClass,
1373 bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
1374 GlobalVariable::UnnamedAddr UnnamedAddr) {
1375 if (!isValidVisibilityForLinkage(Visibility, Linkage))
1376 return error(NameLoc,
1377 "symbol with local linkage must have default visibility");
1378
1379 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
1380 return error(NameLoc,
1381 "symbol with local linkage cannot have a DLL storage class");
1382
1383 unsigned AddrSpace;
1384 bool IsConstant, IsExternallyInitialized;
1385 LocTy IsExternallyInitializedLoc;
1386 LocTy TyLoc;
1387
1388 Type *Ty = nullptr;
1389 if (parseOptionalAddrSpace(AddrSpace) ||
1390 parseOptionalToken(lltok::kw_externally_initialized,
1391 IsExternallyInitialized,
1392 &IsExternallyInitializedLoc) ||
1393 parseGlobalType(IsConstant) || parseType(Ty, TyLoc))
1394 return true;
1395
1396 // If the linkage is specified and is external, then no initializer is
1397 // present.
1398 Constant *Init = nullptr;
1399 if (!HasLinkage ||
1402 if (parseGlobalValue(Ty, Init))
1403 return true;
1404 }
1405
1407 return error(TyLoc, "invalid type for global variable");
1408
1409 GlobalValue *GVal = nullptr;
1410
1411 // See if the global was forward referenced, if so, use the global.
1412 if (!Name.empty()) {
1413 auto I = ForwardRefVals.find(Name);
1414 if (I != ForwardRefVals.end()) {
1415 GVal = I->second.first;
1416 ForwardRefVals.erase(I);
1417 } else if (M->getNamedValue(Name)) {
1418 return error(NameLoc, "redefinition of global '@" + Name + "'");
1419 }
1420 } else {
1421 // Handle @"", where a name is syntactically specified, but semantically
1422 // missing.
1423 if (NameID == (unsigned)-1)
1424 NameID = NumberedVals.getNext();
1425
1426 auto I = ForwardRefValIDs.find(NameID);
1427 if (I != ForwardRefValIDs.end()) {
1428 GVal = I->second.first;
1429 ForwardRefValIDs.erase(I);
1430 }
1431 }
1432
1433 GlobalVariable *GV = new GlobalVariable(
1434 *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr,
1436
1437 if (Name.empty())
1438 NumberedVals.add(NameID, GV);
1439
1440 // Set the parsed properties on the global.
1441 if (Init)
1442 GV->setInitializer(Init);
1443 GV->setConstant(IsConstant);
1445 maybeSetDSOLocal(DSOLocal, *GV);
1448 GV->setExternallyInitialized(IsExternallyInitialized);
1449 GV->setThreadLocalMode(TLM);
1450 GV->setUnnamedAddr(UnnamedAddr);
1451
1452 if (GVal) {
1453 if (GVal->getAddressSpace() != AddrSpace)
1454 return error(
1455 TyLoc,
1456 "forward reference and definition of global have different types");
1457
1458 GVal->replaceAllUsesWith(GV);
1459 GVal->eraseFromParent();
1460 }
1461
1462 // parse attributes on the global.
1463 while (Lex.getKind() == lltok::comma) {
1464 Lex.Lex();
1465
1466 if (Lex.getKind() == lltok::kw_section) {
1467 Lex.Lex();
1468 GV->setSection(Lex.getStrVal());
1469 if (parseToken(lltok::StringConstant, "expected global section string"))
1470 return true;
1471 } else if (Lex.getKind() == lltok::kw_partition) {
1472 Lex.Lex();
1473 GV->setPartition(Lex.getStrVal());
1474 if (parseToken(lltok::StringConstant, "expected partition string"))
1475 return true;
1476 } else if (Lex.getKind() == lltok::kw_align) {
1477 MaybeAlign Alignment;
1478 if (parseOptionalAlignment(Alignment))
1479 return true;
1480 if (Alignment)
1481 GV->setAlignment(*Alignment);
1482 } else if (Lex.getKind() == lltok::kw_code_model) {
1484 if (parseOptionalCodeModel(CodeModel))
1485 return true;
1486 GV->setCodeModel(CodeModel);
1487 } else if (Lex.getKind() == lltok::MetadataVar) {
1488 if (parseGlobalObjectMetadataAttachment(*GV))
1489 return true;
1490 } else if (isSanitizer(Lex.getKind())) {
1491 if (parseSanitizer(GV))
1492 return true;
1493 } else {
1494 Comdat *C;
1495 if (parseOptionalComdat(Name, C))
1496 return true;
1497 if (C)
1498 GV->setComdat(C);
1499 else
1500 return tokError("unknown global variable property!");
1501 }
1502 }
1503
1504 AttrBuilder Attrs(M->getContext());
1505 LocTy BuiltinLoc;
1506 std::vector<unsigned> FwdRefAttrGrps;
1507 if (parseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
1508 return true;
1509 if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
1510 GV->setAttributes(AttributeSet::get(Context, Attrs));
1511 ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
1512 }
1513
1514 return false;
1515}
1516
1517/// parseUnnamedAttrGrp
1518/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
1519bool LLParser::parseUnnamedAttrGrp() {
1520 assert(Lex.getKind() == lltok::kw_attributes);
1521 LocTy AttrGrpLoc = Lex.getLoc();
1522 Lex.Lex();
1523
1524 if (Lex.getKind() != lltok::AttrGrpID)
1525 return tokError("expected attribute group id");
1526
1527 unsigned VarID = Lex.getUIntVal();
1528 std::vector<unsigned> unused;
1529 LocTy BuiltinLoc;
1530 Lex.Lex();
1531
1532 if (parseToken(lltok::equal, "expected '=' here") ||
1533 parseToken(lltok::lbrace, "expected '{' here"))
1534 return true;
1535
1536 auto R = NumberedAttrBuilders.find(VarID);
1537 if (R == NumberedAttrBuilders.end())
1538 R = NumberedAttrBuilders.emplace(VarID, AttrBuilder(M->getContext())).first;
1539
1540 if (parseFnAttributeValuePairs(R->second, unused, true, BuiltinLoc) ||
1541 parseToken(lltok::rbrace, "expected end of attribute group"))
1542 return true;
1543
1544 if (!R->second.hasAttributes())
1545 return error(AttrGrpLoc, "attribute group has no attributes");
1546
1547 return false;
1548}
1549
1551 switch (Kind) {
1552#define GET_ATTR_NAMES
1553#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
1554 case lltok::kw_##DISPLAY_NAME: \
1555 return Attribute::ENUM_NAME;
1556#include "llvm/IR/Attributes.inc"
1557 default:
1558 return Attribute::None;
1559 }
1560}
1561
1562bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
1563 bool InAttrGroup) {
1564 if (Attribute::isTypeAttrKind(Attr))
1565 return parseRequiredTypeAttr(B, Lex.getKind(), Attr);
1566
1567 switch (Attr) {
1568 case Attribute::Alignment: {
1569 MaybeAlign Alignment;
1570 if (InAttrGroup) {
1571 uint32_t Value = 0;
1572 Lex.Lex();
1573 if (parseToken(lltok::equal, "expected '=' here") || parseUInt32(Value))
1574 return true;
1575 Alignment = Align(Value);
1576 } else {
1577 if (parseOptionalAlignment(Alignment, true))
1578 return true;
1579 }
1580 B.addAlignmentAttr(Alignment);
1581 return false;
1582 }
1583 case Attribute::StackAlignment: {
1584 unsigned Alignment;
1585 if (InAttrGroup) {
1586 Lex.Lex();
1587 if (parseToken(lltok::equal, "expected '=' here") ||
1588 parseUInt32(Alignment))
1589 return true;
1590 } else {
1591 if (parseOptionalStackAlignment(Alignment))
1592 return true;
1593 }
1594 B.addStackAlignmentAttr(Alignment);
1595 return false;
1596 }
1597 case Attribute::AllocSize: {
1598 unsigned ElemSizeArg;
1599 std::optional<unsigned> NumElemsArg;
1600 if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
1601 return true;
1602 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1603 return false;
1604 }
1605 case Attribute::VScaleRange: {
1606 unsigned MinValue, MaxValue;
1607 if (parseVScaleRangeArguments(MinValue, MaxValue))
1608 return true;
1609 B.addVScaleRangeAttr(MinValue,
1610 MaxValue > 0 ? MaxValue : std::optional<unsigned>());
1611 return false;
1612 }
1613 case Attribute::Dereferenceable: {
1614 std::optional<uint64_t> Bytes;
1615 if (parseOptionalAttrBytes(lltok::kw_dereferenceable, Bytes))
1616 return true;
1617 assert(Bytes.has_value());
1618 B.addDereferenceableAttr(Bytes.value());
1619 return false;
1620 }
1621 case Attribute::DeadOnReturn: {
1622 std::optional<uint64_t> Bytes;
1623 if (parseOptionalAttrBytes(lltok::kw_dead_on_return, Bytes,
1624 /*ErrorNoBytes=*/false))
1625 return true;
1626 if (Bytes.has_value()) {
1627 B.addDeadOnReturnAttr(DeadOnReturnInfo(Bytes.value()));
1628 } else {
1629 B.addDeadOnReturnAttr(DeadOnReturnInfo());
1630 }
1631 return false;
1632 }
1633 case Attribute::DereferenceableOrNull: {
1634 std::optional<uint64_t> Bytes;
1635 if (parseOptionalAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1636 return true;
1637 assert(Bytes.has_value());
1638 B.addDereferenceableOrNullAttr(Bytes.value());
1639 return false;
1640 }
1641 case Attribute::UWTable: {
1643 if (parseOptionalUWTableKind(Kind))
1644 return true;
1645 B.addUWTableAttr(Kind);
1646 return false;
1647 }
1648 case Attribute::AllocKind: {
1650 if (parseAllocKind(Kind))
1651 return true;
1652 B.addAllocKindAttr(Kind);
1653 return false;
1654 }
1655 case Attribute::Memory: {
1656 std::optional<MemoryEffects> ME = parseMemoryAttr();
1657 if (!ME)
1658 return true;
1659 B.addMemoryAttr(*ME);
1660 return false;
1661 }
1662 case Attribute::DenormalFPEnv: {
1663 std::optional<DenormalFPEnv> Mode = parseDenormalFPEnvAttr();
1664 if (!Mode)
1665 return true;
1666
1667 B.addDenormalFPEnvAttr(*Mode);
1668 return false;
1669 }
1670 case Attribute::NoFPClass: {
1671 if (FPClassTest NoFPClass =
1672 static_cast<FPClassTest>(parseNoFPClassAttr())) {
1673 B.addNoFPClassAttr(NoFPClass);
1674 return false;
1675 }
1676
1677 return true;
1678 }
1679 case Attribute::Range:
1680 return parseRangeAttr(B);
1681 case Attribute::Initializes:
1682 return parseInitializesAttr(B);
1683 case Attribute::Captures:
1684 return parseCapturesAttr(B);
1685 default:
1686 B.addAttribute(Attr);
1687 Lex.Lex();
1688 return false;
1689 }
1690}
1691
1693 switch (Kind) {
1694 case lltok::kw_readnone:
1695 ME &= MemoryEffects::none();
1696 return true;
1697 case lltok::kw_readonly:
1699 return true;
1700 case lltok::kw_writeonly:
1702 return true;
1705 return true;
1708 return true;
1711 return true;
1712 default:
1713 return false;
1714 }
1715}
1716
1717/// parseFnAttributeValuePairs
1718/// ::= <attr> | <attr> '=' <value>
1719bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
1720 std::vector<unsigned> &FwdRefAttrGrps,
1721 bool InAttrGrp, LocTy &BuiltinLoc) {
1722 bool HaveError = false;
1723
1724 B.clear();
1725
1727 while (true) {
1728 lltok::Kind Token = Lex.getKind();
1729 if (Token == lltok::rbrace)
1730 break; // Finished.
1731
1732 if (Token == lltok::StringConstant) {
1733 if (parseStringAttribute(B))
1734 return true;
1735 continue;
1736 }
1737
1738 if (Token == lltok::AttrGrpID) {
1739 // Allow a function to reference an attribute group:
1740 //
1741 // define void @foo() #1 { ... }
1742 if (InAttrGrp) {
1743 HaveError |= error(
1744 Lex.getLoc(),
1745 "cannot have an attribute group reference in an attribute group");
1746 } else {
1747 // Save the reference to the attribute group. We'll fill it in later.
1748 FwdRefAttrGrps.push_back(Lex.getUIntVal());
1749 }
1750 Lex.Lex();
1751 continue;
1752 }
1753
1754 SMLoc Loc = Lex.getLoc();
1755 if (Token == lltok::kw_builtin)
1756 BuiltinLoc = Loc;
1757
1758 if (upgradeMemoryAttr(ME, Token)) {
1759 Lex.Lex();
1760 continue;
1761 }
1762
1764 if (Attr == Attribute::None) {
1765 if (!InAttrGrp)
1766 break;
1767 return error(Lex.getLoc(), "unterminated attribute group");
1768 }
1769
1770 if (parseEnumAttribute(Attr, B, InAttrGrp))
1771 return true;
1772
1773 // As a hack, we allow function alignment to be initially parsed as an
1774 // attribute on a function declaration/definition or added to an attribute
1775 // group and later moved to the alignment field.
1776 if (!Attribute::canUseAsFnAttr(Attr) && Attr != Attribute::Alignment)
1777 HaveError |= error(Loc, "this attribute does not apply to functions");
1778 }
1779
1780 if (ME != MemoryEffects::unknown())
1781 B.addMemoryAttr(ME);
1782 return HaveError;
1783}
1784
1785//===----------------------------------------------------------------------===//
1786// GlobalValue Reference/Resolution Routines.
1787//===----------------------------------------------------------------------===//
1788
1790 // The used global type does not matter. We will later RAUW it with a
1791 // global/function of the correct type.
1792 return new GlobalVariable(*M, Type::getInt8Ty(M->getContext()), false,
1795 PTy->getAddressSpace());
1796}
1797
1798Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
1799 Value *Val) {
1800 Type *ValTy = Val->getType();
1801 if (ValTy == Ty)
1802 return Val;
1803 if (Ty->isLabelTy())
1804 error(Loc, "'" + Name + "' is not a basic block");
1805 else
1806 error(Loc, "'" + Name + "' defined with type '" +
1807 getTypeString(Val->getType()) + "' but expected '" +
1808 getTypeString(Ty) + "'");
1809 return nullptr;
1810}
1811
1812/// getGlobalVal - Get a value with the specified name or ID, creating a
1813/// forward reference record if needed. This can return null if the value
1814/// exists but does not have the right type.
1815GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty,
1816 LocTy Loc) {
1818 if (!PTy) {
1819 error(Loc, "global variable reference must have pointer type");
1820 return nullptr;
1821 }
1822
1823 // Look this name up in the normal function symbol table.
1824 GlobalValue *Val =
1825 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
1826
1827 // If this is a forward reference for the value, see if we already created a
1828 // forward ref record.
1829 if (!Val) {
1830 auto I = ForwardRefVals.find(Name);
1831 if (I != ForwardRefVals.end())
1832 Val = I->second.first;
1833 }
1834
1835 // If we have the value in the symbol table or fwd-ref table, return it.
1836 if (Val)
1838 checkValidVariableType(Loc, "@" + Name, Ty, Val));
1839
1840 // Otherwise, create a new forward reference for this value and remember it.
1841 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1842 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1843 return FwdVal;
1844}
1845
1846GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1848 if (!PTy) {
1849 error(Loc, "global variable reference must have pointer type");
1850 return nullptr;
1851 }
1852
1853 GlobalValue *Val = NumberedVals.get(ID);
1854
1855 // If this is a forward reference for the value, see if we already created a
1856 // forward ref record.
1857 if (!Val) {
1858 auto I = ForwardRefValIDs.find(ID);
1859 if (I != ForwardRefValIDs.end())
1860 Val = I->second.first;
1861 }
1862
1863 // If we have the value in the symbol table or fwd-ref table, return it.
1864 if (Val)
1866 checkValidVariableType(Loc, "@" + Twine(ID), Ty, Val));
1867
1868 // Otherwise, create a new forward reference for this value and remember it.
1869 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1870 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1871 return FwdVal;
1872}
1873
1874//===----------------------------------------------------------------------===//
1875// Comdat Reference/Resolution Routines.
1876//===----------------------------------------------------------------------===//
1877
1878Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1879 // Look this name up in the comdat symbol table.
1880 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1881 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1882 if (I != ComdatSymTab.end())
1883 return &I->second;
1884
1885 // Otherwise, create a new forward reference for this value and remember it.
1886 Comdat *C = M->getOrInsertComdat(Name);
1887 ForwardRefComdats[Name] = Loc;
1888 return C;
1889}
1890
1891//===----------------------------------------------------------------------===//
1892// Helper Routines.
1893//===----------------------------------------------------------------------===//
1894
1895/// parseToken - If the current token has the specified kind, eat it and return
1896/// success. Otherwise, emit the specified error and return failure.
1897bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) {
1898 if (Lex.getKind() != T)
1899 return tokError(ErrMsg);
1900 Lex.Lex();
1901 return false;
1902}
1903
1904/// parseStringConstant
1905/// ::= StringConstant
1906bool LLParser::parseStringConstant(std::string &Result) {
1907 if (Lex.getKind() != lltok::StringConstant)
1908 return tokError("expected string constant");
1909 Result = Lex.getStrVal();
1910 Lex.Lex();
1911 return false;
1912}
1913
1914/// parseUInt32
1915/// ::= uint32
1916bool LLParser::parseUInt32(uint32_t &Val) {
1917 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1918 return tokError("expected integer");
1919 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1920 if (Val64 != unsigned(Val64))
1921 return tokError("expected 32-bit integer (too large)");
1922 Val = Val64;
1923 Lex.Lex();
1924 return false;
1925}
1926
1927/// parseUInt64
1928/// ::= uint64
1929bool LLParser::parseUInt64(uint64_t &Val) {
1930 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1931 return tokError("expected integer");
1932 Val = Lex.getAPSIntVal().getLimitedValue();
1933 Lex.Lex();
1934 return false;
1935}
1936
1937/// parseTLSModel
1938/// := 'localdynamic'
1939/// := 'initialexec'
1940/// := 'localexec'
1941bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1942 switch (Lex.getKind()) {
1943 default:
1944 return tokError("expected localdynamic, initialexec or localexec");
1947 break;
1950 break;
1953 break;
1954 }
1955
1956 Lex.Lex();
1957 return false;
1958}
1959
1960/// parseOptionalThreadLocal
1961/// := /*empty*/
1962/// := 'thread_local'
1963/// := 'thread_local' '(' tlsmodel ')'
1964bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1966 if (!EatIfPresent(lltok::kw_thread_local))
1967 return false;
1968
1970 if (Lex.getKind() == lltok::lparen) {
1971 Lex.Lex();
1972 return parseTLSModel(TLM) ||
1973 parseToken(lltok::rparen, "expected ')' after thread local model");
1974 }
1975 return false;
1976}
1977
1978/// parseOptionalAddrSpace
1979/// := /*empty*/
1980/// := 'addrspace' '(' uint32 ')'
1981bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
1982 AddrSpace = DefaultAS;
1983 if (!EatIfPresent(lltok::kw_addrspace))
1984 return false;
1985
1986 auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool {
1987 if (Lex.getKind() == lltok::StringConstant) {
1988 const std::string &AddrSpaceStr = Lex.getStrVal();
1989 if (AddrSpaceStr == "A") {
1990 AddrSpace = M->getDataLayout().getAllocaAddrSpace();
1991 } else if (AddrSpaceStr == "G") {
1992 AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace();
1993 } else if (AddrSpaceStr == "P") {
1994 AddrSpace = M->getDataLayout().getProgramAddressSpace();
1995 } else if (std::optional<unsigned> AS =
1996 M->getDataLayout().getNamedAddressSpace(AddrSpaceStr)) {
1997 AddrSpace = *AS;
1998 } else {
1999 return tokError("invalid symbolic addrspace '" + AddrSpaceStr + "'");
2000 }
2001 Lex.Lex();
2002 return false;
2003 }
2004 if (Lex.getKind() != lltok::APSInt)
2005 return tokError("expected integer or string constant");
2006 SMLoc Loc = Lex.getLoc();
2007 if (parseUInt32(AddrSpace))
2008 return true;
2009 if (!isUInt<24>(AddrSpace))
2010 return error(Loc, "invalid address space, must be a 24-bit integer");
2011 return false;
2012 };
2013
2014 return parseToken(lltok::lparen, "expected '(' in address space") ||
2015 ParseAddrspaceValue(AddrSpace) ||
2016 parseToken(lltok::rparen, "expected ')' in address space");
2017}
2018
2019/// parseStringAttribute
2020/// := StringConstant
2021/// := StringConstant '=' StringConstant
2022bool LLParser::parseStringAttribute(AttrBuilder &B) {
2023 std::string Attr = Lex.getStrVal();
2024 Lex.Lex();
2025 std::string Val;
2026 if (EatIfPresent(lltok::equal) && parseStringConstant(Val))
2027 return true;
2028 B.addAttribute(Attr, Val);
2029 return false;
2030}
2031
2032/// Parse a potentially empty list of parameter or return attributes.
2033bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) {
2034 bool HaveError = false;
2035
2036 B.clear();
2037
2038 while (true) {
2039 lltok::Kind Token = Lex.getKind();
2040 if (Token == lltok::StringConstant) {
2041 if (parseStringAttribute(B))
2042 return true;
2043 continue;
2044 }
2045
2046 if (Token == lltok::kw_nocapture) {
2047 Lex.Lex();
2048 B.addCapturesAttr(CaptureInfo::none());
2049 continue;
2050 }
2051
2052 SMLoc Loc = Lex.getLoc();
2054 if (Attr == Attribute::None)
2055 return HaveError;
2056
2057 if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false))
2058 return true;
2059
2060 if (IsParam && !Attribute::canUseAsParamAttr(Attr))
2061 HaveError |= error(Loc, "this attribute does not apply to parameters");
2062 if (!IsParam && !Attribute::canUseAsRetAttr(Attr))
2063 HaveError |= error(Loc, "this attribute does not apply to return values");
2064 }
2065}
2066
2067static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
2068 HasLinkage = true;
2069 switch (Kind) {
2070 default:
2071 HasLinkage = false;
2073 case lltok::kw_private:
2075 case lltok::kw_internal:
2077 case lltok::kw_weak:
2079 case lltok::kw_weak_odr:
2081 case lltok::kw_linkonce:
2089 case lltok::kw_common:
2093 case lltok::kw_external:
2095 }
2096}
2097
2098/// parseOptionalLinkage
2099/// ::= /*empty*/
2100/// ::= 'private'
2101/// ::= 'internal'
2102/// ::= 'weak'
2103/// ::= 'weak_odr'
2104/// ::= 'linkonce'
2105/// ::= 'linkonce_odr'
2106/// ::= 'available_externally'
2107/// ::= 'appending'
2108/// ::= 'common'
2109/// ::= 'extern_weak'
2110/// ::= 'external'
2111bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
2112 unsigned &Visibility,
2113 unsigned &DLLStorageClass, bool &DSOLocal) {
2114 Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
2115 if (HasLinkage)
2116 Lex.Lex();
2117 parseOptionalDSOLocal(DSOLocal);
2118 parseOptionalVisibility(Visibility);
2119 parseOptionalDLLStorageClass(DLLStorageClass);
2120
2121 if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
2122 return error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
2123 }
2124
2125 return false;
2126}
2127
2128void LLParser::parseOptionalDSOLocal(bool &DSOLocal) {
2129 switch (Lex.getKind()) {
2130 default:
2131 DSOLocal = false;
2132 break;
2134 DSOLocal = true;
2135 Lex.Lex();
2136 break;
2138 DSOLocal = false;
2139 Lex.Lex();
2140 break;
2141 }
2142}
2143
2144/// parseOptionalVisibility
2145/// ::= /*empty*/
2146/// ::= 'default'
2147/// ::= 'hidden'
2148/// ::= 'protected'
2149///
2150void LLParser::parseOptionalVisibility(unsigned &Res) {
2151 switch (Lex.getKind()) {
2152 default:
2154 return;
2155 case lltok::kw_default:
2157 break;
2158 case lltok::kw_hidden:
2160 break;
2163 break;
2164 }
2165 Lex.Lex();
2166}
2167
2168bool LLParser::parseOptionalImportType(lltok::Kind Kind,
2170 switch (Kind) {
2171 default:
2172 return tokError("unknown import kind. Expect definition or declaration.");
2175 return false;
2178 return false;
2179 }
2180}
2181
2182/// parseOptionalDLLStorageClass
2183/// ::= /*empty*/
2184/// ::= 'dllimport'
2185/// ::= 'dllexport'
2186///
2187void LLParser::parseOptionalDLLStorageClass(unsigned &Res) {
2188 switch (Lex.getKind()) {
2189 default:
2191 return;
2194 break;
2197 break;
2198 }
2199 Lex.Lex();
2200}
2201
2202/// parseOptionalCallingConv
2203/// ::= /*empty*/
2204/// ::= 'ccc'
2205/// ::= 'fastcc'
2206/// ::= 'intel_ocl_bicc'
2207/// ::= 'coldcc'
2208/// ::= 'cfguard_checkcc'
2209/// ::= 'x86_stdcallcc'
2210/// ::= 'x86_fastcallcc'
2211/// ::= 'x86_thiscallcc'
2212/// ::= 'x86_vectorcallcc'
2213/// ::= 'arm_apcscc'
2214/// ::= 'arm_aapcscc'
2215/// ::= 'arm_aapcs_vfpcc'
2216/// ::= 'aarch64_vector_pcs'
2217/// ::= 'aarch64_sve_vector_pcs'
2218/// ::= 'aarch64_sme_preservemost_from_x0'
2219/// ::= 'aarch64_sme_preservemost_from_x1'
2220/// ::= 'aarch64_sme_preservemost_from_x2'
2221/// ::= 'msp430_intrcc'
2222/// ::= 'avr_intrcc'
2223/// ::= 'avr_signalcc'
2224/// ::= 'ptx_kernel'
2225/// ::= 'ptx_device'
2226/// ::= 'spir_func'
2227/// ::= 'spir_kernel'
2228/// ::= 'x86_64_sysvcc'
2229/// ::= 'win64cc'
2230/// ::= 'anyregcc'
2231/// ::= 'preserve_mostcc'
2232/// ::= 'preserve_allcc'
2233/// ::= 'preserve_nonecc'
2234/// ::= 'ghccc'
2235/// ::= 'swiftcc'
2236/// ::= 'swifttailcc'
2237/// ::= 'x86_intrcc'
2238/// ::= 'hhvmcc'
2239/// ::= 'hhvm_ccc'
2240/// ::= 'cxx_fast_tlscc'
2241/// ::= 'amdgpu_vs'
2242/// ::= 'amdgpu_ls'
2243/// ::= 'amdgpu_hs'
2244/// ::= 'amdgpu_es'
2245/// ::= 'amdgpu_gs'
2246/// ::= 'amdgpu_ps'
2247/// ::= 'amdgpu_cs'
2248/// ::= 'amdgpu_cs_chain'
2249/// ::= 'amdgpu_cs_chain_preserve'
2250/// ::= 'amdgpu_kernel'
2251/// ::= 'tailcc'
2252/// ::= 'm68k_rtdcc'
2253/// ::= 'graalcc'
2254/// ::= 'riscv_vector_cc'
2255/// ::= 'riscv_vls_cc'
2256/// ::= 'cc' UINT
2257///
2258bool LLParser::parseOptionalCallingConv(unsigned &CC) {
2259 switch (Lex.getKind()) {
2260 default: CC = CallingConv::C; return false;
2261 case lltok::kw_ccc: CC = CallingConv::C; break;
2262 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
2263 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
2276 break;
2279 break;
2282 break;
2285 break;
2295 case lltok::kw_win64cc: CC = CallingConv::Win64; break;
2296 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
2300 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
2301 case lltok::kw_swiftcc: CC = CallingConv::Swift; break;
2304 case lltok::kw_hhvmcc:
2306 break;
2307 case lltok::kw_hhvm_ccc:
2309 break;
2321 break;
2324 break;
2328 break;
2329 case lltok::kw_tailcc: CC = CallingConv::Tail; break;
2331 case lltok::kw_graalcc: CC = CallingConv::GRAAL; break;
2334 break;
2336 // Default ABI_VLEN
2338 Lex.Lex();
2339 if (!EatIfPresent(lltok::lparen))
2340 break;
2341 uint32_t ABIVlen;
2342 if (parseUInt32(ABIVlen) || !EatIfPresent(lltok::rparen))
2343 return true;
2344 switch (ABIVlen) {
2345 default:
2346 return tokError("unknown RISC-V ABI VLEN");
2347#define CC_VLS_CASE(ABIVlen) \
2348 case ABIVlen: \
2349 CC = CallingConv::RISCV_VLSCall_##ABIVlen; \
2350 break;
2351 CC_VLS_CASE(32)
2352 CC_VLS_CASE(64)
2353 CC_VLS_CASE(128)
2354 CC_VLS_CASE(256)
2355 CC_VLS_CASE(512)
2356 CC_VLS_CASE(1024)
2357 CC_VLS_CASE(2048)
2358 CC_VLS_CASE(4096)
2359 CC_VLS_CASE(8192)
2360 CC_VLS_CASE(16384)
2361 CC_VLS_CASE(32768)
2362 CC_VLS_CASE(65536)
2363#undef CC_VLS_CASE
2364 }
2365 return false;
2368 break;
2371 break;
2374 break;
2375 case lltok::kw_cc: {
2376 Lex.Lex();
2377 return parseUInt32(CC);
2378 }
2379 }
2380
2381 Lex.Lex();
2382 return false;
2383}
2384
2385/// parseMetadataAttachment
2386/// ::= !dbg !42
2387bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
2388 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
2389
2390 std::string Name = Lex.getStrVal();
2391 Kind = M->getMDKindID(Name);
2392 Lex.Lex();
2393
2394 return parseMDNode(MD);
2395}
2396
2397/// parseInstructionMetadata
2398/// ::= !dbg !42 (',' !dbg !57)*
2399bool LLParser::parseInstructionMetadata(Instruction &Inst) {
2400 do {
2401 if (Lex.getKind() != lltok::MetadataVar)
2402 return tokError("expected metadata after comma");
2403
2404 unsigned MDK;
2405 MDNode *N;
2406 if (parseMetadataAttachment(MDK, N))
2407 return true;
2408
2409 if (MDK == LLVMContext::MD_DIAssignID)
2410 TempDIAssignIDAttachments[N].push_back(&Inst);
2411 else
2412 Inst.setMetadata(MDK, N);
2413
2414 if (MDK == LLVMContext::MD_tbaa)
2415 InstsWithTBAATag.push_back(&Inst);
2416
2417 // If this is the end of the list, we're done.
2418 } while (EatIfPresent(lltok::comma));
2419 return false;
2420}
2421
2422/// parseGlobalObjectMetadataAttachment
2423/// ::= !dbg !57
2424bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) {
2425 unsigned MDK;
2426 MDNode *N;
2427 if (parseMetadataAttachment(MDK, N))
2428 return true;
2429
2430 GO.addMetadata(MDK, *N);
2431 return false;
2432}
2433
2434/// parseOptionalFunctionMetadata
2435/// ::= (!dbg !57)*
2436bool LLParser::parseOptionalFunctionMetadata(Function &F) {
2437 while (Lex.getKind() == lltok::MetadataVar)
2438 if (parseGlobalObjectMetadataAttachment(F))
2439 return true;
2440 return false;
2441}
2442
2443/// parseOptionalAlignment
2444/// ::= /* empty */
2445/// ::= 'align' 4
2446bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
2447 Alignment = std::nullopt;
2448 if (!EatIfPresent(lltok::kw_align))
2449 return false;
2450 LocTy AlignLoc = Lex.getLoc();
2451 uint64_t Value = 0;
2452
2453 LocTy ParenLoc = Lex.getLoc();
2454 bool HaveParens = false;
2455 if (AllowParens) {
2456 if (EatIfPresent(lltok::lparen))
2457 HaveParens = true;
2458 }
2459
2460 if (parseUInt64(Value))
2461 return true;
2462
2463 if (HaveParens && !EatIfPresent(lltok::rparen))
2464 return error(ParenLoc, "expected ')'");
2465
2466 if (!isPowerOf2_64(Value))
2467 return error(AlignLoc, "alignment is not a power of two");
2469 return error(AlignLoc, "huge alignments are not supported yet");
2470 Alignment = Align(Value);
2471 return false;
2472}
2473
2474/// parseOptionalPrefAlignment
2475/// ::= /* empty */
2476/// ::= 'prefalign' '(' 4 ')'
2477bool LLParser::parseOptionalPrefAlignment(MaybeAlign &Alignment) {
2478 Alignment = std::nullopt;
2479 if (!EatIfPresent(lltok::kw_prefalign))
2480 return false;
2481 LocTy AlignLoc = Lex.getLoc();
2482 uint64_t Value = 0;
2483
2484 LocTy ParenLoc = Lex.getLoc();
2485 if (!EatIfPresent(lltok::lparen))
2486 return error(ParenLoc, "expected '('");
2487
2488 if (parseUInt64(Value))
2489 return true;
2490
2491 ParenLoc = Lex.getLoc();
2492 if (!EatIfPresent(lltok::rparen))
2493 return error(ParenLoc, "expected ')'");
2494
2495 if (!isPowerOf2_64(Value))
2496 return error(AlignLoc, "alignment is not a power of two");
2498 return error(AlignLoc, "huge alignments are not supported yet");
2499 Alignment = Align(Value);
2500 return false;
2501}
2502
2503/// parseOptionalCodeModel
2504/// ::= /* empty */
2505/// ::= 'code_model' "large"
2506bool LLParser::parseOptionalCodeModel(CodeModel::Model &model) {
2507 Lex.Lex();
2508 auto StrVal = Lex.getStrVal();
2509 auto ErrMsg = "expected global code model string";
2510 if (StrVal == "tiny")
2511 model = CodeModel::Tiny;
2512 else if (StrVal == "small")
2513 model = CodeModel::Small;
2514 else if (StrVal == "kernel")
2515 model = CodeModel::Kernel;
2516 else if (StrVal == "medium")
2517 model = CodeModel::Medium;
2518 else if (StrVal == "large")
2519 model = CodeModel::Large;
2520 else
2521 return tokError(ErrMsg);
2522 if (parseToken(lltok::StringConstant, ErrMsg))
2523 return true;
2524 return false;
2525}
2526
2527/// parseOptionalAttrBytes
2528/// ::= /* empty */
2529/// ::= AttrKind '(' 4 ')'
2530///
2531/// where AttrKind is either 'dereferenceable', 'dereferenceable_or_null', or
2532/// 'dead_on_return'
2533bool LLParser::parseOptionalAttrBytes(lltok::Kind AttrKind,
2534 std::optional<uint64_t> &Bytes,
2535 bool ErrorNoBytes) {
2536 assert((AttrKind == lltok::kw_dereferenceable ||
2537 AttrKind == lltok::kw_dereferenceable_or_null ||
2538 AttrKind == lltok::kw_dead_on_return) &&
2539 "contract!");
2540
2541 Bytes = 0;
2542 if (!EatIfPresent(AttrKind))
2543 return false;
2544 LocTy ParenLoc = Lex.getLoc();
2545 if (!EatIfPresent(lltok::lparen)) {
2546 if (ErrorNoBytes)
2547 return error(ParenLoc, "expected '('");
2548 Bytes = std::nullopt;
2549 return false;
2550 }
2551 LocTy DerefLoc = Lex.getLoc();
2552 if (parseUInt64(Bytes.value()))
2553 return true;
2554 ParenLoc = Lex.getLoc();
2555 if (!EatIfPresent(lltok::rparen))
2556 return error(ParenLoc, "expected ')'");
2557 if (!Bytes.value())
2558 return error(DerefLoc, "byte count specified must be non-zero");
2559 return false;
2560}
2561
2562bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) {
2563 Lex.Lex();
2565 if (!EatIfPresent(lltok::lparen))
2566 return false;
2567 LocTy KindLoc = Lex.getLoc();
2568 if (Lex.getKind() == lltok::kw_sync)
2570 else if (Lex.getKind() == lltok::kw_async)
2572 else
2573 return error(KindLoc, "expected unwind table kind");
2574 Lex.Lex();
2575 return parseToken(lltok::rparen, "expected ')'");
2576}
2577
2578bool LLParser::parseAllocKind(AllocFnKind &Kind) {
2579 Lex.Lex();
2580 LocTy ParenLoc = Lex.getLoc();
2581 if (!EatIfPresent(lltok::lparen))
2582 return error(ParenLoc, "expected '('");
2583 LocTy KindLoc = Lex.getLoc();
2584 std::string Arg;
2585 if (parseStringConstant(Arg))
2586 return error(KindLoc, "expected allockind value");
2587 for (StringRef A : llvm::split(Arg, ",")) {
2588 if (A == "alloc") {
2590 } else if (A == "realloc") {
2592 } else if (A == "free") {
2594 } else if (A == "uninitialized") {
2596 } else if (A == "zeroed") {
2598 } else if (A == "aligned") {
2600 } else {
2601 return error(KindLoc, Twine("unknown allockind ") + A);
2602 }
2603 }
2604 ParenLoc = Lex.getLoc();
2605 if (!EatIfPresent(lltok::rparen))
2606 return error(ParenLoc, "expected ')'");
2607 if (Kind == AllocFnKind::Unknown)
2608 return error(KindLoc, "expected allockind value");
2609 return false;
2610}
2611
2612static std::optional<MemoryEffects::Location> keywordToLoc(lltok::Kind Tok) {
2613 switch (Tok) {
2614 case lltok::kw_argmem:
2615 return IRMemLocation::ArgMem;
2618 case lltok::kw_errnomem:
2624 default:
2625 return std::nullopt;
2626 }
2627}
2628
2629static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) {
2630 switch (Tok) {
2631 case lltok::kw_none:
2632 return ModRefInfo::NoModRef;
2633 case lltok::kw_read:
2634 return ModRefInfo::Ref;
2635 case lltok::kw_write:
2636 return ModRefInfo::Mod;
2638 return ModRefInfo::ModRef;
2639 default:
2640 return std::nullopt;
2641 }
2642}
2643
2644static std::optional<DenormalMode::DenormalModeKind>
2646 switch (Tok) {
2647 case lltok::kw_ieee:
2648 return DenormalMode::IEEE;
2653 case lltok::kw_dynamic:
2654 return DenormalMode::Dynamic;
2655 default:
2656 return std::nullopt;
2657 }
2658}
2659
2660std::optional<MemoryEffects> LLParser::parseMemoryAttr() {
2662
2663 // We use syntax like memory(argmem: read), so the colon should not be
2664 // interpreted as a label terminator.
2665 Lex.setIgnoreColonInIdentifiers(true);
2666 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
2667
2668 Lex.Lex();
2669 if (!EatIfPresent(lltok::lparen)) {
2670 tokError("expected '('");
2671 return std::nullopt;
2672 }
2673
2674 bool SeenLoc = false;
2675 do {
2676 std::optional<IRMemLocation> Loc = keywordToLoc(Lex.getKind());
2677 if (Loc) {
2678 Lex.Lex();
2679 if (!EatIfPresent(lltok::colon)) {
2680 tokError("expected ':' after location");
2681 return std::nullopt;
2682 }
2683 }
2684
2685 std::optional<ModRefInfo> MR = keywordToModRef(Lex.getKind());
2686 if (!MR) {
2687 if (!Loc)
2688 tokError("expected memory location (argmem, inaccessiblemem, errnomem) "
2689 "or access kind (none, read, write, readwrite)");
2690 else
2691 tokError("expected access kind (none, read, write, readwrite)");
2692 return std::nullopt;
2693 }
2694
2695 Lex.Lex();
2696 if (Loc) {
2697 SeenLoc = true;
2698 ME = ME.getWithModRef(*Loc, *MR);
2699 } else {
2700 if (SeenLoc) {
2701 tokError("default access kind must be specified first");
2702 return std::nullopt;
2703 }
2704 ME = MemoryEffects(*MR);
2705 }
2706
2707 if (EatIfPresent(lltok::rparen))
2708 return ME;
2709 } while (EatIfPresent(lltok::comma));
2710
2711 tokError("unterminated memory attribute");
2712 return std::nullopt;
2713}
2714
2715std::optional<DenormalMode> LLParser::parseDenormalFPEnvEntry() {
2716 std::optional<DenormalMode::DenormalModeKind> OutputMode =
2717 keywordToDenormalModeKind(Lex.getKind());
2718 if (!OutputMode) {
2719 tokError("expected denormal behavior kind (ieee, preservesign, "
2720 "positivezero, dynamic)");
2721 return {};
2722 }
2723
2724 Lex.Lex();
2725
2726 std::optional<DenormalMode::DenormalModeKind> InputMode;
2727 if (EatIfPresent(lltok::bar)) {
2728 InputMode = keywordToDenormalModeKind(Lex.getKind());
2729 if (!InputMode) {
2730 tokError("expected denormal behavior kind (ieee, preservesign, "
2731 "positivezero, dynamic)");
2732 return {};
2733 }
2734
2735 Lex.Lex();
2736 } else {
2737 // Single item, input == output mode
2738 InputMode = OutputMode;
2739 }
2740
2741 return DenormalMode(*OutputMode, *InputMode);
2742}
2743
2744std::optional<DenormalFPEnv> LLParser::parseDenormalFPEnvAttr() {
2745 // We use syntax like denormal_fpenv(float: preservesign), so the colon should
2746 // not be interpreted as a label terminator.
2747 Lex.setIgnoreColonInIdentifiers(true);
2748 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
2749
2750 Lex.Lex();
2751
2752 if (parseToken(lltok::lparen, "expected '('"))
2753 return {};
2754
2755 DenormalMode DefaultMode = DenormalMode::getIEEE();
2756 DenormalMode F32Mode = DenormalMode::getInvalid();
2757
2758 bool HasDefaultSection = false;
2759 if (Lex.getKind() != lltok::Type) {
2760 std::optional<DenormalMode> ParsedDefaultMode = parseDenormalFPEnvEntry();
2761 if (!ParsedDefaultMode)
2762 return {};
2763 DefaultMode = *ParsedDefaultMode;
2764 HasDefaultSection = true;
2765 }
2766
2767 bool HasComma = EatIfPresent(lltok::comma);
2768 if (Lex.getKind() == lltok::Type) {
2769 if (HasDefaultSection && !HasComma) {
2770 tokError("expected ',' before float:");
2771 return {};
2772 }
2773
2774 Type *Ty = nullptr;
2775 if (parseType(Ty) || !Ty->isFloatTy()) {
2776 tokError("expected float:");
2777 return {};
2778 }
2779
2780 if (parseToken(lltok::colon, "expected ':' before float denormal_fpenv"))
2781 return {};
2782
2783 std::optional<DenormalMode> ParsedF32Mode = parseDenormalFPEnvEntry();
2784 if (!ParsedF32Mode)
2785 return {};
2786
2787 F32Mode = *ParsedF32Mode;
2788 }
2789
2790 if (parseToken(lltok::rparen, "unterminated denormal_fpenv"))
2791 return {};
2792
2793 return DenormalFPEnv(DefaultMode, F32Mode);
2794}
2795
2796static unsigned keywordToFPClassTest(lltok::Kind Tok) {
2797 switch (Tok) {
2798 case lltok::kw_all:
2799 return fcAllFlags;
2800 case lltok::kw_nan:
2801 return fcNan;
2802 case lltok::kw_snan:
2803 return fcSNan;
2804 case lltok::kw_qnan:
2805 return fcQNan;
2806 case lltok::kw_inf:
2807 return fcInf;
2808 case lltok::kw_ninf:
2809 return fcNegInf;
2810 case lltok::kw_pinf:
2811 return fcPosInf;
2812 case lltok::kw_norm:
2813 return fcNormal;
2814 case lltok::kw_nnorm:
2815 return fcNegNormal;
2816 case lltok::kw_pnorm:
2817 return fcPosNormal;
2818 case lltok::kw_sub:
2819 return fcSubnormal;
2820 case lltok::kw_nsub:
2821 return fcNegSubnormal;
2822 case lltok::kw_psub:
2823 return fcPosSubnormal;
2824 case lltok::kw_zero:
2825 return fcZero;
2826 case lltok::kw_nzero:
2827 return fcNegZero;
2828 case lltok::kw_pzero:
2829 return fcPosZero;
2830 default:
2831 return 0;
2832 }
2833}
2834
2835unsigned LLParser::parseNoFPClassAttr() {
2836 unsigned Mask = fcNone;
2837
2838 Lex.Lex();
2839 if (!EatIfPresent(lltok::lparen)) {
2840 tokError("expected '('");
2841 return 0;
2842 }
2843
2844 do {
2845 uint64_t Value = 0;
2846 unsigned TestMask = keywordToFPClassTest(Lex.getKind());
2847 if (TestMask != 0) {
2848 Mask |= TestMask;
2849 // TODO: Disallow overlapping masks to avoid copy paste errors
2850 } else if (Mask == 0 && Lex.getKind() == lltok::APSInt &&
2851 !parseUInt64(Value)) {
2852 if (Value == 0 || (Value & ~static_cast<unsigned>(fcAllFlags)) != 0) {
2853 error(Lex.getLoc(), "invalid mask value for 'nofpclass'");
2854 return 0;
2855 }
2856
2857 if (!EatIfPresent(lltok::rparen)) {
2858 error(Lex.getLoc(), "expected ')'");
2859 return 0;
2860 }
2861
2862 return Value;
2863 } else {
2864 error(Lex.getLoc(), "expected nofpclass test mask");
2865 return 0;
2866 }
2867
2868 Lex.Lex();
2869 if (EatIfPresent(lltok::rparen))
2870 return Mask;
2871 } while (1);
2872
2873 llvm_unreachable("unterminated nofpclass attribute");
2874}
2875
2876/// parseOptionalCommaAlign
2877/// ::=
2878/// ::= ',' align 4
2879///
2880/// This returns with AteExtraComma set to true if it ate an excess comma at the
2881/// end.
2882bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment,
2883 bool &AteExtraComma) {
2884 AteExtraComma = false;
2885 while (EatIfPresent(lltok::comma)) {
2886 // Metadata at the end is an early exit.
2887 if (Lex.getKind() == lltok::MetadataVar) {
2888 AteExtraComma = true;
2889 return false;
2890 }
2891
2892 if (Lex.getKind() != lltok::kw_align)
2893 return error(Lex.getLoc(), "expected metadata or 'align'");
2894
2895 if (parseOptionalAlignment(Alignment))
2896 return true;
2897 }
2898
2899 return false;
2900}
2901
2902/// parseOptionalCommaAddrSpace
2903/// ::=
2904/// ::= ',' addrspace(1)
2905///
2906/// This returns with AteExtraComma set to true if it ate an excess comma at the
2907/// end.
2908bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
2909 bool &AteExtraComma) {
2910 AteExtraComma = false;
2911 while (EatIfPresent(lltok::comma)) {
2912 // Metadata at the end is an early exit.
2913 if (Lex.getKind() == lltok::MetadataVar) {
2914 AteExtraComma = true;
2915 return false;
2916 }
2917
2918 Loc = Lex.getLoc();
2919 if (Lex.getKind() != lltok::kw_addrspace)
2920 return error(Lex.getLoc(), "expected metadata or 'addrspace'");
2921
2922 if (parseOptionalAddrSpace(AddrSpace))
2923 return true;
2924 }
2925
2926 return false;
2927}
2928
2929bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
2930 std::optional<unsigned> &HowManyArg) {
2931 Lex.Lex();
2932
2933 auto StartParen = Lex.getLoc();
2934 if (!EatIfPresent(lltok::lparen))
2935 return error(StartParen, "expected '('");
2936
2937 if (parseUInt32(BaseSizeArg))
2938 return true;
2939
2940 if (EatIfPresent(lltok::comma)) {
2941 auto HowManyAt = Lex.getLoc();
2942 unsigned HowMany;
2943 if (parseUInt32(HowMany))
2944 return true;
2945 if (HowMany == BaseSizeArg)
2946 return error(HowManyAt,
2947 "'allocsize' indices can't refer to the same parameter");
2948 HowManyArg = HowMany;
2949 } else
2950 HowManyArg = std::nullopt;
2951
2952 auto EndParen = Lex.getLoc();
2953 if (!EatIfPresent(lltok::rparen))
2954 return error(EndParen, "expected ')'");
2955 return false;
2956}
2957
2958bool LLParser::parseVScaleRangeArguments(unsigned &MinValue,
2959 unsigned &MaxValue) {
2960 Lex.Lex();
2961
2962 auto StartParen = Lex.getLoc();
2963 if (!EatIfPresent(lltok::lparen))
2964 return error(StartParen, "expected '('");
2965
2966 if (parseUInt32(MinValue))
2967 return true;
2968
2969 if (EatIfPresent(lltok::comma)) {
2970 if (parseUInt32(MaxValue))
2971 return true;
2972 } else
2973 MaxValue = MinValue;
2974
2975 auto EndParen = Lex.getLoc();
2976 if (!EatIfPresent(lltok::rparen))
2977 return error(EndParen, "expected ')'");
2978 return false;
2979}
2980
2981/// parseScopeAndOrdering
2982/// if isAtomic: ::= SyncScope? AtomicOrdering
2983/// else: ::=
2984///
2985/// This sets Scope and Ordering to the parsed values.
2986bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
2987 AtomicOrdering &Ordering) {
2988 if (!IsAtomic)
2989 return false;
2990
2991 return parseScope(SSID) || parseOrdering(Ordering);
2992}
2993
2994/// parseScope
2995/// ::= syncscope("singlethread" | "<target scope>")?
2996///
2997/// This sets synchronization scope ID to the ID of the parsed value.
2998bool LLParser::parseScope(SyncScope::ID &SSID) {
2999 SSID = SyncScope::System;
3000 if (EatIfPresent(lltok::kw_syncscope)) {
3001 auto StartParenAt = Lex.getLoc();
3002 if (!EatIfPresent(lltok::lparen))
3003 return error(StartParenAt, "Expected '(' in syncscope");
3004
3005 std::string SSN;
3006 auto SSNAt = Lex.getLoc();
3007 if (parseStringConstant(SSN))
3008 return error(SSNAt, "Expected synchronization scope name");
3009
3010 auto EndParenAt = Lex.getLoc();
3011 if (!EatIfPresent(lltok::rparen))
3012 return error(EndParenAt, "Expected ')' in syncscope");
3013
3014 SSID = Context.getOrInsertSyncScopeID(SSN);
3015 }
3016
3017 return false;
3018}
3019
3020/// parseOrdering
3021/// ::= AtomicOrdering
3022///
3023/// This sets Ordering to the parsed value.
3024bool LLParser::parseOrdering(AtomicOrdering &Ordering) {
3025 switch (Lex.getKind()) {
3026 default:
3027 return tokError("Expected ordering on atomic instruction");
3030 // Not specified yet:
3031 // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
3035 case lltok::kw_seq_cst:
3037 break;
3038 }
3039 Lex.Lex();
3040 return false;
3041}
3042
3043/// parseOptionalStackAlignment
3044/// ::= /* empty */
3045/// ::= 'alignstack' '(' 4 ')'
3046bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) {
3047 Alignment = 0;
3048 if (!EatIfPresent(lltok::kw_alignstack))
3049 return false;
3050 LocTy ParenLoc = Lex.getLoc();
3051 if (!EatIfPresent(lltok::lparen))
3052 return error(ParenLoc, "expected '('");
3053 LocTy AlignLoc = Lex.getLoc();
3054 if (parseUInt32(Alignment))
3055 return true;
3056 ParenLoc = Lex.getLoc();
3057 if (!EatIfPresent(lltok::rparen))
3058 return error(ParenLoc, "expected ')'");
3059 if (!isPowerOf2_32(Alignment))
3060 return error(AlignLoc, "stack alignment is not a power of two");
3061 return false;
3062}
3063
3064/// parseIndexList - This parses the index list for an insert/extractvalue
3065/// instruction. This sets AteExtraComma in the case where we eat an extra
3066/// comma at the end of the line and find that it is followed by metadata.
3067/// Clients that don't allow metadata can call the version of this function that
3068/// only takes one argument.
3069///
3070/// parseIndexList
3071/// ::= (',' uint32)+
3072///
3073bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices,
3074 bool &AteExtraComma) {
3075 AteExtraComma = false;
3076
3077 if (Lex.getKind() != lltok::comma)
3078 return tokError("expected ',' as start of index list");
3079
3080 while (EatIfPresent(lltok::comma)) {
3081 if (Lex.getKind() == lltok::MetadataVar) {
3082 if (Indices.empty())
3083 return tokError("expected index");
3084 AteExtraComma = true;
3085 return false;
3086 }
3087 unsigned Idx = 0;
3088 if (parseUInt32(Idx))
3089 return true;
3090 Indices.push_back(Idx);
3091 }
3092
3093 return false;
3094}
3095
3096//===----------------------------------------------------------------------===//
3097// Type Parsing.
3098//===----------------------------------------------------------------------===//
3099
3100/// parseType - parse a type.
3101bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
3102 SMLoc TypeLoc = Lex.getLoc();
3103 switch (Lex.getKind()) {
3104 default:
3105 return tokError(Msg);
3106 case lltok::Type:
3107 // Type ::= 'float' | 'void' (etc)
3108 Result = Lex.getTyVal();
3109 Lex.Lex();
3110
3111 // Handle "ptr" opaque pointer type.
3112 //
3113 // Type ::= ptr ('addrspace' '(' uint32 ')')?
3114 if (Result->isPointerTy()) {
3115 unsigned AddrSpace;
3116 if (parseOptionalAddrSpace(AddrSpace))
3117 return true;
3118 Result = PointerType::get(getContext(), AddrSpace);
3119
3120 // Give a nice error for 'ptr*'.
3121 if (Lex.getKind() == lltok::star)
3122 return tokError("ptr* is invalid - use ptr instead");
3123
3124 // Fall through to parsing the type suffixes only if this 'ptr' is a
3125 // function return. Otherwise, return success, implicitly rejecting other
3126 // suffixes.
3127 if (Lex.getKind() != lltok::lparen)
3128 return false;
3129 }
3130 break;
3131 case lltok::kw_target: {
3132 // Type ::= TargetExtType
3133 if (parseTargetExtType(Result))
3134 return true;
3135 break;
3136 }
3137 case lltok::lbrace:
3138 // Type ::= StructType
3139 if (parseAnonStructType(Result, false))
3140 return true;
3141 break;
3142 case lltok::lsquare:
3143 // Type ::= '[' ... ']'
3144 Lex.Lex(); // eat the lsquare.
3145 if (parseArrayVectorType(Result, false))
3146 return true;
3147 break;
3148 case lltok::less: // Either vector or packed struct.
3149 // Type ::= '<' ... '>'
3150 Lex.Lex();
3151 if (Lex.getKind() == lltok::lbrace) {
3152 if (parseAnonStructType(Result, true) ||
3153 parseToken(lltok::greater, "expected '>' at end of packed struct"))
3154 return true;
3155 } else if (parseArrayVectorType(Result, true))
3156 return true;
3157 break;
3158 case lltok::LocalVar: {
3159 // Type ::= %foo
3160 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
3161
3162 // If the type hasn't been defined yet, create a forward definition and
3163 // remember where that forward def'n was seen (in case it never is defined).
3164 if (!Entry.first) {
3165 Entry.first = StructType::create(Context, Lex.getStrVal());
3166 Entry.second = Lex.getLoc();
3167 }
3168 Result = Entry.first;
3169 Lex.Lex();
3170 break;
3171 }
3172
3173 case lltok::LocalVarID: {
3174 // Type ::= %4
3175 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
3176
3177 // If the type hasn't been defined yet, create a forward definition and
3178 // remember where that forward def'n was seen (in case it never is defined).
3179 if (!Entry.first) {
3180 Entry.first = StructType::create(Context);
3181 Entry.second = Lex.getLoc();
3182 }
3183 Result = Entry.first;
3184 Lex.Lex();
3185 break;
3186 }
3187 }
3188
3189 // parse the type suffixes.
3190 while (true) {
3191 switch (Lex.getKind()) {
3192 // End of type.
3193 default:
3194 if (!AllowVoid && Result->isVoidTy())
3195 return error(TypeLoc, "void type only allowed for function results");
3196 return false;
3197
3198 // Type ::= Type '*'
3199 case lltok::star:
3200 if (Result->isLabelTy())
3201 return tokError("basic block pointers are invalid");
3202 if (Result->isVoidTy())
3203 return tokError("pointers to void are invalid - use i8* instead");
3205 return tokError("pointer to this type is invalid");
3206 Result = PointerType::getUnqual(Context);
3207 Lex.Lex();
3208 break;
3209
3210 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
3211 case lltok::kw_addrspace: {
3212 if (Result->isLabelTy())
3213 return tokError("basic block pointers are invalid");
3214 if (Result->isVoidTy())
3215 return tokError("pointers to void are invalid; use i8* instead");
3217 return tokError("pointer to this type is invalid");
3218 unsigned AddrSpace;
3219 if (parseOptionalAddrSpace(AddrSpace) ||
3220 parseToken(lltok::star, "expected '*' in address space"))
3221 return true;
3222
3223 Result = PointerType::get(Context, AddrSpace);
3224 break;
3225 }
3226
3227 /// Types '(' ArgTypeListI ')' OptFuncAttrs
3228 case lltok::lparen:
3229 if (parseFunctionType(Result))
3230 return true;
3231 break;
3232 }
3233 }
3234}
3235
3236/// parseParameterList
3237/// ::= '(' ')'
3238/// ::= '(' Arg (',' Arg)* ')'
3239/// Arg
3240/// ::= Type OptionalAttributes Value OptionalAttributes
3241bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
3242 PerFunctionState &PFS, bool IsMustTailCall,
3243 bool InVarArgsFunc) {
3244 if (parseToken(lltok::lparen, "expected '(' in call"))
3245 return true;
3246
3247 while (Lex.getKind() != lltok::rparen) {
3248 // If this isn't the first argument, we need a comma.
3249 if (!ArgList.empty() &&
3250 parseToken(lltok::comma, "expected ',' in argument list"))
3251 return true;
3252
3253 // parse an ellipsis if this is a musttail call in a variadic function.
3254 if (Lex.getKind() == lltok::dotdotdot) {
3255 const char *Msg = "unexpected ellipsis in argument list for ";
3256 if (!IsMustTailCall)
3257 return tokError(Twine(Msg) + "non-musttail call");
3258 if (!InVarArgsFunc)
3259 return tokError(Twine(Msg) + "musttail call in non-varargs function");
3260 Lex.Lex(); // Lex the '...', it is purely for readability.
3261 return parseToken(lltok::rparen, "expected ')' at end of argument list");
3262 }
3263
3264 // parse the argument.
3265 LocTy ArgLoc;
3266 Type *ArgTy = nullptr;
3267 Value *V;
3268 if (parseType(ArgTy, ArgLoc))
3269 return true;
3271 return error(ArgLoc, "invalid type for function argument");
3272
3273 AttrBuilder ArgAttrs(M->getContext());
3274
3275 if (ArgTy->isMetadataTy()) {
3276 if (parseMetadataAsValue(V, PFS))
3277 return true;
3278 } else {
3279 // Otherwise, handle normal operands.
3280 if (parseOptionalParamAttrs(ArgAttrs) || parseValue(ArgTy, V, PFS))
3281 return true;
3282 }
3283 ArgList.push_back(ParamInfo(
3284 ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
3285 }
3286
3287 if (IsMustTailCall && InVarArgsFunc)
3288 return tokError("expected '...' at end of argument list for musttail call "
3289 "in varargs function");
3290
3291 Lex.Lex(); // Lex the ')'.
3292 return false;
3293}
3294
3295/// parseRequiredTypeAttr
3296/// ::= attrname(<ty>)
3297bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
3298 Attribute::AttrKind AttrKind) {
3299 Type *Ty = nullptr;
3300 if (!EatIfPresent(AttrToken))
3301 return true;
3302 if (!EatIfPresent(lltok::lparen))
3303 return error(Lex.getLoc(), "expected '('");
3304 if (parseType(Ty))
3305 return true;
3306 if (!EatIfPresent(lltok::rparen))
3307 return error(Lex.getLoc(), "expected ')'");
3308
3309 B.addTypeAttr(AttrKind, Ty);
3310 return false;
3311}
3312
3313/// parseRangeAttr
3314/// ::= range(<ty> <n>,<n>)
3315bool LLParser::parseRangeAttr(AttrBuilder &B) {
3316 Lex.Lex();
3317
3318 APInt Lower;
3319 APInt Upper;
3320 Type *Ty = nullptr;
3321 LocTy TyLoc;
3322
3323 auto ParseAPSInt = [&](unsigned BitWidth, APInt &Val) {
3324 if (Lex.getKind() != lltok::APSInt)
3325 return tokError("expected integer");
3326 if (Lex.getAPSIntVal().getBitWidth() > BitWidth)
3327 return tokError(
3328 "integer is too large for the bit width of specified type");
3329 Val = Lex.getAPSIntVal().extend(BitWidth);
3330 Lex.Lex();
3331 return false;
3332 };
3333
3334 if (parseToken(lltok::lparen, "expected '('") || parseType(Ty, TyLoc))
3335 return true;
3336 if (!Ty->isIntegerTy())
3337 return error(TyLoc, "the range must have integer type!");
3338
3339 unsigned BitWidth = Ty->getPrimitiveSizeInBits();
3340
3341 if (ParseAPSInt(BitWidth, Lower) ||
3342 parseToken(lltok::comma, "expected ','") || ParseAPSInt(BitWidth, Upper))
3343 return true;
3344 if (Lower == Upper && !Lower.isZero())
3345 return tokError("the range represent the empty set but limits aren't 0!");
3346
3347 if (parseToken(lltok::rparen, "expected ')'"))
3348 return true;
3349
3350 B.addRangeAttr(ConstantRange(Lower, Upper));
3351 return false;
3352}
3353
3354/// parseInitializesAttr
3355/// ::= initializes((Lo1,Hi1),(Lo2,Hi2),...)
3356bool LLParser::parseInitializesAttr(AttrBuilder &B) {
3357 Lex.Lex();
3358
3359 auto ParseAPSInt = [&](APInt &Val) {
3360 if (Lex.getKind() != lltok::APSInt)
3361 return tokError("expected integer");
3362 Val = Lex.getAPSIntVal().extend(64);
3363 Lex.Lex();
3364 return false;
3365 };
3366
3367 if (parseToken(lltok::lparen, "expected '('"))
3368 return true;
3369
3371 // Parse each constant range.
3372 do {
3373 APInt Lower, Upper;
3374 if (parseToken(lltok::lparen, "expected '('"))
3375 return true;
3376
3377 if (ParseAPSInt(Lower) || parseToken(lltok::comma, "expected ','") ||
3378 ParseAPSInt(Upper))
3379 return true;
3380
3381 if (Lower == Upper)
3382 return tokError("the range should not represent the full or empty set!");
3383
3384 if (parseToken(lltok::rparen, "expected ')'"))
3385 return true;
3386
3387 RangeList.push_back(ConstantRange(Lower, Upper));
3388 } while (EatIfPresent(lltok::comma));
3389
3390 if (parseToken(lltok::rparen, "expected ')'"))
3391 return true;
3392
3393 auto CRLOrNull = ConstantRangeList::getConstantRangeList(RangeList);
3394 if (!CRLOrNull.has_value())
3395 return tokError("Invalid (unordered or overlapping) range list");
3396 B.addInitializesAttr(*CRLOrNull);
3397 return false;
3398}
3399
3400bool LLParser::parseCapturesAttr(AttrBuilder &B) {
3402 std::optional<CaptureComponents> Ret;
3403
3404 // We use syntax like captures(ret: address, provenance), so the colon
3405 // should not be interpreted as a label terminator.
3406 Lex.setIgnoreColonInIdentifiers(true);
3407 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
3408
3409 Lex.Lex();
3410 if (parseToken(lltok::lparen, "expected '('"))
3411 return true;
3412
3413 CaptureComponents *Current = &Other;
3414 bool SeenComponent = false;
3415 while (true) {
3416 if (EatIfPresent(lltok::kw_ret)) {
3417 if (parseToken(lltok::colon, "expected ':'"))
3418 return true;
3419 if (Ret)
3420 return tokError("duplicate 'ret' location");
3422 Current = &*Ret;
3423 SeenComponent = false;
3424 }
3425
3426 if (EatIfPresent(lltok::kw_none)) {
3427 if (SeenComponent)
3428 return tokError("cannot use 'none' with other component");
3429 *Current = CaptureComponents::None;
3430 } else {
3431 if (SeenComponent && capturesNothing(*Current))
3432 return tokError("cannot use 'none' with other component");
3433
3434 if (EatIfPresent(lltok::kw_address_is_null))
3436 else if (EatIfPresent(lltok::kw_address))
3437 *Current |= CaptureComponents::Address;
3438 else if (EatIfPresent(lltok::kw_provenance))
3440 else if (EatIfPresent(lltok::kw_read_provenance))
3442 else
3443 return tokError("expected one of 'none', 'address', 'address_is_null', "
3444 "'provenance' or 'read_provenance'");
3445 }
3446
3447 SeenComponent = true;
3448 if (EatIfPresent(lltok::rparen))
3449 break;
3450
3451 if (parseToken(lltok::comma, "expected ',' or ')'"))
3452 return true;
3453 }
3454
3455 B.addCapturesAttr(CaptureInfo(Other, Ret.value_or(Other)));
3456 return false;
3457}
3458
3459/// parseOptionalOperandBundles
3460/// ::= /*empty*/
3461/// ::= '[' OperandBundle [, OperandBundle ]* ']'
3462///
3463/// OperandBundle
3464/// ::= bundle-tag '(' ')'
3465/// ::= bundle-tag '(' Type Value [, Type Value ]* ')'
3466///
3467/// bundle-tag ::= String Constant
3468bool LLParser::parseOptionalOperandBundles(
3469 SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
3470 LocTy BeginLoc = Lex.getLoc();
3471 if (!EatIfPresent(lltok::lsquare))
3472 return false;
3473
3474 while (Lex.getKind() != lltok::rsquare) {
3475 // If this isn't the first operand bundle, we need a comma.
3476 if (!BundleList.empty() &&
3477 parseToken(lltok::comma, "expected ',' in input list"))
3478 return true;
3479
3480 std::string Tag;
3481 if (parseStringConstant(Tag))
3482 return true;
3483
3484 if (parseToken(lltok::lparen, "expected '(' in operand bundle"))
3485 return true;
3486
3487 std::vector<Value *> Inputs;
3488 while (Lex.getKind() != lltok::rparen) {
3489 // If this isn't the first input, we need a comma.
3490 if (!Inputs.empty() &&
3491 parseToken(lltok::comma, "expected ',' in input list"))
3492 return true;
3493
3494 Type *Ty = nullptr;
3495 Value *Input = nullptr;
3496 if (parseType(Ty))
3497 return true;
3498 if (Ty->isMetadataTy()) {
3499 if (parseMetadataAsValue(Input, PFS))
3500 return true;
3501 } else if (parseValue(Ty, Input, PFS)) {
3502 return true;
3503 }
3504 Inputs.push_back(Input);
3505 }
3506
3507 BundleList.emplace_back(std::move(Tag), std::move(Inputs));
3508
3509 Lex.Lex(); // Lex the ')'.
3510 }
3511
3512 if (BundleList.empty())
3513 return error(BeginLoc, "operand bundle set must not be empty");
3514
3515 Lex.Lex(); // Lex the ']'.
3516 return false;
3517}
3518
3519bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix,
3520 unsigned NextID, unsigned ID) {
3521 if (ID < NextID)
3522 return error(Loc, Kind + " expected to be numbered '" + Prefix +
3523 Twine(NextID) + "' or greater");
3524
3525 return false;
3526}
3527
3528/// parseArgumentList - parse the argument list for a function type or function
3529/// prototype.
3530/// ::= '(' ArgTypeListI ')'
3531/// ArgTypeListI
3532/// ::= /*empty*/
3533/// ::= '...'
3534/// ::= ArgTypeList ',' '...'
3535/// ::= ArgType (',' ArgType)*
3536///
3537bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
3538 SmallVectorImpl<unsigned> &UnnamedArgNums,
3539 bool &IsVarArg) {
3540 unsigned CurValID = 0;
3541 IsVarArg = false;
3542 assert(Lex.getKind() == lltok::lparen);
3543 Lex.Lex(); // eat the (.
3544
3545 if (Lex.getKind() != lltok::rparen) {
3546 do {
3547 // Handle ... at end of arg list.
3548 if (EatIfPresent(lltok::dotdotdot)) {
3549 IsVarArg = true;
3550 break;
3551 }
3552
3553 // Otherwise must be an argument type.
3554 LocTy TypeLoc = Lex.getLoc();
3555 Type *ArgTy = nullptr;
3556 AttrBuilder Attrs(M->getContext());
3557 if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
3558 return true;
3559
3560 if (ArgTy->isVoidTy())
3561 return error(TypeLoc, "argument can not have void type");
3562
3563 std::string Name;
3564 FileLoc IdentStart;
3565 FileLoc IdentEnd;
3566 bool Unnamed = false;
3567 if (Lex.getKind() == lltok::LocalVar) {
3568 Name = Lex.getStrVal();
3569 IdentStart = getTokLineColumnPos();
3570 Lex.Lex();
3571 IdentEnd = getPrevTokEndLineColumnPos();
3572 } else {
3573 unsigned ArgID;
3574 if (Lex.getKind() == lltok::LocalVarID) {
3575 ArgID = Lex.getUIntVal();
3576 IdentStart = getTokLineColumnPos();
3577 if (checkValueID(TypeLoc, "argument", "%", CurValID, ArgID))
3578 return true;
3579 Lex.Lex();
3580 IdentEnd = getPrevTokEndLineColumnPos();
3581 } else {
3582 ArgID = CurValID;
3583 Unnamed = true;
3584 }
3585 UnnamedArgNums.push_back(ArgID);
3586 CurValID = ArgID + 1;
3587 }
3588
3590 return error(TypeLoc, "invalid type for function argument");
3591
3592 ArgList.emplace_back(
3593 TypeLoc, ArgTy,
3594 Unnamed ? std::nullopt
3595 : std::make_optional(FileLocRange(IdentStart, IdentEnd)),
3596 AttributeSet::get(ArgTy->getContext(), Attrs), std::move(Name));
3597 } while (EatIfPresent(lltok::comma));
3598 }
3599
3600 return parseToken(lltok::rparen, "expected ')' at end of argument list");
3601}
3602
3603/// parseFunctionType
3604/// ::= Type ArgumentList OptionalAttrs
3605bool LLParser::parseFunctionType(Type *&Result) {
3606 assert(Lex.getKind() == lltok::lparen);
3607
3609 return tokError("invalid function return type");
3610
3612 bool IsVarArg;
3613 SmallVector<unsigned> UnnamedArgNums;
3614 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg))
3615 return true;
3616
3617 // Reject names on the arguments lists.
3618 for (const ArgInfo &Arg : ArgList) {
3619 if (!Arg.Name.empty())
3620 return error(Arg.Loc, "argument name invalid in function type");
3621 if (Arg.Attrs.hasAttributes())
3622 return error(Arg.Loc, "argument attributes invalid in function type");
3623 }
3624
3625 SmallVector<Type*, 16> ArgListTy;
3626 for (const ArgInfo &Arg : ArgList)
3627 ArgListTy.push_back(Arg.Ty);
3628
3629 Result = FunctionType::get(Result, ArgListTy, IsVarArg);
3630 return false;
3631}
3632
3633/// parseAnonStructType - parse an anonymous struct type, which is inlined into
3634/// other structs.
3635bool LLParser::parseAnonStructType(Type *&Result, bool Packed) {
3637 if (parseStructBody(Elts))
3638 return true;
3639
3640 Result = StructType::get(Context, Elts, Packed);
3641 return false;
3642}
3643
3644/// parseStructDefinition - parse a struct in a 'type' definition.
3645bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name,
3646 std::pair<Type *, LocTy> &Entry,
3647 Type *&ResultTy) {
3648 // If the type was already defined, diagnose the redefinition.
3649 if (Entry.first && !Entry.second.isValid())
3650 return error(TypeLoc, "redefinition of type");
3651
3652 // If we have opaque, just return without filling in the definition for the
3653 // struct. This counts as a definition as far as the .ll file goes.
3654 if (EatIfPresent(lltok::kw_opaque)) {
3655 // This type is being defined, so clear the location to indicate this.
3656 Entry.second = SMLoc();
3657
3658 // If this type number has never been uttered, create it.
3659 if (!Entry.first)
3660 Entry.first = StructType::create(Context, Name);
3661 ResultTy = Entry.first;
3662 return false;
3663 }
3664
3665 // If the type starts with '<', then it is either a packed struct or a vector.
3666 bool isPacked = EatIfPresent(lltok::less);
3667
3668 // If we don't have a struct, then we have a random type alias, which we
3669 // accept for compatibility with old files. These types are not allowed to be
3670 // forward referenced and not allowed to be recursive.
3671 if (Lex.getKind() != lltok::lbrace) {
3672 if (Entry.first)
3673 return error(TypeLoc, "forward references to non-struct type");
3674
3675 ResultTy = nullptr;
3676 if (isPacked)
3677 return parseArrayVectorType(ResultTy, true);
3678 return parseType(ResultTy);
3679 }
3680
3681 // This type is being defined, so clear the location to indicate this.
3682 Entry.second = SMLoc();
3683
3684 // If this type number has never been uttered, create it.
3685 if (!Entry.first)
3686 Entry.first = StructType::create(Context, Name);
3687
3688 StructType *STy = cast<StructType>(Entry.first);
3689
3691 if (parseStructBody(Body) ||
3692 (isPacked && parseToken(lltok::greater, "expected '>' in packed struct")))
3693 return true;
3694
3695 if (auto E = STy->setBodyOrError(Body, isPacked))
3696 return tokError(toString(std::move(E)));
3697
3698 ResultTy = STy;
3699 return false;
3700}
3701
3702/// parseStructType: Handles packed and unpacked types. </> parsed elsewhere.
3703/// StructType
3704/// ::= '{' '}'
3705/// ::= '{' Type (',' Type)* '}'
3706/// ::= '<' '{' '}' '>'
3707/// ::= '<' '{' Type (',' Type)* '}' '>'
3708bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) {
3709 assert(Lex.getKind() == lltok::lbrace);
3710 Lex.Lex(); // Consume the '{'
3711
3712 // Handle the empty struct.
3713 if (EatIfPresent(lltok::rbrace))
3714 return false;
3715
3716 LocTy EltTyLoc = Lex.getLoc();
3717 Type *Ty = nullptr;
3718 if (parseType(Ty))
3719 return true;
3720 Body.push_back(Ty);
3721
3723 return error(EltTyLoc, "invalid element type for struct");
3724
3725 while (EatIfPresent(lltok::comma)) {
3726 EltTyLoc = Lex.getLoc();
3727 if (parseType(Ty))
3728 return true;
3729
3731 return error(EltTyLoc, "invalid element type for struct");
3732
3733 Body.push_back(Ty);
3734 }
3735
3736 return parseToken(lltok::rbrace, "expected '}' at end of struct");
3737}
3738
3739/// parseArrayVectorType - parse an array or vector type, assuming the first
3740/// token has already been consumed.
3741/// Type
3742/// ::= '[' APSINTVAL 'x' Types ']'
3743/// ::= '<' APSINTVAL 'x' Types '>'
3744/// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
3745bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) {
3746 bool Scalable = false;
3747
3748 if (IsVector && Lex.getKind() == lltok::kw_vscale) {
3749 Lex.Lex(); // consume the 'vscale'
3750 if (parseToken(lltok::kw_x, "expected 'x' after vscale"))
3751 return true;
3752
3753 Scalable = true;
3754 }
3755
3756 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
3757 Lex.getAPSIntVal().getBitWidth() > 64)
3758 return tokError("expected number in address space");
3759
3760 LocTy SizeLoc = Lex.getLoc();
3761 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
3762 Lex.Lex();
3763
3764 if (parseToken(lltok::kw_x, "expected 'x' after element count"))
3765 return true;
3766
3767 LocTy TypeLoc = Lex.getLoc();
3768 Type *EltTy = nullptr;
3769 if (parseType(EltTy))
3770 return true;
3771
3772 if (parseToken(IsVector ? lltok::greater : lltok::rsquare,
3773 "expected end of sequential type"))
3774 return true;
3775
3776 if (IsVector) {
3777 if (Size == 0)
3778 return error(SizeLoc, "zero element vector is illegal");
3779 if ((unsigned)Size != Size)
3780 return error(SizeLoc, "size too large for vector");
3782 return error(TypeLoc, "invalid vector element type");
3783 Result = VectorType::get(EltTy, unsigned(Size), Scalable);
3784 } else {
3786 return error(TypeLoc, "invalid array element type");
3787 Result = ArrayType::get(EltTy, Size);
3788 }
3789 return false;
3790}
3791
3792/// parseTargetExtType - handle target extension type syntax
3793/// TargetExtType
3794/// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')'
3795///
3796/// TargetExtTypeParams
3797/// ::= /*empty*/
3798/// ::= ',' Type TargetExtTypeParams
3799///
3800/// TargetExtIntParams
3801/// ::= /*empty*/
3802/// ::= ',' uint32 TargetExtIntParams
3803bool LLParser::parseTargetExtType(Type *&Result) {
3804 Lex.Lex(); // Eat the 'target' keyword.
3805
3806 // Get the mandatory type name.
3807 std::string TypeName;
3808 if (parseToken(lltok::lparen, "expected '(' in target extension type") ||
3809 parseStringConstant(TypeName))
3810 return true;
3811
3812 // Parse all of the integer and type parameters at the same time; the use of
3813 // SeenInt will allow us to catch cases where type parameters follow integer
3814 // parameters.
3815 SmallVector<Type *> TypeParams;
3816 SmallVector<unsigned> IntParams;
3817 bool SeenInt = false;
3818 while (Lex.getKind() == lltok::comma) {
3819 Lex.Lex(); // Eat the comma.
3820
3821 if (Lex.getKind() == lltok::APSInt) {
3822 SeenInt = true;
3823 unsigned IntVal;
3824 if (parseUInt32(IntVal))
3825 return true;
3826 IntParams.push_back(IntVal);
3827 } else if (SeenInt) {
3828 // The only other kind of parameter we support is type parameters, which
3829 // must precede the integer parameters. This is therefore an error.
3830 return tokError("expected uint32 param");
3831 } else {
3832 Type *TypeParam;
3833 if (parseType(TypeParam, /*AllowVoid=*/true))
3834 return true;
3835 TypeParams.push_back(TypeParam);
3836 }
3837 }
3838
3839 if (parseToken(lltok::rparen, "expected ')' in target extension type"))
3840 return true;
3841
3842 auto TTy =
3843 TargetExtType::getOrError(Context, TypeName, TypeParams, IntParams);
3844 if (auto E = TTy.takeError())
3845 return tokError(toString(std::move(E)));
3846
3847 Result = *TTy;
3848 return false;
3849}
3850
3851//===----------------------------------------------------------------------===//
3852// Function Semantic Analysis.
3853//===----------------------------------------------------------------------===//
3854
3855LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
3856 int functionNumber,
3857 ArrayRef<unsigned> UnnamedArgNums)
3858 : P(p), F(f), FunctionNumber(functionNumber) {
3859
3860 // Insert unnamed arguments into the NumberedVals list.
3861 auto It = UnnamedArgNums.begin();
3862 for (Argument &A : F.args()) {
3863 if (!A.hasName()) {
3864 unsigned ArgNum = *It++;
3865 NumberedVals.add(ArgNum, &A);
3866 }
3867 }
3868}
3869
3870LLParser::PerFunctionState::~PerFunctionState() {
3871 // If there were any forward referenced non-basicblock values, delete them.
3872
3873 for (const auto &P : ForwardRefVals) {
3874 if (isa<BasicBlock>(P.second.first))
3875 continue;
3876 P.second.first->replaceAllUsesWith(
3877 PoisonValue::get(P.second.first->getType()));
3878 P.second.first->deleteValue();
3879 }
3880
3881 for (const auto &P : ForwardRefValIDs) {
3882 if (isa<BasicBlock>(P.second.first))
3883 continue;
3884 P.second.first->replaceAllUsesWith(
3885 PoisonValue::get(P.second.first->getType()));
3886 P.second.first->deleteValue();
3887 }
3888}
3889
3890bool LLParser::PerFunctionState::finishFunction() {
3891 if (!ForwardRefVals.empty())
3892 return P.error(ForwardRefVals.begin()->second.second,
3893 "use of undefined value '%" + ForwardRefVals.begin()->first +
3894 "'");
3895 if (!ForwardRefValIDs.empty())
3896 return P.error(ForwardRefValIDs.begin()->second.second,
3897 "use of undefined value '%" +
3898 Twine(ForwardRefValIDs.begin()->first) + "'");
3899 return false;
3900}
3901
3902/// getVal - Get a value with the specified name or ID, creating a
3903/// forward reference record if needed. This can return null if the value
3904/// exists but does not have the right type.
3905Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty,
3906 LocTy Loc) {
3907 // Look this name up in the normal function symbol table.
3908 Value *Val = F.getValueSymbolTable()->lookup(Name);
3909
3910 // If this is a forward reference for the value, see if we already created a
3911 // forward ref record.
3912 if (!Val) {
3913 auto I = ForwardRefVals.find(Name);
3914 if (I != ForwardRefVals.end())
3915 Val = I->second.first;
3916 }
3917
3918 // If we have the value in the symbol table or fwd-ref table, return it.
3919 if (Val)
3920 return P.checkValidVariableType(Loc, "%" + Name, Ty, Val);
3921
3922 // Don't make placeholders with invalid type.
3923 if (!Ty->isFirstClassType()) {
3924 P.error(Loc, "invalid use of a non-first-class type");
3925 return nullptr;
3926 }
3927
3928 // Otherwise, create a new forward reference for this value and remember it.
3929 Value *FwdVal;
3930 if (Ty->isLabelTy()) {
3931 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
3932 } else {
3933 FwdVal = new Argument(Ty, Name);
3934 }
3935 if (FwdVal->getName() != Name) {
3936 P.error(Loc, "name is too long which can result in name collisions, "
3937 "consider making the name shorter or "
3938 "increasing -non-global-value-max-name-size");
3939 return nullptr;
3940 }
3941
3942 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
3943 return FwdVal;
3944}
3945
3946Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) {
3947 // Look this name up in the normal function symbol table.
3948 Value *Val = NumberedVals.get(ID);
3949
3950 // If this is a forward reference for the value, see if we already created a
3951 // forward ref record.
3952 if (!Val) {
3953 auto I = ForwardRefValIDs.find(ID);
3954 if (I != ForwardRefValIDs.end())
3955 Val = I->second.first;
3956 }
3957
3958 // If we have the value in the symbol table or fwd-ref table, return it.
3959 if (Val)
3960 return P.checkValidVariableType(Loc, "%" + Twine(ID), Ty, Val);
3961
3962 if (!Ty->isFirstClassType()) {
3963 P.error(Loc, "invalid use of a non-first-class type");
3964 return nullptr;
3965 }
3966
3967 // Otherwise, create a new forward reference for this value and remember it.
3968 Value *FwdVal;
3969 if (Ty->isLabelTy()) {
3970 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
3971 } else {
3972 FwdVal = new Argument(Ty);
3973 }
3974
3975 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
3976 return FwdVal;
3977}
3978
3979/// setInstName - After an instruction is parsed and inserted into its
3980/// basic block, this installs its name.
3981bool LLParser::PerFunctionState::setInstName(int NameID,
3982 const std::string &NameStr,
3983 LocTy NameLoc, Instruction *Inst) {
3984 // If this instruction has void type, it cannot have a name or ID specified.
3985 if (Inst->getType()->isVoidTy()) {
3986 if (NameID != -1 || !NameStr.empty())
3987 return P.error(NameLoc, "instructions returning void cannot have a name");
3988 return false;
3989 }
3990
3991 // If this was a numbered instruction, verify that the instruction is the
3992 // expected value and resolve any forward references.
3993 if (NameStr.empty()) {
3994 // If neither a name nor an ID was specified, just use the next ID.
3995 if (NameID == -1)
3996 NameID = NumberedVals.getNext();
3997
3998 if (P.checkValueID(NameLoc, "instruction", "%", NumberedVals.getNext(),
3999 NameID))
4000 return true;
4001
4002 auto FI = ForwardRefValIDs.find(NameID);
4003 if (FI != ForwardRefValIDs.end()) {
4004 Value *Sentinel = FI->second.first;
4005 if (Sentinel->getType() != Inst->getType())
4006 return P.error(NameLoc, "instruction forward referenced with type '" +
4007 getTypeString(FI->second.first->getType()) +
4008 "'");
4009
4010 Sentinel->replaceAllUsesWith(Inst);
4011 Sentinel->deleteValue();
4012 ForwardRefValIDs.erase(FI);
4013 }
4014
4015 NumberedVals.add(NameID, Inst);
4016 return false;
4017 }
4018
4019 // Otherwise, the instruction had a name. Resolve forward refs and set it.
4020 auto FI = ForwardRefVals.find(NameStr);
4021 if (FI != ForwardRefVals.end()) {
4022 Value *Sentinel = FI->second.first;
4023 if (Sentinel->getType() != Inst->getType())
4024 return P.error(NameLoc, "instruction forward referenced with type '" +
4025 getTypeString(FI->second.first->getType()) +
4026 "'");
4027
4028 Sentinel->replaceAllUsesWith(Inst);
4029 Sentinel->deleteValue();
4030 ForwardRefVals.erase(FI);
4031 }
4032
4033 // Set the name on the instruction.
4034 Inst->setName(NameStr);
4035
4036 if (Inst->getName() != NameStr)
4037 return P.error(NameLoc, "multiple definition of local value named '" +
4038 NameStr + "'");
4039 return false;
4040}
4041
4042/// getBB - Get a basic block with the specified name or ID, creating a
4043/// forward reference record if needed.
4044BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name,
4045 LocTy Loc) {
4047 getVal(Name, Type::getLabelTy(F.getContext()), Loc));
4048}
4049
4050BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) {
4052 getVal(ID, Type::getLabelTy(F.getContext()), Loc));
4053}
4054
4055/// defineBB - Define the specified basic block, which is either named or
4056/// unnamed. If there is an error, this returns null otherwise it returns
4057/// the block being defined.
4058BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
4059 int NameID, LocTy Loc) {
4060 BasicBlock *BB;
4061 if (Name.empty()) {
4062 if (NameID != -1) {
4063 if (P.checkValueID(Loc, "label", "", NumberedVals.getNext(), NameID))
4064 return nullptr;
4065 } else {
4066 NameID = NumberedVals.getNext();
4067 }
4068 BB = getBB(NameID, Loc);
4069 if (!BB) {
4070 P.error(Loc, "unable to create block numbered '" + Twine(NameID) + "'");
4071 return nullptr;
4072 }
4073 } else {
4074 BB = getBB(Name, Loc);
4075 if (!BB) {
4076 P.error(Loc, "unable to create block named '" + Name + "'");
4077 return nullptr;
4078 }
4079 }
4080
4081 // Move the block to the end of the function. Forward ref'd blocks are
4082 // inserted wherever they happen to be referenced.
4083 F.splice(F.end(), &F, BB->getIterator());
4084
4085 // Remove the block from forward ref sets.
4086 if (Name.empty()) {
4087 ForwardRefValIDs.erase(NameID);
4088 NumberedVals.add(NameID, BB);
4089 } else {
4090 // BB forward references are already in the function symbol table.
4091 ForwardRefVals.erase(Name);
4092 }
4093
4094 return BB;
4095}
4096
4097//===----------------------------------------------------------------------===//
4098// Constants.
4099//===----------------------------------------------------------------------===//
4100
4101/// parseValID - parse an abstract value that doesn't necessarily have a
4102/// type implied. For example, if we parse "4" we don't know what integer type
4103/// it has. The value will later be combined with its type and checked for
4104/// basic correctness. PFS is used to convert function-local operands of
4105/// metadata (since metadata operands are not just parsed here but also
4106/// converted to values). PFS can be null when we are not parsing metadata
4107/// values inside a function.
4108bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
4109 ID.Loc = Lex.getLoc();
4110 switch (Lex.getKind()) {
4111 default:
4112 return tokError("expected value token");
4113 case lltok::GlobalID: // @42
4114 ID.UIntVal = Lex.getUIntVal();
4115 ID.Kind = ValID::t_GlobalID;
4116 break;
4117 case lltok::GlobalVar: // @foo
4118 ID.StrVal = Lex.getStrVal();
4119 ID.Kind = ValID::t_GlobalName;
4120 break;
4121 case lltok::LocalVarID: // %42
4122 ID.UIntVal = Lex.getUIntVal();
4123 ID.Kind = ValID::t_LocalID;
4124 break;
4125 case lltok::LocalVar: // %foo
4126 ID.StrVal = Lex.getStrVal();
4127 ID.Kind = ValID::t_LocalName;
4128 break;
4129 case lltok::APSInt:
4130 ID.APSIntVal = Lex.getAPSIntVal();
4131 ID.Kind = ValID::t_APSInt;
4132 break;
4133 case lltok::APFloat:
4134 ID.APFloatVal = Lex.getAPFloatVal();
4135 ID.Kind = ValID::t_APFloat;
4136 break;
4137 case lltok::kw_true:
4138 ID.ConstantVal = ConstantInt::getTrue(Context);
4139 ID.Kind = ValID::t_Constant;
4140 break;
4141 case lltok::kw_false:
4142 ID.ConstantVal = ConstantInt::getFalse(Context);
4143 ID.Kind = ValID::t_Constant;
4144 break;
4145 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
4146 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
4147 case lltok::kw_poison: ID.Kind = ValID::t_Poison; break;
4148 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
4149 case lltok::kw_none: ID.Kind = ValID::t_None; break;
4150
4151 case lltok::lbrace: {
4152 // ValID ::= '{' ConstVector '}'
4153 Lex.Lex();
4155 if (parseGlobalValueVector(Elts) ||
4156 parseToken(lltok::rbrace, "expected end of struct constant"))
4157 return true;
4158
4159 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
4160 ID.UIntVal = Elts.size();
4161 memcpy(ID.ConstantStructElts.get(), Elts.data(),
4162 Elts.size() * sizeof(Elts[0]));
4164 return false;
4165 }
4166 case lltok::less: {
4167 // ValID ::= '<' ConstVector '>' --> Vector.
4168 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
4169 Lex.Lex();
4170 bool isPackedStruct = EatIfPresent(lltok::lbrace);
4171
4173 LocTy FirstEltLoc = Lex.getLoc();
4174 if (parseGlobalValueVector(Elts) ||
4175 (isPackedStruct &&
4176 parseToken(lltok::rbrace, "expected end of packed struct")) ||
4177 parseToken(lltok::greater, "expected end of constant"))
4178 return true;
4179
4180 if (isPackedStruct) {
4181 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
4182 memcpy(ID.ConstantStructElts.get(), Elts.data(),
4183 Elts.size() * sizeof(Elts[0]));
4184 ID.UIntVal = Elts.size();
4186 return false;
4187 }
4188
4189 if (Elts.empty())
4190 return error(ID.Loc, "constant vector must not be empty");
4191
4192 if (!Elts[0]->getType()->isIntegerTy() &&
4193 !Elts[0]->getType()->isFloatingPointTy() &&
4194 !Elts[0]->getType()->isPointerTy())
4195 return error(
4196 FirstEltLoc,
4197 "vector elements must have integer, pointer or floating point type");
4198
4199 // Verify that all the vector elements have the same type.
4200 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
4201 if (Elts[i]->getType() != Elts[0]->getType())
4202 return error(FirstEltLoc, "vector element #" + Twine(i) +
4203 " is not of type '" +
4204 getTypeString(Elts[0]->getType()));
4205
4206 ID.ConstantVal = ConstantVector::get(Elts);
4207 ID.Kind = ValID::t_Constant;
4208 return false;
4209 }
4210 case lltok::lsquare: { // Array Constant
4211 Lex.Lex();
4213 LocTy FirstEltLoc = Lex.getLoc();
4214 if (parseGlobalValueVector(Elts) ||
4215 parseToken(lltok::rsquare, "expected end of array constant"))
4216 return true;
4217
4218 // Handle empty element.
4219 if (Elts.empty()) {
4220 // Use undef instead of an array because it's inconvenient to determine
4221 // the element type at this point, there being no elements to examine.
4222 ID.Kind = ValID::t_EmptyArray;
4223 return false;
4224 }
4225
4226 if (!Elts[0]->getType()->isFirstClassType())
4227 return error(FirstEltLoc, "invalid array element type: " +
4228 getTypeString(Elts[0]->getType()));
4229
4230 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
4231
4232 // Verify all elements are correct type!
4233 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
4234 if (Elts[i]->getType() != Elts[0]->getType())
4235 return error(FirstEltLoc, "array element #" + Twine(i) +
4236 " is not of type '" +
4237 getTypeString(Elts[0]->getType()));
4238 }
4239
4240 ID.ConstantVal = ConstantArray::get(ATy, Elts);
4241 ID.Kind = ValID::t_Constant;
4242 return false;
4243 }
4244 case lltok::kw_c: // c "foo"
4245 Lex.Lex();
4246 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
4247 false);
4248 if (parseToken(lltok::StringConstant, "expected string"))
4249 return true;
4250 ID.Kind = ValID::t_Constant;
4251 return false;
4252
4253 case lltok::kw_asm: {
4254 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
4255 // STRINGCONSTANT
4256 bool HasSideEffect, AlignStack, AsmDialect, CanThrow;
4257 Lex.Lex();
4258 if (parseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
4259 parseOptionalToken(lltok::kw_alignstack, AlignStack) ||
4260 parseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
4261 parseOptionalToken(lltok::kw_unwind, CanThrow) ||
4262 parseStringConstant(ID.StrVal) ||
4263 parseToken(lltok::comma, "expected comma in inline asm expression") ||
4264 parseToken(lltok::StringConstant, "expected constraint string"))
4265 return true;
4266 ID.StrVal2 = Lex.getStrVal();
4267 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) |
4268 (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3);
4269 ID.Kind = ValID::t_InlineAsm;
4270 return false;
4271 }
4272
4274 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
4275 Lex.Lex();
4276
4277 ValID Fn, Label;
4278
4279 if (parseToken(lltok::lparen, "expected '(' in block address expression") ||
4280 parseValID(Fn, PFS) ||
4281 parseToken(lltok::comma,
4282 "expected comma in block address expression") ||
4283 parseValID(Label, PFS) ||
4284 parseToken(lltok::rparen, "expected ')' in block address expression"))
4285 return true;
4286
4288 return error(Fn.Loc, "expected function name in blockaddress");
4289 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
4290 return error(Label.Loc, "expected basic block name in blockaddress");
4291
4292 // Try to find the function (but skip it if it's forward-referenced).
4293 GlobalValue *GV = nullptr;
4294 if (Fn.Kind == ValID::t_GlobalID) {
4295 GV = NumberedVals.get(Fn.UIntVal);
4296 } else if (!ForwardRefVals.count(Fn.StrVal)) {
4297 GV = M->getNamedValue(Fn.StrVal);
4298 }
4299 Function *F = nullptr;
4300 if (GV) {
4301 // Confirm that it's actually a function with a definition.
4302 if (!isa<Function>(GV))
4303 return error(Fn.Loc, "expected function name in blockaddress");
4304 F = cast<Function>(GV);
4305 if (F->isDeclaration())
4306 return error(Fn.Loc, "cannot take blockaddress inside a declaration");
4307 }
4308
4309 if (!F) {
4310 // Make a global variable as a placeholder for this reference.
4311 GlobalValue *&FwdRef =
4312 ForwardRefBlockAddresses[std::move(Fn)][std::move(Label)];
4313 if (!FwdRef) {
4314 unsigned FwdDeclAS;
4315 if (ExpectedTy) {
4316 // If we know the type that the blockaddress is being assigned to,
4317 // we can use the address space of that type.
4318 if (!ExpectedTy->isPointerTy())
4319 return error(ID.Loc,
4320 "type of blockaddress must be a pointer and not '" +
4321 getTypeString(ExpectedTy) + "'");
4322 FwdDeclAS = ExpectedTy->getPointerAddressSpace();
4323 } else if (PFS) {
4324 // Otherwise, we default the address space of the current function.
4325 FwdDeclAS = PFS->getFunction().getAddressSpace();
4326 } else {
4327 llvm_unreachable("Unknown address space for blockaddress");
4328 }
4329 FwdRef = new GlobalVariable(
4330 *M, Type::getInt8Ty(Context), false, GlobalValue::InternalLinkage,
4331 nullptr, "", nullptr, GlobalValue::NotThreadLocal, FwdDeclAS);
4332 }
4333
4334 ID.ConstantVal = FwdRef;
4335 ID.Kind = ValID::t_Constant;
4336 return false;
4337 }
4338
4339 // We found the function; now find the basic block. Don't use PFS, since we
4340 // might be inside a constant expression.
4341 BasicBlock *BB;
4342 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
4343 if (Label.Kind == ValID::t_LocalID)
4344 BB = BlockAddressPFS->getBB(Label.UIntVal, Label.Loc);
4345 else
4346 BB = BlockAddressPFS->getBB(Label.StrVal, Label.Loc);
4347 if (!BB)
4348 return error(Label.Loc, "referenced value is not a basic block");
4349 } else {
4350 if (Label.Kind == ValID::t_LocalID)
4351 return error(Label.Loc, "cannot take address of numeric label after "
4352 "the function is defined");
4354 F->getValueSymbolTable()->lookup(Label.StrVal));
4355 if (!BB)
4356 return error(Label.Loc, "referenced value is not a basic block");
4357 }
4358
4359 ID.ConstantVal = BlockAddress::get(F, BB);
4360 ID.Kind = ValID::t_Constant;
4361 return false;
4362 }
4363
4365 // ValID ::= 'dso_local_equivalent' @foo
4366 Lex.Lex();
4367
4368 ValID Fn;
4369
4370 if (parseValID(Fn, PFS))
4371 return true;
4372
4374 return error(Fn.Loc,
4375 "expected global value name in dso_local_equivalent");
4376
4377 // Try to find the function (but skip it if it's forward-referenced).
4378 GlobalValue *GV = nullptr;
4379 if (Fn.Kind == ValID::t_GlobalID) {
4380 GV = NumberedVals.get(Fn.UIntVal);
4381 } else if (!ForwardRefVals.count(Fn.StrVal)) {
4382 GV = M->getNamedValue(Fn.StrVal);
4383 }
4384
4385 if (!GV) {
4386 // Make a placeholder global variable as a placeholder for this reference.
4387 auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID)
4388 ? ForwardRefDSOLocalEquivalentIDs
4389 : ForwardRefDSOLocalEquivalentNames;
4390 GlobalValue *&FwdRef = FwdRefMap[Fn];
4391 if (!FwdRef) {
4392 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
4393 GlobalValue::InternalLinkage, nullptr, "",
4395 }
4396
4397 ID.ConstantVal = FwdRef;
4398 ID.Kind = ValID::t_Constant;
4399 return false;
4400 }
4401
4402 if (!GV->getValueType()->isFunctionTy())
4403 return error(Fn.Loc, "expected a function, alias to function, or ifunc "
4404 "in dso_local_equivalent");
4405
4406 ID.ConstantVal = DSOLocalEquivalent::get(GV);
4407 ID.Kind = ValID::t_Constant;
4408 return false;
4409 }
4410
4411 case lltok::kw_no_cfi: {
4412 // ValID ::= 'no_cfi' @foo
4413 Lex.Lex();
4414
4415 if (parseValID(ID, PFS))
4416 return true;
4417
4418 if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName)
4419 return error(ID.Loc, "expected global value name in no_cfi");
4420
4421 ID.NoCFI = true;
4422 return false;
4423 }
4424 case lltok::kw_ptrauth: {
4425 // ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key>
4426 // (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)?
4427 // )? )? ')'
4428 Lex.Lex();
4429
4430 Constant *Ptr, *Key;
4431 Constant *Disc = nullptr, *AddrDisc = nullptr,
4432 *DeactivationSymbol = nullptr;
4433
4434 if (parseToken(lltok::lparen,
4435 "expected '(' in constant ptrauth expression") ||
4436 parseGlobalTypeAndValue(Ptr) ||
4437 parseToken(lltok::comma,
4438 "expected comma in constant ptrauth expression") ||
4439 parseGlobalTypeAndValue(Key))
4440 return true;
4441 // If present, parse the optional disc/addrdisc/ds.
4442 if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(Disc))
4443 return true;
4444 if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc))
4445 return true;
4446 if (EatIfPresent(lltok::comma) &&
4447 parseGlobalTypeAndValue(DeactivationSymbol))
4448 return true;
4449 if (parseToken(lltok::rparen,
4450 "expected ')' in constant ptrauth expression"))
4451 return true;
4452
4453 if (!Ptr->getType()->isPointerTy())
4454 return error(ID.Loc, "constant ptrauth base pointer must be a pointer");
4455
4456 auto *KeyC = dyn_cast<ConstantInt>(Key);
4457 if (!KeyC || KeyC->getBitWidth() != 32)
4458 return error(ID.Loc, "constant ptrauth key must be i32 constant");
4459
4460 ConstantInt *DiscC = nullptr;
4461 if (Disc) {
4462 DiscC = dyn_cast<ConstantInt>(Disc);
4463 if (!DiscC || DiscC->getBitWidth() != 64)
4464 return error(
4465 ID.Loc,
4466 "constant ptrauth integer discriminator must be i64 constant");
4467 } else {
4468 DiscC = ConstantInt::get(Type::getInt64Ty(Context), 0);
4469 }
4470
4471 if (AddrDisc) {
4472 if (!AddrDisc->getType()->isPointerTy())
4473 return error(
4474 ID.Loc, "constant ptrauth address discriminator must be a pointer");
4475 } else {
4476 AddrDisc = ConstantPointerNull::get(PointerType::get(Context, 0));
4477 }
4478
4479 if (!DeactivationSymbol)
4480 DeactivationSymbol =
4482 if (!DeactivationSymbol->getType()->isPointerTy())
4483 return error(ID.Loc,
4484 "constant ptrauth deactivation symbol must be a pointer");
4485
4486 ID.ConstantVal =
4487 ConstantPtrAuth::get(Ptr, KeyC, DiscC, AddrDisc, DeactivationSymbol);
4488 ID.Kind = ValID::t_Constant;
4489 return false;
4490 }
4491
4492 case lltok::kw_trunc:
4493 case lltok::kw_bitcast:
4495 case lltok::kw_inttoptr:
4497 case lltok::kw_ptrtoint: {
4498 unsigned Opc = Lex.getUIntVal();
4499 Type *DestTy = nullptr;
4500 Constant *SrcVal;
4501 Lex.Lex();
4502 if (parseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
4503 parseGlobalTypeAndValue(SrcVal) ||
4504 parseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
4505 parseType(DestTy) ||
4506 parseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
4507 return true;
4508 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
4509 return error(ID.Loc, "invalid cast opcode for cast from '" +
4510 getTypeString(SrcVal->getType()) + "' to '" +
4511 getTypeString(DestTy) + "'");
4513 SrcVal, DestTy);
4514 ID.Kind = ValID::t_Constant;
4515 return false;
4516 }
4518 return error(ID.Loc, "extractvalue constexprs are no longer supported");
4520 return error(ID.Loc, "insertvalue constexprs are no longer supported");
4521 case lltok::kw_udiv:
4522 return error(ID.Loc, "udiv constexprs are no longer supported");
4523 case lltok::kw_sdiv:
4524 return error(ID.Loc, "sdiv constexprs are no longer supported");
4525 case lltok::kw_urem:
4526 return error(ID.Loc, "urem constexprs are no longer supported");
4527 case lltok::kw_srem:
4528 return error(ID.Loc, "srem constexprs are no longer supported");
4529 case lltok::kw_fadd:
4530 return error(ID.Loc, "fadd constexprs are no longer supported");
4531 case lltok::kw_fsub:
4532 return error(ID.Loc, "fsub constexprs are no longer supported");
4533 case lltok::kw_fmul:
4534 return error(ID.Loc, "fmul constexprs are no longer supported");
4535 case lltok::kw_fdiv:
4536 return error(ID.Loc, "fdiv constexprs are no longer supported");
4537 case lltok::kw_frem:
4538 return error(ID.Loc, "frem constexprs are no longer supported");
4539 case lltok::kw_and:
4540 return error(ID.Loc, "and constexprs are no longer supported");
4541 case lltok::kw_or:
4542 return error(ID.Loc, "or constexprs are no longer supported");
4543 case lltok::kw_lshr:
4544 return error(ID.Loc, "lshr constexprs are no longer supported");
4545 case lltok::kw_ashr:
4546 return error(ID.Loc, "ashr constexprs are no longer supported");
4547 case lltok::kw_shl:
4548 return error(ID.Loc, "shl constexprs are no longer supported");
4549 case lltok::kw_mul:
4550 return error(ID.Loc, "mul constexprs are no longer supported");
4551 case lltok::kw_fneg:
4552 return error(ID.Loc, "fneg constexprs are no longer supported");
4553 case lltok::kw_select:
4554 return error(ID.Loc, "select constexprs are no longer supported");
4555 case lltok::kw_zext:
4556 return error(ID.Loc, "zext constexprs are no longer supported");
4557 case lltok::kw_sext:
4558 return error(ID.Loc, "sext constexprs are no longer supported");
4559 case lltok::kw_fptrunc:
4560 return error(ID.Loc, "fptrunc constexprs are no longer supported");
4561 case lltok::kw_fpext:
4562 return error(ID.Loc, "fpext constexprs are no longer supported");
4563 case lltok::kw_uitofp:
4564 return error(ID.Loc, "uitofp constexprs are no longer supported");
4565 case lltok::kw_sitofp:
4566 return error(ID.Loc, "sitofp constexprs are no longer supported");
4567 case lltok::kw_fptoui:
4568 return error(ID.Loc, "fptoui constexprs are no longer supported");
4569 case lltok::kw_fptosi:
4570 return error(ID.Loc, "fptosi constexprs are no longer supported");
4571 case lltok::kw_icmp:
4572 return error(ID.Loc, "icmp constexprs are no longer supported");
4573 case lltok::kw_fcmp:
4574 return error(ID.Loc, "fcmp constexprs are no longer supported");
4575
4576 // Binary Operators.
4577 case lltok::kw_add:
4578 case lltok::kw_sub:
4579 case lltok::kw_xor: {
4580 bool NUW = false;
4581 bool NSW = false;
4582 unsigned Opc = Lex.getUIntVal();
4583 Constant *Val0, *Val1;
4584 Lex.Lex();
4585 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
4586 Opc == Instruction::Mul) {
4587 if (EatIfPresent(lltok::kw_nuw))
4588 NUW = true;
4589 if (EatIfPresent(lltok::kw_nsw)) {
4590 NSW = true;
4591 if (EatIfPresent(lltok::kw_nuw))
4592 NUW = true;
4593 }
4594 }
4595 if (parseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
4596 parseGlobalTypeAndValue(Val0) ||
4597 parseToken(lltok::comma, "expected comma in binary constantexpr") ||
4598 parseGlobalTypeAndValue(Val1) ||
4599 parseToken(lltok::rparen, "expected ')' in binary constantexpr"))
4600 return true;
4601 if (Val0->getType() != Val1->getType())
4602 return error(ID.Loc, "operands of constexpr must have same type");
4603 // Check that the type is valid for the operator.
4604 if (!Val0->getType()->isIntOrIntVectorTy())
4605 return error(ID.Loc,
4606 "constexpr requires integer or integer vector operands");
4607 unsigned Flags = 0;
4610 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1, Flags);
4611 ID.Kind = ValID::t_Constant;
4612 return false;
4613 }
4614
4615 case lltok::kw_splat: {
4616 Lex.Lex();
4617 if (parseToken(lltok::lparen, "expected '(' after vector splat"))
4618 return true;
4619 Constant *C;
4620 if (parseGlobalTypeAndValue(C))
4621 return true;
4622 if (parseToken(lltok::rparen, "expected ')' at end of vector splat"))
4623 return true;
4624
4625 ID.ConstantVal = C;
4627 return false;
4628 }
4629
4634 unsigned Opc = Lex.getUIntVal();
4636 GEPNoWrapFlags NW;
4637 bool HasInRange = false;
4638 APSInt InRangeStart;
4639 APSInt InRangeEnd;
4640 Type *Ty;
4641 Lex.Lex();
4642
4643 if (Opc == Instruction::GetElementPtr) {
4644 while (true) {
4645 if (EatIfPresent(lltok::kw_inbounds))
4647 else if (EatIfPresent(lltok::kw_nusw))
4649 else if (EatIfPresent(lltok::kw_nuw))
4651 else
4652 break;
4653 }
4654
4655 if (EatIfPresent(lltok::kw_inrange)) {
4656 if (parseToken(lltok::lparen, "expected '('"))
4657 return true;
4658 if (Lex.getKind() != lltok::APSInt)
4659 return tokError("expected integer");
4660 InRangeStart = Lex.getAPSIntVal();
4661 Lex.Lex();
4662 if (parseToken(lltok::comma, "expected ','"))
4663 return true;
4664 if (Lex.getKind() != lltok::APSInt)
4665 return tokError("expected integer");
4666 InRangeEnd = Lex.getAPSIntVal();
4667 Lex.Lex();
4668 if (parseToken(lltok::rparen, "expected ')'"))
4669 return true;
4670 HasInRange = true;
4671 }
4672 }
4673
4674 if (parseToken(lltok::lparen, "expected '(' in constantexpr"))
4675 return true;
4676
4677 if (Opc == Instruction::GetElementPtr) {
4678 if (parseType(Ty) ||
4679 parseToken(lltok::comma, "expected comma after getelementptr's type"))
4680 return true;
4681 }
4682
4683 if (parseGlobalValueVector(Elts) ||
4684 parseToken(lltok::rparen, "expected ')' in constantexpr"))
4685 return true;
4686
4687 if (Opc == Instruction::GetElementPtr) {
4688 if (Elts.size() == 0 ||
4689 !Elts[0]->getType()->isPtrOrPtrVectorTy())
4690 return error(ID.Loc, "base of getelementptr must be a pointer");
4691
4692 Type *BaseType = Elts[0]->getType();
4693 std::optional<ConstantRange> InRange;
4694 if (HasInRange) {
4695 unsigned IndexWidth =
4696 M->getDataLayout().getIndexTypeSizeInBits(BaseType);
4697 InRangeStart = InRangeStart.extOrTrunc(IndexWidth);
4698 InRangeEnd = InRangeEnd.extOrTrunc(IndexWidth);
4699 if (InRangeStart.sge(InRangeEnd))
4700 return error(ID.Loc, "expected end to be larger than start");
4701 InRange = ConstantRange::getNonEmpty(InRangeStart, InRangeEnd);
4702 }
4703
4704 unsigned GEPWidth =
4705 BaseType->isVectorTy()
4706 ? cast<FixedVectorType>(BaseType)->getNumElements()
4707 : 0;
4708
4709 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
4710 for (Constant *Val : Indices) {
4711 Type *ValTy = Val->getType();
4712 if (!ValTy->isIntOrIntVectorTy())
4713 return error(ID.Loc, "getelementptr index must be an integer");
4714 if (auto *ValVTy = dyn_cast<VectorType>(ValTy)) {
4715 unsigned ValNumEl = cast<FixedVectorType>(ValVTy)->getNumElements();
4716 if (GEPWidth && (ValNumEl != GEPWidth))
4717 return error(
4718 ID.Loc,
4719 "getelementptr vector index has a wrong number of elements");
4720 // GEPWidth may have been unknown because the base is a scalar,
4721 // but it is known now.
4722 GEPWidth = ValNumEl;
4723 }
4724 }
4725
4726 SmallPtrSet<Type*, 4> Visited;
4727 if (!Indices.empty() && !Ty->isSized(&Visited))
4728 return error(ID.Loc, "base element of getelementptr must be sized");
4729
4731 return error(ID.Loc, "invalid base element for constant getelementptr");
4732
4733 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
4734 return error(ID.Loc, "invalid getelementptr indices");
4735
4736 ID.ConstantVal =
4737 ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices, NW, InRange);
4738 } else if (Opc == Instruction::ShuffleVector) {
4739 if (Elts.size() != 3)
4740 return error(ID.Loc, "expected three operands to shufflevector");
4741 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4742 return error(ID.Loc, "invalid operands to shufflevector");
4743 SmallVector<int, 16> Mask;
4745 ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1], Mask);
4746 } else if (Opc == Instruction::ExtractElement) {
4747 if (Elts.size() != 2)
4748 return error(ID.Loc, "expected two operands to extractelement");
4749 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
4750 return error(ID.Loc, "invalid extractelement operands");
4751 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
4752 } else {
4753 assert(Opc == Instruction::InsertElement && "Unknown opcode");
4754 if (Elts.size() != 3)
4755 return error(ID.Loc, "expected three operands to insertelement");
4756 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4757 return error(ID.Loc, "invalid insertelement operands");
4758 ID.ConstantVal =
4759 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
4760 }
4761
4762 ID.Kind = ValID::t_Constant;
4763 return false;
4764 }
4765 }
4766
4767 Lex.Lex();
4768 return false;
4769}
4770
4771/// parseGlobalValue - parse a global value with the specified type.
4772bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) {
4773 C = nullptr;
4774 ValID ID;
4775 Value *V = nullptr;
4776 bool Parsed = parseValID(ID, /*PFS=*/nullptr, Ty) ||
4777 convertValIDToValue(Ty, ID, V, nullptr);
4778 if (V && !(C = dyn_cast<Constant>(V)))
4779 return error(ID.Loc, "global values must be constants");
4780 return Parsed;
4781}
4782
4783bool LLParser::parseGlobalTypeAndValue(Constant *&V) {
4784 Type *Ty = nullptr;
4785 return parseType(Ty) || parseGlobalValue(Ty, V);
4786}
4787
4788bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
4789 C = nullptr;
4790
4791 LocTy KwLoc = Lex.getLoc();
4792 if (!EatIfPresent(lltok::kw_comdat))
4793 return false;
4794
4795 if (EatIfPresent(lltok::lparen)) {
4796 if (Lex.getKind() != lltok::ComdatVar)
4797 return tokError("expected comdat variable");
4798 C = getComdat(Lex.getStrVal(), Lex.getLoc());
4799 Lex.Lex();
4800 if (parseToken(lltok::rparen, "expected ')' after comdat var"))
4801 return true;
4802 } else {
4803 if (GlobalName.empty())
4804 return tokError("comdat cannot be unnamed");
4805 C = getComdat(std::string(GlobalName), KwLoc);
4806 }
4807
4808 return false;
4809}
4810
4811/// parseGlobalValueVector
4812/// ::= /*empty*/
4813/// ::= TypeAndValue (',' TypeAndValue)*
4814bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
4815 // Empty list.
4816 if (Lex.getKind() == lltok::rbrace ||
4817 Lex.getKind() == lltok::rsquare ||
4818 Lex.getKind() == lltok::greater ||
4819 Lex.getKind() == lltok::rparen)
4820 return false;
4821
4822 do {
4823 // Let the caller deal with inrange.
4824 if (Lex.getKind() == lltok::kw_inrange)
4825 return false;
4826
4827 Constant *C;
4828 if (parseGlobalTypeAndValue(C))
4829 return true;
4830 Elts.push_back(C);
4831 } while (EatIfPresent(lltok::comma));
4832
4833 return false;
4834}
4835
4836bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
4838 if (parseMDNodeVector(Elts))
4839 return true;
4840
4841 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
4842 return false;
4843}
4844
4845/// MDNode:
4846/// ::= !{ ... }
4847/// ::= !7
4848/// ::= !DILocation(...)
4849bool LLParser::parseMDNode(MDNode *&N) {
4850 if (Lex.getKind() == lltok::MetadataVar)
4851 return parseSpecializedMDNode(N);
4852
4853 return parseToken(lltok::exclaim, "expected '!' here") || parseMDNodeTail(N);
4854}
4855
4856bool LLParser::parseMDNodeTail(MDNode *&N) {
4857 // !{ ... }
4858 if (Lex.getKind() == lltok::lbrace)
4859 return parseMDTuple(N);
4860
4861 // !42
4862 return parseMDNodeID(N);
4863}
4864
4865namespace {
4866
4867/// Structure to represent an optional metadata field.
4868template <class FieldTy> struct MDFieldImpl {
4869 typedef MDFieldImpl ImplTy;
4870 FieldTy Val;
4871 bool Seen;
4872
4873 void assign(FieldTy Val) {
4874 Seen = true;
4875 this->Val = std::move(Val);
4876 }
4877
4878 explicit MDFieldImpl(FieldTy Default)
4879 : Val(std::move(Default)), Seen(false) {}
4880};
4881
4882/// Structure to represent an optional metadata field that
4883/// can be of either type (A or B) and encapsulates the
4884/// MD<typeofA>Field and MD<typeofB>Field structs, so not
4885/// to reimplement the specifics for representing each Field.
4886template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
4887 typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
4888 FieldTypeA A;
4889 FieldTypeB B;
4890 bool Seen;
4891
4892 enum {
4893 IsInvalid = 0,
4894 IsTypeA = 1,
4895 IsTypeB = 2
4896 } WhatIs;
4897
4898 void assign(FieldTypeA A) {
4899 Seen = true;
4900 this->A = std::move(A);
4901 WhatIs = IsTypeA;
4902 }
4903
4904 void assign(FieldTypeB B) {
4905 Seen = true;
4906 this->B = std::move(B);
4907 WhatIs = IsTypeB;
4908 }
4909
4910 explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
4911 : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
4912 WhatIs(IsInvalid) {}
4913};
4914
4915struct MDUnsignedField : public MDFieldImpl<uint64_t> {
4916 uint64_t Max;
4917
4918 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
4919 : ImplTy(Default), Max(Max) {}
4920};
4921
4922struct LineField : public MDUnsignedField {
4923 LineField() : MDUnsignedField(0, UINT32_MAX) {}
4924};
4925
4926struct ColumnField : public MDUnsignedField {
4927 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
4928};
4929
4930struct DwarfTagField : public MDUnsignedField {
4931 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
4932 DwarfTagField(dwarf::Tag DefaultTag)
4933 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
4934};
4935
4936struct DwarfMacinfoTypeField : public MDUnsignedField {
4937 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
4938 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
4939 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
4940};
4941
4942struct DwarfAttEncodingField : public MDUnsignedField {
4943 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
4944};
4945
4946struct DwarfVirtualityField : public MDUnsignedField {
4947 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
4948};
4949
4950struct DwarfLangField : public MDUnsignedField {
4951 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
4952};
4953
4954struct DwarfSourceLangNameField : public MDUnsignedField {
4955 DwarfSourceLangNameField() : MDUnsignedField(0, UINT32_MAX) {}
4956};
4957
4958struct DwarfCCField : public MDUnsignedField {
4959 DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
4960};
4961
4962struct DwarfEnumKindField : public MDUnsignedField {
4963 DwarfEnumKindField()
4964 : MDUnsignedField(dwarf::DW_APPLE_ENUM_KIND_invalid,
4965 dwarf::DW_APPLE_ENUM_KIND_max) {}
4966};
4967
4968struct EmissionKindField : public MDUnsignedField {
4969 EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
4970};
4971
4972struct FixedPointKindField : public MDUnsignedField {
4973 FixedPointKindField()
4974 : MDUnsignedField(0, DIFixedPointType::LastFixedPointKind) {}
4975};
4976
4977struct NameTableKindField : public MDUnsignedField {
4978 NameTableKindField()
4979 : MDUnsignedField(
4980 0, (unsigned)
4981 DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
4982};
4983
4984struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
4985 DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
4986};
4987
4988struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> {
4989 DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {}
4990};
4991
4992struct MDAPSIntField : public MDFieldImpl<APSInt> {
4993 MDAPSIntField() : ImplTy(APSInt()) {}
4994};
4995
4996struct MDSignedField : public MDFieldImpl<int64_t> {
4997 int64_t Min = INT64_MIN;
4998 int64_t Max = INT64_MAX;
4999
5000 MDSignedField(int64_t Default = 0)
5001 : ImplTy(Default) {}
5002 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
5003 : ImplTy(Default), Min(Min), Max(Max) {}
5004};
5005
5006struct MDBoolField : public MDFieldImpl<bool> {
5007 MDBoolField(bool Default = false) : ImplTy(Default) {}
5008};
5009
5010struct MDField : public MDFieldImpl<Metadata *> {
5011 bool AllowNull;
5012
5013 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
5014};
5015
5016struct MDStringField : public MDFieldImpl<MDString *> {
5017 enum class EmptyIs {
5018 Null, //< Allow empty input string, map to nullptr
5019 Empty, //< Allow empty input string, map to an empty MDString
5020 Error, //< Disallow empty string, map to an error
5021 } EmptyIs;
5022 MDStringField(enum EmptyIs EmptyIs = EmptyIs::Null)
5023 : ImplTy(nullptr), EmptyIs(EmptyIs) {}
5024};
5025
5026struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
5027 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
5028};
5029
5030struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
5031 ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
5032};
5033
5034struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
5035 MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
5036 : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
5037
5038 MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
5039 bool AllowNull = true)
5040 : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
5041
5042 bool isMDSignedField() const { return WhatIs == IsTypeA; }
5043 bool isMDField() const { return WhatIs == IsTypeB; }
5044 int64_t getMDSignedValue() const {
5045 assert(isMDSignedField() && "Wrong field type");
5046 return A.Val;
5047 }
5048 Metadata *getMDFieldValue() const {
5049 assert(isMDField() && "Wrong field type");
5050 return B.Val;
5051 }
5052};
5053
5054struct MDUnsignedOrMDField : MDEitherFieldImpl<MDUnsignedField, MDField> {
5055 MDUnsignedOrMDField(uint64_t Default = 0, bool AllowNull = true)
5056 : ImplTy(MDUnsignedField(Default), MDField(AllowNull)) {}
5057
5058 MDUnsignedOrMDField(uint64_t Default, uint64_t Max, bool AllowNull = true)
5059 : ImplTy(MDUnsignedField(Default, Max), MDField(AllowNull)) {}
5060
5061 bool isMDUnsignedField() const { return WhatIs == IsTypeA; }
5062 bool isMDField() const { return WhatIs == IsTypeB; }
5063 uint64_t getMDUnsignedValue() const {
5064 assert(isMDUnsignedField() && "Wrong field type");
5065 return A.Val;
5066 }
5067 Metadata *getMDFieldValue() const {
5068 assert(isMDField() && "Wrong field type");
5069 return B.Val;
5070 }
5071
5072 Metadata *getValueAsMetadata(LLVMContext &Context) const {
5073 if (isMDUnsignedField())
5075 ConstantInt::get(Type::getInt64Ty(Context), getMDUnsignedValue()));
5076 if (isMDField())
5077 return getMDFieldValue();
5078 return nullptr;
5079 }
5080};
5081
5082} // end anonymous namespace
5083
5084namespace llvm {
5085
5086template <>
5087bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) {
5088 if (Lex.getKind() != lltok::APSInt)
5089 return tokError("expected integer");
5090
5091 Result.assign(Lex.getAPSIntVal());
5092 Lex.Lex();
5093 return false;
5094}
5095
5096template <>
5097bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5098 MDUnsignedField &Result) {
5099 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
5100 return tokError("expected unsigned integer");
5101
5102 auto &U = Lex.getAPSIntVal();
5103 if (U.ugt(Result.Max))
5104 return tokError("value for '" + Name + "' too large, limit is " +
5105 Twine(Result.Max));
5106 Result.assign(U.getZExtValue());
5107 assert(Result.Val <= Result.Max && "Expected value in range");
5108 Lex.Lex();
5109 return false;
5110}
5111
5112template <>
5113bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) {
5114 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5115}
5116template <>
5117bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
5118 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5119}
5120
5121template <>
5122bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
5123 if (Lex.getKind() == lltok::APSInt)
5124 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5125
5126 if (Lex.getKind() != lltok::DwarfTag)
5127 return tokError("expected DWARF tag");
5128
5129 unsigned Tag = dwarf::getTag(Lex.getStrVal());
5131 return tokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
5132 assert(Tag <= Result.Max && "Expected valid DWARF tag");
5133
5134 Result.assign(Tag);
5135 Lex.Lex();
5136 return false;
5137}
5138
5139template <>
5140bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5141 DwarfMacinfoTypeField &Result) {
5142 if (Lex.getKind() == lltok::APSInt)
5143 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5144
5145 if (Lex.getKind() != lltok::DwarfMacinfo)
5146 return tokError("expected DWARF macinfo type");
5147
5148 unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
5149 if (Macinfo == dwarf::DW_MACINFO_invalid)
5150 return tokError("invalid DWARF macinfo type" + Twine(" '") +
5151 Lex.getStrVal() + "'");
5152 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
5153
5154 Result.assign(Macinfo);
5155 Lex.Lex();
5156 return false;
5157}
5158
5159template <>
5160bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5161 DwarfVirtualityField &Result) {
5162 if (Lex.getKind() == lltok::APSInt)
5163 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5164
5165 if (Lex.getKind() != lltok::DwarfVirtuality)
5166 return tokError("expected DWARF virtuality code");
5167
5168 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
5169 if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
5170 return tokError("invalid DWARF virtuality code" + Twine(" '") +
5171 Lex.getStrVal() + "'");
5172 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
5173 Result.assign(Virtuality);
5174 Lex.Lex();
5175 return false;
5176}
5177
5178template <>
5179bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5180 DwarfEnumKindField &Result) {
5181 if (Lex.getKind() == lltok::APSInt)
5182 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5183
5184 if (Lex.getKind() != lltok::DwarfEnumKind)
5185 return tokError("expected DWARF enum kind code");
5186
5187 unsigned EnumKind = dwarf::getEnumKind(Lex.getStrVal());
5188 if (EnumKind == dwarf::DW_APPLE_ENUM_KIND_invalid)
5189 return tokError("invalid DWARF enum kind code" + Twine(" '") +
5190 Lex.getStrVal() + "'");
5191 assert(EnumKind <= Result.Max && "Expected valid DWARF enum kind code");
5192 Result.assign(EnumKind);
5193 Lex.Lex();
5194 return false;
5195}
5196
5197template <>
5198bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
5199 if (Lex.getKind() == lltok::APSInt)
5200 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5201
5202 if (Lex.getKind() != lltok::DwarfLang)
5203 return tokError("expected DWARF language");
5204
5205 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
5206 if (!Lang)
5207 return tokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
5208 "'");
5209 assert(Lang <= Result.Max && "Expected valid DWARF language");
5210 Result.assign(Lang);
5211 Lex.Lex();
5212 return false;
5213}
5214
5215template <>
5216bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5217 DwarfSourceLangNameField &Result) {
5218 if (Lex.getKind() == lltok::APSInt)
5219 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5220
5221 if (Lex.getKind() != lltok::DwarfSourceLangName)
5222 return tokError("expected DWARF source language name");
5223
5224 unsigned Lang = dwarf::getSourceLanguageName(Lex.getStrVal());
5225 if (!Lang)
5226 return tokError("invalid DWARF source language name" + Twine(" '") +
5227 Lex.getStrVal() + "'");
5228 assert(Lang <= Result.Max && "Expected valid DWARF source language name");
5229 Result.assign(Lang);
5230 Lex.Lex();
5231 return false;
5232}
5233
5234template <>
5235bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
5236 if (Lex.getKind() == lltok::APSInt)
5237 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5238
5239 if (Lex.getKind() != lltok::DwarfCC)
5240 return tokError("expected DWARF calling convention");
5241
5242 unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
5243 if (!CC)
5244 return tokError("invalid DWARF calling convention" + Twine(" '") +
5245 Lex.getStrVal() + "'");
5246 assert(CC <= Result.Max && "Expected valid DWARF calling convention");
5247 Result.assign(CC);
5248 Lex.Lex();
5249 return false;
5250}
5251
5252template <>
5253bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5254 EmissionKindField &Result) {
5255 if (Lex.getKind() == lltok::APSInt)
5256 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5257
5258 if (Lex.getKind() != lltok::EmissionKind)
5259 return tokError("expected emission kind");
5260
5261 auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
5262 if (!Kind)
5263 return tokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
5264 "'");
5265 assert(*Kind <= Result.Max && "Expected valid emission kind");
5266 Result.assign(*Kind);
5267 Lex.Lex();
5268 return false;
5269}
5270
5271template <>
5272bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5273 FixedPointKindField &Result) {
5274 if (Lex.getKind() == lltok::APSInt)
5275 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5276
5277 if (Lex.getKind() != lltok::FixedPointKind)
5278 return tokError("expected fixed-point kind");
5279
5280 auto Kind = DIFixedPointType::getFixedPointKind(Lex.getStrVal());
5281 if (!Kind)
5282 return tokError("invalid fixed-point kind" + Twine(" '") + Lex.getStrVal() +
5283 "'");
5284 assert(*Kind <= Result.Max && "Expected valid fixed-point kind");
5285 Result.assign(*Kind);
5286 Lex.Lex();
5287 return false;
5288}
5289
5290template <>
5291bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5292 NameTableKindField &Result) {
5293 if (Lex.getKind() == lltok::APSInt)
5294 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5295
5296 if (Lex.getKind() != lltok::NameTableKind)
5297 return tokError("expected nameTable kind");
5298
5299 auto Kind = DICompileUnit::getNameTableKind(Lex.getStrVal());
5300 if (!Kind)
5301 return tokError("invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
5302 "'");
5303 assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
5304 Result.assign((unsigned)*Kind);
5305 Lex.Lex();
5306 return false;
5307}
5308
5309template <>
5310bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5311 DwarfAttEncodingField &Result) {
5312 if (Lex.getKind() == lltok::APSInt)
5313 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
5314
5315 if (Lex.getKind() != lltok::DwarfAttEncoding)
5316 return tokError("expected DWARF type attribute encoding");
5317
5318 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
5319 if (!Encoding)
5320 return tokError("invalid DWARF type attribute encoding" + Twine(" '") +
5321 Lex.getStrVal() + "'");
5322 assert(Encoding <= Result.Max && "Expected valid DWARF language");
5323 Result.assign(Encoding);
5324 Lex.Lex();
5325 return false;
5326}
5327
5328/// DIFlagField
5329/// ::= uint32
5330/// ::= DIFlagVector
5331/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
5332template <>
5333bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
5334
5335 // parser for a single flag.
5336 auto parseFlag = [&](DINode::DIFlags &Val) {
5337 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
5338 uint32_t TempVal = static_cast<uint32_t>(Val);
5339 bool Res = parseUInt32(TempVal);
5340 Val = static_cast<DINode::DIFlags>(TempVal);
5341 return Res;
5342 }
5343
5344 if (Lex.getKind() != lltok::DIFlag)
5345 return tokError("expected debug info flag");
5346
5347 Val = DINode::getFlag(Lex.getStrVal());
5348 if (!Val)
5349 return tokError(Twine("invalid debug info flag '") + Lex.getStrVal() +
5350 "'");
5351 Lex.Lex();
5352 return false;
5353 };
5354
5355 // parse the flags and combine them together.
5356 DINode::DIFlags Combined = DINode::FlagZero;
5357 do {
5358 DINode::DIFlags Val;
5359 if (parseFlag(Val))
5360 return true;
5361 Combined |= Val;
5362 } while (EatIfPresent(lltok::bar));
5363
5364 Result.assign(Combined);
5365 return false;
5366}
5367
5368/// DISPFlagField
5369/// ::= uint32
5370/// ::= DISPFlagVector
5371/// ::= DISPFlagVector '|' DISPFlag* '|' uint32
5372template <>
5373bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) {
5374
5375 // parser for a single flag.
5376 auto parseFlag = [&](DISubprogram::DISPFlags &Val) {
5377 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
5378 uint32_t TempVal = static_cast<uint32_t>(Val);
5379 bool Res = parseUInt32(TempVal);
5380 Val = static_cast<DISubprogram::DISPFlags>(TempVal);
5381 return Res;
5382 }
5383
5384 if (Lex.getKind() != lltok::DISPFlag)
5385 return tokError("expected debug info flag");
5386
5387 Val = DISubprogram::getFlag(Lex.getStrVal());
5388 if (!Val)
5389 return tokError(Twine("invalid subprogram debug info flag '") +
5390 Lex.getStrVal() + "'");
5391 Lex.Lex();
5392 return false;
5393 };
5394
5395 // parse the flags and combine them together.
5396 DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero;
5397 do {
5399 if (parseFlag(Val))
5400 return true;
5401 Combined |= Val;
5402 } while (EatIfPresent(lltok::bar));
5403
5404 Result.assign(Combined);
5405 return false;
5406}
5407
5408template <>
5409bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) {
5410 if (Lex.getKind() != lltok::APSInt)
5411 return tokError("expected signed integer");
5412
5413 auto &S = Lex.getAPSIntVal();
5414 if (S < Result.Min)
5415 return tokError("value for '" + Name + "' too small, limit is " +
5416 Twine(Result.Min));
5417 if (S > Result.Max)
5418 return tokError("value for '" + Name + "' too large, limit is " +
5419 Twine(Result.Max));
5420 Result.assign(S.getExtValue());
5421 assert(Result.Val >= Result.Min && "Expected value in range");
5422 assert(Result.Val <= Result.Max && "Expected value in range");
5423 Lex.Lex();
5424 return false;
5425}
5426
5427template <>
5428bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
5429 switch (Lex.getKind()) {
5430 default:
5431 return tokError("expected 'true' or 'false'");
5432 case lltok::kw_true:
5433 Result.assign(true);
5434 break;
5435 case lltok::kw_false:
5436 Result.assign(false);
5437 break;
5438 }
5439 Lex.Lex();
5440 return false;
5441}
5442
5443template <>
5444bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) {
5445 if (Lex.getKind() == lltok::kw_null) {
5446 if (!Result.AllowNull)
5447 return tokError("'" + Name + "' cannot be null");
5448 Lex.Lex();
5449 Result.assign(nullptr);
5450 return false;
5451 }
5452
5453 Metadata *MD;
5454 if (parseMetadata(MD, nullptr))
5455 return true;
5456
5457 Result.assign(MD);
5458 return false;
5459}
5460
5461template <>
5462bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5463 MDSignedOrMDField &Result) {
5464 // Try to parse a signed int.
5465 if (Lex.getKind() == lltok::APSInt) {
5466 MDSignedField Res = Result.A;
5467 if (!parseMDField(Loc, Name, Res)) {
5468 Result.assign(Res);
5469 return false;
5470 }
5471 return true;
5472 }
5473
5474 // Otherwise, try to parse as an MDField.
5475 MDField Res = Result.B;
5476 if (!parseMDField(Loc, Name, Res)) {
5477 Result.assign(Res);
5478 return false;
5479 }
5480
5481 return true;
5482}
5483
5484template <>
5485bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5486 MDUnsignedOrMDField &Result) {
5487 // Try to parse an unsigned int.
5488 if (Lex.getKind() == lltok::APSInt) {
5489 MDUnsignedField Res = Result.A;
5490 if (!parseMDField(Loc, Name, Res)) {
5491 Result.assign(Res);
5492 return false;
5493 }
5494 return true;
5495 }
5496
5497 // Otherwise, try to parse as an MDField.
5498 MDField Res = Result.B;
5499 if (!parseMDField(Loc, Name, Res)) {
5500 Result.assign(Res);
5501 return false;
5502 }
5503
5504 return true;
5505}
5506
5507template <>
5508bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
5509 LocTy ValueLoc = Lex.getLoc();
5510 std::string S;
5511 if (parseStringConstant(S))
5512 return true;
5513
5514 if (S.empty()) {
5515 switch (Result.EmptyIs) {
5516 case MDStringField::EmptyIs::Null:
5517 Result.assign(nullptr);
5518 return false;
5519 case MDStringField::EmptyIs::Empty:
5520 break;
5521 case MDStringField::EmptyIs::Error:
5522 return error(ValueLoc, "'" + Name + "' cannot be empty");
5523 }
5524 }
5525
5526 Result.assign(MDString::get(Context, S));
5527 return false;
5528}
5529
5530template <>
5531bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
5533 if (parseMDNodeVector(MDs))
5534 return true;
5535
5536 Result.assign(std::move(MDs));
5537 return false;
5538}
5539
5540template <>
5541bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5542 ChecksumKindField &Result) {
5543 std::optional<DIFile::ChecksumKind> CSKind =
5544 DIFile::getChecksumKind(Lex.getStrVal());
5545
5546 if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
5547 return tokError("invalid checksum kind" + Twine(" '") + Lex.getStrVal() +
5548 "'");
5549
5550 Result.assign(*CSKind);
5551 Lex.Lex();
5552 return false;
5553}
5554
5555} // end namespace llvm
5556
5557template <class ParserTy>
5558bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) {
5559 do {
5560 if (Lex.getKind() != lltok::LabelStr)
5561 return tokError("expected field label here");
5562
5563 if (ParseField())
5564 return true;
5565 } while (EatIfPresent(lltok::comma));
5566
5567 return false;
5568}
5569
5570template <class ParserTy>
5571bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) {
5572 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5573 Lex.Lex();
5574
5575 if (parseToken(lltok::lparen, "expected '(' here"))
5576 return true;
5577 if (Lex.getKind() != lltok::rparen)
5578 if (parseMDFieldsImplBody(ParseField))
5579 return true;
5580
5581 ClosingLoc = Lex.getLoc();
5582 return parseToken(lltok::rparen, "expected ')' here");
5583}
5584
5585template <class FieldTy>
5586bool LLParser::parseMDField(StringRef Name, FieldTy &Result) {
5587 if (Result.Seen)
5588 return tokError("field '" + Name + "' cannot be specified more than once");
5589
5590 LocTy Loc = Lex.getLoc();
5591 Lex.Lex();
5592 return parseMDField(Loc, Name, Result);
5593}
5594
5595bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
5596 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5597
5598#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
5599 if (Lex.getStrVal() == #CLASS) \
5600 return parse##CLASS(N, IsDistinct);
5601#include "llvm/IR/Metadata.def"
5602
5603 return tokError("expected metadata type");
5604}
5605
5606#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
5607#define NOP_FIELD(NAME, TYPE, INIT)
5608#define REQUIRE_FIELD(NAME, TYPE, INIT) \
5609 if (!NAME.Seen) \
5610 return error(ClosingLoc, "missing required field '" #NAME "'");
5611#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
5612 if (Lex.getStrVal() == #NAME) \
5613 return parseMDField(#NAME, NAME);
5614#define PARSE_MD_FIELDS() \
5615 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
5616 do { \
5617 LocTy ClosingLoc; \
5618 if (parseMDFieldsImpl( \
5619 [&]() -> bool { \
5620 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
5621 return tokError(Twine("invalid field '") + Lex.getStrVal() + \
5622 "'"); \
5623 }, \
5624 ClosingLoc)) \
5625 return true; \
5626 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
5627 } while (false)
5628#define GET_OR_DISTINCT(CLASS, ARGS) \
5629 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
5630
5631/// parseDILocationFields:
5632/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
5633/// isImplicitCode: true, atomGroup: 1, atomRank: 1)
5634bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) {
5635#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5636 OPTIONAL(line, LineField, ); \
5637 OPTIONAL(column, ColumnField, ); \
5638 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5639 OPTIONAL(inlinedAt, MDField, ); \
5640 OPTIONAL(isImplicitCode, MDBoolField, (false)); \
5641 OPTIONAL(atomGroup, MDUnsignedField, (0, UINT64_MAX)); \
5642 OPTIONAL(atomRank, MDUnsignedField, (0, UINT8_MAX));
5644#undef VISIT_MD_FIELDS
5645
5646 Result = GET_OR_DISTINCT(
5647 DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val,
5648 isImplicitCode.Val, atomGroup.Val, atomRank.Val));
5649 return false;
5650}
5651
5652/// parseDIAssignID:
5653/// ::= distinct !DIAssignID()
5654bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) {
5655 if (!IsDistinct)
5656 return tokError("missing 'distinct', required for !DIAssignID()");
5657
5658 Lex.Lex();
5659
5660 // Now eat the parens.
5661 if (parseToken(lltok::lparen, "expected '(' here"))
5662 return true;
5663 if (parseToken(lltok::rparen, "expected ')' here"))
5664 return true;
5665
5667 return false;
5668}
5669
5670/// parseGenericDINode:
5671/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
5672bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) {
5673#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5674 REQUIRED(tag, DwarfTagField, ); \
5675 OPTIONAL(header, MDStringField, ); \
5676 OPTIONAL(operands, MDFieldList, );
5678#undef VISIT_MD_FIELDS
5679
5680 Result = GET_OR_DISTINCT(GenericDINode,
5681 (Context, tag.Val, header.Val, operands.Val));
5682 return false;
5683}
5684
5685/// parseDISubrangeType:
5686/// ::= !DISubrangeType(name: "whatever", file: !0,
5687/// line: 7, scope: !1, baseType: !2, size: 32,
5688/// align: 32, flags: 0, lowerBound: !3
5689/// upperBound: !4, stride: !5, bias: !6)
5690bool LLParser::parseDISubrangeType(MDNode *&Result, bool IsDistinct) {
5691#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5692 OPTIONAL(name, MDStringField, ); \
5693 OPTIONAL(file, MDField, ); \
5694 OPTIONAL(line, LineField, ); \
5695 OPTIONAL(scope, MDField, ); \
5696 OPTIONAL(baseType, MDField, ); \
5697 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5698 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5699 OPTIONAL(flags, DIFlagField, ); \
5700 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5701 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5702 OPTIONAL(stride, MDSignedOrMDField, ); \
5703 OPTIONAL(bias, MDSignedOrMDField, );
5705#undef VISIT_MD_FIELDS
5706
5707 auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
5708 if (Bound.isMDSignedField())
5710 Type::getInt64Ty(Context), Bound.getMDSignedValue()));
5711 if (Bound.isMDField())
5712 return Bound.getMDFieldValue();
5713 return nullptr;
5714 };
5715
5716 Metadata *LowerBound = convToMetadata(lowerBound);
5717 Metadata *UpperBound = convToMetadata(upperBound);
5718 Metadata *Stride = convToMetadata(stride);
5719 Metadata *Bias = convToMetadata(bias);
5720
5722 DISubrangeType, (Context, name.Val, file.Val, line.Val, scope.Val,
5723 size.getValueAsMetadata(Context), align.Val, flags.Val,
5724 baseType.Val, LowerBound, UpperBound, Stride, Bias));
5725
5726 return false;
5727}
5728
5729/// parseDISubrange:
5730/// ::= !DISubrange(count: 30, lowerBound: 2)
5731/// ::= !DISubrange(count: !node, lowerBound: 2)
5732/// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3)
5733bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) {
5734#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5735 OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
5736 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5737 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5738 OPTIONAL(stride, MDSignedOrMDField, );
5740#undef VISIT_MD_FIELDS
5741
5742 Metadata *Count = nullptr;
5743 Metadata *LowerBound = nullptr;
5744 Metadata *UpperBound = nullptr;
5745 Metadata *Stride = nullptr;
5746
5747 auto convToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * {
5748 if (Bound.isMDSignedField())
5750 Type::getInt64Ty(Context), Bound.getMDSignedValue()));
5751 if (Bound.isMDField())
5752 return Bound.getMDFieldValue();
5753 return nullptr;
5754 };
5755
5756 Count = convToMetadata(count);
5757 LowerBound = convToMetadata(lowerBound);
5758 UpperBound = convToMetadata(upperBound);
5759 Stride = convToMetadata(stride);
5760
5761 Result = GET_OR_DISTINCT(DISubrange,
5762 (Context, Count, LowerBound, UpperBound, Stride));
5763
5764 return false;
5765}
5766
5767/// parseDIGenericSubrange:
5768/// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride:
5769/// !node3)
5770bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) {
5771#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5772 OPTIONAL(count, MDSignedOrMDField, ); \
5773 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5774 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5775 OPTIONAL(stride, MDSignedOrMDField, );
5777#undef VISIT_MD_FIELDS
5778
5779 auto ConvToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * {
5780 if (Bound.isMDSignedField())
5781 return DIExpression::get(
5782 Context, {dwarf::DW_OP_consts,
5783 static_cast<uint64_t>(Bound.getMDSignedValue())});
5784 if (Bound.isMDField())
5785 return Bound.getMDFieldValue();
5786 return nullptr;
5787 };
5788
5789 Metadata *Count = ConvToMetadata(count);
5790 Metadata *LowerBound = ConvToMetadata(lowerBound);
5791 Metadata *UpperBound = ConvToMetadata(upperBound);
5792 Metadata *Stride = ConvToMetadata(stride);
5793
5794 Result = GET_OR_DISTINCT(DIGenericSubrange,
5795 (Context, Count, LowerBound, UpperBound, Stride));
5796
5797 return false;
5798}
5799
5800/// parseDIEnumerator:
5801/// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
5802bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) {
5803#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5804 REQUIRED(name, MDStringField, ); \
5805 REQUIRED(value, MDAPSIntField, ); \
5806 OPTIONAL(isUnsigned, MDBoolField, (false));
5808#undef VISIT_MD_FIELDS
5809
5810 if (isUnsigned.Val && value.Val.isNegative())
5811 return tokError("unsigned enumerator with negative value");
5812
5813 APSInt Value(value.Val);
5814 // Add a leading zero so that unsigned values with the msb set are not
5815 // mistaken for negative values when used for signed enumerators.
5816 if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet())
5817 Value = Value.zext(Value.getBitWidth() + 1);
5818
5819 Result =
5820 GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
5821
5822 return false;
5823}
5824
5825/// parseDIBasicType:
5826/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
5827/// encoding: DW_ATE_encoding, flags: 0)
5828bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) {
5829#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5830 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
5831 OPTIONAL(name, MDStringField, ); \
5832 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5833 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5834 OPTIONAL(dataSize, MDUnsignedField, (0, UINT32_MAX)); \
5835 OPTIONAL(encoding, DwarfAttEncodingField, ); \
5836 OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
5837 OPTIONAL(flags, DIFlagField, );
5839#undef VISIT_MD_FIELDS
5840
5842 DIBasicType,
5843 (Context, tag.Val, name.Val, size.getValueAsMetadata(Context), align.Val,
5844 encoding.Val, num_extra_inhabitants.Val, dataSize.Val, flags.Val));
5845 return false;
5846}
5847
5848/// parseDIFixedPointType:
5849/// ::= !DIFixedPointType(tag: DW_TAG_base_type, name: "xyz", size: 32,
5850/// align: 32, encoding: DW_ATE_signed_fixed,
5851/// flags: 0, kind: Rational, factor: 3, numerator: 1,
5852/// denominator: 8)
5853bool LLParser::parseDIFixedPointType(MDNode *&Result, bool IsDistinct) {
5854#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5855 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
5856 OPTIONAL(name, MDStringField, ); \
5857 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5858 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5859 OPTIONAL(encoding, DwarfAttEncodingField, ); \
5860 OPTIONAL(flags, DIFlagField, ); \
5861 OPTIONAL(kind, FixedPointKindField, ); \
5862 OPTIONAL(factor, MDSignedField, ); \
5863 OPTIONAL(numerator, MDAPSIntField, ); \
5864 OPTIONAL(denominator, MDAPSIntField, );
5866#undef VISIT_MD_FIELDS
5867
5868 Result = GET_OR_DISTINCT(DIFixedPointType,
5869 (Context, tag.Val, name.Val,
5870 size.getValueAsMetadata(Context), align.Val,
5871 encoding.Val, flags.Val, kind.Val, factor.Val,
5872 numerator.Val, denominator.Val));
5873 return false;
5874}
5875
5876/// parseDIStringType:
5877/// ::= !DIStringType(name: "character(4)", size: 32, align: 32)
5878bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) {
5879#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5880 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \
5881 OPTIONAL(name, MDStringField, ); \
5882 OPTIONAL(stringLength, MDField, ); \
5883 OPTIONAL(stringLengthExpression, MDField, ); \
5884 OPTIONAL(stringLocationExpression, MDField, ); \
5885 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5886 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5887 OPTIONAL(encoding, DwarfAttEncodingField, );
5889#undef VISIT_MD_FIELDS
5890
5892 DIStringType,
5893 (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val,
5894 stringLocationExpression.Val, size.getValueAsMetadata(Context),
5895 align.Val, encoding.Val));
5896 return false;
5897}
5898
5899/// parseDIDerivedType:
5900/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
5901/// line: 7, scope: !1, baseType: !2, size: 32,
5902/// align: 32, offset: 0, flags: 0, extraData: !3,
5903/// dwarfAddressSpace: 3, ptrAuthKey: 1,
5904/// ptrAuthIsAddressDiscriminated: true,
5905/// ptrAuthExtraDiscriminator: 0x1234,
5906/// ptrAuthIsaPointer: 1, ptrAuthAuthenticatesNullValues:1
5907/// )
5908bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) {
5909#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5910 REQUIRED(tag, DwarfTagField, ); \
5911 OPTIONAL(name, MDStringField, ); \
5912 OPTIONAL(file, MDField, ); \
5913 OPTIONAL(line, LineField, ); \
5914 OPTIONAL(scope, MDField, ); \
5915 REQUIRED(baseType, MDField, ); \
5916 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5917 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5918 OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5919 OPTIONAL(flags, DIFlagField, ); \
5920 OPTIONAL(extraData, MDField, ); \
5921 OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \
5922 OPTIONAL(annotations, MDField, ); \
5923 OPTIONAL(ptrAuthKey, MDUnsignedField, (0, 7)); \
5924 OPTIONAL(ptrAuthIsAddressDiscriminated, MDBoolField, ); \
5925 OPTIONAL(ptrAuthExtraDiscriminator, MDUnsignedField, (0, 0xffff)); \
5926 OPTIONAL(ptrAuthIsaPointer, MDBoolField, ); \
5927 OPTIONAL(ptrAuthAuthenticatesNullValues, MDBoolField, );
5929#undef VISIT_MD_FIELDS
5930
5931 std::optional<unsigned> DWARFAddressSpace;
5932 if (dwarfAddressSpace.Val != UINT32_MAX)
5933 DWARFAddressSpace = dwarfAddressSpace.Val;
5934 std::optional<DIDerivedType::PtrAuthData> PtrAuthData;
5935 if (ptrAuthKey.Val)
5936 PtrAuthData.emplace(
5937 (unsigned)ptrAuthKey.Val, ptrAuthIsAddressDiscriminated.Val,
5938 (unsigned)ptrAuthExtraDiscriminator.Val, ptrAuthIsaPointer.Val,
5939 ptrAuthAuthenticatesNullValues.Val);
5940
5942 DIDerivedType, (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val,
5943 baseType.Val, size.getValueAsMetadata(Context), align.Val,
5944 offset.getValueAsMetadata(Context), DWARFAddressSpace,
5945 PtrAuthData, flags.Val, extraData.Val, annotations.Val));
5946 return false;
5947}
5948
5949bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
5950#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5951 REQUIRED(tag, DwarfTagField, ); \
5952 OPTIONAL(name, MDStringField, ); \
5953 OPTIONAL(file, MDField, ); \
5954 OPTIONAL(line, LineField, ); \
5955 OPTIONAL(scope, MDField, ); \
5956 OPTIONAL(baseType, MDField, ); \
5957 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5958 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5959 OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5960 OPTIONAL(flags, DIFlagField, ); \
5961 OPTIONAL(elements, MDField, ); \
5962 OPTIONAL(runtimeLang, DwarfLangField, ); \
5963 OPTIONAL(enumKind, DwarfEnumKindField, ); \
5964 OPTIONAL(vtableHolder, MDField, ); \
5965 OPTIONAL(templateParams, MDField, ); \
5966 OPTIONAL(identifier, MDStringField, ); \
5967 OPTIONAL(discriminator, MDField, ); \
5968 OPTIONAL(dataLocation, MDField, ); \
5969 OPTIONAL(associated, MDField, ); \
5970 OPTIONAL(allocated, MDField, ); \
5971 OPTIONAL(rank, MDSignedOrMDField, ); \
5972 OPTIONAL(annotations, MDField, ); \
5973 OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
5974 OPTIONAL(specification, MDField, ); \
5975 OPTIONAL(bitStride, MDField, );
5977#undef VISIT_MD_FIELDS
5978
5979 Metadata *Rank = nullptr;
5980 if (rank.isMDSignedField())
5982 Type::getInt64Ty(Context), rank.getMDSignedValue()));
5983 else if (rank.isMDField())
5984 Rank = rank.getMDFieldValue();
5985
5986 std::optional<unsigned> EnumKind;
5987 if (enumKind.Val != dwarf::DW_APPLE_ENUM_KIND_invalid)
5988 EnumKind = enumKind.Val;
5989
5990 // If this has an identifier try to build an ODR type.
5991 if (identifier.Val)
5992 if (auto *CT = DICompositeType::buildODRType(
5993 Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
5994 scope.Val, baseType.Val, size.getValueAsMetadata(Context),
5995 align.Val, offset.getValueAsMetadata(Context), specification.Val,
5996 num_extra_inhabitants.Val, flags.Val, elements.Val, runtimeLang.Val,
5997 EnumKind, vtableHolder.Val, templateParams.Val, discriminator.Val,
5998 dataLocation.Val, associated.Val, allocated.Val, Rank,
5999 annotations.Val, bitStride.Val)) {
6000 Result = CT;
6001 return false;
6002 }
6003
6004 // Create a new node, and save it in the context if it belongs in the type
6005 // map.
6007 DICompositeType,
6008 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
6009 size.getValueAsMetadata(Context), align.Val,
6010 offset.getValueAsMetadata(Context), flags.Val, elements.Val,
6011 runtimeLang.Val, EnumKind, vtableHolder.Val, templateParams.Val,
6012 identifier.Val, discriminator.Val, dataLocation.Val, associated.Val,
6013 allocated.Val, Rank, annotations.Val, specification.Val,
6014 num_extra_inhabitants.Val, bitStride.Val));
6015 return false;
6016}
6017
6018bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) {
6019#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6020 OPTIONAL(flags, DIFlagField, ); \
6021 OPTIONAL(cc, DwarfCCField, ); \
6022 REQUIRED(types, MDField, );
6024#undef VISIT_MD_FIELDS
6025
6026 Result = GET_OR_DISTINCT(DISubroutineType,
6027 (Context, flags.Val, cc.Val, types.Val));
6028 return false;
6029}
6030
6031/// parseDIFileType:
6032/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
6033/// checksumkind: CSK_MD5,
6034/// checksum: "000102030405060708090a0b0c0d0e0f",
6035/// source: "source file contents")
6036bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) {
6037 // The default constructed value for checksumkind is required, but will never
6038 // be used, as the parser checks if the field was actually Seen before using
6039 // the Val.
6040#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6041 REQUIRED(filename, MDStringField, ); \
6042 REQUIRED(directory, MDStringField, ); \
6043 OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
6044 OPTIONAL(checksum, MDStringField, ); \
6045 OPTIONAL(source, MDStringField, (MDStringField::EmptyIs::Empty));
6047#undef VISIT_MD_FIELDS
6048
6049 std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
6050 if (checksumkind.Seen && checksum.Seen)
6051 OptChecksum.emplace(checksumkind.Val, checksum.Val);
6052 else if (checksumkind.Seen || checksum.Seen)
6053 return tokError("'checksumkind' and 'checksum' must be provided together");
6054
6055 MDString *Source = nullptr;
6056 if (source.Seen)
6057 Source = source.Val;
6059 DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source));
6060 return false;
6061}
6062
6063/// parseDICompileUnit:
6064/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
6065/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
6066/// splitDebugFilename: "abc.debug",
6067/// emissionKind: FullDebug, enums: !1, retainedTypes: !2,
6068/// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd,
6069/// sysroot: "/", sdk: "MacOSX.sdk")
6070bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
6071 if (!IsDistinct)
6072 return tokError("missing 'distinct', required for !DICompileUnit");
6073
6074 LocTy Loc = Lex.getLoc();
6075
6076#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6077 REQUIRED(file, MDField, (/* AllowNull */ false)); \
6078 OPTIONAL(language, DwarfLangField, ); \
6079 OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \
6080 OPTIONAL(sourceLanguageVersion, MDUnsignedField, (0, UINT32_MAX)); \
6081 OPTIONAL(producer, MDStringField, ); \
6082 OPTIONAL(isOptimized, MDBoolField, ); \
6083 OPTIONAL(flags, MDStringField, ); \
6084 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
6085 OPTIONAL(splitDebugFilename, MDStringField, ); \
6086 OPTIONAL(emissionKind, EmissionKindField, ); \
6087 OPTIONAL(enums, MDField, ); \
6088 OPTIONAL(retainedTypes, MDField, ); \
6089 OPTIONAL(globals, MDField, ); \
6090 OPTIONAL(imports, MDField, ); \
6091 OPTIONAL(macros, MDField, ); \
6092 OPTIONAL(dwoId, MDUnsignedField, ); \
6093 OPTIONAL(splitDebugInlining, MDBoolField, = true); \
6094 OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \
6095 OPTIONAL(nameTableKind, NameTableKindField, ); \
6096 OPTIONAL(rangesBaseAddress, MDBoolField, = false); \
6097 OPTIONAL(sysroot, MDStringField, ); \
6098 OPTIONAL(sdk, MDStringField, );
6100#undef VISIT_MD_FIELDS
6101
6102 if (!language.Seen && !sourceLanguageName.Seen)
6103 return error(Loc, "missing one of 'language' or 'sourceLanguageName', "
6104 "required for !DICompileUnit");
6105
6106 if (language.Seen && sourceLanguageName.Seen)
6107 return error(Loc, "can only specify one of 'language' and "
6108 "'sourceLanguageName' on !DICompileUnit");
6109
6110 if (sourceLanguageVersion.Seen && !sourceLanguageName.Seen)
6111 return error(Loc, "'sourceLanguageVersion' requires an associated "
6112 "'sourceLanguageName' on !DICompileUnit");
6113
6115 Context,
6116 language.Seen ? DISourceLanguageName(language.Val)
6117 : DISourceLanguageName(sourceLanguageName.Val,
6118 sourceLanguageVersion.Val),
6119 file.Val, producer.Val, isOptimized.Val, flags.Val, runtimeVersion.Val,
6120 splitDebugFilename.Val, emissionKind.Val, enums.Val, retainedTypes.Val,
6121 globals.Val, imports.Val, macros.Val, dwoId.Val, splitDebugInlining.Val,
6122 debugInfoForProfiling.Val, nameTableKind.Val, rangesBaseAddress.Val,
6123 sysroot.Val, sdk.Val);
6124 return false;
6125}
6126
6127/// parseDISubprogram:
6128/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
6129/// file: !1, line: 7, type: !2, isLocal: false,
6130/// isDefinition: true, scopeLine: 8, containingType: !3,
6131/// virtuality: DW_VIRTUALTIY_pure_virtual,
6132/// virtualIndex: 10, thisAdjustment: 4, flags: 11,
6133/// spFlags: 10, isOptimized: false, templateParams: !4,
6134/// declaration: !5, retainedNodes: !6, thrownTypes: !7,
6135/// annotations: !8)
6136bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
6137 auto Loc = Lex.getLoc();
6138#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6139 OPTIONAL(scope, MDField, ); \
6140 OPTIONAL(name, MDStringField, ); \
6141 OPTIONAL(linkageName, MDStringField, ); \
6142 OPTIONAL(file, MDField, ); \
6143 OPTIONAL(line, LineField, ); \
6144 OPTIONAL(type, MDField, ); \
6145 OPTIONAL(isLocal, MDBoolField, ); \
6146 OPTIONAL(isDefinition, MDBoolField, (true)); \
6147 OPTIONAL(scopeLine, LineField, ); \
6148 OPTIONAL(containingType, MDField, ); \
6149 OPTIONAL(virtuality, DwarfVirtualityField, ); \
6150 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
6151 OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \
6152 OPTIONAL(flags, DIFlagField, ); \
6153 OPTIONAL(spFlags, DISPFlagField, ); \
6154 OPTIONAL(isOptimized, MDBoolField, ); \
6155 OPTIONAL(unit, MDField, ); \
6156 OPTIONAL(templateParams, MDField, ); \
6157 OPTIONAL(declaration, MDField, ); \
6158 OPTIONAL(retainedNodes, MDField, ); \
6159 OPTIONAL(thrownTypes, MDField, ); \
6160 OPTIONAL(annotations, MDField, ); \
6161 OPTIONAL(targetFuncName, MDStringField, ); \
6162 OPTIONAL(keyInstructions, MDBoolField, );
6164#undef VISIT_MD_FIELDS
6165
6166 // An explicit spFlags field takes precedence over individual fields in
6167 // older IR versions.
6168 DISubprogram::DISPFlags SPFlags =
6169 spFlags.Seen ? spFlags.Val
6170 : DISubprogram::toSPFlags(isLocal.Val, isDefinition.Val,
6171 isOptimized.Val, virtuality.Val);
6172 if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct)
6173 return error(
6174 Loc,
6175 "missing 'distinct', required for !DISubprogram that is a Definition");
6177 DISubprogram,
6178 (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
6179 type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
6180 thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
6181 declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val,
6182 targetFuncName.Val, keyInstructions.Val));
6183
6184 if (IsDistinct)
6185 NewDistinctSPs.push_back(cast<DISubprogram>(Result));
6186
6187 return false;
6188}
6189
6190/// parseDILexicalBlock:
6191/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
6192bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
6193#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6194 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6195 OPTIONAL(file, MDField, ); \
6196 OPTIONAL(line, LineField, ); \
6197 OPTIONAL(column, ColumnField, );
6199#undef VISIT_MD_FIELDS
6200
6202 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
6203 return false;
6204}
6205
6206/// parseDILexicalBlockFile:
6207/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
6208bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
6209#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6210 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6211 OPTIONAL(file, MDField, ); \
6212 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
6214#undef VISIT_MD_FIELDS
6215
6216 Result = GET_OR_DISTINCT(DILexicalBlockFile,
6217 (Context, scope.Val, file.Val, discriminator.Val));
6218 return false;
6219}
6220
6221/// parseDICommonBlock:
6222/// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9)
6223bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) {
6224#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6225 REQUIRED(scope, MDField, ); \
6226 OPTIONAL(declaration, MDField, ); \
6227 OPTIONAL(name, MDStringField, ); \
6228 OPTIONAL(file, MDField, ); \
6229 OPTIONAL(line, LineField, );
6231#undef VISIT_MD_FIELDS
6232
6233 Result = GET_OR_DISTINCT(DICommonBlock,
6234 (Context, scope.Val, declaration.Val, name.Val,
6235 file.Val, line.Val));
6236 return false;
6237}
6238
6239/// parseDINamespace:
6240/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
6241bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) {
6242#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6243 REQUIRED(scope, MDField, ); \
6244 OPTIONAL(name, MDStringField, ); \
6245 OPTIONAL(exportSymbols, MDBoolField, );
6247#undef VISIT_MD_FIELDS
6248
6249 Result = GET_OR_DISTINCT(DINamespace,
6250 (Context, scope.Val, name.Val, exportSymbols.Val));
6251 return false;
6252}
6253
6254/// parseDIMacro:
6255/// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value:
6256/// "SomeValue")
6257bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) {
6258#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6259 REQUIRED(type, DwarfMacinfoTypeField, ); \
6260 OPTIONAL(line, LineField, ); \
6261 REQUIRED(name, MDStringField, ); \
6262 OPTIONAL(value, MDStringField, );
6264#undef VISIT_MD_FIELDS
6265
6266 Result = GET_OR_DISTINCT(DIMacro,
6267 (Context, type.Val, line.Val, name.Val, value.Val));
6268 return false;
6269}
6270
6271/// parseDIMacroFile:
6272/// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
6273bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) {
6274#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6275 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
6276 OPTIONAL(line, LineField, ); \
6277 REQUIRED(file, MDField, ); \
6278 OPTIONAL(nodes, MDField, );
6280#undef VISIT_MD_FIELDS
6281
6282 Result = GET_OR_DISTINCT(DIMacroFile,
6283 (Context, type.Val, line.Val, file.Val, nodes.Val));
6284 return false;
6285}
6286
6287/// parseDIModule:
6288/// ::= !DIModule(scope: !0, name: "SomeModule", configMacros:
6289/// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes",
6290/// file: !1, line: 4, isDecl: false)
6291bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) {
6292#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6293 REQUIRED(scope, MDField, ); \
6294 REQUIRED(name, MDStringField, ); \
6295 OPTIONAL(configMacros, MDStringField, ); \
6296 OPTIONAL(includePath, MDStringField, ); \
6297 OPTIONAL(apinotes, MDStringField, ); \
6298 OPTIONAL(file, MDField, ); \
6299 OPTIONAL(line, LineField, ); \
6300 OPTIONAL(isDecl, MDBoolField, );
6302#undef VISIT_MD_FIELDS
6303
6304 Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val,
6305 configMacros.Val, includePath.Val,
6306 apinotes.Val, line.Val, isDecl.Val));
6307 return false;
6308}
6309
6310/// parseDITemplateTypeParameter:
6311/// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false)
6312bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
6313#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6314 OPTIONAL(name, MDStringField, ); \
6315 REQUIRED(type, MDField, ); \
6316 OPTIONAL(defaulted, MDBoolField, );
6318#undef VISIT_MD_FIELDS
6319
6320 Result = GET_OR_DISTINCT(DITemplateTypeParameter,
6321 (Context, name.Val, type.Val, defaulted.Val));
6322 return false;
6323}
6324
6325/// parseDITemplateValueParameter:
6326/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
6327/// name: "V", type: !1, defaulted: false,
6328/// value: i32 7)
6329bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
6330#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6331 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
6332 OPTIONAL(name, MDStringField, ); \
6333 OPTIONAL(type, MDField, ); \
6334 OPTIONAL(defaulted, MDBoolField, ); \
6335 REQUIRED(value, MDField, );
6336
6338#undef VISIT_MD_FIELDS
6339
6341 DITemplateValueParameter,
6342 (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val));
6343 return false;
6344}
6345
6346/// parseDIGlobalVariable:
6347/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
6348/// file: !1, line: 7, type: !2, isLocal: false,
6349/// isDefinition: true, templateParams: !3,
6350/// declaration: !4, align: 8)
6351bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
6352#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6353 OPTIONAL(name, MDStringField, (MDStringField::EmptyIs::Error)); \
6354 OPTIONAL(scope, MDField, ); \
6355 OPTIONAL(linkageName, MDStringField, ); \
6356 OPTIONAL(file, MDField, ); \
6357 OPTIONAL(line, LineField, ); \
6358 OPTIONAL(type, MDField, ); \
6359 OPTIONAL(isLocal, MDBoolField, ); \
6360 OPTIONAL(isDefinition, MDBoolField, (true)); \
6361 OPTIONAL(templateParams, MDField, ); \
6362 OPTIONAL(declaration, MDField, ); \
6363 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6364 OPTIONAL(annotations, MDField, );
6366#undef VISIT_MD_FIELDS
6367
6368 Result =
6369 GET_OR_DISTINCT(DIGlobalVariable,
6370 (Context, scope.Val, name.Val, linkageName.Val, file.Val,
6371 line.Val, type.Val, isLocal.Val, isDefinition.Val,
6372 declaration.Val, templateParams.Val, align.Val,
6373 annotations.Val));
6374 return false;
6375}
6376
6377/// parseDILocalVariable:
6378/// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
6379/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
6380/// align: 8)
6381/// ::= !DILocalVariable(scope: !0, name: "foo",
6382/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
6383/// align: 8)
6384bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) {
6385#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6386 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6387 OPTIONAL(name, MDStringField, ); \
6388 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
6389 OPTIONAL(file, MDField, ); \
6390 OPTIONAL(line, LineField, ); \
6391 OPTIONAL(type, MDField, ); \
6392 OPTIONAL(flags, DIFlagField, ); \
6393 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6394 OPTIONAL(annotations, MDField, );
6396#undef VISIT_MD_FIELDS
6397
6398 Result = GET_OR_DISTINCT(DILocalVariable,
6399 (Context, scope.Val, name.Val, file.Val, line.Val,
6400 type.Val, arg.Val, flags.Val, align.Val,
6401 annotations.Val));
6402 return false;
6403}
6404
6405/// parseDILabel:
6406/// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7, column: 4)
6407bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) {
6408#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6409 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6410 REQUIRED(name, MDStringField, ); \
6411 REQUIRED(file, MDField, ); \
6412 REQUIRED(line, LineField, ); \
6413 OPTIONAL(column, ColumnField, ); \
6414 OPTIONAL(isArtificial, MDBoolField, ); \
6415 OPTIONAL(coroSuspendIdx, MDUnsignedField, );
6417#undef VISIT_MD_FIELDS
6418
6419 std::optional<unsigned> CoroSuspendIdx =
6420 coroSuspendIdx.Seen ? std::optional<unsigned>(coroSuspendIdx.Val)
6421 : std::nullopt;
6422
6423 Result = GET_OR_DISTINCT(DILabel,
6424 (Context, scope.Val, name.Val, file.Val, line.Val,
6425 column.Val, isArtificial.Val, CoroSuspendIdx));
6426 return false;
6427}
6428
6429/// parseDIExpressionBody:
6430/// ::= (0, 7, -1)
6431bool LLParser::parseDIExpressionBody(MDNode *&Result, bool IsDistinct) {
6432 if (parseToken(lltok::lparen, "expected '(' here"))
6433 return true;
6434
6435 SmallVector<uint64_t, 8> Elements;
6436 if (Lex.getKind() != lltok::rparen)
6437 do {
6438 if (Lex.getKind() == lltok::DwarfOp) {
6439 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
6440 Lex.Lex();
6441 Elements.push_back(Op);
6442 continue;
6443 }
6444 return tokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
6445 }
6446
6447 if (Lex.getKind() == lltok::DwarfAttEncoding) {
6448 if (unsigned Op = dwarf::getAttributeEncoding(Lex.getStrVal())) {
6449 Lex.Lex();
6450 Elements.push_back(Op);
6451 continue;
6452 }
6453 return tokError(Twine("invalid DWARF attribute encoding '") +
6454 Lex.getStrVal() + "'");
6455 }
6456
6457 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
6458 return tokError("expected unsigned integer");
6459
6460 auto &U = Lex.getAPSIntVal();
6461 if (U.ugt(UINT64_MAX))
6462 return tokError("element too large, limit is " + Twine(UINT64_MAX));
6463 Elements.push_back(U.getZExtValue());
6464 Lex.Lex();
6465 } while (EatIfPresent(lltok::comma));
6466
6467 if (parseToken(lltok::rparen, "expected ')' here"))
6468 return true;
6469
6470 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
6471 return false;
6472}
6473
6474/// parseDIExpression:
6475/// ::= !DIExpression(0, 7, -1)
6476bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
6477 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
6478 assert(Lex.getStrVal() == "DIExpression" && "Expected '!DIExpression'");
6479 Lex.Lex();
6480
6481 return parseDIExpressionBody(Result, IsDistinct);
6482}
6483
6484/// ParseDIArgList:
6485/// ::= !DIArgList(i32 7, i64 %0)
6486bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) {
6487 assert(PFS && "Expected valid function state");
6488 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
6489 Lex.Lex();
6490
6491 if (parseToken(lltok::lparen, "expected '(' here"))
6492 return true;
6493
6495 if (Lex.getKind() != lltok::rparen)
6496 do {
6497 Metadata *MD;
6498 if (parseValueAsMetadata(MD, "expected value-as-metadata operand", PFS))
6499 return true;
6500 Args.push_back(dyn_cast<ValueAsMetadata>(MD));
6501 } while (EatIfPresent(lltok::comma));
6502
6503 if (parseToken(lltok::rparen, "expected ')' here"))
6504 return true;
6505
6506 MD = DIArgList::get(Context, Args);
6507 return false;
6508}
6509
6510/// parseDIGlobalVariableExpression:
6511/// ::= !DIGlobalVariableExpression(var: !0, expr: !1)
6512bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result,
6513 bool IsDistinct) {
6514#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6515 REQUIRED(var, MDField, ); \
6516 REQUIRED(expr, MDField, );
6518#undef VISIT_MD_FIELDS
6519
6520 Result =
6521 GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
6522 return false;
6523}
6524
6525/// parseDIObjCProperty:
6526/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
6527/// getter: "getFoo", attributes: 7, type: !2)
6528bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
6529#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6530 OPTIONAL(name, MDStringField, ); \
6531 OPTIONAL(file, MDField, ); \
6532 OPTIONAL(line, LineField, ); \
6533 OPTIONAL(setter, MDStringField, ); \
6534 OPTIONAL(getter, MDStringField, ); \
6535 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
6536 OPTIONAL(type, MDField, );
6538#undef VISIT_MD_FIELDS
6539
6540 Result = GET_OR_DISTINCT(DIObjCProperty,
6541 (Context, name.Val, file.Val, line.Val, getter.Val,
6542 setter.Val, attributes.Val, type.Val));
6543 return false;
6544}
6545
6546/// parseDIImportedEntity:
6547/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
6548/// line: 7, name: "foo", elements: !2)
6549bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
6550#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6551 REQUIRED(tag, DwarfTagField, ); \
6552 REQUIRED(scope, MDField, ); \
6553 OPTIONAL(entity, MDField, ); \
6554 OPTIONAL(file, MDField, ); \
6555 OPTIONAL(line, LineField, ); \
6556 OPTIONAL(name, MDStringField, ); \
6557 OPTIONAL(elements, MDField, );
6559#undef VISIT_MD_FIELDS
6560
6561 Result = GET_OR_DISTINCT(DIImportedEntity,
6562 (Context, tag.Val, scope.Val, entity.Val, file.Val,
6563 line.Val, name.Val, elements.Val));
6564 return false;
6565}
6566
6567#undef PARSE_MD_FIELD
6568#undef NOP_FIELD
6569#undef REQUIRE_FIELD
6570#undef DECLARE_FIELD
6571
6572/// parseMetadataAsValue
6573/// ::= metadata i32 %local
6574/// ::= metadata i32 @global
6575/// ::= metadata i32 7
6576/// ::= metadata !0
6577/// ::= metadata !{...}
6578/// ::= metadata !"string"
6579bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
6580 // Note: the type 'metadata' has already been parsed.
6581 Metadata *MD;
6582 if (parseMetadata(MD, &PFS))
6583 return true;
6584
6585 V = MetadataAsValue::get(Context, MD);
6586 return false;
6587}
6588
6589/// parseValueAsMetadata
6590/// ::= i32 %local
6591/// ::= i32 @global
6592/// ::= i32 7
6593bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
6594 PerFunctionState *PFS) {
6595 Type *Ty;
6596 LocTy Loc;
6597 if (parseType(Ty, TypeMsg, Loc))
6598 return true;
6599 if (Ty->isMetadataTy())
6600 return error(Loc, "invalid metadata-value-metadata roundtrip");
6601
6602 Value *V;
6603 if (parseValue(Ty, V, PFS))
6604 return true;
6605
6606 MD = ValueAsMetadata::get(V);
6607 return false;
6608}
6609
6610/// parseMetadata
6611/// ::= i32 %local
6612/// ::= i32 @global
6613/// ::= i32 7
6614/// ::= !42
6615/// ::= !{...}
6616/// ::= !"string"
6617/// ::= !DILocation(...)
6618bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) {
6619 if (Lex.getKind() == lltok::MetadataVar) {
6620 // DIArgLists are a special case, as they are a list of ValueAsMetadata and
6621 // so parsing this requires a Function State.
6622 if (Lex.getStrVal() == "DIArgList") {
6623 Metadata *AL;
6624 if (parseDIArgList(AL, PFS))
6625 return true;
6626 MD = AL;
6627 return false;
6628 }
6629 MDNode *N;
6630 if (parseSpecializedMDNode(N)) {
6631 return true;
6632 }
6633 MD = N;
6634 return false;
6635 }
6636
6637 // ValueAsMetadata:
6638 // <type> <value>
6639 if (Lex.getKind() != lltok::exclaim)
6640 return parseValueAsMetadata(MD, "expected metadata operand", PFS);
6641
6642 // '!'.
6643 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
6644 Lex.Lex();
6645
6646 // MDString:
6647 // ::= '!' STRINGCONSTANT
6648 if (Lex.getKind() == lltok::StringConstant) {
6649 MDString *S;
6650 if (parseMDString(S))
6651 return true;
6652 MD = S;
6653 return false;
6654 }
6655
6656 // MDNode:
6657 // !{ ... }
6658 // !7
6659 MDNode *N;
6660 if (parseMDNodeTail(N))
6661 return true;
6662 MD = N;
6663 return false;
6664}
6665
6666//===----------------------------------------------------------------------===//
6667// Function Parsing.
6668//===----------------------------------------------------------------------===//
6669
6670bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
6671 PerFunctionState *PFS) {
6672 if (Ty->isFunctionTy())
6673 return error(ID.Loc, "functions are not values, refer to them as pointers");
6674
6675 switch (ID.Kind) {
6676 case ValID::t_LocalID:
6677 if (!PFS)
6678 return error(ID.Loc, "invalid use of function-local name");
6679 V = PFS->getVal(ID.UIntVal, Ty, ID.Loc);
6680 return V == nullptr;
6681 case ValID::t_LocalName:
6682 if (!PFS)
6683 return error(ID.Loc, "invalid use of function-local name");
6684 V = PFS->getVal(ID.StrVal, Ty, ID.Loc);
6685 return V == nullptr;
6686 case ValID::t_InlineAsm: {
6687 if (!ID.FTy)
6688 return error(ID.Loc, "invalid type for inline asm constraint string");
6689 if (Error Err = InlineAsm::verify(ID.FTy, ID.StrVal2))
6690 return error(ID.Loc, toString(std::move(Err)));
6691 V = InlineAsm::get(
6692 ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1, (ID.UIntVal >> 1) & 1,
6693 InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), (ID.UIntVal >> 3) & 1);
6694 return false;
6695 }
6697 V = getGlobalVal(ID.StrVal, Ty, ID.Loc);
6698 if (V && ID.NoCFI)
6700 return V == nullptr;
6701 case ValID::t_GlobalID:
6702 V = getGlobalVal(ID.UIntVal, Ty, ID.Loc);
6703 if (V && ID.NoCFI)
6705 return V == nullptr;
6706 case ValID::t_APSInt:
6707 if (!Ty->isIntegerTy())
6708 return error(ID.Loc, "integer constant must have integer type");
6709 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
6710 V = ConstantInt::get(Context, ID.APSIntVal);
6711 return false;
6712 case ValID::t_APFloat:
6713 if (!Ty->isFloatingPointTy() ||
6714 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
6715 return error(ID.Loc, "floating point constant invalid for type");
6716
6717 // The lexer has no type info, so builds all half, bfloat, float, and double
6718 // FP constants as double. Fix this here. Long double does not need this.
6719 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
6720 // Check for signaling before potentially converting and losing that info.
6721 bool IsSNAN = ID.APFloatVal.isSignaling();
6722 bool Ignored;
6723 if (Ty->isHalfTy())
6725 &Ignored);
6726 else if (Ty->isBFloatTy())
6727 ID.APFloatVal.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven,
6728 &Ignored);
6729 else if (Ty->isFloatTy())
6731 &Ignored);
6732 if (IsSNAN) {
6733 // The convert call above may quiet an SNaN, so manufacture another
6734 // SNaN. The bitcast works because the payload (significand) parameter
6735 // is truncated to fit.
6736 APInt Payload = ID.APFloatVal.bitcastToAPInt();
6737 ID.APFloatVal = APFloat::getSNaN(ID.APFloatVal.getSemantics(),
6738 ID.APFloatVal.isNegative(), &Payload);
6739 }
6740 }
6741 V = ConstantFP::get(Context, ID.APFloatVal);
6742
6743 if (V->getType() != Ty)
6744 return error(ID.Loc, "floating point constant does not have type '" +
6745 getTypeString(Ty) + "'");
6746
6747 return false;
6748 case ValID::t_Null:
6749 if (!Ty->isPointerTy())
6750 return error(ID.Loc, "null must be a pointer type");
6752 return false;
6753 case ValID::t_Undef:
6754 // FIXME: LabelTy should not be a first-class type.
6755 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6756 return error(ID.Loc, "invalid type for undef constant");
6757 V = UndefValue::get(Ty);
6758 return false;
6760 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
6761 return error(ID.Loc, "invalid empty array initializer");
6762 V = PoisonValue::get(Ty);
6763 return false;
6764 case ValID::t_Zero:
6765 // FIXME: LabelTy should not be a first-class type.
6766 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6767 return error(ID.Loc, "invalid type for null constant");
6768 if (auto *TETy = dyn_cast<TargetExtType>(Ty))
6769 if (!TETy->hasProperty(TargetExtType::HasZeroInit))
6770 return error(ID.Loc, "invalid type for null constant");
6772 return false;
6773 case ValID::t_None:
6774 if (!Ty->isTokenTy())
6775 return error(ID.Loc, "invalid type for none constant");
6777 return false;
6778 case ValID::t_Poison:
6779 // FIXME: LabelTy should not be a first-class type.
6780 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6781 return error(ID.Loc, "invalid type for poison constant");
6782 V = PoisonValue::get(Ty);
6783 return false;
6784 case ValID::t_Constant:
6785 if (ID.ConstantVal->getType() != Ty)
6786 return error(ID.Loc, "constant expression type mismatch: got type '" +
6787 getTypeString(ID.ConstantVal->getType()) +
6788 "' but expected '" + getTypeString(Ty) + "'");
6789 V = ID.ConstantVal;
6790 return false;
6792 if (!Ty->isVectorTy())
6793 return error(ID.Loc, "vector constant must have vector type");
6794 if (ID.ConstantVal->getType() != Ty->getScalarType())
6795 return error(ID.Loc, "constant expression type mismatch: got type '" +
6796 getTypeString(ID.ConstantVal->getType()) +
6797 "' but expected '" +
6798 getTypeString(Ty->getScalarType()) + "'");
6799 V = ConstantVector::getSplat(cast<VectorType>(Ty)->getElementCount(),
6800 ID.ConstantVal);
6801 return false;
6804 if (StructType *ST = dyn_cast<StructType>(Ty)) {
6805 if (ST->getNumElements() != ID.UIntVal)
6806 return error(ID.Loc,
6807 "initializer with struct type has wrong # elements");
6808 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
6809 return error(ID.Loc, "packed'ness of initializer and type don't match");
6810
6811 // Verify that the elements are compatible with the structtype.
6812 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
6813 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
6814 return error(
6815 ID.Loc,
6816 "element " + Twine(i) +
6817 " of struct initializer doesn't match struct element type");
6818
6820 ST, ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
6821 } else
6822 return error(ID.Loc, "constant expression type mismatch");
6823 return false;
6824 }
6825 llvm_unreachable("Invalid ValID");
6826}
6827
6828bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
6829 C = nullptr;
6830 ValID ID;
6831 auto Loc = Lex.getLoc();
6832 if (parseValID(ID, /*PFS=*/nullptr))
6833 return true;
6834 switch (ID.Kind) {
6835 case ValID::t_APSInt:
6836 case ValID::t_APFloat:
6837 case ValID::t_Undef:
6838 case ValID::t_Poison:
6839 case ValID::t_Zero:
6840 case ValID::t_Constant:
6844 Value *V;
6845 if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr))
6846 return true;
6847 assert(isa<Constant>(V) && "Expected a constant value");
6848 C = cast<Constant>(V);
6849 return false;
6850 }
6851 case ValID::t_Null:
6853 return false;
6854 default:
6855 return error(Loc, "expected a constant value");
6856 }
6857}
6858
6859bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
6860 V = nullptr;
6861 ValID ID;
6862
6863 FileLoc Start = getTokLineColumnPos();
6864 bool Ret = parseValID(ID, PFS, Ty) || convertValIDToValue(Ty, ID, V, PFS);
6865 if (!Ret && ParserContext) {
6866 FileLoc End = getPrevTokEndLineColumnPos();
6867 ParserContext->addValueReferenceAtLocation(V, FileLocRange(Start, End));
6868 }
6869 return Ret;
6870}
6871
6872bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) {
6873 Type *Ty = nullptr;
6874 return parseType(Ty) || parseValue(Ty, V, PFS);
6875}
6876
6877bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
6878 PerFunctionState &PFS) {
6879 Value *V;
6880 Loc = Lex.getLoc();
6881 if (parseTypeAndValue(V, PFS))
6882 return true;
6883 if (!isa<BasicBlock>(V))
6884 return error(Loc, "expected a basic block");
6885 BB = cast<BasicBlock>(V);
6886 return false;
6887}
6888
6890 // Exit early for the common (non-debug-intrinsic) case.
6891 // We can make this the only check when we begin supporting all "llvm.dbg"
6892 // intrinsics in the new debug info format.
6893 if (!Name.starts_with("llvm.dbg."))
6894 return false;
6896 return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value ||
6897 FnID == Intrinsic::dbg_assign;
6898}
6899
6900/// FunctionHeader
6901/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
6902/// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
6903/// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
6904/// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
6905bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
6906 unsigned &FunctionNumber,
6907 SmallVectorImpl<unsigned> &UnnamedArgNums) {
6908 // parse the linkage.
6909 LocTy LinkageLoc = Lex.getLoc();
6910 unsigned Linkage;
6911 unsigned Visibility;
6912 unsigned DLLStorageClass;
6913 bool DSOLocal;
6914 AttrBuilder RetAttrs(M->getContext());
6915 unsigned CC;
6916 bool HasLinkage;
6917 Type *RetType = nullptr;
6918 LocTy RetTypeLoc = Lex.getLoc();
6919 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
6920 DSOLocal) ||
6921 parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
6922 parseType(RetType, RetTypeLoc, true /*void allowed*/))
6923 return true;
6924
6925 // Verify that the linkage is ok.
6928 break; // always ok.
6930 if (IsDefine)
6931 return error(LinkageLoc, "invalid linkage for function definition");
6932 break;
6940 if (!IsDefine)
6941 return error(LinkageLoc, "invalid linkage for function declaration");
6942 break;
6945 return error(LinkageLoc, "invalid function linkage type");
6946 }
6947
6948 if (!isValidVisibilityForLinkage(Visibility, Linkage))
6949 return error(LinkageLoc,
6950 "symbol with local linkage must have default visibility");
6951
6952 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
6953 return error(LinkageLoc,
6954 "symbol with local linkage cannot have a DLL storage class");
6955
6956 if (!FunctionType::isValidReturnType(RetType))
6957 return error(RetTypeLoc, "invalid function return type");
6958
6959 LocTy NameLoc = Lex.getLoc();
6960
6961 std::string FunctionName;
6962 if (Lex.getKind() == lltok::GlobalVar) {
6963 FunctionName = Lex.getStrVal();
6964 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
6965 FunctionNumber = Lex.getUIntVal();
6966 if (checkValueID(NameLoc, "function", "@", NumberedVals.getNext(),
6967 FunctionNumber))
6968 return true;
6969 } else {
6970 return tokError("expected function name");
6971 }
6972
6973 Lex.Lex();
6974
6975 if (Lex.getKind() != lltok::lparen)
6976 return tokError("expected '(' in function argument list");
6977
6979 bool IsVarArg;
6980 AttrBuilder FuncAttrs(M->getContext());
6981 std::vector<unsigned> FwdRefAttrGrps;
6982 LocTy BuiltinLoc;
6983 std::string Section;
6984 std::string Partition;
6985 MaybeAlign Alignment, PrefAlignment;
6986 std::string GC;
6988 unsigned AddrSpace = 0;
6989 Constant *Prefix = nullptr;
6990 Constant *Prologue = nullptr;
6991 Constant *PersonalityFn = nullptr;
6992 Comdat *C;
6993
6994 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg) ||
6995 parseOptionalUnnamedAddr(UnnamedAddr) ||
6996 parseOptionalProgramAddrSpace(AddrSpace) ||
6997 parseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
6998 BuiltinLoc) ||
6999 (EatIfPresent(lltok::kw_section) && parseStringConstant(Section)) ||
7000 (EatIfPresent(lltok::kw_partition) && parseStringConstant(Partition)) ||
7001 parseOptionalComdat(FunctionName, C) ||
7002 parseOptionalAlignment(Alignment) ||
7003 parseOptionalPrefAlignment(PrefAlignment) ||
7004 (EatIfPresent(lltok::kw_gc) && parseStringConstant(GC)) ||
7005 (EatIfPresent(lltok::kw_prefix) && parseGlobalTypeAndValue(Prefix)) ||
7006 (EatIfPresent(lltok::kw_prologue) && parseGlobalTypeAndValue(Prologue)) ||
7007 (EatIfPresent(lltok::kw_personality) &&
7008 parseGlobalTypeAndValue(PersonalityFn)))
7009 return true;
7010
7011 if (FuncAttrs.contains(Attribute::Builtin))
7012 return error(BuiltinLoc, "'builtin' attribute not valid on function");
7013
7014 // If the alignment was parsed as an attribute, move to the alignment field.
7015 if (MaybeAlign A = FuncAttrs.getAlignment()) {
7016 Alignment = A;
7017 FuncAttrs.removeAttribute(Attribute::Alignment);
7018 }
7019
7020 // Okay, if we got here, the function is syntactically valid. Convert types
7021 // and do semantic checks.
7022 std::vector<Type*> ParamTypeList;
7024
7025 for (const ArgInfo &Arg : ArgList) {
7026 ParamTypeList.push_back(Arg.Ty);
7027 Attrs.push_back(Arg.Attrs);
7028 }
7029
7030 AttributeList PAL =
7031 AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
7032 AttributeSet::get(Context, RetAttrs), Attrs);
7033
7034 if (PAL.hasParamAttr(0, Attribute::StructRet) && !RetType->isVoidTy())
7035 return error(RetTypeLoc, "functions with 'sret' argument must return void");
7036
7037 FunctionType *FT = FunctionType::get(RetType, ParamTypeList, IsVarArg);
7038 PointerType *PFT = PointerType::get(Context, AddrSpace);
7039
7040 Fn = nullptr;
7041 GlobalValue *FwdFn = nullptr;
7042 if (!FunctionName.empty()) {
7043 // If this was a definition of a forward reference, remove the definition
7044 // from the forward reference table and fill in the forward ref.
7045 auto FRVI = ForwardRefVals.find(FunctionName);
7046 if (FRVI != ForwardRefVals.end()) {
7047 FwdFn = FRVI->second.first;
7048 if (FwdFn->getType() != PFT)
7049 return error(FRVI->second.second,
7050 "invalid forward reference to "
7051 "function '" +
7052 FunctionName +
7053 "' with wrong type: "
7054 "expected '" +
7055 getTypeString(PFT) + "' but was '" +
7056 getTypeString(FwdFn->getType()) + "'");
7057 ForwardRefVals.erase(FRVI);
7058 } else if ((Fn = M->getFunction(FunctionName))) {
7059 // Reject redefinitions.
7060 return error(NameLoc,
7061 "invalid redefinition of function '" + FunctionName + "'");
7062 } else if (M->getNamedValue(FunctionName)) {
7063 return error(NameLoc, "redefinition of function '@" + FunctionName + "'");
7064 }
7065
7066 } else {
7067 // Handle @"", where a name is syntactically specified, but semantically
7068 // missing.
7069 if (FunctionNumber == (unsigned)-1)
7070 FunctionNumber = NumberedVals.getNext();
7071
7072 // If this is a definition of a forward referenced function, make sure the
7073 // types agree.
7074 auto I = ForwardRefValIDs.find(FunctionNumber);
7075 if (I != ForwardRefValIDs.end()) {
7076 FwdFn = I->second.first;
7077 if (FwdFn->getType() != PFT)
7078 return error(NameLoc, "type of definition and forward reference of '@" +
7079 Twine(FunctionNumber) +
7080 "' disagree: "
7081 "expected '" +
7082 getTypeString(PFT) + "' but was '" +
7083 getTypeString(FwdFn->getType()) + "'");
7084 ForwardRefValIDs.erase(I);
7085 }
7086 }
7087
7089 FunctionName, M);
7090
7091 assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
7092
7093 if (FunctionName.empty())
7094 NumberedVals.add(FunctionNumber, Fn);
7095
7097 maybeSetDSOLocal(DSOLocal, *Fn);
7100 Fn->setCallingConv(CC);
7101 Fn->setAttributes(PAL);
7102 Fn->setUnnamedAddr(UnnamedAddr);
7103 if (Alignment)
7104 Fn->setAlignment(*Alignment);
7105 Fn->setPreferredAlignment(PrefAlignment);
7106 Fn->setSection(Section);
7107 Fn->setPartition(Partition);
7108 Fn->setComdat(C);
7109 Fn->setPersonalityFn(PersonalityFn);
7110 if (!GC.empty()) Fn->setGC(GC);
7111 Fn->setPrefixData(Prefix);
7112 Fn->setPrologueData(Prologue);
7113 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
7114
7115 // Add all of the arguments we parsed to the function.
7116 Function::arg_iterator ArgIt = Fn->arg_begin();
7117 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
7118 if (ParserContext && ArgList[i].IdentLoc)
7119 ParserContext->addInstructionOrArgumentLocation(
7120 &*ArgIt, ArgList[i].IdentLoc.value());
7121 // If the argument has a name, insert it into the argument symbol table.
7122 if (ArgList[i].Name.empty()) continue;
7123
7124 // Set the name, if it conflicted, it will be auto-renamed.
7125 ArgIt->setName(ArgList[i].Name);
7126
7127 if (ArgIt->getName() != ArgList[i].Name)
7128 return error(ArgList[i].Loc,
7129 "redefinition of argument '%" + ArgList[i].Name + "'");
7130 }
7131
7132 if (FwdFn) {
7133 FwdFn->replaceAllUsesWith(Fn);
7134 FwdFn->eraseFromParent();
7135 }
7136
7137 if (IsDefine)
7138 return false;
7139
7140 // Check the declaration has no block address forward references.
7141 ValID ID;
7142 if (FunctionName.empty()) {
7143 ID.Kind = ValID::t_GlobalID;
7144 ID.UIntVal = FunctionNumber;
7145 } else {
7146 ID.Kind = ValID::t_GlobalName;
7147 ID.StrVal = FunctionName;
7148 }
7149 auto Blocks = ForwardRefBlockAddresses.find(ID);
7150 if (Blocks != ForwardRefBlockAddresses.end())
7151 return error(Blocks->first.Loc,
7152 "cannot take blockaddress inside a declaration");
7153 return false;
7154}
7155
7156bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
7157 ValID ID;
7158 if (FunctionNumber == -1) {
7159 ID.Kind = ValID::t_GlobalName;
7160 ID.StrVal = std::string(F.getName());
7161 } else {
7162 ID.Kind = ValID::t_GlobalID;
7163 ID.UIntVal = FunctionNumber;
7164 }
7165
7166 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
7167 if (Blocks == P.ForwardRefBlockAddresses.end())
7168 return false;
7169
7170 for (const auto &I : Blocks->second) {
7171 const ValID &BBID = I.first;
7172 GlobalValue *GV = I.second;
7173
7174 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
7175 "Expected local id or name");
7176 BasicBlock *BB;
7177 if (BBID.Kind == ValID::t_LocalName)
7178 BB = getBB(BBID.StrVal, BBID.Loc);
7179 else
7180 BB = getBB(BBID.UIntVal, BBID.Loc);
7181 if (!BB)
7182 return P.error(BBID.Loc, "referenced value is not a basic block");
7183
7184 Value *ResolvedVal = BlockAddress::get(&F, BB);
7185 ResolvedVal = P.checkValidVariableType(BBID.Loc, BBID.StrVal, GV->getType(),
7186 ResolvedVal);
7187 if (!ResolvedVal)
7188 return true;
7189 GV->replaceAllUsesWith(ResolvedVal);
7190 GV->eraseFromParent();
7191 }
7192
7193 P.ForwardRefBlockAddresses.erase(Blocks);
7194 return false;
7195}
7196
7197/// parseFunctionBody
7198/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
7199bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber,
7200 ArrayRef<unsigned> UnnamedArgNums) {
7201 if (Lex.getKind() != lltok::lbrace)
7202 return tokError("expected '{' in function body");
7203 Lex.Lex(); // eat the {.
7204
7205 PerFunctionState PFS(*this, Fn, FunctionNumber, UnnamedArgNums);
7206
7207 // Resolve block addresses and allow basic blocks to be forward-declared
7208 // within this function.
7209 if (PFS.resolveForwardRefBlockAddresses())
7210 return true;
7211 SaveAndRestore ScopeExit(BlockAddressPFS, &PFS);
7212
7213 // We need at least one basic block.
7214 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
7215 return tokError("function body requires at least one basic block");
7216
7217 while (Lex.getKind() != lltok::rbrace &&
7218 Lex.getKind() != lltok::kw_uselistorder)
7219 if (parseBasicBlock(PFS))
7220 return true;
7221
7222 while (Lex.getKind() != lltok::rbrace)
7223 if (parseUseListOrder(&PFS))
7224 return true;
7225
7226 // Eat the }.
7227 Lex.Lex();
7228
7229 // Verify function is ok.
7230 return PFS.finishFunction();
7231}
7232
7233/// parseBasicBlock
7234/// ::= (LabelStr|LabelID)? Instruction*
7235bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
7236 FileLoc BBStart = getTokLineColumnPos();
7237
7238 // If this basic block starts out with a name, remember it.
7239 std::string Name;
7240 int NameID = -1;
7241 LocTy NameLoc = Lex.getLoc();
7242 if (Lex.getKind() == lltok::LabelStr) {
7243 Name = Lex.getStrVal();
7244 Lex.Lex();
7245 } else if (Lex.getKind() == lltok::LabelID) {
7246 NameID = Lex.getUIntVal();
7247 Lex.Lex();
7248 }
7249
7250 BasicBlock *BB = PFS.defineBB(Name, NameID, NameLoc);
7251 if (!BB)
7252 return true;
7253
7254 std::string NameStr;
7255
7256 // Parse the instructions and debug values in this block until we get a
7257 // terminator.
7258 Instruction *Inst;
7259 auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord(); };
7260 using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>;
7261 SmallVector<DbgRecordPtr> TrailingDbgRecord;
7262 do {
7263 // Handle debug records first - there should always be an instruction
7264 // following the debug records, i.e. they cannot appear after the block
7265 // terminator.
7266 while (Lex.getKind() == lltok::hash) {
7267 if (SeenOldDbgInfoFormat)
7268 return error(Lex.getLoc(), "debug record should not appear in a module "
7269 "containing debug info intrinsics");
7270 SeenNewDbgInfoFormat = true;
7271 Lex.Lex();
7272
7273 DbgRecord *DR;
7274 if (parseDebugRecord(DR, PFS))
7275 return true;
7276 TrailingDbgRecord.emplace_back(DR, DeleteDbgRecord);
7277 }
7278
7279 FileLoc InstStart = getTokLineColumnPos();
7280 // This instruction may have three possibilities for a name: a) none
7281 // specified, b) name specified "%foo =", c) number specified: "%4 =".
7282 LocTy NameLoc = Lex.getLoc();
7283 int NameID = -1;
7284 NameStr = "";
7285
7286 if (Lex.getKind() == lltok::LocalVarID) {
7287 NameID = Lex.getUIntVal();
7288 Lex.Lex();
7289 if (parseToken(lltok::equal, "expected '=' after instruction id"))
7290 return true;
7291 } else if (Lex.getKind() == lltok::LocalVar) {
7292 NameStr = Lex.getStrVal();
7293 Lex.Lex();
7294 if (parseToken(lltok::equal, "expected '=' after instruction name"))
7295 return true;
7296 }
7297
7298 switch (parseInstruction(Inst, BB, PFS)) {
7299 default:
7300 llvm_unreachable("Unknown parseInstruction result!");
7301 case InstError: return true;
7302 case InstNormal:
7303 Inst->insertInto(BB, BB->end());
7304
7305 // With a normal result, we check to see if the instruction is followed by
7306 // a comma and metadata.
7307 if (EatIfPresent(lltok::comma))
7308 if (parseInstructionMetadata(*Inst))
7309 return true;
7310 break;
7311 case InstExtraComma:
7312 Inst->insertInto(BB, BB->end());
7313
7314 // If the instruction parser ate an extra comma at the end of it, it
7315 // *must* be followed by metadata.
7316 if (parseInstructionMetadata(*Inst))
7317 return true;
7318 break;
7319 }
7320
7321 // Set the name on the instruction.
7322 if (PFS.setInstName(NameID, NameStr, NameLoc, Inst))
7323 return true;
7324
7325 // Attach any preceding debug values to this instruction.
7326 for (DbgRecordPtr &DR : TrailingDbgRecord)
7327 BB->insertDbgRecordBefore(DR.release(), Inst->getIterator());
7328 TrailingDbgRecord.clear();
7329 if (ParserContext) {
7330 ParserContext->addInstructionOrArgumentLocation(
7331 Inst, FileLocRange(InstStart, getPrevTokEndLineColumnPos()));
7332 }
7333 } while (!Inst->isTerminator());
7334
7335 if (ParserContext)
7336 ParserContext->addBlockLocation(
7337 BB, FileLocRange(BBStart, getPrevTokEndLineColumnPos()));
7338
7339 assert(TrailingDbgRecord.empty() &&
7340 "All debug values should have been attached to an instruction.");
7341
7342 return false;
7343}
7344
7345/// parseDebugRecord
7346/// ::= #dbg_label '(' MDNode ')'
7347/// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ','
7348/// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')'
7349bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
7350 using RecordKind = DbgRecord::Kind;
7351 using LocType = DbgVariableRecord::LocationType;
7352 LocTy DVRLoc = Lex.getLoc();
7353 if (Lex.getKind() != lltok::DbgRecordType)
7354 return error(DVRLoc, "expected debug record type here");
7355 RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal())
7356 .Case("declare", RecordKind::ValueKind)
7357 .Case("value", RecordKind::ValueKind)
7358 .Case("assign", RecordKind::ValueKind)
7359 .Case("label", RecordKind::LabelKind)
7360 .Case("declare_value", RecordKind::ValueKind);
7361
7362 // Parsing labels is trivial; parse here and early exit, otherwise go into the
7363 // full DbgVariableRecord processing stage.
7364 if (RecordType == RecordKind::LabelKind) {
7365 Lex.Lex();
7366 if (parseToken(lltok::lparen, "Expected '(' here"))
7367 return true;
7368 MDNode *Label;
7369 if (parseMDNode(Label))
7370 return true;
7371 if (parseToken(lltok::comma, "Expected ',' here"))
7372 return true;
7373 MDNode *DbgLoc;
7374 if (parseMDNode(DbgLoc))
7375 return true;
7376 if (parseToken(lltok::rparen, "Expected ')' here"))
7377 return true;
7379 return false;
7380 }
7381
7382 LocType ValueType = StringSwitch<LocType>(Lex.getStrVal())
7383 .Case("declare", LocType::Declare)
7384 .Case("value", LocType::Value)
7385 .Case("assign", LocType::Assign)
7386 .Case("declare_value", LocType::DeclareValue);
7387
7388 Lex.Lex();
7389 if (parseToken(lltok::lparen, "Expected '(' here"))
7390 return true;
7391
7392 // Parse Value field.
7393 Metadata *ValLocMD;
7394 if (parseMetadata(ValLocMD, &PFS))
7395 return true;
7396 if (parseToken(lltok::comma, "Expected ',' here"))
7397 return true;
7398
7399 // Parse Variable field.
7400 MDNode *Variable;
7401 if (parseMDNode(Variable))
7402 return true;
7403 if (parseToken(lltok::comma, "Expected ',' here"))
7404 return true;
7405
7406 // Parse Expression field.
7407 MDNode *Expression;
7408 if (parseMDNode(Expression))
7409 return true;
7410 if (parseToken(lltok::comma, "Expected ',' here"))
7411 return true;
7412
7413 // Parse additional fields for #dbg_assign.
7414 MDNode *AssignID = nullptr;
7415 Metadata *AddressLocation = nullptr;
7416 MDNode *AddressExpression = nullptr;
7417 if (ValueType == LocType::Assign) {
7418 // Parse DIAssignID.
7419 if (parseMDNode(AssignID))
7420 return true;
7421 if (parseToken(lltok::comma, "Expected ',' here"))
7422 return true;
7423
7424 // Parse address ValueAsMetadata.
7425 if (parseMetadata(AddressLocation, &PFS))
7426 return true;
7427 if (parseToken(lltok::comma, "Expected ',' here"))
7428 return true;
7429
7430 // Parse address DIExpression.
7431 if (parseMDNode(AddressExpression))
7432 return true;
7433 if (parseToken(lltok::comma, "Expected ',' here"))
7434 return true;
7435 }
7436
7437 /// Parse DILocation.
7438 MDNode *DebugLoc;
7439 if (parseMDNode(DebugLoc))
7440 return true;
7441
7442 if (parseToken(lltok::rparen, "Expected ')' here"))
7443 return true;
7445 ValueType, ValLocMD, Variable, Expression, AssignID, AddressLocation,
7446 AddressExpression, DebugLoc);
7447 return false;
7448}
7449//===----------------------------------------------------------------------===//
7450// Instruction Parsing.
7451//===----------------------------------------------------------------------===//
7452
7453/// parseInstruction - parse one of the many different instructions.
7454///
7455int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
7456 PerFunctionState &PFS) {
7457 lltok::Kind Token = Lex.getKind();
7458 if (Token == lltok::Eof)
7459 return tokError("found end of file when expecting more instructions");
7460 LocTy Loc = Lex.getLoc();
7461 unsigned KeywordVal = Lex.getUIntVal();
7462 Lex.Lex(); // Eat the keyword.
7463
7464 switch (Token) {
7465 default:
7466 return error(Loc, "expected instruction opcode");
7467 // Terminator Instructions.
7468 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
7469 case lltok::kw_ret:
7470 return parseRet(Inst, BB, PFS);
7471 case lltok::kw_br:
7472 return parseBr(Inst, PFS);
7473 case lltok::kw_switch:
7474 return parseSwitch(Inst, PFS);
7476 return parseIndirectBr(Inst, PFS);
7477 case lltok::kw_invoke:
7478 return parseInvoke(Inst, PFS);
7479 case lltok::kw_resume:
7480 return parseResume(Inst, PFS);
7482 return parseCleanupRet(Inst, PFS);
7483 case lltok::kw_catchret:
7484 return parseCatchRet(Inst, PFS);
7486 return parseCatchSwitch(Inst, PFS);
7487 case lltok::kw_catchpad:
7488 return parseCatchPad(Inst, PFS);
7490 return parseCleanupPad(Inst, PFS);
7491 case lltok::kw_callbr:
7492 return parseCallBr(Inst, PFS);
7493 // Unary Operators.
7494 case lltok::kw_fneg: {
7495 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7496 int Res = parseUnaryOp(Inst, PFS, KeywordVal, /*IsFP*/ true);
7497 if (Res != 0)
7498 return Res;
7499 if (FMF.any())
7500 Inst->setFastMathFlags(FMF);
7501 return false;
7502 }
7503 // Binary Operators.
7504 case lltok::kw_add:
7505 case lltok::kw_sub:
7506 case lltok::kw_mul:
7507 case lltok::kw_shl: {
7508 bool NUW = EatIfPresent(lltok::kw_nuw);
7509 bool NSW = EatIfPresent(lltok::kw_nsw);
7510 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
7511
7512 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
7513 return true;
7514
7515 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
7516 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
7517 return false;
7518 }
7519 case lltok::kw_fadd:
7520 case lltok::kw_fsub:
7521 case lltok::kw_fmul:
7522 case lltok::kw_fdiv:
7523 case lltok::kw_frem: {
7524 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7525 int Res = parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ true);
7526 if (Res != 0)
7527 return Res;
7528 if (FMF.any())
7529 Inst->setFastMathFlags(FMF);
7530 return 0;
7531 }
7532
7533 case lltok::kw_sdiv:
7534 case lltok::kw_udiv:
7535 case lltok::kw_lshr:
7536 case lltok::kw_ashr: {
7537 bool Exact = EatIfPresent(lltok::kw_exact);
7538
7539 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
7540 return true;
7541 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
7542 return false;
7543 }
7544
7545 case lltok::kw_urem:
7546 case lltok::kw_srem:
7547 return parseArithmetic(Inst, PFS, KeywordVal,
7548 /*IsFP*/ false);
7549 case lltok::kw_or: {
7550 bool Disjoint = EatIfPresent(lltok::kw_disjoint);
7551 if (parseLogical(Inst, PFS, KeywordVal))
7552 return true;
7553 if (Disjoint)
7554 cast<PossiblyDisjointInst>(Inst)->setIsDisjoint(true);
7555 return false;
7556 }
7557 case lltok::kw_and:
7558 case lltok::kw_xor:
7559 return parseLogical(Inst, PFS, KeywordVal);
7560 case lltok::kw_icmp: {
7561 bool SameSign = EatIfPresent(lltok::kw_samesign);
7562 if (parseCompare(Inst, PFS, KeywordVal))
7563 return true;
7564 if (SameSign)
7565 cast<ICmpInst>(Inst)->setSameSign();
7566 return false;
7567 }
7568 case lltok::kw_fcmp: {
7569 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7570 int Res = parseCompare(Inst, PFS, KeywordVal);
7571 if (Res != 0)
7572 return Res;
7573 if (FMF.any())
7574 Inst->setFastMathFlags(FMF);
7575 return 0;
7576 }
7577
7578 // Casts.
7579 case lltok::kw_uitofp:
7580 case lltok::kw_zext: {
7581 bool NonNeg = EatIfPresent(lltok::kw_nneg);
7582 bool Res = parseCast(Inst, PFS, KeywordVal);
7583 if (Res != 0)
7584 return Res;
7585 if (NonNeg)
7586 Inst->setNonNeg();
7587 return 0;
7588 }
7589 case lltok::kw_trunc: {
7590 bool NUW = EatIfPresent(lltok::kw_nuw);
7591 bool NSW = EatIfPresent(lltok::kw_nsw);
7592 if (!NUW)
7593 NUW = EatIfPresent(lltok::kw_nuw);
7594 if (parseCast(Inst, PFS, KeywordVal))
7595 return true;
7596 if (NUW)
7597 cast<TruncInst>(Inst)->setHasNoUnsignedWrap(true);
7598 if (NSW)
7599 cast<TruncInst>(Inst)->setHasNoSignedWrap(true);
7600 return false;
7601 }
7602 case lltok::kw_sext:
7603 case lltok::kw_bitcast:
7605 case lltok::kw_sitofp:
7606 case lltok::kw_fptoui:
7607 case lltok::kw_fptosi:
7608 case lltok::kw_inttoptr:
7610 case lltok::kw_ptrtoint:
7611 return parseCast(Inst, PFS, KeywordVal);
7612 case lltok::kw_fptrunc:
7613 case lltok::kw_fpext: {
7614 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7615 if (parseCast(Inst, PFS, KeywordVal))
7616 return true;
7617 if (FMF.any())
7618 Inst->setFastMathFlags(FMF);
7619 return false;
7620 }
7621
7622 // Other.
7623 case lltok::kw_select: {
7624 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7625 int Res = parseSelect(Inst, PFS);
7626 if (Res != 0)
7627 return Res;
7628 if (FMF.any()) {
7629 if (!isa<FPMathOperator>(Inst))
7630 return error(Loc, "fast-math-flags specified for select without "
7631 "floating-point scalar or vector return type");
7632 Inst->setFastMathFlags(FMF);
7633 }
7634 return 0;
7635 }
7636 case lltok::kw_va_arg:
7637 return parseVAArg(Inst, PFS);
7639 return parseExtractElement(Inst, PFS);
7641 return parseInsertElement(Inst, PFS);
7643 return parseShuffleVector(Inst, PFS);
7644 case lltok::kw_phi: {
7645 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7646 int Res = parsePHI(Inst, PFS);
7647 if (Res != 0)
7648 return Res;
7649 if (FMF.any()) {
7650 if (!isa<FPMathOperator>(Inst))
7651 return error(Loc, "fast-math-flags specified for phi without "
7652 "floating-point scalar or vector return type");
7653 Inst->setFastMathFlags(FMF);
7654 }
7655 return 0;
7656 }
7658 return parseLandingPad(Inst, PFS);
7659 case lltok::kw_freeze:
7660 return parseFreeze(Inst, PFS);
7661 // Call.
7662 case lltok::kw_call:
7663 return parseCall(Inst, PFS, CallInst::TCK_None);
7664 case lltok::kw_tail:
7665 return parseCall(Inst, PFS, CallInst::TCK_Tail);
7666 case lltok::kw_musttail:
7667 return parseCall(Inst, PFS, CallInst::TCK_MustTail);
7668 case lltok::kw_notail:
7669 return parseCall(Inst, PFS, CallInst::TCK_NoTail);
7670 // Memory.
7671 case lltok::kw_alloca:
7672 return parseAlloc(Inst, PFS);
7673 case lltok::kw_load:
7674 return parseLoad(Inst, PFS);
7675 case lltok::kw_store:
7676 return parseStore(Inst, PFS);
7677 case lltok::kw_cmpxchg:
7678 return parseCmpXchg(Inst, PFS);
7680 return parseAtomicRMW(Inst, PFS);
7681 case lltok::kw_fence:
7682 return parseFence(Inst, PFS);
7684 return parseGetElementPtr(Inst, PFS);
7686 return parseExtractValue(Inst, PFS);
7688 return parseInsertValue(Inst, PFS);
7689 }
7690}
7691
7692/// parseCmpPredicate - parse an integer or fp predicate, based on Kind.
7693bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) {
7694 if (Opc == Instruction::FCmp) {
7695 switch (Lex.getKind()) {
7696 default:
7697 return tokError("expected fcmp predicate (e.g. 'oeq')");
7698 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
7699 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
7700 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
7701 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
7702 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
7703 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
7704 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
7705 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
7706 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
7707 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
7708 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
7709 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
7710 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
7711 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
7712 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
7713 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
7714 }
7715 } else {
7716 switch (Lex.getKind()) {
7717 default:
7718 return tokError("expected icmp predicate (e.g. 'eq')");
7719 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
7720 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
7721 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
7722 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
7723 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
7724 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
7725 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
7726 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
7727 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
7728 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
7729 }
7730 }
7731 Lex.Lex();
7732 return false;
7733}
7734
7735//===----------------------------------------------------------------------===//
7736// Terminator Instructions.
7737//===----------------------------------------------------------------------===//
7738
7739/// parseRet - parse a return instruction.
7740/// ::= 'ret' void (',' !dbg, !1)*
7741/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
7742bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB,
7743 PerFunctionState &PFS) {
7744 SMLoc TypeLoc = Lex.getLoc();
7745 Type *Ty = nullptr;
7746 if (parseType(Ty, true /*void allowed*/))
7747 return true;
7748
7749 Type *ResType = PFS.getFunction().getReturnType();
7750
7751 if (Ty->isVoidTy()) {
7752 if (!ResType->isVoidTy())
7753 return error(TypeLoc, "value doesn't match function result type '" +
7754 getTypeString(ResType) + "'");
7755
7756 Inst = ReturnInst::Create(Context);
7757 return false;
7758 }
7759
7760 Value *RV;
7761 if (parseValue(Ty, RV, PFS))
7762 return true;
7763
7764 if (ResType != RV->getType())
7765 return error(TypeLoc, "value doesn't match function result type '" +
7766 getTypeString(ResType) + "'");
7767
7768 Inst = ReturnInst::Create(Context, RV);
7769 return false;
7770}
7771
7772/// parseBr
7773/// ::= 'br' TypeAndValue
7774/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
7775bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) {
7776 LocTy Loc, Loc2;
7777 Value *Op0;
7778 BasicBlock *Op1, *Op2;
7779 if (parseTypeAndValue(Op0, Loc, PFS))
7780 return true;
7781
7782 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
7783 Inst = BranchInst::Create(BB);
7784 return false;
7785 }
7786
7787 if (Op0->getType() != Type::getInt1Ty(Context))
7788 return error(Loc, "branch condition must have 'i1' type");
7789
7790 if (parseToken(lltok::comma, "expected ',' after branch condition") ||
7791 parseTypeAndBasicBlock(Op1, Loc, PFS) ||
7792 parseToken(lltok::comma, "expected ',' after true destination") ||
7793 parseTypeAndBasicBlock(Op2, Loc2, PFS))
7794 return true;
7795
7796 Inst = BranchInst::Create(Op1, Op2, Op0);
7797 return false;
7798}
7799
7800/// parseSwitch
7801/// Instruction
7802/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
7803/// JumpTable
7804/// ::= (TypeAndValue ',' TypeAndValue)*
7805bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
7806 LocTy CondLoc, BBLoc;
7807 Value *Cond;
7808 BasicBlock *DefaultBB;
7809 if (parseTypeAndValue(Cond, CondLoc, PFS) ||
7810 parseToken(lltok::comma, "expected ',' after switch condition") ||
7811 parseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
7812 parseToken(lltok::lsquare, "expected '[' with switch table"))
7813 return true;
7814
7815 if (!Cond->getType()->isIntegerTy())
7816 return error(CondLoc, "switch condition must have integer type");
7817
7818 // parse the jump table pairs.
7819 SmallPtrSet<Value*, 32> SeenCases;
7821 while (Lex.getKind() != lltok::rsquare) {
7822 Value *Constant;
7823 BasicBlock *DestBB;
7824
7825 if (parseTypeAndValue(Constant, CondLoc, PFS) ||
7826 parseToken(lltok::comma, "expected ',' after case value") ||
7827 parseTypeAndBasicBlock(DestBB, PFS))
7828 return true;
7829
7830 if (!SeenCases.insert(Constant).second)
7831 return error(CondLoc, "duplicate case value in switch");
7832 if (!isa<ConstantInt>(Constant))
7833 return error(CondLoc, "case value is not a constant integer");
7834
7835 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
7836 }
7837
7838 Lex.Lex(); // Eat the ']'.
7839
7840 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
7841 for (const auto &[OnVal, Dest] : Table)
7842 SI->addCase(OnVal, Dest);
7843 Inst = SI;
7844 return false;
7845}
7846
7847/// parseIndirectBr
7848/// Instruction
7849/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
7850bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
7851 LocTy AddrLoc;
7852 Value *Address;
7853 if (parseTypeAndValue(Address, AddrLoc, PFS) ||
7854 parseToken(lltok::comma, "expected ',' after indirectbr address") ||
7855 parseToken(lltok::lsquare, "expected '[' with indirectbr"))
7856 return true;
7857
7858 if (!Address->getType()->isPointerTy())
7859 return error(AddrLoc, "indirectbr address must have pointer type");
7860
7861 // parse the destination list.
7862 SmallVector<BasicBlock*, 16> DestList;
7863
7864 if (Lex.getKind() != lltok::rsquare) {
7865 BasicBlock *DestBB;
7866 if (parseTypeAndBasicBlock(DestBB, PFS))
7867 return true;
7868 DestList.push_back(DestBB);
7869
7870 while (EatIfPresent(lltok::comma)) {
7871 if (parseTypeAndBasicBlock(DestBB, PFS))
7872 return true;
7873 DestList.push_back(DestBB);
7874 }
7875 }
7876
7877 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
7878 return true;
7879
7880 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
7881 for (BasicBlock *Dest : DestList)
7882 IBI->addDestination(Dest);
7883 Inst = IBI;
7884 return false;
7885}
7886
7887// If RetType is a non-function pointer type, then this is the short syntax
7888// for the call, which means that RetType is just the return type. Infer the
7889// rest of the function argument types from the arguments that are present.
7890bool LLParser::resolveFunctionType(Type *RetType, ArrayRef<ParamInfo> ArgList,
7891 FunctionType *&FuncTy) {
7892 FuncTy = dyn_cast<FunctionType>(RetType);
7893 if (!FuncTy) {
7894 // Pull out the types of all of the arguments...
7895 SmallVector<Type *, 8> ParamTypes;
7896 ParamTypes.reserve(ArgList.size());
7897 for (const ParamInfo &Arg : ArgList)
7898 ParamTypes.push_back(Arg.V->getType());
7899
7900 if (!FunctionType::isValidReturnType(RetType))
7901 return true;
7902
7903 FuncTy = FunctionType::get(RetType, ParamTypes, false);
7904 }
7905 return false;
7906}
7907
7908/// parseInvoke
7909/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
7910/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
7911bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
7912 LocTy CallLoc = Lex.getLoc();
7913 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
7914 std::vector<unsigned> FwdRefAttrGrps;
7915 LocTy NoBuiltinLoc;
7916 unsigned CC;
7917 unsigned InvokeAddrSpace;
7918 Type *RetType = nullptr;
7919 LocTy RetTypeLoc;
7920 ValID CalleeID;
7923
7924 BasicBlock *NormalBB, *UnwindBB;
7925 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
7926 parseOptionalProgramAddrSpace(InvokeAddrSpace) ||
7927 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
7928 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
7929 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
7930 NoBuiltinLoc) ||
7931 parseOptionalOperandBundles(BundleList, PFS) ||
7932 parseToken(lltok::kw_to, "expected 'to' in invoke") ||
7933 parseTypeAndBasicBlock(NormalBB, PFS) ||
7934 parseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
7935 parseTypeAndBasicBlock(UnwindBB, PFS))
7936 return true;
7937
7938 // If RetType is a non-function pointer type, then this is the short syntax
7939 // for the call, which means that RetType is just the return type. Infer the
7940 // rest of the function argument types from the arguments that are present.
7941 FunctionType *Ty;
7942 if (resolveFunctionType(RetType, ArgList, Ty))
7943 return error(RetTypeLoc, "Invalid result type for LLVM function");
7944
7945 CalleeID.FTy = Ty;
7946
7947 // Look up the callee.
7948 Value *Callee;
7949 if (convertValIDToValue(PointerType::get(Context, InvokeAddrSpace), CalleeID,
7950 Callee, &PFS))
7951 return true;
7952
7953 // Set up the Attribute for the function.
7954 SmallVector<Value *, 8> Args;
7956
7957 // Loop through FunctionType's arguments and ensure they are specified
7958 // correctly. Also, gather any parameter attributes.
7959 FunctionType::param_iterator I = Ty->param_begin();
7960 FunctionType::param_iterator E = Ty->param_end();
7961 for (const ParamInfo &Arg : ArgList) {
7962 Type *ExpectedTy = nullptr;
7963 if (I != E) {
7964 ExpectedTy = *I++;
7965 } else if (!Ty->isVarArg()) {
7966 return error(Arg.Loc, "too many arguments specified");
7967 }
7968
7969 if (ExpectedTy && ExpectedTy != Arg.V->getType())
7970 return error(Arg.Loc, "argument is not of expected type '" +
7971 getTypeString(ExpectedTy) + "'");
7972 Args.push_back(Arg.V);
7973 ArgAttrs.push_back(Arg.Attrs);
7974 }
7975
7976 if (I != E)
7977 return error(CallLoc, "not enough parameters specified for call");
7978
7979 // Finish off the Attribute and check them
7980 AttributeList PAL =
7981 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
7982 AttributeSet::get(Context, RetAttrs), ArgAttrs);
7983
7984 InvokeInst *II =
7985 InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
7986 II->setCallingConv(CC);
7987 II->setAttributes(PAL);
7988 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
7989 Inst = II;
7990 return false;
7991}
7992
7993/// parseResume
7994/// ::= 'resume' TypeAndValue
7995bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) {
7996 Value *Exn; LocTy ExnLoc;
7997 if (parseTypeAndValue(Exn, ExnLoc, PFS))
7998 return true;
7999
8000 ResumeInst *RI = ResumeInst::Create(Exn);
8001 Inst = RI;
8002 return false;
8003}
8004
8005bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args,
8006 PerFunctionState &PFS) {
8007 if (parseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
8008 return true;
8009
8010 while (Lex.getKind() != lltok::rsquare) {
8011 // If this isn't the first argument, we need a comma.
8012 if (!Args.empty() &&
8013 parseToken(lltok::comma, "expected ',' in argument list"))
8014 return true;
8015
8016 // parse the argument.
8017 LocTy ArgLoc;
8018 Type *ArgTy = nullptr;
8019 if (parseType(ArgTy, ArgLoc))
8020 return true;
8021
8022 Value *V;
8023 if (ArgTy->isMetadataTy()) {
8024 if (parseMetadataAsValue(V, PFS))
8025 return true;
8026 } else {
8027 if (parseValue(ArgTy, V, PFS))
8028 return true;
8029 }
8030 Args.push_back(V);
8031 }
8032
8033 Lex.Lex(); // Lex the ']'.
8034 return false;
8035}
8036
8037/// parseCleanupRet
8038/// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
8039bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
8040 Value *CleanupPad = nullptr;
8041
8042 if (parseToken(lltok::kw_from, "expected 'from' after cleanupret"))
8043 return true;
8044
8045 if (parseValue(Type::getTokenTy(Context), CleanupPad, PFS))
8046 return true;
8047
8048 if (parseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
8049 return true;
8050
8051 BasicBlock *UnwindBB = nullptr;
8052 if (Lex.getKind() == lltok::kw_to) {
8053 Lex.Lex();
8054 if (parseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
8055 return true;
8056 } else {
8057 if (parseTypeAndBasicBlock(UnwindBB, PFS)) {
8058 return true;
8059 }
8060 }
8061
8062 Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
8063 return false;
8064}
8065
8066/// parseCatchRet
8067/// ::= 'catchret' from Parent Value 'to' TypeAndValue
8068bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
8069 Value *CatchPad = nullptr;
8070
8071 if (parseToken(lltok::kw_from, "expected 'from' after catchret"))
8072 return true;
8073
8074 if (parseValue(Type::getTokenTy(Context), CatchPad, PFS))
8075 return true;
8076
8077 BasicBlock *BB;
8078 if (parseToken(lltok::kw_to, "expected 'to' in catchret") ||
8079 parseTypeAndBasicBlock(BB, PFS))
8080 return true;
8081
8082 Inst = CatchReturnInst::Create(CatchPad, BB);
8083 return false;
8084}
8085
8086/// parseCatchSwitch
8087/// ::= 'catchswitch' within Parent
8088bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
8089 Value *ParentPad;
8090
8091 if (parseToken(lltok::kw_within, "expected 'within' after catchswitch"))
8092 return true;
8093
8094 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
8095 Lex.getKind() != lltok::LocalVarID)
8096 return tokError("expected scope value for catchswitch");
8097
8098 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
8099 return true;
8100
8101 if (parseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
8102 return true;
8103
8105 do {
8106 BasicBlock *DestBB;
8107 if (parseTypeAndBasicBlock(DestBB, PFS))
8108 return true;
8109 Table.push_back(DestBB);
8110 } while (EatIfPresent(lltok::comma));
8111
8112 if (parseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
8113 return true;
8114
8115 if (parseToken(lltok::kw_unwind, "expected 'unwind' after catchswitch scope"))
8116 return true;
8117
8118 BasicBlock *UnwindBB = nullptr;
8119 if (EatIfPresent(lltok::kw_to)) {
8120 if (parseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
8121 return true;
8122 } else {
8123 if (parseTypeAndBasicBlock(UnwindBB, PFS))
8124 return true;
8125 }
8126
8127 auto *CatchSwitch =
8128 CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
8129 for (BasicBlock *DestBB : Table)
8130 CatchSwitch->addHandler(DestBB);
8131 Inst = CatchSwitch;
8132 return false;
8133}
8134
8135/// parseCatchPad
8136/// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
8137bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
8138 Value *CatchSwitch = nullptr;
8139
8140 if (parseToken(lltok::kw_within, "expected 'within' after catchpad"))
8141 return true;
8142
8143 if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
8144 return tokError("expected scope value for catchpad");
8145
8146 if (parseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
8147 return true;
8148
8149 SmallVector<Value *, 8> Args;
8150 if (parseExceptionArgs(Args, PFS))
8151 return true;
8152
8153 Inst = CatchPadInst::Create(CatchSwitch, Args);
8154 return false;
8155}
8156
8157/// parseCleanupPad
8158/// ::= 'cleanuppad' within Parent ParamList
8159bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
8160 Value *ParentPad = nullptr;
8161
8162 if (parseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
8163 return true;
8164
8165 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
8166 Lex.getKind() != lltok::LocalVarID)
8167 return tokError("expected scope value for cleanuppad");
8168
8169 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
8170 return true;
8171
8172 SmallVector<Value *, 8> Args;
8173 if (parseExceptionArgs(Args, PFS))
8174 return true;
8175
8176 Inst = CleanupPadInst::Create(ParentPad, Args);
8177 return false;
8178}
8179
8180//===----------------------------------------------------------------------===//
8181// Unary Operators.
8182//===----------------------------------------------------------------------===//
8183
8184/// parseUnaryOp
8185/// ::= UnaryOp TypeAndValue ',' Value
8186///
8187/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
8188/// operand is allowed.
8189bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
8190 unsigned Opc, bool IsFP) {
8191 LocTy Loc; Value *LHS;
8192 if (parseTypeAndValue(LHS, Loc, PFS))
8193 return true;
8194
8195 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
8197
8198 if (!Valid)
8199 return error(Loc, "invalid operand type for instruction");
8200
8202 return false;
8203}
8204
8205/// parseCallBr
8206/// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList
8207/// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue
8208/// '[' LabelList ']'
8209bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
8210 LocTy CallLoc = Lex.getLoc();
8211 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8212 std::vector<unsigned> FwdRefAttrGrps;
8213 LocTy NoBuiltinLoc;
8214 unsigned CC;
8215 Type *RetType = nullptr;
8216 LocTy RetTypeLoc;
8217 ValID CalleeID;
8220
8221 BasicBlock *DefaultDest;
8222 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
8223 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
8224 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
8225 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
8226 NoBuiltinLoc) ||
8227 parseOptionalOperandBundles(BundleList, PFS) ||
8228 parseToken(lltok::kw_to, "expected 'to' in callbr") ||
8229 parseTypeAndBasicBlock(DefaultDest, PFS) ||
8230 parseToken(lltok::lsquare, "expected '[' in callbr"))
8231 return true;
8232
8233 // parse the destination list.
8234 SmallVector<BasicBlock *, 16> IndirectDests;
8235
8236 if (Lex.getKind() != lltok::rsquare) {
8237 BasicBlock *DestBB;
8238 if (parseTypeAndBasicBlock(DestBB, PFS))
8239 return true;
8240 IndirectDests.push_back(DestBB);
8241
8242 while (EatIfPresent(lltok::comma)) {
8243 if (parseTypeAndBasicBlock(DestBB, PFS))
8244 return true;
8245 IndirectDests.push_back(DestBB);
8246 }
8247 }
8248
8249 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
8250 return true;
8251
8252 // If RetType is a non-function pointer type, then this is the short syntax
8253 // for the call, which means that RetType is just the return type. Infer the
8254 // rest of the function argument types from the arguments that are present.
8255 FunctionType *Ty;
8256 if (resolveFunctionType(RetType, ArgList, Ty))
8257 return error(RetTypeLoc, "Invalid result type for LLVM function");
8258
8259 CalleeID.FTy = Ty;
8260
8261 // Look up the callee.
8262 Value *Callee;
8263 if (convertValIDToValue(PointerType::getUnqual(Context), CalleeID, Callee,
8264 &PFS))
8265 return true;
8266
8267 // Set up the Attribute for the function.
8268 SmallVector<Value *, 8> Args;
8270
8271 // Loop through FunctionType's arguments and ensure they are specified
8272 // correctly. Also, gather any parameter attributes.
8273 FunctionType::param_iterator I = Ty->param_begin();
8274 FunctionType::param_iterator E = Ty->param_end();
8275 for (const ParamInfo &Arg : ArgList) {
8276 Type *ExpectedTy = nullptr;
8277 if (I != E) {
8278 ExpectedTy = *I++;
8279 } else if (!Ty->isVarArg()) {
8280 return error(Arg.Loc, "too many arguments specified");
8281 }
8282
8283 if (ExpectedTy && ExpectedTy != Arg.V->getType())
8284 return error(Arg.Loc, "argument is not of expected type '" +
8285 getTypeString(ExpectedTy) + "'");
8286 Args.push_back(Arg.V);
8287 ArgAttrs.push_back(Arg.Attrs);
8288 }
8289
8290 if (I != E)
8291 return error(CallLoc, "not enough parameters specified for call");
8292
8293 // Finish off the Attribute and check them
8294 AttributeList PAL =
8295 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
8296 AttributeSet::get(Context, RetAttrs), ArgAttrs);
8297
8298 CallBrInst *CBI =
8299 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
8300 BundleList);
8301 CBI->setCallingConv(CC);
8302 CBI->setAttributes(PAL);
8303 ForwardRefAttrGroups[CBI] = FwdRefAttrGrps;
8304 Inst = CBI;
8305 return false;
8306}
8307
8308//===----------------------------------------------------------------------===//
8309// Binary Operators.
8310//===----------------------------------------------------------------------===//
8311
8312/// parseArithmetic
8313/// ::= ArithmeticOps TypeAndValue ',' Value
8314///
8315/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
8316/// operand is allowed.
8317bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
8318 unsigned Opc, bool IsFP) {
8319 LocTy Loc; Value *LHS, *RHS;
8320 if (parseTypeAndValue(LHS, Loc, PFS) ||
8321 parseToken(lltok::comma, "expected ',' in arithmetic operation") ||
8322 parseValue(LHS->getType(), RHS, PFS))
8323 return true;
8324
8325 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
8327
8328 if (!Valid)
8329 return error(Loc, "invalid operand type for instruction");
8330
8332 return false;
8333}
8334
8335/// parseLogical
8336/// ::= ArithmeticOps TypeAndValue ',' Value {
8337bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS,
8338 unsigned Opc) {
8339 LocTy Loc; Value *LHS, *RHS;
8340 if (parseTypeAndValue(LHS, Loc, PFS) ||
8341 parseToken(lltok::comma, "expected ',' in logical operation") ||
8342 parseValue(LHS->getType(), RHS, PFS))
8343 return true;
8344
8345 if (!LHS->getType()->isIntOrIntVectorTy())
8346 return error(Loc,
8347 "instruction requires integer or integer vector operands");
8348
8350 return false;
8351}
8352
8353/// parseCompare
8354/// ::= 'icmp' IPredicates TypeAndValue ',' Value
8355/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
8356bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS,
8357 unsigned Opc) {
8358 // parse the integer/fp comparison predicate.
8359 LocTy Loc;
8360 unsigned Pred;
8361 Value *LHS, *RHS;
8362 if (parseCmpPredicate(Pred, Opc) || parseTypeAndValue(LHS, Loc, PFS) ||
8363 parseToken(lltok::comma, "expected ',' after compare value") ||
8364 parseValue(LHS->getType(), RHS, PFS))
8365 return true;
8366
8367 if (Opc == Instruction::FCmp) {
8368 if (!LHS->getType()->isFPOrFPVectorTy())
8369 return error(Loc, "fcmp requires floating point operands");
8370 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
8371 } else {
8372 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
8373 if (!LHS->getType()->isIntOrIntVectorTy() &&
8375 return error(Loc, "icmp requires integer operands");
8376 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
8377 }
8378 return false;
8379}
8380
8381//===----------------------------------------------------------------------===//
8382// Other Instructions.
8383//===----------------------------------------------------------------------===//
8384
8385/// parseCast
8386/// ::= CastOpc TypeAndValue 'to' Type
8387bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS,
8388 unsigned Opc) {
8389 LocTy Loc;
8390 Value *Op;
8391 Type *DestTy = nullptr;
8392 if (parseTypeAndValue(Op, Loc, PFS) ||
8393 parseToken(lltok::kw_to, "expected 'to' after cast value") ||
8394 parseType(DestTy))
8395 return true;
8396
8398 return error(Loc, "invalid cast opcode for cast from '" +
8399 getTypeString(Op->getType()) + "' to '" +
8400 getTypeString(DestTy) + "'");
8401 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
8402 return false;
8403}
8404
8405/// parseSelect
8406/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8407bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) {
8408 LocTy Loc;
8409 Value *Op0, *Op1, *Op2;
8410 if (parseTypeAndValue(Op0, Loc, PFS) ||
8411 parseToken(lltok::comma, "expected ',' after select condition") ||
8412 parseTypeAndValue(Op1, PFS) ||
8413 parseToken(lltok::comma, "expected ',' after select value") ||
8414 parseTypeAndValue(Op2, PFS))
8415 return true;
8416
8417 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
8418 return error(Loc, Reason);
8419
8420 Inst = SelectInst::Create(Op0, Op1, Op2);
8421 return false;
8422}
8423
8424/// parseVAArg
8425/// ::= 'va_arg' TypeAndValue ',' Type
8426bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) {
8427 Value *Op;
8428 Type *EltTy = nullptr;
8429 LocTy TypeLoc;
8430 if (parseTypeAndValue(Op, PFS) ||
8431 parseToken(lltok::comma, "expected ',' after vaarg operand") ||
8432 parseType(EltTy, TypeLoc))
8433 return true;
8434
8435 if (!EltTy->isFirstClassType())
8436 return error(TypeLoc, "va_arg requires operand with first class type");
8437
8438 Inst = new VAArgInst(Op, EltTy);
8439 return false;
8440}
8441
8442/// parseExtractElement
8443/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
8444bool LLParser::parseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
8445 LocTy Loc;
8446 Value *Op0, *Op1;
8447 if (parseTypeAndValue(Op0, Loc, PFS) ||
8448 parseToken(lltok::comma, "expected ',' after extract value") ||
8449 parseTypeAndValue(Op1, PFS))
8450 return true;
8451
8453 return error(Loc, "invalid extractelement operands");
8454
8455 Inst = ExtractElementInst::Create(Op0, Op1);
8456 return false;
8457}
8458
8459/// parseInsertElement
8460/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8461bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
8462 LocTy Loc;
8463 Value *Op0, *Op1, *Op2;
8464 if (parseTypeAndValue(Op0, Loc, PFS) ||
8465 parseToken(lltok::comma, "expected ',' after insertelement value") ||
8466 parseTypeAndValue(Op1, PFS) ||
8467 parseToken(lltok::comma, "expected ',' after insertelement value") ||
8468 parseTypeAndValue(Op2, PFS))
8469 return true;
8470
8471 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
8472 return error(Loc, "invalid insertelement operands");
8473
8474 Inst = InsertElementInst::Create(Op0, Op1, Op2);
8475 return false;
8476}
8477
8478/// parseShuffleVector
8479/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8480bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
8481 LocTy Loc;
8482 Value *Op0, *Op1, *Op2;
8483 if (parseTypeAndValue(Op0, Loc, PFS) ||
8484 parseToken(lltok::comma, "expected ',' after shuffle mask") ||
8485 parseTypeAndValue(Op1, PFS) ||
8486 parseToken(lltok::comma, "expected ',' after shuffle value") ||
8487 parseTypeAndValue(Op2, PFS))
8488 return true;
8489
8490 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
8491 return error(Loc, "invalid shufflevector operands");
8492
8493 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
8494 return false;
8495}
8496
8497/// parsePHI
8498/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
8499int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) {
8500 Type *Ty = nullptr; LocTy TypeLoc;
8501 Value *Op0, *Op1;
8502
8503 if (parseType(Ty, TypeLoc))
8504 return true;
8505
8506 if (!Ty->isFirstClassType())
8507 return error(TypeLoc, "phi node must have first class type");
8508
8509 bool First = true;
8510 bool AteExtraComma = false;
8512
8513 while (true) {
8514 if (First) {
8515 if (Lex.getKind() != lltok::lsquare)
8516 break;
8517 First = false;
8518 } else if (!EatIfPresent(lltok::comma))
8519 break;
8520
8521 if (Lex.getKind() == lltok::MetadataVar) {
8522 AteExtraComma = true;
8523 break;
8524 }
8525
8526 if (parseToken(lltok::lsquare, "expected '[' in phi value list") ||
8527 parseValue(Ty, Op0, PFS) ||
8528 parseToken(lltok::comma, "expected ',' after insertelement value") ||
8529 parseValue(Type::getLabelTy(Context), Op1, PFS) ||
8530 parseToken(lltok::rsquare, "expected ']' in phi value list"))
8531 return true;
8532
8533 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
8534 }
8535
8536 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
8537 for (const auto &[Val, BB] : PHIVals)
8538 PN->addIncoming(Val, BB);
8539 Inst = PN;
8540 return AteExtraComma ? InstExtraComma : InstNormal;
8541}
8542
8543/// parseLandingPad
8544/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
8545/// Clause
8546/// ::= 'catch' TypeAndValue
8547/// ::= 'filter'
8548/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
8549bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
8550 Type *Ty = nullptr; LocTy TyLoc;
8551
8552 if (parseType(Ty, TyLoc))
8553 return true;
8554
8555 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
8556 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
8557
8558 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
8560 if (EatIfPresent(lltok::kw_catch))
8562 else if (EatIfPresent(lltok::kw_filter))
8564 else
8565 return tokError("expected 'catch' or 'filter' clause type");
8566
8567 Value *V;
8568 LocTy VLoc;
8569 if (parseTypeAndValue(V, VLoc, PFS))
8570 return true;
8571
8572 // A 'catch' type expects a non-array constant. A filter clause expects an
8573 // array constant.
8574 if (CT == LandingPadInst::Catch) {
8575 if (isa<ArrayType>(V->getType()))
8576 return error(VLoc, "'catch' clause has an invalid type");
8577 } else {
8578 if (!isa<ArrayType>(V->getType()))
8579 return error(VLoc, "'filter' clause has an invalid type");
8580 }
8581
8583 if (!CV)
8584 return error(VLoc, "clause argument must be a constant");
8585 LP->addClause(CV);
8586 }
8587
8588 Inst = LP.release();
8589 return false;
8590}
8591
8592/// parseFreeze
8593/// ::= 'freeze' Type Value
8594bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) {
8595 LocTy Loc;
8596 Value *Op;
8597 if (parseTypeAndValue(Op, Loc, PFS))
8598 return true;
8599
8600 Inst = new FreezeInst(Op);
8601 return false;
8602}
8603
8604/// parseCall
8605/// ::= 'call' OptionalFastMathFlags OptionalCallingConv
8606/// OptionalAttrs Type Value ParameterList OptionalAttrs
8607/// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
8608/// OptionalAttrs Type Value ParameterList OptionalAttrs
8609/// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
8610/// OptionalAttrs Type Value ParameterList OptionalAttrs
8611/// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv
8612/// OptionalAttrs Type Value ParameterList OptionalAttrs
8613bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
8615 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8616 std::vector<unsigned> FwdRefAttrGrps;
8617 LocTy BuiltinLoc;
8618 unsigned CallAddrSpace;
8619 unsigned CC;
8620 Type *RetType = nullptr;
8621 LocTy RetTypeLoc;
8622 ValID CalleeID;
8625 LocTy CallLoc = Lex.getLoc();
8626
8627 if (TCK != CallInst::TCK_None &&
8628 parseToken(lltok::kw_call,
8629 "expected 'tail call', 'musttail call', or 'notail call'"))
8630 return true;
8631
8632 FastMathFlags FMF = EatFastMathFlagsIfPresent();
8633
8634 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
8635 parseOptionalProgramAddrSpace(CallAddrSpace) ||
8636 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
8637 parseValID(CalleeID, &PFS) ||
8638 parseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
8639 PFS.getFunction().isVarArg()) ||
8640 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
8641 parseOptionalOperandBundles(BundleList, PFS))
8642 return true;
8643
8644 // If RetType is a non-function pointer type, then this is the short syntax
8645 // for the call, which means that RetType is just the return type. Infer the
8646 // rest of the function argument types from the arguments that are present.
8647 FunctionType *Ty;
8648 if (resolveFunctionType(RetType, ArgList, Ty))
8649 return error(RetTypeLoc, "Invalid result type for LLVM function");
8650
8651 CalleeID.FTy = Ty;
8652
8653 // Look up the callee.
8654 Value *Callee;
8655 if (convertValIDToValue(PointerType::get(Context, CallAddrSpace), CalleeID,
8656 Callee, &PFS))
8657 return true;
8658
8659 // Set up the Attribute for the function.
8661
8662 SmallVector<Value*, 8> Args;
8663
8664 // Loop through FunctionType's arguments and ensure they are specified
8665 // correctly. Also, gather any parameter attributes.
8666 FunctionType::param_iterator I = Ty->param_begin();
8667 FunctionType::param_iterator E = Ty->param_end();
8668 for (const ParamInfo &Arg : ArgList) {
8669 Type *ExpectedTy = nullptr;
8670 if (I != E) {
8671 ExpectedTy = *I++;
8672 } else if (!Ty->isVarArg()) {
8673 return error(Arg.Loc, "too many arguments specified");
8674 }
8675
8676 if (ExpectedTy && ExpectedTy != Arg.V->getType())
8677 return error(Arg.Loc, "argument is not of expected type '" +
8678 getTypeString(ExpectedTy) + "'");
8679 Args.push_back(Arg.V);
8680 Attrs.push_back(Arg.Attrs);
8681 }
8682
8683 if (I != E)
8684 return error(CallLoc, "not enough parameters specified for call");
8685
8686 // Finish off the Attribute and check them
8687 AttributeList PAL =
8688 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
8689 AttributeSet::get(Context, RetAttrs), Attrs);
8690
8691 CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
8692 CI->setTailCallKind(TCK);
8693 CI->setCallingConv(CC);
8694 if (FMF.any()) {
8695 if (!isa<FPMathOperator>(CI)) {
8696 CI->deleteValue();
8697 return error(CallLoc, "fast-math-flags specified for call without "
8698 "floating-point scalar or vector return type");
8699 }
8700 CI->setFastMathFlags(FMF);
8701 }
8702
8703 if (CalleeID.Kind == ValID::t_GlobalName &&
8704 isOldDbgFormatIntrinsic(CalleeID.StrVal)) {
8705 if (SeenNewDbgInfoFormat) {
8706 CI->deleteValue();
8707 return error(CallLoc, "llvm.dbg intrinsic should not appear in a module "
8708 "using non-intrinsic debug info");
8709 }
8710 SeenOldDbgInfoFormat = true;
8711 }
8712 CI->setAttributes(PAL);
8713 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
8714 Inst = CI;
8715 return false;
8716}
8717
8718//===----------------------------------------------------------------------===//
8719// Memory Instructions.
8720//===----------------------------------------------------------------------===//
8721
8722/// parseAlloc
8723/// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
8724/// (',' 'align' i32)? (',', 'addrspace(n))?
8725int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
8726 Value *Size = nullptr;
8727 LocTy SizeLoc, TyLoc, ASLoc;
8728 MaybeAlign Alignment;
8729 unsigned AddrSpace = 0;
8730 Type *Ty = nullptr;
8731
8732 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
8733 bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
8734
8735 if (parseType(Ty, TyLoc))
8736 return true;
8737
8739 return error(TyLoc, "invalid type for alloca");
8740
8741 bool AteExtraComma = false;
8742 if (EatIfPresent(lltok::comma)) {
8743 if (Lex.getKind() == lltok::kw_align) {
8744 if (parseOptionalAlignment(Alignment))
8745 return true;
8746 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
8747 return true;
8748 } else if (Lex.getKind() == lltok::kw_addrspace) {
8749 ASLoc = Lex.getLoc();
8750 if (parseOptionalAddrSpace(AddrSpace))
8751 return true;
8752 } else if (Lex.getKind() == lltok::MetadataVar) {
8753 AteExtraComma = true;
8754 } else {
8755 if (parseTypeAndValue(Size, SizeLoc, PFS))
8756 return true;
8757 if (EatIfPresent(lltok::comma)) {
8758 if (Lex.getKind() == lltok::kw_align) {
8759 if (parseOptionalAlignment(Alignment))
8760 return true;
8761 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
8762 return true;
8763 } else if (Lex.getKind() == lltok::kw_addrspace) {
8764 ASLoc = Lex.getLoc();
8765 if (parseOptionalAddrSpace(AddrSpace))
8766 return true;
8767 } else if (Lex.getKind() == lltok::MetadataVar) {
8768 AteExtraComma = true;
8769 }
8770 }
8771 }
8772 }
8773
8774 if (Size && !Size->getType()->isIntegerTy())
8775 return error(SizeLoc, "element count must have integer type");
8776
8777 SmallPtrSet<Type *, 4> Visited;
8778 if (!Alignment && !Ty->isSized(&Visited))
8779 return error(TyLoc, "Cannot allocate unsized type");
8780 if (!Alignment)
8781 Alignment = M->getDataLayout().getPrefTypeAlign(Ty);
8782 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment);
8783 AI->setUsedWithInAlloca(IsInAlloca);
8784 AI->setSwiftError(IsSwiftError);
8785 Inst = AI;
8786 return AteExtraComma ? InstExtraComma : InstNormal;
8787}
8788
8789/// parseLoad
8790/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
8791/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
8792/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
8793int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) {
8794 Value *Val; LocTy Loc;
8795 MaybeAlign Alignment;
8796 bool AteExtraComma = false;
8797 bool isAtomic = false;
8800
8801 if (Lex.getKind() == lltok::kw_atomic) {
8802 isAtomic = true;
8803 Lex.Lex();
8804 }
8805
8806 bool isVolatile = false;
8807 if (Lex.getKind() == lltok::kw_volatile) {
8808 isVolatile = true;
8809 Lex.Lex();
8810 }
8811
8812 Type *Ty;
8813 LocTy ExplicitTypeLoc = Lex.getLoc();
8814 if (parseType(Ty) ||
8815 parseToken(lltok::comma, "expected comma after load's type") ||
8816 parseTypeAndValue(Val, Loc, PFS) ||
8817 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
8818 parseOptionalCommaAlign(Alignment, AteExtraComma))
8819 return true;
8820
8821 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
8822 return error(Loc, "load operand must be a pointer to a first class type");
8823 if (isAtomic && !Alignment)
8824 return error(Loc, "atomic load must have explicit non-zero alignment");
8825 if (Ordering == AtomicOrdering::Release ||
8827 return error(Loc, "atomic load cannot use Release ordering");
8828
8829 SmallPtrSet<Type *, 4> Visited;
8830 if (!Alignment && !Ty->isSized(&Visited))
8831 return error(ExplicitTypeLoc, "loading unsized types is not allowed");
8832 if (!Alignment)
8833 Alignment = M->getDataLayout().getABITypeAlign(Ty);
8834 Inst = new LoadInst(Ty, Val, "", isVolatile, *Alignment, Ordering, SSID);
8835 return AteExtraComma ? InstExtraComma : InstNormal;
8836}
8837
8838/// parseStore
8839
8840/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
8841/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
8842/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
8843int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) {
8844 Value *Val, *Ptr; LocTy Loc, PtrLoc;
8845 MaybeAlign Alignment;
8846 bool AteExtraComma = false;
8847 bool isAtomic = false;
8850
8851 if (Lex.getKind() == lltok::kw_atomic) {
8852 isAtomic = true;
8853 Lex.Lex();
8854 }
8855
8856 bool isVolatile = false;
8857 if (Lex.getKind() == lltok::kw_volatile) {
8858 isVolatile = true;
8859 Lex.Lex();
8860 }
8861
8862 if (parseTypeAndValue(Val, Loc, PFS) ||
8863 parseToken(lltok::comma, "expected ',' after store operand") ||
8864 parseTypeAndValue(Ptr, PtrLoc, PFS) ||
8865 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
8866 parseOptionalCommaAlign(Alignment, AteExtraComma))
8867 return true;
8868
8869 if (!Ptr->getType()->isPointerTy())
8870 return error(PtrLoc, "store operand must be a pointer");
8871 if (!Val->getType()->isFirstClassType())
8872 return error(Loc, "store operand must be a first class value");
8873 if (isAtomic && !Alignment)
8874 return error(Loc, "atomic store must have explicit non-zero alignment");
8875 if (Ordering == AtomicOrdering::Acquire ||
8877 return error(Loc, "atomic store cannot use Acquire ordering");
8878 SmallPtrSet<Type *, 4> Visited;
8879 if (!Alignment && !Val->getType()->isSized(&Visited))
8880 return error(Loc, "storing unsized types is not allowed");
8881 if (!Alignment)
8882 Alignment = M->getDataLayout().getABITypeAlign(Val->getType());
8883
8884 Inst = new StoreInst(Val, Ptr, isVolatile, *Alignment, Ordering, SSID);
8885 return AteExtraComma ? InstExtraComma : InstNormal;
8886}
8887
8888/// parseCmpXchg
8889/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
8890/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ','
8891/// 'Align'?
8892int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
8893 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
8894 bool AteExtraComma = false;
8895 AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
8896 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
8898 bool isVolatile = false;
8899 bool isWeak = false;
8900 MaybeAlign Alignment;
8901
8902 if (EatIfPresent(lltok::kw_weak))
8903 isWeak = true;
8904
8905 if (EatIfPresent(lltok::kw_volatile))
8906 isVolatile = true;
8907
8908 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
8909 parseToken(lltok::comma, "expected ',' after cmpxchg address") ||
8910 parseTypeAndValue(Cmp, CmpLoc, PFS) ||
8911 parseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
8912 parseTypeAndValue(New, NewLoc, PFS) ||
8913 parseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
8914 parseOrdering(FailureOrdering) ||
8915 parseOptionalCommaAlign(Alignment, AteExtraComma))
8916 return true;
8917
8918 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering))
8919 return tokError("invalid cmpxchg success ordering");
8920 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering))
8921 return tokError("invalid cmpxchg failure ordering");
8922 if (!Ptr->getType()->isPointerTy())
8923 return error(PtrLoc, "cmpxchg operand must be a pointer");
8924 if (Cmp->getType() != New->getType())
8925 return error(NewLoc, "compare value and new value type do not match");
8926 if (!New->getType()->isFirstClassType())
8927 return error(NewLoc, "cmpxchg operand must be a first class value");
8928
8929 const Align DefaultAlignment(
8930 PFS.getFunction().getDataLayout().getTypeStoreSize(
8931 Cmp->getType()));
8932
8933 AtomicCmpXchgInst *CXI =
8934 new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(DefaultAlignment),
8935 SuccessOrdering, FailureOrdering, SSID);
8936 CXI->setVolatile(isVolatile);
8937 CXI->setWeak(isWeak);
8938
8939 Inst = CXI;
8940 return AteExtraComma ? InstExtraComma : InstNormal;
8941}
8942
8943/// parseAtomicRMW
8944/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
8945/// 'singlethread'? AtomicOrdering
8946int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
8947 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
8948 bool AteExtraComma = false;
8951 bool isVolatile = false;
8952 bool IsFP = false;
8954 MaybeAlign Alignment;
8955
8956 if (EatIfPresent(lltok::kw_volatile))
8957 isVolatile = true;
8958
8959 switch (Lex.getKind()) {
8960 default:
8961 return tokError("expected binary operation in atomicrmw");
8975 break;
8978 break;
8981 break;
8982 case lltok::kw_usub_sat:
8984 break;
8985 case lltok::kw_fadd:
8987 IsFP = true;
8988 break;
8989 case lltok::kw_fsub:
8991 IsFP = true;
8992 break;
8993 case lltok::kw_fmax:
8995 IsFP = true;
8996 break;
8997 case lltok::kw_fmin:
8999 IsFP = true;
9000 break;
9001 case lltok::kw_fmaximum:
9003 IsFP = true;
9004 break;
9005 case lltok::kw_fminimum:
9007 IsFP = true;
9008 break;
9009 }
9010 Lex.Lex(); // Eat the operation.
9011
9012 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
9013 parseToken(lltok::comma, "expected ',' after atomicrmw address") ||
9014 parseTypeAndValue(Val, ValLoc, PFS) ||
9015 parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering) ||
9016 parseOptionalCommaAlign(Alignment, AteExtraComma))
9017 return true;
9018
9019 if (Ordering == AtomicOrdering::Unordered)
9020 return tokError("atomicrmw cannot be unordered");
9021 if (!Ptr->getType()->isPointerTy())
9022 return error(PtrLoc, "atomicrmw operand must be a pointer");
9023 if (Val->getType()->isScalableTy())
9024 return error(ValLoc, "atomicrmw operand may not be scalable");
9025
9027 if (!Val->getType()->isIntegerTy() &&
9028 !Val->getType()->isFloatingPointTy() &&
9029 !Val->getType()->isPointerTy()) {
9030 return error(
9031 ValLoc,
9033 " operand must be an integer, floating point, or pointer type");
9034 }
9035 } else if (IsFP) {
9036 if (!Val->getType()->isFPOrFPVectorTy()) {
9037 return error(ValLoc, "atomicrmw " +
9039 " operand must be a floating point type");
9040 }
9041 } else {
9042 if (!Val->getType()->isIntegerTy()) {
9043 return error(ValLoc, "atomicrmw " +
9045 " operand must be an integer");
9046 }
9047 }
9048
9049 unsigned Size =
9050 PFS.getFunction().getDataLayout().getTypeStoreSizeInBits(
9051 Val->getType());
9052 if (Size < 8 || (Size & (Size - 1)))
9053 return error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
9054 " integer");
9055 const Align DefaultAlignment(
9056 PFS.getFunction().getDataLayout().getTypeStoreSize(
9057 Val->getType()));
9058 AtomicRMWInst *RMWI =
9059 new AtomicRMWInst(Operation, Ptr, Val,
9060 Alignment.value_or(DefaultAlignment), Ordering, SSID);
9061 RMWI->setVolatile(isVolatile);
9062 Inst = RMWI;
9063 return AteExtraComma ? InstExtraComma : InstNormal;
9064}
9065
9066/// parseFence
9067/// ::= 'fence' 'singlethread'? AtomicOrdering
9068int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) {
9071 if (parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
9072 return true;
9073
9074 if (Ordering == AtomicOrdering::Unordered)
9075 return tokError("fence cannot be unordered");
9076 if (Ordering == AtomicOrdering::Monotonic)
9077 return tokError("fence cannot be monotonic");
9078
9079 Inst = new FenceInst(Context, Ordering, SSID);
9080 return InstNormal;
9081}
9082
9083/// parseGetElementPtr
9084/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
9085int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
9086 Value *Ptr = nullptr;
9087 Value *Val = nullptr;
9088 LocTy Loc, EltLoc;
9089 GEPNoWrapFlags NW;
9090
9091 while (true) {
9092 if (EatIfPresent(lltok::kw_inbounds))
9094 else if (EatIfPresent(lltok::kw_nusw))
9096 else if (EatIfPresent(lltok::kw_nuw))
9098 else
9099 break;
9100 }
9101
9102 Type *Ty = nullptr;
9103 if (parseType(Ty) ||
9104 parseToken(lltok::comma, "expected comma after getelementptr's type") ||
9105 parseTypeAndValue(Ptr, Loc, PFS))
9106 return true;
9107
9108 Type *BaseType = Ptr->getType();
9109 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
9110 if (!BasePointerType)
9111 return error(Loc, "base of getelementptr must be a pointer");
9112
9113 SmallVector<Value*, 16> Indices;
9114 bool AteExtraComma = false;
9115 // GEP returns a vector of pointers if at least one of parameters is a vector.
9116 // All vector parameters should have the same vector width.
9117 ElementCount GEPWidth = BaseType->isVectorTy()
9118 ? cast<VectorType>(BaseType)->getElementCount()
9120
9121 while (EatIfPresent(lltok::comma)) {
9122 if (Lex.getKind() == lltok::MetadataVar) {
9123 AteExtraComma = true;
9124 break;
9125 }
9126 if (parseTypeAndValue(Val, EltLoc, PFS))
9127 return true;
9128 if (!Val->getType()->isIntOrIntVectorTy())
9129 return error(EltLoc, "getelementptr index must be an integer");
9130
9131 if (auto *ValVTy = dyn_cast<VectorType>(Val->getType())) {
9132 ElementCount ValNumEl = ValVTy->getElementCount();
9133 if (GEPWidth != ElementCount::getFixed(0) && GEPWidth != ValNumEl)
9134 return error(
9135 EltLoc,
9136 "getelementptr vector index has a wrong number of elements");
9137 GEPWidth = ValNumEl;
9138 }
9139 Indices.push_back(Val);
9140 }
9141
9142 SmallPtrSet<Type*, 4> Visited;
9143 if (!Indices.empty() && !Ty->isSized(&Visited))
9144 return error(Loc, "base element of getelementptr must be sized");
9145
9146 auto *STy = dyn_cast<StructType>(Ty);
9147 if (STy && STy->isScalableTy())
9148 return error(Loc, "getelementptr cannot target structure that contains "
9149 "scalable vector type");
9150
9151 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
9152 return error(Loc, "invalid getelementptr indices");
9153 GetElementPtrInst *GEP = GetElementPtrInst::Create(Ty, Ptr, Indices);
9154 Inst = GEP;
9155 GEP->setNoWrapFlags(NW);
9156 return AteExtraComma ? InstExtraComma : InstNormal;
9157}
9158
9159/// parseExtractValue
9160/// ::= 'extractvalue' TypeAndValue (',' uint32)+
9161int LLParser::parseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
9162 Value *Val; LocTy Loc;
9163 SmallVector<unsigned, 4> Indices;
9164 bool AteExtraComma;
9165 if (parseTypeAndValue(Val, Loc, PFS) ||
9166 parseIndexList(Indices, AteExtraComma))
9167 return true;
9168
9169 if (!Val->getType()->isAggregateType())
9170 return error(Loc, "extractvalue operand must be aggregate type");
9171
9172 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
9173 return error(Loc, "invalid indices for extractvalue");
9174 Inst = ExtractValueInst::Create(Val, Indices);
9175 return AteExtraComma ? InstExtraComma : InstNormal;
9176}
9177
9178/// parseInsertValue
9179/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
9180int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
9181 Value *Val0, *Val1; LocTy Loc0, Loc1;
9182 SmallVector<unsigned, 4> Indices;
9183 bool AteExtraComma;
9184 if (parseTypeAndValue(Val0, Loc0, PFS) ||
9185 parseToken(lltok::comma, "expected comma after insertvalue operand") ||
9186 parseTypeAndValue(Val1, Loc1, PFS) ||
9187 parseIndexList(Indices, AteExtraComma))
9188 return true;
9189
9190 if (!Val0->getType()->isAggregateType())
9191 return error(Loc0, "insertvalue operand must be aggregate type");
9192
9193 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
9194 if (!IndexedType)
9195 return error(Loc0, "invalid indices for insertvalue");
9196 if (IndexedType != Val1->getType())
9197 return error(Loc1, "insertvalue operand and field disagree in type: '" +
9198 getTypeString(Val1->getType()) + "' instead of '" +
9199 getTypeString(IndexedType) + "'");
9200 Inst = InsertValueInst::Create(Val0, Val1, Indices);
9201 return AteExtraComma ? InstExtraComma : InstNormal;
9202}
9203
9204//===----------------------------------------------------------------------===//
9205// Embedded metadata.
9206//===----------------------------------------------------------------------===//
9207
9208/// parseMDNodeVector
9209/// ::= { Element (',' Element)* }
9210/// Element
9211/// ::= 'null' | Metadata
9212bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
9213 if (parseToken(lltok::lbrace, "expected '{' here"))
9214 return true;
9215
9216 // Check for an empty list.
9217 if (EatIfPresent(lltok::rbrace))
9218 return false;
9219
9220 do {
9221 if (EatIfPresent(lltok::kw_null)) {
9222 Elts.push_back(nullptr);
9223 continue;
9224 }
9225
9226 Metadata *MD;
9227 if (parseMetadata(MD, nullptr))
9228 return true;
9229 Elts.push_back(MD);
9230 } while (EatIfPresent(lltok::comma));
9231
9232 return parseToken(lltok::rbrace, "expected end of metadata node");
9233}
9234
9235//===----------------------------------------------------------------------===//
9236// Use-list order directives.
9237//===----------------------------------------------------------------------===//
9238bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
9239 SMLoc Loc) {
9240 if (!V->hasUseList())
9241 return false;
9242 if (V->use_empty())
9243 return error(Loc, "value has no uses");
9244
9245 unsigned NumUses = 0;
9246 SmallDenseMap<const Use *, unsigned, 16> Order;
9247 for (const Use &U : V->uses()) {
9248 if (++NumUses > Indexes.size())
9249 break;
9250 Order[&U] = Indexes[NumUses - 1];
9251 }
9252 if (NumUses < 2)
9253 return error(Loc, "value only has one use");
9254 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
9255 return error(Loc,
9256 "wrong number of indexes, expected " + Twine(V->getNumUses()));
9257
9258 V->sortUseList([&](const Use &L, const Use &R) {
9259 return Order.lookup(&L) < Order.lookup(&R);
9260 });
9261 return false;
9262}
9263
9264/// parseUseListOrderIndexes
9265/// ::= '{' uint32 (',' uint32)+ '}'
9266bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
9267 SMLoc Loc = Lex.getLoc();
9268 if (parseToken(lltok::lbrace, "expected '{' here"))
9269 return true;
9270 if (Lex.getKind() == lltok::rbrace)
9271 return tokError("expected non-empty list of uselistorder indexes");
9272
9273 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
9274 // indexes should be distinct numbers in the range [0, size-1], and should
9275 // not be in order.
9276 unsigned Offset = 0;
9277 unsigned Max = 0;
9278 bool IsOrdered = true;
9279 assert(Indexes.empty() && "Expected empty order vector");
9280 do {
9281 unsigned Index;
9282 if (parseUInt32(Index))
9283 return true;
9284
9285 // Update consistency checks.
9286 Offset += Index - Indexes.size();
9287 Max = std::max(Max, Index);
9288 IsOrdered &= Index == Indexes.size();
9289
9290 Indexes.push_back(Index);
9291 } while (EatIfPresent(lltok::comma));
9292
9293 if (parseToken(lltok::rbrace, "expected '}' here"))
9294 return true;
9295
9296 if (Indexes.size() < 2)
9297 return error(Loc, "expected >= 2 uselistorder indexes");
9298 if (Offset != 0 || Max >= Indexes.size())
9299 return error(Loc,
9300 "expected distinct uselistorder indexes in range [0, size)");
9301 if (IsOrdered)
9302 return error(Loc, "expected uselistorder indexes to change the order");
9303
9304 return false;
9305}
9306
9307/// parseUseListOrder
9308/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
9309bool LLParser::parseUseListOrder(PerFunctionState *PFS) {
9310 SMLoc Loc = Lex.getLoc();
9311 if (parseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
9312 return true;
9313
9314 Value *V;
9315 SmallVector<unsigned, 16> Indexes;
9316 if (parseTypeAndValue(V, PFS) ||
9317 parseToken(lltok::comma, "expected comma in uselistorder directive") ||
9318 parseUseListOrderIndexes(Indexes))
9319 return true;
9320
9321 return sortUseListOrder(V, Indexes, Loc);
9322}
9323
9324/// parseUseListOrderBB
9325/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
9326bool LLParser::parseUseListOrderBB() {
9327 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
9328 SMLoc Loc = Lex.getLoc();
9329 Lex.Lex();
9330
9331 ValID Fn, Label;
9332 SmallVector<unsigned, 16> Indexes;
9333 if (parseValID(Fn, /*PFS=*/nullptr) ||
9334 parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
9335 parseValID(Label, /*PFS=*/nullptr) ||
9336 parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
9337 parseUseListOrderIndexes(Indexes))
9338 return true;
9339
9340 // Check the function.
9341 GlobalValue *GV;
9342 if (Fn.Kind == ValID::t_GlobalName)
9343 GV = M->getNamedValue(Fn.StrVal);
9344 else if (Fn.Kind == ValID::t_GlobalID)
9345 GV = NumberedVals.get(Fn.UIntVal);
9346 else
9347 return error(Fn.Loc, "expected function name in uselistorder_bb");
9348 if (!GV)
9349 return error(Fn.Loc,
9350 "invalid function forward reference in uselistorder_bb");
9351 auto *F = dyn_cast<Function>(GV);
9352 if (!F)
9353 return error(Fn.Loc, "expected function name in uselistorder_bb");
9354 if (F->isDeclaration())
9355 return error(Fn.Loc, "invalid declaration in uselistorder_bb");
9356
9357 // Check the basic block.
9358 if (Label.Kind == ValID::t_LocalID)
9359 return error(Label.Loc, "invalid numeric label in uselistorder_bb");
9360 if (Label.Kind != ValID::t_LocalName)
9361 return error(Label.Loc, "expected basic block name in uselistorder_bb");
9362 Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
9363 if (!V)
9364 return error(Label.Loc, "invalid basic block in uselistorder_bb");
9365 if (!isa<BasicBlock>(V))
9366 return error(Label.Loc, "expected basic block in uselistorder_bb");
9367
9368 return sortUseListOrder(V, Indexes, Loc);
9369}
9370
9371/// ModuleEntry
9372/// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
9373/// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
9374bool LLParser::parseModuleEntry(unsigned ID) {
9375 assert(Lex.getKind() == lltok::kw_module);
9376 Lex.Lex();
9377
9378 std::string Path;
9379 if (parseToken(lltok::colon, "expected ':' here") ||
9380 parseToken(lltok::lparen, "expected '(' here") ||
9381 parseToken(lltok::kw_path, "expected 'path' here") ||
9382 parseToken(lltok::colon, "expected ':' here") ||
9383 parseStringConstant(Path) ||
9384 parseToken(lltok::comma, "expected ',' here") ||
9385 parseToken(lltok::kw_hash, "expected 'hash' here") ||
9386 parseToken(lltok::colon, "expected ':' here") ||
9387 parseToken(lltok::lparen, "expected '(' here"))
9388 return true;
9389
9390 ModuleHash Hash;
9391 if (parseUInt32(Hash[0]) || parseToken(lltok::comma, "expected ',' here") ||
9392 parseUInt32(Hash[1]) || parseToken(lltok::comma, "expected ',' here") ||
9393 parseUInt32(Hash[2]) || parseToken(lltok::comma, "expected ',' here") ||
9394 parseUInt32(Hash[3]) || parseToken(lltok::comma, "expected ',' here") ||
9395 parseUInt32(Hash[4]))
9396 return true;
9397
9398 if (parseToken(lltok::rparen, "expected ')' here") ||
9399 parseToken(lltok::rparen, "expected ')' here"))
9400 return true;
9401
9402 auto ModuleEntry = Index->addModule(Path, Hash);
9403 ModuleIdMap[ID] = ModuleEntry->first();
9404
9405 return false;
9406}
9407
9408/// TypeIdEntry
9409/// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
9410bool LLParser::parseTypeIdEntry(unsigned ID) {
9411 assert(Lex.getKind() == lltok::kw_typeid);
9412 Lex.Lex();
9413
9414 std::string Name;
9415 if (parseToken(lltok::colon, "expected ':' here") ||
9416 parseToken(lltok::lparen, "expected '(' here") ||
9417 parseToken(lltok::kw_name, "expected 'name' here") ||
9418 parseToken(lltok::colon, "expected ':' here") ||
9419 parseStringConstant(Name))
9420 return true;
9421
9422 TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(Name);
9423 if (parseToken(lltok::comma, "expected ',' here") ||
9424 parseTypeIdSummary(TIS) || parseToken(lltok::rparen, "expected ')' here"))
9425 return true;
9426
9427 // Check if this ID was forward referenced, and if so, update the
9428 // corresponding GUIDs.
9429 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
9430 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
9431 for (auto TIDRef : FwdRefTIDs->second) {
9432 assert(!*TIDRef.first &&
9433 "Forward referenced type id GUID expected to be 0");
9434 *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
9435 }
9436 ForwardRefTypeIds.erase(FwdRefTIDs);
9437 }
9438
9439 return false;
9440}
9441
9442/// TypeIdSummary
9443/// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
9444bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) {
9445 if (parseToken(lltok::kw_summary, "expected 'summary' here") ||
9446 parseToken(lltok::colon, "expected ':' here") ||
9447 parseToken(lltok::lparen, "expected '(' here") ||
9448 parseTypeTestResolution(TIS.TTRes))
9449 return true;
9450
9451 if (EatIfPresent(lltok::comma)) {
9452 // Expect optional wpdResolutions field
9453 if (parseOptionalWpdResolutions(TIS.WPDRes))
9454 return true;
9455 }
9456
9457 if (parseToken(lltok::rparen, "expected ')' here"))
9458 return true;
9459
9460 return false;
9461}
9462
9464 ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
9465
9466/// TypeIdCompatibleVtableEntry
9467/// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
9468/// TypeIdCompatibleVtableInfo
9469/// ')'
9470bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
9472 Lex.Lex();
9473
9474 std::string Name;
9475 if (parseToken(lltok::colon, "expected ':' here") ||
9476 parseToken(lltok::lparen, "expected '(' here") ||
9477 parseToken(lltok::kw_name, "expected 'name' here") ||
9478 parseToken(lltok::colon, "expected ':' here") ||
9479 parseStringConstant(Name))
9480 return true;
9481
9483 Index->getOrInsertTypeIdCompatibleVtableSummary(Name);
9484 if (parseToken(lltok::comma, "expected ',' here") ||
9485 parseToken(lltok::kw_summary, "expected 'summary' here") ||
9486 parseToken(lltok::colon, "expected ':' here") ||
9487 parseToken(lltok::lparen, "expected '(' here"))
9488 return true;
9489
9490 IdToIndexMapType IdToIndexMap;
9491 // parse each call edge
9492 do {
9494 if (parseToken(lltok::lparen, "expected '(' here") ||
9495 parseToken(lltok::kw_offset, "expected 'offset' here") ||
9496 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
9497 parseToken(lltok::comma, "expected ',' here"))
9498 return true;
9499
9500 LocTy Loc = Lex.getLoc();
9501 unsigned GVId;
9502 ValueInfo VI;
9503 if (parseGVReference(VI, GVId))
9504 return true;
9505
9506 // Keep track of the TypeIdCompatibleVtableInfo array index needing a
9507 // forward reference. We will save the location of the ValueInfo needing an
9508 // update, but can only do so once the std::vector is finalized.
9509 if (VI == EmptyVI)
9510 IdToIndexMap[GVId].push_back(std::make_pair(TI.size(), Loc));
9511 TI.push_back({Offset, VI});
9512
9513 if (parseToken(lltok::rparen, "expected ')' in call"))
9514 return true;
9515 } while (EatIfPresent(lltok::comma));
9516
9517 // Now that the TI vector is finalized, it is safe to save the locations
9518 // of any forward GV references that need updating later.
9519 for (auto I : IdToIndexMap) {
9520 auto &Infos = ForwardRefValueInfos[I.first];
9521 for (auto P : I.second) {
9522 assert(TI[P.first].VTableVI == EmptyVI &&
9523 "Forward referenced ValueInfo expected to be empty");
9524 Infos.emplace_back(&TI[P.first].VTableVI, P.second);
9525 }
9526 }
9527
9528 if (parseToken(lltok::rparen, "expected ')' here") ||
9529 parseToken(lltok::rparen, "expected ')' here"))
9530 return true;
9531
9532 // Check if this ID was forward referenced, and if so, update the
9533 // corresponding GUIDs.
9534 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
9535 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
9536 for (auto TIDRef : FwdRefTIDs->second) {
9537 assert(!*TIDRef.first &&
9538 "Forward referenced type id GUID expected to be 0");
9539 *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(Name);
9540 }
9541 ForwardRefTypeIds.erase(FwdRefTIDs);
9542 }
9543
9544 return false;
9545}
9546
9547/// TypeTestResolution
9548/// ::= 'typeTestRes' ':' '(' 'kind' ':'
9549/// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
9550/// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
9551/// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
9552/// [',' 'inlinesBits' ':' UInt64]? ')'
9553bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) {
9554 if (parseToken(lltok::kw_typeTestRes, "expected 'typeTestRes' here") ||
9555 parseToken(lltok::colon, "expected ':' here") ||
9556 parseToken(lltok::lparen, "expected '(' here") ||
9557 parseToken(lltok::kw_kind, "expected 'kind' here") ||
9558 parseToken(lltok::colon, "expected ':' here"))
9559 return true;
9560
9561 switch (Lex.getKind()) {
9562 case lltok::kw_unknown:
9564 break;
9565 case lltok::kw_unsat:
9567 break;
9570 break;
9571 case lltok::kw_inline:
9573 break;
9574 case lltok::kw_single:
9576 break;
9577 case lltok::kw_allOnes:
9579 break;
9580 default:
9581 return error(Lex.getLoc(), "unexpected TypeTestResolution kind");
9582 }
9583 Lex.Lex();
9584
9585 if (parseToken(lltok::comma, "expected ',' here") ||
9586 parseToken(lltok::kw_sizeM1BitWidth, "expected 'sizeM1BitWidth' here") ||
9587 parseToken(lltok::colon, "expected ':' here") ||
9588 parseUInt32(TTRes.SizeM1BitWidth))
9589 return true;
9590
9591 // parse optional fields
9592 while (EatIfPresent(lltok::comma)) {
9593 switch (Lex.getKind()) {
9595 Lex.Lex();
9596 if (parseToken(lltok::colon, "expected ':'") ||
9597 parseUInt64(TTRes.AlignLog2))
9598 return true;
9599 break;
9600 case lltok::kw_sizeM1:
9601 Lex.Lex();
9602 if (parseToken(lltok::colon, "expected ':'") || parseUInt64(TTRes.SizeM1))
9603 return true;
9604 break;
9605 case lltok::kw_bitMask: {
9606 unsigned Val;
9607 Lex.Lex();
9608 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(Val))
9609 return true;
9610 assert(Val <= 0xff);
9611 TTRes.BitMask = (uint8_t)Val;
9612 break;
9613 }
9615 Lex.Lex();
9616 if (parseToken(lltok::colon, "expected ':'") ||
9617 parseUInt64(TTRes.InlineBits))
9618 return true;
9619 break;
9620 default:
9621 return error(Lex.getLoc(), "expected optional TypeTestResolution field");
9622 }
9623 }
9624
9625 if (parseToken(lltok::rparen, "expected ')' here"))
9626 return true;
9627
9628 return false;
9629}
9630
9631/// OptionalWpdResolutions
9632/// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
9633/// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
9634bool LLParser::parseOptionalWpdResolutions(
9635 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
9636 if (parseToken(lltok::kw_wpdResolutions, "expected 'wpdResolutions' here") ||
9637 parseToken(lltok::colon, "expected ':' here") ||
9638 parseToken(lltok::lparen, "expected '(' here"))
9639 return true;
9640
9641 do {
9642 uint64_t Offset;
9643 WholeProgramDevirtResolution WPDRes;
9644 if (parseToken(lltok::lparen, "expected '(' here") ||
9645 parseToken(lltok::kw_offset, "expected 'offset' here") ||
9646 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
9647 parseToken(lltok::comma, "expected ',' here") || parseWpdRes(WPDRes) ||
9648 parseToken(lltok::rparen, "expected ')' here"))
9649 return true;
9650 WPDResMap[Offset] = WPDRes;
9651 } while (EatIfPresent(lltok::comma));
9652
9653 if (parseToken(lltok::rparen, "expected ')' here"))
9654 return true;
9655
9656 return false;
9657}
9658
9659/// WpdRes
9660/// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
9661/// [',' OptionalResByArg]? ')'
9662/// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
9663/// ',' 'singleImplName' ':' STRINGCONSTANT ','
9664/// [',' OptionalResByArg]? ')'
9665/// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
9666/// [',' OptionalResByArg]? ')'
9667bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) {
9668 if (parseToken(lltok::kw_wpdRes, "expected 'wpdRes' here") ||
9669 parseToken(lltok::colon, "expected ':' here") ||
9670 parseToken(lltok::lparen, "expected '(' here") ||
9671 parseToken(lltok::kw_kind, "expected 'kind' here") ||
9672 parseToken(lltok::colon, "expected ':' here"))
9673 return true;
9674
9675 switch (Lex.getKind()) {
9676 case lltok::kw_indir:
9678 break;
9681 break;
9684 break;
9685 default:
9686 return error(Lex.getLoc(), "unexpected WholeProgramDevirtResolution kind");
9687 }
9688 Lex.Lex();
9689
9690 // parse optional fields
9691 while (EatIfPresent(lltok::comma)) {
9692 switch (Lex.getKind()) {
9694 Lex.Lex();
9695 if (parseToken(lltok::colon, "expected ':' here") ||
9696 parseStringConstant(WPDRes.SingleImplName))
9697 return true;
9698 break;
9699 case lltok::kw_resByArg:
9700 if (parseOptionalResByArg(WPDRes.ResByArg))
9701 return true;
9702 break;
9703 default:
9704 return error(Lex.getLoc(),
9705 "expected optional WholeProgramDevirtResolution field");
9706 }
9707 }
9708
9709 if (parseToken(lltok::rparen, "expected ')' here"))
9710 return true;
9711
9712 return false;
9713}
9714
9715/// OptionalResByArg
9716/// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
9717/// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
9718/// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
9719/// 'virtualConstProp' )
9720/// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
9721/// [',' 'bit' ':' UInt32]? ')'
9722bool LLParser::parseOptionalResByArg(
9723 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
9724 &ResByArg) {
9725 if (parseToken(lltok::kw_resByArg, "expected 'resByArg' here") ||
9726 parseToken(lltok::colon, "expected ':' here") ||
9727 parseToken(lltok::lparen, "expected '(' here"))
9728 return true;
9729
9730 do {
9731 std::vector<uint64_t> Args;
9732 if (parseArgs(Args) || parseToken(lltok::comma, "expected ',' here") ||
9733 parseToken(lltok::kw_byArg, "expected 'byArg here") ||
9734 parseToken(lltok::colon, "expected ':' here") ||
9735 parseToken(lltok::lparen, "expected '(' here") ||
9736 parseToken(lltok::kw_kind, "expected 'kind' here") ||
9737 parseToken(lltok::colon, "expected ':' here"))
9738 return true;
9739
9740 WholeProgramDevirtResolution::ByArg ByArg;
9741 switch (Lex.getKind()) {
9742 case lltok::kw_indir:
9744 break;
9747 break;
9750 break;
9753 break;
9754 default:
9755 return error(Lex.getLoc(),
9756 "unexpected WholeProgramDevirtResolution::ByArg kind");
9757 }
9758 Lex.Lex();
9759
9760 // parse optional fields
9761 while (EatIfPresent(lltok::comma)) {
9762 switch (Lex.getKind()) {
9763 case lltok::kw_info:
9764 Lex.Lex();
9765 if (parseToken(lltok::colon, "expected ':' here") ||
9766 parseUInt64(ByArg.Info))
9767 return true;
9768 break;
9769 case lltok::kw_byte:
9770 Lex.Lex();
9771 if (parseToken(lltok::colon, "expected ':' here") ||
9772 parseUInt32(ByArg.Byte))
9773 return true;
9774 break;
9775 case lltok::kw_bit:
9776 Lex.Lex();
9777 if (parseToken(lltok::colon, "expected ':' here") ||
9778 parseUInt32(ByArg.Bit))
9779 return true;
9780 break;
9781 default:
9782 return error(Lex.getLoc(),
9783 "expected optional whole program devirt field");
9784 }
9785 }
9786
9787 if (parseToken(lltok::rparen, "expected ')' here"))
9788 return true;
9789
9790 ResByArg[Args] = ByArg;
9791 } while (EatIfPresent(lltok::comma));
9792
9793 if (parseToken(lltok::rparen, "expected ')' here"))
9794 return true;
9795
9796 return false;
9797}
9798
9799/// OptionalResByArg
9800/// ::= 'args' ':' '(' UInt64[, UInt64]* ')'
9801bool LLParser::parseArgs(std::vector<uint64_t> &Args) {
9802 if (parseToken(lltok::kw_args, "expected 'args' here") ||
9803 parseToken(lltok::colon, "expected ':' here") ||
9804 parseToken(lltok::lparen, "expected '(' here"))
9805 return true;
9806
9807 do {
9808 uint64_t Val;
9809 if (parseUInt64(Val))
9810 return true;
9811 Args.push_back(Val);
9812 } while (EatIfPresent(lltok::comma));
9813
9814 if (parseToken(lltok::rparen, "expected ')' here"))
9815 return true;
9816
9817 return false;
9818}
9819
9820static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8;
9821
9822static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
9823 bool ReadOnly = Fwd->isReadOnly();
9824 bool WriteOnly = Fwd->isWriteOnly();
9825 assert(!(ReadOnly && WriteOnly));
9826 *Fwd = Resolved;
9827 if (ReadOnly)
9828 Fwd->setReadOnly();
9829 if (WriteOnly)
9830 Fwd->setWriteOnly();
9831}
9832
9833/// Stores the given Name/GUID and associated summary into the Index.
9834/// Also updates any forward references to the associated entry ID.
9835bool LLParser::addGlobalValueToIndex(
9836 std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
9837 unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) {
9838 // First create the ValueInfo utilizing the Name or GUID.
9839 ValueInfo VI;
9840 if (GUID != 0) {
9841 assert(Name.empty());
9842 VI = Index->getOrInsertValueInfo(GUID);
9843 } else {
9844 assert(!Name.empty());
9845 if (M) {
9846 auto *GV = M->getNamedValue(Name);
9847 if (!GV)
9848 return error(Loc, "Reference to undefined global \"" + Name + "\"");
9849
9850 VI = Index->getOrInsertValueInfo(GV);
9851 } else {
9852 assert(
9853 (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
9854 "Need a source_filename to compute GUID for local");
9856 GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
9857 VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
9858 }
9859 }
9860
9861 // Resolve forward references from calls/refs
9862 auto FwdRefVIs = ForwardRefValueInfos.find(ID);
9863 if (FwdRefVIs != ForwardRefValueInfos.end()) {
9864 for (auto VIRef : FwdRefVIs->second) {
9865 assert(VIRef.first->getRef() == FwdVIRef &&
9866 "Forward referenced ValueInfo expected to be empty");
9867 resolveFwdRef(VIRef.first, VI);
9868 }
9869 ForwardRefValueInfos.erase(FwdRefVIs);
9870 }
9871
9872 // Resolve forward references from aliases
9873 auto FwdRefAliasees = ForwardRefAliasees.find(ID);
9874 if (FwdRefAliasees != ForwardRefAliasees.end()) {
9875 for (auto AliaseeRef : FwdRefAliasees->second) {
9876 assert(!AliaseeRef.first->hasAliasee() &&
9877 "Forward referencing alias already has aliasee");
9878 assert(Summary && "Aliasee must be a definition");
9879 AliaseeRef.first->setAliasee(VI, Summary.get());
9880 }
9881 ForwardRefAliasees.erase(FwdRefAliasees);
9882 }
9883
9884 // Add the summary if one was provided.
9885 if (Summary)
9886 Index->addGlobalValueSummary(VI, std::move(Summary));
9887
9888 // Save the associated ValueInfo for use in later references by ID.
9889 if (ID == NumberedValueInfos.size())
9890 NumberedValueInfos.push_back(VI);
9891 else {
9892 // Handle non-continuous numbers (to make test simplification easier).
9893 if (ID > NumberedValueInfos.size())
9894 NumberedValueInfos.resize(ID + 1);
9895 NumberedValueInfos[ID] = VI;
9896 }
9897
9898 return false;
9899}
9900
9901/// parseSummaryIndexFlags
9902/// ::= 'flags' ':' UInt64
9903bool LLParser::parseSummaryIndexFlags() {
9904 assert(Lex.getKind() == lltok::kw_flags);
9905 Lex.Lex();
9906
9907 if (parseToken(lltok::colon, "expected ':' here"))
9908 return true;
9909 uint64_t Flags;
9910 if (parseUInt64(Flags))
9911 return true;
9912 if (Index)
9913 Index->setFlags(Flags);
9914 return false;
9915}
9916
9917/// parseBlockCount
9918/// ::= 'blockcount' ':' UInt64
9919bool LLParser::parseBlockCount() {
9920 assert(Lex.getKind() == lltok::kw_blockcount);
9921 Lex.Lex();
9922
9923 if (parseToken(lltok::colon, "expected ':' here"))
9924 return true;
9925 uint64_t BlockCount;
9926 if (parseUInt64(BlockCount))
9927 return true;
9928 if (Index)
9929 Index->setBlockCount(BlockCount);
9930 return false;
9931}
9932
9933/// parseGVEntry
9934/// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
9935/// [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
9936/// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
9937bool LLParser::parseGVEntry(unsigned ID) {
9938 assert(Lex.getKind() == lltok::kw_gv);
9939 Lex.Lex();
9940
9941 if (parseToken(lltok::colon, "expected ':' here") ||
9942 parseToken(lltok::lparen, "expected '(' here"))
9943 return true;
9944
9945 LocTy Loc = Lex.getLoc();
9946 std::string Name;
9948 switch (Lex.getKind()) {
9949 case lltok::kw_name:
9950 Lex.Lex();
9951 if (parseToken(lltok::colon, "expected ':' here") ||
9952 parseStringConstant(Name))
9953 return true;
9954 // Can't create GUID/ValueInfo until we have the linkage.
9955 break;
9956 case lltok::kw_guid:
9957 Lex.Lex();
9958 if (parseToken(lltok::colon, "expected ':' here") || parseUInt64(GUID))
9959 return true;
9960 break;
9961 default:
9962 return error(Lex.getLoc(), "expected name or guid tag");
9963 }
9964
9965 if (!EatIfPresent(lltok::comma)) {
9966 // No summaries. Wrap up.
9967 if (parseToken(lltok::rparen, "expected ')' here"))
9968 return true;
9969 // This was created for a call to an external or indirect target.
9970 // A GUID with no summary came from a VALUE_GUID record, dummy GUID
9971 // created for indirect calls with VP. A Name with no GUID came from
9972 // an external definition. We pass ExternalLinkage since that is only
9973 // used when the GUID must be computed from Name, and in that case
9974 // the symbol must have external linkage.
9975 return addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
9976 nullptr, Loc);
9977 }
9978
9979 // Have a list of summaries
9980 if (parseToken(lltok::kw_summaries, "expected 'summaries' here") ||
9981 parseToken(lltok::colon, "expected ':' here") ||
9982 parseToken(lltok::lparen, "expected '(' here"))
9983 return true;
9984 do {
9985 switch (Lex.getKind()) {
9986 case lltok::kw_function:
9987 if (parseFunctionSummary(Name, GUID, ID))
9988 return true;
9989 break;
9990 case lltok::kw_variable:
9991 if (parseVariableSummary(Name, GUID, ID))
9992 return true;
9993 break;
9994 case lltok::kw_alias:
9995 if (parseAliasSummary(Name, GUID, ID))
9996 return true;
9997 break;
9998 default:
9999 return error(Lex.getLoc(), "expected summary type");
10000 }
10001 } while (EatIfPresent(lltok::comma));
10002
10003 if (parseToken(lltok::rparen, "expected ')' here") ||
10004 parseToken(lltok::rparen, "expected ')' here"))
10005 return true;
10006
10007 return false;
10008}
10009
10010/// FunctionSummary
10011/// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
10012/// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
10013/// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]?
10014/// [',' OptionalRefs]? ')'
10015bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
10016 unsigned ID) {
10017 LocTy Loc = Lex.getLoc();
10018 assert(Lex.getKind() == lltok::kw_function);
10019 Lex.Lex();
10020
10021 StringRef ModulePath;
10022 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10024 /*NotEligibleToImport=*/false,
10025 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10026 GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false);
10027 unsigned InstCount;
10029 FunctionSummary::TypeIdInfo TypeIdInfo;
10030 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
10032 std::vector<CallsiteInfo> Callsites;
10033 std::vector<AllocInfo> Allocs;
10034 // Default is all-zeros (conservative values).
10035 FunctionSummary::FFlags FFlags = {};
10036 if (parseToken(lltok::colon, "expected ':' here") ||
10037 parseToken(lltok::lparen, "expected '(' here") ||
10038 parseModuleReference(ModulePath) ||
10039 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
10040 parseToken(lltok::comma, "expected ',' here") ||
10041 parseToken(lltok::kw_insts, "expected 'insts' here") ||
10042 parseToken(lltok::colon, "expected ':' here") || parseUInt32(InstCount))
10043 return true;
10044
10045 // parse optional fields
10046 while (EatIfPresent(lltok::comma)) {
10047 switch (Lex.getKind()) {
10049 if (parseOptionalFFlags(FFlags))
10050 return true;
10051 break;
10052 case lltok::kw_calls:
10053 if (parseOptionalCalls(Calls))
10054 return true;
10055 break;
10057 if (parseOptionalTypeIdInfo(TypeIdInfo))
10058 return true;
10059 break;
10060 case lltok::kw_refs:
10061 if (parseOptionalRefs(Refs))
10062 return true;
10063 break;
10064 case lltok::kw_params:
10065 if (parseOptionalParamAccesses(ParamAccesses))
10066 return true;
10067 break;
10068 case lltok::kw_allocs:
10069 if (parseOptionalAllocs(Allocs))
10070 return true;
10071 break;
10073 if (parseOptionalCallsites(Callsites))
10074 return true;
10075 break;
10076 default:
10077 return error(Lex.getLoc(), "expected optional function summary field");
10078 }
10079 }
10080
10081 if (parseToken(lltok::rparen, "expected ')' here"))
10082 return true;
10083
10084 auto FS = std::make_unique<FunctionSummary>(
10085 GVFlags, InstCount, FFlags, std::move(Refs), std::move(Calls),
10086 std::move(TypeIdInfo.TypeTests),
10087 std::move(TypeIdInfo.TypeTestAssumeVCalls),
10088 std::move(TypeIdInfo.TypeCheckedLoadVCalls),
10089 std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
10090 std::move(TypeIdInfo.TypeCheckedLoadConstVCalls),
10091 std::move(ParamAccesses), std::move(Callsites), std::move(Allocs));
10092
10093 FS->setModulePath(ModulePath);
10094
10095 return addGlobalValueToIndex(Name, GUID,
10097 std::move(FS), Loc);
10098}
10099
10100/// VariableSummary
10101/// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
10102/// [',' OptionalRefs]? ')'
10103bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
10104 unsigned ID) {
10105 LocTy Loc = Lex.getLoc();
10106 assert(Lex.getKind() == lltok::kw_variable);
10107 Lex.Lex();
10108
10109 StringRef ModulePath;
10110 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10112 /*NotEligibleToImport=*/false,
10113 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10114 GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false);
10115 GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
10116 /* WriteOnly */ false,
10117 /* Constant */ false,
10120 VTableFuncList VTableFuncs;
10121 if (parseToken(lltok::colon, "expected ':' here") ||
10122 parseToken(lltok::lparen, "expected '(' here") ||
10123 parseModuleReference(ModulePath) ||
10124 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
10125 parseToken(lltok::comma, "expected ',' here") ||
10126 parseGVarFlags(GVarFlags))
10127 return true;
10128
10129 // parse optional fields
10130 while (EatIfPresent(lltok::comma)) {
10131 switch (Lex.getKind()) {
10133 if (parseOptionalVTableFuncs(VTableFuncs))
10134 return true;
10135 break;
10136 case lltok::kw_refs:
10137 if (parseOptionalRefs(Refs))
10138 return true;
10139 break;
10140 default:
10141 return error(Lex.getLoc(), "expected optional variable summary field");
10142 }
10143 }
10144
10145 if (parseToken(lltok::rparen, "expected ')' here"))
10146 return true;
10147
10148 auto GS =
10149 std::make_unique<GlobalVarSummary>(GVFlags, GVarFlags, std::move(Refs));
10150
10151 GS->setModulePath(ModulePath);
10152 GS->setVTableFuncs(std::move(VTableFuncs));
10153
10154 return addGlobalValueToIndex(Name, GUID,
10156 std::move(GS), Loc);
10157}
10158
10159/// AliasSummary
10160/// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
10161/// 'aliasee' ':' GVReference ')'
10162bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
10163 unsigned ID) {
10164 assert(Lex.getKind() == lltok::kw_alias);
10165 LocTy Loc = Lex.getLoc();
10166 Lex.Lex();
10167
10168 StringRef ModulePath;
10169 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10171 /*NotEligibleToImport=*/false,
10172 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10173 GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false);
10174 if (parseToken(lltok::colon, "expected ':' here") ||
10175 parseToken(lltok::lparen, "expected '(' here") ||
10176 parseModuleReference(ModulePath) ||
10177 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
10178 parseToken(lltok::comma, "expected ',' here") ||
10179 parseToken(lltok::kw_aliasee, "expected 'aliasee' here") ||
10180 parseToken(lltok::colon, "expected ':' here"))
10181 return true;
10182
10183 ValueInfo AliaseeVI;
10184 unsigned GVId;
10185 auto AS = std::make_unique<AliasSummary>(GVFlags);
10186 AS->setModulePath(ModulePath);
10187
10188 if (!EatIfPresent(lltok::kw_null)) {
10189 if (parseGVReference(AliaseeVI, GVId))
10190 return true;
10191
10192 // Record forward reference if the aliasee is not parsed yet.
10193 if (AliaseeVI.getRef() == FwdVIRef) {
10194 ForwardRefAliasees[GVId].emplace_back(AS.get(), Loc);
10195 } else {
10196 auto Summary = Index->findSummaryInModule(AliaseeVI, ModulePath);
10197 assert(Summary && "Aliasee must be a definition");
10198 AS->setAliasee(AliaseeVI, Summary);
10199 }
10200 }
10201
10202 if (parseToken(lltok::rparen, "expected ')' here"))
10203 return true;
10204
10205 return addGlobalValueToIndex(Name, GUID,
10207 std::move(AS), Loc);
10208}
10209
10210/// Flag
10211/// ::= [0|1]
10212bool LLParser::parseFlag(unsigned &Val) {
10213 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
10214 return tokError("expected integer");
10215 Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
10216 Lex.Lex();
10217 return false;
10218}
10219
10220/// OptionalFFlags
10221/// := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
10222/// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
10223/// [',' 'returnDoesNotAlias' ':' Flag]? ')'
10224/// [',' 'noInline' ':' Flag]? ')'
10225/// [',' 'alwaysInline' ':' Flag]? ')'
10226/// [',' 'noUnwind' ':' Flag]? ')'
10227/// [',' 'mayThrow' ':' Flag]? ')'
10228/// [',' 'hasUnknownCall' ':' Flag]? ')'
10229/// [',' 'mustBeUnreachable' ':' Flag]? ')'
10230
10231bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
10232 assert(Lex.getKind() == lltok::kw_funcFlags);
10233 Lex.Lex();
10234
10235 if (parseToken(lltok::colon, "expected ':' in funcFlags") ||
10236 parseToken(lltok::lparen, "expected '(' in funcFlags"))
10237 return true;
10238
10239 do {
10240 unsigned Val = 0;
10241 switch (Lex.getKind()) {
10242 case lltok::kw_readNone:
10243 Lex.Lex();
10244 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10245 return true;
10246 FFlags.ReadNone = Val;
10247 break;
10248 case lltok::kw_readOnly:
10249 Lex.Lex();
10250 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10251 return true;
10252 FFlags.ReadOnly = Val;
10253 break;
10255 Lex.Lex();
10256 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10257 return true;
10258 FFlags.NoRecurse = Val;
10259 break;
10261 Lex.Lex();
10262 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10263 return true;
10264 FFlags.ReturnDoesNotAlias = Val;
10265 break;
10266 case lltok::kw_noInline:
10267 Lex.Lex();
10268 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10269 return true;
10270 FFlags.NoInline = Val;
10271 break;
10273 Lex.Lex();
10274 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10275 return true;
10276 FFlags.AlwaysInline = Val;
10277 break;
10278 case lltok::kw_noUnwind:
10279 Lex.Lex();
10280 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10281 return true;
10282 FFlags.NoUnwind = Val;
10283 break;
10284 case lltok::kw_mayThrow:
10285 Lex.Lex();
10286 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10287 return true;
10288 FFlags.MayThrow = Val;
10289 break;
10291 Lex.Lex();
10292 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10293 return true;
10294 FFlags.HasUnknownCall = Val;
10295 break;
10297 Lex.Lex();
10298 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
10299 return true;
10300 FFlags.MustBeUnreachable = Val;
10301 break;
10302 default:
10303 return error(Lex.getLoc(), "expected function flag type");
10304 }
10305 } while (EatIfPresent(lltok::comma));
10306
10307 if (parseToken(lltok::rparen, "expected ')' in funcFlags"))
10308 return true;
10309
10310 return false;
10311}
10312
10313/// OptionalCalls
10314/// := 'calls' ':' '(' Call [',' Call]* ')'
10315/// Call ::= '(' 'callee' ':' GVReference
10316/// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]?
10317/// [ ',' 'tail' ]? ')'
10318bool LLParser::parseOptionalCalls(
10319 SmallVectorImpl<FunctionSummary::EdgeTy> &Calls) {
10320 assert(Lex.getKind() == lltok::kw_calls);
10321 Lex.Lex();
10322
10323 if (parseToken(lltok::colon, "expected ':' in calls") ||
10324 parseToken(lltok::lparen, "expected '(' in calls"))
10325 return true;
10326
10327 IdToIndexMapType IdToIndexMap;
10328 // parse each call edge
10329 do {
10330 ValueInfo VI;
10331 if (parseToken(lltok::lparen, "expected '(' in call") ||
10332 parseToken(lltok::kw_callee, "expected 'callee' in call") ||
10333 parseToken(lltok::colon, "expected ':'"))
10334 return true;
10335
10336 LocTy Loc = Lex.getLoc();
10337 unsigned GVId;
10338 if (parseGVReference(VI, GVId))
10339 return true;
10340
10342 unsigned RelBF = 0;
10343 unsigned HasTailCall = false;
10344
10345 // parse optional fields
10346 while (EatIfPresent(lltok::comma)) {
10347 switch (Lex.getKind()) {
10348 case lltok::kw_hotness:
10349 Lex.Lex();
10350 if (parseToken(lltok::colon, "expected ':'") || parseHotness(Hotness))
10351 return true;
10352 break;
10353 // Deprecated, keep in order to support old files.
10354 case lltok::kw_relbf:
10355 Lex.Lex();
10356 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(RelBF))
10357 return true;
10358 break;
10359 case lltok::kw_tail:
10360 Lex.Lex();
10361 if (parseToken(lltok::colon, "expected ':'") || parseFlag(HasTailCall))
10362 return true;
10363 break;
10364 default:
10365 return error(Lex.getLoc(), "expected hotness, relbf, or tail");
10366 }
10367 }
10368 // Keep track of the Call array index needing a forward reference.
10369 // We will save the location of the ValueInfo needing an update, but
10370 // can only do so once the std::vector is finalized.
10371 if (VI.getRef() == FwdVIRef)
10372 IdToIndexMap[GVId].push_back(std::make_pair(Calls.size(), Loc));
10373 Calls.push_back(
10374 FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, HasTailCall)});
10375
10376 if (parseToken(lltok::rparen, "expected ')' in call"))
10377 return true;
10378 } while (EatIfPresent(lltok::comma));
10379
10380 // Now that the Calls vector is finalized, it is safe to save the locations
10381 // of any forward GV references that need updating later.
10382 for (auto I : IdToIndexMap) {
10383 auto &Infos = ForwardRefValueInfos[I.first];
10384 for (auto P : I.second) {
10385 assert(Calls[P.first].first.getRef() == FwdVIRef &&
10386 "Forward referenced ValueInfo expected to be empty");
10387 Infos.emplace_back(&Calls[P.first].first, P.second);
10388 }
10389 }
10390
10391 if (parseToken(lltok::rparen, "expected ')' in calls"))
10392 return true;
10393
10394 return false;
10395}
10396
10397/// Hotness
10398/// := ('unknown'|'cold'|'none'|'hot'|'critical')
10399bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) {
10400 switch (Lex.getKind()) {
10401 case lltok::kw_unknown:
10403 break;
10404 case lltok::kw_cold:
10406 break;
10407 case lltok::kw_none:
10409 break;
10410 case lltok::kw_hot:
10412 break;
10413 case lltok::kw_critical:
10415 break;
10416 default:
10417 return error(Lex.getLoc(), "invalid call edge hotness");
10418 }
10419 Lex.Lex();
10420 return false;
10421}
10422
10423/// OptionalVTableFuncs
10424/// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')'
10425/// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')'
10426bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) {
10427 assert(Lex.getKind() == lltok::kw_vTableFuncs);
10428 Lex.Lex();
10429
10430 if (parseToken(lltok::colon, "expected ':' in vTableFuncs") ||
10431 parseToken(lltok::lparen, "expected '(' in vTableFuncs"))
10432 return true;
10433
10434 IdToIndexMapType IdToIndexMap;
10435 // parse each virtual function pair
10436 do {
10437 ValueInfo VI;
10438 if (parseToken(lltok::lparen, "expected '(' in vTableFunc") ||
10439 parseToken(lltok::kw_virtFunc, "expected 'callee' in vTableFunc") ||
10440 parseToken(lltok::colon, "expected ':'"))
10441 return true;
10442
10443 LocTy Loc = Lex.getLoc();
10444 unsigned GVId;
10445 if (parseGVReference(VI, GVId))
10446 return true;
10447
10448 uint64_t Offset;
10449 if (parseToken(lltok::comma, "expected comma") ||
10450 parseToken(lltok::kw_offset, "expected offset") ||
10451 parseToken(lltok::colon, "expected ':'") || parseUInt64(Offset))
10452 return true;
10453
10454 // Keep track of the VTableFuncs array index needing a forward reference.
10455 // We will save the location of the ValueInfo needing an update, but
10456 // can only do so once the std::vector is finalized.
10457 if (VI == EmptyVI)
10458 IdToIndexMap[GVId].push_back(std::make_pair(VTableFuncs.size(), Loc));
10459 VTableFuncs.push_back({VI, Offset});
10460
10461 if (parseToken(lltok::rparen, "expected ')' in vTableFunc"))
10462 return true;
10463 } while (EatIfPresent(lltok::comma));
10464
10465 // Now that the VTableFuncs vector is finalized, it is safe to save the
10466 // locations of any forward GV references that need updating later.
10467 for (auto I : IdToIndexMap) {
10468 auto &Infos = ForwardRefValueInfos[I.first];
10469 for (auto P : I.second) {
10470 assert(VTableFuncs[P.first].FuncVI == EmptyVI &&
10471 "Forward referenced ValueInfo expected to be empty");
10472 Infos.emplace_back(&VTableFuncs[P.first].FuncVI, P.second);
10473 }
10474 }
10475
10476 if (parseToken(lltok::rparen, "expected ')' in vTableFuncs"))
10477 return true;
10478
10479 return false;
10480}
10481
10482/// ParamNo := 'param' ':' UInt64
10483bool LLParser::parseParamNo(uint64_t &ParamNo) {
10484 if (parseToken(lltok::kw_param, "expected 'param' here") ||
10485 parseToken(lltok::colon, "expected ':' here") || parseUInt64(ParamNo))
10486 return true;
10487 return false;
10488}
10489
10490/// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']'
10491bool LLParser::parseParamAccessOffset(ConstantRange &Range) {
10492 APSInt Lower;
10493 APSInt Upper;
10494 auto ParseAPSInt = [&](APSInt &Val) {
10495 if (Lex.getKind() != lltok::APSInt)
10496 return tokError("expected integer");
10497 Val = Lex.getAPSIntVal();
10498 Val = Val.extOrTrunc(FunctionSummary::ParamAccess::RangeWidth);
10499 Val.setIsSigned(true);
10500 Lex.Lex();
10501 return false;
10502 };
10503 if (parseToken(lltok::kw_offset, "expected 'offset' here") ||
10504 parseToken(lltok::colon, "expected ':' here") ||
10505 parseToken(lltok::lsquare, "expected '[' here") || ParseAPSInt(Lower) ||
10506 parseToken(lltok::comma, "expected ',' here") || ParseAPSInt(Upper) ||
10507 parseToken(lltok::rsquare, "expected ']' here"))
10508 return true;
10509
10510 ++Upper;
10511 Range =
10512 (Lower == Upper && !Lower.isMaxValue())
10513 ? ConstantRange::getEmpty(FunctionSummary::ParamAccess::RangeWidth)
10514 : ConstantRange(Lower, Upper);
10515
10516 return false;
10517}
10518
10519/// ParamAccessCall
10520/// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')'
10521bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
10522 IdLocListType &IdLocList) {
10523 if (parseToken(lltok::lparen, "expected '(' here") ||
10524 parseToken(lltok::kw_callee, "expected 'callee' here") ||
10525 parseToken(lltok::colon, "expected ':' here"))
10526 return true;
10527
10528 unsigned GVId;
10529 ValueInfo VI;
10530 LocTy Loc = Lex.getLoc();
10531 if (parseGVReference(VI, GVId))
10532 return true;
10533
10534 Call.Callee = VI;
10535 IdLocList.emplace_back(GVId, Loc);
10536
10537 if (parseToken(lltok::comma, "expected ',' here") ||
10538 parseParamNo(Call.ParamNo) ||
10539 parseToken(lltok::comma, "expected ',' here") ||
10540 parseParamAccessOffset(Call.Offsets))
10541 return true;
10542
10543 if (parseToken(lltok::rparen, "expected ')' here"))
10544 return true;
10545
10546 return false;
10547}
10548
10549/// ParamAccess
10550/// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')'
10551/// OptionalParamAccessCalls := '(' Call [',' Call]* ')'
10552bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param,
10553 IdLocListType &IdLocList) {
10554 if (parseToken(lltok::lparen, "expected '(' here") ||
10555 parseParamNo(Param.ParamNo) ||
10556 parseToken(lltok::comma, "expected ',' here") ||
10557 parseParamAccessOffset(Param.Use))
10558 return true;
10559
10560 if (EatIfPresent(lltok::comma)) {
10561 if (parseToken(lltok::kw_calls, "expected 'calls' here") ||
10562 parseToken(lltok::colon, "expected ':' here") ||
10563 parseToken(lltok::lparen, "expected '(' here"))
10564 return true;
10565 do {
10566 FunctionSummary::ParamAccess::Call Call;
10567 if (parseParamAccessCall(Call, IdLocList))
10568 return true;
10569 Param.Calls.push_back(Call);
10570 } while (EatIfPresent(lltok::comma));
10571
10572 if (parseToken(lltok::rparen, "expected ')' here"))
10573 return true;
10574 }
10575
10576 if (parseToken(lltok::rparen, "expected ')' here"))
10577 return true;
10578
10579 return false;
10580}
10581
10582/// OptionalParamAccesses
10583/// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')'
10584bool LLParser::parseOptionalParamAccesses(
10585 std::vector<FunctionSummary::ParamAccess> &Params) {
10586 assert(Lex.getKind() == lltok::kw_params);
10587 Lex.Lex();
10588
10589 if (parseToken(lltok::colon, "expected ':' here") ||
10590 parseToken(lltok::lparen, "expected '(' here"))
10591 return true;
10592
10593 IdLocListType VContexts;
10594 size_t CallsNum = 0;
10595 do {
10596 FunctionSummary::ParamAccess ParamAccess;
10597 if (parseParamAccess(ParamAccess, VContexts))
10598 return true;
10599 CallsNum += ParamAccess.Calls.size();
10600 assert(VContexts.size() == CallsNum);
10601 (void)CallsNum;
10602 Params.emplace_back(std::move(ParamAccess));
10603 } while (EatIfPresent(lltok::comma));
10604
10605 if (parseToken(lltok::rparen, "expected ')' here"))
10606 return true;
10607
10608 // Now that the Params is finalized, it is safe to save the locations
10609 // of any forward GV references that need updating later.
10610 IdLocListType::const_iterator ItContext = VContexts.begin();
10611 for (auto &PA : Params) {
10612 for (auto &C : PA.Calls) {
10613 if (C.Callee.getRef() == FwdVIRef)
10614 ForwardRefValueInfos[ItContext->first].emplace_back(&C.Callee,
10615 ItContext->second);
10616 ++ItContext;
10617 }
10618 }
10619 assert(ItContext == VContexts.end());
10620
10621 return false;
10622}
10623
10624/// OptionalRefs
10625/// := 'refs' ':' '(' GVReference [',' GVReference]* ')'
10626bool LLParser::parseOptionalRefs(SmallVectorImpl<ValueInfo> &Refs) {
10627 assert(Lex.getKind() == lltok::kw_refs);
10628 Lex.Lex();
10629
10630 if (parseToken(lltok::colon, "expected ':' in refs") ||
10631 parseToken(lltok::lparen, "expected '(' in refs"))
10632 return true;
10633
10634 struct ValueContext {
10635 ValueInfo VI;
10636 unsigned GVId;
10637 LocTy Loc;
10638 };
10639 std::vector<ValueContext> VContexts;
10640 // parse each ref edge
10641 do {
10642 ValueContext VC;
10643 VC.Loc = Lex.getLoc();
10644 if (parseGVReference(VC.VI, VC.GVId))
10645 return true;
10646 VContexts.push_back(VC);
10647 } while (EatIfPresent(lltok::comma));
10648
10649 // Sort value contexts so that ones with writeonly
10650 // and readonly ValueInfo are at the end of VContexts vector.
10651 // See FunctionSummary::specialRefCounts()
10652 llvm::sort(VContexts, [](const ValueContext &VC1, const ValueContext &VC2) {
10653 return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier();
10654 });
10655
10656 IdToIndexMapType IdToIndexMap;
10657 for (auto &VC : VContexts) {
10658 // Keep track of the Refs array index needing a forward reference.
10659 // We will save the location of the ValueInfo needing an update, but
10660 // can only do so once the std::vector is finalized.
10661 if (VC.VI.getRef() == FwdVIRef)
10662 IdToIndexMap[VC.GVId].push_back(std::make_pair(Refs.size(), VC.Loc));
10663 Refs.push_back(VC.VI);
10664 }
10665
10666 // Now that the Refs vector is finalized, it is safe to save the locations
10667 // of any forward GV references that need updating later.
10668 for (auto I : IdToIndexMap) {
10669 auto &Infos = ForwardRefValueInfos[I.first];
10670 for (auto P : I.second) {
10671 assert(Refs[P.first].getRef() == FwdVIRef &&
10672 "Forward referenced ValueInfo expected to be empty");
10673 Infos.emplace_back(&Refs[P.first], P.second);
10674 }
10675 }
10676
10677 if (parseToken(lltok::rparen, "expected ')' in refs"))
10678 return true;
10679
10680 return false;
10681}
10682
10683/// OptionalTypeIdInfo
10684/// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
10685/// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]?
10686/// [',' TypeCheckedLoadConstVCalls]? ')'
10687bool LLParser::parseOptionalTypeIdInfo(
10688 FunctionSummary::TypeIdInfo &TypeIdInfo) {
10689 assert(Lex.getKind() == lltok::kw_typeIdInfo);
10690 Lex.Lex();
10691
10692 if (parseToken(lltok::colon, "expected ':' here") ||
10693 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
10694 return true;
10695
10696 do {
10697 switch (Lex.getKind()) {
10699 if (parseTypeTests(TypeIdInfo.TypeTests))
10700 return true;
10701 break;
10703 if (parseVFuncIdList(lltok::kw_typeTestAssumeVCalls,
10704 TypeIdInfo.TypeTestAssumeVCalls))
10705 return true;
10706 break;
10708 if (parseVFuncIdList(lltok::kw_typeCheckedLoadVCalls,
10709 TypeIdInfo.TypeCheckedLoadVCalls))
10710 return true;
10711 break;
10713 if (parseConstVCallList(lltok::kw_typeTestAssumeConstVCalls,
10714 TypeIdInfo.TypeTestAssumeConstVCalls))
10715 return true;
10716 break;
10718 if (parseConstVCallList(lltok::kw_typeCheckedLoadConstVCalls,
10719 TypeIdInfo.TypeCheckedLoadConstVCalls))
10720 return true;
10721 break;
10722 default:
10723 return error(Lex.getLoc(), "invalid typeIdInfo list type");
10724 }
10725 } while (EatIfPresent(lltok::comma));
10726
10727 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
10728 return true;
10729
10730 return false;
10731}
10732
10733/// TypeTests
10734/// ::= 'typeTests' ':' '(' (SummaryID | UInt64)
10735/// [',' (SummaryID | UInt64)]* ')'
10736bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
10737 assert(Lex.getKind() == lltok::kw_typeTests);
10738 Lex.Lex();
10739
10740 if (parseToken(lltok::colon, "expected ':' here") ||
10741 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
10742 return true;
10743
10744 IdToIndexMapType IdToIndexMap;
10745 do {
10747 if (Lex.getKind() == lltok::SummaryID) {
10748 unsigned ID = Lex.getUIntVal();
10749 LocTy Loc = Lex.getLoc();
10750 // Keep track of the TypeTests array index needing a forward reference.
10751 // We will save the location of the GUID needing an update, but
10752 // can only do so once the std::vector is finalized.
10753 IdToIndexMap[ID].push_back(std::make_pair(TypeTests.size(), Loc));
10754 Lex.Lex();
10755 } else if (parseUInt64(GUID))
10756 return true;
10757 TypeTests.push_back(GUID);
10758 } while (EatIfPresent(lltok::comma));
10759
10760 // Now that the TypeTests vector is finalized, it is safe to save the
10761 // locations of any forward GV references that need updating later.
10762 for (auto I : IdToIndexMap) {
10763 auto &Ids = ForwardRefTypeIds[I.first];
10764 for (auto P : I.second) {
10765 assert(TypeTests[P.first] == 0 &&
10766 "Forward referenced type id GUID expected to be 0");
10767 Ids.emplace_back(&TypeTests[P.first], P.second);
10768 }
10769 }
10770
10771 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
10772 return true;
10773
10774 return false;
10775}
10776
10777/// VFuncIdList
10778/// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
10779bool LLParser::parseVFuncIdList(
10780 lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
10781 assert(Lex.getKind() == Kind);
10782 Lex.Lex();
10783
10784 if (parseToken(lltok::colon, "expected ':' here") ||
10785 parseToken(lltok::lparen, "expected '(' here"))
10786 return true;
10787
10788 IdToIndexMapType IdToIndexMap;
10789 do {
10790 FunctionSummary::VFuncId VFuncId;
10791 if (parseVFuncId(VFuncId, IdToIndexMap, VFuncIdList.size()))
10792 return true;
10793 VFuncIdList.push_back(VFuncId);
10794 } while (EatIfPresent(lltok::comma));
10795
10796 if (parseToken(lltok::rparen, "expected ')' here"))
10797 return true;
10798
10799 // Now that the VFuncIdList vector is finalized, it is safe to save the
10800 // locations of any forward GV references that need updating later.
10801 for (auto I : IdToIndexMap) {
10802 auto &Ids = ForwardRefTypeIds[I.first];
10803 for (auto P : I.second) {
10804 assert(VFuncIdList[P.first].GUID == 0 &&
10805 "Forward referenced type id GUID expected to be 0");
10806 Ids.emplace_back(&VFuncIdList[P.first].GUID, P.second);
10807 }
10808 }
10809
10810 return false;
10811}
10812
10813/// ConstVCallList
10814/// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
10815bool LLParser::parseConstVCallList(
10816 lltok::Kind Kind,
10817 std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
10818 assert(Lex.getKind() == Kind);
10819 Lex.Lex();
10820
10821 if (parseToken(lltok::colon, "expected ':' here") ||
10822 parseToken(lltok::lparen, "expected '(' here"))
10823 return true;
10824
10825 IdToIndexMapType IdToIndexMap;
10826 do {
10827 FunctionSummary::ConstVCall ConstVCall;
10828 if (parseConstVCall(ConstVCall, IdToIndexMap, ConstVCallList.size()))
10829 return true;
10830 ConstVCallList.push_back(ConstVCall);
10831 } while (EatIfPresent(lltok::comma));
10832
10833 if (parseToken(lltok::rparen, "expected ')' here"))
10834 return true;
10835
10836 // Now that the ConstVCallList vector is finalized, it is safe to save the
10837 // locations of any forward GV references that need updating later.
10838 for (auto I : IdToIndexMap) {
10839 auto &Ids = ForwardRefTypeIds[I.first];
10840 for (auto P : I.second) {
10841 assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
10842 "Forward referenced type id GUID expected to be 0");
10843 Ids.emplace_back(&ConstVCallList[P.first].VFunc.GUID, P.second);
10844 }
10845 }
10846
10847 return false;
10848}
10849
10850/// ConstVCall
10851/// ::= '(' VFuncId ',' Args ')'
10852bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
10853 IdToIndexMapType &IdToIndexMap, unsigned Index) {
10854 if (parseToken(lltok::lparen, "expected '(' here") ||
10855 parseVFuncId(ConstVCall.VFunc, IdToIndexMap, Index))
10856 return true;
10857
10858 if (EatIfPresent(lltok::comma))
10859 if (parseArgs(ConstVCall.Args))
10860 return true;
10861
10862 if (parseToken(lltok::rparen, "expected ')' here"))
10863 return true;
10864
10865 return false;
10866}
10867
10868/// VFuncId
10869/// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
10870/// 'offset' ':' UInt64 ')'
10871bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId,
10872 IdToIndexMapType &IdToIndexMap, unsigned Index) {
10873 assert(Lex.getKind() == lltok::kw_vFuncId);
10874 Lex.Lex();
10875
10876 if (parseToken(lltok::colon, "expected ':' here") ||
10877 parseToken(lltok::lparen, "expected '(' here"))
10878 return true;
10879
10880 if (Lex.getKind() == lltok::SummaryID) {
10881 VFuncId.GUID = 0;
10882 unsigned ID = Lex.getUIntVal();
10883 LocTy Loc = Lex.getLoc();
10884 // Keep track of the array index needing a forward reference.
10885 // We will save the location of the GUID needing an update, but
10886 // can only do so once the caller's std::vector is finalized.
10887 IdToIndexMap[ID].push_back(std::make_pair(Index, Loc));
10888 Lex.Lex();
10889 } else if (parseToken(lltok::kw_guid, "expected 'guid' here") ||
10890 parseToken(lltok::colon, "expected ':' here") ||
10891 parseUInt64(VFuncId.GUID))
10892 return true;
10893
10894 if (parseToken(lltok::comma, "expected ',' here") ||
10895 parseToken(lltok::kw_offset, "expected 'offset' here") ||
10896 parseToken(lltok::colon, "expected ':' here") ||
10897 parseUInt64(VFuncId.Offset) ||
10898 parseToken(lltok::rparen, "expected ')' here"))
10899 return true;
10900
10901 return false;
10902}
10903
10904/// GVFlags
10905/// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
10906/// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ','
10907/// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ','
10908/// 'canAutoHide' ':' Flag ',' ')'
10909bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
10910 assert(Lex.getKind() == lltok::kw_flags);
10911 Lex.Lex();
10912
10913 if (parseToken(lltok::colon, "expected ':' here") ||
10914 parseToken(lltok::lparen, "expected '(' here"))
10915 return true;
10916
10917 do {
10918 unsigned Flag = 0;
10919 switch (Lex.getKind()) {
10920 case lltok::kw_linkage:
10921 Lex.Lex();
10922 if (parseToken(lltok::colon, "expected ':'"))
10923 return true;
10924 bool HasLinkage;
10925 GVFlags.Linkage = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
10926 assert(HasLinkage && "Linkage not optional in summary entry");
10927 Lex.Lex();
10928 break;
10930 Lex.Lex();
10931 if (parseToken(lltok::colon, "expected ':'"))
10932 return true;
10933 parseOptionalVisibility(Flag);
10934 GVFlags.Visibility = Flag;
10935 break;
10937 Lex.Lex();
10938 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10939 return true;
10940 GVFlags.NotEligibleToImport = Flag;
10941 break;
10942 case lltok::kw_live:
10943 Lex.Lex();
10944 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10945 return true;
10946 GVFlags.Live = Flag;
10947 break;
10948 case lltok::kw_dsoLocal:
10949 Lex.Lex();
10950 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10951 return true;
10952 GVFlags.DSOLocal = Flag;
10953 break;
10955 Lex.Lex();
10956 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10957 return true;
10958 GVFlags.CanAutoHide = Flag;
10959 break;
10961 Lex.Lex();
10962 if (parseToken(lltok::colon, "expected ':'"))
10963 return true;
10965 if (parseOptionalImportType(Lex.getKind(), IK))
10966 return true;
10967 GVFlags.ImportType = static_cast<unsigned>(IK);
10968 Lex.Lex();
10969 break;
10971 Lex.Lex();
10972 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10973 return true;
10974 GVFlags.NoRenameOnPromotion = Flag;
10975 break;
10976 default:
10977 return error(Lex.getLoc(), "expected gv flag type");
10978 }
10979 } while (EatIfPresent(lltok::comma));
10980
10981 if (parseToken(lltok::rparen, "expected ')' here"))
10982 return true;
10983
10984 return false;
10985}
10986
10987/// GVarFlags
10988/// ::= 'varFlags' ':' '(' 'readonly' ':' Flag
10989/// ',' 'writeonly' ':' Flag
10990/// ',' 'constant' ':' Flag ')'
10991bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) {
10992 assert(Lex.getKind() == lltok::kw_varFlags);
10993 Lex.Lex();
10994
10995 if (parseToken(lltok::colon, "expected ':' here") ||
10996 parseToken(lltok::lparen, "expected '(' here"))
10997 return true;
10998
10999 auto ParseRest = [this](unsigned int &Val) {
11000 Lex.Lex();
11001 if (parseToken(lltok::colon, "expected ':'"))
11002 return true;
11003 return parseFlag(Val);
11004 };
11005
11006 do {
11007 unsigned Flag = 0;
11008 switch (Lex.getKind()) {
11009 case lltok::kw_readonly:
11010 if (ParseRest(Flag))
11011 return true;
11012 GVarFlags.MaybeReadOnly = Flag;
11013 break;
11014 case lltok::kw_writeonly:
11015 if (ParseRest(Flag))
11016 return true;
11017 GVarFlags.MaybeWriteOnly = Flag;
11018 break;
11019 case lltok::kw_constant:
11020 if (ParseRest(Flag))
11021 return true;
11022 GVarFlags.Constant = Flag;
11023 break;
11025 if (ParseRest(Flag))
11026 return true;
11027 GVarFlags.VCallVisibility = Flag;
11028 break;
11029 default:
11030 return error(Lex.getLoc(), "expected gvar flag type");
11031 }
11032 } while (EatIfPresent(lltok::comma));
11033 return parseToken(lltok::rparen, "expected ')' here");
11034}
11035
11036/// ModuleReference
11037/// ::= 'module' ':' UInt
11038bool LLParser::parseModuleReference(StringRef &ModulePath) {
11039 // parse module id.
11040 if (parseToken(lltok::kw_module, "expected 'module' here") ||
11041 parseToken(lltok::colon, "expected ':' here") ||
11042 parseToken(lltok::SummaryID, "expected module ID"))
11043 return true;
11044
11045 unsigned ModuleID = Lex.getUIntVal();
11046 auto I = ModuleIdMap.find(ModuleID);
11047 // We should have already parsed all module IDs
11048 assert(I != ModuleIdMap.end());
11049 ModulePath = I->second;
11050 return false;
11051}
11052
11053/// GVReference
11054/// ::= SummaryID
11055bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) {
11056 bool WriteOnly = false, ReadOnly = EatIfPresent(lltok::kw_readonly);
11057 if (!ReadOnly)
11058 WriteOnly = EatIfPresent(lltok::kw_writeonly);
11059 if (parseToken(lltok::SummaryID, "expected GV ID"))
11060 return true;
11061
11062 GVId = Lex.getUIntVal();
11063 // Check if we already have a VI for this GV
11064 if (GVId < NumberedValueInfos.size() && NumberedValueInfos[GVId]) {
11065 assert(NumberedValueInfos[GVId].getRef() != FwdVIRef);
11066 VI = NumberedValueInfos[GVId];
11067 } else
11068 // We will create a forward reference to the stored location.
11069 VI = ValueInfo(false, FwdVIRef);
11070
11071 if (ReadOnly)
11072 VI.setReadOnly();
11073 if (WriteOnly)
11074 VI.setWriteOnly();
11075 return false;
11076}
11077
11078/// OptionalAllocs
11079/// := 'allocs' ':' '(' Alloc [',' Alloc]* ')'
11080/// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')'
11081/// ',' MemProfs ')'
11082/// Version ::= UInt32
11083bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) {
11084 assert(Lex.getKind() == lltok::kw_allocs);
11085 Lex.Lex();
11086
11087 if (parseToken(lltok::colon, "expected ':' in allocs") ||
11088 parseToken(lltok::lparen, "expected '(' in allocs"))
11089 return true;
11090
11091 // parse each alloc
11092 do {
11093 if (parseToken(lltok::lparen, "expected '(' in alloc") ||
11094 parseToken(lltok::kw_versions, "expected 'versions' in alloc") ||
11095 parseToken(lltok::colon, "expected ':'") ||
11096 parseToken(lltok::lparen, "expected '(' in versions"))
11097 return true;
11098
11099 SmallVector<uint8_t> Versions;
11100 do {
11101 uint8_t V = 0;
11102 if (parseAllocType(V))
11103 return true;
11104 Versions.push_back(V);
11105 } while (EatIfPresent(lltok::comma));
11106
11107 if (parseToken(lltok::rparen, "expected ')' in versions") ||
11108 parseToken(lltok::comma, "expected ',' in alloc"))
11109 return true;
11110
11111 std::vector<MIBInfo> MIBs;
11112 if (parseMemProfs(MIBs))
11113 return true;
11114
11115 Allocs.push_back({Versions, MIBs});
11116
11117 if (parseToken(lltok::rparen, "expected ')' in alloc"))
11118 return true;
11119 } while (EatIfPresent(lltok::comma));
11120
11121 if (parseToken(lltok::rparen, "expected ')' in allocs"))
11122 return true;
11123
11124 return false;
11125}
11126
11127/// MemProfs
11128/// := 'memProf' ':' '(' MemProf [',' MemProf]* ')'
11129/// MemProf ::= '(' 'type' ':' AllocType
11130/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
11131/// StackId ::= UInt64
11132bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) {
11133 assert(Lex.getKind() == lltok::kw_memProf);
11134 Lex.Lex();
11135
11136 if (parseToken(lltok::colon, "expected ':' in memprof") ||
11137 parseToken(lltok::lparen, "expected '(' in memprof"))
11138 return true;
11139
11140 // parse each MIB
11141 do {
11142 if (parseToken(lltok::lparen, "expected '(' in memprof") ||
11143 parseToken(lltok::kw_type, "expected 'type' in memprof") ||
11144 parseToken(lltok::colon, "expected ':'"))
11145 return true;
11146
11147 uint8_t AllocType;
11148 if (parseAllocType(AllocType))
11149 return true;
11150
11151 if (parseToken(lltok::comma, "expected ',' in memprof") ||
11152 parseToken(lltok::kw_stackIds, "expected 'stackIds' in memprof") ||
11153 parseToken(lltok::colon, "expected ':'") ||
11154 parseToken(lltok::lparen, "expected '(' in stackIds"))
11155 return true;
11156
11157 SmallVector<unsigned> StackIdIndices;
11158 // Combined index alloc records may not have a stack id list.
11159 if (Lex.getKind() != lltok::rparen) {
11160 do {
11161 uint64_t StackId = 0;
11162 if (parseUInt64(StackId))
11163 return true;
11164 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
11165 } while (EatIfPresent(lltok::comma));
11166 }
11167
11168 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
11169 return true;
11170
11171 MIBs.push_back({(AllocationType)AllocType, StackIdIndices});
11172
11173 if (parseToken(lltok::rparen, "expected ')' in memprof"))
11174 return true;
11175 } while (EatIfPresent(lltok::comma));
11176
11177 if (parseToken(lltok::rparen, "expected ')' in memprof"))
11178 return true;
11179
11180 return false;
11181}
11182
11183/// AllocType
11184/// := ('none'|'notcold'|'cold'|'hot')
11185bool LLParser::parseAllocType(uint8_t &AllocType) {
11186 switch (Lex.getKind()) {
11187 case lltok::kw_none:
11189 break;
11190 case lltok::kw_notcold:
11192 break;
11193 case lltok::kw_cold:
11195 break;
11196 case lltok::kw_hot:
11197 AllocType = (uint8_t)AllocationType::Hot;
11198 break;
11199 default:
11200 return error(Lex.getLoc(), "invalid alloc type");
11201 }
11202 Lex.Lex();
11203 return false;
11204}
11205
11206/// OptionalCallsites
11207/// := 'callsites' ':' '(' Callsite [',' Callsite]* ')'
11208/// Callsite ::= '(' 'callee' ':' GVReference
11209/// ',' 'clones' ':' '(' Version [',' Version]* ')'
11210/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
11211/// Version ::= UInt32
11212/// StackId ::= UInt64
11213bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) {
11214 assert(Lex.getKind() == lltok::kw_callsites);
11215 Lex.Lex();
11216
11217 if (parseToken(lltok::colon, "expected ':' in callsites") ||
11218 parseToken(lltok::lparen, "expected '(' in callsites"))
11219 return true;
11220
11221 IdToIndexMapType IdToIndexMap;
11222 // parse each callsite
11223 do {
11224 if (parseToken(lltok::lparen, "expected '(' in callsite") ||
11225 parseToken(lltok::kw_callee, "expected 'callee' in callsite") ||
11226 parseToken(lltok::colon, "expected ':'"))
11227 return true;
11228
11229 ValueInfo VI;
11230 unsigned GVId = 0;
11231 LocTy Loc = Lex.getLoc();
11232 if (!EatIfPresent(lltok::kw_null)) {
11233 if (parseGVReference(VI, GVId))
11234 return true;
11235 }
11236
11237 if (parseToken(lltok::comma, "expected ',' in callsite") ||
11238 parseToken(lltok::kw_clones, "expected 'clones' in callsite") ||
11239 parseToken(lltok::colon, "expected ':'") ||
11240 parseToken(lltok::lparen, "expected '(' in clones"))
11241 return true;
11242
11243 SmallVector<unsigned> Clones;
11244 do {
11245 unsigned V = 0;
11246 if (parseUInt32(V))
11247 return true;
11248 Clones.push_back(V);
11249 } while (EatIfPresent(lltok::comma));
11250
11251 if (parseToken(lltok::rparen, "expected ')' in clones") ||
11252 parseToken(lltok::comma, "expected ',' in callsite") ||
11253 parseToken(lltok::kw_stackIds, "expected 'stackIds' in callsite") ||
11254 parseToken(lltok::colon, "expected ':'") ||
11255 parseToken(lltok::lparen, "expected '(' in stackIds"))
11256 return true;
11257
11258 SmallVector<unsigned> StackIdIndices;
11259 // Synthesized callsite records will not have a stack id list.
11260 if (Lex.getKind() != lltok::rparen) {
11261 do {
11262 uint64_t StackId = 0;
11263 if (parseUInt64(StackId))
11264 return true;
11265 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
11266 } while (EatIfPresent(lltok::comma));
11267 }
11268
11269 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
11270 return true;
11271
11272 // Keep track of the Callsites array index needing a forward reference.
11273 // We will save the location of the ValueInfo needing an update, but
11274 // can only do so once the SmallVector is finalized.
11275 if (VI.getRef() == FwdVIRef)
11276 IdToIndexMap[GVId].push_back(std::make_pair(Callsites.size(), Loc));
11277 Callsites.push_back({VI, Clones, StackIdIndices});
11278
11279 if (parseToken(lltok::rparen, "expected ')' in callsite"))
11280 return true;
11281 } while (EatIfPresent(lltok::comma));
11282
11283 // Now that the Callsites vector is finalized, it is safe to save the
11284 // locations of any forward GV references that need updating later.
11285 for (auto I : IdToIndexMap) {
11286 auto &Infos = ForwardRefValueInfos[I.first];
11287 for (auto P : I.second) {
11288 assert(Callsites[P.first].Callee.getRef() == FwdVIRef &&
11289 "Forward referenced ValueInfo expected to be empty");
11290 Infos.emplace_back(&Callsites[P.first].Callee, P.second);
11291 }
11292 }
11293
11294 if (parseToken(lltok::rparen, "expected ')' in callsites"))
11295 return true;
11296
11297 return false;
11298}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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< 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")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil globals
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:245
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 std::optional< MemoryEffects::Location > keywordToLoc(lltok::Kind Tok)
static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved)
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:152
#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
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)"
static const char * name
This file contains some templates that are useful if you are working with the STL at all.
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 make_scope_exit function, which executes user-defined cleanup logic at scope ex...
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 & IEEEsingle()
Definition APFloat.h:296
static const fltSemantics & BFloat()
Definition APFloat.h:295
static const fltSemantics & IEEEdouble()
Definition APFloat.h:297
static constexpr roundingMode rmNearestTiesToEven
Definition APFloat.h:344
static const fltSemantics & IEEEhalf()
Definition APFloat.h:294
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition APFloat.h:1179
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1244
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
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
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:765
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
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
@ UDecWrap
Decrement one until a minimum value or zero.
@ 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:143
static LLVM_ABI bool canUseAsFnAttr(AttrKind Kind)
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:124
@ None
No attributes have been set.
Definition Attributes.h:126
static LLVM_ABI bool canUseAsParamAttr(AttrKind Kind)
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:483
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 BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
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:384
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:676
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition InstrTypes.h:679
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition InstrTypes.h:693
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition InstrTypes.h:684
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition InstrTypes.h:687
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition InstrTypes.h:692
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition InstrTypes.h:678
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:686
@ 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 LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
static LLVM_ABI Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
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_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:1412
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 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:1284
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.
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, MDNode *DL)
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, MDNode *DI)
Used to create DbgVariableRecords during parsing, where some metadata references may still be unresol...
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition DenseMap.h:205
unsigned size() const
Definition DenseMap.h:110
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
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:59
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:403
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:398
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:168
Argument * arg_iterator
Definition Function.h:73
void setPrefixData(Constant *PrefixData)
void setGC(std::string Str)
Definition Function.cpp:824
void setPersonalityFn(Constant *Fn)
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition Function.cpp:450
arg_iterator arg_begin()
Definition Function.h:868
void setAlignment(Align Align)
Sets the alignment attribute of the Function.
Definition Function.h:1040
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition Function.h:357
void setPreferredAlignment(MaybeAlign Align)
Sets the prefalign attribute of the Function.
Definition Function.h:1052
void setPrologueData(Constant *PrologueData)
void setCallingConv(CallingConv::ID CC)
Definition Function.h:276
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:613
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:670
LLVM_ABI void setComdat(Comdat *C)
Definition Globals.cpp:215
LLVM_ABI void setSection(StringRef S)
Change the section for this global.
Definition Globals.cpp:276
LLVM_ABI void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
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:78
LLVM_ABI const SanitizerMetadata & getSanitizerMetadata() const
Definition Globals.cpp:246
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.
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:94
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:162
void setVisibility(VisibilityTypes V)
LLVM_ABI void setSanitizerMetadata(SanitizerMetadata Meta)
Definition Globals.cpp:252
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:229
LLVM_ABI void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition Globals.cpp:534
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:581
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:72
LocTy getLoc() const
Definition LLLexer.h:71
bool parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read, const SlotMapping *Slots)
Definition LLParser.cpp:124
LLLexer::LocTy LocTy
Definition LLParser.h:110
LLVMContext & getContext()
Definition LLParser.h:231
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition LLParser.cpp:108
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition LLParser.cpp:95
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition LLParser.cpp:76
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:1080
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1580
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
A single uniqued string.
Definition Metadata.h:722
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition Metadata.h:1540
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1529
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition Metadata.h:1549
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:201
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:143
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Definition ModRef.h:149
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:110
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:67
StringMap< Comdat > ComdatSymTabType
The type of the comdat "symbol" table.
Definition Module.h:82
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 LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition Type.cpp:875
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
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:224
iterator find(StringRef Key)
Definition StringMap.h:237
StringMapIterBase< Comdat, false > iterator
Definition StringMap.h:221
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
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:413
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:619
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition Type.cpp:703
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:538
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Returns true if this struct contains a scalable vector.
Definition Type.cpp:440
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:913
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:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:297
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:264
static LLVM_ABI Type * getTokenTy(LLVMContext &C)
Definition Type.cpp:287
LLVM_ABI bool isScalableTy(SmallPtrSetImpl< const Type * > &Visited) const
Return true if this is a type whose size is a known multiple of vscale.
Definition Type.cpp:61
bool isLabelTy() const
Return true if this is 'label'.
Definition Type.h:228
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:153
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition Type.h:145
static LLVM_ABI Type * getLabelTy(LLVMContext &C)
Definition Type.cpp:281
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
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:249
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:197
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:304
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition Type.h:142
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:128
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:293
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:184
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition Type.h:258
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
bool isTokenTy() const
Return true if this is 'token'.
Definition Type.h:234
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
bool isMetadataTy() const
Return true if this is 'metadata'.
Definition Type.h:231
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 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:509
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
static constexpr uint64_t MaximumAlignment
Definition Value.h:832
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:397
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:111
bool use_empty() const
Definition Value.h:347
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
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:599
LLVM_ABI unsigned getOperationEncoding(StringRef OperationEncodingString)
Definition Dwarf.cpp:165
LLVM_ABI unsigned getAttributeEncoding(StringRef EncodingString)
Definition Dwarf.cpp:274
LLVM_ABI unsigned getTag(StringRef TagString)
Definition Dwarf.cpp:32
LLVM_ABI unsigned getCallingConvention(StringRef LanguageString)
Definition Dwarf.cpp:632
LLVM_ABI unsigned getLanguage(StringRef LanguageString)
Definition Dwarf.cpp:423
LLVM_ABI unsigned getVirtuality(StringRef VirtualityString)
Definition Dwarf.cpp:385
LLVM_ABI unsigned getEnumKind(StringRef EnumKindString)
Definition Dwarf.cpp:404
LLVM_ABI unsigned getMacinfo(StringRef MacinfoString)
Definition Dwarf.cpp:704
#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 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
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ 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:81
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > Tys={})
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 getIntrinsicSignature(Intrinsic::ID, FunctionType *FT, SmallVectorImpl< Type * > &ArgTys)
Gets the type arguments of an intrinsic call by matching type contraints specified by the ....
Flag
These should be considered private to the implementation of the MCInstrDesc class.
@ 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:765
@ DW_ATE_hi_user
Definition Dwarf.h:163
@ DW_APPLE_ENUM_KIND_max
Definition Dwarf.h:206
@ DW_LANG_hi_user
Definition Dwarf.h:220
MacinfoRecordType
Definition Dwarf.h:815
@ DW_MACINFO_vendor_ext
Definition Dwarf.h:821
@ 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:154
@ kw_riscv_vls_cc
Definition LLToken.h:190
@ kw_cxx_fast_tlscc
Definition LLToken.h:173
@ kw_extractvalue
Definition LLToken.h:373
@ kw_dso_preemptable
Definition LLToken.h:51
@ DwarfVirtuality
Definition LLToken.h:509
@ kw_arm_apcscc
Definition LLToken.h:146
@ kw_inteldialect
Definition LLToken.h:128
@ kw_x86_stdcallcc
Definition LLToken.h:141
@ kw_constant
Definition LLToken.h:48
@ kw_initialexec
Definition LLToken.h:74
@ kw_aarch64_sme_preservemost_from_x1
Definition LLToken.h:152
@ kw_provenance
Definition LLToken.h:223
@ kw_mustBeUnreachable
Definition LLToken.h:420
@ kw_internal
Definition LLToken.h:54
@ kw_no_sanitize_hwaddress
Definition LLToken.h:488
@ kw_datalayout
Definition LLToken.h:92
@ kw_wpdResolutions
Definition LLToken.h:459
@ kw_canAutoHide
Definition LLToken.h:403
@ kw_alwaysInline
Definition LLToken.h:416
@ kw_insertelement
Definition LLToken.h:370
@ kw_linkonce
Definition LLToken.h:55
@ kw_cheriot_librarycallcc
Definition LLToken.h:193
@ kw_inaccessiblememonly
Definition LLToken.h:216
@ kw_amdgpu_gfx
Definition LLToken.h:184
@ kw_getelementptr
Definition LLToken.h:367
@ kw_m68k_rtdcc
Definition LLToken.h:187
@ kw_preserve_nonecc
Definition LLToken.h:168
@ kw_x86_fastcallcc
Definition LLToken.h:142
@ kw_visibility
Definition LLToken.h:399
@ kw_cheriot_compartmentcalleecc
Definition LLToken.h:192
@ kw_positivezero
Definition LLToken.h:229
@ kw_unordered
Definition LLToken.h:95
@ kw_singleImpl
Definition LLToken.h:462
@ kw_localexec
Definition LLToken.h:75
@ kw_cfguard_checkcc
Definition LLToken.h:140
@ kw_typeCheckedLoadConstVCalls
Definition LLToken.h:439
@ kw_aarch64_sve_vector_pcs
Definition LLToken.h:150
@ kw_amdgpu_kernel
Definition LLToken.h:183
@ kw_uselistorder
Definition LLToken.h:386
@ kw_blockcount
Definition LLToken.h:397
@ kw_notEligibleToImport
Definition LLToken.h:400
@ 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:144
@ kw_ptx_device
Definition LLToken.h:158
@ kw_personality
Definition LLToken.h:342
@ DwarfEnumKind
Definition LLToken.h:522
@ kw_declaration
Definition LLToken.h:406
@ DwarfAttEncoding
Definition LLToken.h:508
@ kw_external
Definition LLToken.h:71
@ kw_spir_kernel
Definition LLToken.h:159
@ kw_local_unnamed_addr
Definition LLToken.h:68
@ kw_hasUnknownCall
Definition LLToken.h:419
@ kw_x86_intrcc
Definition LLToken.h:170
@ kw_addrspacecast
Definition LLToken.h:337
@ kw_zeroinitializer
Definition LLToken.h:76
@ StringConstant
Definition LLToken.h:506
@ kw_x86_thiscallcc
Definition LLToken.h:143
@ kw_cheriot_compartmentcallcc
Definition LLToken.h:191
@ kw_unnamed_addr
Definition LLToken.h:67
@ kw_uselistorder_bb
Definition LLToken.h:387
@ NameTableKind
Definition LLToken.h:514
@ kw_inlineBits
Definition LLToken.h:457
@ kw_weak_odr
Definition LLToken.h:58
@ kw_dllimport
Definition LLToken.h:60
@ kw_argmemonly
Definition LLToken.h:215
@ kw_blockaddress
Definition LLToken.h:375
@ kw_amdgpu_gfx_whole_wave
Definition LLToken.h:185
@ kw_landingpad
Definition LLToken.h:341
@ kw_aarch64_vector_pcs
Definition LLToken.h:149
@ kw_source_filename
Definition LLToken.h:90
@ kw_typeTestAssumeConstVCalls
Definition LLToken.h:438
@ FixedPointKind
Definition LLToken.h:515
@ kw_target_mem1
Definition LLToken.h:211
@ kw_ptx_kernel
Definition LLToken.h:157
@ kw_extractelement
Definition LLToken.h:369
@ kw_branchFunnel
Definition LLToken.h:463
@ kw_typeidCompatibleVTable
Definition LLToken.h:444
@ kw_vTableFuncs
Definition LLToken.h:430
@ kw_volatile
Definition LLToken.h:93
@ kw_typeCheckedLoadVCalls
Definition LLToken.h:437
@ kw_no_sanitize_address
Definition LLToken.h:485
@ kw_inaccessiblemem_or_argmemonly
Definition LLToken.h:217
@ kw_externally_initialized
Definition LLToken.h:69
@ kw_sanitize_address_dyninit
Definition LLToken.h:491
@ DwarfSourceLangName
Definition LLToken.h:511
@ kw_noRenameOnPromotion
Definition LLToken.h:407
@ kw_amdgpu_cs_chain_preserve
Definition LLToken.h:182
@ kw_thread_local
Definition LLToken.h:72
@ kw_catchswitch
Definition LLToken.h:355
@ kw_extern_weak
Definition LLToken.h:70
@ kw_arm_aapcscc
Definition LLToken.h:147
@ kw_read_provenance
Definition LLToken.h:224
@ kw_cleanuppad
Definition LLToken.h:358
@ kw_available_externally
Definition LLToken.h:63
@ kw_singleImplName
Definition LLToken.h:464
@ kw_target_mem0
Definition LLToken.h:210
@ kw_swifttailcc
Definition LLToken.h:165
@ kw_monotonic
Definition LLToken.h:96
@ kw_typeTestAssumeVCalls
Definition LLToken.h:436
@ kw_preservesign
Definition LLToken.h:228
@ kw_attributes
Definition LLToken.h:196
@ kw_code_model
Definition LLToken.h:122
@ kw_localdynamic
Definition LLToken.h:73
@ kw_uniformRetVal
Definition LLToken.h:467
@ kw_sideeffect
Definition LLToken.h:127
@ kw_sizeM1BitWidth
Definition LLToken.h:453
@ kw_nodeduplicate
Definition LLToken.h:259
@ kw_avr_signalcc
Definition LLToken.h:156
@ kw_exactmatch
Definition LLToken.h:257
@ kw_unreachable
Definition LLToken.h:353
@ kw_intel_ocl_bicc
Definition LLToken.h:139
@ kw_dso_local
Definition LLToken.h:50
@ kw_returnDoesNotAlias
Definition LLToken.h:414
@ kw_aarch64_sme_preservemost_from_x0
Definition LLToken.h:151
@ kw_preserve_allcc
Definition LLToken.h:167
@ kw_importType
Definition LLToken.h:404
@ kw_cleanupret
Definition LLToken.h:354
@ kw_shufflevector
Definition LLToken.h:371
@ kw_riscv_vector_cc
Definition LLToken.h:189
@ kw_avr_intrcc
Definition LLToken.h:155
@ kw_definition
Definition LLToken.h:405
@ kw_virtualConstProp
Definition LLToken.h:469
@ kw_vcall_visibility
Definition LLToken.h:458
@ kw_appending
Definition LLToken.h:59
@ kw_inaccessiblemem
Definition LLToken.h:209
@ kw_preserve_mostcc
Definition LLToken.h:166
@ kw_arm_aapcs_vfpcc
Definition LLToken.h:148
@ kw_typeTestRes
Definition LLToken.h:446
@ kw_x86_regcallcc
Definition LLToken.h:145
@ kw_typeIdInfo
Definition LLToken.h:434
@ kw_amdgpu_cs_chain
Definition LLToken.h:181
@ kw_dso_local_equivalent
Definition LLToken.h:376
@ kw_x86_64_sysvcc
Definition LLToken.h:161
@ DbgRecordType
Definition LLToken.h:521
@ kw_address_is_null
Definition LLToken.h:222
@ kw_musttail
Definition LLToken.h:86
@ kw_aarch64_sme_preservemost_from_x2
Definition LLToken.h:153
@ kw_uniqueRetVal
Definition LLToken.h:468
@ kw_insertvalue
Definition LLToken.h:374
@ kw_indirectbr
Definition LLToken.h:350
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:578
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
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:1669
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:53
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:634
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.
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:313
void copyModuleAttrToFunctions(Module &M)
Copies module attributes to the functions in the module.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
UWTableKind
Definition CodeGen.h:154
@ Async
"Asynchronous" unwind tables (instr precise)
Definition CodeGen.h:157
@ Sync
"Synchronous" unwind tables
Definition CodeGen.h:156
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:364
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
CaptureComponents
Components of the pointer that may be captured.
Definition ModRef.h:322
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
@ ErrnoMem
Errno memory.
Definition ModRef.h:66
@ ArgMem
Access to memory via argument pointers.
Definition ModRef.h:62
@ TargetMem0
Represents target specific state.
Definition ModRef.h:70
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
Definition ModRef.h:64
llvm::function_ref< std::optional< std::string >(StringRef, StringRef)> DataLayoutCallbackTy
Definition Parser.h:36
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:2012
DWARFExpression::Operation Op
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:1917
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
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:332
LLVM_ABI MDNode * UpgradeTBAANode(MDNode &TBAANode)
If the given TBAA tag uses the scalar TBAA format, create a new node corresponding to the upgrade to ...
#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
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.