Skip to content

Commit b8cf84d

Browse files
committed
[GlobalISel] Allow some load/store instructions to be folded in Match Table backend
Load/store instruction can be folded into non-adjacent instruction within the same basic block, if there are no other instructions with memory/other side effects between them.
1 parent 6be0e97 commit b8cf84d

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

llvm/lib/CodeGen/GlobalISel/GIMatchTableExecutor.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ bool GIMatchTableExecutor::isObviouslySafeToFold(MachineInstr &MI,
7070
if (MI.isConvergent() && MI.getParent() != IntoMI.getParent())
7171
return false;
7272

73-
return !MI.mayLoadOrStore() && !MI.mayRaiseFPException() &&
74-
!MI.hasUnmodeledSideEffects() && MI.implicit_operands().empty();
73+
auto IsSafe = [](const MachineInstr &MI) {
74+
return !MI.mayRaiseFPException() && !MI.hasUnmodeledSideEffects() &&
75+
MI.implicit_operands().empty();
76+
};
77+
auto IsSafeNoMem = [IsSafe](const MachineInstr &MI) {
78+
return !MI.mayLoadOrStore() && IsSafe(MI);
79+
};
80+
81+
// If source instruction uses memory, fold if no intermediate
82+
// instructions use it.
83+
if (MI.mayLoadOrStore() && IsSafe(MI) &&
84+
MI.getParent() == IntoMI.getParent()) {
85+
auto IntoIt = IntoMI.getIterator();
86+
auto NextIt = std::next(MI.getIterator());
87+
while (!NextIt.isEnd() && NextIt != IntoIt && IsSafeNoMem(*NextIt))
88+
++NextIt;
89+
if (NextIt == IntoIt)
90+
return true;
91+
}
92+
93+
return IsSafeNoMem(MI);
7594
}

0 commit comments

Comments
 (0)