10b57cec5SDimitry Andric //===- MachineVerifier.cpp - Machine Code Verifier ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Pass to verify generated machine code. The following is checked:
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Operand counts: All explicit operands must be present.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Register classes: All physical and virtual register operands must be
140b57cec5SDimitry Andric // compatible with the register class required by the instruction descriptor.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // Register live intervals: Registers must be defined only once, and must be
170b57cec5SDimitry Andric // defined before use.
180b57cec5SDimitry Andric //
195ffd83dbSDimitry Andric // The machine code verifier is enabled with the command-line option
205ffd83dbSDimitry Andric // -verify-machineinstrs.
210b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
250b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
260b57cec5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h"
275ffd83dbSDimitry Andric #include "llvm/ADT/PostOrderIterator.h"
280b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
290b57cec5SDimitry Andric #include "llvm/ADT/SetOperations.h"
300b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
310b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
320b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
330b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
340b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h"
350b57cec5SDimitry Andric #include "llvm/CodeGen/GlobalISel/RegisterBank.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h"
375ffd83dbSDimitry Andric #include "llvm/CodeGen/LiveIntervalCalc.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/LiveStacks.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
430b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
440b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
450b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
460b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h"
470b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
480b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
490b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
500b57cec5SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h"
510b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
520b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h"
530b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h"
540b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h"
550b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
560b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
570b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
580b57cec5SDimitry Andric #include "llvm/IR/Function.h"
590b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
600b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
61480093f4SDimitry Andric #include "llvm/InitializePasses.h"
620b57cec5SDimitry Andric #include "llvm/MC/LaneBitmask.h"
630b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
640b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h"
650b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
660b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h"
670b57cec5SDimitry Andric #include "llvm/Pass.h"
680b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
690b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
700b57cec5SDimitry Andric #include "llvm/Support/LowLevelTypeImpl.h"
710b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
720b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
730b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
740b57cec5SDimitry Andric #include <algorithm>
750b57cec5SDimitry Andric #include <cassert>
760b57cec5SDimitry Andric #include <cstddef>
770b57cec5SDimitry Andric #include <cstdint>
780b57cec5SDimitry Andric #include <iterator>
790b57cec5SDimitry Andric #include <string>
800b57cec5SDimitry Andric #include <utility>
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric using namespace llvm;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric namespace {
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   struct MachineVerifier {
MachineVerifier__anon86168b390111::MachineVerifier870b57cec5SDimitry Andric     MachineVerifier(Pass *pass, const char *b) : PASS(pass), Banner(b) {}
880b57cec5SDimitry Andric 
89af732203SDimitry Andric     unsigned verify(const MachineFunction &MF);
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric     Pass *const PASS;
920b57cec5SDimitry Andric     const char *Banner;
930b57cec5SDimitry Andric     const MachineFunction *MF;
940b57cec5SDimitry Andric     const TargetMachine *TM;
950b57cec5SDimitry Andric     const TargetInstrInfo *TII;
960b57cec5SDimitry Andric     const TargetRegisterInfo *TRI;
970b57cec5SDimitry Andric     const MachineRegisterInfo *MRI;
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric     unsigned foundErrors;
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric     // Avoid querying the MachineFunctionProperties for each operand.
1020b57cec5SDimitry Andric     bool isFunctionRegBankSelected;
1030b57cec5SDimitry Andric     bool isFunctionSelected;
1040b57cec5SDimitry Andric 
105af732203SDimitry Andric     using RegVector = SmallVector<Register, 16>;
1060b57cec5SDimitry Andric     using RegMaskVector = SmallVector<const uint32_t *, 4>;
107af732203SDimitry Andric     using RegSet = DenseSet<Register>;
108af732203SDimitry Andric     using RegMap = DenseMap<Register, const MachineInstr *>;
1090b57cec5SDimitry Andric     using BlockSet = SmallPtrSet<const MachineBasicBlock *, 8>;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric     const MachineInstr *FirstNonPHI;
1120b57cec5SDimitry Andric     const MachineInstr *FirstTerminator;
1130b57cec5SDimitry Andric     BlockSet FunctionBlocks;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric     BitVector regsReserved;
1160b57cec5SDimitry Andric     RegSet regsLive;
1170b57cec5SDimitry Andric     RegVector regsDefined, regsDead, regsKilled;
1180b57cec5SDimitry Andric     RegMaskVector regMasks;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     SlotIndex lastIndex;
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric     // Add Reg and any sub-registers to RV
addRegWithSubRegs__anon86168b390111::MachineVerifier123af732203SDimitry Andric     void addRegWithSubRegs(RegVector &RV, Register Reg) {
1240b57cec5SDimitry Andric       RV.push_back(Reg);
125af732203SDimitry Andric       if (Reg.isPhysical())
126af732203SDimitry Andric         append_range(RV, TRI->subregs(Reg.asMCReg()));
1270b57cec5SDimitry Andric     }
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric     struct BBInfo {
1300b57cec5SDimitry Andric       // Is this MBB reachable from the MF entry point?
1310b57cec5SDimitry Andric       bool reachable = false;
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric       // Vregs that must be live in because they are used without being
134af732203SDimitry Andric       // defined. Map value is the user. vregsLiveIn doesn't include regs
135af732203SDimitry Andric       // that only are used by PHI nodes.
1360b57cec5SDimitry Andric       RegMap vregsLiveIn;
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric       // Regs killed in MBB. They may be defined again, and will then be in both
1390b57cec5SDimitry Andric       // regsKilled and regsLiveOut.
1400b57cec5SDimitry Andric       RegSet regsKilled;
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric       // Regs defined in MBB and live out. Note that vregs passing through may
1430b57cec5SDimitry Andric       // be live out without being mentioned here.
1440b57cec5SDimitry Andric       RegSet regsLiveOut;
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric       // Vregs that pass through MBB untouched. This set is disjoint from
1470b57cec5SDimitry Andric       // regsKilled and regsLiveOut.
1480b57cec5SDimitry Andric       RegSet vregsPassed;
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric       // Vregs that must pass through MBB because they are needed by a successor
1510b57cec5SDimitry Andric       // block. This set is disjoint from regsLiveOut.
1520b57cec5SDimitry Andric       RegSet vregsRequired;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric       // Set versions of block's predecessor and successor lists.
1550b57cec5SDimitry Andric       BlockSet Preds, Succs;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric       BBInfo() = default;
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric       // Add register to vregsRequired if it belongs there. Return true if
1600b57cec5SDimitry Andric       // anything changed.
addRequired__anon86168b390111::MachineVerifier::BBInfo161af732203SDimitry Andric       bool addRequired(Register Reg) {
162af732203SDimitry Andric         if (!Reg.isVirtual())
1630b57cec5SDimitry Andric           return false;
1640b57cec5SDimitry Andric         if (regsLiveOut.count(Reg))
1650b57cec5SDimitry Andric           return false;
1660b57cec5SDimitry Andric         return vregsRequired.insert(Reg).second;
1670b57cec5SDimitry Andric       }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric       // Same for a full set.
addRequired__anon86168b390111::MachineVerifier::BBInfo1700b57cec5SDimitry Andric       bool addRequired(const RegSet &RS) {
1715ffd83dbSDimitry Andric         bool Changed = false;
172af732203SDimitry Andric         for (Register Reg : RS)
1735ffd83dbSDimitry Andric           Changed |= addRequired(Reg);
1745ffd83dbSDimitry Andric         return Changed;
1750b57cec5SDimitry Andric       }
1760b57cec5SDimitry Andric 
1770b57cec5SDimitry Andric       // Same for a full map.
addRequired__anon86168b390111::MachineVerifier::BBInfo1780b57cec5SDimitry Andric       bool addRequired(const RegMap &RM) {
1795ffd83dbSDimitry Andric         bool Changed = false;
1805ffd83dbSDimitry Andric         for (const auto &I : RM)
1815ffd83dbSDimitry Andric           Changed |= addRequired(I.first);
1825ffd83dbSDimitry Andric         return Changed;
1830b57cec5SDimitry Andric       }
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric       // Live-out registers are either in regsLiveOut or vregsPassed.
isLiveOut__anon86168b390111::MachineVerifier::BBInfo186af732203SDimitry Andric       bool isLiveOut(Register Reg) const {
1870b57cec5SDimitry Andric         return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
1880b57cec5SDimitry Andric       }
1890b57cec5SDimitry Andric     };
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric     // Extra register info per MBB.
1920b57cec5SDimitry Andric     DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
1930b57cec5SDimitry Andric 
isReserved__anon86168b390111::MachineVerifier194af732203SDimitry Andric     bool isReserved(Register Reg) {
195af732203SDimitry Andric       return Reg.id() < regsReserved.size() && regsReserved.test(Reg.id());
1960b57cec5SDimitry Andric     }
1970b57cec5SDimitry Andric 
isAllocatable__anon86168b390111::MachineVerifier198af732203SDimitry Andric     bool isAllocatable(Register Reg) const {
199af732203SDimitry Andric       return Reg.id() < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) &&
200af732203SDimitry Andric              !regsReserved.test(Reg.id());
2010b57cec5SDimitry Andric     }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric     // Analysis information if available
2040b57cec5SDimitry Andric     LiveVariables *LiveVars;
2050b57cec5SDimitry Andric     LiveIntervals *LiveInts;
2060b57cec5SDimitry Andric     LiveStacks *LiveStks;
2070b57cec5SDimitry Andric     SlotIndexes *Indexes;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric     void visitMachineFunctionBefore();
2100b57cec5SDimitry Andric     void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
2110b57cec5SDimitry Andric     void visitMachineBundleBefore(const MachineInstr *MI);
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric     bool verifyVectorElementMatch(LLT Ty0, LLT Ty1, const MachineInstr *MI);
2140b57cec5SDimitry Andric     void verifyPreISelGenericInstruction(const MachineInstr *MI);
2150b57cec5SDimitry Andric     void visitMachineInstrBefore(const MachineInstr *MI);
2160b57cec5SDimitry Andric     void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
2170b57cec5SDimitry Andric     void visitMachineBundleAfter(const MachineInstr *MI);
2180b57cec5SDimitry Andric     void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
2190b57cec5SDimitry Andric     void visitMachineFunctionAfter();
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric     void report(const char *msg, const MachineFunction *MF);
2220b57cec5SDimitry Andric     void report(const char *msg, const MachineBasicBlock *MBB);
2230b57cec5SDimitry Andric     void report(const char *msg, const MachineInstr *MI);
2240b57cec5SDimitry Andric     void report(const char *msg, const MachineOperand *MO, unsigned MONum,
2250b57cec5SDimitry Andric                 LLT MOVRegType = LLT{});
226*5f7ddb14SDimitry Andric     void report(const Twine &Msg, const MachineInstr *MI);
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     void report_context(const LiveInterval &LI) const;
229af732203SDimitry Andric     void report_context(const LiveRange &LR, Register VRegUnit,
2300b57cec5SDimitry Andric                         LaneBitmask LaneMask) const;
2310b57cec5SDimitry Andric     void report_context(const LiveRange::Segment &S) const;
2320b57cec5SDimitry Andric     void report_context(const VNInfo &VNI) const;
2330b57cec5SDimitry Andric     void report_context(SlotIndex Pos) const;
2340b57cec5SDimitry Andric     void report_context(MCPhysReg PhysReg) const;
2350b57cec5SDimitry Andric     void report_context_liverange(const LiveRange &LR) const;
2360b57cec5SDimitry Andric     void report_context_lanemask(LaneBitmask LaneMask) const;
237af732203SDimitry Andric     void report_context_vreg(Register VReg) const;
238af732203SDimitry Andric     void report_context_vreg_regunit(Register VRegOrUnit) const;
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric     void verifyInlineAsm(const MachineInstr *MI);
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric     void checkLiveness(const MachineOperand *MO, unsigned MONum);
2430b57cec5SDimitry Andric     void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
244af732203SDimitry Andric                             SlotIndex UseIdx, const LiveRange &LR,
245af732203SDimitry Andric                             Register VRegOrUnit,
2460b57cec5SDimitry Andric                             LaneBitmask LaneMask = LaneBitmask::getNone());
2470b57cec5SDimitry Andric     void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
248af732203SDimitry Andric                             SlotIndex DefIdx, const LiveRange &LR,
249af732203SDimitry Andric                             Register VRegOrUnit, bool SubRangeCheck = false,
2500b57cec5SDimitry Andric                             LaneBitmask LaneMask = LaneBitmask::getNone());
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric     void markReachable(const MachineBasicBlock *MBB);
2530b57cec5SDimitry Andric     void calcRegsPassed();
2540b57cec5SDimitry Andric     void checkPHIOps(const MachineBasicBlock &MBB);
2550b57cec5SDimitry Andric 
2560b57cec5SDimitry Andric     void calcRegsRequired();
2570b57cec5SDimitry Andric     void verifyLiveVariables();
2580b57cec5SDimitry Andric     void verifyLiveIntervals();
2590b57cec5SDimitry Andric     void verifyLiveInterval(const LiveInterval&);
260af732203SDimitry Andric     void verifyLiveRangeValue(const LiveRange &, const VNInfo *, Register,
2610b57cec5SDimitry Andric                               LaneBitmask);
2620b57cec5SDimitry Andric     void verifyLiveRangeSegment(const LiveRange &,
263af732203SDimitry Andric                                 const LiveRange::const_iterator I, Register,
2640b57cec5SDimitry Andric                                 LaneBitmask);
265af732203SDimitry Andric     void verifyLiveRange(const LiveRange &, Register,
2660b57cec5SDimitry Andric                          LaneBitmask LaneMask = LaneBitmask::getNone());
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric     void verifyStackFrame();
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric     void verifySlotIndexes() const;
2710b57cec5SDimitry Andric     void verifyProperties(const MachineFunction &MF);
2720b57cec5SDimitry Andric   };
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   struct MachineVerifierPass : public MachineFunctionPass {
2750b57cec5SDimitry Andric     static char ID; // Pass ID, replacement for typeid
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric     const std::string Banner;
2780b57cec5SDimitry Andric 
MachineVerifierPass__anon86168b390111::MachineVerifierPass2790b57cec5SDimitry Andric     MachineVerifierPass(std::string banner = std::string())
2800b57cec5SDimitry Andric       : MachineFunctionPass(ID), Banner(std::move(banner)) {
2810b57cec5SDimitry Andric         initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
2820b57cec5SDimitry Andric       }
2830b57cec5SDimitry Andric 
getAnalysisUsage__anon86168b390111::MachineVerifierPass2840b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
2850b57cec5SDimitry Andric       AU.setPreservesAll();
2860b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
2870b57cec5SDimitry Andric     }
2880b57cec5SDimitry Andric 
runOnMachineFunction__anon86168b390111::MachineVerifierPass2890b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override {
2900b57cec5SDimitry Andric       unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
2910b57cec5SDimitry Andric       if (FoundErrors)
2920b57cec5SDimitry Andric         report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
2930b57cec5SDimitry Andric       return false;
2940b57cec5SDimitry Andric     }
2950b57cec5SDimitry Andric   };
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric } // end anonymous namespace
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric char MachineVerifierPass::ID = 0;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
3020b57cec5SDimitry Andric                 "Verify generated machine code", false, false)
3030b57cec5SDimitry Andric 
createMachineVerifierPass(const std::string & Banner)3040b57cec5SDimitry Andric FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
3050b57cec5SDimitry Andric   return new MachineVerifierPass(Banner);
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric 
verifyMachineFunction(MachineFunctionAnalysisManager *,const std::string & Banner,const MachineFunction & MF)308af732203SDimitry Andric void llvm::verifyMachineFunction(MachineFunctionAnalysisManager *,
309af732203SDimitry Andric                                  const std::string &Banner,
310af732203SDimitry Andric                                  const MachineFunction &MF) {
311af732203SDimitry Andric   // TODO: Use MFAM after porting below analyses.
312af732203SDimitry Andric   // LiveVariables *LiveVars;
313af732203SDimitry Andric   // LiveIntervals *LiveInts;
314af732203SDimitry Andric   // LiveStacks *LiveStks;
315af732203SDimitry Andric   // SlotIndexes *Indexes;
316af732203SDimitry Andric   unsigned FoundErrors = MachineVerifier(nullptr, Banner.c_str()).verify(MF);
317af732203SDimitry Andric   if (FoundErrors)
318af732203SDimitry Andric     report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors.");
319af732203SDimitry Andric }
320af732203SDimitry Andric 
verify(Pass * p,const char * Banner,bool AbortOnErrors) const3210b57cec5SDimitry Andric bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
3220b57cec5SDimitry Andric     const {
3230b57cec5SDimitry Andric   MachineFunction &MF = const_cast<MachineFunction&>(*this);
3240b57cec5SDimitry Andric   unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
3250b57cec5SDimitry Andric   if (AbortOnErrors && FoundErrors)
3260b57cec5SDimitry Andric     report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
3270b57cec5SDimitry Andric   return FoundErrors == 0;
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
verifySlotIndexes() const3300b57cec5SDimitry Andric void MachineVerifier::verifySlotIndexes() const {
3310b57cec5SDimitry Andric   if (Indexes == nullptr)
3320b57cec5SDimitry Andric     return;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   // Ensure the IdxMBB list is sorted by slot indexes.
3350b57cec5SDimitry Andric   SlotIndex Last;
3360b57cec5SDimitry Andric   for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
3370b57cec5SDimitry Andric        E = Indexes->MBBIndexEnd(); I != E; ++I) {
3380b57cec5SDimitry Andric     assert(!Last.isValid() || I->first > Last);
3390b57cec5SDimitry Andric     Last = I->first;
3400b57cec5SDimitry Andric   }
3410b57cec5SDimitry Andric }
3420b57cec5SDimitry Andric 
verifyProperties(const MachineFunction & MF)3430b57cec5SDimitry Andric void MachineVerifier::verifyProperties(const MachineFunction &MF) {
3440b57cec5SDimitry Andric   // If a pass has introduced virtual registers without clearing the
3450b57cec5SDimitry Andric   // NoVRegs property (or set it without allocating the vregs)
3460b57cec5SDimitry Andric   // then report an error.
3470b57cec5SDimitry Andric   if (MF.getProperties().hasProperty(
3480b57cec5SDimitry Andric           MachineFunctionProperties::Property::NoVRegs) &&
3490b57cec5SDimitry Andric       MRI->getNumVirtRegs())
3500b57cec5SDimitry Andric     report("Function has NoVRegs property but there are VReg operands", &MF);
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric 
verify(const MachineFunction & MF)353af732203SDimitry Andric unsigned MachineVerifier::verify(const MachineFunction &MF) {
3540b57cec5SDimitry Andric   foundErrors = 0;
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   this->MF = &MF;
3570b57cec5SDimitry Andric   TM = &MF.getTarget();
3580b57cec5SDimitry Andric   TII = MF.getSubtarget().getInstrInfo();
3590b57cec5SDimitry Andric   TRI = MF.getSubtarget().getRegisterInfo();
3600b57cec5SDimitry Andric   MRI = &MF.getRegInfo();
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   const bool isFunctionFailedISel = MF.getProperties().hasProperty(
3630b57cec5SDimitry Andric       MachineFunctionProperties::Property::FailedISel);
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // If we're mid-GlobalISel and we already triggered the fallback path then
3660b57cec5SDimitry Andric   // it's expected that the MIR is somewhat broken but that's ok since we'll
3670b57cec5SDimitry Andric   // reset it and clear the FailedISel attribute in ResetMachineFunctions.
3680b57cec5SDimitry Andric   if (isFunctionFailedISel)
3690b57cec5SDimitry Andric     return foundErrors;
3700b57cec5SDimitry Andric 
3715ffd83dbSDimitry Andric   isFunctionRegBankSelected = MF.getProperties().hasProperty(
3720b57cec5SDimitry Andric       MachineFunctionProperties::Property::RegBankSelected);
3735ffd83dbSDimitry Andric   isFunctionSelected = MF.getProperties().hasProperty(
3740b57cec5SDimitry Andric       MachineFunctionProperties::Property::Selected);
3755ffd83dbSDimitry Andric 
3760b57cec5SDimitry Andric   LiveVars = nullptr;
3770b57cec5SDimitry Andric   LiveInts = nullptr;
3780b57cec5SDimitry Andric   LiveStks = nullptr;
3790b57cec5SDimitry Andric   Indexes = nullptr;
3800b57cec5SDimitry Andric   if (PASS) {
3810b57cec5SDimitry Andric     LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
3820b57cec5SDimitry Andric     // We don't want to verify LiveVariables if LiveIntervals is available.
3830b57cec5SDimitry Andric     if (!LiveInts)
3840b57cec5SDimitry Andric       LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
3850b57cec5SDimitry Andric     LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
3860b57cec5SDimitry Andric     Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
3870b57cec5SDimitry Andric   }
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   verifySlotIndexes();
3900b57cec5SDimitry Andric 
3910b57cec5SDimitry Andric   verifyProperties(MF);
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric   visitMachineFunctionBefore();
3945ffd83dbSDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
3955ffd83dbSDimitry Andric     visitMachineBasicBlockBefore(&MBB);
3960b57cec5SDimitry Andric     // Keep track of the current bundle header.
3970b57cec5SDimitry Andric     const MachineInstr *CurBundle = nullptr;
3980b57cec5SDimitry Andric     // Do we expect the next instruction to be part of the same bundle?
3990b57cec5SDimitry Andric     bool InBundle = false;
4000b57cec5SDimitry Andric 
4015ffd83dbSDimitry Andric     for (const MachineInstr &MI : MBB.instrs()) {
4025ffd83dbSDimitry Andric       if (MI.getParent() != &MBB) {
4035ffd83dbSDimitry Andric         report("Bad instruction parent pointer", &MBB);
4045ffd83dbSDimitry Andric         errs() << "Instruction: " << MI;
4050b57cec5SDimitry Andric         continue;
4060b57cec5SDimitry Andric       }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric       // Check for consistent bundle flags.
4095ffd83dbSDimitry Andric       if (InBundle && !MI.isBundledWithPred())
4100b57cec5SDimitry Andric         report("Missing BundledPred flag, "
4110b57cec5SDimitry Andric                "BundledSucc was set on predecessor",
4125ffd83dbSDimitry Andric                &MI);
4135ffd83dbSDimitry Andric       if (!InBundle && MI.isBundledWithPred())
4140b57cec5SDimitry Andric         report("BundledPred flag is set, "
4150b57cec5SDimitry Andric                "but BundledSucc not set on predecessor",
4165ffd83dbSDimitry Andric                &MI);
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric       // Is this a bundle header?
4195ffd83dbSDimitry Andric       if (!MI.isInsideBundle()) {
4200b57cec5SDimitry Andric         if (CurBundle)
4210b57cec5SDimitry Andric           visitMachineBundleAfter(CurBundle);
4225ffd83dbSDimitry Andric         CurBundle = &MI;
4230b57cec5SDimitry Andric         visitMachineBundleBefore(CurBundle);
4240b57cec5SDimitry Andric       } else if (!CurBundle)
4255ffd83dbSDimitry Andric         report("No bundle header", &MI);
4265ffd83dbSDimitry Andric       visitMachineInstrBefore(&MI);
4275ffd83dbSDimitry Andric       for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
4280b57cec5SDimitry Andric         const MachineOperand &Op = MI.getOperand(I);
4290b57cec5SDimitry Andric         if (Op.getParent() != &MI) {
4300b57cec5SDimitry Andric           // Make sure to use correct addOperand / RemoveOperand / ChangeTo
4310b57cec5SDimitry Andric           // functions when replacing operands of a MachineInstr.
4320b57cec5SDimitry Andric           report("Instruction has operand with wrong parent set", &MI);
4330b57cec5SDimitry Andric         }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric         visitMachineOperand(&Op, I);
4360b57cec5SDimitry Andric       }
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric       // Was this the last bundled instruction?
4395ffd83dbSDimitry Andric       InBundle = MI.isBundledWithSucc();
4400b57cec5SDimitry Andric     }
4410b57cec5SDimitry Andric     if (CurBundle)
4420b57cec5SDimitry Andric       visitMachineBundleAfter(CurBundle);
4430b57cec5SDimitry Andric     if (InBundle)
4445ffd83dbSDimitry Andric       report("BundledSucc flag set on last instruction in block", &MBB.back());
4455ffd83dbSDimitry Andric     visitMachineBasicBlockAfter(&MBB);
4460b57cec5SDimitry Andric   }
4470b57cec5SDimitry Andric   visitMachineFunctionAfter();
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   // Clean up.
4500b57cec5SDimitry Andric   regsLive.clear();
4510b57cec5SDimitry Andric   regsDefined.clear();
4520b57cec5SDimitry Andric   regsDead.clear();
4530b57cec5SDimitry Andric   regsKilled.clear();
4540b57cec5SDimitry Andric   regMasks.clear();
4550b57cec5SDimitry Andric   MBBInfoMap.clear();
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   return foundErrors;
4580b57cec5SDimitry Andric }
4590b57cec5SDimitry Andric 
report(const char * msg,const MachineFunction * MF)4600b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
4610b57cec5SDimitry Andric   assert(MF);
4620b57cec5SDimitry Andric   errs() << '\n';
4630b57cec5SDimitry Andric   if (!foundErrors++) {
4640b57cec5SDimitry Andric     if (Banner)
4650b57cec5SDimitry Andric       errs() << "# " << Banner << '\n';
4660b57cec5SDimitry Andric     if (LiveInts != nullptr)
4670b57cec5SDimitry Andric       LiveInts->print(errs());
4680b57cec5SDimitry Andric     else
4690b57cec5SDimitry Andric       MF->print(errs(), Indexes);
4700b57cec5SDimitry Andric   }
4710b57cec5SDimitry Andric   errs() << "*** Bad machine code: " << msg << " ***\n"
4720b57cec5SDimitry Andric       << "- function:    " << MF->getName() << "\n";
4730b57cec5SDimitry Andric }
4740b57cec5SDimitry Andric 
report(const char * msg,const MachineBasicBlock * MBB)4750b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
4760b57cec5SDimitry Andric   assert(MBB);
4770b57cec5SDimitry Andric   report(msg, MBB->getParent());
4780b57cec5SDimitry Andric   errs() << "- basic block: " << printMBBReference(*MBB) << ' '
4790b57cec5SDimitry Andric          << MBB->getName() << " (" << (const void *)MBB << ')';
4800b57cec5SDimitry Andric   if (Indexes)
4810b57cec5SDimitry Andric     errs() << " [" << Indexes->getMBBStartIdx(MBB)
4820b57cec5SDimitry Andric         << ';' <<  Indexes->getMBBEndIdx(MBB) << ')';
4830b57cec5SDimitry Andric   errs() << '\n';
4840b57cec5SDimitry Andric }
4850b57cec5SDimitry Andric 
report(const char * msg,const MachineInstr * MI)4860b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
4870b57cec5SDimitry Andric   assert(MI);
4880b57cec5SDimitry Andric   report(msg, MI->getParent());
4890b57cec5SDimitry Andric   errs() << "- instruction: ";
4900b57cec5SDimitry Andric   if (Indexes && Indexes->hasIndex(*MI))
4910b57cec5SDimitry Andric     errs() << Indexes->getInstructionIndex(*MI) << '\t';
492af732203SDimitry Andric   MI->print(errs(), /*IsStandalone=*/true);
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric 
report(const char * msg,const MachineOperand * MO,unsigned MONum,LLT MOVRegType)4950b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineOperand *MO,
4960b57cec5SDimitry Andric                              unsigned MONum, LLT MOVRegType) {
4970b57cec5SDimitry Andric   assert(MO);
4980b57cec5SDimitry Andric   report(msg, MO->getParent());
4990b57cec5SDimitry Andric   errs() << "- operand " << MONum << ":   ";
5000b57cec5SDimitry Andric   MO->print(errs(), MOVRegType, TRI);
5010b57cec5SDimitry Andric   errs() << "\n";
5020b57cec5SDimitry Andric }
5030b57cec5SDimitry Andric 
report(const Twine & Msg,const MachineInstr * MI)504*5f7ddb14SDimitry Andric void MachineVerifier::report(const Twine &Msg, const MachineInstr *MI) {
505*5f7ddb14SDimitry Andric   report(Msg.str().c_str(), MI);
506*5f7ddb14SDimitry Andric }
507*5f7ddb14SDimitry Andric 
report_context(SlotIndex Pos) const5080b57cec5SDimitry Andric void MachineVerifier::report_context(SlotIndex Pos) const {
5090b57cec5SDimitry Andric   errs() << "- at:          " << Pos << '\n';
5100b57cec5SDimitry Andric }
5110b57cec5SDimitry Andric 
report_context(const LiveInterval & LI) const5120b57cec5SDimitry Andric void MachineVerifier::report_context(const LiveInterval &LI) const {
5130b57cec5SDimitry Andric   errs() << "- interval:    " << LI << '\n';
5140b57cec5SDimitry Andric }
5150b57cec5SDimitry Andric 
report_context(const LiveRange & LR,Register VRegUnit,LaneBitmask LaneMask) const516af732203SDimitry Andric void MachineVerifier::report_context(const LiveRange &LR, Register VRegUnit,
5170b57cec5SDimitry Andric                                      LaneBitmask LaneMask) const {
5180b57cec5SDimitry Andric   report_context_liverange(LR);
5190b57cec5SDimitry Andric   report_context_vreg_regunit(VRegUnit);
5200b57cec5SDimitry Andric   if (LaneMask.any())
5210b57cec5SDimitry Andric     report_context_lanemask(LaneMask);
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric 
report_context(const LiveRange::Segment & S) const5240b57cec5SDimitry Andric void MachineVerifier::report_context(const LiveRange::Segment &S) const {
5250b57cec5SDimitry Andric   errs() << "- segment:     " << S << '\n';
5260b57cec5SDimitry Andric }
5270b57cec5SDimitry Andric 
report_context(const VNInfo & VNI) const5280b57cec5SDimitry Andric void MachineVerifier::report_context(const VNInfo &VNI) const {
5290b57cec5SDimitry Andric   errs() << "- ValNo:       " << VNI.id << " (def " << VNI.def << ")\n";
5300b57cec5SDimitry Andric }
5310b57cec5SDimitry Andric 
report_context_liverange(const LiveRange & LR) const5320b57cec5SDimitry Andric void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
5330b57cec5SDimitry Andric   errs() << "- liverange:   " << LR << '\n';
5340b57cec5SDimitry Andric }
5350b57cec5SDimitry Andric 
report_context(MCPhysReg PReg) const5360b57cec5SDimitry Andric void MachineVerifier::report_context(MCPhysReg PReg) const {
5370b57cec5SDimitry Andric   errs() << "- p. register: " << printReg(PReg, TRI) << '\n';
5380b57cec5SDimitry Andric }
5390b57cec5SDimitry Andric 
report_context_vreg(Register VReg) const540af732203SDimitry Andric void MachineVerifier::report_context_vreg(Register VReg) const {
5410b57cec5SDimitry Andric   errs() << "- v. register: " << printReg(VReg, TRI) << '\n';
5420b57cec5SDimitry Andric }
5430b57cec5SDimitry Andric 
report_context_vreg_regunit(Register VRegOrUnit) const544af732203SDimitry Andric void MachineVerifier::report_context_vreg_regunit(Register VRegOrUnit) const {
5458bcb0991SDimitry Andric   if (Register::isVirtualRegister(VRegOrUnit)) {
5460b57cec5SDimitry Andric     report_context_vreg(VRegOrUnit);
5470b57cec5SDimitry Andric   } else {
5480b57cec5SDimitry Andric     errs() << "- regunit:     " << printRegUnit(VRegOrUnit, TRI) << '\n';
5490b57cec5SDimitry Andric   }
5500b57cec5SDimitry Andric }
5510b57cec5SDimitry Andric 
report_context_lanemask(LaneBitmask LaneMask) const5520b57cec5SDimitry Andric void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
5530b57cec5SDimitry Andric   errs() << "- lanemask:    " << PrintLaneMask(LaneMask) << '\n';
5540b57cec5SDimitry Andric }
5550b57cec5SDimitry Andric 
markReachable(const MachineBasicBlock * MBB)5560b57cec5SDimitry Andric void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
5570b57cec5SDimitry Andric   BBInfo &MInfo = MBBInfoMap[MBB];
5580b57cec5SDimitry Andric   if (!MInfo.reachable) {
5590b57cec5SDimitry Andric     MInfo.reachable = true;
5605ffd83dbSDimitry Andric     for (const MachineBasicBlock *Succ : MBB->successors())
5615ffd83dbSDimitry Andric       markReachable(Succ);
5620b57cec5SDimitry Andric   }
5630b57cec5SDimitry Andric }
5640b57cec5SDimitry Andric 
visitMachineFunctionBefore()5650b57cec5SDimitry Andric void MachineVerifier::visitMachineFunctionBefore() {
5660b57cec5SDimitry Andric   lastIndex = SlotIndex();
5670b57cec5SDimitry Andric   regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs()
5680b57cec5SDimitry Andric                                            : TRI->getReservedRegs(*MF);
5690b57cec5SDimitry Andric 
5700b57cec5SDimitry Andric   if (!MF->empty())
5710b57cec5SDimitry Andric     markReachable(&MF->front());
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   // Build a set of the basic blocks in the function.
5740b57cec5SDimitry Andric   FunctionBlocks.clear();
5750b57cec5SDimitry Andric   for (const auto &MBB : *MF) {
5760b57cec5SDimitry Andric     FunctionBlocks.insert(&MBB);
5770b57cec5SDimitry Andric     BBInfo &MInfo = MBBInfoMap[&MBB];
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric     MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
5800b57cec5SDimitry Andric     if (MInfo.Preds.size() != MBB.pred_size())
5810b57cec5SDimitry Andric       report("MBB has duplicate entries in its predecessor list.", &MBB);
5820b57cec5SDimitry Andric 
5830b57cec5SDimitry Andric     MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
5840b57cec5SDimitry Andric     if (MInfo.Succs.size() != MBB.succ_size())
5850b57cec5SDimitry Andric       report("MBB has duplicate entries in its successor list.", &MBB);
5860b57cec5SDimitry Andric   }
5870b57cec5SDimitry Andric 
5880b57cec5SDimitry Andric   // Check that the register use lists are sane.
5890b57cec5SDimitry Andric   MRI->verifyUseLists();
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric   if (!MF->empty())
5920b57cec5SDimitry Andric     verifyStackFrame();
5930b57cec5SDimitry Andric }
5940b57cec5SDimitry Andric 
5950b57cec5SDimitry Andric void
visitMachineBasicBlockBefore(const MachineBasicBlock * MBB)5960b57cec5SDimitry Andric MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
5970b57cec5SDimitry Andric   FirstTerminator = nullptr;
5980b57cec5SDimitry Andric   FirstNonPHI = nullptr;
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric   if (!MF->getProperties().hasProperty(
6010b57cec5SDimitry Andric       MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) {
6020b57cec5SDimitry Andric     // If this block has allocatable physical registers live-in, check that
6030b57cec5SDimitry Andric     // it is an entry block or landing pad.
6040b57cec5SDimitry Andric     for (const auto &LI : MBB->liveins()) {
6050b57cec5SDimitry Andric       if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
6060b57cec5SDimitry Andric           MBB->getIterator() != MBB->getParent()->begin()) {
6070b57cec5SDimitry Andric         report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB);
6080b57cec5SDimitry Andric         report_context(LI.PhysReg);
6090b57cec5SDimitry Andric       }
6100b57cec5SDimitry Andric     }
6110b57cec5SDimitry Andric   }
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric   // Count the number of landing pad successors.
6145ffd83dbSDimitry Andric   SmallPtrSet<const MachineBasicBlock*, 4> LandingPadSuccs;
6155ffd83dbSDimitry Andric   for (const auto *succ : MBB->successors()) {
6165ffd83dbSDimitry Andric     if (succ->isEHPad())
6175ffd83dbSDimitry Andric       LandingPadSuccs.insert(succ);
6185ffd83dbSDimitry Andric     if (!FunctionBlocks.count(succ))
6190b57cec5SDimitry Andric       report("MBB has successor that isn't part of the function.", MBB);
6205ffd83dbSDimitry Andric     if (!MBBInfoMap[succ].Preds.count(MBB)) {
6210b57cec5SDimitry Andric       report("Inconsistent CFG", MBB);
6220b57cec5SDimitry Andric       errs() << "MBB is not in the predecessor list of the successor "
6235ffd83dbSDimitry Andric              << printMBBReference(*succ) << ".\n";
6240b57cec5SDimitry Andric     }
6250b57cec5SDimitry Andric   }
6260b57cec5SDimitry Andric 
6270b57cec5SDimitry Andric   // Check the predecessor list.
6285ffd83dbSDimitry Andric   for (const MachineBasicBlock *Pred : MBB->predecessors()) {
6295ffd83dbSDimitry Andric     if (!FunctionBlocks.count(Pred))
6300b57cec5SDimitry Andric       report("MBB has predecessor that isn't part of the function.", MBB);
6315ffd83dbSDimitry Andric     if (!MBBInfoMap[Pred].Succs.count(MBB)) {
6320b57cec5SDimitry Andric       report("Inconsistent CFG", MBB);
6330b57cec5SDimitry Andric       errs() << "MBB is not in the successor list of the predecessor "
6345ffd83dbSDimitry Andric              << printMBBReference(*Pred) << ".\n";
6350b57cec5SDimitry Andric     }
6360b57cec5SDimitry Andric   }
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric   const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
6390b57cec5SDimitry Andric   const BasicBlock *BB = MBB->getBasicBlock();
6400b57cec5SDimitry Andric   const Function &F = MF->getFunction();
6410b57cec5SDimitry Andric   if (LandingPadSuccs.size() > 1 &&
6420b57cec5SDimitry Andric       !(AsmInfo &&
6430b57cec5SDimitry Andric         AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
6440b57cec5SDimitry Andric         BB && isa<SwitchInst>(BB->getTerminator())) &&
6450b57cec5SDimitry Andric       !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
6460b57cec5SDimitry Andric     report("MBB has more than one landing pad successor", MBB);
6470b57cec5SDimitry Andric 
6485ffd83dbSDimitry Andric   // Call analyzeBranch. If it succeeds, there several more conditions to check.
6490b57cec5SDimitry Andric   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
6500b57cec5SDimitry Andric   SmallVector<MachineOperand, 4> Cond;
6510b57cec5SDimitry Andric   if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
6520b57cec5SDimitry Andric                           Cond)) {
6535ffd83dbSDimitry Andric     // Ok, analyzeBranch thinks it knows what's going on with this block. Let's
6540b57cec5SDimitry Andric     // check whether its answers match up with reality.
6550b57cec5SDimitry Andric     if (!TBB && !FBB) {
6560b57cec5SDimitry Andric       // Block falls through to its successor.
6570b57cec5SDimitry Andric       if (!MBB->empty() && MBB->back().isBarrier() &&
6580b57cec5SDimitry Andric           !TII->isPredicated(MBB->back())) {
6590b57cec5SDimitry Andric         report("MBB exits via unconditional fall-through but ends with a "
6600b57cec5SDimitry Andric                "barrier instruction!", MBB);
6610b57cec5SDimitry Andric       }
6620b57cec5SDimitry Andric       if (!Cond.empty()) {
6630b57cec5SDimitry Andric         report("MBB exits via unconditional fall-through but has a condition!",
6640b57cec5SDimitry Andric                MBB);
6650b57cec5SDimitry Andric       }
6660b57cec5SDimitry Andric     } else if (TBB && !FBB && Cond.empty()) {
6670b57cec5SDimitry Andric       // Block unconditionally branches somewhere.
6680b57cec5SDimitry Andric       if (MBB->empty()) {
6690b57cec5SDimitry Andric         report("MBB exits via unconditional branch but doesn't contain "
6700b57cec5SDimitry Andric                "any instructions!", MBB);
6710b57cec5SDimitry Andric       } else if (!MBB->back().isBarrier()) {
6720b57cec5SDimitry Andric         report("MBB exits via unconditional branch but doesn't end with a "
6730b57cec5SDimitry Andric                "barrier instruction!", MBB);
6740b57cec5SDimitry Andric       } else if (!MBB->back().isTerminator()) {
6750b57cec5SDimitry Andric         report("MBB exits via unconditional branch but the branch isn't a "
6760b57cec5SDimitry Andric                "terminator instruction!", MBB);
6770b57cec5SDimitry Andric       }
6780b57cec5SDimitry Andric     } else if (TBB && !FBB && !Cond.empty()) {
6790b57cec5SDimitry Andric       // Block conditionally branches somewhere, otherwise falls through.
6800b57cec5SDimitry Andric       if (MBB->empty()) {
6810b57cec5SDimitry Andric         report("MBB exits via conditional branch/fall-through but doesn't "
6820b57cec5SDimitry Andric                "contain any instructions!", MBB);
6830b57cec5SDimitry Andric       } else if (MBB->back().isBarrier()) {
6840b57cec5SDimitry Andric         report("MBB exits via conditional branch/fall-through but ends with a "
6850b57cec5SDimitry Andric                "barrier instruction!", MBB);
6860b57cec5SDimitry Andric       } else if (!MBB->back().isTerminator()) {
6870b57cec5SDimitry Andric         report("MBB exits via conditional branch/fall-through but the branch "
6880b57cec5SDimitry Andric                "isn't a terminator instruction!", MBB);
6890b57cec5SDimitry Andric       }
6900b57cec5SDimitry Andric     } else if (TBB && FBB) {
6910b57cec5SDimitry Andric       // Block conditionally branches somewhere, otherwise branches
6920b57cec5SDimitry Andric       // somewhere else.
6930b57cec5SDimitry Andric       if (MBB->empty()) {
6940b57cec5SDimitry Andric         report("MBB exits via conditional branch/branch but doesn't "
6950b57cec5SDimitry Andric                "contain any instructions!", MBB);
6960b57cec5SDimitry Andric       } else if (!MBB->back().isBarrier()) {
6970b57cec5SDimitry Andric         report("MBB exits via conditional branch/branch but doesn't end with a "
6980b57cec5SDimitry Andric                "barrier instruction!", MBB);
6990b57cec5SDimitry Andric       } else if (!MBB->back().isTerminator()) {
7000b57cec5SDimitry Andric         report("MBB exits via conditional branch/branch but the branch "
7010b57cec5SDimitry Andric                "isn't a terminator instruction!", MBB);
7020b57cec5SDimitry Andric       }
7030b57cec5SDimitry Andric       if (Cond.empty()) {
7040b57cec5SDimitry Andric         report("MBB exits via conditional branch/branch but there's no "
7050b57cec5SDimitry Andric                "condition!", MBB);
7060b57cec5SDimitry Andric       }
7070b57cec5SDimitry Andric     } else {
7085ffd83dbSDimitry Andric       report("analyzeBranch returned invalid data!", MBB);
7095ffd83dbSDimitry Andric     }
7105ffd83dbSDimitry Andric 
7115ffd83dbSDimitry Andric     // Now check that the successors match up with the answers reported by
7125ffd83dbSDimitry Andric     // analyzeBranch.
7135ffd83dbSDimitry Andric     if (TBB && !MBB->isSuccessor(TBB))
7145ffd83dbSDimitry Andric       report("MBB exits via jump or conditional branch, but its target isn't a "
7155ffd83dbSDimitry Andric              "CFG successor!",
7165ffd83dbSDimitry Andric              MBB);
7175ffd83dbSDimitry Andric     if (FBB && !MBB->isSuccessor(FBB))
7185ffd83dbSDimitry Andric       report("MBB exits via conditional branch, but its target isn't a CFG "
7195ffd83dbSDimitry Andric              "successor!",
7205ffd83dbSDimitry Andric              MBB);
7215ffd83dbSDimitry Andric 
7225ffd83dbSDimitry Andric     // There might be a fallthrough to the next block if there's either no
7235ffd83dbSDimitry Andric     // unconditional true branch, or if there's a condition, and one of the
7245ffd83dbSDimitry Andric     // branches is missing.
7255ffd83dbSDimitry Andric     bool Fallthrough = !TBB || (!Cond.empty() && !FBB);
7265ffd83dbSDimitry Andric 
7275ffd83dbSDimitry Andric     // A conditional fallthrough must be an actual CFG successor, not
7285ffd83dbSDimitry Andric     // unreachable. (Conversely, an unconditional fallthrough might not really
7295ffd83dbSDimitry Andric     // be a successor, because the block might end in unreachable.)
7305ffd83dbSDimitry Andric     if (!Cond.empty() && !FBB) {
7315ffd83dbSDimitry Andric       MachineFunction::const_iterator MBBI = std::next(MBB->getIterator());
7325ffd83dbSDimitry Andric       if (MBBI == MF->end()) {
7335ffd83dbSDimitry Andric         report("MBB conditionally falls through out of function!", MBB);
7345ffd83dbSDimitry Andric       } else if (!MBB->isSuccessor(&*MBBI))
7355ffd83dbSDimitry Andric         report("MBB exits via conditional branch/fall-through but the CFG "
7365ffd83dbSDimitry Andric                "successors don't match the actual successors!",
7375ffd83dbSDimitry Andric                MBB);
7385ffd83dbSDimitry Andric     }
7395ffd83dbSDimitry Andric 
7405ffd83dbSDimitry Andric     // Verify that there aren't any extra un-accounted-for successors.
7415ffd83dbSDimitry Andric     for (const MachineBasicBlock *SuccMBB : MBB->successors()) {
7425ffd83dbSDimitry Andric       // If this successor is one of the branch targets, it's okay.
7435ffd83dbSDimitry Andric       if (SuccMBB == TBB || SuccMBB == FBB)
7445ffd83dbSDimitry Andric         continue;
7455ffd83dbSDimitry Andric       // If we might have a fallthrough, and the successor is the fallthrough
7465ffd83dbSDimitry Andric       // block, that's also ok.
7475ffd83dbSDimitry Andric       if (Fallthrough && SuccMBB == MBB->getNextNode())
7485ffd83dbSDimitry Andric         continue;
7495ffd83dbSDimitry Andric       // Also accept successors which are for exception-handling or might be
7505ffd83dbSDimitry Andric       // inlineasm_br targets.
7515ffd83dbSDimitry Andric       if (SuccMBB->isEHPad() || SuccMBB->isInlineAsmBrIndirectTarget())
7525ffd83dbSDimitry Andric         continue;
7535ffd83dbSDimitry Andric       report("MBB has unexpected successors which are not branch targets, "
7545ffd83dbSDimitry Andric              "fallthrough, EHPads, or inlineasm_br targets.",
7555ffd83dbSDimitry Andric              MBB);
7560b57cec5SDimitry Andric     }
7570b57cec5SDimitry Andric   }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   regsLive.clear();
7600b57cec5SDimitry Andric   if (MRI->tracksLiveness()) {
7610b57cec5SDimitry Andric     for (const auto &LI : MBB->liveins()) {
7628bcb0991SDimitry Andric       if (!Register::isPhysicalRegister(LI.PhysReg)) {
7630b57cec5SDimitry Andric         report("MBB live-in list contains non-physical register", MBB);
7640b57cec5SDimitry Andric         continue;
7650b57cec5SDimitry Andric       }
766480093f4SDimitry Andric       for (const MCPhysReg &SubReg : TRI->subregs_inclusive(LI.PhysReg))
767480093f4SDimitry Andric         regsLive.insert(SubReg);
7680b57cec5SDimitry Andric     }
7690b57cec5SDimitry Andric   }
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   const MachineFrameInfo &MFI = MF->getFrameInfo();
7720b57cec5SDimitry Andric   BitVector PR = MFI.getPristineRegs(*MF);
7730b57cec5SDimitry Andric   for (unsigned I : PR.set_bits()) {
774480093f4SDimitry Andric     for (const MCPhysReg &SubReg : TRI->subregs_inclusive(I))
775480093f4SDimitry Andric       regsLive.insert(SubReg);
7760b57cec5SDimitry Andric   }
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric   regsKilled.clear();
7790b57cec5SDimitry Andric   regsDefined.clear();
7800b57cec5SDimitry Andric 
7810b57cec5SDimitry Andric   if (Indexes)
7820b57cec5SDimitry Andric     lastIndex = Indexes->getMBBStartIdx(MBB);
7830b57cec5SDimitry Andric }
7840b57cec5SDimitry Andric 
7850b57cec5SDimitry Andric // This function gets called for all bundle headers, including normal
7860b57cec5SDimitry Andric // stand-alone unbundled instructions.
visitMachineBundleBefore(const MachineInstr * MI)7870b57cec5SDimitry Andric void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
7880b57cec5SDimitry Andric   if (Indexes && Indexes->hasIndex(*MI)) {
7890b57cec5SDimitry Andric     SlotIndex idx = Indexes->getInstructionIndex(*MI);
7900b57cec5SDimitry Andric     if (!(idx > lastIndex)) {
7910b57cec5SDimitry Andric       report("Instruction index out of order", MI);
7920b57cec5SDimitry Andric       errs() << "Last instruction was at " << lastIndex << '\n';
7930b57cec5SDimitry Andric     }
7940b57cec5SDimitry Andric     lastIndex = idx;
7950b57cec5SDimitry Andric   }
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric   // Ensure non-terminators don't follow terminators.
798af732203SDimitry Andric   if (MI->isTerminator()) {
7990b57cec5SDimitry Andric     if (!FirstTerminator)
8000b57cec5SDimitry Andric       FirstTerminator = MI;
8015ffd83dbSDimitry Andric   } else if (FirstTerminator) {
8020b57cec5SDimitry Andric     report("Non-terminator instruction after the first terminator", MI);
8030b57cec5SDimitry Andric     errs() << "First terminator was:\t" << *FirstTerminator;
8040b57cec5SDimitry Andric   }
8050b57cec5SDimitry Andric }
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric // The operands on an INLINEASM instruction must follow a template.
8080b57cec5SDimitry Andric // Verify that the flag operands make sense.
verifyInlineAsm(const MachineInstr * MI)8090b57cec5SDimitry Andric void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
8100b57cec5SDimitry Andric   // The first two operands on INLINEASM are the asm string and global flags.
8110b57cec5SDimitry Andric   if (MI->getNumOperands() < 2) {
8120b57cec5SDimitry Andric     report("Too few operands on inline asm", MI);
8130b57cec5SDimitry Andric     return;
8140b57cec5SDimitry Andric   }
8150b57cec5SDimitry Andric   if (!MI->getOperand(0).isSymbol())
8160b57cec5SDimitry Andric     report("Asm string must be an external symbol", MI);
8170b57cec5SDimitry Andric   if (!MI->getOperand(1).isImm())
8180b57cec5SDimitry Andric     report("Asm flags must be an immediate", MI);
8190b57cec5SDimitry Andric   // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
8200b57cec5SDimitry Andric   // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
8210b57cec5SDimitry Andric   // and Extra_IsConvergent = 32.
8220b57cec5SDimitry Andric   if (!isUInt<6>(MI->getOperand(1).getImm()))
8230b57cec5SDimitry Andric     report("Unknown asm flags", &MI->getOperand(1), 1);
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric   static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
8260b57cec5SDimitry Andric 
8270b57cec5SDimitry Andric   unsigned OpNo = InlineAsm::MIOp_FirstOperand;
8280b57cec5SDimitry Andric   unsigned NumOps;
8290b57cec5SDimitry Andric   for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
8300b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(OpNo);
8310b57cec5SDimitry Andric     // There may be implicit ops after the fixed operands.
8320b57cec5SDimitry Andric     if (!MO.isImm())
8330b57cec5SDimitry Andric       break;
8340b57cec5SDimitry Andric     NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
8350b57cec5SDimitry Andric   }
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric   if (OpNo > MI->getNumOperands())
8380b57cec5SDimitry Andric     report("Missing operands in last group", MI);
8390b57cec5SDimitry Andric 
8400b57cec5SDimitry Andric   // An optional MDNode follows the groups.
8410b57cec5SDimitry Andric   if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
8420b57cec5SDimitry Andric     ++OpNo;
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric   // All trailing operands must be implicit registers.
8450b57cec5SDimitry Andric   for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
8460b57cec5SDimitry Andric     const MachineOperand &MO = MI->getOperand(OpNo);
8470b57cec5SDimitry Andric     if (!MO.isReg() || !MO.isImplicit())
8480b57cec5SDimitry Andric       report("Expected implicit register after groups", &MO, OpNo);
8490b57cec5SDimitry Andric   }
8500b57cec5SDimitry Andric }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric /// Check that types are consistent when two operands need to have the same
8530b57cec5SDimitry Andric /// number of vector elements.
8540b57cec5SDimitry Andric /// \return true if the types are valid.
verifyVectorElementMatch(LLT Ty0,LLT Ty1,const MachineInstr * MI)8550b57cec5SDimitry Andric bool MachineVerifier::verifyVectorElementMatch(LLT Ty0, LLT Ty1,
8560b57cec5SDimitry Andric                                                const MachineInstr *MI) {
8570b57cec5SDimitry Andric   if (Ty0.isVector() != Ty1.isVector()) {
8580b57cec5SDimitry Andric     report("operand types must be all-vector or all-scalar", MI);
8590b57cec5SDimitry Andric     // Generally we try to report as many issues as possible at once, but in
8600b57cec5SDimitry Andric     // this case it's not clear what should we be comparing the size of the
8610b57cec5SDimitry Andric     // scalar with: the size of the whole vector or its lane. Instead of
8620b57cec5SDimitry Andric     // making an arbitrary choice and emitting not so helpful message, let's
8630b57cec5SDimitry Andric     // avoid the extra noise and stop here.
8640b57cec5SDimitry Andric     return false;
8650b57cec5SDimitry Andric   }
8660b57cec5SDimitry Andric 
8670b57cec5SDimitry Andric   if (Ty0.isVector() && Ty0.getNumElements() != Ty1.getNumElements()) {
8680b57cec5SDimitry Andric     report("operand types must preserve number of vector elements", MI);
8690b57cec5SDimitry Andric     return false;
8700b57cec5SDimitry Andric   }
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric   return true;
8730b57cec5SDimitry Andric }
8740b57cec5SDimitry Andric 
verifyPreISelGenericInstruction(const MachineInstr * MI)8750b57cec5SDimitry Andric void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) {
8760b57cec5SDimitry Andric   if (isFunctionSelected)
8770b57cec5SDimitry Andric     report("Unexpected generic instruction in a Selected function", MI);
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI->getDesc();
8800b57cec5SDimitry Andric   unsigned NumOps = MI->getNumOperands();
8810b57cec5SDimitry Andric 
8825ffd83dbSDimitry Andric   // Branches must reference a basic block if they are not indirect
8835ffd83dbSDimitry Andric   if (MI->isBranch() && !MI->isIndirectBranch()) {
8845ffd83dbSDimitry Andric     bool HasMBB = false;
8855ffd83dbSDimitry Andric     for (const MachineOperand &Op : MI->operands()) {
8865ffd83dbSDimitry Andric       if (Op.isMBB()) {
8875ffd83dbSDimitry Andric         HasMBB = true;
8885ffd83dbSDimitry Andric         break;
8895ffd83dbSDimitry Andric       }
8905ffd83dbSDimitry Andric     }
8915ffd83dbSDimitry Andric 
8925ffd83dbSDimitry Andric     if (!HasMBB) {
8935ffd83dbSDimitry Andric       report("Branch instruction is missing a basic block operand or "
8945ffd83dbSDimitry Andric              "isIndirectBranch property",
8955ffd83dbSDimitry Andric              MI);
8965ffd83dbSDimitry Andric     }
8975ffd83dbSDimitry Andric   }
8985ffd83dbSDimitry Andric 
8990b57cec5SDimitry Andric   // Check types.
9000b57cec5SDimitry Andric   SmallVector<LLT, 4> Types;
9010b57cec5SDimitry Andric   for (unsigned I = 0, E = std::min(MCID.getNumOperands(), NumOps);
9020b57cec5SDimitry Andric        I != E; ++I) {
9030b57cec5SDimitry Andric     if (!MCID.OpInfo[I].isGenericType())
9040b57cec5SDimitry Andric       continue;
9050b57cec5SDimitry Andric     // Generic instructions specify type equality constraints between some of
9060b57cec5SDimitry Andric     // their operands. Make sure these are consistent.
9070b57cec5SDimitry Andric     size_t TypeIdx = MCID.OpInfo[I].getGenericTypeIndex();
9080b57cec5SDimitry Andric     Types.resize(std::max(TypeIdx + 1, Types.size()));
9090b57cec5SDimitry Andric 
9100b57cec5SDimitry Andric     const MachineOperand *MO = &MI->getOperand(I);
9110b57cec5SDimitry Andric     if (!MO->isReg()) {
9120b57cec5SDimitry Andric       report("generic instruction must use register operands", MI);
9130b57cec5SDimitry Andric       continue;
9140b57cec5SDimitry Andric     }
9150b57cec5SDimitry Andric 
9160b57cec5SDimitry Andric     LLT OpTy = MRI->getType(MO->getReg());
9170b57cec5SDimitry Andric     // Don't report a type mismatch if there is no actual mismatch, only a
9180b57cec5SDimitry Andric     // type missing, to reduce noise:
9190b57cec5SDimitry Andric     if (OpTy.isValid()) {
9200b57cec5SDimitry Andric       // Only the first valid type for a type index will be printed: don't
9210b57cec5SDimitry Andric       // overwrite it later so it's always clear which type was expected:
9220b57cec5SDimitry Andric       if (!Types[TypeIdx].isValid())
9230b57cec5SDimitry Andric         Types[TypeIdx] = OpTy;
9240b57cec5SDimitry Andric       else if (Types[TypeIdx] != OpTy)
9250b57cec5SDimitry Andric         report("Type mismatch in generic instruction", MO, I, OpTy);
9260b57cec5SDimitry Andric     } else {
9270b57cec5SDimitry Andric       // Generic instructions must have types attached to their operands.
9280b57cec5SDimitry Andric       report("Generic instruction is missing a virtual register type", MO, I);
9290b57cec5SDimitry Andric     }
9300b57cec5SDimitry Andric   }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   // Generic opcodes must not have physical register operands.
9330b57cec5SDimitry Andric   for (unsigned I = 0; I < MI->getNumOperands(); ++I) {
9340b57cec5SDimitry Andric     const MachineOperand *MO = &MI->getOperand(I);
9358bcb0991SDimitry Andric     if (MO->isReg() && Register::isPhysicalRegister(MO->getReg()))
9360b57cec5SDimitry Andric       report("Generic instruction cannot have physical register", MO, I);
9370b57cec5SDimitry Andric   }
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric   // Avoid out of bounds in checks below. This was already reported earlier.
9400b57cec5SDimitry Andric   if (MI->getNumOperands() < MCID.getNumOperands())
9410b57cec5SDimitry Andric     return;
9420b57cec5SDimitry Andric 
9430b57cec5SDimitry Andric   StringRef ErrorInfo;
9440b57cec5SDimitry Andric   if (!TII->verifyInstruction(*MI, ErrorInfo))
9450b57cec5SDimitry Andric     report(ErrorInfo.data(), MI);
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric   // Verify properties of various specific instruction types
948*5f7ddb14SDimitry Andric   unsigned Opc = MI->getOpcode();
949*5f7ddb14SDimitry Andric   switch (Opc) {
950*5f7ddb14SDimitry Andric   case TargetOpcode::G_ASSERT_SEXT:
951*5f7ddb14SDimitry Andric   case TargetOpcode::G_ASSERT_ZEXT: {
952*5f7ddb14SDimitry Andric     std::string OpcName =
953*5f7ddb14SDimitry Andric         Opc == TargetOpcode::G_ASSERT_ZEXT ? "G_ASSERT_ZEXT" : "G_ASSERT_SEXT";
954*5f7ddb14SDimitry Andric     if (!MI->getOperand(2).isImm()) {
955*5f7ddb14SDimitry Andric       report(Twine(OpcName, " expects an immediate operand #2"), MI);
956*5f7ddb14SDimitry Andric       break;
957*5f7ddb14SDimitry Andric     }
958*5f7ddb14SDimitry Andric 
959*5f7ddb14SDimitry Andric     Register Dst = MI->getOperand(0).getReg();
960*5f7ddb14SDimitry Andric     Register Src = MI->getOperand(1).getReg();
961*5f7ddb14SDimitry Andric     LLT SrcTy = MRI->getType(Src);
962*5f7ddb14SDimitry Andric     int64_t Imm = MI->getOperand(2).getImm();
963*5f7ddb14SDimitry Andric     if (Imm <= 0) {
964*5f7ddb14SDimitry Andric       report(Twine(OpcName, " size must be >= 1"), MI);
965*5f7ddb14SDimitry Andric       break;
966*5f7ddb14SDimitry Andric     }
967*5f7ddb14SDimitry Andric 
968*5f7ddb14SDimitry Andric     if (Imm >= SrcTy.getScalarSizeInBits()) {
969*5f7ddb14SDimitry Andric       report(Twine(OpcName, " size must be less than source bit width"), MI);
970*5f7ddb14SDimitry Andric       break;
971*5f7ddb14SDimitry Andric     }
972*5f7ddb14SDimitry Andric 
973*5f7ddb14SDimitry Andric     if (MRI->getRegBankOrNull(Src) != MRI->getRegBankOrNull(Dst)) {
974*5f7ddb14SDimitry Andric       report(
975*5f7ddb14SDimitry Andric           Twine(OpcName, " source and destination register banks must match"),
976*5f7ddb14SDimitry Andric           MI);
977*5f7ddb14SDimitry Andric       break;
978*5f7ddb14SDimitry Andric     }
979*5f7ddb14SDimitry Andric 
980*5f7ddb14SDimitry Andric     if (MRI->getRegClassOrNull(Src) != MRI->getRegClassOrNull(Dst))
981*5f7ddb14SDimitry Andric       report(
982*5f7ddb14SDimitry Andric           Twine(OpcName, " source and destination register classes must match"),
983*5f7ddb14SDimitry Andric           MI);
984*5f7ddb14SDimitry Andric 
985*5f7ddb14SDimitry Andric     break;
986*5f7ddb14SDimitry Andric   }
987*5f7ddb14SDimitry Andric 
9880b57cec5SDimitry Andric   case TargetOpcode::G_CONSTANT:
9890b57cec5SDimitry Andric   case TargetOpcode::G_FCONSTANT: {
9900b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
9910b57cec5SDimitry Andric     if (DstTy.isVector())
9920b57cec5SDimitry Andric       report("Instruction cannot use a vector result type", MI);
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric     if (MI->getOpcode() == TargetOpcode::G_CONSTANT) {
9950b57cec5SDimitry Andric       if (!MI->getOperand(1).isCImm()) {
9960b57cec5SDimitry Andric         report("G_CONSTANT operand must be cimm", MI);
9970b57cec5SDimitry Andric         break;
9980b57cec5SDimitry Andric       }
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric       const ConstantInt *CI = MI->getOperand(1).getCImm();
10010b57cec5SDimitry Andric       if (CI->getBitWidth() != DstTy.getSizeInBits())
10020b57cec5SDimitry Andric         report("inconsistent constant size", MI);
10030b57cec5SDimitry Andric     } else {
10040b57cec5SDimitry Andric       if (!MI->getOperand(1).isFPImm()) {
10050b57cec5SDimitry Andric         report("G_FCONSTANT operand must be fpimm", MI);
10060b57cec5SDimitry Andric         break;
10070b57cec5SDimitry Andric       }
10080b57cec5SDimitry Andric       const ConstantFP *CF = MI->getOperand(1).getFPImm();
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric       if (APFloat::getSizeInBits(CF->getValueAPF().getSemantics()) !=
10110b57cec5SDimitry Andric           DstTy.getSizeInBits()) {
10120b57cec5SDimitry Andric         report("inconsistent constant size", MI);
10130b57cec5SDimitry Andric       }
10140b57cec5SDimitry Andric     }
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric     break;
10170b57cec5SDimitry Andric   }
10180b57cec5SDimitry Andric   case TargetOpcode::G_LOAD:
10190b57cec5SDimitry Andric   case TargetOpcode::G_STORE:
10200b57cec5SDimitry Andric   case TargetOpcode::G_ZEXTLOAD:
10210b57cec5SDimitry Andric   case TargetOpcode::G_SEXTLOAD: {
10220b57cec5SDimitry Andric     LLT ValTy = MRI->getType(MI->getOperand(0).getReg());
10230b57cec5SDimitry Andric     LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
10240b57cec5SDimitry Andric     if (!PtrTy.isPointer())
10250b57cec5SDimitry Andric       report("Generic memory instruction must access a pointer", MI);
10260b57cec5SDimitry Andric 
10270b57cec5SDimitry Andric     // Generic loads and stores must have a single MachineMemOperand
10280b57cec5SDimitry Andric     // describing that access.
10290b57cec5SDimitry Andric     if (!MI->hasOneMemOperand()) {
10300b57cec5SDimitry Andric       report("Generic instruction accessing memory must have one mem operand",
10310b57cec5SDimitry Andric              MI);
10320b57cec5SDimitry Andric     } else {
10330b57cec5SDimitry Andric       const MachineMemOperand &MMO = **MI->memoperands_begin();
10340b57cec5SDimitry Andric       if (MI->getOpcode() == TargetOpcode::G_ZEXTLOAD ||
10350b57cec5SDimitry Andric           MI->getOpcode() == TargetOpcode::G_SEXTLOAD) {
10360b57cec5SDimitry Andric         if (MMO.getSizeInBits() >= ValTy.getSizeInBits())
10370b57cec5SDimitry Andric           report("Generic extload must have a narrower memory type", MI);
10380b57cec5SDimitry Andric       } else if (MI->getOpcode() == TargetOpcode::G_LOAD) {
10390b57cec5SDimitry Andric         if (MMO.getSize() > ValTy.getSizeInBytes())
10400b57cec5SDimitry Andric           report("load memory size cannot exceed result size", MI);
10410b57cec5SDimitry Andric       } else if (MI->getOpcode() == TargetOpcode::G_STORE) {
10420b57cec5SDimitry Andric         if (ValTy.getSizeInBytes() < MMO.getSize())
10430b57cec5SDimitry Andric           report("store memory size cannot exceed value size", MI);
10440b57cec5SDimitry Andric       }
10450b57cec5SDimitry Andric     }
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric     break;
10480b57cec5SDimitry Andric   }
10490b57cec5SDimitry Andric   case TargetOpcode::G_PHI: {
10500b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1051af732203SDimitry Andric     if (!DstTy.isValid() || !all_of(drop_begin(MI->operands()),
10520b57cec5SDimitry Andric                                     [this, &DstTy](const MachineOperand &MO) {
10530b57cec5SDimitry Andric                                       if (!MO.isReg())
10540b57cec5SDimitry Andric                                         return true;
10550b57cec5SDimitry Andric                                       LLT Ty = MRI->getType(MO.getReg());
10560b57cec5SDimitry Andric                                       if (!Ty.isValid() || (Ty != DstTy))
10570b57cec5SDimitry Andric                                         return false;
10580b57cec5SDimitry Andric                                       return true;
10590b57cec5SDimitry Andric                                     }))
10600b57cec5SDimitry Andric       report("Generic Instruction G_PHI has operands with incompatible/missing "
10610b57cec5SDimitry Andric              "types",
10620b57cec5SDimitry Andric              MI);
10630b57cec5SDimitry Andric     break;
10640b57cec5SDimitry Andric   }
10650b57cec5SDimitry Andric   case TargetOpcode::G_BITCAST: {
10660b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
10670b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
10680b57cec5SDimitry Andric     if (!DstTy.isValid() || !SrcTy.isValid())
10690b57cec5SDimitry Andric       break;
10700b57cec5SDimitry Andric 
10710b57cec5SDimitry Andric     if (SrcTy.isPointer() != DstTy.isPointer())
10720b57cec5SDimitry Andric       report("bitcast cannot convert between pointers and other types", MI);
10730b57cec5SDimitry Andric 
10740b57cec5SDimitry Andric     if (SrcTy.getSizeInBits() != DstTy.getSizeInBits())
10750b57cec5SDimitry Andric       report("bitcast sizes must match", MI);
10765ffd83dbSDimitry Andric 
10775ffd83dbSDimitry Andric     if (SrcTy == DstTy)
10785ffd83dbSDimitry Andric       report("bitcast must change the type", MI);
10795ffd83dbSDimitry Andric 
10800b57cec5SDimitry Andric     break;
10810b57cec5SDimitry Andric   }
10820b57cec5SDimitry Andric   case TargetOpcode::G_INTTOPTR:
10830b57cec5SDimitry Andric   case TargetOpcode::G_PTRTOINT:
10840b57cec5SDimitry Andric   case TargetOpcode::G_ADDRSPACE_CAST: {
10850b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
10860b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
10870b57cec5SDimitry Andric     if (!DstTy.isValid() || !SrcTy.isValid())
10880b57cec5SDimitry Andric       break;
10890b57cec5SDimitry Andric 
10900b57cec5SDimitry Andric     verifyVectorElementMatch(DstTy, SrcTy, MI);
10910b57cec5SDimitry Andric 
10920b57cec5SDimitry Andric     DstTy = DstTy.getScalarType();
10930b57cec5SDimitry Andric     SrcTy = SrcTy.getScalarType();
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric     if (MI->getOpcode() == TargetOpcode::G_INTTOPTR) {
10960b57cec5SDimitry Andric       if (!DstTy.isPointer())
10970b57cec5SDimitry Andric         report("inttoptr result type must be a pointer", MI);
10980b57cec5SDimitry Andric       if (SrcTy.isPointer())
10990b57cec5SDimitry Andric         report("inttoptr source type must not be a pointer", MI);
11000b57cec5SDimitry Andric     } else if (MI->getOpcode() == TargetOpcode::G_PTRTOINT) {
11010b57cec5SDimitry Andric       if (!SrcTy.isPointer())
11020b57cec5SDimitry Andric         report("ptrtoint source type must be a pointer", MI);
11030b57cec5SDimitry Andric       if (DstTy.isPointer())
11040b57cec5SDimitry Andric         report("ptrtoint result type must not be a pointer", MI);
11050b57cec5SDimitry Andric     } else {
11060b57cec5SDimitry Andric       assert(MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST);
11070b57cec5SDimitry Andric       if (!SrcTy.isPointer() || !DstTy.isPointer())
11080b57cec5SDimitry Andric         report("addrspacecast types must be pointers", MI);
11090b57cec5SDimitry Andric       else {
11100b57cec5SDimitry Andric         if (SrcTy.getAddressSpace() == DstTy.getAddressSpace())
11110b57cec5SDimitry Andric           report("addrspacecast must convert different address spaces", MI);
11120b57cec5SDimitry Andric       }
11130b57cec5SDimitry Andric     }
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric     break;
11160b57cec5SDimitry Andric   }
1117480093f4SDimitry Andric   case TargetOpcode::G_PTR_ADD: {
11180b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
11190b57cec5SDimitry Andric     LLT PtrTy = MRI->getType(MI->getOperand(1).getReg());
11200b57cec5SDimitry Andric     LLT OffsetTy = MRI->getType(MI->getOperand(2).getReg());
11210b57cec5SDimitry Andric     if (!DstTy.isValid() || !PtrTy.isValid() || !OffsetTy.isValid())
11220b57cec5SDimitry Andric       break;
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric     if (!PtrTy.getScalarType().isPointer())
11250b57cec5SDimitry Andric       report("gep first operand must be a pointer", MI);
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric     if (OffsetTy.getScalarType().isPointer())
11280b57cec5SDimitry Andric       report("gep offset operand must not be a pointer", MI);
11290b57cec5SDimitry Andric 
11300b57cec5SDimitry Andric     // TODO: Is the offset allowed to be a scalar with a vector?
11310b57cec5SDimitry Andric     break;
11320b57cec5SDimitry Andric   }
11335ffd83dbSDimitry Andric   case TargetOpcode::G_PTRMASK: {
11345ffd83dbSDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
11355ffd83dbSDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
11365ffd83dbSDimitry Andric     LLT MaskTy = MRI->getType(MI->getOperand(2).getReg());
11375ffd83dbSDimitry Andric     if (!DstTy.isValid() || !SrcTy.isValid() || !MaskTy.isValid())
11385ffd83dbSDimitry Andric       break;
11395ffd83dbSDimitry Andric 
11405ffd83dbSDimitry Andric     if (!DstTy.getScalarType().isPointer())
11415ffd83dbSDimitry Andric       report("ptrmask result type must be a pointer", MI);
11425ffd83dbSDimitry Andric 
11435ffd83dbSDimitry Andric     if (!MaskTy.getScalarType().isScalar())
11445ffd83dbSDimitry Andric       report("ptrmask mask type must be an integer", MI);
11455ffd83dbSDimitry Andric 
11465ffd83dbSDimitry Andric     verifyVectorElementMatch(DstTy, MaskTy, MI);
11475ffd83dbSDimitry Andric     break;
11485ffd83dbSDimitry Andric   }
11490b57cec5SDimitry Andric   case TargetOpcode::G_SEXT:
11500b57cec5SDimitry Andric   case TargetOpcode::G_ZEXT:
11510b57cec5SDimitry Andric   case TargetOpcode::G_ANYEXT:
11520b57cec5SDimitry Andric   case TargetOpcode::G_TRUNC:
11530b57cec5SDimitry Andric   case TargetOpcode::G_FPEXT:
11540b57cec5SDimitry Andric   case TargetOpcode::G_FPTRUNC: {
11550b57cec5SDimitry Andric     // Number of operands and presense of types is already checked (and
11560b57cec5SDimitry Andric     // reported in case of any issues), so no need to report them again. As
11570b57cec5SDimitry Andric     // we're trying to report as many issues as possible at once, however, the
11580b57cec5SDimitry Andric     // instructions aren't guaranteed to have the right number of operands or
11590b57cec5SDimitry Andric     // types attached to them at this point
11600b57cec5SDimitry Andric     assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}");
11610b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
11620b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
11630b57cec5SDimitry Andric     if (!DstTy.isValid() || !SrcTy.isValid())
11640b57cec5SDimitry Andric       break;
11650b57cec5SDimitry Andric 
11660b57cec5SDimitry Andric     LLT DstElTy = DstTy.getScalarType();
11670b57cec5SDimitry Andric     LLT SrcElTy = SrcTy.getScalarType();
11680b57cec5SDimitry Andric     if (DstElTy.isPointer() || SrcElTy.isPointer())
11690b57cec5SDimitry Andric       report("Generic extend/truncate can not operate on pointers", MI);
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric     verifyVectorElementMatch(DstTy, SrcTy, MI);
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric     unsigned DstSize = DstElTy.getSizeInBits();
11740b57cec5SDimitry Andric     unsigned SrcSize = SrcElTy.getSizeInBits();
11750b57cec5SDimitry Andric     switch (MI->getOpcode()) {
11760b57cec5SDimitry Andric     default:
11770b57cec5SDimitry Andric       if (DstSize <= SrcSize)
11780b57cec5SDimitry Andric         report("Generic extend has destination type no larger than source", MI);
11790b57cec5SDimitry Andric       break;
11800b57cec5SDimitry Andric     case TargetOpcode::G_TRUNC:
11810b57cec5SDimitry Andric     case TargetOpcode::G_FPTRUNC:
11820b57cec5SDimitry Andric       if (DstSize >= SrcSize)
11830b57cec5SDimitry Andric         report("Generic truncate has destination type no smaller than source",
11840b57cec5SDimitry Andric                MI);
11850b57cec5SDimitry Andric       break;
11860b57cec5SDimitry Andric     }
11870b57cec5SDimitry Andric     break;
11880b57cec5SDimitry Andric   }
11890b57cec5SDimitry Andric   case TargetOpcode::G_SELECT: {
11900b57cec5SDimitry Andric     LLT SelTy = MRI->getType(MI->getOperand(0).getReg());
11910b57cec5SDimitry Andric     LLT CondTy = MRI->getType(MI->getOperand(1).getReg());
11920b57cec5SDimitry Andric     if (!SelTy.isValid() || !CondTy.isValid())
11930b57cec5SDimitry Andric       break;
11940b57cec5SDimitry Andric 
11950b57cec5SDimitry Andric     // Scalar condition select on a vector is valid.
11960b57cec5SDimitry Andric     if (CondTy.isVector())
11970b57cec5SDimitry Andric       verifyVectorElementMatch(SelTy, CondTy, MI);
11980b57cec5SDimitry Andric     break;
11990b57cec5SDimitry Andric   }
12000b57cec5SDimitry Andric   case TargetOpcode::G_MERGE_VALUES: {
12010b57cec5SDimitry Andric     // G_MERGE_VALUES should only be used to merge scalars into a larger scalar,
12020b57cec5SDimitry Andric     // e.g. s2N = MERGE sN, sN
12030b57cec5SDimitry Andric     // Merging multiple scalars into a vector is not allowed, should use
12040b57cec5SDimitry Andric     // G_BUILD_VECTOR for that.
12050b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
12060b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
12070b57cec5SDimitry Andric     if (DstTy.isVector() || SrcTy.isVector())
12080b57cec5SDimitry Andric       report("G_MERGE_VALUES cannot operate on vectors", MI);
12090b57cec5SDimitry Andric 
12100b57cec5SDimitry Andric     const unsigned NumOps = MI->getNumOperands();
12110b57cec5SDimitry Andric     if (DstTy.getSizeInBits() != SrcTy.getSizeInBits() * (NumOps - 1))
12120b57cec5SDimitry Andric       report("G_MERGE_VALUES result size is inconsistent", MI);
12130b57cec5SDimitry Andric 
12140b57cec5SDimitry Andric     for (unsigned I = 2; I != NumOps; ++I) {
12150b57cec5SDimitry Andric       if (MRI->getType(MI->getOperand(I).getReg()) != SrcTy)
12160b57cec5SDimitry Andric         report("G_MERGE_VALUES source types do not match", MI);
12170b57cec5SDimitry Andric     }
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric     break;
12200b57cec5SDimitry Andric   }
12210b57cec5SDimitry Andric   case TargetOpcode::G_UNMERGE_VALUES: {
12220b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
12230b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(MI->getNumOperands()-1).getReg());
12240b57cec5SDimitry Andric     // For now G_UNMERGE can split vectors.
12250b57cec5SDimitry Andric     for (unsigned i = 0; i < MI->getNumOperands()-1; ++i) {
12260b57cec5SDimitry Andric       if (MRI->getType(MI->getOperand(i).getReg()) != DstTy)
12270b57cec5SDimitry Andric         report("G_UNMERGE_VALUES destination types do not match", MI);
12280b57cec5SDimitry Andric     }
12290b57cec5SDimitry Andric     if (SrcTy.getSizeInBits() !=
12300b57cec5SDimitry Andric         (DstTy.getSizeInBits() * (MI->getNumOperands() - 1))) {
12310b57cec5SDimitry Andric       report("G_UNMERGE_VALUES source operand does not cover dest operands",
12320b57cec5SDimitry Andric              MI);
12330b57cec5SDimitry Andric     }
12340b57cec5SDimitry Andric     break;
12350b57cec5SDimitry Andric   }
12360b57cec5SDimitry Andric   case TargetOpcode::G_BUILD_VECTOR: {
12370b57cec5SDimitry Andric     // Source types must be scalars, dest type a vector. Total size of scalars
12380b57cec5SDimitry Andric     // must match the dest vector size.
12390b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
12400b57cec5SDimitry Andric     LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
12410b57cec5SDimitry Andric     if (!DstTy.isVector() || SrcEltTy.isVector()) {
12420b57cec5SDimitry Andric       report("G_BUILD_VECTOR must produce a vector from scalar operands", MI);
12430b57cec5SDimitry Andric       break;
12440b57cec5SDimitry Andric     }
12450b57cec5SDimitry Andric 
12460b57cec5SDimitry Andric     if (DstTy.getElementType() != SrcEltTy)
12470b57cec5SDimitry Andric       report("G_BUILD_VECTOR result element type must match source type", MI);
12480b57cec5SDimitry Andric 
12490b57cec5SDimitry Andric     if (DstTy.getNumElements() != MI->getNumOperands() - 1)
12500b57cec5SDimitry Andric       report("G_BUILD_VECTOR must have an operand for each elemement", MI);
12510b57cec5SDimitry Andric 
12520b57cec5SDimitry Andric     for (unsigned i = 2; i < MI->getNumOperands(); ++i) {
12530b57cec5SDimitry Andric       if (MRI->getType(MI->getOperand(1).getReg()) !=
12540b57cec5SDimitry Andric           MRI->getType(MI->getOperand(i).getReg()))
12550b57cec5SDimitry Andric         report("G_BUILD_VECTOR source operand types are not homogeneous", MI);
12560b57cec5SDimitry Andric     }
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric     break;
12590b57cec5SDimitry Andric   }
12600b57cec5SDimitry Andric   case TargetOpcode::G_BUILD_VECTOR_TRUNC: {
12610b57cec5SDimitry Andric     // Source types must be scalars, dest type a vector. Scalar types must be
12620b57cec5SDimitry Andric     // larger than the dest vector elt type, as this is a truncating operation.
12630b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
12640b57cec5SDimitry Andric     LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg());
12650b57cec5SDimitry Andric     if (!DstTy.isVector() || SrcEltTy.isVector())
12660b57cec5SDimitry Andric       report("G_BUILD_VECTOR_TRUNC must produce a vector from scalar operands",
12670b57cec5SDimitry Andric              MI);
12680b57cec5SDimitry Andric     for (unsigned i = 2; i < MI->getNumOperands(); ++i) {
12690b57cec5SDimitry Andric       if (MRI->getType(MI->getOperand(1).getReg()) !=
12700b57cec5SDimitry Andric           MRI->getType(MI->getOperand(i).getReg()))
12710b57cec5SDimitry Andric         report("G_BUILD_VECTOR_TRUNC source operand types are not homogeneous",
12720b57cec5SDimitry Andric                MI);
12730b57cec5SDimitry Andric     }
12740b57cec5SDimitry Andric     if (SrcEltTy.getSizeInBits() <= DstTy.getElementType().getSizeInBits())
12750b57cec5SDimitry Andric       report("G_BUILD_VECTOR_TRUNC source operand types are not larger than "
12760b57cec5SDimitry Andric              "dest elt type",
12770b57cec5SDimitry Andric              MI);
12780b57cec5SDimitry Andric     break;
12790b57cec5SDimitry Andric   }
12800b57cec5SDimitry Andric   case TargetOpcode::G_CONCAT_VECTORS: {
12810b57cec5SDimitry Andric     // Source types should be vectors, and total size should match the dest
12820b57cec5SDimitry Andric     // vector size.
12830b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
12840b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
12850b57cec5SDimitry Andric     if (!DstTy.isVector() || !SrcTy.isVector())
12860b57cec5SDimitry Andric       report("G_CONCAT_VECTOR requires vector source and destination operands",
12870b57cec5SDimitry Andric              MI);
1288*5f7ddb14SDimitry Andric 
1289*5f7ddb14SDimitry Andric     if (MI->getNumOperands() < 3)
1290*5f7ddb14SDimitry Andric       report("G_CONCAT_VECTOR requires at least 2 source operands", MI);
1291*5f7ddb14SDimitry Andric 
12920b57cec5SDimitry Andric     for (unsigned i = 2; i < MI->getNumOperands(); ++i) {
12930b57cec5SDimitry Andric       if (MRI->getType(MI->getOperand(1).getReg()) !=
12940b57cec5SDimitry Andric           MRI->getType(MI->getOperand(i).getReg()))
12950b57cec5SDimitry Andric         report("G_CONCAT_VECTOR source operand types are not homogeneous", MI);
12960b57cec5SDimitry Andric     }
12970b57cec5SDimitry Andric     if (DstTy.getNumElements() !=
12980b57cec5SDimitry Andric         SrcTy.getNumElements() * (MI->getNumOperands() - 1))
12990b57cec5SDimitry Andric       report("G_CONCAT_VECTOR num dest and source elements should match", MI);
13000b57cec5SDimitry Andric     break;
13010b57cec5SDimitry Andric   }
13020b57cec5SDimitry Andric   case TargetOpcode::G_ICMP:
13030b57cec5SDimitry Andric   case TargetOpcode::G_FCMP: {
13040b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
13050b57cec5SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(2).getReg());
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric     if ((DstTy.isVector() != SrcTy.isVector()) ||
13080b57cec5SDimitry Andric         (DstTy.isVector() && DstTy.getNumElements() != SrcTy.getNumElements()))
13090b57cec5SDimitry Andric       report("Generic vector icmp/fcmp must preserve number of lanes", MI);
13100b57cec5SDimitry Andric 
13110b57cec5SDimitry Andric     break;
13120b57cec5SDimitry Andric   }
13130b57cec5SDimitry Andric   case TargetOpcode::G_EXTRACT: {
13140b57cec5SDimitry Andric     const MachineOperand &SrcOp = MI->getOperand(1);
13150b57cec5SDimitry Andric     if (!SrcOp.isReg()) {
13160b57cec5SDimitry Andric       report("extract source must be a register", MI);
13170b57cec5SDimitry Andric       break;
13180b57cec5SDimitry Andric     }
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric     const MachineOperand &OffsetOp = MI->getOperand(2);
13210b57cec5SDimitry Andric     if (!OffsetOp.isImm()) {
13220b57cec5SDimitry Andric       report("extract offset must be a constant", MI);
13230b57cec5SDimitry Andric       break;
13240b57cec5SDimitry Andric     }
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric     unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
13270b57cec5SDimitry Andric     unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
13280b57cec5SDimitry Andric     if (SrcSize == DstSize)
13290b57cec5SDimitry Andric       report("extract source must be larger than result", MI);
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric     if (DstSize + OffsetOp.getImm() > SrcSize)
13320b57cec5SDimitry Andric       report("extract reads past end of register", MI);
13330b57cec5SDimitry Andric     break;
13340b57cec5SDimitry Andric   }
13350b57cec5SDimitry Andric   case TargetOpcode::G_INSERT: {
13360b57cec5SDimitry Andric     const MachineOperand &SrcOp = MI->getOperand(2);
13370b57cec5SDimitry Andric     if (!SrcOp.isReg()) {
13380b57cec5SDimitry Andric       report("insert source must be a register", MI);
13390b57cec5SDimitry Andric       break;
13400b57cec5SDimitry Andric     }
13410b57cec5SDimitry Andric 
13420b57cec5SDimitry Andric     const MachineOperand &OffsetOp = MI->getOperand(3);
13430b57cec5SDimitry Andric     if (!OffsetOp.isImm()) {
13440b57cec5SDimitry Andric       report("insert offset must be a constant", MI);
13450b57cec5SDimitry Andric       break;
13460b57cec5SDimitry Andric     }
13470b57cec5SDimitry Andric 
13480b57cec5SDimitry Andric     unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits();
13490b57cec5SDimitry Andric     unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits();
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric     if (DstSize <= SrcSize)
13520b57cec5SDimitry Andric       report("inserted size must be smaller than total register", MI);
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric     if (SrcSize + OffsetOp.getImm() > DstSize)
13550b57cec5SDimitry Andric       report("insert writes past end of register", MI);
13560b57cec5SDimitry Andric 
13570b57cec5SDimitry Andric     break;
13580b57cec5SDimitry Andric   }
13590b57cec5SDimitry Andric   case TargetOpcode::G_JUMP_TABLE: {
13600b57cec5SDimitry Andric     if (!MI->getOperand(1).isJTI())
13610b57cec5SDimitry Andric       report("G_JUMP_TABLE source operand must be a jump table index", MI);
13620b57cec5SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
13630b57cec5SDimitry Andric     if (!DstTy.isPointer())
13640b57cec5SDimitry Andric       report("G_JUMP_TABLE dest operand must have a pointer type", MI);
13650b57cec5SDimitry Andric     break;
13660b57cec5SDimitry Andric   }
13670b57cec5SDimitry Andric   case TargetOpcode::G_BRJT: {
13680b57cec5SDimitry Andric     if (!MRI->getType(MI->getOperand(0).getReg()).isPointer())
13690b57cec5SDimitry Andric       report("G_BRJT src operand 0 must be a pointer type", MI);
13700b57cec5SDimitry Andric 
13710b57cec5SDimitry Andric     if (!MI->getOperand(1).isJTI())
13720b57cec5SDimitry Andric       report("G_BRJT src operand 1 must be a jump table index", MI);
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric     const auto &IdxOp = MI->getOperand(2);
13750b57cec5SDimitry Andric     if (!IdxOp.isReg() || MRI->getType(IdxOp.getReg()).isPointer())
13760b57cec5SDimitry Andric       report("G_BRJT src operand 2 must be a scalar reg type", MI);
13770b57cec5SDimitry Andric     break;
13780b57cec5SDimitry Andric   }
13790b57cec5SDimitry Andric   case TargetOpcode::G_INTRINSIC:
13800b57cec5SDimitry Andric   case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: {
13810b57cec5SDimitry Andric     // TODO: Should verify number of def and use operands, but the current
13820b57cec5SDimitry Andric     // interface requires passing in IR types for mangling.
13830b57cec5SDimitry Andric     const MachineOperand &IntrIDOp = MI->getOperand(MI->getNumExplicitDefs());
13840b57cec5SDimitry Andric     if (!IntrIDOp.isIntrinsicID()) {
13850b57cec5SDimitry Andric       report("G_INTRINSIC first src operand must be an intrinsic ID", MI);
13860b57cec5SDimitry Andric       break;
13870b57cec5SDimitry Andric     }
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric     bool NoSideEffects = MI->getOpcode() == TargetOpcode::G_INTRINSIC;
13900b57cec5SDimitry Andric     unsigned IntrID = IntrIDOp.getIntrinsicID();
13910b57cec5SDimitry Andric     if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) {
13920b57cec5SDimitry Andric       AttributeList Attrs
13930b57cec5SDimitry Andric         = Intrinsic::getAttributes(MF->getFunction().getContext(),
13940b57cec5SDimitry Andric                                    static_cast<Intrinsic::ID>(IntrID));
13950b57cec5SDimitry Andric       bool DeclHasSideEffects = !Attrs.hasFnAttribute(Attribute::ReadNone);
13960b57cec5SDimitry Andric       if (NoSideEffects && DeclHasSideEffects) {
13970b57cec5SDimitry Andric         report("G_INTRINSIC used with intrinsic that accesses memory", MI);
13980b57cec5SDimitry Andric         break;
13990b57cec5SDimitry Andric       }
14000b57cec5SDimitry Andric       if (!NoSideEffects && !DeclHasSideEffects) {
14010b57cec5SDimitry Andric         report("G_INTRINSIC_W_SIDE_EFFECTS used with readnone intrinsic", MI);
14020b57cec5SDimitry Andric         break;
14030b57cec5SDimitry Andric       }
14040b57cec5SDimitry Andric     }
1405af732203SDimitry Andric 
14068bcb0991SDimitry Andric     break;
14078bcb0991SDimitry Andric   }
14088bcb0991SDimitry Andric   case TargetOpcode::G_SEXT_INREG: {
14098bcb0991SDimitry Andric     if (!MI->getOperand(2).isImm()) {
14108bcb0991SDimitry Andric       report("G_SEXT_INREG expects an immediate operand #2", MI);
14118bcb0991SDimitry Andric       break;
14128bcb0991SDimitry Andric     }
14130b57cec5SDimitry Andric 
14148bcb0991SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
14158bcb0991SDimitry Andric     int64_t Imm = MI->getOperand(2).getImm();
14168bcb0991SDimitry Andric     if (Imm <= 0)
14178bcb0991SDimitry Andric       report("G_SEXT_INREG size must be >= 1", MI);
14188bcb0991SDimitry Andric     if (Imm >= SrcTy.getScalarSizeInBits())
14198bcb0991SDimitry Andric       report("G_SEXT_INREG size must be less than source bit width", MI);
14208bcb0991SDimitry Andric     break;
14218bcb0991SDimitry Andric   }
14228bcb0991SDimitry Andric   case TargetOpcode::G_SHUFFLE_VECTOR: {
14238bcb0991SDimitry Andric     const MachineOperand &MaskOp = MI->getOperand(3);
14248bcb0991SDimitry Andric     if (!MaskOp.isShuffleMask()) {
14258bcb0991SDimitry Andric       report("Incorrect mask operand type for G_SHUFFLE_VECTOR", MI);
14268bcb0991SDimitry Andric       break;
14278bcb0991SDimitry Andric     }
14288bcb0991SDimitry Andric 
14298bcb0991SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
14308bcb0991SDimitry Andric     LLT Src0Ty = MRI->getType(MI->getOperand(1).getReg());
14318bcb0991SDimitry Andric     LLT Src1Ty = MRI->getType(MI->getOperand(2).getReg());
14328bcb0991SDimitry Andric 
14338bcb0991SDimitry Andric     if (Src0Ty != Src1Ty)
14348bcb0991SDimitry Andric       report("Source operands must be the same type", MI);
14358bcb0991SDimitry Andric 
14368bcb0991SDimitry Andric     if (Src0Ty.getScalarType() != DstTy.getScalarType())
14378bcb0991SDimitry Andric       report("G_SHUFFLE_VECTOR cannot change element type", MI);
14388bcb0991SDimitry Andric 
14398bcb0991SDimitry Andric     // Don't check that all operands are vector because scalars are used in
14408bcb0991SDimitry Andric     // place of 1 element vectors.
14418bcb0991SDimitry Andric     int SrcNumElts = Src0Ty.isVector() ? Src0Ty.getNumElements() : 1;
14428bcb0991SDimitry Andric     int DstNumElts = DstTy.isVector() ? DstTy.getNumElements() : 1;
14438bcb0991SDimitry Andric 
1444480093f4SDimitry Andric     ArrayRef<int> MaskIdxes = MaskOp.getShuffleMask();
14458bcb0991SDimitry Andric 
14468bcb0991SDimitry Andric     if (static_cast<int>(MaskIdxes.size()) != DstNumElts)
14478bcb0991SDimitry Andric       report("Wrong result type for shufflemask", MI);
14488bcb0991SDimitry Andric 
14498bcb0991SDimitry Andric     for (int Idx : MaskIdxes) {
14508bcb0991SDimitry Andric       if (Idx < 0)
14518bcb0991SDimitry Andric         continue;
14528bcb0991SDimitry Andric 
14538bcb0991SDimitry Andric       if (Idx >= 2 * SrcNumElts)
14548bcb0991SDimitry Andric         report("Out of bounds shuffle index", MI);
14558bcb0991SDimitry Andric     }
14568bcb0991SDimitry Andric 
14578bcb0991SDimitry Andric     break;
14588bcb0991SDimitry Andric   }
14598bcb0991SDimitry Andric   case TargetOpcode::G_DYN_STACKALLOC: {
14608bcb0991SDimitry Andric     const MachineOperand &DstOp = MI->getOperand(0);
14618bcb0991SDimitry Andric     const MachineOperand &AllocOp = MI->getOperand(1);
14628bcb0991SDimitry Andric     const MachineOperand &AlignOp = MI->getOperand(2);
14638bcb0991SDimitry Andric 
14648bcb0991SDimitry Andric     if (!DstOp.isReg() || !MRI->getType(DstOp.getReg()).isPointer()) {
14658bcb0991SDimitry Andric       report("dst operand 0 must be a pointer type", MI);
14668bcb0991SDimitry Andric       break;
14678bcb0991SDimitry Andric     }
14688bcb0991SDimitry Andric 
14698bcb0991SDimitry Andric     if (!AllocOp.isReg() || !MRI->getType(AllocOp.getReg()).isScalar()) {
14708bcb0991SDimitry Andric       report("src operand 1 must be a scalar reg type", MI);
14718bcb0991SDimitry Andric       break;
14728bcb0991SDimitry Andric     }
14738bcb0991SDimitry Andric 
14748bcb0991SDimitry Andric     if (!AlignOp.isImm()) {
14758bcb0991SDimitry Andric       report("src operand 2 must be an immediate type", MI);
14768bcb0991SDimitry Andric       break;
14778bcb0991SDimitry Andric     }
14780b57cec5SDimitry Andric     break;
14790b57cec5SDimitry Andric   }
1480*5f7ddb14SDimitry Andric   case TargetOpcode::G_MEMCPY_INLINE:
1481af732203SDimitry Andric   case TargetOpcode::G_MEMCPY:
1482af732203SDimitry Andric   case TargetOpcode::G_MEMMOVE: {
1483af732203SDimitry Andric     ArrayRef<MachineMemOperand *> MMOs = MI->memoperands();
1484af732203SDimitry Andric     if (MMOs.size() != 2) {
1485af732203SDimitry Andric       report("memcpy/memmove must have 2 memory operands", MI);
1486af732203SDimitry Andric       break;
1487af732203SDimitry Andric     }
1488af732203SDimitry Andric 
1489af732203SDimitry Andric     if ((!MMOs[0]->isStore() || MMOs[0]->isLoad()) ||
1490af732203SDimitry Andric         (MMOs[1]->isStore() || !MMOs[1]->isLoad())) {
1491af732203SDimitry Andric       report("wrong memory operand types", MI);
1492af732203SDimitry Andric       break;
1493af732203SDimitry Andric     }
1494af732203SDimitry Andric 
1495af732203SDimitry Andric     if (MMOs[0]->getSize() != MMOs[1]->getSize())
1496af732203SDimitry Andric       report("inconsistent memory operand sizes", MI);
1497af732203SDimitry Andric 
1498af732203SDimitry Andric     LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg());
1499af732203SDimitry Andric     LLT SrcPtrTy = MRI->getType(MI->getOperand(1).getReg());
1500af732203SDimitry Andric 
1501af732203SDimitry Andric     if (!DstPtrTy.isPointer() || !SrcPtrTy.isPointer()) {
1502af732203SDimitry Andric       report("memory instruction operand must be a pointer", MI);
1503af732203SDimitry Andric       break;
1504af732203SDimitry Andric     }
1505af732203SDimitry Andric 
1506af732203SDimitry Andric     if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace())
1507af732203SDimitry Andric       report("inconsistent store address space", MI);
1508af732203SDimitry Andric     if (SrcPtrTy.getAddressSpace() != MMOs[1]->getAddrSpace())
1509af732203SDimitry Andric       report("inconsistent load address space", MI);
1510af732203SDimitry Andric 
1511*5f7ddb14SDimitry Andric     if (Opc != TargetOpcode::G_MEMCPY_INLINE)
1512*5f7ddb14SDimitry Andric       if (!MI->getOperand(3).isImm() || (MI->getOperand(3).getImm() & ~1LL))
1513*5f7ddb14SDimitry Andric         report("'tail' flag (operand 3) must be an immediate 0 or 1", MI);
1514*5f7ddb14SDimitry Andric 
1515af732203SDimitry Andric     break;
1516af732203SDimitry Andric   }
1517*5f7ddb14SDimitry Andric   case TargetOpcode::G_BZERO:
1518af732203SDimitry Andric   case TargetOpcode::G_MEMSET: {
1519af732203SDimitry Andric     ArrayRef<MachineMemOperand *> MMOs = MI->memoperands();
1520*5f7ddb14SDimitry Andric     std::string Name = Opc == TargetOpcode::G_MEMSET ? "memset" : "bzero";
1521af732203SDimitry Andric     if (MMOs.size() != 1) {
1522*5f7ddb14SDimitry Andric       report(Twine(Name, " must have 1 memory operand"), MI);
1523af732203SDimitry Andric       break;
1524af732203SDimitry Andric     }
1525af732203SDimitry Andric 
1526af732203SDimitry Andric     if ((!MMOs[0]->isStore() || MMOs[0]->isLoad())) {
1527*5f7ddb14SDimitry Andric       report(Twine(Name, " memory operand must be a store"), MI);
1528af732203SDimitry Andric       break;
1529af732203SDimitry Andric     }
1530af732203SDimitry Andric 
1531af732203SDimitry Andric     LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg());
1532af732203SDimitry Andric     if (!DstPtrTy.isPointer()) {
1533*5f7ddb14SDimitry Andric       report(Twine(Name, " operand must be a pointer"), MI);
1534af732203SDimitry Andric       break;
1535af732203SDimitry Andric     }
1536af732203SDimitry Andric 
1537af732203SDimitry Andric     if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace())
1538*5f7ddb14SDimitry Andric       report("inconsistent " + Twine(Name, " address space"), MI);
1539*5f7ddb14SDimitry Andric 
1540*5f7ddb14SDimitry Andric     if (!MI->getOperand(MI->getNumOperands() - 1).isImm() ||
1541*5f7ddb14SDimitry Andric         (MI->getOperand(MI->getNumOperands() - 1).getImm() & ~1LL))
1542*5f7ddb14SDimitry Andric       report("'tail' flag (last operand) must be an immediate 0 or 1", MI);
1543af732203SDimitry Andric 
1544af732203SDimitry Andric     break;
1545af732203SDimitry Andric   }
1546af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_SEQ_FADD:
1547af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_SEQ_FMUL: {
1548af732203SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1549af732203SDimitry Andric     LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg());
1550af732203SDimitry Andric     LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg());
1551af732203SDimitry Andric     if (!DstTy.isScalar())
1552af732203SDimitry Andric       report("Vector reduction requires a scalar destination type", MI);
1553af732203SDimitry Andric     if (!Src1Ty.isScalar())
1554af732203SDimitry Andric       report("Sequential FADD/FMUL vector reduction requires a scalar 1st operand", MI);
1555af732203SDimitry Andric     if (!Src2Ty.isVector())
1556af732203SDimitry Andric       report("Sequential FADD/FMUL vector reduction must have a vector 2nd operand", MI);
1557af732203SDimitry Andric     break;
1558af732203SDimitry Andric   }
1559af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_FADD:
1560af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_FMUL:
1561af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_FMAX:
1562af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_FMIN:
1563af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_ADD:
1564af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_MUL:
1565af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_AND:
1566af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_OR:
1567af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_XOR:
1568af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_SMAX:
1569af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_SMIN:
1570af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_UMAX:
1571af732203SDimitry Andric   case TargetOpcode::G_VECREDUCE_UMIN: {
1572af732203SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1573af732203SDimitry Andric     LLT SrcTy = MRI->getType(MI->getOperand(1).getReg());
1574af732203SDimitry Andric     if (!DstTy.isScalar())
1575af732203SDimitry Andric       report("Vector reduction requires a scalar destination type", MI);
1576af732203SDimitry Andric     if (!SrcTy.isVector())
1577af732203SDimitry Andric       report("Vector reduction requires vector source=", MI);
1578af732203SDimitry Andric     break;
1579af732203SDimitry Andric   }
1580*5f7ddb14SDimitry Andric 
1581*5f7ddb14SDimitry Andric   case TargetOpcode::G_SBFX:
1582*5f7ddb14SDimitry Andric   case TargetOpcode::G_UBFX: {
1583*5f7ddb14SDimitry Andric     LLT DstTy = MRI->getType(MI->getOperand(0).getReg());
1584*5f7ddb14SDimitry Andric     if (DstTy.isVector()) {
1585*5f7ddb14SDimitry Andric       report("Bitfield extraction is not supported on vectors", MI);
1586*5f7ddb14SDimitry Andric       break;
1587*5f7ddb14SDimitry Andric     }
1588*5f7ddb14SDimitry Andric     break;
1589*5f7ddb14SDimitry Andric   }
1590*5f7ddb14SDimitry Andric   case TargetOpcode::G_ROTR:
1591*5f7ddb14SDimitry Andric   case TargetOpcode::G_ROTL: {
1592*5f7ddb14SDimitry Andric     LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg());
1593*5f7ddb14SDimitry Andric     LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg());
1594*5f7ddb14SDimitry Andric     if (Src1Ty.isVector() != Src2Ty.isVector()) {
1595*5f7ddb14SDimitry Andric       report("Rotate requires operands to be either all scalars or all vectors",
1596*5f7ddb14SDimitry Andric              MI);
1597*5f7ddb14SDimitry Andric       break;
1598*5f7ddb14SDimitry Andric     }
1599*5f7ddb14SDimitry Andric     break;
1600*5f7ddb14SDimitry Andric   }
1601*5f7ddb14SDimitry Andric 
16020b57cec5SDimitry Andric   default:
16030b57cec5SDimitry Andric     break;
16040b57cec5SDimitry Andric   }
16050b57cec5SDimitry Andric }
16060b57cec5SDimitry Andric 
visitMachineInstrBefore(const MachineInstr * MI)16070b57cec5SDimitry Andric void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
16080b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI->getDesc();
16090b57cec5SDimitry Andric   if (MI->getNumOperands() < MCID.getNumOperands()) {
16100b57cec5SDimitry Andric     report("Too few operands", MI);
16110b57cec5SDimitry Andric     errs() << MCID.getNumOperands() << " operands expected, but "
16120b57cec5SDimitry Andric            << MI->getNumOperands() << " given.\n";
16130b57cec5SDimitry Andric   }
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric   if (MI->isPHI()) {
16160b57cec5SDimitry Andric     if (MF->getProperties().hasProperty(
16170b57cec5SDimitry Andric             MachineFunctionProperties::Property::NoPHIs))
16180b57cec5SDimitry Andric       report("Found PHI instruction with NoPHIs property set", MI);
16190b57cec5SDimitry Andric 
16200b57cec5SDimitry Andric     if (FirstNonPHI)
16210b57cec5SDimitry Andric       report("Found PHI instruction after non-PHI", MI);
16220b57cec5SDimitry Andric   } else if (FirstNonPHI == nullptr)
16230b57cec5SDimitry Andric     FirstNonPHI = MI;
16240b57cec5SDimitry Andric 
16250b57cec5SDimitry Andric   // Check the tied operands.
16260b57cec5SDimitry Andric   if (MI->isInlineAsm())
16270b57cec5SDimitry Andric     verifyInlineAsm(MI);
16280b57cec5SDimitry Andric 
1629af732203SDimitry Andric   // Check that unspillable terminators define a reg and have at most one use.
1630af732203SDimitry Andric   if (TII->isUnspillableTerminator(MI)) {
1631af732203SDimitry Andric     if (!MI->getOperand(0).isReg() || !MI->getOperand(0).isDef())
1632af732203SDimitry Andric       report("Unspillable Terminator does not define a reg", MI);
1633af732203SDimitry Andric     Register Def = MI->getOperand(0).getReg();
1634af732203SDimitry Andric     if (Def.isVirtual() &&
1635af732203SDimitry Andric         std::distance(MRI->use_nodbg_begin(Def), MRI->use_nodbg_end()) > 1)
1636af732203SDimitry Andric       report("Unspillable Terminator expected to have at most one use!", MI);
1637af732203SDimitry Andric   }
1638af732203SDimitry Andric 
16395ffd83dbSDimitry Andric   // A fully-formed DBG_VALUE must have a location. Ignore partially formed
16405ffd83dbSDimitry Andric   // DBG_VALUEs: these are convenient to use in tests, but should never get
16415ffd83dbSDimitry Andric   // generated.
16425ffd83dbSDimitry Andric   if (MI->isDebugValue() && MI->getNumOperands() == 4)
16435ffd83dbSDimitry Andric     if (!MI->getDebugLoc())
16445ffd83dbSDimitry Andric       report("Missing DebugLoc for debug instruction", MI);
16455ffd83dbSDimitry Andric 
1646af732203SDimitry Andric   // Meta instructions should never be the subject of debug value tracking,
1647af732203SDimitry Andric   // they don't create a value in the output program at all.
1648af732203SDimitry Andric   if (MI->isMetaInstruction() && MI->peekDebugInstrNum())
1649af732203SDimitry Andric     report("Metadata instruction should not have a value tracking number", MI);
1650af732203SDimitry Andric 
16510b57cec5SDimitry Andric   // Check the MachineMemOperands for basic consistency.
16525ffd83dbSDimitry Andric   for (MachineMemOperand *Op : MI->memoperands()) {
16535ffd83dbSDimitry Andric     if (Op->isLoad() && !MI->mayLoad())
16540b57cec5SDimitry Andric       report("Missing mayLoad flag", MI);
16555ffd83dbSDimitry Andric     if (Op->isStore() && !MI->mayStore())
16560b57cec5SDimitry Andric       report("Missing mayStore flag", MI);
16570b57cec5SDimitry Andric   }
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric   // Debug values must not have a slot index.
16600b57cec5SDimitry Andric   // Other instructions must have one, unless they are inside a bundle.
16610b57cec5SDimitry Andric   if (LiveInts) {
16620b57cec5SDimitry Andric     bool mapped = !LiveInts->isNotInMIMap(*MI);
1663*5f7ddb14SDimitry Andric     if (MI->isDebugOrPseudoInstr()) {
16640b57cec5SDimitry Andric       if (mapped)
16650b57cec5SDimitry Andric         report("Debug instruction has a slot index", MI);
16660b57cec5SDimitry Andric     } else if (MI->isInsideBundle()) {
16670b57cec5SDimitry Andric       if (mapped)
16680b57cec5SDimitry Andric         report("Instruction inside bundle has a slot index", MI);
16690b57cec5SDimitry Andric     } else {
16700b57cec5SDimitry Andric       if (!mapped)
16710b57cec5SDimitry Andric         report("Missing slot index", MI);
16720b57cec5SDimitry Andric     }
16730b57cec5SDimitry Andric   }
16740b57cec5SDimitry Andric 
1675*5f7ddb14SDimitry Andric   unsigned Opc = MCID.getOpcode();
1676*5f7ddb14SDimitry Andric   if (isPreISelGenericOpcode(Opc) || isPreISelGenericOptimizationHint(Opc)) {
16770b57cec5SDimitry Andric     verifyPreISelGenericInstruction(MI);
16780b57cec5SDimitry Andric     return;
16790b57cec5SDimitry Andric   }
16800b57cec5SDimitry Andric 
16810b57cec5SDimitry Andric   StringRef ErrorInfo;
16820b57cec5SDimitry Andric   if (!TII->verifyInstruction(*MI, ErrorInfo))
16830b57cec5SDimitry Andric     report(ErrorInfo.data(), MI);
16840b57cec5SDimitry Andric 
16850b57cec5SDimitry Andric   // Verify properties of various specific instruction types
16860b57cec5SDimitry Andric   switch (MI->getOpcode()) {
16870b57cec5SDimitry Andric   case TargetOpcode::COPY: {
16880b57cec5SDimitry Andric     const MachineOperand &DstOp = MI->getOperand(0);
16890b57cec5SDimitry Andric     const MachineOperand &SrcOp = MI->getOperand(1);
1690*5f7ddb14SDimitry Andric     const Register SrcReg = SrcOp.getReg();
1691*5f7ddb14SDimitry Andric     const Register DstReg = DstOp.getReg();
1692*5f7ddb14SDimitry Andric 
1693*5f7ddb14SDimitry Andric     LLT DstTy = MRI->getType(DstReg);
1694*5f7ddb14SDimitry Andric     LLT SrcTy = MRI->getType(SrcReg);
16950b57cec5SDimitry Andric     if (SrcTy.isValid() && DstTy.isValid()) {
16960b57cec5SDimitry Andric       // If both types are valid, check that the types are the same.
16970b57cec5SDimitry Andric       if (SrcTy != DstTy) {
16980b57cec5SDimitry Andric         report("Copy Instruction is illegal with mismatching types", MI);
16990b57cec5SDimitry Andric         errs() << "Def = " << DstTy << ", Src = " << SrcTy << "\n";
17000b57cec5SDimitry Andric       }
1701*5f7ddb14SDimitry Andric 
1702*5f7ddb14SDimitry Andric       break;
17030b57cec5SDimitry Andric     }
1704*5f7ddb14SDimitry Andric 
1705*5f7ddb14SDimitry Andric     if (!SrcTy.isValid() && !DstTy.isValid())
1706*5f7ddb14SDimitry Andric       break;
1707*5f7ddb14SDimitry Andric 
1708*5f7ddb14SDimitry Andric     // If we have only one valid type, this is likely a copy between a virtual
1709*5f7ddb14SDimitry Andric     // and physical register.
1710*5f7ddb14SDimitry Andric     unsigned SrcSize = 0;
1711*5f7ddb14SDimitry Andric     unsigned DstSize = 0;
1712*5f7ddb14SDimitry Andric     if (SrcReg.isPhysical() && DstTy.isValid()) {
1713*5f7ddb14SDimitry Andric       const TargetRegisterClass *SrcRC =
1714*5f7ddb14SDimitry Andric           TRI->getMinimalPhysRegClassLLT(SrcReg, DstTy);
1715*5f7ddb14SDimitry Andric       if (SrcRC)
1716*5f7ddb14SDimitry Andric         SrcSize = TRI->getRegSizeInBits(*SrcRC);
1717*5f7ddb14SDimitry Andric     }
1718*5f7ddb14SDimitry Andric 
1719*5f7ddb14SDimitry Andric     if (SrcSize == 0)
1720*5f7ddb14SDimitry Andric       SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI);
1721*5f7ddb14SDimitry Andric 
1722*5f7ddb14SDimitry Andric     if (DstReg.isPhysical() && SrcTy.isValid()) {
1723*5f7ddb14SDimitry Andric       const TargetRegisterClass *DstRC =
1724*5f7ddb14SDimitry Andric           TRI->getMinimalPhysRegClassLLT(DstReg, SrcTy);
1725*5f7ddb14SDimitry Andric       if (DstRC)
1726*5f7ddb14SDimitry Andric         DstSize = TRI->getRegSizeInBits(*DstRC);
1727*5f7ddb14SDimitry Andric     }
1728*5f7ddb14SDimitry Andric 
1729*5f7ddb14SDimitry Andric     if (DstSize == 0)
1730*5f7ddb14SDimitry Andric       DstSize = TRI->getRegSizeInBits(DstReg, *MRI);
1731*5f7ddb14SDimitry Andric 
1732*5f7ddb14SDimitry Andric     if (SrcSize != 0 && DstSize != 0 && SrcSize != DstSize) {
17330b57cec5SDimitry Andric       if (!DstOp.getSubReg() && !SrcOp.getSubReg()) {
17340b57cec5SDimitry Andric         report("Copy Instruction is illegal with mismatching sizes", MI);
17350b57cec5SDimitry Andric         errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize
17360b57cec5SDimitry Andric                << "\n";
17370b57cec5SDimitry Andric       }
17380b57cec5SDimitry Andric     }
17390b57cec5SDimitry Andric     break;
17400b57cec5SDimitry Andric   }
17415ffd83dbSDimitry Andric   case TargetOpcode::STATEPOINT: {
17425ffd83dbSDimitry Andric     StatepointOpers SO(MI);
17435ffd83dbSDimitry Andric     if (!MI->getOperand(SO.getIDPos()).isImm() ||
17445ffd83dbSDimitry Andric         !MI->getOperand(SO.getNBytesPos()).isImm() ||
17455ffd83dbSDimitry Andric         !MI->getOperand(SO.getNCallArgsPos()).isImm()) {
17460b57cec5SDimitry Andric       report("meta operands to STATEPOINT not constant!", MI);
17470b57cec5SDimitry Andric       break;
17485ffd83dbSDimitry Andric     }
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric     auto VerifyStackMapConstant = [&](unsigned Offset) {
1751af732203SDimitry Andric       if (Offset >= MI->getNumOperands()) {
1752af732203SDimitry Andric         report("stack map constant to STATEPOINT is out of range!", MI);
1753af732203SDimitry Andric         return;
1754af732203SDimitry Andric       }
17555ffd83dbSDimitry Andric       if (!MI->getOperand(Offset - 1).isImm() ||
17565ffd83dbSDimitry Andric           MI->getOperand(Offset - 1).getImm() != StackMaps::ConstantOp ||
17575ffd83dbSDimitry Andric           !MI->getOperand(Offset).isImm())
17580b57cec5SDimitry Andric         report("stack map constant to STATEPOINT not well formed!", MI);
17590b57cec5SDimitry Andric     };
17605ffd83dbSDimitry Andric     VerifyStackMapConstant(SO.getCCIdx());
17615ffd83dbSDimitry Andric     VerifyStackMapConstant(SO.getFlagsIdx());
17625ffd83dbSDimitry Andric     VerifyStackMapConstant(SO.getNumDeoptArgsIdx());
1763af732203SDimitry Andric     VerifyStackMapConstant(SO.getNumGCPtrIdx());
1764af732203SDimitry Andric     VerifyStackMapConstant(SO.getNumAllocaIdx());
1765af732203SDimitry Andric     VerifyStackMapConstant(SO.getNumGcMapEntriesIdx());
1766af732203SDimitry Andric 
1767af732203SDimitry Andric     // Verify that all explicit statepoint defs are tied to gc operands as
1768af732203SDimitry Andric     // they are expected to be a relocation of gc operands.
1769af732203SDimitry Andric     unsigned FirstGCPtrIdx = SO.getFirstGCPtrIdx();
1770af732203SDimitry Andric     unsigned LastGCPtrIdx = SO.getNumAllocaIdx() - 2;
1771af732203SDimitry Andric     for (unsigned Idx = 0; Idx < MI->getNumDefs(); Idx++) {
1772af732203SDimitry Andric       unsigned UseOpIdx;
1773af732203SDimitry Andric       if (!MI->isRegTiedToUseOperand(Idx, &UseOpIdx)) {
1774af732203SDimitry Andric         report("STATEPOINT defs expected to be tied", MI);
1775af732203SDimitry Andric         break;
1776af732203SDimitry Andric       }
1777af732203SDimitry Andric       if (UseOpIdx < FirstGCPtrIdx || UseOpIdx > LastGCPtrIdx) {
1778af732203SDimitry Andric         report("STATEPOINT def tied to non-gc operand", MI);
1779af732203SDimitry Andric         break;
1780af732203SDimitry Andric       }
1781af732203SDimitry Andric     }
17820b57cec5SDimitry Andric 
17830b57cec5SDimitry Andric     // TODO: verify we have properly encoded deopt arguments
17845ffd83dbSDimitry Andric   } break;
1785*5f7ddb14SDimitry Andric   case TargetOpcode::INSERT_SUBREG: {
1786*5f7ddb14SDimitry Andric     unsigned InsertedSize;
1787*5f7ddb14SDimitry Andric     if (unsigned SubIdx = MI->getOperand(2).getSubReg())
1788*5f7ddb14SDimitry Andric       InsertedSize = TRI->getSubRegIdxSize(SubIdx);
1789*5f7ddb14SDimitry Andric     else
1790*5f7ddb14SDimitry Andric       InsertedSize = TRI->getRegSizeInBits(MI->getOperand(2).getReg(), *MRI);
1791*5f7ddb14SDimitry Andric     unsigned SubRegSize = TRI->getSubRegIdxSize(MI->getOperand(3).getImm());
1792*5f7ddb14SDimitry Andric     if (SubRegSize < InsertedSize) {
1793*5f7ddb14SDimitry Andric       report("INSERT_SUBREG expected inserted value to have equal or lesser "
1794*5f7ddb14SDimitry Andric              "size than the subreg it was inserted into", MI);
1795*5f7ddb14SDimitry Andric       break;
1796*5f7ddb14SDimitry Andric     }
1797*5f7ddb14SDimitry Andric   } break;
17980b57cec5SDimitry Andric   }
17990b57cec5SDimitry Andric }
18000b57cec5SDimitry Andric 
18010b57cec5SDimitry Andric void
visitMachineOperand(const MachineOperand * MO,unsigned MONum)18020b57cec5SDimitry Andric MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
18030b57cec5SDimitry Andric   const MachineInstr *MI = MO->getParent();
18040b57cec5SDimitry Andric   const MCInstrDesc &MCID = MI->getDesc();
18050b57cec5SDimitry Andric   unsigned NumDefs = MCID.getNumDefs();
18060b57cec5SDimitry Andric   if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
18070b57cec5SDimitry Andric     NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
18080b57cec5SDimitry Andric 
18090b57cec5SDimitry Andric   // The first MCID.NumDefs operands must be explicit register defines
18100b57cec5SDimitry Andric   if (MONum < NumDefs) {
18110b57cec5SDimitry Andric     const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
18120b57cec5SDimitry Andric     if (!MO->isReg())
18130b57cec5SDimitry Andric       report("Explicit definition must be a register", MO, MONum);
18140b57cec5SDimitry Andric     else if (!MO->isDef() && !MCOI.isOptionalDef())
18150b57cec5SDimitry Andric       report("Explicit definition marked as use", MO, MONum);
18160b57cec5SDimitry Andric     else if (MO->isImplicit())
18170b57cec5SDimitry Andric       report("Explicit definition marked as implicit", MO, MONum);
18180b57cec5SDimitry Andric   } else if (MONum < MCID.getNumOperands()) {
18190b57cec5SDimitry Andric     const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
18200b57cec5SDimitry Andric     // Don't check if it's the last operand in a variadic instruction. See,
1821480093f4SDimitry Andric     // e.g., LDM_RET in the arm back end. Check non-variadic operands only.
1822480093f4SDimitry Andric     bool IsOptional = MI->isVariadic() && MONum == MCID.getNumOperands() - 1;
1823480093f4SDimitry Andric     if (!IsOptional) {
1824480093f4SDimitry Andric       if (MO->isReg()) {
18255ffd83dbSDimitry Andric         if (MO->isDef() && !MCOI.isOptionalDef() && !MCID.variadicOpsAreDefs())
18260b57cec5SDimitry Andric           report("Explicit operand marked as def", MO, MONum);
18270b57cec5SDimitry Andric         if (MO->isImplicit())
18280b57cec5SDimitry Andric           report("Explicit operand marked as implicit", MO, MONum);
18290b57cec5SDimitry Andric       }
18300b57cec5SDimitry Andric 
1831480093f4SDimitry Andric       // Check that an instruction has register operands only as expected.
1832480093f4SDimitry Andric       if (MCOI.OperandType == MCOI::OPERAND_REGISTER &&
1833480093f4SDimitry Andric           !MO->isReg() && !MO->isFI())
1834480093f4SDimitry Andric         report("Expected a register operand.", MO, MONum);
1835*5f7ddb14SDimitry Andric       if (MO->isReg()) {
1836*5f7ddb14SDimitry Andric         if (MCOI.OperandType == MCOI::OPERAND_IMMEDIATE ||
1837*5f7ddb14SDimitry Andric             (MCOI.OperandType == MCOI::OPERAND_PCREL &&
1838*5f7ddb14SDimitry Andric              !TII->isPCRelRegisterOperandLegal(*MO)))
1839480093f4SDimitry Andric           report("Expected a non-register operand.", MO, MONum);
1840480093f4SDimitry Andric       }
1841*5f7ddb14SDimitry Andric     }
1842480093f4SDimitry Andric 
18430b57cec5SDimitry Andric     int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
18440b57cec5SDimitry Andric     if (TiedTo != -1) {
18450b57cec5SDimitry Andric       if (!MO->isReg())
18460b57cec5SDimitry Andric         report("Tied use must be a register", MO, MONum);
18470b57cec5SDimitry Andric       else if (!MO->isTied())
18480b57cec5SDimitry Andric         report("Operand should be tied", MO, MONum);
18490b57cec5SDimitry Andric       else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
18500b57cec5SDimitry Andric         report("Tied def doesn't match MCInstrDesc", MO, MONum);
18518bcb0991SDimitry Andric       else if (Register::isPhysicalRegister(MO->getReg())) {
18520b57cec5SDimitry Andric         const MachineOperand &MOTied = MI->getOperand(TiedTo);
18530b57cec5SDimitry Andric         if (!MOTied.isReg())
18540b57cec5SDimitry Andric           report("Tied counterpart must be a register", &MOTied, TiedTo);
18558bcb0991SDimitry Andric         else if (Register::isPhysicalRegister(MOTied.getReg()) &&
18560b57cec5SDimitry Andric                  MO->getReg() != MOTied.getReg())
18570b57cec5SDimitry Andric           report("Tied physical registers must match.", &MOTied, TiedTo);
18580b57cec5SDimitry Andric       }
18590b57cec5SDimitry Andric     } else if (MO->isReg() && MO->isTied())
18600b57cec5SDimitry Andric       report("Explicit operand should not be tied", MO, MONum);
18610b57cec5SDimitry Andric   } else {
18620b57cec5SDimitry Andric     // ARM adds %reg0 operands to indicate predicates. We'll allow that.
18630b57cec5SDimitry Andric     if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
18640b57cec5SDimitry Andric       report("Extra explicit operand on non-variadic instruction", MO, MONum);
18650b57cec5SDimitry Andric   }
18660b57cec5SDimitry Andric 
18670b57cec5SDimitry Andric   switch (MO->getType()) {
18680b57cec5SDimitry Andric   case MachineOperand::MO_Register: {
18698bcb0991SDimitry Andric     const Register Reg = MO->getReg();
18700b57cec5SDimitry Andric     if (!Reg)
18710b57cec5SDimitry Andric       return;
18720b57cec5SDimitry Andric     if (MRI->tracksLiveness() && !MI->isDebugValue())
18730b57cec5SDimitry Andric       checkLiveness(MO, MONum);
18740b57cec5SDimitry Andric 
18750b57cec5SDimitry Andric     // Verify the consistency of tied operands.
18760b57cec5SDimitry Andric     if (MO->isTied()) {
18770b57cec5SDimitry Andric       unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
18780b57cec5SDimitry Andric       const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
18790b57cec5SDimitry Andric       if (!OtherMO.isReg())
18800b57cec5SDimitry Andric         report("Must be tied to a register", MO, MONum);
18810b57cec5SDimitry Andric       if (!OtherMO.isTied())
18820b57cec5SDimitry Andric         report("Missing tie flags on tied operand", MO, MONum);
18830b57cec5SDimitry Andric       if (MI->findTiedOperandIdx(OtherIdx) != MONum)
18840b57cec5SDimitry Andric         report("Inconsistent tie links", MO, MONum);
18850b57cec5SDimitry Andric       if (MONum < MCID.getNumDefs()) {
18860b57cec5SDimitry Andric         if (OtherIdx < MCID.getNumOperands()) {
18870b57cec5SDimitry Andric           if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
18880b57cec5SDimitry Andric             report("Explicit def tied to explicit use without tie constraint",
18890b57cec5SDimitry Andric                    MO, MONum);
18900b57cec5SDimitry Andric         } else {
18910b57cec5SDimitry Andric           if (!OtherMO.isImplicit())
18920b57cec5SDimitry Andric             report("Explicit def should be tied to implicit use", MO, MONum);
18930b57cec5SDimitry Andric         }
18940b57cec5SDimitry Andric       }
18950b57cec5SDimitry Andric     }
18960b57cec5SDimitry Andric 
18975ffd83dbSDimitry Andric     // Verify two-address constraints after the twoaddressinstruction pass.
18985ffd83dbSDimitry Andric     // Both twoaddressinstruction pass and phi-node-elimination pass call
18995ffd83dbSDimitry Andric     // MRI->leaveSSA() to set MF as NoSSA, we should do the verification after
19005ffd83dbSDimitry Andric     // twoaddressinstruction pass not after phi-node-elimination pass. So we
19015ffd83dbSDimitry Andric     // shouldn't use the NoSSA as the condition, we should based on
19025ffd83dbSDimitry Andric     // TiedOpsRewritten property to verify two-address constraints, this
19035ffd83dbSDimitry Andric     // property will be set in twoaddressinstruction pass.
19040b57cec5SDimitry Andric     unsigned DefIdx;
19055ffd83dbSDimitry Andric     if (MF->getProperties().hasProperty(
19065ffd83dbSDimitry Andric             MachineFunctionProperties::Property::TiedOpsRewritten) &&
19075ffd83dbSDimitry Andric         MO->isUse() && MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
19080b57cec5SDimitry Andric         Reg != MI->getOperand(DefIdx).getReg())
19090b57cec5SDimitry Andric       report("Two-address instruction operands must be identical", MO, MONum);
19100b57cec5SDimitry Andric 
19110b57cec5SDimitry Andric     // Check register classes.
19120b57cec5SDimitry Andric     unsigned SubIdx = MO->getSubReg();
19130b57cec5SDimitry Andric 
19148bcb0991SDimitry Andric     if (Register::isPhysicalRegister(Reg)) {
19150b57cec5SDimitry Andric       if (SubIdx) {
19160b57cec5SDimitry Andric         report("Illegal subregister index for physical register", MO, MONum);
19170b57cec5SDimitry Andric         return;
19180b57cec5SDimitry Andric       }
19190b57cec5SDimitry Andric       if (MONum < MCID.getNumOperands()) {
19200b57cec5SDimitry Andric         if (const TargetRegisterClass *DRC =
19210b57cec5SDimitry Andric               TII->getRegClass(MCID, MONum, TRI, *MF)) {
19220b57cec5SDimitry Andric           if (!DRC->contains(Reg)) {
19230b57cec5SDimitry Andric             report("Illegal physical register for instruction", MO, MONum);
19240b57cec5SDimitry Andric             errs() << printReg(Reg, TRI) << " is not a "
19250b57cec5SDimitry Andric                    << TRI->getRegClassName(DRC) << " register.\n";
19260b57cec5SDimitry Andric           }
19270b57cec5SDimitry Andric         }
19280b57cec5SDimitry Andric       }
19290b57cec5SDimitry Andric       if (MO->isRenamable()) {
19300b57cec5SDimitry Andric         if (MRI->isReserved(Reg)) {
19310b57cec5SDimitry Andric           report("isRenamable set on reserved register", MO, MONum);
19320b57cec5SDimitry Andric           return;
19330b57cec5SDimitry Andric         }
19340b57cec5SDimitry Andric       }
19350b57cec5SDimitry Andric       if (MI->isDebugValue() && MO->isUse() && !MO->isDebug()) {
19360b57cec5SDimitry Andric         report("Use-reg is not IsDebug in a DBG_VALUE", MO, MONum);
19370b57cec5SDimitry Andric         return;
19380b57cec5SDimitry Andric       }
19390b57cec5SDimitry Andric     } else {
19400b57cec5SDimitry Andric       // Virtual register.
19410b57cec5SDimitry Andric       const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
19420b57cec5SDimitry Andric       if (!RC) {
19430b57cec5SDimitry Andric         // This is a generic virtual register.
19440b57cec5SDimitry Andric 
19455ffd83dbSDimitry Andric         // Do not allow undef uses for generic virtual registers. This ensures
19465ffd83dbSDimitry Andric         // getVRegDef can never fail and return null on a generic register.
19475ffd83dbSDimitry Andric         //
19485ffd83dbSDimitry Andric         // FIXME: This restriction should probably be broadened to all SSA
19495ffd83dbSDimitry Andric         // MIR. However, DetectDeadLanes/ProcessImplicitDefs technically still
19505ffd83dbSDimitry Andric         // run on the SSA function just before phi elimination.
19515ffd83dbSDimitry Andric         if (MO->isUndef())
19525ffd83dbSDimitry Andric           report("Generic virtual register use cannot be undef", MO, MONum);
19535ffd83dbSDimitry Andric 
19540b57cec5SDimitry Andric         // If we're post-Select, we can't have gvregs anymore.
19550b57cec5SDimitry Andric         if (isFunctionSelected) {
19560b57cec5SDimitry Andric           report("Generic virtual register invalid in a Selected function",
19570b57cec5SDimitry Andric                  MO, MONum);
19580b57cec5SDimitry Andric           return;
19590b57cec5SDimitry Andric         }
19600b57cec5SDimitry Andric 
19610b57cec5SDimitry Andric         // The gvreg must have a type and it must not have a SubIdx.
19620b57cec5SDimitry Andric         LLT Ty = MRI->getType(Reg);
19630b57cec5SDimitry Andric         if (!Ty.isValid()) {
19640b57cec5SDimitry Andric           report("Generic virtual register must have a valid type", MO,
19650b57cec5SDimitry Andric                  MONum);
19660b57cec5SDimitry Andric           return;
19670b57cec5SDimitry Andric         }
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric         const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
19700b57cec5SDimitry Andric 
19710b57cec5SDimitry Andric         // If we're post-RegBankSelect, the gvreg must have a bank.
19720b57cec5SDimitry Andric         if (!RegBank && isFunctionRegBankSelected) {
19730b57cec5SDimitry Andric           report("Generic virtual register must have a bank in a "
19740b57cec5SDimitry Andric                  "RegBankSelected function",
19750b57cec5SDimitry Andric                  MO, MONum);
19760b57cec5SDimitry Andric           return;
19770b57cec5SDimitry Andric         }
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric         // Make sure the register fits into its register bank if any.
19800b57cec5SDimitry Andric         if (RegBank && Ty.isValid() &&
19810b57cec5SDimitry Andric             RegBank->getSize() < Ty.getSizeInBits()) {
19820b57cec5SDimitry Andric           report("Register bank is too small for virtual register", MO,
19830b57cec5SDimitry Andric                  MONum);
19840b57cec5SDimitry Andric           errs() << "Register bank " << RegBank->getName() << " too small("
19850b57cec5SDimitry Andric                  << RegBank->getSize() << ") to fit " << Ty.getSizeInBits()
19860b57cec5SDimitry Andric                  << "-bits\n";
19870b57cec5SDimitry Andric           return;
19880b57cec5SDimitry Andric         }
19890b57cec5SDimitry Andric         if (SubIdx)  {
19900b57cec5SDimitry Andric           report("Generic virtual register does not allow subregister index", MO,
19910b57cec5SDimitry Andric                  MONum);
19920b57cec5SDimitry Andric           return;
19930b57cec5SDimitry Andric         }
19940b57cec5SDimitry Andric 
19950b57cec5SDimitry Andric         // If this is a target specific instruction and this operand
19960b57cec5SDimitry Andric         // has register class constraint, the virtual register must
19970b57cec5SDimitry Andric         // comply to it.
19980b57cec5SDimitry Andric         if (!isPreISelGenericOpcode(MCID.getOpcode()) &&
19990b57cec5SDimitry Andric             MONum < MCID.getNumOperands() &&
20000b57cec5SDimitry Andric             TII->getRegClass(MCID, MONum, TRI, *MF)) {
20010b57cec5SDimitry Andric           report("Virtual register does not match instruction constraint", MO,
20020b57cec5SDimitry Andric                  MONum);
20030b57cec5SDimitry Andric           errs() << "Expect register class "
20040b57cec5SDimitry Andric                  << TRI->getRegClassName(
20050b57cec5SDimitry Andric                         TII->getRegClass(MCID, MONum, TRI, *MF))
20060b57cec5SDimitry Andric                  << " but got nothing\n";
20070b57cec5SDimitry Andric           return;
20080b57cec5SDimitry Andric         }
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric         break;
20110b57cec5SDimitry Andric       }
20120b57cec5SDimitry Andric       if (SubIdx) {
20130b57cec5SDimitry Andric         const TargetRegisterClass *SRC =
20140b57cec5SDimitry Andric           TRI->getSubClassWithSubReg(RC, SubIdx);
20150b57cec5SDimitry Andric         if (!SRC) {
20160b57cec5SDimitry Andric           report("Invalid subregister index for virtual register", MO, MONum);
20170b57cec5SDimitry Andric           errs() << "Register class " << TRI->getRegClassName(RC)
20180b57cec5SDimitry Andric               << " does not support subreg index " << SubIdx << "\n";
20190b57cec5SDimitry Andric           return;
20200b57cec5SDimitry Andric         }
20210b57cec5SDimitry Andric         if (RC != SRC) {
20220b57cec5SDimitry Andric           report("Invalid register class for subregister index", MO, MONum);
20230b57cec5SDimitry Andric           errs() << "Register class " << TRI->getRegClassName(RC)
20240b57cec5SDimitry Andric               << " does not fully support subreg index " << SubIdx << "\n";
20250b57cec5SDimitry Andric           return;
20260b57cec5SDimitry Andric         }
20270b57cec5SDimitry Andric       }
20280b57cec5SDimitry Andric       if (MONum < MCID.getNumOperands()) {
20290b57cec5SDimitry Andric         if (const TargetRegisterClass *DRC =
20300b57cec5SDimitry Andric               TII->getRegClass(MCID, MONum, TRI, *MF)) {
20310b57cec5SDimitry Andric           if (SubIdx) {
20320b57cec5SDimitry Andric             const TargetRegisterClass *SuperRC =
20330b57cec5SDimitry Andric                 TRI->getLargestLegalSuperClass(RC, *MF);
20340b57cec5SDimitry Andric             if (!SuperRC) {
20350b57cec5SDimitry Andric               report("No largest legal super class exists.", MO, MONum);
20360b57cec5SDimitry Andric               return;
20370b57cec5SDimitry Andric             }
20380b57cec5SDimitry Andric             DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
20390b57cec5SDimitry Andric             if (!DRC) {
20400b57cec5SDimitry Andric               report("No matching super-reg register class.", MO, MONum);
20410b57cec5SDimitry Andric               return;
20420b57cec5SDimitry Andric             }
20430b57cec5SDimitry Andric           }
20440b57cec5SDimitry Andric           if (!RC->hasSuperClassEq(DRC)) {
20450b57cec5SDimitry Andric             report("Illegal virtual register for instruction", MO, MONum);
20460b57cec5SDimitry Andric             errs() << "Expected a " << TRI->getRegClassName(DRC)
20470b57cec5SDimitry Andric                 << " register, but got a " << TRI->getRegClassName(RC)
20480b57cec5SDimitry Andric                 << " register\n";
20490b57cec5SDimitry Andric           }
20500b57cec5SDimitry Andric         }
20510b57cec5SDimitry Andric       }
20520b57cec5SDimitry Andric     }
20530b57cec5SDimitry Andric     break;
20540b57cec5SDimitry Andric   }
20550b57cec5SDimitry Andric 
20560b57cec5SDimitry Andric   case MachineOperand::MO_RegisterMask:
20570b57cec5SDimitry Andric     regMasks.push_back(MO->getRegMask());
20580b57cec5SDimitry Andric     break;
20590b57cec5SDimitry Andric 
20600b57cec5SDimitry Andric   case MachineOperand::MO_MachineBasicBlock:
20610b57cec5SDimitry Andric     if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
20620b57cec5SDimitry Andric       report("PHI operand is not in the CFG", MO, MONum);
20630b57cec5SDimitry Andric     break;
20640b57cec5SDimitry Andric 
20650b57cec5SDimitry Andric   case MachineOperand::MO_FrameIndex:
20660b57cec5SDimitry Andric     if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
20670b57cec5SDimitry Andric         LiveInts && !LiveInts->isNotInMIMap(*MI)) {
20680b57cec5SDimitry Andric       int FI = MO->getIndex();
20690b57cec5SDimitry Andric       LiveInterval &LI = LiveStks->getInterval(FI);
20700b57cec5SDimitry Andric       SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
20710b57cec5SDimitry Andric 
20720b57cec5SDimitry Andric       bool stores = MI->mayStore();
20730b57cec5SDimitry Andric       bool loads = MI->mayLoad();
20740b57cec5SDimitry Andric       // For a memory-to-memory move, we need to check if the frame
20750b57cec5SDimitry Andric       // index is used for storing or loading, by inspecting the
20760b57cec5SDimitry Andric       // memory operands.
20770b57cec5SDimitry Andric       if (stores && loads) {
20780b57cec5SDimitry Andric         for (auto *MMO : MI->memoperands()) {
20790b57cec5SDimitry Andric           const PseudoSourceValue *PSV = MMO->getPseudoValue();
20800b57cec5SDimitry Andric           if (PSV == nullptr) continue;
20810b57cec5SDimitry Andric           const FixedStackPseudoSourceValue *Value =
20820b57cec5SDimitry Andric             dyn_cast<FixedStackPseudoSourceValue>(PSV);
20830b57cec5SDimitry Andric           if (Value == nullptr) continue;
20840b57cec5SDimitry Andric           if (Value->getFrameIndex() != FI) continue;
20850b57cec5SDimitry Andric 
20860b57cec5SDimitry Andric           if (MMO->isStore())
20870b57cec5SDimitry Andric             loads = false;
20880b57cec5SDimitry Andric           else
20890b57cec5SDimitry Andric             stores = false;
20900b57cec5SDimitry Andric           break;
20910b57cec5SDimitry Andric         }
20920b57cec5SDimitry Andric         if (loads == stores)
20930b57cec5SDimitry Andric           report("Missing fixed stack memoperand.", MI);
20940b57cec5SDimitry Andric       }
20950b57cec5SDimitry Andric       if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
20960b57cec5SDimitry Andric         report("Instruction loads from dead spill slot", MO, MONum);
20970b57cec5SDimitry Andric         errs() << "Live stack: " << LI << '\n';
20980b57cec5SDimitry Andric       }
20990b57cec5SDimitry Andric       if (stores && !LI.liveAt(Idx.getRegSlot())) {
21000b57cec5SDimitry Andric         report("Instruction stores to dead spill slot", MO, MONum);
21010b57cec5SDimitry Andric         errs() << "Live stack: " << LI << '\n';
21020b57cec5SDimitry Andric       }
21030b57cec5SDimitry Andric     }
21040b57cec5SDimitry Andric     break;
21050b57cec5SDimitry Andric 
21060b57cec5SDimitry Andric   default:
21070b57cec5SDimitry Andric     break;
21080b57cec5SDimitry Andric   }
21090b57cec5SDimitry Andric }
21100b57cec5SDimitry Andric 
checkLivenessAtUse(const MachineOperand * MO,unsigned MONum,SlotIndex UseIdx,const LiveRange & LR,Register VRegOrUnit,LaneBitmask LaneMask)21110b57cec5SDimitry Andric void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
2112af732203SDimitry Andric                                          unsigned MONum, SlotIndex UseIdx,
2113af732203SDimitry Andric                                          const LiveRange &LR,
2114af732203SDimitry Andric                                          Register VRegOrUnit,
21150b57cec5SDimitry Andric                                          LaneBitmask LaneMask) {
21160b57cec5SDimitry Andric   LiveQueryResult LRQ = LR.Query(UseIdx);
21170b57cec5SDimitry Andric   // Check if we have a segment at the use, note however that we only need one
21180b57cec5SDimitry Andric   // live subregister range, the others may be dead.
21190b57cec5SDimitry Andric   if (!LRQ.valueIn() && LaneMask.none()) {
21200b57cec5SDimitry Andric     report("No live segment at use", MO, MONum);
21210b57cec5SDimitry Andric     report_context_liverange(LR);
21220b57cec5SDimitry Andric     report_context_vreg_regunit(VRegOrUnit);
21230b57cec5SDimitry Andric     report_context(UseIdx);
21240b57cec5SDimitry Andric   }
21250b57cec5SDimitry Andric   if (MO->isKill() && !LRQ.isKill()) {
21260b57cec5SDimitry Andric     report("Live range continues after kill flag", MO, MONum);
21270b57cec5SDimitry Andric     report_context_liverange(LR);
21280b57cec5SDimitry Andric     report_context_vreg_regunit(VRegOrUnit);
21290b57cec5SDimitry Andric     if (LaneMask.any())
21300b57cec5SDimitry Andric       report_context_lanemask(LaneMask);
21310b57cec5SDimitry Andric     report_context(UseIdx);
21320b57cec5SDimitry Andric   }
21330b57cec5SDimitry Andric }
21340b57cec5SDimitry Andric 
checkLivenessAtDef(const MachineOperand * MO,unsigned MONum,SlotIndex DefIdx,const LiveRange & LR,Register VRegOrUnit,bool SubRangeCheck,LaneBitmask LaneMask)21350b57cec5SDimitry Andric void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
2136af732203SDimitry Andric                                          unsigned MONum, SlotIndex DefIdx,
2137af732203SDimitry Andric                                          const LiveRange &LR,
2138af732203SDimitry Andric                                          Register VRegOrUnit,
2139af732203SDimitry Andric                                          bool SubRangeCheck,
2140af732203SDimitry Andric                                          LaneBitmask LaneMask) {
21410b57cec5SDimitry Andric   if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
21420b57cec5SDimitry Andric     assert(VNI && "NULL valno is not allowed");
21430b57cec5SDimitry Andric     if (VNI->def != DefIdx) {
21440b57cec5SDimitry Andric       report("Inconsistent valno->def", MO, MONum);
21450b57cec5SDimitry Andric       report_context_liverange(LR);
21460b57cec5SDimitry Andric       report_context_vreg_regunit(VRegOrUnit);
21470b57cec5SDimitry Andric       if (LaneMask.any())
21480b57cec5SDimitry Andric         report_context_lanemask(LaneMask);
21490b57cec5SDimitry Andric       report_context(*VNI);
21500b57cec5SDimitry Andric       report_context(DefIdx);
21510b57cec5SDimitry Andric     }
21520b57cec5SDimitry Andric   } else {
21530b57cec5SDimitry Andric     report("No live segment at def", MO, MONum);
21540b57cec5SDimitry Andric     report_context_liverange(LR);
21550b57cec5SDimitry Andric     report_context_vreg_regunit(VRegOrUnit);
21560b57cec5SDimitry Andric     if (LaneMask.any())
21570b57cec5SDimitry Andric       report_context_lanemask(LaneMask);
21580b57cec5SDimitry Andric     report_context(DefIdx);
21590b57cec5SDimitry Andric   }
21600b57cec5SDimitry Andric   // Check that, if the dead def flag is present, LiveInts agree.
21610b57cec5SDimitry Andric   if (MO->isDead()) {
21620b57cec5SDimitry Andric     LiveQueryResult LRQ = LR.Query(DefIdx);
21630b57cec5SDimitry Andric     if (!LRQ.isDeadDef()) {
21648bcb0991SDimitry Andric       assert(Register::isVirtualRegister(VRegOrUnit) &&
21650b57cec5SDimitry Andric              "Expecting a virtual register.");
21660b57cec5SDimitry Andric       // A dead subreg def only tells us that the specific subreg is dead. There
21670b57cec5SDimitry Andric       // could be other non-dead defs of other subregs, or we could have other
21680b57cec5SDimitry Andric       // parts of the register being live through the instruction. So unless we
21690b57cec5SDimitry Andric       // are checking liveness for a subrange it is ok for the live range to
21700b57cec5SDimitry Andric       // continue, given that we have a dead def of a subregister.
21710b57cec5SDimitry Andric       if (SubRangeCheck || MO->getSubReg() == 0) {
21720b57cec5SDimitry Andric         report("Live range continues after dead def flag", MO, MONum);
21730b57cec5SDimitry Andric         report_context_liverange(LR);
21740b57cec5SDimitry Andric         report_context_vreg_regunit(VRegOrUnit);
21750b57cec5SDimitry Andric         if (LaneMask.any())
21760b57cec5SDimitry Andric           report_context_lanemask(LaneMask);
21770b57cec5SDimitry Andric       }
21780b57cec5SDimitry Andric     }
21790b57cec5SDimitry Andric   }
21800b57cec5SDimitry Andric }
21810b57cec5SDimitry Andric 
checkLiveness(const MachineOperand * MO,unsigned MONum)21820b57cec5SDimitry Andric void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
21830b57cec5SDimitry Andric   const MachineInstr *MI = MO->getParent();
2184af732203SDimitry Andric   const Register Reg = MO->getReg();
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric   // Both use and def operands can read a register.
21870b57cec5SDimitry Andric   if (MO->readsReg()) {
21880b57cec5SDimitry Andric     if (MO->isKill())
21890b57cec5SDimitry Andric       addRegWithSubRegs(regsKilled, Reg);
21900b57cec5SDimitry Andric 
21910b57cec5SDimitry Andric     // Check that LiveVars knows this kill.
21928bcb0991SDimitry Andric     if (LiveVars && Register::isVirtualRegister(Reg) && MO->isKill()) {
21930b57cec5SDimitry Andric       LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
21940b57cec5SDimitry Andric       if (!is_contained(VI.Kills, MI))
21950b57cec5SDimitry Andric         report("Kill missing from LiveVariables", MO, MONum);
21960b57cec5SDimitry Andric     }
21970b57cec5SDimitry Andric 
21980b57cec5SDimitry Andric     // Check LiveInts liveness and kill.
21990b57cec5SDimitry Andric     if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
22000b57cec5SDimitry Andric       SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
22010b57cec5SDimitry Andric       // Check the cached regunit intervals.
2202af732203SDimitry Andric       if (Reg.isPhysical() && !isReserved(Reg)) {
2203af732203SDimitry Andric         for (MCRegUnitIterator Units(Reg.asMCReg(), TRI); Units.isValid();
2204af732203SDimitry Andric              ++Units) {
22050b57cec5SDimitry Andric           if (MRI->isReservedRegUnit(*Units))
22060b57cec5SDimitry Andric             continue;
22070b57cec5SDimitry Andric           if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
22080b57cec5SDimitry Andric             checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
22090b57cec5SDimitry Andric         }
22100b57cec5SDimitry Andric       }
22110b57cec5SDimitry Andric 
22128bcb0991SDimitry Andric       if (Register::isVirtualRegister(Reg)) {
22130b57cec5SDimitry Andric         if (LiveInts->hasInterval(Reg)) {
22140b57cec5SDimitry Andric           // This is a virtual register interval.
22150b57cec5SDimitry Andric           const LiveInterval &LI = LiveInts->getInterval(Reg);
22160b57cec5SDimitry Andric           checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
22170b57cec5SDimitry Andric 
22180b57cec5SDimitry Andric           if (LI.hasSubRanges() && !MO->isDef()) {
22190b57cec5SDimitry Andric             unsigned SubRegIdx = MO->getSubReg();
22200b57cec5SDimitry Andric             LaneBitmask MOMask = SubRegIdx != 0
22210b57cec5SDimitry Andric                                ? TRI->getSubRegIndexLaneMask(SubRegIdx)
22220b57cec5SDimitry Andric                                : MRI->getMaxLaneMaskForVReg(Reg);
22230b57cec5SDimitry Andric             LaneBitmask LiveInMask;
22240b57cec5SDimitry Andric             for (const LiveInterval::SubRange &SR : LI.subranges()) {
22250b57cec5SDimitry Andric               if ((MOMask & SR.LaneMask).none())
22260b57cec5SDimitry Andric                 continue;
22270b57cec5SDimitry Andric               checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
22280b57cec5SDimitry Andric               LiveQueryResult LRQ = SR.Query(UseIdx);
22290b57cec5SDimitry Andric               if (LRQ.valueIn())
22300b57cec5SDimitry Andric                 LiveInMask |= SR.LaneMask;
22310b57cec5SDimitry Andric             }
22320b57cec5SDimitry Andric             // At least parts of the register has to be live at the use.
22330b57cec5SDimitry Andric             if ((LiveInMask & MOMask).none()) {
22340b57cec5SDimitry Andric               report("No live subrange at use", MO, MONum);
22350b57cec5SDimitry Andric               report_context(LI);
22360b57cec5SDimitry Andric               report_context(UseIdx);
22370b57cec5SDimitry Andric             }
22380b57cec5SDimitry Andric           }
22390b57cec5SDimitry Andric         } else {
22400b57cec5SDimitry Andric           report("Virtual register has no live interval", MO, MONum);
22410b57cec5SDimitry Andric         }
22420b57cec5SDimitry Andric       }
22430b57cec5SDimitry Andric     }
22440b57cec5SDimitry Andric 
22450b57cec5SDimitry Andric     // Use of a dead register.
22460b57cec5SDimitry Andric     if (!regsLive.count(Reg)) {
22478bcb0991SDimitry Andric       if (Register::isPhysicalRegister(Reg)) {
22480b57cec5SDimitry Andric         // Reserved registers may be used even when 'dead'.
22490b57cec5SDimitry Andric         bool Bad = !isReserved(Reg);
22500b57cec5SDimitry Andric         // We are fine if just any subregister has a defined value.
22510b57cec5SDimitry Andric         if (Bad) {
2252480093f4SDimitry Andric 
2253480093f4SDimitry Andric           for (const MCPhysReg &SubReg : TRI->subregs(Reg)) {
2254480093f4SDimitry Andric             if (regsLive.count(SubReg)) {
22550b57cec5SDimitry Andric               Bad = false;
22560b57cec5SDimitry Andric               break;
22570b57cec5SDimitry Andric             }
22580b57cec5SDimitry Andric           }
22590b57cec5SDimitry Andric         }
22600b57cec5SDimitry Andric         // If there is an additional implicit-use of a super register we stop
22610b57cec5SDimitry Andric         // here. By definition we are fine if the super register is not
22620b57cec5SDimitry Andric         // (completely) dead, if the complete super register is dead we will
22630b57cec5SDimitry Andric         // get a report for its operand.
22640b57cec5SDimitry Andric         if (Bad) {
22650b57cec5SDimitry Andric           for (const MachineOperand &MOP : MI->uses()) {
22660b57cec5SDimitry Andric             if (!MOP.isReg() || !MOP.isImplicit())
22670b57cec5SDimitry Andric               continue;
22680b57cec5SDimitry Andric 
22698bcb0991SDimitry Andric             if (!Register::isPhysicalRegister(MOP.getReg()))
22700b57cec5SDimitry Andric               continue;
22710b57cec5SDimitry Andric 
2272*5f7ddb14SDimitry Andric             if (llvm::is_contained(TRI->subregs(MOP.getReg()), Reg))
22730b57cec5SDimitry Andric               Bad = false;
22740b57cec5SDimitry Andric           }
22750b57cec5SDimitry Andric         }
22760b57cec5SDimitry Andric         if (Bad)
22770b57cec5SDimitry Andric           report("Using an undefined physical register", MO, MONum);
22780b57cec5SDimitry Andric       } else if (MRI->def_empty(Reg)) {
22790b57cec5SDimitry Andric         report("Reading virtual register without a def", MO, MONum);
22800b57cec5SDimitry Andric       } else {
22810b57cec5SDimitry Andric         BBInfo &MInfo = MBBInfoMap[MI->getParent()];
22820b57cec5SDimitry Andric         // We don't know which virtual registers are live in, so only complain
22830b57cec5SDimitry Andric         // if vreg was killed in this MBB. Otherwise keep track of vregs that
22840b57cec5SDimitry Andric         // must be live in. PHI instructions are handled separately.
22850b57cec5SDimitry Andric         if (MInfo.regsKilled.count(Reg))
22860b57cec5SDimitry Andric           report("Using a killed virtual register", MO, MONum);
22870b57cec5SDimitry Andric         else if (!MI->isPHI())
22880b57cec5SDimitry Andric           MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
22890b57cec5SDimitry Andric       }
22900b57cec5SDimitry Andric     }
22910b57cec5SDimitry Andric   }
22920b57cec5SDimitry Andric 
22930b57cec5SDimitry Andric   if (MO->isDef()) {
22940b57cec5SDimitry Andric     // Register defined.
22950b57cec5SDimitry Andric     // TODO: verify that earlyclobber ops are not used.
22960b57cec5SDimitry Andric     if (MO->isDead())
22970b57cec5SDimitry Andric       addRegWithSubRegs(regsDead, Reg);
22980b57cec5SDimitry Andric     else
22990b57cec5SDimitry Andric       addRegWithSubRegs(regsDefined, Reg);
23000b57cec5SDimitry Andric 
23010b57cec5SDimitry Andric     // Verify SSA form.
23028bcb0991SDimitry Andric     if (MRI->isSSA() && Register::isVirtualRegister(Reg) &&
23030b57cec5SDimitry Andric         std::next(MRI->def_begin(Reg)) != MRI->def_end())
23040b57cec5SDimitry Andric       report("Multiple virtual register defs in SSA form", MO, MONum);
23050b57cec5SDimitry Andric 
23060b57cec5SDimitry Andric     // Check LiveInts for a live segment, but only for virtual registers.
23070b57cec5SDimitry Andric     if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
23080b57cec5SDimitry Andric       SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
23090b57cec5SDimitry Andric       DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
23100b57cec5SDimitry Andric 
23118bcb0991SDimitry Andric       if (Register::isVirtualRegister(Reg)) {
23120b57cec5SDimitry Andric         if (LiveInts->hasInterval(Reg)) {
23130b57cec5SDimitry Andric           const LiveInterval &LI = LiveInts->getInterval(Reg);
23140b57cec5SDimitry Andric           checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
23150b57cec5SDimitry Andric 
23160b57cec5SDimitry Andric           if (LI.hasSubRanges()) {
23170b57cec5SDimitry Andric             unsigned SubRegIdx = MO->getSubReg();
23180b57cec5SDimitry Andric             LaneBitmask MOMask = SubRegIdx != 0
23190b57cec5SDimitry Andric               ? TRI->getSubRegIndexLaneMask(SubRegIdx)
23200b57cec5SDimitry Andric               : MRI->getMaxLaneMaskForVReg(Reg);
23210b57cec5SDimitry Andric             for (const LiveInterval::SubRange &SR : LI.subranges()) {
23220b57cec5SDimitry Andric               if ((SR.LaneMask & MOMask).none())
23230b57cec5SDimitry Andric                 continue;
23240b57cec5SDimitry Andric               checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, true, SR.LaneMask);
23250b57cec5SDimitry Andric             }
23260b57cec5SDimitry Andric           }
23270b57cec5SDimitry Andric         } else {
23280b57cec5SDimitry Andric           report("Virtual register has no Live interval", MO, MONum);
23290b57cec5SDimitry Andric         }
23300b57cec5SDimitry Andric       }
23310b57cec5SDimitry Andric     }
23320b57cec5SDimitry Andric   }
23330b57cec5SDimitry Andric }
23340b57cec5SDimitry Andric 
23350b57cec5SDimitry Andric // This function gets called after visiting all instructions in a bundle. The
23360b57cec5SDimitry Andric // argument points to the bundle header.
23370b57cec5SDimitry Andric // Normal stand-alone instructions are also considered 'bundles', and this
23380b57cec5SDimitry Andric // function is called for all of them.
visitMachineBundleAfter(const MachineInstr * MI)23390b57cec5SDimitry Andric void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
23400b57cec5SDimitry Andric   BBInfo &MInfo = MBBInfoMap[MI->getParent()];
23410b57cec5SDimitry Andric   set_union(MInfo.regsKilled, regsKilled);
23420b57cec5SDimitry Andric   set_subtract(regsLive, regsKilled); regsKilled.clear();
23430b57cec5SDimitry Andric   // Kill any masked registers.
23440b57cec5SDimitry Andric   while (!regMasks.empty()) {
23450b57cec5SDimitry Andric     const uint32_t *Mask = regMasks.pop_back_val();
2346af732203SDimitry Andric     for (Register Reg : regsLive)
2347af732203SDimitry Andric       if (Reg.isPhysical() &&
2348af732203SDimitry Andric           MachineOperand::clobbersPhysReg(Mask, Reg.asMCReg()))
23495ffd83dbSDimitry Andric         regsDead.push_back(Reg);
23500b57cec5SDimitry Andric   }
23510b57cec5SDimitry Andric   set_subtract(regsLive, regsDead);   regsDead.clear();
23520b57cec5SDimitry Andric   set_union(regsLive, regsDefined);   regsDefined.clear();
23530b57cec5SDimitry Andric }
23540b57cec5SDimitry Andric 
23550b57cec5SDimitry Andric void
visitMachineBasicBlockAfter(const MachineBasicBlock * MBB)23560b57cec5SDimitry Andric MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
23570b57cec5SDimitry Andric   MBBInfoMap[MBB].regsLiveOut = regsLive;
23580b57cec5SDimitry Andric   regsLive.clear();
23590b57cec5SDimitry Andric 
23600b57cec5SDimitry Andric   if (Indexes) {
23610b57cec5SDimitry Andric     SlotIndex stop = Indexes->getMBBEndIdx(MBB);
23620b57cec5SDimitry Andric     if (!(stop > lastIndex)) {
23630b57cec5SDimitry Andric       report("Block ends before last instruction index", MBB);
23640b57cec5SDimitry Andric       errs() << "Block ends at " << stop
23650b57cec5SDimitry Andric           << " last instruction was at " << lastIndex << '\n';
23660b57cec5SDimitry Andric     }
23670b57cec5SDimitry Andric     lastIndex = stop;
23680b57cec5SDimitry Andric   }
23690b57cec5SDimitry Andric }
23700b57cec5SDimitry Andric 
23715ffd83dbSDimitry Andric namespace {
23725ffd83dbSDimitry Andric // This implements a set of registers that serves as a filter: can filter other
23735ffd83dbSDimitry Andric // sets by passing through elements not in the filter and blocking those that
23745ffd83dbSDimitry Andric // are. Any filter implicitly includes the full set of physical registers upon
23755ffd83dbSDimitry Andric // creation, thus filtering them all out. The filter itself as a set only grows,
23765ffd83dbSDimitry Andric // and needs to be as efficient as possible.
23775ffd83dbSDimitry Andric struct VRegFilter {
23785ffd83dbSDimitry Andric   // Add elements to the filter itself. \pre Input set \p FromRegSet must have
23795ffd83dbSDimitry Andric   // no duplicates. Both virtual and physical registers are fine.
add__anon86168b390411::VRegFilter23805ffd83dbSDimitry Andric   template <typename RegSetT> void add(const RegSetT &FromRegSet) {
2381af732203SDimitry Andric     SmallVector<Register, 0> VRegsBuffer;
23825ffd83dbSDimitry Andric     filterAndAdd(FromRegSet, VRegsBuffer);
23835ffd83dbSDimitry Andric   }
23845ffd83dbSDimitry Andric   // Filter \p FromRegSet through the filter and append passed elements into \p
23855ffd83dbSDimitry Andric   // ToVRegs. All elements appended are then added to the filter itself.
23865ffd83dbSDimitry Andric   // \returns true if anything changed.
23875ffd83dbSDimitry Andric   template <typename RegSetT>
filterAndAdd__anon86168b390411::VRegFilter23885ffd83dbSDimitry Andric   bool filterAndAdd(const RegSetT &FromRegSet,
2389af732203SDimitry Andric                     SmallVectorImpl<Register> &ToVRegs) {
23905ffd83dbSDimitry Andric     unsigned SparseUniverse = Sparse.size();
23915ffd83dbSDimitry Andric     unsigned NewSparseUniverse = SparseUniverse;
23925ffd83dbSDimitry Andric     unsigned NewDenseSize = Dense.size();
23935ffd83dbSDimitry Andric     size_t Begin = ToVRegs.size();
2394af732203SDimitry Andric     for (Register Reg : FromRegSet) {
2395af732203SDimitry Andric       if (!Reg.isVirtual())
23965ffd83dbSDimitry Andric         continue;
23975ffd83dbSDimitry Andric       unsigned Index = Register::virtReg2Index(Reg);
23985ffd83dbSDimitry Andric       if (Index < SparseUniverseMax) {
23995ffd83dbSDimitry Andric         if (Index < SparseUniverse && Sparse.test(Index))
24005ffd83dbSDimitry Andric           continue;
24015ffd83dbSDimitry Andric         NewSparseUniverse = std::max(NewSparseUniverse, Index + 1);
24025ffd83dbSDimitry Andric       } else {
24035ffd83dbSDimitry Andric         if (Dense.count(Reg))
24045ffd83dbSDimitry Andric           continue;
24055ffd83dbSDimitry Andric         ++NewDenseSize;
24065ffd83dbSDimitry Andric       }
24075ffd83dbSDimitry Andric       ToVRegs.push_back(Reg);
24085ffd83dbSDimitry Andric     }
24095ffd83dbSDimitry Andric     size_t End = ToVRegs.size();
24105ffd83dbSDimitry Andric     if (Begin == End)
24115ffd83dbSDimitry Andric       return false;
24125ffd83dbSDimitry Andric     // Reserving space in sets once performs better than doing so continuously
24135ffd83dbSDimitry Andric     // and pays easily for double look-ups (even in Dense with SparseUniverseMax
24145ffd83dbSDimitry Andric     // tuned all the way down) and double iteration (the second one is over a
24155ffd83dbSDimitry Andric     // SmallVector, which is a lot cheaper compared to DenseSet or BitVector).
24165ffd83dbSDimitry Andric     Sparse.resize(NewSparseUniverse);
24175ffd83dbSDimitry Andric     Dense.reserve(NewDenseSize);
24185ffd83dbSDimitry Andric     for (unsigned I = Begin; I < End; ++I) {
2419af732203SDimitry Andric       Register Reg = ToVRegs[I];
24205ffd83dbSDimitry Andric       unsigned Index = Register::virtReg2Index(Reg);
24215ffd83dbSDimitry Andric       if (Index < SparseUniverseMax)
24225ffd83dbSDimitry Andric         Sparse.set(Index);
24235ffd83dbSDimitry Andric       else
24245ffd83dbSDimitry Andric         Dense.insert(Reg);
24255ffd83dbSDimitry Andric     }
24265ffd83dbSDimitry Andric     return true;
24275ffd83dbSDimitry Andric   }
24285ffd83dbSDimitry Andric 
24295ffd83dbSDimitry Andric private:
24305ffd83dbSDimitry Andric   static constexpr unsigned SparseUniverseMax = 10 * 1024 * 8;
24315ffd83dbSDimitry Andric   // VRegs indexed within SparseUniverseMax are tracked by Sparse, those beyound
24325ffd83dbSDimitry Andric   // are tracked by Dense. The only purpose of the threashold and the Dense set
24335ffd83dbSDimitry Andric   // is to have a reasonably growing memory usage in pathological cases (large
24345ffd83dbSDimitry Andric   // number of very sparse VRegFilter instances live at the same time). In
24355ffd83dbSDimitry Andric   // practice even in the worst-by-execution time cases having all elements
24365ffd83dbSDimitry Andric   // tracked by Sparse (very large SparseUniverseMax scenario) tends to be more
24375ffd83dbSDimitry Andric   // space efficient than if tracked by Dense. The threashold is set to keep the
24385ffd83dbSDimitry Andric   // worst-case memory usage within 2x of figures determined empirically for
24395ffd83dbSDimitry Andric   // "all Dense" scenario in such worst-by-execution-time cases.
24405ffd83dbSDimitry Andric   BitVector Sparse;
24415ffd83dbSDimitry Andric   DenseSet<unsigned> Dense;
24425ffd83dbSDimitry Andric };
24435ffd83dbSDimitry Andric 
24445ffd83dbSDimitry Andric // Implements both a transfer function and a (binary, in-place) join operator
24455ffd83dbSDimitry Andric // for a dataflow over register sets with set union join and filtering transfer
24465ffd83dbSDimitry Andric // (out_b = in_b \ filter_b). filter_b is expected to be set-up ahead of time.
24475ffd83dbSDimitry Andric // Maintains out_b as its state, allowing for O(n) iteration over it at any
24485ffd83dbSDimitry Andric // time, where n is the size of the set (as opposed to O(U) where U is the
24495ffd83dbSDimitry Andric // universe). filter_b implicitly contains all physical registers at all times.
24505ffd83dbSDimitry Andric class FilteringVRegSet {
24515ffd83dbSDimitry Andric   VRegFilter Filter;
2452af732203SDimitry Andric   SmallVector<Register, 0> VRegs;
24535ffd83dbSDimitry Andric 
24545ffd83dbSDimitry Andric public:
24555ffd83dbSDimitry Andric   // Set-up the filter_b. \pre Input register set \p RS must have no duplicates.
24565ffd83dbSDimitry Andric   // Both virtual and physical registers are fine.
addToFilter(const RegSetT & RS)24575ffd83dbSDimitry Andric   template <typename RegSetT> void addToFilter(const RegSetT &RS) {
24585ffd83dbSDimitry Andric     Filter.add(RS);
24595ffd83dbSDimitry Andric   }
24605ffd83dbSDimitry Andric   // Passes \p RS through the filter_b (transfer function) and adds what's left
24615ffd83dbSDimitry Andric   // to itself (out_b).
add(const RegSetT & RS)24625ffd83dbSDimitry Andric   template <typename RegSetT> bool add(const RegSetT &RS) {
24635ffd83dbSDimitry Andric     // Double-duty the Filter: to maintain VRegs a set (and the join operation
24645ffd83dbSDimitry Andric     // a set union) just add everything being added here to the Filter as well.
24655ffd83dbSDimitry Andric     return Filter.filterAndAdd(RS, VRegs);
24665ffd83dbSDimitry Andric   }
24675ffd83dbSDimitry Andric   using const_iterator = decltype(VRegs)::const_iterator;
begin() const24685ffd83dbSDimitry Andric   const_iterator begin() const { return VRegs.begin(); }
end() const24695ffd83dbSDimitry Andric   const_iterator end() const { return VRegs.end(); }
size() const24705ffd83dbSDimitry Andric   size_t size() const { return VRegs.size(); }
24715ffd83dbSDimitry Andric };
24725ffd83dbSDimitry Andric } // namespace
24735ffd83dbSDimitry Andric 
24740b57cec5SDimitry Andric // Calculate the largest possible vregsPassed sets. These are the registers that
24750b57cec5SDimitry Andric // can pass through an MBB live, but may not be live every time. It is assumed
24760b57cec5SDimitry Andric // that all vregsPassed sets are empty before the call.
calcRegsPassed()24770b57cec5SDimitry Andric void MachineVerifier::calcRegsPassed() {
2478af732203SDimitry Andric   if (MF->empty())
24795ffd83dbSDimitry Andric     // ReversePostOrderTraversal doesn't handle empty functions.
24805ffd83dbSDimitry Andric     return;
24810b57cec5SDimitry Andric 
2482af732203SDimitry Andric   for (const MachineBasicBlock *MB :
2483af732203SDimitry Andric        ReversePostOrderTraversal<const MachineFunction *>(MF)) {
2484af732203SDimitry Andric     FilteringVRegSet VRegs;
2485af732203SDimitry Andric     BBInfo &Info = MBBInfoMap[MB];
2486af732203SDimitry Andric     assert(Info.reachable);
2487af732203SDimitry Andric 
2488af732203SDimitry Andric     VRegs.addToFilter(Info.regsKilled);
2489af732203SDimitry Andric     VRegs.addToFilter(Info.regsLiveOut);
2490af732203SDimitry Andric     for (const MachineBasicBlock *Pred : MB->predecessors()) {
2491af732203SDimitry Andric       const BBInfo &PredInfo = MBBInfoMap[Pred];
2492af732203SDimitry Andric       if (!PredInfo.reachable)
24930b57cec5SDimitry Andric         continue;
2494af732203SDimitry Andric 
2495af732203SDimitry Andric       VRegs.add(PredInfo.regsLiveOut);
2496af732203SDimitry Andric       VRegs.add(PredInfo.vregsPassed);
24970b57cec5SDimitry Andric     }
2498af732203SDimitry Andric     Info.vregsPassed.reserve(VRegs.size());
2499af732203SDimitry Andric     Info.vregsPassed.insert(VRegs.begin(), VRegs.end());
25005ffd83dbSDimitry Andric   }
25010b57cec5SDimitry Andric }
25020b57cec5SDimitry Andric 
25030b57cec5SDimitry Andric // Calculate the set of virtual registers that must be passed through each basic
25040b57cec5SDimitry Andric // block in order to satisfy the requirements of successor blocks. This is very
25050b57cec5SDimitry Andric // similar to calcRegsPassed, only backwards.
calcRegsRequired()25060b57cec5SDimitry Andric void MachineVerifier::calcRegsRequired() {
25070b57cec5SDimitry Andric   // First push live-in regs to predecessors' vregsRequired.
25080b57cec5SDimitry Andric   SmallPtrSet<const MachineBasicBlock*, 8> todo;
25090b57cec5SDimitry Andric   for (const auto &MBB : *MF) {
25100b57cec5SDimitry Andric     BBInfo &MInfo = MBBInfoMap[&MBB];
25115ffd83dbSDimitry Andric     for (const MachineBasicBlock *Pred : MBB.predecessors()) {
25125ffd83dbSDimitry Andric       BBInfo &PInfo = MBBInfoMap[Pred];
25130b57cec5SDimitry Andric       if (PInfo.addRequired(MInfo.vregsLiveIn))
25145ffd83dbSDimitry Andric         todo.insert(Pred);
25150b57cec5SDimitry Andric     }
2516af732203SDimitry Andric 
2517af732203SDimitry Andric     // Handle the PHI node.
2518af732203SDimitry Andric     for (const MachineInstr &MI : MBB.phis()) {
2519af732203SDimitry Andric       for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) {
2520af732203SDimitry Andric         // Skip those Operands which are undef regs or not regs.
2521af732203SDimitry Andric         if (!MI.getOperand(i).isReg() || !MI.getOperand(i).readsReg())
2522af732203SDimitry Andric           continue;
2523af732203SDimitry Andric 
2524af732203SDimitry Andric         // Get register and predecessor for one PHI edge.
2525af732203SDimitry Andric         Register Reg = MI.getOperand(i).getReg();
2526af732203SDimitry Andric         const MachineBasicBlock *Pred = MI.getOperand(i + 1).getMBB();
2527af732203SDimitry Andric 
2528af732203SDimitry Andric         BBInfo &PInfo = MBBInfoMap[Pred];
2529af732203SDimitry Andric         if (PInfo.addRequired(Reg))
2530af732203SDimitry Andric           todo.insert(Pred);
2531af732203SDimitry Andric       }
2532af732203SDimitry Andric     }
25330b57cec5SDimitry Andric   }
25340b57cec5SDimitry Andric 
25350b57cec5SDimitry Andric   // Iteratively push vregsRequired to predecessors. This will converge to the
25360b57cec5SDimitry Andric   // same final state regardless of DenseSet iteration order.
25370b57cec5SDimitry Andric   while (!todo.empty()) {
25380b57cec5SDimitry Andric     const MachineBasicBlock *MBB = *todo.begin();
25390b57cec5SDimitry Andric     todo.erase(MBB);
25400b57cec5SDimitry Andric     BBInfo &MInfo = MBBInfoMap[MBB];
25415ffd83dbSDimitry Andric     for (const MachineBasicBlock *Pred : MBB->predecessors()) {
25425ffd83dbSDimitry Andric       if (Pred == MBB)
25430b57cec5SDimitry Andric         continue;
25445ffd83dbSDimitry Andric       BBInfo &SInfo = MBBInfoMap[Pred];
25450b57cec5SDimitry Andric       if (SInfo.addRequired(MInfo.vregsRequired))
25465ffd83dbSDimitry Andric         todo.insert(Pred);
25470b57cec5SDimitry Andric     }
25480b57cec5SDimitry Andric   }
25490b57cec5SDimitry Andric }
25500b57cec5SDimitry Andric 
25510b57cec5SDimitry Andric // Check PHI instructions at the beginning of MBB. It is assumed that
25520b57cec5SDimitry Andric // calcRegsPassed has been run so BBInfo::isLiveOut is valid.
checkPHIOps(const MachineBasicBlock & MBB)25530b57cec5SDimitry Andric void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) {
25540b57cec5SDimitry Andric   BBInfo &MInfo = MBBInfoMap[&MBB];
25550b57cec5SDimitry Andric 
25560b57cec5SDimitry Andric   SmallPtrSet<const MachineBasicBlock*, 8> seen;
25570b57cec5SDimitry Andric   for (const MachineInstr &Phi : MBB) {
25580b57cec5SDimitry Andric     if (!Phi.isPHI())
25590b57cec5SDimitry Andric       break;
25600b57cec5SDimitry Andric     seen.clear();
25610b57cec5SDimitry Andric 
25620b57cec5SDimitry Andric     const MachineOperand &MODef = Phi.getOperand(0);
25630b57cec5SDimitry Andric     if (!MODef.isReg() || !MODef.isDef()) {
25640b57cec5SDimitry Andric       report("Expected first PHI operand to be a register def", &MODef, 0);
25650b57cec5SDimitry Andric       continue;
25660b57cec5SDimitry Andric     }
25670b57cec5SDimitry Andric     if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() ||
25680b57cec5SDimitry Andric         MODef.isEarlyClobber() || MODef.isDebug())
25690b57cec5SDimitry Andric       report("Unexpected flag on PHI operand", &MODef, 0);
25708bcb0991SDimitry Andric     Register DefReg = MODef.getReg();
25718bcb0991SDimitry Andric     if (!Register::isVirtualRegister(DefReg))
25720b57cec5SDimitry Andric       report("Expected first PHI operand to be a virtual register", &MODef, 0);
25730b57cec5SDimitry Andric 
25740b57cec5SDimitry Andric     for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) {
25750b57cec5SDimitry Andric       const MachineOperand &MO0 = Phi.getOperand(I);
25760b57cec5SDimitry Andric       if (!MO0.isReg()) {
25770b57cec5SDimitry Andric         report("Expected PHI operand to be a register", &MO0, I);
25780b57cec5SDimitry Andric         continue;
25790b57cec5SDimitry Andric       }
25800b57cec5SDimitry Andric       if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() ||
25810b57cec5SDimitry Andric           MO0.isDebug() || MO0.isTied())
25820b57cec5SDimitry Andric         report("Unexpected flag on PHI operand", &MO0, I);
25830b57cec5SDimitry Andric 
25840b57cec5SDimitry Andric       const MachineOperand &MO1 = Phi.getOperand(I + 1);
25850b57cec5SDimitry Andric       if (!MO1.isMBB()) {
25860b57cec5SDimitry Andric         report("Expected PHI operand to be a basic block", &MO1, I + 1);
25870b57cec5SDimitry Andric         continue;
25880b57cec5SDimitry Andric       }
25890b57cec5SDimitry Andric 
25900b57cec5SDimitry Andric       const MachineBasicBlock &Pre = *MO1.getMBB();
25910b57cec5SDimitry Andric       if (!Pre.isSuccessor(&MBB)) {
25920b57cec5SDimitry Andric         report("PHI input is not a predecessor block", &MO1, I + 1);
25930b57cec5SDimitry Andric         continue;
25940b57cec5SDimitry Andric       }
25950b57cec5SDimitry Andric 
25960b57cec5SDimitry Andric       if (MInfo.reachable) {
25970b57cec5SDimitry Andric         seen.insert(&Pre);
25980b57cec5SDimitry Andric         BBInfo &PrInfo = MBBInfoMap[&Pre];
25990b57cec5SDimitry Andric         if (!MO0.isUndef() && PrInfo.reachable &&
26000b57cec5SDimitry Andric             !PrInfo.isLiveOut(MO0.getReg()))
26010b57cec5SDimitry Andric           report("PHI operand is not live-out from predecessor", &MO0, I);
26020b57cec5SDimitry Andric       }
26030b57cec5SDimitry Andric     }
26040b57cec5SDimitry Andric 
26050b57cec5SDimitry Andric     // Did we see all predecessors?
26060b57cec5SDimitry Andric     if (MInfo.reachable) {
26070b57cec5SDimitry Andric       for (MachineBasicBlock *Pred : MBB.predecessors()) {
26080b57cec5SDimitry Andric         if (!seen.count(Pred)) {
26090b57cec5SDimitry Andric           report("Missing PHI operand", &Phi);
26100b57cec5SDimitry Andric           errs() << printMBBReference(*Pred)
26110b57cec5SDimitry Andric                  << " is a predecessor according to the CFG.\n";
26120b57cec5SDimitry Andric         }
26130b57cec5SDimitry Andric       }
26140b57cec5SDimitry Andric     }
26150b57cec5SDimitry Andric   }
26160b57cec5SDimitry Andric }
26170b57cec5SDimitry Andric 
visitMachineFunctionAfter()26180b57cec5SDimitry Andric void MachineVerifier::visitMachineFunctionAfter() {
26190b57cec5SDimitry Andric   calcRegsPassed();
26200b57cec5SDimitry Andric 
26210b57cec5SDimitry Andric   for (const MachineBasicBlock &MBB : *MF)
26220b57cec5SDimitry Andric     checkPHIOps(MBB);
26230b57cec5SDimitry Andric 
26240b57cec5SDimitry Andric   // Now check liveness info if available
26250b57cec5SDimitry Andric   calcRegsRequired();
26260b57cec5SDimitry Andric 
26270b57cec5SDimitry Andric   // Check for killed virtual registers that should be live out.
26280b57cec5SDimitry Andric   for (const auto &MBB : *MF) {
26290b57cec5SDimitry Andric     BBInfo &MInfo = MBBInfoMap[&MBB];
2630af732203SDimitry Andric     for (Register VReg : MInfo.vregsRequired)
26315ffd83dbSDimitry Andric       if (MInfo.regsKilled.count(VReg)) {
26320b57cec5SDimitry Andric         report("Virtual register killed in block, but needed live out.", &MBB);
26335ffd83dbSDimitry Andric         errs() << "Virtual register " << printReg(VReg)
26340b57cec5SDimitry Andric                << " is used after the block.\n";
26350b57cec5SDimitry Andric       }
26360b57cec5SDimitry Andric   }
26370b57cec5SDimitry Andric 
26380b57cec5SDimitry Andric   if (!MF->empty()) {
26390b57cec5SDimitry Andric     BBInfo &MInfo = MBBInfoMap[&MF->front()];
2640af732203SDimitry Andric     for (Register VReg : MInfo.vregsRequired) {
26410b57cec5SDimitry Andric       report("Virtual register defs don't dominate all uses.", MF);
26425ffd83dbSDimitry Andric       report_context_vreg(VReg);
26430b57cec5SDimitry Andric     }
26440b57cec5SDimitry Andric   }
26450b57cec5SDimitry Andric 
26460b57cec5SDimitry Andric   if (LiveVars)
26470b57cec5SDimitry Andric     verifyLiveVariables();
26480b57cec5SDimitry Andric   if (LiveInts)
26490b57cec5SDimitry Andric     verifyLiveIntervals();
26500b57cec5SDimitry Andric 
2651480093f4SDimitry Andric   // Check live-in list of each MBB. If a register is live into MBB, check
2652480093f4SDimitry Andric   // that the register is in regsLiveOut of each predecessor block. Since
2653480093f4SDimitry Andric   // this must come from a definition in the predecesssor or its live-in
2654480093f4SDimitry Andric   // list, this will catch a live-through case where the predecessor does not
2655480093f4SDimitry Andric   // have the register in its live-in list.  This currently only checks
2656480093f4SDimitry Andric   // registers that have no aliases, are not allocatable and are not
2657480093f4SDimitry Andric   // reserved, which could mean a condition code register for instance.
2658480093f4SDimitry Andric   if (MRI->tracksLiveness())
2659480093f4SDimitry Andric     for (const auto &MBB : *MF)
2660480093f4SDimitry Andric       for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) {
2661480093f4SDimitry Andric         MCPhysReg LiveInReg = P.PhysReg;
2662480093f4SDimitry Andric         bool hasAliases = MCRegAliasIterator(LiveInReg, TRI, false).isValid();
2663480093f4SDimitry Andric         if (hasAliases || isAllocatable(LiveInReg) || isReserved(LiveInReg))
2664480093f4SDimitry Andric           continue;
2665480093f4SDimitry Andric         for (const MachineBasicBlock *Pred : MBB.predecessors()) {
2666480093f4SDimitry Andric           BBInfo &PInfo = MBBInfoMap[Pred];
2667480093f4SDimitry Andric           if (!PInfo.regsLiveOut.count(LiveInReg)) {
2668480093f4SDimitry Andric             report("Live in register not found to be live out from predecessor.",
2669480093f4SDimitry Andric                    &MBB);
2670480093f4SDimitry Andric             errs() << TRI->getName(LiveInReg)
2671480093f4SDimitry Andric                    << " not found to be live out from "
2672480093f4SDimitry Andric                    << printMBBReference(*Pred) << "\n";
2673480093f4SDimitry Andric           }
2674480093f4SDimitry Andric         }
2675480093f4SDimitry Andric       }
2676480093f4SDimitry Andric 
26770b57cec5SDimitry Andric   for (auto CSInfo : MF->getCallSitesInfo())
26780b57cec5SDimitry Andric     if (!CSInfo.first->isCall())
26790b57cec5SDimitry Andric       report("Call site info referencing instruction that is not call", MF);
2680af732203SDimitry Andric 
2681af732203SDimitry Andric   // If there's debug-info, check that we don't have any duplicate value
2682af732203SDimitry Andric   // tracking numbers.
2683af732203SDimitry Andric   if (MF->getFunction().getSubprogram()) {
2684af732203SDimitry Andric     DenseSet<unsigned> SeenNumbers;
2685af732203SDimitry Andric     for (auto &MBB : *MF) {
2686af732203SDimitry Andric       for (auto &MI : MBB) {
2687af732203SDimitry Andric         if (auto Num = MI.peekDebugInstrNum()) {
2688af732203SDimitry Andric           auto Result = SeenNumbers.insert((unsigned)Num);
2689af732203SDimitry Andric           if (!Result.second)
2690af732203SDimitry Andric             report("Instruction has a duplicated value tracking number", &MI);
2691af732203SDimitry Andric         }
2692af732203SDimitry Andric       }
2693af732203SDimitry Andric     }
2694af732203SDimitry Andric   }
26950b57cec5SDimitry Andric }
26960b57cec5SDimitry Andric 
verifyLiveVariables()26970b57cec5SDimitry Andric void MachineVerifier::verifyLiveVariables() {
26980b57cec5SDimitry Andric   assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
2699af732203SDimitry Andric   for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
2700af732203SDimitry Andric     Register Reg = Register::index2VirtReg(I);
27010b57cec5SDimitry Andric     LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
27020b57cec5SDimitry Andric     for (const auto &MBB : *MF) {
27030b57cec5SDimitry Andric       BBInfo &MInfo = MBBInfoMap[&MBB];
27040b57cec5SDimitry Andric 
27050b57cec5SDimitry Andric       // Our vregsRequired should be identical to LiveVariables' AliveBlocks
27060b57cec5SDimitry Andric       if (MInfo.vregsRequired.count(Reg)) {
27070b57cec5SDimitry Andric         if (!VI.AliveBlocks.test(MBB.getNumber())) {
27080b57cec5SDimitry Andric           report("LiveVariables: Block missing from AliveBlocks", &MBB);
27090b57cec5SDimitry Andric           errs() << "Virtual register " << printReg(Reg)
27100b57cec5SDimitry Andric                  << " must be live through the block.\n";
27110b57cec5SDimitry Andric         }
27120b57cec5SDimitry Andric       } else {
27130b57cec5SDimitry Andric         if (VI.AliveBlocks.test(MBB.getNumber())) {
27140b57cec5SDimitry Andric           report("LiveVariables: Block should not be in AliveBlocks", &MBB);
27150b57cec5SDimitry Andric           errs() << "Virtual register " << printReg(Reg)
27160b57cec5SDimitry Andric                  << " is not needed live through the block.\n";
27170b57cec5SDimitry Andric         }
27180b57cec5SDimitry Andric       }
27190b57cec5SDimitry Andric     }
27200b57cec5SDimitry Andric   }
27210b57cec5SDimitry Andric }
27220b57cec5SDimitry Andric 
verifyLiveIntervals()27230b57cec5SDimitry Andric void MachineVerifier::verifyLiveIntervals() {
27240b57cec5SDimitry Andric   assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
2725af732203SDimitry Andric   for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
2726af732203SDimitry Andric     Register Reg = Register::index2VirtReg(I);
27270b57cec5SDimitry Andric 
27280b57cec5SDimitry Andric     // Spilling and splitting may leave unused registers around. Skip them.
27290b57cec5SDimitry Andric     if (MRI->reg_nodbg_empty(Reg))
27300b57cec5SDimitry Andric       continue;
27310b57cec5SDimitry Andric 
27320b57cec5SDimitry Andric     if (!LiveInts->hasInterval(Reg)) {
27330b57cec5SDimitry Andric       report("Missing live interval for virtual register", MF);
27340b57cec5SDimitry Andric       errs() << printReg(Reg, TRI) << " still has defs or uses\n";
27350b57cec5SDimitry Andric       continue;
27360b57cec5SDimitry Andric     }
27370b57cec5SDimitry Andric 
27380b57cec5SDimitry Andric     const LiveInterval &LI = LiveInts->getInterval(Reg);
2739af732203SDimitry Andric     assert(Reg == LI.reg() && "Invalid reg to interval mapping");
27400b57cec5SDimitry Andric     verifyLiveInterval(LI);
27410b57cec5SDimitry Andric   }
27420b57cec5SDimitry Andric 
27430b57cec5SDimitry Andric   // Verify all the cached regunit intervals.
27440b57cec5SDimitry Andric   for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
27450b57cec5SDimitry Andric     if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
27460b57cec5SDimitry Andric       verifyLiveRange(*LR, i);
27470b57cec5SDimitry Andric }
27480b57cec5SDimitry Andric 
verifyLiveRangeValue(const LiveRange & LR,const VNInfo * VNI,Register Reg,LaneBitmask LaneMask)27490b57cec5SDimitry Andric void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
2750af732203SDimitry Andric                                            const VNInfo *VNI, Register Reg,
27510b57cec5SDimitry Andric                                            LaneBitmask LaneMask) {
27520b57cec5SDimitry Andric   if (VNI->isUnused())
27530b57cec5SDimitry Andric     return;
27540b57cec5SDimitry Andric 
27550b57cec5SDimitry Andric   const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
27560b57cec5SDimitry Andric 
27570b57cec5SDimitry Andric   if (!DefVNI) {
27580b57cec5SDimitry Andric     report("Value not live at VNInfo def and not marked unused", MF);
27590b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
27600b57cec5SDimitry Andric     report_context(*VNI);
27610b57cec5SDimitry Andric     return;
27620b57cec5SDimitry Andric   }
27630b57cec5SDimitry Andric 
27640b57cec5SDimitry Andric   if (DefVNI != VNI) {
27650b57cec5SDimitry Andric     report("Live segment at def has different VNInfo", MF);
27660b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
27670b57cec5SDimitry Andric     report_context(*VNI);
27680b57cec5SDimitry Andric     return;
27690b57cec5SDimitry Andric   }
27700b57cec5SDimitry Andric 
27710b57cec5SDimitry Andric   const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
27720b57cec5SDimitry Andric   if (!MBB) {
27730b57cec5SDimitry Andric     report("Invalid VNInfo definition index", MF);
27740b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
27750b57cec5SDimitry Andric     report_context(*VNI);
27760b57cec5SDimitry Andric     return;
27770b57cec5SDimitry Andric   }
27780b57cec5SDimitry Andric 
27790b57cec5SDimitry Andric   if (VNI->isPHIDef()) {
27800b57cec5SDimitry Andric     if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
27810b57cec5SDimitry Andric       report("PHIDef VNInfo is not defined at MBB start", MBB);
27820b57cec5SDimitry Andric       report_context(LR, Reg, LaneMask);
27830b57cec5SDimitry Andric       report_context(*VNI);
27840b57cec5SDimitry Andric     }
27850b57cec5SDimitry Andric     return;
27860b57cec5SDimitry Andric   }
27870b57cec5SDimitry Andric 
27880b57cec5SDimitry Andric   // Non-PHI def.
27890b57cec5SDimitry Andric   const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
27900b57cec5SDimitry Andric   if (!MI) {
27910b57cec5SDimitry Andric     report("No instruction at VNInfo def index", MBB);
27920b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
27930b57cec5SDimitry Andric     report_context(*VNI);
27940b57cec5SDimitry Andric     return;
27950b57cec5SDimitry Andric   }
27960b57cec5SDimitry Andric 
27970b57cec5SDimitry Andric   if (Reg != 0) {
27980b57cec5SDimitry Andric     bool hasDef = false;
27990b57cec5SDimitry Andric     bool isEarlyClobber = false;
28000b57cec5SDimitry Andric     for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
28010b57cec5SDimitry Andric       if (!MOI->isReg() || !MOI->isDef())
28020b57cec5SDimitry Andric         continue;
28038bcb0991SDimitry Andric       if (Register::isVirtualRegister(Reg)) {
28040b57cec5SDimitry Andric         if (MOI->getReg() != Reg)
28050b57cec5SDimitry Andric           continue;
28060b57cec5SDimitry Andric       } else {
28078bcb0991SDimitry Andric         if (!Register::isPhysicalRegister(MOI->getReg()) ||
28080b57cec5SDimitry Andric             !TRI->hasRegUnit(MOI->getReg(), Reg))
28090b57cec5SDimitry Andric           continue;
28100b57cec5SDimitry Andric       }
28110b57cec5SDimitry Andric       if (LaneMask.any() &&
28120b57cec5SDimitry Andric           (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none())
28130b57cec5SDimitry Andric         continue;
28140b57cec5SDimitry Andric       hasDef = true;
28150b57cec5SDimitry Andric       if (MOI->isEarlyClobber())
28160b57cec5SDimitry Andric         isEarlyClobber = true;
28170b57cec5SDimitry Andric     }
28180b57cec5SDimitry Andric 
28190b57cec5SDimitry Andric     if (!hasDef) {
28200b57cec5SDimitry Andric       report("Defining instruction does not modify register", MI);
28210b57cec5SDimitry Andric       report_context(LR, Reg, LaneMask);
28220b57cec5SDimitry Andric       report_context(*VNI);
28230b57cec5SDimitry Andric     }
28240b57cec5SDimitry Andric 
28250b57cec5SDimitry Andric     // Early clobber defs begin at USE slots, but other defs must begin at
28260b57cec5SDimitry Andric     // DEF slots.
28270b57cec5SDimitry Andric     if (isEarlyClobber) {
28280b57cec5SDimitry Andric       if (!VNI->def.isEarlyClobber()) {
28290b57cec5SDimitry Andric         report("Early clobber def must be at an early-clobber slot", MBB);
28300b57cec5SDimitry Andric         report_context(LR, Reg, LaneMask);
28310b57cec5SDimitry Andric         report_context(*VNI);
28320b57cec5SDimitry Andric       }
28330b57cec5SDimitry Andric     } else if (!VNI->def.isRegister()) {
28340b57cec5SDimitry Andric       report("Non-PHI, non-early clobber def must be at a register slot", MBB);
28350b57cec5SDimitry Andric       report_context(LR, Reg, LaneMask);
28360b57cec5SDimitry Andric       report_context(*VNI);
28370b57cec5SDimitry Andric     }
28380b57cec5SDimitry Andric   }
28390b57cec5SDimitry Andric }
28400b57cec5SDimitry Andric 
verifyLiveRangeSegment(const LiveRange & LR,const LiveRange::const_iterator I,Register Reg,LaneBitmask LaneMask)28410b57cec5SDimitry Andric void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
28420b57cec5SDimitry Andric                                              const LiveRange::const_iterator I,
2843af732203SDimitry Andric                                              Register Reg,
2844af732203SDimitry Andric                                              LaneBitmask LaneMask) {
28450b57cec5SDimitry Andric   const LiveRange::Segment &S = *I;
28460b57cec5SDimitry Andric   const VNInfo *VNI = S.valno;
28470b57cec5SDimitry Andric   assert(VNI && "Live segment has no valno");
28480b57cec5SDimitry Andric 
28490b57cec5SDimitry Andric   if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
28500b57cec5SDimitry Andric     report("Foreign valno in live segment", MF);
28510b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
28520b57cec5SDimitry Andric     report_context(S);
28530b57cec5SDimitry Andric     report_context(*VNI);
28540b57cec5SDimitry Andric   }
28550b57cec5SDimitry Andric 
28560b57cec5SDimitry Andric   if (VNI->isUnused()) {
28570b57cec5SDimitry Andric     report("Live segment valno is marked unused", MF);
28580b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
28590b57cec5SDimitry Andric     report_context(S);
28600b57cec5SDimitry Andric   }
28610b57cec5SDimitry Andric 
28620b57cec5SDimitry Andric   const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
28630b57cec5SDimitry Andric   if (!MBB) {
28640b57cec5SDimitry Andric     report("Bad start of live segment, no basic block", MF);
28650b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
28660b57cec5SDimitry Andric     report_context(S);
28670b57cec5SDimitry Andric     return;
28680b57cec5SDimitry Andric   }
28690b57cec5SDimitry Andric   SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
28700b57cec5SDimitry Andric   if (S.start != MBBStartIdx && S.start != VNI->def) {
28710b57cec5SDimitry Andric     report("Live segment must begin at MBB entry or valno def", MBB);
28720b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
28730b57cec5SDimitry Andric     report_context(S);
28740b57cec5SDimitry Andric   }
28750b57cec5SDimitry Andric 
28760b57cec5SDimitry Andric   const MachineBasicBlock *EndMBB =
28770b57cec5SDimitry Andric     LiveInts->getMBBFromIndex(S.end.getPrevSlot());
28780b57cec5SDimitry Andric   if (!EndMBB) {
28790b57cec5SDimitry Andric     report("Bad end of live segment, no basic block", MF);
28800b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
28810b57cec5SDimitry Andric     report_context(S);
28820b57cec5SDimitry Andric     return;
28830b57cec5SDimitry Andric   }
28840b57cec5SDimitry Andric 
28850b57cec5SDimitry Andric   // No more checks for live-out segments.
28860b57cec5SDimitry Andric   if (S.end == LiveInts->getMBBEndIdx(EndMBB))
28870b57cec5SDimitry Andric     return;
28880b57cec5SDimitry Andric 
28890b57cec5SDimitry Andric   // RegUnit intervals are allowed dead phis.
28908bcb0991SDimitry Andric   if (!Register::isVirtualRegister(Reg) && VNI->isPHIDef() &&
28910b57cec5SDimitry Andric       S.start == VNI->def && S.end == VNI->def.getDeadSlot())
28920b57cec5SDimitry Andric     return;
28930b57cec5SDimitry Andric 
28940b57cec5SDimitry Andric   // The live segment is ending inside EndMBB
28950b57cec5SDimitry Andric   const MachineInstr *MI =
28960b57cec5SDimitry Andric     LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
28970b57cec5SDimitry Andric   if (!MI) {
28980b57cec5SDimitry Andric     report("Live segment doesn't end at a valid instruction", EndMBB);
28990b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
29000b57cec5SDimitry Andric     report_context(S);
29010b57cec5SDimitry Andric     return;
29020b57cec5SDimitry Andric   }
29030b57cec5SDimitry Andric 
29040b57cec5SDimitry Andric   // The block slot must refer to a basic block boundary.
29050b57cec5SDimitry Andric   if (S.end.isBlock()) {
29060b57cec5SDimitry Andric     report("Live segment ends at B slot of an instruction", EndMBB);
29070b57cec5SDimitry Andric     report_context(LR, Reg, LaneMask);
29080b57cec5SDimitry Andric     report_context(S);
29090b57cec5SDimitry Andric   }
29100b57cec5SDimitry Andric 
29110b57cec5SDimitry Andric   if (S.end.isDead()) {
29120b57cec5SDimitry Andric     // Segment ends on the dead slot.
29130b57cec5SDimitry Andric     // That means there must be a dead def.
29140b57cec5SDimitry Andric     if (!SlotIndex::isSameInstr(S.start, S.end)) {
29150b57cec5SDimitry Andric       report("Live segment ending at dead slot spans instructions", EndMBB);
29160b57cec5SDimitry Andric       report_context(LR, Reg, LaneMask);
29170b57cec5SDimitry Andric       report_context(S);
29180b57cec5SDimitry Andric     }
29190b57cec5SDimitry Andric   }
29200b57cec5SDimitry Andric 
29210b57cec5SDimitry Andric   // A live segment can only end at an early-clobber slot if it is being
29220b57cec5SDimitry Andric   // redefined by an early-clobber def.
29230b57cec5SDimitry Andric   if (S.end.isEarlyClobber()) {
29240b57cec5SDimitry Andric     if (I+1 == LR.end() || (I+1)->start != S.end) {
29250b57cec5SDimitry Andric       report("Live segment ending at early clobber slot must be "
29260b57cec5SDimitry Andric              "redefined by an EC def in the same instruction", EndMBB);
29270b57cec5SDimitry Andric       report_context(LR, Reg, LaneMask);
29280b57cec5SDimitry Andric       report_context(S);
29290b57cec5SDimitry Andric     }
29300b57cec5SDimitry Andric   }
29310b57cec5SDimitry Andric 
29320b57cec5SDimitry Andric   // The following checks only apply to virtual registers. Physreg liveness
29330b57cec5SDimitry Andric   // is too weird to check.
29348bcb0991SDimitry Andric   if (Register::isVirtualRegister(Reg)) {
29350b57cec5SDimitry Andric     // A live segment can end with either a redefinition, a kill flag on a
29360b57cec5SDimitry Andric     // use, or a dead flag on a def.
29370b57cec5SDimitry Andric     bool hasRead = false;
29380b57cec5SDimitry Andric     bool hasSubRegDef = false;
29390b57cec5SDimitry Andric     bool hasDeadDef = false;
29400b57cec5SDimitry Andric     for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
29410b57cec5SDimitry Andric       if (!MOI->isReg() || MOI->getReg() != Reg)
29420b57cec5SDimitry Andric         continue;
29430b57cec5SDimitry Andric       unsigned Sub = MOI->getSubReg();
29440b57cec5SDimitry Andric       LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub)
29450b57cec5SDimitry Andric                                  : LaneBitmask::getAll();
29460b57cec5SDimitry Andric       if (MOI->isDef()) {
29470b57cec5SDimitry Andric         if (Sub != 0) {
29480b57cec5SDimitry Andric           hasSubRegDef = true;
29490b57cec5SDimitry Andric           // An operand %0:sub0 reads %0:sub1..n. Invert the lane
29500b57cec5SDimitry Andric           // mask for subregister defs. Read-undef defs will be handled by
29510b57cec5SDimitry Andric           // readsReg below.
29520b57cec5SDimitry Andric           SLM = ~SLM;
29530b57cec5SDimitry Andric         }
29540b57cec5SDimitry Andric         if (MOI->isDead())
29550b57cec5SDimitry Andric           hasDeadDef = true;
29560b57cec5SDimitry Andric       }
29570b57cec5SDimitry Andric       if (LaneMask.any() && (LaneMask & SLM).none())
29580b57cec5SDimitry Andric         continue;
29590b57cec5SDimitry Andric       if (MOI->readsReg())
29600b57cec5SDimitry Andric         hasRead = true;
29610b57cec5SDimitry Andric     }
29620b57cec5SDimitry Andric     if (S.end.isDead()) {
29630b57cec5SDimitry Andric       // Make sure that the corresponding machine operand for a "dead" live
29640b57cec5SDimitry Andric       // range has the dead flag. We cannot perform this check for subregister
29650b57cec5SDimitry Andric       // liveranges as partially dead values are allowed.
29660b57cec5SDimitry Andric       if (LaneMask.none() && !hasDeadDef) {
29670b57cec5SDimitry Andric         report("Instruction ending live segment on dead slot has no dead flag",
29680b57cec5SDimitry Andric                MI);
29690b57cec5SDimitry Andric         report_context(LR, Reg, LaneMask);
29700b57cec5SDimitry Andric         report_context(S);
29710b57cec5SDimitry Andric       }
29720b57cec5SDimitry Andric     } else {
29730b57cec5SDimitry Andric       if (!hasRead) {
29740b57cec5SDimitry Andric         // When tracking subregister liveness, the main range must start new
29750b57cec5SDimitry Andric         // values on partial register writes, even if there is no read.
29760b57cec5SDimitry Andric         if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() ||
29770b57cec5SDimitry Andric             !hasSubRegDef) {
29780b57cec5SDimitry Andric           report("Instruction ending live segment doesn't read the register",
29790b57cec5SDimitry Andric                  MI);
29800b57cec5SDimitry Andric           report_context(LR, Reg, LaneMask);
29810b57cec5SDimitry Andric           report_context(S);
29820b57cec5SDimitry Andric         }
29830b57cec5SDimitry Andric       }
29840b57cec5SDimitry Andric     }
29850b57cec5SDimitry Andric   }
29860b57cec5SDimitry Andric 
29870b57cec5SDimitry Andric   // Now check all the basic blocks in this live segment.
29880b57cec5SDimitry Andric   MachineFunction::const_iterator MFI = MBB->getIterator();
29890b57cec5SDimitry Andric   // Is this live segment the beginning of a non-PHIDef VN?
29900b57cec5SDimitry Andric   if (S.start == VNI->def && !VNI->isPHIDef()) {
29910b57cec5SDimitry Andric     // Not live-in to any blocks.
29920b57cec5SDimitry Andric     if (MBB == EndMBB)
29930b57cec5SDimitry Andric       return;
29940b57cec5SDimitry Andric     // Skip this block.
29950b57cec5SDimitry Andric     ++MFI;
29960b57cec5SDimitry Andric   }
29970b57cec5SDimitry Andric 
29980b57cec5SDimitry Andric   SmallVector<SlotIndex, 4> Undefs;
29990b57cec5SDimitry Andric   if (LaneMask.any()) {
30000b57cec5SDimitry Andric     LiveInterval &OwnerLI = LiveInts->getInterval(Reg);
30010b57cec5SDimitry Andric     OwnerLI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes);
30020b57cec5SDimitry Andric   }
30030b57cec5SDimitry Andric 
30040b57cec5SDimitry Andric   while (true) {
30050b57cec5SDimitry Andric     assert(LiveInts->isLiveInToMBB(LR, &*MFI));
30060b57cec5SDimitry Andric     // We don't know how to track physregs into a landing pad.
30078bcb0991SDimitry Andric     if (!Register::isVirtualRegister(Reg) && MFI->isEHPad()) {
30080b57cec5SDimitry Andric       if (&*MFI == EndMBB)
30090b57cec5SDimitry Andric         break;
30100b57cec5SDimitry Andric       ++MFI;
30110b57cec5SDimitry Andric       continue;
30120b57cec5SDimitry Andric     }
30130b57cec5SDimitry Andric 
30140b57cec5SDimitry Andric     // Is VNI a PHI-def in the current block?
30150b57cec5SDimitry Andric     bool IsPHI = VNI->isPHIDef() &&
30160b57cec5SDimitry Andric       VNI->def == LiveInts->getMBBStartIdx(&*MFI);
30170b57cec5SDimitry Andric 
30180b57cec5SDimitry Andric     // Check that VNI is live-out of all predecessors.
30195ffd83dbSDimitry Andric     for (const MachineBasicBlock *Pred : MFI->predecessors()) {
30205ffd83dbSDimitry Andric       SlotIndex PEnd = LiveInts->getMBBEndIdx(Pred);
3021*5f7ddb14SDimitry Andric       // Predecessor of landing pad live-out on last call.
3022*5f7ddb14SDimitry Andric       if (MFI->isEHPad()) {
3023*5f7ddb14SDimitry Andric         for (auto I = Pred->rbegin(), E = Pred->rend(); I != E; ++I) {
3024*5f7ddb14SDimitry Andric           if (I->isCall()) {
3025*5f7ddb14SDimitry Andric             PEnd = Indexes->getInstructionIndex(*I).getBoundaryIndex();
3026*5f7ddb14SDimitry Andric             break;
3027*5f7ddb14SDimitry Andric           }
3028*5f7ddb14SDimitry Andric         }
3029*5f7ddb14SDimitry Andric       }
30300b57cec5SDimitry Andric       const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
30310b57cec5SDimitry Andric 
30320b57cec5SDimitry Andric       // All predecessors must have a live-out value. However for a phi
30330b57cec5SDimitry Andric       // instruction with subregister intervals
30340b57cec5SDimitry Andric       // only one of the subregisters (not necessarily the current one) needs to
30350b57cec5SDimitry Andric       // be defined.
30360b57cec5SDimitry Andric       if (!PVNI && (LaneMask.none() || !IsPHI)) {
30375ffd83dbSDimitry Andric         if (LiveRangeCalc::isJointlyDominated(Pred, Undefs, *Indexes))
30380b57cec5SDimitry Andric           continue;
30395ffd83dbSDimitry Andric         report("Register not marked live out of predecessor", Pred);
30400b57cec5SDimitry Andric         report_context(LR, Reg, LaneMask);
30410b57cec5SDimitry Andric         report_context(*VNI);
30420b57cec5SDimitry Andric         errs() << " live into " << printMBBReference(*MFI) << '@'
30430b57cec5SDimitry Andric                << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
30440b57cec5SDimitry Andric                << PEnd << '\n';
30450b57cec5SDimitry Andric         continue;
30460b57cec5SDimitry Andric       }
30470b57cec5SDimitry Andric 
30480b57cec5SDimitry Andric       // Only PHI-defs can take different predecessor values.
30490b57cec5SDimitry Andric       if (!IsPHI && PVNI != VNI) {
30505ffd83dbSDimitry Andric         report("Different value live out of predecessor", Pred);
30510b57cec5SDimitry Andric         report_context(LR, Reg, LaneMask);
30520b57cec5SDimitry Andric         errs() << "Valno #" << PVNI->id << " live out of "
30535ffd83dbSDimitry Andric                << printMBBReference(*Pred) << '@' << PEnd << "\nValno #"
30540b57cec5SDimitry Andric                << VNI->id << " live into " << printMBBReference(*MFI) << '@'
30550b57cec5SDimitry Andric                << LiveInts->getMBBStartIdx(&*MFI) << '\n';
30560b57cec5SDimitry Andric       }
30570b57cec5SDimitry Andric     }
30580b57cec5SDimitry Andric     if (&*MFI == EndMBB)
30590b57cec5SDimitry Andric       break;
30600b57cec5SDimitry Andric     ++MFI;
30610b57cec5SDimitry Andric   }
30620b57cec5SDimitry Andric }
30630b57cec5SDimitry Andric 
verifyLiveRange(const LiveRange & LR,Register Reg,LaneBitmask LaneMask)3064af732203SDimitry Andric void MachineVerifier::verifyLiveRange(const LiveRange &LR, Register Reg,
30650b57cec5SDimitry Andric                                       LaneBitmask LaneMask) {
30660b57cec5SDimitry Andric   for (const VNInfo *VNI : LR.valnos)
30670b57cec5SDimitry Andric     verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
30680b57cec5SDimitry Andric 
30690b57cec5SDimitry Andric   for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
30700b57cec5SDimitry Andric     verifyLiveRangeSegment(LR, I, Reg, LaneMask);
30710b57cec5SDimitry Andric }
30720b57cec5SDimitry Andric 
verifyLiveInterval(const LiveInterval & LI)30730b57cec5SDimitry Andric void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
3074af732203SDimitry Andric   Register Reg = LI.reg();
30758bcb0991SDimitry Andric   assert(Register::isVirtualRegister(Reg));
30760b57cec5SDimitry Andric   verifyLiveRange(LI, Reg);
30770b57cec5SDimitry Andric 
30780b57cec5SDimitry Andric   LaneBitmask Mask;
30790b57cec5SDimitry Andric   LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
30800b57cec5SDimitry Andric   for (const LiveInterval::SubRange &SR : LI.subranges()) {
30810b57cec5SDimitry Andric     if ((Mask & SR.LaneMask).any()) {
30820b57cec5SDimitry Andric       report("Lane masks of sub ranges overlap in live interval", MF);
30830b57cec5SDimitry Andric       report_context(LI);
30840b57cec5SDimitry Andric     }
30850b57cec5SDimitry Andric     if ((SR.LaneMask & ~MaxMask).any()) {
30860b57cec5SDimitry Andric       report("Subrange lanemask is invalid", MF);
30870b57cec5SDimitry Andric       report_context(LI);
30880b57cec5SDimitry Andric     }
30890b57cec5SDimitry Andric     if (SR.empty()) {
30900b57cec5SDimitry Andric       report("Subrange must not be empty", MF);
3091af732203SDimitry Andric       report_context(SR, LI.reg(), SR.LaneMask);
30920b57cec5SDimitry Andric     }
30930b57cec5SDimitry Andric     Mask |= SR.LaneMask;
3094af732203SDimitry Andric     verifyLiveRange(SR, LI.reg(), SR.LaneMask);
30950b57cec5SDimitry Andric     if (!LI.covers(SR)) {
30960b57cec5SDimitry Andric       report("A Subrange is not covered by the main range", MF);
30970b57cec5SDimitry Andric       report_context(LI);
30980b57cec5SDimitry Andric     }
30990b57cec5SDimitry Andric   }
31000b57cec5SDimitry Andric 
31010b57cec5SDimitry Andric   // Check the LI only has one connected component.
31020b57cec5SDimitry Andric   ConnectedVNInfoEqClasses ConEQ(*LiveInts);
31030b57cec5SDimitry Andric   unsigned NumComp = ConEQ.Classify(LI);
31040b57cec5SDimitry Andric   if (NumComp > 1) {
31050b57cec5SDimitry Andric     report("Multiple connected components in live interval", MF);
31060b57cec5SDimitry Andric     report_context(LI);
31070b57cec5SDimitry Andric     for (unsigned comp = 0; comp != NumComp; ++comp) {
31080b57cec5SDimitry Andric       errs() << comp << ": valnos";
31095ffd83dbSDimitry Andric       for (const VNInfo *I : LI.valnos)
31105ffd83dbSDimitry Andric         if (comp == ConEQ.getEqClass(I))
31115ffd83dbSDimitry Andric           errs() << ' ' << I->id;
31120b57cec5SDimitry Andric       errs() << '\n';
31130b57cec5SDimitry Andric     }
31140b57cec5SDimitry Andric   }
31150b57cec5SDimitry Andric }
31160b57cec5SDimitry Andric 
31170b57cec5SDimitry Andric namespace {
31180b57cec5SDimitry Andric 
31190b57cec5SDimitry Andric   // FrameSetup and FrameDestroy can have zero adjustment, so using a single
31200b57cec5SDimitry Andric   // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
31210b57cec5SDimitry Andric   // value is zero.
31220b57cec5SDimitry Andric   // We use a bool plus an integer to capture the stack state.
31230b57cec5SDimitry Andric   struct StackStateOfBB {
31240b57cec5SDimitry Andric     StackStateOfBB() = default;
StackStateOfBB__anon86168b390511::StackStateOfBB31250b57cec5SDimitry Andric     StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
31260b57cec5SDimitry Andric       EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
31270b57cec5SDimitry Andric       ExitIsSetup(ExitSetup) {}
31280b57cec5SDimitry Andric 
31290b57cec5SDimitry Andric     // Can be negative, which means we are setting up a frame.
31300b57cec5SDimitry Andric     int EntryValue = 0;
31310b57cec5SDimitry Andric     int ExitValue = 0;
31320b57cec5SDimitry Andric     bool EntryIsSetup = false;
31330b57cec5SDimitry Andric     bool ExitIsSetup = false;
31340b57cec5SDimitry Andric   };
31350b57cec5SDimitry Andric 
31360b57cec5SDimitry Andric } // end anonymous namespace
31370b57cec5SDimitry Andric 
31380b57cec5SDimitry Andric /// Make sure on every path through the CFG, a FrameSetup <n> is always followed
31390b57cec5SDimitry Andric /// by a FrameDestroy <n>, stack adjustments are identical on all
31400b57cec5SDimitry Andric /// CFG edges to a merge point, and frame is destroyed at end of a return block.
verifyStackFrame()31410b57cec5SDimitry Andric void MachineVerifier::verifyStackFrame() {
31420b57cec5SDimitry Andric   unsigned FrameSetupOpcode   = TII->getCallFrameSetupOpcode();
31430b57cec5SDimitry Andric   unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
31440b57cec5SDimitry Andric   if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u)
31450b57cec5SDimitry Andric     return;
31460b57cec5SDimitry Andric 
31470b57cec5SDimitry Andric   SmallVector<StackStateOfBB, 8> SPState;
31480b57cec5SDimitry Andric   SPState.resize(MF->getNumBlockIDs());
31490b57cec5SDimitry Andric   df_iterator_default_set<const MachineBasicBlock*> Reachable;
31500b57cec5SDimitry Andric 
31510b57cec5SDimitry Andric   // Visit the MBBs in DFS order.
31520b57cec5SDimitry Andric   for (df_ext_iterator<const MachineFunction *,
31530b57cec5SDimitry Andric                        df_iterator_default_set<const MachineBasicBlock *>>
31540b57cec5SDimitry Andric        DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
31550b57cec5SDimitry Andric        DFI != DFE; ++DFI) {
31560b57cec5SDimitry Andric     const MachineBasicBlock *MBB = *DFI;
31570b57cec5SDimitry Andric 
31580b57cec5SDimitry Andric     StackStateOfBB BBState;
31590b57cec5SDimitry Andric     // Check the exit state of the DFS stack predecessor.
31600b57cec5SDimitry Andric     if (DFI.getPathLength() >= 2) {
31610b57cec5SDimitry Andric       const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
31620b57cec5SDimitry Andric       assert(Reachable.count(StackPred) &&
31630b57cec5SDimitry Andric              "DFS stack predecessor is already visited.\n");
31640b57cec5SDimitry Andric       BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
31650b57cec5SDimitry Andric       BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
31660b57cec5SDimitry Andric       BBState.ExitValue = BBState.EntryValue;
31670b57cec5SDimitry Andric       BBState.ExitIsSetup = BBState.EntryIsSetup;
31680b57cec5SDimitry Andric     }
31690b57cec5SDimitry Andric 
31700b57cec5SDimitry Andric     // Update stack state by checking contents of MBB.
31710b57cec5SDimitry Andric     for (const auto &I : *MBB) {
31720b57cec5SDimitry Andric       if (I.getOpcode() == FrameSetupOpcode) {
31730b57cec5SDimitry Andric         if (BBState.ExitIsSetup)
31740b57cec5SDimitry Andric           report("FrameSetup is after another FrameSetup", &I);
31750b57cec5SDimitry Andric         BBState.ExitValue -= TII->getFrameTotalSize(I);
31760b57cec5SDimitry Andric         BBState.ExitIsSetup = true;
31770b57cec5SDimitry Andric       }
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric       if (I.getOpcode() == FrameDestroyOpcode) {
31800b57cec5SDimitry Andric         int Size = TII->getFrameTotalSize(I);
31810b57cec5SDimitry Andric         if (!BBState.ExitIsSetup)
31820b57cec5SDimitry Andric           report("FrameDestroy is not after a FrameSetup", &I);
31830b57cec5SDimitry Andric         int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
31840b57cec5SDimitry Andric                                                BBState.ExitValue;
31850b57cec5SDimitry Andric         if (BBState.ExitIsSetup && AbsSPAdj != Size) {
31860b57cec5SDimitry Andric           report("FrameDestroy <n> is after FrameSetup <m>", &I);
31870b57cec5SDimitry Andric           errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
31880b57cec5SDimitry Andric               << AbsSPAdj << ">.\n";
31890b57cec5SDimitry Andric         }
31900b57cec5SDimitry Andric         BBState.ExitValue += Size;
31910b57cec5SDimitry Andric         BBState.ExitIsSetup = false;
31920b57cec5SDimitry Andric       }
31930b57cec5SDimitry Andric     }
31940b57cec5SDimitry Andric     SPState[MBB->getNumber()] = BBState;
31950b57cec5SDimitry Andric 
31960b57cec5SDimitry Andric     // Make sure the exit state of any predecessor is consistent with the entry
31970b57cec5SDimitry Andric     // state.
31985ffd83dbSDimitry Andric     for (const MachineBasicBlock *Pred : MBB->predecessors()) {
31995ffd83dbSDimitry Andric       if (Reachable.count(Pred) &&
32005ffd83dbSDimitry Andric           (SPState[Pred->getNumber()].ExitValue != BBState.EntryValue ||
32015ffd83dbSDimitry Andric            SPState[Pred->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
32020b57cec5SDimitry Andric         report("The exit stack state of a predecessor is inconsistent.", MBB);
32035ffd83dbSDimitry Andric         errs() << "Predecessor " << printMBBReference(*Pred)
32045ffd83dbSDimitry Andric                << " has exit state (" << SPState[Pred->getNumber()].ExitValue
32055ffd83dbSDimitry Andric                << ", " << SPState[Pred->getNumber()].ExitIsSetup << "), while "
32060b57cec5SDimitry Andric                << printMBBReference(*MBB) << " has entry state ("
32070b57cec5SDimitry Andric                << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
32080b57cec5SDimitry Andric       }
32090b57cec5SDimitry Andric     }
32100b57cec5SDimitry Andric 
32110b57cec5SDimitry Andric     // Make sure the entry state of any successor is consistent with the exit
32120b57cec5SDimitry Andric     // state.
32135ffd83dbSDimitry Andric     for (const MachineBasicBlock *Succ : MBB->successors()) {
32145ffd83dbSDimitry Andric       if (Reachable.count(Succ) &&
32155ffd83dbSDimitry Andric           (SPState[Succ->getNumber()].EntryValue != BBState.ExitValue ||
32165ffd83dbSDimitry Andric            SPState[Succ->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
32170b57cec5SDimitry Andric         report("The entry stack state of a successor is inconsistent.", MBB);
32185ffd83dbSDimitry Andric         errs() << "Successor " << printMBBReference(*Succ)
32195ffd83dbSDimitry Andric                << " has entry state (" << SPState[Succ->getNumber()].EntryValue
32205ffd83dbSDimitry Andric                << ", " << SPState[Succ->getNumber()].EntryIsSetup << "), while "
32210b57cec5SDimitry Andric                << printMBBReference(*MBB) << " has exit state ("
32220b57cec5SDimitry Andric                << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
32230b57cec5SDimitry Andric       }
32240b57cec5SDimitry Andric     }
32250b57cec5SDimitry Andric 
32260b57cec5SDimitry Andric     // Make sure a basic block with return ends with zero stack adjustment.
32270b57cec5SDimitry Andric     if (!MBB->empty() && MBB->back().isReturn()) {
32280b57cec5SDimitry Andric       if (BBState.ExitIsSetup)
32290b57cec5SDimitry Andric         report("A return block ends with a FrameSetup.", MBB);
32300b57cec5SDimitry Andric       if (BBState.ExitValue)
32310b57cec5SDimitry Andric         report("A return block ends with a nonzero stack adjustment.", MBB);
32320b57cec5SDimitry Andric     }
32330b57cec5SDimitry Andric   }
32340b57cec5SDimitry Andric }
3235