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 | |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 26 | #include "llvm/BasicBlock.h" |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 27 | #include "llvm/Instructions.h" |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/LiveVariables.h" |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Jakob Stoklund Olesen | 30e98a0 | 2012-02-29 00:33:41 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineMemOperand.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 36 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCAsmInfo.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Target/TargetRegisterInfo.h" |
| 40 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/DenseSet.h" |
| 42 | #include "llvm/ADT/SetOperations.h" |
| 43 | #include "llvm/ADT/SmallVector.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorHandling.h" |
| 46 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
| 49 | namespace { |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 50 | struct MachineVerifier { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 51 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 52 | MachineVerifier(Pass *pass, const char *b) : |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 53 | PASS(pass), |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 54 | Banner(b), |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 55 | OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS")) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 56 | {} |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 57 | |
| 58 | bool runOnMachineFunction(MachineFunction &MF); |
| 59 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 60 | Pass *const PASS; |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 61 | const char *Banner; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 62 | const char *const OutFileName; |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 63 | raw_ostream *OS; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 64 | const MachineFunction *MF; |
| 65 | const TargetMachine *TM; |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 66 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 67 | const TargetRegisterInfo *TRI; |
| 68 | const MachineRegisterInfo *MRI; |
| 69 | |
| 70 | unsigned foundErrors; |
| 71 | |
| 72 | typedef SmallVector<unsigned, 16> RegVector; |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 73 | typedef SmallVector<const uint32_t*, 4> RegMaskVector; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 74 | typedef DenseSet<unsigned> RegSet; |
| 75 | typedef DenseMap<unsigned, const MachineInstr*> RegMap; |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 76 | typedef SmallPtrSet<const MachineBasicBlock*, 8> BlockSet; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 77 | |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 78 | const MachineInstr *FirstTerminator; |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 79 | BlockSet FunctionBlocks; |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 80 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 81 | BitVector regsReserved; |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 82 | BitVector regsAllocatable; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 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) { |
| 188 | return Reg < regsAllocatable.size() && regsAllocatable.test(Reg); |
| 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); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 215 | |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 216 | void checkLiveness(const MachineOperand *MO, unsigned MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 217 | void markReachable(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 218 | void calcRegsPassed(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 219 | void checkPHIOps(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 220 | |
| 221 | void calcRegsRequired(); |
| 222 | void verifyLiveVariables(); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 223 | void verifyLiveIntervals(); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 224 | void verifyLiveInterval(const LiveInterval&); |
| 225 | void verifyLiveIntervalValue(const LiveInterval&, VNInfo*); |
| 226 | void verifyLiveIntervalSegment(const LiveInterval&, |
| 227 | LiveInterval::const_iterator); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 228 | }; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 229 | |
| 230 | struct MachineVerifierPass : public MachineFunctionPass { |
| 231 | static char ID; // Pass ID, replacement for typeid |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 232 | const char *const Banner; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 233 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 234 | MachineVerifierPass(const char *b = 0) |
| 235 | : MachineFunctionPass(ID), Banner(b) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 236 | initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry()); |
| 237 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 238 | |
| 239 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 240 | AU.setPreservesAll(); |
| 241 | MachineFunctionPass::getAnalysisUsage(AU); |
| 242 | } |
| 243 | |
| 244 | bool runOnMachineFunction(MachineFunction &MF) { |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 245 | MF.verify(this, Banner); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | }; |
| 249 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 252 | char MachineVerifierPass::ID = 0; |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 253 | INITIALIZE_PASS(MachineVerifierPass, "machineverifier", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 254 | "Verify generated machine code", false, false) |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 255 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 256 | FunctionPass *llvm::createMachineVerifierPass(const char *Banner) { |
| 257 | return new MachineVerifierPass(Banner); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 260 | void MachineFunction::verify(Pass *p, const char *Banner) const { |
| 261 | MachineVerifier(p, Banner) |
| 262 | .runOnMachineFunction(const_cast<MachineFunction&>(*this)); |
Jakob Stoklund Olesen | ce727d0 | 2009-11-13 21:56:09 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 265 | bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) { |
| 266 | raw_ostream *OutFile = 0; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 267 | if (OutFileName) { |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 268 | std::string ErrorInfo; |
| 269 | OutFile = new raw_fd_ostream(OutFileName, ErrorInfo, |
| 270 | raw_fd_ostream::F_Append); |
| 271 | if (!ErrorInfo.empty()) { |
| 272 | errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n'; |
| 273 | exit(1); |
| 274 | } |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 276 | OS = OutFile; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 277 | } else { |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 278 | OS = &errs(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | foundErrors = 0; |
| 282 | |
| 283 | this->MF = &MF; |
| 284 | TM = &MF.getTarget(); |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 285 | TII = TM->getInstrInfo(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 286 | TRI = TM->getRegisterInfo(); |
| 287 | MRI = &MF.getRegInfo(); |
| 288 | |
Jakob Stoklund Olesen | c910c8d | 2010-08-05 23:51:26 +0000 | [diff] [blame] | 289 | LiveVars = NULL; |
| 290 | LiveInts = NULL; |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 291 | LiveStks = NULL; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 292 | Indexes = NULL; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 293 | if (PASS) { |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 294 | LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>(); |
Jakob Stoklund Olesen | c910c8d | 2010-08-05 23:51:26 +0000 | [diff] [blame] | 295 | // We don't want to verify LiveVariables if LiveIntervals is available. |
| 296 | if (!LiveInts) |
| 297 | LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>(); |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 298 | LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>(); |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 299 | Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>(); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 302 | visitMachineFunctionBefore(); |
| 303 | for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end(); |
| 304 | MFI!=MFE; ++MFI) { |
| 305 | visitMachineBasicBlockBefore(MFI); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 306 | // Keep track of the current bundle header. |
| 307 | const MachineInstr *CurBundle = 0; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 308 | for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(), |
| 309 | MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) { |
Jakob Stoklund Olesen | 7bd46da | 2011-01-12 21:27:41 +0000 | [diff] [blame] | 310 | if (MBBI->getParent() != MFI) { |
| 311 | report("Bad instruction parent pointer", MFI); |
| 312 | *OS << "Instruction: " << *MBBI; |
| 313 | continue; |
| 314 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 315 | // Is this a bundle header? |
| 316 | if (!MBBI->isInsideBundle()) { |
| 317 | if (CurBundle) |
| 318 | visitMachineBundleAfter(CurBundle); |
| 319 | CurBundle = MBBI; |
| 320 | visitMachineBundleBefore(CurBundle); |
| 321 | } else if (!CurBundle) |
| 322 | report("No bundle header", MBBI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 323 | visitMachineInstrBefore(MBBI); |
| 324 | for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) |
| 325 | visitMachineOperand(&MBBI->getOperand(I), I); |
| 326 | visitMachineInstrAfter(MBBI); |
| 327 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 328 | if (CurBundle) |
| 329 | visitMachineBundleAfter(CurBundle); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 330 | visitMachineBasicBlockAfter(MFI); |
| 331 | } |
| 332 | visitMachineFunctionAfter(); |
| 333 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 334 | if (OutFile) |
| 335 | delete OutFile; |
| 336 | else if (foundErrors) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 337 | report_fatal_error("Found "+Twine(foundErrors)+" machine code errors."); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 338 | |
Jakob Stoklund Olesen | 6349668 | 2009-08-08 15:34:50 +0000 | [diff] [blame] | 339 | // Clean up. |
| 340 | regsLive.clear(); |
| 341 | regsDefined.clear(); |
| 342 | regsDead.clear(); |
| 343 | regsKilled.clear(); |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 344 | regMasks.clear(); |
Jakob Stoklund Olesen | 6349668 | 2009-08-08 15:34:50 +0000 | [diff] [blame] | 345 | regsLiveInButUnused.clear(); |
| 346 | MBBInfoMap.clear(); |
| 347 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 348 | return false; // no changes |
| 349 | } |
| 350 | |
Chris Lattner | 372fefe | 2009-08-23 01:03:30 +0000 | [diff] [blame] | 351 | void MachineVerifier::report(const char *msg, const MachineFunction *MF) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 352 | assert(MF); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 353 | *OS << '\n'; |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 354 | if (!foundErrors++) { |
| 355 | if (Banner) |
| 356 | *OS << "# " << Banner << '\n'; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 357 | MF->print(*OS, Indexes); |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 358 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 359 | *OS << "*** Bad machine code: " << msg << " ***\n" |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 360 | << "- function: " << MF->getName() << "\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 363 | void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 364 | assert(MBB); |
| 365 | report(msg, MBB->getParent()); |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 366 | *OS << "- basic block: BB#" << MBB->getNumber() |
| 367 | << ' ' << MBB->getName() |
| 368 | << " (" << (void*)MBB << ')'; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 369 | if (Indexes) |
| 370 | *OS << " [" << Indexes->getMBBStartIdx(MBB) |
| 371 | << ';' << Indexes->getMBBEndIdx(MBB) << ')'; |
| 372 | *OS << '\n'; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 375 | void MachineVerifier::report(const char *msg, const MachineInstr *MI) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 376 | assert(MI); |
| 377 | report(msg, MI->getParent()); |
| 378 | *OS << "- instruction: "; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 379 | if (Indexes && Indexes->hasIndex(MI)) |
| 380 | *OS << Indexes->getInstructionIndex(MI) << '\t'; |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 381 | MI->print(*OS, TM); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 384 | void MachineVerifier::report(const char *msg, |
| 385 | const MachineOperand *MO, unsigned MONum) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 386 | assert(MO); |
| 387 | report(msg, MO->getParent()); |
| 388 | *OS << "- operand " << MONum << ": "; |
| 389 | MO->print(*OS, TM); |
| 390 | *OS << "\n"; |
| 391 | } |
| 392 | |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 393 | void MachineVerifier::report(const char *msg, const MachineFunction *MF, |
| 394 | const LiveInterval &LI) { |
| 395 | report(msg, MF); |
| 396 | *OS << "- interval: "; |
| 397 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) |
| 398 | *OS << PrintReg(LI.reg, TRI); |
| 399 | else |
| 400 | *OS << PrintRegUnit(LI.reg, TRI); |
| 401 | *OS << ' ' << LI << '\n'; |
| 402 | } |
| 403 | |
| 404 | void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB, |
| 405 | const LiveInterval &LI) { |
| 406 | report(msg, MBB); |
| 407 | *OS << "- interval: "; |
| 408 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) |
| 409 | *OS << PrintReg(LI.reg, TRI); |
| 410 | else |
| 411 | *OS << PrintRegUnit(LI.reg, TRI); |
| 412 | *OS << ' ' << LI << '\n'; |
| 413 | } |
| 414 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 415 | void MachineVerifier::markReachable(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 416 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 417 | if (!MInfo.reachable) { |
| 418 | MInfo.reachable = true; |
| 419 | for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), |
| 420 | SuE = MBB->succ_end(); SuI != SuE; ++SuI) |
| 421 | markReachable(*SuI); |
| 422 | } |
| 423 | } |
| 424 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 425 | void MachineVerifier::visitMachineFunctionBefore() { |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 426 | lastIndex = SlotIndex(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 427 | regsReserved = TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 428 | |
| 429 | // A sub-register of a reserved register is also reserved |
| 430 | for (int Reg = regsReserved.find_first(); Reg>=0; |
| 431 | Reg = regsReserved.find_next(Reg)) { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 432 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 433 | // FIXME: This should probably be: |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 434 | // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register"); |
| 435 | regsReserved.set(*SubRegs); |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 436 | } |
| 437 | } |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 438 | |
| 439 | regsAllocatable = TRI->getAllocatableSet(*MF); |
| 440 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 441 | markReachable(&MF->front()); |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 442 | |
| 443 | // Build a set of the basic blocks in the function. |
| 444 | FunctionBlocks.clear(); |
| 445 | for (MachineFunction::const_iterator |
| 446 | I = MF->begin(), E = MF->end(); I != E; ++I) { |
| 447 | FunctionBlocks.insert(I); |
| 448 | BBInfo &MInfo = MBBInfoMap[I]; |
| 449 | |
| 450 | MInfo.Preds.insert(I->pred_begin(), I->pred_end()); |
| 451 | if (MInfo.Preds.size() != I->pred_size()) |
| 452 | report("MBB has duplicate entries in its predecessor list.", I); |
| 453 | |
| 454 | MInfo.Succs.insert(I->succ_begin(), I->succ_end()); |
| 455 | if (MInfo.Succs.size() != I->succ_size()) |
| 456 | report("MBB has duplicate entries in its successor list.", I); |
| 457 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 460 | // Does iterator point to a and b as the first two elements? |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 461 | static bool matchPair(MachineBasicBlock::const_succ_iterator i, |
| 462 | const MachineBasicBlock *a, const MachineBasicBlock *b) { |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 463 | if (*i == a) |
| 464 | return *++i == b; |
| 465 | if (*i == b) |
| 466 | return *++i == a; |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | void |
| 471 | MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 472 | FirstTerminator = 0; |
| 473 | |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 474 | if (MRI->isSSA()) { |
| 475 | // If this block has allocatable physical registers live-in, check that |
| 476 | // it is an entry block or landing pad. |
| 477 | for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(), |
| 478 | LE = MBB->livein_end(); |
| 479 | LI != LE; ++LI) { |
| 480 | unsigned reg = *LI; |
| 481 | if (isAllocatable(reg) && !MBB->isLandingPad() && |
| 482 | MBB != MBB->getParent()->begin()) { |
| 483 | report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 488 | // Count the number of landing pad successors. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 489 | SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs; |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 490 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 491 | E = MBB->succ_end(); I != E; ++I) { |
| 492 | if ((*I)->isLandingPad()) |
| 493 | LandingPadSuccs.insert(*I); |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 494 | if (!FunctionBlocks.count(*I)) |
| 495 | report("MBB has successor that isn't part of the function.", MBB); |
| 496 | if (!MBBInfoMap[*I].Preds.count(MBB)) { |
| 497 | report("Inconsistent CFG", MBB); |
| 498 | *OS << "MBB is not in the predecessor list of the successor BB#" |
| 499 | << (*I)->getNumber() << ".\n"; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Check the predecessor list. |
| 504 | for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(), |
| 505 | E = MBB->pred_end(); I != E; ++I) { |
| 506 | if (!FunctionBlocks.count(*I)) |
| 507 | report("MBB has predecessor that isn't part of the function.", MBB); |
| 508 | if (!MBBInfoMap[*I].Succs.count(MBB)) { |
| 509 | report("Inconsistent CFG", MBB); |
| 510 | *OS << "MBB is not in the successor list of the predecessor BB#" |
| 511 | << (*I)->getNumber() << ".\n"; |
| 512 | } |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 513 | } |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 514 | |
| 515 | const MCAsmInfo *AsmInfo = TM->getMCAsmInfo(); |
| 516 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 517 | if (LandingPadSuccs.size() > 1 && |
| 518 | !(AsmInfo && |
| 519 | AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj && |
| 520 | BB && isa<SwitchInst>(BB->getTerminator()))) |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 521 | report("MBB has more than one landing pad successor", MBB); |
| 522 | |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 523 | // Call AnalyzeBranch. If it succeeds, there several more conditions to check. |
| 524 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 525 | SmallVector<MachineOperand, 4> Cond; |
| 526 | if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB), |
| 527 | TBB, FBB, Cond)) { |
| 528 | // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's |
| 529 | // check whether its answers match up with reality. |
| 530 | if (!TBB && !FBB) { |
| 531 | // Block falls through to its successor. |
| 532 | MachineFunction::const_iterator MBBI = MBB; |
| 533 | ++MBBI; |
| 534 | if (MBBI == MF->end()) { |
Dan Gohman | a01a80fa | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 535 | // It's possible that the block legitimately ends with a noreturn |
| 536 | // call or an unreachable, in which case it won't actually fall |
| 537 | // out the bottom of the function. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 538 | } else if (MBB->succ_size() == LandingPadSuccs.size()) { |
Dan Gohman | a01a80fa | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 539 | // It's possible that the block legitimately ends with a noreturn |
| 540 | // call or an unreachable, in which case it won't actuall fall |
| 541 | // out of the block. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 542 | } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 543 | report("MBB exits via unconditional fall-through but doesn't have " |
| 544 | "exactly one CFG successor!", MBB); |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 545 | } else if (!MBB->isSuccessor(MBBI)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 546 | report("MBB exits via unconditional fall-through but its successor " |
| 547 | "differs from its CFG successor!", MBB); |
| 548 | } |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 549 | if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() && |
| 550 | !TII->isPredicated(getBundleStart(&MBB->back()))) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 551 | report("MBB exits via unconditional fall-through but ends with a " |
| 552 | "barrier instruction!", MBB); |
| 553 | } |
| 554 | if (!Cond.empty()) { |
| 555 | report("MBB exits via unconditional fall-through but has a condition!", |
| 556 | MBB); |
| 557 | } |
| 558 | } else if (TBB && !FBB && Cond.empty()) { |
| 559 | // Block unconditionally branches somewhere. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 560 | if (MBB->succ_size() != 1+LandingPadSuccs.size()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 561 | report("MBB exits via unconditional branch but doesn't have " |
| 562 | "exactly one CFG successor!", MBB); |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 563 | } else if (!MBB->isSuccessor(TBB)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 564 | report("MBB exits via unconditional branch but the CFG " |
| 565 | "successor doesn't match the actual successor!", MBB); |
| 566 | } |
| 567 | if (MBB->empty()) { |
| 568 | report("MBB exits via unconditional branch but doesn't contain " |
| 569 | "any instructions!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 570 | } else if (!getBundleStart(&MBB->back())->isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 571 | report("MBB exits via unconditional branch but doesn't end with a " |
| 572 | "barrier instruction!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 573 | } else if (!getBundleStart(&MBB->back())->isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 574 | report("MBB exits via unconditional branch but the branch isn't a " |
| 575 | "terminator instruction!", MBB); |
| 576 | } |
| 577 | } else if (TBB && !FBB && !Cond.empty()) { |
| 578 | // Block conditionally branches somewhere, otherwise falls through. |
| 579 | MachineFunction::const_iterator MBBI = MBB; |
| 580 | ++MBBI; |
| 581 | if (MBBI == MF->end()) { |
| 582 | report("MBB conditionally falls through out of function!", MBB); |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 583 | } if (MBB->succ_size() == 1) { |
| 584 | // A conditional branch with only one successor is weird, but allowed. |
| 585 | if (&*MBBI != TBB) |
| 586 | report("MBB exits via conditional branch/fall-through but only has " |
| 587 | "one CFG successor!", MBB); |
| 588 | else if (TBB != *MBB->succ_begin()) |
| 589 | report("MBB exits via conditional branch/fall-through but the CFG " |
| 590 | "successor don't match the actual successor!", MBB); |
| 591 | } else if (MBB->succ_size() != 2) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 592 | report("MBB exits via conditional branch/fall-through but doesn't have " |
| 593 | "exactly two CFG successors!", MBB); |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 594 | } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 595 | report("MBB exits via conditional branch/fall-through but the CFG " |
| 596 | "successors don't match the actual successors!", MBB); |
| 597 | } |
| 598 | if (MBB->empty()) { |
| 599 | report("MBB exits via conditional branch/fall-through but doesn't " |
| 600 | "contain any instructions!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 601 | } else if (getBundleStart(&MBB->back())->isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 602 | report("MBB exits via conditional branch/fall-through but ends with a " |
| 603 | "barrier instruction!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 604 | } else if (!getBundleStart(&MBB->back())->isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 605 | report("MBB exits via conditional branch/fall-through but the branch " |
| 606 | "isn't a terminator instruction!", MBB); |
| 607 | } |
| 608 | } else if (TBB && FBB) { |
| 609 | // Block conditionally branches somewhere, otherwise branches |
| 610 | // somewhere else. |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 611 | if (MBB->succ_size() == 1) { |
| 612 | // A conditional branch with only one successor is weird, but allowed. |
| 613 | if (FBB != TBB) |
| 614 | report("MBB exits via conditional branch/branch through but only has " |
| 615 | "one CFG successor!", MBB); |
| 616 | else if (TBB != *MBB->succ_begin()) |
| 617 | report("MBB exits via conditional branch/branch through but the CFG " |
| 618 | "successor don't match the actual successor!", MBB); |
| 619 | } else if (MBB->succ_size() != 2) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 620 | report("MBB exits via conditional branch/branch but doesn't have " |
| 621 | "exactly two CFG successors!", MBB); |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 622 | } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 623 | report("MBB exits via conditional branch/branch but the CFG " |
| 624 | "successors don't match the actual successors!", MBB); |
| 625 | } |
| 626 | if (MBB->empty()) { |
| 627 | report("MBB exits via conditional branch/branch but doesn't " |
| 628 | "contain any instructions!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 629 | } else if (!getBundleStart(&MBB->back())->isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 630 | report("MBB exits via conditional branch/branch but doesn't end with a " |
| 631 | "barrier instruction!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 632 | } else if (!getBundleStart(&MBB->back())->isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 633 | report("MBB exits via conditional branch/branch but the branch " |
| 634 | "isn't a terminator instruction!", MBB); |
| 635 | } |
| 636 | if (Cond.empty()) { |
| 637 | report("MBB exits via conditinal branch/branch but there's no " |
| 638 | "condition!", MBB); |
| 639 | } |
| 640 | } else { |
| 641 | report("AnalyzeBranch returned invalid data!", MBB); |
| 642 | } |
| 643 | } |
| 644 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 645 | regsLive.clear(); |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 646 | for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(), |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 647 | E = MBB->livein_end(); I != E; ++I) { |
| 648 | if (!TargetRegisterInfo::isPhysicalRegister(*I)) { |
| 649 | report("MBB live-in list contains non-physical register", MBB); |
| 650 | continue; |
| 651 | } |
| 652 | regsLive.insert(*I); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 653 | for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs) |
| 654 | regsLive.insert(*SubRegs); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 655 | } |
Jakob Stoklund Olesen | 710b13b | 2009-08-08 13:19:25 +0000 | [diff] [blame] | 656 | regsLiveInButUnused = regsLive; |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 657 | |
| 658 | const MachineFrameInfo *MFI = MF->getFrameInfo(); |
| 659 | assert(MFI && "Function has no frame info"); |
| 660 | BitVector PR = MFI->getPristineRegs(MBB); |
| 661 | for (int I = PR.find_first(); I>0; I = PR.find_next(I)) { |
| 662 | regsLive.insert(I); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 663 | for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs) |
| 664 | regsLive.insert(*SubRegs); |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 667 | regsKilled.clear(); |
| 668 | regsDefined.clear(); |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 669 | |
| 670 | if (Indexes) |
| 671 | lastIndex = Indexes->getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 674 | // This function gets called for all bundle headers, including normal |
| 675 | // stand-alone unbundled instructions. |
| 676 | void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) { |
| 677 | if (Indexes && Indexes->hasIndex(MI)) { |
| 678 | SlotIndex idx = Indexes->getInstructionIndex(MI); |
| 679 | if (!(idx > lastIndex)) { |
| 680 | report("Instruction index out of order", MI); |
| 681 | *OS << "Last instruction was at " << lastIndex << '\n'; |
| 682 | } |
| 683 | lastIndex = idx; |
| 684 | } |
Pete Cooper | 83569cb | 2012-06-07 17:41:39 +0000 | [diff] [blame] | 685 | |
| 686 | // Ensure non-terminators don't follow terminators. |
| 687 | // Ignore predicated terminators formed by if conversion. |
| 688 | // FIXME: If conversion shouldn't need to violate this rule. |
| 689 | if (MI->isTerminator() && !TII->isPredicated(MI)) { |
| 690 | if (!FirstTerminator) |
| 691 | FirstTerminator = MI; |
| 692 | } else if (FirstTerminator) { |
| 693 | report("Non-terminator instruction after the first terminator", MI); |
| 694 | *OS << "First terminator was:\t" << *FirstTerminator; |
| 695 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 698 | void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 699 | const MCInstrDesc &MCID = MI->getDesc(); |
| 700 | if (MI->getNumOperands() < MCID.getNumOperands()) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 701 | report("Too few operands", MI); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 702 | *OS << MCID.getNumOperands() << " operands expected, but " |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 703 | << MI->getNumExplicitOperands() << " given.\n"; |
| 704 | } |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 705 | |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame^] | 706 | // Check the tied operands. |
| 707 | SmallVector<unsigned, 4> TiedDefs; |
| 708 | SmallVector<unsigned, 4> TiedUses; |
| 709 | for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { |
| 710 | const MachineOperand &MO = MI->getOperand(i); |
| 711 | if (!MO.isReg() || !MO.isTied()) |
| 712 | continue; |
| 713 | if (MO.isDef()) { |
| 714 | TiedDefs.push_back(i); |
| 715 | continue; |
| 716 | } |
| 717 | TiedUses.push_back(i); |
| 718 | if (TiedDefs.size() < TiedUses.size()) { |
| 719 | report("No tied def for tied use", &MO, i); |
| 720 | break; |
| 721 | } |
| 722 | if (i >= MCID.getNumOperands()) |
| 723 | continue; |
| 724 | int DefIdx = MCID.getOperandConstraint(i, MCOI::TIED_TO); |
| 725 | if (unsigned(DefIdx) != TiedDefs[TiedUses.size() - 1]) { |
| 726 | report("Tied def doesn't match MCInstrDesc", &MO, i); |
| 727 | *OS << "Descriptor says tied def should be operand " << DefIdx << ".\n"; |
| 728 | } |
| 729 | } |
| 730 | if (TiedDefs.size() > TiedUses.size()) { |
| 731 | unsigned i = TiedDefs[TiedUses.size() - 1]; |
| 732 | report("No tied use for tied def", &MI->getOperand(i), i); |
| 733 | } |
| 734 | |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 735 | // Check the MachineMemOperands for basic consistency. |
| 736 | for (MachineInstr::mmo_iterator I = MI->memoperands_begin(), |
| 737 | E = MI->memoperands_end(); I != E; ++I) { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 738 | if ((*I)->isLoad() && !MI->mayLoad()) |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 739 | report("Missing mayLoad flag", MI); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 740 | if ((*I)->isStore() && !MI->mayStore()) |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 741 | report("Missing mayStore flag", MI); |
| 742 | } |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 743 | |
| 744 | // Debug values must not have a slot index. |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 745 | // Other instructions must have one, unless they are inside a bundle. |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 746 | if (LiveInts) { |
| 747 | bool mapped = !LiveInts->isNotInMIMap(MI); |
| 748 | if (MI->isDebugValue()) { |
| 749 | if (mapped) |
| 750 | report("Debug instruction has a slot index", MI); |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 751 | } else if (MI->isInsideBundle()) { |
| 752 | if (mapped) |
| 753 | report("Instruction inside bundle has a slot index", MI); |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 754 | } else { |
| 755 | if (!mapped) |
| 756 | report("Missing slot index", MI); |
| 757 | } |
| 758 | } |
| 759 | |
Andrew Trick | 3be654f | 2011-09-21 02:20:46 +0000 | [diff] [blame] | 760 | StringRef ErrorInfo; |
| 761 | if (!TII->verifyInstruction(MI, ErrorInfo)) |
| 762 | report(ErrorInfo.data(), MI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 766 | MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 767 | const MachineInstr *MI = MO->getParent(); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 768 | const MCInstrDesc &MCID = MI->getDesc(); |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 769 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 770 | // The first MCID.NumDefs operands must be explicit register defines |
| 771 | if (MONum < MCID.getNumDefs()) { |
Richard Smith | 11a4fa4 | 2012-08-15 01:39:31 +0000 | [diff] [blame] | 772 | const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 773 | if (!MO->isReg()) |
| 774 | report("Explicit definition must be a register", MO, MONum); |
Evan Cheng | cac58aa | 2012-05-29 19:40:44 +0000 | [diff] [blame] | 775 | else if (!MO->isDef() && !MCOI.isOptionalDef()) |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 776 | report("Explicit definition marked as use", MO, MONum); |
| 777 | else if (MO->isImplicit()) |
| 778 | report("Explicit definition marked as implicit", MO, MONum); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 779 | } else if (MONum < MCID.getNumOperands()) { |
Richard Smith | 11a4fa4 | 2012-08-15 01:39:31 +0000 | [diff] [blame] | 780 | const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; |
Eric Christopher | 113a06c | 2010-11-17 00:55:36 +0000 | [diff] [blame] | 781 | // Don't check if it's the last operand in a variadic instruction. See, |
| 782 | // e.g., LDM_RET in the arm back end. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 783 | if (MO->isReg() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 784 | !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 785 | if (MO->isDef() && !MCOI.isOptionalDef()) |
Cameron Zwarich | 22d67cf | 2010-12-19 21:37:23 +0000 | [diff] [blame] | 786 | report("Explicit operand marked as def", MO, MONum); |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 787 | if (MO->isImplicit()) |
| 788 | report("Explicit operand marked as implicit", MO, MONum); |
| 789 | } |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame^] | 790 | |
| 791 | if (MCID.getOperandConstraint(MONum, MCOI::TIED_TO) != -1) { |
| 792 | if (!MO->isReg()) |
| 793 | report("Tied use must be a register", MO, MONum); |
| 794 | else if (!MO->isTied()) |
| 795 | report("Operand should be tied", MO, MONum); |
| 796 | } else if (MO->isReg() && MO->isTied()) |
| 797 | report("Explicit operand should not be tied", MO, MONum); |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 798 | } else { |
Jakob Stoklund Olesen | 5711564 | 2009-12-22 21:48:20 +0000 | [diff] [blame] | 799 | // ARM adds %reg0 operands to indicate predicates. We'll allow that. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 800 | if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg()) |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 801 | report("Extra explicit operand on non-variadic instruction", MO, MONum); |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 804 | switch (MO->getType()) { |
| 805 | case MachineOperand::MO_Register: { |
| 806 | const unsigned Reg = MO->getReg(); |
| 807 | if (!Reg) |
| 808 | return; |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 809 | if (MRI->tracksLiveness() && !MI->isDebugValue()) |
| 810 | checkLiveness(MO, MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 811 | |
Jakob Stoklund Olesen | eba2bbb | 2012-07-25 16:49:11 +0000 | [diff] [blame] | 812 | // Verify two-address constraints after leaving SSA form. |
| 813 | unsigned DefIdx; |
| 814 | if (!MRI->isSSA() && MO->isUse() && |
| 815 | MI->isRegTiedToDefOperand(MONum, &DefIdx) && |
| 816 | Reg != MI->getOperand(DefIdx).getReg()) |
| 817 | report("Two-address instruction operands must be identical", MO, MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 818 | |
| 819 | // Check register classes. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 820 | if (MONum < MCID.getNumOperands() && !MO->isImplicit()) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 821 | unsigned SubIdx = MO->getSubReg(); |
| 822 | |
| 823 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 824 | if (SubIdx) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 825 | report("Illegal subregister index for physical register", MO, MONum); |
| 826 | return; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 827 | } |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 828 | if (const TargetRegisterClass *DRC = |
| 829 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 830 | if (!DRC->contains(Reg)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 831 | report("Illegal physical register for instruction", MO, MONum); |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 832 | *OS << TRI->getName(Reg) << " is not a " |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 833 | << DRC->getName() << " register.\n"; |
| 834 | } |
| 835 | } |
| 836 | } else { |
| 837 | // Virtual register. |
| 838 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 839 | if (SubIdx) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 840 | const TargetRegisterClass *SRC = |
| 841 | TRI->getSubClassWithSubReg(RC, SubIdx); |
Jakob Stoklund Olesen | 6a8d2c6 | 2010-05-18 17:31:12 +0000 | [diff] [blame] | 842 | if (!SRC) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 843 | report("Invalid subregister index for virtual register", MO, MONum); |
Jakob Stoklund Olesen | 6a8d2c6 | 2010-05-18 17:31:12 +0000 | [diff] [blame] | 844 | *OS << "Register class " << RC->getName() |
| 845 | << " does not support subreg index " << SubIdx << "\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 846 | return; |
| 847 | } |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 848 | if (RC != SRC) { |
| 849 | report("Invalid register class for subregister index", MO, MONum); |
| 850 | *OS << "Register class " << RC->getName() |
| 851 | << " does not fully support subreg index " << SubIdx << "\n"; |
| 852 | return; |
| 853 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 854 | } |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 855 | if (const TargetRegisterClass *DRC = |
| 856 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 857 | if (SubIdx) { |
| 858 | const TargetRegisterClass *SuperRC = |
| 859 | TRI->getLargestLegalSuperClass(RC); |
| 860 | if (!SuperRC) { |
| 861 | report("No largest legal super class exists.", MO, MONum); |
| 862 | return; |
| 863 | } |
| 864 | DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx); |
| 865 | if (!DRC) { |
| 866 | report("No matching super-reg register class.", MO, MONum); |
| 867 | return; |
| 868 | } |
| 869 | } |
Jakob Stoklund Olesen | fa226bc | 2011-06-02 05:43:46 +0000 | [diff] [blame] | 870 | if (!RC->hasSuperClassEq(DRC)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 871 | report("Illegal virtual register for instruction", MO, MONum); |
| 872 | *OS << "Expected a " << DRC->getName() << " register, but got a " |
| 873 | << RC->getName() << " register\n"; |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | break; |
| 879 | } |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 880 | |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 881 | case MachineOperand::MO_RegisterMask: |
| 882 | regMasks.push_back(MO->getRegMask()); |
| 883 | break; |
| 884 | |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 885 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 886 | if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent())) |
| 887 | report("PHI operand is not in the CFG", MO, MONum); |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 888 | break; |
| 889 | |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 890 | case MachineOperand::MO_FrameIndex: |
| 891 | if (LiveStks && LiveStks->hasInterval(MO->getIndex()) && |
| 892 | LiveInts && !LiveInts->isNotInMIMap(MI)) { |
| 893 | LiveInterval &LI = LiveStks->getInterval(MO->getIndex()); |
| 894 | SlotIndex Idx = LiveInts->getInstructionIndex(MI); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 895 | if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) { |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 896 | report("Instruction loads from dead spill slot", MO, MONum); |
| 897 | *OS << "Live stack: " << LI << '\n'; |
| 898 | } |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 899 | if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) { |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 900 | report("Instruction stores to dead spill slot", MO, MONum); |
| 901 | *OS << "Live stack: " << LI << '\n'; |
| 902 | } |
| 903 | } |
| 904 | break; |
| 905 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 906 | default: |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 911 | void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) { |
| 912 | const MachineInstr *MI = MO->getParent(); |
| 913 | const unsigned Reg = MO->getReg(); |
| 914 | |
| 915 | // Both use and def operands can read a register. |
| 916 | if (MO->readsReg()) { |
| 917 | regsLiveInButUnused.erase(Reg); |
| 918 | |
Jakob Stoklund Olesen | eba2bbb | 2012-07-25 16:49:11 +0000 | [diff] [blame] | 919 | if (MO->isKill()) |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 920 | addRegWithSubRegs(regsKilled, Reg); |
| 921 | |
| 922 | // Check that LiveVars knows this kill. |
| 923 | if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 924 | MO->isKill()) { |
| 925 | LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); |
| 926 | if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end()) |
| 927 | report("Kill missing from LiveVariables", MO, MONum); |
| 928 | } |
| 929 | |
| 930 | // Check LiveInts liveness and kill. |
Jakob Stoklund Olesen | a62e1e8 | 2012-08-01 23:52:40 +0000 | [diff] [blame] | 931 | if (LiveInts && !LiveInts->isNotInMIMap(MI)) { |
| 932 | SlotIndex UseIdx = LiveInts->getInstructionIndex(MI); |
| 933 | // Check the cached regunit intervals. |
| 934 | if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) { |
| 935 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) { |
| 936 | if (const LiveInterval *LI = LiveInts->getCachedRegUnit(*Units)) { |
| 937 | LiveRangeQuery LRQ(*LI, UseIdx); |
| 938 | if (!LRQ.valueIn()) { |
| 939 | report("No live range at use", MO, MONum); |
| 940 | *OS << UseIdx << " is not live in " << PrintRegUnit(*Units, TRI) |
| 941 | << ' ' << *LI << '\n'; |
| 942 | } |
| 943 | if (MO->isKill() && !LRQ.isKill()) { |
| 944 | report("Live range continues after kill flag", MO, MONum); |
| 945 | *OS << PrintRegUnit(*Units, TRI) << ' ' << *LI << '\n'; |
| 946 | } |
| 947 | } |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 948 | } |
Jakob Stoklund Olesen | a62e1e8 | 2012-08-01 23:52:40 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 952 | if (LiveInts->hasInterval(Reg)) { |
| 953 | // This is a virtual register interval. |
| 954 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 955 | LiveRangeQuery LRQ(LI, UseIdx); |
| 956 | if (!LRQ.valueIn()) { |
| 957 | report("No live range at use", MO, MONum); |
| 958 | *OS << UseIdx << " is not live in " << LI << '\n'; |
| 959 | } |
| 960 | // Check for extra kill flags. |
| 961 | // Note that we allow missing kill flags for now. |
| 962 | if (MO->isKill() && !LRQ.isKill()) { |
| 963 | report("Live range continues after kill flag", MO, MONum); |
| 964 | *OS << "Live range: " << LI << '\n'; |
| 965 | } |
| 966 | } else { |
| 967 | report("Virtual register has no live interval", MO, MONum); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 968 | } |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 969 | } |
| 970 | } |
| 971 | |
| 972 | // Use of a dead register. |
| 973 | if (!regsLive.count(Reg)) { |
| 974 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 975 | // Reserved registers may be used even when 'dead'. |
| 976 | if (!isReserved(Reg)) |
| 977 | report("Using an undefined physical register", MO, MONum); |
Pete Cooper | b97c57a | 2012-07-19 23:40:38 +0000 | [diff] [blame] | 978 | } else if (MRI->def_empty(Reg)) { |
| 979 | report("Reading virtual register without a def", MO, MONum); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 980 | } else { |
| 981 | BBInfo &MInfo = MBBInfoMap[MI->getParent()]; |
| 982 | // We don't know which virtual registers are live in, so only complain |
| 983 | // if vreg was killed in this MBB. Otherwise keep track of vregs that |
| 984 | // must be live in. PHI instructions are handled separately. |
| 985 | if (MInfo.regsKilled.count(Reg)) |
| 986 | report("Using a killed virtual register", MO, MONum); |
| 987 | else if (!MI->isPHI()) |
| 988 | MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI)); |
| 989 | } |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | if (MO->isDef()) { |
| 994 | // Register defined. |
| 995 | // TODO: verify that earlyclobber ops are not used. |
| 996 | if (MO->isDead()) |
| 997 | addRegWithSubRegs(regsDead, Reg); |
| 998 | else |
| 999 | addRegWithSubRegs(regsDefined, Reg); |
| 1000 | |
| 1001 | // Verify SSA form. |
| 1002 | if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 1003 | llvm::next(MRI->def_begin(Reg)) != MRI->def_end()) |
| 1004 | report("Multiple virtual register defs in SSA form", MO, MONum); |
| 1005 | |
| 1006 | // Check LiveInts for a live range, but only for virtual registers. |
| 1007 | if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 1008 | !LiveInts->isNotInMIMap(MI)) { |
Jakob Stoklund Olesen | f935e94 | 2012-06-22 22:23:58 +0000 | [diff] [blame] | 1009 | SlotIndex DefIdx = LiveInts->getInstructionIndex(MI); |
| 1010 | DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber()); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1011 | if (LiveInts->hasInterval(Reg)) { |
| 1012 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 1013 | if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) { |
| 1014 | assert(VNI && "NULL valno is not allowed"); |
Jakob Stoklund Olesen | f935e94 | 2012-06-22 22:23:58 +0000 | [diff] [blame] | 1015 | if (VNI->def != DefIdx) { |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1016 | report("Inconsistent valno->def", MO, MONum); |
| 1017 | *OS << "Valno " << VNI->id << " is not defined at " |
| 1018 | << DefIdx << " in " << LI << '\n'; |
| 1019 | } |
| 1020 | } else { |
| 1021 | report("No live range at def", MO, MONum); |
| 1022 | *OS << DefIdx << " is not live in " << LI << '\n'; |
| 1023 | } |
| 1024 | } else { |
| 1025 | report("Virtual register has no Live interval", MO, MONum); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1031 | void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | // This function gets called after visiting all instructions in a bundle. The |
| 1035 | // argument points to the bundle header. |
| 1036 | // Normal stand-alone instructions are also considered 'bundles', and this |
| 1037 | // function is called for all of them. |
| 1038 | void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1039 | BBInfo &MInfo = MBBInfoMap[MI->getParent()]; |
| 1040 | set_union(MInfo.regsKilled, regsKilled); |
Jakob Stoklund Olesen | 73cf709 | 2010-08-05 18:59:59 +0000 | [diff] [blame] | 1041 | set_subtract(regsLive, regsKilled); regsKilled.clear(); |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 1042 | // Kill any masked registers. |
| 1043 | while (!regMasks.empty()) { |
| 1044 | const uint32_t *Mask = regMasks.pop_back_val(); |
| 1045 | for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I) |
| 1046 | if (TargetRegisterInfo::isPhysicalRegister(*I) && |
| 1047 | MachineOperand::clobbersPhysReg(Mask, *I)) |
| 1048 | regsDead.push_back(*I); |
| 1049 | } |
Jakob Stoklund Olesen | 73cf709 | 2010-08-05 18:59:59 +0000 | [diff] [blame] | 1050 | set_subtract(regsLive, regsDead); regsDead.clear(); |
| 1051 | set_union(regsLive, regsDefined); regsDefined.clear(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | void |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1055 | MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1056 | MBBInfoMap[MBB].regsLiveOut = regsLive; |
| 1057 | regsLive.clear(); |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 1058 | |
| 1059 | if (Indexes) { |
| 1060 | SlotIndex stop = Indexes->getMBBEndIdx(MBB); |
| 1061 | if (!(stop > lastIndex)) { |
| 1062 | report("Block ends before last instruction index", MBB); |
| 1063 | *OS << "Block ends at " << stop |
| 1064 | << " last instruction was at " << lastIndex << '\n'; |
| 1065 | } |
| 1066 | lastIndex = stop; |
| 1067 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | // Calculate the largest possible vregsPassed sets. These are the registers that |
| 1071 | // can pass through an MBB live, but may not be live every time. It is assumed |
| 1072 | // that all vregsPassed sets are empty before the call. |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1073 | void MachineVerifier::calcRegsPassed() { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1074 | // First push live-out regs to successors' vregsPassed. Remember the MBBs that |
| 1075 | // have any vregsPassed. |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1076 | SmallPtrSet<const MachineBasicBlock*, 8> todo; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1077 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1078 | MFI != MFE; ++MFI) { |
| 1079 | const MachineBasicBlock &MBB(*MFI); |
| 1080 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 1081 | if (!MInfo.reachable) |
| 1082 | continue; |
| 1083 | for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(), |
| 1084 | SuE = MBB.succ_end(); SuI != SuE; ++SuI) { |
| 1085 | BBInfo &SInfo = MBBInfoMap[*SuI]; |
| 1086 | if (SInfo.addPassed(MInfo.regsLiveOut)) |
| 1087 | todo.insert(*SuI); |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | // Iteratively push vregsPassed to successors. This will converge to the same |
| 1092 | // final state regardless of DenseSet iteration order. |
| 1093 | while (!todo.empty()) { |
| 1094 | const MachineBasicBlock *MBB = *todo.begin(); |
| 1095 | todo.erase(MBB); |
| 1096 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 1097 | for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), |
| 1098 | SuE = MBB->succ_end(); SuI != SuE; ++SuI) { |
| 1099 | if (*SuI == MBB) |
| 1100 | continue; |
| 1101 | BBInfo &SInfo = MBBInfoMap[*SuI]; |
| 1102 | if (SInfo.addPassed(MInfo.vregsPassed)) |
| 1103 | todo.insert(*SuI); |
| 1104 | } |
| 1105 | } |
| 1106 | } |
| 1107 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1108 | // Calculate the set of virtual registers that must be passed through each basic |
| 1109 | // 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] | 1110 | // similar to calcRegsPassed, only backwards. |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1111 | void MachineVerifier::calcRegsRequired() { |
| 1112 | // First push live-in regs to predecessors' vregsRequired. |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1113 | SmallPtrSet<const MachineBasicBlock*, 8> todo; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1114 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1115 | MFI != MFE; ++MFI) { |
| 1116 | const MachineBasicBlock &MBB(*MFI); |
| 1117 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 1118 | for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(), |
| 1119 | PrE = MBB.pred_end(); PrI != PrE; ++PrI) { |
| 1120 | BBInfo &PInfo = MBBInfoMap[*PrI]; |
| 1121 | if (PInfo.addRequired(MInfo.vregsLiveIn)) |
| 1122 | todo.insert(*PrI); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | // Iteratively push vregsRequired to predecessors. This will converge to the |
| 1127 | // same final state regardless of DenseSet iteration order. |
| 1128 | while (!todo.empty()) { |
| 1129 | const MachineBasicBlock *MBB = *todo.begin(); |
| 1130 | todo.erase(MBB); |
| 1131 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 1132 | for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), |
| 1133 | PrE = MBB->pred_end(); PrI != PrE; ++PrI) { |
| 1134 | if (*PrI == MBB) |
| 1135 | continue; |
| 1136 | BBInfo &SInfo = MBBInfoMap[*PrI]; |
| 1137 | if (SInfo.addRequired(MInfo.vregsRequired)) |
| 1138 | todo.insert(*PrI); |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1143 | // 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] | 1144 | // calcRegsPassed has been run so BBInfo::isLiveOut is valid. |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1145 | void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1146 | SmallPtrSet<const MachineBasicBlock*, 8> seen; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1147 | for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1148 | BBI != BBE && BBI->isPHI(); ++BBI) { |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1149 | seen.clear(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1150 | |
| 1151 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) { |
| 1152 | unsigned Reg = BBI->getOperand(i).getReg(); |
| 1153 | const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB(); |
| 1154 | if (!Pre->isSuccessor(MBB)) |
| 1155 | continue; |
| 1156 | seen.insert(Pre); |
| 1157 | BBInfo &PrInfo = MBBInfoMap[Pre]; |
| 1158 | if (PrInfo.reachable && !PrInfo.isLiveOut(Reg)) |
| 1159 | report("PHI operand is not live-out from predecessor", |
| 1160 | &BBI->getOperand(i), i); |
| 1161 | } |
| 1162 | |
| 1163 | // Did we see all predecessors? |
| 1164 | for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), |
| 1165 | PrE = MBB->pred_end(); PrI != PrE; ++PrI) { |
| 1166 | if (!seen.count(*PrI)) { |
| 1167 | report("Missing PHI operand", BBI); |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1168 | *OS << "BB#" << (*PrI)->getNumber() |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1169 | << " is a predecessor according to the CFG.\n"; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1175 | void MachineVerifier::visitMachineFunctionAfter() { |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1176 | calcRegsPassed(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1177 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1178 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1179 | MFI != MFE; ++MFI) { |
| 1180 | BBInfo &MInfo = MBBInfoMap[MFI]; |
| 1181 | |
| 1182 | // Skip unreachable MBBs. |
| 1183 | if (!MInfo.reachable) |
| 1184 | continue; |
| 1185 | |
| 1186 | checkPHIOps(MFI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1187 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1188 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1189 | // Now check liveness info if available |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1190 | calcRegsRequired(); |
| 1191 | |
Jakob Stoklund Olesen | bb07216 | 2012-06-29 21:00:00 +0000 | [diff] [blame] | 1192 | // Check for killed virtual registers that should be live out. |
| 1193 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1194 | MFI != MFE; ++MFI) { |
| 1195 | BBInfo &MInfo = MBBInfoMap[MFI]; |
| 1196 | for (RegSet::iterator |
| 1197 | I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; |
| 1198 | ++I) |
| 1199 | if (MInfo.regsKilled.count(*I)) { |
Bill Wendling | 96cb112 | 2012-07-19 00:04:14 +0000 | [diff] [blame] | 1200 | report("Virtual register killed in block, but needed live out.", MFI); |
| 1201 | *OS << "Virtual register " << PrintReg(*I) |
Jakob Stoklund Olesen | bb07216 | 2012-06-29 21:00:00 +0000 | [diff] [blame] | 1202 | << " is used after the block.\n"; |
| 1203 | } |
| 1204 | } |
| 1205 | |
Jakob Stoklund Olesen | a4e6397 | 2012-06-25 18:18:27 +0000 | [diff] [blame] | 1206 | if (!MF->empty()) { |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1207 | BBInfo &MInfo = MBBInfoMap[&MF->front()]; |
| 1208 | for (RegSet::iterator |
| 1209 | I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; |
Jakob Stoklund Olesen | ff0275e | 2012-03-10 00:44:11 +0000 | [diff] [blame] | 1210 | ++I) |
| 1211 | report("Virtual register def doesn't dominate all uses.", |
| 1212 | MRI->getVRegDef(*I)); |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1215 | if (LiveVars) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1216 | verifyLiveVariables(); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1217 | if (LiveInts) |
| 1218 | verifyLiveIntervals(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1219 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1220 | |
| 1221 | void MachineVerifier::verifyLiveVariables() { |
| 1222 | assert(LiveVars && "Don't call verifyLiveVariables without LiveVars"); |
Jakob Stoklund Olesen | 98c5476 | 2011-01-08 23:11:02 +0000 | [diff] [blame] | 1223 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 1224 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1225 | LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); |
| 1226 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1227 | MFI != MFE; ++MFI) { |
| 1228 | BBInfo &MInfo = MBBInfoMap[MFI]; |
| 1229 | |
| 1230 | // Our vregsRequired should be identical to LiveVariables' AliveBlocks |
| 1231 | if (MInfo.vregsRequired.count(Reg)) { |
| 1232 | if (!VI.AliveBlocks.test(MFI->getNumber())) { |
| 1233 | report("LiveVariables: Block missing from AliveBlocks", MFI); |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 1234 | *OS << "Virtual register " << PrintReg(Reg) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1235 | << " must be live through the block.\n"; |
| 1236 | } |
| 1237 | } else { |
| 1238 | if (VI.AliveBlocks.test(MFI->getNumber())) { |
| 1239 | report("LiveVariables: Block should not be in AliveBlocks", MFI); |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 1240 | *OS << "Virtual register " << PrintReg(Reg) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1241 | << " is not needed live through the block.\n"; |
| 1242 | } |
| 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1248 | void MachineVerifier::verifyLiveIntervals() { |
| 1249 | assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts"); |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1250 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 1251 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 893ab5d | 2010-10-06 23:54:35 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Spilling and splitting may leave unused registers around. Skip them. |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1254 | if (MRI->reg_nodbg_empty(Reg)) |
Jakob Stoklund Olesen | 893ab5d | 2010-10-06 23:54:35 +0000 | [diff] [blame] | 1255 | continue; |
| 1256 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1257 | if (!LiveInts->hasInterval(Reg)) { |
| 1258 | report("Missing live interval for virtual register", MF); |
| 1259 | *OS << PrintReg(Reg, TRI) << " still has defs or uses\n"; |
Jakob Stoklund Olesen | 8c45642 | 2010-10-28 20:44:22 +0000 | [diff] [blame] | 1260 | continue; |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1261 | } |
Jakob Stoklund Olesen | 8c45642 | 2010-10-28 20:44:22 +0000 | [diff] [blame] | 1262 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1263 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 1264 | assert(Reg == LI.reg && "Invalid reg to interval mapping"); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1265 | verifyLiveInterval(LI); |
| 1266 | } |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 1267 | |
| 1268 | // Verify all the cached regunit intervals. |
| 1269 | for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) |
| 1270 | if (const LiveInterval *LI = LiveInts->getCachedRegUnit(i)) |
| 1271 | verifyLiveInterval(*LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1272 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1273 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1274 | void MachineVerifier::verifyLiveIntervalValue(const LiveInterval &LI, |
| 1275 | VNInfo *VNI) { |
| 1276 | if (VNI->isUnused()) |
| 1277 | return; |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1278 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1279 | const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1280 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1281 | if (!DefVNI) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1282 | report("Valno not live at def and not marked unused", MF, LI); |
| 1283 | *OS << "Valno #" << VNI->id << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1284 | return; |
| 1285 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1286 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1287 | if (DefVNI != VNI) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1288 | report("Live range at def has different valno", MF, LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1289 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1290 | << " where valno #" << DefVNI->id << " is live\n"; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1291 | return; |
| 1292 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1293 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1294 | const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def); |
| 1295 | if (!MBB) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1296 | report("Invalid definition index", MF, LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1297 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
| 1298 | << " in " << LI << '\n'; |
| 1299 | return; |
| 1300 | } |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1301 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1302 | if (VNI->isPHIDef()) { |
| 1303 | if (VNI->def != LiveInts->getMBBStartIdx(MBB)) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1304 | report("PHIDef value is not defined at MBB start", MBB, LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1305 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1306 | << ", not at the beginning of BB#" << MBB->getNumber() << '\n'; |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1307 | } |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1308 | return; |
| 1309 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1310 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1311 | // Non-PHI def. |
| 1312 | const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); |
| 1313 | if (!MI) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1314 | report("No instruction at def index", MBB, LI); |
| 1315 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1316 | return; |
| 1317 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1318 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1319 | bool hasDef = false; |
| 1320 | bool isEarlyClobber = false; |
| 1321 | for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { |
| 1322 | if (!MOI->isReg() || !MOI->isDef()) |
| 1323 | continue; |
Jakob Stoklund Olesen | 8c593f9 | 2010-10-27 00:39:01 +0000 | [diff] [blame] | 1324 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1325 | if (MOI->getReg() != LI.reg) |
| 1326 | continue; |
| 1327 | } else { |
| 1328 | if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) || |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 1329 | !TRI->hasRegUnit(MOI->getReg(), LI.reg)) |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1330 | continue; |
| 1331 | } |
| 1332 | hasDef = true; |
| 1333 | if (MOI->isEarlyClobber()) |
| 1334 | isEarlyClobber = true; |
| 1335 | } |
| 1336 | |
| 1337 | if (!hasDef) { |
| 1338 | report("Defining instruction does not modify register", MI); |
| 1339 | *OS << "Valno #" << VNI->id << " in " << LI << '\n'; |
| 1340 | } |
| 1341 | |
| 1342 | // Early clobber defs begin at USE slots, but other defs must begin at |
| 1343 | // DEF slots. |
| 1344 | if (isEarlyClobber) { |
| 1345 | if (!VNI->def.isEarlyClobber()) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1346 | report("Early clobber def must be at an early-clobber slot", MBB, LI); |
| 1347 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } else if (!VNI->def.isRegister()) { |
| 1350 | report("Non-PHI, non-early clobber def must be at a register slot", |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1351 | MBB, LI); |
| 1352 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | void |
| 1357 | MachineVerifier::verifyLiveIntervalSegment(const LiveInterval &LI, |
| 1358 | LiveInterval::const_iterator I) { |
| 1359 | const VNInfo *VNI = I->valno; |
| 1360 | assert(VNI && "Live range has no valno"); |
| 1361 | |
| 1362 | if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1363 | report("Foreign valno in live range", MF, LI); |
| 1364 | *OS << *I << " has a bad valno\n"; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | if (VNI->isUnused()) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1368 | report("Live range valno is marked unused", MF, LI); |
| 1369 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start); |
| 1373 | if (!MBB) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1374 | report("Bad start of live segment, no basic block", MF, LI); |
| 1375 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1376 | return; |
| 1377 | } |
| 1378 | SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB); |
| 1379 | if (I->start != MBBStartIdx && I->start != VNI->def) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1380 | report("Live segment must begin at MBB entry or valno def", MBB, LI); |
| 1381 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | const MachineBasicBlock *EndMBB = |
| 1385 | LiveInts->getMBBFromIndex(I->end.getPrevSlot()); |
| 1386 | if (!EndMBB) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1387 | report("Bad end of live segment, no basic block", MF, LI); |
| 1388 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | } |
| 1391 | |
| 1392 | // No more checks for live-out segments. |
| 1393 | if (I->end == LiveInts->getMBBEndIdx(EndMBB)) |
| 1394 | return; |
| 1395 | |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 1396 | // RegUnit intervals are allowed dead phis. |
| 1397 | if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && VNI->isPHIDef() && |
| 1398 | I->start == VNI->def && I->end == VNI->def.getDeadSlot()) |
| 1399 | return; |
| 1400 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1401 | // The live segment is ending inside EndMBB |
| 1402 | const MachineInstr *MI = |
| 1403 | LiveInts->getInstructionFromIndex(I->end.getPrevSlot()); |
| 1404 | if (!MI) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1405 | report("Live segment doesn't end at a valid instruction", EndMBB, LI); |
| 1406 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1407 | return; |
| 1408 | } |
| 1409 | |
| 1410 | // The block slot must refer to a basic block boundary. |
| 1411 | if (I->end.isBlock()) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1412 | report("Live segment ends at B slot of an instruction", EndMBB, LI); |
| 1413 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | if (I->end.isDead()) { |
| 1417 | // Segment ends on the dead slot. |
| 1418 | // That means there must be a dead def. |
| 1419 | if (!SlotIndex::isSameInstr(I->start, I->end)) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1420 | report("Live segment ending at dead slot spans instructions", EndMBB, LI); |
| 1421 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | // A live segment can only end at an early-clobber slot if it is being |
| 1426 | // redefined by an early-clobber def. |
| 1427 | if (I->end.isEarlyClobber()) { |
| 1428 | if (I+1 == LI.end() || (I+1)->start != I->end) { |
| 1429 | report("Live segment ending at early clobber slot must be " |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1430 | "redefined by an EC def in the same instruction", EndMBB, LI); |
| 1431 | *OS << *I << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | // The following checks only apply to virtual registers. Physreg liveness |
| 1436 | // is too weird to check. |
| 1437 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
| 1438 | // A live range can end with either a redefinition, a kill flag on a |
| 1439 | // use, or a dead flag on a def. |
| 1440 | bool hasRead = false; |
| 1441 | bool hasDeadDef = false; |
| 1442 | for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { |
| 1443 | if (!MOI->isReg() || MOI->getReg() != LI.reg) |
| 1444 | continue; |
| 1445 | if (MOI->readsReg()) |
| 1446 | hasRead = true; |
| 1447 | if (MOI->isDef() && MOI->isDead()) |
| 1448 | hasDeadDef = true; |
| 1449 | } |
| 1450 | |
| 1451 | if (I->end.isDead()) { |
| 1452 | if (!hasDeadDef) { |
| 1453 | report("Instruction doesn't have a dead def operand", MI); |
| 1454 | I->print(*OS); |
| 1455 | *OS << " in " << LI << '\n'; |
| 1456 | } |
| 1457 | } else { |
| 1458 | if (!hasRead) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1459 | report("Instruction ending live range doesn't read the register", MI); |
| 1460 | *OS << *I << " in " << LI << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | // Now check all the basic blocks in this live segment. |
| 1466 | MachineFunction::const_iterator MFI = MBB; |
| 1467 | // Is this live range the beginning of a non-PHIDef VN? |
| 1468 | if (I->start == VNI->def && !VNI->isPHIDef()) { |
| 1469 | // Not live-in to any blocks. |
| 1470 | if (MBB == EndMBB) |
| 1471 | return; |
| 1472 | // Skip this block. |
| 1473 | ++MFI; |
| 1474 | } |
| 1475 | for (;;) { |
| 1476 | assert(LiveInts->isLiveInToMBB(LI, MFI)); |
| 1477 | // We don't know how to track physregs into a landing pad. |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 1478 | if (!TargetRegisterInfo::isVirtualRegister(LI.reg) && |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1479 | MFI->isLandingPad()) { |
| 1480 | if (&*MFI == EndMBB) |
| 1481 | break; |
| 1482 | ++MFI; |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
| 1486 | // Is VNI a PHI-def in the current block? |
| 1487 | bool IsPHI = VNI->isPHIDef() && |
| 1488 | VNI->def == LiveInts->getMBBStartIdx(MFI); |
| 1489 | |
| 1490 | // Check that VNI is live-out of all predecessors. |
| 1491 | for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(), |
| 1492 | PE = MFI->pred_end(); PI != PE; ++PI) { |
| 1493 | SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI); |
| 1494 | const VNInfo *PVNI = LI.getVNInfoBefore(PEnd); |
| 1495 | |
| 1496 | // All predecessors must have a live-out value. |
| 1497 | if (!PVNI) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1498 | report("Register not marked live out of predecessor", *PI, LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1499 | *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber() |
| 1500 | << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before " |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1501 | << PEnd << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1502 | continue; |
| 1503 | } |
| 1504 | |
| 1505 | // Only PHI-defs can take different predecessor values. |
| 1506 | if (!IsPHI && PVNI != VNI) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1507 | report("Different value live out of predecessor", *PI, LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1508 | *OS << "Valno #" << PVNI->id << " live out of BB#" |
| 1509 | << (*PI)->getNumber() << '@' << PEnd |
| 1510 | << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber() |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1511 | << '@' << LiveInts->getMBBStartIdx(MFI) << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | if (&*MFI == EndMBB) |
| 1515 | break; |
| 1516 | ++MFI; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) { |
| 1521 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end(); |
| 1522 | I!=E; ++I) |
| 1523 | verifyLiveIntervalValue(LI, *I); |
| 1524 | |
| 1525 | for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) |
| 1526 | verifyLiveIntervalSegment(LI, I); |
| 1527 | |
| 1528 | // Check the LI only has one connected component. |
| 1529 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
| 1530 | ConnectedVNInfoEqClasses ConEQ(*LiveInts); |
| 1531 | unsigned NumComp = ConEQ.Classify(&LI); |
| 1532 | if (NumComp > 1) { |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 1533 | report("Multiple connected components in live interval", MF, LI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1534 | for (unsigned comp = 0; comp != NumComp; ++comp) { |
| 1535 | *OS << comp << ": valnos"; |
| 1536 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), |
| 1537 | E = LI.vni_end(); I!=E; ++I) |
| 1538 | if (comp == ConEQ.getEqClass(*I)) |
| 1539 | *OS << ' ' << (*I)->id; |
| 1540 | *OS << '\n'; |
Jakob Stoklund Olesen | 8c593f9 | 2010-10-27 00:39:01 +0000 | [diff] [blame] | 1541 | } |
Jakob Stoklund Olesen | 501dc42 | 2010-10-26 22:36:07 +0000 | [diff] [blame] | 1542 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1543 | } |
| 1544 | } |