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