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