[PATCH] D50222: [CodeGen] [SelectionDAG] More efficient code for X % C == 0 (UREM case)

Roman Lebedev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 30 06:55:59 PDT 2018


lebedev.ri added inline comments.


================
Comment at: lib/CodeGen/SelectionDAG/TargetLowering.cpp:3746-3755
+  APInt D = Divisor->getAPIntValue();
+  unsigned W = D.getBitWidth();
+
+  // Rewrite D = D0 * 2^K
+  unsigned K = D.countTrailingZeros();
+  APInt D0 = D.lshr(K);
+
----------------
Well, i see it now - https://rise4fun.com/Alive/aXUu.
Then this should be:
```
APInt D = Divisor->getAPIntValue();

if(D.isPowerOf2()) {
  // rem by power-of-two is better represented by and-mask.
  return SDValue();
}

// Decompose D into D0 * 2^K
...
```


================
Comment at: lib/CodeGen/SelectionDAG/TargetLowering.cpp:3764
+  // Q = floor((2^W - 1) / D0)
+  APInt Q = APInt::getAllOnesValue(W);
+  Q = Q.udiv(D0);
----------------
lebedev.ri wrote:
> I would think just writing this as one line would be as clean
> ```
> APInt Q = APInt::getAllOnesValue(W).udiv(D0);
> ```
Also, move the `W` here, to the use.


Repository:
  rL LLVM

https://reviews.llvm.org/D50222





More information about the llvm-commits mailing list