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