LLParser.h 28 KB

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