LCOV - code coverage report
Current view: top level - lib/AsmParser - LLParser.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2720 3036 89.6 %
Date: 2018-05-20 00:06:23 Functions: 280 281 99.6 %
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       32211 : bool LLParser::Run() {
      66             :   // Prime the lexer.
      67       32211 :   Lex.Lex();
      68             : 
      69       32211 :   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       64204 :   return ParseTopLevelEntities() ||
      75       31993 :          ValidateEndOfModule();
      76             : }
      77             : 
      78         628 : bool LLParser::parseStandaloneConstantValue(Constant *&C,
      79             :                                             const SlotMapping *Slots) {
      80         628 :   restoreParsingState(Slots);
      81         628 :   Lex.Lex();
      82             : 
      83         628 :   Type *Ty = nullptr;
      84         628 :   if (ParseType(Ty) || parseConstantValue(Ty, C))
      85             :     return true;
      86         623 :   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         650 : void LLParser::restoreParsingState(const SlotMapping *Slots) {
     108         650 :   if (!Slots)
     109             :     return;
     110         615 :   NumberedVals = Slots->GlobalValues;
     111             :   NumberedMetadata = Slots->MetadataNodes;
     112        1285 :   for (const auto &I : Slots->NamedTypes)
     113             :     NamedTypes.insert(
     114          55 :         std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
     115         639 :   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       31993 : bool LLParser::ValidateEndOfModule() {
     123             :   // Handle any function attribute group forward references.
     124      450631 :   for (const auto &RAG : ForwardRefAttrGroups) {
     125      418638 :     Value *V = RAG.first;
     126             :     const std::vector<unsigned> &Attrs = RAG.second;
     127             :     AttrBuilder B;
     128             : 
     129      418638 :     for (const auto &Attr : Attrs)
     130       54421 :       B.merge(NumberedAttrBuilders[Attr]);
     131             : 
     132             :     if (Function *Fn = dyn_cast<Function>(V)) {
     133      284969 :       AttributeList AS = Fn->getAttributes();
     134      284969 :       AttrBuilder FnAttrs(AS.getFnAttributes());
     135      284969 :       AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
     136             : 
     137      284969 :       FnAttrs.merge(B);
     138             : 
     139             :       // If the alignment was parsed as an attribute, move to the alignment
     140             :       // field.
     141      284969 :       if (FnAttrs.hasAlignmentAttr()) {
     142           0 :         Fn->setAlignment(FnAttrs.getAlignment());
     143           0 :         FnAttrs.removeAttribute(Attribute::Alignment);
     144             :       }
     145             : 
     146      284969 :       AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
     147      569938 :                             AttributeSet::get(Context, FnAttrs));
     148             :       Fn->setAttributes(AS);
     149             :     } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
     150      131732 :       AttributeList AS = CI->getAttributes();
     151      131732 :       AttrBuilder FnAttrs(AS.getFnAttributes());
     152      131732 :       AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
     153      131732 :       FnAttrs.merge(B);
     154      131732 :       AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
     155      263464 :                             AttributeSet::get(Context, FnAttrs));
     156             :       CI->setAttributes(AS);
     157             :     } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
     158        1901 :       AttributeList AS = II->getAttributes();
     159        1901 :       AttrBuilder FnAttrs(AS.getFnAttributes());
     160        1901 :       AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
     161        1901 :       FnAttrs.merge(B);
     162        1901 :       AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
     163        3802 :                             AttributeSet::get(Context, FnAttrs));
     164             :       II->setAttributes(AS);
     165             :     } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
     166          36 :       AttrBuilder Attrs(GV->getAttributes());
     167          36 :       Attrs.merge(B);
     168          36 :       GV->setAttributes(AttributeSet::get(Context,Attrs));
     169             :     } else {
     170           0 :       llvm_unreachable("invalid object with forward attribute group reference");
     171             :     }
     172             :   }
     173             : 
     174             :   // If there are entries in ForwardRefBlockAddresses at this point, the
     175             :   // function was never defined.
     176       31993 :   if (!ForwardRefBlockAddresses.empty())
     177           0 :     return Error(ForwardRefBlockAddresses.begin()->first.Loc,
     178             :                  "expected function name in blockaddress");
     179             : 
     180       32537 :   for (const auto &NT : NumberedTypes)
     181         544 :     if (NT.second.second.isValid())
     182             :       return Error(NT.second.second,
     183           0 :                    "use of undefined type '%" + Twine(NT.first) + "'");
     184             : 
     185             :   for (StringMap<std::pair<Type*, LocTy> >::iterator I =
     186       75555 :        NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
     187       11571 :     if (I->second.second.isValid())
     188             :       return Error(I->second.second,
     189           4 :                    "use of undefined type named '" + I->getKey() + "'");
     190             : 
     191       31991 :   if (!ForwardRefComdats.empty())
     192           1 :     return Error(ForwardRefComdats.begin()->second,
     193           2 :                  "use of undefined comdat '$" +
     194           2 :                      ForwardRefComdats.begin()->first + "'");
     195             : 
     196       31990 :   if (!ForwardRefVals.empty())
     197           0 :     return Error(ForwardRefVals.begin()->second.second,
     198           0 :                  "use of undefined value '@" + ForwardRefVals.begin()->first +
     199             :                  "'");
     200             : 
     201       31990 :   if (!ForwardRefValIDs.empty())
     202             :     return Error(ForwardRefValIDs.begin()->second.second,
     203           0 :                  "use of undefined value '@" +
     204           0 :                  Twine(ForwardRefValIDs.begin()->first) + "'");
     205             : 
     206       31990 :   if (!ForwardRefMDNodes.empty())
     207             :     return Error(ForwardRefMDNodes.begin()->second.second,
     208           2 :                  "use of undefined metadata '!" +
     209           6 :                  Twine(ForwardRefMDNodes.begin()->first) + "'");
     210             : 
     211             :   // Resolve metadata cycles.
     212       86418 :   for (auto &N : NumberedMetadata) {
     213       54430 :     if (N.second && !N.second->isResolved())
     214         176 :       N.second->resolveCycles();
     215             :   }
     216             : 
     217       40852 :   for (auto *Inst : InstsWithTBAATag) {
     218             :     MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
     219             :     assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
     220        4432 :     auto *UpgradedMD = UpgradeTBAANode(*MD);
     221        4432 :     if (MD != UpgradedMD)
     222         199 :       Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
     223             :   }
     224             : 
     225             :   // Look for intrinsic functions and CallInst that need to be upgraded
     226      635266 :   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
     227      285645 :     UpgradeCallsToIntrinsic(&*FI++); // must be post-increment, as we remove
     228             : 
     229             :   // Some types could be renamed during loading if several modules are
     230             :   // loaded in the same LLVMContext (LTO scenario). In this case we should
     231             :   // remangle intrinsics names as well.
     232      629540 :   for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) {
     233             :     Function *F = &*FI++;
     234      282782 :     if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
     235           0 :       F->replaceAllUsesWith(Remangled.getValue());
     236           0 :       F->eraseFromParent();
     237             :     }
     238             :   }
     239             : 
     240       31988 :   if (UpgradeDebugInfo)
     241       12589 :     llvm::UpgradeDebugInfo(*M);
     242             : 
     243       31988 :   UpgradeModuleFlags(*M);
     244       31988 :   UpgradeSectionAttributes(*M);
     245             : 
     246       31988 :   if (!Slots)
     247             :     return false;
     248             :   // Initialize the slot mapping.
     249             :   // Because by this point we've parsed and validated everything, we can "steal"
     250             :   // the mapping from LLParser as it doesn't need it anymore.
     251         752 :   Slots->GlobalValues = std::move(NumberedVals);
     252         752 :   Slots->MetadataNodes = std::move(NumberedMetadata);
     253        1566 :   for (const auto &I : NamedTypes)
     254         124 :     Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
     255         755 :   for (const auto &I : NumberedTypes)
     256           6 :     Slots->Types.insert(std::make_pair(I.first, I.second.first));
     257             : 
     258             :   return false;
     259             : }
     260             : 
     261             : //===----------------------------------------------------------------------===//
     262             : // Top-Level Entities
     263             : //===----------------------------------------------------------------------===//
     264             : 
     265       32211 : bool LLParser::ParseTopLevelEntities() {
     266             :   while (true) {
     267      440940 :     switch (Lex.getKind()) {
     268           3 :     default:         return TokError("expected top-level entity");
     269             :     case lltok::Eof: return false;
     270       59088 :     case lltok::kw_declare: if (ParseDeclare()) return true; break;
     271      225985 :     case lltok::kw_define:  if (ParseDefine()) return true; break;
     272         344 :     case lltok::kw_module:  if (ParseModuleAsm()) return true; break;
     273       12721 :     case lltok::kw_target:  if (ParseTargetDefinition()) return true; break;
     274        1508 :     case lltok::kw_source_filename:
     275        1508 :       if (ParseSourceFileName())
     276             :         return true;
     277             :       break;
     278           0 :     case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
     279         544 :     case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
     280       11583 :     case lltok::LocalVar:   if (ParseNamedType()) return true; break;
     281         695 :     case lltok::GlobalID:   if (ParseUnnamedGlobal()) return true; break;
     282       27146 :     case lltok::GlobalVar:  if (ParseNamedGlobal()) return true; break;
     283         708 :     case lltok::ComdatVar:  if (parseComdat()) return true; break;
     284       54498 :     case lltok::exclaim:    if (ParseStandaloneMetadata()) return true; break;
     285        5265 :     case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
     286        8730 :     case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
     287         117 :     case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break;
     288          12 :     case lltok::kw_uselistorder_bb:
     289          12 :       if (ParseUseListOrderBB())
     290             :         return true;
     291             :       break;
     292             :     }
     293             :   }
     294             : }
     295             : 
     296             : /// toplevelentity
     297             : ///   ::= 'module' 'asm' STRINGCONSTANT
     298         344 : bool LLParser::ParseModuleAsm() {
     299             :   assert(Lex.getKind() == lltok::kw_module);
     300         344 :   Lex.Lex();
     301             : 
     302             :   std::string AsmStr;
     303         688 :   if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
     304         344 :       ParseStringConstant(AsmStr)) return true;
     305             : 
     306         688 :   M->appendModuleInlineAsm(AsmStr);
     307         344 :   return false;
     308             : }
     309             : 
     310             : /// toplevelentity
     311             : ///   ::= 'target' 'triple' '=' STRINGCONSTANT
     312             : ///   ::= 'target' 'datalayout' '=' STRINGCONSTANT
     313       12721 : bool LLParser::ParseTargetDefinition() {
     314             :   assert(Lex.getKind() == lltok::kw_target);
     315             :   std::string Str;
     316       25442 :   switch (Lex.Lex()) {
     317           0 :   default: return TokError("unknown target property");
     318        5071 :   case lltok::kw_triple:
     319             :     Lex.Lex();
     320       10142 :     if (ParseToken(lltok::equal, "expected '=' after target triple") ||
     321        5071 :         ParseStringConstant(Str))
     322             :       return true;
     323       10142 :     M->setTargetTriple(Str);
     324        5071 :     return false;
     325        7650 :   case lltok::kw_datalayout:
     326             :     Lex.Lex();
     327       15300 :     if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
     328        7650 :         ParseStringConstant(Str))
     329             :       return true;
     330        7650 :     if (DataLayoutStr.empty())
     331       15296 :       M->setDataLayout(Str);
     332             :     return false;
     333             :   }
     334             : }
     335             : 
     336             : /// toplevelentity
     337             : ///   ::= 'source_filename' '=' STRINGCONSTANT
     338        1508 : bool LLParser::ParseSourceFileName() {
     339             :   assert(Lex.getKind() == lltok::kw_source_filename);
     340             :   std::string Str;
     341        1508 :   Lex.Lex();
     342        3016 :   if (ParseToken(lltok::equal, "expected '=' after source_filename") ||
     343        1508 :       ParseStringConstant(Str))
     344             :     return true;
     345        3016 :   M->setSourceFileName(Str);
     346        1508 :   return false;
     347             : }
     348             : 
     349             : /// toplevelentity
     350             : ///   ::= 'deplibs' '=' '[' ']'
     351             : ///   ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
     352             : /// FIXME: Remove in 4.0. Currently parse, but ignore.
     353           0 : bool LLParser::ParseDepLibs() {
     354             :   assert(Lex.getKind() == lltok::kw_deplibs);
     355           0 :   Lex.Lex();
     356           0 :   if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
     357           0 :       ParseToken(lltok::lsquare, "expected '=' after deplibs"))
     358             :     return true;
     359             : 
     360             :   if (EatIfPresent(lltok::rsquare))
     361             :     return false;
     362             : 
     363             :   do {
     364             :     std::string Str;
     365           0 :     if (ParseStringConstant(Str)) return true;
     366             :   } while (EatIfPresent(lltok::comma));
     367             : 
     368           0 :   return ParseToken(lltok::rsquare, "expected ']' at end of list");
     369             : }
     370             : 
     371             : /// ParseUnnamedType:
     372             : ///   ::= LocalVarID '=' 'type' type
     373         544 : bool LLParser::ParseUnnamedType() {
     374         544 :   LocTy TypeLoc = Lex.getLoc();
     375         544 :   unsigned TypeID = Lex.getUIntVal();
     376         544 :   Lex.Lex(); // eat LocalVarID;
     377             : 
     378        1088 :   if (ParseToken(lltok::equal, "expected '=' after name") ||
     379         544 :       ParseToken(lltok::kw_type, "expected 'type' after '='"))
     380             :     return true;
     381             : 
     382         544 :   Type *Result = nullptr;
     383         544 :   if (ParseStructDefinition(TypeLoc, "",
     384         544 :                             NumberedTypes[TypeID], Result)) return true;
     385             : 
     386        1088 :   if (!isa<StructType>(Result)) {
     387           4 :     std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
     388           4 :     if (Entry.first)
     389           0 :       return Error(TypeLoc, "non-struct types may not be recursive");
     390           4 :     Entry.first = Result;
     391           4 :     Entry.second = SMLoc();
     392             :   }
     393             : 
     394             :   return false;
     395             : }
     396             : 
     397             : /// toplevelentity
     398             : ///   ::= LocalVar '=' 'type' type
     399       11583 : bool LLParser::ParseNamedType() {
     400             :   std::string Name = Lex.getStrVal();
     401       11583 :   LocTy NameLoc = Lex.getLoc();
     402       11583 :   Lex.Lex();  // eat LocalVar.
     403             : 
     404       23166 :   if (ParseToken(lltok::equal, "expected '=' after name") ||
     405       11583 :       ParseToken(lltok::kw_type, "expected 'type' after name"))
     406             :     return true;
     407             : 
     408       11583 :   Type *Result = nullptr;
     409       23166 :   if (ParseStructDefinition(NameLoc, Name,
     410             :                             NamedTypes[Name], Result)) return true;
     411             : 
     412       23166 :   if (!isa<StructType>(Result)) {
     413             :     std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
     414         440 :     if (Entry.first)
     415           0 :       return Error(NameLoc, "non-struct types may not be recursive");
     416         440 :     Entry.first = Result;
     417         440 :     Entry.second = SMLoc();
     418             :   }
     419             : 
     420             :   return false;
     421             : }
     422             : 
     423             : /// toplevelentity
     424             : ///   ::= 'declare' FunctionHeader
     425       59088 : bool LLParser::ParseDeclare() {
     426             :   assert(Lex.getKind() == lltok::kw_declare);
     427       59088 :   Lex.Lex();
     428             : 
     429             :   std::vector<std::pair<unsigned, MDNode *>> MDs;
     430       59148 :   while (Lex.getKind() == lltok::MetadataVar) {
     431             :     unsigned MDK;
     432             :     MDNode *N;
     433          30 :     if (ParseMetadataAttachment(MDK, N))
     434           0 :       return true;
     435          30 :     MDs.push_back({MDK, N});
     436             :   }
     437             : 
     438             :   Function *F;
     439       59088 :   if (ParseFunctionHeader(F, false))
     440             :     return true;
     441       59110 :   for (auto &MD : MDs)
     442          30 :     F->addMetadata(MD.first, *MD.second);
     443             :   return false;
     444             : }
     445             : 
     446             : /// toplevelentity
     447             : ///   ::= 'define' FunctionHeader (!dbg !56)* '{' ...
     448      225985 : bool LLParser::ParseDefine() {
     449             :   assert(Lex.getKind() == lltok::kw_define);
     450      225985 :   Lex.Lex();
     451             : 
     452             :   Function *F;
     453      451964 :   return ParseFunctionHeader(F, true) ||
     454      451964 :          ParseOptionalFunctionMetadata(*F) ||
     455      451964 :          ParseFunctionBody(*F);
     456             : }
     457             : 
     458             : /// ParseGlobalType
     459             : ///   ::= 'constant'
     460             : ///   ::= 'global'
     461       26847 : bool LLParser::ParseGlobalType(bool &IsConstant) {
     462       26847 :   if (Lex.getKind() == lltok::kw_constant)
     463        4592 :     IsConstant = true;
     464       22255 :   else if (Lex.getKind() == lltok::kw_global)
     465       22255 :     IsConstant = false;
     466             :   else {
     467           0 :     IsConstant = false;
     468           0 :     return TokError("expected 'global' or 'constant'");
     469             :   }
     470       26847 :   Lex.Lex();
     471       26847 :   return false;
     472             : }
     473             : 
     474      312904 : bool LLParser::ParseOptionalUnnamedAddr(
     475             :     GlobalVariable::UnnamedAddr &UnnamedAddr) {
     476             :   if (EatIfPresent(lltok::kw_unnamed_addr))
     477        3032 :     UnnamedAddr = GlobalValue::UnnamedAddr::Global;
     478             :   else if (EatIfPresent(lltok::kw_local_unnamed_addr))
     479        4764 :     UnnamedAddr = GlobalValue::UnnamedAddr::Local;
     480             :   else
     481      305108 :     UnnamedAddr = GlobalValue::UnnamedAddr::None;
     482      312904 :   return false;
     483             : }
     484             : 
     485             : /// ParseUnnamedGlobal:
     486             : ///   OptionalVisibility (ALIAS | IFUNC) ...
     487             : ///   OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
     488             : ///   OptionalDLLStorageClass
     489             : ///                                                     ...   -> global variable
     490             : ///   GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
     491             : ///   GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
     492             : ///                OptionalDLLStorageClass
     493             : ///                                                     ...   -> global variable
     494         695 : bool LLParser::ParseUnnamedGlobal() {
     495        1390 :   unsigned VarID = NumberedVals.size();
     496             :   std::string Name;
     497         695 :   LocTy NameLoc = Lex.getLoc();
     498             : 
     499             :   // Handle the GlobalID form.
     500         695 :   if (Lex.getKind() == lltok::GlobalID) {
     501         695 :     if (Lex.getUIntVal() != VarID)
     502           0 :       return Error(Lex.getLoc(), "variable expected to be numbered '%" +
     503           0 :                    Twine(VarID) + "'");
     504         695 :     Lex.Lex(); // eat GlobalID;
     505             : 
     506         695 :     if (ParseToken(lltok::equal, "expected '=' after name"))
     507             :       return true;
     508             :   }
     509             : 
     510             :   bool HasLinkage;
     511             :   unsigned Linkage, Visibility, DLLStorageClass;
     512             :   bool DSOLocal;
     513             :   GlobalVariable::ThreadLocalMode TLM;
     514             :   GlobalVariable::UnnamedAddr UnnamedAddr;
     515         695 :   if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
     516         695 :                            DSOLocal) ||
     517        1390 :       ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
     518             :     return true;
     519             : 
     520         695 :   if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
     521         691 :     return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
     522         691 :                        DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     523             : 
     524           4 :   return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
     525           4 :                              DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     526             : }
     527             : 
     528             : /// ParseNamedGlobal:
     529             : ///   GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
     530             : ///   GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
     531             : ///                 OptionalVisibility OptionalDLLStorageClass
     532             : ///                                                     ...   -> global variable
     533       27146 : bool LLParser::ParseNamedGlobal() {
     534             :   assert(Lex.getKind() == lltok::GlobalVar);
     535       27146 :   LocTy NameLoc = Lex.getLoc();
     536             :   std::string Name = Lex.getStrVal();
     537       27146 :   Lex.Lex();
     538             : 
     539             :   bool HasLinkage;
     540             :   unsigned Linkage, Visibility, DLLStorageClass;
     541             :   bool DSOLocal;
     542             :   GlobalVariable::ThreadLocalMode TLM;
     543             :   GlobalVariable::UnnamedAddr UnnamedAddr;
     544       54291 :   if (ParseToken(lltok::equal, "expected '=' in global variable") ||
     545       27145 :       ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
     546       27145 :                            DSOLocal) ||
     547       81436 :       ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
     548             :     return true;
     549             : 
     550       27145 :   if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
     551       26160 :     return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
     552       26160 :                        DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     553             : 
     554         985 :   return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
     555         985 :                              DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
     556             : }
     557             : 
     558         708 : bool LLParser::parseComdat() {
     559             :   assert(Lex.getKind() == lltok::ComdatVar);
     560             :   std::string Name = Lex.getStrVal();
     561         708 :   LocTy NameLoc = Lex.getLoc();
     562         708 :   Lex.Lex();
     563             : 
     564         708 :   if (ParseToken(lltok::equal, "expected '=' here"))
     565             :     return true;
     566             : 
     567         708 :   if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
     568           0 :     return TokError("expected comdat type");
     569             : 
     570             :   Comdat::SelectionKind SK;
     571         708 :   switch (Lex.getKind()) {
     572             :   default:
     573           0 :     return TokError("unknown selection kind");
     574             :   case lltok::kw_any:
     575             :     SK = Comdat::Any;
     576             :     break;
     577           5 :   case lltok::kw_exactmatch:
     578             :     SK = Comdat::ExactMatch;
     579           5 :     break;
     580          57 :   case lltok::kw_largest:
     581             :     SK = Comdat::Largest;
     582          57 :     break;
     583           7 :   case lltok::kw_noduplicates:
     584             :     SK = Comdat::NoDuplicates;
     585           7 :     break;
     586           7 :   case lltok::kw_samesize:
     587             :     SK = Comdat::SameSize;
     588           7 :     break;
     589             :   }
     590             :   Lex.Lex();
     591             : 
     592             :   // See if the comdat was forward referenced, if so, use the comdat.
     593         708 :   Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
     594         708 :   Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
     595        1418 :   if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
     596           4 :     return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
     597             : 
     598             :   Comdat *C;
     599        1414 :   if (I != ComdatSymTab.end())
     600           1 :     C = &I->second;
     601             :   else
     602        1412 :     C = M->getOrInsertComdat(Name);
     603             :   C->setSelectionKind(SK);
     604             : 
     605         707 :   return false;
     606             : }
     607             : 
     608             : // MDString:
     609             : //   ::= '!' STRINGCONSTANT
     610       11805 : bool LLParser::ParseMDString(MDString *&Result) {
     611             :   std::string Str;
     612       11805 :   if (ParseStringConstant(Str)) return true;
     613       11805 :   Result = MDString::get(Context, Str);
     614       11805 :   return false;
     615             : }
     616             : 
     617             : // MDNode:
     618             : //   ::= '!' MDNodeNumber
     619      114969 : bool LLParser::ParseMDNodeID(MDNode *&Result) {
     620             :   // !{ ..., !42, ... }
     621      114969 :   LocTy IDLoc = Lex.getLoc();
     622      114969 :   unsigned MID = 0;
     623      114969 :   if (ParseUInt32(MID))
     624             :     return true;
     625             : 
     626             :   // If not a forward reference, just return it now.
     627             :   if (NumberedMetadata.count(MID)) {
     628      124052 :     Result = NumberedMetadata[MID];
     629       62026 :     return false;
     630             :   }
     631             : 
     632             :   // Otherwise, create MDNode forward reference.
     633       52943 :   auto &FwdRef = ForwardRefMDNodes[MID];
     634       52943 :   FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), IDLoc);
     635             : 
     636       52943 :   Result = FwdRef.first.get();
     637       52943 :   NumberedMetadata[MID].reset(Result);
     638       52943 :   return false;
     639             : }
     640             : 
     641             : /// ParseNamedMetadata:
     642             : ///   !foo = !{ !1, !2 }
     643        5265 : bool LLParser::ParseNamedMetadata() {
     644             :   assert(Lex.getKind() == lltok::MetadataVar);
     645             :   std::string Name = Lex.getStrVal();
     646        5265 :   Lex.Lex();
     647             : 
     648       10530 :   if (ParseToken(lltok::equal, "expected '=' here") ||
     649       10530 :       ParseToken(lltok::exclaim, "Expected '!' here") ||
     650        5265 :       ParseToken(lltok::lbrace, "Expected '{' here"))
     651             :     return true;
     652             : 
     653       10530 :   NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
     654        5265 :   if (Lex.getKind() != lltok::rbrace)
     655             :     do {
     656        9580 :       MDNode *N = nullptr;
     657             :       // Parse DIExpressions inline as a special case. They are still MDNodes,
     658             :       // so they can still appear in named metadata. Remove this logic if they
     659             :       // become plain Metadata.
     660        9620 :       if (Lex.getKind() == lltok::MetadataVar &&
     661             :           Lex.getStrVal() == "DIExpression") {
     662          40 :         if (ParseDIExpression(N, /*IsDistinct=*/false))
     663           0 :           return true;
     664       19080 :       } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
     665        9540 :                  ParseMDNodeID(N)) {
     666             :         return true;
     667             :       }
     668        9580 :       NMD->addOperand(N);
     669             :     } while (EatIfPresent(lltok::comma));
     670             : 
     671        5265 :   return ParseToken(lltok::rbrace, "expected end of metadata node");
     672             : }
     673             : 
     674             : /// ParseStandaloneMetadata:
     675             : ///   !42 = !{...}
     676       54498 : bool LLParser::ParseStandaloneMetadata() {
     677             :   assert(Lex.getKind() == lltok::exclaim);
     678       54498 :   Lex.Lex();
     679       54498 :   unsigned MetadataID = 0;
     680             : 
     681             :   MDNode *Init;
     682      108996 :   if (ParseUInt32(MetadataID) ||
     683       54498 :       ParseToken(lltok::equal, "expected '=' here"))
     684             :     return true;
     685             : 
     686             :   // Detect common error, from old metadata syntax.
     687       54498 :   if (Lex.getKind() == lltok::Type)
     688           1 :     return TokError("unexpected type in metadata definition");
     689             : 
     690             :   bool IsDistinct = EatIfPresent(lltok::kw_distinct);
     691       54497 :   if (Lex.getKind() == lltok::MetadataVar) {
     692       35234 :     if (ParseSpecializedMDNode(Init, IsDistinct))
     693             :       return true;
     694       38526 :   } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
     695       19263 :              ParseMDTuple(Init, IsDistinct))
     696             :     return true;
     697             : 
     698             :   // See if this was forward referenced, if so, handle it.
     699             :   auto FI = ForwardRefMDNodes.find(MetadataID);
     700       54444 :   if (FI != ForwardRefMDNodes.end()) {
     701       52939 :     FI->second.first->replaceAllUsesWith(Init);
     702             :     ForwardRefMDNodes.erase(FI);
     703             : 
     704             :     assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
     705             :   } else {
     706             :     if (NumberedMetadata.count(MetadataID))
     707           0 :       return TokError("Metadata id is already used");
     708        1505 :     NumberedMetadata[MetadataID].reset(Init);
     709             :   }
     710             : 
     711             :   return false;
     712             : }
     713             : 
     714             : static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
     715        5326 :   return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
     716             :          (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
     717             : }
     718             : 
     719             : // If there was an explicit dso_local, update GV. In the absence of an explicit
     720             : // dso_local we keep the default value.
     721             : static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
     722      312870 :   if (DSOLocal)
     723             :     GV.setDSOLocal(true);
     724             : }
     725             : 
     726             : /// parseIndirectSymbol:
     727             : ///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier 
     728             : ///                     OptionalVisibility OptionalDLLStorageClass
     729             : ///                     OptionalThreadLocal OptionalUnnamedAddr
     730             : //                      'alias|ifunc' IndirectSymbol
     731             : ///
     732             : /// IndirectSymbol
     733             : ///   ::= TypeAndValue
     734             : ///
     735             : /// Everything through OptionalUnnamedAddr has already been parsed.
     736             : ///
     737         989 : bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
     738             :                                    unsigned L, unsigned Visibility,
     739             :                                    unsigned DLLStorageClass, bool DSOLocal,
     740             :                                    GlobalVariable::ThreadLocalMode TLM,
     741             :                                    GlobalVariable::UnnamedAddr UnnamedAddr) {
     742             :   bool IsAlias;
     743         989 :   if (Lex.getKind() == lltok::kw_alias)
     744             :     IsAlias = true;
     745          37 :   else if (Lex.getKind() == lltok::kw_ifunc)
     746             :     IsAlias = false;
     747             :   else
     748           0 :     llvm_unreachable("Not an alias or ifunc!");
     749         989 :   Lex.Lex();
     750             : 
     751             :   GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
     752             : 
     753         989 :   if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
     754           0 :     return Error(NameLoc, "invalid linkage type for alias");
     755             : 
     756             :   if (!isValidVisibilityForLinkage(Visibility, L))
     757           4 :     return Error(NameLoc,
     758             :                  "symbol with local linkage must have default visibility");
     759             : 
     760             :   Type *Ty;
     761         985 :   LocTy ExplicitTypeLoc = Lex.getLoc();
     762        1970 :   if (ParseType(Ty) ||
     763         985 :       ParseToken(lltok::comma, "expected comma after alias or ifunc's type"))
     764             :     return true;
     765             : 
     766             :   Constant *Aliasee;
     767         985 :   LocTy AliaseeLoc = Lex.getLoc();
     768        1816 :   if (Lex.getKind() != lltok::kw_bitcast &&
     769         720 :       Lex.getKind() != lltok::kw_getelementptr &&
     770        1694 :       Lex.getKind() != lltok::kw_addrspacecast &&
     771             :       Lex.getKind() != lltok::kw_inttoptr) {
     772         701 :     if (ParseGlobalTypeAndValue(Aliasee))
     773             :       return true;
     774             :   } else {
     775             :     // The bitcast dest type is not present, it is implied by the dest type.
     776         567 :     ValID ID;
     777         284 :     if (ParseValID(ID))
     778           1 :       return true;
     779         283 :     if (ID.Kind != ValID::t_Constant)
     780           0 :       return Error(AliaseeLoc, "invalid aliasee");
     781         283 :     Aliasee = ID.ConstantVal;
     782             :   }
     783             : 
     784         984 :   Type *AliaseeType = Aliasee->getType();
     785             :   auto *PTy = dyn_cast<PointerType>(AliaseeType);
     786             :   if (!PTy)
     787           0 :     return Error(AliaseeLoc, "An alias or ifunc must have pointer type");
     788             :   unsigned AddrSpace = PTy->getAddressSpace();
     789             : 
     790         984 :   if (IsAlias && Ty != PTy->getElementType())
     791           1 :     return Error(
     792             :         ExplicitTypeLoc,
     793             :         "explicit pointee type doesn't match operand's pointee type");
     794             : 
     795        1057 :   if (!IsAlias && !PTy->getElementType()->isFunctionTy())
     796           0 :     return Error(
     797             :         ExplicitTypeLoc,
     798             :         "explicit pointee type should be a function type");
     799             : 
     800             :   GlobalValue *GVal = nullptr;
     801             : 
     802             :   // See if the alias was forward referenced, if so, prepare to replace the
     803             :   // forward reference.
     804         983 :   if (!Name.empty()) {
     805        1958 :     GVal = M->getNamedValue(Name);
     806         979 :     if (GVal) {
     807          55 :       if (!ForwardRefVals.erase(Name))
     808           4 :         return Error(NameLoc, "redefinition of global '@" + Name + "'");
     809             :     }
     810             :   } else {
     811           8 :     auto I = ForwardRefValIDs.find(NumberedVals.size());
     812           4 :     if (I != ForwardRefValIDs.end()) {
     813           1 :       GVal = I->second.first;
     814             :       ForwardRefValIDs.erase(I);
     815             :     }
     816             :   }
     817             : 
     818             :   // Okay, create the alias but do not insert it into the module yet.
     819             :   std::unique_ptr<GlobalIndirectSymbol> GA;
     820         982 :   if (IsAlias)
     821        1890 :     GA.reset(GlobalAlias::create(Ty, AddrSpace,
     822             :                                  (GlobalValue::LinkageTypes)Linkage, Name,
     823             :                                  Aliasee, /*Parent*/ nullptr));
     824             :   else
     825          74 :     GA.reset(GlobalIFunc::create(Ty, AddrSpace,
     826             :                                  (GlobalValue::LinkageTypes)Linkage, Name,
     827             :                                  Aliasee, /*Parent*/ nullptr));
     828             :   GA->setThreadLocalMode(TLM);
     829             :   GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
     830             :   GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
     831             :   GA->setUnnamedAddr(UnnamedAddr);
     832             :   maybeSetDSOLocal(DSOLocal, *GA);
     833             : 
     834         982 :   if (Name.empty())
     835           8 :     NumberedVals.push_back(GA.get());
     836             : 
     837         982 :   if (GVal) {
     838             :     // Verify that types agree.
     839          55 :     if (GVal->getType() != GA->getType())
     840           0 :       return Error(
     841             :           ExplicitTypeLoc,
     842             :           "forward reference and definition of alias have different types");
     843             : 
     844             :     // If they agree, just RAUW the old value with the alias and remove the
     845             :     // forward ref info.
     846          55 :     GVal->replaceAllUsesWith(GA.get());
     847          55 :     GVal->eraseFromParent();
     848             :   }
     849             : 
     850             :   // Insert into the module, we know its name won't collide now.
     851         982 :   if (IsAlias)
     852         945 :     M->getAliasList().push_back(cast<GlobalAlias>(GA.get()));
     853             :   else
     854          37 :     M->getIFuncList().push_back(cast<GlobalIFunc>(GA.get()));
     855             :   assert(GA->getName() == Name && "Should not be a name conflict!");
     856             : 
     857             :   // The module owns this now
     858             :   GA.release();
     859             : 
     860             :   return false;
     861             : }
     862             : 
     863             : /// ParseGlobal
     864             : ///   ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
     865             : ///       OptionalVisibility OptionalDLLStorageClass
     866             : ///       OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
     867             : ///       OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
     868             : ///   ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
     869             : ///       OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
     870             : ///       OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
     871             : ///       Const OptionalAttrs
     872             : ///
     873             : /// Everything up to and including OptionalUnnamedAddr has been parsed
     874             : /// already.
     875             : ///
     876       26851 : bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
     877             :                            unsigned Linkage, bool HasLinkage,
     878             :                            unsigned Visibility, unsigned DLLStorageClass,
     879             :                            bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
     880             :                            GlobalVariable::UnnamedAddr UnnamedAddr) {
     881             :   if (!isValidVisibilityForLinkage(Visibility, Linkage))
     882           4 :     return Error(NameLoc,
     883             :                  "symbol with local linkage must have default visibility");
     884             : 
     885             :   unsigned AddrSpace;
     886             :   bool IsConstant, IsExternallyInitialized;
     887             :   LocTy IsExternallyInitializedLoc;
     888             :   LocTy TyLoc;
     889             : 
     890       26847 :   Type *Ty = nullptr;
     891       26847 :   if (ParseOptionalAddrSpace(AddrSpace) ||
     892             :       ParseOptionalToken(lltok::kw_externally_initialized,
     893             :                          IsExternallyInitialized,
     894       26847 :                          &IsExternallyInitializedLoc) ||
     895       80541 :       ParseGlobalType(IsConstant) ||
     896             :       ParseType(Ty, TyLoc))
     897             :     return true;
     898             : 
     899             :   // If the linkage is specified and is external, then no initializer is
     900             :   // present.
     901       26845 :   Constant *Init = nullptr;
     902       26845 :   if (!HasLinkage ||
     903             :       !GlobalValue::isValidDeclarationLinkage(
     904             :           (GlobalValue::LinkageTypes)Linkage)) {
     905       20264 :     if (ParseGlobalValue(Ty, Init))
     906             :       return true;
     907             :   }
     908             : 
     909       53666 :   if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
     910           2 :     return Error(TyLoc, "invalid type for global variable");
     911             : 
     912             :   GlobalValue *GVal = nullptr;
     913             : 
     914             :   // See if the global was forward referenced, if so, use the global.
     915       26831 :   if (!Name.empty()) {
     916       52292 :     GVal = M->getNamedValue(Name);
     917       26146 :     if (GVal) {
     918         412 :       if (!ForwardRefVals.erase(Name))
     919           4 :         return Error(NameLoc, "redefinition of global '@" + Name + "'");
     920             :     }
     921             :   } else {
     922        1370 :     auto I = ForwardRefValIDs.find(NumberedVals.size());
     923         685 :     if (I != ForwardRefValIDs.end()) {
     924          22 :       GVal = I->second.first;
     925             :       ForwardRefValIDs.erase(I);
     926             :     }
     927             :   }
     928             : 
     929             :   GlobalVariable *GV;
     930       26167 :   if (!GVal) {
     931       52794 :     GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
     932             :                             Name, nullptr, GlobalVariable::NotThreadLocal,
     933       52794 :                             AddrSpace);
     934             :   } else {
     935         433 :     if (GVal->getValueType() != Ty)
     936           1 :       return Error(TyLoc,
     937             :             "forward reference and definition of global have different types");
     938             : 
     939             :     GV = cast<GlobalVariable>(GVal);
     940             : 
     941             :     // Move the forward-reference to the correct spot in the module.
     942         864 :     M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
     943             :   }
     944             : 
     945       26829 :   if (Name.empty())
     946        1370 :     NumberedVals.push_back(GV);
     947             : 
     948             :   // Set the parsed properties on the global.
     949       26829 :   if (Init)
     950       20249 :     GV->setInitializer(Init);
     951       26829 :   GV->setConstant(IsConstant);
     952             :   GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
     953             :   maybeSetDSOLocal(DSOLocal, *GV);
     954             :   GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
     955             :   GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
     956             :   GV->setExternallyInitialized(IsExternallyInitialized);
     957             :   GV->setThreadLocalMode(TLM);
     958             :   GV->setUnnamedAddr(UnnamedAddr);
     959             : 
     960             :   // Parse attributes on the global.
     961       43702 :   while (Lex.getKind() == lltok::comma) {
     962       16873 :     Lex.Lex();
     963             : 
     964       16873 :     if (Lex.getKind() == lltok::kw_section) {
     965             :       Lex.Lex();
     966        1092 :       GV->setSection(Lex.getStrVal());
     967         546 :       if (ParseToken(lltok::StringConstant, "expected global section string"))
     968             :         return true;
     969       16327 :     } else if (Lex.getKind() == lltok::kw_align) {
     970             :       unsigned Alignment;
     971       14462 :       if (ParseOptionalAlignment(Alignment)) return true;
     972       14462 :       GV->setAlignment(Alignment);
     973        1865 :     } else if (Lex.getKind() == lltok::MetadataVar) {
     974        1456 :       if (ParseGlobalObjectMetadataAttachment(*GV))
     975             :         return true;
     976             :     } else {
     977             :       Comdat *C;
     978         409 :       if (parseOptionalComdat(Name, C))
     979           0 :         return true;
     980         409 :       if (C)
     981             :         GV->setComdat(C);
     982             :       else
     983           0 :         return TokError("unknown global variable property!");
     984             :     }
     985             :   }
     986             : 
     987             :   AttrBuilder Attrs;
     988       26829 :   LocTy BuiltinLoc;
     989             :   std::vector<unsigned> FwdRefAttrGrps;
     990       26829 :   if (ParseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
     991             :     return true;
     992       53652 :   if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
     993          36 :     GV->setAttributes(AttributeSet::get(Context, Attrs));
     994          36 :     ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
     995             :   }
     996             : 
     997             :   return false;
     998             : }
     999             : 
    1000             : /// ParseUnnamedAttrGrp
    1001             : ///   ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
    1002        8730 : bool LLParser::ParseUnnamedAttrGrp() {
    1003             :   assert(Lex.getKind() == lltok::kw_attributes);
    1004        8730 :   LocTy AttrGrpLoc = Lex.getLoc();
    1005        8730 :   Lex.Lex();
    1006             : 
    1007        8730 :   if (Lex.getKind() != lltok::AttrGrpID)
    1008           1 :     return TokError("expected attribute group id");
    1009             : 
    1010        8729 :   unsigned VarID = Lex.getUIntVal();
    1011             :   std::vector<unsigned> unused;
    1012        8729 :   LocTy BuiltinLoc;
    1013             :   Lex.Lex();
    1014             : 
    1015       17458 :   if (ParseToken(lltok::equal, "expected '=' here") ||
    1016       17458 :       ParseToken(lltok::lbrace, "expected '{' here") ||
    1017        8729 :       ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
    1018       17458 :                                  BuiltinLoc) ||
    1019        8729 :       ParseToken(lltok::rbrace, "expected end of attribute group"))
    1020             :     return true;
    1021             : 
    1022        8729 :   if (!NumberedAttrBuilders[VarID].hasAttributes())
    1023           0 :     return Error(AttrGrpLoc, "attribute group has no attributes");
    1024             : 
    1025             :   return false;
    1026             : }
    1027             : 
    1028             : /// ParseFnAttributeValuePairs
    1029             : ///   ::= <attr> | <attr> '=' <value>
    1030      454268 : bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
    1031             :                                           std::vector<unsigned> &FwdRefAttrGrps,
    1032             :                                           bool inAttrGrp, LocTy &BuiltinLoc) {
    1033             :   bool HaveError = false;
    1034             : 
    1035      454268 :   B.clear();
    1036             : 
    1037             :   while (true) {
    1038      647503 :     lltok::Kind Token = Lex.getKind();
    1039      647503 :     if (Token == lltok::kw_builtin)
    1040          56 :       BuiltinLoc = Lex.getLoc();
    1041      647503 :     switch (Token) {
    1042      445537 :     default:
    1043      445537 :       if (!inAttrGrp) return HaveError;
    1044           0 :       return Error(Lex.getLoc(), "unterminated attribute group");
    1045             :     case lltok::rbrace:
    1046             :       // Finished.
    1047             :       return false;
    1048             : 
    1049       54421 :     case lltok::AttrGrpID: {
    1050             :       // Allow a function to reference an attribute group:
    1051             :       //
    1052             :       //   define void @foo() #1 { ... }
    1053       54421 :       if (inAttrGrp)
    1054           0 :         HaveError |=
    1055           0 :           Error(Lex.getLoc(),
    1056             :               "cannot have an attribute group reference in an attribute group");
    1057             : 
    1058       54421 :       unsigned AttrGrpNum = Lex.getUIntVal();
    1059       54421 :       if (inAttrGrp) break;
    1060             : 
    1061             :       // Save the reference to the attribute group. We'll fill it in later.
    1062       54421 :       FwdRefAttrGrps.push_back(AttrGrpNum);
    1063       54421 :       break;
    1064             :     }
    1065             :     // Target-dependent attributes:
    1066       17383 :     case lltok::StringConstant: {
    1067       17383 :       if (ParseStringAttribute(B))
    1068             :         return true;
    1069       17383 :       continue;
    1070             :     }
    1071             : 
    1072             :     // Target-independent attributes:
    1073         628 :     case lltok::kw_align: {
    1074             :       // As a hack, we allow function alignment to be initially parsed as an
    1075             :       // attribute on a function declaration/definition or added to an attribute
    1076             :       // group and later moved to the alignment field.
    1077             :       unsigned Alignment;
    1078         628 :       if (inAttrGrp) {
    1079           1 :         Lex.Lex();
    1080           2 :         if (ParseToken(lltok::equal, "expected '=' here") ||
    1081           1 :             ParseUInt32(Alignment))
    1082           0 :           return true;
    1083             :       } else {
    1084         627 :         if (ParseOptionalAlignment(Alignment))
    1085             :           return true;
    1086             :       }
    1087         628 :       B.addAlignmentAttr(Alignment);
    1088         628 :       continue;
    1089             :     }
    1090          48 :     case lltok::kw_alignstack: {
    1091             :       unsigned Alignment;
    1092          48 :       if (inAttrGrp) {
    1093          14 :         Lex.Lex();
    1094          28 :         if (ParseToken(lltok::equal, "expected '=' here") ||
    1095          14 :             ParseUInt32(Alignment))
    1096           0 :           return true;
    1097             :       } else {
    1098          34 :         if (ParseOptionalStackAlignment(Alignment))
    1099             :           return true;
    1100             :       }
    1101          48 :       B.addStackAlignmentAttr(Alignment);
    1102          48 :       continue;
    1103             :     }
    1104             :     case lltok::kw_allocsize: {
    1105             :       unsigned ElemSizeArg;
    1106             :       Optional<unsigned> NumElemsArg;
    1107             :       // inAttrGrp doesn't matter; we only support allocsize(a[, b])
    1108          25 :       if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
    1109             :         return true;
    1110          24 :       B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
    1111             :       continue;
    1112             :     }
    1113         241 :     case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
    1114         325 :     case lltok::kw_argmemonly: B.addAttribute(Attribute::ArgMemOnly); break;
    1115          56 :     case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
    1116          32 :     case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
    1117         186 :     case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
    1118          20 :     case lltok::kw_inaccessiblememonly:
    1119          20 :       B.addAttribute(Attribute::InaccessibleMemOnly); break;
    1120          15 :     case lltok::kw_inaccessiblemem_or_argmemonly:
    1121          15 :       B.addAttribute(Attribute::InaccessibleMemOrArgMemOnly); break;
    1122         147 :     case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
    1123           9 :     case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
    1124         651 :     case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
    1125          28 :     case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
    1126         135 :     case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
    1127          31 :     case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
    1128          48 :     case lltok::kw_noimplicitfloat:
    1129          48 :       B.addAttribute(Attribute::NoImplicitFloat); break;
    1130        1144 :     case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
    1131          43 :     case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
    1132         195 :     case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
    1133         491 :     case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
    1134           2 :     case lltok::kw_nocf_check: B.addAttribute(Attribute::NoCfCheck); break;
    1135         276 :     case lltok::kw_norecurse: B.addAttribute(Attribute::NoRecurse); break;
    1136       77586 :     case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
    1137           1 :     case lltok::kw_optforfuzzing:
    1138           1 :       B.addAttribute(Attribute::OptForFuzzing); break;
    1139         137 :     case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
    1140        5383 :     case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
    1141       20020 :     case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
    1142        2359 :     case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
    1143          75 :     case lltok::kw_returns_twice:
    1144          75 :       B.addAttribute(Attribute::ReturnsTwice); break;
    1145         287 :     case lltok::kw_speculatable: B.addAttribute(Attribute::Speculatable); break;
    1146        5165 :     case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
    1147          75 :     case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
    1148          76 :     case lltok::kw_sspstrong:
    1149          76 :       B.addAttribute(Attribute::StackProtectStrong); break;
    1150         194 :     case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
    1151          11 :     case lltok::kw_shadowcallstack:
    1152          11 :       B.addAttribute(Attribute::ShadowCallStack); break;
    1153         329 :     case lltok::kw_sanitize_address:
    1154         329 :       B.addAttribute(Attribute::SanitizeAddress); break;
    1155         136 :     case lltok::kw_sanitize_hwaddress:
    1156         136 :       B.addAttribute(Attribute::SanitizeHWAddress); break;
    1157          66 :     case lltok::kw_sanitize_thread:
    1158          66 :       B.addAttribute(Attribute::SanitizeThread); break;
    1159         289 :     case lltok::kw_sanitize_memory:
    1160         289 :       B.addAttribute(Attribute::SanitizeMemory); break;
    1161           6 :     case lltok::kw_strictfp: B.addAttribute(Attribute::StrictFP); break;
    1162        4440 :     case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
    1163          21 :     case lltok::kw_writeonly: B.addAttribute(Attribute::WriteOnly); break;
    1164             : 
    1165             :     // Error handling.
    1166             :     case lltok::kw_inreg:
    1167             :     case lltok::kw_signext:
    1168             :     case lltok::kw_zeroext:
    1169           0 :       HaveError |=
    1170           0 :         Error(Lex.getLoc(),
    1171             :               "invalid use of attribute on a function");
    1172           0 :       break;
    1173             :     case lltok::kw_byval:
    1174             :     case lltok::kw_dereferenceable:
    1175             :     case lltok::kw_dereferenceable_or_null:
    1176             :     case lltok::kw_inalloca:
    1177             :     case lltok::kw_nest:
    1178             :     case lltok::kw_noalias:
    1179             :     case lltok::kw_nocapture:
    1180             :     case lltok::kw_nonnull:
    1181             :     case lltok::kw_returned:
    1182             :     case lltok::kw_sret:
    1183             :     case lltok::kw_swifterror:
    1184             :     case lltok::kw_swiftself:
    1185           0 :       HaveError |=
    1186           0 :         Error(Lex.getLoc(),
    1187             :               "invalid use of parameter-only attribute on a function");
    1188       17383 :       break;
    1189             :     }
    1190             : 
    1191      175152 :     Lex.Lex();
    1192             :   }
    1193             : }
    1194             : 
    1195             : //===----------------------------------------------------------------------===//
    1196             : // GlobalValue Reference/Resolution Routines.
    1197             : //===----------------------------------------------------------------------===//
    1198             : 
    1199       38537 : static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy,
    1200             :                                               const std::string &Name) {
    1201       38537 :   if (auto *FT = dyn_cast<FunctionType>(PTy->getElementType()))
    1202       38076 :     return Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
    1203             :   else
    1204             :     return new GlobalVariable(*M, PTy->getElementType(), false,
    1205             :                               GlobalValue::ExternalWeakLinkage, nullptr, Name,
    1206             :                               nullptr, GlobalVariable::NotThreadLocal,
    1207         461 :                               PTy->getAddressSpace());
    1208             : }
    1209             : 
    1210             : /// GetGlobalVal - Get a value with the specified name or ID, creating a
    1211             : /// forward reference record if needed.  This can return null if the value
    1212             : /// exists but does not have the right type.
    1213      199302 : GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
    1214             :                                     LocTy Loc) {
    1215             :   PointerType *PTy = dyn_cast<PointerType>(Ty);
    1216             :   if (!PTy) {
    1217           0 :     Error(Loc, "global variable reference must have pointer type");
    1218           0 :     return nullptr;
    1219             :   }
    1220             : 
    1221             :   // Look this name up in the normal function symbol table.
    1222             :   GlobalValue *Val =
    1223      199302 :     cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
    1224             : 
    1225             :   // If this is a forward reference for the value, see if we already created a
    1226             :   // forward ref record.
    1227             :   if (!Val) {
    1228             :     auto I = ForwardRefVals.find(Name);
    1229       38494 :     if (I != ForwardRefVals.end())
    1230           0 :       Val = I->second.first;
    1231             :   }
    1232             : 
    1233             :   // If we have the value in the symbol table or fwd-ref table, return it.
    1234      199302 :   if (Val) {
    1235      160808 :     if (Val->getType() == Ty) return Val;
    1236          10 :     Error(Loc, "'@" + Name + "' defined with type '" +
    1237           8 :           getTypeString(Val->getType()) + "'");
    1238           2 :     return nullptr;
    1239             :   }
    1240             : 
    1241             :   // Otherwise, create a new forward reference for this value and remember it.
    1242       38494 :   GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, Name);
    1243       38494 :   ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
    1244       38494 :   return FwdVal;
    1245             : }
    1246             : 
    1247         663 : GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
    1248             :   PointerType *PTy = dyn_cast<PointerType>(Ty);
    1249             :   if (!PTy) {
    1250           0 :     Error(Loc, "global variable reference must have pointer type");
    1251           0 :     return nullptr;
    1252             :   }
    1253             : 
    1254        1938 :   GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
    1255             : 
    1256             :   // If this is a forward reference for the value, see if we already created a
    1257             :   // forward ref record.
    1258         612 :   if (!Val) {
    1259             :     auto I = ForwardRefValIDs.find(ID);
    1260          51 :     if (I != ForwardRefValIDs.end())
    1261           8 :       Val = I->second.first;
    1262             :   }
    1263             : 
    1264             :   // If we have the value in the symbol table or fwd-ref table, return it.
    1265         663 :   if (Val) {
    1266         620 :     if (Val->getType() == Ty) return Val;
    1267           0 :     Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
    1268           0 :           getTypeString(Val->getType()) + "'");
    1269           0 :     return nullptr;
    1270             :   }
    1271             : 
    1272             :   // Otherwise, create a new forward reference for this value and remember it.
    1273          86 :   GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, "");
    1274          43 :   ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
    1275          43 :   return FwdVal;
    1276             : }
    1277             : 
    1278             : //===----------------------------------------------------------------------===//
    1279             : // Comdat Reference/Resolution Routines.
    1280             : //===----------------------------------------------------------------------===//
    1281             : 
    1282         803 : Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
    1283             :   // Look this name up in the comdat symbol table.
    1284         803 :   Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
    1285         803 :   Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
    1286        1606 :   if (I != ComdatSymTab.end())
    1287         801 :     return &I->second;
    1288             : 
    1289             :   // Otherwise, create a new forward reference for this value and remember it.
    1290           4 :   Comdat *C = M->getOrInsertComdat(Name);
    1291           2 :   ForwardRefComdats[Name] = Loc;
    1292           2 :   return C;
    1293             : }
    1294             : 
    1295             : //===----------------------------------------------------------------------===//
    1296             : // Helper Routines.
    1297             : //===----------------------------------------------------------------------===//
    1298             : 
    1299             : /// ParseToken - If the current token has the specified kind, eat it and return
    1300             : /// success.  Otherwise, emit the specified error and return failure.
    1301     6103020 : bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
    1302     6103020 :   if (Lex.getKind() != T)
    1303           5 :     return TokError(ErrMsg);
    1304     6103015 :   Lex.Lex();
    1305     6103015 :   return false;
    1306             : }
    1307             : 
    1308             : /// ParseStringConstant
    1309             : ///   ::= StringConstant
    1310       81933 : bool LLParser::ParseStringConstant(std::string &Result) {
    1311       81933 :   if (Lex.getKind() != lltok::StringConstant)
    1312           0 :     return TokError("expected string constant");
    1313             :   Result = Lex.getStrVal();
    1314       81933 :   Lex.Lex();
    1315       81933 :   return false;
    1316             : }
    1317             : 
    1318             : /// ParseUInt32
    1319             : ///   ::= uint32
    1320      528112 : bool LLParser::ParseUInt32(uint32_t &Val) {
    1321      528112 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    1322           0 :     return TokError("expected integer");
    1323      528112 :   uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
    1324      528112 :   if (Val64 != unsigned(Val64))
    1325           0 :     return TokError("expected 32-bit integer (too large)");
    1326      528112 :   Val = Val64;
    1327      528112 :   Lex.Lex();
    1328      528112 :   return false;
    1329             : }
    1330             : 
    1331             : /// ParseUInt64
    1332             : ///   ::= uint64
    1333         496 : bool LLParser::ParseUInt64(uint64_t &Val) {
    1334         496 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    1335           0 :     return TokError("expected integer");
    1336         992 :   Val = Lex.getAPSIntVal().getLimitedValue();
    1337         496 :   Lex.Lex();
    1338         496 :   return false;
    1339             : }
    1340             : 
    1341             : /// ParseTLSModel
    1342             : ///   := 'localdynamic'
    1343             : ///   := 'initialexec'
    1344             : ///   := 'localexec'
    1345         152 : bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
    1346         152 :   switch (Lex.getKind()) {
    1347             :     default:
    1348           0 :       return TokError("expected localdynamic, initialexec or localexec");
    1349          51 :     case lltok::kw_localdynamic:
    1350          51 :       TLM = GlobalVariable::LocalDynamicTLSModel;
    1351          51 :       break;
    1352          54 :     case lltok::kw_initialexec:
    1353          54 :       TLM = GlobalVariable::InitialExecTLSModel;
    1354          54 :       break;
    1355          47 :     case lltok::kw_localexec:
    1356          47 :       TLM = GlobalVariable::LocalExecTLSModel;
    1357          47 :       break;
    1358             :   }
    1359             : 
    1360         152 :   Lex.Lex();
    1361         152 :   return false;
    1362             : }
    1363             : 
    1364             : /// ParseOptionalThreadLocal
    1365             : ///   := /*empty*/
    1366             : ///   := 'thread_local'
    1367             : ///   := 'thread_local' '(' tlsmodel ')'
    1368       27840 : bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
    1369       27840 :   TLM = GlobalVariable::NotThreadLocal;
    1370             :   if (!EatIfPresent(lltok::kw_thread_local))
    1371             :     return false;
    1372             : 
    1373         836 :   TLM = GlobalVariable::GeneralDynamicTLSModel;
    1374         836 :   if (Lex.getKind() == lltok::lparen) {
    1375             :     Lex.Lex();
    1376         304 :     return ParseTLSModel(TLM) ||
    1377         152 :       ParseToken(lltok::rparen, "expected ')' after thread local model");
    1378             :   }
    1379             :   return false;
    1380             : }
    1381             : 
    1382             : /// ParseOptionalAddrSpace
    1383             : ///   := /*empty*/
    1384             : ///   := 'addrspace' '(' uint32 ')'
    1385      122271 : bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
    1386      122271 :   AddrSpace = 0;
    1387             :   if (!EatIfPresent(lltok::kw_addrspace))
    1388             :     return false;
    1389      191868 :   return ParseToken(lltok::lparen, "expected '(' in address space") ||
    1390      191868 :          ParseUInt32(AddrSpace) ||
    1391       95934 :          ParseToken(lltok::rparen, "expected ')' in address space");
    1392             : }
    1393             : 
    1394             : /// ParseStringAttribute
    1395             : ///   := StringConstant
    1396             : ///   := StringConstant '=' StringConstant
    1397       17413 : bool LLParser::ParseStringAttribute(AttrBuilder &B) {
    1398             :   std::string Attr = Lex.getStrVal();
    1399       17413 :   Lex.Lex();
    1400             :   std::string Val;
    1401       16254 :   if (EatIfPresent(lltok::equal) && ParseStringConstant(Val))
    1402             :     return true;
    1403       17413 :   B.addAttribute(Attr, Val);
    1404       17413 :   return false;
    1405             : }
    1406             : 
    1407             : /// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
    1408      754124 : bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
    1409             :   bool HaveError = false;
    1410             : 
    1411      754124 :   B.clear();
    1412             : 
    1413             :   while (true) {
    1414      803021 :     lltok::Kind Token = Lex.getKind();
    1415      803021 :     switch (Token) {
    1416             :     default:  // End of attributes.
    1417             :       return HaveError;
    1418          14 :     case lltok::StringConstant: {
    1419          14 :       if (ParseStringAttribute(B))
    1420             :         return true;
    1421          14 :       continue;
    1422             :     }
    1423        4911 :     case lltok::kw_align: {
    1424             :       unsigned Alignment;
    1425        4911 :       if (ParseOptionalAlignment(Alignment))
    1426           1 :         return true;
    1427        4910 :       B.addAlignmentAttr(Alignment);
    1428        4910 :       continue;
    1429             :     }
    1430        1720 :     case lltok::kw_byval:           B.addAttribute(Attribute::ByVal); break;
    1431         385 :     case lltok::kw_dereferenceable: {
    1432             :       uint64_t Bytes;
    1433         385 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
    1434           0 :         return true;
    1435         385 :       B.addDereferenceableAttr(Bytes);
    1436         385 :       continue;
    1437             :     }
    1438          22 :     case lltok::kw_dereferenceable_or_null: {
    1439             :       uint64_t Bytes;
    1440          22 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
    1441           0 :         return true;
    1442          22 :       B.addDereferenceableOrNullAttr(Bytes);
    1443          22 :       continue;
    1444             :     }
    1445         128 :     case lltok::kw_inalloca:        B.addAttribute(Attribute::InAlloca); break;
    1446        2539 :     case lltok::kw_inreg:           B.addAttribute(Attribute::InReg); break;
    1447          71 :     case lltok::kw_nest:            B.addAttribute(Attribute::Nest); break;
    1448        5028 :     case lltok::kw_noalias:         B.addAttribute(Attribute::NoAlias); break;
    1449       13041 :     case lltok::kw_nocapture:       B.addAttribute(Attribute::NoCapture); break;
    1450        1431 :     case lltok::kw_nonnull:         B.addAttribute(Attribute::NonNull); break;
    1451         211 :     case lltok::kw_readnone:        B.addAttribute(Attribute::ReadNone); break;
    1452        3581 :     case lltok::kw_readonly:        B.addAttribute(Attribute::ReadOnly); break;
    1453         202 :     case lltok::kw_returned:        B.addAttribute(Attribute::Returned); break;
    1454        8903 :     case lltok::kw_signext:         B.addAttribute(Attribute::SExt); break;
    1455        1056 :     case lltok::kw_sret:            B.addAttribute(Attribute::StructRet); break;
    1456         329 :     case lltok::kw_swifterror:      B.addAttribute(Attribute::SwiftError); break;
    1457         206 :     case lltok::kw_swiftself:       B.addAttribute(Attribute::SwiftSelf); break;
    1458         228 :     case lltok::kw_writeonly:       B.addAttribute(Attribute::WriteOnly); break;
    1459        4891 :     case lltok::kw_zeroext:         B.addAttribute(Attribute::ZExt); break;
    1460             : 
    1461             :     case lltok::kw_alignstack:
    1462             :     case lltok::kw_alwaysinline:
    1463             :     case lltok::kw_argmemonly:
    1464             :     case lltok::kw_builtin:
    1465             :     case lltok::kw_inlinehint:
    1466             :     case lltok::kw_jumptable:
    1467             :     case lltok::kw_minsize:
    1468             :     case lltok::kw_naked:
    1469             :     case lltok::kw_nobuiltin:
    1470             :     case lltok::kw_noduplicate:
    1471             :     case lltok::kw_noimplicitfloat:
    1472             :     case lltok::kw_noinline:
    1473             :     case lltok::kw_nonlazybind:
    1474             :     case lltok::kw_noredzone:
    1475             :     case lltok::kw_noreturn:
    1476             :     case lltok::kw_nocf_check:
    1477             :     case lltok::kw_nounwind:
    1478             :     case lltok::kw_optforfuzzing:
    1479             :     case lltok::kw_optnone:
    1480             :     case lltok::kw_optsize:
    1481             :     case lltok::kw_returns_twice:
    1482             :     case lltok::kw_sanitize_address:
    1483             :     case lltok::kw_sanitize_hwaddress:
    1484             :     case lltok::kw_sanitize_memory:
    1485             :     case lltok::kw_sanitize_thread:
    1486             :     case lltok::kw_ssp:
    1487             :     case lltok::kw_sspreq:
    1488             :     case lltok::kw_sspstrong:
    1489             :     case lltok::kw_safestack:
    1490             :     case lltok::kw_shadowcallstack:
    1491             :     case lltok::kw_strictfp:
    1492             :     case lltok::kw_uwtable:
    1493           2 :       HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
    1494          15 :       break;
    1495             :     }
    1496             : 
    1497       43566 :     Lex.Lex();
    1498             :   }
    1499             : }
    1500             : 
    1501             : /// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
    1502      418726 : bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
    1503             :   bool HaveError = false;
    1504             : 
    1505      418726 :   B.clear();
    1506             : 
    1507             :   while (true) {
    1508      428318 :     lltok::Kind Token = Lex.getKind();
    1509      428318 :     switch (Token) {
    1510             :     default:  // End of attributes.
    1511             :       return HaveError;
    1512          16 :     case lltok::StringConstant: {
    1513          16 :       if (ParseStringAttribute(B))
    1514             :         return true;
    1515          16 :       continue;
    1516             :     }
    1517          75 :     case lltok::kw_dereferenceable: {
    1518             :       uint64_t Bytes;
    1519          75 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
    1520           0 :         return true;
    1521          75 :       B.addDereferenceableAttr(Bytes);
    1522          75 :       continue;
    1523             :     }
    1524          14 :     case lltok::kw_dereferenceable_or_null: {
    1525             :       uint64_t Bytes;
    1526          14 :       if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
    1527           0 :         return true;
    1528          14 :       B.addDereferenceableOrNullAttr(Bytes);
    1529          14 :       continue;
    1530             :     }
    1531           4 :     case lltok::kw_align: {
    1532             :       unsigned Alignment;
    1533           4 :       if (ParseOptionalAlignment(Alignment))
    1534           0 :         return true;
    1535           4 :       B.addAlignmentAttr(Alignment);
    1536           4 :       continue;
    1537             :     }
    1538          82 :     case lltok::kw_inreg:           B.addAttribute(Attribute::InReg); break;
    1539         506 :     case lltok::kw_noalias:         B.addAttribute(Attribute::NoAlias); break;
    1540          66 :     case lltok::kw_nonnull:         B.addAttribute(Attribute::NonNull); break;
    1541        4690 :     case lltok::kw_signext:         B.addAttribute(Attribute::SExt); break;
    1542        4137 :     case lltok::kw_zeroext:         B.addAttribute(Attribute::ZExt); break;
    1543             : 
    1544             :     // Error handling.
    1545             :     case lltok::kw_byval:
    1546             :     case lltok::kw_inalloca:
    1547             :     case lltok::kw_nest:
    1548             :     case lltok::kw_nocapture:
    1549             :     case lltok::kw_returned:
    1550             :     case lltok::kw_sret:
    1551             :     case lltok::kw_swifterror:
    1552             :     case lltok::kw_swiftself:
    1553           2 :       HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
    1554           1 :       break;
    1555             : 
    1556             :     case lltok::kw_alignstack:
    1557             :     case lltok::kw_alwaysinline:
    1558             :     case lltok::kw_argmemonly:
    1559             :     case lltok::kw_builtin:
    1560             :     case lltok::kw_cold:
    1561             :     case lltok::kw_inlinehint:
    1562             :     case lltok::kw_jumptable:
    1563             :     case lltok::kw_minsize:
    1564             :     case lltok::kw_naked:
    1565             :     case lltok::kw_nobuiltin:
    1566             :     case lltok::kw_noduplicate:
    1567             :     case lltok::kw_noimplicitfloat:
    1568             :     case lltok::kw_noinline:
    1569             :     case lltok::kw_nonlazybind:
    1570             :     case lltok::kw_noredzone:
    1571             :     case lltok::kw_noreturn:
    1572             :     case lltok::kw_nocf_check:
    1573             :     case lltok::kw_nounwind:
    1574             :     case lltok::kw_optforfuzzing:
    1575             :     case lltok::kw_optnone:
    1576             :     case lltok::kw_optsize:
    1577             :     case lltok::kw_returns_twice:
    1578             :     case lltok::kw_sanitize_address:
    1579             :     case lltok::kw_sanitize_hwaddress:
    1580             :     case lltok::kw_sanitize_memory:
    1581             :     case lltok::kw_sanitize_thread:
    1582             :     case lltok::kw_ssp:
    1583             :     case lltok::kw_sspreq:
    1584             :     case lltok::kw_sspstrong:
    1585             :     case lltok::kw_safestack:
    1586             :     case lltok::kw_shadowcallstack:
    1587             :     case lltok::kw_strictfp:
    1588             :     case lltok::kw_uwtable:
    1589           2 :       HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
    1590           1 :       break;
    1591             : 
    1592             :     case lltok::kw_readnone:
    1593             :     case lltok::kw_readonly:
    1594          16 :       HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
    1595             :     }
    1596             : 
    1597        9483 :     Lex.Lex();
    1598             :   }
    1599             : }
    1600             : 
    1601      312913 : static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
    1602      312913 :   HasLinkage = true;
    1603      312913 :   switch (Kind) {
    1604      293733 :   default:
    1605      293733 :     HasLinkage = false;
    1606      293733 :     return GlobalValue::ExternalLinkage;
    1607             :   case lltok::kw_private:
    1608             :     return GlobalValue::PrivateLinkage;
    1609        3445 :   case lltok::kw_internal:
    1610        3445 :     return GlobalValue::InternalLinkage;
    1611         693 :   case lltok::kw_weak:
    1612         693 :     return GlobalValue::WeakAnyLinkage;
    1613         287 :   case lltok::kw_weak_odr:
    1614         287 :     return GlobalValue::WeakODRLinkage;
    1615         138 :   case lltok::kw_linkonce:
    1616         138 :     return GlobalValue::LinkOnceAnyLinkage;
    1617         875 :   case lltok::kw_linkonce_odr:
    1618         875 :     return GlobalValue::LinkOnceODRLinkage;
    1619         133 :   case lltok::kw_available_externally:
    1620         133 :     return GlobalValue::AvailableExternallyLinkage;
    1621         329 :   case lltok::kw_appending:
    1622         329 :     return GlobalValue::AppendingLinkage;
    1623        4756 :   case lltok::kw_common:
    1624        4756 :     return GlobalValue::CommonLinkage;
    1625         116 :   case lltok::kw_extern_weak:
    1626         116 :     return GlobalValue::ExternalWeakLinkage;
    1627        6527 :   case lltok::kw_external:
    1628        6527 :     return GlobalValue::ExternalLinkage;
    1629             :   }
    1630             : }
    1631             : 
    1632             : /// ParseOptionalLinkage
    1633             : ///   ::= /*empty*/
    1634             : ///   ::= 'private'
    1635             : ///   ::= 'internal'
    1636             : ///   ::= 'weak'
    1637             : ///   ::= 'weak_odr'
    1638             : ///   ::= 'linkonce'
    1639             : ///   ::= 'linkonce_odr'
    1640             : ///   ::= 'available_externally'
    1641             : ///   ::= 'appending'
    1642             : ///   ::= 'common'
    1643             : ///   ::= 'extern_weak'
    1644             : ///   ::= 'external'
    1645      312913 : bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage,
    1646             :                                     unsigned &Visibility,
    1647             :                                     unsigned &DLLStorageClass,
    1648             :                                     bool &DSOLocal) {
    1649      312913 :   Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
    1650      312913 :   if (HasLinkage)
    1651       19180 :     Lex.Lex();
    1652      312913 :   ParseOptionalDSOLocal(DSOLocal);
    1653      312913 :   ParseOptionalVisibility(Visibility);
    1654      312913 :   ParseOptionalDLLStorageClass(DLLStorageClass);
    1655             : 
    1656      312913 :   if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
    1657           2 :     return Error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
    1658             :   }
    1659             : 
    1660             :   return false;
    1661             : }
    1662             : 
    1663      312913 : void LLParser::ParseOptionalDSOLocal(bool &DSOLocal) {
    1664      312913 :   switch (Lex.getKind()) {
    1665      311944 :   default:
    1666      311944 :     DSOLocal = false;
    1667      311944 :     break;
    1668         821 :   case lltok::kw_dso_local:
    1669         821 :     DSOLocal = true;
    1670         821 :     Lex.Lex();
    1671             :     break;
    1672         148 :   case lltok::kw_dso_preemptable:
    1673         148 :     DSOLocal = false;
    1674         148 :     Lex.Lex();
    1675             :     break;
    1676             :   }
    1677      312913 : }
    1678             : 
    1679             : /// ParseOptionalVisibility
    1680             : ///   ::= /*empty*/
    1681             : ///   ::= 'default'
    1682             : ///   ::= 'hidden'
    1683             : ///   ::= 'protected'
    1684             : ///
    1685      312913 : void LLParser::ParseOptionalVisibility(unsigned &Res) {
    1686      312913 :   switch (Lex.getKind()) {
    1687      311928 :   default:
    1688      311928 :     Res = GlobalValue::DefaultVisibility;
    1689      311928 :     return;
    1690           9 :   case lltok::kw_default:
    1691           9 :     Res = GlobalValue::DefaultVisibility;
    1692           9 :     break;
    1693         867 :   case lltok::kw_hidden:
    1694         867 :     Res = GlobalValue::HiddenVisibility;
    1695         867 :     break;
    1696         109 :   case lltok::kw_protected:
    1697         109 :     Res = GlobalValue::ProtectedVisibility;
    1698         109 :     break;
    1699             :   }
    1700         985 :   Lex.Lex();
    1701             : }
    1702             : 
    1703             : /// ParseOptionalDLLStorageClass
    1704             : ///   ::= /*empty*/
    1705             : ///   ::= 'dllimport'
    1706             : ///   ::= 'dllexport'
    1707             : ///
    1708      312913 : void LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
    1709      312913 :   switch (Lex.getKind()) {
    1710      312472 :   default:
    1711      312472 :     Res = GlobalValue::DefaultStorageClass;
    1712      312472 :     return;
    1713         129 :   case lltok::kw_dllimport:
    1714         129 :     Res = GlobalValue::DLLImportStorageClass;
    1715         129 :     break;
    1716         312 :   case lltok::kw_dllexport:
    1717         312 :     Res = GlobalValue::DLLExportStorageClass;
    1718         312 :     break;
    1719             :   }
    1720         441 :   Lex.Lex();
    1721             : }
    1722             : 
    1723             : /// ParseOptionalCallingConv
    1724             : ///   ::= /*empty*/
    1725             : ///   ::= 'ccc'
    1726             : ///   ::= 'fastcc'
    1727             : ///   ::= 'intel_ocl_bicc'
    1728             : ///   ::= 'coldcc'
    1729             : ///   ::= 'x86_stdcallcc'
    1730             : ///   ::= 'x86_fastcallcc'
    1731             : ///   ::= 'x86_thiscallcc'
    1732             : ///   ::= 'x86_vectorcallcc'
    1733             : ///   ::= 'arm_apcscc'
    1734             : ///   ::= 'arm_aapcscc'
    1735             : ///   ::= 'arm_aapcs_vfpcc'
    1736             : ///   ::= 'msp430_intrcc'
    1737             : ///   ::= 'avr_intrcc'
    1738             : ///   ::= 'avr_signalcc'
    1739             : ///   ::= 'ptx_kernel'
    1740             : ///   ::= 'ptx_device'
    1741             : ///   ::= 'spir_func'
    1742             : ///   ::= 'spir_kernel'
    1743             : ///   ::= 'x86_64_sysvcc'
    1744             : ///   ::= 'win64cc'
    1745             : ///   ::= 'webkit_jscc'
    1746             : ///   ::= 'anyregcc'
    1747             : ///   ::= 'preserve_mostcc'
    1748             : ///   ::= 'preserve_allcc'
    1749             : ///   ::= 'ghccc'
    1750             : ///   ::= 'swiftcc'
    1751             : ///   ::= 'x86_intrcc'
    1752             : ///   ::= 'hhvmcc'
    1753             : ///   ::= 'hhvm_ccc'
    1754             : ///   ::= 'cxx_fast_tlscc'
    1755             : ///   ::= 'amdgpu_vs'
    1756             : ///   ::= 'amdgpu_ls'
    1757             : ///   ::= 'amdgpu_hs'
    1758             : ///   ::= 'amdgpu_es'
    1759             : ///   ::= 'amdgpu_gs'
    1760             : ///   ::= 'amdgpu_ps'
    1761             : ///   ::= 'amdgpu_cs'
    1762             : ///   ::= 'amdgpu_kernel'
    1763             : ///   ::= 'cc' UINT
    1764             : ///
    1765      418726 : bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
    1766      418726 :   switch (Lex.getKind()) {
    1767      393132 :   default:                       CC = CallingConv::C; return false;
    1768           3 :   case lltok::kw_ccc:            CC = CallingConv::C; break;
    1769        1739 :   case lltok::kw_fastcc:         CC = CallingConv::Fast; break;
    1770         116 :   case lltok::kw_coldcc:         CC = CallingConv::Cold; break;
    1771          87 :   case lltok::kw_x86_stdcallcc:  CC = CallingConv::X86_StdCall; break;
    1772         102 :   case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
    1773         200 :   case lltok::kw_x86_regcallcc:  CC = CallingConv::X86_RegCall; break;
    1774         335 :   case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
    1775          68 :   case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
    1776          40 :   case lltok::kw_arm_apcscc:     CC = CallingConv::ARM_APCS; break;
    1777         656 :   case lltok::kw_arm_aapcscc:    CC = CallingConv::ARM_AAPCS; break;
    1778         738 :   case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
    1779          13 :   case lltok::kw_msp430_intrcc:  CC = CallingConv::MSP430_INTR; break;
    1780           3 :   case lltok::kw_avr_intrcc:     CC = CallingConv::AVR_INTR; break;
    1781           3 :   case lltok::kw_avr_signalcc:   CC = CallingConv::AVR_SIGNAL; break;
    1782          21 :   case lltok::kw_ptx_kernel:     CC = CallingConv::PTX_Kernel; break;
    1783         185 :   case lltok::kw_ptx_device:     CC = CallingConv::PTX_Device; break;
    1784          23 :   case lltok::kw_spir_kernel:    CC = CallingConv::SPIR_KERNEL; break;
    1785          42 :   case lltok::kw_spir_func:      CC = CallingConv::SPIR_FUNC; break;
    1786          92 :   case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
    1787          11 :   case lltok::kw_x86_64_sysvcc:  CC = CallingConv::X86_64_SysV; break;
    1788          46 :   case lltok::kw_win64cc:        CC = CallingConv::Win64; break;
    1789          33 :   case lltok::kw_webkit_jscc:    CC = CallingConv::WebKit_JS; break;
    1790          84 :   case lltok::kw_anyregcc:       CC = CallingConv::AnyReg; break;
    1791          15 :   case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
    1792          11 :   case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
    1793          56 :   case lltok::kw_ghccc:          CC = CallingConv::GHC; break;
    1794         443 :   case lltok::kw_swiftcc:        CC = CallingConv::Swift; break;
    1795          30 :   case lltok::kw_x86_intrcc:     CC = CallingConv::X86_INTR; break;
    1796          27 :   case lltok::kw_hhvmcc:         CC = CallingConv::HHVM; break;
    1797          16 :   case lltok::kw_hhvm_ccc:       CC = CallingConv::HHVM_C; break;
    1798         108 :   case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break;
    1799         177 :   case lltok::kw_amdgpu_vs:      CC = CallingConv::AMDGPU_VS; break;
    1800           5 :   case lltok::kw_amdgpu_ls:      CC = CallingConv::AMDGPU_LS; break;
    1801          15 :   case lltok::kw_amdgpu_hs:      CC = CallingConv::AMDGPU_HS; break;
    1802           5 :   case lltok::kw_amdgpu_es:      CC = CallingConv::AMDGPU_ES; break;
    1803          69 :   case lltok::kw_amdgpu_gs:      CC = CallingConv::AMDGPU_GS; break;
    1804         954 :   case lltok::kw_amdgpu_ps:      CC = CallingConv::AMDGPU_PS; break;
    1805          34 :   case lltok::kw_amdgpu_cs:      CC = CallingConv::AMDGPU_CS; break;
    1806       18878 :   case lltok::kw_amdgpu_kernel:  CC = CallingConv::AMDGPU_KERNEL; break;
    1807         111 :   case lltok::kw_cc: {
    1808         111 :       Lex.Lex();
    1809         111 :       return ParseUInt32(CC);
    1810             :     }
    1811             :   }
    1812             : 
    1813       25483 :   Lex.Lex();
    1814       25483 :   return false;
    1815             : }
    1816             : 
    1817             : /// ParseMetadataAttachment
    1818             : ///   ::= !dbg !42
    1819       31434 : bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
    1820             :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
    1821             : 
    1822             :   std::string Name = Lex.getStrVal();
    1823       62868 :   Kind = M->getMDKindID(Name);
    1824       31434 :   Lex.Lex();
    1825             : 
    1826       62868 :   return ParseMDNode(MD);
    1827             : }
    1828             : 
    1829             : /// ParseInstructionMetadata
    1830             : ///   ::= !dbg !42 (',' !dbg !57)*
    1831       25243 : bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
    1832             :   do {
    1833       26598 :     if (Lex.getKind() != lltok::MetadataVar)
    1834           2 :       return TokError("expected metadata after comma");
    1835             : 
    1836             :     unsigned MDK;
    1837             :     MDNode *N;
    1838       26596 :     if (ParseMetadataAttachment(MDK, N))
    1839             :       return true;
    1840             : 
    1841       26594 :     Inst.setMetadata(MDK, N);
    1842       26594 :     if (MDK == LLVMContext::MD_tbaa)
    1843        4433 :       InstsWithTBAATag.push_back(&Inst);
    1844             : 
    1845             :     // If this is the end of the list, we're done.
    1846             :   } while (EatIfPresent(lltok::comma));
    1847             :   return false;
    1848             : }
    1849             : 
    1850             : /// ParseGlobalObjectMetadataAttachment
    1851             : ///   ::= !dbg !57
    1852        4808 : bool LLParser::ParseGlobalObjectMetadataAttachment(GlobalObject &GO) {
    1853             :   unsigned MDK;
    1854             :   MDNode *N;
    1855        4808 :   if (ParseMetadataAttachment(MDK, N))
    1856             :     return true;
    1857             : 
    1858        4808 :   GO.addMetadata(MDK, *N);
    1859        4808 :   return false;
    1860             : }
    1861             : 
    1862             : /// ParseOptionalFunctionMetadata
    1863             : ///   ::= (!dbg !57)*
    1864      225979 : bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
    1865      229331 :   while (Lex.getKind() == lltok::MetadataVar)
    1866        3352 :     if (ParseGlobalObjectMetadataAttachment(F))
    1867             :       return true;
    1868             :   return false;
    1869             : }
    1870             : 
    1871             : /// ParseOptionalAlignment
    1872             : ///   ::= /* empty */
    1873             : ///   ::= 'align' 4
    1874      536417 : bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
    1875      536417 :   Alignment = 0;
    1876             :   if (!EatIfPresent(lltok::kw_align))
    1877             :     return false;
    1878      251492 :   LocTy AlignLoc = Lex.getLoc();
    1879      251492 :   if (ParseUInt32(Alignment)) return true;
    1880      251492 :   if (!isPowerOf2_32(Alignment))
    1881           1 :     return Error(AlignLoc, "alignment is not a power of two");
    1882      251491 :   if (Alignment > Value::MaximumAlignment)
    1883           3 :     return Error(AlignLoc, "huge alignments are not supported yet");
    1884             :   return false;
    1885             : }
    1886             : 
    1887             : /// ParseOptionalDerefAttrBytes
    1888             : ///   ::= /* empty */
    1889             : ///   ::= AttrKind '(' 4 ')'
    1890             : ///
    1891             : /// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
    1892         496 : bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
    1893             :                                            uint64_t &Bytes) {
    1894             :   assert((AttrKind == lltok::kw_dereferenceable ||
    1895             :           AttrKind == lltok::kw_dereferenceable_or_null) &&
    1896             :          "contract!");
    1897             : 
    1898         496 :   Bytes = 0;
    1899             :   if (!EatIfPresent(AttrKind))
    1900             :     return false;
    1901         496 :   LocTy ParenLoc = Lex.getLoc();
    1902             :   if (!EatIfPresent(lltok::lparen))
    1903           0 :     return Error(ParenLoc, "expected '('");
    1904         496 :   LocTy DerefLoc = Lex.getLoc();
    1905         496 :   if (ParseUInt64(Bytes)) return true;
    1906         496 :   ParenLoc = Lex.getLoc();
    1907             :   if (!EatIfPresent(lltok::rparen))
    1908           0 :     return Error(ParenLoc, "expected ')'");
    1909         496 :   if (!Bytes)
    1910           0 :     return Error(DerefLoc, "dereferenceable bytes must be non-zero");
    1911             :   return false;
    1912             : }
    1913             : 
    1914             : /// ParseOptionalCommaAlign
    1915             : ///   ::=
    1916             : ///   ::= ',' align 4
    1917             : ///
    1918             : /// This returns with AteExtraComma set to true if it ate an excess comma at the
    1919             : /// end.
    1920      278899 : bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
    1921             :                                        bool &AteExtraComma) {
    1922      278899 :   AteExtraComma = false;
    1923             :   while (EatIfPresent(lltok::comma)) {
    1924             :     // Metadata at the end is an early exit.
    1925      196855 :     if (Lex.getKind() == lltok::MetadataVar) {
    1926        9224 :       AteExtraComma = true;
    1927        9224 :       return false;
    1928             :     }
    1929             : 
    1930      187631 :     if (Lex.getKind() != lltok::kw_align)
    1931           0 :       return Error(Lex.getLoc(), "expected metadata or 'align'");
    1932             : 
    1933      187631 :     if (ParseOptionalAlignment(Alignment)) return true;
    1934             :   }
    1935             : 
    1936             :   return false;
    1937             : }
    1938             : 
    1939             : /// ParseOptionalCommaAddrSpace
    1940             : ///   ::=
    1941             : ///   ::= ',' addrspace(1)
    1942             : ///
    1943             : /// This returns with AteExtraComma set to true if it ate an excess comma at the
    1944             : /// end.
    1945       43719 : bool LLParser::ParseOptionalCommaAddrSpace(unsigned &AddrSpace,
    1946             :                                            LocTy &Loc,
    1947             :                                            bool &AteExtraComma) {
    1948       43719 :   AteExtraComma = false;
    1949             :   while (EatIfPresent(lltok::comma)) {
    1950             :     // Metadata at the end is an early exit.
    1951         577 :     if (Lex.getKind() == lltok::MetadataVar) {
    1952          45 :       AteExtraComma = true;
    1953          45 :       return false;
    1954             :     }
    1955             : 
    1956         532 :     Loc = Lex.getLoc();
    1957         532 :     if (Lex.getKind() != lltok::kw_addrspace)
    1958           0 :       return Error(Lex.getLoc(), "expected metadata or 'addrspace'");
    1959             : 
    1960         532 :     if (ParseOptionalAddrSpace(AddrSpace))
    1961             :       return true;
    1962             :   }
    1963             : 
    1964             :   return false;
    1965             : }
    1966             : 
    1967          25 : bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
    1968             :                                        Optional<unsigned> &HowManyArg) {
    1969          25 :   Lex.Lex();
    1970             : 
    1971          25 :   auto StartParen = Lex.getLoc();
    1972             :   if (!EatIfPresent(lltok::lparen))
    1973           0 :     return Error(StartParen, "expected '('");
    1974             : 
    1975          25 :   if (ParseUInt32(BaseSizeArg))
    1976             :     return true;
    1977             : 
    1978             :   if (EatIfPresent(lltok::comma)) {
    1979          12 :     auto HowManyAt = Lex.getLoc();
    1980             :     unsigned HowMany;
    1981          12 :     if (ParseUInt32(HowMany))
    1982           1 :       return true;
    1983          12 :     if (HowMany == BaseSizeArg)
    1984           1 :       return Error(HowManyAt,
    1985             :                    "'allocsize' indices can't refer to the same parameter");
    1986             :     HowManyArg = HowMany;
    1987             :   } else
    1988             :     HowManyArg = None;
    1989             : 
    1990          24 :   auto EndParen = Lex.getLoc();
    1991             :   if (!EatIfPresent(lltok::rparen))
    1992           0 :     return Error(EndParen, "expected ')'");
    1993             :   return false;
    1994             : }
    1995             : 
    1996             : /// ParseScopeAndOrdering
    1997             : ///   if isAtomic: ::= SyncScope? AtomicOrdering
    1998             : ///   else: ::=
    1999             : ///
    2000             : /// This sets Scope and Ordering to the parsed values.
    2001      286508 : bool LLParser::ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
    2002             :                                      AtomicOrdering &Ordering) {
    2003      286508 :   if (!isAtomic)
    2004             :     return false;
    2005             : 
    2006        9642 :   return ParseScope(SSID) || ParseOrdering(Ordering);
    2007             : }
    2008             : 
    2009             : /// ParseScope
    2010             : ///   ::= syncscope("singlethread" | "<target scope>")?
    2011             : ///
    2012             : /// This sets synchronization scope ID to the ID of the parsed value.
    2013        9642 : bool LLParser::ParseScope(SyncScope::ID &SSID) {
    2014        9642 :   SSID = SyncScope::System;
    2015             :   if (EatIfPresent(lltok::kw_syncscope)) {
    2016         875 :     auto StartParenAt = Lex.getLoc();
    2017             :     if (!EatIfPresent(lltok::lparen))
    2018           0 :       return Error(StartParenAt, "Expected '(' in syncscope");
    2019             : 
    2020             :     std::string SSN;
    2021         875 :     auto SSNAt = Lex.getLoc();
    2022         875 :     if (ParseStringConstant(SSN))
    2023           0 :       return Error(SSNAt, "Expected synchronization scope name");
    2024             : 
    2025         875 :     auto EndParenAt = Lex.getLoc();
    2026             :     if (!EatIfPresent(lltok::rparen))
    2027           0 :       return Error(EndParenAt, "Expected ')' in syncscope");
    2028             : 
    2029        1750 :     SSID = Context.getOrInsertSyncScopeID(SSN);
    2030             :   }
    2031             : 
    2032             :   return false;
    2033             : }
    2034             : 
    2035             : /// ParseOrdering
    2036             : ///   ::= AtomicOrdering
    2037             : ///
    2038             : /// This sets Ordering to the parsed value.
    2039       10707 : bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
    2040       10707 :   switch (Lex.getKind()) {
    2041           0 :   default: return TokError("Expected ordering on atomic instruction");
    2042         469 :   case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break;
    2043        2498 :   case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break;
    2044             :   // Not specified yet:
    2045             :   // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
    2046        1412 :   case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break;
    2047         908 :   case lltok::kw_release: Ordering = AtomicOrdering::Release; break;
    2048         532 :   case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break;
    2049        4888 :   case lltok::kw_seq_cst:
    2050        4888 :     Ordering = AtomicOrdering::SequentiallyConsistent;
    2051        4888 :     break;
    2052             :   }
    2053       10707 :   Lex.Lex();
    2054       10707 :   return false;
    2055             : }
    2056             : 
    2057             : /// ParseOptionalStackAlignment
    2058             : ///   ::= /* empty */
    2059             : ///   ::= 'alignstack' '(' 4 ')'
    2060          34 : bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
    2061          34 :   Alignment = 0;
    2062             :   if (!EatIfPresent(lltok::kw_alignstack))
    2063             :     return false;
    2064          34 :   LocTy ParenLoc = Lex.getLoc();
    2065             :   if (!EatIfPresent(lltok::lparen))
    2066           0 :     return Error(ParenLoc, "expected '('");
    2067          34 :   LocTy AlignLoc = Lex.getLoc();
    2068          34 :   if (ParseUInt32(Alignment)) return true;
    2069          34 :   ParenLoc = Lex.getLoc();
    2070             :   if (!EatIfPresent(lltok::rparen))
    2071           0 :     return Error(ParenLoc, "expected ')'");
    2072          34 :   if (!isPowerOf2_32(Alignment))
    2073           0 :     return Error(AlignLoc, "stack alignment is not a power of two");
    2074             :   return false;
    2075             : }
    2076             : 
    2077             : /// ParseIndexList - This parses the index list for an insert/extractvalue
    2078             : /// instruction.  This sets AteExtraComma in the case where we eat an extra
    2079             : /// comma at the end of the line and find that it is followed by metadata.
    2080             : /// Clients that don't allow metadata can call the version of this function that
    2081             : /// only takes one argument.
    2082             : ///
    2083             : /// ParseIndexList
    2084             : ///    ::=  (',' uint32)+
    2085             : ///
    2086        7546 : bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
    2087             :                               bool &AteExtraComma) {
    2088        7546 :   AteExtraComma = false;
    2089             : 
    2090        7546 :   if (Lex.getKind() != lltok::comma)
    2091           0 :     return TokError("expected ',' as start of index list");
    2092             : 
    2093        7793 :   while (EatIfPresent(lltok::comma)) {
    2094        7861 :     if (Lex.getKind() == lltok::MetadataVar) {
    2095          69 :       if (Indices.empty()) return TokError("expected index");
    2096          67 :       AteExtraComma = true;
    2097          67 :       return false;
    2098             :     }
    2099        7793 :     unsigned Idx = 0;
    2100        7793 :     if (ParseUInt32(Idx)) return true;
    2101        7793 :     Indices.push_back(Idx);
    2102             :   }
    2103             : 
    2104             :   return false;
    2105             : }
    2106             : 
    2107             : //===----------------------------------------------------------------------===//
    2108             : // Type Parsing.
    2109             : //===----------------------------------------------------------------------===//
    2110             : 
    2111             : /// ParseType - Parse a type.
    2112     5765353 : bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
    2113     5765353 :   SMLoc TypeLoc = Lex.getLoc();
    2114     5765353 :   switch (Lex.getKind()) {
    2115             :   default:
    2116           5 :     return TokError(Msg);
    2117     4390267 :   case lltok::Type:
    2118             :     // Type ::= 'float' | 'void' (etc)
    2119     4390267 :     Result = Lex.getTyVal();
    2120     4390267 :     Lex.Lex();
    2121             :     break;
    2122       22032 :   case lltok::lbrace:
    2123             :     // Type ::= StructType
    2124       22032 :     if (ParseAnonStructType(Result, false))
    2125             :       return true;
    2126             :     break;
    2127      124068 :   case lltok::lsquare:
    2128             :     // Type ::= '[' ... ']'
    2129      124068 :     Lex.Lex(); // eat the lsquare.
    2130      124068 :     if (ParseArrayVectorType(Result, false))
    2131             :       return true;
    2132             :     break;
    2133     1143274 :   case lltok::less: // Either vector or packed struct.
    2134             :     // Type ::= '<' ... '>'
    2135     1143274 :     Lex.Lex();
    2136     1143274 :     if (Lex.getKind() == lltok::lbrace) {
    2137        1490 :       if (ParseAnonStructType(Result, true) ||
    2138         745 :           ParseToken(lltok::greater, "expected '>' at end of packed struct"))
    2139             :         return true;
    2140     1142529 :     } else if (ParseArrayVectorType(Result, true))
    2141             :       return true;
    2142             :     break;
    2143       83402 :   case lltok::LocalVar: {
    2144             :     // Type ::= %foo
    2145       83402 :     std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
    2146             : 
    2147             :     // If the type hasn't been defined yet, create a forward definition and
    2148             :     // remember where that forward def'n was seen (in case it never is defined).
    2149       83402 :     if (!Entry.first) {
    2150        4356 :       Entry.first = StructType::create(Context, Lex.getStrVal());
    2151        4356 :       Entry.second = Lex.getLoc();
    2152             :     }
    2153       83402 :     Result = Entry.first;
    2154       83402 :     Lex.Lex();
    2155             :     break;
    2156             :   }
    2157             : 
    2158        2305 :   case lltok::LocalVarID: {
    2159             :     // Type ::= %4
    2160        2305 :     std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
    2161             : 
    2162             :     // If the type hasn't been defined yet, create a forward definition and
    2163             :     // remember where that forward def'n was seen (in case it never is defined).
    2164        2305 :     if (!Entry.first) {
    2165         180 :       Entry.first = StructType::create(Context);
    2166         180 :       Entry.second = Lex.getLoc();
    2167             :     }
    2168        2305 :     Result = Entry.first;
    2169        2305 :     Lex.Lex();
    2170             :     break;
    2171             :   }
    2172             :   }
    2173             : 
    2174             :   // Parse the type suffixes.
    2175             :   while (true) {
    2176     6521029 :     switch (Lex.getKind()) {
    2177             :     // End of type.
    2178     5765346 :     default:
    2179    10882728 :       if (!AllowVoid && Result->isVoidTy())
    2180           0 :         return Error(TypeLoc, "void type only allowed for function results");
    2181             :       return false;
    2182             : 
    2183             :     // Type ::= Type '*'
    2184      642536 :     case lltok::star:
    2185     1285072 :       if (Result->isLabelTy())
    2186           1 :         return TokError("basic block pointers are invalid");
    2187      642535 :       if (Result->isVoidTy())
    2188           0 :         return TokError("pointers to void are invalid - use i8* instead");
    2189      642535 :       if (!PointerType::isValidElementType(Result))
    2190           0 :         return TokError("pointer to this type is invalid");
    2191     1285070 :       Result = PointerType::getUnqual(Result);
    2192      642535 :       Lex.Lex();
    2193             :       break;
    2194             : 
    2195             :     // Type ::= Type 'addrspace' '(' uint32 ')' '*'
    2196       94463 :     case lltok::kw_addrspace: {
    2197      188926 :       if (Result->isLabelTy())
    2198           0 :         return TokError("basic block pointers are invalid");
    2199       94463 :       if (Result->isVoidTy())
    2200           0 :         return TokError("pointers to void are invalid; use i8* instead");
    2201       94463 :       if (!PointerType::isValidElementType(Result))
    2202           0 :         return TokError("pointer to this type is invalid");
    2203             :       unsigned AddrSpace;
    2204      188926 :       if (ParseOptionalAddrSpace(AddrSpace) ||
    2205       94463 :           ParseToken(lltok::star, "expected '*' in address space"))
    2206             :         return true;
    2207             : 
    2208       94463 :       Result = PointerType::get(Result, AddrSpace);
    2209       94463 :       break;
    2210             :     }
    2211             : 
    2212             :     /// Types '(' ArgTypeListI ')' OptFuncAttrs
    2213       18684 :     case lltok::lparen:
    2214       18684 :       if (ParseFunctionType(Result))
    2215             :         return true;
    2216             :       break;
    2217             :     }
    2218             :   }
    2219             : }
    2220             : 
    2221             : /// ParseParameterList
    2222             : ///    ::= '(' ')'
    2223             : ///    ::= '(' Arg (',' Arg)* ')'
    2224             : ///  Arg
    2225             : ///    ::= Type OptionalAttributes Value OptionalAttributes
    2226      133654 : bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
    2227             :                                   PerFunctionState &PFS, bool IsMustTailCall,
    2228             :                                   bool InVarArgsFunc) {
    2229      133654 :   if (ParseToken(lltok::lparen, "expected '(' in call"))
    2230             :     return true;
    2231             : 
    2232      388044 :   while (Lex.getKind() != lltok::rparen) {
    2233             :     // If this isn't the first argument, we need a comma.
    2234      400533 :     if (!ArgList.empty() &&
    2235      146087 :         ParseToken(lltok::comma, "expected ',' in argument list"))
    2236          56 :       return true;
    2237             : 
    2238             :     // Parse an ellipsis if this is a musttail call in a variadic function.
    2239      254446 :     if (Lex.getKind() == lltok::dotdotdot) {
    2240             :       const char *Msg = "unexpected ellipsis in argument list for ";
    2241          50 :       if (!IsMustTailCall)
    2242           0 :         return TokError(Twine(Msg) + "non-musttail call");
    2243          50 :       if (!InVarArgsFunc)
    2244           1 :         return TokError(Twine(Msg) + "musttail call in non-varargs function");
    2245          49 :       Lex.Lex();  // Lex the '...', it is purely for readability.
    2246          49 :       return ParseToken(lltok::rparen, "expected ')' at end of argument list");
    2247             :     }
    2248             : 
    2249             :     // Parse the argument.
    2250             :     LocTy ArgLoc;
    2251      254396 :     Type *ArgTy = nullptr;
    2252             :     AttrBuilder ArgAttrs;
    2253             :     Value *V;
    2254      254396 :     if (ParseType(ArgTy, ArgLoc))
    2255             :       return true;
    2256             : 
    2257      508792 :     if (ArgTy->isMetadataTy()) {
    2258        8688 :       if (ParseMetadataAsValue(V, PFS))
    2259             :         return true;
    2260             :     } else {
    2261             :       // Otherwise, handle normal operands.
    2262      491415 :       if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
    2263             :         return true;
    2264             :     }
    2265      508780 :     ArgList.push_back(ParamInfo(
    2266             :         ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
    2267             :   }
    2268             : 
    2269      133598 :   if (IsMustTailCall && InVarArgsFunc)
    2270           1 :     return TokError("expected '...' at end of argument list for musttail call "
    2271             :                     "in varargs function");
    2272             : 
    2273      133597 :   Lex.Lex();  // Lex the ')'.
    2274      133597 :   return false;
    2275             : }
    2276             : 
    2277             : /// ParseOptionalOperandBundles
    2278             : ///    ::= /*empty*/
    2279             : ///    ::= '[' OperandBundle [, OperandBundle ]* ']'
    2280             : ///
    2281             : /// OperandBundle
    2282             : ///    ::= bundle-tag '(' ')'
    2283             : ///    ::= bundle-tag '(' Type Value [, Type Value ]* ')'
    2284             : ///
    2285             : /// bundle-tag ::= String Constant
    2286      133646 : bool LLParser::ParseOptionalOperandBundles(
    2287             :     SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
    2288      133646 :   LocTy BeginLoc = Lex.getLoc();
    2289             :   if (!EatIfPresent(lltok::lsquare))
    2290             :     return false;
    2291             : 
    2292        2206 :   while (Lex.getKind() != lltok::rsquare) {
    2293             :     // If this isn't the first operand bundle, we need a comma.
    2294        1163 :     if (!BundleList.empty() &&
    2295          40 :         ParseToken(lltok::comma, "expected ',' in input list"))
    2296           0 :       return true;
    2297             : 
    2298             :     std::string Tag;
    2299        1123 :     if (ParseStringConstant(Tag))
    2300             :       return true;
    2301             : 
    2302        1123 :     if (ParseToken(lltok::lparen, "expected '(' in operand bundle"))
    2303             :       return true;
    2304             : 
    2305             :     std::vector<Value *> Inputs;
    2306        3331 :     while (Lex.getKind() != lltok::rparen) {
    2307             :       // If this isn't the first input, we need a comma.
    2308        1648 :       if (!Inputs.empty() &&
    2309         544 :           ParseToken(lltok::comma, "expected ',' in input list"))
    2310           0 :         return true;
    2311             : 
    2312        1104 :       Type *Ty = nullptr;
    2313        1104 :       Value *Input = nullptr;
    2314        2208 :       if (ParseType(Ty) || ParseValue(Ty, Input, PFS))
    2315             :         return true;
    2316        1104 :       Inputs.push_back(Input);
    2317             :     }
    2318             : 
    2319        1123 :     BundleList.emplace_back(std::move(Tag), std::move(Inputs));
    2320             : 
    2321             :     Lex.Lex(); // Lex the ')'.
    2322             :   }
    2323             : 
    2324        1083 :   if (BundleList.empty())
    2325           0 :     return Error(BeginLoc, "operand bundle set must not be empty");
    2326             : 
    2327             :   Lex.Lex(); // Lex the ']'.
    2328        1083 :   return false;
    2329             : }
    2330             : 
    2331             : /// ParseArgumentList - Parse the argument list for a function type or function
    2332             : /// prototype.
    2333             : ///   ::= '(' ArgTypeListI ')'
    2334             : /// ArgTypeListI
    2335             : ///   ::= /*empty*/
    2336             : ///   ::= '...'
    2337             : ///   ::= ArgTypeList ',' '...'
    2338             : ///   ::= ArgType (',' ArgType)*
    2339             : ///
    2340      303750 : bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
    2341             :                                  bool &isVarArg){
    2342      303750 :   isVarArg = false;
    2343             :   assert(Lex.getKind() == lltok::lparen);
    2344      303750 :   Lex.Lex(); // eat the (.
    2345             : 
    2346      303750 :   if (Lex.getKind() == lltok::rparen) {
    2347             :     // empty
    2348      257388 :   } else if (Lex.getKind() == lltok::dotdotdot) {
    2349        6536 :     isVarArg = true;
    2350             :     Lex.Lex();
    2351             :   } else {
    2352      250852 :     LocTy TypeLoc = Lex.getLoc();
    2353      250852 :     Type *ArgTy = nullptr;
    2354             :     AttrBuilder Attrs;
    2355             :     std::string Name;
    2356             : 
    2357      501703 :     if (ParseType(ArgTy) ||
    2358      250851 :         ParseOptionalParamAttrs(Attrs)) return true;
    2359             : 
    2360      501700 :     if (ArgTy->isVoidTy())
    2361           0 :       return Error(TypeLoc, "argument can not have void type");
    2362             : 
    2363      250850 :     if (Lex.getKind() == lltok::LocalVar) {
    2364             :       Name = Lex.getStrVal();
    2365             :       Lex.Lex();
    2366             :     }
    2367             : 
    2368      250850 :     if (!FunctionType::isValidArgumentType(ArgTy))
    2369           0 :       return Error(TypeLoc, "invalid type for function argument");
    2370             : 
    2371      250850 :     ArgList.emplace_back(TypeLoc, ArgTy,
    2372      501700 :                          AttributeSet::get(ArgTy->getContext(), Attrs),
    2373             :                          std::move(Name));
    2374             : 
    2375      257565 :     while (EatIfPresent(lltok::comma)) {
    2376             :       // Handle ... at end of arg list.
    2377             :       if (EatIfPresent(lltok::dotdotdot)) {
    2378        4354 :         isVarArg = true;
    2379        4354 :         break;
    2380             :       }
    2381             : 
    2382             :       // Otherwise must be an argument type.
    2383      257565 :       TypeLoc = Lex.getLoc();
    2384      257565 :       if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
    2385             : 
    2386      515130 :       if (ArgTy->isVoidTy())
    2387           0 :         return Error(TypeLoc, "argument can not have void type");
    2388             : 
    2389      257565 :       if (Lex.getKind() == lltok::LocalVar) {
    2390             :         Name = Lex.getStrVal();
    2391             :         Lex.Lex();
    2392             :       } else {
    2393             :         Name = "";
    2394             :       }
    2395             : 
    2396      257565 :       if (!ArgTy->isFirstClassType())
    2397           0 :         return Error(TypeLoc, "invalid type for function argument");
    2398             : 
    2399      257565 :       ArgList.emplace_back(TypeLoc, ArgTy,
    2400      515130 :                            AttributeSet::get(ArgTy->getContext(), Attrs),
    2401             :                            std::move(Name));
    2402             :     }
    2403             :   }
    2404             : 
    2405      303748 :   return ParseToken(lltok::rparen, "expected ')' at end of argument list");
    2406             : }
    2407             : 
    2408             : /// ParseFunctionType
    2409             : ///  ::= Type ArgumentList OptionalAttrs
    2410       18684 : bool LLParser::ParseFunctionType(Type *&Result) {
    2411             :   assert(Lex.getKind() == lltok::lparen);
    2412             : 
    2413       18684 :   if (!FunctionType::isValidReturnType(Result))
    2414           0 :     return TokError("invalid function return type");
    2415             : 
    2416       18684 :   SmallVector<ArgInfo, 8> ArgList;
    2417             :   bool isVarArg;
    2418       18684 :   if (ParseArgumentList(ArgList, isVarArg))
    2419             :     return true;
    2420             : 
    2421             :   // Reject names on the arguments lists.
    2422       36403 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    2423       35438 :     if (!ArgList[i].Name.empty())
    2424           0 :       return Error(ArgList[i].Loc, "argument name invalid in function type");
    2425       17719 :     if (ArgList[i].Attrs.hasAttributes())
    2426           0 :       return Error(ArgList[i].Loc,
    2427             :                    "argument attributes invalid in function type");
    2428             :   }
    2429             : 
    2430             :   SmallVector<Type*, 16> ArgListTy;
    2431       54122 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
    2432       35438 :     ArgListTy.push_back(ArgList[i].Ty);
    2433             : 
    2434       37368 :   Result = FunctionType::get(Result, ArgListTy, isVarArg);
    2435             :   return false;
    2436             : }
    2437             : 
    2438             : /// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
    2439             : /// other structs.
    2440       22777 : bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
    2441             :   SmallVector<Type*, 8> Elts;
    2442       22777 :   if (ParseStructBody(Elts)) return true;
    2443             : 
    2444       45554 :   Result = StructType::get(Context, Elts, Packed);
    2445       22777 :   return false;
    2446             : }
    2447             : 
    2448             : /// ParseStructDefinition - Parse a struct in a 'type' definition.
    2449       12127 : bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
    2450             :                                      std::pair<Type*, LocTy> &Entry,
    2451             :                                      Type *&ResultTy) {
    2452             :   // If the type was already defined, diagnose the redefinition.
    2453       16658 :   if (Entry.first && !Entry.second.isValid())
    2454           0 :     return Error(TypeLoc, "redefinition of type");
    2455             : 
    2456             :   // If we have opaque, just return without filling in the definition for the
    2457             :   // struct.  This counts as a definition as far as the .ll file goes.
    2458             :   if (EatIfPresent(lltok::kw_opaque)) {
    2459             :     // This type is being defined, so clear the location to indicate this.
    2460         718 :     Entry.second = SMLoc();
    2461             : 
    2462             :     // If this type number has never been uttered, create it.
    2463         718 :     if (!Entry.first)
    2464         400 :       Entry.first = StructType::create(Context, Name);
    2465         718 :     ResultTy = Entry.first;
    2466         718 :     return false;
    2467             :   }
    2468             : 
    2469             :   // If the type starts with '<', then it is either a packed struct or a vector.
    2470             :   bool isPacked = EatIfPresent(lltok::less);
    2471             : 
    2472             :   // If we don't have a struct, then we have a random type alias, which we
    2473             :   // accept for compatibility with old files.  These types are not allowed to be
    2474             :   // forward referenced and not allowed to be recursive.
    2475       11409 :   if (Lex.getKind() != lltok::lbrace) {
    2476         444 :     if (Entry.first)
    2477           0 :       return Error(TypeLoc, "forward references to non-struct type");
    2478             : 
    2479         444 :     ResultTy = nullptr;
    2480         444 :     if (isPacked)
    2481         374 :       return ParseArrayVectorType(ResultTy, true);
    2482          70 :     return ParseType(ResultTy);
    2483             :   }
    2484             : 
    2485             :   // This type is being defined, so clear the location to indicate this.
    2486       10965 :   Entry.second = SMLoc();
    2487             : 
    2488             :   // If this type number has never been uttered, create it.
    2489       10965 :   if (!Entry.first)
    2490        6752 :     Entry.first = StructType::create(Context, Name);
    2491             : 
    2492       10965 :   StructType *STy = cast<StructType>(Entry.first);
    2493             : 
    2494             :   SmallVector<Type*, 8> Body;
    2495       10965 :   if (ParseStructBody(Body) ||
    2496         755 :       (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
    2497             :     return true;
    2498             : 
    2499       21930 :   STy->setBody(Body, isPacked);
    2500       10965 :   ResultTy = STy;
    2501       10965 :   return false;
    2502             : }
    2503             : 
    2504             : /// ParseStructType: Handles packed and unpacked types.  </> parsed elsewhere.
    2505             : ///   StructType
    2506             : ///     ::= '{' '}'
    2507             : ///     ::= '{' Type (',' Type)* '}'
    2508             : ///     ::= '<' '{' '}' '>'
    2509             : ///     ::= '<' '{' Type (',' Type)* '}' '>'
    2510       33742 : bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
    2511             :   assert(Lex.getKind() == lltok::lbrace);
    2512       33742 :   Lex.Lex(); // Consume the '{'
    2513             : 
    2514             :   // Handle the empty struct.
    2515             :   if (EatIfPresent(lltok::rbrace))
    2516             :     return false;
    2517             : 
    2518       33096 :   LocTy EltTyLoc = Lex.getLoc();
    2519       33096 :   Type *Ty = nullptr;
    2520       33096 :   if (ParseType(Ty)) return true;
    2521       33096 :   Body.push_back(Ty);
    2522             : 
    2523       33096 :   if (!StructType::isValidElementType(Ty))
    2524           0 :     return Error(EltTyLoc, "invalid element type for struct");
    2525             : 
    2526       77679 :   while (EatIfPresent(lltok::comma)) {
    2527       77679 :     EltTyLoc = Lex.getLoc();
    2528       77679 :     if (ParseType(Ty)) return true;
    2529             : 
    2530       77679 :     if (!StructType::isValidElementType(Ty))
    2531           0 :       return Error(EltTyLoc, "invalid element type for struct");
    2532             : 
    2533       77679 :     Body.push_back(Ty);
    2534             :   }
    2535             : 
    2536       33096 :   return ParseToken(lltok::rbrace, "expected '}' at end of struct");
    2537             : }
    2538             : 
    2539             : /// ParseArrayVectorType - Parse an array or vector type, assuming the first
    2540             : /// token has already been consumed.
    2541             : ///   Type
    2542             : ///     ::= '[' APSINTVAL 'x' Types ']'
    2543             : ///     ::= '<' APSINTVAL 'x' Types '>'
    2544     1266971 : bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
    2545     2533942 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
    2546     1266971 :       Lex.getAPSIntVal().getBitWidth() > 64)
    2547           0 :     return TokError("expected number in address space");
    2548             : 
    2549     1266971 :   LocTy SizeLoc = Lex.getLoc();
    2550             :   uint64_t Size = Lex.getAPSIntVal().getZExtValue();
    2551     1266971 :   Lex.Lex();
    2552             : 
    2553     1266971 :   if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
    2554             :       return true;
    2555             : 
    2556     1266971 :   LocTy TypeLoc = Lex.getLoc();
    2557     1266971 :   Type *EltTy = nullptr;
    2558     1266971 :   if (ParseType(EltTy)) return true;
    2559             : 
    2560     1266971 :   if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
    2561             :                  "expected end of sequential type"))
    2562             :     return true;
    2563             : 
    2564     1266971 :   if (isVector) {
    2565     1142903 :     if (Size == 0)
    2566           0 :       return Error(SizeLoc, "zero element vector is illegal");
    2567     1142903 :     if ((unsigned)Size != Size)
    2568           0 :       return Error(SizeLoc, "size too large for vector");
    2569     1142903 :     if (!VectorType::isValidElementType(EltTy))
    2570           0 :       return Error(TypeLoc, "invalid vector element type");
    2571     1142903 :     Result = VectorType::get(EltTy, unsigned(Size));
    2572             :   } else {
    2573      124068 :     if (!ArrayType::isValidElementType(EltTy))
    2574           1 :       return Error(TypeLoc, "invalid array element type");
    2575      124067 :     Result = ArrayType::get(EltTy, Size);
    2576             :   }
    2577             :   return false;
    2578             : }
    2579             : 
    2580             : //===----------------------------------------------------------------------===//
    2581             : // Function Semantic Analysis.
    2582             : //===----------------------------------------------------------------------===//
    2583             : 
    2584      225979 : LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
    2585      225979 :                                              int functionNumber)
    2586      677937 :   : P(p), F(f), FunctionNumber(functionNumber) {
    2587             : 
    2588             :   // Insert unnamed arguments into the NumberedVals list.
    2589      606208 :   for (Argument &A : F.args())
    2590      380229 :     if (!A.hasName())
    2591        7356 :       NumberedVals.push_back(&A);
    2592      225979 : }
    2593             : 
    2594      451958 : LLParser::PerFunctionState::~PerFunctionState() {
    2595             :   // If there were any forward referenced non-basicblock values, delete them.
    2596             : 
    2597      225988 :   for (const auto &P : ForwardRefVals) {
    2598          18 :     if (isa<BasicBlock>(P.second.first))
    2599           3 :       continue;
    2600           6 :     P.second.first->replaceAllUsesWith(
    2601           6 :         UndefValue::get(P.second.first->getType()));
    2602           6 :     P.second.first->deleteValue();
    2603             :   }
    2604             : 
    2605      225980 :   for (const auto &P : ForwardRefValIDs) {
    2606           2 :     if (isa<BasicBlock>(P.second.first))
    2607           0 :       continue;
    2608           1 :     P.second.first->replaceAllUsesWith(
    2609           1 :         UndefValue::get(P.second.first->getType()));
    2610           1 :     P.second.first->deleteValue();
    2611             :   }
    2612      225979 : }
    2613             : 
    2614      225909 : bool LLParser::PerFunctionState::FinishFunction() {
    2615      225909 :   if (!ForwardRefVals.empty())
    2616           4 :     return P.Error(ForwardRefVals.begin()->second.second,
    2617           6 :                    "use of undefined value '%" + ForwardRefVals.begin()->first +
    2618             :                    "'");
    2619      225907 :   if (!ForwardRefValIDs.empty())
    2620           0 :     return P.Error(ForwardRefValIDs.begin()->second.second,
    2621           0 :                    "use of undefined value '%" +
    2622           0 :                    Twine(ForwardRefValIDs.begin()->first) + "'");
    2623             :   return false;
    2624             : }
    2625             : 
    2626     1603462 : static bool isValidVariableType(Module *M, Type *Ty, Value *Val, bool IsCall) {
    2627     1603462 :   if (Val->getType() == Ty)
    2628             :     return true;
    2629             :   // For calls we also accept variables in the program address space
    2630          18 :   if (IsCall && isa<PointerType>(Ty)) {
    2631          12 :     Type *TyInProgAS = cast<PointerType>(Ty)->getElementType()->getPointerTo(
    2632          12 :         M->getDataLayout().getProgramAddressSpace());
    2633           6 :     if (Val->getType() == TyInProgAS)
    2634             :       return true;
    2635             :   }
    2636             :   return false;
    2637             : }
    2638             : 
    2639             : /// GetVal - Get a value with the specified name or ID, creating a
    2640             : /// forward reference record if needed.  This can return null if the value
    2641             : /// exists but does not have the right type.
    2642     1529909 : Value *LLParser::PerFunctionState::GetVal(const std::string &Name, Type *Ty,
    2643             :                                           LocTy Loc, bool IsCall) {
    2644             :   // Look this name up in the normal function symbol table.
    2645     1529909 :   Value *Val = F.getValueSymbolTable()->lookup(Name);
    2646             : 
    2647             :   // If this is a forward reference for the value, see if we already created a
    2648             :   // forward ref record.
    2649     1529909 :   if (!Val) {
    2650             :     auto I = ForwardRefVals.find(Name);
    2651      180218 :     if (I != ForwardRefVals.end())
    2652         843 :       Val = I->second.first;
    2653             :   }
    2654             : 
    2655             :   // If we have the value in the symbol table or fwd-ref table, return it.
    2656     1529909 :   if (Val) {
    2657     1350534 :     if (isValidVariableType(P.M, Ty, Val, IsCall))
    2658             :       return Val;
    2659           5 :     if (Ty->isLabelTy())
    2660           0 :       P.Error(Loc, "'%" + Name + "' is not a basic block");
    2661             :     else
    2662          30 :       P.Error(Loc, "'%" + Name + "' defined with type '" +
    2663          20 :               getTypeString(Val->getType()) + "'");
    2664             :     return nullptr;
    2665             :   }
    2666             : 
    2667             :   // Don't make placeholders with invalid type.
    2668             :   if (!Ty->isFirstClassType()) {
    2669           0 :     P.Error(Loc, "invalid use of a non-first-class type");
    2670           0 :     return nullptr;
    2671             :   }
    2672             : 
    2673             :   // Otherwise, create a new forward reference for this value and remember it.
    2674             :   Value *FwdVal;
    2675      179375 :   if (Ty->isLabelTy()) {
    2676      492075 :     FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
    2677             :   } else {
    2678       15350 :     FwdVal = new Argument(Ty, Name);
    2679             :   }
    2680             : 
    2681      179375 :   ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
    2682      179375 :   return FwdVal;
    2683             : }
    2684             : 
    2685      403107 : Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, LocTy Loc,
    2686             :                                           bool IsCall) {
    2687             :   // Look this name up in the normal function symbol table.
    2688     1056317 :   Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
    2689             : 
    2690             :   // If this is a forward reference for the value, see if we already created a
    2691             :   // forward ref record.
    2692      250103 :   if (!Val) {
    2693             :     auto I = ForwardRefValIDs.find(ID);
    2694      153004 :     if (I != ForwardRefValIDs.end())
    2695        2825 :       Val = I->second.first;
    2696             :   }
    2697             : 
    2698             :   // If we have the value in the symbol table or fwd-ref table, return it.
    2699      403107 :   if (Val) {
    2700      252928 :     if (isValidVariableType(P.M, Ty, Val, IsCall))
    2701             :       return Val;
    2702           4 :     if (Ty->isLabelTy())
    2703           0 :       P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
    2704             :     else
    2705          12 :       P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
    2706          16 :               getTypeString(Val->getType()) + "'");
    2707             :     return nullptr;
    2708             :   }
    2709             : 
    2710             :   if (!Ty->isFirstClassType()) {
    2711           0 :     P.Error(Loc, "invalid use of a non-first-class type");
    2712           0 :     return nullptr;
    2713             :   }
    2714             : 
    2715             :   // Otherwise, create a new forward reference for this value and remember it.
    2716             :   Value *FwdVal;
    2717      150179 :   if (Ty->isLabelTy()) {
    2718      447603 :     FwdVal = BasicBlock::Create(F.getContext(), "", &F);
    2719             :   } else {
    2720         978 :     FwdVal = new Argument(Ty);
    2721             :   }
    2722             : 
    2723      150179 :   ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
    2724      150179 :   return FwdVal;
    2725             : }
    2726             : 
    2727             : /// SetInstName - After an instruction is parsed and inserted into its
    2728             : /// basic block, this installs its name.
    2729     1362075 : bool LLParser::PerFunctionState::SetInstName(int NameID,
    2730             :                                              const std::string &NameStr,
    2731             :                                              LocTy NameLoc, Instruction *Inst) {
    2732             :   // If this instruction has void type, it cannot have a name or ID specified.
    2733     2724150 :   if (Inst->getType()->isVoidTy()) {
    2734      953162 :     if (NameID != -1 || !NameStr.empty())
    2735           0 :       return P.Error(NameLoc, "instructions returning void cannot have a name");
    2736             :     return false;
    2737             :   }
    2738             : 
    2739             :   // If this was a numbered instruction, verify that the instruction is the
    2740             :   // expected value and resolve any forward references.
    2741      885494 :   if (NameStr.empty()) {
    2742             :     // If neither a name nor an ID was specified, just use the next ID.
    2743      231846 :     if (NameID == -1)
    2744       11970 :       NameID = NumberedVals.size();
    2745             : 
    2746      463692 :     if (unsigned(NameID) != NumberedVals.size())
    2747           0 :       return P.Error(NameLoc, "instruction expected to be numbered '%" +
    2748           0 :                      Twine(NumberedVals.size()) + "'");
    2749             : 
    2750             :     auto FI = ForwardRefValIDs.find(NameID);
    2751      231846 :     if (FI != ForwardRefValIDs.end()) {
    2752         977 :       Value *Sentinel = FI->second.first;
    2753         977 :       if (Sentinel->getType() != Inst->getType())
    2754           0 :         return P.Error(NameLoc, "instruction forward referenced with type '" +
    2755           0 :                        getTypeString(FI->second.first->getType()) + "'");
    2756             : 
    2757         977 :       Sentinel->replaceAllUsesWith(Inst);
    2758         977 :       Sentinel->deleteValue();
    2759             :       ForwardRefValIDs.erase(FI);
    2760             :     }
    2761             : 
    2762      463692 :     NumberedVals.push_back(Inst);
    2763      231846 :     return false;
    2764             :   }
    2765             : 
    2766             :   // Otherwise, the instruction had a name.  Resolve forward refs and set it.
    2767             :   auto FI = ForwardRefVals.find(NameStr);
    2768      653648 :   if (FI != ForwardRefVals.end()) {
    2769       15344 :     Value *Sentinel = FI->second.first;
    2770       15344 :     if (Sentinel->getType() != Inst->getType())
    2771           0 :       return P.Error(NameLoc, "instruction forward referenced with type '" +
    2772           0 :                      getTypeString(FI->second.first->getType()) + "'");
    2773             : 
    2774       15344 :     Sentinel->replaceAllUsesWith(Inst);
    2775       15344 :     Sentinel->deleteValue();
    2776             :     ForwardRefVals.erase(FI);
    2777             :   }
    2778             : 
    2779             :   // Set the name on the instruction.
    2780     1307296 :   Inst->setName(NameStr);
    2781             : 
    2782      653648 :   if (Inst->getName() != NameStr)
    2783           3 :     return P.Error(NameLoc, "multiple definition of local value named '" +
    2784           2 :                    NameStr + "'");
    2785             :   return false;
    2786             : }
    2787             : 
    2788             : /// GetBB - Get a basic block with the specified name or ID, creating a
    2789             : /// forward reference record if needed.
    2790      164510 : BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
    2791             :                                               LocTy Loc) {
    2792      164510 :   return dyn_cast_or_null<BasicBlock>(
    2793      329020 :       GetVal(Name, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
    2794             : }
    2795             : 
    2796      149214 : BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
    2797      149214 :   return dyn_cast_or_null<BasicBlock>(
    2798      298428 :       GetVal(ID, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
    2799             : }
    2800             : 
    2801             : /// DefineBB - Define the specified basic block, which is either named or
    2802             : /// unnamed.  If there is an error, this returns null otherwise it returns
    2803             : /// the block being defined.
    2804      313224 : BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
    2805             :                                                  LocTy Loc) {
    2806             :   BasicBlock *BB;
    2807      313224 :   if (Name.empty())
    2808      298402 :     BB = GetBB(NumberedVals.size(), Loc);
    2809             :   else
    2810      164023 :     BB = GetBB(Name, Loc);
    2811      313224 :   if (!BB) return nullptr; // Already diagnosed error.
    2812             : 
    2813             :   // Move the block to the end of the function.  Forward ref'd blocks are
    2814             :   // inserted wherever they happen to be referenced.
    2815      626446 :   F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
    2816             : 
    2817             :   // Remove the block from forward ref sets.
    2818      313223 :   if (Name.empty()) {
    2819      447603 :     ForwardRefValIDs.erase(NumberedVals.size());
    2820      298402 :     NumberedVals.push_back(BB);
    2821             :   } else {
    2822             :     // BB forward references are already in the function symbol table.
    2823             :     ForwardRefVals.erase(Name);
    2824             :   }
    2825             : 
    2826             :   return BB;
    2827             : }
    2828             : 
    2829             : //===----------------------------------------------------------------------===//
    2830             : // Constants.
    2831             : //===----------------------------------------------------------------------===//
    2832             : 
    2833             : /// ParseValID - Parse an abstract value that doesn't necessarily have a
    2834             : /// type implied.  For example, if we parse "4" we don't know what integer type
    2835             : /// it has.  The value will later be combined with its type and checked for
    2836             : /// sanity.  PFS is used to convert function-local operands of metadata (since
    2837             : /// metadata operands are not just parsed here but also converted to values).
    2838             : /// PFS can be null when we are not parsing metadata values inside a function.
    2839     3353971 : bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
    2840     3353971 :   ID.Loc = Lex.getLoc();
    2841     3353971 :   switch (Lex.getKind()) {
    2842           1 :   default: return TokError("expected value token");
    2843         663 :   case lltok::GlobalID:  // @42
    2844         663 :     ID.UIntVal = Lex.getUIntVal();
    2845         663 :     ID.Kind = ValID::t_GlobalID;
    2846         663 :     break;
    2847      199948 :   case lltok::GlobalVar:  // @foo
    2848      199948 :     ID.StrVal = Lex.getStrVal();
    2849      199948 :     ID.Kind = ValID::t_GlobalName;
    2850      199948 :     break;
    2851      253912 :   case lltok::LocalVarID:  // %42
    2852      253912 :     ID.UIntVal = Lex.getUIntVal();
    2853      253912 :     ID.Kind = ValID::t_LocalID;
    2854      253912 :     break;
    2855     1366021 :   case lltok::LocalVar:  // %foo
    2856     1366021 :     ID.StrVal = Lex.getStrVal();
    2857     1366021 :     ID.Kind = ValID::t_LocalName;
    2858     1366021 :     break;
    2859     1143411 :   case lltok::APSInt:
    2860             :     ID.APSIntVal = Lex.getAPSIntVal();
    2861     1143411 :     ID.Kind = ValID::t_APSInt;
    2862     1143411 :     break;
    2863       45135 :   case lltok::APFloat:
    2864             :     ID.APFloatVal = Lex.getAPFloatVal();
    2865       45135 :     ID.Kind = ValID::t_APFloat;
    2866       45135 :     break;
    2867        6618 :   case lltok::kw_true:
    2868        6618 :     ID.ConstantVal = ConstantInt::getTrue(Context);
    2869        6618 :     ID.Kind = ValID::t_Constant;
    2870        6618 :     break;
    2871       14213 :   case lltok::kw_false:
    2872       14213 :     ID.ConstantVal = ConstantInt::getFalse(Context);
    2873       14213 :     ID.Kind = ValID::t_Constant;
    2874       14213 :     break;
    2875        7470 :   case lltok::kw_null: ID.Kind = ValID::t_Null; break;
    2876      161175 :   case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
    2877       25532 :   case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
    2878         474 :   case lltok::kw_none: ID.Kind = ValID::t_None; break;
    2879             : 
    2880        1677 :   case lltok::lbrace: {
    2881             :     // ValID ::= '{' ConstVector '}'
    2882        1677 :     Lex.Lex();
    2883             :     SmallVector<Constant*, 16> Elts;
    2884        3349 :     if (ParseGlobalValueVector(Elts) ||
    2885        1672 :         ParseToken(lltok::rbrace, "expected end of struct constant"))
    2886             :       return true;
    2887             : 
    2888        3344 :     ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
    2889        1672 :     ID.UIntVal = Elts.size();
    2890        3344 :     memcpy(ID.ConstantStructElts.get(), Elts.data(),
    2891             :            Elts.size() * sizeof(Elts[0]));
    2892        1672 :     ID.Kind = ValID::t_ConstantStruct;
    2893        1672 :     return false;
    2894             :   }
    2895       65893 :   case lltok::less: {
    2896             :     // ValID ::= '<' ConstVector '>'         --> Vector.
    2897             :     // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
    2898       65893 :     Lex.Lex();
    2899             :     bool isPackedStruct = EatIfPresent(lltok::lbrace);
    2900             : 
    2901             :     SmallVector<Constant*, 16> Elts;
    2902       65893 :     LocTy FirstEltLoc = Lex.getLoc();
    2903      131786 :     if (ParseGlobalValueVector(Elts) ||
    2904          95 :         (isPackedStruct &&
    2905      131881 :          ParseToken(lltok::rbrace, "expected end of packed struct")) ||
    2906       65893 :         ParseToken(lltok::greater, "expected end of constant"))
    2907             :       return true;
    2908             : 
    2909       65893 :     if (isPackedStruct) {
    2910         190 :       ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
    2911         190 :       memcpy(ID.ConstantStructElts.get(), Elts.data(),
    2912             :              Elts.size() * sizeof(Elts[0]));
    2913          95 :       ID.UIntVal = Elts.size();
    2914          95 :       ID.Kind = ValID::t_PackedConstantStruct;
    2915          95 :       return false;
    2916             :     }
    2917             : 
    2918       65798 :     if (Elts.empty())
    2919           0 :       return Error(ID.Loc, "constant vector must not be empty");
    2920             : 
    2921       65798 :     if (!Elts[0]->getType()->isIntegerTy() &&
    2922       65817 :         !Elts[0]->getType()->isFloatingPointTy() &&
    2923             :         !Elts[0]->getType()->isPointerTy())
    2924           0 :       return Error(FirstEltLoc,
    2925             :             "vector elements must have integer, pointer or floating point type");
    2926             : 
    2927             :     // Verify that all the vector elements have the same type.
    2928     1687630 :     for (unsigned i = 1, e = Elts.size(); i != e; ++i)
    2929     1556034 :       if (Elts[i]->getType() != Elts[0]->getType())
    2930             :         return Error(FirstEltLoc,
    2931           0 :                      "vector element #" + Twine(i) +
    2932           0 :                     " is not of type '" + getTypeString(Elts[0]->getType()));
    2933             : 
    2934       65798 :     ID.ConstantVal = ConstantVector::get(Elts);
    2935       65798 :     ID.Kind = ValID::t_Constant;
    2936       65798 :     return false;
    2937             :   }
    2938        1371 :   case lltok::lsquare: {   // Array Constant
    2939        1371 :     Lex.Lex();
    2940             :     SmallVector<Constant*, 16> Elts;
    2941        1371 :     LocTy FirstEltLoc = Lex.getLoc();
    2942        2742 :     if (ParseGlobalValueVector(Elts) ||
    2943        1371 :         ParseToken(lltok::rsquare, "expected end of array constant"))
    2944             :       return true;
    2945             : 
    2946             :     // Handle empty element.
    2947        1371 :     if (Elts.empty()) {
    2948             :       // Use undef instead of an array because it's inconvenient to determine
    2949             :       // the element type at this point, there being no elements to examine.
    2950          16 :       ID.Kind = ValID::t_EmptyArray;
    2951          16 :       return false;
    2952             :     }
    2953             : 
    2954        1355 :     if (!Elts[0]->getType()->isFirstClassType())
    2955           0 :       return Error(FirstEltLoc, "invalid array element type: " +
    2956           0 :                    getTypeString(Elts[0]->getType()));
    2957             : 
    2958        1355 :     ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
    2959             : 
    2960             :     // Verify all elements are correct type!
    2961       13332 :     for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
    2962       10622 :       if (Elts[i]->getType() != Elts[0]->getType())
    2963             :         return Error(FirstEltLoc,
    2964           0 :                      "array element #" + Twine(i) +
    2965           0 :                      " is not of type '" + getTypeString(Elts[0]->getType()));
    2966             :     }
    2967             : 
    2968        1355 :     ID.ConstantVal = ConstantArray::get(ATy, Elts);
    2969        1355 :     ID.Kind = ValID::t_Constant;
    2970        1355 :     return false;
    2971             :   }
    2972        1999 :   case lltok::kw_c:  // c "foo"
    2973        1999 :     Lex.Lex();
    2974        1999 :     ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
    2975             :                                                   false);
    2976        1999 :     if (ParseToken(lltok::StringConstant, "expected string")) return true;
    2977        1999 :     ID.Kind = ValID::t_Constant;
    2978        1999 :     return false;
    2979             : 
    2980       12199 :   case lltok::kw_asm: {
    2981             :     // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
    2982             :     //             STRINGCONSTANT
    2983             :     bool HasSideEffect, AlignStack, AsmDialect;
    2984       12199 :     Lex.Lex();
    2985       12199 :     if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
    2986             :         ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
    2987       12199 :         ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
    2988       24398 :         ParseStringConstant(ID.StrVal) ||
    2989       24398 :         ParseToken(lltok::comma, "expected comma in inline asm expression") ||
    2990       12199 :         ParseToken(lltok::StringConstant, "expected constraint string"))
    2991             :       return true;
    2992       12199 :     ID.StrVal2 = Lex.getStrVal();
    2993       24398 :     ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
    2994       12199 :       (unsigned(AsmDialect)<<2);
    2995       12199 :     ID.Kind = ValID::t_InlineAsm;
    2996       12199 :     return false;
    2997             :   }
    2998             : 
    2999         625 :   case lltok::kw_blockaddress: {
    3000             :     // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
    3001         625 :     Lex.Lex();
    3002             : 
    3003        1250 :     ValID Fn, Label;
    3004             : 
    3005        1250 :     if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
    3006        1250 :         ParseValID(Fn) ||
    3007        1250 :         ParseToken(lltok::comma, "expected comma in block address expression")||
    3008        1875 :         ParseValID(Label) ||
    3009         625 :         ParseToken(lltok::rparen, "expected ')' in block address expression"))
    3010             :       return true;
    3011             : 
    3012         625 :     if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
    3013           0 :       return Error(Fn.Loc, "expected function name in blockaddress");
    3014         625 :     if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
    3015           0 :       return Error(Label.Loc, "expected basic block name in blockaddress");
    3016             : 
    3017             :     // Try to find the function (but skip it if it's forward-referenced).
    3018             :     GlobalValue *GV = nullptr;
    3019         625 :     if (Fn.Kind == ValID::t_GlobalID) {
    3020           0 :       if (Fn.UIntVal < NumberedVals.size())
    3021           0 :         GV = NumberedVals[Fn.UIntVal];
    3022             :     } else if (!ForwardRefVals.count(Fn.StrVal)) {
    3023        1250 :       GV = M->getNamedValue(Fn.StrVal);
    3024             :     }
    3025             :     Function *F = nullptr;
    3026         625 :     if (GV) {
    3027             :       // Confirm that it's actually a function with a definition.
    3028         304 :       if (!isa<Function>(GV))
    3029           0 :         return Error(Fn.Loc, "expected function name in blockaddress");
    3030             :       F = cast<Function>(GV);
    3031         304 :       if (F->isDeclaration())
    3032           0 :         return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
    3033             :     }
    3034             : 
    3035             :     if (!F) {
    3036             :       // Make a global variable as a placeholder for this reference.
    3037             :       GlobalValue *&FwdRef =
    3038         321 :           ForwardRefBlockAddresses.insert(std::make_pair(
    3039             :                                               std::move(Fn),
    3040         321 :                                               std::map<ValID, GlobalValue *>()))
    3041         321 :               .first->second.insert(std::make_pair(std::move(Label), nullptr))
    3042             :               .first->second;
    3043         321 :       if (!FwdRef)
    3044         807 :         FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
    3045         269 :                                     GlobalValue::InternalLinkage, nullptr, "");
    3046         321 :       ID.ConstantVal = FwdRef;
    3047         321 :       ID.Kind = ValID::t_Constant;
    3048         321 :       return false;
    3049             :     }
    3050             : 
    3051             :     // We found the function; now find the basic block.  Don't use PFS, since we
    3052             :     // might be inside a constant expression.
    3053             :     BasicBlock *BB;
    3054         606 :     if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
    3055         231 :       if (Label.Kind == ValID::t_LocalID)
    3056           4 :         BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
    3057             :       else
    3058         227 :         BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
    3059         231 :       if (!BB)
    3060           0 :         return Error(Label.Loc, "referenced value is not a basic block");
    3061             :     } else {
    3062          73 :       if (Label.Kind == ValID::t_LocalID)
    3063           0 :         return Error(Label.Loc, "cannot take address of numeric label after "
    3064             :                                 "the function is defined");
    3065             :       BB = dyn_cast_or_null<BasicBlock>(
    3066             :           F->getValueSymbolTable()->lookup(Label.StrVal));
    3067             :       if (!BB)
    3068           0 :         return Error(Label.Loc, "referenced value is not a basic block");
    3069             :     }
    3070             : 
    3071         304 :     ID.ConstantVal = BlockAddress::get(F, BB);
    3072         304 :     ID.Kind = ValID::t_Constant;
    3073         304 :     return false;
    3074             :   }
    3075             : 
    3076        8249 :   case lltok::kw_trunc:
    3077             :   case lltok::kw_zext:
    3078             :   case lltok::kw_sext:
    3079             :   case lltok::kw_fptrunc:
    3080             :   case lltok::kw_fpext:
    3081             :   case lltok::kw_bitcast:
    3082             :   case lltok::kw_addrspacecast:
    3083             :   case lltok::kw_uitofp:
    3084             :   case lltok::kw_sitofp:
    3085             :   case lltok::kw_fptoui:
    3086             :   case lltok::kw_fptosi:
    3087             :   case lltok::kw_inttoptr:
    3088             :   case lltok::kw_ptrtoint: {
    3089        8249 :     unsigned Opc = Lex.getUIntVal();
    3090        8249 :     Type *DestTy = nullptr;
    3091             :     Constant *SrcVal;
    3092        8249 :     Lex.Lex();
    3093       16498 :     if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
    3094       16497 :         ParseGlobalTypeAndValue(SrcVal) ||
    3095       16496 :         ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
    3096       16497 :         ParseType(DestTy) ||
    3097        8248 :         ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
    3098             :       return true;
    3099        8248 :     if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
    3100          30 :       return Error(ID.Loc, "invalid cast opcode for cast from '" +
    3101          50 :                    getTypeString(SrcVal->getType()) + "' to '" +
    3102          40 :                    getTypeString(DestTy) + "'");
    3103        8238 :     ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
    3104             :                                                  SrcVal, DestTy);
    3105        8238 :     ID.Kind = ValID::t_Constant;
    3106        8238 :     return false;
    3107             :   }
    3108          11 :   case lltok::kw_extractvalue: {
    3109          11 :     Lex.Lex();
    3110             :     Constant *Val;
    3111             :     SmallVector<unsigned, 4> Indices;
    3112          22 :     if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
    3113          22 :         ParseGlobalTypeAndValue(Val) ||
    3114          33 :         ParseIndexList(Indices) ||
    3115          11 :         ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
    3116             :       return true;
    3117             : 
    3118          11 :     if (!Val->getType()->isAggregateType())
    3119           0 :       return Error(ID.Loc, "extractvalue operand must be aggregate type");
    3120          11 :     if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
    3121           0 :       return Error(ID.Loc, "invalid indices for extractvalue");
    3122          11 :     ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
    3123          11 :     ID.Kind = ValID::t_Constant;
    3124          11 :     return false;
    3125             :   }
    3126          10 :   case lltok::kw_insertvalue: {
    3127          10 :     Lex.Lex();
    3128             :     Constant *Val0, *Val1;
    3129             :     SmallVector<unsigned, 4> Indices;
    3130          20 :     if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
    3131          20 :         ParseGlobalTypeAndValue(Val0) ||
    3132          20 :         ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
    3133          20 :         ParseGlobalTypeAndValue(Val1) ||
    3134          30 :         ParseIndexList(Indices) ||
    3135          10 :         ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
    3136             :       return true;
    3137          10 :     if (!Val0->getType()->isAggregateType())
    3138           0 :       return Error(ID.Loc, "insertvalue operand must be aggregate type");
    3139             :     Type *IndexedType =
    3140          10 :         ExtractValueInst::getIndexedType(Val0->getType(), Indices);
    3141          10 :     if (!IndexedType)
    3142           0 :       return Error(ID.Loc, "invalid indices for insertvalue");
    3143          10 :     if (IndexedType != Val1->getType())
    3144           3 :       return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
    3145           4 :                                getTypeString(Val1->getType()) +
    3146           5 :                                "' instead of '" + getTypeString(IndexedType) +
    3147             :                                "'");
    3148           9 :     ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
    3149           9 :     ID.Kind = ValID::t_Constant;
    3150           9 :     return false;
    3151             :   }
    3152         270 :   case lltok::kw_icmp:
    3153             :   case lltok::kw_fcmp: {
    3154         270 :     unsigned PredVal, Opc = Lex.getUIntVal();
    3155             :     Constant *Val0, *Val1;
    3156         270 :     Lex.Lex();
    3157         540 :     if (ParseCmpPredicate(PredVal, Opc) ||
    3158         540 :         ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
    3159         540 :         ParseGlobalTypeAndValue(Val0) ||
    3160         540 :         ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
    3161         810 :         ParseGlobalTypeAndValue(Val1) ||
    3162         270 :         ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
    3163             :       return true;
    3164             : 
    3165         270 :     if (Val0->getType() != Val1->getType())
    3166           0 :       return Error(ID.Loc, "compare operands must have the same type");
    3167             : 
    3168         270 :     CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
    3169             : 
    3170         270 :     if (Opc == Instruction::FCmp) {
    3171             :       if (!Val0->getType()->isFPOrFPVectorTy())
    3172           0 :         return Error(ID.Loc, "fcmp requires floating point operands");
    3173           6 :       ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
    3174             :     } else {
    3175             :       assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
    3176         264 :       if (!Val0->getType()->isIntOrIntVectorTy() &&
    3177             :           !Val0->getType()->isPtrOrPtrVectorTy())
    3178           0 :         return Error(ID.Loc, "icmp requires pointer or integer operands");
    3179         264 :       ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
    3180             :     }
    3181         270 :     ID.Kind = ValID::t_Constant;
    3182         270 :     return false;
    3183             :   }
    3184             : 
    3185             :   // Binary Operators.
    3186         624 :   case lltok::kw_add:
    3187             :   case lltok::kw_fadd:
    3188             :   case lltok::kw_sub:
    3189             :   case lltok::kw_fsub:
    3190             :   case lltok::kw_mul:
    3191             :   case lltok::kw_fmul:
    3192             :   case lltok::kw_udiv:
    3193             :   case lltok::kw_sdiv:
    3194             :   case lltok::kw_fdiv:
    3195             :   case lltok::kw_urem:
    3196             :   case lltok::kw_srem:
    3197             :   case lltok::kw_frem:
    3198             :   case lltok::kw_shl:
    3199             :   case lltok::kw_lshr:
    3200             :   case lltok::kw_ashr: {
    3201             :     bool NUW = false;
    3202             :     bool NSW = false;
    3203             :     bool Exact = false;
    3204         624 :     unsigned Opc = Lex.getUIntVal();
    3205             :     Constant *Val0, *Val1;
    3206         624 :     Lex.Lex();
    3207         624 :     LocTy ModifierLoc = Lex.getLoc();
    3208         624 :     if (Opc == Instruction::Add || Opc == Instruction::Sub ||
    3209         263 :         Opc == Instruction::Mul || Opc == Instruction::Shl) {
    3210             :       if (EatIfPresent(lltok::kw_nuw))
    3211             :         NUW = true;
    3212             :       if (EatIfPresent(lltok::kw_nsw)) {
    3213             :         NSW = true;
    3214             :         if (EatIfPresent(lltok::kw_nuw))
    3215             :           NUW = true;
    3216             :       }
    3217         296 :     } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
    3218         204 :                Opc == Instruction::LShr || Opc == Instruction::AShr) {
    3219             :       if (EatIfPresent(lltok::kw_exact))
    3220             :         Exact = true;
    3221             :     }
    3222        1248 :     if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
    3223        1248 :         ParseGlobalTypeAndValue(Val0) ||
    3224        1248 :         ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
    3225        1872 :         ParseGlobalTypeAndValue(Val1) ||
    3226         624 :         ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
    3227             :       return true;
    3228         624 :     if (Val0->getType() != Val1->getType())
    3229           0 :       return Error(ID.Loc, "operands of constexpr must have same type");
    3230         624 :     if (!Val0->getType()->isIntOrIntVectorTy()) {
    3231          21 :       if (NUW)
    3232           0 :         return Error(ModifierLoc, "nuw only applies to integer operations");
    3233          21 :       if (NSW)
    3234           0 :         return Error(ModifierLoc, "nsw only applies to integer operations");
    3235             :     }
    3236             :     // Check that the type is valid for the operator.
    3237             :     switch (Opc) {
    3238             :     case Instruction::Add:
    3239             :     case Instruction::Sub:
    3240             :     case Instruction::Mul:
    3241             :     case Instruction::UDiv:
    3242             :     case Instruction::SDiv:
    3243             :     case Instruction::URem:
    3244             :     case Instruction::SRem:
    3245             :     case Instruction::Shl:
    3246             :     case Instruction::AShr:
    3247             :     case Instruction::LShr:
    3248         604 :       if (!Val0->getType()->isIntOrIntVectorTy())
    3249           1 :         return Error(ID.Loc, "constexpr requires integer operands");
    3250             :       break;
    3251             :     case Instruction::FAdd:
    3252             :     case Instruction::FSub:
    3253             :     case Instruction::FMul:
    3254             :     case Instruction::FDiv:
    3255             :     case Instruction::FRem:
    3256             :       if (!Val0->getType()->isFPOrFPVectorTy())
    3257           0 :         return Error(ID.Loc, "constexpr requires fp operands");
    3258             :       break;
    3259           0 :     default: llvm_unreachable("Unknown binary operator!");
    3260             :     }
    3261             :     unsigned Flags = 0;
    3262         623 :     if (NUW)   Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
    3263         623 :     if (NSW)   Flags |= OverflowingBinaryOperator::NoSignedWrap;
    3264         623 :     if (Exact) Flags |= PossiblyExactOperator::IsExact;
    3265         623 :     Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
    3266         623 :     ID.ConstantVal = C;
    3267         623 :     ID.Kind = ValID::t_Constant;
    3268         623 :     return false;
    3269             :   }
    3270             : 
    3271             :   // Logical Operations
    3272         114 :   case lltok::kw_and:
    3273             :   case lltok::kw_or:
    3274             :   case lltok::kw_xor: {
    3275         114 :     unsigned Opc = Lex.getUIntVal();
    3276             :     Constant *Val0, *Val1;
    3277         114 :     Lex.Lex();
    3278         228 :     if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
    3279         228 :         ParseGlobalTypeAndValue(Val0) ||
    3280         228 :         ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
    3281         342 :         ParseGlobalTypeAndValue(Val1) ||
    3282         114 :         ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
    3283             :       return true;
    3284         114 :     if (Val0->getType() != Val1->getType())
    3285           0 :       return Error(ID.Loc, "operands of constexpr must have same type");
    3286         114 :     if (!Val0->getType()->isIntOrIntVectorTy())
    3287           0 :       return Error(ID.Loc,
    3288             :                    "constexpr requires integer or integer vector operands");
    3289         114 :     ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
    3290         114 :     ID.Kind = ValID::t_Constant;
    3291         114 :     return false;
    3292             :   }
    3293             : 
    3294       36356 :   case lltok::kw_getelementptr:
    3295             :   case lltok::kw_shufflevector:
    3296             :   case lltok::kw_insertelement:
    3297             :   case lltok::kw_extractelement:
    3298             :   case lltok::kw_select: {
    3299       36356 :     unsigned Opc = Lex.getUIntVal();
    3300             :     SmallVector<Constant*, 16> Elts;
    3301             :     bool InBounds = false;
    3302             :     Type *Ty;
    3303       36356 :     Lex.Lex();
    3304             : 
    3305       36356 :     if (Opc == Instruction::GetElementPtr)
    3306             :       InBounds = EatIfPresent(lltok::kw_inbounds);
    3307             : 
    3308       36356 :     if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
    3309             :       return true;
    3310             : 
    3311       36356 :     LocTy ExplicitTypeLoc = Lex.getLoc();
    3312       36356 :     if (Opc == Instruction::GetElementPtr) {
    3313       72542 :       if (ParseType(Ty) ||
    3314       36271 :           ParseToken(lltok::comma, "expected comma after getelementptr's type"))
    3315             :         return true;
    3316             :     }
    3317             : 
    3318             :     Optional<unsigned> InRangeOp;
    3319       36356 :     if (ParseGlobalValueVector(
    3320       72711 :             Elts, Opc == Instruction::GetElementPtr ? &InRangeOp : nullptr) ||
    3321       36355 :         ParseToken(lltok::rparen, "expected ')' in constantexpr"))
    3322             :       return true;
    3323             : 
    3324       36355 :     if (Opc == Instruction::GetElementPtr) {
    3325       72540 :       if (Elts.size() == 0 ||
    3326       36270 :           !Elts[0]->getType()->isPtrOrPtrVectorTy())
    3327           0 :         return Error(ID.Loc, "base of getelementptr must be a pointer");
    3328             : 
    3329             :       Type *BaseType = Elts[0]->getType();
    3330             :       auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
    3331       36270 :       if (Ty != BasePointerType->getElementType())
    3332           0 :         return Error(
    3333             :             ExplicitTypeLoc,
    3334             :             "explicit pointee type doesn't match operand's pointee type");
    3335             : 
    3336             :       unsigned GEPWidth =
    3337       36270 :           BaseType->isVectorTy() ? BaseType->getVectorNumElements() : 0;
    3338             : 
    3339       36270 :       ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
    3340      181804 :       for (Constant *Val : Indices) {
    3341       72769 :         Type *ValTy = Val->getType();
    3342       72769 :         if (!ValTy->isIntOrIntVectorTy())
    3343           0 :           return Error(ID.Loc, "getelementptr index must be an integer");
    3344       72769 :         if (ValTy->isVectorTy()) {
    3345             :           unsigned ValNumEl = ValTy->getVectorNumElements();
    3346          38 :           if (GEPWidth && (ValNumEl != GEPWidth))
    3347           2 :             return Error(
    3348             :                 ID.Loc,
    3349             :                 "getelementptr vector index has a wrong number of elements");
    3350             :           // GEPWidth may have been unknown because the base is a scalar,
    3351             :           // but it is known now.
    3352             :           GEPWidth = ValNumEl;
    3353             :         }
    3354             :       }
    3355             : 
    3356             :       SmallPtrSet<Type*, 4> Visited;
    3357       36268 :       if (!Indices.empty() && !Ty->isSized(&Visited))
    3358           0 :         return Error(ID.Loc, "base element of getelementptr must be sized");
    3359             : 
    3360       36268 :       if (!GetElementPtrInst::getIndexedType(Ty, Indices))
    3361           0 :         return Error(ID.Loc, "invalid getelementptr indices");
    3362             : 
    3363       36268 :       if (InRangeOp) {
    3364          65 :         if (*InRangeOp == 0)
    3365           0 :           return Error(ID.Loc,
    3366             :                        "inrange keyword may not appear on pointer operand");
    3367          65 :         --*InRangeOp;
    3368             :       }
    3369             : 
    3370      108804 :       ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices,
    3371             :                                                       InBounds, InRangeOp);
    3372          85 :     } else if (Opc == Instruction::Select) {
    3373          52 :       if (Elts.size() != 3)
    3374           0 :         return Error(ID.Loc, "expected three operands to select");
    3375         104 :       if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
    3376          52 :                                                               Elts[2]))
    3377           0 :         return Error(ID.Loc, Reason);
    3378          52 :       ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
    3379          33 :     } else if (Opc == Instruction::ShuffleVector) {
    3380          10 :       if (Elts.size() != 3)
    3381           0 :         return Error(ID.Loc, "expected three operands to shufflevector");
    3382          10 :       if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
    3383           0 :         return Error(ID.Loc, "invalid operands to shufflevector");
    3384          10 :       ID.ConstantVal =
    3385          10 :                  ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
    3386          23 :     } else if (Opc == Instruction::ExtractElement) {
    3387          21 :       if (Elts.size() != 2)
    3388           0 :         return Error(ID.Loc, "expected two operands to extractelement");
    3389          21 :       if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
    3390           0 :         return Error(ID.Loc, "invalid extractelement operands");
    3391          21 :       ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
    3392             :     } else {
    3393             :       assert(Opc == Instruction::InsertElement && "Unknown opcode");
    3394           2 :       if (Elts.size() != 3)
    3395           0 :       return Error(ID.Loc, "expected three operands to insertelement");
    3396           2 :       if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
    3397           0 :         return Error(ID.Loc, "invalid insertelement operands");
    3398           2 :       ID.ConstantVal =
    3399           2 :                  ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
    3400             :     }
    3401             : 
    3402       36353 :     ID.Kind = ValID::t_Constant;
    3403       36353 :     return false;
    3404             :   }
    3405             :   }
    3406             : 
    3407     3224572 :   Lex.Lex();
    3408     3224572 :   return false;
    3409             : }
    3410             : 
    3411             : /// ParseGlobalValue - Parse a global value with the specified type.
    3412      995902 : bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
    3413      995902 :   C = nullptr;
    3414     1991804 :   ValID ID;
    3415      995902 :   Value *V = nullptr;
    3416     1991787 :   bool Parsed = ParseValID(ID) ||
    3417      995885 :                 ConvertValIDToValue(Ty, ID, V, nullptr, /*IsCall=*/false);
    3418     1991785 :   if (V && !(C = dyn_cast<Constant>(V)))
    3419           0 :     return Error(ID.Loc, "global values must be constants");
    3420             :   return Parsed;
    3421             : }
    3422             : 
    3423      975638 : bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
    3424      975638 :   Type *Ty = nullptr;
    3425     1951276 :   return ParseType(Ty) ||
    3426     1951276 :          ParseGlobalValue(Ty, V);
    3427             : }
    3428             : 
    3429      285472 : bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
    3430      285472 :   C = nullptr;
    3431             : 
    3432      285472 :   LocTy KwLoc = Lex.getLoc();
    3433             :   if (!EatIfPresent(lltok::kw_comdat))
    3434             :     return false;
    3435             : 
    3436             :   if (EatIfPresent(lltok::lparen)) {
    3437         301 :     if (Lex.getKind() != lltok::ComdatVar)
    3438           0 :       return TokError("expected comdat variable");
    3439         602 :     C = getComdat(Lex.getStrVal(), Lex.getLoc());
    3440             :     Lex.Lex();
    3441         301 :     if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
    3442             :       return true;
    3443             :   } else {
    3444         503 :     if (GlobalName.empty())
    3445           1 :       return TokError("comdat cannot be unnamed");
    3446        1004 :     C = getComdat(GlobalName, KwLoc);
    3447             :   }
    3448             : 
    3449             :   return false;
    3450             : }
    3451             : 
    3452             : /// ParseGlobalValueVector
    3453             : ///   ::= /*empty*/
    3454             : ///   ::= [inrange] TypeAndValue (',' [inrange] TypeAndValue)*
    3455      105297 : bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
    3456             :                                       Optional<unsigned> *InRangeOp) {
    3457             :   // Empty list.
    3458      210585 :   if (Lex.getKind() == lltok::rbrace ||
    3459      105272 :       Lex.getKind() == lltok::rsquare ||
    3460      210569 :       Lex.getKind() == lltok::greater ||
    3461             :       Lex.getKind() == lltok::rparen)
    3462             :     return false;
    3463             : 
    3464             :   do {
    3465      963511 :     if (InRangeOp && !*InRangeOp && EatIfPresent(lltok::kw_inrange))
    3466          65 :       *InRangeOp = Elts.size();
    3467             : 
    3468             :     Constant *C;
    3469      963511 :     if (ParseGlobalTypeAndValue(C)) return true;
    3470      963505 :     Elts.push_back(C);
    3471             :   } while (EatIfPresent(lltok::comma));
    3472             : 
    3473             :   return false;
    3474             : }
    3475             : 
    3476       19583 : bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
    3477             :   SmallVector<Metadata *, 16> Elts;
    3478       19583 :   if (ParseMDNodeVector(Elts))
    3479             :     return true;
    3480             : 
    3481       39150 :   MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
    3482       19575 :   return false;
    3483             : }
    3484             : 
    3485             : /// MDNode:
    3486             : ///  ::= !{ ... }
    3487             : ///  ::= !7
    3488             : ///  ::= !DILocation(...)
    3489       31434 : bool LLParser::ParseMDNode(MDNode *&N) {
    3490       31434 :   if (Lex.getKind() == lltok::MetadataVar)
    3491          95 :     return ParseSpecializedMDNode(N);
    3492             : 
    3493       62678 :   return ParseToken(lltok::exclaim, "expected '!' here") ||
    3494       31339 :          ParseMDNodeTail(N);
    3495             : }
    3496             : 
    3497      105749 : bool LLParser::ParseMDNodeTail(MDNode *&N) {
    3498             :   // !{ ... }
    3499      105749 :   if (Lex.getKind() == lltok::lbrace)
    3500         320 :     return ParseMDTuple(N);
    3501             : 
    3502             :   // !42
    3503      105429 :   return ParseMDNodeID(N);
    3504             : }
    3505             : 
    3506             : namespace {
    3507             : 
    3508             : /// Structure to represent an optional metadata field.
    3509             : template <class FieldTy> struct MDFieldImpl {
    3510             :   typedef MDFieldImpl ImplTy;
    3511             :   FieldTy Val;
    3512             :   bool Seen;
    3513             : 
    3514             :   void assign(FieldTy Val) {
    3515      143393 :     Seen = true;
    3516      143381 :     this->Val = std::move(Val);
    3517             :   }
    3518             : 
    3519      240733 :   explicit MDFieldImpl(FieldTy Default)
    3520      240733 :       : Val(std::move(Default)), Seen(false) {}
    3521             : };
    3522             : 
    3523             : /// Structure to represent an optional metadata field that
    3524             : /// can be of either type (A or B) and encapsulates the
    3525             : /// MD<typeofA>Field and MD<typeofB>Field structs, so not
    3526             : /// to reimplement the specifics for representing each Field.
    3527             : template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
    3528             :   typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
    3529             :   FieldTypeA A;
    3530             :   FieldTypeB B;
    3531             :   bool Seen;
    3532             : 
    3533             :   enum {
    3534             :     IsInvalid = 0,
    3535             :     IsTypeA = 1,
    3536             :     IsTypeB = 2
    3537             :   } WhatIs;
    3538             : 
    3539             :   void assign(FieldTypeA A) {
    3540         188 :     Seen = true;
    3541         188 :     this->A = std::move(A);
    3542         188 :     WhatIs = IsTypeA;
    3543             :   }
    3544             : 
    3545             :   void assign(FieldTypeB B) {
    3546        6148 :     Seen = true;
    3547        6148 :     this->B = std::move(B);
    3548        6148 :     WhatIs = IsTypeB;
    3549             :   }
    3550             : 
    3551        6341 :   explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
    3552             :       : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
    3553        6341 :         WhatIs(IsInvalid) {}
    3554             : };
    3555             : 
    3556             : struct MDUnsignedField : public MDFieldImpl<uint64_t> {
    3557             :   uint64_t Max;
    3558             : 
    3559             :   MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
    3560      127311 :       : ImplTy(Default), Max(Max) {}
    3561             : };
    3562             : 
    3563             : struct LineField : public MDUnsignedField {
    3564             :   LineField() : MDUnsignedField(0, UINT32_MAX) {}
    3565             : };
    3566             : 
    3567             : struct ColumnField : public MDUnsignedField {
    3568             :   ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
    3569             : };
    3570             : 
    3571             : struct DwarfTagField : public MDUnsignedField {
    3572             :   DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
    3573             :   DwarfTagField(dwarf::Tag DefaultTag)
    3574             :       : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
    3575             : };
    3576             : 
    3577             : struct DwarfMacinfoTypeField : public MDUnsignedField {
    3578             :   DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
    3579             :   DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
    3580             :     : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
    3581             : };
    3582             : 
    3583             : struct DwarfAttEncodingField : public MDUnsignedField {
    3584             :   DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
    3585             : };
    3586             : 
    3587             : struct DwarfVirtualityField : public MDUnsignedField {
    3588             :   DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
    3589             : };
    3590             : 
    3591             : struct DwarfLangField : public MDUnsignedField {
    3592             :   DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
    3593             : };
    3594             : 
    3595             : struct DwarfCCField : public MDUnsignedField {
    3596             :   DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
    3597             : };
    3598             : 
    3599             : struct EmissionKindField : public MDUnsignedField {
    3600             :   EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
    3601             : };
    3602             : 
    3603             : struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
    3604             :   DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
    3605             : };
    3606             : 
    3607             : struct MDSignedField : public MDFieldImpl<int64_t> {
    3608             :   int64_t Min;
    3609             :   int64_t Max;
    3610             : 
    3611             :   MDSignedField(int64_t Default = 0)
    3612         368 :       : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
    3613             :   MDSignedField(int64_t Default, int64_t Min, int64_t Max)
    3614        5684 :       : ImplTy(Default), Min(Min), Max(Max) {}
    3615             : };
    3616             : 
    3617             : struct MDBoolField : public MDFieldImpl<bool> {
    3618             :   MDBoolField(bool Default = false) : ImplTy(Default) {}
    3619             : };
    3620             : 
    3621             : struct MDField : public MDFieldImpl<Metadata *> {
    3622             :   bool AllowNull;
    3623             : 
    3624      141586 :   MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
    3625             : };
    3626             : 
    3627             : struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
    3628             :   MDConstant() : ImplTy(nullptr) {}
    3629             : };
    3630             : 
    3631             : struct MDStringField : public MDFieldImpl<MDString *> {
    3632             :   bool AllowEmpty;
    3633             :   MDStringField(bool AllowEmpty = true)
    3634       68151 :       : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
    3635             : };
    3636             : 
    3637             : struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
    3638             :   MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
    3639             : };
    3640             : 
    3641             : struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
    3642             :   ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
    3643             : };
    3644             : 
    3645             : struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
    3646             :   MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
    3647             :       : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
    3648             : 
    3649             :   MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
    3650             :                     bool AllowNull = true)
    3651             :       : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
    3652             : 
    3653             :   bool isMDSignedField() const { return WhatIs == IsTypeA; }
    3654             :   bool isMDField() const { return WhatIs == IsTypeB; }
    3655             :   int64_t getMDSignedValue() const {
    3656             :     assert(isMDSignedField() && "Wrong field type");
    3657             :     return A.Val;
    3658             :   }
    3659             :   Metadata *getMDFieldValue() const {
    3660             :     assert(isMDField() && "Wrong field type");
    3661             :     return B.Val;
    3662             :   }
    3663             : };
    3664             : 
    3665             : struct MDSignedOrUnsignedField
    3666             :     : MDEitherFieldImpl<MDSignedField, MDUnsignedField> {
    3667             :   MDSignedOrUnsignedField() : ImplTy(MDSignedField(0), MDUnsignedField(0)) {}
    3668             : 
    3669             :   bool isMDSignedField() const { return WhatIs == IsTypeA; }
    3670             :   bool isMDUnsignedField() const { return WhatIs == IsTypeB; }
    3671             :   int64_t getMDSignedValue() const {
    3672             :     assert(isMDSignedField() && "Wrong field type");
    3673             :     return A.Val;
    3674             :   }
    3675             :   uint64_t getMDUnsignedValue() const {
    3676             :     assert(isMDUnsignedField() && "Wrong field type");
    3677             :     return B.Val;
    3678             :   }
    3679             : };
    3680             : 
    3681             : } // end anonymous namespace
    3682             : 
    3683             : namespace llvm {
    3684             : 
    3685             : template <>
    3686       43923 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3687             :                             MDUnsignedField &Result) {
    3688       43923 :   if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    3689           1 :     return TokError("expected unsigned integer");
    3690             : 
    3691             :   auto &U = Lex.getAPSIntVal();
    3692       43922 :   if (U.ugt(Result.Max))
    3693           6 :     return TokError("value for '" + Name + "' too large, limit is " +
    3694          12 :                     Twine(Result.Max));
    3695             :   Result.assign(U.getZExtValue());
    3696             :   assert(Result.Val <= Result.Max && "Expected value in range");
    3697       43916 :   Lex.Lex();
    3698             :   return false;
    3699             : }
    3700             : 
    3701             : template <>
    3702             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
    3703       19410 :   return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3704             : }
    3705             : template <>
    3706             : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
    3707        7825 :   return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3708             : }
    3709             : 
    3710             : template <>
    3711        3788 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
    3712        3788 :   if (Lex.getKind() == lltok::APSInt)
    3713          22 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3714             : 
    3715        3766 :   if (Lex.getKind() != lltok::DwarfTag)
    3716           1 :     return TokError("expected DWARF tag");
    3717             : 
    3718        3765 :   unsigned Tag = dwarf::getTag(Lex.getStrVal());
    3719        3765 :   if (Tag == dwarf::DW_TAG_invalid)
    3720           1 :     return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
    3721             :   assert(Tag <= Result.Max && "Expected valid DWARF tag");
    3722             : 
    3723        3764 :   Result.assign(Tag);
    3724        3764 :   Lex.Lex();
    3725             :   return false;
    3726             : }
    3727             : 
    3728             : template <>
    3729          23 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3730             :                             DwarfMacinfoTypeField &Result) {
    3731          23 :   if (Lex.getKind() == lltok::APSInt)
    3732           0 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3733             : 
    3734          23 :   if (Lex.getKind() != lltok::DwarfMacinfo)
    3735           0 :     return TokError("expected DWARF macinfo type");
    3736             : 
    3737          23 :   unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
    3738          23 :   if (Macinfo == dwarf::DW_MACINFO_invalid)
    3739             :     return TokError(
    3740           0 :         "invalid DWARF macinfo type" + Twine(" '") + Lex.getStrVal() + "'");
    3741             :   assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
    3742             : 
    3743          23 :   Result.assign(Macinfo);
    3744          23 :   Lex.Lex();
    3745             :   return false;
    3746             : }
    3747             : 
    3748             : template <>
    3749          68 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3750             :                             DwarfVirtualityField &Result) {
    3751          68 :   if (Lex.getKind() == lltok::APSInt)
    3752           0 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3753             : 
    3754          68 :   if (Lex.getKind() != lltok::DwarfVirtuality)
    3755           0 :     return TokError("expected DWARF virtuality code");
    3756             : 
    3757          68 :   unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
    3758          68 :   if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
    3759           0 :     return TokError("invalid DWARF virtuality code" + Twine(" '") +
    3760           0 :                     Lex.getStrVal() + "'");
    3761             :   assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
    3762          68 :   Result.assign(Virtuality);
    3763          68 :   Lex.Lex();
    3764             :   return false;
    3765             : }
    3766             : 
    3767             : template <>
    3768        1953 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
    3769        1953 :   if (Lex.getKind() == lltok::APSInt)
    3770          14 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3771             : 
    3772        1939 :   if (Lex.getKind() != lltok::DwarfLang)
    3773           0 :     return TokError("expected DWARF language");
    3774             : 
    3775        1939 :   unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
    3776        1939 :   if (!Lang)
    3777           1 :     return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
    3778           1 :                     "'");
    3779             :   assert(Lang <= Result.Max && "Expected valid DWARF language");
    3780        1938 :   Result.assign(Lang);
    3781        1938 :   Lex.Lex();
    3782             :   return false;
    3783             : }
    3784             : 
    3785             : template <>
    3786          78 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
    3787          78 :   if (Lex.getKind() == lltok::APSInt)
    3788           0 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3789             : 
    3790          78 :   if (Lex.getKind() != lltok::DwarfCC)
    3791           0 :     return TokError("expected DWARF calling convention");
    3792             : 
    3793          78 :   unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
    3794          78 :   if (!CC)
    3795           0 :     return TokError("invalid DWARF calling convention" + Twine(" '") + Lex.getStrVal() +
    3796           0 :                     "'");
    3797             :   assert(CC <= Result.Max && "Expected valid DWARF calling convention");
    3798          78 :   Result.assign(CC);
    3799          78 :   Lex.Lex();
    3800             :   return false;
    3801             : }
    3802             : 
    3803             : template <>
    3804        1848 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, EmissionKindField &Result) {
    3805        1848 :   if (Lex.getKind() == lltok::APSInt)
    3806          81 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3807             : 
    3808        1767 :   if (Lex.getKind() != lltok::EmissionKind)
    3809           0 :     return TokError("expected emission kind");
    3810             : 
    3811        1767 :   auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
    3812        1767 :   if (!Kind)
    3813           0 :     return TokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
    3814           0 :                     "'");
    3815             :   assert(*Kind <= Result.Max && "Expected valid emission kind");
    3816        1767 :   Result.assign(*Kind);
    3817        1767 :   Lex.Lex();
    3818             :   return false;
    3819             : }
    3820             :   
    3821             : template <>
    3822        1403 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3823             :                             DwarfAttEncodingField &Result) {
    3824        1403 :   if (Lex.getKind() == lltok::APSInt)
    3825           3 :     return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
    3826             : 
    3827        1400 :   if (Lex.getKind() != lltok::DwarfAttEncoding)
    3828           0 :     return TokError("expected DWARF type attribute encoding");
    3829             : 
    3830        1400 :   unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
    3831        1400 :   if (!Encoding)
    3832           0 :     return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
    3833           0 :                     Lex.getStrVal() + "'");
    3834             :   assert(Encoding <= Result.Max && "Expected valid DWARF language");
    3835        1400 :   Result.assign(Encoding);
    3836        1400 :   Lex.Lex();
    3837             :   return false;
    3838             : }
    3839             : 
    3840             : /// DIFlagField
    3841             : ///  ::= uint32
    3842             : ///  ::= DIFlagVector
    3843             : ///  ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
    3844             : template <>
    3845        2879 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
    3846             : 
    3847             :   // Parser for a single flag.
    3848        3459 :   auto parseFlag = [&](DINode::DIFlags &Val) {
    3849        6916 :     if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
    3850           2 :       uint32_t TempVal = static_cast<uint32_t>(Val);
    3851           2 :       bool Res = ParseUInt32(TempVal);
    3852           2 :       Val = static_cast<DINode::DIFlags>(TempVal);
    3853             :       return Res;
    3854             :     }
    3855             : 
    3856        3457 :     if (Lex.getKind() != lltok::DIFlag)
    3857           0 :       return TokError("expected debug info flag");
    3858             : 
    3859        3457 :     Val = DINode::getFlag(Lex.getStrVal());
    3860        3457 :     if (!Val)
    3861           0 :       return TokError(Twine("invalid debug info flag flag '") +
    3862           0 :                       Lex.getStrVal() + "'");
    3863        3457 :     Lex.Lex();
    3864        3457 :     return false;
    3865        2879 :   };
    3866             : 
    3867             :   // Parse the flags and combine them together.
    3868             :   DINode::DIFlags Combined = DINode::FlagZero;
    3869             :   do {
    3870             :     DINode::DIFlags Val;
    3871        3459 :     if (parseFlag(Val))
    3872           0 :       return true;
    3873        3459 :     Combined |= Val;
    3874             :   } while (EatIfPresent(lltok::bar));
    3875             : 
    3876             :   Result.assign(Combined);
    3877             :   return false;
    3878             : }
    3879             : 
    3880             : template <>
    3881         243 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3882             :                             MDSignedField &Result) {
    3883         243 :   if (Lex.getKind() != lltok::APSInt)
    3884           0 :     return TokError("expected signed integer");
    3885             : 
    3886             :   auto &S = Lex.getAPSIntVal();
    3887         243 :   if (S < Result.Min)
    3888           2 :     return TokError("value for '" + Name + "' too small, limit is " +
    3889           4 :                     Twine(Result.Min));
    3890         241 :   if (S > Result.Max)
    3891           2 :     return TokError("value for '" + Name + "' too large, limit is " +
    3892           4 :                     Twine(Result.Max));
    3893             :   Result.assign(S.getExtValue());
    3894             :   assert(Result.Val >= Result.Min && "Expected value in range");
    3895             :   assert(Result.Val <= Result.Max && "Expected value in range");
    3896         239 :   Lex.Lex();
    3897             :   return false;
    3898             : }
    3899             : 
    3900             : template <>
    3901       11862 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
    3902       11862 :   switch (Lex.getKind()) {
    3903             :   default:
    3904           0 :     return TokError("expected 'true' or 'false'");
    3905        5370 :   case lltok::kw_true:
    3906             :     Result.assign(true);
    3907             :     break;
    3908        6492 :   case lltok::kw_false:
    3909             :     Result.assign(false);
    3910             :     break;
    3911             :   }
    3912       11862 :   Lex.Lex();
    3913             :   return false;
    3914             : }
    3915             : 
    3916             : template <>
    3917       50866 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
    3918       50866 :   if (Lex.getKind() == lltok::kw_null) {
    3919         679 :     if (!Result.AllowNull)
    3920           6 :       return TokError("'" + Name + "' cannot be null");
    3921         673 :     Lex.Lex();
    3922             :     Result.assign(nullptr);
    3923             :     return false;
    3924             :   }
    3925             : 
    3926             :   Metadata *MD;
    3927       50187 :   if (ParseMetadata(MD, nullptr))
    3928             :     return true;
    3929             : 
    3930       50187 :   Result.assign(MD);
    3931             :   return false;
    3932             : }
    3933             : 
    3934             : template <>
    3935         183 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3936             :                             MDSignedOrMDField &Result) {
    3937             :   // Try to parse a signed int.
    3938         183 :   if (Lex.getKind() == lltok::APSInt) {
    3939         164 :     MDSignedField Res = Result.A;
    3940         164 :     if (!ParseMDField(Loc, Name, Res)) {
    3941             :       Result.assign(Res);
    3942             :       return false;
    3943             :     }
    3944             :     return true;
    3945             :   }
    3946             : 
    3947             :   // Otherwise, try to parse as an MDField.
    3948          19 :   MDField Res = Result.B;
    3949          19 :   if (!ParseMDField(Loc, Name, Res)) {
    3950             :     Result.assign(Res);
    3951             :     return false;
    3952             :   }
    3953             : 
    3954             :   return true;
    3955             : }
    3956             : 
    3957             : template <>
    3958        6156 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    3959             :                             MDSignedOrUnsignedField &Result) {
    3960        6156 :   if (Lex.getKind() != lltok::APSInt)
    3961             :     return false;
    3962             : 
    3963        6156 :   if (Lex.getAPSIntVal().isSigned()) {
    3964          26 :     MDSignedField Res = Result.A;
    3965          26 :     if (ParseMDField(Loc, Name, Res))
    3966             :       return true;
    3967             :     Result.assign(Res);
    3968             :     return false;
    3969             :   }
    3970             : 
    3971        6130 :   MDUnsignedField Res = Result.B;
    3972        6130 :   if (ParseMDField(Loc, Name, Res))
    3973             :     return true;
    3974             :   Result.assign(Res);
    3975             :   return false;
    3976             : }
    3977             : 
    3978             : template <>
    3979       24452 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
    3980       24452 :   LocTy ValueLoc = Lex.getLoc();
    3981             :   std::string S;
    3982       24452 :   if (ParseStringConstant(S))
    3983             :     return true;
    3984             : 
    3985       25406 :   if (!Result.AllowEmpty && S.empty())
    3986           1 :     return Error(ValueLoc, "'" + Name + "' cannot be empty");
    3987             : 
    3988       72996 :   Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
    3989             :   return false;
    3990             : }
    3991             : 
    3992             : template <>
    3993          12 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
    3994             :   SmallVector<Metadata *, 4> MDs;
    3995          12 :   if (ParseMDNodeVector(MDs))
    3996             :     return true;
    3997             : 
    3998          12 :   Result.assign(std::move(MDs));
    3999             :   return false;
    4000             : }
    4001             : 
    4002             : template <>
    4003         136 : bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
    4004             :                             ChecksumKindField &Result) {
    4005             :   Optional<DIFile::ChecksumKind> CSKind =
    4006         136 :       DIFile::getChecksumKind(Lex.getStrVal());
    4007             : 
    4008         136 :   if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
    4009             :     return TokError(
    4010           0 :         "invalid checksum kind" + Twine(" '") + Lex.getStrVal() + "'");
    4011             : 
    4012         136 :   Result.assign(*CSKind);
    4013         136 :   Lex.Lex();
    4014             :   return false;
    4015             : }
    4016             : 
    4017             : } // end namespace llvm
    4018             : 
    4019             : template <class ParserTy>
    4020       34912 : bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
    4021             :   do {
    4022      143416 :     if (Lex.getKind() != lltok::LabelStr)
    4023           0 :       return TokError("expected field label here");
    4024             : 
    4025      143416 :     if (parseField())
    4026             :       return true;
    4027             :   } while (EatIfPresent(lltok::comma));
    4028             : 
    4029             :   return false;
    4030             : }
    4031             : 
    4032             : template <class ParserTy>
    4033       34939 : bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
    4034             :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
    4035       34939 :   Lex.Lex();
    4036             : 
    4037       34939 :   if (ParseToken(lltok::lparen, "expected '(' here"))
    4038             :     return true;
    4039       34939 :   if (Lex.getKind() != lltok::rparen)
    4040       34912 :     if (ParseMDFieldsImplBody(parseField))
    4041             :       return true;
    4042             : 
    4043       34916 :   ClosingLoc = Lex.getLoc();
    4044       34916 :   return ParseToken(lltok::rparen, "expected ')' here");
    4045             : }
    4046             : 
    4047             : template <class FieldTy>
    4048      143415 : bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
    4049      143415 :   if (Result.Seen)
    4050           1 :     return TokError("field '" + Name + "' cannot be specified more than once");
    4051             : 
    4052             :   LocTy Loc = Lex.getLoc();
    4053      143414 :   Lex.Lex();
    4054      143414 :   return ParseMDField(Loc, Name, Result);
    4055             : }
    4056             : 
    4057       37886 : bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
    4058             :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
    4059             : 
    4060             : #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \
    4061             :   if (Lex.getStrVal() == #CLASS)                                               \
    4062             :     return Parse##CLASS(N, IsDistinct);
    4063             : #include "llvm/IR/Metadata.def"
    4064             : 
    4065           1 :   return TokError("expected metadata type");
    4066             : }
    4067             : 
    4068             : #define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
    4069             : #define NOP_FIELD(NAME, TYPE, INIT)
    4070             : #define REQUIRE_FIELD(NAME, TYPE, INIT)                                        \
    4071             :   if (!NAME.Seen)                                                              \
    4072             :     return Error(ClosingLoc, "missing required field '" #NAME "'");
    4073             : #define PARSE_MD_FIELD(NAME, TYPE, DEFAULT)                                    \
    4074             :   if (Lex.getStrVal() == #NAME)                                                \
    4075             :     return ParseMDField(#NAME, NAME);
    4076             : #define PARSE_MD_FIELDS()                                                      \
    4077             :   VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD)                                \
    4078             :   do {                                                                         \
    4079             :     LocTy ClosingLoc;                                                          \
    4080             :     if (ParseMDFieldsImpl([&]() -> bool {                                      \
    4081             :       VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD)                          \
    4082             :       return TokError(Twine("invalid field '") + Lex.getStrVal() + "'");       \
    4083             :     }, ClosingLoc))                                                            \
    4084             :       return true;                                                             \
    4085             :     VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD)                                  \
    4086             :   } while (false)
    4087             : #define GET_OR_DISTINCT(CLASS, ARGS)                                           \
    4088             :   (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
    4089             : 
    4090             : /// ParseDILocationFields:
    4091             : ///   ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
    4092        8786 : bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
    4093             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4094             :   OPTIONAL(line, LineField, );                                                 \
    4095             :   OPTIONAL(column, ColumnField, );                                             \
    4096             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4097             :   OPTIONAL(inlinedAt, MDField, );
    4098      109121 :   PARSE_MD_FIELDS();
    4099             : #undef VISIT_MD_FIELDS
    4100             : 
    4101       26337 :   Result = GET_OR_DISTINCT(
    4102             :       DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val));
    4103        8779 :   return false;
    4104             : }
    4105             : 
    4106             : /// ParseGenericDINode:
    4107             : ///   ::= !GenericDINode(tag: 15, header: "...", operands: {...})
    4108          39 : bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
    4109             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4110             :   REQUIRED(tag, DwarfTagField, );                                              \
    4111             :   OPTIONAL(header, MDStringField, );                                           \
    4112             :   OPTIONAL(operands, MDFieldList, );
    4113         270 :   PARSE_MD_FIELDS();
    4114             : #undef VISIT_MD_FIELDS
    4115             : 
    4116         105 :   Result = GET_OR_DISTINCT(GenericDINode,
    4117             :                            (Context, tag.Val, header.Val, operands.Val));
    4118          35 :   return false;
    4119             : }
    4120             : 
    4121             : /// ParseDISubrange:
    4122             : ///   ::= !DISubrange(count: 30, lowerBound: 2)
    4123             : ///   ::= !DISubrange(count: !node, lowerBound: 2)
    4124         184 : bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
    4125             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4126             :   REQUIRED(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false));              \
    4127             :   OPTIONAL(lowerBound, MDSignedField, );
    4128         896 :   PARSE_MD_FIELDS();
    4129             : #undef VISIT_MD_FIELDS
    4130             : 
    4131         178 :   if (count.isMDSignedField())
    4132         480 :     Result = GET_OR_DISTINCT(
    4133             :         DISubrange, (Context, count.getMDSignedValue(), lowerBound.Val));
    4134          18 :   else if (count.isMDField())
    4135          54 :     Result = GET_OR_DISTINCT(
    4136             :         DISubrange, (Context, count.getMDFieldValue(), lowerBound.Val));
    4137             :   else
    4138             :     return true;
    4139             : 
    4140             :   return false;
    4141             : }
    4142             : 
    4143             : /// ParseDIEnumerator:
    4144             : ///   ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
    4145        6157 : bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
    4146             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4147             :   REQUIRED(name, MDStringField, );                                             \
    4148             :   REQUIRED(value, MDSignedOrUnsignedField, );                                  \
    4149             :   OPTIONAL(isUnsigned, MDBoolField, (false));
    4150       49303 :   PARSE_MD_FIELDS();
    4151             : #undef VISIT_MD_FIELDS
    4152             : 
    4153        6165 :   if (isUnsigned.Val && value.isMDSignedField())
    4154           0 :     return TokError("unsigned enumerator with negative value");
    4155             : 
    4156        6155 :   int64_t Value = value.isMDSignedField()
    4157       12284 :                       ? value.getMDSignedValue()
    4158        6129 :                       : static_cast<int64_t>(value.getMDUnsignedValue());
    4159       12310 :   Result =
    4160        6155 :       GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
    4161             : 
    4162        6155 :   return false;
    4163             : }
    4164             : 
    4165             : /// ParseDIBasicType:
    4166             : ///   ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32)
    4167        1423 : bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
    4168             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4169             :   OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type));                     \
    4170             :   OPTIONAL(name, MDStringField, );                                             \
    4171             :   OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
    4172             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
    4173             :   OPTIONAL(encoding, DwarfAttEncodingField, );
    4174       30745 :   PARSE_MD_FIELDS();
    4175             : #undef VISIT_MD_FIELDS
    4176             : 
    4177        4269 :   Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
    4178             :                                          align.Val, encoding.Val));
    4179        1423 :   return false;
    4180             : }
    4181             : 
    4182             : /// ParseDIDerivedType:
    4183             : ///   ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
    4184             : ///                      line: 7, scope: !1, baseType: !2, size: 32,
    4185             : ///                      align: 32, offset: 0, flags: 0, extraData: !3,
    4186             : ///                      dwarfAddressSpace: 3)
    4187        2065 : bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
    4188             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4189             :   REQUIRED(tag, DwarfTagField, );                                              \
    4190             :   OPTIONAL(name, MDStringField, );                                             \
    4191             :   OPTIONAL(file, MDField, );                                                   \
    4192             :   OPTIONAL(line, LineField, );                                                 \
    4193             :   OPTIONAL(scope, MDField, );                                                  \
    4194             :   REQUIRED(baseType, MDField, );                                               \
    4195             :   OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
    4196             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
    4197             :   OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX));                          \
    4198             :   OPTIONAL(flags, DIFlagField, );                                              \
    4199             :   OPTIONAL(extraData, MDField, );                                              \
    4200             :   OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX));
    4201       78932 :   PARSE_MD_FIELDS();
    4202             : #undef VISIT_MD_FIELDS
    4203             : 
    4204             :   Optional<unsigned> DWARFAddressSpace;
    4205        2063 :   if (dwarfAddressSpace.Val != UINT32_MAX)
    4206          21 :     DWARFAddressSpace = dwarfAddressSpace.Val;
    4207             : 
    4208        8252 :   Result = GET_OR_DISTINCT(DIDerivedType,
    4209             :                            (Context, tag.Val, name.Val, file.Val, line.Val,
    4210             :                             scope.Val, baseType.Val, size.Val, align.Val,
    4211             :                             offset.Val, DWARFAddressSpace, flags.Val,
    4212             :                             extraData.Val));
    4213             :   return false;
    4214             : }
    4215             : 
    4216         960 : bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
    4217             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4218             :   REQUIRED(tag, DwarfTagField, );                                              \
    4219             :   OPTIONAL(name, MDStringField, );                                             \
    4220             :   OPTIONAL(file, MDField, );                                                   \
    4221             :   OPTIONAL(line, LineField, );                                                 \
    4222             :   OPTIONAL(scope, MDField, );                                                  \
    4223             :   OPTIONAL(baseType, MDField, );                                               \
    4224             :   OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX));                            \
    4225             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));                           \
    4226             :   OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX));                          \
    4227             :   OPTIONAL(flags, DIFlagField, );                                              \
    4228             :   OPTIONAL(elements, MDField, );                                               \
    4229             :   OPTIONAL(runtimeLang, DwarfLangField, );                                     \
    4230             :   OPTIONAL(vtableHolder, MDField, );                                           \
    4231             :   OPTIONAL(templateParams, MDField, );                                         \
    4232             :   OPTIONAL(identifier, MDStringField, );                                       \
    4233             :   OPTIONAL(discriminator, MDField, );
    4234       51401 :   PARSE_MD_FIELDS();
    4235             : #undef VISIT_MD_FIELDS
    4236             : 
    4237             :   // If this has an identifier try to build an ODR type.
    4238         959 :   if (identifier.Val)
    4239        2664 :     if (auto *CT = DICompositeType::buildODRType(
    4240         888 :             Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
    4241         444 :             scope.Val, baseType.Val, size.Val, align.Val, offset.Val, flags.Val,
    4242         444 :             elements.Val, runtimeLang.Val, vtableHolder.Val,
    4243         444 :             templateParams.Val, discriminator.Val)) {
    4244          83 :       Result = CT;
    4245          83 :       return false;
    4246             :     }
    4247             : 
    4248             :   // Create a new node, and save it in the context if it belongs in the type
    4249             :   // map.
    4250        2380 :   Result = GET_OR_DISTINCT(
    4251             :       DICompositeType,
    4252             :       (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
    4253             :        size.Val, align.Val, offset.Val, flags.Val, elements.Val,
    4254             :        runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
    4255             :        discriminator.Val));
    4256         876 :   return false;
    4257             : }
    4258             : 
    4259        1866 : bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
    4260             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4261             :   OPTIONAL(flags, DIFlagField, );                                              \
    4262             :   OPTIONAL(cc, DwarfCCField, );                                                \
    4263             :   REQUIRED(types, MDField, );
    4264       11568 :   PARSE_MD_FIELDS();
    4265             : #undef VISIT_MD_FIELDS
    4266             : 
    4267        5595 :   Result = GET_OR_DISTINCT(DISubroutineType,
    4268             :                            (Context, flags.Val, cc.Val, types.Val));
    4269        1865 :   return false;
    4270             : }
    4271             : 
    4272             : /// ParseDIFileType:
    4273             : ///   ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
    4274             : ///                   checksumkind: CSK_MD5,
    4275             : ///                   checksum: "000102030405060708090a0b0c0d0e0f",
    4276             : ///                   source: "source file contents")
    4277        2525 : bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
    4278             :   // The default constructed value for checksumkind is required, but will never
    4279             :   // be used, as the parser checks if the field was actually Seen before using
    4280             :   // the Val.
    4281             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4282             :   REQUIRED(filename, MDStringField, );                                         \
    4283             :   REQUIRED(directory, MDStringField, );                                        \
    4284             :   OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5));                \
    4285             :   OPTIONAL(checksum, MDStringField, );                                         \
    4286             :   OPTIONAL(source, MDStringField, );
    4287       21889 :   PARSE_MD_FIELDS();
    4288             : #undef VISIT_MD_FIELDS
    4289             : 
    4290             :   Optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
    4291        2659 :   if (checksumkind.Seen && checksum.Seen)
    4292             :     OptChecksum.emplace(checksumkind.Val, checksum.Val);
    4293        2387 :   else if (checksumkind.Seen || checksum.Seen)
    4294           0 :     return Lex.Error("'checksumkind' and 'checksum' must be provided together");
    4295             : 
    4296             :   Optional<MDString *> OptSource;
    4297        2523 :   if (source.Seen)
    4298          28 :     OptSource = source.Val;
    4299        5046 :   Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val,
    4300             :                                     OptChecksum, OptSource));
    4301             :   return false;
    4302             : }
    4303             : 
    4304             : /// ParseDICompileUnit:
    4305             : ///   ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
    4306             : ///                      isOptimized: true, flags: "-O2", runtimeVersion: 1,
    4307             : ///                      splitDebugFilename: "abc.debug",
    4308             : ///                      emissionKind: FullDebug, enums: !1, retainedTypes: !2,
    4309             : ///                      globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd)
    4310        1915 : bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
    4311        1915 :   if (!IsDistinct)
    4312           2 :     return Lex.Error("missing 'distinct', required for !DICompileUnit");
    4313             : 
    4314             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4315             :   REQUIRED(language, DwarfLangField, );                                        \
    4316             :   REQUIRED(file, MDField, (/* AllowNull */ false));                            \
    4317             :   OPTIONAL(producer, MDStringField, );                                         \
    4318             :   OPTIONAL(isOptimized, MDBoolField, );                                        \
    4319             :   OPTIONAL(flags, MDStringField, );                                            \
    4320             :   OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX));                  \
    4321             :   OPTIONAL(splitDebugFilename, MDStringField, );                               \
    4322             :   OPTIONAL(emissionKind, EmissionKindField, );                                 \
    4323             :   OPTIONAL(enums, MDField, );                                                  \
    4324             :   OPTIONAL(retainedTypes, MDField, );                                          \
    4325             :   OPTIONAL(globals, MDField, );                                                \
    4326             :   OPTIONAL(imports, MDField, );                                                \
    4327             :   OPTIONAL(macros, MDField, );                                                 \
    4328             :   OPTIONAL(dwoId, MDUnsignedField, );                                          \
    4329             :   OPTIONAL(splitDebugInlining, MDBoolField, = true);                           \
    4330             :   OPTIONAL(debugInfoForProfiling, MDBoolField, = false);                       \
    4331             :   OPTIONAL(gnuPubnames, MDBoolField, = false);
    4332      114253 :   PARSE_MD_FIELDS();
    4333             : #undef VISIT_MD_FIELDS
    4334             : 
    4335       15272 :   Result = DICompileUnit::getDistinct(
    4336        3818 :       Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
    4337        3818 :       runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
    4338             :       retainedTypes.Val, globals.Val, imports.Val, macros.Val, dwoId.Val,
    4339        5727 :       splitDebugInlining.Val, debugInfoForProfiling.Val, gnuPubnames.Val);
    4340        1909 :   return false;
    4341             : }
    4342             : 
    4343             : /// ParseDISubprogram:
    4344             : ///   ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
    4345             : ///                     file: !1, line: 7, type: !2, isLocal: false,
    4346             : ///                     isDefinition: true, scopeLine: 8, containingType: !3,
    4347             : ///                     virtuality: DW_VIRTUALTIY_pure_virtual,
    4348             : ///                     virtualIndex: 10, thisAdjustment: 4, flags: 11,
    4349             : ///                     isOptimized: false, templateParams: !4, declaration: !5,
    4350             : ///                     retainedNodes: !6, thrownTypes: !7)
    4351        2842 : bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
    4352        2842 :   auto Loc = Lex.getLoc();
    4353             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4354             :   OPTIONAL(scope, MDField, );                                                  \
    4355             :   OPTIONAL(name, MDStringField, );                                             \
    4356             :   OPTIONAL(linkageName, MDStringField, );                                      \
    4357             :   OPTIONAL(file, MDField, );                                                   \
    4358             :   OPTIONAL(line, LineField, );                                                 \
    4359             :   OPTIONAL(type, MDField, );                                                   \
    4360             :   OPTIONAL(isLocal, MDBoolField, );                                            \
    4361             :   OPTIONAL(isDefinition, MDBoolField, (true));                                 \
    4362             :   OPTIONAL(scopeLine, LineField, );                                            \
    4363             :   OPTIONAL(containingType, MDField, );                                         \
    4364             :   OPTIONAL(virtuality, DwarfVirtualityField, );                                \
    4365             :   OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX));                    \
    4366             :   OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX));          \
    4367             :   OPTIONAL(flags, DIFlagField, );                                              \
    4368             :   OPTIONAL(isOptimized, MDBoolField, );                                        \
    4369             :   OPTIONAL(unit, MDField, );                                                   \
    4370             :   OPTIONAL(templateParams, MDField, );                                         \
    4371             :   OPTIONAL(declaration, MDField, );                                            \
    4372             :   OPTIONAL(retainedNodes, MDField, );                                              \
    4373             :   OPTIONAL(thrownTypes, MDField, );
    4374      337427 :   PARSE_MD_FIELDS();
    4375             : #undef VISIT_MD_FIELDS
    4376             : 
    4377        2842 :   if (isDefinition.Val && !IsDistinct)
    4378           2 :     return Lex.Error(
    4379             :         Loc,
    4380           1 :         "missing 'distinct', required for !DISubprogram when 'isDefinition'");
    4381             : 
    4382        6317 :   Result = GET_OR_DISTINCT(
    4383             :       DISubprogram,
    4384             :       (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
    4385             :        type.Val, isLocal.Val, isDefinition.Val, scopeLine.Val,
    4386             :        containingType.Val, virtuality.Val, virtualIndex.Val, thisAdjustment.Val,
    4387             :        flags.Val, isOptimized.Val, unit.Val, templateParams.Val,
    4388             :        declaration.Val, retainedNodes.Val, thrownTypes.Val));
    4389        2841 :   return false;
    4390             : }
    4391             : 
    4392             : /// ParseDILexicalBlock:
    4393             : ///   ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
    4394        1101 : bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
    4395             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4396             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4397             :   OPTIONAL(file, MDField, );                                                   \
    4398             :   OPTIONAL(line, LineField, );                                                 \
    4399             :   OPTIONAL(column, ColumnField, );
    4400       20417 :   PARSE_MD_FIELDS();
    4401             : #undef VISIT_MD_FIELDS
    4402             : 
    4403        3297 :   Result = GET_OR_DISTINCT(
    4404             :       DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
    4405        1099 :   return false;
    4406             : }
    4407             : 
    4408             : /// ParseDILexicalBlockFile:
    4409             : ///   ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
    4410         245 : bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
    4411             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4412             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4413             :   OPTIONAL(file, MDField, );                                                   \
    4414             :   REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
    4415        3137 :   PARSE_MD_FIELDS();
    4416             : #undef VISIT_MD_FIELDS
    4417             : 
    4418         726 :   Result = GET_OR_DISTINCT(DILexicalBlockFile,
    4419             :                            (Context, scope.Val, file.Val, discriminator.Val));
    4420         242 :   return false;
    4421             : }
    4422             : 
    4423             : /// ParseDINamespace:
    4424             : ///   ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
    4425         103 : bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
    4426             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4427             :   REQUIRED(scope, MDField, );                                                  \
    4428             :   OPTIONAL(name, MDStringField, );                                             \
    4429             :   OPTIONAL(exportSymbols, MDBoolField, );
    4430         793 :   PARSE_MD_FIELDS();
    4431             : #undef VISIT_MD_FIELDS
    4432             : 
    4433         306 :   Result = GET_OR_DISTINCT(DINamespace,
    4434             :                            (Context, scope.Val, name.Val, exportSymbols.Val));
    4435         102 :   return false;
    4436             : }
    4437             : 
    4438             : /// ParseDIMacro:
    4439             : ///   ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: "SomeValue")
    4440          21 : bool LLParser::ParseDIMacro(MDNode *&Result, bool IsDistinct) {
    4441             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4442             :   REQUIRED(type, DwarfMacinfoTypeField, );                                     \
    4443             :   OPTIONAL(line, LineField, );                                                 \
    4444             :   REQUIRED(name, MDStringField, );                                             \
    4445             :   OPTIONAL(value, MDStringField, );
    4446         367 :   PARSE_MD_FIELDS();
    4447             : #undef VISIT_MD_FIELDS
    4448             : 
    4449          63 :   Result = GET_OR_DISTINCT(DIMacro,
    4450             :                            (Context, type.Val, line.Val, name.Val, value.Val));
    4451          21 :   return false;
    4452             : }
    4453             : 
    4454             : /// ParseDIMacroFile:
    4455             : ///   ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
    4456          27 : bool LLParser::ParseDIMacroFile(MDNode *&Result, bool IsDistinct) {
    4457             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4458             :   OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file));       \
    4459             :   OPTIONAL(line, LineField, );                                                 \
    4460             :   REQUIRED(file, MDField, );                                                   \
    4461             :   OPTIONAL(nodes, MDField, );
    4462         372 :   PARSE_MD_FIELDS();
    4463             : #undef VISIT_MD_FIELDS
    4464             : 
    4465          81 :   Result = GET_OR_DISTINCT(DIMacroFile,
    4466             :                            (Context, type.Val, line.Val, file.Val, nodes.Val));
    4467          27 :   return false;
    4468             : }
    4469             : 
    4470             : /// ParseDIModule:
    4471             : ///   ::= !DIModule(scope: !0, name: "SomeModule", configMacros: "-DNDEBUG",
    4472             : ///                 includePath: "/usr/include", isysroot: "/")
    4473          21 : bool LLParser::ParseDIModule(MDNode *&Result, bool IsDistinct) {
    4474             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4475             :   REQUIRED(scope, MDField, );                                                  \
    4476             :   REQUIRED(name, MDStringField, );                                             \
    4477             :   OPTIONAL(configMacros, MDStringField, );                                     \
    4478             :   OPTIONAL(includePath, MDStringField, );                                      \
    4479             :   OPTIONAL(isysroot, MDStringField, );
    4480         367 :   PARSE_MD_FIELDS();
    4481             : #undef VISIT_MD_FIELDS
    4482             : 
    4483          63 :   Result = GET_OR_DISTINCT(DIModule, (Context, scope.Val, name.Val,
    4484             :                            configMacros.Val, includePath.Val, isysroot.Val));
    4485          21 :   return false;
    4486             : }
    4487             : 
    4488             : /// ParseDITemplateTypeParameter:
    4489             : ///   ::= !DITemplateTypeParameter(name: "Ty", type: !1)
    4490          56 : bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
    4491             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4492             :   OPTIONAL(name, MDStringField, );                                             \
    4493             :   REQUIRED(type, MDField, );
    4494         380 :   PARSE_MD_FIELDS();
    4495             : #undef VISIT_MD_FIELDS
    4496             : 
    4497         110 :   Result =
    4498          55 :       GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
    4499          55 :   return false;
    4500             : }
    4501             : 
    4502             : /// ParseDITemplateValueParameter:
    4503             : ///   ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
    4504             : ///                                 name: "V", type: !1, value: i32 7)
    4505          56 : bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
    4506             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4507             :   OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter));      \
    4508             :   OPTIONAL(name, MDStringField, );                                             \
    4509             :   OPTIONAL(type, MDField, );                                                   \
    4510             :   REQUIRED(value, MDField, );
    4511         859 :   PARSE_MD_FIELDS();
    4512             : #undef VISIT_MD_FIELDS
    4513             : 
    4514         165 :   Result = GET_OR_DISTINCT(DITemplateValueParameter,
    4515             :                            (Context, tag.Val, name.Val, type.Val, value.Val));
    4516          55 :   return false;
    4517             : }
    4518             : 
    4519             : /// ParseDIGlobalVariable:
    4520             : ///   ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
    4521             : ///                         file: !1, line: 7, type: !2, isLocal: false,
    4522             : ///                         isDefinition: true, declaration: !3, align: 8)
    4523         955 : bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
    4524             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4525             :   REQUIRED(name, MDStringField, (/* AllowEmpty */ false));                     \
    4526             :   OPTIONAL(scope, MDField, );                                                  \
    4527             :   OPTIONAL(linkageName, MDStringField, );                                      \
    4528             :   OPTIONAL(file, MDField, );                                                   \
    4529             :   OPTIONAL(line, LineField, );                                                 \
    4530             :   OPTIONAL(type, MDField, );                                                   \
    4531             :   OPTIONAL(isLocal, MDBoolField, );                                            \
    4532             :   OPTIONAL(isDefinition, MDBoolField, (true));                                 \
    4533             :   OPTIONAL(declaration, MDField, );                                            \
    4534             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
    4535       46619 :   PARSE_MD_FIELDS();
    4536             : #undef VISIT_MD_FIELDS
    4537             : 
    4538        2859 :   Result = GET_OR_DISTINCT(DIGlobalVariable,
    4539             :                            (Context, scope.Val, name.Val, linkageName.Val,
    4540             :                             file.Val, line.Val, type.Val, isLocal.Val,
    4541             :                             isDefinition.Val, declaration.Val, align.Val));
    4542         953 :   return false;
    4543             : }
    4544             : 
    4545             : /// ParseDILocalVariable:
    4546             : ///   ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
    4547             : ///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
    4548             : ///                        align: 8)
    4549             : ///   ::= !DILocalVariable(scope: !0, name: "foo",
    4550             : ///                        file: !1, line: 7, type: !2, arg: 2, flags: 7,
    4551             : ///                        align: 8)
    4552        2352 : bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
    4553             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4554             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4555             :   OPTIONAL(name, MDStringField, );                                             \
    4556             :   OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX));                             \
    4557             :   OPTIONAL(file, MDField, );                                                   \
    4558             :   OPTIONAL(line, LineField, );                                                 \
    4559             :   OPTIONAL(type, MDField, );                                                   \
    4560             :   OPTIONAL(flags, DIFlagField, );                                              \
    4561             :   OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
    4562       72334 :   PARSE_MD_FIELDS();
    4563             : #undef VISIT_MD_FIELDS
    4564             : 
    4565        7044 :   Result = GET_OR_DISTINCT(DILocalVariable,
    4566             :                            (Context, scope.Val, name.Val, file.Val, line.Val,
    4567             :                             type.Val, arg.Val, flags.Val, align.Val));
    4568        2348 :   return false;
    4569             : }
    4570             : 
    4571             : /// ParseDILabel:
    4572             : ///   ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7)
    4573          14 : bool LLParser::ParseDILabel(MDNode *&Result, bool IsDistinct) {
    4574             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4575             :   REQUIRED(scope, MDField, (/* AllowNull */ false));                           \
    4576             :   REQUIRED(name, MDStringField, );                                             \
    4577             :   REQUIRED(file, MDField, );                                                   \
    4578             :   REQUIRED(line, LineField, );
    4579         266 :   PARSE_MD_FIELDS();
    4580             : #undef VISIT_MD_FIELDS
    4581             : 
    4582          42 :   Result = GET_OR_DISTINCT(DILabel,
    4583             :                            (Context, scope.Val, name.Val, file.Val, line.Val));
    4584          14 :   return false;
    4585             : }
    4586             : 
    4587             : /// ParseDIExpression:
    4588             : ///   ::= !DIExpression(0, 7, -1)
    4589        2985 : bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
    4590             :   assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
    4591        2985 :   Lex.Lex();
    4592             : 
    4593        2985 :   if (ParseToken(lltok::lparen, "expected '(' here"))
    4594             :     return true;
    4595             : 
    4596             :   SmallVector<uint64_t, 8> Elements;
    4597        2985 :   if (Lex.getKind() != lltok::rparen)
    4598             :     do {
    4599         679 :       if (Lex.getKind() == lltok::DwarfOp) {
    4600         790 :         if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
    4601             :           Lex.Lex();
    4602         395 :           Elements.push_back(Op);
    4603         395 :           continue;
    4604             :         }
    4605           0 :         return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
    4606             :       }
    4607             : 
    4608         284 :       if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
    4609           0 :         return TokError("expected unsigned integer");
    4610             : 
    4611             :       auto &U = Lex.getAPSIntVal();
    4612         284 :       if (U.ugt(UINT64_MAX))
    4613           2 :         return TokError("element too large, limit is " + Twine(UINT64_MAX));
    4614         283 :       Elements.push_back(U.getZExtValue());
    4615             :       Lex.Lex();
    4616             :     } while (EatIfPresent(lltok::comma));
    4617             : 
    4618        2984 :   if (ParseToken(lltok::rparen, "expected ')' here"))
    4619             :     return true;
    4620             : 
    4621        8952 :   Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
    4622        2984 :   return false;
    4623             : }
    4624             : 
    4625             : /// ParseDIGlobalVariableExpression:
    4626             : ///   ::= !DIGlobalVariableExpression(var: !0, expr: !1)
    4627         942 : bool LLParser::ParseDIGlobalVariableExpression(MDNode *&Result,
    4628             :                                                bool IsDistinct) {
    4629             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4630             :   REQUIRED(var, MDField, );                                                    \
    4631             :   REQUIRED(expr, MDField, );
    4632        7536 :   PARSE_MD_FIELDS();
    4633             : #undef VISIT_MD_FIELDS
    4634             : 
    4635        1884 :   Result =
    4636         942 :       GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
    4637         942 :   return false;
    4638             : }
    4639             : 
    4640             : /// ParseDIObjCProperty:
    4641             : ///   ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
    4642             : ///                       getter: "getFoo", attributes: 7, type: !2)
    4643          21 : bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
    4644             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4645             :   OPTIONAL(name, MDStringField, );                                             \
    4646             :   OPTIONAL(file, MDField, );                                                   \
    4647             :   OPTIONAL(line, LineField, );                                                 \
    4648             :   OPTIONAL(setter, MDStringField, );                                           \
    4649             :   OPTIONAL(getter, MDStringField, );                                           \
    4650             :   OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX));                      \
    4651             :   OPTIONAL(type, MDField, );
    4652         467 :   PARSE_MD_FIELDS();
    4653             : #undef VISIT_MD_FIELDS
    4654             : 
    4655          63 :   Result = GET_OR_DISTINCT(DIObjCProperty,
    4656             :                            (Context, name.Val, file.Val, line.Val, setter.Val,
    4657             :                             getter.Val, attributes.Val, type.Val));
    4658          21 :   return false;
    4659             : }
    4660             : 
    4661             : /// ParseDIImportedEntity:
    4662             : ///   ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
    4663             : ///                         line: 7, name: "foo")
    4664         264 : bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
    4665             : #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED)                                    \
    4666             :   REQUIRED(tag, DwarfTagField, );                                              \
    4667             :   REQUIRED(scope, MDField, );                                                  \
    4668             :   OPTIONAL(entity, MDField, );                                                 \
    4669             :   OPTIONAL(file, MDField, );                                                   \
    4670             :   OPTIONAL(line, LineField, );                                                 \
    4671             :   OPTIONAL(name, MDStringField, );
    4672        6692 :   PARSE_MD_FIELDS();
    4673             : #undef VISIT_MD_FIELDS
    4674             : 
    4675         786 :   Result = GET_OR_DISTINCT(
    4676             :       DIImportedEntity,
    4677             :       (Context, tag.Val, scope.Val, entity.Val, file.Val, line.Val, name.Val));
    4678         262 :   return false;
    4679             : }
    4680             : 
    4681             : #undef PARSE_MD_FIELD
    4682             : #undef NOP_FIELD
    4683             : #undef REQUIRE_FIELD
    4684             : #undef DECLARE_FIELD
    4685             : 
    4686             : /// ParseMetadataAsValue
    4687             : ///  ::= metadata i32 %local
    4688             : ///  ::= metadata i32 @global
    4689             : ///  ::= metadata i32 7
    4690             : ///  ::= metadata !0
    4691             : ///  ::= metadata !{...}
    4692             : ///  ::= metadata !"string"
    4693        8688 : bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
    4694             :   // Note: the type 'metadata' has already been parsed.
    4695             :   Metadata *MD;
    4696        8688 :   if (ParseMetadata(MD, &PFS))
    4697             :     return true;
    4698             : 
    4699        8685 :   V = MetadataAsValue::get(Context, MD);
    4700        8685 :   return false;
    4701             : }
    4702             : 
    4703             : /// ParseValueAsMetadata
    4704             : ///  ::= i32 %local
    4705             : ///  ::= i32 @global
    4706             : ///  ::= i32 7
    4707       21644 : bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
    4708             :                                     PerFunctionState *PFS) {
    4709             :   Type *Ty;
    4710             :   LocTy Loc;
    4711       21644 :   if (ParseType(Ty, TypeMsg, Loc))
    4712             :     return true;
    4713       43286 :   if (Ty->isMetadataTy())
    4714           1 :     return Error(Loc, "invalid metadata-value-metadata roundtrip");
    4715             : 
    4716             :   Value *V;
    4717       21642 :   if (ParseValue(Ty, V, PFS))
    4718             :     return true;
    4719             : 
    4720       21637 :   MD = ValueAsMetadata::get(V);
    4721       21637 :   return false;
    4722             : }
    4723             : 
    4724             : /// ParseMetadata
    4725             : ///  ::= i32 %local
    4726             : ///  ::= i32 @global
    4727             : ///  ::= i32 7
    4728             : ///  ::= !42
    4729             : ///  ::= !{...}
    4730             : ///  ::= !"string"
    4731             : ///  ::= !DILocation(...)
    4732      110416 : bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
    4733      110416 :   if (Lex.getKind() == lltok::MetadataVar) {
    4734             :     MDNode *N;
    4735        2557 :     if (ParseSpecializedMDNode(N))
    4736             :       return true;
    4737        2557 :     MD = N;
    4738        2557 :     return false;
    4739             :   }
    4740             : 
    4741             :   // ValueAsMetadata:
    4742             :   // <type> <value>
    4743      107859 :   if (Lex.getKind() != lltok::exclaim)
    4744       21644 :     return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
    4745             : 
    4746             :   // '!'.
    4747             :   assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
    4748       86215 :   Lex.Lex();
    4749             : 
    4750             :   // MDString:
    4751             :   //   ::= '!' STRINGCONSTANT
    4752       86215 :   if (Lex.getKind() == lltok::StringConstant) {
    4753             :     MDString *S;
    4754       11805 :     if (ParseMDString(S))
    4755             :       return true;
    4756       11805 :     MD = S;
    4757       11805 :     return false;
    4758             :   }
    4759             : 
    4760             :   // MDNode:
    4761             :   // !{ ... }
    4762             :   // !7
    4763             :   MDNode *N;
    4764       74410 :   if (ParseMDNodeTail(N))
    4765             :     return true;
    4766       74407 :   MD = N;
    4767       74407 :   return false;
    4768             : }
    4769             : 
    4770             : //===----------------------------------------------------------------------===//
    4771             : // Function Parsing.
    4772             : //===----------------------------------------------------------------------===//
    4773             : 
    4774     3352379 : bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
    4775             :                                    PerFunctionState *PFS, bool IsCall) {
    4776     3352379 :   if (Ty->isFunctionTy())
    4777           0 :     return Error(ID.Loc, "functions are not values, refer to them as pointers");
    4778             : 
    4779     3352379 :   switch (ID.Kind) {
    4780      253893 :   case ValID::t_LocalID:
    4781      253893 :     if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
    4782      253893 :     V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc, IsCall);
    4783      253893 :     return V == nullptr;
    4784     1365403 :   case ValID::t_LocalName:
    4785     1365407 :     if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
    4786     1365399 :     V = PFS->GetVal(ID.StrVal, Ty, ID.Loc, IsCall);
    4787     1365399 :     return V == nullptr;
    4788       12199 :   case ValID::t_InlineAsm: {
    4789       36593 :     if (!ID.FTy || !InlineAsm::Verify(ID.FTy, ID.StrVal2))
    4790           3 :       return Error(ID.Loc, "invalid type for inline asm constraint string");
    4791       24392 :     V = InlineAsm::get(ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1,
    4792             :                        (ID.UIntVal >> 1) & 1,
    4793       12196 :                        (InlineAsm::AsmDialect(ID.UIntVal >> 2)));
    4794       12196 :     return false;
    4795             :   }
    4796      199302 :   case ValID::t_GlobalName:
    4797      199302 :     V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
    4798      199302 :     return V == nullptr;
    4799         663 :   case ValID::t_GlobalID:
    4800         663 :     V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
    4801         663 :     return V == nullptr;
    4802             :   case ValID::t_APSInt:
    4803     1143411 :     if (!Ty->isIntegerTy())
    4804           1 :       return Error(ID.Loc, "integer constant must have integer type");
    4805     2286820 :     ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
    4806     1143410 :     V = ConstantInt::get(Context, ID.APSIntVal);
    4807     1143410 :     return false;
    4808             :   case ValID::t_APFloat:
    4809       45133 :     if (!Ty->isFloatingPointTy() ||
    4810       45133 :         !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
    4811           2 :       return Error(ID.Loc, "floating point constant invalid for type");
    4812             : 
    4813             :     // The lexer has no type info, so builds all half, float, and double FP
    4814             :     // constants as double.  Fix this here.  Long double does not need this.
    4815       45133 :     if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
    4816             :       bool Ignored;
    4817       43641 :       if (Ty->isHalfTy())
    4818        1834 :         ID.APFloatVal.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
    4819             :                               &Ignored);
    4820       41807 :       else if (Ty->isFloatTy())
    4821       30326 :         ID.APFloatVal.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
    4822             :                               &Ignored);
    4823             :     }
    4824       45133 :     V = ConstantFP::get(Context, ID.APFloatVal);
    4825             : 
    4826       45133 :     if (V->getType() != Ty)
    4827           3 :       return Error(ID.Loc, "floating point constant does not have type '" +
    4828           4 :                    getTypeString(Ty) + "'");
    4829             : 
    4830             :     return false;
    4831             :   case ValID::t_Null:
    4832        7466 :     if (!Ty->isPointerTy())
    4833           0 :       return Error(ID.Loc, "null must be a pointer type");
    4834        7466 :     V = ConstantPointerNull::get(cast<PointerType>(Ty));
    4835        7466 :     return false;
    4836             :   case ValID::t_Undef:
    4837             :     // FIXME: LabelTy should not be a first-class type.
    4838      161175 :     if (!Ty->isFirstClassType() || Ty->isLabelTy())
    4839           0 :       return Error(ID.Loc, "invalid type for undef constant");
    4840      161175 :     V = UndefValue::get(Ty);
    4841      161175 :     return false;
    4842             :   case ValID::t_EmptyArray:
    4843          16 :     if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
    4844           0 :       return Error(ID.Loc, "invalid empty array initializer");
    4845          16 :     V = UndefValue::get(Ty);
    4846          16 :     return false;
    4847             :   case ValID::t_Zero:
    4848             :     // FIXME: LabelTy should not be a first-class type.
    4849       25532 :     if (!Ty->isFirstClassType() || Ty->isLabelTy())
    4850           0 :       return Error(ID.Loc, "invalid type for null constant");
    4851       25532 :     V = Constant::getNullValue(Ty);
    4852       25532 :     return false;
    4853             :   case ValID::t_None:
    4854         474 :     if (!Ty->isTokenTy())
    4855           0 :       return Error(ID.Loc, "invalid type for none constant");
    4856         474 :     V = Constant::getNullValue(Ty);
    4857         474 :     return false;
    4858      135943 :   case ValID::t_Constant:
    4859      135943 :     if (ID.ConstantVal->getType() != Ty)
    4860           0 :       return Error(ID.Loc, "constant expression type mismatch");
    4861             : 
    4862      135943 :     V = ID.ConstantVal;
    4863      135943 :     return false;
    4864             :   case ValID::t_ConstantStruct:
    4865             :   case ValID::t_PackedConstantStruct:
    4866             :     if (StructType *ST = dyn_cast<StructType>(Ty)) {
    4867        1767 :       if (ST->getNumElements() != ID.UIntVal)
    4868           1 :         return Error(ID.Loc,
    4869             :                      "initializer with struct type has wrong # elements");
    4870        1766 :       if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
    4871           0 :         return Error(ID.Loc, "packed'ness of initializer and type don't match");
    4872             : 
    4873             :       // Verify that the elements are compatible with the structtype.
    4874       11964 :       for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
    4875       15300 :         if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
    4876           1 :           return Error(ID.Loc, "element " + Twine(i) +
    4877           1 :                     " of struct initializer doesn't match struct element type");
    4878             : 
    4879        3530 :       V = ConstantStruct::get(
    4880             :           ST, makeArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
    4881             :     } else
    4882           0 :       return Error(ID.Loc, "constant expression type mismatch");
    4883        1765 :     return false;
    4884             :   }
    4885           0 :   llvm_unreachable("Invalid ValID");
    4886             : }
    4887             : 
    4888         626 : bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
    4889         626 :   C = nullptr;
    4890        1252 :   ValID ID;
    4891         626 :   auto Loc = Lex.getLoc();
    4892         626 :   if (ParseValID(ID, /*PFS=*/nullptr))
    4893             :     return true;
    4894         626 :   switch (ID.Kind) {
    4895         621 :   case ValID::t_APSInt:
    4896             :   case ValID::t_APFloat:
    4897             :   case ValID::t_Undef:
    4898             :   case ValID::t_Constant:
    4899             :   case ValID::t_ConstantStruct:
    4900             :   case ValID::t_PackedConstantStruct: {
    4901             :     Value *V;
    4902         621 :     if (ConvertValIDToValue(Ty, ID, V, /*PFS=*/nullptr, /*IsCall=*/false))
    4903             :       return true;
    4904             :     assert(isa<Constant>(V) && "Expected a constant value");
    4905         619 :     C = cast<Constant>(V);
    4906         619 :     return false;
    4907             :   }
    4908           4 :   case ValID::t_Null:
    4909           4 :     C = Constant::getNullValue(Ty);
    4910           4 :     return false;
    4911             :   default:
    4912           1 :     return Error(Loc, "expected a constant value");
    4913             :   }
    4914             : }
    4915             : 
    4916     2222231 : bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
    4917     2222231 :   V = nullptr;
    4918     4444462 :   ValID ID;
    4919     4444458 :   return ParseValID(ID, PFS) ||
    4920     4444458 :          ConvertValIDToValue(Ty, ID, V, PFS, /*IsCall=*/false);
    4921             : }
    4922             : 
    4923     1470196 : bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
    4924     1470196 :   Type *Ty = nullptr;
    4925     2940392 :   return ParseType(Ty) ||
    4926     2940392 :          ParseValue(Ty, V, PFS);
    4927             : }
    4928             : 
    4929       83604 : bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
    4930             :                                       PerFunctionState &PFS) {
    4931             :   Value *V;
    4932       83604 :   Loc = Lex.getLoc();
    4933       83604 :   if (ParseTypeAndValue(V, PFS)) return true;
    4934      167208 :   if (!isa<BasicBlock>(V))
    4935           0 :     return Error(Loc, "expected a basic block");
    4936       83604 :   BB = cast<BasicBlock>(V);
    4937       83604 :   return false;
    4938             : }
    4939             : 
    4940             : /// FunctionHeader
    4941             : ///   ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
    4942             : ///       OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
    4943             : ///       '(' ArgList ')' OptFuncAttrs OptSection OptionalAlign OptGC
    4944             : ///       OptionalPrefix OptionalPrologue OptPersonalityFn
    4945      285073 : bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
    4946             :   // Parse the linkage.
    4947      285073 :   LocTy LinkageLoc = Lex.getLoc();
    4948             :   unsigned Linkage;
    4949             :   unsigned Visibility;
    4950             :   unsigned DLLStorageClass;
    4951             :   bool DSOLocal;
    4952             :   AttrBuilder RetAttrs;
    4953             :   unsigned CC;
    4954             :   bool HasLinkage;
    4955      285073 :   Type *RetType = nullptr;
    4956             :   LocTy RetTypeLoc = Lex.getLoc();
    4957      285073 :   if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
    4958      285072 :                            DSOLocal) ||
    4959      855215 :       ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
    4960             :       ParseType(RetType, RetTypeLoc, true /*void allowed*/))
    4961             :     return true;
    4962             : 
    4963             :   // Verify that the linkage is ok.
    4964      285070 :   switch ((GlobalValue::LinkageTypes)Linkage) {
    4965             :   case GlobalValue::ExternalLinkage:
    4966             :     break; // always ok.
    4967          52 :   case GlobalValue::ExternalWeakLinkage:
    4968          52 :     if (isDefine)
    4969           0 :       return Error(LinkageLoc, "invalid linkage for function definition");
    4970             :     break;
    4971        2515 :   case GlobalValue::PrivateLinkage:
    4972             :   case GlobalValue::InternalLinkage:
    4973             :   case GlobalValue::AvailableExternallyLinkage:
    4974             :   case GlobalValue::LinkOnceAnyLinkage:
    4975             :   case GlobalValue::LinkOnceODRLinkage:
    4976             :   case GlobalValue::WeakAnyLinkage:
    4977             :   case GlobalValue::WeakODRLinkage:
    4978        2515 :     if (!isDefine)
    4979           0 :       return Error(LinkageLoc, "invalid linkage for function declaration");
    4980             :     break;
    4981             :   case GlobalValue::AppendingLinkage:
    4982             :   case GlobalValue::CommonLinkage:
    4983           0 :     return Error(LinkageLoc, "invalid function linkage type");
    4984             :   }
    4985             : 
    4986      285070 :   if (!isValidVisibilityForLinkage(Visibility, Linkage))
    4987           4 :     return Error(LinkageLoc,
    4988             :                  "symbol with local linkage must have default visibility");
    4989             : 
    4990      285066 :   if (!FunctionType::isValidReturnType(RetType))
    4991           0 :     return Error(RetTypeLoc, "invalid function return type");
    4992             : 
    4993      285066 :   LocTy NameLoc = Lex.getLoc();
    4994             : 
    4995             :   std::string FunctionName;
    4996      285066 :   if (Lex.getKind() == lltok::GlobalVar) {
    4997             :     FunctionName = Lex.getStrVal();
    4998          38 :   } else if (Lex.getKind() == lltok::GlobalID) {     // @42 is ok.
    4999          38 :     unsigned NameID = Lex.getUIntVal();
    5000             : 
    5001          76 :     if (NameID != NumberedVals.size())
    5002           0 :       return TokError("function expected to be numbered '%" +
    5003           0 :                       Twine(NumberedVals.size()) + "'");
    5004             :   } else {
    5005           0 :     return TokError("expected function name");
    5006             :   }
    5007             : 
    5008      285066 :   Lex.Lex();
    5009             : 
    5010      285066 :   if (Lex.getKind() != lltok::lparen)
    5011           0 :     return TokError("expected '(' in function argument list");
    5012             : 
    5013      285066 :   SmallVector<ArgInfo, 8> ArgList;
    5014             :   bool isVarArg;
    5015             :   AttrBuilder FuncAttrs;
    5016             :   std::vector<unsigned> FwdRefAttrGrps;
    5017      285066 :   LocTy BuiltinLoc;
    5018             :   std::string Section;
    5019             :   unsigned Alignment;
    5020             :   std::string GC;
    5021      285066 :   GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
    5022      285066 :   Constant *Prefix = nullptr;
    5023      285066 :   Constant *Prologue = nullptr;
    5024      285066 :   Constant *PersonalityFn = nullptr;
    5025             :   Comdat *C;
    5026             : 
    5027      570130 :   if (ParseArgumentList(ArgList, isVarArg) ||
    5028      570128 :       ParseOptionalUnnamedAddr(UnnamedAddr) ||
    5029      285064 :       ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
    5030             :                                  BuiltinLoc) ||
    5031         166 :       (EatIfPresent(lltok::kw_section) &&
    5032      285229 :        ParseStringConstant(Section)) ||
    5033      570129 :       parseOptionalComdat(FunctionName, C) ||
    5034      285062 :       ParseOptionalAlignment(Alignment) ||
    5035         486 :       (EatIfPresent(lltok::kw_gc) &&
    5036         486 :        ParseStringConstant(GC)) ||
    5037          25 :       (EatIfPresent(lltok::kw_prefix) &&
    5038          25 :        ParseGlobalTypeAndValue(Prefix)) ||
    5039          46 :       (EatIfPresent(lltok::kw_prologue) &&
    5040      285112 :        ParseGlobalTypeAndValue(Prologue)) ||
    5041        1059 :       (EatIfPresent(lltok::kw_personality) &&
    5042        1059 :        ParseGlobalTypeAndValue(PersonalityFn)))
    5043             :     return true;
    5044             : 
    5045      285062 :   if (FuncAttrs.contains(Attribute::Builtin))
    5046           0 :     return Error(BuiltinLoc, "'builtin' attribute not valid on function");
    5047             : 
    5048             :   // If the alignment was parsed as an attribute, move to the alignment field.
    5049      285062 :   if (FuncAttrs.hasAlignmentAttr()) {
    5050         626 :     Alignment = FuncAttrs.getAlignment();
    5051         626 :     FuncAttrs.removeAttribute(Attribute::Alignment);
    5052             :   }
    5053             : 
    5054             :   // Okay, if we got here, the function is syntactically valid.  Convert types
    5055             :   // and do semantic checks.
    5056             :   std::vector<Type*> ParamTypeList;
    5057             :   SmallVector<AttributeSet, 8> Attrs;
    5058             : 
    5059      775756 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    5060      981388 :     ParamTypeList.push_back(ArgList[i].Ty);
    5061      490694 :     Attrs.push_back(ArgList[i].Attrs);
    5062             :   }
    5063             : 
    5064             :   AttributeList PAL =
    5065             :       AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
    5066      285062 :                          AttributeSet::get(Context, RetAttrs), Attrs);
    5067             : 
    5068      285747 :   if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
    5069           0 :     return Error(RetTypeLoc, "functions with 'sret' argument must return void");
    5070             : 
    5071             :   FunctionType *FT =
    5072      570124 :     FunctionType::get(RetType, ParamTypeList, isVarArg);
    5073             :   PointerType *PFT = PointerType::getUnqual(FT);
    5074             : 
    5075      285062 :   Fn = nullptr;
    5076      285062 :   if (!FunctionName.empty()) {
    5077             :     // If this was a definition of a forward reference, remove the definition
    5078             :     // from the forward reference table and fill in the forward ref.
    5079             :     auto FRVI = ForwardRefVals.find(FunctionName);
    5080      285019 :     if (FRVI != ForwardRefVals.end()) {
    5081       76048 :       Fn = M->getFunction(FunctionName);
    5082       38024 :       if (!Fn)
    5083           1 :         return Error(FRVI->second.second, "invalid forward reference to "
    5084             :                      "function as global value!");
    5085       38023 :       if (Fn->getType() != PFT)
    5086           1 :         return Error(FRVI->second.second, "invalid forward reference to "
    5087           3 :                      "function '" + FunctionName + "' with wrong type!");
    5088             : 
    5089             :       ForwardRefVals.erase(FRVI);
    5090      493990 :     } else if ((Fn = M->getFunction(FunctionName))) {
    5091             :       // Reject redefinitions.
    5092           3 :       return Error(NameLoc, "invalid redefinition of function '" +
    5093           2 :                    FunctionName + "'");
    5094      493988 :     } else if (M->getNamedValue(FunctionName)) {
    5095           0 :       return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
    5096             :     }
    5097             : 
    5098             :   } else {
    5099             :     // If this is a definition of a forward referenced function, make sure the
    5100             :     // types agree.
    5101          86 :     auto I = ForwardRefValIDs.find(NumberedVals.size());
    5102          43 :     if (I != ForwardRefValIDs.end()) {
    5103          20 :       Fn = cast<Function>(I->second.first);
    5104          20 :       if (Fn->getType() != PFT)
    5105           0 :         return Error(NameLoc, "type of definition and forward reference of '@" +
    5106           0 :                      Twine(NumberedVals.size()) + "' disagree");
    5107             :       ForwardRefValIDs.erase(I);
    5108             :     }
    5109             :   }
    5110             : 
    5111      285059 :   if (!Fn)
    5112      494034 :     Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
    5113             :   else // Move the forward-reference to the correct spot in the module.
    5114       76084 :     M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
    5115             : 
    5116      285059 :   if (FunctionName.empty())
    5117          86 :     NumberedVals.push_back(Fn);
    5118             : 
    5119      285059 :   Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
    5120      285059 :   maybeSetDSOLocal(DSOLocal, *Fn);
    5121      285059 :   Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
    5122      285059 :   Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
    5123      285059 :   Fn->setCallingConv(CC);
    5124             :   Fn->setAttributes(PAL);
    5125      285059 :   Fn->setUnnamedAddr(UnnamedAddr);
    5126      285059 :   Fn->setAlignment(Alignment);
    5127      570118 :   Fn->setSection(Section);
    5128      285059 :   Fn->setComdat(C);
    5129      285059 :   Fn->setPersonalityFn(PersonalityFn);
    5130      286031 :   if (!GC.empty()) Fn->setGC(GC);
    5131      285059 :   Fn->setPrefixData(Prefix);
    5132      285059 :   Fn->setPrologueData(Prologue);
    5133      285059 :   ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
    5134             : 
    5135             :   // Add all of the arguments we parsed to the function.
    5136      285059 :   Function::arg_iterator ArgIt = Fn->arg_begin();
    5137      775748 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
    5138             :     // If the argument has a name, insert it into the argument symbol table.
    5139      981378 :     if (ArgList[i].Name.empty()) continue;
    5140             : 
    5141             :     // Set the name, if it conflicted, it will be auto-renamed.
    5142      764126 :     ArgIt->setName(ArgList[i].Name);
    5143             : 
    5144      382063 :     if (ArgIt->getName() != ArgList[i].Name)
    5145           0 :       return Error(ArgList[i].Loc, "redefinition of argument '%" +
    5146           0 :                    ArgList[i].Name + "'");
    5147             :   }
    5148             : 
    5149      285059 :   if (isDefine)
    5150             :     return false;
    5151             : 
    5152             :   // Check the declaration has no block address forward references.
    5153      118160 :   ValID ID;
    5154       59080 :   if (FunctionName.empty()) {
    5155           0 :     ID.Kind = ValID::t_GlobalID;
    5156           0 :     ID.UIntVal = NumberedVals.size() - 1;
    5157             :   } else {
    5158       59080 :     ID.Kind = ValID::t_GlobalName;
    5159             :     ID.StrVal = FunctionName;
    5160             :   }
    5161             :   auto Blocks = ForwardRefBlockAddresses.find(ID);
    5162       59080 :   if (Blocks != ForwardRefBlockAddresses.end())
    5163           0 :     return Error(Blocks->first.Loc,
    5164             :                  "cannot take blockaddress inside a declaration");
    5165             :   return false;
    5166             : }
    5167             : 
    5168      225979 : bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
    5169      451958 :   ValID ID;
    5170      225979 :   if (FunctionNumber == -1) {
    5171      225936 :     ID.Kind = ValID::t_GlobalName;
    5172      677808 :     ID.StrVal = F.getName();
    5173             :   } else {
    5174          43 :     ID.Kind = ValID::t_GlobalID;
    5175          43 :     ID.UIntVal = FunctionNumber;
    5176             :   }
    5177             : 
    5178      225979 :   auto Blocks = P.ForwardRefBlockAddresses.find(ID);
    5179      451958 :   if (Blocks == P.ForwardRefBlockAddresses.end())
    5180             :     return false;
    5181             : 
    5182         384 :   for (const auto &I : Blocks->second) {
    5183         269 :     const ValID &BBID = I.first;
    5184         269 :     GlobalValue *GV = I.second;
    5185             : 
    5186             :     assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
    5187             :            "Expected local id or name");
    5188             :     BasicBlock *BB;
    5189         269 :     if (BBID.Kind == ValID::t_LocalName)
    5190         260 :       BB = GetBB(BBID.StrVal, BBID.Loc);
    5191             :     else
    5192           9 :       BB = GetBB(BBID.UIntVal, BBID.Loc);
    5193         269 :     if (!BB)
    5194           0 :       return P.Error(BBID.Loc, "referenced value is not a basic block");
    5195             : 
    5196         269 :     GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
    5197         269 :     GV->eraseFromParent();
    5198             :   }
    5199             : 
    5200         115 :   P.ForwardRefBlockAddresses.erase(Blocks);
    5201         115 :   return false;
    5202             : }
    5203             : 
    5204             : /// ParseFunctionBody
    5205             : ///   ::= '{' BasicBlock+ UseListOrderDirective* '}'
    5206      225979 : bool LLParser::ParseFunctionBody(Function &Fn) {
    5207      225979 :   if (Lex.getKind() != lltok::lbrace)
    5208           0 :     return TokError("expected '{' in function body");
    5209      225979 :   Lex.Lex();  // eat the {.
    5210             : 
    5211             :   int FunctionNumber = -1;
    5212      226022 :   if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
    5213             : 
    5214      451958 :   PerFunctionState PFS(*this, Fn, FunctionNumber);
    5215             : 
    5216             :   // Resolve block addresses and allow basic blocks to be forward-declared
    5217             :   // within this function.
    5218      225979 :   if (PFS.resolveForwardRefBlockAddresses())
    5219             :     return true;
    5220      225979 :   SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
    5221             : 
    5222             :   // We need at least one basic block.
    5223      225979 :   if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
    5224           0 :     return TokError("function body requires at least one basic block");
    5225             : 
    5226      539136 :   while (Lex.getKind() != lltok::rbrace &&
    5227             :          Lex.getKind() != lltok::kw_uselistorder)
    5228      313224 :     if (ParseBasicBlock(PFS)) return true;
    5229             : 
    5230      226660 :   while (Lex.getKind() != lltok::rbrace)
    5231         751 :     if (ParseUseListOrder(&PFS))
    5232             :       return true;
    5233             : 
    5234             :   // Eat the }.
    5235             :   Lex.Lex();
    5236             : 
    5237             :   // Verify function is ok.
    5238      225909 :   return PFS.FinishFunction();
    5239             : }
    5240             : 
    5241             : /// ParseBasicBlock
    5242             : ///   ::= LabelStr? Instruction*
    5243      313224 : bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
    5244             :   // If this basic block starts out with a name, remember it.
    5245             :   std::string Name;
    5246      313224 :   LocTy NameLoc = Lex.getLoc();
    5247      313224 :   if (Lex.getKind() == lltok::LabelStr) {
    5248             :     Name = Lex.getStrVal();
    5249      164023 :     Lex.Lex();
    5250             :   }
    5251             : 
    5252      313224 :   BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
    5253      313224 :   if (!BB)
    5254           1 :     return Error(NameLoc,
    5255           3 :                  "unable to create block named '" + Name + "'");
    5256             : 
    5257             :   std::string NameStr;
    5258             : 
    5259             :   // Parse the instructions in this block until we get a terminator.
    5260             :   Instruction *Inst;
    5261             :   do {
    5262             :     // This instruction may have three possibilities for a name: a) none
    5263             :     // specified, b) name specified "%foo =", c) number specified: "%4 =".
    5264     1362140 :     LocTy NameLoc = Lex.getLoc();
    5265             :     int NameID = -1;
    5266             :     NameStr = "";
    5267             : 
    5268     1362140 :     if (Lex.getKind() == lltok::LocalVarID) {
    5269      225862 :       NameID = Lex.getUIntVal();
    5270      225862 :       Lex.Lex();
    5271      225862 :       if (ParseToken(lltok::equal, "expected '=' after instruction id"))
    5272          66 :         return true;
    5273     1136278 :     } else if (Lex.getKind() == lltok::LocalVar) {
    5274             :       NameStr = Lex.getStrVal();
    5275      653677 :       Lex.Lex();
    5276      653677 :       if (ParseToken(lltok::equal, "expected '=' after instruction name"))
    5277             :         return true;
    5278             :     }
    5279             : 
    5280     1362140 :     switch (ParseInstruction(Inst, BB, PFS)) {
    5281           0 :     default: llvm_unreachable("Unknown ParseInstruction result!");
    5282             :     case InstError: return true;
    5283             :     case InstNormal:
    5284     1351680 :       BB->getInstList().push_back(Inst);
    5285             : 
    5286             :       // With a normal result, we check to see if the instruction is followed by
    5287             :       // a comma and metadata.
    5288             :       if (EatIfPresent(lltok::comma))
    5289       14844 :         if (ParseInstructionMetadata(*Inst))
    5290             :           return true;
    5291             :       break;
    5292             :     case InstExtraComma:
    5293       10399 :       BB->getInstList().push_back(Inst);
    5294             : 
    5295             :       // If the instruction parser ate an extra comma at the end of it, it
    5296             :       // *must* be followed by metadata.
    5297       10399 :       if (ParseInstructionMetadata(*Inst))
    5298             :         return true;
    5299             :       break;
    5300             :     }
    5301             : 
    5302             :     // Set the name on the instruction.
    5303     1362075 :     if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
    5304     2724148 :   } while (!isa<TerminatorInst>(Inst));
    5305             : 
    5306             :   return false;
    5307             : }
    5308             : 
    5309             : //===----------------------------------------------------------------------===//
    5310             : // Instruction Parsing.
    5311             : //===----------------------------------------------------------------------===//
    5312             : 
    5313             : /// ParseInstruction - Parse one of the many different instructions.
    5314             : ///
    5315     1362140 : int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
    5316             :                                PerFunctionState &PFS) {
    5317     1362140 :   lltok::Kind Token = Lex.getKind();
    5318     1362140 :   if (Token == lltok::Eof)
    5319           1 :     return TokError("found end of file when expecting more instructions");
    5320     1362139 :   LocTy Loc = Lex.getLoc();
    5321     1362139 :   unsigned KeywordVal = Lex.getUIntVal();
    5322     1362139 :   Lex.Lex();  // Eat the keyword.
    5323             : 
    5324     1362139 :   switch (Token) {
    5325           2 :   default:                    return Error(Loc, "expected instruction opcode");
    5326             :   // Terminator Instructions.
    5327        3674 :   case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
    5328      229240 :   case lltok::kw_ret:         return ParseRet(Inst, BB, PFS);
    5329       75757 :   case lltok::kw_br:          return ParseBr(Inst, PFS);
    5330        1387 :   case lltok::kw_switch:      return ParseSwitch(Inst, PFS);
    5331         326 :   case lltok::kw_indirectbr:  return ParseIndirectBr(Inst, PFS);
    5332        1903 :   case lltok::kw_invoke:      return ParseInvoke(Inst, PFS);
    5333         248 :   case lltok::kw_resume:      return ParseResume(Inst, PFS);
    5334         181 :   case lltok::kw_cleanupret:  return ParseCleanupRet(Inst, PFS);
    5335         195 :   case lltok::kw_catchret:    return ParseCatchRet(Inst, PFS);
    5336         258 :   case lltok::kw_catchswitch: return ParseCatchSwitch(Inst, PFS);
    5337         282 :   case lltok::kw_catchpad:    return ParseCatchPad(Inst, PFS);
    5338         266 :   case lltok::kw_cleanuppad:  return ParseCleanupPad(Inst, PFS);
    5339             :   // Binary Operators.
    5340             :   case lltok::kw_add:
    5341             :   case lltok::kw_sub:
    5342             :   case lltok::kw_mul:
    5343             :   case lltok::kw_shl: {
    5344             :     bool NUW = EatIfPresent(lltok::kw_nuw);
    5345             :     bool NSW = EatIfPresent(lltok::kw_nsw);
    5346       74016 :     if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
    5347             : 
    5348       74016 :     if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
    5349             : 
    5350       74015 :     if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
    5351       74015 :     if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
    5352             :     return false;
    5353             :   }
    5354       35720 :   case lltok::kw_fadd:
    5355             :   case lltok::kw_fsub:
    5356             :   case lltok::kw_fmul:
    5357             :   case lltok::kw_fdiv:
    5358             :   case lltok::kw_frem: {
    5359       35720 :     FastMathFlags FMF = EatFastMathFlagsIfPresent();
    5360       35720 :     int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
    5361       35720 :     if (Res != 0)
    5362             :       return Res;
    5363       35720 :     if (FMF.any())
    5364        3958 :       Inst->setFastMathFlags(FMF);
    5365             :     return 0;
    5366             :   }
    5367             : 
    5368             :   case lltok::kw_sdiv:
    5369             :   case lltok::kw_udiv:
    5370             :   case lltok::kw_lshr:
    5371             :   case lltok::kw_ashr: {
    5372             :     bool Exact = EatIfPresent(lltok::kw_exact);
    5373             : 
    5374       18033 :     if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
    5375       18033 :     if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
    5376             :     return false;
    5377             :   }
    5378             : 
    5379        3737 :   case lltok::kw_urem:
    5380        3737 :   case lltok::kw_srem:   return ParseArithmetic(Inst, PFS, KeywordVal, 1);
    5381       33300 :   case lltok::kw_and:
    5382             :   case lltok::kw_or:
    5383       33300 :   case lltok::kw_xor:    return ParseLogical(Inst, PFS, KeywordVal);
    5384       53476 :   case lltok::kw_icmp:   return ParseCompare(Inst, PFS, KeywordVal);
    5385        9817 :   case lltok::kw_fcmp: {
    5386        9817 :     FastMathFlags FMF = EatFastMathFlagsIfPresent();
    5387        9817 :     int Res = ParseCompare(Inst, PFS, KeywordVal);
    5388        9817 :     if (Res != 0)
    5389             :       return Res;
    5390        9817 :     if (FMF.any())
    5391         738 :       Inst->setFastMathFlags(FMF);
    5392             :     return 0;
    5393             :   }
    5394             : 
    5395             :   // Casts.
    5396      116806 :   case lltok::kw_trunc:
    5397             :   case lltok::kw_zext:
    5398             :   case lltok::kw_sext:
    5399             :   case lltok::kw_fptrunc:
    5400             :   case lltok::kw_fpext:
    5401             :   case lltok::kw_bitcast:
    5402             :   case lltok::kw_addrspacecast:
    5403             :   case lltok::kw_uitofp:
    5404             :   case lltok::kw_sitofp:
    5405             :   case lltok::kw_fptoui:
    5406             :   case lltok::kw_fptosi:
    5407             :   case lltok::kw_inttoptr:
    5408      116806 :   case lltok::kw_ptrtoint:       return ParseCast(Inst, PFS, KeywordVal);
    5409             :   // Other.
    5410       23620 :   case lltok::kw_select:         return ParseSelect(Inst, PFS);
    5411         210 :   case lltok::kw_va_arg:         return ParseVA_Arg(Inst, PFS);
    5412       22817 :   case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
    5413       31205 :   case lltok::kw_insertelement:  return ParseInsertElement(Inst, PFS);
    5414       38799 :   case lltok::kw_shufflevector:  return ParseShuffleVector(Inst, PFS);
    5415       23540 :   case lltok::kw_phi:            return ParsePHI(Inst, PFS);
    5416        1109 :   case lltok::kw_landingpad:     return ParseLandingPad(Inst, PFS);
    5417             :   // Call.
    5418       98145 :   case lltok::kw_call:     return ParseCall(Inst, PFS, CallInst::TCK_None);
    5419       33370 :   case lltok::kw_tail:     return ParseCall(Inst, PFS, CallInst::TCK_Tail);
    5420         168 :   case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
    5421          68 :   case lltok::kw_notail:   return ParseCall(Inst, PFS, CallInst::TCK_NoTail);
    5422             :   // Memory.
    5423       47885 :   case lltok::kw_alloca:         return ParseAlloc(Inst, PFS);
    5424      156590 :   case lltok::kw_load:           return ParseLoad(Inst, PFS);
    5425      122311 :   case lltok::kw_store:          return ParseStore(Inst, PFS);
    5426        1065 :   case lltok::kw_cmpxchg:        return ParseCmpXchg(Inst, PFS);
    5427        5873 :   case lltok::kw_atomicrmw:      return ParseAtomicRMW(Inst, PFS);
    5428         671 :   case lltok::kw_fence:          return ParseFence(Inst, PFS);
    5429       88544 :   case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
    5430        6164 :   case lltok::kw_extractvalue:  return ParseExtractValue(Inst, PFS);
    5431        1361 :   case lltok::kw_insertvalue:   return ParseInsertValue(Inst, PFS);
    5432             :   }
    5433             : }
    5434             : 
    5435             : /// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
    5436       63563 : bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
    5437       63563 :   if (Opc == Instruction::FCmp) {
    5438        9823 :     switch (Lex.getKind()) {
    5439           0 :     default: return TokError("expected fcmp predicate (e.g. 'oeq')");
    5440        2548 :     case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
    5441         295 :     case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
    5442        1307 :     case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
    5443        1837 :     case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
    5444         454 :     case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
    5445         519 :     case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
    5446         256 :     case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
    5447         294 :     case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
    5448         261 :     case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
    5449         525 :     case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
    5450         386 :     case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
    5451         349 :     case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
    5452         280 :     case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
    5453         366 :     case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
    5454          72 :     case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
    5455          74 :     case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
    5456             :     }
    5457             :   } else {
    5458       53740 :     switch (Lex.getKind()) {
    5459           0 :     default: return TokError("expected icmp predicate (e.g. 'eq')");
    5460       19069 :     case lltok::kw_eq:  P = CmpInst::ICMP_EQ; break;
    5461        6927 :     case lltok::kw_ne:  P = CmpInst::ICMP_NE; break;
    5462        7994 :     case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
    5463        7390 :     case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
    5464        1519 :     case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
    5465        1619 :     case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
    5466        4223 :     case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
    5467        2609 :     case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
    5468        1141 :     case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
    5469        1249 :     case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
    5470             :     }
    5471             :   }
    5472       63563 :   Lex.Lex();
    5473       63563 :   return false;
    5474             : }
    5475             : 
    5476             : //===----------------------------------------------------------------------===//
    5477             : // Terminator Instructions.
    5478             : //===----------------------------------------------------------------------===//
    5479             : 
    5480             : /// ParseRet - Parse a return instruction.
    5481             : ///   ::= 'ret' void (',' !dbg, !1)*
    5482             : ///   ::= 'ret' TypeAndValue (',' !dbg, !1)*
    5483      229240 : bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
    5484             :                         PerFunctionState &PFS) {
    5485      229240 :   SMLoc TypeLoc = Lex.getLoc();
    5486      229240 :   Type *Ty = nullptr;
    5487      229240 :   if (ParseType(Ty, true /*void allowed*/)) return true;
    5488             : 
    5489      229240 :   Type *ResType = PFS.getFunction().getReturnType();
    5490             : 
    5491      458480 :   if (Ty->isVoidTy()) {
    5492       73550 :     if (!ResType->isVoidTy())
    5493           0 :       return Error(TypeLoc, "value doesn't match function result type '" +
    5494           0 :                    getTypeString(ResType) + "'");
    5495             : 
    5496      147100 :     Inst = ReturnInst::Create(Context);
    5497       73550 :     return false;
    5498             :   }
    5499             : 
    5500             :   Value *RV;
    5501      155690 :   if (ParseValue(Ty, RV, PFS)) return true;
    5502             : 
    5503      155683 :   if (ResType != RV->getType())
    5504           6 :     return Error(TypeLoc, "value doesn't match function result type '" +
    5505           8 :                  getTypeString(ResType) + "'");
    5506             : 
    5507      155681 :   Inst = ReturnInst::Create(Context, RV);
    5508      155681 :   return false;
    5509             : }
    5510             : 
    5511             : /// ParseBr
    5512             : ///   ::= 'br' TypeAndValue
    5513             : ///   ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    5514       75757 : bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
    5515       75757 :   LocTy Loc, Loc2;
    5516             :   Value *Op0;
    5517             :   BasicBlock *Op1, *Op2;
    5518       75757 :   if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
    5519             : 
    5520       75757 :   if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
    5521       40754 :     Inst = BranchInst::Create(BB);
    5522       40754 :     return false;
    5523             :   }
    5524             : 
    5525       35003 :   if (Op0->getType() != Type::getInt1Ty(Context))
    5526           0 :     return Error(Loc, "branch condition must have 'i1' type");
    5527             : 
    5528       70006 :   if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
    5529       70006 :       ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
    5530      105009 :       ParseToken(lltok::comma, "expected ',' after true destination") ||
    5531       35003 :       ParseTypeAndBasicBlock(Op2, Loc2, PFS))
    5532             :     return true;
    5533             : 
    5534       70006 :   Inst = BranchInst::Create(Op1, Op2, Op0);
    5535       35003 :   return false;
    5536             : }
    5537             : 
    5538             : /// ParseSwitch
    5539             : ///  Instruction
    5540             : ///    ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
    5541             : ///  JumpTable
    5542             : ///    ::= (TypeAndValue ',' TypeAndValue)*
    5543        1387 : bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
    5544        1387 :   LocTy CondLoc, BBLoc;
    5545             :   Value *Cond;
    5546             :   BasicBlock *DefaultBB;
    5547        1387 :   if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
    5548        2774 :       ParseToken(lltok::comma, "expected ',' after switch condition") ||
    5549        4161 :       ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
    5550        1387 :       ParseToken(lltok::lsquare, "expected '[' with switch table"))
    5551             :     return true;
    5552             : 
    5553        2774 :   if (!Cond->getType()->isIntegerTy())
    5554           0 :     return Error(CondLoc, "switch condition must have integer type");
    5555             : 
    5556             :   // Parse the jump table pairs.
    5557             :   SmallPtrSet<Value*, 32> SeenCases;
    5558             :   SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
    5559       15123 :   while (Lex.getKind() != lltok::rsquare) {
    5560             :     Value *Constant;
    5561             :     BasicBlock *DestBB;
    5562             : 
    5563        6868 :     if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
    5564       13736 :         ParseToken(lltok::comma, "expected ',' after case value") ||
    5565             :         ParseTypeAndBasicBlock(DestBB, PFS))
    5566           0 :       return true;
    5567             : 
    5568        6868 :     if (!SeenCases.insert(Constant).second)
    5569           0 :       return Error(CondLoc, "duplicate case value in switch");
    5570       13736 :     if (!isa<ConstantInt>(Constant))
    5571           0 :       return Error(CondLoc, "case value is not a constant integer");
    5572             : 
    5573        6868 :     Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
    5574             :   }
    5575             : 
    5576        1387 :   Lex.Lex();  // Eat the ']'.
    5577             : 
    5578        1387 :   SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
    5579        8255 :   for (unsigned i = 0, e = Table.size(); i != e; ++i)
    5580       13736 :     SI->addCase(Table[i].first, Table[i].second);
    5581        1387 :   Inst = SI;
    5582        1387 :   return false;
    5583             : }
    5584             : 
    5585             : /// ParseIndirectBr
    5586             : ///  Instruction
    5587             : ///    ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
    5588         326 : bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
    5589             :   LocTy AddrLoc;
    5590             :   Value *Address;
    5591         326 :   if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
    5592         652 :       ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
    5593         326 :       ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
    5594             :     return true;
    5595             : 
    5596         652 :   if (!Address->getType()->isPointerTy())
    5597           0 :     return Error(AddrLoc, "indirectbr address must have pointer type");
    5598             : 
    5599             :   // Parse the destination list.
    5600             :   SmallVector<BasicBlock*, 16> DestList;
    5601             : 
    5602         326 :   if (Lex.getKind() != lltok::rsquare) {
    5603             :     BasicBlock *DestBB;
    5604         322 :     if (ParseTypeAndBasicBlock(DestBB, PFS))
    5605           0 :       return true;
    5606         322 :     DestList.push_back(DestBB);
    5607             : 
    5608         610 :     while (EatIfPresent(lltok::comma)) {
    5609         610 :       if (ParseTypeAndBasicBlock(DestBB, PFS))
    5610             :         return true;
    5611         610 :       DestList.push_back(DestBB);
    5612             :     }
    5613             :   }
    5614             : 
    5615         326 :   if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
    5616             :     return true;
    5617             : 
    5618         326 :   IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
    5619        1258 :   for (unsigned i = 0, e = DestList.size(); i != e; ++i)
    5620        1864 :     IBI->addDestination(DestList[i]);
    5621         326 :   Inst = IBI;
    5622         326 :   return false;
    5623             : }
    5624             : 
    5625             : /// ParseInvoke
    5626             : ///   ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
    5627             : ///       OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
    5628        1903 : bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
    5629        1903 :   LocTy CallLoc = Lex.getLoc();
    5630             :   AttrBuilder RetAttrs, FnAttrs;
    5631             :   std::vector<unsigned> FwdRefAttrGrps;
    5632        1903 :   LocTy NoBuiltinLoc;
    5633             :   unsigned CC;
    5634        1903 :   Type *RetType = nullptr;
    5635             :   LocTy RetTypeLoc;
    5636        3806 :   ValID CalleeID;
    5637             :   SmallVector<ParamInfo, 16> ArgList;
    5638        1903 :   SmallVector<OperandBundleDef, 2> BundleList;
    5639             : 
    5640             :   BasicBlock *NormalBB, *UnwindBB;
    5641        5709 :   if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
    5642        1903 :       ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
    5643        5709 :       ParseValID(CalleeID) || ParseParameterList(ArgList, PFS) ||
    5644        1903 :       ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
    5645        1903 :                                  NoBuiltinLoc) ||
    5646        3806 :       ParseOptionalOperandBundles(BundleList, PFS) ||
    5647        3806 :       ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
    5648        1903 :       ParseTypeAndBasicBlock(NormalBB, PFS) ||
    5649        5709 :       ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
    5650             :       ParseTypeAndBasicBlock(UnwindBB, PFS))
    5651             :     return true;
    5652             : 
    5653             :   // If RetType is a non-function pointer type, then this is the short syntax
    5654             :   // for the call, which means that RetType is just the return type.  Infer the
    5655             :   // rest of the function argument types from the arguments that are present.
    5656        1903 :   FunctionType *Ty = dyn_cast<FunctionType>(RetType);
    5657             :   if (!Ty) {
    5658             :     // Pull out the types of all of the arguments...
    5659             :     std::vector<Type*> ParamTypes;
    5660        3707 :     for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
    5661        5634 :       ParamTypes.push_back(ArgList[i].V->getType());
    5662             : 
    5663        1829 :     if (!FunctionType::isValidReturnType(RetType))
    5664           0 :       return Error(RetTypeLoc, "Invalid result type for LLVM function");
    5665             : 
    5666        1829 :     Ty = FunctionType::get(RetType, ParamTypes, false);
    5667             :   }
    5668             : 
    5669        1903 :   CalleeID.FTy = Ty;
    5670             : 
    5671             :   // Look up the callee.
    5672             :   Value *Callee;
    5673        1903 :   if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS,
    5674             :                           /*IsCall=*/true))
    5675             :     return true;
    5676             : 
    5677             :   // Set up the Attribute for the function.
    5678             :   SmallVector<Value *, 8> Args;
    5679             :   SmallVector<AttributeSet, 8> ArgAttrs;
    5680             : 
    5681             :   // Loop through FunctionType's arguments and ensure they are specified
    5682             :   // correctly.  Also, gather any parameter attributes.
    5683        1902 :   FunctionType::param_iterator I = Ty->param_begin();
    5684        1902 :   FunctionType::param_iterator E = Ty->param_end();
    5685        4083 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    5686             :     Type *ExpectedTy = nullptr;
    5687        2181 :     if (I != E) {
    5688        1995 :       ExpectedTy = *I++;
    5689         186 :     } else if (!Ty->isVarArg()) {
    5690           0 :       return Error(ArgList[i].Loc, "too many arguments specified");
    5691             :     }
    5692             : 
    5693        5985 :     if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
    5694           0 :       return Error(ArgList[i].Loc, "argument is not of expected type '" +
    5695           0 :                    getTypeString(ExpectedTy) + "'");
    5696        4362 :     Args.push_back(ArgList[i].V);
    5697        2181 :     ArgAttrs.push_back(ArgList[i].Attrs);
    5698             :   }
    5699             : 
    5700        1902 :   if (I != E)
    5701           0 :     return Error(CallLoc, "not enough parameters specified for call");
    5702             : 
    5703        1902 :   if (FnAttrs.hasAlignmentAttr())
    5704           0 :     return Error(CallLoc, "invoke instructions may not have an alignment");
    5705             : 
    5706             :   // Finish off the Attribute and check them
    5707             :   AttributeList PAL =
    5708             :       AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
    5709        1902 :                          AttributeSet::get(Context, RetAttrs), ArgAttrs);
    5710             : 
    5711             :   InvokeInst *II =
    5712        1902 :       InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
    5713        1902 :   II->setCallingConv(CC);
    5714             :   II->setAttributes(PAL);
    5715        1902 :   ForwardRefAttrGroups[II] = FwdRefAttrGrps;
    5716        1902 :   Inst = II;
    5717        1902 :   return false;
    5718             : }
    5719             : 
    5720             : /// ParseResume
    5721             : ///   ::= 'resume' TypeAndValue
    5722         248 : bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
    5723             :   Value *Exn; LocTy ExnLoc;
    5724         248 :   if (ParseTypeAndValue(Exn, ExnLoc, PFS))
    5725             :     return true;
    5726             : 
    5727         248 :   ResumeInst *RI = ResumeInst::Create(Exn);
    5728         248 :   Inst = RI;
    5729         248 :   return false;
    5730             : }
    5731             : 
    5732         548 : bool LLParser::ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
    5733             :                                   PerFunctionState &PFS) {
    5734         548 :   if (ParseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
    5735             :     return true;
    5736             : 
    5737        1534 :   while (Lex.getKind() != lltok::rsquare) {
    5738             :     // If this isn't the first argument, we need a comma.
    5739         755 :     if (!Args.empty() &&
    5740         262 :         ParseToken(lltok::comma, "expected ',' in argument list"))
    5741           0 :       return true;
    5742             : 
    5743             :     // Parse the argument.
    5744             :     LocTy ArgLoc;
    5745         493 :     Type *ArgTy = nullptr;
    5746         493 :     if (ParseType(ArgTy, ArgLoc))
    5747             :       return true;
    5748             : 
    5749             :     Value *V;
    5750         986 :     if (ArgTy->isMetadataTy()) {
    5751           0 :       if (ParseMetadataAsValue(V, PFS))
    5752             :         return true;
    5753             :     } else {
    5754         493 :       if (ParseValue(ArgTy, V, PFS))
    5755             :         return true;
    5756             :     }
    5757         493 :     Args.push_back(V);
    5758             :   }
    5759             : 
    5760         548 :   Lex.Lex();  // Lex the ']'.
    5761         548 :   return false;
    5762             : }
    5763             : 
    5764             : /// ParseCleanupRet
    5765             : ///   ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
    5766         181 : bool LLParser::ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
    5767         181 :   Value *CleanupPad = nullptr;
    5768             : 
    5769         181 :   if (ParseToken(lltok::kw_from, "expected 'from' after cleanupret"))
    5770             :     return true;
    5771             : 
    5772         362 :   if (ParseValue(Type::getTokenTy(Context), CleanupPad, PFS))
    5773             :     return true;
    5774             : 
    5775         181 :   if (ParseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
    5776             :     return true;
    5777             : 
    5778         181 :   BasicBlock *UnwindBB = nullptr;
    5779         181 :   if (Lex.getKind() == lltok::kw_to) {
    5780         122 :     Lex.Lex();
    5781         122 :     if (ParseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
    5782             :       return true;
    5783             :   } else {
    5784          59 :     if (ParseTypeAndBasicBlock(UnwindBB, PFS)) {
    5785             :       return true;
    5786             :     }
    5787             :   }
    5788             : 
    5789         181 :   Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
    5790         181 :   return false;
    5791             : }
    5792             : 
    5793             : /// ParseCatchRet
    5794             : ///   ::= 'catchret' from Parent Value 'to' TypeAndValue
    5795         195 : bool LLParser::ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
    5796         195 :   Value *CatchPad = nullptr;
    5797             : 
    5798         195 :   if (ParseToken(lltok::kw_from, "expected 'from' after catchret"))
    5799             :     return true;
    5800             : 
    5801         390 :   if (ParseValue(Type::getTokenTy(Context), CatchPad, PFS))
    5802             :     return true;
    5803             : 
    5804             :   BasicBlock *BB;
    5805         390 :   if (ParseToken(lltok::kw_to, "expected 'to' in catchret") ||
    5806             :       ParseTypeAndBasicBlock(BB, PFS))
    5807             :       return true;
    5808             : 
    5809         390 :   Inst = CatchReturnInst::Create(CatchPad, BB);
    5810         195 :   return false;
    5811             : }
    5812             : 
    5813             : /// ParseCatchSwitch
    5814             : ///   ::= 'catchswitch' within Parent
    5815         258 : bool LLParser::ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
    5816             :   Value *ParentPad;
    5817             : 
    5818         258 :   if (ParseToken(lltok::kw_within, "expected 'within' after catchswitch"))
    5819             :     return true;
    5820             : 
    5821         258 :   if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
    5822             :       Lex.getKind() != lltok::LocalVarID)
    5823           0 :     return TokError("expected scope value for catchswitch");
    5824             : 
    5825         516 :   if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
    5826             :     return true;
    5827             : 
    5828         258 :   if (ParseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
    5829             :     return true;
    5830             : 
    5831             :   SmallVector<BasicBlock *, 32> Table;
    5832             :   do {
    5833             :     BasicBlock *DestBB;
    5834         282 :     if (ParseTypeAndBasicBlock(DestBB, PFS))
    5835           0 :       return true;
    5836         282 :     Table.push_back(DestBB);
    5837             :   } while (EatIfPresent(lltok::comma));
    5838             : 
    5839         258 :   if (ParseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
    5840             :     return true;
    5841             : 
    5842         258 :   if (ParseToken(lltok::kw_unwind,
    5843             :                  "expected 'unwind' after catchswitch scope"))
    5844             :     return true;
    5845             : 
    5846         258 :   BasicBlock *UnwindBB = nullptr;
    5847             :   if (EatIfPresent(lltok::kw_to)) {
    5848         189 :     if (ParseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
    5849             :       return true;
    5850             :   } else {
    5851          69 :     if (ParseTypeAndBasicBlock(UnwindBB, PFS))
    5852             :       return true;
    5853             :   }
    5854             : 
    5855             :   auto *CatchSwitch =
    5856         516 :       CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
    5857         822 :   for (BasicBlock *DestBB : Table)
    5858         282 :     CatchSwitch->addHandler(DestBB);
    5859         258 :   Inst = CatchSwitch;
    5860         258 :   return false;
    5861             : }
    5862             : 
    5863             : /// ParseCatchPad
    5864             : ///   ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
    5865         282 : bool LLParser::ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
    5866         282 :   Value *CatchSwitch = nullptr;
    5867             : 
    5868         282 :   if (ParseToken(lltok::kw_within, "expected 'within' after catchpad"))
    5869             :     return true;
    5870             : 
    5871         282 :   if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
    5872           0 :     return TokError("expected scope value for catchpad");
    5873             : 
    5874         564 :   if (ParseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
    5875             :     return true;
    5876             : 
    5877             :   SmallVector<Value *, 8> Args;
    5878         282 :   if (ParseExceptionArgs(Args, PFS))
    5879             :     return true;
    5880             : 
    5881         282 :   Inst = CatchPadInst::Create(CatchSwitch, Args);
    5882         282 :   return false;
    5883             : }
    5884             : 
    5885             : /// ParseCleanupPad
    5886             : ///   ::= 'cleanuppad' within Parent ParamList
    5887         266 : bool LLParser::ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
    5888         266 :   Value *ParentPad = nullptr;
    5889             : 
    5890         266 :   if (ParseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
    5891             :     return true;
    5892             : 
    5893         266 :   if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
    5894             :       Lex.getKind() != lltok::LocalVarID)
    5895           0 :     return TokError("expected scope value for cleanuppad");
    5896             : 
    5897         532 :   if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
    5898             :     return true;
    5899             : 
    5900             :   SmallVector<Value *, 8> Args;
    5901         266 :   if (ParseExceptionArgs(Args, PFS))
    5902             :     return true;
    5903             : 
    5904         266 :   Inst = CleanupPadInst::Create(ParentPad, Args);
    5905         266 :   return false;
    5906             : }
    5907             : 
    5908             : //===----------------------------------------------------------------------===//
    5909             : // Binary Operators.
    5910             : //===----------------------------------------------------------------------===//
    5911             : 
    5912             : /// ParseArithmetic
    5913             : ///  ::= ArithmeticOps TypeAndValue ',' Value
    5914             : ///
    5915             : /// If OperandType is 0, then any FP or integer operand is allowed.  If it is 1,
    5916             : /// then any integer operand is allowed, if it is 2, any fp operand is allowed.
    5917      131506 : bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
    5918             :                                unsigned Opc, unsigned OperandType) {
    5919             :   LocTy Loc; Value *LHS, *RHS;
    5920      131506 :   if (ParseTypeAndValue(LHS, Loc, PFS) ||
    5921      263012 :       ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
    5922      131506 :       ParseValue(LHS->getType(), RHS, PFS))
    5923             :     return true;
    5924             : 
    5925             :   bool Valid;
    5926      131505 :   switch (OperandType) {
    5927           0 :   default: llvm_unreachable("Unknown operand type!");
    5928           0 :   case 0: // int or FP.
    5929           0 :     Valid = LHS->getType()->isIntOrIntVectorTy() ||
    5930             :             LHS->getType()->isFPOrFPVectorTy();
    5931             :     break;
    5932      191570 :   case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
    5933       35720 :   case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
    5934             :   }
    5935             : 
    5936      131505 :   if (!Valid)
    5937           0 :     return Error(Loc, "invalid operand type for instruction");
    5938             : 
    5939      131505 :   Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
    5940      131505 :   return false;
    5941             : }
    5942             : 
    5943             : /// ParseLogical
    5944             : ///  ::= ArithmeticOps TypeAndValue ',' Value {
    5945       33300 : bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
    5946             :                             unsigned Opc) {
    5947             :   LocTy Loc; Value *LHS, *RHS;
    5948       33300 :   if (ParseTypeAndValue(LHS, Loc, PFS) ||
    5949       66600 :       ParseToken(lltok::comma, "expected ',' in logical operation") ||
    5950       33300 :       ParseValue(LHS->getType(), RHS, PFS))
    5951             :     return true;
    5952             : 
    5953       66600 :   if (!LHS->getType()->isIntOrIntVectorTy())
    5954           0 :     return Error(Loc,"instruction requires integer or integer vector operands");
    5955             : 
    5956       33300 :   Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
    5957       33300 :   return false;
    5958             : }
    5959             : 
    5960             : /// ParseCompare
    5961             : ///  ::= 'icmp' IPredicates TypeAndValue ',' Value
    5962             : ///  ::= 'fcmp' FPredicates TypeAndValue ',' Value
    5963       63293 : bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
    5964             :                             unsigned Opc) {
    5965             :   // Parse the integer/fp comparison predicate.
    5966             :   LocTy Loc;
    5967             :   unsigned Pred;
    5968             :   Value *LHS, *RHS;
    5969      126586 :   if (ParseCmpPredicate(Pred, Opc) ||
    5970       63293 :       ParseTypeAndValue(LHS, Loc, PFS) ||
    5971      189879 :       ParseToken(lltok::comma, "expected ',' after compare value") ||
    5972       63293 :       ParseValue(LHS->getType(), RHS, PFS))
    5973             :     return true;
    5974             : 
    5975       63293 :   if (Opc == Instruction::FCmp) {
    5976        9817 :     if (!LHS->getType()->isFPOrFPVectorTy())
    5977           0 :       return Error(Loc, "fcmp requires floating point operands");
    5978        9817 :     Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
    5979             :   } else {
    5980             :     assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
    5981      106952 :     if (!LHS->getType()->isIntOrIntVectorTy() &&
    5982             :         !LHS->getType()->isPtrOrPtrVectorTy())
    5983           0 :       return Error(Loc, "icmp requires integer operands");
    5984       53476 :     Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
    5985             :   }
    5986             :   return false;
    5987             : }
    5988             : 
    5989             : //===----------------------------------------------------------------------===//
    5990             : // Other Instructions.
    5991             : //===----------------------------------------------------------------------===//
    5992             : 
    5993             : 
    5994             : /// ParseCast
    5995             : ///   ::= CastOpc TypeAndValue 'to' Type
    5996      116806 : bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
    5997             :                          unsigned Opc) {
    5998             :   LocTy Loc;
    5999             :   Value *Op;
    6000      116806 :   Type *DestTy = nullptr;
    6001      116806 :   if (ParseTypeAndValue(Op, Loc, PFS) ||
    6002      233612 :       ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
    6003             :       ParseType(DestTy))
    6004             :     return true;
    6005             : 
    6006      116805 :   if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
    6007           7 :     CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
    6008          21 :     return Error(Loc, "invalid cast opcode for cast from '" +
    6009          35 :                  getTypeString(Op->getType()) + "' to '" +
    6010          28 :                  getTypeString(DestTy) + "'");
    6011             :   }
    6012      116798 :   Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
    6013      116798 :   return false;
    6014             : }
    6015             : 
    6016             : /// ParseSelect
    6017             : ///   ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    6018       23620 : bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
    6019             :   LocTy Loc;
    6020             :   Value *Op0, *Op1, *Op2;
    6021       23620 :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6022       47240 :       ParseToken(lltok::comma, "expected ',' after select condition") ||
    6023       23620 :       ParseTypeAndValue(Op1, PFS) ||
    6024       70860 :       ParseToken(lltok::comma, "expected ',' after select value") ||
    6025             :       ParseTypeAndValue(Op2, PFS))
    6026             :     return true;
    6027             : 
    6028       23620 :   if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
    6029           1 :     return Error(Loc, Reason);
    6030             : 
    6031       47238 :   Inst = SelectInst::Create(Op0, Op1, Op2);
    6032       23619 :   return false;
    6033             : }
    6034             : 
    6035             : /// ParseVA_Arg
    6036             : ///   ::= 'va_arg' TypeAndValue ',' Type
    6037         210 : bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
    6038             :   Value *Op;
    6039         210 :   Type *EltTy = nullptr;
    6040             :   LocTy TypeLoc;
    6041         210 :   if (ParseTypeAndValue(Op, PFS) ||
    6042         420 :       ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
    6043             :       ParseType(EltTy, TypeLoc))
    6044             :     return true;
    6045             : 
    6046         210 :   if (!EltTy->isFirstClassType())
    6047           0 :     return Error(TypeLoc, "va_arg requires operand with first class type");
    6048             : 
    6049         420 :   Inst = new VAArgInst(Op, EltTy);
    6050         210 :   return false;
    6051             : }
    6052             : 
    6053             : /// ParseExtractElement
    6054             : ///   ::= 'extractelement' TypeAndValue ',' TypeAndValue
    6055       22817 : bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
    6056             :   LocTy Loc;
    6057             :   Value *Op0, *Op1;
    6058       22817 :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6059       45634 :       ParseToken(lltok::comma, "expected ',' after extract value") ||
    6060             :       ParseTypeAndValue(Op1, PFS))
    6061             :     return true;
    6062             : 
    6063       22817 :   if (!ExtractElementInst::isValidOperands(Op0, Op1))
    6064           0 :     return Error(Loc, "invalid extractelement operands");
    6065             : 
    6066       45634 :   Inst = ExtractElementInst::Create(Op0, Op1);
    6067       22817 :   return false;
    6068             : }
    6069             : 
    6070             : /// ParseInsertElement
    6071             : ///   ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    6072       31205 : bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
    6073             :   LocTy Loc;
    6074             :   Value *Op0, *Op1, *Op2;
    6075       31205 :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6076       62410 :       ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6077       31205 :       ParseTypeAndValue(Op1, PFS) ||
    6078       93615 :       ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6079             :       ParseTypeAndValue(Op2, PFS))
    6080             :     return true;
    6081             : 
    6082       31205 :   if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
    6083           0 :     return Error(Loc, "invalid insertelement operands");
    6084             : 
    6085       62410 :   Inst = InsertElementInst::Create(Op0, Op1, Op2);
    6086       31205 :   return false;
    6087             : }
    6088             : 
    6089             : /// ParseShuffleVector
    6090             : ///   ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    6091       38799 : bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
    6092             :   LocTy Loc;
    6093             :   Value *Op0, *Op1, *Op2;
    6094       38799 :   if (ParseTypeAndValue(Op0, Loc, PFS) ||
    6095       77598 :       ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
    6096       38799 :       ParseTypeAndValue(Op1, PFS) ||
    6097      116397 :       ParseToken(lltok::comma, "expected ',' after shuffle value") ||
    6098             :       ParseTypeAndValue(Op2, PFS))
    6099             :     return true;
    6100             : 
    6101       38799 :   if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
    6102           0 :     return Error(Loc, "invalid shufflevector operands");
    6103             : 
    6104       38799 :   Inst = new ShuffleVectorInst(Op0, Op1, Op2);
    6105       38799 :   return false;
    6106             : }
    6107             : 
    6108             : /// ParsePHI
    6109             : ///   ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
    6110       23540 : int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
    6111       23540 :   Type *Ty = nullptr;  LocTy TypeLoc;
    6112             :   Value *Op0, *Op1;
    6113             : 
    6114       23540 :   if (ParseType(Ty, TypeLoc) ||
    6115       47080 :       ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
    6116       47080 :       ParseValue(Ty, Op0, PFS) ||
    6117       47080 :       ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6118       70620 :       ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
    6119       23540 :       ParseToken(lltok::rsquare, "expected ']' in phi value list"))
    6120             :     return true;
    6121             : 
    6122             :   bool AteExtraComma = false;
    6123             :   SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
    6124             : 
    6125             :   while (true) {
    6126       49059 :     PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
    6127             : 
    6128             :     if (!EatIfPresent(lltok::comma))
    6129             :       break;
    6130             : 
    6131       25580 :     if (Lex.getKind() == lltok::MetadataVar) {
    6132             :       AteExtraComma = true;
    6133             :       break;
    6134             :     }
    6135             : 
    6136       51038 :     if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
    6137       51038 :         ParseValue(Ty, Op0, PFS) ||
    6138       51038 :         ParseToken(lltok::comma, "expected ',' after insertelement value") ||
    6139       76557 :         ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
    6140       25519 :         ParseToken(lltok::rsquare, "expected ']' in phi value list"))
    6141             :       return true;
    6142             :   }
    6143             : 
    6144       23540 :   if (!Ty->isFirstClassType())
    6145           0 :     return Error(TypeLoc, "phi node must have first class type");
    6146             : 
    6147       47080 :   PHINode *PN = PHINode::Create(Ty, PHIVals.size());
    6148      145198 :   for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
    6149       98118 :     PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
    6150       23540 :   Inst = PN;
    6151       23540 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6152             : }
    6153             : 
    6154             : /// ParseLandingPad
    6155             : ///   ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
    6156             : /// Clause
    6157             : ///   ::= 'catch' TypeAndValue
    6158             : ///   ::= 'filter'
    6159             : ///   ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
    6160        1109 : bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
    6161        1109 :   Type *Ty = nullptr; LocTy TyLoc;
    6162             : 
    6163        1109 :   if (ParseType(Ty, TyLoc))
    6164             :     return true;
    6165             : 
    6166        3327 :   std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
    6167             :   LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
    6168             : 
    6169        2147 :   while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
    6170             :     LandingPadInst::ClauseType CT;
    6171             :     if (EatIfPresent(lltok::kw_catch))
    6172             :       CT = LandingPadInst::Catch;
    6173             :     else if (EatIfPresent(lltok::kw_filter))
    6174             :       CT = LandingPadInst::Filter;
    6175             :     else
    6176           0 :       return TokError("expected 'catch' or 'filter' clause type");
    6177             : 
    6178             :     Value *V;
    6179             :     LocTy VLoc;
    6180         520 :     if (ParseTypeAndValue(V, VLoc, PFS))
    6181             :       return true;
    6182             : 
    6183             :     // A 'catch' type expects a non-array constant. A filter clause expects an
    6184             :     // array constant.
    6185         520 :     if (CT == LandingPadInst::Catch) {
    6186         940 :       if (isa<ArrayType>(V->getType()))
    6187           0 :         Error(VLoc, "'catch' clause has an invalid type");
    6188             :     } else {
    6189         100 :       if (!isa<ArrayType>(V->getType()))
    6190           1 :         Error(VLoc, "'filter' clause has an invalid type");
    6191             :     }
    6192             : 
    6193         520 :     Constant *CV = dyn_cast<Constant>(V);
    6194             :     if (!CV)
    6195           1 :       return Error(VLoc, "clause argument must be a constant");
    6196         519 :     LP->addClause(CV);
    6197             :   }
    6198             : 
    6199        1108 :   Inst = LP.release();
    6200        1108 :   return false;
    6201             : }
    6202             : 
    6203             : /// ParseCall
    6204             : ///   ::= 'call' OptionalFastMathFlags OptionalCallingConv
    6205             : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6206             : ///   ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
    6207             : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6208             : ///   ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
    6209             : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6210             : ///   ::= 'notail' 'call'  OptionalFastMathFlags OptionalCallingConv
    6211             : ///           OptionalAttrs Type Value ParameterList OptionalAttrs
    6212      131751 : bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
    6213             :                          CallInst::TailCallKind TCK) {
    6214             :   AttrBuilder RetAttrs, FnAttrs;
    6215             :   std::vector<unsigned> FwdRefAttrGrps;
    6216      131751 :   LocTy BuiltinLoc;
    6217             :   unsigned CC;
    6218      131751 :   Type *RetType = nullptr;
    6219             :   LocTy RetTypeLoc;
    6220      263502 :   ValID CalleeID;
    6221             :   SmallVector<ParamInfo, 16> ArgList;
    6222      131751 :   SmallVector<OperandBundleDef, 2> BundleList;
    6223      131751 :   LocTy CallLoc = Lex.getLoc();
    6224             : 
    6225      165357 :   if (TCK != CallInst::TCK_None &&
    6226       33606 :       ParseToken(lltok::kw_call,
    6227             :                  "expected 'tail call', 'musttail call', or 'notail call'"))
    6228             :     return true;
    6229             : 
    6230      131751 :   FastMathFlags FMF = EatFastMathFlagsIfPresent();
    6231             : 
    6232      395253 :   if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
    6233      131751 :       ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
    6234      263502 :       ParseValID(CalleeID) ||
    6235      131751 :       ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
    6236      263494 :                          PFS.getFunction().isVarArg()) ||
    6237      395237 :       ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
    6238      131743 :       ParseOptionalOperandBundles(BundleList, PFS))
    6239             :     return true;
    6240             : 
    6241      131743 :   if (FMF.any() && !RetType->isFPOrFPVectorTy())
    6242           0 :     return Error(CallLoc, "fast-math-flags specified for call without "
    6243             :                           "floating-point scalar or vector return type");
    6244             : 
    6245             :   // If RetType is a non-function pointer type, then this is the short syntax
    6246             :   // for the call, which means that RetType is just the return type.  Infer the
    6247             :   // rest of the function argument types from the arguments that are present.
    6248      131743 :   FunctionType *Ty = dyn_cast<FunctionType>(RetType);
    6249             :   if (!Ty) {
    6250             :     // Pull out the types of all of the arguments...
    6251             :     std::vector<Type*> ParamTypes;
    6252      369944 :     for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
    6253      727482 :       ParamTypes.push_back(ArgList[i].V->getType());
    6254             : 
    6255      127450 :     if (!FunctionType::isValidReturnType(RetType))
    6256           0 :       return Error(RetTypeLoc, "Invalid result type for LLVM function");
    6257             : 
    6258      127450 :     Ty = FunctionType::get(RetType, ParamTypes, false);
    6259             :   }
    6260             : 
    6261      131743 :   CalleeID.FTy = Ty;
    6262             : 
    6263             :   // Look up the callee.
    6264             :   Value *Callee;
    6265      131743 :   if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS,
    6266             :                           /*IsCall=*/true))
    6267             :     return true;
    6268             : 
    6269             :   // Set up the Attribute for the function.
    6270             :   SmallVector<AttributeSet, 8> Attrs;
    6271             : 
    6272             :   SmallVector<Value*, 8> Args;
    6273             : 
    6274             :   // Loop through FunctionType's arguments and ensure they are specified
    6275             :   // correctly.  Also, gather any parameter attributes.
    6276      131738 :   FunctionType::param_iterator I = Ty->param_begin();
    6277      131738 :   FunctionType::param_iterator E = Ty->param_end();
    6278      383939 :   for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
    6279             :     Type *ExpectedTy = nullptr;
    6280      252201 :     if (I != E) {
    6281      246843 :       ExpectedTy = *I++;
    6282        5358 :     } else if (!Ty->isVarArg()) {
    6283           0 :       return Error(ArgList[i].Loc, "too many arguments specified");
    6284             :     }
    6285             : 
    6286      740529 :     if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
    6287           0 :       return Error(ArgList[i].Loc, "argument is not of expected type '" +
    6288           0 :                    getTypeString(ExpectedTy) + "'");
    6289      504402 :     Args.push_back(ArgList[i].V);
    6290      252201 :     Attrs.push_back(ArgList[i].Attrs);
    6291             :   }
    6292             : 
    6293      131738 :   if (I != E)
    6294           0 :     return Error(CallLoc, "not enough parameters specified for call");
    6295             : 
    6296      131738 :   if (FnAttrs.hasAlignmentAttr())
    6297           1 :     return Error(CallLoc, "call instructions may not have an alignment");
    6298             : 
    6299             :   // Finish off the Attribute and check them
    6300             :   AttributeList PAL =
    6301             :       AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
    6302      131737 :                          AttributeSet::get(Context, RetAttrs), Attrs);
    6303             : 
    6304      131737 :   CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
    6305             :   CI->setTailCallKind(TCK);
    6306      131737 :   CI->setCallingConv(CC);
    6307      131737 :   if (FMF.any())
    6308        1076 :     CI->setFastMathFlags(FMF);
    6309             :   CI->setAttributes(PAL);
    6310      131737 :   ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
    6311      131737 :   Inst = CI;
    6312      131737 :   return false;
    6313             : }
    6314             : 
    6315             : //===----------------------------------------------------------------------===//
    6316             : // Memory Instructions.
    6317             : //===----------------------------------------------------------------------===//
    6318             : 
    6319             : /// ParseAlloc
    6320             : ///   ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
    6321             : ///       (',' 'align' i32)? (',', 'addrspace(n))?
    6322       47885 : int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
    6323       47885 :   Value *Size = nullptr;
    6324       47885 :   LocTy SizeLoc, TyLoc, ASLoc;
    6325       47885 :   unsigned Alignment = 0;
    6326       47885 :   unsigned AddrSpace = 0;
    6327       47885 :   Type *Ty = nullptr;
    6328             : 
    6329             :   bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
    6330             :   bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
    6331             : 
    6332       47885 :   if (ParseType(Ty, TyLoc)) return true;
    6333             : 
    6334       95770 :   if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
    6335           4 :     return Error(TyLoc, "invalid type for alloca");
    6336             : 
    6337       47881 :   bool AteExtraComma = false;
    6338             :   if (EatIfPresent(lltok::comma)) {
    6339       44844 :     if (Lex.getKind() == lltok::kw_align) {
    6340       43359 :       if (ParseOptionalAlignment(Alignment))
    6341             :         return true;
    6342       43358 :       if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
    6343             :         return true;
    6344        1485 :     } else if (Lex.getKind() == lltok::kw_addrspace) {
    6345         421 :       ASLoc = Lex.getLoc();
    6346         421 :       if (ParseOptionalAddrSpace(AddrSpace))
    6347             :         return true;
    6348        1064 :     } else if (Lex.getKind() == lltok::MetadataVar) {
    6349           0 :       AteExtraComma = true;
    6350             :     } else {
    6351        1064 :       if (ParseTypeAndValue(Size, SizeLoc, PFS))
    6352             :         return true;
    6353             :       if (EatIfPresent(lltok::comma)) {
    6354         372 :         if (Lex.getKind() == lltok::kw_align) {
    6355         361 :           if (ParseOptionalAlignment(Alignment))
    6356             :             return true;
    6357         361 :           if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
    6358             :             return true;
    6359          11 :         } else if (Lex.getKind() == lltok::kw_addrspace) {
    6360           8 :           ASLoc = Lex.getLoc();
    6361           8 :           if (ParseOptionalAddrSpace(AddrSpace))
    6362             :             return true;
    6363           3 :         } else if (Lex.getKind() == lltok::MetadataVar) {
    6364           3 :           AteExtraComma = true;
    6365             :         }
    6366             :       }
    6367             :     }
    6368             :   }
    6369             : 
    6370       48944 :   if (Size && !Size->getType()->isIntegerTy())
    6371           0 :     return Error(SizeLoc, "element count must have integer type");
    6372             : 
    6373       47880 :   AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
    6374             :   AI->setUsedWithInAlloca(IsInAlloca);
    6375             :   AI->setSwiftError(IsSwiftError);
    6376       47880 :   Inst = AI;
    6377       47880 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6378             : }
    6379             : 
    6380             : /// ParseLoad
    6381             : ///   ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
    6382             : ///   ::= 'load' 'atomic' 'volatile'? TypeAndValue
    6383             : ///       'singlethread'? AtomicOrdering (',' 'align' i32)?
    6384      156590 : int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
    6385             :   Value *Val; LocTy Loc;
    6386      156590 :   unsigned Alignment = 0;
    6387      156590 :   bool AteExtraComma = false;
    6388             :   bool isAtomic = false;
    6389      156590 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6390      156590 :   SyncScope::ID SSID = SyncScope::System;
    6391             : 
    6392      156590 :   if (Lex.getKind() == lltok::kw_atomic) {
    6393             :     isAtomic = true;
    6394        1020 :     Lex.Lex();
    6395             :   }
    6396             : 
    6397             :   bool isVolatile = false;
    6398      156590 :   if (Lex.getKind() == lltok::kw_volatile) {
    6399             :     isVolatile = true;
    6400        9641 :     Lex.Lex();
    6401             :   }
    6402             : 
    6403             :   Type *Ty;
    6404      156590 :   LocTy ExplicitTypeLoc = Lex.getLoc();
    6405      156590 :   if (ParseType(Ty) ||
    6406      313179 :       ParseToken(lltok::comma, "expected comma after load's type") ||
    6407      156589 :       ParseTypeAndValue(Val, Loc, PFS) ||
    6408      469768 :       ParseScopeAndOrdering(isAtomic, SSID, Ordering) ||
    6409      156589 :       ParseOptionalCommaAlign(Alignment, AteExtraComma))
    6410             :     return true;
    6411             : 
    6412      313176 :   if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
    6413           0 :     return Error(Loc, "load operand must be a pointer to a first class type");
    6414      157608 :   if (isAtomic && !Alignment)
    6415           0 :     return Error(Loc, "atomic load must have explicit non-zero alignment");
    6416      156588 :   if (Ordering == AtomicOrdering::Release ||
    6417             :       Ordering == AtomicOrdering::AcquireRelease)
    6418           0 :     return Error(Loc, "atomic load cannot use Release ordering");
    6419             : 
    6420      156588 :   if (Ty != cast<PointerType>(Val->getType())->getElementType())
    6421           1 :     return Error(ExplicitTypeLoc,
    6422           1 :                  "explicit pointee type doesn't match operand's pointee type");
    6423             : 
    6424      156587 :   Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, SSID);
    6425      156587 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6426             : }
    6427             : 
    6428             : /// ParseStore
    6429             : 
    6430             : ///   ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
    6431             : ///   ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
    6432             : ///       'singlethread'? AtomicOrdering (',' 'align' i32)?
    6433      122311 : int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
    6434             :   Value *Val, *Ptr; LocTy Loc, PtrLoc;
    6435      122311 :   unsigned Alignment = 0;
    6436      122311 :   bool AteExtraComma = false;
    6437             :   bool isAtomic = false;
    6438      122311 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6439      122311 :   SyncScope::ID SSID = SyncScope::System;
    6440             : 
    6441      122311 :   if (Lex.getKind() == lltok::kw_atomic) {
    6442             :     isAtomic = true;
    6443        1013 :     Lex.Lex();
    6444             :   }
    6445             : 
    6446             :   bool isVolatile = false;
    6447      122311 :   if (Lex.getKind() == lltok::kw_volatile) {
    6448             :     isVolatile = true;
    6449       10095 :     Lex.Lex();
    6450             :   }
    6451             : 
    6452      122310 :   if (ParseTypeAndValue(Val, Loc, PFS) ||
    6453      244620 :       ParseToken(lltok::comma, "expected ',' after store operand") ||
    6454      122310 :       ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
    6455      366931 :       ParseScopeAndOrdering(isAtomic, SSID, Ordering) ||
    6456      122310 :       ParseOptionalCommaAlign(Alignment, AteExtraComma))
    6457             :     return true;
    6458             : 
    6459      244618 :   if (!Ptr->getType()->isPointerTy())
    6460           0 :     return Error(PtrLoc, "store operand must be a pointer");
    6461      122309 :   if (!Val->getType()->isFirstClassType())
    6462           0 :     return Error(Loc, "store operand must be a first class value");
    6463      122309 :   if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
    6464           0 :     return Error(Loc, "stored value and pointer type do not match");
    6465      122309 :   if (isAtomic && !Alignment)
    6466           0 :     return Error(Loc, "atomic store must have explicit non-zero alignment");
    6467      122309 :   if (Ordering == AtomicOrdering::Acquire ||
    6468             :       Ordering == AtomicOrdering::AcquireRelease)
    6469           0 :     return Error(Loc, "atomic store cannot use Acquire ordering");
    6470             : 
    6471      122309 :   Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, SSID);
    6472      122309 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6473             : }
    6474             : 
    6475             : /// ParseCmpXchg
    6476             : ///   ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
    6477             : ///       TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
    6478        1065 : int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
    6479             :   Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
    6480             :   bool AteExtraComma = false;
    6481        1065 :   AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
    6482        1065 :   AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
    6483        1065 :   SyncScope::ID SSID = SyncScope::System;
    6484             :   bool isVolatile = false;
    6485             :   bool isWeak = false;
    6486             : 
    6487             :   if (EatIfPresent(lltok::kw_weak))
    6488             :     isWeak = true;
    6489             : 
    6490             :   if (EatIfPresent(lltok::kw_volatile))
    6491             :     isVolatile = true;
    6492             : 
    6493        1065 :   if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
    6494        2130 :       ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
    6495        1065 :       ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
    6496        2130 :       ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
    6497        1065 :       ParseTypeAndValue(New, NewLoc, PFS) ||
    6498        3195 :       ParseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
    6499        1065 :       ParseOrdering(FailureOrdering))
    6500             :     return true;
    6501             : 
    6502        2130 :   if (SuccessOrdering == AtomicOrdering::Unordered ||
    6503        1065 :       FailureOrdering == AtomicOrdering::Unordered)
    6504           0 :     return TokError("cmpxchg cannot be unordered");
    6505        1065 :   if (isStrongerThan(FailureOrdering, SuccessOrdering))
    6506           0 :     return TokError("cmpxchg failure argument shall be no stronger than the "
    6507           0 :                     "success argument");
    6508        1065 :   if (FailureOrdering == AtomicOrdering::Release ||
    6509             :       FailureOrdering == AtomicOrdering::AcquireRelease)
    6510           0 :     return TokError(
    6511           0 :         "cmpxchg failure ordering cannot include release semantics");
    6512        2130 :   if (!Ptr->getType()->isPointerTy())
    6513           0 :     return Error(PtrLoc, "cmpxchg operand must be a pointer");
    6514        1065 :   if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
    6515           0 :     return Error(CmpLoc, "compare value and pointer type do not match");
    6516        1065 :   if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
    6517           0 :     return Error(NewLoc, "new value and pointer type do not match");
    6518             :   if (!New->getType()->isFirstClassType())
    6519           0 :     return Error(NewLoc, "cmpxchg operand must be a first class value");
    6520             :   AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
    6521        1065 :       Ptr, Cmp, New, SuccessOrdering, FailureOrdering, SSID);
    6522             :   CXI->setVolatile(isVolatile);
    6523             :   CXI->setWeak(isWeak);
    6524        1065 :   Inst = CXI;
    6525             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6526             : }
    6527             : 
    6528             : /// ParseAtomicRMW
    6529             : ///   ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
    6530             : ///       'singlethread'? AtomicOrdering
    6531        5873 : int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
    6532             :   Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
    6533             :   bool AteExtraComma = false;
    6534        5873 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6535        5873 :   SyncScope::ID SSID = SyncScope::System;
    6536             :   bool isVolatile = false;
    6537             :   AtomicRMWInst::BinOp Operation;
    6538             : 
    6539             :   if (EatIfPresent(lltok::kw_volatile))
    6540             :     isVolatile = true;
    6541             : 
    6542        5873 :   switch (Lex.getKind()) {
    6543           0 :   default: return TokError("expected binary operation in atomicrmw");
    6544             :   case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
    6545         879 :   case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
    6546         741 :   case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
    6547         619 :   case lltok::kw_and: Operation = AtomicRMWInst::And; break;
    6548         268 :   case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
    6549         588 :   case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
    6550         570 :   case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
    6551         405 :   case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
    6552         424 :   case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
    6553         416 :   case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
    6554         417 :   case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
    6555             :   }
    6556        5873 :   Lex.Lex();  // Eat the operation.
    6557             : 
    6558        5873 :   if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
    6559       11746 :       ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
    6560       11746 :       ParseTypeAndValue(Val, ValLoc, PFS) ||
    6561        5873 :       ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
    6562             :     return true;
    6563             : 
    6564        5873 :   if (Ordering == AtomicOrdering::Unordered)
    6565           0 :     return TokError("atomicrmw cannot be unordered");
    6566       11746 :   if (!Ptr->getType()->isPointerTy())
    6567           0 :     return Error(PtrLoc, "atomicrmw operand must be a pointer");
    6568        5873 :   if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
    6569           0 :     return Error(ValLoc, "atomicrmw value and pointer type do not match");
    6570        5873 :   if (!Val->getType()->isIntegerTy())
    6571           0 :     return Error(ValLoc, "atomicrmw operand must be an integer");
    6572        5873 :   unsigned Size = Val->getType()->getPrimitiveSizeInBits();
    6573        5873 :   if (Size < 8 || (Size & (Size - 1)))
    6574           0 :     return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
    6575           0 :                          " integer");
    6576             : 
    6577             :   AtomicRMWInst *RMWI =
    6578        5873 :     new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID);
    6579             :   RMWI->setVolatile(isVolatile);
    6580        5873 :   Inst = RMWI;
    6581             :   return AteExtraComma ? InstExtraComma : InstNormal;
    6582             : }
    6583             : 
    6584             : /// ParseFence
    6585             : ///   ::= 'fence' 'singlethread'? AtomicOrdering
    6586         671 : int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
    6587         671 :   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
    6588         671 :   SyncScope::ID SSID = SyncScope::System;
    6589         671 :   if (ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
    6590             :     return true;
    6591             : 
    6592         671 :   if (Ordering == AtomicOrdering::Unordered)
    6593           0 :     return TokError("fence cannot be unordered");
    6594         671 :   if (Ordering == AtomicOrdering::Monotonic)
    6595           0 :     return TokError("fence cannot be monotonic");
    6596             : 
    6597         671 :   Inst = new FenceInst(Context, Ordering, SSID);
    6598         671 :   return InstNormal;
    6599             : }
    6600             : 
    6601             : /// ParseGetElementPtr
    6602             : ///   ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
    6603       88544 : int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
    6604       88544 :   Value *Ptr = nullptr;
    6605       88544 :   Value *Val = nullptr;
    6606             :   LocTy Loc, EltLoc;
    6607             : 
    6608             :   bool InBounds = EatIfPresent(lltok::kw_inbounds);
    6609             : 
    6610       88544 :   Type *Ty = nullptr;
    6611       88544 :   LocTy ExplicitTypeLoc = Lex.getLoc();
    6612       88544 :   if (ParseType(Ty) ||
    6613      177087 :       ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
    6614             :       ParseTypeAndValue(Ptr, Loc, PFS))
    6615             :     return true;
    6616             : 
    6617       88542 :   Type *BaseType = Ptr->getType();
    6618             :   PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
    6619             :   if (!BasePointerType)
    6620           1 :     return Error(Loc, "base of getelementptr must be a pointer");
    6621             : 
    6622       88541 :   if (Ty != BasePointerType->getElementType())
    6623           1 :     return Error(ExplicitTypeLoc,
    6624           1 :                  "explicit pointee type doesn't match operand's pointee type");
    6625             : 
    6626             :   SmallVector<Value*, 16> Indices;
    6627             :   bool AteExtraComma = false;
    6628             :   // GEP returns a vector of pointers if at least one of parameters is a vector.
    6629             :   // All vector parameters should have the same vector width.
    6630       88540 :   unsigned GEPWidth = BaseType->isVectorTy() ?
    6631             :     BaseType->getVectorNumElements() : 0;
    6632             : 
    6633      114353 :   while (EatIfPresent(lltok::comma)) {
    6634      115353 :     if (Lex.getKind() == lltok::MetadataVar) {
    6635             :       AteExtraComma = true;
    6636             :       break;
    6637             :     }
    6638      114354 :     if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
    6639      228708 :     if (!Val->getType()->isIntOrIntVectorTy())
    6640           0 :       return Error(EltLoc, "getelementptr index must be an integer");
    6641             : 
    6642      114354 :     if (Val->getType()->isVectorTy()) {
    6643             :       unsigned ValNumEl = Val->getType()->getVectorNumElements();
    6644         455 :       if (GEPWidth && GEPWidth != ValNumEl)
    6645           1 :         return Error(EltLoc,
    6646           1 :           "getelementptr vector index has a wrong number of elements");
    6647             :       GEPWidth = ValNumEl;
    6648             :     }
    6649      114353 :     Indices.push_back(Val);
    6650             :   }
    6651             : 
    6652             :   SmallPtrSet<Type*, 4> Visited;
    6653       88539 :   if (!Indices.empty() && !Ty->isSized(&Visited))
    6654           1 :     return Error(Loc, "base element of getelementptr must be sized");
    6655             : 
    6656       88538 :   if (!GetElementPtrInst::getIndexedType(Ty, Indices))
    6657           3 :     return Error(Loc, "invalid getelementptr indices");
    6658       88535 :   Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
    6659       88535 :   if (InBounds)
    6660       65624 :     cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
    6661       88535 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6662             : }
    6663             : 
    6664             : /// ParseExtractValue
    6665             : ///   ::= 'extractvalue' TypeAndValue (',' uint32)+
    6666        6164 : int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
    6667             :   Value *Val; LocTy Loc;
    6668             :   SmallVector<unsigned, 4> Indices;
    6669             :   bool AteExtraComma;
    6670       12328 :   if (ParseTypeAndValue(Val, Loc, PFS) ||
    6671        6164 :       ParseIndexList(Indices, AteExtraComma))
    6672             :     return true;
    6673             : 
    6674        6163 :   if (!Val->getType()->isAggregateType())
    6675           0 :     return Error(Loc, "extractvalue operand must be aggregate type");
    6676             : 
    6677        6163 :   if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
    6678           1 :     return Error(Loc, "invalid indices for extractvalue");
    6679        6162 :   Inst = ExtractValueInst::Create(Val, Indices);
    6680        6162 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6681             : }
    6682             : 
    6683             : /// ParseInsertValue
    6684             : ///   ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
    6685        1361 : int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
    6686             :   Value *Val0, *Val1; LocTy Loc0, Loc1;
    6687             :   SmallVector<unsigned, 4> Indices;
    6688             :   bool AteExtraComma;
    6689        1361 :   if (ParseTypeAndValue(Val0, Loc0, PFS) ||
    6690        2722 :       ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
    6691        2722 :       ParseTypeAndValue(Val1, Loc1, PFS) ||
    6692        1361 :       ParseIndexList(Indices, AteExtraComma))
    6693             :     return true;
    6694             : 
    6695        1361 :   if (!Val0->getType()->isAggregateType())
    6696           0 :     return Error(Loc0, "insertvalue operand must be aggregate type");
    6697             : 
    6698        1361 :   Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
    6699        1361 :   if (!IndexedType)
    6700           1 :     return Error(Loc0, "invalid indices for insertvalue");
    6701        1360 :   if (IndexedType != Val1->getType())
    6702           3 :     return Error(Loc1, "insertvalue operand and field disagree in type: '" +
    6703           5 :                            getTypeString(Val1->getType()) + "' instead of '" +
    6704           5 :                            getTypeString(IndexedType) + "'");
    6705        1359 :   Inst = InsertValueInst::Create(Val0, Val1, Indices);
    6706        1359 :   return AteExtraComma ? InstExtraComma : InstNormal;
    6707             : }
    6708             : 
    6709             : //===----------------------------------------------------------------------===//
    6710             : // Embedded metadata.
    6711             : //===----------------------------------------------------------------------===//
    6712             : 
    6713             : /// ParseMDNodeVector
    6714             : ///   ::= { Element (',' Element)* }
    6715             : /// Element
    6716             : ///   ::= 'null' | TypeAndValue
    6717       19595 : bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
    6718       19595 :   if (ParseToken(lltok::lbrace, "expected '{' here"))
    6719             :     return true;
    6720             : 
    6721             :   // Check for an empty list.
    6722             :   if (EatIfPresent(lltok::rbrace))
    6723             :     return false;
    6724             : 
    6725             :   do {
    6726             :     // Null is a special case since it is typeless.
    6727         918 :     if (EatIfPresent(lltok::kw_null)) {
    6728         918 :       Elts.push_back(nullptr);
    6729         918 :       continue;
    6730             :     }
    6731             : 
    6732             :     Metadata *MD;
    6733       51541 :     if (ParseMetadata(MD, nullptr))
    6734           7 :       return true;
    6735       51534 :     Elts.push_back(MD);
    6736             :   } while (EatIfPresent(lltok::comma));
    6737             : 
    6738       17702 :   return ParseToken(lltok::rbrace, "expected end of metadata node");
    6739             : }
    6740             : 
    6741             : //===----------------------------------------------------------------------===//
    6742             : // Use-list order directives.
    6743             : //===----------------------------------------------------------------------===//
    6744         869 : bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
    6745             :                                 SMLoc Loc) {
    6746         869 :   if (V->use_empty())
    6747           4 :     return Error(Loc, "value has no uses");
    6748             : 
    6749             :   unsigned NumUses = 0;
    6750             :   SmallDenseMap<const Use *, unsigned, 16> Order;
    6751        4060 :   for (const Use &U : V->uses()) {
    6752        3196 :     if (++NumUses > Indexes.size())
    6753             :       break;
    6754        9585 :     Order[&U] = Indexes[NumUses - 1];
    6755             :   }
    6756         865 :   if (NumUses < 2)
    6757           1 :     return Error(Loc, "value only has one use");
    6758         864 :   if (Order.size() != Indexes.size() || NumUses > Indexes.size())
    6759             :     return Error(Loc,
    6760           6 :                  "wrong number of indexes, expected " + Twine(V->getNumUses()));
    6761             : 
    6762        5185 :   V->sortUseList([&](const Use &L, const Use &R) {
    6763        8648 :     return Order.lookup(&L) < Order.lookup(&R);
    6764        4324 :   });
    6765         861 :   return false;
    6766             : }
    6767             : 
    6768             : /// ParseUseListOrderIndexes
    6769             : ///   ::= '{' uint32 (',' uint32)+ '}'
    6770         878 : bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
    6771         878 :   SMLoc Loc = Lex.getLoc();
    6772         878 :   if (ParseToken(lltok::lbrace, "expected '{' here"))
    6773             :     return true;
    6774         878 :   if (Lex.getKind() == lltok::rbrace)
    6775           0 :     return Lex.Error("expected non-empty list of uselistorder indexes");
    6776             : 
    6777             :   // Use Offset, Max, and IsOrdered to check consistency of indexes.  The
    6778             :   // indexes should be distinct numbers in the range [0, size-1], and should
    6779             :   // not be in order.
    6780             :   unsigned Offset = 0;
    6781         878 :   unsigned Max = 0;
    6782             :   bool IsOrdered = true;
    6783             :   assert(Indexes.empty() && "Expected empty order vector");
    6784             :   do {
    6785             :     unsigned Index;
    6786        3227 :     if (ParseUInt32(Index))
    6787           0 :       return true;
    6788             : 
    6789             :     // Update consistency checks.
    6790        6454 :     Offset += Index - Indexes.size();
    6791        3227 :     Max = std::max(Max, Index);
    6792        3227 :     IsOrdered &= Index == Indexes.size();
    6793             : 
    6794        3227 :     Indexes.push_back(Index);
    6795             :   } while (EatIfPresent(lltok::comma));
    6796             : 
    6797         878 :   if (ParseToken(lltok::rbrace, "expected '}' here"))
    6798             :     return true;
    6799             : 
    6800         878 :   if (Indexes.size() < 2)
    6801           0 :     return Error(Loc, "expected >= 2 uselistorder indexes");
    6802         878 :   if (Offset != 0 || Max >= Indexes.size())
    6803           2 :     return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
    6804         876 :   if (IsOrdered)
    6805           1 :     return Error(Loc, "expected uselistorder indexes to change the order");
    6806             : 
    6807             :   return false;
    6808             : }
    6809             : 
    6810             : /// ParseUseListOrder
    6811             : ///   ::= 'uselistorder' Type Value ',' UseListOrderIndexes
    6812         868 : bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
    6813         868 :   SMLoc Loc = Lex.getLoc();
    6814         868 :   if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
    6815             :     return true;
    6816             : 
    6817             :   Value *V;
    6818             :   SmallVector<unsigned, 16> Indexes;
    6819        1733 :   if (ParseTypeAndValue(V, PFS) ||
    6820        1733 :       ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
    6821         866 :       ParseUseListOrderIndexes(Indexes))
    6822             :     return true;
    6823             : 
    6824         863 :   return sortUseListOrder(V, Indexes, Loc);
    6825             : }
    6826             : 
    6827             : /// ParseUseListOrderBB
    6828             : ///   ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
    6829          12 : bool LLParser::ParseUseListOrderBB() {
    6830             :   assert(Lex.getKind() == lltok::kw_uselistorder_bb);
    6831          12 :   SMLoc Loc = Lex.getLoc();
    6832          12 :   Lex.Lex();
    6833             : 
    6834          24 :   ValID Fn, Label;
    6835             :   SmallVector<unsigned, 16> Indexes;
    6836          24 :   if (ParseValID(Fn) ||
    6837          24 :       ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
    6838          24 :       ParseValID(Label) ||
    6839          36 :       ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
    6840          12 :       ParseUseListOrderIndexes(Indexes))
    6841             :     return true;
    6842             : 
    6843             :   // Check the function.
    6844             :   GlobalValue *GV;
    6845          12 :   if (Fn.Kind == ValID::t_GlobalName)
    6846          24 :     GV = M->getNamedValue(Fn.StrVal);
    6847           0 :   else if (Fn.Kind == ValID::t_GlobalID)
    6848           0 :     GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
    6849             :   else
    6850           0 :     return Error(Fn.Loc, "expected function name in uselistorder_bb");
    6851          12 :   if (!GV)
    6852           1 :     return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
    6853             :   auto *F = dyn_cast<Function>(GV);
    6854             :   if (!F)
    6855           1 :     return Error(Fn.Loc, "expected function name in uselistorder_bb");
    6856          10 :   if (F->isDeclaration())
    6857           1 :     return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
    6858             : 
    6859             :   // Check the basic block.
    6860           9 :   if (Label.Kind == ValID::t_LocalID)
    6861           1 :     return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
    6862           8 :   if (Label.Kind != ValID::t_LocalName)
    6863           0 :     return Error(Label.Loc, "expected basic block name in uselistorder_bb");
    6864             :   Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
    6865           8 :   if (!V)
    6866           1 :     return Error(Label.Loc, "invalid basic block in uselistorder_bb");
    6867           7 :   if (!isa<BasicBlock>(V))
    6868           1 :     return Error(Label.Loc, "expected basic block in uselistorder_bb");
    6869             : 
    6870           6 :   return sortUseListOrder(V, Indexes, Loc);
    6871             : }

Generated by: LCOV version 1.13