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