@@ -1691,6 +1691,47 @@ void PreRARematStage::collectRematerializableInstructions() {
16911691 if (Def->getParent () == UseI->getParent ())
16921692 continue ;
16931693
1694+ bool HasRematDependency = false ;
1695+ // Check if this instruction uses any registers that are planned to be
1696+ // rematerialized
1697+ for (auto &RematEntry : RematerializableInsts) {
1698+ if (find_if (RematEntry.second ,
1699+ [&Def](std::pair<MachineInstr *, MachineInstr *> &Remat) {
1700+ for (MachineOperand &MO : Def->operands ()) {
1701+ if (!MO.isReg ())
1702+ continue ;
1703+ if (MO.getReg () == Remat.first ->getOperand (0 ).getReg ())
1704+ return true ;
1705+ }
1706+ return false ;
1707+ }) != RematEntry.second .end ()) {
1708+ HasRematDependency = true ;
1709+ break ;
1710+ }
1711+ }
1712+ // Do not rematerialize an instruction if it uses an instruction that we
1713+ // have designated for rematerialization.
1714+ // FIXME: Allow for rematerialization chains: this requires 1. updating
1715+ // remat points to account for uses that are rematerialized, and 2. either
1716+ // rematerializing the candidates in careful ordering, or deferring the MBB
1717+ // RP walk until the entire chain has been rematerialized.
1718+ if (HasRematDependency)
1719+ continue ;
1720+
1721+ // Similarly, check if the UseI is planned to be remat.
1722+ for (auto &RematEntry : RematerializableInsts) {
1723+ if (find_if (RematEntry.second ,
1724+ [&UseI](std::pair<MachineInstr *, MachineInstr *> &Remat) {
1725+ return Remat.first == UseI;
1726+ }) != RematEntry.second .end ()) {
1727+ HasRematDependency = true ;
1728+ break ;
1729+ }
1730+ }
1731+
1732+ if (HasRematDependency)
1733+ break ;
1734+
16941735 // We are only collecting defs that are defined in another block and are
16951736 // live-through or used inside regions at MinOccupancy. This means that the
16961737 // register must be in the live-in set for the region.
0 commit comments