[llvm] [GlobalISel] Handle div-by-pow2 (PR #83155)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 13 21:46:29 PDT 2024


================
@@ -5286,6 +5286,148 @@ MachineInstr *CombinerHelper::buildSDivUsingMul(MachineInstr &MI) {
   return MIB.buildMul(Ty, Res, Factor);
 }
 
+bool CombinerHelper::matchDivByPow2(MachineInstr &MI, bool IsSigned) {
+  assert((MI.getOpcode() == TargetOpcode::G_SDIV ||
+          MI.getOpcode() == TargetOpcode::G_UDIV) &&
+         "Expected SDIV or UDIV");
+  auto &Div = cast<GenericMachineInstr>(MI);
+  Register RHS = Div.getReg(2);
+  auto MatchPow2 = [&](const Constant *C) {
+    auto *CI = dyn_cast<ConstantInt>(C);
----------------
arsenm wrote:

I'm surprised this predicate uses Constant, but I doubt it's possible for the dyn_cast to fail (so use cast<>?)

https://github.com/llvm/llvm-project/pull/83155


More information about the llvm-commits mailing list