[llvm] [AArch64] Prevent the AArch64LoadStoreOptimizer from reordering CFI instructions (PR #101317)
Momchil Velikov via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 5 10:06:16 PDT 2024
================
@@ -1971,20 +1976,37 @@ maybeMoveCFI(MachineInstr &MI, MachineBasicBlock::iterator MaybeCFI) {
}
}
-MachineBasicBlock::iterator
-AArch64LoadStoreOpt::mergeUpdateInsn(MachineBasicBlock::iterator I,
- MachineBasicBlock::iterator Update,
- bool IsPreIdx) {
+std::optional<MachineBasicBlock::iterator> AArch64LoadStoreOpt::mergeUpdateInsn(
+ MachineBasicBlock::iterator I, MachineBasicBlock::iterator Update,
+ bool IsForward, bool IsPreIdx, bool MergeEither) {
assert((Update->getOpcode() == AArch64::ADDXri ||
Update->getOpcode() == AArch64::SUBXri) &&
"Unexpected base register update instruction to merge!");
MachineBasicBlock::iterator E = I->getParent()->end();
MachineBasicBlock::iterator NextI = next_nodbg(I, E);
- // If updating the SP and the following instruction is CFA offset related CFI
- // instruction move it after the merged instruction.
- MachineBasicBlock::iterator CFI =
- IsPreIdx ? maybeMoveCFI(*Update, next_nodbg(Update, E)) : E;
+ // If updating the SP and the following instruction is CFA offset related CFI,
+ // make sure the CFI follows the SP update either by merging at the location
+ // of the update or by moving the CFI after the merged instruction. If unable
+ // to do so, bail.
+ MachineBasicBlock::iterator InsertPt = I;
+ if (IsForward) {
+ assert(IsPreIdx);
+ if (auto CFI = maybeMoveCFI(*Update, next_nodbg(Update, E)); CFI != E) {
+ if (MergeEither) {
+ InsertPt = Update;
+ } else {
+ // Take care not to reorder CFIs.
+ if (std::any_of(std::next(CFI), I, [](const auto &Insn) {
+ return Insn.getOpcode() == TargetOpcode::CFI_INSTRUCTION;
+ }))
+ return std::nullopt;
----------------
momchil-velikov wrote:
Added tests.
https://github.com/llvm/llvm-project/pull/101317
More information about the llvm-commits
mailing list