For remainder: If (1 << (Bitwidth / 2)) % Divisor == 1, we can add the high and low halves together and use a (Bitwidth / 2) urem. If (BitWidth /2) is a legal integer type, this urem will be expand by DAGCombiner using multiply by magic constant. We do have to take into account that adding high and low together can produce a carry, making it a (BitWidth / 2)+1 bit number. So we need to also add back in the carry from the first addition. For division: We can use the above trick to compute the remainder, subtract that remainder from the dividend, then multiply by the multiplicative inverse of the Divisor modulo (1 << BitWidth). This is based on the section "Remainder by Summing Digits" in Hacker's delight. The remainder trick is similar to a trick you may have learned for determining if a decimal number is divisible by 3. You can add all the digits together and see if the sum is divisible by 3. If you're not sure if the sum is divisible by 3, you can add its digits together. This can be repeated until you have a single decimal digit. If that digit is 3, 6, or 9, then the original number is divisible by 3. This works because 10 % 3 == 1. gcc already does this same trick. There are additional tricks gcc does urem as well as srem, udiv, and sdiv that I plan to add in future patches. Reviewed By: RKSimon Differential Revision: https://reviews.llvm.org/D130862
137 lines
3.5 KiB
LLVM
137 lines
3.5 KiB
LLVM
; RUN: llc < %s -mtriple=arm-apple-ios -mcpu=cortex-a8 | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-SWDIV
|
|
; RUN: llc < %s -mtriple=arm-apple-ios -mcpu=swift | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-HWDIV
|
|
; RUN: llc < %s -mtriple=arm-apple-ios -mcpu=cortex-r4 | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-SWDIV
|
|
; RUN: llc < %s -mtriple=arm-apple-ios -mcpu=cortex-r4f | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-SWDIV
|
|
; RUN: llc < %s -mtriple=arm-apple-ios -mcpu=cortex-r5 | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-HWDIV
|
|
; RUN: llc < %s -mtriple=arm-none-eabi -mcpu=cortex-a8 | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-EABI
|
|
; RUN: llc < %s -mtriple=armv7ve-none-linux-gnu | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-HWDIV
|
|
; RUN: llc < %s -mtriple=thumbv7ve-none-linux-gnu | \
|
|
; RUN: FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-HWDIV \
|
|
; RUN: -check-prefix=CHECK-THUMB
|
|
|
|
define i32 @f1(i32 %a, i32 %b) {
|
|
entry:
|
|
; CHECK-LABEL: f1
|
|
; CHECK-SWDIV: __divsi3
|
|
|
|
; CHECK-THUMB: .thumb_func
|
|
; CHECK-HWDIV: sdiv
|
|
|
|
; CHECK-EABI: __aeabi_idiv
|
|
%tmp1 = sdiv i32 %a, %b ; <i32> [#uses=1]
|
|
ret i32 %tmp1
|
|
}
|
|
|
|
define i32 @f2(i32 %a, i32 %b) {
|
|
entry:
|
|
; CHECK-LABEL: f2
|
|
; CHECK-SWDIV: __udivsi3
|
|
|
|
; CHECK-THUMB: .thumb_func
|
|
; CHECK-HWDIV: udiv
|
|
|
|
; CHECK-EABI: __aeabi_uidiv
|
|
%tmp1 = udiv i32 %a, %b ; <i32> [#uses=1]
|
|
ret i32 %tmp1
|
|
}
|
|
|
|
define i32 @f3(i32 %a, i32 %b) {
|
|
entry:
|
|
; CHECK-LABEL: f3
|
|
; CHECK-SWDIV: __modsi3
|
|
|
|
; CHECK-THUMB: .thumb_func
|
|
; CHECK-HWDIV: sdiv
|
|
; CHECK-HWDIV: mls
|
|
|
|
; EABI MODE = Remainder in R1, quotient in R0
|
|
; CHECK-EABI: __aeabi_idivmod
|
|
; CHECK-EABI-NEXT: mov r0, r1
|
|
%tmp1 = srem i32 %a, %b ; <i32> [#uses=1]
|
|
ret i32 %tmp1
|
|
}
|
|
|
|
define i32 @f4(i32 %a, i32 %b) {
|
|
entry:
|
|
; CHECK-LABEL: f4
|
|
; CHECK-SWDIV: __umodsi3
|
|
|
|
; CHECK-THUMB: .thumb_func
|
|
; CHECK-HWDIV: udiv
|
|
; CHECK-HWDIV: mls
|
|
|
|
; EABI MODE = Remainder in R1, quotient in R0
|
|
; CHECK-EABI: __aeabi_uidivmod
|
|
; CHECK-EABI-NEXT: mov r0, r1
|
|
%tmp1 = urem i32 %a, %b ; <i32> [#uses=1]
|
|
ret i32 %tmp1
|
|
}
|
|
|
|
|
|
define i64 @f5(i64 %a, i64 %b) {
|
|
entry:
|
|
; CHECK-LABEL: f5
|
|
; CHECK-SWDIV: __moddi3
|
|
|
|
; CHECK-HWDIV: __moddi3
|
|
|
|
; EABI MODE = Remainder in R2-R3, quotient in R0-R1
|
|
; CHECK-EABI: __aeabi_ldivmod
|
|
; CHECK-EABI-NEXT: mov r0, r2
|
|
; CHECK-EABI-NEXT: mov r1, r3
|
|
%tmp1 = srem i64 %a, %b ; <i64> [#uses=1]
|
|
ret i64 %tmp1
|
|
}
|
|
|
|
define i64 @f6(i64 %a, i64 %b) {
|
|
entry:
|
|
; CHECK-LABEL: f6
|
|
; CHECK-SWDIV: __umoddi3
|
|
|
|
; CHECK-HWDIV: __umoddi3
|
|
|
|
; EABI MODE = Remainder in R2-R3, quotient in R0-R1
|
|
; CHECK-EABI: __aeabi_uldivmod
|
|
; CHECK-EABI-NEXT: mov r0, r2
|
|
; CHECK-EABI-NEXT: mov r1, r3
|
|
%tmp1 = urem i64 %a, %b ; <i64> [#uses=1]
|
|
ret i64 %tmp1
|
|
}
|
|
|
|
; Make sure we avoid a libcall for some constants.
|
|
define i64 @f7(i64 %a) {
|
|
; CHECK-SWDIV-LABEL: f7
|
|
; CHECK-SWDIV: adc
|
|
; CHECK-SWDIV: umull
|
|
; CHECK-HWDIV-LABEL: f7
|
|
; CHECK-HWDIV: adc
|
|
; CHECK-HWDIV: umull
|
|
; CHECK-EABI-LABEL: f7
|
|
; CHECK-EABI: adc
|
|
; CHECK-EABI: umull
|
|
%tmp1 = urem i64 %a, 3
|
|
ret i64 %tmp1
|
|
}
|
|
|
|
; Make sure we avoid a libcall for some constants.
|
|
define i64 @f8(i64 %a) {
|
|
; CHECK-SWDIV-LABEL: f8
|
|
; CHECK-SWDIV: adc
|
|
; CHECK-SWDIV: umull
|
|
; CHECK-HWDIV-LABEL: f8
|
|
; CHECK-HWDIV: adc
|
|
; CHECK-HWDIV: umull
|
|
; CHECK-EABI-LABEL: f8
|
|
; CHECK-EABI: adc
|
|
; CHECK-EABI: umull
|
|
%tmp1 = udiv i64 %a, 3
|
|
ret i64 %tmp1
|
|
}
|