1*9880d681SAndroid Build Coastguard Worker //===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // Pass to verify generated machine code. The following is checked:
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker // Operand counts: All explicit operands must be present.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker // Register classes: All physical and virtual register operands must be
15*9880d681SAndroid Build Coastguard Worker // compatible with the register class required by the instruction descriptor.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // Register live intervals: Registers must be defined only once, and must be
18*9880d681SAndroid Build Coastguard Worker // defined before use.
19*9880d681SAndroid Build Coastguard Worker //
20*9880d681SAndroid Build Coastguard Worker // The machine code verifier is enabled from LLVMTargetMachine.cpp with the
21*9880d681SAndroid Build Coastguard Worker // command-line option -verify-machineinstrs, or by defining the environment
22*9880d681SAndroid Build Coastguard Worker // variable LLVM_VERIFY_MACHINEINSTRS to the name of a file that will receive
23*9880d681SAndroid Build Coastguard Worker // the verifier errors.
24*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/Passes.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DenseSet.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/DepthFirstIterator.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SetOperations.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/EHPersonalities.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveIntervalAnalysis.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveStackAnalysis.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/LiveVariables.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
37*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineMemOperand.h"
38*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
39*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
40*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InlineAsm.h"
41*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
42*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAsmInfo.h"
43*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
44*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
45*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
46*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
47*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
48*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
49*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
50*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetSubtargetInfo.h"
51*9880d681SAndroid Build Coastguard Worker using namespace llvm;
52*9880d681SAndroid Build Coastguard Worker
53*9880d681SAndroid Build Coastguard Worker namespace {
54*9880d681SAndroid Build Coastguard Worker struct MachineVerifier {
55*9880d681SAndroid Build Coastguard Worker
MachineVerifier__anon2ea96b9d0111::MachineVerifier56*9880d681SAndroid Build Coastguard Worker MachineVerifier(Pass *pass, const char *b) :
57*9880d681SAndroid Build Coastguard Worker PASS(pass),
58*9880d681SAndroid Build Coastguard Worker Banner(b)
59*9880d681SAndroid Build Coastguard Worker {}
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker unsigned verify(MachineFunction &MF);
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker Pass *const PASS;
64*9880d681SAndroid Build Coastguard Worker const char *Banner;
65*9880d681SAndroid Build Coastguard Worker const MachineFunction *MF;
66*9880d681SAndroid Build Coastguard Worker const TargetMachine *TM;
67*9880d681SAndroid Build Coastguard Worker const TargetInstrInfo *TII;
68*9880d681SAndroid Build Coastguard Worker const TargetRegisterInfo *TRI;
69*9880d681SAndroid Build Coastguard Worker const MachineRegisterInfo *MRI;
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker unsigned foundErrors;
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker typedef SmallVector<unsigned, 16> RegVector;
74*9880d681SAndroid Build Coastguard Worker typedef SmallVector<const uint32_t*, 4> RegMaskVector;
75*9880d681SAndroid Build Coastguard Worker typedef DenseSet<unsigned> RegSet;
76*9880d681SAndroid Build Coastguard Worker typedef DenseMap<unsigned, const MachineInstr*> RegMap;
77*9880d681SAndroid Build Coastguard Worker typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet;
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker const MachineInstr *FirstTerminator;
80*9880d681SAndroid Build Coastguard Worker BlockSet FunctionBlocks;
81*9880d681SAndroid Build Coastguard Worker
82*9880d681SAndroid Build Coastguard Worker BitVector regsReserved;
83*9880d681SAndroid Build Coastguard Worker RegSet regsLive;
84*9880d681SAndroid Build Coastguard Worker RegVector regsDefined, regsDead, regsKilled;
85*9880d681SAndroid Build Coastguard Worker RegMaskVector regMasks;
86*9880d681SAndroid Build Coastguard Worker RegSet regsLiveInButUnused;
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker SlotIndex lastIndex;
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker // Add Reg and any sub-registers to RV
addRegWithSubRegs__anon2ea96b9d0111::MachineVerifier91*9880d681SAndroid Build Coastguard Worker void addRegWithSubRegs(RegVector &RV, unsigned Reg) {
92*9880d681SAndroid Build Coastguard Worker RV.push_back(Reg);
93*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg))
94*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
95*9880d681SAndroid Build Coastguard Worker RV.push_back(*SubRegs);
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker struct BBInfo {
99*9880d681SAndroid Build Coastguard Worker // Is this MBB reachable from the MF entry point?
100*9880d681SAndroid Build Coastguard Worker bool reachable;
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker // Vregs that must be live in because they are used without being
103*9880d681SAndroid Build Coastguard Worker // defined. Map value is the user.
104*9880d681SAndroid Build Coastguard Worker RegMap vregsLiveIn;
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker // Regs killed in MBB. They may be defined again, and will then be in both
107*9880d681SAndroid Build Coastguard Worker // regsKilled and regsLiveOut.
108*9880d681SAndroid Build Coastguard Worker RegSet regsKilled;
109*9880d681SAndroid Build Coastguard Worker
110*9880d681SAndroid Build Coastguard Worker // Regs defined in MBB and live out. Note that vregs passing through may
111*9880d681SAndroid Build Coastguard Worker // be live out without being mentioned here.
112*9880d681SAndroid Build Coastguard Worker RegSet regsLiveOut;
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker // Vregs that pass through MBB untouched. This set is disjoint from
115*9880d681SAndroid Build Coastguard Worker // regsKilled and regsLiveOut.
116*9880d681SAndroid Build Coastguard Worker RegSet vregsPassed;
117*9880d681SAndroid Build Coastguard Worker
118*9880d681SAndroid Build Coastguard Worker // Vregs that must pass through MBB because they are needed by a successor
119*9880d681SAndroid Build Coastguard Worker // block. This set is disjoint from regsLiveOut.
120*9880d681SAndroid Build Coastguard Worker RegSet vregsRequired;
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker // Set versions of block's predecessor and successor lists.
123*9880d681SAndroid Build Coastguard Worker BlockSet Preds, Succs;
124*9880d681SAndroid Build Coastguard Worker
BBInfo__anon2ea96b9d0111::MachineVerifier::BBInfo125*9880d681SAndroid Build Coastguard Worker BBInfo() : reachable(false) {}
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker // Add register to vregsPassed if it belongs there. Return true if
128*9880d681SAndroid Build Coastguard Worker // anything changed.
addPassed__anon2ea96b9d0111::MachineVerifier::BBInfo129*9880d681SAndroid Build Coastguard Worker bool addPassed(unsigned Reg) {
130*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
131*9880d681SAndroid Build Coastguard Worker return false;
132*9880d681SAndroid Build Coastguard Worker if (regsKilled.count(Reg) || regsLiveOut.count(Reg))
133*9880d681SAndroid Build Coastguard Worker return false;
134*9880d681SAndroid Build Coastguard Worker return vregsPassed.insert(Reg).second;
135*9880d681SAndroid Build Coastguard Worker }
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker // Same for a full set.
addPassed__anon2ea96b9d0111::MachineVerifier::BBInfo138*9880d681SAndroid Build Coastguard Worker bool addPassed(const RegSet &RS) {
139*9880d681SAndroid Build Coastguard Worker bool changed = false;
140*9880d681SAndroid Build Coastguard Worker for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
141*9880d681SAndroid Build Coastguard Worker if (addPassed(*I))
142*9880d681SAndroid Build Coastguard Worker changed = true;
143*9880d681SAndroid Build Coastguard Worker return changed;
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker // Add register to vregsRequired if it belongs there. Return true if
147*9880d681SAndroid Build Coastguard Worker // anything changed.
addRequired__anon2ea96b9d0111::MachineVerifier::BBInfo148*9880d681SAndroid Build Coastguard Worker bool addRequired(unsigned Reg) {
149*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg))
150*9880d681SAndroid Build Coastguard Worker return false;
151*9880d681SAndroid Build Coastguard Worker if (regsLiveOut.count(Reg))
152*9880d681SAndroid Build Coastguard Worker return false;
153*9880d681SAndroid Build Coastguard Worker return vregsRequired.insert(Reg).second;
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker // Same for a full set.
addRequired__anon2ea96b9d0111::MachineVerifier::BBInfo157*9880d681SAndroid Build Coastguard Worker bool addRequired(const RegSet &RS) {
158*9880d681SAndroid Build Coastguard Worker bool changed = false;
159*9880d681SAndroid Build Coastguard Worker for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I)
160*9880d681SAndroid Build Coastguard Worker if (addRequired(*I))
161*9880d681SAndroid Build Coastguard Worker changed = true;
162*9880d681SAndroid Build Coastguard Worker return changed;
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker
165*9880d681SAndroid Build Coastguard Worker // Same for a full map.
addRequired__anon2ea96b9d0111::MachineVerifier::BBInfo166*9880d681SAndroid Build Coastguard Worker bool addRequired(const RegMap &RM) {
167*9880d681SAndroid Build Coastguard Worker bool changed = false;
168*9880d681SAndroid Build Coastguard Worker for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I)
169*9880d681SAndroid Build Coastguard Worker if (addRequired(I->first))
170*9880d681SAndroid Build Coastguard Worker changed = true;
171*9880d681SAndroid Build Coastguard Worker return changed;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker
174*9880d681SAndroid Build Coastguard Worker // Live-out registers are either in regsLiveOut or vregsPassed.
isLiveOut__anon2ea96b9d0111::MachineVerifier::BBInfo175*9880d681SAndroid Build Coastguard Worker bool isLiveOut(unsigned Reg) const {
176*9880d681SAndroid Build Coastguard Worker return regsLiveOut.count(Reg) || vregsPassed.count(Reg);
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker };
179*9880d681SAndroid Build Coastguard Worker
180*9880d681SAndroid Build Coastguard Worker // Extra register info per MBB.
181*9880d681SAndroid Build Coastguard Worker DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap;
182*9880d681SAndroid Build Coastguard Worker
isReserved__anon2ea96b9d0111::MachineVerifier183*9880d681SAndroid Build Coastguard Worker bool isReserved(unsigned Reg) {
184*9880d681SAndroid Build Coastguard Worker return Reg < regsReserved.size() && regsReserved.test(Reg);
185*9880d681SAndroid Build Coastguard Worker }
186*9880d681SAndroid Build Coastguard Worker
isAllocatable__anon2ea96b9d0111::MachineVerifier187*9880d681SAndroid Build Coastguard Worker bool isAllocatable(unsigned Reg) {
188*9880d681SAndroid Build Coastguard Worker return Reg < TRI->getNumRegs() && MRI->isAllocatable(Reg);
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker
191*9880d681SAndroid Build Coastguard Worker // Analysis information if available
192*9880d681SAndroid Build Coastguard Worker LiveVariables *LiveVars;
193*9880d681SAndroid Build Coastguard Worker LiveIntervals *LiveInts;
194*9880d681SAndroid Build Coastguard Worker LiveStacks *LiveStks;
195*9880d681SAndroid Build Coastguard Worker SlotIndexes *Indexes;
196*9880d681SAndroid Build Coastguard Worker
197*9880d681SAndroid Build Coastguard Worker void visitMachineFunctionBefore();
198*9880d681SAndroid Build Coastguard Worker void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB);
199*9880d681SAndroid Build Coastguard Worker void visitMachineBundleBefore(const MachineInstr *MI);
200*9880d681SAndroid Build Coastguard Worker void visitMachineInstrBefore(const MachineInstr *MI);
201*9880d681SAndroid Build Coastguard Worker void visitMachineOperand(const MachineOperand *MO, unsigned MONum);
202*9880d681SAndroid Build Coastguard Worker void visitMachineInstrAfter(const MachineInstr *MI);
203*9880d681SAndroid Build Coastguard Worker void visitMachineBundleAfter(const MachineInstr *MI);
204*9880d681SAndroid Build Coastguard Worker void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB);
205*9880d681SAndroid Build Coastguard Worker void visitMachineFunctionAfter();
206*9880d681SAndroid Build Coastguard Worker
report__anon2ea96b9d0111::MachineVerifier207*9880d681SAndroid Build Coastguard Worker template <typename T> void report(const char *msg, ilist_iterator<T> I) {
208*9880d681SAndroid Build Coastguard Worker report(msg, &*I);
209*9880d681SAndroid Build Coastguard Worker }
210*9880d681SAndroid Build Coastguard Worker void report(const char *msg, const MachineFunction *MF);
211*9880d681SAndroid Build Coastguard Worker void report(const char *msg, const MachineBasicBlock *MBB);
212*9880d681SAndroid Build Coastguard Worker void report(const char *msg, const MachineInstr *MI);
213*9880d681SAndroid Build Coastguard Worker void report(const char *msg, const MachineOperand *MO, unsigned MONum);
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker void report_context(const LiveInterval &LI) const;
216*9880d681SAndroid Build Coastguard Worker void report_context(const LiveRange &LR, unsigned Reg,
217*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask) const;
218*9880d681SAndroid Build Coastguard Worker void report_context(const LiveRange::Segment &S) const;
219*9880d681SAndroid Build Coastguard Worker void report_context(const VNInfo &VNI) const;
220*9880d681SAndroid Build Coastguard Worker void report_context(SlotIndex Pos) const;
221*9880d681SAndroid Build Coastguard Worker void report_context_liverange(const LiveRange &LR) const;
222*9880d681SAndroid Build Coastguard Worker void report_context_lanemask(LaneBitmask LaneMask) const;
223*9880d681SAndroid Build Coastguard Worker void report_context_vreg(unsigned VReg) const;
224*9880d681SAndroid Build Coastguard Worker void report_context_vreg_regunit(unsigned VRegOrRegUnit) const;
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker void verifyInlineAsm(const MachineInstr *MI);
227*9880d681SAndroid Build Coastguard Worker
228*9880d681SAndroid Build Coastguard Worker void checkLiveness(const MachineOperand *MO, unsigned MONum);
229*9880d681SAndroid Build Coastguard Worker void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum,
230*9880d681SAndroid Build Coastguard Worker SlotIndex UseIdx, const LiveRange &LR, unsigned Reg,
231*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask = 0);
232*9880d681SAndroid Build Coastguard Worker void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum,
233*9880d681SAndroid Build Coastguard Worker SlotIndex DefIdx, const LiveRange &LR, unsigned Reg,
234*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask = 0);
235*9880d681SAndroid Build Coastguard Worker
236*9880d681SAndroid Build Coastguard Worker void markReachable(const MachineBasicBlock *MBB);
237*9880d681SAndroid Build Coastguard Worker void calcRegsPassed();
238*9880d681SAndroid Build Coastguard Worker void checkPHIOps(const MachineBasicBlock *MBB);
239*9880d681SAndroid Build Coastguard Worker
240*9880d681SAndroid Build Coastguard Worker void calcRegsRequired();
241*9880d681SAndroid Build Coastguard Worker void verifyLiveVariables();
242*9880d681SAndroid Build Coastguard Worker void verifyLiveIntervals();
243*9880d681SAndroid Build Coastguard Worker void verifyLiveInterval(const LiveInterval&);
244*9880d681SAndroid Build Coastguard Worker void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned,
245*9880d681SAndroid Build Coastguard Worker unsigned);
246*9880d681SAndroid Build Coastguard Worker void verifyLiveRangeSegment(const LiveRange&,
247*9880d681SAndroid Build Coastguard Worker const LiveRange::const_iterator I, unsigned,
248*9880d681SAndroid Build Coastguard Worker unsigned);
249*9880d681SAndroid Build Coastguard Worker void verifyLiveRange(const LiveRange&, unsigned, LaneBitmask LaneMask = 0);
250*9880d681SAndroid Build Coastguard Worker
251*9880d681SAndroid Build Coastguard Worker void verifyStackFrame();
252*9880d681SAndroid Build Coastguard Worker
253*9880d681SAndroid Build Coastguard Worker void verifySlotIndexes() const;
254*9880d681SAndroid Build Coastguard Worker void verifyProperties(const MachineFunction &MF);
255*9880d681SAndroid Build Coastguard Worker };
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker struct MachineVerifierPass : public MachineFunctionPass {
258*9880d681SAndroid Build Coastguard Worker static char ID; // Pass ID, replacement for typeid
259*9880d681SAndroid Build Coastguard Worker const std::string Banner;
260*9880d681SAndroid Build Coastguard Worker
MachineVerifierPass__anon2ea96b9d0111::MachineVerifierPass261*9880d681SAndroid Build Coastguard Worker MachineVerifierPass(const std::string &banner = nullptr)
262*9880d681SAndroid Build Coastguard Worker : MachineFunctionPass(ID), Banner(banner) {
263*9880d681SAndroid Build Coastguard Worker initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry());
264*9880d681SAndroid Build Coastguard Worker }
265*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon2ea96b9d0111::MachineVerifierPass266*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
267*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
268*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
269*9880d681SAndroid Build Coastguard Worker }
270*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction__anon2ea96b9d0111::MachineVerifierPass271*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override {
272*9880d681SAndroid Build Coastguard Worker unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF);
273*9880d681SAndroid Build Coastguard Worker if (FoundErrors)
274*9880d681SAndroid Build Coastguard Worker report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
275*9880d681SAndroid Build Coastguard Worker return false;
276*9880d681SAndroid Build Coastguard Worker }
277*9880d681SAndroid Build Coastguard Worker };
278*9880d681SAndroid Build Coastguard Worker
279*9880d681SAndroid Build Coastguard Worker }
280*9880d681SAndroid Build Coastguard Worker
281*9880d681SAndroid Build Coastguard Worker char MachineVerifierPass::ID = 0;
282*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(MachineVerifierPass, "machineverifier",
283*9880d681SAndroid Build Coastguard Worker "Verify generated machine code", false, false)
284*9880d681SAndroid Build Coastguard Worker
createMachineVerifierPass(const std::string & Banner)285*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) {
286*9880d681SAndroid Build Coastguard Worker return new MachineVerifierPass(Banner);
287*9880d681SAndroid Build Coastguard Worker }
288*9880d681SAndroid Build Coastguard Worker
verify(Pass * p,const char * Banner,bool AbortOnErrors) const289*9880d681SAndroid Build Coastguard Worker bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors)
290*9880d681SAndroid Build Coastguard Worker const {
291*9880d681SAndroid Build Coastguard Worker MachineFunction &MF = const_cast<MachineFunction&>(*this);
292*9880d681SAndroid Build Coastguard Worker unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF);
293*9880d681SAndroid Build Coastguard Worker if (AbortOnErrors && FoundErrors)
294*9880d681SAndroid Build Coastguard Worker report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors.");
295*9880d681SAndroid Build Coastguard Worker return FoundErrors == 0;
296*9880d681SAndroid Build Coastguard Worker }
297*9880d681SAndroid Build Coastguard Worker
verifySlotIndexes() const298*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifySlotIndexes() const {
299*9880d681SAndroid Build Coastguard Worker if (Indexes == nullptr)
300*9880d681SAndroid Build Coastguard Worker return;
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard Worker // Ensure the IdxMBB list is sorted by slot indexes.
303*9880d681SAndroid Build Coastguard Worker SlotIndex Last;
304*9880d681SAndroid Build Coastguard Worker for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(),
305*9880d681SAndroid Build Coastguard Worker E = Indexes->MBBIndexEnd(); I != E; ++I) {
306*9880d681SAndroid Build Coastguard Worker assert(!Last.isValid() || I->first > Last);
307*9880d681SAndroid Build Coastguard Worker Last = I->first;
308*9880d681SAndroid Build Coastguard Worker }
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker
verifyProperties(const MachineFunction & MF)311*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyProperties(const MachineFunction &MF) {
312*9880d681SAndroid Build Coastguard Worker // If a pass has introduced virtual registers without clearing the
313*9880d681SAndroid Build Coastguard Worker // AllVRegsAllocated property (or set it without allocating the vregs)
314*9880d681SAndroid Build Coastguard Worker // then report an error.
315*9880d681SAndroid Build Coastguard Worker if (MF.getProperties().hasProperty(
316*9880d681SAndroid Build Coastguard Worker MachineFunctionProperties::Property::AllVRegsAllocated) &&
317*9880d681SAndroid Build Coastguard Worker MRI->getNumVirtRegs()) {
318*9880d681SAndroid Build Coastguard Worker report(
319*9880d681SAndroid Build Coastguard Worker "Function has AllVRegsAllocated property but there are VReg operands",
320*9880d681SAndroid Build Coastguard Worker &MF);
321*9880d681SAndroid Build Coastguard Worker }
322*9880d681SAndroid Build Coastguard Worker }
323*9880d681SAndroid Build Coastguard Worker
verify(MachineFunction & MF)324*9880d681SAndroid Build Coastguard Worker unsigned MachineVerifier::verify(MachineFunction &MF) {
325*9880d681SAndroid Build Coastguard Worker foundErrors = 0;
326*9880d681SAndroid Build Coastguard Worker
327*9880d681SAndroid Build Coastguard Worker this->MF = &MF;
328*9880d681SAndroid Build Coastguard Worker TM = &MF.getTarget();
329*9880d681SAndroid Build Coastguard Worker TII = MF.getSubtarget().getInstrInfo();
330*9880d681SAndroid Build Coastguard Worker TRI = MF.getSubtarget().getRegisterInfo();
331*9880d681SAndroid Build Coastguard Worker MRI = &MF.getRegInfo();
332*9880d681SAndroid Build Coastguard Worker
333*9880d681SAndroid Build Coastguard Worker LiveVars = nullptr;
334*9880d681SAndroid Build Coastguard Worker LiveInts = nullptr;
335*9880d681SAndroid Build Coastguard Worker LiveStks = nullptr;
336*9880d681SAndroid Build Coastguard Worker Indexes = nullptr;
337*9880d681SAndroid Build Coastguard Worker if (PASS) {
338*9880d681SAndroid Build Coastguard Worker LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>();
339*9880d681SAndroid Build Coastguard Worker // We don't want to verify LiveVariables if LiveIntervals is available.
340*9880d681SAndroid Build Coastguard Worker if (!LiveInts)
341*9880d681SAndroid Build Coastguard Worker LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>();
342*9880d681SAndroid Build Coastguard Worker LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>();
343*9880d681SAndroid Build Coastguard Worker Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>();
344*9880d681SAndroid Build Coastguard Worker }
345*9880d681SAndroid Build Coastguard Worker
346*9880d681SAndroid Build Coastguard Worker verifySlotIndexes();
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker verifyProperties(MF);
349*9880d681SAndroid Build Coastguard Worker
350*9880d681SAndroid Build Coastguard Worker visitMachineFunctionBefore();
351*9880d681SAndroid Build Coastguard Worker for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end();
352*9880d681SAndroid Build Coastguard Worker MFI!=MFE; ++MFI) {
353*9880d681SAndroid Build Coastguard Worker visitMachineBasicBlockBefore(&*MFI);
354*9880d681SAndroid Build Coastguard Worker // Keep track of the current bundle header.
355*9880d681SAndroid Build Coastguard Worker const MachineInstr *CurBundle = nullptr;
356*9880d681SAndroid Build Coastguard Worker // Do we expect the next instruction to be part of the same bundle?
357*9880d681SAndroid Build Coastguard Worker bool InBundle = false;
358*9880d681SAndroid Build Coastguard Worker
359*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(),
360*9880d681SAndroid Build Coastguard Worker MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) {
361*9880d681SAndroid Build Coastguard Worker if (MBBI->getParent() != &*MFI) {
362*9880d681SAndroid Build Coastguard Worker report("Bad instruction parent pointer", MFI);
363*9880d681SAndroid Build Coastguard Worker errs() << "Instruction: " << *MBBI;
364*9880d681SAndroid Build Coastguard Worker continue;
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker // Check for consistent bundle flags.
368*9880d681SAndroid Build Coastguard Worker if (InBundle && !MBBI->isBundledWithPred())
369*9880d681SAndroid Build Coastguard Worker report("Missing BundledPred flag, "
370*9880d681SAndroid Build Coastguard Worker "BundledSucc was set on predecessor",
371*9880d681SAndroid Build Coastguard Worker &*MBBI);
372*9880d681SAndroid Build Coastguard Worker if (!InBundle && MBBI->isBundledWithPred())
373*9880d681SAndroid Build Coastguard Worker report("BundledPred flag is set, "
374*9880d681SAndroid Build Coastguard Worker "but BundledSucc not set on predecessor",
375*9880d681SAndroid Build Coastguard Worker &*MBBI);
376*9880d681SAndroid Build Coastguard Worker
377*9880d681SAndroid Build Coastguard Worker // Is this a bundle header?
378*9880d681SAndroid Build Coastguard Worker if (!MBBI->isInsideBundle()) {
379*9880d681SAndroid Build Coastguard Worker if (CurBundle)
380*9880d681SAndroid Build Coastguard Worker visitMachineBundleAfter(CurBundle);
381*9880d681SAndroid Build Coastguard Worker CurBundle = &*MBBI;
382*9880d681SAndroid Build Coastguard Worker visitMachineBundleBefore(CurBundle);
383*9880d681SAndroid Build Coastguard Worker } else if (!CurBundle)
384*9880d681SAndroid Build Coastguard Worker report("No bundle header", MBBI);
385*9880d681SAndroid Build Coastguard Worker visitMachineInstrBefore(&*MBBI);
386*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) {
387*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI = *MBBI;
388*9880d681SAndroid Build Coastguard Worker const MachineOperand &Op = MI.getOperand(I);
389*9880d681SAndroid Build Coastguard Worker if (Op.getParent() != &MI) {
390*9880d681SAndroid Build Coastguard Worker // Make sure to use correct addOperand / RemoveOperand / ChangeTo
391*9880d681SAndroid Build Coastguard Worker // functions when replacing operands of a MachineInstr.
392*9880d681SAndroid Build Coastguard Worker report("Instruction has operand with wrong parent set", &MI);
393*9880d681SAndroid Build Coastguard Worker }
394*9880d681SAndroid Build Coastguard Worker
395*9880d681SAndroid Build Coastguard Worker visitMachineOperand(&Op, I);
396*9880d681SAndroid Build Coastguard Worker }
397*9880d681SAndroid Build Coastguard Worker
398*9880d681SAndroid Build Coastguard Worker visitMachineInstrAfter(&*MBBI);
399*9880d681SAndroid Build Coastguard Worker
400*9880d681SAndroid Build Coastguard Worker // Was this the last bundled instruction?
401*9880d681SAndroid Build Coastguard Worker InBundle = MBBI->isBundledWithSucc();
402*9880d681SAndroid Build Coastguard Worker }
403*9880d681SAndroid Build Coastguard Worker if (CurBundle)
404*9880d681SAndroid Build Coastguard Worker visitMachineBundleAfter(CurBundle);
405*9880d681SAndroid Build Coastguard Worker if (InBundle)
406*9880d681SAndroid Build Coastguard Worker report("BundledSucc flag set on last instruction in block", &MFI->back());
407*9880d681SAndroid Build Coastguard Worker visitMachineBasicBlockAfter(&*MFI);
408*9880d681SAndroid Build Coastguard Worker }
409*9880d681SAndroid Build Coastguard Worker visitMachineFunctionAfter();
410*9880d681SAndroid Build Coastguard Worker
411*9880d681SAndroid Build Coastguard Worker // Clean up.
412*9880d681SAndroid Build Coastguard Worker regsLive.clear();
413*9880d681SAndroid Build Coastguard Worker regsDefined.clear();
414*9880d681SAndroid Build Coastguard Worker regsDead.clear();
415*9880d681SAndroid Build Coastguard Worker regsKilled.clear();
416*9880d681SAndroid Build Coastguard Worker regMasks.clear();
417*9880d681SAndroid Build Coastguard Worker regsLiveInButUnused.clear();
418*9880d681SAndroid Build Coastguard Worker MBBInfoMap.clear();
419*9880d681SAndroid Build Coastguard Worker
420*9880d681SAndroid Build Coastguard Worker return foundErrors;
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker
report(const char * msg,const MachineFunction * MF)423*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
424*9880d681SAndroid Build Coastguard Worker assert(MF);
425*9880d681SAndroid Build Coastguard Worker errs() << '\n';
426*9880d681SAndroid Build Coastguard Worker if (!foundErrors++) {
427*9880d681SAndroid Build Coastguard Worker if (Banner)
428*9880d681SAndroid Build Coastguard Worker errs() << "# " << Banner << '\n';
429*9880d681SAndroid Build Coastguard Worker if (LiveInts != nullptr)
430*9880d681SAndroid Build Coastguard Worker LiveInts->print(errs());
431*9880d681SAndroid Build Coastguard Worker else
432*9880d681SAndroid Build Coastguard Worker MF->print(errs(), Indexes);
433*9880d681SAndroid Build Coastguard Worker }
434*9880d681SAndroid Build Coastguard Worker errs() << "*** Bad machine code: " << msg << " ***\n"
435*9880d681SAndroid Build Coastguard Worker << "- function: " << MF->getName() << "\n";
436*9880d681SAndroid Build Coastguard Worker }
437*9880d681SAndroid Build Coastguard Worker
report(const char * msg,const MachineBasicBlock * MBB)438*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) {
439*9880d681SAndroid Build Coastguard Worker assert(MBB);
440*9880d681SAndroid Build Coastguard Worker report(msg, MBB->getParent());
441*9880d681SAndroid Build Coastguard Worker errs() << "- basic block: BB#" << MBB->getNumber()
442*9880d681SAndroid Build Coastguard Worker << ' ' << MBB->getName()
443*9880d681SAndroid Build Coastguard Worker << " (" << (const void*)MBB << ')';
444*9880d681SAndroid Build Coastguard Worker if (Indexes)
445*9880d681SAndroid Build Coastguard Worker errs() << " [" << Indexes->getMBBStartIdx(MBB)
446*9880d681SAndroid Build Coastguard Worker << ';' << Indexes->getMBBEndIdx(MBB) << ')';
447*9880d681SAndroid Build Coastguard Worker errs() << '\n';
448*9880d681SAndroid Build Coastguard Worker }
449*9880d681SAndroid Build Coastguard Worker
report(const char * msg,const MachineInstr * MI)450*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report(const char *msg, const MachineInstr *MI) {
451*9880d681SAndroid Build Coastguard Worker assert(MI);
452*9880d681SAndroid Build Coastguard Worker report(msg, MI->getParent());
453*9880d681SAndroid Build Coastguard Worker errs() << "- instruction: ";
454*9880d681SAndroid Build Coastguard Worker if (Indexes && Indexes->hasIndex(*MI))
455*9880d681SAndroid Build Coastguard Worker errs() << Indexes->getInstructionIndex(*MI) << '\t';
456*9880d681SAndroid Build Coastguard Worker MI->print(errs(), /*SkipOpers=*/true);
457*9880d681SAndroid Build Coastguard Worker errs() << '\n';
458*9880d681SAndroid Build Coastguard Worker }
459*9880d681SAndroid Build Coastguard Worker
report(const char * msg,const MachineOperand * MO,unsigned MONum)460*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report(const char *msg,
461*9880d681SAndroid Build Coastguard Worker const MachineOperand *MO, unsigned MONum) {
462*9880d681SAndroid Build Coastguard Worker assert(MO);
463*9880d681SAndroid Build Coastguard Worker report(msg, MO->getParent());
464*9880d681SAndroid Build Coastguard Worker errs() << "- operand " << MONum << ": ";
465*9880d681SAndroid Build Coastguard Worker MO->print(errs(), TRI);
466*9880d681SAndroid Build Coastguard Worker errs() << "\n";
467*9880d681SAndroid Build Coastguard Worker }
468*9880d681SAndroid Build Coastguard Worker
report_context(SlotIndex Pos) const469*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context(SlotIndex Pos) const {
470*9880d681SAndroid Build Coastguard Worker errs() << "- at: " << Pos << '\n';
471*9880d681SAndroid Build Coastguard Worker }
472*9880d681SAndroid Build Coastguard Worker
report_context(const LiveInterval & LI) const473*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context(const LiveInterval &LI) const {
474*9880d681SAndroid Build Coastguard Worker errs() << "- interval: " << LI << '\n';
475*9880d681SAndroid Build Coastguard Worker }
476*9880d681SAndroid Build Coastguard Worker
report_context(const LiveRange & LR,unsigned Reg,LaneBitmask LaneMask) const477*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context(const LiveRange &LR, unsigned Reg,
478*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask) const {
479*9880d681SAndroid Build Coastguard Worker report_context_liverange(LR);
480*9880d681SAndroid Build Coastguard Worker errs() << "- register: " << PrintReg(Reg, TRI) << '\n';
481*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0)
482*9880d681SAndroid Build Coastguard Worker report_context_lanemask(LaneMask);
483*9880d681SAndroid Build Coastguard Worker }
484*9880d681SAndroid Build Coastguard Worker
report_context(const LiveRange::Segment & S) const485*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context(const LiveRange::Segment &S) const {
486*9880d681SAndroid Build Coastguard Worker errs() << "- segment: " << S << '\n';
487*9880d681SAndroid Build Coastguard Worker }
488*9880d681SAndroid Build Coastguard Worker
report_context(const VNInfo & VNI) const489*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context(const VNInfo &VNI) const {
490*9880d681SAndroid Build Coastguard Worker errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n";
491*9880d681SAndroid Build Coastguard Worker }
492*9880d681SAndroid Build Coastguard Worker
report_context_liverange(const LiveRange & LR) const493*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context_liverange(const LiveRange &LR) const {
494*9880d681SAndroid Build Coastguard Worker errs() << "- liverange: " << LR << '\n';
495*9880d681SAndroid Build Coastguard Worker }
496*9880d681SAndroid Build Coastguard Worker
report_context_vreg(unsigned VReg) const497*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context_vreg(unsigned VReg) const {
498*9880d681SAndroid Build Coastguard Worker errs() << "- v. register: " << PrintReg(VReg, TRI) << '\n';
499*9880d681SAndroid Build Coastguard Worker }
500*9880d681SAndroid Build Coastguard Worker
report_context_vreg_regunit(unsigned VRegOrUnit) const501*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const {
502*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
503*9880d681SAndroid Build Coastguard Worker report_context_vreg(VRegOrUnit);
504*9880d681SAndroid Build Coastguard Worker } else {
505*9880d681SAndroid Build Coastguard Worker errs() << "- regunit: " << PrintRegUnit(VRegOrUnit, TRI) << '\n';
506*9880d681SAndroid Build Coastguard Worker }
507*9880d681SAndroid Build Coastguard Worker }
508*9880d681SAndroid Build Coastguard Worker
report_context_lanemask(LaneBitmask LaneMask) const509*9880d681SAndroid Build Coastguard Worker void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const {
510*9880d681SAndroid Build Coastguard Worker errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n';
511*9880d681SAndroid Build Coastguard Worker }
512*9880d681SAndroid Build Coastguard Worker
markReachable(const MachineBasicBlock * MBB)513*9880d681SAndroid Build Coastguard Worker void MachineVerifier::markReachable(const MachineBasicBlock *MBB) {
514*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[MBB];
515*9880d681SAndroid Build Coastguard Worker if (!MInfo.reachable) {
516*9880d681SAndroid Build Coastguard Worker MInfo.reachable = true;
517*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
518*9880d681SAndroid Build Coastguard Worker SuE = MBB->succ_end(); SuI != SuE; ++SuI)
519*9880d681SAndroid Build Coastguard Worker markReachable(*SuI);
520*9880d681SAndroid Build Coastguard Worker }
521*9880d681SAndroid Build Coastguard Worker }
522*9880d681SAndroid Build Coastguard Worker
visitMachineFunctionBefore()523*9880d681SAndroid Build Coastguard Worker void MachineVerifier::visitMachineFunctionBefore() {
524*9880d681SAndroid Build Coastguard Worker lastIndex = SlotIndex();
525*9880d681SAndroid Build Coastguard Worker regsReserved = MRI->getReservedRegs();
526*9880d681SAndroid Build Coastguard Worker
527*9880d681SAndroid Build Coastguard Worker // A sub-register of a reserved register is also reserved
528*9880d681SAndroid Build Coastguard Worker for (int Reg = regsReserved.find_first(); Reg>=0;
529*9880d681SAndroid Build Coastguard Worker Reg = regsReserved.find_next(Reg)) {
530*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) {
531*9880d681SAndroid Build Coastguard Worker // FIXME: This should probably be:
532*9880d681SAndroid Build Coastguard Worker // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register");
533*9880d681SAndroid Build Coastguard Worker regsReserved.set(*SubRegs);
534*9880d681SAndroid Build Coastguard Worker }
535*9880d681SAndroid Build Coastguard Worker }
536*9880d681SAndroid Build Coastguard Worker
537*9880d681SAndroid Build Coastguard Worker markReachable(&MF->front());
538*9880d681SAndroid Build Coastguard Worker
539*9880d681SAndroid Build Coastguard Worker // Build a set of the basic blocks in the function.
540*9880d681SAndroid Build Coastguard Worker FunctionBlocks.clear();
541*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *MF) {
542*9880d681SAndroid Build Coastguard Worker FunctionBlocks.insert(&MBB);
543*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MBB];
544*9880d681SAndroid Build Coastguard Worker
545*9880d681SAndroid Build Coastguard Worker MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end());
546*9880d681SAndroid Build Coastguard Worker if (MInfo.Preds.size() != MBB.pred_size())
547*9880d681SAndroid Build Coastguard Worker report("MBB has duplicate entries in its predecessor list.", &MBB);
548*9880d681SAndroid Build Coastguard Worker
549*9880d681SAndroid Build Coastguard Worker MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end());
550*9880d681SAndroid Build Coastguard Worker if (MInfo.Succs.size() != MBB.succ_size())
551*9880d681SAndroid Build Coastguard Worker report("MBB has duplicate entries in its successor list.", &MBB);
552*9880d681SAndroid Build Coastguard Worker }
553*9880d681SAndroid Build Coastguard Worker
554*9880d681SAndroid Build Coastguard Worker // Check that the register use lists are sane.
555*9880d681SAndroid Build Coastguard Worker MRI->verifyUseLists();
556*9880d681SAndroid Build Coastguard Worker
557*9880d681SAndroid Build Coastguard Worker verifyStackFrame();
558*9880d681SAndroid Build Coastguard Worker }
559*9880d681SAndroid Build Coastguard Worker
560*9880d681SAndroid Build Coastguard Worker // Does iterator point to a and b as the first two elements?
matchPair(MachineBasicBlock::const_succ_iterator i,const MachineBasicBlock * a,const MachineBasicBlock * b)561*9880d681SAndroid Build Coastguard Worker static bool matchPair(MachineBasicBlock::const_succ_iterator i,
562*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *a, const MachineBasicBlock *b) {
563*9880d681SAndroid Build Coastguard Worker if (*i == a)
564*9880d681SAndroid Build Coastguard Worker return *++i == b;
565*9880d681SAndroid Build Coastguard Worker if (*i == b)
566*9880d681SAndroid Build Coastguard Worker return *++i == a;
567*9880d681SAndroid Build Coastguard Worker return false;
568*9880d681SAndroid Build Coastguard Worker }
569*9880d681SAndroid Build Coastguard Worker
570*9880d681SAndroid Build Coastguard Worker void
visitMachineBasicBlockBefore(const MachineBasicBlock * MBB)571*9880d681SAndroid Build Coastguard Worker MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) {
572*9880d681SAndroid Build Coastguard Worker FirstTerminator = nullptr;
573*9880d681SAndroid Build Coastguard Worker
574*9880d681SAndroid Build Coastguard Worker if (MRI->isSSA()) {
575*9880d681SAndroid Build Coastguard Worker // If this block has allocatable physical registers live-in, check that
576*9880d681SAndroid Build Coastguard Worker // it is an entry block or landing pad.
577*9880d681SAndroid Build Coastguard Worker for (const auto &LI : MBB->liveins()) {
578*9880d681SAndroid Build Coastguard Worker if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() &&
579*9880d681SAndroid Build Coastguard Worker MBB->getIterator() != MBB->getParent()->begin()) {
580*9880d681SAndroid Build Coastguard Worker report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB);
581*9880d681SAndroid Build Coastguard Worker }
582*9880d681SAndroid Build Coastguard Worker }
583*9880d681SAndroid Build Coastguard Worker }
584*9880d681SAndroid Build Coastguard Worker
585*9880d681SAndroid Build Coastguard Worker // Count the number of landing pad successors.
586*9880d681SAndroid Build Coastguard Worker SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs;
587*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
588*9880d681SAndroid Build Coastguard Worker E = MBB->succ_end(); I != E; ++I) {
589*9880d681SAndroid Build Coastguard Worker if ((*I)->isEHPad())
590*9880d681SAndroid Build Coastguard Worker LandingPadSuccs.insert(*I);
591*9880d681SAndroid Build Coastguard Worker if (!FunctionBlocks.count(*I))
592*9880d681SAndroid Build Coastguard Worker report("MBB has successor that isn't part of the function.", MBB);
593*9880d681SAndroid Build Coastguard Worker if (!MBBInfoMap[*I].Preds.count(MBB)) {
594*9880d681SAndroid Build Coastguard Worker report("Inconsistent CFG", MBB);
595*9880d681SAndroid Build Coastguard Worker errs() << "MBB is not in the predecessor list of the successor BB#"
596*9880d681SAndroid Build Coastguard Worker << (*I)->getNumber() << ".\n";
597*9880d681SAndroid Build Coastguard Worker }
598*9880d681SAndroid Build Coastguard Worker }
599*9880d681SAndroid Build Coastguard Worker
600*9880d681SAndroid Build Coastguard Worker // Check the predecessor list.
601*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
602*9880d681SAndroid Build Coastguard Worker E = MBB->pred_end(); I != E; ++I) {
603*9880d681SAndroid Build Coastguard Worker if (!FunctionBlocks.count(*I))
604*9880d681SAndroid Build Coastguard Worker report("MBB has predecessor that isn't part of the function.", MBB);
605*9880d681SAndroid Build Coastguard Worker if (!MBBInfoMap[*I].Succs.count(MBB)) {
606*9880d681SAndroid Build Coastguard Worker report("Inconsistent CFG", MBB);
607*9880d681SAndroid Build Coastguard Worker errs() << "MBB is not in the successor list of the predecessor BB#"
608*9880d681SAndroid Build Coastguard Worker << (*I)->getNumber() << ".\n";
609*9880d681SAndroid Build Coastguard Worker }
610*9880d681SAndroid Build Coastguard Worker }
611*9880d681SAndroid Build Coastguard Worker
612*9880d681SAndroid Build Coastguard Worker const MCAsmInfo *AsmInfo = TM->getMCAsmInfo();
613*9880d681SAndroid Build Coastguard Worker const BasicBlock *BB = MBB->getBasicBlock();
614*9880d681SAndroid Build Coastguard Worker const Function *Fn = MF->getFunction();
615*9880d681SAndroid Build Coastguard Worker if (LandingPadSuccs.size() > 1 &&
616*9880d681SAndroid Build Coastguard Worker !(AsmInfo &&
617*9880d681SAndroid Build Coastguard Worker AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj &&
618*9880d681SAndroid Build Coastguard Worker BB && isa<SwitchInst>(BB->getTerminator())) &&
619*9880d681SAndroid Build Coastguard Worker !isFuncletEHPersonality(classifyEHPersonality(Fn->getPersonalityFn())))
620*9880d681SAndroid Build Coastguard Worker report("MBB has more than one landing pad successor", MBB);
621*9880d681SAndroid Build Coastguard Worker
622*9880d681SAndroid Build Coastguard Worker // Call AnalyzeBranch. If it succeeds, there several more conditions to check.
623*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
624*9880d681SAndroid Build Coastguard Worker SmallVector<MachineOperand, 4> Cond;
625*9880d681SAndroid Build Coastguard Worker if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB,
626*9880d681SAndroid Build Coastguard Worker Cond)) {
627*9880d681SAndroid Build Coastguard Worker // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's
628*9880d681SAndroid Build Coastguard Worker // check whether its answers match up with reality.
629*9880d681SAndroid Build Coastguard Worker if (!TBB && !FBB) {
630*9880d681SAndroid Build Coastguard Worker // Block falls through to its successor.
631*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator MBBI = MBB->getIterator();
632*9880d681SAndroid Build Coastguard Worker ++MBBI;
633*9880d681SAndroid Build Coastguard Worker if (MBBI == MF->end()) {
634*9880d681SAndroid Build Coastguard Worker // It's possible that the block legitimately ends with a noreturn
635*9880d681SAndroid Build Coastguard Worker // call or an unreachable, in which case it won't actually fall
636*9880d681SAndroid Build Coastguard Worker // out the bottom of the function.
637*9880d681SAndroid Build Coastguard Worker } else if (MBB->succ_size() == LandingPadSuccs.size()) {
638*9880d681SAndroid Build Coastguard Worker // It's possible that the block legitimately ends with a noreturn
639*9880d681SAndroid Build Coastguard Worker // call or an unreachable, in which case it won't actuall fall
640*9880d681SAndroid Build Coastguard Worker // out of the block.
641*9880d681SAndroid Build Coastguard Worker } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) {
642*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional fall-through but doesn't have "
643*9880d681SAndroid Build Coastguard Worker "exactly one CFG successor!", MBB);
644*9880d681SAndroid Build Coastguard Worker } else if (!MBB->isSuccessor(&*MBBI)) {
645*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional fall-through but its successor "
646*9880d681SAndroid Build Coastguard Worker "differs from its CFG successor!", MBB);
647*9880d681SAndroid Build Coastguard Worker }
648*9880d681SAndroid Build Coastguard Worker if (!MBB->empty() && MBB->back().isBarrier() &&
649*9880d681SAndroid Build Coastguard Worker !TII->isPredicated(MBB->back())) {
650*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional fall-through but ends with a "
651*9880d681SAndroid Build Coastguard Worker "barrier instruction!", MBB);
652*9880d681SAndroid Build Coastguard Worker }
653*9880d681SAndroid Build Coastguard Worker if (!Cond.empty()) {
654*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional fall-through but has a condition!",
655*9880d681SAndroid Build Coastguard Worker MBB);
656*9880d681SAndroid Build Coastguard Worker }
657*9880d681SAndroid Build Coastguard Worker } else if (TBB && !FBB && Cond.empty()) {
658*9880d681SAndroid Build Coastguard Worker // Block unconditionally branches somewhere.
659*9880d681SAndroid Build Coastguard Worker // If the block has exactly one successor, that happens to be a
660*9880d681SAndroid Build Coastguard Worker // landingpad, accept it as valid control flow.
661*9880d681SAndroid Build Coastguard Worker if (MBB->succ_size() != 1+LandingPadSuccs.size() &&
662*9880d681SAndroid Build Coastguard Worker (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 ||
663*9880d681SAndroid Build Coastguard Worker *MBB->succ_begin() != *LandingPadSuccs.begin())) {
664*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional branch but doesn't have "
665*9880d681SAndroid Build Coastguard Worker "exactly one CFG successor!", MBB);
666*9880d681SAndroid Build Coastguard Worker } else if (!MBB->isSuccessor(TBB)) {
667*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional branch but the CFG "
668*9880d681SAndroid Build Coastguard Worker "successor doesn't match the actual successor!", MBB);
669*9880d681SAndroid Build Coastguard Worker }
670*9880d681SAndroid Build Coastguard Worker if (MBB->empty()) {
671*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional branch but doesn't contain "
672*9880d681SAndroid Build Coastguard Worker "any instructions!", MBB);
673*9880d681SAndroid Build Coastguard Worker } else if (!MBB->back().isBarrier()) {
674*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional branch but doesn't end with a "
675*9880d681SAndroid Build Coastguard Worker "barrier instruction!", MBB);
676*9880d681SAndroid Build Coastguard Worker } else if (!MBB->back().isTerminator()) {
677*9880d681SAndroid Build Coastguard Worker report("MBB exits via unconditional branch but the branch isn't a "
678*9880d681SAndroid Build Coastguard Worker "terminator instruction!", MBB);
679*9880d681SAndroid Build Coastguard Worker }
680*9880d681SAndroid Build Coastguard Worker } else if (TBB && !FBB && !Cond.empty()) {
681*9880d681SAndroid Build Coastguard Worker // Block conditionally branches somewhere, otherwise falls through.
682*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator MBBI = MBB->getIterator();
683*9880d681SAndroid Build Coastguard Worker ++MBBI;
684*9880d681SAndroid Build Coastguard Worker if (MBBI == MF->end()) {
685*9880d681SAndroid Build Coastguard Worker report("MBB conditionally falls through out of function!", MBB);
686*9880d681SAndroid Build Coastguard Worker } else if (MBB->succ_size() == 1) {
687*9880d681SAndroid Build Coastguard Worker // A conditional branch with only one successor is weird, but allowed.
688*9880d681SAndroid Build Coastguard Worker if (&*MBBI != TBB)
689*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but only has "
690*9880d681SAndroid Build Coastguard Worker "one CFG successor!", MBB);
691*9880d681SAndroid Build Coastguard Worker else if (TBB != *MBB->succ_begin())
692*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but the CFG "
693*9880d681SAndroid Build Coastguard Worker "successor don't match the actual successor!", MBB);
694*9880d681SAndroid Build Coastguard Worker } else if (MBB->succ_size() != 2) {
695*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but doesn't have "
696*9880d681SAndroid Build Coastguard Worker "exactly two CFG successors!", MBB);
697*9880d681SAndroid Build Coastguard Worker } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) {
698*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but the CFG "
699*9880d681SAndroid Build Coastguard Worker "successors don't match the actual successors!", MBB);
700*9880d681SAndroid Build Coastguard Worker }
701*9880d681SAndroid Build Coastguard Worker if (MBB->empty()) {
702*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but doesn't "
703*9880d681SAndroid Build Coastguard Worker "contain any instructions!", MBB);
704*9880d681SAndroid Build Coastguard Worker } else if (MBB->back().isBarrier()) {
705*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but ends with a "
706*9880d681SAndroid Build Coastguard Worker "barrier instruction!", MBB);
707*9880d681SAndroid Build Coastguard Worker } else if (!MBB->back().isTerminator()) {
708*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/fall-through but the branch "
709*9880d681SAndroid Build Coastguard Worker "isn't a terminator instruction!", MBB);
710*9880d681SAndroid Build Coastguard Worker }
711*9880d681SAndroid Build Coastguard Worker } else if (TBB && FBB) {
712*9880d681SAndroid Build Coastguard Worker // Block conditionally branches somewhere, otherwise branches
713*9880d681SAndroid Build Coastguard Worker // somewhere else.
714*9880d681SAndroid Build Coastguard Worker if (MBB->succ_size() == 1) {
715*9880d681SAndroid Build Coastguard Worker // A conditional branch with only one successor is weird, but allowed.
716*9880d681SAndroid Build Coastguard Worker if (FBB != TBB)
717*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch through but only has "
718*9880d681SAndroid Build Coastguard Worker "one CFG successor!", MBB);
719*9880d681SAndroid Build Coastguard Worker else if (TBB != *MBB->succ_begin())
720*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch through but the CFG "
721*9880d681SAndroid Build Coastguard Worker "successor don't match the actual successor!", MBB);
722*9880d681SAndroid Build Coastguard Worker } else if (MBB->succ_size() != 2) {
723*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch but doesn't have "
724*9880d681SAndroid Build Coastguard Worker "exactly two CFG successors!", MBB);
725*9880d681SAndroid Build Coastguard Worker } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) {
726*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch but the CFG "
727*9880d681SAndroid Build Coastguard Worker "successors don't match the actual successors!", MBB);
728*9880d681SAndroid Build Coastguard Worker }
729*9880d681SAndroid Build Coastguard Worker if (MBB->empty()) {
730*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch but doesn't "
731*9880d681SAndroid Build Coastguard Worker "contain any instructions!", MBB);
732*9880d681SAndroid Build Coastguard Worker } else if (!MBB->back().isBarrier()) {
733*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch but doesn't end with a "
734*9880d681SAndroid Build Coastguard Worker "barrier instruction!", MBB);
735*9880d681SAndroid Build Coastguard Worker } else if (!MBB->back().isTerminator()) {
736*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditional branch/branch but the branch "
737*9880d681SAndroid Build Coastguard Worker "isn't a terminator instruction!", MBB);
738*9880d681SAndroid Build Coastguard Worker }
739*9880d681SAndroid Build Coastguard Worker if (Cond.empty()) {
740*9880d681SAndroid Build Coastguard Worker report("MBB exits via conditinal branch/branch but there's no "
741*9880d681SAndroid Build Coastguard Worker "condition!", MBB);
742*9880d681SAndroid Build Coastguard Worker }
743*9880d681SAndroid Build Coastguard Worker } else {
744*9880d681SAndroid Build Coastguard Worker report("AnalyzeBranch returned invalid data!", MBB);
745*9880d681SAndroid Build Coastguard Worker }
746*9880d681SAndroid Build Coastguard Worker }
747*9880d681SAndroid Build Coastguard Worker
748*9880d681SAndroid Build Coastguard Worker regsLive.clear();
749*9880d681SAndroid Build Coastguard Worker for (const auto &LI : MBB->liveins()) {
750*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) {
751*9880d681SAndroid Build Coastguard Worker report("MBB live-in list contains non-physical register", MBB);
752*9880d681SAndroid Build Coastguard Worker continue;
753*9880d681SAndroid Build Coastguard Worker }
754*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true);
755*9880d681SAndroid Build Coastguard Worker SubRegs.isValid(); ++SubRegs)
756*9880d681SAndroid Build Coastguard Worker regsLive.insert(*SubRegs);
757*9880d681SAndroid Build Coastguard Worker }
758*9880d681SAndroid Build Coastguard Worker regsLiveInButUnused = regsLive;
759*9880d681SAndroid Build Coastguard Worker
760*9880d681SAndroid Build Coastguard Worker const MachineFrameInfo *MFI = MF->getFrameInfo();
761*9880d681SAndroid Build Coastguard Worker assert(MFI && "Function has no frame info");
762*9880d681SAndroid Build Coastguard Worker BitVector PR = MFI->getPristineRegs(*MF);
763*9880d681SAndroid Build Coastguard Worker for (int I = PR.find_first(); I>0; I = PR.find_next(I)) {
764*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true);
765*9880d681SAndroid Build Coastguard Worker SubRegs.isValid(); ++SubRegs)
766*9880d681SAndroid Build Coastguard Worker regsLive.insert(*SubRegs);
767*9880d681SAndroid Build Coastguard Worker }
768*9880d681SAndroid Build Coastguard Worker
769*9880d681SAndroid Build Coastguard Worker regsKilled.clear();
770*9880d681SAndroid Build Coastguard Worker regsDefined.clear();
771*9880d681SAndroid Build Coastguard Worker
772*9880d681SAndroid Build Coastguard Worker if (Indexes)
773*9880d681SAndroid Build Coastguard Worker lastIndex = Indexes->getMBBStartIdx(MBB);
774*9880d681SAndroid Build Coastguard Worker }
775*9880d681SAndroid Build Coastguard Worker
776*9880d681SAndroid Build Coastguard Worker // This function gets called for all bundle headers, including normal
777*9880d681SAndroid Build Coastguard Worker // stand-alone unbundled instructions.
visitMachineBundleBefore(const MachineInstr * MI)778*9880d681SAndroid Build Coastguard Worker void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) {
779*9880d681SAndroid Build Coastguard Worker if (Indexes && Indexes->hasIndex(*MI)) {
780*9880d681SAndroid Build Coastguard Worker SlotIndex idx = Indexes->getInstructionIndex(*MI);
781*9880d681SAndroid Build Coastguard Worker if (!(idx > lastIndex)) {
782*9880d681SAndroid Build Coastguard Worker report("Instruction index out of order", MI);
783*9880d681SAndroid Build Coastguard Worker errs() << "Last instruction was at " << lastIndex << '\n';
784*9880d681SAndroid Build Coastguard Worker }
785*9880d681SAndroid Build Coastguard Worker lastIndex = idx;
786*9880d681SAndroid Build Coastguard Worker }
787*9880d681SAndroid Build Coastguard Worker
788*9880d681SAndroid Build Coastguard Worker // Ensure non-terminators don't follow terminators.
789*9880d681SAndroid Build Coastguard Worker // Ignore predicated terminators formed by if conversion.
790*9880d681SAndroid Build Coastguard Worker // FIXME: If conversion shouldn't need to violate this rule.
791*9880d681SAndroid Build Coastguard Worker if (MI->isTerminator() && !TII->isPredicated(*MI)) {
792*9880d681SAndroid Build Coastguard Worker if (!FirstTerminator)
793*9880d681SAndroid Build Coastguard Worker FirstTerminator = MI;
794*9880d681SAndroid Build Coastguard Worker } else if (FirstTerminator) {
795*9880d681SAndroid Build Coastguard Worker report("Non-terminator instruction after the first terminator", MI);
796*9880d681SAndroid Build Coastguard Worker errs() << "First terminator was:\t" << *FirstTerminator;
797*9880d681SAndroid Build Coastguard Worker }
798*9880d681SAndroid Build Coastguard Worker }
799*9880d681SAndroid Build Coastguard Worker
800*9880d681SAndroid Build Coastguard Worker // The operands on an INLINEASM instruction must follow a template.
801*9880d681SAndroid Build Coastguard Worker // Verify that the flag operands make sense.
verifyInlineAsm(const MachineInstr * MI)802*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) {
803*9880d681SAndroid Build Coastguard Worker // The first two operands on INLINEASM are the asm string and global flags.
804*9880d681SAndroid Build Coastguard Worker if (MI->getNumOperands() < 2) {
805*9880d681SAndroid Build Coastguard Worker report("Too few operands on inline asm", MI);
806*9880d681SAndroid Build Coastguard Worker return;
807*9880d681SAndroid Build Coastguard Worker }
808*9880d681SAndroid Build Coastguard Worker if (!MI->getOperand(0).isSymbol())
809*9880d681SAndroid Build Coastguard Worker report("Asm string must be an external symbol", MI);
810*9880d681SAndroid Build Coastguard Worker if (!MI->getOperand(1).isImm())
811*9880d681SAndroid Build Coastguard Worker report("Asm flags must be an immediate", MI);
812*9880d681SAndroid Build Coastguard Worker // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2,
813*9880d681SAndroid Build Coastguard Worker // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16,
814*9880d681SAndroid Build Coastguard Worker // and Extra_IsConvergent = 32.
815*9880d681SAndroid Build Coastguard Worker if (!isUInt<6>(MI->getOperand(1).getImm()))
816*9880d681SAndroid Build Coastguard Worker report("Unknown asm flags", &MI->getOperand(1), 1);
817*9880d681SAndroid Build Coastguard Worker
818*9880d681SAndroid Build Coastguard Worker static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
819*9880d681SAndroid Build Coastguard Worker
820*9880d681SAndroid Build Coastguard Worker unsigned OpNo = InlineAsm::MIOp_FirstOperand;
821*9880d681SAndroid Build Coastguard Worker unsigned NumOps;
822*9880d681SAndroid Build Coastguard Worker for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) {
823*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI->getOperand(OpNo);
824*9880d681SAndroid Build Coastguard Worker // There may be implicit ops after the fixed operands.
825*9880d681SAndroid Build Coastguard Worker if (!MO.isImm())
826*9880d681SAndroid Build Coastguard Worker break;
827*9880d681SAndroid Build Coastguard Worker NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm());
828*9880d681SAndroid Build Coastguard Worker }
829*9880d681SAndroid Build Coastguard Worker
830*9880d681SAndroid Build Coastguard Worker if (OpNo > MI->getNumOperands())
831*9880d681SAndroid Build Coastguard Worker report("Missing operands in last group", MI);
832*9880d681SAndroid Build Coastguard Worker
833*9880d681SAndroid Build Coastguard Worker // An optional MDNode follows the groups.
834*9880d681SAndroid Build Coastguard Worker if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata())
835*9880d681SAndroid Build Coastguard Worker ++OpNo;
836*9880d681SAndroid Build Coastguard Worker
837*9880d681SAndroid Build Coastguard Worker // All trailing operands must be implicit registers.
838*9880d681SAndroid Build Coastguard Worker for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) {
839*9880d681SAndroid Build Coastguard Worker const MachineOperand &MO = MI->getOperand(OpNo);
840*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isImplicit())
841*9880d681SAndroid Build Coastguard Worker report("Expected implicit register after groups", &MO, OpNo);
842*9880d681SAndroid Build Coastguard Worker }
843*9880d681SAndroid Build Coastguard Worker }
844*9880d681SAndroid Build Coastguard Worker
visitMachineInstrBefore(const MachineInstr * MI)845*9880d681SAndroid Build Coastguard Worker void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) {
846*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = MI->getDesc();
847*9880d681SAndroid Build Coastguard Worker if (MI->getNumOperands() < MCID.getNumOperands()) {
848*9880d681SAndroid Build Coastguard Worker report("Too few operands", MI);
849*9880d681SAndroid Build Coastguard Worker errs() << MCID.getNumOperands() << " operands expected, but "
850*9880d681SAndroid Build Coastguard Worker << MI->getNumOperands() << " given.\n";
851*9880d681SAndroid Build Coastguard Worker }
852*9880d681SAndroid Build Coastguard Worker
853*9880d681SAndroid Build Coastguard Worker // Check the tied operands.
854*9880d681SAndroid Build Coastguard Worker if (MI->isInlineAsm())
855*9880d681SAndroid Build Coastguard Worker verifyInlineAsm(MI);
856*9880d681SAndroid Build Coastguard Worker
857*9880d681SAndroid Build Coastguard Worker // Check the MachineMemOperands for basic consistency.
858*9880d681SAndroid Build Coastguard Worker for (MachineInstr::mmo_iterator I = MI->memoperands_begin(),
859*9880d681SAndroid Build Coastguard Worker E = MI->memoperands_end(); I != E; ++I) {
860*9880d681SAndroid Build Coastguard Worker if ((*I)->isLoad() && !MI->mayLoad())
861*9880d681SAndroid Build Coastguard Worker report("Missing mayLoad flag", MI);
862*9880d681SAndroid Build Coastguard Worker if ((*I)->isStore() && !MI->mayStore())
863*9880d681SAndroid Build Coastguard Worker report("Missing mayStore flag", MI);
864*9880d681SAndroid Build Coastguard Worker }
865*9880d681SAndroid Build Coastguard Worker
866*9880d681SAndroid Build Coastguard Worker // Debug values must not have a slot index.
867*9880d681SAndroid Build Coastguard Worker // Other instructions must have one, unless they are inside a bundle.
868*9880d681SAndroid Build Coastguard Worker if (LiveInts) {
869*9880d681SAndroid Build Coastguard Worker bool mapped = !LiveInts->isNotInMIMap(*MI);
870*9880d681SAndroid Build Coastguard Worker if (MI->isDebugValue()) {
871*9880d681SAndroid Build Coastguard Worker if (mapped)
872*9880d681SAndroid Build Coastguard Worker report("Debug instruction has a slot index", MI);
873*9880d681SAndroid Build Coastguard Worker } else if (MI->isInsideBundle()) {
874*9880d681SAndroid Build Coastguard Worker if (mapped)
875*9880d681SAndroid Build Coastguard Worker report("Instruction inside bundle has a slot index", MI);
876*9880d681SAndroid Build Coastguard Worker } else {
877*9880d681SAndroid Build Coastguard Worker if (!mapped)
878*9880d681SAndroid Build Coastguard Worker report("Missing slot index", MI);
879*9880d681SAndroid Build Coastguard Worker }
880*9880d681SAndroid Build Coastguard Worker }
881*9880d681SAndroid Build Coastguard Worker
882*9880d681SAndroid Build Coastguard Worker StringRef ErrorInfo;
883*9880d681SAndroid Build Coastguard Worker if (!TII->verifyInstruction(*MI, ErrorInfo))
884*9880d681SAndroid Build Coastguard Worker report(ErrorInfo.data(), MI);
885*9880d681SAndroid Build Coastguard Worker }
886*9880d681SAndroid Build Coastguard Worker
887*9880d681SAndroid Build Coastguard Worker void
visitMachineOperand(const MachineOperand * MO,unsigned MONum)888*9880d681SAndroid Build Coastguard Worker MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) {
889*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI = MO->getParent();
890*9880d681SAndroid Build Coastguard Worker const MCInstrDesc &MCID = MI->getDesc();
891*9880d681SAndroid Build Coastguard Worker unsigned NumDefs = MCID.getNumDefs();
892*9880d681SAndroid Build Coastguard Worker if (MCID.getOpcode() == TargetOpcode::PATCHPOINT)
893*9880d681SAndroid Build Coastguard Worker NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0;
894*9880d681SAndroid Build Coastguard Worker
895*9880d681SAndroid Build Coastguard Worker // The first MCID.NumDefs operands must be explicit register defines
896*9880d681SAndroid Build Coastguard Worker if (MONum < NumDefs) {
897*9880d681SAndroid Build Coastguard Worker const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
898*9880d681SAndroid Build Coastguard Worker if (!MO->isReg())
899*9880d681SAndroid Build Coastguard Worker report("Explicit definition must be a register", MO, MONum);
900*9880d681SAndroid Build Coastguard Worker else if (!MO->isDef() && !MCOI.isOptionalDef())
901*9880d681SAndroid Build Coastguard Worker report("Explicit definition marked as use", MO, MONum);
902*9880d681SAndroid Build Coastguard Worker else if (MO->isImplicit())
903*9880d681SAndroid Build Coastguard Worker report("Explicit definition marked as implicit", MO, MONum);
904*9880d681SAndroid Build Coastguard Worker } else if (MONum < MCID.getNumOperands()) {
905*9880d681SAndroid Build Coastguard Worker const MCOperandInfo &MCOI = MCID.OpInfo[MONum];
906*9880d681SAndroid Build Coastguard Worker // Don't check if it's the last operand in a variadic instruction. See,
907*9880d681SAndroid Build Coastguard Worker // e.g., LDM_RET in the arm back end.
908*9880d681SAndroid Build Coastguard Worker if (MO->isReg() &&
909*9880d681SAndroid Build Coastguard Worker !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) {
910*9880d681SAndroid Build Coastguard Worker if (MO->isDef() && !MCOI.isOptionalDef())
911*9880d681SAndroid Build Coastguard Worker report("Explicit operand marked as def", MO, MONum);
912*9880d681SAndroid Build Coastguard Worker if (MO->isImplicit())
913*9880d681SAndroid Build Coastguard Worker report("Explicit operand marked as implicit", MO, MONum);
914*9880d681SAndroid Build Coastguard Worker }
915*9880d681SAndroid Build Coastguard Worker
916*9880d681SAndroid Build Coastguard Worker int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO);
917*9880d681SAndroid Build Coastguard Worker if (TiedTo != -1) {
918*9880d681SAndroid Build Coastguard Worker if (!MO->isReg())
919*9880d681SAndroid Build Coastguard Worker report("Tied use must be a register", MO, MONum);
920*9880d681SAndroid Build Coastguard Worker else if (!MO->isTied())
921*9880d681SAndroid Build Coastguard Worker report("Operand should be tied", MO, MONum);
922*9880d681SAndroid Build Coastguard Worker else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum))
923*9880d681SAndroid Build Coastguard Worker report("Tied def doesn't match MCInstrDesc", MO, MONum);
924*9880d681SAndroid Build Coastguard Worker } else if (MO->isReg() && MO->isTied())
925*9880d681SAndroid Build Coastguard Worker report("Explicit operand should not be tied", MO, MONum);
926*9880d681SAndroid Build Coastguard Worker } else {
927*9880d681SAndroid Build Coastguard Worker // ARM adds %reg0 operands to indicate predicates. We'll allow that.
928*9880d681SAndroid Build Coastguard Worker if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg())
929*9880d681SAndroid Build Coastguard Worker report("Extra explicit operand on non-variadic instruction", MO, MONum);
930*9880d681SAndroid Build Coastguard Worker }
931*9880d681SAndroid Build Coastguard Worker
932*9880d681SAndroid Build Coastguard Worker switch (MO->getType()) {
933*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_Register: {
934*9880d681SAndroid Build Coastguard Worker const unsigned Reg = MO->getReg();
935*9880d681SAndroid Build Coastguard Worker if (!Reg)
936*9880d681SAndroid Build Coastguard Worker return;
937*9880d681SAndroid Build Coastguard Worker if (MRI->tracksLiveness() && !MI->isDebugValue())
938*9880d681SAndroid Build Coastguard Worker checkLiveness(MO, MONum);
939*9880d681SAndroid Build Coastguard Worker
940*9880d681SAndroid Build Coastguard Worker // Verify the consistency of tied operands.
941*9880d681SAndroid Build Coastguard Worker if (MO->isTied()) {
942*9880d681SAndroid Build Coastguard Worker unsigned OtherIdx = MI->findTiedOperandIdx(MONum);
943*9880d681SAndroid Build Coastguard Worker const MachineOperand &OtherMO = MI->getOperand(OtherIdx);
944*9880d681SAndroid Build Coastguard Worker if (!OtherMO.isReg())
945*9880d681SAndroid Build Coastguard Worker report("Must be tied to a register", MO, MONum);
946*9880d681SAndroid Build Coastguard Worker if (!OtherMO.isTied())
947*9880d681SAndroid Build Coastguard Worker report("Missing tie flags on tied operand", MO, MONum);
948*9880d681SAndroid Build Coastguard Worker if (MI->findTiedOperandIdx(OtherIdx) != MONum)
949*9880d681SAndroid Build Coastguard Worker report("Inconsistent tie links", MO, MONum);
950*9880d681SAndroid Build Coastguard Worker if (MONum < MCID.getNumDefs()) {
951*9880d681SAndroid Build Coastguard Worker if (OtherIdx < MCID.getNumOperands()) {
952*9880d681SAndroid Build Coastguard Worker if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO))
953*9880d681SAndroid Build Coastguard Worker report("Explicit def tied to explicit use without tie constraint",
954*9880d681SAndroid Build Coastguard Worker MO, MONum);
955*9880d681SAndroid Build Coastguard Worker } else {
956*9880d681SAndroid Build Coastguard Worker if (!OtherMO.isImplicit())
957*9880d681SAndroid Build Coastguard Worker report("Explicit def should be tied to implicit use", MO, MONum);
958*9880d681SAndroid Build Coastguard Worker }
959*9880d681SAndroid Build Coastguard Worker }
960*9880d681SAndroid Build Coastguard Worker }
961*9880d681SAndroid Build Coastguard Worker
962*9880d681SAndroid Build Coastguard Worker // Verify two-address constraints after leaving SSA form.
963*9880d681SAndroid Build Coastguard Worker unsigned DefIdx;
964*9880d681SAndroid Build Coastguard Worker if (!MRI->isSSA() && MO->isUse() &&
965*9880d681SAndroid Build Coastguard Worker MI->isRegTiedToDefOperand(MONum, &DefIdx) &&
966*9880d681SAndroid Build Coastguard Worker Reg != MI->getOperand(DefIdx).getReg())
967*9880d681SAndroid Build Coastguard Worker report("Two-address instruction operands must be identical", MO, MONum);
968*9880d681SAndroid Build Coastguard Worker
969*9880d681SAndroid Build Coastguard Worker // Check register classes.
970*9880d681SAndroid Build Coastguard Worker if (MONum < MCID.getNumOperands() && !MO->isImplicit()) {
971*9880d681SAndroid Build Coastguard Worker unsigned SubIdx = MO->getSubReg();
972*9880d681SAndroid Build Coastguard Worker
973*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
974*9880d681SAndroid Build Coastguard Worker if (SubIdx) {
975*9880d681SAndroid Build Coastguard Worker report("Illegal subregister index for physical register", MO, MONum);
976*9880d681SAndroid Build Coastguard Worker return;
977*9880d681SAndroid Build Coastguard Worker }
978*9880d681SAndroid Build Coastguard Worker if (const TargetRegisterClass *DRC =
979*9880d681SAndroid Build Coastguard Worker TII->getRegClass(MCID, MONum, TRI, *MF)) {
980*9880d681SAndroid Build Coastguard Worker if (!DRC->contains(Reg)) {
981*9880d681SAndroid Build Coastguard Worker report("Illegal physical register for instruction", MO, MONum);
982*9880d681SAndroid Build Coastguard Worker errs() << TRI->getName(Reg) << " is not a "
983*9880d681SAndroid Build Coastguard Worker << TRI->getRegClassName(DRC) << " register.\n";
984*9880d681SAndroid Build Coastguard Worker }
985*9880d681SAndroid Build Coastguard Worker }
986*9880d681SAndroid Build Coastguard Worker } else {
987*9880d681SAndroid Build Coastguard Worker // Virtual register.
988*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg);
989*9880d681SAndroid Build Coastguard Worker if (!RC) {
990*9880d681SAndroid Build Coastguard Worker // This is a generic virtual register.
991*9880d681SAndroid Build Coastguard Worker // It must have a size and it must not have a SubIdx.
992*9880d681SAndroid Build Coastguard Worker unsigned Size = MRI->getSize(Reg);
993*9880d681SAndroid Build Coastguard Worker if (!Size) {
994*9880d681SAndroid Build Coastguard Worker report("Generic virtual register must have a size", MO, MONum);
995*9880d681SAndroid Build Coastguard Worker return;
996*9880d681SAndroid Build Coastguard Worker }
997*9880d681SAndroid Build Coastguard Worker // Make sure the register fits into its register bank if any.
998*9880d681SAndroid Build Coastguard Worker const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg);
999*9880d681SAndroid Build Coastguard Worker if (RegBank && RegBank->getSize() < Size) {
1000*9880d681SAndroid Build Coastguard Worker report("Register bank is too small for virtual register", MO,
1001*9880d681SAndroid Build Coastguard Worker MONum);
1002*9880d681SAndroid Build Coastguard Worker errs() << "Register bank " << RegBank->getName() << " too small("
1003*9880d681SAndroid Build Coastguard Worker << RegBank->getSize() << ") to fit " << Size << "-bits\n";
1004*9880d681SAndroid Build Coastguard Worker return;
1005*9880d681SAndroid Build Coastguard Worker }
1006*9880d681SAndroid Build Coastguard Worker if (SubIdx) {
1007*9880d681SAndroid Build Coastguard Worker report("Generic virtual register does not subregister index", MO, MONum);
1008*9880d681SAndroid Build Coastguard Worker return;
1009*9880d681SAndroid Build Coastguard Worker }
1010*9880d681SAndroid Build Coastguard Worker break;
1011*9880d681SAndroid Build Coastguard Worker }
1012*9880d681SAndroid Build Coastguard Worker if (SubIdx) {
1013*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *SRC =
1014*9880d681SAndroid Build Coastguard Worker TRI->getSubClassWithSubReg(RC, SubIdx);
1015*9880d681SAndroid Build Coastguard Worker if (!SRC) {
1016*9880d681SAndroid Build Coastguard Worker report("Invalid subregister index for virtual register", MO, MONum);
1017*9880d681SAndroid Build Coastguard Worker errs() << "Register class " << TRI->getRegClassName(RC)
1018*9880d681SAndroid Build Coastguard Worker << " does not support subreg index " << SubIdx << "\n";
1019*9880d681SAndroid Build Coastguard Worker return;
1020*9880d681SAndroid Build Coastguard Worker }
1021*9880d681SAndroid Build Coastguard Worker if (RC != SRC) {
1022*9880d681SAndroid Build Coastguard Worker report("Invalid register class for subregister index", MO, MONum);
1023*9880d681SAndroid Build Coastguard Worker errs() << "Register class " << TRI->getRegClassName(RC)
1024*9880d681SAndroid Build Coastguard Worker << " does not fully support subreg index " << SubIdx << "\n";
1025*9880d681SAndroid Build Coastguard Worker return;
1026*9880d681SAndroid Build Coastguard Worker }
1027*9880d681SAndroid Build Coastguard Worker }
1028*9880d681SAndroid Build Coastguard Worker if (const TargetRegisterClass *DRC =
1029*9880d681SAndroid Build Coastguard Worker TII->getRegClass(MCID, MONum, TRI, *MF)) {
1030*9880d681SAndroid Build Coastguard Worker if (SubIdx) {
1031*9880d681SAndroid Build Coastguard Worker const TargetRegisterClass *SuperRC =
1032*9880d681SAndroid Build Coastguard Worker TRI->getLargestLegalSuperClass(RC, *MF);
1033*9880d681SAndroid Build Coastguard Worker if (!SuperRC) {
1034*9880d681SAndroid Build Coastguard Worker report("No largest legal super class exists.", MO, MONum);
1035*9880d681SAndroid Build Coastguard Worker return;
1036*9880d681SAndroid Build Coastguard Worker }
1037*9880d681SAndroid Build Coastguard Worker DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx);
1038*9880d681SAndroid Build Coastguard Worker if (!DRC) {
1039*9880d681SAndroid Build Coastguard Worker report("No matching super-reg register class.", MO, MONum);
1040*9880d681SAndroid Build Coastguard Worker return;
1041*9880d681SAndroid Build Coastguard Worker }
1042*9880d681SAndroid Build Coastguard Worker }
1043*9880d681SAndroid Build Coastguard Worker if (!RC->hasSuperClassEq(DRC)) {
1044*9880d681SAndroid Build Coastguard Worker report("Illegal virtual register for instruction", MO, MONum);
1045*9880d681SAndroid Build Coastguard Worker errs() << "Expected a " << TRI->getRegClassName(DRC)
1046*9880d681SAndroid Build Coastguard Worker << " register, but got a " << TRI->getRegClassName(RC)
1047*9880d681SAndroid Build Coastguard Worker << " register\n";
1048*9880d681SAndroid Build Coastguard Worker }
1049*9880d681SAndroid Build Coastguard Worker }
1050*9880d681SAndroid Build Coastguard Worker }
1051*9880d681SAndroid Build Coastguard Worker }
1052*9880d681SAndroid Build Coastguard Worker break;
1053*9880d681SAndroid Build Coastguard Worker }
1054*9880d681SAndroid Build Coastguard Worker
1055*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_RegisterMask:
1056*9880d681SAndroid Build Coastguard Worker regMasks.push_back(MO->getRegMask());
1057*9880d681SAndroid Build Coastguard Worker break;
1058*9880d681SAndroid Build Coastguard Worker
1059*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_MachineBasicBlock:
1060*9880d681SAndroid Build Coastguard Worker if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent()))
1061*9880d681SAndroid Build Coastguard Worker report("PHI operand is not in the CFG", MO, MONum);
1062*9880d681SAndroid Build Coastguard Worker break;
1063*9880d681SAndroid Build Coastguard Worker
1064*9880d681SAndroid Build Coastguard Worker case MachineOperand::MO_FrameIndex:
1065*9880d681SAndroid Build Coastguard Worker if (LiveStks && LiveStks->hasInterval(MO->getIndex()) &&
1066*9880d681SAndroid Build Coastguard Worker LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1067*9880d681SAndroid Build Coastguard Worker int FI = MO->getIndex();
1068*9880d681SAndroid Build Coastguard Worker LiveInterval &LI = LiveStks->getInterval(FI);
1069*9880d681SAndroid Build Coastguard Worker SlotIndex Idx = LiveInts->getInstructionIndex(*MI);
1070*9880d681SAndroid Build Coastguard Worker
1071*9880d681SAndroid Build Coastguard Worker bool stores = MI->mayStore();
1072*9880d681SAndroid Build Coastguard Worker bool loads = MI->mayLoad();
1073*9880d681SAndroid Build Coastguard Worker // For a memory-to-memory move, we need to check if the frame
1074*9880d681SAndroid Build Coastguard Worker // index is used for storing or loading, by inspecting the
1075*9880d681SAndroid Build Coastguard Worker // memory operands.
1076*9880d681SAndroid Build Coastguard Worker if (stores && loads) {
1077*9880d681SAndroid Build Coastguard Worker for (auto *MMO : MI->memoperands()) {
1078*9880d681SAndroid Build Coastguard Worker const PseudoSourceValue *PSV = MMO->getPseudoValue();
1079*9880d681SAndroid Build Coastguard Worker if (PSV == nullptr) continue;
1080*9880d681SAndroid Build Coastguard Worker const FixedStackPseudoSourceValue *Value =
1081*9880d681SAndroid Build Coastguard Worker dyn_cast<FixedStackPseudoSourceValue>(PSV);
1082*9880d681SAndroid Build Coastguard Worker if (Value == nullptr) continue;
1083*9880d681SAndroid Build Coastguard Worker if (Value->getFrameIndex() != FI) continue;
1084*9880d681SAndroid Build Coastguard Worker
1085*9880d681SAndroid Build Coastguard Worker if (MMO->isStore())
1086*9880d681SAndroid Build Coastguard Worker loads = false;
1087*9880d681SAndroid Build Coastguard Worker else
1088*9880d681SAndroid Build Coastguard Worker stores = false;
1089*9880d681SAndroid Build Coastguard Worker break;
1090*9880d681SAndroid Build Coastguard Worker }
1091*9880d681SAndroid Build Coastguard Worker if (loads == stores)
1092*9880d681SAndroid Build Coastguard Worker report("Missing fixed stack memoperand.", MI);
1093*9880d681SAndroid Build Coastguard Worker }
1094*9880d681SAndroid Build Coastguard Worker if (loads && !LI.liveAt(Idx.getRegSlot(true))) {
1095*9880d681SAndroid Build Coastguard Worker report("Instruction loads from dead spill slot", MO, MONum);
1096*9880d681SAndroid Build Coastguard Worker errs() << "Live stack: " << LI << '\n';
1097*9880d681SAndroid Build Coastguard Worker }
1098*9880d681SAndroid Build Coastguard Worker if (stores && !LI.liveAt(Idx.getRegSlot())) {
1099*9880d681SAndroid Build Coastguard Worker report("Instruction stores to dead spill slot", MO, MONum);
1100*9880d681SAndroid Build Coastguard Worker errs() << "Live stack: " << LI << '\n';
1101*9880d681SAndroid Build Coastguard Worker }
1102*9880d681SAndroid Build Coastguard Worker }
1103*9880d681SAndroid Build Coastguard Worker break;
1104*9880d681SAndroid Build Coastguard Worker
1105*9880d681SAndroid Build Coastguard Worker default:
1106*9880d681SAndroid Build Coastguard Worker break;
1107*9880d681SAndroid Build Coastguard Worker }
1108*9880d681SAndroid Build Coastguard Worker }
1109*9880d681SAndroid Build Coastguard Worker
checkLivenessAtUse(const MachineOperand * MO,unsigned MONum,SlotIndex UseIdx,const LiveRange & LR,unsigned VRegOrUnit,LaneBitmask LaneMask)1110*9880d681SAndroid Build Coastguard Worker void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO,
1111*9880d681SAndroid Build Coastguard Worker unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit,
1112*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask) {
1113*9880d681SAndroid Build Coastguard Worker LiveQueryResult LRQ = LR.Query(UseIdx);
1114*9880d681SAndroid Build Coastguard Worker // Check if we have a segment at the use, note however that we only need one
1115*9880d681SAndroid Build Coastguard Worker // live subregister range, the others may be dead.
1116*9880d681SAndroid Build Coastguard Worker if (!LRQ.valueIn() && LaneMask == 0) {
1117*9880d681SAndroid Build Coastguard Worker report("No live segment at use", MO, MONum);
1118*9880d681SAndroid Build Coastguard Worker report_context_liverange(LR);
1119*9880d681SAndroid Build Coastguard Worker report_context_vreg_regunit(VRegOrUnit);
1120*9880d681SAndroid Build Coastguard Worker report_context(UseIdx);
1121*9880d681SAndroid Build Coastguard Worker }
1122*9880d681SAndroid Build Coastguard Worker if (MO->isKill() && !LRQ.isKill()) {
1123*9880d681SAndroid Build Coastguard Worker report("Live range continues after kill flag", MO, MONum);
1124*9880d681SAndroid Build Coastguard Worker report_context_liverange(LR);
1125*9880d681SAndroid Build Coastguard Worker report_context_vreg_regunit(VRegOrUnit);
1126*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0)
1127*9880d681SAndroid Build Coastguard Worker report_context_lanemask(LaneMask);
1128*9880d681SAndroid Build Coastguard Worker report_context(UseIdx);
1129*9880d681SAndroid Build Coastguard Worker }
1130*9880d681SAndroid Build Coastguard Worker }
1131*9880d681SAndroid Build Coastguard Worker
checkLivenessAtDef(const MachineOperand * MO,unsigned MONum,SlotIndex DefIdx,const LiveRange & LR,unsigned VRegOrUnit,LaneBitmask LaneMask)1132*9880d681SAndroid Build Coastguard Worker void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO,
1133*9880d681SAndroid Build Coastguard Worker unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit,
1134*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask) {
1135*9880d681SAndroid Build Coastguard Worker if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) {
1136*9880d681SAndroid Build Coastguard Worker assert(VNI && "NULL valno is not allowed");
1137*9880d681SAndroid Build Coastguard Worker if (VNI->def != DefIdx) {
1138*9880d681SAndroid Build Coastguard Worker report("Inconsistent valno->def", MO, MONum);
1139*9880d681SAndroid Build Coastguard Worker report_context_liverange(LR);
1140*9880d681SAndroid Build Coastguard Worker report_context_vreg_regunit(VRegOrUnit);
1141*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0)
1142*9880d681SAndroid Build Coastguard Worker report_context_lanemask(LaneMask);
1143*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1144*9880d681SAndroid Build Coastguard Worker report_context(DefIdx);
1145*9880d681SAndroid Build Coastguard Worker }
1146*9880d681SAndroid Build Coastguard Worker } else {
1147*9880d681SAndroid Build Coastguard Worker report("No live segment at def", MO, MONum);
1148*9880d681SAndroid Build Coastguard Worker report_context_liverange(LR);
1149*9880d681SAndroid Build Coastguard Worker report_context_vreg_regunit(VRegOrUnit);
1150*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0)
1151*9880d681SAndroid Build Coastguard Worker report_context_lanemask(LaneMask);
1152*9880d681SAndroid Build Coastguard Worker report_context(DefIdx);
1153*9880d681SAndroid Build Coastguard Worker }
1154*9880d681SAndroid Build Coastguard Worker // Check that, if the dead def flag is present, LiveInts agree.
1155*9880d681SAndroid Build Coastguard Worker if (MO->isDead()) {
1156*9880d681SAndroid Build Coastguard Worker LiveQueryResult LRQ = LR.Query(DefIdx);
1157*9880d681SAndroid Build Coastguard Worker if (!LRQ.isDeadDef()) {
1158*9880d681SAndroid Build Coastguard Worker // In case of physregs we can have a non-dead definition on another
1159*9880d681SAndroid Build Coastguard Worker // operand.
1160*9880d681SAndroid Build Coastguard Worker bool otherDef = false;
1161*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) {
1162*9880d681SAndroid Build Coastguard Worker const MachineInstr &MI = *MO->getParent();
1163*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MO : MI.operands()) {
1164*9880d681SAndroid Build Coastguard Worker if (!MO.isReg() || !MO.isDef() || MO.isDead())
1165*9880d681SAndroid Build Coastguard Worker continue;
1166*9880d681SAndroid Build Coastguard Worker unsigned Reg = MO.getReg();
1167*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1168*9880d681SAndroid Build Coastguard Worker if (*Units == VRegOrUnit) {
1169*9880d681SAndroid Build Coastguard Worker otherDef = true;
1170*9880d681SAndroid Build Coastguard Worker break;
1171*9880d681SAndroid Build Coastguard Worker }
1172*9880d681SAndroid Build Coastguard Worker }
1173*9880d681SAndroid Build Coastguard Worker }
1174*9880d681SAndroid Build Coastguard Worker }
1175*9880d681SAndroid Build Coastguard Worker
1176*9880d681SAndroid Build Coastguard Worker if (!otherDef) {
1177*9880d681SAndroid Build Coastguard Worker report("Live range continues after dead def flag", MO, MONum);
1178*9880d681SAndroid Build Coastguard Worker report_context_liverange(LR);
1179*9880d681SAndroid Build Coastguard Worker report_context_vreg_regunit(VRegOrUnit);
1180*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0)
1181*9880d681SAndroid Build Coastguard Worker report_context_lanemask(LaneMask);
1182*9880d681SAndroid Build Coastguard Worker }
1183*9880d681SAndroid Build Coastguard Worker }
1184*9880d681SAndroid Build Coastguard Worker }
1185*9880d681SAndroid Build Coastguard Worker }
1186*9880d681SAndroid Build Coastguard Worker
checkLiveness(const MachineOperand * MO,unsigned MONum)1187*9880d681SAndroid Build Coastguard Worker void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) {
1188*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI = MO->getParent();
1189*9880d681SAndroid Build Coastguard Worker const unsigned Reg = MO->getReg();
1190*9880d681SAndroid Build Coastguard Worker
1191*9880d681SAndroid Build Coastguard Worker // Both use and def operands can read a register.
1192*9880d681SAndroid Build Coastguard Worker if (MO->readsReg()) {
1193*9880d681SAndroid Build Coastguard Worker regsLiveInButUnused.erase(Reg);
1194*9880d681SAndroid Build Coastguard Worker
1195*9880d681SAndroid Build Coastguard Worker if (MO->isKill())
1196*9880d681SAndroid Build Coastguard Worker addRegWithSubRegs(regsKilled, Reg);
1197*9880d681SAndroid Build Coastguard Worker
1198*9880d681SAndroid Build Coastguard Worker // Check that LiveVars knows this kill.
1199*9880d681SAndroid Build Coastguard Worker if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) &&
1200*9880d681SAndroid Build Coastguard Worker MO->isKill()) {
1201*9880d681SAndroid Build Coastguard Worker LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1202*9880d681SAndroid Build Coastguard Worker if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end())
1203*9880d681SAndroid Build Coastguard Worker report("Kill missing from LiveVariables", MO, MONum);
1204*9880d681SAndroid Build Coastguard Worker }
1205*9880d681SAndroid Build Coastguard Worker
1206*9880d681SAndroid Build Coastguard Worker // Check LiveInts liveness and kill.
1207*9880d681SAndroid Build Coastguard Worker if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1208*9880d681SAndroid Build Coastguard Worker SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI);
1209*9880d681SAndroid Build Coastguard Worker // Check the cached regunit intervals.
1210*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) {
1211*9880d681SAndroid Build Coastguard Worker for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
1212*9880d681SAndroid Build Coastguard Worker if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units))
1213*9880d681SAndroid Build Coastguard Worker checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units);
1214*9880d681SAndroid Build Coastguard Worker }
1215*9880d681SAndroid Build Coastguard Worker }
1216*9880d681SAndroid Build Coastguard Worker
1217*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1218*9880d681SAndroid Build Coastguard Worker if (LiveInts->hasInterval(Reg)) {
1219*9880d681SAndroid Build Coastguard Worker // This is a virtual register interval.
1220*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LiveInts->getInterval(Reg);
1221*9880d681SAndroid Build Coastguard Worker checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg);
1222*9880d681SAndroid Build Coastguard Worker
1223*9880d681SAndroid Build Coastguard Worker if (LI.hasSubRanges() && !MO->isDef()) {
1224*9880d681SAndroid Build Coastguard Worker unsigned SubRegIdx = MO->getSubReg();
1225*9880d681SAndroid Build Coastguard Worker LaneBitmask MOMask = SubRegIdx != 0
1226*9880d681SAndroid Build Coastguard Worker ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1227*9880d681SAndroid Build Coastguard Worker : MRI->getMaxLaneMaskForVReg(Reg);
1228*9880d681SAndroid Build Coastguard Worker LaneBitmask LiveInMask = 0;
1229*9880d681SAndroid Build Coastguard Worker for (const LiveInterval::SubRange &SR : LI.subranges()) {
1230*9880d681SAndroid Build Coastguard Worker if ((MOMask & SR.LaneMask) == 0)
1231*9880d681SAndroid Build Coastguard Worker continue;
1232*9880d681SAndroid Build Coastguard Worker checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask);
1233*9880d681SAndroid Build Coastguard Worker LiveQueryResult LRQ = SR.Query(UseIdx);
1234*9880d681SAndroid Build Coastguard Worker if (LRQ.valueIn())
1235*9880d681SAndroid Build Coastguard Worker LiveInMask |= SR.LaneMask;
1236*9880d681SAndroid Build Coastguard Worker }
1237*9880d681SAndroid Build Coastguard Worker // At least parts of the register has to be live at the use.
1238*9880d681SAndroid Build Coastguard Worker if ((LiveInMask & MOMask) == 0) {
1239*9880d681SAndroid Build Coastguard Worker report("No live subrange at use", MO, MONum);
1240*9880d681SAndroid Build Coastguard Worker report_context(LI);
1241*9880d681SAndroid Build Coastguard Worker report_context(UseIdx);
1242*9880d681SAndroid Build Coastguard Worker }
1243*9880d681SAndroid Build Coastguard Worker }
1244*9880d681SAndroid Build Coastguard Worker } else {
1245*9880d681SAndroid Build Coastguard Worker report("Virtual register has no live interval", MO, MONum);
1246*9880d681SAndroid Build Coastguard Worker }
1247*9880d681SAndroid Build Coastguard Worker }
1248*9880d681SAndroid Build Coastguard Worker }
1249*9880d681SAndroid Build Coastguard Worker
1250*9880d681SAndroid Build Coastguard Worker // Use of a dead register.
1251*9880d681SAndroid Build Coastguard Worker if (!regsLive.count(Reg)) {
1252*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
1253*9880d681SAndroid Build Coastguard Worker // Reserved registers may be used even when 'dead'.
1254*9880d681SAndroid Build Coastguard Worker bool Bad = !isReserved(Reg);
1255*9880d681SAndroid Build Coastguard Worker // We are fine if just any subregister has a defined value.
1256*9880d681SAndroid Build Coastguard Worker if (Bad) {
1257*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid();
1258*9880d681SAndroid Build Coastguard Worker ++SubRegs) {
1259*9880d681SAndroid Build Coastguard Worker if (regsLive.count(*SubRegs)) {
1260*9880d681SAndroid Build Coastguard Worker Bad = false;
1261*9880d681SAndroid Build Coastguard Worker break;
1262*9880d681SAndroid Build Coastguard Worker }
1263*9880d681SAndroid Build Coastguard Worker }
1264*9880d681SAndroid Build Coastguard Worker }
1265*9880d681SAndroid Build Coastguard Worker // If there is an additional implicit-use of a super register we stop
1266*9880d681SAndroid Build Coastguard Worker // here. By definition we are fine if the super register is not
1267*9880d681SAndroid Build Coastguard Worker // (completely) dead, if the complete super register is dead we will
1268*9880d681SAndroid Build Coastguard Worker // get a report for its operand.
1269*9880d681SAndroid Build Coastguard Worker if (Bad) {
1270*9880d681SAndroid Build Coastguard Worker for (const MachineOperand &MOP : MI->uses()) {
1271*9880d681SAndroid Build Coastguard Worker if (!MOP.isReg())
1272*9880d681SAndroid Build Coastguard Worker continue;
1273*9880d681SAndroid Build Coastguard Worker if (!MOP.isImplicit())
1274*9880d681SAndroid Build Coastguard Worker continue;
1275*9880d681SAndroid Build Coastguard Worker for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid();
1276*9880d681SAndroid Build Coastguard Worker ++SubRegs) {
1277*9880d681SAndroid Build Coastguard Worker if (*SubRegs == Reg) {
1278*9880d681SAndroid Build Coastguard Worker Bad = false;
1279*9880d681SAndroid Build Coastguard Worker break;
1280*9880d681SAndroid Build Coastguard Worker }
1281*9880d681SAndroid Build Coastguard Worker }
1282*9880d681SAndroid Build Coastguard Worker }
1283*9880d681SAndroid Build Coastguard Worker }
1284*9880d681SAndroid Build Coastguard Worker if (Bad)
1285*9880d681SAndroid Build Coastguard Worker report("Using an undefined physical register", MO, MONum);
1286*9880d681SAndroid Build Coastguard Worker } else if (MRI->def_empty(Reg)) {
1287*9880d681SAndroid Build Coastguard Worker report("Reading virtual register without a def", MO, MONum);
1288*9880d681SAndroid Build Coastguard Worker } else {
1289*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1290*9880d681SAndroid Build Coastguard Worker // We don't know which virtual registers are live in, so only complain
1291*9880d681SAndroid Build Coastguard Worker // if vreg was killed in this MBB. Otherwise keep track of vregs that
1292*9880d681SAndroid Build Coastguard Worker // must be live in. PHI instructions are handled separately.
1293*9880d681SAndroid Build Coastguard Worker if (MInfo.regsKilled.count(Reg))
1294*9880d681SAndroid Build Coastguard Worker report("Using a killed virtual register", MO, MONum);
1295*9880d681SAndroid Build Coastguard Worker else if (!MI->isPHI())
1296*9880d681SAndroid Build Coastguard Worker MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI));
1297*9880d681SAndroid Build Coastguard Worker }
1298*9880d681SAndroid Build Coastguard Worker }
1299*9880d681SAndroid Build Coastguard Worker }
1300*9880d681SAndroid Build Coastguard Worker
1301*9880d681SAndroid Build Coastguard Worker if (MO->isDef()) {
1302*9880d681SAndroid Build Coastguard Worker // Register defined.
1303*9880d681SAndroid Build Coastguard Worker // TODO: verify that earlyclobber ops are not used.
1304*9880d681SAndroid Build Coastguard Worker if (MO->isDead())
1305*9880d681SAndroid Build Coastguard Worker addRegWithSubRegs(regsDead, Reg);
1306*9880d681SAndroid Build Coastguard Worker else
1307*9880d681SAndroid Build Coastguard Worker addRegWithSubRegs(regsDefined, Reg);
1308*9880d681SAndroid Build Coastguard Worker
1309*9880d681SAndroid Build Coastguard Worker // Verify SSA form.
1310*9880d681SAndroid Build Coastguard Worker if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) &&
1311*9880d681SAndroid Build Coastguard Worker std::next(MRI->def_begin(Reg)) != MRI->def_end())
1312*9880d681SAndroid Build Coastguard Worker report("Multiple virtual register defs in SSA form", MO, MONum);
1313*9880d681SAndroid Build Coastguard Worker
1314*9880d681SAndroid Build Coastguard Worker // Check LiveInts for a live segment, but only for virtual registers.
1315*9880d681SAndroid Build Coastguard Worker if (LiveInts && !LiveInts->isNotInMIMap(*MI)) {
1316*9880d681SAndroid Build Coastguard Worker SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI);
1317*9880d681SAndroid Build Coastguard Worker DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber());
1318*9880d681SAndroid Build Coastguard Worker
1319*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1320*9880d681SAndroid Build Coastguard Worker if (LiveInts->hasInterval(Reg)) {
1321*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LiveInts->getInterval(Reg);
1322*9880d681SAndroid Build Coastguard Worker checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg);
1323*9880d681SAndroid Build Coastguard Worker
1324*9880d681SAndroid Build Coastguard Worker if (LI.hasSubRanges()) {
1325*9880d681SAndroid Build Coastguard Worker unsigned SubRegIdx = MO->getSubReg();
1326*9880d681SAndroid Build Coastguard Worker LaneBitmask MOMask = SubRegIdx != 0
1327*9880d681SAndroid Build Coastguard Worker ? TRI->getSubRegIndexLaneMask(SubRegIdx)
1328*9880d681SAndroid Build Coastguard Worker : MRI->getMaxLaneMaskForVReg(Reg);
1329*9880d681SAndroid Build Coastguard Worker for (const LiveInterval::SubRange &SR : LI.subranges()) {
1330*9880d681SAndroid Build Coastguard Worker if ((SR.LaneMask & MOMask) == 0)
1331*9880d681SAndroid Build Coastguard Worker continue;
1332*9880d681SAndroid Build Coastguard Worker checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, SR.LaneMask);
1333*9880d681SAndroid Build Coastguard Worker }
1334*9880d681SAndroid Build Coastguard Worker }
1335*9880d681SAndroid Build Coastguard Worker } else {
1336*9880d681SAndroid Build Coastguard Worker report("Virtual register has no Live interval", MO, MONum);
1337*9880d681SAndroid Build Coastguard Worker }
1338*9880d681SAndroid Build Coastguard Worker }
1339*9880d681SAndroid Build Coastguard Worker }
1340*9880d681SAndroid Build Coastguard Worker }
1341*9880d681SAndroid Build Coastguard Worker }
1342*9880d681SAndroid Build Coastguard Worker
visitMachineInstrAfter(const MachineInstr * MI)1343*9880d681SAndroid Build Coastguard Worker void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {
1344*9880d681SAndroid Build Coastguard Worker }
1345*9880d681SAndroid Build Coastguard Worker
1346*9880d681SAndroid Build Coastguard Worker // This function gets called after visiting all instructions in a bundle. The
1347*9880d681SAndroid Build Coastguard Worker // argument points to the bundle header.
1348*9880d681SAndroid Build Coastguard Worker // Normal stand-alone instructions are also considered 'bundles', and this
1349*9880d681SAndroid Build Coastguard Worker // function is called for all of them.
visitMachineBundleAfter(const MachineInstr * MI)1350*9880d681SAndroid Build Coastguard Worker void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) {
1351*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[MI->getParent()];
1352*9880d681SAndroid Build Coastguard Worker set_union(MInfo.regsKilled, regsKilled);
1353*9880d681SAndroid Build Coastguard Worker set_subtract(regsLive, regsKilled); regsKilled.clear();
1354*9880d681SAndroid Build Coastguard Worker // Kill any masked registers.
1355*9880d681SAndroid Build Coastguard Worker while (!regMasks.empty()) {
1356*9880d681SAndroid Build Coastguard Worker const uint32_t *Mask = regMasks.pop_back_val();
1357*9880d681SAndroid Build Coastguard Worker for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I)
1358*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isPhysicalRegister(*I) &&
1359*9880d681SAndroid Build Coastguard Worker MachineOperand::clobbersPhysReg(Mask, *I))
1360*9880d681SAndroid Build Coastguard Worker regsDead.push_back(*I);
1361*9880d681SAndroid Build Coastguard Worker }
1362*9880d681SAndroid Build Coastguard Worker set_subtract(regsLive, regsDead); regsDead.clear();
1363*9880d681SAndroid Build Coastguard Worker set_union(regsLive, regsDefined); regsDefined.clear();
1364*9880d681SAndroid Build Coastguard Worker }
1365*9880d681SAndroid Build Coastguard Worker
1366*9880d681SAndroid Build Coastguard Worker void
visitMachineBasicBlockAfter(const MachineBasicBlock * MBB)1367*9880d681SAndroid Build Coastguard Worker MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) {
1368*9880d681SAndroid Build Coastguard Worker MBBInfoMap[MBB].regsLiveOut = regsLive;
1369*9880d681SAndroid Build Coastguard Worker regsLive.clear();
1370*9880d681SAndroid Build Coastguard Worker
1371*9880d681SAndroid Build Coastguard Worker if (Indexes) {
1372*9880d681SAndroid Build Coastguard Worker SlotIndex stop = Indexes->getMBBEndIdx(MBB);
1373*9880d681SAndroid Build Coastguard Worker if (!(stop > lastIndex)) {
1374*9880d681SAndroid Build Coastguard Worker report("Block ends before last instruction index", MBB);
1375*9880d681SAndroid Build Coastguard Worker errs() << "Block ends at " << stop
1376*9880d681SAndroid Build Coastguard Worker << " last instruction was at " << lastIndex << '\n';
1377*9880d681SAndroid Build Coastguard Worker }
1378*9880d681SAndroid Build Coastguard Worker lastIndex = stop;
1379*9880d681SAndroid Build Coastguard Worker }
1380*9880d681SAndroid Build Coastguard Worker }
1381*9880d681SAndroid Build Coastguard Worker
1382*9880d681SAndroid Build Coastguard Worker // Calculate the largest possible vregsPassed sets. These are the registers that
1383*9880d681SAndroid Build Coastguard Worker // can pass through an MBB live, but may not be live every time. It is assumed
1384*9880d681SAndroid Build Coastguard Worker // that all vregsPassed sets are empty before the call.
calcRegsPassed()1385*9880d681SAndroid Build Coastguard Worker void MachineVerifier::calcRegsPassed() {
1386*9880d681SAndroid Build Coastguard Worker // First push live-out regs to successors' vregsPassed. Remember the MBBs that
1387*9880d681SAndroid Build Coastguard Worker // have any vregsPassed.
1388*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const MachineBasicBlock*, 8> todo;
1389*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *MF) {
1390*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MBB];
1391*9880d681SAndroid Build Coastguard Worker if (!MInfo.reachable)
1392*9880d681SAndroid Build Coastguard Worker continue;
1393*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(),
1394*9880d681SAndroid Build Coastguard Worker SuE = MBB.succ_end(); SuI != SuE; ++SuI) {
1395*9880d681SAndroid Build Coastguard Worker BBInfo &SInfo = MBBInfoMap[*SuI];
1396*9880d681SAndroid Build Coastguard Worker if (SInfo.addPassed(MInfo.regsLiveOut))
1397*9880d681SAndroid Build Coastguard Worker todo.insert(*SuI);
1398*9880d681SAndroid Build Coastguard Worker }
1399*9880d681SAndroid Build Coastguard Worker }
1400*9880d681SAndroid Build Coastguard Worker
1401*9880d681SAndroid Build Coastguard Worker // Iteratively push vregsPassed to successors. This will converge to the same
1402*9880d681SAndroid Build Coastguard Worker // final state regardless of DenseSet iteration order.
1403*9880d681SAndroid Build Coastguard Worker while (!todo.empty()) {
1404*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = *todo.begin();
1405*9880d681SAndroid Build Coastguard Worker todo.erase(MBB);
1406*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[MBB];
1407*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(),
1408*9880d681SAndroid Build Coastguard Worker SuE = MBB->succ_end(); SuI != SuE; ++SuI) {
1409*9880d681SAndroid Build Coastguard Worker if (*SuI == MBB)
1410*9880d681SAndroid Build Coastguard Worker continue;
1411*9880d681SAndroid Build Coastguard Worker BBInfo &SInfo = MBBInfoMap[*SuI];
1412*9880d681SAndroid Build Coastguard Worker if (SInfo.addPassed(MInfo.vregsPassed))
1413*9880d681SAndroid Build Coastguard Worker todo.insert(*SuI);
1414*9880d681SAndroid Build Coastguard Worker }
1415*9880d681SAndroid Build Coastguard Worker }
1416*9880d681SAndroid Build Coastguard Worker }
1417*9880d681SAndroid Build Coastguard Worker
1418*9880d681SAndroid Build Coastguard Worker // Calculate the set of virtual registers that must be passed through each basic
1419*9880d681SAndroid Build Coastguard Worker // block in order to satisfy the requirements of successor blocks. This is very
1420*9880d681SAndroid Build Coastguard Worker // similar to calcRegsPassed, only backwards.
calcRegsRequired()1421*9880d681SAndroid Build Coastguard Worker void MachineVerifier::calcRegsRequired() {
1422*9880d681SAndroid Build Coastguard Worker // First push live-in regs to predecessors' vregsRequired.
1423*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const MachineBasicBlock*, 8> todo;
1424*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *MF) {
1425*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MBB];
1426*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(),
1427*9880d681SAndroid Build Coastguard Worker PrE = MBB.pred_end(); PrI != PrE; ++PrI) {
1428*9880d681SAndroid Build Coastguard Worker BBInfo &PInfo = MBBInfoMap[*PrI];
1429*9880d681SAndroid Build Coastguard Worker if (PInfo.addRequired(MInfo.vregsLiveIn))
1430*9880d681SAndroid Build Coastguard Worker todo.insert(*PrI);
1431*9880d681SAndroid Build Coastguard Worker }
1432*9880d681SAndroid Build Coastguard Worker }
1433*9880d681SAndroid Build Coastguard Worker
1434*9880d681SAndroid Build Coastguard Worker // Iteratively push vregsRequired to predecessors. This will converge to the
1435*9880d681SAndroid Build Coastguard Worker // same final state regardless of DenseSet iteration order.
1436*9880d681SAndroid Build Coastguard Worker while (!todo.empty()) {
1437*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = *todo.begin();
1438*9880d681SAndroid Build Coastguard Worker todo.erase(MBB);
1439*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[MBB];
1440*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1441*9880d681SAndroid Build Coastguard Worker PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1442*9880d681SAndroid Build Coastguard Worker if (*PrI == MBB)
1443*9880d681SAndroid Build Coastguard Worker continue;
1444*9880d681SAndroid Build Coastguard Worker BBInfo &SInfo = MBBInfoMap[*PrI];
1445*9880d681SAndroid Build Coastguard Worker if (SInfo.addRequired(MInfo.vregsRequired))
1446*9880d681SAndroid Build Coastguard Worker todo.insert(*PrI);
1447*9880d681SAndroid Build Coastguard Worker }
1448*9880d681SAndroid Build Coastguard Worker }
1449*9880d681SAndroid Build Coastguard Worker }
1450*9880d681SAndroid Build Coastguard Worker
1451*9880d681SAndroid Build Coastguard Worker // Check PHI instructions at the beginning of MBB. It is assumed that
1452*9880d681SAndroid Build Coastguard Worker // calcRegsPassed has been run so BBInfo::isLiveOut is valid.
checkPHIOps(const MachineBasicBlock * MBB)1453*9880d681SAndroid Build Coastguard Worker void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) {
1454*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const MachineBasicBlock*, 8> seen;
1455*9880d681SAndroid Build Coastguard Worker for (const auto &BBI : *MBB) {
1456*9880d681SAndroid Build Coastguard Worker if (!BBI.isPHI())
1457*9880d681SAndroid Build Coastguard Worker break;
1458*9880d681SAndroid Build Coastguard Worker seen.clear();
1459*9880d681SAndroid Build Coastguard Worker
1460*9880d681SAndroid Build Coastguard Worker for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2) {
1461*9880d681SAndroid Build Coastguard Worker unsigned Reg = BBI.getOperand(i).getReg();
1462*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *Pre = BBI.getOperand(i + 1).getMBB();
1463*9880d681SAndroid Build Coastguard Worker if (!Pre->isSuccessor(MBB))
1464*9880d681SAndroid Build Coastguard Worker continue;
1465*9880d681SAndroid Build Coastguard Worker seen.insert(Pre);
1466*9880d681SAndroid Build Coastguard Worker BBInfo &PrInfo = MBBInfoMap[Pre];
1467*9880d681SAndroid Build Coastguard Worker if (PrInfo.reachable && !PrInfo.isLiveOut(Reg))
1468*9880d681SAndroid Build Coastguard Worker report("PHI operand is not live-out from predecessor",
1469*9880d681SAndroid Build Coastguard Worker &BBI.getOperand(i), i);
1470*9880d681SAndroid Build Coastguard Worker }
1471*9880d681SAndroid Build Coastguard Worker
1472*9880d681SAndroid Build Coastguard Worker // Did we see all predecessors?
1473*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(),
1474*9880d681SAndroid Build Coastguard Worker PrE = MBB->pred_end(); PrI != PrE; ++PrI) {
1475*9880d681SAndroid Build Coastguard Worker if (!seen.count(*PrI)) {
1476*9880d681SAndroid Build Coastguard Worker report("Missing PHI operand", &BBI);
1477*9880d681SAndroid Build Coastguard Worker errs() << "BB#" << (*PrI)->getNumber()
1478*9880d681SAndroid Build Coastguard Worker << " is a predecessor according to the CFG.\n";
1479*9880d681SAndroid Build Coastguard Worker }
1480*9880d681SAndroid Build Coastguard Worker }
1481*9880d681SAndroid Build Coastguard Worker }
1482*9880d681SAndroid Build Coastguard Worker }
1483*9880d681SAndroid Build Coastguard Worker
visitMachineFunctionAfter()1484*9880d681SAndroid Build Coastguard Worker void MachineVerifier::visitMachineFunctionAfter() {
1485*9880d681SAndroid Build Coastguard Worker calcRegsPassed();
1486*9880d681SAndroid Build Coastguard Worker
1487*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *MF) {
1488*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MBB];
1489*9880d681SAndroid Build Coastguard Worker
1490*9880d681SAndroid Build Coastguard Worker // Skip unreachable MBBs.
1491*9880d681SAndroid Build Coastguard Worker if (!MInfo.reachable)
1492*9880d681SAndroid Build Coastguard Worker continue;
1493*9880d681SAndroid Build Coastguard Worker
1494*9880d681SAndroid Build Coastguard Worker checkPHIOps(&MBB);
1495*9880d681SAndroid Build Coastguard Worker }
1496*9880d681SAndroid Build Coastguard Worker
1497*9880d681SAndroid Build Coastguard Worker // Now check liveness info if available
1498*9880d681SAndroid Build Coastguard Worker calcRegsRequired();
1499*9880d681SAndroid Build Coastguard Worker
1500*9880d681SAndroid Build Coastguard Worker // Check for killed virtual registers that should be live out.
1501*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *MF) {
1502*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MBB];
1503*9880d681SAndroid Build Coastguard Worker for (RegSet::iterator
1504*9880d681SAndroid Build Coastguard Worker I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1505*9880d681SAndroid Build Coastguard Worker ++I)
1506*9880d681SAndroid Build Coastguard Worker if (MInfo.regsKilled.count(*I)) {
1507*9880d681SAndroid Build Coastguard Worker report("Virtual register killed in block, but needed live out.", &MBB);
1508*9880d681SAndroid Build Coastguard Worker errs() << "Virtual register " << PrintReg(*I)
1509*9880d681SAndroid Build Coastguard Worker << " is used after the block.\n";
1510*9880d681SAndroid Build Coastguard Worker }
1511*9880d681SAndroid Build Coastguard Worker }
1512*9880d681SAndroid Build Coastguard Worker
1513*9880d681SAndroid Build Coastguard Worker if (!MF->empty()) {
1514*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MF->front()];
1515*9880d681SAndroid Build Coastguard Worker for (RegSet::iterator
1516*9880d681SAndroid Build Coastguard Worker I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E;
1517*9880d681SAndroid Build Coastguard Worker ++I) {
1518*9880d681SAndroid Build Coastguard Worker report("Virtual register defs don't dominate all uses.", MF);
1519*9880d681SAndroid Build Coastguard Worker report_context_vreg(*I);
1520*9880d681SAndroid Build Coastguard Worker }
1521*9880d681SAndroid Build Coastguard Worker }
1522*9880d681SAndroid Build Coastguard Worker
1523*9880d681SAndroid Build Coastguard Worker if (LiveVars)
1524*9880d681SAndroid Build Coastguard Worker verifyLiveVariables();
1525*9880d681SAndroid Build Coastguard Worker if (LiveInts)
1526*9880d681SAndroid Build Coastguard Worker verifyLiveIntervals();
1527*9880d681SAndroid Build Coastguard Worker }
1528*9880d681SAndroid Build Coastguard Worker
verifyLiveVariables()1529*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyLiveVariables() {
1530*9880d681SAndroid Build Coastguard Worker assert(LiveVars && "Don't call verifyLiveVariables without LiveVars");
1531*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1532*9880d681SAndroid Build Coastguard Worker unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1533*9880d681SAndroid Build Coastguard Worker LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg);
1534*9880d681SAndroid Build Coastguard Worker for (const auto &MBB : *MF) {
1535*9880d681SAndroid Build Coastguard Worker BBInfo &MInfo = MBBInfoMap[&MBB];
1536*9880d681SAndroid Build Coastguard Worker
1537*9880d681SAndroid Build Coastguard Worker // Our vregsRequired should be identical to LiveVariables' AliveBlocks
1538*9880d681SAndroid Build Coastguard Worker if (MInfo.vregsRequired.count(Reg)) {
1539*9880d681SAndroid Build Coastguard Worker if (!VI.AliveBlocks.test(MBB.getNumber())) {
1540*9880d681SAndroid Build Coastguard Worker report("LiveVariables: Block missing from AliveBlocks", &MBB);
1541*9880d681SAndroid Build Coastguard Worker errs() << "Virtual register " << PrintReg(Reg)
1542*9880d681SAndroid Build Coastguard Worker << " must be live through the block.\n";
1543*9880d681SAndroid Build Coastguard Worker }
1544*9880d681SAndroid Build Coastguard Worker } else {
1545*9880d681SAndroid Build Coastguard Worker if (VI.AliveBlocks.test(MBB.getNumber())) {
1546*9880d681SAndroid Build Coastguard Worker report("LiveVariables: Block should not be in AliveBlocks", &MBB);
1547*9880d681SAndroid Build Coastguard Worker errs() << "Virtual register " << PrintReg(Reg)
1548*9880d681SAndroid Build Coastguard Worker << " is not needed live through the block.\n";
1549*9880d681SAndroid Build Coastguard Worker }
1550*9880d681SAndroid Build Coastguard Worker }
1551*9880d681SAndroid Build Coastguard Worker }
1552*9880d681SAndroid Build Coastguard Worker }
1553*9880d681SAndroid Build Coastguard Worker }
1554*9880d681SAndroid Build Coastguard Worker
verifyLiveIntervals()1555*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyLiveIntervals() {
1556*9880d681SAndroid Build Coastguard Worker assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts");
1557*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
1558*9880d681SAndroid Build Coastguard Worker unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
1559*9880d681SAndroid Build Coastguard Worker
1560*9880d681SAndroid Build Coastguard Worker // Spilling and splitting may leave unused registers around. Skip them.
1561*9880d681SAndroid Build Coastguard Worker if (MRI->reg_nodbg_empty(Reg))
1562*9880d681SAndroid Build Coastguard Worker continue;
1563*9880d681SAndroid Build Coastguard Worker
1564*9880d681SAndroid Build Coastguard Worker if (!LiveInts->hasInterval(Reg)) {
1565*9880d681SAndroid Build Coastguard Worker report("Missing live interval for virtual register", MF);
1566*9880d681SAndroid Build Coastguard Worker errs() << PrintReg(Reg, TRI) << " still has defs or uses\n";
1567*9880d681SAndroid Build Coastguard Worker continue;
1568*9880d681SAndroid Build Coastguard Worker }
1569*9880d681SAndroid Build Coastguard Worker
1570*9880d681SAndroid Build Coastguard Worker const LiveInterval &LI = LiveInts->getInterval(Reg);
1571*9880d681SAndroid Build Coastguard Worker assert(Reg == LI.reg && "Invalid reg to interval mapping");
1572*9880d681SAndroid Build Coastguard Worker verifyLiveInterval(LI);
1573*9880d681SAndroid Build Coastguard Worker }
1574*9880d681SAndroid Build Coastguard Worker
1575*9880d681SAndroid Build Coastguard Worker // Verify all the cached regunit intervals.
1576*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
1577*9880d681SAndroid Build Coastguard Worker if (const LiveRange *LR = LiveInts->getCachedRegUnit(i))
1578*9880d681SAndroid Build Coastguard Worker verifyLiveRange(*LR, i);
1579*9880d681SAndroid Build Coastguard Worker }
1580*9880d681SAndroid Build Coastguard Worker
verifyLiveRangeValue(const LiveRange & LR,const VNInfo * VNI,unsigned Reg,LaneBitmask LaneMask)1581*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR,
1582*9880d681SAndroid Build Coastguard Worker const VNInfo *VNI, unsigned Reg,
1583*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask) {
1584*9880d681SAndroid Build Coastguard Worker if (VNI->isUnused())
1585*9880d681SAndroid Build Coastguard Worker return;
1586*9880d681SAndroid Build Coastguard Worker
1587*9880d681SAndroid Build Coastguard Worker const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def);
1588*9880d681SAndroid Build Coastguard Worker
1589*9880d681SAndroid Build Coastguard Worker if (!DefVNI) {
1590*9880d681SAndroid Build Coastguard Worker report("Value not live at VNInfo def and not marked unused", MF);
1591*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1592*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1593*9880d681SAndroid Build Coastguard Worker return;
1594*9880d681SAndroid Build Coastguard Worker }
1595*9880d681SAndroid Build Coastguard Worker
1596*9880d681SAndroid Build Coastguard Worker if (DefVNI != VNI) {
1597*9880d681SAndroid Build Coastguard Worker report("Live segment at def has different VNInfo", MF);
1598*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1599*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1600*9880d681SAndroid Build Coastguard Worker return;
1601*9880d681SAndroid Build Coastguard Worker }
1602*9880d681SAndroid Build Coastguard Worker
1603*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def);
1604*9880d681SAndroid Build Coastguard Worker if (!MBB) {
1605*9880d681SAndroid Build Coastguard Worker report("Invalid VNInfo definition index", MF);
1606*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1607*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1608*9880d681SAndroid Build Coastguard Worker return;
1609*9880d681SAndroid Build Coastguard Worker }
1610*9880d681SAndroid Build Coastguard Worker
1611*9880d681SAndroid Build Coastguard Worker if (VNI->isPHIDef()) {
1612*9880d681SAndroid Build Coastguard Worker if (VNI->def != LiveInts->getMBBStartIdx(MBB)) {
1613*9880d681SAndroid Build Coastguard Worker report("PHIDef VNInfo is not defined at MBB start", MBB);
1614*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1615*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1616*9880d681SAndroid Build Coastguard Worker }
1617*9880d681SAndroid Build Coastguard Worker return;
1618*9880d681SAndroid Build Coastguard Worker }
1619*9880d681SAndroid Build Coastguard Worker
1620*9880d681SAndroid Build Coastguard Worker // Non-PHI def.
1621*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def);
1622*9880d681SAndroid Build Coastguard Worker if (!MI) {
1623*9880d681SAndroid Build Coastguard Worker report("No instruction at VNInfo def index", MBB);
1624*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1625*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1626*9880d681SAndroid Build Coastguard Worker return;
1627*9880d681SAndroid Build Coastguard Worker }
1628*9880d681SAndroid Build Coastguard Worker
1629*9880d681SAndroid Build Coastguard Worker if (Reg != 0) {
1630*9880d681SAndroid Build Coastguard Worker bool hasDef = false;
1631*9880d681SAndroid Build Coastguard Worker bool isEarlyClobber = false;
1632*9880d681SAndroid Build Coastguard Worker for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
1633*9880d681SAndroid Build Coastguard Worker if (!MOI->isReg() || !MOI->isDef())
1634*9880d681SAndroid Build Coastguard Worker continue;
1635*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1636*9880d681SAndroid Build Coastguard Worker if (MOI->getReg() != Reg)
1637*9880d681SAndroid Build Coastguard Worker continue;
1638*9880d681SAndroid Build Coastguard Worker } else {
1639*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) ||
1640*9880d681SAndroid Build Coastguard Worker !TRI->hasRegUnit(MOI->getReg(), Reg))
1641*9880d681SAndroid Build Coastguard Worker continue;
1642*9880d681SAndroid Build Coastguard Worker }
1643*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0 &&
1644*9880d681SAndroid Build Coastguard Worker (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask) == 0)
1645*9880d681SAndroid Build Coastguard Worker continue;
1646*9880d681SAndroid Build Coastguard Worker hasDef = true;
1647*9880d681SAndroid Build Coastguard Worker if (MOI->isEarlyClobber())
1648*9880d681SAndroid Build Coastguard Worker isEarlyClobber = true;
1649*9880d681SAndroid Build Coastguard Worker }
1650*9880d681SAndroid Build Coastguard Worker
1651*9880d681SAndroid Build Coastguard Worker if (!hasDef) {
1652*9880d681SAndroid Build Coastguard Worker report("Defining instruction does not modify register", MI);
1653*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1654*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1655*9880d681SAndroid Build Coastguard Worker }
1656*9880d681SAndroid Build Coastguard Worker
1657*9880d681SAndroid Build Coastguard Worker // Early clobber defs begin at USE slots, but other defs must begin at
1658*9880d681SAndroid Build Coastguard Worker // DEF slots.
1659*9880d681SAndroid Build Coastguard Worker if (isEarlyClobber) {
1660*9880d681SAndroid Build Coastguard Worker if (!VNI->def.isEarlyClobber()) {
1661*9880d681SAndroid Build Coastguard Worker report("Early clobber def must be at an early-clobber slot", MBB);
1662*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1663*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1664*9880d681SAndroid Build Coastguard Worker }
1665*9880d681SAndroid Build Coastguard Worker } else if (!VNI->def.isRegister()) {
1666*9880d681SAndroid Build Coastguard Worker report("Non-PHI, non-early clobber def must be at a register slot", MBB);
1667*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1668*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1669*9880d681SAndroid Build Coastguard Worker }
1670*9880d681SAndroid Build Coastguard Worker }
1671*9880d681SAndroid Build Coastguard Worker }
1672*9880d681SAndroid Build Coastguard Worker
verifyLiveRangeSegment(const LiveRange & LR,const LiveRange::const_iterator I,unsigned Reg,LaneBitmask LaneMask)1673*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR,
1674*9880d681SAndroid Build Coastguard Worker const LiveRange::const_iterator I,
1675*9880d681SAndroid Build Coastguard Worker unsigned Reg, LaneBitmask LaneMask)
1676*9880d681SAndroid Build Coastguard Worker {
1677*9880d681SAndroid Build Coastguard Worker const LiveRange::Segment &S = *I;
1678*9880d681SAndroid Build Coastguard Worker const VNInfo *VNI = S.valno;
1679*9880d681SAndroid Build Coastguard Worker assert(VNI && "Live segment has no valno");
1680*9880d681SAndroid Build Coastguard Worker
1681*9880d681SAndroid Build Coastguard Worker if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) {
1682*9880d681SAndroid Build Coastguard Worker report("Foreign valno in live segment", MF);
1683*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1684*9880d681SAndroid Build Coastguard Worker report_context(S);
1685*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1686*9880d681SAndroid Build Coastguard Worker }
1687*9880d681SAndroid Build Coastguard Worker
1688*9880d681SAndroid Build Coastguard Worker if (VNI->isUnused()) {
1689*9880d681SAndroid Build Coastguard Worker report("Live segment valno is marked unused", MF);
1690*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1691*9880d681SAndroid Build Coastguard Worker report_context(S);
1692*9880d681SAndroid Build Coastguard Worker }
1693*9880d681SAndroid Build Coastguard Worker
1694*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start);
1695*9880d681SAndroid Build Coastguard Worker if (!MBB) {
1696*9880d681SAndroid Build Coastguard Worker report("Bad start of live segment, no basic block", MF);
1697*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1698*9880d681SAndroid Build Coastguard Worker report_context(S);
1699*9880d681SAndroid Build Coastguard Worker return;
1700*9880d681SAndroid Build Coastguard Worker }
1701*9880d681SAndroid Build Coastguard Worker SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB);
1702*9880d681SAndroid Build Coastguard Worker if (S.start != MBBStartIdx && S.start != VNI->def) {
1703*9880d681SAndroid Build Coastguard Worker report("Live segment must begin at MBB entry or valno def", MBB);
1704*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1705*9880d681SAndroid Build Coastguard Worker report_context(S);
1706*9880d681SAndroid Build Coastguard Worker }
1707*9880d681SAndroid Build Coastguard Worker
1708*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *EndMBB =
1709*9880d681SAndroid Build Coastguard Worker LiveInts->getMBBFromIndex(S.end.getPrevSlot());
1710*9880d681SAndroid Build Coastguard Worker if (!EndMBB) {
1711*9880d681SAndroid Build Coastguard Worker report("Bad end of live segment, no basic block", MF);
1712*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1713*9880d681SAndroid Build Coastguard Worker report_context(S);
1714*9880d681SAndroid Build Coastguard Worker return;
1715*9880d681SAndroid Build Coastguard Worker }
1716*9880d681SAndroid Build Coastguard Worker
1717*9880d681SAndroid Build Coastguard Worker // No more checks for live-out segments.
1718*9880d681SAndroid Build Coastguard Worker if (S.end == LiveInts->getMBBEndIdx(EndMBB))
1719*9880d681SAndroid Build Coastguard Worker return;
1720*9880d681SAndroid Build Coastguard Worker
1721*9880d681SAndroid Build Coastguard Worker // RegUnit intervals are allowed dead phis.
1722*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() &&
1723*9880d681SAndroid Build Coastguard Worker S.start == VNI->def && S.end == VNI->def.getDeadSlot())
1724*9880d681SAndroid Build Coastguard Worker return;
1725*9880d681SAndroid Build Coastguard Worker
1726*9880d681SAndroid Build Coastguard Worker // The live segment is ending inside EndMBB
1727*9880d681SAndroid Build Coastguard Worker const MachineInstr *MI =
1728*9880d681SAndroid Build Coastguard Worker LiveInts->getInstructionFromIndex(S.end.getPrevSlot());
1729*9880d681SAndroid Build Coastguard Worker if (!MI) {
1730*9880d681SAndroid Build Coastguard Worker report("Live segment doesn't end at a valid instruction", EndMBB);
1731*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1732*9880d681SAndroid Build Coastguard Worker report_context(S);
1733*9880d681SAndroid Build Coastguard Worker return;
1734*9880d681SAndroid Build Coastguard Worker }
1735*9880d681SAndroid Build Coastguard Worker
1736*9880d681SAndroid Build Coastguard Worker // The block slot must refer to a basic block boundary.
1737*9880d681SAndroid Build Coastguard Worker if (S.end.isBlock()) {
1738*9880d681SAndroid Build Coastguard Worker report("Live segment ends at B slot of an instruction", EndMBB);
1739*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1740*9880d681SAndroid Build Coastguard Worker report_context(S);
1741*9880d681SAndroid Build Coastguard Worker }
1742*9880d681SAndroid Build Coastguard Worker
1743*9880d681SAndroid Build Coastguard Worker if (S.end.isDead()) {
1744*9880d681SAndroid Build Coastguard Worker // Segment ends on the dead slot.
1745*9880d681SAndroid Build Coastguard Worker // That means there must be a dead def.
1746*9880d681SAndroid Build Coastguard Worker if (!SlotIndex::isSameInstr(S.start, S.end)) {
1747*9880d681SAndroid Build Coastguard Worker report("Live segment ending at dead slot spans instructions", EndMBB);
1748*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1749*9880d681SAndroid Build Coastguard Worker report_context(S);
1750*9880d681SAndroid Build Coastguard Worker }
1751*9880d681SAndroid Build Coastguard Worker }
1752*9880d681SAndroid Build Coastguard Worker
1753*9880d681SAndroid Build Coastguard Worker // A live segment can only end at an early-clobber slot if it is being
1754*9880d681SAndroid Build Coastguard Worker // redefined by an early-clobber def.
1755*9880d681SAndroid Build Coastguard Worker if (S.end.isEarlyClobber()) {
1756*9880d681SAndroid Build Coastguard Worker if (I+1 == LR.end() || (I+1)->start != S.end) {
1757*9880d681SAndroid Build Coastguard Worker report("Live segment ending at early clobber slot must be "
1758*9880d681SAndroid Build Coastguard Worker "redefined by an EC def in the same instruction", EndMBB);
1759*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1760*9880d681SAndroid Build Coastguard Worker report_context(S);
1761*9880d681SAndroid Build Coastguard Worker }
1762*9880d681SAndroid Build Coastguard Worker }
1763*9880d681SAndroid Build Coastguard Worker
1764*9880d681SAndroid Build Coastguard Worker // The following checks only apply to virtual registers. Physreg liveness
1765*9880d681SAndroid Build Coastguard Worker // is too weird to check.
1766*9880d681SAndroid Build Coastguard Worker if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1767*9880d681SAndroid Build Coastguard Worker // A live segment can end with either a redefinition, a kill flag on a
1768*9880d681SAndroid Build Coastguard Worker // use, or a dead flag on a def.
1769*9880d681SAndroid Build Coastguard Worker bool hasRead = false;
1770*9880d681SAndroid Build Coastguard Worker bool hasSubRegDef = false;
1771*9880d681SAndroid Build Coastguard Worker bool hasDeadDef = false;
1772*9880d681SAndroid Build Coastguard Worker for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) {
1773*9880d681SAndroid Build Coastguard Worker if (!MOI->isReg() || MOI->getReg() != Reg)
1774*9880d681SAndroid Build Coastguard Worker continue;
1775*9880d681SAndroid Build Coastguard Worker if (LaneMask != 0 &&
1776*9880d681SAndroid Build Coastguard Worker (LaneMask & TRI->getSubRegIndexLaneMask(MOI->getSubReg())) == 0)
1777*9880d681SAndroid Build Coastguard Worker continue;
1778*9880d681SAndroid Build Coastguard Worker if (MOI->isDef()) {
1779*9880d681SAndroid Build Coastguard Worker if (MOI->getSubReg() != 0)
1780*9880d681SAndroid Build Coastguard Worker hasSubRegDef = true;
1781*9880d681SAndroid Build Coastguard Worker if (MOI->isDead())
1782*9880d681SAndroid Build Coastguard Worker hasDeadDef = true;
1783*9880d681SAndroid Build Coastguard Worker }
1784*9880d681SAndroid Build Coastguard Worker if (MOI->readsReg())
1785*9880d681SAndroid Build Coastguard Worker hasRead = true;
1786*9880d681SAndroid Build Coastguard Worker }
1787*9880d681SAndroid Build Coastguard Worker if (S.end.isDead()) {
1788*9880d681SAndroid Build Coastguard Worker // Make sure that the corresponding machine operand for a "dead" live
1789*9880d681SAndroid Build Coastguard Worker // range has the dead flag. We cannot perform this check for subregister
1790*9880d681SAndroid Build Coastguard Worker // liveranges as partially dead values are allowed.
1791*9880d681SAndroid Build Coastguard Worker if (LaneMask == 0 && !hasDeadDef) {
1792*9880d681SAndroid Build Coastguard Worker report("Instruction ending live segment on dead slot has no dead flag",
1793*9880d681SAndroid Build Coastguard Worker MI);
1794*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1795*9880d681SAndroid Build Coastguard Worker report_context(S);
1796*9880d681SAndroid Build Coastguard Worker }
1797*9880d681SAndroid Build Coastguard Worker } else {
1798*9880d681SAndroid Build Coastguard Worker if (!hasRead) {
1799*9880d681SAndroid Build Coastguard Worker // When tracking subregister liveness, the main range must start new
1800*9880d681SAndroid Build Coastguard Worker // values on partial register writes, even if there is no read.
1801*9880d681SAndroid Build Coastguard Worker if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask != 0 ||
1802*9880d681SAndroid Build Coastguard Worker !hasSubRegDef) {
1803*9880d681SAndroid Build Coastguard Worker report("Instruction ending live segment doesn't read the register",
1804*9880d681SAndroid Build Coastguard Worker MI);
1805*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1806*9880d681SAndroid Build Coastguard Worker report_context(S);
1807*9880d681SAndroid Build Coastguard Worker }
1808*9880d681SAndroid Build Coastguard Worker }
1809*9880d681SAndroid Build Coastguard Worker }
1810*9880d681SAndroid Build Coastguard Worker }
1811*9880d681SAndroid Build Coastguard Worker
1812*9880d681SAndroid Build Coastguard Worker // Now check all the basic blocks in this live segment.
1813*9880d681SAndroid Build Coastguard Worker MachineFunction::const_iterator MFI = MBB->getIterator();
1814*9880d681SAndroid Build Coastguard Worker // Is this live segment the beginning of a non-PHIDef VN?
1815*9880d681SAndroid Build Coastguard Worker if (S.start == VNI->def && !VNI->isPHIDef()) {
1816*9880d681SAndroid Build Coastguard Worker // Not live-in to any blocks.
1817*9880d681SAndroid Build Coastguard Worker if (MBB == EndMBB)
1818*9880d681SAndroid Build Coastguard Worker return;
1819*9880d681SAndroid Build Coastguard Worker // Skip this block.
1820*9880d681SAndroid Build Coastguard Worker ++MFI;
1821*9880d681SAndroid Build Coastguard Worker }
1822*9880d681SAndroid Build Coastguard Worker for (;;) {
1823*9880d681SAndroid Build Coastguard Worker assert(LiveInts->isLiveInToMBB(LR, &*MFI));
1824*9880d681SAndroid Build Coastguard Worker // We don't know how to track physregs into a landing pad.
1825*9880d681SAndroid Build Coastguard Worker if (!TargetRegisterInfo::isVirtualRegister(Reg) &&
1826*9880d681SAndroid Build Coastguard Worker MFI->isEHPad()) {
1827*9880d681SAndroid Build Coastguard Worker if (&*MFI == EndMBB)
1828*9880d681SAndroid Build Coastguard Worker break;
1829*9880d681SAndroid Build Coastguard Worker ++MFI;
1830*9880d681SAndroid Build Coastguard Worker continue;
1831*9880d681SAndroid Build Coastguard Worker }
1832*9880d681SAndroid Build Coastguard Worker
1833*9880d681SAndroid Build Coastguard Worker // Is VNI a PHI-def in the current block?
1834*9880d681SAndroid Build Coastguard Worker bool IsPHI = VNI->isPHIDef() &&
1835*9880d681SAndroid Build Coastguard Worker VNI->def == LiveInts->getMBBStartIdx(&*MFI);
1836*9880d681SAndroid Build Coastguard Worker
1837*9880d681SAndroid Build Coastguard Worker // Check that VNI is live-out of all predecessors.
1838*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(),
1839*9880d681SAndroid Build Coastguard Worker PE = MFI->pred_end(); PI != PE; ++PI) {
1840*9880d681SAndroid Build Coastguard Worker SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI);
1841*9880d681SAndroid Build Coastguard Worker const VNInfo *PVNI = LR.getVNInfoBefore(PEnd);
1842*9880d681SAndroid Build Coastguard Worker
1843*9880d681SAndroid Build Coastguard Worker // All predecessors must have a live-out value if this is not a
1844*9880d681SAndroid Build Coastguard Worker // subregister liverange.
1845*9880d681SAndroid Build Coastguard Worker if (!PVNI && LaneMask == 0) {
1846*9880d681SAndroid Build Coastguard Worker report("Register not marked live out of predecessor", *PI);
1847*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1848*9880d681SAndroid Build Coastguard Worker report_context(*VNI);
1849*9880d681SAndroid Build Coastguard Worker errs() << " live into BB#" << MFI->getNumber()
1850*9880d681SAndroid Build Coastguard Worker << '@' << LiveInts->getMBBStartIdx(&*MFI) << ", not live before "
1851*9880d681SAndroid Build Coastguard Worker << PEnd << '\n';
1852*9880d681SAndroid Build Coastguard Worker continue;
1853*9880d681SAndroid Build Coastguard Worker }
1854*9880d681SAndroid Build Coastguard Worker
1855*9880d681SAndroid Build Coastguard Worker // Only PHI-defs can take different predecessor values.
1856*9880d681SAndroid Build Coastguard Worker if (!IsPHI && PVNI != VNI) {
1857*9880d681SAndroid Build Coastguard Worker report("Different value live out of predecessor", *PI);
1858*9880d681SAndroid Build Coastguard Worker report_context(LR, Reg, LaneMask);
1859*9880d681SAndroid Build Coastguard Worker errs() << "Valno #" << PVNI->id << " live out of BB#"
1860*9880d681SAndroid Build Coastguard Worker << (*PI)->getNumber() << '@' << PEnd << "\nValno #" << VNI->id
1861*9880d681SAndroid Build Coastguard Worker << " live into BB#" << MFI->getNumber() << '@'
1862*9880d681SAndroid Build Coastguard Worker << LiveInts->getMBBStartIdx(&*MFI) << '\n';
1863*9880d681SAndroid Build Coastguard Worker }
1864*9880d681SAndroid Build Coastguard Worker }
1865*9880d681SAndroid Build Coastguard Worker if (&*MFI == EndMBB)
1866*9880d681SAndroid Build Coastguard Worker break;
1867*9880d681SAndroid Build Coastguard Worker ++MFI;
1868*9880d681SAndroid Build Coastguard Worker }
1869*9880d681SAndroid Build Coastguard Worker }
1870*9880d681SAndroid Build Coastguard Worker
verifyLiveRange(const LiveRange & LR,unsigned Reg,LaneBitmask LaneMask)1871*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg,
1872*9880d681SAndroid Build Coastguard Worker LaneBitmask LaneMask) {
1873*9880d681SAndroid Build Coastguard Worker for (const VNInfo *VNI : LR.valnos)
1874*9880d681SAndroid Build Coastguard Worker verifyLiveRangeValue(LR, VNI, Reg, LaneMask);
1875*9880d681SAndroid Build Coastguard Worker
1876*9880d681SAndroid Build Coastguard Worker for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I)
1877*9880d681SAndroid Build Coastguard Worker verifyLiveRangeSegment(LR, I, Reg, LaneMask);
1878*9880d681SAndroid Build Coastguard Worker }
1879*9880d681SAndroid Build Coastguard Worker
verifyLiveInterval(const LiveInterval & LI)1880*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) {
1881*9880d681SAndroid Build Coastguard Worker unsigned Reg = LI.reg;
1882*9880d681SAndroid Build Coastguard Worker assert(TargetRegisterInfo::isVirtualRegister(Reg));
1883*9880d681SAndroid Build Coastguard Worker verifyLiveRange(LI, Reg);
1884*9880d681SAndroid Build Coastguard Worker
1885*9880d681SAndroid Build Coastguard Worker LaneBitmask Mask = 0;
1886*9880d681SAndroid Build Coastguard Worker LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg);
1887*9880d681SAndroid Build Coastguard Worker for (const LiveInterval::SubRange &SR : LI.subranges()) {
1888*9880d681SAndroid Build Coastguard Worker if ((Mask & SR.LaneMask) != 0) {
1889*9880d681SAndroid Build Coastguard Worker report("Lane masks of sub ranges overlap in live interval", MF);
1890*9880d681SAndroid Build Coastguard Worker report_context(LI);
1891*9880d681SAndroid Build Coastguard Worker }
1892*9880d681SAndroid Build Coastguard Worker if ((SR.LaneMask & ~MaxMask) != 0) {
1893*9880d681SAndroid Build Coastguard Worker report("Subrange lanemask is invalid", MF);
1894*9880d681SAndroid Build Coastguard Worker report_context(LI);
1895*9880d681SAndroid Build Coastguard Worker }
1896*9880d681SAndroid Build Coastguard Worker if (SR.empty()) {
1897*9880d681SAndroid Build Coastguard Worker report("Subrange must not be empty", MF);
1898*9880d681SAndroid Build Coastguard Worker report_context(SR, LI.reg, SR.LaneMask);
1899*9880d681SAndroid Build Coastguard Worker }
1900*9880d681SAndroid Build Coastguard Worker Mask |= SR.LaneMask;
1901*9880d681SAndroid Build Coastguard Worker verifyLiveRange(SR, LI.reg, SR.LaneMask);
1902*9880d681SAndroid Build Coastguard Worker if (!LI.covers(SR)) {
1903*9880d681SAndroid Build Coastguard Worker report("A Subrange is not covered by the main range", MF);
1904*9880d681SAndroid Build Coastguard Worker report_context(LI);
1905*9880d681SAndroid Build Coastguard Worker }
1906*9880d681SAndroid Build Coastguard Worker }
1907*9880d681SAndroid Build Coastguard Worker
1908*9880d681SAndroid Build Coastguard Worker // Check the LI only has one connected component.
1909*9880d681SAndroid Build Coastguard Worker ConnectedVNInfoEqClasses ConEQ(*LiveInts);
1910*9880d681SAndroid Build Coastguard Worker unsigned NumComp = ConEQ.Classify(LI);
1911*9880d681SAndroid Build Coastguard Worker if (NumComp > 1) {
1912*9880d681SAndroid Build Coastguard Worker report("Multiple connected components in live interval", MF);
1913*9880d681SAndroid Build Coastguard Worker report_context(LI);
1914*9880d681SAndroid Build Coastguard Worker for (unsigned comp = 0; comp != NumComp; ++comp) {
1915*9880d681SAndroid Build Coastguard Worker errs() << comp << ": valnos";
1916*9880d681SAndroid Build Coastguard Worker for (LiveInterval::const_vni_iterator I = LI.vni_begin(),
1917*9880d681SAndroid Build Coastguard Worker E = LI.vni_end(); I!=E; ++I)
1918*9880d681SAndroid Build Coastguard Worker if (comp == ConEQ.getEqClass(*I))
1919*9880d681SAndroid Build Coastguard Worker errs() << ' ' << (*I)->id;
1920*9880d681SAndroid Build Coastguard Worker errs() << '\n';
1921*9880d681SAndroid Build Coastguard Worker }
1922*9880d681SAndroid Build Coastguard Worker }
1923*9880d681SAndroid Build Coastguard Worker }
1924*9880d681SAndroid Build Coastguard Worker
1925*9880d681SAndroid Build Coastguard Worker namespace {
1926*9880d681SAndroid Build Coastguard Worker // FrameSetup and FrameDestroy can have zero adjustment, so using a single
1927*9880d681SAndroid Build Coastguard Worker // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the
1928*9880d681SAndroid Build Coastguard Worker // value is zero.
1929*9880d681SAndroid Build Coastguard Worker // We use a bool plus an integer to capture the stack state.
1930*9880d681SAndroid Build Coastguard Worker struct StackStateOfBB {
StackStateOfBB__anon2ea96b9d0211::StackStateOfBB1931*9880d681SAndroid Build Coastguard Worker StackStateOfBB() : EntryValue(0), ExitValue(0), EntryIsSetup(false),
1932*9880d681SAndroid Build Coastguard Worker ExitIsSetup(false) { }
StackStateOfBB__anon2ea96b9d0211::StackStateOfBB1933*9880d681SAndroid Build Coastguard Worker StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) :
1934*9880d681SAndroid Build Coastguard Worker EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup),
1935*9880d681SAndroid Build Coastguard Worker ExitIsSetup(ExitSetup) { }
1936*9880d681SAndroid Build Coastguard Worker // Can be negative, which means we are setting up a frame.
1937*9880d681SAndroid Build Coastguard Worker int EntryValue;
1938*9880d681SAndroid Build Coastguard Worker int ExitValue;
1939*9880d681SAndroid Build Coastguard Worker bool EntryIsSetup;
1940*9880d681SAndroid Build Coastguard Worker bool ExitIsSetup;
1941*9880d681SAndroid Build Coastguard Worker };
1942*9880d681SAndroid Build Coastguard Worker }
1943*9880d681SAndroid Build Coastguard Worker
1944*9880d681SAndroid Build Coastguard Worker /// Make sure on every path through the CFG, a FrameSetup <n> is always followed
1945*9880d681SAndroid Build Coastguard Worker /// by a FrameDestroy <n>, stack adjustments are identical on all
1946*9880d681SAndroid Build Coastguard Worker /// CFG edges to a merge point, and frame is destroyed at end of a return block.
verifyStackFrame()1947*9880d681SAndroid Build Coastguard Worker void MachineVerifier::verifyStackFrame() {
1948*9880d681SAndroid Build Coastguard Worker unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode();
1949*9880d681SAndroid Build Coastguard Worker unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode();
1950*9880d681SAndroid Build Coastguard Worker
1951*9880d681SAndroid Build Coastguard Worker SmallVector<StackStateOfBB, 8> SPState;
1952*9880d681SAndroid Build Coastguard Worker SPState.resize(MF->getNumBlockIDs());
1953*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const MachineBasicBlock*, 8> Reachable;
1954*9880d681SAndroid Build Coastguard Worker
1955*9880d681SAndroid Build Coastguard Worker // Visit the MBBs in DFS order.
1956*9880d681SAndroid Build Coastguard Worker for (df_ext_iterator<const MachineFunction*,
1957*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const MachineBasicBlock*, 8> >
1958*9880d681SAndroid Build Coastguard Worker DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable);
1959*9880d681SAndroid Build Coastguard Worker DFI != DFE; ++DFI) {
1960*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *MBB = *DFI;
1961*9880d681SAndroid Build Coastguard Worker
1962*9880d681SAndroid Build Coastguard Worker StackStateOfBB BBState;
1963*9880d681SAndroid Build Coastguard Worker // Check the exit state of the DFS stack predecessor.
1964*9880d681SAndroid Build Coastguard Worker if (DFI.getPathLength() >= 2) {
1965*9880d681SAndroid Build Coastguard Worker const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2);
1966*9880d681SAndroid Build Coastguard Worker assert(Reachable.count(StackPred) &&
1967*9880d681SAndroid Build Coastguard Worker "DFS stack predecessor is already visited.\n");
1968*9880d681SAndroid Build Coastguard Worker BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue;
1969*9880d681SAndroid Build Coastguard Worker BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup;
1970*9880d681SAndroid Build Coastguard Worker BBState.ExitValue = BBState.EntryValue;
1971*9880d681SAndroid Build Coastguard Worker BBState.ExitIsSetup = BBState.EntryIsSetup;
1972*9880d681SAndroid Build Coastguard Worker }
1973*9880d681SAndroid Build Coastguard Worker
1974*9880d681SAndroid Build Coastguard Worker // Update stack state by checking contents of MBB.
1975*9880d681SAndroid Build Coastguard Worker for (const auto &I : *MBB) {
1976*9880d681SAndroid Build Coastguard Worker if (I.getOpcode() == FrameSetupOpcode) {
1977*9880d681SAndroid Build Coastguard Worker // The first operand of a FrameOpcode should be i32.
1978*9880d681SAndroid Build Coastguard Worker int Size = I.getOperand(0).getImm();
1979*9880d681SAndroid Build Coastguard Worker assert(Size >= 0 &&
1980*9880d681SAndroid Build Coastguard Worker "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1981*9880d681SAndroid Build Coastguard Worker
1982*9880d681SAndroid Build Coastguard Worker if (BBState.ExitIsSetup)
1983*9880d681SAndroid Build Coastguard Worker report("FrameSetup is after another FrameSetup", &I);
1984*9880d681SAndroid Build Coastguard Worker BBState.ExitValue -= Size;
1985*9880d681SAndroid Build Coastguard Worker BBState.ExitIsSetup = true;
1986*9880d681SAndroid Build Coastguard Worker }
1987*9880d681SAndroid Build Coastguard Worker
1988*9880d681SAndroid Build Coastguard Worker if (I.getOpcode() == FrameDestroyOpcode) {
1989*9880d681SAndroid Build Coastguard Worker // The first operand of a FrameOpcode should be i32.
1990*9880d681SAndroid Build Coastguard Worker int Size = I.getOperand(0).getImm();
1991*9880d681SAndroid Build Coastguard Worker assert(Size >= 0 &&
1992*9880d681SAndroid Build Coastguard Worker "Value should be non-negative in FrameSetup and FrameDestroy.\n");
1993*9880d681SAndroid Build Coastguard Worker
1994*9880d681SAndroid Build Coastguard Worker if (!BBState.ExitIsSetup)
1995*9880d681SAndroid Build Coastguard Worker report("FrameDestroy is not after a FrameSetup", &I);
1996*9880d681SAndroid Build Coastguard Worker int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue :
1997*9880d681SAndroid Build Coastguard Worker BBState.ExitValue;
1998*9880d681SAndroid Build Coastguard Worker if (BBState.ExitIsSetup && AbsSPAdj != Size) {
1999*9880d681SAndroid Build Coastguard Worker report("FrameDestroy <n> is after FrameSetup <m>", &I);
2000*9880d681SAndroid Build Coastguard Worker errs() << "FrameDestroy <" << Size << "> is after FrameSetup <"
2001*9880d681SAndroid Build Coastguard Worker << AbsSPAdj << ">.\n";
2002*9880d681SAndroid Build Coastguard Worker }
2003*9880d681SAndroid Build Coastguard Worker BBState.ExitValue += Size;
2004*9880d681SAndroid Build Coastguard Worker BBState.ExitIsSetup = false;
2005*9880d681SAndroid Build Coastguard Worker }
2006*9880d681SAndroid Build Coastguard Worker }
2007*9880d681SAndroid Build Coastguard Worker SPState[MBB->getNumber()] = BBState;
2008*9880d681SAndroid Build Coastguard Worker
2009*9880d681SAndroid Build Coastguard Worker // Make sure the exit state of any predecessor is consistent with the entry
2010*9880d681SAndroid Build Coastguard Worker // state.
2011*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(),
2012*9880d681SAndroid Build Coastguard Worker E = MBB->pred_end(); I != E; ++I) {
2013*9880d681SAndroid Build Coastguard Worker if (Reachable.count(*I) &&
2014*9880d681SAndroid Build Coastguard Worker (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue ||
2015*9880d681SAndroid Build Coastguard Worker SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) {
2016*9880d681SAndroid Build Coastguard Worker report("The exit stack state of a predecessor is inconsistent.", MBB);
2017*9880d681SAndroid Build Coastguard Worker errs() << "Predecessor BB#" << (*I)->getNumber() << " has exit state ("
2018*9880d681SAndroid Build Coastguard Worker << SPState[(*I)->getNumber()].ExitValue << ", "
2019*9880d681SAndroid Build Coastguard Worker << SPState[(*I)->getNumber()].ExitIsSetup
2020*9880d681SAndroid Build Coastguard Worker << "), while BB#" << MBB->getNumber() << " has entry state ("
2021*9880d681SAndroid Build Coastguard Worker << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n";
2022*9880d681SAndroid Build Coastguard Worker }
2023*9880d681SAndroid Build Coastguard Worker }
2024*9880d681SAndroid Build Coastguard Worker
2025*9880d681SAndroid Build Coastguard Worker // Make sure the entry state of any successor is consistent with the exit
2026*9880d681SAndroid Build Coastguard Worker // state.
2027*9880d681SAndroid Build Coastguard Worker for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(),
2028*9880d681SAndroid Build Coastguard Worker E = MBB->succ_end(); I != E; ++I) {
2029*9880d681SAndroid Build Coastguard Worker if (Reachable.count(*I) &&
2030*9880d681SAndroid Build Coastguard Worker (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue ||
2031*9880d681SAndroid Build Coastguard Worker SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) {
2032*9880d681SAndroid Build Coastguard Worker report("The entry stack state of a successor is inconsistent.", MBB);
2033*9880d681SAndroid Build Coastguard Worker errs() << "Successor BB#" << (*I)->getNumber() << " has entry state ("
2034*9880d681SAndroid Build Coastguard Worker << SPState[(*I)->getNumber()].EntryValue << ", "
2035*9880d681SAndroid Build Coastguard Worker << SPState[(*I)->getNumber()].EntryIsSetup
2036*9880d681SAndroid Build Coastguard Worker << "), while BB#" << MBB->getNumber() << " has exit state ("
2037*9880d681SAndroid Build Coastguard Worker << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n";
2038*9880d681SAndroid Build Coastguard Worker }
2039*9880d681SAndroid Build Coastguard Worker }
2040*9880d681SAndroid Build Coastguard Worker
2041*9880d681SAndroid Build Coastguard Worker // Make sure a basic block with return ends with zero stack adjustment.
2042*9880d681SAndroid Build Coastguard Worker if (!MBB->empty() && MBB->back().isReturn()) {
2043*9880d681SAndroid Build Coastguard Worker if (BBState.ExitIsSetup)
2044*9880d681SAndroid Build Coastguard Worker report("A return block ends with a FrameSetup.", MBB);
2045*9880d681SAndroid Build Coastguard Worker if (BBState.ExitValue)
2046*9880d681SAndroid Build Coastguard Worker report("A return block ends with a nonzero stack adjustment.", MBB);
2047*9880d681SAndroid Build Coastguard Worker }
2048*9880d681SAndroid Build Coastguard Worker }
2049*9880d681SAndroid Build Coastguard Worker }
2050