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
189 PendingDbgRecords;
191 PendingDbgInsts;
192
193 /// Only the llvm-as tool may set this to false to bypass
194 /// UpgradeDebuginfo so it can generate broken bitcode.
195 bool UpgradeDebugInfo;
196
197 bool SeenNewDbgInfoFormat = false;
198 bool SeenOldDbgInfoFormat = false;
199
200 std::string SourceFileName;
201
202 FileLoc getTokLineColumnPos() {
203 if (ParserContext)
204 return Lex.getTokLineColumnPos();
205 return {0u, 0u};
206 }
207
208 FileLoc getPrevTokEndLineColumnPos() {
209 if (ParserContext)
210 return Lex.getPrevTokEndLineColumnPos();
211 return {0u, 0u};
212 }
213
214 public:
216 ModuleSummaryIndex *Index, LLVMContext &Context,
217 SlotMapping *Slots = nullptr,
218 AsmParserContext *ParserContext = nullptr)
219 : Context(Context), OPLex(F, SM, Err, Context),
220 Lex(F, SM, Err, Context), M(M), Index(Index), Slots(Slots),
221 BlockAddressPFS(nullptr), ParserContext(ParserContext) {}
222 bool Run(
223 bool UpgradeDebugInfo,
224 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
225 return std::nullopt;
226 });
227
228 bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
229
230 bool parseTypeAtBeginning(Type *&Ty, unsigned &Read,
231 const SlotMapping *Slots);
232
233 bool parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read,
234 const SlotMapping *Slots);
235
236 LLVMContext &getContext() { return Context; }
237
238 private:
239 bool error(LocTy L, const Twine &Msg) { return Lex.ParseError(L, Msg); }
240 bool tokError(const Twine &Msg) { return error(Lex.getLoc(), Msg); }
241
242 bool checkValueID(LocTy L, StringRef Kind, StringRef Prefix,
243 unsigned NextID, unsigned ID);
244
245 /// Restore the internal name and slot mappings using the mappings that
246 /// were created at an earlier parsing stage.
247 void restoreParsingState(const SlotMapping *Slots);
248
249 /// getGlobalVal - Get a value with the specified name or ID, creating a
250 /// forward reference record if needed. This can return null if the value
251 /// exists but does not have the right type.
252 GlobalValue *getGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
253 GlobalValue *getGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
254
255 /// Get a Comdat with the specified name, creating a forward reference
256 /// record if needed.
257 Comdat *getComdat(const std::string &Name, LocTy Loc);
258
259 // Helper Routines.
260 bool parseToken(lltok::Kind T, const char *ErrMsg);
261 bool EatIfPresent(lltok::Kind T) {
262 if (Lex.getKind() != T) return false;
263 Lex.Lex();
264 return true;
265 }
266
267 FastMathFlags EatFastMathFlagsIfPresent() {
268 FastMathFlags FMF;
269 while (true)
270 switch (Lex.getKind()) {
271 case lltok::kw_fast: FMF.setFast(); Lex.Lex(); continue;
272 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
273 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
274 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
275 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
277 FMF.setAllowContract(true);
278 Lex.Lex();
279 continue;
280 case lltok::kw_reassoc: FMF.setAllowReassoc(); Lex.Lex(); continue;
281 case lltok::kw_afn: FMF.setApproxFunc(); Lex.Lex(); continue;
282 default: return FMF;
283 }
284 return FMF;
285 }
286
287 bool parseOptionalToken(lltok::Kind T, bool &Present,
288 LocTy *Loc = nullptr) {
289 if (Lex.getKind() != T) {
290 Present = false;
291 } else {
292 if (Loc)
293 *Loc = Lex.getLoc();
294 Lex.Lex();
295 Present = true;
296 }
297 return false;
298 }
299 bool parseStringConstant(std::string &Result);
300 bool parseUInt32(unsigned &Val);
301 bool parseUInt32(unsigned &Val, LocTy &Loc) {
302 Loc = Lex.getLoc();
303 return parseUInt32(Val);
304 }
305 bool parseUInt64(uint64_t &Val);
306 bool parseUInt64(uint64_t &Val, LocTy &Loc) {
307 Loc = Lex.getLoc();
308 return parseUInt64(Val);
309 }
310 bool parseFlag(unsigned &Val);
311
312 bool parseStringAttribute(AttrBuilder &B);
313
314 bool parseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
315 bool parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
316 bool parseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr);
317 bool parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS = 0);
318 bool parseOptionalProgramAddrSpace(unsigned &AddrSpace) {
319 return parseOptionalAddrSpace(
320 AddrSpace, M->getDataLayout().getProgramAddressSpace());
321 };
322 bool parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
323 bool InAttrGroup);
324 bool parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam);
325 bool parseOptionalParamAttrs(AttrBuilder &B) {
326 return parseOptionalParamOrReturnAttrs(B, true);
327 }
328 bool parseOptionalReturnAttrs(AttrBuilder &B) {
329 return parseOptionalParamOrReturnAttrs(B, false);
330 }
331 bool parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
332 unsigned &Visibility, unsigned &DLLStorageClass,
333 bool &DSOLocal);
334 void parseOptionalDSOLocal(bool &DSOLocal);
335 void parseOptionalVisibility(unsigned &Res);
336 bool parseOptionalImportType(lltok::Kind Kind,
338 void parseOptionalDLLStorageClass(unsigned &Res);
339 bool parseOptionalCallingConv(unsigned &CC);
340 bool parseOptionalAlignment(MaybeAlign &Alignment,
341 bool AllowParens = false);
342 bool parseOptionalPrefAlignment(MaybeAlign &Alignment);
343 bool parseOptionalCodeModel(CodeModel::Model &model);
344 bool parseOptionalAttrBytes(lltok::Kind AttrKind,
345 std::optional<uint64_t> &Bytes,
346 bool ErrorNoBytes = true);
347 bool parseOptionalUWTableKind(UWTableKind &Kind);
348 bool parseAllocKind(AllocFnKind &Kind);
349 std::optional<MemoryEffects> parseMemoryAttr();
350 std::optional<DenormalMode> parseDenormalFPEnvEntry();
351 std::optional<DenormalFPEnv> parseDenormalFPEnvAttr();
352 unsigned parseNoFPClassAttr();
353 bool parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
354 AtomicOrdering &Ordering);
355 bool parseScope(SyncScope::ID &SSID);
356 bool parseOrdering(AtomicOrdering &Ordering);
357 bool parseOptionalStackAlignment(unsigned &Alignment);
358 bool parseOptionalCommaAlign(MaybeAlign &Alignment, bool &AteExtraComma);
359 bool parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
360 bool &AteExtraComma);
361 bool parseAllocSizeArguments(unsigned &BaseSizeArg,
362 std::optional<unsigned> &HowManyArg);
363 bool parseVScaleRangeArguments(unsigned &MinValue, unsigned &MaxValue);
364 bool parseIndexList(SmallVectorImpl<unsigned> &Indices,
365 bool &AteExtraComma);
366 bool parseIndexList(SmallVectorImpl<unsigned> &Indices) {
367 bool AteExtraComma;
368 if (parseIndexList(Indices, AteExtraComma))
369 return true;
370 if (AteExtraComma)
371 return tokError("expected index");
372 return false;
373 }
374
375 // Top-Level Entities
376 bool parseTopLevelEntities();
377 void dropUnknownMetadataReferences();
378 bool validateEndOfModule(bool UpgradeDebugInfo);
379 bool validateEndOfIndex();
380 bool parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback);
381 bool parseTargetDefinition(std::string &TentativeDLStr, LocTy &DLStrLoc);
382 bool parseModuleAsm();
383 bool parseSourceFileName();
384 bool parseUnnamedType();
385 bool parseNamedType();
386 bool parseDeclare();
387 bool parseDefine();
388
389 bool parseGlobalType(bool &IsConstant);
390 bool parseUnnamedGlobal();
391 bool parseNamedGlobal();
392 bool parseGlobal(const std::string &Name, unsigned NameID, LocTy NameLoc,
393 unsigned Linkage, bool HasLinkage, unsigned Visibility,
394 unsigned DLLStorageClass, bool DSOLocal,
396 GlobalVariable::UnnamedAddr UnnamedAddr);
397 bool parseAliasOrIFunc(const std::string &Name, unsigned NameID,
398 LocTy NameLoc, unsigned L, unsigned Visibility,
399 unsigned DLLStorageClass, bool DSOLocal,
401 GlobalVariable::UnnamedAddr UnnamedAddr);
402 bool parseComdat();
403 bool parseStandaloneMetadata();
404 bool parseNamedMetadata();
405 bool parseMDString(MDString *&Result);
406 bool parseMDNodeID(MDNode *&Result);
407 bool parseUnnamedAttrGrp();
408 bool parseFnAttributeValuePairs(AttrBuilder &B,
409 std::vector<unsigned> &FwdRefAttrGrps,
410 bool inAttrGrp, LocTy &BuiltinLoc);
411 bool parseRangeAttr(AttrBuilder &B);
412 bool parseInitializesAttr(AttrBuilder &B);
413 bool parseCapturesAttr(AttrBuilder &B);
414 bool parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
415 Attribute::AttrKind AttrKind);
416
417 // Module Summary Index Parsing.
418 bool skipModuleSummaryEntry();
419 bool parseSummaryEntry();
420 bool parseModuleEntry(unsigned ID);
421 bool parseModuleReference(StringRef &ModulePath);
422 bool parseGVReference(ValueInfo &VI, unsigned &GVId);
423 bool parseSummaryIndexFlags();
424 bool parseBlockCount();
425 bool parseGVEntry(unsigned ID);
426 bool parseFunctionSummary(std::string Name, GlobalValue::GUID, unsigned ID);
427 bool parseVariableSummary(std::string Name, GlobalValue::GUID, unsigned ID);
428 bool parseAliasSummary(std::string Name, GlobalValue::GUID, unsigned ID);
429 bool parseGVFlags(GlobalValueSummary::GVFlags &GVFlags);
430 bool parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags);
431 bool parseOptionalFFlags(FunctionSummary::FFlags &FFlags);
432 bool parseOptionalCalls(SmallVectorImpl<FunctionSummary::EdgeTy> &Calls);
433 bool parseHotness(CalleeInfo::HotnessType &Hotness);
434 bool parseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo);
435 bool parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests);
436 bool parseVFuncIdList(lltok::Kind Kind,
437 std::vector<FunctionSummary::VFuncId> &VFuncIdList);
438 bool parseConstVCallList(
439 lltok::Kind Kind,
440 std::vector<FunctionSummary::ConstVCall> &ConstVCallList);
441 using IdToIndexMapType =
442 std::map<unsigned, std::vector<std::pair<unsigned, LocTy>>>;
443 bool parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
444 IdToIndexMapType &IdToIndexMap, unsigned Index);
445 bool parseVFuncId(FunctionSummary::VFuncId &VFuncId,
446 IdToIndexMapType &IdToIndexMap, unsigned Index);
447 bool parseOptionalVTableFuncs(VTableFuncList &VTableFuncs);
448 bool parseOptionalParamAccesses(
449 std::vector<FunctionSummary::ParamAccess> &Params);
450 bool parseParamNo(uint64_t &ParamNo);
451 using IdLocListType = std::vector<std::pair<unsigned, LocTy>>;
452 bool parseParamAccess(FunctionSummary::ParamAccess &Param,
453 IdLocListType &IdLocList);
454 bool parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
455 IdLocListType &IdLocList);
456 bool parseParamAccessOffset(ConstantRange &Range);
457 bool parseOptionalRefs(SmallVectorImpl<ValueInfo> &Refs);
458 bool parseTypeIdEntry(unsigned ID);
459 bool parseTypeIdSummary(TypeIdSummary &TIS);
460 bool parseTypeIdCompatibleVtableEntry(unsigned ID);
461 bool parseTypeTestResolution(TypeTestResolution &TTRes);
462 bool parseOptionalWpdResolutions(
463 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap);
464 bool parseWpdRes(WholeProgramDevirtResolution &WPDRes);
465 bool parseOptionalResByArg(
466 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
467 &ResByArg);
468 bool parseArgs(std::vector<uint64_t> &Args);
469 bool addGlobalValueToIndex(std::string Name, GlobalValue::GUID,
471 std::unique_ptr<GlobalValueSummary> Summary,
472 LocTy Loc);
473 bool parseOptionalAllocs(std::vector<AllocInfo> &Allocs);
474 bool parseMemProfs(std::vector<MIBInfo> &MIBs);
475 bool parseAllocType(uint8_t &AllocType);
476 bool parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites);
477
478 // Type Parsing.
479 bool parseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
480 bool parseType(Type *&Result, bool AllowVoid = false) {
481 return parseType(Result, "expected type", AllowVoid);
482 }
483 bool parseType(Type *&Result, const Twine &Msg, LocTy &Loc,
484 bool AllowVoid = false) {
485 Loc = Lex.getLoc();
486 return parseType(Result, Msg, AllowVoid);
487 }
488 bool parseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
489 Loc = Lex.getLoc();
490 return parseType(Result, AllowVoid);
491 }
492 bool parseAnonStructType(Type *&Result, bool Packed);
493 bool parseStructBody(SmallVectorImpl<Type *> &Body);
494 bool parseStructDefinition(SMLoc TypeLoc, StringRef Name,
495 std::pair<Type *, LocTy> &Entry,
496 Type *&ResultTy);
497
498 bool parseArrayVectorType(Type *&Result, bool IsVector);
499 bool parseFunctionType(Type *&Result);
500 bool parseTargetExtType(Type *&Result);
501
502 // Function Semantic Analysis.
503 class PerFunctionState {
504 LLParser &P;
505 Function &F;
506 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
507 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
508 NumberedValues<Value *> NumberedVals;
509
510 /// FunctionNumber - If this is an unnamed function, this is the slot
511 /// number of it, otherwise it is -1.
512 int FunctionNumber;
513
514 public:
515 PerFunctionState(LLParser &p, Function &f, int functionNumber,
516 ArrayRef<unsigned> UnnamedArgNums);
517 ~PerFunctionState();
518
519 Function &getFunction() const { return F; }
520
521 bool finishFunction();
522
523 /// GetVal - Get a value with the specified name or ID, creating a
524 /// forward reference record if needed. This can return null if the value
525 /// exists but does not have the right type.
526 Value *getVal(const std::string &Name, Type *Ty, LocTy Loc);
527 Value *getVal(unsigned ID, Type *Ty, LocTy Loc);
528
529 /// setInstName - After an instruction is parsed and inserted into its
530 /// basic block, this installs its name.
531 bool setInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
532 Instruction *Inst);
533
534 /// GetBB - Get a basic block with the specified name or ID, creating a
535 /// forward reference record if needed. This can return null if the value
536 /// is not a BasicBlock.
537 BasicBlock *getBB(const std::string &Name, LocTy Loc);
538 BasicBlock *getBB(unsigned ID, LocTy Loc);
539
540 /// DefineBB - Define the specified basic block, which is either named or
541 /// unnamed. If there is an error, this returns null otherwise it returns
542 /// the block being defined.
543 BasicBlock *defineBB(const std::string &Name, int NameID, LocTy Loc);
544
545 bool resolveForwardRefBlockAddresses();
546 };
547
548 bool convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
549 PerFunctionState *PFS);
550
551 Value *checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
552 Value *Val);
553
554 bool parseConstantValue(Type *Ty, Constant *&C);
555 bool parseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
556 bool parseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
557 return parseValue(Ty, V, &PFS);
558 }
559
560 bool parseValue(Type *Ty, Value *&V, LocTy &Loc, PerFunctionState &PFS) {
561 Loc = Lex.getLoc();
562 return parseValue(Ty, V, &PFS);
563 }
564
565 bool parseTypeAndValue(Value *&V, PerFunctionState *PFS);
566 bool parseTypeAndValue(Value *&V, PerFunctionState &PFS) {
567 return parseTypeAndValue(V, &PFS);
568 }
569 bool parseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
570 Loc = Lex.getLoc();
571 return parseTypeAndValue(V, PFS);
572 }
573 bool parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
574 PerFunctionState &PFS);
575 bool parseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
576 LocTy Loc;
577 return parseTypeAndBasicBlock(BB, Loc, PFS);
578 }
579
580 struct ParamInfo {
581 LocTy Loc;
582 Value *V;
583 AttributeSet Attrs;
584 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
585 : Loc(loc), V(v), Attrs(attrs) {}
586 };
587 bool parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
588 PerFunctionState &PFS, bool IsMustTailCall = false,
589 bool InVarArgsFunc = false);
590
591 bool
592 parseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList,
593 PerFunctionState &PFS);
594
595 bool parseExceptionArgs(SmallVectorImpl<Value *> &Args,
596 PerFunctionState &PFS);
597
598 bool resolveFunctionType(Type *RetType, ArrayRef<ParamInfo> ArgList,
599 FunctionType *&FuncTy);
600
601 // Constant Parsing.
602 bool parseValID(ValID &ID, PerFunctionState *PFS,
603 Type *ExpectedTy = nullptr);
604 bool parseGlobalValue(Type *Ty, Constant *&C);
605 bool parseGlobalTypeAndValue(Constant *&V);
606 bool parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
607 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
608 bool parseSanitizer(GlobalVariable *GV);
609 bool parseMetadataAsValue(Value *&V, PerFunctionState &PFS);
610 bool parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
611 PerFunctionState *PFS);
612 bool parseDIArgList(Metadata *&MD, PerFunctionState *PFS);
613 bool parseMetadata(Metadata *&MD, PerFunctionState *PFS);
614 bool parseMDTuple(MDNode *&MD, bool IsDistinct = false);
615 bool parseMDNode(MDNode *&N);
616 bool parseMDNodeTail(MDNode *&N);
617 bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
618 bool parseMetadataAttachment(unsigned &Kind, MDNode *&MD);
619 bool parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS);
620 bool parseInstructionMetadata(Instruction &Inst);
621 bool parseGlobalObjectMetadataAttachment(GlobalObject &GO);
622 bool parseOptionalFunctionMetadata(Function &F);
623
624 template <class FieldTy>
625 bool parseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
626 template <class FieldTy> bool parseMDField(StringRef Name, FieldTy &Result);
627 template <class ParserTy> bool parseMDFieldsImplBody(ParserTy ParseField);
628 template <class ParserTy>
629 bool parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc);
630 bool parseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
631 bool parseDIExpressionBody(MDNode *&Result, bool IsDistinct);
632
633#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
634 bool parse##CLASS(MDNode *&Result, bool IsDistinct);
635#include "llvm/IR/Metadata.def"
636
637 // Function Parsing.
638 struct ArgInfo {
639 LocTy Loc;
640 Type *Ty;
641 std::optional<FileLocRange> IdentLoc;
642 AttributeSet Attrs;
643 std::string Name;
644 ArgInfo(LocTy L, Type *ty, std::optional<FileLocRange> IdentLoc,
645 AttributeSet Attr, const std::string &N)
646 : Loc(L), Ty(ty), IdentLoc(IdentLoc), Attrs(Attr), Name(N) {}
647 };
648 bool parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
649 SmallVectorImpl<unsigned> &UnnamedArgNums,
650 bool &IsVarArg);
651 bool parseFunctionHeader(Function *&Fn, bool IsDefine,
652 unsigned &FunctionNumber,
653 SmallVectorImpl<unsigned> &UnnamedArgNums);
654 bool parseFunctionBody(Function &Fn, unsigned FunctionNumber,
655 ArrayRef<unsigned> UnnamedArgNums);
656 bool parseBasicBlock(PerFunctionState &PFS);
657
658 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
659
660 // Instruction Parsing. Each instruction parsing routine can return with a
661 // normal result, an error result, or return having eaten an extra comma.
662 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
663 int parseInstruction(Instruction *&Inst, BasicBlock *BB,
664 PerFunctionState &PFS);
665 bool parseCmpPredicate(unsigned &P, unsigned Opc);
666
667 bool parseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
668 bool parseBr(Instruction *&Inst, PerFunctionState &PFS);
669 bool parseSwitch(Instruction *&Inst, PerFunctionState &PFS);
670 bool parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
671 bool parseInvoke(Instruction *&Inst, PerFunctionState &PFS);
672 bool parseResume(Instruction *&Inst, PerFunctionState &PFS);
673 bool parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS);
674 bool parseCatchRet(Instruction *&Inst, PerFunctionState &PFS);
675 bool parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS);
676 bool parseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
677 bool parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
678 bool parseCallBr(Instruction *&Inst, PerFunctionState &PFS);
679
680 bool parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc,
681 bool IsFP);
682 bool parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
683 unsigned Opc, bool IsFP);
684 bool parseLogical(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
685 bool parseCompare(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
686 bool parseCast(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
687 bool parseSelect(Instruction *&Inst, PerFunctionState &PFS);
688 bool parseVAArg(Instruction *&Inst, PerFunctionState &PFS);
689 bool parseExtractElement(Instruction *&Inst, PerFunctionState &PFS);
690 bool parseInsertElement(Instruction *&Inst, PerFunctionState &PFS);
691 bool parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS);
692 int parsePHI(Instruction *&Inst, PerFunctionState &PFS);
693 bool parseLandingPad(Instruction *&Inst, PerFunctionState &PFS);
694 bool parseCall(Instruction *&Inst, PerFunctionState &PFS,
696 int parseAlloc(Instruction *&Inst, PerFunctionState &PFS);
697 int parseLoad(Instruction *&Inst, PerFunctionState &PFS);
698 int parseStore(Instruction *&Inst, PerFunctionState &PFS);
699 int parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS);
700 int parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS);
701 int parseFence(Instruction *&Inst, PerFunctionState &PFS);
702 int parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS);
703 int parseExtractValue(Instruction *&Inst, PerFunctionState &PFS);
704 int parseInsertValue(Instruction *&Inst, PerFunctionState &PFS);
705 bool parseFreeze(Instruction *&I, PerFunctionState &PFS);
706
707 // Use-list order directives.
708 bool parseUseListOrder(PerFunctionState *PFS = nullptr);
709 bool parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
710 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
711 };
712} // End llvm namespace
713
714#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:236
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:215
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:68
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:303
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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:46
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.
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