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