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