Skip to content

Commit

Permalink
[AMDGPU] Try to determine sign bit during div/rem expansion
Browse files Browse the repository at this point in the history
This is preparation for D79294, which removes an expensive
InstSimplify optimization, on the assumption that it will be
picked up by InstCombine instead. Of course, this does not hold
up if a backend performs non-trivial IR expansions without running
a canonicalization pipeline afterwards, which turned up as an
issue in the context of AMDGPU div/rem expansion.

This patch mitigates the issue by explicitly performing a known
bits calculation where it matters. No test changes, as those would
only be visible after the other patch lands.

Differential Revision: https://reviews.llvm.org/D79596
  • Loading branch information
nikic committed May 8, 2020
1 parent 568787f commit 5fa87ec
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp
Expand Up @@ -1005,6 +1005,16 @@ bool AMDGPUCodeGenPrepare::divHasSpecialOptimization(
return false;
}

static Value *getSign32(Value *V, IRBuilder<> &Builder, const DataLayout *DL) {
// Check whether the sign can be determined statically.
KnownBits Known = computeKnownBits(V, *DL);
if (Known.isNegative())
return Constant::getAllOnesValue(V->getType());
if (Known.isNonNegative())
return Constant::getNullValue(V->getType());
return Builder.CreateAShr(V, Builder.getInt32(31));
}

Value* AMDGPUCodeGenPrepare::expandDivRem32(IRBuilder<> &Builder,
BinaryOperator &I,
Value *Num, Value *Den) const {
Expand Down Expand Up @@ -1046,9 +1056,8 @@ Value* AMDGPUCodeGenPrepare::expandDivRem32(IRBuilder<> &Builder,

Value *Sign = nullptr;
if (IsSigned) {
ConstantInt *K31 = Builder.getInt32(31);
Value *LHSign = Builder.CreateAShr(Num, K31);
Value *RHSign = Builder.CreateAShr(Den, K31);
Value *LHSign = getSign32(Num, Builder, DL);
Value *RHSign = getSign32(Den, Builder, DL);
// Remainder sign is the same as LHS
Sign = IsDiv ? Builder.CreateXor(LHSign, RHSign) : LHSign;

Expand Down

0 comments on commit 5fa87ec

Please sign in to comment.