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 23*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineVerifier.h" 240b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 250b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 260b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 270b57cec5SDimitry Andric #include "llvm/ADT/DepthFirstIterator.h" 285ffd83dbSDimitry Andric #include "llvm/ADT/PostOrderIterator.h" 290b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 300b57cec5SDimitry Andric #include "llvm/ADT/SetOperations.h" 310b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 320b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 330b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 340b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 3581ad6265SDimitry Andric #include "llvm/CodeGen/CodeGenCommonISel.h" 365f757f3fSDimitry Andric #include "llvm/CodeGen/GlobalISel/GenericMachineInstrs.h" 370b57cec5SDimitry Andric #include "llvm/CodeGen/LiveInterval.h" 380b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 3981ad6265SDimitry Andric #include "llvm/CodeGen/LiveRangeCalc.h" 400b57cec5SDimitry Andric #include "llvm/CodeGen/LiveStacks.h" 410b57cec5SDimitry Andric #include "llvm/CodeGen/LiveVariables.h" 420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 43*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineConvergenceVerifier.h" 44*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 450b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 460b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 470b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 480b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 490b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBundle.h" 500b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 510b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 520b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 530b57cec5SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h" 5481ad6265SDimitry Andric #include "llvm/CodeGen/RegisterBank.h" 5581ad6265SDimitry Andric #include "llvm/CodeGen/RegisterBankInfo.h" 560b57cec5SDimitry Andric #include "llvm/CodeGen/SlotIndexes.h" 570b57cec5SDimitry Andric #include "llvm/CodeGen/StackMaps.h" 580b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 59*0fca6ea1SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 600b57cec5SDimitry Andric #include "llvm/CodeGen/TargetOpcodes.h" 610b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 620b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 63*0fca6ea1SDimitry Andric #include "llvm/CodeGenTypes/LowLevelType.h" 640b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 6581ad6265SDimitry Andric #include "llvm/IR/Constants.h" 6606c3fb27SDimitry Andric #include "llvm/IR/EHPersonalities.h" 670b57cec5SDimitry Andric #include "llvm/IR/Function.h" 680b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h" 690b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 70480093f4SDimitry Andric #include "llvm/InitializePasses.h" 710b57cec5SDimitry Andric #include "llvm/MC/LaneBitmask.h" 720b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 7381ad6265SDimitry Andric #include "llvm/MC/MCDwarf.h" 740b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h" 750b57cec5SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 760b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 770b57cec5SDimitry Andric #include "llvm/Pass.h" 780b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 790b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 800b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 81bdd1243dSDimitry Andric #include "llvm/Support/ModRef.h" 820b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 830b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 840b57cec5SDimitry Andric #include <algorithm> 850b57cec5SDimitry Andric #include <cassert> 860b57cec5SDimitry Andric #include <cstddef> 870b57cec5SDimitry Andric #include <cstdint> 880b57cec5SDimitry Andric #include <iterator> 890b57cec5SDimitry Andric #include <string> 900b57cec5SDimitry Andric #include <utility> 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric using namespace llvm; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric namespace { 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric struct MachineVerifier { 97*0fca6ea1SDimitry Andric MachineVerifier(MachineFunctionAnalysisManager &MFAM, const char *b) 98*0fca6ea1SDimitry Andric : MFAM(&MFAM), Banner(b) {} 99*0fca6ea1SDimitry Andric 1000b57cec5SDimitry Andric MachineVerifier(Pass *pass, const char *b) : PASS(pass), Banner(b) {} 1010b57cec5SDimitry Andric 1025f757f3fSDimitry Andric MachineVerifier(const char *b, LiveVariables *LiveVars, 1035f757f3fSDimitry Andric LiveIntervals *LiveInts, LiveStacks *LiveStks, 1045f757f3fSDimitry Andric SlotIndexes *Indexes) 1055f757f3fSDimitry Andric : Banner(b), LiveVars(LiveVars), LiveInts(LiveInts), LiveStks(LiveStks), 1065f757f3fSDimitry Andric Indexes(Indexes) {} 1075f757f3fSDimitry Andric 108e8d8bef9SDimitry Andric unsigned verify(const MachineFunction &MF); 1090b57cec5SDimitry Andric 110*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager *MFAM = nullptr; 1115f757f3fSDimitry Andric Pass *const PASS = nullptr; 1120b57cec5SDimitry Andric const char *Banner; 11306c3fb27SDimitry Andric const MachineFunction *MF = nullptr; 11406c3fb27SDimitry Andric const TargetMachine *TM = nullptr; 11506c3fb27SDimitry Andric const TargetInstrInfo *TII = nullptr; 11606c3fb27SDimitry Andric const TargetRegisterInfo *TRI = nullptr; 11706c3fb27SDimitry Andric const MachineRegisterInfo *MRI = nullptr; 11806c3fb27SDimitry Andric const RegisterBankInfo *RBI = nullptr; 1190b57cec5SDimitry Andric 12006c3fb27SDimitry Andric unsigned foundErrors = 0; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric // Avoid querying the MachineFunctionProperties for each operand. 12306c3fb27SDimitry Andric bool isFunctionRegBankSelected = false; 12406c3fb27SDimitry Andric bool isFunctionSelected = false; 12506c3fb27SDimitry Andric bool isFunctionTracksDebugUserValues = false; 1260b57cec5SDimitry Andric 127e8d8bef9SDimitry Andric using RegVector = SmallVector<Register, 16>; 1280b57cec5SDimitry Andric using RegMaskVector = SmallVector<const uint32_t *, 4>; 129e8d8bef9SDimitry Andric using RegSet = DenseSet<Register>; 130e8d8bef9SDimitry Andric using RegMap = DenseMap<Register, const MachineInstr *>; 1310b57cec5SDimitry Andric using BlockSet = SmallPtrSet<const MachineBasicBlock *, 8>; 1320b57cec5SDimitry Andric 13306c3fb27SDimitry Andric const MachineInstr *FirstNonPHI = nullptr; 13406c3fb27SDimitry Andric const MachineInstr *FirstTerminator = nullptr; 1350b57cec5SDimitry Andric BlockSet FunctionBlocks; 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric BitVector regsReserved; 1380b57cec5SDimitry Andric RegSet regsLive; 1390b57cec5SDimitry Andric RegVector regsDefined, regsDead, regsKilled; 1400b57cec5SDimitry Andric RegMaskVector regMasks; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric SlotIndex lastIndex; 1430b57cec5SDimitry Andric 1440b57cec5SDimitry Andric // Add Reg and any sub-registers to RV 145e8d8bef9SDimitry Andric void addRegWithSubRegs(RegVector &RV, Register Reg) { 1460b57cec5SDimitry Andric RV.push_back(Reg); 147e8d8bef9SDimitry Andric if (Reg.isPhysical()) 148e8d8bef9SDimitry Andric append_range(RV, TRI->subregs(Reg.asMCReg())); 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric struct BBInfo { 1520b57cec5SDimitry Andric // Is this MBB reachable from the MF entry point? 1530b57cec5SDimitry Andric bool reachable = false; 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric // Vregs that must be live in because they are used without being 156e8d8bef9SDimitry Andric // defined. Map value is the user. vregsLiveIn doesn't include regs 157e8d8bef9SDimitry Andric // that only are used by PHI nodes. 1580b57cec5SDimitry Andric RegMap vregsLiveIn; 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric // Regs killed in MBB. They may be defined again, and will then be in both 1610b57cec5SDimitry Andric // regsKilled and regsLiveOut. 1620b57cec5SDimitry Andric RegSet regsKilled; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric // Regs defined in MBB and live out. Note that vregs passing through may 1650b57cec5SDimitry Andric // be live out without being mentioned here. 1660b57cec5SDimitry Andric RegSet regsLiveOut; 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric // Vregs that pass through MBB untouched. This set is disjoint from 1690b57cec5SDimitry Andric // regsKilled and regsLiveOut. 1700b57cec5SDimitry Andric RegSet vregsPassed; 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Vregs that must pass through MBB because they are needed by a successor 1730b57cec5SDimitry Andric // block. This set is disjoint from regsLiveOut. 1740b57cec5SDimitry Andric RegSet vregsRequired; 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric // Set versions of block's predecessor and successor lists. 1770b57cec5SDimitry Andric BlockSet Preds, Succs; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric BBInfo() = default; 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric // Add register to vregsRequired if it belongs there. Return true if 1820b57cec5SDimitry Andric // anything changed. 183e8d8bef9SDimitry Andric bool addRequired(Register Reg) { 184e8d8bef9SDimitry Andric if (!Reg.isVirtual()) 1850b57cec5SDimitry Andric return false; 1860b57cec5SDimitry Andric if (regsLiveOut.count(Reg)) 1870b57cec5SDimitry Andric return false; 1880b57cec5SDimitry Andric return vregsRequired.insert(Reg).second; 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric // Same for a full set. 1920b57cec5SDimitry Andric bool addRequired(const RegSet &RS) { 1935ffd83dbSDimitry Andric bool Changed = false; 194e8d8bef9SDimitry Andric for (Register Reg : RS) 1955ffd83dbSDimitry Andric Changed |= addRequired(Reg); 1965ffd83dbSDimitry Andric return Changed; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // Same for a full map. 2000b57cec5SDimitry Andric bool addRequired(const RegMap &RM) { 2015ffd83dbSDimitry Andric bool Changed = false; 2025ffd83dbSDimitry Andric for (const auto &I : RM) 2035ffd83dbSDimitry Andric Changed |= addRequired(I.first); 2045ffd83dbSDimitry Andric return Changed; 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric // Live-out registers are either in regsLiveOut or vregsPassed. 208e8d8bef9SDimitry Andric bool isLiveOut(Register Reg) const { 2090b57cec5SDimitry Andric return regsLiveOut.count(Reg) || vregsPassed.count(Reg); 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric }; 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric // Extra register info per MBB. 2140b57cec5SDimitry Andric DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap; 2150b57cec5SDimitry Andric 216e8d8bef9SDimitry Andric bool isReserved(Register Reg) { 217e8d8bef9SDimitry Andric return Reg.id() < regsReserved.size() && regsReserved.test(Reg.id()); 2180b57cec5SDimitry Andric } 2190b57cec5SDimitry Andric 220e8d8bef9SDimitry Andric bool isAllocatable(Register Reg) const { 221e8d8bef9SDimitry Andric return Reg.id() < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) && 222e8d8bef9SDimitry Andric !regsReserved.test(Reg.id()); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric // Analysis information if available 22606c3fb27SDimitry Andric LiveVariables *LiveVars = nullptr; 22706c3fb27SDimitry Andric LiveIntervals *LiveInts = nullptr; 22806c3fb27SDimitry Andric LiveStacks *LiveStks = nullptr; 22906c3fb27SDimitry Andric SlotIndexes *Indexes = nullptr; 2300b57cec5SDimitry Andric 231*0fca6ea1SDimitry Andric // This is calculated only when trying to verify convergence control tokens. 232*0fca6ea1SDimitry Andric // Similar to the LLVM IR verifier, we calculate this locally instead of 233*0fca6ea1SDimitry Andric // relying on the pass manager. 234*0fca6ea1SDimitry Andric MachineDominatorTree DT; 235*0fca6ea1SDimitry Andric 2360b57cec5SDimitry Andric void visitMachineFunctionBefore(); 2370b57cec5SDimitry Andric void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB); 2380b57cec5SDimitry Andric void visitMachineBundleBefore(const MachineInstr *MI); 2390b57cec5SDimitry Andric 240349cc55cSDimitry Andric /// Verify that all of \p MI's virtual register operands are scalars. 241349cc55cSDimitry Andric /// \returns True if all virtual register operands are scalar. False 242349cc55cSDimitry Andric /// otherwise. 243349cc55cSDimitry Andric bool verifyAllRegOpsScalar(const MachineInstr &MI, 244349cc55cSDimitry Andric const MachineRegisterInfo &MRI); 2450b57cec5SDimitry Andric bool verifyVectorElementMatch(LLT Ty0, LLT Ty1, const MachineInstr *MI); 2465f757f3fSDimitry Andric 2475f757f3fSDimitry Andric bool verifyGIntrinsicSideEffects(const MachineInstr *MI); 2485f757f3fSDimitry Andric bool verifyGIntrinsicConvergence(const MachineInstr *MI); 2490b57cec5SDimitry Andric void verifyPreISelGenericInstruction(const MachineInstr *MI); 2505f757f3fSDimitry Andric 2510b57cec5SDimitry Andric void visitMachineInstrBefore(const MachineInstr *MI); 2520b57cec5SDimitry Andric void visitMachineOperand(const MachineOperand *MO, unsigned MONum); 2530b57cec5SDimitry Andric void visitMachineBundleAfter(const MachineInstr *MI); 2540b57cec5SDimitry Andric void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB); 2550b57cec5SDimitry Andric void visitMachineFunctionAfter(); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric void report(const char *msg, const MachineFunction *MF); 2580b57cec5SDimitry Andric void report(const char *msg, const MachineBasicBlock *MBB); 2590b57cec5SDimitry Andric void report(const char *msg, const MachineInstr *MI); 2600b57cec5SDimitry Andric void report(const char *msg, const MachineOperand *MO, unsigned MONum, 2610b57cec5SDimitry Andric LLT MOVRegType = LLT{}); 262fe6060f1SDimitry Andric void report(const Twine &Msg, const MachineInstr *MI); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric void report_context(const LiveInterval &LI) const; 265e8d8bef9SDimitry Andric void report_context(const LiveRange &LR, Register VRegUnit, 2660b57cec5SDimitry Andric LaneBitmask LaneMask) const; 2670b57cec5SDimitry Andric void report_context(const LiveRange::Segment &S) const; 2680b57cec5SDimitry Andric void report_context(const VNInfo &VNI) const; 2690b57cec5SDimitry Andric void report_context(SlotIndex Pos) const; 2700b57cec5SDimitry Andric void report_context(MCPhysReg PhysReg) const; 2710b57cec5SDimitry Andric void report_context_liverange(const LiveRange &LR) const; 2720b57cec5SDimitry Andric void report_context_lanemask(LaneBitmask LaneMask) const; 273e8d8bef9SDimitry Andric void report_context_vreg(Register VReg) const; 274e8d8bef9SDimitry Andric void report_context_vreg_regunit(Register VRegOrUnit) const; 2750b57cec5SDimitry Andric 2760b57cec5SDimitry Andric void verifyInlineAsm(const MachineInstr *MI); 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric void checkLiveness(const MachineOperand *MO, unsigned MONum); 2790b57cec5SDimitry Andric void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum, 280e8d8bef9SDimitry Andric SlotIndex UseIdx, const LiveRange &LR, 281e8d8bef9SDimitry Andric Register VRegOrUnit, 2820b57cec5SDimitry Andric LaneBitmask LaneMask = LaneBitmask::getNone()); 2830b57cec5SDimitry Andric void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum, 284e8d8bef9SDimitry Andric SlotIndex DefIdx, const LiveRange &LR, 285e8d8bef9SDimitry Andric Register VRegOrUnit, bool SubRangeCheck = false, 2860b57cec5SDimitry Andric LaneBitmask LaneMask = LaneBitmask::getNone()); 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric void markReachable(const MachineBasicBlock *MBB); 2890b57cec5SDimitry Andric void calcRegsPassed(); 2900b57cec5SDimitry Andric void checkPHIOps(const MachineBasicBlock &MBB); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric void calcRegsRequired(); 2930b57cec5SDimitry Andric void verifyLiveVariables(); 2940b57cec5SDimitry Andric void verifyLiveIntervals(); 2950b57cec5SDimitry Andric void verifyLiveInterval(const LiveInterval&); 296e8d8bef9SDimitry Andric void verifyLiveRangeValue(const LiveRange &, const VNInfo *, Register, 2970b57cec5SDimitry Andric LaneBitmask); 2980b57cec5SDimitry Andric void verifyLiveRangeSegment(const LiveRange &, 299e8d8bef9SDimitry Andric const LiveRange::const_iterator I, Register, 3000b57cec5SDimitry Andric LaneBitmask); 301e8d8bef9SDimitry Andric void verifyLiveRange(const LiveRange &, Register, 3020b57cec5SDimitry Andric LaneBitmask LaneMask = LaneBitmask::getNone()); 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric void verifyStackFrame(); 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric void verifySlotIndexes() const; 3070b57cec5SDimitry Andric void verifyProperties(const MachineFunction &MF); 3080b57cec5SDimitry Andric }; 3090b57cec5SDimitry Andric 310*0fca6ea1SDimitry Andric struct MachineVerifierLegacyPass : public MachineFunctionPass { 3110b57cec5SDimitry Andric static char ID; // Pass ID, replacement for typeid 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric const std::string Banner; 3140b57cec5SDimitry Andric 315*0fca6ea1SDimitry Andric MachineVerifierLegacyPass(std::string banner = std::string()) 3160b57cec5SDimitry Andric : MachineFunctionPass(ID), Banner(std::move(banner)) { 317*0fca6ea1SDimitry Andric initializeMachineVerifierLegacyPassPass(*PassRegistry::getPassRegistry()); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 321753f127fSDimitry Andric AU.addUsedIfAvailable<LiveStacks>(); 322*0fca6ea1SDimitry Andric AU.addUsedIfAvailable<LiveVariablesWrapperPass>(); 323*0fca6ea1SDimitry Andric AU.addUsedIfAvailable<SlotIndexesWrapperPass>(); 324*0fca6ea1SDimitry Andric AU.addUsedIfAvailable<LiveIntervalsWrapperPass>(); 3250b57cec5SDimitry Andric AU.setPreservesAll(); 3260b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 330349cc55cSDimitry Andric // Skip functions that have known verification problems. 331349cc55cSDimitry Andric // FIXME: Remove this mechanism when all problematic passes have been 332349cc55cSDimitry Andric // fixed. 333349cc55cSDimitry Andric if (MF.getProperties().hasProperty( 334349cc55cSDimitry Andric MachineFunctionProperties::Property::FailsVerification)) 335349cc55cSDimitry Andric return false; 336349cc55cSDimitry Andric 3370b57cec5SDimitry Andric unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF); 3380b57cec5SDimitry Andric if (FoundErrors) 3390b57cec5SDimitry Andric report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors."); 3400b57cec5SDimitry Andric return false; 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric }; 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric } // end anonymous namespace 3450b57cec5SDimitry Andric 346*0fca6ea1SDimitry Andric PreservedAnalyses 347*0fca6ea1SDimitry Andric MachineVerifierPass::run(MachineFunction &MF, 348*0fca6ea1SDimitry Andric MachineFunctionAnalysisManager &MFAM) { 349*0fca6ea1SDimitry Andric // Skip functions that have known verification problems. 350*0fca6ea1SDimitry Andric // FIXME: Remove this mechanism when all problematic passes have been 351*0fca6ea1SDimitry Andric // fixed. 352*0fca6ea1SDimitry Andric if (MF.getProperties().hasProperty( 353*0fca6ea1SDimitry Andric MachineFunctionProperties::Property::FailsVerification)) 354*0fca6ea1SDimitry Andric return PreservedAnalyses::all(); 355*0fca6ea1SDimitry Andric unsigned FoundErrors = MachineVerifier(MFAM, Banner.c_str()).verify(MF); 356*0fca6ea1SDimitry Andric if (FoundErrors) 357*0fca6ea1SDimitry Andric report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors."); 358*0fca6ea1SDimitry Andric return PreservedAnalyses::all(); 359*0fca6ea1SDimitry Andric } 3600b57cec5SDimitry Andric 361*0fca6ea1SDimitry Andric char MachineVerifierLegacyPass::ID = 0; 362*0fca6ea1SDimitry Andric 363*0fca6ea1SDimitry Andric INITIALIZE_PASS(MachineVerifierLegacyPass, "machineverifier", 3640b57cec5SDimitry Andric "Verify generated machine code", false, false) 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) { 367*0fca6ea1SDimitry Andric return new MachineVerifierLegacyPass(Banner); 3680b57cec5SDimitry Andric } 3690b57cec5SDimitry Andric 370*0fca6ea1SDimitry Andric void llvm::verifyMachineFunction(const std::string &Banner, 371e8d8bef9SDimitry Andric const MachineFunction &MF) { 372e8d8bef9SDimitry Andric // TODO: Use MFAM after porting below analyses. 373e8d8bef9SDimitry Andric // LiveVariables *LiveVars; 374e8d8bef9SDimitry Andric // LiveIntervals *LiveInts; 375e8d8bef9SDimitry Andric // LiveStacks *LiveStks; 376e8d8bef9SDimitry Andric // SlotIndexes *Indexes; 377e8d8bef9SDimitry Andric unsigned FoundErrors = MachineVerifier(nullptr, Banner.c_str()).verify(MF); 378e8d8bef9SDimitry Andric if (FoundErrors) 379e8d8bef9SDimitry Andric report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors."); 380e8d8bef9SDimitry Andric } 381e8d8bef9SDimitry Andric 3820b57cec5SDimitry Andric bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors) 3830b57cec5SDimitry Andric const { 3840b57cec5SDimitry Andric MachineFunction &MF = const_cast<MachineFunction&>(*this); 3850b57cec5SDimitry Andric unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF); 3860b57cec5SDimitry Andric if (AbortOnErrors && FoundErrors) 3870b57cec5SDimitry Andric report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors."); 3880b57cec5SDimitry Andric return FoundErrors == 0; 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3915f757f3fSDimitry Andric bool MachineFunction::verify(LiveIntervals *LiveInts, SlotIndexes *Indexes, 3925f757f3fSDimitry Andric const char *Banner, bool AbortOnErrors) const { 3935f757f3fSDimitry Andric MachineFunction &MF = const_cast<MachineFunction &>(*this); 3945f757f3fSDimitry Andric unsigned FoundErrors = 3955f757f3fSDimitry Andric MachineVerifier(Banner, nullptr, LiveInts, nullptr, Indexes).verify(MF); 3965f757f3fSDimitry Andric if (AbortOnErrors && FoundErrors) 3975f757f3fSDimitry Andric report_fatal_error("Found " + Twine(FoundErrors) + " machine code errors."); 3985f757f3fSDimitry Andric return FoundErrors == 0; 3995f757f3fSDimitry Andric } 4005f757f3fSDimitry Andric 4010b57cec5SDimitry Andric void MachineVerifier::verifySlotIndexes() const { 4020b57cec5SDimitry Andric if (Indexes == nullptr) 4030b57cec5SDimitry Andric return; 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric // Ensure the IdxMBB list is sorted by slot indexes. 4060b57cec5SDimitry Andric SlotIndex Last; 4070b57cec5SDimitry Andric for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(), 4080b57cec5SDimitry Andric E = Indexes->MBBIndexEnd(); I != E; ++I) { 4090b57cec5SDimitry Andric assert(!Last.isValid() || I->first > Last); 4100b57cec5SDimitry Andric Last = I->first; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric void MachineVerifier::verifyProperties(const MachineFunction &MF) { 4150b57cec5SDimitry Andric // If a pass has introduced virtual registers without clearing the 4160b57cec5SDimitry Andric // NoVRegs property (or set it without allocating the vregs) 4170b57cec5SDimitry Andric // then report an error. 4180b57cec5SDimitry Andric if (MF.getProperties().hasProperty( 4190b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs) && 4200b57cec5SDimitry Andric MRI->getNumVirtRegs()) 4210b57cec5SDimitry Andric report("Function has NoVRegs property but there are VReg operands", &MF); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 424e8d8bef9SDimitry Andric unsigned MachineVerifier::verify(const MachineFunction &MF) { 4250b57cec5SDimitry Andric foundErrors = 0; 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric this->MF = &MF; 4280b57cec5SDimitry Andric TM = &MF.getTarget(); 4290b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 4300b57cec5SDimitry Andric TRI = MF.getSubtarget().getRegisterInfo(); 43181ad6265SDimitry Andric RBI = MF.getSubtarget().getRegBankInfo(); 4320b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric const bool isFunctionFailedISel = MF.getProperties().hasProperty( 4350b57cec5SDimitry Andric MachineFunctionProperties::Property::FailedISel); 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric // If we're mid-GlobalISel and we already triggered the fallback path then 4380b57cec5SDimitry Andric // it's expected that the MIR is somewhat broken but that's ok since we'll 4390b57cec5SDimitry Andric // reset it and clear the FailedISel attribute in ResetMachineFunctions. 4400b57cec5SDimitry Andric if (isFunctionFailedISel) 4410b57cec5SDimitry Andric return foundErrors; 4420b57cec5SDimitry Andric 4435ffd83dbSDimitry Andric isFunctionRegBankSelected = MF.getProperties().hasProperty( 4440b57cec5SDimitry Andric MachineFunctionProperties::Property::RegBankSelected); 4455ffd83dbSDimitry Andric isFunctionSelected = MF.getProperties().hasProperty( 4460b57cec5SDimitry Andric MachineFunctionProperties::Property::Selected); 4470eae32dcSDimitry Andric isFunctionTracksDebugUserValues = MF.getProperties().hasProperty( 4480eae32dcSDimitry Andric MachineFunctionProperties::Property::TracksDebugUserValues); 4495ffd83dbSDimitry Andric 4500b57cec5SDimitry Andric if (PASS) { 451*0fca6ea1SDimitry Andric auto *LISWrapper = PASS->getAnalysisIfAvailable<LiveIntervalsWrapperPass>(); 452*0fca6ea1SDimitry Andric LiveInts = LISWrapper ? &LISWrapper->getLIS() : nullptr; 4530b57cec5SDimitry Andric // We don't want to verify LiveVariables if LiveIntervals is available. 454*0fca6ea1SDimitry Andric auto *LVWrapper = PASS->getAnalysisIfAvailable<LiveVariablesWrapperPass>(); 4550b57cec5SDimitry Andric if (!LiveInts) 456*0fca6ea1SDimitry Andric LiveVars = LVWrapper ? &LVWrapper->getLV() : nullptr; 4570b57cec5SDimitry Andric LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>(); 458*0fca6ea1SDimitry Andric auto *SIWrapper = PASS->getAnalysisIfAvailable<SlotIndexesWrapperPass>(); 459*0fca6ea1SDimitry Andric Indexes = SIWrapper ? &SIWrapper->getSI() : nullptr; 460*0fca6ea1SDimitry Andric } 461*0fca6ea1SDimitry Andric if (MFAM) { 462*0fca6ea1SDimitry Andric MachineFunction &Func = const_cast<MachineFunction &>(MF); 463*0fca6ea1SDimitry Andric LiveInts = MFAM->getCachedResult<LiveIntervalsAnalysis>(Func); 464*0fca6ea1SDimitry Andric if (!LiveInts) 465*0fca6ea1SDimitry Andric LiveVars = MFAM->getCachedResult<LiveVariablesAnalysis>(Func); 466*0fca6ea1SDimitry Andric // TODO: LiveStks = MFAM->getCachedResult<LiveStacksAnalysis>(Func); 467*0fca6ea1SDimitry Andric Indexes = MFAM->getCachedResult<SlotIndexesAnalysis>(Func); 4680b57cec5SDimitry Andric } 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric verifySlotIndexes(); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric verifyProperties(MF); 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric visitMachineFunctionBefore(); 4755ffd83dbSDimitry Andric for (const MachineBasicBlock &MBB : MF) { 4765ffd83dbSDimitry Andric visitMachineBasicBlockBefore(&MBB); 4770b57cec5SDimitry Andric // Keep track of the current bundle header. 4780b57cec5SDimitry Andric const MachineInstr *CurBundle = nullptr; 4790b57cec5SDimitry Andric // Do we expect the next instruction to be part of the same bundle? 4800b57cec5SDimitry Andric bool InBundle = false; 4810b57cec5SDimitry Andric 4825ffd83dbSDimitry Andric for (const MachineInstr &MI : MBB.instrs()) { 4835ffd83dbSDimitry Andric if (MI.getParent() != &MBB) { 4845ffd83dbSDimitry Andric report("Bad instruction parent pointer", &MBB); 4855ffd83dbSDimitry Andric errs() << "Instruction: " << MI; 4860b57cec5SDimitry Andric continue; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric // Check for consistent bundle flags. 4905ffd83dbSDimitry Andric if (InBundle && !MI.isBundledWithPred()) 4910b57cec5SDimitry Andric report("Missing BundledPred flag, " 4920b57cec5SDimitry Andric "BundledSucc was set on predecessor", 4935ffd83dbSDimitry Andric &MI); 4945ffd83dbSDimitry Andric if (!InBundle && MI.isBundledWithPred()) 4950b57cec5SDimitry Andric report("BundledPred flag is set, " 4960b57cec5SDimitry Andric "but BundledSucc not set on predecessor", 4975ffd83dbSDimitry Andric &MI); 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // Is this a bundle header? 5005ffd83dbSDimitry Andric if (!MI.isInsideBundle()) { 5010b57cec5SDimitry Andric if (CurBundle) 5020b57cec5SDimitry Andric visitMachineBundleAfter(CurBundle); 5035ffd83dbSDimitry Andric CurBundle = &MI; 5040b57cec5SDimitry Andric visitMachineBundleBefore(CurBundle); 5050b57cec5SDimitry Andric } else if (!CurBundle) 5065ffd83dbSDimitry Andric report("No bundle header", &MI); 5075ffd83dbSDimitry Andric visitMachineInstrBefore(&MI); 5085ffd83dbSDimitry Andric for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) { 5090b57cec5SDimitry Andric const MachineOperand &Op = MI.getOperand(I); 5100b57cec5SDimitry Andric if (Op.getParent() != &MI) { 51181ad6265SDimitry Andric // Make sure to use correct addOperand / removeOperand / ChangeTo 5120b57cec5SDimitry Andric // functions when replacing operands of a MachineInstr. 5130b57cec5SDimitry Andric report("Instruction has operand with wrong parent set", &MI); 5140b57cec5SDimitry Andric } 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric visitMachineOperand(&Op, I); 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric // Was this the last bundled instruction? 5205ffd83dbSDimitry Andric InBundle = MI.isBundledWithSucc(); 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric if (CurBundle) 5230b57cec5SDimitry Andric visitMachineBundleAfter(CurBundle); 5240b57cec5SDimitry Andric if (InBundle) 5255ffd83dbSDimitry Andric report("BundledSucc flag set on last instruction in block", &MBB.back()); 5265ffd83dbSDimitry Andric visitMachineBasicBlockAfter(&MBB); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric visitMachineFunctionAfter(); 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric // Clean up. 5310b57cec5SDimitry Andric regsLive.clear(); 5320b57cec5SDimitry Andric regsDefined.clear(); 5330b57cec5SDimitry Andric regsDead.clear(); 5340b57cec5SDimitry Andric regsKilled.clear(); 5350b57cec5SDimitry Andric regMasks.clear(); 5360b57cec5SDimitry Andric MBBInfoMap.clear(); 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric return foundErrors; 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineFunction *MF) { 5420b57cec5SDimitry Andric assert(MF); 5430b57cec5SDimitry Andric errs() << '\n'; 5440b57cec5SDimitry Andric if (!foundErrors++) { 5450b57cec5SDimitry Andric if (Banner) 5460b57cec5SDimitry Andric errs() << "# " << Banner << '\n'; 5470b57cec5SDimitry Andric if (LiveInts != nullptr) 5480b57cec5SDimitry Andric LiveInts->print(errs()); 5490b57cec5SDimitry Andric else 5500b57cec5SDimitry Andric MF->print(errs(), Indexes); 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric errs() << "*** Bad machine code: " << msg << " ***\n" 5530b57cec5SDimitry Andric << "- function: " << MF->getName() << "\n"; 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) { 5570b57cec5SDimitry Andric assert(MBB); 5580b57cec5SDimitry Andric report(msg, MBB->getParent()); 5590b57cec5SDimitry Andric errs() << "- basic block: " << printMBBReference(*MBB) << ' ' 5600b57cec5SDimitry Andric << MBB->getName() << " (" << (const void *)MBB << ')'; 5610b57cec5SDimitry Andric if (Indexes) 5620b57cec5SDimitry Andric errs() << " [" << Indexes->getMBBStartIdx(MBB) 5630b57cec5SDimitry Andric << ';' << Indexes->getMBBEndIdx(MBB) << ')'; 5640b57cec5SDimitry Andric errs() << '\n'; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric 5670b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineInstr *MI) { 5680b57cec5SDimitry Andric assert(MI); 5690b57cec5SDimitry Andric report(msg, MI->getParent()); 5700b57cec5SDimitry Andric errs() << "- instruction: "; 5710b57cec5SDimitry Andric if (Indexes && Indexes->hasIndex(*MI)) 5720b57cec5SDimitry Andric errs() << Indexes->getInstructionIndex(*MI) << '\t'; 573e8d8bef9SDimitry Andric MI->print(errs(), /*IsStandalone=*/true); 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric void MachineVerifier::report(const char *msg, const MachineOperand *MO, 5770b57cec5SDimitry Andric unsigned MONum, LLT MOVRegType) { 5780b57cec5SDimitry Andric assert(MO); 5790b57cec5SDimitry Andric report(msg, MO->getParent()); 5800b57cec5SDimitry Andric errs() << "- operand " << MONum << ": "; 5810b57cec5SDimitry Andric MO->print(errs(), MOVRegType, TRI); 5820b57cec5SDimitry Andric errs() << "\n"; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric 585fe6060f1SDimitry Andric void MachineVerifier::report(const Twine &Msg, const MachineInstr *MI) { 586fe6060f1SDimitry Andric report(Msg.str().c_str(), MI); 587fe6060f1SDimitry Andric } 588fe6060f1SDimitry Andric 5890b57cec5SDimitry Andric void MachineVerifier::report_context(SlotIndex Pos) const { 5900b57cec5SDimitry Andric errs() << "- at: " << Pos << '\n'; 5910b57cec5SDimitry Andric } 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric void MachineVerifier::report_context(const LiveInterval &LI) const { 5940b57cec5SDimitry Andric errs() << "- interval: " << LI << '\n'; 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 597e8d8bef9SDimitry Andric void MachineVerifier::report_context(const LiveRange &LR, Register VRegUnit, 5980b57cec5SDimitry Andric LaneBitmask LaneMask) const { 5990b57cec5SDimitry Andric report_context_liverange(LR); 6000b57cec5SDimitry Andric report_context_vreg_regunit(VRegUnit); 6010b57cec5SDimitry Andric if (LaneMask.any()) 6020b57cec5SDimitry Andric report_context_lanemask(LaneMask); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric void MachineVerifier::report_context(const LiveRange::Segment &S) const { 6060b57cec5SDimitry Andric errs() << "- segment: " << S << '\n'; 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric void MachineVerifier::report_context(const VNInfo &VNI) const { 6100b57cec5SDimitry Andric errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n"; 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric void MachineVerifier::report_context_liverange(const LiveRange &LR) const { 6140b57cec5SDimitry Andric errs() << "- liverange: " << LR << '\n'; 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric void MachineVerifier::report_context(MCPhysReg PReg) const { 6180b57cec5SDimitry Andric errs() << "- p. register: " << printReg(PReg, TRI) << '\n'; 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 621e8d8bef9SDimitry Andric void MachineVerifier::report_context_vreg(Register VReg) const { 6220b57cec5SDimitry Andric errs() << "- v. register: " << printReg(VReg, TRI) << '\n'; 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric 625e8d8bef9SDimitry Andric void MachineVerifier::report_context_vreg_regunit(Register VRegOrUnit) const { 626bdd1243dSDimitry Andric if (VRegOrUnit.isVirtual()) { 6270b57cec5SDimitry Andric report_context_vreg(VRegOrUnit); 6280b57cec5SDimitry Andric } else { 6290b57cec5SDimitry Andric errs() << "- regunit: " << printRegUnit(VRegOrUnit, TRI) << '\n'; 6300b57cec5SDimitry Andric } 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const { 6340b57cec5SDimitry Andric errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n'; 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric void MachineVerifier::markReachable(const MachineBasicBlock *MBB) { 6380b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[MBB]; 6390b57cec5SDimitry Andric if (!MInfo.reachable) { 6400b57cec5SDimitry Andric MInfo.reachable = true; 6415ffd83dbSDimitry Andric for (const MachineBasicBlock *Succ : MBB->successors()) 6425ffd83dbSDimitry Andric markReachable(Succ); 6430b57cec5SDimitry Andric } 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 6460b57cec5SDimitry Andric void MachineVerifier::visitMachineFunctionBefore() { 6470b57cec5SDimitry Andric lastIndex = SlotIndex(); 6480b57cec5SDimitry Andric regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs() 6490b57cec5SDimitry Andric : TRI->getReservedRegs(*MF); 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric if (!MF->empty()) 6520b57cec5SDimitry Andric markReachable(&MF->front()); 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric // Build a set of the basic blocks in the function. 6550b57cec5SDimitry Andric FunctionBlocks.clear(); 6560b57cec5SDimitry Andric for (const auto &MBB : *MF) { 6570b57cec5SDimitry Andric FunctionBlocks.insert(&MBB); 6580b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[&MBB]; 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end()); 6610b57cec5SDimitry Andric if (MInfo.Preds.size() != MBB.pred_size()) 6620b57cec5SDimitry Andric report("MBB has duplicate entries in its predecessor list.", &MBB); 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end()); 6650b57cec5SDimitry Andric if (MInfo.Succs.size() != MBB.succ_size()) 6660b57cec5SDimitry Andric report("MBB has duplicate entries in its successor list.", &MBB); 6670b57cec5SDimitry Andric } 6680b57cec5SDimitry Andric 6690b57cec5SDimitry Andric // Check that the register use lists are sane. 6700b57cec5SDimitry Andric MRI->verifyUseLists(); 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric if (!MF->empty()) 6730b57cec5SDimitry Andric verifyStackFrame(); 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric void 6770b57cec5SDimitry Andric MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) { 6780b57cec5SDimitry Andric FirstTerminator = nullptr; 6790b57cec5SDimitry Andric FirstNonPHI = nullptr; 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric if (!MF->getProperties().hasProperty( 6820b57cec5SDimitry Andric MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) { 6830b57cec5SDimitry Andric // If this block has allocatable physical registers live-in, check that 6840b57cec5SDimitry Andric // it is an entry block or landing pad. 6850b57cec5SDimitry Andric for (const auto &LI : MBB->liveins()) { 6860b57cec5SDimitry Andric if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() && 68706c3fb27SDimitry Andric MBB->getIterator() != MBB->getParent()->begin() && 68806c3fb27SDimitry Andric !MBB->isInlineAsmBrIndirectTarget()) { 68906c3fb27SDimitry Andric report("MBB has allocatable live-in, but isn't entry, landing-pad, or " 69006c3fb27SDimitry Andric "inlineasm-br-indirect-target.", 69106c3fb27SDimitry Andric MBB); 6920b57cec5SDimitry Andric report_context(LI.PhysReg); 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric } 6950b57cec5SDimitry Andric } 6960b57cec5SDimitry Andric 697bdd1243dSDimitry Andric if (MBB->isIRBlockAddressTaken()) { 698bdd1243dSDimitry Andric if (!MBB->getAddressTakenIRBlock()->hasAddressTaken()) 699bdd1243dSDimitry Andric report("ir-block-address-taken is associated with basic block not used by " 700bdd1243dSDimitry Andric "a blockaddress.", 701bdd1243dSDimitry Andric MBB); 702bdd1243dSDimitry Andric } 703bdd1243dSDimitry Andric 7040b57cec5SDimitry Andric // Count the number of landing pad successors. 7055ffd83dbSDimitry Andric SmallPtrSet<const MachineBasicBlock*, 4> LandingPadSuccs; 7065ffd83dbSDimitry Andric for (const auto *succ : MBB->successors()) { 7075ffd83dbSDimitry Andric if (succ->isEHPad()) 7085ffd83dbSDimitry Andric LandingPadSuccs.insert(succ); 7095ffd83dbSDimitry Andric if (!FunctionBlocks.count(succ)) 7100b57cec5SDimitry Andric report("MBB has successor that isn't part of the function.", MBB); 7115ffd83dbSDimitry Andric if (!MBBInfoMap[succ].Preds.count(MBB)) { 7120b57cec5SDimitry Andric report("Inconsistent CFG", MBB); 7130b57cec5SDimitry Andric errs() << "MBB is not in the predecessor list of the successor " 7145ffd83dbSDimitry Andric << printMBBReference(*succ) << ".\n"; 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric // Check the predecessor list. 7195ffd83dbSDimitry Andric for (const MachineBasicBlock *Pred : MBB->predecessors()) { 7205ffd83dbSDimitry Andric if (!FunctionBlocks.count(Pred)) 7210b57cec5SDimitry Andric report("MBB has predecessor that isn't part of the function.", MBB); 7225ffd83dbSDimitry Andric if (!MBBInfoMap[Pred].Succs.count(MBB)) { 7230b57cec5SDimitry Andric report("Inconsistent CFG", MBB); 7240b57cec5SDimitry Andric errs() << "MBB is not in the successor list of the predecessor " 7255ffd83dbSDimitry Andric << printMBBReference(*Pred) << ".\n"; 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric const MCAsmInfo *AsmInfo = TM->getMCAsmInfo(); 7300b57cec5SDimitry Andric const BasicBlock *BB = MBB->getBasicBlock(); 7310b57cec5SDimitry Andric const Function &F = MF->getFunction(); 7320b57cec5SDimitry Andric if (LandingPadSuccs.size() > 1 && 7330b57cec5SDimitry Andric !(AsmInfo && 7340b57cec5SDimitry Andric AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj && 7350b57cec5SDimitry Andric BB && isa<SwitchInst>(BB->getTerminator())) && 7360b57cec5SDimitry Andric !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) 7370b57cec5SDimitry Andric report("MBB has more than one landing pad successor", MBB); 7380b57cec5SDimitry Andric 7395ffd83dbSDimitry Andric // Call analyzeBranch. If it succeeds, there several more conditions to check. 7400b57cec5SDimitry Andric MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 7410b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 7420b57cec5SDimitry Andric if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB, 7430b57cec5SDimitry Andric Cond)) { 7445ffd83dbSDimitry Andric // Ok, analyzeBranch thinks it knows what's going on with this block. Let's 7450b57cec5SDimitry Andric // check whether its answers match up with reality. 7460b57cec5SDimitry Andric if (!TBB && !FBB) { 7470b57cec5SDimitry Andric // Block falls through to its successor. 7480b57cec5SDimitry Andric if (!MBB->empty() && MBB->back().isBarrier() && 7490b57cec5SDimitry Andric !TII->isPredicated(MBB->back())) { 7500b57cec5SDimitry Andric report("MBB exits via unconditional fall-through but ends with a " 7510b57cec5SDimitry Andric "barrier instruction!", MBB); 7520b57cec5SDimitry Andric } 7530b57cec5SDimitry Andric if (!Cond.empty()) { 7540b57cec5SDimitry Andric report("MBB exits via unconditional fall-through but has a condition!", 7550b57cec5SDimitry Andric MBB); 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric } else if (TBB && !FBB && Cond.empty()) { 7580b57cec5SDimitry Andric // Block unconditionally branches somewhere. 7590b57cec5SDimitry Andric if (MBB->empty()) { 7600b57cec5SDimitry Andric report("MBB exits via unconditional branch but doesn't contain " 7610b57cec5SDimitry Andric "any instructions!", MBB); 7620b57cec5SDimitry Andric } else if (!MBB->back().isBarrier()) { 7630b57cec5SDimitry Andric report("MBB exits via unconditional branch but doesn't end with a " 7640b57cec5SDimitry Andric "barrier instruction!", MBB); 7650b57cec5SDimitry Andric } else if (!MBB->back().isTerminator()) { 7660b57cec5SDimitry Andric report("MBB exits via unconditional branch but the branch isn't a " 7670b57cec5SDimitry Andric "terminator instruction!", MBB); 7680b57cec5SDimitry Andric } 7690b57cec5SDimitry Andric } else if (TBB && !FBB && !Cond.empty()) { 7700b57cec5SDimitry Andric // Block conditionally branches somewhere, otherwise falls through. 7710b57cec5SDimitry Andric if (MBB->empty()) { 7720b57cec5SDimitry Andric report("MBB exits via conditional branch/fall-through but doesn't " 7730b57cec5SDimitry Andric "contain any instructions!", MBB); 7740b57cec5SDimitry Andric } else if (MBB->back().isBarrier()) { 7750b57cec5SDimitry Andric report("MBB exits via conditional branch/fall-through but ends with a " 7760b57cec5SDimitry Andric "barrier instruction!", MBB); 7770b57cec5SDimitry Andric } else if (!MBB->back().isTerminator()) { 7780b57cec5SDimitry Andric report("MBB exits via conditional branch/fall-through but the branch " 7790b57cec5SDimitry Andric "isn't a terminator instruction!", MBB); 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric } else if (TBB && FBB) { 7820b57cec5SDimitry Andric // Block conditionally branches somewhere, otherwise branches 7830b57cec5SDimitry Andric // somewhere else. 7840b57cec5SDimitry Andric if (MBB->empty()) { 7850b57cec5SDimitry Andric report("MBB exits via conditional branch/branch but doesn't " 7860b57cec5SDimitry Andric "contain any instructions!", MBB); 7870b57cec5SDimitry Andric } else if (!MBB->back().isBarrier()) { 7880b57cec5SDimitry Andric report("MBB exits via conditional branch/branch but doesn't end with a " 7890b57cec5SDimitry Andric "barrier instruction!", MBB); 7900b57cec5SDimitry Andric } else if (!MBB->back().isTerminator()) { 7910b57cec5SDimitry Andric report("MBB exits via conditional branch/branch but the branch " 7920b57cec5SDimitry Andric "isn't a terminator instruction!", MBB); 7930b57cec5SDimitry Andric } 7940b57cec5SDimitry Andric if (Cond.empty()) { 7950b57cec5SDimitry Andric report("MBB exits via conditional branch/branch but there's no " 7960b57cec5SDimitry Andric "condition!", MBB); 7970b57cec5SDimitry Andric } 7980b57cec5SDimitry Andric } else { 7995ffd83dbSDimitry Andric report("analyzeBranch returned invalid data!", MBB); 8005ffd83dbSDimitry Andric } 8015ffd83dbSDimitry Andric 8025ffd83dbSDimitry Andric // Now check that the successors match up with the answers reported by 8035ffd83dbSDimitry Andric // analyzeBranch. 8045ffd83dbSDimitry Andric if (TBB && !MBB->isSuccessor(TBB)) 8055ffd83dbSDimitry Andric report("MBB exits via jump or conditional branch, but its target isn't a " 8065ffd83dbSDimitry Andric "CFG successor!", 8075ffd83dbSDimitry Andric MBB); 8085ffd83dbSDimitry Andric if (FBB && !MBB->isSuccessor(FBB)) 8095ffd83dbSDimitry Andric report("MBB exits via conditional branch, but its target isn't a CFG " 8105ffd83dbSDimitry Andric "successor!", 8115ffd83dbSDimitry Andric MBB); 8125ffd83dbSDimitry Andric 8135ffd83dbSDimitry Andric // There might be a fallthrough to the next block if there's either no 8145ffd83dbSDimitry Andric // unconditional true branch, or if there's a condition, and one of the 8155ffd83dbSDimitry Andric // branches is missing. 8165ffd83dbSDimitry Andric bool Fallthrough = !TBB || (!Cond.empty() && !FBB); 8175ffd83dbSDimitry Andric 8185ffd83dbSDimitry Andric // A conditional fallthrough must be an actual CFG successor, not 8195ffd83dbSDimitry Andric // unreachable. (Conversely, an unconditional fallthrough might not really 8205ffd83dbSDimitry Andric // be a successor, because the block might end in unreachable.) 8215ffd83dbSDimitry Andric if (!Cond.empty() && !FBB) { 8225ffd83dbSDimitry Andric MachineFunction::const_iterator MBBI = std::next(MBB->getIterator()); 8235ffd83dbSDimitry Andric if (MBBI == MF->end()) { 8245ffd83dbSDimitry Andric report("MBB conditionally falls through out of function!", MBB); 8255ffd83dbSDimitry Andric } else if (!MBB->isSuccessor(&*MBBI)) 8265ffd83dbSDimitry Andric report("MBB exits via conditional branch/fall-through but the CFG " 8275ffd83dbSDimitry Andric "successors don't match the actual successors!", 8285ffd83dbSDimitry Andric MBB); 8295ffd83dbSDimitry Andric } 8305ffd83dbSDimitry Andric 8315ffd83dbSDimitry Andric // Verify that there aren't any extra un-accounted-for successors. 8325ffd83dbSDimitry Andric for (const MachineBasicBlock *SuccMBB : MBB->successors()) { 8335ffd83dbSDimitry Andric // If this successor is one of the branch targets, it's okay. 8345ffd83dbSDimitry Andric if (SuccMBB == TBB || SuccMBB == FBB) 8355ffd83dbSDimitry Andric continue; 8365ffd83dbSDimitry Andric // If we might have a fallthrough, and the successor is the fallthrough 8375ffd83dbSDimitry Andric // block, that's also ok. 8385ffd83dbSDimitry Andric if (Fallthrough && SuccMBB == MBB->getNextNode()) 8395ffd83dbSDimitry Andric continue; 8405ffd83dbSDimitry Andric // Also accept successors which are for exception-handling or might be 8415ffd83dbSDimitry Andric // inlineasm_br targets. 8425ffd83dbSDimitry Andric if (SuccMBB->isEHPad() || SuccMBB->isInlineAsmBrIndirectTarget()) 8435ffd83dbSDimitry Andric continue; 8445ffd83dbSDimitry Andric report("MBB has unexpected successors which are not branch targets, " 8455ffd83dbSDimitry Andric "fallthrough, EHPads, or inlineasm_br targets.", 8465ffd83dbSDimitry Andric MBB); 8470b57cec5SDimitry Andric } 8480b57cec5SDimitry Andric } 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric regsLive.clear(); 8510b57cec5SDimitry Andric if (MRI->tracksLiveness()) { 8520b57cec5SDimitry Andric for (const auto &LI : MBB->liveins()) { 8538bcb0991SDimitry Andric if (!Register::isPhysicalRegister(LI.PhysReg)) { 8540b57cec5SDimitry Andric report("MBB live-in list contains non-physical register", MBB); 8550b57cec5SDimitry Andric continue; 8560b57cec5SDimitry Andric } 857480093f4SDimitry Andric for (const MCPhysReg &SubReg : TRI->subregs_inclusive(LI.PhysReg)) 858480093f4SDimitry Andric regsLive.insert(SubReg); 8590b57cec5SDimitry Andric } 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric const MachineFrameInfo &MFI = MF->getFrameInfo(); 8630b57cec5SDimitry Andric BitVector PR = MFI.getPristineRegs(*MF); 8640b57cec5SDimitry Andric for (unsigned I : PR.set_bits()) { 865480093f4SDimitry Andric for (const MCPhysReg &SubReg : TRI->subregs_inclusive(I)) 866480093f4SDimitry Andric regsLive.insert(SubReg); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric 8690b57cec5SDimitry Andric regsKilled.clear(); 8700b57cec5SDimitry Andric regsDefined.clear(); 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric if (Indexes) 8730b57cec5SDimitry Andric lastIndex = Indexes->getMBBStartIdx(MBB); 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric 8760b57cec5SDimitry Andric // This function gets called for all bundle headers, including normal 8770b57cec5SDimitry Andric // stand-alone unbundled instructions. 8780b57cec5SDimitry Andric void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) { 8790b57cec5SDimitry Andric if (Indexes && Indexes->hasIndex(*MI)) { 8800b57cec5SDimitry Andric SlotIndex idx = Indexes->getInstructionIndex(*MI); 8810b57cec5SDimitry Andric if (!(idx > lastIndex)) { 8820b57cec5SDimitry Andric report("Instruction index out of order", MI); 8830b57cec5SDimitry Andric errs() << "Last instruction was at " << lastIndex << '\n'; 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric lastIndex = idx; 8860b57cec5SDimitry Andric } 8870b57cec5SDimitry Andric 8880b57cec5SDimitry Andric // Ensure non-terminators don't follow terminators. 889e8d8bef9SDimitry Andric if (MI->isTerminator()) { 8900b57cec5SDimitry Andric if (!FirstTerminator) 8910b57cec5SDimitry Andric FirstTerminator = MI; 8925ffd83dbSDimitry Andric } else if (FirstTerminator) { 893bdd1243dSDimitry Andric // For GlobalISel, G_INVOKE_REGION_START is a terminator that we allow to 894bdd1243dSDimitry Andric // precede non-terminators. 895bdd1243dSDimitry Andric if (FirstTerminator->getOpcode() != TargetOpcode::G_INVOKE_REGION_START) { 8960b57cec5SDimitry Andric report("Non-terminator instruction after the first terminator", MI); 8970b57cec5SDimitry Andric errs() << "First terminator was:\t" << *FirstTerminator; 8980b57cec5SDimitry Andric } 8990b57cec5SDimitry Andric } 900bdd1243dSDimitry Andric } 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric // The operands on an INLINEASM instruction must follow a template. 9030b57cec5SDimitry Andric // Verify that the flag operands make sense. 9040b57cec5SDimitry Andric void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) { 9050b57cec5SDimitry Andric // The first two operands on INLINEASM are the asm string and global flags. 9060b57cec5SDimitry Andric if (MI->getNumOperands() < 2) { 9070b57cec5SDimitry Andric report("Too few operands on inline asm", MI); 9080b57cec5SDimitry Andric return; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric if (!MI->getOperand(0).isSymbol()) 9110b57cec5SDimitry Andric report("Asm string must be an external symbol", MI); 9120b57cec5SDimitry Andric if (!MI->getOperand(1).isImm()) 9130b57cec5SDimitry Andric report("Asm flags must be an immediate", MI); 9140b57cec5SDimitry Andric // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2, 9150b57cec5SDimitry Andric // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16, 9160b57cec5SDimitry Andric // and Extra_IsConvergent = 32. 9170b57cec5SDimitry Andric if (!isUInt<6>(MI->getOperand(1).getImm())) 9180b57cec5SDimitry Andric report("Unknown asm flags", &MI->getOperand(1), 1); 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed"); 9210b57cec5SDimitry Andric 9220b57cec5SDimitry Andric unsigned OpNo = InlineAsm::MIOp_FirstOperand; 9230b57cec5SDimitry Andric unsigned NumOps; 9240b57cec5SDimitry Andric for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) { 9250b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNo); 9260b57cec5SDimitry Andric // There may be implicit ops after the fixed operands. 9270b57cec5SDimitry Andric if (!MO.isImm()) 9280b57cec5SDimitry Andric break; 9295f757f3fSDimitry Andric const InlineAsm::Flag F(MO.getImm()); 9305f757f3fSDimitry Andric NumOps = 1 + F.getNumOperandRegisters(); 9310b57cec5SDimitry Andric } 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric if (OpNo > MI->getNumOperands()) 9340b57cec5SDimitry Andric report("Missing operands in last group", MI); 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric // An optional MDNode follows the groups. 9370b57cec5SDimitry Andric if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata()) 9380b57cec5SDimitry Andric ++OpNo; 9390b57cec5SDimitry Andric 9400b57cec5SDimitry Andric // All trailing operands must be implicit registers. 9410b57cec5SDimitry Andric for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) { 9420b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(OpNo); 9430b57cec5SDimitry Andric if (!MO.isReg() || !MO.isImplicit()) 9440b57cec5SDimitry Andric report("Expected implicit register after groups", &MO, OpNo); 9450b57cec5SDimitry Andric } 946bdd1243dSDimitry Andric 947bdd1243dSDimitry Andric if (MI->getOpcode() == TargetOpcode::INLINEASM_BR) { 948bdd1243dSDimitry Andric const MachineBasicBlock *MBB = MI->getParent(); 949bdd1243dSDimitry Andric 950bdd1243dSDimitry Andric for (unsigned i = InlineAsm::MIOp_FirstOperand, e = MI->getNumOperands(); 951bdd1243dSDimitry Andric i != e; ++i) { 952bdd1243dSDimitry Andric const MachineOperand &MO = MI->getOperand(i); 953bdd1243dSDimitry Andric 954bdd1243dSDimitry Andric if (!MO.isMBB()) 955bdd1243dSDimitry Andric continue; 956bdd1243dSDimitry Andric 957bdd1243dSDimitry Andric // Check the successor & predecessor lists look ok, assume they are 958bdd1243dSDimitry Andric // not. Find the indirect target without going through the successors. 959bdd1243dSDimitry Andric const MachineBasicBlock *IndirectTargetMBB = MO.getMBB(); 960bdd1243dSDimitry Andric if (!IndirectTargetMBB) { 961bdd1243dSDimitry Andric report("INLINEASM_BR indirect target does not exist", &MO, i); 962bdd1243dSDimitry Andric break; 963bdd1243dSDimitry Andric } 964bdd1243dSDimitry Andric 965bdd1243dSDimitry Andric if (!MBB->isSuccessor(IndirectTargetMBB)) 966bdd1243dSDimitry Andric report("INLINEASM_BR indirect target missing from successor list", &MO, 967bdd1243dSDimitry Andric i); 968bdd1243dSDimitry Andric 969bdd1243dSDimitry Andric if (!IndirectTargetMBB->isPredecessor(MBB)) 970bdd1243dSDimitry Andric report("INLINEASM_BR indirect target predecessor list missing parent", 971bdd1243dSDimitry Andric &MO, i); 972bdd1243dSDimitry Andric } 973bdd1243dSDimitry Andric } 9740b57cec5SDimitry Andric } 9750b57cec5SDimitry Andric 976349cc55cSDimitry Andric bool MachineVerifier::verifyAllRegOpsScalar(const MachineInstr &MI, 977349cc55cSDimitry Andric const MachineRegisterInfo &MRI) { 978349cc55cSDimitry Andric if (none_of(MI.explicit_operands(), [&MRI](const MachineOperand &Op) { 979349cc55cSDimitry Andric if (!Op.isReg()) 980349cc55cSDimitry Andric return false; 981349cc55cSDimitry Andric const auto Reg = Op.getReg(); 982349cc55cSDimitry Andric if (Reg.isPhysical()) 983349cc55cSDimitry Andric return false; 984349cc55cSDimitry Andric return !MRI.getType(Reg).isScalar(); 985349cc55cSDimitry Andric })) 986349cc55cSDimitry Andric return true; 987349cc55cSDimitry Andric report("All register operands must have scalar types", &MI); 988349cc55cSDimitry Andric return false; 989349cc55cSDimitry Andric } 990349cc55cSDimitry Andric 9910b57cec5SDimitry Andric /// Check that types are consistent when two operands need to have the same 9920b57cec5SDimitry Andric /// number of vector elements. 9930b57cec5SDimitry Andric /// \return true if the types are valid. 9940b57cec5SDimitry Andric bool MachineVerifier::verifyVectorElementMatch(LLT Ty0, LLT Ty1, 9950b57cec5SDimitry Andric const MachineInstr *MI) { 9960b57cec5SDimitry Andric if (Ty0.isVector() != Ty1.isVector()) { 9970b57cec5SDimitry Andric report("operand types must be all-vector or all-scalar", MI); 9980b57cec5SDimitry Andric // Generally we try to report as many issues as possible at once, but in 9990b57cec5SDimitry Andric // this case it's not clear what should we be comparing the size of the 10000b57cec5SDimitry Andric // scalar with: the size of the whole vector or its lane. Instead of 10010b57cec5SDimitry Andric // making an arbitrary choice and emitting not so helpful message, let's 10020b57cec5SDimitry Andric // avoid the extra noise and stop here. 10030b57cec5SDimitry Andric return false; 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 10065f757f3fSDimitry Andric if (Ty0.isVector() && Ty0.getElementCount() != Ty1.getElementCount()) { 10070b57cec5SDimitry Andric report("operand types must preserve number of vector elements", MI); 10080b57cec5SDimitry Andric return false; 10090b57cec5SDimitry Andric } 10100b57cec5SDimitry Andric 10110b57cec5SDimitry Andric return true; 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric 10145f757f3fSDimitry Andric bool MachineVerifier::verifyGIntrinsicSideEffects(const MachineInstr *MI) { 10155f757f3fSDimitry Andric auto Opcode = MI->getOpcode(); 10165f757f3fSDimitry Andric bool NoSideEffects = Opcode == TargetOpcode::G_INTRINSIC || 10175f757f3fSDimitry Andric Opcode == TargetOpcode::G_INTRINSIC_CONVERGENT; 10185f757f3fSDimitry Andric unsigned IntrID = cast<GIntrinsic>(MI)->getIntrinsicID(); 10195f757f3fSDimitry Andric if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) { 10205f757f3fSDimitry Andric AttributeList Attrs = Intrinsic::getAttributes( 10215f757f3fSDimitry Andric MF->getFunction().getContext(), static_cast<Intrinsic::ID>(IntrID)); 10225f757f3fSDimitry Andric bool DeclHasSideEffects = !Attrs.getMemoryEffects().doesNotAccessMemory(); 10235f757f3fSDimitry Andric if (NoSideEffects && DeclHasSideEffects) { 10245f757f3fSDimitry Andric report(Twine(TII->getName(Opcode), 10255f757f3fSDimitry Andric " used with intrinsic that accesses memory"), 10265f757f3fSDimitry Andric MI); 10275f757f3fSDimitry Andric return false; 10285f757f3fSDimitry Andric } 10295f757f3fSDimitry Andric if (!NoSideEffects && !DeclHasSideEffects) { 10305f757f3fSDimitry Andric report(Twine(TII->getName(Opcode), " used with readnone intrinsic"), MI); 10315f757f3fSDimitry Andric return false; 10325f757f3fSDimitry Andric } 10335f757f3fSDimitry Andric } 10345f757f3fSDimitry Andric 10355f757f3fSDimitry Andric return true; 10365f757f3fSDimitry Andric } 10375f757f3fSDimitry Andric 10385f757f3fSDimitry Andric bool MachineVerifier::verifyGIntrinsicConvergence(const MachineInstr *MI) { 10395f757f3fSDimitry Andric auto Opcode = MI->getOpcode(); 10405f757f3fSDimitry Andric bool NotConvergent = Opcode == TargetOpcode::G_INTRINSIC || 10415f757f3fSDimitry Andric Opcode == TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS; 10425f757f3fSDimitry Andric unsigned IntrID = cast<GIntrinsic>(MI)->getIntrinsicID(); 10435f757f3fSDimitry Andric if (IntrID != 0 && IntrID < Intrinsic::num_intrinsics) { 10445f757f3fSDimitry Andric AttributeList Attrs = Intrinsic::getAttributes( 10455f757f3fSDimitry Andric MF->getFunction().getContext(), static_cast<Intrinsic::ID>(IntrID)); 10465f757f3fSDimitry Andric bool DeclIsConvergent = Attrs.hasFnAttr(Attribute::Convergent); 10475f757f3fSDimitry Andric if (NotConvergent && DeclIsConvergent) { 10485f757f3fSDimitry Andric report(Twine(TII->getName(Opcode), " used with a convergent intrinsic"), 10495f757f3fSDimitry Andric MI); 10505f757f3fSDimitry Andric return false; 10515f757f3fSDimitry Andric } 10525f757f3fSDimitry Andric if (!NotConvergent && !DeclIsConvergent) { 10535f757f3fSDimitry Andric report( 10545f757f3fSDimitry Andric Twine(TII->getName(Opcode), " used with a non-convergent intrinsic"), 10555f757f3fSDimitry Andric MI); 10565f757f3fSDimitry Andric return false; 10575f757f3fSDimitry Andric } 10585f757f3fSDimitry Andric } 10595f757f3fSDimitry Andric 10605f757f3fSDimitry Andric return true; 10615f757f3fSDimitry Andric } 10625f757f3fSDimitry Andric 10630b57cec5SDimitry Andric void MachineVerifier::verifyPreISelGenericInstruction(const MachineInstr *MI) { 10640b57cec5SDimitry Andric if (isFunctionSelected) 10650b57cec5SDimitry Andric report("Unexpected generic instruction in a Selected function", MI); 10660b57cec5SDimitry Andric 10670b57cec5SDimitry Andric const MCInstrDesc &MCID = MI->getDesc(); 10680b57cec5SDimitry Andric unsigned NumOps = MI->getNumOperands(); 10690b57cec5SDimitry Andric 10705ffd83dbSDimitry Andric // Branches must reference a basic block if they are not indirect 10715ffd83dbSDimitry Andric if (MI->isBranch() && !MI->isIndirectBranch()) { 10725ffd83dbSDimitry Andric bool HasMBB = false; 10735ffd83dbSDimitry Andric for (const MachineOperand &Op : MI->operands()) { 10745ffd83dbSDimitry Andric if (Op.isMBB()) { 10755ffd83dbSDimitry Andric HasMBB = true; 10765ffd83dbSDimitry Andric break; 10775ffd83dbSDimitry Andric } 10785ffd83dbSDimitry Andric } 10795ffd83dbSDimitry Andric 10805ffd83dbSDimitry Andric if (!HasMBB) { 10815ffd83dbSDimitry Andric report("Branch instruction is missing a basic block operand or " 10825ffd83dbSDimitry Andric "isIndirectBranch property", 10835ffd83dbSDimitry Andric MI); 10845ffd83dbSDimitry Andric } 10855ffd83dbSDimitry Andric } 10865ffd83dbSDimitry Andric 10870b57cec5SDimitry Andric // Check types. 10880b57cec5SDimitry Andric SmallVector<LLT, 4> Types; 10890b57cec5SDimitry Andric for (unsigned I = 0, E = std::min(MCID.getNumOperands(), NumOps); 10900b57cec5SDimitry Andric I != E; ++I) { 1091bdd1243dSDimitry Andric if (!MCID.operands()[I].isGenericType()) 10920b57cec5SDimitry Andric continue; 10930b57cec5SDimitry Andric // Generic instructions specify type equality constraints between some of 10940b57cec5SDimitry Andric // their operands. Make sure these are consistent. 1095bdd1243dSDimitry Andric size_t TypeIdx = MCID.operands()[I].getGenericTypeIndex(); 10960b57cec5SDimitry Andric Types.resize(std::max(TypeIdx + 1, Types.size())); 10970b57cec5SDimitry Andric 10980b57cec5SDimitry Andric const MachineOperand *MO = &MI->getOperand(I); 10990b57cec5SDimitry Andric if (!MO->isReg()) { 11000b57cec5SDimitry Andric report("generic instruction must use register operands", MI); 11010b57cec5SDimitry Andric continue; 11020b57cec5SDimitry Andric } 11030b57cec5SDimitry Andric 11040b57cec5SDimitry Andric LLT OpTy = MRI->getType(MO->getReg()); 11050b57cec5SDimitry Andric // Don't report a type mismatch if there is no actual mismatch, only a 11060b57cec5SDimitry Andric // type missing, to reduce noise: 11070b57cec5SDimitry Andric if (OpTy.isValid()) { 11080b57cec5SDimitry Andric // Only the first valid type for a type index will be printed: don't 11090b57cec5SDimitry Andric // overwrite it later so it's always clear which type was expected: 11100b57cec5SDimitry Andric if (!Types[TypeIdx].isValid()) 11110b57cec5SDimitry Andric Types[TypeIdx] = OpTy; 11120b57cec5SDimitry Andric else if (Types[TypeIdx] != OpTy) 11130b57cec5SDimitry Andric report("Type mismatch in generic instruction", MO, I, OpTy); 11140b57cec5SDimitry Andric } else { 11150b57cec5SDimitry Andric // Generic instructions must have types attached to their operands. 11160b57cec5SDimitry Andric report("Generic instruction is missing a virtual register type", MO, I); 11170b57cec5SDimitry Andric } 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric 11200b57cec5SDimitry Andric // Generic opcodes must not have physical register operands. 11210b57cec5SDimitry Andric for (unsigned I = 0; I < MI->getNumOperands(); ++I) { 11220b57cec5SDimitry Andric const MachineOperand *MO = &MI->getOperand(I); 1123bdd1243dSDimitry Andric if (MO->isReg() && MO->getReg().isPhysical()) 11240b57cec5SDimitry Andric report("Generic instruction cannot have physical register", MO, I); 11250b57cec5SDimitry Andric } 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric // Avoid out of bounds in checks below. This was already reported earlier. 11280b57cec5SDimitry Andric if (MI->getNumOperands() < MCID.getNumOperands()) 11290b57cec5SDimitry Andric return; 11300b57cec5SDimitry Andric 11310b57cec5SDimitry Andric StringRef ErrorInfo; 11320b57cec5SDimitry Andric if (!TII->verifyInstruction(*MI, ErrorInfo)) 11330b57cec5SDimitry Andric report(ErrorInfo.data(), MI); 11340b57cec5SDimitry Andric 11350b57cec5SDimitry Andric // Verify properties of various specific instruction types 1136fe6060f1SDimitry Andric unsigned Opc = MI->getOpcode(); 1137fe6060f1SDimitry Andric switch (Opc) { 1138fe6060f1SDimitry Andric case TargetOpcode::G_ASSERT_SEXT: 1139fe6060f1SDimitry Andric case TargetOpcode::G_ASSERT_ZEXT: { 1140fe6060f1SDimitry Andric std::string OpcName = 1141fe6060f1SDimitry Andric Opc == TargetOpcode::G_ASSERT_ZEXT ? "G_ASSERT_ZEXT" : "G_ASSERT_SEXT"; 1142fe6060f1SDimitry Andric if (!MI->getOperand(2).isImm()) { 1143fe6060f1SDimitry Andric report(Twine(OpcName, " expects an immediate operand #2"), MI); 1144fe6060f1SDimitry Andric break; 1145fe6060f1SDimitry Andric } 1146fe6060f1SDimitry Andric 1147fe6060f1SDimitry Andric Register Dst = MI->getOperand(0).getReg(); 1148fe6060f1SDimitry Andric Register Src = MI->getOperand(1).getReg(); 1149fe6060f1SDimitry Andric LLT SrcTy = MRI->getType(Src); 1150fe6060f1SDimitry Andric int64_t Imm = MI->getOperand(2).getImm(); 1151fe6060f1SDimitry Andric if (Imm <= 0) { 1152fe6060f1SDimitry Andric report(Twine(OpcName, " size must be >= 1"), MI); 1153fe6060f1SDimitry Andric break; 1154fe6060f1SDimitry Andric } 1155fe6060f1SDimitry Andric 1156fe6060f1SDimitry Andric if (Imm >= SrcTy.getScalarSizeInBits()) { 1157fe6060f1SDimitry Andric report(Twine(OpcName, " size must be less than source bit width"), MI); 1158fe6060f1SDimitry Andric break; 1159fe6060f1SDimitry Andric } 1160fe6060f1SDimitry Andric 116181ad6265SDimitry Andric const RegisterBank *SrcRB = RBI->getRegBank(Src, *MRI, *TRI); 116281ad6265SDimitry Andric const RegisterBank *DstRB = RBI->getRegBank(Dst, *MRI, *TRI); 116381ad6265SDimitry Andric 116481ad6265SDimitry Andric // Allow only the source bank to be set. 116581ad6265SDimitry Andric if ((SrcRB && DstRB && SrcRB != DstRB) || (DstRB && !SrcRB)) { 116681ad6265SDimitry Andric report(Twine(OpcName, " cannot change register bank"), MI); 1167fe6060f1SDimitry Andric break; 1168fe6060f1SDimitry Andric } 1169fe6060f1SDimitry Andric 117081ad6265SDimitry Andric // Don't allow a class change. Do allow member class->regbank. 117181ad6265SDimitry Andric const TargetRegisterClass *DstRC = MRI->getRegClassOrNull(Dst); 117281ad6265SDimitry Andric if (DstRC && DstRC != MRI->getRegClassOrNull(Src)) { 1173fe6060f1SDimitry Andric report( 1174fe6060f1SDimitry Andric Twine(OpcName, " source and destination register classes must match"), 1175fe6060f1SDimitry Andric MI); 117681ad6265SDimitry Andric break; 117781ad6265SDimitry Andric } 1178fe6060f1SDimitry Andric 1179fe6060f1SDimitry Andric break; 1180fe6060f1SDimitry Andric } 1181fe6060f1SDimitry Andric 11820b57cec5SDimitry Andric case TargetOpcode::G_CONSTANT: 11830b57cec5SDimitry Andric case TargetOpcode::G_FCONSTANT: { 11840b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 11850b57cec5SDimitry Andric if (DstTy.isVector()) 11860b57cec5SDimitry Andric report("Instruction cannot use a vector result type", MI); 11870b57cec5SDimitry Andric 11880b57cec5SDimitry Andric if (MI->getOpcode() == TargetOpcode::G_CONSTANT) { 11890b57cec5SDimitry Andric if (!MI->getOperand(1).isCImm()) { 11900b57cec5SDimitry Andric report("G_CONSTANT operand must be cimm", MI); 11910b57cec5SDimitry Andric break; 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric const ConstantInt *CI = MI->getOperand(1).getCImm(); 11950b57cec5SDimitry Andric if (CI->getBitWidth() != DstTy.getSizeInBits()) 11960b57cec5SDimitry Andric report("inconsistent constant size", MI); 11970b57cec5SDimitry Andric } else { 11980b57cec5SDimitry Andric if (!MI->getOperand(1).isFPImm()) { 11990b57cec5SDimitry Andric report("G_FCONSTANT operand must be fpimm", MI); 12000b57cec5SDimitry Andric break; 12010b57cec5SDimitry Andric } 12020b57cec5SDimitry Andric const ConstantFP *CF = MI->getOperand(1).getFPImm(); 12030b57cec5SDimitry Andric 12040b57cec5SDimitry Andric if (APFloat::getSizeInBits(CF->getValueAPF().getSemantics()) != 12050b57cec5SDimitry Andric DstTy.getSizeInBits()) { 12060b57cec5SDimitry Andric report("inconsistent constant size", MI); 12070b57cec5SDimitry Andric } 12080b57cec5SDimitry Andric } 12090b57cec5SDimitry Andric 12100b57cec5SDimitry Andric break; 12110b57cec5SDimitry Andric } 12120b57cec5SDimitry Andric case TargetOpcode::G_LOAD: 12130b57cec5SDimitry Andric case TargetOpcode::G_STORE: 12140b57cec5SDimitry Andric case TargetOpcode::G_ZEXTLOAD: 12150b57cec5SDimitry Andric case TargetOpcode::G_SEXTLOAD: { 12160b57cec5SDimitry Andric LLT ValTy = MRI->getType(MI->getOperand(0).getReg()); 12170b57cec5SDimitry Andric LLT PtrTy = MRI->getType(MI->getOperand(1).getReg()); 12180b57cec5SDimitry Andric if (!PtrTy.isPointer()) 12190b57cec5SDimitry Andric report("Generic memory instruction must access a pointer", MI); 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric // Generic loads and stores must have a single MachineMemOperand 12220b57cec5SDimitry Andric // describing that access. 12230b57cec5SDimitry Andric if (!MI->hasOneMemOperand()) { 12240b57cec5SDimitry Andric report("Generic instruction accessing memory must have one mem operand", 12250b57cec5SDimitry Andric MI); 12260b57cec5SDimitry Andric } else { 12270b57cec5SDimitry Andric const MachineMemOperand &MMO = **MI->memoperands_begin(); 12280b57cec5SDimitry Andric if (MI->getOpcode() == TargetOpcode::G_ZEXTLOAD || 12290b57cec5SDimitry Andric MI->getOpcode() == TargetOpcode::G_SEXTLOAD) { 1230*0fca6ea1SDimitry Andric if (TypeSize::isKnownGE(MMO.getSizeInBits().getValue(), 1231*0fca6ea1SDimitry Andric ValTy.getSizeInBits())) 12320b57cec5SDimitry Andric report("Generic extload must have a narrower memory type", MI); 12330b57cec5SDimitry Andric } else if (MI->getOpcode() == TargetOpcode::G_LOAD) { 1234*0fca6ea1SDimitry Andric if (TypeSize::isKnownGT(MMO.getSize().getValue(), 1235*0fca6ea1SDimitry Andric ValTy.getSizeInBytes())) 12360b57cec5SDimitry Andric report("load memory size cannot exceed result size", MI); 12370b57cec5SDimitry Andric } else if (MI->getOpcode() == TargetOpcode::G_STORE) { 1238*0fca6ea1SDimitry Andric if (TypeSize::isKnownLT(ValTy.getSizeInBytes(), 1239*0fca6ea1SDimitry Andric MMO.getSize().getValue())) 12400b57cec5SDimitry Andric report("store memory size cannot exceed value size", MI); 12410b57cec5SDimitry Andric } 124281ad6265SDimitry Andric 124381ad6265SDimitry Andric const AtomicOrdering Order = MMO.getSuccessOrdering(); 124481ad6265SDimitry Andric if (Opc == TargetOpcode::G_STORE) { 124581ad6265SDimitry Andric if (Order == AtomicOrdering::Acquire || 124681ad6265SDimitry Andric Order == AtomicOrdering::AcquireRelease) 124781ad6265SDimitry Andric report("atomic store cannot use acquire ordering", MI); 124881ad6265SDimitry Andric 124981ad6265SDimitry Andric } else { 125081ad6265SDimitry Andric if (Order == AtomicOrdering::Release || 125181ad6265SDimitry Andric Order == AtomicOrdering::AcquireRelease) 125281ad6265SDimitry Andric report("atomic load cannot use release ordering", MI); 125381ad6265SDimitry Andric } 12540b57cec5SDimitry Andric } 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric break; 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric case TargetOpcode::G_PHI: { 12590b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1260e8d8bef9SDimitry Andric if (!DstTy.isValid() || !all_of(drop_begin(MI->operands()), 12610b57cec5SDimitry Andric [this, &DstTy](const MachineOperand &MO) { 12620b57cec5SDimitry Andric if (!MO.isReg()) 12630b57cec5SDimitry Andric return true; 12640b57cec5SDimitry Andric LLT Ty = MRI->getType(MO.getReg()); 12650b57cec5SDimitry Andric if (!Ty.isValid() || (Ty != DstTy)) 12660b57cec5SDimitry Andric return false; 12670b57cec5SDimitry Andric return true; 12680b57cec5SDimitry Andric })) 12690b57cec5SDimitry Andric report("Generic Instruction G_PHI has operands with incompatible/missing " 12700b57cec5SDimitry Andric "types", 12710b57cec5SDimitry Andric MI); 12720b57cec5SDimitry Andric break; 12730b57cec5SDimitry Andric } 12740b57cec5SDimitry Andric case TargetOpcode::G_BITCAST: { 12750b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 12760b57cec5SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 12770b57cec5SDimitry Andric if (!DstTy.isValid() || !SrcTy.isValid()) 12780b57cec5SDimitry Andric break; 12790b57cec5SDimitry Andric 12800b57cec5SDimitry Andric if (SrcTy.isPointer() != DstTy.isPointer()) 12810b57cec5SDimitry Andric report("bitcast cannot convert between pointers and other types", MI); 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric if (SrcTy.getSizeInBits() != DstTy.getSizeInBits()) 12840b57cec5SDimitry Andric report("bitcast sizes must match", MI); 12855ffd83dbSDimitry Andric 12865ffd83dbSDimitry Andric if (SrcTy == DstTy) 12875ffd83dbSDimitry Andric report("bitcast must change the type", MI); 12885ffd83dbSDimitry Andric 12890b57cec5SDimitry Andric break; 12900b57cec5SDimitry Andric } 12910b57cec5SDimitry Andric case TargetOpcode::G_INTTOPTR: 12920b57cec5SDimitry Andric case TargetOpcode::G_PTRTOINT: 12930b57cec5SDimitry Andric case TargetOpcode::G_ADDRSPACE_CAST: { 12940b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 12950b57cec5SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 12960b57cec5SDimitry Andric if (!DstTy.isValid() || !SrcTy.isValid()) 12970b57cec5SDimitry Andric break; 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric verifyVectorElementMatch(DstTy, SrcTy, MI); 13000b57cec5SDimitry Andric 13010b57cec5SDimitry Andric DstTy = DstTy.getScalarType(); 13020b57cec5SDimitry Andric SrcTy = SrcTy.getScalarType(); 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric if (MI->getOpcode() == TargetOpcode::G_INTTOPTR) { 13050b57cec5SDimitry Andric if (!DstTy.isPointer()) 13060b57cec5SDimitry Andric report("inttoptr result type must be a pointer", MI); 13070b57cec5SDimitry Andric if (SrcTy.isPointer()) 13080b57cec5SDimitry Andric report("inttoptr source type must not be a pointer", MI); 13090b57cec5SDimitry Andric } else if (MI->getOpcode() == TargetOpcode::G_PTRTOINT) { 13100b57cec5SDimitry Andric if (!SrcTy.isPointer()) 13110b57cec5SDimitry Andric report("ptrtoint source type must be a pointer", MI); 13120b57cec5SDimitry Andric if (DstTy.isPointer()) 13130b57cec5SDimitry Andric report("ptrtoint result type must not be a pointer", MI); 13140b57cec5SDimitry Andric } else { 13150b57cec5SDimitry Andric assert(MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST); 13160b57cec5SDimitry Andric if (!SrcTy.isPointer() || !DstTy.isPointer()) 13170b57cec5SDimitry Andric report("addrspacecast types must be pointers", MI); 13180b57cec5SDimitry Andric else { 13190b57cec5SDimitry Andric if (SrcTy.getAddressSpace() == DstTy.getAddressSpace()) 13200b57cec5SDimitry Andric report("addrspacecast must convert different address spaces", MI); 13210b57cec5SDimitry Andric } 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric break; 13250b57cec5SDimitry Andric } 1326480093f4SDimitry Andric case TargetOpcode::G_PTR_ADD: { 13270b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 13280b57cec5SDimitry Andric LLT PtrTy = MRI->getType(MI->getOperand(1).getReg()); 13290b57cec5SDimitry Andric LLT OffsetTy = MRI->getType(MI->getOperand(2).getReg()); 13300b57cec5SDimitry Andric if (!DstTy.isValid() || !PtrTy.isValid() || !OffsetTy.isValid()) 13310b57cec5SDimitry Andric break; 13320b57cec5SDimitry Andric 1333*0fca6ea1SDimitry Andric if (!PtrTy.isPointerOrPointerVector()) 13340b57cec5SDimitry Andric report("gep first operand must be a pointer", MI); 13350b57cec5SDimitry Andric 1336*0fca6ea1SDimitry Andric if (OffsetTy.isPointerOrPointerVector()) 13370b57cec5SDimitry Andric report("gep offset operand must not be a pointer", MI); 13380b57cec5SDimitry Andric 1339*0fca6ea1SDimitry Andric if (PtrTy.isPointerOrPointerVector()) { 1340*0fca6ea1SDimitry Andric const DataLayout &DL = MF->getDataLayout(); 1341*0fca6ea1SDimitry Andric unsigned AS = PtrTy.getAddressSpace(); 1342*0fca6ea1SDimitry Andric unsigned IndexSizeInBits = DL.getIndexSize(AS) * 8; 1343*0fca6ea1SDimitry Andric if (OffsetTy.getScalarSizeInBits() != IndexSizeInBits) { 1344*0fca6ea1SDimitry Andric report("gep offset operand must match index size for address space", 1345*0fca6ea1SDimitry Andric MI); 1346*0fca6ea1SDimitry Andric } 1347*0fca6ea1SDimitry Andric } 1348*0fca6ea1SDimitry Andric 13490b57cec5SDimitry Andric // TODO: Is the offset allowed to be a scalar with a vector? 13500b57cec5SDimitry Andric break; 13510b57cec5SDimitry Andric } 13525ffd83dbSDimitry Andric case TargetOpcode::G_PTRMASK: { 13535ffd83dbSDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 13545ffd83dbSDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 13555ffd83dbSDimitry Andric LLT MaskTy = MRI->getType(MI->getOperand(2).getReg()); 13565ffd83dbSDimitry Andric if (!DstTy.isValid() || !SrcTy.isValid() || !MaskTy.isValid()) 13575ffd83dbSDimitry Andric break; 13585ffd83dbSDimitry Andric 1359*0fca6ea1SDimitry Andric if (!DstTy.isPointerOrPointerVector()) 13605ffd83dbSDimitry Andric report("ptrmask result type must be a pointer", MI); 13615ffd83dbSDimitry Andric 13625ffd83dbSDimitry Andric if (!MaskTy.getScalarType().isScalar()) 13635ffd83dbSDimitry Andric report("ptrmask mask type must be an integer", MI); 13645ffd83dbSDimitry Andric 13655ffd83dbSDimitry Andric verifyVectorElementMatch(DstTy, MaskTy, MI); 13665ffd83dbSDimitry Andric break; 13675ffd83dbSDimitry Andric } 13680b57cec5SDimitry Andric case TargetOpcode::G_SEXT: 13690b57cec5SDimitry Andric case TargetOpcode::G_ZEXT: 13700b57cec5SDimitry Andric case TargetOpcode::G_ANYEXT: 13710b57cec5SDimitry Andric case TargetOpcode::G_TRUNC: 13720b57cec5SDimitry Andric case TargetOpcode::G_FPEXT: 13730b57cec5SDimitry Andric case TargetOpcode::G_FPTRUNC: { 13740b57cec5SDimitry Andric // Number of operands and presense of types is already checked (and 13750b57cec5SDimitry Andric // reported in case of any issues), so no need to report them again. As 13760b57cec5SDimitry Andric // we're trying to report as many issues as possible at once, however, the 13770b57cec5SDimitry Andric // instructions aren't guaranteed to have the right number of operands or 13780b57cec5SDimitry Andric // types attached to them at this point 13790b57cec5SDimitry Andric assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}"); 13800b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 13810b57cec5SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 13820b57cec5SDimitry Andric if (!DstTy.isValid() || !SrcTy.isValid()) 13830b57cec5SDimitry Andric break; 13840b57cec5SDimitry Andric 1385*0fca6ea1SDimitry Andric if (DstTy.isPointerOrPointerVector() || SrcTy.isPointerOrPointerVector()) 13860b57cec5SDimitry Andric report("Generic extend/truncate can not operate on pointers", MI); 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric verifyVectorElementMatch(DstTy, SrcTy, MI); 13890b57cec5SDimitry Andric 1390*0fca6ea1SDimitry Andric unsigned DstSize = DstTy.getScalarSizeInBits(); 1391*0fca6ea1SDimitry Andric unsigned SrcSize = SrcTy.getScalarSizeInBits(); 13920b57cec5SDimitry Andric switch (MI->getOpcode()) { 13930b57cec5SDimitry Andric default: 13940b57cec5SDimitry Andric if (DstSize <= SrcSize) 13950b57cec5SDimitry Andric report("Generic extend has destination type no larger than source", MI); 13960b57cec5SDimitry Andric break; 13970b57cec5SDimitry Andric case TargetOpcode::G_TRUNC: 13980b57cec5SDimitry Andric case TargetOpcode::G_FPTRUNC: 13990b57cec5SDimitry Andric if (DstSize >= SrcSize) 14000b57cec5SDimitry Andric report("Generic truncate has destination type no smaller than source", 14010b57cec5SDimitry Andric MI); 14020b57cec5SDimitry Andric break; 14030b57cec5SDimitry Andric } 14040b57cec5SDimitry Andric break; 14050b57cec5SDimitry Andric } 14060b57cec5SDimitry Andric case TargetOpcode::G_SELECT: { 14070b57cec5SDimitry Andric LLT SelTy = MRI->getType(MI->getOperand(0).getReg()); 14080b57cec5SDimitry Andric LLT CondTy = MRI->getType(MI->getOperand(1).getReg()); 14090b57cec5SDimitry Andric if (!SelTy.isValid() || !CondTy.isValid()) 14100b57cec5SDimitry Andric break; 14110b57cec5SDimitry Andric 14120b57cec5SDimitry Andric // Scalar condition select on a vector is valid. 14130b57cec5SDimitry Andric if (CondTy.isVector()) 14140b57cec5SDimitry Andric verifyVectorElementMatch(SelTy, CondTy, MI); 14150b57cec5SDimitry Andric break; 14160b57cec5SDimitry Andric } 14170b57cec5SDimitry Andric case TargetOpcode::G_MERGE_VALUES: { 14180b57cec5SDimitry Andric // G_MERGE_VALUES should only be used to merge scalars into a larger scalar, 14190b57cec5SDimitry Andric // e.g. s2N = MERGE sN, sN 14200b57cec5SDimitry Andric // Merging multiple scalars into a vector is not allowed, should use 14210b57cec5SDimitry Andric // G_BUILD_VECTOR for that. 14220b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 14230b57cec5SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 14240b57cec5SDimitry Andric if (DstTy.isVector() || SrcTy.isVector()) 14250b57cec5SDimitry Andric report("G_MERGE_VALUES cannot operate on vectors", MI); 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric const unsigned NumOps = MI->getNumOperands(); 14280b57cec5SDimitry Andric if (DstTy.getSizeInBits() != SrcTy.getSizeInBits() * (NumOps - 1)) 14290b57cec5SDimitry Andric report("G_MERGE_VALUES result size is inconsistent", MI); 14300b57cec5SDimitry Andric 14310b57cec5SDimitry Andric for (unsigned I = 2; I != NumOps; ++I) { 14320b57cec5SDimitry Andric if (MRI->getType(MI->getOperand(I).getReg()) != SrcTy) 14330b57cec5SDimitry Andric report("G_MERGE_VALUES source types do not match", MI); 14340b57cec5SDimitry Andric } 14350b57cec5SDimitry Andric 14360b57cec5SDimitry Andric break; 14370b57cec5SDimitry Andric } 14380b57cec5SDimitry Andric case TargetOpcode::G_UNMERGE_VALUES: { 1439bdd1243dSDimitry Andric unsigned NumDsts = MI->getNumOperands() - 1; 14400b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1441bdd1243dSDimitry Andric for (unsigned i = 1; i < NumDsts; ++i) { 1442bdd1243dSDimitry Andric if (MRI->getType(MI->getOperand(i).getReg()) != DstTy) { 14430b57cec5SDimitry Andric report("G_UNMERGE_VALUES destination types do not match", MI); 1444bdd1243dSDimitry Andric break; 14450b57cec5SDimitry Andric } 1446bdd1243dSDimitry Andric } 1447bdd1243dSDimitry Andric 1448bdd1243dSDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(NumDsts).getReg()); 1449bdd1243dSDimitry Andric if (DstTy.isVector()) { 1450bdd1243dSDimitry Andric // This case is the converse of G_CONCAT_VECTORS. 1451bdd1243dSDimitry Andric if (!SrcTy.isVector() || SrcTy.getScalarType() != DstTy.getScalarType() || 1452*0fca6ea1SDimitry Andric SrcTy.isScalableVector() != DstTy.isScalableVector() || 1453*0fca6ea1SDimitry Andric SrcTy.getSizeInBits() != NumDsts * DstTy.getSizeInBits()) 1454bdd1243dSDimitry Andric report("G_UNMERGE_VALUES source operand does not match vector " 1455bdd1243dSDimitry Andric "destination operands", 14560b57cec5SDimitry Andric MI); 1457bdd1243dSDimitry Andric } else if (SrcTy.isVector()) { 1458bdd1243dSDimitry Andric // This case is the converse of G_BUILD_VECTOR, but relaxed to allow 1459bdd1243dSDimitry Andric // mismatched types as long as the total size matches: 1460bdd1243dSDimitry Andric // %0:_(s64), %1:_(s64) = G_UNMERGE_VALUES %2:_(<4 x s32>) 1461bdd1243dSDimitry Andric if (SrcTy.getSizeInBits() != NumDsts * DstTy.getSizeInBits()) 1462bdd1243dSDimitry Andric report("G_UNMERGE_VALUES vector source operand does not match scalar " 1463bdd1243dSDimitry Andric "destination operands", 1464bdd1243dSDimitry Andric MI); 1465bdd1243dSDimitry Andric } else { 1466bdd1243dSDimitry Andric // This case is the converse of G_MERGE_VALUES. 1467bdd1243dSDimitry Andric if (SrcTy.getSizeInBits() != NumDsts * DstTy.getSizeInBits()) { 1468bdd1243dSDimitry Andric report("G_UNMERGE_VALUES scalar source operand does not match scalar " 1469bdd1243dSDimitry Andric "destination operands", 1470bdd1243dSDimitry Andric MI); 1471bdd1243dSDimitry Andric } 14720b57cec5SDimitry Andric } 14730b57cec5SDimitry Andric break; 14740b57cec5SDimitry Andric } 14750b57cec5SDimitry Andric case TargetOpcode::G_BUILD_VECTOR: { 14760b57cec5SDimitry Andric // Source types must be scalars, dest type a vector. Total size of scalars 14770b57cec5SDimitry Andric // must match the dest vector size. 14780b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 14790b57cec5SDimitry Andric LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg()); 14800b57cec5SDimitry Andric if (!DstTy.isVector() || SrcEltTy.isVector()) { 14810b57cec5SDimitry Andric report("G_BUILD_VECTOR must produce a vector from scalar operands", MI); 14820b57cec5SDimitry Andric break; 14830b57cec5SDimitry Andric } 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric if (DstTy.getElementType() != SrcEltTy) 14860b57cec5SDimitry Andric report("G_BUILD_VECTOR result element type must match source type", MI); 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric if (DstTy.getNumElements() != MI->getNumOperands() - 1) 14890b57cec5SDimitry Andric report("G_BUILD_VECTOR must have an operand for each elemement", MI); 14900b57cec5SDimitry Andric 14914824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2)) 14924824e7fdSDimitry Andric if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg())) 14930b57cec5SDimitry Andric report("G_BUILD_VECTOR source operand types are not homogeneous", MI); 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric break; 14960b57cec5SDimitry Andric } 14970b57cec5SDimitry Andric case TargetOpcode::G_BUILD_VECTOR_TRUNC: { 14980b57cec5SDimitry Andric // Source types must be scalars, dest type a vector. Scalar types must be 14990b57cec5SDimitry Andric // larger than the dest vector elt type, as this is a truncating operation. 15000b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 15010b57cec5SDimitry Andric LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg()); 15020b57cec5SDimitry Andric if (!DstTy.isVector() || SrcEltTy.isVector()) 15030b57cec5SDimitry Andric report("G_BUILD_VECTOR_TRUNC must produce a vector from scalar operands", 15040b57cec5SDimitry Andric MI); 15054824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2)) 15064824e7fdSDimitry Andric if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg())) 15070b57cec5SDimitry Andric report("G_BUILD_VECTOR_TRUNC source operand types are not homogeneous", 15080b57cec5SDimitry Andric MI); 15090b57cec5SDimitry Andric if (SrcEltTy.getSizeInBits() <= DstTy.getElementType().getSizeInBits()) 15100b57cec5SDimitry Andric report("G_BUILD_VECTOR_TRUNC source operand types are not larger than " 15110b57cec5SDimitry Andric "dest elt type", 15120b57cec5SDimitry Andric MI); 15130b57cec5SDimitry Andric break; 15140b57cec5SDimitry Andric } 15150b57cec5SDimitry Andric case TargetOpcode::G_CONCAT_VECTORS: { 15160b57cec5SDimitry Andric // Source types should be vectors, and total size should match the dest 15170b57cec5SDimitry Andric // vector size. 15180b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 15190b57cec5SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 15200b57cec5SDimitry Andric if (!DstTy.isVector() || !SrcTy.isVector()) 15210b57cec5SDimitry Andric report("G_CONCAT_VECTOR requires vector source and destination operands", 15220b57cec5SDimitry Andric MI); 1523fe6060f1SDimitry Andric 1524fe6060f1SDimitry Andric if (MI->getNumOperands() < 3) 1525fe6060f1SDimitry Andric report("G_CONCAT_VECTOR requires at least 2 source operands", MI); 1526fe6060f1SDimitry Andric 15274824e7fdSDimitry Andric for (const MachineOperand &MO : llvm::drop_begin(MI->operands(), 2)) 15284824e7fdSDimitry Andric if (MRI->getType(MI->getOperand(1).getReg()) != MRI->getType(MO.getReg())) 15290b57cec5SDimitry Andric report("G_CONCAT_VECTOR source operand types are not homogeneous", MI); 1530*0fca6ea1SDimitry Andric if (DstTy.getElementCount() != 1531*0fca6ea1SDimitry Andric SrcTy.getElementCount() * (MI->getNumOperands() - 1)) 15320b57cec5SDimitry Andric report("G_CONCAT_VECTOR num dest and source elements should match", MI); 15330b57cec5SDimitry Andric break; 15340b57cec5SDimitry Andric } 15350b57cec5SDimitry Andric case TargetOpcode::G_ICMP: 15360b57cec5SDimitry Andric case TargetOpcode::G_FCMP: { 15370b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 15380b57cec5SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(2).getReg()); 15390b57cec5SDimitry Andric 15400b57cec5SDimitry Andric if ((DstTy.isVector() != SrcTy.isVector()) || 1541*0fca6ea1SDimitry Andric (DstTy.isVector() && 1542*0fca6ea1SDimitry Andric DstTy.getElementCount() != SrcTy.getElementCount())) 15430b57cec5SDimitry Andric report("Generic vector icmp/fcmp must preserve number of lanes", MI); 15440b57cec5SDimitry Andric 15450b57cec5SDimitry Andric break; 15460b57cec5SDimitry Andric } 1547*0fca6ea1SDimitry Andric case TargetOpcode::G_SCMP: 1548*0fca6ea1SDimitry Andric case TargetOpcode::G_UCMP: { 1549*0fca6ea1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1550*0fca6ea1SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 1551*0fca6ea1SDimitry Andric LLT SrcTy2 = MRI->getType(MI->getOperand(2).getReg()); 1552*0fca6ea1SDimitry Andric 1553*0fca6ea1SDimitry Andric if (SrcTy.isPointerOrPointerVector() || SrcTy2.isPointerOrPointerVector()) { 1554*0fca6ea1SDimitry Andric report("Generic scmp/ucmp does not support pointers as operands", MI); 1555*0fca6ea1SDimitry Andric break; 1556*0fca6ea1SDimitry Andric } 1557*0fca6ea1SDimitry Andric 1558*0fca6ea1SDimitry Andric if (DstTy.isPointerOrPointerVector()) { 1559*0fca6ea1SDimitry Andric report("Generic scmp/ucmp does not support pointers as a result", MI); 1560*0fca6ea1SDimitry Andric break; 1561*0fca6ea1SDimitry Andric } 1562*0fca6ea1SDimitry Andric 1563*0fca6ea1SDimitry Andric if ((DstTy.isVector() != SrcTy.isVector()) || 1564*0fca6ea1SDimitry Andric (DstTy.isVector() && 1565*0fca6ea1SDimitry Andric DstTy.getElementCount() != SrcTy.getElementCount())) { 1566*0fca6ea1SDimitry Andric report("Generic vector scmp/ucmp must preserve number of lanes", MI); 1567*0fca6ea1SDimitry Andric break; 1568*0fca6ea1SDimitry Andric } 1569*0fca6ea1SDimitry Andric 1570*0fca6ea1SDimitry Andric if (SrcTy != SrcTy2) { 1571*0fca6ea1SDimitry Andric report("Generic scmp/ucmp must have same input types", MI); 1572*0fca6ea1SDimitry Andric break; 1573*0fca6ea1SDimitry Andric } 1574*0fca6ea1SDimitry Andric 1575*0fca6ea1SDimitry Andric break; 1576*0fca6ea1SDimitry Andric } 15770b57cec5SDimitry Andric case TargetOpcode::G_EXTRACT: { 15780b57cec5SDimitry Andric const MachineOperand &SrcOp = MI->getOperand(1); 15790b57cec5SDimitry Andric if (!SrcOp.isReg()) { 15800b57cec5SDimitry Andric report("extract source must be a register", MI); 15810b57cec5SDimitry Andric break; 15820b57cec5SDimitry Andric } 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric const MachineOperand &OffsetOp = MI->getOperand(2); 15850b57cec5SDimitry Andric if (!OffsetOp.isImm()) { 15860b57cec5SDimitry Andric report("extract offset must be a constant", MI); 15870b57cec5SDimitry Andric break; 15880b57cec5SDimitry Andric } 15890b57cec5SDimitry Andric 15900b57cec5SDimitry Andric unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits(); 15910b57cec5SDimitry Andric unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits(); 15920b57cec5SDimitry Andric if (SrcSize == DstSize) 15930b57cec5SDimitry Andric report("extract source must be larger than result", MI); 15940b57cec5SDimitry Andric 15950b57cec5SDimitry Andric if (DstSize + OffsetOp.getImm() > SrcSize) 15960b57cec5SDimitry Andric report("extract reads past end of register", MI); 15970b57cec5SDimitry Andric break; 15980b57cec5SDimitry Andric } 15990b57cec5SDimitry Andric case TargetOpcode::G_INSERT: { 16000b57cec5SDimitry Andric const MachineOperand &SrcOp = MI->getOperand(2); 16010b57cec5SDimitry Andric if (!SrcOp.isReg()) { 16020b57cec5SDimitry Andric report("insert source must be a register", MI); 16030b57cec5SDimitry Andric break; 16040b57cec5SDimitry Andric } 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric const MachineOperand &OffsetOp = MI->getOperand(3); 16070b57cec5SDimitry Andric if (!OffsetOp.isImm()) { 16080b57cec5SDimitry Andric report("insert offset must be a constant", MI); 16090b57cec5SDimitry Andric break; 16100b57cec5SDimitry Andric } 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric unsigned DstSize = MRI->getType(MI->getOperand(0).getReg()).getSizeInBits(); 16130b57cec5SDimitry Andric unsigned SrcSize = MRI->getType(SrcOp.getReg()).getSizeInBits(); 16140b57cec5SDimitry Andric 16150b57cec5SDimitry Andric if (DstSize <= SrcSize) 16160b57cec5SDimitry Andric report("inserted size must be smaller than total register", MI); 16170b57cec5SDimitry Andric 16180b57cec5SDimitry Andric if (SrcSize + OffsetOp.getImm() > DstSize) 16190b57cec5SDimitry Andric report("insert writes past end of register", MI); 16200b57cec5SDimitry Andric 16210b57cec5SDimitry Andric break; 16220b57cec5SDimitry Andric } 16230b57cec5SDimitry Andric case TargetOpcode::G_JUMP_TABLE: { 16240b57cec5SDimitry Andric if (!MI->getOperand(1).isJTI()) 16250b57cec5SDimitry Andric report("G_JUMP_TABLE source operand must be a jump table index", MI); 16260b57cec5SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 16270b57cec5SDimitry Andric if (!DstTy.isPointer()) 16280b57cec5SDimitry Andric report("G_JUMP_TABLE dest operand must have a pointer type", MI); 16290b57cec5SDimitry Andric break; 16300b57cec5SDimitry Andric } 16310b57cec5SDimitry Andric case TargetOpcode::G_BRJT: { 16320b57cec5SDimitry Andric if (!MRI->getType(MI->getOperand(0).getReg()).isPointer()) 16330b57cec5SDimitry Andric report("G_BRJT src operand 0 must be a pointer type", MI); 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric if (!MI->getOperand(1).isJTI()) 16360b57cec5SDimitry Andric report("G_BRJT src operand 1 must be a jump table index", MI); 16370b57cec5SDimitry Andric 16380b57cec5SDimitry Andric const auto &IdxOp = MI->getOperand(2); 16390b57cec5SDimitry Andric if (!IdxOp.isReg() || MRI->getType(IdxOp.getReg()).isPointer()) 16400b57cec5SDimitry Andric report("G_BRJT src operand 2 must be a scalar reg type", MI); 16410b57cec5SDimitry Andric break; 16420b57cec5SDimitry Andric } 16430b57cec5SDimitry Andric case TargetOpcode::G_INTRINSIC: 16445f757f3fSDimitry Andric case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: 16455f757f3fSDimitry Andric case TargetOpcode::G_INTRINSIC_CONVERGENT: 16465f757f3fSDimitry Andric case TargetOpcode::G_INTRINSIC_CONVERGENT_W_SIDE_EFFECTS: { 16470b57cec5SDimitry Andric // TODO: Should verify number of def and use operands, but the current 16480b57cec5SDimitry Andric // interface requires passing in IR types for mangling. 16490b57cec5SDimitry Andric const MachineOperand &IntrIDOp = MI->getOperand(MI->getNumExplicitDefs()); 16500b57cec5SDimitry Andric if (!IntrIDOp.isIntrinsicID()) { 16510b57cec5SDimitry Andric report("G_INTRINSIC first src operand must be an intrinsic ID", MI); 16520b57cec5SDimitry Andric break; 16530b57cec5SDimitry Andric } 16540b57cec5SDimitry Andric 16555f757f3fSDimitry Andric if (!verifyGIntrinsicSideEffects(MI)) 16560b57cec5SDimitry Andric break; 16575f757f3fSDimitry Andric if (!verifyGIntrinsicConvergence(MI)) 16580b57cec5SDimitry Andric break; 1659e8d8bef9SDimitry Andric 16608bcb0991SDimitry Andric break; 16618bcb0991SDimitry Andric } 16628bcb0991SDimitry Andric case TargetOpcode::G_SEXT_INREG: { 16638bcb0991SDimitry Andric if (!MI->getOperand(2).isImm()) { 16648bcb0991SDimitry Andric report("G_SEXT_INREG expects an immediate operand #2", MI); 16658bcb0991SDimitry Andric break; 16668bcb0991SDimitry Andric } 16670b57cec5SDimitry Andric 16688bcb0991SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 16698bcb0991SDimitry Andric int64_t Imm = MI->getOperand(2).getImm(); 16708bcb0991SDimitry Andric if (Imm <= 0) 16718bcb0991SDimitry Andric report("G_SEXT_INREG size must be >= 1", MI); 16728bcb0991SDimitry Andric if (Imm >= SrcTy.getScalarSizeInBits()) 16738bcb0991SDimitry Andric report("G_SEXT_INREG size must be less than source bit width", MI); 16748bcb0991SDimitry Andric break; 16758bcb0991SDimitry Andric } 16765f757f3fSDimitry Andric case TargetOpcode::G_BSWAP: { 16775f757f3fSDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 16785f757f3fSDimitry Andric if (DstTy.getScalarSizeInBits() % 16 != 0) 16795f757f3fSDimitry Andric report("G_BSWAP size must be a multiple of 16 bits", MI); 16805f757f3fSDimitry Andric break; 16815f757f3fSDimitry Andric } 1682*0fca6ea1SDimitry Andric case TargetOpcode::G_VSCALE: { 1683*0fca6ea1SDimitry Andric if (!MI->getOperand(1).isCImm()) { 1684*0fca6ea1SDimitry Andric report("G_VSCALE operand must be cimm", MI); 1685*0fca6ea1SDimitry Andric break; 1686*0fca6ea1SDimitry Andric } 1687*0fca6ea1SDimitry Andric if (MI->getOperand(1).getCImm()->isZero()) { 1688*0fca6ea1SDimitry Andric report("G_VSCALE immediate cannot be zero", MI); 1689*0fca6ea1SDimitry Andric break; 1690*0fca6ea1SDimitry Andric } 1691*0fca6ea1SDimitry Andric break; 1692*0fca6ea1SDimitry Andric } 1693*0fca6ea1SDimitry Andric case TargetOpcode::G_INSERT_SUBVECTOR: { 1694*0fca6ea1SDimitry Andric const MachineOperand &Src0Op = MI->getOperand(1); 1695*0fca6ea1SDimitry Andric if (!Src0Op.isReg()) { 1696*0fca6ea1SDimitry Andric report("G_INSERT_SUBVECTOR first source must be a register", MI); 1697*0fca6ea1SDimitry Andric break; 1698*0fca6ea1SDimitry Andric } 1699*0fca6ea1SDimitry Andric 1700*0fca6ea1SDimitry Andric const MachineOperand &Src1Op = MI->getOperand(2); 1701*0fca6ea1SDimitry Andric if (!Src1Op.isReg()) { 1702*0fca6ea1SDimitry Andric report("G_INSERT_SUBVECTOR second source must be a register", MI); 1703*0fca6ea1SDimitry Andric break; 1704*0fca6ea1SDimitry Andric } 1705*0fca6ea1SDimitry Andric 1706*0fca6ea1SDimitry Andric const MachineOperand &IndexOp = MI->getOperand(3); 1707*0fca6ea1SDimitry Andric if (!IndexOp.isImm()) { 1708*0fca6ea1SDimitry Andric report("G_INSERT_SUBVECTOR index must be an immediate", MI); 1709*0fca6ea1SDimitry Andric break; 1710*0fca6ea1SDimitry Andric } 1711*0fca6ea1SDimitry Andric 1712*0fca6ea1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1713*0fca6ea1SDimitry Andric LLT Src0Ty = MRI->getType(Src0Op.getReg()); 1714*0fca6ea1SDimitry Andric LLT Src1Ty = MRI->getType(Src1Op.getReg()); 1715*0fca6ea1SDimitry Andric 1716*0fca6ea1SDimitry Andric if (!DstTy.isVector()) { 1717*0fca6ea1SDimitry Andric report("Destination type must be a vector", MI); 1718*0fca6ea1SDimitry Andric break; 1719*0fca6ea1SDimitry Andric } 1720*0fca6ea1SDimitry Andric 1721*0fca6ea1SDimitry Andric if (!Src0Ty.isVector()) { 1722*0fca6ea1SDimitry Andric report("First source must be a vector", MI); 1723*0fca6ea1SDimitry Andric break; 1724*0fca6ea1SDimitry Andric } 1725*0fca6ea1SDimitry Andric 1726*0fca6ea1SDimitry Andric if (!Src1Ty.isVector()) { 1727*0fca6ea1SDimitry Andric report("Second source must be a vector", MI); 1728*0fca6ea1SDimitry Andric break; 1729*0fca6ea1SDimitry Andric } 1730*0fca6ea1SDimitry Andric 1731*0fca6ea1SDimitry Andric if (DstTy != Src0Ty) { 1732*0fca6ea1SDimitry Andric report("Destination type must match the first source vector type", MI); 1733*0fca6ea1SDimitry Andric break; 1734*0fca6ea1SDimitry Andric } 1735*0fca6ea1SDimitry Andric 1736*0fca6ea1SDimitry Andric if (Src0Ty.getElementType() != Src1Ty.getElementType()) { 1737*0fca6ea1SDimitry Andric report("Element type of source vectors must be the same", MI); 1738*0fca6ea1SDimitry Andric break; 1739*0fca6ea1SDimitry Andric } 1740*0fca6ea1SDimitry Andric 1741*0fca6ea1SDimitry Andric if (IndexOp.getImm() != 0 && 1742*0fca6ea1SDimitry Andric Src1Ty.getElementCount().getKnownMinValue() % IndexOp.getImm() != 0) { 1743*0fca6ea1SDimitry Andric report("Index must be a multiple of the second source vector's " 1744*0fca6ea1SDimitry Andric "minimum vector length", 1745*0fca6ea1SDimitry Andric MI); 1746*0fca6ea1SDimitry Andric break; 1747*0fca6ea1SDimitry Andric } 1748*0fca6ea1SDimitry Andric break; 1749*0fca6ea1SDimitry Andric } 1750*0fca6ea1SDimitry Andric case TargetOpcode::G_EXTRACT_SUBVECTOR: { 1751*0fca6ea1SDimitry Andric const MachineOperand &SrcOp = MI->getOperand(1); 1752*0fca6ea1SDimitry Andric if (!SrcOp.isReg()) { 1753*0fca6ea1SDimitry Andric report("G_EXTRACT_SUBVECTOR first source must be a register", MI); 1754*0fca6ea1SDimitry Andric break; 1755*0fca6ea1SDimitry Andric } 1756*0fca6ea1SDimitry Andric 1757*0fca6ea1SDimitry Andric const MachineOperand &IndexOp = MI->getOperand(2); 1758*0fca6ea1SDimitry Andric if (!IndexOp.isImm()) { 1759*0fca6ea1SDimitry Andric report("G_EXTRACT_SUBVECTOR index must be an immediate", MI); 1760*0fca6ea1SDimitry Andric break; 1761*0fca6ea1SDimitry Andric } 1762*0fca6ea1SDimitry Andric 1763*0fca6ea1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1764*0fca6ea1SDimitry Andric LLT SrcTy = MRI->getType(SrcOp.getReg()); 1765*0fca6ea1SDimitry Andric 1766*0fca6ea1SDimitry Andric if (!DstTy.isVector()) { 1767*0fca6ea1SDimitry Andric report("Destination type must be a vector", MI); 1768*0fca6ea1SDimitry Andric break; 1769*0fca6ea1SDimitry Andric } 1770*0fca6ea1SDimitry Andric 1771*0fca6ea1SDimitry Andric if (!SrcTy.isVector()) { 1772*0fca6ea1SDimitry Andric report("First source must be a vector", MI); 1773*0fca6ea1SDimitry Andric break; 1774*0fca6ea1SDimitry Andric } 1775*0fca6ea1SDimitry Andric 1776*0fca6ea1SDimitry Andric if (DstTy.getElementType() != SrcTy.getElementType()) { 1777*0fca6ea1SDimitry Andric report("Element type of vectors must be the same", MI); 1778*0fca6ea1SDimitry Andric break; 1779*0fca6ea1SDimitry Andric } 1780*0fca6ea1SDimitry Andric 1781*0fca6ea1SDimitry Andric if (IndexOp.getImm() != 0 && 1782*0fca6ea1SDimitry Andric SrcTy.getElementCount().getKnownMinValue() % IndexOp.getImm() != 0) { 1783*0fca6ea1SDimitry Andric report("Index must be a multiple of the source vector's minimum vector " 1784*0fca6ea1SDimitry Andric "length", 1785*0fca6ea1SDimitry Andric MI); 1786*0fca6ea1SDimitry Andric break; 1787*0fca6ea1SDimitry Andric } 1788*0fca6ea1SDimitry Andric 1789*0fca6ea1SDimitry Andric break; 1790*0fca6ea1SDimitry Andric } 17918bcb0991SDimitry Andric case TargetOpcode::G_SHUFFLE_VECTOR: { 17928bcb0991SDimitry Andric const MachineOperand &MaskOp = MI->getOperand(3); 17938bcb0991SDimitry Andric if (!MaskOp.isShuffleMask()) { 17948bcb0991SDimitry Andric report("Incorrect mask operand type for G_SHUFFLE_VECTOR", MI); 17958bcb0991SDimitry Andric break; 17968bcb0991SDimitry Andric } 17978bcb0991SDimitry Andric 17988bcb0991SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 17998bcb0991SDimitry Andric LLT Src0Ty = MRI->getType(MI->getOperand(1).getReg()); 18008bcb0991SDimitry Andric LLT Src1Ty = MRI->getType(MI->getOperand(2).getReg()); 18018bcb0991SDimitry Andric 18028bcb0991SDimitry Andric if (Src0Ty != Src1Ty) 18038bcb0991SDimitry Andric report("Source operands must be the same type", MI); 18048bcb0991SDimitry Andric 18058bcb0991SDimitry Andric if (Src0Ty.getScalarType() != DstTy.getScalarType()) 18068bcb0991SDimitry Andric report("G_SHUFFLE_VECTOR cannot change element type", MI); 18078bcb0991SDimitry Andric 18088bcb0991SDimitry Andric // Don't check that all operands are vector because scalars are used in 18098bcb0991SDimitry Andric // place of 1 element vectors. 18108bcb0991SDimitry Andric int SrcNumElts = Src0Ty.isVector() ? Src0Ty.getNumElements() : 1; 18118bcb0991SDimitry Andric int DstNumElts = DstTy.isVector() ? DstTy.getNumElements() : 1; 18128bcb0991SDimitry Andric 1813480093f4SDimitry Andric ArrayRef<int> MaskIdxes = MaskOp.getShuffleMask(); 18148bcb0991SDimitry Andric 18158bcb0991SDimitry Andric if (static_cast<int>(MaskIdxes.size()) != DstNumElts) 18168bcb0991SDimitry Andric report("Wrong result type for shufflemask", MI); 18178bcb0991SDimitry Andric 18188bcb0991SDimitry Andric for (int Idx : MaskIdxes) { 18198bcb0991SDimitry Andric if (Idx < 0) 18208bcb0991SDimitry Andric continue; 18218bcb0991SDimitry Andric 18228bcb0991SDimitry Andric if (Idx >= 2 * SrcNumElts) 18238bcb0991SDimitry Andric report("Out of bounds shuffle index", MI); 18248bcb0991SDimitry Andric } 18258bcb0991SDimitry Andric 18268bcb0991SDimitry Andric break; 18278bcb0991SDimitry Andric } 1828*0fca6ea1SDimitry Andric 1829*0fca6ea1SDimitry Andric case TargetOpcode::G_SPLAT_VECTOR: { 1830*0fca6ea1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1831*0fca6ea1SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 1832*0fca6ea1SDimitry Andric 1833*0fca6ea1SDimitry Andric if (!DstTy.isScalableVector()) { 1834*0fca6ea1SDimitry Andric report("Destination type must be a scalable vector", MI); 1835*0fca6ea1SDimitry Andric break; 1836*0fca6ea1SDimitry Andric } 1837*0fca6ea1SDimitry Andric 1838*0fca6ea1SDimitry Andric if (!SrcTy.isScalar()) { 1839*0fca6ea1SDimitry Andric report("Source type must be a scalar", MI); 1840*0fca6ea1SDimitry Andric break; 1841*0fca6ea1SDimitry Andric } 1842*0fca6ea1SDimitry Andric 1843*0fca6ea1SDimitry Andric if (TypeSize::isKnownGT(DstTy.getElementType().getSizeInBits(), 1844*0fca6ea1SDimitry Andric SrcTy.getSizeInBits())) { 1845*0fca6ea1SDimitry Andric report("Element type of the destination must be the same size or smaller " 1846*0fca6ea1SDimitry Andric "than the source type", 1847*0fca6ea1SDimitry Andric MI); 1848*0fca6ea1SDimitry Andric break; 1849*0fca6ea1SDimitry Andric } 1850*0fca6ea1SDimitry Andric 1851*0fca6ea1SDimitry Andric break; 1852*0fca6ea1SDimitry Andric } 1853*0fca6ea1SDimitry Andric case TargetOpcode::G_EXTRACT_VECTOR_ELT: { 1854*0fca6ea1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1855*0fca6ea1SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 1856*0fca6ea1SDimitry Andric LLT IdxTy = MRI->getType(MI->getOperand(2).getReg()); 1857*0fca6ea1SDimitry Andric 1858*0fca6ea1SDimitry Andric if (!DstTy.isScalar() && !DstTy.isPointer()) { 1859*0fca6ea1SDimitry Andric report("Destination type must be a scalar or pointer", MI); 1860*0fca6ea1SDimitry Andric break; 1861*0fca6ea1SDimitry Andric } 1862*0fca6ea1SDimitry Andric 1863*0fca6ea1SDimitry Andric if (!SrcTy.isVector()) { 1864*0fca6ea1SDimitry Andric report("First source must be a vector", MI); 1865*0fca6ea1SDimitry Andric break; 1866*0fca6ea1SDimitry Andric } 1867*0fca6ea1SDimitry Andric 1868*0fca6ea1SDimitry Andric auto TLI = MF->getSubtarget().getTargetLowering(); 1869*0fca6ea1SDimitry Andric if (IdxTy.getSizeInBits() != 1870*0fca6ea1SDimitry Andric TLI->getVectorIdxTy(MF->getDataLayout()).getFixedSizeInBits()) { 1871*0fca6ea1SDimitry Andric report("Index type must match VectorIdxTy", MI); 1872*0fca6ea1SDimitry Andric break; 1873*0fca6ea1SDimitry Andric } 1874*0fca6ea1SDimitry Andric 1875*0fca6ea1SDimitry Andric break; 1876*0fca6ea1SDimitry Andric } 1877*0fca6ea1SDimitry Andric case TargetOpcode::G_INSERT_VECTOR_ELT: { 1878*0fca6ea1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 1879*0fca6ea1SDimitry Andric LLT VecTy = MRI->getType(MI->getOperand(1).getReg()); 1880*0fca6ea1SDimitry Andric LLT ScaTy = MRI->getType(MI->getOperand(2).getReg()); 1881*0fca6ea1SDimitry Andric LLT IdxTy = MRI->getType(MI->getOperand(3).getReg()); 1882*0fca6ea1SDimitry Andric 1883*0fca6ea1SDimitry Andric if (!DstTy.isVector()) { 1884*0fca6ea1SDimitry Andric report("Destination type must be a vector", MI); 1885*0fca6ea1SDimitry Andric break; 1886*0fca6ea1SDimitry Andric } 1887*0fca6ea1SDimitry Andric 1888*0fca6ea1SDimitry Andric if (VecTy != DstTy) { 1889*0fca6ea1SDimitry Andric report("Destination type and vector type must match", MI); 1890*0fca6ea1SDimitry Andric break; 1891*0fca6ea1SDimitry Andric } 1892*0fca6ea1SDimitry Andric 1893*0fca6ea1SDimitry Andric if (!ScaTy.isScalar() && !ScaTy.isPointer()) { 1894*0fca6ea1SDimitry Andric report("Inserted element must be a scalar or pointer", MI); 1895*0fca6ea1SDimitry Andric break; 1896*0fca6ea1SDimitry Andric } 1897*0fca6ea1SDimitry Andric 1898*0fca6ea1SDimitry Andric auto TLI = MF->getSubtarget().getTargetLowering(); 1899*0fca6ea1SDimitry Andric if (IdxTy.getSizeInBits() != 1900*0fca6ea1SDimitry Andric TLI->getVectorIdxTy(MF->getDataLayout()).getFixedSizeInBits()) { 1901*0fca6ea1SDimitry Andric report("Index type must match VectorIdxTy", MI); 1902*0fca6ea1SDimitry Andric break; 1903*0fca6ea1SDimitry Andric } 1904*0fca6ea1SDimitry Andric 1905*0fca6ea1SDimitry Andric break; 1906*0fca6ea1SDimitry Andric } 19078bcb0991SDimitry Andric case TargetOpcode::G_DYN_STACKALLOC: { 19088bcb0991SDimitry Andric const MachineOperand &DstOp = MI->getOperand(0); 19098bcb0991SDimitry Andric const MachineOperand &AllocOp = MI->getOperand(1); 19108bcb0991SDimitry Andric const MachineOperand &AlignOp = MI->getOperand(2); 19118bcb0991SDimitry Andric 19128bcb0991SDimitry Andric if (!DstOp.isReg() || !MRI->getType(DstOp.getReg()).isPointer()) { 19138bcb0991SDimitry Andric report("dst operand 0 must be a pointer type", MI); 19148bcb0991SDimitry Andric break; 19158bcb0991SDimitry Andric } 19168bcb0991SDimitry Andric 19178bcb0991SDimitry Andric if (!AllocOp.isReg() || !MRI->getType(AllocOp.getReg()).isScalar()) { 19188bcb0991SDimitry Andric report("src operand 1 must be a scalar reg type", MI); 19198bcb0991SDimitry Andric break; 19208bcb0991SDimitry Andric } 19218bcb0991SDimitry Andric 19228bcb0991SDimitry Andric if (!AlignOp.isImm()) { 19238bcb0991SDimitry Andric report("src operand 2 must be an immediate type", MI); 19248bcb0991SDimitry Andric break; 19258bcb0991SDimitry Andric } 19260b57cec5SDimitry Andric break; 19270b57cec5SDimitry Andric } 1928fe6060f1SDimitry Andric case TargetOpcode::G_MEMCPY_INLINE: 1929e8d8bef9SDimitry Andric case TargetOpcode::G_MEMCPY: 1930e8d8bef9SDimitry Andric case TargetOpcode::G_MEMMOVE: { 1931e8d8bef9SDimitry Andric ArrayRef<MachineMemOperand *> MMOs = MI->memoperands(); 1932e8d8bef9SDimitry Andric if (MMOs.size() != 2) { 1933e8d8bef9SDimitry Andric report("memcpy/memmove must have 2 memory operands", MI); 1934e8d8bef9SDimitry Andric break; 1935e8d8bef9SDimitry Andric } 1936e8d8bef9SDimitry Andric 1937e8d8bef9SDimitry Andric if ((!MMOs[0]->isStore() || MMOs[0]->isLoad()) || 1938e8d8bef9SDimitry Andric (MMOs[1]->isStore() || !MMOs[1]->isLoad())) { 1939e8d8bef9SDimitry Andric report("wrong memory operand types", MI); 1940e8d8bef9SDimitry Andric break; 1941e8d8bef9SDimitry Andric } 1942e8d8bef9SDimitry Andric 1943e8d8bef9SDimitry Andric if (MMOs[0]->getSize() != MMOs[1]->getSize()) 1944e8d8bef9SDimitry Andric report("inconsistent memory operand sizes", MI); 1945e8d8bef9SDimitry Andric 1946e8d8bef9SDimitry Andric LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg()); 1947e8d8bef9SDimitry Andric LLT SrcPtrTy = MRI->getType(MI->getOperand(1).getReg()); 1948e8d8bef9SDimitry Andric 1949e8d8bef9SDimitry Andric if (!DstPtrTy.isPointer() || !SrcPtrTy.isPointer()) { 1950e8d8bef9SDimitry Andric report("memory instruction operand must be a pointer", MI); 1951e8d8bef9SDimitry Andric break; 1952e8d8bef9SDimitry Andric } 1953e8d8bef9SDimitry Andric 1954e8d8bef9SDimitry Andric if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace()) 1955e8d8bef9SDimitry Andric report("inconsistent store address space", MI); 1956e8d8bef9SDimitry Andric if (SrcPtrTy.getAddressSpace() != MMOs[1]->getAddrSpace()) 1957e8d8bef9SDimitry Andric report("inconsistent load address space", MI); 1958e8d8bef9SDimitry Andric 1959fe6060f1SDimitry Andric if (Opc != TargetOpcode::G_MEMCPY_INLINE) 1960fe6060f1SDimitry Andric if (!MI->getOperand(3).isImm() || (MI->getOperand(3).getImm() & ~1LL)) 1961fe6060f1SDimitry Andric report("'tail' flag (operand 3) must be an immediate 0 or 1", MI); 1962fe6060f1SDimitry Andric 1963e8d8bef9SDimitry Andric break; 1964e8d8bef9SDimitry Andric } 1965fe6060f1SDimitry Andric case TargetOpcode::G_BZERO: 1966e8d8bef9SDimitry Andric case TargetOpcode::G_MEMSET: { 1967e8d8bef9SDimitry Andric ArrayRef<MachineMemOperand *> MMOs = MI->memoperands(); 1968fe6060f1SDimitry Andric std::string Name = Opc == TargetOpcode::G_MEMSET ? "memset" : "bzero"; 1969e8d8bef9SDimitry Andric if (MMOs.size() != 1) { 1970fe6060f1SDimitry Andric report(Twine(Name, " must have 1 memory operand"), MI); 1971e8d8bef9SDimitry Andric break; 1972e8d8bef9SDimitry Andric } 1973e8d8bef9SDimitry Andric 1974e8d8bef9SDimitry Andric if ((!MMOs[0]->isStore() || MMOs[0]->isLoad())) { 1975fe6060f1SDimitry Andric report(Twine(Name, " memory operand must be a store"), MI); 1976e8d8bef9SDimitry Andric break; 1977e8d8bef9SDimitry Andric } 1978e8d8bef9SDimitry Andric 1979e8d8bef9SDimitry Andric LLT DstPtrTy = MRI->getType(MI->getOperand(0).getReg()); 1980e8d8bef9SDimitry Andric if (!DstPtrTy.isPointer()) { 1981fe6060f1SDimitry Andric report(Twine(Name, " operand must be a pointer"), MI); 1982e8d8bef9SDimitry Andric break; 1983e8d8bef9SDimitry Andric } 1984e8d8bef9SDimitry Andric 1985e8d8bef9SDimitry Andric if (DstPtrTy.getAddressSpace() != MMOs[0]->getAddrSpace()) 1986fe6060f1SDimitry Andric report("inconsistent " + Twine(Name, " address space"), MI); 1987fe6060f1SDimitry Andric 1988fe6060f1SDimitry Andric if (!MI->getOperand(MI->getNumOperands() - 1).isImm() || 1989fe6060f1SDimitry Andric (MI->getOperand(MI->getNumOperands() - 1).getImm() & ~1LL)) 1990fe6060f1SDimitry Andric report("'tail' flag (last operand) must be an immediate 0 or 1", MI); 1991e8d8bef9SDimitry Andric 1992e8d8bef9SDimitry Andric break; 1993e8d8bef9SDimitry Andric } 1994*0fca6ea1SDimitry Andric case TargetOpcode::G_UBSANTRAP: { 1995*0fca6ea1SDimitry Andric const MachineOperand &KindOp = MI->getOperand(0); 1996*0fca6ea1SDimitry Andric if (!MI->getOperand(0).isImm()) { 1997*0fca6ea1SDimitry Andric report("Crash kind must be an immediate", &KindOp, 0); 1998*0fca6ea1SDimitry Andric break; 1999*0fca6ea1SDimitry Andric } 2000*0fca6ea1SDimitry Andric int64_t Kind = MI->getOperand(0).getImm(); 2001*0fca6ea1SDimitry Andric if (!isInt<8>(Kind)) 2002*0fca6ea1SDimitry Andric report("Crash kind must be 8 bit wide", &KindOp, 0); 2003*0fca6ea1SDimitry Andric break; 2004*0fca6ea1SDimitry Andric } 2005e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_SEQ_FADD: 2006e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_SEQ_FMUL: { 2007e8d8bef9SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 2008e8d8bef9SDimitry Andric LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg()); 2009e8d8bef9SDimitry Andric LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg()); 2010e8d8bef9SDimitry Andric if (!DstTy.isScalar()) 2011e8d8bef9SDimitry Andric report("Vector reduction requires a scalar destination type", MI); 2012e8d8bef9SDimitry Andric if (!Src1Ty.isScalar()) 2013e8d8bef9SDimitry Andric report("Sequential FADD/FMUL vector reduction requires a scalar 1st operand", MI); 2014e8d8bef9SDimitry Andric if (!Src2Ty.isVector()) 2015e8d8bef9SDimitry Andric report("Sequential FADD/FMUL vector reduction must have a vector 2nd operand", MI); 2016e8d8bef9SDimitry Andric break; 2017e8d8bef9SDimitry Andric } 2018e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_FADD: 2019e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_FMUL: 2020e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_FMAX: 2021e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_FMIN: 20225f757f3fSDimitry Andric case TargetOpcode::G_VECREDUCE_FMAXIMUM: 20235f757f3fSDimitry Andric case TargetOpcode::G_VECREDUCE_FMINIMUM: 2024e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_ADD: 2025e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_MUL: 2026e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_AND: 2027e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_OR: 2028e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_XOR: 2029e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_SMAX: 2030e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_SMIN: 2031e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_UMAX: 2032e8d8bef9SDimitry Andric case TargetOpcode::G_VECREDUCE_UMIN: { 2033e8d8bef9SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 2034e8d8bef9SDimitry Andric if (!DstTy.isScalar()) 2035e8d8bef9SDimitry Andric report("Vector reduction requires a scalar destination type", MI); 2036e8d8bef9SDimitry Andric break; 2037e8d8bef9SDimitry Andric } 2038fe6060f1SDimitry Andric 2039fe6060f1SDimitry Andric case TargetOpcode::G_SBFX: 2040fe6060f1SDimitry Andric case TargetOpcode::G_UBFX: { 2041fe6060f1SDimitry Andric LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); 2042fe6060f1SDimitry Andric if (DstTy.isVector()) { 2043fe6060f1SDimitry Andric report("Bitfield extraction is not supported on vectors", MI); 2044fe6060f1SDimitry Andric break; 2045fe6060f1SDimitry Andric } 2046fe6060f1SDimitry Andric break; 2047fe6060f1SDimitry Andric } 20480eae32dcSDimitry Andric case TargetOpcode::G_SHL: 20490eae32dcSDimitry Andric case TargetOpcode::G_LSHR: 20500eae32dcSDimitry Andric case TargetOpcode::G_ASHR: 2051fe6060f1SDimitry Andric case TargetOpcode::G_ROTR: 2052fe6060f1SDimitry Andric case TargetOpcode::G_ROTL: { 2053fe6060f1SDimitry Andric LLT Src1Ty = MRI->getType(MI->getOperand(1).getReg()); 2054fe6060f1SDimitry Andric LLT Src2Ty = MRI->getType(MI->getOperand(2).getReg()); 2055fe6060f1SDimitry Andric if (Src1Ty.isVector() != Src2Ty.isVector()) { 20560eae32dcSDimitry Andric report("Shifts and rotates require operands to be either all scalars or " 20570eae32dcSDimitry Andric "all vectors", 2058fe6060f1SDimitry Andric MI); 2059fe6060f1SDimitry Andric break; 2060fe6060f1SDimitry Andric } 2061fe6060f1SDimitry Andric break; 2062fe6060f1SDimitry Andric } 2063349cc55cSDimitry Andric case TargetOpcode::G_LLROUND: 2064349cc55cSDimitry Andric case TargetOpcode::G_LROUND: { 2065349cc55cSDimitry Andric verifyAllRegOpsScalar(*MI, *MRI); 2066349cc55cSDimitry Andric break; 2067349cc55cSDimitry Andric } 206881ad6265SDimitry Andric case TargetOpcode::G_IS_FPCLASS: { 206981ad6265SDimitry Andric LLT DestTy = MRI->getType(MI->getOperand(0).getReg()); 207081ad6265SDimitry Andric LLT DestEltTy = DestTy.getScalarType(); 207181ad6265SDimitry Andric if (!DestEltTy.isScalar()) { 207281ad6265SDimitry Andric report("Destination must be a scalar or vector of scalars", MI); 207381ad6265SDimitry Andric break; 207481ad6265SDimitry Andric } 207581ad6265SDimitry Andric LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); 207681ad6265SDimitry Andric LLT SrcEltTy = SrcTy.getScalarType(); 207781ad6265SDimitry Andric if (!SrcEltTy.isScalar()) { 207881ad6265SDimitry Andric report("Source must be a scalar or vector of scalars", MI); 207981ad6265SDimitry Andric break; 208081ad6265SDimitry Andric } 208181ad6265SDimitry Andric if (!verifyVectorElementMatch(DestTy, SrcTy, MI)) 208281ad6265SDimitry Andric break; 208381ad6265SDimitry Andric const MachineOperand &TestMO = MI->getOperand(2); 208481ad6265SDimitry Andric if (!TestMO.isImm()) { 208581ad6265SDimitry Andric report("floating-point class set (operand 2) must be an immediate", MI); 208681ad6265SDimitry Andric break; 208781ad6265SDimitry Andric } 208881ad6265SDimitry Andric int64_t Test = TestMO.getImm(); 208981ad6265SDimitry Andric if (Test < 0 || Test > fcAllFlags) { 209081ad6265SDimitry Andric report("Incorrect floating-point class set (operand 2)", MI); 209181ad6265SDimitry Andric break; 209281ad6265SDimitry Andric } 209381ad6265SDimitry Andric break; 209481ad6265SDimitry Andric } 20955f757f3fSDimitry Andric case TargetOpcode::G_PREFETCH: { 20965f757f3fSDimitry Andric const MachineOperand &AddrOp = MI->getOperand(0); 20975f757f3fSDimitry Andric if (!AddrOp.isReg() || !MRI->getType(AddrOp.getReg()).isPointer()) { 20985f757f3fSDimitry Andric report("addr operand must be a pointer", &AddrOp, 0); 20995f757f3fSDimitry Andric break; 21005f757f3fSDimitry Andric } 21015f757f3fSDimitry Andric const MachineOperand &RWOp = MI->getOperand(1); 21025f757f3fSDimitry Andric if (!RWOp.isImm() || (uint64_t)RWOp.getImm() >= 2) { 21035f757f3fSDimitry Andric report("rw operand must be an immediate 0-1", &RWOp, 1); 21045f757f3fSDimitry Andric break; 21055f757f3fSDimitry Andric } 21065f757f3fSDimitry Andric const MachineOperand &LocalityOp = MI->getOperand(2); 21075f757f3fSDimitry Andric if (!LocalityOp.isImm() || (uint64_t)LocalityOp.getImm() >= 4) { 21085f757f3fSDimitry Andric report("locality operand must be an immediate 0-3", &LocalityOp, 2); 21095f757f3fSDimitry Andric break; 21105f757f3fSDimitry Andric } 21115f757f3fSDimitry Andric const MachineOperand &CacheTypeOp = MI->getOperand(3); 21125f757f3fSDimitry Andric if (!CacheTypeOp.isImm() || (uint64_t)CacheTypeOp.getImm() >= 2) { 21135f757f3fSDimitry Andric report("cache type operand must be an immediate 0-1", &CacheTypeOp, 3); 21145f757f3fSDimitry Andric break; 21155f757f3fSDimitry Andric } 21165f757f3fSDimitry Andric break; 21175f757f3fSDimitry Andric } 2118bdd1243dSDimitry Andric case TargetOpcode::G_ASSERT_ALIGN: { 2119bdd1243dSDimitry Andric if (MI->getOperand(2).getImm() < 1) 2120bdd1243dSDimitry Andric report("alignment immediate must be >= 1", MI); 212181ad6265SDimitry Andric break; 212281ad6265SDimitry Andric } 212306c3fb27SDimitry Andric case TargetOpcode::G_CONSTANT_POOL: { 212406c3fb27SDimitry Andric if (!MI->getOperand(1).isCPI()) 212506c3fb27SDimitry Andric report("Src operand 1 must be a constant pool index", MI); 212606c3fb27SDimitry Andric if (!MRI->getType(MI->getOperand(0).getReg()).isPointer()) 212706c3fb27SDimitry Andric report("Dst operand 0 must be a pointer", MI); 212806c3fb27SDimitry Andric break; 212906c3fb27SDimitry Andric } 2130*0fca6ea1SDimitry Andric case TargetOpcode::G_PTRAUTH_GLOBAL_VALUE: { 2131*0fca6ea1SDimitry Andric const MachineOperand &AddrOp = MI->getOperand(1); 2132*0fca6ea1SDimitry Andric if (!AddrOp.isReg() || !MRI->getType(AddrOp.getReg()).isPointer()) 2133*0fca6ea1SDimitry Andric report("addr operand must be a pointer", &AddrOp, 1); 2134*0fca6ea1SDimitry Andric break; 2135*0fca6ea1SDimitry Andric } 21360b57cec5SDimitry Andric default: 21370b57cec5SDimitry Andric break; 21380b57cec5SDimitry Andric } 21390b57cec5SDimitry Andric } 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) { 21420b57cec5SDimitry Andric const MCInstrDesc &MCID = MI->getDesc(); 21430b57cec5SDimitry Andric if (MI->getNumOperands() < MCID.getNumOperands()) { 21440b57cec5SDimitry Andric report("Too few operands", MI); 21450b57cec5SDimitry Andric errs() << MCID.getNumOperands() << " operands expected, but " 21460b57cec5SDimitry Andric << MI->getNumOperands() << " given.\n"; 21470b57cec5SDimitry Andric } 21480b57cec5SDimitry Andric 21495f757f3fSDimitry Andric if (MI->getFlag(MachineInstr::NoConvergent) && !MCID.isConvergent()) 21505f757f3fSDimitry Andric report("NoConvergent flag expected only on convergent instructions.", MI); 21515f757f3fSDimitry Andric 21520b57cec5SDimitry Andric if (MI->isPHI()) { 21530b57cec5SDimitry Andric if (MF->getProperties().hasProperty( 21540b57cec5SDimitry Andric MachineFunctionProperties::Property::NoPHIs)) 21550b57cec5SDimitry Andric report("Found PHI instruction with NoPHIs property set", MI); 21560b57cec5SDimitry Andric 21570b57cec5SDimitry Andric if (FirstNonPHI) 21580b57cec5SDimitry Andric report("Found PHI instruction after non-PHI", MI); 21590b57cec5SDimitry Andric } else if (FirstNonPHI == nullptr) 21600b57cec5SDimitry Andric FirstNonPHI = MI; 21610b57cec5SDimitry Andric 21620b57cec5SDimitry Andric // Check the tied operands. 21630b57cec5SDimitry Andric if (MI->isInlineAsm()) 21640b57cec5SDimitry Andric verifyInlineAsm(MI); 21650b57cec5SDimitry Andric 2166e8d8bef9SDimitry Andric // Check that unspillable terminators define a reg and have at most one use. 2167e8d8bef9SDimitry Andric if (TII->isUnspillableTerminator(MI)) { 2168e8d8bef9SDimitry Andric if (!MI->getOperand(0).isReg() || !MI->getOperand(0).isDef()) 2169e8d8bef9SDimitry Andric report("Unspillable Terminator does not define a reg", MI); 2170e8d8bef9SDimitry Andric Register Def = MI->getOperand(0).getReg(); 2171e8d8bef9SDimitry Andric if (Def.isVirtual() && 2172349cc55cSDimitry Andric !MF->getProperties().hasProperty( 2173349cc55cSDimitry Andric MachineFunctionProperties::Property::NoPHIs) && 2174e8d8bef9SDimitry Andric std::distance(MRI->use_nodbg_begin(Def), MRI->use_nodbg_end()) > 1) 2175e8d8bef9SDimitry Andric report("Unspillable Terminator expected to have at most one use!", MI); 2176e8d8bef9SDimitry Andric } 2177e8d8bef9SDimitry Andric 21785ffd83dbSDimitry Andric // A fully-formed DBG_VALUE must have a location. Ignore partially formed 21795ffd83dbSDimitry Andric // DBG_VALUEs: these are convenient to use in tests, but should never get 21805ffd83dbSDimitry Andric // generated. 21815ffd83dbSDimitry Andric if (MI->isDebugValue() && MI->getNumOperands() == 4) 21825ffd83dbSDimitry Andric if (!MI->getDebugLoc()) 21835ffd83dbSDimitry Andric report("Missing DebugLoc for debug instruction", MI); 21845ffd83dbSDimitry Andric 2185e8d8bef9SDimitry Andric // Meta instructions should never be the subject of debug value tracking, 2186e8d8bef9SDimitry Andric // they don't create a value in the output program at all. 2187e8d8bef9SDimitry Andric if (MI->isMetaInstruction() && MI->peekDebugInstrNum()) 2188e8d8bef9SDimitry Andric report("Metadata instruction should not have a value tracking number", MI); 2189e8d8bef9SDimitry Andric 21900b57cec5SDimitry Andric // Check the MachineMemOperands for basic consistency. 21915ffd83dbSDimitry Andric for (MachineMemOperand *Op : MI->memoperands()) { 21925ffd83dbSDimitry Andric if (Op->isLoad() && !MI->mayLoad()) 21930b57cec5SDimitry Andric report("Missing mayLoad flag", MI); 21945ffd83dbSDimitry Andric if (Op->isStore() && !MI->mayStore()) 21950b57cec5SDimitry Andric report("Missing mayStore flag", MI); 21960b57cec5SDimitry Andric } 21970b57cec5SDimitry Andric 21980b57cec5SDimitry Andric // Debug values must not have a slot index. 21990b57cec5SDimitry Andric // Other instructions must have one, unless they are inside a bundle. 22000b57cec5SDimitry Andric if (LiveInts) { 22010b57cec5SDimitry Andric bool mapped = !LiveInts->isNotInMIMap(*MI); 2202fe6060f1SDimitry Andric if (MI->isDebugOrPseudoInstr()) { 22030b57cec5SDimitry Andric if (mapped) 22040b57cec5SDimitry Andric report("Debug instruction has a slot index", MI); 22050b57cec5SDimitry Andric } else if (MI->isInsideBundle()) { 22060b57cec5SDimitry Andric if (mapped) 22070b57cec5SDimitry Andric report("Instruction inside bundle has a slot index", MI); 22080b57cec5SDimitry Andric } else { 22090b57cec5SDimitry Andric if (!mapped) 22100b57cec5SDimitry Andric report("Missing slot index", MI); 22110b57cec5SDimitry Andric } 22120b57cec5SDimitry Andric } 22130b57cec5SDimitry Andric 2214fe6060f1SDimitry Andric unsigned Opc = MCID.getOpcode(); 2215fe6060f1SDimitry Andric if (isPreISelGenericOpcode(Opc) || isPreISelGenericOptimizationHint(Opc)) { 22160b57cec5SDimitry Andric verifyPreISelGenericInstruction(MI); 22170b57cec5SDimitry Andric return; 22180b57cec5SDimitry Andric } 22190b57cec5SDimitry Andric 22200b57cec5SDimitry Andric StringRef ErrorInfo; 22210b57cec5SDimitry Andric if (!TII->verifyInstruction(*MI, ErrorInfo)) 22220b57cec5SDimitry Andric report(ErrorInfo.data(), MI); 22230b57cec5SDimitry Andric 22240b57cec5SDimitry Andric // Verify properties of various specific instruction types 22250b57cec5SDimitry Andric switch (MI->getOpcode()) { 22260b57cec5SDimitry Andric case TargetOpcode::COPY: { 22270b57cec5SDimitry Andric const MachineOperand &DstOp = MI->getOperand(0); 22280b57cec5SDimitry Andric const MachineOperand &SrcOp = MI->getOperand(1); 2229fe6060f1SDimitry Andric const Register SrcReg = SrcOp.getReg(); 2230fe6060f1SDimitry Andric const Register DstReg = DstOp.getReg(); 2231fe6060f1SDimitry Andric 2232fe6060f1SDimitry Andric LLT DstTy = MRI->getType(DstReg); 2233fe6060f1SDimitry Andric LLT SrcTy = MRI->getType(SrcReg); 22340b57cec5SDimitry Andric if (SrcTy.isValid() && DstTy.isValid()) { 22350b57cec5SDimitry Andric // If both types are valid, check that the types are the same. 22360b57cec5SDimitry Andric if (SrcTy != DstTy) { 22370b57cec5SDimitry Andric report("Copy Instruction is illegal with mismatching types", MI); 22380b57cec5SDimitry Andric errs() << "Def = " << DstTy << ", Src = " << SrcTy << "\n"; 22390b57cec5SDimitry Andric } 2240fe6060f1SDimitry Andric 2241fe6060f1SDimitry Andric break; 22420b57cec5SDimitry Andric } 2243fe6060f1SDimitry Andric 2244fe6060f1SDimitry Andric if (!SrcTy.isValid() && !DstTy.isValid()) 2245fe6060f1SDimitry Andric break; 2246fe6060f1SDimitry Andric 2247fe6060f1SDimitry Andric // If we have only one valid type, this is likely a copy between a virtual 2248fe6060f1SDimitry Andric // and physical register. 22495f757f3fSDimitry Andric TypeSize SrcSize = TRI->getRegSizeInBits(SrcReg, *MRI); 22505f757f3fSDimitry Andric TypeSize DstSize = TRI->getRegSizeInBits(DstReg, *MRI); 2251fe6060f1SDimitry Andric if (SrcReg.isPhysical() && DstTy.isValid()) { 2252fe6060f1SDimitry Andric const TargetRegisterClass *SrcRC = 2253fe6060f1SDimitry Andric TRI->getMinimalPhysRegClassLLT(SrcReg, DstTy); 2254fe6060f1SDimitry Andric if (SrcRC) 2255fe6060f1SDimitry Andric SrcSize = TRI->getRegSizeInBits(*SrcRC); 2256fe6060f1SDimitry Andric } 2257fe6060f1SDimitry Andric 2258fe6060f1SDimitry Andric if (DstReg.isPhysical() && SrcTy.isValid()) { 2259fe6060f1SDimitry Andric const TargetRegisterClass *DstRC = 2260fe6060f1SDimitry Andric TRI->getMinimalPhysRegClassLLT(DstReg, SrcTy); 2261fe6060f1SDimitry Andric if (DstRC) 2262fe6060f1SDimitry Andric DstSize = TRI->getRegSizeInBits(*DstRC); 2263fe6060f1SDimitry Andric } 2264fe6060f1SDimitry Andric 22655f757f3fSDimitry Andric // The next two checks allow COPY between physical and virtual registers, 22665f757f3fSDimitry Andric // when the virtual register has a scalable size and the physical register 22675f757f3fSDimitry Andric // has a fixed size. These checks allow COPY between *potentialy* mismatched 22685f757f3fSDimitry Andric // sizes. However, once RegisterBankSelection occurs, MachineVerifier should 22695f757f3fSDimitry Andric // be able to resolve a fixed size for the scalable vector, and at that 22705f757f3fSDimitry Andric // point this function will know for sure whether the sizes are mismatched 22715f757f3fSDimitry Andric // and correctly report a size mismatch. 22725f757f3fSDimitry Andric if (SrcReg.isPhysical() && DstReg.isVirtual() && DstSize.isScalable() && 22735f757f3fSDimitry Andric !SrcSize.isScalable()) 22745f757f3fSDimitry Andric break; 22755f757f3fSDimitry Andric if (SrcReg.isVirtual() && DstReg.isPhysical() && SrcSize.isScalable() && 22765f757f3fSDimitry Andric !DstSize.isScalable()) 22775f757f3fSDimitry Andric break; 2278fe6060f1SDimitry Andric 22795f757f3fSDimitry Andric if (SrcSize.isNonZero() && DstSize.isNonZero() && SrcSize != DstSize) { 22800b57cec5SDimitry Andric if (!DstOp.getSubReg() && !SrcOp.getSubReg()) { 22810b57cec5SDimitry Andric report("Copy Instruction is illegal with mismatching sizes", MI); 22820b57cec5SDimitry Andric errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize 22830b57cec5SDimitry Andric << "\n"; 22840b57cec5SDimitry Andric } 22850b57cec5SDimitry Andric } 22860b57cec5SDimitry Andric break; 22870b57cec5SDimitry Andric } 22885ffd83dbSDimitry Andric case TargetOpcode::STATEPOINT: { 22895ffd83dbSDimitry Andric StatepointOpers SO(MI); 22905ffd83dbSDimitry Andric if (!MI->getOperand(SO.getIDPos()).isImm() || 22915ffd83dbSDimitry Andric !MI->getOperand(SO.getNBytesPos()).isImm() || 22925ffd83dbSDimitry Andric !MI->getOperand(SO.getNCallArgsPos()).isImm()) { 22930b57cec5SDimitry Andric report("meta operands to STATEPOINT not constant!", MI); 22940b57cec5SDimitry Andric break; 22955ffd83dbSDimitry Andric } 22960b57cec5SDimitry Andric 22970b57cec5SDimitry Andric auto VerifyStackMapConstant = [&](unsigned Offset) { 2298e8d8bef9SDimitry Andric if (Offset >= MI->getNumOperands()) { 2299e8d8bef9SDimitry Andric report("stack map constant to STATEPOINT is out of range!", MI); 2300e8d8bef9SDimitry Andric return; 2301e8d8bef9SDimitry Andric } 23025ffd83dbSDimitry Andric if (!MI->getOperand(Offset - 1).isImm() || 23035ffd83dbSDimitry Andric MI->getOperand(Offset - 1).getImm() != StackMaps::ConstantOp || 23045ffd83dbSDimitry Andric !MI->getOperand(Offset).isImm()) 23050b57cec5SDimitry Andric report("stack map constant to STATEPOINT not well formed!", MI); 23060b57cec5SDimitry Andric }; 23075ffd83dbSDimitry Andric VerifyStackMapConstant(SO.getCCIdx()); 23085ffd83dbSDimitry Andric VerifyStackMapConstant(SO.getFlagsIdx()); 23095ffd83dbSDimitry Andric VerifyStackMapConstant(SO.getNumDeoptArgsIdx()); 2310e8d8bef9SDimitry Andric VerifyStackMapConstant(SO.getNumGCPtrIdx()); 2311e8d8bef9SDimitry Andric VerifyStackMapConstant(SO.getNumAllocaIdx()); 2312e8d8bef9SDimitry Andric VerifyStackMapConstant(SO.getNumGcMapEntriesIdx()); 2313e8d8bef9SDimitry Andric 2314e8d8bef9SDimitry Andric // Verify that all explicit statepoint defs are tied to gc operands as 2315e8d8bef9SDimitry Andric // they are expected to be a relocation of gc operands. 2316e8d8bef9SDimitry Andric unsigned FirstGCPtrIdx = SO.getFirstGCPtrIdx(); 2317e8d8bef9SDimitry Andric unsigned LastGCPtrIdx = SO.getNumAllocaIdx() - 2; 2318e8d8bef9SDimitry Andric for (unsigned Idx = 0; Idx < MI->getNumDefs(); Idx++) { 2319e8d8bef9SDimitry Andric unsigned UseOpIdx; 2320e8d8bef9SDimitry Andric if (!MI->isRegTiedToUseOperand(Idx, &UseOpIdx)) { 2321e8d8bef9SDimitry Andric report("STATEPOINT defs expected to be tied", MI); 2322e8d8bef9SDimitry Andric break; 2323e8d8bef9SDimitry Andric } 2324e8d8bef9SDimitry Andric if (UseOpIdx < FirstGCPtrIdx || UseOpIdx > LastGCPtrIdx) { 2325e8d8bef9SDimitry Andric report("STATEPOINT def tied to non-gc operand", MI); 2326e8d8bef9SDimitry Andric break; 2327e8d8bef9SDimitry Andric } 2328e8d8bef9SDimitry Andric } 23290b57cec5SDimitry Andric 23300b57cec5SDimitry Andric // TODO: verify we have properly encoded deopt arguments 23315ffd83dbSDimitry Andric } break; 2332fe6060f1SDimitry Andric case TargetOpcode::INSERT_SUBREG: { 2333fe6060f1SDimitry Andric unsigned InsertedSize; 2334fe6060f1SDimitry Andric if (unsigned SubIdx = MI->getOperand(2).getSubReg()) 2335fe6060f1SDimitry Andric InsertedSize = TRI->getSubRegIdxSize(SubIdx); 2336fe6060f1SDimitry Andric else 2337fe6060f1SDimitry Andric InsertedSize = TRI->getRegSizeInBits(MI->getOperand(2).getReg(), *MRI); 2338fe6060f1SDimitry Andric unsigned SubRegSize = TRI->getSubRegIdxSize(MI->getOperand(3).getImm()); 2339fe6060f1SDimitry Andric if (SubRegSize < InsertedSize) { 2340fe6060f1SDimitry Andric report("INSERT_SUBREG expected inserted value to have equal or lesser " 2341fe6060f1SDimitry Andric "size than the subreg it was inserted into", MI); 2342fe6060f1SDimitry Andric break; 2343fe6060f1SDimitry Andric } 2344fe6060f1SDimitry Andric } break; 2345bdd1243dSDimitry Andric case TargetOpcode::REG_SEQUENCE: { 2346bdd1243dSDimitry Andric unsigned NumOps = MI->getNumOperands(); 2347bdd1243dSDimitry Andric if (!(NumOps & 1)) { 2348bdd1243dSDimitry Andric report("Invalid number of operands for REG_SEQUENCE", MI); 2349bdd1243dSDimitry Andric break; 2350bdd1243dSDimitry Andric } 2351bdd1243dSDimitry Andric 2352bdd1243dSDimitry Andric for (unsigned I = 1; I != NumOps; I += 2) { 2353bdd1243dSDimitry Andric const MachineOperand &RegOp = MI->getOperand(I); 2354bdd1243dSDimitry Andric const MachineOperand &SubRegOp = MI->getOperand(I + 1); 2355bdd1243dSDimitry Andric 2356bdd1243dSDimitry Andric if (!RegOp.isReg()) 2357bdd1243dSDimitry Andric report("Invalid register operand for REG_SEQUENCE", &RegOp, I); 2358bdd1243dSDimitry Andric 2359bdd1243dSDimitry Andric if (!SubRegOp.isImm() || SubRegOp.getImm() == 0 || 2360bdd1243dSDimitry Andric SubRegOp.getImm() >= TRI->getNumSubRegIndices()) { 2361bdd1243dSDimitry Andric report("Invalid subregister index operand for REG_SEQUENCE", 2362bdd1243dSDimitry Andric &SubRegOp, I + 1); 2363bdd1243dSDimitry Andric } 2364bdd1243dSDimitry Andric } 2365bdd1243dSDimitry Andric 2366bdd1243dSDimitry Andric Register DstReg = MI->getOperand(0).getReg(); 2367bdd1243dSDimitry Andric if (DstReg.isPhysical()) 2368bdd1243dSDimitry Andric report("REG_SEQUENCE does not support physical register results", MI); 2369bdd1243dSDimitry Andric 2370bdd1243dSDimitry Andric if (MI->getOperand(0).getSubReg()) 2371bdd1243dSDimitry Andric report("Invalid subreg result for REG_SEQUENCE", MI); 2372bdd1243dSDimitry Andric 2373bdd1243dSDimitry Andric break; 2374bdd1243dSDimitry Andric } 23750b57cec5SDimitry Andric } 23760b57cec5SDimitry Andric } 23770b57cec5SDimitry Andric 23780b57cec5SDimitry Andric void 23790b57cec5SDimitry Andric MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) { 23800b57cec5SDimitry Andric const MachineInstr *MI = MO->getParent(); 23810b57cec5SDimitry Andric const MCInstrDesc &MCID = MI->getDesc(); 23820b57cec5SDimitry Andric unsigned NumDefs = MCID.getNumDefs(); 23830b57cec5SDimitry Andric if (MCID.getOpcode() == TargetOpcode::PATCHPOINT) 23840b57cec5SDimitry Andric NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0; 23850b57cec5SDimitry Andric 23860b57cec5SDimitry Andric // The first MCID.NumDefs operands must be explicit register defines 23870b57cec5SDimitry Andric if (MONum < NumDefs) { 2388bdd1243dSDimitry Andric const MCOperandInfo &MCOI = MCID.operands()[MONum]; 23890b57cec5SDimitry Andric if (!MO->isReg()) 23900b57cec5SDimitry Andric report("Explicit definition must be a register", MO, MONum); 23910b57cec5SDimitry Andric else if (!MO->isDef() && !MCOI.isOptionalDef()) 23920b57cec5SDimitry Andric report("Explicit definition marked as use", MO, MONum); 23930b57cec5SDimitry Andric else if (MO->isImplicit()) 23940b57cec5SDimitry Andric report("Explicit definition marked as implicit", MO, MONum); 23950b57cec5SDimitry Andric } else if (MONum < MCID.getNumOperands()) { 2396bdd1243dSDimitry Andric const MCOperandInfo &MCOI = MCID.operands()[MONum]; 23970b57cec5SDimitry Andric // Don't check if it's the last operand in a variadic instruction. See, 2398480093f4SDimitry Andric // e.g., LDM_RET in the arm back end. Check non-variadic operands only. 2399480093f4SDimitry Andric bool IsOptional = MI->isVariadic() && MONum == MCID.getNumOperands() - 1; 2400480093f4SDimitry Andric if (!IsOptional) { 2401480093f4SDimitry Andric if (MO->isReg()) { 24025ffd83dbSDimitry Andric if (MO->isDef() && !MCOI.isOptionalDef() && !MCID.variadicOpsAreDefs()) 24030b57cec5SDimitry Andric report("Explicit operand marked as def", MO, MONum); 24040b57cec5SDimitry Andric if (MO->isImplicit()) 24050b57cec5SDimitry Andric report("Explicit operand marked as implicit", MO, MONum); 24060b57cec5SDimitry Andric } 24070b57cec5SDimitry Andric 2408480093f4SDimitry Andric // Check that an instruction has register operands only as expected. 2409480093f4SDimitry Andric if (MCOI.OperandType == MCOI::OPERAND_REGISTER && 2410480093f4SDimitry Andric !MO->isReg() && !MO->isFI()) 2411480093f4SDimitry Andric report("Expected a register operand.", MO, MONum); 2412fe6060f1SDimitry Andric if (MO->isReg()) { 2413fe6060f1SDimitry Andric if (MCOI.OperandType == MCOI::OPERAND_IMMEDIATE || 2414fe6060f1SDimitry Andric (MCOI.OperandType == MCOI::OPERAND_PCREL && 2415fe6060f1SDimitry Andric !TII->isPCRelRegisterOperandLegal(*MO))) 2416480093f4SDimitry Andric report("Expected a non-register operand.", MO, MONum); 2417480093f4SDimitry Andric } 2418fe6060f1SDimitry Andric } 2419480093f4SDimitry Andric 24200b57cec5SDimitry Andric int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO); 24210b57cec5SDimitry Andric if (TiedTo != -1) { 24220b57cec5SDimitry Andric if (!MO->isReg()) 24230b57cec5SDimitry Andric report("Tied use must be a register", MO, MONum); 24240b57cec5SDimitry Andric else if (!MO->isTied()) 24250b57cec5SDimitry Andric report("Operand should be tied", MO, MONum); 24260b57cec5SDimitry Andric else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum)) 24270b57cec5SDimitry Andric report("Tied def doesn't match MCInstrDesc", MO, MONum); 2428bdd1243dSDimitry Andric else if (MO->getReg().isPhysical()) { 24290b57cec5SDimitry Andric const MachineOperand &MOTied = MI->getOperand(TiedTo); 24300b57cec5SDimitry Andric if (!MOTied.isReg()) 24310b57cec5SDimitry Andric report("Tied counterpart must be a register", &MOTied, TiedTo); 2432bdd1243dSDimitry Andric else if (MOTied.getReg().isPhysical() && 24330b57cec5SDimitry Andric MO->getReg() != MOTied.getReg()) 24340b57cec5SDimitry Andric report("Tied physical registers must match.", &MOTied, TiedTo); 24350b57cec5SDimitry Andric } 24360b57cec5SDimitry Andric } else if (MO->isReg() && MO->isTied()) 24370b57cec5SDimitry Andric report("Explicit operand should not be tied", MO, MONum); 24385f757f3fSDimitry Andric } else if (!MI->isVariadic()) { 24390b57cec5SDimitry Andric // ARM adds %reg0 operands to indicate predicates. We'll allow that. 24405f757f3fSDimitry Andric if (!MO->isValidExcessOperand()) 24410b57cec5SDimitry Andric report("Extra explicit operand on non-variadic instruction", MO, MONum); 24420b57cec5SDimitry Andric } 24430b57cec5SDimitry Andric 24440b57cec5SDimitry Andric switch (MO->getType()) { 24450b57cec5SDimitry Andric case MachineOperand::MO_Register: { 2446349cc55cSDimitry Andric // Verify debug flag on debug instructions. Check this first because reg0 2447349cc55cSDimitry Andric // indicates an undefined debug value. 2448349cc55cSDimitry Andric if (MI->isDebugInstr() && MO->isUse()) { 2449349cc55cSDimitry Andric if (!MO->isDebug()) 2450349cc55cSDimitry Andric report("Register operand must be marked debug", MO, MONum); 2451349cc55cSDimitry Andric } else if (MO->isDebug()) { 2452349cc55cSDimitry Andric report("Register operand must not be marked debug", MO, MONum); 2453349cc55cSDimitry Andric } 2454349cc55cSDimitry Andric 24558bcb0991SDimitry Andric const Register Reg = MO->getReg(); 24560b57cec5SDimitry Andric if (!Reg) 24570b57cec5SDimitry Andric return; 24581fd87a68SDimitry Andric if (MRI->tracksLiveness() && !MI->isDebugInstr()) 24590b57cec5SDimitry Andric checkLiveness(MO, MONum); 24600b57cec5SDimitry Andric 246181ad6265SDimitry Andric if (MO->isDef() && MO->isUndef() && !MO->getSubReg() && 246281ad6265SDimitry Andric MO->getReg().isVirtual()) // TODO: Apply to physregs too 246381ad6265SDimitry Andric report("Undef virtual register def operands require a subregister", MO, MONum); 246481ad6265SDimitry Andric 24650b57cec5SDimitry Andric // Verify the consistency of tied operands. 24660b57cec5SDimitry Andric if (MO->isTied()) { 24670b57cec5SDimitry Andric unsigned OtherIdx = MI->findTiedOperandIdx(MONum); 24680b57cec5SDimitry Andric const MachineOperand &OtherMO = MI->getOperand(OtherIdx); 24690b57cec5SDimitry Andric if (!OtherMO.isReg()) 24700b57cec5SDimitry Andric report("Must be tied to a register", MO, MONum); 24710b57cec5SDimitry Andric if (!OtherMO.isTied()) 24720b57cec5SDimitry Andric report("Missing tie flags on tied operand", MO, MONum); 24730b57cec5SDimitry Andric if (MI->findTiedOperandIdx(OtherIdx) != MONum) 24740b57cec5SDimitry Andric report("Inconsistent tie links", MO, MONum); 24750b57cec5SDimitry Andric if (MONum < MCID.getNumDefs()) { 24760b57cec5SDimitry Andric if (OtherIdx < MCID.getNumOperands()) { 24770b57cec5SDimitry Andric if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO)) 24780b57cec5SDimitry Andric report("Explicit def tied to explicit use without tie constraint", 24790b57cec5SDimitry Andric MO, MONum); 24800b57cec5SDimitry Andric } else { 24810b57cec5SDimitry Andric if (!OtherMO.isImplicit()) 24820b57cec5SDimitry Andric report("Explicit def should be tied to implicit use", MO, MONum); 24830b57cec5SDimitry Andric } 24840b57cec5SDimitry Andric } 24850b57cec5SDimitry Andric } 24860b57cec5SDimitry Andric 24875ffd83dbSDimitry Andric // Verify two-address constraints after the twoaddressinstruction pass. 24885ffd83dbSDimitry Andric // Both twoaddressinstruction pass and phi-node-elimination pass call 24895f757f3fSDimitry Andric // MRI->leaveSSA() to set MF as not IsSSA, we should do the verification 24905f757f3fSDimitry Andric // after twoaddressinstruction pass not after phi-node-elimination pass. So 24915f757f3fSDimitry Andric // we shouldn't use the IsSSA as the condition, we should based on 24925ffd83dbSDimitry Andric // TiedOpsRewritten property to verify two-address constraints, this 24935ffd83dbSDimitry Andric // property will be set in twoaddressinstruction pass. 24940b57cec5SDimitry Andric unsigned DefIdx; 24955ffd83dbSDimitry Andric if (MF->getProperties().hasProperty( 24965ffd83dbSDimitry Andric MachineFunctionProperties::Property::TiedOpsRewritten) && 24975ffd83dbSDimitry Andric MO->isUse() && MI->isRegTiedToDefOperand(MONum, &DefIdx) && 24980b57cec5SDimitry Andric Reg != MI->getOperand(DefIdx).getReg()) 24990b57cec5SDimitry Andric report("Two-address instruction operands must be identical", MO, MONum); 25000b57cec5SDimitry Andric 25010b57cec5SDimitry Andric // Check register classes. 25020b57cec5SDimitry Andric unsigned SubIdx = MO->getSubReg(); 25030b57cec5SDimitry Andric 2504bdd1243dSDimitry Andric if (Reg.isPhysical()) { 25050b57cec5SDimitry Andric if (SubIdx) { 25060b57cec5SDimitry Andric report("Illegal subregister index for physical register", MO, MONum); 25070b57cec5SDimitry Andric return; 25080b57cec5SDimitry Andric } 25090b57cec5SDimitry Andric if (MONum < MCID.getNumOperands()) { 25100b57cec5SDimitry Andric if (const TargetRegisterClass *DRC = 25110b57cec5SDimitry Andric TII->getRegClass(MCID, MONum, TRI, *MF)) { 25120b57cec5SDimitry Andric if (!DRC->contains(Reg)) { 25130b57cec5SDimitry Andric report("Illegal physical register for instruction", MO, MONum); 25140b57cec5SDimitry Andric errs() << printReg(Reg, TRI) << " is not a " 25150b57cec5SDimitry Andric << TRI->getRegClassName(DRC) << " register.\n"; 25160b57cec5SDimitry Andric } 25170b57cec5SDimitry Andric } 25180b57cec5SDimitry Andric } 25190b57cec5SDimitry Andric if (MO->isRenamable()) { 25200b57cec5SDimitry Andric if (MRI->isReserved(Reg)) { 25210b57cec5SDimitry Andric report("isRenamable set on reserved register", MO, MONum); 25220b57cec5SDimitry Andric return; 25230b57cec5SDimitry Andric } 25240b57cec5SDimitry Andric } 25250b57cec5SDimitry Andric } else { 25260b57cec5SDimitry Andric // Virtual register. 25270b57cec5SDimitry Andric const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg); 25280b57cec5SDimitry Andric if (!RC) { 25290b57cec5SDimitry Andric // This is a generic virtual register. 25300b57cec5SDimitry Andric 25315ffd83dbSDimitry Andric // Do not allow undef uses for generic virtual registers. This ensures 25325ffd83dbSDimitry Andric // getVRegDef can never fail and return null on a generic register. 25335ffd83dbSDimitry Andric // 25345ffd83dbSDimitry Andric // FIXME: This restriction should probably be broadened to all SSA 25355ffd83dbSDimitry Andric // MIR. However, DetectDeadLanes/ProcessImplicitDefs technically still 25365ffd83dbSDimitry Andric // run on the SSA function just before phi elimination. 25375ffd83dbSDimitry Andric if (MO->isUndef()) 25385ffd83dbSDimitry Andric report("Generic virtual register use cannot be undef", MO, MONum); 25395ffd83dbSDimitry Andric 25400eae32dcSDimitry Andric // Debug value instruction is permitted to use undefined vregs. 25410eae32dcSDimitry Andric // This is a performance measure to skip the overhead of immediately 25420eae32dcSDimitry Andric // pruning unused debug operands. The final undef substitution occurs 25430eae32dcSDimitry Andric // when debug values are allocated in LDVImpl::handleDebugValue, so 25440eae32dcSDimitry Andric // these verifications always apply after this pass. 25450eae32dcSDimitry Andric if (isFunctionTracksDebugUserValues || !MO->isUse() || 25460eae32dcSDimitry Andric !MI->isDebugValue() || !MRI->def_empty(Reg)) { 25470b57cec5SDimitry Andric // If we're post-Select, we can't have gvregs anymore. 25480b57cec5SDimitry Andric if (isFunctionSelected) { 25490b57cec5SDimitry Andric report("Generic virtual register invalid in a Selected function", 25500b57cec5SDimitry Andric MO, MONum); 25510b57cec5SDimitry Andric return; 25520b57cec5SDimitry Andric } 25530b57cec5SDimitry Andric 25540b57cec5SDimitry Andric // The gvreg must have a type and it must not have a SubIdx. 25550b57cec5SDimitry Andric LLT Ty = MRI->getType(Reg); 25560b57cec5SDimitry Andric if (!Ty.isValid()) { 25570b57cec5SDimitry Andric report("Generic virtual register must have a valid type", MO, 25580b57cec5SDimitry Andric MONum); 25590b57cec5SDimitry Andric return; 25600b57cec5SDimitry Andric } 25610b57cec5SDimitry Andric 25620b57cec5SDimitry Andric const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg); 256306c3fb27SDimitry Andric const RegisterBankInfo *RBI = MF->getSubtarget().getRegBankInfo(); 25640b57cec5SDimitry Andric 25650b57cec5SDimitry Andric // If we're post-RegBankSelect, the gvreg must have a bank. 25660b57cec5SDimitry Andric if (!RegBank && isFunctionRegBankSelected) { 25670b57cec5SDimitry Andric report("Generic virtual register must have a bank in a " 25680b57cec5SDimitry Andric "RegBankSelected function", 25690b57cec5SDimitry Andric MO, MONum); 25700b57cec5SDimitry Andric return; 25710b57cec5SDimitry Andric } 25720b57cec5SDimitry Andric 25730b57cec5SDimitry Andric // Make sure the register fits into its register bank if any. 25745f757f3fSDimitry Andric if (RegBank && Ty.isValid() && !Ty.isScalableVector() && 257506c3fb27SDimitry Andric RBI->getMaximumSize(RegBank->getID()) < Ty.getSizeInBits()) { 25760b57cec5SDimitry Andric report("Register bank is too small for virtual register", MO, 25770b57cec5SDimitry Andric MONum); 25780b57cec5SDimitry Andric errs() << "Register bank " << RegBank->getName() << " too small(" 257906c3fb27SDimitry Andric << RBI->getMaximumSize(RegBank->getID()) << ") to fit " 258006c3fb27SDimitry Andric << Ty.getSizeInBits() << "-bits\n"; 25810b57cec5SDimitry Andric return; 25820b57cec5SDimitry Andric } 25830eae32dcSDimitry Andric } 25840eae32dcSDimitry Andric 25850b57cec5SDimitry Andric if (SubIdx) { 25860b57cec5SDimitry Andric report("Generic virtual register does not allow subregister index", MO, 25870b57cec5SDimitry Andric MONum); 25880b57cec5SDimitry Andric return; 25890b57cec5SDimitry Andric } 25900b57cec5SDimitry Andric 25910b57cec5SDimitry Andric // If this is a target specific instruction and this operand 25920b57cec5SDimitry Andric // has register class constraint, the virtual register must 25930b57cec5SDimitry Andric // comply to it. 25940b57cec5SDimitry Andric if (!isPreISelGenericOpcode(MCID.getOpcode()) && 25950b57cec5SDimitry Andric MONum < MCID.getNumOperands() && 25960b57cec5SDimitry Andric TII->getRegClass(MCID, MONum, TRI, *MF)) { 25970b57cec5SDimitry Andric report("Virtual register does not match instruction constraint", MO, 25980b57cec5SDimitry Andric MONum); 25990b57cec5SDimitry Andric errs() << "Expect register class " 26000b57cec5SDimitry Andric << TRI->getRegClassName( 26010b57cec5SDimitry Andric TII->getRegClass(MCID, MONum, TRI, *MF)) 26020b57cec5SDimitry Andric << " but got nothing\n"; 26030b57cec5SDimitry Andric return; 26040b57cec5SDimitry Andric } 26050b57cec5SDimitry Andric 26060b57cec5SDimitry Andric break; 26070b57cec5SDimitry Andric } 26080b57cec5SDimitry Andric if (SubIdx) { 26090b57cec5SDimitry Andric const TargetRegisterClass *SRC = 26100b57cec5SDimitry Andric TRI->getSubClassWithSubReg(RC, SubIdx); 26110b57cec5SDimitry Andric if (!SRC) { 26120b57cec5SDimitry Andric report("Invalid subregister index for virtual register", MO, MONum); 26130b57cec5SDimitry Andric errs() << "Register class " << TRI->getRegClassName(RC) 26140b57cec5SDimitry Andric << " does not support subreg index " << SubIdx << "\n"; 26150b57cec5SDimitry Andric return; 26160b57cec5SDimitry Andric } 26170b57cec5SDimitry Andric if (RC != SRC) { 26180b57cec5SDimitry Andric report("Invalid register class for subregister index", MO, MONum); 26190b57cec5SDimitry Andric errs() << "Register class " << TRI->getRegClassName(RC) 26200b57cec5SDimitry Andric << " does not fully support subreg index " << SubIdx << "\n"; 26210b57cec5SDimitry Andric return; 26220b57cec5SDimitry Andric } 26230b57cec5SDimitry Andric } 26240b57cec5SDimitry Andric if (MONum < MCID.getNumOperands()) { 26250b57cec5SDimitry Andric if (const TargetRegisterClass *DRC = 26260b57cec5SDimitry Andric TII->getRegClass(MCID, MONum, TRI, *MF)) { 26270b57cec5SDimitry Andric if (SubIdx) { 26280b57cec5SDimitry Andric const TargetRegisterClass *SuperRC = 26290b57cec5SDimitry Andric TRI->getLargestLegalSuperClass(RC, *MF); 26300b57cec5SDimitry Andric if (!SuperRC) { 26310b57cec5SDimitry Andric report("No largest legal super class exists.", MO, MONum); 26320b57cec5SDimitry Andric return; 26330b57cec5SDimitry Andric } 26340b57cec5SDimitry Andric DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx); 26350b57cec5SDimitry Andric if (!DRC) { 26360b57cec5SDimitry Andric report("No matching super-reg register class.", MO, MONum); 26370b57cec5SDimitry Andric return; 26380b57cec5SDimitry Andric } 26390b57cec5SDimitry Andric } 26400b57cec5SDimitry Andric if (!RC->hasSuperClassEq(DRC)) { 26410b57cec5SDimitry Andric report("Illegal virtual register for instruction", MO, MONum); 26420b57cec5SDimitry Andric errs() << "Expected a " << TRI->getRegClassName(DRC) 26430b57cec5SDimitry Andric << " register, but got a " << TRI->getRegClassName(RC) 26440b57cec5SDimitry Andric << " register\n"; 26450b57cec5SDimitry Andric } 26460b57cec5SDimitry Andric } 26470b57cec5SDimitry Andric } 26480b57cec5SDimitry Andric } 26490b57cec5SDimitry Andric break; 26500b57cec5SDimitry Andric } 26510b57cec5SDimitry Andric 26520b57cec5SDimitry Andric case MachineOperand::MO_RegisterMask: 26530b57cec5SDimitry Andric regMasks.push_back(MO->getRegMask()); 26540b57cec5SDimitry Andric break; 26550b57cec5SDimitry Andric 26560b57cec5SDimitry Andric case MachineOperand::MO_MachineBasicBlock: 26570b57cec5SDimitry Andric if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent())) 26580b57cec5SDimitry Andric report("PHI operand is not in the CFG", MO, MONum); 26590b57cec5SDimitry Andric break; 26600b57cec5SDimitry Andric 26610b57cec5SDimitry Andric case MachineOperand::MO_FrameIndex: 26620b57cec5SDimitry Andric if (LiveStks && LiveStks->hasInterval(MO->getIndex()) && 26630b57cec5SDimitry Andric LiveInts && !LiveInts->isNotInMIMap(*MI)) { 26640b57cec5SDimitry Andric int FI = MO->getIndex(); 26650b57cec5SDimitry Andric LiveInterval &LI = LiveStks->getInterval(FI); 26660b57cec5SDimitry Andric SlotIndex Idx = LiveInts->getInstructionIndex(*MI); 26670b57cec5SDimitry Andric 26680b57cec5SDimitry Andric bool stores = MI->mayStore(); 26690b57cec5SDimitry Andric bool loads = MI->mayLoad(); 26700b57cec5SDimitry Andric // For a memory-to-memory move, we need to check if the frame 26710b57cec5SDimitry Andric // index is used for storing or loading, by inspecting the 26720b57cec5SDimitry Andric // memory operands. 26730b57cec5SDimitry Andric if (stores && loads) { 26740b57cec5SDimitry Andric for (auto *MMO : MI->memoperands()) { 26750b57cec5SDimitry Andric const PseudoSourceValue *PSV = MMO->getPseudoValue(); 26760b57cec5SDimitry Andric if (PSV == nullptr) continue; 26770b57cec5SDimitry Andric const FixedStackPseudoSourceValue *Value = 26780b57cec5SDimitry Andric dyn_cast<FixedStackPseudoSourceValue>(PSV); 26790b57cec5SDimitry Andric if (Value == nullptr) continue; 26800b57cec5SDimitry Andric if (Value->getFrameIndex() != FI) continue; 26810b57cec5SDimitry Andric 26820b57cec5SDimitry Andric if (MMO->isStore()) 26830b57cec5SDimitry Andric loads = false; 26840b57cec5SDimitry Andric else 26850b57cec5SDimitry Andric stores = false; 26860b57cec5SDimitry Andric break; 26870b57cec5SDimitry Andric } 26880b57cec5SDimitry Andric if (loads == stores) 26890b57cec5SDimitry Andric report("Missing fixed stack memoperand.", MI); 26900b57cec5SDimitry Andric } 26910b57cec5SDimitry Andric if (loads && !LI.liveAt(Idx.getRegSlot(true))) { 26920b57cec5SDimitry Andric report("Instruction loads from dead spill slot", MO, MONum); 26930b57cec5SDimitry Andric errs() << "Live stack: " << LI << '\n'; 26940b57cec5SDimitry Andric } 26950b57cec5SDimitry Andric if (stores && !LI.liveAt(Idx.getRegSlot())) { 26960b57cec5SDimitry Andric report("Instruction stores to dead spill slot", MO, MONum); 26970b57cec5SDimitry Andric errs() << "Live stack: " << LI << '\n'; 26980b57cec5SDimitry Andric } 26990b57cec5SDimitry Andric } 27000b57cec5SDimitry Andric break; 27010b57cec5SDimitry Andric 270281ad6265SDimitry Andric case MachineOperand::MO_CFIIndex: 270381ad6265SDimitry Andric if (MO->getCFIIndex() >= MF->getFrameInstructions().size()) 270481ad6265SDimitry Andric report("CFI instruction has invalid index", MO, MONum); 270581ad6265SDimitry Andric break; 270681ad6265SDimitry Andric 27070b57cec5SDimitry Andric default: 27080b57cec5SDimitry Andric break; 27090b57cec5SDimitry Andric } 27100b57cec5SDimitry Andric } 27110b57cec5SDimitry Andric 27120b57cec5SDimitry Andric void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO, 2713e8d8bef9SDimitry Andric unsigned MONum, SlotIndex UseIdx, 2714e8d8bef9SDimitry Andric const LiveRange &LR, 2715e8d8bef9SDimitry Andric Register VRegOrUnit, 27160b57cec5SDimitry Andric LaneBitmask LaneMask) { 27175f757f3fSDimitry Andric const MachineInstr *MI = MO->getParent(); 27180b57cec5SDimitry Andric LiveQueryResult LRQ = LR.Query(UseIdx); 27195f757f3fSDimitry Andric bool HasValue = LRQ.valueIn() || (MI->isPHI() && LRQ.valueOut()); 27200b57cec5SDimitry Andric // Check if we have a segment at the use, note however that we only need one 27210b57cec5SDimitry Andric // live subregister range, the others may be dead. 27225f757f3fSDimitry Andric if (!HasValue && LaneMask.none()) { 27230b57cec5SDimitry Andric report("No live segment at use", MO, MONum); 27240b57cec5SDimitry Andric report_context_liverange(LR); 27250b57cec5SDimitry Andric report_context_vreg_regunit(VRegOrUnit); 27260b57cec5SDimitry Andric report_context(UseIdx); 27270b57cec5SDimitry Andric } 27280b57cec5SDimitry Andric if (MO->isKill() && !LRQ.isKill()) { 27290b57cec5SDimitry Andric report("Live range continues after kill flag", MO, MONum); 27300b57cec5SDimitry Andric report_context_liverange(LR); 27310b57cec5SDimitry Andric report_context_vreg_regunit(VRegOrUnit); 27320b57cec5SDimitry Andric if (LaneMask.any()) 27330b57cec5SDimitry Andric report_context_lanemask(LaneMask); 27340b57cec5SDimitry Andric report_context(UseIdx); 27350b57cec5SDimitry Andric } 27360b57cec5SDimitry Andric } 27370b57cec5SDimitry Andric 27380b57cec5SDimitry Andric void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO, 2739e8d8bef9SDimitry Andric unsigned MONum, SlotIndex DefIdx, 2740e8d8bef9SDimitry Andric const LiveRange &LR, 2741e8d8bef9SDimitry Andric Register VRegOrUnit, 2742e8d8bef9SDimitry Andric bool SubRangeCheck, 2743e8d8bef9SDimitry Andric LaneBitmask LaneMask) { 27440b57cec5SDimitry Andric if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) { 2745bdd1243dSDimitry Andric // The LR can correspond to the whole reg and its def slot is not obliged 2746bdd1243dSDimitry Andric // to be the same as the MO' def slot. E.g. when we check here "normal" 2747bdd1243dSDimitry Andric // subreg MO but there is other EC subreg MO in the same instruction so the 2748bdd1243dSDimitry Andric // whole reg has EC def slot and differs from the currently checked MO' def 2749bdd1243dSDimitry Andric // slot. For example: 2750bdd1243dSDimitry Andric // %0 [16e,32r:0) 0@16e L..3 [16e,32r:0) 0@16e L..C [16r,32r:0) 0@16r 2751bdd1243dSDimitry Andric // Check that there is an early-clobber def of the same superregister 2752bdd1243dSDimitry Andric // somewhere is performed in visitMachineFunctionAfter() 2753bdd1243dSDimitry Andric if (((SubRangeCheck || MO->getSubReg() == 0) && VNI->def != DefIdx) || 2754bdd1243dSDimitry Andric !SlotIndex::isSameInstr(VNI->def, DefIdx) || 2755bdd1243dSDimitry Andric (VNI->def != DefIdx && 2756bdd1243dSDimitry Andric (!VNI->def.isEarlyClobber() || !DefIdx.isRegister()))) { 27570b57cec5SDimitry Andric report("Inconsistent valno->def", MO, MONum); 27580b57cec5SDimitry Andric report_context_liverange(LR); 27590b57cec5SDimitry Andric report_context_vreg_regunit(VRegOrUnit); 27600b57cec5SDimitry Andric if (LaneMask.any()) 27610b57cec5SDimitry Andric report_context_lanemask(LaneMask); 27620b57cec5SDimitry Andric report_context(*VNI); 27630b57cec5SDimitry Andric report_context(DefIdx); 27640b57cec5SDimitry Andric } 27650b57cec5SDimitry Andric } else { 27660b57cec5SDimitry Andric report("No live segment at def", MO, MONum); 27670b57cec5SDimitry Andric report_context_liverange(LR); 27680b57cec5SDimitry Andric report_context_vreg_regunit(VRegOrUnit); 27690b57cec5SDimitry Andric if (LaneMask.any()) 27700b57cec5SDimitry Andric report_context_lanemask(LaneMask); 27710b57cec5SDimitry Andric report_context(DefIdx); 27720b57cec5SDimitry Andric } 27730b57cec5SDimitry Andric // Check that, if the dead def flag is present, LiveInts agree. 27740b57cec5SDimitry Andric if (MO->isDead()) { 27750b57cec5SDimitry Andric LiveQueryResult LRQ = LR.Query(DefIdx); 27760b57cec5SDimitry Andric if (!LRQ.isDeadDef()) { 2777bdd1243dSDimitry Andric assert(VRegOrUnit.isVirtual() && "Expecting a virtual register."); 27780b57cec5SDimitry Andric // A dead subreg def only tells us that the specific subreg is dead. There 27790b57cec5SDimitry Andric // could be other non-dead defs of other subregs, or we could have other 27800b57cec5SDimitry Andric // parts of the register being live through the instruction. So unless we 27810b57cec5SDimitry Andric // are checking liveness for a subrange it is ok for the live range to 27820b57cec5SDimitry Andric // continue, given that we have a dead def of a subregister. 27830b57cec5SDimitry Andric if (SubRangeCheck || MO->getSubReg() == 0) { 27840b57cec5SDimitry Andric report("Live range continues after dead def flag", MO, MONum); 27850b57cec5SDimitry Andric report_context_liverange(LR); 27860b57cec5SDimitry Andric report_context_vreg_regunit(VRegOrUnit); 27870b57cec5SDimitry Andric if (LaneMask.any()) 27880b57cec5SDimitry Andric report_context_lanemask(LaneMask); 27890b57cec5SDimitry Andric } 27900b57cec5SDimitry Andric } 27910b57cec5SDimitry Andric } 27920b57cec5SDimitry Andric } 27930b57cec5SDimitry Andric 27940b57cec5SDimitry Andric void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) { 27950b57cec5SDimitry Andric const MachineInstr *MI = MO->getParent(); 2796e8d8bef9SDimitry Andric const Register Reg = MO->getReg(); 2797349cc55cSDimitry Andric const unsigned SubRegIdx = MO->getSubReg(); 2798349cc55cSDimitry Andric 2799349cc55cSDimitry Andric const LiveInterval *LI = nullptr; 2800349cc55cSDimitry Andric if (LiveInts && Reg.isVirtual()) { 2801349cc55cSDimitry Andric if (LiveInts->hasInterval(Reg)) { 2802349cc55cSDimitry Andric LI = &LiveInts->getInterval(Reg); 28030eae32dcSDimitry Andric if (SubRegIdx != 0 && (MO->isDef() || !MO->isUndef()) && !LI->empty() && 28040eae32dcSDimitry Andric !LI->hasSubRanges() && MRI->shouldTrackSubRegLiveness(Reg)) 2805349cc55cSDimitry Andric report("Live interval for subreg operand has no subranges", MO, MONum); 2806349cc55cSDimitry Andric } else { 2807349cc55cSDimitry Andric report("Virtual register has no live interval", MO, MONum); 2808349cc55cSDimitry Andric } 2809349cc55cSDimitry Andric } 28100b57cec5SDimitry Andric 28110b57cec5SDimitry Andric // Both use and def operands can read a register. 28120b57cec5SDimitry Andric if (MO->readsReg()) { 28130b57cec5SDimitry Andric if (MO->isKill()) 28140b57cec5SDimitry Andric addRegWithSubRegs(regsKilled, Reg); 28150b57cec5SDimitry Andric 2816349cc55cSDimitry Andric // Check that LiveVars knows this kill (unless we are inside a bundle, in 2817349cc55cSDimitry Andric // which case we have already checked that LiveVars knows any kills on the 2818349cc55cSDimitry Andric // bundle header instead). 2819349cc55cSDimitry Andric if (LiveVars && Reg.isVirtual() && MO->isKill() && 2820349cc55cSDimitry Andric !MI->isBundledWithPred()) { 28210b57cec5SDimitry Andric LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); 28220b57cec5SDimitry Andric if (!is_contained(VI.Kills, MI)) 28230b57cec5SDimitry Andric report("Kill missing from LiveVariables", MO, MONum); 28240b57cec5SDimitry Andric } 28250b57cec5SDimitry Andric 28260b57cec5SDimitry Andric // Check LiveInts liveness and kill. 28270b57cec5SDimitry Andric if (LiveInts && !LiveInts->isNotInMIMap(*MI)) { 28285f757f3fSDimitry Andric SlotIndex UseIdx; 28295f757f3fSDimitry Andric if (MI->isPHI()) { 28305f757f3fSDimitry Andric // PHI use occurs on the edge, so check for live out here instead. 28315f757f3fSDimitry Andric UseIdx = LiveInts->getMBBEndIdx( 28325f757f3fSDimitry Andric MI->getOperand(MONum + 1).getMBB()).getPrevSlot(); 28335f757f3fSDimitry Andric } else { 28345f757f3fSDimitry Andric UseIdx = LiveInts->getInstructionIndex(*MI); 28355f757f3fSDimitry Andric } 28360b57cec5SDimitry Andric // Check the cached regunit intervals. 2837e8d8bef9SDimitry Andric if (Reg.isPhysical() && !isReserved(Reg)) { 283806c3fb27SDimitry Andric for (MCRegUnit Unit : TRI->regunits(Reg.asMCReg())) { 283906c3fb27SDimitry Andric if (MRI->isReservedRegUnit(Unit)) 28400b57cec5SDimitry Andric continue; 284106c3fb27SDimitry Andric if (const LiveRange *LR = LiveInts->getCachedRegUnit(Unit)) 284206c3fb27SDimitry Andric checkLivenessAtUse(MO, MONum, UseIdx, *LR, Unit); 28430b57cec5SDimitry Andric } 28440b57cec5SDimitry Andric } 28450b57cec5SDimitry Andric 2846349cc55cSDimitry Andric if (Reg.isVirtual()) { 28470b57cec5SDimitry Andric // This is a virtual register interval. 2848349cc55cSDimitry Andric checkLivenessAtUse(MO, MONum, UseIdx, *LI, Reg); 28490b57cec5SDimitry Andric 2850349cc55cSDimitry Andric if (LI->hasSubRanges() && !MO->isDef()) { 28510b57cec5SDimitry Andric LaneBitmask MOMask = SubRegIdx != 0 28520b57cec5SDimitry Andric ? TRI->getSubRegIndexLaneMask(SubRegIdx) 28530b57cec5SDimitry Andric : MRI->getMaxLaneMaskForVReg(Reg); 28540b57cec5SDimitry Andric LaneBitmask LiveInMask; 2855349cc55cSDimitry Andric for (const LiveInterval::SubRange &SR : LI->subranges()) { 28560b57cec5SDimitry Andric if ((MOMask & SR.LaneMask).none()) 28570b57cec5SDimitry Andric continue; 28580b57cec5SDimitry Andric checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask); 28590b57cec5SDimitry Andric LiveQueryResult LRQ = SR.Query(UseIdx); 28605f757f3fSDimitry Andric if (LRQ.valueIn() || (MI->isPHI() && LRQ.valueOut())) 28610b57cec5SDimitry Andric LiveInMask |= SR.LaneMask; 28620b57cec5SDimitry Andric } 28630b57cec5SDimitry Andric // At least parts of the register has to be live at the use. 28640b57cec5SDimitry Andric if ((LiveInMask & MOMask).none()) { 28650b57cec5SDimitry Andric report("No live subrange at use", MO, MONum); 2866349cc55cSDimitry Andric report_context(*LI); 28670b57cec5SDimitry Andric report_context(UseIdx); 28680b57cec5SDimitry Andric } 28695f757f3fSDimitry Andric // For PHIs all lanes should be live 28705f757f3fSDimitry Andric if (MI->isPHI() && LiveInMask != MOMask) { 28715f757f3fSDimitry Andric report("Not all lanes of PHI source live at use", MO, MONum); 28725f757f3fSDimitry Andric report_context(*LI); 28735f757f3fSDimitry Andric report_context(UseIdx); 28745f757f3fSDimitry Andric } 28750b57cec5SDimitry Andric } 28760b57cec5SDimitry Andric } 28770b57cec5SDimitry Andric } 28780b57cec5SDimitry Andric 28790b57cec5SDimitry Andric // Use of a dead register. 28800b57cec5SDimitry Andric if (!regsLive.count(Reg)) { 2881349cc55cSDimitry Andric if (Reg.isPhysical()) { 28820b57cec5SDimitry Andric // Reserved registers may be used even when 'dead'. 28830b57cec5SDimitry Andric bool Bad = !isReserved(Reg); 28840b57cec5SDimitry Andric // We are fine if just any subregister has a defined value. 28850b57cec5SDimitry Andric if (Bad) { 2886480093f4SDimitry Andric 2887480093f4SDimitry Andric for (const MCPhysReg &SubReg : TRI->subregs(Reg)) { 2888480093f4SDimitry Andric if (regsLive.count(SubReg)) { 28890b57cec5SDimitry Andric Bad = false; 28900b57cec5SDimitry Andric break; 28910b57cec5SDimitry Andric } 28920b57cec5SDimitry Andric } 28930b57cec5SDimitry Andric } 28940b57cec5SDimitry Andric // If there is an additional implicit-use of a super register we stop 28950b57cec5SDimitry Andric // here. By definition we are fine if the super register is not 28960b57cec5SDimitry Andric // (completely) dead, if the complete super register is dead we will 28970b57cec5SDimitry Andric // get a report for its operand. 28980b57cec5SDimitry Andric if (Bad) { 28990b57cec5SDimitry Andric for (const MachineOperand &MOP : MI->uses()) { 29000b57cec5SDimitry Andric if (!MOP.isReg() || !MOP.isImplicit()) 29010b57cec5SDimitry Andric continue; 29020b57cec5SDimitry Andric 2903349cc55cSDimitry Andric if (!MOP.getReg().isPhysical()) 29040b57cec5SDimitry Andric continue; 29050b57cec5SDimitry Andric 2906fe6060f1SDimitry Andric if (llvm::is_contained(TRI->subregs(MOP.getReg()), Reg)) 29070b57cec5SDimitry Andric Bad = false; 29080b57cec5SDimitry Andric } 29090b57cec5SDimitry Andric } 29100b57cec5SDimitry Andric if (Bad) 29110b57cec5SDimitry Andric report("Using an undefined physical register", MO, MONum); 29120b57cec5SDimitry Andric } else if (MRI->def_empty(Reg)) { 29130b57cec5SDimitry Andric report("Reading virtual register without a def", MO, MONum); 29140b57cec5SDimitry Andric } else { 29150b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[MI->getParent()]; 29160b57cec5SDimitry Andric // We don't know which virtual registers are live in, so only complain 29170b57cec5SDimitry Andric // if vreg was killed in this MBB. Otherwise keep track of vregs that 29180b57cec5SDimitry Andric // must be live in. PHI instructions are handled separately. 29190b57cec5SDimitry Andric if (MInfo.regsKilled.count(Reg)) 29200b57cec5SDimitry Andric report("Using a killed virtual register", MO, MONum); 29210b57cec5SDimitry Andric else if (!MI->isPHI()) 29220b57cec5SDimitry Andric MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI)); 29230b57cec5SDimitry Andric } 29240b57cec5SDimitry Andric } 29250b57cec5SDimitry Andric } 29260b57cec5SDimitry Andric 29270b57cec5SDimitry Andric if (MO->isDef()) { 29280b57cec5SDimitry Andric // Register defined. 29290b57cec5SDimitry Andric // TODO: verify that earlyclobber ops are not used. 29300b57cec5SDimitry Andric if (MO->isDead()) 29310b57cec5SDimitry Andric addRegWithSubRegs(regsDead, Reg); 29320b57cec5SDimitry Andric else 29330b57cec5SDimitry Andric addRegWithSubRegs(regsDefined, Reg); 29340b57cec5SDimitry Andric 29350b57cec5SDimitry Andric // Verify SSA form. 2936349cc55cSDimitry Andric if (MRI->isSSA() && Reg.isVirtual() && 29370b57cec5SDimitry Andric std::next(MRI->def_begin(Reg)) != MRI->def_end()) 29380b57cec5SDimitry Andric report("Multiple virtual register defs in SSA form", MO, MONum); 29390b57cec5SDimitry Andric 29400b57cec5SDimitry Andric // Check LiveInts for a live segment, but only for virtual registers. 29410b57cec5SDimitry Andric if (LiveInts && !LiveInts->isNotInMIMap(*MI)) { 29420b57cec5SDimitry Andric SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI); 29430b57cec5SDimitry Andric DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber()); 29440b57cec5SDimitry Andric 2945349cc55cSDimitry Andric if (Reg.isVirtual()) { 2946349cc55cSDimitry Andric checkLivenessAtDef(MO, MONum, DefIdx, *LI, Reg); 29470b57cec5SDimitry Andric 2948349cc55cSDimitry Andric if (LI->hasSubRanges()) { 29490b57cec5SDimitry Andric LaneBitmask MOMask = SubRegIdx != 0 29500b57cec5SDimitry Andric ? TRI->getSubRegIndexLaneMask(SubRegIdx) 29510b57cec5SDimitry Andric : MRI->getMaxLaneMaskForVReg(Reg); 2952349cc55cSDimitry Andric for (const LiveInterval::SubRange &SR : LI->subranges()) { 29530b57cec5SDimitry Andric if ((SR.LaneMask & MOMask).none()) 29540b57cec5SDimitry Andric continue; 29550b57cec5SDimitry Andric checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, true, SR.LaneMask); 29560b57cec5SDimitry Andric } 29570b57cec5SDimitry Andric } 29580b57cec5SDimitry Andric } 29590b57cec5SDimitry Andric } 29600b57cec5SDimitry Andric } 29610b57cec5SDimitry Andric } 29620b57cec5SDimitry Andric 29630b57cec5SDimitry Andric // This function gets called after visiting all instructions in a bundle. The 29640b57cec5SDimitry Andric // argument points to the bundle header. 29650b57cec5SDimitry Andric // Normal stand-alone instructions are also considered 'bundles', and this 29660b57cec5SDimitry Andric // function is called for all of them. 29670b57cec5SDimitry Andric void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) { 29680b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[MI->getParent()]; 29690b57cec5SDimitry Andric set_union(MInfo.regsKilled, regsKilled); 29700b57cec5SDimitry Andric set_subtract(regsLive, regsKilled); regsKilled.clear(); 29710b57cec5SDimitry Andric // Kill any masked registers. 29720b57cec5SDimitry Andric while (!regMasks.empty()) { 29730b57cec5SDimitry Andric const uint32_t *Mask = regMasks.pop_back_val(); 2974e8d8bef9SDimitry Andric for (Register Reg : regsLive) 2975e8d8bef9SDimitry Andric if (Reg.isPhysical() && 2976e8d8bef9SDimitry Andric MachineOperand::clobbersPhysReg(Mask, Reg.asMCReg())) 29775ffd83dbSDimitry Andric regsDead.push_back(Reg); 29780b57cec5SDimitry Andric } 29790b57cec5SDimitry Andric set_subtract(regsLive, regsDead); regsDead.clear(); 29800b57cec5SDimitry Andric set_union(regsLive, regsDefined); regsDefined.clear(); 29810b57cec5SDimitry Andric } 29820b57cec5SDimitry Andric 29830b57cec5SDimitry Andric void 29840b57cec5SDimitry Andric MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) { 29850b57cec5SDimitry Andric MBBInfoMap[MBB].regsLiveOut = regsLive; 29860b57cec5SDimitry Andric regsLive.clear(); 29870b57cec5SDimitry Andric 29880b57cec5SDimitry Andric if (Indexes) { 29890b57cec5SDimitry Andric SlotIndex stop = Indexes->getMBBEndIdx(MBB); 29900b57cec5SDimitry Andric if (!(stop > lastIndex)) { 29910b57cec5SDimitry Andric report("Block ends before last instruction index", MBB); 29920b57cec5SDimitry Andric errs() << "Block ends at " << stop 29930b57cec5SDimitry Andric << " last instruction was at " << lastIndex << '\n'; 29940b57cec5SDimitry Andric } 29950b57cec5SDimitry Andric lastIndex = stop; 29960b57cec5SDimitry Andric } 29970b57cec5SDimitry Andric } 29980b57cec5SDimitry Andric 29995ffd83dbSDimitry Andric namespace { 30005ffd83dbSDimitry Andric // This implements a set of registers that serves as a filter: can filter other 30015ffd83dbSDimitry Andric // sets by passing through elements not in the filter and blocking those that 30025ffd83dbSDimitry Andric // are. Any filter implicitly includes the full set of physical registers upon 30035ffd83dbSDimitry Andric // creation, thus filtering them all out. The filter itself as a set only grows, 30045ffd83dbSDimitry Andric // and needs to be as efficient as possible. 30055ffd83dbSDimitry Andric struct VRegFilter { 30065ffd83dbSDimitry Andric // Add elements to the filter itself. \pre Input set \p FromRegSet must have 30075ffd83dbSDimitry Andric // no duplicates. Both virtual and physical registers are fine. 30085ffd83dbSDimitry Andric template <typename RegSetT> void add(const RegSetT &FromRegSet) { 3009e8d8bef9SDimitry Andric SmallVector<Register, 0> VRegsBuffer; 30105ffd83dbSDimitry Andric filterAndAdd(FromRegSet, VRegsBuffer); 30115ffd83dbSDimitry Andric } 30125ffd83dbSDimitry Andric // Filter \p FromRegSet through the filter and append passed elements into \p 30135ffd83dbSDimitry Andric // ToVRegs. All elements appended are then added to the filter itself. 30145ffd83dbSDimitry Andric // \returns true if anything changed. 30155ffd83dbSDimitry Andric template <typename RegSetT> 30165ffd83dbSDimitry Andric bool filterAndAdd(const RegSetT &FromRegSet, 3017e8d8bef9SDimitry Andric SmallVectorImpl<Register> &ToVRegs) { 30185ffd83dbSDimitry Andric unsigned SparseUniverse = Sparse.size(); 30195ffd83dbSDimitry Andric unsigned NewSparseUniverse = SparseUniverse; 30205ffd83dbSDimitry Andric unsigned NewDenseSize = Dense.size(); 30215ffd83dbSDimitry Andric size_t Begin = ToVRegs.size(); 3022e8d8bef9SDimitry Andric for (Register Reg : FromRegSet) { 3023e8d8bef9SDimitry Andric if (!Reg.isVirtual()) 30245ffd83dbSDimitry Andric continue; 30255ffd83dbSDimitry Andric unsigned Index = Register::virtReg2Index(Reg); 30265ffd83dbSDimitry Andric if (Index < SparseUniverseMax) { 30275ffd83dbSDimitry Andric if (Index < SparseUniverse && Sparse.test(Index)) 30285ffd83dbSDimitry Andric continue; 30295ffd83dbSDimitry Andric NewSparseUniverse = std::max(NewSparseUniverse, Index + 1); 30305ffd83dbSDimitry Andric } else { 30315ffd83dbSDimitry Andric if (Dense.count(Reg)) 30325ffd83dbSDimitry Andric continue; 30335ffd83dbSDimitry Andric ++NewDenseSize; 30345ffd83dbSDimitry Andric } 30355ffd83dbSDimitry Andric ToVRegs.push_back(Reg); 30365ffd83dbSDimitry Andric } 30375ffd83dbSDimitry Andric size_t End = ToVRegs.size(); 30385ffd83dbSDimitry Andric if (Begin == End) 30395ffd83dbSDimitry Andric return false; 30405ffd83dbSDimitry Andric // Reserving space in sets once performs better than doing so continuously 30415ffd83dbSDimitry Andric // and pays easily for double look-ups (even in Dense with SparseUniverseMax 30425ffd83dbSDimitry Andric // tuned all the way down) and double iteration (the second one is over a 30435ffd83dbSDimitry Andric // SmallVector, which is a lot cheaper compared to DenseSet or BitVector). 30445ffd83dbSDimitry Andric Sparse.resize(NewSparseUniverse); 30455ffd83dbSDimitry Andric Dense.reserve(NewDenseSize); 30465ffd83dbSDimitry Andric for (unsigned I = Begin; I < End; ++I) { 3047e8d8bef9SDimitry Andric Register Reg = ToVRegs[I]; 30485ffd83dbSDimitry Andric unsigned Index = Register::virtReg2Index(Reg); 30495ffd83dbSDimitry Andric if (Index < SparseUniverseMax) 30505ffd83dbSDimitry Andric Sparse.set(Index); 30515ffd83dbSDimitry Andric else 30525ffd83dbSDimitry Andric Dense.insert(Reg); 30535ffd83dbSDimitry Andric } 30545ffd83dbSDimitry Andric return true; 30555ffd83dbSDimitry Andric } 30565ffd83dbSDimitry Andric 30575ffd83dbSDimitry Andric private: 30585ffd83dbSDimitry Andric static constexpr unsigned SparseUniverseMax = 10 * 1024 * 8; 30595ffd83dbSDimitry Andric // VRegs indexed within SparseUniverseMax are tracked by Sparse, those beyound 30605ffd83dbSDimitry Andric // are tracked by Dense. The only purpose of the threashold and the Dense set 30615ffd83dbSDimitry Andric // is to have a reasonably growing memory usage in pathological cases (large 30625ffd83dbSDimitry Andric // number of very sparse VRegFilter instances live at the same time). In 30635ffd83dbSDimitry Andric // practice even in the worst-by-execution time cases having all elements 30645ffd83dbSDimitry Andric // tracked by Sparse (very large SparseUniverseMax scenario) tends to be more 30655ffd83dbSDimitry Andric // space efficient than if tracked by Dense. The threashold is set to keep the 30665ffd83dbSDimitry Andric // worst-case memory usage within 2x of figures determined empirically for 30675ffd83dbSDimitry Andric // "all Dense" scenario in such worst-by-execution-time cases. 30685ffd83dbSDimitry Andric BitVector Sparse; 30695ffd83dbSDimitry Andric DenseSet<unsigned> Dense; 30705ffd83dbSDimitry Andric }; 30715ffd83dbSDimitry Andric 30725ffd83dbSDimitry Andric // Implements both a transfer function and a (binary, in-place) join operator 30735ffd83dbSDimitry Andric // for a dataflow over register sets with set union join and filtering transfer 30745ffd83dbSDimitry Andric // (out_b = in_b \ filter_b). filter_b is expected to be set-up ahead of time. 30755ffd83dbSDimitry Andric // Maintains out_b as its state, allowing for O(n) iteration over it at any 30765ffd83dbSDimitry Andric // time, where n is the size of the set (as opposed to O(U) where U is the 30775ffd83dbSDimitry Andric // universe). filter_b implicitly contains all physical registers at all times. 30785ffd83dbSDimitry Andric class FilteringVRegSet { 30795ffd83dbSDimitry Andric VRegFilter Filter; 3080e8d8bef9SDimitry Andric SmallVector<Register, 0> VRegs; 30815ffd83dbSDimitry Andric 30825ffd83dbSDimitry Andric public: 30835ffd83dbSDimitry Andric // Set-up the filter_b. \pre Input register set \p RS must have no duplicates. 30845ffd83dbSDimitry Andric // Both virtual and physical registers are fine. 30855ffd83dbSDimitry Andric template <typename RegSetT> void addToFilter(const RegSetT &RS) { 30865ffd83dbSDimitry Andric Filter.add(RS); 30875ffd83dbSDimitry Andric } 30885ffd83dbSDimitry Andric // Passes \p RS through the filter_b (transfer function) and adds what's left 30895ffd83dbSDimitry Andric // to itself (out_b). 30905ffd83dbSDimitry Andric template <typename RegSetT> bool add(const RegSetT &RS) { 30915ffd83dbSDimitry Andric // Double-duty the Filter: to maintain VRegs a set (and the join operation 30925ffd83dbSDimitry Andric // a set union) just add everything being added here to the Filter as well. 30935ffd83dbSDimitry Andric return Filter.filterAndAdd(RS, VRegs); 30945ffd83dbSDimitry Andric } 30955ffd83dbSDimitry Andric using const_iterator = decltype(VRegs)::const_iterator; 30965ffd83dbSDimitry Andric const_iterator begin() const { return VRegs.begin(); } 30975ffd83dbSDimitry Andric const_iterator end() const { return VRegs.end(); } 30985ffd83dbSDimitry Andric size_t size() const { return VRegs.size(); } 30995ffd83dbSDimitry Andric }; 31005ffd83dbSDimitry Andric } // namespace 31015ffd83dbSDimitry Andric 31020b57cec5SDimitry Andric // Calculate the largest possible vregsPassed sets. These are the registers that 31030b57cec5SDimitry Andric // can pass through an MBB live, but may not be live every time. It is assumed 31040b57cec5SDimitry Andric // that all vregsPassed sets are empty before the call. 31050b57cec5SDimitry Andric void MachineVerifier::calcRegsPassed() { 3106e8d8bef9SDimitry Andric if (MF->empty()) 31075ffd83dbSDimitry Andric // ReversePostOrderTraversal doesn't handle empty functions. 31085ffd83dbSDimitry Andric return; 31090b57cec5SDimitry Andric 3110e8d8bef9SDimitry Andric for (const MachineBasicBlock *MB : 3111e8d8bef9SDimitry Andric ReversePostOrderTraversal<const MachineFunction *>(MF)) { 3112e8d8bef9SDimitry Andric FilteringVRegSet VRegs; 3113e8d8bef9SDimitry Andric BBInfo &Info = MBBInfoMap[MB]; 3114e8d8bef9SDimitry Andric assert(Info.reachable); 3115e8d8bef9SDimitry Andric 3116e8d8bef9SDimitry Andric VRegs.addToFilter(Info.regsKilled); 3117e8d8bef9SDimitry Andric VRegs.addToFilter(Info.regsLiveOut); 3118e8d8bef9SDimitry Andric for (const MachineBasicBlock *Pred : MB->predecessors()) { 3119e8d8bef9SDimitry Andric const BBInfo &PredInfo = MBBInfoMap[Pred]; 3120e8d8bef9SDimitry Andric if (!PredInfo.reachable) 31210b57cec5SDimitry Andric continue; 3122e8d8bef9SDimitry Andric 3123e8d8bef9SDimitry Andric VRegs.add(PredInfo.regsLiveOut); 3124e8d8bef9SDimitry Andric VRegs.add(PredInfo.vregsPassed); 31250b57cec5SDimitry Andric } 3126e8d8bef9SDimitry Andric Info.vregsPassed.reserve(VRegs.size()); 3127e8d8bef9SDimitry Andric Info.vregsPassed.insert(VRegs.begin(), VRegs.end()); 31285ffd83dbSDimitry Andric } 31290b57cec5SDimitry Andric } 31300b57cec5SDimitry Andric 31310b57cec5SDimitry Andric // Calculate the set of virtual registers that must be passed through each basic 31320b57cec5SDimitry Andric // block in order to satisfy the requirements of successor blocks. This is very 31330b57cec5SDimitry Andric // similar to calcRegsPassed, only backwards. 31340b57cec5SDimitry Andric void MachineVerifier::calcRegsRequired() { 31350b57cec5SDimitry Andric // First push live-in regs to predecessors' vregsRequired. 31360b57cec5SDimitry Andric SmallPtrSet<const MachineBasicBlock*, 8> todo; 31370b57cec5SDimitry Andric for (const auto &MBB : *MF) { 31380b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[&MBB]; 31395ffd83dbSDimitry Andric for (const MachineBasicBlock *Pred : MBB.predecessors()) { 31405ffd83dbSDimitry Andric BBInfo &PInfo = MBBInfoMap[Pred]; 31410b57cec5SDimitry Andric if (PInfo.addRequired(MInfo.vregsLiveIn)) 31425ffd83dbSDimitry Andric todo.insert(Pred); 31430b57cec5SDimitry Andric } 3144e8d8bef9SDimitry Andric 3145e8d8bef9SDimitry Andric // Handle the PHI node. 3146e8d8bef9SDimitry Andric for (const MachineInstr &MI : MBB.phis()) { 3147e8d8bef9SDimitry Andric for (unsigned i = 1, e = MI.getNumOperands(); i != e; i += 2) { 3148e8d8bef9SDimitry Andric // Skip those Operands which are undef regs or not regs. 3149e8d8bef9SDimitry Andric if (!MI.getOperand(i).isReg() || !MI.getOperand(i).readsReg()) 3150e8d8bef9SDimitry Andric continue; 3151e8d8bef9SDimitry Andric 3152e8d8bef9SDimitry Andric // Get register and predecessor for one PHI edge. 3153e8d8bef9SDimitry Andric Register Reg = MI.getOperand(i).getReg(); 3154e8d8bef9SDimitry Andric const MachineBasicBlock *Pred = MI.getOperand(i + 1).getMBB(); 3155e8d8bef9SDimitry Andric 3156e8d8bef9SDimitry Andric BBInfo &PInfo = MBBInfoMap[Pred]; 3157e8d8bef9SDimitry Andric if (PInfo.addRequired(Reg)) 3158e8d8bef9SDimitry Andric todo.insert(Pred); 3159e8d8bef9SDimitry Andric } 3160e8d8bef9SDimitry Andric } 31610b57cec5SDimitry Andric } 31620b57cec5SDimitry Andric 31630b57cec5SDimitry Andric // Iteratively push vregsRequired to predecessors. This will converge to the 31640b57cec5SDimitry Andric // same final state regardless of DenseSet iteration order. 31650b57cec5SDimitry Andric while (!todo.empty()) { 31660b57cec5SDimitry Andric const MachineBasicBlock *MBB = *todo.begin(); 31670b57cec5SDimitry Andric todo.erase(MBB); 31680b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[MBB]; 31695ffd83dbSDimitry Andric for (const MachineBasicBlock *Pred : MBB->predecessors()) { 31705ffd83dbSDimitry Andric if (Pred == MBB) 31710b57cec5SDimitry Andric continue; 31725ffd83dbSDimitry Andric BBInfo &SInfo = MBBInfoMap[Pred]; 31730b57cec5SDimitry Andric if (SInfo.addRequired(MInfo.vregsRequired)) 31745ffd83dbSDimitry Andric todo.insert(Pred); 31750b57cec5SDimitry Andric } 31760b57cec5SDimitry Andric } 31770b57cec5SDimitry Andric } 31780b57cec5SDimitry Andric 31790b57cec5SDimitry Andric // Check PHI instructions at the beginning of MBB. It is assumed that 31800b57cec5SDimitry Andric // calcRegsPassed has been run so BBInfo::isLiveOut is valid. 31810b57cec5SDimitry Andric void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) { 31820b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[&MBB]; 31830b57cec5SDimitry Andric 31840b57cec5SDimitry Andric SmallPtrSet<const MachineBasicBlock*, 8> seen; 31850b57cec5SDimitry Andric for (const MachineInstr &Phi : MBB) { 31860b57cec5SDimitry Andric if (!Phi.isPHI()) 31870b57cec5SDimitry Andric break; 31880b57cec5SDimitry Andric seen.clear(); 31890b57cec5SDimitry Andric 31900b57cec5SDimitry Andric const MachineOperand &MODef = Phi.getOperand(0); 31910b57cec5SDimitry Andric if (!MODef.isReg() || !MODef.isDef()) { 31920b57cec5SDimitry Andric report("Expected first PHI operand to be a register def", &MODef, 0); 31930b57cec5SDimitry Andric continue; 31940b57cec5SDimitry Andric } 31950b57cec5SDimitry Andric if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() || 31960b57cec5SDimitry Andric MODef.isEarlyClobber() || MODef.isDebug()) 31970b57cec5SDimitry Andric report("Unexpected flag on PHI operand", &MODef, 0); 31988bcb0991SDimitry Andric Register DefReg = MODef.getReg(); 3199bdd1243dSDimitry Andric if (!DefReg.isVirtual()) 32000b57cec5SDimitry Andric report("Expected first PHI operand to be a virtual register", &MODef, 0); 32010b57cec5SDimitry Andric 32020b57cec5SDimitry Andric for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) { 32030b57cec5SDimitry Andric const MachineOperand &MO0 = Phi.getOperand(I); 32040b57cec5SDimitry Andric if (!MO0.isReg()) { 32050b57cec5SDimitry Andric report("Expected PHI operand to be a register", &MO0, I); 32060b57cec5SDimitry Andric continue; 32070b57cec5SDimitry Andric } 32080b57cec5SDimitry Andric if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() || 32090b57cec5SDimitry Andric MO0.isDebug() || MO0.isTied()) 32100b57cec5SDimitry Andric report("Unexpected flag on PHI operand", &MO0, I); 32110b57cec5SDimitry Andric 32120b57cec5SDimitry Andric const MachineOperand &MO1 = Phi.getOperand(I + 1); 32130b57cec5SDimitry Andric if (!MO1.isMBB()) { 32140b57cec5SDimitry Andric report("Expected PHI operand to be a basic block", &MO1, I + 1); 32150b57cec5SDimitry Andric continue; 32160b57cec5SDimitry Andric } 32170b57cec5SDimitry Andric 32180b57cec5SDimitry Andric const MachineBasicBlock &Pre = *MO1.getMBB(); 32190b57cec5SDimitry Andric if (!Pre.isSuccessor(&MBB)) { 32200b57cec5SDimitry Andric report("PHI input is not a predecessor block", &MO1, I + 1); 32210b57cec5SDimitry Andric continue; 32220b57cec5SDimitry Andric } 32230b57cec5SDimitry Andric 32240b57cec5SDimitry Andric if (MInfo.reachable) { 32250b57cec5SDimitry Andric seen.insert(&Pre); 32260b57cec5SDimitry Andric BBInfo &PrInfo = MBBInfoMap[&Pre]; 32270b57cec5SDimitry Andric if (!MO0.isUndef() && PrInfo.reachable && 32280b57cec5SDimitry Andric !PrInfo.isLiveOut(MO0.getReg())) 32290b57cec5SDimitry Andric report("PHI operand is not live-out from predecessor", &MO0, I); 32300b57cec5SDimitry Andric } 32310b57cec5SDimitry Andric } 32320b57cec5SDimitry Andric 32330b57cec5SDimitry Andric // Did we see all predecessors? 32340b57cec5SDimitry Andric if (MInfo.reachable) { 32350b57cec5SDimitry Andric for (MachineBasicBlock *Pred : MBB.predecessors()) { 32360b57cec5SDimitry Andric if (!seen.count(Pred)) { 32370b57cec5SDimitry Andric report("Missing PHI operand", &Phi); 32380b57cec5SDimitry Andric errs() << printMBBReference(*Pred) 32390b57cec5SDimitry Andric << " is a predecessor according to the CFG.\n"; 32400b57cec5SDimitry Andric } 32410b57cec5SDimitry Andric } 32420b57cec5SDimitry Andric } 32430b57cec5SDimitry Andric } 32440b57cec5SDimitry Andric } 32450b57cec5SDimitry Andric 3246*0fca6ea1SDimitry Andric static void 3247*0fca6ea1SDimitry Andric verifyConvergenceControl(const MachineFunction &MF, MachineDominatorTree &DT, 3248*0fca6ea1SDimitry Andric std::function<void(const Twine &Message)> FailureCB) { 3249*0fca6ea1SDimitry Andric MachineConvergenceVerifier CV; 3250*0fca6ea1SDimitry Andric CV.initialize(&errs(), FailureCB, MF); 3251*0fca6ea1SDimitry Andric 3252*0fca6ea1SDimitry Andric for (const auto &MBB : MF) { 3253*0fca6ea1SDimitry Andric CV.visit(MBB); 3254*0fca6ea1SDimitry Andric for (const auto &MI : MBB.instrs()) 3255*0fca6ea1SDimitry Andric CV.visit(MI); 3256*0fca6ea1SDimitry Andric } 3257*0fca6ea1SDimitry Andric 3258*0fca6ea1SDimitry Andric if (CV.sawTokens()) { 3259*0fca6ea1SDimitry Andric DT.recalculate(const_cast<MachineFunction &>(MF)); 3260*0fca6ea1SDimitry Andric CV.verify(DT); 3261*0fca6ea1SDimitry Andric } 3262*0fca6ea1SDimitry Andric } 3263*0fca6ea1SDimitry Andric 32640b57cec5SDimitry Andric void MachineVerifier::visitMachineFunctionAfter() { 3265*0fca6ea1SDimitry Andric auto FailureCB = [this](const Twine &Message) { 3266*0fca6ea1SDimitry Andric report(Message.str().c_str(), MF); 3267*0fca6ea1SDimitry Andric }; 3268*0fca6ea1SDimitry Andric verifyConvergenceControl(*MF, DT, FailureCB); 3269*0fca6ea1SDimitry Andric 32700b57cec5SDimitry Andric calcRegsPassed(); 32710b57cec5SDimitry Andric 32720b57cec5SDimitry Andric for (const MachineBasicBlock &MBB : *MF) 32730b57cec5SDimitry Andric checkPHIOps(MBB); 32740b57cec5SDimitry Andric 32750b57cec5SDimitry Andric // Now check liveness info if available 32760b57cec5SDimitry Andric calcRegsRequired(); 32770b57cec5SDimitry Andric 32780b57cec5SDimitry Andric // Check for killed virtual registers that should be live out. 32790b57cec5SDimitry Andric for (const auto &MBB : *MF) { 32800b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[&MBB]; 3281e8d8bef9SDimitry Andric for (Register VReg : MInfo.vregsRequired) 32825ffd83dbSDimitry Andric if (MInfo.regsKilled.count(VReg)) { 32830b57cec5SDimitry Andric report("Virtual register killed in block, but needed live out.", &MBB); 32845ffd83dbSDimitry Andric errs() << "Virtual register " << printReg(VReg) 32850b57cec5SDimitry Andric << " is used after the block.\n"; 32860b57cec5SDimitry Andric } 32870b57cec5SDimitry Andric } 32880b57cec5SDimitry Andric 32890b57cec5SDimitry Andric if (!MF->empty()) { 32900b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[&MF->front()]; 3291e8d8bef9SDimitry Andric for (Register VReg : MInfo.vregsRequired) { 32920b57cec5SDimitry Andric report("Virtual register defs don't dominate all uses.", MF); 32935ffd83dbSDimitry Andric report_context_vreg(VReg); 32940b57cec5SDimitry Andric } 32950b57cec5SDimitry Andric } 32960b57cec5SDimitry Andric 32970b57cec5SDimitry Andric if (LiveVars) 32980b57cec5SDimitry Andric verifyLiveVariables(); 32990b57cec5SDimitry Andric if (LiveInts) 33000b57cec5SDimitry Andric verifyLiveIntervals(); 33010b57cec5SDimitry Andric 3302480093f4SDimitry Andric // Check live-in list of each MBB. If a register is live into MBB, check 3303480093f4SDimitry Andric // that the register is in regsLiveOut of each predecessor block. Since 3304480093f4SDimitry Andric // this must come from a definition in the predecesssor or its live-in 3305480093f4SDimitry Andric // list, this will catch a live-through case where the predecessor does not 3306480093f4SDimitry Andric // have the register in its live-in list. This currently only checks 3307480093f4SDimitry Andric // registers that have no aliases, are not allocatable and are not 3308480093f4SDimitry Andric // reserved, which could mean a condition code register for instance. 3309480093f4SDimitry Andric if (MRI->tracksLiveness()) 3310480093f4SDimitry Andric for (const auto &MBB : *MF) 3311480093f4SDimitry Andric for (MachineBasicBlock::RegisterMaskPair P : MBB.liveins()) { 3312480093f4SDimitry Andric MCPhysReg LiveInReg = P.PhysReg; 3313480093f4SDimitry Andric bool hasAliases = MCRegAliasIterator(LiveInReg, TRI, false).isValid(); 3314480093f4SDimitry Andric if (hasAliases || isAllocatable(LiveInReg) || isReserved(LiveInReg)) 3315480093f4SDimitry Andric continue; 3316480093f4SDimitry Andric for (const MachineBasicBlock *Pred : MBB.predecessors()) { 3317480093f4SDimitry Andric BBInfo &PInfo = MBBInfoMap[Pred]; 3318480093f4SDimitry Andric if (!PInfo.regsLiveOut.count(LiveInReg)) { 3319480093f4SDimitry Andric report("Live in register not found to be live out from predecessor.", 3320480093f4SDimitry Andric &MBB); 3321480093f4SDimitry Andric errs() << TRI->getName(LiveInReg) 3322480093f4SDimitry Andric << " not found to be live out from " 3323480093f4SDimitry Andric << printMBBReference(*Pred) << "\n"; 3324480093f4SDimitry Andric } 3325480093f4SDimitry Andric } 3326480093f4SDimitry Andric } 3327480093f4SDimitry Andric 33280b57cec5SDimitry Andric for (auto CSInfo : MF->getCallSitesInfo()) 33290b57cec5SDimitry Andric if (!CSInfo.first->isCall()) 33300b57cec5SDimitry Andric report("Call site info referencing instruction that is not call", MF); 3331e8d8bef9SDimitry Andric 3332e8d8bef9SDimitry Andric // If there's debug-info, check that we don't have any duplicate value 3333e8d8bef9SDimitry Andric // tracking numbers. 3334e8d8bef9SDimitry Andric if (MF->getFunction().getSubprogram()) { 3335e8d8bef9SDimitry Andric DenseSet<unsigned> SeenNumbers; 3336fcaf7f86SDimitry Andric for (const auto &MBB : *MF) { 3337fcaf7f86SDimitry Andric for (const auto &MI : MBB) { 3338e8d8bef9SDimitry Andric if (auto Num = MI.peekDebugInstrNum()) { 3339e8d8bef9SDimitry Andric auto Result = SeenNumbers.insert((unsigned)Num); 3340e8d8bef9SDimitry Andric if (!Result.second) 3341e8d8bef9SDimitry Andric report("Instruction has a duplicated value tracking number", &MI); 3342e8d8bef9SDimitry Andric } 3343e8d8bef9SDimitry Andric } 3344e8d8bef9SDimitry Andric } 3345e8d8bef9SDimitry Andric } 33460b57cec5SDimitry Andric } 33470b57cec5SDimitry Andric 33480b57cec5SDimitry Andric void MachineVerifier::verifyLiveVariables() { 33490b57cec5SDimitry Andric assert(LiveVars && "Don't call verifyLiveVariables without LiveVars"); 3350e8d8bef9SDimitry Andric for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) { 3351e8d8bef9SDimitry Andric Register Reg = Register::index2VirtReg(I); 33520b57cec5SDimitry Andric LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); 33530b57cec5SDimitry Andric for (const auto &MBB : *MF) { 33540b57cec5SDimitry Andric BBInfo &MInfo = MBBInfoMap[&MBB]; 33550b57cec5SDimitry Andric 33560b57cec5SDimitry Andric // Our vregsRequired should be identical to LiveVariables' AliveBlocks 33570b57cec5SDimitry Andric if (MInfo.vregsRequired.count(Reg)) { 33580b57cec5SDimitry Andric if (!VI.AliveBlocks.test(MBB.getNumber())) { 33590b57cec5SDimitry Andric report("LiveVariables: Block missing from AliveBlocks", &MBB); 33600b57cec5SDimitry Andric errs() << "Virtual register " << printReg(Reg) 33610b57cec5SDimitry Andric << " must be live through the block.\n"; 33620b57cec5SDimitry Andric } 33630b57cec5SDimitry Andric } else { 33640b57cec5SDimitry Andric if (VI.AliveBlocks.test(MBB.getNumber())) { 33650b57cec5SDimitry Andric report("LiveVariables: Block should not be in AliveBlocks", &MBB); 33660b57cec5SDimitry Andric errs() << "Virtual register " << printReg(Reg) 33670b57cec5SDimitry Andric << " is not needed live through the block.\n"; 33680b57cec5SDimitry Andric } 33690b57cec5SDimitry Andric } 33700b57cec5SDimitry Andric } 33710b57cec5SDimitry Andric } 33720b57cec5SDimitry Andric } 33730b57cec5SDimitry Andric 33740b57cec5SDimitry Andric void MachineVerifier::verifyLiveIntervals() { 33750b57cec5SDimitry Andric assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts"); 3376e8d8bef9SDimitry Andric for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) { 3377e8d8bef9SDimitry Andric Register Reg = Register::index2VirtReg(I); 33780b57cec5SDimitry Andric 33790b57cec5SDimitry Andric // Spilling and splitting may leave unused registers around. Skip them. 33800b57cec5SDimitry Andric if (MRI->reg_nodbg_empty(Reg)) 33810b57cec5SDimitry Andric continue; 33820b57cec5SDimitry Andric 33830b57cec5SDimitry Andric if (!LiveInts->hasInterval(Reg)) { 33840b57cec5SDimitry Andric report("Missing live interval for virtual register", MF); 33850b57cec5SDimitry Andric errs() << printReg(Reg, TRI) << " still has defs or uses\n"; 33860b57cec5SDimitry Andric continue; 33870b57cec5SDimitry Andric } 33880b57cec5SDimitry Andric 33890b57cec5SDimitry Andric const LiveInterval &LI = LiveInts->getInterval(Reg); 3390e8d8bef9SDimitry Andric assert(Reg == LI.reg() && "Invalid reg to interval mapping"); 33910b57cec5SDimitry Andric verifyLiveInterval(LI); 33920b57cec5SDimitry Andric } 33930b57cec5SDimitry Andric 33940b57cec5SDimitry Andric // Verify all the cached regunit intervals. 33950b57cec5SDimitry Andric for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) 33960b57cec5SDimitry Andric if (const LiveRange *LR = LiveInts->getCachedRegUnit(i)) 33970b57cec5SDimitry Andric verifyLiveRange(*LR, i); 33980b57cec5SDimitry Andric } 33990b57cec5SDimitry Andric 34000b57cec5SDimitry Andric void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR, 3401e8d8bef9SDimitry Andric const VNInfo *VNI, Register Reg, 34020b57cec5SDimitry Andric LaneBitmask LaneMask) { 34030b57cec5SDimitry Andric if (VNI->isUnused()) 34040b57cec5SDimitry Andric return; 34050b57cec5SDimitry Andric 34060b57cec5SDimitry Andric const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def); 34070b57cec5SDimitry Andric 34080b57cec5SDimitry Andric if (!DefVNI) { 34090b57cec5SDimitry Andric report("Value not live at VNInfo def and not marked unused", MF); 34100b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34110b57cec5SDimitry Andric report_context(*VNI); 34120b57cec5SDimitry Andric return; 34130b57cec5SDimitry Andric } 34140b57cec5SDimitry Andric 34150b57cec5SDimitry Andric if (DefVNI != VNI) { 34160b57cec5SDimitry Andric report("Live segment at def has different VNInfo", MF); 34170b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34180b57cec5SDimitry Andric report_context(*VNI); 34190b57cec5SDimitry Andric return; 34200b57cec5SDimitry Andric } 34210b57cec5SDimitry Andric 34220b57cec5SDimitry Andric const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def); 34230b57cec5SDimitry Andric if (!MBB) { 34240b57cec5SDimitry Andric report("Invalid VNInfo definition index", MF); 34250b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34260b57cec5SDimitry Andric report_context(*VNI); 34270b57cec5SDimitry Andric return; 34280b57cec5SDimitry Andric } 34290b57cec5SDimitry Andric 34300b57cec5SDimitry Andric if (VNI->isPHIDef()) { 34310b57cec5SDimitry Andric if (VNI->def != LiveInts->getMBBStartIdx(MBB)) { 34320b57cec5SDimitry Andric report("PHIDef VNInfo is not defined at MBB start", MBB); 34330b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34340b57cec5SDimitry Andric report_context(*VNI); 34350b57cec5SDimitry Andric } 34360b57cec5SDimitry Andric return; 34370b57cec5SDimitry Andric } 34380b57cec5SDimitry Andric 34390b57cec5SDimitry Andric // Non-PHI def. 34400b57cec5SDimitry Andric const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); 34410b57cec5SDimitry Andric if (!MI) { 34420b57cec5SDimitry Andric report("No instruction at VNInfo def index", MBB); 34430b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34440b57cec5SDimitry Andric report_context(*VNI); 34450b57cec5SDimitry Andric return; 34460b57cec5SDimitry Andric } 34470b57cec5SDimitry Andric 34480b57cec5SDimitry Andric if (Reg != 0) { 34490b57cec5SDimitry Andric bool hasDef = false; 34500b57cec5SDimitry Andric bool isEarlyClobber = false; 34510b57cec5SDimitry Andric for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) { 34520b57cec5SDimitry Andric if (!MOI->isReg() || !MOI->isDef()) 34530b57cec5SDimitry Andric continue; 3454bdd1243dSDimitry Andric if (Reg.isVirtual()) { 34550b57cec5SDimitry Andric if (MOI->getReg() != Reg) 34560b57cec5SDimitry Andric continue; 34570b57cec5SDimitry Andric } else { 3458bdd1243dSDimitry Andric if (!MOI->getReg().isPhysical() || !TRI->hasRegUnit(MOI->getReg(), Reg)) 34590b57cec5SDimitry Andric continue; 34600b57cec5SDimitry Andric } 34610b57cec5SDimitry Andric if (LaneMask.any() && 34620b57cec5SDimitry Andric (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none()) 34630b57cec5SDimitry Andric continue; 34640b57cec5SDimitry Andric hasDef = true; 34650b57cec5SDimitry Andric if (MOI->isEarlyClobber()) 34660b57cec5SDimitry Andric isEarlyClobber = true; 34670b57cec5SDimitry Andric } 34680b57cec5SDimitry Andric 34690b57cec5SDimitry Andric if (!hasDef) { 34700b57cec5SDimitry Andric report("Defining instruction does not modify register", MI); 34710b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34720b57cec5SDimitry Andric report_context(*VNI); 34730b57cec5SDimitry Andric } 34740b57cec5SDimitry Andric 34750b57cec5SDimitry Andric // Early clobber defs begin at USE slots, but other defs must begin at 34760b57cec5SDimitry Andric // DEF slots. 34770b57cec5SDimitry Andric if (isEarlyClobber) { 34780b57cec5SDimitry Andric if (!VNI->def.isEarlyClobber()) { 34790b57cec5SDimitry Andric report("Early clobber def must be at an early-clobber slot", MBB); 34800b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34810b57cec5SDimitry Andric report_context(*VNI); 34820b57cec5SDimitry Andric } 34830b57cec5SDimitry Andric } else if (!VNI->def.isRegister()) { 34840b57cec5SDimitry Andric report("Non-PHI, non-early clobber def must be at a register slot", MBB); 34850b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 34860b57cec5SDimitry Andric report_context(*VNI); 34870b57cec5SDimitry Andric } 34880b57cec5SDimitry Andric } 34890b57cec5SDimitry Andric } 34900b57cec5SDimitry Andric 34910b57cec5SDimitry Andric void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR, 34920b57cec5SDimitry Andric const LiveRange::const_iterator I, 3493e8d8bef9SDimitry Andric Register Reg, 3494e8d8bef9SDimitry Andric LaneBitmask LaneMask) { 34950b57cec5SDimitry Andric const LiveRange::Segment &S = *I; 34960b57cec5SDimitry Andric const VNInfo *VNI = S.valno; 34970b57cec5SDimitry Andric assert(VNI && "Live segment has no valno"); 34980b57cec5SDimitry Andric 34990b57cec5SDimitry Andric if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) { 35000b57cec5SDimitry Andric report("Foreign valno in live segment", MF); 35010b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35020b57cec5SDimitry Andric report_context(S); 35030b57cec5SDimitry Andric report_context(*VNI); 35040b57cec5SDimitry Andric } 35050b57cec5SDimitry Andric 35060b57cec5SDimitry Andric if (VNI->isUnused()) { 35070b57cec5SDimitry Andric report("Live segment valno is marked unused", MF); 35080b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35090b57cec5SDimitry Andric report_context(S); 35100b57cec5SDimitry Andric } 35110b57cec5SDimitry Andric 35120b57cec5SDimitry Andric const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start); 35130b57cec5SDimitry Andric if (!MBB) { 35140b57cec5SDimitry Andric report("Bad start of live segment, no basic block", MF); 35150b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35160b57cec5SDimitry Andric report_context(S); 35170b57cec5SDimitry Andric return; 35180b57cec5SDimitry Andric } 35190b57cec5SDimitry Andric SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB); 35200b57cec5SDimitry Andric if (S.start != MBBStartIdx && S.start != VNI->def) { 35210b57cec5SDimitry Andric report("Live segment must begin at MBB entry or valno def", MBB); 35220b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35230b57cec5SDimitry Andric report_context(S); 35240b57cec5SDimitry Andric } 35250b57cec5SDimitry Andric 35260b57cec5SDimitry Andric const MachineBasicBlock *EndMBB = 35270b57cec5SDimitry Andric LiveInts->getMBBFromIndex(S.end.getPrevSlot()); 35280b57cec5SDimitry Andric if (!EndMBB) { 35290b57cec5SDimitry Andric report("Bad end of live segment, no basic block", MF); 35300b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35310b57cec5SDimitry Andric report_context(S); 35320b57cec5SDimitry Andric return; 35330b57cec5SDimitry Andric } 35340b57cec5SDimitry Andric 353506c3fb27SDimitry Andric // Checks for non-live-out segments. 353606c3fb27SDimitry Andric if (S.end != LiveInts->getMBBEndIdx(EndMBB)) { 35370b57cec5SDimitry Andric // RegUnit intervals are allowed dead phis. 3538bdd1243dSDimitry Andric if (!Reg.isVirtual() && VNI->isPHIDef() && S.start == VNI->def && 3539bdd1243dSDimitry Andric S.end == VNI->def.getDeadSlot()) 35400b57cec5SDimitry Andric return; 35410b57cec5SDimitry Andric 35420b57cec5SDimitry Andric // The live segment is ending inside EndMBB 35430b57cec5SDimitry Andric const MachineInstr *MI = 35440b57cec5SDimitry Andric LiveInts->getInstructionFromIndex(S.end.getPrevSlot()); 35450b57cec5SDimitry Andric if (!MI) { 35460b57cec5SDimitry Andric report("Live segment doesn't end at a valid instruction", EndMBB); 35470b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35480b57cec5SDimitry Andric report_context(S); 35490b57cec5SDimitry Andric return; 35500b57cec5SDimitry Andric } 35510b57cec5SDimitry Andric 35520b57cec5SDimitry Andric // The block slot must refer to a basic block boundary. 35530b57cec5SDimitry Andric if (S.end.isBlock()) { 35540b57cec5SDimitry Andric report("Live segment ends at B slot of an instruction", EndMBB); 35550b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35560b57cec5SDimitry Andric report_context(S); 35570b57cec5SDimitry Andric } 35580b57cec5SDimitry Andric 35590b57cec5SDimitry Andric if (S.end.isDead()) { 35600b57cec5SDimitry Andric // Segment ends on the dead slot. 35610b57cec5SDimitry Andric // That means there must be a dead def. 35620b57cec5SDimitry Andric if (!SlotIndex::isSameInstr(S.start, S.end)) { 35630b57cec5SDimitry Andric report("Live segment ending at dead slot spans instructions", EndMBB); 35640b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35650b57cec5SDimitry Andric report_context(S); 35660b57cec5SDimitry Andric } 35670b57cec5SDimitry Andric } 35680b57cec5SDimitry Andric 3569349cc55cSDimitry Andric // After tied operands are rewritten, a live segment can only end at an 3570349cc55cSDimitry Andric // early-clobber slot if it is being redefined by an early-clobber def. 357106c3fb27SDimitry Andric // TODO: Before tied operands are rewritten, a live segment can only end at 357206c3fb27SDimitry Andric // an early-clobber slot if the last use is tied to an early-clobber def. 3573349cc55cSDimitry Andric if (MF->getProperties().hasProperty( 3574349cc55cSDimitry Andric MachineFunctionProperties::Property::TiedOpsRewritten) && 3575349cc55cSDimitry Andric S.end.isEarlyClobber()) { 35760b57cec5SDimitry Andric if (I + 1 == LR.end() || (I + 1)->start != S.end) { 35770b57cec5SDimitry Andric report("Live segment ending at early clobber slot must be " 357806c3fb27SDimitry Andric "redefined by an EC def in the same instruction", 357906c3fb27SDimitry Andric EndMBB); 35800b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 35810b57cec5SDimitry Andric report_context(S); 35820b57cec5SDimitry Andric } 35830b57cec5SDimitry Andric } 35840b57cec5SDimitry Andric 35850b57cec5SDimitry Andric // The following checks only apply to virtual registers. Physreg liveness 35860b57cec5SDimitry Andric // is too weird to check. 3587bdd1243dSDimitry Andric if (Reg.isVirtual()) { 35880b57cec5SDimitry Andric // A live segment can end with either a redefinition, a kill flag on a 35890b57cec5SDimitry Andric // use, or a dead flag on a def. 35900b57cec5SDimitry Andric bool hasRead = false; 35910b57cec5SDimitry Andric bool hasSubRegDef = false; 35920b57cec5SDimitry Andric bool hasDeadDef = false; 35930b57cec5SDimitry Andric for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) { 35940b57cec5SDimitry Andric if (!MOI->isReg() || MOI->getReg() != Reg) 35950b57cec5SDimitry Andric continue; 35960b57cec5SDimitry Andric unsigned Sub = MOI->getSubReg(); 359706c3fb27SDimitry Andric LaneBitmask SLM = 359806c3fb27SDimitry Andric Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub) : LaneBitmask::getAll(); 35990b57cec5SDimitry Andric if (MOI->isDef()) { 36000b57cec5SDimitry Andric if (Sub != 0) { 36010b57cec5SDimitry Andric hasSubRegDef = true; 36020b57cec5SDimitry Andric // An operand %0:sub0 reads %0:sub1..n. Invert the lane 36030b57cec5SDimitry Andric // mask for subregister defs. Read-undef defs will be handled by 36040b57cec5SDimitry Andric // readsReg below. 36050b57cec5SDimitry Andric SLM = ~SLM; 36060b57cec5SDimitry Andric } 36070b57cec5SDimitry Andric if (MOI->isDead()) 36080b57cec5SDimitry Andric hasDeadDef = true; 36090b57cec5SDimitry Andric } 36100b57cec5SDimitry Andric if (LaneMask.any() && (LaneMask & SLM).none()) 36110b57cec5SDimitry Andric continue; 36120b57cec5SDimitry Andric if (MOI->readsReg()) 36130b57cec5SDimitry Andric hasRead = true; 36140b57cec5SDimitry Andric } 36150b57cec5SDimitry Andric if (S.end.isDead()) { 36160b57cec5SDimitry Andric // Make sure that the corresponding machine operand for a "dead" live 36170b57cec5SDimitry Andric // range has the dead flag. We cannot perform this check for subregister 36180b57cec5SDimitry Andric // liveranges as partially dead values are allowed. 36190b57cec5SDimitry Andric if (LaneMask.none() && !hasDeadDef) { 362006c3fb27SDimitry Andric report( 362106c3fb27SDimitry Andric "Instruction ending live segment on dead slot has no dead flag", 36220b57cec5SDimitry Andric MI); 36230b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 36240b57cec5SDimitry Andric report_context(S); 36250b57cec5SDimitry Andric } 36260b57cec5SDimitry Andric } else { 36270b57cec5SDimitry Andric if (!hasRead) { 36280b57cec5SDimitry Andric // When tracking subregister liveness, the main range must start new 36290b57cec5SDimitry Andric // values on partial register writes, even if there is no read. 36300b57cec5SDimitry Andric if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() || 36310b57cec5SDimitry Andric !hasSubRegDef) { 36320b57cec5SDimitry Andric report("Instruction ending live segment doesn't read the register", 36330b57cec5SDimitry Andric MI); 36340b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 36350b57cec5SDimitry Andric report_context(S); 36360b57cec5SDimitry Andric } 36370b57cec5SDimitry Andric } 36380b57cec5SDimitry Andric } 36390b57cec5SDimitry Andric } 364006c3fb27SDimitry Andric } 36410b57cec5SDimitry Andric 36420b57cec5SDimitry Andric // Now check all the basic blocks in this live segment. 36430b57cec5SDimitry Andric MachineFunction::const_iterator MFI = MBB->getIterator(); 36440b57cec5SDimitry Andric // Is this live segment the beginning of a non-PHIDef VN? 36450b57cec5SDimitry Andric if (S.start == VNI->def && !VNI->isPHIDef()) { 36460b57cec5SDimitry Andric // Not live-in to any blocks. 36470b57cec5SDimitry Andric if (MBB == EndMBB) 36480b57cec5SDimitry Andric return; 36490b57cec5SDimitry Andric // Skip this block. 36500b57cec5SDimitry Andric ++MFI; 36510b57cec5SDimitry Andric } 36520b57cec5SDimitry Andric 36530b57cec5SDimitry Andric SmallVector<SlotIndex, 4> Undefs; 36540b57cec5SDimitry Andric if (LaneMask.any()) { 36550b57cec5SDimitry Andric LiveInterval &OwnerLI = LiveInts->getInterval(Reg); 36560b57cec5SDimitry Andric OwnerLI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes); 36570b57cec5SDimitry Andric } 36580b57cec5SDimitry Andric 36590b57cec5SDimitry Andric while (true) { 36600b57cec5SDimitry Andric assert(LiveInts->isLiveInToMBB(LR, &*MFI)); 36610b57cec5SDimitry Andric // We don't know how to track physregs into a landing pad. 3662bdd1243dSDimitry Andric if (!Reg.isVirtual() && MFI->isEHPad()) { 36630b57cec5SDimitry Andric if (&*MFI == EndMBB) 36640b57cec5SDimitry Andric break; 36650b57cec5SDimitry Andric ++MFI; 36660b57cec5SDimitry Andric continue; 36670b57cec5SDimitry Andric } 36680b57cec5SDimitry Andric 36690b57cec5SDimitry Andric // Is VNI a PHI-def in the current block? 36700b57cec5SDimitry Andric bool IsPHI = VNI->isPHIDef() && 36710b57cec5SDimitry Andric VNI->def == LiveInts->getMBBStartIdx(&*MFI); 36720b57cec5SDimitry Andric 36730b57cec5SDimitry Andric // Check that VNI is live-out of all predecessors. 36745ffd83dbSDimitry Andric for (const MachineBasicBlock *Pred : MFI->predecessors()) { 36755ffd83dbSDimitry Andric SlotIndex PEnd = LiveInts->getMBBEndIdx(Pred); 3676fe6060f1SDimitry Andric // Predecessor of landing pad live-out on last call. 3677fe6060f1SDimitry Andric if (MFI->isEHPad()) { 36784824e7fdSDimitry Andric for (const MachineInstr &MI : llvm::reverse(*Pred)) { 36794824e7fdSDimitry Andric if (MI.isCall()) { 36804824e7fdSDimitry Andric PEnd = Indexes->getInstructionIndex(MI).getBoundaryIndex(); 3681fe6060f1SDimitry Andric break; 3682fe6060f1SDimitry Andric } 3683fe6060f1SDimitry Andric } 3684fe6060f1SDimitry Andric } 36850b57cec5SDimitry Andric const VNInfo *PVNI = LR.getVNInfoBefore(PEnd); 36860b57cec5SDimitry Andric 36870b57cec5SDimitry Andric // All predecessors must have a live-out value. However for a phi 36880b57cec5SDimitry Andric // instruction with subregister intervals 36890b57cec5SDimitry Andric // only one of the subregisters (not necessarily the current one) needs to 36900b57cec5SDimitry Andric // be defined. 36910b57cec5SDimitry Andric if (!PVNI && (LaneMask.none() || !IsPHI)) { 36925ffd83dbSDimitry Andric if (LiveRangeCalc::isJointlyDominated(Pred, Undefs, *Indexes)) 36930b57cec5SDimitry Andric continue; 36945ffd83dbSDimitry Andric report("Register not marked live out of predecessor", Pred); 36950b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 36960b57cec5SDimitry Andric report_context(*VNI); 36970b57cec5SDimitry Andric errs() << " live into " << printMBBReference(*MFI) << '@' 36980b57cec5SDimitry Andric << LiveInts->getMBBStartIdx(&*MFI) << ", not live before " 36990b57cec5SDimitry Andric << PEnd << '\n'; 37000b57cec5SDimitry Andric continue; 37010b57cec5SDimitry Andric } 37020b57cec5SDimitry Andric 37030b57cec5SDimitry Andric // Only PHI-defs can take different predecessor values. 37040b57cec5SDimitry Andric if (!IsPHI && PVNI != VNI) { 37055ffd83dbSDimitry Andric report("Different value live out of predecessor", Pred); 37060b57cec5SDimitry Andric report_context(LR, Reg, LaneMask); 37070b57cec5SDimitry Andric errs() << "Valno #" << PVNI->id << " live out of " 37085ffd83dbSDimitry Andric << printMBBReference(*Pred) << '@' << PEnd << "\nValno #" 37090b57cec5SDimitry Andric << VNI->id << " live into " << printMBBReference(*MFI) << '@' 37100b57cec5SDimitry Andric << LiveInts->getMBBStartIdx(&*MFI) << '\n'; 37110b57cec5SDimitry Andric } 37120b57cec5SDimitry Andric } 37130b57cec5SDimitry Andric if (&*MFI == EndMBB) 37140b57cec5SDimitry Andric break; 37150b57cec5SDimitry Andric ++MFI; 37160b57cec5SDimitry Andric } 37170b57cec5SDimitry Andric } 37180b57cec5SDimitry Andric 3719e8d8bef9SDimitry Andric void MachineVerifier::verifyLiveRange(const LiveRange &LR, Register Reg, 37200b57cec5SDimitry Andric LaneBitmask LaneMask) { 37210b57cec5SDimitry Andric for (const VNInfo *VNI : LR.valnos) 37220b57cec5SDimitry Andric verifyLiveRangeValue(LR, VNI, Reg, LaneMask); 37230b57cec5SDimitry Andric 37240b57cec5SDimitry Andric for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I) 37250b57cec5SDimitry Andric verifyLiveRangeSegment(LR, I, Reg, LaneMask); 37260b57cec5SDimitry Andric } 37270b57cec5SDimitry Andric 37280b57cec5SDimitry Andric void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) { 3729e8d8bef9SDimitry Andric Register Reg = LI.reg(); 3730bdd1243dSDimitry Andric assert(Reg.isVirtual()); 37310b57cec5SDimitry Andric verifyLiveRange(LI, Reg); 37320b57cec5SDimitry Andric 37335f757f3fSDimitry Andric if (LI.hasSubRanges()) { 37340b57cec5SDimitry Andric LaneBitmask Mask; 37350b57cec5SDimitry Andric LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg); 37360b57cec5SDimitry Andric for (const LiveInterval::SubRange &SR : LI.subranges()) { 37370b57cec5SDimitry Andric if ((Mask & SR.LaneMask).any()) { 37380b57cec5SDimitry Andric report("Lane masks of sub ranges overlap in live interval", MF); 37390b57cec5SDimitry Andric report_context(LI); 37400b57cec5SDimitry Andric } 37410b57cec5SDimitry Andric if ((SR.LaneMask & ~MaxMask).any()) { 37420b57cec5SDimitry Andric report("Subrange lanemask is invalid", MF); 37430b57cec5SDimitry Andric report_context(LI); 37440b57cec5SDimitry Andric } 37450b57cec5SDimitry Andric if (SR.empty()) { 37460b57cec5SDimitry Andric report("Subrange must not be empty", MF); 3747e8d8bef9SDimitry Andric report_context(SR, LI.reg(), SR.LaneMask); 37480b57cec5SDimitry Andric } 37490b57cec5SDimitry Andric Mask |= SR.LaneMask; 3750e8d8bef9SDimitry Andric verifyLiveRange(SR, LI.reg(), SR.LaneMask); 37510b57cec5SDimitry Andric if (!LI.covers(SR)) { 37520b57cec5SDimitry Andric report("A Subrange is not covered by the main range", MF); 37530b57cec5SDimitry Andric report_context(LI); 37540b57cec5SDimitry Andric } 37550b57cec5SDimitry Andric } 37565f757f3fSDimitry Andric } 37570b57cec5SDimitry Andric 37580b57cec5SDimitry Andric // Check the LI only has one connected component. 37590b57cec5SDimitry Andric ConnectedVNInfoEqClasses ConEQ(*LiveInts); 37600b57cec5SDimitry Andric unsigned NumComp = ConEQ.Classify(LI); 37610b57cec5SDimitry Andric if (NumComp > 1) { 37620b57cec5SDimitry Andric report("Multiple connected components in live interval", MF); 37630b57cec5SDimitry Andric report_context(LI); 37640b57cec5SDimitry Andric for (unsigned comp = 0; comp != NumComp; ++comp) { 37650b57cec5SDimitry Andric errs() << comp << ": valnos"; 37665ffd83dbSDimitry Andric for (const VNInfo *I : LI.valnos) 37675ffd83dbSDimitry Andric if (comp == ConEQ.getEqClass(I)) 37685ffd83dbSDimitry Andric errs() << ' ' << I->id; 37690b57cec5SDimitry Andric errs() << '\n'; 37700b57cec5SDimitry Andric } 37710b57cec5SDimitry Andric } 37720b57cec5SDimitry Andric } 37730b57cec5SDimitry Andric 37740b57cec5SDimitry Andric namespace { 37750b57cec5SDimitry Andric 37760b57cec5SDimitry Andric // FrameSetup and FrameDestroy can have zero adjustment, so using a single 37770b57cec5SDimitry Andric // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the 37780b57cec5SDimitry Andric // value is zero. 37790b57cec5SDimitry Andric // We use a bool plus an integer to capture the stack state. 37800b57cec5SDimitry Andric struct StackStateOfBB { 37810b57cec5SDimitry Andric StackStateOfBB() = default; 37820b57cec5SDimitry Andric StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) : 37830b57cec5SDimitry Andric EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup), 37840b57cec5SDimitry Andric ExitIsSetup(ExitSetup) {} 37850b57cec5SDimitry Andric 37860b57cec5SDimitry Andric // Can be negative, which means we are setting up a frame. 37870b57cec5SDimitry Andric int EntryValue = 0; 37880b57cec5SDimitry Andric int ExitValue = 0; 37890b57cec5SDimitry Andric bool EntryIsSetup = false; 37900b57cec5SDimitry Andric bool ExitIsSetup = false; 37910b57cec5SDimitry Andric }; 37920b57cec5SDimitry Andric 37930b57cec5SDimitry Andric } // end anonymous namespace 37940b57cec5SDimitry Andric 37950b57cec5SDimitry Andric /// Make sure on every path through the CFG, a FrameSetup <n> is always followed 37960b57cec5SDimitry Andric /// by a FrameDestroy <n>, stack adjustments are identical on all 37970b57cec5SDimitry Andric /// CFG edges to a merge point, and frame is destroyed at end of a return block. 37980b57cec5SDimitry Andric void MachineVerifier::verifyStackFrame() { 37990b57cec5SDimitry Andric unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode(); 38000b57cec5SDimitry Andric unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode(); 38010b57cec5SDimitry Andric if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u) 38020b57cec5SDimitry Andric return; 38030b57cec5SDimitry Andric 38040b57cec5SDimitry Andric SmallVector<StackStateOfBB, 8> SPState; 38050b57cec5SDimitry Andric SPState.resize(MF->getNumBlockIDs()); 38060b57cec5SDimitry Andric df_iterator_default_set<const MachineBasicBlock*> Reachable; 38070b57cec5SDimitry Andric 38080b57cec5SDimitry Andric // Visit the MBBs in DFS order. 38090b57cec5SDimitry Andric for (df_ext_iterator<const MachineFunction *, 38100b57cec5SDimitry Andric df_iterator_default_set<const MachineBasicBlock *>> 38110b57cec5SDimitry Andric DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable); 38120b57cec5SDimitry Andric DFI != DFE; ++DFI) { 38130b57cec5SDimitry Andric const MachineBasicBlock *MBB = *DFI; 38140b57cec5SDimitry Andric 38150b57cec5SDimitry Andric StackStateOfBB BBState; 38160b57cec5SDimitry Andric // Check the exit state of the DFS stack predecessor. 38170b57cec5SDimitry Andric if (DFI.getPathLength() >= 2) { 38180b57cec5SDimitry Andric const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2); 38190b57cec5SDimitry Andric assert(Reachable.count(StackPred) && 38200b57cec5SDimitry Andric "DFS stack predecessor is already visited.\n"); 38210b57cec5SDimitry Andric BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue; 38220b57cec5SDimitry Andric BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup; 38230b57cec5SDimitry Andric BBState.ExitValue = BBState.EntryValue; 38240b57cec5SDimitry Andric BBState.ExitIsSetup = BBState.EntryIsSetup; 38250b57cec5SDimitry Andric } 38260b57cec5SDimitry Andric 38275f757f3fSDimitry Andric if ((int)MBB->getCallFrameSize() != -BBState.EntryValue) { 38285f757f3fSDimitry Andric report("Call frame size on entry does not match value computed from " 38295f757f3fSDimitry Andric "predecessor", 38305f757f3fSDimitry Andric MBB); 38315f757f3fSDimitry Andric errs() << "Call frame size on entry " << MBB->getCallFrameSize() 38325f757f3fSDimitry Andric << " does not match value computed from predecessor " 38335f757f3fSDimitry Andric << -BBState.EntryValue << '\n'; 38345f757f3fSDimitry Andric } 38355f757f3fSDimitry Andric 38360b57cec5SDimitry Andric // Update stack state by checking contents of MBB. 38370b57cec5SDimitry Andric for (const auto &I : *MBB) { 38380b57cec5SDimitry Andric if (I.getOpcode() == FrameSetupOpcode) { 38390b57cec5SDimitry Andric if (BBState.ExitIsSetup) 38400b57cec5SDimitry Andric report("FrameSetup is after another FrameSetup", &I); 3841*0fca6ea1SDimitry Andric if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack()) 3842*0fca6ea1SDimitry Andric report("AdjustsStack not set in presence of a frame pseudo " 3843*0fca6ea1SDimitry Andric "instruction.", &I); 38440b57cec5SDimitry Andric BBState.ExitValue -= TII->getFrameTotalSize(I); 38450b57cec5SDimitry Andric BBState.ExitIsSetup = true; 38460b57cec5SDimitry Andric } 38470b57cec5SDimitry Andric 38480b57cec5SDimitry Andric if (I.getOpcode() == FrameDestroyOpcode) { 38490b57cec5SDimitry Andric int Size = TII->getFrameTotalSize(I); 38500b57cec5SDimitry Andric if (!BBState.ExitIsSetup) 38510b57cec5SDimitry Andric report("FrameDestroy is not after a FrameSetup", &I); 38520b57cec5SDimitry Andric int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue : 38530b57cec5SDimitry Andric BBState.ExitValue; 38540b57cec5SDimitry Andric if (BBState.ExitIsSetup && AbsSPAdj != Size) { 38550b57cec5SDimitry Andric report("FrameDestroy <n> is after FrameSetup <m>", &I); 38560b57cec5SDimitry Andric errs() << "FrameDestroy <" << Size << "> is after FrameSetup <" 38570b57cec5SDimitry Andric << AbsSPAdj << ">.\n"; 38580b57cec5SDimitry Andric } 3859*0fca6ea1SDimitry Andric if (!MRI->isSSA() && !MF->getFrameInfo().adjustsStack()) 3860*0fca6ea1SDimitry Andric report("AdjustsStack not set in presence of a frame pseudo " 3861*0fca6ea1SDimitry Andric "instruction.", &I); 38620b57cec5SDimitry Andric BBState.ExitValue += Size; 38630b57cec5SDimitry Andric BBState.ExitIsSetup = false; 38640b57cec5SDimitry Andric } 38650b57cec5SDimitry Andric } 38660b57cec5SDimitry Andric SPState[MBB->getNumber()] = BBState; 38670b57cec5SDimitry Andric 38680b57cec5SDimitry Andric // Make sure the exit state of any predecessor is consistent with the entry 38690b57cec5SDimitry Andric // state. 38705ffd83dbSDimitry Andric for (const MachineBasicBlock *Pred : MBB->predecessors()) { 38715ffd83dbSDimitry Andric if (Reachable.count(Pred) && 38725ffd83dbSDimitry Andric (SPState[Pred->getNumber()].ExitValue != BBState.EntryValue || 38735ffd83dbSDimitry Andric SPState[Pred->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) { 38740b57cec5SDimitry Andric report("The exit stack state of a predecessor is inconsistent.", MBB); 38755ffd83dbSDimitry Andric errs() << "Predecessor " << printMBBReference(*Pred) 38765ffd83dbSDimitry Andric << " has exit state (" << SPState[Pred->getNumber()].ExitValue 38775ffd83dbSDimitry Andric << ", " << SPState[Pred->getNumber()].ExitIsSetup << "), while " 38780b57cec5SDimitry Andric << printMBBReference(*MBB) << " has entry state (" 38790b57cec5SDimitry Andric << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n"; 38800b57cec5SDimitry Andric } 38810b57cec5SDimitry Andric } 38820b57cec5SDimitry Andric 38830b57cec5SDimitry Andric // Make sure the entry state of any successor is consistent with the exit 38840b57cec5SDimitry Andric // state. 38855ffd83dbSDimitry Andric for (const MachineBasicBlock *Succ : MBB->successors()) { 38865ffd83dbSDimitry Andric if (Reachable.count(Succ) && 38875ffd83dbSDimitry Andric (SPState[Succ->getNumber()].EntryValue != BBState.ExitValue || 38885ffd83dbSDimitry Andric SPState[Succ->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) { 38890b57cec5SDimitry Andric report("The entry stack state of a successor is inconsistent.", MBB); 38905ffd83dbSDimitry Andric errs() << "Successor " << printMBBReference(*Succ) 38915ffd83dbSDimitry Andric << " has entry state (" << SPState[Succ->getNumber()].EntryValue 38925ffd83dbSDimitry Andric << ", " << SPState[Succ->getNumber()].EntryIsSetup << "), while " 38930b57cec5SDimitry Andric << printMBBReference(*MBB) << " has exit state (" 38940b57cec5SDimitry Andric << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n"; 38950b57cec5SDimitry Andric } 38960b57cec5SDimitry Andric } 38970b57cec5SDimitry Andric 38980b57cec5SDimitry Andric // Make sure a basic block with return ends with zero stack adjustment. 38990b57cec5SDimitry Andric if (!MBB->empty() && MBB->back().isReturn()) { 39000b57cec5SDimitry Andric if (BBState.ExitIsSetup) 39010b57cec5SDimitry Andric report("A return block ends with a FrameSetup.", MBB); 39020b57cec5SDimitry Andric if (BBState.ExitValue) 39030b57cec5SDimitry Andric report("A return block ends with a nonzero stack adjustment.", MBB); 39040b57cec5SDimitry Andric } 39050b57cec5SDimitry Andric } 39060b57cec5SDimitry Andric } 3907