Index: lib/CodeGen/GlobalISel/Legalizer.cpp =================================================================== --- lib/CodeGen/GlobalISel/Legalizer.cpp +++ lib/CodeGen/GlobalISel/Legalizer.cpp @@ -173,10 +173,8 @@ // and are assumed to be legal. if (!isPreISelGenericOpcode(MI->getOpcode())) continue; - unsigned NumNewInsns = 0; SmallVector WorkList; Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) { - ++NumNewInsns; WorkList.push_back(MI); }); WorkList.push_back(&*MI); @@ -185,30 +183,48 @@ LegalizerHelper::LegalizeResult Res; unsigned Idx = 0; do { - Res = Helper.legalizeInstrStep(*WorkList[Idx]); + MachineInstr &CurrMI = *WorkList[Idx]; +#ifndef NDEBUG + // Record the instructions either side of the current instruction so we + // can iterate over its replacements (even after CurrMI has been erased). + // PrintFrom is set to MBB.end() if there is no instruction before CurrMI. + MachineBasicBlock::iterator PrintFrom = MBB.end(); + if (CurrMI != MBB.begin()) { + PrintFrom = CurrMI; + --PrintFrom; + } + MachineBasicBlock::iterator PrintTo = + std::next(MachineBasicBlock::iterator(CurrMI)); +#endif // NDEBUG + + Res = Helper.legalizeInstrStep(CurrMI); // Error out if we couldn't legalize this instruction. We may want to // fall back to DAG ISel instead in the future. if (Res == LegalizerHelper::UnableToLegalize) { Helper.MIRBuilder.stopRecordingInsertions(); if (Res == LegalizerHelper::UnableToLegalize) { reportGISelFailure(MF, TPC, MORE, "gisel-legalize", - "unable to legalize instruction", - *WorkList[Idx]); + "unable to legalize instruction", CurrMI); return false; } } Changed |= Res == LegalizerHelper::Legalized; - ++Idx; #ifndef NDEBUG - if (NumNewInsns) { - DEBUG(dbgs() << ".. .. Emitted " << NumNewInsns << " insns\n"); - for (auto I = WorkList.end() - NumNewInsns, E = WorkList.end(); - I != E; ++I) - DEBUG(dbgs() << ".. .. New MI: "; (*I)->print(dbgs())); - NumNewInsns = 0; - } -#endif + // Now that the current instruction has been erased (if it was going to + // be) advancing PrintFrom will select it again if we didn't erase it, + // select PrintTo if we erased it but emitted zero instructions, or the + // first newly created instruction. + if (PrintFrom == MBB.end()) + PrintFrom = MBB.begin(); + else + ++PrintFrom; + + for (auto I = PrintFrom, E = PrintTo; I != E; ++I) + DEBUG(dbgs() << ".. .. MI: "; I->print(dbgs())); +#endif // NDEBUG + + ++Idx; } while (Idx < WorkList.size()); Helper.MIRBuilder.stopRecordingInsertions();