llvm-project/llvm/lib/Support/DivisionByConstantInfo.cpp
MITSUNARI Shigeo 3e24a39357
[SelectionDAG] Optimize 32-bit udiv with 33-bit magic constants on 64-bit targets (#181288)
This PR optimizes 32-bit unsigned division by constants when the magic
constant is 33 bits (IsAdd=true case in UnsignedDivisionByConstantInfo)
on 64-bit targets.

## Overview

Compiler optimization for constant division of `uint32_t` variables
(such as `x / 7`) is based on the method
proposed by Granlund and Montgomery in 1994 (hereafter referred to as
the GM method).
However, the GM method for the IsAdd=true case was optimized for 32-bit
CPUs, not 64-bit CPUs.

This patch provides optimizations specifically for 64-bit CPUs (such as
x86_64 and Apple M-series).
A simple benchmark demonstrates over 60% speedup on both Intel Xeon and
Apple M4 processors.

## The GM Method

The GM method for `x / 7` can be expressed in C code as follows,
where the constants `c` and `a` are magic numbers determined by the
divisor:

```cpp
uint32_t udiv_original(uint32_t x) {
    uint64_t v = x * c;
    v >>= 32;
    uint32_t t = uint32_t(x) - uint32_t(v);
    t >>= 1;
    t += uint32_t(v);
    t >>= a - 33;
    return t;
}
```

For example, division by 7 on x86_64 generates 7 instructions:

```asm
movl    %edi, %eax
imulq   $613566757, %rax, %rax
shrq    $32, %rax
subl    %eax, %edi
shrl    %edi
addl    %edi, %eax
shrl    $2, %eax
```

## Proposed Solution

This patch generates the following optimized code:

```cpp
uint32_t udiv_optimized(uint32_t x) {
    uint128_t v = uint128_t(x) * ((c + 0x100000000) << (64 - a));
    return uint32_t(v >> 64);
}
```

Since a 64-bit right shift of a 128-bit variable extracts the upper 64
bits,
this code eliminates the need for shifts after multiplication.

The implementation pre-shifts the 33-bit magic constant `c = 2^32 +
Magic` left by `(64-a)` bits
and uses the high 64 bits of a 64 x 64 -> 128 bit multiplication
directly.
This eliminates the add/sub/shift sequence.

After optimization, division by 7 becomes 4 instructions (or 3 with
BMI2):

```asm
# Standard (4 instructions)
movl    %edi, %eax
movabsq $2635249153617166336, %rcx
mulq    %rcx
movq    %rdx, %rax

# With BMI2 (3 instructions)
movl    %edi, %edx
movabsq $2635249153617166336, %rax
mulxq   %rax, %rax, %rax
```
2026-03-06 15:18:34 -08:00

174 lines
5.9 KiB
C++

//===----- DivisionByConstantInfo.cpp - division by constant -*- C++ -*----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// This file implements support for optimizing divisions by a constant
///
//===----------------------------------------------------------------------===//
#include "llvm/Support/DivisionByConstantInfo.h"
using namespace llvm;
/// Calculate the magic numbers required to implement a signed integer division
/// by a constant as a sequence of multiplies, adds and shifts. Requires that
/// the divisor not be 0, 1, or -1. Taken from "Hacker's Delight", Henry S.
/// Warren, Jr., Chapter 10.
SignedDivisionByConstantInfo SignedDivisionByConstantInfo::get(const APInt &D) {
assert(!D.isZero() && "Precondition violation.");
// We'd be endlessly stuck in the loop.
assert(D.getBitWidth() >= 3 && "Does not work at smaller bitwidths.");
APInt Delta;
APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
struct SignedDivisionByConstantInfo Retval;
APInt AD = D.abs();
APInt T = SignedMin + (D.lshr(D.getBitWidth() - 1));
APInt ANC = T - 1 - T.urem(AD); // absolute value of NC
unsigned P = D.getBitWidth() - 1; // initialize P
APInt Q1, R1, Q2, R2;
// initialize Q1 = 2P/abs(NC); R1 = rem(2P,abs(NC))
APInt::udivrem(SignedMin, ANC, Q1, R1);
// initialize Q2 = 2P/abs(D); R2 = rem(2P,abs(D))
APInt::udivrem(SignedMin, AD, Q2, R2);
do {
P = P + 1;
Q1 <<= 1; // update Q1 = 2P/abs(NC)
R1 <<= 1; // update R1 = rem(2P/abs(NC))
if (R1.uge(ANC)) { // must be unsigned comparison
++Q1;
R1 -= ANC;
}
Q2 <<= 1; // update Q2 = 2P/abs(D)
R2 <<= 1; // update R2 = rem(2P/abs(D))
if (R2.uge(AD)) { // must be unsigned comparison
++Q2;
R2 -= AD;
}
// Delta = AD - R2
Delta = AD;
Delta -= R2;
} while (Q1.ult(Delta) || (Q1 == Delta && R1.isZero()));
Retval.Magic = std::move(Q2);
++Retval.Magic;
if (D.isNegative())
Retval.Magic.negate(); // resulting magic number
Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
return Retval;
}
/// Calculate the magic numbers required to implement an unsigned integer
/// division by a constant as a sequence of multiplies, adds and shifts.
/// Requires that the divisor not be 0. Taken from "Hacker's Delight", Henry
/// S. Warren, Jr., chapter 10.
/// LeadingZeros can be used to simplify the calculation if the upper bits
/// of the divided value are known zero.
UnsignedDivisionByConstantInfo
UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros,
bool AllowEvenDivisorOptimization,
bool AllowWidenOptimization) {
assert(!D.isZero() && !D.isOne() && "Precondition violation.");
assert(D.getBitWidth() > 1 && "Does not work at smaller bitwidths.");
APInt Delta;
struct UnsignedDivisionByConstantInfo Retval;
Retval.IsAdd = false; // initialize "add" indicator
Retval.Widen = false; // initialize widen indicator
APInt AllOnes =
APInt::getLowBitsSet(D.getBitWidth(), D.getBitWidth() - LeadingZeros);
APInt SignedMin = APInt::getSignedMinValue(D.getBitWidth());
APInt SignedMax = APInt::getSignedMaxValue(D.getBitWidth());
// Calculate NC, the largest dividend such that NC.urem(D) == D-1.
APInt NC = AllOnes - (AllOnes + 1 - D).urem(D);
assert(NC.urem(D) == D - 1 && "Unexpected NC value");
unsigned P = D.getBitWidth() - 1; // initialize P
APInt Q1, R1, Q2, R2;
// initialize Q1 = 2P/NC; R1 = rem(2P,NC)
APInt::udivrem(SignedMin, NC, Q1, R1);
// initialize Q2 = (2P-1)/D; R2 = rem((2P-1),D)
APInt::udivrem(SignedMax, D, Q2, R2);
do {
P = P + 1;
if (R1.uge(NC - R1)) {
// update Q1
Q1 <<= 1;
++Q1;
// update R1
R1 <<= 1;
R1 -= NC;
} else {
Q1 <<= 1; // update Q1
R1 <<= 1; // update R1
}
if ((R2 + 1).uge(D - R2)) {
if (Q2.uge(SignedMax))
Retval.IsAdd = true;
// update Q2
Q2 <<= 1;
++Q2;
// update R2
R2 <<= 1;
++R2;
R2 -= D;
} else {
if (Q2.uge(SignedMin))
Retval.IsAdd = true;
// update Q2
Q2 <<= 1;
// update R2
R2 <<= 1;
++R2;
}
// Delta = D - 1 - R2
Delta = D;
--Delta;
Delta -= R2;
} while (P < D.getBitWidth() * 2 &&
(Q1.ult(Delta) || (Q1 == Delta && R1.isZero())));
if (Retval.IsAdd && !D[0] && AllowEvenDivisorOptimization) {
unsigned PreShift = D.countr_zero();
APInt ShiftedD = D.lshr(PreShift);
Retval =
UnsignedDivisionByConstantInfo::get(ShiftedD, LeadingZeros + PreShift);
assert(Retval.IsAdd == 0 && Retval.PreShift == 0);
Retval.PreShift = PreShift;
return Retval;
}
Retval.Magic = std::move(Q2); // resulting magic number
++Retval.Magic;
Retval.PostShift = P - D.getBitWidth(); // resulting shift
// Reduce shift amount for IsAdd.
if (Retval.IsAdd) {
assert(Retval.PostShift > 0 && "Unexpected shift");
Retval.PostShift -= 1;
}
Retval.PreShift = 0;
// For IsAdd case with AllowWidenOptimization, compute widened magic.
// This is for optimizing 32-bit division using 64-bit multiplication.
// The actual magic constant is 2^W + Magic ((W+1)-bit).
// We pre-shift it left by (W*2 - OriginalShift) to avoid runtime shift.
if (Retval.IsAdd && AllowWidenOptimization) {
unsigned W = D.getBitWidth();
unsigned OriginalShift = Retval.PostShift + W + 1;
// Since PostShift >= 1, shift amount is at most W-2, so W*2 bits suffice.
Retval.Magic = (APInt(W * 2, 1).shl(W) + Retval.Magic.zext(W * 2))
.shl(W * 2 - OriginalShift);
Retval.IsAdd = false;
Retval.PostShift = 0;
Retval.Widen = true;
}
return Retval;
}