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

Generated by: LCOV version 1.13