Bill Wendling | 5567bb0 | 2010-08-19 18:52:17 +0000 | [diff] [blame] | 1 | //===-- MachineVerifier.cpp - Machine Code Verifier -----------------------===// |
Jakob Stoklund Olesen | 48872e0 | 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 | |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 26 | #include "llvm/Instructions.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 27 | #include "llvm/Function.h" |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/LiveVariables.h" |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/LiveStackAnalysis.h" |
Jakob Stoklund Olesen | 30e98a0 | 2012-02-29 00:33:41 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineInstrBundle.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineMemOperand.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 36 | #include "llvm/CodeGen/Passes.h" |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCAsmInfo.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetMachine.h" |
| 39 | #include "llvm/Target/TargetRegisterInfo.h" |
| 40 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | cf143a4 | 2009-08-23 03:13:20 +0000 | [diff] [blame] | 41 | #include "llvm/ADT/DenseSet.h" |
| 42 | #include "llvm/ADT/SetOperations.h" |
| 43 | #include "llvm/ADT/SmallVector.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 44 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 45 | #include "llvm/Support/ErrorHandling.h" |
| 46 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 47 | using namespace llvm; |
| 48 | |
| 49 | namespace { |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 50 | struct MachineVerifier { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 51 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 52 | MachineVerifier(Pass *pass, const char *b) : |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 53 | PASS(pass), |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 54 | Banner(b), |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 55 | OutFileName(getenv("LLVM_VERIFY_MACHINEINSTRS")) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 56 | {} |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 57 | |
| 58 | bool runOnMachineFunction(MachineFunction &MF); |
| 59 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 60 | Pass *const PASS; |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 61 | const char *Banner; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 62 | const char *const OutFileName; |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 63 | raw_ostream *OS; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 64 | const MachineFunction *MF; |
| 65 | const TargetMachine *TM; |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 66 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 67 | const TargetRegisterInfo *TRI; |
| 68 | const MachineRegisterInfo *MRI; |
| 69 | |
| 70 | unsigned foundErrors; |
| 71 | |
| 72 | typedef SmallVector<unsigned, 16> RegVector; |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 73 | typedef SmallVector<const uint32_t*, 4> RegMaskVector; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 74 | typedef DenseSet<unsigned> RegSet; |
| 75 | typedef DenseMap<unsigned, const MachineInstr*> RegMap; |
| 76 | |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 77 | const MachineInstr *FirstTerminator; |
| 78 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 79 | BitVector regsReserved; |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 80 | BitVector regsAllocatable; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 81 | RegSet regsLive; |
Jakob Stoklund Olesen | 710b13b | 2009-08-08 13:19:25 +0000 | [diff] [blame] | 82 | RegVector regsDefined, regsDead, regsKilled; |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 83 | RegMaskVector regMasks; |
Jakob Stoklund Olesen | 710b13b | 2009-08-08 13:19:25 +0000 | [diff] [blame] | 84 | RegSet regsLiveInButUnused; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 85 | |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 86 | SlotIndex lastIndex; |
| 87 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 88 | // Add Reg and any sub-registers to RV |
| 89 | void addRegWithSubRegs(RegVector &RV, unsigned Reg) { |
| 90 | RV.push_back(Reg); |
| 91 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 92 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) |
| 93 | RV.push_back(*SubRegs); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 96 | struct BBInfo { |
| 97 | // Is this MBB reachable from the MF entry point? |
| 98 | bool reachable; |
| 99 | |
| 100 | // Vregs that must be live in because they are used without being |
| 101 | // defined. Map value is the user. |
| 102 | RegMap vregsLiveIn; |
| 103 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 104 | // Regs killed in MBB. They may be defined again, and will then be in both |
| 105 | // regsKilled and regsLiveOut. |
| 106 | RegSet regsKilled; |
| 107 | |
| 108 | // Regs defined in MBB and live out. Note that vregs passing through may |
| 109 | // be live out without being mentioned here. |
| 110 | RegSet regsLiveOut; |
| 111 | |
| 112 | // Vregs that pass through MBB untouched. This set is disjoint from |
| 113 | // regsKilled and regsLiveOut. |
| 114 | RegSet vregsPassed; |
| 115 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 116 | // Vregs that must pass through MBB because they are needed by a successor |
| 117 | // block. This set is disjoint from regsLiveOut. |
| 118 | RegSet vregsRequired; |
| 119 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 120 | BBInfo() : reachable(false) {} |
| 121 | |
| 122 | // Add register to vregsPassed if it belongs there. Return true if |
| 123 | // anything changed. |
| 124 | bool addPassed(unsigned Reg) { |
| 125 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 126 | return false; |
| 127 | if (regsKilled.count(Reg) || regsLiveOut.count(Reg)) |
| 128 | return false; |
| 129 | return vregsPassed.insert(Reg).second; |
| 130 | } |
| 131 | |
| 132 | // Same for a full set. |
| 133 | bool addPassed(const RegSet &RS) { |
| 134 | bool changed = false; |
| 135 | for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) |
| 136 | if (addPassed(*I)) |
| 137 | changed = true; |
| 138 | return changed; |
| 139 | } |
| 140 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 141 | // Add register to vregsRequired if it belongs there. Return true if |
| 142 | // anything changed. |
| 143 | bool addRequired(unsigned Reg) { |
| 144 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) |
| 145 | return false; |
| 146 | if (regsLiveOut.count(Reg)) |
| 147 | return false; |
| 148 | return vregsRequired.insert(Reg).second; |
| 149 | } |
| 150 | |
| 151 | // Same for a full set. |
| 152 | bool addRequired(const RegSet &RS) { |
| 153 | bool changed = false; |
| 154 | for (RegSet::const_iterator I = RS.begin(), E = RS.end(); I != E; ++I) |
| 155 | if (addRequired(*I)) |
| 156 | changed = true; |
| 157 | return changed; |
| 158 | } |
| 159 | |
| 160 | // Same for a full map. |
| 161 | bool addRequired(const RegMap &RM) { |
| 162 | bool changed = false; |
| 163 | for (RegMap::const_iterator I = RM.begin(), E = RM.end(); I != E; ++I) |
| 164 | if (addRequired(I->first)) |
| 165 | changed = true; |
| 166 | return changed; |
| 167 | } |
| 168 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 169 | // Live-out registers are either in regsLiveOut or vregsPassed. |
| 170 | bool isLiveOut(unsigned Reg) const { |
| 171 | return regsLiveOut.count(Reg) || vregsPassed.count(Reg); |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | // Extra register info per MBB. |
| 176 | DenseMap<const MachineBasicBlock*, BBInfo> MBBInfoMap; |
| 177 | |
| 178 | bool isReserved(unsigned Reg) { |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 179 | return Reg < regsReserved.size() && regsReserved.test(Reg); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 182 | bool isAllocatable(unsigned Reg) { |
| 183 | return Reg < regsAllocatable.size() && regsAllocatable.test(Reg); |
| 184 | } |
| 185 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 186 | // Analysis information if available |
| 187 | LiveVariables *LiveVars; |
Jakob Stoklund Olesen | 501dc42 | 2010-10-26 22:36:07 +0000 | [diff] [blame] | 188 | LiveIntervals *LiveInts; |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 189 | LiveStacks *LiveStks; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 190 | SlotIndexes *Indexes; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 191 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 192 | void visitMachineFunctionBefore(); |
| 193 | void visitMachineBasicBlockBefore(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 194 | void visitMachineBundleBefore(const MachineInstr *MI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 195 | void visitMachineInstrBefore(const MachineInstr *MI); |
| 196 | void visitMachineOperand(const MachineOperand *MO, unsigned MONum); |
| 197 | void visitMachineInstrAfter(const MachineInstr *MI); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 198 | void visitMachineBundleAfter(const MachineInstr *MI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 199 | void visitMachineBasicBlockAfter(const MachineBasicBlock *MBB); |
| 200 | void visitMachineFunctionAfter(); |
| 201 | |
| 202 | void report(const char *msg, const MachineFunction *MF); |
| 203 | void report(const char *msg, const MachineBasicBlock *MBB); |
| 204 | void report(const char *msg, const MachineInstr *MI); |
| 205 | void report(const char *msg, const MachineOperand *MO, unsigned MONum); |
| 206 | |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 207 | void checkLiveness(const MachineOperand *MO, unsigned MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 208 | void markReachable(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 209 | void calcRegsPassed(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 210 | void checkPHIOps(const MachineBasicBlock *MBB); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 211 | |
| 212 | void calcRegsRequired(); |
| 213 | void verifyLiveVariables(); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 214 | void verifyLiveIntervals(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 215 | }; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 216 | |
| 217 | struct MachineVerifierPass : public MachineFunctionPass { |
| 218 | static char ID; // Pass ID, replacement for typeid |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 219 | const char *const Banner; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 220 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 221 | MachineVerifierPass(const char *b = 0) |
| 222 | : MachineFunctionPass(ID), Banner(b) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 223 | initializeMachineVerifierPassPass(*PassRegistry::getPassRegistry()); |
| 224 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 225 | |
| 226 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 227 | AU.setPreservesAll(); |
| 228 | MachineFunctionPass::getAnalysisUsage(AU); |
| 229 | } |
| 230 | |
| 231 | bool runOnMachineFunction(MachineFunction &MF) { |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 232 | MF.verify(this, Banner); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | }; |
| 236 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 239 | char MachineVerifierPass::ID = 0; |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 240 | INITIALIZE_PASS(MachineVerifierPass, "machineverifier", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 241 | "Verify generated machine code", false, false) |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 242 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 243 | FunctionPass *llvm::createMachineVerifierPass(const char *Banner) { |
| 244 | return new MachineVerifierPass(Banner); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 247 | void MachineFunction::verify(Pass *p, const char *Banner) const { |
| 248 | MachineVerifier(p, Banner) |
| 249 | .runOnMachineFunction(const_cast<MachineFunction&>(*this)); |
Jakob Stoklund Olesen | ce727d0 | 2009-11-13 21:56:09 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 252 | bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) { |
| 253 | raw_ostream *OutFile = 0; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 254 | if (OutFileName) { |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 255 | std::string ErrorInfo; |
| 256 | OutFile = new raw_fd_ostream(OutFileName, ErrorInfo, |
| 257 | raw_fd_ostream::F_Append); |
| 258 | if (!ErrorInfo.empty()) { |
| 259 | errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n'; |
| 260 | exit(1); |
| 261 | } |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 263 | OS = OutFile; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 264 | } else { |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 265 | OS = &errs(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | foundErrors = 0; |
| 269 | |
| 270 | this->MF = &MF; |
| 271 | TM = &MF.getTarget(); |
Evan Cheng | 15993f8 | 2011-06-27 21:26:13 +0000 | [diff] [blame] | 272 | TII = TM->getInstrInfo(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 273 | TRI = TM->getRegisterInfo(); |
| 274 | MRI = &MF.getRegInfo(); |
| 275 | |
Jakob Stoklund Olesen | c910c8d | 2010-08-05 23:51:26 +0000 | [diff] [blame] | 276 | LiveVars = NULL; |
| 277 | LiveInts = NULL; |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 278 | LiveStks = NULL; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 279 | Indexes = NULL; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 280 | if (PASS) { |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 281 | LiveInts = PASS->getAnalysisIfAvailable<LiveIntervals>(); |
Jakob Stoklund Olesen | c910c8d | 2010-08-05 23:51:26 +0000 | [diff] [blame] | 282 | // We don't want to verify LiveVariables if LiveIntervals is available. |
| 283 | if (!LiveInts) |
| 284 | LiveVars = PASS->getAnalysisIfAvailable<LiveVariables>(); |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 285 | LiveStks = PASS->getAnalysisIfAvailable<LiveStacks>(); |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 286 | Indexes = PASS->getAnalysisIfAvailable<SlotIndexes>(); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 289 | visitMachineFunctionBefore(); |
| 290 | for (MachineFunction::const_iterator MFI = MF.begin(), MFE = MF.end(); |
| 291 | MFI!=MFE; ++MFI) { |
| 292 | visitMachineBasicBlockBefore(MFI); |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 293 | // Keep track of the current bundle header. |
| 294 | const MachineInstr *CurBundle = 0; |
Evan Cheng | ddfd137 | 2011-12-14 02:11:42 +0000 | [diff] [blame] | 295 | for (MachineBasicBlock::const_instr_iterator MBBI = MFI->instr_begin(), |
| 296 | MBBE = MFI->instr_end(); MBBI != MBBE; ++MBBI) { |
Jakob Stoklund Olesen | 7bd46da | 2011-01-12 21:27:41 +0000 | [diff] [blame] | 297 | if (MBBI->getParent() != MFI) { |
| 298 | report("Bad instruction parent pointer", MFI); |
| 299 | *OS << "Instruction: " << *MBBI; |
| 300 | continue; |
| 301 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 302 | // Is this a bundle header? |
| 303 | if (!MBBI->isInsideBundle()) { |
| 304 | if (CurBundle) |
| 305 | visitMachineBundleAfter(CurBundle); |
| 306 | CurBundle = MBBI; |
| 307 | visitMachineBundleBefore(CurBundle); |
| 308 | } else if (!CurBundle) |
| 309 | report("No bundle header", MBBI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 310 | visitMachineInstrBefore(MBBI); |
| 311 | for (unsigned I = 0, E = MBBI->getNumOperands(); I != E; ++I) |
| 312 | visitMachineOperand(&MBBI->getOperand(I), I); |
| 313 | visitMachineInstrAfter(MBBI); |
| 314 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 315 | if (CurBundle) |
| 316 | visitMachineBundleAfter(CurBundle); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 317 | visitMachineBasicBlockAfter(MFI); |
| 318 | } |
| 319 | visitMachineFunctionAfter(); |
| 320 | |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 321 | if (OutFile) |
| 322 | delete OutFile; |
| 323 | else if (foundErrors) |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 324 | report_fatal_error("Found "+Twine(foundErrors)+" machine code errors."); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 325 | |
Jakob Stoklund Olesen | 6349668 | 2009-08-08 15:34:50 +0000 | [diff] [blame] | 326 | // Clean up. |
| 327 | regsLive.clear(); |
| 328 | regsDefined.clear(); |
| 329 | regsDead.clear(); |
| 330 | regsKilled.clear(); |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 331 | regMasks.clear(); |
Jakob Stoklund Olesen | 6349668 | 2009-08-08 15:34:50 +0000 | [diff] [blame] | 332 | regsLiveInButUnused.clear(); |
| 333 | MBBInfoMap.clear(); |
| 334 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 335 | return false; // no changes |
| 336 | } |
| 337 | |
Chris Lattner | 372fefe | 2009-08-23 01:03:30 +0000 | [diff] [blame] | 338 | void MachineVerifier::report(const char *msg, const MachineFunction *MF) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 339 | assert(MF); |
Chris Lattner | 17e9edc | 2009-08-23 02:51:22 +0000 | [diff] [blame] | 340 | *OS << '\n'; |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 341 | if (!foundErrors++) { |
| 342 | if (Banner) |
| 343 | *OS << "# " << Banner << '\n'; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 344 | MF->print(*OS, Indexes); |
Jakob Stoklund Olesen | 89cab93 | 2010-12-18 00:06:56 +0000 | [diff] [blame] | 345 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 346 | *OS << "*** Bad machine code: " << msg << " ***\n" |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 347 | << "- function: " << MF->getFunction()->getName() << "\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 350 | void MachineVerifier::report(const char *msg, const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 351 | assert(MBB); |
| 352 | report(msg, MBB->getParent()); |
Jakob Stoklund Olesen | 324da76 | 2009-11-20 01:17:03 +0000 | [diff] [blame] | 353 | *OS << "- basic block: " << MBB->getName() |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 354 | << " " << (void*)MBB |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 355 | << " (BB#" << MBB->getNumber() << ")"; |
| 356 | if (Indexes) |
| 357 | *OS << " [" << Indexes->getMBBStartIdx(MBB) |
| 358 | << ';' << Indexes->getMBBEndIdx(MBB) << ')'; |
| 359 | *OS << '\n'; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 362 | void MachineVerifier::report(const char *msg, const MachineInstr *MI) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 363 | assert(MI); |
| 364 | report(msg, MI->getParent()); |
| 365 | *OS << "- instruction: "; |
Jakob Stoklund Olesen | f4a1e1a | 2010-10-26 20:21:46 +0000 | [diff] [blame] | 366 | if (Indexes && Indexes->hasIndex(MI)) |
| 367 | *OS << Indexes->getInstructionIndex(MI) << '\t'; |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 368 | MI->print(*OS, TM); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 371 | void MachineVerifier::report(const char *msg, |
| 372 | const MachineOperand *MO, unsigned MONum) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 373 | assert(MO); |
| 374 | report(msg, MO->getParent()); |
| 375 | *OS << "- operand " << MONum << ": "; |
| 376 | MO->print(*OS, TM); |
| 377 | *OS << "\n"; |
| 378 | } |
| 379 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 380 | void MachineVerifier::markReachable(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 381 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 382 | if (!MInfo.reachable) { |
| 383 | MInfo.reachable = true; |
| 384 | for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), |
| 385 | SuE = MBB->succ_end(); SuI != SuE; ++SuI) |
| 386 | markReachable(*SuI); |
| 387 | } |
| 388 | } |
| 389 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 390 | void MachineVerifier::visitMachineFunctionBefore() { |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 391 | lastIndex = SlotIndex(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 392 | regsReserved = TRI->getReservedRegs(*MF); |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 393 | |
| 394 | // A sub-register of a reserved register is also reserved |
| 395 | for (int Reg = regsReserved.find_first(); Reg>=0; |
| 396 | Reg = regsReserved.find_next(Reg)) { |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 397 | for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs) { |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 398 | // FIXME: This should probably be: |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 399 | // assert(regsReserved.test(*SubRegs) && "Non-reserved sub-register"); |
| 400 | regsReserved.set(*SubRegs); |
Jakob Stoklund Olesen | d37bc5a | 2009-08-04 19:18:01 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 403 | |
| 404 | regsAllocatable = TRI->getAllocatableSet(*MF); |
| 405 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 406 | markReachable(&MF->front()); |
| 407 | } |
| 408 | |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 409 | // Does iterator point to a and b as the first two elements? |
Dan Gohman | b357983 | 2010-04-15 17:08:50 +0000 | [diff] [blame] | 410 | static bool matchPair(MachineBasicBlock::const_succ_iterator i, |
| 411 | const MachineBasicBlock *a, const MachineBasicBlock *b) { |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 412 | if (*i == a) |
| 413 | return *++i == b; |
| 414 | if (*i == b) |
| 415 | return *++i == a; |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | void |
| 420 | MachineVerifier::visitMachineBasicBlockBefore(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 5adc07e | 2011-09-23 22:45:39 +0000 | [diff] [blame] | 421 | FirstTerminator = 0; |
| 422 | |
Lang Hames | 03698de | 2012-02-14 19:17:48 +0000 | [diff] [blame] | 423 | if (MRI->isSSA()) { |
| 424 | // If this block has allocatable physical registers live-in, check that |
| 425 | // it is an entry block or landing pad. |
| 426 | for (MachineBasicBlock::livein_iterator LI = MBB->livein_begin(), |
| 427 | LE = MBB->livein_end(); |
| 428 | LI != LE; ++LI) { |
| 429 | unsigned reg = *LI; |
| 430 | if (isAllocatable(reg) && !MBB->isLandingPad() && |
| 431 | MBB != MBB->getParent()->begin()) { |
| 432 | report("MBB has allocable live-in, but isn't entry or landing-pad.", MBB); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 437 | // Count the number of landing pad successors. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 438 | SmallPtrSet<MachineBasicBlock*, 4> LandingPadSuccs; |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 439 | for (MachineBasicBlock::const_succ_iterator I = MBB->succ_begin(), |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 440 | E = MBB->succ_end(); I != E; ++I) { |
| 441 | if ((*I)->isLandingPad()) |
| 442 | LandingPadSuccs.insert(*I); |
| 443 | } |
Bill Wendling | d29052b | 2011-05-04 22:54:05 +0000 | [diff] [blame] | 444 | |
| 445 | const MCAsmInfo *AsmInfo = TM->getMCAsmInfo(); |
| 446 | const BasicBlock *BB = MBB->getBasicBlock(); |
| 447 | if (LandingPadSuccs.size() > 1 && |
| 448 | !(AsmInfo && |
| 449 | AsmInfo->getExceptionHandlingType() == ExceptionHandling::SjLj && |
| 450 | BB && isa<SwitchInst>(BB->getTerminator()))) |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 451 | report("MBB has more than one landing pad successor", MBB); |
| 452 | |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 453 | // Call AnalyzeBranch. If it succeeds, there several more conditions to check. |
| 454 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 455 | SmallVector<MachineOperand, 4> Cond; |
| 456 | if (!TII->AnalyzeBranch(*const_cast<MachineBasicBlock *>(MBB), |
| 457 | TBB, FBB, Cond)) { |
| 458 | // Ok, AnalyzeBranch thinks it knows what's going on with this block. Let's |
| 459 | // check whether its answers match up with reality. |
| 460 | if (!TBB && !FBB) { |
| 461 | // Block falls through to its successor. |
| 462 | MachineFunction::const_iterator MBBI = MBB; |
| 463 | ++MBBI; |
| 464 | if (MBBI == MF->end()) { |
Dan Gohman | a01a80f | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 465 | // It's possible that the block legitimately ends with a noreturn |
| 466 | // call or an unreachable, in which case it won't actually fall |
| 467 | // out the bottom of the function. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 468 | } else if (MBB->succ_size() == LandingPadSuccs.size()) { |
Dan Gohman | a01a80f | 2009-08-27 18:14:26 +0000 | [diff] [blame] | 469 | // It's possible that the block legitimately ends with a noreturn |
| 470 | // call or an unreachable, in which case it won't actuall fall |
| 471 | // out of the block. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 472 | } else if (MBB->succ_size() != 1+LandingPadSuccs.size()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 473 | report("MBB exits via unconditional fall-through but doesn't have " |
| 474 | "exactly one CFG successor!", MBB); |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 475 | } else if (!MBB->isSuccessor(MBBI)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 476 | report("MBB exits via unconditional fall-through but its successor " |
| 477 | "differs from its CFG successor!", MBB); |
| 478 | } |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 479 | if (!MBB->empty() && getBundleStart(&MBB->back())->isBarrier() && |
| 480 | !TII->isPredicated(getBundleStart(&MBB->back()))) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 481 | report("MBB exits via unconditional fall-through but ends with a " |
| 482 | "barrier instruction!", MBB); |
| 483 | } |
| 484 | if (!Cond.empty()) { |
| 485 | report("MBB exits via unconditional fall-through but has a condition!", |
| 486 | MBB); |
| 487 | } |
| 488 | } else if (TBB && !FBB && Cond.empty()) { |
| 489 | // Block unconditionally branches somewhere. |
Cameron Zwarich | 2100d21 | 2010-12-20 04:19:48 +0000 | [diff] [blame] | 490 | if (MBB->succ_size() != 1+LandingPadSuccs.size()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 491 | report("MBB exits via unconditional branch but doesn't have " |
| 492 | "exactly one CFG successor!", MBB); |
Jakob Stoklund Olesen | 0a7bbcb | 2010-10-21 18:47:06 +0000 | [diff] [blame] | 493 | } else if (!MBB->isSuccessor(TBB)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 494 | report("MBB exits via unconditional branch but the CFG " |
| 495 | "successor doesn't match the actual successor!", MBB); |
| 496 | } |
| 497 | if (MBB->empty()) { |
| 498 | report("MBB exits via unconditional branch but doesn't contain " |
| 499 | "any instructions!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 500 | } else if (!getBundleStart(&MBB->back())->isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 501 | report("MBB exits via unconditional branch but doesn't end with a " |
| 502 | "barrier instruction!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 503 | } else if (!getBundleStart(&MBB->back())->isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 504 | report("MBB exits via unconditional branch but the branch isn't a " |
| 505 | "terminator instruction!", MBB); |
| 506 | } |
| 507 | } else if (TBB && !FBB && !Cond.empty()) { |
| 508 | // Block conditionally branches somewhere, otherwise falls through. |
| 509 | MachineFunction::const_iterator MBBI = MBB; |
| 510 | ++MBBI; |
| 511 | if (MBBI == MF->end()) { |
| 512 | report("MBB conditionally falls through out of function!", MBB); |
| 513 | } if (MBB->succ_size() != 2) { |
| 514 | report("MBB exits via conditional branch/fall-through but doesn't have " |
| 515 | "exactly two CFG successors!", MBB); |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 516 | } else if (!matchPair(MBB->succ_begin(), TBB, MBBI)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 517 | report("MBB exits via conditional branch/fall-through but the CFG " |
| 518 | "successors don't match the actual successors!", MBB); |
| 519 | } |
| 520 | if (MBB->empty()) { |
| 521 | report("MBB exits via conditional branch/fall-through but doesn't " |
| 522 | "contain any instructions!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 523 | } else if (getBundleStart(&MBB->back())->isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 524 | report("MBB exits via conditional branch/fall-through but ends with a " |
| 525 | "barrier instruction!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 526 | } else if (!getBundleStart(&MBB->back())->isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 527 | report("MBB exits via conditional branch/fall-through but the branch " |
| 528 | "isn't a terminator instruction!", MBB); |
| 529 | } |
| 530 | } else if (TBB && FBB) { |
| 531 | // Block conditionally branches somewhere, otherwise branches |
| 532 | // somewhere else. |
| 533 | if (MBB->succ_size() != 2) { |
| 534 | report("MBB exits via conditional branch/branch but doesn't have " |
| 535 | "exactly two CFG successors!", MBB); |
Jakob Stoklund Olesen | 1dc0fcb | 2009-11-13 21:55:54 +0000 | [diff] [blame] | 536 | } else if (!matchPair(MBB->succ_begin(), TBB, FBB)) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 537 | report("MBB exits via conditional branch/branch but the CFG " |
| 538 | "successors don't match the actual successors!", MBB); |
| 539 | } |
| 540 | if (MBB->empty()) { |
| 541 | report("MBB exits via conditional branch/branch but doesn't " |
| 542 | "contain any instructions!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 543 | } else if (!getBundleStart(&MBB->back())->isBarrier()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 544 | report("MBB exits via conditional branch/branch but doesn't end with a " |
| 545 | "barrier instruction!", MBB); |
Akira Hatanaka | 6b0cd9b | 2012-06-14 20:51:13 +0000 | [diff] [blame] | 546 | } else if (!getBundleStart(&MBB->back())->isTerminator()) { |
Dan Gohman | 2792059 | 2009-08-27 02:43:49 +0000 | [diff] [blame] | 547 | report("MBB exits via conditional branch/branch but the branch " |
| 548 | "isn't a terminator instruction!", MBB); |
| 549 | } |
| 550 | if (Cond.empty()) { |
| 551 | report("MBB exits via conditinal branch/branch but there's no " |
| 552 | "condition!", MBB); |
| 553 | } |
| 554 | } else { |
| 555 | report("AnalyzeBranch returned invalid data!", MBB); |
| 556 | } |
| 557 | } |
| 558 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 559 | regsLive.clear(); |
Dan Gohman | 81bf03e | 2010-04-13 16:57:55 +0000 | [diff] [blame] | 560 | for (MachineBasicBlock::livein_iterator I = MBB->livein_begin(), |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 561 | E = MBB->livein_end(); I != E; ++I) { |
| 562 | if (!TargetRegisterInfo::isPhysicalRegister(*I)) { |
| 563 | report("MBB live-in list contains non-physical register", MBB); |
| 564 | continue; |
| 565 | } |
| 566 | regsLive.insert(*I); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 567 | for (MCSubRegIterator SubRegs(*I, TRI); SubRegs.isValid(); ++SubRegs) |
| 568 | regsLive.insert(*SubRegs); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 569 | } |
Jakob Stoklund Olesen | 710b13b | 2009-08-08 13:19:25 +0000 | [diff] [blame] | 570 | regsLiveInButUnused = regsLive; |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 571 | |
| 572 | const MachineFrameInfo *MFI = MF->getFrameInfo(); |
| 573 | assert(MFI && "Function has no frame info"); |
| 574 | BitVector PR = MFI->getPristineRegs(MBB); |
| 575 | for (int I = PR.find_first(); I>0; I = PR.find_next(I)) { |
| 576 | regsLive.insert(I); |
Jakob Stoklund Olesen | 396618b | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 577 | for (MCSubRegIterator SubRegs(I, TRI); SubRegs.isValid(); ++SubRegs) |
| 578 | regsLive.insert(*SubRegs); |
Jakob Stoklund Olesen | a6b677d | 2009-08-13 16:19:51 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 581 | regsKilled.clear(); |
| 582 | regsDefined.clear(); |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 583 | |
| 584 | if (Indexes) |
| 585 | lastIndex = Indexes->getMBBStartIdx(MBB); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 588 | // This function gets called for all bundle headers, including normal |
| 589 | // stand-alone unbundled instructions. |
| 590 | void MachineVerifier::visitMachineBundleBefore(const MachineInstr *MI) { |
| 591 | if (Indexes && Indexes->hasIndex(MI)) { |
| 592 | SlotIndex idx = Indexes->getInstructionIndex(MI); |
| 593 | if (!(idx > lastIndex)) { |
| 594 | report("Instruction index out of order", MI); |
| 595 | *OS << "Last instruction was at " << lastIndex << '\n'; |
| 596 | } |
| 597 | lastIndex = idx; |
| 598 | } |
Pete Cooper | 83569cb | 2012-06-07 17:41:39 +0000 | [diff] [blame] | 599 | |
| 600 | // Ensure non-terminators don't follow terminators. |
| 601 | // Ignore predicated terminators formed by if conversion. |
| 602 | // FIXME: If conversion shouldn't need to violate this rule. |
| 603 | if (MI->isTerminator() && !TII->isPredicated(MI)) { |
| 604 | if (!FirstTerminator) |
| 605 | FirstTerminator = MI; |
| 606 | } else if (FirstTerminator) { |
| 607 | report("Non-terminator instruction after the first terminator", MI); |
| 608 | *OS << "First terminator was:\t" << *FirstTerminator; |
| 609 | } |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 612 | void MachineVerifier::visitMachineInstrBefore(const MachineInstr *MI) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 613 | const MCInstrDesc &MCID = MI->getDesc(); |
| 614 | if (MI->getNumOperands() < MCID.getNumOperands()) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 615 | report("Too few operands", MI); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 616 | *OS << MCID.getNumOperands() << " operands expected, but " |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 617 | << MI->getNumExplicitOperands() << " given.\n"; |
| 618 | } |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 619 | |
| 620 | // Check the MachineMemOperands for basic consistency. |
| 621 | for (MachineInstr::mmo_iterator I = MI->memoperands_begin(), |
| 622 | E = MI->memoperands_end(); I != E; ++I) { |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 623 | if ((*I)->isLoad() && !MI->mayLoad()) |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 624 | report("Missing mayLoad flag", MI); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 625 | if ((*I)->isStore() && !MI->mayStore()) |
Dan Gohman | 2dbc4c8 | 2009-10-07 17:36:00 +0000 | [diff] [blame] | 626 | report("Missing mayStore flag", MI); |
| 627 | } |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 628 | |
| 629 | // Debug values must not have a slot index. |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 630 | // Other instructions must have one, unless they are inside a bundle. |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 631 | if (LiveInts) { |
| 632 | bool mapped = !LiveInts->isNotInMIMap(MI); |
| 633 | if (MI->isDebugValue()) { |
| 634 | if (mapped) |
| 635 | report("Debug instruction has a slot index", MI); |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 636 | } else if (MI->isInsideBundle()) { |
| 637 | if (mapped) |
| 638 | report("Instruction inside bundle has a slot index", MI); |
Jakob Stoklund Olesen | 1fe9c34 | 2010-08-05 22:32:21 +0000 | [diff] [blame] | 639 | } else { |
| 640 | if (!mapped) |
| 641 | report("Missing slot index", MI); |
| 642 | } |
| 643 | } |
| 644 | |
Andrew Trick | 3be654f | 2011-09-21 02:20:46 +0000 | [diff] [blame] | 645 | StringRef ErrorInfo; |
| 646 | if (!TII->verifyInstruction(MI, ErrorInfo)) |
| 647 | report(ErrorInfo.data(), MI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | void |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 651 | MachineVerifier::visitMachineOperand(const MachineOperand *MO, unsigned MONum) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 652 | const MachineInstr *MI = MO->getParent(); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 653 | const MCInstrDesc &MCID = MI->getDesc(); |
| 654 | const MCOperandInfo &MCOI = MCID.OpInfo[MONum]; |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 655 | |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 656 | // The first MCID.NumDefs operands must be explicit register defines |
| 657 | if (MONum < MCID.getNumDefs()) { |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 658 | if (!MO->isReg()) |
| 659 | report("Explicit definition must be a register", MO, MONum); |
Evan Cheng | cac58aa | 2012-05-29 19:40:44 +0000 | [diff] [blame] | 660 | else if (!MO->isDef() && !MCOI.isOptionalDef()) |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 661 | report("Explicit definition marked as use", MO, MONum); |
| 662 | else if (MO->isImplicit()) |
| 663 | report("Explicit definition marked as implicit", MO, MONum); |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 664 | } else if (MONum < MCID.getNumOperands()) { |
Eric Christopher | 113a06c | 2010-11-17 00:55:36 +0000 | [diff] [blame] | 665 | // Don't check if it's the last operand in a variadic instruction. See, |
| 666 | // e.g., LDM_RET in the arm back end. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 667 | if (MO->isReg() && |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 668 | !(MI->isVariadic() && MONum == MCID.getNumOperands()-1)) { |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 669 | if (MO->isDef() && !MCOI.isOptionalDef()) |
Cameron Zwarich | 22d67cf | 2010-12-19 21:37:23 +0000 | [diff] [blame] | 670 | report("Explicit operand marked as def", MO, MONum); |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 671 | if (MO->isImplicit()) |
| 672 | report("Explicit operand marked as implicit", MO, MONum); |
| 673 | } |
| 674 | } else { |
Jakob Stoklund Olesen | 5711564 | 2009-12-22 21:48:20 +0000 | [diff] [blame] | 675 | // ARM adds %reg0 operands to indicate predicates. We'll allow that. |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 676 | if (MO->isReg() && !MO->isImplicit() && !MI->isVariadic() && MO->getReg()) |
Jakob Stoklund Olesen | 39523e2 | 2009-09-23 20:57:55 +0000 | [diff] [blame] | 677 | report("Extra explicit operand on non-variadic instruction", MO, MONum); |
Jakob Stoklund Olesen | 44b27e5 | 2009-05-16 07:25:20 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 680 | switch (MO->getType()) { |
| 681 | case MachineOperand::MO_Register: { |
| 682 | const unsigned Reg = MO->getReg(); |
| 683 | if (!Reg) |
| 684 | return; |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 685 | if (MRI->tracksLiveness() && !MI->isDebugValue()) |
| 686 | checkLiveness(MO, MONum); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 687 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 688 | |
| 689 | // Check register classes. |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 690 | if (MONum < MCID.getNumOperands() && !MO->isImplicit()) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 691 | unsigned SubIdx = MO->getSubReg(); |
| 692 | |
| 693 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 694 | if (SubIdx) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 695 | report("Illegal subregister index for physical register", MO, MONum); |
| 696 | return; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 697 | } |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 698 | if (const TargetRegisterClass *DRC = |
| 699 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 700 | if (!DRC->contains(Reg)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 701 | report("Illegal physical register for instruction", MO, MONum); |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 702 | *OS << TRI->getName(Reg) << " is not a " |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 703 | << DRC->getName() << " register.\n"; |
| 704 | } |
| 705 | } |
| 706 | } else { |
| 707 | // Virtual register. |
| 708 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 709 | if (SubIdx) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 710 | const TargetRegisterClass *SRC = |
| 711 | TRI->getSubClassWithSubReg(RC, SubIdx); |
Jakob Stoklund Olesen | 6a8d2c6 | 2010-05-18 17:31:12 +0000 | [diff] [blame] | 712 | if (!SRC) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 713 | report("Invalid subregister index for virtual register", MO, MONum); |
Jakob Stoklund Olesen | 6a8d2c6 | 2010-05-18 17:31:12 +0000 | [diff] [blame] | 714 | *OS << "Register class " << RC->getName() |
| 715 | << " does not support subreg index " << SubIdx << "\n"; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 716 | return; |
| 717 | } |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 718 | if (RC != SRC) { |
| 719 | report("Invalid register class for subregister index", MO, MONum); |
| 720 | *OS << "Register class " << RC->getName() |
| 721 | << " does not fully support subreg index " << SubIdx << "\n"; |
| 722 | return; |
| 723 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 724 | } |
Jakob Stoklund Olesen | 397fc48 | 2012-05-07 22:10:26 +0000 | [diff] [blame] | 725 | if (const TargetRegisterClass *DRC = |
| 726 | TII->getRegClass(MCID, MONum, TRI, *MF)) { |
Jakob Stoklund Olesen | b4a0221 | 2011-10-05 22:12:57 +0000 | [diff] [blame] | 727 | if (SubIdx) { |
| 728 | const TargetRegisterClass *SuperRC = |
| 729 | TRI->getLargestLegalSuperClass(RC); |
| 730 | if (!SuperRC) { |
| 731 | report("No largest legal super class exists.", MO, MONum); |
| 732 | return; |
| 733 | } |
| 734 | DRC = TRI->getMatchingSuperRegClass(SuperRC, DRC, SubIdx); |
| 735 | if (!DRC) { |
| 736 | report("No matching super-reg register class.", MO, MONum); |
| 737 | return; |
| 738 | } |
| 739 | } |
Jakob Stoklund Olesen | fa226bc | 2011-06-02 05:43:46 +0000 | [diff] [blame] | 740 | if (!RC->hasSuperClassEq(DRC)) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 741 | report("Illegal virtual register for instruction", MO, MONum); |
| 742 | *OS << "Expected a " << DRC->getName() << " register, but got a " |
| 743 | << RC->getName() << " register\n"; |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | break; |
| 749 | } |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 750 | |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 751 | case MachineOperand::MO_RegisterMask: |
| 752 | regMasks.push_back(MO->getRegMask()); |
| 753 | break; |
| 754 | |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 755 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 756 | if (MI->isPHI() && !MO->getMBB()->isSuccessor(MI->getParent())) |
| 757 | report("PHI operand is not in the CFG", MO, MONum); |
Jakob Stoklund Olesen | a5ba07c | 2009-09-21 07:19:08 +0000 | [diff] [blame] | 758 | break; |
| 759 | |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 760 | case MachineOperand::MO_FrameIndex: |
| 761 | if (LiveStks && LiveStks->hasInterval(MO->getIndex()) && |
| 762 | LiveInts && !LiveInts->isNotInMIMap(MI)) { |
| 763 | LiveInterval &LI = LiveStks->getInterval(MO->getIndex()); |
| 764 | SlotIndex Idx = LiveInts->getInstructionIndex(MI); |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 765 | if (MI->mayLoad() && !LI.liveAt(Idx.getRegSlot(true))) { |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 766 | report("Instruction loads from dead spill slot", MO, MONum); |
| 767 | *OS << "Live stack: " << LI << '\n'; |
| 768 | } |
Evan Cheng | 5a96b3d | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 769 | if (MI->mayStore() && !LI.liveAt(Idx.getRegSlot())) { |
Jakob Stoklund Olesen | e8f0823 | 2010-11-01 19:49:52 +0000 | [diff] [blame] | 770 | report("Instruction stores to dead spill slot", MO, MONum); |
| 771 | *OS << "Live stack: " << LI << '\n'; |
| 772 | } |
| 773 | } |
| 774 | break; |
| 775 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 776 | default: |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 781 | void MachineVerifier::checkLiveness(const MachineOperand *MO, unsigned MONum) { |
| 782 | const MachineInstr *MI = MO->getParent(); |
| 783 | const unsigned Reg = MO->getReg(); |
| 784 | |
| 785 | // Both use and def operands can read a register. |
| 786 | if (MO->readsReg()) { |
| 787 | regsLiveInButUnused.erase(Reg); |
| 788 | |
| 789 | bool isKill = false; |
| 790 | unsigned defIdx; |
| 791 | if (MI->isRegTiedToDefOperand(MONum, &defIdx)) { |
| 792 | // A two-addr use counts as a kill if use and def are the same. |
| 793 | unsigned DefReg = MI->getOperand(defIdx).getReg(); |
| 794 | if (Reg == DefReg) |
| 795 | isKill = true; |
| 796 | else if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 797 | report("Two-address instruction operands must be identical", MO, MONum); |
| 798 | } |
| 799 | } else |
| 800 | isKill = MO->isKill(); |
| 801 | |
| 802 | if (isKill) |
| 803 | addRegWithSubRegs(regsKilled, Reg); |
| 804 | |
| 805 | // Check that LiveVars knows this kill. |
| 806 | if (LiveVars && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 807 | MO->isKill()) { |
| 808 | LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); |
| 809 | if (std::find(VI.Kills.begin(), VI.Kills.end(), MI) == VI.Kills.end()) |
| 810 | report("Kill missing from LiveVariables", MO, MONum); |
| 811 | } |
| 812 | |
| 813 | // Check LiveInts liveness and kill. |
| 814 | if (TargetRegisterInfo::isVirtualRegister(Reg) && |
| 815 | LiveInts && !LiveInts->isNotInMIMap(MI)) { |
| 816 | SlotIndex UseIdx = LiveInts->getInstructionIndex(MI).getRegSlot(true); |
| 817 | if (LiveInts->hasInterval(Reg)) { |
| 818 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 819 | if (!LI.liveAt(UseIdx)) { |
| 820 | report("No live range at use", MO, MONum); |
| 821 | *OS << UseIdx << " is not live in " << LI << '\n'; |
| 822 | } |
| 823 | // Check for extra kill flags. |
| 824 | // Note that we allow missing kill flags for now. |
| 825 | if (MO->isKill() && !LI.killedAt(UseIdx.getRegSlot())) { |
| 826 | report("Live range continues after kill flag", MO, MONum); |
| 827 | *OS << "Live range: " << LI << '\n'; |
| 828 | } |
| 829 | } else { |
| 830 | report("Virtual register has no Live interval", MO, MONum); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | // Use of a dead register. |
| 835 | if (!regsLive.count(Reg)) { |
| 836 | if (TargetRegisterInfo::isPhysicalRegister(Reg)) { |
| 837 | // Reserved registers may be used even when 'dead'. |
| 838 | if (!isReserved(Reg)) |
| 839 | report("Using an undefined physical register", MO, MONum); |
| 840 | } else { |
| 841 | BBInfo &MInfo = MBBInfoMap[MI->getParent()]; |
| 842 | // We don't know which virtual registers are live in, so only complain |
| 843 | // if vreg was killed in this MBB. Otherwise keep track of vregs that |
| 844 | // must be live in. PHI instructions are handled separately. |
| 845 | if (MInfo.regsKilled.count(Reg)) |
| 846 | report("Using a killed virtual register", MO, MONum); |
| 847 | else if (!MI->isPHI()) |
| 848 | MInfo.vregsLiveIn.insert(std::make_pair(Reg, MI)); |
| 849 | } |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | if (MO->isDef()) { |
| 854 | // Register defined. |
| 855 | // TODO: verify that earlyclobber ops are not used. |
| 856 | if (MO->isDead()) |
| 857 | addRegWithSubRegs(regsDead, Reg); |
| 858 | else |
| 859 | addRegWithSubRegs(regsDefined, Reg); |
| 860 | |
| 861 | // Verify SSA form. |
| 862 | if (MRI->isSSA() && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 863 | llvm::next(MRI->def_begin(Reg)) != MRI->def_end()) |
| 864 | report("Multiple virtual register defs in SSA form", MO, MONum); |
| 865 | |
| 866 | // Check LiveInts for a live range, but only for virtual registers. |
| 867 | if (LiveInts && TargetRegisterInfo::isVirtualRegister(Reg) && |
| 868 | !LiveInts->isNotInMIMap(MI)) { |
Jakob Stoklund Olesen | f935e94 | 2012-06-22 22:23:58 +0000 | [diff] [blame] | 869 | SlotIndex DefIdx = LiveInts->getInstructionIndex(MI); |
| 870 | DefIdx = DefIdx.getRegSlot(MO->isEarlyClobber()); |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 871 | if (LiveInts->hasInterval(Reg)) { |
| 872 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 873 | if (const VNInfo *VNI = LI.getVNInfoAt(DefIdx)) { |
| 874 | assert(VNI && "NULL valno is not allowed"); |
Jakob Stoklund Olesen | f935e94 | 2012-06-22 22:23:58 +0000 | [diff] [blame] | 875 | if (VNI->def != DefIdx) { |
Jakob Stoklund Olesen | 948a444 | 2012-03-28 20:47:35 +0000 | [diff] [blame] | 876 | report("Inconsistent valno->def", MO, MONum); |
| 877 | *OS << "Valno " << VNI->id << " is not defined at " |
| 878 | << DefIdx << " in " << LI << '\n'; |
| 879 | } |
| 880 | } else { |
| 881 | report("No live range at def", MO, MONum); |
| 882 | *OS << DefIdx << " is not live in " << LI << '\n'; |
| 883 | } |
| 884 | } else { |
| 885 | report("Virtual register has no Live interval", MO, MONum); |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 891 | void MachineVerifier::visitMachineInstrAfter(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 1f9c3ec | 2012-06-06 22:34:30 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | // This function gets called after visiting all instructions in a bundle. The |
| 895 | // argument points to the bundle header. |
| 896 | // Normal stand-alone instructions are also considered 'bundles', and this |
| 897 | // function is called for all of them. |
| 898 | void MachineVerifier::visitMachineBundleAfter(const MachineInstr *MI) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 899 | BBInfo &MInfo = MBBInfoMap[MI->getParent()]; |
| 900 | set_union(MInfo.regsKilled, regsKilled); |
Jakob Stoklund Olesen | 73cf709 | 2010-08-05 18:59:59 +0000 | [diff] [blame] | 901 | set_subtract(regsLive, regsKilled); regsKilled.clear(); |
Jakob Stoklund Olesen | 9ca12d2 | 2012-02-28 01:42:41 +0000 | [diff] [blame] | 902 | // Kill any masked registers. |
| 903 | while (!regMasks.empty()) { |
| 904 | const uint32_t *Mask = regMasks.pop_back_val(); |
| 905 | for (RegSet::iterator I = regsLive.begin(), E = regsLive.end(); I != E; ++I) |
| 906 | if (TargetRegisterInfo::isPhysicalRegister(*I) && |
| 907 | MachineOperand::clobbersPhysReg(Mask, *I)) |
| 908 | regsDead.push_back(*I); |
| 909 | } |
Jakob Stoklund Olesen | 73cf709 | 2010-08-05 18:59:59 +0000 | [diff] [blame] | 910 | set_subtract(regsLive, regsDead); regsDead.clear(); |
| 911 | set_union(regsLive, regsDefined); regsDefined.clear(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | void |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 915 | MachineVerifier::visitMachineBasicBlockAfter(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 916 | MBBInfoMap[MBB].regsLiveOut = regsLive; |
| 917 | regsLive.clear(); |
Jakob Stoklund Olesen | fc69c37 | 2011-01-12 21:27:48 +0000 | [diff] [blame] | 918 | |
| 919 | if (Indexes) { |
| 920 | SlotIndex stop = Indexes->getMBBEndIdx(MBB); |
| 921 | if (!(stop > lastIndex)) { |
| 922 | report("Block ends before last instruction index", MBB); |
| 923 | *OS << "Block ends at " << stop |
| 924 | << " last instruction was at " << lastIndex << '\n'; |
| 925 | } |
| 926 | lastIndex = stop; |
| 927 | } |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // Calculate the largest possible vregsPassed sets. These are the registers that |
| 931 | // can pass through an MBB live, but may not be live every time. It is assumed |
| 932 | // that all vregsPassed sets are empty before the call. |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 933 | void MachineVerifier::calcRegsPassed() { |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 934 | // First push live-out regs to successors' vregsPassed. Remember the MBBs that |
| 935 | // have any vregsPassed. |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 936 | SmallPtrSet<const MachineBasicBlock*, 8> todo; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 937 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 938 | MFI != MFE; ++MFI) { |
| 939 | const MachineBasicBlock &MBB(*MFI); |
| 940 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 941 | if (!MInfo.reachable) |
| 942 | continue; |
| 943 | for (MachineBasicBlock::const_succ_iterator SuI = MBB.succ_begin(), |
| 944 | SuE = MBB.succ_end(); SuI != SuE; ++SuI) { |
| 945 | BBInfo &SInfo = MBBInfoMap[*SuI]; |
| 946 | if (SInfo.addPassed(MInfo.regsLiveOut)) |
| 947 | todo.insert(*SuI); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | // Iteratively push vregsPassed to successors. This will converge to the same |
| 952 | // final state regardless of DenseSet iteration order. |
| 953 | while (!todo.empty()) { |
| 954 | const MachineBasicBlock *MBB = *todo.begin(); |
| 955 | todo.erase(MBB); |
| 956 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 957 | for (MachineBasicBlock::const_succ_iterator SuI = MBB->succ_begin(), |
| 958 | SuE = MBB->succ_end(); SuI != SuE; ++SuI) { |
| 959 | if (*SuI == MBB) |
| 960 | continue; |
| 961 | BBInfo &SInfo = MBBInfoMap[*SuI]; |
| 962 | if (SInfo.addPassed(MInfo.vregsPassed)) |
| 963 | todo.insert(*SuI); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 968 | // Calculate the set of virtual registers that must be passed through each basic |
| 969 | // block in order to satisfy the requirements of successor blocks. This is very |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 970 | // similar to calcRegsPassed, only backwards. |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 971 | void MachineVerifier::calcRegsRequired() { |
| 972 | // First push live-in regs to predecessors' vregsRequired. |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 973 | SmallPtrSet<const MachineBasicBlock*, 8> todo; |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 974 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 975 | MFI != MFE; ++MFI) { |
| 976 | const MachineBasicBlock &MBB(*MFI); |
| 977 | BBInfo &MInfo = MBBInfoMap[&MBB]; |
| 978 | for (MachineBasicBlock::const_pred_iterator PrI = MBB.pred_begin(), |
| 979 | PrE = MBB.pred_end(); PrI != PrE; ++PrI) { |
| 980 | BBInfo &PInfo = MBBInfoMap[*PrI]; |
| 981 | if (PInfo.addRequired(MInfo.vregsLiveIn)) |
| 982 | todo.insert(*PrI); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | // Iteratively push vregsRequired to predecessors. This will converge to the |
| 987 | // same final state regardless of DenseSet iteration order. |
| 988 | while (!todo.empty()) { |
| 989 | const MachineBasicBlock *MBB = *todo.begin(); |
| 990 | todo.erase(MBB); |
| 991 | BBInfo &MInfo = MBBInfoMap[MBB]; |
| 992 | for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), |
| 993 | PrE = MBB->pred_end(); PrI != PrE; ++PrI) { |
| 994 | if (*PrI == MBB) |
| 995 | continue; |
| 996 | BBInfo &SInfo = MBBInfoMap[*PrI]; |
| 997 | if (SInfo.addRequired(MInfo.vregsRequired)) |
| 998 | todo.insert(*PrI); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1003 | // Check PHI instructions at the beginning of MBB. It is assumed that |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1004 | // calcRegsPassed has been run so BBInfo::isLiveOut is valid. |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1005 | void MachineVerifier::checkPHIOps(const MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1006 | SmallPtrSet<const MachineBasicBlock*, 8> seen; |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1007 | for (MachineBasicBlock::const_iterator BBI = MBB->begin(), BBE = MBB->end(); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 1008 | BBI != BBE && BBI->isPHI(); ++BBI) { |
Jakob Stoklund Olesen | 1efd6b9 | 2012-03-10 00:36:04 +0000 | [diff] [blame] | 1009 | seen.clear(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1010 | |
| 1011 | for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) { |
| 1012 | unsigned Reg = BBI->getOperand(i).getReg(); |
| 1013 | const MachineBasicBlock *Pre = BBI->getOperand(i + 1).getMBB(); |
| 1014 | if (!Pre->isSuccessor(MBB)) |
| 1015 | continue; |
| 1016 | seen.insert(Pre); |
| 1017 | BBInfo &PrInfo = MBBInfoMap[Pre]; |
| 1018 | if (PrInfo.reachable && !PrInfo.isLiveOut(Reg)) |
| 1019 | report("PHI operand is not live-out from predecessor", |
| 1020 | &BBI->getOperand(i), i); |
| 1021 | } |
| 1022 | |
| 1023 | // Did we see all predecessors? |
| 1024 | for (MachineBasicBlock::const_pred_iterator PrI = MBB->pred_begin(), |
| 1025 | PrE = MBB->pred_end(); PrI != PrE; ++PrI) { |
| 1026 | if (!seen.count(*PrI)) { |
| 1027 | report("Missing PHI operand", BBI); |
Dan Gohman | 0ba90f3 | 2009-10-31 20:19:03 +0000 | [diff] [blame] | 1028 | *OS << "BB#" << (*PrI)->getNumber() |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1029 | << " is a predecessor according to the CFG.\n"; |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | |
Jakob Stoklund Olesen | b44fad7 | 2009-10-04 18:18:39 +0000 | [diff] [blame] | 1035 | void MachineVerifier::visitMachineFunctionAfter() { |
Jakob Stoklund Olesen | b31defe | 2010-01-05 20:59:36 +0000 | [diff] [blame] | 1036 | calcRegsPassed(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1037 | |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1038 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1039 | MFI != MFE; ++MFI) { |
| 1040 | BBInfo &MInfo = MBBInfoMap[MFI]; |
| 1041 | |
| 1042 | // Skip unreachable MBBs. |
| 1043 | if (!MInfo.reachable) |
| 1044 | continue; |
| 1045 | |
| 1046 | checkPHIOps(MFI); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1047 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1048 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1049 | // Now check liveness info if available |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1050 | calcRegsRequired(); |
| 1051 | |
| 1052 | if (MRI->isSSA() && !MF->empty()) { |
| 1053 | BBInfo &MInfo = MBBInfoMap[&MF->front()]; |
| 1054 | for (RegSet::iterator |
| 1055 | I = MInfo.vregsRequired.begin(), E = MInfo.vregsRequired.end(); I != E; |
Jakob Stoklund Olesen | ff0275e | 2012-03-10 00:44:11 +0000 | [diff] [blame] | 1056 | ++I) |
| 1057 | report("Virtual register def doesn't dominate all uses.", |
| 1058 | MRI->getVRegDef(*I)); |
Jakob Stoklund Olesen | 64ffa83 | 2012-03-10 00:36:06 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1061 | if (LiveVars) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1062 | verifyLiveVariables(); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1063 | if (LiveInts) |
| 1064 | verifyLiveIntervals(); |
Jakob Stoklund Olesen | 48872e0 | 2009-05-16 00:33:53 +0000 | [diff] [blame] | 1065 | } |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1066 | |
| 1067 | void MachineVerifier::verifyLiveVariables() { |
| 1068 | assert(LiveVars && "Don't call verifyLiveVariables without LiveVars"); |
Jakob Stoklund Olesen | 98c5476 | 2011-01-08 23:11:02 +0000 | [diff] [blame] | 1069 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 1070 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1071 | LiveVariables::VarInfo &VI = LiveVars->getVarInfo(Reg); |
| 1072 | for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); |
| 1073 | MFI != MFE; ++MFI) { |
| 1074 | BBInfo &MInfo = MBBInfoMap[MFI]; |
| 1075 | |
| 1076 | // Our vregsRequired should be identical to LiveVariables' AliveBlocks |
| 1077 | if (MInfo.vregsRequired.count(Reg)) { |
| 1078 | if (!VI.AliveBlocks.test(MFI->getNumber())) { |
| 1079 | report("LiveVariables: Block missing from AliveBlocks", MFI); |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 1080 | *OS << "Virtual register " << PrintReg(Reg) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1081 | << " must be live through the block.\n"; |
| 1082 | } |
| 1083 | } else { |
| 1084 | if (VI.AliveBlocks.test(MFI->getNumber())) { |
| 1085 | report("LiveVariables: Block should not be in AliveBlocks", MFI); |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 1086 | *OS << "Virtual register " << PrintReg(Reg) |
Jakob Stoklund Olesen | 8f16e02 | 2009-11-18 20:36:57 +0000 | [diff] [blame] | 1087 | << " is not needed live through the block.\n"; |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1094 | void MachineVerifier::verifyLiveIntervals() { |
| 1095 | assert(LiveInts && "Don't call verifyLiveIntervals without LiveInts"); |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1096 | for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { |
| 1097 | unsigned Reg = TargetRegisterInfo::index2VirtReg(i); |
Jakob Stoklund Olesen | 893ab5d | 2010-10-06 23:54:35 +0000 | [diff] [blame] | 1098 | |
| 1099 | // Spilling and splitting may leave unused registers around. Skip them. |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1100 | if (MRI->reg_nodbg_empty(Reg)) |
Jakob Stoklund Olesen | 893ab5d | 2010-10-06 23:54:35 +0000 | [diff] [blame] | 1101 | continue; |
| 1102 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1103 | if (!LiveInts->hasInterval(Reg)) { |
| 1104 | report("Missing live interval for virtual register", MF); |
| 1105 | *OS << PrintReg(Reg, TRI) << " still has defs or uses\n"; |
Jakob Stoklund Olesen | 8c45642 | 2010-10-28 20:44:22 +0000 | [diff] [blame] | 1106 | continue; |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1107 | } |
Jakob Stoklund Olesen | 8c45642 | 2010-10-28 20:44:22 +0000 | [diff] [blame] | 1108 | |
Jakob Stoklund Olesen | 12a7be9 | 2012-06-20 23:23:59 +0000 | [diff] [blame] | 1109 | const LiveInterval &LI = LiveInts->getInterval(Reg); |
| 1110 | assert(Reg == LI.reg && "Invalid reg to interval mapping"); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1111 | |
| 1112 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end(); |
| 1113 | I!=E; ++I) { |
| 1114 | VNInfo *VNI = *I; |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1115 | const VNInfo *DefVNI = LI.getVNInfoAt(VNI->def); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1116 | |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1117 | if (!DefVNI) { |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1118 | if (!VNI->isUnused()) { |
| 1119 | report("Valno not live at def and not marked unused", MF); |
| 1120 | *OS << "Valno #" << VNI->id << " in " << LI << '\n'; |
| 1121 | } |
| 1122 | continue; |
| 1123 | } |
| 1124 | |
| 1125 | if (VNI->isUnused()) |
| 1126 | continue; |
| 1127 | |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1128 | if (DefVNI != VNI) { |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1129 | report("Live range at def has different valno", MF); |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1130 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
Jakob Stoklund Olesen | dbcc2e1 | 2010-10-26 20:21:43 +0000 | [diff] [blame] | 1131 | << " where valno #" << DefVNI->id << " is live in " << LI << '\n'; |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1132 | continue; |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1135 | const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(VNI->def); |
| 1136 | if (!MBB) { |
| 1137 | report("Invalid definition index", MF); |
Jakob Stoklund Olesen | dbcc2e1 | 2010-10-26 20:21:43 +0000 | [diff] [blame] | 1138 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
| 1139 | << " in " << LI << '\n'; |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1140 | continue; |
| 1141 | } |
| 1142 | |
| 1143 | if (VNI->isPHIDef()) { |
| 1144 | if (VNI->def != LiveInts->getMBBStartIdx(MBB)) { |
| 1145 | report("PHIDef value is not defined at MBB start", MF); |
| 1146 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
Jakob Stoklund Olesen | dbcc2e1 | 2010-10-26 20:21:43 +0000 | [diff] [blame] | 1147 | << ", not at the beginning of BB#" << MBB->getNumber() |
| 1148 | << " in " << LI << '\n'; |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } else { |
| 1151 | // Non-PHI def. |
Jakob Stoklund Olesen | 30e98a0 | 2012-02-29 00:33:41 +0000 | [diff] [blame] | 1152 | const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1153 | if (!MI) { |
| 1154 | report("No instruction at def index", MF); |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1155 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
| 1156 | << " in " << LI << '\n'; |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1157 | continue; |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1158 | } |
Cameron Zwarich | 0b13d7d | 2010-12-20 03:15:20 +0000 | [diff] [blame] | 1159 | |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1160 | bool hasDef = false; |
Cameron Zwarich | 0b13d7d | 2010-12-20 03:15:20 +0000 | [diff] [blame] | 1161 | bool isEarlyClobber = false; |
Jakob Stoklund Olesen | 30e98a0 | 2012-02-29 00:33:41 +0000 | [diff] [blame] | 1162 | for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1163 | if (!MOI->isReg() || !MOI->isDef()) |
| 1164 | continue; |
| 1165 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
| 1166 | if (MOI->getReg() != LI.reg) |
| 1167 | continue; |
| 1168 | } else { |
| 1169 | if (!TargetRegisterInfo::isPhysicalRegister(MOI->getReg()) || |
| 1170 | !TRI->regsOverlap(LI.reg, MOI->getReg())) |
| 1171 | continue; |
Cameron Zwarich | 0b13d7d | 2010-12-20 03:15:20 +0000 | [diff] [blame] | 1172 | } |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1173 | hasDef = true; |
| 1174 | if (MOI->isEarlyClobber()) |
| 1175 | isEarlyClobber = true; |
| 1176 | } |
| 1177 | |
| 1178 | if (!hasDef) { |
| 1179 | report("Defining instruction does not modify register", MI); |
| 1180 | *OS << "Valno #" << VNI->id << " in " << LI << '\n'; |
Cameron Zwarich | 0b13d7d | 2010-12-20 03:15:20 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | // Early clobber defs begin at USE slots, but other defs must begin at |
| 1184 | // DEF slots. |
| 1185 | if (isEarlyClobber) { |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 1186 | if (!VNI->def.isEarlyClobber()) { |
| 1187 | report("Early clobber def must be at an early-clobber slot", MF); |
Cameron Zwarich | 0b13d7d | 2010-12-20 03:15:20 +0000 | [diff] [blame] | 1188 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
| 1189 | << " in " << LI << '\n'; |
| 1190 | } |
Jakob Stoklund Olesen | 2debd48 | 2011-11-13 20:45:27 +0000 | [diff] [blame] | 1191 | } else if (!VNI->def.isRegister()) { |
| 1192 | report("Non-PHI, non-early clobber def must be at a register slot", |
| 1193 | MF); |
Cameron Zwarich | 0b13d7d | 2010-12-20 03:15:20 +0000 | [diff] [blame] | 1194 | *OS << "Valno #" << VNI->id << " is defined at " << VNI->def |
| 1195 | << " in " << LI << '\n'; |
| 1196 | } |
Jakob Stoklund Olesen | 3bf7cf9 | 2010-10-22 22:48:58 +0000 | [diff] [blame] | 1197 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | for (LiveInterval::const_iterator I = LI.begin(), E = LI.end(); I!=E; ++I) { |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1201 | const VNInfo *VNI = I->valno; |
| 1202 | assert(VNI && "Live range has no valno"); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1203 | |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1204 | if (VNI->id >= LI.getNumValNums() || VNI != LI.getValNumInfo(VNI->id)) { |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1205 | report("Foreign valno in live range", MF); |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1206 | I->print(*OS); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1207 | *OS << " has a valno not in " << LI << '\n'; |
| 1208 | } |
| 1209 | |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1210 | if (VNI->isUnused()) { |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1211 | report("Live range valno is marked unused", MF); |
Jakob Stoklund Olesen | ed82635 | 2010-10-02 05:24:46 +0000 | [diff] [blame] | 1212 | I->print(*OS); |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1213 | *OS << " in " << LI << '\n'; |
| 1214 | } |
| 1215 | |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1216 | const MachineBasicBlock *MBB = LiveInts->getMBBFromIndex(I->start); |
| 1217 | if (!MBB) { |
| 1218 | report("Bad start of live segment, no basic block", MF); |
| 1219 | I->print(*OS); |
| 1220 | *OS << " in " << LI << '\n'; |
| 1221 | continue; |
| 1222 | } |
| 1223 | SlotIndex MBBStartIdx = LiveInts->getMBBStartIdx(MBB); |
| 1224 | if (I->start != MBBStartIdx && I->start != VNI->def) { |
| 1225 | report("Live segment must begin at MBB entry or valno def", MBB); |
| 1226 | I->print(*OS); |
| 1227 | *OS << " in " << LI << '\n' << "Basic block starts at " |
| 1228 | << MBBStartIdx << '\n'; |
| 1229 | } |
| 1230 | |
| 1231 | const MachineBasicBlock *EndMBB = |
| 1232 | LiveInts->getMBBFromIndex(I->end.getPrevSlot()); |
| 1233 | if (!EndMBB) { |
| 1234 | report("Bad end of live segment, no basic block", MF); |
| 1235 | I->print(*OS); |
| 1236 | *OS << " in " << LI << '\n'; |
| 1237 | continue; |
| 1238 | } |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1239 | |
| 1240 | // No more checks for live-out segments. |
| 1241 | if (I->end == LiveInts->getMBBEndIdx(EndMBB)) |
| 1242 | continue; |
| 1243 | |
| 1244 | // The live segment is ending inside EndMBB |
Jakob Stoklund Olesen | 30e98a0 | 2012-02-29 00:33:41 +0000 | [diff] [blame] | 1245 | const MachineInstr *MI = |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1246 | LiveInts->getInstructionFromIndex(I->end.getPrevSlot()); |
| 1247 | if (!MI) { |
| 1248 | report("Live segment doesn't end at a valid instruction", EndMBB); |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1249 | I->print(*OS); |
| 1250 | *OS << " in " << LI << '\n' << "Basic block starts at " |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1251 | << MBBStartIdx << '\n'; |
| 1252 | continue; |
| 1253 | } |
Cameron Zwarich | 636f15f | 2010-12-20 01:22:37 +0000 | [diff] [blame] | 1254 | |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1255 | // The block slot must refer to a basic block boundary. |
| 1256 | if (I->end.isBlock()) { |
| 1257 | report("Live segment ends at B slot of an instruction", MI); |
| 1258 | I->print(*OS); |
| 1259 | *OS << " in " << LI << '\n'; |
| 1260 | } |
| 1261 | |
| 1262 | if (I->end.isDead()) { |
| 1263 | // Segment ends on the dead slot. |
| 1264 | // That means there must be a dead def. |
| 1265 | if (!SlotIndex::isSameInstr(I->start, I->end)) { |
| 1266 | report("Live segment ending at dead slot spans instructions", MI); |
| 1267 | I->print(*OS); |
| 1268 | *OS << " in " << LI << '\n'; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | // A live segment can only end at an early-clobber slot if it is being |
| 1273 | // redefined by an early-clobber def. |
| 1274 | if (I->end.isEarlyClobber()) { |
| 1275 | if (I+1 == E || (I+1)->start != I->end) { |
| 1276 | report("Live segment ending at early clobber slot must be " |
| 1277 | "redefined by an EC def in the same instruction", MI); |
| 1278 | I->print(*OS); |
| 1279 | *OS << " in " << LI << '\n'; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | // The following checks only apply to virtual registers. Physreg liveness |
| 1284 | // is too weird to check. |
| 1285 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
| 1286 | // A live range can end with either a redefinition, a kill flag on a |
| 1287 | // use, or a dead flag on a def. |
| 1288 | bool hasRead = false; |
| 1289 | bool hasDeadDef = false; |
Jakob Stoklund Olesen | 30e98a0 | 2012-02-29 00:33:41 +0000 | [diff] [blame] | 1290 | for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1291 | if (!MOI->isReg() || MOI->getReg() != LI.reg) |
| 1292 | continue; |
| 1293 | if (MOI->readsReg()) |
| 1294 | hasRead = true; |
| 1295 | if (MOI->isDef() && MOI->isDead()) |
| 1296 | hasDeadDef = true; |
| 1297 | } |
| 1298 | |
| 1299 | if (I->end.isDead()) { |
Cameron Zwarich | 636f15f | 2010-12-20 01:22:37 +0000 | [diff] [blame] | 1300 | if (!hasDeadDef) { |
Jakob Stoklund Olesen | 121b179 | 2012-02-27 18:24:30 +0000 | [diff] [blame] | 1301 | report("Instruction doesn't have a dead def operand", MI); |
| 1302 | I->print(*OS); |
| 1303 | *OS << " in " << LI << '\n'; |
| 1304 | } |
| 1305 | } else { |
| 1306 | if (!hasRead) { |
| 1307 | report("Instruction ending live range doesn't read the register", |
| 1308 | MI); |
Cameron Zwarich | 636f15f | 2010-12-20 01:22:37 +0000 | [diff] [blame] | 1309 | I->print(*OS); |
| 1310 | *OS << " in " << LI << '\n'; |
| 1311 | } |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | // Now check all the basic blocks in this live segment. |
| 1316 | MachineFunction::const_iterator MFI = MBB; |
Cameron Zwarich | cb584d0 | 2010-12-28 23:45:38 +0000 | [diff] [blame] | 1317 | // Is this live range the beginning of a non-PHIDef VN? |
| 1318 | if (I->start == VNI->def && !VNI->isPHIDef()) { |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1319 | // Not live-in to any blocks. |
| 1320 | if (MBB == EndMBB) |
| 1321 | continue; |
| 1322 | // Skip this block. |
| 1323 | ++MFI; |
| 1324 | } |
| 1325 | for (;;) { |
| 1326 | assert(LiveInts->isLiveInToMBB(LI, MFI)); |
Jakob Stoklund Olesen | e459d55 | 2010-10-26 16:49:23 +0000 | [diff] [blame] | 1327 | // We don't know how to track physregs into a landing pad. |
| 1328 | if (TargetRegisterInfo::isPhysicalRegister(LI.reg) && |
| 1329 | MFI->isLandingPad()) { |
| 1330 | if (&*MFI == EndMBB) |
| 1331 | break; |
| 1332 | ++MFI; |
| 1333 | continue; |
| 1334 | } |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1335 | // Check that VNI is live-out of all predecessors. |
| 1336 | for (MachineBasicBlock::const_pred_iterator PI = MFI->pred_begin(), |
| 1337 | PE = MFI->pred_end(); PI != PE; ++PI) { |
Jakob Stoklund Olesen | 194eb71 | 2011-11-14 01:39:36 +0000 | [diff] [blame] | 1338 | SlotIndex PEnd = LiveInts->getMBBEndIdx(*PI); |
| 1339 | const VNInfo *PVNI = LI.getVNInfoBefore(PEnd); |
Cameron Zwarich | 4eee42c | 2010-12-27 05:17:23 +0000 | [diff] [blame] | 1340 | |
Jakob Stoklund Olesen | df8412c | 2011-09-15 05:16:30 +0000 | [diff] [blame] | 1341 | if (VNI->isPHIDef() && VNI->def == LiveInts->getMBBStartIdx(MFI)) |
Cameron Zwarich | 4eee42c | 2010-12-27 05:17:23 +0000 | [diff] [blame] | 1342 | continue; |
Cameron Zwarich | 4eee42c | 2010-12-27 05:17:23 +0000 | [diff] [blame] | 1343 | |
Cameron Zwarich | cb584d0 | 2010-12-28 23:45:38 +0000 | [diff] [blame] | 1344 | if (!PVNI) { |
| 1345 | report("Register not marked live out of predecessor", *PI); |
| 1346 | *OS << "Valno #" << VNI->id << " live into BB#" << MFI->getNumber() |
Jakob Stoklund Olesen | 194eb71 | 2011-11-14 01:39:36 +0000 | [diff] [blame] | 1347 | << '@' << LiveInts->getMBBStartIdx(MFI) << ", not live before " |
Cameron Zwarich | cb584d0 | 2010-12-28 23:45:38 +0000 | [diff] [blame] | 1348 | << PEnd << " in " << LI << '\n'; |
| 1349 | continue; |
| 1350 | } |
| 1351 | |
Cameron Zwarich | 4eee42c | 2010-12-27 05:17:23 +0000 | [diff] [blame] | 1352 | if (PVNI != VNI) { |
Jakob Stoklund Olesen | 7871687 | 2010-10-23 00:49:09 +0000 | [diff] [blame] | 1353 | report("Different value live out of predecessor", *PI); |
| 1354 | *OS << "Valno #" << PVNI->id << " live out of BB#" |
| 1355 | << (*PI)->getNumber() << '@' << PEnd |
| 1356 | << "\nValno #" << VNI->id << " live into BB#" << MFI->getNumber() |
| 1357 | << '@' << LiveInts->getMBBStartIdx(MFI) << " in " << LI << '\n'; |
| 1358 | } |
| 1359 | } |
| 1360 | if (&*MFI == EndMBB) |
| 1361 | break; |
| 1362 | ++MFI; |
| 1363 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1364 | } |
Jakob Stoklund Olesen | 501dc42 | 2010-10-26 22:36:07 +0000 | [diff] [blame] | 1365 | |
| 1366 | // Check the LI only has one connected component. |
Jakob Stoklund Olesen | 8c593f9 | 2010-10-27 00:39:01 +0000 | [diff] [blame] | 1367 | if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { |
| 1368 | ConnectedVNInfoEqClasses ConEQ(*LiveInts); |
| 1369 | unsigned NumComp = ConEQ.Classify(&LI); |
| 1370 | if (NumComp > 1) { |
| 1371 | report("Multiple connected components in live interval", MF); |
| 1372 | *OS << NumComp << " components in " << LI << '\n'; |
Jakob Stoklund Olesen | cb36777 | 2010-10-29 00:40:57 +0000 | [diff] [blame] | 1373 | for (unsigned comp = 0; comp != NumComp; ++comp) { |
| 1374 | *OS << comp << ": valnos"; |
| 1375 | for (LiveInterval::const_vni_iterator I = LI.vni_begin(), |
| 1376 | E = LI.vni_end(); I!=E; ++I) |
| 1377 | if (comp == ConEQ.getEqClass(*I)) |
| 1378 | *OS << ' ' << (*I)->id; |
| 1379 | *OS << '\n'; |
| 1380 | } |
Jakob Stoklund Olesen | 8c593f9 | 2010-10-27 00:39:01 +0000 | [diff] [blame] | 1381 | } |
Jakob Stoklund Olesen | 501dc42 | 2010-10-26 22:36:07 +0000 | [diff] [blame] | 1382 | } |
Jakob Stoklund Olesen | 58e1248 | 2010-08-06 18:04:19 +0000 | [diff] [blame] | 1383 | } |
| 1384 | } |