Bug Summary

File:lib/CodeGen/MIRParser/MIParser.cpp
Warning:line 1548, column 56
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MIParser.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-eagerly-assume -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-7/lib/clang/7.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/lib/CodeGen/MIRParser -I /build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser -I /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/include -I /build/llvm-toolchain-snapshot-7~svn329677/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/x86_64-linux-gnu/c++/7.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/7.3.0/../../../../include/c++/7.3.0/backward -internal-isystem /usr/include/clang/7.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-7/lib/clang/7.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-7~svn329677/build-llvm/lib/CodeGen/MIRParser -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2018-04-11-031539-24776-1 -x c++ /build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp
1//===- MIParser.cpp - Machine instructions parser implementation ----------===//
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 implements the parsing of machine instructions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MIParser.h"
15#include "MILexer.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/StringRef.h"
25#include "llvm/ADT/StringSwitch.h"
26#include "llvm/ADT/Twine.h"
27#include "llvm/AsmParser/Parser.h"
28#include "llvm/AsmParser/SlotMapping.h"
29#include "llvm/CodeGen/MIRPrinter.h"
30#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineFrameInfo.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineInstr.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineMemOperand.h"
36#include "llvm/CodeGen/MachineOperand.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
38#include "llvm/CodeGen/TargetInstrInfo.h"
39#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
41#include "llvm/IR/BasicBlock.h"
42#include "llvm/IR/Constants.h"
43#include "llvm/IR/DataLayout.h"
44#include "llvm/IR/DebugInfoMetadata.h"
45#include "llvm/IR/DebugLoc.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/InstrTypes.h"
48#include "llvm/IR/Instructions.h"
49#include "llvm/IR/Intrinsics.h"
50#include "llvm/IR/Metadata.h"
51#include "llvm/IR/Module.h"
52#include "llvm/IR/ModuleSlotTracker.h"
53#include "llvm/IR/Type.h"
54#include "llvm/IR/Value.h"
55#include "llvm/IR/ValueSymbolTable.h"
56#include "llvm/MC/LaneBitmask.h"
57#include "llvm/MC/MCDwarf.h"
58#include "llvm/MC/MCInstrDesc.h"
59#include "llvm/MC/MCRegisterInfo.h"
60#include "llvm/Support/AtomicOrdering.h"
61#include "llvm/Support/BranchProbability.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
64#include "llvm/Support/LowLevelTypeImpl.h"
65#include "llvm/Support/MemoryBuffer.h"
66#include "llvm/Support/SMLoc.h"
67#include "llvm/Support/SourceMgr.h"
68#include "llvm/Support/raw_ostream.h"
69#include "llvm/Target/TargetIntrinsicInfo.h"
70#include "llvm/Target/TargetMachine.h"
71#include <algorithm>
72#include <cassert>
73#include <cctype>
74#include <cstddef>
75#include <cstdint>
76#include <limits>
77#include <string>
78#include <utility>
79
80using namespace llvm;
81
82PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
83 SourceMgr &SM, const SlotMapping &IRSlots,
84 const Name2RegClassMap &Names2RegClasses,
85 const Name2RegBankMap &Names2RegBanks)
86 : MF(MF), SM(&SM), IRSlots(IRSlots), Names2RegClasses(Names2RegClasses),
87 Names2RegBanks(Names2RegBanks) {
88}
89
90VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
91 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
92 if (I.second) {
93 MachineRegisterInfo &MRI = MF.getRegInfo();
94 VRegInfo *Info = new (Allocator) VRegInfo;
95 Info->VReg = MRI.createIncompleteVirtualRegister();
96 I.first->second = Info;
97 }
98 return *I.first->second;
99}
100
101VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) {
102 assert(RegName != "" && "Expected named reg.")(static_cast <bool> (RegName != "" && "Expected named reg."
) ? void (0) : __assert_fail ("RegName != \"\" && \"Expected named reg.\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 102, __extension__ __PRETTY_FUNCTION__))
;
103
104 auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
105 if (I.second) {
106 VRegInfo *Info = new (Allocator) VRegInfo;
107 Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
108 I.first->second = Info;
109 }
110 return *I.first->second;
111}
112
113namespace {
114
115/// A wrapper struct around the 'MachineOperand' struct that includes a source
116/// range and other attributes.
117struct ParsedMachineOperand {
118 MachineOperand Operand;
119 StringRef::iterator Begin;
120 StringRef::iterator End;
121 Optional<unsigned> TiedDefIdx;
122
123 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
124 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
125 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
126 if (TiedDefIdx)
127 assert(Operand.isReg() && Operand.isUse() &&(static_cast <bool> (Operand.isReg() && Operand
.isUse() && "Only used register operands can be tied"
) ? void (0) : __assert_fail ("Operand.isReg() && Operand.isUse() && \"Only used register operands can be tied\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 128, __extension__ __PRETTY_FUNCTION__))
128 "Only used register operands can be tied")(static_cast <bool> (Operand.isReg() && Operand
.isUse() && "Only used register operands can be tied"
) ? void (0) : __assert_fail ("Operand.isReg() && Operand.isUse() && \"Only used register operands can be tied\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 128, __extension__ __PRETTY_FUNCTION__))
;
129 }
130};
131
132class MIParser {
133 MachineFunction &MF;
134 SMDiagnostic &Error;
135 StringRef Source, CurrentSource;
136 MIToken Token;
137 PerFunctionMIParsingState &PFS;
138 /// Maps from instruction names to op codes.
139 StringMap<unsigned> Names2InstrOpCodes;
140 /// Maps from register names to registers.
141 StringMap<unsigned> Names2Regs;
142 /// Maps from register mask names to register masks.
143 StringMap<const uint32_t *> Names2RegMasks;
144 /// Maps from subregister names to subregister indices.
145 StringMap<unsigned> Names2SubRegIndices;
146 /// Maps from slot numbers to function's unnamed basic blocks.
147 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
148 /// Maps from slot numbers to function's unnamed values.
149 DenseMap<unsigned, const Value *> Slots2Values;
150 /// Maps from target index names to target indices.
151 StringMap<int> Names2TargetIndices;
152 /// Maps from direct target flag names to the direct target flag values.
153 StringMap<unsigned> Names2DirectTargetFlags;
154 /// Maps from direct target flag names to the bitmask target flag values.
155 StringMap<unsigned> Names2BitmaskTargetFlags;
156 /// Maps from MMO target flag names to MMO target flag values.
157 StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
158
159public:
160 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
161 StringRef Source);
162
163 /// \p SkipChar gives the number of characters to skip before looking
164 /// for the next token.
165 void lex(unsigned SkipChar = 0);
166
167 /// Report an error at the current location with the given message.
168 ///
169 /// This function always return true.
170 bool error(const Twine &Msg);
171
172 /// Report an error at the given location with the given message.
173 ///
174 /// This function always return true.
175 bool error(StringRef::iterator Loc, const Twine &Msg);
176
177 bool
178 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
179 bool parseBasicBlocks();
180 bool parse(MachineInstr *&MI);
181 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
182 bool parseStandaloneNamedRegister(unsigned &Reg);
183 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
184 bool parseStandaloneRegister(unsigned &Reg);
185 bool parseStandaloneStackObject(int &FI);
186 bool parseStandaloneMDNode(MDNode *&Node);
187
188 bool
189 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
190 bool parseBasicBlock(MachineBasicBlock &MBB,
191 MachineBasicBlock *&AddFalthroughFrom);
192 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
193 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
194
195 bool parseNamedRegister(unsigned &Reg);
196 bool parseVirtualRegister(VRegInfo *&Info);
197 bool parseNamedVirtualRegister(VRegInfo *&Info);
198 bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
199 bool parseRegisterFlag(unsigned &Flags);
200 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
201 bool parseSubRegisterIndex(unsigned &SubReg);
202 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
203 bool parseRegisterOperand(MachineOperand &Dest,
204 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
205 bool parseImmediateOperand(MachineOperand &Dest);
206 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
207 const Constant *&C);
208 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
209 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
210 bool parseTypedImmediateOperand(MachineOperand &Dest);
211 bool parseFPImmediateOperand(MachineOperand &Dest);
212 bool parseMBBReference(MachineBasicBlock *&MBB);
213 bool parseMBBOperand(MachineOperand &Dest);
214 bool parseStackFrameIndex(int &FI);
215 bool parseStackObjectOperand(MachineOperand &Dest);
216 bool parseFixedStackFrameIndex(int &FI);
217 bool parseFixedStackObjectOperand(MachineOperand &Dest);
218 bool parseGlobalValue(GlobalValue *&GV);
219 bool parseGlobalAddressOperand(MachineOperand &Dest);
220 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
221 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
222 bool parseJumpTableIndexOperand(MachineOperand &Dest);
223 bool parseExternalSymbolOperand(MachineOperand &Dest);
224 bool parseMDNode(MDNode *&Node);
225 bool parseDIExpression(MDNode *&Node);
226 bool parseMetadataOperand(MachineOperand &Dest);
227 bool parseCFIOffset(int &Offset);
228 bool parseCFIRegister(unsigned &Reg);
229 bool parseCFIEscapeValues(std::string& Values);
230 bool parseCFIOperand(MachineOperand &Dest);
231 bool parseIRBlock(BasicBlock *&BB, const Function &F);
232 bool parseBlockAddressOperand(MachineOperand &Dest);
233 bool parseIntrinsicOperand(MachineOperand &Dest);
234 bool parsePredicateOperand(MachineOperand &Dest);
235 bool parseTargetIndexOperand(MachineOperand &Dest);
236 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
237 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
238 bool parseMachineOperand(MachineOperand &Dest,
239 Optional<unsigned> &TiedDefIdx);
240 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
241 Optional<unsigned> &TiedDefIdx);
242 bool parseOffset(int64_t &Offset);
243 bool parseAlignment(unsigned &Alignment);
244 bool parseAddrspace(unsigned &Addrspace);
245 bool parseOperandsOffset(MachineOperand &Op);
246 bool parseIRValue(const Value *&V);
247 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
248 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
249 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
250 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
251 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
252 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
253
254private:
255 /// Convert the integer literal in the current token into an unsigned integer.
256 ///
257 /// Return true if an error occurred.
258 bool getUnsigned(unsigned &Result);
259
260 /// Convert the integer literal in the current token into an uint64.
261 ///
262 /// Return true if an error occurred.
263 bool getUint64(uint64_t &Result);
264
265 /// Convert the hexadecimal literal in the current token into an unsigned
266 /// APInt with a minimum bitwidth required to represent the value.
267 ///
268 /// Return true if the literal does not represent an integer value.
269 bool getHexUint(APInt &Result);
270
271 /// If the current token is of the given kind, consume it and return false.
272 /// Otherwise report an error and return true.
273 bool expectAndConsume(MIToken::TokenKind TokenKind);
274
275 /// If the current token is of the given kind, consume it and return true.
276 /// Otherwise return false.
277 bool consumeIfPresent(MIToken::TokenKind TokenKind);
278
279 void initNames2InstrOpCodes();
280
281 /// Try to convert an instruction name to an opcode. Return true if the
282 /// instruction name is invalid.
283 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
284
285 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
286
287 bool assignRegisterTies(MachineInstr &MI,
288 ArrayRef<ParsedMachineOperand> Operands);
289
290 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
291 const MCInstrDesc &MCID);
292
293 void initNames2Regs();
294
295 /// Try to convert a register name to a register number. Return true if the
296 /// register name is invalid.
297 bool getRegisterByName(StringRef RegName, unsigned &Reg);
298
299 void initNames2RegMasks();
300
301 /// Check if the given identifier is a name of a register mask.
302 ///
303 /// Return null if the identifier isn't a register mask.
304 const uint32_t *getRegMask(StringRef Identifier);
305
306 void initNames2SubRegIndices();
307
308 /// Check if the given identifier is a name of a subregister index.
309 ///
310 /// Return 0 if the name isn't a subregister index class.
311 unsigned getSubRegIndex(StringRef Name);
312
313 const BasicBlock *getIRBlock(unsigned Slot);
314 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
315
316 const Value *getIRValue(unsigned Slot);
317
318 void initNames2TargetIndices();
319
320 /// Try to convert a name of target index to the corresponding target index.
321 ///
322 /// Return true if the name isn't a name of a target index.
323 bool getTargetIndex(StringRef Name, int &Index);
324
325 void initNames2DirectTargetFlags();
326
327 /// Try to convert a name of a direct target flag to the corresponding
328 /// target flag.
329 ///
330 /// Return true if the name isn't a name of a direct flag.
331 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
332
333 void initNames2BitmaskTargetFlags();
334
335 /// Try to convert a name of a bitmask target flag to the corresponding
336 /// target flag.
337 ///
338 /// Return true if the name isn't a name of a bitmask target flag.
339 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
340
341 void initNames2MMOTargetFlags();
342
343 /// Try to convert a name of a MachineMemOperand target flag to the
344 /// corresponding target flag.
345 ///
346 /// Return true if the name isn't a name of a target MMO flag.
347 bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag);
348
349 /// parseStringConstant
350 /// ::= StringConstant
351 bool parseStringConstant(std::string &Result);
352};
353
354} // end anonymous namespace
355
356MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
357 StringRef Source)
358 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
359{}
360
361void MIParser::lex(unsigned SkipChar) {
362 CurrentSource = lexMIToken(
363 CurrentSource.data() + SkipChar, Token,
364 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
365}
366
367bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
368
369bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
370 const SourceMgr &SM = *PFS.SM;
371 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()))(static_cast <bool> (Loc >= Source.data() &&
Loc <= (Source.data() + Source.size())) ? void (0) : __assert_fail
("Loc >= Source.data() && Loc <= (Source.data() + Source.size())"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 371, __extension__ __PRETTY_FUNCTION__))
;
372 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
373 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
374 // Create an ordinary diagnostic when the source manager's buffer is the
375 // source string.
376 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
377 return true;
378 }
379 // Create a diagnostic for a YAML string literal.
380 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
381 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
382 Source, None, None);
383 return true;
384}
385
386static const char *toString(MIToken::TokenKind TokenKind) {
387 switch (TokenKind) {
388 case MIToken::comma:
389 return "','";
390 case MIToken::equal:
391 return "'='";
392 case MIToken::colon:
393 return "':'";
394 case MIToken::lparen:
395 return "'('";
396 case MIToken::rparen:
397 return "')'";
398 default:
399 return "<unknown token>";
400 }
401}
402
403bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
404 if (Token.isNot(TokenKind))
405 return error(Twine("expected ") + toString(TokenKind));
406 lex();
407 return false;
408}
409
410bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
411 if (Token.isNot(TokenKind))
412 return false;
413 lex();
414 return true;
415}
416
417bool MIParser::parseBasicBlockDefinition(
418 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
419 assert(Token.is(MIToken::MachineBasicBlockLabel))(static_cast <bool> (Token.is(MIToken::MachineBasicBlockLabel
)) ? void (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 419, __extension__ __PRETTY_FUNCTION__))
;
420 unsigned ID = 0;
421 if (getUnsigned(ID))
422 return true;
423 auto Loc = Token.location();
424 auto Name = Token.stringValue();
425 lex();
426 bool HasAddressTaken = false;
427 bool IsLandingPad = false;
428 unsigned Alignment = 0;
429 BasicBlock *BB = nullptr;
430 if (consumeIfPresent(MIToken::lparen)) {
431 do {
432 // TODO: Report an error when multiple same attributes are specified.
433 switch (Token.kind()) {
434 case MIToken::kw_address_taken:
435 HasAddressTaken = true;
436 lex();
437 break;
438 case MIToken::kw_landing_pad:
439 IsLandingPad = true;
440 lex();
441 break;
442 case MIToken::kw_align:
443 if (parseAlignment(Alignment))
444 return true;
445 break;
446 case MIToken::IRBlock:
447 // TODO: Report an error when both name and ir block are specified.
448 if (parseIRBlock(BB, MF.getFunction()))
449 return true;
450 lex();
451 break;
452 default:
453 break;
454 }
455 } while (consumeIfPresent(MIToken::comma));
456 if (expectAndConsume(MIToken::rparen))
457 return true;
458 }
459 if (expectAndConsume(MIToken::colon))
460 return true;
461
462 if (!Name.empty()) {
463 BB = dyn_cast_or_null<BasicBlock>(
464 MF.getFunction().getValueSymbolTable()->lookup(Name));
465 if (!BB)
466 return error(Loc, Twine("basic block '") + Name +
467 "' is not defined in the function '" +
468 MF.getName() + "'");
469 }
470 auto *MBB = MF.CreateMachineBasicBlock(BB);
471 MF.insert(MF.end(), MBB);
472 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
473 if (!WasInserted)
474 return error(Loc, Twine("redefinition of machine basic block with id #") +
475 Twine(ID));
476 if (Alignment)
477 MBB->setAlignment(Alignment);
478 if (HasAddressTaken)
479 MBB->setHasAddressTaken();
480 MBB->setIsEHPad(IsLandingPad);
481 return false;
482}
483
484bool MIParser::parseBasicBlockDefinitions(
485 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
486 lex();
487 // Skip until the first machine basic block.
488 while (Token.is(MIToken::Newline))
489 lex();
490 if (Token.isErrorOrEOF())
491 return Token.isError();
492 if (Token.isNot(MIToken::MachineBasicBlockLabel))
493 return error("expected a basic block definition before instructions");
494 unsigned BraceDepth = 0;
495 do {
496 if (parseBasicBlockDefinition(MBBSlots))
497 return true;
498 bool IsAfterNewline = false;
499 // Skip until the next machine basic block.
500 while (true) {
501 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
502 Token.isErrorOrEOF())
503 break;
504 else if (Token.is(MIToken::MachineBasicBlockLabel))
505 return error("basic block definition should be located at the start of "
506 "the line");
507 else if (consumeIfPresent(MIToken::Newline)) {
508 IsAfterNewline = true;
509 continue;
510 }
511 IsAfterNewline = false;
512 if (Token.is(MIToken::lbrace))
513 ++BraceDepth;
514 if (Token.is(MIToken::rbrace)) {
515 if (!BraceDepth)
516 return error("extraneous closing brace ('}')");
517 --BraceDepth;
518 }
519 lex();
520 }
521 // Verify that we closed all of the '{' at the end of a file or a block.
522 if (!Token.isError() && BraceDepth)
523 return error("expected '}'"); // FIXME: Report a note that shows '{'.
524 } while (!Token.isErrorOrEOF());
525 return Token.isError();
526}
527
528bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
529 assert(Token.is(MIToken::kw_liveins))(static_cast <bool> (Token.is(MIToken::kw_liveins)) ? void
(0) : __assert_fail ("Token.is(MIToken::kw_liveins)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 529, __extension__ __PRETTY_FUNCTION__))
;
530 lex();
531 if (expectAndConsume(MIToken::colon))
532 return true;
533 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
534 return false;
535 do {
536 if (Token.isNot(MIToken::NamedRegister))
537 return error("expected a named register");
538 unsigned Reg = 0;
539 if (parseNamedRegister(Reg))
540 return true;
541 lex();
542 LaneBitmask Mask = LaneBitmask::getAll();
543 if (consumeIfPresent(MIToken::colon)) {
544 // Parse lane mask.
545 if (Token.isNot(MIToken::IntegerLiteral) &&
546 Token.isNot(MIToken::HexLiteral))
547 return error("expected a lane mask");
548 static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
549 "Use correct get-function for lane mask");
550 LaneBitmask::Type V;
551 if (getUnsigned(V))
552 return error("invalid lane mask value");
553 Mask = LaneBitmask(V);
554 lex();
555 }
556 MBB.addLiveIn(Reg, Mask);
557 } while (consumeIfPresent(MIToken::comma));
558 return false;
559}
560
561bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
562 assert(Token.is(MIToken::kw_successors))(static_cast <bool> (Token.is(MIToken::kw_successors)) ?
void (0) : __assert_fail ("Token.is(MIToken::kw_successors)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 562, __extension__ __PRETTY_FUNCTION__))
;
563 lex();
564 if (expectAndConsume(MIToken::colon))
565 return true;
566 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
567 return false;
568 do {
569 if (Token.isNot(MIToken::MachineBasicBlock))
570 return error("expected a machine basic block reference");
571 MachineBasicBlock *SuccMBB = nullptr;
572 if (parseMBBReference(SuccMBB))
573 return true;
574 lex();
575 unsigned Weight = 0;
576 if (consumeIfPresent(MIToken::lparen)) {
577 if (Token.isNot(MIToken::IntegerLiteral) &&
578 Token.isNot(MIToken::HexLiteral))
579 return error("expected an integer literal after '('");
580 if (getUnsigned(Weight))
581 return true;
582 lex();
583 if (expectAndConsume(MIToken::rparen))
584 return true;
585 }
586 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
587 } while (consumeIfPresent(MIToken::comma));
588 MBB.normalizeSuccProbs();
589 return false;
590}
591
592bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
593 MachineBasicBlock *&AddFalthroughFrom) {
594 // Skip the definition.
595 assert(Token.is(MIToken::MachineBasicBlockLabel))(static_cast <bool> (Token.is(MIToken::MachineBasicBlockLabel
)) ? void (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 595, __extension__ __PRETTY_FUNCTION__))
;
596 lex();
597 if (consumeIfPresent(MIToken::lparen)) {
7
Taking false branch
598 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
599 lex();
600 consumeIfPresent(MIToken::rparen);
601 }
602 consumeIfPresent(MIToken::colon);
603
604 // Parse the liveins and successors.
605 // N.B: Multiple lists of successors and liveins are allowed and they're
606 // merged into one.
607 // Example:
608 // liveins: %edi
609 // liveins: %esi
610 //
611 // is equivalent to
612 // liveins: %edi, %esi
613 bool ExplicitSuccessors = false;
614 while (true) {
8
Loop condition is true. Entering loop body
615 if (Token.is(MIToken::kw_successors)) {
9
Taking false branch
616 if (parseBasicBlockSuccessors(MBB))
617 return true;
618 ExplicitSuccessors = true;
619 } else if (Token.is(MIToken::kw_liveins)) {
10
Taking false branch
620 if (parseBasicBlockLiveins(MBB))
621 return true;
622 } else if (consumeIfPresent(MIToken::Newline)) {
11
Taking false branch
623 continue;
624 } else
625 break;
12
Execution continues on line 632
626 if (!Token.isNewlineOrEOF())
627 return error("expected line break at the end of a list");
628 lex();
629 }
630
631 // Parse the instructions.
632 bool IsInBundle = false;
633 MachineInstr *PrevMI = nullptr;
634 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
13
Loop condition is true. Entering loop body
635 !Token.is(MIToken::Eof)) {
636 if (consumeIfPresent(MIToken::Newline))
14
Taking false branch
637 continue;
638 if (consumeIfPresent(MIToken::rbrace)) {
15
Taking false branch
639 // The first parsing pass should verify that all closing '}' have an
640 // opening '{'.
641 assert(IsInBundle)(static_cast <bool> (IsInBundle) ? void (0) : __assert_fail
("IsInBundle", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 641, __extension__ __PRETTY_FUNCTION__))
;
642 IsInBundle = false;
643 continue;
644 }
645 MachineInstr *MI = nullptr;
646 if (parse(MI))
16
Calling 'MIParser::parse'
647 return true;
648 MBB.insert(MBB.end(), MI);
649 if (IsInBundle) {
650 PrevMI->setFlag(MachineInstr::BundledSucc);
651 MI->setFlag(MachineInstr::BundledPred);
652 }
653 PrevMI = MI;
654 if (Token.is(MIToken::lbrace)) {
655 if (IsInBundle)
656 return error("nested instruction bundles are not allowed");
657 lex();
658 // This instruction is the start of the bundle.
659 MI->setFlag(MachineInstr::BundledSucc);
660 IsInBundle = true;
661 if (!Token.is(MIToken::Newline))
662 // The next instruction can be on the same line.
663 continue;
664 }
665 assert(Token.isNewlineOrEOF() && "MI is not fully parsed")(static_cast <bool> (Token.isNewlineOrEOF() && "MI is not fully parsed"
) ? void (0) : __assert_fail ("Token.isNewlineOrEOF() && \"MI is not fully parsed\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 665, __extension__ __PRETTY_FUNCTION__))
;
666 lex();
667 }
668
669 // Construct successor list by searching for basic block machine operands.
670 if (!ExplicitSuccessors) {
671 SmallVector<MachineBasicBlock*,4> Successors;
672 bool IsFallthrough;
673 guessSuccessors(MBB, Successors, IsFallthrough);
674 for (MachineBasicBlock *Succ : Successors)
675 MBB.addSuccessor(Succ);
676
677 if (IsFallthrough) {
678 AddFalthroughFrom = &MBB;
679 } else {
680 MBB.normalizeSuccProbs();
681 }
682 }
683
684 return false;
685}
686
687bool MIParser::parseBasicBlocks() {
688 lex();
689 // Skip until the first machine basic block.
690 while (Token.is(MIToken::Newline))
2
Loop condition is false. Execution continues on line 692
691 lex();
692 if (Token.isErrorOrEOF())
3
Taking false branch
693 return Token.isError();
694 // The first parsing pass should have verified that this token is a MBB label
695 // in the 'parseBasicBlockDefinitions' method.
696 assert(Token.is(MIToken::MachineBasicBlockLabel))(static_cast <bool> (Token.is(MIToken::MachineBasicBlockLabel
)) ? void (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 696, __extension__ __PRETTY_FUNCTION__))
;
697 MachineBasicBlock *AddFalthroughFrom = nullptr;
698 do {
699 MachineBasicBlock *MBB = nullptr;
700 if (parseMBBReference(MBB))
4
Taking false branch
701 return true;
702 if (AddFalthroughFrom) {
5
Taking false branch
703 if (!AddFalthroughFrom->isSuccessor(MBB))
704 AddFalthroughFrom->addSuccessor(MBB);
705 AddFalthroughFrom->normalizeSuccProbs();
706 AddFalthroughFrom = nullptr;
707 }
708 if (parseBasicBlock(*MBB, AddFalthroughFrom))
6
Calling 'MIParser::parseBasicBlock'
709 return true;
710 // The method 'parseBasicBlock' should parse the whole block until the next
711 // block or the end of file.
712 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))(static_cast <bool> (Token.is(MIToken::MachineBasicBlockLabel
) || Token.is(MIToken::Eof)) ? void (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 712, __extension__ __PRETTY_FUNCTION__))
;
713 } while (Token.isNot(MIToken::Eof));
714 return false;
715}
716
717bool MIParser::parse(MachineInstr *&MI) {
718 // Parse any register operands before '='
719 MachineOperand MO = MachineOperand::CreateImm(0);
720 SmallVector<ParsedMachineOperand, 8> Operands;
721 while (Token.isRegister() || Token.isRegisterFlag()) {
17
Loop condition is false. Execution continues on line 732
722 auto Loc = Token.location();
723 Optional<unsigned> TiedDefIdx;
724 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
725 return true;
726 Operands.push_back(
727 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
728 if (Token.isNot(MIToken::comma))
729 break;
730 lex();
731 }
732 if (!Operands.empty() && expectAndConsume(MIToken::equal))
18
Taking false branch
733 return true;
734
735 unsigned OpCode, Flags = 0;
736 if (Token.isError() || parseInstruction(OpCode, Flags))
19
Taking false branch
737 return true;
738
739 // Parse the remaining machine operands.
740 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
741 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
742 auto Loc = Token.location();
743 Optional<unsigned> TiedDefIdx;
744 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
745 return true;
746 Operands.push_back(
747 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
748 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
749 Token.is(MIToken::lbrace))
750 break;
751 if (Token.isNot(MIToken::comma))
752 return error("expected ',' before the next machine operand");
753 lex();
754 }
755
756 DebugLoc DebugLocation;
757 if (Token.is(MIToken::kw_debug_location)) {
20
Taking true branch
758 lex();
759 if (Token.isNot(MIToken::exclaim))
21
Taking false branch
760 return error("expected a metadata node after 'debug-location'");
761 MDNode *Node = nullptr;
762 if (parseMDNode(Node))
22
Calling 'MIParser::parseMDNode'
763 return true;
764 DebugLocation = DebugLoc(Node);
765 }
766
767 // Parse the machine memory operands.
768 SmallVector<MachineMemOperand *, 2> MemOperands;
769 if (Token.is(MIToken::coloncolon)) {
770 lex();
771 while (!Token.isNewlineOrEOF()) {
772 MachineMemOperand *MemOp = nullptr;
773 if (parseMachineMemoryOperand(MemOp))
774 return true;
775 MemOperands.push_back(MemOp);
776 if (Token.isNewlineOrEOF())
777 break;
778 if (Token.isNot(MIToken::comma))
779 return error("expected ',' before the next machine memory operand");
780 lex();
781 }
782 }
783
784 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
785 if (!MCID.isVariadic()) {
786 // FIXME: Move the implicit operand verification to the machine verifier.
787 if (verifyImplicitOperands(Operands, MCID))
788 return true;
789 }
790
791 // TODO: Check for extraneous machine operands.
792 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
793 MI->setFlags(Flags);
794 for (const auto &Operand : Operands)
795 MI->addOperand(MF, Operand.Operand);
796 if (assignRegisterTies(*MI, Operands))
797 return true;
798 if (MemOperands.empty())
799 return false;
800 MachineInstr::mmo_iterator MemRefs =
801 MF.allocateMemRefsArray(MemOperands.size());
802 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
803 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
804 return false;
805}
806
807bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
808 lex();
809 if (Token.isNot(MIToken::MachineBasicBlock))
810 return error("expected a machine basic block reference");
811 if (parseMBBReference(MBB))
812 return true;
813 lex();
814 if (Token.isNot(MIToken::Eof))
815 return error(
816 "expected end of string after the machine basic block reference");
817 return false;
818}
819
820bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
821 lex();
822 if (Token.isNot(MIToken::NamedRegister))
823 return error("expected a named register");
824 if (parseNamedRegister(Reg))
825 return true;
826 lex();
827 if (Token.isNot(MIToken::Eof))
828 return error("expected end of string after the register reference");
829 return false;
830}
831
832bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
833 lex();
834 if (Token.isNot(MIToken::VirtualRegister))
835 return error("expected a virtual register");
836 if (parseVirtualRegister(Info))
837 return true;
838 lex();
839 if (Token.isNot(MIToken::Eof))
840 return error("expected end of string after the register reference");
841 return false;
842}
843
844bool MIParser::parseStandaloneRegister(unsigned &Reg) {
845 lex();
846 if (Token.isNot(MIToken::NamedRegister) &&
847 Token.isNot(MIToken::VirtualRegister))
848 return error("expected either a named or virtual register");
849
850 VRegInfo *Info;
851 if (parseRegister(Reg, Info))
852 return true;
853
854 lex();
855 if (Token.isNot(MIToken::Eof))
856 return error("expected end of string after the register reference");
857 return false;
858}
859
860bool MIParser::parseStandaloneStackObject(int &FI) {
861 lex();
862 if (Token.isNot(MIToken::StackObject))
863 return error("expected a stack object");
864 if (parseStackFrameIndex(FI))
865 return true;
866 if (Token.isNot(MIToken::Eof))
867 return error("expected end of string after the stack object reference");
868 return false;
869}
870
871bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
872 lex();
873 if (Token.is(MIToken::exclaim)) {
874 if (parseMDNode(Node))
875 return true;
876 } else if (Token.is(MIToken::md_diexpr)) {
877 if (parseDIExpression(Node))
878 return true;
879 } else
880 return error("expected a metadata node");
881 if (Token.isNot(MIToken::Eof))
882 return error("expected end of string after the metadata node");
883 return false;
884}
885
886static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
887 assert(MO.isImplicit())(static_cast <bool> (MO.isImplicit()) ? void (0) : __assert_fail
("MO.isImplicit()", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 887, __extension__ __PRETTY_FUNCTION__))
;
888 return MO.isDef() ? "implicit-def" : "implicit";
889}
890
891static std::string getRegisterName(const TargetRegisterInfo *TRI,
892 unsigned Reg) {
893 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg")(static_cast <bool> (TargetRegisterInfo::isPhysicalRegister
(Reg) && "expected phys reg") ? void (0) : __assert_fail
("TargetRegisterInfo::isPhysicalRegister(Reg) && \"expected phys reg\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 893, __extension__ __PRETTY_FUNCTION__))
;
894 return StringRef(TRI->getName(Reg)).lower();
895}
896
897/// Return true if the parsed machine operands contain a given machine operand.
898static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
899 ArrayRef<ParsedMachineOperand> Operands) {
900 for (const auto &I : Operands) {
901 if (ImplicitOperand.isIdenticalTo(I.Operand))
902 return true;
903 }
904 return false;
905}
906
907bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
908 const MCInstrDesc &MCID) {
909 if (MCID.isCall())
910 // We can't verify call instructions as they can contain arbitrary implicit
911 // register and register mask operands.
912 return false;
913
914 // Gather all the expected implicit operands.
915 SmallVector<MachineOperand, 4> ImplicitOperands;
916 if (MCID.ImplicitDefs)
917 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
918 ImplicitOperands.push_back(
919 MachineOperand::CreateReg(*ImpDefs, true, true));
920 if (MCID.ImplicitUses)
921 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
922 ImplicitOperands.push_back(
923 MachineOperand::CreateReg(*ImpUses, false, true));
924
925 const auto *TRI = MF.getSubtarget().getRegisterInfo();
926 assert(TRI && "Expected target register info")(static_cast <bool> (TRI && "Expected target register info"
) ? void (0) : __assert_fail ("TRI && \"Expected target register info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 926, __extension__ __PRETTY_FUNCTION__))
;
927 for (const auto &I : ImplicitOperands) {
928 if (isImplicitOperandIn(I, Operands))
929 continue;
930 return error(Operands.empty() ? Token.location() : Operands.back().End,
931 Twine("missing implicit register operand '") +
932 printImplicitRegisterFlag(I) + " %" +
933 getRegisterName(TRI, I.getReg()) + "'");
934 }
935 return false;
936}
937
938bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
939 // Allow both:
940 // * frame-setup frame-destroy OPCODE
941 // * frame-destroy frame-setup OPCODE
942 while (Token.is(MIToken::kw_frame_setup) ||
943 Token.is(MIToken::kw_frame_destroy)) {
944 Flags |= Token.is(MIToken::kw_frame_setup) ? MachineInstr::FrameSetup
945 : MachineInstr::FrameDestroy;
946 lex();
947 }
948 if (Token.isNot(MIToken::Identifier))
949 return error("expected a machine instruction");
950 StringRef InstrName = Token.stringValue();
951 if (parseInstrName(InstrName, OpCode))
952 return error(Twine("unknown machine instruction name '") + InstrName + "'");
953 lex();
954 return false;
955}
956
957bool MIParser::parseNamedRegister(unsigned &Reg) {
958 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token")(static_cast <bool> (Token.is(MIToken::NamedRegister) &&
"Needs NamedRegister token") ? void (0) : __assert_fail ("Token.is(MIToken::NamedRegister) && \"Needs NamedRegister token\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 958, __extension__ __PRETTY_FUNCTION__))
;
959 StringRef Name = Token.stringValue();
960 if (getRegisterByName(Name, Reg))
961 return error(Twine("unknown register name '") + Name + "'");
962 return false;
963}
964
965bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
966 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token")(static_cast <bool> (Token.is(MIToken::NamedVirtualRegister
) && "Expected NamedVReg token") ? void (0) : __assert_fail
("Token.is(MIToken::NamedVirtualRegister) && \"Expected NamedVReg token\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 966, __extension__ __PRETTY_FUNCTION__))
;
967 StringRef Name = Token.stringValue();
968 // TODO: Check that the VReg name is not the same as a physical register name.
969 // If it is, then print a warning (when warnings are implemented).
970 Info = &PFS.getVRegInfoNamed(Name);
971 return false;
972}
973
974bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
975 if (Token.is(MIToken::NamedVirtualRegister))
976 return parseNamedVirtualRegister(Info);
977 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token")(static_cast <bool> (Token.is(MIToken::VirtualRegister)
&& "Needs VirtualRegister token") ? void (0) : __assert_fail
("Token.is(MIToken::VirtualRegister) && \"Needs VirtualRegister token\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 977, __extension__ __PRETTY_FUNCTION__))
;
978 unsigned ID;
979 if (getUnsigned(ID))
980 return true;
981 Info = &PFS.getVRegInfo(ID);
982 return false;
983}
984
985bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
986 switch (Token.kind()) {
987 case MIToken::underscore:
988 Reg = 0;
989 return false;
990 case MIToken::NamedRegister:
991 return parseNamedRegister(Reg);
992 case MIToken::NamedVirtualRegister:
993 case MIToken::VirtualRegister:
994 if (parseVirtualRegister(Info))
995 return true;
996 Reg = Info->VReg;
997 return false;
998 // TODO: Parse other register kinds.
999 default:
1000 llvm_unreachable("The current token should be a register")::llvm::llvm_unreachable_internal("The current token should be a register"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1000)
;
1001 }
1002}
1003
1004bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
1005 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1006 return error("expected '_', register class, or register bank name");
1007 StringRef::iterator Loc = Token.location();
1008 StringRef Name = Token.stringValue();
1009
1010 // Was it a register class?
1011 auto RCNameI = PFS.Names2RegClasses.find(Name);
1012 if (RCNameI != PFS.Names2RegClasses.end()) {
1013 lex();
1014 const TargetRegisterClass &RC = *RCNameI->getValue();
1015
1016 switch (RegInfo.Kind) {
1017 case VRegInfo::UNKNOWN:
1018 case VRegInfo::NORMAL:
1019 RegInfo.Kind = VRegInfo::NORMAL;
1020 if (RegInfo.Explicit && RegInfo.D.RC != &RC) {
1021 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1022 return error(Loc, Twine("conflicting register classes, previously: ") +
1023 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1024 }
1025 RegInfo.D.RC = &RC;
1026 RegInfo.Explicit = true;
1027 return false;
1028
1029 case VRegInfo::GENERIC:
1030 case VRegInfo::REGBANK:
1031 return error(Loc, "register class specification on generic register");
1032 }
1033 llvm_unreachable("Unexpected register kind")::llvm::llvm_unreachable_internal("Unexpected register kind",
"/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1033)
;
1034 }
1035
1036 // Should be a register bank or a generic register.
1037 const RegisterBank *RegBank = nullptr;
1038 if (Name != "_") {
1039 auto RBNameI = PFS.Names2RegBanks.find(Name);
1040 if (RBNameI == PFS.Names2RegBanks.end())
1041 return error(Loc, "expected '_', register class, or register bank name");
1042 RegBank = RBNameI->getValue();
1043 }
1044
1045 lex();
1046
1047 switch (RegInfo.Kind) {
1048 case VRegInfo::UNKNOWN:
1049 case VRegInfo::GENERIC:
1050 case VRegInfo::REGBANK:
1051 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1052 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1053 return error(Loc, "conflicting generic register banks");
1054 RegInfo.D.RegBank = RegBank;
1055 RegInfo.Explicit = true;
1056 return false;
1057
1058 case VRegInfo::NORMAL:
1059 return error(Loc, "register bank specification on normal register");
1060 }
1061 llvm_unreachable("Unexpected register kind")::llvm::llvm_unreachable_internal("Unexpected register kind",
"/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1061)
;
1062}
1063
1064bool MIParser::parseRegisterFlag(unsigned &Flags) {
1065 const unsigned OldFlags = Flags;
1066 switch (Token.kind()) {
1067 case MIToken::kw_implicit:
1068 Flags |= RegState::Implicit;
1069 break;
1070 case MIToken::kw_implicit_define:
1071 Flags |= RegState::ImplicitDefine;
1072 break;
1073 case MIToken::kw_def:
1074 Flags |= RegState::Define;
1075 break;
1076 case MIToken::kw_dead:
1077 Flags |= RegState::Dead;
1078 break;
1079 case MIToken::kw_killed:
1080 Flags |= RegState::Kill;
1081 break;
1082 case MIToken::kw_undef:
1083 Flags |= RegState::Undef;
1084 break;
1085 case MIToken::kw_internal:
1086 Flags |= RegState::InternalRead;
1087 break;
1088 case MIToken::kw_early_clobber:
1089 Flags |= RegState::EarlyClobber;
1090 break;
1091 case MIToken::kw_debug_use:
1092 Flags |= RegState::Debug;
1093 break;
1094 case MIToken::kw_renamable:
1095 Flags |= RegState::Renamable;
1096 break;
1097 default:
1098 llvm_unreachable("The current token should be a register flag")::llvm::llvm_unreachable_internal("The current token should be a register flag"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1098)
;
1099 }
1100 if (OldFlags == Flags)
1101 // We know that the same flag is specified more than once when the flags
1102 // weren't modified.
1103 return error("duplicate '" + Token.stringValue() + "' register flag");
1104 lex();
1105 return false;
1106}
1107
1108bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
1109 assert(Token.is(MIToken::dot))(static_cast <bool> (Token.is(MIToken::dot)) ? void (0)
: __assert_fail ("Token.is(MIToken::dot)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1109, __extension__ __PRETTY_FUNCTION__))
;
1110 lex();
1111 if (Token.isNot(MIToken::Identifier))
1112 return error("expected a subregister index after '.'");
1113 auto Name = Token.stringValue();
1114 SubReg = getSubRegIndex(Name);
1115 if (!SubReg)
1116 return error(Twine("use of unknown subregister index '") + Name + "'");
1117 lex();
1118 return false;
1119}
1120
1121bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1122 if (!consumeIfPresent(MIToken::kw_tied_def))
1123 return true;
1124 if (Token.isNot(MIToken::IntegerLiteral))
1125 return error("expected an integer literal after 'tied-def'");
1126 if (getUnsigned(TiedDefIdx))
1127 return true;
1128 lex();
1129 if (expectAndConsume(MIToken::rparen))
1130 return true;
1131 return false;
1132}
1133
1134bool MIParser::assignRegisterTies(MachineInstr &MI,
1135 ArrayRef<ParsedMachineOperand> Operands) {
1136 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1137 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1138 if (!Operands[I].TiedDefIdx)
1139 continue;
1140 // The parser ensures that this operand is a register use, so we just have
1141 // to check the tied-def operand.
1142 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1143 if (DefIdx >= E)
1144 return error(Operands[I].Begin,
1145 Twine("use of invalid tied-def operand index '" +
1146 Twine(DefIdx) + "'; instruction has only ") +
1147 Twine(E) + " operands");
1148 const auto &DefOperand = Operands[DefIdx].Operand;
1149 if (!DefOperand.isReg() || !DefOperand.isDef())
1150 // FIXME: add note with the def operand.
1151 return error(Operands[I].Begin,
1152 Twine("use of invalid tied-def operand index '") +
1153 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1154 " isn't a defined register");
1155 // Check that the tied-def operand wasn't tied elsewhere.
1156 for (const auto &TiedPair : TiedRegisterPairs) {
1157 if (TiedPair.first == DefIdx)
1158 return error(Operands[I].Begin,
1159 Twine("the tied-def operand #") + Twine(DefIdx) +
1160 " is already tied with another register operand");
1161 }
1162 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1163 }
1164 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1165 // indices must be less than tied max.
1166 for (const auto &TiedPair : TiedRegisterPairs)
1167 MI.tieOperands(TiedPair.first, TiedPair.second);
1168 return false;
1169}
1170
1171bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1172 Optional<unsigned> &TiedDefIdx,
1173 bool IsDef) {
1174 unsigned Flags = IsDef ? RegState::Define : 0;
1175 while (Token.isRegisterFlag()) {
1176 if (parseRegisterFlag(Flags))
1177 return true;
1178 }
1179 if (!Token.isRegister())
1180 return error("expected a register after register flags");
1181 unsigned Reg;
1182 VRegInfo *RegInfo;
1183 if (parseRegister(Reg, RegInfo))
1184 return true;
1185 lex();
1186 unsigned SubReg = 0;
1187 if (Token.is(MIToken::dot)) {
1188 if (parseSubRegisterIndex(SubReg))
1189 return true;
1190 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1191 return error("subregister index expects a virtual register");
1192 }
1193 if (Token.is(MIToken::colon)) {
1194 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1195 return error("register class specification expects a virtual register");
1196 lex();
1197 if (parseRegisterClassOrBank(*RegInfo))
1198 return true;
1199 }
1200 MachineRegisterInfo &MRI = MF.getRegInfo();
1201 if ((Flags & RegState::Define) == 0) {
1202 if (consumeIfPresent(MIToken::lparen)) {
1203 unsigned Idx;
1204 if (!parseRegisterTiedDefIndex(Idx))
1205 TiedDefIdx = Idx;
1206 else {
1207 // Try a redundant low-level type.
1208 LLT Ty;
1209 if (parseLowLevelType(Token.location(), Ty))
1210 return error("expected tied-def or low-level type after '('");
1211
1212 if (expectAndConsume(MIToken::rparen))
1213 return true;
1214
1215 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1216 return error("inconsistent type for generic virtual register");
1217
1218 MRI.setType(Reg, Ty);
1219 }
1220 }
1221 } else if (consumeIfPresent(MIToken::lparen)) {
1222 // Virtual registers may have a tpe with GlobalISel.
1223 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1224 return error("unexpected type on physical register");
1225
1226 LLT Ty;
1227 if (parseLowLevelType(Token.location(), Ty))
1228 return true;
1229
1230 if (expectAndConsume(MIToken::rparen))
1231 return true;
1232
1233 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1234 return error("inconsistent type for generic virtual register");
1235
1236 MRI.setType(Reg, Ty);
1237 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1238 // Generic virtual registers must have a type.
1239 // If we end up here this means the type hasn't been specified and
1240 // this is bad!
1241 if (RegInfo->Kind == VRegInfo::GENERIC ||
1242 RegInfo->Kind == VRegInfo::REGBANK)
1243 return error("generic virtual registers must have a type");
1244 }
1245 Dest = MachineOperand::CreateReg(
1246 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
1247 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
1248 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
1249 Flags & RegState::InternalRead, Flags & RegState::Renamable);
1250
1251 return false;
1252}
1253
1254bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1255 assert(Token.is(MIToken::IntegerLiteral))(static_cast <bool> (Token.is(MIToken::IntegerLiteral))
? void (0) : __assert_fail ("Token.is(MIToken::IntegerLiteral)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1255, __extension__ __PRETTY_FUNCTION__))
;
1256 const APSInt &Int = Token.integerValue();
1257 if (Int.getMinSignedBits() > 64)
1258 return error("integer literal is too large to be an immediate operand");
1259 Dest = MachineOperand::CreateImm(Int.getExtValue());
1260 lex();
1261 return false;
1262}
1263
1264bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1265 const Constant *&C) {
1266 auto Source = StringValue.str(); // The source has to be null terminated.
1267 SMDiagnostic Err;
1268 C = parseConstantValue(Source, Err, *MF.getFunction().getParent(),
1269 &PFS.IRSlots);
1270 if (!C)
1271 return error(Loc + Err.getColumnNo(), Err.getMessage());
1272 return false;
1273}
1274
1275bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1276 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1277 return true;
1278 lex();
1279 return false;
1280}
1281
1282bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
1283 if (Token.is(MIToken::ScalarType)) {
1284 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1285 lex();
1286 return false;
1287 } else if (Token.is(MIToken::PointerType)) {
1288 const DataLayout &DL = MF.getDataLayout();
1289 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1290 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
1291 lex();
1292 return false;
1293 }
1294
1295 // Now we're looking for a vector.
1296 if (Token.isNot(MIToken::less))
1297 return error(Loc,
1298 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1299
1300 lex();
1301
1302 if (Token.isNot(MIToken::IntegerLiteral))
1303 return error(Loc, "expected <N x sM> for vctor type");
1304 uint64_t NumElements = Token.integerValue().getZExtValue();
1305 lex();
1306
1307 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1308 return error(Loc, "expected '<N x sM>' for vector type");
1309 lex();
1310
1311 if (Token.isNot(MIToken::ScalarType))
1312 return error(Loc, "expected '<N x sM>' for vector type");
1313 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1314 lex();
1315
1316 if (Token.isNot(MIToken::greater))
1317 return error(Loc, "expected '<N x sM>' for vector type");
1318 lex();
1319
1320 Ty = LLT::vector(NumElements, ScalarSize);
1321 return false;
1322}
1323
1324bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1325 assert(Token.is(MIToken::IntegerType))(static_cast <bool> (Token.is(MIToken::IntegerType)) ? void
(0) : __assert_fail ("Token.is(MIToken::IntegerType)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1325, __extension__ __PRETTY_FUNCTION__))
;
1326 auto Loc = Token.location();
1327 lex();
1328 if (Token.isNot(MIToken::IntegerLiteral))
1329 return error("expected an integer literal");
1330 const Constant *C = nullptr;
1331 if (parseIRConstant(Loc, C))
1332 return true;
1333 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1334 return false;
1335}
1336
1337bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1338 auto Loc = Token.location();
1339 lex();
1340 if (Token.isNot(MIToken::FloatingPointLiteral) &&
1341 Token.isNot(MIToken::HexLiteral))
1342 return error("expected a floating point literal");
1343 const Constant *C = nullptr;
1344 if (parseIRConstant(Loc, C))
1345 return true;
1346 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1347 return false;
1348}
1349
1350bool MIParser::getUnsigned(unsigned &Result) {
1351 if (Token.hasIntegerValue()) {
26
Assuming the condition is true
27
Taking true branch
1352 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1353 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1354 if (Val64 == Limit)
28
Assuming 'Val64' is equal to 'Limit'
29
Taking true branch
1355 return error("expected 32-bit integer (too large)");
30
Returning without writing to 'Result'
1356 Result = Val64;
1357 return false;
1358 }
1359 if (Token.is(MIToken::HexLiteral)) {
1360 APInt A;
1361 if (getHexUint(A))
1362 return true;
1363 if (A.getBitWidth() > 32)
1364 return error("expected 32-bit integer (too large)");
1365 Result = A.getZExtValue();
1366 return false;
1367 }
1368 return true;
1369}
1370
1371bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
1372 assert(Token.is(MIToken::MachineBasicBlock) ||(static_cast <bool> (Token.is(MIToken::MachineBasicBlock
) || Token.is(MIToken::MachineBasicBlockLabel)) ? void (0) : __assert_fail
("Token.is(MIToken::MachineBasicBlock) || Token.is(MIToken::MachineBasicBlockLabel)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1373, __extension__ __PRETTY_FUNCTION__))
1373 Token.is(MIToken::MachineBasicBlockLabel))(static_cast <bool> (Token.is(MIToken::MachineBasicBlock
) || Token.is(MIToken::MachineBasicBlockLabel)) ? void (0) : __assert_fail
("Token.is(MIToken::MachineBasicBlock) || Token.is(MIToken::MachineBasicBlockLabel)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1373, __extension__ __PRETTY_FUNCTION__))
;
1374 unsigned Number;
1375 if (getUnsigned(Number))
1376 return true;
1377 auto MBBInfo = PFS.MBBSlots.find(Number);
1378 if (MBBInfo == PFS.MBBSlots.end())
1379 return error(Twine("use of undefined machine basic block #") +
1380 Twine(Number));
1381 MBB = MBBInfo->second;
1382 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
1383 // we drop the <irname> from the bb.<id>.<irname> format.
1384 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1385 return error(Twine("the name of machine basic block #") + Twine(Number) +
1386 " isn't '" + Token.stringValue() + "'");
1387 return false;
1388}
1389
1390bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1391 MachineBasicBlock *MBB;
1392 if (parseMBBReference(MBB))
1393 return true;
1394 Dest = MachineOperand::CreateMBB(MBB);
1395 lex();
1396 return false;
1397}
1398
1399bool MIParser::parseStackFrameIndex(int &FI) {
1400 assert(Token.is(MIToken::StackObject))(static_cast <bool> (Token.is(MIToken::StackObject)) ? void
(0) : __assert_fail ("Token.is(MIToken::StackObject)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1400, __extension__ __PRETTY_FUNCTION__))
;
1401 unsigned ID;
1402 if (getUnsigned(ID))
1403 return true;
1404 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1405 if (ObjectInfo == PFS.StackObjectSlots.end())
1406 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1407 "'");
1408 StringRef Name;
1409 if (const auto *Alloca =
1410 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
1411 Name = Alloca->getName();
1412 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1413 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1414 "' isn't '" + Token.stringValue() + "'");
1415 lex();
1416 FI = ObjectInfo->second;
1417 return false;
1418}
1419
1420bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1421 int FI;
1422 if (parseStackFrameIndex(FI))
1423 return true;
1424 Dest = MachineOperand::CreateFI(FI);
1425 return false;
1426}
1427
1428bool MIParser::parseFixedStackFrameIndex(int &FI) {
1429 assert(Token.is(MIToken::FixedStackObject))(static_cast <bool> (Token.is(MIToken::FixedStackObject
)) ? void (0) : __assert_fail ("Token.is(MIToken::FixedStackObject)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1429, __extension__ __PRETTY_FUNCTION__))
;
1430 unsigned ID;
1431 if (getUnsigned(ID))
1432 return true;
1433 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1434 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1435 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1436 Twine(ID) + "'");
1437 lex();
1438 FI = ObjectInfo->second;
1439 return false;
1440}
1441
1442bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1443 int FI;
1444 if (parseFixedStackFrameIndex(FI))
1445 return true;
1446 Dest = MachineOperand::CreateFI(FI);
1447 return false;
1448}
1449
1450bool MIParser::parseGlobalValue(GlobalValue *&GV) {
1451 switch (Token.kind()) {
1452 case MIToken::NamedGlobalValue: {
1453 const Module *M = MF.getFunction().getParent();
1454 GV = M->getNamedValue(Token.stringValue());
1455 if (!GV)
1456 return error(Twine("use of undefined global value '") + Token.range() +
1457 "'");
1458 break;
1459 }
1460 case MIToken::GlobalValue: {
1461 unsigned GVIdx;
1462 if (getUnsigned(GVIdx))
1463 return true;
1464 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
1465 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1466 "'");
1467 GV = PFS.IRSlots.GlobalValues[GVIdx];
1468 break;
1469 }
1470 default:
1471 llvm_unreachable("The current token should be a global value")::llvm::llvm_unreachable_internal("The current token should be a global value"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1471)
;
1472 }
1473 return false;
1474}
1475
1476bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1477 GlobalValue *GV = nullptr;
1478 if (parseGlobalValue(GV))
1479 return true;
1480 lex();
1481 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
1482 if (parseOperandsOffset(Dest))
1483 return true;
1484 return false;
1485}
1486
1487bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1488 assert(Token.is(MIToken::ConstantPoolItem))(static_cast <bool> (Token.is(MIToken::ConstantPoolItem
)) ? void (0) : __assert_fail ("Token.is(MIToken::ConstantPoolItem)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1488, __extension__ __PRETTY_FUNCTION__))
;
1489 unsigned ID;
1490 if (getUnsigned(ID))
1491 return true;
1492 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1493 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1494 return error("use of undefined constant '%const." + Twine(ID) + "'");
1495 lex();
1496 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
1497 if (parseOperandsOffset(Dest))
1498 return true;
1499 return false;
1500}
1501
1502bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1503 assert(Token.is(MIToken::JumpTableIndex))(static_cast <bool> (Token.is(MIToken::JumpTableIndex))
? void (0) : __assert_fail ("Token.is(MIToken::JumpTableIndex)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1503, __extension__ __PRETTY_FUNCTION__))
;
1504 unsigned ID;
1505 if (getUnsigned(ID))
1506 return true;
1507 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1508 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1509 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1510 lex();
1511 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1512 return false;
1513}
1514
1515bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
1516 assert(Token.is(MIToken::ExternalSymbol))(static_cast <bool> (Token.is(MIToken::ExternalSymbol))
? void (0) : __assert_fail ("Token.is(MIToken::ExternalSymbol)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1516, __extension__ __PRETTY_FUNCTION__))
;
1517 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
1518 lex();
1519 Dest = MachineOperand::CreateES(Symbol);
1520 if (parseOperandsOffset(Dest))
1521 return true;
1522 return false;
1523}
1524
1525bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1526 assert(Token.is(MIToken::SubRegisterIndex))(static_cast <bool> (Token.is(MIToken::SubRegisterIndex
)) ? void (0) : __assert_fail ("Token.is(MIToken::SubRegisterIndex)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1526, __extension__ __PRETTY_FUNCTION__))
;
1527 StringRef Name = Token.stringValue();
1528 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1529 if (SubRegIndex == 0)
1530 return error(Twine("unknown subregister index '") + Name + "'");
1531 lex();
1532 Dest = MachineOperand::CreateImm(SubRegIndex);
1533 return false;
1534}
1535
1536bool MIParser::parseMDNode(MDNode *&Node) {
1537 assert(Token.is(MIToken::exclaim))(static_cast <bool> (Token.is(MIToken::exclaim)) ? void
(0) : __assert_fail ("Token.is(MIToken::exclaim)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1537, __extension__ __PRETTY_FUNCTION__))
;
1538
1539 auto Loc = Token.location();
1540 lex();
1541 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
23
Taking false branch
1542 return error("expected metadata id after '!'");
1543 unsigned ID;
24
'ID' declared without an initial value
1544 if (getUnsigned(ID))
25
Calling 'MIParser::getUnsigned'
31
Returning from 'MIParser::getUnsigned'
32
Assuming the condition is false
33
Taking false branch
1545 return true;
1546 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1547 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
34
Assuming the condition is true
35
Taking true branch
1548 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
36
1st function call argument is an uninitialized value
1549 lex();
1550 Node = NodeInfo->second.get();
1551 return false;
1552}
1553
1554bool MIParser::parseDIExpression(MDNode *&Expr) {
1555 assert(Token.is(MIToken::md_diexpr))(static_cast <bool> (Token.is(MIToken::md_diexpr)) ? void
(0) : __assert_fail ("Token.is(MIToken::md_diexpr)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1555, __extension__ __PRETTY_FUNCTION__))
;
1556 lex();
1557
1558 // FIXME: Share this parsing with the IL parser.
1559 SmallVector<uint64_t, 8> Elements;
1560
1561 if (expectAndConsume(MIToken::lparen))
1562 return true;
1563
1564 if (Token.isNot(MIToken::rparen)) {
1565 do {
1566 if (Token.is(MIToken::Identifier)) {
1567 if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
1568 lex();
1569 Elements.push_back(Op);
1570 continue;
1571 }
1572 return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1573 }
1574
1575 if (Token.isNot(MIToken::IntegerLiteral) ||
1576 Token.integerValue().isSigned())
1577 return error("expected unsigned integer");
1578
1579 auto &U = Token.integerValue();
1580 if (U.ugt(UINT64_MAX(18446744073709551615UL)))
1581 return error("element too large, limit is " + Twine(UINT64_MAX(18446744073709551615UL)));
1582 Elements.push_back(U.getZExtValue());
1583 lex();
1584
1585 } while (consumeIfPresent(MIToken::comma));
1586 }
1587
1588 if (expectAndConsume(MIToken::rparen))
1589 return true;
1590
1591 Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
1592 return false;
1593}
1594
1595bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1596 MDNode *Node = nullptr;
1597 if (Token.is(MIToken::exclaim)) {
1598 if (parseMDNode(Node))
1599 return true;
1600 } else if (Token.is(MIToken::md_diexpr)) {
1601 if (parseDIExpression(Node))
1602 return true;
1603 }
1604 Dest = MachineOperand::CreateMetadata(Node);
1605 return false;
1606}
1607
1608bool MIParser::parseCFIOffset(int &Offset) {
1609 if (Token.isNot(MIToken::IntegerLiteral))
1610 return error("expected a cfi offset");
1611 if (Token.integerValue().getMinSignedBits() > 32)
1612 return error("expected a 32 bit integer (the cfi offset is too large)");
1613 Offset = (int)Token.integerValue().getExtValue();
1614 lex();
1615 return false;
1616}
1617
1618bool MIParser::parseCFIRegister(unsigned &Reg) {
1619 if (Token.isNot(MIToken::NamedRegister))
1620 return error("expected a cfi register");
1621 unsigned LLVMReg;
1622 if (parseNamedRegister(LLVMReg))
1623 return true;
1624 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1625 assert(TRI && "Expected target register info")(static_cast <bool> (TRI && "Expected target register info"
) ? void (0) : __assert_fail ("TRI && \"Expected target register info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1625, __extension__ __PRETTY_FUNCTION__))
;
1626 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1627 if (DwarfReg < 0)
1628 return error("invalid DWARF register");
1629 Reg = (unsigned)DwarfReg;
1630 lex();
1631 return false;
1632}
1633
1634bool MIParser::parseCFIEscapeValues(std::string &Values) {
1635 do {
1636 if (Token.isNot(MIToken::HexLiteral))
1637 return error("expected a hexadecimal literal");
1638 unsigned Value;
1639 if (getUnsigned(Value))
1640 return true;
1641 if (Value > UINT8_MAX(255))
1642 return error("expected a 8-bit integer (too large)");
1643 Values.push_back(static_cast<uint8_t>(Value));
1644 lex();
1645 } while (consumeIfPresent(MIToken::comma));
1646 return false;
1647}
1648
1649bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1650 auto Kind = Token.kind();
1651 lex();
1652 int Offset;
1653 unsigned Reg;
1654 unsigned CFIIndex;
1655 switch (Kind) {
1656 case MIToken::kw_cfi_same_value:
1657 if (parseCFIRegister(Reg))
1658 return true;
1659 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
1660 break;
1661 case MIToken::kw_cfi_offset:
1662 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1663 parseCFIOffset(Offset))
1664 return true;
1665 CFIIndex =
1666 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
1667 break;
1668 case MIToken::kw_cfi_rel_offset:
1669 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1670 parseCFIOffset(Offset))
1671 return true;
1672 CFIIndex = MF.addFrameInst(
1673 MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
1674 break;
1675 case MIToken::kw_cfi_def_cfa_register:
1676 if (parseCFIRegister(Reg))
1677 return true;
1678 CFIIndex =
1679 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
1680 break;
1681 case MIToken::kw_cfi_def_cfa_offset:
1682 if (parseCFIOffset(Offset))
1683 return true;
1684 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
1685 CFIIndex = MF.addFrameInst(
1686 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1687 break;
1688 case MIToken::kw_cfi_adjust_cfa_offset:
1689 if (parseCFIOffset(Offset))
1690 return true;
1691 CFIIndex = MF.addFrameInst(
1692 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
1693 break;
1694 case MIToken::kw_cfi_def_cfa:
1695 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1696 parseCFIOffset(Offset))
1697 return true;
1698 // NB: MCCFIInstruction::createDefCfa negates the offset.
1699 CFIIndex =
1700 MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
1701 break;
1702 case MIToken::kw_cfi_remember_state:
1703 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
1704 break;
1705 case MIToken::kw_cfi_restore:
1706 if (parseCFIRegister(Reg))
1707 return true;
1708 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
1709 break;
1710 case MIToken::kw_cfi_restore_state:
1711 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
1712 break;
1713 case MIToken::kw_cfi_undefined:
1714 if (parseCFIRegister(Reg))
1715 return true;
1716 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
1717 break;
1718 case MIToken::kw_cfi_register: {
1719 unsigned Reg2;
1720 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1721 parseCFIRegister(Reg2))
1722 return true;
1723
1724 CFIIndex =
1725 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
1726 break;
1727 }
1728 case MIToken::kw_cfi_window_save:
1729 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
1730 break;
1731 case MIToken::kw_cfi_escape: {
1732 std::string Values;
1733 if (parseCFIEscapeValues(Values))
1734 return true;
1735 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
1736 break;
1737 }
1738 default:
1739 // TODO: Parse the other CFI operands.
1740 llvm_unreachable("The current token should be a cfi operand")::llvm::llvm_unreachable_internal("The current token should be a cfi operand"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1740)
;
1741 }
1742 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
1743 return false;
1744}
1745
1746bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1747 switch (Token.kind()) {
1748 case MIToken::NamedIRBlock: {
1749 BB = dyn_cast_or_null<BasicBlock>(
1750 F.getValueSymbolTable()->lookup(Token.stringValue()));
1751 if (!BB)
1752 return error(Twine("use of undefined IR block '") + Token.range() + "'");
1753 break;
1754 }
1755 case MIToken::IRBlock: {
1756 unsigned SlotNumber = 0;
1757 if (getUnsigned(SlotNumber))
1758 return true;
1759 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
1760 if (!BB)
1761 return error(Twine("use of undefined IR block '%ir-block.") +
1762 Twine(SlotNumber) + "'");
1763 break;
1764 }
1765 default:
1766 llvm_unreachable("The current token should be an IR block reference")::llvm::llvm_unreachable_internal("The current token should be an IR block reference"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1766)
;
1767 }
1768 return false;
1769}
1770
1771bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1772 assert(Token.is(MIToken::kw_blockaddress))(static_cast <bool> (Token.is(MIToken::kw_blockaddress)
) ? void (0) : __assert_fail ("Token.is(MIToken::kw_blockaddress)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1772, __extension__ __PRETTY_FUNCTION__))
;
1773 lex();
1774 if (expectAndConsume(MIToken::lparen))
1775 return true;
1776 if (Token.isNot(MIToken::GlobalValue) &&
1777 Token.isNot(MIToken::NamedGlobalValue))
1778 return error("expected a global value");
1779 GlobalValue *GV = nullptr;
1780 if (parseGlobalValue(GV))
1781 return true;
1782 auto *F = dyn_cast<Function>(GV);
1783 if (!F)
1784 return error("expected an IR function reference");
1785 lex();
1786 if (expectAndConsume(MIToken::comma))
1787 return true;
1788 BasicBlock *BB = nullptr;
1789 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
1790 return error("expected an IR block reference");
1791 if (parseIRBlock(BB, *F))
1792 return true;
1793 lex();
1794 if (expectAndConsume(MIToken::rparen))
1795 return true;
1796 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
1797 if (parseOperandsOffset(Dest))
1798 return true;
1799 return false;
1800}
1801
1802bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1803 assert(Token.is(MIToken::kw_intrinsic))(static_cast <bool> (Token.is(MIToken::kw_intrinsic)) ?
void (0) : __assert_fail ("Token.is(MIToken::kw_intrinsic)",
"/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1803, __extension__ __PRETTY_FUNCTION__))
;
1804 lex();
1805 if (expectAndConsume(MIToken::lparen))
1806 return error("expected syntax intrinsic(@llvm.whatever)");
1807
1808 if (Token.isNot(MIToken::NamedGlobalValue))
1809 return error("expected syntax intrinsic(@llvm.whatever)");
1810
1811 std::string Name = Token.stringValue();
1812 lex();
1813
1814 if (expectAndConsume(MIToken::rparen))
1815 return error("expected ')' to terminate intrinsic name");
1816
1817 // Find out what intrinsic we're dealing with, first try the global namespace
1818 // and then the target's private intrinsics if that fails.
1819 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1820 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1821 if (ID == Intrinsic::not_intrinsic && TII)
1822 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1823
1824 if (ID == Intrinsic::not_intrinsic)
1825 return error("unknown intrinsic name");
1826 Dest = MachineOperand::CreateIntrinsicID(ID);
1827
1828 return false;
1829}
1830
1831bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1832 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred))(static_cast <bool> (Token.is(MIToken::kw_intpred) || Token
.is(MIToken::kw_floatpred)) ? void (0) : __assert_fail ("Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1832, __extension__ __PRETTY_FUNCTION__))
;
1833 bool IsFloat = Token.is(MIToken::kw_floatpred);
1834 lex();
1835
1836 if (expectAndConsume(MIToken::lparen))
1837 return error("expected syntax intpred(whatever) or floatpred(whatever");
1838
1839 if (Token.isNot(MIToken::Identifier))
1840 return error("whatever");
1841
1842 CmpInst::Predicate Pred;
1843 if (IsFloat) {
1844 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1845 .Case("false", CmpInst::FCMP_FALSE)
1846 .Case("oeq", CmpInst::FCMP_OEQ)
1847 .Case("ogt", CmpInst::FCMP_OGT)
1848 .Case("oge", CmpInst::FCMP_OGE)
1849 .Case("olt", CmpInst::FCMP_OLT)
1850 .Case("ole", CmpInst::FCMP_OLE)
1851 .Case("one", CmpInst::FCMP_ONE)
1852 .Case("ord", CmpInst::FCMP_ORD)
1853 .Case("uno", CmpInst::FCMP_UNO)
1854 .Case("ueq", CmpInst::FCMP_UEQ)
1855 .Case("ugt", CmpInst::FCMP_UGT)
1856 .Case("uge", CmpInst::FCMP_UGE)
1857 .Case("ult", CmpInst::FCMP_ULT)
1858 .Case("ule", CmpInst::FCMP_ULE)
1859 .Case("une", CmpInst::FCMP_UNE)
1860 .Case("true", CmpInst::FCMP_TRUE)
1861 .Default(CmpInst::BAD_FCMP_PREDICATE);
1862 if (!CmpInst::isFPPredicate(Pred))
1863 return error("invalid floating-point predicate");
1864 } else {
1865 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1866 .Case("eq", CmpInst::ICMP_EQ)
1867 .Case("ne", CmpInst::ICMP_NE)
1868 .Case("sgt", CmpInst::ICMP_SGT)
1869 .Case("sge", CmpInst::ICMP_SGE)
1870 .Case("slt", CmpInst::ICMP_SLT)
1871 .Case("sle", CmpInst::ICMP_SLE)
1872 .Case("ugt", CmpInst::ICMP_UGT)
1873 .Case("uge", CmpInst::ICMP_UGE)
1874 .Case("ult", CmpInst::ICMP_ULT)
1875 .Case("ule", CmpInst::ICMP_ULE)
1876 .Default(CmpInst::BAD_ICMP_PREDICATE);
1877 if (!CmpInst::isIntPredicate(Pred))
1878 return error("invalid integer predicate");
1879 }
1880
1881 lex();
1882 Dest = MachineOperand::CreatePredicate(Pred);
1883 if (expectAndConsume(MIToken::rparen))
1884 return error("predicate should be terminated by ')'.");
1885
1886 return false;
1887}
1888
1889bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1890 assert(Token.is(MIToken::kw_target_index))(static_cast <bool> (Token.is(MIToken::kw_target_index)
) ? void (0) : __assert_fail ("Token.is(MIToken::kw_target_index)"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1890, __extension__ __PRETTY_FUNCTION__))
;
1891 lex();
1892 if (expectAndConsume(MIToken::lparen))
1893 return true;
1894 if (Token.isNot(MIToken::Identifier))
1895 return error("expected the name of the target index");
1896 int Index = 0;
1897 if (getTargetIndex(Token.stringValue(), Index))
1898 return error("use of undefined target index '" + Token.stringValue() + "'");
1899 lex();
1900 if (expectAndConsume(MIToken::rparen))
1901 return true;
1902 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
1903 if (parseOperandsOffset(Dest))
1904 return true;
1905 return false;
1906}
1907
1908bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
1909 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask")(static_cast <bool> (Token.stringValue() == "CustomRegMask"
&& "Expected a custom RegMask") ? void (0) : __assert_fail
("Token.stringValue() == \"CustomRegMask\" && \"Expected a custom RegMask\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1909, __extension__ __PRETTY_FUNCTION__))
;
1910 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1911 assert(TRI && "Expected target register info")(static_cast <bool> (TRI && "Expected target register info"
) ? void (0) : __assert_fail ("TRI && \"Expected target register info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1911, __extension__ __PRETTY_FUNCTION__))
;
1912 lex();
1913 if (expectAndConsume(MIToken::lparen))
1914 return true;
1915
1916 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1917 while (true) {
1918 if (Token.isNot(MIToken::NamedRegister))
1919 return error("expected a named register");
1920 unsigned Reg;
1921 if (parseNamedRegister(Reg))
1922 return true;
1923 lex();
1924 Mask[Reg / 32] |= 1U << (Reg % 32);
1925 // TODO: Report an error if the same register is used more than once.
1926 if (Token.isNot(MIToken::comma))
1927 break;
1928 lex();
1929 }
1930
1931 if (expectAndConsume(MIToken::rparen))
1932 return true;
1933 Dest = MachineOperand::CreateRegMask(Mask);
1934 return false;
1935}
1936
1937bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1938 assert(Token.is(MIToken::kw_liveout))(static_cast <bool> (Token.is(MIToken::kw_liveout)) ? void
(0) : __assert_fail ("Token.is(MIToken::kw_liveout)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1938, __extension__ __PRETTY_FUNCTION__))
;
1939 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1940 assert(TRI && "Expected target register info")(static_cast <bool> (TRI && "Expected target register info"
) ? void (0) : __assert_fail ("TRI && \"Expected target register info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 1940, __extension__ __PRETTY_FUNCTION__))
;
1941 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1942 lex();
1943 if (expectAndConsume(MIToken::lparen))
1944 return true;
1945 while (true) {
1946 if (Token.isNot(MIToken::NamedRegister))
1947 return error("expected a named register");
1948 unsigned Reg;
1949 if (parseNamedRegister(Reg))
1950 return true;
1951 lex();
1952 Mask[Reg / 32] |= 1U << (Reg % 32);
1953 // TODO: Report an error if the same register is used more than once.
1954 if (Token.isNot(MIToken::comma))
1955 break;
1956 lex();
1957 }
1958 if (expectAndConsume(MIToken::rparen))
1959 return true;
1960 Dest = MachineOperand::CreateRegLiveOut(Mask);
1961 return false;
1962}
1963
1964bool MIParser::parseMachineOperand(MachineOperand &Dest,
1965 Optional<unsigned> &TiedDefIdx) {
1966 switch (Token.kind()) {
1967 case MIToken::kw_implicit:
1968 case MIToken::kw_implicit_define:
1969 case MIToken::kw_def:
1970 case MIToken::kw_dead:
1971 case MIToken::kw_killed:
1972 case MIToken::kw_undef:
1973 case MIToken::kw_internal:
1974 case MIToken::kw_early_clobber:
1975 case MIToken::kw_debug_use:
1976 case MIToken::kw_renamable:
1977 case MIToken::underscore:
1978 case MIToken::NamedRegister:
1979 case MIToken::VirtualRegister:
1980 case MIToken::NamedVirtualRegister:
1981 return parseRegisterOperand(Dest, TiedDefIdx);
1982 case MIToken::IntegerLiteral:
1983 return parseImmediateOperand(Dest);
1984 case MIToken::IntegerType:
1985 return parseTypedImmediateOperand(Dest);
1986 case MIToken::kw_half:
1987 case MIToken::kw_float:
1988 case MIToken::kw_double:
1989 case MIToken::kw_x86_fp80:
1990 case MIToken::kw_fp128:
1991 case MIToken::kw_ppc_fp128:
1992 return parseFPImmediateOperand(Dest);
1993 case MIToken::MachineBasicBlock:
1994 return parseMBBOperand(Dest);
1995 case MIToken::StackObject:
1996 return parseStackObjectOperand(Dest);
1997 case MIToken::FixedStackObject:
1998 return parseFixedStackObjectOperand(Dest);
1999 case MIToken::GlobalValue:
2000 case MIToken::NamedGlobalValue:
2001 return parseGlobalAddressOperand(Dest);
2002 case MIToken::ConstantPoolItem:
2003 return parseConstantPoolIndexOperand(Dest);
2004 case MIToken::JumpTableIndex:
2005 return parseJumpTableIndexOperand(Dest);
2006 case MIToken::ExternalSymbol:
2007 return parseExternalSymbolOperand(Dest);
2008 case MIToken::SubRegisterIndex:
2009 return parseSubRegisterIndexOperand(Dest);
2010 case MIToken::md_diexpr:
2011 case MIToken::exclaim:
2012 return parseMetadataOperand(Dest);
2013 case MIToken::kw_cfi_same_value:
2014 case MIToken::kw_cfi_offset:
2015 case MIToken::kw_cfi_rel_offset:
2016 case MIToken::kw_cfi_def_cfa_register:
2017 case MIToken::kw_cfi_def_cfa_offset:
2018 case MIToken::kw_cfi_adjust_cfa_offset:
2019 case MIToken::kw_cfi_escape:
2020 case MIToken::kw_cfi_def_cfa:
2021 case MIToken::kw_cfi_register:
2022 case MIToken::kw_cfi_remember_state:
2023 case MIToken::kw_cfi_restore:
2024 case MIToken::kw_cfi_restore_state:
2025 case MIToken::kw_cfi_undefined:
2026 case MIToken::kw_cfi_window_save:
2027 return parseCFIOperand(Dest);
2028 case MIToken::kw_blockaddress:
2029 return parseBlockAddressOperand(Dest);
2030 case MIToken::kw_intrinsic:
2031 return parseIntrinsicOperand(Dest);
2032 case MIToken::kw_target_index:
2033 return parseTargetIndexOperand(Dest);
2034 case MIToken::kw_liveout:
2035 return parseLiveoutRegisterMaskOperand(Dest);
2036 case MIToken::kw_floatpred:
2037 case MIToken::kw_intpred:
2038 return parsePredicateOperand(Dest);
2039 case MIToken::Error:
2040 return true;
2041 case MIToken::Identifier:
2042 if (const auto *RegMask = getRegMask(Token.stringValue())) {
2043 Dest = MachineOperand::CreateRegMask(RegMask);
2044 lex();
2045 break;
2046 } else
2047 return parseCustomRegisterMaskOperand(Dest);
2048 default:
2049 // FIXME: Parse the MCSymbol machine operand.
2050 return error("expected a machine operand");
2051 }
2052 return false;
2053}
2054
2055bool MIParser::parseMachineOperandAndTargetFlags(
2056 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
2057 unsigned TF = 0;
2058 bool HasTargetFlags = false;
2059 if (Token.is(MIToken::kw_target_flags)) {
2060 HasTargetFlags = true;
2061 lex();
2062 if (expectAndConsume(MIToken::lparen))
2063 return true;
2064 if (Token.isNot(MIToken::Identifier))
2065 return error("expected the name of the target flag");
2066 if (getDirectTargetFlag(Token.stringValue(), TF)) {
2067 if (getBitmaskTargetFlag(Token.stringValue(), TF))
2068 return error("use of undefined target flag '" + Token.stringValue() +
2069 "'");
2070 }
2071 lex();
2072 while (Token.is(MIToken::comma)) {
2073 lex();
2074 if (Token.isNot(MIToken::Identifier))
2075 return error("expected the name of the target flag");
2076 unsigned BitFlag = 0;
2077 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2078 return error("use of undefined target flag '" + Token.stringValue() +
2079 "'");
2080 // TODO: Report an error when using a duplicate bit target flag.
2081 TF |= BitFlag;
2082 lex();
2083 }
2084 if (expectAndConsume(MIToken::rparen))
2085 return true;
2086 }
2087 auto Loc = Token.location();
2088 if (parseMachineOperand(Dest, TiedDefIdx))
2089 return true;
2090 if (!HasTargetFlags)
2091 return false;
2092 if (Dest.isReg())
2093 return error(Loc, "register operands can't have target flags");
2094 Dest.setTargetFlags(TF);
2095 return false;
2096}
2097
2098bool MIParser::parseOffset(int64_t &Offset) {
2099 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
2100 return false;
2101 StringRef Sign = Token.range();
2102 bool IsNegative = Token.is(MIToken::minus);
2103 lex();
2104 if (Token.isNot(MIToken::IntegerLiteral))
2105 return error("expected an integer literal after '" + Sign + "'");
2106 if (Token.integerValue().getMinSignedBits() > 64)
2107 return error("expected 64-bit integer (too large)");
2108 Offset = Token.integerValue().getExtValue();
2109 if (IsNegative)
2110 Offset = -Offset;
2111 lex();
2112 return false;
2113}
2114
2115bool MIParser::parseAlignment(unsigned &Alignment) {
2116 assert(Token.is(MIToken::kw_align))(static_cast <bool> (Token.is(MIToken::kw_align)) ? void
(0) : __assert_fail ("Token.is(MIToken::kw_align)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2116, __extension__ __PRETTY_FUNCTION__))
;
2117 lex();
2118 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2119 return error("expected an integer literal after 'align'");
2120 if (getUnsigned(Alignment))
2121 return true;
2122 lex();
2123 return false;
2124}
2125
2126bool MIParser::parseAddrspace(unsigned &Addrspace) {
2127 assert(Token.is(MIToken::kw_addrspace))(static_cast <bool> (Token.is(MIToken::kw_addrspace)) ?
void (0) : __assert_fail ("Token.is(MIToken::kw_addrspace)",
"/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2127, __extension__ __PRETTY_FUNCTION__))
;
2128 lex();
2129 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2130 return error("expected an integer literal after 'addrspace'");
2131 if (getUnsigned(Addrspace))
2132 return true;
2133 lex();
2134 return false;
2135}
2136
2137bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2138 int64_t Offset = 0;
2139 if (parseOffset(Offset))
2140 return true;
2141 Op.setOffset(Offset);
2142 return false;
2143}
2144
2145bool MIParser::parseIRValue(const Value *&V) {
2146 switch (Token.kind()) {
2147 case MIToken::NamedIRValue: {
2148 V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
2149 break;
2150 }
2151 case MIToken::IRValue: {
2152 unsigned SlotNumber = 0;
2153 if (getUnsigned(SlotNumber))
2154 return true;
2155 V = getIRValue(SlotNumber);
2156 break;
2157 }
2158 case MIToken::NamedGlobalValue:
2159 case MIToken::GlobalValue: {
2160 GlobalValue *GV = nullptr;
2161 if (parseGlobalValue(GV))
2162 return true;
2163 V = GV;
2164 break;
2165 }
2166 case MIToken::QuotedIRValue: {
2167 const Constant *C = nullptr;
2168 if (parseIRConstant(Token.location(), Token.stringValue(), C))
2169 return true;
2170 V = C;
2171 break;
2172 }
2173 default:
2174 llvm_unreachable("The current token should be an IR block reference")::llvm::llvm_unreachable_internal("The current token should be an IR block reference"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2174)
;
2175 }
2176 if (!V)
2177 return error(Twine("use of undefined IR value '") + Token.range() + "'");
2178 return false;
2179}
2180
2181bool MIParser::getUint64(uint64_t &Result) {
2182 if (Token.hasIntegerValue()) {
2183 if (Token.integerValue().getActiveBits() > 64)
2184 return error("expected 64-bit integer (too large)");
2185 Result = Token.integerValue().getZExtValue();
2186 return false;
2187 }
2188 if (Token.is(MIToken::HexLiteral)) {
2189 APInt A;
2190 if (getHexUint(A))
2191 return true;
2192 if (A.getBitWidth() > 64)
2193 return error("expected 64-bit integer (too large)");
2194 Result = A.getZExtValue();
2195 return false;
2196 }
2197 return true;
2198}
2199
2200bool MIParser::getHexUint(APInt &Result) {
2201 assert(Token.is(MIToken::HexLiteral))(static_cast <bool> (Token.is(MIToken::HexLiteral)) ? void
(0) : __assert_fail ("Token.is(MIToken::HexLiteral)", "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2201, __extension__ __PRETTY_FUNCTION__))
;
2202 StringRef S = Token.range();
2203 assert(S[0] == '0' && tolower(S[1]) == 'x')(static_cast <bool> (S[0] == '0' && tolower(S[1
]) == 'x') ? void (0) : __assert_fail ("S[0] == '0' && tolower(S[1]) == 'x'"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2203, __extension__ __PRETTY_FUNCTION__))
;
2204 // This could be a floating point literal with a special prefix.
2205 if (!isxdigit(S[2]))
2206 return true;
2207 StringRef V = S.substr(2);
2208 APInt A(V.size()*4, V, 16);
2209
2210 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2211 // sure it isn't the case before constructing result.
2212 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2213 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2214 return false;
2215}
2216
2217bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2218 const auto OldFlags = Flags;
2219 switch (Token.kind()) {
2220 case MIToken::kw_volatile:
2221 Flags |= MachineMemOperand::MOVolatile;
2222 break;
2223 case MIToken::kw_non_temporal:
2224 Flags |= MachineMemOperand::MONonTemporal;
2225 break;
2226 case MIToken::kw_dereferenceable:
2227 Flags |= MachineMemOperand::MODereferenceable;
2228 break;
2229 case MIToken::kw_invariant:
2230 Flags |= MachineMemOperand::MOInvariant;
2231 break;
2232 case MIToken::StringConstant: {
2233 MachineMemOperand::Flags TF;
2234 if (getMMOTargetFlag(Token.stringValue(), TF))
2235 return error("use of undefined target MMO flag '" + Token.stringValue() +
2236 "'");
2237 Flags |= TF;
2238 break;
2239 }
2240 default:
2241 llvm_unreachable("The current token should be a memory operand flag")::llvm::llvm_unreachable_internal("The current token should be a memory operand flag"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2241)
;
2242 }
2243 if (OldFlags == Flags)
2244 // We know that the same flag is specified more than once when the flags
2245 // weren't modified.
2246 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
2247 lex();
2248 return false;
2249}
2250
2251bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2252 switch (Token.kind()) {
2253 case MIToken::kw_stack:
2254 PSV = MF.getPSVManager().getStack();
2255 break;
2256 case MIToken::kw_got:
2257 PSV = MF.getPSVManager().getGOT();
2258 break;
2259 case MIToken::kw_jump_table:
2260 PSV = MF.getPSVManager().getJumpTable();
2261 break;
2262 case MIToken::kw_constant_pool:
2263 PSV = MF.getPSVManager().getConstantPool();
2264 break;
2265 case MIToken::FixedStackObject: {
2266 int FI;
2267 if (parseFixedStackFrameIndex(FI))
2268 return true;
2269 PSV = MF.getPSVManager().getFixedStack(FI);
2270 // The token was already consumed, so use return here instead of break.
2271 return false;
2272 }
2273 case MIToken::StackObject: {
2274 int FI;
2275 if (parseStackFrameIndex(FI))
2276 return true;
2277 PSV = MF.getPSVManager().getFixedStack(FI);
2278 // The token was already consumed, so use return here instead of break.
2279 return false;
2280 }
2281 case MIToken::kw_call_entry:
2282 lex();
2283 switch (Token.kind()) {
2284 case MIToken::GlobalValue:
2285 case MIToken::NamedGlobalValue: {
2286 GlobalValue *GV = nullptr;
2287 if (parseGlobalValue(GV))
2288 return true;
2289 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2290 break;
2291 }
2292 case MIToken::ExternalSymbol:
2293 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2294 MF.createExternalSymbolName(Token.stringValue()));
2295 break;
2296 default:
2297 return error(
2298 "expected a global value or an external symbol after 'call-entry'");
2299 }
2300 break;
2301 default:
2302 llvm_unreachable("The current token should be pseudo source value")::llvm::llvm_unreachable_internal("The current token should be pseudo source value"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2302)
;
2303 }
2304 lex();
2305 return false;
2306}
2307
2308bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
2309 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
2310 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
2311 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2312 Token.is(MIToken::kw_call_entry)) {
2313 const PseudoSourceValue *PSV = nullptr;
2314 if (parseMemoryPseudoSourceValue(PSV))
2315 return true;
2316 int64_t Offset = 0;
2317 if (parseOffset(Offset))
2318 return true;
2319 Dest = MachinePointerInfo(PSV, Offset);
2320 return false;
2321 }
2322 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2323 Token.isNot(MIToken::GlobalValue) &&
2324 Token.isNot(MIToken::NamedGlobalValue) &&
2325 Token.isNot(MIToken::QuotedIRValue))
2326 return error("expected an IR value reference");
2327 const Value *V = nullptr;
2328 if (parseIRValue(V))
2329 return true;
2330 if (!V->getType()->isPointerTy())
2331 return error("expected a pointer IR value");
2332 lex();
2333 int64_t Offset = 0;
2334 if (parseOffset(Offset))
2335 return true;
2336 Dest = MachinePointerInfo(V, Offset);
2337 return false;
2338}
2339
2340bool MIParser::parseOptionalScope(LLVMContext &Context,
2341 SyncScope::ID &SSID) {
2342 SSID = SyncScope::System;
2343 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
2344 lex();
2345 if (expectAndConsume(MIToken::lparen))
2346 return error("expected '(' in syncscope");
2347
2348 std::string SSN;
2349 if (parseStringConstant(SSN))
2350 return true;
2351
2352 SSID = Context.getOrInsertSyncScopeID(SSN);
2353 if (expectAndConsume(MIToken::rparen))
2354 return error("expected ')' in syncscope");
2355 }
2356
2357 return false;
2358}
2359
2360bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2361 Order = AtomicOrdering::NotAtomic;
2362 if (Token.isNot(MIToken::Identifier))
2363 return false;
2364
2365 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2366 .Case("unordered", AtomicOrdering::Unordered)
2367 .Case("monotonic", AtomicOrdering::Monotonic)
2368 .Case("acquire", AtomicOrdering::Acquire)
2369 .Case("release", AtomicOrdering::Release)
2370 .Case("acq_rel", AtomicOrdering::AcquireRelease)
2371 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2372 .Default(AtomicOrdering::NotAtomic);
2373
2374 if (Order != AtomicOrdering::NotAtomic) {
2375 lex();
2376 return false;
2377 }
2378
2379 return error("expected an atomic scope, ordering or a size integer literal");
2380}
2381
2382bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2383 if (expectAndConsume(MIToken::lparen))
2384 return true;
2385 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
2386 while (Token.isMemoryOperandFlag()) {
2387 if (parseMemoryOperandFlag(Flags))
2388 return true;
2389 }
2390 if (Token.isNot(MIToken::Identifier) ||
2391 (Token.stringValue() != "load" && Token.stringValue() != "store"))
2392 return error("expected 'load' or 'store' memory operation");
2393 if (Token.stringValue() == "load")
2394 Flags |= MachineMemOperand::MOLoad;
2395 else
2396 Flags |= MachineMemOperand::MOStore;
2397 lex();
2398
2399 // Optional 'store' for operands that both load and store.
2400 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
2401 Flags |= MachineMemOperand::MOStore;
2402 lex();
2403 }
2404
2405 // Optional synchronization scope.
2406 SyncScope::ID SSID;
2407 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
2408 return true;
2409
2410 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2411 AtomicOrdering Order, FailureOrder;
2412 if (parseOptionalAtomicOrdering(Order))
2413 return true;
2414
2415 if (parseOptionalAtomicOrdering(FailureOrder))
2416 return true;
2417
2418 if (Token.isNot(MIToken::IntegerLiteral))
2419 return error("expected the size integer literal after memory operation");
2420 uint64_t Size;
2421 if (getUint64(Size))
2422 return true;
2423 lex();
2424
2425 MachinePointerInfo Ptr = MachinePointerInfo();
2426 if (Token.is(MIToken::Identifier)) {
2427 const char *Word =
2428 ((Flags & MachineMemOperand::MOLoad) &&
2429 (Flags & MachineMemOperand::MOStore))
2430 ? "on"
2431 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
2432 if (Token.stringValue() != Word)
2433 return error(Twine("expected '") + Word + "'");
2434 lex();
2435
2436 if (parseMachinePointerInfo(Ptr))
2437 return true;
2438 }
2439 unsigned BaseAlignment = Size;
2440 AAMDNodes AAInfo;
2441 MDNode *Range = nullptr;
2442 while (consumeIfPresent(MIToken::comma)) {
2443 switch (Token.kind()) {
2444 case MIToken::kw_align:
2445 if (parseAlignment(BaseAlignment))
2446 return true;
2447 break;
2448 case MIToken::kw_addrspace:
2449 if (parseAddrspace(Ptr.AddrSpace))
2450 return true;
2451 break;
2452 case MIToken::md_tbaa:
2453 lex();
2454 if (parseMDNode(AAInfo.TBAA))
2455 return true;
2456 break;
2457 case MIToken::md_alias_scope:
2458 lex();
2459 if (parseMDNode(AAInfo.Scope))
2460 return true;
2461 break;
2462 case MIToken::md_noalias:
2463 lex();
2464 if (parseMDNode(AAInfo.NoAlias))
2465 return true;
2466 break;
2467 case MIToken::md_range:
2468 lex();
2469 if (parseMDNode(Range))
2470 return true;
2471 break;
2472 // TODO: Report an error on duplicate metadata nodes.
2473 default:
2474 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2475 "'!noalias' or '!range'");
2476 }
2477 }
2478 if (expectAndConsume(MIToken::rparen))
2479 return true;
2480 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2481 SSID, Order, FailureOrder);
2482 return false;
2483}
2484
2485void MIParser::initNames2InstrOpCodes() {
2486 if (!Names2InstrOpCodes.empty())
2487 return;
2488 const auto *TII = MF.getSubtarget().getInstrInfo();
2489 assert(TII && "Expected target instruction info")(static_cast <bool> (TII && "Expected target instruction info"
) ? void (0) : __assert_fail ("TII && \"Expected target instruction info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2489, __extension__ __PRETTY_FUNCTION__))
;
2490 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2491 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2492}
2493
2494bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2495 initNames2InstrOpCodes();
2496 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2497 if (InstrInfo == Names2InstrOpCodes.end())
2498 return true;
2499 OpCode = InstrInfo->getValue();
2500 return false;
2501}
2502
2503void MIParser::initNames2Regs() {
2504 if (!Names2Regs.empty())
2505 return;
2506 // The '%noreg' register is the register 0.
2507 Names2Regs.insert(std::make_pair("noreg", 0));
2508 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2509 assert(TRI && "Expected target register info")(static_cast <bool> (TRI && "Expected target register info"
) ? void (0) : __assert_fail ("TRI && \"Expected target register info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2509, __extension__ __PRETTY_FUNCTION__))
;
2510 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2511 bool WasInserted =
2512 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2513 .second;
2514 (void)WasInserted;
2515 assert(WasInserted && "Expected registers to be unique case-insensitively")(static_cast <bool> (WasInserted && "Expected registers to be unique case-insensitively"
) ? void (0) : __assert_fail ("WasInserted && \"Expected registers to be unique case-insensitively\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2515, __extension__ __PRETTY_FUNCTION__))
;
2516 }
2517}
2518
2519bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2520 initNames2Regs();
2521 auto RegInfo = Names2Regs.find(RegName);
2522 if (RegInfo == Names2Regs.end())
2523 return true;
2524 Reg = RegInfo->getValue();
2525 return false;
2526}
2527
2528void MIParser::initNames2RegMasks() {
2529 if (!Names2RegMasks.empty())
2530 return;
2531 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2532 assert(TRI && "Expected target register info")(static_cast <bool> (TRI && "Expected target register info"
) ? void (0) : __assert_fail ("TRI && \"Expected target register info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2532, __extension__ __PRETTY_FUNCTION__))
;
2533 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2534 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2535 assert(RegMasks.size() == RegMaskNames.size())(static_cast <bool> (RegMasks.size() == RegMaskNames.size
()) ? void (0) : __assert_fail ("RegMasks.size() == RegMaskNames.size()"
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2535, __extension__ __PRETTY_FUNCTION__))
;
2536 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2537 Names2RegMasks.insert(
2538 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2539}
2540
2541const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2542 initNames2RegMasks();
2543 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2544 if (RegMaskInfo == Names2RegMasks.end())
2545 return nullptr;
2546 return RegMaskInfo->getValue();
2547}
2548
2549void MIParser::initNames2SubRegIndices() {
2550 if (!Names2SubRegIndices.empty())
2551 return;
2552 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2553 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2554 Names2SubRegIndices.insert(
2555 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2556}
2557
2558unsigned MIParser::getSubRegIndex(StringRef Name) {
2559 initNames2SubRegIndices();
2560 auto SubRegInfo = Names2SubRegIndices.find(Name);
2561 if (SubRegInfo == Names2SubRegIndices.end())
2562 return 0;
2563 return SubRegInfo->getValue();
2564}
2565
2566static void initSlots2BasicBlocks(
2567 const Function &F,
2568 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2569 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2570 MST.incorporateFunction(F);
2571 for (auto &BB : F) {
2572 if (BB.hasName())
2573 continue;
2574 int Slot = MST.getLocalSlot(&BB);
2575 if (Slot == -1)
2576 continue;
2577 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2578 }
2579}
2580
2581static const BasicBlock *getIRBlockFromSlot(
2582 unsigned Slot,
2583 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2584 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2585 if (BlockInfo == Slots2BasicBlocks.end())
2586 return nullptr;
2587 return BlockInfo->second;
2588}
2589
2590const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2591 if (Slots2BasicBlocks.empty())
2592 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
2593 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2594}
2595
2596const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2597 if (&F == &MF.getFunction())
2598 return getIRBlock(Slot);
2599 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2600 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2601 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2602}
2603
2604static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2605 DenseMap<unsigned, const Value *> &Slots2Values) {
2606 int Slot = MST.getLocalSlot(V);
2607 if (Slot == -1)
2608 return;
2609 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2610}
2611
2612/// Creates the mapping from slot numbers to function's unnamed IR values.
2613static void initSlots2Values(const Function &F,
2614 DenseMap<unsigned, const Value *> &Slots2Values) {
2615 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2616 MST.incorporateFunction(F);
2617 for (const auto &Arg : F.args())
2618 mapValueToSlot(&Arg, MST, Slots2Values);
2619 for (const auto &BB : F) {
2620 mapValueToSlot(&BB, MST, Slots2Values);
2621 for (const auto &I : BB)
2622 mapValueToSlot(&I, MST, Slots2Values);
2623 }
2624}
2625
2626const Value *MIParser::getIRValue(unsigned Slot) {
2627 if (Slots2Values.empty())
2628 initSlots2Values(MF.getFunction(), Slots2Values);
2629 auto ValueInfo = Slots2Values.find(Slot);
2630 if (ValueInfo == Slots2Values.end())
2631 return nullptr;
2632 return ValueInfo->second;
2633}
2634
2635void MIParser::initNames2TargetIndices() {
2636 if (!Names2TargetIndices.empty())
2637 return;
2638 const auto *TII = MF.getSubtarget().getInstrInfo();
2639 assert(TII && "Expected target instruction info")(static_cast <bool> (TII && "Expected target instruction info"
) ? void (0) : __assert_fail ("TII && \"Expected target instruction info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2639, __extension__ __PRETTY_FUNCTION__))
;
2640 auto Indices = TII->getSerializableTargetIndices();
2641 for (const auto &I : Indices)
2642 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2643}
2644
2645bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2646 initNames2TargetIndices();
2647 auto IndexInfo = Names2TargetIndices.find(Name);
2648 if (IndexInfo == Names2TargetIndices.end())
2649 return true;
2650 Index = IndexInfo->second;
2651 return false;
2652}
2653
2654void MIParser::initNames2DirectTargetFlags() {
2655 if (!Names2DirectTargetFlags.empty())
2656 return;
2657 const auto *TII = MF.getSubtarget().getInstrInfo();
2658 assert(TII && "Expected target instruction info")(static_cast <bool> (TII && "Expected target instruction info"
) ? void (0) : __assert_fail ("TII && \"Expected target instruction info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2658, __extension__ __PRETTY_FUNCTION__))
;
2659 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2660 for (const auto &I : Flags)
2661 Names2DirectTargetFlags.insert(
2662 std::make_pair(StringRef(I.second), I.first));
2663}
2664
2665bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2666 initNames2DirectTargetFlags();
2667 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2668 if (FlagInfo == Names2DirectTargetFlags.end())
2669 return true;
2670 Flag = FlagInfo->second;
2671 return false;
2672}
2673
2674void MIParser::initNames2BitmaskTargetFlags() {
2675 if (!Names2BitmaskTargetFlags.empty())
2676 return;
2677 const auto *TII = MF.getSubtarget().getInstrInfo();
2678 assert(TII && "Expected target instruction info")(static_cast <bool> (TII && "Expected target instruction info"
) ? void (0) : __assert_fail ("TII && \"Expected target instruction info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2678, __extension__ __PRETTY_FUNCTION__))
;
2679 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2680 for (const auto &I : Flags)
2681 Names2BitmaskTargetFlags.insert(
2682 std::make_pair(StringRef(I.second), I.first));
2683}
2684
2685bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2686 initNames2BitmaskTargetFlags();
2687 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2688 if (FlagInfo == Names2BitmaskTargetFlags.end())
2689 return true;
2690 Flag = FlagInfo->second;
2691 return false;
2692}
2693
2694void MIParser::initNames2MMOTargetFlags() {
2695 if (!Names2MMOTargetFlags.empty())
2696 return;
2697 const auto *TII = MF.getSubtarget().getInstrInfo();
2698 assert(TII && "Expected target instruction info")(static_cast <bool> (TII && "Expected target instruction info"
) ? void (0) : __assert_fail ("TII && \"Expected target instruction info\""
, "/build/llvm-toolchain-snapshot-7~svn329677/lib/CodeGen/MIRParser/MIParser.cpp"
, 2698, __extension__ __PRETTY_FUNCTION__))
;
2699 auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
2700 for (const auto &I : Flags)
2701 Names2MMOTargetFlags.insert(
2702 std::make_pair(StringRef(I.second), I.first));
2703}
2704
2705bool MIParser::getMMOTargetFlag(StringRef Name,
2706 MachineMemOperand::Flags &Flag) {
2707 initNames2MMOTargetFlags();
2708 auto FlagInfo = Names2MMOTargetFlags.find(Name);
2709 if (FlagInfo == Names2MMOTargetFlags.end())
2710 return true;
2711 Flag = FlagInfo->second;
2712 return false;
2713}
2714
2715bool MIParser::parseStringConstant(std::string &Result) {
2716 if (Token.isNot(MIToken::StringConstant))
2717 return error("expected string constant");
2718 Result = Token.stringValue();
2719 lex();
2720 return false;
2721}
2722
2723bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2724 StringRef Src,
2725 SMDiagnostic &Error) {
2726 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2727}
2728
2729bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
2730 StringRef Src, SMDiagnostic &Error) {
2731 return MIParser(PFS, Error, Src).parseBasicBlocks();
1
Calling 'MIParser::parseBasicBlocks'
2732}
2733
2734bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
2735 MachineBasicBlock *&MBB, StringRef Src,
2736 SMDiagnostic &Error) {
2737 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
2738}
2739
2740bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
2741 unsigned &Reg, StringRef Src,
2742 SMDiagnostic &Error) {
2743 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2744}
2745
2746bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
2747 unsigned &Reg, StringRef Src,
2748 SMDiagnostic &Error) {
2749 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
2750}
2751
2752bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2753 VRegInfo *&Info, StringRef Src,
2754 SMDiagnostic &Error) {
2755 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
2756}
2757
2758bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
2759 int &FI, StringRef Src,
2760 SMDiagnostic &Error) {
2761 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
2762}
2763
2764bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
2765 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2766 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
2767}