Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1 | //===- MachineVerifier.cpp - Machine Code Verifier ------------------------===// |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Pass to verify generated machine code. The following is checked: |
| 11 | // |
| 12 | // Operand counts: All explicit operands must be present. |
| 13 | // |
| 14 | // Register classes: All physical and virtual register operands must be |
| 15 | // compatible with the register class required by the instruction descriptor. |
| 16 | // |
| 17 | // Register live intervals: Registers must be defined only once, and must be |
| 18 | // defined before use. |
| 19 | // |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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. |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | |
Krzysztof Parzyszek | 5ede58d | 2018-08-16 19:13:28 +0000 | [diff] [blame] | 26 | #include "LiveRangeCalc.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/BitVector.h" |
| 28 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/DenseSet.h" |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/DepthFirstIterator.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/SetOperations.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringRef.h" |
| 36 | #include "llvm/ADT/Twine.h" |
David Majnemer | 1114aa2 | 2015-12-02 23:06:39 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/EHPersonalities.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/GlobalISel/RegisterBank.h" |
| 39 | #include "llvm/CodeGen/LiveInterval.h" |
Matthias Braun | fa621d2 | 2017-12-13 02:51:04 +0000 | [diff] [blame] | 40 | #include "llvm/CodeGen/LiveIntervals.h" |
Matthias Braun | 209f048 | 2017-12-18 23:19:44 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/LiveStacks.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/LiveVariables.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 43 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 44 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 45 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 46 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 47 | #include "llvm/CodeGen/MachineInstr.h" |
| 48 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 49 | #include "llvm/CodeGen/MachineMemOperand.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 50 | #include "llvm/CodeGen/MachineOperand.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 51 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 52 | #include "llvm/CodeGen/PseudoSourceValue.h" |
| 53 | #include "llvm/CodeGen/SlotIndexes.h" |
Philip Reames | bfffaf7 | 2017-06-02 16:36:37 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/StackMaps.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 56 | #include "llvm/CodeGen/TargetOpcodes.h" |
| 57 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 58 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 59 | #include "llvm/IR/BasicBlock.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 60 | #include "llvm/IR/Function.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 61 | #include "llvm/IR/InlineAsm.h" |
| 62 | #include "llvm/IR/Instructions.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 63 | #include "llvm/MC/LaneBitmask.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 64 | #include "llvm/MC/MCAsmInfo.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 65 | #include "llvm/MC/MCInstrDesc.h" |
| 66 | #include "llvm/MC/MCRegisterInfo.h" |
| 67 | #include "llvm/MC/MCTargetOptions.h" |
| 68 | #include "llvm/Pass.h" |
| 69 | #include "llvm/Support/Casting.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 70 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 71 | #include "llvm/Support/LowLevelTypeImpl.h" |
| 72 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 73 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 74 | #include "llvm/Target/TargetMachine.h" |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 75 | #include <algorithm> |
| 76 | #include <cassert> |
| 77 | #include <cstddef> |
| 78 | #include <cstdint> |
| 79 | #include <iterator> |
| 80 | #include <string> |
| 81 | #include <utility> |
| 82 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 83 | using namespace llvm; |
| 84 | |
| 85 | namespace { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 86 | |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 87 | struct MachineVerifier { |
| 88 | MachineVerifier(Pass *pass, const char *b) : PASS(pass), Banner(b) {} |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 89 | |
Matthias Braun | 2168cf4 | 2016-02-15 19:25:31 +0000 | [diff] [blame] | 90 | unsigned verify(MachineFunction &MF); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 91 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 92 | Pass *const PASS; |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 93 | const char *Banner; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 94 | const MachineFunction *MF; |
| 95 | const TargetMachine *TM; |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 96 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 97 | const TargetRegisterInfo *TRI; |
| 98 | const MachineRegisterInfo *MRI; |
| 99 | |
| 100 | unsigned foundErrors; |
| 101 | |
Ahmed Bougacha | 1f82c34 | 2016-08-02 16:17:15 +0000 | [diff] [blame] | 102 | // Avoid querying the MachineFunctionProperties for each operand. |
| 103 | bool isFunctionRegBankSelected; |
Ahmed Bougacha | 35426be | 2016-08-02 16:49:22 +0000 | [diff] [blame] | 104 | bool isFunctionSelected; |
Ahmed Bougacha | 1f82c34 | 2016-08-02 16:17:15 +0000 | [diff] [blame] | 105 | |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 106 | using RegVector = SmallVector<unsigned, 16>; |
| 107 | using RegMaskVector = SmallVector<const uint32_t *, 4>; |
| 108 | using RegSet = DenseSet<unsigned>; |
| 109 | using RegMap = DenseMap<unsigned, const MachineInstr *>; |
| 110 | using BlockSet = SmallPtrSet<const MachineBasicBlock *, 8>; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 111 | |
Daniel Sanders | b95f965 | 2018-10-03 22:05:31 +0000 | [diff] [blame] | 112 | const MachineInstr *FirstNonPHI; |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 113 | const MachineInstr *FirstTerminator; |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 114 | BlockSet FunctionBlocks; |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 116 | BitVector regsReserved; |
| 117 | RegSet regsLive; |
Jakob Stoklund Olesen | 710b13b | 2009-08-08 13:19:25 +0000 | [diff] [blame] | 118 | RegVector regsDefined, regsDead, regsKilled; |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 119 | RegMaskVector regMasks; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 120 | |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 121 | SlotIndex lastIndex; |
| 122 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 123 | // Add Reg and any sub-registers to RV |
| 124 | void addRegWithSubRegs(RegVector &RV, unsigned Reg) { |
| 125 | RV.push_back(Reg); |
| 126 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 127 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 128 | RV.push_back(*SubRegs); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 131 | struct BBInfo { |
| 132 | // Is this MBB reachable from the MF entry point? |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 133 | bool reachable = false; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 134 | |
| 135 | // Vregs that must be live in because they are used without being |
| 136 | // defined. Map value is the user. |
| 137 | RegMap vregsLiveIn; |
| 138 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 139 | // Regs killed in MBB. They may be defined again, and will then be in both |
| 140 | // regsKilled and regsLiveOut. |
| 141 | RegSet regsKilled; |
| 142 | |
| 143 | // Regs defined in MBB and live out. Note that vregs passing through may |
| 144 | // be live out without being mentioned here. |
| 145 | RegSet regsLiveOut; |
| 146 | |
| 147 | // Vregs that pass through MBB untouched. This set is disjoint from |
| 148 | // regsKilled and regsLiveOut. |
| 149 | RegSet vregsPassed; |
| 150 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 151 | // Vregs that must pass through MBB because they are needed by a successor |
| 152 | // block. This set is disjoint from regsLiveOut. |
| 153 | RegSet vregsRequired; |
| 154 | |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 155 | // Set versions of block's predecessor and successor lists. |
| 156 | BlockSet Preds, Succs; |
| 157 | |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 158 | BBInfo() = default; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 159 | |
| 160 | // Add register to vregsPassed if it belongs there. Return true if |
| 161 | // anything changed. |
| 162 | bool addPassed(unsigned Reg) { |
| 163 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 164 | return false; |
| 165 | if (regsKilled.count(Reg) || regsLiveOut.count(Reg)) |
| 166 | return false; |
| 167 | return vregsPassed.insert(Reg).second; |
| 168 | } |
| 169 | |
| 170 | // Same for a full set. |
| 171 | bool addPassed(const RegSet &RS) { |
| 172 | bool changed = false; |
| 173 | for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) |
| 174 | if (addPassed(*I)) |
| 175 | changed = true; |
| 176 | return changed; |
| 177 | } |
| 178 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 179 | // Add register to vregsRequired if it belongs there. Return true if |
| 180 | // anything changed. |
| 181 | bool addRequired(unsigned Reg) { |
| 182 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 183 | return false; |
| 184 | if (regsLiveOut.count(Reg)) |
| 185 | return false; |
| 186 | return vregsRequired.insert(Reg).second; |
| 187 | } |
| 188 | |
| 189 | // Same for a full set. |
| 190 | bool addRequired(const RegSet &RS) { |
| 191 | bool changed = false; |
| 192 | for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) |
| 193 | if (addRequired(*I)) |
| 194 | changed = true; |
| 195 | return changed; |
| 196 | } |
| 197 | |
| 198 | // Same for a full map. |
| 199 | bool addRequired(const RegMap &RM) { |
| 200 | bool changed = false; |
| 201 | for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I) |
| 202 | if (addRequired(I->first)) |
| 203 | changed = true; |
| 204 | return changed; |
| 205 | } |
| 206 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 207 | // Live-out registers are either in regsLiveOut or vregsPassed. |
| 208 | bool isLiveOut(unsigned Reg) const { |
| 209 | return regsLiveOut.count(Reg) || vregsPassed.count(Reg); |
| 210 | } |
| 211 | }; |
| 212 | |
| 213 | // Extra register info per MBB. |
| 214 | DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap; |
| 215 | |
| 216 | bool isReserved(unsigned Reg) { |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 217 | return Reg < regsReserved.size() && regsReserved.test(Reg); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Matthias Braun | 97beda0 | 2017-05-05 22:04:05 +0000 | [diff] [blame] | 220 | bool isAllocatable(unsigned Reg) const { |
| 221 | return Reg < TRI->getNumRegs() && TRI->isInAllocatableClass(Reg) && |
| 222 | !regsReserved.test(Reg); |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 225 | // Analysis information if available |
| 226 | LiveVariables *LiveVars; |
Jakob Stoklund Olesen | 501dc42 | 2010-10-26 22:36:07 +0000 | [diff] [blame] | 227 | LiveIntervals *LiveInts; |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 228 | LiveStacks *LiveStks; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 229 | SlotIndexes *Indexes; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 230 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 231 | void visitMachineFunctionBefore(); |
| 232 | void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 233 | void visitMachineBundleBefore(const MachineInstr *MI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 234 | void visitMachineInstrBefore(const MachineInstr *MI); |
| 235 | void visitMachineOperand(const MachineOperand *MO, unsigned MONum); |
| 236 | void visitMachineInstrAfter(const MachineInstr *MI); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 237 | void visitMachineBundleAfter(const MachineInstr *MI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 238 | void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB); |
| 239 | void visitMachineFunctionAfter(); |
| 240 | |
| 241 | void report(const char *msg, const MachineFunction *MF); |
| 242 | void report(const char *msg, const MachineBasicBlock *MBB); |
| 243 | void report(const char *msg, const MachineInstr *MI); |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 244 | void report(const char *msg, const MachineOperand *MO, unsigned MONum, |
| 245 | LLT MOVRegType = LLT{}); |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 246 | |
| 247 | void report_context(const LiveInterval &LI) const; |
Matt Arsenault | 065de94 | 2016-07-25 19:39:01 +0000 | [diff] [blame] | 248 | void report_context(const LiveRange &LR, unsigned VRegUnit, |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 249 | LaneBitmask LaneMask) const; |
| 250 | void report_context(const LiveRange::Segment &S) const; |
| 251 | void report_context(const VNInfo &VNI) const; |
Matthias Braun | 6aca1f7 | 2016-02-02 02:44:25 +0000 | [diff] [blame] | 252 | void report_context(SlotIndex Pos) const; |
Florian Hahn | 55534df | 2019-01-08 15:16:23 +0000 | [diff] [blame] | 253 | void report_context(MCPhysReg PhysReg) const; |
Matthias Braun | 6aca1f7 | 2016-02-02 02:44:25 +0000 | [diff] [blame] | 254 | void report_context_liverange(const LiveRange &LR) const; |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 255 | void report_context_lanemask(LaneBitmask LaneMask) const; |
Matthias Braun | 1e25675 | 2016-05-11 21:31:39 +0000 | [diff] [blame] | 256 | void report_context_vreg(unsigned VReg) const; |
Fangrui Song | 7d88286 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 257 | void report_context_vreg_regunit(unsigned VRegOrUnit) const; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 258 | |
Jakob Stoklund Olesen | 90a4f78 | 2012-08-29 18:11:05 +0000 | [diff] [blame] | 259 | void verifyInlineAsm(const MachineInstr *MI); |
Jakob Stoklund Olesen | 90a4f78 | 2012-08-29 18:11:05 +0000 | [diff] [blame] | 260 | |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 261 | void checkLiveness(const MachineOperand *MO, unsigned MONum); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 262 | void checkLivenessAtUse(const MachineOperand *MO, unsigned MONum, |
Fangrui Song | 7d88286 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 263 | SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit, |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 264 | LaneBitmask LaneMask = LaneBitmask::getNone()); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 265 | void checkLivenessAtDef(const MachineOperand *MO, unsigned MONum, |
Fangrui Song | 7d88286 | 2018-07-16 18:51:40 +0000 | [diff] [blame] | 266 | SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit, |
Bjorn Pettersson | 32a3289 | 2018-09-20 06:59:18 +0000 | [diff] [blame] | 267 | bool SubRangeCheck = false, |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 268 | LaneBitmask LaneMask = LaneBitmask::getNone()); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 269 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 270 | void markReachable(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 271 | void calcRegsPassed(); |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 272 | void checkPHIOps(const MachineBasicBlock &MBB); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 273 | |
| 274 | void calcRegsRequired(); |
| 275 | void verifyLiveVariables(); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 276 | void verifyLiveIntervals(); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 277 | void verifyLiveInterval(const LiveInterval&); |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 278 | void verifyLiveRangeValue(const LiveRange&, const VNInfo*, unsigned, |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 279 | LaneBitmask); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 280 | void verifyLiveRangeSegment(const LiveRange&, |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 281 | const LiveRange::const_iterator I, unsigned, |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 282 | LaneBitmask); |
| 283 | void verifyLiveRange(const LiveRange&, unsigned, |
| 284 | LaneBitmask LaneMask = LaneBitmask::getNone()); |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 285 | |
| 286 | void verifyStackFrame(); |
Matthias Braun | ccc5028 | 2015-09-09 17:49:46 +0000 | [diff] [blame] | 287 | |
| 288 | void verifySlotIndexes() const; |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 289 | void verifyProperties(const MachineFunction &MF); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 290 | }; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 291 | |
| 292 | struct MachineVerifierPass : public MachineFunctionPass { |
| 293 | static char ID; // Pass ID, replacement for typeid |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 294 | |
Matthias Braun | 9173c77 | 2014-12-11 19:41:51 +0000 | [diff] [blame] | 295 | const std::string Banner; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 296 | |
Sven van Haastregt | cc54be8 | 2017-03-29 15:25:06 +0000 | [diff] [blame] | 297 | MachineVerifierPass(std::string banner = std::string()) |
Sven van Haastregt | 64532cd | 2017-03-29 09:08:25 +0000 | [diff] [blame] | 298 | : MachineFunctionPass(ID), Banner(std::move(banner)) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 299 | initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry()); |
| 300 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 301 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 302 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 303 | AU.setPreservesAll(); |
| 304 | MachineFunctionPass::getAnalysisUsage(AU); |
| 305 | } |
| 306 | |
Craig Topper | 9f998de | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 307 | bool runOnMachineFunction(MachineFunction &MF) override { |
Matthias Braun | 2168cf4 | 2016-02-15 19:25:31 +0000 | [diff] [blame] | 308 | unsigned FoundErrors = MachineVerifier(this, Banner.c_str()).verify(MF); |
| 309 | if (FoundErrors) |
| 310 | report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors."); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | }; |
| 314 | |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 315 | } // end anonymous namespace |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 316 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 317 | char MachineVerifierPass::ID = 0; |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 318 | |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 319 | INITIALIZE_PASS(MachineVerifierPass, "machineverifier", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 320 | "Verify generated machine code", false, false) |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 321 | |
Matthias Braun | 9173c77 | 2014-12-11 19:41:51 +0000 | [diff] [blame] | 322 | FunctionPass *llvm::createMachineVerifierPass(const std::string &Banner) { |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 323 | return new MachineVerifierPass(Banner); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Matthias Braun | 2168cf4 | 2016-02-15 19:25:31 +0000 | [diff] [blame] | 326 | bool MachineFunction::verify(Pass *p, const char *Banner, bool AbortOnErrors) |
| 327 | const { |
| 328 | MachineFunction &MF = const_cast<MachineFunction&>(*this); |
| 329 | unsigned FoundErrors = MachineVerifier(p, Banner).verify(MF); |
| 330 | if (AbortOnErrors && FoundErrors) |
| 331 | report_fatal_error("Found "+Twine(FoundErrors)+" machine code errors."); |
| 332 | return FoundErrors == 0; |
Jakob Stoklund Olesen | ce727d0 | 2009-11-13 21:56:09 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Matthias Braun | ccc5028 | 2015-09-09 17:49:46 +0000 | [diff] [blame] | 335 | void MachineVerifier::verifySlotIndexes() const { |
| 336 | if (Indexes == nullptr) |
| 337 | return; |
| 338 | |
| 339 | // Ensure the IdxMBB list is sorted by slot indexes. |
| 340 | SlotIndex Last; |
| 341 | for (SlotIndexes::MBBIndexIterator I = Indexes->MBBIndexBegin(), |
| 342 | E = Indexes->MBBIndexEnd(); I != E; ++I) { |
| 343 | assert(!Last.isValid() || I->first > Last); |
| 344 | Last = I->first; |
| 345 | } |
| 346 | } |
| 347 | |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 348 | void MachineVerifier::verifyProperties(const MachineFunction &MF) { |
| 349 | // If a pass has introduced virtual registers without clearing the |
Matthias Braun | 690a3cb | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 350 | // NoVRegs property (or set it without allocating the vregs) |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 351 | // then report an error. |
| 352 | if (MF.getProperties().hasProperty( |
Matthias Braun | 690a3cb | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 353 | MachineFunctionProperties::Property::NoVRegs) && |
| 354 | MRI->getNumVirtRegs()) |
| 355 | report("Function has NoVRegs property but there are VReg operands", &MF); |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Matthias Braun | 2168cf4 | 2016-02-15 19:25:31 +0000 | [diff] [blame] | 358 | unsigned MachineVerifier::verify(MachineFunction &MF) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 359 | foundErrors = 0; |
| 360 | |
| 361 | this->MF = &MF; |
| 362 | TM = &MF.getTarget(); |
Eric Christopher | d879de1 | 2014-10-14 07:00:33 +0000 | [diff] [blame] | 363 | TII = MF.getSubtarget().getInstrInfo(); |
| 364 | TRI = MF.getSubtarget().getRegisterInfo(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 365 | MRI = &MF.getRegInfo(); |
| 366 | |
Roman Tereshin | 8e63a83 | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 367 | const bool isFunctionFailedISel = MF.getProperties().hasProperty( |
| 368 | MachineFunctionProperties::Property::FailedISel); |
Daniel Sanders | 8f3aebf | 2018-10-02 17:56:58 +0000 | [diff] [blame] | 369 | |
| 370 | // If we're mid-GlobalISel and we already triggered the fallback path then |
| 371 | // it's expected that the MIR is somewhat broken but that's ok since we'll |
| 372 | // reset it and clear the FailedISel attribute in ResetMachineFunctions. |
| 373 | if (isFunctionFailedISel) |
| 374 | return foundErrors; |
| 375 | |
Roman Tereshin | 8e63a83 | 2018-02-28 17:55:45 +0000 | [diff] [blame] | 376 | isFunctionRegBankSelected = |
| 377 | !isFunctionFailedISel && |
| 378 | MF.getProperties().hasProperty( |
| 379 | MachineFunctionProperties::Property::RegBankSelected); |
| 380 | isFunctionSelected = !isFunctionFailedISel && |
| 381 | MF.getProperties().hasProperty( |
| 382 | MachineFunctionProperties::Property::Selected); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 383 | LiveVars = nullptr; |
| 384 | LiveInts = nullptr; |
| 385 | LiveStks = nullptr; |
| 386 | Indexes = nullptr; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 387 | if (PASS) { |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 388 | LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>(); |
Jakob Stoklund Olesen | c910c8d | 2010-08-05 23:51:26 +0000 | [diff] [blame] | 389 | // We don't want to verify LiveVariables if LiveIntervals is available. |
| 390 | if (!LiveInts) |
| 391 | LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>(); |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 392 | LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>(); |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 393 | Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>(); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Matthias Braun | ccc5028 | 2015-09-09 17:49:46 +0000 | [diff] [blame] | 396 | verifySlotIndexes(); |
| 397 | |
Derek Schuff | b65f550 | 2016-03-29 17:40:22 +0000 | [diff] [blame] | 398 | verifyProperties(MF); |
| 399 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 400 | visitMachineFunctionBefore(); |
| 401 | for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end(); |
| 402 | MFI!=MFE; ++MFI) { |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 403 | visitMachineBasicBlockBefore(&*MFI); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 404 | // Keep track of the current bundle header. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 405 | const MachineInstr *CurBundle = nullptr; |
Jakob Stoklund Olesen | 9466bde | 2012-12-18 22:55:07 +0000 | [diff] [blame] | 406 | // Do we expect the next instruction to be part of the same bundle? |
| 407 | bool InBundle = false; |
| 408 | |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 409 | for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(), |
| 410 | MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) { |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 411 | if (MBBI->getParent() != &*MFI) { |
Duncan P. N. Exon Smith | 31a5b87 | 2016-09-03 01:22:56 +0000 | [diff] [blame] | 412 | report("Bad instruction parent pointer", &*MFI); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 413 | errs() << "Instruction: " << *MBBI; |
Jakob Stoklund Olesen | 7bd46da | 2011-01-12 21:27:41 +0000 | [diff] [blame] | 414 | continue; |
| 415 | } |
Jakob Stoklund Olesen | 9466bde | 2012-12-18 22:55:07 +0000 | [diff] [blame] | 416 | |
| 417 | // Check for consistent bundle flags. |
| 418 | if (InBundle && !MBBI->isBundledWithPred()) |
| 419 | report("Missing BundledPred flag, " |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 420 | "BundledSucc was set on predecessor", |
| 421 | &*MBBI); |
Jakob Stoklund Olesen | 9466bde | 2012-12-18 22:55:07 +0000 | [diff] [blame] | 422 | if (!InBundle && MBBI->isBundledWithPred()) |
| 423 | report("BundledPred flag is set, " |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 424 | "but BundledSucc not set on predecessor", |
| 425 | &*MBBI); |
Jakob Stoklund Olesen | 9466bde | 2012-12-18 22:55:07 +0000 | [diff] [blame] | 426 | |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 427 | // Is this a bundle header? |
| 428 | if (!MBBI->isInsideBundle()) { |
| 429 | if (CurBundle) |
| 430 | visitMachineBundleAfter(CurBundle); |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 431 | CurBundle = &*MBBI; |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 432 | visitMachineBundleBefore(CurBundle); |
| 433 | } else if (!CurBundle) |
Duncan P. N. Exon Smith | 31a5b87 | 2016-09-03 01:22:56 +0000 | [diff] [blame] | 434 | report("No bundle header", &*MBBI); |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 435 | visitMachineInstrBefore(&*MBBI); |
Matt Arsenault | 816fd00 | 2015-04-30 19:35:41 +0000 | [diff] [blame] | 436 | for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) { |
| 437 | const MachineInstr &MI = *MBBI; |
| 438 | const MachineOperand &Op = MI.getOperand(I); |
| 439 | if (Op.getParent() != &MI) { |
Matt Arsenault | 04a0292 | 2015-04-30 23:20:56 +0000 | [diff] [blame] | 440 | // Make sure to use correct addOperand / RemoveOperand / ChangeTo |
Matt Arsenault | 816fd00 | 2015-04-30 19:35:41 +0000 | [diff] [blame] | 441 | // functions when replacing operands of a MachineInstr. |
| 442 | report("Instruction has operand with wrong parent set", &MI); |
| 443 | } |
| 444 | |
| 445 | visitMachineOperand(&Op, I); |
| 446 | } |
| 447 | |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 448 | visitMachineInstrAfter(&*MBBI); |
Jakob Stoklund Olesen | 9466bde | 2012-12-18 22:55:07 +0000 | [diff] [blame] | 449 | |
| 450 | // Was this the last bundled instruction? |
| 451 | InBundle = MBBI->isBundledWithSucc(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 452 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 453 | if (CurBundle) |
| 454 | visitMachineBundleAfter(CurBundle); |
Jakob Stoklund Olesen | 9466bde | 2012-12-18 22:55:07 +0000 | [diff] [blame] | 455 | if (InBundle) |
| 456 | report("BundledSucc flag set on last instruction in block", &MFI->back()); |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 457 | visitMachineBasicBlockAfter(&*MFI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 458 | } |
| 459 | visitMachineFunctionAfter(); |
| 460 | |
Jakob Stoklund Olesen | 6349668 | 2009-08-08 15:34:50 +0000 | [diff] [blame] | 461 | // Clean up. |
| 462 | regsLive.clear(); |
| 463 | regsDefined.clear(); |
| 464 | regsDead.clear(); |
| 465 | regsKilled.clear(); |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 466 | regMasks.clear(); |
Jakob Stoklund Olesen | 6349668 | 2009-08-08 15:34:50 +0000 | [diff] [blame] | 467 | MBBInfoMap.clear(); |
| 468 | |
Matthias Braun | 2168cf4 | 2016-02-15 19:25:31 +0000 | [diff] [blame] | 469 | return foundErrors; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Chris Lattner | 372fefe | 2009-08-23 01:03:30 +0000 | [diff] [blame] | 472 | void MachineVerifier::report(const char *msg, const MachineFunction *MF) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 473 | assert(MF); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 474 | errs() << '\n'; |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 475 | if (!foundErrors++) { |
| 476 | if (Banner) |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 477 | errs() << "# " << Banner << '\n'; |
Matthias Braun | 806e686 | 2015-11-09 23:59:23 +0000 | [diff] [blame] | 478 | if (LiveInts != nullptr) |
| 479 | LiveInts->print(errs()); |
| 480 | else |
| 481 | MF->print(errs(), Indexes); |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 482 | } |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 483 | errs() << "*** Bad machine code: " << msg << " ***\n" |
Craig Topper | 96601ca | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 484 | << "- function: " << MF->getName() << "\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 487 | void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 488 | assert(MBB); |
| 489 | report(msg, MBB->getParent()); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 490 | errs() << "- basic block: " << printMBBReference(*MBB) << ' ' |
| 491 | << MBB->getName() << " (" << (const void *)MBB << ')'; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 492 | if (Indexes) |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 493 | errs() << " [" << Indexes->getMBBStartIdx(MBB) |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 494 | << ';' << Indexes->getMBBEndIdx(MBB) << ')'; |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 495 | errs() << '\n'; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 498 | void MachineVerifier::report(const char *msg, const MachineInstr *MI) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 499 | assert(MI); |
| 500 | report(msg, MI->getParent()); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 501 | errs() << "- instruction: "; |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 502 | if (Indexes && Indexes->hasIndex(*MI)) |
| 503 | errs() << Indexes->getInstructionIndex(*MI) << '\t'; |
Matthias Braun | e5351a1 | 2015-11-09 23:59:25 +0000 | [diff] [blame] | 504 | MI->print(errs(), /*SkipOpers=*/true); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 507 | void MachineVerifier::report(const char *msg, const MachineOperand *MO, |
| 508 | unsigned MONum, LLT MOVRegType) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 509 | assert(MO); |
| 510 | report(msg, MO->getParent()); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 511 | errs() << "- operand " << MONum << ": "; |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 512 | MO->print(errs(), MOVRegType, TRI); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 513 | errs() << "\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Matthias Braun | 6aca1f7 | 2016-02-02 02:44:25 +0000 | [diff] [blame] | 516 | void MachineVerifier::report_context(SlotIndex Pos) const { |
| 517 | errs() << "- at: " << Pos << '\n'; |
| 518 | } |
| 519 | |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 520 | void MachineVerifier::report_context(const LiveInterval &LI) const { |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 521 | errs() << "- interval: " << LI << '\n'; |
Jakob Stoklund Olesen | 79240f95 | 2012-08-02 14:31:49 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Matt Arsenault | 065de94 | 2016-07-25 19:39:01 +0000 | [diff] [blame] | 524 | void MachineVerifier::report_context(const LiveRange &LR, unsigned VRegUnit, |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 525 | LaneBitmask LaneMask) const { |
Matthias Braun | 6aca1f7 | 2016-02-02 02:44:25 +0000 | [diff] [blame] | 526 | report_context_liverange(LR); |
Matt Arsenault | 065de94 | 2016-07-25 19:39:01 +0000 | [diff] [blame] | 527 | report_context_vreg_regunit(VRegUnit); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 528 | if (LaneMask.any()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 529 | report_context_lanemask(LaneMask); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 532 | void MachineVerifier::report_context(const LiveRange::Segment &S) const { |
| 533 | errs() << "- segment: " << S << '\n'; |
| 534 | } |
| 535 | |
| 536 | void MachineVerifier::report_context(const VNInfo &VNI) const { |
| 537 | errs() << "- ValNo: " << VNI.id << " (def " << VNI.def << ")\n"; |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Matthias Braun | 6aca1f7 | 2016-02-02 02:44:25 +0000 | [diff] [blame] | 540 | void MachineVerifier::report_context_liverange(const LiveRange &LR) const { |
| 541 | errs() << "- liverange: " << LR << '\n'; |
| 542 | } |
| 543 | |
Florian Hahn | 55534df | 2019-01-08 15:16:23 +0000 | [diff] [blame] | 544 | void MachineVerifier::report_context(MCPhysReg PReg) const { |
| 545 | errs() << "- p. register: " << printReg(PReg, TRI) << '\n'; |
| 546 | } |
| 547 | |
Matthias Braun | 1e25675 | 2016-05-11 21:31:39 +0000 | [diff] [blame] | 548 | void MachineVerifier::report_context_vreg(unsigned VReg) const { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 549 | errs() << "- v. register: " << printReg(VReg, TRI) << '\n'; |
Matthias Braun | 1e25675 | 2016-05-11 21:31:39 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 552 | void MachineVerifier::report_context_vreg_regunit(unsigned VRegOrUnit) const { |
| 553 | if (TargetRegisterInfo::isVirtualRegister(VRegOrUnit)) { |
Matthias Braun | 1e25675 | 2016-05-11 21:31:39 +0000 | [diff] [blame] | 554 | report_context_vreg(VRegOrUnit); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 555 | } else { |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 556 | errs() << "- regunit: " << printRegUnit(VRegOrUnit, TRI) << '\n'; |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | void MachineVerifier::report_context_lanemask(LaneBitmask LaneMask) const { |
| 561 | errs() << "- lanemask: " << PrintLaneMask(LaneMask) << '\n'; |
| 562 | } |
| 563 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 564 | void MachineVerifier::markReachable(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 565 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 566 | if (!MInfo.reachable) { |
| 567 | MInfo.reachable = true; |
| 568 | for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), |
| 569 | SuE = MBB->succ_end(); SuI != SuE; ++SuI) |
| 570 | markReachable(*SuI); |
| 571 | } |
| 572 | } |
| 573 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 574 | void MachineVerifier::visitMachineFunctionBefore() { |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 575 | lastIndex = SlotIndex(); |
Matthias Braun | 97beda0 | 2017-05-05 22:04:05 +0000 | [diff] [blame] | 576 | regsReserved = MRI->reservedRegsFrozen() ? MRI->getReservedRegs() |
| 577 | : TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 578 | |
Justin Bogner | b86967b | 2017-04-11 19:32:41 +0000 | [diff] [blame] | 579 | if (!MF->empty()) |
| 580 | markReachable(&MF->front()); |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 581 | |
| 582 | // Build a set of the basic blocks in the function. |
| 583 | FunctionBlocks.clear(); |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 584 | for (const auto &MBB : *MF) { |
| 585 | FunctionBlocks.insert(&MBB); |
| 586 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 587 | |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 588 | MInfo.Preds.insert(MBB.pred_begin(), MBB.pred_end()); |
| 589 | if (MInfo.Preds.size() != MBB.pred_size()) |
| 590 | report("MBB has duplicate entries in its predecessor list.", &MBB); |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 591 | |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 592 | MInfo.Succs.insert(MBB.succ_begin(), MBB.succ_end()); |
| 593 | if (MInfo.Succs.size() != MBB.succ_size()) |
| 594 | report("MBB has duplicate entries in its successor list.", &MBB); |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 595 | } |
Jakob Stoklund Olesen | a58d67a | 2013-04-19 21:40:57 +0000 | [diff] [blame] | 596 | |
| 597 | // Check that the register use lists are sane. |
| 598 | MRI->verifyUseLists(); |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 599 | |
Justin Bogner | b86967b | 2017-04-11 19:32:41 +0000 | [diff] [blame] | 600 | if (!MF->empty()) |
| 601 | verifyStackFrame(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 604 | // Does iterator point to a and b as the first two elements? |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 605 | static bool matchPair(MachineBasicBlock::const_succ_iterator i, |
| 606 | const MachineBasicBlock *a, const MachineBasicBlock *b) { |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 607 | if (*i == a) |
| 608 | return *++i == b; |
| 609 | if (*i == b) |
| 610 | return *++i == a; |
| 611 | return false; |
| 612 | } |
| 613 | |
| 614 | void |
| 615 | MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) { |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 616 | FirstTerminator = nullptr; |
Daniel Sanders | b95f965 | 2018-10-03 22:05:31 +0000 | [diff] [blame] | 617 | FirstNonPHI = nullptr; |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 618 | |
Matthias Braun | 6648973 | 2016-08-24 01:32:41 +0000 | [diff] [blame] | 619 | if (!MF->getProperties().hasProperty( |
Matthias Braun | 4700463 | 2017-01-05 20:01:19 +0000 | [diff] [blame] | 620 | MachineFunctionProperties::Property::NoPHIs) && MRI->tracksLiveness()) { |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 621 | // If this block has allocatable physical registers live-in, check that |
| 622 | // it is an entry block or landing pad. |
Matthias Braun | af5ff60 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 623 | for (const auto &LI : MBB->liveins()) { |
| 624 | if (isAllocatable(LI.PhysReg) && !MBB->isEHPad() && |
Duncan P. N. Exon Smith | 8de6150 | 2016-02-21 20:39:50 +0000 | [diff] [blame] | 625 | MBB->getIterator() != MBB->getParent()->begin()) { |
Matt Arsenault | ad35e22 | 2017-02-15 22:19:06 +0000 | [diff] [blame] | 626 | report("MBB has allocatable live-in, but isn't entry or landing-pad.", MBB); |
Florian Hahn | 55534df | 2019-01-08 15:16:23 +0000 | [diff] [blame] | 627 | report_context(LI.PhysReg); |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 632 | // Count the number of landing pad successors. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 633 | SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs; |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 634 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 635 | E = MBB->succ_end(); I != E; ++I) { |
Reid Kleckner | c0e64ad | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 636 | if ((*I)->isEHPad()) |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 637 | LandingPadSuccs.insert(*I); |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 638 | if (!FunctionBlocks.count(*I)) |
| 639 | report("MBB has successor that isn't part of the function.", MBB); |
| 640 | if (!MBBInfoMap[*I].Preds.count(MBB)) { |
| 641 | report("Inconsistent CFG", MBB); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 642 | errs() << "MBB is not in the predecessor list of the successor " |
| 643 | << printMBBReference(*(*I)) << ".\n"; |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
| 647 | // Check the predecessor list. |
| 648 | for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(), |
| 649 | E = MBB->pred_end(); I != E; ++I) { |
| 650 | if (!FunctionBlocks.count(*I)) |
| 651 | report("MBB has predecessor that isn't part of the function.", MBB); |
| 652 | if (!MBBInfoMap[*I].Succs.count(MBB)) { |
| 653 | report("Inconsistent CFG", MBB); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 654 | errs() << "MBB is not in the successor list of the predecessor " |
| 655 | << printMBBReference(*(*I)) << ".\n"; |
Jakob Stoklund Olesen | b254c6d | 2012-08-20 20:52:06 +0000 | [diff] [blame] | 656 | } |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 657 | } |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 658 | |
| 659 | const MCAsmInfo *AsmInfo = TM->getMCAsmInfo(); |
| 660 | const BasicBlock *BB = MBB->getBasicBlock(); |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 661 | const Function &F = MF->getFunction(); |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 662 | if (LandingPadSuccs.size() > 1 && |
| 663 | !(AsmInfo && |
| 664 | AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj && |
Reid Kleckner | c324f8d | 2015-11-09 21:04:00 +0000 | [diff] [blame] | 665 | BB && isa<SwitchInst>(BB->getTerminator())) && |
Heejin Ahn | 5b752cf | 2018-05-17 20:52:03 +0000 | [diff] [blame] | 666 | !isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn()))) |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 667 | report("MBB has more than one landing pad successor", MBB); |
| 668 | |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 669 | // Call AnalyzeBranch. If it succeeds, there several more conditions to check. |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 670 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 671 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 48ed4ab | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 672 | if (!TII->analyzeBranch(*const_cast<MachineBasicBlock *>(MBB), TBB, FBB, |
| 673 | Cond)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 674 | // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's |
| 675 | // check whether its answers match up with reality. |
| 676 | if (!TBB && !FBB) { |
| 677 | // Block falls through to its successor. |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 678 | MachineFunction::const_iterator MBBI = MBB->getIterator(); |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 679 | ++MBBI; |
| 680 | if (MBBI == MF->end()) { |
Dan Gohman | a01a80fa | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 681 | // It's possible that the block legitimately ends with a noreturn |
| 682 | // call or an unreachable, in which case it won't actually fall |
| 683 | // out the bottom of the function. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 684 | } else if (MBB->succ_size() == LandingPadSuccs.size()) { |
Dan Gohman | a01a80fa | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 685 | // It's possible that the block legitimately ends with a noreturn |
Hiroshi Inoue | 7a9527e | 2019-01-09 05:11:10 +0000 | [diff] [blame] | 686 | // call or an unreachable, in which case it won't actually fall |
Dan Gohman | a01a80fa | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 687 | // out of the block. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 688 | } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 689 | report("MBB exits via unconditional fall-through but doesn't have " |
| 690 | "exactly one CFG successor!", MBB); |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 691 | } else if (!MBB->isSuccessor(&*MBBI)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 692 | report("MBB exits via unconditional fall-through but its successor " |
| 693 | "differs from its CFG successor!", MBB); |
| 694 | } |
Benjamin Kramer | c734d38 | 2014-05-24 13:31:10 +0000 | [diff] [blame] | 695 | if (!MBB->empty() && MBB->back().isBarrier() && |
Duncan P. N. Exon Smith | 5b9b80e | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 696 | !TII->isPredicated(MBB->back())) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 697 | report("MBB exits via unconditional fall-through but ends with a " |
| 698 | "barrier instruction!", MBB); |
| 699 | } |
| 700 | if (!Cond.empty()) { |
| 701 | report("MBB exits via unconditional fall-through but has a condition!", |
| 702 | MBB); |
| 703 | } |
| 704 | } else if (TBB && !FBB && Cond.empty()) { |
| 705 | // Block unconditionally branches somewhere. |
Ahmed Bougacha | eb1f2e8 | 2014-12-01 18:43:53 +0000 | [diff] [blame] | 706 | // If the block has exactly one successor, that happens to be a |
| 707 | // landingpad, accept it as valid control flow. |
| 708 | if (MBB->succ_size() != 1+LandingPadSuccs.size() && |
| 709 | (MBB->succ_size() != 1 || LandingPadSuccs.size() != 1 || |
| 710 | *MBB->succ_begin() != *LandingPadSuccs.begin())) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 711 | report("MBB exits via unconditional branch but doesn't have " |
| 712 | "exactly one CFG successor!", MBB); |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 713 | } else if (!MBB->isSuccessor(TBB)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 714 | report("MBB exits via unconditional branch but the CFG " |
| 715 | "successor doesn't match the actual successor!", MBB); |
| 716 | } |
| 717 | if (MBB->empty()) { |
| 718 | report("MBB exits via unconditional branch but doesn't contain " |
| 719 | "any instructions!", MBB); |
Benjamin Kramer | c734d38 | 2014-05-24 13:31:10 +0000 | [diff] [blame] | 720 | } else if (!MBB->back().isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 721 | report("MBB exits via unconditional branch but doesn't end with a " |
| 722 | "barrier instruction!", MBB); |
Benjamin Kramer | c734d38 | 2014-05-24 13:31:10 +0000 | [diff] [blame] | 723 | } else if (!MBB->back().isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 724 | report("MBB exits via unconditional branch but the branch isn't a " |
| 725 | "terminator instruction!", MBB); |
| 726 | } |
| 727 | } else if (TBB && !FBB && !Cond.empty()) { |
| 728 | // Block conditionally branches somewhere, otherwise falls through. |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 729 | MachineFunction::const_iterator MBBI = MBB->getIterator(); |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 730 | ++MBBI; |
| 731 | if (MBBI == MF->end()) { |
| 732 | report("MBB conditionally falls through out of function!", MBB); |
Dmitri Gribenko | 344df79 | 2012-12-19 22:13:01 +0000 | [diff] [blame] | 733 | } else if (MBB->succ_size() == 1) { |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 734 | // A conditional branch with only one successor is weird, but allowed. |
| 735 | if (&*MBBI != TBB) |
| 736 | report("MBB exits via conditional branch/fall-through but only has " |
| 737 | "one CFG successor!", MBB); |
| 738 | else if (TBB != *MBB->succ_begin()) |
| 739 | report("MBB exits via conditional branch/fall-through but the CFG " |
| 740 | "successor don't match the actual successor!", MBB); |
| 741 | } else if (MBB->succ_size() != 2) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 742 | report("MBB exits via conditional branch/fall-through but doesn't have " |
| 743 | "exactly two CFG successors!", MBB); |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 744 | } else if (!matchPair(MBB->succ_begin(), TBB, &*MBBI)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 745 | report("MBB exits via conditional branch/fall-through but the CFG " |
| 746 | "successors don't match the actual successors!", MBB); |
| 747 | } |
| 748 | if (MBB->empty()) { |
| 749 | report("MBB exits via conditional branch/fall-through but doesn't " |
| 750 | "contain any instructions!", MBB); |
Benjamin Kramer | c734d38 | 2014-05-24 13:31:10 +0000 | [diff] [blame] | 751 | } else if (MBB->back().isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 752 | report("MBB exits via conditional branch/fall-through but ends with a " |
| 753 | "barrier instruction!", MBB); |
Benjamin Kramer | c734d38 | 2014-05-24 13:31:10 +0000 | [diff] [blame] | 754 | } else if (!MBB->back().isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 755 | report("MBB exits via conditional branch/fall-through but the branch " |
| 756 | "isn't a terminator instruction!", MBB); |
| 757 | } |
| 758 | } else if (TBB && FBB) { |
| 759 | // Block conditionally branches somewhere, otherwise branches |
| 760 | // somewhere else. |
Jakob Stoklund Olesen | e7fdef4 | 2012-08-20 21:39:52 +0000 | [diff] [blame] | 761 | if (MBB->succ_size() == 1) { |
| 762 | // A conditional branch with only one successor is weird, but allowed. |
| 763 | if (FBB != TBB) |
| 764 | report("MBB exits via conditional branch/branch through but only has " |
| 765 | "one CFG successor!", MBB); |
| 766 | else if (TBB != *MBB->succ_begin()) |
| 767 | report("MBB exits via conditional branch/branch through but the CFG " |
| 768 | "successor don't match the actual successor!", MBB); |
| 769 | } else if (MBB->succ_size() != 2) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 770 | report("MBB exits via conditional branch/branch but doesn't have " |
| 771 | "exactly two CFG successors!", MBB); |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 772 | } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 773 | report("MBB exits via conditional branch/branch but the CFG " |
| 774 | "successors don't match the actual successors!", MBB); |
| 775 | } |
| 776 | if (MBB->empty()) { |
| 777 | report("MBB exits via conditional branch/branch but doesn't " |
| 778 | "contain any instructions!", MBB); |
Benjamin Kramer | 9d934ac | 2014-05-24 13:13:17 +0000 | [diff] [blame] | 779 | } else if (!MBB->back().isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 780 | report("MBB exits via conditional branch/branch but doesn't end with a " |
| 781 | "barrier instruction!", MBB); |
Benjamin Kramer | 9d934ac | 2014-05-24 13:13:17 +0000 | [diff] [blame] | 782 | } else if (!MBB->back().isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 783 | report("MBB exits via conditional branch/branch but the branch " |
| 784 | "isn't a terminator instruction!", MBB); |
| 785 | } |
| 786 | if (Cond.empty()) { |
Matt Arsenault | f596114 | 2018-10-23 21:23:52 +0000 | [diff] [blame] | 787 | report("MBB exits via conditional branch/branch but there's no " |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 788 | "condition!", MBB); |
| 789 | } |
| 790 | } else { |
| 791 | report("AnalyzeBranch returned invalid data!", MBB); |
| 792 | } |
| 793 | } |
| 794 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 795 | regsLive.clear(); |
Matthias Braun | 4700463 | 2017-01-05 20:01:19 +0000 | [diff] [blame] | 796 | if (MRI->tracksLiveness()) { |
| 797 | for (const auto &LI : MBB->liveins()) { |
| 798 | if (!TargetRegisterInfo::isPhysicalRegister(LI.PhysReg)) { |
| 799 | report("MBB live-in list contains non-physical register", MBB); |
| 800 | continue; |
| 801 | } |
| 802 | for (MCSubRegIterator SubRegs(LI.PhysReg, TRI, /*IncludeSelf=*/true); |
| 803 | SubRegs.isValid(); ++SubRegs) |
| 804 | regsLive.insert(*SubRegs); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 805 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 806 | } |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 807 | |
Matthias Braun | f79c57a | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 808 | const MachineFrameInfo &MFI = MF->getFrameInfo(); |
| 809 | BitVector PR = MFI.getPristineRegs(*MF); |
Francis Visoiu Mistrih | 1179b5e | 2017-05-17 01:07:53 +0000 | [diff] [blame] | 810 | for (unsigned I : PR.set_bits()) { |
Chad Rosier | 62c320a | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 811 | for (MCSubRegIterator SubRegs(I, TRI, /*IncludeSelf=*/true); |
| 812 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 813 | regsLive.insert(*SubRegs); |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 816 | regsKilled.clear(); |
| 817 | regsDefined.clear(); |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 818 | |
| 819 | if (Indexes) |
| 820 | lastIndex = Indexes->getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 823 | // This function gets called for all bundle headers, including normal |
| 824 | // stand-alone unbundled instructions. |
| 825 | void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) { |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 826 | if (Indexes && Indexes->hasIndex(*MI)) { |
| 827 | SlotIndex idx = Indexes->getInstructionIndex(*MI); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 828 | if (!(idx > lastIndex)) { |
| 829 | report("Instruction index out of order", MI); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 830 | errs() << "Last instruction was at " << lastIndex << '\n'; |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 831 | } |
| 832 | lastIndex = idx; |
| 833 | } |
Pete Cooper | 83569cb | 2012-06-07 17:41:39 +0000 | [diff] [blame] | 834 | |
| 835 | // Ensure non-terminators don't follow terminators. |
| 836 | // Ignore predicated terminators formed by if conversion. |
| 837 | // FIXME: If conversion shouldn't need to violate this rule. |
Duncan P. N. Exon Smith | 5b9b80e | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 838 | if (MI->isTerminator() && !TII->isPredicated(*MI)) { |
Pete Cooper | 83569cb | 2012-06-07 17:41:39 +0000 | [diff] [blame] | 839 | if (!FirstTerminator) |
| 840 | FirstTerminator = MI; |
| 841 | } else if (FirstTerminator) { |
| 842 | report("Non-terminator instruction after the first terminator", MI); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 843 | errs() << "First terminator was:\t" << *FirstTerminator; |
Pete Cooper | 83569cb | 2012-06-07 17:41:39 +0000 | [diff] [blame] | 844 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Jakob Stoklund Olesen | 90a4f78 | 2012-08-29 18:11:05 +0000 | [diff] [blame] | 847 | // The operands on an INLINEASM instruction must follow a template. |
| 848 | // Verify that the flag operands make sense. |
| 849 | void MachineVerifier::verifyInlineAsm(const MachineInstr *MI) { |
| 850 | // The first two operands on INLINEASM are the asm string and global flags. |
| 851 | if (MI->getNumOperands() < 2) { |
| 852 | report("Too few operands on inline asm", MI); |
| 853 | return; |
| 854 | } |
| 855 | if (!MI->getOperand(0).isSymbol()) |
| 856 | report("Asm string must be an external symbol", MI); |
| 857 | if (!MI->getOperand(1).isImm()) |
| 858 | report("Asm flags must be an immediate", MI); |
Chad Rosier | 3d71688 | 2012-10-30 19:11:54 +0000 | [diff] [blame] | 859 | // Allowed flags are Extra_HasSideEffects = 1, Extra_IsAlignStack = 2, |
Wei Ding | ef86963 | 2016-06-22 18:51:08 +0000 | [diff] [blame] | 860 | // Extra_AsmDialect = 4, Extra_MayLoad = 8, and Extra_MayStore = 16, |
| 861 | // and Extra_IsConvergent = 32. |
| 862 | if (!isUInt<6>(MI->getOperand(1).getImm())) |
Jakob Stoklund Olesen | 90a4f78 | 2012-08-29 18:11:05 +0000 | [diff] [blame] | 863 | report("Unknown asm flags", &MI->getOperand(1), 1); |
| 864 | |
Gabor Horvath | 1fc0a8d | 2015-03-16 09:53:42 +0000 | [diff] [blame] | 865 | static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed"); |
Jakob Stoklund Olesen | 90a4f78 | 2012-08-29 18:11:05 +0000 | [diff] [blame] | 866 | |
| 867 | unsigned OpNo = InlineAsm::MIOp_FirstOperand; |
| 868 | unsigned NumOps; |
| 869 | for (unsigned e = MI->getNumOperands(); OpNo < e; OpNo += NumOps) { |
| 870 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 871 | // There may be implicit ops after the fixed operands. |
| 872 | if (!MO.isImm()) |
| 873 | break; |
| 874 | NumOps = 1 + InlineAsm::getNumOperandRegisters(MO.getImm()); |
| 875 | } |
| 876 | |
| 877 | if (OpNo > MI->getNumOperands()) |
| 878 | report("Missing operands in last group", MI); |
| 879 | |
| 880 | // An optional MDNode follows the groups. |
| 881 | if (OpNo < MI->getNumOperands() && MI->getOperand(OpNo).isMetadata()) |
| 882 | ++OpNo; |
| 883 | |
| 884 | // All trailing operands must be implicit registers. |
| 885 | for (unsigned e = MI->getNumOperands(); OpNo < e; ++OpNo) { |
| 886 | const MachineOperand &MO = MI->getOperand(OpNo); |
| 887 | if (!MO.isReg() || !MO.isImplicit()) |
| 888 | report("Expected implicit register after groups", &MO, OpNo); |
| 889 | } |
| 890 | } |
| 891 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 892 | void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 893 | const MCInstrDesc &MCID = MI->getDesc(); |
| 894 | if (MI->getNumOperands() < MCID.getNumOperands()) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 895 | report("Too few operands", MI); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 896 | errs() << MCID.getNumOperands() << " operands expected, but " |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 897 | << MI->getNumOperands() << " given.\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 898 | } |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 899 | |
Daniel Sanders | b95f965 | 2018-10-03 22:05:31 +0000 | [diff] [blame] | 900 | if (MI->isPHI()) { |
| 901 | if (MF->getProperties().hasProperty( |
| 902 | MachineFunctionProperties::Property::NoPHIs)) |
| 903 | report("Found PHI instruction with NoPHIs property set", MI); |
| 904 | |
| 905 | if (FirstNonPHI) |
| 906 | report("Found PHI instruction after non-PHI", MI); |
| 907 | } else if (FirstNonPHI == nullptr) |
| 908 | FirstNonPHI = MI; |
Matthias Braun | db9ce2f | 2016-08-23 21:19:49 +0000 | [diff] [blame] | 909 | |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame] | 910 | // Check the tied operands. |
Jakob Stoklund Olesen | 90a4f78 | 2012-08-29 18:11:05 +0000 | [diff] [blame] | 911 | if (MI->isInlineAsm()) |
| 912 | verifyInlineAsm(MI); |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame] | 913 | |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 914 | // Check the MachineMemOperands for basic consistency. |
| 915 | for (MachineInstr::mmo_iterator I = MI->memoperands_begin(), |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 916 | E = MI->memoperands_end(); |
| 917 | I != E; ++I) { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 918 | if ((*I)->isLoad() && !MI->mayLoad()) |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 919 | report("Missing mayLoad flag", MI); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 920 | if ((*I)->isStore() && !MI->mayStore()) |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 921 | report("Missing mayStore flag", MI); |
| 922 | } |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 923 | |
| 924 | // Debug values must not have a slot index. |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 925 | // Other instructions must have one, unless they are inside a bundle. |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 926 | if (LiveInts) { |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 927 | bool mapped = !LiveInts->isNotInMIMap(*MI); |
Shiva Chen | 24abe71 | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 928 | if (MI->isDebugInstr()) { |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 929 | if (mapped) |
| 930 | report("Debug instruction has a slot index", MI); |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 931 | } else if (MI->isInsideBundle()) { |
| 932 | if (mapped) |
| 933 | report("Instruction inside bundle has a slot index", MI); |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 934 | } else { |
| 935 | if (!mapped) |
| 936 | report("Missing slot index", MI); |
| 937 | } |
| 938 | } |
| 939 | |
Ahmed Bougacha | e27b94c | 2016-07-28 16:58:27 +0000 | [diff] [blame] | 940 | if (isPreISelGenericOpcode(MCID.getOpcode())) { |
Ahmed Bougacha | 35426be | 2016-08-02 16:49:22 +0000 | [diff] [blame] | 941 | if (isFunctionSelected) |
| 942 | report("Unexpected generic instruction in a Selected function", MI); |
| 943 | |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 944 | // Check types. |
Tim Northover | 59282d3 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 945 | SmallVector<LLT, 4> Types; |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 946 | for (unsigned I = 0; I < MCID.getNumOperands(); ++I) { |
| 947 | if (!MCID.OpInfo[I].isGenericType()) |
Tim Northover | 59282d3 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 948 | continue; |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 949 | // Generic instructions specify type equality constraints between some of |
| 950 | // their operands. Make sure these are consistent. |
| 951 | size_t TypeIdx = MCID.OpInfo[I].getGenericTypeIndex(); |
Tim Northover | 59282d3 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 952 | Types.resize(std::max(TypeIdx + 1, Types.size())); |
| 953 | |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 954 | const MachineOperand *MO = &MI->getOperand(I); |
| 955 | LLT OpTy = MRI->getType(MO->getReg()); |
Roman Tereshin | 297777e | 2018-05-07 22:31:47 +0000 | [diff] [blame] | 956 | // Don't report a type mismatch if there is no actual mismatch, only a |
| 957 | // type missing, to reduce noise: |
| 958 | if (OpTy.isValid()) { |
| 959 | // Only the first valid type for a type index will be printed: don't |
| 960 | // overwrite it later so it's always clear which type was expected: |
| 961 | if (!Types[TypeIdx].isValid()) |
| 962 | Types[TypeIdx] = OpTy; |
| 963 | else if (Types[TypeIdx] != OpTy) |
| 964 | report("Type mismatch in generic instruction", MO, I, OpTy); |
| 965 | } else { |
| 966 | // Generic instructions must have types attached to their operands. |
| 967 | report("Generic instruction is missing a virtual register type", MO, I); |
| 968 | } |
Tim Northover | 59282d3 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 969 | } |
Ahmed Bougacha | e27b94c | 2016-07-28 16:58:27 +0000 | [diff] [blame] | 970 | |
Roman Tereshin | 481686f | 2018-05-07 22:31:12 +0000 | [diff] [blame] | 971 | // Generic opcodes must not have physical register operands. |
| 972 | for (unsigned I = 0; I < MI->getNumOperands(); ++I) { |
| 973 | const MachineOperand *MO = &MI->getOperand(I); |
| 974 | if (MO->isReg() && TargetRegisterInfo::isPhysicalRegister(MO->getReg())) |
Roman Tereshin | 297777e | 2018-05-07 22:31:47 +0000 | [diff] [blame] | 975 | report("Generic instruction cannot have physical register", MO, I); |
Tim Northover | 8be91b5 | 2016-08-30 18:52:46 +0000 | [diff] [blame] | 976 | } |
| 977 | } |
| 978 | |
Andrew Trick | 3be654f | 2011-09-21 02:20:46 +0000 | [diff] [blame] | 979 | StringRef ErrorInfo; |
Duncan P. N. Exon Smith | 567409d | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 980 | if (!TII->verifyInstruction(*MI, ErrorInfo)) |
Andrew Trick | 3be654f | 2011-09-21 02:20:46 +0000 | [diff] [blame] | 981 | report(ErrorInfo.data(), MI); |
Philip Reames | bfffaf7 | 2017-06-02 16:36:37 +0000 | [diff] [blame] | 982 | |
| 983 | // Verify properties of various specific instruction types |
| 984 | switch(MI->getOpcode()) { |
| 985 | default: |
| 986 | break; |
| 987 | case TargetOpcode::G_LOAD: |
| 988 | case TargetOpcode::G_STORE: |
| 989 | // Generic loads and stores must have a single MachineMemOperand |
| 990 | // describing that access. |
| 991 | if (!MI->hasOneMemOperand()) |
| 992 | report("Generic instruction accessing memory must have one mem operand", |
| 993 | MI); |
| 994 | break; |
Aditya Nandakumar | 5f06407 | 2017-08-23 20:45:48 +0000 | [diff] [blame] | 995 | case TargetOpcode::G_PHI: { |
| 996 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 997 | if (!DstTy.isValid() || |
| 998 | !std::all_of(MI->operands_begin() + 1, MI->operands_end(), |
| 999 | [this, &DstTy](const MachineOperand &MO) { |
| 1000 | if (!MO.isReg()) |
| 1001 | return true; |
| 1002 | LLT Ty = MRI->getType(MO.getReg()); |
| 1003 | if (!Ty.isValid() || (Ty != DstTy)) |
| 1004 | return false; |
| 1005 | return true; |
| 1006 | })) |
| 1007 | report("Generic Instruction G_PHI has operands with incompatible/missing " |
| 1008 | "types", |
| 1009 | MI); |
| 1010 | break; |
| 1011 | } |
Roman Tereshin | 5074502 | 2018-05-08 02:48:15 +0000 | [diff] [blame] | 1012 | case TargetOpcode::G_SEXT: |
| 1013 | case TargetOpcode::G_ZEXT: |
| 1014 | case TargetOpcode::G_ANYEXT: |
| 1015 | case TargetOpcode::G_TRUNC: |
| 1016 | case TargetOpcode::G_FPEXT: |
| 1017 | case TargetOpcode::G_FPTRUNC: { |
| 1018 | // Number of operands and presense of types is already checked (and |
| 1019 | // reported in case of any issues), so no need to report them again. As |
| 1020 | // we're trying to report as many issues as possible at once, however, the |
| 1021 | // instructions aren't guaranteed to have the right number of operands or |
| 1022 | // types attached to them at this point |
| 1023 | assert(MCID.getNumOperands() == 2 && "Expected 2 operands G_*{EXT,TRUNC}"); |
| 1024 | if (MI->getNumOperands() < MCID.getNumOperands()) |
| 1025 | break; |
| 1026 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 1027 | LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); |
| 1028 | if (!DstTy.isValid() || !SrcTy.isValid()) |
| 1029 | break; |
| 1030 | |
| 1031 | LLT DstElTy = DstTy.isVector() ? DstTy.getElementType() : DstTy; |
| 1032 | LLT SrcElTy = SrcTy.isVector() ? SrcTy.getElementType() : SrcTy; |
| 1033 | if (DstElTy.isPointer() || SrcElTy.isPointer()) |
| 1034 | report("Generic extend/truncate can not operate on pointers", MI); |
| 1035 | |
| 1036 | if (DstTy.isVector() != SrcTy.isVector()) { |
| 1037 | report("Generic extend/truncate must be all-vector or all-scalar", MI); |
| 1038 | // Generally we try to report as many issues as possible at once, but in |
| 1039 | // this case it's not clear what should we be comparing the size of the |
| 1040 | // scalar with: the size of the whole vector or its lane. Instead of |
| 1041 | // making an arbitrary choice and emitting not so helpful message, let's |
| 1042 | // avoid the extra noise and stop here. |
| 1043 | break; |
| 1044 | } |
| 1045 | if (DstTy.isVector() && DstTy.getNumElements() != SrcTy.getNumElements()) |
| 1046 | report("Generic vector extend/truncate must preserve number of lanes", |
| 1047 | MI); |
| 1048 | unsigned DstSize = DstElTy.getSizeInBits(); |
| 1049 | unsigned SrcSize = SrcElTy.getSizeInBits(); |
| 1050 | switch (MI->getOpcode()) { |
| 1051 | default: |
| 1052 | if (DstSize <= SrcSize) |
| 1053 | report("Generic extend has destination type no larger than source", MI); |
| 1054 | break; |
| 1055 | case TargetOpcode::G_TRUNC: |
| 1056 | case TargetOpcode::G_FPTRUNC: |
| 1057 | if (DstSize >= SrcSize) |
| 1058 | report("Generic truncate has destination type no smaller than source", |
| 1059 | MI); |
| 1060 | break; |
| 1061 | } |
| 1062 | break; |
| 1063 | } |
Amara Emerson | e7dca1e | 2018-12-10 18:44:58 +0000 | [diff] [blame] | 1064 | case TargetOpcode::G_MERGE_VALUES: { |
| 1065 | // G_MERGE_VALUES should only be used to merge scalars into a larger scalar, |
| 1066 | // e.g. s2N = MERGE sN, sN |
| 1067 | // Merging multiple scalars into a vector is not allowed, should use |
| 1068 | // G_BUILD_VECTOR for that. |
| 1069 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 1070 | LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); |
| 1071 | if (DstTy.isVector() || SrcTy.isVector()) |
| 1072 | report("G_MERGE_VALUES cannot operate on vectors", MI); |
| 1073 | break; |
| 1074 | } |
| 1075 | case TargetOpcode::G_UNMERGE_VALUES: { |
| 1076 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 1077 | LLT SrcTy = MRI->getType(MI->getOperand(MI->getNumOperands()-1).getReg()); |
| 1078 | // For now G_UNMERGE can split vectors. |
| 1079 | for (unsigned i = 0; i < MI->getNumOperands()-1; ++i) { |
| 1080 | if (MRI->getType(MI->getOperand(i).getReg()) != DstTy) |
| 1081 | report("G_UNMERGE_VALUES destination types do not match", MI); |
| 1082 | } |
| 1083 | if (SrcTy.getSizeInBits() != |
| 1084 | (DstTy.getSizeInBits() * (MI->getNumOperands() - 1))) { |
| 1085 | report("G_UNMERGE_VALUES source operand does not cover dest operands", |
| 1086 | MI); |
| 1087 | } |
| 1088 | break; |
| 1089 | } |
Amara Emerson | 922f82f | 2018-12-05 23:53:30 +0000 | [diff] [blame] | 1090 | case TargetOpcode::G_BUILD_VECTOR: { |
| 1091 | // Source types must be scalars, dest type a vector. Total size of scalars |
| 1092 | // must match the dest vector size. |
| 1093 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 1094 | LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg()); |
| 1095 | if (!DstTy.isVector() || SrcEltTy.isVector()) |
| 1096 | report("G_BUILD_VECTOR must produce a vector from scalar operands", MI); |
| 1097 | for (unsigned i = 2; i < MI->getNumOperands(); ++i) { |
| 1098 | if (MRI->getType(MI->getOperand(1).getReg()) != |
| 1099 | MRI->getType(MI->getOperand(i).getReg())) |
| 1100 | report("G_BUILD_VECTOR source operand types are not homogeneous", MI); |
| 1101 | } |
| 1102 | if (DstTy.getSizeInBits() != |
| 1103 | SrcEltTy.getSizeInBits() * (MI->getNumOperands() - 1)) |
| 1104 | report("G_BUILD_VECTOR src operands total size don't match dest " |
| 1105 | "size.", |
| 1106 | MI); |
| 1107 | break; |
| 1108 | } |
| 1109 | case TargetOpcode::G_BUILD_VECTOR_TRUNC: { |
| 1110 | // Source types must be scalars, dest type a vector. Scalar types must be |
| 1111 | // larger than the dest vector elt type, as this is a truncating operation. |
| 1112 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 1113 | LLT SrcEltTy = MRI->getType(MI->getOperand(1).getReg()); |
| 1114 | if (!DstTy.isVector() || SrcEltTy.isVector()) |
| 1115 | report("G_BUILD_VECTOR_TRUNC must produce a vector from scalar operands", |
| 1116 | MI); |
| 1117 | for (unsigned i = 2; i < MI->getNumOperands(); ++i) { |
| 1118 | if (MRI->getType(MI->getOperand(1).getReg()) != |
| 1119 | MRI->getType(MI->getOperand(i).getReg())) |
| 1120 | report("G_BUILD_VECTOR_TRUNC source operand types are not homogeneous", |
| 1121 | MI); |
| 1122 | } |
| 1123 | if (SrcEltTy.getSizeInBits() <= DstTy.getElementType().getSizeInBits()) |
| 1124 | report("G_BUILD_VECTOR_TRUNC source operand types are not larger than " |
| 1125 | "dest elt type", |
| 1126 | MI); |
| 1127 | break; |
| 1128 | } |
| 1129 | case TargetOpcode::G_CONCAT_VECTORS: { |
| 1130 | // Source types should be vectors, and total size should match the dest |
| 1131 | // vector size. |
| 1132 | LLT DstTy = MRI->getType(MI->getOperand(0).getReg()); |
| 1133 | LLT SrcTy = MRI->getType(MI->getOperand(1).getReg()); |
| 1134 | if (!DstTy.isVector() || !SrcTy.isVector()) |
| 1135 | report("G_CONCAT_VECTOR requires vector source and destination operands", |
| 1136 | MI); |
| 1137 | for (unsigned i = 2; i < MI->getNumOperands(); ++i) { |
| 1138 | if (MRI->getType(MI->getOperand(1).getReg()) != |
| 1139 | MRI->getType(MI->getOperand(i).getReg())) |
| 1140 | report("G_CONCAT_VECTOR source operand types are not homogeneous", MI); |
| 1141 | } |
| 1142 | if (DstTy.getNumElements() != |
| 1143 | SrcTy.getNumElements() * (MI->getNumOperands() - 1)) |
| 1144 | report("G_CONCAT_VECTOR num dest and source elements should match", MI); |
| 1145 | break; |
| 1146 | } |
Aditya Nandakumar | 5aa15f4 | 2018-02-09 01:27:23 +0000 | [diff] [blame] | 1147 | case TargetOpcode::COPY: { |
| 1148 | if (foundErrors) |
| 1149 | break; |
| 1150 | const MachineOperand &DstOp = MI->getOperand(0); |
| 1151 | const MachineOperand &SrcOp = MI->getOperand(1); |
| 1152 | LLT DstTy = MRI->getType(DstOp.getReg()); |
| 1153 | LLT SrcTy = MRI->getType(SrcOp.getReg()); |
| 1154 | if (SrcTy.isValid() && DstTy.isValid()) { |
| 1155 | // If both types are valid, check that the types are the same. |
| 1156 | if (SrcTy != DstTy) { |
| 1157 | report("Copy Instruction is illegal with mismatching types", MI); |
| 1158 | errs() << "Def = " << DstTy << ", Src = " << SrcTy << "\n"; |
| 1159 | } |
| 1160 | } |
| 1161 | if (SrcTy.isValid() || DstTy.isValid()) { |
| 1162 | // If one of them have valid types, let's just check they have the same |
| 1163 | // size. |
| 1164 | unsigned SrcSize = TRI->getRegSizeInBits(SrcOp.getReg(), *MRI); |
| 1165 | unsigned DstSize = TRI->getRegSizeInBits(DstOp.getReg(), *MRI); |
| 1166 | assert(SrcSize && "Expecting size here"); |
| 1167 | assert(DstSize && "Expecting size here"); |
| 1168 | if (SrcSize != DstSize) |
| 1169 | if (!DstOp.getSubReg() && !SrcOp.getSubReg()) { |
| 1170 | report("Copy Instruction is illegal with mismatching sizes", MI); |
| 1171 | errs() << "Def Size = " << DstSize << ", Src Size = " << SrcSize |
| 1172 | << "\n"; |
| 1173 | } |
| 1174 | } |
| 1175 | break; |
| 1176 | } |
Philip Reames | bfffaf7 | 2017-06-02 16:36:37 +0000 | [diff] [blame] | 1177 | case TargetOpcode::STATEPOINT: |
| 1178 | if (!MI->getOperand(StatepointOpers::IDPos).isImm() || |
| 1179 | !MI->getOperand(StatepointOpers::NBytesPos).isImm() || |
| 1180 | !MI->getOperand(StatepointOpers::NCallArgsPos).isImm()) |
| 1181 | report("meta operands to STATEPOINT not constant!", MI); |
| 1182 | break; |
Philip Reames | 4b07d03 | 2017-06-02 17:02:33 +0000 | [diff] [blame] | 1183 | |
| 1184 | auto VerifyStackMapConstant = [&](unsigned Offset) { |
| 1185 | if (!MI->getOperand(Offset).isImm() || |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1186 | MI->getOperand(Offset).getImm() != StackMaps::ConstantOp || |
| 1187 | !MI->getOperand(Offset + 1).isImm()) |
Philip Reames | 4b07d03 | 2017-06-02 17:02:33 +0000 | [diff] [blame] | 1188 | report("stack map constant to STATEPOINT not well formed!", MI); |
| 1189 | }; |
| 1190 | const unsigned VarStart = StatepointOpers(MI).getVarIdx(); |
| 1191 | VerifyStackMapConstant(VarStart + StatepointOpers::CCOffset); |
| 1192 | VerifyStackMapConstant(VarStart + StatepointOpers::FlagsOffset); |
| 1193 | VerifyStackMapConstant(VarStart + StatepointOpers::NumDeoptOperandsOffset); |
| 1194 | |
| 1195 | // TODO: verify we have properly encoded deopt arguments |
Philip Reames | bfffaf7 | 2017-06-02 16:36:37 +0000 | [diff] [blame] | 1196 | }; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | void |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1200 | MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1201 | const MachineInstr *MI = MO->getParent(); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1202 | const MCInstrDesc &MCID = MI->getDesc(); |
Alex Lorenz | 43e844c | 2015-08-10 21:47:36 +0000 | [diff] [blame] | 1203 | unsigned NumDefs = MCID.getNumDefs(); |
| 1204 | if (MCID.getOpcode() == TargetOpcode::PATCHPOINT) |
| 1205 | NumDefs = (MONum == 0 && MO->isReg()) ? NumDefs : 0; |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 1206 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1207 | // The first MCID.NumDefs operands must be explicit register defines |
Alex Lorenz | 43e844c | 2015-08-10 21:47:36 +0000 | [diff] [blame] | 1208 | if (MONum < NumDefs) { |
Richard Smith | 11a4fa4 | 2012-08-15 01:39:31 +0000 | [diff] [blame] | 1209 | const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 1210 | if (!MO->isReg()) |
| 1211 | report("Explicit definition must be a register", MO, MONum); |
Evan Cheng | cac58aa | 2012-05-29 19:40:44 +0000 | [diff] [blame] | 1212 | else if (!MO->isDef() && !MCOI.isOptionalDef()) |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 1213 | report("Explicit definition marked as use", MO, MONum); |
| 1214 | else if (MO->isImplicit()) |
| 1215 | report("Explicit definition marked as implicit", MO, MONum); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1216 | } else if (MONum < MCID.getNumOperands()) { |
Richard Smith | 11a4fa4 | 2012-08-15 01:39:31 +0000 | [diff] [blame] | 1217 | const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; |
Eric Christopher | 113a06c | 2010-11-17 00:55:36 +0000 | [diff] [blame] | 1218 | // Don't check if it's the last operand in a variadic instruction. See, |
| 1219 | // e.g., LDM_RET in the arm back end. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1220 | if (MO->isReg() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1221 | !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1222 | if (MO->isDef() && !MCOI.isOptionalDef()) |
Matthias Braun | b38d987 | 2013-10-04 16:53:00 +0000 | [diff] [blame] | 1223 | report("Explicit operand marked as def", MO, MONum); |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 1224 | if (MO->isImplicit()) |
| 1225 | report("Explicit operand marked as implicit", MO, MONum); |
| 1226 | } |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame] | 1227 | |
Jakob Stoklund Olesen | daddf07 | 2012-09-04 18:38:28 +0000 | [diff] [blame] | 1228 | int TiedTo = MCID.getOperandConstraint(MONum, MCOI::TIED_TO); |
| 1229 | if (TiedTo != -1) { |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame] | 1230 | if (!MO->isReg()) |
| 1231 | report("Tied use must be a register", MO, MONum); |
| 1232 | else if (!MO->isTied()) |
| 1233 | report("Operand should be tied", MO, MONum); |
Jakob Stoklund Olesen | daddf07 | 2012-09-04 18:38:28 +0000 | [diff] [blame] | 1234 | else if (unsigned(TiedTo) != MI->findTiedOperandIdx(MONum)) |
| 1235 | report("Tied def doesn't match MCInstrDesc", MO, MONum); |
Mikael Holmen | 0bce6b7 | 2017-07-06 13:18:21 +0000 | [diff] [blame] | 1236 | else if (TargetRegisterInfo::isPhysicalRegister(MO->getReg())) { |
| 1237 | const MachineOperand &MOTied = MI->getOperand(TiedTo); |
| 1238 | if (!MOTied.isReg()) |
| 1239 | report("Tied counterpart must be a register", &MOTied, TiedTo); |
| 1240 | else if (TargetRegisterInfo::isPhysicalRegister(MOTied.getReg()) && |
| 1241 | MO->getReg() != MOTied.getReg()) |
| 1242 | report("Tied physical registers must match.", &MOTied, TiedTo); |
| 1243 | } |
Jakob Stoklund Olesen | ca71c5d | 2012-08-29 00:38:03 +0000 | [diff] [blame] | 1244 | } else if (MO->isReg() && MO->isTied()) |
| 1245 | report("Explicit operand should not be tied", MO, MONum); |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 1246 | } else { |
Jakob Stoklund Olesen | 5711564 | 2009-12-22 21:48:20 +0000 | [diff] [blame] | 1247 | // ARM adds %reg0 operands to indicate predicates. We'll allow that. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 1248 | if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg()) |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 1249 | report("Extra explicit operand on non-variadic instruction", MO, MONum); |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1252 | switch (MO->getType()) { |
| 1253 | case MachineOperand::MO_Register: { |
| 1254 | const unsigned Reg = MO->getReg(); |
| 1255 | if (!Reg) |
| 1256 | return; |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1257 | if (MRI->tracksLiveness() && !MI->isDebugValue()) |
| 1258 | checkLiveness(MO, MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1259 | |
Jakob Stoklund Olesen | daddf07 | 2012-09-04 18:38:28 +0000 | [diff] [blame] | 1260 | // Verify the consistency of tied operands. |
| 1261 | if (MO->isTied()) { |
| 1262 | unsigned OtherIdx = MI->findTiedOperandIdx(MONum); |
| 1263 | const MachineOperand &OtherMO = MI->getOperand(OtherIdx); |
| 1264 | if (!OtherMO.isReg()) |
| 1265 | report("Must be tied to a register", MO, MONum); |
| 1266 | if (!OtherMO.isTied()) |
| 1267 | report("Missing tie flags on tied operand", MO, MONum); |
| 1268 | if (MI->findTiedOperandIdx(OtherIdx) != MONum) |
| 1269 | report("Inconsistent tie links", MO, MONum); |
| 1270 | if (MONum < MCID.getNumDefs()) { |
| 1271 | if (OtherIdx < MCID.getNumOperands()) { |
| 1272 | if (-1 == MCID.getOperandConstraint(OtherIdx, MCOI::TIED_TO)) |
| 1273 | report("Explicit def tied to explicit use without tie constraint", |
| 1274 | MO, MONum); |
| 1275 | } else { |
| 1276 | if (!OtherMO.isImplicit()) |
| 1277 | report("Explicit def should be tied to implicit use", MO, MONum); |
| 1278 | } |
| 1279 | } |
| 1280 | } |
| 1281 | |
Jakob Stoklund Olesen | eba2bbb | 2012-07-25 16:49:11 +0000 | [diff] [blame] | 1282 | // Verify two-address constraints after leaving SSA form. |
| 1283 | unsigned DefIdx; |
| 1284 | if (!MRI->isSSA() && MO->isUse() && |
| 1285 | MI->isRegTiedToDefOperand(MONum, &DefIdx) && |
| 1286 | Reg != MI->getOperand(DefIdx).getReg()) |
| 1287 | report("Two-address instruction operands must be identical", MO, MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1288 | |
| 1289 | // Check register classes. |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1290 | unsigned SubIdx = MO->getSubReg(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1291 | |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1292 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1293 | if (SubIdx) { |
| 1294 | report("Illegal subregister index for physical register", MO, MONum); |
| 1295 | return; |
| 1296 | } |
| 1297 | if (MONum < MCID.getNumOperands()) { |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 1298 | if (const TargetRegisterClass *DRC = |
| 1299 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 1300 | if (!DRC->contains(Reg)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1301 | report("Illegal physical register for instruction", MO, MONum); |
Francis Visoiu Mistrih | e6b8991 | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1302 | errs() << printReg(Reg, TRI) << " is not a " |
| 1303 | << TRI->getRegClassName(DRC) << " register.\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1306 | } |
Geoff Berry | 42ffd3b | 2018-01-29 18:57:07 +0000 | [diff] [blame] | 1307 | if (MO->isRenamable()) { |
Geoff Berry | 13357c9 | 2018-02-23 18:25:08 +0000 | [diff] [blame] | 1308 | if (MRI->isReserved(Reg)) { |
Geoff Berry | 42ffd3b | 2018-01-29 18:57:07 +0000 | [diff] [blame] | 1309 | report("isRenamable set on reserved register", MO, MONum); |
Geoff Berry | 13357c9 | 2018-02-23 18:25:08 +0000 | [diff] [blame] | 1310 | return; |
| 1311 | } |
Geoff Berry | 3b391fe | 2017-12-12 17:53:59 +0000 | [diff] [blame] | 1312 | } |
Mikael Holmen | b16b4ba | 2018-06-21 10:03:34 +0000 | [diff] [blame] | 1313 | if (MI->isDebugValue() && MO->isUse() && !MO->isDebug()) { |
| 1314 | report("Use-reg is not IsDebug in a DBG_VALUE", MO, MONum); |
| 1315 | return; |
| 1316 | } |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1317 | } else { |
| 1318 | // Virtual register. |
| 1319 | const TargetRegisterClass *RC = MRI->getRegClassOrNull(Reg); |
| 1320 | if (!RC) { |
| 1321 | // This is a generic virtual register. |
Ahmed Bougacha | 35426be | 2016-08-02 16:49:22 +0000 | [diff] [blame] | 1322 | |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1323 | // If we're post-Select, we can't have gvregs anymore. |
| 1324 | if (isFunctionSelected) { |
| 1325 | report("Generic virtual register invalid in a Selected function", |
| 1326 | MO, MONum); |
| 1327 | return; |
Quentin Colombet | 7500ba0 | 2016-04-08 16:35:22 +0000 | [diff] [blame] | 1328 | } |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1329 | |
| 1330 | // The gvreg must have a type and it must not have a SubIdx. |
| 1331 | LLT Ty = MRI->getType(Reg); |
| 1332 | if (!Ty.isValid()) { |
| 1333 | report("Generic virtual register must have a valid type", MO, |
| 1334 | MONum); |
| 1335 | return; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1336 | } |
Matthias Braun | 026c5b3 | 2017-11-28 03:54:20 +0000 | [diff] [blame] | 1337 | |
| 1338 | const RegisterBank *RegBank = MRI->getRegBankOrNull(Reg); |
| 1339 | |
| 1340 | // If we're post-RegBankSelect, the gvreg must have a bank. |
| 1341 | if (!RegBank && isFunctionRegBankSelected) { |
| 1342 | report("Generic virtual register must have a bank in a " |
| 1343 | "RegBankSelected function", |
| 1344 | MO, MONum); |
| 1345 | return; |
| 1346 | } |
| 1347 | |
| 1348 | // Make sure the register fits into its register bank if any. |
| 1349 | if (RegBank && Ty.isValid() && |
| 1350 | RegBank->getSize() < Ty.getSizeInBits()) { |
| 1351 | report("Register bank is too small for virtual register", MO, |
| 1352 | MONum); |
| 1353 | errs() << "Register bank " << RegBank->getName() << " too small(" |
| 1354 | << RegBank->getSize() << ") to fit " << Ty.getSizeInBits() |
| 1355 | << "-bits\n"; |
| 1356 | return; |
| 1357 | } |
| 1358 | if (SubIdx) { |
| 1359 | report("Generic virtual register does not subregister index", MO, |
| 1360 | MONum); |
| 1361 | return; |
| 1362 | } |
| 1363 | |
| 1364 | // If this is a target specific instruction and this operand |
| 1365 | // has register class constraint, the virtual register must |
| 1366 | // comply to it. |
| 1367 | if (!isPreISelGenericOpcode(MCID.getOpcode()) && |
| 1368 | MONum < MCID.getNumOperands() && |
| 1369 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
| 1370 | report("Virtual register does not match instruction constraint", MO, |
| 1371 | MONum); |
| 1372 | errs() << "Expect register class " |
| 1373 | << TRI->getRegClassName( |
| 1374 | TII->getRegClass(MCID, MONum, TRI, *MF)) |
| 1375 | << " but got nothing\n"; |
| 1376 | return; |
| 1377 | } |
| 1378 | |
| 1379 | break; |
| 1380 | } |
| 1381 | if (SubIdx) { |
| 1382 | const TargetRegisterClass *SRC = |
| 1383 | TRI->getSubClassWithSubReg(RC, SubIdx); |
| 1384 | if (!SRC) { |
| 1385 | report("Invalid subregister index for virtual register", MO, MONum); |
| 1386 | errs() << "Register class " << TRI->getRegClassName(RC) |
| 1387 | << " does not support subreg index " << SubIdx << "\n"; |
| 1388 | return; |
| 1389 | } |
| 1390 | if (RC != SRC) { |
| 1391 | report("Invalid register class for subregister index", MO, MONum); |
| 1392 | errs() << "Register class " << TRI->getRegClassName(RC) |
| 1393 | << " does not fully support subreg index " << SubIdx << "\n"; |
| 1394 | return; |
| 1395 | } |
| 1396 | } |
| 1397 | if (MONum < MCID.getNumOperands()) { |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 1398 | if (const TargetRegisterClass *DRC = |
| 1399 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 1400 | if (SubIdx) { |
| 1401 | const TargetRegisterClass *SuperRC = |
Eric Christopher | 4ec858e | 2015-03-10 23:46:01 +0000 | [diff] [blame] | 1402 | TRI->getLargestLegalSuperClass(RC, *MF); |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 1403 | if (!SuperRC) { |
| 1404 | report("No largest legal super class exists.", MO, MONum); |
| 1405 | return; |
| 1406 | } |
| 1407 | DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx); |
| 1408 | if (!DRC) { |
| 1409 | report("No matching super-reg register class.", MO, MONum); |
| 1410 | return; |
| 1411 | } |
| 1412 | } |
Jakob Stoklund Olesen | fa226bc | 2011-06-02 05:43:46 +0000 | [diff] [blame] | 1413 | if (!RC->hasSuperClassEq(DRC)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1414 | report("Illegal virtual register for instruction", MO, MONum); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 1415 | errs() << "Expected a " << TRI->getRegClassName(DRC) |
Craig Topper | a5babc8 | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 1416 | << " register, but got a " << TRI->getRegClassName(RC) |
| 1417 | << " register\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | } |
| 1422 | break; |
| 1423 | } |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 1424 | |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 1425 | case MachineOperand::MO_RegisterMask: |
| 1426 | regMasks.push_back(MO->getRegMask()); |
| 1427 | break; |
| 1428 | |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 1429 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1430 | if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent())) |
| 1431 | report("PHI operand is not in the CFG", MO, MONum); |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 1432 | break; |
| 1433 | |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 1434 | case MachineOperand::MO_FrameIndex: |
| 1435 | if (LiveStks && LiveStks->hasInterval(MO->getIndex()) && |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1436 | LiveInts && !LiveInts->isNotInMIMap(*MI)) { |
Jonas Paulsson | 0b88d3e | 2015-10-29 08:28:35 +0000 | [diff] [blame] | 1437 | int FI = MO->getIndex(); |
| 1438 | LiveInterval &LI = LiveStks->getInterval(FI); |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1439 | SlotIndex Idx = LiveInts->getInstructionIndex(*MI); |
Jonas Paulsson | 407b46b | 2015-10-21 07:39:47 +0000 | [diff] [blame] | 1440 | |
Jonas Paulsson | 407b46b | 2015-10-21 07:39:47 +0000 | [diff] [blame] | 1441 | bool stores = MI->mayStore(); |
Jonas Paulsson | 0b88d3e | 2015-10-29 08:28:35 +0000 | [diff] [blame] | 1442 | bool loads = MI->mayLoad(); |
| 1443 | // For a memory-to-memory move, we need to check if the frame |
| 1444 | // index is used for storing or loading, by inspecting the |
| 1445 | // memory operands. |
| 1446 | if (stores && loads) { |
| 1447 | for (auto *MMO : MI->memoperands()) { |
| 1448 | const PseudoSourceValue *PSV = MMO->getPseudoValue(); |
| 1449 | if (PSV == nullptr) continue; |
| 1450 | const FixedStackPseudoSourceValue *Value = |
| 1451 | dyn_cast<FixedStackPseudoSourceValue>(PSV); |
| 1452 | if (Value == nullptr) continue; |
| 1453 | if (Value->getFrameIndex() != FI) continue; |
| 1454 | |
| 1455 | if (MMO->isStore()) |
| 1456 | loads = false; |
| 1457 | else |
| 1458 | stores = false; |
| 1459 | break; |
| 1460 | } |
| 1461 | if (loads == stores) |
| 1462 | report("Missing fixed stack memoperand.", MI); |
| 1463 | } |
| 1464 | if (loads && !LI.liveAt(Idx.getRegSlot(true))) { |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 1465 | report("Instruction loads from dead spill slot", MO, MONum); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 1466 | errs() << "Live stack: " << LI << '\n'; |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 1467 | } |
Jonas Paulsson | 407b46b | 2015-10-21 07:39:47 +0000 | [diff] [blame] | 1468 | if (stores && !LI.liveAt(Idx.getRegSlot())) { |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 1469 | report("Instruction stores to dead spill slot", MO, MONum); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 1470 | errs() << "Live stack: " << LI << '\n'; |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | break; |
| 1474 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1475 | default: |
| 1476 | break; |
| 1477 | } |
| 1478 | } |
| 1479 | |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1480 | void MachineVerifier::checkLivenessAtUse(const MachineOperand *MO, |
| 1481 | unsigned MONum, SlotIndex UseIdx, const LiveRange &LR, unsigned VRegOrUnit, |
| 1482 | LaneBitmask LaneMask) { |
| 1483 | LiveQueryResult LRQ = LR.Query(UseIdx); |
| 1484 | // Check if we have a segment at the use, note however that we only need one |
| 1485 | // live subregister range, the others may be dead. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1486 | if (!LRQ.valueIn() && LaneMask.none()) { |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1487 | report("No live segment at use", MO, MONum); |
| 1488 | report_context_liverange(LR); |
| 1489 | report_context_vreg_regunit(VRegOrUnit); |
| 1490 | report_context(UseIdx); |
| 1491 | } |
| 1492 | if (MO->isKill() && !LRQ.isKill()) { |
| 1493 | report("Live range continues after kill flag", MO, MONum); |
| 1494 | report_context_liverange(LR); |
| 1495 | report_context_vreg_regunit(VRegOrUnit); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1496 | if (LaneMask.any()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1497 | report_context_lanemask(LaneMask); |
| 1498 | report_context(UseIdx); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | void MachineVerifier::checkLivenessAtDef(const MachineOperand *MO, |
| 1503 | unsigned MONum, SlotIndex DefIdx, const LiveRange &LR, unsigned VRegOrUnit, |
Bjorn Pettersson | 32a3289 | 2018-09-20 06:59:18 +0000 | [diff] [blame] | 1504 | bool SubRangeCheck, LaneBitmask LaneMask) { |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1505 | if (const VNInfo *VNI = LR.getVNInfoAt(DefIdx)) { |
| 1506 | assert(VNI && "NULL valno is not allowed"); |
| 1507 | if (VNI->def != DefIdx) { |
| 1508 | report("Inconsistent valno->def", MO, MONum); |
| 1509 | report_context_liverange(LR); |
| 1510 | report_context_vreg_regunit(VRegOrUnit); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1511 | if (LaneMask.any()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1512 | report_context_lanemask(LaneMask); |
| 1513 | report_context(*VNI); |
| 1514 | report_context(DefIdx); |
| 1515 | } |
| 1516 | } else { |
| 1517 | report("No live segment at def", MO, MONum); |
| 1518 | report_context_liverange(LR); |
| 1519 | report_context_vreg_regunit(VRegOrUnit); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1520 | if (LaneMask.any()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1521 | report_context_lanemask(LaneMask); |
| 1522 | report_context(DefIdx); |
| 1523 | } |
| 1524 | // Check that, if the dead def flag is present, LiveInts agree. |
| 1525 | if (MO->isDead()) { |
| 1526 | LiveQueryResult LRQ = LR.Query(DefIdx); |
| 1527 | if (!LRQ.isDeadDef()) { |
Bjorn Pettersson | 32a3289 | 2018-09-20 06:59:18 +0000 | [diff] [blame] | 1528 | assert(TargetRegisterInfo::isVirtualRegister(VRegOrUnit) && |
| 1529 | "Expecting a virtual register."); |
| 1530 | // A dead subreg def only tells us that the specific subreg is dead. There |
| 1531 | // could be other non-dead defs of other subregs, or we could have other |
| 1532 | // parts of the register being live through the instruction. So unless we |
| 1533 | // are checking liveness for a subrange it is ok for the live range to |
| 1534 | // continue, given that we have a dead def of a subregister. |
| 1535 | if (SubRangeCheck || MO->getSubReg() == 0) { |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1536 | report("Live range continues after dead def flag", MO, MONum); |
| 1537 | report_context_liverange(LR); |
| 1538 | report_context_vreg_regunit(VRegOrUnit); |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 1539 | if (LaneMask.any()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1540 | report_context_lanemask(LaneMask); |
| 1541 | } |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1546 | void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) { |
| 1547 | const MachineInstr *MI = MO->getParent(); |
| 1548 | const unsigned Reg = MO->getReg(); |
| 1549 | |
| 1550 | // Both use and def operands can read a register. |
| 1551 | if (MO->readsReg()) { |
Jakob Stoklund Olesen | eba2bbb | 2012-07-25 16:49:11 +0000 | [diff] [blame] | 1552 | if (MO->isKill()) |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1553 | addRegWithSubRegs(regsKilled, Reg); |
| 1554 | |
| 1555 | // Check that LiveVars knows this kill. |
| 1556 | if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 1557 | MO->isKill()) { |
| 1558 | LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); |
David Majnemer | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 1559 | if (!is_contained(VI.Kills, MI)) |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1560 | report("Kill missing from LiveVariables", MO, MONum); |
| 1561 | } |
| 1562 | |
| 1563 | // Check LiveInts liveness and kill. |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1564 | if (LiveInts && !LiveInts->isNotInMIMap(*MI)) { |
| 1565 | SlotIndex UseIdx = LiveInts->getInstructionIndex(*MI); |
Jakob Stoklund Olesen | a62e1e8 | 2012-08-01 23:52:40 +0000 | [diff] [blame] | 1566 | // Check the cached regunit intervals. |
| 1567 | if (TargetRegisterInfo::isPhysicalRegister(Reg) && !isReserved(Reg)) { |
| 1568 | for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) { |
Matthias Braun | 01b6128 | 2017-09-01 18:36:26 +0000 | [diff] [blame] | 1569 | if (MRI->isReservedRegUnit(*Units)) |
| 1570 | continue; |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1571 | if (const LiveRange *LR = LiveInts->getCachedRegUnit(*Units)) |
| 1572 | checkLivenessAtUse(MO, MONum, UseIdx, *LR, *Units); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1573 | } |
Jakob Stoklund Olesen | a62e1e8 | 2012-08-01 23:52:40 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 1577 | if (LiveInts->hasInterval(Reg)) { |
| 1578 | // This is a virtual register interval. |
| 1579 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1580 | checkLivenessAtUse(MO, MONum, UseIdx, LI, Reg); |
| 1581 | |
| 1582 | if (LI.hasSubRanges() && !MO->isDef()) { |
| 1583 | unsigned SubRegIdx = MO->getSubReg(); |
| 1584 | LaneBitmask MOMask = SubRegIdx != 0 |
| 1585 | ? TRI->getSubRegIndexLaneMask(SubRegIdx) |
| 1586 | : MRI->getMaxLaneMaskForVReg(Reg); |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1587 | LaneBitmask LiveInMask; |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1588 | for (const LiveInterval::SubRange &SR : LI.subranges()) { |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1589 | if ((MOMask & SR.LaneMask).none()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1590 | continue; |
| 1591 | checkLivenessAtUse(MO, MONum, UseIdx, SR, Reg, SR.LaneMask); |
| 1592 | LiveQueryResult LRQ = SR.Query(UseIdx); |
| 1593 | if (LRQ.valueIn()) |
| 1594 | LiveInMask |= SR.LaneMask; |
| 1595 | } |
| 1596 | // At least parts of the register has to be live at the use. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1597 | if ((LiveInMask & MOMask).none()) { |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1598 | report("No live subrange at use", MO, MONum); |
| 1599 | report_context(LI); |
| 1600 | report_context(UseIdx); |
| 1601 | } |
Jakob Stoklund Olesen | a62e1e8 | 2012-08-01 23:52:40 +0000 | [diff] [blame] | 1602 | } |
| 1603 | } else { |
| 1604 | report("Virtual register has no live interval", MO, MONum); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1605 | } |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | |
| 1609 | // Use of a dead register. |
| 1610 | if (!regsLive.count(Reg)) { |
| 1611 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 1612 | // Reserved registers may be used even when 'dead'. |
Matthias Braun | 5e40bd7 | 2014-12-10 01:13:13 +0000 | [diff] [blame] | 1613 | bool Bad = !isReserved(Reg); |
| 1614 | // We are fine if just any subregister has a defined value. |
| 1615 | if (Bad) { |
| 1616 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); |
| 1617 | ++SubRegs) { |
| 1618 | if (regsLive.count(*SubRegs)) { |
| 1619 | Bad = false; |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | } |
Matthias Braun | de8202b | 2015-01-14 22:25:14 +0000 | [diff] [blame] | 1624 | // If there is an additional implicit-use of a super register we stop |
| 1625 | // here. By definition we are fine if the super register is not |
| 1626 | // (completely) dead, if the complete super register is dead we will |
| 1627 | // get a report for its operand. |
| 1628 | if (Bad) { |
| 1629 | for (const MachineOperand &MOP : MI->uses()) { |
Matt Arsenault | 9b313de | 2018-08-27 17:40:09 +0000 | [diff] [blame] | 1630 | if (!MOP.isReg() || !MOP.isImplicit()) |
Matthias Braun | de8202b | 2015-01-14 22:25:14 +0000 | [diff] [blame] | 1631 | continue; |
Matt Arsenault | 9b313de | 2018-08-27 17:40:09 +0000 | [diff] [blame] | 1632 | |
| 1633 | if (!TargetRegisterInfo::isPhysicalRegister(MOP.getReg())) |
Matthias Braun | de8202b | 2015-01-14 22:25:14 +0000 | [diff] [blame] | 1634 | continue; |
Matt Arsenault | 9b313de | 2018-08-27 17:40:09 +0000 | [diff] [blame] | 1635 | |
Matthias Braun | de8202b | 2015-01-14 22:25:14 +0000 | [diff] [blame] | 1636 | for (MCSubRegIterator SubRegs(MOP.getReg(), TRI); SubRegs.isValid(); |
| 1637 | ++SubRegs) { |
| 1638 | if (*SubRegs == Reg) { |
| 1639 | Bad = false; |
| 1640 | break; |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | } |
Matthias Braun | 5e40bd7 | 2014-12-10 01:13:13 +0000 | [diff] [blame] | 1645 | if (Bad) |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1646 | report("Using an undefined physical register", MO, MONum); |
Pete Cooper | b97c57a | 2012-07-19 23:40:38 +0000 | [diff] [blame] | 1647 | } else if (MRI->def_empty(Reg)) { |
| 1648 | report("Reading virtual register without a def", MO, MONum); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1649 | } else { |
| 1650 | BBInfo &MInfo = MBBInfoMap[MI->getParent()]; |
| 1651 | // We don't know which virtual registers are live in, so only complain |
| 1652 | // if vreg was killed in this MBB. Otherwise keep track of vregs that |
| 1653 | // must be live in. PHI instructions are handled separately. |
| 1654 | if (MInfo.regsKilled.count(Reg)) |
| 1655 | report("Using a killed virtual register", MO, MONum); |
| 1656 | else if (!MI->isPHI()) |
| 1657 | MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI)); |
| 1658 | } |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | if (MO->isDef()) { |
| 1663 | // Register defined. |
| 1664 | // TODO: verify that earlyclobber ops are not used. |
| 1665 | if (MO->isDead()) |
| 1666 | addRegWithSubRegs(regsDead, Reg); |
| 1667 | else |
| 1668 | addRegWithSubRegs(regsDefined, Reg); |
| 1669 | |
| 1670 | // Verify SSA form. |
| 1671 | if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) && |
Benjamin Kramer | d628f19 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1672 | std::next(MRI->def_begin(Reg)) != MRI->def_end()) |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1673 | report("Multiple virtual register defs in SSA form", MO, MONum); |
| 1674 | |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 1675 | // Check LiveInts for a live segment, but only for virtual registers. |
Duncan P. N. Exon Smith | 42e1835 | 2016-02-27 06:40:41 +0000 | [diff] [blame] | 1676 | if (LiveInts && !LiveInts->isNotInMIMap(*MI)) { |
| 1677 | SlotIndex DefIdx = LiveInts->getInstructionIndex(*MI); |
Jakob Stoklund Olesen | f935e94 | 2012-06-22 22:23:58 +0000 | [diff] [blame] | 1678 | DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber()); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1679 | |
| 1680 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 1681 | if (LiveInts->hasInterval(Reg)) { |
| 1682 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 1683 | checkLivenessAtDef(MO, MONum, DefIdx, LI, Reg); |
| 1684 | |
| 1685 | if (LI.hasSubRanges()) { |
| 1686 | unsigned SubRegIdx = MO->getSubReg(); |
| 1687 | LaneBitmask MOMask = SubRegIdx != 0 |
| 1688 | ? TRI->getSubRegIndexLaneMask(SubRegIdx) |
| 1689 | : MRI->getMaxLaneMaskForVReg(Reg); |
| 1690 | for (const LiveInterval::SubRange &SR : LI.subranges()) { |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 1691 | if ((SR.LaneMask & MOMask).none()) |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1692 | continue; |
Bjorn Pettersson | 32a3289 | 2018-09-20 06:59:18 +0000 | [diff] [blame] | 1693 | checkLivenessAtDef(MO, MONum, DefIdx, SR, Reg, true, SR.LaneMask); |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1694 | } |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1695 | } |
| 1696 | } else { |
Matthias Braun | 34a1c16 | 2016-02-02 20:04:51 +0000 | [diff] [blame] | 1697 | report("Virtual register has no Live interval", MO, MONum); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1698 | } |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 1704 | void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) {} |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 1705 | |
| 1706 | // This function gets called after visiting all instructions in a bundle. The |
| 1707 | // argument points to the bundle header. |
| 1708 | // Normal stand-alone instructions are also considered 'bundles', and this |
| 1709 | // function is called for all of them. |
| 1710 | void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1711 | BBInfo &MInfo = MBBInfoMap[MI->getParent()]; |
| 1712 | set_union(MInfo.regsKilled, regsKilled); |
Jakob Stoklund Olesen | 73cf709 | 2010-08-05 18:59:59 +0000 | [diff] [blame] | 1713 | set_subtract(regsLive, regsKilled); regsKilled.clear(); |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 1714 | // Kill any masked registers. |
| 1715 | while (!regMasks.empty()) { |
| 1716 | const uint32_t *Mask = regMasks.pop_back_val(); |
| 1717 | for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I) |
| 1718 | if (TargetRegisterInfo::isPhysicalRegister(*I) && |
| 1719 | MachineOperand::clobbersPhysReg(Mask, *I)) |
| 1720 | regsDead.push_back(*I); |
| 1721 | } |
Jakob Stoklund Olesen | 73cf709 | 2010-08-05 18:59:59 +0000 | [diff] [blame] | 1722 | set_subtract(regsLive, regsDead); regsDead.clear(); |
| 1723 | set_union(regsLive, regsDefined); regsDefined.clear(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | void |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1727 | MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1728 | MBBInfoMap[MBB].regsLiveOut = regsLive; |
| 1729 | regsLive.clear(); |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 1730 | |
| 1731 | if (Indexes) { |
| 1732 | SlotIndex stop = Indexes->getMBBEndIdx(MBB); |
| 1733 | if (!(stop > lastIndex)) { |
| 1734 | report("Block ends before last instruction index", MBB); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 1735 | errs() << "Block ends at " << stop |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 1736 | << " last instruction was at " << lastIndex << '\n'; |
| 1737 | } |
| 1738 | lastIndex = stop; |
| 1739 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | // Calculate the largest possible vregsPassed sets. These are the registers that |
| 1743 | // can pass through an MBB live, but may not be live every time. It is assumed |
| 1744 | // that all vregsPassed sets are empty before the call. |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1745 | void MachineVerifier::calcRegsPassed() { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1746 | // First push live-out regs to successors' vregsPassed. Remember the MBBs that |
| 1747 | // have any vregsPassed. |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1748 | SmallPtrSet<const MachineBasicBlock*, 8> todo; |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1749 | for (const auto &MBB : *MF) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1750 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 1751 | if (!MInfo.reachable) |
| 1752 | continue; |
| 1753 | for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(), |
| 1754 | SuE = MBB.succ_end(); SuI != SuE; ++SuI) { |
| 1755 | BBInfo &SInfo = MBBInfoMap[*SuI]; |
| 1756 | if (SInfo.addPassed(MInfo.regsLiveOut)) |
| 1757 | todo.insert(*SuI); |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | // Iteratively push vregsPassed to successors. This will converge to the same |
| 1762 | // final state regardless of DenseSet iteration order. |
| 1763 | while (!todo.empty()) { |
| 1764 | const MachineBasicBlock *MBB = *todo.begin(); |
| 1765 | todo.erase(MBB); |
| 1766 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 1767 | for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), |
| 1768 | SuE = MBB->succ_end(); SuI != SuE; ++SuI) { |
| 1769 | if (*SuI == MBB) |
| 1770 | continue; |
| 1771 | BBInfo &SInfo = MBBInfoMap[*SuI]; |
| 1772 | if (SInfo.addPassed(MInfo.vregsPassed)) |
| 1773 | todo.insert(*SuI); |
| 1774 | } |
| 1775 | } |
| 1776 | } |
| 1777 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1778 | // Calculate the set of virtual registers that must be passed through each basic |
| 1779 | // block in order to satisfy the requirements of successor blocks. This is very |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1780 | // similar to calcRegsPassed, only backwards. |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1781 | void MachineVerifier::calcRegsRequired() { |
| 1782 | // First push live-in regs to predecessors' vregsRequired. |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1783 | SmallPtrSet<const MachineBasicBlock*, 8> todo; |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1784 | for (const auto &MBB : *MF) { |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1785 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 1786 | for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(), |
| 1787 | PrE = MBB.pred_end(); PrI != PrE; ++PrI) { |
| 1788 | BBInfo &PInfo = MBBInfoMap[*PrI]; |
| 1789 | if (PInfo.addRequired(MInfo.vregsLiveIn)) |
| 1790 | todo.insert(*PrI); |
| 1791 | } |
| 1792 | } |
| 1793 | |
| 1794 | // Iteratively push vregsRequired to predecessors. This will converge to the |
| 1795 | // same final state regardless of DenseSet iteration order. |
| 1796 | while (!todo.empty()) { |
| 1797 | const MachineBasicBlock *MBB = *todo.begin(); |
| 1798 | todo.erase(MBB); |
| 1799 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 1800 | for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), |
| 1801 | PrE = MBB->pred_end(); PrI != PrE; ++PrI) { |
| 1802 | if (*PrI == MBB) |
| 1803 | continue; |
| 1804 | BBInfo &SInfo = MBBInfoMap[*PrI]; |
| 1805 | if (SInfo.addRequired(MInfo.vregsRequired)) |
| 1806 | todo.insert(*PrI); |
| 1807 | } |
| 1808 | } |
| 1809 | } |
| 1810 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1811 | // Check PHI instructions at the beginning of MBB. It is assumed that |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1812 | // calcRegsPassed has been run so BBInfo::isLiveOut is valid. |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1813 | void MachineVerifier::checkPHIOps(const MachineBasicBlock &MBB) { |
| 1814 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 1815 | |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1816 | SmallPtrSet<const MachineBasicBlock*, 8> seen; |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1817 | for (const MachineInstr &Phi : MBB) { |
| 1818 | if (!Phi.isPHI()) |
Alexey Samsonov | 8467812 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 1819 | break; |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1820 | seen.clear(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1821 | |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1822 | const MachineOperand &MODef = Phi.getOperand(0); |
| 1823 | if (!MODef.isReg() || !MODef.isDef()) { |
| 1824 | report("Expected first PHI operand to be a register def", &MODef, 0); |
| 1825 | continue; |
| 1826 | } |
| 1827 | if (MODef.isTied() || MODef.isImplicit() || MODef.isInternalRead() || |
| 1828 | MODef.isEarlyClobber() || MODef.isDebug()) |
| 1829 | report("Unexpected flag on PHI operand", &MODef, 0); |
| 1830 | unsigned DefReg = MODef.getReg(); |
| 1831 | if (!TargetRegisterInfo::isVirtualRegister(DefReg)) |
| 1832 | report("Expected first PHI operand to be a virtual register", &MODef, 0); |
| 1833 | |
| 1834 | for (unsigned I = 1, E = Phi.getNumOperands(); I != E; I += 2) { |
| 1835 | const MachineOperand &MO0 = Phi.getOperand(I); |
| 1836 | if (!MO0.isReg()) { |
| 1837 | report("Expected PHI operand to be a register", &MO0, I); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1838 | continue; |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1839 | } |
| 1840 | if (MO0.isImplicit() || MO0.isInternalRead() || MO0.isEarlyClobber() || |
| 1841 | MO0.isDebug() || MO0.isTied()) |
| 1842 | report("Unexpected flag on PHI operand", &MO0, I); |
| 1843 | |
| 1844 | const MachineOperand &MO1 = Phi.getOperand(I + 1); |
| 1845 | if (!MO1.isMBB()) { |
| 1846 | report("Expected PHI operand to be a basic block", &MO1, I + 1); |
| 1847 | continue; |
| 1848 | } |
| 1849 | |
| 1850 | const MachineBasicBlock &Pre = *MO1.getMBB(); |
| 1851 | if (!Pre.isSuccessor(&MBB)) { |
| 1852 | report("PHI input is not a predecessor block", &MO1, I + 1); |
| 1853 | continue; |
| 1854 | } |
| 1855 | |
| 1856 | if (MInfo.reachable) { |
| 1857 | seen.insert(&Pre); |
| 1858 | BBInfo &PrInfo = MBBInfoMap[&Pre]; |
Matthias Braun | 998cdc4 | 2017-12-04 18:57:48 +0000 | [diff] [blame] | 1859 | if (!MO0.isUndef() && PrInfo.reachable && |
| 1860 | !PrInfo.isLiveOut(MO0.getReg())) |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1861 | report("PHI operand is not live-out from predecessor", &MO0, I); |
| 1862 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | // Did we see all predecessors? |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1866 | if (MInfo.reachable) { |
| 1867 | for (MachineBasicBlock *Pred : MBB.predecessors()) { |
| 1868 | if (!seen.count(Pred)) { |
| 1869 | report("Missing PHI operand", &Phi); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 1870 | errs() << printMBBReference(*Pred) |
| 1871 | << " is a predecessor according to the CFG.\n"; |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1872 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1878 | void MachineVerifier::visitMachineFunctionAfter() { |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1879 | calcRegsPassed(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1880 | |
Matthias Braun | ee61a1d | 2017-11-28 03:54:19 +0000 | [diff] [blame] | 1881 | for (const MachineBasicBlock &MBB : *MF) |
| 1882 | checkPHIOps(MBB); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1883 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1884 | // Now check liveness info if available |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1885 | calcRegsRequired(); |
| 1886 | |
Jakob Stoklund Olesen | bb07216 | 2012-06-29 21:00:00 +0000 | [diff] [blame] | 1887 | // Check for killed virtual registers that should be live out. |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1888 | for (const auto &MBB : *MF) { |
| 1889 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
Jakob Stoklund Olesen | bb07216 | 2012-06-29 21:00:00 +0000 | [diff] [blame] | 1890 | for (RegSet::iterator |
| 1891 | I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; |
| 1892 | ++I) |
| 1893 | if (MInfo.regsKilled.count(*I)) { |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1894 | report("Virtual register killed in block, but needed live out.", &MBB); |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1895 | errs() << "Virtual register " << printReg(*I) |
Francis Visoiu Mistrih | e6b8991 | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1896 | << " is used after the block.\n"; |
Jakob Stoklund Olesen | bb07216 | 2012-06-29 21:00:00 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
Jakob Stoklund Olesen | a4e6397 | 2012-06-25 18:18:27 +0000 | [diff] [blame] | 1900 | if (!MF->empty()) { |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1901 | BBInfo &MInfo = MBBInfoMap[&MF->front()]; |
| 1902 | for (RegSet::iterator |
| 1903 | I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; |
Matthias Braun | 1e25675 | 2016-05-11 21:31:39 +0000 | [diff] [blame] | 1904 | ++I) { |
| 1905 | report("Virtual register defs don't dominate all uses.", MF); |
| 1906 | report_context_vreg(*I); |
| 1907 | } |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1910 | if (LiveVars) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1911 | verifyLiveVariables(); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1912 | if (LiveInts) |
| 1913 | verifyLiveIntervals(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1914 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1915 | |
| 1916 | void MachineVerifier::verifyLiveVariables() { |
| 1917 | assert(LiveVars && "Don't call verifyLiveVariables without LiveVars"); |
Jakob Stoklund Olesen | 98c5476 | 2011-01-08 23:11:02 +0000 | [diff] [blame] | 1918 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 1919 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1920 | LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1921 | for (const auto &MBB : *MF) { |
| 1922 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1923 | |
| 1924 | // Our vregsRequired should be identical to LiveVariables' AliveBlocks |
| 1925 | if (MInfo.vregsRequired.count(Reg)) { |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1926 | if (!VI.AliveBlocks.test(MBB.getNumber())) { |
| 1927 | report("LiveVariables: Block missing from AliveBlocks", &MBB); |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1928 | errs() << "Virtual register " << printReg(Reg) |
Francis Visoiu Mistrih | e6b8991 | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1929 | << " must be live through the block.\n"; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } else { |
Alexey Samsonov | 4aef727 | 2014-04-30 18:29:51 +0000 | [diff] [blame] | 1932 | if (VI.AliveBlocks.test(MBB.getNumber())) { |
| 1933 | report("LiveVariables: Block should not be in AliveBlocks", &MBB); |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1934 | errs() << "Virtual register " << printReg(Reg) |
Francis Visoiu Mistrih | e6b8991 | 2017-11-30 16:12:24 +0000 | [diff] [blame] | 1935 | << " is not needed live through the block.\n"; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | } |
| 1939 | } |
| 1940 | } |
| 1941 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1942 | void MachineVerifier::verifyLiveIntervals() { |
| 1943 | assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts"); |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1944 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 1945 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 893ab5d | 2010-10-06 23:54:35 +0000 | [diff] [blame] | 1946 | |
| 1947 | // Spilling and splitting may leave unused registers around. Skip them. |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1948 | if (MRI->reg_nodbg_empty(Reg)) |
Jakob Stoklund Olesen | 893ab5d | 2010-10-06 23:54:35 +0000 | [diff] [blame] | 1949 | continue; |
| 1950 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1951 | if (!LiveInts->hasInterval(Reg)) { |
| 1952 | report("Missing live interval for virtual register", MF); |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 1953 | errs() << printReg(Reg, TRI) << " still has defs or uses\n"; |
Jakob Stoklund Olesen | 8c45642 | 2010-10-28 20:44:22 +0000 | [diff] [blame] | 1954 | continue; |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1955 | } |
Jakob Stoklund Olesen | 8c45642 | 2010-10-28 20:44:22 +0000 | [diff] [blame] | 1956 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1957 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 1958 | assert(Reg == LI.reg && "Invalid reg to interval mapping"); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1959 | verifyLiveInterval(LI); |
| 1960 | } |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 1961 | |
| 1962 | // Verify all the cached regunit intervals. |
| 1963 | for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i) |
Matthias Braun | 4f3b5e8 | 2013-10-10 21:29:02 +0000 | [diff] [blame] | 1964 | if (const LiveRange *LR = LiveInts->getCachedRegUnit(i)) |
| 1965 | verifyLiveRange(*LR, i); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1966 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1967 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 1968 | void MachineVerifier::verifyLiveRangeValue(const LiveRange &LR, |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 1969 | const VNInfo *VNI, unsigned Reg, |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 1970 | LaneBitmask LaneMask) { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1971 | if (VNI->isUnused()) |
| 1972 | return; |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1973 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 1974 | const VNInfo *DefVNI = LR.getVNInfoAt(VNI->def); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1975 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1976 | if (!DefVNI) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 1977 | report("Value not live at VNInfo def and not marked unused", MF); |
| 1978 | report_context(LR, Reg, LaneMask); |
| 1979 | report_context(*VNI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1982 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1983 | if (DefVNI != VNI) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 1984 | report("Live segment at def has different VNInfo", MF); |
| 1985 | report_context(LR, Reg, LaneMask); |
| 1986 | report_context(*VNI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1987 | return; |
| 1988 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1989 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1990 | const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def); |
| 1991 | if (!MBB) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 1992 | report("Invalid VNInfo definition index", MF); |
| 1993 | report_context(LR, Reg, LaneMask); |
| 1994 | report_context(*VNI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1995 | return; |
| 1996 | } |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1997 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 1998 | if (VNI->isPHIDef()) { |
| 1999 | if (VNI->def != LiveInts->getMBBStartIdx(MBB)) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2000 | report("PHIDef VNInfo is not defined at MBB start", MBB); |
| 2001 | report_context(LR, Reg, LaneMask); |
| 2002 | report_context(*VNI); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 2003 | } |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2004 | return; |
| 2005 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 2006 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2007 | // Non-PHI def. |
| 2008 | const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); |
| 2009 | if (!MI) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2010 | report("No instruction at VNInfo def index", MBB); |
| 2011 | report_context(LR, Reg, LaneMask); |
| 2012 | report_context(*VNI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2013 | return; |
| 2014 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 2015 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2016 | if (Reg != 0) { |
| 2017 | bool hasDef = false; |
| 2018 | bool isEarlyClobber = false; |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 2019 | for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) { |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2020 | if (!MOI->isReg() || !MOI->isDef()) |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2021 | continue; |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2022 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
| 2023 | if (MOI->getReg() != Reg) |
| 2024 | continue; |
| 2025 | } else { |
| 2026 | if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) || |
| 2027 | !TRI->hasRegUnit(MOI->getReg(), Reg)) |
| 2028 | continue; |
| 2029 | } |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 2030 | if (LaneMask.any() && |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 2031 | (TRI->getSubRegIndexLaneMask(MOI->getSubReg()) & LaneMask).none()) |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 2032 | continue; |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2033 | hasDef = true; |
| 2034 | if (MOI->isEarlyClobber()) |
| 2035 | isEarlyClobber = true; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2036 | } |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2037 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2038 | if (!hasDef) { |
| 2039 | report("Defining instruction does not modify register", MI); |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2040 | report_context(LR, Reg, LaneMask); |
| 2041 | report_context(*VNI); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2042 | } |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2043 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2044 | // Early clobber defs begin at USE slots, but other defs must begin at |
| 2045 | // DEF slots. |
| 2046 | if (isEarlyClobber) { |
| 2047 | if (!VNI->def.isEarlyClobber()) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2048 | report("Early clobber def must be at an early-clobber slot", MBB); |
| 2049 | report_context(LR, Reg, LaneMask); |
| 2050 | report_context(*VNI); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } else if (!VNI->def.isRegister()) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2053 | report("Non-PHI, non-early clobber def must be at a register slot", MBB); |
| 2054 | report_context(LR, Reg, LaneMask); |
| 2055 | report_context(*VNI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2056 | } |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2057 | } |
| 2058 | } |
| 2059 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2060 | void MachineVerifier::verifyLiveRangeSegment(const LiveRange &LR, |
| 2061 | const LiveRange::const_iterator I, |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 2062 | unsigned Reg, LaneBitmask LaneMask) |
| 2063 | { |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2064 | const LiveRange::Segment &S = *I; |
| 2065 | const VNInfo *VNI = S.valno; |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 2066 | assert(VNI && "Live segment has no valno"); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2067 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2068 | if (VNI->id >= LR.getNumValNums() || VNI != LR.getValNumInfo(VNI->id)) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2069 | report("Foreign valno in live segment", MF); |
| 2070 | report_context(LR, Reg, LaneMask); |
| 2071 | report_context(S); |
| 2072 | report_context(*VNI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | if (VNI->isUnused()) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2076 | report("Live segment valno is marked unused", MF); |
| 2077 | report_context(LR, Reg, LaneMask); |
| 2078 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2079 | } |
| 2080 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2081 | const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(S.start); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2082 | if (!MBB) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2083 | report("Bad start of live segment, no basic block", MF); |
| 2084 | report_context(LR, Reg, LaneMask); |
| 2085 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2086 | return; |
| 2087 | } |
| 2088 | SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2089 | if (S.start != MBBStartIdx && S.start != VNI->def) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2090 | report("Live segment must begin at MBB entry or valno def", MBB); |
| 2091 | report_context(LR, Reg, LaneMask); |
| 2092 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | const MachineBasicBlock *EndMBB = |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2096 | LiveInts->getMBBFromIndex(S.end.getPrevSlot()); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2097 | if (!EndMBB) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2098 | report("Bad end of live segment, no basic block", MF); |
| 2099 | report_context(LR, Reg, LaneMask); |
| 2100 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2101 | return; |
| 2102 | } |
| 2103 | |
| 2104 | // No more checks for live-out segments. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2105 | if (S.end == LiveInts->getMBBEndIdx(EndMBB)) |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2106 | return; |
| 2107 | |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 2108 | // RegUnit intervals are allowed dead phis. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2109 | if (!TargetRegisterInfo::isVirtualRegister(Reg) && VNI->isPHIDef() && |
| 2110 | S.start == VNI->def && S.end == VNI->def.getDeadSlot()) |
Jakob Stoklund Olesen | 8044689 | 2012-08-02 16:36:50 +0000 | [diff] [blame] | 2111 | return; |
| 2112 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2113 | // The live segment is ending inside EndMBB |
| 2114 | const MachineInstr *MI = |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2115 | LiveInts->getInstructionFromIndex(S.end.getPrevSlot()); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2116 | if (!MI) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2117 | report("Live segment doesn't end at a valid instruction", EndMBB); |
| 2118 | report_context(LR, Reg, LaneMask); |
| 2119 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2120 | return; |
| 2121 | } |
| 2122 | |
| 2123 | // The block slot must refer to a basic block boundary. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2124 | if (S.end.isBlock()) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2125 | report("Live segment ends at B slot of an instruction", EndMBB); |
| 2126 | report_context(LR, Reg, LaneMask); |
| 2127 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2130 | if (S.end.isDead()) { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2131 | // Segment ends on the dead slot. |
| 2132 | // That means there must be a dead def. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2133 | if (!SlotIndex::isSameInstr(S.start, S.end)) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2134 | report("Live segment ending at dead slot spans instructions", EndMBB); |
| 2135 | report_context(LR, Reg, LaneMask); |
| 2136 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | // A live segment can only end at an early-clobber slot if it is being |
| 2141 | // redefined by an early-clobber def. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2142 | if (S.end.isEarlyClobber()) { |
| 2143 | if (I+1 == LR.end() || (I+1)->start != S.end) { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2144 | report("Live segment ending at early clobber slot must be " |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2145 | "redefined by an EC def in the same instruction", EndMBB); |
| 2146 | report_context(LR, Reg, LaneMask); |
| 2147 | report_context(S); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | // The following checks only apply to virtual registers. Physreg liveness |
| 2152 | // is too weird to check. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2153 | if (TargetRegisterInfo::isVirtualRegister(Reg)) { |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 2154 | // A live segment can end with either a redefinition, a kill flag on a |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2155 | // use, or a dead flag on a def. |
| 2156 | bool hasRead = false; |
Matthias Braun | 8f08516 | 2014-12-10 01:13:11 +0000 | [diff] [blame] | 2157 | bool hasSubRegDef = false; |
Matthias Braun | 1df6732 | 2016-03-29 19:07:43 +0000 | [diff] [blame] | 2158 | bool hasDeadDef = false; |
Duncan P. N. Exon Smith | 63ec7f0 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 2159 | for (ConstMIBundleOperands MOI(*MI); MOI.isValid(); ++MOI) { |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2160 | if (!MOI->isReg() || MOI->getReg() != Reg) |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2161 | continue; |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 2162 | unsigned Sub = MOI->getSubReg(); |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 2163 | LaneBitmask SLM = Sub != 0 ? TRI->getSubRegIndexLaneMask(Sub) |
| 2164 | : LaneBitmask::getAll(); |
Matthias Braun | 1df6732 | 2016-03-29 19:07:43 +0000 | [diff] [blame] | 2165 | if (MOI->isDef()) { |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 2166 | if (Sub != 0) { |
Matthias Braun | 1df6732 | 2016-03-29 19:07:43 +0000 | [diff] [blame] | 2167 | hasSubRegDef = true; |
Francis Visoiu Mistrih | fd11bc0 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 2168 | // An operand %0:sub0 reads %0:sub1..n. Invert the lane |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 2169 | // mask for subregister defs. Read-undef defs will be handled by |
| 2170 | // readsReg below. |
Krzysztof Parzyszek | adef5a6 | 2016-08-29 13:15:35 +0000 | [diff] [blame] | 2171 | SLM = ~SLM; |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 2172 | } |
Matthias Braun | 1df6732 | 2016-03-29 19:07:43 +0000 | [diff] [blame] | 2173 | if (MOI->isDead()) |
| 2174 | hasDeadDef = true; |
| 2175 | } |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 2176 | if (LaneMask.any() && (LaneMask & SLM).none()) |
Krzysztof Parzyszek | 31a5f88 | 2016-08-24 13:37:55 +0000 | [diff] [blame] | 2177 | continue; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2178 | if (MOI->readsReg()) |
| 2179 | hasRead = true; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2180 | } |
Matthias Braun | 1df6732 | 2016-03-29 19:07:43 +0000 | [diff] [blame] | 2181 | if (S.end.isDead()) { |
| 2182 | // Make sure that the corresponding machine operand for a "dead" live |
| 2183 | // range has the dead flag. We cannot perform this check for subregister |
| 2184 | // liveranges as partially dead values are allowed. |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 2185 | if (LaneMask.none() && !hasDeadDef) { |
Matthias Braun | 1df6732 | 2016-03-29 19:07:43 +0000 | [diff] [blame] | 2186 | report("Instruction ending live segment on dead slot has no dead flag", |
| 2187 | MI); |
| 2188 | report_context(LR, Reg, LaneMask); |
| 2189 | report_context(S); |
| 2190 | } |
| 2191 | } else { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2192 | if (!hasRead) { |
Matthias Braun | 8f08516 | 2014-12-10 01:13:11 +0000 | [diff] [blame] | 2193 | // When tracking subregister liveness, the main range must start new |
| 2194 | // values on partial register writes, even if there is no read. |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 2195 | if (!MRI->shouldTrackSubRegLiveness(Reg) || LaneMask.any() || |
Matthias Braun | 5101c89 | 2015-03-19 00:21:58 +0000 | [diff] [blame] | 2196 | !hasSubRegDef) { |
Matthias Braun | 8f08516 | 2014-12-10 01:13:11 +0000 | [diff] [blame] | 2197 | report("Instruction ending live segment doesn't read the register", |
| 2198 | MI); |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2199 | report_context(LR, Reg, LaneMask); |
| 2200 | report_context(S); |
Matthias Braun | 8f08516 | 2014-12-10 01:13:11 +0000 | [diff] [blame] | 2201 | } |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | // Now check all the basic blocks in this live segment. |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 2207 | MachineFunction::const_iterator MFI = MBB->getIterator(); |
Matthias Braun | 331de11 | 2013-10-10 21:28:43 +0000 | [diff] [blame] | 2208 | // Is this live segment the beginning of a non-PHIDef VN? |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2209 | if (S.start == VNI->def && !VNI->isPHIDef()) { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2210 | // Not live-in to any blocks. |
| 2211 | if (MBB == EndMBB) |
| 2212 | return; |
| 2213 | // Skip this block. |
| 2214 | ++MFI; |
| 2215 | } |
Krzysztof Parzyszek | 5ede58d | 2018-08-16 19:13:28 +0000 | [diff] [blame] | 2216 | |
| 2217 | SmallVector<SlotIndex, 4> Undefs; |
| 2218 | if (LaneMask.any()) { |
| 2219 | LiveInterval &OwnerLI = LiveInts->getInterval(Reg); |
| 2220 | OwnerLI.computeSubRangeUndefs(Undefs, LaneMask, *MRI, *Indexes); |
| 2221 | } |
| 2222 | |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2223 | while (true) { |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 2224 | assert(LiveInts->isLiveInToMBB(LR, &*MFI)); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2225 | // We don't know how to track physregs into a landing pad. |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2226 | if (!TargetRegisterInfo::isVirtualRegister(Reg) && |
Reid Kleckner | c0e64ad | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 2227 | MFI->isEHPad()) { |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2228 | if (&*MFI == EndMBB) |
| 2229 | break; |
| 2230 | ++MFI; |
| 2231 | continue; |
| 2232 | } |
| 2233 | |
| 2234 | // Is VNI a PHI-def in the current block? |
| 2235 | bool IsPHI = VNI->isPHIDef() && |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 2236 | VNI->def == LiveInts->getMBBStartIdx(&*MFI); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2237 | |
| 2238 | // Check that VNI is live-out of all predecessors. |
| 2239 | for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(), |
| 2240 | PE = MFI->pred_end(); PI != PE; ++PI) { |
| 2241 | SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2242 | const VNInfo *PVNI = LR.getVNInfoBefore(PEnd); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2243 | |
Matthias Braun | 9b4cf76 | 2017-06-08 21:30:54 +0000 | [diff] [blame] | 2244 | // All predecessors must have a live-out value. However for a phi |
| 2245 | // instruction with subregister intervals |
| 2246 | // only one of the subregisters (not necessarily the current one) needs to |
| 2247 | // be defined. |
Krzysztof Parzyszek | 5ede58d | 2018-08-16 19:13:28 +0000 | [diff] [blame] | 2248 | if (!PVNI && (LaneMask.none() || !IsPHI)) { |
| 2249 | if (LiveRangeCalc::isJointlyDominated(*PI, Undefs, *Indexes)) |
| 2250 | continue; |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2251 | report("Register not marked live out of predecessor", *PI); |
| 2252 | report_context(LR, Reg, LaneMask); |
| 2253 | report_context(*VNI); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 2254 | errs() << " live into " << printMBBReference(*MFI) << '@' |
| 2255 | << LiveInts->getMBBStartIdx(&*MFI) << ", not live before " |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 2256 | << PEnd << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2257 | continue; |
| 2258 | } |
| 2259 | |
| 2260 | // Only PHI-defs can take different predecessor values. |
| 2261 | if (!IsPHI && PVNI != VNI) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2262 | report("Different value live out of predecessor", *PI); |
| 2263 | report_context(LR, Reg, LaneMask); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 2264 | errs() << "Valno #" << PVNI->id << " live out of " |
| 2265 | << printMBBReference(*(*PI)) << '@' << PEnd << "\nValno #" |
| 2266 | << VNI->id << " live into " << printMBBReference(*MFI) << '@' |
Duncan P. N. Exon Smith | 9731c60 | 2015-10-09 19:40:45 +0000 | [diff] [blame] | 2267 | << LiveInts->getMBBStartIdx(&*MFI) << '\n'; |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2268 | } |
| 2269 | } |
| 2270 | if (&*MFI == EndMBB) |
| 2271 | break; |
| 2272 | ++MFI; |
| 2273 | } |
| 2274 | } |
| 2275 | |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 2276 | void MachineVerifier::verifyLiveRange(const LiveRange &LR, unsigned Reg, |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 2277 | LaneBitmask LaneMask) { |
Matthias Braun | 218d20a | 2014-12-10 23:07:54 +0000 | [diff] [blame] | 2278 | for (const VNInfo *VNI : LR.valnos) |
| 2279 | verifyLiveRangeValue(LR, VNI, Reg, LaneMask); |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2280 | |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2281 | for (LiveRange::const_iterator I = LR.begin(), E = LR.end(); I != E; ++I) |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 2282 | verifyLiveRangeSegment(LR, I, Reg, LaneMask); |
Matthias Braun | a4aed9a | 2013-10-10 21:28:54 +0000 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | void MachineVerifier::verifyLiveInterval(const LiveInterval &LI) { |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 2286 | unsigned Reg = LI.reg; |
Matthias Braun | dffffd2 | 2015-03-25 21:18:22 +0000 | [diff] [blame] | 2287 | assert(TargetRegisterInfo::isVirtualRegister(Reg)); |
| 2288 | verifyLiveRange(LI, Reg); |
| 2289 | |
Krzysztof Parzyszek | d6ca3f0 | 2016-12-15 14:36:06 +0000 | [diff] [blame] | 2290 | LaneBitmask Mask; |
Matthias Braun | dfc5b65 | 2015-09-25 21:51:14 +0000 | [diff] [blame] | 2291 | LaneBitmask MaxMask = MRI->getMaxLaneMaskForVReg(Reg); |
Matthias Braun | dffffd2 | 2015-03-25 21:18:22 +0000 | [diff] [blame] | 2292 | for (const LiveInterval::SubRange &SR : LI.subranges()) { |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 2293 | if ((Mask & SR.LaneMask).any()) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2294 | report("Lane masks of sub ranges overlap in live interval", MF); |
| 2295 | report_context(LI); |
| 2296 | } |
Krzysztof Parzyszek | 308c60d | 2016-12-16 19:11:56 +0000 | [diff] [blame] | 2297 | if ((SR.LaneMask & ~MaxMask).any()) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2298 | report("Subrange lanemask is invalid", MF); |
| 2299 | report_context(LI); |
| 2300 | } |
| 2301 | if (SR.empty()) { |
| 2302 | report("Subrange must not be empty", MF); |
| 2303 | report_context(SR, LI.reg, SR.LaneMask); |
| 2304 | } |
Matthias Braun | dffffd2 | 2015-03-25 21:18:22 +0000 | [diff] [blame] | 2305 | Mask |= SR.LaneMask; |
| 2306 | verifyLiveRange(SR, LI.reg, SR.LaneMask); |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2307 | if (!LI.covers(SR)) { |
| 2308 | report("A Subrange is not covered by the main range", MF); |
| 2309 | report_context(LI); |
| 2310 | } |
Matthias Braun | 01ddf04 | 2014-12-10 01:12:10 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Jakob Stoklund Olesen | e5c79a5 | 2012-08-02 00:20:20 +0000 | [diff] [blame] | 2313 | // Check the LI only has one connected component. |
Matthias Braun | dffffd2 | 2015-03-25 21:18:22 +0000 | [diff] [blame] | 2314 | ConnectedVNInfoEqClasses ConEQ(*LiveInts); |
Matthias Braun | dcaeedf | 2016-01-08 01:16:35 +0000 | [diff] [blame] | 2315 | unsigned NumComp = ConEQ.Classify(LI); |
Matthias Braun | dffffd2 | 2015-03-25 21:18:22 +0000 | [diff] [blame] | 2316 | if (NumComp > 1) { |
Matthias Braun | ee21e9f | 2015-11-09 23:59:33 +0000 | [diff] [blame] | 2317 | report("Multiple connected components in live interval", MF); |
| 2318 | report_context(LI); |
Matthias Braun | dffffd2 | 2015-03-25 21:18:22 +0000 | [diff] [blame] | 2319 | for (unsigned comp = 0; comp != NumComp; ++comp) { |
| 2320 | errs() << comp << ": valnos"; |
| 2321 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), |
| 2322 | E = LI.vni_end(); I!=E; ++I) |
| 2323 | if (comp == ConEQ.getEqClass(*I)) |
| 2324 | errs() << ' ' << (*I)->id; |
| 2325 | errs() << '\n'; |
Jakob Stoklund Olesen | 501dc42 | 2010-10-26 22:36:07 +0000 | [diff] [blame] | 2326 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 2327 | } |
| 2328 | } |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2329 | |
| 2330 | namespace { |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2331 | |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2332 | // FrameSetup and FrameDestroy can have zero adjustment, so using a single |
| 2333 | // integer, we can't tell whether it is a FrameSetup or FrameDestroy if the |
| 2334 | // value is zero. |
| 2335 | // We use a bool plus an integer to capture the stack state. |
| 2336 | struct StackStateOfBB { |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2337 | StackStateOfBB() = default; |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2338 | StackStateOfBB(int EntryVal, int ExitVal, bool EntrySetup, bool ExitSetup) : |
| 2339 | EntryValue(EntryVal), ExitValue(ExitVal), EntryIsSetup(EntrySetup), |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2340 | ExitIsSetup(ExitSetup) {} |
| 2341 | |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2342 | // Can be negative, which means we are setting up a frame. |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2343 | int EntryValue = 0; |
| 2344 | int ExitValue = 0; |
| 2345 | bool EntryIsSetup = false; |
| 2346 | bool ExitIsSetup = false; |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2347 | }; |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2348 | |
| 2349 | } // end anonymous namespace |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2350 | |
| 2351 | /// Make sure on every path through the CFG, a FrameSetup <n> is always followed |
| 2352 | /// by a FrameDestroy <n>, stack adjustments are identical on all |
| 2353 | /// CFG edges to a merge point, and frame is destroyed at end of a return block. |
| 2354 | void MachineVerifier::verifyStackFrame() { |
Matthias Braun | e4603f0 | 2015-05-18 20:27:55 +0000 | [diff] [blame] | 2355 | unsigned FrameSetupOpcode = TII->getCallFrameSetupOpcode(); |
| 2356 | unsigned FrameDestroyOpcode = TII->getCallFrameDestroyOpcode(); |
Serge Pavlov | ff09ea4 | 2017-04-20 01:34:04 +0000 | [diff] [blame] | 2357 | if (FrameSetupOpcode == ~0u && FrameDestroyOpcode == ~0u) |
| 2358 | return; |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2359 | |
| 2360 | SmallVector<StackStateOfBB, 8> SPState; |
| 2361 | SPState.resize(MF->getNumBlockIDs()); |
David Callahan | 8be61a8 | 2016-10-05 21:36:16 +0000 | [diff] [blame] | 2362 | df_iterator_default_set<const MachineBasicBlock*> Reachable; |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2363 | |
| 2364 | // Visit the MBBs in DFS order. |
Eugene Zelenko | 8fd0504 | 2017-09-11 23:00:48 +0000 | [diff] [blame] | 2365 | for (df_ext_iterator<const MachineFunction *, |
| 2366 | df_iterator_default_set<const MachineBasicBlock *>> |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2367 | DFI = df_ext_begin(MF, Reachable), DFE = df_ext_end(MF, Reachable); |
| 2368 | DFI != DFE; ++DFI) { |
| 2369 | const MachineBasicBlock *MBB = *DFI; |
| 2370 | |
| 2371 | StackStateOfBB BBState; |
| 2372 | // Check the exit state of the DFS stack predecessor. |
| 2373 | if (DFI.getPathLength() >= 2) { |
| 2374 | const MachineBasicBlock *StackPred = DFI.getPath(DFI.getPathLength() - 2); |
| 2375 | assert(Reachable.count(StackPred) && |
| 2376 | "DFS stack predecessor is already visited.\n"); |
| 2377 | BBState.EntryValue = SPState[StackPred->getNumber()].ExitValue; |
| 2378 | BBState.EntryIsSetup = SPState[StackPred->getNumber()].ExitIsSetup; |
| 2379 | BBState.ExitValue = BBState.EntryValue; |
| 2380 | BBState.ExitIsSetup = BBState.EntryIsSetup; |
| 2381 | } |
| 2382 | |
| 2383 | // Update stack state by checking contents of MBB. |
Alexey Samsonov | 8467812 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 2384 | for (const auto &I : *MBB) { |
| 2385 | if (I.getOpcode() == FrameSetupOpcode) { |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2386 | if (BBState.ExitIsSetup) |
Alexey Samsonov | 8467812 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 2387 | report("FrameSetup is after another FrameSetup", &I); |
Serge Pavlov | 1f4a80f | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 2388 | BBState.ExitValue -= TII->getFrameTotalSize(I); |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2389 | BBState.ExitIsSetup = true; |
| 2390 | } |
| 2391 | |
Alexey Samsonov | 8467812 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 2392 | if (I.getOpcode() == FrameDestroyOpcode) { |
Serge Pavlov | 1f4a80f | 2017-05-09 13:35:13 +0000 | [diff] [blame] | 2393 | int Size = TII->getFrameTotalSize(I); |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2394 | if (!BBState.ExitIsSetup) |
Alexey Samsonov | 8467812 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 2395 | report("FrameDestroy is not after a FrameSetup", &I); |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2396 | int AbsSPAdj = BBState.ExitValue < 0 ? -BBState.ExitValue : |
| 2397 | BBState.ExitValue; |
| 2398 | if (BBState.ExitIsSetup && AbsSPAdj != Size) { |
Alexey Samsonov | 8467812 | 2014-04-30 22:17:38 +0000 | [diff] [blame] | 2399 | report("FrameDestroy <n> is after FrameSetup <m>", &I); |
Owen Anderson | a657cab | 2015-02-04 00:02:59 +0000 | [diff] [blame] | 2400 | errs() << "FrameDestroy <" << Size << "> is after FrameSetup <" |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2401 | << AbsSPAdj << ">.\n"; |
| 2402 | } |
| 2403 | BBState.ExitValue += Size; |
| 2404 | BBState.ExitIsSetup = false; |
| 2405 | } |
| 2406 | } |
| 2407 | SPState[MBB->getNumber()] = BBState; |
| 2408 | |
| 2409 | // Make sure the exit state of any predecessor is consistent with the entry |
| 2410 | // state. |
| 2411 | for (MachineBasicBlock::const_pred_iterator I = MBB->pred_begin(), |
| 2412 | E = MBB->pred_end(); I != E; ++I) { |
| 2413 | if (Reachable.count(*I) && |
| 2414 | (SPState[(*I)->getNumber()].ExitValue != BBState.EntryValue || |
| 2415 | SPState[(*I)->getNumber()].ExitIsSetup != BBState.EntryIsSetup)) { |
| 2416 | report("The exit stack state of a predecessor is inconsistent.", MBB); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 2417 | errs() << "Predecessor " << printMBBReference(*(*I)) |
| 2418 | << " has exit state (" << SPState[(*I)->getNumber()].ExitValue |
| 2419 | << ", " << SPState[(*I)->getNumber()].ExitIsSetup << "), while " |
| 2420 | << printMBBReference(*MBB) << " has entry state (" |
| 2421 | << BBState.EntryValue << ", " << BBState.EntryIsSetup << ").\n"; |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | // Make sure the entry state of any successor is consistent with the exit |
| 2426 | // state. |
| 2427 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
| 2428 | E = MBB->succ_end(); I != E; ++I) { |
| 2429 | if (Reachable.count(*I) && |
| 2430 | (SPState[(*I)->getNumber()].EntryValue != BBState.ExitValue || |
| 2431 | SPState[(*I)->getNumber()].EntryIsSetup != BBState.ExitIsSetup)) { |
| 2432 | report("The entry stack state of a successor is inconsistent.", MBB); |
Francis Visoiu Mistrih | ca0df55 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 2433 | errs() << "Successor " << printMBBReference(*(*I)) |
| 2434 | << " has entry state (" << SPState[(*I)->getNumber()].EntryValue |
| 2435 | << ", " << SPState[(*I)->getNumber()].EntryIsSetup << "), while " |
| 2436 | << printMBBReference(*MBB) << " has exit state (" |
| 2437 | << BBState.ExitValue << ", " << BBState.ExitIsSetup << ").\n"; |
Manman Ren | 7310b75 | 2013-07-15 21:26:31 +0000 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | // Make sure a basic block with return ends with zero stack adjustment. |
| 2442 | if (!MBB->empty() && MBB->back().isReturn()) { |
| 2443 | if (BBState.ExitIsSetup) |
| 2444 | report("A return block ends with a FrameSetup.", MBB); |
| 2445 | if (BBState.ExitValue) |
| 2446 | report("A return block ends with a nonzero stack adjustment.", MBB); |
| 2447 | } |
| 2448 | } |
| 2449 | } |