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