This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner][AArch64][VE] Teach BuildUDIV/SDIV to use 2x mul when mulh/mul_lohi are not available.
ClosedPublic

Authored by craig.topper on May 10 2023, 5:22 PM.

Details

Summary

Correct the legality of i32 mul_lohi on AArch64.

Previously, AArch64 incorrectly reported i32 mul_lohi as Legal.
This allowed BuildUDIV/SDIV to use them. A later DAGCombiner would
replace them with MULHS/MULHU because only the high half was used.
This conversion does not check the legality of MULHS/MULHU under
the assumption that LegalizeDAG can turn it back into MUL_LOHI later.

After they are converted to MULHS/MULHU, DAGCombine ran and saw that
these operations aren't supported but an i64 MUL is. So they get
converted to that plus a shift. Without this, LegalizeDAG would
convert back MUL_LOHI and isel would fail to find a pattern.

This patch teaches BuildUDIV/SDIV to create the wide mul and shift
so that we can report the correct operation legality on AArch64. It
also enables div by constant folding for more cases on VE.

I don't know if VE wants this div by constant optimization or not. If they
don't want it, they can use the isIntDivCheap hook to disable it.

Diff Detail

Event Timeline

craig.topper created this revision.May 10 2023, 5:22 PM
craig.topper requested review of this revision.May 10 2023, 5:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2023, 5:22 PM
craig.topper edited the summary of this revision. (Show Details)May 10 2023, 5:27 PM

SGTM - probably need to wait for VE review though

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
5996

We now have a few cases where we're doing this - we already have widenIntegerVectorElementType but for some reason don't have an equivalent widenIntegerScalarType

SGTM - probably need to wait for VE review though

I might have missed something but looking at commit log, I think VE development has been inactive in 2023.

RKSimon accepted this revision.May 12 2023, 8:32 AM

LGTM then, but please raise a bug for the missing VE isIntDivCheap handling

This revision is now accepted and ready to land.May 12 2023, 8:32 AM

This change broke the test CodeGen/M68k/Arith/divide-by-constant.ll on M68k:

FAIL: LLVM :: CodeGen/M68k/Arith/divide-by-constant.ll (11158 of 65166)
******************** TEST 'LLVM :: CodeGen/M68k/Arith/divide-by-constant.ll' FAILED ********************
Script:
--
: 'RUN: at line 2';   /srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/bin/llc < /srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll -mtriple=m68k-linux -verify-machineinstrs | /srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/stage1/bin/FileCheck /srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll
--
Exit Code: 1

Command Output (stderr):
--
/srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll:42:15: error: CHECK-NEXT: expected string not found in input
; CHECK-NEXT: mulu #21846, %d0
              ^
<stdin>:42:17: note: scanning from here
 and.l #255, %d0
                ^
<stdin>:43:2: note: possible intended match here
 muls #171, %d0
 ^
/srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll:128:15: error: CHECK-NEXT: is not on the line after the previous match
; CHECK-NEXT: and.l #255, %d0
              ^
<stdin>:132:2: note: 'next' match was here
 and.l #255, %d0
 ^
<stdin>:130:21: note: previous match ended here
 move.b (7,%sp), %d0
                    ^
<stdin>:131:1: note: non-matching line after previous match is here
 lsr.b #1, %d0
^
/srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll:142:15: error: CHECK-NEXT: is not on the line after the previous match
; CHECK-NEXT: and.l #255, %d0
              ^
<stdin>:148:2: note: 'next' match was here
 and.l #255, %d0
 ^
<stdin>:146:21: note: previous match ended here
 move.b (7,%sp), %d0
                    ^
<stdin>:147:1: note: non-matching line after previous match is here
 lsr.b #2, %d0
^

Input file: <stdin>
Check file: /srv/buildbot/workers/suse-gary-m68k-cross/clang-m68k-linux-cross/llvm/llvm/test/CodeGen/M68k/Arith/divide-by-constant.ll

-dump-input=help explains the following input dump.

Input was:
<<<<<<
           .
           .
           .
          37:  .type test3,@function 
          38: test3: ; @test3 
          39:  .cfi_startproc 
          40: ; %bb.0: ; %entry 
          41:  move.b (11,%sp), %d0 
          42:  and.l #255, %d0 
next:42'0                     X error: no match found
          43:  muls #171, %d0 
next:42'0     ~~~~~~~~~~~~~~~~
next:42'1      ?               possible intended match
          44:  move.w #9, %d1 
next:42'0     ~~~~~~~~~~~~~~~~
          45:  lsr.w %d1, %d0 
next:42'0     ~~~~~~~~~~~~~~~~
          46:  and.l #65535, %d0 
next:42'0     ~~~~~~~~~~~~~~~~~~~
          47:  rts 
next:42'0     ~~~~~
          48: .Lfunc_end2: 
next:42'0     ~~~~~~~~~~~~~
           .
           .
           .
         127:  .type test8,@function
         128: test8: ; @test8 
         129: ; %bb.0: 
         130:  move.b (7,%sp), %d0 
         131:  lsr.b #1, %d0 
         132:  and.l #255, %d0 
next:128       !~~~~~~~~~~~~~~  error: match on wrong line
         133:  muls #211, %d0 
         134:  move.w #13, %d1 
         135:  lsr.w %d1, %d0 
         136:  ; kill: def $bd0 killed $bd0 killed $d0 
         137:  rts 
           .
           .
           .
         143:  .type test9,@function 
         144: test9: ; @test9 
         145: ; %bb.0: 
         146:  move.b (7,%sp), %d0 
         147:  lsr.b #2, %d0 
         148:  and.l #255, %d0 
next:142       !~~~~~~~~~~~~~~  error: match on wrong line
         149:  muls #71, %d0 
         150:  move.w #11, %d1 
         151:  lsr.w %d1, %d0 
         152:  ; kill: def $bd0 killed $bd0 killed $d0 
         153:  rts 
           .
           .
           .
>>>>>>

--

********************

See: https://lab.llvm.org/buildbot/#/builders/192/builds/1874