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