LLParser.h 26 KB

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