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