LCOV - code coverage report
Current view: top level - lib/AsmParser - LLParser.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 3438 4146 82.9 %
Date: 2018-10-20 13:21:21 Functions: 267 302 88.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- LLParser.cpp - Parser Class ---------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : //  This file defines the parser class for .ll files.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "LLParser.h"
      15             : #include "llvm/ADT/DenseMap.h"
      16             : #include "llvm/ADT/None.h"
      17             : #include "llvm/ADT/Optional.h"
      18             : #include "llvm/ADT/STLExtras.h"
      19             : #include "llvm/ADT/SmallPtrSet.h"
      20             : #include "llvm/AsmParser/SlotMapping.h"
      21             : #include "llvm/BinaryFormat/Dwarf.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"
      27             : #include "llvm/IR/Constants.h"
      28             : #include "llvm/IR/DebugInfoMetadata.h"
      29             : #include "llvm/IR/DerivedTypes.h"
      30             : #include "llvm/IR/Function.h"
      31             : #include "llvm/IR/GlobalIFunc.h"
      32             : #include "llvm/IR/GlobalObject.h"
      33             : #include "llvm/IR/InlineAsm.h"
      34             : #include "llvm/IR/Instruction.h"
      35             : #include "llvm/IR/Instructions.h"
      36             : #include "llvm/IR/Intrinsics.h"
      37             : #include "llvm/IR/LLVMContext.h"
      38             : #include "llvm/IR/Metadata.h"
      39             : #include "llvm/IR/Module.h"
      40             : #include "llvm/IR/Operator.h"
      41             : #include "llvm/IR/Type.h"
      42             : #include "llvm/IR/Value.h"
      43             : #include "llvm/IR/ValueSymbolTable.h"
      44             : #include "llvm/Support/Casting.h"
      45             : #include "llvm/Support/ErrorHandling.h"
      46             : #include "llvm/Support/MathExtras.h"
      47             : #include "llvm/Support/SaveAndRestore.h"
      48             : #include "llvm/Support/raw_ostream.h"
      49             : #include <algorithm>
      50             : #include <cassert>
      51             : #include <cstring>
      52             : #include <iterator>
      53             : #include <vector>
      54             : 
      55             : using namespace llvm;
      56             : 
      57          79 : static std::string getTypeString(Type *T) {
      58             :   std::string Result;
      59          79 :   raw_string_ostream Tmp(Result);
      60             :   Tmp << *T;
      61          79 :   return Tmp.str();
      62             : }
      63             : 
      64             : /// Run: module ::= toplevelentity*
      65       34600 : bool LLParser::Run() {
      66             :   // Prime the lexer.
      67       34600 :   Lex.Lex();
      68             : 
      69       34600 :   if (Context.shouldDiscardValueNames())
      70           0 :     return Error(
      71             :         Lex.getLoc(),
      72             :         "Can't read textual IR with a Context that discards named Values");
      73             : 
      74       68963 :   return ParseTopLevelEntities() || ValidateEndOfModule() ||
      75       34363 :          ValidateEndOfIndex();
      76             : }
      77             : 
      78        1042 : bool LLParser::parseStandaloneConstantValue(Constant *&C,
      79             :                                             const SlotMapping *Slots) {
      80        1042 :   restoreParsingState(Slots);
      81        1042 :   Lex.Lex();
      82             : 
      83        1042 :   Type *Ty = nullptr;
      84        1042 :   if (ParseType(Ty) || parseConstantValue(Ty, C))
      85           5 :     return true;
      86        1037 :   if (Lex.getKind() != lltok::Eof)
      87           1 :     return Error(Lex.getLoc(), "expected end of string");
      88             :   return false;
      89             : }
      90             : 
      91          22 : bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
      92             :                                     const SlotMapping *Slots) {
      93          22 :   restoreParsingState(Slots);
      94          22 :   Lex.Lex();
      95             : 
      96          22 :   Read = 0;
      97          22 :   SMLoc Start = Lex.getLoc();
      98          22 :   Ty = nullptr;
      99          22 :   if (ParseType(Ty))
     100             :     return true;
     101          22 :   SMLoc End = Lex.getLoc();
     102          22 :   Read = End.getPointer() - Start.getPointer();
     103             : 
     104          22 :   return false;
     105             : }
     106             : 
     107        1064 : void LLParser::restoreParsingState(const SlotMapping *Slots) {
     108        1064 :   if (!Slots)
     109             :     return;
     110        1025 :   NumberedVals = Slots->GlobalValues;
     111             :   NumberedMetadata = Slots->MetadataNodes;
     112        2105 :   for (const auto &I : Slots->NamedTypes)
     113             :     NamedTypes.insert(
     114          55 :         std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
     115        1049 :   for (const auto &I : Slots->Types)
     116             :     NumberedTypes.insert(
     117          24 :         std::make_pair(I.first, std::make_pair(I.second, LocTy())));
     118             : }
     119             : 
     120             : /// ValidateEndOfModule - Do final validity and sanity checks at the end of the
     121             : /// module.
     122       34370 : bool LLParser::ValidateEndOfModule() {
     123       34370 :   if (!M)
     124             :     return false;
     125             :   // Handle any function attribute group forward references.
     126      513645 :   for (const auto &RAG : ForwardRefAttrGroups) {
     127      479275 :     Value *V = RAG.first;
     128             :     const std::vector<unsigned> &Attrs = RAG.second;
     129             :     AttrBuilder B;
     130             : 
     131      538515 :     for (const auto &Attr : Attrs)
     132       59240 :       B.merge(NumberedAttrBuilders[Attr]);
     133             : 
     134             :     if (Function *Fn = dyn_cast<Function>(V)) {
     135      323200 :       AttributeList AS = Fn->getAttributes();
     136      323200 :       AttrBuilder FnAttrs(AS.getFnAttributes());
     137      323200 :       AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
     138             : 
     139      323200 :       FnAttrs.merge(B);
     140             : 
     141             :       // If the alignment was parsed as an attribute, move to the alignment
     142             :       // field.
     143      323200 :       if (FnAttrs.hasAlignmentAttr()) {
     144           0 :         Fn->setAlignment(FnAttrs.getAlignment());
     145           0 :         FnAttrs.removeAttribute(Attribute::Alignment);
     146             :       }
     147             : 
     148             :       AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
     149      646400 :                             AttributeSet::get(Context, FnAttrs));
     150             :       Fn->setAttributes(AS);
     151             :     } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
     152      154044 :       AttributeList AS = CI->getAttributes();
     153      154044 :       AttrBuilder FnAttrs(AS.getFnAttributes());
     154      154044 :       AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
     155      154044 :       FnAttrs.merge(B);
     156             :       AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
     157      308088 :                             AttributeSet::get(Context, FnAttrs));
     158             :       CI->setAttributes(AS);
     159             :     } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
     160        1995 :       AttributeList AS = II->getAttributes();
     161        1995 :       AttrBuilder FnAttrs(AS.getFnAttributes());
     162        1995 :       AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
     163        1995 :       FnAttrs.merge(B);
     164             :       AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
     165        3990 :                             AttributeSet::get(Context, FnAttrs));
     166             :       II->setAttributes(AS);
     167             :     } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
     168          36 :       AttrBuilder Attrs(GV->getAttributes());
     169          36 :       Attrs.merge(B);
     170          36 :       GV->setAttributes(AttributeSet::get(Context,Attrs));
     171             :     } else {
     172           0 :       llvm_unreachable("invalid object with forward attribute group reference");
     173             :     }
     174             :   }
     175             : 
     176             :   // If there are entries in ForwardRefBlockAddresses at this point, the
     177             :   // function was never defined.
     178       34370 :   if (!ForwardRefBlockAddresses.empty())
     179           0 :     return Error(ForwardRefBlockAddresses.begin()->first.Loc,
     180             :                  "expected function name in blockaddress");
     181             : 
     182       34956 :   for (const auto &NT : NumberedTypes)
     183         586 :     if (NT.second.second.isValid())
     184           0 :       return Error(NT.second.second,
     185           0 :                    "use of undefined type '%" + Twine(NT.first) + "'");
     186             : 
     187             :   for (StringMap<std::pair<Type*, LocTy> >::iterator I =
     188       80908 :        NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
     189       12170 :     if (I->second.second.isValid())
     190           2 :       return Error(I->second.second,
     191           4 :                    "use of undefined type named '" + I->getKey() + "'");
     192             : 
     193       34368 :   if (!ForwardRefComdats.empty())
     194           1 :     return Error(ForwardRefComdats.begin()->second,
     195           1 :                  "use of undefined comdat '$" +
     196           2 :                      ForwardRefComdats.begin()->first + "'");
     197             : 
     198       34367 :   if (!ForwardRefVals.empty())
     199           1 :     return Error(ForwardRefVals.begin()->second.second,
     200           2 :                  "use of undefined value '@" + ForwardRefVals.begin()->first +
     201             :                  "'");
     202             : 
     203       34366 :   if (!ForwardRefValIDs.empty())
     204           0 :     return Error(ForwardRefValIDs.begin()->second.second,
     205           0 :                  "use of undefined value '@" +
     206           0 :                  Twine(ForwardRefValIDs.begin()->first) + "'");
     207             : 
     208       34366 :   if (!ForwardRefMDNodes.empty())
     209           3 :     return Error(ForwardRefMDNodes.begin()->second.second,
     210           3 :                  "use of undefined metadata '!" +
     211           6 :                  Twine(ForwardRefMDNodes.begin()->first) + "'");
     212             : 
     213             :   // Resolve metadata cycles.
     214       92126 :   for (auto &N : NumberedMetadata) {
     215       57763 :     if (N.second && !N.second->isResolved())
     216         184 :       N.second->resolveCycles();
     217             :   }
     218             : 
     219       38923 :   for (auto *Inst : InstsWithTBAATag) {
     220             :     MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
     221             :     assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
     222        4560 :     auto *UpgradedMD = UpgradeTBAANode(*MD);
     223        4560 :     if (MD != UpgradedMD)
     224         198 :       Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
     225             :   }
     226             : 
     227             :   // Look for intrinsic functions and CallInst that need to be upgraded
     228      359110 :   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
     229      324747 :     UpgradeCallsToIntrinsic(&*FI++); // must be post-increment, as we remove
     230             : 
     231             :   // Some types could be renamed during loading if several modules are
     232             :   // loaded in the same LLVMContext (LTO scenario). In this case we should
     233             :   // remangle intrinsics names as well.
     234      354070 :   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) {
     235             :     Function *F = &*FI++;
     236      319707 :     if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
     237           0 :       F->replaceAllUsesWith(Remangled.getValue());
     238           0 :       F->eraseFromParent();
     239             :     }
     240             :   }
     241             : 
     242       34363 :   if (UpgradeDebugInfo)
     243       13313 :     llvm::UpgradeDebugInfo(*M);
     244             : 
     245       34363 :   UpgradeModuleFlags(*M);
     246       34363 :   UpgradeSectionAttributes(*M);
     247             : 
     248       34363 :   if (!Slots)
     249             :     return false;
     250             :   // Initialize the slot mapping.
     251             :   // Because by this point we've parsed and validated everything, we can "steal"
     252             :   // the mapping from LLParser as it doesn't need it anymore.
     253         903 :   Slots->GlobalValues = std::move(NumberedVals);
     254         903 :   Slots->MetadataNodes = std::move(NumberedMetadata);
     255        1869 :   for (const auto &I : NamedTypes)
     256         126 :     Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
     257         906 :   for (const auto &I : NumberedTypes)
     258           3 :     Slots->Types.insert(std::make_pair(I.first, I.second.first));
     259             : 
     260             :   return false;
     261             : }
     262             : 
     263             : /// Do final validity and sanity checks at the end of the index.
     264       34363 : bool LLParser::ValidateEndOfIndex() {
     265       34363 :   if (!Index)
     266             :     return false;
     267             : 
     268        1022 :   if (!ForwardRefValueInfos.empty())
     269           0 :     return Error(ForwardRefValueInfos.begin()->second.front().second,
     270           0 :                  "use of undefined summary '^" +
     271           0 :                      Twine(ForwardRefValueInfos.begin()->first) + "'");
     272             : 
     273        1022 :   if (!ForwardRefAliasees.empty())
     274           0 :     return Error(ForwardRefAliasees.begin()->second.front().second,
     275           0 :                  "use of undefined summary '^" +
     276           0 :                      Twine(ForwardRefAliasees.begin()->first) + "'");
     277             : 
     278        1022 :   if (!ForwardRefTypeIds.empty())
     279           0 :     return Error(ForwardRefTypeIds.begin()->second.front().second,
     280           0 :                  "use of undefined type id summary '^" +
     281           0 :                      Twine(ForwardRefTypeIds.begin()->first) + "'");
     282             : 
     283             :   return false;
     284             : }
     285             : 
     286             : //===----------------------------------------------------------------------===//
     287             : // Top-Level Entities
     288             : //===----------------------------------------------------------------------===//
     289             : 
     290       34600 : bool LLParser::ParseTopLevelEntities() {
     291             :   // If there is no Module, then parse just the summary index entries.
     292       34600 :   if (!M) {
     293             :     while (true) {
     294           0 :       switch (Lex.getKind()) {
     295             :       case lltok::Eof:
     296             :         return false;
     297           0 :       case lltok::SummaryID:
     298           0 :         if (ParseSummaryEntry())
     299             :           return true;
     300             :         break;
     301           0 :       case lltok::kw_source_filename:
     302           0 :         if (ParseSourceFileName())
     303             :           return true;
     304             :         break;
     305           0 :       default:
     306             :         // Skip everything else
     307           0 :         Lex.Lex();
     308             :       }
     309             :     }
     310             :   }
     311             :   while (true) {
     312      489201 :     switch (Lex.getKind()) {
     313           4 :     default:         return TokError("expected top-level entity");
     314             :     case lltok::Eof: return false;
     315       66400 :     case lltok::kw_declare: if (ParseDeclare()) return true; break;
     316      256915 :     case lltok::kw_define:  if (ParseDefine()) return true; break;
     317         344 :     case lltok::kw_module:  if (ParseModuleAsm()) return true; break;
     318       13761 :     case lltok::kw_target:  if (ParseTargetDefinition()) return true; break;
     319        1624 :     case lltok::kw_source_filename:
     320        1624 :       if (ParseSourceFileName())
     321             :         return true;
     322             :       break;
     323           0 :     case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
     324         586 :     case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
     325       12186 :     case lltok::LocalVar:   if (ParseNamedType()) return true; break;
     326         688 :     case lltok::GlobalID:   if (ParseUnnamedGlobal()) return true; break;
     327       28449 :     case lltok::GlobalVar:  if (ParseNamedGlobal()) return true; break;
     328         739 :     case lltok::ComdatVar:  if (parseComdat()) return true; break;
     329       57832 :     case lltok::exclaim:    if (ParseStandaloneMetadata()) return true; break;
     330         130 :     case lltok::SummaryID:
     331         130 :       if (ParseSummaryEntry())
     332             :         return true;
     333             :       break;
     334        5698 :     case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
     335        9338 :     case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
     336         125 :     case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break;
     337          12 :     case lltok::kw_uselistorder_bb:
     338          12 :       if (ParseUseListOrderBB())
     339             :         return true;
     340             :       break;
     341             :     }
     342             :   }
     343             : }
     344             : 
     345             : /// toplevelentity
     346             : ///   ::= 'module' 'asm' STRINGCONSTANT
     347         344 : bool LLParser::ParseModuleAsm() {
     348             :   assert(Lex.getKind() == lltok::kw_module);
     349         344 :   Lex.Lex();
     350             : 
     351             :   std::string AsmStr;
     352         688 :   if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
     353         344 :       ParseStringConstant(AsmStr)) return true;
     354             : 
     355         688 :   M->appendModuleInlineAsm(AsmStr);
     356         344 :   return false;
     357             : }
     358             : 
     359             : /// toplevelentity
     360             : ///   ::= 'target' 'triple' '=' STRINGCONSTANT
     361             : ///   ::= 'target' 'datalayout' '=' STRINGCONSTANT
     362       13761 : bool LLParser::ParseTargetDefinition() {
     363             :   assert(Lex.getKind() == lltok::kw_target);
     364             :   std::string Str;
     365       13761 :   switch (Lex.Lex()) {
     366           0 :   default: return TokError("unknown target property");
     367        5647 :   case lltok::kw_triple:
     368             :     Lex.Lex();
     369       11294 :     if (ParseToken(lltok::equal, "expected '=' after target triple") ||
     370        5647 :         ParseStringConstant(Str))
     371           0 :       return true;
     372       11294 :     M->setTargetTriple(Str);
     373        5647 :     return false;
     374        8114 :   case lltok::kw_datalayout:
     375             :     Lex.Lex();
     376       16228 :     if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
     377        8114 :         ParseStringConstant(Str))
     378           0 :       return true;
     379        8114 :     if (DataLayoutStr.empty())
     380       16224 :       M->setDataLayout(Str);
     381             :     return false;
     382             :   }
     383             : }
     384             : 
     385             : /// toplevelentity
     386             : ///   ::= 'source_filename' '=' STRINGCONSTANT
     387        1624 : bool LLParser::ParseSourceFileName() {
     388             :   assert(Lex.getKind() == lltok::kw_source_filename);
     389        1624 :   Lex.Lex();
     390        3248 :   if (ParseToken(lltok::equal, "expected '=' after source_filename") ||
     391        1624 :       ParseStringConstant(SourceFileName))
     392           0 :     return true;
     393        1624 :   if (M)
     394        1624 :     M->setSourceFileName(SourceFileName);
     395             :   return false;
     396             : }
     397             : 
     398             : /// toplevelentity
     399             : ///   ::= 'deplibs' '=' '[' ']'
     400             : ///   ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
     401             : /// FIXME: Remove in 4.0. Currently parse, but ignore.
     402           0 : bool LLParser::ParseDepLibs() {
     403             :   assert(Lex.getKind() == lltok::kw_deplibs);
     404           0 :   Lex.Lex();
     405           0 :   if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
     406           0 :       ParseToken(lltok::lsquare, "expected '=' after deplibs"))
     407           0 :     return true;
     408             : 
     409             :   if (EatIfPresent(lltok::rsquare))
     410           0 :     return false;
     411             : 
     412             :   do {
     413             :     std::string Str;
     414           0 :     if (ParseStringConstant(Str)) return true;
     415             :   } while (EatIfPresent(lltok::comma));
     416             : 
     417           0 :   return ParseToken(lltok::rsquare, "expected ']' at end of list");
     418             : }
     419             : 
     420             : /// ParseUnnamedType:
     421             : ///   ::= LocalVarID '=' 'type' type
     422         586 : bool LLParser::ParseUnnamedType() {
     423         586 :   LocTy TypeLoc = Lex.getLoc();
     424         586 :   unsigned TypeID = Lex.getUIntVal();
     425         586 :   Lex.Lex(); // eat LocalVarID;
     426             : 
     427        1172 :   if (ParseToken(lltok::equal, "expected '=' after name") ||
     428         586 :       ParseToken(lltok::kw_type, "expected 'type' after '='"))
     429           0 :     return true;
     430             : 
     431         586 :   Type *Result = nullptr;
     432         586 :   if (ParseStructDefinition(TypeLoc, "",
     433         586 :                             NumberedTypes[TypeID], Result)) return true;
     434             : 
     435        1172 :   if (!isa<StructType>(Result)) {
     436           4 :     std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
     437           4 :     if (Entry.first)
     438           0 :       return Error(TypeLoc, "non-struct types may not be recursive");
     439           4 :     Entry.first = Result;
     440           4 :     Entry.second = SMLoc();
     441             :   }
     442             : 
     443             :   return false;
     444             : }
     445             : 
     446             : /// toplevelentity
     447             : ///   ::= LocalVar '=' 'type' type
     448       12186 : bool LLParser::ParseNamedType() {
     449             :   std::string Name = Lex.getStrVal();
     450       12186 :   LocTy NameLoc = Lex.getLoc();
     451       12186 :   Lex.Lex();  // eat LocalVar.
     452             : 
     453       24372 :   if (ParseToken(lltok::equal, "expected '=' after name") ||
     454       12186 :       ParseToken(lltok::kw_type, "expected 'type' after name"))
     455           0 :     return true;
     456             : 
     457       12186 :   Type *Result = nullptr;
     458       12186 :   if (ParseStructDefinition(NameLoc, Name,
     459             :                             NamedTypes[Name], Result)) return true;
     460             : 
     461       24372 :   if (!isa<StructType>(Result)) {
     462         448 :     std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
     463         448 :     if (Entry.first)
     464           0 :       return Error(NameLoc, "non-struct types may not be recursive");
     465         448 :     Entry.first = Result;
     466         448 :     Entry.second = SMLoc();
     467             :   }
     468             : 
     469             :   return false;
     470             : }
     471             : 
     472             : /// toplevelentity
     473             : ///   ::= 'declare' FunctionHeader
     474       66400 : bool LLParser::ParseDeclare() {
     475             :   assert(Lex.getKind() == lltok::kw_declare);
     476       66400 :   Lex.Lex();
     477             : 
     478             :   std::vector<std::pair<unsigned, MDNode *>> MDs;
     479       66437 :   while (Lex.getKind() == lltok::MetadataVar) {
     480             :     unsigned MDK;
     481             :     MDNode *N;
     482          37 :     if (ParseMetadataAttachment(MDK, N))
     483           0 :       return true;
     484          37 :     MDs.push_back({MDK, N});
     485             :   }
     486             : 
     487             :   Function *F;
     488       66400 :   if (ParseFunctionHeader(F, false))
     489             :     return true;
     490       66429 :   for (auto &MD : MDs)
     491          37 :     F->addMetadata(MD.first, *MD.second);
     492             :   return false;
     493             : }
     494             : 
     495             : /// toplevelentity
     496             : ///   ::= 'define' FunctionHeader (!dbg !56)* '{' ...
     497      256915 : bool LLParser::ParseDefine() {
     498             :   assert(Lex.getKind() == lltok::kw_define);
     499      256915 :   Lex.Lex();
     500             : 
     501             :   Function *F;
     502      513824 :   return ParseFunctionHeader(F, true) ||
     503      513824 :          ParseOptionalFunctionMetadata(*F) ||
     504      256909 :          ParseFunctionBody(*F);
     505             : }
     506             : 
     507             : /// ParseGlobalType
     508             : ///   ::= 'constant'
     509             : ///   ::= 'global'
     510       28109 : bool LLParser::ParseGlobalType(bool &IsConstant) {
     511       28109 :   if (Lex.getKind() == lltok::kw_constant)
     512        4901 :     IsConstant = true;
     513       23208 :   else if (Lex.getKind() == lltok::kw_global)
     514       23208 :     IsConstant = false;
     515             :   else {
     516           0 :     IsConstant = false;
     517           0 :     return TokError("expected 'global' or 'constant'");
     518             :   }
     519       28109 :   Lex.Lex();
     520       28109 :   return false;
     521             : }
     522             : 
     523      352442 : bool LLParser::ParseOptionalUnnamedAddr(
     524             :     GlobalVariable::UnnamedAddr &UnnamedAddr) {
     525             :   if (EatIfPresent(lltok::kw_unnamed_addr))
     526        3277 :     UnnamedAddr = GlobalValue::UnnamedAddr::Global;
     527             :   else if (EatIfPresent(lltok::kw_local_unnamed_addr))
     528        5456 :     UnnamedAddr = GlobalValue::UnnamedAddr::Local;
     529             :   else
     530      343709 :     UnnamedAddr = GlobalValue::UnnamedAddr::None;
     531      352442 :   return false;
     532             : }
     533             : 
     534             : /// ParseUnnamedGlobal:
     535             : ///   OptionalVisibility (ALIAS | IFUNC) ...
     536             : ///   OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
     537             : ///   OptionalDLLStorageClass
     538             : ///                                                     ...   -> global variable
     539             : ///   GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
     540             : ///   GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
     541             : ///                OptionalDLLStorageClass
     542             : ///                                                     ...   -> global variable
     543         688 : bool LLParser::ParseUnnamedGlobal() {
     544        1376 :   unsigned VarID = NumberedVals.size();
     545             :   std::string Name;
     546         688 :   LocTy NameLoc = Lex.getLoc();
     547             : 
     548             :   // Handle the GlobalID form.
     549         688 :   if (Lex.getKind() == lltok::GlobalID) {
     550         688 :     if (Lex.getUIntVal() != VarID)
     551           0 :       return Error(Lex.getLoc(), "variable expected to be numbered '%" +
     552           0 :                    Twine(VarID) + "'");
     553         688 :     Lex.Lex(); // eat GlobalID;
     554             : 
     555         688 :     if (ParseToken(lltok::equal, "expected '=' after name"))
     556             :       return true;
     557             :   }
     558             : 
     559             :   bool HasLinkage;
     560             :   unsigned Linkage, Visibility, DLLStorageClass;
     561             :   bool DSOLocal;
     562             :   GlobalVariable::ThreadLocalMode TLM;
     563             :   GlobalVariable::UnnamedAddr UnnamedAddr;
     564         688 :   if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
     565         688 :                            DSOLocal) ||
     566        1376 :       ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
     567           0 :     return true;
     568             : 
     569         688 :   if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
     570         684 :     return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
     571         684 :                        DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     572             : 
     573           4 :   return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
     574           4 :                              DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     575             : }
     576             : 
     577             : /// ParseNamedGlobal:
     578             : ///   GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
     579             : ///   GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
     580             : ///                 OptionalVisibility OptionalDLLStorageClass
     581             : ///                                                     ...   -> global variable
     582       28449 : bool LLParser::ParseNamedGlobal() {
     583             :   assert(Lex.getKind() == lltok::GlobalVar);
     584       28449 :   LocTy NameLoc = Lex.getLoc();
     585             :   std::string Name = Lex.getStrVal();
     586       28449 :   Lex.Lex();
     587             : 
     588             :   bool HasLinkage;
     589             :   unsigned Linkage, Visibility, DLLStorageClass;
     590             :   bool DSOLocal;
     591             :   GlobalVariable::ThreadLocalMode TLM;
     592             :   GlobalVariable::UnnamedAddr UnnamedAddr;
     593       56897 :   if (ParseToken(lltok::equal, "expected '=' in global variable") ||
     594       28448 :       ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
     595       28448 :                            DSOLocal) ||
     596       85345 :       ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
     597           1 :     return true;
     598             : 
     599       28448 :   if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
     600       27429 :     return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
     601       27429 :                        DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     602             : 
     603        1019 :   return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
     604        1019 :                              DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     605             : }
     606             : 
     607         739 : bool LLParser::parseComdat() {
     608             :   assert(Lex.getKind() == lltok::ComdatVar);
     609             :   std::string Name = Lex.getStrVal();
     610         739 :   LocTy NameLoc = Lex.getLoc();
     611         739 :   Lex.Lex();
     612             : 
     613         739 :   if (ParseToken(lltok::equal, "expected '=' here"))
     614             :     return true;
     615             : 
     616         739 :   if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
     617           0 :     return TokError("expected comdat type");
     618             : 
     619             :   Comdat::SelectionKind SK;
     620         739 :   switch (Lex.getKind()) {
     621             :   default:
     622           0 :     return TokError("unknown selection kind");
     623             :   case lltok::kw_any:
     624             :     SK = Comdat::Any;
     625             :     break;
     626             :   case lltok::kw_exactmatch:
     627             :     SK = Comdat::ExactMatch;
     628             :     break;
     629             :   case lltok::kw_largest:
     630             :     SK = Comdat::Largest;
     631             :     break;
     632             :   case lltok::kw_noduplicates:
     633             :     SK = Comdat::NoDuplicates;
     634             :     break;
     635             :   case lltok::kw_samesize:
     636             :     SK = Comdat::SameSize;
     637             :     break;
     638             :   }
     639             :   Lex.Lex();
     640             : 
     641             :   // See if the comdat was forward referenced, if so, use the comdat.
     642         739 :   Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
     643         739 :   Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
     644        1480 :   if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
     645           3 :     return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
     646             : 
     647             :   Comdat *C;
     648        1476 :   if (I != ComdatSymTab.end())
     649           1 :     C = &I->second;
     650             :   else
     651        1474 :     C = M->getOrInsertComdat(Name);
     652             :   C->setSelectionKind(SK);
     653             : 
     654         738 :   return false;
     655             : }
     656             : 
     657             : // MDString:
     658             : //   ::= '!' STRINGCONSTANT
     659       12954 : bool LLParser::ParseMDString(MDString *&Result) {
     660             :   std::string Str;
     661       12954 :   if (ParseStringConstant(Str)) return true;
     662       12954 :   Result = MDString::get(Context, Str);
     663       12954 :   return false;
     664             : }
     665             : 
     666             : // MDNode:
     667             : //   ::= '!' MDNodeNumber
     668      121941 : bool LLParser::ParseMDNodeID(MDNode *&Result) {
     669             :   // !{ ..., !42, ... }
     670      121941 :   LocTy IDLoc = Lex.getLoc();
     671      121941 :   unsigned MID = 0;
     672      121941 :   if (ParseUInt32(MID))
     673             :     return true;
     674             : 
     675             :   // If not a forward reference, just return it now.
     676             :   if (NumberedMetadata.count(MID)) {
     677       65754 :     Result = NumberedMetadata[MID];
     678       65754 :     return false;
     679             :   }
     680             : 
     681             :   // Otherwise, create MDNode forward reference.
     682       56187 :   auto &FwdRef = ForwardRefMDNodes[MID];
     683       56187 :   FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), IDLoc);
     684             : 
     685       56187 :   Result = FwdRef.first.get();
     686       56187 :   NumberedMetadata[MID].reset(Result);
     687       56187 :   return false;
     688             : }
     689             : 
     690             : /// ParseNamedMetadata:
     691             : ///   !foo = !{ !1, !2 }
     692        5698 : bool LLParser::ParseNamedMetadata() {
     693             :   assert(Lex.getKind() == lltok::MetadataVar);
     694             :   std::string Name = Lex.getStrVal();
     695        5698 :   Lex.Lex();
     696             : 
     697       11396 :   if (ParseToken(lltok::equal, "expected '=' here") ||
     698       11396 :       ParseToken(lltok::exclaim, "Expected '!' here") ||
     699        5698 :       ParseToken(lltok::lbrace, "Expected '{' here"))
     700           0 :     return true;
     701             : 
     702       11396 :   NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
     703        5698 :   if (Lex.getKind() != lltok::rbrace)
     704             :     do {
     705       10354 :       MDNode *N = nullptr;
     706             :       // Parse DIExpressions inline as a special case. They are still MDNodes,
     707             :       // so they can still appear in named metadata. Remove this logic if they
     708             :       // become plain Metadata.
     709       10394 :       if (Lex.getKind() == lltok::MetadataVar &&
     710             :           Lex.getStrVal() == "DIExpression") {
     711          40 :         if (ParseDIExpression(N, /*IsDistinct=*/false))
     712           0 :           return true;
     713       20628 :       } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
     714       10314 :                  ParseMDNodeID(N)) {
     715           0 :         return true;
     716             :       }
     717       10354 :       NMD->addOperand(N);
     718             :     } while (EatIfPresent(lltok::comma));
     719             : 
     720        5698 :   return ParseToken(lltok::rbrace, "expected end of metadata node");
     721             : }
     722             : 
     723             : /// ParseStandaloneMetadata:
     724             : ///   !42 = !{...}
     725       57832 : bool LLParser::ParseStandaloneMetadata() {
     726             :   assert(Lex.getKind() == lltok::exclaim);
     727       57832 :   Lex.Lex();
     728       57832 :   unsigned MetadataID = 0;
     729             : 
     730             :   MDNode *Init;
     731      115664 :   if (ParseUInt32(MetadataID) ||
     732       57832 :       ParseToken(lltok::equal, "expected '=' here"))
     733           0 :     return true;
     734             : 
     735             :   // Detect common error, from old metadata syntax.
     736       57832 :   if (Lex.getKind() == lltok::Type)
     737           1 :     return TokError("unexpected type in metadata definition");
     738             : 
     739             :   bool IsDistinct = EatIfPresent(lltok::kw_distinct);
     740       57831 :   if (Lex.getKind() == lltok::MetadataVar) {
     741       37255 :     if (ParseSpecializedMDNode(Init, IsDistinct))
     742             :       return true;
     743       41152 :   } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
     744       20576 :              ParseMDTuple(Init, IsDistinct))
     745           3 :     return true;
     746             : 
     747             :   // See if this was forward referenced, if so, handle it.
     748             :   auto FI = ForwardRefMDNodes.find(MetadataID);
     749       57778 :   if (FI != ForwardRefMDNodes.end()) {
     750       56182 :     FI->second.first->replaceAllUsesWith(Init);
     751             :     ForwardRefMDNodes.erase(FI);
     752             : 
     753             :     assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
     754             :   } else {
     755             :     if (NumberedMetadata.count(MetadataID))
     756           0 :       return TokError("Metadata id is already used");
     757        1596 :     NumberedMetadata[MetadataID].reset(Init);
     758             :   }
     759             : 
     760             :   return false;
     761             : }
     762             : 
     763             : // Skips a single module summary entry.
     764           6 : bool LLParser::SkipModuleSummaryEntry() {
     765             :   // Each module summary entry consists of a tag for the entry
     766             :   // type, followed by a colon, then the fields surrounded by nested sets of
     767             :   // parentheses. The "tag:" looks like a Label. Once parsing support is
     768             :   // in place we will look for the tokens corresponding to the expected tags.
     769           6 :   if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module &&
     770             :       Lex.getKind() != lltok::kw_typeid)
     771           1 :     return TokError(
     772             :         "Expected 'gv', 'module', or 'typeid' at the start of summary entry");
     773           5 :   Lex.Lex();
     774          10 :   if (ParseToken(lltok::colon, "expected ':' at start of summary entry") ||
     775           5 :       ParseToken(lltok::lparen, "expected '(' at start of summary entry"))
     776           1 :     return true;
     777             :   // Now walk through the parenthesized entry, until the number of open
     778             :   // parentheses goes back down to 0 (the first '(' was parsed above).
     779             :   unsigned NumOpenParen = 1;
     780             :   do {
     781          94 :     switch (Lex.getKind()) {
     782           6 :     case lltok::lparen:
     783           6 :       NumOpenParen++;
     784           6 :       break;
     785           9 :     case lltok::rparen:
     786           9 :       NumOpenParen--;
     787           9 :       break;
     788             :     case lltok::Eof:
     789           1 :       return TokError("found end of file while parsing summary entry");
     790             :     default:
     791             :       // Skip everything in between parentheses.
     792             :       break;
     793             :     }
     794             :     Lex.Lex();
     795          93 :   } while (NumOpenParen > 0);
     796             :   return false;
     797             : }
     798             : 
     799             : /// SummaryEntry
     800             : ///   ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
     801         130 : bool LLParser::ParseSummaryEntry() {
     802             :   assert(Lex.getKind() == lltok::SummaryID);
     803         130 :   unsigned SummaryID = Lex.getUIntVal();
     804             : 
     805             :   // For summary entries, colons should be treated as distinct tokens,
     806             :   // not an indication of the end of a label token.
     807             :   Lex.setIgnoreColonInIdentifiers(true);
     808             : 
     809         130 :   Lex.Lex();
     810         130 :   if (ParseToken(lltok::equal, "expected '=' here"))
     811             :     return true;
     812             : 
     813             :   // If we don't have an index object, skip the summary entry.
     814         130 :   if (!Index)
     815           6 :     return SkipModuleSummaryEntry();
     816             : 
     817         124 :   switch (Lex.getKind()) {
     818          96 :   case lltok::kw_gv:
     819          96 :     return ParseGVEntry(SummaryID);
     820          21 :   case lltok::kw_module:
     821          21 :     return ParseModuleEntry(SummaryID);
     822           7 :   case lltok::kw_typeid:
     823           7 :     return ParseTypeIdEntry(SummaryID);
     824             :     break;
     825             :   default:
     826           0 :     return Error(Lex.getLoc(), "unexpected summary kind");
     827             :   }
     828             :   Lex.setIgnoreColonInIdentifiers(false);
     829             :   return false;
     830             : }
     831             : 
     832             : static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
     833        5715 :   return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
     834             :          (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
     835             : }
     836             : 
     837             : // If there was an explicit dso_local, update GV. In the absence of an explicit
     838             : // dso_local we keep the default value.
     839             : static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
     840       29107 :   if (DSOLocal)
     841             :     GV.setDSOLocal(true);
     842             : }
     843             : 
     844             : /// parseIndirectSymbol:
     845             : ///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
     846             : ///                     OptionalVisibility OptionalDLLStorageClass
     847             : ///                     OptionalThreadLocal OptionalUnnamedAddr
     848             : //                      'alias|ifunc' IndirectSymbol
     849             : ///
     850             : /// IndirectSymbol
     851             : ///   ::= TypeAndValue
     852             : ///
     853             : /// Everything through OptionalUnnamedAddr has already been parsed.
     854             : ///
     855        1023 : bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
     856             :                                    unsigned L, unsigned Visibility,
     857             :                                    unsigned DLLStorageClass, bool DSOLocal,
     858             :                                    GlobalVariable::ThreadLocalMode TLM,
     859             :                                    GlobalVariable::UnnamedAddr UnnamedAddr) {
     860             :   bool IsAlias;
     861        1023 :   if (Lex.getKind() == lltok::kw_alias)
     862             :     IsAlias = true;
     863          41 :   else if (Lex.getKind() == lltok::kw_ifunc)
     864             :     IsAlias = false;
     865             :   else
     866           0 :     llvm_unreachable("Not an alias or ifunc!");
     867        1023 :   Lex.Lex();
     868             : 
     869             :   GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
     870             : 
     871        1023 :   if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
     872           0 :     return Error(NameLoc, "invalid linkage type for alias");
     873             : 
     874             :   if (!isValidVisibilityForLinkage(Visibility, L))
     875           4 :     return Error(NameLoc,
     876             :                  "symbol with local linkage must have default visibility");
     877             : 
     878             :   Type *Ty;
     879        1019 :   LocTy ExplicitTypeLoc = Lex.getLoc();
     880        2038 :   if (ParseType(Ty) ||
     881        1019 :       ParseToken(lltok::comma, "expected comma after alias or ifunc's type"))
     882           0 :     return true;
     883             : 
     884             :   Constant *Aliasee;
     885        1019 :   LocTy AliaseeLoc = Lex.getLoc();
     886         864 :   if (Lex.getKind() != lltok::kw_bitcast &&
     887         753 :       Lex.getKind() != lltok::kw_getelementptr &&
     888        1761 :       Lex.getKind() != lltok::kw_addrspacecast &&
     889             :       Lex.getKind() != lltok::kw_inttoptr) {
     890         734 :     if (ParseGlobalTypeAndValue(Aliasee))
     891             :       return true;
     892             :   } else {
     893             :     // The bitcast dest type is not present, it is implied by the dest type.
     894         285 :     ValID ID;
     895         285 :     if (ParseValID(ID))
     896           1 :       return true;
     897         284 :     if (ID.Kind != ValID::t_Constant)
     898           0 :       return Error(AliaseeLoc, "invalid aliasee");
     899         284 :     Aliasee = ID.ConstantVal;
     900             :   }
     901             : 
     902        1018 :   Type *AliaseeType = Aliasee->getType();
     903             :   auto *PTy = dyn_cast<PointerType>(AliaseeType);
     904             :   if (!PTy)
     905           0 :     return Error(AliaseeLoc, "An alias or ifunc must have pointer type");
     906             :   unsigned AddrSpace = PTy->getAddressSpace();
     907             : 
     908        1018 :   if (IsAlias && Ty != PTy->getElementType())
     909           1 :     return Error(
     910             :         ExplicitTypeLoc,
     911             :         "explicit pointee type doesn't match operand's pointee type");
     912             : 
     913        1017 :   if (!IsAlias && !PTy->getElementType()->isFunctionTy())
     914           0 :     return Error(
     915             :         ExplicitTypeLoc,
     916             :         "explicit pointee type should be a function type");
     917             : 
     918             :   GlobalValue *GVal = nullptr;
     919             : 
     920             :   // See if the alias was forward referenced, if so, prepare to replace the
     921             :   // forward reference.
     922        1017 :   if (!Name.empty()) {
     923        2026 :     GVal = M->getNamedValue(Name);
     924        1013 :     if (GVal) {
     925          66 :       if (!ForwardRefVals.erase(Name))
     926           3 :         return Error(NameLoc, "redefinition of global '@" + Name + "'");
     927             :     }
     928             :   } else {
     929           8 :     auto I = ForwardRefValIDs.find(NumberedVals.size());
     930           4 :     if (I != ForwardRefValIDs.end()) {
     931           1 :       GVal = I->second.first;
     932             :       ForwardRefValIDs.erase(I);
     933             :     }
     934             :   }
     935             : 
     936             :   // Okay, create the alias but do not insert it into the module yet.
     937             :   std::unique_ptr<GlobalIndirectSymbol> GA;
     938        1016 :   if (IsAlias)
     939        1950 :     GA.reset(GlobalAlias::create(Ty, AddrSpace,
     940             :                                  (GlobalValue::LinkageTypes)Linkage, Name,
     941             :                                  Aliasee, /*Parent*/ nullptr));
     942             :   else
     943          82 :     GA.reset(GlobalIFunc::create(Ty, AddrSpace,
     944             :                                  (GlobalValue::LinkageTypes)Linkage, Name,
     945             :                                  Aliasee, /*Parent*/ nullptr));
     946             :   GA->setThreadLocalMode(TLM);
     947             :   GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
     948             :   GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
     949             :   GA->setUnnamedAddr(UnnamedAddr);
     950             :   maybeSetDSOLocal(DSOLocal, *GA);
     951             : 
     952        1016 :   if (Name.empty())
     953           4 :     NumberedVals.push_back(GA.get());
     954             : 
     955        1016 :   if (GVal) {
     956             :     // Verify that types agree.
     957          66 :     if (GVal->getType() != GA->getType())
     958           0 :       return Error(
     959             :           ExplicitTypeLoc,
     960             :           "forward reference and definition of alias have different types");
     961             : 
     962             :     // If they agree, just RAUW the old value with the alias and remove the
     963             :     // forward ref info.
     964          66 :     GVal->replaceAllUsesWith(GA.get());
     965          66 :     GVal->eraseFromParent();
     966             :   }
     967             : 
     968             :   // Insert into the module, we know its name won't collide now.
     969        1016 :   if (IsAlias)
     970         975 :     M->getAliasList().push_back(cast<GlobalAlias>(GA.get()));
     971             :   else
     972          41 :     M->getIFuncList().push_back(cast<GlobalIFunc>(GA.get()));
     973             :   assert(GA->getName() == Name && "Should not be a name conflict!");
     974             : 
     975             :   // The module owns this now
     976             :   GA.release();
     977             : 
     978             :   return false;
     979             : }
     980             : 
     981             : /// ParseGlobal
     982             : ///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
     983             : ///       OptionalVisibility OptionalDLLStorageClass
     984             : ///       OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
     985             : ///       OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
     986             : ///   ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
     987             : ///       OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
     988             : ///       OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
     989             : ///       Const OptionalAttrs
     990             : ///
     991             : /// Everything up to and including OptionalUnnamedAddr has been parsed
     992             : /// already.
     993             : ///
     994       28113 : bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
     995             :                            unsigned Linkage, bool HasLinkage,
     996             :                            unsigned Visibility, unsigned DLLStorageClass,
     997             :                            bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
     998             :                            GlobalVariable::UnnamedAddr UnnamedAddr) {
     999             :   if (!isValidVisibilityForLinkage(Visibility, Linkage))
    1000           4 :     return Error(NameLoc,
    1001             :                  "symbol with local linkage must have default visibility");
    1002             : 
    1003             :   unsigned AddrSpace;
    1004             :   bool IsConstant, IsExternallyInitialized;
    1005             :   LocTy IsExternallyInitializedLoc;
    1006             :   LocTy TyLoc;
    1007             : 
    1008       28109 :   Type *Ty = nullptr;
    1009       28109 :   if (ParseOptionalAddrSpace(AddrSpace) ||
    1010             :       ParseOptionalToken(lltok::kw_externally_initialized,
    1011             :                          IsExternallyInitialized,
    1012       28109 :                          &IsExternallyInitializedLoc) ||
    1013       84327 :       ParseGlobalType(IsConstant) ||
    1014             :       ParseType(Ty, TyLoc))
    1015           2 :     return true;
    1016             : 
    1017             :   // If the linkage is specified and is external, then no initializer is
    1018             :   // present.
    1019       28107 :   Constant *Init = nullptr;
    1020       28107 :   if (!HasLinkage ||
    1021             :       !GlobalValue::isValidDeclarationLinkage(
    1022             :           (GlobalValue::LinkageTypes)Linkage)) {
    1023       21294 :     if (ParseGlobalValue(Ty, Init))
    1024             :       return true;
    1025             :   }
    1026             : 
    1027       56190 :   if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
    1028           2 :     return Error(TyLoc, "invalid type for global variable");
    1029             : 
    1030             :   GlobalValue *GVal = nullptr;
    1031             : 
    1032             :   // See if the global was forward referenced, if so, use the global.
    1033       28093 :   if (!Name.empty()) {
    1034       54830 :     GVal = M->getNamedValue(Name);
    1035       27415 :     if (GVal) {
    1036         454 :       if (!ForwardRefVals.erase(Name))
    1037           3 :         return Error(NameLoc, "redefinition of global '@" + Name + "'");
    1038             :     }
    1039             :   } else {
    1040        1356 :     auto I = ForwardRefValIDs.find(NumberedVals.size());
    1041         678 :     if (I != ForwardRefValIDs.end()) {
    1042          22 :       GVal = I->second.first;
    1043             :       ForwardRefValIDs.erase(I);
    1044             :     }
    1045             :   }
    1046             : 
    1047             :   GlobalVariable *GV;
    1048       27436 :   if (!GVal) {
    1049       27617 :     GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
    1050             :                             Name, nullptr, GlobalVariable::NotThreadLocal,
    1051       27617 :                             AddrSpace);
    1052             :   } else {
    1053         475 :     if (GVal->getValueType() != Ty)
    1054           1 :       return Error(TyLoc,
    1055             :             "forward reference and definition of global have different types");
    1056             : 
    1057             :     GV = cast<GlobalVariable>(GVal);
    1058             : 
    1059             :     // Move the forward-reference to the correct spot in the module.
    1060         948 :     M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
    1061             :   }
    1062             : 
    1063       28091 :   if (Name.empty())
    1064         678 :     NumberedVals.push_back(GV);
    1065             : 
    1066             :   // Set the parsed properties on the global.
    1067       28091 :   if (Init)
    1068       21279 :     GV->setInitializer(Init);
    1069       28091 :   GV->setConstant(IsConstant);
    1070             :   GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
    1071             :   maybeSetDSOLocal(DSOLocal, *GV);
    1072             :   GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
    1073             :   GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
    1074             :   GV->setExternallyInitialized(IsExternallyInitialized);
    1075             :   GV->setThreadLocalMode(TLM);
    1076             :   GV->setUnnamedAddr(UnnamedAddr);
    1077             : 
    1078             :   // Parse attributes on the global.
    1079       45992 :   while (Lex.getKind() == lltok::comma) {
    1080       17901 :     Lex.Lex();
    1081             : 
    1082       17901 :     if (Lex.getKind() == lltok::kw_section) {
    1083             :       Lex.Lex();
    1084        1126 :       GV->setSection(Lex.getStrVal());
    1085         563 :       if (ParseToken(lltok::StringConstant, "expected global section string"))
    1086             :         return true;
    1087       17338 :     } else if (Lex.getKind() == lltok::kw_align) {
    1088             :       unsigned Alignment;
    1089       15407 :       if (ParseOptionalAlignment(Alignment)) return true;
    1090       15407 :       GV->setAlignment(Alignment);
    1091        1931 :     } else if (Lex.getKind() == lltok::MetadataVar) {
    1092        1502 :       if (ParseGlobalObjectMetadataAttachment(*GV))
    1093             :         return true;
    1094             :     } else {
    1095             :       Comdat *C;
    1096         429 :       if (parseOptionalComdat(Name, C))
    1097           0 :         return true;
    1098         429 :       if (C)
    1099             :         GV->setComdat(C);
    1100             :       else
    1101           0 :         return TokError("unknown global variable property!");
    1102             :     }
    1103             :   }
    1104             : 
    1105             :   AttrBuilder Attrs;
    1106       28091 :   LocTy BuiltinLoc;
    1107             :   std::vector<unsigned> FwdRefAttrGrps;
    1108       28091 :   if (ParseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
    1109             :     return true;
    1110       28091 :   if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
    1111          36 :     GV->setAttributes(AttributeSet::get(Context, Attrs));
    1112          36 :     ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
    1113             :   }
    1114             : 
    1115             :   return false;
    1116             : }
    1117             : 
    1118             : /// ParseUnnamedAttrGrp
    1119             : ///   ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
    1120        9338 : bool LLParser::ParseUnnamedAttrGrp() {
    1121             :   assert(Lex.getKind() == lltok::kw_attributes);
    1122        9338 :   LocTy AttrGrpLoc = Lex.getLoc();
    1123        9338 :   Lex.Lex();
    1124             : 
    1125        9338 :   if (Lex.getKind() != lltok::AttrGrpID)
    1126           1 :     return TokError("expected attribute group id");
    1127             : 
    1128        9337 :   unsigned VarID = Lex.getUIntVal();
    1129             :   std::vector<unsigned> unused;
    1130        9337 :   LocTy BuiltinLoc;
    1131             :   Lex.Lex();
    1132             : 
    1133       18674 :   if (ParseToken(lltok::equal, "expected '=' here") ||
    1134       18674 :       ParseToken(lltok::lbrace, "expected '{' here") ||
    1135        9337 :       ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
    1136       18674 :                                  BuiltinLoc) ||
    1137        9337 :       ParseToken(lltok::rbrace, "expected end of attribute group"))
    1138           0 :     return true;
    1139             : 
    1140        9337 :   if (!NumberedAttrBuilders[VarID].hasAttributes())
    1141           0 :     return Error(AttrGrpLoc, "attribute group has no attributes");
    1142             : 
    1143             :   return false;
    1144             : }
    1145             : 
    1146             : /// ParseFnAttributeValuePairs
    1147             : ///   ::= <attr> | <attr> '=' <value>
    1148      516793 : bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
    1149             :                                           std::vector<unsigned> &FwdRefAttrGrps,
    1150             :                                           bool inAttrGrp, LocTy &BuiltinLoc) {
    1151             :   bool HaveError = false;
    1152             : 
    1153      516793 :   B.clear();
    1154             : 
    1155             :   while (true) {
    1156      727024 :     lltok::Kind Token = Lex.getKind();
    1157      727024 :     if (Token == lltok::kw_builtin)
    1158          56 :       BuiltinLoc = Lex.getLoc();
    1159      727024 :     switch (Token) {
    1160      507454 :     default:
    1161      507454 :       if (!inAttrGrp) return HaveError;
    1162           0 :       return Error(Lex.getLoc(), "unterminated attribute group");
    1163             :     case lltok::rbrace:
    1164             :       // Finished.
    1165             :       return false;
    1166             : 
    1167       59243 :     case lltok::AttrGrpID: {
    1168             :       // Allow a function to reference an attribute group:
    1169             :       //
    1170             :       //   define void @foo() #1 { ... }
    1171       59243 :       if (inAttrGrp)
    1172           0 :         HaveError |=
    1173           0 :           Error(Lex.getLoc(),
    1174             :               "cannot have an attribute group reference in an attribute group");
    1175             : 
    1176       59243 :       unsigned AttrGrpNum = Lex.getUIntVal();
    1177       59243 :       if (inAttrGrp) break;
    1178             : 
    1179             :       // Save the reference to the attribute group. We'll fill it in later.
    1180       59243 :       FwdRefAttrGrps.push_back(AttrGrpNum);
    1181       59243 :       break;
    1182             :     }
    1183             :     // Target-dependent attributes:
    1184       18725 :     case lltok::StringConstant: {
    1185       18725 :       if (ParseStringAttribute(B))
    1186             :         return true;
    1187             :       continue;
    1188             :     }
    1189             : 
    1190             :     // Target-independent attributes:
    1191         637 :     case lltok::kw_align: {
    1192             :       // As a hack, we allow function alignment to be initially parsed as an
    1193             :       // attribute on a function declaration/definition or added to an attribute
    1194             :       // group and later moved to the alignment field.
    1195             :       unsigned Alignment;
    1196         637 :       if (inAttrGrp) {
    1197           1 :         Lex.Lex();
    1198           2 :         if (ParseToken(lltok::equal, "expected '=' here") ||
    1199           1 :             ParseUInt32(Alignment))
    1200           0 :           return true;
    1201             :       } else {
    1202         636 :         if (ParseOptionalAlignment(Alignment))
    1203             :           return true;
    1204             :       }
    1205         637 :       B.addAlignmentAttr(Alignment);
    1206         637 :       continue;
    1207             :     }
    1208          48 :     case lltok::kw_alignstack: {
    1209             :       unsigned Alignment;
    1210          48 :       if (inAttrGrp) {
    1211          14 :         Lex.Lex();
    1212          28 :         if (ParseToken(lltok::equal, "expected '=' here") ||
    1213          14 :             ParseUInt32(Alignment))
    1214           0 :           return true;
    1215             :       } else {
    1216          34 :         if (ParseOptionalStackAlignment(Alignment))
    1217             :           return true;
    1218             :       }
    1219          48 :       B.addStackAlignmentAttr(Alignment);
    1220          48 :       continue;
    1221             :     }
    1222             :     case lltok::kw_allocsize: {
    1223             :       unsigned ElemSizeArg;
    1224             :       Optional<unsigned> NumElemsArg;
    1225             :       // inAttrGrp doesn't matter; we only support allocsize(a[, b])
    1226          25 :       if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
    1227             :         return true;
    1228          24 :       B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
    1229             :       continue;
    1230             :     }
    1231         257 :     case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
    1232         392 :     case lltok::kw_argmemonly: B.addAttribute(Attribute::ArgMemOnly); break;
    1233          56 :     case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
    1234          33 :     case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
    1235         193 :     case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
    1236          24 :     case lltok::kw_inaccessiblememonly:
    1237          24 :       B.addAttribute(Attribute::InaccessibleMemOnly); break;
    1238          17 :     case lltok::kw_inaccessiblemem_or_argmemonly:
    1239          17 :       B.addAttribute(Attribute::InaccessibleMemOrArgMemOnly); break;
    1240         156 :     case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
    1241           9 :     case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
    1242         715 :     case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
    1243          30 :     case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
    1244         135 :     case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
    1245          35 :     case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
    1246          48 :     case lltok::kw_noimplicitfloat:
    1247          48 :       B.addAttribute(Attribute::NoImplicitFloat); break;
    1248        1245 :     case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
    1249          44 :     case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
    1250         209 :     case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
    1251         536 :     case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
    1252           2 :     case lltok::kw_nocf_check: B.addAttribute(Attribute::NoCfCheck); break;
    1253         312 :     case lltok::kw_norecurse: B.addAttribute(Attribute::NoRecurse); break;
    1254       85301 :     case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
    1255           1 :     case lltok::kw_optforfuzzing:
    1256           1 :       B.addAttribute(Attribute::OptForFuzzing); break;
    1257         175 :     case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
    1258        5486 :     case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
    1259       22000 :     case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
    1260        2526 :     case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
    1261          89 :     case lltok::kw_returns_twice:
    1262          89 :       B.addAttribute(Attribute::ReturnsTwice); break;
    1263         354 :     case lltok::kw_speculatable: B.addAttribute(Attribute::Speculatable); break;
    1264        5294 :     case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
    1265          75 :     case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
    1266          78 :     case lltok::kw_sspstrong:
    1267          78 :       B.addAttribute(Attribute::StackProtectStrong); break;
    1268         199 :     case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
    1269          11 :     case lltok::kw_shadowcallstack:
    1270          11 :       B.addAttribute(Attribute::ShadowCallStack); break;
    1271         358 :     case lltok::kw_sanitize_address:
    1272         358 :       B.addAttribute(Attribute::SanitizeAddress); break;
    1273         147 :     case lltok::kw_sanitize_hwaddress:
    1274         147 :       B.addAttribute(Attribute::SanitizeHWAddress); break;
    1275          66 :     case lltok::kw_sanitize_thread:
    1276          66 :       B.addAttribute(Attribute::SanitizeThread); break;
    1277         315 :     case lltok::kw_sanitize_memory:
    1278         315 :       B.addAttribute(Attribute::SanitizeMemory); break;
    1279          23 :     case lltok::kw_speculative_load_hardening:
    1280          23 :       B.addAttribute(Attribute::SpeculativeLoadHardening);
    1281          23 :       break;
    1282           6 :     case lltok::kw_strictfp: B.addAttribute(Attribute::StrictFP); break;
    1283        4573 :     case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
    1284          29 :     case lltok::kw_writeonly: B.addAttribute(Attribute::WriteOnly); break;
    1285             : 
    1286             :     // Error handling.
    1287             :     case lltok::kw_inreg:
    1288             :     case lltok::kw_signext:
    1289             :     case lltok::kw_zeroext:
    1290           0 :       HaveError |=
    1291           0 :         Error(Lex.getLoc(),
    1292             :               "invalid use of attribute on a function");
    1293           0 :       break;
    1294             :     case lltok::kw_byval:
    1295             :     case lltok::kw_dereferenceable:
    1296             :     case lltok::kw_dereferenceable_or_null:
    1297             :     case lltok::kw_inalloca:
    1298             :     case lltok::kw_nest:
    1299             :     case lltok::kw_noalias:
    1300             :     case lltok::kw_nocapture:
    1301             :     case lltok::kw_nonnull:
    1302             :     case lltok::kw_returned:
    1303             :     case lltok::kw_sret:
    1304             :     case lltok::kw_swifterror:
    1305             :     case lltok::kw_swiftself:
    1306           0 :       HaveError |=
    1307           0 :         Error(Lex.getLoc(),
    1308             :               "invalid use of parameter-only attribute on a function");
    1309           0 :       break;
    1310             :     }
    1311             : 
    1312      190797 :     Lex.Lex();
    1313             :   }
    1314             : }
    1315             : 
    1316             : //===----------------------------------------------------------------------===//
    1317             : // GlobalValue Reference/Resolution Routines.
    1318             : //===----------------------------------------------------------------------===//
    1319             : 
    1320       43258 : static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy,
    1321             :                                               const std::string &Name) {
    1322       43258 :   if (auto *FT = dyn_cast<FunctionType>(PTy->getElementType()))
    1323       42750 :     return Function::Create(FT, GlobalValue::ExternalWeakLinkage,
    1324             :                             PTy->getAddressSpace(), Name, M);
    1325             :   else
    1326             :     return new GlobalVariable(*M, PTy->getElementType(), false,
    1327             :                               GlobalValue::ExternalWeakLinkage, nullptr, Name,
    1328             :                               nullptr, GlobalVariable::NotThreadLocal,
    1329         508 :                               PTy->getAddressSpace());
    1330             : }
    1331             : 
    1332     1983832 : Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
    1333             :                                         Value *Val, bool IsCall) {
    1334     1983832 :   if (Val->getType() == Ty)
    1335             :     return Val;
    1336             :   // For calls we also accept variables in the program address space.
    1337             :   Type *SuggestedTy = Ty;
    1338          15 :   if (IsCall && isa<PointerType>(Ty)) {
    1339           8 :     Type *TyInProgAS = cast<PointerType>(Ty)->getElementType()->getPointerTo(
    1340           8 :         M->getDataLayout().getProgramAddressSpace());
    1341             :     SuggestedTy = TyInProgAS;
    1342           8 :     if (Val->getType() == TyInProgAS)
    1343             :       return Val;
    1344             :   }
    1345          15 :   if (Ty->isLabelTy())
    1346           0 :     Error(Loc, "'" + Name + "' is not a basic block");
    1347             :   else
    1348          30 :     Error(Loc, "'" + Name + "' defined with type '" +
    1349          15 :                    getTypeString(Val->getType()) + "' but expected '" +
    1350          42 :                    getTypeString(SuggestedTy) + "'");
    1351             :   return nullptr;
    1352             : }
    1353             : 
    1354             : /// GetGlobalVal - Get a value with the specified name or ID, creating a
    1355             : /// forward reference record if needed.  This can return null if the value
    1356             : /// exists but does not have the right type.
    1357      219691 : GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
    1358             :                                     LocTy Loc, bool IsCall) {
    1359             :   PointerType *PTy = dyn_cast<PointerType>(Ty);
    1360             :   if (!PTy) {
    1361           0 :     Error(Loc, "global variable reference must have pointer type");
    1362           0 :     return nullptr;
    1363             :   }
    1364             : 
    1365             :   // Look this name up in the normal function symbol table.
    1366             :   GlobalValue *Val =
    1367      219691 :     cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
    1368             : 
    1369             :   // If this is a forward reference for the value, see if we already created a
    1370             :   // forward ref record.
    1371             :   if (!Val) {
    1372             :     auto I = ForwardRefVals.find(Name);
    1373       43214 :     if (I != ForwardRefVals.end())
    1374           0 :       Val = I->second.first;
    1375             :   }
    1376             : 
    1377             :   // If we have the value in the symbol table or fwd-ref table, return it.
    1378      219691 :   if (Val)
    1379      352954 :     return cast_or_null<GlobalValue>(
    1380      352954 :         checkValidVariableType(Loc, "@" + Name, Ty, Val, IsCall));
    1381             : 
    1382             :   // Otherwise, create a new forward reference for this value and remember it.
    1383       43214 :   GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, Name);
    1384       43214 :   ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
    1385       43214 :   return FwdVal;
    1386             : }
    1387             : 
    1388         643 : GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc,
    1389             :                                     bool IsCall) {
    1390             :   PointerType *PTy = dyn_cast<PointerType>(Ty);
    1391             :   if (!PTy) {
    1392           0 :     Error(Loc, "global variable reference must have pointer type");
    1393           0 :     return nullptr;
    1394             :   }
    1395             : 
    1396        1286 :   GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
    1397             : 
    1398             :   // If this is a forward reference for the value, see if we already created a
    1399             :   // forward ref record.
    1400         591 :   if (!Val) {
    1401             :     auto I = ForwardRefValIDs.find(ID);
    1402          52 :     if (I != ForwardRefValIDs.end())
    1403           8 :       Val = I->second.first;
    1404             :   }
    1405             : 
    1406             :   // If we have the value in the symbol table or fwd-ref table, return it.
    1407         643 :   if (Val)
    1408         599 :     return cast_or_null<GlobalValue>(
    1409         599 :         checkValidVariableType(Loc, "@" + Twine(ID), Ty, Val, IsCall));
    1410             : 
    1411             :   // Otherwise, create a new forward reference for this value and remember it.
    1412          88 :   GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, "");
    1413          44 :   ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
    1414          44 :   return FwdVal;
    1415             : }
    1416             : 
    1417             : //===----------------------------------------------------------------------===//
    1418             : // Comdat Reference/Resolution Routines.
    1419             : //===----------------------------------------------------------------------===//
    1420             : 
    1421         838 : Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
    1422             :   // Look this name up in the comdat symbol table.
    1423         838 :   Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
    1424         838 :   Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
    1425        1676 :   if (I != ComdatSymTab.end())
    1426         836 :     return &I->second;
    1427             : 
    1428             :   // Otherwise, create a new forward reference for this value and remember it.
    1429           4 :   Comdat *C = M->getOrInsertComdat(Name);
    1430           2 :   ForwardRefComdats[Name] = Loc;
    1431           2 :   return C;
    1432             : }
    1433             : 
    1434             : //===----------------------------------------------------------------------===//
    1435             : // Helper Routines.
    1436             : //===----------------------------------------------------------------------===//
    1437             : 
    1438             : /// ParseToken - If the current token has the specified kind, eat it and return
    1439             : /// success.  Otherwise, emit the specified error and return failure.
    1440     6877465 : bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
    1441     6877465 :   if (Lex.getKind() != T)
    1442           6 :     return TokError(ErrMsg);
    1443     6877459 :   Lex.Lex();
    1444     6877459 :   return false;
    1445             : }
    1446             : 
    1447             : /// ParseStringConstant
    1448             : ///   ::= StringConstant
    1449       91809 : bool LLParser::ParseStringConstant(std::string &Result) {
    1450       91809 :   if (Lex.getKind() != lltok::StringConstant)
    1451           0 :     return TokError("expected string constant");
    1452             :   Result = Lex.getStrVal();
    1453       91809 :   Lex.Lex();
    1454       91809 :   return false;
    1455             : }
    1456             : 
    1457             : /// ParseUInt32
    1458             : ///   ::= uint32
    1459      557072 : bool LLParser::ParseUInt32(uint32_t &Val) {
    1460      557072 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    1461           0 :     return TokError("expected integer");
    1462      557072 :   uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
    1463      557072 :   if (Val64 != unsigned(Val64))
    1464           0 :     return TokError("expected 32-bit integer (too large)");
    1465      557072 :   Val = Val64;
    1466      557072 :   Lex.Lex();
    1467      557072 :   return false;
    1468             : }
    1469             : 
    1470             : /// ParseUInt64
    1471             : ///   ::= uint64
    1472         720 : bool LLParser::ParseUInt64(uint64_t &Val) {
    1473         720 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    1474           0 :     return TokError("expected integer");
    1475         720 :   Val = Lex.getAPSIntVal().getLimitedValue();
    1476         720 :   Lex.Lex();
    1477         720 :   return false;
    1478             : }
    1479             : 
    1480             : /// ParseTLSModel
    1481             : ///   := 'localdynamic'
    1482             : ///   := 'initialexec'
    1483             : ///   := 'localexec'
    1484         164 : bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
    1485         164 :   switch (Lex.getKind()) {
    1486             :     default:
    1487           0 :       return TokError("expected localdynamic, initialexec or localexec");
    1488          55 :     case lltok::kw_localdynamic:
    1489          55 :       TLM = GlobalVariable::LocalDynamicTLSModel;
    1490          55 :       break;
    1491          57 :     case lltok::kw_initialexec:
    1492          57 :       TLM = GlobalVariable::InitialExecTLSModel;
    1493          57 :       break;
    1494          52 :     case lltok::kw_localexec:
    1495          52 :       TLM = GlobalVariable::LocalExecTLSModel;
    1496          52 :       break;
    1497             :   }
    1498             : 
    1499         164 :   Lex.Lex();
    1500         164 :   return false;
    1501             : }
    1502             : 
    1503             : /// ParseOptionalThreadLocal
    1504             : ///   := /*empty*/
    1505             : ///   := 'thread_local'
    1506             : ///   := 'thread_local' '(' tlsmodel ')'
    1507       29136 : bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
    1508       29136 :   TLM = GlobalVariable::NotThreadLocal;
    1509             :   if (!EatIfPresent(lltok::kw_thread_local))
    1510             :     return false;
    1511             : 
    1512         884 :   TLM = GlobalVariable::GeneralDynamicTLSModel;
    1513         884 :   if (Lex.getKind() == lltok::lparen) {
    1514             :     Lex.Lex();
    1515         328 :     return ParseTLSModel(TLM) ||
    1516         164 :       ParseToken(lltok::rparen, "expected ')' after thread local model");
    1517             :   }
    1518             :   return false;
    1519             : }
    1520             : 
    1521             : /// ParseOptionalAddrSpace
    1522             : ///   := /*empty*/
    1523             : ///   := 'addrspace' '(' uint32 ')'
    1524      610516 : bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
    1525      610516 :   AddrSpace = DefaultAS;
    1526             :   if (!EatIfPresent(lltok::kw_addrspace))
    1527             :     return false;
    1528      207226 :   return ParseToken(lltok::lparen, "expected '(' in address space") ||
    1529      207226 :          ParseUInt32(AddrSpace) ||
    1530      103613 :          ParseToken(lltok::rparen, "expected ')' in address space");
    1531             : }
    1532             : 
    1533             : /// ParseStringAttribute
    1534             : ///   := StringConstant
    1535             : ///   := StringConstant '=' StringConstant
    1536       18755 : bool LLParser::ParseStringAttribute(AttrBuilder &B) {
    1537             :   std::string Attr = Lex.getStrVal();
    1538       18755 :   Lex.Lex();
    1539             :   std::string Val;
    1540       17559 :   if (EatIfPresent(lltok::equal) && ParseStringConstant(Val))
    1541             :     return true;
    1542       18755 :   B.addAttribute(Attr, Val);
    1543       18755 :   return false;
    1544             : }
    1545             : 
    1546             : /// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
    1547      875782 : bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
    1548             :   bool HaveError = false;
    1549             : 
    1550      875782 :   B.clear();
    1551             : 
    1552             :   while (true) {
    1553      932049 :     lltok::Kind Token = Lex.getKind();
    1554      932049 :     switch (Token) {
    1555             :     default:  // End of attributes.
    1556             :       return HaveError;
    1557          14 :     case lltok::StringConstant: {
    1558          14 :       if (ParseStringAttribute(B))
    1559             :         return true;
    1560             :       continue;
    1561             :     }
    1562        5525 :     case lltok::kw_align: {
    1563             :       unsigned Alignment;
    1564        5525 :       if (ParseOptionalAlignment(Alignment))
    1565           1 :         return true;
    1566        5524 :       B.addAlignmentAttr(Alignment);
    1567        5524 :       continue;
    1568             :     }
    1569        1796 :     case lltok::kw_byval:           B.addAttribute(Attribute::ByVal); break;
    1570         475 :     case lltok::kw_dereferenceable: {
    1571             :       uint64_t Bytes;
    1572         475 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
    1573           0 :         return true;
    1574         475 :       B.addDereferenceableAttr(Bytes);
    1575         475 :       continue;
    1576             :     }
    1577          32 :     case lltok::kw_dereferenceable_or_null: {
    1578             :       uint64_t Bytes;
    1579          32 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
    1580           0 :         return true;
    1581          32 :       B.addDereferenceableOrNullAttr(Bytes);
    1582          32 :       continue;
    1583             :     }
    1584         130 :     case lltok::kw_inalloca:        B.addAttribute(Attribute::InAlloca); break;
    1585        3511 :     case lltok::kw_inreg:           B.addAttribute(Attribute::InReg); break;
    1586          71 :     case lltok::kw_nest:            B.addAttribute(Attribute::Nest); break;
    1587        5383 :     case lltok::kw_noalias:         B.addAttribute(Attribute::NoAlias); break;
    1588       14651 :     case lltok::kw_nocapture:       B.addAttribute(Attribute::NoCapture); break;
    1589        1628 :     case lltok::kw_nonnull:         B.addAttribute(Attribute::NonNull); break;
    1590         266 :     case lltok::kw_readnone:        B.addAttribute(Attribute::ReadNone); break;
    1591        4233 :     case lltok::kw_readonly:        B.addAttribute(Attribute::ReadOnly); break;
    1592         261 :     case lltok::kw_returned:        B.addAttribute(Attribute::Returned); break;
    1593        9329 :     case lltok::kw_signext:         B.addAttribute(Attribute::SExt); break;
    1594        1127 :     case lltok::kw_sret:            B.addAttribute(Attribute::StructRet); break;
    1595         343 :     case lltok::kw_swifterror:      B.addAttribute(Attribute::SwiftError); break;
    1596         206 :     case lltok::kw_swiftself:       B.addAttribute(Attribute::SwiftSelf); break;
    1597         281 :     case lltok::kw_writeonly:       B.addAttribute(Attribute::WriteOnly); break;
    1598        7005 :     case lltok::kw_zeroext:         B.addAttribute(Attribute::ZExt); break;
    1599             : 
    1600             :     case lltok::kw_alignstack:
    1601             :     case lltok::kw_alwaysinline:
    1602             :     case lltok::kw_argmemonly:
    1603             :     case lltok::kw_builtin:
    1604             :     case lltok::kw_inlinehint:
    1605             :     case lltok::kw_jumptable:
    1606             :     case lltok::kw_minsize:
    1607             :     case lltok::kw_naked:
    1608             :     case lltok::kw_nobuiltin:
    1609             :     case lltok::kw_noduplicate:
    1610             :     case lltok::kw_noimplicitfloat:
    1611             :     case lltok::kw_noinline:
    1612             :     case lltok::kw_nonlazybind:
    1613             :     case lltok::kw_noredzone:
    1614             :     case lltok::kw_noreturn:
    1615             :     case lltok::kw_nocf_check:
    1616             :     case lltok::kw_nounwind:
    1617             :     case lltok::kw_optforfuzzing:
    1618             :     case lltok::kw_optnone:
    1619             :     case lltok::kw_optsize:
    1620             :     case lltok::kw_returns_twice:
    1621             :     case lltok::kw_sanitize_address:
    1622             :     case lltok::kw_sanitize_hwaddress:
    1623             :     case lltok::kw_sanitize_memory:
    1624             :     case lltok::kw_sanitize_thread:
    1625             :     case lltok::kw_speculative_load_hardening:
    1626             :     case lltok::kw_ssp:
    1627             :     case lltok::kw_sspreq:
    1628             :     case lltok::kw_sspstrong:
    1629             :     case lltok::kw_safestack:
    1630             :     case lltok::kw_shadowcallstack:
    1631             :     case lltok::kw_strictfp:
    1632             :     case lltok::kw_uwtable:
    1633           1 :       HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
    1634           1 :       break;
    1635             :     }
    1636             : 
    1637       50222 :     Lex.Lex();
    1638             :   }
    1639             : }
    1640             : 
    1641             : /// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
    1642      479381 : bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
    1643             :   bool HaveError = false;
    1644             : 
    1645      479381 :   B.clear();
    1646             : 
    1647             :   while (true) {
    1648      489233 :     lltok::Kind Token = Lex.getKind();
    1649      489233 :     switch (Token) {
    1650             :     default:  // End of attributes.
    1651             :       return HaveError;
    1652          16 :     case lltok::StringConstant: {
    1653          16 :       if (ParseStringAttribute(B))
    1654             :         return true;
    1655             :       continue;
    1656             :     }
    1657          77 :     case lltok::kw_dereferenceable: {
    1658             :       uint64_t Bytes;
    1659          77 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
    1660           0 :         return true;
    1661          77 :       B.addDereferenceableAttr(Bytes);
    1662          77 :       continue;
    1663             :     }
    1664          14 :     case lltok::kw_dereferenceable_or_null: {
    1665             :       uint64_t Bytes;
    1666          14 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
    1667           0 :         return true;
    1668          14 :       B.addDereferenceableOrNullAttr(Bytes);
    1669          14 :       continue;
    1670             :     }
    1671           4 :     case lltok::kw_align: {
    1672             :       unsigned Alignment;
    1673           4 :       if (ParseOptionalAlignment(Alignment))
    1674           0 :         return true;
    1675           4 :       B.addAlignmentAttr(Alignment);
    1676           4 :       continue;
    1677             :     }
    1678          88 :     case lltok::kw_inreg:           B.addAttribute(Attribute::InReg); break;
    1679         524 :     case lltok::kw_noalias:         B.addAttribute(Attribute::NoAlias); break;
    1680          73 :     case lltok::kw_nonnull:         B.addAttribute(Attribute::NonNull); break;
    1681        4815 :     case lltok::kw_signext:         B.addAttribute(Attribute::SExt); break;
    1682        4239 :     case lltok::kw_zeroext:         B.addAttribute(Attribute::ZExt); break;
    1683             : 
    1684             :     // Error handling.
    1685             :     case lltok::kw_byval:
    1686             :     case lltok::kw_inalloca:
    1687             :     case lltok::kw_nest:
    1688             :     case lltok::kw_nocapture:
    1689             :     case lltok::kw_returned:
    1690             :     case lltok::kw_sret:
    1691             :     case lltok::kw_swifterror:
    1692             :     case lltok::kw_swiftself:
    1693           1 :       HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
    1694           1 :       break;
    1695             : 
    1696             :     case lltok::kw_alignstack:
    1697             :     case lltok::kw_alwaysinline:
    1698             :     case lltok::kw_argmemonly:
    1699             :     case lltok::kw_builtin:
    1700             :     case lltok::kw_cold:
    1701             :     case lltok::kw_inlinehint:
    1702             :     case lltok::kw_jumptable:
    1703             :     case lltok::kw_minsize:
    1704             :     case lltok::kw_naked:
    1705             :     case lltok::kw_nobuiltin:
    1706             :     case lltok::kw_noduplicate:
    1707             :     case lltok::kw_noimplicitfloat:
    1708             :     case lltok::kw_noinline:
    1709             :     case lltok::kw_nonlazybind:
    1710             :     case lltok::kw_noredzone:
    1711             :     case lltok::kw_noreturn:
    1712             :     case lltok::kw_nocf_check:
    1713             :     case lltok::kw_nounwind:
    1714             :     case lltok::kw_optforfuzzing:
    1715             :     case lltok::kw_optnone:
    1716             :     case lltok::kw_optsize:
    1717             :     case lltok::kw_returns_twice:
    1718             :     case lltok::kw_sanitize_address:
    1719             :     case lltok::kw_sanitize_hwaddress:
    1720             :     case lltok::kw_sanitize_memory:
    1721             :     case lltok::kw_sanitize_thread:
    1722             :     case lltok::kw_speculative_load_hardening:
    1723             :     case lltok::kw_ssp:
    1724             :     case lltok::kw_sspreq:
    1725             :     case lltok::kw_sspstrong:
    1726             :     case lltok::kw_safestack:
    1727             :     case lltok::kw_shadowcallstack:
    1728             :     case lltok::kw_strictfp:
    1729             :     case lltok::kw_uwtable:
    1730           1 :       HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
    1731           1 :       break;
    1732             : 
    1733             :     case lltok::kw_readnone:
    1734             :     case lltok::kw_readonly:
    1735           0 :       HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
    1736             :     }
    1737             : 
    1738        9741 :     Lex.Lex();
    1739             :   }
    1740             : }
    1741             : 
    1742      352526 : static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
    1743      352526 :   HasLinkage = true;
    1744      352526 :   switch (Kind) {
    1745      332349 :   default:
    1746      332349 :     HasLinkage = false;
    1747      332349 :     return GlobalValue::ExternalLinkage;
    1748             :   case lltok::kw_private:
    1749             :     return GlobalValue::PrivateLinkage;
    1750        3650 :   case lltok::kw_internal:
    1751        3650 :     return GlobalValue::InternalLinkage;
    1752         705 :   case lltok::kw_weak:
    1753         705 :     return GlobalValue::WeakAnyLinkage;
    1754         312 :   case lltok::kw_weak_odr:
    1755         312 :     return GlobalValue::WeakODRLinkage;
    1756         140 :   case lltok::kw_linkonce:
    1757         140 :     return GlobalValue::LinkOnceAnyLinkage;
    1758         910 :   case lltok::kw_linkonce_odr:
    1759         910 :     return GlobalValue::LinkOnceODRLinkage;
    1760         135 :   case lltok::kw_available_externally:
    1761         135 :     return GlobalValue::AvailableExternallyLinkage;
    1762         344 :   case lltok::kw_appending:
    1763         344 :     return GlobalValue::AppendingLinkage;
    1764        4965 :   case lltok::kw_common:
    1765        4965 :     return GlobalValue::CommonLinkage;
    1766         131 :   case lltok::kw_extern_weak:
    1767         131 :     return GlobalValue::ExternalWeakLinkage;
    1768        6817 :   case lltok::kw_external:
    1769        6817 :     return GlobalValue::ExternalLinkage;
    1770             :   }
    1771             : }
    1772             : 
    1773             : /// ParseOptionalLinkage
    1774             : ///   ::= /*empty*/
    1775             : ///   ::= 'private'
    1776             : ///   ::= 'internal'
    1777             : ///   ::= 'weak'
    1778             : ///   ::= 'weak_odr'
    1779             : ///   ::= 'linkonce'
    1780             : ///   ::= 'linkonce_odr'
    1781             : ///   ::= 'available_externally'
    1782             : ///   ::= 'appending'
    1783             : ///   ::= 'common'
    1784             : ///   ::= 'extern_weak'
    1785             : ///   ::= 'external'
    1786      352451 : bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage,
    1787             :                                     unsigned &Visibility,
    1788             :                                     unsigned &DLLStorageClass,
    1789             :                                     bool &DSOLocal) {
    1790      352451 :   Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
    1791      352451 :   if (HasLinkage)
    1792       20102 :     Lex.Lex();
    1793      352451 :   ParseOptionalDSOLocal(DSOLocal);
    1794      352451 :   ParseOptionalVisibility(Visibility);
    1795      352451 :   ParseOptionalDLLStorageClass(DLLStorageClass);
    1796             : 
    1797      352451 :   if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
    1798           1 :     return Error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
    1799             :   }
    1800             : 
    1801             :   return false;
    1802             : }
    1803             : 
    1804      352451 : void LLParser::ParseOptionalDSOLocal(bool &DSOLocal) {
    1805      352451 :   switch (Lex.getKind()) {
    1806      350765 :   default:
    1807      350765 :     DSOLocal = false;
    1808      350765 :     break;
    1809        1538 :   case lltok::kw_dso_local:
    1810        1538 :     DSOLocal = true;
    1811        1538 :     Lex.Lex();
    1812             :     break;
    1813         148 :   case lltok::kw_dso_preemptable:
    1814         148 :     DSOLocal = false;
    1815         148 :     Lex.Lex();
    1816             :     break;
    1817             :   }
    1818      352451 : }
    1819             : 
    1820             : /// ParseOptionalVisibility
    1821             : ///   ::= /*empty*/
    1822             : ///   ::= 'default'
    1823             : ///   ::= 'hidden'
    1824             : ///   ::= 'protected'
    1825             : ///
    1826      352451 : void LLParser::ParseOptionalVisibility(unsigned &Res) {
    1827      352451 :   switch (Lex.getKind()) {
    1828      351242 :   default:
    1829      351242 :     Res = GlobalValue::DefaultVisibility;
    1830      351242 :     return;
    1831          17 :   case lltok::kw_default:
    1832          17 :     Res = GlobalValue::DefaultVisibility;
    1833          17 :     break;
    1834        1081 :   case lltok::kw_hidden:
    1835        1081 :     Res = GlobalValue::HiddenVisibility;
    1836        1081 :     break;
    1837         111 :   case lltok::kw_protected:
    1838         111 :     Res = GlobalValue::ProtectedVisibility;
    1839         111 :     break;
    1840             :   }
    1841        1209 :   Lex.Lex();
    1842             : }
    1843             : 
    1844             : /// ParseOptionalDLLStorageClass
    1845             : ///   ::= /*empty*/
    1846             : ///   ::= 'dllimport'
    1847             : ///   ::= 'dllexport'
    1848             : ///
    1849      352451 : void LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
    1850      352451 :   switch (Lex.getKind()) {
    1851      352001 :   default:
    1852      352001 :     Res = GlobalValue::DefaultStorageClass;
    1853      352001 :     return;
    1854         136 :   case lltok::kw_dllimport:
    1855         136 :     Res = GlobalValue::DLLImportStorageClass;
    1856         136 :     break;
    1857         314 :   case lltok::kw_dllexport:
    1858         314 :     Res = GlobalValue::DLLExportStorageClass;
    1859         314 :     break;
    1860             :   }
    1861         450 :   Lex.Lex();
    1862             : }
    1863             : 
    1864             : /// ParseOptionalCallingConv
    1865             : ///   ::= /*empty*/
    1866             : ///   ::= 'ccc'
    1867             : ///   ::= 'fastcc'
    1868             : ///   ::= 'intel_ocl_bicc'
    1869             : ///   ::= 'coldcc'
    1870             : ///   ::= 'x86_stdcallcc'
    1871             : ///   ::= 'x86_fastcallcc'
    1872             : ///   ::= 'x86_thiscallcc'
    1873             : ///   ::= 'x86_vectorcallcc'
    1874             : ///   ::= 'arm_apcscc'
    1875             : ///   ::= 'arm_aapcscc'
    1876             : ///   ::= 'arm_aapcs_vfpcc'
    1877             : ///   ::= 'aarch64_vector_pcs'
    1878             : ///   ::= 'msp430_intrcc'
    1879             : ///   ::= 'avr_intrcc'
    1880             : ///   ::= 'avr_signalcc'
    1881             : ///   ::= 'ptx_kernel'
    1882             : ///   ::= 'ptx_device'
    1883             : ///   ::= 'spir_func'
    1884             : ///   ::= 'spir_kernel'
    1885             : ///   ::= 'x86_64_sysvcc'
    1886             : ///   ::= 'win64cc'
    1887             : ///   ::= 'webkit_jscc'
    1888             : ///   ::= 'anyregcc'
    1889             : ///   ::= 'preserve_mostcc'
    1890             : ///   ::= 'preserve_allcc'
    1891             : ///   ::= 'ghccc'
    1892             : ///   ::= 'swiftcc'
    1893             : ///   ::= 'x86_intrcc'
    1894             : ///   ::= 'hhvmcc'
    1895             : ///   ::= 'hhvm_ccc'
    1896             : ///   ::= 'cxx_fast_tlscc'
    1897             : ///   ::= 'amdgpu_vs'
    1898             : ///   ::= 'amdgpu_ls'
    1899             : ///   ::= 'amdgpu_hs'
    1900             : ///   ::= 'amdgpu_es'
    1901             : ///   ::= 'amdgpu_gs'
    1902             : ///   ::= 'amdgpu_ps'
    1903             : ///   ::= 'amdgpu_cs'
    1904             : ///   ::= 'amdgpu_kernel'
    1905             : ///   ::= 'cc' UINT
    1906             : ///
    1907      479381 : bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
    1908      479381 :   switch (Lex.getKind()) {
    1909      451463 :   default:                       CC = CallingConv::C; return false;
    1910          12 :   case lltok::kw_ccc:            CC = CallingConv::C; break;
    1911        1840 :   case lltok::kw_fastcc:         CC = CallingConv::Fast; break;
    1912         121 :   case lltok::kw_coldcc:         CC = CallingConv::Cold; break;
    1913          87 :   case lltok::kw_x86_stdcallcc:  CC = CallingConv::X86_StdCall; break;
    1914         102 :   case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
    1915         200 :   case lltok::kw_x86_regcallcc:  CC = CallingConv::X86_RegCall; break;
    1916         337 :   case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
    1917          68 :   case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
    1918          40 :   case lltok::kw_arm_apcscc:     CC = CallingConv::ARM_APCS; break;
    1919         737 :   case lltok::kw_arm_aapcscc:    CC = CallingConv::ARM_AAPCS; break;
    1920         763 :   case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
    1921          17 :   case lltok::kw_aarch64_vector_pcs:CC = CallingConv::AArch64_VectorCall; break;
    1922          13 :   case lltok::kw_msp430_intrcc:  CC = CallingConv::MSP430_INTR; break;
    1923           5 :   case lltok::kw_avr_intrcc:     CC = CallingConv::AVR_INTR; break;
    1924           5 :   case lltok::kw_avr_signalcc:   CC = CallingConv::AVR_SIGNAL; break;
    1925          21 :   case lltok::kw_ptx_kernel:     CC = CallingConv::PTX_Kernel; break;
    1926         185 :   case lltok::kw_ptx_device:     CC = CallingConv::PTX_Device; break;
    1927          22 :   case lltok::kw_spir_kernel:    CC = CallingConv::SPIR_KERNEL; break;
    1928          45 :   case lltok::kw_spir_func:      CC = CallingConv::SPIR_FUNC; break;
    1929          92 :   case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
    1930          11 :   case lltok::kw_x86_64_sysvcc:  CC = CallingConv::X86_64_SysV; break;
    1931          46 :   case lltok::kw_win64cc:        CC = CallingConv::Win64; break;
    1932          33 :   case lltok::kw_webkit_jscc:    CC = CallingConv::WebKit_JS; break;
    1933          84 :   case lltok::kw_anyregcc:       CC = CallingConv::AnyReg; break;
    1934          15 :   case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
    1935          11 :   case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
    1936          56 :   case lltok::kw_ghccc:          CC = CallingConv::GHC; break;
    1937         445 :   case lltok::kw_swiftcc:        CC = CallingConv::Swift; break;
    1938          30 :   case lltok::kw_x86_intrcc:     CC = CallingConv::X86_INTR; break;
    1939          27 :   case lltok::kw_hhvmcc:         CC = CallingConv::HHVM; break;
    1940          16 :   case lltok::kw_hhvm_ccc:       CC = CallingConv::HHVM_C; break;
    1941         108 :   case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break;
    1942         213 :   case lltok::kw_amdgpu_vs:      CC = CallingConv::AMDGPU_VS; break;
    1943           5 :   case lltok::kw_amdgpu_ls:      CC = CallingConv::AMDGPU_LS; break;
    1944          15 :   case lltok::kw_amdgpu_hs:      CC = CallingConv::AMDGPU_HS; break;
    1945           5 :   case lltok::kw_amdgpu_es:      CC = CallingConv::AMDGPU_ES; break;
    1946          71 :   case lltok::kw_amdgpu_gs:      CC = CallingConv::AMDGPU_GS; break;
    1947        1597 :   case lltok::kw_amdgpu_ps:      CC = CallingConv::AMDGPU_PS; break;
    1948          52 :   case lltok::kw_amdgpu_cs:      CC = CallingConv::AMDGPU_CS; break;
    1949       20255 :   case lltok::kw_amdgpu_kernel:  CC = CallingConv::AMDGPU_KERNEL; break;
    1950         111 :   case lltok::kw_cc: {
    1951         111 :       Lex.Lex();
    1952         111 :       return ParseUInt32(CC);
    1953             :     }
    1954             :   }
    1955             : 
    1956       27807 :   Lex.Lex();
    1957       27807 :   return false;
    1958             : }
    1959             : 
    1960             : /// ParseMetadataAttachment
    1961             : ///   ::= !dbg !42
    1962       33274 : bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
    1963             :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
    1964             : 
    1965             :   std::string Name = Lex.getStrVal();
    1966       66548 :   Kind = M->getMDKindID(Name);
    1967       33274 :   Lex.Lex();
    1968             : 
    1969       33274 :   return ParseMDNode(MD);
    1970             : }
    1971             : 
    1972             : /// ParseInstructionMetadata
    1973             : ///   ::= !dbg !42 (',' !dbg !57)*
    1974       26644 : bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
    1975             :   do {
    1976       28061 :     if (Lex.getKind() != lltok::MetadataVar)
    1977           2 :       return TokError("expected metadata after comma");
    1978             : 
    1979             :     unsigned MDK;
    1980             :     MDNode *N;
    1981       28059 :     if (ParseMetadataAttachment(MDK, N))
    1982             :       return true;
    1983             : 
    1984       28057 :     Inst.setMetadata(MDK, N);
    1985       28057 :     if (MDK == LLVMContext::MD_tbaa)
    1986        4561 :       InstsWithTBAATag.push_back(&Inst);
    1987             : 
    1988             :     // If this is the end of the list, we're done.
    1989             :   } while (EatIfPresent(lltok::comma));
    1990             :   return false;
    1991             : }
    1992             : 
    1993             : /// ParseGlobalObjectMetadataAttachment
    1994             : ///   ::= !dbg !57
    1995        5178 : bool LLParser::ParseGlobalObjectMetadataAttachment(GlobalObject &GO) {
    1996             :   unsigned MDK;
    1997             :   MDNode *N;
    1998        5178 :   if (ParseMetadataAttachment(MDK, N))
    1999             :     return true;
    2000             : 
    2001        5178 :   GO.addMetadata(MDK, *N);
    2002        5178 :   return false;
    2003             : }
    2004             : 
    2005             : /// ParseOptionalFunctionMetadata
    2006             : ///   ::= (!dbg !57)*
    2007      256909 : bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
    2008      260585 :   while (Lex.getKind() == lltok::MetadataVar)
    2009        3676 :     if (ParseGlobalObjectMetadataAttachment(F))
    2010             :       return true;
    2011             :   return false;
    2012             : }
    2013             : 
    2014             : /// ParseOptionalAlignment
    2015             : ///   ::= /* empty */
    2016             : ///   ::= 'align' 4
    2017      584823 : bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
    2018      584823 :   Alignment = 0;
    2019             :   if (!EatIfPresent(lltok::kw_align))
    2020             :     return false;
    2021      261657 :   LocTy AlignLoc = Lex.getLoc();
    2022      261657 :   if (ParseUInt32(Alignment)) return true;
    2023      261657 :   if (!isPowerOf2_32(Alignment))
    2024           1 :     return Error(AlignLoc, "alignment is not a power of two");
    2025      261656 :   if (Alignment > Value::MaximumAlignment)
    2026           3 :     return Error(AlignLoc, "huge alignments are not supported yet");
    2027             :   return false;
    2028             : }
    2029             : 
    2030             : /// ParseOptionalDerefAttrBytes
    2031             : ///   ::= /* empty */
    2032             : ///   ::= AttrKind '(' 4 ')'
    2033             : ///
    2034             : /// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
    2035         598 : bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
    2036             :                                            uint64_t &Bytes) {
    2037             :   assert((AttrKind == lltok::kw_dereferenceable ||
    2038             :           AttrKind == lltok::kw_dereferenceable_or_null) &&
    2039             :          "contract!");
    2040             : 
    2041         598 :   Bytes = 0;
    2042             :   if (!EatIfPresent(AttrKind))
    2043             :     return false;
    2044         598 :   LocTy ParenLoc = Lex.getLoc();
    2045             :   if (!EatIfPresent(lltok::lparen))
    2046           0 :     return Error(ParenLoc, "expected '('");
    2047         598 :   LocTy DerefLoc = Lex.getLoc();
    2048         598 :   if (ParseUInt64(Bytes)) return true;
    2049         598 :   ParenLoc = Lex.getLoc();
    2050             :   if (!EatIfPresent(lltok::rparen))
    2051           0 :     return Error(ParenLoc, "expected ')'");
    2052         598 :   if (!Bytes)
    2053           0 :     return Error(DerefLoc, "dereferenceable bytes must be non-zero");
    2054             :   return false;
    2055             : }
    2056             : 
    2057             : /// ParseOptionalCommaAlign
    2058             : ///   ::=
    2059             : ///   ::= ',' align 4
    2060             : ///
    2061             : /// This returns with AteExtraComma set to true if it ate an excess comma at the
    2062             : /// end.
    2063      293707 : bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
    2064             :                                        bool &AteExtraComma) {
    2065      293707 :   AteExtraComma = false;
    2066             :   while (EatIfPresent(lltok::comma)) {
    2067             :     // Metadata at the end is an early exit.
    2068      204678 :     if (Lex.getKind() == lltok::MetadataVar) {
    2069        9600 :       AteExtraComma = true;
    2070        9600 :       return false;
    2071             :     }
    2072             : 
    2073      195078 :     if (Lex.getKind() != lltok::kw_align)
    2074           0 :       return Error(Lex.getLoc(), "expected metadata or 'align'");
    2075             : 
    2076      195078 :     if (ParseOptionalAlignment(Alignment)) return true;
    2077             :   }
    2078             : 
    2079             :   return false;
    2080             : }
    2081             : 
    2082             : /// ParseOptionalCommaAddrSpace
    2083             : ///   ::=
    2084             : ///   ::= ',' addrspace(1)
    2085             : ///
    2086             : /// This returns with AteExtraComma set to true if it ate an excess comma at the
    2087             : /// end.
    2088       44868 : bool LLParser::ParseOptionalCommaAddrSpace(unsigned &AddrSpace,
    2089             :                                            LocTy &Loc,
    2090             :                                            bool &AteExtraComma) {
    2091       44868 :   AteExtraComma = false;
    2092             :   while (EatIfPresent(lltok::comma)) {
    2093             :     // Metadata at the end is an early exit.
    2094         605 :     if (Lex.getKind() == lltok::MetadataVar) {
    2095          47 :       AteExtraComma = true;
    2096          47 :       return false;
    2097             :     }
    2098             : 
    2099         558 :     Loc = Lex.getLoc();
    2100         558 :     if (Lex.getKind() != lltok::kw_addrspace)
    2101           0 :       return Error(Lex.getLoc(), "expected metadata or 'addrspace'");
    2102             : 
    2103         558 :     if (ParseOptionalAddrSpace(AddrSpace))
    2104             :       return true;
    2105             :   }
    2106             : 
    2107             :   return false;
    2108             : }
    2109             : 
    2110          25 : bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
    2111             :                                        Optional<unsigned> &HowManyArg) {
    2112          25 :   Lex.Lex();
    2113             : 
    2114          25 :   auto StartParen = Lex.getLoc();
    2115             :   if (!EatIfPresent(lltok::lparen))
    2116           0 :     return Error(StartParen, "expected '('");
    2117             : 
    2118          25 :   if (ParseUInt32(BaseSizeArg))
    2119             :     return true;
    2120             : 
    2121             :   if (EatIfPresent(lltok::comma)) {
    2122          12 :     auto HowManyAt = Lex.getLoc();
    2123             :     unsigned HowMany;
    2124          12 :     if (ParseUInt32(HowMany))
    2125           1 :       return true;
    2126          12 :     if (HowMany == BaseSizeArg)
    2127           1 :       return Error(HowManyAt,
    2128             :                    "'allocsize' indices can't refer to the same parameter");
    2129             :     HowManyArg = HowMany;
    2130             :   } else
    2131             :     HowManyArg = None;
    2132             : 
    2133          24 :   auto EndParen = Lex.getLoc();
    2134             :   if (!EatIfPresent(lltok::rparen))
    2135           0 :     return Error(EndParen, "expected ')'");
    2136             :   return false;
    2137             : }
    2138             : 
    2139             : /// ParseScopeAndOrdering
    2140             : ///   if isAtomic: ::= SyncScope? AtomicOrdering
    2141             : ///   else: ::=
    2142             : ///
    2143             : /// This sets Scope and Ordering to the parsed values.
    2144      302055 : bool LLParser::ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
    2145             :                                      AtomicOrdering &Ordering) {
    2146      302055 :   if (!isAtomic)
    2147             :     return false;
    2148             : 
    2149       10692 :   return ParseScope(SSID) || ParseOrdering(Ordering);
    2150             : }
    2151             : 
    2152             : /// ParseScope
    2153             : ///   ::= syncscope("singlethread" | "<target scope>")?
    2154             : ///
    2155             : /// This sets synchronization scope ID to the ID of the parsed value.
    2156       10692 : bool LLParser::ParseScope(SyncScope::ID &SSID) {
    2157       10692 :   SSID = SyncScope::System;
    2158             :   if (EatIfPresent(lltok::kw_syncscope)) {
    2159         877 :     auto StartParenAt = Lex.getLoc();
    2160             :     if (!EatIfPresent(lltok::lparen))
    2161           0 :       return Error(StartParenAt, "Expected '(' in syncscope");
    2162             : 
    2163             :     std::string SSN;
    2164         877 :     auto SSNAt = Lex.getLoc();
    2165         877 :     if (ParseStringConstant(SSN))
    2166           0 :       return Error(SSNAt, "Expected synchronization scope name");
    2167             : 
    2168         877 :     auto EndParenAt = Lex.getLoc();
    2169             :     if (!EatIfPresent(lltok::rparen))
    2170           0 :       return Error(EndParenAt, "Expected ')' in syncscope");
    2171             : 
    2172        1754 :     SSID = Context.getOrInsertSyncScopeID(SSN);
    2173             :   }
    2174             : 
    2175             :   return false;
    2176             : }
    2177             : 
    2178             : /// ParseOrdering
    2179             : ///   ::= AtomicOrdering
    2180             : ///
    2181             : /// This sets Ordering to the parsed value.
    2182       11909 : bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
    2183       11909 :   switch (Lex.getKind()) {
    2184           0 :   default: return TokError("Expected ordering on atomic instruction");
    2185         497 :   case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break;
    2186        2792 :   case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break;
    2187             :   // Not specified yet:
    2188             :   // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
    2189        1470 :   case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break;
    2190         956 :   case lltok::kw_release: Ordering = AtomicOrdering::Release; break;
    2191         615 :   case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break;
    2192        5579 :   case lltok::kw_seq_cst:
    2193        5579 :     Ordering = AtomicOrdering::SequentiallyConsistent;
    2194        5579 :     break;
    2195             :   }
    2196       11909 :   Lex.Lex();
    2197       11909 :   return false;
    2198             : }
    2199             : 
    2200             : /// ParseOptionalStackAlignment
    2201             : ///   ::= /* empty */
    2202             : ///   ::= 'alignstack' '(' 4 ')'
    2203          34 : bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
    2204          34 :   Alignment = 0;
    2205             :   if (!EatIfPresent(lltok::kw_alignstack))
    2206             :     return false;
    2207          34 :   LocTy ParenLoc = Lex.getLoc();
    2208             :   if (!EatIfPresent(lltok::lparen))
    2209           0 :     return Error(ParenLoc, "expected '('");
    2210          34 :   LocTy AlignLoc = Lex.getLoc();
    2211          34 :   if (ParseUInt32(Alignment)) return true;
    2212          34 :   ParenLoc = Lex.getLoc();
    2213             :   if (!EatIfPresent(lltok::rparen))
    2214           0 :     return Error(ParenLoc, "expected ')'");
    2215          34 :   if (!isPowerOf2_32(Alignment))
    2216           0 :     return Error(AlignLoc, "stack alignment is not a power of two");
    2217             :   return false;
    2218             : }
    2219             : 
    2220             : /// ParseIndexList - This parses the index list for an insert/extractvalue
    2221             : /// instruction.  This sets AteExtraComma in the case where we eat an extra
    2222             : /// comma at the end of the line and find that it is followed by metadata.
    2223             : /// Clients that don't allow metadata can call the version of this function that
    2224             : /// only takes one argument.
    2225             : ///
    2226             : /// ParseIndexList
    2227             : ///    ::=  (',' uint32)+
    2228             : ///
    2229        8056 : bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
    2230             :                               bool &AteExtraComma) {
    2231        8056 :   AteExtraComma = false;
    2232             : 
    2233        8056 :   if (Lex.getKind() != lltok::comma)
    2234           0 :     return TokError("expected ',' as start of index list");
    2235             : 
    2236        8387 :   while (EatIfPresent(lltok::comma)) {
    2237        8455 :     if (Lex.getKind() == lltok::MetadataVar) {
    2238          69 :       if (Indices.empty()) return TokError("expected index");
    2239          67 :       AteExtraComma = true;
    2240          67 :       return false;
    2241             :     }
    2242        8387 :     unsigned Idx = 0;
    2243        8387 :     if (ParseUInt32(Idx)) return true;
    2244        8387 :     Indices.push_back(Idx);
    2245             :   }
    2246             : 
    2247             :   return false;
    2248             : }
    2249             : 
    2250             : //===----------------------------------------------------------------------===//
    2251             : // Type Parsing.
    2252             : //===----------------------------------------------------------------------===//
    2253             : 
    2254             : /// ParseType - Parse a type.
    2255     6449045 : bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
    2256     6449045 :   SMLoc TypeLoc = Lex.getLoc();
    2257     6449045 :   switch (Lex.getKind()) {
    2258             :   default:
    2259           5 :     return TokError(Msg);
    2260     4883239 :   case lltok::Type:
    2261             :     // Type ::= 'float' | 'void' (etc)
    2262     4883239 :     Result = Lex.getTyVal();
    2263     4883239 :     Lex.Lex();
    2264             :     break;
    2265       23401 :   case lltok::lbrace:
    2266             :     // Type ::= StructType
    2267       23401 :     if (ParseAnonStructType(Result, false))
    2268             :       return true;
    2269             :     break;
    2270      128346 :   case lltok::lsquare:
    2271             :     // Type ::= '[' ... ']'
    2272      128346 :     Lex.Lex(); // eat the lsquare.
    2273      128346 :     if (ParseArrayVectorType(Result, false))
    2274             :       return true;
    2275             :     break;
    2276     1324566 :   case lltok::less: // Either vector or packed struct.
    2277             :     // Type ::= '<' ... '>'
    2278     1324566 :     Lex.Lex();
    2279     1324566 :     if (Lex.getKind() == lltok::lbrace) {
    2280        1522 :       if (ParseAnonStructType(Result, true) ||
    2281         761 :           ParseToken(lltok::greater, "expected '>' at end of packed struct"))
    2282           0 :         return true;
    2283     1323805 :     } else if (ParseArrayVectorType(Result, true))
    2284             :       return true;
    2285             :     break;
    2286       87069 :   case lltok::LocalVar: {
    2287             :     // Type ::= %foo
    2288       87069 :     std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
    2289             : 
    2290             :     // If the type hasn't been defined yet, create a forward definition and
    2291             :     // remember where that forward def'n was seen (in case it never is defined).
    2292       87069 :     if (!Entry.first) {
    2293        4408 :       Entry.first = StructType::create(Context, Lex.getStrVal());
    2294        4408 :       Entry.second = Lex.getLoc();
    2295             :     }
    2296       87069 :     Result = Entry.first;
    2297       87069 :     Lex.Lex();
    2298             :     break;
    2299             :   }
    2300             : 
    2301        2419 :   case lltok::LocalVarID: {
    2302             :     // Type ::= %4
    2303        2419 :     std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
    2304             : 
    2305             :     // If the type hasn't been defined yet, create a forward definition and
    2306             :     // remember where that forward def'n was seen (in case it never is defined).
    2307        2419 :     if (!Entry.first) {
    2308         212 :       Entry.first = StructType::create(Context);
    2309         212 :       Entry.second = Lex.getLoc();
    2310             :     }
    2311        2419 :     Result = Entry.first;
    2312        2419 :     Lex.Lex();
    2313             :     break;
    2314             :   }
    2315             :   }
    2316             : 
    2317             :   // Parse the type suffixes.
    2318             :   while (true) {
    2319     7251747 :     switch (Lex.getKind()) {
    2320             :     // End of type.
    2321     6449038 :     default:
    2322     6449038 :       if (!AllowVoid && Result->isVoidTy())
    2323           0 :         return Error(TypeLoc, "void type only allowed for function results");
    2324             :       return false;
    2325             : 
    2326             :     // Type ::= Type '*'
    2327      680683 :     case lltok::star:
    2328     1361366 :       if (Result->isLabelTy())
    2329           1 :         return TokError("basic block pointers are invalid");
    2330      680682 :       if (Result->isVoidTy())
    2331           0 :         return TokError("pointers to void are invalid - use i8* instead");
    2332      680682 :       if (!PointerType::isValidElementType(Result))
    2333           0 :         return TokError("pointer to this type is invalid");
    2334      680682 :       Result = PointerType::getUnqual(Result);
    2335      680682 :       Lex.Lex();
    2336             :       break;
    2337             : 
    2338             :     // Type ::= Type 'addrspace' '(' uint32 ')' '*'
    2339      102049 :     case lltok::kw_addrspace: {
    2340      204098 :       if (Result->isLabelTy())
    2341           0 :         return TokError("basic block pointers are invalid");
    2342      102049 :       if (Result->isVoidTy())
    2343           0 :         return TokError("pointers to void are invalid; use i8* instead");
    2344      102049 :       if (!PointerType::isValidElementType(Result))
    2345           0 :         return TokError("pointer to this type is invalid");
    2346             :       unsigned AddrSpace;
    2347      204098 :       if (ParseOptionalAddrSpace(AddrSpace) ||
    2348      102049 :           ParseToken(lltok::star, "expected '*' in address space"))
    2349           0 :         return true;
    2350             : 
    2351      102049 :       Result = PointerType::get(Result, AddrSpace);
    2352      102049 :       break;
    2353             :     }
    2354             : 
    2355             :     /// Types '(' ArgTypeListI ')' OptFuncAttrs
    2356       19977 :     case lltok::lparen:
    2357       19977 :       if (ParseFunctionType(Result))
    2358             :         return true;
    2359             :       break;
    2360             :     }
    2361             :   }
    2362             : }
    2363             : 
    2364             : /// ParseParameterList
    2365             : ///    ::= '(' ')'
    2366             : ///    ::= '(' Arg (',' Arg)* ')'
    2367             : ///  Arg
    2368             : ///    ::= Type OptionalAttributes Value OptionalAttributes
    2369      156067 : bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
    2370             :                                   PerFunctionState &PFS, bool IsMustTailCall,
    2371             :                                   bool InVarArgsFunc) {
    2372      156067 :   if (ParseToken(lltok::lparen, "expected '(' in call"))
    2373             :     return true;
    2374             : 
    2375      454460 :   while (Lex.getKind() != lltok::rparen) {
    2376             :     // If this isn't the first argument, we need a comma.
    2377      472228 :     if (!ArgList.empty() &&
    2378      173778 :         ParseToken(lltok::comma, "expected ',' in argument list"))
    2379          57 :       return true;
    2380             : 
    2381             :     // Parse an ellipsis if this is a musttail call in a variadic function.
    2382      298450 :     if (Lex.getKind() == lltok::dotdotdot) {
    2383             :       const char *Msg = "unexpected ellipsis in argument list for ";
    2384          51 :       if (!IsMustTailCall)
    2385           0 :         return TokError(Twine(Msg) + "non-musttail call");
    2386          51 :       if (!InVarArgsFunc)
    2387           1 :         return TokError(Twine(Msg) + "musttail call in non-varargs function");
    2388          50 :       Lex.Lex();  // Lex the '...', it is purely for readability.
    2389          50 :       return ParseToken(lltok::rparen, "expected ')' at end of argument list");
    2390             :     }
    2391             : 
    2392             :     // Parse the argument.
    2393             :     LocTy ArgLoc;
    2394      298399 :     Type *ArgTy = nullptr;
    2395             :     AttrBuilder ArgAttrs;
    2396             :     Value *V;
    2397      298399 :     if (ParseType(ArgTy, ArgLoc))
    2398             :       return true;
    2399             : 
    2400      596798 :     if (ArgTy->isMetadataTy()) {
    2401        9669 :       if (ParseMetadataAsValue(V, PFS))
    2402             :         return true;
    2403             :     } else {
    2404             :       // Otherwise, handle normal operands.
    2405      288730 :       if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
    2406           3 :         return true;
    2407             :     }
    2408      298393 :     ArgList.push_back(ParamInfo(
    2409             :         ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
    2410             :   }
    2411             : 
    2412      156010 :   if (IsMustTailCall && InVarArgsFunc)
    2413           1 :     return TokError("expected '...' at end of argument list for musttail call "
    2414             :                     "in varargs function");
    2415             : 
    2416      156009 :   Lex.Lex();  // Lex the ')'.
    2417      156009 :   return false;
    2418             : }
    2419             : 
    2420             : /// ParseOptionalOperandBundles
    2421             : ///    ::= /*empty*/
    2422             : ///    ::= '[' OperandBundle [, OperandBundle ]* ']'
    2423             : ///
    2424             : /// OperandBundle
    2425             : ///    ::= bundle-tag '(' ')'
    2426             : ///    ::= bundle-tag '(' Type Value [, Type Value ]* ')'
    2427             : ///
    2428             : /// bundle-tag ::= String Constant
    2429      156059 : bool LLParser::ParseOptionalOperandBundles(
    2430             :     SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
    2431      156059 :   LocTy BeginLoc = Lex.getLoc();
    2432             :   if (!EatIfPresent(lltok::lsquare))
    2433             :     return false;
    2434             : 
    2435        2790 :   while (Lex.getKind() != lltok::rsquare) {
    2436             :     // If this isn't the first operand bundle, we need a comma.
    2437        1455 :     if (!BundleList.empty() &&
    2438          40 :         ParseToken(lltok::comma, "expected ',' in input list"))
    2439           0 :       return true;
    2440             : 
    2441             :     std::string Tag;
    2442        1415 :     if (ParseStringConstant(Tag))
    2443             :       return true;
    2444             : 
    2445        1415 :     if (ParseToken(lltok::lparen, "expected '(' in operand bundle"))
    2446             :       return true;
    2447             : 
    2448             :     std::vector<Value *> Inputs;
    2449        2606 :     while (Lex.getKind() != lltok::rparen) {
    2450             :       // If this isn't the first input, we need a comma.
    2451        1735 :       if (!Inputs.empty() &&
    2452         544 :           ParseToken(lltok::comma, "expected ',' in input list"))
    2453           0 :         return true;
    2454             : 
    2455        1191 :       Type *Ty = nullptr;
    2456        1191 :       Value *Input = nullptr;
    2457        1191 :       if (ParseType(Ty) || ParseValue(Ty, Input, PFS))
    2458           0 :         return true;
    2459        1191 :       Inputs.push_back(Input);
    2460             :     }
    2461             : 
    2462        1415 :     BundleList.emplace_back(std::move(Tag), std::move(Inputs));
    2463             : 
    2464             :     Lex.Lex(); // Lex the ')'.
    2465             :   }
    2466             : 
    2467        1375 :   if (BundleList.empty())
    2468           0 :     return Error(BeginLoc, "operand bundle set must not be empty");
    2469             : 
    2470             :   Lex.Lex(); // Lex the ']'.
    2471        1375 :   return false;
    2472             : }
    2473             : 
    2474             : /// ParseArgumentList - Parse the argument list for a function type or function
    2475             : /// prototype.
    2476             : ///   ::= '(' ArgTypeListI ')'
    2477             : /// ArgTypeListI
    2478             : ///   ::= /*empty*/
    2479             : ///   ::= '...'
    2480             : ///   ::= ArgTypeList ',' '...'
    2481             : ///   ::= ArgType (',' ArgType)*
    2482             : ///
    2483      343285 : bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
    2484             :                                  bool &isVarArg){
    2485      343285 :   isVarArg = false;
    2486             :   assert(Lex.getKind() == lltok::lparen);
    2487      343285 :   Lex.Lex(); // eat the (.
    2488             : 
    2489      343285 :   if (Lex.getKind() == lltok::rparen) {
    2490             :     // empty
    2491      292962 :   } else if (Lex.getKind() == lltok::dotdotdot) {
    2492        6925 :     isVarArg = true;
    2493             :     Lex.Lex();
    2494             :   } else {
    2495      286037 :     LocTy TypeLoc = Lex.getLoc();
    2496      286037 :     Type *ArgTy = nullptr;
    2497             :     AttrBuilder Attrs;
    2498             :     std::string Name;
    2499             : 
    2500      572073 :     if (ParseType(ArgTy) ||
    2501      286038 :         ParseOptionalParamAttrs(Attrs)) return true;
    2502             : 
    2503      572070 :     if (ArgTy->isVoidTy())
    2504           0 :       return Error(TypeLoc, "argument can not have void type");
    2505             : 
    2506      286035 :     if (Lex.getKind() == lltok::LocalVar) {
    2507             :       Name = Lex.getStrVal();
    2508             :       Lex.Lex();
    2509             :     }
    2510             : 
    2511      286035 :     if (!FunctionType::isValidArgumentType(ArgTy))
    2512           0 :       return Error(TypeLoc, "invalid type for function argument");
    2513             : 
    2514      286035 :     ArgList.emplace_back(TypeLoc, ArgTy,
    2515      572070 :                          AttributeSet::get(ArgTy->getContext(), Attrs),
    2516             :                          std::move(Name));
    2517             : 
    2518      301016 :     while (EatIfPresent(lltok::comma)) {
    2519             :       // Handle ... at end of arg list.
    2520             :       if (EatIfPresent(lltok::dotdotdot)) {
    2521        4635 :         isVarArg = true;
    2522        4635 :         break;
    2523             :       }
    2524             : 
    2525             :       // Otherwise must be an argument type.
    2526      301016 :       TypeLoc = Lex.getLoc();
    2527      301016 :       if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
    2528             : 
    2529      602032 :       if (ArgTy->isVoidTy())
    2530           0 :         return Error(TypeLoc, "argument can not have void type");
    2531             : 
    2532      301016 :       if (Lex.getKind() == lltok::LocalVar) {
    2533             :         Name = Lex.getStrVal();
    2534             :         Lex.Lex();
    2535             :       } else {
    2536             :         Name = "";
    2537             :       }
    2538             : 
    2539      301016 :       if (!ArgTy->isFirstClassType())
    2540           0 :         return Error(TypeLoc, "invalid type for function argument");
    2541             : 
    2542      301016 :       ArgList.emplace_back(TypeLoc, ArgTy,
    2543      602032 :                            AttributeSet::get(ArgTy->getContext(), Attrs),
    2544             :                            std::move(Name));
    2545             :     }
    2546             :   }
    2547             : 
    2548      343283 :   return ParseToken(lltok::rparen, "expected ')' at end of argument list");
    2549             : }
    2550             : 
    2551             : /// ParseFunctionType
    2552             : ///  ::= Type ArgumentList OptionalAttrs
    2553       19977 : bool LLParser::ParseFunctionType(Type *&Result) {
    2554             :   assert(Lex.getKind() == lltok::lparen);
    2555             : 
    2556       19977 :   if (!FunctionType::isValidReturnType(Result))
    2557           0 :     return TokError("invalid function return type");
    2558             : 
    2559       19977 :   SmallVector<ArgInfo, 8> ArgList;
    2560             :   bool isVarArg;
    2561       19977 :   if (ParseArgumentList(ArgList, isVarArg))
    2562             :     return true;
    2563             : 
    2564             :   // Reject names on the arguments lists.
    2565       38333 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    2566       36712 :     if (!ArgList[i].Name.empty())
    2567           0 :       return Error(ArgList[i].Loc, "argument name invalid in function type");
    2568       18356 :     if (ArgList[i].Attrs.hasAttributes())
    2569           0 :       return Error(ArgList[i].Loc,
    2570             :                    "argument attributes invalid in function type");
    2571             :   }
    2572             : 
    2573             :   SmallVector<Type*, 16> ArgListTy;
    2574       38333 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
    2575       36712 :     ArgListTy.push_back(ArgList[i].Ty);
    2576             : 
    2577       39954 :   Result = FunctionType::get(Result, ArgListTy, isVarArg);
    2578             :   return false;
    2579             : }
    2580             : 
    2581             : /// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
    2582             : /// other structs.
    2583       24162 : bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
    2584             :   SmallVector<Type*, 8> Elts;
    2585       24162 :   if (ParseStructBody(Elts)) return true;
    2586             : 
    2587       48324 :   Result = StructType::get(Context, Elts, Packed);
    2588       24162 :   return false;
    2589             : }
    2590             : 
    2591             : /// ParseStructDefinition - Parse a struct in a 'type' definition.
    2592       12772 : bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
    2593             :                                      std::pair<Type*, LocTy> &Entry,
    2594             :                                      Type *&ResultTy) {
    2595             :   // If the type was already defined, diagnose the redefinition.
    2596       12772 :   if (Entry.first && !Entry.second.isValid())
    2597           0 :     return Error(TypeLoc, "redefinition of type");
    2598             : 
    2599             :   // If we have opaque, just return without filling in the definition for the
    2600             :   // struct.  This counts as a definition as far as the .ll file goes.
    2601             :   if (EatIfPresent(lltok::kw_opaque)) {
    2602             :     // This type is being defined, so clear the location to indicate this.
    2603         723 :     Entry.second = SMLoc();
    2604             : 
    2605             :     // If this type number has never been uttered, create it.
    2606         723 :     if (!Entry.first)
    2607         405 :       Entry.first = StructType::create(Context, Name);
    2608         723 :     ResultTy = Entry.first;
    2609         723 :     return false;
    2610             :   }
    2611             : 
    2612             :   // If the type starts with '<', then it is either a packed struct or a vector.
    2613             :   bool isPacked = EatIfPresent(lltok::less);
    2614             : 
    2615             :   // If we don't have a struct, then we have a random type alias, which we
    2616             :   // accept for compatibility with old files.  These types are not allowed to be
    2617             :   // forward referenced and not allowed to be recursive.
    2618       12049 :   if (Lex.getKind() != lltok::lbrace) {
    2619         452 :     if (Entry.first)
    2620           0 :       return Error(TypeLoc, "forward references to non-struct type");
    2621             : 
    2622         452 :     ResultTy = nullptr;
    2623         452 :     if (isPacked)
    2624         374 :       return ParseArrayVectorType(ResultTy, true);
    2625          78 :     return ParseType(ResultTy);
    2626             :   }
    2627             : 
    2628             :   // This type is being defined, so clear the location to indicate this.
    2629       11597 :   Entry.second = SMLoc();
    2630             : 
    2631             :   // If this type number has never been uttered, create it.
    2632       11597 :   if (!Entry.first)
    2633        7300 :     Entry.first = StructType::create(Context, Name);
    2634             : 
    2635       11597 :   StructType *STy = cast<StructType>(Entry.first);
    2636             : 
    2637             :   SmallVector<Type*, 8> Body;
    2638       11597 :   if (ParseStructBody(Body) ||
    2639         760 :       (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
    2640           0 :     return true;
    2641             : 
    2642       23194 :   STy->setBody(Body, isPacked);
    2643       11597 :   ResultTy = STy;
    2644       11597 :   return false;
    2645             : }
    2646             : 
    2647             : /// ParseStructType: Handles packed and unpacked types.  </> parsed elsewhere.
    2648             : ///   StructType
    2649             : ///     ::= '{' '}'
    2650             : ///     ::= '{' Type (',' Type)* '}'
    2651             : ///     ::= '<' '{' '}' '>'
    2652             : ///     ::= '<' '{' Type (',' Type)* '}' '>'
    2653       35759 : bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
    2654             :   assert(Lex.getKind() == lltok::lbrace);
    2655       35759 :   Lex.Lex(); // Consume the '{'
    2656             : 
    2657             :   // Handle the empty struct.
    2658             :   if (EatIfPresent(lltok::rbrace))
    2659         724 :     return false;
    2660             : 
    2661       35035 :   LocTy EltTyLoc = Lex.getLoc();
    2662       35035 :   Type *Ty = nullptr;
    2663       35035 :   if (ParseType(Ty)) return true;
    2664       35035 :   Body.push_back(Ty);
    2665             : 
    2666       35035 :   if (!StructType::isValidElementType(Ty))
    2667           0 :     return Error(EltTyLoc, "invalid element type for struct");
    2668             : 
    2669       80072 :   while (EatIfPresent(lltok::comma)) {
    2670       80072 :     EltTyLoc = Lex.getLoc();
    2671       80072 :     if (ParseType(Ty)) return true;
    2672             : 
    2673       80072 :     if (!StructType::isValidElementType(Ty))
    2674           0 :       return Error(EltTyLoc, "invalid element type for struct");
    2675             : 
    2676       80072 :     Body.push_back(Ty);
    2677             :   }
    2678             : 
    2679       35035 :   return ParseToken(lltok::rbrace, "expected '}' at end of struct");
    2680             : }
    2681             : 
    2682             : /// ParseArrayVectorType - Parse an array or vector type, assuming the first
    2683             : /// token has already been consumed.
    2684             : ///   Type
    2685             : ///     ::= '[' APSINTVAL 'x' Types ']'
    2686             : ///     ::= '<' APSINTVAL 'x' Types '>'
    2687     1452525 : bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
    2688     1452525 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
    2689     1452525 :       Lex.getAPSIntVal().getBitWidth() > 64)
    2690           0 :     return TokError("expected number in address space");
    2691             : 
    2692     1452525 :   LocTy SizeLoc = Lex.getLoc();
    2693             :   uint64_t Size = Lex.getAPSIntVal().getZExtValue();
    2694     1452525 :   Lex.Lex();
    2695             : 
    2696     1452525 :   if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
    2697             :       return true;
    2698             : 
    2699     1452525 :   LocTy TypeLoc = Lex.getLoc();
    2700     1452525 :   Type *EltTy = nullptr;
    2701     1452525 :   if (ParseType(EltTy)) return true;
    2702             : 
    2703     1580871 :   if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
    2704             :                  "expected end of sequential type"))
    2705             :     return true;
    2706             : 
    2707     1452525 :   if (isVector) {
    2708     1324179 :     if (Size == 0)
    2709           0 :       return Error(SizeLoc, "zero element vector is illegal");
    2710     1324179 :     if ((unsigned)Size != Size)
    2711           0 :       return Error(SizeLoc, "size too large for vector");
    2712     1324179 :     if (!VectorType::isValidElementType(EltTy))
    2713           0 :       return Error(TypeLoc, "invalid vector element type");
    2714     1324179 :     Result = VectorType::get(EltTy, unsigned(Size));
    2715             :   } else {
    2716      128346 :     if (!ArrayType::isValidElementType(EltTy))
    2717           1 :       return Error(TypeLoc, "invalid array element type");
    2718      128345 :     Result = ArrayType::get(EltTy, Size);
    2719             :   }
    2720             :   return false;
    2721             : }
    2722             : 
    2723             : //===----------------------------------------------------------------------===//
    2724             : // Function Semantic Analysis.
    2725             : //===----------------------------------------------------------------------===//
    2726             : 
    2727      256909 : LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
    2728      256909 :                                              int functionNumber)
    2729      513818 :   : P(p), F(f), FunctionNumber(functionNumber) {
    2730             : 
    2731             :   // Insert unnamed arguments into the NumberedVals list.
    2732      698259 :   for (Argument &A : F.args())
    2733      441350 :     if (!A.hasName())
    2734        4887 :       NumberedVals.push_back(&A);
    2735      256909 : }
    2736             : 
    2737      256909 : LLParser::PerFunctionState::~PerFunctionState() {
    2738             :   // If there were any forward referenced non-basicblock values, delete them.
    2739             : 
    2740      256918 :   for (const auto &P : ForwardRefVals) {
    2741          18 :     if (isa<BasicBlock>(P.second.first))
    2742             :       continue;
    2743           6 :     P.second.first->replaceAllUsesWith(
    2744           6 :         UndefValue::get(P.second.first->getType()));
    2745           6 :     P.second.first->deleteValue();
    2746             :   }
    2747             : 
    2748      256910 :   for (const auto &P : ForwardRefValIDs) {
    2749           2 :     if (isa<BasicBlock>(P.second.first))
    2750             :       continue;
    2751           1 :     P.second.first->replaceAllUsesWith(
    2752           1 :         UndefValue::get(P.second.first->getType()));
    2753           1 :     P.second.first->deleteValue();
    2754             :   }
    2755      256909 : }
    2756             : 
    2757      256831 : bool LLParser::PerFunctionState::FinishFunction() {
    2758      256831 :   if (!ForwardRefVals.empty())
    2759           4 :     return P.Error(ForwardRefVals.begin()->second.second,
    2760           4 :                    "use of undefined value '%" + ForwardRefVals.begin()->first +
    2761             :                    "'");
    2762      256829 :   if (!ForwardRefValIDs.empty())
    2763           0 :     return P.Error(ForwardRefValIDs.begin()->second.second,
    2764           0 :                    "use of undefined value '%" +
    2765           0 :                    Twine(ForwardRefValIDs.begin()->first) + "'");
    2766             :   return false;
    2767             : }
    2768             : 
    2769             : /// GetVal - Get a value with the specified name or ID, creating a
    2770             : /// forward reference record if needed.  This can return null if the value
    2771             : /// exists but does not have the right type.
    2772     1721860 : Value *LLParser::PerFunctionState::GetVal(const std::string &Name, Type *Ty,
    2773             :                                           LocTy Loc, bool IsCall) {
    2774             :   // Look this name up in the normal function symbol table.
    2775     1721860 :   Value *Val = F.getValueSymbolTable()->lookup(Name);
    2776             : 
    2777             :   // If this is a forward reference for the value, see if we already created a
    2778             :   // forward ref record.
    2779     1721860 :   if (!Val) {
    2780             :     auto I = ForwardRefVals.find(Name);
    2781      195420 :     if (I != ForwardRefVals.end())
    2782        1012 :       Val = I->second.first;
    2783             :   }
    2784             : 
    2785             :   // If we have the value in the symbol table or fwd-ref table, return it.
    2786     1721860 :   if (Val)
    2787     1551723 :     return P.checkValidVariableType(Loc, "%" + Name, Ty, Val, IsCall);
    2788             : 
    2789             :   // Don't make placeholders with invalid type.
    2790             :   if (!Ty->isFirstClassType()) {
    2791           0 :     P.Error(Loc, "invalid use of a non-first-class type");
    2792           0 :     return nullptr;
    2793             :   }
    2794             : 
    2795             :   // Otherwise, create a new forward reference for this value and remember it.
    2796             :   Value *FwdVal;
    2797      194408 :   if (Ty->isLabelTy()) {
    2798      533256 :     FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
    2799             :   } else {
    2800       16656 :     FwdVal = new Argument(Ty, Name);
    2801             :   }
    2802             : 
    2803      194408 :   ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
    2804      194408 :   return FwdVal;
    2805             : }
    2806             : 
    2807      452838 : Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, LocTy Loc,
    2808             :                                           bool IsCall) {
    2809             :   // Look this name up in the normal function symbol table.
    2810      905676 :   Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
    2811             : 
    2812             :   // If this is a forward reference for the value, see if we already created a
    2813             :   // forward ref record.
    2814      275798 :   if (!Val) {
    2815             :     auto I = ForwardRefValIDs.find(ID);
    2816      177040 :     if (I != ForwardRefValIDs.end())
    2817        3506 :       Val = I->second.first;
    2818             :   }
    2819             : 
    2820             :   // If we have the value in the symbol table or fwd-ref table, return it.
    2821      452838 :   if (Val)
    2822      279304 :     return P.checkValidVariableType(Loc, "%" + Twine(ID), Ty, Val, IsCall);
    2823             : 
    2824             :   if (!Ty->isFirstClassType()) {
    2825           0 :     P.Error(Loc, "invalid use of a non-first-class type");
    2826           0 :     return nullptr;
    2827             :   }
    2828             : 
    2829             :   // Otherwise, create a new forward reference for this value and remember it.
    2830             :   Value *FwdVal;
    2831      173534 :   if (Ty->isLabelTy()) {
    2832      517416 :     FwdVal = BasicBlock::Create(F.getContext(), "", &F);
    2833             :   } else {
    2834        1062 :     FwdVal = new Argument(Ty);
    2835             :   }
    2836             : 
    2837      173534 :   ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
    2838      173534 :   return FwdVal;
    2839             : }
    2840             : 
    2841             : /// SetInstName - After an instruction is parsed and inserted into its
    2842             : /// basic block, this installs its name.
    2843     1519237 : bool LLParser::PerFunctionState::SetInstName(int NameID,
    2844             :                                              const std::string &NameStr,
    2845             :                                              LocTy NameLoc, Instruction *Inst) {
    2846             :   // If this instruction has void type, it cannot have a name or ID specified.
    2847     3038474 :   if (Inst->getType()->isVoidTy()) {
    2848      526605 :     if (NameID != -1 || !NameStr.empty())
    2849           0 :       return P.Error(NameLoc, "instructions returning void cannot have a name");
    2850             :     return false;
    2851             :   }
    2852             : 
    2853             :   // If this was a numbered instruction, verify that the instruction is the
    2854             :   // expected value and resolve any forward references.
    2855      992632 :   if (NameStr.empty()) {
    2856             :     // If neither a name nor an ID was specified, just use the next ID.
    2857      255321 :     if (NameID == -1)
    2858       12372 :       NameID = NumberedVals.size();
    2859             : 
    2860      510642 :     if (unsigned(NameID) != NumberedVals.size())
    2861           0 :       return P.Error(NameLoc, "instruction expected to be numbered '%" +
    2862           0 :                      Twine(NumberedVals.size()) + "'");
    2863             : 
    2864             :     auto FI = ForwardRefValIDs.find(NameID);
    2865      255321 :     if (FI != ForwardRefValIDs.end()) {
    2866        1061 :       Value *Sentinel = FI->second.first;
    2867        1061 :       if (Sentinel->getType() != Inst->getType())
    2868           0 :         return P.Error(NameLoc, "instruction forward referenced with type '" +
    2869           0 :                        getTypeString(FI->second.first->getType()) + "'");
    2870             : 
    2871        1061 :       Sentinel->replaceAllUsesWith(Inst);
    2872        1061 :       Sentinel->deleteValue();
    2873             :       ForwardRefValIDs.erase(FI);
    2874             :     }
    2875             : 
    2876      255321 :     NumberedVals.push_back(Inst);
    2877      255321 :     return false;
    2878             :   }
    2879             : 
    2880             :   // Otherwise, the instruction had a name.  Resolve forward refs and set it.
    2881             :   auto FI = ForwardRefVals.find(NameStr);
    2882      737311 :   if (FI != ForwardRefVals.end()) {
    2883       16650 :     Value *Sentinel = FI->second.first;
    2884       16650 :     if (Sentinel->getType() != Inst->getType())
    2885           0 :       return P.Error(NameLoc, "instruction forward referenced with type '" +
    2886           0 :                      getTypeString(FI->second.first->getType()) + "'");
    2887             : 
    2888       16650 :     Sentinel->replaceAllUsesWith(Inst);
    2889       16650 :     Sentinel->deleteValue();
    2890             :     ForwardRefVals.erase(FI);
    2891             :   }
    2892             : 
    2893             :   // Set the name on the instruction.
    2894     1474622 :   Inst->setName(NameStr);
    2895             : 
    2896      737311 :   if (Inst->getName() != NameStr)
    2897           3 :     return P.Error(NameLoc, "multiple definition of local value named '" +
    2898           2 :                    NameStr + "'");
    2899             :   return false;
    2900             : }
    2901             : 
    2902             : /// GetBB - Get a basic block with the specified name or ID, creating a
    2903             : /// forward reference record if needed.
    2904      178312 : BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
    2905             :                                               LocTy Loc) {
    2906      178312 :   return dyn_cast_or_null<BasicBlock>(
    2907      178312 :       GetVal(Name, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
    2908             : }
    2909             : 
    2910      172485 : BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
    2911      172485 :   return dyn_cast_or_null<BasicBlock>(
    2912      172485 :       GetVal(ID, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
    2913             : }
    2914             : 
    2915             : /// DefineBB - Define the specified basic block, which is either named or
    2916             : /// unnamed.  If there is an error, this returns null otherwise it returns
    2917             : /// the block being defined.
    2918      350222 : BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
    2919             :                                                  LocTy Loc) {
    2920             :   BasicBlock *BB;
    2921      350222 :   if (Name.empty())
    2922      344944 :     BB = GetBB(NumberedVals.size(), Loc);
    2923             :   else
    2924      177750 :     BB = GetBB(Name, Loc);
    2925      350222 :   if (!BB) return nullptr; // Already diagnosed error.
    2926             : 
    2927             :   // Move the block to the end of the function.  Forward ref'd blocks are
    2928             :   // inserted wherever they happen to be referenced.
    2929      700442 :   F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
    2930             : 
    2931             :   // Remove the block from forward ref sets.
    2932      350221 :   if (Name.empty()) {
    2933      344944 :     ForwardRefValIDs.erase(NumberedVals.size());
    2934      172472 :     NumberedVals.push_back(BB);
    2935             :   } else {
    2936             :     // BB forward references are already in the function symbol table.
    2937             :     ForwardRefVals.erase(Name);
    2938             :   }
    2939             : 
    2940             :   return BB;
    2941             : }
    2942             : 
    2943             : //===----------------------------------------------------------------------===//
    2944             : // Constants.
    2945             : //===----------------------------------------------------------------------===//
    2946             : 
    2947             : /// ParseValID - Parse an abstract value that doesn't necessarily have a
    2948             : /// type implied.  For example, if we parse "4" we don't know what integer type
    2949             : /// it has.  The value will later be combined with its type and checked for
    2950             : /// sanity.  PFS is used to convert function-local operands of metadata (since
    2951             : /// metadata operands are not just parsed here but also converted to values).
    2952             : /// PFS can be null when we are not parsing metadata values inside a function.
    2953     3734226 : bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
    2954     3734226 :   ID.Loc = Lex.getLoc();
    2955     3734226 :   switch (Lex.getKind()) {
    2956           1 :   default: return TokError("expected value token");
    2957         643 :   case lltok::GlobalID:  // @42
    2958         643 :     ID.UIntVal = Lex.getUIntVal();
    2959         643 :     ID.Kind = ValID::t_GlobalID;
    2960         643 :     break;
    2961      220414 :   case lltok::GlobalVar:  // @foo
    2962      220414 :     ID.StrVal = Lex.getStrVal();
    2963      220414 :     ID.Kind = ValID::t_GlobalName;
    2964      220414 :     break;
    2965      280372 :   case lltok::LocalVarID:  // %42
    2966      280372 :     ID.UIntVal = Lex.getUIntVal();
    2967      280372 :     ID.Kind = ValID::t_LocalID;
    2968      280372 :     break;
    2969     1544247 :   case lltok::LocalVar:  // %foo
    2970     1544247 :     ID.StrVal = Lex.getStrVal();
    2971     1544247 :     ID.Kind = ValID::t_LocalName;
    2972     1544247 :     break;
    2973     1253558 :   case lltok::APSInt:
    2974             :     ID.APSIntVal = Lex.getAPSIntVal();
    2975     1253558 :     ID.Kind = ValID::t_APSInt;
    2976     1253558 :     break;
    2977       58769 :   case lltok::APFloat:
    2978             :     ID.APFloatVal = Lex.getAPFloatVal();
    2979       58769 :     ID.Kind = ValID::t_APFloat;
    2980       58769 :     break;
    2981        7175 :   case lltok::kw_true:
    2982        7175 :     ID.ConstantVal = ConstantInt::getTrue(Context);
    2983        7175 :     ID.Kind = ValID::t_Constant;
    2984        7175 :     break;
    2985       13091 :   case lltok::kw_false:
    2986       13091 :     ID.ConstantVal = ConstantInt::getFalse(Context);
    2987       13091 :     ID.Kind = ValID::t_Constant;
    2988       13091 :     break;
    2989        8100 :   case lltok::kw_null: ID.Kind = ValID::t_Null; break;
    2990      171695 :   case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
    2991       29561 :   case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
    2992         503 :   case lltok::kw_none: ID.Kind = ValID::t_None; break;
    2993             : 
    2994        1741 :   case lltok::lbrace: {
    2995             :     // ValID ::= '{' ConstVector '}'
    2996        1741 :     Lex.Lex();
    2997             :     SmallVector<Constant*, 16> Elts;
    2998        3477 :     if (ParseGlobalValueVector(Elts) ||
    2999        1736 :         ParseToken(lltok::rbrace, "expected end of struct constant"))
    3000           5 :       return true;
    3001             : 
    3002        3472 :     ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
    3003        1736 :     ID.UIntVal = Elts.size();
    3004        1736 :     memcpy(ID.ConstantStructElts.get(), Elts.data(),
    3005             :            Elts.size() * sizeof(Elts[0]));
    3006        1736 :     ID.Kind = ValID::t_ConstantStruct;
    3007        1736 :     return false;
    3008             :   }
    3009       76574 :   case lltok::less: {
    3010             :     // ValID ::= '<' ConstVector '>'         --> Vector.
    3011             :     // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
    3012       76574 :     Lex.Lex();
    3013             :     bool isPackedStruct = EatIfPresent(lltok::lbrace);
    3014             : 
    3015             :     SmallVector<Constant*, 16> Elts;
    3016       76574 :     LocTy FirstEltLoc = Lex.getLoc();
    3017      153148 :     if (ParseGlobalValueVector(Elts) ||
    3018          92 :         (isPackedStruct &&
    3019      153240 :          ParseToken(lltok::rbrace, "expected end of packed struct")) ||
    3020       76574 :         ParseToken(lltok::greater, "expected end of constant"))
    3021           0 :       return true;
    3022             : 
    3023       76574 :     if (isPackedStruct) {
    3024         184 :       ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
    3025          92 :       memcpy(ID.ConstantStructElts.get(), Elts.data(),
    3026          92 :              Elts.size() * sizeof(Elts[0]));
    3027          92 :       ID.UIntVal = Elts.size();
    3028          92 :       ID.Kind = ValID::t_PackedConstantStruct;
    3029          92 :       return false;
    3030             :     }
    3031             : 
    3032       76482 :     if (Elts.empty())
    3033           0 :       return Error(ID.Loc, "constant vector must not be empty");
    3034             : 
    3035       76482 :     if (!Elts[0]->getType()->isIntegerTy() &&
    3036       76541 :         !Elts[0]->getType()->isFloatingPointTy() &&
    3037             :         !Elts[0]->getType()->isPointerTy())
    3038           0 :       return Error(FirstEltLoc,
    3039             :             "vector elements must have integer, pointer or floating point type");
    3040             : 
    3041             :     // Verify that all the vector elements have the same type.
    3042      927558 :     for (unsigned i = 1, e = Elts.size(); i != e; ++i)
    3043     1702152 :       if (Elts[i]->getType() != Elts[0]->getType())
    3044           0 :         return Error(FirstEltLoc,
    3045           0 :                      "vector element #" + Twine(i) +
    3046           0 :                     " is not of type '" + getTypeString(Elts[0]->getType()));
    3047             : 
    3048       76482 :     ID.ConstantVal = ConstantVector::get(Elts);
    3049       76482 :     ID.Kind = ValID::t_Constant;
    3050       76482 :     return false;
    3051             :   }
    3052        1456 :   case lltok::lsquare: {   // Array Constant
    3053        1456 :     Lex.Lex();
    3054             :     SmallVector<Constant*, 16> Elts;
    3055        1456 :     LocTy FirstEltLoc = Lex.getLoc();
    3056        2912 :     if (ParseGlobalValueVector(Elts) ||
    3057        1456 :         ParseToken(lltok::rsquare, "expected end of array constant"))
    3058           0 :       return true;
    3059             : 
    3060             :     // Handle empty element.
    3061        1456 :     if (Elts.empty()) {
    3062             :       // Use undef instead of an array because it's inconvenient to determine
    3063             :       // the element type at this point, there being no elements to examine.
    3064          19 :       ID.Kind = ValID::t_EmptyArray;
    3065          19 :       return false;
    3066             :     }
    3067             : 
    3068        1437 :     if (!Elts[0]->getType()->isFirstClassType())
    3069           0 :       return Error(FirstEltLoc, "invalid array element type: " +
    3070           0 :                    getTypeString(Elts[0]->getType()));
    3071             : 
    3072        1437 :     ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
    3073             : 
    3074             :     // Verify all elements are correct type!
    3075        7075 :     for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
    3076       11276 :       if (Elts[i]->getType() != Elts[0]->getType())
    3077           0 :         return Error(FirstEltLoc,
    3078           0 :                      "array element #" + Twine(i) +
    3079           0 :                      " is not of type '" + getTypeString(Elts[0]->getType()));
    3080             :     }
    3081             : 
    3082        1437 :     ID.ConstantVal = ConstantArray::get(ATy, Elts);
    3083        1437 :     ID.Kind = ValID::t_Constant;
    3084        1437 :     return false;
    3085             :   }
    3086        2195 :   case lltok::kw_c:  // c "foo"
    3087        2195 :     Lex.Lex();
    3088        2195 :     ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
    3089             :                                                   false);
    3090        2195 :     if (ParseToken(lltok::StringConstant, "expected string")) return true;
    3091        2195 :     ID.Kind = ValID::t_Constant;
    3092        2195 :     return false;
    3093             : 
    3094       16764 :   case lltok::kw_asm: {
    3095             :     // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
    3096             :     //             STRINGCONSTANT
    3097             :     bool HasSideEffect, AlignStack, AsmDialect;
    3098       16764 :     Lex.Lex();
    3099       16764 :     if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
    3100             :         ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
    3101       16764 :         ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
    3102       33528 :         ParseStringConstant(ID.StrVal) ||
    3103       33528 :         ParseToken(lltok::comma, "expected comma in inline asm expression") ||
    3104       16764 :         ParseToken(lltok::StringConstant, "expected constraint string"))
    3105             :       return true;
    3106       16764 :     ID.StrVal2 = Lex.getStrVal();
    3107       33528 :     ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
    3108       16764 :       (unsigned(AsmDialect)<<2);
    3109       16764 :     ID.Kind = ValID::t_InlineAsm;
    3110       16764 :     return false;
    3111             :   }
    3112             : 
    3113         702 :   case lltok::kw_blockaddress: {
    3114             :     // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
    3115         702 :     Lex.Lex();
    3116             : 
    3117        1404 :     ValID Fn, Label;
    3118             : 
    3119        1404 :     if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
    3120        1404 :         ParseValID(Fn) ||
    3121        1404 :         ParseToken(lltok::comma, "expected comma in block address expression")||
    3122        2106 :         ParseValID(Label) ||
    3123         702 :         ParseToken(lltok::rparen, "expected ')' in block address expression"))
    3124           0 :       return true;
    3125             : 
    3126         702 :     if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
    3127           0 :       return Error(Fn.Loc, "expected function name in blockaddress");
    3128         702 :     if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
    3129           0 :       return Error(Label.Loc, "expected basic block name in blockaddress");
    3130             : 
    3131             :     // Try to find the function (but skip it if it's forward-referenced).
    3132             :     GlobalValue *GV = nullptr;
    3133         702 :     if (Fn.Kind == ValID::t_GlobalID) {
    3134           0 :       if (Fn.UIntVal < NumberedVals.size())
    3135           0 :         GV = NumberedVals[Fn.UIntVal];
    3136             :     } else if (!ForwardRefVals.count(Fn.StrVal)) {
    3137        1404 :       GV = M->getNamedValue(Fn.StrVal);
    3138             :     }
    3139             :     Function *F = nullptr;
    3140         702 :     if (GV) {
    3141             :       // Confirm that it's actually a function with a definition.
    3142         324 :       if (!isa<Function>(GV))
    3143           0 :         return Error(Fn.Loc, "expected function name in blockaddress");
    3144             :       F = cast<Function>(GV);
    3145         324 :       if (F->isDeclaration())
    3146           0 :         return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
    3147             :     }
    3148             : 
    3149             :     if (!F) {
    3150             :       // Make a global variable as a placeholder for this reference.
    3151             :       GlobalValue *&FwdRef =
    3152         378 :           ForwardRefBlockAddresses.insert(std::make_pair(
    3153             :                                               std::move(Fn),
    3154         378 :                                               std::map<ValID, GlobalValue *>()))
    3155         378 :               .first->second.insert(std::make_pair(std::move(Label), nullptr))
    3156             :               .first->second;
    3157         378 :       if (!FwdRef)
    3158         652 :         FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
    3159         326 :                                     GlobalValue::InternalLinkage, nullptr, "");
    3160         378 :       ID.ConstantVal = FwdRef;
    3161         378 :       ID.Kind = ValID::t_Constant;
    3162         378 :       return false;
    3163             :     }
    3164             : 
    3165             :     // We found the function; now find the basic block.  Don't use PFS, since we
    3166             :     // might be inside a constant expression.
    3167             :     BasicBlock *BB;
    3168         324 :     if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
    3169         249 :       if (Label.Kind == ValID::t_LocalID)
    3170           4 :         BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
    3171             :       else
    3172         245 :         BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
    3173         249 :       if (!BB)
    3174           0 :         return Error(Label.Loc, "referenced value is not a basic block");
    3175             :     } else {
    3176          75 :       if (Label.Kind == ValID::t_LocalID)
    3177           0 :         return Error(Label.Loc, "cannot take address of numeric label after "
    3178             :                                 "the function is defined");
    3179          75 :       BB = dyn_cast_or_null<BasicBlock>(
    3180             :           F->getValueSymbolTable()->lookup(Label.StrVal));
    3181             :       if (!BB)
    3182           0 :         return Error(Label.Loc, "referenced value is not a basic block");
    3183             :     }
    3184             : 
    3185         324 :     ID.ConstantVal = BlockAddress::get(F, BB);
    3186         324 :     ID.Kind = ValID::t_Constant;
    3187         324 :     return false;
    3188             :   }
    3189             : 
    3190        8676 :   case lltok::kw_trunc:
    3191             :   case lltok::kw_zext:
    3192             :   case lltok::kw_sext:
    3193             :   case lltok::kw_fptrunc:
    3194             :   case lltok::kw_fpext:
    3195             :   case lltok::kw_bitcast:
    3196             :   case lltok::kw_addrspacecast:
    3197             :   case lltok::kw_uitofp:
    3198             :   case lltok::kw_sitofp:
    3199             :   case lltok::kw_fptoui:
    3200             :   case lltok::kw_fptosi:
    3201             :   case lltok::kw_inttoptr:
    3202             :   case lltok::kw_ptrtoint: {
    3203        8676 :     unsigned Opc = Lex.getUIntVal();
    3204        8676 :     Type *DestTy = nullptr;
    3205             :     Constant *SrcVal;
    3206        8676 :     Lex.Lex();
    3207       17352 :     if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
    3208       17351 :         ParseGlobalTypeAndValue(SrcVal) ||
    3209       17350 :         ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
    3210       17351 :         ParseType(DestTy) ||
    3211        8675 :         ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
    3212           1 :       return true;
    3213        8675 :     if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
    3214          30 :       return Error(ID.Loc, "invalid cast opcode for cast from '" +
    3215          40 :                    getTypeString(SrcVal->getType()) + "' to '" +
    3216          40 :                    getTypeString(DestTy) + "'");
    3217        8665 :     ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
    3218             :                                                  SrcVal, DestTy);
    3219        8665 :     ID.Kind = ValID::t_Constant;
    3220        8665 :     return false;
    3221             :   }
    3222          11 :   case lltok::kw_extractvalue: {
    3223          11 :     Lex.Lex();
    3224             :     Constant *Val;
    3225             :     SmallVector<unsigned, 4> Indices;
    3226          22 :     if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
    3227          22 :         ParseGlobalTypeAndValue(Val) ||
    3228          33 :         ParseIndexList(Indices) ||
    3229          11 :         ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
    3230           0 :       return true;
    3231             : 
    3232          11 :     if (!Val->getType()->isAggregateType())
    3233           0 :       return Error(ID.Loc, "extractvalue operand must be aggregate type");
    3234          11 :     if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
    3235           0 :       return Error(ID.Loc, "invalid indices for extractvalue");
    3236          11 :     ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
    3237          11 :     ID.Kind = ValID::t_Constant;
    3238          11 :     return false;
    3239             :   }
    3240          10 :   case lltok::kw_insertvalue: {
    3241          10 :     Lex.Lex();
    3242             :     Constant *Val0, *Val1;
    3243             :     SmallVector<unsigned, 4> Indices;
    3244          20 :     if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
    3245          20 :         ParseGlobalTypeAndValue(Val0) ||
    3246          20 :         ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
    3247          20 :         ParseGlobalTypeAndValue(Val1) ||
    3248          30 :         ParseIndexList(Indices) ||
    3249          10 :         ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
    3250           0 :       return true;
    3251          10 :     if (!Val0->getType()->isAggregateType())
    3252           0 :       return Error(ID.Loc, "insertvalue operand must be aggregate type");
    3253             :     Type *IndexedType =
    3254          10 :         ExtractValueInst::getIndexedType(Val0->getType(), Indices);
    3255          10 :     if (!IndexedType)
    3256           0 :       return Error(ID.Loc, "invalid indices for insertvalue");
    3257          10 :     if (IndexedType != Val1->getType())
    3258           3 :       return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
    3259           4 :                                getTypeString(Val1->getType()) +
    3260           4 :                                "' instead of '" + getTypeString(IndexedType) +
    3261             :                                "'");
    3262           9 :     ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
    3263           9 :     ID.Kind = ValID::t_Constant;
    3264           9 :     return false;
    3265             :   }
    3266         307 :   case lltok::kw_icmp:
    3267             :   case lltok::kw_fcmp: {
    3268         307 :     unsigned PredVal, Opc = Lex.getUIntVal();
    3269             :     Constant *Val0, *Val1;
    3270         307 :     Lex.Lex();
    3271         614 :     if (ParseCmpPredicate(PredVal, Opc) ||
    3272         614 :         ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
    3273         614 :         ParseGlobalTypeAndValue(Val0) ||
    3274         614 :         ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
    3275         921 :         ParseGlobalTypeAndValue(Val1) ||
    3276         307 :         ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
    3277           0 :       return true;
    3278             : 
    3279         307 :     if (Val0->getType() != Val1->getType())
    3280           0 :       return Error(ID.Loc, "compare operands must have the same type");
    3281             : 
    3282         307 :     CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
    3283             : 
    3284         307 :     if (Opc == Instruction::FCmp) {
    3285             :       if (!Val0->getType()->isFPOrFPVectorTy())
    3286           0 :         return Error(ID.Loc, "fcmp requires floating point operands");
    3287           6 :       ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
    3288             :     } else {
    3289             :       assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
    3290         301 :       if (!Val0->getType()->isIntOrIntVectorTy() &&
    3291             :           !Val0->getType()->isPtrOrPtrVectorTy())
    3292           0 :         return Error(ID.Loc, "icmp requires pointer or integer operands");
    3293         301 :       ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
    3294             :     }
    3295         307 :     ID.Kind = ValID::t_Constant;
    3296         307 :     return false;
    3297             :   }
    3298             : 
    3299             :   // Binary Operators.
    3300         631 :   case lltok::kw_add:
    3301             :   case lltok::kw_fadd:
    3302             :   case lltok::kw_sub:
    3303             :   case lltok::kw_fsub:
    3304             :   case lltok::kw_mul:
    3305             :   case lltok::kw_fmul:
    3306             :   case lltok::kw_udiv:
    3307             :   case lltok::kw_sdiv:
    3308             :   case lltok::kw_fdiv:
    3309             :   case lltok::kw_urem:
    3310             :   case lltok::kw_srem:
    3311             :   case lltok::kw_frem:
    3312             :   case lltok::kw_shl:
    3313             :   case lltok::kw_lshr:
    3314             :   case lltok::kw_ashr: {
    3315             :     bool NUW = false;
    3316             :     bool NSW = false;
    3317             :     bool Exact = false;
    3318         631 :     unsigned Opc = Lex.getUIntVal();
    3319             :     Constant *Val0, *Val1;
    3320         631 :     Lex.Lex();
    3321         631 :     LocTy ModifierLoc = Lex.getLoc();
    3322         631 :     if (Opc == Instruction::Add || Opc == Instruction::Sub ||
    3323         265 :         Opc == Instruction::Mul || Opc == Instruction::Shl) {
    3324             :       if (EatIfPresent(lltok::kw_nuw))
    3325             :         NUW = true;
    3326             :       if (EatIfPresent(lltok::kw_nsw)) {
    3327             :         NSW = true;
    3328             :         if (EatIfPresent(lltok::kw_nuw))
    3329             :           NUW = true;
    3330             :       }
    3331         296 :     } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
    3332         148 :                Opc == Instruction::LShr || Opc == Instruction::AShr) {
    3333             :       if (EatIfPresent(lltok::kw_exact))
    3334             :         Exact = true;
    3335             :     }
    3336        1262 :     if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
    3337        1262 :         ParseGlobalTypeAndValue(Val0) ||
    3338        1262 :         ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
    3339        1893 :         ParseGlobalTypeAndValue(Val1) ||
    3340         631 :         ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
    3341           0 :       return true;
    3342         631 :     if (Val0->getType() != Val1->getType())
    3343           0 :       return Error(ID.Loc, "operands of constexpr must have same type");
    3344         631 :     if (!Val0->getType()->isIntOrIntVectorTy()) {
    3345          21 :       if (NUW)
    3346           0 :         return Error(ModifierLoc, "nuw only applies to integer operations");
    3347          21 :       if (NSW)
    3348           0 :         return Error(ModifierLoc, "nsw only applies to integer operations");
    3349             :     }
    3350             :     // Check that the type is valid for the operator.
    3351             :     switch (Opc) {
    3352             :     case Instruction::Add:
    3353             :     case Instruction::Sub:
    3354             :     case Instruction::Mul:
    3355             :     case Instruction::UDiv:
    3356             :     case Instruction::SDiv:
    3357             :     case Instruction::URem:
    3358             :     case Instruction::SRem:
    3359             :     case Instruction::Shl:
    3360             :     case Instruction::AShr:
    3361             :     case Instruction::LShr:
    3362         611 :       if (!Val0->getType()->isIntOrIntVectorTy())
    3363           1 :         return Error(ID.Loc, "constexpr requires integer operands");
    3364             :       break;
    3365             :     case Instruction::FAdd:
    3366             :     case Instruction::FSub:
    3367             :     case Instruction::FMul:
    3368             :     case Instruction::FDiv:
    3369             :     case Instruction::FRem:
    3370             :       if (!Val0->getType()->isFPOrFPVectorTy())
    3371           0 :         return Error(ID.Loc, "constexpr requires fp operands");
    3372             :       break;
    3373           0 :     default: llvm_unreachable("Unknown binary operator!");
    3374             :     }
    3375             :     unsigned Flags = 0;
    3376         630 :     if (NUW)   Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
    3377         630 :     if (NSW)   Flags |= OverflowingBinaryOperator::NoSignedWrap;
    3378         630 :     if (Exact) Flags |= PossiblyExactOperator::IsExact;
    3379         630 :     Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
    3380         630 :     ID.ConstantVal = C;
    3381         630 :     ID.Kind = ValID::t_Constant;
    3382         630 :     return false;
    3383             :   }
    3384             : 
    3385             :   // Logical Operations
    3386         114 :   case lltok::kw_and:
    3387             :   case lltok::kw_or:
    3388             :   case lltok::kw_xor: {
    3389         114 :     unsigned Opc = Lex.getUIntVal();
    3390             :     Constant *Val0, *Val1;
    3391         114 :     Lex.Lex();
    3392         228 :     if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
    3393         228 :         ParseGlobalTypeAndValue(Val0) ||
    3394         228 :         ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
    3395         342 :         ParseGlobalTypeAndValue(Val1) ||
    3396         114 :         ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
    3397           0 :       return true;
    3398         114 :     if (Val0->getType() != Val1->getType())
    3399           0 :       return Error(ID.Loc, "operands of constexpr must have same type");
    3400         114 :     if (!Val0->getType()->isIntOrIntVectorTy())
    3401           0 :       return Error(ID.Loc,
    3402             :                    "constexpr requires integer or integer vector operands");
    3403         114 :     ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
    3404         114 :     ID.Kind = ValID::t_Constant;
    3405         114 :     return false;
    3406             :   }
    3407             : 
    3408       36916 :   case lltok::kw_getelementptr:
    3409             :   case lltok::kw_shufflevector:
    3410             :   case lltok::kw_insertelement:
    3411             :   case lltok::kw_extractelement:
    3412             :   case lltok::kw_select: {
    3413       36916 :     unsigned Opc = Lex.getUIntVal();
    3414             :     SmallVector<Constant*, 16> Elts;
    3415             :     bool InBounds = false;
    3416             :     Type *Ty;
    3417       36916 :     Lex.Lex();
    3418             : 
    3419       36916 :     if (Opc == Instruction::GetElementPtr)
    3420             :       InBounds = EatIfPresent(lltok::kw_inbounds);
    3421             : 
    3422       36916 :     if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
    3423             :       return true;
    3424             : 
    3425       36916 :     LocTy ExplicitTypeLoc = Lex.getLoc();
    3426       36916 :     if (Opc == Instruction::GetElementPtr) {
    3427       73654 :       if (ParseType(Ty) ||
    3428       36827 :           ParseToken(lltok::comma, "expected comma after getelementptr's type"))
    3429           0 :         return true;
    3430             :     }
    3431             : 
    3432             :     Optional<unsigned> InRangeOp;
    3433       37005 :     if (ParseGlobalValueVector(
    3434       73831 :             Elts, Opc == Instruction::GetElementPtr ? &InRangeOp : nullptr) ||
    3435       36915 :         ParseToken(lltok::rparen, "expected ')' in constantexpr"))
    3436           1 :       return true;
    3437             : 
    3438       36915 :     if (Opc == Instruction::GetElementPtr) {
    3439      110478 :       if (Elts.size() == 0 ||
    3440       36826 :           !Elts[0]->getType()->isPtrOrPtrVectorTy())
    3441           0 :         return Error(ID.Loc, "base of getelementptr must be a pointer");
    3442             : 
    3443             :       Type *BaseType = Elts[0]->getType();
    3444             :       auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
    3445       36826 :       if (Ty != BasePointerType->getElementType())
    3446           0 :         return Error(
    3447             :             ExplicitTypeLoc,
    3448             :             "explicit pointee type doesn't match operand's pointee type");
    3449             : 
    3450             :       unsigned GEPWidth =
    3451       36826 :           BaseType->isVectorTy() ? BaseType->getVectorNumElements() : 0;
    3452             : 
    3453       36826 :       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
    3454      110718 :       for (Constant *Val : Indices) {
    3455       73894 :         Type *ValTy = Val->getType();
    3456       73894 :         if (!ValTy->isIntOrIntVectorTy())
    3457           0 :           return Error(ID.Loc, "getelementptr index must be an integer");
    3458       73894 :         if (ValTy->isVectorTy()) {
    3459             :           unsigned ValNumEl = ValTy->getVectorNumElements();
    3460          38 :           if (GEPWidth && (ValNumEl != GEPWidth))
    3461           2 :             return Error(
    3462             :                 ID.Loc,
    3463             :                 "getelementptr vector index has a wrong number of elements");
    3464             :           // GEPWidth may have been unknown because the base is a scalar,
    3465             :           // but it is known now.
    3466             :           GEPWidth = ValNumEl;
    3467             :         }
    3468             :       }
    3469             : 
    3470             :       SmallPtrSet<Type*, 4> Visited;
    3471       36824 :       if (!Indices.empty() && !Ty->isSized(&Visited))
    3472           0 :         return Error(ID.Loc, "base element of getelementptr must be sized");
    3473             : 
    3474       36824 :       if (!GetElementPtrInst::getIndexedType(Ty, Indices))
    3475           0 :         return Error(ID.Loc, "invalid getelementptr indices");
    3476             : 
    3477       36824 :       if (InRangeOp) {
    3478          71 :         if (*InRangeOp == 0)
    3479           0 :           return Error(ID.Loc,
    3480             :                        "inrange keyword may not appear on pointer operand");
    3481          71 :         --*InRangeOp;
    3482             :       }
    3483             : 
    3484      110472 :       ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices,
    3485             :                                                       InBounds, InRangeOp);
    3486          89 :     } else if (Opc == Instruction::Select) {
    3487          54 :       if (Elts.size() != 3)
    3488           0 :         return Error(ID.Loc, "expected three operands to select");
    3489          54 :       if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
    3490             :                                                               Elts[2]))
    3491           0 :         return Error(ID.Loc, Reason);
    3492          54 :       ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
    3493          35 :     } else if (Opc == Instruction::ShuffleVector) {
    3494          10 :       if (Elts.size() != 3)
    3495           0 :         return Error(ID.Loc, "expected three operands to shufflevector");
    3496          10 :       if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
    3497           0 :         return Error(ID.Loc, "invalid operands to shufflevector");
    3498          10 :       ID.ConstantVal =
    3499          10 :                  ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
    3500          25 :     } else if (Opc == Instruction::ExtractElement) {
    3501          23 :       if (Elts.size() != 2)
    3502           0 :         return Error(ID.Loc, "expected two operands to extractelement");
    3503          23 :       if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
    3504           0 :         return Error(ID.Loc, "invalid extractelement operands");
    3505          23 :       ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
    3506             :     } else {
    3507             :       assert(Opc == Instruction::InsertElement && "Unknown opcode");
    3508           2 :       if (Elts.size() != 3)
    3509           0 :       return Error(ID.Loc, "expected three operands to insertelement");
    3510           2 :       if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
    3511           0 :         return Error(ID.Loc, "invalid insertelement operands");
    3512           2 :       ID.ConstantVal =
    3513           2 :                  ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
    3514             :     }
    3515             : 
    3516       36913 :     ID.Kind = ValID::t_Constant;
    3517       36913 :     return false;
    3518             :   }
    3519             :   }
    3520             : 
    3521     3588128 :   Lex.Lex();
    3522     3588128 :   return false;
    3523             : }
    3524             : 
    3525             : /// ParseGlobalValue - Parse a global value with the specified type.
    3526     1083435 : bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
    3527     1083435 :   C = nullptr;
    3528     2166870 :   ValID ID;
    3529     1083435 :   Value *V = nullptr;
    3530     2166853 :   bool Parsed = ParseValID(ID) ||
    3531     1083418 :                 ConvertValIDToValue(Ty, ID, V, nullptr, /*IsCall=*/false);
    3532     2166851 :   if (V && !(C = dyn_cast<Constant>(V)))
    3533           0 :     return Error(ID.Loc, "global values must be constants");
    3534             :   return Parsed;
    3535             : }
    3536             : 
    3537     1062141 : bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
    3538     1062141 :   Type *Ty = nullptr;
    3539     2124282 :   return ParseType(Ty) ||
    3540     1062141 :          ParseGlobalValue(Ty, V);
    3541             : }
    3542             : 
    3543      323734 : bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
    3544      323734 :   C = nullptr;
    3545             : 
    3546      323734 :   LocTy KwLoc = Lex.getLoc();
    3547             :   if (!EatIfPresent(lltok::kw_comdat))
    3548             :     return false;
    3549             : 
    3550             :   if (EatIfPresent(lltok::lparen)) {
    3551         306 :     if (Lex.getKind() != lltok::ComdatVar)
    3552           0 :       return TokError("expected comdat variable");
    3553         612 :     C = getComdat(Lex.getStrVal(), Lex.getLoc());
    3554             :     Lex.Lex();
    3555         306 :     if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
    3556           0 :       return true;
    3557             :   } else {
    3558         533 :     if (GlobalName.empty())
    3559           1 :       return TokError("comdat cannot be unnamed");
    3560         716 :     C = getComdat(GlobalName, KwLoc);
    3561             :   }
    3562             : 
    3563             :   return false;
    3564             : }
    3565             : 
    3566             : /// ParseGlobalValueVector
    3567             : ///   ::= /*empty*/
    3568             : ///   ::= [inrange] TypeAndValue (',' [inrange] TypeAndValue)*
    3569      116687 : bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
    3570             :                                       Optional<unsigned> *InRangeOp) {
    3571             :   // Empty list.
    3572      116677 :   if (Lex.getKind() == lltok::rbrace ||
    3573      116658 :       Lex.getKind() == lltok::rsquare ||
    3574      233345 :       Lex.getKind() == lltok::greater ||
    3575             :       Lex.getKind() == lltok::rparen)
    3576             :     return false;
    3577             : 
    3578             :   do {
    3579     1049397 :     if (InRangeOp && !*InRangeOp && EatIfPresent(lltok::kw_inrange))
    3580          71 :       *InRangeOp = Elts.size();
    3581             : 
    3582             :     Constant *C;
    3583     1049397 :     if (ParseGlobalTypeAndValue(C)) return true;
    3584     1049391 :     Elts.push_back(C);
    3585             :   } while (EatIfPresent(lltok::comma));
    3586             : 
    3587             :   return false;
    3588             : }
    3589             : 
    3590       20902 : bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
    3591             :   SmallVector<Metadata *, 16> Elts;
    3592       20902 :   if (ParseMDNodeVector(Elts))
    3593             :     return true;
    3594             : 
    3595       41150 :   MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
    3596       20894 :   return false;
    3597             : }
    3598             : 
    3599             : /// MDNode:
    3600             : ///  ::= !{ ... }
    3601             : ///  ::= !7
    3602             : ///  ::= !DILocation(...)
    3603       33274 : bool LLParser::ParseMDNode(MDNode *&N) {
    3604       33274 :   if (Lex.getKind() == lltok::MetadataVar)
    3605          97 :     return ParseSpecializedMDNode(N);
    3606             : 
    3607       66354 :   return ParseToken(lltok::exclaim, "expected '!' here") ||
    3608       33177 :          ParseMDNodeTail(N);
    3609             : }
    3610             : 
    3611      111953 : bool LLParser::ParseMDNodeTail(MDNode *&N) {
    3612             :   // !{ ... }
    3613      111953 :   if (Lex.getKind() == lltok::lbrace)
    3614         326 :     return ParseMDTuple(N);
    3615             : 
    3616             :   // !42
    3617      111627 :   return ParseMDNodeID(N);
    3618             : }
    3619             : 
    3620             : namespace {
    3621             : 
    3622             : /// Structure to represent an optional metadata field.
    3623             : template <class FieldTy> struct MDFieldImpl {
    3624             :   typedef MDFieldImpl ImplTy;
    3625             :   FieldTy Val;
    3626             :   bool Seen;
    3627             : 
    3628           0 :   void assign(FieldTy Val) {
    3629           0 :     Seen = true;
    3630           0 :     this->Val = std::move(Val);
    3631           0 :   }
    3632           0 : 
    3633           0 :   explicit MDFieldImpl(FieldTy Default)
    3634           0 :       : Val(std::move(Default)), Seen(false) {}
    3635           0 : };
    3636           0 : 
    3637           0 : /// Structure to represent an optional metadata field that
    3638           0 : /// can be of either type (A or B) and encapsulates the
    3639           0 : /// MD<typeofA>Field and MD<typeofB>Field structs, so not
    3640           0 : /// to reimplement the specifics for representing each Field.
    3641           0 : template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
    3642           0 :   typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
    3643           0 :   FieldTypeA A;
    3644           0 :   FieldTypeB B;
    3645           0 :   bool Seen;
    3646           0 : 
    3647           0 :   enum {
    3648           0 :     IsInvalid = 0,
    3649           0 :     IsTypeA = 1,
    3650           0 :     IsTypeB = 2
    3651           0 :   } WhatIs;
    3652           0 : 
    3653           0 :   void assign(FieldTypeA A) {
    3654           0 :     Seen = true;
    3655           0 :     this->A = std::move(A);
    3656             :     WhatIs = IsTypeA;
    3657      268698 :   }
    3658      268698 : 
    3659             :   void assign(FieldTypeB B) {
    3660             :     Seen = true;
    3661             :     this->B = std::move(B);
    3662             :     WhatIs = IsTypeB;
    3663             :   }
    3664             : 
    3665             :   explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
    3666             :       : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
    3667             :         WhatIs(IsInvalid) {}
    3668             : };
    3669             : 
    3670             : struct MDUnsignedField : public MDFieldImpl<uint64_t> {
    3671             :   uint64_t Max;
    3672             : 
    3673             :   MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
    3674             :       : ImplTy(Default), Max(Max) {}
    3675             : };
    3676             : 
    3677             : struct LineField : public MDUnsignedField {
    3678           0 :   LineField() : MDUnsignedField(0, UINT32_MAX) {}
    3679           0 : };
    3680           0 : 
    3681             : struct ColumnField : public MDUnsignedField {
    3682             :   ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
    3683           0 : };
    3684           0 : 
    3685           0 : struct DwarfTagField : public MDUnsignedField {
    3686           0 :   DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
    3687           0 :   DwarfTagField(dwarf::Tag DefaultTag)
    3688             :       : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
    3689        6358 : };
    3690             : 
    3691        6358 : struct DwarfMacinfoTypeField : public MDUnsignedField {
    3692             :   DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
    3693             :   DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
    3694             :     : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
    3695             : };
    3696             : 
    3697             : struct DwarfAttEncodingField : public MDUnsignedField {
    3698      140278 :   DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
    3699             : };
    3700             : 
    3701             : struct DwarfVirtualityField : public MDUnsignedField {
    3702             :   DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
    3703             : };
    3704             : 
    3705             : struct DwarfLangField : public MDUnsignedField {
    3706             :   DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
    3707             : };
    3708             : 
    3709             : struct DwarfCCField : public MDUnsignedField {
    3710             :   DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
    3711             : };
    3712             : 
    3713             : struct EmissionKindField : public MDUnsignedField {
    3714             :   EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
    3715             : };
    3716             : 
    3717             : struct NameTableKindField : public MDUnsignedField {
    3718             :   NameTableKindField()
    3719             :       : MDUnsignedField(
    3720             :             0, (unsigned)
    3721             :                    DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
    3722             : };
    3723             : 
    3724             : struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
    3725             :   DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
    3726             : };
    3727             : 
    3728             : struct MDSignedField : public MDFieldImpl<int64_t> {
    3729             :   int64_t Min;
    3730             :   int64_t Max;
    3731             : 
    3732             :   MDSignedField(int64_t Default = 0)
    3733             :       : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
    3734             :   MDSignedField(int64_t Default, int64_t Min, int64_t Max)
    3735             :       : ImplTy(Default), Min(Min), Max(Max) {}
    3736             : };
    3737             : 
    3738             : struct MDBoolField : public MDFieldImpl<bool> {
    3739             :   MDBoolField(bool Default = false) : ImplTy(Default) {}
    3740             : };
    3741             : 
    3742             : struct MDField : public MDFieldImpl<Metadata *> {
    3743             :   bool AllowNull;
    3744             : 
    3745             :   MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
    3746             : };
    3747             : 
    3748             : struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
    3749             :   MDConstant() : ImplTy(nullptr) {}
    3750             : };
    3751             : 
    3752             : struct MDStringField : public MDFieldImpl<MDString *> {
    3753             :   bool AllowEmpty;
    3754             :   MDStringField(bool AllowEmpty = true)
    3755             :       : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
    3756             : };
    3757         392 : 
    3758             : struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
    3759        6146 :   MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
    3760             : };
    3761             : 
    3762             : struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
    3763             :   ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
    3764             : };
    3765             : 
    3766             : struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
    3767             :   MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
    3768             :       : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
    3769      152329 : 
    3770             :   MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
    3771             :                     bool AllowNull = true)
    3772             :       : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
    3773             : 
    3774             :   bool isMDSignedField() const { return WhatIs == IsTypeA; }
    3775             :   bool isMDField() const { return WhatIs == IsTypeB; }
    3776             :   int64_t getMDSignedValue() const {
    3777             :     assert(isMDSignedField() && "Wrong field type");
    3778             :     return A.Val;
    3779       72000 :   }
    3780             :   Metadata *getMDFieldValue() const {
    3781             :     assert(isMDField() && "Wrong field type");
    3782             :     return B.Val;
    3783             :   }
    3784             : };
    3785             : 
    3786             : struct MDSignedOrUnsignedField
    3787             :     : MDEitherFieldImpl<MDSignedField, MDUnsignedField> {
    3788             :   MDSignedOrUnsignedField() : ImplTy(MDSignedField(0), MDUnsignedField(0)) {}
    3789             : 
    3790             :   bool isMDSignedField() const { return WhatIs == IsTypeA; }
    3791             :   bool isMDUnsignedField() const { return WhatIs == IsTypeB; }
    3792             :   int64_t getMDSignedValue() const {
    3793             :     assert(isMDSignedField() && "Wrong field type");
    3794             :     return A.Val;
    3795             :   }
    3796             :   uint64_t getMDUnsignedValue() const {
    3797             :     assert(isMDUnsignedField() && "Wrong field type");
    3798           0 :     return B.Val;
    3799           0 :   }
    3800           0 : };
    3801             : 
    3802           0 : } // end anonymous namespace
    3803             : 
    3804           0 : namespace llvm {
    3805             : 
    3806           0 : template <>
    3807             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3808             :                             MDUnsignedField &Result) {
    3809             :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    3810             :     return TokError("expected unsigned integer");
    3811             : 
    3812             :   auto &U = Lex.getAPSIntVal();
    3813             :   if (U.ugt(Result.Max))
    3814           0 :     return TokError("value for '" + Name + "' too large, limit is " +
    3815             :                     Twine(Result.Max));
    3816           0 :   Result.assign(U.getZExtValue());
    3817             :   assert(Result.Val <= Result.Max && "Expected value in range");
    3818           0 :   Lex.Lex();
    3819             :   return false;
    3820           0 : }
    3821             : 
    3822           0 : template <>
    3823             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
    3824             :   return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3825             : }
    3826             : template <>
    3827             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
    3828             :   return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3829             : }
    3830             : 
    3831           0 : template <>
    3832             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
    3833           0 :   if (Lex.getKind() == lltok::APSInt)
    3834           0 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3835             : 
    3836             :   if (Lex.getKind() != lltok::DwarfTag)
    3837           0 :     return TokError("expected DWARF tag");
    3838           0 : 
    3839           0 :   unsigned Tag = dwarf::getTag(Lex.getStrVal());
    3840             :   if (Tag == dwarf::DW_TAG_invalid)
    3841             :     return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
    3842           0 :   assert(Tag <= Result.Max && "Expected valid DWARF tag");
    3843           0 : 
    3844             :   Result.assign(Tag);
    3845             :   Lex.Lex();
    3846             :   return false;
    3847           0 : }
    3848       20816 : 
    3849             : template <>
    3850             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3851           0 :                             DwarfMacinfoTypeField &Result) {
    3852        8287 :   if (Lex.getKind() == lltok::APSInt)
    3853             :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3854             : 
    3855             :   if (Lex.getKind() != lltok::DwarfMacinfo)
    3856           0 :     return TokError("expected DWARF macinfo type");
    3857           0 : 
    3858           0 :   unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
    3859             :   if (Macinfo == dwarf::DW_MACINFO_invalid)
    3860           0 :     return TokError(
    3861           0 :         "invalid DWARF macinfo type" + Twine(" '") + Lex.getStrVal() + "'");
    3862             :   assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
    3863           0 : 
    3864           0 :   Result.assign(Macinfo);
    3865           0 :   Lex.Lex();
    3866             :   return false;
    3867             : }
    3868           0 : 
    3869           0 : template <>
    3870           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3871             :                             DwarfVirtualityField &Result) {
    3872             :   if (Lex.getKind() == lltok::APSInt)
    3873             :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3874           0 : 
    3875             :   if (Lex.getKind() != lltok::DwarfVirtuality)
    3876           0 :     return TokError("expected DWARF virtuality code");
    3877           0 : 
    3878             :   unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
    3879           0 :   if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
    3880           0 :     return TokError("invalid DWARF virtuality code" + Twine(" '") +
    3881             :                     Lex.getStrVal() + "'");
    3882           0 :   assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
    3883           0 :   Result.assign(Virtuality);
    3884             :   Lex.Lex();
    3885           0 :   return false;
    3886             : }
    3887             : 
    3888           0 : template <>
    3889           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
    3890           0 :   if (Lex.getKind() == lltok::APSInt)
    3891             :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3892             : 
    3893             :   if (Lex.getKind() != lltok::DwarfLang)
    3894           0 :     return TokError("expected DWARF language");
    3895             : 
    3896           0 :   unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
    3897           0 :   if (!Lang)
    3898             :     return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
    3899           0 :                     "'");
    3900           0 :   assert(Lang <= Result.Max && "Expected valid DWARF language");
    3901             :   Result.assign(Lang);
    3902           0 :   Lex.Lex();
    3903           0 :   return false;
    3904           0 : }
    3905           0 : 
    3906             : template <>
    3907           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
    3908           0 :   if (Lex.getKind() == lltok::APSInt)
    3909           0 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3910             : 
    3911             :   if (Lex.getKind() != lltok::DwarfCC)
    3912             :     return TokError("expected DWARF calling convention");
    3913           0 : 
    3914           0 :   unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
    3915           0 :   if (!CC)
    3916             :     return TokError("invalid DWARF calling convention" + Twine(" '") + Lex.getStrVal() +
    3917           0 :                     "'");
    3918           0 :   assert(CC <= Result.Max && "Expected valid DWARF calling convention");
    3919             :   Result.assign(CC);
    3920           0 :   Lex.Lex();
    3921           0 :   return false;
    3922           0 : }
    3923           0 : 
    3924             : template <>
    3925           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, EmissionKindField &Result) {
    3926           0 :   if (Lex.getKind() == lltok::APSInt)
    3927           0 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3928             : 
    3929             :   if (Lex.getKind() != lltok::EmissionKind)
    3930             :     return TokError("expected emission kind");
    3931           0 : 
    3932           0 :   auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
    3933           0 :   if (!Kind)
    3934             :     return TokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
    3935           0 :                     "'");
    3936           0 :   assert(*Kind <= Result.Max && "Expected valid emission kind");
    3937             :   Result.assign(*Kind);
    3938           0 :   Lex.Lex();
    3939           0 :   return false;
    3940           0 : }
    3941           0 : 
    3942             : template <>
    3943           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3944           0 :                             NameTableKindField &Result) {
    3945           0 :   if (Lex.getKind() == lltok::APSInt)
    3946             :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3947             : 
    3948             :   if (Lex.getKind() != lltok::NameTableKind)
    3949           0 :     return TokError("expected nameTable kind");
    3950           0 : 
    3951           0 :   auto Kind = DICompileUnit::getNameTableKind(Lex.getStrVal());
    3952             :   if (!Kind)
    3953           0 :     return TokError("invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
    3954           0 :                     "'");
    3955             :   assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
    3956           0 :   Result.assign((unsigned)*Kind);
    3957           0 :   Lex.Lex();
    3958           0 :   return false;
    3959           0 : }
    3960             : 
    3961           0 : template <>
    3962           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3963           0 :                             DwarfAttEncodingField &Result) {
    3964             :   if (Lex.getKind() == lltok::APSInt)
    3965             :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3966             : 
    3967           0 :   if (Lex.getKind() != lltok::DwarfAttEncoding)
    3968             :     return TokError("expected DWARF type attribute encoding");
    3969           0 : 
    3970           0 :   unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
    3971             :   if (!Encoding)
    3972           0 :     return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
    3973           0 :                     Lex.getStrVal() + "'");
    3974             :   assert(Encoding <= Result.Max && "Expected valid DWARF language");
    3975           0 :   Result.assign(Encoding);
    3976           0 :   Lex.Lex();
    3977           0 :   return false;
    3978           0 : }
    3979             : 
    3980           0 : /// DIFlagField
    3981           0 : ///  ::= uint32
    3982           0 : ///  ::= DIFlagVector
    3983             : ///  ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
    3984             : template <>
    3985             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
    3986           0 : 
    3987             :   // Parser for a single flag.
    3988           0 :   auto parseFlag = [&](DINode::DIFlags &Val) {
    3989           0 :     if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
    3990             :       uint32_t TempVal = static_cast<uint32_t>(Val);
    3991           0 :       bool Res = ParseUInt32(TempVal);
    3992           0 :       Val = static_cast<DINode::DIFlags>(TempVal);
    3993             :       return Res;
    3994           0 :     }
    3995           0 : 
    3996           0 :     if (Lex.getKind() != lltok::DIFlag)
    3997           0 :       return TokError("expected debug info flag");
    3998             : 
    3999           0 :     Val = DINode::getFlag(Lex.getStrVal());
    4000           0 :     if (!Val)
    4001           0 :       return TokError(Twine("invalid debug info flag flag '") +
    4002             :                       Lex.getStrVal() + "'");
    4003             :     Lex.Lex();
    4004             :     return false;
    4005             :   };
    4006             : 
    4007             :   // Parse the flags and combine them together.
    4008             :   DINode::DIFlags Combined = DINode::FlagZero;
    4009           0 :   do {
    4010             :     DINode::DIFlags Val;
    4011             :     if (parseFlag(Val))
    4012             :       return true;
    4013             :     Combined |= Val;
    4014             :   } while (EatIfPresent(lltok::bar));
    4015             : 
    4016             :   Result.assign(Combined);
    4017             :   return false;
    4018             : }
    4019             : 
    4020             : template <>
    4021             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    4022             :                             MDSignedField &Result) {
    4023             :   if (Lex.getKind() != lltok::APSInt)
    4024             :     return TokError("expected signed integer");
    4025             : 
    4026             :   auto &S = Lex.getAPSIntVal();
    4027             :   if (S < Result.Min)
    4028             :     return TokError("value for '" + Name + "' too small, limit is " +
    4029           0 :                     Twine(Result.Min));
    4030             :   if (S > Result.Max)
    4031             :     return TokError("value for '" + Name + "' too large, limit is " +
    4032             :                     Twine(Result.Max));
    4033             :   Result.assign(S.getExtValue());
    4034             :   assert(Result.Val >= Result.Min && "Expected value in range");
    4035           0 :   assert(Result.Val <= Result.Max && "Expected value in range");
    4036           0 :   Lex.Lex();
    4037           0 :   return false;
    4038             : }
    4039             : 
    4040             : template <>
    4041           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
    4042             :   switch (Lex.getKind()) {
    4043             :   default:
    4044             :     return TokError("expected 'true' or 'false'");
    4045           0 :   case lltok::kw_true:
    4046             :     Result.assign(true);
    4047           0 :     break;
    4048           0 :   case lltok::kw_false:
    4049             :     Result.assign(false);
    4050             :     break;
    4051           0 :   }
    4052           0 :   Lex.Lex();
    4053           0 :   return false;
    4054           0 : }
    4055           0 : 
    4056           0 : template <>
    4057             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
    4058             :   if (Lex.getKind() == lltok::kw_null) {
    4059             :     if (!Result.AllowNull)
    4060           0 :       return TokError("'" + Name + "' cannot be null");
    4061           0 :     Lex.Lex();
    4062             :     Result.assign(nullptr);
    4063             :     return false;
    4064             :   }
    4065           0 : 
    4066           0 :   Metadata *MD;
    4067             :   if (ParseMetadata(MD, nullptr))
    4068           0 :     return true;
    4069           0 : 
    4070             :   Result.assign(MD);
    4071             :   return false;
    4072           0 : }
    4073             : 
    4074             : template <>
    4075             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    4076           0 :                             MDSignedOrMDField &Result) {
    4077           0 :   // Try to parse a signed int.
    4078             :   if (Lex.getKind() == lltok::APSInt) {
    4079             :     MDSignedField Res = Result.A;
    4080             :     if (!ParseMDField(Loc, Name, Res)) {
    4081           0 :       Result.assign(Res);
    4082           0 :       return false;
    4083           0 :     }
    4084           0 :     return true;
    4085           0 :   }
    4086             : 
    4087           0 :   // Otherwise, try to parse as an MDField.
    4088             :   MDField Res = Result.B;
    4089             :   if (!ParseMDField(Loc, Name, Res)) {
    4090             :     Result.assign(Res);
    4091           0 :     return false;
    4092           0 :   }
    4093             : 
    4094           0 :   return true;
    4095           0 : }
    4096             : 
    4097             : template <>
    4098             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    4099           0 :                             MDSignedOrUnsignedField &Result) {
    4100             :   if (Lex.getKind() != lltok::APSInt)
    4101             :     return false;
    4102           0 : 
    4103           0 :   if (Lex.getAPSIntVal().isSigned()) {
    4104           0 :     MDSignedField Res = Result.A;
    4105             :     if (ParseMDField(Loc, Name, Res))
    4106           0 :       return true;
    4107             :     Result.assign(Res);
    4108           0 :     return false;
    4109             :   }
    4110             : 
    4111             :   MDUnsignedField Res = Result.B;
    4112           0 :   if (ParseMDField(Loc, Name, Res))
    4113           0 :     return true;
    4114           0 :   Result.assign(Res);
    4115           0 :   return false;
    4116             : }
    4117             : 
    4118             : template <>
    4119             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
    4120             :   LocTy ValueLoc = Lex.getLoc();
    4121             :   std::string S;
    4122           0 :   if (ParseStringConstant(S))
    4123             :     return true;
    4124           0 : 
    4125           0 :   if (!Result.AllowEmpty && S.empty())
    4126             :     return Error(ValueLoc, "'" + Name + "' cannot be empty");
    4127           0 : 
    4128           0 :   Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
    4129           0 :   return false;
    4130           0 : }
    4131             : 
    4132           0 : template <>
    4133             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
    4134             :   SmallVector<Metadata *, 4> MDs;
    4135           0 :   if (ParseMDNodeVector(MDs))
    4136           0 :     return true;
    4137           0 : 
    4138             :   Result.assign(std::move(MDs));
    4139           0 :   return false;
    4140             : }
    4141             : 
    4142             : template <>
    4143           0 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    4144           0 :                             ChecksumKindField &Result) {
    4145             :   Optional<DIFile::ChecksumKind> CSKind =
    4146           0 :       DIFile::getChecksumKind(Lex.getStrVal());
    4147           0 : 
    4148             :   if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
    4149           0 :     return TokError(
    4150           0 :         "invalid checksum kind" + Twine(" '") + Lex.getStrVal() + "'");
    4151             : 
    4152           0 :   Result.assign(*CSKind);
    4153           0 :   Lex.Lex();
    4154             :   return false;
    4155             : }
    4156             : 
    4157           0 : } // end namespace llvm
    4158             : 
    4159           0 : template <class ParserTy>
    4160           0 : bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
    4161             :   do {
    4162           0 :     if (Lex.getKind() != lltok::LabelStr)
    4163           0 :       return TokError("expected field label here");
    4164             : 
    4165             :     if (parseField())
    4166             :       return true;
    4167           0 :   } while (EatIfPresent(lltok::comma));
    4168             : 
    4169             :   return false;
    4170           0 : }
    4171             : 
    4172           0 : template <class ParserTy>
    4173             : bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
    4174           0 :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
    4175             :   Lex.Lex();
    4176           0 : 
    4177           0 :   if (ParseToken(lltok::lparen, "expected '(' here"))
    4178           0 :     return true;
    4179             :   if (Lex.getKind() != lltok::rparen)
    4180             :     if (ParseMDFieldsImplBody(parseField))
    4181             :       return true;
    4182             : 
    4183             :   ClosingLoc = Lex.getLoc();
    4184       36926 :   return ParseToken(lltok::rparen, "expected ')' here");
    4185             : }
    4186      152329 : 
    4187           0 : template <class FieldTy>
    4188             : bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
    4189      152329 :   if (Result.Seen)
    4190             :     return TokError("field '" + Name + "' cannot be specified more than once");
    4191             : 
    4192             :   LocTy Loc = Lex.getLoc();
    4193             :   Lex.Lex();
    4194             :   return ParseMDField(Loc, Name, Result);
    4195         263 : }
    4196             : 
    4197         776 : bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
    4198           0 :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
    4199             : 
    4200         776 : #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \
    4201             :   if (Lex.getStrVal() == #CLASS)                                               \
    4202             :     return Parse##CLASS(N, IsDistinct);
    4203             : #include "llvm/IR/Metadata.def"
    4204             : 
    4205             :   return TokError("expected metadata type");
    4206        9419 : }
    4207             : 
    4208       26524 : #define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
    4209           0 : #define NOP_FIELD(NAME, TYPE, INIT)
    4210             : #define REQUIRE_FIELD(NAME, TYPE, INIT)                                        \
    4211       26524 :   if (!NAME.Seen)                                                              \
    4212             :     return Error(ClosingLoc, "missing required field '" #NAME "'");
    4213             : #define PARSE_MD_FIELD(NAME, TYPE, DEFAULT)                                    \
    4214             :   if (Lex.getStrVal() == #NAME)                                                \
    4215             :     return ParseMDField(#NAME, NAME);
    4216             : #define PARSE_MD_FIELDS()                                                      \
    4217          39 :   VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD)                                \
    4218             :   do {                                                                         \
    4219          63 :     LocTy ClosingLoc;                                                          \
    4220           0 :     if (ParseMDFieldsImpl([&]() -> bool {                                      \
    4221             :       VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
    4222          63 :       return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
    4223             :     }, ClosingLoc))                                                            \
    4224             :       return true;                                                             \
    4225             :     VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD)                                  \
    4226             :   } while (false)
    4227             : #define GET_OR_DISTINCT(CLASS, ARGS)                                           \
    4228         196 :   (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
    4229             : 
    4230         234 : /// ParseDILocationFields:
    4231           0 : ///   ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
    4232             : ///   isImplicitCode: true)
    4233         234 : bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
    4234             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4235             :   OPTIONAL(line, LineField, );                                                 \
    4236             :   OPTIONAL(column, ColumnField, );                                             \
    4237             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4238             :   OPTIONAL(inlinedAt, MDField, );                                              \
    4239        6162 :   OPTIONAL(isImplicitCode, MDBoolField, (false));
    4240             :   PARSE_MD_FIELDS();
    4241       12332 : #undef VISIT_MD_FIELDS
    4242           0 : 
    4243             :   Result =
    4244       12332 :       GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val,
    4245             :                                    inlinedAt.Val, isImplicitCode.Val));
    4246             :   return false;
    4247             : }
    4248             : 
    4249             : /// ParseGenericDINode:
    4250        1569 : ///   ::= !GenericDINode(tag: 15, header: "...", operands: {...})
    4251             : bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
    4252        6088 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4253           0 :   REQUIRED(tag, DwarfTagField, );                                              \
    4254             :   OPTIONAL(header, MDStringField, );                                           \
    4255        6088 :   OPTIONAL(operands, MDFieldList, );
    4256             :   PARSE_MD_FIELDS();
    4257             : #undef VISIT_MD_FIELDS
    4258             : 
    4259             :   Result = GET_OR_DISTINCT(GenericDINode,
    4260             :                            (Context, tag.Val, header.Val, operands.Val));
    4261        2157 :   return false;
    4262             : }
    4263       11652 : 
    4264           0 : /// ParseDISubrange:
    4265             : ///   ::= !DISubrange(count: 30, lowerBound: 2)
    4266       11652 : ///   ::= !DISubrange(count: !node, lowerBound: 2)
    4267             : bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
    4268             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4269             :   REQUIRED(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false));              \
    4270             :   OPTIONAL(lowerBound, MDSignedField, );
    4271             :   PARSE_MD_FIELDS();
    4272        1019 : #undef VISIT_MD_FIELDS
    4273             : 
    4274        6663 :   if (count.isMDSignedField())
    4275           0 :     Result = GET_OR_DISTINCT(
    4276             :         DISubrange, (Context, count.getMDSignedValue(), lowerBound.Val));
    4277        6663 :   else if (count.isMDField())
    4278             :     Result = GET_OR_DISTINCT(
    4279             :         DISubrange, (Context, count.getMDFieldValue(), lowerBound.Val));
    4280             :   else
    4281             :     return true;
    4282             : 
    4283        2022 :   return false;
    4284             : }
    4285        2121 : 
    4286           0 : /// ParseDIEnumerator:
    4287             : ///   ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
    4288        2121 : bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
    4289             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4290             :   REQUIRED(name, MDStringField, );                                             \
    4291             :   REQUIRED(value, MDSignedOrUnsignedField, );                                  \
    4292             :   OPTIONAL(isUnsigned, MDBoolField, (false));
    4293             :   PARSE_MD_FIELDS();
    4294        2695 : #undef VISIT_MD_FIELDS
    4295             : 
    4296        5768 :   if (isUnsigned.Val && value.isMDSignedField())
    4297           0 :     return TokError("unsigned enumerator with negative value");
    4298             : 
    4299        5768 :   int64_t Value = value.isMDSignedField()
    4300             :                       ? value.getMDSignedValue()
    4301             :                       : static_cast<int64_t>(value.getMDUnsignedValue());
    4302             :   Result =
    4303             :       GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
    4304             : 
    4305        2052 :   return false;
    4306             : }
    4307       15727 : 
    4308           0 : /// ParseDIBasicType:
    4309             : ///   ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
    4310       15727 : ///                    encoding: DW_ATE_encoding, flags: 0)
    4311             : bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
    4312             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4313             :   OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type));                     \
    4314             :   OPTIONAL(name, MDStringField, );                                             \
    4315             :   OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
    4316        3062 :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
    4317             :   OPTIONAL(encoding, DwarfAttEncodingField, );                                 \
    4318       35044 :   OPTIONAL(flags, DIFlagField, );
    4319           0 :   PARSE_MD_FIELDS();
    4320             : #undef VISIT_MD_FIELDS
    4321       35044 : 
    4322             :   Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
    4323             :                                          align.Val, encoding.Val, flags.Val));
    4324             :   return false;
    4325             : }
    4326             : 
    4327        1169 : /// ParseDIDerivedType:
    4328             : ///   ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
    4329        4574 : ///                      line: 7, scope: !1, baseType: !2, size: 32,
    4330           0 : ///                      align: 32, offset: 0, flags: 0, extraData: !3,
    4331             : ///                      dwarfAddressSpace: 3)
    4332        4574 : bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
    4333             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4334             :   REQUIRED(tag, DwarfTagField, );                                              \
    4335             :   OPTIONAL(name, MDStringField, );                                             \
    4336             :   OPTIONAL(file, MDField, );                                                   \
    4337             :   OPTIONAL(line, LineField, );                                                 \
    4338         264 :   OPTIONAL(scope, MDField, );                                                  \
    4339             :   REQUIRED(baseType, MDField, );                                               \
    4340        1286 :   OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
    4341           0 :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
    4342             :   OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX));                          \
    4343        1286 :   OPTIONAL(flags, DIFlagField, );                                              \
    4344             :   OPTIONAL(extraData, MDField, );                                              \
    4345             :   OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX));
    4346             :   PARSE_MD_FIELDS();
    4347             : #undef VISIT_MD_FIELDS
    4348             : 
    4349         103 :   Optional<unsigned> DWARFAddressSpace;
    4350             :   if (dwarfAddressSpace.Val != UINT32_MAX)
    4351         195 :     DWARFAddressSpace = dwarfAddressSpace.Val;
    4352           0 : 
    4353             :   Result = GET_OR_DISTINCT(DIDerivedType,
    4354         195 :                            (Context, tag.Val, name.Val, file.Val, line.Val,
    4355             :                             scope.Val, baseType.Val, size.Val, align.Val,
    4356             :                             offset.Val, DWARFAddressSpace, flags.Val,
    4357             :                             extraData.Val));
    4358             :   return false;
    4359             : }
    4360          21 : 
    4361             : bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
    4362          77 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4363           0 :   REQUIRED(tag, DwarfTagField, );                                              \
    4364             :   OPTIONAL(name, MDStringField, );                                             \
    4365          77 :   OPTIONAL(file, MDField, );                                                   \
    4366             :   OPTIONAL(line, LineField, );                                                 \
    4367             :   OPTIONAL(scope, MDField, );                                                  \
    4368             :   OPTIONAL(baseType, MDField, );                                               \
    4369             :   OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
    4370             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
    4371          27 :   OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX));                          \
    4372             :   OPTIONAL(flags, DIFlagField, );                                              \
    4373          70 :   OPTIONAL(elements, MDField, );                                               \
    4374           0 :   OPTIONAL(runtimeLang, DwarfLangField, );                                     \
    4375             :   OPTIONAL(vtableHolder, MDField, );                                           \
    4376          70 :   OPTIONAL(templateParams, MDField, );                                         \
    4377             :   OPTIONAL(identifier, MDStringField, );                                       \
    4378             :   OPTIONAL(discriminator, MDField, );
    4379             :   PARSE_MD_FIELDS();
    4380             : #undef VISIT_MD_FIELDS
    4381             : 
    4382          21 :   // If this has an identifier try to build an ODR type.
    4383             :   if (identifier.Val)
    4384          75 :     if (auto *CT = DICompositeType::buildODRType(
    4385           0 :             Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
    4386             :             scope.Val, baseType.Val, size.Val, align.Val, offset.Val, flags.Val,
    4387          75 :             elements.Val, runtimeLang.Val, vtableHolder.Val,
    4388             :             templateParams.Val, discriminator.Val)) {
    4389             :       Result = CT;
    4390             :       return false;
    4391             :     }
    4392             : 
    4393          68 :   // Create a new node, and save it in the context if it belongs in the type
    4394             :   // map.
    4395         111 :   Result = GET_OR_DISTINCT(
    4396           0 :       DICompositeType,
    4397             :       (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
    4398         111 :        size.Val, align.Val, offset.Val, flags.Val, elements.Val,
    4399             :        runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
    4400             :        discriminator.Val));
    4401             :   return false;
    4402             : }
    4403             : 
    4404          57 : bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
    4405             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4406         164 :   OPTIONAL(flags, DIFlagField, );                                              \
    4407           0 :   OPTIONAL(cc, DwarfCCField, );                                                \
    4408             :   REQUIRED(types, MDField, );
    4409         164 :   PARSE_MD_FIELDS();
    4410             : #undef VISIT_MD_FIELDS
    4411             : 
    4412             :   Result = GET_OR_DISTINCT(DISubroutineType,
    4413             :                            (Context, flags.Val, cc.Val, types.Val));
    4414             :   return false;
    4415         997 : }
    4416             : 
    4417        7130 : /// ParseDIFileType:
    4418           0 : ///   ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
    4419             : ///                   checksumkind: CSK_MD5,
    4420        7130 : ///                   checksum: "000102030405060708090a0b0c0d0e0f",
    4421             : ///                   source: "source file contents")
    4422             : bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
    4423             :   // The default constructed value for checksumkind is required, but will never
    4424             :   // be used, as the parser checks if the field was actually Seen before using
    4425             :   // the Val.
    4426        2529 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4427             :   REQUIRED(filename, MDStringField, );                                         \
    4428       13532 :   REQUIRED(directory, MDStringField, );                                        \
    4429           0 :   OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5));                \
    4430             :   OPTIONAL(checksum, MDStringField, );                                         \
    4431       13532 :   OPTIONAL(source, MDStringField, );
    4432             :   PARSE_MD_FIELDS();
    4433             : #undef VISIT_MD_FIELDS
    4434             : 
    4435             :   Optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
    4436             :   if (checksumkind.Seen && checksum.Seen)
    4437          23 :     OptChecksum.emplace(checksumkind.Val, checksum.Val);
    4438             :   else if (checksumkind.Seen || checksum.Seen)
    4439          92 :     return Lex.Error("'checksumkind' and 'checksum' must be provided together");
    4440           0 : 
    4441             :   Optional<MDString *> OptSource;
    4442          92 :   if (source.Seen)
    4443             :     OptSource = source.Val;
    4444             :   Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val,
    4445             :                                     OptChecksum, OptSource));
    4446             :   return false;
    4447             : }
    4448         978 : 
    4449             : /// ParseDICompileUnit:
    4450        1956 : ///   ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
    4451           0 : ///                      isOptimized: true, flags: "-O2", runtimeVersion: 1,
    4452             : ///                      splitDebugFilename: "abc.debug",
    4453        1956 : ///                      emissionKind: FullDebug, enums: !1, retainedTypes: !2,
    4454             : ///                      globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd)
    4455             : bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
    4456             :   if (!IsDistinct)
    4457             :     return Lex.Error("missing 'distinct', required for !DICompileUnit");
    4458             : 
    4459          14 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4460             :   REQUIRED(language, DwarfLangField, );                                        \
    4461          75 :   REQUIRED(file, MDField, (/* AllowNull */ false));                            \
    4462           0 :   OPTIONAL(producer, MDStringField, );                                         \
    4463             :   OPTIONAL(isOptimized, MDBoolField, );                                        \
    4464          75 :   OPTIONAL(flags, MDStringField, );                                            \
    4465             :   OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX));                  \
    4466             :   OPTIONAL(splitDebugFilename, MDStringField, );                               \
    4467             :   OPTIONAL(emissionKind, EmissionKindField, );                                 \
    4468             :   OPTIONAL(enums, MDField, );                                                  \
    4469             :   OPTIONAL(retainedTypes, MDField, );                                          \
    4470             :   OPTIONAL(globals, MDField, );                                                \
    4471             :   OPTIONAL(imports, MDField, );                                                \
    4472       36954 :   OPTIONAL(macros, MDField, );                                                 \
    4473             :   OPTIONAL(dwoId, MDUnsignedField, );                                          \
    4474       36954 :   OPTIONAL(splitDebugInlining, MDBoolField, = true);                           \
    4475             :   OPTIONAL(debugInfoForProfiling, MDBoolField, = false);                       \
    4476       36954 :   OPTIONAL(nameTableKind, NameTableKindField, );
    4477             :   PARSE_MD_FIELDS();
    4478       36954 : #undef VISIT_MD_FIELDS
    4479       36926 : 
    4480             :   Result = DICompileUnit::getDistinct(
    4481             :       Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
    4482       36931 :       runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
    4483       36931 :       retainedTypes.Val, globals.Val, imports.Val, macros.Val, dwoId.Val,
    4484             :       splitDebugInlining.Val, debugInfoForProfiling.Val, nameTableKind.Val);
    4485         263 :   return false;
    4486             : }
    4487         263 : 
    4488             : /// ParseDISubprogram:
    4489         263 : ///   ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
    4490             : ///                     file: !1, line: 7, type: !2, isLocal: false,
    4491         263 : ///                     isDefinition: true, scopeLine: 8, containingType: !3,
    4492         263 : ///                     virtuality: DW_VIRTUALTIY_pure_virtual,
    4493             : ///                     virtualIndex: 10, thisAdjustment: 4, flags: 11,
    4494             : ///                     isOptimized: false, templateParams: !4, declaration: !5,
    4495         262 : ///                     retainedNodes: !6, thrownTypes: !7)
    4496         262 : bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
    4497             :   auto Loc = Lex.getLoc();
    4498        9420 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4499             :   OPTIONAL(scope, MDField, );                                                  \
    4500        9420 :   OPTIONAL(name, MDStringField, );                                             \
    4501             :   OPTIONAL(linkageName, MDStringField, );                                      \
    4502        9420 :   OPTIONAL(file, MDField, );                                                   \
    4503             :   OPTIONAL(line, LineField, );                                                 \
    4504        9420 :   OPTIONAL(type, MDField, );                                                   \
    4505        9419 :   OPTIONAL(isLocal, MDBoolField, );                                            \
    4506             :   OPTIONAL(isDefinition, MDBoolField, (true));                                 \
    4507             :   OPTIONAL(scopeLine, LineField, );                                            \
    4508        9415 :   OPTIONAL(containingType, MDField, );                                         \
    4509        9415 :   OPTIONAL(virtuality, DwarfVirtualityField, );                                \
    4510             :   OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX));                    \
    4511          39 :   OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX));          \
    4512             :   OPTIONAL(flags, DIFlagField, );                                              \
    4513          39 :   OPTIONAL(isOptimized, MDBoolField, );                                        \
    4514             :   OPTIONAL(unit, MDField, );                                                   \
    4515          39 :   OPTIONAL(templateParams, MDField, );                                         \
    4516             :   OPTIONAL(declaration, MDField, );                                            \
    4517          39 :   OPTIONAL(retainedNodes, MDField, );                                              \
    4518          39 :   OPTIONAL(thrownTypes, MDField, );
    4519             :   PARSE_MD_FIELDS();
    4520             : #undef VISIT_MD_FIELDS
    4521          36 : 
    4522          36 :   if (isDefinition.Val && !IsDistinct)
    4523             :     return Lex.Error(
    4524         196 :         Loc,
    4525             :         "missing 'distinct', required for !DISubprogram when 'isDefinition'");
    4526         196 : 
    4527             :   Result = GET_OR_DISTINCT(
    4528         196 :       DISubprogram,
    4529             :       (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
    4530         196 :        type.Val, isLocal.Val, isDefinition.Val, scopeLine.Val,
    4531         196 :        containingType.Val, virtuality.Val, virtualIndex.Val, thisAdjustment.Val,
    4532             :        flags.Val, isOptimized.Val, unit.Val, templateParams.Val,
    4533             :        declaration.Val, retainedNodes.Val, thrownTypes.Val));
    4534         191 :   return false;
    4535         191 : }
    4536             : 
    4537        6162 : /// ParseDILexicalBlock:
    4538             : ///   ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
    4539        6162 : bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
    4540             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4541        6162 :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4542             :   OPTIONAL(file, MDField, );                                                   \
    4543        6162 :   OPTIONAL(line, LineField, );                                                 \
    4544        6162 :   OPTIONAL(column, ColumnField, );
    4545             :   PARSE_MD_FIELDS();
    4546             : #undef VISIT_MD_FIELDS
    4547        6162 : 
    4548        6162 :   Result = GET_OR_DISTINCT(
    4549             :       DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
    4550        1576 :   return false;
    4551             : }
    4552        1576 : 
    4553             : /// ParseDILexicalBlockFile:
    4554        1576 : ///   ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
    4555             : bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
    4556        1576 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4557        1569 :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4558             :   OPTIONAL(file, MDField, );                                                   \
    4559             :   REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
    4560        1576 :   PARSE_MD_FIELDS();
    4561        1576 : #undef VISIT_MD_FIELDS
    4562             : 
    4563        2157 :   Result = GET_OR_DISTINCT(DILexicalBlockFile,
    4564             :                            (Context, scope.Val, file.Val, discriminator.Val));
    4565        2157 :   return false;
    4566             : }
    4567        2157 : 
    4568             : /// ParseDINamespace:
    4569        2157 : ///   ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
    4570        2157 : bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
    4571             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4572             :   REQUIRED(scope, MDField, );                                                  \
    4573        2157 :   OPTIONAL(name, MDStringField, );                                             \
    4574        2157 :   OPTIONAL(exportSymbols, MDBoolField, );
    4575             :   PARSE_MD_FIELDS();
    4576        1019 : #undef VISIT_MD_FIELDS
    4577             : 
    4578        1019 :   Result = GET_OR_DISTINCT(DINamespace,
    4579             :                            (Context, scope.Val, name.Val, exportSymbols.Val));
    4580        1019 :   return false;
    4581             : }
    4582        1019 : 
    4583        1019 : /// ParseDIMacro:
    4584             : ///   ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: "SomeValue")
    4585             : bool LLParser::ParseDIMacro(MDNode *&Result, bool IsDistinct) {
    4586        1019 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4587        1019 :   REQUIRED(type, DwarfMacinfoTypeField, );                                     \
    4588             :   OPTIONAL(line, LineField, );                                                 \
    4589        2022 :   REQUIRED(name, MDStringField, );                                             \
    4590             :   OPTIONAL(value, MDStringField, );
    4591        2022 :   PARSE_MD_FIELDS();
    4592             : #undef VISIT_MD_FIELDS
    4593        2022 : 
    4594             :   Result = GET_OR_DISTINCT(DIMacro,
    4595        2022 :                            (Context, type.Val, line.Val, name.Val, value.Val));
    4596        2022 :   return false;
    4597             : }
    4598             : 
    4599        2022 : /// ParseDIMacroFile:
    4600        2022 : ///   ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
    4601             : bool LLParser::ParseDIMacroFile(MDNode *&Result, bool IsDistinct) {
    4602        2695 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4603             :   OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file));       \
    4604        2695 :   OPTIONAL(line, LineField, );                                                 \
    4605             :   REQUIRED(file, MDField, );                                                   \
    4606        2695 :   OPTIONAL(nodes, MDField, );
    4607             :   PARSE_MD_FIELDS();
    4608        2695 : #undef VISIT_MD_FIELDS
    4609        2695 : 
    4610             :   Result = GET_OR_DISTINCT(DIMacroFile,
    4611             :                            (Context, type.Val, line.Val, file.Val, nodes.Val));
    4612        2695 :   return false;
    4613        2695 : }
    4614             : 
    4615        2052 : /// ParseDIModule:
    4616             : ///   ::= !DIModule(scope: !0, name: "SomeModule", configMacros: "-DNDEBUG",
    4617        2052 : ///                 includePath: "/usr/include", isysroot: "/")
    4618             : bool LLParser::ParseDIModule(MDNode *&Result, bool IsDistinct) {
    4619        2052 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4620             :   REQUIRED(scope, MDField, );                                                  \
    4621        2052 :   REQUIRED(name, MDStringField, );                                             \
    4622        2052 :   OPTIONAL(configMacros, MDStringField, );                                     \
    4623             :   OPTIONAL(includePath, MDStringField, );                                      \
    4624             :   OPTIONAL(isysroot, MDStringField, );
    4625        2048 :   PARSE_MD_FIELDS();
    4626        2048 : #undef VISIT_MD_FIELDS
    4627             : 
    4628        3073 :   Result = GET_OR_DISTINCT(DIModule, (Context, scope.Val, name.Val,
    4629             :                            configMacros.Val, includePath.Val, isysroot.Val));
    4630        3073 :   return false;
    4631             : }
    4632        3073 : 
    4633             : /// ParseDITemplateTypeParameter:
    4634        3073 : ///   ::= !DITemplateTypeParameter(name: "Ty", type: !1)
    4635        3062 : bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
    4636             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4637             :   OPTIONAL(name, MDStringField, );                                             \
    4638        3073 :   REQUIRED(type, MDField, );
    4639        3073 :   PARSE_MD_FIELDS();
    4640             : #undef VISIT_MD_FIELDS
    4641        1169 : 
    4642             :   Result =
    4643        1169 :       GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
    4644             :   return false;
    4645        1169 : }
    4646             : 
    4647        1169 : /// ParseDITemplateValueParameter:
    4648        1169 : ///   ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
    4649             : ///                                 name: "V", type: !1, value: i32 7)
    4650             : bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
    4651        1168 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4652        1168 :   OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter));      \
    4653             :   OPTIONAL(name, MDStringField, );                                             \
    4654         264 :   OPTIONAL(type, MDField, );                                                   \
    4655             :   REQUIRED(value, MDField, );
    4656         264 :   PARSE_MD_FIELDS();
    4657             : #undef VISIT_MD_FIELDS
    4658         264 : 
    4659             :   Result = GET_OR_DISTINCT(DITemplateValueParameter,
    4660         264 :                            (Context, tag.Val, name.Val, type.Val, value.Val));
    4661         264 :   return false;
    4662             : }
    4663             : 
    4664         264 : /// ParseDIGlobalVariable:
    4665         264 : ///   ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
    4666             : ///                         file: !1, line: 7, type: !2, isLocal: false,
    4667         103 : ///                         isDefinition: true, templateParams: !3,
    4668             : ///                         declaration: !4, align: 8)
    4669         103 : bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
    4670             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4671         103 :   REQUIRED(name, MDStringField, (/* AllowEmpty */ false));                     \
    4672             :   OPTIONAL(scope, MDField, );                                                  \
    4673         103 :   OPTIONAL(linkageName, MDStringField, );                                      \
    4674         103 :   OPTIONAL(file, MDField, );                                                   \
    4675             :   OPTIONAL(line, LineField, );                                                 \
    4676             :   OPTIONAL(type, MDField, );                                                   \
    4677         103 :   OPTIONAL(isLocal, MDBoolField, );                                            \
    4678         103 :   OPTIONAL(isDefinition, MDBoolField, (true));                                 \
    4679             :   OPTIONAL(templateParams, MDField, );                                         \
    4680          21 :   OPTIONAL(declaration, MDField, );                                            \
    4681             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
    4682          21 :   PARSE_MD_FIELDS();
    4683             : #undef VISIT_MD_FIELDS
    4684          21 : 
    4685             :   Result =
    4686          21 :       GET_OR_DISTINCT(DIGlobalVariable,
    4687          21 :                       (Context, scope.Val, name.Val, linkageName.Val, file.Val,
    4688             :                        line.Val, type.Val, isLocal.Val, isDefinition.Val,
    4689             :                        declaration.Val, templateParams.Val, align.Val));
    4690          21 :   return false;
    4691          21 : }
    4692             : 
    4693          27 : /// ParseDILocalVariable:
    4694             : ///   ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
    4695          27 : ///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
    4696             : ///                        align: 8)
    4697          27 : ///   ::= !DILocalVariable(scope: !0, name: "foo",
    4698             : ///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
    4699          27 : ///                        align: 8)
    4700          27 : bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
    4701             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4702             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4703          27 :   OPTIONAL(name, MDStringField, );                                             \
    4704          27 :   OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX));                             \
    4705             :   OPTIONAL(file, MDField, );                                                   \
    4706          21 :   OPTIONAL(line, LineField, );                                                 \
    4707             :   OPTIONAL(type, MDField, );                                                   \
    4708          21 :   OPTIONAL(flags, DIFlagField, );                                              \
    4709             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
    4710          21 :   PARSE_MD_FIELDS();
    4711             : #undef VISIT_MD_FIELDS
    4712          21 : 
    4713          21 :   Result = GET_OR_DISTINCT(DILocalVariable,
    4714             :                            (Context, scope.Val, name.Val, file.Val, line.Val,
    4715             :                             type.Val, arg.Val, flags.Val, align.Val));
    4716          21 :   return false;
    4717          21 : }
    4718             : 
    4719          68 : /// ParseDILabel:
    4720             : ///   ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7)
    4721          68 : bool LLParser::ParseDILabel(MDNode *&Result, bool IsDistinct) {
    4722             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4723          68 :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4724             :   REQUIRED(name, MDStringField, );                                             \
    4725          68 :   REQUIRED(file, MDField, );                                                   \
    4726          68 :   REQUIRED(line, LineField, );
    4727             :   PARSE_MD_FIELDS();
    4728             : #undef VISIT_MD_FIELDS
    4729          68 : 
    4730          68 :   Result = GET_OR_DISTINCT(DILabel,
    4731             :                            (Context, scope.Val, name.Val, file.Val, line.Val));
    4732          57 :   return false;
    4733             : }
    4734          57 : 
    4735             : /// ParseDIExpression:
    4736          57 : ///   ::= !DIExpression(0, 7, -1)
    4737             : bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
    4738          57 :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
    4739          57 :   Lex.Lex();
    4740             : 
    4741             :   if (ParseToken(lltok::lparen, "expected '(' here"))
    4742          57 :     return true;
    4743          57 : 
    4744             :   SmallVector<uint64_t, 8> Elements;
    4745         998 :   if (Lex.getKind() != lltok::rparen)
    4746             :     do {
    4747         998 :       if (Lex.getKind() == lltok::DwarfOp) {
    4748             :         if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
    4749         998 :           Lex.Lex();
    4750             :           Elements.push_back(Op);
    4751         998 :           continue;
    4752         997 :         }
    4753             :         return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
    4754             :       }
    4755         997 : 
    4756         997 :       if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    4757             :         return TokError("expected unsigned integer");
    4758        2530 : 
    4759             :       auto &U = Lex.getAPSIntVal();
    4760        2530 :       if (U.ugt(UINT64_MAX))
    4761             :         return TokError("element too large, limit is " + Twine(UINT64_MAX));
    4762        2530 :       Elements.push_back(U.getZExtValue());
    4763             :       Lex.Lex();
    4764        2530 :     } while (EatIfPresent(lltok::comma));
    4765        2529 : 
    4766             :   if (ParseToken(lltok::rparen, "expected ')' here"))
    4767             :     return true;
    4768        2527 : 
    4769        2527 :   Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
    4770             :   return false;
    4771          23 : }
    4772             : 
    4773          23 : /// ParseDIGlobalVariableExpression:
    4774             : ///   ::= !DIGlobalVariableExpression(var: !0, expr: !1)
    4775          23 : bool LLParser::ParseDIGlobalVariableExpression(MDNode *&Result,
    4776             :                                                bool IsDistinct) {
    4777          23 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4778          23 :   REQUIRED(var, MDField, );                                                    \
    4779             :   REQUIRED(expr, MDField, );
    4780             :   PARSE_MD_FIELDS();
    4781          23 : #undef VISIT_MD_FIELDS
    4782          23 : 
    4783             :   Result =
    4784         978 :       GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
    4785             :   return false;
    4786         978 : }
    4787             : 
    4788         978 : /// ParseDIObjCProperty:
    4789             : ///   ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
    4790         978 : ///                       getter: "getFoo", attributes: 7, type: !2)
    4791         978 : bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
    4792             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4793             :   OPTIONAL(name, MDStringField, );                                             \
    4794         978 :   OPTIONAL(file, MDField, );                                                   \
    4795         978 :   OPTIONAL(line, LineField, );                                                 \
    4796             :   OPTIONAL(setter, MDStringField, );                                           \
    4797          21 :   OPTIONAL(getter, MDStringField, );                                           \
    4798             :   OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX));                      \
    4799          21 :   OPTIONAL(type, MDField, );
    4800             :   PARSE_MD_FIELDS();
    4801          21 : #undef VISIT_MD_FIELDS
    4802             : 
    4803          21 :   Result = GET_OR_DISTINCT(DIObjCProperty,
    4804          14 :                            (Context, name.Val, file.Val, line.Val, setter.Val,
    4805             :                             getter.Val, attributes.Val, type.Val));
    4806             :   return false;
    4807          21 : }
    4808          21 : 
    4809             : /// ParseDIImportedEntity:
    4810             : ///   ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
    4811             : ///                         line: 7, name: "foo")
    4812      152328 : bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
    4813      152328 : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4814           1 :   REQUIRED(tag, DwarfTagField, );                                              \
    4815             :   REQUIRED(scope, MDField, );                                                  \
    4816             :   OPTIONAL(entity, MDField, );                                                 \
    4817      152327 :   OPTIONAL(file, MDField, );                                                   \
    4818      152327 :   OPTIONAL(line, LineField, );                                                 \
    4819             :   OPTIONAL(name, MDStringField, );
    4820        6161 :   PARSE_MD_FIELDS();
    4821        6161 : #undef VISIT_MD_FIELDS
    4822           0 : 
    4823             :   Result = GET_OR_DISTINCT(
    4824             :       DIImportedEntity,
    4825        6161 :       (Context, tag.Val, scope.Val, entity.Val, file.Val, line.Val, name.Val));
    4826        6161 :   return false;
    4827             : }
    4828       20817 : 
    4829       20817 : #undef PARSE_MD_FIELD
    4830           1 : #undef NOP_FIELD
    4831             : #undef REQUIRE_FIELD
    4832             : #undef DECLARE_FIELD
    4833       20816 : 
    4834       20816 : /// ParseMetadataAsValue
    4835             : ///  ::= metadata i32 %local
    4836        8287 : ///  ::= metadata i32 @global
    4837        8287 : ///  ::= metadata i32 7
    4838           0 : ///  ::= metadata !0
    4839             : ///  ::= metadata !{...}
    4840             : ///  ::= metadata !"string"
    4841        8287 : bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
    4842        8287 :   // Note: the type 'metadata' has already been parsed.
    4843             :   Metadata *MD;
    4844       54288 :   if (ParseMetadata(MD, &PFS))
    4845       54288 :     return true;
    4846           0 : 
    4847             :   V = MetadataAsValue::get(Context, MD);
    4848             :   return false;
    4849       54288 : }
    4850       54288 : 
    4851             : /// ParseValueAsMetadata
    4852       12699 : ///  ::= i32 %local
    4853       12699 : ///  ::= i32 @global
    4854           0 : ///  ::= i32 7
    4855             : bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
    4856             :                                     PerFunctionState *PFS) {
    4857       12699 :   Type *Ty;
    4858       12699 :   LocTy Loc;
    4859             :   if (ParseType(Ty, TypeMsg, Loc))
    4860        3944 :     return true;
    4861        3944 :   if (Ty->isMetadataTy())
    4862           0 :     return Error(Loc, "invalid metadata-value-metadata roundtrip");
    4863             : 
    4864             :   Value *V;
    4865        3944 :   if (ParseValue(Ty, V, PFS))
    4866        3944 :     return true;
    4867             : 
    4868       25769 :   MD = ValueAsMetadata::get(V);
    4869       25769 :   return false;
    4870           0 : }
    4871             : 
    4872             : /// ParseMetadata
    4873       25769 : ///  ::= i32 %local
    4874       25769 : ///  ::= i32 @global
    4875             : ///  ::= i32 7
    4876          12 : ///  ::= !42
    4877          12 : ///  ::= !{...}
    4878           0 : ///  ::= !"string"
    4879             : ///  ::= !DILocation(...)
    4880             : bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
    4881          12 :   if (Lex.getKind() == lltok::MetadataVar) {
    4882          12 :     MDNode *N;
    4883             :     if (ParseSpecializedMDNode(N))
    4884         195 :       return true;
    4885         195 :     MD = N;
    4886           0 :     return false;
    4887             :   }
    4888             : 
    4889         195 :   // ValueAsMetadata:
    4890         195 :   // <type> <value>
    4891             :   if (Lex.getKind() != lltok::exclaim)
    4892          53 :     return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
    4893          53 : 
    4894           0 :   // '!'.
    4895             :   assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
    4896             :   Lex.Lex();
    4897          53 : 
    4898          53 :   // MDString:
    4899             :   //   ::= '!' STRINGCONSTANT
    4900          23 :   if (Lex.getKind() == lltok::StringConstant) {
    4901          23 :     MDString *S;
    4902           0 :     if (ParseMDString(S))
    4903             :       return true;
    4904             :     MD = S;
    4905          23 :     return false;
    4906          23 :   }
    4907             : 
    4908       10982 :   // MDNode:
    4909       10982 :   // !{ ... }
    4910           0 :   // !7
    4911             :   MDNode *N;
    4912             :   if (ParseMDNodeTail(N))
    4913       10982 :     return true;
    4914       10982 :   MD = N;
    4915             :   return false;
    4916        1555 : }
    4917        1555 : 
    4918           0 : //===----------------------------------------------------------------------===//
    4919             : // Function Parsing.
    4920             : //===----------------------------------------------------------------------===//
    4921        1555 : 
    4922        1555 : bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
    4923             :                                    PerFunctionState *PFS, bool IsCall) {
    4924        3109 :   if (Ty->isFunctionTy())
    4925        3109 :     return Error(ID.Loc, "functions are not values, refer to them as pointers");
    4926           0 : 
    4927             :   switch (ID.Kind) {
    4928             :   case ValID::t_LocalID:
    4929        3109 :     if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
    4930        3109 :     V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc, IsCall);
    4931             :     return V == nullptr;
    4932        2091 :   case ValID::t_LocalName:
    4933        2091 :     if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
    4934           0 :     V = PFS->GetVal(ID.StrVal, Ty, ID.Loc, IsCall);
    4935             :     return V == nullptr;
    4936             :   case ValID::t_InlineAsm: {
    4937        2091 :     if (!ID.FTy || !InlineAsm::Verify(ID.FTy, ID.StrVal2))
    4938        2091 :       return Error(ID.Loc, "invalid type for inline asm constraint string");
    4939             :     V = InlineAsm::get(ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1,
    4940          79 :                        (ID.UIntVal >> 1) & 1,
    4941          79 :                        (InlineAsm::AsmDialect(ID.UIntVal >> 2)));
    4942           0 :     return false;
    4943             :   }
    4944             :   case ValID::t_GlobalName:
    4945          79 :     V = GetGlobalVal(ID.StrVal, Ty, ID.Loc, IsCall);
    4946          79 :     return V == nullptr;
    4947             :   case ValID::t_GlobalID:
    4948         176 :     V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc, IsCall);
    4949         176 :     return V == nullptr;
    4950           0 :   case ValID::t_APSInt:
    4951             :     if (!Ty->isIntegerTy())
    4952             :       return Error(ID.Loc, "integer constant must have integer type");
    4953         176 :     ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
    4954         176 :     V = ConstantInt::get(Context, ID.APSIntVal);
    4955             :     return false;
    4956        1982 :   case ValID::t_APFloat:
    4957        1982 :     if (!Ty->isFloatingPointTy() ||
    4958           0 :         !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
    4959             :       return Error(ID.Loc, "floating point constant invalid for type");
    4960             : 
    4961        1982 :     // The lexer has no type info, so builds all half, float, and double FP
    4962        1982 :     // constants as double.  Fix this here.  Long double does not need this.
    4963             :     if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
    4964          38 :       bool Ignored;
    4965          38 :       if (Ty->isHalfTy())
    4966           0 :         ID.APFloatVal.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
    4967             :                               &Ignored);
    4968             :       else if (Ty->isFloatTy())
    4969          38 :         ID.APFloatVal.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
    4970          38 :                               &Ignored);
    4971             :     }
    4972          68 :     V = ConstantFP::get(Context, ID.APFloatVal);
    4973          68 : 
    4974           0 :     if (V->getType() != Ty)
    4975             :       return Error(ID.Loc, "floating point constant does not have type '" +
    4976             :                    getTypeString(Ty) + "'");
    4977          68 : 
    4978          68 :     return false;
    4979             :   case ValID::t_Null:
    4980             :     if (!Ty->isPointerTy())
    4981       40125 :       return Error(ID.Loc, "null must be a pointer type");
    4982             :     V = ConstantPointerNull::get(cast<PointerType>(Ty));
    4983             :     return false;
    4984             :   case ValID::t_Undef:
    4985             :     // FIXME: LabelTy should not be a first-class type.
    4986             :     if (!Ty->isFirstClassType() || Ty->isLabelTy())
    4987             :       return Error(ID.Loc, "invalid type for undef constant");
    4988             :     V = UndefValue::get(Ty);
    4989           1 :     return false;
    4990             :   case ValID::t_EmptyArray:
    4991             :     if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
    4992             :       return Error(ID.Loc, "invalid empty array initializer");
    4993             :     V = UndefValue::get(Ty);
    4994             :     return false;
    4995             :   case ValID::t_Zero:
    4996             :     // FIXME: LabelTy should not be a first-class type.
    4997             :     if (!Ty->isFirstClassType() || Ty->isLabelTy())
    4998             :       return Error(ID.Loc, "invalid type for null constant");
    4999             :     V = Constant::getNullValue(Ty);
    5000             :     return false;
    5001             :   case ValID::t_None:
    5002             :     if (!Ty->isTokenTy())
    5003             :       return Error(ID.Loc, "invalid type for none constant");
    5004             :     V = Constant::getNullValue(Ty);
    5005             :     return false;
    5006             :   case ValID::t_Constant:
    5007             :     if (ID.ConstantVal->getType() != Ty)
    5008             :       return Error(ID.Loc, "constant expression type mismatch");
    5009             : 
    5010             :     V = ID.ConstantVal;
    5011             :     return false;
    5012             :   case ValID::t_ConstantStruct:
    5013             :   case ValID::t_PackedConstantStruct:
    5014             :     if (StructType *ST = dyn_cast<StructType>(Ty)) {
    5015             :       if (ST->getNumElements() != ID.UIntVal)
    5016             :         return Error(ID.Loc,
    5017        9420 :                      "initializer with struct type has wrong # elements");
    5018             :       if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
    5019             :         return Error(ID.Loc, "packed'ness of initializer and type don't match");
    5020             : 
    5021             :       // Verify that the elements are compatible with the structtype.
    5022             :       for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
    5023             :         if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
    5024        9422 :           return Error(ID.Loc, "element " + Twine(i) +
    5025             :                     " of struct initializer doesn't match struct element type");
    5026             : 
    5027        9413 :       V = ConstantStruct::get(
    5028        9413 :           ST, makeArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
    5029             :     } else
    5030        9413 :       return Error(ID.Loc, "constant expression type mismatch");
    5031             :     return false;
    5032             :   }
    5033             :   llvm_unreachable("Invalid ValID");
    5034             : }
    5035          39 : 
    5036             : bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
    5037             :   C = nullptr;
    5038             :   ValID ID;
    5039             :   auto Loc = Lex.getLoc();
    5040          40 :   if (ParseValID(ID, /*PFS=*/nullptr))
    5041             :     return true;
    5042             :   switch (ID.Kind) {
    5043          35 :   case ValID::t_APSInt:
    5044             :   case ValID::t_APFloat:
    5045          35 :   case ValID::t_Undef:
    5046             :   case ValID::t_Constant:
    5047             :   case ValID::t_ConstantStruct:
    5048             :   case ValID::t_PackedConstantStruct: {
    5049             :     Value *V;
    5050             :     if (ConvertValIDToValue(Ty, ID, V, /*PFS=*/nullptr, /*IsCall=*/false))
    5051         196 :       return true;
    5052             :     assert(isa<Constant>(V) && "Expected a constant value");
    5053             :     C = cast<Constant>(V);
    5054             :     return false;
    5055         197 :   }
    5056             :   case ValID::t_Null:
    5057             :     C = Constant::getNullValue(Ty);
    5058         190 :     return false;
    5059         169 :   default:
    5060             :     return Error(Loc, "expected a constant value");
    5061          21 :   }
    5062          21 : }
    5063             : 
    5064             : bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
    5065             :   V = nullptr;
    5066             :   ValID ID;
    5067             :   return ParseValID(ID, PFS) ||
    5068             :          ConvertValIDToValue(Ty, ID, V, PFS, /*IsCall=*/false);
    5069             : }
    5070             : 
    5071             : bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
    5072        6162 :   Type *Ty = nullptr;
    5073             :   return ParseType(Ty) ||
    5074             :          ParseValue(Ty, V, PFS);
    5075             : }
    5076             : 
    5077        6164 : bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
    5078             :                                       PerFunctionState &PFS) {
    5079             :   Value *V;
    5080        6160 :   Loc = Lex.getLoc();
    5081           0 :   if (ParseTypeAndValue(V, PFS)) return true;
    5082             :   if (!isa<BasicBlock>(V))
    5083        6160 :     return Error(Loc, "expected a basic block");
    5084        6160 :   BB = cast<BasicBlock>(V);
    5085        6134 :   return false;
    5086        6160 : }
    5087        6160 : 
    5088             : /// FunctionHeader
    5089        6160 : ///   ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
    5090             : ///       OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
    5091             : ///       '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
    5092             : ///       OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
    5093             : bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
    5094             :   // Parse the linkage.
    5095        1576 :   LocTy LinkageLoc = Lex.getLoc();
    5096             :   unsigned Linkage;
    5097             :   unsigned Visibility;
    5098             :   unsigned DLLStorageClass;
    5099             :   bool DSOLocal;
    5100             :   AttrBuilder RetAttrs;
    5101             :   unsigned CC;
    5102             :   bool HasLinkage;
    5103        1576 :   Type *RetType = nullptr;
    5104             :   LocTy RetTypeLoc = Lex.getLoc();
    5105             :   if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
    5106        1576 :                            DSOLocal) ||
    5107             :       ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
    5108        1576 :       ParseType(RetType, RetTypeLoc, true /*void allowed*/))
    5109             :     return true;
    5110             : 
    5111             :   // Verify that the linkage is ok.
    5112             :   switch ((GlobalValue::LinkageTypes)Linkage) {
    5113             :   case GlobalValue::ExternalLinkage:
    5114             :     break; // always ok.
    5115             :   case GlobalValue::ExternalWeakLinkage:
    5116        2157 :     if (isDefine)
    5117             :       return Error(LinkageLoc, "invalid linkage for function definition");
    5118             :     break;
    5119             :   case GlobalValue::PrivateLinkage:
    5120             :   case GlobalValue::InternalLinkage:
    5121             :   case GlobalValue::AvailableExternallyLinkage:
    5122             :   case GlobalValue::LinkOnceAnyLinkage:
    5123             :   case GlobalValue::LinkOnceODRLinkage:
    5124             :   case GlobalValue::WeakAnyLinkage:
    5125             :   case GlobalValue::WeakODRLinkage:
    5126             :     if (!isDefine)
    5127             :       return Error(LinkageLoc, "invalid linkage for function declaration");
    5128             :     break;
    5129             :   case GlobalValue::AppendingLinkage:
    5130        2159 :   case GlobalValue::CommonLinkage:
    5131             :     return Error(LinkageLoc, "invalid function linkage type");
    5132             :   }
    5133             : 
    5134        2155 :   if (!isValidVisibilityForLinkage(Visibility, Linkage))
    5135          21 :     return Error(LinkageLoc,
    5136             :                  "symbol with local linkage must have default visibility");
    5137        4331 : 
    5138             :   if (!FunctionType::isValidReturnType(RetType))
    5139             :     return Error(RetTypeLoc, "invalid function return type");
    5140             : 
    5141             :   LocTy NameLoc = Lex.getLoc();
    5142             : 
    5143             :   std::string FunctionName;
    5144             :   if (Lex.getKind() == lltok::GlobalVar) {
    5145        1019 :     FunctionName = Lex.getStrVal();
    5146             :   } else if (Lex.getKind() == lltok::GlobalID) {     // @42 is ok.
    5147             :     unsigned NameID = Lex.getUIntVal();
    5148             : 
    5149             :     if (NameID != NumberedVals.size())
    5150             :       return TokError("function expected to be numbered '%" +
    5151             :                       Twine(NumberedVals.size()) + "'");
    5152             :   } else {
    5153             :     return TokError("expected function name");
    5154             :   }
    5155             : 
    5156             :   Lex.Lex();
    5157             : 
    5158             :   if (Lex.getKind() != lltok::lparen)
    5159             :     return TokError("expected '(' in function argument list");
    5160             : 
    5161             :   SmallVector<ArgInfo, 8> ArgList;
    5162             :   bool isVarArg;
    5163        1020 :   AttrBuilder FuncAttrs;
    5164             :   std::vector<unsigned> FwdRefAttrGrps;
    5165             :   LocTy BuiltinLoc;
    5166             :   std::string Section;
    5167        1018 :   unsigned Alignment;
    5168         478 :   std::string GC;
    5169         956 :   GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
    5170         478 :   unsigned AddrSpace = 0;
    5171         478 :   Constant *Prefix = nullptr;
    5172             :   Constant *Prologue = nullptr;
    5173          87 :   Constant *PersonalityFn = nullptr;
    5174          87 :   Comdat *C;
    5175             : 
    5176             :   if (ParseArgumentList(ArgList, isVarArg) ||
    5177             :       ParseOptionalUnnamedAddr(UnnamedAddr) ||
    5178             :       ParseOptionalProgramAddrSpace(AddrSpace) ||
    5179         931 :       ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
    5180             :                                  BuiltinLoc) ||
    5181             :       (EatIfPresent(lltok::kw_section) &&
    5182             :        ParseStringConstant(Section)) ||
    5183             :       parseOptionalComdat(FunctionName, C) ||
    5184             :       ParseOptionalAlignment(Alignment) ||
    5185         931 :       (EatIfPresent(lltok::kw_gc) &&
    5186             :        ParseStringConstant(GC)) ||
    5187             :       (EatIfPresent(lltok::kw_prefix) &&
    5188        2022 :        ParseGlobalTypeAndValue(Prefix)) ||
    5189             :       (EatIfPresent(lltok::kw_prologue) &&
    5190             :        ParseGlobalTypeAndValue(Prologue)) ||
    5191             :       (EatIfPresent(lltok::kw_personality) &&
    5192             :        ParseGlobalTypeAndValue(PersonalityFn)))
    5193        2023 :     return true;
    5194             : 
    5195             :   if (FuncAttrs.contains(Attribute::Builtin))
    5196        2021 :     return Error(BuiltinLoc, "'builtin' attribute not valid on function");
    5197             : 
    5198        2021 :   // If the alignment was parsed as an attribute, move to the alignment field.
    5199             :   if (FuncAttrs.hasAlignmentAttr()) {
    5200             :     Alignment = FuncAttrs.getAlignment();
    5201             :     FuncAttrs.removeAttribute(Attribute::Alignment);
    5202             :   }
    5203             : 
    5204             :   // Okay, if we got here, the function is syntactically valid.  Convert types
    5205             :   // and do semantic checks.
    5206        2695 :   std::vector<Type*> ParamTypeList;
    5207             :   SmallVector<AttributeSet, 8> Attrs;
    5208             : 
    5209             :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    5210             :     ParamTypeList.push_back(ArgList[i].Ty);
    5211             :     Attrs.push_back(ArgList[i].Attrs);
    5212             :   }
    5213             : 
    5214             :   AttributeList PAL =
    5215             :       AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
    5216        2697 :                          AttributeSet::get(Context, RetAttrs), Attrs);
    5217             : 
    5218             :   if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
    5219             :     return Error(RetTypeLoc, "functions with 'sret' argument must return void");
    5220        2693 : 
    5221             :   FunctionType *FT =
    5222        2517 :     FunctionType::get(RetType, ParamTypeList, isVarArg);
    5223           0 :   PointerType *PFT = PointerType::get(FT, AddrSpace);
    5224             : 
    5225             :   Fn = nullptr;
    5226        2693 :   if (!FunctionName.empty()) {
    5227          28 :     // If this was a definition of a forward reference, remove the definition
    5228        5386 :     // from the forward reference table and fill in the forward ref.
    5229             :     auto FRVI = ForwardRefVals.find(FunctionName);
    5230             :     if (FRVI != ForwardRefVals.end()) {
    5231             :       Fn = M->getFunction(FunctionName);
    5232             :       if (!Fn)
    5233             :         return Error(FRVI->second.second, "invalid forward reference to "
    5234             :                      "function as global value!");
    5235             :       if (Fn->getType() != PFT)
    5236             :         return Error(FRVI->second.second, "invalid forward reference to "
    5237             :                      "function '" + FunctionName + "' with wrong type: "
    5238             :                      "expected '" + getTypeString(PFT) + "' but was '" +
    5239        2053 :                      getTypeString(Fn->getType()) + "'");
    5240        2053 :       ForwardRefVals.erase(FRVI);
    5241           1 :     } else if ((Fn = M->getFunction(FunctionName))) {
    5242             :       // Reject redefinitions.
    5243             :       return Error(NameLoc, "invalid redefinition of function '" +
    5244             :                    FunctionName + "'");
    5245             :     } else if (M->getNamedValue(FunctionName)) {
    5246             :       return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
    5247             :     }
    5248             : 
    5249             :   } else {
    5250             :     // If this is a definition of a forward referenced function, make sure the
    5251             :     // types agree.
    5252             :     auto I = ForwardRefValIDs.find(NumberedVals.size());
    5253             :     if (I != ForwardRefValIDs.end()) {
    5254             :       Fn = cast<Function>(I->second.first);
    5255             :       if (Fn->getType() != PFT)
    5256             :         return Error(NameLoc, "type of definition and forward reference of '@" +
    5257             :                      Twine(NumberedVals.size()) + "' disagree: "
    5258             :                      "expected '" + getTypeString(PFT) + "' but was '" +
    5259             :                      getTypeString(Fn->getType()) + "'");
    5260             :       ForwardRefValIDs.erase(I);
    5261        2053 :     }
    5262             :   }
    5263             : 
    5264       16376 :   if (!Fn)
    5265        4094 :     Fn = Function::Create(FT, GlobalValue::ExternalLinkage, AddrSpace,
    5266        4094 :                           FunctionName, M);
    5267             :   else // Move the forward-reference to the correct spot in the module.
    5268        2047 :     M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
    5269        2047 : 
    5270             :   assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
    5271             : 
    5272             :   if (FunctionName.empty())
    5273             :     NumberedVals.push_back(Fn);
    5274             : 
    5275             :   Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
    5276             :   maybeSetDSOLocal(DSOLocal, *Fn);
    5277             :   Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
    5278             :   Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
    5279             :   Fn->setCallingConv(CC);
    5280        3073 :   Fn->setAttributes(PAL);
    5281        3073 :   Fn->setUnnamedAddr(UnnamedAddr);
    5282             :   Fn->setAlignment(Alignment);
    5283             :   Fn->setSection(Section);
    5284             :   Fn->setComdat(C);
    5285             :   Fn->setPersonalityFn(PersonalityFn);
    5286             :   if (!GC.empty()) Fn->setGC(GC);
    5287             :   Fn->setPrefixData(Prefix);
    5288             :   Fn->setPrologueData(Prologue);
    5289             :   ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
    5290             : 
    5291             :   // Add all of the arguments we parsed to the function.
    5292             :   Function::arg_iterator ArgIt = Fn->arg_begin();
    5293             :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
    5294             :     // If the argument has a name, insert it into the argument symbol table.
    5295             :     if (ArgList[i].Name.empty()) continue;
    5296             : 
    5297             :     // Set the name, if it conflicted, it will be auto-renamed.
    5298             :     ArgIt->setName(ArgList[i].Name);
    5299             : 
    5300             :     if (ArgIt->getName() != ArgList[i].Name)
    5301             :       return Error(ArgList[i].Loc, "redefinition of argument '%" +
    5302             :                    ArgList[i].Name + "'");
    5303        3073 :   }
    5304             : 
    5305             :   if (isDefine)
    5306        3073 :     return false;
    5307           2 : 
    5308             :   // Check the declaration has no block address forward references.
    5309             :   ValID ID;
    5310             :   if (FunctionName.empty()) {
    5311        3072 :     ID.Kind = ValID::t_GlobalID;
    5312             :     ID.UIntVal = NumberedVals.size() - 1;
    5313             :   } else {
    5314             :     ID.Kind = ValID::t_GlobalName;
    5315             :     ID.StrVal = FunctionName;
    5316             :   }
    5317             :   auto Blocks = ForwardRefBlockAddresses.find(ID);
    5318        3072 :   if (Blocks != ForwardRefBlockAddresses.end())
    5319             :     return Error(Blocks->first.Loc,
    5320             :                  "cannot take blockaddress inside a declaration");
    5321             :   return false;
    5322             : }
    5323        1169 : 
    5324             : bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
    5325             :   ValID ID;
    5326             :   if (FunctionNumber == -1) {
    5327             :     ID.Kind = ValID::t_GlobalName;
    5328             :     ID.StrVal = F.getName();
    5329        1170 :   } else {
    5330             :     ID.Kind = ValID::t_GlobalID;
    5331             :     ID.UIntVal = FunctionNumber;
    5332        1167 :   }
    5333             : 
    5334        1167 :   auto Blocks = P.ForwardRefBlockAddresses.find(ID);
    5335             :   if (Blocks == P.ForwardRefBlockAddresses.end())
    5336             :     return false;
    5337             : 
    5338             :   for (const auto &I : Blocks->second) {
    5339         263 :     const ValID &BBID = I.first;
    5340             :     GlobalValue *GV = I.second;
    5341             : 
    5342             :     assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
    5343             :            "Expected local id or name");
    5344         265 :     BasicBlock *BB;
    5345             :     if (BBID.Kind == ValID::t_LocalName)
    5346             :       BB = GetBB(BBID.StrVal, BBID.Loc);
    5347         260 :     else
    5348             :       BB = GetBB(BBID.UIntVal, BBID.Loc);
    5349         260 :     if (!BB)
    5350             :       return P.Error(BBID.Loc, "referenced value is not a basic block");
    5351             : 
    5352             :     GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
    5353             :     GV->eraseFromParent();
    5354         103 :   }
    5355             : 
    5356             :   P.ForwardRefBlockAddresses.erase(Blocks);
    5357             :   return false;
    5358             : }
    5359         104 : 
    5360             : /// ParseFunctionBody
    5361             : ///   ::= '{' BasicBlock+ UseListOrderDirective* '}'
    5362         102 : bool LLParser::ParseFunctionBody(Function &Fn) {
    5363             :   if (Lex.getKind() != lltok::lbrace)
    5364         102 :     return TokError("expected '{' in function body");
    5365             :   Lex.Lex();  // eat the {.
    5366             : 
    5367             :   int FunctionNumber = -1;
    5368             :   if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
    5369          21 : 
    5370             :   PerFunctionState PFS(*this, Fn, FunctionNumber);
    5371             : 
    5372             :   // Resolve block addresses and allow basic blocks to be forward-declared
    5373             :   // within this function.
    5374             :   if (PFS.resolveForwardRefBlockAddresses())
    5375          21 :     return true;
    5376             :   SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
    5377             : 
    5378          21 :   // We need at least one basic block.
    5379             :   if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
    5380          21 :     return TokError("function body requires at least one basic block");
    5381             : 
    5382             :   while (Lex.getKind() != lltok::rbrace &&
    5383             :          Lex.getKind() != lltok::kw_uselistorder)
    5384             :     if (ParseBasicBlock(PFS)) return true;
    5385          27 : 
    5386             :   while (Lex.getKind() != lltok::rbrace)
    5387             :     if (ParseUseListOrder(&PFS))
    5388             :       return true;
    5389             : 
    5390             :   // Eat the }.
    5391          27 :   Lex.Lex();
    5392             : 
    5393             :   // Verify function is ok.
    5394          27 :   return PFS.FinishFunction();
    5395             : }
    5396          27 : 
    5397             : /// ParseBasicBlock
    5398             : ///   ::= LabelStr? Instruction*
    5399             : bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
    5400             :   // If this basic block starts out with a name, remember it.
    5401             :   std::string Name;
    5402          21 :   LocTy NameLoc = Lex.getLoc();
    5403             :   if (Lex.getKind() == lltok::LabelStr) {
    5404             :     Name = Lex.getStrVal();
    5405             :     Lex.Lex();
    5406             :   }
    5407             : 
    5408             :   BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
    5409          21 :   if (!BB)
    5410             :     return Error(NameLoc,
    5411             :                  "unable to create block named '" + Name + "'");
    5412          21 : 
    5413             :   std::string NameStr;
    5414          21 : 
    5415             :   // Parse the instructions in this block until we get a terminator.
    5416             :   Instruction *Inst;
    5417             :   do {
    5418             :     // This instruction may have three possibilities for a name: a) none
    5419          68 :     // specified, b) name specified "%foo =", c) number specified: "%4 =".
    5420             :     LocTy NameLoc = Lex.getLoc();
    5421             :     int NameID = -1;
    5422             :     NameStr = "";
    5423          69 : 
    5424             :     if (Lex.getKind() == lltok::LocalVarID) {
    5425             :       NameID = Lex.getUIntVal();
    5426          67 :       Lex.Lex();
    5427          67 :       if (ParseToken(lltok::equal, "expected '=' after instruction id"))
    5428          67 :         return true;
    5429             :     } else if (Lex.getKind() == lltok::LocalVar) {
    5430             :       NameStr = Lex.getStrVal();
    5431             :       Lex.Lex();
    5432             :       if (ParseToken(lltok::equal, "expected '=' after instruction name"))
    5433             :         return true;
    5434          57 :     }
    5435             : 
    5436             :     switch (ParseInstruction(Inst, BB, PFS)) {
    5437             :     default: llvm_unreachable("Unknown ParseInstruction result!");
    5438             :     case InstError: return true;
    5439             :     case InstNormal:
    5440          58 :       BB->getInstList().push_back(Inst);
    5441             : 
    5442             :       // With a normal result, we check to see if the instruction is followed by
    5443          56 :       // a comma and metadata.
    5444             :       if (EatIfPresent(lltok::comma))
    5445          56 :         if (ParseInstructionMetadata(*Inst))
    5446             :           return true;
    5447             :       break;
    5448             :     case InstExtraComma:
    5449             :       BB->getInstList().push_back(Inst);
    5450             : 
    5451             :       // If the instruction parser ate an extra comma at the end of it, it
    5452             :       // *must* be followed by metadata.
    5453         998 :       if (ParseInstructionMetadata(*Inst))
    5454             :         return true;
    5455             :       break;
    5456             :     }
    5457             : 
    5458             :     // Set the name on the instruction.
    5459             :     if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
    5460             :   } while (!Inst->isTerminator());
    5461             : 
    5462             :   return false;
    5463             : }
    5464             : 
    5465             : //===----------------------------------------------------------------------===//
    5466         999 : // Instruction Parsing.
    5467             : //===----------------------------------------------------------------------===//
    5468             : 
    5469         996 : /// ParseInstruction - Parse one of the many different instructions.
    5470         996 : ///
    5471             : int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
    5472             :                                PerFunctionState &PFS) {
    5473             :   lltok::Kind Token = Lex.getKind();
    5474         996 :   if (Token == lltok::Eof)
    5475             :     return TokError("found end of file when expecting more instructions");
    5476             :   LocTy Loc = Lex.getLoc();
    5477             :   unsigned KeywordVal = Lex.getUIntVal();
    5478             :   Lex.Lex();  // Eat the keyword.
    5479             : 
    5480             :   switch (Token) {
    5481             :   default:                    return Error(Loc, "expected instruction opcode");
    5482             :   // Terminator Instructions.
    5483             :   case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
    5484        2530 :   case lltok::kw_ret:         return ParseRet(Inst, BB, PFS);
    5485             :   case lltok::kw_br:          return ParseBr(Inst, PFS);
    5486             :   case lltok::kw_switch:      return ParseSwitch(Inst, PFS);
    5487             :   case lltok::kw_indirectbr:  return ParseIndirectBr(Inst, PFS);
    5488             :   case lltok::kw_invoke:      return ParseInvoke(Inst, PFS);
    5489             :   case lltok::kw_resume:      return ParseResume(Inst, PFS);
    5490             :   case lltok::kw_cleanupret:  return ParseCleanupRet(Inst, PFS);
    5491             :   case lltok::kw_catchret:    return ParseCatchRet(Inst, PFS);
    5492             :   case lltok::kw_catchswitch: return ParseCatchSwitch(Inst, PFS);
    5493             :   case lltok::kw_catchpad:    return ParseCatchPad(Inst, PFS);
    5494        2531 :   case lltok::kw_cleanuppad:  return ParseCleanupPad(Inst, PFS);
    5495             :   // Binary Operators.
    5496             :   case lltok::kw_add:
    5497        2526 :   case lltok::kw_sub:
    5498             :   case lltok::kw_mul:
    5499             :   case lltok::kw_shl: {
    5500        2526 :     bool NUW = EatIfPresent(lltok::kw_nuw);
    5501             :     bool NSW = EatIfPresent(lltok::kw_nsw);
    5502             :     if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
    5503             : 
    5504             :     if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
    5505          23 : 
    5506             :     if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
    5507             :     if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
    5508             :     return false;
    5509             :   }
    5510             :   case lltok::kw_fadd:
    5511          23 :   case lltok::kw_fsub:
    5512             :   case lltok::kw_fmul:
    5513             :   case lltok::kw_fdiv:
    5514          23 :   case lltok::kw_frem: {
    5515             :     FastMathFlags FMF = EatFastMathFlagsIfPresent();
    5516          23 :     int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
    5517             :     if (Res != 0)
    5518             :       return Res;
    5519             :     if (FMF.any())
    5520             :       Inst->setFastMathFlags(FMF);
    5521        3209 :     return 0;
    5522             :   }
    5523        3209 : 
    5524             :   case lltok::kw_sdiv:
    5525        3209 :   case lltok::kw_udiv:
    5526             :   case lltok::kw_lshr:
    5527             :   case lltok::kw_ashr: {
    5528             :     bool Exact = EatIfPresent(lltok::kw_exact);
    5529        3209 : 
    5530             :     if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
    5531         707 :     if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
    5532         417 :     return false;
    5533             :   }
    5534         417 : 
    5535         417 :   case lltok::kw_urem:
    5536             :   case lltok::kw_srem:   return ParseArithmetic(Inst, PFS, KeywordVal, 1);
    5537           0 :   case lltok::kw_and:
    5538             :   case lltok::kw_or:
    5539             :   case lltok::kw_xor:    return ParseLogical(Inst, PFS, KeywordVal);
    5540         290 :   case lltok::kw_icmp:   return ParseCompare(Inst, PFS, KeywordVal);
    5541           0 :   case lltok::kw_fcmp: {
    5542             :     FastMathFlags FMF = EatFastMathFlagsIfPresent();
    5543             :     int Res = ParseCompare(Inst, PFS, KeywordVal);
    5544         290 :     if (Res != 0)
    5545           2 :       return Res;
    5546         289 :     if (FMF.any())
    5547             :       Inst->setFastMathFlags(FMF);
    5548             :     return 0;
    5549             :   }
    5550        3208 : 
    5551             :   // Casts.
    5552             :   case lltok::kw_trunc:
    5553        3208 :   case lltok::kw_zext:
    5554        3208 :   case lltok::kw_sext:
    5555             :   case lltok::kw_fptrunc:
    5556             :   case lltok::kw_fpext:
    5557             :   case lltok::kw_bitcast:
    5558             :   case lltok::kw_addrspacecast:
    5559         978 :   case lltok::kw_uitofp:
    5560             :   case lltok::kw_sitofp:
    5561             :   case lltok::kw_fptoui:
    5562             :   case lltok::kw_fptosi:
    5563             :   case lltok::kw_inttoptr:
    5564         978 :   case lltok::kw_ptrtoint:       return ParseCast(Inst, PFS, KeywordVal);
    5565             :   // Other.
    5566             :   case lltok::kw_select:         return ParseSelect(Inst, PFS);
    5567         978 :   case lltok::kw_va_arg:         return ParseVA_Arg(Inst, PFS);
    5568         978 :   case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
    5569         978 :   case lltok::kw_insertelement:  return ParseInsertElement(Inst, PFS);
    5570             :   case lltok::kw_shufflevector:  return ParseShuffleVector(Inst, PFS);
    5571             :   case lltok::kw_phi:            return ParsePHI(Inst, PFS);
    5572             :   case lltok::kw_landingpad:     return ParseLandingPad(Inst, PFS);
    5573             :   // Call.
    5574             :   case lltok::kw_call:     return ParseCall(Inst, PFS, CallInst::TCK_None);
    5575          21 :   case lltok::kw_tail:     return ParseCall(Inst, PFS, CallInst::TCK_Tail);
    5576             :   case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
    5577             :   case lltok::kw_notail:   return ParseCall(Inst, PFS, CallInst::TCK_NoTail);
    5578             :   // Memory.
    5579             :   case lltok::kw_alloca:         return ParseAlloc(Inst, PFS);
    5580             :   case lltok::kw_load:           return ParseLoad(Inst, PFS);
    5581             :   case lltok::kw_store:          return ParseStore(Inst, PFS);
    5582             :   case lltok::kw_cmpxchg:        return ParseCmpXchg(Inst, PFS);
    5583             :   case lltok::kw_atomicrmw:      return ParseAtomicRMW(Inst, PFS);
    5584          21 :   case lltok::kw_fence:          return ParseFence(Inst, PFS);
    5585             :   case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
    5586             :   case lltok::kw_extractvalue:  return ParseExtractValue(Inst, PFS);
    5587          21 :   case lltok::kw_insertvalue:   return ParseInsertValue(Inst, PFS);
    5588             :   }
    5589             : }
    5590          21 : 
    5591             : /// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
    5592             : bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
    5593             :   if (Opc == Instruction::FCmp) {
    5594             :     switch (Lex.getKind()) {
    5595             :     default: return TokError("expected fcmp predicate (e.g. 'oeq')");
    5596         264 :     case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
    5597             :     case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
    5598             :     case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
    5599             :     case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
    5600             :     case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
    5601             :     case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
    5602             :     case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
    5603             :     case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
    5604         266 :     case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
    5605             :     case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
    5606             :     case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
    5607         262 :     case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
    5608             :     case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
    5609             :     case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
    5610         262 :     case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
    5611             :     case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
    5612             :     }
    5613             :   } else {
    5614             :     switch (Lex.getKind()) {
    5615             :     default: return TokError("expected icmp predicate (e.g. 'eq')");
    5616             :     case lltok::kw_eq:  P = CmpInst::ICMP_EQ; break;
    5617             :     case lltok::kw_ne:  P = CmpInst::ICMP_NE; break;
    5618             :     case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
    5619             :     case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
    5620             :     case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
    5621             :     case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
    5622             :     case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
    5623             :     case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
    5624             :     case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
    5625        9669 :     case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
    5626             :     }
    5627             :   }
    5628        9669 :   Lex.Lex();
    5629             :   return false;
    5630             : }
    5631        9666 : 
    5632        9666 : //===----------------------------------------------------------------------===//
    5633             : // Terminator Instructions.
    5634             : //===----------------------------------------------------------------------===//
    5635             : 
    5636             : /// ParseRet - Parse a return instruction.
    5637             : ///   ::= 'ret' void (',' !dbg, !1)*
    5638             : ///   ::= 'ret' TypeAndValue (',' !dbg, !1)*
    5639       23348 : bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
    5640             :                         PerFunctionState &PFS) {
    5641             :   SMLoc TypeLoc = Lex.getLoc();
    5642             :   Type *Ty = nullptr;
    5643       23348 :   if (ParseType(Ty, true /*void allowed*/)) return true;
    5644             : 
    5645       46694 :   Type *ResType = PFS.getFunction().getReturnType();
    5646           1 : 
    5647             :   if (Ty->isVoidTy()) {
    5648             :     if (!ResType->isVoidTy())
    5649       23346 :       return Error(TypeLoc, "value doesn't match function result type '" +
    5650             :                    getTypeString(ResType) + "'");
    5651             : 
    5652       23341 :     Inst = ReturnInst::Create(Context);
    5653       23341 :     return false;
    5654             :   }
    5655             : 
    5656             :   Value *RV;
    5657             :   if (ParseValue(Ty, RV, PFS)) return true;
    5658             : 
    5659             :   if (ResType != RV->getType())
    5660             :     return Error(TypeLoc, "value doesn't match function result type '" +
    5661             :                  getTypeString(ResType) + "'");
    5662             : 
    5663             :   Inst = ReturnInst::Create(Context, RV);
    5664      117851 :   return false;
    5665      117851 : }
    5666             : 
    5667        2773 : /// ParseBr
    5668             : ///   ::= 'br' TypeAndValue
    5669        2773 : ///   ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    5670        2773 : bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
    5671             :   LocTy Loc, Loc2;
    5672             :   Value *Op0;
    5673             :   BasicBlock *Op1, *Op2;
    5674             :   if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
    5675      115078 : 
    5676       23348 :   if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
    5677             :     Inst = BranchInst::Create(BB);
    5678             :     return false;
    5679             :   }
    5680       91730 : 
    5681             :   if (Op0->getType() != Type::getInt1Ty(Context))
    5682             :     return Error(Loc, "branch condition must have 'i1' type");
    5683             : 
    5684       91730 :   if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
    5685             :       ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
    5686       12954 :       ParseToken(lltok::comma, "expected ',' after true destination") ||
    5687             :       ParseTypeAndBasicBlock(Op2, Loc2, PFS))
    5688       12954 :     return true;
    5689       12954 : 
    5690             :   Inst = BranchInst::Create(Op1, Op2, Op0);
    5691             :   return false;
    5692             : }
    5693             : 
    5694             : /// ParseSwitch
    5695             : ///  Instruction
    5696       78776 : ///    ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
    5697             : ///  JumpTable
    5698       78773 : ///    ::= (TypeAndValue ',' TypeAndValue)*
    5699       78773 : bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
    5700             :   LocTy CondLoc, BBLoc;
    5701             :   Value *Cond;
    5702             :   BasicBlock *DefaultBB;
    5703             :   if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
    5704             :       ParseToken(lltok::comma, "expected ',' after switch condition") ||
    5705             :       ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
    5706     3732479 :       ParseToken(lltok::lsquare, "expected '[' with switch table"))
    5707             :     return true;
    5708     3732479 : 
    5709           0 :   if (!Cond->getType()->isIntegerTy())
    5710             :     return Error(CondLoc, "switch condition must have integer type");
    5711     3732479 : 
    5712      280353 :   // Parse the jump table pairs.
    5713      280353 :   SmallPtrSet<Value*, 32> SeenCases;
    5714      280353 :   SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
    5715      280353 :   while (Lex.getKind() != lltok::rsquare) {
    5716     1543552 :     Value *Constant;
    5717     1543556 :     BasicBlock *DestBB;
    5718     1543548 : 
    5719     1543548 :     if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
    5720       16764 :         ParseToken(lltok::comma, "expected ',' after case value") ||
    5721       16764 :         ParseTypeAndBasicBlock(DestBB, PFS))
    5722           3 :       return true;
    5723       16761 : 
    5724             :     if (!SeenCases.insert(Constant).second)
    5725       16761 :       return Error(CondLoc, "duplicate case value in switch");
    5726       16761 :     if (!isa<ConstantInt>(Constant))
    5727             :       return Error(CondLoc, "case value is not a constant integer");
    5728      219691 : 
    5729      219691 :     Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
    5730      219691 :   }
    5731         643 : 
    5732         643 :   Lex.Lex();  // Eat the ']'.
    5733         643 : 
    5734             :   SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
    5735     1253558 :   for (unsigned i = 0, e = Table.size(); i != e; ++i)
    5736           1 :     SI->addCase(Table[i].first, Table[i].second);
    5737     1253557 :   Inst = SI;
    5738     1253557 :   return false;
    5739     1253557 : }
    5740             : 
    5741       58767 : /// ParseIndirectBr
    5742       58767 : ///  Instruction
    5743           2 : ///    ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
    5744             : bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
    5745             :   LocTy AddrLoc;
    5746             :   Value *Address;
    5747       58767 :   if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
    5748             :       ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
    5749       57149 :       ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
    5750        2222 :     return true;
    5751             : 
    5752       54927 :   if (!Address->getType()->isPointerTy())
    5753       38772 :     return Error(AddrLoc, "indirectbr address must have pointer type");
    5754             : 
    5755             :   // Parse the destination list.
    5756       58767 :   SmallVector<BasicBlock*, 16> DestList;
    5757             : 
    5758       58767 :   if (Lex.getKind() != lltok::rsquare) {
    5759           3 :     BasicBlock *DestBB;
    5760           4 :     if (ParseTypeAndBasicBlock(DestBB, PFS))
    5761             :       return true;
    5762             :     DestList.push_back(DestBB);
    5763             : 
    5764        8096 :     while (EatIfPresent(lltok::comma)) {
    5765           0 :       if (ParseTypeAndBasicBlock(DestBB, PFS))
    5766        8096 :         return true;
    5767        8096 :       DestList.push_back(DestBB);
    5768             :     }
    5769             :   }
    5770      171695 : 
    5771           0 :   if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
    5772      171695 :     return true;
    5773      171695 : 
    5774             :   IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
    5775          19 :   for (unsigned i = 0, e = DestList.size(); i != e; ++i)
    5776           0 :     IBI->addDestination(DestList[i]);
    5777          19 :   Inst = IBI;
    5778          19 :   return false;
    5779             : }
    5780             : 
    5781       29561 : /// ParseInvoke
    5782           0 : ///   ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
    5783       29561 : ///       OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
    5784       29561 : bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
    5785             :   LocTy CallLoc = Lex.getLoc();
    5786         503 :   AttrBuilder RetAttrs, FnAttrs;
    5787           0 :   std::vector<unsigned> FwdRefAttrGrps;
    5788         503 :   LocTy NoBuiltinLoc;
    5789         503 :   unsigned CC;
    5790      147447 :   unsigned InvokeAddrSpace;
    5791      147447 :   Type *RetType = nullptr;
    5792           0 :   LocTy RetTypeLoc;
    5793             :   ValID CalleeID;
    5794      147447 :   SmallVector<ParamInfo, 16> ArgList;
    5795      147447 :   SmallVector<OperandBundleDef, 2> BundleList;
    5796             : 
    5797             :   BasicBlock *NormalBB, *UnwindBB;
    5798             :   if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
    5799        1828 :       ParseOptionalProgramAddrSpace(InvokeAddrSpace) ||
    5800           1 :       ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
    5801             :       ParseValID(CalleeID) || ParseParameterList(ArgList, PFS) ||
    5802        1827 :       ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
    5803           0 :                                  NoBuiltinLoc) ||
    5804             :       ParseOptionalOperandBundles(BundleList, PFS) ||
    5805             :       ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
    5806        7051 :       ParseTypeAndBasicBlock(NormalBB, PFS) ||
    5807       15675 :       ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
    5808           1 :       ParseTypeAndBasicBlock(UnwindBB, PFS))
    5809           1 :     return true;
    5810             : 
    5811        3652 :   // If RetType is a non-function pointer type, then this is the short syntax
    5812             :   // for the call, which means that RetType is just the return type.  Infer the
    5813             :   // rest of the function argument types from the arguments that are present.
    5814           0 :   FunctionType *Ty = dyn_cast<FunctionType>(RetType);
    5815        1826 :   if (!Ty) {
    5816             :     // Pull out the types of all of the arguments...
    5817           0 :     std::vector<Type*> ParamTypes;
    5818             :     for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
    5819             :       ParamTypes.push_back(ArgList[i].V->getType());
    5820        1040 : 
    5821        1040 :     if (!FunctionType::isValidReturnType(RetType))
    5822        2080 :       return Error(RetTypeLoc, "Invalid result type for LLVM function");
    5823        1040 : 
    5824        1040 :     Ty = FunctionType::get(RetType, ParamTypes, false);
    5825             :   }
    5826        1040 : 
    5827        1035 :   CalleeID.FTy = Ty;
    5828             : 
    5829             :   // Look up the callee.
    5830             :   Value *Callee;
    5831             :   if (ConvertValIDToValue(PointerType::get(Ty, InvokeAddrSpace), CalleeID,
    5832             :                           Callee, &PFS, /*IsCall=*/true))
    5833             :     return true;
    5834        1035 : 
    5835             :   // Set up the Attribute for the function.
    5836             :   SmallVector<Value *, 8> Args;
    5837        1033 :   SmallVector<AttributeSet, 8> ArgAttrs;
    5838        1033 : 
    5839             :   // Loop through FunctionType's arguments and ensure they are specified
    5840           4 :   // correctly.  Also, gather any parameter attributes.
    5841           4 :   FunctionType::param_iterator I = Ty->param_begin();
    5842           4 :   FunctionType::param_iterator E = Ty->param_end();
    5843             :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    5844           1 :     Type *ExpectedTy = nullptr;
    5845             :     if (I != E) {
    5846             :       ExpectedTy = *I++;
    5847             :     } else if (!Ty->isVarArg()) {
    5848     2491971 :       return Error(ArgList[i].Loc, "too many arguments specified");
    5849     2491971 :     }
    5850     4983942 : 
    5851     4983938 :     if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
    5852     2491967 :       return Error(ArgList[i].Loc, "argument is not of expected type '" +
    5853             :                    getTypeString(ExpectedTy) + "'");
    5854             :     Args.push_back(ArgList[i].V);
    5855     1625385 :     ArgAttrs.push_back(ArgList[i].Attrs);
    5856     1625385 :   }
    5857     3250770 : 
    5858     1625385 :   if (I != E)
    5859             :     return Error(CallLoc, "not enough parameters specified for call");
    5860             : 
    5861       89815 :   if (FnAttrs.hasAlignmentAttr())
    5862             :     return Error(CallLoc, "invoke instructions may not have an alignment");
    5863             : 
    5864       89815 :   // Finish off the Attribute and check them
    5865       89815 :   AttributeList PAL =
    5866      179630 :       AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
    5867           0 :                          AttributeSet::get(Context, RetAttrs), ArgAttrs);
    5868       89815 : 
    5869       89815 :   InvokeInst *II =
    5870             :       InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
    5871             :   II->setCallingConv(CC);
    5872             :   II->setAttributes(PAL);
    5873             :   ForwardRefAttrGroups[II] = FwdRefAttrGrps;
    5874             :   Inst = II;
    5875             :   return false;
    5876             : }
    5877      323315 : 
    5878             : /// ParseResume
    5879      323315 : ///   ::= 'resume' TypeAndValue
    5880             : bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
    5881             :   Value *Exn; LocTy ExnLoc;
    5882             :   if (ParseTypeAndValue(Exn, ExnLoc, PFS))
    5883             :     return true;
    5884             : 
    5885             :   ResumeInst *RI = ResumeInst::Create(Exn);
    5886             :   Inst = RI;
    5887      323315 :   return false;
    5888             : }
    5889      323315 : 
    5890      323314 : bool LLParser::ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
    5891      969941 :                                   PerFunctionState &PFS) {
    5892             :   if (ParseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
    5893           3 :     return true;
    5894             : 
    5895             :   while (Lex.getKind() != lltok::rsquare) {
    5896      323312 :     // If this isn't the first argument, we need a comma.
    5897             :     if (!Args.empty() &&
    5898             :         ParseToken(lltok::comma, "expected ',' in argument list"))
    5899          61 :       return true;
    5900          61 : 
    5901           0 :     // Parse the argument.
    5902             :     LocTy ArgLoc;
    5903        2628 :     Type *ArgTy = nullptr;
    5904             :     if (ParseType(ArgTy, ArgLoc))
    5905             :       return true;
    5906             : 
    5907             :     Value *V;
    5908             :     if (ArgTy->isMetadataTy()) {
    5909             :       if (ParseMetadataAsValue(V, PFS))
    5910        2628 :         return true;
    5911           0 :     } else {
    5912             :       if (ParseValue(ArgTy, V, PFS))
    5913             :         return true;
    5914             :     }
    5915           0 :     Args.push_back(V);
    5916             :   }
    5917             : 
    5918      323312 :   Lex.Lex();  // Lex the ']'.
    5919           4 :   return false;
    5920             : }
    5921             : 
    5922      323308 : /// ParseCleanupRet
    5923           0 : ///   ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
    5924             : bool LLParser::ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
    5925      323308 :   Value *CleanupPad = nullptr;
    5926             : 
    5927             :   if (ParseToken(lltok::kw_from, "expected 'from' after cleanupret"))
    5928      323308 :     return true;
    5929             : 
    5930          44 :   if (ParseValue(Type::getTokenTy(Context), CleanupPad, PFS))
    5931          44 :     return true;
    5932             : 
    5933          88 :   if (ParseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
    5934           0 :     return true;
    5935           0 : 
    5936             :   BasicBlock *UnwindBB = nullptr;
    5937           0 :   if (Lex.getKind() == lltok::kw_to) {
    5938             :     Lex.Lex();
    5939             :     if (ParseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
    5940      323308 :       return true;
    5941             :   } else {
    5942      323308 :     if (ParseTypeAndBasicBlock(UnwindBB, PFS)) {
    5943           0 :       return true;
    5944             :     }
    5945      323308 :   }
    5946             : 
    5947             :   Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
    5948             :   return false;
    5949      323308 : }
    5950             : 
    5951             : /// ParseCatchRet
    5952             : ///   ::= 'catchret' from Parent Value 'to' TypeAndValue
    5953      323308 : bool LLParser::ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
    5954      323308 :   Value *CatchPad = nullptr;
    5955      323308 : 
    5956      323308 :   if (ParseToken(lltok::kw_from, "expected 'from' after catchret"))
    5957      323308 :     return true;
    5958             : 
    5959             :   if (ParseValue(Type::getTokenTy(Context), CatchPad, PFS))
    5960      646614 :     return true;
    5961      646612 : 
    5962      646612 :   BasicBlock *BB;
    5963      323306 :   if (ParseToken(lltok::kw_to, "expected 'to' in catchret") ||
    5964             :       ParseTypeAndBasicBlock(BB, PFS))
    5965         182 :       return true;
    5966      323487 : 
    5967      969913 :   Inst = CatchReturnInst::Create(CatchPad, BB);
    5968      323304 :   return false;
    5969         488 : }
    5970         488 : 
    5971          25 : /// ParseCatchSwitch
    5972          25 : ///   ::= 'catchswitch' within Parent
    5973          46 : bool LLParser::ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
    5974      323354 :   Value *ParentPad;
    5975        1128 : 
    5976        1128 :   if (ParseToken(lltok::kw_within, "expected 'within' after catchswitch"))
    5977           4 :     return true;
    5978             : 
    5979      323304 :   if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
    5980           0 :       Lex.getKind() != lltok::LocalVarID)
    5981             :     return TokError("expected scope value for catchswitch");
    5982             : 
    5983      323304 :   if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
    5984         635 :     return true;
    5985         635 : 
    5986             :   if (ParseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
    5987             :     return true;
    5988             : 
    5989             :   SmallVector<BasicBlock *, 32> Table;
    5990             :   do {
    5991             :     BasicBlock *DestBB;
    5992             :     if (ParseTypeAndBasicBlock(DestBB, PFS))
    5993      891997 :       return true;
    5994     1137386 :     Table.push_back(DestBB);
    5995      568693 :   } while (EatIfPresent(lltok::comma));
    5996             : 
    5997             :   if (ParseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
    5998             :     return true;
    5999             : 
    6000      323304 :   if (ParseToken(lltok::kw_unwind,
    6001             :                  "expected 'unwind' after catchswitch scope"))
    6002      323304 :     return true;
    6003           0 : 
    6004             :   BasicBlock *UnwindBB = nullptr;
    6005             :   if (EatIfPresent(lltok::kw_to)) {
    6006      646608 :     if (ParseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
    6007      323304 :       return true;
    6008             :   } else {
    6009      323304 :     if (ParseTypeAndBasicBlock(UnwindBB, PFS))
    6010      323304 :       return true;
    6011             :   }
    6012             : 
    6013             :   auto *CatchSwitch =
    6014      323255 :       CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
    6015       85380 :   for (BasicBlock *DestBB : Table)
    6016       42690 :     CatchSwitch->addHandler(DestBB);
    6017           1 :   Inst = CatchSwitch;
    6018             :   return false;
    6019       42689 : }
    6020           1 : 
    6021           2 : /// ParseCatchPad
    6022           4 : ///   ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
    6023           4 : bool LLParser::ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
    6024             :   Value *CatchSwitch = nullptr;
    6025      561130 : 
    6026             :   if (ParseToken(lltok::kw_within, "expected 'within' after catchpad"))
    6027           3 :     return true;
    6028           2 : 
    6029      561128 :   if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
    6030           0 :     return TokError("expected scope value for catchpad");
    6031             : 
    6032             :   if (ParseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
    6033             :     return true;
    6034             : 
    6035             :   SmallVector<Value *, 8> Args;
    6036          98 :   if (ParseExceptionArgs(Args, PFS))
    6037          49 :     return true;
    6038          21 : 
    6039          21 :   Inst = CatchPadInst::Create(CatchSwitch, Args);
    6040           0 :   return false;
    6041           0 : }
    6042           0 : 
    6043           0 : /// ParseCleanupPad
    6044             : ///   ::= 'cleanuppad' within Parent ParamList
    6045             : bool LLParser::ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
    6046             :   Value *ParentPad = nullptr;
    6047             : 
    6048      323301 :   if (ParseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
    6049      841776 :     return true;
    6050             : 
    6051             :   if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
    6052       85418 :       Lex.getKind() != lltok::LocalVarID)
    6053             :     return TokError("expected scope value for cleanuppad");
    6054             : 
    6055             :   if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
    6056      323301 :     return true;
    6057          49 : 
    6058             :   SmallVector<Value *, 8> Args;
    6059      323301 :   if (ParseExceptionArgs(Args, PFS))
    6060      323301 :     return true;
    6061      323301 : 
    6062      323301 :   Inst = CleanupPadInst::Create(ParentPad, Args);
    6063      323301 :   return false;
    6064             : }
    6065      323301 : 
    6066      323301 : //===----------------------------------------------------------------------===//
    6067      646602 : // Binary Operators.
    6068      323301 : //===----------------------------------------------------------------------===//
    6069      323301 : 
    6070      323789 : /// ParseArithmetic
    6071      323301 : ///  ::= ArithmeticOps TypeAndValue ',' Value
    6072      323301 : ///
    6073      323301 : /// If OperandType is 0, then any FP or integer operand is allowed.  If it is 1,
    6074             : /// then any integer operand is allowed, if it is 2, any fp operand is allowed.
    6075             : bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
    6076      323301 :                                unsigned Opc, unsigned OperandType) {
    6077      891989 :   LocTy Loc; Value *LHS, *RHS;
    6078             :   if (ParseTypeAndValue(LHS, Loc, PFS) ||
    6079     1137376 :       ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
    6080             :       ParseValue(LHS->getType(), RHS, PFS))
    6081             :     return true;
    6082      885516 : 
    6083             :   bool Valid;
    6084      442758 :   switch (OperandType) {
    6085           0 :   default: llvm_unreachable("Unknown operand type!");
    6086           0 :   case 0: // int or FP.
    6087             :     Valid = LHS->getType()->isIntOrIntVectorTy() ||
    6088             :             LHS->getType()->isFPOrFPVectorTy();
    6089      323301 :     break;
    6090             :   case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
    6091             :   case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
    6092             :   }
    6093      132784 : 
    6094       66392 :   if (!Valid)
    6095           1 :     return Error(Loc, "invalid operand type for instruction");
    6096           2 : 
    6097             :   Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
    6098       66391 :   return false;
    6099             : }
    6100             : 
    6101             : /// ParseLogical
    6102       66392 : ///  ::= ArithmeticOps TypeAndValue ',' Value {
    6103           0 : bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
    6104             :                             unsigned Opc) {
    6105             :   LocTy Loc; Value *LHS, *RHS;
    6106             :   if (ParseTypeAndValue(LHS, Loc, PFS) ||
    6107             :       ParseToken(lltok::comma, "expected ',' in logical operation") ||
    6108      256909 :       ParseValue(LHS->getType(), RHS, PFS))
    6109      513818 :     return true;
    6110      256909 : 
    6111      256861 :   if (!LHS->getType()->isIntOrIntVectorTy())
    6112      770583 :     return Error(Loc,"instruction requires integer or integer vector operands");
    6113             : 
    6114          48 :   Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
    6115          48 :   return false;
    6116             : }
    6117             : 
    6118      256909 : /// ParseCompare
    6119      513818 : ///  ::= 'icmp' IPredicates TypeAndValue ',' Value
    6120             : ///  ::= 'fcmp' FPredicates TypeAndValue ',' Value
    6121             : bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
    6122         452 :                             unsigned Opc) {
    6123         326 :   // Parse the integer/fp comparison predicate.
    6124         326 :   LocTy Loc;
    6125             :   unsigned Pred;
    6126             :   Value *LHS, *RHS;
    6127             :   if (ParseCmpPredicate(Pred, Opc) ||
    6128             :       ParseTypeAndValue(LHS, Loc, PFS) ||
    6129         326 :       ParseToken(lltok::comma, "expected ',' after compare value") ||
    6130         317 :       ParseValue(LHS->getType(), RHS, PFS))
    6131             :     return true;
    6132           9 : 
    6133         326 :   if (Opc == Instruction::FCmp) {
    6134           0 :     if (!LHS->getType()->isFPOrFPVectorTy())
    6135             :       return Error(Loc, "fcmp requires floating point operands");
    6136         326 :     Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
    6137         326 :   } else {
    6138             :     assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
    6139             :     if (!LHS->getType()->isIntOrIntVectorTy() &&
    6140         126 :         !LHS->getType()->isPtrOrPtrVectorTy())
    6141         126 :       return Error(Loc, "icmp requires integer operands");
    6142             :     Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
    6143             :   }
    6144             :   return false;
    6145             : }
    6146      256909 : 
    6147      256909 : //===----------------------------------------------------------------------===//
    6148           0 : // Other Instructions.
    6149      256909 : //===----------------------------------------------------------------------===//
    6150             : 
    6151             : 
    6152      256909 : /// ParseCast
    6153             : ///   ::= CastOpc TypeAndValue 'to' Type
    6154      513818 : bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
    6155             :                          unsigned Opc) {
    6156             :   LocTy Loc;
    6157             :   Value *Op;
    6158      256909 :   Type *DestTy = nullptr;
    6159             :   if (ParseTypeAndValue(Op, Loc, PFS) ||
    6160      256909 :       ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
    6161             :       ParseType(DestTy))
    6162             :     return true;
    6163      256909 : 
    6164           0 :   if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
    6165             :     CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
    6166      607056 :     return Error(Loc, "invalid cast opcode for cast from '" +
    6167             :                  getTypeString(Op->getType()) + "' to '" +
    6168      350222 :                  getTypeString(DestTy) + "'");
    6169             :   }
    6170      257590 :   Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
    6171         759 :   return false;
    6172             : }
    6173             : 
    6174             : /// ParseSelect
    6175             : ///   ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    6176             : bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
    6177             :   LocTy Loc;
    6178      256831 :   Value *Op0, *Op1, *Op2;
    6179             :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6180             :       ParseToken(lltok::comma, "expected ',' after select condition") ||
    6181             :       ParseTypeAndValue(Op1, PFS) ||
    6182             :       ParseToken(lltok::comma, "expected ',' after select value") ||
    6183      350222 :       ParseTypeAndValue(Op2, PFS))
    6184             :     return true;
    6185             : 
    6186      350222 :   if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
    6187      350222 :     return Error(Loc, Reason);
    6188             : 
    6189      177750 :   Inst = SelectInst::Create(Op0, Op1, Op2);
    6190             :   return false;
    6191             : }
    6192      350222 : 
    6193      350222 : /// ParseVA_Arg
    6194           1 : ///   ::= 'va_arg' TypeAndValue ',' Type
    6195           2 : bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
    6196             :   Value *Op;
    6197             :   Type *EltTy = nullptr;
    6198             :   LocTy TypeLoc;
    6199             :   if (ParseTypeAndValue(Op, PFS) ||
    6200             :       ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
    6201             :       ParseType(EltTy, TypeLoc))
    6202             :     return true;
    6203             : 
    6204     1519310 :   if (!EltTy->isFirstClassType())
    6205             :     return Error(TypeLoc, "va_arg requires operand with first class type");
    6206             : 
    6207             :   Inst = new VAArgInst(Op, EltTy);
    6208     1519310 :   return false;
    6209      249136 : }
    6210      249136 : 
    6211      249136 : /// ParseExtractElement
    6212          74 : ///   ::= 'extractelement' TypeAndValue ',' TypeAndValue
    6213     1270174 : bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
    6214             :   LocTy Loc;
    6215      737344 :   Value *Op0, *Op1;
    6216      737344 :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6217             :       ParseToken(lltok::comma, "expected ',' after extract value") ||
    6218             :       ParseTypeAndValue(Op1, PFS))
    6219             :     return true;
    6220     1519310 : 
    6221           0 :   if (!ExtractElementInst::isValidOperands(Op0, Op1))
    6222             :     return Error(Loc, "invalid extractelement operands");
    6223             : 
    6224     1508443 :   Inst = ExtractElementInst::Create(Op0, Op1);
    6225             :   return false;
    6226             : }
    6227             : 
    6228             : /// ParseInsertElement
    6229       15846 : ///   ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    6230             : bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
    6231             :   LocTy Loc;
    6232             :   Value *Op0, *Op1, *Op2;
    6233       10798 :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6234             :       ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6235             :       ParseTypeAndValue(Op1, PFS) ||
    6236             :       ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6237       10798 :       ParseTypeAndValue(Op2, PFS))
    6238             :     return true;
    6239             : 
    6240             :   if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
    6241             :     return Error(Loc, "invalid insertelement operands");
    6242             : 
    6243     1519237 :   Inst = InsertElementInst::Create(Op0, Op1, Op2);
    6244     3038472 :   return false;
    6245             : }
    6246             : 
    6247             : /// ParseShuffleVector
    6248             : ///   ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    6249             : bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
    6250             :   LocTy Loc;
    6251             :   Value *Op0, *Op1, *Op2;
    6252             :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6253             :       ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
    6254             :       ParseTypeAndValue(Op1, PFS) ||
    6255     1519310 :       ParseToken(lltok::comma, "expected ',' after shuffle value") ||
    6256             :       ParseTypeAndValue(Op2, PFS))
    6257     1519310 :     return true;
    6258     1519310 : 
    6259           1 :   if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
    6260     1519309 :     return Error(Loc, "invalid shufflevector operands");
    6261     1519309 : 
    6262     1519309 :   Inst = new ShuffleVectorInst(Op0, Op1, Op2);
    6263             :   return false;
    6264     1519309 : }
    6265           2 : 
    6266             : /// ParsePHI
    6267        3892 : ///   ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
    6268      260211 : int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
    6269       81243 :   Type *Ty = nullptr;  LocTy TypeLoc;
    6270        1515 :   Value *Op0, *Op1;
    6271         350 : 
    6272        1998 :   if (ParseType(Ty, TypeLoc) ||
    6273         254 :       ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
    6274         196 :       ParseValue(Ty, Op0, PFS) ||
    6275         219 :       ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6276         281 :       ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
    6277         305 :       ParseToken(lltok::rsquare, "expected ']' in phi value list"))
    6278         287 :     return true;
    6279             : 
    6280             :   bool AteExtraComma = false;
    6281             :   SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
    6282             : 
    6283             :   while (true) {
    6284             :     PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
    6285             : 
    6286       86971 :     if (!EatIfPresent(lltok::comma))
    6287             :       break;
    6288       86971 : 
    6289             :     if (Lex.getKind() == lltok::MetadataVar) {
    6290       86970 :       AteExtraComma = true;
    6291       86970 :       break;
    6292             :     }
    6293             : 
    6294       40570 :     if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
    6295             :         ParseValue(Ty, Op0, PFS) ||
    6296             :         ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6297             :         ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
    6298             :         ParseToken(lltok::rsquare, "expected ']' in phi value list"))
    6299       40570 :       return true;
    6300       40570 :   }
    6301       40570 : 
    6302             :   if (!Ty->isFirstClassType())
    6303       40570 :     return Error(TypeLoc, "phi node must have first class type");
    6304        4267 : 
    6305             :   PHINode *PN = PHINode::Create(Ty, PHIVals.size());
    6306             :   for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
    6307             :     PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
    6308             :   Inst = PN;
    6309             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6310             : }
    6311             : 
    6312             : /// ParseLandingPad
    6313             : ///   ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
    6314       21132 : /// Clause
    6315       21132 : ///   ::= 'catch' TypeAndValue
    6316             : ///   ::= 'filter'
    6317             : ///   ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
    6318             : bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
    6319        4131 :   Type *Ty = nullptr; LocTy TyLoc;
    6320        4131 : 
    6321       39925 :   if (ParseType(Ty, TyLoc))
    6322             :     return true;
    6323       39925 : 
    6324       59985 :   std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
    6325       10938 :   LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
    6326       10938 : 
    6327       10938 :   while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
    6328       10938 :     LandingPadInst::ClauseType CT;
    6329             :     if (EatIfPresent(lltok::kw_catch))
    6330       10938 :       CT = LandingPadInst::Catch;
    6331         840 :     else if (EatIfPresent(lltok::kw_filter))
    6332             :       CT = LandingPadInst::Filter;
    6333             :     else
    6334             :       return TokError("expected 'catch' or 'filter' clause type");
    6335             : 
    6336      133784 :     Value *V;
    6337             :     LocTy VLoc;
    6338             :     if (ParseTypeAndValue(V, VLoc, PFS))
    6339             :       return true;
    6340             : 
    6341             :     // A 'catch' type expects a non-array constant. A filter clause expects an
    6342             :     // array constant.
    6343             :     if (CT == LandingPadInst::Catch) {
    6344             :       if (isa<ArrayType>(V->getType()))
    6345             :         Error(VLoc, "'catch' clause has an invalid type");
    6346             :     } else {
    6347             :       if (!isa<ArrayType>(V->getType()))
    6348      133784 :         Error(VLoc, "'filter' clause has an invalid type");
    6349             :     }
    6350       28892 : 
    6351         217 :     Constant *CV = dyn_cast<Constant>(V);
    6352       30148 :     if (!CV)
    6353       36306 :       return Error(VLoc, "clause argument must be a constant");
    6354       43507 :     LP->addClause(CV);
    6355       25622 :   }
    6356        1152 : 
    6357             :   Inst = LP.release();
    6358      116691 :   return false;
    6359       37129 : }
    6360         181 : 
    6361          68 : /// ParseCall
    6362             : ///   ::= 'call' OptionalFastMathFlags OptionalCallingConv
    6363       49148 : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6364      166125 : ///   ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
    6365      127584 : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6366        1217 : ///   ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
    6367        6433 : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6368         698 : ///   ::= 'notail' 'call'  OptionalFastMathFlags OptionalCallingConv
    6369       91967 : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6370        6605 : bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
    6371        1430 :                          CallInst::TailCallKind TCK) {
    6372             :   AttrBuilder RetAttrs, FnAttrs;
    6373             :   std::vector<unsigned> FwdRefAttrGrps;
    6374             :   LocTy BuiltinLoc;
    6375             :   unsigned CallAddrSpace;
    6376       71230 :   unsigned CC;
    6377       71230 :   Type *RetType = nullptr;
    6378       10944 :   LocTy RetTypeLoc;
    6379           0 :   ValID CalleeID;
    6380        2736 :   SmallVector<ParamInfo, 16> ArgList;
    6381         328 :   SmallVector<OperandBundleDef, 2> BundleList;
    6382        1401 :   LocTy CallLoc = Lex.getLoc();
    6383        2007 : 
    6384         525 :   if (TCK != CallInst::TCK_None &&
    6385         582 :       ParseToken(lltok::kw_call,
    6386         322 :                  "expected 'tail call', 'musttail call', or 'notail call'"))
    6387         340 :     return true;
    6388         280 : 
    6389         634 :   FastMathFlags FMF = EatFastMathFlagsIfPresent();
    6390         424 : 
    6391         431 :   if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
    6392         358 :       ParseOptionalProgramAddrSpace(CallAddrSpace) ||
    6393         430 :       ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
    6394          72 :       ParseValID(CalleeID) ||
    6395          74 :       ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
    6396             :                          PFS.getFunction().isVarArg()) ||
    6397             :       ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
    6398       60286 :       ParseOptionalOperandBundles(BundleList, PFS))
    6399           0 :     return true;
    6400       21711 : 
    6401        7709 :   if (FMF.any() && !RetType->isFPOrFPVectorTy())
    6402        8754 :     return Error(CallLoc, "fast-math-flags specified for call without "
    6403        8063 :                           "floating-point scalar or vector return type");
    6404        1602 : 
    6405        1675 :   // If RetType is a non-function pointer type, then this is the short syntax
    6406        4759 :   // for the call, which means that RetType is just the return type.  Infer the
    6407        3406 :   // rest of the function argument types from the arguments that are present.
    6408        1221 :   FunctionType *Ty = dyn_cast<FunctionType>(RetType);
    6409        1386 :   if (!Ty) {
    6410             :     // Pull out the types of all of the arguments...
    6411             :     std::vector<Type*> ParamTypes;
    6412       71230 :     for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
    6413       71230 :       ParamTypes.push_back(ArgList[i].V->getType());
    6414             : 
    6415             :     if (!FunctionType::isValidReturnType(RetType))
    6416             :       return Error(RetTypeLoc, "Invalid result type for LLVM function");
    6417             : 
    6418             :     Ty = FunctionType::get(RetType, ParamTypes, false);
    6419             :   }
    6420             : 
    6421             :   CalleeID.FTy = Ty;
    6422             : 
    6423      260211 :   // Look up the callee.
    6424             :   Value *Callee;
    6425      260211 :   if (ConvertValIDToValue(PointerType::get(Ty, CallAddrSpace), CalleeID, Callee,
    6426      260211 :                           &PFS, /*IsCall=*/true))
    6427      260211 :     return true;
    6428             : 
    6429      260211 :   // Set up the Attribute for the function.
    6430             :   SmallVector<AttributeSet, 8> Attrs;
    6431      520422 : 
    6432       79150 :   SmallVector<Value*, 8> Args;
    6433           0 : 
    6434           0 :   // Loop through FunctionType's arguments and ensure they are specified
    6435             :   // correctly.  Also, gather any parameter attributes.
    6436       79150 :   FunctionType::param_iterator I = Ty->param_begin();
    6437       79150 :   FunctionType::param_iterator E = Ty->param_end();
    6438             :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    6439             :     Type *ExpectedTy = nullptr;
    6440             :     if (I != E) {
    6441      181061 :       ExpectedTy = *I++;
    6442             :     } else if (!Ty->isVarArg()) {
    6443      181054 :       return Error(ArgList[i].Loc, "too many arguments specified");
    6444           6 :     }
    6445           8 : 
    6446             :     if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
    6447      181052 :       return Error(ArgList[i].Loc, "argument is not of expected type '" +
    6448      181052 :                    getTypeString(ExpectedTy) + "'");
    6449             :     Args.push_back(ArgList[i].V);
    6450             :     Attrs.push_back(ArgList[i].Attrs);
    6451             :   }
    6452             : 
    6453             :   if (I != E)
    6454       81243 :     return Error(CallLoc, "not enough parameters specified for call");
    6455       81243 : 
    6456             :   if (FnAttrs.hasAlignmentAttr())
    6457             :     return Error(CallLoc, "call instructions may not have an alignment");
    6458       81243 : 
    6459             :   // Finish off the Attribute and check them
    6460       81243 :   AttributeList PAL =
    6461       43666 :       AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
    6462       43666 :                          AttributeSet::get(Context, RetAttrs), Attrs);
    6463             : 
    6464             :   CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
    6465       37577 :   CI->setTailCallKind(TCK);
    6466           0 :   CI->setCallingConv(CC);
    6467             :   if (FMF.any())
    6468       75154 :     CI->setFastMathFlags(FMF);
    6469       75154 :   CI->setAttributes(PAL);
    6470      112731 :   ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
    6471       37577 :   Inst = CI;
    6472           0 :   return false;
    6473             : }
    6474       37577 : 
    6475       37577 : //===----------------------------------------------------------------------===//
    6476             : // Memory Instructions.
    6477             : //===----------------------------------------------------------------------===//
    6478             : 
    6479             : /// ParseAlloc
    6480             : ///   ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
    6481             : ///       (',' 'align' i32)? (',', 'addrspace(n))?
    6482             : int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
    6483        1515 :   Value *Size = nullptr;
    6484        1515 :   LocTy SizeLoc, TyLoc, ASLoc;
    6485             :   unsigned Alignment = 0;
    6486             :   unsigned AddrSpace = 0;
    6487        1515 :   Type *Ty = nullptr;
    6488        3030 : 
    6489        4545 :   bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
    6490        1515 :   bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
    6491           0 : 
    6492             :   if (ParseType(Ty, TyLoc)) return true;
    6493        3030 : 
    6494           0 :   if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
    6495             :     return Error(TyLoc, "invalid type for alloca");
    6496             : 
    6497             :   bool AteExtraComma = false;
    6498             :   if (EatIfPresent(lltok::comma)) {
    6499        8986 :     if (Lex.getKind() == lltok::kw_align) {
    6500             :       if (ParseOptionalAlignment(Alignment))
    6501             :         return true;
    6502             :       if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
    6503        7471 :         return true;
    6504       14942 :     } else if (Lex.getKind() == lltok::kw_addrspace) {
    6505             :       ASLoc = Lex.getLoc();
    6506           0 :       if (ParseOptionalAddrSpace(AddrSpace))
    6507             :         return true;
    6508        7471 :     } else if (Lex.getKind() == lltok::MetadataVar) {
    6509           0 :       AteExtraComma = true;
    6510       14942 :     } else {
    6511           0 :       if (ParseTypeAndValue(Size, SizeLoc, PFS))
    6512             :         return true;
    6513        7471 :       if (EatIfPresent(lltok::comma)) {
    6514             :         if (Lex.getKind() == lltok::kw_align) {
    6515             :           if (ParseOptionalAlignment(Alignment))
    6516        1515 :             return true;
    6517             :           if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
    6518        1515 :             return true;
    6519        8986 :         } else if (Lex.getKind() == lltok::kw_addrspace) {
    6520       14942 :           ASLoc = Lex.getLoc();
    6521        1515 :           if (ParseOptionalAddrSpace(AddrSpace))
    6522        1515 :             return true;
    6523             :         } else if (Lex.getKind() == lltok::MetadataVar) {
    6524             :           AteExtraComma = true;
    6525             :         }
    6526             :       }
    6527             :     }
    6528         350 :   }
    6529             : 
    6530             :   if (Size && !Size->getType()->isIntegerTy())
    6531         350 :     return Error(SizeLoc, "element count must have integer type");
    6532         700 : 
    6533         350 :   AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
    6534           0 :   AI->setUsedWithInAlloca(IsInAlloca);
    6535             :   AI->setSwiftError(IsSwiftError);
    6536         700 :   Inst = AI;
    6537           0 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6538             : }
    6539             : 
    6540             : /// ParseLoad
    6541             : ///   ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
    6542         350 : ///   ::= 'load' 'atomic' 'volatile'? TypeAndValue
    6543             : ///       'singlethread'? AtomicOrdering (',' 'align' i32)?
    6544         346 : int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
    6545           0 :   Value *Val; LocTy Loc;
    6546         346 :   unsigned Alignment = 0;
    6547             :   bool AteExtraComma = false;
    6548         673 :   bool isAtomic = false;
    6549         673 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6550             :   SyncScope::ID SSID = SyncScope::System;
    6551         673 : 
    6552             :   if (Lex.getKind() == lltok::kw_atomic) {
    6553             :     isAtomic = true;
    6554             :     Lex.Lex();
    6555         350 :   }
    6556             : 
    6557             :   bool isVolatile = false;
    6558         350 :   if (Lex.getKind() == lltok::kw_volatile) {
    6559        1369 :     isVolatile = true;
    6560        2038 :     Lex.Lex();
    6561         350 :   }
    6562         350 : 
    6563             :   Type *Ty;
    6564             :   LocTy ExplicitTypeLoc = Lex.getLoc();
    6565             :   if (ParseType(Ty) ||
    6566             :       ParseToken(lltok::comma, "expected comma after load's type") ||
    6567             :       ParseTypeAndValue(Val, Loc, PFS) ||
    6568        1998 :       ParseScopeAndOrdering(isAtomic, SSID, Ordering) ||
    6569        1998 :       ParseOptionalCommaAlign(Alignment, AteExtraComma))
    6570             :     return true;
    6571             : 
    6572        1998 :   if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
    6573             :     return Error(Loc, "load operand must be a pointer to a first class type");
    6574             :   if (isAtomic && !Alignment)
    6575        1998 :     return Error(Loc, "atomic load must have explicit non-zero alignment");
    6576             :   if (Ordering == AtomicOrdering::Release ||
    6577        3996 :       Ordering == AtomicOrdering::AcquireRelease)
    6578             :     return Error(Loc, "atomic load cannot use Release ordering");
    6579        1998 : 
    6580             :   if (Ty != cast<PointerType>(Val->getType())->getElementType())
    6581             :     return Error(ExplicitTypeLoc,
    6582        5994 :                  "explicit pointee type doesn't match operand's pointee type");
    6583        3996 : 
    6584        1998 :   Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, SSID);
    6585        5994 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6586        1998 : }
    6587        1998 : 
    6588        3996 : /// ParseStore
    6589        3996 : 
    6590        1998 : ///   ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
    6591        5994 : ///   ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
    6592             : ///       'singlethread'? AtomicOrdering (',' 'align' i32)?
    6593           0 : int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
    6594             :   Value *Val, *Ptr; LocTy Loc, PtrLoc;
    6595             :   unsigned Alignment = 0;
    6596             :   bool AteExtraComma = false;
    6597             :   bool isAtomic = false;
    6598        1998 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6599             :   SyncScope::ID SSID = SyncScope::System;
    6600             : 
    6601             :   if (Lex.getKind() == lltok::kw_atomic) {
    6602        3851 :     isAtomic = true;
    6603        3854 :     Lex.Lex();
    6604             :   }
    6605        1924 : 
    6606           0 :   bool isVolatile = false;
    6607             :   if (Lex.getKind() == lltok::kw_volatile) {
    6608        1924 :     isVolatile = true;
    6609             :     Lex.Lex();
    6610             :   }
    6611        1998 : 
    6612             :   if (ParseTypeAndValue(Val, Loc, PFS) ||
    6613             :       ParseToken(lltok::comma, "expected ',' after store operand") ||
    6614             :       ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
    6615        1998 :       ParseScopeAndOrdering(isAtomic, SSID, Ordering) ||
    6616             :       ParseOptionalCommaAlign(Alignment, AteExtraComma))
    6617             :     return true;
    6618             : 
    6619             :   if (!Ptr->getType()->isPointerTy())
    6620             :     return Error(PtrLoc, "store operand must be a pointer");
    6621             :   if (!Val->getType()->isFirstClassType())
    6622             :     return Error(Loc, "store operand must be a first class value");
    6623             :   if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
    6624             :     return Error(Loc, "stored value and pointer type do not match");
    6625        1997 :   if (isAtomic && !Alignment)
    6626        1997 :     return Error(Loc, "atomic store must have explicit non-zero alignment");
    6627        4227 :   if (Ordering == AtomicOrdering::Acquire ||
    6628             :       Ordering == AtomicOrdering::AcquireRelease)
    6629        2230 :     return Error(Loc, "atomic store cannot use Acquire ordering");
    6630        2044 : 
    6631         186 :   Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, SSID);
    6632           0 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6633             : }
    6634             : 
    6635        2044 : /// ParseCmpXchg
    6636           0 : ///   ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
    6637           0 : ///       TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
    6638        4460 : int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
    6639        2230 :   Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
    6640             :   bool AteExtraComma = false;
    6641             :   AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
    6642        1997 :   AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
    6643           0 :   SyncScope::ID SSID = SyncScope::System;
    6644             :   bool isVolatile = false;
    6645        1997 :   bool isWeak = false;
    6646           0 : 
    6647             :   if (EatIfPresent(lltok::kw_weak))
    6648             :     isWeak = true;
    6649             : 
    6650             :   if (EatIfPresent(lltok::kw_volatile))
    6651        1997 :     isVolatile = true;
    6652             : 
    6653             :   if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
    6654        1997 :       ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
    6655        1997 :       ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
    6656             :       ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
    6657        1997 :       ParseTypeAndValue(New, NewLoc, PFS) ||
    6658        1997 :       ParseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
    6659        1997 :       ParseOrdering(FailureOrdering))
    6660             :     return true;
    6661             : 
    6662             :   if (SuccessOrdering == AtomicOrdering::Unordered ||
    6663             :       FailureOrdering == AtomicOrdering::Unordered)
    6664         254 :     return TokError("cmpxchg cannot be unordered");
    6665             :   if (isStrongerThan(FailureOrdering, SuccessOrdering))
    6666         254 :     return TokError("cmpxchg failure argument shall be no stronger than the "
    6667             :                     "success argument");
    6668             :   if (FailureOrdering == AtomicOrdering::Release ||
    6669         254 :       FailureOrdering == AtomicOrdering::AcquireRelease)
    6670         254 :     return TokError(
    6671         254 :         "cmpxchg failure ordering cannot include release semantics");
    6672             :   if (!Ptr->getType()->isPointerTy())
    6673             :     return Error(PtrLoc, "cmpxchg operand must be a pointer");
    6674         592 :   if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
    6675             :     return Error(CmpLoc, "compare value and pointer type do not match");
    6676         592 :   if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
    6677             :     return Error(NewLoc, "new value and pointer type do not match");
    6678             :   if (!New->getType()->isFirstClassType())
    6679        1111 :     return Error(NewLoc, "cmpxchg operand must be a first class value");
    6680             :   AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
    6681         784 :       Ptr, Cmp, New, SuccessOrdering, FailureOrdering, SSID);
    6682         265 :   CXI->setVolatile(isVolatile);
    6683           0 :   CXI->setWeak(isWeak);
    6684             :   Inst = CXI;
    6685             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6686             : }
    6687         519 : 
    6688         519 : /// ParseAtomicRMW
    6689             : ///   ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
    6690             : ///       'singlethread'? AtomicOrdering
    6691             : int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
    6692        1038 :   Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
    6693           0 :   bool AteExtraComma = false;
    6694             :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6695             :   SyncScope::ID SSID = SyncScope::System;
    6696         519 :   bool isVolatile = false;
    6697             :   AtomicRMWInst::BinOp Operation;
    6698             : 
    6699         519 :   if (EatIfPresent(lltok::kw_volatile))
    6700             :     isVolatile = true;
    6701             : 
    6702         592 :   switch (Lex.getKind()) {
    6703         592 :   default: return TokError("expected binary operation in atomicrmw");
    6704             :   case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
    6705             :   case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
    6706             :   case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
    6707             :   case lltok::kw_and: Operation = AtomicRMWInst::And; break;
    6708         196 :   case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
    6709         196 :   case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
    6710             :   case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
    6711         196 :   case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
    6712             :   case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
    6713             :   case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
    6714         392 :   case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
    6715             :   }
    6716             :   Lex.Lex();  // Eat the operation.
    6717         196 : 
    6718             :   if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
    6719             :       ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
    6720         196 :       ParseTypeAndValue(Val, ValLoc, PFS) ||
    6721         196 :       ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
    6722         133 :     return true;
    6723         133 : 
    6724             :   if (Ordering == AtomicOrdering::Unordered)
    6725             :     return TokError("atomicrmw cannot be unordered");
    6726          63 :   if (!Ptr->getType()->isPointerTy())
    6727             :     return Error(PtrLoc, "atomicrmw operand must be a pointer");
    6728             :   if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
    6729             :     return Error(ValLoc, "atomicrmw value and pointer type do not match");
    6730             : 
    6731         196 :   if (!Val->getType()->isIntegerTy()) {
    6732         196 :     return Error(ValLoc, "atomicrmw " +
    6733             :                  AtomicRMWInst::getOperationName(Operation) +
    6734             :                  " operand must be an integer");
    6735             :   }
    6736             : 
    6737         219 :   unsigned Size = Val->getType()->getPrimitiveSizeInBits();
    6738         219 :   if (Size < 8 || (Size & (Size - 1)))
    6739             :     return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
    6740         219 :                          " integer");
    6741             : 
    6742             :   AtomicRMWInst *RMWI =
    6743         438 :     new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID);
    6744             :   RMWI->setVolatile(isVolatile);
    6745             :   Inst = RMWI;
    6746             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6747         438 : }
    6748             : 
    6749           0 : /// ParseFence
    6750             : ///   ::= 'fence' 'singlethread'? AtomicOrdering
    6751         219 : int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
    6752         219 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6753             :   SyncScope::ID SSID = SyncScope::System;
    6754             :   if (ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
    6755             :     return true;
    6756             : 
    6757         281 :   if (Ordering == AtomicOrdering::Unordered)
    6758             :     return TokError("fence cannot be unordered");
    6759             :   if (Ordering == AtomicOrdering::Monotonic)
    6760         281 :     return TokError("fence cannot be monotonic");
    6761             : 
    6762             :   Inst = new FenceInst(Context, Ordering, SSID);
    6763         281 :   return InstNormal;
    6764             : }
    6765           0 : 
    6766             : /// ParseGetElementPtr
    6767         562 : ///   ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
    6768             : int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
    6769             :   Value *Ptr = nullptr;
    6770         281 :   Value *Val = nullptr;
    6771             :   LocTy Loc, EltLoc;
    6772             : 
    6773             :   bool InBounds = EatIfPresent(lltok::kw_inbounds);
    6774             : 
    6775             :   Type *Ty = nullptr;
    6776         305 :   LocTy ExplicitTypeLoc = Lex.getLoc();
    6777           0 :   if (ParseType(Ty) ||
    6778         305 :       ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
    6779             :       ParseTypeAndValue(Ptr, Loc, PFS))
    6780             :     return true;
    6781         281 : 
    6782             :   Type *BaseType = Ptr->getType();
    6783             :   PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
    6784         281 :   if (!BasePointerType)
    6785             :     return Error(Loc, "base of getelementptr must be a pointer");
    6786             : 
    6787             :   if (Ty != BasePointerType->getElementType())
    6788         281 :     return Error(ExplicitTypeLoc,
    6789             :                  "explicit pointee type doesn't match operand's pointee type");
    6790         208 : 
    6791             :   SmallVector<Value*, 16> Indices;
    6792             :   bool AteExtraComma = false;
    6793          73 :   // GEP returns a vector of pointers if at least one of parameters is a vector.
    6794             :   // All vector parameters should have the same vector width.
    6795             :   unsigned GEPWidth = BaseType->isVectorTy() ?
    6796             :     BaseType->getVectorNumElements() : 0;
    6797             : 
    6798         562 :   while (EatIfPresent(lltok::comma)) {
    6799         586 :     if (Lex.getKind() == lltok::MetadataVar) {
    6800         305 :       AteExtraComma = true;
    6801         281 :       break;
    6802         281 :     }
    6803             :     if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
    6804             :     if (!Val->getType()->isIntOrIntVectorTy())
    6805             :       return Error(EltLoc, "getelementptr index must be an integer");
    6806             : 
    6807         305 :     if (Val->getType()->isVectorTy()) {
    6808         305 :       unsigned ValNumEl = Val->getType()->getVectorNumElements();
    6809             :       if (GEPWidth && GEPWidth != ValNumEl)
    6810         305 :         return Error(EltLoc,
    6811             :           "getelementptr vector index has a wrong number of elements");
    6812             :       GEPWidth = ValNumEl;
    6813         305 :     }
    6814           0 :     Indices.push_back(Val);
    6815             :   }
    6816         610 : 
    6817             :   SmallPtrSet<Type*, 4> Visited;
    6818             :   if (!Indices.empty() && !Ty->isSized(&Visited))
    6819             :     return Error(Loc, "base element of getelementptr must be sized");
    6820         305 : 
    6821             :   if (!GetElementPtrInst::getIndexedType(Ty, Indices))
    6822             :     return Error(Loc, "invalid getelementptr indices");
    6823         305 :   Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
    6824         305 :   if (InBounds)
    6825             :     cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
    6826             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6827             : }
    6828             : 
    6829         287 : /// ParseExtractValue
    6830         287 : ///   ::= 'extractvalue' TypeAndValue (',' uint32)+
    6831             : int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
    6832         287 :   Value *Val; LocTy Loc;
    6833             :   SmallVector<unsigned, 4> Indices;
    6834             :   bool AteExtraComma;
    6835         287 :   if (ParseTypeAndValue(Val, Loc, PFS) ||
    6836             :       ParseIndexList(Indices, AteExtraComma))
    6837           0 :     return true;
    6838             : 
    6839         574 :   if (!Val->getType()->isAggregateType())
    6840             :     return Error(Loc, "extractvalue operand must be aggregate type");
    6841             : 
    6842             :   if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
    6843         287 :     return Error(Loc, "invalid indices for extractvalue");
    6844             :   Inst = ExtractValueInst::Create(Val, Indices);
    6845             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6846         287 : }
    6847         287 : 
    6848             : /// ParseInsertValue
    6849             : ///   ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
    6850             : int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
    6851             :   Value *Val0, *Val1; LocTy Loc0, Loc1;
    6852             :   SmallVector<unsigned, 4> Indices;
    6853             :   bool AteExtraComma;
    6854             :   if (ParseTypeAndValue(Val0, Loc0, PFS) ||
    6855             :       ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
    6856             :       ParseTypeAndValue(Val1, Loc1, PFS) ||
    6857             :       ParseIndexList(Indices, AteExtraComma))
    6858             :     return true;
    6859      152804 : 
    6860             :   if (!Val0->getType()->isAggregateType())
    6861             :     return Error(Loc0, "insertvalue operand must be aggregate type");
    6862      152804 : 
    6863      305608 :   Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
    6864      152804 :   if (!IndexedType)
    6865           1 :     return Error(Loc0, "invalid indices for insertvalue");
    6866             :   if (IndexedType != Val1->getType())
    6867             :     return Error(Loc1, "insertvalue operand and field disagree in type: '" +
    6868      152803 :                            getTypeString(Val1->getType()) + "' instead of '" +
    6869           0 :                            getTypeString(IndexedType) + "'");
    6870           0 :   Inst = InsertValueInst::Create(Val0, Val1, Indices);
    6871           0 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6872             : }
    6873             : 
    6874      142786 : //===----------------------------------------------------------------------===//
    6875       40570 : // Embedded metadata.
    6876             : //===----------------------------------------------------------------------===//
    6877             : 
    6878      152803 : /// ParseMDNodeVector
    6879           0 : ///   ::= { Element (',' Element)* }
    6880             : /// Element
    6881      152803 : ///   ::= 'null' | TypeAndValue
    6882      152803 : bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
    6883             :   if (ParseToken(lltok::lbrace, "expected '{' here"))
    6884             :     return true;
    6885             : 
    6886             :   // Check for an empty list.
    6887       39925 :   if (EatIfPresent(lltok::rbrace))
    6888             :     return false;
    6889             : 
    6890       39925 :   do {
    6891       79850 :     // Null is a special case since it is typeless.
    6892       39925 :     if (EatIfPresent(lltok::kw_null)) {
    6893           0 :       Elts.push_back(nullptr);
    6894             :       continue;
    6895       51869 :     }
    6896           0 : 
    6897             :     Metadata *MD;
    6898       39925 :     if (ParseMetadata(MD, nullptr))
    6899       39925 :       return true;
    6900             :     Elts.push_back(MD);
    6901             :   } while (EatIfPresent(lltok::comma));
    6902             : 
    6903             :   return ParseToken(lltok::rbrace, "expected end of metadata node");
    6904             : }
    6905       70923 : 
    6906             : //===----------------------------------------------------------------------===//
    6907             : // Use-list order directives.
    6908             : //===----------------------------------------------------------------------===//
    6909             : bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
    6910             :                                 SMLoc Loc) {
    6911      141846 :   if (V->use_empty())
    6912       70923 :     return Error(Loc, "value has no uses");
    6913      212769 : 
    6914       70923 :   unsigned NumUses = 0;
    6915           0 :   SmallDenseMap<const Use *, unsigned, 16> Order;
    6916             :   for (const Use &U : V->uses()) {
    6917       70923 :     if (++NumUses > Indexes.size())
    6918       10938 :       break;
    6919           0 :     Order[&U] = Indexes[NumUses - 1];
    6920       10938 :   }
    6921             :   if (NumUses < 2)
    6922             :     return Error(Loc, "value only has one use");
    6923       77169 :   if (Order.size() != Indexes.size() || NumUses > Indexes.size())
    6924             :     return Error(Loc,
    6925           0 :                  "wrong number of indexes, expected " + Twine(V->getNumUses()));
    6926       59985 : 
    6927             :   V->sortUseList([&](const Use &L, const Use &R) {
    6928             :     return Order.lookup(&L) < Order.lookup(&R);
    6929             :   });
    6930             :   return false;
    6931             : }
    6932             : 
    6933             : /// ParseUseListOrderIndexes
    6934             : ///   ::= '{' uint32 (',' uint32)+ '}'
    6935             : bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
    6936             :   SMLoc Loc = Lex.getLoc();
    6937             :   if (ParseToken(lltok::lbrace, "expected '{' here"))
    6938      133784 :     return true;
    6939             :   if (Lex.getKind() == lltok::rbrace)
    6940             :     return Lex.Error("expected non-empty list of uselistorder indexes");
    6941             : 
    6942      133784 :   // Use Offset, Max, and IsOrdered to check consistency of indexes.  The
    6943      133784 :   // indexes should be distinct numbers in the range [0, size-1], and should
    6944      267568 :   // not be in order.
    6945             :   unsigned Offset = 0;
    6946           1 :   unsigned Max = 0;
    6947             :   bool IsOrdered = true;
    6948      133783 :   assert(Indexes.empty() && "Expected empty order vector");
    6949          10 :   do {
    6950          30 :     unsigned Index;
    6951          40 :     if (ParseUInt32(Index))
    6952          40 :       return true;
    6953             : 
    6954      133773 :     // Update consistency checks.
    6955      133773 :     Offset += Index - Indexes.size();
    6956             :     Max = std::max(Max, Index);
    6957             :     IsOrdered &= Index == Indexes.size();
    6958             : 
    6959             :     Indexes.push_back(Index);
    6960       28892 :   } while (EatIfPresent(lltok::comma));
    6961             : 
    6962             :   if (ParseToken(lltok::rbrace, "expected '}' here"))
    6963       28892 :     return true;
    6964       57784 : 
    6965       28892 :   if (Indexes.size() < 2)
    6966       86676 :     return Error(Loc, "expected >= 2 uselistorder indexes");
    6967             :   if (Offset != 0 || Max >= Indexes.size())
    6968           0 :     return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
    6969             :   if (IsOrdered)
    6970       28892 :     return Error(Loc, "expected uselistorder indexes to change the order");
    6971           1 : 
    6972             :   return false;
    6973       28891 : }
    6974       28891 : 
    6975             : /// ParseUseListOrder
    6976             : ///   ::= 'uselistorder' Type Value ',' UseListOrderIndexes
    6977             : bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
    6978             :   SMLoc Loc = Lex.getLoc();
    6979         217 :   if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
    6980             :     return true;
    6981         217 : 
    6982             :   Value *V;
    6983         217 :   SmallVector<unsigned, 16> Indexes;
    6984         434 :   if (ParseTypeAndValue(V, PFS) ||
    6985             :       ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
    6986           0 :       ParseUseListOrderIndexes(Indexes))
    6987             :     return true;
    6988         217 : 
    6989           0 :   return sortUseListOrder(V, Indexes, Loc);
    6990             : }
    6991         217 : 
    6992         217 : /// ParseUseListOrderBB
    6993             : ///   ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
    6994             : bool LLParser::ParseUseListOrderBB() {
    6995             :   assert(Lex.getKind() == lltok::kw_uselistorder_bb);
    6996             :   SMLoc Loc = Lex.getLoc();
    6997       30148 :   Lex.Lex();
    6998             : 
    6999             :   ValID Fn, Label;
    7000       30148 :   SmallVector<unsigned, 16> Indexes;
    7001       60296 :   if (ParseValID(Fn) ||
    7002             :       ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
    7003           0 :       ParseValID(Label) ||
    7004             :       ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
    7005       30148 :       ParseUseListOrderIndexes(Indexes))
    7006           0 :     return true;
    7007             : 
    7008       30148 :   // Check the function.
    7009       30148 :   GlobalValue *GV;
    7010             :   if (Fn.Kind == ValID::t_GlobalName)
    7011             :     GV = M->getNamedValue(Fn.StrVal);
    7012             :   else if (Fn.Kind == ValID::t_GlobalID)
    7013             :     GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
    7014       36306 :   else
    7015             :     return Error(Fn.Loc, "expected function name in uselistorder_bb");
    7016             :   if (!GV)
    7017       36306 :     return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
    7018       72612 :   auto *F = dyn_cast<Function>(GV);
    7019       36306 :   if (!F)
    7020      108918 :     return Error(Fn.Loc, "expected function name in uselistorder_bb");
    7021             :   if (F->isDeclaration())
    7022           0 :     return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
    7023             : 
    7024       36306 :   // Check the basic block.
    7025           0 :   if (Label.Kind == ValID::t_LocalID)
    7026             :     return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
    7027       36306 :   if (Label.Kind != ValID::t_LocalName)
    7028       36306 :     return Error(Label.Loc, "expected basic block name in uselistorder_bb");
    7029             :   Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
    7030             :   if (!V)
    7031             :     return Error(Label.Loc, "invalid basic block in uselistorder_bb");
    7032             :   if (!isa<BasicBlock>(V))
    7033       43507 :     return Error(Label.Loc, "expected basic block in uselistorder_bb");
    7034             : 
    7035             :   return sortUseListOrder(V, Indexes, Loc);
    7036       43507 : }
    7037       87014 : 
    7038       43507 : /// ModuleEntry
    7039      130521 : ///   ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
    7040             : /// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
    7041           0 : bool LLParser::ParseModuleEntry(unsigned ID) {
    7042             :   assert(Lex.getKind() == lltok::kw_module);
    7043       43507 :   Lex.Lex();
    7044           0 : 
    7045             :   std::string Path;
    7046       43507 :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7047       43507 :       ParseToken(lltok::lparen, "expected '(' here") ||
    7048             :       ParseToken(lltok::kw_path, "expected 'path' here") ||
    7049             :       ParseToken(lltok::colon, "expected ':' here") ||
    7050             :       ParseStringConstant(Path) ||
    7051             :       ParseToken(lltok::comma, "expected ',' here") ||
    7052       25622 :       ParseToken(lltok::kw_hash, "expected 'hash' here") ||
    7053       25622 :       ParseToken(lltok::colon, "expected ':' here") ||
    7054             :       ParseToken(lltok::lparen, "expected '(' here"))
    7055             :     return true;
    7056       25622 : 
    7057       51244 :   ModuleHash Hash;
    7058       51244 :   if (ParseUInt32(Hash[0]) || ParseToken(lltok::comma, "expected ',' here") ||
    7059       51244 :       ParseUInt32(Hash[1]) || ParseToken(lltok::comma, "expected ',' here") ||
    7060       76866 :       ParseUInt32(Hash[2]) || ParseToken(lltok::comma, "expected ',' here") ||
    7061       25622 :       ParseUInt32(Hash[3]) || ParseToken(lltok::comma, "expected ',' here") ||
    7062           0 :       ParseUInt32(Hash[4]))
    7063             :     return true;
    7064             : 
    7065             :   if (ParseToken(lltok::rparen, "expected ')' here") ||
    7066             :       ParseToken(lltok::rparen, "expected ')' here"))
    7067             :     return true;
    7068       53400 : 
    7069             :   auto ModuleEntry = Index->addModule(Path, ID, Hash);
    7070             :   ModuleIdMap[ID] = ModuleEntry->first();
    7071             : 
    7072             :   return false;
    7073       27843 : }
    7074             : 
    7075             : /// TypeIdEntry
    7076             : ///   ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
    7077             : bool LLParser::ParseTypeIdEntry(unsigned ID) {
    7078       55556 :   assert(Lex.getKind() == lltok::kw_typeid);
    7079       55556 :   Lex.Lex();
    7080       55556 : 
    7081       83334 :   std::string Name;
    7082       27778 :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7083           0 :       ParseToken(lltok::lparen, "expected '(' here") ||
    7084             :       ParseToken(lltok::kw_name, "expected 'name' here") ||
    7085             :       ParseToken(lltok::colon, "expected ':' here") ||
    7086       25622 :       ParseStringConstant(Name))
    7087           0 :     return true;
    7088             : 
    7089       25622 :   TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(Name);
    7090       79022 :   if (ParseToken(lltok::comma, "expected ',' here") ||
    7091      106800 :       ParseTypeIdSummary(TIS) || ParseToken(lltok::rparen, "expected ')' here"))
    7092       25622 :     return true;
    7093       25622 : 
    7094             :   // Check if this ID was forward referenced, and if so, update the
    7095             :   // corresponding GUIDs.
    7096             :   auto FwdRefTIDs = ForwardRefTypeIds.find(ID);
    7097             :   if (FwdRefTIDs != ForwardRefTypeIds.end()) {
    7098             :     for (auto TIDRef : FwdRefTIDs->second) {
    7099             :       assert(!*TIDRef.first &&
    7100             :              "Forward referenced type id GUID expected to be 0");
    7101             :       *TIDRef.first = GlobalValue::getGUID(Name);
    7102        1152 :     }
    7103        1152 :     ForwardRefTypeIds.erase(FwdRefTIDs);
    7104             :   }
    7105        1152 : 
    7106             :   return false;
    7107             : }
    7108        2304 : 
    7109             : /// TypeIdSummary
    7110             : ///   ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
    7111        1706 : bool LLParser::ParseTypeIdSummary(TypeIdSummary &TIS) {
    7112             :   if (ParseToken(lltok::kw_summary, "expected 'summary' here") ||
    7113             :       ParseToken(lltok::colon, "expected ':' here") ||
    7114             :       ParseToken(lltok::lparen, "expected '(' here") ||
    7115             :       ParseTypeTestResolution(TIS.TTRes))
    7116             :     return true;
    7117             : 
    7118           0 :   if (EatIfPresent(lltok::comma)) {
    7119             :     // Expect optional wpdResolutions field
    7120             :     if (ParseOptionalWpdResolutions(TIS.WPDRes))
    7121             :       return true;
    7122         555 :   }
    7123             : 
    7124             :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7125             :     return true;
    7126             : 
    7127         555 :   return false;
    7128         996 : }
    7129           0 : 
    7130             : /// TypeTestResolution
    7131         114 : ///   ::= 'typeTestRes' ':' '(' 'kind' ':'
    7132           1 : ///         ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
    7133             : ///         'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
    7134             : ///         [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
    7135         555 : ///         [',' 'inlinesBits' ':' UInt64]? ')'
    7136             : bool LLParser::ParseTypeTestResolution(TypeTestResolution &TTRes) {
    7137           1 :   if (ParseToken(lltok::kw_typeTestRes, "expected 'typeTestRes' here") ||
    7138         554 :       ParseToken(lltok::colon, "expected ':' here") ||
    7139             :       ParseToken(lltok::lparen, "expected '(' here") ||
    7140             :       ParseToken(lltok::kw_kind, "expected 'kind' here") ||
    7141        1151 :       ParseToken(lltok::colon, "expected ':' here"))
    7142        1151 :     return true;
    7143             : 
    7144             :   switch (Lex.getKind()) {
    7145             :   case lltok::kw_unsat:
    7146             :     TTRes.TheKind = TypeTestResolution::Unsat;
    7147             :     break;
    7148             :   case lltok::kw_byteArray:
    7149             :     TTRes.TheKind = TypeTestResolution::ByteArray;
    7150             :     break;
    7151             :   case lltok::kw_inline:
    7152             :     TTRes.TheKind = TypeTestResolution::Inline;
    7153             :     break;
    7154      154069 :   case lltok::kw_single:
    7155             :     TTRes.TheKind = TypeTestResolution::Single;
    7156             :     break;
    7157             :   case lltok::kw_allOnes:
    7158      154069 :     TTRes.TheKind = TypeTestResolution::AllOnes;
    7159             :     break;
    7160             :   default:
    7161      154069 :     return Error(Lex.getLoc(), "unexpected TypeTestResolution kind");
    7162             :   }
    7163      308138 :   Lex.Lex();
    7164             : 
    7165      154069 :   if (ParseToken(lltok::comma, "expected ',' here") ||
    7166      154069 :       ParseToken(lltok::kw_sizeM1BitWidth, "expected 'sizeM1BitWidth' here") ||
    7167             :       ParseToken(lltok::colon, "expected ':' here") ||
    7168      191447 :       ParseUInt32(TTRes.SizeM1BitWidth))
    7169       37378 :     return true;
    7170             : 
    7171             :   // Parse optional fields
    7172             :   while (EatIfPresent(lltok::comma)) {
    7173      154069 :     switch (Lex.getKind()) {
    7174             :     case lltok::kw_alignLog2:
    7175      462207 :       Lex.Lex();
    7176      308138 :       if (ParseToken(lltok::colon, "expected ':'") ||
    7177      154069 :           ParseUInt64(TTRes.AlignLog2))
    7178      308138 :         return true;
    7179      154069 :       break;
    7180      308130 :     case lltok::kw_sizeM1:
    7181      462191 :       Lex.Lex();
    7182      154061 :       if (ParseToken(lltok::colon, "expected ':'") || ParseUInt64(TTRes.SizeM1))
    7183           8 :         return true;
    7184             :       break;
    7185      154061 :     case lltok::kw_bitMask: {
    7186           0 :       unsigned Val;
    7187             :       Lex.Lex();
    7188             :       if (ParseToken(lltok::colon, "expected ':'") || ParseUInt32(Val))
    7189             :         return true;
    7190             :       assert(Val <= 0xff);
    7191             :       TTRes.BitMask = (uint8_t)Val;
    7192      154061 :       break;
    7193             :     }
    7194             :     case lltok::kw_inlineBits:
    7195             :       Lex.Lex();
    7196      434818 :       if (ParseToken(lltok::colon, "expected ':'") ||
    7197      570824 :           ParseUInt64(TTRes.InlineBits))
    7198             :         return true;
    7199      149406 :       break;
    7200           0 :     default:
    7201             :       return Error(Lex.getLoc(), "expected optional TypeTestResolution field");
    7202      149406 :     }
    7203             :   }
    7204             : 
    7205      154061 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7206             :     return true;
    7207             : 
    7208             :   return false;
    7209      154061 : }
    7210             : 
    7211             : /// OptionalWpdResolutions
    7212             : ///   ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
    7213             : /// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
    7214             : bool LLParser::ParseOptionalWpdResolutions(
    7215             :     std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
    7216             :   if (ParseToken(lltok::kw_wpdResolutions, "expected 'wpdResolutions' here") ||
    7217             :       ParseToken(lltok::colon, "expected ':' here") ||
    7218             :       ParseToken(lltok::lparen, "expected '(' here"))
    7219             :     return true;
    7220      154052 : 
    7221      154052 :   do {
    7222      450203 :     uint64_t Offset;
    7223             :     WholeProgramDevirtResolution WPDRes;
    7224      296151 :     if (ParseToken(lltok::lparen, "expected '(' here") ||
    7225      290004 :         ParseToken(lltok::kw_offset, "expected 'offset' here") ||
    7226        6147 :         ParseToken(lltok::colon, "expected ':' here") || ParseUInt64(Offset) ||
    7227           0 :         ParseToken(lltok::comma, "expected ',' here") || ParseWpdRes(WPDRes) ||
    7228             :         ParseToken(lltok::rparen, "expected ')' here"))
    7229             :       return true;
    7230      290004 :     WPDResMap[Offset] = WPDRes;
    7231           0 :   } while (EatIfPresent(lltok::comma));
    7232           0 : 
    7233      592302 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7234      296151 :     return true;
    7235             : 
    7236             :   return false;
    7237      154052 : }
    7238           0 : 
    7239             : /// WpdRes
    7240      154052 : ///   ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
    7241           1 : ///         [',' OptionalResByArg]? ')'
    7242             : ///   ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
    7243             : ///         ',' 'singleImplName' ':' STRINGCONSTANT ','
    7244             : ///         [',' OptionalResByArg]? ')'
    7245             : ///   ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
    7246      154051 : ///         [',' OptionalResByArg]? ')'
    7247             : bool LLParser::ParseWpdRes(WholeProgramDevirtResolution &WPDRes) {
    7248      154051 :   if (ParseToken(lltok::kw_wpdRes, "expected 'wpdRes' here") ||
    7249             :       ParseToken(lltok::colon, "expected ':' here") ||
    7250      154051 :       ParseToken(lltok::lparen, "expected '(' here") ||
    7251      154051 :       ParseToken(lltok::kw_kind, "expected 'kind' here") ||
    7252        1205 :       ParseToken(lltok::colon, "expected ':' here"))
    7253             :     return true;
    7254      154051 : 
    7255      154051 :   switch (Lex.getKind()) {
    7256      154051 :   case lltok::kw_indir:
    7257             :     WPDRes.TheKind = WholeProgramDevirtResolution::Indir;
    7258             :     break;
    7259             :   case lltok::kw_singleImpl:
    7260             :     WPDRes.TheKind = WholeProgramDevirtResolution::SingleImpl;
    7261             :     break;
    7262             :   case lltok::kw_branchFunnel:
    7263             :     WPDRes.TheKind = WholeProgramDevirtResolution::BranchFunnel;
    7264             :     break;
    7265             :   default:
    7266       49148 :     return Error(Lex.getLoc(), "unexpected WholeProgramDevirtResolution kind");
    7267       49148 :   }
    7268       49148 :   Lex.Lex();
    7269       49148 : 
    7270       49148 :   // Parse optional fields
    7271       49148 :   while (EatIfPresent(lltok::comma)) {
    7272             :     switch (Lex.getKind()) {
    7273             :     case lltok::kw_singleImplName:
    7274             :       Lex.Lex();
    7275             :       if (ParseToken(lltok::colon, "expected ':' here") ||
    7276       49148 :           ParseStringConstant(WPDRes.SingleImplName))
    7277             :         return true;
    7278       98296 :       break;
    7279           4 :     case lltok::kw_resByArg:
    7280             :       if (ParseOptionalResByArg(WPDRes.ResByArg))
    7281       49144 :         return true;
    7282             :       break;
    7283       46018 :     default:
    7284       44484 :       return Error(Lex.getLoc(),
    7285             :                    "expected optional WholeProgramDevirtResolution field");
    7286       44483 :     }
    7287             :   }
    7288        1534 : 
    7289         419 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7290         419 :     return true;
    7291             : 
    7292        1115 :   return false;
    7293           0 : }
    7294             : 
    7295        1115 : /// OptionalResByArg
    7296             : ///   ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
    7297             : /// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
    7298         397 : ///                ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
    7299         385 : ///                  'virtualConstProp' )
    7300             : ///                [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
    7301         385 : ///                [',' 'bit' ':' UInt32]? ')'
    7302             : bool LLParser::ParseOptionalResByArg(
    7303          12 :     std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
    7304           8 :         &ResByArg) {
    7305           8 :   if (ParseToken(lltok::kw_resByArg, "expected 'resByArg' here") ||
    7306             :       ParseToken(lltok::colon, "expected ':' here") ||
    7307           4 :       ParseToken(lltok::lparen, "expected '(' here"))
    7308           4 :     return true;
    7309             : 
    7310             :   do {
    7311             :     std::vector<uint64_t> Args;
    7312             :     if (ParseArgs(Args) || ParseToken(lltok::comma, "expected ',' here") ||
    7313             :         ParseToken(lltok::kw_byArg, "expected 'byArg here") ||
    7314       49143 :         ParseToken(lltok::colon, "expected ':' here") ||
    7315           0 :         ParseToken(lltok::lparen, "expected '(' here") ||
    7316             :         ParseToken(lltok::kw_kind, "expected 'kind' here") ||
    7317       49143 :         ParseToken(lltok::colon, "expected ':' here"))
    7318             :       return true;
    7319             : 
    7320       49143 :     WholeProgramDevirtResolution::ByArg ByArg;
    7321       49143 :     switch (Lex.getKind()) {
    7322             :     case lltok::kw_indir:
    7323             :       ByArg.TheKind = WholeProgramDevirtResolution::ByArg::Indir;
    7324             :       break;
    7325             :     case lltok::kw_uniformRetVal:
    7326             :       ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal;
    7327             :       break;
    7328      166125 :     case lltok::kw_uniqueRetVal:
    7329             :       ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal;
    7330      166125 :       break;
    7331      166125 :     case lltok::kw_virtualConstProp:
    7332             :       ByArg.TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp;
    7333      166125 :       break;
    7334      166125 :     default:
    7335             :       return Error(Lex.getLoc(),
    7336      166125 :                    "unexpected WholeProgramDevirtResolution::ByArg kind");
    7337             :     }
    7338        1182 :     Lex.Lex();
    7339             : 
    7340             :     // Parse optional fields
    7341             :     while (EatIfPresent(lltok::comma)) {
    7342      166125 :       switch (Lex.getKind()) {
    7343             :       case lltok::kw_info:
    7344       10234 :         Lex.Lex();
    7345             :         if (ParseToken(lltok::colon, "expected ':' here") ||
    7346             :             ParseUInt64(ByArg.Info))
    7347             :           return true;
    7348      166125 :         break;
    7349      166125 :       case lltok::kw_byte:
    7350      332249 :         Lex.Lex();
    7351      166124 :         if (ParseToken(lltok::colon, "expected ':' here") ||
    7352      498373 :             ParseUInt32(ByArg.Byte))
    7353      166124 :           return true;
    7354           2 :         break;
    7355             :       case lltok::kw_bit:
    7356      332246 :         Lex.Lex();
    7357           0 :         if (ParseToken(lltok::colon, "expected ':' here") ||
    7358      166123 :             ParseUInt32(ByArg.Bit))
    7359           0 :           return true;
    7360      166123 :         break;
    7361             :       default:
    7362           0 :         return Error(Lex.getLoc(),
    7363             :                      "expected optional whole program devirt field");
    7364      166123 :       }
    7365           2 :     }
    7366           1 : 
    7367             :     if (ParseToken(lltok::rparen, "expected ')' here"))
    7368      166122 :       return true;
    7369      166122 : 
    7370             :     ResByArg[Args] = ByArg;
    7371             :   } while (EatIfPresent(lltok::comma));
    7372             : 
    7373             :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7374             :     return true;
    7375             : 
    7376             :   return false;
    7377      127584 : }
    7378             : 
    7379      127584 : /// OptionalResByArg
    7380      127584 : ///   ::= 'args' ':' '(' UInt64[, UInt64]* ')'
    7381             : bool LLParser::ParseArgs(std::vector<uint64_t> &Args) {
    7382      127584 :   if (ParseToken(lltok::kw_args, "expected 'args' here") ||
    7383      127584 :       ParseToken(lltok::colon, "expected ':' here") ||
    7384             :       ParseToken(lltok::lparen, "expected '(' here"))
    7385      127584 :     return true;
    7386             : 
    7387        1162 :   do {
    7388             :     uint64_t Val;
    7389             :     if (ParseUInt64(Val))
    7390             :       return true;
    7391      127584 :     Args.push_back(Val);
    7392             :   } while (EatIfPresent(lltok::comma));
    7393       11016 : 
    7394             :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7395             :     return true;
    7396      127583 : 
    7397      255166 :   return false;
    7398      127583 : }
    7399      382750 : 
    7400      127583 : static ValueInfo EmptyVI =
    7401           2 :     ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
    7402             : 
    7403      255164 : /// Stores the given Name/GUID and associated summary into the Index.
    7404           0 : /// Also updates any forward references to the associated entry ID.
    7405      127582 : void LLParser::AddGlobalValueToIndex(
    7406           0 :     std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
    7407      127582 :     unsigned ID, std::unique_ptr<GlobalValueSummary> Summary) {
    7408           0 :   // First create the ValueInfo utilizing the Name or GUID.
    7409      127582 :   ValueInfo VI;
    7410           0 :   if (GUID != 0) {
    7411      127582 :     assert(Name.empty());
    7412             :     VI = Index->getOrInsertValueInfo(GUID);
    7413           0 :   } else {
    7414             :     assert(!Name.empty());
    7415      127582 :     if (M) {
    7416      127582 :       auto *GV = M->getNamedValue(Name);
    7417             :       assert(GV);
    7418             :       VI = Index->getOrInsertValueInfo(GV);
    7419             :     } else {
    7420             :       assert(
    7421             :           (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
    7422        1217 :           "Need a source_filename to compute GUID for local");
    7423             :       GUID = GlobalValue::getGUID(
    7424             :           GlobalValue::getGlobalIdentifier(Name, Linkage, SourceFileName));
    7425        1217 :       VI = Index->getOrInsertValueInfo(GUID, Index->saveString(Name));
    7426        1217 :     }
    7427        1217 :   }
    7428             : 
    7429             :   // Add the summary if one was provided.
    7430             :   if (Summary)
    7431             :     Index->addGlobalValueSummary(VI, std::move(Summary));
    7432             : 
    7433             :   // Resolve forward references from calls/refs
    7434             :   auto FwdRefVIs = ForwardRefValueInfos.find(ID);
    7435             :   if (FwdRefVIs != ForwardRefValueInfos.end()) {
    7436             :     for (auto VIRef : FwdRefVIs->second) {
    7437        1217 :       assert(*VIRef.first == EmptyVI &&
    7438        2434 :              "Forward referenced ValueInfo expected to be empty");
    7439        1217 :       *VIRef.first = VI;
    7440        2434 :     }
    7441        1217 :     ForwardRefValueInfos.erase(FwdRefVIs);
    7442        3651 :   }
    7443        1217 : 
    7444           0 :   // Resolve forward references from aliases
    7445             :   auto FwdRefAliasees = ForwardRefAliasees.find(ID);
    7446        1217 :   if (FwdRefAliasees != ForwardRefAliasees.end()) {
    7447        1217 :     for (auto AliaseeRef : FwdRefAliasees->second) {
    7448           0 :       assert(!AliaseeRef.first->hasAliasee() &&
    7449        1217 :              "Forward referencing alias already has aliasee");
    7450           0 :       AliaseeRef.first->setAliasee(VI.getSummaryList().front().get());
    7451           0 :     }
    7452        1217 :     ForwardRefAliasees.erase(FwdRefAliasees);
    7453             :   }
    7454           0 : 
    7455           0 :   // Save the associated ValueInfo for use in later references by ID.
    7456        2434 :   if (ID == NumberedValueInfos.size())
    7457           0 :     NumberedValueInfos.push_back(VI);
    7458        1217 :   else {
    7459           0 :     // Handle non-continuous numbers (to make test simplification easier).
    7460        1217 :     if (ID > NumberedValueInfos.size())
    7461           0 :       NumberedValueInfos.resize(ID + 1);
    7462             :     NumberedValueInfos[ID] = VI;
    7463           0 :   }
    7464             : }
    7465        1217 : 
    7466             : /// ParseGVEntry
    7467             : ///   ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
    7468        1217 : ///         [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
    7469             : /// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
    7470             : bool LLParser::ParseGVEntry(unsigned ID) {
    7471             :   assert(Lex.getKind() == lltok::kw_gv);
    7472             :   Lex.Lex();
    7473             : 
    7474             :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7475        6433 :       ParseToken(lltok::lparen, "expected '(' here"))
    7476             :     return true;
    7477             : 
    7478        6433 :   std::string Name;
    7479        6433 :   GlobalValue::GUID GUID = 0;
    7480             :   switch (Lex.getKind()) {
    7481             :   case lltok::kw_name:
    7482             :     Lex.Lex();
    7483             :     if (ParseToken(lltok::colon, "expected ':' here") ||
    7484             :         ParseStringConstant(Name))
    7485             :       return true;
    7486        6433 :     // Can't create GUID/ValueInfo until we have the linkage.
    7487           0 :     break;
    7488             :   case lltok::kw_guid:
    7489        1057 :     Lex.Lex();
    7490         835 :     if (ParseToken(lltok::colon, "expected ':' here") || ParseUInt64(GUID))
    7491         671 :       return true;
    7492         323 :     break;
    7493         640 :   default:
    7494         621 :     return Error(Lex.getLoc(), "expected name or guid tag");
    7495         412 :   }
    7496         431 : 
    7497         423 :   if (!EatIfPresent(lltok::comma)) {
    7498         424 :     // No summaries. Wrap up.
    7499             :     if (ParseToken(lltok::rparen, "expected ')' here"))
    7500        6433 :       return true;
    7501             :     // This was created for a call to an external or indirect target.
    7502        6433 :     // A GUID with no summary came from a VALUE_GUID record, dummy GUID
    7503       12866 :     // created for indirect calls with VP. A Name with no GUID came from
    7504       12866 :     // an external definition. We pass ExternalLinkage since that is only
    7505        6433 :     // used when the GUID must be computed from Name, and in that case
    7506           0 :     // the symbol must have external linkage.
    7507             :     AddGlobalValueToIndex(Name, GUID, GlobalValue::ExternalLinkage, ID,
    7508        6433 :                           nullptr);
    7509           0 :     return false;
    7510       12866 :   }
    7511           0 : 
    7512        6433 :   // Have a list of summaries
    7513           0 :   if (ParseToken(lltok::kw_summaries, "expected 'summaries' here") ||
    7514             :       ParseToken(lltok::colon, "expected ':' here"))
    7515        6433 :     return true;
    7516           2 : 
    7517           1 :   do {
    7518           1 :     if (ParseToken(lltok::lparen, "expected '(' here"))
    7519             :       return true;
    7520             :     switch (Lex.getKind()) {
    7521        6432 :     case lltok::kw_function:
    7522        6432 :       if (ParseFunctionSummary(Name, GUID, ID))
    7523           0 :         return true;
    7524           0 :       break;
    7525             :     case lltok::kw_variable:
    7526             :       if (ParseVariableSummary(Name, GUID, ID))
    7527        6432 :         return true;
    7528             :       break;
    7529        6432 :     case lltok::kw_alias:
    7530             :       if (ParseAliasSummary(Name, GUID, ID))
    7531             :         return true;
    7532             :       break;
    7533             :     default:
    7534             :       return Error(Lex.getLoc(), "expected summary type");
    7535         698 :     }
    7536         698 :     if (ParseToken(lltok::rparen, "expected ')' here"))
    7537         698 :       return true;
    7538         698 :   } while (EatIfPresent(lltok::comma));
    7539             : 
    7540             :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7541         698 :     return true;
    7542           0 : 
    7543         698 :   return false;
    7544           0 : }
    7545             : 
    7546         698 : /// FunctionSummary
    7547         698 : ///   ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
    7548             : ///         ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
    7549             : ///         [',' OptionalTypeIdInfo]? [',' OptionalRefs]? ')'
    7550             : bool LLParser::ParseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
    7551             :                                     unsigned ID) {
    7552       91967 :   assert(Lex.getKind() == lltok::kw_function);
    7553       91967 :   Lex.Lex();
    7554       91967 : 
    7555             :   StringRef ModulePath;
    7556             :   GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
    7557             :       /*Linkage=*/GlobalValue::ExternalLinkage, /*NotEligibleToImport=*/false,
    7558             :       /*Live=*/false, /*IsLocal=*/false);
    7559       91967 :   unsigned InstCount;
    7560       91967 :   std::vector<FunctionSummary::EdgeTy> Calls;
    7561       91967 :   FunctionSummary::TypeIdInfo TypeIdInfo;
    7562      183933 :   std::vector<ValueInfo> Refs;
    7563             :   // Default is all-zeros (conservative values).
    7564           2 :   FunctionSummary::FFlags FFlags = {};
    7565             :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7566       91965 :       ParseToken(lltok::lparen, "expected '(' here") ||
    7567             :       ParseModuleReference(ModulePath) ||
    7568             :       ParseToken(lltok::comma, "expected ',' here") || ParseGVFlags(GVFlags) ||
    7569           1 :       ParseToken(lltok::comma, "expected ',' here") ||
    7570             :       ParseToken(lltok::kw_insts, "expected 'insts' here") ||
    7571       91964 :       ParseToken(lltok::colon, "expected ':' here") || ParseUInt32(InstCount))
    7572           2 :     return true;
    7573           1 : 
    7574             :   // Parse optional fields
    7575             :   while (EatIfPresent(lltok::comma)) {
    7576             :     switch (Lex.getKind()) {
    7577             :     case lltok::kw_funcFlags:
    7578             :       if (ParseOptionalFFlags(FFlags))
    7579       91963 :         return true;
    7580             :       break;
    7581             :     case lltok::kw_calls:
    7582      118880 :       if (ParseOptionalCalls(Calls))
    7583      119896 :         return true;
    7584             :       break;
    7585             :     case lltok::kw_typeIdInfo:
    7586             :       if (ParseOptionalTypeIdInfo(TypeIdInfo))
    7587      118881 :         return true;
    7588      119382 :       break;
    7589           0 :     case lltok::kw_refs:
    7590             :       if (ParseOptionalRefs(Refs))
    7591      118881 :         return true;
    7592             :       break;
    7593         501 :     default:
    7594           2 :       return Error(Lex.getLoc(), "expected optional function summary field");
    7595           1 :     }
    7596             :   }
    7597             : 
    7598      118880 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7599             :     return true;
    7600             : 
    7601             :   auto FS = llvm::make_unique<FunctionSummary>(
    7602       91962 :       GVFlags, InstCount, FFlags, std::move(Refs), std::move(Calls),
    7603           1 :       std::move(TypeIdInfo.TypeTests),
    7604             :       std::move(TypeIdInfo.TypeTestAssumeVCalls),
    7605       91961 :       std::move(TypeIdInfo.TypeCheckedLoadVCalls),
    7606           3 :       std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
    7607       91958 :       std::move(TypeIdInfo.TypeCheckedLoadConstVCalls));
    7608       91958 : 
    7609       68886 :   FS->setModulePath(ModulePath);
    7610       91958 : 
    7611             :   AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
    7612             :                         ID, std::move(FS));
    7613             : 
    7614             :   return false;
    7615        6605 : }
    7616             : 
    7617             : /// VariableSummary
    7618             : ///   ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
    7619       13210 : ///         [',' OptionalRefs]? ')'
    7620        6605 : bool LLParser::ParseVariableSummary(std::string Name, GlobalValue::GUID GUID,
    7621           1 :                                     unsigned ID) {
    7622             :   assert(Lex.getKind() == lltok::kw_variable);
    7623        6604 :   Lex.Lex();
    7624           0 : 
    7625             :   StringRef ModulePath;
    7626        6604 :   GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
    7627           1 :       /*Linkage=*/GlobalValue::ExternalLinkage, /*NotEligibleToImport=*/false,
    7628        6603 :       /*Live=*/false, /*IsLocal=*/false);
    7629        6603 :   std::vector<ValueInfo> Refs;
    7630             :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7631             :       ParseToken(lltok::lparen, "expected '(' here") ||
    7632             :       ParseModuleReference(ModulePath) ||
    7633             :       ParseToken(lltok::comma, "expected ',' here") || ParseGVFlags(GVFlags))
    7634        1430 :     return true;
    7635             : 
    7636             :   // Parse optional refs field
    7637             :   if (EatIfPresent(lltok::comma)) {
    7638        1430 :     if (ParseOptionalRefs(Refs))
    7639        2860 :       return true;
    7640        2860 :   }
    7641        1430 : 
    7642           0 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7643             :     return true;
    7644        1430 : 
    7645           0 :   auto GS = llvm::make_unique<GlobalVarSummary>(GVFlags, std::move(Refs));
    7646             : 
    7647        1430 :   GS->setModulePath(ModulePath);
    7648        1430 : 
    7649           1 :   AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
    7650        1429 :                         ID, std::move(GS));
    7651           2 : 
    7652           4 :   return false;
    7653           5 : }
    7654        1428 : 
    7655        1428 : /// AliasSummary
    7656             : ///   ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
    7657             : ///         'aliasee' ':' GVReference ')'
    7658             : bool LLParser::ParseAliasSummary(std::string Name, GlobalValue::GUID GUID,
    7659             :                                  unsigned ID) {
    7660             :   assert(Lex.getKind() == lltok::kw_alias);
    7661             :   LocTy Loc = Lex.getLoc();
    7662             :   Lex.Lex();
    7663             : 
    7664             :   StringRef ModulePath;
    7665             :   GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
    7666       20914 :       /*Linkage=*/GlobalValue::ExternalLinkage, /*NotEligibleToImport=*/false,
    7667       20914 :       /*Live=*/false, /*IsLocal=*/false);
    7668             :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7669             :       ParseToken(lltok::lparen, "expected '(' here") ||
    7670             :       ParseModuleReference(ModulePath) ||
    7671             :       ParseToken(lltok::comma, "expected ',' here") || ParseGVFlags(GVFlags) ||
    7672        2034 :       ParseToken(lltok::comma, "expected ',' here") ||
    7673             :       ParseToken(lltok::kw_aliasee, "expected 'aliasee' here") ||
    7674             :       ParseToken(lltok::colon, "expected ':' here"))
    7675             :     return true;
    7676             : 
    7677         978 :   ValueInfo AliaseeVI;
    7678         978 :   unsigned GVId;
    7679             :   if (ParseGVReference(AliaseeVI, GVId))
    7680             :     return true;
    7681             : 
    7682       54583 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    7683           7 :     return true;
    7684       54576 : 
    7685             :   auto AS = llvm::make_unique<AliasSummary>(GVFlags);
    7686             : 
    7687       18872 :   AS->setModulePath(ModulePath);
    7688             : 
    7689             :   // Record forward reference if the aliasee is not parsed yet.
    7690             :   if (AliaseeVI == EmptyVI) {
    7691             :     auto FwdRef = ForwardRefAliasees.insert(
    7692             :         std::make_pair(GVId, std::vector<std::pair<AliasSummary *, LocTy>>()));
    7693         885 :     FwdRef.first->second.push_back(std::make_pair(AS.get(), Loc));
    7694             :   } else
    7695         885 :     AS->setAliasee(AliaseeVI.getSummaryList().front().get());
    7696           4 : 
    7697             :   AddGlobalValueToIndex(Name, GUID, (GlobalValue::LinkageTypes)GVFlags.Linkage,
    7698             :                         ID, std::move(AS));
    7699             : 
    7700        4110 :   return false;
    7701        3230 : }
    7702             : 
    7703        6458 : /// Flag
    7704             : ///   ::= [0|1]
    7705         881 : bool LLParser::ParseFlag(unsigned &Val) {
    7706           1 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    7707         880 :     return TokError("expected integer");
    7708           3 :   Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
    7709           6 :   Lex.Lex();
    7710             :   return false;
    7711         877 : }
    7712             : 
    7713             : /// OptionalFFlags
    7714         877 : ///   := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
    7715             : ///        [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
    7716             : ///        [',' 'returnDoesNotAlias' ':' Flag]? ')'
    7717             : bool LLParser::ParseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
    7718             :   assert(Lex.getKind() == lltok::kw_funcFlags);
    7719         894 :   Lex.Lex();
    7720         894 : 
    7721         894 :   if (ParseToken(lltok::colon, "expected ':' in funcFlags") |
    7722             :       ParseToken(lltok::lparen, "expected '(' in funcFlags"))
    7723         894 :     return true;
    7724           0 : 
    7725             :   do {
    7726             :     unsigned Val;
    7727             :     switch (Lex.getKind()) {
    7728             :     case lltok::kw_readNone:
    7729             :       Lex.Lex();
    7730         894 :       if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
    7731             :         return true;
    7732             :       FFlags.ReadNone = Val;
    7733             :       break;
    7734             :     case lltok::kw_readOnly:
    7735        3261 :       Lex.Lex();
    7736           0 :       if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
    7737             :         return true;
    7738             :       FFlags.ReadOnly = Val;
    7739        3261 :       break;
    7740        3261 :     case lltok::kw_noRecurse:
    7741        3261 :       Lex.Lex();
    7742             :       if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
    7743        3261 :         return true;
    7744             :       FFlags.NoRecurse = Val;
    7745             :       break;
    7746         894 :     case lltok::kw_returnDoesNotAlias:
    7747             :       Lex.Lex();
    7748             :       if (ParseToken(lltok::colon, "expected ':'") || ParseFlag(Val))
    7749         894 :         return true;
    7750           0 :       FFlags.ReturnDoesNotAlias = Val;
    7751         894 :       break;
    7752           2 :     default:
    7753         892 :       return Error(Lex.getLoc(), "expected function flag type");
    7754           1 :     }
    7755             :   } while (EatIfPresent(lltok::comma));
    7756             : 
    7757             :   if (ParseToken(lltok::rparen, "expected ')' in funcFlags"))
    7758             :     return true;
    7759             : 
    7760             :   return false;
    7761         884 : }
    7762         884 : 
    7763         884 : /// OptionalCalls
    7764             : ///   := 'calls' ':' '(' Call [',' Call]* ')'
    7765             : /// Call ::= '(' 'callee' ':' GVReference
    7766             : ///            [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]? ')'
    7767             : bool LLParser::ParseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) {
    7768        1765 :   assert(Lex.getKind() == lltok::kw_calls);
    7769        1765 :   Lex.Lex();
    7770         882 : 
    7771           4 :   if (ParseToken(lltok::colon, "expected ':' in calls") |
    7772             :       ParseToken(lltok::lparen, "expected '(' in calls"))
    7773         879 :     return true;
    7774             : 
    7775             :   IdToIndexMapType IdToIndexMap;
    7776             :   // Parse each call edge
    7777             :   do {
    7778          12 :     ValueInfo VI;
    7779             :     if (ParseToken(lltok::lparen, "expected '(' in call") ||
    7780          12 :         ParseToken(lltok::kw_callee, "expected 'callee' in call") ||
    7781          12 :         ParseToken(lltok::colon, "expected ':'"))
    7782             :       return true;
    7783          24 : 
    7784             :     LocTy Loc = Lex.getLoc();
    7785          24 :     unsigned GVId;
    7786          24 :     if (ParseGVReference(VI, GVId))
    7787          24 :       return true;
    7788          36 : 
    7789          12 :     CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
    7790           0 :     unsigned RelBF = 0;
    7791             :     if (EatIfPresent(lltok::comma)) {
    7792             :       // Expect either hotness or relbf
    7793             :       if (EatIfPresent(lltok::kw_hotness)) {
    7794          12 :         if (ParseToken(lltok::colon, "expected ':'") || ParseHotness(Hotness))
    7795          24 :           return true;
    7796           0 :       } else {
    7797           0 :         if (ParseToken(lltok::kw_relbf, "expected relbf") ||
    7798             :             ParseToken(lltok::colon, "expected ':'") || ParseUInt32(RelBF))
    7799           0 :           return true;
    7800          12 :       }
    7801           1 :     }
    7802             :     // Keep track of the Call array index needing a forward reference.
    7803             :     // We will save the location of the ValueInfo needing an update, but
    7804           1 :     // can only do so once the std::vector is finalized.
    7805          10 :     if (VI == EmptyVI)
    7806           1 :       IdToIndexMap[GVId].push_back(std::make_pair(Calls.size(), Loc));
    7807             :     Calls.push_back(FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, RelBF)});
    7808             : 
    7809           9 :     if (ParseToken(lltok::rparen, "expected ')' in call"))
    7810           1 :       return true;
    7811           8 :   } while (EatIfPresent(lltok::comma));
    7812           0 : 
    7813           8 :   // Now that the Calls vector is finalized, it is safe to save the locations
    7814           8 :   // of any forward GV references that need updating later.
    7815           1 :   for (auto I : IdToIndexMap) {
    7816           7 :     for (auto P : I.second) {
    7817           1 :       assert(Calls[P.first].first == EmptyVI &&
    7818             :              "Forward referenced ValueInfo expected to be empty");
    7819           6 :       auto FwdRef = ForwardRefValueInfos.insert(std::make_pair(
    7820             :           I.first, std::vector<std::pair<ValueInfo *, LocTy>>()));
    7821             :       FwdRef.first->second.push_back(
    7822             :           std::make_pair(&Calls[P.first].first, P.second));
    7823             :     }
    7824             :   }
    7825          21 : 
    7826             :   if (ParseToken(lltok::rparen, "expected ')' in calls"))
    7827          21 :     return true;
    7828             : 
    7829             :   return false;
    7830          42 : }
    7831          42 : 
    7832          42 : /// Hotness
    7833          42 : ///   := ('unknown'|'cold'|'none'|'hot'|'critical')
    7834          42 : bool LLParser::ParseHotness(CalleeInfo::HotnessType &Hotness) {
    7835          42 :   switch (Lex.getKind()) {
    7836          42 :   case lltok::kw_unknown:
    7837          63 :     Hotness = CalleeInfo::HotnessType::Unknown;
    7838          21 :     break;
    7839           0 :   case lltok::kw_cold:
    7840             :     Hotness = CalleeInfo::HotnessType::Cold;
    7841             :     break;
    7842          63 :   case lltok::kw_none:
    7843          63 :     Hotness = CalleeInfo::HotnessType::None;
    7844          63 :     break;
    7845          84 :   case lltok::kw_hot:
    7846          21 :     Hotness = CalleeInfo::HotnessType::Hot;
    7847           0 :     break;
    7848             :   case lltok::kw_critical:
    7849          42 :     Hotness = CalleeInfo::HotnessType::Critical;
    7850          21 :     break;
    7851           0 :   default:
    7852             :     return Error(Lex.getLoc(), "invalid call edge hotness");
    7853          42 :   }
    7854          21 :   Lex.Lex();
    7855             :   return false;
    7856          21 : }
    7857             : 
    7858             : /// OptionalRefs
    7859             : ///   := 'refs' ':' '(' GVReference [',' GVReference]* ')'
    7860             : bool LLParser::ParseOptionalRefs(std::vector<ValueInfo> &Refs) {
    7861           7 :   assert(Lex.getKind() == lltok::kw_refs);
    7862             :   Lex.Lex();
    7863           7 : 
    7864             :   if (ParseToken(lltok::colon, "expected ':' in refs") |
    7865             :       ParseToken(lltok::lparen, "expected '(' in refs"))
    7866          14 :     return true;
    7867          14 : 
    7868          14 :   IdToIndexMapType IdToIndexMap;
    7869          21 :   // Parse each ref edge
    7870           7 :   do {
    7871           0 :     ValueInfo VI;
    7872             :     LocTy Loc = Lex.getLoc();
    7873          14 :     unsigned GVId;
    7874          14 :     if (ParseGVReference(VI, GVId))
    7875          14 :       return true;
    7876           0 : 
    7877             :     // Keep track of the Refs array index needing a forward reference.
    7878             :     // We will save the location of the ValueInfo needing an update, but
    7879             :     // can only do so once the std::vector is finalized.
    7880             :     if (VI == EmptyVI)
    7881           7 :       IdToIndexMap[GVId].push_back(std::make_pair(Refs.size(), Loc));
    7882          18 :     Refs.push_back(VI);
    7883             :   } while (EatIfPresent(lltok::comma));
    7884             : 
    7885          11 :   // Now that the Refs vector is finalized, it is safe to save the locations
    7886             :   // of any forward GV references that need updating later.
    7887             :   for (auto I : IdToIndexMap) {
    7888             :     for (auto P : I.second) {
    7889             :       assert(Refs[P.first] == EmptyVI &&
    7890             :              "Forward referenced ValueInfo expected to be empty");
    7891             :       auto FwdRef = ForwardRefValueInfos.insert(std::make_pair(
    7892             :           I.first, std::vector<std::pair<ValueInfo *, LocTy>>()));
    7893             :       FwdRef.first->second.push_back(std::make_pair(&Refs[P.first], P.second));
    7894             :     }
    7895           7 :   }
    7896          14 : 
    7897          14 :   if (ParseToken(lltok::rparen, "expected ')' in refs"))
    7898          21 :     return true;
    7899           7 : 
    7900           0 :   return false;
    7901             : }
    7902             : 
    7903             : /// OptionalTypeIdInfo
    7904           2 : ///   := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
    7905             : ///         [',' TypeCheckedLoadVCalls]?  [',' TypeTestAssumeConstVCalls]?
    7906             : ///         [',' TypeCheckedLoadConstVCalls]? ')'
    7907             : bool LLParser::ParseOptionalTypeIdInfo(
    7908           7 :     FunctionSummary::TypeIdInfo &TypeIdInfo) {
    7909           0 :   assert(Lex.getKind() == lltok::kw_typeIdInfo);
    7910             :   Lex.Lex();
    7911             : 
    7912             :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7913             :       ParseToken(lltok::lparen, "expected '(' in typeIdInfo"))
    7914             :     return true;
    7915             : 
    7916             :   do {
    7917             :     switch (Lex.getKind()) {
    7918             :     case lltok::kw_typeTests:
    7919             :       if (ParseTypeTests(TypeIdInfo.TypeTests))
    7920           7 :         return true;
    7921          14 :       break;
    7922          14 :     case lltok::kw_typeTestAssumeVCalls:
    7923          14 :       if (ParseVFuncIdList(lltok::kw_typeTestAssumeVCalls,
    7924          21 :                            TypeIdInfo.TypeTestAssumeVCalls))
    7925           7 :         return true;
    7926           0 :       break;
    7927             :     case lltok::kw_typeCheckedLoadVCalls:
    7928           7 :       if (ParseVFuncIdList(lltok::kw_typeCheckedLoadVCalls,
    7929           1 :                            TypeIdInfo.TypeCheckedLoadVCalls))
    7930           1 :         return true;
    7931           1 :       break;
    7932           1 :     case lltok::kw_typeTestAssumeConstVCalls:
    7933           1 :       if (ParseConstVCallList(lltok::kw_typeTestAssumeConstVCalls,
    7934           1 :                               TypeIdInfo.TypeTestAssumeConstVCalls))
    7935           1 :         return true;
    7936           1 :       break;
    7937           1 :     case lltok::kw_typeCheckedLoadConstVCalls:
    7938           2 :       if (ParseConstVCallList(lltok::kw_typeCheckedLoadConstVCalls,
    7939           2 :                               TypeIdInfo.TypeCheckedLoadConstVCalls))
    7940           2 :         return true;
    7941           2 :       break;
    7942           2 :     default:
    7943           2 :       return Error(Lex.getLoc(), "invalid typeIdInfo list type");
    7944             :     }
    7945           0 :   } while (EatIfPresent(lltok::comma));
    7946             : 
    7947           7 :   if (ParseToken(lltok::rparen, "expected ')' in typeIdInfo"))
    7948             :     return true;
    7949          14 : 
    7950          14 :   return false;
    7951          21 : }
    7952           7 : 
    7953           0 : /// TypeTests
    7954             : ///   ::= 'typeTests' ':' '(' (SummaryID | UInt64)
    7955             : ///         [',' (SummaryID | UInt64)]* ')'
    7956             : bool LLParser::ParseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
    7957           4 :   assert(Lex.getKind() == lltok::kw_typeTests);
    7958           1 :   Lex.Lex();
    7959             : 
    7960           2 :   if (ParseToken(lltok::colon, "expected ':' here") ||
    7961           1 :       ParseToken(lltok::lparen, "expected '(' in typeIdInfo"))
    7962           0 :     return true;
    7963             : 
    7964           1 :   IdToIndexMapType IdToIndexMap;
    7965             :   do {
    7966           1 :     GlobalValue::GUID GUID = 0;
    7967           0 :     if (Lex.getKind() == lltok::SummaryID) {
    7968             :       unsigned ID = Lex.getUIntVal();
    7969           1 :       LocTy Loc = Lex.getLoc();
    7970             :       // Keep track of the TypeTests array index needing a forward reference.
    7971             :       // We will save the location of the GUID needing an update, but
    7972           1 :       // can only do so once the std::vector is finalized.
    7973           0 :       IdToIndexMap[ID].push_back(std::make_pair(TypeTests.size(), Loc));
    7974             :       Lex.Lex();
    7975           1 :     } else if (ParseUInt64(GUID))
    7976           1 :       return true;
    7977             :     TypeTests.push_back(GUID);
    7978           1 :   } while (EatIfPresent(lltok::comma));
    7979             : 
    7980           2 :   // Now that the TypeTests vector is finalized, it is safe to save the
    7981           1 :   // locations of any forward GV references that need updating later.
    7982           0 :   for (auto I : IdToIndexMap) {
    7983             :     for (auto P : I.second) {
    7984             :       assert(TypeTests[P.first] == 0 &&
    7985           0 :              "Forward referenced type id GUID expected to be 0");
    7986             :       auto FwdRef = ForwardRefTypeIds.insert(std::make_pair(
    7987             :           I.first, std::vector<std::pair<GlobalValue::GUID *, LocTy>>()));
    7988             :       FwdRef.first->second.push_back(
    7989           7 :           std::make_pair(&TypeTests[P.first], P.second));
    7990           0 :     }
    7991             :   }
    7992             : 
    7993             :   if (ParseToken(lltok::rparen, "expected ')' in typeIdInfo"))
    7994             :     return true;
    7995             : 
    7996             :   return false;
    7997             : }
    7998           2 : 
    7999             : /// VFuncIdList
    8000           4 : ///   ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
    8001           4 : bool LLParser::ParseVFuncIdList(
    8002           2 :     lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
    8003           0 :   assert(Lex.getKind() == Kind);
    8004             :   Lex.Lex();
    8005             : 
    8006             :   if (ParseToken(lltok::colon, "expected ':' here") ||
    8007           5 :       ParseToken(lltok::lparen, "expected '(' here"))
    8008          10 :     return true;
    8009          10 : 
    8010          15 :   IdToIndexMapType IdToIndexMap;
    8011          20 :   do {
    8012           5 :     FunctionSummary::VFuncId VFuncId;
    8013           0 :     if (ParseVFuncId(VFuncId, IdToIndexMap, VFuncIdList.size()))
    8014           5 :       return true;
    8015             :     VFuncIdList.push_back(VFuncId);
    8016             :   } while (EatIfPresent(lltok::comma));
    8017           2 : 
    8018           0 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    8019             :     return true;
    8020             : 
    8021             :   // Now that the VFuncIdList vector is finalized, it is safe to save the
    8022             :   // locations of any forward GV references that need updating later.
    8023             :   for (auto I : IdToIndexMap) {
    8024             :     for (auto P : I.second) {
    8025             :       assert(VFuncIdList[P.first].GUID == 0 &&
    8026             :              "Forward referenced type id GUID expected to be 0");
    8027             :       auto FwdRef = ForwardRefTypeIds.insert(std::make_pair(
    8028             :           I.first, std::vector<std::pair<GlobalValue::GUID *, LocTy>>()));
    8029             :       FwdRef.first->second.push_back(
    8030             :           std::make_pair(&VFuncIdList[P.first].GUID, P.second));
    8031           5 :     }
    8032          10 :   }
    8033          10 : 
    8034          10 :   return false;
    8035          15 : }
    8036           5 : 
    8037           0 : /// ConstVCallList
    8038             : ///   ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
    8039           5 : bool LLParser::ParseConstVCallList(
    8040           1 :     lltok::Kind Kind,
    8041           1 :     std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
    8042           1 :   assert(Lex.getKind() == Kind);
    8043           2 :   Lex.Lex();
    8044           2 : 
    8045           2 :   if (ParseToken(lltok::colon, "expected ':' here") ||
    8046           2 :       ParseToken(lltok::lparen, "expected '(' here"))
    8047           2 :     return true;
    8048           2 : 
    8049             :   IdToIndexMapType IdToIndexMap;
    8050           0 :   do {
    8051             :     FunctionSummary::ConstVCall ConstVCall;
    8052           5 :     if (ParseConstVCall(ConstVCall, IdToIndexMap, ConstVCallList.size()))
    8053             :       return true;
    8054             :     ConstVCallList.push_back(ConstVCall);
    8055             :   } while (EatIfPresent(lltok::comma));
    8056           3 : 
    8057           2 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    8058             :     return true;
    8059           4 : 
    8060           2 :   // Now that the ConstVCallList vector is finalized, it is safe to save the
    8061           0 :   // locations of any forward GV references that need updating later.
    8062             :   for (auto I : IdToIndexMap) {
    8063           1 :     for (auto P : I.second) {
    8064           1 :       assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
    8065             :              "Forward referenced type id GUID expected to be 0");
    8066             :       auto FwdRef = ForwardRefTypeIds.insert(std::make_pair(
    8067             :           I.first, std::vector<std::pair<GlobalValue::GUID *, LocTy>>()));
    8068           0 :       FwdRef.first->second.push_back(
    8069             :           std::make_pair(&ConstVCallList[P.first].VFunc.GUID, P.second));
    8070             :     }
    8071             :   }
    8072             : 
    8073           5 :   return false;
    8074           0 : }
    8075             : 
    8076             : /// ConstVCall
    8077             : ///   ::= '(' VFuncId ',' Args ')'
    8078             : bool LLParser::ParseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
    8079             :                                IdToIndexMapType &IdToIndexMap, unsigned Index) {
    8080             :   if (ParseToken(lltok::lparen, "expected '(' here") ||
    8081             :       ParseVFuncId(ConstVCall.VFunc, IdToIndexMap, Index))
    8082             :     return true;
    8083             : 
    8084             :   if (EatIfPresent(lltok::comma))
    8085             :     if (ParseArgs(ConstVCall.Args))
    8086           1 :       return true;
    8087             : 
    8088             :   if (ParseToken(lltok::rparen, "expected ')' here"))
    8089           2 :     return true;
    8090           2 : 
    8091           1 :   return false;
    8092           0 : }
    8093             : 
    8094             : /// VFuncId
    8095             : ///   ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
    8096          12 : ///         'offset' ':' UInt64 ')'
    8097           8 : bool LLParser::ParseVFuncId(FunctionSummary::VFuncId &VFuncId,
    8098           8 :                             IdToIndexMapType &IdToIndexMap, unsigned Index) {
    8099           8 :   assert(Lex.getKind() == lltok::kw_vFuncId);
    8100          12 :   Lex.Lex();
    8101           4 : 
    8102           0 :   if (ParseToken(lltok::colon, "expected ':' here") ||
    8103             :       ParseToken(lltok::lparen, "expected '(' here"))
    8104           4 :     return true;
    8105           4 : 
    8106             :   if (Lex.getKind() == lltok::SummaryID) {
    8107             :     VFuncId.GUID = 0;
    8108             :     unsigned ID = Lex.getUIntVal();
    8109           1 :     LocTy Loc = Lex.getLoc();
    8110           1 :     // Keep track of the array index needing a forward reference.
    8111           1 :     // We will save the location of the GUID needing an update, but
    8112           1 :     // can only do so once the caller's std::vector is finalized.
    8113           1 :     IdToIndexMap[ID].push_back(std::make_pair(Index, Loc));
    8114           1 :     Lex.Lex();
    8115           1 :   } else if (ParseToken(lltok::kw_guid, "expected 'guid' here") ||
    8116           1 :              ParseToken(lltok::colon, "expected ':' here") ||
    8117           1 :              ParseUInt64(VFuncId.GUID))
    8118             :     return true;
    8119           0 : 
    8120             :   if (ParseToken(lltok::comma, "expected ',' here") ||
    8121             :       ParseToken(lltok::kw_offset, "expected 'offset' here") ||
    8122           4 :       ParseToken(lltok::colon, "expected ':' here") ||
    8123             :       ParseUInt64(VFuncId.Offset) ||
    8124             :       ParseToken(lltok::rparen, "expected ')' here"))
    8125             :     return true;
    8126           4 : 
    8127           2 :   return false;
    8128             : }
    8129           4 : 
    8130           2 : /// GVFlags
    8131           0 : ///   ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
    8132             : ///         'notEligibleToImport' ':' Flag ',' 'live' ':' Flag ','
    8133           1 : ///         'dsoLocal' ':' Flag ')'
    8134             : bool LLParser::ParseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
    8135           2 :   assert(Lex.getKind() == lltok::kw_flags);
    8136           1 :   Lex.Lex();
    8137           0 : 
    8138             :   bool HasLinkage;
    8139           1 :   if (ParseToken(lltok::colon, "expected ':' here") ||
    8140             :       ParseToken(lltok::lparen, "expected '(' here") ||
    8141           2 :       ParseToken(lltok::kw_linkage, "expected 'linkage' here") ||
    8142           1 :       ParseToken(lltok::colon, "expected ':' here"))
    8143           0 :     return true;
    8144             : 
    8145             :   GVFlags.Linkage = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
    8146           0 :   assert(HasLinkage && "Linkage not optional in summary entry");
    8147             :   Lex.Lex();
    8148             : 
    8149             :   unsigned Flag;
    8150             :   if (ParseToken(lltok::comma, "expected ',' here") ||
    8151           4 :       ParseToken(lltok::kw_notEligibleToImport,
    8152             :                  "expected 'notEligibleToImport' here") ||
    8153             :       ParseToken(lltok::colon, "expected ':' here") || ParseFlag(Flag))
    8154           4 :     return true;
    8155             :   GVFlags.NotEligibleToImport = Flag;
    8156             : 
    8157           1 :   if (ParseToken(lltok::comma, "expected ',' here") ||
    8158           0 :       ParseToken(lltok::kw_live, "expected 'live' here") ||
    8159             :       ParseToken(lltok::colon, "expected ':' here") || ParseFlag(Flag))
    8160             :     return true;
    8161             :   GVFlags.Live = Flag;
    8162             : 
    8163             :   if (ParseToken(lltok::comma, "expected ',' here") ||
    8164             :       ParseToken(lltok::kw_dsoLocal, "expected 'dsoLocal' here") ||
    8165          12 :       ParseToken(lltok::colon, "expected ':' here") || ParseFlag(Flag))
    8166          24 :     return true;
    8167          24 :   GVFlags.DSOLocal = Flag;
    8168          12 : 
    8169           0 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    8170             :     return true;
    8171             : 
    8172             :   return false;
    8173          13 : }
    8174           0 : 
    8175          13 : /// ModuleReference
    8176             : ///   ::= 'module' ':' UInt
    8177             : bool LLParser::ParseModuleReference(StringRef &ModulePath) {
    8178          12 :   // Parse module id.
    8179           0 :   if (ParseToken(lltok::kw_module, "expected 'module' here") ||
    8180             :       ParseToken(lltok::colon, "expected ':' here") ||
    8181             :       ParseToken(lltok::SummaryID, "expected module ID"))
    8182             :     return true;
    8183             : 
    8184             :   unsigned ModuleID = Lex.getUIntVal();
    8185             :   auto I = ModuleIdMap.find(ModuleID);
    8186             :   // We should have already parsed all module IDs
    8187             :   assert(I != ModuleIdMap.end());
    8188             :   ModulePath = I->second;
    8189          96 :   return false;
    8190             : }
    8191             : 
    8192             : /// GVReference
    8193          96 : ///   ::= SummaryID
    8194          96 : bool LLParser::ParseGVReference(ValueInfo &VI, unsigned &GVId) {
    8195             :   if (ParseToken(lltok::SummaryID, "expected GV ID"))
    8196          54 :     return true;
    8197             : 
    8198             :   GVId = Lex.getUIntVal();
    8199          42 : 
    8200          42 :   // Check if we already have a VI for this GV
    8201             :   if (GVId < NumberedValueInfos.size()) {
    8202          42 :     assert(NumberedValueInfos[GVId] != EmptyVI);
    8203             :     VI = NumberedValueInfos[GVId];
    8204             :   } else
    8205             :     // We will create a forward reference to the stored location.
    8206             :     VI = EmptyVI;
    8207           0 : 
    8208           0 :   return false;
    8209           0 : }

Generated by: LCOV version 1.13