[PATCH] D37896: [DAGCombine] Resolving PR34474 by transforming mul(x, 2^c +/- 1) -> sub/add(shl(x, c) x) for any type including vector types

Chad Rosier via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 15 12:17:46 PDT 2017


mcrosier added a comment.

Please add test cases.

Both ARM and AArch64 have similar combines in ISelLowering.cpp.   It would be nice if these were moved to the target independent code, if possible.

For example, AArch64 currently handles:

  // (mul x, 2^N + 1) => (add (shl x, N), x)
  // (mul x, 2^N - 1) => (sub (shl x, N), x)
  // (mul x, (2^N + 1) * 2^M) => (shl (add (shl x, N), x), M)
  // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
  // (mul x, -(2^N + 1)) => - (add (shl x, N), x)



================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:2678
 
+  // change (mul X, 2^C+/-1) -> (add/sub (shl X, C ), X)
+  if (N1IsConst && ConstValue1.getSExtValue() > 2) {
----------------
Comments should be in English prose, which means they should use proper capitalization, punctuation, etc. 


================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:2679
+  // change (mul X, 2^C+/-1) -> (add/sub (shl X, C ), X)
+  if (N1IsConst && ConstValue1.getSExtValue() > 2) {
+    APInt Plus1 = ConstValue1 + 1;
----------------
Would it be interesting to handle negative constants?

    // (mul x, -(2^N - 1)) => (sub x, (shl x, N))
    // (mul x, -(2^N + 1)) => - (add (shl x, N), x)


https://reviews.llvm.org/D37896





More information about the llvm-commits mailing list