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