Bug Summary

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