LLVM 22.0.0git
MachineVerifier.cpp
Go to the documentation of this file.
1//===- MachineVerifier.cpp - Machine Code Verifier ------------------------===//
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// Pass to verify generated machine code. The following is checked:
10//
11// Operand counts: All explicit operands must be present.
12//
13// Register classes: All physical and virtual register operands must be
14// compatible with the register class required by the instruction descriptor.
15//
16// Register live intervals: Registers must be defined only once, and must be
17// defined before use.
18//
19// The machine code verifier is enabled with the command-line option
20// -verify-machineinstrs.
21//===----------------------------------------------------------------------===//
22
24#include "llvm/ADT/BitVector.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/DenseSet.h"
29#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/ADT/Twine.h"
64#include "llvm/IR/BasicBlock.h"
65#include "llvm/IR/Constants.h"
67#include "llvm/IR/Function.h"
68#include "llvm/IR/InlineAsm.h"
71#include "llvm/MC/LaneBitmask.h"
72#include "llvm/MC/MCAsmInfo.h"
73#include "llvm/MC/MCDwarf.h"
74#include "llvm/MC/MCInstrDesc.h"
77#include "llvm/Pass.h"
82#include "llvm/Support/ModRef.h"
83#include "llvm/Support/Mutex.h"
86#include <algorithm>
87#include <cassert>
88#include <cstddef>
89#include <cstdint>
90#include <iterator>
91#include <string>
92#include <utility>
93
94using namespace llvm;
95
96namespace {
97
98/// Used the by the ReportedErrors class to guarantee only one error is reported
99/// at one time.
100static ManagedStatic<sys::SmartMutex<true>> ReportedErrorsLock;
101
102struct MachineVerifier {
103 MachineVerifier(MachineFunctionAnalysisManager &MFAM, const char *b,
104 raw_ostream *OS, bool AbortOnError = true)
105 : MFAM(&MFAM), OS(OS ? *OS : nulls()), Banner(b),
106 ReportedErrs(AbortOnError) {}
107
108 MachineVerifier(Pass *pass, const char *b, raw_ostream *OS,
109 bool AbortOnError = true)
110 : PASS(pass), OS(OS ? *OS : nulls()), Banner(b),
111 ReportedErrs(AbortOnError) {}
112
113 MachineVerifier(const char *b, LiveVariables *LiveVars,
114 LiveIntervals *LiveInts, LiveStacks *LiveStks,
115 SlotIndexes *Indexes, raw_ostream *OS,
116 bool AbortOnError = true)
117 : OS(OS ? *OS : nulls()), Banner(b), LiveVars(LiveVars),
118 LiveInts(LiveInts), LiveStks(LiveStks), Indexes(Indexes),
119 ReportedErrs(AbortOnError) {}
120
121 /// \returns true if no problems were found.
122 bool verify(const MachineFunction &MF);
123
124 MachineFunctionAnalysisManager *MFAM = nullptr;
125 Pass *const PASS = nullptr;
126 raw_ostream &OS;
127 const char *Banner;
128 const MachineFunction *MF = nullptr;
129 const TargetMachine *TM = nullptr;
130 const TargetInstrInfo *TII = nullptr;
131 const TargetRegisterInfo *TRI = nullptr;
132 const MachineRegisterInfo *MRI = nullptr;
133 const RegisterBankInfo *RBI = nullptr;
134
135 // Avoid querying the MachineFunctionProperties for each operand.
136 bool isFunctionRegBankSelected = false;
137 bool isFunctionSelected = false;
138 bool isFunctionTracksDebugUserValues = false;
139
140 using RegVector = SmallVector<Register, 16>;
141 using RegMaskVector = SmallVector<const uint32_t *, 4>;
142 using RegSet = DenseSet<Register>;
143 using RegMap = DenseMap<Register, const MachineInstr *>;
144 using BlockSet = SmallPtrSet<const MachineBasicBlock *, 8>;
145
146 const MachineInstr *FirstNonPHI = nullptr;
147 const MachineInstr *FirstTerminator = nullptr;
148 BlockSet FunctionBlocks;
149
150 BitVector regsReserved;
151 RegSet regsLive;
152 RegVector regsDefined, regsDead, regsKilled;
153 RegMaskVector regMasks;
154
155 SlotIndex lastIndex;
156
157 // Add Reg and any sub-registers to RV
158 void addRegWithSubRegs(RegVector &RV, Register Reg) {
159 RV.push_back(Reg);
160 if (Reg.isPhysical())
161 append_range(RV, TRI->subregs(Reg.asMCReg()));
162 }
163
164 struct BBInfo {
165 // Is this MBB reachable from the MF entry point?
166 bool reachable = false;
167
168 // Vregs that must be live in because they are used without being
169 // defined. Map value is the user. vregsLiveIn doesn't include regs
170 // that only are used by PHI nodes.
171 RegMap vregsLiveIn;
172
173 // Regs killed in MBB. They may be defined again, and will then be in both
174 // regsKilled and regsLiveOut.
175 RegSet regsKilled;
176
177 // Regs defined in MBB and live out. Note that vregs passing through may
178 // be live out without being mentioned here.
179 RegSet regsLiveOut;
180
181 // Vregs that pass through MBB untouched. This set is disjoint from
182 // regsKilled and regsLiveOut.
183 RegSet vregsPassed;
184
185 // Vregs that must pass through MBB because they are needed by a successor
186 // block. This set is disjoint from regsLiveOut.
187 RegSet vregsRequired;
188
189 // Set versions of block's predecessor and successor lists.
190 BlockSet Preds, Succs;
191
192 BBInfo() = default;
193
194 // Add register to vregsRequired if it belongs there. Return true if
195 // anything changed.
196 bool addRequired(Register Reg) {
197 if (!Reg.isVirtual())
198 return false;
199 if (regsLiveOut.count(Reg))
200 return false;
201 return vregsRequired.insert(Reg).second;
202 }
203
204 // Same for a full set.
205 bool addRequired(const RegSet &RS) {
206 bool Changed = false;
207 for (Register Reg : RS)
208 Changed |= addRequired(Reg);
209 return Changed;
210 }
211
212 // Same for a full map.
213 bool addRequired(const RegMap &RM) {
214 bool Changed = false;
215 for (const auto &I : RM)
216 Changed |= addRequired(I.first);
217 return Changed;
218 }
219
220 // Live-out registers are either in regsLiveOut or vregsPassed.
221 bool isLiveOut(Register Reg) const {
222 return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
223 }
224 };
225
226 // Extra register info per MBB.
227 DenseMap<const MachineBasicBlock *, BBInfo> MBBInfoMap;
228
229 bool isReserved(Register Reg) {
230 return Reg.id() < regsReserved.size() && regsReserved.test(Reg.id());
231 }
232
233 bool isAllocatable(Register Reg) const {
234 return Reg.id() < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
235 !regsReserved.test(Reg.id());
236 }
237
238 // Analysis information if available
239 LiveVariables *LiveVars = nullptr;
240 LiveIntervals *LiveInts = nullptr;
241 LiveStacks *LiveStks = nullptr;
242 SlotIndexes *Indexes = nullptr;
243
244 /// A class to track the number of reported error and to guarantee that only
245 /// one error is reported at one time.
246 class ReportedErrors {
247 unsigned NumReported = 0;
248 bool AbortOnError;
249
250 public:
251 /// \param AbortOnError -- If set, abort after printing the first error.
252 ReportedErrors(bool AbortOnError) : AbortOnError(AbortOnError) {}
253
254 ~ReportedErrors() {
255 if (!hasError())
256 return;
257 if (AbortOnError)
258 report_fatal_error("Found " + Twine(NumReported) +
259 " machine code errors.");
260 // Since we haven't aborted, release the lock to allow other threads to
261 // report errors.
262 ReportedErrorsLock->unlock();
263 }
264
265 /// Increment the number of reported errors.
266 /// \returns true if this is the first reported error.
267 bool increment() {
268 // If this is the first error this thread has encountered, grab the lock
269 // to prevent other threads from reporting errors at the same time.
270 // Otherwise we assume we already have the lock.
271 if (!hasError())
272 ReportedErrorsLock->lock();
273 ++NumReported;
274 return NumReported == 1;
275 }
276
277 /// \returns true if an error was reported.
278 bool hasError() { return NumReported; }
279 };
280 ReportedErrors ReportedErrs;
281
282 // This is calculated only when trying to verify convergence control tokens.
283 // Similar to the LLVM IR verifier, we calculate this locally instead of
284 // relying on the pass manager.
285 MachineDominatorTree DT;
286
287 void visitMachineFunctionBefore();
288 void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
289 void visitMachineBundleBefore(const MachineInstr *MI);
290
291 /// Verify that all of \p MI's virtual register operands are scalars.
292 /// \returns True if all virtual register operands are scalar. False
293 /// otherwise.
294 bool verifyAllRegOpsScalar(const MachineInstr &MI,
295 const MachineRegisterInfo &MRI);
296 bool verifyVectorElementMatch(LLT Ty0, LLT Ty1, const MachineInstr *MI);
297
298 bool verifyGIntrinsicSideEffects(const MachineInstr *MI);
299 bool verifyGIntrinsicConvergence(const MachineInstr *MI);
300 void verifyPreISelGenericInstruction(const MachineInstr *MI);
301
302 void visitMachineInstrBefore(const MachineInstr *MI);
303 void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
304 void visitMachineBundleAfter(const MachineInstr *MI);
305 void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
306 void visitMachineFunctionAfter();
307
308 void report(const char *msg, const MachineFunction *MF);
309 void report(const char *msg, const MachineBasicBlock *MBB);
310 void report(const char *msg, const MachineInstr *MI);
311 void report(const char *msg, const MachineOperand *MO, unsigned MONum,
312 LLT MOVRegType = LLT{});
313 void report(const Twine &Msg, const MachineInstr *MI);
314
315 void report_context(const LiveInterval &LI) const;
316 void report_context(const LiveRange &LR, VirtRegOrUnit VRegOrUnit,
317 LaneBitmask LaneMask) const;
318 void report_context(const LiveRange::Segment &S) const;
319 void report_context(const VNInfo &VNI) const;
320 void report_context(SlotIndex Pos) const;
321 void report_context(MCPhysReg PhysReg) const;
322 void report_context_liverange(const LiveRange &LR) const;
323 void report_context_lanemask(LaneBitmask LaneMask) const;
324 void report_context_vreg(Register VReg) const;
325 void report_context_vreg_regunit(VirtRegOrUnit VRegOrUnit) const;
326
327 void verifyInlineAsm(const MachineInstr *MI);
328
329 void checkLiveness(const MachineOperand *MO, unsigned MONum);
330 void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
331 SlotIndex UseIdx, const LiveRange &LR,
332 VirtRegOrUnit VRegOrUnit,
333 LaneBitmask LaneMask = LaneBitmask::getNone());
334 void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
335 SlotIndex DefIdx, const LiveRange &LR,
336 VirtRegOrUnit VRegOrUnit, bool SubRangeCheck = false,
337 LaneBitmask LaneMask = LaneBitmask::getNone());
338
339 void markReachable(const MachineBasicBlock *MBB);
340 void calcRegsPassed();
341 void checkPHIOps(const MachineBasicBlock &MBB);
342
343 void calcRegsRequired();
344 void verifyLiveVariables();
345 void verifyLiveIntervals();
346 void verifyLiveInterval(const LiveInterval &);
347 void verifyLiveRangeValue(const LiveRange &, const VNInfo *, VirtRegOrUnit,
348 LaneBitmask);
349 void verifyLiveRangeSegment(const LiveRange &,
350 const LiveRange::const_iterator I, VirtRegOrUnit,
351 LaneBitmask);
352 void verifyLiveRange(const LiveRange &, VirtRegOrUnit,
353 LaneBitmask LaneMask = LaneBitmask::getNone());
354
355 void verifyStackFrame();
356 /// Check that the stack protector is the top-most object in the stack.
357 void verifyStackProtector();
358
359 void verifySlotIndexes() const;
360 void verifyProperties(const MachineFunction &MF);
361};
362
363struct MachineVerifierLegacyPass : public MachineFunctionPass {
364 static char ID; // Pass ID, replacement for typeid
365
366 const std::string Banner;
367
368 MachineVerifierLegacyPass(std::string banner = std::string())
369 : MachineFunctionPass(ID), Banner(std::move(banner)) {
371 }
372
373 void getAnalysisUsage(AnalysisUsage &AU) const override {
374 AU.addUsedIfAvailable<LiveStacksWrapperLegacy>();
375 AU.addUsedIfAvailable<LiveVariablesWrapperPass>();
376 AU.addUsedIfAvailable<SlotIndexesWrapperPass>();
377 AU.addUsedIfAvailable<LiveIntervalsWrapperPass>();
378 AU.setPreservesAll();
380 }
381
382 bool runOnMachineFunction(MachineFunction &MF) override {
383 // Skip functions that have known verification problems.
384 // FIXME: Remove this mechanism when all problematic passes have been
385 // fixed.
386 if (MF.getProperties().hasFailsVerification())
387 return false;
388
389 MachineVerifier(this, Banner.c_str(), &errs()).verify(MF);
390 return false;
391 }
392};
393
394} // end anonymous namespace
395
399 // Skip functions that have known verification problems.
400 // FIXME: Remove this mechanism when all problematic passes have been
401 // fixed.
402 if (MF.getProperties().hasFailsVerification())
403 return PreservedAnalyses::all();
404 MachineVerifier(MFAM, Banner.c_str(), &errs()).verify(MF);
405 return PreservedAnalyses::all();
406}
407
408char MachineVerifierLegacyPass::ID = 0;
409
410INITIALIZE_PASS(MachineVerifierLegacyPass, "machineverifier",
411 "Verify generated machine code", false, false)
412
414 return new MachineVerifierLegacyPass(Banner);
415}
416
417void llvm::verifyMachineFunction(const std::string &Banner,
418 const MachineFunction &MF) {
419 // TODO: Use MFAM after porting below analyses.
420 // LiveVariables *LiveVars;
421 // LiveIntervals *LiveInts;
422 // LiveStacks *LiveStks;
423 // SlotIndexes *Indexes;
424 MachineVerifier(nullptr, Banner.c_str(), &errs()).verify(MF);
425}
426
427bool MachineFunction::verify(Pass *p, const char *Banner, raw_ostream *OS,
428 bool AbortOnError) const {
429 return MachineVerifier(p, Banner, OS, AbortOnError).verify(*this);
430}
431
433 const char *Banner, raw_ostream *OS,
434 bool AbortOnError) const {
435 return MachineVerifier(MFAM, Banner, OS, AbortOnError).verify(*this);
436}
437
439 const char *Banner, raw_ostream *OS,
440 bool AbortOnError) const {
441 return MachineVerifier(Banner, /*LiveVars=*/nullptr, LiveInts,
442 /*LiveStks=*/nullptr, Indexes, OS, AbortOnError)
443 .verify(*this);
444}
445
446void MachineVerifier::verifySlotIndexes() const {
447 if (Indexes == nullptr)
448 return;
449
450 // Ensure the IdxMBB list is sorted by slot indexes.
453 E = Indexes->MBBIndexEnd(); I != E; ++I) {
454 assert(!Last.isValid() || I->first > Last);
455 Last = I->first;
456 }
457}
458
459void MachineVerifier::verifyProperties(const MachineFunction &MF) {
460 // If a pass has introduced virtual registers without clearing the
461 // NoVRegs property (or set it without allocating the vregs)
462 // then report an error.
463 if (MF.getProperties().hasNoVRegs() && MRI->getNumVirtRegs())
464 report("Function has NoVRegs property but there are VReg operands", &MF);
465}
466
467bool MachineVerifier::verify(const MachineFunction &MF) {
468 this->MF = &MF;
469 TM = &MF.getTarget();
472 RBI = MF.getSubtarget().getRegBankInfo();
473 MRI = &MF.getRegInfo();
474
475 const MachineFunctionProperties &Props = MF.getProperties();
476 const bool isFunctionFailedISel = Props.hasFailedISel();
477
478 // If we're mid-GlobalISel and we already triggered the fallback path then
479 // it's expected that the MIR is somewhat broken but that's ok since we'll
480 // reset it and clear the FailedISel attribute in ResetMachineFunctions.
481 if (isFunctionFailedISel)
482 return true;
483
484 isFunctionRegBankSelected = Props.hasRegBankSelected();
485 isFunctionSelected = Props.hasSelected();
486 isFunctionTracksDebugUserValues = Props.hasTracksDebugUserValues();
487
488 if (PASS) {
489 auto *LISWrapper = PASS->getAnalysisIfAvailable<LiveIntervalsWrapperPass>();
490 LiveInts = LISWrapper ? &LISWrapper->getLIS() : nullptr;
491 // We don't want to verify LiveVariables if LiveIntervals is available.
492 auto *LVWrapper = PASS->getAnalysisIfAvailable<LiveVariablesWrapperPass>();
493 if (!LiveInts)
494 LiveVars = LVWrapper ? &LVWrapper->getLV() : nullptr;
495 auto *LSWrapper = PASS->getAnalysisIfAvailable<LiveStacksWrapperLegacy>();
496 LiveStks = LSWrapper ? &LSWrapper->getLS() : nullptr;
497 auto *SIWrapper = PASS->getAnalysisIfAvailable<SlotIndexesWrapperPass>();
498 Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr;
499 }
500 if (MFAM) {
501 MachineFunction &Func = const_cast<MachineFunction &>(MF);
502 LiveInts = MFAM->getCachedResult<LiveIntervalsAnalysis>(Func);
503 if (!LiveInts)
504 LiveVars = MFAM->getCachedResult<LiveVariablesAnalysis>(Func);
505 // TODO: LiveStks = MFAM->getCachedResult<LiveStacksAnalysis>(Func);
506 Indexes = MFAM->getCachedResult<SlotIndexesAnalysis>(Func);
507 }
508
509 verifySlotIndexes();
510
511 verifyProperties(MF);
512
513 visitMachineFunctionBefore();
514 for (const MachineBasicBlock &MBB : MF) {
515 visitMachineBasicBlockBefore(&MBB);
516 // Keep track of the current bundle header.
517 const MachineInstr *CurBundle = nullptr;
518 // Do we expect the next instruction to be part of the same bundle?
519 bool InBundle = false;
520
521 for (const MachineInstr &MI : MBB.instrs()) {
522 if (MI.getParent() != &MBB) {
523 report("Bad instruction parent pointer", &MBB);
524 OS << "Instruction: " << MI;
525 continue;
526 }
527
528 // Check for consistent bundle flags.
529 if (InBundle && !MI.isBundledWithPred())
530 report("Missing BundledPred flag, "
531 "BundledSucc was set on predecessor",
532 &MI);
533 if (!InBundle && MI.isBundledWithPred())
534 report("BundledPred flag is set, "
535 "but BundledSucc not set on predecessor",
536 &MI);
537
538 // Is this a bundle header?
539 if (!MI.isInsideBundle()) {
540 if (CurBundle)
541 visitMachineBundleAfter(CurBundle);
542 CurBundle = &MI;
543 visitMachineBundleBefore(CurBundle);
544 } else if (!CurBundle)
545 report("No bundle header", &MI);
546 visitMachineInstrBefore(&MI);
547 for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
548 const MachineOperand &Op = MI.getOperand(I);
549 if (Op.getParent() != &MI) {
550 // Make sure to use correct addOperand / removeOperand / ChangeTo
551 // functions when replacing operands of a MachineInstr.
552 report("Instruction has operand with wrong parent set", &MI);
553 }
554
555 visitMachineOperand(&Op, I);
556 }
557
558 // Was this the last bundled instruction?
559 InBundle = MI.isBundledWithSucc();
560 }
561 if (CurBundle)
562 visitMachineBundleAfter(CurBundle);
563 if (InBundle)
564 report("BundledSucc flag set on last instruction in block", &MBB.back());
565 visitMachineBasicBlockAfter(&MBB);
566 }
567 visitMachineFunctionAfter();
568
569 // Clean up.
570 regsLive.clear();
571 regsDefined.clear();
572 regsDead.clear();
573 regsKilled.clear();
574 regMasks.clear();
575 MBBInfoMap.clear();
576
577 return !ReportedErrs.hasError();
578}
579
580void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
581 assert(MF);
582 OS << '\n';
583 if (ReportedErrs.increment()) {
584 if (Banner)
585 OS << "# " << Banner << '\n';
586
587 if (LiveInts != nullptr)
588 LiveInts->print(OS);
589 else
590 MF->print(OS, Indexes);
591 }
592
593 OS << "*** Bad machine code: " << msg << " ***\n"
594 << "- function: " << MF->getName() << '\n';
595}
596
597void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
598 assert(MBB);
599 report(msg, MBB->getParent());
600 OS << "- basic block: " << printMBBReference(*MBB) << ' ' << MBB->getName()
601 << " (" << (const void *)MBB << ')';
602 if (Indexes)
603 OS << " [" << Indexes->getMBBStartIdx(MBB) << ';'
604 << Indexes->getMBBEndIdx(MBB) << ')';
605 OS << '\n';
606}
607
608void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
609 assert(MI);
610 report(msg, MI->getParent());
611 OS << "- instruction: ";
612 if (Indexes && Indexes->hasIndex(*MI))
613 OS << Indexes->getInstructionIndex(*MI) << '\t';
614 MI->print(OS, /*IsStandalone=*/true);
615}
616
617void MachineVerifier::report(const char *msg, const MachineOperand *MO,
618 unsigned MONum, LLT MOVRegType) {
619 assert(MO);
620 report(msg, MO->getParent());
621 OS << "- operand " << MONum << ": ";
622 MO->print(OS, MOVRegType, TRI);
623 OS << '\n';
624}
625
626void MachineVerifier::report(const Twine &Msg, const MachineInstr *MI) {
627 report(Msg.str().c_str(), MI);
628}
629
630void MachineVerifier::report_context(SlotIndex Pos) const {
631 OS << "- at: " << Pos << '\n';
632}
633
634void MachineVerifier::report_context(const LiveInterval &LI) const {
635 OS << "- interval: " << LI << '\n';
636}
637
638void MachineVerifier::report_context(const LiveRange &LR,
639 VirtRegOrUnit VRegOrUnit,
640 LaneBitmask LaneMask) const {
641 report_context_liverange(LR);
642 report_context_vreg_regunit(VRegOrUnit);
643 if (LaneMask.any())
644 report_context_lanemask(LaneMask);
645}
646
647void MachineVerifier::report_context(const LiveRange::Segment &S) const {
648 OS << "- segment: " << S << '\n';
649}
650
651void MachineVerifier::report_context(const VNInfo &VNI) const {
652 OS << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
653}
654
655void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
656 OS << "- liverange: " << LR << '\n';
657}
658
659void MachineVerifier::report_context(MCPhysReg PReg) const {
660 OS << "- p. register: " << printReg(PReg, TRI) << '\n';
661}
662
663void MachineVerifier::report_context_vreg(Register VReg) const {
664 OS << "- v. register: " << printReg(VReg, TRI) << '\n';
665}
666
667void MachineVerifier::report_context_vreg_regunit(
668 VirtRegOrUnit VRegOrUnit) const {
669 if (VRegOrUnit.isVirtualReg()) {
670 report_context_vreg(VRegOrUnit.asVirtualReg());
671 } else {
672 OS << "- regunit: " << printRegUnit(VRegOrUnit.asMCRegUnit(), TRI)
673 << '\n';
674 }
675}
676
677void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
678 OS << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
679}
680
681void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
682 BBInfo &MInfo = MBBInfoMap[MBB];
683 if (!MInfo.reachable) {
684 MInfo.reachable = true;
685 for (const MachineBasicBlock *Succ : MBB->successors())
686 markReachable(Succ);
687 }
688}
689
690void MachineVerifier::visitMachineFunctionBefore() {
691 lastIndex = SlotIndex();
692 regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
693 : TRI->getReservedRegs(*MF);
694
695 if (!MF->empty())
696 markReachable(&MF->front());
697
698 // Build a set of the basic blocks in the function.
699 FunctionBlocks.clear();
700 for (const auto &MBB : *MF) {
701 FunctionBlocks.insert(&MBB);
702 BBInfo &MInfo = MBBInfoMap[&MBB];
703
704 MInfo.Preds.insert_range(MBB.predecessors());
705 if (MInfo.Preds.size() != MBB.pred_size())
706 report("MBB has duplicate entries in its predecessor list.", &MBB);
707
708 MInfo.Succs.insert_range(MBB.successors());
709 if (MInfo.Succs.size() != MBB.succ_size())
710 report("MBB has duplicate entries in its successor list.", &MBB);
711 }
712
713 // Check that the register use lists are sane.
714 MRI->verifyUseLists();
715
716 if (!MF->empty()) {
717 verifyStackFrame();
718 verifyStackProtector();
719 }
720}
721
722void
723MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
724 FirstTerminator = nullptr;
725 FirstNonPHI = nullptr;
726
727 if (!MF->getProperties().hasNoPHIs() && MRI->tracksLiveness()) {
728 // If this block has allocatable physical registers live-in, check that
729 // it is an entry block or landing pad.
730 for (const auto &LI : MBB->liveins()) {
731 if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
732 MBB->getIterator() != MBB->getParent()->begin() &&
734 report("MBB has allocatable live-in, but isn't entry, landing-pad, or "
735 "inlineasm-br-indirect-target.",
736 MBB);
737 report_context(LI.PhysReg);
738 }
739 }
740 }
741
742 if (MBB->isIRBlockAddressTaken()) {
744 report("ir-block-address-taken is associated with basic block not used by "
745 "a blockaddress.",
746 MBB);
747 }
748
749 // Count the number of landing pad successors.
751 for (const auto *succ : MBB->successors()) {
752 if (succ->isEHPad())
753 LandingPadSuccs.insert(succ);
754 if (!FunctionBlocks.count(succ))
755 report("MBB has successor that isn't part of the function.", MBB);
756 if (!MBBInfoMap[succ].Preds.count(MBB)) {
757 report("Inconsistent CFG", MBB);
758 OS << "MBB is not in the predecessor list of the successor "
759 << printMBBReference(*succ) << ".\n";
760 }
761 }
762
763 // Check the predecessor list.
764 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
765 if (!FunctionBlocks.count(Pred))
766 report("MBB has predecessor that isn't part of the function.", MBB);
767 if (!MBBInfoMap[Pred].Succs.count(MBB)) {
768 report("Inconsistent CFG", MBB);
769 OS << "MBB is not in the successor list of the predecessor "
770 << printMBBReference(*Pred) << ".\n";
771 }
772 }
773
774 const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
775 const BasicBlock *BB = MBB->getBasicBlock();
776 const Function &F = MF->getFunction();
777 if (LandingPadSuccs.size() > 1 &&
778 !(AsmInfo &&
780 BB && isa<SwitchInst>(BB->getTerminator())) &&
781 !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
782 report("MBB has more than one landing pad successor", MBB);
783
784 // Call analyzeBranch. If it succeeds, there several more conditions to check.
785 MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
787 if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
788 Cond)) {
789 // Ok, analyzeBranch thinks it knows what's going on with this block. Let's
790 // check whether its answers match up with reality.
791 if (!TBB && !FBB) {
792 // Block falls through to its successor.
793 if (!MBB->empty() && MBB->back().isBarrier() &&
794 !TII->isPredicated(MBB->back())) {
795 report("MBB exits via unconditional fall-through but ends with a "
796 "barrier instruction!", MBB);
797 }
798 if (!Cond.empty()) {
799 report("MBB exits via unconditional fall-through but has a condition!",
800 MBB);
801 }
802 } else if (TBB && !FBB && Cond.empty()) {
803 // Block unconditionally branches somewhere.
804 if (MBB->empty()) {
805 report("MBB exits via unconditional branch but doesn't contain "
806 "any instructions!", MBB);
807 } else if (!MBB->back().isBarrier()) {
808 report("MBB exits via unconditional branch but doesn't end with a "
809 "barrier instruction!", MBB);
810 } else if (!MBB->back().isTerminator()) {
811 report("MBB exits via unconditional branch but the branch isn't a "
812 "terminator instruction!", MBB);
813 }
814 } else if (TBB && !FBB && !Cond.empty()) {
815 // Block conditionally branches somewhere, otherwise falls through.
816 if (MBB->empty()) {
817 report("MBB exits via conditional branch/fall-through but doesn't "
818 "contain any instructions!", MBB);
819 } else if (MBB->back().isBarrier()) {
820 report("MBB exits via conditional branch/fall-through but ends with a "
821 "barrier instruction!", MBB);
822 } else if (!MBB->back().isTerminator()) {
823 report("MBB exits via conditional branch/fall-through but the branch "
824 "isn't a terminator instruction!", MBB);
825 }
826 } else if (TBB && FBB) {
827 // Block conditionally branches somewhere, otherwise branches
828 // somewhere else.
829 if (MBB->empty()) {
830 report("MBB exits via conditional branch/branch but doesn't "
831 "contain any instructions!", MBB);
832 } else if (!MBB->back().isBarrier()) {
833 report("MBB exits via conditional branch/branch but doesn't end with a "
834 "barrier instruction!", MBB);
835 } else if (!MBB->back().isTerminator()) {
836 report("MBB exits via conditional branch/branch but the branch "
837 "isn't a terminator instruction!", MBB);
838 }
839 if (Cond.empty()) {
840 report("MBB exits via conditional branch/branch but there's no "
841 "condition!", MBB);
842 }
843 } else {
844 report("analyzeBranch returned invalid data!", MBB);
845 }
846
847 // Now check that the successors match up with the answers reported by
848 // analyzeBranch.
849 if (TBB && !MBB->isSuccessor(TBB))
850 report("MBB exits via jump or conditional branch, but its target isn't a "
851 "CFG successor!",
852 MBB);
853 if (FBB && !MBB->isSuccessor(FBB))
854 report("MBB exits via conditional branch, but its target isn't a CFG "
855 "successor!",
856 MBB);
857
858 // There might be a fallthrough to the next block if there's either no
859 // unconditional true branch, or if there's a condition, and one of the
860 // branches is missing.
861 bool Fallthrough = !TBB || (!Cond.empty() && !FBB);
862
863 // A conditional fallthrough must be an actual CFG successor, not
864 // unreachable. (Conversely, an unconditional fallthrough might not really
865 // be a successor, because the block might end in unreachable.)
866 if (!Cond.empty() && !FBB) {
868 if (MBBI == MF->end()) {
869 report("MBB conditionally falls through out of function!", MBB);
870 } else if (!MBB->isSuccessor(&*MBBI))
871 report("MBB exits via conditional branch/fall-through but the CFG "
872 "successors don't match the actual successors!",
873 MBB);
874 }
875
876 // Verify that there aren't any extra un-accounted-for successors.
877 for (const MachineBasicBlock *SuccMBB : MBB->successors()) {
878 // If this successor is one of the branch targets, it's okay.
879 if (SuccMBB == TBB || SuccMBB == FBB)
880 continue;
881 // If we might have a fallthrough, and the successor is the fallthrough
882 // block, that's also ok.
883 if (Fallthrough && SuccMBB == MBB->getNextNode())
884 continue;
885 // Also accept successors which are for exception-handling or might be
886 // inlineasm_br targets.
887 if (SuccMBB->isEHPad() || SuccMBB->isInlineAsmBrIndirectTarget())
888 continue;
889 report("MBB has unexpected successors which are not branch targets, "
890 "fallthrough, EHPads, or inlineasm_br targets.",
891 MBB);
892 }
893 }
894
895 regsLive.clear();
896 if (MRI->tracksLiveness()) {
897 for (const auto &LI : MBB->liveins()) {
898 if (!LI.PhysReg.isPhysical()) {
899 report("MBB live-in list contains non-physical register", MBB);
900 continue;
901 }
902 regsLive.insert_range(TRI->subregs_inclusive(LI.PhysReg));
903 }
904 }
905
906 const MachineFrameInfo &MFI = MF->getFrameInfo();
907 BitVector PR = MFI.getPristineRegs(*MF);
908 for (unsigned I : PR.set_bits())
909 regsLive.insert_range(TRI->subregs_inclusive(I));
910
911 regsKilled.clear();
912 regsDefined.clear();
913
914 if (Indexes)
915 lastIndex = Indexes->getMBBStartIdx(MBB);
916}
917
918// This function gets called for all bundle headers, including normal
919// stand-alone unbundled instructions.
920void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
921 if (Indexes && Indexes->hasIndex(*MI)) {
922 SlotIndex idx = Indexes->getInstructionIndex(*MI);
923 if (!(idx > lastIndex)) {
924 report("Instruction index out of order", MI);
925 OS << "Last instruction was at " << lastIndex << '\n';
926 }
927 lastIndex = idx;
928 }
929
930 // Ensure non-terminators don't follow terminators.
931 if (MI->isTerminator()) {
932 if (!FirstTerminator)
933 FirstTerminator = MI;
934 } else if (FirstTerminator) {
935 // For GlobalISel, G_INVOKE_REGION_START is a terminator that we allow to
936 // precede non-terminators.
937 if (FirstTerminator->getOpcode() != TargetOpcode::G_INVOKE_REGION_START) {
938 report("Non-terminator instruction after the first terminator", MI);
939 OS << "First terminator was:\t" << *FirstTerminator;
940 }
941 }
942}
943
944// The operands on an INLINEASM instruction must follow a template.
945// Verify that the flag operands make sense.
946void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
947 // The first two operands on INLINEASM are the asm string and global flags.
948 if (MI->getNumOperands() < 2) {
949 report("Too few operands on inline asm", MI);
950 return;
951 }
952 if (!MI->getOperand(0).isSymbol())
953 report("Asm string must be an external symbol", MI);
954 if (!MI->getOperand(1).isImm())
955 report("Asm flags must be an immediate", MI);
956 // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
957 // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
958 // and Extra_IsConvergent = 32.
959 if (!isUInt<6>(MI->getOperand(1).getImm()))
960 report("Unknown asm flags", &MI->getOperand(1), 1);
961
962 static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
963
964 unsigned OpNo = InlineAsm::MIOp_FirstOperand;
965 unsigned NumOps;
966 for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
967 const MachineOperand &MO = MI->getOperand(OpNo);
968 // There may be implicit ops after the fixed operands.
969 if (!MO.isImm())
970 break;
971 const InlineAsm::Flag F(MO.getImm());
972 NumOps = 1 + F.getNumOperandRegisters();
973 }
974
975 if (OpNo > MI->getNumOperands())
976 report("Missing operands in last group", MI);
977
978 // An optional MDNode follows the groups.
979 if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
980 ++OpNo;
981
982 // All trailing operands must be implicit registers.
983 for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
984 const MachineOperand &MO = MI->getOperand(OpNo);
985 if (!MO.isReg() || !MO.isImplicit())
986 report("Expected implicit register after groups", &MO, OpNo);
987 }
988
989 if (MI->getOpcode() == TargetOpcode::INLINEASM_BR) {
990 const MachineBasicBlock *MBB = MI->getParent();
991
992 for (unsigned i = InlineAsm::MIOp_FirstOperand, e = MI->getNumOperands();
993 i != e; ++i) {
994 const MachineOperand &MO = MI->getOperand(i);
995
996 if (!MO.isMBB())
997 continue;
998
999 // Check the successor & predecessor lists look ok, assume they are
1000 // not. Find the indirect target without going through the successors.
1001 const MachineBasicBlock *IndirectTargetMBB = MO.getMBB();
1002 if (!IndirectTargetMBB) {
1003 report("INLINEASM_BR indirect target does not exist", &MO, i);
1004 break;
1005 }
1006
1007 if (!MBB->isSuccessor(IndirectTargetMBB))
1008 report("INLINEASM_BR indirect target missing from successor list", &MO,
1009 i);
1010
1011 if (!IndirectTargetMBB->isPredecessor(MBB))
1012 report("INLINEASM_BR indirect target predecessor list missing parent",
1013 &MO, i);
1014 }
1015 }
1016}
1017
1018bool MachineVerifier::verifyAllRegOpsScalar(const MachineInstr &MI,
1019 const MachineRegisterInfo &MRI) {
1020 if (none_of(MI.explicit_operands(), [&MRI](const MachineOperand &Op) {
1021 if (!Op.isReg())
1022 return false;
1023 const auto Reg = Op.getReg();
1024 if (Reg.isPhysical())
1025 return false;
1026 return !MRI.getType(Reg).isScalar();
1027 }))
1028 return true;
1029 report("All register operands must have scalar types", &MI);
1030 return false;
1031}
1032
1033/// Check that types are consistent when two operands need to have the same
1034/// number of vector elements.
1035/// \return true if the types are valid.
1036bool MachineVerifier::verifyVectorElementMatch(LLT Ty0, LLT Ty1,
1037 const MachineInstr *MI) {
1038 if (Ty0.isVector() != Ty1.isVector()) {
1039 report("operand types must be all-vector or all-scalar", MI);
1040 // Generally we try to report as many issues as possible at once, but in
1041 // this case it's not clear what should we be comparing the size of the
1042 // scalar with: the size of the whole vector or its lane. Instead of
1043 // making an arbitrary choice and emitting not so helpful message, let's
1044 // avoid the extra noise and stop here.
1045 return false;
1046 }
1047
1048 if (Ty0.isVector() && Ty0.getElementCount() != Ty1.getElementCount()) {
1049 report("operand types must preserve number of vector elements", MI);
1050 return false;
1051 }
1052
1053 return true;
1054}
1055
1056bool MachineVerifier::verifyGIntrinsicSideEffects(const MachineInstr *MI) {
1057 auto Opcode = MI->getOpcode();
1058 bool NoSideEffects = Opcode == TargetOpcode::G_INTRINSIC ||
1059 Opcode == TargetOpcode::G_INTRINSIC_CONVERGENT;
1060 unsigned IntrID = cast<GIntrinsic>(MI)->getIntrinsicID();
1061 if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) {
1063 MF->getFunction().getContext(), static_cast<Intrinsic::ID>(IntrID));
1064 bool DeclHasSideEffects = !Attrs.getMemoryEffects().doesNotAccessMemory();
1065 if (NoSideEffects && DeclHasSideEffects) {
1066 report(Twine(TII->getName(Opcode),
1067 " used with intrinsic that accesses memory"),
1068 MI);
1069 return false;
1070 }
1071 if (!NoSideEffects && !DeclHasSideEffects) {
1072 report(Twine(TII->getName(Opcode), " used with readnone intrinsic"), MI);
1073 return false;
1074 }
1075 }
1076
1077 return true;
1078}
1079
1080bool MachineVerifier::verifyGIntrinsicConvergence(const MachineInstr *MI) {
1081 auto Opcode = MI->getOpcode();
1082 bool NotConvergent = Opcode == TargetOpcode::G_INTRINSIC ||
1083 Opcode == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS;
1084 unsigned IntrID = cast<GIntrinsic>(MI)->getIntrinsicID();
1085 if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) {
1087 MF->getFunction().getContext(), static_cast<Intrinsic::ID>(IntrID));
1088 bool DeclIsConvergent = Attrs.hasAttribute(Attribute::Convergent);
1089 if (NotConvergent && DeclIsConvergent) {
1090 report(Twine(TII->getName(Opcode), " used with a convergent intrinsic"),
1091 MI);
1092 return false;
1093 }
1094 if (!NotConvergent && !DeclIsConvergent) {
1095 report(
1096 Twine(TII->getName(Opcode), " used with a non-convergent intrinsic"),
1097 MI);
1098 return false;
1099 }
1100 }
1101
1102 return true;
1103}
1104
1105void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
1106 if (isFunctionSelected)
1107 report("Unexpected generic instruction in a Selected function", MI);
1108
1109 const MCInstrDesc &MCID = MI->getDesc();
1110 unsigned NumOps = MI->getNumOperands();
1111
1112 // Branches must reference a basic block if they are not indirect
1113 if (MI->isBranch() && !MI->isIndirectBranch()) {
1114 bool HasMBB = false;
1115 for (const MachineOperand &Op : MI->operands()) {
1116 if (Op.isMBB()) {
1117 HasMBB = true;
1118 break;
1119 }
1120 }
1121
1122 if (!HasMBB) {
1123 report("Branch instruction is missing a basic block operand or "
1124 "isIndirectBranch property",
1125 MI);
1126 }
1127 }
1128
1129 // Check types.
1131 for (unsigned I = 0, E = std::min(MCID.getNumOperands(), NumOps);
1132 I != E; ++I) {
1133 if (!MCID.operands()[I].isGenericType())
1134 continue;
1135 // Generic instructions specify type equality constraints between some of
1136 // their operands. Make sure these are consistent.
1137 size_t TypeIdx = MCID.operands()[I].getGenericTypeIndex();
1138 Types.resize(std::max(TypeIdx + 1, Types.size()));
1139
1140 const MachineOperand *MO = &MI->getOperand(I);
1141 if (!MO->isReg()) {
1142 report("generic instruction must use register operands", MI);
1143 continue;
1144 }
1145
1146 LLT OpTy = MRI->getType(MO->getReg());
1147 // Don't report a type mismatch if there is no actual mismatch, only a
1148 // type missing, to reduce noise:
1149 if (OpTy.isValid()) {
1150 // Only the first valid type for a type index will be printed: don't
1151 // overwrite it later so it's always clear which type was expected:
1152 if (!Types[TypeIdx].isValid())
1153 Types[TypeIdx] = OpTy;
1154 else if (Types[TypeIdx] != OpTy)
1155 report("Type mismatch in generic instruction", MO, I, OpTy);
1156 } else {
1157 // Generic instructions must have types attached to their operands.
1158 report("Generic instruction is missing a virtual register type", MO, I);
1159 }
1160 }
1161
1162 // Generic opcodes must not have physical register operands.
1163 for (unsigned I = 0; I < MI->getNumOperands(); ++I) {
1164 const MachineOperand *MO = &MI->getOperand(I);
1165 if (MO->isReg() && MO->getReg().isPhysical())
1166 report("Generic instruction cannot have physical register", MO, I);
1167 }
1168
1169 // Avoid out of bounds in checks below. This was already reported earlier.
1170 if (MI->getNumOperands() < MCID.getNumOperands())
1171 return;
1172
1174 if (!TII->verifyInstruction(*MI, ErrorInfo))
1175 report(ErrorInfo.data(), MI);
1176
1177 // Verify properties of various specific instruction types
1178 unsigned Opc = MI->getOpcode();
1179 switch (Opc) {
1180 case TargetOpcode::G_ASSERT_SEXT:
1181 case TargetOpcode::G_ASSERT_ZEXT: {
1182 std::string OpcName =
1183 Opc == TargetOpcode::G_ASSERT_ZEXT ? "G_ASSERT_ZEXT" : "G_ASSERT_SEXT";
1184 if (!MI->getOperand(2).isImm()) {
1185 report(Twine(OpcName, " expects an immediate operand #2"), MI);
1186 break;
1187 }
1188
1189 Register Dst = MI->getOperand(0).getReg();
1190 Register Src = MI->getOperand(1).getReg();
1191 LLT SrcTy = MRI->getType(Src);
1192 int64_t Imm = MI->getOperand(2).getImm();
1193 if (Imm <= 0) {
1194 report(Twine(OpcName, " size must be >= 1"), MI);
1195 break;
1196 }
1197
1198 if (Imm >= SrcTy.getScalarSizeInBits()) {
1199 report(Twine(OpcName, " size must be less than source bit width"), MI);
1200 break;
1201 }
1202
1203 const RegisterBank *SrcRB = RBI->getRegBank(Src, *MRI, *TRI);
1204 const RegisterBank *DstRB = RBI->getRegBank(Dst, *MRI, *TRI);
1205
1206 // Allow only the source bank to be set.
1207 if ((SrcRB && DstRB && SrcRB != DstRB) || (DstRB && !SrcRB)) {
1208 report(Twine(OpcName, " cannot change register bank"), MI);
1209 break;
1210 }
1211
1212 // Don't allow a class change. Do allow member class->regbank.
1213 const TargetRegisterClass *DstRC = MRI->getRegClassOrNull(Dst);
1214 if (DstRC && DstRC != MRI->getRegClassOrNull(Src)) {
1215 report(
1216 Twine(OpcName, " source and destination register classes must match"),
1217 MI);
1218 break;
1219 }
1220
1221 break;
1222 }
1223
1224 case TargetOpcode::G_CONSTANT:
1225 case TargetOpcode::G_FCONSTANT: {
1226 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1227 if (DstTy.isVector())
1228 report("Instruction cannot use a vector result type", MI);
1229
1230 if (MI->getOpcode() == TargetOpcode::G_CONSTANT) {
1231 if (!MI->getOperand(1).isCImm()) {
1232 report("G_CONSTANT operand must be cimm", MI);
1233 break;
1234 }
1235
1236 const ConstantInt *CI = MI->getOperand(1).getCImm();
1237 if (CI->getBitWidth() != DstTy.getSizeInBits())
1238 report("inconsistent constant size", MI);
1239 } else {
1240 if (!MI->getOperand(1).isFPImm()) {
1241 report("G_FCONSTANT operand must be fpimm", MI);
1242 break;
1243 }
1244 const ConstantFP *CF = MI->getOperand(1).getFPImm();
1245
1247 DstTy.getSizeInBits()) {
1248 report("inconsistent constant size", MI);
1249 }
1250 }
1251
1252 break;
1253 }
1254 case TargetOpcode::G_LOAD:
1255 case TargetOpcode::G_STORE:
1256 case TargetOpcode::G_ZEXTLOAD:
1257 case TargetOpcode::G_SEXTLOAD: {
1258 LLT ValTy = MRI->getType(MI->getOperand(0).getReg());
1259 LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
1260 if (!PtrTy.isPointer())
1261 report("Generic memory instruction must access a pointer", MI);
1262
1263 // Generic loads and stores must have a single MachineMemOperand
1264 // describing that access.
1265 if (!MI->hasOneMemOperand()) {
1266 report("Generic instruction accessing memory must have one mem operand",
1267 MI);
1268 } else {
1269 const MachineMemOperand &MMO = **MI->memoperands_begin();
1270 if (MI->getOpcode() == TargetOpcode::G_ZEXTLOAD ||
1271 MI->getOpcode() == TargetOpcode::G_SEXTLOAD) {
1273 ValTy.getSizeInBits()))
1274 report("Generic extload must have a narrower memory type", MI);
1275 } else if (MI->getOpcode() == TargetOpcode::G_LOAD) {
1277 ValTy.getSizeInBytes()))
1278 report("load memory size cannot exceed result size", MI);
1279
1280 if (MMO.getRanges()) {
1281 ConstantInt *i =
1283 const LLT RangeTy = LLT::scalar(i->getIntegerType()->getBitWidth());
1284 const LLT MemTy = MMO.getMemoryType();
1285 if (MemTy.getScalarType() != RangeTy ||
1286 ValTy.isScalar() != MemTy.isScalar() ||
1287 (ValTy.isVector() &&
1288 ValTy.getNumElements() != MemTy.getNumElements())) {
1289 report("range is incompatible with the result type", MI);
1290 }
1291 }
1292 } else if (MI->getOpcode() == TargetOpcode::G_STORE) {
1294 MMO.getSize().getValue()))
1295 report("store memory size cannot exceed value size", MI);
1296 }
1297
1298 const AtomicOrdering Order = MMO.getSuccessOrdering();
1299 if (Opc == TargetOpcode::G_STORE) {
1300 if (Order == AtomicOrdering::Acquire ||
1302 report("atomic store cannot use acquire ordering", MI);
1303
1304 } else {
1305 if (Order == AtomicOrdering::Release ||
1307 report("atomic load cannot use release ordering", MI);
1308 }
1309 }
1310
1311 break;
1312 }
1313 case TargetOpcode::G_PHI: {
1314 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1315 if (!DstTy.isValid() || !all_of(drop_begin(MI->operands()),
1316 [this, &DstTy](const MachineOperand &MO) {
1317 if (!MO.isReg())
1318 return true;
1319 LLT Ty = MRI->getType(MO.getReg());
1320 if (!Ty.isValid() || (Ty != DstTy))
1321 return false;
1322 return true;
1323 }))
1324 report("Generic Instruction G_PHI has operands with incompatible/missing "
1325 "types",
1326 MI);
1327 break;
1328 }
1329 case TargetOpcode::G_BITCAST: {
1330 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1331 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1332 if (!DstTy.isValid() || !SrcTy.isValid())
1333 break;
1334
1335 if (SrcTy.isPointer() != DstTy.isPointer())
1336 report("bitcast cannot convert between pointers and other types", MI);
1337
1338 if (SrcTy.getSizeInBits() != DstTy.getSizeInBits())
1339 report("bitcast sizes must match", MI);
1340
1341 if (SrcTy == DstTy)
1342 report("bitcast must change the type", MI);
1343
1344 break;
1345 }
1346 case TargetOpcode::G_INTTOPTR:
1347 case TargetOpcode::G_PTRTOINT:
1348 case TargetOpcode::G_ADDRSPACE_CAST: {
1349 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1350 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1351 if (!DstTy.isValid() || !SrcTy.isValid())
1352 break;
1353
1354 verifyVectorElementMatch(DstTy, SrcTy, MI);
1355
1356 DstTy = DstTy.getScalarType();
1357 SrcTy = SrcTy.getScalarType();
1358
1359 if (MI->getOpcode() == TargetOpcode::G_INTTOPTR) {
1360 if (!DstTy.isPointer())
1361 report("inttoptr result type must be a pointer", MI);
1362 if (SrcTy.isPointer())
1363 report("inttoptr source type must not be a pointer", MI);
1364 } else if (MI->getOpcode() == TargetOpcode::G_PTRTOINT) {
1365 if (!SrcTy.isPointer())
1366 report("ptrtoint source type must be a pointer", MI);
1367 if (DstTy.isPointer())
1368 report("ptrtoint result type must not be a pointer", MI);
1369 } else {
1370 assert(MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST);
1371 if (!SrcTy.isPointer() || !DstTy.isPointer())
1372 report("addrspacecast types must be pointers", MI);
1373 else {
1374 if (SrcTy.getAddressSpace() == DstTy.getAddressSpace())
1375 report("addrspacecast must convert different address spaces", MI);
1376 }
1377 }
1378
1379 break;
1380 }
1381 case TargetOpcode::G_PTR_ADD: {
1382 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1383 LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
1384 LLT OffsetTy = MRI->getType(MI->getOperand(2).getReg());
1385 if (!DstTy.isValid() || !PtrTy.isValid() || !OffsetTy.isValid())
1386 break;
1387
1388 if (!PtrTy.isPointerOrPointerVector())
1389 report("gep first operand must be a pointer", MI);
1390
1391 if (OffsetTy.isPointerOrPointerVector())
1392 report("gep offset operand must not be a pointer", MI);
1393
1394 if (PtrTy.isPointerOrPointerVector()) {
1395 const DataLayout &DL = MF->getDataLayout();
1396 unsigned AS = PtrTy.getAddressSpace();
1397 unsigned IndexSizeInBits = DL.getIndexSize(AS) * 8;
1398 if (OffsetTy.getScalarSizeInBits() != IndexSizeInBits) {
1399 report("gep offset operand must match index size for address space",
1400 MI);
1401 }
1402 }
1403
1404 // TODO: Is the offset allowed to be a scalar with a vector?
1405 break;
1406 }
1407 case TargetOpcode::G_PTRMASK: {
1408 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1409 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1410 LLT MaskTy = MRI->getType(MI->getOperand(2).getReg());
1411 if (!DstTy.isValid() || !SrcTy.isValid() || !MaskTy.isValid())
1412 break;
1413
1414 if (!DstTy.isPointerOrPointerVector())
1415 report("ptrmask result type must be a pointer", MI);
1416
1417 if (!MaskTy.getScalarType().isScalar())
1418 report("ptrmask mask type must be an integer", MI);
1419
1420 verifyVectorElementMatch(DstTy, MaskTy, MI);
1421 break;
1422 }
1423 case TargetOpcode::G_SEXT:
1424 case TargetOpcode::G_ZEXT:
1425 case TargetOpcode::G_ANYEXT:
1426 case TargetOpcode::G_TRUNC:
1427 case TargetOpcode::G_TRUNC_SSAT_S:
1428 case TargetOpcode::G_TRUNC_SSAT_U:
1429 case TargetOpcode::G_TRUNC_USAT_U:
1430 case TargetOpcode::G_FPEXT:
1431 case TargetOpcode::G_FPTRUNC: {
1432 // Number of operands and presense of types is already checked (and
1433 // reported in case of any issues), so no need to report them again. As
1434 // we're trying to report as many issues as possible at once, however, the
1435 // instructions aren't guaranteed to have the right number of operands or
1436 // types attached to them at this point
1437 assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}");
1438 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1439 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1440 if (!DstTy.isValid() || !SrcTy.isValid())
1441 break;
1442
1444 report("Generic extend/truncate can not operate on pointers", MI);
1445
1446 verifyVectorElementMatch(DstTy, SrcTy, MI);
1447
1448 unsigned DstSize = DstTy.getScalarSizeInBits();
1449 unsigned SrcSize = SrcTy.getScalarSizeInBits();
1450 switch (MI->getOpcode()) {
1451 default:
1452 if (DstSize <= SrcSize)
1453 report("Generic extend has destination type no larger than source", MI);
1454 break;
1455 case TargetOpcode::G_TRUNC:
1456 case TargetOpcode::G_TRUNC_SSAT_S:
1457 case TargetOpcode::G_TRUNC_SSAT_U:
1458 case TargetOpcode::G_TRUNC_USAT_U:
1459 case TargetOpcode::G_FPTRUNC:
1460 if (DstSize >= SrcSize)
1461 report("Generic truncate has destination type no smaller than source",
1462 MI);
1463 break;
1464 }
1465 break;
1466 }
1467 case TargetOpcode::G_SELECT: {
1468 LLT SelTy = MRI->getType(MI->getOperand(0).getReg());
1469 LLT CondTy = MRI->getType(MI->getOperand(1).getReg());
1470 if (!SelTy.isValid() || !CondTy.isValid())
1471 break;
1472
1473 // Scalar condition select on a vector is valid.
1474 if (CondTy.isVector())
1475 verifyVectorElementMatch(SelTy, CondTy, MI);
1476 break;
1477 }
1478 case TargetOpcode::G_MERGE_VALUES: {
1479 // G_MERGE_VALUES should only be used to merge scalars into a larger scalar,
1480 // e.g. s2N = MERGE sN, sN
1481 // Merging multiple scalars into a vector is not allowed, should use
1482 // G_BUILD_VECTOR for that.
1483 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1484 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1485 if (DstTy.isVector() || SrcTy.isVector())
1486 report("G_MERGE_VALUES cannot operate on vectors", MI);
1487
1488 const unsigned NumOps = MI->getNumOperands();
1489 if (DstTy.getSizeInBits() != SrcTy.getSizeInBits() * (NumOps - 1))
1490 report("G_MERGE_VALUES result size is inconsistent", MI);
1491
1492 for (unsigned I = 2; I != NumOps; ++I) {
1493 if (MRI->getType(MI->getOperand(I).getReg()) != SrcTy)
1494 report("G_MERGE_VALUES source types do not match", MI);
1495 }
1496
1497 break;
1498 }
1499 case TargetOpcode::G_UNMERGE_VALUES: {
1500 unsigned NumDsts = MI->getNumOperands() - 1;
1501 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1502 for (unsigned i = 1; i < NumDsts; ++i) {
1503 if (MRI->getType(MI->getOperand(i).getReg()) != DstTy) {
1504 report("G_UNMERGE_VALUES destination types do not match", MI);
1505 break;
1506 }
1507 }
1508
1509 LLT SrcTy = MRI->getType(MI->getOperand(NumDsts).getReg());
1510 if (DstTy.isVector()) {
1511 // This case is the converse of G_CONCAT_VECTORS.
1512 if (!SrcTy.isVector() ||
1513 (SrcTy.getScalarType() != DstTy.getScalarType() &&
1514 !SrcTy.isPointerVector()) ||
1515 SrcTy.isScalableVector() != DstTy.isScalableVector() ||
1516 SrcTy.getSizeInBits() != NumDsts * DstTy.getSizeInBits())
1517 report("G_UNMERGE_VALUES source operand does not match vector "
1518 "destination operands",
1519 MI);
1520 } else if (SrcTy.isVector()) {
1521 // This case is the converse of G_BUILD_VECTOR, but relaxed to allow
1522 // mismatched types as long as the total size matches:
1523 // %0:_(s64), %1:_(s64) = G_UNMERGE_VALUES %2:_(<4 x s32>)
1524 if (SrcTy.getSizeInBits() != NumDsts * DstTy.getSizeInBits())
1525 report("G_UNMERGE_VALUES vector source operand does not match scalar "
1526 "destination operands",
1527 MI);
1528 } else {
1529 // This case is the converse of G_MERGE_VALUES.
1530 if (SrcTy.getSizeInBits() != NumDsts * DstTy.getSizeInBits()) {
1531 report("G_UNMERGE_VALUES scalar source operand does not match scalar "
1532 "destination operands",
1533 MI);
1534 }
1535 }
1536 break;
1537 }
1538 case TargetOpcode::G_BUILD_VECTOR: {
1539 // Source types must be scalars, dest type a vector. Total size of scalars
1540 // must match the dest vector size.
1541 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1542 LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
1543 if (!DstTy.isVector() || SrcEltTy.isVector()) {
1544 report("G_BUILD_VECTOR must produce a vector from scalar operands", MI);
1545 break;
1546 }
1547
1548 if (DstTy.getElementType() != SrcEltTy)
1549 report("G_BUILD_VECTOR result element type must match source type", MI);
1550
1551 if (DstTy.getNumElements() != MI->getNumOperands() - 1)
1552 report("G_BUILD_VECTOR must have an operand for each element", MI);
1553
1554 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
1555 if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg()))
1556 report("G_BUILD_VECTOR source operand types are not homogeneous", MI);
1557
1558 break;
1559 }
1560 case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
1561 // Source types must be scalars, dest type a vector. Scalar types must be
1562 // larger than the dest vector elt type, as this is a truncating operation.
1563 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1564 LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
1565 if (!DstTy.isVector() || SrcEltTy.isVector())
1566 report("G_BUILD_VECTOR_TRUNC must produce a vector from scalar operands",
1567 MI);
1568 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
1569 if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg()))
1570 report("G_BUILD_VECTOR_TRUNC source operand types are not homogeneous",
1571 MI);
1572 if (SrcEltTy.getSizeInBits() <= DstTy.getElementType().getSizeInBits())
1573 report("G_BUILD_VECTOR_TRUNC source operand types are not larger than "
1574 "dest elt type",
1575 MI);
1576 break;
1577 }
1578 case TargetOpcode::G_CONCAT_VECTORS: {
1579 // Source types should be vectors, and total size should match the dest
1580 // vector size.
1581 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1582 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1583 if (!DstTy.isVector() || !SrcTy.isVector())
1584 report("G_CONCAT_VECTOR requires vector source and destination operands",
1585 MI);
1586
1587 if (MI->getNumOperands() < 3)
1588 report("G_CONCAT_VECTOR requires at least 2 source operands", MI);
1589
1590 for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2))
1591 if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg()))
1592 report("G_CONCAT_VECTOR source operand types are not homogeneous", MI);
1593 if (DstTy.getElementCount() !=
1594 SrcTy.getElementCount() * (MI->getNumOperands() - 1))
1595 report("G_CONCAT_VECTOR num dest and source elements should match", MI);
1596 break;
1597 }
1598 case TargetOpcode::G_ICMP:
1599 case TargetOpcode::G_FCMP: {
1600 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1601 LLT SrcTy = MRI->getType(MI->getOperand(2).getReg());
1602
1603 if ((DstTy.isVector() != SrcTy.isVector()) ||
1604 (DstTy.isVector() &&
1605 DstTy.getElementCount() != SrcTy.getElementCount()))
1606 report("Generic vector icmp/fcmp must preserve number of lanes", MI);
1607
1608 break;
1609 }
1610 case TargetOpcode::G_SCMP:
1611 case TargetOpcode::G_UCMP: {
1612 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1613 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1614
1615 if (SrcTy.isPointerOrPointerVector()) {
1616 report("Generic scmp/ucmp does not support pointers as operands", MI);
1617 break;
1618 }
1619
1620 if (DstTy.isPointerOrPointerVector()) {
1621 report("Generic scmp/ucmp does not support pointers as a result", MI);
1622 break;
1623 }
1624
1625 if (DstTy.getScalarSizeInBits() < 2) {
1626 report("Result type must be at least 2 bits wide", MI);
1627 break;
1628 }
1629
1630 if ((DstTy.isVector() != SrcTy.isVector()) ||
1631 (DstTy.isVector() &&
1632 DstTy.getElementCount() != SrcTy.getElementCount())) {
1633 report("Generic vector scmp/ucmp must preserve number of lanes", MI);
1634 break;
1635 }
1636
1637 break;
1638 }
1639 case TargetOpcode::G_EXTRACT: {
1640 const MachineOperand &SrcOp = MI->getOperand(1);
1641 if (!SrcOp.isReg()) {
1642 report("extract source must be a register", MI);
1643 break;
1644 }
1645
1646 const MachineOperand &OffsetOp = MI->getOperand(2);
1647 if (!OffsetOp.isImm()) {
1648 report("extract offset must be a constant", MI);
1649 break;
1650 }
1651
1652 unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
1653 unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
1654 if (SrcSize == DstSize)
1655 report("extract source must be larger than result", MI);
1656
1657 if (DstSize + OffsetOp.getImm() > SrcSize)
1658 report("extract reads past end of register", MI);
1659 break;
1660 }
1661 case TargetOpcode::G_INSERT: {
1662 const MachineOperand &SrcOp = MI->getOperand(2);
1663 if (!SrcOp.isReg()) {
1664 report("insert source must be a register", MI);
1665 break;
1666 }
1667
1668 const MachineOperand &OffsetOp = MI->getOperand(3);
1669 if (!OffsetOp.isImm()) {
1670 report("insert offset must be a constant", MI);
1671 break;
1672 }
1673
1674 unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
1675 unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
1676
1677 if (DstSize <= SrcSize)
1678 report("inserted size must be smaller than total register", MI);
1679
1680 if (SrcSize + OffsetOp.getImm() > DstSize)
1681 report("insert writes past end of register", MI);
1682
1683 break;
1684 }
1685 case TargetOpcode::G_JUMP_TABLE: {
1686 if (!MI->getOperand(1).isJTI())
1687 report("G_JUMP_TABLE source operand must be a jump table index", MI);
1688 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1689 if (!DstTy.isPointer())
1690 report("G_JUMP_TABLE dest operand must have a pointer type", MI);
1691 break;
1692 }
1693 case TargetOpcode::G_BRJT: {
1694 if (!MRI->getType(MI->getOperand(0).getReg()).isPointer())
1695 report("G_BRJT src operand 0 must be a pointer type", MI);
1696
1697 if (!MI->getOperand(1).isJTI())
1698 report("G_BRJT src operand 1 must be a jump table index", MI);
1699
1700 const auto &IdxOp = MI->getOperand(2);
1701 if (!IdxOp.isReg() || MRI->getType(IdxOp.getReg()).isPointer())
1702 report("G_BRJT src operand 2 must be a scalar reg type", MI);
1703 break;
1704 }
1705 case TargetOpcode::G_INTRINSIC:
1706 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
1707 case TargetOpcode::G_INTRINSIC_CONVERGENT:
1708 case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS: {
1709 // TODO: Should verify number of def and use operands, but the current
1710 // interface requires passing in IR types for mangling.
1711 const MachineOperand &IntrIDOp = MI->getOperand(MI->getNumExplicitDefs());
1712 if (!IntrIDOp.isIntrinsicID()) {
1713 report("G_INTRINSIC first src operand must be an intrinsic ID", MI);
1714 break;
1715 }
1716
1717 if (!verifyGIntrinsicSideEffects(MI))
1718 break;
1719 if (!verifyGIntrinsicConvergence(MI))
1720 break;
1721
1722 break;
1723 }
1724 case TargetOpcode::G_SEXT_INREG: {
1725 if (!MI->getOperand(2).isImm()) {
1726 report("G_SEXT_INREG expects an immediate operand #2", MI);
1727 break;
1728 }
1729
1730 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1731 int64_t Imm = MI->getOperand(2).getImm();
1732 if (Imm <= 0)
1733 report("G_SEXT_INREG size must be >= 1", MI);
1734 if (Imm >= SrcTy.getScalarSizeInBits())
1735 report("G_SEXT_INREG size must be less than source bit width", MI);
1736 break;
1737 }
1738 case TargetOpcode::G_BSWAP: {
1739 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1740 if (DstTy.getScalarSizeInBits() % 16 != 0)
1741 report("G_BSWAP size must be a multiple of 16 bits", MI);
1742 break;
1743 }
1744 case TargetOpcode::G_VSCALE: {
1745 if (!MI->getOperand(1).isCImm()) {
1746 report("G_VSCALE operand must be cimm", MI);
1747 break;
1748 }
1749 if (MI->getOperand(1).getCImm()->isZero()) {
1750 report("G_VSCALE immediate cannot be zero", MI);
1751 break;
1752 }
1753 break;
1754 }
1755 case TargetOpcode::G_STEP_VECTOR: {
1756 if (!MI->getOperand(1).isCImm()) {
1757 report("operand must be cimm", MI);
1758 break;
1759 }
1760
1761 if (!MI->getOperand(1).getCImm()->getValue().isStrictlyPositive()) {
1762 report("step must be > 0", MI);
1763 break;
1764 }
1765
1766 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1767 if (!DstTy.isScalableVector()) {
1768 report("Destination type must be a scalable vector", MI);
1769 break;
1770 }
1771
1772 // <vscale x 2 x p0>
1773 if (!DstTy.getElementType().isScalar()) {
1774 report("Destination element type must be scalar", MI);
1775 break;
1776 }
1777
1778 if (MI->getOperand(1).getCImm()->getBitWidth() !=
1780 report("step bitwidth differs from result type element bitwidth", MI);
1781 break;
1782 }
1783 break;
1784 }
1785 case TargetOpcode::G_INSERT_SUBVECTOR: {
1786 const MachineOperand &Src0Op = MI->getOperand(1);
1787 if (!Src0Op.isReg()) {
1788 report("G_INSERT_SUBVECTOR first source must be a register", MI);
1789 break;
1790 }
1791
1792 const MachineOperand &Src1Op = MI->getOperand(2);
1793 if (!Src1Op.isReg()) {
1794 report("G_INSERT_SUBVECTOR second source must be a register", MI);
1795 break;
1796 }
1797
1798 const MachineOperand &IndexOp = MI->getOperand(3);
1799 if (!IndexOp.isImm()) {
1800 report("G_INSERT_SUBVECTOR index must be an immediate", MI);
1801 break;
1802 }
1803
1804 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1805 LLT Src1Ty = MRI->getType(Src1Op.getReg());
1806
1807 if (!DstTy.isVector()) {
1808 report("Destination type must be a vector", MI);
1809 break;
1810 }
1811
1812 if (!Src1Ty.isVector()) {
1813 report("Second source must be a vector", MI);
1814 break;
1815 }
1816
1817 if (DstTy.getElementType() != Src1Ty.getElementType()) {
1818 report("Element type of vectors must be the same", MI);
1819 break;
1820 }
1821
1822 if (Src1Ty.isScalable() != DstTy.isScalable()) {
1823 report("Vector types must both be fixed or both be scalable", MI);
1824 break;
1825 }
1826
1828 DstTy.getElementCount())) {
1829 report("Second source must be smaller than destination vector", MI);
1830 break;
1831 }
1832
1833 uint64_t Idx = IndexOp.getImm();
1834 uint64_t Src1MinLen = Src1Ty.getElementCount().getKnownMinValue();
1835 if (IndexOp.getImm() % Src1MinLen != 0) {
1836 report("Index must be a multiple of the second source vector's "
1837 "minimum vector length",
1838 MI);
1839 break;
1840 }
1841
1842 uint64_t DstMinLen = DstTy.getElementCount().getKnownMinValue();
1843 if (Idx >= DstMinLen || Idx + Src1MinLen > DstMinLen) {
1844 report("Subvector type and index must not cause insert to overrun the "
1845 "vector being inserted into",
1846 MI);
1847 break;
1848 }
1849
1850 break;
1851 }
1852 case TargetOpcode::G_EXTRACT_SUBVECTOR: {
1853 const MachineOperand &SrcOp = MI->getOperand(1);
1854 if (!SrcOp.isReg()) {
1855 report("G_EXTRACT_SUBVECTOR first source must be a register", MI);
1856 break;
1857 }
1858
1859 const MachineOperand &IndexOp = MI->getOperand(2);
1860 if (!IndexOp.isImm()) {
1861 report("G_EXTRACT_SUBVECTOR index must be an immediate", MI);
1862 break;
1863 }
1864
1865 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1866 LLT SrcTy = MRI->getType(SrcOp.getReg());
1867
1868 if (!DstTy.isVector()) {
1869 report("Destination type must be a vector", MI);
1870 break;
1871 }
1872
1873 if (!SrcTy.isVector()) {
1874 report("Source must be a vector", MI);
1875 break;
1876 }
1877
1878 if (DstTy.getElementType() != SrcTy.getElementType()) {
1879 report("Element type of vectors must be the same", MI);
1880 break;
1881 }
1882
1883 if (SrcTy.isScalable() != DstTy.isScalable()) {
1884 report("Vector types must both be fixed or both be scalable", MI);
1885 break;
1886 }
1887
1889 SrcTy.getElementCount())) {
1890 report("Destination vector must be smaller than source vector", MI);
1891 break;
1892 }
1893
1894 uint64_t Idx = IndexOp.getImm();
1895 uint64_t DstMinLen = DstTy.getElementCount().getKnownMinValue();
1896 if (Idx % DstMinLen != 0) {
1897 report("Index must be a multiple of the destination vector's minimum "
1898 "vector length",
1899 MI);
1900 break;
1901 }
1902
1903 uint64_t SrcMinLen = SrcTy.getElementCount().getKnownMinValue();
1904 if (Idx >= SrcMinLen || Idx + DstMinLen > SrcMinLen) {
1905 report("Destination type and index must not cause extract to overrun the "
1906 "source vector",
1907 MI);
1908 break;
1909 }
1910
1911 break;
1912 }
1913 case TargetOpcode::G_SHUFFLE_VECTOR: {
1914 const MachineOperand &MaskOp = MI->getOperand(3);
1915 if (!MaskOp.isShuffleMask()) {
1916 report("Incorrect mask operand type for G_SHUFFLE_VECTOR", MI);
1917 break;
1918 }
1919
1920 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1921 LLT Src0Ty = MRI->getType(MI->getOperand(1).getReg());
1922 LLT Src1Ty = MRI->getType(MI->getOperand(2).getReg());
1923
1924 if (Src0Ty != Src1Ty)
1925 report("Source operands must be the same type", MI);
1926
1927 if (Src0Ty.getScalarType() != DstTy.getScalarType()) {
1928 report("G_SHUFFLE_VECTOR cannot change element type", MI);
1929 break;
1930 }
1931 if (!Src0Ty.isVector()) {
1932 report("G_SHUFFLE_VECTOR must have vector src", MI);
1933 break;
1934 }
1935 if (!DstTy.isVector()) {
1936 report("G_SHUFFLE_VECTOR must have vector dst", MI);
1937 break;
1938 }
1939
1940 // Don't check that all operands are vector because scalars are used in
1941 // place of 1 element vectors.
1942 int SrcNumElts = Src0Ty.getNumElements();
1943 int DstNumElts = DstTy.getNumElements();
1944
1945 ArrayRef<int> MaskIdxes = MaskOp.getShuffleMask();
1946
1947 if (static_cast<int>(MaskIdxes.size()) != DstNumElts)
1948 report("Wrong result type for shufflemask", MI);
1949
1950 for (int Idx : MaskIdxes) {
1951 if (Idx < 0)
1952 continue;
1953
1954 if (Idx >= 2 * SrcNumElts)
1955 report("Out of bounds shuffle index", MI);
1956 }
1957
1958 break;
1959 }
1960
1961 case TargetOpcode::G_SPLAT_VECTOR: {
1962 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1963 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1964
1965 if (!DstTy.isScalableVector()) {
1966 report("Destination type must be a scalable vector", MI);
1967 break;
1968 }
1969
1970 if (!SrcTy.isScalar() && !SrcTy.isPointer()) {
1971 report("Source type must be a scalar or pointer", MI);
1972 break;
1973 }
1974
1976 SrcTy.getSizeInBits())) {
1977 report("Element type of the destination must be the same size or smaller "
1978 "than the source type",
1979 MI);
1980 break;
1981 }
1982
1983 break;
1984 }
1985 case TargetOpcode::G_EXTRACT_VECTOR_ELT: {
1986 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1987 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1988 LLT IdxTy = MRI->getType(MI->getOperand(2).getReg());
1989
1990 if (!DstTy.isScalar() && !DstTy.isPointer()) {
1991 report("Destination type must be a scalar or pointer", MI);
1992 break;
1993 }
1994
1995 if (!SrcTy.isVector()) {
1996 report("First source must be a vector", MI);
1997 break;
1998 }
1999
2000 auto TLI = MF->getSubtarget().getTargetLowering();
2001 if (IdxTy.getSizeInBits() != TLI->getVectorIdxWidth(MF->getDataLayout())) {
2002 report("Index type must match VectorIdxTy", MI);
2003 break;
2004 }
2005
2006 break;
2007 }
2008 case TargetOpcode::G_INSERT_VECTOR_ELT: {
2009 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
2010 LLT VecTy = MRI->getType(MI->getOperand(1).getReg());
2011 LLT ScaTy = MRI->getType(MI->getOperand(2).getReg());
2012 LLT IdxTy = MRI->getType(MI->getOperand(3).getReg());
2013
2014 if (!DstTy.isVector()) {
2015 report("Destination type must be a vector", MI);
2016 break;
2017 }
2018
2019 if (VecTy != DstTy) {
2020 report("Destination type and vector type must match", MI);
2021 break;
2022 }
2023
2024 if (!ScaTy.isScalar() && !ScaTy.isPointer()) {
2025 report("Inserted element must be a scalar or pointer", MI);
2026 break;
2027 }
2028
2029 auto TLI = MF->getSubtarget().getTargetLowering();
2030 if (IdxTy.getSizeInBits() != TLI->getVectorIdxWidth(MF->getDataLayout())) {
2031 report("Index type must match VectorIdxTy", MI);
2032 break;
2033 }
2034
2035 break;
2036 }
2037 case TargetOpcode::G_DYN_STACKALLOC: {
2038 const MachineOperand &DstOp = MI->getOperand(0);
2039 const MachineOperand &AllocOp = MI->getOperand(1);
2040 const MachineOperand &AlignOp = MI->getOperand(2);
2041
2042 if (!DstOp.isReg() || !MRI->getType(DstOp.getReg()).isPointer()) {
2043 report("dst operand 0 must be a pointer type", MI);
2044 break;
2045 }
2046
2047 if (!AllocOp.isReg() || !MRI->getType(AllocOp.getReg()).isScalar()) {
2048 report("src operand 1 must be a scalar reg type", MI);
2049 break;
2050 }
2051
2052 if (!AlignOp.isImm()) {
2053 report("src operand 2 must be an immediate type", MI);
2054 break;
2055 }
2056 break;
2057 }
2058 case TargetOpcode::G_MEMCPY_INLINE:
2059 case TargetOpcode::G_MEMCPY:
2060 case TargetOpcode::G_MEMMOVE: {
2061 ArrayRef<MachineMemOperand *> MMOs = MI->memoperands();
2062 if (MMOs.size() != 2) {
2063 report("memcpy/memmove must have 2 memory operands", MI);
2064 break;
2065 }
2066
2067 if ((!MMOs[0]->isStore() || MMOs[0]->isLoad()) ||
2068 (MMOs[1]->isStore() || !MMOs[1]->isLoad())) {
2069 report("wrong memory operand types", MI);
2070 break;
2071 }
2072
2073 if (MMOs[0]->getSize() != MMOs[1]->getSize())
2074 report("inconsistent memory operand sizes", MI);
2075
2076 LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg());
2077 LLT SrcPtrTy = MRI->getType(MI->getOperand(1).getReg());
2078
2079 if (!DstPtrTy.isPointer() || !SrcPtrTy.isPointer()) {
2080 report("memory instruction operand must be a pointer", MI);
2081 break;
2082 }
2083
2084 if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace())
2085 report("inconsistent store address space", MI);
2086 if (SrcPtrTy.getAddressSpace() != MMOs[1]->getAddrSpace())
2087 report("inconsistent load address space", MI);
2088
2089 if (Opc != TargetOpcode::G_MEMCPY_INLINE)
2090 if (!MI->getOperand(3).isImm() || (MI->getOperand(3).getImm() & ~1LL))
2091 report("'tail' flag (operand 3) must be an immediate 0 or 1", MI);
2092
2093 break;
2094 }
2095 case TargetOpcode::G_BZERO:
2096 case TargetOpcode::G_MEMSET: {
2097 ArrayRef<MachineMemOperand *> MMOs = MI->memoperands();
2098 std::string Name = Opc == TargetOpcode::G_MEMSET ? "memset" : "bzero";
2099 if (MMOs.size() != 1) {
2100 report(Twine(Name, " must have 1 memory operand"), MI);
2101 break;
2102 }
2103
2104 if ((!MMOs[0]->isStore() || MMOs[0]->isLoad())) {
2105 report(Twine(Name, " memory operand must be a store"), MI);
2106 break;
2107 }
2108
2109 LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg());
2110 if (!DstPtrTy.isPointer()) {
2111 report(Twine(Name, " operand must be a pointer"), MI);
2112 break;
2113 }
2114
2115 if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace())
2116 report("inconsistent " + Twine(Name, " address space"), MI);
2117
2118 if (!MI->getOperand(MI->getNumOperands() - 1).isImm() ||
2119 (MI->getOperand(MI->getNumOperands() - 1).getImm() & ~1LL))
2120 report("'tail' flag (last operand) must be an immediate 0 or 1", MI);
2121
2122 break;
2123 }
2124 case TargetOpcode::G_UBSANTRAP: {
2125 const MachineOperand &KindOp = MI->getOperand(0);
2126 if (!MI->getOperand(0).isImm()) {
2127 report("Crash kind must be an immediate", &KindOp, 0);
2128 break;
2129 }
2130 int64_t Kind = MI->getOperand(0).getImm();
2131 if (!isInt<8>(Kind))
2132 report("Crash kind must be 8 bit wide", &KindOp, 0);
2133 break;
2134 }
2135 case TargetOpcode::G_VECREDUCE_SEQ_FADD:
2136 case TargetOpcode::G_VECREDUCE_SEQ_FMUL: {
2137 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
2138 LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg());
2139 LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg());
2140 if (!DstTy.isScalar())
2141 report("Vector reduction requires a scalar destination type", MI);
2142 if (!Src1Ty.isScalar())
2143 report("Sequential FADD/FMUL vector reduction requires a scalar 1st operand", MI);
2144 if (!Src2Ty.isVector())
2145 report("Sequential FADD/FMUL vector reduction must have a vector 2nd operand", MI);
2146 break;
2147 }
2148 case TargetOpcode::G_VECREDUCE_FADD:
2149 case TargetOpcode::G_VECREDUCE_FMUL:
2150 case TargetOpcode::G_VECREDUCE_FMAX:
2151 case TargetOpcode::G_VECREDUCE_FMIN:
2152 case TargetOpcode::G_VECREDUCE_FMAXIMUM:
2153 case TargetOpcode::G_VECREDUCE_FMINIMUM:
2154 case TargetOpcode::G_VECREDUCE_ADD:
2155 case TargetOpcode::G_VECREDUCE_MUL:
2156 case TargetOpcode::G_VECREDUCE_AND:
2157 case TargetOpcode::G_VECREDUCE_OR:
2158 case TargetOpcode::G_VECREDUCE_XOR:
2159 case TargetOpcode::G_VECREDUCE_SMAX:
2160 case TargetOpcode::G_VECREDUCE_SMIN:
2161 case TargetOpcode::G_VECREDUCE_UMAX:
2162 case TargetOpcode::G_VECREDUCE_UMIN: {
2163 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
2164 if (!DstTy.isScalar())
2165 report("Vector reduction requires a scalar destination type", MI);
2166 break;
2167 }
2168
2169 case TargetOpcode::G_SBFX:
2170 case TargetOpcode::G_UBFX: {
2171 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
2172 if (DstTy.isVector()) {
2173 report("Bitfield extraction is not supported on vectors", MI);
2174 break;
2175 }
2176 break;
2177 }
2178 case TargetOpcode::G_SHL:
2179 case TargetOpcode::G_LSHR:
2180 case TargetOpcode::G_ASHR:
2181 case TargetOpcode::G_ROTR:
2182 case TargetOpcode::G_ROTL: {
2183 LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg());
2184 LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg());
2185 if (Src1Ty.isVector() != Src2Ty.isVector()) {
2186 report("Shifts and rotates require operands to be either all scalars or "
2187 "all vectors",
2188 MI);
2189 break;
2190 }
2191 break;
2192 }
2193 case TargetOpcode::G_LLROUND:
2194 case TargetOpcode::G_LROUND: {
2195 LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
2196 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
2197 if (!DstTy.isValid() || !SrcTy.isValid())
2198 break;
2199 if (SrcTy.isPointer() || DstTy.isPointer()) {
2200 StringRef Op = SrcTy.isPointer() ? "Source" : "Destination";
2201 report(Twine(Op, " operand must not be a pointer type"), MI);
2202 } else if (SrcTy.isScalar()) {
2203 verifyAllRegOpsScalar(*MI, *MRI);
2204 break;
2205 } else if (SrcTy.isVector()) {
2206 verifyVectorElementMatch(SrcTy, DstTy, MI);
2207 break;
2208 }
2209 break;
2210 }
2211 case TargetOpcode::G_IS_FPCLASS: {
2212 LLT DestTy = MRI->getType(MI->getOperand(0).getReg());
2213 LLT DestEltTy = DestTy.getScalarType();
2214 if (!DestEltTy.isScalar()) {
2215 report("Destination must be a scalar or vector of scalars", MI);
2216 break;
2217 }
2218 LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
2219 LLT SrcEltTy = SrcTy.getScalarType();
2220 if (!SrcEltTy.isScalar()) {
2221 report("Source must be a scalar or vector of scalars", MI);
2222 break;
2223 }
2224 if (!verifyVectorElementMatch(DestTy, SrcTy, MI))
2225 break;
2226 const MachineOperand &TestMO = MI->getOperand(2);
2227 if (!TestMO.isImm()) {
2228 report("floating-point class set (operand 2) must be an immediate", MI);
2229 break;
2230 }
2231 int64_t Test = TestMO.getImm();
2233 report("Incorrect floating-point class set (operand 2)", MI);
2234 break;
2235 }
2236 break;
2237 }
2238 case TargetOpcode::G_PREFETCH: {
2239 const MachineOperand &AddrOp = MI->getOperand(0);
2240 if (!AddrOp.isReg() || !MRI->getType(AddrOp.getReg()).isPointer()) {
2241 report("addr operand must be a pointer", &AddrOp, 0);
2242 break;
2243 }
2244 const MachineOperand &RWOp = MI->getOperand(1);
2245 if (!RWOp.isImm() || (uint64_t)RWOp.getImm() >= 2) {
2246 report("rw operand must be an immediate 0-1", &RWOp, 1);
2247 break;
2248 }
2249 const MachineOperand &LocalityOp = MI->getOperand(2);
2250 if (!LocalityOp.isImm() || (uint64_t)LocalityOp.getImm() >= 4) {
2251 report("locality operand must be an immediate 0-3", &LocalityOp, 2);
2252 break;
2253 }
2254 const MachineOperand &CacheTypeOp = MI->getOperand(3);
2255 if (!CacheTypeOp.isImm() || (uint64_t)CacheTypeOp.getImm() >= 2) {
2256 report("cache type operand must be an immediate 0-1", &CacheTypeOp, 3);
2257 break;
2258 }
2259 break;
2260 }
2261 case TargetOpcode::G_ASSERT_ALIGN: {
2262 if (MI->getOperand(2).getImm() < 1)
2263 report("alignment immediate must be >= 1", MI);
2264 break;
2265 }
2266 case TargetOpcode::G_CONSTANT_POOL: {
2267 if (!MI->getOperand(1).isCPI())
2268 report("Src operand 1 must be a constant pool index", MI);
2269 if (!MRI->getType(MI->getOperand(0).getReg()).isPointer())
2270 report("Dst operand 0 must be a pointer", MI);
2271 break;
2272 }
2273 case TargetOpcode::G_PTRAUTH_GLOBAL_VALUE: {
2274 const MachineOperand &AddrOp = MI->getOperand(1);
2275 if (!AddrOp.isReg() || !MRI->getType(AddrOp.getReg()).isPointer())
2276 report("addr operand must be a pointer", &AddrOp, 1);
2277 break;
2278 }
2279 default:
2280 break;
2281 }
2282}
2283
2284void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
2285 const MCInstrDesc &MCID = MI->getDesc();
2286 if (MI->getNumOperands() < MCID.getNumOperands()) {
2287 report("Too few operands", MI);
2288 OS << MCID.getNumOperands() << " operands expected, but "
2289 << MI->getNumOperands() << " given.\n";
2290 }
2291
2292 if (MI->getFlag(MachineInstr::NoConvergent) && !MCID.isConvergent())
2293 report("NoConvergent flag expected only on convergent instructions.", MI);
2294
2295 if (MI->isPHI()) {
2296 if (MF->getProperties().hasNoPHIs())
2297 report("Found PHI instruction with NoPHIs property set", MI);
2298
2299 if (FirstNonPHI)
2300 report("Found PHI instruction after non-PHI", MI);
2301 } else if (FirstNonPHI == nullptr)
2302 FirstNonPHI = MI;
2303
2304 // Check the tied operands.
2305 if (MI->isInlineAsm())
2306 verifyInlineAsm(MI);
2307
2308 // Check that unspillable terminators define a reg and have at most one use.
2309 if (TII->isUnspillableTerminator(MI)) {
2310 if (!MI->getOperand(0).isReg() || !MI->getOperand(0).isDef())
2311 report("Unspillable Terminator does not define a reg", MI);
2312 Register Def = MI->getOperand(0).getReg();
2313 if (Def.isVirtual() && !MF->getProperties().hasNoPHIs() &&
2314 std::distance(MRI->use_nodbg_begin(Def), MRI->use_nodbg_end()) > 1)
2315 report("Unspillable Terminator expected to have at most one use!", MI);
2316 }
2317
2318 // A fully-formed DBG_VALUE must have a location. Ignore partially formed
2319 // DBG_VALUEs: these are convenient to use in tests, but should never get
2320 // generated.
2321 if (MI->isDebugValue() && MI->getNumOperands() == 4)
2322 if (!MI->getDebugLoc())
2323 report("Missing DebugLoc for debug instruction", MI);
2324
2325 // Meta instructions should never be the subject of debug value tracking,
2326 // they don't create a value in the output program at all.
2327 if (MI->isMetaInstruction() && MI->peekDebugInstrNum())
2328 report("Metadata instruction should not have a value tracking number", MI);
2329
2330 // Check the MachineMemOperands for basic consistency.
2331 for (MachineMemOperand *Op : MI->memoperands()) {
2332 if (Op->isLoad() && !MI->mayLoad())
2333 report("Missing mayLoad flag", MI);
2334 if (Op->isStore() && !MI->mayStore())
2335 report("Missing mayStore flag", MI);
2336 }
2337
2338 // Debug values must not have a slot index.
2339 // Other instructions must have one, unless they are inside a bundle.
2340 if (LiveInts) {
2341 bool mapped = !LiveInts->isNotInMIMap(*MI);
2342 if (MI->isDebugOrPseudoInstr()) {
2343 if (mapped)
2344 report("Debug instruction has a slot index", MI);
2345 } else if (MI->isInsideBundle()) {
2346 if (mapped)
2347 report("Instruction inside bundle has a slot index", MI);
2348 } else {
2349 if (!mapped)
2350 report("Missing slot index", MI);
2351 }
2352 }
2353
2354 unsigned Opc = MCID.getOpcode();
2356 verifyPreISelGenericInstruction(MI);
2357 return;
2358 }
2359
2361 if (!TII->verifyInstruction(*MI, ErrorInfo))
2362 report(ErrorInfo.data(), MI);
2363
2364 // Verify properties of various specific instruction types
2365 switch (MI->getOpcode()) {
2366 case TargetOpcode::COPY: {
2367 const MachineOperand &DstOp = MI->getOperand(0);
2368 const MachineOperand &SrcOp = MI->getOperand(1);
2369 const Register SrcReg = SrcOp.getReg();
2370 const Register DstReg = DstOp.getReg();
2371
2372 LLT DstTy = MRI->getType(DstReg);
2373 LLT SrcTy = MRI->getType(SrcReg);
2374 if (SrcTy.isValid() && DstTy.isValid()) {
2375 // If both types are valid, check that the types are the same.
2376 if (SrcTy != DstTy) {
2377 report("Copy Instruction is illegal with mismatching types", MI);
2378 OS << "Def = " << DstTy << ", Src = " << SrcTy << '\n';
2379 }
2380
2381 break;
2382 }
2383
2384 if (!SrcTy.isValid() && !DstTy.isValid())
2385 break;
2386
2387 // If we have only one valid type, this is likely a copy between a virtual
2388 // and physical register.
2389 TypeSize SrcSize = TypeSize::getZero();
2390 TypeSize DstSize = TypeSize::getZero();
2391 if (SrcReg.isPhysical() && DstTy.isValid()) {
2392 const TargetRegisterClass *SrcRC =
2393 TRI->getMinimalPhysRegClassLLT(SrcReg, DstTy);
2394 if (!SrcRC)
2395 SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
2396 } else {
2397 SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
2398 }
2399
2400 if (DstReg.isPhysical() && SrcTy.isValid()) {
2401 const TargetRegisterClass *DstRC =
2402 TRI->getMinimalPhysRegClassLLT(DstReg, SrcTy);
2403 if (!DstRC)
2404 DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
2405 } else {
2406 DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
2407 }
2408
2409 // The next two checks allow COPY between physical and virtual registers,
2410 // when the virtual register has a scalable size and the physical register
2411 // has a fixed size. These checks allow COPY between *potentially*
2412 // mismatched sizes. However, once RegisterBankSelection occurs,
2413 // MachineVerifier should be able to resolve a fixed size for the scalable
2414 // vector, and at that point this function will know for sure whether the
2415 // sizes are mismatched and correctly report a size mismatch.
2416 if (SrcReg.isPhysical() && DstReg.isVirtual() && DstSize.isScalable() &&
2417 !SrcSize.isScalable())
2418 break;
2419 if (SrcReg.isVirtual() && DstReg.isPhysical() && SrcSize.isScalable() &&
2420 !DstSize.isScalable())
2421 break;
2422
2423 if (SrcSize.isNonZero() && DstSize.isNonZero() && SrcSize != DstSize) {
2424 if (!DstOp.getSubReg() && !SrcOp.getSubReg()) {
2425 report("Copy Instruction is illegal with mismatching sizes", MI);
2426 OS << "Def Size = " << DstSize << ", Src Size = " << SrcSize << '\n';
2427 }
2428 }
2429 break;
2430 }
2431 case TargetOpcode::STATEPOINT: {
2432 StatepointOpers SO(MI);
2433 if (!MI->getOperand(SO.getIDPos()).isImm() ||
2434 !MI->getOperand(SO.getNBytesPos()).isImm() ||
2435 !MI->getOperand(SO.getNCallArgsPos()).isImm()) {
2436 report("meta operands to STATEPOINT not constant!", MI);
2437 break;
2438 }
2439
2440 auto VerifyStackMapConstant = [&](unsigned Offset) {
2441 if (Offset >= MI->getNumOperands()) {
2442 report("stack map constant to STATEPOINT is out of range!", MI);
2443 return;
2444 }
2445 if (!MI->getOperand(Offset - 1).isImm() ||
2446 MI->getOperand(Offset - 1).getImm() != StackMaps::ConstantOp ||
2447 !MI->getOperand(Offset).isImm())
2448 report("stack map constant to STATEPOINT not well formed!", MI);
2449 };
2450 VerifyStackMapConstant(SO.getCCIdx());
2451 VerifyStackMapConstant(SO.getFlagsIdx());
2452 VerifyStackMapConstant(SO.getNumDeoptArgsIdx());
2453 VerifyStackMapConstant(SO.getNumGCPtrIdx());
2454 VerifyStackMapConstant(SO.getNumAllocaIdx());
2455 VerifyStackMapConstant(SO.getNumGcMapEntriesIdx());
2456
2457 // Verify that all explicit statepoint defs are tied to gc operands as
2458 // they are expected to be a relocation of gc operands.
2459 unsigned FirstGCPtrIdx = SO.getFirstGCPtrIdx();
2460 unsigned LastGCPtrIdx = SO.getNumAllocaIdx() - 2;
2461 for (unsigned Idx = 0; Idx < MI->getNumDefs(); Idx++) {
2462 unsigned UseOpIdx;
2463 if (!MI->isRegTiedToUseOperand(Idx, &UseOpIdx)) {
2464 report("STATEPOINT defs expected to be tied", MI);
2465 break;
2466 }
2467 if (UseOpIdx < FirstGCPtrIdx || UseOpIdx > LastGCPtrIdx) {
2468 report("STATEPOINT def tied to non-gc operand", MI);
2469 break;
2470 }
2471 }
2472
2473 // TODO: verify we have properly encoded deopt arguments
2474 } break;
2475 case TargetOpcode::INSERT_SUBREG: {
2476 unsigned InsertedSize;
2477 if (unsigned SubIdx = MI->getOperand(2).getSubReg())
2478 InsertedSize = TRI->getSubRegIdxSize(SubIdx);
2479 else
2480 InsertedSize = TRI->getRegSizeInBits(MI->getOperand(2).getReg(), *MRI);
2481 unsigned SubRegSize = TRI->getSubRegIdxSize(MI->getOperand(3).getImm());
2482 if (SubRegSize < InsertedSize) {
2483 report("INSERT_SUBREG expected inserted value to have equal or lesser "
2484 "size than the subreg it was inserted into", MI);
2485 break;
2486 }
2487 } break;
2488 case TargetOpcode::REG_SEQUENCE: {
2489 unsigned NumOps = MI->getNumOperands();
2490 if (!(NumOps & 1)) {
2491 report("Invalid number of operands for REG_SEQUENCE", MI);
2492 break;
2493 }
2494
2495 for (unsigned I = 1; I != NumOps; I += 2) {
2496 const MachineOperand &RegOp = MI->getOperand(I);
2497 const MachineOperand &SubRegOp = MI->getOperand(I + 1);
2498
2499 if (!RegOp.isReg())
2500 report("Invalid register operand for REG_SEQUENCE", &RegOp, I);
2501
2502 if (!SubRegOp.isImm() || SubRegOp.getImm() == 0 ||
2503 SubRegOp.getImm() >= TRI->getNumSubRegIndices()) {
2504 report("Invalid subregister index operand for REG_SEQUENCE",
2505 &SubRegOp, I + 1);
2506 }
2507 }
2508
2509 Register DstReg = MI->getOperand(0).getReg();
2510 if (DstReg.isPhysical())
2511 report("REG_SEQUENCE does not support physical register results", MI);
2512
2513 if (MI->getOperand(0).getSubReg())
2514 report("Invalid subreg result for REG_SEQUENCE", MI);
2515
2516 break;
2517 }
2518 }
2519}
2520
2521void
2522MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
2523 const MachineInstr *MI = MO->getParent();
2524 const MCInstrDesc &MCID = MI->getDesc();
2525 unsigned NumDefs = MCID.getNumDefs();
2526 if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
2527 NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
2528
2529 // The first MCID.NumDefs operands must be explicit register defines
2530 if (MONum < NumDefs) {
2531 const MCOperandInfo &MCOI = MCID.operands()[MONum];
2532 if (!MO->isReg())
2533 report("Explicit definition must be a register", MO, MONum);
2534 else if (!MO->isDef() && !MCOI.isOptionalDef())
2535 report("Explicit definition marked as use", MO, MONum);
2536 else if (MO->isImplicit())
2537 report("Explicit definition marked as implicit", MO, MONum);
2538 } else if (MONum < MCID.getNumOperands()) {
2539 const MCOperandInfo &MCOI = MCID.operands()[MONum];
2540 // Don't check if it's the last operand in a variadic instruction. See,
2541 // e.g., LDM_RET in the arm back end. Check non-variadic operands only.
2542 bool IsOptional = MI->isVariadic() && MONum == MCID.getNumOperands() - 1;
2543 if (!IsOptional) {
2544 if (MO->isReg()) {
2545 if (MO->isDef() && !MCOI.isOptionalDef() && !MCID.variadicOpsAreDefs())
2546 report("Explicit operand marked as def", MO, MONum);
2547 if (MO->isImplicit())
2548 report("Explicit operand marked as implicit", MO, MONum);
2549 }
2550
2551 // Check that an instruction has register operands only as expected.
2552 if (MCOI.OperandType == MCOI::OPERAND_REGISTER &&
2553 !MO->isReg() && !MO->isFI())
2554 report("Expected a register operand.", MO, MONum);
2555 if (MO->isReg()) {
2556 if (MCOI.OperandType == MCOI::OPERAND_IMMEDIATE ||
2557 (MCOI.OperandType == MCOI::OPERAND_PCREL &&
2558 !TII->isPCRelRegisterOperandLegal(*MO)))
2559 report("Expected a non-register operand.", MO, MONum);
2560 }
2561 }
2562
2563 int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
2564 if (TiedTo != -1) {
2565 if (!MO->isReg())
2566 report("Tied use must be a register", MO, MONum);
2567 else if (!MO->isTied())
2568 report("Operand should be tied", MO, MONum);
2569 else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
2570 report("Tied def doesn't match MCInstrDesc", MO, MONum);
2571 else if (MO->getReg().isPhysical()) {
2572 const MachineOperand &MOTied = MI->getOperand(TiedTo);
2573 if (!MOTied.isReg())
2574 report("Tied counterpart must be a register", &MOTied, TiedTo);
2575 else if (MOTied.getReg().isPhysical() &&
2576 MO->getReg() != MOTied.getReg())
2577 report("Tied physical registers must match.", &MOTied, TiedTo);
2578 }
2579 } else if (MO->isReg() && MO->isTied())
2580 report("Explicit operand should not be tied", MO, MONum);
2581 } else if (!MI->isVariadic()) {
2582 // ARM adds %reg0 operands to indicate predicates. We'll allow that.
2583 if (!MO->isValidExcessOperand())
2584 report("Extra explicit operand on non-variadic instruction", MO, MONum);
2585 }
2586
2587 // Verify earlyClobber def operand
2588 if (MCID.getOperandConstraint(MONum, MCOI::EARLY_CLOBBER) != -1) {
2589 if (!MO->isReg())
2590 report("Early clobber must be a register", MI);
2591 if (!MO->isEarlyClobber())
2592 report("Missing earlyClobber flag", MI);
2593 }
2594
2595 switch (MO->getType()) {
2597 // Verify debug flag on debug instructions. Check this first because reg0
2598 // indicates an undefined debug value.
2599 if (MI->isDebugInstr() && MO->isUse()) {
2600 if (!MO->isDebug())
2601 report("Register operand must be marked debug", MO, MONum);
2602 } else if (MO->isDebug()) {
2603 report("Register operand must not be marked debug", MO, MONum);
2604 }
2605
2606 const Register Reg = MO->getReg();
2607 if (!Reg)
2608 return;
2609 if (MRI->tracksLiveness() && !MI->isDebugInstr())
2610 checkLiveness(MO, MONum);
2611
2612 if (MO->isDef() && MO->isUndef() && !MO->getSubReg() &&
2613 MO->getReg().isVirtual()) // TODO: Apply to physregs too
2614 report("Undef virtual register def operands require a subregister", MO, MONum);
2615
2616 // Verify the consistency of tied operands.
2617 if (MO->isTied()) {
2618 unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
2619 const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
2620 if (!OtherMO.isReg())
2621 report("Must be tied to a register", MO, MONum);
2622 if (!OtherMO.isTied())
2623 report("Missing tie flags on tied operand", MO, MONum);
2624 if (MI->findTiedOperandIdx(OtherIdx) != MONum)
2625 report("Inconsistent tie links", MO, MONum);
2626 if (MONum < MCID.getNumDefs()) {
2627 if (OtherIdx < MCID.getNumOperands()) {
2628 if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
2629 report("Explicit def tied to explicit use without tie constraint",
2630 MO, MONum);
2631 } else {
2632 if (!OtherMO.isImplicit())
2633 report("Explicit def should be tied to implicit use", MO, MONum);
2634 }
2635 }
2636 }
2637
2638 // Verify two-address constraints after the twoaddressinstruction pass.
2639 // Both twoaddressinstruction pass and phi-node-elimination pass call
2640 // MRI->leaveSSA() to set MF as not IsSSA, we should do the verification
2641 // after twoaddressinstruction pass not after phi-node-elimination pass. So
2642 // we shouldn't use the IsSSA as the condition, we should based on
2643 // TiedOpsRewritten property to verify two-address constraints, this
2644 // property will be set in twoaddressinstruction pass.
2645 unsigned DefIdx;
2646 if (MF->getProperties().hasTiedOpsRewritten() && MO->isUse() &&
2647 MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
2648 Reg != MI->getOperand(DefIdx).getReg())
2649 report("Two-address instruction operands must be identical", MO, MONum);
2650
2651 // Check register classes.
2652 unsigned SubIdx = MO->getSubReg();
2653
2654 if (Reg.isPhysical()) {
2655 if (SubIdx) {
2656 report("Illegal subregister index for physical register", MO, MONum);
2657 return;
2658 }
2659 if (MONum < MCID.getNumOperands()) {
2660 if (const TargetRegisterClass *DRC =
2661 TII->getRegClass(MCID, MONum, TRI)) {
2662 if (!DRC->contains(Reg)) {
2663 report("Illegal physical register for instruction", MO, MONum);
2664 OS << printReg(Reg, TRI) << " is not a "
2665 << TRI->getRegClassName(DRC) << " register.\n";
2666 }
2667 }
2668 }
2669 if (MO->isRenamable()) {
2670 if (MRI->isReserved(Reg)) {
2671 report("isRenamable set on reserved register", MO, MONum);
2672 return;
2673 }
2674 }
2675 } else {
2676 // Virtual register.
2677 const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
2678 if (!RC) {
2679 // This is a generic virtual register.
2680
2681 // Do not allow undef uses for generic virtual registers. This ensures
2682 // getVRegDef can never fail and return null on a generic register.
2683 //
2684 // FIXME: This restriction should probably be broadened to all SSA
2685 // MIR. However, DetectDeadLanes/ProcessImplicitDefs technically still
2686 // run on the SSA function just before phi elimination.
2687 if (MO->isUndef())
2688 report("Generic virtual register use cannot be undef", MO, MONum);
2689
2690 // Debug value instruction is permitted to use undefined vregs.
2691 // This is a performance measure to skip the overhead of immediately
2692 // pruning unused debug operands. The final undef substitution occurs
2693 // when debug values are allocated in LDVImpl::handleDebugValue, so
2694 // these verifications always apply after this pass.
2695 if (isFunctionTracksDebugUserValues || !MO->isUse() ||
2696 !MI->isDebugValue() || !MRI->def_empty(Reg)) {
2697 // If we're post-Select, we can't have gvregs anymore.
2698 if (isFunctionSelected) {
2699 report("Generic virtual register invalid in a Selected function",
2700 MO, MONum);
2701 return;
2702 }
2703
2704 // The gvreg must have a type and it must not have a SubIdx.
2705 LLT Ty = MRI->getType(Reg);
2706 if (!Ty.isValid()) {
2707 report("Generic virtual register must have a valid type", MO,
2708 MONum);
2709 return;
2710 }
2711
2712 const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
2713 const RegisterBankInfo *RBI = MF->getSubtarget().getRegBankInfo();
2714
2715 // If we're post-RegBankSelect, the gvreg must have a bank.
2716 if (!RegBank && isFunctionRegBankSelected) {
2717 report("Generic virtual register must have a bank in a "
2718 "RegBankSelected function",
2719 MO, MONum);
2720 return;
2721 }
2722
2723 // Make sure the register fits into its register bank if any.
2724 if (RegBank && Ty.isValid() && !Ty.isScalableVector() &&
2725 RBI->getMaximumSize(RegBank->getID()) < Ty.getSizeInBits()) {
2726 report("Register bank is too small for virtual register", MO,
2727 MONum);
2728 OS << "Register bank " << RegBank->getName() << " too small("
2729 << RBI->getMaximumSize(RegBank->getID()) << ") to fit "
2730 << Ty.getSizeInBits() << "-bits\n";
2731 return;
2732 }
2733 }
2734
2735 if (SubIdx) {
2736 report("Generic virtual register does not allow subregister index", MO,
2737 MONum);
2738 return;
2739 }
2740
2741 // If this is a target specific instruction and this operand
2742 // has register class constraint, the virtual register must
2743 // comply to it.
2744 if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
2745 MONum < MCID.getNumOperands() &&
2746 TII->getRegClass(MCID, MONum, TRI)) {
2747 report("Virtual register does not match instruction constraint", MO,
2748 MONum);
2749 OS << "Expect register class "
2750 << TRI->getRegClassName(TII->getRegClass(MCID, MONum, TRI))
2751 << " but got nothing\n";
2752 return;
2753 }
2754
2755 break;
2756 }
2757 if (SubIdx) {
2758 const TargetRegisterClass *SRC =
2759 TRI->getSubClassWithSubReg(RC, SubIdx);
2760 if (!SRC) {
2761 report("Invalid subregister index for virtual register", MO, MONum);
2762 OS << "Register class " << TRI->getRegClassName(RC)
2763 << " does not support subreg index "
2764 << TRI->getSubRegIndexName(SubIdx) << '\n';
2765 return;
2766 }
2767 if (RC != SRC) {
2768 report("Invalid register class for subregister index", MO, MONum);
2769 OS << "Register class " << TRI->getRegClassName(RC)
2770 << " does not fully support subreg index "
2771 << TRI->getSubRegIndexName(SubIdx) << '\n';
2772 return;
2773 }
2774 }
2775 if (MONum < MCID.getNumOperands()) {
2776 if (const TargetRegisterClass *DRC =
2777 TII->getRegClass(MCID, MONum, TRI)) {
2778 if (SubIdx) {
2779 const TargetRegisterClass *SuperRC =
2780 TRI->getLargestLegalSuperClass(RC, *MF);
2781 if (!SuperRC) {
2782 report("No largest legal super class exists.", MO, MONum);
2783 return;
2784 }
2785 DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
2786 if (!DRC) {
2787 report("No matching super-reg register class.", MO, MONum);
2788 return;
2789 }
2790 }
2791 if (!RC->hasSuperClassEq(DRC)) {
2792 report("Illegal virtual register for instruction", MO, MONum);
2793 OS << "Expected a " << TRI->getRegClassName(DRC)
2794 << " register, but got a " << TRI->getRegClassName(RC)
2795 << " register\n";
2796 }
2797 }
2798 }
2799 }
2800 break;
2801 }
2802
2804 regMasks.push_back(MO->getRegMask());
2805 break;
2806
2808 if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
2809 report("PHI operand is not in the CFG", MO, MONum);
2810 break;
2811
2813 if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
2814 LiveInts && !LiveInts->isNotInMIMap(*MI)) {
2815 int FI = MO->getIndex();
2816 LiveInterval &LI = LiveStks->getInterval(FI);
2817 SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
2818
2819 bool stores = MI->mayStore();
2820 bool loads = MI->mayLoad();
2821 // For a memory-to-memory move, we need to check if the frame
2822 // index is used for storing or loading, by inspecting the
2823 // memory operands.
2824 if (stores && loads) {
2825 for (auto *MMO : MI->memoperands()) {
2826 const PseudoSourceValue *PSV = MMO->getPseudoValue();
2827 if (PSV == nullptr) continue;
2830 if (Value == nullptr) continue;
2831 if (Value->getFrameIndex() != FI) continue;
2832
2833 if (MMO->isStore())
2834 loads = false;
2835 else
2836 stores = false;
2837 break;
2838 }
2839 if (loads == stores)
2840 report("Missing fixed stack memoperand.", MI);
2841 }
2842 if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
2843 report("Instruction loads from dead spill slot", MO, MONum);
2844 OS << "Live stack: " << LI << '\n';
2845 }
2846 if (stores && !LI.liveAt(Idx.getRegSlot())) {
2847 report("Instruction stores to dead spill slot", MO, MONum);
2848 OS << "Live stack: " << LI << '\n';
2849 }
2850 }
2851 break;
2852
2854 if (MO->getCFIIndex() >= MF->getFrameInstructions().size())
2855 report("CFI instruction has invalid index", MO, MONum);
2856 break;
2857
2858 default:
2859 break;
2860 }
2861}
2862
2863void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
2864 unsigned MONum, SlotIndex UseIdx,
2865 const LiveRange &LR,
2866 VirtRegOrUnit VRegOrUnit,
2867 LaneBitmask LaneMask) {
2868 const MachineInstr *MI = MO->getParent();
2869
2870 if (!LR.verify()) {
2871 report("invalid live range", MO, MONum);
2872 report_context_liverange(LR);
2873 report_context_vreg_regunit(VRegOrUnit);
2874 report_context(UseIdx);
2875 return;
2876 }
2877
2878 LiveQueryResult LRQ = LR.Query(UseIdx);
2879 bool HasValue = LRQ.valueIn() || (MI->isPHI() && LRQ.valueOut());
2880 // Check if we have a segment at the use, note however that we only need one
2881 // live subregister range, the others may be dead.
2882 if (!HasValue && LaneMask.none()) {
2883 report("No live segment at use", MO, MONum);
2884 report_context_liverange(LR);
2885 report_context_vreg_regunit(VRegOrUnit);
2886 report_context(UseIdx);
2887 }
2888 if (MO->isKill() && !LRQ.isKill()) {
2889 report("Live range continues after kill flag", MO, MONum);
2890 report_context_liverange(LR);
2891 report_context_vreg_regunit(VRegOrUnit);
2892 if (LaneMask.any())
2893 report_context_lanemask(LaneMask);
2894 report_context(UseIdx);
2895 }
2896}
2897
2898void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
2899 unsigned MONum, SlotIndex DefIdx,
2900 const LiveRange &LR,
2901 VirtRegOrUnit VRegOrUnit,
2902 bool SubRangeCheck,
2903 LaneBitmask LaneMask) {
2904 if (!LR.verify()) {
2905 report("invalid live range", MO, MONum);
2906 report_context_liverange(LR);
2907 report_context_vreg_regunit(VRegOrUnit);
2908 if (LaneMask.any())
2909 report_context_lanemask(LaneMask);
2910 report_context(DefIdx);
2911 }
2912
2913 if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
2914 // The LR can correspond to the whole reg and its def slot is not obliged
2915 // to be the same as the MO' def slot. E.g. when we check here "normal"
2916 // subreg MO but there is other EC subreg MO in the same instruction so the
2917 // whole reg has EC def slot and differs from the currently checked MO' def
2918 // slot. For example:
2919 // %0 [16e,32r:0) 0@16e L..3 [16e,32r:0) 0@16e L..C [16r,32r:0) 0@16r
2920 // Check that there is an early-clobber def of the same superregister
2921 // somewhere is performed in visitMachineFunctionAfter()
2922 if (((SubRangeCheck || MO->getSubReg() == 0) && VNI->def != DefIdx) ||
2923 !SlotIndex::isSameInstr(VNI->def, DefIdx) ||
2924 (VNI->def != DefIdx &&
2925 (!VNI->def.isEarlyClobber() || !DefIdx.isRegister()))) {
2926 report("Inconsistent valno->def", MO, MONum);
2927 report_context_liverange(LR);
2928 report_context_vreg_regunit(VRegOrUnit);
2929 if (LaneMask.any())
2930 report_context_lanemask(LaneMask);
2931 report_context(*VNI);
2932 report_context(DefIdx);
2933 }
2934 } else {
2935 report("No live segment at def", MO, MONum);
2936 report_context_liverange(LR);
2937 report_context_vreg_regunit(VRegOrUnit);
2938 if (LaneMask.any())
2939 report_context_lanemask(LaneMask);
2940 report_context(DefIdx);
2941 }
2942 // Check that, if the dead def flag is present, LiveInts agree.
2943 if (MO->isDead()) {
2944 LiveQueryResult LRQ = LR.Query(DefIdx);
2945 if (!LRQ.isDeadDef()) {
2946 assert(VRegOrUnit.isVirtualReg() && "Expecting a virtual register.");
2947 // A dead subreg def only tells us that the specific subreg is dead. There
2948 // could be other non-dead defs of other subregs, or we could have other
2949 // parts of the register being live through the instruction. So unless we
2950 // are checking liveness for a subrange it is ok for the live range to
2951 // continue, given that we have a dead def of a subregister.
2952 if (SubRangeCheck || MO->getSubReg() == 0) {
2953 report("Live range continues after dead def flag", MO, MONum);
2954 report_context_liverange(LR);
2955 report_context_vreg_regunit(VRegOrUnit);
2956 if (LaneMask.any())
2957 report_context_lanemask(LaneMask);
2958 }
2959 }
2960 }
2961}
2962
2963void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
2964 const MachineInstr *MI = MO->getParent();
2965 const Register Reg = MO->getReg();
2966 const unsigned SubRegIdx = MO->getSubReg();
2967
2968 const LiveInterval *LI = nullptr;
2969 if (LiveInts && Reg.isVirtual()) {
2970 if (LiveInts->hasInterval(Reg)) {
2971 LI = &LiveInts->getInterval(Reg);
2972 if (SubRegIdx != 0 && (MO->isDef() || !MO->isUndef()) && !LI->empty() &&
2973 !LI->hasSubRanges() && MRI->shouldTrackSubRegLiveness(Reg))
2974 report("Live interval for subreg operand has no subranges", MO, MONum);
2975 } else {
2976 report("Virtual register has no live interval", MO, MONum);
2977 }
2978 }
2979
2980 // Both use and def operands can read a register.
2981 if (MO->readsReg()) {
2982 if (MO->isKill())
2983 addRegWithSubRegs(regsKilled, Reg);
2984
2985 // Check that LiveVars knows this kill (unless we are inside a bundle, in
2986 // which case we have already checked that LiveVars knows any kills on the
2987 // bundle header instead).
2988 if (LiveVars && Reg.isVirtual() && MO->isKill() &&
2989 !MI->isBundledWithPred()) {
2991 if (!is_contained(VI.Kills, MI))
2992 report("Kill missing from LiveVariables", MO, MONum);
2993 }
2994
2995 // Check LiveInts liveness and kill.
2996 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
2997 SlotIndex UseIdx;
2998 if (MI->isPHI()) {
2999 // PHI use occurs on the edge, so check for live out here instead.
3000 UseIdx = LiveInts->getMBBEndIdx(
3001 MI->getOperand(MONum + 1).getMBB()).getPrevSlot();
3002 } else {
3003 UseIdx = LiveInts->getInstructionIndex(*MI);
3004 }
3005 // Check the cached regunit intervals.
3006 if (Reg.isPhysical() && !isReserved(Reg)) {
3007 for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg())) {
3008 if (MRI->isReservedRegUnit(Unit))
3009 continue;
3010 if (const LiveRange *LR = LiveInts->getCachedRegUnit(Unit))
3011 checkLivenessAtUse(MO, MONum, UseIdx, *LR, VirtRegOrUnit(Unit));
3012 }
3013 }
3014
3015 if (Reg.isVirtual()) {
3016 // This is a virtual register interval.
3017 checkLivenessAtUse(MO, MONum, UseIdx, *LI, VirtRegOrUnit(Reg));
3018
3019 if (LI->hasSubRanges() && !MO->isDef()) {
3020 LaneBitmask MOMask = SubRegIdx != 0
3021 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
3022 : MRI->getMaxLaneMaskForVReg(Reg);
3023 LaneBitmask LiveInMask;
3024 for (const LiveInterval::SubRange &SR : LI->subranges()) {
3025 if ((MOMask & SR.LaneMask).none())
3026 continue;
3027 checkLivenessAtUse(MO, MONum, UseIdx, SR, VirtRegOrUnit(Reg),
3028 SR.LaneMask);
3029 LiveQueryResult LRQ = SR.Query(UseIdx);
3030 if (LRQ.valueIn() || (MI->isPHI() && LRQ.valueOut()))
3031 LiveInMask |= SR.LaneMask;
3032 }
3033 // At least parts of the register has to be live at the use.
3034 if ((LiveInMask & MOMask).none()) {
3035 report("No live subrange at use", MO, MONum);
3036 report_context(*LI);
3037 report_context(UseIdx);
3038 }
3039 // For PHIs all lanes should be live
3040 if (MI->isPHI() && LiveInMask != MOMask) {
3041 report("Not all lanes of PHI source live at use", MO, MONum);
3042 report_context(*LI);
3043 report_context(UseIdx);
3044 }
3045 }
3046 }
3047 }
3048
3049 // Use of a dead register.
3050 if (!regsLive.count(Reg)) {
3051 if (Reg.isPhysical()) {
3052 // Reserved registers may be used even when 'dead'.
3053 bool Bad = !isReserved(Reg);
3054 // We are fine if just any subregister has a defined value.
3055 if (Bad) {
3056
3057 for (const MCPhysReg &SubReg : TRI->subregs(Reg)) {
3058 if (regsLive.count(SubReg)) {
3059 Bad = false;
3060 break;
3061 }
3062 }
3063 }
3064 // If there is an additional implicit-use of a super register we stop
3065 // here. By definition we are fine if the super register is not
3066 // (completely) dead, if the complete super register is dead we will
3067 // get a report for its operand.
3068 if (Bad) {
3069 for (const MachineOperand &MOP : MI->uses()) {
3070 if (!MOP.isReg() || !MOP.isImplicit())
3071 continue;
3072
3073 if (!MOP.getReg().isPhysical())
3074 continue;
3075
3076 if (MOP.getReg() != Reg &&
3077 all_of(TRI->regunits(Reg), [&](const MCRegUnit RegUnit) {
3078 return llvm::is_contained(TRI->regunits(MOP.getReg()),
3079 RegUnit);
3080 }))
3081 Bad = false;
3082 }
3083 }
3084 if (Bad)
3085 report("Using an undefined physical register", MO, MONum);
3086 } else if (MRI->def_empty(Reg)) {
3087 report("Reading virtual register without a def", MO, MONum);
3088 } else {
3089 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
3090 // We don't know which virtual registers are live in, so only complain
3091 // if vreg was killed in this MBB. Otherwise keep track of vregs that
3092 // must be live in. PHI instructions are handled separately.
3093 if (MInfo.regsKilled.count(Reg))
3094 report("Using a killed virtual register", MO, MONum);
3095 else if (!MI->isPHI())
3096 MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
3097 }
3098 }
3099 }
3100
3101 if (MO->isDef()) {
3102 // Register defined.
3103 // TODO: verify that earlyclobber ops are not used.
3104 if (MO->isDead())
3105 addRegWithSubRegs(regsDead, Reg);
3106 else
3107 addRegWithSubRegs(regsDefined, Reg);
3108
3109 // Verify SSA form.
3110 if (MRI->isSSA() && Reg.isVirtual() &&
3111 std::next(MRI->def_begin(Reg)) != MRI->def_end())
3112 report("Multiple virtual register defs in SSA form", MO, MONum);
3113
3114 // Check LiveInts for a live segment, but only for virtual registers.
3115 if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
3116 SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
3117 DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
3118
3119 if (Reg.isVirtual()) {
3120 checkLivenessAtDef(MO, MONum, DefIdx, *LI, VirtRegOrUnit(Reg));
3121
3122 if (LI->hasSubRanges()) {
3123 LaneBitmask MOMask = SubRegIdx != 0
3124 ? TRI->getSubRegIndexLaneMask(SubRegIdx)
3125 : MRI->getMaxLaneMaskForVReg(Reg);
3126 for (const LiveInterval::SubRange &SR : LI->subranges()) {
3127 if ((SR.LaneMask & MOMask).none())
3128 continue;
3129 checkLivenessAtDef(MO, MONum, DefIdx, SR, VirtRegOrUnit(Reg), true,
3130 SR.LaneMask);
3131 }
3132 }
3133 }
3134 }
3135 }
3136}
3137
3138// This function gets called after visiting all instructions in a bundle. The
3139// argument points to the bundle header.
3140// Normal stand-alone instructions are also considered 'bundles', and this
3141// function is called for all of them.
3142void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
3143 BBInfo &MInfo = MBBInfoMap[MI->getParent()];
3144 set_union(MInfo.regsKilled, regsKilled);
3145 set_subtract(regsLive, regsKilled); regsKilled.clear();
3146 // Kill any masked registers.
3147 while (!regMasks.empty()) {
3148 const uint32_t *Mask = regMasks.pop_back_val();
3149 for (Register Reg : regsLive)
3150 if (Reg.isPhysical() &&
3152 regsDead.push_back(Reg);
3153 }
3154 set_subtract(regsLive, regsDead); regsDead.clear();
3155 set_union(regsLive, regsDefined); regsDefined.clear();
3156}
3157
3158void
3159MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
3160 MBBInfoMap[MBB].regsLiveOut = regsLive;
3161 regsLive.clear();
3162
3163 if (Indexes) {
3164 SlotIndex stop = Indexes->getMBBEndIdx(MBB);
3165 if (!(stop > lastIndex)) {
3166 report("Block ends before last instruction index", MBB);
3167 OS << "Block ends at " << stop << " last instruction was at " << lastIndex
3168 << '\n';
3169 }
3170 lastIndex = stop;
3171 }
3172}
3173
3174namespace {
3175// This implements a set of registers that serves as a filter: can filter other
3176// sets by passing through elements not in the filter and blocking those that
3177// are. Any filter implicitly includes the full set of physical registers upon
3178// creation, thus filtering them all out. The filter itself as a set only grows,
3179// and needs to be as efficient as possible.
3180struct VRegFilter {
3181 // Add elements to the filter itself. \pre Input set \p FromRegSet must have
3182 // no duplicates. Both virtual and physical registers are fine.
3183 template <typename RegSetT> void add(const RegSetT &FromRegSet) {
3184 SmallVector<Register, 0> VRegsBuffer;
3185 filterAndAdd(FromRegSet, VRegsBuffer);
3186 }
3187 // Filter \p FromRegSet through the filter and append passed elements into \p
3188 // ToVRegs. All elements appended are then added to the filter itself.
3189 // \returns true if anything changed.
3190 template <typename RegSetT>
3191 bool filterAndAdd(const RegSetT &FromRegSet,
3192 SmallVectorImpl<Register> &ToVRegs) {
3193 unsigned SparseUniverse = Sparse.size();
3194 unsigned NewSparseUniverse = SparseUniverse;
3195 unsigned NewDenseSize = Dense.size();
3196 size_t Begin = ToVRegs.size();
3197 for (Register Reg : FromRegSet) {
3198 if (!Reg.isVirtual())
3199 continue;
3200 unsigned Index = Reg.virtRegIndex();
3201 if (Index < SparseUniverseMax) {
3202 if (Index < SparseUniverse && Sparse.test(Index))
3203 continue;
3204 NewSparseUniverse = std::max(NewSparseUniverse, Index + 1);
3205 } else {
3206 if (Dense.count(Reg))
3207 continue;
3208 ++NewDenseSize;
3209 }
3210 ToVRegs.push_back(Reg);
3211 }
3212 size_t End = ToVRegs.size();
3213 if (Begin == End)
3214 return false;
3215 // Reserving space in sets once performs better than doing so continuously
3216 // and pays easily for double look-ups (even in Dense with SparseUniverseMax
3217 // tuned all the way down) and double iteration (the second one is over a
3218 // SmallVector, which is a lot cheaper compared to DenseSet or BitVector).
3219 Sparse.resize(NewSparseUniverse);
3220 Dense.reserve(NewDenseSize);
3221 for (unsigned I = Begin; I < End; ++I) {
3222 Register Reg = ToVRegs[I];
3223 unsigned Index = Reg.virtRegIndex();
3224 if (Index < SparseUniverseMax)
3225 Sparse.set(Index);
3226 else
3227 Dense.insert(Reg);
3228 }
3229 return true;
3230 }
3231
3232private:
3233 static constexpr unsigned SparseUniverseMax = 10 * 1024 * 8;
3234 // VRegs indexed within SparseUniverseMax are tracked by Sparse, those beyond
3235 // are tracked by Dense. The only purpose of the threshold and the Dense set
3236 // is to have a reasonably growing memory usage in pathological cases (large
3237 // number of very sparse VRegFilter instances live at the same time). In
3238 // practice even in the worst-by-execution time cases having all elements
3239 // tracked by Sparse (very large SparseUniverseMax scenario) tends to be more
3240 // space efficient than if tracked by Dense. The threshold is set to keep the
3241 // worst-case memory usage within 2x of figures determined empirically for
3242 // "all Dense" scenario in such worst-by-execution-time cases.
3243 BitVector Sparse;
3244 DenseSet<Register> Dense;
3245};
3246
3247// Implements both a transfer function and a (binary, in-place) join operator
3248// for a dataflow over register sets with set union join and filtering transfer
3249// (out_b = in_b \ filter_b). filter_b is expected to be set-up ahead of time.
3250// Maintains out_b as its state, allowing for O(n) iteration over it at any
3251// time, where n is the size of the set (as opposed to O(U) where U is the
3252// universe). filter_b implicitly contains all physical registers at all times.
3253class FilteringVRegSet {
3254 VRegFilter Filter;
3256
3257public:
3258 // Set-up the filter_b. \pre Input register set \p RS must have no duplicates.
3259 // Both virtual and physical registers are fine.
3260 template <typename RegSetT> void addToFilter(const RegSetT &RS) {
3261 Filter.add(RS);
3262 }
3263 // Passes \p RS through the filter_b (transfer function) and adds what's left
3264 // to itself (out_b).
3265 template <typename RegSetT> bool add(const RegSetT &RS) {
3266 // Double-duty the Filter: to maintain VRegs a set (and the join operation
3267 // a set union) just add everything being added here to the Filter as well.
3268 return Filter.filterAndAdd(RS, VRegs);
3269 }
3270 using const_iterator = decltype(VRegs)::const_iterator;
3271 const_iterator begin() const { return VRegs.begin(); }
3272 const_iterator end() const { return VRegs.end(); }
3273 size_t size() const { return VRegs.size(); }
3274};
3275} // namespace
3276
3277// Calculate the largest possible vregsPassed sets. These are the registers that
3278// can pass through an MBB live, but may not be live every time. It is assumed
3279// that all vregsPassed sets are empty before the call.
3280void MachineVerifier::calcRegsPassed() {
3281 if (MF->empty())
3282 // ReversePostOrderTraversal doesn't handle empty functions.
3283 return;
3284
3285 for (const MachineBasicBlock *MB :
3287 FilteringVRegSet VRegs;
3288 BBInfo &Info = MBBInfoMap[MB];
3289 assert(Info.reachable);
3290
3291 VRegs.addToFilter(Info.regsKilled);
3292 VRegs.addToFilter(Info.regsLiveOut);
3293 for (const MachineBasicBlock *Pred : MB->predecessors()) {
3294 const BBInfo &PredInfo = MBBInfoMap[Pred];
3295 if (!PredInfo.reachable)
3296 continue;
3297
3298 VRegs.add(PredInfo.regsLiveOut);
3299 VRegs.add(PredInfo.vregsPassed);
3300 }
3301 Info.vregsPassed.reserve(VRegs.size());
3302 Info.vregsPassed.insert_range(VRegs);
3303 }
3304}
3305
3306// Calculate the set of virtual registers that must be passed through each basic
3307// block in order to satisfy the requirements of successor blocks. This is very
3308// similar to calcRegsPassed, only backwards.
3309void MachineVerifier::calcRegsRequired() {
3310 // First push live-in regs to predecessors' vregsRequired.
3312 for (const auto &MBB : *MF) {
3313 BBInfo &MInfo = MBBInfoMap[&MBB];
3314 for (const MachineBasicBlock *Pred : MBB.predecessors()) {
3315 BBInfo &PInfo = MBBInfoMap[Pred];
3316 if (PInfo.addRequired(MInfo.vregsLiveIn))
3317 todo.insert(Pred);
3318 }
3319
3320 // Handle the PHI node.
3321 for (const MachineInstr &MI : MBB.phis()) {
3322 for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
3323 // Skip those Operands which are undef regs or not regs.
3324 if (!MI.getOperand(i).isReg() || !MI.getOperand(i).readsReg())
3325 continue;
3326
3327 // Get register and predecessor for one PHI edge.
3328 Register Reg = MI.getOperand(i).getReg();
3329 const MachineBasicBlock *Pred = MI.getOperand(i + 1).getMBB();
3330
3331 BBInfo &PInfo = MBBInfoMap[Pred];
3332 if (PInfo.addRequired(Reg))
3333 todo.insert(Pred);
3334 }
3335 }
3336 }
3337
3338 // Iteratively push vregsRequired to predecessors. This will converge to the
3339 // same final state regardless of DenseSet iteration order.
3340 while (!todo.empty()) {
3341 const MachineBasicBlock *MBB = *todo.begin();
3342 todo.erase(MBB);
3343 BBInfo &MInfo = MBBInfoMap[MBB];
3344 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
3345 if (Pred == MBB)
3346 continue;
3347 BBInfo &SInfo = MBBInfoMap[Pred];
3348 if (SInfo.addRequired(MInfo.vregsRequired))
3349 todo.insert(Pred);
3350 }
3351 }
3352}
3353
3354// Check PHI instructions at the beginning of MBB. It is assumed that
3355// calcRegsPassed has been run so BBInfo::isLiveOut is valid.
3356void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) {
3357 BBInfo &MInfo = MBBInfoMap[&MBB];
3358
3360 for (const MachineInstr &Phi : MBB) {
3361 if (!Phi.isPHI())
3362 break;
3363 seen.clear();
3364
3365 const MachineOperand &MODef = Phi.getOperand(0);
3366 if (!MODef.isReg() || !MODef.isDef()) {
3367 report("Expected first PHI operand to be a register def", &MODef, 0);
3368 continue;
3369 }
3370 if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() ||
3371 MODef.isEarlyClobber() || MODef.isDebug())
3372 report("Unexpected flag on PHI operand", &MODef, 0);
3373 Register DefReg = MODef.getReg();
3374 if (!DefReg.isVirtual())
3375 report("Expected first PHI operand to be a virtual register", &MODef, 0);
3376
3377 for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
3378 const MachineOperand &MO0 = Phi.getOperand(I);
3379 if (!MO0.isReg()) {
3380 report("Expected PHI operand to be a register", &MO0, I);
3381 continue;
3382 }
3383 if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() ||
3384 MO0.isDebug() || MO0.isTied())
3385 report("Unexpected flag on PHI operand", &MO0, I);
3386
3387 const MachineOperand &MO1 = Phi.getOperand(I + 1);
3388 if (!MO1.isMBB()) {
3389 report("Expected PHI operand to be a basic block", &MO1, I + 1);
3390 continue;
3391 }
3392
3393 const MachineBasicBlock &Pre = *MO1.getMBB();
3394 if (!Pre.isSuccessor(&MBB)) {
3395 report("PHI input is not a predecessor block", &MO1, I + 1);
3396 continue;
3397 }
3398
3399 if (MInfo.reachable) {
3400 seen.insert(&Pre);
3401 BBInfo &PrInfo = MBBInfoMap[&Pre];
3402 if (!MO0.isUndef() && PrInfo.reachable &&
3403 !PrInfo.isLiveOut(MO0.getReg()))
3404 report("PHI operand is not live-out from predecessor", &MO0, I);
3405 }
3406 }
3407
3408 // Did we see all predecessors?
3409 if (MInfo.reachable) {
3410 for (MachineBasicBlock *Pred : MBB.predecessors()) {
3411 if (!seen.count(Pred)) {
3412 report("Missing PHI operand", &Phi);
3413 OS << printMBBReference(*Pred)
3414 << " is a predecessor according to the CFG.\n";
3415 }
3416 }
3417 }
3418 }
3419}
3420
3421static void
3423 std::function<void(const Twine &Message)> FailureCB,
3424 raw_ostream &OS) {
3426 CV.initialize(&OS, FailureCB, MF);
3427
3428 for (const auto &MBB : MF) {
3429 CV.visit(MBB);
3430 for (const auto &MI : MBB.instrs())
3431 CV.visit(MI);
3432 }
3433
3434 if (CV.sawTokens()) {
3435 DT.recalculate(const_cast<MachineFunction &>(MF));
3436 CV.verify(DT);
3437 }
3438}
3439
3440void MachineVerifier::visitMachineFunctionAfter() {
3441 auto FailureCB = [this](const Twine &Message) {
3442 report(Message.str().c_str(), MF);
3443 };
3444 verifyConvergenceControl(*MF, DT, FailureCB, OS);
3445
3446 calcRegsPassed();
3447
3448 for (const MachineBasicBlock &MBB : *MF)
3449 checkPHIOps(MBB);
3450
3451 // Now check liveness info if available
3452 calcRegsRequired();
3453
3454 // Check for killed virtual registers that should be live out.
3455 for (const auto &MBB : *MF) {
3456 BBInfo &MInfo = MBBInfoMap[&MBB];
3457 for (Register VReg : MInfo.vregsRequired)
3458 if (MInfo.regsKilled.count(VReg)) {
3459 report("Virtual register killed in block, but needed live out.", &MBB);
3460 OS << "Virtual register " << printReg(VReg)
3461 << " is used after the block.\n";
3462 }
3463 }
3464
3465 if (!MF->empty()) {
3466 BBInfo &MInfo = MBBInfoMap[&MF->front()];
3467 for (Register VReg : MInfo.vregsRequired) {
3468 report("Virtual register defs don't dominate all uses.", MF);
3469 report_context_vreg(VReg);
3470 }
3471 }
3472
3473 if (LiveVars)
3474 verifyLiveVariables();
3475 if (LiveInts)
3476 verifyLiveIntervals();
3477
3478 // Check live-in list of each MBB. If a register is live into MBB, check
3479 // that the register is in regsLiveOut of each predecessor block. Since
3480 // this must come from a definition in the predecessor or its live-in
3481 // list, this will catch a live-through case where the predecessor does not
3482 // have the register in its live-in list. This currently only checks
3483 // registers that have no aliases, are not allocatable and are not
3484 // reserved, which could mean a condition code register for instance.
3485 if (MRI->tracksLiveness())
3486 for (const auto &MBB : *MF)
3488 MCRegister LiveInReg = P.PhysReg;
3489 bool hasAliases = MCRegAliasIterator(LiveInReg, TRI, false).isValid();
3490 if (hasAliases || isAllocatable(LiveInReg) || isReserved(LiveInReg))
3491 continue;
3492 for (const MachineBasicBlock *Pred : MBB.predecessors()) {
3493 BBInfo &PInfo = MBBInfoMap[Pred];
3494 if (!PInfo.regsLiveOut.count(LiveInReg)) {
3495 report("Live in register not found to be live out from predecessor.",
3496 &MBB);
3497 OS << TRI->getName(LiveInReg) << " not found to be live out from "
3498 << printMBBReference(*Pred) << '\n';
3499 }
3500 }
3501 }
3502
3503 for (auto CSInfo : MF->getCallSitesInfo())
3504 if (!CSInfo.first->isCall())
3505 report("Call site info referencing instruction that is not call", MF);
3506
3507 // If there's debug-info, check that we don't have any duplicate value
3508 // tracking numbers.
3509 if (MF->getFunction().getSubprogram()) {
3510 DenseSet<unsigned> SeenNumbers;
3511 for (const auto &MBB : *MF) {
3512 for (const auto &MI : MBB) {
3513 if (auto Num = MI.peekDebugInstrNum()) {
3514 auto Result = SeenNumbers.insert((unsigned)Num);
3515 if (!Result.second)
3516 report("Instruction has a duplicated value tracking number", &MI);
3517 }
3518 }
3519 }
3520 }
3521}
3522
3523void MachineVerifier::verifyLiveVariables() {
3524 assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
3525 for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
3528 for (const auto &MBB : *MF) {
3529 BBInfo &MInfo = MBBInfoMap[&MBB];
3530
3531 // Our vregsRequired should be identical to LiveVariables' AliveBlocks
3532 if (MInfo.vregsRequired.count(Reg)) {
3533 if (!VI.AliveBlocks.test(MBB.getNumber())) {
3534 report("LiveVariables: Block missing from AliveBlocks", &MBB);
3535 OS << "Virtual register " << printReg(Reg)
3536 << " must be live through the block.\n";
3537 }
3538 } else {
3539 if (VI.AliveBlocks.test(MBB.getNumber())) {
3540 report("LiveVariables: Block should not be in AliveBlocks", &MBB);
3541 OS << "Virtual register " << printReg(Reg)
3542 << " is not needed live through the block.\n";
3543 }
3544 }
3545 }
3546 }
3547}
3548
3549void MachineVerifier::verifyLiveIntervals() {
3550 assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
3551 for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
3553
3554 // Spilling and splitting may leave unused registers around. Skip them.
3555 if (MRI->reg_nodbg_empty(Reg))
3556 continue;
3557
3558 if (!LiveInts->hasInterval(Reg)) {
3559 report("Missing live interval for virtual register", MF);
3560 OS << printReg(Reg, TRI) << " still has defs or uses\n";
3561 continue;
3562 }
3563
3564 const LiveInterval &LI = LiveInts->getInterval(Reg);
3565 assert(Reg == LI.reg() && "Invalid reg to interval mapping");
3566 verifyLiveInterval(LI);
3567 }
3568
3569 // Verify all the cached regunit intervals.
3570 for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
3571 if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
3572 verifyLiveRange(*LR, VirtRegOrUnit(i));
3573}
3574
3575void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
3576 const VNInfo *VNI,
3577 VirtRegOrUnit VRegOrUnit,
3578 LaneBitmask LaneMask) {
3579 if (VNI->isUnused())
3580 return;
3581
3582 const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
3583
3584 if (!DefVNI) {
3585 report("Value not live at VNInfo def and not marked unused", MF);
3586 report_context(LR, VRegOrUnit, LaneMask);
3587 report_context(*VNI);
3588 return;
3589 }
3590
3591 if (DefVNI != VNI) {
3592 report("Live segment at def has different VNInfo", MF);
3593 report_context(LR, VRegOrUnit, LaneMask);
3594 report_context(*VNI);
3595 return;
3596 }
3597
3598 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
3599 if (!MBB) {
3600 report("Invalid VNInfo definition index", MF);
3601 report_context(LR, VRegOrUnit, LaneMask);
3602 report_context(*VNI);
3603 return;
3604 }
3605
3606 if (VNI->isPHIDef()) {
3607 if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
3608 report("PHIDef VNInfo is not defined at MBB start", MBB);
3609 report_context(LR, VRegOrUnit, LaneMask);
3610 report_context(*VNI);
3611 }
3612 return;
3613 }
3614
3615 // Non-PHI def.
3616 const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
3617 if (!MI) {
3618 report("No instruction at VNInfo def index", MBB);
3619 report_context(LR, VRegOrUnit, LaneMask);
3620 report_context(*VNI);
3621 return;
3622 }
3623
3624 bool hasDef = false;
3625 bool isEarlyClobber = false;
3626 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
3627 if (!MOI->isReg() || !MOI->isDef())
3628 continue;
3629 if (VRegOrUnit.isVirtualReg()) {
3630 if (MOI->getReg() != VRegOrUnit.asVirtualReg())
3631 continue;
3632 } else {
3633 if (!MOI->getReg().isPhysical() ||
3634 !TRI->hasRegUnit(MOI->getReg(), VRegOrUnit.asMCRegUnit()))
3635 continue;
3636 }
3637 if (LaneMask.any() &&
3638 (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
3639 continue;
3640 hasDef = true;
3641 if (MOI->isEarlyClobber())
3642 isEarlyClobber = true;
3643 }
3644
3645 if (!hasDef) {
3646 report("Defining instruction does not modify register", MI);
3647 report_context(LR, VRegOrUnit, LaneMask);
3648 report_context(*VNI);
3649 }
3650
3651 // Early clobber defs begin at USE slots, but other defs must begin at
3652 // DEF slots.
3653 if (isEarlyClobber) {
3654 if (!VNI->def.isEarlyClobber()) {
3655 report("Early clobber def must be at an early-clobber slot", MBB);
3656 report_context(LR, VRegOrUnit, LaneMask);
3657 report_context(*VNI);
3658 }
3659 } else if (!VNI->def.isRegister()) {
3660 report("Non-PHI, non-early clobber def must be at a register slot", MBB);
3661 report_context(LR, VRegOrUnit, LaneMask);
3662 report_context(*VNI);
3663 }
3664}
3665
3666void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
3668 VirtRegOrUnit VRegOrUnit,
3669 LaneBitmask LaneMask) {
3670 const LiveRange::Segment &S = *I;
3671 const VNInfo *VNI = S.valno;
3672 assert(VNI && "Live segment has no valno");
3673
3674 if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
3675 report("Foreign valno in live segment", MF);
3676 report_context(LR, VRegOrUnit, LaneMask);
3677 report_context(S);
3678 report_context(*VNI);
3679 }
3680
3681 if (VNI->isUnused()) {
3682 report("Live segment valno is marked unused", MF);
3683 report_context(LR, VRegOrUnit, LaneMask);
3684 report_context(S);
3685 }
3686
3687 const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
3688 if (!MBB) {
3689 report("Bad start of live segment, no basic block", MF);
3690 report_context(LR, VRegOrUnit, LaneMask);
3691 report_context(S);
3692 return;
3693 }
3694 SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
3695 if (S.start != MBBStartIdx && S.start != VNI->def) {
3696 report("Live segment must begin at MBB entry or valno def", MBB);
3697 report_context(LR, VRegOrUnit, LaneMask);
3698 report_context(S);
3699 }
3700
3701 const MachineBasicBlock *EndMBB =
3702 LiveInts->getMBBFromIndex(S.end.getPrevSlot());
3703 if (!EndMBB) {
3704 report("Bad end of live segment, no basic block", MF);
3705 report_context(LR, VRegOrUnit, LaneMask);
3706 report_context(S);
3707 return;
3708 }
3709
3710 // Checks for non-live-out segments.
3711 if (S.end != LiveInts->getMBBEndIdx(EndMBB)) {
3712 // RegUnit intervals are allowed dead phis.
3713 if (!VRegOrUnit.isVirtualReg() && VNI->isPHIDef() && S.start == VNI->def &&
3714 S.end == VNI->def.getDeadSlot())
3715 return;
3716
3717 // The live segment is ending inside EndMBB
3718 const MachineInstr *MI =
3719 LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
3720 if (!MI) {
3721 report("Live segment doesn't end at a valid instruction", EndMBB);
3722 report_context(LR, VRegOrUnit, LaneMask);
3723 report_context(S);
3724 return;
3725 }
3726
3727 // The block slot must refer to a basic block boundary.
3728 if (S.end.isBlock()) {
3729 report("Live segment ends at B slot of an instruction", EndMBB);
3730 report_context(LR, VRegOrUnit, LaneMask);
3731 report_context(S);
3732 }
3733
3734 if (S.end.isDead()) {
3735 // Segment ends on the dead slot.
3736 // That means there must be a dead def.
3737 if (!SlotIndex::isSameInstr(S.start, S.end)) {
3738 report("Live segment ending at dead slot spans instructions", EndMBB);
3739 report_context(LR, VRegOrUnit, LaneMask);
3740 report_context(S);
3741 }
3742 }
3743
3744 // After tied operands are rewritten, a live segment can only end at an
3745 // early-clobber slot if it is being redefined by an early-clobber def.
3746 // TODO: Before tied operands are rewritten, a live segment can only end at
3747 // an early-clobber slot if the last use is tied to an early-clobber def.
3748 if (MF->getProperties().hasTiedOpsRewritten() && S.end.isEarlyClobber()) {
3749 if (I + 1 == LR.end() || (I + 1)->start != S.end) {
3750 report("Live segment ending at early clobber slot must be "
3751 "redefined by an EC def in the same instruction",
3752 EndMBB);
3753 report_context(LR, VRegOrUnit, LaneMask);
3754 report_context(S);
3755 }
3756 }
3757
3758 // The following checks only apply to virtual registers. Physreg liveness
3759 // is too weird to check.
3760 if (VRegOrUnit.isVirtualReg()) {
3761 // A live segment can end with either a redefinition, a kill flag on a
3762 // use, or a dead flag on a def.
3763 bool hasRead = false;
3764 bool hasSubRegDef = false;
3765 bool hasDeadDef = false;
3766 for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
3767 if (!MOI->isReg() || MOI->getReg() != VRegOrUnit.asVirtualReg())
3768 continue;
3769 unsigned Sub = MOI->getSubReg();
3770 LaneBitmask SLM =
3771 Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub) : LaneBitmask::getAll();
3772 if (MOI->isDef()) {
3773 if (Sub != 0) {
3774 hasSubRegDef = true;
3775 // An operand %0:sub0 reads %0:sub1..n. Invert the lane
3776 // mask for subregister defs. Read-undef defs will be handled by
3777 // readsReg below.
3778 SLM = ~SLM;
3779 }
3780 if (MOI->isDead())
3781 hasDeadDef = true;
3782 }
3783 if (LaneMask.any() && (LaneMask & SLM).none())
3784 continue;
3785 if (MOI->readsReg())
3786 hasRead = true;
3787 }
3788 if (S.end.isDead()) {
3789 // Make sure that the corresponding machine operand for a "dead" live
3790 // range has the dead flag. We cannot perform this check for subregister
3791 // liveranges as partially dead values are allowed.
3792 if (LaneMask.none() && !hasDeadDef) {
3793 report(
3794 "Instruction ending live segment on dead slot has no dead flag",
3795 MI);
3796 report_context(LR, VRegOrUnit, LaneMask);
3797 report_context(S);
3798 }
3799 } else {
3800 if (!hasRead) {
3801 // When tracking subregister liveness, the main range must start new
3802 // values on partial register writes, even if there is no read.
3803 if (!MRI->shouldTrackSubRegLiveness(VRegOrUnit.asVirtualReg()) ||
3804 LaneMask.any() || !hasSubRegDef) {
3805 report("Instruction ending live segment doesn't read the register",
3806 MI);
3807 report_context(LR, VRegOrUnit, LaneMask);
3808 report_context(S);
3809 }
3810 }
3811 }
3812 }
3813 }
3814
3815 // Now check all the basic blocks in this live segment.
3817 // Is this live segment the beginning of a non-PHIDef VN?
3818 if (S.start == VNI->def && !VNI->isPHIDef()) {
3819 // Not live-in to any blocks.
3820 if (MBB == EndMBB)
3821 return;
3822 // Skip this block.
3823 ++MFI;
3824 }
3825
3827 if (LaneMask.any()) {
3828 LiveInterval &OwnerLI = LiveInts->getInterval(VRegOrUnit.asVirtualReg());
3829 OwnerLI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes);
3830 }
3831
3832 while (true) {
3833 assert(LiveInts->isLiveInToMBB(LR, &*MFI));
3834 // We don't know how to track physregs into a landing pad.
3835 if (!VRegOrUnit.isVirtualReg() && MFI->isEHPad()) {
3836 if (&*MFI == EndMBB)
3837 break;
3838 ++MFI;
3839 continue;
3840 }
3841
3842 // Is VNI a PHI-def in the current block?
3843 bool IsPHI = VNI->isPHIDef() &&
3844 VNI->def == LiveInts->getMBBStartIdx(&*MFI);
3845
3846 // Check that VNI is live-out of all predecessors.
3847 for (const MachineBasicBlock *Pred : MFI->predecessors()) {
3848 SlotIndex PEnd = LiveInts->getMBBEndIdx(Pred);
3849 // Predecessor of landing pad live-out on last call.
3850 if (MFI->isEHPad()) {
3851 for (const MachineInstr &MI : llvm::reverse(*Pred)) {
3852 if (MI.isCall()) {
3853 PEnd = Indexes->getInstructionIndex(MI).getBoundaryIndex();
3854 break;
3855 }
3856 }
3857 }
3858 const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
3859
3860 // All predecessors must have a live-out value. However for a phi
3861 // instruction with subregister intervals
3862 // only one of the subregisters (not necessarily the current one) needs to
3863 // be defined.
3864 if (!PVNI && (LaneMask.none() || !IsPHI)) {
3865 if (LiveRangeCalc::isJointlyDominated(Pred, Undefs, *Indexes))
3866 continue;
3867 report("Register not marked live out of predecessor", Pred);
3868 report_context(LR, VRegOrUnit, LaneMask);
3869 report_context(*VNI);
3870 OS << " live into " << printMBBReference(*MFI) << '@'
3871 << LiveInts->getMBBStartIdx(&*MFI) << ", not live before " << PEnd
3872 << '\n';
3873 continue;
3874 }
3875
3876 // Only PHI-defs can take different predecessor values.
3877 if (!IsPHI && PVNI != VNI) {
3878 report("Different value live out of predecessor", Pred);
3879 report_context(LR, VRegOrUnit, LaneMask);
3880 OS << "Valno #" << PVNI->id << " live out of "
3881 << printMBBReference(*Pred) << '@' << PEnd << "\nValno #" << VNI->id
3882 << " live into " << printMBBReference(*MFI) << '@'
3883 << LiveInts->getMBBStartIdx(&*MFI) << '\n';
3884 }
3885 }
3886 if (&*MFI == EndMBB)
3887 break;
3888 ++MFI;
3889 }
3890}
3891
3892void MachineVerifier::verifyLiveRange(const LiveRange &LR,
3893 VirtRegOrUnit VRegOrUnit,
3894 LaneBitmask LaneMask) {
3895 for (const VNInfo *VNI : LR.valnos)
3896 verifyLiveRangeValue(LR, VNI, VRegOrUnit, LaneMask);
3897
3898 for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
3899 verifyLiveRangeSegment(LR, I, VRegOrUnit, LaneMask);
3900}
3901
3902void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
3903 Register Reg = LI.reg();
3904 assert(Reg.isVirtual());
3905 verifyLiveRange(LI, VirtRegOrUnit(Reg));
3906
3907 if (LI.hasSubRanges()) {
3909 LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
3910 for (const LiveInterval::SubRange &SR : LI.subranges()) {
3911 if ((Mask & SR.LaneMask).any()) {
3912 report("Lane masks of sub ranges overlap in live interval", MF);
3913 report_context(LI);
3914 }
3915 if ((SR.LaneMask & ~MaxMask).any()) {
3916 report("Subrange lanemask is invalid", MF);
3917 report_context(LI);
3918 }
3919 if (SR.empty()) {
3920 report("Subrange must not be empty", MF);
3921 report_context(SR, VirtRegOrUnit(LI.reg()), SR.LaneMask);
3922 }
3923 Mask |= SR.LaneMask;
3924 verifyLiveRange(SR, VirtRegOrUnit(LI.reg()), SR.LaneMask);
3925 if (!LI.covers(SR)) {
3926 report("A Subrange is not covered by the main range", MF);
3927 report_context(LI);
3928 }
3929 }
3930 }
3931
3932 // Check the LI only has one connected component.
3933 ConnectedVNInfoEqClasses ConEQ(*LiveInts);
3934 unsigned NumComp = ConEQ.Classify(LI);
3935 if (NumComp > 1) {
3936 report("Multiple connected components in live interval", MF);
3937 report_context(LI);
3938 for (unsigned comp = 0; comp != NumComp; ++comp) {
3939 OS << comp << ": valnos";
3940 for (const VNInfo *I : LI.valnos)
3941 if (comp == ConEQ.getEqClass(I))
3942 OS << ' ' << I->id;
3943 OS << '\n';
3944 }
3945 }
3946}
3947
3948namespace {
3949
3950 // FrameSetup and FrameDestroy can have zero adjustment, so using a single
3951 // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
3952 // value is zero.
3953 // We use a bool plus an integer to capture the stack state.
3954struct StackStateOfBB {
3955 StackStateOfBB() = default;
3956 StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup)
3957 : EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
3958 ExitIsSetup(ExitSetup) {}
3959
3960 // Can be negative, which means we are setting up a frame.
3961 int EntryValue = 0;
3962 int ExitValue = 0;
3963 bool EntryIsSetup = false;
3964 bool ExitIsSetup = false;
3965};
3966
3967} // end anonymous namespace
3968
3969/// Make sure on every path through the CFG, a FrameSetup <n> is always followed
3970/// by a FrameDestroy <n>, stack adjustments are identical on all
3971/// CFG edges to a merge point, and frame is destroyed at end of a return block.
3972void MachineVerifier::verifyStackFrame() {
3973 unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
3974 unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
3975 if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
3976 return;
3977
3979 SPState.resize(MF->getNumBlockIDs());
3981
3982 // Visit the MBBs in DFS order.
3983 for (df_ext_iterator<const MachineFunction *,
3985 DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
3986 DFI != DFE; ++DFI) {
3987 const MachineBasicBlock *MBB = *DFI;
3988
3989 StackStateOfBB BBState;
3990 // Check the exit state of the DFS stack predecessor.
3991 if (DFI.getPathLength() >= 2) {
3992 const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
3993 assert(Reachable.count(StackPred) &&
3994 "DFS stack predecessor is already visited.\n");
3995 BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
3996 BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
3997 BBState.ExitValue = BBState.EntryValue;
3998 BBState.ExitIsSetup = BBState.EntryIsSetup;
3999 }
4000
4001 if ((int)MBB->getCallFrameSize() != -BBState.EntryValue) {
4002 report("Call frame size on entry does not match value computed from "
4003 "predecessor",
4004 MBB);
4005 OS << "Call frame size on entry " << MBB->getCallFrameSize()
4006 << " does not match value computed from predecessor "
4007 << -BBState.EntryValue << '\n';
4008 }
4009
4010 // Update stack state by checking contents of MBB.
4011 for (const auto &I : *MBB) {
4012 if (I.getOpcode() == FrameSetupOpcode) {
4013 if (BBState.ExitIsSetup)
4014 report("FrameSetup is after another FrameSetup", &I);
4015 if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack())
4016 report("AdjustsStack not set in presence of a frame pseudo "
4017 "instruction.", &I);
4018 BBState.ExitValue -= TII->getFrameTotalSize(I);
4019 BBState.ExitIsSetup = true;
4020 }
4021
4022 if (I.getOpcode() == FrameDestroyOpcode) {
4023 int Size = TII->getFrameTotalSize(I);
4024 if (!BBState.ExitIsSetup)
4025 report("FrameDestroy is not after a FrameSetup", &I);
4026 int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
4027 BBState.ExitValue;
4028 if (BBState.ExitIsSetup && AbsSPAdj != Size) {
4029 report("FrameDestroy <n> is after FrameSetup <m>", &I);
4030 OS << "FrameDestroy <" << Size << "> is after FrameSetup <"
4031 << AbsSPAdj << ">.\n";
4032 }
4033 if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack())
4034 report("AdjustsStack not set in presence of a frame pseudo "
4035 "instruction.", &I);
4036 BBState.ExitValue += Size;
4037 BBState.ExitIsSetup = false;
4038 }
4039 }
4040 SPState[MBB->getNumber()] = BBState;
4041
4042 // Make sure the exit state of any predecessor is consistent with the entry
4043 // state.
4044 for (const MachineBasicBlock *Pred : MBB->predecessors()) {
4045 if (Reachable.count(Pred) &&
4046 (SPState[Pred->getNumber()].ExitValue != BBState.EntryValue ||
4047 SPState[Pred->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
4048 report("The exit stack state of a predecessor is inconsistent.", MBB);
4049 OS << "Predecessor " << printMBBReference(*Pred) << " has exit state ("
4050 << SPState[Pred->getNumber()].ExitValue << ", "
4051 << SPState[Pred->getNumber()].ExitIsSetup << "), while "
4052 << printMBBReference(*MBB) << " has entry state ("
4053 << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
4054 }
4055 }
4056
4057 // Make sure the entry state of any successor is consistent with the exit
4058 // state.
4059 for (const MachineBasicBlock *Succ : MBB->successors()) {
4060 if (Reachable.count(Succ) &&
4061 (SPState[Succ->getNumber()].EntryValue != BBState.ExitValue ||
4062 SPState[Succ->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
4063 report("The entry stack state of a successor is inconsistent.", MBB);
4064 OS << "Successor " << printMBBReference(*Succ) << " has entry state ("
4065 << SPState[Succ->getNumber()].EntryValue << ", "
4066 << SPState[Succ->getNumber()].EntryIsSetup << "), while "
4067 << printMBBReference(*MBB) << " has exit state ("
4068 << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
4069 }
4070 }
4071
4072 // Make sure a basic block with return ends with zero stack adjustment.
4073 if (!MBB->empty() && MBB->back().isReturn()) {
4074 if (BBState.ExitIsSetup)
4075 report("A return block ends with a FrameSetup.", MBB);
4076 if (BBState.ExitValue)
4077 report("A return block ends with a nonzero stack adjustment.", MBB);
4078 }
4079 }
4080}
4081
4082void MachineVerifier::verifyStackProtector() {
4083 const MachineFrameInfo &MFI = MF->getFrameInfo();
4084 if (!MFI.hasStackProtectorIndex())
4085 return;
4086 // Only applicable when the offsets of frame objects have been determined,
4087 // which is indicated by a non-zero stack size.
4088 if (!MFI.getStackSize())
4089 return;
4090 const TargetFrameLowering &TFI = *MF->getSubtarget().getFrameLowering();
4091 bool StackGrowsDown =
4093 unsigned FI = MFI.getStackProtectorIndex();
4094 int64_t SPStart = MFI.getObjectOffset(FI);
4095 int64_t SPEnd = SPStart + MFI.getObjectSize(FI);
4096 for (unsigned I = 0, E = MFI.getObjectIndexEnd(); I != E; ++I) {
4097 if (I == FI)
4098 continue;
4099 if (MFI.isDeadObjectIndex(I))
4100 continue;
4101 // FIXME: Skip non-default stack objects, as some targets may place them
4102 // above the stack protector. This is a workaround for the fact that
4103 // backends such as AArch64 may place SVE stack objects *above* the stack
4104 // protector.
4106 continue;
4107 // Skip variable-sized objects because they do not have a fixed offset.
4109 continue;
4110 // FIXME: Skip spill slots which may be allocated above the stack protector.
4111 // Ideally this would only skip callee-saved registers, but we don't have
4112 // that information here. For example, spill-slots used for scavenging are
4113 // not described in CalleeSavedInfo.
4114 if (MFI.isSpillSlotObjectIndex(I))
4115 continue;
4116 int64_t ObjStart = MFI.getObjectOffset(I);
4117 int64_t ObjEnd = ObjStart + MFI.getObjectSize(I);
4118 if (SPStart < ObjEnd && ObjStart < SPEnd) {
4119 report("Stack protector overlaps with another stack object", MF);
4120 break;
4121 }
4122 if ((StackGrowsDown && SPStart <= ObjStart) ||
4123 (!StackGrowsDown && SPStart >= ObjStart)) {
4124 report("Stack protector is not the top-most object on the stack", MF);
4125 break;
4126 }
4127 }
4128}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static bool isLoad(int Opcode)
static bool isStore(int Opcode)
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
MachineBasicBlock MachineBasicBlock::iterator MBBI
This file implements the BitVector class.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
const HexagonInstrInfo * TII
hexagon widen Hexagon Store false hexagon widen loads
hexagon widen stores
IRTranslator LLVM IR MI
std::pair< Instruction::BinaryOps, Value * > OffsetOp
Find all possible pairs (BinOp, RHS) that BinOp V, RHS can be simplified.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
A common definition of LaneBitmask for use in TableGen and CodeGen.
Implement a low-level type suitable for MachineInstr level instruction selection.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
print mir2vec MIR2Vec Vocabulary Printer Pass
Definition MIR2Vec.cpp:593
This file declares the MIR specialization of the GenericConvergenceVerifier template.
Register Reg
Register const TargetRegisterInfo * TRI
static void verifyConvergenceControl(const MachineFunction &MF, MachineDominatorTree &DT, std::function< void(const Twine &Message)> FailureCB, raw_ostream &OS)
Promote Memory to Register
Definition Mem2Reg.cpp:110
modulo schedule Modulo Schedule test pass
#define P(N)
ppc ctr loops verify
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg)
SI Optimize VGPR LiveRange
std::unordered_set< BasicBlock * > BlockSet
This file contains some templates that are useful if you are working with the STL at all.
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file describes how to lower LLVM code to machine code.
static unsigned getSize(unsigned Kind)
static LLVM_ABI unsigned getSizeInBits(const fltSemantics &Sem)
Returns the size of the floating point number (in bits) in the given semantics.
Definition APFloat.cpp:354
const fltSemantics & getSemantics() const
Definition APFloat.h:1439
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:361
LLVM Basic Block Representation.
Definition BasicBlock.h:62
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:690
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
void clear()
clear - Removes all bits from the bitvector.
Definition BitVector.h:354
iterator_range< const_set_bits_iterator > set_bits() const
Definition BitVector.h:159
ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a LiveInterval into equivalence cl...
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
const APFloat & getValueAPF() const
Definition Constants.h:320
This is the shared class of boolean and integer constants.
Definition Constants.h:87
IntegerType * getIntegerType() const
Variant of the getType() method to always return an IntegerType, which reduces the amount of casting ...
Definition Constants.h:193
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
Definition Constants.h:157
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
Implements a dense probed hash-table based set.
Definition DenseSet.h:279
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
Register getReg() const
Base class for user error types.
Definition Error.h:354
A specialized PseudoSourceValue for holding FixedStack values, which must include a frame index.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
const Function & getFunction() const
Definition Function.h:164
void initialize(raw_ostream *OS, function_ref< void(const Twine &Message)> FailureCB, const FunctionT &F)
bool isPredicated(const MachineInstr &MI) const override
Returns true if the instruction is already predicated.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e....
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
constexpr bool isScalableVector() const
Returns true if the LLT is a scalable vector.
constexpr unsigned getScalarSizeInBits() const
constexpr bool isScalar() const
constexpr bool isPointerVector() const
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
constexpr bool isValid() const
constexpr uint16_t getNumElements() const
Returns the number of elements in a vector LLT.
constexpr bool isVector() const
constexpr bool isScalable() const
Returns true if the LLT is a scalable vector.
constexpr TypeSize getSizeInBits() const
Returns the total size of the type. Must only be called on sized types.
constexpr bool isPointer() const
constexpr LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
constexpr ElementCount getElementCount() const
constexpr unsigned getAddressSpace() const
constexpr bool isPointerOrPointerVector() const
constexpr LLT getScalarType() const
constexpr TypeSize getSizeInBytes() const
Returns the total size of the type in bytes, i.e.
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
Register reg() const
bool hasSubRanges() const
Returns true if subregister liveness information is available.
iterator_range< subrange_iterator > subranges()
LLVM_ABI void computeSubRangeUndefs(SmallVectorImpl< SlotIndex > &Undefs, LaneBitmask LaneMask, const MachineRegisterInfo &MRI, const SlotIndexes &Indexes) const
For a given lane mask LaneMask, compute indexes at which the lane is marked undefined by subregister ...
void print(raw_ostream &O, const Module *=nullptr) const override
Implement the dump method.
Result of a LiveRange query.
bool isDeadDef() const
Return true if this instruction has a dead def.
VNInfo * valueIn() const
Return the value that is live-in to the instruction.
VNInfo * valueOut() const
Return the value leaving the instruction, if any.
bool isKill() const
Return true if the live-in value is killed by this instruction.
static LLVM_ABI bool isJointlyDominated(const MachineBasicBlock *MBB, ArrayRef< SlotIndex > Defs, const SlotIndexes &Indexes)
A diagnostic function to check if the end of the block MBB is jointly dominated by the blocks corresp...
This class represents the liveness of a register, stack slot, etc.
VNInfo * getValNumInfo(unsigned ValNo)
getValNumInfo - Returns pointer to the specified val#.
Segments::const_iterator const_iterator
bool liveAt(SlotIndex index) const
LLVM_ABI bool covers(const LiveRange &Other) const
Returns true if all segments of the Other live range are completely covered by this live range.
bool empty() const
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarily including Idx,...
bool verify() const
Walk the range and assert if any invariants fail to hold.
unsigned getNumValNums() const
iterator begin()
VNInfoList valnos
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
LLVM_ABI VarInfo & getVarInfo(Register Reg)
getVarInfo - Return the VarInfo structure for the specified VIRTUAL register.
TypeSize getValue() const
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
ExceptionHandling getExceptionHandlingType() const
Definition MCAsmInfo.h:633
Describe properties that are true of each instruction in the target description file.
This holds information about one operand of a machine instruction, indicating the register class for ...
Definition MCInstrDesc.h:87
MCRegAliasIterator enumerates all registers aliasing Reg.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:33
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
bool isValid() const
isValid - Returns true until all the operands have been visited.
bool isInlineAsmBrIndirectTarget() const
Returns true if this is the indirect dest of an INLINEASM_BR.
bool isEHPad() const
Returns true if the block is a landing pad.
iterator_range< livein_iterator > liveins() const
iterator_range< iterator > phis()
Returns a range that iterates over the phis in the basic block.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
bool isIRBlockAddressTaken() const
Test whether this block is the target of an IR BlockAddress.
BasicBlock * getAddressTakenIRBlock() const
Retrieves the BasicBlock which corresponds to this MachineBasicBlock.
LLVM_ABI bool isPredecessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a predecessor of this block.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
unsigned getCallFrameSize() const
Return the call frame size on entry to this basic block.
iterator_range< succ_iterator > successors()
LLVM_ABI bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
iterator_range< pred_iterator > predecessors()
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted.
uint64_t getStackSize() const
Return the number of bytes that must be allocated to hold all of the fixed size frame objects.
int getStackProtectorIndex() const
Return the index for the stack protector object.
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
LLVM_ABI BitVector getPristineRegs(const MachineFunction &MF) const
Return a set of physical registers that are pristine.
bool isVariableSizedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a variable sized object.
int getObjectIndexEnd() const
Return one past the maximum frame object index.
bool hasStackProtectorIndex() const
uint8_t getStackID(int ObjectIdx) const
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
bool isDeadObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a dead object.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool verify(Pass *p=nullptr, const char *Banner=nullptr, raw_ostream *OS=nullptr, bool AbortOnError=true) const
Run the current MachineFunction through the machine code verifier, useful for debugger use.
const MachineFunctionProperties & getProperties() const
Get the function properties.
const MachineBasicBlock & front() const
void print(raw_ostream &OS, const SlotIndexes *=nullptr) const
print - Print out the MachineFunction in a format suitable for debugging to the specified stream.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
BasicBlockListType::const_iterator const_iterator
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool isReturn(QueryType Type=AnyInBundle) const
bool isTerminator(QueryType Type=AnyInBundle) const
Returns true if this instruction part of the terminator for a basic block.
bool isBarrier(QueryType Type=AnyInBundle) const
Returns true if the specified instruction stops control flow from executing the instruction immediate...
A description of a memory reference used in the backend.
LocationSize getSize() const
Return the size in bytes of the memory reference.
const PseudoSourceValue * getPseudoValue() const
LLT getMemoryType() const
Return the memory type of the memory reference.
const MDNode * getRanges() const
Return the range tag for the memory reference.
AtomicOrdering getSuccessOrdering() const
Return the atomic ordering requirements for this memory operation.
LocationSize getSizeInBits() const
Return the size in bits of the memory reference.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
int64_t getImm() const
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isIntrinsicID() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
MachineBasicBlock * getMBB() const
ArrayRef< int > getShuffleMask() const
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isValidExcessOperand() const
Return true if this operand can validly be appended to an arbitrary operand list.
bool isShuffleMask() const
LLVM_ABI void print(raw_ostream &os, const TargetRegisterInfo *TRI=nullptr) const
Print the MachineOperand to os.
unsigned getCFIIndex() const
LLVM_ABI bool isRenamable() const
isRenamable - Returns true if this register may be renamed, i.e.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
bool isInternalRead() const
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
const uint32_t * getRegMask() const
getRegMask - Returns a bit mask of registers preserved by this RegMask operand.
@ MO_CFIIndex
MCCFIInstruction index.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_FrameIndex
Abstract Stack Frame Index.
@ MO_Register
Register operand.
bool isMBB() const
isMBB - Tests if this is a MO_MachineBasicBlock operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:140
AnalysisType * getAnalysisIfAvailable() const
getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to get analysis information tha...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Special value supplied for machine level alias analysis.
Holds all the information related to register banks.
const RegisterBank & getRegBank(unsigned ID)
Get the register bank identified by ID.
unsigned getMaximumSize(unsigned RegBankID) const
Get the maximum size in bits that fits in the given register bank.
This class implements the register bank concept.
const char * getName() const
Get a user friendly name of this register bank.
unsigned getID() const
Get the identifier of this register bank.
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Definition Register.h:87
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr unsigned id() const
Definition Register.h:100
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
bool isBlock() const
isBlock - Returns true if this is a block boundary slot.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
bool isEarlyClobber() const
isEarlyClobber - Returns true if this is an early-clobber slot.
bool isRegister() const
isRegister - Returns true if this is a normal register use/def slot.
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def.
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
SlotIndexes pass.
MBBIndexIterator MBBIndexBegin() const
Returns an iterator for the begin of the idx2MBBMap.
MBBIndexIterator MBBIndexEnd() const
Return an iterator for the end of the idx2MBBMap.
SmallVectorImpl< IdxMBBPair >::const_iterator MBBIndexIterator
Iterator over the idx2MBBMap (sorted pairs of slot index of basic block begin and basic block)
size_type size() const
Definition SmallPtrSet.h:99
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator begin() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Register getReg() const
MI-level Statepoint operands.
Definition StackMaps.h:159
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Information about stack frame layout on the target.
StackDirection getStackGrowthDirection() const
getStackGrowthDirection - Return the direction the stack grows
bool hasSuperClassEq(const TargetRegisterClass *RC) const
Returns true if RC is a super-class of or equal to this class.
virtual const RegisterBankInfo * getRegBankInfo() const
If the information for the register banks is available, return it.
virtual const TargetInstrInfo * getInstrInfo() const
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
static constexpr TypeSize getZero()
Definition TypeSize.h:350
VNInfo - Value Number Information.
bool isUnused() const
Returns true if this value is unused.
unsigned id
The ID number of this value.
SlotIndex def
The index of the defining instruction.
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
LLVM Value Representation.
Definition Value.h:75
Wrapper class representing a virtual register or register unit.
Definition Register.h:181
constexpr bool isVirtualReg() const
Definition Register.h:192
constexpr MCRegUnit asMCRegUnit() const
Definition Register.h:196
constexpr Register asVirtualReg() const
Definition Register.h:201
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
constexpr bool isNonZero() const
Definition TypeSize.h:156
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:217
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
static constexpr bool isKnownGT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:224
static constexpr bool isKnownGE(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:238
self_iterator getIterator()
Definition ilist_node.h:123
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
LLVM_ABI AttributeSet getFnAttributes(LLVMContext &C, ID id)
Return the function attributes for an intrinsic.
@ OPERAND_IMMEDIATE
Definition MCInstrDesc.h:62
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< DefNode * > Def
Definition RDFGraph.h:384
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
iterator end() const
Definition BasicBlock.h:89
LLVM_ABI iterator begin() const
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:477
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1725
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:165
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ SjLj
setjmp/longjmp based exceptions
Definition CodeGen.h:56
bool isPreISelGenericOpcode(unsigned Opcode)
Check whether the given Opcode is a generic opcode that is not supposed to appear after ISel.
LLVM_ABI Printable printRegUnit(unsigned Unit, const TargetRegisterInfo *TRI)
Create Printable object to print register units on a raw_ostream.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2136
void set_subtract(S1Ty &S1, const S2Ty &S2)
set_subtract(A, B) - Compute A := A - B
Printable PrintLaneMask(LaneBitmask LaneMask)
Create Printable object to print LaneBitmasks on a raw_ostream.
Definition LaneBitmask.h:92
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool isPreISelGenericOptimizationHint(unsigned Opcode)
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
LLVM_ABI FunctionPass * createMachineVerifierPass(const std::string &Banner)
createMachineVerifierPass - This pass verifies cenerated machine code instructions for correctness.
LLVM_ABI void verifyMachineFunction(const std::string &Banner, const MachineFunction &MF)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:406
LLVM_ABI void initializeMachineVerifierLegacyPassPass(PassRegistry &)
detail::ValueMatchesPoly< M > HasValue(M Matcher)
Definition Error.h:221
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
GenericConvergenceVerifier< MachineSSAContext > MachineConvergenceVerifier
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
LLVM_ABI raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
unsigned MCRegUnit
Register units are used to compute register aliasing.
Definition MCRegister.h:30
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Sub
Subtraction of integers.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1867
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1897
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:867
static constexpr LaneBitmask getAll()
Definition LaneBitmask.h:82
constexpr bool none() const
Definition LaneBitmask.h:52
constexpr bool any() const
Definition LaneBitmask.h:53
static constexpr LaneBitmask getNone()
Definition LaneBitmask.h:81
This represents a simple continuous liveness interval for a value.
VarInfo - This represents the regions where a virtual register is live in the program.
Pair of physical register and lane mask.