]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/AsmParser/LLParser.h
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / AsmParser / LLParser.h
1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the parser class for .ll files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_LIB_ASMPARSER_LLPARSER_H
15 #define LLVM_LIB_ASMPARSER_LLPARSER_H
16
17 #include "LLLexer.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/Attributes.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/ModuleSummaryIndex.h"
24 #include "llvm/IR/Operator.h"
25 #include "llvm/IR/Type.h"
26 #include "llvm/IR/ValueHandle.h"
27 #include <map>
28
29 namespace llvm {
30   class Module;
31   class OpaqueType;
32   class Function;
33   class Value;
34   class BasicBlock;
35   class Instruction;
36   class Constant;
37   class GlobalValue;
38   class Comdat;
39   class MDString;
40   class MDNode;
41   struct SlotMapping;
42   class StructType;
43
44   /// ValID - Represents a reference of a definition of some sort with no type.
45   /// There are several cases where we have to parse the value but where the
46   /// type can depend on later context.  This may either be a numeric reference
47   /// or a symbolic (%var) reference.  This is just a discriminated union.
48   struct ValID {
49     enum {
50       t_LocalID, t_GlobalID,           // ID in UIntVal.
51       t_LocalName, t_GlobalName,       // Name in StrVal.
52       t_APSInt, t_APFloat,             // Value in APSIntVal/APFloatVal.
53       t_Null, t_Undef, t_Zero, t_None, // No value.
54       t_EmptyArray,                    // No value:  []
55       t_Constant,                      // Value in ConstantVal.
56       t_InlineAsm,                     // Value in FTy/StrVal/StrVal2/UIntVal.
57       t_ConstantStruct,                // Value in ConstantStructElts.
58       t_PackedConstantStruct           // Value in ConstantStructElts.
59     } Kind = t_LocalID;
60
61     LLLexer::LocTy Loc;
62     unsigned UIntVal;
63     FunctionType *FTy = nullptr;
64     std::string StrVal, StrVal2;
65     APSInt APSIntVal;
66     APFloat APFloatVal{0.0};
67     Constant *ConstantVal;
68     std::unique_ptr<Constant *[]> ConstantStructElts;
69
70     ValID() = default;
71     ValID(const ValID &RHS)
72         : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy),
73           StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal),
74           APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal) {
75       assert(!RHS.ConstantStructElts);
76     }
77
78     bool operator<(const ValID &RHS) const {
79       if (Kind == t_LocalID || Kind == t_GlobalID)
80         return UIntVal < RHS.UIntVal;
81       assert((Kind == t_LocalName || Kind == t_GlobalName ||
82               Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
83              "Ordering not defined for this ValID kind yet");
84       return StrVal < RHS.StrVal;
85     }
86   };
87
88   class LLParser {
89   public:
90     typedef LLLexer::LocTy LocTy;
91   private:
92     LLVMContext &Context;
93     LLLexer Lex;
94     // Module being parsed, null if we are only parsing summary index.
95     Module *M;
96     // Summary index being parsed, null if we are only parsing Module.
97     ModuleSummaryIndex *Index;
98     SlotMapping *Slots;
99
100     // Instruction metadata resolution.  Each instruction can have a list of
101     // MDRef info associated with them.
102     //
103     // The simpler approach of just creating temporary MDNodes and then calling
104     // RAUW on them when the definition is processed doesn't work because some
105     // instruction metadata kinds, such as dbg, get stored in the IR in an
106     // "optimized" format which doesn't participate in the normal value use
107     // lists. This means that RAUW doesn't work, even on temporary MDNodes
108     // which otherwise support RAUW. Instead, we defer resolving MDNode
109     // references until the definitions have been processed.
110     struct MDRef {
111       SMLoc Loc;
112       unsigned MDKind, MDSlot;
113     };
114
115     SmallVector<Instruction*, 64> InstsWithTBAATag;
116
117     // Type resolution handling data structures.  The location is set when we
118     // have processed a use of the type but not a definition yet.
119     StringMap<std::pair<Type*, LocTy> > NamedTypes;
120     std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
121
122     std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
123     std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
124
125     // Global Value reference information.
126     std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
127     std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
128     std::vector<GlobalValue*> NumberedVals;
129
130     // Comdat forward reference information.
131     std::map<std::string, LocTy> ForwardRefComdats;
132
133     // References to blockaddress.  The key is the function ValID, the value is
134     // a list of references to blocks in that function.
135     std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
136     class PerFunctionState;
137     /// Reference to per-function state to allow basic blocks to be
138     /// forward-referenced by blockaddress instructions within the same
139     /// function.
140     PerFunctionState *BlockAddressPFS;
141
142     // Attribute builder reference information.
143     std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
144     std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
145
146     // Summary global value reference information.
147     std::map<unsigned, std::vector<std::pair<ValueInfo *, LocTy>>>
148         ForwardRefValueInfos;
149     std::map<unsigned, std::vector<std::pair<AliasSummary *, LocTy>>>
150         ForwardRefAliasees;
151     std::vector<ValueInfo> NumberedValueInfos;
152
153     // Summary type id reference information.
154     std::map<unsigned, std::vector<std::pair<GlobalValue::GUID *, LocTy>>>
155         ForwardRefTypeIds;
156
157     // Map of module ID to path.
158     std::map<unsigned, StringRef> ModuleIdMap;
159
160     /// Only the llvm-as tool may set this to false to bypass
161     /// UpgradeDebuginfo so it can generate broken bitcode.
162     bool UpgradeDebugInfo;
163
164     /// DataLayout string to override that in LLVM assembly.
165     StringRef DataLayoutStr;
166
167     std::string SourceFileName;
168
169   public:
170     LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
171              ModuleSummaryIndex *Index, LLVMContext &Context,
172              SlotMapping *Slots = nullptr, bool UpgradeDebugInfo = true,
173              StringRef DataLayoutString = "")
174         : Context(Context), Lex(F, SM, Err, Context), M(M), Index(Index),
175           Slots(Slots), BlockAddressPFS(nullptr),
176           UpgradeDebugInfo(UpgradeDebugInfo), DataLayoutStr(DataLayoutString) {
177       if (!DataLayoutStr.empty())
178         M->setDataLayout(DataLayoutStr);
179     }
180     bool Run();
181
182     bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
183
184     bool parseTypeAtBeginning(Type *&Ty, unsigned &Read,
185                               const SlotMapping *Slots);
186
187     LLVMContext &getContext() { return Context; }
188
189   private:
190
191     bool Error(LocTy L, const Twine &Msg) const {
192       return Lex.Error(L, Msg);
193     }
194     bool TokError(const Twine &Msg) const {
195       return Error(Lex.getLoc(), Msg);
196     }
197
198     /// Restore the internal name and slot mappings using the mappings that
199     /// were created at an earlier parsing stage.
200     void restoreParsingState(const SlotMapping *Slots);
201
202     /// GetGlobalVal - Get a value with the specified name or ID, creating a
203     /// forward reference record if needed.  This can return null if the value
204     /// exists but does not have the right type.
205     GlobalValue *GetGlobalVal(const std::string &Name, Type *Ty, LocTy Loc);
206     GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
207
208     /// Get a Comdat with the specified name, creating a forward reference
209     /// record if needed.
210     Comdat *getComdat(const std::string &Name, LocTy Loc);
211
212     // Helper Routines.
213     bool ParseToken(lltok::Kind T, const char *ErrMsg);
214     bool EatIfPresent(lltok::Kind T) {
215       if (Lex.getKind() != T) return false;
216       Lex.Lex();
217       return true;
218     }
219
220     FastMathFlags EatFastMathFlagsIfPresent() {
221       FastMathFlags FMF;
222       while (true)
223         switch (Lex.getKind()) {
224         case lltok::kw_fast: FMF.setFast();            Lex.Lex(); continue;
225         case lltok::kw_nnan: FMF.setNoNaNs();          Lex.Lex(); continue;
226         case lltok::kw_ninf: FMF.setNoInfs();          Lex.Lex(); continue;
227         case lltok::kw_nsz:  FMF.setNoSignedZeros();   Lex.Lex(); continue;
228         case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
229         case lltok::kw_contract:
230           FMF.setAllowContract(true);
231           Lex.Lex();
232           continue;
233         case lltok::kw_reassoc: FMF.setAllowReassoc(); Lex.Lex(); continue;
234         case lltok::kw_afn:     FMF.setApproxFunc();   Lex.Lex(); continue;
235         default: return FMF;
236         }
237       return FMF;
238     }
239
240     bool ParseOptionalToken(lltok::Kind T, bool &Present,
241                             LocTy *Loc = nullptr) {
242       if (Lex.getKind() != T) {
243         Present = false;
244       } else {
245         if (Loc)
246           *Loc = Lex.getLoc();
247         Lex.Lex();
248         Present = true;
249       }
250       return false;
251     }
252     bool ParseStringConstant(std::string &Result);
253     bool ParseUInt32(unsigned &Val);
254     bool ParseUInt32(unsigned &Val, LocTy &Loc) {
255       Loc = Lex.getLoc();
256       return ParseUInt32(Val);
257     }
258     bool ParseUInt64(uint64_t &Val);
259     bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
260       Loc = Lex.getLoc();
261       return ParseUInt64(Val);
262     }
263     bool ParseFlag(unsigned &Val);
264
265     bool ParseStringAttribute(AttrBuilder &B);
266
267     bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
268     bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
269     bool ParseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr);
270     bool ParseOptionalAddrSpace(unsigned &AddrSpace);
271     bool ParseOptionalParamAttrs(AttrBuilder &B);
272     bool ParseOptionalReturnAttrs(AttrBuilder &B);
273     bool ParseOptionalLinkage(unsigned &Res, bool &HasLinkage,
274                               unsigned &Visibility, unsigned &DLLStorageClass,
275                               bool &DSOLocal);
276     void ParseOptionalDSOLocal(bool &DSOLocal);
277     void ParseOptionalVisibility(unsigned &Res);
278     void ParseOptionalDLLStorageClass(unsigned &Res);
279     bool ParseOptionalCallingConv(unsigned &CC);
280     bool ParseOptionalAlignment(unsigned &Alignment);
281     bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
282     bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
283                                AtomicOrdering &Ordering);
284     bool ParseScope(SyncScope::ID &SSID);
285     bool ParseOrdering(AtomicOrdering &Ordering);
286     bool ParseOptionalStackAlignment(unsigned &Alignment);
287     bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
288     bool ParseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
289                                      bool &AteExtraComma);
290     bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
291     bool parseAllocSizeArguments(unsigned &BaseSizeArg,
292                                  Optional<unsigned> &HowManyArg);
293     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,
294                         bool &AteExtraComma);
295     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
296       bool AteExtraComma;
297       if (ParseIndexList(Indices, AteExtraComma)) return true;
298       if (AteExtraComma)
299         return TokError("expected index");
300       return false;
301     }
302
303     // Top-Level Entities
304     bool ParseTopLevelEntities();
305     bool ValidateEndOfModule();
306     bool ValidateEndOfIndex();
307     bool ParseTargetDefinition();
308     bool ParseModuleAsm();
309     bool ParseSourceFileName();
310     bool ParseDepLibs();        // FIXME: Remove in 4.0.
311     bool ParseUnnamedType();
312     bool ParseNamedType();
313     bool ParseDeclare();
314     bool ParseDefine();
315
316     bool ParseGlobalType(bool &IsConstant);
317     bool ParseUnnamedGlobal();
318     bool ParseNamedGlobal();
319     bool ParseGlobal(const std::string &Name, LocTy NameLoc, unsigned Linkage,
320                      bool HasLinkage, unsigned Visibility,
321                      unsigned DLLStorageClass, bool DSOLocal,
322                      GlobalVariable::ThreadLocalMode TLM,
323                      GlobalVariable::UnnamedAddr UnnamedAddr);
324     bool parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
325                              unsigned L, unsigned Visibility,
326                              unsigned DLLStorageClass, bool DSOLocal,
327                              GlobalVariable::ThreadLocalMode TLM,
328                              GlobalVariable::UnnamedAddr UnnamedAddr);
329     bool parseComdat();
330     bool ParseStandaloneMetadata();
331     bool ParseNamedMetadata();
332     bool ParseMDString(MDString *&Result);
333     bool ParseMDNodeID(MDNode *&Result);
334     bool ParseUnnamedAttrGrp();
335     bool ParseFnAttributeValuePairs(AttrBuilder &B,
336                                     std::vector<unsigned> &FwdRefAttrGrps,
337                                     bool inAttrGrp, LocTy &BuiltinLoc);
338
339     // Module Summary Index Parsing.
340     bool SkipModuleSummaryEntry();
341     bool ParseSummaryEntry();
342     bool ParseModuleEntry(unsigned ID);
343     bool ParseModuleReference(StringRef &ModulePath);
344     bool ParseGVReference(ValueInfo &VI, unsigned &GVId);
345     bool ParseGVEntry(unsigned ID);
346     bool ParseFunctionSummary(std::string Name, GlobalValue::GUID, unsigned ID);
347     bool ParseVariableSummary(std::string Name, GlobalValue::GUID, unsigned ID);
348     bool ParseAliasSummary(std::string Name, GlobalValue::GUID, unsigned ID);
349     bool ParseGVFlags(GlobalValueSummary::GVFlags &GVFlags);
350     bool ParseOptionalFFlags(FunctionSummary::FFlags &FFlags);
351     bool ParseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls);
352     bool ParseHotness(CalleeInfo::HotnessType &Hotness);
353     bool ParseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo);
354     bool ParseTypeTests(std::vector<GlobalValue::GUID> &TypeTests);
355     bool ParseVFuncIdList(lltok::Kind Kind,
356                           std::vector<FunctionSummary::VFuncId> &VFuncIdList);
357     bool ParseConstVCallList(
358         lltok::Kind Kind,
359         std::vector<FunctionSummary::ConstVCall> &ConstVCallList);
360     using IdToIndexMapType =
361         std::map<unsigned, std::vector<std::pair<unsigned, LocTy>>>;
362     bool ParseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
363                          IdToIndexMapType &IdToIndexMap, unsigned Index);
364     bool ParseVFuncId(FunctionSummary::VFuncId &VFuncId,
365                       IdToIndexMapType &IdToIndexMap, unsigned Index);
366     bool ParseOptionalRefs(std::vector<ValueInfo> &Refs);
367     bool ParseTypeIdEntry(unsigned ID);
368     bool ParseTypeIdSummary(TypeIdSummary &TIS);
369     bool ParseTypeTestResolution(TypeTestResolution &TTRes);
370     bool ParseOptionalWpdResolutions(
371         std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap);
372     bool ParseWpdRes(WholeProgramDevirtResolution &WPDRes);
373     bool ParseOptionalResByArg(
374         std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
375             &ResByArg);
376     bool ParseArgs(std::vector<uint64_t> &Args);
377     void AddGlobalValueToIndex(std::string Name, GlobalValue::GUID,
378                                GlobalValue::LinkageTypes Linkage, unsigned ID,
379                                std::unique_ptr<GlobalValueSummary> Summary);
380
381     // Type Parsing.
382     bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
383     bool ParseType(Type *&Result, bool AllowVoid = false) {
384       return ParseType(Result, "expected type", AllowVoid);
385     }
386     bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
387                    bool AllowVoid = false) {
388       Loc = Lex.getLoc();
389       return ParseType(Result, Msg, AllowVoid);
390     }
391     bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
392       Loc = Lex.getLoc();
393       return ParseType(Result, AllowVoid);
394     }
395     bool ParseAnonStructType(Type *&Result, bool Packed);
396     bool ParseStructBody(SmallVectorImpl<Type*> &Body);
397     bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
398                                std::pair<Type*, LocTy> &Entry,
399                                Type *&ResultTy);
400
401     bool ParseArrayVectorType(Type *&Result, bool isVector);
402     bool ParseFunctionType(Type *&Result);
403
404     // Function Semantic Analysis.
405     class PerFunctionState {
406       LLParser &P;
407       Function &F;
408       std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
409       std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
410       std::vector<Value*> NumberedVals;
411
412       /// FunctionNumber - If this is an unnamed function, this is the slot
413       /// number of it, otherwise it is -1.
414       int FunctionNumber;
415     public:
416       PerFunctionState(LLParser &p, Function &f, int functionNumber);
417       ~PerFunctionState();
418
419       Function &getFunction() const { return F; }
420
421       bool FinishFunction();
422
423       /// GetVal - Get a value with the specified name or ID, creating a
424       /// forward reference record if needed.  This can return null if the value
425       /// exists but does not have the right type.
426       Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc, bool IsCall);
427       Value *GetVal(unsigned ID, Type *Ty, LocTy Loc, bool IsCall);
428
429       /// SetInstName - After an instruction is parsed and inserted into its
430       /// basic block, this installs its name.
431       bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
432                        Instruction *Inst);
433
434       /// GetBB - Get a basic block with the specified name or ID, creating a
435       /// forward reference record if needed.  This can return null if the value
436       /// is not a BasicBlock.
437       BasicBlock *GetBB(const std::string &Name, LocTy Loc);
438       BasicBlock *GetBB(unsigned ID, LocTy Loc);
439
440       /// DefineBB - Define the specified basic block, which is either named or
441       /// unnamed.  If there is an error, this returns null otherwise it returns
442       /// the block being defined.
443       BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
444
445       bool resolveForwardRefBlockAddresses();
446     };
447
448     bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
449                              PerFunctionState *PFS, bool IsCall);
450
451     bool parseConstantValue(Type *Ty, Constant *&C);
452     bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
453     bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
454       return ParseValue(Ty, V, &PFS);
455     }
456
457     bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
458                     PerFunctionState &PFS) {
459       Loc = Lex.getLoc();
460       return ParseValue(Ty, V, &PFS);
461     }
462
463     bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
464     bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
465       return ParseTypeAndValue(V, &PFS);
466     }
467     bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
468       Loc = Lex.getLoc();
469       return ParseTypeAndValue(V, PFS);
470     }
471     bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
472                                 PerFunctionState &PFS);
473     bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
474       LocTy Loc;
475       return ParseTypeAndBasicBlock(BB, Loc, PFS);
476     }
477
478
479     struct ParamInfo {
480       LocTy Loc;
481       Value *V;
482       AttributeSet Attrs;
483       ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
484           : Loc(loc), V(v), Attrs(attrs) {}
485     };
486     bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
487                             PerFunctionState &PFS,
488                             bool IsMustTailCall = false,
489                             bool InVarArgsFunc = false);
490
491     bool
492     ParseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList,
493                                 PerFunctionState &PFS);
494
495     bool ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
496                             PerFunctionState &PFS);
497
498     // Constant Parsing.
499     bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
500     bool ParseGlobalValue(Type *Ty, Constant *&C);
501     bool ParseGlobalTypeAndValue(Constant *&V);
502     bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
503                                 Optional<unsigned> *InRangeOp = nullptr);
504     bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
505     bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
506     bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
507                               PerFunctionState *PFS);
508     bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
509     bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false);
510     bool ParseMDNode(MDNode *&N);
511     bool ParseMDNodeTail(MDNode *&N);
512     bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
513     bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD);
514     bool ParseInstructionMetadata(Instruction &Inst);
515     bool ParseGlobalObjectMetadataAttachment(GlobalObject &GO);
516     bool ParseOptionalFunctionMetadata(Function &F);
517
518     template <class FieldTy>
519     bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
520     template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result);
521     template <class ParserTy>
522     bool ParseMDFieldsImplBody(ParserTy parseField);
523     template <class ParserTy>
524     bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc);
525     bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
526
527 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS)                                  \
528   bool Parse##CLASS(MDNode *&Result, bool IsDistinct);
529 #include "llvm/IR/Metadata.def"
530
531     // Function Parsing.
532     struct ArgInfo {
533       LocTy Loc;
534       Type *Ty;
535       AttributeSet Attrs;
536       std::string Name;
537       ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
538           : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
539     };
540     bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
541     bool ParseFunctionHeader(Function *&Fn, bool isDefine);
542     bool ParseFunctionBody(Function &Fn);
543     bool ParseBasicBlock(PerFunctionState &PFS);
544
545     enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
546
547     // Instruction Parsing.  Each instruction parsing routine can return with a
548     // normal result, an error result, or return having eaten an extra comma.
549     enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
550     int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
551                          PerFunctionState &PFS);
552     bool ParseCmpPredicate(unsigned &P, unsigned Opc);
553
554     bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
555     bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
556     bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
557     bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
558     bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
559     bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
560     bool ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS);
561     bool ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS);
562     bool ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS);
563     bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
564     bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
565
566     bool ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc,
567                          unsigned OperandType);
568     bool ParseLogical(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
569     bool ParseCompare(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
570     bool ParseCast(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
571     bool ParseSelect(Instruction *&Inst, PerFunctionState &PFS);
572     bool ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS);
573     bool ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS);
574     bool ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS);
575     bool ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS);
576     int ParsePHI(Instruction *&Inst, PerFunctionState &PFS);
577     bool ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS);
578     bool ParseCall(Instruction *&Inst, PerFunctionState &PFS,
579                    CallInst::TailCallKind TCK);
580     int ParseAlloc(Instruction *&Inst, PerFunctionState &PFS);
581     int ParseLoad(Instruction *&Inst, PerFunctionState &PFS);
582     int ParseStore(Instruction *&Inst, PerFunctionState &PFS);
583     int ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS);
584     int ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS);
585     int ParseFence(Instruction *&Inst, PerFunctionState &PFS);
586     int ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS);
587     int ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS);
588     int ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS);
589
590     // Use-list order directives.
591     bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
592     bool ParseUseListOrderBB();
593     bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
594     bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
595   };
596 } // End llvm namespace
597
598 #endif