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