From ee83cda2e46fd8d4602dca1917a9b8b817dc0784 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Tue, 14 Apr 2020 14:50:03 -0700 Subject: [PATCH] [AArch64InstrInfo] Ignore debug insts in areCFlagsAccessedBetweenInstrs [7/14] Summary: Fix an issue where the presence of debug info could disable a peephole optimization due to areCFlagsAccessedBetweenInstrs returning the wrong result. In test/CodeGen/AArch64/arm64-csel.ll, the issue was found in the function @foo5, in which the first compare could successfully be optimized but not the second. Reviewers: t.p.northover, eastig, paquette Subscribers: kristof.beyls, hiraditya, danielkiss, aprantl, dsanders, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D78157 --- lib/CodeGen/PeepholeOptimizer.cpp | 1 + lib/Target/AArch64/AArch64InstrInfo.cpp | 7 +++---- test/CodeGen/AArch64/arm64-csel.ll | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/CodeGen/PeepholeOptimizer.cpp b/lib/CodeGen/PeepholeOptimizer.cpp index b9e8c692eea..4a66863ea80 100644 --- a/lib/CodeGen/PeepholeOptimizer.cpp +++ b/lib/CodeGen/PeepholeOptimizer.cpp @@ -616,6 +616,7 @@ bool PeepholeOptimizer::optimizeCmpInstr(MachineInstr &MI) { // Attempt to optimize the comparison instruction. LLVM_DEBUG(dbgs() << "Attempting to optimize compare: " << MI); if (TII->optimizeCompareInstr(MI, SrcReg, SrcReg2, CmpMask, CmpValue, MRI)) { + LLVM_DEBUG(dbgs() << " -> Successfully optimized compare!\n"); ++NumCmps; return true; } diff --git a/lib/Target/AArch64/AArch64InstrInfo.cpp b/lib/Target/AArch64/AArch64InstrInfo.cpp index cd52d04385e..4a7c3cc1301 100644 --- a/lib/Target/AArch64/AArch64InstrInfo.cpp +++ b/lib/Target/AArch64/AArch64InstrInfo.cpp @@ -1183,10 +1183,9 @@ static bool areCFlagsAccessedBetweenInstrs( return MI.getIterator() == From; }) != To->getParent()->rend()); - // We iterate backward starting \p To until we hit \p From. - for (--To; To != From; --To) { - const MachineInstr &Instr = *To; - + // We iterate backward starting at \p To until we hit \p From. + for (const MachineInstr &Instr : + reversedInstructionsWithoutDebug(std::prev(To), From)) { if (((AccessToCheck & AK_Write) && Instr.modifiesRegister(AArch64::NZCV, TRI)) || ((AccessToCheck & AK_Read) && Instr.readsRegister(AArch64::NZCV, TRI))) diff --git a/test/CodeGen/AArch64/arm64-csel.ll b/test/CodeGen/AArch64/arm64-csel.ll index 32d3119bcce..f031710a4dc 100644 --- a/test/CodeGen/AArch64/arm64-csel.ll +++ b/test/CodeGen/AArch64/arm64-csel.ll @@ -1,4 +1,4 @@ -; RUN: llc -O3 < %s | FileCheck %s +; RUN: llc -debugify-and-strip-all-safe -O3 < %s | FileCheck %s target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-n32:64" target triple = "arm64-unknown-unknown"