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