[llvm] 3f749a5 - [Support][SelectionDAG][GlobalISel] Hoist PostShift adjustment for IsAdd into UnsignedDivideUsingMagic.
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Wed Jan 4 15:31:40 PST 2023
Author: Craig Topper
Date: 2023-01-04T15:18:12-08:00
New Revision: 3f749a5d9dfd6111fff77e97ffa4a0f658ae3c41
URL: https://github.com/llvm/llvm-project/commit/3f749a5d9dfd6111fff77e97ffa4a0f658ae3c41
DIFF: https://github.com/llvm/llvm-project/commit/3f749a5d9dfd6111fff77e97ffa4a0f658ae3c41.diff
LOG: [Support][SelectionDAG][GlobalISel] Hoist PostShift adjustment for IsAdd into UnsignedDivideUsingMagic.
Instead of doing the adjustment in 3 different places in the code
base, do it inside UnsignedDivideUsingMagic::get.
Differential Revision: https://reviews.llvm.org/D141014
Added:
Modified:
llvm/include/llvm/Support/DivisionByConstantInfo.h
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
llvm/lib/Support/DivisionByConstantInfo.cpp
llvm/unittests/Support/DivisionByConstantTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/DivisionByConstantInfo.h b/llvm/include/llvm/Support/DivisionByConstantInfo.h
index 464e412a3d489..caa0b35e71447 100644
--- a/llvm/include/llvm/Support/DivisionByConstantInfo.h
+++ b/llvm/include/llvm/Support/DivisionByConstantInfo.h
@@ -31,7 +31,7 @@ struct UnsignedDivisionByConstantInfo {
bool AllowEvenDivisorOptimization = true);
APInt Magic; ///< magic number
bool IsAdd; ///< add indicator
- unsigned ShiftAmount; ///< shift amount
+ unsigned PostShift; ///< post-shift amount
unsigned PreShift; ///< pre-shift amount
};
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index 9899875e47c56..01edd908822f4 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -4962,16 +4962,14 @@ MachineInstr *CombinerHelper::buildUDivUsingMul(MachineInstr &MI) {
Magic = std::move(magics.Magic);
- if (!magics.IsAdd) {
- assert(magics.ShiftAmount < Divisor.getBitWidth() &&
- "We shouldn't generate an undefined shift!");
- PostShift = magics.ShiftAmount;
- PreShift = magics.PreShift;
- } else {
- assert(magics.PreShift == 0 && "Unexpected pre-shift");
- PostShift = magics.ShiftAmount - 1;
- SelNPQ = true;
- }
+ assert(magics.PreShift < Divisor.getBitWidth() &&
+ "We shouldn't generate an undefined shift!");
+ assert(magics.PostShift < Divisor.getBitWidth() &&
+ "We shouldn't generate an undefined shift!");
+ assert((!magics.IsAdd || magics.PreShift == 0) && "Unexpected pre-shift");
+ PreShift = magics.PreShift;
+ PostShift = magics.PostShift;
+ SelNPQ = magics.IsAdd;
}
PreShifts.push_back(
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 310fe14c378e0..f7e6003807820 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -6056,16 +6056,15 @@ SDValue TargetLowering::BuildUDIV(SDNode *N, SelectionDAG &DAG,
Magic = std::move(magics.Magic);
- if (!magics.IsAdd) {
- assert(magics.ShiftAmount < Divisor.getBitWidth() &&
- "We shouldn't generate an undefined shift!");
- PostShift = magics.ShiftAmount;
- PreShift = magics.PreShift;
- } else {
- assert(magics.PreShift == 0 && "Unexpected pre-shift");
- PostShift = magics.ShiftAmount - 1;
- SelNPQ = true;
- }
+ assert(magics.PreShift < Divisor.getBitWidth() &&
+ "We shouldn't generate an undefined shift!");
+ assert(magics.PostShift < Divisor.getBitWidth() &&
+ "We shouldn't generate an undefined shift!");
+ assert((!magics.IsAdd || magics.PreShift == 0) &&
+ "Unexpected pre-shift");
+ PreShift = magics.PreShift;
+ PostShift = magics.PostShift;
+ SelNPQ = magics.IsAdd;
}
PreShifts.push_back(DAG.getConstant(PreShift, dl, ShSVT));
diff --git a/llvm/lib/Support/DivisionByConstantInfo.cpp b/llvm/lib/Support/DivisionByConstantInfo.cpp
index a40f49ba69bb3..e7072d94e49ce 100644
--- a/llvm/lib/Support/DivisionByConstantInfo.cpp
+++ b/llvm/lib/Support/DivisionByConstantInfo.cpp
@@ -143,7 +143,12 @@ UnsignedDivisionByConstantInfo::get(const APInt &D, unsigned LeadingZeros,
Retval.Magic = std::move(Q2); // resulting magic number
++Retval.Magic;
- Retval.ShiftAmount = P - D.getBitWidth(); // resulting shift
+ 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;
return Retval;
}
diff --git a/llvm/unittests/Support/DivisionByConstantTest.cpp b/llvm/unittests/Support/DivisionByConstantTest.cpp
index 43e44c9b81c4d..5d19bad748e49 100644
--- a/llvm/unittests/Support/DivisionByConstantTest.cpp
+++ b/llvm/unittests/Support/DivisionByConstantTest.cpp
@@ -116,22 +116,14 @@ APInt UnsignedDivideUsingMagic(const APInt &Numerator, const APInt &Divisor,
}
}
- unsigned PreShift = 0;
- unsigned PostShift = 0;
- bool UseNPQ = false;
- if (!Magics.IsAdd) {
- assert(Magics.ShiftAmount < Divisor.getBitWidth() &&
- "We shouldn't generate an undefined shift!");
- PreShift = Magics.PreShift;
- PostShift = Magics.ShiftAmount;
- UseNPQ = false;
- } else {
- assert(Magics.PreShift == 0 && "Unexpected pre-shift");
- PostShift = Magics.ShiftAmount - 1;
- assert(PostShift < Divisor.getBitWidth() &&
- "We shouldn't generate an undefined shift!");
- UseNPQ = true;
- }
+ assert(Magics.PreShift < Divisor.getBitWidth() &&
+ "We shouldn't generate an undefined shift!");
+ assert(Magics.PostShift < Divisor.getBitWidth() &&
+ "We shouldn't generate an undefined shift!");
+ assert((!Magics.IsAdd || Magics.PreShift == 0) && "Unexpected pre-shift");
+ unsigned PreShift = Magics.PreShift;
+ unsigned PostShift = Magics.PostShift;
+ bool UseNPQ = Magics.IsAdd;
APInt NPQFactor =
UseNPQ ? APInt::getSignedMinValue(Bits) : APInt::getZero(Bits);
More information about the llvm-commits
mailing list