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