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