LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 51476 - [AArch64] Peephole pass incorrectly removes comparison
Summary: [AArch64] Peephole pass incorrectly removes comparison
Status: RESOLVED FIXED
Alias: None
Product: libraries
Classification: Unclassified
Component: Backend: AArch64 (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned LLVM Bugs
URL:
Keywords:
Depends on:
Blocks: release-13.0.1
  Show dependency tree
 
Reported: 2021-08-14 02:43 PDT by Nikita Popov
Modified: 2021-10-11 20:29 PDT (History)
5 users (show)

See Also:
Fixed By Commit(s): 81b106584f2baf33e09be2362c35c1bf2f6bfe94 b643ee1b9c1a


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nikita Popov 2021-08-14 02:43:40 PDT
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
target triple = "aarch64-unknown-linux-gnu"

define void @test(i8 %arg) {
  %tmp = alloca i8
  %cmp1 = icmp ne i8 %arg, 1
  %zext = zext i1 %cmp1 to i8
  store i8 %zext, i8* %tmp
  %zext2 = load i8, i8* %tmp
  %cmp2 = icmp eq i8 %zext2, 3
  br i1 %cmp2, label %exit, label %do_call

do_call:
  call void @unknown(i8 %zext2)
  ret void

exit:
  ret void
}

declare void @unknown(i8)

Running through llc produces:

// %bb.0:
  str x30, [sp, #-16]!                // 8-byte Folded Spill
  .cfi_def_cfa_offset 16
  .cfi_offset w30, -16
  and w8, w0, #0xff
  cmp w8, #1
  cset  w0, ne
  strb  w0, [sp, #12]
  b.ne  .LBB0_2
// %bb.1:                               // %do_call
  bl  unknown
.LBB0_2:                                // %common.ret
  ldr x30, [sp], #16                  // 8-byte Folded Reload
  ret

Note how the final comparison to #3 has now reuses the result of the comparison to #1.

This is caused by the new optimization introduced in https://reviews.llvm.org/D98564. From a cursory reading, this optimization was written under the assumption that CmpValue==1 means that the comparison is against #1, while analyzeCompare() canonicalizes the comparison such that CmpValue==1 indicates a comparison against any non-zero value.
Comment 1 Nikita Popov 2021-08-14 14:51:59 PDT
Candidate patch: https://reviews.llvm.org/D108076
Comment 2 Nikita Popov 2021-08-15 03:54:31 PDT
Fixed by https://github.com/llvm/llvm-project/commit/81b106584f2baf33e09be2362c35c1bf2f6bfe94. Keeping this open to track release/13.x backport.
Comment 3 Tom Stellard 2021-08-18 20:08:12 PDT
Merged: b643ee1b9c1a