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