LLVM 19.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/AutoUpgrade.h"
24#include "llvm/IR/BasicBlock.h"
25#include "llvm/IR/CallingConv.h"
26#include "llvm/IR/Comdat.h"
28#include "llvm/IR/Constants.h"
31#include "llvm/IR/Function.h"
32#include "llvm/IR/GlobalIFunc.h"
34#include "llvm/IR/InlineAsm.h"
38#include "llvm/IR/Intrinsics.h"
39#include "llvm/IR/LLVMContext.h"
40#include "llvm/IR/Metadata.h"
41#include "llvm/IR/Module.h"
42#include "llvm/IR/Operator.h"
43#include "llvm/IR/Value.h"
48#include "llvm/Support/ModRef.h"
51#include <algorithm>
52#include <cassert>
53#include <cstring>
54#include <optional>
55#include <vector>
56
57using namespace llvm;
58
60 "allow-incomplete-ir", cl::init(false), cl::Hidden,
62 "Allow incomplete IR on a best effort basis (references to unknown "
63 "metadata will be dropped)"));
64
69
70static std::string getTypeString(Type *T) {
71 std::string Result;
72 raw_string_ostream Tmp(Result);
73 Tmp << *T;
74 return Tmp.str();
75}
76
77// Whatever debug info format we parsed, we should convert to the expected debug
78// info format immediately afterwards.
79bool LLParser::finalizeDebugInfoFormat(Module *M) {
80 // We should have already returned an error if we observed both intrinsics and
81 // records in this IR.
82 assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) &&
83 "Mixed debug intrinsics/records seen without a parsing error?");
85 UseNewDbgInfoFormat = SeenNewDbgInfoFormat;
86 WriteNewDbgInfoFormatToBitcode = SeenNewDbgInfoFormat;
87 WriteNewDbgInfoFormat = SeenNewDbgInfoFormat;
88 } else if (M) {
89 M->setIsNewDbgInfoFormat(false);
90 }
91 return false;
92}
93
94/// Run: module ::= toplevelentity*
96 DataLayoutCallbackTy DataLayoutCallback) {
97 // Prime the lexer.
98 Lex.Lex();
99
100 if (Context.shouldDiscardValueNames())
101 return error(
102 Lex.getLoc(),
103 "Can't read textual IR with a Context that discards named Values");
104
105 if (M) {
106 if (parseTargetDefinitions(DataLayoutCallback))
107 return true;
108 }
109
110 return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) ||
111 validateEndOfIndex() || finalizeDebugInfoFormat(M);
112}
113
115 const SlotMapping *Slots) {
116 restoreParsingState(Slots);
117 Lex.Lex();
118
119 Type *Ty = nullptr;
120 if (parseType(Ty) || parseConstantValue(Ty, C))
121 return true;
122 if (Lex.getKind() != lltok::Eof)
123 return error(Lex.getLoc(), "expected end of string");
124 return false;
125}
126
127bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
128 const SlotMapping *Slots) {
129 restoreParsingState(Slots);
130 Lex.Lex();
131
132 Read = 0;
133 SMLoc Start = Lex.getLoc();
134 Ty = nullptr;
135 if (parseType(Ty))
136 return true;
137 SMLoc End = Lex.getLoc();
138 Read = End.getPointer() - Start.getPointer();
139
140 return false;
141}
142
143void LLParser::restoreParsingState(const SlotMapping *Slots) {
144 if (!Slots)
145 return;
146 NumberedVals = Slots->GlobalValues;
147 NumberedMetadata = Slots->MetadataNodes;
148 for (const auto &I : Slots->NamedTypes)
149 NamedTypes.insert(
150 std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
151 for (const auto &I : Slots->Types)
152 NumberedTypes.insert(
153 std::make_pair(I.first, std::make_pair(I.second, LocTy())));
154}
155
157 // White-list intrinsics that are safe to drop.
158 if (!isa<DbgInfoIntrinsic>(II) &&
159 II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl)
160 return;
161
163 for (Value *V : II->args())
164 if (auto *MV = dyn_cast<MetadataAsValue>(V))
165 if (auto *MD = dyn_cast<MDNode>(MV->getMetadata()))
166 if (MD->isTemporary())
167 MVs.push_back(MV);
168
169 if (!MVs.empty()) {
170 assert(II->use_empty() && "Cannot have uses");
171 II->eraseFromParent();
172
173 // Also remove no longer used MetadataAsValue wrappers.
174 for (MetadataAsValue *MV : MVs)
175 if (MV->use_empty())
176 delete MV;
177 }
178}
179
180void LLParser::dropUnknownMetadataReferences() {
181 auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); };
182 for (Function &F : *M) {
183 F.eraseMetadataIf(Pred);
185 I.eraseMetadataIf(Pred);
186
187 if (auto *II = dyn_cast<IntrinsicInst>(&I))
189 }
190 }
191
192 for (GlobalVariable &GV : M->globals())
193 GV.eraseMetadataIf(Pred);
194
195 for (const auto &[ID, Info] : make_early_inc_range(ForwardRefMDNodes)) {
196 // Check whether there is only a single use left, which would be in our
197 // own NumberedMetadata.
198 if (Info.first->getNumTemporaryUses() == 1) {
199 NumberedMetadata.erase(ID);
200 ForwardRefMDNodes.erase(ID);
201 }
202 }
203}
204
205/// validateEndOfModule - Do final validity and basic correctness checks at the
206/// end of the module.
207bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
208 if (!M)
209 return false;
210 // Handle any function attribute group forward references.
211 for (const auto &RAG : ForwardRefAttrGroups) {
212 Value *V = RAG.first;
213 const std::vector<unsigned> &Attrs = RAG.second;
214 AttrBuilder B(Context);
215
216 for (const auto &Attr : Attrs) {
217 auto R = NumberedAttrBuilders.find(Attr);
218 if (R != NumberedAttrBuilders.end())
219 B.merge(R->second);
220 }
221
222 if (Function *Fn = dyn_cast<Function>(V)) {
223 AttributeList AS = Fn->getAttributes();
224 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
225 AS = AS.removeFnAttributes(Context);
226
227 FnAttrs.merge(B);
228
229 // If the alignment was parsed as an attribute, move to the alignment
230 // field.
231 if (MaybeAlign A = FnAttrs.getAlignment()) {
232 Fn->setAlignment(*A);
233 FnAttrs.removeAttribute(Attribute::Alignment);
234 }
235
236 AS = AS.addFnAttributes(Context, FnAttrs);
237 Fn->setAttributes(AS);
238 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
239 AttributeList AS = CI->getAttributes();
240 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
241 AS = AS.removeFnAttributes(Context);
242 FnAttrs.merge(B);
243 AS = AS.addFnAttributes(Context, FnAttrs);
244 CI->setAttributes(AS);
245 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
246 AttributeList AS = II->getAttributes();
247 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
248 AS = AS.removeFnAttributes(Context);
249 FnAttrs.merge(B);
250 AS = AS.addFnAttributes(Context, FnAttrs);
251 II->setAttributes(AS);
252 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(V)) {
253 AttributeList AS = CBI->getAttributes();
254 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
255 AS = AS.removeFnAttributes(Context);
256 FnAttrs.merge(B);
257 AS = AS.addFnAttributes(Context, FnAttrs);
258 CBI->setAttributes(AS);
259 } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
260 AttrBuilder Attrs(M->getContext(), GV->getAttributes());
261 Attrs.merge(B);
262 GV->setAttributes(AttributeSet::get(Context,Attrs));
263 } else {
264 llvm_unreachable("invalid object with forward attribute group reference");
265 }
266 }
267
268 // If there are entries in ForwardRefBlockAddresses at this point, the
269 // function was never defined.
270 if (!ForwardRefBlockAddresses.empty())
271 return error(ForwardRefBlockAddresses.begin()->first.Loc,
272 "expected function name in blockaddress");
273
274 auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef,
275 GlobalValue *FwdRef) {
276 GlobalValue *GV = nullptr;
277 if (GVRef.Kind == ValID::t_GlobalName) {
278 GV = M->getNamedValue(GVRef.StrVal);
279 } else {
280 GV = NumberedVals.get(GVRef.UIntVal);
281 }
282
283 if (!GV)
284 return error(GVRef.Loc, "unknown function '" + GVRef.StrVal +
285 "' referenced by dso_local_equivalent");
286
287 if (!GV->getValueType()->isFunctionTy())
288 return error(GVRef.Loc,
289 "expected a function, alias to function, or ifunc "
290 "in dso_local_equivalent");
291
292 auto *Equiv = DSOLocalEquivalent::get(GV);
293 FwdRef->replaceAllUsesWith(Equiv);
294 FwdRef->eraseFromParent();
295 return false;
296 };
297
298 // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this
299 // point, they are references after the function was defined. Resolve those
300 // now.
301 for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) {
302 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
303 return true;
304 }
305 for (auto &Iter : ForwardRefDSOLocalEquivalentNames) {
306 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
307 return true;
308 }
309 ForwardRefDSOLocalEquivalentIDs.clear();
310 ForwardRefDSOLocalEquivalentNames.clear();
311
312 for (const auto &NT : NumberedTypes)
313 if (NT.second.second.isValid())
314 return error(NT.second.second,
315 "use of undefined type '%" + Twine(NT.first) + "'");
316
317 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
318 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
319 if (I->second.second.isValid())
320 return error(I->second.second,
321 "use of undefined type named '" + I->getKey() + "'");
322
323 if (!ForwardRefComdats.empty())
324 return error(ForwardRefComdats.begin()->second,
325 "use of undefined comdat '$" +
326 ForwardRefComdats.begin()->first + "'");
327
328 for (const auto &[Name, Info] : make_early_inc_range(ForwardRefVals)) {
329 if (StringRef(Name).starts_with("llvm.")) {
331 if (IID == Intrinsic::not_intrinsic)
332 // Don't do anything for unknown intrinsics.
333 continue;
334
335 // Automatically create declarations for intrinsics. Intrinsics can only
336 // be called directly, so the call function type directly determines the
337 // declaration function type.
338 //
339 // Additionally, automatically add the required mangling suffix to the
340 // intrinsic name. This means that we may replace a single forward
341 // declaration with multiple functions here.
342 for (Use &U : make_early_inc_range(Info.first->uses())) {
343 auto *CB = dyn_cast<CallBase>(U.getUser());
344 if (!CB || !CB->isCallee(&U))
345 return error(Info.second, "intrinsic can only be used as callee");
346
347 SmallVector<Type *> OverloadTys;
348 if (!Intrinsic::getIntrinsicSignature(IID, CB->getFunctionType(),
349 OverloadTys))
350 return error(Info.second, "invalid intrinsic signature");
351
352 U.set(Intrinsic::getDeclaration(M, IID, OverloadTys));
353 }
354
355 Info.first->eraseFromParent();
356 ForwardRefVals.erase(Name);
357 continue;
358 }
359
360 // If incomplete IR is allowed, also add declarations for
361 // non-intrinsics.
363 continue;
364
365 auto GetCommonFunctionType = [](Value *V) -> FunctionType * {
366 FunctionType *FTy = nullptr;
367 for (Use &U : V->uses()) {
368 auto *CB = dyn_cast<CallBase>(U.getUser());
369 if (!CB || !CB->isCallee(&U) || (FTy && FTy != CB->getFunctionType()))
370 return nullptr;
371 FTy = CB->getFunctionType();
372 }
373 return FTy;
374 };
375
376 // First check whether this global is only used in calls with the same
377 // type, in which case we'll insert a function. Otherwise, fall back to
378 // using a dummy i8 type.
379 Type *Ty = GetCommonFunctionType(Info.first);
380 if (!Ty)
381 Ty = Type::getInt8Ty(Context);
382
383 GlobalValue *GV;
384 if (auto *FTy = dyn_cast<FunctionType>(Ty))
386 else
387 GV = new GlobalVariable(*M, Ty, /*isConstant*/ false,
389 /*Initializer*/ nullptr, Name);
390 Info.first->replaceAllUsesWith(GV);
391 Info.first->eraseFromParent();
392 ForwardRefVals.erase(Name);
393 }
394
395 if (!ForwardRefVals.empty())
396 return error(ForwardRefVals.begin()->second.second,
397 "use of undefined value '@" + ForwardRefVals.begin()->first +
398 "'");
399
400 if (!ForwardRefValIDs.empty())
401 return error(ForwardRefValIDs.begin()->second.second,
402 "use of undefined value '@" +
403 Twine(ForwardRefValIDs.begin()->first) + "'");
404
405 if (AllowIncompleteIR && !ForwardRefMDNodes.empty())
406 dropUnknownMetadataReferences();
407
408 if (!ForwardRefMDNodes.empty())
409 return error(ForwardRefMDNodes.begin()->second.second,
410 "use of undefined metadata '!" +
411 Twine(ForwardRefMDNodes.begin()->first) + "'");
412
413 // Resolve metadata cycles.
414 for (auto &N : NumberedMetadata) {
415 if (N.second && !N.second->isResolved())
416 N.second->resolveCycles();
417 }
418
419 for (auto *Inst : InstsWithTBAATag) {
420 MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
421 // With incomplete IR, the tbaa metadata may have been dropped.
423 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
424 if (MD) {
425 auto *UpgradedMD = UpgradeTBAANode(*MD);
426 if (MD != UpgradedMD)
427 Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
428 }
429 }
430
431 // Look for intrinsic functions and CallInst that need to be upgraded. We use
432 // make_early_inc_range here because we may remove some functions.
435
436 if (UpgradeDebugInfo)
438
441
442 if (!Slots)
443 return false;
444 // Initialize the slot mapping.
445 // Because by this point we've parsed and validated everything, we can "steal"
446 // the mapping from LLParser as it doesn't need it anymore.
447 Slots->GlobalValues = std::move(NumberedVals);
448 Slots->MetadataNodes = std::move(NumberedMetadata);
449 for (const auto &I : NamedTypes)
450 Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
451 for (const auto &I : NumberedTypes)
452 Slots->Types.insert(std::make_pair(I.first, I.second.first));
453
454 return false;
455}
456
457/// Do final validity and basic correctness checks at the end of the index.
458bool LLParser::validateEndOfIndex() {
459 if (!Index)
460 return false;
461
462 if (!ForwardRefValueInfos.empty())
463 return error(ForwardRefValueInfos.begin()->second.front().second,
464 "use of undefined summary '^" +
465 Twine(ForwardRefValueInfos.begin()->first) + "'");
466
467 if (!ForwardRefAliasees.empty())
468 return error(ForwardRefAliasees.begin()->second.front().second,
469 "use of undefined summary '^" +
470 Twine(ForwardRefAliasees.begin()->first) + "'");
471
472 if (!ForwardRefTypeIds.empty())
473 return error(ForwardRefTypeIds.begin()->second.front().second,
474 "use of undefined type id summary '^" +
475 Twine(ForwardRefTypeIds.begin()->first) + "'");
476
477 return false;
478}
479
480//===----------------------------------------------------------------------===//
481// Top-Level Entities
482//===----------------------------------------------------------------------===//
483
484bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) {
485 // Delay parsing of the data layout string until the target triple is known.
486 // Then, pass both the the target triple and the tentative data layout string
487 // to DataLayoutCallback, allowing to override the DL string.
488 // This enables importing modules with invalid DL strings.
489 std::string TentativeDLStr = M->getDataLayoutStr();
490 LocTy DLStrLoc;
491
492 bool Done = false;
493 while (!Done) {
494 switch (Lex.getKind()) {
495 case lltok::kw_target:
496 if (parseTargetDefinition(TentativeDLStr, DLStrLoc))
497 return true;
498 break;
500 if (parseSourceFileName())
501 return true;
502 break;
503 default:
504 Done = true;
505 }
506 }
507 // Run the override callback to potentially change the data layout string, and
508 // parse the data layout string.
509 if (auto LayoutOverride =
510 DataLayoutCallback(M->getTargetTriple(), TentativeDLStr)) {
511 TentativeDLStr = *LayoutOverride;
512 DLStrLoc = {};
513 }
514 Expected<DataLayout> MaybeDL = DataLayout::parse(TentativeDLStr);
515 if (!MaybeDL)
516 return error(DLStrLoc, toString(MaybeDL.takeError()));
517 M->setDataLayout(MaybeDL.get());
518 return false;
519}
520
521bool LLParser::parseTopLevelEntities() {
522 // If there is no Module, then parse just the summary index entries.
523 if (!M) {
524 while (true) {
525 switch (Lex.getKind()) {
526 case lltok::Eof:
527 return false;
528 case lltok::SummaryID:
529 if (parseSummaryEntry())
530 return true;
531 break;
533 if (parseSourceFileName())
534 return true;
535 break;
536 default:
537 // Skip everything else
538 Lex.Lex();
539 }
540 }
541 }
542 while (true) {
543 switch (Lex.getKind()) {
544 default:
545 return tokError("expected top-level entity");
546 case lltok::Eof: return false;
548 if (parseDeclare())
549 return true;
550 break;
551 case lltok::kw_define:
552 if (parseDefine())
553 return true;
554 break;
555 case lltok::kw_module:
556 if (parseModuleAsm())
557 return true;
558 break;
560 if (parseUnnamedType())
561 return true;
562 break;
563 case lltok::LocalVar:
564 if (parseNamedType())
565 return true;
566 break;
567 case lltok::GlobalID:
568 if (parseUnnamedGlobal())
569 return true;
570 break;
571 case lltok::GlobalVar:
572 if (parseNamedGlobal())
573 return true;
574 break;
575 case lltok::ComdatVar: if (parseComdat()) return true; break;
576 case lltok::exclaim:
577 if (parseStandaloneMetadata())
578 return true;
579 break;
580 case lltok::SummaryID:
581 if (parseSummaryEntry())
582 return true;
583 break;
585 if (parseNamedMetadata())
586 return true;
587 break;
589 if (parseUnnamedAttrGrp())
590 return true;
591 break;
593 if (parseUseListOrder())
594 return true;
595 break;
597 if (parseUseListOrderBB())
598 return true;
599 break;
600 }
601 }
602}
603
604/// toplevelentity
605/// ::= 'module' 'asm' STRINGCONSTANT
606bool LLParser::parseModuleAsm() {
608 Lex.Lex();
609
610 std::string AsmStr;
611 if (parseToken(lltok::kw_asm, "expected 'module asm'") ||
612 parseStringConstant(AsmStr))
613 return true;
614
615 M->appendModuleInlineAsm(AsmStr);
616 return false;
617}
618
619/// toplevelentity
620/// ::= 'target' 'triple' '=' STRINGCONSTANT
621/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
622bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
623 LocTy &DLStrLoc) {
625 std::string Str;
626 switch (Lex.Lex()) {
627 default:
628 return tokError("unknown target property");
629 case lltok::kw_triple:
630 Lex.Lex();
631 if (parseToken(lltok::equal, "expected '=' after target triple") ||
632 parseStringConstant(Str))
633 return true;
634 M->setTargetTriple(Str);
635 return false;
637 Lex.Lex();
638 if (parseToken(lltok::equal, "expected '=' after target datalayout"))
639 return true;
640 DLStrLoc = Lex.getLoc();
641 if (parseStringConstant(TentativeDLStr))
642 return true;
643 return false;
644 }
645}
646
647/// toplevelentity
648/// ::= 'source_filename' '=' STRINGCONSTANT
649bool LLParser::parseSourceFileName() {
651 Lex.Lex();
652 if (parseToken(lltok::equal, "expected '=' after source_filename") ||
653 parseStringConstant(SourceFileName))
654 return true;
655 if (M)
656 M->setSourceFileName(SourceFileName);
657 return false;
658}
659
660/// parseUnnamedType:
661/// ::= LocalVarID '=' 'type' type
662bool LLParser::parseUnnamedType() {
663 LocTy TypeLoc = Lex.getLoc();
664 unsigned TypeID = Lex.getUIntVal();
665 Lex.Lex(); // eat LocalVarID;
666
667 if (parseToken(lltok::equal, "expected '=' after name") ||
668 parseToken(lltok::kw_type, "expected 'type' after '='"))
669 return true;
670
671 Type *Result = nullptr;
672 if (parseStructDefinition(TypeLoc, "", NumberedTypes[TypeID], Result))
673 return true;
674
675 if (!isa<StructType>(Result)) {
676 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
677 if (Entry.first)
678 return error(TypeLoc, "non-struct types may not be recursive");
679 Entry.first = Result;
680 Entry.second = SMLoc();
681 }
682
683 return false;
684}
685
686/// toplevelentity
687/// ::= LocalVar '=' 'type' type
688bool LLParser::parseNamedType() {
689 std::string Name = Lex.getStrVal();
690 LocTy NameLoc = Lex.getLoc();
691 Lex.Lex(); // eat LocalVar.
692
693 if (parseToken(lltok::equal, "expected '=' after name") ||
694 parseToken(lltok::kw_type, "expected 'type' after name"))
695 return true;
696
697 Type *Result = nullptr;
698 if (parseStructDefinition(NameLoc, Name, NamedTypes[Name], Result))
699 return true;
700
701 if (!isa<StructType>(Result)) {
702 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
703 if (Entry.first)
704 return error(NameLoc, "non-struct types may not be recursive");
705 Entry.first = Result;
706 Entry.second = SMLoc();
707 }
708
709 return false;
710}
711
712/// toplevelentity
713/// ::= 'declare' FunctionHeader
714bool LLParser::parseDeclare() {
716 Lex.Lex();
717
718 std::vector<std::pair<unsigned, MDNode *>> MDs;
719 while (Lex.getKind() == lltok::MetadataVar) {
720 unsigned MDK;
721 MDNode *N;
722 if (parseMetadataAttachment(MDK, N))
723 return true;
724 MDs.push_back({MDK, N});
725 }
726
727 Function *F;
728 unsigned FunctionNumber = -1;
729 SmallVector<unsigned> UnnamedArgNums;
730 if (parseFunctionHeader(F, false, FunctionNumber, UnnamedArgNums))
731 return true;
732 for (auto &MD : MDs)
733 F->addMetadata(MD.first, *MD.second);
734 return false;
735}
736
737/// toplevelentity
738/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
739bool LLParser::parseDefine() {
741 Lex.Lex();
742
743 Function *F;
744 unsigned FunctionNumber = -1;
745 SmallVector<unsigned> UnnamedArgNums;
746 return parseFunctionHeader(F, true, FunctionNumber, UnnamedArgNums) ||
747 parseOptionalFunctionMetadata(*F) ||
748 parseFunctionBody(*F, FunctionNumber, UnnamedArgNums);
749}
750
751/// parseGlobalType
752/// ::= 'constant'
753/// ::= 'global'
754bool LLParser::parseGlobalType(bool &IsConstant) {
755 if (Lex.getKind() == lltok::kw_constant)
756 IsConstant = true;
757 else if (Lex.getKind() == lltok::kw_global)
758 IsConstant = false;
759 else {
760 IsConstant = false;
761 return tokError("expected 'global' or 'constant'");
762 }
763 Lex.Lex();
764 return false;
765}
766
767bool LLParser::parseOptionalUnnamedAddr(
768 GlobalVariable::UnnamedAddr &UnnamedAddr) {
769 if (EatIfPresent(lltok::kw_unnamed_addr))
771 else if (EatIfPresent(lltok::kw_local_unnamed_addr))
773 else
774 UnnamedAddr = GlobalValue::UnnamedAddr::None;
775 return false;
776}
777
778/// parseUnnamedGlobal:
779/// OptionalVisibility (ALIAS | IFUNC) ...
780/// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
781/// OptionalDLLStorageClass
782/// ... -> global variable
783/// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
784/// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier
785/// OptionalVisibility
786/// OptionalDLLStorageClass
787/// ... -> global variable
788bool LLParser::parseUnnamedGlobal() {
789 unsigned VarID;
790 std::string Name;
791 LocTy NameLoc = Lex.getLoc();
792
793 // Handle the GlobalID form.
794 if (Lex.getKind() == lltok::GlobalID) {
795 VarID = Lex.getUIntVal();
796 if (checkValueID(NameLoc, "global", "@", NumberedVals.getNext(), VarID))
797 return true;
798
799 Lex.Lex(); // eat GlobalID;
800 if (parseToken(lltok::equal, "expected '=' after name"))
801 return true;
802 } else {
803 VarID = NumberedVals.getNext();
804 }
805
806 bool HasLinkage;
807 unsigned Linkage, Visibility, DLLStorageClass;
808 bool DSOLocal;
810 GlobalVariable::UnnamedAddr UnnamedAddr;
811 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
812 DSOLocal) ||
813 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
814 return true;
815
816 switch (Lex.getKind()) {
817 default:
818 return parseGlobal(Name, VarID, NameLoc, Linkage, HasLinkage, Visibility,
819 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
820 case lltok::kw_alias:
821 case lltok::kw_ifunc:
822 return parseAliasOrIFunc(Name, VarID, NameLoc, Linkage, Visibility,
823 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
824 }
825}
826
827/// parseNamedGlobal:
828/// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
829/// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
830/// OptionalVisibility OptionalDLLStorageClass
831/// ... -> global variable
832bool LLParser::parseNamedGlobal() {
834 LocTy NameLoc = Lex.getLoc();
835 std::string Name = Lex.getStrVal();
836 Lex.Lex();
837
838 bool HasLinkage;
839 unsigned Linkage, Visibility, DLLStorageClass;
840 bool DSOLocal;
842 GlobalVariable::UnnamedAddr UnnamedAddr;
843 if (parseToken(lltok::equal, "expected '=' in global variable") ||
844 parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
845 DSOLocal) ||
846 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
847 return true;
848
849 switch (Lex.getKind()) {
850 default:
851 return parseGlobal(Name, -1, NameLoc, Linkage, HasLinkage, Visibility,
852 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
853 case lltok::kw_alias:
854 case lltok::kw_ifunc:
855 return parseAliasOrIFunc(Name, -1, NameLoc, Linkage, Visibility,
856 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
857 }
858}
859
860bool LLParser::parseComdat() {
862 std::string Name = Lex.getStrVal();
863 LocTy NameLoc = Lex.getLoc();
864 Lex.Lex();
865
866 if (parseToken(lltok::equal, "expected '=' here"))
867 return true;
868
869 if (parseToken(lltok::kw_comdat, "expected comdat keyword"))
870 return tokError("expected comdat type");
871
873 switch (Lex.getKind()) {
874 default:
875 return tokError("unknown selection kind");
876 case lltok::kw_any:
877 SK = Comdat::Any;
878 break;
881 break;
883 SK = Comdat::Largest;
884 break;
887 break;
889 SK = Comdat::SameSize;
890 break;
891 }
892 Lex.Lex();
893
894 // See if the comdat was forward referenced, if so, use the comdat.
895 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
897 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
898 return error(NameLoc, "redefinition of comdat '$" + Name + "'");
899
900 Comdat *C;
901 if (I != ComdatSymTab.end())
902 C = &I->second;
903 else
904 C = M->getOrInsertComdat(Name);
905 C->setSelectionKind(SK);
906
907 return false;
908}
909
910// MDString:
911// ::= '!' STRINGCONSTANT
912bool LLParser::parseMDString(MDString *&Result) {
913 std::string Str;
914 if (parseStringConstant(Str))
915 return true;
916 Result = MDString::get(Context, Str);
917 return false;
918}
919
920// MDNode:
921// ::= '!' MDNodeNumber
922bool LLParser::parseMDNodeID(MDNode *&Result) {
923 // !{ ..., !42, ... }
924 LocTy IDLoc = Lex.getLoc();
925 unsigned MID = 0;
926 if (parseUInt32(MID))
927 return true;
928
929 // If not a forward reference, just return it now.
930 if (NumberedMetadata.count(MID)) {
931 Result = NumberedMetadata[MID];
932 return false;
933 }
934
935 // Otherwise, create MDNode forward reference.
936 auto &FwdRef = ForwardRefMDNodes[MID];
937 FwdRef = std::make_pair(MDTuple::getTemporary(Context, std::nullopt), IDLoc);
938
939 Result = FwdRef.first.get();
940 NumberedMetadata[MID].reset(Result);
941 return false;
942}
943
944/// parseNamedMetadata:
945/// !foo = !{ !1, !2 }
946bool LLParser::parseNamedMetadata() {
948 std::string Name = Lex.getStrVal();
949 Lex.Lex();
950
951 if (parseToken(lltok::equal, "expected '=' here") ||
952 parseToken(lltok::exclaim, "Expected '!' here") ||
953 parseToken(lltok::lbrace, "Expected '{' here"))
954 return true;
955
956 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
957 if (Lex.getKind() != lltok::rbrace)
958 do {
959 MDNode *N = nullptr;
960 // parse DIExpressions inline as a special case. They are still MDNodes,
961 // so they can still appear in named metadata. Remove this logic if they
962 // become plain Metadata.
963 if (Lex.getKind() == lltok::MetadataVar &&
964 Lex.getStrVal() == "DIExpression") {
965 if (parseDIExpression(N, /*IsDistinct=*/false))
966 return true;
967 // DIArgLists should only appear inline in a function, as they may
968 // contain LocalAsMetadata arguments which require a function context.
969 } else if (Lex.getKind() == lltok::MetadataVar &&
970 Lex.getStrVal() == "DIArgList") {
971 return tokError("found DIArgList outside of function");
972 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
973 parseMDNodeID(N)) {
974 return true;
975 }
976 NMD->addOperand(N);
977 } while (EatIfPresent(lltok::comma));
978
979 return parseToken(lltok::rbrace, "expected end of metadata node");
980}
981
982/// parseStandaloneMetadata:
983/// !42 = !{...}
984bool LLParser::parseStandaloneMetadata() {
985 assert(Lex.getKind() == lltok::exclaim);
986 Lex.Lex();
987 unsigned MetadataID = 0;
988
989 MDNode *Init;
990 if (parseUInt32(MetadataID) || parseToken(lltok::equal, "expected '=' here"))
991 return true;
992
993 // Detect common error, from old metadata syntax.
994 if (Lex.getKind() == lltok::Type)
995 return tokError("unexpected type in metadata definition");
996
997 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
998 if (Lex.getKind() == lltok::MetadataVar) {
999 if (parseSpecializedMDNode(Init, IsDistinct))
1000 return true;
1001 } else if (parseToken(lltok::exclaim, "Expected '!' here") ||
1002 parseMDTuple(Init, IsDistinct))
1003 return true;
1004
1005 // See if this was forward referenced, if so, handle it.
1006 auto FI = ForwardRefMDNodes.find(MetadataID);
1007 if (FI != ForwardRefMDNodes.end()) {
1008 auto *ToReplace = FI->second.first.get();
1009 // DIAssignID has its own special forward-reference "replacement" for
1010 // attachments (the temporary attachments are never actually attached).
1011 if (isa<DIAssignID>(Init)) {
1012 for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) {
1013 assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) &&
1014 "Inst unexpectedly already has DIAssignID attachment");
1015 Inst->setMetadata(LLVMContext::MD_DIAssignID, Init);
1016 }
1017 }
1018
1019 ToReplace->replaceAllUsesWith(Init);
1020 ForwardRefMDNodes.erase(FI);
1021
1022 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
1023 } else {
1024 if (NumberedMetadata.count(MetadataID))
1025 return tokError("Metadata id is already used");
1026 NumberedMetadata[MetadataID].reset(Init);
1027 }
1028
1029 return false;
1030}
1031
1032// Skips a single module summary entry.
1033bool LLParser::skipModuleSummaryEntry() {
1034 // Each module summary entry consists of a tag for the entry
1035 // type, followed by a colon, then the fields which may be surrounded by
1036 // nested sets of parentheses. The "tag:" looks like a Label. Once parsing
1037 // support is in place we will look for the tokens corresponding to the
1038 // expected tags.
1039 if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module &&
1040 Lex.getKind() != lltok::kw_typeid && Lex.getKind() != lltok::kw_flags &&
1042 return tokError(
1043 "Expected 'gv', 'module', 'typeid', 'flags' or 'blockcount' at the "
1044 "start of summary entry");
1045 if (Lex.getKind() == lltok::kw_flags)
1046 return parseSummaryIndexFlags();
1047 if (Lex.getKind() == lltok::kw_blockcount)
1048 return parseBlockCount();
1049 Lex.Lex();
1050 if (parseToken(lltok::colon, "expected ':' at start of summary entry") ||
1051 parseToken(lltok::lparen, "expected '(' at start of summary entry"))
1052 return true;
1053 // Now walk through the parenthesized entry, until the number of open
1054 // parentheses goes back down to 0 (the first '(' was parsed above).
1055 unsigned NumOpenParen = 1;
1056 do {
1057 switch (Lex.getKind()) {
1058 case lltok::lparen:
1059 NumOpenParen++;
1060 break;
1061 case lltok::rparen:
1062 NumOpenParen--;
1063 break;
1064 case lltok::Eof:
1065 return tokError("found end of file while parsing summary entry");
1066 default:
1067 // Skip everything in between parentheses.
1068 break;
1069 }
1070 Lex.Lex();
1071 } while (NumOpenParen > 0);
1072 return false;
1073}
1074
1075/// SummaryEntry
1076/// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
1077bool LLParser::parseSummaryEntry() {
1079 unsigned SummaryID = Lex.getUIntVal();
1080
1081 // For summary entries, colons should be treated as distinct tokens,
1082 // not an indication of the end of a label token.
1084
1085 Lex.Lex();
1086 if (parseToken(lltok::equal, "expected '=' here"))
1087 return true;
1088
1089 // If we don't have an index object, skip the summary entry.
1090 if (!Index)
1091 return skipModuleSummaryEntry();
1092
1093 bool result = false;
1094 switch (Lex.getKind()) {
1095 case lltok::kw_gv:
1096 result = parseGVEntry(SummaryID);
1097 break;
1098 case lltok::kw_module:
1099 result = parseModuleEntry(SummaryID);
1100 break;
1101 case lltok::kw_typeid:
1102 result = parseTypeIdEntry(SummaryID);
1103 break;
1105 result = parseTypeIdCompatibleVtableEntry(SummaryID);
1106 break;
1107 case lltok::kw_flags:
1108 result = parseSummaryIndexFlags();
1109 break;
1111 result = parseBlockCount();
1112 break;
1113 default:
1114 result = error(Lex.getLoc(), "unexpected summary kind");
1115 break;
1116 }
1117 Lex.setIgnoreColonInIdentifiers(false);
1118 return result;
1119}
1120
1121static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
1124}
1125static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L) {
1128}
1129
1130// If there was an explicit dso_local, update GV. In the absence of an explicit
1131// dso_local we keep the default value.
1132static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
1133 if (DSOLocal)
1134 GV.setDSOLocal(true);
1135}
1136
1137/// parseAliasOrIFunc:
1138/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1139/// OptionalVisibility OptionalDLLStorageClass
1140/// OptionalThreadLocal OptionalUnnamedAddr
1141/// 'alias|ifunc' AliaseeOrResolver SymbolAttrs*
1142///
1143/// AliaseeOrResolver
1144/// ::= TypeAndValue
1145///
1146/// SymbolAttrs
1147/// ::= ',' 'partition' StringConstant
1148///
1149/// Everything through OptionalUnnamedAddr has already been parsed.
1150///
1151bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID,
1152 LocTy NameLoc, unsigned L, unsigned Visibility,
1153 unsigned DLLStorageClass, bool DSOLocal,
1155 GlobalVariable::UnnamedAddr UnnamedAddr) {
1156 bool IsAlias;
1157 if (Lex.getKind() == lltok::kw_alias)
1158 IsAlias = true;
1159 else if (Lex.getKind() == lltok::kw_ifunc)
1160 IsAlias = false;
1161 else
1162 llvm_unreachable("Not an alias or ifunc!");
1163 Lex.Lex();
1164
1166
1167 if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
1168 return error(NameLoc, "invalid linkage type for alias");
1169
1170 if (!isValidVisibilityForLinkage(Visibility, L))
1171 return error(NameLoc,
1172 "symbol with local linkage must have default visibility");
1173
1174 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, L))
1175 return error(NameLoc,
1176 "symbol with local linkage cannot have a DLL storage class");
1177
1178 Type *Ty;
1179 LocTy ExplicitTypeLoc = Lex.getLoc();
1180 if (parseType(Ty) ||
1181 parseToken(lltok::comma, "expected comma after alias or ifunc's type"))
1182 return true;
1183
1184 Constant *Aliasee;
1185 LocTy AliaseeLoc = Lex.getLoc();
1186 if (Lex.getKind() != lltok::kw_bitcast &&
1189 Lex.getKind() != lltok::kw_inttoptr) {
1190 if (parseGlobalTypeAndValue(Aliasee))
1191 return true;
1192 } else {
1193 // The bitcast dest type is not present, it is implied by the dest type.
1194 ValID ID;
1195 if (parseValID(ID, /*PFS=*/nullptr))
1196 return true;
1197 if (ID.Kind != ValID::t_Constant)
1198 return error(AliaseeLoc, "invalid aliasee");
1199 Aliasee = ID.ConstantVal;
1200 }
1201
1202 Type *AliaseeType = Aliasee->getType();
1203 auto *PTy = dyn_cast<PointerType>(AliaseeType);
1204 if (!PTy)
1205 return error(AliaseeLoc, "An alias or ifunc must have pointer type");
1206 unsigned AddrSpace = PTy->getAddressSpace();
1207
1208 GlobalValue *GVal = nullptr;
1209
1210 // See if the alias was forward referenced, if so, prepare to replace the
1211 // forward reference.
1212 if (!Name.empty()) {
1213 auto I = ForwardRefVals.find(Name);
1214 if (I != ForwardRefVals.end()) {
1215 GVal = I->second.first;
1216 ForwardRefVals.erase(Name);
1217 } else if (M->getNamedValue(Name)) {
1218 return error(NameLoc, "redefinition of global '@" + Name + "'");
1219 }
1220 } else {
1221 auto I = ForwardRefValIDs.find(NameID);
1222 if (I != ForwardRefValIDs.end()) {
1223 GVal = I->second.first;
1224 ForwardRefValIDs.erase(I);
1225 }
1226 }
1227
1228 // Okay, create the alias/ifunc but do not insert it into the module yet.
1229 std::unique_ptr<GlobalAlias> GA;
1230 std::unique_ptr<GlobalIFunc> GI;
1231 GlobalValue *GV;
1232 if (IsAlias) {
1233 GA.reset(GlobalAlias::create(Ty, AddrSpace,
1235 Aliasee, /*Parent*/ nullptr));
1236 GV = GA.get();
1237 } else {
1238 GI.reset(GlobalIFunc::create(Ty, AddrSpace,
1240 Aliasee, /*Parent*/ nullptr));
1241 GV = GI.get();
1242 }
1243 GV->setThreadLocalMode(TLM);
1246 GV->setUnnamedAddr(UnnamedAddr);
1247 maybeSetDSOLocal(DSOLocal, *GV);
1248
1249 // At this point we've parsed everything except for the IndirectSymbolAttrs.
1250 // Now parse them if there are any.
1251 while (Lex.getKind() == lltok::comma) {
1252 Lex.Lex();
1253
1254 if (Lex.getKind() == lltok::kw_partition) {
1255 Lex.Lex();
1256 GV->setPartition(Lex.getStrVal());
1257 if (parseToken(lltok::StringConstant, "expected partition string"))
1258 return true;
1259 } else {
1260 return tokError("unknown alias or ifunc property!");
1261 }
1262 }
1263
1264 if (Name.empty())
1265 NumberedVals.add(NameID, GV);
1266
1267 if (GVal) {
1268 // Verify that types agree.
1269 if (GVal->getType() != GV->getType())
1270 return error(
1271 ExplicitTypeLoc,
1272 "forward reference and definition of alias have different types");
1273
1274 // If they agree, just RAUW the old value with the alias and remove the
1275 // forward ref info.
1276 GVal->replaceAllUsesWith(GV);
1277 GVal->eraseFromParent();
1278 }
1279
1280 // Insert into the module, we know its name won't collide now.
1281 if (IsAlias)
1282 M->insertAlias(GA.release());
1283 else
1284 M->insertIFunc(GI.release());
1285 assert(GV->getName() == Name && "Should not be a name conflict!");
1286
1287 return false;
1288}
1289
1290static bool isSanitizer(lltok::Kind Kind) {
1291 switch (Kind) {
1294 case lltok::kw_sanitize_memtag:
1296 return true;
1297 default:
1298 return false;
1299 }
1300}
1301
1302bool LLParser::parseSanitizer(GlobalVariable *GV) {
1305 if (GV->hasSanitizerMetadata())
1306 Meta = GV->getSanitizerMetadata();
1307
1308 switch (Lex.getKind()) {
1310 Meta.NoAddress = true;
1311 break;
1313 Meta.NoHWAddress = true;
1314 break;
1315 case lltok::kw_sanitize_memtag:
1316 Meta.Memtag = true;
1317 break;
1319 Meta.IsDynInit = true;
1320 break;
1321 default:
1322 return tokError("non-sanitizer token passed to LLParser::parseSanitizer()");
1323 }
1324 GV->setSanitizerMetadata(Meta);
1325 Lex.Lex();
1326 return false;
1327}
1328
1329/// parseGlobal
1330/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1331/// OptionalVisibility OptionalDLLStorageClass
1332/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
1333/// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
1334/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
1335/// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
1336/// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
1337/// Const OptionalAttrs
1338///
1339/// Everything up to and including OptionalUnnamedAddr has been parsed
1340/// already.
1341///
1342bool LLParser::parseGlobal(const std::string &Name, unsigned NameID,
1343 LocTy NameLoc, unsigned Linkage, bool HasLinkage,
1344 unsigned Visibility, unsigned DLLStorageClass,
1345 bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
1346 GlobalVariable::UnnamedAddr UnnamedAddr) {
1347 if (!isValidVisibilityForLinkage(Visibility, Linkage))
1348 return error(NameLoc,
1349 "symbol with local linkage must have default visibility");
1350
1351 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
1352 return error(NameLoc,
1353 "symbol with local linkage cannot have a DLL storage class");
1354
1355 unsigned AddrSpace;
1356 bool IsConstant, IsExternallyInitialized;
1357 LocTy IsExternallyInitializedLoc;
1358 LocTy TyLoc;
1359
1360 Type *Ty = nullptr;
1361 if (parseOptionalAddrSpace(AddrSpace) ||
1362 parseOptionalToken(lltok::kw_externally_initialized,
1363 IsExternallyInitialized,
1364 &IsExternallyInitializedLoc) ||
1365 parseGlobalType(IsConstant) || parseType(Ty, TyLoc))
1366 return true;
1367
1368 // If the linkage is specified and is external, then no initializer is
1369 // present.
1370 Constant *Init = nullptr;
1371 if (!HasLinkage ||
1373 (GlobalValue::LinkageTypes)Linkage)) {
1374 if (parseGlobalValue(Ty, Init))
1375 return true;
1376 }
1377
1379 return error(TyLoc, "invalid type for global variable");
1380
1381 GlobalValue *GVal = nullptr;
1382
1383 // See if the global was forward referenced, if so, use the global.
1384 if (!Name.empty()) {
1385 auto I = ForwardRefVals.find(Name);
1386 if (I != ForwardRefVals.end()) {
1387 GVal = I->second.first;
1388 ForwardRefVals.erase(I);
1389 } else if (M->getNamedValue(Name)) {
1390 return error(NameLoc, "redefinition of global '@" + Name + "'");
1391 }
1392 } else {
1393 // Handle @"", where a name is syntactically specified, but semantically
1394 // missing.
1395 if (NameID == (unsigned)-1)
1396 NameID = NumberedVals.getNext();
1397
1398 auto I = ForwardRefValIDs.find(NameID);
1399 if (I != ForwardRefValIDs.end()) {
1400 GVal = I->second.first;
1401 ForwardRefValIDs.erase(I);
1402 }
1403 }
1404
1406 *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr,
1408
1409 if (Name.empty())
1410 NumberedVals.add(NameID, GV);
1411
1412 // Set the parsed properties on the global.
1413 if (Init)
1414 GV->setInitializer(Init);
1415 GV->setConstant(IsConstant);
1417 maybeSetDSOLocal(DSOLocal, *GV);
1420 GV->setExternallyInitialized(IsExternallyInitialized);
1421 GV->setThreadLocalMode(TLM);
1422 GV->setUnnamedAddr(UnnamedAddr);
1423
1424 if (GVal) {
1425 if (GVal->getAddressSpace() != AddrSpace)
1426 return error(
1427 TyLoc,
1428 "forward reference and definition of global have different types");
1429
1430 GVal->replaceAllUsesWith(GV);
1431 GVal->eraseFromParent();
1432 }
1433
1434 // parse attributes on the global.
1435 while (Lex.getKind() == lltok::comma) {
1436 Lex.Lex();
1437
1438 if (Lex.getKind() == lltok::kw_section) {
1439 Lex.Lex();
1440 GV->setSection(Lex.getStrVal());
1441 if (parseToken(lltok::StringConstant, "expected global section string"))
1442 return true;
1443 } else if (Lex.getKind() == lltok::kw_partition) {
1444 Lex.Lex();
1445 GV->setPartition(Lex.getStrVal());
1446 if (parseToken(lltok::StringConstant, "expected partition string"))
1447 return true;
1448 } else if (Lex.getKind() == lltok::kw_align) {
1449 MaybeAlign Alignment;
1450 if (parseOptionalAlignment(Alignment))
1451 return true;
1452 if (Alignment)
1453 GV->setAlignment(*Alignment);
1454 } else if (Lex.getKind() == lltok::kw_code_model) {
1456 if (parseOptionalCodeModel(CodeModel))
1457 return true;
1458 GV->setCodeModel(CodeModel);
1459 } else if (Lex.getKind() == lltok::MetadataVar) {
1460 if (parseGlobalObjectMetadataAttachment(*GV))
1461 return true;
1462 } else if (isSanitizer(Lex.getKind())) {
1463 if (parseSanitizer(GV))
1464 return true;
1465 } else {
1466 Comdat *C;
1467 if (parseOptionalComdat(Name, C))
1468 return true;
1469 if (C)
1470 GV->setComdat(C);
1471 else
1472 return tokError("unknown global variable property!");
1473 }
1474 }
1475
1476 AttrBuilder Attrs(M->getContext());
1477 LocTy BuiltinLoc;
1478 std::vector<unsigned> FwdRefAttrGrps;
1479 if (parseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
1480 return true;
1481 if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
1482 GV->setAttributes(AttributeSet::get(Context, Attrs));
1483 ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
1484 }
1485
1486 return false;
1487}
1488
1489/// parseUnnamedAttrGrp
1490/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
1491bool LLParser::parseUnnamedAttrGrp() {
1493 LocTy AttrGrpLoc = Lex.getLoc();
1494 Lex.Lex();
1495
1496 if (Lex.getKind() != lltok::AttrGrpID)
1497 return tokError("expected attribute group id");
1498
1499 unsigned VarID = Lex.getUIntVal();
1500 std::vector<unsigned> unused;
1501 LocTy BuiltinLoc;
1502 Lex.Lex();
1503
1504 if (parseToken(lltok::equal, "expected '=' here") ||
1505 parseToken(lltok::lbrace, "expected '{' here"))
1506 return true;
1507
1508 auto R = NumberedAttrBuilders.find(VarID);
1509 if (R == NumberedAttrBuilders.end())
1510 R = NumberedAttrBuilders.emplace(VarID, AttrBuilder(M->getContext())).first;
1511
1512 if (parseFnAttributeValuePairs(R->second, unused, true, BuiltinLoc) ||
1513 parseToken(lltok::rbrace, "expected end of attribute group"))
1514 return true;
1515
1516 if (!R->second.hasAttributes())
1517 return error(AttrGrpLoc, "attribute group has no attributes");
1518
1519 return false;
1520}
1521
1523 switch (Kind) {
1524#define GET_ATTR_NAMES
1525#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
1526 case lltok::kw_##DISPLAY_NAME: \
1527 return Attribute::ENUM_NAME;
1528#include "llvm/IR/Attributes.inc"
1529 default:
1530 return Attribute::None;
1531 }
1532}
1533
1534bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
1535 bool InAttrGroup) {
1536 if (Attribute::isTypeAttrKind(Attr))
1537 return parseRequiredTypeAttr(B, Lex.getKind(), Attr);
1538
1539 switch (Attr) {
1540 case Attribute::Alignment: {
1541 MaybeAlign Alignment;
1542 if (InAttrGroup) {
1543 uint32_t Value = 0;
1544 Lex.Lex();
1545 if (parseToken(lltok::equal, "expected '=' here") || parseUInt32(Value))
1546 return true;
1547 Alignment = Align(Value);
1548 } else {
1549 if (parseOptionalAlignment(Alignment, true))
1550 return true;
1551 }
1552 B.addAlignmentAttr(Alignment);
1553 return false;
1554 }
1555 case Attribute::StackAlignment: {
1556 unsigned Alignment;
1557 if (InAttrGroup) {
1558 Lex.Lex();
1559 if (parseToken(lltok::equal, "expected '=' here") ||
1560 parseUInt32(Alignment))
1561 return true;
1562 } else {
1563 if (parseOptionalStackAlignment(Alignment))
1564 return true;
1565 }
1566 B.addStackAlignmentAttr(Alignment);
1567 return false;
1568 }
1569 case Attribute::AllocSize: {
1570 unsigned ElemSizeArg;
1571 std::optional<unsigned> NumElemsArg;
1572 if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
1573 return true;
1574 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1575 return false;
1576 }
1577 case Attribute::VScaleRange: {
1578 unsigned MinValue, MaxValue;
1579 if (parseVScaleRangeArguments(MinValue, MaxValue))
1580 return true;
1581 B.addVScaleRangeAttr(MinValue,
1582 MaxValue > 0 ? MaxValue : std::optional<unsigned>());
1583 return false;
1584 }
1585 case Attribute::Dereferenceable: {
1586 uint64_t Bytes;
1587 if (parseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
1588 return true;
1589 B.addDereferenceableAttr(Bytes);
1590 return false;
1591 }
1592 case Attribute::DereferenceableOrNull: {
1593 uint64_t Bytes;
1594 if (parseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1595 return true;
1596 B.addDereferenceableOrNullAttr(Bytes);
1597 return false;
1598 }
1599 case Attribute::UWTable: {
1601 if (parseOptionalUWTableKind(Kind))
1602 return true;
1603 B.addUWTableAttr(Kind);
1604 return false;
1605 }
1606 case Attribute::AllocKind: {
1608 if (parseAllocKind(Kind))
1609 return true;
1610 B.addAllocKindAttr(Kind);
1611 return false;
1612 }
1613 case Attribute::Memory: {
1614 std::optional<MemoryEffects> ME = parseMemoryAttr();
1615 if (!ME)
1616 return true;
1617 B.addMemoryAttr(*ME);
1618 return false;
1619 }
1620 case Attribute::NoFPClass: {
1621 if (FPClassTest NoFPClass =
1622 static_cast<FPClassTest>(parseNoFPClassAttr())) {
1623 B.addNoFPClassAttr(NoFPClass);
1624 return false;
1625 }
1626
1627 return true;
1628 }
1629 case Attribute::Range:
1630 return parseRangeAttr(B);
1631 default:
1632 B.addAttribute(Attr);
1633 Lex.Lex();
1634 return false;
1635 }
1636}
1637
1639 switch (Kind) {
1640 case lltok::kw_readnone:
1641 ME &= MemoryEffects::none();
1642 return true;
1643 case lltok::kw_readonly:
1645 return true;
1646 case lltok::kw_writeonly:
1648 return true;
1651 return true;
1654 return true;
1657 return true;
1658 default:
1659 return false;
1660 }
1661}
1662
1663/// parseFnAttributeValuePairs
1664/// ::= <attr> | <attr> '=' <value>
1665bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
1666 std::vector<unsigned> &FwdRefAttrGrps,
1667 bool InAttrGrp, LocTy &BuiltinLoc) {
1668 bool HaveError = false;
1669
1670 B.clear();
1671
1673 while (true) {
1674 lltok::Kind Token = Lex.getKind();
1675 if (Token == lltok::rbrace)
1676 break; // Finished.
1677
1678 if (Token == lltok::StringConstant) {
1679 if (parseStringAttribute(B))
1680 return true;
1681 continue;
1682 }
1683
1684 if (Token == lltok::AttrGrpID) {
1685 // Allow a function to reference an attribute group:
1686 //
1687 // define void @foo() #1 { ... }
1688 if (InAttrGrp) {
1689 HaveError |= error(
1690 Lex.getLoc(),
1691 "cannot have an attribute group reference in an attribute group");
1692 } else {
1693 // Save the reference to the attribute group. We'll fill it in later.
1694 FwdRefAttrGrps.push_back(Lex.getUIntVal());
1695 }
1696 Lex.Lex();
1697 continue;
1698 }
1699
1700 SMLoc Loc = Lex.getLoc();
1701 if (Token == lltok::kw_builtin)
1702 BuiltinLoc = Loc;
1703
1704 if (upgradeMemoryAttr(ME, Token)) {
1705 Lex.Lex();
1706 continue;
1707 }
1708
1710 if (Attr == Attribute::None) {
1711 if (!InAttrGrp)
1712 break;
1713 return error(Lex.getLoc(), "unterminated attribute group");
1714 }
1715
1716 if (parseEnumAttribute(Attr, B, InAttrGrp))
1717 return true;
1718
1719 // As a hack, we allow function alignment to be initially parsed as an
1720 // attribute on a function declaration/definition or added to an attribute
1721 // group and later moved to the alignment field.
1722 if (!Attribute::canUseAsFnAttr(Attr) && Attr != Attribute::Alignment)
1723 HaveError |= error(Loc, "this attribute does not apply to functions");
1724 }
1725
1726 if (ME != MemoryEffects::unknown())
1727 B.addMemoryAttr(ME);
1728 return HaveError;
1729}
1730
1731//===----------------------------------------------------------------------===//
1732// GlobalValue Reference/Resolution Routines.
1733//===----------------------------------------------------------------------===//
1734
1736 // The used global type does not matter. We will later RAUW it with a
1737 // global/function of the correct type.
1738 return new GlobalVariable(*M, Type::getInt8Ty(M->getContext()), false,
1741 PTy->getAddressSpace());
1742}
1743
1744Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
1745 Value *Val) {
1746 Type *ValTy = Val->getType();
1747 if (ValTy == Ty)
1748 return Val;
1749 if (Ty->isLabelTy())
1750 error(Loc, "'" + Name + "' is not a basic block");
1751 else
1752 error(Loc, "'" + Name + "' defined with type '" +
1753 getTypeString(Val->getType()) + "' but expected '" +
1754 getTypeString(Ty) + "'");
1755 return nullptr;
1756}
1757
1758/// getGlobalVal - Get a value with the specified name or ID, creating a
1759/// forward reference record if needed. This can return null if the value
1760/// exists but does not have the right type.
1761GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty,
1762 LocTy Loc) {
1763 PointerType *PTy = dyn_cast<PointerType>(Ty);
1764 if (!PTy) {
1765 error(Loc, "global variable reference must have pointer type");
1766 return nullptr;
1767 }
1768
1769 // Look this name up in the normal function symbol table.
1770 GlobalValue *Val =
1771 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
1772
1773 // If this is a forward reference for the value, see if we already created a
1774 // forward ref record.
1775 if (!Val) {
1776 auto I = ForwardRefVals.find(Name);
1777 if (I != ForwardRefVals.end())
1778 Val = I->second.first;
1779 }
1780
1781 // If we have the value in the symbol table or fwd-ref table, return it.
1782 if (Val)
1783 return cast_or_null<GlobalValue>(
1784 checkValidVariableType(Loc, "@" + Name, Ty, Val));
1785
1786 // Otherwise, create a new forward reference for this value and remember it.
1787 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1788 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1789 return FwdVal;
1790}
1791
1792GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1793 PointerType *PTy = dyn_cast<PointerType>(Ty);
1794 if (!PTy) {
1795 error(Loc, "global variable reference must have pointer type");
1796 return nullptr;
1797 }
1798
1799 GlobalValue *Val = NumberedVals.get(ID);
1800
1801 // If this is a forward reference for the value, see if we already created a
1802 // forward ref record.
1803 if (!Val) {
1804 auto I = ForwardRefValIDs.find(ID);
1805 if (I != ForwardRefValIDs.end())
1806 Val = I->second.first;
1807 }
1808
1809 // If we have the value in the symbol table or fwd-ref table, return it.
1810 if (Val)
1811 return cast_or_null<GlobalValue>(
1812 checkValidVariableType(Loc, "@" + Twine(ID), Ty, Val));
1813
1814 // Otherwise, create a new forward reference for this value and remember it.
1815 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1816 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1817 return FwdVal;
1818}
1819
1820//===----------------------------------------------------------------------===//
1821// Comdat Reference/Resolution Routines.
1822//===----------------------------------------------------------------------===//
1823
1824Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1825 // Look this name up in the comdat symbol table.
1826 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1828 if (I != ComdatSymTab.end())
1829 return &I->second;
1830
1831 // Otherwise, create a new forward reference for this value and remember it.
1832 Comdat *C = M->getOrInsertComdat(Name);
1833 ForwardRefComdats[Name] = Loc;
1834 return C;
1835}
1836
1837//===----------------------------------------------------------------------===//
1838// Helper Routines.
1839//===----------------------------------------------------------------------===//
1840
1841/// parseToken - If the current token has the specified kind, eat it and return
1842/// success. Otherwise, emit the specified error and return failure.
1843bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) {
1844 if (Lex.getKind() != T)
1845 return tokError(ErrMsg);
1846 Lex.Lex();
1847 return false;
1848}
1849
1850/// parseStringConstant
1851/// ::= StringConstant
1852bool LLParser::parseStringConstant(std::string &Result) {
1853 if (Lex.getKind() != lltok::StringConstant)
1854 return tokError("expected string constant");
1855 Result = Lex.getStrVal();
1856 Lex.Lex();
1857 return false;
1858}
1859
1860/// parseUInt32
1861/// ::= uint32
1862bool LLParser::parseUInt32(uint32_t &Val) {
1863 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1864 return tokError("expected integer");
1865 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1866 if (Val64 != unsigned(Val64))
1867 return tokError("expected 32-bit integer (too large)");
1868 Val = Val64;
1869 Lex.Lex();
1870 return false;
1871}
1872
1873/// parseUInt64
1874/// ::= uint64
1875bool LLParser::parseUInt64(uint64_t &Val) {
1876 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1877 return tokError("expected integer");
1878 Val = Lex.getAPSIntVal().getLimitedValue();
1879 Lex.Lex();
1880 return false;
1881}
1882
1883/// parseTLSModel
1884/// := 'localdynamic'
1885/// := 'initialexec'
1886/// := 'localexec'
1887bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1888 switch (Lex.getKind()) {
1889 default:
1890 return tokError("expected localdynamic, initialexec or localexec");
1893 break;
1896 break;
1899 break;
1900 }
1901
1902 Lex.Lex();
1903 return false;
1904}
1905
1906/// parseOptionalThreadLocal
1907/// := /*empty*/
1908/// := 'thread_local'
1909/// := 'thread_local' '(' tlsmodel ')'
1910bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1912 if (!EatIfPresent(lltok::kw_thread_local))
1913 return false;
1914
1916 if (Lex.getKind() == lltok::lparen) {
1917 Lex.Lex();
1918 return parseTLSModel(TLM) ||
1919 parseToken(lltok::rparen, "expected ')' after thread local model");
1920 }
1921 return false;
1922}
1923
1924/// parseOptionalAddrSpace
1925/// := /*empty*/
1926/// := 'addrspace' '(' uint32 ')'
1927bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
1928 AddrSpace = DefaultAS;
1929 if (!EatIfPresent(lltok::kw_addrspace))
1930 return false;
1931
1932 auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool {
1933 if (Lex.getKind() == lltok::StringConstant) {
1934 auto AddrSpaceStr = Lex.getStrVal();
1935 if (AddrSpaceStr == "A") {
1936 AddrSpace = M->getDataLayout().getAllocaAddrSpace();
1937 } else if (AddrSpaceStr == "G") {
1938 AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace();
1939 } else if (AddrSpaceStr == "P") {
1940 AddrSpace = M->getDataLayout().getProgramAddressSpace();
1941 } else {
1942 return tokError("invalid symbolic addrspace '" + AddrSpaceStr + "'");
1943 }
1944 Lex.Lex();
1945 return false;
1946 }
1947 if (Lex.getKind() != lltok::APSInt)
1948 return tokError("expected integer or string constant");
1949 SMLoc Loc = Lex.getLoc();
1950 if (parseUInt32(AddrSpace))
1951 return true;
1952 if (!isUInt<24>(AddrSpace))
1953 return error(Loc, "invalid address space, must be a 24-bit integer");
1954 return false;
1955 };
1956
1957 return parseToken(lltok::lparen, "expected '(' in address space") ||
1958 ParseAddrspaceValue(AddrSpace) ||
1959 parseToken(lltok::rparen, "expected ')' in address space");
1960}
1961
1962/// parseStringAttribute
1963/// := StringConstant
1964/// := StringConstant '=' StringConstant
1965bool LLParser::parseStringAttribute(AttrBuilder &B) {
1966 std::string Attr = Lex.getStrVal();
1967 Lex.Lex();
1968 std::string Val;
1969 if (EatIfPresent(lltok::equal) && parseStringConstant(Val))
1970 return true;
1971 B.addAttribute(Attr, Val);
1972 return false;
1973}
1974
1975/// Parse a potentially empty list of parameter or return attributes.
1976bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) {
1977 bool HaveError = false;
1978
1979 B.clear();
1980
1981 while (true) {
1982 lltok::Kind Token = Lex.getKind();
1983 if (Token == lltok::StringConstant) {
1984 if (parseStringAttribute(B))
1985 return true;
1986 continue;
1987 }
1988
1989 SMLoc Loc = Lex.getLoc();
1991 if (Attr == Attribute::None)
1992 return HaveError;
1993
1994 if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false))
1995 return true;
1996
1997 if (IsParam && !Attribute::canUseAsParamAttr(Attr))
1998 HaveError |= error(Loc, "this attribute does not apply to parameters");
1999 if (!IsParam && !Attribute::canUseAsRetAttr(Attr))
2000 HaveError |= error(Loc, "this attribute does not apply to return values");
2001 }
2002}
2003
2004static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
2005 HasLinkage = true;
2006 switch (Kind) {
2007 default:
2008 HasLinkage = false;
2010 case lltok::kw_private:
2012 case lltok::kw_internal:
2014 case lltok::kw_weak:
2016 case lltok::kw_weak_odr:
2018 case lltok::kw_linkonce:
2026 case lltok::kw_common:
2030 case lltok::kw_external:
2032 }
2033}
2034
2035/// parseOptionalLinkage
2036/// ::= /*empty*/
2037/// ::= 'private'
2038/// ::= 'internal'
2039/// ::= 'weak'
2040/// ::= 'weak_odr'
2041/// ::= 'linkonce'
2042/// ::= 'linkonce_odr'
2043/// ::= 'available_externally'
2044/// ::= 'appending'
2045/// ::= 'common'
2046/// ::= 'extern_weak'
2047/// ::= 'external'
2048bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
2049 unsigned &Visibility,
2050 unsigned &DLLStorageClass, bool &DSOLocal) {
2051 Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
2052 if (HasLinkage)
2053 Lex.Lex();
2054 parseOptionalDSOLocal(DSOLocal);
2055 parseOptionalVisibility(Visibility);
2056 parseOptionalDLLStorageClass(DLLStorageClass);
2057
2058 if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
2059 return error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
2060 }
2061
2062 return false;
2063}
2064
2065void LLParser::parseOptionalDSOLocal(bool &DSOLocal) {
2066 switch (Lex.getKind()) {
2067 default:
2068 DSOLocal = false;
2069 break;
2071 DSOLocal = true;
2072 Lex.Lex();
2073 break;
2075 DSOLocal = false;
2076 Lex.Lex();
2077 break;
2078 }
2079}
2080
2081/// parseOptionalVisibility
2082/// ::= /*empty*/
2083/// ::= 'default'
2084/// ::= 'hidden'
2085/// ::= 'protected'
2086///
2087void LLParser::parseOptionalVisibility(unsigned &Res) {
2088 switch (Lex.getKind()) {
2089 default:
2091 return;
2092 case lltok::kw_default:
2094 break;
2095 case lltok::kw_hidden:
2097 break;
2100 break;
2101 }
2102 Lex.Lex();
2103}
2104
2105bool LLParser::parseOptionalImportType(lltok::Kind Kind,
2107 switch (Kind) {
2108 default:
2109 return tokError("unknown import kind. Expect definition or declaration.");
2112 return false;
2115 return false;
2116 }
2117}
2118
2119/// parseOptionalDLLStorageClass
2120/// ::= /*empty*/
2121/// ::= 'dllimport'
2122/// ::= 'dllexport'
2123///
2124void LLParser::parseOptionalDLLStorageClass(unsigned &Res) {
2125 switch (Lex.getKind()) {
2126 default:
2128 return;
2131 break;
2134 break;
2135 }
2136 Lex.Lex();
2137}
2138
2139/// parseOptionalCallingConv
2140/// ::= /*empty*/
2141/// ::= 'ccc'
2142/// ::= 'fastcc'
2143/// ::= 'intel_ocl_bicc'
2144/// ::= 'coldcc'
2145/// ::= 'cfguard_checkcc'
2146/// ::= 'x86_stdcallcc'
2147/// ::= 'x86_fastcallcc'
2148/// ::= 'x86_thiscallcc'
2149/// ::= 'x86_vectorcallcc'
2150/// ::= 'arm_apcscc'
2151/// ::= 'arm_aapcscc'
2152/// ::= 'arm_aapcs_vfpcc'
2153/// ::= 'aarch64_vector_pcs'
2154/// ::= 'aarch64_sve_vector_pcs'
2155/// ::= 'aarch64_sme_preservemost_from_x0'
2156/// ::= 'aarch64_sme_preservemost_from_x2'
2157/// ::= 'msp430_intrcc'
2158/// ::= 'avr_intrcc'
2159/// ::= 'avr_signalcc'
2160/// ::= 'ptx_kernel'
2161/// ::= 'ptx_device'
2162/// ::= 'spir_func'
2163/// ::= 'spir_kernel'
2164/// ::= 'x86_64_sysvcc'
2165/// ::= 'win64cc'
2166/// ::= 'anyregcc'
2167/// ::= 'preserve_mostcc'
2168/// ::= 'preserve_allcc'
2169/// ::= 'preserve_nonecc'
2170/// ::= 'ghccc'
2171/// ::= 'swiftcc'
2172/// ::= 'swifttailcc'
2173/// ::= 'x86_intrcc'
2174/// ::= 'hhvmcc'
2175/// ::= 'hhvm_ccc'
2176/// ::= 'cxx_fast_tlscc'
2177/// ::= 'amdgpu_vs'
2178/// ::= 'amdgpu_ls'
2179/// ::= 'amdgpu_hs'
2180/// ::= 'amdgpu_es'
2181/// ::= 'amdgpu_gs'
2182/// ::= 'amdgpu_ps'
2183/// ::= 'amdgpu_cs'
2184/// ::= 'amdgpu_cs_chain'
2185/// ::= 'amdgpu_cs_chain_preserve'
2186/// ::= 'amdgpu_kernel'
2187/// ::= 'tailcc'
2188/// ::= 'm68k_rtdcc'
2189/// ::= 'graalcc'
2190/// ::= 'riscv_vector_cc'
2191/// ::= 'cc' UINT
2192///
2193bool LLParser::parseOptionalCallingConv(unsigned &CC) {
2194 switch (Lex.getKind()) {
2195 default: CC = CallingConv::C; return false;
2196 case lltok::kw_ccc: CC = CallingConv::C; break;
2197 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
2198 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
2211 break;
2214 break;
2217 break;
2232 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
2236 case lltok::kw_hhvmcc:
2238 break;
2239 case lltok::kw_hhvm_ccc:
2241 break;
2253 break;
2256 break;
2258 case lltok::kw_tailcc: CC = CallingConv::Tail; break;
2263 break;
2264 case lltok::kw_cc: {
2265 Lex.Lex();
2266 return parseUInt32(CC);
2267 }
2268 }
2269
2270 Lex.Lex();
2271 return false;
2272}
2273
2274/// parseMetadataAttachment
2275/// ::= !dbg !42
2276bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
2277 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
2278
2279 std::string Name = Lex.getStrVal();
2280 Kind = M->getMDKindID(Name);
2281 Lex.Lex();
2282
2283 return parseMDNode(MD);
2284}
2285
2286/// parseInstructionMetadata
2287/// ::= !dbg !42 (',' !dbg !57)*
2288bool LLParser::parseInstructionMetadata(Instruction &Inst) {
2289 do {
2290 if (Lex.getKind() != lltok::MetadataVar)
2291 return tokError("expected metadata after comma");
2292
2293 unsigned MDK;
2294 MDNode *N;
2295 if (parseMetadataAttachment(MDK, N))
2296 return true;
2297
2298 if (MDK == LLVMContext::MD_DIAssignID)
2299 TempDIAssignIDAttachments[N].push_back(&Inst);
2300 else
2301 Inst.setMetadata(MDK, N);
2302
2303 if (MDK == LLVMContext::MD_tbaa)
2304 InstsWithTBAATag.push_back(&Inst);
2305
2306 // If this is the end of the list, we're done.
2307 } while (EatIfPresent(lltok::comma));
2308 return false;
2309}
2310
2311/// parseGlobalObjectMetadataAttachment
2312/// ::= !dbg !57
2313bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) {
2314 unsigned MDK;
2315 MDNode *N;
2316 if (parseMetadataAttachment(MDK, N))
2317 return true;
2318
2319 GO.addMetadata(MDK, *N);
2320 return false;
2321}
2322
2323/// parseOptionalFunctionMetadata
2324/// ::= (!dbg !57)*
2325bool LLParser::parseOptionalFunctionMetadata(Function &F) {
2326 while (Lex.getKind() == lltok::MetadataVar)
2327 if (parseGlobalObjectMetadataAttachment(F))
2328 return true;
2329 return false;
2330}
2331
2332/// parseOptionalAlignment
2333/// ::= /* empty */
2334/// ::= 'align' 4
2335bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
2336 Alignment = std::nullopt;
2337 if (!EatIfPresent(lltok::kw_align))
2338 return false;
2339 LocTy AlignLoc = Lex.getLoc();
2340 uint64_t Value = 0;
2341
2342 LocTy ParenLoc = Lex.getLoc();
2343 bool HaveParens = false;
2344 if (AllowParens) {
2345 if (EatIfPresent(lltok::lparen))
2346 HaveParens = true;
2347 }
2348
2349 if (parseUInt64(Value))
2350 return true;
2351
2352 if (HaveParens && !EatIfPresent(lltok::rparen))
2353 return error(ParenLoc, "expected ')'");
2354
2355 if (!isPowerOf2_64(Value))
2356 return error(AlignLoc, "alignment is not a power of two");
2358 return error(AlignLoc, "huge alignments are not supported yet");
2359 Alignment = Align(Value);
2360 return false;
2361}
2362
2363/// parseOptionalCodeModel
2364/// ::= /* empty */
2365/// ::= 'code_model' "large"
2366bool LLParser::parseOptionalCodeModel(CodeModel::Model &model) {
2367 Lex.Lex();
2368 auto StrVal = Lex.getStrVal();
2369 auto ErrMsg = "expected global code model string";
2370 if (StrVal == "tiny")
2371 model = CodeModel::Tiny;
2372 else if (StrVal == "small")
2373 model = CodeModel::Small;
2374 else if (StrVal == "kernel")
2375 model = CodeModel::Kernel;
2376 else if (StrVal == "medium")
2377 model = CodeModel::Medium;
2378 else if (StrVal == "large")
2379 model = CodeModel::Large;
2380 else
2381 return tokError(ErrMsg);
2382 if (parseToken(lltok::StringConstant, ErrMsg))
2383 return true;
2384 return false;
2385}
2386
2387/// parseOptionalDerefAttrBytes
2388/// ::= /* empty */
2389/// ::= AttrKind '(' 4 ')'
2390///
2391/// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
2392bool LLParser::parseOptionalDerefAttrBytes(lltok::Kind AttrKind,
2393 uint64_t &Bytes) {
2394 assert((AttrKind == lltok::kw_dereferenceable ||
2395 AttrKind == lltok::kw_dereferenceable_or_null) &&
2396 "contract!");
2397
2398 Bytes = 0;
2399 if (!EatIfPresent(AttrKind))
2400 return false;
2401 LocTy ParenLoc = Lex.getLoc();
2402 if (!EatIfPresent(lltok::lparen))
2403 return error(ParenLoc, "expected '('");
2404 LocTy DerefLoc = Lex.getLoc();
2405 if (parseUInt64(Bytes))
2406 return true;
2407 ParenLoc = Lex.getLoc();
2408 if (!EatIfPresent(lltok::rparen))
2409 return error(ParenLoc, "expected ')'");
2410 if (!Bytes)
2411 return error(DerefLoc, "dereferenceable bytes must be non-zero");
2412 return false;
2413}
2414
2415bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) {
2416 Lex.Lex();
2418 if (!EatIfPresent(lltok::lparen))
2419 return false;
2420 LocTy KindLoc = Lex.getLoc();
2421 if (Lex.getKind() == lltok::kw_sync)
2423 else if (Lex.getKind() == lltok::kw_async)
2425 else
2426 return error(KindLoc, "expected unwind table kind");
2427 Lex.Lex();
2428 return parseToken(lltok::rparen, "expected ')'");
2429}
2430
2431bool LLParser::parseAllocKind(AllocFnKind &Kind) {
2432 Lex.Lex();
2433 LocTy ParenLoc = Lex.getLoc();
2434 if (!EatIfPresent(lltok::lparen))
2435 return error(ParenLoc, "expected '('");
2436 LocTy KindLoc = Lex.getLoc();
2437 std::string Arg;
2438 if (parseStringConstant(Arg))
2439 return error(KindLoc, "expected allockind value");
2440 for (StringRef A : llvm::split(Arg, ",")) {
2441 if (A == "alloc") {
2443 } else if (A == "realloc") {
2445 } else if (A == "free") {
2447 } else if (A == "uninitialized") {
2449 } else if (A == "zeroed") {
2451 } else if (A == "aligned") {
2453 } else {
2454 return error(KindLoc, Twine("unknown allockind ") + A);
2455 }
2456 }
2457 ParenLoc = Lex.getLoc();
2458 if (!EatIfPresent(lltok::rparen))
2459 return error(ParenLoc, "expected ')'");
2460 if (Kind == AllocFnKind::Unknown)
2461 return error(KindLoc, "expected allockind value");
2462 return false;
2463}
2464
2465static std::optional<MemoryEffects::Location> keywordToLoc(lltok::Kind Tok) {
2466 switch (Tok) {
2467 case lltok::kw_argmem:
2468 return IRMemLocation::ArgMem;
2471 default:
2472 return std::nullopt;
2473 }
2474}
2475
2476static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) {
2477 switch (Tok) {
2478 case lltok::kw_none:
2479 return ModRefInfo::NoModRef;
2480 case lltok::kw_read:
2481 return ModRefInfo::Ref;
2482 case lltok::kw_write:
2483 return ModRefInfo::Mod;
2485 return ModRefInfo::ModRef;
2486 default:
2487 return std::nullopt;
2488 }
2489}
2490
2491std::optional<MemoryEffects> LLParser::parseMemoryAttr() {
2493
2494 // We use syntax like memory(argmem: read), so the colon should not be
2495 // interpreted as a label terminator.
2497 auto _ = make_scope_exit([&] { Lex.setIgnoreColonInIdentifiers(false); });
2498
2499 Lex.Lex();
2500 if (!EatIfPresent(lltok::lparen)) {
2501 tokError("expected '('");
2502 return std::nullopt;
2503 }
2504
2505 bool SeenLoc = false;
2506 do {
2507 std::optional<IRMemLocation> Loc = keywordToLoc(Lex.getKind());
2508 if (Loc) {
2509 Lex.Lex();
2510 if (!EatIfPresent(lltok::colon)) {
2511 tokError("expected ':' after location");
2512 return std::nullopt;
2513 }
2514 }
2515
2516 std::optional<ModRefInfo> MR = keywordToModRef(Lex.getKind());
2517 if (!MR) {
2518 if (!Loc)
2519 tokError("expected memory location (argmem, inaccessiblemem) "
2520 "or access kind (none, read, write, readwrite)");
2521 else
2522 tokError("expected access kind (none, read, write, readwrite)");
2523 return std::nullopt;
2524 }
2525
2526 Lex.Lex();
2527 if (Loc) {
2528 SeenLoc = true;
2529 ME = ME.getWithModRef(*Loc, *MR);
2530 } else {
2531 if (SeenLoc) {
2532 tokError("default access kind must be specified first");
2533 return std::nullopt;
2534 }
2535 ME = MemoryEffects(*MR);
2536 }
2537
2538 if (EatIfPresent(lltok::rparen))
2539 return ME;
2540 } while (EatIfPresent(lltok::comma));
2541
2542 tokError("unterminated memory attribute");
2543 return std::nullopt;
2544}
2545
2546static unsigned keywordToFPClassTest(lltok::Kind Tok) {
2547 switch (Tok) {
2548 case lltok::kw_all:
2549 return fcAllFlags;
2550 case lltok::kw_nan:
2551 return fcNan;
2552 case lltok::kw_snan:
2553 return fcSNan;
2554 case lltok::kw_qnan:
2555 return fcQNan;
2556 case lltok::kw_inf:
2557 return fcInf;
2558 case lltok::kw_ninf:
2559 return fcNegInf;
2560 case lltok::kw_pinf:
2561 return fcPosInf;
2562 case lltok::kw_norm:
2563 return fcNormal;
2564 case lltok::kw_nnorm:
2565 return fcNegNormal;
2566 case lltok::kw_pnorm:
2567 return fcPosNormal;
2568 case lltok::kw_sub:
2569 return fcSubnormal;
2570 case lltok::kw_nsub:
2571 return fcNegSubnormal;
2572 case lltok::kw_psub:
2573 return fcPosSubnormal;
2574 case lltok::kw_zero:
2575 return fcZero;
2576 case lltok::kw_nzero:
2577 return fcNegZero;
2578 case lltok::kw_pzero:
2579 return fcPosZero;
2580 default:
2581 return 0;
2582 }
2583}
2584
2585unsigned LLParser::parseNoFPClassAttr() {
2586 unsigned Mask = fcNone;
2587
2588 Lex.Lex();
2589 if (!EatIfPresent(lltok::lparen)) {
2590 tokError("expected '('");
2591 return 0;
2592 }
2593
2594 do {
2595 uint64_t Value = 0;
2596 unsigned TestMask = keywordToFPClassTest(Lex.getKind());
2597 if (TestMask != 0) {
2598 Mask |= TestMask;
2599 // TODO: Disallow overlapping masks to avoid copy paste errors
2600 } else if (Mask == 0 && Lex.getKind() == lltok::APSInt &&
2601 !parseUInt64(Value)) {
2602 if (Value == 0 || (Value & ~static_cast<unsigned>(fcAllFlags)) != 0) {
2603 error(Lex.getLoc(), "invalid mask value for 'nofpclass'");
2604 return 0;
2605 }
2606
2607 if (!EatIfPresent(lltok::rparen)) {
2608 error(Lex.getLoc(), "expected ')'");
2609 return 0;
2610 }
2611
2612 return Value;
2613 } else {
2614 error(Lex.getLoc(), "expected nofpclass test mask");
2615 return 0;
2616 }
2617
2618 Lex.Lex();
2619 if (EatIfPresent(lltok::rparen))
2620 return Mask;
2621 } while (1);
2622
2623 llvm_unreachable("unterminated nofpclass attribute");
2624}
2625
2626/// parseOptionalCommaAlign
2627/// ::=
2628/// ::= ',' align 4
2629///
2630/// This returns with AteExtraComma set to true if it ate an excess comma at the
2631/// end.
2632bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment,
2633 bool &AteExtraComma) {
2634 AteExtraComma = false;
2635 while (EatIfPresent(lltok::comma)) {
2636 // Metadata at the end is an early exit.
2637 if (Lex.getKind() == lltok::MetadataVar) {
2638 AteExtraComma = true;
2639 return false;
2640 }
2641
2642 if (Lex.getKind() != lltok::kw_align)
2643 return error(Lex.getLoc(), "expected metadata or 'align'");
2644
2645 if (parseOptionalAlignment(Alignment))
2646 return true;
2647 }
2648
2649 return false;
2650}
2651
2652/// parseOptionalCommaAddrSpace
2653/// ::=
2654/// ::= ',' addrspace(1)
2655///
2656/// This returns with AteExtraComma set to true if it ate an excess comma at the
2657/// end.
2658bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
2659 bool &AteExtraComma) {
2660 AteExtraComma = false;
2661 while (EatIfPresent(lltok::comma)) {
2662 // Metadata at the end is an early exit.
2663 if (Lex.getKind() == lltok::MetadataVar) {
2664 AteExtraComma = true;
2665 return false;
2666 }
2667
2668 Loc = Lex.getLoc();
2669 if (Lex.getKind() != lltok::kw_addrspace)
2670 return error(Lex.getLoc(), "expected metadata or 'addrspace'");
2671
2672 if (parseOptionalAddrSpace(AddrSpace))
2673 return true;
2674 }
2675
2676 return false;
2677}
2678
2679bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
2680 std::optional<unsigned> &HowManyArg) {
2681 Lex.Lex();
2682
2683 auto StartParen = Lex.getLoc();
2684 if (!EatIfPresent(lltok::lparen))
2685 return error(StartParen, "expected '('");
2686
2687 if (parseUInt32(BaseSizeArg))
2688 return true;
2689
2690 if (EatIfPresent(lltok::comma)) {
2691 auto HowManyAt = Lex.getLoc();
2692 unsigned HowMany;
2693 if (parseUInt32(HowMany))
2694 return true;
2695 if (HowMany == BaseSizeArg)
2696 return error(HowManyAt,
2697 "'allocsize' indices can't refer to the same parameter");
2698 HowManyArg = HowMany;
2699 } else
2700 HowManyArg = std::nullopt;
2701
2702 auto EndParen = Lex.getLoc();
2703 if (!EatIfPresent(lltok::rparen))
2704 return error(EndParen, "expected ')'");
2705 return false;
2706}
2707
2708bool LLParser::parseVScaleRangeArguments(unsigned &MinValue,
2709 unsigned &MaxValue) {
2710 Lex.Lex();
2711
2712 auto StartParen = Lex.getLoc();
2713 if (!EatIfPresent(lltok::lparen))
2714 return error(StartParen, "expected '('");
2715
2716 if (parseUInt32(MinValue))
2717 return true;
2718
2719 if (EatIfPresent(lltok::comma)) {
2720 if (parseUInt32(MaxValue))
2721 return true;
2722 } else
2723 MaxValue = MinValue;
2724
2725 auto EndParen = Lex.getLoc();
2726 if (!EatIfPresent(lltok::rparen))
2727 return error(EndParen, "expected ')'");
2728 return false;
2729}
2730
2731/// parseScopeAndOrdering
2732/// if isAtomic: ::= SyncScope? AtomicOrdering
2733/// else: ::=
2734///
2735/// This sets Scope and Ordering to the parsed values.
2736bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
2737 AtomicOrdering &Ordering) {
2738 if (!IsAtomic)
2739 return false;
2740
2741 return parseScope(SSID) || parseOrdering(Ordering);
2742}
2743
2744/// parseScope
2745/// ::= syncscope("singlethread" | "<target scope>")?
2746///
2747/// This sets synchronization scope ID to the ID of the parsed value.
2748bool LLParser::parseScope(SyncScope::ID &SSID) {
2749 SSID = SyncScope::System;
2750 if (EatIfPresent(lltok::kw_syncscope)) {
2751 auto StartParenAt = Lex.getLoc();
2752 if (!EatIfPresent(lltok::lparen))
2753 return error(StartParenAt, "Expected '(' in syncscope");
2754
2755 std::string SSN;
2756 auto SSNAt = Lex.getLoc();
2757 if (parseStringConstant(SSN))
2758 return error(SSNAt, "Expected synchronization scope name");
2759
2760 auto EndParenAt = Lex.getLoc();
2761 if (!EatIfPresent(lltok::rparen))
2762 return error(EndParenAt, "Expected ')' in syncscope");
2763
2764 SSID = Context.getOrInsertSyncScopeID(SSN);
2765 }
2766
2767 return false;
2768}
2769
2770/// parseOrdering
2771/// ::= AtomicOrdering
2772///
2773/// This sets Ordering to the parsed value.
2774bool LLParser::parseOrdering(AtomicOrdering &Ordering) {
2775 switch (Lex.getKind()) {
2776 default:
2777 return tokError("Expected ordering on atomic instruction");
2778 case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break;
2779 case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break;
2780 // Not specified yet:
2781 // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
2782 case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break;
2783 case lltok::kw_release: Ordering = AtomicOrdering::Release; break;
2784 case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break;
2785 case lltok::kw_seq_cst:
2787 break;
2788 }
2789 Lex.Lex();
2790 return false;
2791}
2792
2793/// parseOptionalStackAlignment
2794/// ::= /* empty */
2795/// ::= 'alignstack' '(' 4 ')'
2796bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) {
2797 Alignment = 0;
2798 if (!EatIfPresent(lltok::kw_alignstack))
2799 return false;
2800 LocTy ParenLoc = Lex.getLoc();
2801 if (!EatIfPresent(lltok::lparen))
2802 return error(ParenLoc, "expected '('");
2803 LocTy AlignLoc = Lex.getLoc();
2804 if (parseUInt32(Alignment))
2805 return true;
2806 ParenLoc = Lex.getLoc();
2807 if (!EatIfPresent(lltok::rparen))
2808 return error(ParenLoc, "expected ')'");
2809 if (!isPowerOf2_32(Alignment))
2810 return error(AlignLoc, "stack alignment is not a power of two");
2811 return false;
2812}
2813
2814/// parseIndexList - This parses the index list for an insert/extractvalue
2815/// instruction. This sets AteExtraComma in the case where we eat an extra
2816/// comma at the end of the line and find that it is followed by metadata.
2817/// Clients that don't allow metadata can call the version of this function that
2818/// only takes one argument.
2819///
2820/// parseIndexList
2821/// ::= (',' uint32)+
2822///
2823bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices,
2824 bool &AteExtraComma) {
2825 AteExtraComma = false;
2826
2827 if (Lex.getKind() != lltok::comma)
2828 return tokError("expected ',' as start of index list");
2829
2830 while (EatIfPresent(lltok::comma)) {
2831 if (Lex.getKind() == lltok::MetadataVar) {
2832 if (Indices.empty())
2833 return tokError("expected index");
2834 AteExtraComma = true;
2835 return false;
2836 }
2837 unsigned Idx = 0;
2838 if (parseUInt32(Idx))
2839 return true;
2840 Indices.push_back(Idx);
2841 }
2842
2843 return false;
2844}
2845
2846//===----------------------------------------------------------------------===//
2847// Type Parsing.
2848//===----------------------------------------------------------------------===//
2849
2850/// parseType - parse a type.
2851bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
2852 SMLoc TypeLoc = Lex.getLoc();
2853 switch (Lex.getKind()) {
2854 default:
2855 return tokError(Msg);
2856 case lltok::Type:
2857 // Type ::= 'float' | 'void' (etc)
2858 Result = Lex.getTyVal();
2859 Lex.Lex();
2860
2861 // Handle "ptr" opaque pointer type.
2862 //
2863 // Type ::= ptr ('addrspace' '(' uint32 ')')?
2864 if (Result->isPointerTy()) {
2865 unsigned AddrSpace;
2866 if (parseOptionalAddrSpace(AddrSpace))
2867 return true;
2868 Result = PointerType::get(getContext(), AddrSpace);
2869
2870 // Give a nice error for 'ptr*'.
2871 if (Lex.getKind() == lltok::star)
2872 return tokError("ptr* is invalid - use ptr instead");
2873
2874 // Fall through to parsing the type suffixes only if this 'ptr' is a
2875 // function return. Otherwise, return success, implicitly rejecting other
2876 // suffixes.
2877 if (Lex.getKind() != lltok::lparen)
2878 return false;
2879 }
2880 break;
2881 case lltok::kw_target: {
2882 // Type ::= TargetExtType
2883 if (parseTargetExtType(Result))
2884 return true;
2885 break;
2886 }
2887 case lltok::lbrace:
2888 // Type ::= StructType
2889 if (parseAnonStructType(Result, false))
2890 return true;
2891 break;
2892 case lltok::lsquare:
2893 // Type ::= '[' ... ']'
2894 Lex.Lex(); // eat the lsquare.
2895 if (parseArrayVectorType(Result, false))
2896 return true;
2897 break;
2898 case lltok::less: // Either vector or packed struct.
2899 // Type ::= '<' ... '>'
2900 Lex.Lex();
2901 if (Lex.getKind() == lltok::lbrace) {
2902 if (parseAnonStructType(Result, true) ||
2903 parseToken(lltok::greater, "expected '>' at end of packed struct"))
2904 return true;
2905 } else if (parseArrayVectorType(Result, true))
2906 return true;
2907 break;
2908 case lltok::LocalVar: {
2909 // Type ::= %foo
2910 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
2911
2912 // If the type hasn't been defined yet, create a forward definition and
2913 // remember where that forward def'n was seen (in case it never is defined).
2914 if (!Entry.first) {
2915 Entry.first = StructType::create(Context, Lex.getStrVal());
2916 Entry.second = Lex.getLoc();
2917 }
2918 Result = Entry.first;
2919 Lex.Lex();
2920 break;
2921 }
2922
2923 case lltok::LocalVarID: {
2924 // Type ::= %4
2925 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
2926
2927 // If the type hasn't been defined yet, create a forward definition and
2928 // remember where that forward def'n was seen (in case it never is defined).
2929 if (!Entry.first) {
2930 Entry.first = StructType::create(Context);
2931 Entry.second = Lex.getLoc();
2932 }
2933 Result = Entry.first;
2934 Lex.Lex();
2935 break;
2936 }
2937 }
2938
2939 // parse the type suffixes.
2940 while (true) {
2941 switch (Lex.getKind()) {
2942 // End of type.
2943 default:
2944 if (!AllowVoid && Result->isVoidTy())
2945 return error(TypeLoc, "void type only allowed for function results");
2946 return false;
2947
2948 // Type ::= Type '*'
2949 case lltok::star:
2950 if (Result->isLabelTy())
2951 return tokError("basic block pointers are invalid");
2952 if (Result->isVoidTy())
2953 return tokError("pointers to void are invalid - use i8* instead");
2955 return tokError("pointer to this type is invalid");
2957 Lex.Lex();
2958 break;
2959
2960 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
2961 case lltok::kw_addrspace: {
2962 if (Result->isLabelTy())
2963 return tokError("basic block pointers are invalid");
2964 if (Result->isVoidTy())
2965 return tokError("pointers to void are invalid; use i8* instead");
2967 return tokError("pointer to this type is invalid");
2968 unsigned AddrSpace;
2969 if (parseOptionalAddrSpace(AddrSpace) ||
2970 parseToken(lltok::star, "expected '*' in address space"))
2971 return true;
2972
2973 Result = PointerType::get(Result, AddrSpace);
2974 break;
2975 }
2976
2977 /// Types '(' ArgTypeListI ')' OptFuncAttrs
2978 case lltok::lparen:
2979 if (parseFunctionType(Result))
2980 return true;
2981 break;
2982 }
2983 }
2984}
2985
2986/// parseParameterList
2987/// ::= '(' ')'
2988/// ::= '(' Arg (',' Arg)* ')'
2989/// Arg
2990/// ::= Type OptionalAttributes Value OptionalAttributes
2991bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
2992 PerFunctionState &PFS, bool IsMustTailCall,
2993 bool InVarArgsFunc) {
2994 if (parseToken(lltok::lparen, "expected '(' in call"))
2995 return true;
2996
2997 while (Lex.getKind() != lltok::rparen) {
2998 // If this isn't the first argument, we need a comma.
2999 if (!ArgList.empty() &&
3000 parseToken(lltok::comma, "expected ',' in argument list"))
3001 return true;
3002
3003 // parse an ellipsis if this is a musttail call in a variadic function.
3004 if (Lex.getKind() == lltok::dotdotdot) {
3005 const char *Msg = "unexpected ellipsis in argument list for ";
3006 if (!IsMustTailCall)
3007 return tokError(Twine(Msg) + "non-musttail call");
3008 if (!InVarArgsFunc)
3009 return tokError(Twine(Msg) + "musttail call in non-varargs function");
3010 Lex.Lex(); // Lex the '...', it is purely for readability.
3011 return parseToken(lltok::rparen, "expected ')' at end of argument list");
3012 }
3013
3014 // parse the argument.
3015 LocTy ArgLoc;
3016 Type *ArgTy = nullptr;
3017 Value *V;
3018 if (parseType(ArgTy, ArgLoc))
3019 return true;
3020
3021 AttrBuilder ArgAttrs(M->getContext());
3022
3023 if (ArgTy->isMetadataTy()) {
3024 if (parseMetadataAsValue(V, PFS))
3025 return true;
3026 } else {
3027 // Otherwise, handle normal operands.
3028 if (parseOptionalParamAttrs(ArgAttrs) || parseValue(ArgTy, V, PFS))
3029 return true;
3030 }
3031 ArgList.push_back(ParamInfo(
3032 ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
3033 }
3034
3035 if (IsMustTailCall && InVarArgsFunc)
3036 return tokError("expected '...' at end of argument list for musttail call "
3037 "in varargs function");
3038
3039 Lex.Lex(); // Lex the ')'.
3040 return false;
3041}
3042
3043/// parseRequiredTypeAttr
3044/// ::= attrname(<ty>)
3045bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
3046 Attribute::AttrKind AttrKind) {
3047 Type *Ty = nullptr;
3048 if (!EatIfPresent(AttrToken))
3049 return true;
3050 if (!EatIfPresent(lltok::lparen))
3051 return error(Lex.getLoc(), "expected '('");
3052 if (parseType(Ty))
3053 return true;
3054 if (!EatIfPresent(lltok::rparen))
3055 return error(Lex.getLoc(), "expected ')'");
3056
3057 B.addTypeAttr(AttrKind, Ty);
3058 return false;
3059}
3060
3061/// parseRangeAttr
3062/// ::= range(<ty> <n>,<n>)
3063bool LLParser::parseRangeAttr(AttrBuilder &B) {
3064 Lex.Lex();
3065
3066 APInt Lower;
3067 APInt Upper;
3068 Type *Ty = nullptr;
3069 LocTy TyLoc;
3070
3071 auto ParseAPSInt = [&](unsigned BitWidth, APInt &Val) {
3072 if (Lex.getKind() != lltok::APSInt)
3073 return tokError("expected integer");
3074 if (Lex.getAPSIntVal().getBitWidth() > BitWidth)
3075 return tokError(
3076 "integer is too large for the bit width of specified type");
3077 Val = Lex.getAPSIntVal().extend(BitWidth);
3078 Lex.Lex();
3079 return false;
3080 };
3081
3082 if (parseToken(lltok::lparen, "expected '('") || parseType(Ty, TyLoc))
3083 return true;
3084 if (!Ty->isIntegerTy())
3085 return error(TyLoc, "the range must have integer type!");
3086
3087 unsigned BitWidth = Ty->getPrimitiveSizeInBits();
3088
3089 if (ParseAPSInt(BitWidth, Lower) ||
3090 parseToken(lltok::comma, "expected ','") || ParseAPSInt(BitWidth, Upper))
3091 return true;
3092 if (Lower == Upper)
3093 return tokError("the range should not represent the full or empty set!");
3094
3095 if (parseToken(lltok::rparen, "expected ')'"))
3096 return true;
3097
3098 B.addRangeAttr(ConstantRange(Lower, Upper));
3099 return false;
3100}
3101
3102/// parseOptionalOperandBundles
3103/// ::= /*empty*/
3104/// ::= '[' OperandBundle [, OperandBundle ]* ']'
3105///
3106/// OperandBundle
3107/// ::= bundle-tag '(' ')'
3108/// ::= bundle-tag '(' Type Value [, Type Value ]* ')'
3109///
3110/// bundle-tag ::= String Constant
3111bool LLParser::parseOptionalOperandBundles(
3112 SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
3113 LocTy BeginLoc = Lex.getLoc();
3114 if (!EatIfPresent(lltok::lsquare))
3115 return false;
3116
3117 while (Lex.getKind() != lltok::rsquare) {
3118 // If this isn't the first operand bundle, we need a comma.
3119 if (!BundleList.empty() &&
3120 parseToken(lltok::comma, "expected ',' in input list"))
3121 return true;
3122
3123 std::string Tag;
3124 if (parseStringConstant(Tag))
3125 return true;
3126
3127 if (parseToken(lltok::lparen, "expected '(' in operand bundle"))
3128 return true;
3129
3130 std::vector<Value *> Inputs;
3131 while (Lex.getKind() != lltok::rparen) {
3132 // If this isn't the first input, we need a comma.
3133 if (!Inputs.empty() &&
3134 parseToken(lltok::comma, "expected ',' in input list"))
3135 return true;
3136
3137 Type *Ty = nullptr;
3138 Value *Input = nullptr;
3139 if (parseType(Ty) || parseValue(Ty, Input, PFS))
3140 return true;
3141 Inputs.push_back(Input);
3142 }
3143
3144 BundleList.emplace_back(std::move(Tag), std::move(Inputs));
3145
3146 Lex.Lex(); // Lex the ')'.
3147 }
3148
3149 if (BundleList.empty())
3150 return error(BeginLoc, "operand bundle set must not be empty");
3151
3152 Lex.Lex(); // Lex the ']'.
3153 return false;
3154}
3155
3156bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix,
3157 unsigned NextID, unsigned ID) const {
3158 if (ID < NextID)
3159 return error(Loc, Kind + " expected to be numbered '" + Prefix +
3160 Twine(NextID) + "' or greater");
3161
3162 return false;
3163}
3164
3165/// parseArgumentList - parse the argument list for a function type or function
3166/// prototype.
3167/// ::= '(' ArgTypeListI ')'
3168/// ArgTypeListI
3169/// ::= /*empty*/
3170/// ::= '...'
3171/// ::= ArgTypeList ',' '...'
3172/// ::= ArgType (',' ArgType)*
3173///
3174bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
3175 SmallVectorImpl<unsigned> &UnnamedArgNums,
3176 bool &IsVarArg) {
3177 unsigned CurValID = 0;
3178 IsVarArg = false;
3179 assert(Lex.getKind() == lltok::lparen);
3180 Lex.Lex(); // eat the (.
3181
3182 if (Lex.getKind() != lltok::rparen) {
3183 do {
3184 // Handle ... at end of arg list.
3185 if (EatIfPresent(lltok::dotdotdot)) {
3186 IsVarArg = true;
3187 break;
3188 }
3189
3190 // Otherwise must be an argument type.
3191 LocTy TypeLoc = Lex.getLoc();
3192 Type *ArgTy = nullptr;
3193 AttrBuilder Attrs(M->getContext());
3194 if (parseType(ArgTy) || parseOptionalParamAttrs(Attrs))
3195 return true;
3196
3197 if (ArgTy->isVoidTy())
3198 return error(TypeLoc, "argument can not have void type");
3199
3200 std::string Name;
3201 if (Lex.getKind() == lltok::LocalVar) {
3202 Name = Lex.getStrVal();
3203 Lex.Lex();
3204 } else {
3205 unsigned ArgID;
3206 if (Lex.getKind() == lltok::LocalVarID) {
3207 ArgID = Lex.getUIntVal();
3208 if (checkValueID(TypeLoc, "argument", "%", CurValID, ArgID))
3209 return true;
3210 Lex.Lex();
3211 } else {
3212 ArgID = CurValID;
3213 }
3214 UnnamedArgNums.push_back(ArgID);
3215 CurValID = ArgID + 1;
3216 }
3217
3218 if (!ArgTy->isFirstClassType())
3219 return error(TypeLoc, "invalid type for function argument");
3220
3221 ArgList.emplace_back(TypeLoc, ArgTy,
3222 AttributeSet::get(ArgTy->getContext(), Attrs),
3223 std::move(Name));
3224 } while (EatIfPresent(lltok::comma));
3225 }
3226
3227 return parseToken(lltok::rparen, "expected ')' at end of argument list");
3228}
3229
3230/// parseFunctionType
3231/// ::= Type ArgumentList OptionalAttrs
3232bool LLParser::parseFunctionType(Type *&Result) {
3233 assert(Lex.getKind() == lltok::lparen);
3234
3236 return tokError("invalid function return type");
3237
3239 bool IsVarArg;
3240 SmallVector<unsigned> UnnamedArgNums;
3241 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg))
3242 return true;
3243
3244 // Reject names on the arguments lists.
3245 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
3246 if (!ArgList[i].Name.empty())
3247 return error(ArgList[i].Loc, "argument name invalid in function type");
3248 if (ArgList[i].Attrs.hasAttributes())
3249 return error(ArgList[i].Loc,
3250 "argument attributes invalid in function type");
3251 }
3252
3253 SmallVector<Type*, 16> ArgListTy;
3254 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
3255 ArgListTy.push_back(ArgList[i].Ty);
3256
3257 Result = FunctionType::get(Result, ArgListTy, IsVarArg);
3258 return false;
3259}
3260
3261/// parseAnonStructType - parse an anonymous struct type, which is inlined into
3262/// other structs.
3263bool LLParser::parseAnonStructType(Type *&Result, bool Packed) {
3265 if (parseStructBody(Elts))
3266 return true;
3267
3268 Result = StructType::get(Context, Elts, Packed);
3269 return false;
3270}
3271
3272/// parseStructDefinition - parse a struct in a 'type' definition.
3273bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name,
3274 std::pair<Type *, LocTy> &Entry,
3275 Type *&ResultTy) {
3276 // If the type was already defined, diagnose the redefinition.
3277 if (Entry.first && !Entry.second.isValid())
3278 return error(TypeLoc, "redefinition of type");
3279
3280 // If we have opaque, just return without filling in the definition for the
3281 // struct. This counts as a definition as far as the .ll file goes.
3282 if (EatIfPresent(lltok::kw_opaque)) {
3283 // This type is being defined, so clear the location to indicate this.
3284 Entry.second = SMLoc();
3285
3286 // If this type number has never been uttered, create it.
3287 if (!Entry.first)
3288 Entry.first = StructType::create(Context, Name);
3289 ResultTy = Entry.first;
3290 return false;
3291 }
3292
3293 // If the type starts with '<', then it is either a packed struct or a vector.
3294 bool isPacked = EatIfPresent(lltok::less);
3295
3296 // If we don't have a struct, then we have a random type alias, which we
3297 // accept for compatibility with old files. These types are not allowed to be
3298 // forward referenced and not allowed to be recursive.
3299 if (Lex.getKind() != lltok::lbrace) {
3300 if (Entry.first)
3301 return error(TypeLoc, "forward references to non-struct type");
3302
3303 ResultTy = nullptr;
3304 if (isPacked)
3305 return parseArrayVectorType(ResultTy, true);
3306 return parseType(ResultTy);
3307 }
3308
3309 // This type is being defined, so clear the location to indicate this.
3310 Entry.second = SMLoc();
3311
3312 // If this type number has never been uttered, create it.
3313 if (!Entry.first)
3314 Entry.first = StructType::create(Context, Name);
3315
3316 StructType *STy = cast<StructType>(Entry.first);
3317
3319 if (parseStructBody(Body) ||
3320 (isPacked && parseToken(lltok::greater, "expected '>' in packed struct")))
3321 return true;
3322
3323 STy->setBody(Body, isPacked);
3324 ResultTy = STy;
3325 return false;
3326}
3327
3328/// parseStructType: Handles packed and unpacked types. </> parsed elsewhere.
3329/// StructType
3330/// ::= '{' '}'
3331/// ::= '{' Type (',' Type)* '}'
3332/// ::= '<' '{' '}' '>'
3333/// ::= '<' '{' Type (',' Type)* '}' '>'
3334bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) {
3335 assert(Lex.getKind() == lltok::lbrace);
3336 Lex.Lex(); // Consume the '{'
3337
3338 // Handle the empty struct.
3339 if (EatIfPresent(lltok::rbrace))
3340 return false;
3341
3342 LocTy EltTyLoc = Lex.getLoc();
3343 Type *Ty = nullptr;
3344 if (parseType(Ty))
3345 return true;
3346 Body.push_back(Ty);
3347
3349 return error(EltTyLoc, "invalid element type for struct");
3350
3351 while (EatIfPresent(lltok::comma)) {
3352 EltTyLoc = Lex.getLoc();
3353 if (parseType(Ty))
3354 return true;
3355
3357 return error(EltTyLoc, "invalid element type for struct");
3358
3359 Body.push_back(Ty);
3360 }
3361
3362 return parseToken(lltok::rbrace, "expected '}' at end of struct");
3363}
3364
3365/// parseArrayVectorType - parse an array or vector type, assuming the first
3366/// token has already been consumed.
3367/// Type
3368/// ::= '[' APSINTVAL 'x' Types ']'
3369/// ::= '<' APSINTVAL 'x' Types '>'
3370/// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
3371bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) {
3372 bool Scalable = false;
3373
3374 if (IsVector && Lex.getKind() == lltok::kw_vscale) {
3375 Lex.Lex(); // consume the 'vscale'
3376 if (parseToken(lltok::kw_x, "expected 'x' after vscale"))
3377 return true;
3378
3379 Scalable = true;
3380 }
3381
3382 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
3383 Lex.getAPSIntVal().getBitWidth() > 64)
3384 return tokError("expected number in address space");
3385
3386 LocTy SizeLoc = Lex.getLoc();
3388 Lex.Lex();
3389
3390 if (parseToken(lltok::kw_x, "expected 'x' after element count"))
3391 return true;
3392
3393 LocTy TypeLoc = Lex.getLoc();
3394 Type *EltTy = nullptr;
3395 if (parseType(EltTy))
3396 return true;
3397
3398 if (parseToken(IsVector ? lltok::greater : lltok::rsquare,
3399 "expected end of sequential type"))
3400 return true;
3401
3402 if (IsVector) {
3403 if (Size == 0)
3404 return error(SizeLoc, "zero element vector is illegal");
3405 if ((unsigned)Size != Size)
3406 return error(SizeLoc, "size too large for vector");
3408 return error(TypeLoc, "invalid vector element type");
3409 Result = VectorType::get(EltTy, unsigned(Size), Scalable);
3410 } else {
3412 return error(TypeLoc, "invalid array element type");
3413 Result = ArrayType::get(EltTy, Size);
3414 }
3415 return false;
3416}
3417
3418/// parseTargetExtType - handle target extension type syntax
3419/// TargetExtType
3420/// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')'
3421///
3422/// TargetExtTypeParams
3423/// ::= /*empty*/
3424/// ::= ',' Type TargetExtTypeParams
3425///
3426/// TargetExtIntParams
3427/// ::= /*empty*/
3428/// ::= ',' uint32 TargetExtIntParams
3429bool LLParser::parseTargetExtType(Type *&Result) {
3430 Lex.Lex(); // Eat the 'target' keyword.
3431
3432 // Get the mandatory type name.
3433 std::string TypeName;
3434 if (parseToken(lltok::lparen, "expected '(' in target extension type") ||
3435 parseStringConstant(TypeName))
3436 return true;
3437
3438 // Parse all of the integer and type parameters at the same time; the use of
3439 // SeenInt will allow us to catch cases where type parameters follow integer
3440 // parameters.
3441 SmallVector<Type *> TypeParams;
3442 SmallVector<unsigned> IntParams;
3443 bool SeenInt = false;
3444 while (Lex.getKind() == lltok::comma) {
3445 Lex.Lex(); // Eat the comma.
3446
3447 if (Lex.getKind() == lltok::APSInt) {
3448 SeenInt = true;
3449 unsigned IntVal;
3450 if (parseUInt32(IntVal))
3451 return true;
3452 IntParams.push_back(IntVal);
3453 } else if (SeenInt) {
3454 // The only other kind of parameter we support is type parameters, which
3455 // must precede the integer parameters. This is therefore an error.
3456 return tokError("expected uint32 param");
3457 } else {
3458 Type *TypeParam;
3459 if (parseType(TypeParam, /*AllowVoid=*/true))
3460 return true;
3461 TypeParams.push_back(TypeParam);
3462 }
3463 }
3464
3465 if (parseToken(lltok::rparen, "expected ')' in target extension type"))
3466 return true;
3467
3468 Result = TargetExtType::get(Context, TypeName, TypeParams, IntParams);
3469 return false;
3470}
3471
3472//===----------------------------------------------------------------------===//
3473// Function Semantic Analysis.
3474//===----------------------------------------------------------------------===//
3475
3476LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
3477 int functionNumber,
3478 ArrayRef<unsigned> UnnamedArgNums)
3479 : P(p), F(f), FunctionNumber(functionNumber) {
3480
3481 // Insert unnamed arguments into the NumberedVals list.
3482 auto It = UnnamedArgNums.begin();
3483 for (Argument &A : F.args()) {
3484 if (!A.hasName()) {
3485 unsigned ArgNum = *It++;
3486 NumberedVals.add(ArgNum, &A);
3487 }
3488 }
3489}
3490
3491LLParser::PerFunctionState::~PerFunctionState() {
3492 // If there were any forward referenced non-basicblock values, delete them.
3493
3494 for (const auto &P : ForwardRefVals) {
3495 if (isa<BasicBlock>(P.second.first))
3496 continue;
3497 P.second.first->replaceAllUsesWith(
3498 UndefValue::get(P.second.first->getType()));
3499 P.second.first->deleteValue();
3500 }
3501
3502 for (const auto &P : ForwardRefValIDs) {
3503 if (isa<BasicBlock>(P.second.first))
3504 continue;
3505 P.second.first->replaceAllUsesWith(
3506 UndefValue::get(P.second.first->getType()));
3507 P.second.first->deleteValue();
3508 }
3509}
3510
3511bool LLParser::PerFunctionState::finishFunction() {
3512 if (!ForwardRefVals.empty())
3513 return P.error(ForwardRefVals.begin()->second.second,
3514 "use of undefined value '%" + ForwardRefVals.begin()->first +
3515 "'");
3516 if (!ForwardRefValIDs.empty())
3517 return P.error(ForwardRefValIDs.begin()->second.second,
3518 "use of undefined value '%" +
3519 Twine(ForwardRefValIDs.begin()->first) + "'");
3520 return false;
3521}
3522
3523/// getVal - Get a value with the specified name or ID, creating a
3524/// forward reference record if needed. This can return null if the value
3525/// exists but does not have the right type.
3526Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty,
3527 LocTy Loc) {
3528 // Look this name up in the normal function symbol table.
3529 Value *Val = F.getValueSymbolTable()->lookup(Name);
3530
3531 // If this is a forward reference for the value, see if we already created a
3532 // forward ref record.
3533 if (!Val) {
3534 auto I = ForwardRefVals.find(Name);
3535 if (I != ForwardRefVals.end())
3536 Val = I->second.first;
3537 }
3538
3539 // If we have the value in the symbol table or fwd-ref table, return it.
3540 if (Val)
3541 return P.checkValidVariableType(Loc, "%" + Name, Ty, Val);
3542
3543 // Don't make placeholders with invalid type.
3544 if (!Ty->isFirstClassType()) {
3545 P.error(Loc, "invalid use of a non-first-class type");
3546 return nullptr;
3547 }
3548
3549 // Otherwise, create a new forward reference for this value and remember it.
3550 Value *FwdVal;
3551 if (Ty->isLabelTy()) {
3552 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
3553 } else {
3554 FwdVal = new Argument(Ty, Name);
3555 }
3556 if (FwdVal->getName() != Name) {
3557 P.error(Loc, "name is too long which can result in name collisions, "
3558 "consider making the name shorter or "
3559 "increasing -non-global-value-max-name-size");
3560 return nullptr;
3561 }
3562
3563 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
3564 return FwdVal;
3565}
3566
3567Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) {
3568 // Look this name up in the normal function symbol table.
3569 Value *Val = NumberedVals.get(ID);
3570
3571 // If this is a forward reference for the value, see if we already created a
3572 // forward ref record.
3573 if (!Val) {
3574 auto I = ForwardRefValIDs.find(ID);
3575 if (I != ForwardRefValIDs.end())
3576 Val = I->second.first;
3577 }
3578
3579 // If we have the value in the symbol table or fwd-ref table, return it.
3580 if (Val)
3581 return P.checkValidVariableType(Loc, "%" + Twine(ID), Ty, Val);
3582
3583 if (!Ty->isFirstClassType()) {
3584 P.error(Loc, "invalid use of a non-first-class type");
3585 return nullptr;
3586 }
3587
3588 // Otherwise, create a new forward reference for this value and remember it.
3589 Value *FwdVal;
3590 if (Ty->isLabelTy()) {
3591 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
3592 } else {
3593 FwdVal = new Argument(Ty);
3594 }
3595
3596 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
3597 return FwdVal;
3598}
3599
3600/// setInstName - After an instruction is parsed and inserted into its
3601/// basic block, this installs its name.
3602bool LLParser::PerFunctionState::setInstName(int NameID,
3603 const std::string &NameStr,
3604 LocTy NameLoc, Instruction *Inst) {
3605 // If this instruction has void type, it cannot have a name or ID specified.
3606 if (Inst->getType()->isVoidTy()) {
3607 if (NameID != -1 || !NameStr.empty())
3608 return P.error(NameLoc, "instructions returning void cannot have a name");
3609 return false;
3610 }
3611
3612 // If this was a numbered instruction, verify that the instruction is the
3613 // expected value and resolve any forward references.
3614 if (NameStr.empty()) {
3615 // If neither a name nor an ID was specified, just use the next ID.
3616 if (NameID == -1)
3617 NameID = NumberedVals.getNext();
3618
3619 if (P.checkValueID(NameLoc, "instruction", "%", NumberedVals.getNext(),
3620 NameID))
3621 return true;
3622
3623 auto FI = ForwardRefValIDs.find(NameID);
3624 if (FI != ForwardRefValIDs.end()) {
3625 Value *Sentinel = FI->second.first;
3626 if (Sentinel->getType() != Inst->getType())
3627 return P.error(NameLoc, "instruction forward referenced with type '" +
3628 getTypeString(FI->second.first->getType()) +
3629 "'");
3630
3631 Sentinel->replaceAllUsesWith(Inst);
3632 Sentinel->deleteValue();
3633 ForwardRefValIDs.erase(FI);
3634 }
3635
3636 NumberedVals.add(NameID, Inst);
3637 return false;
3638 }
3639
3640 // Otherwise, the instruction had a name. Resolve forward refs and set it.
3641 auto FI = ForwardRefVals.find(NameStr);
3642 if (FI != ForwardRefVals.end()) {
3643 Value *Sentinel = FI->second.first;
3644 if (Sentinel->getType() != Inst->getType())
3645 return P.error(NameLoc, "instruction forward referenced with type '" +
3646 getTypeString(FI->second.first->getType()) +
3647 "'");
3648
3649 Sentinel->replaceAllUsesWith(Inst);
3650 Sentinel->deleteValue();
3651 ForwardRefVals.erase(FI);
3652 }
3653
3654 // Set the name on the instruction.
3655 Inst->setName(NameStr);
3656
3657 if (Inst->getName() != NameStr)
3658 return P.error(NameLoc, "multiple definition of local value named '" +
3659 NameStr + "'");
3660 return false;
3661}
3662
3663/// getBB - Get a basic block with the specified name or ID, creating a
3664/// forward reference record if needed.
3665BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name,
3666 LocTy Loc) {
3667 return dyn_cast_or_null<BasicBlock>(
3668 getVal(Name, Type::getLabelTy(F.getContext()), Loc));
3669}
3670
3671BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) {
3672 return dyn_cast_or_null<BasicBlock>(
3673 getVal(ID, Type::getLabelTy(F.getContext()), Loc));
3674}
3675
3676/// defineBB - Define the specified basic block, which is either named or
3677/// unnamed. If there is an error, this returns null otherwise it returns
3678/// the block being defined.
3679BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
3680 int NameID, LocTy Loc) {
3681 BasicBlock *BB;
3682 if (Name.empty()) {
3683 if (NameID != -1) {
3684 if (P.checkValueID(Loc, "label", "", NumberedVals.getNext(), NameID))
3685 return nullptr;
3686 } else {
3687 NameID = NumberedVals.getNext();
3688 }
3689 BB = getBB(NameID, Loc);
3690 if (!BB) {
3691 P.error(Loc, "unable to create block numbered '" + Twine(NameID) + "'");
3692 return nullptr;
3693 }
3694 } else {
3695 BB = getBB(Name, Loc);
3696 if (!BB) {
3697 P.error(Loc, "unable to create block named '" + Name + "'");
3698 return nullptr;
3699 }
3700 }
3701
3702 // Move the block to the end of the function. Forward ref'd blocks are
3703 // inserted wherever they happen to be referenced.
3704 F.splice(F.end(), &F, BB->getIterator());
3705
3706 // Remove the block from forward ref sets.
3707 if (Name.empty()) {
3708 ForwardRefValIDs.erase(NameID);
3709 NumberedVals.add(NameID, BB);
3710 } else {
3711 // BB forward references are already in the function symbol table.
3712 ForwardRefVals.erase(Name);
3713 }
3714
3715 return BB;
3716}
3717
3718//===----------------------------------------------------------------------===//
3719// Constants.
3720//===----------------------------------------------------------------------===//
3721
3722/// parseValID - parse an abstract value that doesn't necessarily have a
3723/// type implied. For example, if we parse "4" we don't know what integer type
3724/// it has. The value will later be combined with its type and checked for
3725/// basic correctness. PFS is used to convert function-local operands of
3726/// metadata (since metadata operands are not just parsed here but also
3727/// converted to values). PFS can be null when we are not parsing metadata
3728/// values inside a function.
3729bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
3730 ID.Loc = Lex.getLoc();
3731 switch (Lex.getKind()) {
3732 default:
3733 return tokError("expected value token");
3734 case lltok::GlobalID: // @42
3735 ID.UIntVal = Lex.getUIntVal();
3736 ID.Kind = ValID::t_GlobalID;
3737 break;
3738 case lltok::GlobalVar: // @foo
3739 ID.StrVal = Lex.getStrVal();
3740 ID.Kind = ValID::t_GlobalName;
3741 break;
3742 case lltok::LocalVarID: // %42
3743 ID.UIntVal = Lex.getUIntVal();
3744 ID.Kind = ValID::t_LocalID;
3745 break;
3746 case lltok::LocalVar: // %foo
3747 ID.StrVal = Lex.getStrVal();
3748 ID.Kind = ValID::t_LocalName;
3749 break;
3750 case lltok::APSInt:
3751 ID.APSIntVal = Lex.getAPSIntVal();
3752 ID.Kind = ValID::t_APSInt;
3753 break;
3754 case lltok::APFloat:
3755 ID.APFloatVal = Lex.getAPFloatVal();
3756 ID.Kind = ValID::t_APFloat;
3757 break;
3758 case lltok::kw_true:
3759 ID.ConstantVal = ConstantInt::getTrue(Context);
3760 ID.Kind = ValID::t_Constant;
3761 break;
3762 case lltok::kw_false:
3763 ID.ConstantVal = ConstantInt::getFalse(Context);
3764 ID.Kind = ValID::t_Constant;
3765 break;
3766 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
3767 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
3768 case lltok::kw_poison: ID.Kind = ValID::t_Poison; break;
3769 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
3770 case lltok::kw_none: ID.Kind = ValID::t_None; break;
3771
3772 case lltok::lbrace: {
3773 // ValID ::= '{' ConstVector '}'
3774 Lex.Lex();
3776 if (parseGlobalValueVector(Elts) ||
3777 parseToken(lltok::rbrace, "expected end of struct constant"))
3778 return true;
3779
3780 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
3781 ID.UIntVal = Elts.size();
3782 memcpy(ID.ConstantStructElts.get(), Elts.data(),
3783 Elts.size() * sizeof(Elts[0]));
3785 return false;
3786 }
3787 case lltok::less: {
3788 // ValID ::= '<' ConstVector '>' --> Vector.
3789 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
3790 Lex.Lex();
3791 bool isPackedStruct = EatIfPresent(lltok::lbrace);
3792
3794 LocTy FirstEltLoc = Lex.getLoc();
3795 if (parseGlobalValueVector(Elts) ||
3796 (isPackedStruct &&
3797 parseToken(lltok::rbrace, "expected end of packed struct")) ||
3798 parseToken(lltok::greater, "expected end of constant"))
3799 return true;
3800
3801 if (isPackedStruct) {
3802 ID.ConstantStructElts = std::make_unique<Constant *[]>(Elts.size());
3803 memcpy(ID.ConstantStructElts.get(), Elts.data(),
3804 Elts.size() * sizeof(Elts[0]));
3805 ID.UIntVal = Elts.size();
3807 return false;
3808 }
3809
3810 if (Elts.empty())
3811 return error(ID.Loc, "constant vector must not be empty");
3812
3813 if (!Elts[0]->getType()->isIntegerTy() &&
3814 !Elts[0]->getType()->isFloatingPointTy() &&
3815 !Elts[0]->getType()->isPointerTy())
3816 return error(
3817 FirstEltLoc,
3818 "vector elements must have integer, pointer or floating point type");
3819
3820 // Verify that all the vector elements have the same type.
3821 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
3822 if (Elts[i]->getType() != Elts[0]->getType())
3823 return error(FirstEltLoc, "vector element #" + Twine(i) +
3824 " is not of type '" +
3825 getTypeString(Elts[0]->getType()));
3826
3827 ID.ConstantVal = ConstantVector::get(Elts);
3828 ID.Kind = ValID::t_Constant;
3829 return false;
3830 }
3831 case lltok::lsquare: { // Array Constant
3832 Lex.Lex();
3834 LocTy FirstEltLoc = Lex.getLoc();
3835 if (parseGlobalValueVector(Elts) ||
3836 parseToken(lltok::rsquare, "expected end of array constant"))
3837 return true;
3838
3839 // Handle empty element.
3840 if (Elts.empty()) {
3841 // Use undef instead of an array because it's inconvenient to determine
3842 // the element type at this point, there being no elements to examine.
3843 ID.Kind = ValID::t_EmptyArray;
3844 return false;
3845 }
3846
3847 if (!Elts[0]->getType()->isFirstClassType())
3848 return error(FirstEltLoc, "invalid array element type: " +
3849 getTypeString(Elts[0]->getType()));
3850
3851 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
3852
3853 // Verify all elements are correct type!
3854 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
3855 if (Elts[i]->getType() != Elts[0]->getType())
3856 return error(FirstEltLoc, "array element #" + Twine(i) +
3857 " is not of type '" +
3858 getTypeString(Elts[0]->getType()));
3859 }
3860
3861 ID.ConstantVal = ConstantArray::get(ATy, Elts);
3862 ID.Kind = ValID::t_Constant;
3863 return false;
3864 }
3865 case lltok::kw_c: // c "foo"
3866 Lex.Lex();
3867 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
3868 false);
3869 if (parseToken(lltok::StringConstant, "expected string"))
3870 return true;
3871 ID.Kind = ValID::t_Constant;
3872 return false;
3873
3874 case lltok::kw_asm: {
3875 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
3876 // STRINGCONSTANT
3877 bool HasSideEffect, AlignStack, AsmDialect, CanThrow;
3878 Lex.Lex();
3879 if (parseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
3880 parseOptionalToken(lltok::kw_alignstack, AlignStack) ||
3881 parseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
3882 parseOptionalToken(lltok::kw_unwind, CanThrow) ||
3883 parseStringConstant(ID.StrVal) ||
3884 parseToken(lltok::comma, "expected comma in inline asm expression") ||
3885 parseToken(lltok::StringConstant, "expected constraint string"))
3886 return true;
3887 ID.StrVal2 = Lex.getStrVal();
3888 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) |
3889 (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3);
3890 ID.Kind = ValID::t_InlineAsm;
3891 return false;
3892 }
3893
3895 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
3896 Lex.Lex();
3897
3898 ValID Fn, Label;
3899
3900 if (parseToken(lltok::lparen, "expected '(' in block address expression") ||
3901 parseValID(Fn, PFS) ||
3902 parseToken(lltok::comma,
3903 "expected comma in block address expression") ||
3904 parseValID(Label, PFS) ||
3905 parseToken(lltok::rparen, "expected ')' in block address expression"))
3906 return true;
3907
3909 return error(Fn.Loc, "expected function name in blockaddress");
3910 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
3911 return error(Label.Loc, "expected basic block name in blockaddress");
3912
3913 // Try to find the function (but skip it if it's forward-referenced).
3914 GlobalValue *GV = nullptr;
3915 if (Fn.Kind == ValID::t_GlobalID) {
3916 GV = NumberedVals.get(Fn.UIntVal);
3917 } else if (!ForwardRefVals.count(Fn.StrVal)) {
3918 GV = M->getNamedValue(Fn.StrVal);
3919 }
3920 Function *F = nullptr;
3921 if (GV) {
3922 // Confirm that it's actually a function with a definition.
3923 if (!isa<Function>(GV))
3924 return error(Fn.Loc, "expected function name in blockaddress");
3925 F = cast<Function>(GV);
3926 if (F->isDeclaration())
3927 return error(Fn.Loc, "cannot take blockaddress inside a declaration");
3928 }
3929
3930 if (!F) {
3931 // Make a global variable as a placeholder for this reference.
3932 GlobalValue *&FwdRef =
3933 ForwardRefBlockAddresses.insert(std::make_pair(
3934 std::move(Fn),
3935 std::map<ValID, GlobalValue *>()))
3936 .first->second.insert(std::make_pair(std::move(Label), nullptr))
3937 .first->second;
3938 if (!FwdRef) {
3939 unsigned FwdDeclAS;
3940 if (ExpectedTy) {
3941 // If we know the type that the blockaddress is being assigned to,
3942 // we can use the address space of that type.
3943 if (!ExpectedTy->isPointerTy())
3944 return error(ID.Loc,
3945 "type of blockaddress must be a pointer and not '" +
3946 getTypeString(ExpectedTy) + "'");
3947 FwdDeclAS = ExpectedTy->getPointerAddressSpace();
3948 } else if (PFS) {
3949 // Otherwise, we default the address space of the current function.
3950 FwdDeclAS = PFS->getFunction().getAddressSpace();
3951 } else {
3952 llvm_unreachable("Unknown address space for blockaddress");
3953 }
3954 FwdRef = new GlobalVariable(
3955 *M, Type::getInt8Ty(Context), false, GlobalValue::InternalLinkage,
3956 nullptr, "", nullptr, GlobalValue::NotThreadLocal, FwdDeclAS);
3957 }
3958
3959 ID.ConstantVal = FwdRef;
3960 ID.Kind = ValID::t_Constant;
3961 return false;
3962 }
3963
3964 // We found the function; now find the basic block. Don't use PFS, since we
3965 // might be inside a constant expression.
3966 BasicBlock *BB;
3967 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
3968 if (Label.Kind == ValID::t_LocalID)
3969 BB = BlockAddressPFS->getBB(Label.UIntVal, Label.Loc);
3970 else
3971 BB = BlockAddressPFS->getBB(Label.StrVal, Label.Loc);
3972 if (!BB)
3973 return error(Label.Loc, "referenced value is not a basic block");
3974 } else {
3975 if (Label.Kind == ValID::t_LocalID)
3976 return error(Label.Loc, "cannot take address of numeric label after "
3977 "the function is defined");
3978 BB = dyn_cast_or_null<BasicBlock>(
3979 F->getValueSymbolTable()->lookup(Label.StrVal));
3980 if (!BB)
3981 return error(Label.Loc, "referenced value is not a basic block");
3982 }
3983
3984 ID.ConstantVal = BlockAddress::get(F, BB);
3985 ID.Kind = ValID::t_Constant;
3986 return false;
3987 }
3988
3990 // ValID ::= 'dso_local_equivalent' @foo
3991 Lex.Lex();
3992
3993 ValID Fn;
3994
3995 if (parseValID(Fn, PFS))
3996 return true;
3997
3999 return error(Fn.Loc,
4000 "expected global value name in dso_local_equivalent");
4001
4002 // Try to find the function (but skip it if it's forward-referenced).
4003 GlobalValue *GV = nullptr;
4004 if (Fn.Kind == ValID::t_GlobalID) {
4005 GV = NumberedVals.get(Fn.UIntVal);
4006 } else if (!ForwardRefVals.count(Fn.StrVal)) {
4007 GV = M->getNamedValue(Fn.StrVal);
4008 }
4009
4010 if (!GV) {
4011 // Make a placeholder global variable as a placeholder for this reference.
4012 auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID)
4013 ? ForwardRefDSOLocalEquivalentIDs
4014 : ForwardRefDSOLocalEquivalentNames;
4015 GlobalValue *&FwdRef = FwdRefMap.try_emplace(Fn, nullptr).first->second;
4016 if (!FwdRef) {
4017 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
4018 GlobalValue::InternalLinkage, nullptr, "",
4020 }
4021
4022 ID.ConstantVal = FwdRef;
4023 ID.Kind = ValID::t_Constant;
4024 return false;
4025 }
4026
4027 if (!GV->getValueType()->isFunctionTy())
4028 return error(Fn.Loc, "expected a function, alias to function, or ifunc "
4029 "in dso_local_equivalent");
4030
4031 ID.ConstantVal = DSOLocalEquivalent::get(GV);
4032 ID.Kind = ValID::t_Constant;
4033 return false;
4034 }
4035
4036 case lltok::kw_no_cfi: {
4037 // ValID ::= 'no_cfi' @foo
4038 Lex.Lex();
4039
4040 if (parseValID(ID, PFS))
4041 return true;
4042
4043 if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName)
4044 return error(ID.Loc, "expected global value name in no_cfi");
4045
4046 ID.NoCFI = true;
4047 return false;
4048 }
4049
4050 case lltok::kw_trunc:
4051 case lltok::kw_bitcast:
4053 case lltok::kw_inttoptr:
4054 case lltok::kw_ptrtoint: {
4055 unsigned Opc = Lex.getUIntVal();
4056 Type *DestTy = nullptr;
4057 Constant *SrcVal;
4058 Lex.Lex();
4059 if (parseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
4060 parseGlobalTypeAndValue(SrcVal) ||
4061 parseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
4062 parseType(DestTy) ||
4063 parseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
4064 return true;
4065 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
4066 return error(ID.Loc, "invalid cast opcode for cast from '" +
4067 getTypeString(SrcVal->getType()) + "' to '" +
4068 getTypeString(DestTy) + "'");
4070 SrcVal, DestTy);
4071 ID.Kind = ValID::t_Constant;
4072 return false;
4073 }
4075 return error(ID.Loc, "extractvalue constexprs are no longer supported");
4077 return error(ID.Loc, "insertvalue constexprs are no longer supported");
4078 case lltok::kw_udiv:
4079 return error(ID.Loc, "udiv constexprs are no longer supported");
4080 case lltok::kw_sdiv:
4081 return error(ID.Loc, "sdiv constexprs are no longer supported");
4082 case lltok::kw_urem:
4083 return error(ID.Loc, "urem constexprs are no longer supported");
4084 case lltok::kw_srem:
4085 return error(ID.Loc, "srem constexprs are no longer supported");
4086 case lltok::kw_fadd:
4087 return error(ID.Loc, "fadd constexprs are no longer supported");
4088 case lltok::kw_fsub:
4089 return error(ID.Loc, "fsub constexprs are no longer supported");
4090 case lltok::kw_fmul:
4091 return error(ID.Loc, "fmul constexprs are no longer supported");
4092 case lltok::kw_fdiv:
4093 return error(ID.Loc, "fdiv constexprs are no longer supported");
4094 case lltok::kw_frem:
4095 return error(ID.Loc, "frem constexprs are no longer supported");
4096 case lltok::kw_and:
4097 return error(ID.Loc, "and constexprs are no longer supported");
4098 case lltok::kw_or:
4099 return error(ID.Loc, "or constexprs are no longer supported");
4100 case lltok::kw_lshr:
4101 return error(ID.Loc, "lshr constexprs are no longer supported");
4102 case lltok::kw_ashr:
4103 return error(ID.Loc, "ashr constexprs are no longer supported");
4104 case lltok::kw_fneg:
4105 return error(ID.Loc, "fneg constexprs are no longer supported");
4106 case lltok::kw_select:
4107 return error(ID.Loc, "select constexprs are no longer supported");
4108 case lltok::kw_zext:
4109 return error(ID.Loc, "zext constexprs are no longer supported");
4110 case lltok::kw_sext:
4111 return error(ID.Loc, "sext constexprs are no longer supported");
4112 case lltok::kw_fptrunc:
4113 return error(ID.Loc, "fptrunc constexprs are no longer supported");
4114 case lltok::kw_fpext:
4115 return error(ID.Loc, "fpext constexprs are no longer supported");
4116 case lltok::kw_uitofp:
4117 return error(ID.Loc, "uitofp constexprs are no longer supported");
4118 case lltok::kw_sitofp:
4119 return error(ID.Loc, "sitofp constexprs are no longer supported");
4120 case lltok::kw_fptoui:
4121 return error(ID.Loc, "fptoui constexprs are no longer supported");
4122 case lltok::kw_fptosi:
4123 return error(ID.Loc, "fptosi constexprs are no longer supported");
4124 case lltok::kw_icmp:
4125 case lltok::kw_fcmp: {
4126 unsigned PredVal, Opc = Lex.getUIntVal();
4127 Constant *Val0, *Val1;
4128 Lex.Lex();
4129 if (parseCmpPredicate(PredVal, Opc) ||
4130 parseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
4131 parseGlobalTypeAndValue(Val0) ||
4132 parseToken(lltok::comma, "expected comma in compare constantexpr") ||
4133 parseGlobalTypeAndValue(Val1) ||
4134 parseToken(lltok::rparen, "expected ')' in compare constantexpr"))
4135 return true;
4136
4137 if (Val0->getType() != Val1->getType())
4138 return error(ID.Loc, "compare operands must have the same type");
4139
4140 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
4141
4142 if (Opc == Instruction::FCmp) {
4143 if (!Val0->getType()->isFPOrFPVectorTy())
4144 return error(ID.Loc, "fcmp requires floating point operands");
4145 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
4146 } else {
4147 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
4148 if (!Val0->getType()->isIntOrIntVectorTy() &&
4149 !Val0->getType()->isPtrOrPtrVectorTy())
4150 return error(ID.Loc, "icmp requires pointer or integer operands");
4151 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
4152 }
4153 ID.Kind = ValID::t_Constant;
4154 return false;
4155 }
4156
4157 // Binary Operators.
4158 case lltok::kw_add:
4159 case lltok::kw_sub:
4160 case lltok::kw_mul:
4161 case lltok::kw_shl:
4162 case lltok::kw_xor: {
4163 bool NUW = false;
4164 bool NSW = false;
4165 unsigned Opc = Lex.getUIntVal();
4166 Constant *Val0, *Val1;
4167 Lex.Lex();
4168 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
4169 Opc == Instruction::Mul || Opc == Instruction::Shl) {
4170 if (EatIfPresent(lltok::kw_nuw))
4171 NUW = true;
4172 if (EatIfPresent(lltok::kw_nsw)) {
4173 NSW = true;
4174 if (EatIfPresent(lltok::kw_nuw))
4175 NUW = true;
4176 }
4177 }
4178 if (parseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
4179 parseGlobalTypeAndValue(Val0) ||
4180 parseToken(lltok::comma, "expected comma in binary constantexpr") ||
4181 parseGlobalTypeAndValue(Val1) ||
4182 parseToken(lltok::rparen, "expected ')' in binary constantexpr"))
4183 return true;
4184 if (Val0->getType() != Val1->getType())
4185 return error(ID.Loc, "operands of constexpr must have same type");
4186 // Check that the type is valid for the operator.
4187 if (!Val0->getType()->isIntOrIntVectorTy())
4188 return error(ID.Loc,
4189 "constexpr requires integer or integer vector operands");
4190 unsigned Flags = 0;
4193 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1, Flags);
4194 ID.Kind = ValID::t_Constant;
4195 return false;
4196 }
4197
4198 case lltok::kw_splat: {
4199 Lex.Lex();
4200 if (parseToken(lltok::lparen, "expected '(' after vector splat"))
4201 return true;
4202 Constant *C;
4203 if (parseGlobalTypeAndValue(C))
4204 return true;
4205 if (parseToken(lltok::rparen, "expected ')' at end of vector splat"))
4206 return true;
4207
4208 ID.ConstantVal = C;
4210 return false;
4211 }
4212
4217 unsigned Opc = Lex.getUIntVal();
4219 bool InBounds = false;
4220 bool HasInRange = false;
4221 APSInt InRangeStart;
4222 APSInt InRangeEnd;
4223 Type *Ty;
4224 Lex.Lex();
4225
4226 if (Opc == Instruction::GetElementPtr) {
4227 InBounds = EatIfPresent(lltok::kw_inbounds);
4228 if (EatIfPresent(lltok::kw_inrange)) {
4229 if (parseToken(lltok::lparen, "expected '('"))
4230 return true;
4231 if (Lex.getKind() != lltok::APSInt)
4232 return tokError("expected integer");
4233 InRangeStart = Lex.getAPSIntVal();
4234 Lex.Lex();
4235 if (parseToken(lltok::comma, "expected ','"))
4236 return true;
4237 if (Lex.getKind() != lltok::APSInt)
4238 return tokError("expected integer");
4239 InRangeEnd = Lex.getAPSIntVal();
4240 Lex.Lex();
4241 if (parseToken(lltok::rparen, "expected ')'"))
4242 return true;
4243 HasInRange = true;
4244 }
4245 }
4246
4247 if (parseToken(lltok::lparen, "expected '(' in constantexpr"))
4248 return true;
4249
4250 if (Opc == Instruction::GetElementPtr) {
4251 if (parseType(Ty) ||
4252 parseToken(lltok::comma, "expected comma after getelementptr's type"))
4253 return true;
4254 }
4255
4256 if (parseGlobalValueVector(Elts) ||
4257 parseToken(lltok::rparen, "expected ')' in constantexpr"))
4258 return true;
4259
4260 if (Opc == Instruction::GetElementPtr) {
4261 if (Elts.size() == 0 ||
4262 !Elts[0]->getType()->isPtrOrPtrVectorTy())
4263 return error(ID.Loc, "base of getelementptr must be a pointer");
4264
4265 Type *BaseType = Elts[0]->getType();
4266 std::optional<ConstantRange> InRange;
4267 if (HasInRange) {
4268 unsigned IndexWidth =
4269 M->getDataLayout().getIndexTypeSizeInBits(BaseType);
4270 InRangeStart = InRangeStart.extOrTrunc(IndexWidth);
4271 InRangeEnd = InRangeEnd.extOrTrunc(IndexWidth);
4272 if (InRangeStart.sge(InRangeEnd))
4273 return error(ID.Loc, "expected end to be larger than start");
4274 InRange = ConstantRange::getNonEmpty(InRangeStart, InRangeEnd);
4275 }
4276
4277 unsigned GEPWidth =
4278 BaseType->isVectorTy()
4279 ? cast<FixedVectorType>(BaseType)->getNumElements()
4280 : 0;
4281
4282 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
4283 for (Constant *Val : Indices) {
4284 Type *ValTy = Val->getType();
4285 if (!ValTy->isIntOrIntVectorTy())
4286 return error(ID.Loc, "getelementptr index must be an integer");
4287 if (auto *ValVTy = dyn_cast<VectorType>(ValTy)) {
4288 unsigned ValNumEl = cast<FixedVectorType>(ValVTy)->getNumElements();
4289 if (GEPWidth && (ValNumEl != GEPWidth))
4290 return error(
4291 ID.Loc,
4292 "getelementptr vector index has a wrong number of elements");
4293 // GEPWidth may have been unknown because the base is a scalar,
4294 // but it is known now.
4295 GEPWidth = ValNumEl;
4296 }
4297 }
4298
4299 SmallPtrSet<Type*, 4> Visited;
4300 if (!Indices.empty() && !Ty->isSized(&Visited))
4301 return error(ID.Loc, "base element of getelementptr must be sized");
4302
4303 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
4304 return error(ID.Loc, "invalid getelementptr indices");
4305
4306 ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices,
4307 InBounds, InRange);
4308 } else if (Opc == Instruction::ShuffleVector) {
4309 if (Elts.size() != 3)
4310 return error(ID.Loc, "expected three operands to shufflevector");
4311 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4312 return error(ID.Loc, "invalid operands to shufflevector");
4314 ShuffleVectorInst::getShuffleMask(cast<Constant>(Elts[2]), Mask);
4315 ID.ConstantVal = ConstantExpr::getShuffleVector(Elts[0], Elts[1], Mask);
4316 } else if (Opc == Instruction::ExtractElement) {
4317 if (Elts.size() != 2)
4318 return error(ID.Loc, "expected two operands to extractelement");
4319 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
4320 return error(ID.Loc, "invalid extractelement operands");
4321 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
4322 } else {
4323 assert(Opc == Instruction::InsertElement && "Unknown opcode");
4324 if (Elts.size() != 3)
4325 return error(ID.Loc, "expected three operands to insertelement");
4326 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
4327 return error(ID.Loc, "invalid insertelement operands");
4328 ID.ConstantVal =
4329 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
4330 }
4331
4332 ID.Kind = ValID::t_Constant;
4333 return false;
4334 }
4335 }
4336
4337 Lex.Lex();
4338 return false;
4339}
4340
4341/// parseGlobalValue - parse a global value with the specified type.
4342bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) {
4343 C = nullptr;
4344 ValID ID;
4345 Value *V = nullptr;
4346 bool Parsed = parseValID(ID, /*PFS=*/nullptr, Ty) ||
4347 convertValIDToValue(Ty, ID, V, nullptr);
4348 if (V && !(C = dyn_cast<Constant>(V)))
4349 return error(ID.Loc, "global values must be constants");
4350 return Parsed;
4351}
4352
4353bool LLParser::parseGlobalTypeAndValue(Constant *&V) {
4354 Type *Ty = nullptr;
4355 return parseType(Ty) || parseGlobalValue(Ty, V);
4356}
4357
4358bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
4359 C = nullptr;
4360
4361 LocTy KwLoc = Lex.getLoc();
4362 if (!EatIfPresent(lltok::kw_comdat))
4363 return false;
4364
4365 if (EatIfPresent(lltok::lparen)) {
4366 if (Lex.getKind() != lltok::ComdatVar)
4367 return tokError("expected comdat variable");
4368 C = getComdat(Lex.getStrVal(), Lex.getLoc());
4369 Lex.Lex();
4370 if (parseToken(lltok::rparen, "expected ')' after comdat var"))
4371 return true;
4372 } else {
4373 if (GlobalName.empty())
4374 return tokError("comdat cannot be unnamed");
4375 C = getComdat(std::string(GlobalName), KwLoc);
4376 }
4377
4378 return false;
4379}
4380
4381/// parseGlobalValueVector
4382/// ::= /*empty*/
4383/// ::= TypeAndValue (',' TypeAndValue)*
4384bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
4385 // Empty list.
4386 if (Lex.getKind() == lltok::rbrace ||
4387 Lex.getKind() == lltok::rsquare ||
4388 Lex.getKind() == lltok::greater ||
4389 Lex.getKind() == lltok::rparen)
4390 return false;
4391
4392 do {
4393 // Let the caller deal with inrange.
4394 if (Lex.getKind() == lltok::kw_inrange)
4395 return false;
4396
4397 Constant *C;
4398 if (parseGlobalTypeAndValue(C))
4399 return true;
4400 Elts.push_back(C);
4401 } while (EatIfPresent(lltok::comma));
4402
4403 return false;
4404}
4405
4406bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
4408 if (parseMDNodeVector(Elts))
4409 return true;
4410
4411 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
4412 return false;
4413}
4414
4415/// MDNode:
4416/// ::= !{ ... }
4417/// ::= !7
4418/// ::= !DILocation(...)
4419bool LLParser::parseMDNode(MDNode *&N) {
4420 if (Lex.getKind() == lltok::MetadataVar)
4421 return parseSpecializedMDNode(N);
4422
4423 return parseToken(lltok::exclaim, "expected '!' here") || parseMDNodeTail(N);
4424}
4425
4426bool LLParser::parseMDNodeTail(MDNode *&N) {
4427 // !{ ... }
4428 if (Lex.getKind() == lltok::lbrace)
4429 return parseMDTuple(N);
4430
4431 // !42
4432 return parseMDNodeID(N);
4433}
4434
4435namespace {
4436
4437/// Structure to represent an optional metadata field.
4438template <class FieldTy> struct MDFieldImpl {
4439 typedef MDFieldImpl ImplTy;
4440 FieldTy Val;
4441 bool Seen;
4442
4443 void assign(FieldTy Val) {
4444 Seen = true;
4445 this->Val = std::move(Val);
4446 }
4447
4448 explicit MDFieldImpl(FieldTy Default)
4449 : Val(std::move(Default)), Seen(false) {}
4450};
4451
4452/// Structure to represent an optional metadata field that
4453/// can be of either type (A or B) and encapsulates the
4454/// MD<typeofA>Field and MD<typeofB>Field structs, so not
4455/// to reimplement the specifics for representing each Field.
4456template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
4457 typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
4458 FieldTypeA A;
4459 FieldTypeB B;
4460 bool Seen;
4461
4462 enum {
4463 IsInvalid = 0,
4464 IsTypeA = 1,
4465 IsTypeB = 2
4466 } WhatIs;
4467
4468 void assign(FieldTypeA A) {
4469 Seen = true;
4470 this->A = std::move(A);
4471 WhatIs = IsTypeA;
4472 }
4473
4474 void assign(FieldTypeB B) {
4475 Seen = true;
4476 this->B = std::move(B);
4477 WhatIs = IsTypeB;
4478 }
4479
4480 explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
4481 : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
4482 WhatIs(IsInvalid) {}
4483};
4484
4485struct MDUnsignedField : public MDFieldImpl<uint64_t> {
4486 uint64_t Max;
4487
4488 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
4489 : ImplTy(Default), Max(Max) {}
4490};
4491
4492struct LineField : public MDUnsignedField {
4493 LineField() : MDUnsignedField(0, UINT32_MAX) {}
4494};
4495
4496struct ColumnField : public MDUnsignedField {
4497 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
4498};
4499
4500struct DwarfTagField : public MDUnsignedField {
4501 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
4502 DwarfTagField(dwarf::Tag DefaultTag)
4503 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
4504};
4505
4506struct DwarfMacinfoTypeField : public MDUnsignedField {
4507 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
4508 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
4509 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
4510};
4511
4512struct DwarfAttEncodingField : public MDUnsignedField {
4513 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
4514};
4515
4516struct DwarfVirtualityField : public MDUnsignedField {
4517 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
4518};
4519
4520struct DwarfLangField : public MDUnsignedField {
4521 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
4522};
4523
4524struct DwarfCCField : public MDUnsignedField {
4525 DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
4526};
4527
4528struct EmissionKindField : public MDUnsignedField {
4529 EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
4530};
4531
4532struct NameTableKindField : public MDUnsignedField {
4533 NameTableKindField()
4534 : MDUnsignedField(
4535 0, (unsigned)
4536 DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
4537};
4538
4539struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
4540 DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
4541};
4542
4543struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> {
4544 DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {}
4545};
4546
4547struct MDAPSIntField : public MDFieldImpl<APSInt> {
4548 MDAPSIntField() : ImplTy(APSInt()) {}
4549};
4550
4551struct MDSignedField : public MDFieldImpl<int64_t> {
4552 int64_t Min = INT64_MIN;
4553 int64_t Max = INT64_MAX;
4554
4555 MDSignedField(int64_t Default = 0)
4556 : ImplTy(Default) {}
4557 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
4558 : ImplTy(Default), Min(Min), Max(Max) {}
4559};
4560
4561struct MDBoolField : public MDFieldImpl<bool> {
4562 MDBoolField(bool Default = false) : ImplTy(Default) {}
4563};
4564
4565struct MDField : public MDFieldImpl<Metadata *> {
4566 bool AllowNull;
4567
4568 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
4569};
4570
4571struct MDStringField : public MDFieldImpl<MDString *> {
4572 bool AllowEmpty;
4573 MDStringField(bool AllowEmpty = true)
4574 : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
4575};
4576
4577struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
4578 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
4579};
4580
4581struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
4582 ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
4583};
4584
4585struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
4586 MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
4587 : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
4588
4589 MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
4590 bool AllowNull = true)
4591 : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
4592
4593 bool isMDSignedField() const { return WhatIs == IsTypeA; }
4594 bool isMDField() const { return WhatIs == IsTypeB; }
4595 int64_t getMDSignedValue() const {
4596 assert(isMDSignedField() && "Wrong field type");
4597 return A.Val;
4598 }
4599 Metadata *getMDFieldValue() const {
4600 assert(isMDField() && "Wrong field type");
4601 return B.Val;
4602 }
4603};
4604
4605} // end anonymous namespace
4606
4607namespace llvm {
4608
4609template <>
4610bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) {
4611 if (Lex.getKind() != lltok::APSInt)
4612 return tokError("expected integer");
4613
4614 Result.assign(Lex.getAPSIntVal());
4615 Lex.Lex();
4616 return false;
4617}
4618
4619template <>
4620bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4621 MDUnsignedField &Result) {
4622 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
4623 return tokError("expected unsigned integer");
4624
4625 auto &U = Lex.getAPSIntVal();
4626 if (U.ugt(Result.Max))
4627 return tokError("value for '" + Name + "' too large, limit is " +
4628 Twine(Result.Max));
4629 Result.assign(U.getZExtValue());
4630 assert(Result.Val <= Result.Max && "Expected value in range");
4631 Lex.Lex();
4632 return false;
4633}
4634
4635template <>
4636bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) {
4637 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4638}
4639template <>
4640bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
4641 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4642}
4643
4644template <>
4645bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
4646 if (Lex.getKind() == lltok::APSInt)
4647 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4648
4649 if (Lex.getKind() != lltok::DwarfTag)
4650 return tokError("expected DWARF tag");
4651
4652 unsigned Tag = dwarf::getTag(Lex.getStrVal());
4654 return tokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
4655 assert(Tag <= Result.Max && "Expected valid DWARF tag");
4656
4657 Result.assign(Tag);
4658 Lex.Lex();
4659 return false;
4660}
4661
4662template <>
4663bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4664 DwarfMacinfoTypeField &Result) {
4665 if (Lex.getKind() == lltok::APSInt)
4666 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4667
4668 if (Lex.getKind() != lltok::DwarfMacinfo)
4669 return tokError("expected DWARF macinfo type");
4670
4671 unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
4672 if (Macinfo == dwarf::DW_MACINFO_invalid)
4673 return tokError("invalid DWARF macinfo type" + Twine(" '") +
4674 Lex.getStrVal() + "'");
4675 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
4676
4677 Result.assign(Macinfo);
4678 Lex.Lex();
4679 return false;
4680}
4681
4682template <>
4683bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4684 DwarfVirtualityField &Result) {
4685 if (Lex.getKind() == lltok::APSInt)
4686 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4687
4688 if (Lex.getKind() != lltok::DwarfVirtuality)
4689 return tokError("expected DWARF virtuality code");
4690
4691 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
4692 if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
4693 return tokError("invalid DWARF virtuality code" + Twine(" '") +
4694 Lex.getStrVal() + "'");
4695 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
4696 Result.assign(Virtuality);
4697 Lex.Lex();
4698 return false;
4699}
4700
4701template <>
4702bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
4703 if (Lex.getKind() == lltok::APSInt)
4704 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4705
4706 if (Lex.getKind() != lltok::DwarfLang)
4707 return tokError("expected DWARF language");
4708
4709 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
4710 if (!Lang)
4711 return tokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
4712 "'");
4713 assert(Lang <= Result.Max && "Expected valid DWARF language");
4714 Result.assign(Lang);
4715 Lex.Lex();
4716 return false;
4717}
4718
4719template <>
4720bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
4721 if (Lex.getKind() == lltok::APSInt)
4722 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4723
4724 if (Lex.getKind() != lltok::DwarfCC)
4725 return tokError("expected DWARF calling convention");
4726
4727 unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
4728 if (!CC)
4729 return tokError("invalid DWARF calling convention" + Twine(" '") +
4730 Lex.getStrVal() + "'");
4731 assert(CC <= Result.Max && "Expected valid DWARF calling convention");
4732 Result.assign(CC);
4733 Lex.Lex();
4734 return false;
4735}
4736
4737template <>
4738bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4739 EmissionKindField &Result) {
4740 if (Lex.getKind() == lltok::APSInt)
4741 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4742
4743 if (Lex.getKind() != lltok::EmissionKind)
4744 return tokError("expected emission kind");
4745
4746 auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
4747 if (!Kind)
4748 return tokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
4749 "'");
4750 assert(*Kind <= Result.Max && "Expected valid emission kind");
4751 Result.assign(*Kind);
4752 Lex.Lex();
4753 return false;
4754}
4755
4756template <>
4757bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4758 NameTableKindField &Result) {
4759 if (Lex.getKind() == lltok::APSInt)
4760 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4761
4762 if (Lex.getKind() != lltok::NameTableKind)
4763 return tokError("expected nameTable kind");
4764
4765 auto Kind = DICompileUnit::getNameTableKind(Lex.getStrVal());
4766 if (!Kind)
4767 return tokError("invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
4768 "'");
4769 assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
4770 Result.assign((unsigned)*Kind);
4771 Lex.Lex();
4772 return false;
4773}
4774
4775template <>
4776bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4777 DwarfAttEncodingField &Result) {
4778 if (Lex.getKind() == lltok::APSInt)
4779 return parseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
4780
4781 if (Lex.getKind() != lltok::DwarfAttEncoding)
4782 return tokError("expected DWARF type attribute encoding");
4783
4784 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
4785 if (!Encoding)
4786 return tokError("invalid DWARF type attribute encoding" + Twine(" '") +
4787 Lex.getStrVal() + "'");
4788 assert(Encoding <= Result.Max && "Expected valid DWARF language");
4789 Result.assign(Encoding);
4790 Lex.Lex();
4791 return false;
4792}
4793
4794/// DIFlagField
4795/// ::= uint32
4796/// ::= DIFlagVector
4797/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
4798template <>
4799bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
4800
4801 // parser for a single flag.
4802 auto parseFlag = [&](DINode::DIFlags &Val) {
4803 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
4804 uint32_t TempVal = static_cast<uint32_t>(Val);
4805 bool Res = parseUInt32(TempVal);
4806 Val = static_cast<DINode::DIFlags>(TempVal);
4807 return Res;
4808 }
4809
4810 if (Lex.getKind() != lltok::DIFlag)
4811 return tokError("expected debug info flag");
4812
4813 Val = DINode::getFlag(Lex.getStrVal());
4814 if (!Val)
4815 return tokError(Twine("invalid debug info flag '") + Lex.getStrVal() +
4816 "'");
4817 Lex.Lex();
4818 return false;
4819 };
4820
4821 // parse the flags and combine them together.
4822 DINode::DIFlags Combined = DINode::FlagZero;
4823 do {
4824 DINode::DIFlags Val;
4825 if (parseFlag(Val))
4826 return true;
4827 Combined |= Val;
4828 } while (EatIfPresent(lltok::bar));
4829
4830 Result.assign(Combined);
4831 return false;
4832}
4833
4834/// DISPFlagField
4835/// ::= uint32
4836/// ::= DISPFlagVector
4837/// ::= DISPFlagVector '|' DISPFlag* '|' uint32
4838template <>
4839bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) {
4840
4841 // parser for a single flag.
4842 auto parseFlag = [&](DISubprogram::DISPFlags &Val) {
4843 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
4844 uint32_t TempVal = static_cast<uint32_t>(Val);
4845 bool Res = parseUInt32(TempVal);
4846 Val = static_cast<DISubprogram::DISPFlags>(TempVal);
4847 return Res;
4848 }
4849
4850 if (Lex.getKind() != lltok::DISPFlag)
4851 return tokError("expected debug info flag");
4852
4853 Val = DISubprogram::getFlag(Lex.getStrVal());
4854 if (!Val)
4855 return tokError(Twine("invalid subprogram debug info flag '") +
4856 Lex.getStrVal() + "'");
4857 Lex.Lex();
4858 return false;
4859 };
4860
4861 // parse the flags and combine them together.
4862 DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero;
4863 do {
4865 if (parseFlag(Val))
4866 return true;
4867 Combined |= Val;
4868 } while (EatIfPresent(lltok::bar));
4869
4870 Result.assign(Combined);
4871 return false;
4872}
4873
4874template <>
4875bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) {
4876 if (Lex.getKind() != lltok::APSInt)
4877 return tokError("expected signed integer");
4878
4879 auto &S = Lex.getAPSIntVal();
4880 if (S < Result.Min)
4881 return tokError("value for '" + Name + "' too small, limit is " +
4882 Twine(Result.Min));
4883 if (S > Result.Max)
4884 return tokError("value for '" + Name + "' too large, limit is " +
4885 Twine(Result.Max));
4886 Result.assign(S.getExtValue());
4887 assert(Result.Val >= Result.Min && "Expected value in range");
4888 assert(Result.Val <= Result.Max && "Expected value in range");
4889 Lex.Lex();
4890 return false;
4891}
4892
4893template <>
4894bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
4895 switch (Lex.getKind()) {
4896 default:
4897 return tokError("expected 'true' or 'false'");
4898 case lltok::kw_true:
4899 Result.assign(true);
4900 break;
4901 case lltok::kw_false:
4902 Result.assign(false);
4903 break;
4904 }
4905 Lex.Lex();
4906 return false;
4907}
4908
4909template <>
4910bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) {
4911 if (Lex.getKind() == lltok::kw_null) {
4912 if (!Result.AllowNull)
4913 return tokError("'" + Name + "' cannot be null");
4914 Lex.Lex();
4915 Result.assign(nullptr);
4916 return false;
4917 }
4918
4919 Metadata *MD;
4920 if (parseMetadata(MD, nullptr))
4921 return true;
4922
4923 Result.assign(MD);
4924 return false;
4925}
4926
4927template <>
4928bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4929 MDSignedOrMDField &Result) {
4930 // Try to parse a signed int.
4931 if (Lex.getKind() == lltok::APSInt) {
4932 MDSignedField Res = Result.A;
4933 if (!parseMDField(Loc, Name, Res)) {
4934 Result.assign(Res);
4935 return false;
4936 }
4937 return true;
4938 }
4939
4940 // Otherwise, try to parse as an MDField.
4941 MDField Res = Result.B;
4942 if (!parseMDField(Loc, Name, Res)) {
4943 Result.assign(Res);
4944 return false;
4945 }
4946
4947 return true;
4948}
4949
4950template <>
4951bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
4952 LocTy ValueLoc = Lex.getLoc();
4953 std::string S;
4954 if (parseStringConstant(S))
4955 return true;
4956
4957 if (!Result.AllowEmpty && S.empty())
4958 return error(ValueLoc, "'" + Name + "' cannot be empty");
4959
4960 Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
4961 return false;
4962}
4963
4964template <>
4965bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
4967 if (parseMDNodeVector(MDs))
4968 return true;
4969
4970 Result.assign(std::move(MDs));
4971 return false;
4972}
4973
4974template <>
4975bool LLParser::parseMDField(LocTy Loc, StringRef Name,
4976 ChecksumKindField &Result) {
4977 std::optional<DIFile::ChecksumKind> CSKind =
4979
4980 if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
4981 return tokError("invalid checksum kind" + Twine(" '") + Lex.getStrVal() +
4982 "'");
4983
4984 Result.assign(*CSKind);
4985 Lex.Lex();
4986 return false;
4987}
4988
4989} // end namespace llvm
4990
4991template <class ParserTy>
4992bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) {
4993 do {
4994 if (Lex.getKind() != lltok::LabelStr)
4995 return tokError("expected field label here");
4996
4997 if (ParseField())
4998 return true;
4999 } while (EatIfPresent(lltok::comma));
5000
5001 return false;
5002}
5003
5004template <class ParserTy>
5005bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) {
5006 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5007 Lex.Lex();
5008
5009 if (parseToken(lltok::lparen, "expected '(' here"))
5010 return true;
5011 if (Lex.getKind() != lltok::rparen)
5012 if (parseMDFieldsImplBody(ParseField))
5013 return true;
5014
5015 ClosingLoc = Lex.getLoc();
5016 return parseToken(lltok::rparen, "expected ')' here");
5017}
5018
5019template <class FieldTy>
5020bool LLParser::parseMDField(StringRef Name, FieldTy &Result) {
5021 if (Result.Seen)
5022 return tokError("field '" + Name + "' cannot be specified more than once");
5023
5024 LocTy Loc = Lex.getLoc();
5025 Lex.Lex();
5026 return parseMDField(Loc, Name, Result);
5027}
5028
5029bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
5030 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5031
5032#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
5033 if (Lex.getStrVal() == #CLASS) \
5034 return parse##CLASS(N, IsDistinct);
5035#include "llvm/IR/Metadata.def"
5036
5037 return tokError("expected metadata type");
5038}
5039
5040#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
5041#define NOP_FIELD(NAME, TYPE, INIT)
5042#define REQUIRE_FIELD(NAME, TYPE, INIT) \
5043 if (!NAME.Seen) \
5044 return error(ClosingLoc, "missing required field '" #NAME "'");
5045#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
5046 if (Lex.getStrVal() == #NAME) \
5047 return parseMDField(#NAME, NAME);
5048#define PARSE_MD_FIELDS() \
5049 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
5050 do { \
5051 LocTy ClosingLoc; \
5052 if (parseMDFieldsImpl( \
5053 [&]() -> bool { \
5054 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
5055 return tokError(Twine("invalid field '") + Lex.getStrVal() + \
5056 "'"); \
5057 }, \
5058 ClosingLoc)) \
5059 return true; \
5060 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
5061 } while (false)
5062#define GET_OR_DISTINCT(CLASS, ARGS) \
5063 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
5064
5065/// parseDILocationFields:
5066/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
5067/// isImplicitCode: true)
5068bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) {
5069#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5070 OPTIONAL(line, LineField, ); \
5071 OPTIONAL(column, ColumnField, ); \
5072 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5073 OPTIONAL(inlinedAt, MDField, ); \
5074 OPTIONAL(isImplicitCode, MDBoolField, (false));
5076#undef VISIT_MD_FIELDS
5077
5078 Result =
5079 GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val,
5080 inlinedAt.Val, isImplicitCode.Val));
5081 return false;
5082}
5083
5084/// parseDIAssignID:
5085/// ::= distinct !DIAssignID()
5086bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) {
5087 if (!IsDistinct)
5088 return Lex.Error("missing 'distinct', required for !DIAssignID()");
5089
5090 Lex.Lex();
5091
5092 // Now eat the parens.
5093 if (parseToken(lltok::lparen, "expected '(' here"))
5094 return true;
5095 if (parseToken(lltok::rparen, "expected ')' here"))
5096 return true;
5097
5099 return false;
5100}
5101
5102/// parseGenericDINode:
5103/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
5104bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) {
5105#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5106 REQUIRED(tag, DwarfTagField, ); \
5107 OPTIONAL(header, MDStringField, ); \
5108 OPTIONAL(operands, MDFieldList, );
5110#undef VISIT_MD_FIELDS
5111
5113 (Context, tag.Val, header.Val, operands.Val));
5114 return false;
5115}
5116
5117/// parseDISubrange:
5118/// ::= !DISubrange(count: 30, lowerBound: 2)
5119/// ::= !DISubrange(count: !node, lowerBound: 2)
5120/// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3)
5121bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) {
5122#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5123 OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
5124 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5125 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5126 OPTIONAL(stride, MDSignedOrMDField, );
5128#undef VISIT_MD_FIELDS
5129
5130 Metadata *Count = nullptr;
5131 Metadata *LowerBound = nullptr;
5132 Metadata *UpperBound = nullptr;
5133 Metadata *Stride = nullptr;
5134
5135 auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
5136 if (Bound.isMDSignedField())
5138 Type::getInt64Ty(Context), Bound.getMDSignedValue()));
5139 if (Bound.isMDField())
5140 return Bound.getMDFieldValue();
5141 return nullptr;
5142 };
5143
5144 Count = convToMetadata(count);
5145 LowerBound = convToMetadata(lowerBound);
5146 UpperBound = convToMetadata(upperBound);
5147 Stride = convToMetadata(stride);
5148
5150 (Context, Count, LowerBound, UpperBound, Stride));
5151
5152 return false;
5153}
5154
5155/// parseDIGenericSubrange:
5156/// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride:
5157/// !node3)
5158bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) {
5159#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5160 OPTIONAL(count, MDSignedOrMDField, ); \
5161 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5162 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5163 OPTIONAL(stride, MDSignedOrMDField, );
5165#undef VISIT_MD_FIELDS
5166
5167 auto ConvToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
5168 if (Bound.isMDSignedField())
5169 return DIExpression::get(
5170 Context, {dwarf::DW_OP_consts,
5171 static_cast<uint64_t>(Bound.getMDSignedValue())});
5172 if (Bound.isMDField())
5173 return Bound.getMDFieldValue();
5174 return nullptr;
5175 };
5176
5177 Metadata *Count = ConvToMetadata(count);
5178 Metadata *LowerBound = ConvToMetadata(lowerBound);
5179 Metadata *UpperBound = ConvToMetadata(upperBound);
5180 Metadata *Stride = ConvToMetadata(stride);
5181
5183 (Context, Count, LowerBound, UpperBound, Stride));
5184
5185 return false;
5186}
5187
5188/// parseDIEnumerator:
5189/// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
5190bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) {
5191#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5192 REQUIRED(name, MDStringField, ); \
5193 REQUIRED(value, MDAPSIntField, ); \
5194 OPTIONAL(isUnsigned, MDBoolField, (false));
5196#undef VISIT_MD_FIELDS
5197
5198 if (isUnsigned.Val && value.Val.isNegative())
5199 return tokError("unsigned enumerator with negative value");
5200
5201 APSInt Value(value.Val);
5202 // Add a leading zero so that unsigned values with the msb set are not
5203 // mistaken for negative values when used for signed enumerators.
5204 if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet())
5205 Value = Value.zext(Value.getBitWidth() + 1);
5206
5207 Result =
5208 GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
5209
5210 return false;
5211}
5212
5213/// parseDIBasicType:
5214/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
5215/// encoding: DW_ATE_encoding, flags: 0)
5216bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) {
5217#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5218 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
5219 OPTIONAL(name, MDStringField, ); \
5220 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
5221 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5222 OPTIONAL(encoding, DwarfAttEncodingField, ); \
5223 OPTIONAL(flags, DIFlagField, );
5225#undef VISIT_MD_FIELDS
5226
5227 Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
5228 align.Val, encoding.Val, flags.Val));
5229 return false;
5230}
5231
5232/// parseDIStringType:
5233/// ::= !DIStringType(name: "character(4)", size: 32, align: 32)
5234bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) {
5235#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5236 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \
5237 OPTIONAL(name, MDStringField, ); \
5238 OPTIONAL(stringLength, MDField, ); \
5239 OPTIONAL(stringLengthExpression, MDField, ); \
5240 OPTIONAL(stringLocationExpression, MDField, ); \
5241 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
5242 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5243 OPTIONAL(encoding, DwarfAttEncodingField, );
5245#undef VISIT_MD_FIELDS
5246
5249 (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val,
5250 stringLocationExpression.Val, size.Val, align.Val, encoding.Val));
5251 return false;
5252}
5253
5254/// parseDIDerivedType:
5255/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
5256/// line: 7, scope: !1, baseType: !2, size: 32,
5257/// align: 32, offset: 0, flags: 0, extraData: !3,
5258/// dwarfAddressSpace: 3, ptrAuthKey: 1,
5259/// ptrAuthIsAddressDiscriminated: true,
5260/// ptrAuthExtraDiscriminator: 0x1234,
5261/// ptrAuthIsaPointer: 1, ptrAuthAuthenticatesNullValues:1
5262/// )
5263bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) {
5264#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5265 REQUIRED(tag, DwarfTagField, ); \
5266 OPTIONAL(name, MDStringField, ); \
5267 OPTIONAL(file, MDField, ); \
5268 OPTIONAL(line, LineField, ); \
5269 OPTIONAL(scope, MDField, ); \
5270 REQUIRED(baseType, MDField, ); \
5271 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
5272 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5273 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
5274 OPTIONAL(flags, DIFlagField, ); \
5275 OPTIONAL(extraData, MDField, ); \
5276 OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \
5277 OPTIONAL(annotations, MDField, ); \
5278 OPTIONAL(ptrAuthKey, MDUnsignedField, (0, 7)); \
5279 OPTIONAL(ptrAuthIsAddressDiscriminated, MDBoolField, ); \
5280 OPTIONAL(ptrAuthExtraDiscriminator, MDUnsignedField, (0, 0xffff)); \
5281 OPTIONAL(ptrAuthIsaPointer, MDBoolField, ); \
5282 OPTIONAL(ptrAuthAuthenticatesNullValues, MDBoolField, );
5284#undef VISIT_MD_FIELDS
5285
5286 std::optional<unsigned> DWARFAddressSpace;
5287 if (dwarfAddressSpace.Val != UINT32_MAX)
5288 DWARFAddressSpace = dwarfAddressSpace.Val;
5289 std::optional<DIDerivedType::PtrAuthData> PtrAuthData;
5290 if (ptrAuthKey.Val)
5291 PtrAuthData.emplace(
5292 (unsigned)ptrAuthKey.Val, ptrAuthIsAddressDiscriminated.Val,
5293 (unsigned)ptrAuthExtraDiscriminator.Val, ptrAuthIsaPointer.Val,
5294 ptrAuthAuthenticatesNullValues.Val);
5295
5297 (Context, tag.Val, name.Val, file.Val, line.Val,
5298 scope.Val, baseType.Val, size.Val, align.Val,
5299 offset.Val, DWARFAddressSpace, PtrAuthData,
5300 flags.Val, extraData.Val, annotations.Val));
5301 return false;
5302}
5303
5304bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
5305#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5306 REQUIRED(tag, DwarfTagField, ); \
5307 OPTIONAL(name, MDStringField, ); \
5308 OPTIONAL(file, MDField, ); \
5309 OPTIONAL(line, LineField, ); \
5310 OPTIONAL(scope, MDField, ); \
5311 OPTIONAL(baseType, MDField, ); \
5312 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
5313 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5314 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
5315 OPTIONAL(flags, DIFlagField, ); \
5316 OPTIONAL(elements, MDField, ); \
5317 OPTIONAL(runtimeLang, DwarfLangField, ); \
5318 OPTIONAL(vtableHolder, MDField, ); \
5319 OPTIONAL(templateParams, MDField, ); \
5320 OPTIONAL(identifier, MDStringField, ); \
5321 OPTIONAL(discriminator, MDField, ); \
5322 OPTIONAL(dataLocation, MDField, ); \
5323 OPTIONAL(associated, MDField, ); \
5324 OPTIONAL(allocated, MDField, ); \
5325 OPTIONAL(rank, MDSignedOrMDField, ); \
5326 OPTIONAL(annotations, MDField, );
5328#undef VISIT_MD_FIELDS
5329
5330 Metadata *Rank = nullptr;
5331 if (rank.isMDSignedField())
5333 Type::getInt64Ty(Context), rank.getMDSignedValue()));
5334 else if (rank.isMDField())
5335 Rank = rank.getMDFieldValue();
5336
5337 // If this has an identifier try to build an ODR type.
5338 if (identifier.Val)
5339 if (auto *CT = DICompositeType::buildODRType(
5340 Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
5341 scope.Val, baseType.Val, size.Val, align.Val, offset.Val, flags.Val,
5342 elements.Val, runtimeLang.Val, vtableHolder.Val, templateParams.Val,
5343 discriminator.Val, dataLocation.Val, associated.Val, allocated.Val,
5344 Rank, annotations.Val)) {
5345 Result = CT;
5346 return false;
5347 }
5348
5349 // Create a new node, and save it in the context if it belongs in the type
5350 // map.
5353 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
5354 size.Val, align.Val, offset.Val, flags.Val, elements.Val,
5355 runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
5356 discriminator.Val, dataLocation.Val, associated.Val, allocated.Val, Rank,
5357 annotations.Val));
5358 return false;
5359}
5360
5361bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) {
5362#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5363 OPTIONAL(flags, DIFlagField, ); \
5364 OPTIONAL(cc, DwarfCCField, ); \
5365 REQUIRED(types, MDField, );
5367#undef VISIT_MD_FIELDS
5368
5370 (Context, flags.Val, cc.Val, types.Val));
5371 return false;
5372}
5373
5374/// parseDIFileType:
5375/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
5376/// checksumkind: CSK_MD5,
5377/// checksum: "000102030405060708090a0b0c0d0e0f",
5378/// source: "source file contents")
5379bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) {
5380 // The default constructed value for checksumkind is required, but will never
5381 // be used, as the parser checks if the field was actually Seen before using
5382 // the Val.
5383#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5384 REQUIRED(filename, MDStringField, ); \
5385 REQUIRED(directory, MDStringField, ); \
5386 OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
5387 OPTIONAL(checksum, MDStringField, ); \
5388 OPTIONAL(source, MDStringField, );
5390#undef VISIT_MD_FIELDS
5391
5392 std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
5393 if (checksumkind.Seen && checksum.Seen)
5394 OptChecksum.emplace(checksumkind.Val, checksum.Val);
5395 else if (checksumkind.Seen || checksum.Seen)
5396 return Lex.Error("'checksumkind' and 'checksum' must be provided together");
5397
5398 MDString *Source = nullptr;
5399 if (source.Seen)
5400 Source = source.Val;
5402 DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source));
5403 return false;
5404}
5405
5406/// parseDICompileUnit:
5407/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
5408/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
5409/// splitDebugFilename: "abc.debug",
5410/// emissionKind: FullDebug, enums: !1, retainedTypes: !2,
5411/// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd,
5412/// sysroot: "/", sdk: "MacOSX.sdk")
5413bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
5414 if (!IsDistinct)
5415 return Lex.Error("missing 'distinct', required for !DICompileUnit");
5416
5417#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5418 REQUIRED(language, DwarfLangField, ); \
5419 REQUIRED(file, MDField, (/* AllowNull */ false)); \
5420 OPTIONAL(producer, MDStringField, ); \
5421 OPTIONAL(isOptimized, MDBoolField, ); \
5422 OPTIONAL(flags, MDStringField, ); \
5423 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
5424 OPTIONAL(splitDebugFilename, MDStringField, ); \
5425 OPTIONAL(emissionKind, EmissionKindField, ); \
5426 OPTIONAL(enums, MDField, ); \
5427 OPTIONAL(retainedTypes, MDField, ); \
5428 OPTIONAL(globals, MDField, ); \
5429 OPTIONAL(imports, MDField, ); \
5430 OPTIONAL(macros, MDField, ); \
5431 OPTIONAL(dwoId, MDUnsignedField, ); \
5432 OPTIONAL(splitDebugInlining, MDBoolField, = true); \
5433 OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \
5434 OPTIONAL(nameTableKind, NameTableKindField, ); \
5435 OPTIONAL(rangesBaseAddress, MDBoolField, = false); \
5436 OPTIONAL(sysroot, MDStringField, ); \
5437 OPTIONAL(sdk, MDStringField, );
5439#undef VISIT_MD_FIELDS
5440
5442 Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
5443 runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
5444 retainedTypes.Val, globals.Val, imports.Val, macros.Val, dwoId.Val,
5445 splitDebugInlining.Val, debugInfoForProfiling.Val, nameTableKind.Val,
5446 rangesBaseAddress.Val, sysroot.Val, sdk.Val);
5447 return false;
5448}
5449
5450/// parseDISubprogram:
5451/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
5452/// file: !1, line: 7, type: !2, isLocal: false,
5453/// isDefinition: true, scopeLine: 8, containingType: !3,
5454/// virtuality: DW_VIRTUALTIY_pure_virtual,
5455/// virtualIndex: 10, thisAdjustment: 4, flags: 11,
5456/// spFlags: 10, isOptimized: false, templateParams: !4,
5457/// declaration: !5, retainedNodes: !6, thrownTypes: !7,
5458/// annotations: !8)
5459bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
5460 auto Loc = Lex.getLoc();
5461#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5462 OPTIONAL(scope, MDField, ); \
5463 OPTIONAL(name, MDStringField, ); \
5464 OPTIONAL(linkageName, MDStringField, ); \
5465 OPTIONAL(file, MDField, ); \
5466 OPTIONAL(line, LineField, ); \
5467 OPTIONAL(type, MDField, ); \
5468 OPTIONAL(isLocal, MDBoolField, ); \
5469 OPTIONAL(isDefinition, MDBoolField, (true)); \
5470 OPTIONAL(scopeLine, LineField, ); \
5471 OPTIONAL(containingType, MDField, ); \
5472 OPTIONAL(virtuality, DwarfVirtualityField, ); \
5473 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
5474 OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \
5475 OPTIONAL(flags, DIFlagField, ); \
5476 OPTIONAL(spFlags, DISPFlagField, ); \
5477 OPTIONAL(isOptimized, MDBoolField, ); \
5478 OPTIONAL(unit, MDField, ); \
5479 OPTIONAL(templateParams, MDField, ); \
5480 OPTIONAL(declaration, MDField, ); \
5481 OPTIONAL(retainedNodes, MDField, ); \
5482 OPTIONAL(thrownTypes, MDField, ); \
5483 OPTIONAL(annotations, MDField, ); \
5484 OPTIONAL(targetFuncName, MDStringField, );
5486#undef VISIT_MD_FIELDS
5487
5488 // An explicit spFlags field takes precedence over individual fields in
5489 // older IR versions.
5490 DISubprogram::DISPFlags SPFlags =
5491 spFlags.Seen ? spFlags.Val
5492 : DISubprogram::toSPFlags(isLocal.Val, isDefinition.Val,
5493 isOptimized.Val, virtuality.Val);
5494 if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct)
5495 return Lex.Error(
5496 Loc,
5497 "missing 'distinct', required for !DISubprogram that is a Definition");
5500 (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
5501 type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
5502 thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
5503 declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val,
5504 targetFuncName.Val));
5505 return false;
5506}
5507
5508/// parseDILexicalBlock:
5509/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
5510bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
5511#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5512 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5513 OPTIONAL(file, MDField, ); \
5514 OPTIONAL(line, LineField, ); \
5515 OPTIONAL(column, ColumnField, );
5517#undef VISIT_MD_FIELDS
5518
5520 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
5521 return false;
5522}
5523
5524/// parseDILexicalBlockFile:
5525/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
5526bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
5527#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5528 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5529 OPTIONAL(file, MDField, ); \
5530 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
5532#undef VISIT_MD_FIELDS
5533
5535 (Context, scope.Val, file.Val, discriminator.Val));
5536 return false;
5537}
5538
5539/// parseDICommonBlock:
5540/// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9)
5541bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) {
5542#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5543 REQUIRED(scope, MDField, ); \
5544 OPTIONAL(declaration, MDField, ); \
5545 OPTIONAL(name, MDStringField, ); \
5546 OPTIONAL(file, MDField, ); \
5547 OPTIONAL(line, LineField, );
5549#undef VISIT_MD_FIELDS
5550
5552 (Context, scope.Val, declaration.Val, name.Val,
5553 file.Val, line.Val));
5554 return false;
5555}
5556
5557/// parseDINamespace:
5558/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
5559bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) {
5560#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5561 REQUIRED(scope, MDField, ); \
5562 OPTIONAL(name, MDStringField, ); \
5563 OPTIONAL(exportSymbols, MDBoolField, );
5565#undef VISIT_MD_FIELDS
5566
5568 (Context, scope.Val, name.Val, exportSymbols.Val));
5569 return false;
5570}
5571
5572/// parseDIMacro:
5573/// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value:
5574/// "SomeValue")
5575bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) {
5576#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5577 REQUIRED(type, DwarfMacinfoTypeField, ); \
5578 OPTIONAL(line, LineField, ); \
5579 REQUIRED(name, MDStringField, ); \
5580 OPTIONAL(value, MDStringField, );
5582#undef VISIT_MD_FIELDS
5583
5585 (Context, type.Val, line.Val, name.Val, value.Val));
5586 return false;
5587}
5588
5589/// parseDIMacroFile:
5590/// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
5591bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) {
5592#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5593 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
5594 OPTIONAL(line, LineField, ); \
5595 REQUIRED(file, MDField, ); \
5596 OPTIONAL(nodes, MDField, );
5598#undef VISIT_MD_FIELDS
5599
5601 (Context, type.Val, line.Val, file.Val, nodes.Val));
5602 return false;
5603}
5604
5605/// parseDIModule:
5606/// ::= !DIModule(scope: !0, name: "SomeModule", configMacros:
5607/// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes",
5608/// file: !1, line: 4, isDecl: false)
5609bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) {
5610#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5611 REQUIRED(scope, MDField, ); \
5612 REQUIRED(name, MDStringField, ); \
5613 OPTIONAL(configMacros, MDStringField, ); \
5614 OPTIONAL(includePath, MDStringField, ); \
5615 OPTIONAL(apinotes, MDStringField, ); \
5616 OPTIONAL(file, MDField, ); \
5617 OPTIONAL(line, LineField, ); \
5618 OPTIONAL(isDecl, MDBoolField, );
5620#undef VISIT_MD_FIELDS
5621
5622 Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val,
5623 configMacros.Val, includePath.Val,
5624 apinotes.Val, line.Val, isDecl.Val));
5625 return false;
5626}
5627
5628/// parseDITemplateTypeParameter:
5629/// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false)
5630bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
5631#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5632 OPTIONAL(name, MDStringField, ); \
5633 REQUIRED(type, MDField, ); \
5634 OPTIONAL(defaulted, MDBoolField, );
5636#undef VISIT_MD_FIELDS
5637
5639 (Context, name.Val, type.Val, defaulted.Val));
5640 return false;
5641}
5642
5643/// parseDITemplateValueParameter:
5644/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
5645/// name: "V", type: !1, defaulted: false,
5646/// value: i32 7)
5647bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
5648#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5649 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
5650 OPTIONAL(name, MDStringField, ); \
5651 OPTIONAL(type, MDField, ); \
5652 OPTIONAL(defaulted, MDBoolField, ); \
5653 REQUIRED(value, MDField, );
5654
5656#undef VISIT_MD_FIELDS
5657
5660 (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val));
5661 return false;
5662}
5663
5664/// parseDIGlobalVariable:
5665/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
5666/// file: !1, line: 7, type: !2, isLocal: false,
5667/// isDefinition: true, templateParams: !3,
5668/// declaration: !4, align: 8)
5669bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
5670#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5671 OPTIONAL(name, MDStringField, (/* AllowEmpty */ false)); \
5672 OPTIONAL(scope, MDField, ); \
5673 OPTIONAL(linkageName, MDStringField, ); \
5674 OPTIONAL(file, MDField, ); \
5675 OPTIONAL(line, LineField, ); \
5676 OPTIONAL(type, MDField, ); \
5677 OPTIONAL(isLocal, MDBoolField, ); \
5678 OPTIONAL(isDefinition, MDBoolField, (true)); \
5679 OPTIONAL(templateParams, MDField, ); \
5680 OPTIONAL(declaration, MDField, ); \
5681 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5682 OPTIONAL(annotations, MDField, );
5684#undef VISIT_MD_FIELDS
5685
5686 Result =
5688 (Context, scope.Val, name.Val, linkageName.Val, file.Val,
5689 line.Val, type.Val, isLocal.Val, isDefinition.Val,
5690 declaration.Val, templateParams.Val, align.Val,
5691 annotations.Val));
5692 return false;
5693}
5694
5695/// parseDILocalVariable:
5696/// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
5697/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
5698/// align: 8)
5699/// ::= !DILocalVariable(scope: !0, name: "foo",
5700/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
5701/// align: 8)
5702bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) {
5703#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5704 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5705 OPTIONAL(name, MDStringField, ); \
5706 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
5707 OPTIONAL(file, MDField, ); \
5708 OPTIONAL(line, LineField, ); \
5709 OPTIONAL(type, MDField, ); \
5710 OPTIONAL(flags, DIFlagField, ); \
5711 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5712 OPTIONAL(annotations, MDField, );
5714#undef VISIT_MD_FIELDS
5715
5717 (Context, scope.Val, name.Val, file.Val, line.Val,
5718 type.Val, arg.Val, flags.Val, align.Val,
5719 annotations.Val));
5720 return false;
5721}
5722
5723/// parseDILabel:
5724/// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7)
5725bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) {
5726#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5727 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5728 REQUIRED(name, MDStringField, ); \
5729 REQUIRED(file, MDField, ); \
5730 REQUIRED(line, LineField, );
5732#undef VISIT_MD_FIELDS
5733
5735 (Context, scope.Val, name.Val, file.Val, line.Val));
5736 return false;
5737}
5738
5739/// parseDIExpression:
5740/// ::= !DIExpression(0, 7, -1)
5741bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
5742 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5743 Lex.Lex();
5744
5745 if (parseToken(lltok::lparen, "expected '(' here"))
5746 return true;
5747
5749 if (Lex.getKind() != lltok::rparen)
5750 do {
5751 if (Lex.getKind() == lltok::DwarfOp) {
5752 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
5753 Lex.Lex();
5754 Elements.push_back(Op);
5755 continue;
5756 }
5757 return tokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
5758 }
5759
5760 if (Lex.getKind() == lltok::DwarfAttEncoding) {
5761 if (unsigned Op = dwarf::getAttributeEncoding(Lex.getStrVal())) {
5762 Lex.Lex();
5763 Elements.push_back(Op);
5764 continue;
5765 }
5766 return tokError(Twine("invalid DWARF attribute encoding '") +
5767 Lex.getStrVal() + "'");
5768 }
5769
5770 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
5771 return tokError("expected unsigned integer");
5772
5773 auto &U = Lex.getAPSIntVal();
5774 if (U.ugt(UINT64_MAX))
5775 return tokError("element too large, limit is " + Twine(UINT64_MAX));
5776 Elements.push_back(U.getZExtValue());
5777 Lex.Lex();
5778 } while (EatIfPresent(lltok::comma));
5779
5780 if (parseToken(lltok::rparen, "expected ')' here"))
5781 return true;
5782
5783 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
5784 return false;
5785}
5786
5787/// ParseDIArgList:
5788/// ::= !DIArgList(i32 7, i64 %0)
5789bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) {
5790 assert(PFS && "Expected valid function state");
5791 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5792 Lex.Lex();
5793
5794 if (parseToken(lltok::lparen, "expected '(' here"))
5795 return true;
5796
5798 if (Lex.getKind() != lltok::rparen)
5799 do {
5800 Metadata *MD;
5801 if (parseValueAsMetadata(MD, "expected value-as-metadata operand", PFS))
5802 return true;
5803 Args.push_back(dyn_cast<ValueAsMetadata>(MD));
5804 } while (EatIfPresent(lltok::comma));
5805
5806 if (parseToken(lltok::rparen, "expected ')' here"))
5807 return true;
5808
5809 MD = DIArgList::get(Context, Args);
5810 return false;
5811}
5812
5813/// parseDIGlobalVariableExpression:
5814/// ::= !DIGlobalVariableExpression(var: !0, expr: !1)
5815bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result,
5816 bool IsDistinct) {
5817#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5818 REQUIRED(var, MDField, ); \
5819 REQUIRED(expr, MDField, );
5821#undef VISIT_MD_FIELDS
5822
5823 Result =
5824 GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
5825 return false;
5826}
5827
5828/// parseDIObjCProperty:
5829/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
5830/// getter: "getFoo", attributes: 7, type: !2)
5831bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
5832#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5833 OPTIONAL(name, MDStringField, ); \
5834 OPTIONAL(file, MDField, ); \
5835 OPTIONAL(line, LineField, ); \
5836 OPTIONAL(setter, MDStringField, ); \
5837 OPTIONAL(getter, MDStringField, ); \
5838 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
5839 OPTIONAL(type, MDField, );
5841#undef VISIT_MD_FIELDS
5842
5844 (Context, name.Val, file.Val, line.Val, setter.Val,
5845 getter.Val, attributes.Val, type.Val));
5846 return false;
5847}
5848
5849/// parseDIImportedEntity:
5850/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
5851/// line: 7, name: "foo", elements: !2)
5852bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
5853#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5854 REQUIRED(tag, DwarfTagField, ); \
5855 REQUIRED(scope, MDField, ); \
5856 OPTIONAL(entity, MDField, ); \
5857 OPTIONAL(file, MDField, ); \
5858 OPTIONAL(line, LineField, ); \
5859 OPTIONAL(name, MDStringField, ); \
5860 OPTIONAL(elements, MDField, );
5862#undef VISIT_MD_FIELDS
5863
5865 (Context, tag.Val, scope.Val, entity.Val, file.Val,
5866 line.Val, name.Val, elements.Val));
5867 return false;
5868}
5869
5870#undef PARSE_MD_FIELD
5871#undef NOP_FIELD
5872#undef REQUIRE_FIELD
5873#undef DECLARE_FIELD
5874
5875/// parseMetadataAsValue
5876/// ::= metadata i32 %local
5877/// ::= metadata i32 @global
5878/// ::= metadata i32 7
5879/// ::= metadata !0
5880/// ::= metadata !{...}
5881/// ::= metadata !"string"
5882bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
5883 // Note: the type 'metadata' has already been parsed.
5884 Metadata *MD;
5885 if (parseMetadata(MD, &PFS))
5886 return true;
5887
5888 V = MetadataAsValue::get(Context, MD);
5889 return false;
5890}
5891
5892/// parseValueAsMetadata
5893/// ::= i32 %local
5894/// ::= i32 @global
5895/// ::= i32 7
5896bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
5897 PerFunctionState *PFS) {
5898 Type *Ty;
5899 LocTy Loc;
5900 if (parseType(Ty, TypeMsg, Loc))
5901 return true;
5902 if (Ty->isMetadataTy())
5903 return error(Loc, "invalid metadata-value-metadata roundtrip");
5904
5905 Value *V;
5906 if (parseValue(Ty, V, PFS))
5907 return true;
5908
5909 MD = ValueAsMetadata::get(V);
5910 return false;
5911}
5912
5913/// parseMetadata
5914/// ::= i32 %local
5915/// ::= i32 @global
5916/// ::= i32 7
5917/// ::= !42
5918/// ::= !{...}
5919/// ::= !"string"
5920/// ::= !DILocation(...)
5921bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) {
5922 if (Lex.getKind() == lltok::MetadataVar) {
5923 // DIArgLists are a special case, as they are a list of ValueAsMetadata and
5924 // so parsing this requires a Function State.
5925 if (Lex.getStrVal() == "DIArgList") {
5926 Metadata *AL;
5927 if (parseDIArgList(AL, PFS))
5928 return true;
5929 MD = AL;
5930 return false;
5931 }
5932 MDNode *N;
5933 if (parseSpecializedMDNode(N)) {
5934 return true;
5935 }
5936 MD = N;
5937 return false;
5938 }
5939
5940 // ValueAsMetadata:
5941 // <type> <value>
5942 if (Lex.getKind() != lltok::exclaim)
5943 return parseValueAsMetadata(MD, "expected metadata operand", PFS);
5944
5945 // '!'.
5946 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
5947 Lex.Lex();
5948
5949 // MDString:
5950 // ::= '!' STRINGCONSTANT
5951 if (Lex.getKind() == lltok::StringConstant) {
5952 MDString *S;
5953 if (parseMDString(S))
5954 return true;
5955 MD = S;
5956 return false;
5957 }
5958
5959 // MDNode:
5960 // !{ ... }
5961 // !7
5962 MDNode *N;
5963 if (parseMDNodeTail(N))
5964 return true;
5965 MD = N;
5966 return false;
5967}
5968
5969//===----------------------------------------------------------------------===//
5970// Function Parsing.
5971//===----------------------------------------------------------------------===//
5972
5973bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
5974 PerFunctionState *PFS) {
5975 if (Ty->isFunctionTy())
5976 return error(ID.Loc, "functions are not values, refer to them as pointers");
5977
5978 switch (ID.Kind) {
5979 case ValID::t_LocalID:
5980 if (!PFS)
5981 return error(ID.Loc, "invalid use of function-local name");
5982 V = PFS->getVal(ID.UIntVal, Ty, ID.Loc);
5983 return V == nullptr;
5984 case ValID::t_LocalName:
5985 if (!PFS)
5986 return error(ID.Loc, "invalid use of function-local name");
5987 V = PFS->getVal(ID.StrVal, Ty, ID.Loc);
5988 return V == nullptr;
5989 case ValID::t_InlineAsm: {
5990 if (!ID.FTy)
5991 return error(ID.Loc, "invalid type for inline asm constraint string");
5992 if (Error Err = InlineAsm::verify(ID.FTy, ID.StrVal2))
5993 return error(ID.Loc, toString(std::move(Err)));
5994 V = InlineAsm::get(
5995 ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1, (ID.UIntVal >> 1) & 1,
5996 InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), (ID.UIntVal >> 3) & 1);
5997 return false;
5998 }
6000 V = getGlobalVal(ID.StrVal, Ty, ID.Loc);
6001 if (V && ID.NoCFI)
6002 V = NoCFIValue::get(cast<GlobalValue>(V));
6003 return V == nullptr;
6004 case ValID::t_GlobalID:
6005 V = getGlobalVal(ID.UIntVal, Ty, ID.Loc);
6006 if (V && ID.NoCFI)
6007 V = NoCFIValue::get(cast<GlobalValue>(V));
6008 return V == nullptr;
6009 case ValID::t_APSInt:
6010 if (!Ty->isIntegerTy())
6011 return error(ID.Loc, "integer constant must have integer type");
6012 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
6013 V = ConstantInt::get(Context, ID.APSIntVal);
6014 return false;
6015 case ValID::t_APFloat:
6016 if (!Ty->isFloatingPointTy() ||
6017 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
6018 return error(ID.Loc, "floating point constant invalid for type");
6019
6020 // The lexer has no type info, so builds all half, bfloat, float, and double
6021 // FP constants as double. Fix this here. Long double does not need this.
6022 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
6023 // Check for signaling before potentially converting and losing that info.
6024 bool IsSNAN = ID.APFloatVal.isSignaling();
6025 bool Ignored;
6026 if (Ty->isHalfTy())
6028 &Ignored);
6029 else if (Ty->isBFloatTy())
6030 ID.APFloatVal.convert(APFloat::BFloat(), APFloat::rmNearestTiesToEven,
6031 &Ignored);
6032 else if (Ty->isFloatTy())
6034 &Ignored);
6035 if (IsSNAN) {
6036 // The convert call above may quiet an SNaN, so manufacture another
6037 // SNaN. The bitcast works because the payload (significand) parameter
6038 // is truncated to fit.
6039 APInt Payload = ID.APFloatVal.bitcastToAPInt();
6040 ID.APFloatVal = APFloat::getSNaN(ID.APFloatVal.getSemantics(),
6041 ID.APFloatVal.isNegative(), &Payload);
6042 }
6043 }
6044 V = ConstantFP::get(Context, ID.APFloatVal);
6045
6046 if (V->getType() != Ty)
6047 return error(ID.Loc, "floating point constant does not have type '" +
6048 getTypeString(Ty) + "'");
6049
6050 return false;
6051 case ValID::t_Null:
6052 if (!Ty->isPointerTy())
6053 return error(ID.Loc, "null must be a pointer type");
6054 V = ConstantPointerNull::get(cast<PointerType>(Ty));
6055 return false;
6056 case ValID::t_Undef:
6057 // FIXME: LabelTy should not be a first-class type.
6058 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6059 return error(ID.Loc, "invalid type for undef constant");
6060 V = UndefValue::get(Ty);
6061 return false;
6063 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
6064 return error(ID.Loc, "invalid empty array initializer");
6065 V = UndefValue::get(Ty);
6066 return false;
6067 case ValID::t_Zero:
6068 // FIXME: LabelTy should not be a first-class type.
6069 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6070 return error(ID.Loc, "invalid type for null constant");
6071 if (auto *TETy = dyn_cast<TargetExtType>(Ty))
6072 if (!TETy->hasProperty(TargetExtType::HasZeroInit))
6073 return error(ID.Loc, "invalid type for null constant");
6075 return false;
6076 case ValID::t_None:
6077 if (!Ty->isTokenTy())
6078 return error(ID.Loc, "invalid type for none constant");
6080 return false;
6081 case ValID::t_Poison:
6082 // FIXME: LabelTy should not be a first-class type.
6083 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6084 return error(ID.Loc, "invalid type for poison constant");
6085 V = PoisonValue::get(Ty);
6086 return false;
6087 case ValID::t_Constant:
6088 if (ID.ConstantVal->getType() != Ty)
6089 return error(ID.Loc, "constant expression type mismatch: got type '" +
6090 getTypeString(ID.ConstantVal->getType()) +
6091 "' but expected '" + getTypeString(Ty) + "'");
6092 V = ID.ConstantVal;
6093 return false;
6095 if (!Ty->isVectorTy())
6096 return error(ID.Loc, "vector constant must have vector type");
6097 if (ID.ConstantVal->getType() != Ty->getScalarType())
6098 return error(ID.Loc, "constant expression type mismatch: got type '" +
6099 getTypeString(ID.ConstantVal->getType()) +
6100 "' but expected '" +
6101 getTypeString(Ty->getScalarType()) + "'");
6102 V = ConstantVector::getSplat(cast<VectorType>(Ty)->getElementCount(),
6103 ID.ConstantVal);
6104 return false;
6107 if (StructType *ST = dyn_cast<StructType>(Ty)) {
6108 if (ST->getNumElements() != ID.UIntVal)
6109 return error(ID.Loc,
6110 "initializer with struct type has wrong # elements");
6111 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
6112 return error(ID.Loc, "packed'ness of initializer and type don't match");
6113
6114 // Verify that the elements are compatible with the structtype.
6115 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
6116 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
6117 return error(
6118 ID.Loc,
6119 "element " + Twine(i) +
6120 " of struct initializer doesn't match struct element type");
6121
6123 ST, ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
6124 } else
6125 return error(ID.Loc, "constant expression type mismatch");
6126 return false;
6127 }
6128 llvm_unreachable("Invalid ValID");
6129}
6130
6131bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
6132 C = nullptr;
6133 ValID ID;
6134 auto Loc = Lex.getLoc();
6135 if (parseValID(ID, /*PFS=*/nullptr))
6136 return true;
6137 switch (ID.Kind) {
6138 case ValID::t_APSInt:
6139 case ValID::t_APFloat:
6140 case ValID::t_Undef:
6141 case ValID::t_Constant:
6145 Value *V;
6146 if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr))
6147 return true;
6148 assert(isa<Constant>(V) && "Expected a constant value");
6149 C = cast<Constant>(V);
6150 return false;
6151 }
6152 case ValID::t_Null:
6154 return false;
6155 default:
6156 return error(Loc, "expected a constant value");
6157 }
6158}
6159
6160bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
6161 V = nullptr;
6162 ValID ID;
6163 return parseValID(ID, PFS, Ty) ||
6164 convertValIDToValue(Ty, ID, V, PFS);
6165}
6166
6167bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) {
6168 Type *Ty = nullptr;
6169 return parseType(Ty) || parseValue(Ty, V, PFS);
6170}
6171
6172bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
6173 PerFunctionState &PFS) {
6174 Value *V;
6175 Loc = Lex.getLoc();
6176 if (parseTypeAndValue(V, PFS))
6177 return true;
6178 if (!isa<BasicBlock>(V))
6179 return error(Loc, "expected a basic block");
6180 BB = cast<BasicBlock>(V);
6181 return false;
6182}
6183
6185 // Exit early for the common (non-debug-intrinsic) case.
6186 // We can make this the only check when we begin supporting all "llvm.dbg"
6187 // intrinsics in the new debug info format.
6188 if (!Name.starts_with("llvm.dbg."))
6189 return false;
6191 return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value ||
6192 FnID == Intrinsic::dbg_assign;
6193}
6194
6195/// FunctionHeader
6196/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
6197/// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
6198/// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
6199/// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
6200bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
6201 unsigned &FunctionNumber,
6202 SmallVectorImpl<unsigned> &UnnamedArgNums) {
6203 // parse the linkage.
6204 LocTy LinkageLoc = Lex.getLoc();
6205 unsigned Linkage;
6206 unsigned Visibility;
6207 unsigned DLLStorageClass;
6208 bool DSOLocal;
6209 AttrBuilder RetAttrs(M->getContext());
6210 unsigned CC;
6211 bool HasLinkage;
6212 Type *RetType = nullptr;
6213 LocTy RetTypeLoc = Lex.getLoc();
6214 if (parseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
6215 DSOLocal) ||
6216 parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
6217 parseType(RetType, RetTypeLoc, true /*void allowed*/))
6218 return true;
6219
6220 // Verify that the linkage is ok.
6221 switch ((GlobalValue::LinkageTypes)Linkage) {
6223 break; // always ok.
6225 if (IsDefine)
6226 return error(LinkageLoc, "invalid linkage for function definition");
6227 break;
6235 if (!IsDefine)
6236 return error(LinkageLoc, "invalid linkage for function declaration");
6237 break;
6240 return error(LinkageLoc, "invalid function linkage type");
6241 }
6242
6243 if (!isValidVisibilityForLinkage(Visibility, Linkage))
6244 return error(LinkageLoc,
6245 "symbol with local linkage must have default visibility");
6246
6247 if (!isValidDLLStorageClassForLinkage(DLLStorageClass, Linkage))
6248 return error(LinkageLoc,
6249 "symbol with local linkage cannot have a DLL storage class");
6250
6251 if (!FunctionType::isValidReturnType(RetType))
6252 return error(RetTypeLoc, "invalid function return type");
6253
6254 LocTy NameLoc = Lex.getLoc();
6255
6256 std::string FunctionName;
6257 if (Lex.getKind() == lltok::GlobalVar) {
6258 FunctionName = Lex.getStrVal();
6259 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
6260 FunctionNumber = Lex.getUIntVal();
6261 if (checkValueID(NameLoc, "function", "@", NumberedVals.getNext(),
6262 FunctionNumber))
6263 return true;
6264 } else {
6265 return tokError("expected function name");
6266 }
6267
6268 Lex.Lex();
6269
6270 if (Lex.getKind() != lltok::lparen)
6271 return tokError("expected '(' in function argument list");
6272
6274 bool IsVarArg;
6275 AttrBuilder FuncAttrs(M->getContext());
6276 std::vector<unsigned> FwdRefAttrGrps;
6277 LocTy BuiltinLoc;
6278 std::string Section;
6279 std::string Partition;
6280 MaybeAlign Alignment;
6281 std::string GC;
6283 unsigned AddrSpace = 0;
6284 Constant *Prefix = nullptr;
6285 Constant *Prologue = nullptr;
6286 Constant *PersonalityFn = nullptr;
6287 Comdat *C;
6288
6289 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg) ||
6290 parseOptionalUnnamedAddr(UnnamedAddr) ||
6291 parseOptionalProgramAddrSpace(AddrSpace) ||
6292 parseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
6293 BuiltinLoc) ||
6294 (EatIfPresent(lltok::kw_section) && parseStringConstant(Section)) ||
6295 (EatIfPresent(lltok::kw_partition) && parseStringConstant(Partition)) ||
6296 parseOptionalComdat(FunctionName, C) ||
6297 parseOptionalAlignment(Alignment) ||
6298 (EatIfPresent(lltok::kw_gc) && parseStringConstant(GC)) ||
6299 (EatIfPresent(lltok::kw_prefix) && parseGlobalTypeAndValue(Prefix)) ||
6300 (EatIfPresent(lltok::kw_prologue) && parseGlobalTypeAndValue(Prologue)) ||
6301 (EatIfPresent(lltok::kw_personality) &&
6302 parseGlobalTypeAndValue(PersonalityFn)))
6303 return true;
6304
6305 if (FuncAttrs.contains(Attribute::Builtin))
6306 return error(BuiltinLoc, "'builtin' attribute not valid on function");
6307
6308 // If the alignment was parsed as an attribute, move to the alignment field.
6309 if (MaybeAlign A = FuncAttrs.getAlignment()) {
6310 Alignment = A;
6311 FuncAttrs.removeAttribute(Attribute::Alignment);
6312 }
6313
6314 // Okay, if we got here, the function is syntactically valid. Convert types
6315 // and do semantic checks.
6316 std::vector<Type*> ParamTypeList;
6318
6319 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
6320 ParamTypeList.push_back(ArgList[i].Ty);
6321 Attrs.push_back(ArgList[i].Attrs);
6322 }
6323
6324 AttributeList PAL =
6325 AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
6326 AttributeSet::get(Context, RetAttrs), Attrs);
6327
6328 if (PAL.hasParamAttr(0, Attribute::StructRet) && !RetType->isVoidTy())
6329 return error(RetTypeLoc, "functions with 'sret' argument must return void");
6330
6331 FunctionType *FT = FunctionType::get(RetType, ParamTypeList, IsVarArg);
6332 PointerType *PFT = PointerType::get(FT, AddrSpace);
6333
6334 Fn = nullptr;
6335 GlobalValue *FwdFn = nullptr;
6336 if (!FunctionName.empty()) {
6337 // If this was a definition of a forward reference, remove the definition
6338 // from the forward reference table and fill in the forward ref.
6339 auto FRVI = ForwardRefVals.find(FunctionName);
6340 if (FRVI != ForwardRefVals.end()) {
6341 FwdFn = FRVI->second.first;
6342 if (FwdFn->getType() != PFT)
6343 return error(FRVI->second.second,
6344 "invalid forward reference to "
6345 "function '" +
6346 FunctionName +
6347 "' with wrong type: "
6348 "expected '" +
6349 getTypeString(PFT) + "' but was '" +
6350 getTypeString(FwdFn->getType()) + "'");
6351 ForwardRefVals.erase(FRVI);
6352 } else if ((Fn = M->getFunction(FunctionName))) {
6353 // Reject redefinitions.
6354 return error(NameLoc,
6355 "invalid redefinition of function '" + FunctionName + "'");
6356 } else if (M->getNamedValue(FunctionName)) {
6357 return error(NameLoc, "redefinition of function '@" + FunctionName + "'");
6358 }
6359
6360 } else {
6361 // Handle @"", where a name is syntactically specified, but semantically
6362 // missing.
6363 if (FunctionNumber == (unsigned)-1)
6364 FunctionNumber = NumberedVals.getNext();
6365
6366 // If this is a definition of a forward referenced function, make sure the
6367 // types agree.
6368 auto I = ForwardRefValIDs.find(FunctionNumber);
6369 if (I != ForwardRefValIDs.end()) {
6370 FwdFn = I->second.first;
6371 if (FwdFn->getType() != PFT)
6372 return error(NameLoc, "type of definition and forward reference of '@" +
6373 Twine(FunctionNumber) +
6374 "' disagree: "
6375 "expected '" +
6376 getTypeString(PFT) + "' but was '" +
6377 getTypeString(FwdFn->getType()) + "'");
6378 ForwardRefValIDs.erase(I);
6379 }
6380 }
6381
6383 FunctionName, M);
6384
6385 assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
6386
6387 if (FunctionName.empty())
6388 NumberedVals.add(FunctionNumber, Fn);
6389
6391 maybeSetDSOLocal(DSOLocal, *Fn);
6394 Fn->setCallingConv(CC);
6395 Fn->setAttributes(PAL);
6396 Fn->setUnnamedAddr(UnnamedAddr);
6397 if (Alignment)
6398 Fn->setAlignment(*Alignment);
6399 Fn->setSection(Section);
6400 Fn->setPartition(Partition);
6401 Fn->setComdat(C);
6402 Fn->setPersonalityFn(PersonalityFn);
6403 if (!GC.empty()) Fn->setGC(GC);
6404 Fn->setPrefixData(Prefix);
6405 Fn->setPrologueData(Prologue);
6406 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
6407
6408 // Add all of the arguments we parsed to the function.
6409 Function::arg_iterator ArgIt = Fn->arg_begin();
6410 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
6411 // If the argument has a name, insert it into the argument symbol table.
6412 if (ArgList[i].Name.empty()) continue;
6413
6414 // Set the name, if it conflicted, it will be auto-renamed.
6415 ArgIt->setName(ArgList[i].Name);
6416
6417 if (ArgIt->getName() != ArgList[i].Name)
6418 return error(ArgList[i].Loc,
6419 "redefinition of argument '%" + ArgList[i].Name + "'");
6420 }
6421
6422 if (FwdFn) {
6423 FwdFn->replaceAllUsesWith(Fn);
6424 FwdFn->eraseFromParent();
6425 }
6426
6427 if (IsDefine)
6428 return false;
6429
6430 // Check the declaration has no block address forward references.
6431 ValID ID;
6432 if (FunctionName.empty()) {
6433 ID.Kind = ValID::t_GlobalID;
6434 ID.UIntVal = FunctionNumber;
6435 } else {
6436 ID.Kind = ValID::t_GlobalName;
6437 ID.StrVal = FunctionName;
6438 }
6439 auto Blocks = ForwardRefBlockAddresses.find(ID);
6440 if (Blocks != ForwardRefBlockAddresses.end())
6441 return error(Blocks->first.Loc,
6442 "cannot take blockaddress inside a declaration");
6443 return false;
6444}
6445
6446bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
6447 ValID ID;
6448 if (FunctionNumber == -1) {
6449 ID.Kind = ValID::t_GlobalName;
6450 ID.StrVal = std::string(F.getName());
6451 } else {
6452 ID.Kind = ValID::t_GlobalID;
6453 ID.UIntVal = FunctionNumber;
6454 }
6455
6456 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
6457 if (Blocks == P.ForwardRefBlockAddresses.end())
6458 return false;
6459
6460 for (const auto &I : Blocks->second) {
6461 const ValID &BBID = I.first;
6462 GlobalValue *GV = I.second;
6463
6464 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
6465 "Expected local id or name");
6466 BasicBlock *BB;
6467 if (BBID.Kind == ValID::t_LocalName)
6468 BB = getBB(BBID.StrVal, BBID.Loc);
6469 else
6470 BB = getBB(BBID.UIntVal, BBID.Loc);
6471 if (!BB)
6472 return P.error(BBID.Loc, "referenced value is not a basic block");
6473
6474 Value *ResolvedVal = BlockAddress::get(&F, BB);
6475 ResolvedVal = P.checkValidVariableType(BBID.Loc, BBID.StrVal, GV->getType(),
6476 ResolvedVal);
6477 if (!ResolvedVal)
6478 return true;
6479 GV->replaceAllUsesWith(ResolvedVal);
6480 GV->eraseFromParent();
6481 }
6482
6483 P.ForwardRefBlockAddresses.erase(Blocks);
6484 return false;
6485}
6486
6487/// parseFunctionBody
6488/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
6489bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber,
6490 ArrayRef<unsigned> UnnamedArgNums) {
6491 if (Lex.getKind() != lltok::lbrace)
6492 return tokError("expected '{' in function body");
6493 Lex.Lex(); // eat the {.
6494
6495 PerFunctionState PFS(*this, Fn, FunctionNumber, UnnamedArgNums);
6496
6497 // Resolve block addresses and allow basic blocks to be forward-declared
6498 // within this function.
6499 if (PFS.resolveForwardRefBlockAddresses())
6500 return true;
6501 SaveAndRestore ScopeExit(BlockAddressPFS, &PFS);
6502
6503 // We need at least one basic block.
6504 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
6505 return tokError("function body requires at least one basic block");
6506
6507 while (Lex.getKind() != lltok::rbrace &&
6509 if (parseBasicBlock(PFS))
6510 return true;
6511
6512 while (Lex.getKind() != lltok::rbrace)
6513 if (parseUseListOrder(&PFS))
6514 return true;
6515
6516 // Eat the }.
6517 Lex.Lex();
6518
6519 // Verify function is ok.
6520 return PFS.finishFunction();
6521}
6522
6523/// parseBasicBlock
6524/// ::= (LabelStr|LabelID)? Instruction*
6525bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
6526 // If this basic block starts out with a name, remember it.
6527 std::string Name;
6528 int NameID = -1;
6529 LocTy NameLoc = Lex.getLoc();
6530 if (Lex.getKind() == lltok::LabelStr) {
6531 Name = Lex.getStrVal();
6532 Lex.Lex();
6533 } else if (Lex.getKind() == lltok::LabelID) {
6534 NameID = Lex.getUIntVal();
6535 Lex.Lex();
6536 }
6537
6538 BasicBlock *BB = PFS.defineBB(Name, NameID, NameLoc);
6539 if (!BB)
6540 return true;
6541
6542 std::string NameStr;
6543
6544 // Parse the instructions and debug values in this block until we get a
6545 // terminator.
6546 Instruction *Inst;
6547 auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord(); };
6548 using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>;
6549 SmallVector<DbgRecordPtr> TrailingDbgRecord;
6550 do {
6551 // Handle debug records first - there should always be an instruction
6552 // following the debug records, i.e. they cannot appear after the block
6553 // terminator.
6554 while (Lex.getKind() == lltok::hash) {
6555 if (SeenOldDbgInfoFormat)
6556 return error(Lex.getLoc(), "debug record should not appear in a module "
6557 "containing debug info intrinsics");
6558 if (!SeenNewDbgInfoFormat)
6559 M->setNewDbgInfoFormatFlag(true);
6560 SeenNewDbgInfoFormat = true;
6561 Lex.Lex();
6562
6563 DbgRecord *DR;
6564 if (parseDebugRecord(DR, PFS))
6565 return true;
6566 TrailingDbgRecord.emplace_back(DR, DeleteDbgRecord);
6567 }
6568
6569 // This instruction may have three possibilities for a name: a) none
6570 // specified, b) name specified "%foo =", c) number specified: "%4 =".
6571 LocTy NameLoc = Lex.getLoc();
6572 int NameID = -1;
6573 NameStr = "";
6574
6575 if (Lex.getKind() == lltok::LocalVarID) {
6576 NameID = Lex.getUIntVal();
6577 Lex.Lex();
6578 if (parseToken(lltok::equal, "expected '=' after instruction id"))
6579 return true;
6580 } else if (Lex.getKind() == lltok::LocalVar) {
6581 NameStr = Lex.getStrVal();
6582 Lex.Lex();
6583 if (parseToken(lltok::equal, "expected '=' after instruction name"))
6584 return true;
6585 }
6586
6587 switch (parseInstruction(Inst, BB, PFS)) {
6588 default:
6589 llvm_unreachable("Unknown parseInstruction result!");
6590 case InstError: return true;
6591 case InstNormal:
6592 Inst->insertInto(BB, BB->end());
6593
6594 // With a normal result, we check to see if the instruction is followed by
6595 // a comma and metadata.
6596 if (EatIfPresent(lltok::comma))
6597 if (parseInstructionMetadata(*Inst))
6598 return true;
6599 break;
6600 case InstExtraComma:
6601 Inst->insertInto(BB, BB->end());
6602
6603 // If the instruction parser ate an extra comma at the end of it, it
6604 // *must* be followed by metadata.
6605 if (parseInstructionMetadata(*Inst))
6606 return true;
6607 break;
6608 }
6609
6610 // Set the name on the instruction.
6611 if (PFS.setInstName(NameID, NameStr, NameLoc, Inst))
6612 return true;
6613
6614 // Attach any preceding debug values to this instruction.
6615 for (DbgRecordPtr &DR : TrailingDbgRecord)
6616 BB->insertDbgRecordBefore(DR.release(), Inst->getIterator());
6617 TrailingDbgRecord.clear();
6618 } while (!Inst->isTerminator());
6619
6620 assert(TrailingDbgRecord.empty() &&
6621 "All debug values should have been attached to an instruction.");
6622
6623 return false;
6624}
6625
6626/// parseDebugRecord
6627/// ::= #dbg_label '(' MDNode ')'
6628/// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ','
6629/// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')'
6630bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
6631 using RecordKind = DbgRecord::Kind;
6632 using LocType = DbgVariableRecord::LocationType;
6633 LocTy DVRLoc = Lex.getLoc();
6634 if (Lex.getKind() != lltok::DbgRecordType)
6635 return error(DVRLoc, "expected debug record type here");
6637 .Case("declare", RecordKind::ValueKind)
6638 .Case("value", RecordKind::ValueKind)
6639 .Case("assign", RecordKind::ValueKind)
6640 .Case("label", RecordKind::LabelKind);
6641
6642 // Parsing labels is trivial; parse here and early exit, otherwise go into the
6643 // full DbgVariableRecord processing stage.
6644 if (RecordType == RecordKind::LabelKind) {
6645 Lex.Lex();
6646 if (parseToken(lltok::lparen, "Expected '(' here"))
6647 return true;
6648 MDNode *Label;
6649 if (parseMDNode(Label))
6650 return true;
6651 if (parseToken(lltok::comma, "Expected ',' here"))
6652 return true;
6653 MDNode *DbgLoc;
6654 if (parseMDNode(DbgLoc))
6655 return true;
6656 if (parseToken(lltok::rparen, "Expected ')' here"))
6657 return true;
6659 return false;
6660 }
6661
6663 .Case("declare", LocType::Declare)
6664 .Case("value", LocType::Value)
6665 .Case("assign", LocType::Assign);
6666
6667 Lex.Lex();
6668 if (parseToken(lltok::lparen, "Expected '(' here"))
6669 return true;
6670
6671 // Parse Value field.
6672 Metadata *ValLocMD;
6673 if (parseMetadata(ValLocMD, &PFS))
6674 return true;
6675 if (parseToken(lltok::comma, "Expected ',' here"))
6676 return true;
6677
6678 // Parse Variable field.
6679 MDNode *Variable;
6680 if (parseMDNode(Variable))
6681 return true;
6682 if (parseToken(lltok::comma, "Expected ',' here"))
6683 return true;
6684
6685 // Parse Expression field.
6687 if (parseMDNode(Expression))
6688 return true;
6689 if (parseToken(lltok::comma, "Expected ',' here"))
6690 return true;
6691
6692 // Parse additional fields for #dbg_assign.
6693 MDNode *AssignID = nullptr;
6694 Metadata *AddressLocation = nullptr;
6695 MDNode *AddressExpression = nullptr;
6696 if (ValueType == LocType::Assign) {
6697 // Parse DIAssignID.
6698 if (parseMDNode(AssignID))
6699 return true;
6700 if (parseToken(lltok::comma, "Expected ',' here"))
6701 return true;
6702
6703 // Parse address ValueAsMetadata.
6704 if (parseMetadata(AddressLocation, &PFS))
6705 return true;
6706 if (parseToken(lltok::comma, "Expected ',' here"))
6707 return true;
6708
6709 // Parse address DIExpression.
6710 if (parseMDNode(AddressExpression))
6711 return true;
6712 if (parseToken(lltok::comma, "Expected ',' here"))
6713 return true;
6714 }
6715
6716 /// Parse DILocation.
6718 if (parseMDNode(DebugLoc))
6719 return true;
6720
6721 if (parseToken(lltok::rparen, "Expected ')' here"))
6722 return true;
6724 ValueType, ValLocMD, Variable, Expression, AssignID, AddressLocation,
6725 AddressExpression, DebugLoc);
6726 return false;
6727}
6728//===----------------------------------------------------------------------===//
6729// Instruction Parsing.
6730//===----------------------------------------------------------------------===//
6731
6732/// parseInstruction - parse one of the many different instructions.
6733///
6734int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
6735 PerFunctionState &PFS) {
6736 lltok::Kind Token = Lex.getKind();
6737 if (Token == lltok::Eof)
6738 return tokError("found end of file when expecting more instructions");
6739 LocTy Loc = Lex.getLoc();
6740 unsigned KeywordVal = Lex.getUIntVal();
6741 Lex.Lex(); // Eat the keyword.
6742
6743 switch (Token) {
6744 default:
6745 return error(Loc, "expected instruction opcode");
6746 // Terminator Instructions.
6747 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
6748 case lltok::kw_ret:
6749 return parseRet(Inst, BB, PFS);
6750 case lltok::kw_br:
6751 return parseBr(Inst, PFS);
6752 case lltok::kw_switch:
6753 return parseSwitch(Inst, PFS);
6755 return parseIndirectBr(Inst, PFS);
6756 case lltok::kw_invoke:
6757 return parseInvoke(Inst, PFS);
6758 case lltok::kw_resume:
6759 return parseResume(Inst, PFS);
6761 return parseCleanupRet(Inst, PFS);
6762 case lltok::kw_catchret:
6763 return parseCatchRet(Inst, PFS);
6765 return parseCatchSwitch(Inst, PFS);
6766 case lltok::kw_catchpad:
6767 return parseCatchPad(Inst, PFS);
6769 return parseCleanupPad(Inst, PFS);
6770 case lltok::kw_callbr:
6771 return parseCallBr(Inst, PFS);
6772 // Unary Operators.
6773 case lltok::kw_fneg: {
6774 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6775 int Res = parseUnaryOp(Inst, PFS, KeywordVal, /*IsFP*/ true);
6776 if (Res != 0)
6777 return Res;
6778 if (FMF.any())
6779 Inst->setFastMathFlags(FMF);
6780 return false;
6781 }
6782 // Binary Operators.
6783 case lltok::kw_add:
6784 case lltok::kw_sub:
6785 case lltok::kw_mul:
6786 case lltok::kw_shl: {
6787 bool NUW = EatIfPresent(lltok::kw_nuw);
6788 bool NSW = EatIfPresent(lltok::kw_nsw);
6789 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
6790
6791 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
6792 return true;
6793
6794 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
6795 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
6796 return false;
6797 }
6798 case lltok::kw_fadd:
6799 case lltok::kw_fsub:
6800 case lltok::kw_fmul:
6801 case lltok::kw_fdiv:
6802 case lltok::kw_frem: {
6803 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6804 int Res = parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ true);
6805 if (Res != 0)
6806 return Res;
6807 if (FMF.any())
6808 Inst->setFastMathFlags(FMF);
6809 return 0;
6810 }
6811
6812 case lltok::kw_sdiv:
6813 case lltok::kw_udiv:
6814 case lltok::kw_lshr:
6815 case lltok::kw_ashr: {
6816 bool Exact = EatIfPresent(lltok::kw_exact);
6817
6818 if (parseArithmetic(Inst, PFS, KeywordVal, /*IsFP*/ false))
6819 return true;
6820 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
6821 return false;
6822 }
6823
6824 case lltok::kw_urem:
6825 case lltok::kw_srem:
6826 return parseArithmetic(Inst, PFS, KeywordVal,
6827 /*IsFP*/ false);
6828 case lltok::kw_or: {
6829 bool Disjoint = EatIfPresent(lltok::kw_disjoint);
6830 if (parseLogical(Inst, PFS, KeywordVal))
6831 return true;
6832 if (Disjoint)
6833 cast<PossiblyDisjointInst>(Inst)->setIsDisjoint(true);
6834 return false;
6835 }
6836 case lltok::kw_and:
6837 case lltok::kw_xor:
6838 return parseLogical(Inst, PFS, KeywordVal);
6839 case lltok::kw_icmp:
6840 return parseCompare(Inst, PFS, KeywordVal);
6841 case lltok::kw_fcmp: {
6842 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6843 int Res = parseCompare(Inst, PFS, KeywordVal);
6844 if (Res != 0)
6845 return Res;
6846 if (FMF.any())
6847 Inst->setFastMathFlags(FMF);
6848 return 0;
6849 }
6850
6851 // Casts.
6852 case lltok::kw_uitofp:
6853 case lltok::kw_zext: {
6854 bool NonNeg = EatIfPresent(lltok::kw_nneg);
6855 bool Res = parseCast(Inst, PFS, KeywordVal);
6856 if (Res != 0)
6857 return Res;
6858 if (NonNeg)
6859 Inst->setNonNeg();
6860 return 0;
6861 }
6862 case lltok::kw_trunc: {
6863 bool NUW = EatIfPresent(lltok::kw_nuw);
6864 bool NSW = EatIfPresent(lltok::kw_nsw);
6865 if (!NUW)
6866 NUW = EatIfPresent(lltok::kw_nuw);
6867 if (parseCast(Inst, PFS, KeywordVal))
6868 return true;
6869 if (NUW)
6870 cast<TruncInst>(Inst)->setHasNoUnsignedWrap(true);
6871 if (NSW)
6872 cast<TruncInst>(Inst)->setHasNoSignedWrap(true);
6873 return false;
6874 }
6875 case lltok::kw_sext:
6876 case lltok::kw_fptrunc:
6877 case lltok::kw_fpext:
6878 case lltok::kw_bitcast:
6880 case lltok::kw_sitofp:
6881 case lltok::kw_fptoui:
6882 case lltok::kw_fptosi:
6883 case lltok::kw_inttoptr:
6884 case lltok::kw_ptrtoint:
6885 return parseCast(Inst, PFS, KeywordVal);
6886 // Other.
6887 case lltok::kw_select: {
6888 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6889 int Res = parseSelect(Inst, PFS);
6890 if (Res != 0)
6891 return Res;
6892 if (FMF.any()) {
6893 if (!isa<FPMathOperator>(Inst))
6894 return error(Loc, "fast-math-flags specified for select without "
6895 "floating-point scalar or vector return type");
6896 Inst->setFastMathFlags(FMF);
6897 }
6898 return 0;
6899 }
6900 case lltok::kw_va_arg:
6901 return parseVAArg(Inst, PFS);
6903 return parseExtractElement(Inst, PFS);
6905 return parseInsertElement(Inst, PFS);
6907 return parseShuffleVector(Inst, PFS);
6908 case lltok::kw_phi: {
6909 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6910 int Res = parsePHI(Inst, PFS);
6911 if (Res != 0)
6912 return Res;
6913 if (FMF.any()) {
6914 if (!isa<FPMathOperator>(Inst))
6915 return error(Loc, "fast-math-flags specified for phi without "
6916 "floating-point scalar or vector return type");
6917 Inst->setFastMathFlags(FMF);
6918 }
6919 return 0;
6920 }
6922 return parseLandingPad(Inst, PFS);
6923 case lltok::kw_freeze:
6924 return parseFreeze(Inst, PFS);
6925 // Call.
6926 case lltok::kw_call:
6927 return parseCall(Inst, PFS, CallInst::TCK_None);
6928 case lltok::kw_tail:
6929 return parseCall(Inst, PFS, CallInst::TCK_Tail);
6930 case lltok::kw_musttail:
6931 return parseCall(Inst, PFS, CallInst::TCK_MustTail);
6932 case lltok::kw_notail:
6933 return parseCall(Inst, PFS, CallInst::TCK_NoTail);
6934 // Memory.
6935 case lltok::kw_alloca:
6936 return parseAlloc(Inst, PFS);
6937 case lltok::kw_load:
6938 return parseLoad(Inst, PFS);
6939 case lltok::kw_store:
6940 return parseStore(Inst, PFS);
6941 case lltok::kw_cmpxchg:
6942 return parseCmpXchg(Inst, PFS);
6944 return parseAtomicRMW(Inst, PFS);
6945 case lltok::kw_fence:
6946 return parseFence(Inst, PFS);
6948 return parseGetElementPtr(Inst, PFS);
6950 return parseExtractValue(Inst, PFS);
6952 return parseInsertValue(Inst, PFS);
6953 }
6954}
6955
6956/// parseCmpPredicate - parse an integer or fp predicate, based on Kind.
6957bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) {
6958 if (Opc == Instruction::FCmp) {
6959 switch (Lex.getKind()) {
6960 default:
6961 return tokError("expected fcmp predicate (e.g. 'oeq')");
6962 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
6963 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
6964 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
6965 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
6966 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
6967 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
6968 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
6969 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
6970 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
6971 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
6972 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
6973 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
6974 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
6975 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
6976 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
6977 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
6978 }
6979 } else {
6980 switch (Lex.getKind()) {
6981 default:
6982 return tokError("expected icmp predicate (e.g. 'eq')");
6983 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
6984 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
6985 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
6986 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
6987 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
6988 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
6989 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
6990 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
6991 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
6992 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
6993 }
6994 }
6995 Lex.Lex();
6996 return false;
6997}
6998
6999//===----------------------------------------------------------------------===//
7000// Terminator Instructions.
7001//===----------------------------------------------------------------------===//
7002
7003/// parseRet - parse a return instruction.
7004/// ::= 'ret' void (',' !dbg, !1)*
7005/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
7006bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB,
7007 PerFunctionState &PFS) {
7008 SMLoc TypeLoc = Lex.getLoc();
7009 Type *Ty = nullptr;
7010 if (parseType(Ty, true /*void allowed*/))
7011 return true;
7012
7013 Type *ResType = PFS.getFunction().getReturnType();
7014
7015 if (Ty->isVoidTy()) {
7016 if (!ResType->isVoidTy())
7017 return error(TypeLoc, "value doesn't match function result type '" +
7018 getTypeString(ResType) + "'");
7019
7020 Inst = ReturnInst::Create(Context);
7021 return false;
7022 }
7023
7024 Value *RV;
7025 if (parseValue(Ty, RV, PFS))
7026 return true;
7027
7028 if (ResType != RV->getType())
7029 return error(TypeLoc, "value doesn't match function result type '" +
7030 getTypeString(ResType) + "'");
7031
7032 Inst = ReturnInst::Create(Context, RV);
7033 return false;
7034}
7035
7036/// parseBr
7037/// ::= 'br' TypeAndValue
7038/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
7039bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) {
7040 LocTy Loc, Loc2;
7041 Value *Op0;
7042 BasicBlock *Op1, *Op2;
7043 if (parseTypeAndValue(Op0, Loc, PFS))
7044 return true;
7045
7046 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
7047 Inst = BranchInst::Create(BB);
7048 return false;
7049 }
7050
7051 if (Op0->getType() != Type::getInt1Ty(Context))
7052 return error(Loc, "branch condition must have 'i1' type");
7053
7054 if (parseToken(lltok::comma, "expected ',' after branch condition") ||
7055 parseTypeAndBasicBlock(Op1, Loc, PFS) ||
7056 parseToken(lltok::comma, "expected ',' after true destination") ||
7057 parseTypeAndBasicBlock(Op2, Loc2, PFS))
7058 return true;
7059
7060 Inst = BranchInst::Create(Op1, Op2, Op0);
7061 return false;
7062}
7063
7064/// parseSwitch
7065/// Instruction
7066/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
7067/// JumpTable
7068/// ::= (TypeAndValue ',' TypeAndValue)*
7069bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
7070 LocTy CondLoc, BBLoc;
7071 Value *Cond;
7072 BasicBlock *DefaultBB;
7073 if (parseTypeAndValue(Cond, CondLoc, PFS) ||
7074 parseToken(lltok::comma, "expected ',' after switch condition") ||
7075 parseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
7076 parseToken(lltok::lsquare, "expected '[' with switch table"))
7077 return true;
7078
7079 if (!Cond->getType()->isIntegerTy())
7080 return error(CondLoc, "switch condition must have integer type");
7081
7082 // parse the jump table pairs.
7083 SmallPtrSet<Value*, 32> SeenCases;
7085 while (Lex.getKind() != lltok::rsquare) {
7086 Value *Constant;
7087 BasicBlock *DestBB;
7088
7089 if (parseTypeAndValue(Constant, CondLoc, PFS) ||
7090 parseToken(lltok::comma, "expected ',' after case value") ||
7091 parseTypeAndBasicBlock(DestBB, PFS))
7092 return true;
7093
7094 if (!SeenCases.insert(Constant).second)
7095 return error(CondLoc, "duplicate case value in switch");
7096 if (!isa<ConstantInt>(Constant))
7097 return error(CondLoc, "case value is not a constant integer");
7098
7099 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
7100 }
7101
7102 Lex.Lex(); // Eat the ']'.
7103
7104 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
7105 for (unsigned i = 0, e = Table.size(); i != e; ++i)
7106 SI->addCase(Table[i].first, Table[i].second);
7107 Inst = SI;
7108 return false;
7109}
7110
7111/// parseIndirectBr
7112/// Instruction
7113/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
7114bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
7115 LocTy AddrLoc;
7116 Value *Address;
7117 if (parseTypeAndValue(Address, AddrLoc, PFS) ||
7118 parseToken(lltok::comma, "expected ',' after indirectbr address") ||
7119 parseToken(lltok::lsquare, "expected '[' with indirectbr"))
7120 return true;
7121
7122 if (!Address->getType()->isPointerTy())
7123 return error(AddrLoc, "indirectbr address must have pointer type");
7124
7125 // parse the destination list.
7127
7128 if (Lex.getKind() != lltok::rsquare) {
7129 BasicBlock *DestBB;
7130 if (parseTypeAndBasicBlock(DestBB, PFS))
7131 return true;
7132 DestList.push_back(DestBB);
7133
7134 while (EatIfPresent(lltok::comma)) {
7135 if (parseTypeAndBasicBlock(DestBB, PFS))
7136 return true;
7137 DestList.push_back(DestBB);
7138 }
7139 }
7140
7141 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
7142 return true;
7143
7145 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
7146 IBI->addDestination(DestList[i]);
7147 Inst = IBI;
7148 return false;
7149}
7150
7151// If RetType is a non-function pointer type, then this is the short syntax
7152// for the call, which means that RetType is just the return type. Infer the
7153// rest of the function argument types from the arguments that are present.
7154bool LLParser::resolveFunctionType(Type *RetType,
7155 const SmallVector<ParamInfo, 16> &ArgList,
7156 FunctionType *&FuncTy) {
7157 FuncTy = dyn_cast<FunctionType>(RetType);
7158 if (!FuncTy) {
7159 // Pull out the types of all of the arguments...
7160 std::vector<Type*> ParamTypes;
7161 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
7162 ParamTypes.push_back(ArgList[i].V->getType());
7163
7164 if (!FunctionType::isValidReturnType(RetType))
7165 return true;
7166
7167 FuncTy = FunctionType::get(RetType, ParamTypes, false);
7168 }
7169 return false;
7170}
7171
7172/// parseInvoke
7173/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
7174/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
7175bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
7176 LocTy CallLoc = Lex.getLoc();
7177 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
7178 std::vector<unsigned> FwdRefAttrGrps;
7179 LocTy NoBuiltinLoc;
7180 unsigned CC;
7181 unsigned InvokeAddrSpace;
7182 Type *RetType = nullptr;
7183 LocTy RetTypeLoc;
7184 ValID CalleeID;
7187
7188 BasicBlock *NormalBB, *UnwindBB;
7189 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
7190 parseOptionalProgramAddrSpace(InvokeAddrSpace) ||
7191 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
7192 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
7193 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
7194 NoBuiltinLoc) ||
7195 parseOptionalOperandBundles(BundleList, PFS) ||
7196 parseToken(lltok::kw_to, "expected 'to' in invoke") ||
7197 parseTypeAndBasicBlock(NormalBB, PFS) ||
7198 parseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
7199 parseTypeAndBasicBlock(UnwindBB, PFS))
7200 return true;
7201
7202 // If RetType is a non-function pointer type, then this is the short syntax
7203 // for the call, which means that RetType is just the return type. Infer the
7204 // rest of the function argument types from the arguments that are present.
7205 FunctionType *Ty;
7206 if (resolveFunctionType(RetType, ArgList, Ty))
7207 return error(RetTypeLoc, "Invalid result type for LLVM function");
7208
7209 CalleeID.FTy = Ty;
7210
7211 // Look up the callee.
7212 Value *Callee;
7213 if (convertValIDToValue(PointerType::get(Ty, InvokeAddrSpace), CalleeID,
7214 Callee, &PFS))
7215 return true;
7216
7217 // Set up the Attribute for the function.
7220
7221 // Loop through FunctionType's arguments and ensure they are specified
7222 // correctly. Also, gather any parameter attributes.
7223 FunctionType::param_iterator I = Ty->param_begin();
7224 FunctionType::param_iterator E = Ty->param_end();
7225 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
7226 Type *ExpectedTy = nullptr;
7227 if (I != E) {
7228 ExpectedTy = *I++;
7229 } else if (!Ty->isVarArg()) {
7230 return error(ArgList[i].Loc, "too many arguments specified");
7231 }
7232
7233 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
7234 return error(ArgList[i].Loc, "argument is not of expected type '" +
7235 getTypeString(ExpectedTy) + "'");
7236 Args.push_back(ArgList[i].V);
7237 ArgAttrs.push_back(ArgList[i].Attrs);
7238 }
7239
7240 if (I != E)
7241 return error(CallLoc, "not enough parameters specified for call");
7242
7243 // Finish off the Attribute and check them
7244 AttributeList PAL =
7245 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
7246 AttributeSet::get(Context, RetAttrs), ArgAttrs);
7247
7248 InvokeInst *II =
7249 InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
7250 II->setCallingConv(CC);
7251 II->setAttributes(PAL);
7252 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
7253 Inst = II;
7254 return false;
7255}
7256
7257/// parseResume
7258/// ::= 'resume' TypeAndValue
7259bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) {
7260 Value *Exn; LocTy ExnLoc;
7261 if (parseTypeAndValue(Exn, ExnLoc, PFS))
7262 return true;
7263
7264 ResumeInst *RI = ResumeInst::Create(Exn);
7265 Inst = RI;
7266 return false;
7267}
7268
7269bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args,
7270 PerFunctionState &PFS) {
7271 if (parseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
7272 return true;
7273
7274 while (Lex.getKind() != lltok::rsquare) {
7275 // If this isn't the first argument, we need a comma.
7276 if (!Args.empty() &&
7277 parseToken(lltok::comma, "expected ',' in argument list"))
7278 return true;
7279
7280 // parse the argument.
7281 LocTy ArgLoc;
7282 Type *ArgTy = nullptr;
7283 if (parseType(ArgTy, ArgLoc))
7284 return true;
7285
7286 Value *V;
7287 if (ArgTy->isMetadataTy()) {
7288 if (parseMetadataAsValue(V, PFS))
7289 return true;
7290 } else {
7291 if (parseValue(ArgTy, V, PFS))
7292 return true;
7293 }
7294 Args.push_back(V);
7295 }
7296
7297 Lex.Lex(); // Lex the ']'.
7298 return false;
7299}
7300
7301/// parseCleanupRet
7302/// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
7303bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
7304 Value *CleanupPad = nullptr;
7305
7306 if (parseToken(lltok::kw_from, "expected 'from' after cleanupret"))
7307 return true;
7308
7309 if (parseValue(Type::getTokenTy(Context), CleanupPad, PFS))
7310 return true;
7311
7312 if (parseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
7313 return true;
7314
7315 BasicBlock *UnwindBB = nullptr;
7316 if (Lex.getKind() == lltok::kw_to) {
7317 Lex.Lex();
7318 if (parseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
7319 return true;
7320 } else {
7321 if (parseTypeAndBasicBlock(UnwindBB, PFS)) {
7322 return true;
7323 }
7324 }
7325
7326 Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
7327 return false;
7328}
7329
7330/// parseCatchRet
7331/// ::= 'catchret' from Parent Value 'to' TypeAndValue
7332bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
7333 Value *CatchPad = nullptr;
7334
7335 if (parseToken(lltok::kw_from, "expected 'from' after catchret"))
7336 return true;
7337
7338 if (parseValue(Type::getTokenTy(Context), CatchPad, PFS))
7339 return true;
7340
7341 BasicBlock *BB;
7342 if (parseToken(lltok::kw_to, "expected 'to' in catchret") ||
7343 parseTypeAndBasicBlock(BB, PFS))
7344 return true;
7345
7346 Inst = CatchReturnInst::Create(CatchPad, BB);
7347 return false;
7348}
7349
7350/// parseCatchSwitch
7351/// ::= 'catchswitch' within Parent
7352bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
7353 Value *ParentPad;
7354
7355 if (parseToken(lltok::kw_within, "expected 'within' after catchswitch"))
7356 return true;
7357
7358 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
7359 Lex.getKind() != lltok::LocalVarID)
7360 return tokError("expected scope value for catchswitch");
7361
7362 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
7363 return true;
7364
7365 if (parseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
7366 return true;
7367
7369 do {
7370 BasicBlock *DestBB;
7371 if (parseTypeAndBasicBlock(DestBB, PFS))
7372 return true;
7373 Table.push_back(DestBB);
7374 } while (EatIfPresent(lltok::comma));
7375
7376 if (parseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
7377 return true;
7378
7379 if (parseToken(lltok::kw_unwind, "expected 'unwind' after catchswitch scope"))
7380 return true;
7381
7382 BasicBlock *UnwindBB = nullptr;
7383 if (EatIfPresent(lltok::kw_to)) {
7384 if (parseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
7385 return true;
7386 } else {
7387 if (parseTypeAndBasicBlock(UnwindBB, PFS))
7388 return true;
7389 }
7390
7391 auto *CatchSwitch =
7392 CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
7393 for (BasicBlock *DestBB : Table)
7394 CatchSwitch->addHandler(DestBB);
7395 Inst = CatchSwitch;
7396 return false;
7397}
7398
7399/// parseCatchPad
7400/// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
7401bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
7402 Value *CatchSwitch = nullptr;
7403
7404 if (parseToken(lltok::kw_within, "expected 'within' after catchpad"))
7405 return true;
7406
7407 if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
7408 return tokError("expected scope value for catchpad");
7409
7410 if (parseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
7411 return true;
7412
7414 if (parseExceptionArgs(Args, PFS))
7415 return true;
7416
7417 Inst = CatchPadInst::Create(CatchSwitch, Args);
7418 return false;
7419}
7420
7421/// parseCleanupPad
7422/// ::= 'cleanuppad' within Parent ParamList
7423bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
7424 Value *ParentPad = nullptr;
7425
7426 if (parseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
7427 return true;
7428
7429 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
7430 Lex.getKind() != lltok::LocalVarID)
7431 return tokError("expected scope value for cleanuppad");
7432
7433 if (parseValue(Type::getTokenTy(Context), ParentPad, PFS))
7434 return true;
7435
7437 if (parseExceptionArgs(Args, PFS))
7438 return true;
7439
7440 Inst = CleanupPadInst::Create(ParentPad, Args);
7441 return false;
7442}
7443
7444//===----------------------------------------------------------------------===//
7445// Unary Operators.
7446//===----------------------------------------------------------------------===//
7447
7448/// parseUnaryOp
7449/// ::= UnaryOp TypeAndValue ',' Value
7450///
7451/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
7452/// operand is allowed.
7453bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
7454 unsigned Opc, bool IsFP) {
7455 LocTy Loc; Value *LHS;
7456 if (parseTypeAndValue(LHS, Loc, PFS))
7457 return true;
7458
7459 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
7461
7462 if (!Valid)
7463 return error(Loc, "invalid operand type for instruction");
7464
7466 return false;
7467}
7468
7469/// parseCallBr
7470/// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList
7471/// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue
7472/// '[' LabelList ']'
7473bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
7474 LocTy CallLoc = Lex.getLoc();
7475 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
7476 std::vector<unsigned> FwdRefAttrGrps;
7477 LocTy NoBuiltinLoc;
7478 unsigned CC;
7479 Type *RetType = nullptr;
7480 LocTy RetTypeLoc;
7481 ValID CalleeID;
7484
7485 BasicBlock *DefaultDest;
7486 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
7487 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
7488 parseValID(CalleeID, &PFS) || parseParameterList(ArgList, PFS) ||
7489 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
7490 NoBuiltinLoc) ||
7491 parseOptionalOperandBundles(BundleList, PFS) ||
7492 parseToken(lltok::kw_to, "expected 'to' in callbr") ||
7493 parseTypeAndBasicBlock(DefaultDest, PFS) ||
7494 parseToken(lltok::lsquare, "expected '[' in callbr"))
7495 return true;
7496
7497 // parse the destination list.
7498 SmallVector<BasicBlock *, 16> IndirectDests;
7499
7500 if (Lex.getKind() != lltok::rsquare) {
7501 BasicBlock *DestBB;
7502 if (parseTypeAndBasicBlock(DestBB, PFS))
7503 return true;
7504 IndirectDests.push_back(DestBB);
7505
7506 while (EatIfPresent(lltok::comma)) {
7507 if (parseTypeAndBasicBlock(DestBB, PFS))
7508 return true;
7509 IndirectDests.push_back(DestBB);
7510 }
7511 }
7512
7513 if (parseToken(lltok::rsquare, "expected ']' at end of block list"))
7514 return true;
7515
7516 // If RetType is a non-function pointer type, then this is the short syntax
7517 // for the call, which means that RetType is just the return type. Infer the
7518 // rest of the function argument types from the arguments that are present.
7519 FunctionType *Ty;
7520 if (resolveFunctionType(RetType, ArgList, Ty))
7521 return error(RetTypeLoc, "Invalid result type for LLVM function");
7522
7523 CalleeID.FTy = Ty;
7524
7525 // Look up the callee.
7526 Value *Callee;
7527 if (convertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
7528 return true;
7529
7530 // Set up the Attribute for the function.
7533
7534 // Loop through FunctionType's arguments and ensure they are specified
7535 // correctly. Also, gather any parameter attributes.
7536 FunctionType::param_iterator I = Ty->param_begin();
7537 FunctionType::param_iterator E = Ty->param_end();
7538 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
7539 Type *ExpectedTy = nullptr;
7540 if (I != E) {
7541 ExpectedTy = *I++;
7542 } else if (!Ty->isVarArg()) {
7543 return error(ArgList[i].Loc, "too many arguments specified");
7544 }
7545
7546 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
7547 return error(ArgList[i].Loc, "argument is not of expected type '" +
7548 getTypeString(ExpectedTy) + "'");
7549 Args.push_back(ArgList[i].V);
7550 ArgAttrs.push_back(ArgList[i].Attrs);
7551 }
7552
7553 if (I != E)
7554 return error(CallLoc, "not enough parameters specified for call");
7555
7556 // Finish off the Attribute and check them
7557 AttributeList PAL =
7558 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
7559 AttributeSet::get(Context, RetAttrs), ArgAttrs);
7560
7561 CallBrInst *CBI =
7562 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
7563 BundleList);
7564 CBI->setCallingConv(CC);
7565 CBI->setAttributes(PAL);
7566 ForwardRefAttrGroups[CBI] = FwdRefAttrGrps;
7567 Inst = CBI;
7568 return false;
7569}
7570
7571//===----------------------------------------------------------------------===//
7572// Binary Operators.
7573//===----------------------------------------------------------------------===//
7574
7575/// parseArithmetic
7576/// ::= ArithmeticOps TypeAndValue ',' Value
7577///
7578/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
7579/// operand is allowed.
7580bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
7581 unsigned Opc, bool IsFP) {
7582 LocTy Loc; Value *LHS, *RHS;
7583 if (parseTypeAndValue(LHS, Loc, PFS) ||
7584 parseToken(lltok::comma, "expected ',' in arithmetic operation") ||
7585 parseValue(LHS->getType(), RHS, PFS))
7586 return true;
7587
7588 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
7590
7591 if (!Valid)
7592 return error(Loc, "invalid operand type for instruction");
7593
7594 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
7595 return false;
7596}
7597
7598/// parseLogical
7599/// ::= ArithmeticOps TypeAndValue ',' Value {
7600bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS,
7601 unsigned Opc) {
7602 LocTy Loc; Value *LHS, *RHS;
7603 if (parseTypeAndValue(LHS, Loc, PFS) ||
7604 parseToken(lltok::comma, "expected ',' in logical operation") ||
7605 parseValue(LHS->getType(), RHS, PFS))
7606 return true;
7607
7608 if (!LHS->getType()->isIntOrIntVectorTy())
7609 return error(Loc,
7610 "instruction requires integer or integer vector operands");
7611
7612 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
7613 return false;
7614}
7615
7616/// parseCompare
7617/// ::= 'icmp' IPredicates TypeAndValue ',' Value
7618/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
7619bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS,
7620 unsigned Opc) {
7621 // parse the integer/fp comparison predicate.
7622 LocTy Loc;
7623 unsigned Pred;
7624 Value *LHS, *RHS;
7625 if (parseCmpPredicate(Pred, Opc) || parseTypeAndValue(LHS, Loc, PFS) ||
7626 parseToken(lltok::comma, "expected ',' after compare value") ||
7627 parseValue(LHS->getType(), RHS, PFS))
7628 return true;
7629
7630 if (Opc == Instruction::FCmp) {
7631 if (!LHS->getType()->isFPOrFPVectorTy())
7632 return error(Loc, "fcmp requires floating point operands");
7633 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
7634 } else {
7635 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
7636 if (!LHS->getType()->isIntOrIntVectorTy() &&
7638 return error(Loc, "icmp requires integer operands");
7639 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
7640 }
7641 return false;
7642}
7643
7644//===----------------------------------------------------------------------===//
7645// Other Instructions.
7646//===----------------------------------------------------------------------===//
7647
7648/// parseCast
7649/// ::= CastOpc TypeAndValue 'to' Type
7650bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS,
7651 unsigned Opc) {
7652 LocTy Loc;
7653 Value *Op;
7654 Type *DestTy = nullptr;
7655 if (parseTypeAndValue(Op, Loc, PFS) ||
7656 parseToken(lltok::kw_to, "expected 'to' after cast value") ||
7657 parseType(DestTy))
7658 return true;
7659
7660 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
7662 return error(Loc, "invalid cast opcode for cast from '" +
7663 getTypeString(Op->getType()) + "' to '" +
7664 getTypeString(DestTy) + "'");
7665 }
7666 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
7667 return false;
7668}
7669
7670/// parseSelect
7671/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
7672bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) {
7673 LocTy Loc;
7674 Value *Op0, *Op1, *Op2;
7675 if (parseTypeAndValue(Op0, Loc, PFS) ||
7676 parseToken(lltok::comma, "expected ',' after select condition") ||
7677 parseTypeAndValue(Op1, PFS) ||
7678 parseToken(lltok::comma, "expected ',' after select value") ||
7679 parseTypeAndValue(Op2, PFS))
7680 return true;
7681
7682 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
7683 return error(Loc, Reason);
7684
7685 Inst = SelectInst::Create(Op0, Op1, Op2);
7686 return false;
7687}
7688
7689/// parseVAArg
7690/// ::= 'va_arg' TypeAndValue ',' Type
7691bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) {
7692 Value *Op;
7693 Type *EltTy = nullptr;
7694 LocTy TypeLoc;
7695 if (parseTypeAndValue(Op, PFS) ||
7696 parseToken(lltok::comma, "expected ',' after vaarg operand") ||
7697 parseType(EltTy, TypeLoc))
7698 return true;
7699
7700 if (!EltTy->isFirstClassType())
7701 return error(TypeLoc, "va_arg requires operand with first class type");
7702
7703 Inst = new VAArgInst(Op, EltTy);
7704 return false;
7705}
7706
7707/// parseExtractElement
7708/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
7709bool LLParser::parseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
7710 LocTy Loc;
7711 Value *Op0, *Op1;
7712 if (parseTypeAndValue(Op0, Loc, PFS) ||
7713 parseToken(lltok::comma, "expected ',' after extract value") ||
7714 parseTypeAndValue(Op1, PFS))
7715 return true;
7716
7718 return error(Loc, "invalid extractelement operands");
7719
7720 Inst = ExtractElementInst::Create(Op0, Op1);
7721 return false;
7722}
7723
7724/// parseInsertElement
7725/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
7726bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
7727 LocTy Loc;
7728 Value *Op0, *Op1, *Op2;
7729 if (parseTypeAndValue(Op0, Loc, PFS) ||
7730 parseToken(lltok::comma, "expected ',' after insertelement value") ||
7731 parseTypeAndValue(Op1, PFS) ||
7732 parseToken(lltok::comma, "expected ',' after insertelement value") ||
7733 parseTypeAndValue(Op2, PFS))
7734 return true;
7735
7736 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
7737 return error(Loc, "invalid insertelement operands");
7738
7739 Inst = InsertElementInst::Create(Op0, Op1, Op2);
7740 return false;
7741}
7742
7743/// parseShuffleVector
7744/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
7745bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
7746 LocTy Loc;
7747 Value *Op0, *Op1, *Op2;
7748 if (parseTypeAndValue(Op0, Loc, PFS) ||
7749 parseToken(lltok::comma, "expected ',' after shuffle mask") ||
7750 parseTypeAndValue(Op1, PFS) ||
7751 parseToken(lltok::comma, "expected ',' after shuffle value") ||
7752 parseTypeAndValue(Op2, PFS))
7753 return true;
7754
7755 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
7756 return error(Loc, "invalid shufflevector operands");
7757
7758 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
7759 return false;
7760}
7761
7762/// parsePHI
7763/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
7764int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) {
7765 Type *Ty = nullptr; LocTy TypeLoc;
7766 Value *Op0, *Op1;
7767
7768 if (parseType(Ty, TypeLoc))
7769 return true;
7770
7771 if (!Ty->isFirstClassType())
7772 return error(TypeLoc, "phi node must have first class type");
7773
7774 bool First = true;
7775 bool AteExtraComma = false;
7777
7778 while (true) {
7779 if (First) {
7780 if (Lex.getKind() != lltok::lsquare)
7781 break;
7782 First = false;
7783 } else if (!EatIfPresent(lltok::comma))
7784 break;
7785
7786 if (Lex.getKind() == lltok::MetadataVar) {
7787 AteExtraComma = true;
7788 break;
7789 }
7790
7791 if (parseToken(lltok::lsquare, "expected '[' in phi value list") ||
7792 parseValue(Ty, Op0, PFS) ||
7793 parseToken(lltok::comma, "expected ',' after insertelement value") ||
7794 parseValue(Type::getLabelTy(Context), Op1, PFS) ||
7795 parseToken(lltok::rsquare, "expected ']' in phi value list"))
7796 return true;
7797
7798 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
7799 }
7800
7801 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
7802 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
7803 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
7804 Inst = PN;
7805 return AteExtraComma ? InstExtraComma : InstNormal;
7806}
7807
7808/// parseLandingPad
7809/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
7810/// Clause
7811/// ::= 'catch' TypeAndValue
7812/// ::= 'filter'
7813/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
7814bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
7815 Type *Ty = nullptr; LocTy TyLoc;
7816
7817 if (parseType(Ty, TyLoc))
7818 return true;
7819
7820 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
7821 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
7822
7823 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
7825 if (EatIfPresent(lltok::kw_catch))
7827 else if (EatIfPresent(lltok::kw_filter))
7829 else
7830 return tokError("expected 'catch' or 'filter' clause type");
7831
7832 Value *V;
7833 LocTy VLoc;
7834 if (parseTypeAndValue(V, VLoc, PFS))
7835 return true;
7836
7837 // A 'catch' type expects a non-array constant. A filter clause expects an
7838 // array constant.
7839 if (CT == LandingPadInst::Catch) {
7840 if (isa<ArrayType>(V->getType()))
7841 error(VLoc, "'catch' clause has an invalid type");
7842 } else {
7843 if (!isa<ArrayType>(V->getType()))
7844 error(VLoc, "'filter' clause has an invalid type");
7845 }
7846
7847 Constant *CV = dyn_cast<Constant>(V);
7848 if (!CV)
7849 return error(VLoc, "clause argument must be a constant");
7850 LP->addClause(CV);
7851 }
7852
7853 Inst = LP.release();
7854 return false;
7855}
7856
7857/// parseFreeze
7858/// ::= 'freeze' Type Value
7859bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) {
7860 LocTy Loc;
7861 Value *Op;
7862 if (parseTypeAndValue(Op, Loc, PFS))
7863 return true;
7864
7865 Inst = new FreezeInst(Op);
7866 return false;
7867}
7868
7869/// parseCall
7870/// ::= 'call' OptionalFastMathFlags OptionalCallingConv
7871/// OptionalAttrs Type Value ParameterList OptionalAttrs
7872/// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
7873/// OptionalAttrs Type Value ParameterList OptionalAttrs
7874/// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
7875/// OptionalAttrs Type Value ParameterList OptionalAttrs
7876/// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv
7877/// OptionalAttrs Type Value ParameterList OptionalAttrs
7878bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
7880 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
7881 std::vector<unsigned> FwdRefAttrGrps;
7882 LocTy BuiltinLoc;
7883 unsigned CallAddrSpace;
7884 unsigned CC;
7885 Type *RetType = nullptr;
7886 LocTy RetTypeLoc;
7887 ValID CalleeID;
7890 LocTy CallLoc = Lex.getLoc();
7891
7892 if (TCK != CallInst::TCK_None &&
7893 parseToken(lltok::kw_call,
7894 "expected 'tail call', 'musttail call', or 'notail call'"))
7895 return true;
7896
7897 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7898
7899 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(RetAttrs) ||
7900 parseOptionalProgramAddrSpace(CallAddrSpace) ||
7901 parseType(RetType, RetTypeLoc, true /*void allowed*/) ||
7902 parseValID(CalleeID, &PFS) ||
7903 parseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
7904 PFS.getFunction().isVarArg()) ||
7905 parseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
7906 parseOptionalOperandBundles(BundleList, PFS))
7907 return true;
7908
7909 // If RetType is a non-function pointer type, then this is the short syntax
7910 // for the call, which means that RetType is just the return type. Infer the
7911 // rest of the function argument types from the arguments that are present.
7912 FunctionType *Ty;
7913 if (resolveFunctionType(RetType, ArgList, Ty))
7914 return error(RetTypeLoc, "Invalid result type for LLVM function");
7915
7916 CalleeID.FTy = Ty;
7917
7918 // Look up the callee.
7919 Value *Callee;
7920 if (convertValIDToValue(PointerType::get(Ty, CallAddrSpace), CalleeID, Callee,
7921 &PFS))
7922 return true;
7923
7924 // Set up the Attribute for the function.
7926
7928
7929 // Loop through FunctionType's arguments and ensure they are specified
7930 // correctly. Also, gather any parameter attributes.
7931 FunctionType::param_iterator I = Ty->param_begin();
7932 FunctionType::param_iterator E = Ty->param_end();
7933 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
7934 Type *ExpectedTy = nullptr;
7935 if (I != E) {
7936 ExpectedTy = *I++;
7937 } else if (!Ty->isVarArg()) {
7938 return error(ArgList[i].Loc, "too many arguments specified");
7939 }
7940
7941 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
7942 return error(ArgList[i].Loc, "argument is not of expected type '" +
7943 getTypeString(ExpectedTy) + "'");
7944 Args.push_back(ArgList[i].V);
7945 Attrs.push_back(ArgList[i].Attrs);
7946 }
7947
7948 if (I != E)
7949 return error(CallLoc, "not enough parameters specified for call");
7950
7951 // Finish off the Attribute and check them
7952 AttributeList PAL =
7953 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
7954 AttributeSet::get(Context, RetAttrs), Attrs);
7955
7956 CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
7957 CI->setTailCallKind(TCK);
7958 CI->setCallingConv(CC);
7959 if (FMF.any()) {
7960 if (!isa<FPMathOperator>(CI)) {
7961 CI->deleteValue();
7962 return error(CallLoc, "fast-math-flags specified for call without "
7963 "floating-point scalar or vector return type");
7964 }
7965 CI->setFastMathFlags(FMF);
7966 }
7967
7968 if (CalleeID.Kind == ValID::t_GlobalName &&
7969 isOldDbgFormatIntrinsic(CalleeID.StrVal)) {
7970 if (SeenNewDbgInfoFormat) {
7971 CI->deleteValue();
7972 return error(CallLoc, "llvm.dbg intrinsic should not appear in a module "
7973 "using non-intrinsic debug info");
7974 }
7975 if (!SeenOldDbgInfoFormat)
7976 M->setNewDbgInfoFormatFlag(false);
7977 SeenOldDbgInfoFormat = true;
7978 }
7979 CI->setAttributes(PAL);
7980 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
7981 Inst = CI;
7982 return false;
7983}
7984
7985//===----------------------------------------------------------------------===//
7986// Memory Instructions.
7987//===----------------------------------------------------------------------===//
7988
7989/// parseAlloc
7990/// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
7991/// (',' 'align' i32)? (',', 'addrspace(n))?
7992int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
7993 Value *Size = nullptr;
7994 LocTy SizeLoc, TyLoc, ASLoc;
7995 MaybeAlign Alignment;
7996 unsigned AddrSpace = 0;
7997 Type *Ty = nullptr;
7998
7999 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
8000 bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
8001
8002 if (parseType(Ty, TyLoc))
8003 return true;
8004
8006 return error(TyLoc, "invalid type for alloca");
8007
8008 bool AteExtraComma = false;
8009 if (EatIfPresent(lltok::comma)) {
8010 if (Lex.getKind() == lltok::kw_align) {
8011 if (parseOptionalAlignment(Alignment))
8012 return true;
8013 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
8014 return true;
8015 } else if (Lex.getKind() == lltok::kw_addrspace) {
8016 ASLoc = Lex.getLoc();
8017 if (parseOptionalAddrSpace(AddrSpace))
8018 return true;
8019 } else if (Lex.getKind() == lltok::MetadataVar) {
8020 AteExtraComma = true;
8021 } else {
8022 if (parseTypeAndValue(Size, SizeLoc, PFS))
8023 return true;
8024 if (EatIfPresent(lltok::comma)) {
8025 if (Lex.getKind() == lltok::kw_align) {
8026 if (parseOptionalAlignment(Alignment))
8027 return true;
8028 if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
8029 return true;
8030 } else if (Lex.getKind() == lltok::kw_addrspace) {
8031 ASLoc = Lex.getLoc();
8032 if (parseOptionalAddrSpace(AddrSpace))
8033 return true;
8034 } else if (Lex.getKind() == lltok::MetadataVar) {
8035 AteExtraComma = true;
8036 }
8037 }
8038 }
8039 }
8040
8041 if (Size && !Size->getType()->isIntegerTy())
8042 return error(SizeLoc, "element count must have integer type");
8043
8044 SmallPtrSet<Type *, 4> Visited;
8045 if (!Alignment && !Ty->isSized(&Visited))
8046 return error(TyLoc, "Cannot allocate unsized type");
8047 if (!Alignment)
8048 Alignment = M->getDataLayout().getPrefTypeAlign(Ty);
8049 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment);
8050 AI->setUsedWithInAlloca(IsInAlloca);
8051 AI->setSwiftError(IsSwiftError);
8052 Inst = AI;
8053 return AteExtraComma ? InstExtraComma : InstNormal;
8054}
8055
8056/// parseLoad
8057/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
8058/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
8059/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
8060int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) {
8061 Value *Val; LocTy Loc;
8062 MaybeAlign Alignment;
8063 bool AteExtraComma = false;
8064 bool isAtomic = false;
8067
8068 if (Lex.getKind() == lltok::kw_atomic) {
8069 isAtomic = true;
8070 Lex.Lex();
8071 }
8072
8073 bool isVolatile = false;
8074 if (Lex.getKind() == lltok::kw_volatile) {
8075 isVolatile = true;
8076 Lex.Lex();
8077 }
8078
8079 Type *Ty;
8080 LocTy ExplicitTypeLoc = Lex.getLoc();
8081 if (parseType(Ty) ||
8082 parseToken(lltok::comma, "expected comma after load's type") ||
8083 parseTypeAndValue(Val, Loc, PFS) ||
8084 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
8085 parseOptionalCommaAlign(Alignment, AteExtraComma))
8086 return true;
8087
8088 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
8089 return error(Loc, "load operand must be a pointer to a first class type");
8090 if (isAtomic && !Alignment)
8091 return error(Loc, "atomic load must have explicit non-zero alignment");
8092 if (Ordering == AtomicOrdering::Release ||
8094 return error(Loc, "atomic load cannot use Release ordering");
8095
8096 SmallPtrSet<Type *, 4> Visited;
8097 if (!Alignment && !Ty->isSized(&Visited))
8098 return error(ExplicitTypeLoc, "loading unsized types is not allowed");
8099 if (!Alignment)
8100 Alignment = M->getDataLayout().getABITypeAlign(Ty);
8101 Inst = new LoadInst(Ty, Val, "", isVolatile, *Alignment, Ordering, SSID);
8102 return AteExtraComma ? InstExtraComma : InstNormal;
8103}
8104
8105/// parseStore
8106
8107/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
8108/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
8109/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
8110int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) {
8111 Value *Val, *Ptr; LocTy Loc, PtrLoc;
8112 MaybeAlign Alignment;
8113 bool AteExtraComma = false;
8114 bool isAtomic = false;
8117
8118 if (Lex.getKind() == lltok::kw_atomic) {
8119 isAtomic = true;
8120 Lex.Lex();
8121 }
8122
8123 bool isVolatile = false;
8124 if (Lex.getKind() == lltok::kw_volatile) {
8125 isVolatile = true;
8126 Lex.Lex();
8127 }
8128
8129 if (parseTypeAndValue(Val, Loc, PFS) ||
8130 parseToken(lltok::comma, "expected ',' after store operand") ||
8131 parseTypeAndValue(Ptr, PtrLoc, PFS) ||
8132 parseScopeAndOrdering(isAtomic, SSID, Ordering) ||
8133 parseOptionalCommaAlign(Alignment, AteExtraComma))
8134 return true;
8135
8136 if (!Ptr->getType()->isPointerTy())
8137 return error(PtrLoc, "store operand must be a pointer");
8138 if (!Val->getType()->isFirstClassType())
8139 return error(Loc, "store operand must be a first class value");
8140 if (isAtomic && !Alignment)
8141 return error(Loc, "atomic store must have explicit non-zero alignment");
8142 if (Ordering == AtomicOrdering::Acquire ||
8144 return error(Loc, "atomic store cannot use Acquire ordering");
8145 SmallPtrSet<Type *, 4> Visited;
8146 if (!Alignment && !Val->getType()->isSized(&Visited))
8147 return error(Loc, "storing unsized types is not allowed");
8148 if (!Alignment)
8149 Alignment = M->getDataLayout().getABITypeAlign(Val->getType());
8150
8151 Inst = new StoreInst(Val, Ptr, isVolatile, *Alignment, Ordering, SSID);
8152 return AteExtraComma ? InstExtraComma : InstNormal;
8153}
8154
8155/// parseCmpXchg
8156/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
8157/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ','
8158/// 'Align'?
8159int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
8160 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
8161 bool AteExtraComma = false;
8162 AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
8163 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
8165 bool isVolatile = false;
8166 bool isWeak = false;
8167 MaybeAlign Alignment;
8168
8169 if (EatIfPresent(lltok::kw_weak))
8170 isWeak = true;
8171
8172 if (EatIfPresent(lltok::kw_volatile))
8173 isVolatile = true;
8174
8175 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
8176 parseToken(lltok::comma, "expected ',' after cmpxchg address") ||
8177 parseTypeAndValue(Cmp, CmpLoc, PFS) ||
8178 parseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
8179 parseTypeAndValue(New, NewLoc, PFS) ||
8180 parseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
8181 parseOrdering(FailureOrdering) ||
8182 parseOptionalCommaAlign(Alignment, AteExtraComma))
8183 return true;
8184
8185 if (!AtomicCmpXchgInst::isValidSuccessOrdering(SuccessOrdering))
8186 return tokError("invalid cmpxchg success ordering");
8187 if (!AtomicCmpXchgInst::isValidFailureOrdering(FailureOrdering))
8188 return tokError("invalid cmpxchg failure ordering");
8189 if (!Ptr->getType()->isPointerTy())
8190 return error(PtrLoc, "cmpxchg operand must be a pointer");
8191 if (Cmp->getType() != New->getType())
8192 return error(NewLoc, "compare value and new value type do not match");
8193 if (!New->getType()->isFirstClassType())
8194 return error(NewLoc, "cmpxchg operand must be a first class value");
8195
8196 const Align DefaultAlignment(
8197 PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize(
8198 Cmp->getType()));
8199
8200 AtomicCmpXchgInst *CXI =
8201 new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(DefaultAlignment),
8202 SuccessOrdering, FailureOrdering, SSID);
8203 CXI->setVolatile(isVolatile);
8204 CXI->setWeak(isWeak);
8205
8206 Inst = CXI;
8207 return AteExtraComma ? InstExtraComma : InstNormal;
8208}
8209
8210/// parseAtomicRMW
8211/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
8212/// 'singlethread'? AtomicOrdering
8213int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
8214 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
8215 bool AteExtraComma = false;
8218 bool isVolatile = false;
8219 bool IsFP = false;
8221 MaybeAlign Alignment;
8222
8223 if (EatIfPresent(lltok::kw_volatile))
8224 isVolatile = true;
8225
8226 switch (Lex.getKind()) {
8227 default:
8228 return tokError("expected binary operation in atomicrmw");
8242 break;
8245 break;
8246 case lltok::kw_fadd:
8248 IsFP = true;
8249 break;
8250 case lltok::kw_fsub:
8252 IsFP = true;
8253 break;
8254 case lltok::kw_fmax:
8256 IsFP = true;
8257 break;
8258 case lltok::kw_fmin:
8260 IsFP = true;
8261 break;
8262 }
8263 Lex.Lex(); // Eat the operation.
8264
8265 if (parseTypeAndValue(Ptr, PtrLoc, PFS) ||
8266 parseToken(lltok::comma, "expected ',' after atomicrmw address") ||
8267 parseTypeAndValue(Val, ValLoc, PFS) ||
8268 parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering) ||
8269 parseOptionalCommaAlign(Alignment, AteExtraComma))
8270 return true;
8271
8272 if (Ordering == AtomicOrdering::Unordered)
8273 return tokError("atomicrmw cannot be unordered");
8274 if (!Ptr->getType()->isPointerTy())
8275 return error(PtrLoc, "atomicrmw operand must be a pointer");
8276 if (Val->getType()->isScalableTy())
8277 return error(ValLoc, "atomicrmw operand may not be scalable");
8278
8280 if (!Val->getType()->isIntegerTy() &&
8281 !Val->getType()->isFloatingPointTy() &&
8282 !Val->getType()->isPointerTy()) {
8283 return error(
8284 ValLoc,
8286 " operand must be an integer, floating point, or pointer type");
8287 }
8288 } else if (IsFP) {
8289 if (!Val->getType()->isFPOrFPVectorTy()) {
8290 return error(ValLoc, "atomicrmw " +
8292 " operand must be a floating point type");
8293 }
8294 } else {
8295 if (!Val->getType()->isIntegerTy()) {
8296 return error(ValLoc, "atomicrmw " +
8298 " operand must be an integer");
8299 }
8300 }
8301
8302 unsigned Size =
8303 PFS.getFunction().getParent()->getDataLayout().getTypeStoreSizeInBits(
8304 Val->getType());
8305 if (Size < 8 || (Size & (Size - 1)))
8306 return error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
8307 " integer");
8308 const Align DefaultAlignment(
8309 PFS.getFunction().getParent()->getDataLayout().getTypeStoreSize(
8310 Val->getType()));
8311 AtomicRMWInst *RMWI =
8312 new AtomicRMWInst(Operation, Ptr, Val,
8313 Alignment.value_or(DefaultAlignment), Ordering, SSID);
8314 RMWI->setVolatile(isVolatile);
8315 Inst = RMWI;
8316 return AteExtraComma ? InstExtraComma : InstNormal;
8317}
8318
8319/// parseFence
8320/// ::= 'fence' 'singlethread'? AtomicOrdering
8321int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) {
8324 if (parseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
8325 return true;
8326
8327 if (Ordering == AtomicOrdering::Unordered)
8328 return tokError("fence cannot be unordered");
8329 if (Ordering == AtomicOrdering::Monotonic)
8330 return tokError("fence cannot be monotonic");
8331
8332 Inst = new FenceInst(Context, Ordering, SSID);
8333 return InstNormal;
8334}
8335
8336/// parseGetElementPtr
8337/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
8338int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
8339 Value *Ptr = nullptr;
8340 Value *Val = nullptr;
8341 LocTy Loc, EltLoc;
8342
8343 bool InBounds = EatIfPresent(lltok::kw_inbounds);
8344
8345 Type *Ty = nullptr;
8346 if (parseType(Ty) ||
8347 parseToken(lltok::comma, "expected comma after getelementptr's type") ||
8348 parseTypeAndValue(Ptr, Loc, PFS))
8349 return true;
8350
8351 Type *BaseType = Ptr->getType();
8352 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
8353 if (!BasePointerType)
8354 return error(Loc, "base of getelementptr must be a pointer");
8355
8357 bool AteExtraComma = false;
8358 // GEP returns a vector of pointers if at least one of parameters is a vector.
8359 // All vector parameters should have the same vector width.
8360 ElementCount GEPWidth = BaseType->isVectorTy()
8361 ? cast<VectorType>(BaseType)->getElementCount()
8363
8364 while (EatIfPresent(lltok::comma)) {
8365 if (Lex.getKind() == lltok::MetadataVar) {
8366 AteExtraComma = true;
8367 break;
8368 }
8369 if (parseTypeAndValue(Val, EltLoc, PFS))
8370 return true;
8371 if (!Val->getType()->isIntOrIntVectorTy())
8372 return error(EltLoc, "getelementptr index must be an integer");
8373
8374 if (auto *ValVTy = dyn_cast<VectorType>(Val->getType())) {
8375 ElementCount ValNumEl = ValVTy->getElementCount();
8376 if (GEPWidth != ElementCount::getFixed(0) && GEPWidth != ValNumEl)
8377 return error(
8378 EltLoc,
8379 "getelementptr vector index has a wrong number of elements");
8380 GEPWidth = ValNumEl;
8381 }
8382 Indices.push_back(Val);
8383 }
8384
8385 SmallPtrSet<Type*, 4> Visited;
8386 if (!Indices.empty() && !Ty->isSized(&Visited))
8387 return error(Loc, "base element of getelementptr must be sized");
8388
8389 auto *STy = dyn_cast<StructType>(Ty);
8390 if (STy && STy->containsScalableVectorType())
8391 return error(Loc, "getelementptr cannot target structure that contains "
8392 "scalable vector type");
8393
8394 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
8395 return error(Loc, "invalid getelementptr indices");
8396 Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
8397 if (InBounds)
8398 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
8399 return AteExtraComma ? InstExtraComma : InstNormal;
8400}
8401
8402/// parseExtractValue
8403/// ::= 'extractvalue' TypeAndValue (',' uint32)+
8404int LLParser::parseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
8405 Value *Val; LocTy Loc;
8407 bool AteExtraComma;
8408 if (parseTypeAndValue(Val, Loc, PFS) ||
8409 parseIndexList(Indices, AteExtraComma))
8410 return true;
8411
8412 if (!Val->getType()->isAggregateType())
8413 return error(Loc, "extractvalue operand must be aggregate type");
8414
8415 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
8416 return error(Loc, "invalid indices for extractvalue");
8417 Inst = ExtractValueInst::Create(Val, Indices);
8418 return AteExtraComma ? InstExtraComma : InstNormal;
8419}
8420
8421/// parseInsertValue
8422/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
8423int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
8424 Value *Val0, *Val1; LocTy Loc0, Loc1;
8426 bool AteExtraComma;
8427 if (parseTypeAndValue(Val0, Loc0, PFS) ||
8428 parseToken(lltok::comma, "expected comma after insertvalue operand") ||
8429 parseTypeAndValue(Val1, Loc1, PFS) ||
8430 parseIndexList(Indices, AteExtraComma))
8431 return true;
8432
8433 if (!Val0->getType()->isAggregateType())
8434 return error(Loc0, "insertvalue operand must be aggregate type");
8435
8436 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
8437 if (!IndexedType)
8438 return error(Loc0, "invalid indices for insertvalue");
8439 if (IndexedType != Val1->getType())
8440 return error(Loc1, "insertvalue operand and field disagree in type: '" +
8441 getTypeString(Val1->getType()) + "' instead of '" +
8442 getTypeString(IndexedType) + "'");
8443 Inst = InsertValueInst::Create(Val0, Val1, Indices);
8444 return AteExtraComma ? InstExtraComma : InstNormal;
8445}
8446
8447//===----------------------------------------------------------------------===//
8448// Embedded metadata.
8449//===----------------------------------------------------------------------===//
8450
8451/// parseMDNodeVector
8452/// ::= { Element (',' Element)* }
8453/// Element
8454/// ::= 'null' | Metadata
8455bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
8456 if (parseToken(lltok::lbrace, "expected '{' here"))
8457 return true;
8458
8459 // Check for an empty list.
8460 if (EatIfPresent(lltok::rbrace))
8461 return false;
8462
8463 do {
8464 if (EatIfPresent(lltok::kw_null)) {
8465 Elts.push_back(nullptr);
8466 continue;
8467 }
8468
8469 Metadata *MD;
8470 if (parseMetadata(MD, nullptr))
8471 return true;
8472 Elts.push_back(MD);
8473 } while (EatIfPresent(lltok::comma));
8474
8475 return parseToken(lltok::rbrace, "expected end of metadata node");
8476}
8477
8478//===----------------------------------------------------------------------===//
8479// Use-list order directives.
8480//===----------------------------------------------------------------------===//
8481bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
8482 SMLoc Loc) {
8483 if (V->use_empty())
8484 return error(Loc, "value has no uses");
8485
8486 unsigned NumUses = 0;
8488 for (const Use &U : V->uses()) {
8489 if (++NumUses > Indexes.size())
8490 break;
8491 Order[&U] = Indexes[NumUses - 1];
8492 }
8493 if (NumUses < 2)
8494 return error(Loc, "value only has one use");
8495 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
8496 return error(Loc,
8497 "wrong number of indexes, expected " + Twine(V->getNumUses()));
8498
8499 V->sortUseList([&](const Use &L, const Use &R) {
8500 return Order.lookup(&L) < Order.lookup(&R);
8501 });
8502 return false;
8503}
8504
8505/// parseUseListOrderIndexes
8506/// ::= '{' uint32 (',' uint32)+ '}'
8507bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
8508 SMLoc Loc = Lex.getLoc();
8509 if (parseToken(lltok::lbrace, "expected '{' here"))
8510 return true;
8511 if (Lex.getKind() == lltok::rbrace)
8512 return Lex.Error("expected non-empty list of uselistorder indexes");
8513
8514 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
8515 // indexes should be distinct numbers in the range [0, size-1], and should
8516 // not be in order.
8517 unsigned Offset = 0;
8518 unsigned Max = 0;
8519 bool IsOrdered = true;
8520 assert(Indexes.empty() && "Expected empty order vector");
8521 do {
8522 unsigned Index;
8523 if (parseUInt32(Index))
8524 return true;
8525
8526 // Update consistency checks.
8527 Offset += Index - Indexes.size();
8528 Max = std::max(Max, Index);
8529 IsOrdered &= Index == Indexes.size();
8530
8531 Indexes.push_back(Index);
8532 } while (EatIfPresent(lltok::comma));
8533
8534 if (parseToken(lltok::rbrace, "expected '}' here"))
8535 return true;
8536
8537 if (Indexes.size() < 2)
8538 return error(Loc, "expected >= 2 uselistorder indexes");
8539 if (Offset != 0 || Max >= Indexes.size())
8540 return error(Loc,
8541 "expected distinct uselistorder indexes in range [0, size)");
8542 if (IsOrdered)
8543 return error(Loc, "expected uselistorder indexes to change the order");
8544
8545 return false;
8546}
8547
8548/// parseUseListOrder
8549/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
8550bool LLParser::parseUseListOrder(PerFunctionState *PFS) {
8551 SMLoc Loc = Lex.getLoc();
8552 if (parseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
8553 return true;
8554
8555 Value *V;
8557 if (parseTypeAndValue(V, PFS) ||
8558 parseToken(lltok::comma, "expected comma in uselistorder directive") ||
8559 parseUseListOrderIndexes(Indexes))
8560 return true;
8561
8562 return sortUseListOrder(V, Indexes, Loc);
8563}
8564
8565/// parseUseListOrderBB
8566/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
8567bool LLParser::parseUseListOrderBB() {
8569 SMLoc Loc = Lex.getLoc();
8570 Lex.Lex();
8571
8572 ValID Fn, Label;
8574 if (parseValID(Fn, /*PFS=*/nullptr) ||
8575 parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
8576 parseValID(Label, /*PFS=*/nullptr) ||
8577 parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
8578 parseUseListOrderIndexes(Indexes))
8579 return true;
8580
8581 // Check the function.
8582 GlobalValue *GV;
8583 if (Fn.Kind == ValID::t_GlobalName)
8584 GV = M->getNamedValue(Fn.StrVal);
8585 else if (Fn.Kind == ValID::t_GlobalID)
8586 GV = NumberedVals.get(Fn.UIntVal);
8587 else
8588 return error(Fn.Loc, "expected function name in uselistorder_bb");
8589 if (!GV)
8590 return error(Fn.Loc,
8591 "invalid function forward reference in uselistorder_bb");
8592 auto *F = dyn_cast<Function>(GV);
8593 if (!F)
8594 return error(Fn.Loc, "expected function name in uselistorder_bb");
8595 if (F->isDeclaration())
8596 return error(Fn.Loc, "invalid declaration in uselistorder_bb");
8597
8598 // Check the basic block.
8599 if (Label.Kind == ValID::t_LocalID)
8600 return error(Label.Loc, "invalid numeric label in uselistorder_bb");
8601 if (Label.Kind != ValID::t_LocalName)
8602 return error(Label.Loc, "expected basic block name in uselistorder_bb");
8603 Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
8604 if (!V)
8605 return error(Label.Loc, "invalid basic block in uselistorder_bb");
8606 if (!isa<BasicBlock>(V))
8607 return error(Label.Loc, "expected basic block in uselistorder_bb");
8608
8609 return sortUseListOrder(V, Indexes, Loc);
8610}
8611
8612/// ModuleEntry
8613/// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
8614/// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
8615bool LLParser::parseModuleEntry(unsigned ID) {
8617 Lex.Lex();
8618
8619 std::string Path;
8620 if (parseToken(lltok::colon, "expected ':' here") ||
8621 parseToken(lltok::lparen, "expected '(' here") ||
8622 parseToken(lltok::kw_path, "expected 'path' here") ||
8623 parseToken(lltok::colon, "expected ':' here") ||
8624 parseStringConstant(Path) ||
8625 parseToken(lltok::comma, "expected ',' here") ||
8626 parseToken(lltok::kw_hash, "expected 'hash' here") ||
8627 parseToken(lltok::colon, "expected ':' here") ||
8628 parseToken(lltok::lparen, "expected '(' here"))
8629 return true;
8630
8631 ModuleHash Hash;
8632 if (parseUInt32(Hash[0]) || parseToken(lltok::comma, "expected ',' here") ||
8633 parseUInt32(Hash[1]) || parseToken(lltok::comma, "expected ',' here") ||
8634 parseUInt32(Hash[2]) || parseToken(lltok::comma, "expected ',' here") ||
8635 parseUInt32(Hash[3]) || parseToken(lltok::comma, "expected ',' here") ||
8636 parseUInt32(Hash[4]))
8637 return true;
8638
8639 if (parseToken(lltok::rparen, "expected ')' here") ||
8640 parseToken(lltok::rparen, "expected ')' here"))
8641 return true;
8642
8643 auto ModuleEntry = Index->addModule(Path, Hash);
8644 ModuleIdMap[ID] = ModuleEntry->first();
8645
8646 return false;
8647}
8648
8649/// TypeIdEntry
8650/// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
8651bool LLParser::parseTypeIdEntry(unsigned ID) {
8653 Lex.Lex();
8654
8655 std::string Name;
8656 if (parseToken(lltok::colon, "expected ':' here") ||
8657 parseToken(lltok::lparen, "expected '(' here") ||
8658 parseToken(lltok::kw_name, "expected 'name' here") ||
8659 parseToken(lltok::colon, "expected ':' here") ||
8660 parseStringConstant(Name))
8661 return true;
8662
8663 TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(Name);
8664 if (parseToken(lltok::comma, "expected ',' here") ||
8665 parseTypeIdSummary(TIS) || parseToken(lltok::rparen, "expected ')' here"))
8666 return true;
8667
8668 // Check if this ID was forward referenced, and if so, update the
8669 // corresponding GUIDs.
8670 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
8671 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
8672 for (auto TIDRef : FwdRefTIDs->second) {
8673 assert(!*TIDRef.first &&
8674 "Forward referenced type id GUID expected to be 0");
8675 *TIDRef.first = GlobalValue::getGUID(Name);
8676 }
8677 ForwardRefTypeIds.erase(FwdRefTIDs);
8678 }
8679
8680 return false;
8681}
8682
8683/// TypeIdSummary
8684/// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
8685bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) {
8686 if (parseToken(lltok::kw_summary, "expected 'summary' here") ||
8687 parseToken(lltok::colon, "expected ':' here") ||
8688 parseToken(lltok::lparen, "expected '(' here") ||
8689 parseTypeTestResolution(TIS.TTRes))
8690 return true;
8691
8692 if (EatIfPresent(lltok::comma)) {
8693 // Expect optional wpdResolutions field
8694 if (parseOptionalWpdResolutions(TIS.WPDRes))
8695 return true;
8696 }
8697
8698 if (parseToken(lltok::rparen, "expected ')' here"))
8699 return true;
8700
8701 return false;
8702}
8703
8705 ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
8706
8707/// TypeIdCompatibleVtableEntry
8708/// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
8709/// TypeIdCompatibleVtableInfo
8710/// ')'
8711bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
8713 Lex.Lex();
8714
8715 std::string Name;
8716 if (parseToken(lltok::colon, "expected ':' here") ||
8717 parseToken(lltok::lparen, "expected '(' here") ||
8718 parseToken(lltok::kw_name, "expected 'name' here") ||
8719 parseToken(lltok::colon, "expected ':' here") ||
8720 parseStringConstant(Name))
8721 return true;
8722
8724 Index->getOrInsertTypeIdCompatibleVtableSummary(Name);
8725 if (parseToken(lltok::comma, "expected ',' here") ||
8726 parseToken(lltok::kw_summary, "expected 'summary' here") ||
8727 parseToken(lltok::colon, "expected ':' here") ||
8728 parseToken(lltok::lparen, "expected '(' here"))
8729 return true;
8730
8731 IdToIndexMapType IdToIndexMap;
8732 // parse each call edge
8733 do {
8735 if (parseToken(lltok::lparen, "expected '(' here") ||
8736 parseToken(lltok::kw_offset, "expected 'offset' here") ||
8737 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
8738 parseToken(lltok::comma, "expected ',' here"))
8739 return true;
8740
8741 LocTy Loc = Lex.getLoc();
8742 unsigned GVId;
8743 ValueInfo VI;
8744 if (parseGVReference(VI, GVId))
8745 return true;
8746
8747 // Keep track of the TypeIdCompatibleVtableInfo array index needing a
8748 // forward reference. We will save the location of the ValueInfo needing an
8749 // update, but can only do so once the std::vector is finalized.
8750 if (VI == EmptyVI)
8751 IdToIndexMap[GVId].push_back(std::make_pair(TI.size(), Loc));
8752 TI.push_back({Offset, VI});
8753
8754 if (parseToken(lltok::rparen, "expected ')' in call"))
8755 return true;
8756 } while (EatIfPresent(lltok::comma));
8757
8758 // Now that the TI vector is finalized, it is safe to save the locations
8759 // of any forward GV references that need updating later.
8760 for (auto I : IdToIndexMap) {
8761 auto &Infos = ForwardRefValueInfos[I.first];
8762 for (auto P : I.second) {
8763 assert(TI[P.first].VTableVI == EmptyVI &&
8764 "Forward referenced ValueInfo expected to be empty");
8765 Infos.emplace_back(&TI[P.first].VTableVI, P.second);
8766 }
8767 }
8768
8769 if (parseToken(lltok::rparen, "expected ')' here") ||
8770 parseToken(lltok::rparen, "expected ')' here"))
8771 return true;
8772
8773 // Check if this ID was forward referenced, and if so, update the
8774 // corresponding GUIDs.
8775 auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
8776 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
8777 for (auto TIDRef : FwdRefTIDs->second) {
8778 assert(!*TIDRef.first &&
8779 "Forward referenced type id GUID expected to be 0");
8780 *TIDRef.first = GlobalValue::getGUID(Name);
8781 }
8782 ForwardRefTypeIds.erase(FwdRefTIDs);
8783 }
8784
8785 return false;
8786}
8787
8788/// TypeTestResolution
8789/// ::= 'typeTestRes' ':' '(' 'kind' ':'
8790/// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
8791/// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
8792/// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
8793/// [',' 'inlinesBits' ':' UInt64]? ')'
8794bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) {
8795 if (parseToken(lltok::kw_typeTestRes, "expected 'typeTestRes' here") ||
8796 parseToken(lltok::colon, "expected ':' here") ||
8797 parseToken(lltok::lparen, "expected '(' here") ||
8798 parseToken(lltok::kw_kind, "expected 'kind' here") ||
8799 parseToken(lltok::colon, "expected ':' here"))
8800 return true;
8801
8802 switch (Lex.getKind()) {
8803 case lltok::kw_unknown:
8805 break;
8806 case lltok::kw_unsat:
8808 break;
8811 break;
8812 case lltok::kw_inline:
8814 break;
8815 case lltok::kw_single:
8817 break;
8818 case lltok::kw_allOnes:
8820 break;
8821 default:
8822 return error(Lex.getLoc(), "unexpected TypeTestResolution kind");
8823 }
8824 Lex.Lex();
8825
8826 if (parseToken(lltok::comma, "expected ',' here") ||
8827 parseToken(lltok::kw_sizeM1BitWidth, "expected 'sizeM1BitWidth' here") ||
8828 parseToken(lltok::colon, "expected ':' here") ||
8829 parseUInt32(TTRes.SizeM1BitWidth))
8830 return true;
8831
8832 // parse optional fields
8833 while (EatIfPresent(lltok::comma)) {
8834 switch (Lex.getKind()) {
8836 Lex.Lex();
8837 if (parseToken(lltok::colon, "expected ':'") ||
8838 parseUInt64(TTRes.AlignLog2))
8839 return true;
8840 break;
8841 case lltok::kw_sizeM1:
8842 Lex.Lex();
8843 if (parseToken(lltok::colon, "expected ':'") || parseUInt64(TTRes.SizeM1))
8844 return true;
8845 break;
8846 case lltok::kw_bitMask: {
8847 unsigned Val;
8848 Lex.Lex();
8849 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(Val))
8850 return true;
8851 assert(Val <= 0xff);
8852 TTRes.BitMask = (uint8_t)Val;
8853 break;
8854 }
8856 Lex.Lex();
8857 if (parseToken(lltok::colon, "expected ':'") ||
8858 parseUInt64(TTRes.InlineBits))
8859 return true;
8860 break;
8861 default:
8862 return error(Lex.getLoc(), "expected optional TypeTestResolution field");
8863 }
8864 }
8865
8866 if (parseToken(lltok::rparen, "expected ')' here"))
8867 return true;
8868
8869 return false;
8870}
8871
8872/// OptionalWpdResolutions
8873/// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
8874/// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
8875bool LLParser::parseOptionalWpdResolutions(
8876 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
8877 if (parseToken(lltok::kw_wpdResolutions, "expected 'wpdResolutions' here") ||
8878 parseToken(lltok::colon, "expected ':' here") ||
8879 parseToken(lltok::lparen, "expected '(' here"))
8880 return true;
8881
8882 do {
8885 if (parseToken(lltok::lparen, "expected '(' here") ||
8886 parseToken(lltok::kw_offset, "expected 'offset' here") ||
8887 parseToken(lltok::colon, "expected ':' here") || parseUInt64(Offset) ||
8888 parseToken(lltok::comma, "expected ',' here") || parseWpdRes(WPDRes) ||
8889 parseToken(lltok::rparen, "expected ')' here"))
8890 return true;
8891 WPDResMap[Offset] = WPDRes;
8892 } while (EatIfPresent(lltok::comma));
8893
8894 if (parseToken(lltok::rparen, "expected ')' here"))
8895 return true;
8896
8897 return false;
8898}
8899
8900/// WpdRes
8901/// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
8902/// [',' OptionalResByArg]? ')'
8903/// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
8904/// ',' 'singleImplName' ':' STRINGCONSTANT ','
8905/// [',' OptionalResByArg]? ')'
8906/// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
8907/// [',' OptionalResByArg]? ')'
8908bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) {
8909 if (parseToken(lltok::kw_wpdRes, "expected 'wpdRes' here") ||
8910 parseToken(lltok::colon, "expected ':' here") ||
8911 parseToken(lltok::lparen, "expected '(' here") ||
8912 parseToken(lltok::kw_kind, "expected 'kind' here") ||
8913 parseToken(lltok::colon, "expected ':' here"))
8914 return true;
8915
8916 switch (Lex.getKind()) {
8917 case lltok::kw_indir:
8919 break;
8922 break;
8925 break;
8926 default:
8927 return error(Lex.getLoc(), "unexpected WholeProgramDevirtResolution kind");
8928 }
8929 Lex.Lex();
8930
8931 // parse optional fields
8932 while (EatIfPresent(lltok::comma)) {
8933 switch (Lex.getKind()) {
8935 Lex.Lex();
8936 if (parseToken(lltok::colon, "expected ':' here") ||
8937 parseStringConstant(WPDRes.SingleImplName))
8938 return true;
8939 break;
8940 case lltok::kw_resByArg:
8941 if (parseOptionalResByArg(WPDRes.ResByArg))
8942 return true;
8943 break;
8944 default:
8945 return error(Lex.getLoc(),
8946 "expected optional WholeProgramDevirtResolution field");
8947 }
8948 }
8949
8950 if (parseToken(lltok::rparen, "expected ')' here"))
8951 return true;
8952
8953 return false;
8954}
8955
8956/// OptionalResByArg
8957/// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
8958/// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
8959/// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
8960/// 'virtualConstProp' )
8961/// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
8962/// [',' 'bit' ':' UInt32]? ')'
8963bool LLParser::parseOptionalResByArg(
8964 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
8965 &ResByArg) {
8966 if (parseToken(lltok::kw_resByArg, "expected 'resByArg' here") ||
8967 parseToken(lltok::colon, "expected ':' here") ||
8968 parseToken(lltok::lparen, "expected '(' here"))
8969 return true;
8970
8971 do {
8972 std::vector<uint64_t> Args;
8973 if (parseArgs(Args) || parseToken(lltok::comma, "expected ',' here") ||
8974 parseToken(lltok::kw_byArg, "expected 'byArg here") ||
8975 parseToken(lltok::colon, "expected ':' here") ||
8976 parseToken(lltok::lparen, "expected '(' here") ||
8977 parseToken(lltok::kw_kind, "expected 'kind' here") ||
8978 parseToken(lltok::colon, "expected ':' here"))
8979 return true;
8980
8982 switch (Lex.getKind()) {
8983 case lltok::kw_indir:
8985 break;
8988 break;
8991 break;
8994 break;
8995 default:
8996 return error(Lex.getLoc(),
8997 "unexpected WholeProgramDevirtResolution::ByArg kind");
8998 }
8999 Lex.Lex();
9000
9001 // parse optional fields
9002 while (EatIfPresent(lltok::comma)) {
9003 switch (Lex.getKind()) {
9004 case lltok::kw_info:
9005 Lex.Lex();
9006 if (parseToken(lltok::colon, "expected ':' here") ||
9007 parseUInt64(ByArg.Info))
9008 return true;
9009 break;
9010 case lltok::kw_byte:
9011 Lex.Lex();
9012 if (parseToken(lltok::colon, "expected ':' here") ||
9013 parseUInt32(ByArg.Byte))
9014 return true;
9015 break;
9016 case lltok::kw_bit:
9017 Lex.Lex();
9018 if (parseToken(lltok::colon, "expected ':' here") ||
9019 parseUInt32(ByArg.Bit))
9020 return true;
9021 break;
9022 default:
9023 return error(Lex.getLoc(),
9024 "expected optional whole program devirt field");
9025 }
9026 }
9027
9028 if (parseToken(lltok::rparen, "expected ')' here"))
9029 return true;
9030
9031 ResByArg[Args] = ByArg;
9032 } while (EatIfPresent(lltok::comma));
9033
9034 if (parseToken(lltok::rparen, "expected ')' here"))
9035 return true;
9036
9037 return false;
9038}
9039
9040/// OptionalResByArg
9041/// ::= 'args' ':' '(' UInt64[, UInt64]* ')'
9042bool LLParser::parseArgs(std::vector<uint64_t> &Args) {
9043 if (parseToken(lltok::kw_args, "expected 'args' here") ||
9044 parseToken(lltok::colon, "expected ':' here") ||
9045 parseToken(lltok::lparen, "expected '(' here"))
9046 return true;
9047
9048 do {
9049 uint64_t Val;
9050 if (parseUInt64(Val))
9051 return true;
9052 Args.push_back(Val);
9053 } while (EatIfPresent(lltok::comma));
9054
9055 if (parseToken(lltok::rparen, "expected ')' here"))
9056 return true;
9057
9058 return false;
9059}
9060
9061static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8;
9062
9063static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
9064 bool ReadOnly = Fwd->isReadOnly();
9065 bool WriteOnly = Fwd->isWriteOnly();
9066 assert(!(ReadOnly && WriteOnly));
9067 *Fwd = Resolved;
9068 if (ReadOnly)
9069 Fwd->setReadOnly();
9070 if (WriteOnly)
9071 Fwd->setWriteOnly();
9072}
9073
9074/// Stores the given Name/GUID and associated summary into the Index.
9075/// Also updates any forward references to the associated entry ID.
9076bool LLParser::addGlobalValueToIndex(
9077 std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
9078 unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) {
9079 // First create the ValueInfo utilizing the Name or GUID.
9080 ValueInfo VI;
9081 if (GUID != 0) {
9082 assert(Name.empty());
9083 VI = Index->getOrInsertValueInfo(GUID);
9084 } else {
9085 assert(!Name.empty());
9086 if (M) {
9087 auto *GV = M->getNamedValue(Name);
9088 if (!GV)
9089 return error(Loc, "Reference to undefined global \"" + Name + "\"");
9090
9091 VI = Index->getOrInsertValueInfo(GV);
9092 } else {
9093 assert(
9094 (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
9095 "Need a source_filename to compute GUID for local");
9096 GUID = GlobalValue::getGUID(
9097 GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
9098 VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
9099 }
9100 }
9101
9102 // Resolve forward references from calls/refs
9103 auto FwdRefVIs = ForwardRefValueInfos.find(ID);
9104 if (FwdRefVIs != ForwardRefValueInfos.end()) {
9105 for (auto VIRef : FwdRefVIs->second) {
9106 assert(VIRef.first->getRef() == FwdVIRef &&
9107 "Forward referenced ValueInfo expected to be empty");
9108 resolveFwdRef(VIRef.first, VI);
9109 }
9110 ForwardRefValueInfos.erase(FwdRefVIs);
9111 }
9112
9113 // Resolve forward references from aliases
9114 auto FwdRefAliasees = ForwardRefAliasees.find(ID);
9115 if (FwdRefAliasees != ForwardRefAliasees.end()) {
9116 for (auto AliaseeRef : FwdRefAliasees->second) {
9117 assert(!AliaseeRef.first->hasAliasee() &&
9118 "Forward referencing alias already has aliasee");
9119 assert(Summary && "Aliasee must be a definition");
9120 AliaseeRef.first->setAliasee(VI, Summary.get());
9121 }
9122 ForwardRefAliasees.erase(FwdRefAliasees);
9123 }
9124
9125 // Add the summary if one was provided.
9126 if (Summary)
9127 Index->addGlobalValueSummary(VI, std::move(Summary));
9128
9129 // Save the associated ValueInfo for use in later references by ID.
9130 if (ID == NumberedValueInfos.size())
9131 NumberedValueInfos.push_back(VI);
9132 else {
9133 // Handle non-continuous numbers (to make test simplification easier).
9134 if (ID > NumberedValueInfos.size())
9135 NumberedValueInfos.resize(ID + 1);
9136 NumberedValueInfos[ID] = VI;
9137 }
9138
9139 return false;
9140}
9141
9142/// parseSummaryIndexFlags
9143/// ::= 'flags' ':' UInt64
9144bool LLParser::parseSummaryIndexFlags() {
9145 assert(Lex.getKind() == lltok::kw_flags);
9146 Lex.Lex();
9147
9148 if (parseToken(lltok::colon, "expected ':' here"))
9149 return true;
9151 if (parseUInt64(Flags))
9152 return true;
9153 if (Index)
9154 Index->setFlags(Flags);
9155 return false;
9156}
9157
9158/// parseBlockCount
9159/// ::= 'blockcount' ':' UInt64
9160bool LLParser::parseBlockCount() {
9162 Lex.Lex();
9163
9164 if (parseToken(lltok::colon, "expected ':' here"))
9165 return true;
9166 uint64_t BlockCount;
9167 if (parseUInt64(BlockCount))
9168 return true;
9169 if (Index)
9170 Index->setBlockCount(BlockCount);
9171 return false;
9172}
9173
9174/// parseGVEntry
9175/// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
9176/// [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
9177/// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
9178bool LLParser::parseGVEntry(unsigned ID) {
9179 assert(Lex.getKind() == lltok::kw_gv);
9180 Lex.Lex();
9181
9182 if (parseToken(lltok::colon, "expected ':' here") ||
9183 parseToken(lltok::lparen, "expected '(' here"))
9184 return true;
9185
9186 LocTy Loc = Lex.getLoc();
9187 std::string Name;
9188 GlobalValue::GUID GUID = 0;
9189 switch (Lex.getKind()) {
9190 case lltok::kw_name:
9191 Lex.Lex();
9192 if (parseToken(lltok::colon, "expected ':' here") ||
9193 parseStringConstant(Name))
9194 return true;
9195 // Can't create GUID/ValueInfo until we have the linkage.
9196 break;
9197 case lltok::kw_guid:
9198 Lex.Lex();
9199 if (parseToken(lltok::colon, "expected ':' here") || parseUInt64(GUID))
9200 return true;
9201 break;
9202 default:
9203 return error(Lex.getLoc(), "expected name or guid tag");
9204 }
9205
9206 if (!EatIfPresent(lltok::comma)) {
9207 // No summaries. Wrap up.
9208 if (parseToken(lltok::rparen, "expected ')' here"))
9209 return true;
9210 // This was created for a call to an external or indirect target.
9211 // A GUID with no summary came from a VALUE_GUID record, dummy GUID
9212 // created for indirect calls with VP. A Name with no GUID came from
9213 // an external definition. We pass ExternalLinkage since that is only
9214 // used when the GUID must be computed from Name, and in that case
9215 // the symbol must have external linkage.
9216 return addGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
9217 nullptr, Loc);
9218 }
9219
9220 // Have a list of summaries
9221 if (parseToken(lltok::kw_summaries, "expected 'summaries' here") ||
9222 parseToken(lltok::colon, "expected ':' here") ||
9223 parseToken(lltok::lparen, "expected '(' here"))
9224 return true;
9225 do {
9226 switch (Lex.getKind()) {
9227 case lltok::kw_function:
9228 if (parseFunctionSummary(Name, GUID, ID))
9229 return true;
9230 break;
9231 case lltok::kw_variable:
9232 if (parseVariableSummary(Name, GUID, ID))
9233 return true;
9234 break;
9235 case lltok::kw_alias:
9236 if (parseAliasSummary(Name, GUID, ID))
9237 return true;
9238 break;
9239 default:
9240 return error(Lex.getLoc(), "expected summary type");
9241 }
9242 } while (EatIfPresent(lltok::comma));
9243
9244 if (parseToken(lltok::rparen, "expected ')' here") ||
9245 parseToken(lltok::rparen, "expected ')' here"))
9246 return true;
9247
9248 return false;
9249}
9250
9251/// FunctionSummary
9252/// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
9253/// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
9254/// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]?
9255/// [',' OptionalRefs]? ')'
9256bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
9257 unsigned ID) {
9258 LocTy Loc = Lex.getLoc();
9260 Lex.Lex();
9261
9262 StringRef ModulePath;
9265 /*NotEligibleToImport=*/false,
9266 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
9268 unsigned InstCount;
9269 std::vector<FunctionSummary::EdgeTy> Calls;
9270 FunctionSummary::TypeIdInfo TypeIdInfo;
9271 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
9272 std::vector<ValueInfo> Refs;
9273 std::vector<CallsiteInfo> Callsites;
9274 std::vector<AllocInfo> Allocs;
9275 // Default is all-zeros (conservative values).
9276 FunctionSummary::FFlags FFlags = {};
9277 if (parseToken(lltok::colon, "expected ':' here") ||
9278 parseToken(lltok::lparen, "expected '(' here") ||
9279 parseModuleReference(ModulePath) ||
9280 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
9281 parseToken(lltok::comma, "expected ',' here") ||
9282 parseToken(lltok::kw_insts, "expected 'insts' here") ||
9283 parseToken(lltok::colon, "expected ':' here") || parseUInt32(InstCount))
9284 return true;
9285
9286 // parse optional fields
9287 while (EatIfPresent(lltok::comma)) {
9288 switch (Lex.getKind()) {
9290 if (parseOptionalFFlags(FFlags))
9291 return true;
9292 break;
9293 case lltok::kw_calls:
9294 if (parseOptionalCalls(Calls))
9295 return true;
9296 break;
9298 if (parseOptionalTypeIdInfo(TypeIdInfo))
9299 return true;
9300 break;
9301 case lltok::kw_refs:
9302 if (parseOptionalRefs(Refs))
9303 return true;
9304 break;
9305 case lltok::kw_params:
9306 if (parseOptionalParamAccesses(ParamAccesses))
9307 return true;
9308 break;
9309 case lltok::kw_allocs:
9310 if (parseOptionalAllocs(Allocs))
9311 return true;
9312 break;
9314 if (parseOptionalCallsites(Callsites))
9315 return true;
9316 break;
9317 default:
9318 return error(Lex.getLoc(), "expected optional function summary field");
9319 }
9320 }
9321
9322 if (parseToken(lltok::rparen, "expected ')' here"))
9323 return true;
9324
9325 auto FS = std::make_unique<FunctionSummary>(
9326 GVFlags, InstCount, FFlags, /*EntryCount=*/0, std::move(Refs),
9327 std::move(Calls), std::move(TypeIdInfo.TypeTests),
9328 std::move(TypeIdInfo.TypeTestAssumeVCalls),
9329 std::move(TypeIdInfo.TypeCheckedLoadVCalls),
9330 std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
9331 std::move(TypeIdInfo.TypeCheckedLoadConstVCalls),
9332 std::move(ParamAccesses), std::move(Callsites), std::move(Allocs));
9333
9334 FS->setModulePath(ModulePath);
9335
9336 return addGlobalValueToIndex(Name, GUID,
9338 std::move(FS), Loc);
9339}
9340
9341/// VariableSummary
9342/// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
9343/// [',' OptionalRefs]? ')'
9344bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
9345 unsigned ID) {
9346 LocTy Loc = Lex.getLoc();
9348 Lex.Lex();
9349
9350 StringRef ModulePath;
9353 /*NotEligibleToImport=*/false,
9354 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
9356 GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
9357 /* WriteOnly */ false,
9358 /* Constant */ false,
9360 std::vector<ValueInfo> Refs;
9361 VTableFuncList VTableFuncs;
9362 if (parseToken(lltok::colon, "expected ':' here") ||
9363 parseToken(lltok::lparen, "expected '(' here") ||
9364 parseModuleReference(ModulePath) ||
9365 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
9366 parseToken(lltok::comma, "expected ',' here") ||
9367 parseGVarFlags(GVarFlags))
9368 return true;
9369
9370 // parse optional fields
9371 while (EatIfPresent(lltok::comma)) {
9372 switch (Lex.getKind()) {
9374 if (parseOptionalVTableFuncs(VTableFuncs))
9375 return true;
9376 break;
9377 case lltok::kw_refs:
9378 if (parseOptionalRefs(Refs))
9379 return true;
9380 break;
9381 default:
9382 return error(Lex.getLoc(), "expected optional variable summary field");
9383 }
9384 }
9385
9386 if (parseToken(lltok::rparen, "expected ')' here"))
9387 return true;
9388
9389 auto GS =
9390 std::make_unique<GlobalVarSummary>(GVFlags, GVarFlags, std::move(Refs));
9391
9392 GS->setModulePath(ModulePath);
9393 GS->setVTableFuncs(std::move(VTableFuncs));
9394
9395 return addGlobalValueToIndex(Name, GUID,
9397 std::move(GS), Loc);
9398}
9399
9400/// AliasSummary
9401/// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
9402/// 'aliasee' ':' GVReference ')'
9403bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
9404 unsigned ID) {
9405 assert(Lex.getKind() == lltok::kw_alias);
9406 LocTy Loc = Lex.getLoc();
9407 Lex.Lex();
9408
9409 StringRef ModulePath;
9412 /*NotEligibleToImport=*/false,
9413 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
9415 if (parseToken(lltok::colon, "expected ':' here") ||
9416 parseToken(lltok::lparen, "expected '(' here") ||
9417 parseModuleReference(ModulePath) ||
9418 parseToken(lltok::comma, "expected ',' here") || parseGVFlags(GVFlags) ||
9419 parseToken(lltok::comma, "expected ',' here") ||
9420 parseToken(lltok::kw_aliasee, "expected 'aliasee' here") ||
9421 parseToken(lltok::colon, "expected ':' here"))
9422 return true;
9423
9424 ValueInfo AliaseeVI;
9425 unsigned GVId;
9426 if (parseGVReference(AliaseeVI, GVId))
9427 return true;
9428
9429 if (parseToken(lltok::rparen, "expected ')' here"))
9430 return true;
9431
9432 auto AS = std::make_unique<AliasSummary>(GVFlags);
9433
9434 AS->setModulePath(ModulePath);
9435
9436 // Record forward reference if the aliasee is not parsed yet.
9437 if (AliaseeVI.getRef() == FwdVIRef) {
9438 ForwardRefAliasees[GVId].emplace_back(AS.get(), Loc);
9439 } else {
9440 auto Summary = Index->findSummaryInModule(AliaseeVI, ModulePath);
9441 assert(Summary && "Aliasee must be a definition");
9442 AS->setAliasee(AliaseeVI, Summary);
9443 }
9444
9445 return addGlobalValueToIndex(Name, GUID,
9447 std::move(AS), Loc);
9448}
9449
9450/// Flag
9451/// ::= [0|1]
9452bool LLParser::parseFlag(unsigned &Val) {
9453 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
9454 return tokError("expected integer");
9455 Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
9456 Lex.Lex();
9457 return false;
9458}
9459
9460/// OptionalFFlags
9461/// := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
9462/// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
9463/// [',' 'returnDoesNotAlias' ':' Flag]? ')'
9464/// [',' 'noInline' ':' Flag]? ')'
9465/// [',' 'alwaysInline' ':' Flag]? ')'
9466/// [',' 'noUnwind' ':' Flag]? ')'
9467/// [',' 'mayThrow' ':' Flag]? ')'
9468/// [',' 'hasUnknownCall' ':' Flag]? ')'
9469/// [',' 'mustBeUnreachable' ':' Flag]? ')'
9470
9471bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
9473 Lex.Lex();
9474
9475 if (parseToken(lltok::colon, "expected ':' in funcFlags") ||
9476 parseToken(lltok::lparen, "expected '(' in funcFlags"))
9477 return true;
9478
9479 do {
9480 unsigned Val = 0;
9481 switch (Lex.getKind()) {
9482 case lltok::kw_readNone:
9483 Lex.Lex();
9484 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9485 return true;
9486 FFlags.ReadNone = Val;
9487 break;
9488 case lltok::kw_readOnly:
9489 Lex.Lex();
9490 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9491 return true;
9492 FFlags.ReadOnly = Val;
9493 break;
9495 Lex.Lex();
9496 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9497 return true;
9498 FFlags.NoRecurse = Val;
9499 break;
9501 Lex.Lex();
9502 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9503 return true;
9504 FFlags.ReturnDoesNotAlias = Val;
9505 break;
9506 case lltok::kw_noInline:
9507 Lex.Lex();
9508 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9509 return true;
9510 FFlags.NoInline = Val;
9511 break;
9513 Lex.Lex();
9514 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9515 return true;
9516 FFlags.AlwaysInline = Val;
9517 break;
9518 case lltok::kw_noUnwind:
9519 Lex.Lex();
9520 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9521 return true;
9522 FFlags.NoUnwind = Val;
9523 break;
9524 case lltok::kw_mayThrow:
9525 Lex.Lex();
9526 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9527 return true;
9528 FFlags.MayThrow = Val;
9529 break;
9531 Lex.Lex();
9532 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9533 return true;
9534 FFlags.HasUnknownCall = Val;
9535 break;
9537 Lex.Lex();
9538 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Val))
9539 return true;
9540 FFlags.MustBeUnreachable = Val;
9541 break;
9542 default:
9543 return error(Lex.getLoc(), "expected function flag type");
9544 }
9545 } while (EatIfPresent(lltok::comma));
9546
9547 if (parseToken(lltok::rparen, "expected ')' in funcFlags"))
9548 return true;
9549
9550 return false;
9551}
9552
9553/// OptionalCalls
9554/// := 'calls' ':' '(' Call [',' Call]* ')'
9555/// Call ::= '(' 'callee' ':' GVReference
9556/// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]?
9557/// [ ',' 'tail' ]? ')'
9558bool LLParser::parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) {
9559 assert(Lex.getKind() == lltok::kw_calls);
9560 Lex.Lex();
9561
9562 if (parseToken(lltok::colon, "expected ':' in calls") ||
9563 parseToken(lltok::lparen, "expected '(' in calls"))
9564 return true;
9565
9566 IdToIndexMapType IdToIndexMap;
9567 // parse each call edge
9568 do {
9569 ValueInfo VI;
9570 if (parseToken(lltok::lparen, "expected '(' in call") ||
9571 parseToken(lltok::kw_callee, "expected 'callee' in call") ||
9572 parseToken(lltok::colon, "expected ':'"))
9573 return true;
9574
9575 LocTy Loc = Lex.getLoc();
9576 unsigned GVId;
9577 if (parseGVReference(VI, GVId))
9578 return true;
9579
9581 unsigned RelBF = 0;
9582 unsigned HasTailCall = false;
9583
9584 // parse optional fields
9585 while (EatIfPresent(lltok::comma)) {
9586 switch (Lex.getKind()) {
9587 case lltok::kw_hotness:
9588 Lex.Lex();
9589 if (parseToken(lltok::colon, "expected ':'") || parseHotness(Hotness))
9590 return true;
9591 break;
9592 case lltok::kw_relbf:
9593 Lex.Lex();
9594 if (parseToken(lltok::colon, "expected ':'") || parseUInt32(RelBF))
9595 return true;
9596 break;
9597 case lltok::kw_tail:
9598 Lex.Lex();
9599 if (parseToken(lltok::colon, "expected ':'") || parseFlag(HasTailCall))
9600 return true;
9601 break;
9602 default:
9603 return error(Lex.getLoc(), "expected hotness, relbf, or tail");
9604 }
9605 }
9606 if (Hotness != CalleeInfo::HotnessType::Unknown && RelBF > 0)
9607 return tokError("Expected only one of hotness or relbf");
9608 // Keep track of the Call array index needing a forward reference.
9609 // We will save the location of the ValueInfo needing an update, but
9610 // can only do so once the std::vector is finalized.
9611 if (VI.getRef() == FwdVIRef)
9612 IdToIndexMap[GVId].push_back(std::make_pair(Calls.size(), Loc));
9613 Calls.push_back(
9614 FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, HasTailCall, RelBF)});
9615
9616 if (parseToken(lltok::rparen, "expected ')' in call"))
9617 return true;
9618 } while (EatIfPresent(lltok::comma));
9619
9620 // Now that the Calls vector is finalized, it is safe to save the locations
9621 // of any forward GV references that need updating later.
9622 for (auto I : IdToIndexMap) {
9623 auto &Infos = ForwardRefValueInfos[I.first];
9624 for (auto P : I.second) {
9625 assert(Calls[P.first].first.getRef() == FwdVIRef &&
9626 "Forward referenced ValueInfo expected to be empty");
9627 Infos.emplace_back(&Calls[P.first].first, P.second);
9628 }
9629 }
9630
9631 if (parseToken(lltok::rparen, "expected ')' in calls"))
9632 return true;
9633
9634 return false;
9635}
9636
9637/// Hotness
9638/// := ('unknown'|'cold'|'none'|'hot'|'critical')
9639bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) {
9640 switch (Lex.getKind()) {
9641 case lltok::kw_unknown:
9643 break;
9644 case lltok::kw_cold:
9646 break;
9647 case lltok::kw_none:
9649 break;
9650 case lltok::kw_hot:
9652 break;
9653 case lltok::kw_critical:
9655 break;
9656 default:
9657 return error(Lex.getLoc(), "invalid call edge hotness");
9658 }
9659 Lex.Lex();
9660 return false;
9661}
9662
9663/// OptionalVTableFuncs
9664/// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')'
9665/// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')'
9666bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) {
9668 Lex.Lex();
9669
9670 if (parseToken(lltok::colon, "expected ':' in vTableFuncs") ||
9671 parseToken(lltok::lparen, "expected '(' in vTableFuncs"))
9672 return true;
9673
9674 IdToIndexMapType IdToIndexMap;
9675 // parse each virtual function pair
9676 do {
9677 ValueInfo VI;
9678 if (parseToken(lltok::lparen, "expected '(' in vTableFunc") ||
9679 parseToken(lltok::kw_virtFunc, "expected 'callee' in vTableFunc") ||
9680 parseToken(lltok::colon, "expected ':'"))
9681 return true;
9682
9683 LocTy Loc = Lex.getLoc();
9684 unsigned GVId;
9685 if (parseGVReference(VI, GVId))
9686 return true;
9687
9689 if (parseToken(lltok::comma, "expected comma") ||
9690 parseToken(lltok::kw_offset, "expected offset") ||
9691 parseToken(lltok::colon, "expected ':'") || parseUInt64(Offset))
9692 return true;
9693
9694 // Keep track of the VTableFuncs array index needing a forward reference.
9695 // We will save the location of the ValueInfo needing an update, but
9696 // can only do so once the std::vector is finalized.
9697 if (VI == EmptyVI)
9698 IdToIndexMap[GVId].push_back(std::make_pair(VTableFuncs.size(), Loc));
9699 VTableFuncs.push_back({VI, Offset});
9700
9701 if (parseToken(lltok::rparen, "expected ')' in vTableFunc"))
9702 return true;
9703 } while (EatIfPresent(lltok::comma));
9704
9705 // Now that the VTableFuncs vector is finalized, it is safe to save the
9706 // locations of any forward GV references that need updating later.
9707 for (auto I : IdToIndexMap) {
9708 auto &Infos = ForwardRefValueInfos[I.first];
9709 for (auto P : I.second) {
9710 assert(VTableFuncs[P.first].FuncVI == EmptyVI &&
9711 "Forward referenced ValueInfo expected to be empty");
9712 Infos.emplace_back(&VTableFuncs[P.first].FuncVI, P.second);
9713 }
9714 }
9715
9716 if (parseToken(lltok::rparen, "expected ')' in vTableFuncs"))
9717 return true;
9718
9719 return false;
9720}
9721
9722/// ParamNo := 'param' ':' UInt64
9723bool LLParser::parseParamNo(uint64_t &ParamNo) {
9724 if (parseToken(lltok::kw_param, "expected 'param' here") ||
9725 parseToken(lltok::colon, "expected ':' here") || parseUInt64(ParamNo))
9726 return true;
9727 return false;
9728}
9729
9730/// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']'
9731bool LLParser::parseParamAccessOffset(ConstantRange &Range) {
9732 APSInt Lower;
9733 APSInt Upper;
9734 auto ParseAPSInt = [&](APSInt &Val) {
9735 if (Lex.getKind() != lltok::APSInt)
9736 return tokError("expected integer");
9737 Val = Lex.getAPSIntVal();
9738 Val = Val.extOrTrunc(FunctionSummary::ParamAccess::RangeWidth);
9739 Val.setIsSigned(true);
9740 Lex.Lex();
9741 return false;
9742 };
9743 if (parseToken(lltok::kw_offset, "expected 'offset' here") ||
9744 parseToken(lltok::colon, "expected ':' here") ||
9745 parseToken(lltok::lsquare, "expected '[' here") || ParseAPSInt(Lower) ||
9746 parseToken(lltok::comma, "expected ',' here") || ParseAPSInt(Upper) ||
9747 parseToken(lltok::rsquare, "expected ']' here"))
9748 return true;
9749
9750 ++Upper;
9751 Range =
9752 (Lower == Upper && !Lower.isMaxValue())
9753 ? ConstantRange::getEmpty(FunctionSummary::ParamAccess::RangeWidth)
9755
9756 return false;
9757}
9758
9759/// ParamAccessCall
9760/// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')'
9761bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
9762 IdLocListType &IdLocList) {
9763 if (parseToken(lltok::lparen, "expected '(' here") ||
9764 parseToken(lltok::kw_callee, "expected 'callee' here") ||
9765 parseToken(lltok::colon, "expected ':' here"))
9766 return true;
9767
9768 unsigned GVId;
9769 ValueInfo VI;
9770 LocTy Loc = Lex.getLoc();
9771 if (parseGVReference(VI, GVId))
9772 return true;
9773
9774 Call.Callee = VI;
9775 IdLocList.emplace_back(GVId, Loc);
9776
9777 if (parseToken(lltok::comma, "expected ',' here") ||
9778 parseParamNo(Call.ParamNo) ||
9779 parseToken(lltok::comma, "expected ',' here") ||
9780 parseParamAccessOffset(Call.Offsets))
9781 return true;
9782
9783 if (parseToken(lltok::rparen, "expected ')' here"))
9784 return true;
9785
9786 return false;
9787}
9788
9789/// ParamAccess
9790/// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')'
9791/// OptionalParamAccessCalls := '(' Call [',' Call]* ')'
9792bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param,
9793 IdLocListType &IdLocList) {
9794 if (parseToken(lltok::lparen, "expected '(' here") ||
9795 parseParamNo(Param.ParamNo) ||
9796 parseToken(lltok::comma, "expected ',' here") ||
9797 parseParamAccessOffset(Param.Use))
9798 return true;
9799
9800 if (EatIfPresent(lltok::comma)) {
9801 if (parseToken(lltok::kw_calls, "expected 'calls' here") ||
9802 parseToken(lltok::colon, "expected ':' here") ||
9803 parseToken(lltok::lparen, "expected '(' here"))
9804 return true;
9805 do {
9807 if (parseParamAccessCall(Call, IdLocList))
9808 return true;
9809 Param.Calls.push_back(Call);
9810 } while (EatIfPresent(lltok::comma));
9811
9812 if (parseToken(lltok::rparen, "expected ')' here"))
9813 return true;
9814 }
9815
9816 if (parseToken(lltok::rparen, "expected ')' here"))
9817 return true;
9818
9819 return false;
9820}
9821
9822/// OptionalParamAccesses
9823/// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')'
9824bool LLParser::parseOptionalParamAccesses(
9825 std::vector<FunctionSummary::ParamAccess> &Params) {
9827 Lex.Lex();
9828
9829 if (parseToken(lltok::colon, "expected ':' here") ||
9830 parseToken(lltok::lparen, "expected '(' here"))
9831 return true;
9832
9833 IdLocListType VContexts;
9834 size_t CallsNum = 0;
9835 do {
9836 FunctionSummary::ParamAccess ParamAccess;
9837 if (parseParamAccess(ParamAccess, VContexts))
9838 return true;
9839 CallsNum += ParamAccess.Calls.size();
9840 assert(VContexts.size() == CallsNum);
9841 (void)CallsNum;
9842 Params.emplace_back(std::move(ParamAccess));
9843 } while (EatIfPresent(lltok::comma));
9844
9845 if (parseToken(lltok::rparen, "expected ')' here"))
9846 return true;
9847
9848 // Now that the Params is finalized, it is safe to save the locations
9849 // of any forward GV references that need updating later.
9850 IdLocListType::const_iterator ItContext = VContexts.begin();
9851 for (auto &PA : Params) {
9852 for (auto &C : PA.Calls) {
9853 if (C.Callee.getRef() == FwdVIRef)
9854 ForwardRefValueInfos[ItContext->first].emplace_back(&C.Callee,
9855 ItContext->second);
9856 ++ItContext;
9857 }
9858 }
9859 assert(ItContext == VContexts.end());
9860
9861 return false;
9862}
9863
9864/// OptionalRefs
9865/// := 'refs' ':' '(' GVReference [',' GVReference]* ')'
9866bool LLParser::parseOptionalRefs(std::vector<ValueInfo> &Refs) {
9867 assert(Lex.getKind() == lltok::kw_refs);
9868 Lex.Lex();
9869
9870 if (parseToken(lltok::colon, "expected ':' in refs") ||
9871 parseToken(lltok::lparen, "expected '(' in refs"))
9872 return true;
9873
9874 struct ValueContext {
9875 ValueInfo VI;
9876 unsigned GVId;
9877 LocTy Loc;
9878 };
9879 std::vector<ValueContext> VContexts;
9880 // parse each ref edge
9881 do {
9882 ValueContext VC;
9883 VC.Loc = Lex.getLoc();
9884 if (parseGVReference(VC.VI, VC.GVId))
9885 return true;
9886 VContexts.push_back(VC);
9887 } while (EatIfPresent(lltok::comma));
9888
9889 // Sort value contexts so that ones with writeonly
9890 // and readonly ValueInfo are at the end of VContexts vector.
9891 // See FunctionSummary::specialRefCounts()
9892 llvm::sort(VContexts, [](const ValueContext &VC1, const ValueContext &VC2) {
9893 return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier();
9894 });
9895
9896 IdToIndexMapType IdToIndexMap;
9897 for (auto &VC : VContexts) {
9898 // Keep track of the Refs array index needing a forward reference.
9899 // We will save the location of the ValueInfo needing an update, but
9900 // can only do so once the std::vector is finalized.
9901 if (VC.VI.getRef() == FwdVIRef)
9902 IdToIndexMap[VC.GVId].push_back(std::make_pair(Refs.size(), VC.Loc));
9903 Refs.push_back(VC.VI);
9904 }
9905
9906 // Now that the Refs vector is finalized, it is safe to save the locations
9907 // of any forward GV references that need updating later.
9908 for (auto I : IdToIndexMap) {
9909 auto &Infos = ForwardRefValueInfos[I.first];
9910 for (auto P : I.second) {
9911 assert(Refs[P.first].getRef() == FwdVIRef &&
9912 "Forward referenced ValueInfo expected to be empty");
9913 Infos.emplace_back(&Refs[P.first], P.second);
9914 }
9915 }
9916
9917 if (parseToken(lltok::rparen, "expected ')' in refs"))
9918 return true;
9919
9920 return false;
9921}
9922
9923/// OptionalTypeIdInfo
9924/// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
9925/// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]?
9926/// [',' TypeCheckedLoadConstVCalls]? ')'
9927bool LLParser::parseOptionalTypeIdInfo(
9928 FunctionSummary::TypeIdInfo &TypeIdInfo) {
9930 Lex.Lex();
9931
9932 if (parseToken(lltok::colon, "expected ':' here") ||
9933 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
9934 return true;
9935
9936 do {
9937 switch (Lex.getKind()) {
9939 if (parseTypeTests(TypeIdInfo.TypeTests))
9940 return true;
9941 break;
9943 if (parseVFuncIdList(lltok::kw_typeTestAssumeVCalls,
9944 TypeIdInfo.TypeTestAssumeVCalls))
9945 return true;
9946 break;
9948 if (parseVFuncIdList(lltok::kw_typeCheckedLoadVCalls,
9949 TypeIdInfo.TypeCheckedLoadVCalls))
9950 return true;
9951 break;
9953 if (parseConstVCallList(lltok::kw_typeTestAssumeConstVCalls,
9954 TypeIdInfo.TypeTestAssumeConstVCalls))
9955 return true;
9956 break;
9958 if (parseConstVCallList(lltok::kw_typeCheckedLoadConstVCalls,
9959 TypeIdInfo.TypeCheckedLoadConstVCalls))
9960 return true;
9961 break;
9962 default:
9963 return error(Lex.getLoc(), "invalid typeIdInfo list type");
9964 }
9965 } while (EatIfPresent(lltok::comma));
9966
9967 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
9968 return true;
9969
9970 return false;
9971}
9972
9973/// TypeTests
9974/// ::= 'typeTests' ':' '(' (SummaryID | UInt64)
9975/// [',' (SummaryID | UInt64)]* ')'
9976bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
9978 Lex.Lex();
9979
9980 if (parseToken(lltok::colon, "expected ':' here") ||
9981 parseToken(lltok::lparen, "expected '(' in typeIdInfo"))
9982 return true;
9983
9984 IdToIndexMapType IdToIndexMap;
9985 do {
9986 GlobalValue::GUID GUID = 0;
9987 if (Lex.getKind() == lltok::SummaryID) {
9988 unsigned ID = Lex.getUIntVal();
9989 LocTy Loc = Lex.getLoc();
9990 // Keep track of the TypeTests array index needing a forward reference.
9991 // We will save the location of the GUID needing an update, but
9992 // can only do so once the std::vector is finalized.
9993 IdToIndexMap[ID].push_back(std::make_pair(TypeTests.size(), Loc));
9994 Lex.Lex();
9995 } else if (parseUInt64(GUID))
9996 return true;
9997 TypeTests.push_back(GUID);
9998 } while (EatIfPresent(lltok::comma));
9999
10000 // Now that the TypeTests vector is finalized, it is safe to save the
10001 // locations of any forward GV references that need updating later.
10002 for (auto I : IdToIndexMap) {
10003 auto &Ids = ForwardRefTypeIds[I.first];
10004 for (auto P : I.second) {
10005 assert(TypeTests[P.first] == 0 &&
10006 "Forward referenced type id GUID expected to be 0");
10007 Ids.emplace_back(&TypeTests[P.first], P.second);
10008 }
10009 }
10010
10011 if (parseToken(lltok::rparen, "expected ')' in typeIdInfo"))
10012 return true;
10013
10014 return false;
10015}
10016
10017/// VFuncIdList
10018/// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
10019bool LLParser::parseVFuncIdList(
10020 lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
10021 assert(Lex.getKind() == Kind);
10022 Lex.Lex();
10023
10024 if (parseToken(lltok::colon, "expected ':' here") ||
10025 parseToken(lltok::lparen, "expected '(' here"))
10026 return true;
10027
10028 IdToIndexMapType IdToIndexMap;
10029 do {
10031 if (parseVFuncId(VFuncId, IdToIndexMap, VFuncIdList.size()))
10032 return true;
10033 VFuncIdList.push_back(VFuncId);
10034 } while (EatIfPresent(lltok::comma));
10035
10036 if (parseToken(lltok::rparen, "expected ')' here"))
10037 return true;
10038
10039 // Now that the VFuncIdList vector is finalized, it is safe to save the
10040 // locations of any forward GV references that need updating later.
10041 for (auto I : IdToIndexMap) {
10042 auto &Ids = ForwardRefTypeIds[I.first];
10043 for (auto P : I.second) {
10044 assert(VFuncIdList[P.first].GUID == 0 &&
10045 "Forward referenced type id GUID expected to be 0");
10046 Ids.emplace_back(&VFuncIdList[P.first].GUID, P.second);
10047 }
10048 }
10049
10050 return false;
10051}
10052
10053/// ConstVCallList
10054/// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
10055bool LLParser::parseConstVCallList(
10056 lltok::Kind Kind,
10057 std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
10058 assert(Lex.getKind() == Kind);
10059 Lex.Lex();
10060
10061 if (parseToken(lltok::colon, "expected ':' here") ||
10062 parseToken(lltok::lparen, "expected '(' here"))
10063 return true;
10064
10065 IdToIndexMapType IdToIndexMap;
10066 do {
10067 FunctionSummary::ConstVCall ConstVCall;
10068 if (parseConstVCall(ConstVCall, IdToIndexMap, ConstVCallList.size()))
10069 return true;
10070 ConstVCallList.push_back(ConstVCall);
10071 } while (EatIfPresent(lltok::comma));
10072
10073 if (parseToken(lltok::rparen, "expected ')' here"))
10074 return true;
10075
10076 // Now that the ConstVCallList vector is finalized, it is safe to save the
10077 // locations of any forward GV references that need updating later.
10078 for (auto I : IdToIndexMap) {
10079 auto &Ids = ForwardRefTypeIds[I.first];
10080 for (auto P : I.second) {
10081 assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
10082 "Forward referenced type id GUID expected to be 0");
10083 Ids.emplace_back(&ConstVCallList[P.first].VFunc.GUID, P.second);
10084 }
10085 }
10086
10087 return false;
10088}
10089
10090/// ConstVCall
10091/// ::= '(' VFuncId ',' Args ')'
10092bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
10093 IdToIndexMapType &IdToIndexMap, unsigned Index) {
10094 if (parseToken(lltok::lparen, "expected '(' here") ||
10095 parseVFuncId(ConstVCall.VFunc, IdToIndexMap, Index))
10096 return true;
10097
10098 if (EatIfPresent(lltok::comma))
10099 if (parseArgs(ConstVCall.Args))
10100 return true;
10101
10102 if (parseToken(lltok::rparen, "expected ')' here"))
10103 return true;
10104
10105 return false;
10106}
10107
10108/// VFuncId
10109/// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
10110/// 'offset' ':' UInt64 ')'
10111bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId,
10112 IdToIndexMapType &IdToIndexMap, unsigned Index) {
10114 Lex.Lex();
10115
10116 if (parseToken(lltok::colon, "expected ':' here") ||
10117 parseToken(lltok::lparen, "expected '(' here"))
10118 return true;
10119
10120 if (Lex.getKind() == lltok::SummaryID) {
10121 VFuncId.GUID = 0;
10122 unsigned ID = Lex.getUIntVal();
10123 LocTy Loc = Lex.getLoc();
10124 // Keep track of the array index needing a forward reference.
10125 // We will save the location of the GUID needing an update, but
10126 // can only do so once the caller's std::vector is finalized.
10127 IdToIndexMap[ID].push_back(std::make_pair(Index, Loc));
10128 Lex.Lex();
10129 } else if (parseToken(lltok::kw_guid, "expected 'guid' here") ||
10130 parseToken(lltok::colon, "expected ':' here") ||
10131 parseUInt64(VFuncId.GUID))
10132 return true;
10133
10134 if (parseToken(lltok::comma, "expected ',' here") ||
10135 parseToken(lltok::kw_offset, "expected 'offset' here") ||
10136 parseToken(lltok::colon, "expected ':' here") ||
10137 parseUInt64(VFuncId.Offset) ||
10138 parseToken(lltok::rparen, "expected ')' here"))
10139 return true;
10140
10141 return false;
10142}
10143
10144/// GVFlags
10145/// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
10146/// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ','
10147/// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ','
10148/// 'canAutoHide' ':' Flag ',' ')'
10149bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
10150 assert(Lex.getKind() == lltok::kw_flags);
10151 Lex.Lex();
10152
10153 if (parseToken(lltok::colon, "expected ':' here") ||
10154 parseToken(lltok::lparen, "expected '(' here"))
10155 return true;
10156
10157 do {
10158 unsigned Flag = 0;
10159 switch (Lex.getKind()) {
10160 case lltok::kw_linkage:
10161 Lex.Lex();
10162 if (parseToken(lltok::colon, "expected ':'"))
10163 return true;
10164 bool HasLinkage;
10165 GVFlags.Linkage = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
10166 assert(HasLinkage && "Linkage not optional in summary entry");
10167 Lex.Lex();
10168 break;
10170 Lex.Lex();
10171 if (parseToken(lltok::colon, "expected ':'"))
10172 return true;
10173 parseOptionalVisibility(Flag);
10174 GVFlags.Visibility = Flag;
10175 break;
10177 Lex.Lex();
10178 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10179 return true;
10180 GVFlags.NotEligibleToImport = Flag;
10181 break;
10182 case lltok::kw_live:
10183 Lex.Lex();
10184 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10185 return true;
10186 GVFlags.Live = Flag;
10187 break;
10188 case lltok::kw_dsoLocal:
10189 Lex.Lex();
10190 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10191 return true;
10192 GVFlags.DSOLocal = Flag;
10193 break;
10195 Lex.Lex();
10196 if (parseToken(lltok::colon, "expected ':'") || parseFlag(Flag))
10197 return true;
10198 GVFlags.CanAutoHide = Flag;
10199 break;
10201 Lex.Lex();
10202 if (parseToken(lltok::colon, "expected ':'"))
10203 return true;
10205 if (parseOptionalImportType(Lex.getKind(), IK))
10206 return true;
10207 GVFlags.ImportType = static_cast<unsigned>(IK);
10208 Lex.Lex();
10209 break;
10210 default:
10211 return error(Lex.getLoc(), "expected gv flag type");
10212 }
10213 } while (EatIfPresent(lltok::comma));
10214
10215 if (parseToken(lltok::rparen, "expected ')' here"))
10216 return true;
10217
10218 return false;
10219}
10220
10221/// GVarFlags
10222/// ::= 'varFlags' ':' '(' 'readonly' ':' Flag
10223/// ',' 'writeonly' ':' Flag
10224/// ',' 'constant' ':' Flag ')'
10225bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) {
10227 Lex.Lex();
10228
10229 if (parseToken(lltok::colon, "expected ':' here") ||
10230 parseToken(lltok::lparen, "expected '(' here"))
10231 return true;
10232
10233 auto ParseRest = [this](unsigned int &Val) {
10234 Lex.Lex();
10235 if (parseToken(lltok::colon, "expected ':'"))
10236 return true;
10237 return parseFlag(Val);
10238 };
10239
10240 do {
10241 unsigned Flag = 0;
10242 switch (Lex.getKind()) {
10243 case lltok::kw_readonly:
10244 if (ParseRest(Flag))
10245 return true;
10246 GVarFlags.MaybeReadOnly = Flag;
10247 break;
10248 case lltok::kw_writeonly:
10249 if (ParseRest(Flag))
10250 return true;
10251 GVarFlags.MaybeWriteOnly = Flag;
10252 break;
10253 case lltok::kw_constant:
10254 if (ParseRest(Flag))
10255 return true;
10256 GVarFlags.Constant = Flag;
10257 break;
10259 if (ParseRest(Flag))
10260 return true;
10261 GVarFlags.VCallVisibility = Flag;
10262 break;
10263 default:
10264 return error(Lex.getLoc(), "expected gvar flag type");
10265 }
10266 } while (EatIfPresent(lltok::comma));
10267 return parseToken(lltok::rparen, "expected ')' here");
10268}
10269
10270/// ModuleReference
10271/// ::= 'module' ':' UInt
10272bool LLParser::parseModuleReference(StringRef &ModulePath) {
10273 // parse module id.
10274 if (parseToken(lltok::kw_module, "expected 'module' here") ||
10275 parseToken(lltok::colon, "expected ':' here") ||
10276 parseToken(lltok::SummaryID, "expected module ID"))
10277 return true;
10278
10279 unsigned ModuleID = Lex.getUIntVal();
10280 auto I = ModuleIdMap.find(ModuleID);
10281 // We should have already parsed all module IDs
10282 assert(I != ModuleIdMap.end());
10283 ModulePath = I->second;
10284 return false;
10285}
10286
10287/// GVReference
10288/// ::= SummaryID
10289bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) {
10290 bool WriteOnly = false, ReadOnly = EatIfPresent(lltok::kw_readonly);
10291 if (!ReadOnly)
10292 WriteOnly = EatIfPresent(lltok::kw_writeonly);
10293 if (parseToken(lltok::SummaryID, "expected GV ID"))
10294 return true;
10295
10296 GVId = Lex.getUIntVal();
10297 // Check if we already have a VI for this GV
10298 if (GVId < NumberedValueInfos.size() && NumberedValueInfos[GVId]) {
10299 assert(NumberedValueInfos[GVId].getRef() != FwdVIRef);
10300 VI = NumberedValueInfos[GVId];
10301 } else
10302 // We will create a forward reference to the stored location.
10303 VI = ValueInfo(false, FwdVIRef);
10304
10305 if (ReadOnly)
10306 VI.setReadOnly();
10307 if (WriteOnly)
10308 VI.setWriteOnly();
10309 return false;
10310}
10311
10312/// OptionalAllocs
10313/// := 'allocs' ':' '(' Alloc [',' Alloc]* ')'
10314/// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')'
10315/// ',' MemProfs ')'
10316/// Version ::= UInt32
10317bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) {
10319 Lex.Lex();
10320
10321 if (parseToken(lltok::colon, "expected ':' in allocs") ||
10322 parseToken(lltok::lparen, "expected '(' in allocs"))
10323 return true;
10324
10325 // parse each alloc
10326 do {
10327 if (parseToken(lltok::lparen, "expected '(' in alloc") ||
10328 parseToken(lltok::kw_versions, "expected 'versions' in alloc") ||
10329 parseToken(lltok::colon, "expected ':'") ||
10330 parseToken(lltok::lparen, "expected '(' in versions"))
10331 return true;
10332
10333 SmallVector<uint8_t> Versions;
10334 do {
10335 uint8_t V = 0;
10336 if (parseAllocType(V))
10337 return true;
10338 Versions.push_back(V);
10339 } while (EatIfPresent(lltok::comma));
10340
10341 if (parseToken(lltok::rparen, "expected ')' in versions") ||
10342 parseToken(lltok::comma, "expected ',' in alloc"))
10343 return true;
10344
10345 std::vector<MIBInfo> MIBs;
10346 if (parseMemProfs(MIBs))
10347 return true;
10348
10349 Allocs.push_back({Versions, MIBs});
10350
10351 if (parseToken(lltok::rparen, "expected ')' in alloc"))
10352 return true;
10353 } while (EatIfPresent(lltok::comma));
10354
10355 if (parseToken(lltok::rparen, "expected ')' in allocs"))
10356 return true;
10357
10358 return false;
10359}
10360
10361/// MemProfs
10362/// := 'memProf' ':' '(' MemProf [',' MemProf]* ')'
10363/// MemProf ::= '(' 'type' ':' AllocType
10364/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
10365/// StackId ::= UInt64
10366bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) {
10368 Lex.Lex();
10369
10370 if (parseToken(lltok::colon, "expected ':' in memprof") ||
10371 parseToken(lltok::lparen, "expected '(' in memprof"))
10372 return true;
10373
10374 // parse each MIB
10375 do {
10376 if (parseToken(lltok::lparen, "expected '(' in memprof") ||
10377 parseToken(lltok::kw_type, "expected 'type' in memprof") ||
10378 parseToken(lltok::colon, "expected ':'"))
10379 return true;
10380
10381 uint8_t AllocType;
10382 if (parseAllocType(AllocType))
10383 return true;
10384
10385 if (parseToken(lltok::comma, "expected ',' in memprof") ||
10386 parseToken(lltok::kw_stackIds, "expected 'stackIds' in memprof") ||
10387 parseToken(lltok::colon, "expected ':'") ||
10388 parseToken(lltok::lparen, "expected '(' in stackIds"))
10389 return true;
10390
10391 SmallVector<unsigned> StackIdIndices;
10392 do {
10393 uint64_t StackId = 0;
10394 if (parseUInt64(StackId))
10395 return true;
10396 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
10397 } while (EatIfPresent(lltok::comma));
10398
10399 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
10400 return true;
10401
10402 MIBs.push_back({(AllocationType)AllocType, StackIdIndices});
10403
10404 if (parseToken(lltok::rparen, "expected ')' in memprof"))
10405 return true;
10406 } while (EatIfPresent(lltok::comma));
10407
10408 if (parseToken(lltok::rparen, "expected ')' in memprof"))
10409 return true;
10410
10411 return false;
10412}
10413
10414/// AllocType
10415/// := ('none'|'notcold'|'cold'|'hot')
10416bool LLParser::parseAllocType(uint8_t &AllocType) {
10417 switch (Lex.getKind()) {
10418 case lltok::kw_none:
10420 break;
10421 case lltok::kw_notcold:
10423 break;
10424 case lltok::kw_cold:
10426 break;
10427 case lltok::kw_hot:
10428 AllocType = (uint8_t)AllocationType::Hot;
10429 break;
10430 default:
10431 return error(Lex.getLoc(), "invalid alloc type");
10432 }
10433 Lex.Lex();
10434 return false;
10435}
10436
10437/// OptionalCallsites
10438/// := 'callsites' ':' '(' Callsite [',' Callsite]* ')'
10439/// Callsite ::= '(' 'callee' ':' GVReference
10440/// ',' 'clones' ':' '(' Version [',' Version]* ')'
10441/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
10442/// Version ::= UInt32
10443/// StackId ::= UInt64
10444bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) {
10446 Lex.Lex();
10447
10448 if (parseToken(lltok::colon, "expected ':' in callsites") ||
10449 parseToken(lltok::lparen, "expected '(' in callsites"))
10450 return true;
10451
10452 IdToIndexMapType IdToIndexMap;
10453 // parse each callsite
10454 do {
10455 if (parseToken(lltok::lparen, "expected '(' in callsite") ||
10456 parseToken(lltok::kw_callee, "expected 'callee' in callsite") ||
10457 parseToken(lltok::colon, "expected ':'"))
10458 return true;
10459
10460 ValueInfo VI;
10461 unsigned GVId = 0;
10462 LocTy Loc = Lex.getLoc();
10463 if (!EatIfPresent(lltok::kw_null)) {
10464 if (parseGVReference(VI, GVId))
10465 return true;
10466 }
10467
10468 if (parseToken(lltok::comma, "expected ',' in callsite") ||
10469 parseToken(lltok::kw_clones, "expected 'clones' in callsite") ||
10470 parseToken(lltok::colon, "expected ':'") ||
10471 parseToken(lltok::lparen, "expected '(' in clones"))
10472 return true;
10473
10474 SmallVector<unsigned> Clones;
10475 do {
10476 unsigned V = 0;
10477 if (parseUInt32(V))
10478 return true;
10479 Clones.push_back(V);
10480 } while (EatIfPresent(lltok::comma));
10481
10482 if (parseToken(lltok::rparen, "expected ')' in clones") ||
10483 parseToken(lltok::comma, "expected ',' in callsite") ||
10484 parseToken(lltok::kw_stackIds, "expected 'stackIds' in callsite") ||
10485 parseToken(lltok::colon, "expected ':'") ||
10486 parseToken(lltok::lparen, "expected '(' in stackIds"))
10487 return true;
10488
10489 SmallVector<unsigned> StackIdIndices;
10490 do {
10491 uint64_t StackId = 0;
10492 if (parseUInt64(StackId))
10493 return true;
10494 StackIdIndices.push_back(Index->addOrGetStackIdIndex(StackId));
10495 } while (EatIfPresent(lltok::comma));
10496
10497 if (parseToken(lltok::rparen, "expected ')' in stackIds"))
10498 return true;
10499
10500 // Keep track of the Callsites array index needing a forward reference.
10501 // We will save the location of the ValueInfo needing an update, but
10502 // can only do so once the SmallVector is finalized.
10503 if (VI.getRef() == FwdVIRef)
10504 IdToIndexMap[GVId].push_back(std::make_pair(Callsites.size(), Loc));
10505 Callsites.push_back({VI, Clones, StackIdIndices});
10506
10507 if (parseToken(lltok::rparen, "expected ')' in callsite"))
10508 return true;
10509 } while (EatIfPresent(lltok::comma));
10510
10511 // Now that the Callsites vector is finalized, it is safe to save the
10512 // locations of any forward GV references that need updating later.
10513 for (auto I : IdToIndexMap) {
10514 auto &Infos = ForwardRefValueInfos[I.first];
10515 for (auto P : I.second) {
10516 assert(Callsites[P.first].Callee.getRef() == FwdVIRef &&
10517 "Forward referenced ValueInfo expected to be empty");
10518 Infos.emplace_back(&Callsites[P.first].Callee, P.second);
10519 }
10520 }
10521
10522 if (parseToken(lltok::rparen, "expected ')' in callsites"))
10523 return true;
10524
10525 return false;
10526}
static int64_t upperBound(StackOffset Size)
Unify divergent function exit nodes
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
Expand Atomic instructions
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil globals
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Given that RA is a live value
This file defines the DenseMap class.
@ Default
Definition: DwarfDebug.cpp:87
This file contains constants used for implementing Dwarf debug support.
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
DenseMap< Block *, BlockRelaxAux > Blocks
Definition: ELF_riscv.cpp:507
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
#define _
static GlobalValue * createGlobalFwdRef(Module *M, PointerType *PTy)
Definition: LLParser.cpp:1735
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)
Definition: LLParser.cpp:1132
cl::opt< cl::boolOrDefault > PreserveInputDbgFormat
static bool upgradeMemoryAttr(MemoryEffects &ME, lltok::Kind Kind)
Definition: LLParser.cpp:1638
static std::optional< MemoryEffects::Location > keywordToLoc(lltok::Kind Tok)
Definition: LLParser.cpp:2465
static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved)
Definition: LLParser.cpp:9063
cl::opt< bool > WriteNewDbgInfoFormat
static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage)
Definition: LLParser.cpp:2004
bool WriteNewDbgInfoFormatToBitcode
Definition: BasicBlock.cpp:47
static unsigned keywordToFPClassTest(lltok::Kind Tok)
Definition: LLParser.cpp:2546
llvm::cl::opt< bool > UseNewDbgInfoFormat
static std::optional< ModRefInfo > keywordToModRef(lltok::Kind Tok)
Definition: LLParser.cpp:2476
static bool isSanitizer(lltok::Kind Kind)
Definition: LLParser.cpp:1290
static void dropIntrinsicWithUnknownMetadataArgument(IntrinsicInst *II)
Definition: LLParser.cpp:156
#define PARSE_MD_FIELDS()
Definition: LLParser.cpp:5048
static Attribute::AttrKind tokenToAttribute(lltok::Kind Kind)
Definition: LLParser.cpp:1522
static ValueInfo EmptyVI
Definition: LLParser.cpp:8704
#define GET_OR_DISTINCT(CLASS, ARGS)
Definition: LLParser.cpp:5062
bool isOldDbgFormatIntrinsic(StringRef Name)
Definition: LLParser.cpp:6184
static bool isValidVisibilityForLinkage(unsigned V, unsigned L)
Definition: LLParser.cpp:1121
static std::string getTypeString(Type *T)
Definition: LLParser.cpp:70
static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L)
Definition: LLParser.cpp:1125
static const auto FwdVIRef
Definition: LLParser.cpp:9061
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
AllocType
This file contains the declarations for metadata subclasses.
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
Module.h This file contains the declarations for the Module class.
#define P(N)
PowerPC Reduce CR logical Operation
llvm::cl::opt< bool > UseNewDbgInfoFormat
static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val)
const SmallVectorImpl< MachineOperand > & Cond
dot regions Print regions of function to dot file(with no function bodies)"
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static const char * name
Definition: SMEABIPass.cpp:49
This file contains some templates that are useful if you are working with the STL at all.
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.
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:40
Value * RHS
Value * LHS
static APFloat getSNaN(const fltSemantics &Sem, bool Negative=false, const APInt *payload=nullptr)
Factory for SNaN values.
Definition: APFloat.h:996
Class for arbitrary precision integers.
Definition: APInt.h:76
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition: APInt.h:453
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:449
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1215
An arbitrary precision integer that knows its signedness.
Definition: APSInt.h:23
APSInt extOrTrunc(uint32_t width) const
Definition: APSInt.h:119
APSInt extend(uint32_t width) const
Definition: APSInt.h:112
bool isSigned() const
Definition: APSInt.h:77
an instruction to allocate memory on the stack
Definition: Instructions.h:59
void setSwiftError(bool V)
Specify whether this alloca is used to represent a swifterror.
Definition: Instructions.h:159
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
Definition: Instructions.h:152
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:659
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:647
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:539
void setWeak(bool IsWeak)
Definition: Instructions.h:608
static bool isValidFailureOrdering(AtomicOrdering Ordering)
Definition: Instructions.h:618
void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
Definition: Instructions.h:603
static bool isValidSuccessOrdering(AtomicOrdering Ordering)
Definition: Instructions.h:613
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:748
void setVolatile(bool V)
Specify whether this is a volatile RMW or not.
Definition: Instructions.h:881
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:760
@ Add
*p = old + v
Definition: Instructions.h:764
@ FAdd
*p = old + v
Definition: Instructions.h:785
@ Min
*p = old <signed v ? old : v
Definition: Instructions.h:778
@ Or
*p = old | v
Definition: Instructions.h:772
@ Sub
*p = old - v
Definition: Instructions.h:766
@ And
*p = old & v
Definition: Instructions.h:768
@ Xor
*p = old ^ v
Definition: Instructions.h:774
@ FSub
*p = old - v
Definition: Instructions.h:788
@ UIncWrap
Increment one up to a maximum value.
Definition: Instructions.h:800
@ Max
*p = old >signed v ? old : v
Definition: Instructions.h:776
@ UMin
*p = old <unsigned v ? old : v
Definition: Instructions.h:782
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
Definition: Instructions.h:796
@ UMax
*p = old >unsigned v ? old : v
Definition: Instructions.h:780
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
Definition: Instructions.h:792
@ UDecWrap
Decrement one until a minimum value or zero.
Definition: Instructions.h:804
@ Nand
*p = ~(old & v)
Definition: Instructions.h:770
static StringRef getOperationName(BinOp Op)
AttributeSet getFnAttrs() const
The function attributes are returned.
static AttributeList get(LLVMContext &C, ArrayRef< std::pair< unsigned, Attribute > > Attrs)
Create an AttributeList with the specified parameters in it.
AttributeList removeAttribute(LLVMContext &C, unsigned Index, StringRef Kind) const
Definition: Attributes.h:625
AttributeList addFnAttributes(LLVMContext &C, const AttrBuilder &B) const
Add function attribute to the list.
Definition: Attributes.h:560
bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the attribute exists for the given argument.
Definition: Attributes.h:783
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
AttributeList removeFnAttributes(LLVMContext &C, const AttributeMask &AttrsToRemove) const
Remove the specified attribute at the function index from this attribute list.
Definition: Attributes.h:658
static AttributeSet get(LLVMContext &C, const AttrBuilder &B)
Definition: Attributes.cpp:774
static bool canUseAsRetAttr(AttrKind Kind)
Definition: Attributes.cpp:689
static bool isTypeAttrKind(AttrKind Kind)
Definition: Attributes.h:104
static bool canUseAsFnAttr(AttrKind Kind)
Definition: Attributes.cpp:681
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition: Attributes.h:85
@ None
No attributes have been set.
Definition: Attributes.h:87
static bool canUseAsParamAttr(AttrKind Kind)
Definition: Attributes.cpp:685
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator end()
Definition: BasicBlock.h:443
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:199
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a binary instruction, given the opcode and the two operands.
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Definition: Constants.cpp:1846
static BranchInst * Create(BasicBlock *IfTrue, BasicBlock::iterator InsertBefore)
void setCallingConv(CallingConv::ID CC)
Definition: InstrTypes.h:1804
void setAttributes(AttributeList A)
Set the parameter attributes for this call.
Definition: InstrTypes.h:1823
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1678
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, const Twine &NameStr, BasicBlock::iterator InsertBefore)
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr, BasicBlock::iterator InsertBefore)
void setTailCallKind(TailCallKind TCK)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name, BasicBlock::iterator InsertBefore)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
static 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, BasicBlock::iterator InsertBefore)
static CatchReturnInst * Create(Value *CatchPad, BasicBlock *BB, BasicBlock::iterator InsertBefore)
static CatchSwitchInst * Create(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumHandlers, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static CleanupPadInst * Create(Value *ParentPad, ArrayRef< Value * > Args, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static CleanupReturnInst * Create(Value *CleanupPad, BasicBlock *UnwindBB, BasicBlock::iterator InsertBefore)
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:996
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:1010
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:1001
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:1004
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:1002
@ ICMP_EQ
equal
Definition: InstrTypes.h:1014
@ ICMP_NE
not equal
Definition: InstrTypes.h:1015
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:1009
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:995
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:1003
@ Largest
The linker will choose the largest COMDAT.
Definition: Comdat.h:38
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition: Comdat.h:40
@ Any
The linker may choose any COMDAT.
Definition: Comdat.h:36
@ NoDeduplicate
No deduplication is performed.
Definition: Comdat.h:39
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition: Comdat.h:37
static Constant * get(ArrayType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1291
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:528
static Constant * getString(LLVMContext &Context, StringRef Initializer, bool AddNull=true)
This method constructs a CDS and initializes it with a text string.
Definition: Constants.cpp:2881
static Constant * getFCmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
Definition: Constants.cpp:2427
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2452
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
Definition: Constants.cpp:2041
static Constant * getInsertElement(Constant *Vec, Constant *Elt, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2474
static Constant * getICmp(unsigned short pred, Constant *LHS, Constant *RHS, bool OnlyIfReduced=false)
get* - Return some common constants without having to specify the full Instruction::OPCODE identifier...
Definition: Constants.cpp:2402
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, bool InBounds=false, std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition: Constants.h:1200
static Constant * getShuffleVector(Constant *V1, Constant *V2, ArrayRef< int > Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2497
static 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.
Definition: Constants.cpp:2159
static bool isValueValidForType(Type *Ty, const APFloat &V)
Return true if Ty is big enough to represent V.
Definition: Constants.cpp:1602
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:849
static ConstantInt * getSigned(IntegerType *Ty, int64_t V)
Return a ConstantInt with the specified value for the specified type.
Definition: Constants.h:123
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:856
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1775
This class represents a range of values.
Definition: ConstantRange.h:47
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
static Constant * get(StructType *T, ArrayRef< Constant * > V)
Definition: Constants.cpp:1356
static Constant * getSplat(ElementCount EC, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
Definition: Constants.cpp:1449
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1398
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
static DIArgList * get(LLVMContext &Context, ArrayRef< ValueAsMetadata * > Args)
static DIAssignID * getDistinct(LLVMContext &Context)
Basic type, like 'int' or 'float'.
Debug common block.
DebugEmissionKind getEmissionKind() const
DebugNameTableKind getNameTableKind() const
static DICompositeType * buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, Metadata *Rank, Metadata *Annotations)
Build a DICompositeType with the given ODR identifier.
Enumeration value.
DWARF expression.
static std::optional< ChecksumKind > getChecksumKind(StringRef CSKindStr)
ChecksumKind
Which algorithm (e.g.
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
Debug lexical block.
Debug location.
Represents a module in the programming language, for example, a Clang module, or a Fortran module.
Debug lexical block.
Tagged DWARF-like metadata node.
static DIFlags getFlag(StringRef Flag)
DIFlags
Debug info flags.
String type, Fortran CHARACTER(n)
Subprogram description.
static DISPFlags toSPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized, unsigned Virtuality=SPFlagNonvirtual, bool IsMainSubprogram=false)
static DISPFlags getFlag(StringRef Flag)
DISPFlags
Debug info subprogram flags.
Array subrange.
Type array for a subprogram.
static DSOLocalEquivalent * get(GlobalValue *GV)
Return a DSOLocalEquivalent for the specified global value.
Definition: Constants.cpp:1919
This class represents an Operation in the Expression.
static Expected< DataLayout > parse(StringRef LayoutDescription)
Parse a data layout string and return the layout.
Definition: DataLayout.cpp:223
static DbgLabelRecord * createUnresolvedDbgLabelRecord(MDNode *Label, MDNode *DL)
For use during parsing; creates a DbgLabelRecord from as-of-yet unresolved MDNodes.
Base class for non-instruction debug metadata records that have positions within IR.
Kind
Subclass discriminator.
static 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...
A debug info location.
Definition: DebugLoc.h:33
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:202
unsigned size() const
Definition: DenseMap.h:99
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition: TypeSize.h:296
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
reference get()
Returns a reference to the stored T value.
Definition: Error.h:571
Class representing an expression and its matching format.
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static bool isValidOperands(const Value *Vec, const Value *Idx)
Return true if an extractelement instruction can be formed with the specified operands.
static 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, BasicBlock::iterator InsertBefore)
This instruction compares its operands according to the predicate given to the constructor.
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
bool any() const
Definition: FMF.h:57
An instruction for ordering other memory operations.
Definition: Instructions.h:460
This class represents a freeze function that returns random concrete value if an operand is either a ...
std::pair< ValueInfo, CalleeInfo > EdgeTy
<CalleeValueInfo, CalleeInfo> call edge pair.
Type::subtype_iterator param_iterator
Definition: DerivedTypes.h:126
static bool isValidReturnType(Type *RetTy)
Return true if the specified type is valid as a return type.
Definition: Type.cpp:358
static 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:163
void setPrefixData(Constant *PrefixData)
Definition: Function.cpp:1918
static Intrinsic::ID lookupIntrinsicID(StringRef Name)
This does the actual lookup of an intrinsic ID which matches the given function name.
Definition: Function.cpp:908
void setGC(std::string Str)
Definition: Function.cpp:771
void setPersonalityFn(Constant *Fn)
Definition: Function.cpp:1908
arg_iterator arg_begin()
Definition: Function.h:814
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition: Function.h:342
void setPrologueData(Constant *PrologueData)
Definition: Function.cpp:1928
void setCallingConv(CallingConv::ID CC)
Definition: Function.h:267
Generic tagged DWARF-like metadata node.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static 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:95
static 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:525
static 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:582
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:128
void setComdat(Comdat *C)
Definition: Globals.cpp:197
void setSection(StringRef S)
Change the section for this global.
Definition: Globals.cpp:258
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1522
const SanitizerMetadata & getSanitizerMetadata() const
Definition: Globals.cpp:228
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:409
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:231
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:284
void setThreadLocalMode(ThreadLocalMode Val)
Definition: GlobalValue.h:267
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:537
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition: GlobalValue.h:73
@ DLLExportStorageClass
Function to be accessible from DLL.
Definition: GlobalValue.h:76
@ DLLImportStorageClass
Function to be imported from DLL.
Definition: GlobalValue.h:75
bool hasSanitizerMetadata() const
Definition: GlobalValue.h:355
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Definition: GlobalValue.h:595
void setDSOLocal(bool Local)
Definition: GlobalValue.h:303
void eraseFromParent()
This method unlinks 'this' from the containing module and deletes it.
Definition: Globals.cpp:86
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:66
@ DefaultVisibility
The GV is visible.
Definition: GlobalValue.h:67
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
@ ProtectedVisibility
The GV is protected.
Definition: GlobalValue.h:69
static bool isValidDeclarationLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:418
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:254
void setSanitizerMetadata(SanitizerMetadata Meta)
Definition: Globals.cpp:234
std::string getGlobalIdentifier() const
Return the modified name for this global value suitable to be used as the key for a global lookup (e....
Definition: Globals.cpp:169
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:51
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ CommonLinkage
Tentative definitions.
Definition: GlobalValue.h:62
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:54
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:57
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:56
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:58
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition: GlobalValue.h:53
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition: GlobalValue.h:61
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
Type * getValueType() const
Definition: GlobalValue.h:296
void setPartition(StringRef Part)
Definition: Globals.cpp:211
void setInitializer(Constant *InitVal)
setInitializer - Sets the initializer for this global variable, removing any existing initializer if ...
Definition: Globals.cpp:466
void setAttributes(AttributeSet A)
Set attribute list for this global.
void setConstant(bool Val)
void setCodeModel(CodeModel::Model CM)
Change the code model for this global.
Definition: Globals.cpp:502
void setExternallyInitialized(bool Val)
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
void addDestination(BasicBlock *Dest)
Add a destination.
static IndirectBrInst * Create(Value *Address, unsigned NumDests, BasicBlock::iterator InsertBefore)
static 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 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...
Definition: InlineAsm.cpp:273
static 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 InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr, BasicBlock::iterator InsertBefore)
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr, BasicBlock::iterator InsertBefore)
void setFastMathFlags(FastMathFlags FMF)
Convenience function for setting multiple fast-math flags on this instruction, which must be an opera...
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void setNonNeg(bool b=true)
Set or clear the nneg flag on this instruction, which must be a zext instruction.
bool isTerminator() const
Definition: Instruction.h:255
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1636
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.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
Invoke instruction.
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, BasicBlock::iterator InsertBefore)
lltok::Kind Lex()
Definition: LLLexer.h:52
unsigned getUIntVal() const
Definition: LLLexer.h:61
lltok::Kind getKind() const
Definition: LLLexer.h:58
bool Error(LocTy ErrorLoc, const Twine &Msg) const
Definition: LLLexer.cpp:28
const std::string & getStrVal() const
Definition: LLLexer.h:59
Type * getTyVal() const
Definition: LLLexer.h:60
LocTy getLoc() const
Definition: LLLexer.h:57
const APSInt & getAPSIntVal() const
Definition: LLLexer.h:62
void setIgnoreColonInIdentifiers(bool val)
Definition: LLLexer.h:65
const APFloat & getAPFloatVal() const
Definition: LLLexer.h:63
LLLexer::LocTy LocTy
Definition: LLParser.h:106
LLVMContext & getContext()
Definition: LLParser.h:204
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition: LLParser.cpp:127
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition: LLParser.cpp:114
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition: LLParser.cpp:95
bool shouldDiscardValueNames() const
Return true if the Context runtime configuration is set to discard all value names.
SyncScope::ID getOrInsertSyncScopeID(StringRef SSN)
getOrInsertSyncScopeID - Maps synchronization scope name to synchronization scope ID.
static LandingPadInst * Create(Type *RetTy, unsigned NumReservedClauses, const Twine &NameStr, BasicBlock::iterator InsertBefore)
Constructors - NumReservedClauses is a hint for the number of incoming clauses that this landingpad w...
An instruction for reading from memory.
Definition: Instructions.h:184
Metadata node.
Definition: Metadata.h:1067
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1549
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
A single uniqued string.
Definition: Metadata.h:720
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:600
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a distinct node.
Definition: Metadata.h:1509
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1498
static TempMDTuple getTemporary(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Return a temporary node.
Definition: Metadata.h:1518
static MemoryEffectsBase readOnly()
Create MemoryEffectsBase that can read any memory.
Definition: ModRef.h:122
MemoryEffectsBase getWithModRef(Location Loc, ModRefInfo MR) const
Get new MemoryEffectsBase with modified ModRefInfo for Loc.
Definition: ModRef.h:170
static MemoryEffectsBase argMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access argument memory.
Definition: ModRef.h:132
static MemoryEffectsBase inaccessibleMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible memory.
Definition: ModRef.h:138
static MemoryEffectsBase writeOnly()
Create MemoryEffectsBase that can write any memory.
Definition: ModRef.h:127
static MemoryEffectsBase inaccessibleOrArgMemOnly(ModRefInfo MR=ModRefInfo::ModRef)
Create MemoryEffectsBase that can only access inaccessible or argument memory.
Definition: ModRef.h:145
static MemoryEffectsBase none()
Create MemoryEffectsBase that cannot read or write any memory.
Definition: ModRef.h:117
static MemoryEffectsBase unknown()
Create MemoryEffectsBase that can read and write any memory.
Definition: ModRef.h:112
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:176
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:103
Root of the metadata hierarchy.
Definition: Metadata.h:62
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A tuple of MDNodes.
Definition: Metadata.h:1729
void addOperand(MDNode *M)
Definition: Metadata.cpp:1388
static NoCFIValue * get(GlobalValue *GV)
Return a NoCFIValue for the specified function.
Definition: Constants.cpp:1977
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, BasicBlock::iterator InsertBefore)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:764
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition: DerivedTypes.h:662
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1827
Resume the propagation of an exception.
static ResumeInst * Create(Value *Exn, BasicBlock::iterator InsertBefore)
static ReturnInst * Create(LLVMContext &C, Value *retVal, BasicBlock::iterator InsertBefore)
Represents a location in source code.
Definition: SMLoc.h:23
static 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, BasicBlock::iterator InsertBefore, Instruction *MDFrom=nullptr)
This instruction constructs a fixed permutation of two input vectors.
ArrayRef< int > getShuffleMask() const
static 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.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void push_back(const T &Elt)
Definition: SmallVector.h:426
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:299
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:317
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:127
iterator end()
Definition: StringMap.h:220
iterator find(StringRef Key)
Definition: StringMap.h:233
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
Class to represent struct types.
Definition: DerivedTypes.h:216
static 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:373
void setBody(ArrayRef< Type * > Elements, bool isPacked=false)
Specify a body for an opaque identified type.
Definition: Type.cpp:445
static StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition: Type.cpp:513
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:597
bool containsScalableVectorType(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Returns true if this struct contains a scalable vector.
Definition: Type.cpp:400
Multiway switch.
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, BasicBlock::iterator InsertBefore)
static TargetExtType * get(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types=std::nullopt, ArrayRef< unsigned > Ints=std::nullopt)
Return a target extension type having the specified name and optional type and integer parameters.
Definition: Type.cpp:796
@ HasZeroInit
zeroinitializer is valid for this target extension type.
Definition: DerivedTypes.h:769
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:252
bool isLabelTy() const
Return true if this is 'label'.
Definition: Type.h:219
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
static IntegerType * getInt1Ty(LLVMContext &C)
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:154
bool isBFloatTy() const
Return true if this is 'bfloat', a 16-bit bfloat type.
Definition: Type.h:146
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
static Type * getLabelTy(LLVMContext &C)
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value.
Definition: Type.h:281
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition: Type.h:295
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:143
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:129
static IntegerType * getInt8Ty(LLVMContext &C)
static Type * getTokenTy(LLVMContext &C)
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:185
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:262
bool isFunctionTy() const
True if this is an instance of FunctionType.
Definition: Type.h:246
bool isScalableTy() const
Return true if this is a type whose size is a known multiple of vscale.
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
bool isTokenTy() const
Return true if this is 'token'.
Definition: Type.h:225
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition: Type.h:216
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:140
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
bool isMetadataTy() const
Return true if this is 'metadata'.
Definition: Type.h:222
static UnaryOperator * Create(UnaryOps Op, Value *S, const Twine &Name, BasicBlock::iterator InsertBefore)
Construct a unary instruction, given the opcode and an operand.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
This function has undefined behavior.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:495
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
static constexpr uint64_t MaximumAlignment
Definition: Value.h:807
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
void deleteValue()
Delete a pointer to a generic Value.
Definition: Value.cpp:110
bool use_empty() const
Definition: Value.h:344
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:683
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:676
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition: ilist_node.h:109
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:660
std::string & str()
Returns the string's reference.
Definition: raw_ostream.h:678
unsigned getOperationEncoding(StringRef OperationEncodingString)
Definition: Dwarf.cpp:161
unsigned getAttributeEncoding(StringRef EncodingString)
Definition: Dwarf.cpp:242
unsigned getTag(StringRef TagString)
Definition: Dwarf.cpp:32
unsigned getCallingConvention(StringRef LanguageString)
Definition: Dwarf.cpp:439
unsigned getLanguage(StringRef LanguageString)
Definition: Dwarf.cpp:372
unsigned getVirtuality(StringRef VirtualityString)
Definition: Dwarf.cpp:353
unsigned getMacinfo(StringRef MacinfoString)
Definition: Dwarf.cpp:511
#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 TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
const CustomOperand< const MCSubtargetInfo & > Msg[]
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.
Definition: BitmaskEnum.h:121
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.
Definition: CallingConv.h:221
@ X86_64_SysV
The C convention as specified in the x86-64 supplement to the System V ABI, used on most non-Windows ...
Definition: CallingConv.h:151
@ RISCV_VectorCall
Calling convention used for RISC-V V-extension.
Definition: CallingConv.h:268
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
Definition: CallingConv.h:197
@ AMDGPU_VS
Used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (vertex shader if tess...
Definition: CallingConv.h:188
@ AVR_SIGNAL
Used for AVR signal routines.
Definition: CallingConv.h:179
@ Swift
Calling convention for Swift.
Definition: CallingConv.h:69
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
Definition: CallingConv.h:200
@ AArch64_SVE_VectorCall
Used between AArch64 SVE functions.
Definition: CallingConv.h:224
@ ARM_APCS
ARM Procedure Calling Standard (obsolete, but still used on some targets).
Definition: CallingConv.h:107
@ 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.
Definition: CallingConv.h:176
@ 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.
Definition: CallingConv.h:232
@ DUMMY_HHVM
Placeholders for HHVM calling conventions (deprecated, removed).
Definition: CallingConv.h:166
@ AMDGPU_CS_ChainPreserve
Used on AMDGPUs to give the middle-end more control over argument placement.
Definition: CallingConv.h:249
@ AMDGPU_HS
Used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
Definition: CallingConv.h:206
@ ARM_AAPCS
ARM Architecture Procedure Calling Standard calling convention (aka EABI).
Definition: CallingConv.h:111
@ AMDGPU_GS
Used for Mesa/AMDPAL geometry shaders.
Definition: CallingConv.h:191
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2
Preserve X2-X15, X19-X29, SP, Z0-Z31, P0-P15.
Definition: CallingConv.h:241
@ CXX_FAST_TLS
Used for access functions.
Definition: CallingConv.h:72
@ X86_INTR
x86 hardware interrupt context.
Definition: CallingConv.h:173
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0
Preserve X0-X13, X19-X29, SP, Z0-Z31, P0-P15.
Definition: CallingConv.h:238
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
Definition: CallingConv.h:245
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition: CallingConv.h:50
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
Definition: CallingConv.h:194
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition: CallingConv.h:47
@ X86_ThisCall
Similar to X86_StdCall.
Definition: CallingConv.h:122
@ PTX_Device
Call to a PTX device function.
Definition: CallingConv.h:129
@ SPIR_KERNEL
Used for SPIR kernel functions.
Definition: CallingConv.h:144
@ 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.
Definition: CallingConv.h:138
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition: CallingConv.h:41
@ MSP430_INTR
Used for MSP430 interrupt routines.
Definition: CallingConv.h:117
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
Definition: CallingConv.h:163
@ Intel_OCL_BI
Used for Intel OpenCL built-ins.
Definition: CallingConv.h:147
@ 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.
Definition: CallingConv.h:218
@ 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.
Definition: CallingConv.h:159
@ PTX_Kernel
Call to a PTX kernel. Passes all arguments in parameter space.
Definition: CallingConv.h:125
@ 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.
Definition: CallingConv.h:255
@ AMDGPU_LS
Used for AMDPAL vertex shader if tessellation is in use.
Definition: CallingConv.h:213
@ ARM_AAPCS_VFP
Same as ARM_AAPCS, but uses hard floating point ABI.
Definition: CallingConv.h:114
@ X86_RegCall
Register calling convention used for parameters transfer optimization.
Definition: CallingConv.h:203
@ M68k_RTD
Used for M68k rtd-based CC (similar to X86's stdcall).
Definition: CallingConv.h:252
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ X86_FastCall
'fast' analog of X86_StdCall.
Definition: CallingConv.h:103
bool getIntrinsicSignature(Intrinsic::ID, FunctionType *FT, SmallVectorImpl< Type * > &ArgTys)
Gets the type arguments of an intrinsic call by matching type contraints specified by the ....
Definition: Function.cpp:1749
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1465
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:148
@ System
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:57
@ FS
Definition: X86.h:206
@ GS
Definition: X86.h:205
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
@ DW_CC_hi_user
Definition: Dwarf.h:426
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
@ DW_ATE_hi_user
Definition: Dwarf.h:158
@ DW_LANG_hi_user
Definition: Dwarf.h:209
MacinfoRecordType
Definition: Dwarf.h:470
@ DW_MACINFO_vendor_ext
Definition: Dwarf.h:476
@ DW_VIRTUALITY_max
Definition: Dwarf.h:195
@ DW_TAG_hi_user
Definition: Dwarf.h:107
@ DW_TAG_invalid
LLVM mock tags (see also llvm/BinaryFormat/Dwarf.def).
Definition: Dwarf.h:47
@ DW_MACINFO_invalid
Macinfo type for invalid results.
Definition: Dwarf.h:49
@ DW_VIRTUALITY_invalid
Virtuality for invalid results.
Definition: Dwarf.h:48
@ kw_samesize
Definition: LLToken.h:235
@ kw_msp430_intrcc
Definition: LLToken.h:150
@ kw_acquire
Definition: LLToken.h:97
@ kw_cxx_fast_tlscc
Definition: LLToken.h:169
@ kw_extractvalue
Definition: LLToken.h:343
@ kw_dso_preemptable
Definition: LLToken.h:51
@ DwarfVirtuality
Definition: LLToken.h:477
@ kw_arm_apcscc
Definition: LLToken.h:143
@ kw_inteldialect
Definition: LLToken.h:126
@ kw_x86_stdcallcc
Definition: LLToken.h:138
@ kw_constant
Definition: LLToken.h:48
@ kw_graalcc
Definition: LLToken.h:183
@ kw_initialexec
Definition: LLToken.h:74
@ kw_mustBeUnreachable
Definition: LLToken.h:388
@ kw_internal
Definition: LLToken.h:54
@ kw_hhvm_ccc
Definition: LLToken.h:168
@ kw_ptrtoint
Definition: LLToken.h:305
@ kw_anyregcc
Definition: LLToken.h:159
@ kw_no_sanitize_hwaddress
Definition: LLToken.h:456
@ kw_win64cc
Definition: LLToken.h:158
@ kw_datalayout
Definition: LLToken.h:92
@ kw_wpdResolutions
Definition: LLToken.h:427
@ kw_cleanup
Definition: LLToken.h:313
@ kw_canAutoHide
Definition: LLToken.h:372
@ kw_alwaysInline
Definition: LLToken.h:384
@ kw_notail
Definition: LLToken.h:87
@ kw_insertelement
Definition: LLToken.h:340
@ kw_linkonce
Definition: LLToken.h:55
@ kw_fptrunc
Definition: LLToken.h:298
@ kw_inaccessiblememonly
Definition: LLToken.h:204
@ kw_amdgpu_gfx
Definition: LLToken.h:180
@ kw_getelementptr
Definition: LLToken.h:337
@ kw_m68k_rtdcc
Definition: LLToken.h:182
@ kw_preserve_nonecc
Definition: LLToken.h:164
@ kw_x86_fastcallcc
Definition: LLToken.h:139
@ kw_readOnly
Definition: LLToken.h:380
@ kw_varFlags
Definition: LLToken.h:441
@ kw_partition
Definition: LLToken.h:119
@ kw_visibility
Definition: LLToken.h:368
@ kw_vFuncId
Definition: LLToken.h:408
@ kw_noUnwind
Definition: LLToken.h:385
@ kw_disjoint
Definition: LLToken.h:113
@ kw_bitMask
Definition: LLToken.h:424
@ kw_unordered
Definition: LLToken.h:95
@ kw_singleImpl
Definition: LLToken.h:430
@ kw_swiftcc
Definition: LLToken.h:160
@ kw_localexec
Definition: LLToken.h:75
@ kw_cfguard_checkcc
Definition: LLToken.h:137
@ kw_stackIds
Definition: LLToken.h:445
@ kw_typeCheckedLoadConstVCalls
Definition: LLToken.h:407
@ kw_private
Definition: LLToken.h:53
@ kw_aarch64_sve_vector_pcs
Definition: LLToken.h:147
@ kw_amdgpu_kernel
Definition: LLToken.h:179
@ kw_acq_rel
Definition: LLToken.h:99
@ kw_uselistorder
Definition: LLToken.h:355
@ MetadataVar
Definition: LLToken.h:473
@ kw_blockcount
Definition: LLToken.h:366
@ kw_notEligibleToImport
Definition: LLToken.h:369
@ kw_noRecurse
Definition: LLToken.h:381
@ kw_dsoLocal
Definition: LLToken.h:371
@ kw_linkonce_odr
Definition: LLToken.h:56
@ kw_protected
Definition: LLToken.h:66
@ kw_variable
Definition: LLToken.h:397
@ kw_dllexport
Definition: LLToken.h:61
@ kw_hotness
Definition: LLToken.h:393
@ kw_x86_vectorcallcc
Definition: LLToken.h:141
@ kw_ptx_device
Definition: LLToken.h:154
@ kw_personality
Definition: LLToken.h:312
@ kw_catchpad
Definition: LLToken.h:327
@ kw_spir_func
Definition: LLToken.h:156
@ kw_inbounds
Definition: LLToken.h:114
@ kw_atomic
Definition: LLToken.h:94
@ kw_readNone
Definition: LLToken.h:379
@ kw_declaration
Definition: LLToken.h:375
@ kw_define
Definition: LLToken.h:46
@ DwarfAttEncoding
Definition: LLToken.h:476
@ kw_critical
Definition: LLToken.h:395
@ kw_external
Definition: LLToken.h:71
@ kw_largest
Definition: LLToken.h:233
@ kw_amdgpu_hs
Definition: LLToken.h:172
@ kw_spir_kernel
Definition: LLToken.h:155
@ kw_local_unnamed_addr
Definition: LLToken.h:68
@ kw_amdgpu_es
Definition: LLToken.h:173
@ kw_hasUnknownCall
Definition: LLToken.h:387
@ LocalVarID
Definition: LLToken.h:464
@ kw_seq_cst
Definition: LLToken.h:100
@ kw_unwind
Definition: LLToken.h:91
@ kw_distinct
Definition: LLToken.h:352
@ kw_linkage
Definition: LLToken.h:367
@ kw_amdgpu_gs
Definition: LLToken.h:174
@ kw_x86_intrcc
Definition: LLToken.h:166
@ kw_addrspacecast
Definition: LLToken.h:307
@ kw_callsites
Definition: LLToken.h:443
@ kw_zeroinitializer
Definition: LLToken.h:76
@ StringConstant
Definition: LLToken.h:474
@ kw_x86_thiscallcc
Definition: LLToken.h:140
@ kw_false
Definition: LLToken.h:44
@ kw_unnamed_addr
Definition: LLToken.h:67
@ kw_uselistorder_bb
Definition: LLToken.h:356
@ NameTableKind
Definition: LLToken.h:481
@ kw_amdgpu_vs
Definition: LLToken.h:170
@ kw_inlineBits
Definition: LLToken.h:425
@ kw_weak_odr
Definition: LLToken.h:58
@ kw_udec_wrap
Definition: LLToken.h:268
@ kw_resByArg
Definition: LLToken.h:433
@ kw_inttoptr
Definition: LLToken.h:304
@ kw_dllimport
Definition: LLToken.h:60
@ kw_argmemonly
Definition: LLToken.h:203
@ kw_blockaddress
Definition: LLToken.h:345
@ kw_landingpad
Definition: LLToken.h:311
@ kw_aarch64_vector_pcs
Definition: LLToken.h:146
@ kw_amdgpu_cs
Definition: LLToken.h:176
@ kw_syncscope
Definition: LLToken.h:101
@ kw_noInline
Definition: LLToken.h:383
@ kw_source_filename
Definition: LLToken.h:90
@ kw_typeTestAssumeConstVCalls
Definition: LLToken.h:406
@ kw_inrange
Definition: LLToken.h:116
@ kw_ptx_kernel
Definition: LLToken.h:153
@ kw_summaries
Definition: LLToken.h:364
@ kw_extractelement
Definition: LLToken.h:339
@ kw_branchFunnel
Definition: LLToken.h:431
@ kw_typeidCompatibleVTable
Definition: LLToken.h:412
@ kw_bitcast
Definition: LLToken.h:306
@ kw_declare
Definition: LLToken.h:45
@ kw_allOnes
Definition: LLToken.h:420
@ kw_vTableFuncs
Definition: LLToken.h:398
@ ChecksumKind
Definition: LLToken.h:486
@ DwarfMacinfo
Definition: LLToken.h:485
@ kw_volatile
Definition: LLToken.h:93
@ kw_typeCheckedLoadVCalls
Definition: LLToken.h:405
@ kw_function
Definition: LLToken.h:376
@ kw_default
Definition: LLToken.h:64
@ kw_no_sanitize_address
Definition: LLToken.h:453
@ kw_inaccessiblemem_or_argmemonly
Definition: LLToken.h:205
@ kw_uinc_wrap
Definition: LLToken.h:267
@ kw_externally_initialized
Definition: LLToken.h:69
@ kw_sanitize_address_dyninit
Definition: LLToken.h:459
@ kw_atomicrmw
Definition: LLToken.h:336
@ kw_hidden
Definition: LLToken.h:65
@ EmissionKind
Definition: LLToken.h:480
@ kw_amdgpu_cs_chain_preserve
Definition: LLToken.h:178
@ kw_readwrite
Definition: LLToken.h:198
@ kw_within
Definition: LLToken.h:83
@ kw_section
Definition: LLToken.h:118
@ kw_triple
Definition: LLToken.h:89
@ kw_thread_local
Definition: LLToken.h:72
@ kw_catchswitch
Definition: LLToken.h:325
@ kw_extern_weak
Definition: LLToken.h:70
@ kw_arm_aapcscc
Definition: LLToken.h:144
@ kw_memProf
Definition: LLToken.h:448
@ kw_alignLog2
Definition: LLToken.h:422
@ kw_cleanuppad
Definition: LLToken.h:328
@ kw_available_externally
Definition: LLToken.h:63
@ kw_singleImplName
Definition: LLToken.h:432
@ kw_typeTests
Definition: LLToken.h:403
@ kw_versions
Definition: LLToken.h:447
@ kw_notcold
Definition: LLToken.h:449
@ kw_mayThrow
Definition: LLToken.h:386
@ kw_swifttailcc
Definition: LLToken.h:161
@ kw_monotonic
Definition: LLToken.h:96
@ kw_typeTestAssumeVCalls
Definition: LLToken.h:404
@ kw_amdgpu_ls
Definition: LLToken.h:171
@ kw_caller
Definition: LLToken.h:82
@ kw_vscale
Definition: LLToken.h:41
@ kw_target
Definition: LLToken.h:88
@ kw_attributes
Definition: LLToken.h:187
@ kw_code_model
Definition: LLToken.h:120
@ kw_cmpxchg
Definition: LLToken.h:335
@ kw_funcFlags
Definition: LLToken.h:378
@ kw_localdynamic
Definition: LLToken.h:73
@ kw_uniformRetVal
Definition: LLToken.h:435
@ kw_sideeffect
Definition: LLToken.h:125
@ kw_amdgpu_ps
Definition: LLToken.h:175
@ kw_sizeM1BitWidth
Definition: LLToken.h:421
@ kw_catchret
Definition: LLToken.h:326
@ kw_nodeduplicate
Definition: LLToken.h:234
@ kw_avr_signalcc
Definition: LLToken.h:152
@ kw_exactmatch
Definition: LLToken.h:232
@ kw_aliasee
Definition: LLToken.h:400
@ kw_common
Definition: LLToken.h:62
@ kw_unreachable
Definition: LLToken.h:323
@ kw_intel_ocl_bicc
Definition: LLToken.h:136
@ kw_global
Definition: LLToken.h:47
@ kw_dso_local
Definition: LLToken.h:50
@ kw_undef
Definition: LLToken.h:77
@ kw_addrspace
Definition: LLToken.h:117
@ kw_release
Definition: LLToken.h:98
@ kw_returnDoesNotAlias
Definition: LLToken.h:382
@ kw_aarch64_sme_preservemost_from_x0
Definition: LLToken.h:148
@ kw_preserve_allcc
Definition: LLToken.h:163
@ kw_importType
Definition: LLToken.h:373
@ dotdotdot
Definition: LLToken.h:24
@ kw_cleanupret
Definition: LLToken.h:324
@ kw_shufflevector
Definition: LLToken.h:341
@ kw_riscv_vector_cc
Definition: LLToken.h:184
@ kw_avr_intrcc
Definition: LLToken.h:151
@ kw_definition
Definition: LLToken.h:374
@ kw_prologue
Definition: LLToken.h:129
@ kw_virtualConstProp
Definition: LLToken.h:437
@ kw_vcall_visibility
Definition: LLToken.h:426
@ kw_poison
Definition: LLToken.h:78
@ kw_appending
Definition: LLToken.h:59
@ kw_inaccessiblemem
Definition: LLToken.h:200
@ kw_preserve_mostcc
Definition: LLToken.h:162
@ kw_arm_aapcs_vfpcc
Definition: LLToken.h:145
@ kw_typeTestRes
Definition: LLToken.h:414
@ kw_unknown
Definition: LLToken.h:394
@ kw_x86_regcallcc
Definition: LLToken.h:142
@ kw_typeIdInfo
Definition: LLToken.h:402
@ kw_amdgpu_cs_chain
Definition: LLToken.h:177
@ kw_dso_local_equivalent
Definition: LLToken.h:346
@ kw_x86_64_sysvcc
Definition: LLToken.h:157
@ DbgRecordType
Definition: LLToken.h:487
@ kw_summary
Definition: LLToken.h:413
@ kw_virtFunc
Definition: LLToken.h:399
@ kw_musttail
Definition: LLToken.h:86
@ kw_aarch64_sme_preservemost_from_x2
Definition: LLToken.h:149
@ kw_byteArray
Definition: LLToken.h:417
@ kw_uniqueRetVal
Definition: LLToken.h:436
@ kw_insertvalue
Definition: LLToken.h:344
@ kw_indirectbr
Definition: LLToken.h:320
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:578
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
void UpgradeSectionAttributes(Module &M)
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:1680
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
@ Done
Definition: Threading.h:61
AllocFnKind
Definition: Attributes.h:48
void UpgradeCallsToIntrinsic(Function *F)
This is an auto-upgrade hook for any old intrinsic function syntaxes which need to have both the func...
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:656
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:280
std::vector< VirtFuncOffset > VTableFuncList
List of functions referenced by a particular vtable definition.
UWTableKind
Definition: CodeGen.h:120
@ Async
"Asynchronous" unwind tables (instr precise)
@ Sync
"Synchronous" unwind tables
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:275
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition: ModRef.h:268
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:116
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Ref
The access may reference the value stored in memory.
@ ModRef
The access may reference and may modify the value stored in memory.
@ Mod
The access may modify the value stored in memory.
@ NoModRef
The access neither references nor modifies the value stored in memory.
@ ArgMem
Access to memory via argument pointers.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ InaccessibleMem
Memory that is inaccessible via LLVM IR.
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:1914
DWARFExpression::Operation Op
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
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:1849
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
MDNode * UpgradeTBAANode(MDNode &TBAANode)
If the given TBAA tag uses the scalar TBAA format, create a new node corresponding to the upgrade to ...
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
#define N
static const fltSemantics & IEEEsingle() LLVM_READNONE
Definition: APFloat.cpp:249
static constexpr roundingMode rmNearestTiesToEven
Definition: APFloat.h:230
static const fltSemantics & IEEEdouble() LLVM_READNONE
Definition: APFloat.cpp:250
static const fltSemantics & IEEEhalf() LLVM_READNONE
Definition: APFloat.cpp:247
static const fltSemantics & BFloat() LLVM_READNONE
Definition: APFloat.cpp:248
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Class to accumulate and hold information about a callee.
Helper object to track which of three possible relocation mechanisms are used for a particular value ...
A specification for a virtual function call with all constant integer arguments.
Flags specific to function summaries.
Describes the use of a value in a call instruction, specifying the call's target, the value's paramet...
Describes the uses of a parameter by the function.
std::vector< Call > Calls
In the per-module summary, it summarizes the byte offset applied to each pointer parameter before pas...
static constexpr uint32_t RangeWidth
All type identifier related information.
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....
An "identifier" for a virtual function.
Group flags (Linkage, NotEligibleToImport, etc.) as a bitfield.
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:117
A utility class that uses RAII to save and restore the value of a variable.
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:33
std::map< unsigned, Type * > Types
Definition: SlotMapping.h:37
StringMap< Type * > NamedTypes
Definition: SlotMapping.h:36
std::map< unsigned, TrackingMDNodeRef > MetadataNodes
Definition: SlotMapping.h:35
NumberedValues< GlobalValue * > GlobalValues
Definition: SlotMapping.h:34
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:53
unsigned UIntVal
Definition: LLParser.h:75
@ t_Constant
Definition: LLParser.h:67
@ t_PackedConstantStruct
Definition: LLParser.h:71
@ t_GlobalID
Definition: LLParser.h:56
@ t_EmptyArray
Definition: LLParser.h:66
@ t_GlobalName
Definition: LLParser.h:58
@ t_ConstantStruct
Definition: LLParser.h:70
@ t_LocalName
Definition: LLParser.h:57
@ t_ConstantSplat
Definition: LLParser.h:68
@ t_InlineAsm
Definition: LLParser.h:69
FunctionType * FTy
Definition: LLParser.h:76
LLLexer::LocTy Loc
Definition: LLParser.h:74
enum llvm::ValID::@36 Kind
std::string StrVal
Definition: LLParser.h:77
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.
Utility type to build an inheritance chain that makes it easy to rank overload candidates.
Definition: STLExtras.h:1479