diff --git a/llvm/include/llvm/CodeGen/TargetLowering.h b/llvm/include/llvm/CodeGen/TargetLowering.h --- a/llvm/include/llvm/CodeGen/TargetLowering.h +++ b/llvm/include/llvm/CodeGen/TargetLowering.h @@ -2959,6 +2959,9 @@ /// Check whether or not \p MI needs to be moved close to its uses. virtual bool shouldLocalize(const MachineInstr &MI, const TargetTransformInfo *TTI) const; + /// GlobalISel version of TargetLowering::AdjustInstrPostInstrSelection. + /// Returns true if the MIR was modified. + virtual bool adjustInstrPostGISel(MachineInstr &MI) const; private: const TargetMachine &TM; diff --git a/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp b/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp --- a/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp +++ b/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp @@ -133,6 +133,9 @@ // Keep track of selected blocks, so we can delete unreachable ones later. DenseSet SelectedBlocks; + // List of newly added MachineInstrs during selection. + SmallVector InsertedMIs; + for (MachineBasicBlock *MBB : post_order(&MF)) { ISel->CurMBB = MBB; SelectedBlocks.insert(MBB); @@ -144,10 +147,8 @@ bool ReachedBegin = false; for (auto MII = std::prev(MBB->end()), Begin = MBB->begin(); !ReachedBegin;) { -#ifndef NDEBUG - // Keep track of the insertion range for debug printing. + const auto AfterIt = std::next(MII); -#endif // Select this instruction. MachineInstr &MI = *MII; @@ -193,14 +194,13 @@ return false; } - // Dump the range of instructions that MI expanded into. - LLVM_DEBUG({ - auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII); - dbgs() << "Into:\n"; - for (auto &InsertedMI : make_range(InsertedBegin, AfterIt)) - dbgs() << " " << InsertedMI; - dbgs() << '\n'; - }); + auto InsertedBegin = ReachedBegin ? MBB->begin() : std::next(MII); + LLVM_DEBUG(dbgs() << "Into:\n"); + for (auto &InsertedMI : make_range(InsertedBegin, AfterIt)) { + InsertedMIs.push_back(&InsertedMI); + LLVM_DEBUG(dbgs() << " " << InsertedMI); + } + LLVM_DEBUG(dbgs() << '\n'); } } @@ -304,6 +304,16 @@ auto &TLI = *MF.getSubtarget().getTargetLowering(); TLI.finalizeLowering(MF); + for (MachineInstr *MI : InsertedMIs) { + if (MI->hasPostISelHook()) { + LLVM_DEBUG(dbgs() << "hasPostISelHook handling of:\n " << *MI); + bool Modified = TLI.adjustInstrPostGISel(*MI); + (void)Modified; + LLVM_DEBUG(if (Modified) dbgs() << "Modified To:\n " << *MI; + else dbgs() << "Didn't modify\n";); + } + } + LLVM_DEBUG({ dbgs() << "Rules covered by selecting function: " << MF.getName() << ":"; for (auto RuleID : CoverageInfo.covered()) diff --git a/llvm/lib/CodeGen/TargetLoweringBase.cpp b/llvm/lib/CodeGen/TargetLoweringBase.cpp --- a/llvm/lib/CodeGen/TargetLoweringBase.cpp +++ b/llvm/lib/CodeGen/TargetLoweringBase.cpp @@ -2361,3 +2361,9 @@ } } } + +bool TargetLoweringBase::adjustInstrPostGISel(MachineInstr &MI) const { + // TODO: Add assertion for MI.hasPostISelHook() once every global-isel target + // overrides this function. + return false; +}