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