[PATCH] D54546: [DAGCombiner] Don't expand sdiv when optimising for minsize

Sjoerd Meijer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 16 09:37:24 PST 2018


SjoerdMeijer added a comment.

A little bit of progress:

> What happens for targets that don't have an sdiv instruction? (For example, thumbv6m.)

The Legalizer takes care of it and turns it into a call to `__aeabi_idiv`. But it doesn't look beneficial though:

  $ cat t.c
  int foo(int f) {
    return f / 4;
  }

before:

  0x00000000:    17c1        ASRS     r1,r0,#31
  0x00000002:    0f89        LSRS     r1,r1,#30
  0x00000004:    1840       ADDS     r0,r0,r1
  0x00000006:    1080       ASRS     r0,r0,#2
  0x00000008:    4770       BX       lr

and after:

  0x00000000:    b580        PUSH     {r7,lr}
  0x00000002:    2104        MOVS     r1,#4
  0x00000004:    f7fffffe    BL       __aeabi_idiv
  0x00000008:    bd80         POP      {r7,pc}

This isn't great in many ways: same size, it's slower, and we still need to pull in code for __aeabi_idiv (if it isn't already linked in). Easiest and best thing to do here is to expand SDIV as before. I will see if I can query if SDIV is legal, and add that as a check here.

For X86, this code path doesn't seem to be triggered at all and it is already generating an idiv:

  0:	89 f8                	mov    %edi,%eax
  2:	6a 05                	pushq  $0x5
  4:	59                   	pop    %rcx
  5:	99                   	cltd   
  6:	f7 f9                	idiv   %ecx
  8:	c3                   	retq   

>From a quick look for X86, it leaves the SDIV intact, because it disappears in function `BuildSDIVPow2 `on line 3233 just before my addition there,  queries `TLI.BuildSDIVPow2`, which returns this SDIV node.


https://reviews.llvm.org/D54546





More information about the llvm-commits mailing list