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