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

Pierre van Houtryve via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 6 23:46:22 PST 2024


================
@@ -5286,6 +5286,156 @@ 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) {
+    if (auto *CI = dyn_cast<ConstantInt>(C)) {
+      if (CI->getValue().isPowerOf2())
+        return true;
+      if (IsSigned && CI->getValue().isNegatedPowerOf2())
+        return true;
+    }
+    return false;
----------------
Pierre-vh wrote:

(nit, feel free to ignore if you don't agree but I think this is clearer)
```suggestion
   auto *CI = dyn_cast<ConstantInt>(C);
   if (!CI)
     return false;
   return CI->getValue().isPowerOf2() || (IsSigned && CI->getValue().isNegatedPowerOf2());
```

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


More information about the llvm-commits mailing list