Skip to content

Commit

Permalink
[AArch64ConditionOptimizer] Fix missed optimization due to debug inst…
Browse files Browse the repository at this point in the history
…s [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
  • Loading branch information
vedantk committed Apr 23, 2020
1 parent 78d69e9 commit bf4c70b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
27 changes: 14 additions & 13 deletions llvm/lib/Target/AArch64/AArch64ConditionOptimizer.cpp
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down

0 comments on commit bf4c70b

Please sign in to comment.