Skip to content

Commit 21a1d4c

Browse files
committed
[AMDGPU] Change numBitsSigned for simplicity and document it. NFC.
Change numBitsSigned to return the minimum size of a signed integer that can hold the value. This is different by one from the previous result but is more consistent with numBitsUnsigned. Update all callers. All callers are now more consistent between the signed and unsigned cases, and some callers get simpler, especially the ones that deal with quantities like numBitsSigned(LHS) + numBitsSigned(RHS). Differential Revision: https://reviews.llvm.org/D112813
1 parent 9fb1086 commit 21a1d4c

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

llvm/lib/Target/AMDGPU/AMDGPUCodeGenPrepare.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,14 @@ class AMDGPUCodeGenPrepare : public FunctionPass,
148148
/// \returns True.
149149
bool promoteUniformBitreverseToI32(IntrinsicInst &I) const;
150150

151-
151+
/// \returns The minimum number of bits needed to store the value of \Op as an
152+
/// unsigned integer. Truncating to this size and then zero-extending to
153+
/// ScalarSize will not change the value.
152154
unsigned numBitsUnsigned(Value *Op, unsigned ScalarSize) const;
155+
156+
/// \returns The minimum number of bits needed to store the value of \Op as a
157+
/// signed integer. Truncating to this size and then sign-extending to
158+
/// ScalarSize will not change the value.
153159
unsigned numBitsSigned(Value *Op, unsigned ScalarSize) const;
154160

155161
/// Replace mul instructions with llvm.amdgcn.mul.u24 or llvm.amdgcn.mul.s24.
@@ -449,7 +455,7 @@ unsigned AMDGPUCodeGenPrepare::numBitsSigned(Value *Op,
449455
unsigned ScalarSize) const {
450456
// In order for this to be a signed 24-bit value, bit 23, must
451457
// be a sign bit.
452-
return ScalarSize - ComputeNumSignBits(Op, *DL, 0, AC);
458+
return ScalarSize - ComputeNumSignBits(Op, *DL, 0, AC) + 1;
453459
}
454460

455461
static void extractValues(IRBuilder<> &Builder,
@@ -482,13 +488,13 @@ static Value *insertValues(IRBuilder<> &Builder,
482488
// width of the original destination.
483489
static Value *getMul24(IRBuilder<> &Builder, Value *LHS, Value *RHS,
484490
unsigned Size, unsigned NumBits, bool IsSigned) {
485-
if (Size <= 32 || (IsSigned ? NumBits <= 30 : NumBits <= 32)) {
491+
if (Size <= 32 || NumBits <= 32) {
486492
Intrinsic::ID ID =
487493
IsSigned ? Intrinsic::amdgcn_mul_i24 : Intrinsic::amdgcn_mul_u24;
488494
return Builder.CreateIntrinsic(ID, {}, {LHS, RHS});
489495
}
490496

491-
assert(IsSigned ? NumBits <= 46 : NumBits <= 48);
497+
assert(NumBits <= 48);
492498

493499
Intrinsic::ID LoID =
494500
IsSigned ? Intrinsic::amdgcn_mul_i24 : Intrinsic::amdgcn_mul_u24;
@@ -530,9 +536,8 @@ bool AMDGPUCodeGenPrepare::replaceMulWithMul24(BinaryOperator &I) const {
530536
(RHSBits = numBitsUnsigned(RHS, Size)) <= 24) {
531537
IsSigned = false;
532538

533-
} else if (ST->hasMulI24() &&
534-
(LHSBits = numBitsSigned(LHS, Size)) < 24 &&
535-
(RHSBits = numBitsSigned(RHS, Size)) < 24) {
539+
} else if (ST->hasMulI24() && (LHSBits = numBitsSigned(LHS, Size)) <= 24 &&
540+
(RHSBits = numBitsSigned(RHS, Size)) <= 24) {
536541
IsSigned = true;
537542

538543
} else

llvm/lib/Target/AMDGPU/AMDGPUISelLowering.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ unsigned AMDGPUTargetLowering::numBitsSigned(SDValue Op, SelectionDAG &DAG) {
5353

5454
// In order for this to be a signed 24-bit value, bit 23, must
5555
// be a sign bit.
56-
return VT.getSizeInBits() - DAG.ComputeNumSignBits(Op);
56+
return VT.getSizeInBits() - DAG.ComputeNumSignBits(Op) + 1;
5757
}
5858

5959
AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM,
@@ -2875,7 +2875,7 @@ static bool isI24(SDValue Op, SelectionDAG &DAG) {
28752875
EVT VT = Op.getValueType();
28762876
return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
28772877
// as unsigned 24-bit values.
2878-
AMDGPUTargetLowering::numBitsSigned(Op, DAG) < 24;
2878+
AMDGPUTargetLowering::numBitsSigned(Op, DAG) <= 24;
28792879
}
28802880

28812881
static SDValue simplifyMul24(SDNode *Node24,

llvm/lib/Target/AMDGPU/AMDGPUISelLowering.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ class AMDGPUTargetLowering : public TargetLowering {
3535
SDValue getFFBX_U32(SelectionDAG &DAG, SDValue Op, const SDLoc &DL, unsigned Opc) const;
3636

3737
public:
38+
/// \returns The minimum number of bits needed to store the value of \Op as an
39+
/// unsigned integer. Truncating to this size and then zero-extending to the
40+
/// original size will not change the value.
3841
static unsigned numBitsUnsigned(SDValue Op, SelectionDAG &DAG);
42+
43+
/// \returns The minimum number of bits needed to store the value of \Op as a
44+
/// signed integer. Truncating to this size and then sign-extending to the
45+
/// original size will not change the value.
3946
static unsigned numBitsSigned(SDValue Op, SelectionDAG &DAG);
4047

4148
protected:

llvm/lib/Target/AMDGPU/SIISelLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10464,7 +10464,7 @@ SDValue SITargetLowering::performAddCombine(SDNode *N,
1046410464
return getMad64_32(DAG, SL, VT, MulLHS, MulRHS, AddRHS, false);
1046510465
}
1046610466

10467-
if (numBitsSigned(MulLHS, DAG) < 32 && numBitsSigned(MulRHS, DAG) < 32) {
10467+
if (numBitsSigned(MulLHS, DAG) <= 32 && numBitsSigned(MulRHS, DAG) <= 32) {
1046810468
MulLHS = DAG.getSExtOrTrunc(MulLHS, SL, MVT::i32);
1046910469
MulRHS = DAG.getSExtOrTrunc(MulRHS, SL, MVT::i32);
1047010470
AddRHS = DAG.getSExtOrTrunc(AddRHS, SL, MVT::i64);

0 commit comments

Comments
 (0)