1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the parser class for .ll files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_ASMPARSER_LLPARSER_H 14 #define LLVM_ASMPARSER_LLPARSER_H 15 16 #include "LLLexer.h" 17 #include "llvm/ADT/StringMap.h" 18 #include "llvm/AsmParser/NumberedValues.h" 19 #include "llvm/AsmParser/Parser.h" 20 #include "llvm/IR/Attributes.h" 21 #include "llvm/IR/FMF.h" 22 #include "llvm/IR/Instructions.h" 23 #include "llvm/IR/ModuleSummaryIndex.h" 24 #include "llvm/Support/ModRef.h" 25 #include <map> 26 #include <optional> 27 28 namespace llvm { 29 class Module; 30 class ConstantRange; 31 class FunctionType; 32 class GlobalObject; 33 class SMDiagnostic; 34 class SMLoc; 35 class SourceMgr; 36 class Type; 37 struct MaybeAlign; 38 class Function; 39 class Value; 40 class BasicBlock; 41 class Instruction; 42 class Constant; 43 class GlobalValue; 44 class Comdat; 45 class MDString; 46 class MDNode; 47 struct SlotMapping; 48 49 /// ValID - Represents a reference of a definition of some sort with no type. 50 /// There are several cases where we have to parse the value but where the 51 /// type can depend on later context. This may either be a numeric reference 52 /// or a symbolic (%var) reference. This is just a discriminated union. 53 struct ValID { 54 enum { 55 t_LocalID, // ID in UIntVal. 56 t_GlobalID, // ID in UIntVal. 57 t_LocalName, // Name in StrVal. 58 t_GlobalName, // Name in StrVal. 59 t_APSInt, // Value in APSIntVal. 60 t_APFloat, // Value in APFloatVal. 61 t_Null, // No value. 62 t_Undef, // No value. 63 t_Zero, // No value. 64 t_None, // No value. 65 t_Poison, // No value. 66 t_EmptyArray, // No value: [] 67 t_Constant, // Value in ConstantVal. 68 t_ConstantSplat, // Value in ConstantVal. 69 t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal. 70 t_ConstantStruct, // Value in ConstantStructElts. 71 t_PackedConstantStruct // Value in ConstantStructElts. 72 } Kind = t_LocalID; 73 74 LLLexer::LocTy Loc; 75 unsigned UIntVal; 76 FunctionType *FTy = nullptr; 77 std::string StrVal, StrVal2; 78 APSInt APSIntVal; 79 APFloat APFloatVal{0.0}; 80 Constant *ConstantVal; 81 std::unique_ptr<Constant *[]> ConstantStructElts; 82 bool NoCFI = false; 83 84 ValID() = default; ValIDValID85 ValID(const ValID &RHS) 86 : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy), 87 StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal), 88 APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal), 89 NoCFI(RHS.NoCFI) { 90 assert(!RHS.ConstantStructElts); 91 } 92 93 bool operator<(const ValID &RHS) const { 94 assert(Kind == RHS.Kind && "Comparing ValIDs of different kinds"); 95 if (Kind == t_LocalID || Kind == t_GlobalID) 96 return UIntVal < RHS.UIntVal; 97 assert((Kind == t_LocalName || Kind == t_GlobalName || 98 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && 99 "Ordering not defined for this ValID kind yet"); 100 return StrVal < RHS.StrVal; 101 } 102 }; 103 104 class LLParser { 105 public: 106 typedef LLLexer::LocTy LocTy; 107 private: 108 LLVMContext &Context; 109 // Lexer to determine whether to use opaque pointers or not. 110 LLLexer OPLex; 111 LLLexer Lex; 112 // Module being parsed, null if we are only parsing summary index. 113 Module *M; 114 // Summary index being parsed, null if we are only parsing Module. 115 ModuleSummaryIndex *Index; 116 SlotMapping *Slots; 117 118 SmallVector<Instruction*, 64> InstsWithTBAATag; 119 120 /// DIAssignID metadata does not support temporary RAUW so we cannot use 121 /// the normal metadata forward reference resolution method. Instead, 122 /// non-temporary DIAssignID are attached to instructions (recorded here) 123 /// then replaced later. 124 DenseMap<MDNode *, SmallVector<Instruction *, 2>> TempDIAssignIDAttachments; 125 126 // Type resolution handling data structures. The location is set when we 127 // have processed a use of the type but not a definition yet. 128 StringMap<std::pair<Type*, LocTy> > NamedTypes; 129 std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes; 130 131 std::map<unsigned, TrackingMDNodeRef> NumberedMetadata; 132 std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes; 133 134 // Global Value reference information. 135 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 136 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 137 NumberedValues<GlobalValue *> NumberedVals; 138 139 // Comdat forward reference information. 140 std::map<std::string, LocTy> ForwardRefComdats; 141 142 // References to blockaddress. The key is the function ValID, the value is 143 // a list of references to blocks in that function. 144 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses; 145 class PerFunctionState; 146 /// Reference to per-function state to allow basic blocks to be 147 /// forward-referenced by blockaddress instructions within the same 148 /// function. 149 PerFunctionState *BlockAddressPFS; 150 151 // References to dso_local_equivalent. The key is the global's ValID, the 152 // value is a placeholder value that will be replaced. Note there are two 153 // maps for tracking ValIDs that are GlobalNames and ValIDs that are 154 // GlobalIDs. These are needed because "operator<" doesn't discriminate 155 // between the two. 156 std::map<ValID, GlobalValue *> ForwardRefDSOLocalEquivalentNames; 157 std::map<ValID, GlobalValue *> ForwardRefDSOLocalEquivalentIDs; 158 159 // Attribute builder reference information. 160 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups; 161 std::map<unsigned, AttrBuilder> NumberedAttrBuilders; 162 163 // Summary global value reference information. 164 std::map<unsigned, std::vector<std::pair<ValueInfo *, LocTy>>> 165 ForwardRefValueInfos; 166 std::map<unsigned, std::vector<std::pair<AliasSummary *, LocTy>>> 167 ForwardRefAliasees; 168 std::vector<ValueInfo> NumberedValueInfos; 169 170 // Summary type id reference information. 171 std::map<unsigned, std::vector<std::pair<GlobalValue::GUID *, LocTy>>> 172 ForwardRefTypeIds; 173 174 // Map of module ID to path. 175 std::map<unsigned, StringRef> ModuleIdMap; 176 177 /// Only the llvm-as tool may set this to false to bypass 178 /// UpgradeDebuginfo so it can generate broken bitcode. 179 bool UpgradeDebugInfo; 180 181 bool SeenNewDbgInfoFormat = false; 182 bool SeenOldDbgInfoFormat = false; 183 184 std::string SourceFileName; 185 186 public: 187 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M, 188 ModuleSummaryIndex *Index, LLVMContext &Context, 189 SlotMapping *Slots = nullptr) Context(Context)190 : Context(Context), OPLex(F, SM, Err, Context), 191 Lex(F, SM, Err, Context), M(M), Index(Index), Slots(Slots), 192 BlockAddressPFS(nullptr) {} 193 bool Run( 194 bool UpgradeDebugInfo, 195 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) { 196 return std::nullopt; 197 }); 198 199 bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots); 200 201 bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, 202 const SlotMapping *Slots); 203 getContext()204 LLVMContext &getContext() { return Context; } 205 206 private: error(LocTy L,const Twine & Msg)207 bool error(LocTy L, const Twine &Msg) const { return Lex.Error(L, Msg); } tokError(const Twine & Msg)208 bool tokError(const Twine &Msg) const { return error(Lex.getLoc(), Msg); } 209 210 bool checkValueID(LocTy L, StringRef Kind, StringRef Prefix, 211 unsigned NextID, unsigned ID) const; 212 213 /// Restore the internal name and slot mappings using the mappings that 214 /// were created at an earlier parsing stage. 215 void restoreParsingState(const SlotMapping *Slots); 216 217 /// getGlobalVal - Get a value with the specified name or ID, creating a 218 /// forward reference record if needed. This can return null if the value 219 /// exists but does not have the right type. 220 GlobalValue *getGlobalVal(const std::string &N, Type *Ty, LocTy Loc); 221 GlobalValue *getGlobalVal(unsigned ID, Type *Ty, LocTy Loc); 222 223 /// Get a Comdat with the specified name, creating a forward reference 224 /// record if needed. 225 Comdat *getComdat(const std::string &Name, LocTy Loc); 226 227 // Helper Routines. 228 bool parseToken(lltok::Kind T, const char *ErrMsg); EatIfPresent(lltok::Kind T)229 bool EatIfPresent(lltok::Kind T) { 230 if (Lex.getKind() != T) return false; 231 Lex.Lex(); 232 return true; 233 } 234 EatFastMathFlagsIfPresent()235 FastMathFlags EatFastMathFlagsIfPresent() { 236 FastMathFlags FMF; 237 while (true) 238 switch (Lex.getKind()) { 239 case lltok::kw_fast: FMF.setFast(); Lex.Lex(); continue; 240 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue; 241 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue; 242 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue; 243 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue; 244 case lltok::kw_contract: 245 FMF.setAllowContract(true); 246 Lex.Lex(); 247 continue; 248 case lltok::kw_reassoc: FMF.setAllowReassoc(); Lex.Lex(); continue; 249 case lltok::kw_afn: FMF.setApproxFunc(); Lex.Lex(); continue; 250 default: return FMF; 251 } 252 return FMF; 253 } 254 255 bool parseOptionalToken(lltok::Kind T, bool &Present, 256 LocTy *Loc = nullptr) { 257 if (Lex.getKind() != T) { 258 Present = false; 259 } else { 260 if (Loc) 261 *Loc = Lex.getLoc(); 262 Lex.Lex(); 263 Present = true; 264 } 265 return false; 266 } 267 bool parseStringConstant(std::string &Result); 268 bool parseUInt32(unsigned &Val); parseUInt32(unsigned & Val,LocTy & Loc)269 bool parseUInt32(unsigned &Val, LocTy &Loc) { 270 Loc = Lex.getLoc(); 271 return parseUInt32(Val); 272 } 273 bool parseUInt64(uint64_t &Val); parseUInt64(uint64_t & Val,LocTy & Loc)274 bool parseUInt64(uint64_t &Val, LocTy &Loc) { 275 Loc = Lex.getLoc(); 276 return parseUInt64(Val); 277 } 278 bool parseFlag(unsigned &Val); 279 280 bool parseStringAttribute(AttrBuilder &B); 281 282 bool parseTLSModel(GlobalVariable::ThreadLocalMode &TLM); 283 bool parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); 284 bool parseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr); 285 bool parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS = 0); parseOptionalProgramAddrSpace(unsigned & AddrSpace)286 bool parseOptionalProgramAddrSpace(unsigned &AddrSpace) { 287 return parseOptionalAddrSpace( 288 AddrSpace, M->getDataLayout().getProgramAddressSpace()); 289 }; 290 bool parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B, 291 bool InAttrGroup); 292 bool parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam); parseOptionalParamAttrs(AttrBuilder & B)293 bool parseOptionalParamAttrs(AttrBuilder &B) { 294 return parseOptionalParamOrReturnAttrs(B, true); 295 } parseOptionalReturnAttrs(AttrBuilder & B)296 bool parseOptionalReturnAttrs(AttrBuilder &B) { 297 return parseOptionalParamOrReturnAttrs(B, false); 298 } 299 bool parseOptionalLinkage(unsigned &Res, bool &HasLinkage, 300 unsigned &Visibility, unsigned &DLLStorageClass, 301 bool &DSOLocal); 302 void parseOptionalDSOLocal(bool &DSOLocal); 303 void parseOptionalVisibility(unsigned &Res); 304 bool parseOptionalImportType(lltok::Kind Kind, 305 GlobalValueSummary::ImportKind &Res); 306 void parseOptionalDLLStorageClass(unsigned &Res); 307 bool parseOptionalCallingConv(unsigned &CC); 308 bool parseOptionalAlignment(MaybeAlign &Alignment, 309 bool AllowParens = false); 310 bool parseOptionalCodeModel(CodeModel::Model &model); 311 bool parseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes); 312 bool parseOptionalUWTableKind(UWTableKind &Kind); 313 bool parseAllocKind(AllocFnKind &Kind); 314 std::optional<MemoryEffects> parseMemoryAttr(); 315 unsigned parseNoFPClassAttr(); 316 bool parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID, 317 AtomicOrdering &Ordering); 318 bool parseScope(SyncScope::ID &SSID); 319 bool parseOrdering(AtomicOrdering &Ordering); 320 bool parseOptionalStackAlignment(unsigned &Alignment); 321 bool parseOptionalCommaAlign(MaybeAlign &Alignment, bool &AteExtraComma); 322 bool parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc, 323 bool &AteExtraComma); 324 bool parseAllocSizeArguments(unsigned &BaseSizeArg, 325 std::optional<unsigned> &HowManyArg); 326 bool parseVScaleRangeArguments(unsigned &MinValue, unsigned &MaxValue); 327 bool parseIndexList(SmallVectorImpl<unsigned> &Indices, 328 bool &AteExtraComma); parseIndexList(SmallVectorImpl<unsigned> & Indices)329 bool parseIndexList(SmallVectorImpl<unsigned> &Indices) { 330 bool AteExtraComma; 331 if (parseIndexList(Indices, AteExtraComma)) 332 return true; 333 if (AteExtraComma) 334 return tokError("expected index"); 335 return false; 336 } 337 338 // Top-Level Entities 339 bool parseTopLevelEntities(); 340 bool finalizeDebugInfoFormat(Module *M); 341 void dropUnknownMetadataReferences(); 342 bool validateEndOfModule(bool UpgradeDebugInfo); 343 bool validateEndOfIndex(); 344 bool parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback); 345 bool parseTargetDefinition(std::string &TentativeDLStr, LocTy &DLStrLoc); 346 bool parseModuleAsm(); 347 bool parseSourceFileName(); 348 bool parseUnnamedType(); 349 bool parseNamedType(); 350 bool parseDeclare(); 351 bool parseDefine(); 352 353 bool parseGlobalType(bool &IsConstant); 354 bool parseUnnamedGlobal(); 355 bool parseNamedGlobal(); 356 bool parseGlobal(const std::string &Name, unsigned NameID, LocTy NameLoc, 357 unsigned Linkage, bool HasLinkage, unsigned Visibility, 358 unsigned DLLStorageClass, bool DSOLocal, 359 GlobalVariable::ThreadLocalMode TLM, 360 GlobalVariable::UnnamedAddr UnnamedAddr); 361 bool parseAliasOrIFunc(const std::string &Name, unsigned NameID, 362 LocTy NameLoc, unsigned L, unsigned Visibility, 363 unsigned DLLStorageClass, bool DSOLocal, 364 GlobalVariable::ThreadLocalMode TLM, 365 GlobalVariable::UnnamedAddr UnnamedAddr); 366 bool parseComdat(); 367 bool parseStandaloneMetadata(); 368 bool parseNamedMetadata(); 369 bool parseMDString(MDString *&Result); 370 bool parseMDNodeID(MDNode *&Result); 371 bool parseUnnamedAttrGrp(); 372 bool parseFnAttributeValuePairs(AttrBuilder &B, 373 std::vector<unsigned> &FwdRefAttrGrps, 374 bool inAttrGrp, LocTy &BuiltinLoc); 375 bool parseRangeAttr(AttrBuilder &B); 376 bool parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken, 377 Attribute::AttrKind AttrKind); 378 379 // Module Summary Index Parsing. 380 bool skipModuleSummaryEntry(); 381 bool parseSummaryEntry(); 382 bool parseModuleEntry(unsigned ID); 383 bool parseModuleReference(StringRef &ModulePath); 384 bool parseGVReference(ValueInfo &VI, unsigned &GVId); 385 bool parseSummaryIndexFlags(); 386 bool parseBlockCount(); 387 bool parseGVEntry(unsigned ID); 388 bool parseFunctionSummary(std::string Name, GlobalValue::GUID, unsigned ID); 389 bool parseVariableSummary(std::string Name, GlobalValue::GUID, unsigned ID); 390 bool parseAliasSummary(std::string Name, GlobalValue::GUID, unsigned ID); 391 bool parseGVFlags(GlobalValueSummary::GVFlags &GVFlags); 392 bool parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags); 393 bool parseOptionalFFlags(FunctionSummary::FFlags &FFlags); 394 bool parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls); 395 bool parseHotness(CalleeInfo::HotnessType &Hotness); 396 bool parseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo); 397 bool parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests); 398 bool parseVFuncIdList(lltok::Kind Kind, 399 std::vector<FunctionSummary::VFuncId> &VFuncIdList); 400 bool parseConstVCallList( 401 lltok::Kind Kind, 402 std::vector<FunctionSummary::ConstVCall> &ConstVCallList); 403 using IdToIndexMapType = 404 std::map<unsigned, std::vector<std::pair<unsigned, LocTy>>>; 405 bool parseConstVCall(FunctionSummary::ConstVCall &ConstVCall, 406 IdToIndexMapType &IdToIndexMap, unsigned Index); 407 bool parseVFuncId(FunctionSummary::VFuncId &VFuncId, 408 IdToIndexMapType &IdToIndexMap, unsigned Index); 409 bool parseOptionalVTableFuncs(VTableFuncList &VTableFuncs); 410 bool parseOptionalParamAccesses( 411 std::vector<FunctionSummary::ParamAccess> &Params); 412 bool parseParamNo(uint64_t &ParamNo); 413 using IdLocListType = std::vector<std::pair<unsigned, LocTy>>; 414 bool parseParamAccess(FunctionSummary::ParamAccess &Param, 415 IdLocListType &IdLocList); 416 bool parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call, 417 IdLocListType &IdLocList); 418 bool parseParamAccessOffset(ConstantRange &Range); 419 bool parseOptionalRefs(std::vector<ValueInfo> &Refs); 420 bool parseTypeIdEntry(unsigned ID); 421 bool parseTypeIdSummary(TypeIdSummary &TIS); 422 bool parseTypeIdCompatibleVtableEntry(unsigned ID); 423 bool parseTypeTestResolution(TypeTestResolution &TTRes); 424 bool parseOptionalWpdResolutions( 425 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap); 426 bool parseWpdRes(WholeProgramDevirtResolution &WPDRes); 427 bool parseOptionalResByArg( 428 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> 429 &ResByArg); 430 bool parseArgs(std::vector<uint64_t> &Args); 431 bool addGlobalValueToIndex(std::string Name, GlobalValue::GUID, 432 GlobalValue::LinkageTypes Linkage, unsigned ID, 433 std::unique_ptr<GlobalValueSummary> Summary, 434 LocTy Loc); 435 bool parseOptionalAllocs(std::vector<AllocInfo> &Allocs); 436 bool parseMemProfs(std::vector<MIBInfo> &MIBs); 437 bool parseAllocType(uint8_t &AllocType); 438 bool parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites); 439 440 // Type Parsing. 441 bool parseType(Type *&Result, const Twine &Msg, bool AllowVoid = false); 442 bool parseType(Type *&Result, bool AllowVoid = false) { 443 return parseType(Result, "expected type", AllowVoid); 444 } 445 bool parseType(Type *&Result, const Twine &Msg, LocTy &Loc, 446 bool AllowVoid = false) { 447 Loc = Lex.getLoc(); 448 return parseType(Result, Msg, AllowVoid); 449 } 450 bool parseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { 451 Loc = Lex.getLoc(); 452 return parseType(Result, AllowVoid); 453 } 454 bool parseAnonStructType(Type *&Result, bool Packed); 455 bool parseStructBody(SmallVectorImpl<Type *> &Body); 456 bool parseStructDefinition(SMLoc TypeLoc, StringRef Name, 457 std::pair<Type *, LocTy> &Entry, 458 Type *&ResultTy); 459 460 bool parseArrayVectorType(Type *&Result, bool IsVector); 461 bool parseFunctionType(Type *&Result); 462 bool parseTargetExtType(Type *&Result); 463 464 // Function Semantic Analysis. 465 class PerFunctionState { 466 LLParser &P; 467 Function &F; 468 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 469 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 470 NumberedValues<Value *> NumberedVals; 471 472 /// FunctionNumber - If this is an unnamed function, this is the slot 473 /// number of it, otherwise it is -1. 474 int FunctionNumber; 475 476 public: 477 PerFunctionState(LLParser &p, Function &f, int functionNumber, 478 ArrayRef<unsigned> UnnamedArgNums); 479 ~PerFunctionState(); 480 getFunction()481 Function &getFunction() const { return F; } 482 483 bool finishFunction(); 484 485 /// GetVal - Get a value with the specified name or ID, creating a 486 /// forward reference record if needed. This can return null if the value 487 /// exists but does not have the right type. 488 Value *getVal(const std::string &Name, Type *Ty, LocTy Loc); 489 Value *getVal(unsigned ID, Type *Ty, LocTy Loc); 490 491 /// setInstName - After an instruction is parsed and inserted into its 492 /// basic block, this installs its name. 493 bool setInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 494 Instruction *Inst); 495 496 /// GetBB - Get a basic block with the specified name or ID, creating a 497 /// forward reference record if needed. This can return null if the value 498 /// is not a BasicBlock. 499 BasicBlock *getBB(const std::string &Name, LocTy Loc); 500 BasicBlock *getBB(unsigned ID, LocTy Loc); 501 502 /// DefineBB - Define the specified basic block, which is either named or 503 /// unnamed. If there is an error, this returns null otherwise it returns 504 /// the block being defined. 505 BasicBlock *defineBB(const std::string &Name, int NameID, LocTy Loc); 506 507 bool resolveForwardRefBlockAddresses(); 508 }; 509 510 bool convertValIDToValue(Type *Ty, ValID &ID, Value *&V, 511 PerFunctionState *PFS); 512 513 Value *checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty, 514 Value *Val); 515 516 bool parseConstantValue(Type *Ty, Constant *&C); 517 bool parseValue(Type *Ty, Value *&V, PerFunctionState *PFS); parseValue(Type * Ty,Value * & V,PerFunctionState & PFS)518 bool parseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { 519 return parseValue(Ty, V, &PFS); 520 } 521 parseValue(Type * Ty,Value * & V,LocTy & Loc,PerFunctionState & PFS)522 bool parseValue(Type *Ty, Value *&V, LocTy &Loc, PerFunctionState &PFS) { 523 Loc = Lex.getLoc(); 524 return parseValue(Ty, V, &PFS); 525 } 526 527 bool parseTypeAndValue(Value *&V, PerFunctionState *PFS); parseTypeAndValue(Value * & V,PerFunctionState & PFS)528 bool parseTypeAndValue(Value *&V, PerFunctionState &PFS) { 529 return parseTypeAndValue(V, &PFS); 530 } parseTypeAndValue(Value * & V,LocTy & Loc,PerFunctionState & PFS)531 bool parseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 532 Loc = Lex.getLoc(); 533 return parseTypeAndValue(V, PFS); 534 } 535 bool parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 536 PerFunctionState &PFS); parseTypeAndBasicBlock(BasicBlock * & BB,PerFunctionState & PFS)537 bool parseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { 538 LocTy Loc; 539 return parseTypeAndBasicBlock(BB, Loc, PFS); 540 } 541 542 struct ParamInfo { 543 LocTy Loc; 544 Value *V; 545 AttributeSet Attrs; ParamInfoParamInfo546 ParamInfo(LocTy loc, Value *v, AttributeSet attrs) 547 : Loc(loc), V(v), Attrs(attrs) {} 548 }; 549 bool parseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 550 PerFunctionState &PFS, bool IsMustTailCall = false, 551 bool InVarArgsFunc = false); 552 553 bool 554 parseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList, 555 PerFunctionState &PFS); 556 557 bool parseExceptionArgs(SmallVectorImpl<Value *> &Args, 558 PerFunctionState &PFS); 559 560 bool resolveFunctionType(Type *RetType, 561 const SmallVector<ParamInfo, 16> &ArgList, 562 FunctionType *&FuncTy); 563 564 // Constant Parsing. 565 bool parseValID(ValID &ID, PerFunctionState *PFS, 566 Type *ExpectedTy = nullptr); 567 bool parseGlobalValue(Type *Ty, Constant *&C); 568 bool parseGlobalTypeAndValue(Constant *&V); 569 bool parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts); 570 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C); 571 bool parseSanitizer(GlobalVariable *GV); 572 bool parseMetadataAsValue(Value *&V, PerFunctionState &PFS); 573 bool parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg, 574 PerFunctionState *PFS); 575 bool parseDIArgList(Metadata *&MD, PerFunctionState *PFS); 576 bool parseMetadata(Metadata *&MD, PerFunctionState *PFS); 577 bool parseMDTuple(MDNode *&MD, bool IsDistinct = false); 578 bool parseMDNode(MDNode *&N); 579 bool parseMDNodeTail(MDNode *&N); 580 bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts); 581 bool parseMetadataAttachment(unsigned &Kind, MDNode *&MD); 582 bool parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS); 583 bool parseInstructionMetadata(Instruction &Inst); 584 bool parseGlobalObjectMetadataAttachment(GlobalObject &GO); 585 bool parseOptionalFunctionMetadata(Function &F); 586 587 template <class FieldTy> 588 bool parseMDField(LocTy Loc, StringRef Name, FieldTy &Result); 589 template <class FieldTy> bool parseMDField(StringRef Name, FieldTy &Result); 590 template <class ParserTy> bool parseMDFieldsImplBody(ParserTy ParseField); 591 template <class ParserTy> 592 bool parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc); 593 bool parseSpecializedMDNode(MDNode *&N, bool IsDistinct = false); 594 595 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ 596 bool parse##CLASS(MDNode *&Result, bool IsDistinct); 597 #include "llvm/IR/Metadata.def" 598 599 // Function Parsing. 600 struct ArgInfo { 601 LocTy Loc; 602 Type *Ty; 603 AttributeSet Attrs; 604 std::string Name; ArgInfoArgInfo605 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N) 606 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} 607 }; 608 bool parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, 609 SmallVectorImpl<unsigned> &UnnamedArgNums, 610 bool &IsVarArg); 611 bool parseFunctionHeader(Function *&Fn, bool IsDefine, 612 unsigned &FunctionNumber, 613 SmallVectorImpl<unsigned> &UnnamedArgNums); 614 bool parseFunctionBody(Function &Fn, unsigned FunctionNumber, 615 ArrayRef<unsigned> UnnamedArgNums); 616 bool parseBasicBlock(PerFunctionState &PFS); 617 618 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail }; 619 620 // Instruction Parsing. Each instruction parsing routine can return with a 621 // normal result, an error result, or return having eaten an extra comma. 622 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; 623 int parseInstruction(Instruction *&Inst, BasicBlock *BB, 624 PerFunctionState &PFS); 625 bool parseCmpPredicate(unsigned &P, unsigned Opc); 626 627 bool parseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 628 bool parseBr(Instruction *&Inst, PerFunctionState &PFS); 629 bool parseSwitch(Instruction *&Inst, PerFunctionState &PFS); 630 bool parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); 631 bool parseInvoke(Instruction *&Inst, PerFunctionState &PFS); 632 bool parseResume(Instruction *&Inst, PerFunctionState &PFS); 633 bool parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS); 634 bool parseCatchRet(Instruction *&Inst, PerFunctionState &PFS); 635 bool parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS); 636 bool parseCatchPad(Instruction *&Inst, PerFunctionState &PFS); 637 bool parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS); 638 bool parseCallBr(Instruction *&Inst, PerFunctionState &PFS); 639 640 bool parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc, 641 bool IsFP); 642 bool parseArithmetic(Instruction *&Inst, PerFunctionState &PFS, 643 unsigned Opc, bool IsFP); 644 bool parseLogical(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc); 645 bool parseCompare(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc); 646 bool parseCast(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc); 647 bool parseSelect(Instruction *&Inst, PerFunctionState &PFS); 648 bool parseVAArg(Instruction *&Inst, PerFunctionState &PFS); 649 bool parseExtractElement(Instruction *&Inst, PerFunctionState &PFS); 650 bool parseInsertElement(Instruction *&Inst, PerFunctionState &PFS); 651 bool parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS); 652 int parsePHI(Instruction *&Inst, PerFunctionState &PFS); 653 bool parseLandingPad(Instruction *&Inst, PerFunctionState &PFS); 654 bool parseCall(Instruction *&Inst, PerFunctionState &PFS, 655 CallInst::TailCallKind TCK); 656 int parseAlloc(Instruction *&Inst, PerFunctionState &PFS); 657 int parseLoad(Instruction *&Inst, PerFunctionState &PFS); 658 int parseStore(Instruction *&Inst, PerFunctionState &PFS); 659 int parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS); 660 int parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS); 661 int parseFence(Instruction *&Inst, PerFunctionState &PFS); 662 int parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS); 663 int parseExtractValue(Instruction *&Inst, PerFunctionState &PFS); 664 int parseInsertValue(Instruction *&Inst, PerFunctionState &PFS); 665 bool parseFreeze(Instruction *&I, PerFunctionState &PFS); 666 667 // Use-list order directives. 668 bool parseUseListOrder(PerFunctionState *PFS = nullptr); 669 bool parseUseListOrderBB(); 670 bool parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes); 671 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc); 672 }; 673 } // End llvm namespace 674 675 #endif 676