LLParser.h 26 KB

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