Bug Summary

File:llvm/lib/CodeGen/MIRParser/MIParser.cpp
Warning:line 1863, column 15
The left operand of '>=' is a garbage 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 -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/CodeGen/MIRParser -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/CodeGen/MIRParser -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/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/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.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++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/CodeGen/MIRParser -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/CodeGen/MIRParser/MIParser.cpp

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/CodeGen/MIRParser/MIParser.cpp

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

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/CodeGen/MIRParser/MILexer.h

1//===- MILexer.h - Lexer for machine instructions ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the function that lexes the machine instruction source
10// string.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
15#define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringRef.h"
20#include <string>
21
22namespace llvm {
23
24class Twine;
25
26/// A token produced by the machine instruction lexer.
27struct MIToken {
28 enum TokenKind {
29 // Markers
30 Eof,
31 Error,
32 Newline,
33
34 // Tokens with no info.
35 comma,
36 equal,
37 underscore,
38 colon,
39 coloncolon,
40 dot,
41 exclaim,
42 lparen,
43 rparen,
44 lbrace,
45 rbrace,
46 plus,
47 minus,
48 less,
49 greater,
50
51 // Keywords
52 kw_implicit,
53 kw_implicit_define,
54 kw_def,
55 kw_dead,
56 kw_dereferenceable,
57 kw_killed,
58 kw_undef,
59 kw_internal,
60 kw_early_clobber,
61 kw_debug_use,
62 kw_renamable,
63 kw_tied_def,
64 kw_frame_setup,
65 kw_frame_destroy,
66 kw_nnan,
67 kw_ninf,
68 kw_nsz,
69 kw_arcp,
70 kw_contract,
71 kw_afn,
72 kw_reassoc,
73 kw_nuw,
74 kw_nsw,
75 kw_exact,
76 kw_nofpexcept,
77 kw_debug_location,
78 kw_cfi_same_value,
79 kw_cfi_offset,
80 kw_cfi_rel_offset,
81 kw_cfi_def_cfa_register,
82 kw_cfi_def_cfa_offset,
83 kw_cfi_adjust_cfa_offset,
84 kw_cfi_escape,
85 kw_cfi_def_cfa,
86 kw_cfi_register,
87 kw_cfi_remember_state,
88 kw_cfi_restore,
89 kw_cfi_restore_state,
90 kw_cfi_undefined,
91 kw_cfi_window_save,
92 kw_cfi_aarch64_negate_ra_sign_state,
93 kw_blockaddress,
94 kw_intrinsic,
95 kw_target_index,
96 kw_half,
97 kw_float,
98 kw_double,
99 kw_x86_fp80,
100 kw_fp128,
101 kw_ppc_fp128,
102 kw_target_flags,
103 kw_volatile,
104 kw_non_temporal,
105 kw_invariant,
106 kw_align,
107 kw_addrspace,
108 kw_stack,
109 kw_got,
110 kw_jump_table,
111 kw_constant_pool,
112 kw_call_entry,
113 kw_custom,
114 kw_liveout,
115 kw_address_taken,
116 kw_landing_pad,
117 kw_liveins,
118 kw_successors,
119 kw_floatpred,
120 kw_intpred,
121 kw_shufflemask,
122 kw_pre_instr_symbol,
123 kw_post_instr_symbol,
124 kw_heap_alloc_marker,
125 kw_unknown_size,
126
127 // Named metadata keywords
128 md_tbaa,
129 md_alias_scope,
130 md_noalias,
131 md_range,
132 md_diexpr,
133 md_dilocation,
134
135 // Identifier tokens
136 Identifier,
137 NamedRegister,
138 NamedVirtualRegister,
139 MachineBasicBlockLabel,
140 MachineBasicBlock,
141 StackObject,
142 FixedStackObject,
143 NamedGlobalValue,
144 GlobalValue,
145 ExternalSymbol,
146 MCSymbol,
147
148 // Other tokens
149 IntegerLiteral,
150 FloatingPointLiteral,
151 HexLiteral,
152 VectorLiteral,
153 VirtualRegister,
154 ConstantPoolItem,
155 JumpTableIndex,
156 NamedIRBlock,
157 IRBlock,
158 NamedIRValue,
159 IRValue,
160 QuotedIRValue, // `<constant value>`
161 SubRegisterIndex,
162 StringConstant
163 };
164
165private:
166 TokenKind Kind = Error;
167 StringRef Range;
168 StringRef StringValue;
169 std::string StringValueStorage;
170 APSInt IntVal;
171
172public:
173 MIToken() = default;
174
175 MIToken &reset(TokenKind Kind, StringRef Range);
176
177 MIToken &setStringValue(StringRef StrVal);
178 MIToken &setOwnedStringValue(std::string StrVal);
179 MIToken &setIntegerValue(APSInt IntVal);
180
181 TokenKind kind() const { return Kind; }
5
Returning the value 106, which participates in a condition later
182
183 bool isError() const { return Kind == Error; }
184
185 bool isNewlineOrEOF() const { return Kind == Newline || Kind == Eof; }
186
187 bool isErrorOrEOF() const { return Kind == Error || Kind == Eof; }
188
189 bool isRegister() const {
190 return Kind == NamedRegister || Kind == underscore ||
191 Kind == NamedVirtualRegister || Kind == VirtualRegister;
192 }
193
194 bool isRegisterFlag() const {
195 return Kind == kw_implicit || Kind == kw_implicit_define ||
196 Kind == kw_def || Kind == kw_dead || Kind == kw_killed ||
197 Kind == kw_undef || Kind == kw_internal ||
198 Kind == kw_early_clobber || Kind == kw_debug_use ||
199 Kind == kw_renamable;
200 }
201
202 bool isMemoryOperandFlag() const {
203 return Kind == kw_volatile || Kind == kw_non_temporal ||
204 Kind == kw_dereferenceable || Kind == kw_invariant ||
205 Kind == StringConstant;
206 }
207
208 bool is(TokenKind K) const { return Kind == K; }
209
210 bool isNot(TokenKind K) const { return Kind != K; }
211
212 StringRef::iterator location() const { return Range.begin(); }
213
214 StringRef range() const { return Range; }
215
216 /// Return the token's string value.
217 StringRef stringValue() const { return StringValue; }
218
219 const APSInt &integerValue() const { return IntVal; }
220
221 bool hasIntegerValue() const {
222 return Kind
10.1
Field 'Kind' is not equal to IntegerLiteral
10.1
Field 'Kind' is not equal to IntegerLiteral
== IntegerLiteral || Kind
10.2
Field 'Kind' is not equal to MachineBasicBlock
10.2
Field 'Kind' is not equal to MachineBasicBlock
== MachineBasicBlock ||
11
Returning the value 1, which participates in a condition later
223 Kind
10.3
Field 'Kind' is not equal to MachineBasicBlockLabel
10.3
Field 'Kind' is not equal to MachineBasicBlockLabel
== MachineBasicBlockLabel || Kind
10.4
Field 'Kind' is not equal to StackObject
10.4
Field 'Kind' is not equal to StackObject
== StackObject ||
224 Kind
10.5
Field 'Kind' is not equal to FixedStackObject
10.5
Field 'Kind' is not equal to FixedStackObject
== FixedStackObject || Kind
10.6
Field 'Kind' is equal to GlobalValue
10.6
Field 'Kind' is equal to GlobalValue
== GlobalValue ||
225 Kind == VirtualRegister || Kind == ConstantPoolItem ||
226 Kind == JumpTableIndex || Kind == IRBlock || Kind == IRValue;
227 }
228};
229
230/// Consume a single machine instruction token in the given source and return
231/// the remaining source string.
232StringRef lexMIToken(
233 StringRef Source, MIToken &Token,
234 function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
235
236} // end namespace llvm
237
238#endif // LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H