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