From bf4c70b355620fec56df5c22a5704dea1e5b3b54 Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Wed, 15 Apr 2020 17:49:53 -0700 Subject: [PATCH] [AArch64ConditionOptimizer] Fix missed optimization due to debug insts [11/14] Summary: The findSuitableCompare method can fail if debug instructions are present in the MBB -- fix this by using helpers to skip over debug insts. Reviewers: aemerson, paquette Subscribers: kristof.beyls, hiraditya, danielkiss, aprantl, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D78265 --- .../Target/AArch64/AArch64ConditionOptimizer.cpp | 27 +++++++++++----------- .../CodeGen/AArch64/combine-comparisons-by-cse.ll | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp index 894be8b..a377f5c 100644 --- a/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp +++ b/llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp @@ -158,32 +158,33 @@ MachineInstr *AArch64ConditionOptimizer::findSuitableCompare( return nullptr; // Now find the instruction controlling the terminator. - for (MachineBasicBlock::iterator B = MBB->begin(); I != B;) { - --I; - assert(!I->isTerminator() && "Spurious terminator"); + auto B = MBB->begin(); + for (MachineInstr &I : + reversedInstructionsWithoutDebug(I == B ? I : std::prev(I), B)) { + assert(!I.isTerminator() && "Spurious terminator"); // Check if there is any use of NZCV between CMP and Bcc. - if (I->readsRegister(AArch64::NZCV)) + if (I.readsRegister(AArch64::NZCV)) return nullptr; - switch (I->getOpcode()) { + switch (I.getOpcode()) { // cmp is an alias for subs with a dead destination register. case AArch64::SUBSWri: case AArch64::SUBSXri: // cmn is an alias for adds with a dead destination register. case AArch64::ADDSWri: case AArch64::ADDSXri: { - unsigned ShiftAmt = AArch64_AM::getShiftValue(I->getOperand(3).getImm()); - if (!I->getOperand(2).isImm()) { - LLVM_DEBUG(dbgs() << "Immediate of cmp is symbolic, " << *I << '\n'); + unsigned ShiftAmt = AArch64_AM::getShiftValue(I.getOperand(3).getImm()); + if (!I.getOperand(2).isImm()) { + LLVM_DEBUG(dbgs() << "Immediate of cmp is symbolic, " << I << '\n'); return nullptr; - } else if (I->getOperand(2).getImm() << ShiftAmt >= 0xfff) { - LLVM_DEBUG(dbgs() << "Immediate of cmp may be out of range, " << *I + } else if (I.getOperand(2).getImm() << ShiftAmt >= 0xfff) { + LLVM_DEBUG(dbgs() << "Immediate of cmp may be out of range, " << I << '\n'); return nullptr; - } else if (!MRI->use_empty(I->getOperand(0).getReg())) { - LLVM_DEBUG(dbgs() << "Destination of cmp is not dead, " << *I << '\n'); + } else if (!MRI->use_nodbg_empty(I.getOperand(0).getReg())) { + LLVM_DEBUG(dbgs() << "Destination of cmp is not dead, " << I << '\n'); return nullptr; } - return &*I; + return &I; } // Prevent false positive case like: // cmp w19, #0 diff --git a/llvm/test/CodeGen/AArch64/combine-comparisons-by-cse.ll b/llvm/test/CodeGen/AArch64/combine-comparisons-by-cse.ll index 86be3cc..e7c6e3b 100644 --- a/llvm/test/CodeGen/AArch64/combine-comparisons-by-cse.ll +++ b/llvm/test/CodeGen/AArch64/combine-comparisons-by-cse.ll @@ -1,4 +1,4 @@ -; RUN: llc < %s -mtriple=aarch64-linux-gnu | FileCheck %s +; RUN: llc -debugify-and-strip-all-safe < %s -mtriple=aarch64-linux-gnu | FileCheck %s ; marked as external to prevent possible optimizations @a = external global i32 -- 2.7.4