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