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

Generated by: LCOV version 1.13