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