[PATCH] D48301: DAG combine "and|or (select c, -1, 0), x" -> "select c, x, 0|-1"

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 20 16:40:27 PDT 2018


spatel added inline comments.


================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1904-1905
   // Bail out if any constants are opaque because we can't constant fold those.
+  // The exception is "and" and "or" with either 0 or -1 in which case we can
+  // propagate non constant operands into select.
+  bool CanFoldNonConst = false;
----------------
This deserves an example in the comment since it doesn't match the formula below here. 

Something like:
and (select Cond, 0, -1), X --> select Cond, 0, X
or X, (select Cond, -1, 0) --> select Cond, -1, X


================
Comment at: lib/CodeGen/SelectionDAG/DAGCombiner.cpp:1908-1911
+    ConstantSDNode *CTN = cast<ConstantSDNode>(CT);
+    ConstantSDNode *CFN = cast<ConstantSDNode>(CF);
+    CanFoldNonConst = (CTN->isNullValue() || CTN->isAllOnesValue()) &&
+                      (CFN->isNullValue() || CFN->isAllOnesValue());
----------------
This should handle vector types. Currently, it will crash:


```
define <4 x i32> @select_or1_vec(i32 %x, <4 x i32> %y) {
  %c = icmp slt i32 %x, 11
  %s = select i1 %c, <4 x i32> zeroinitializer, <4 x i32><i32 -1, i32 -1, i32 -1, i32 -1>
  %a = or <4 x i32> %y, %s
  ret <4 x i32> %a
}

```


================
Comment at: test/CodeGen/X86/dagcombine-select.ll:12
   %c = icmp slt i32 %x, 11
   %s = select i1 %c, i32 0, i32 -1
   %a = and i32 %y, %s
----------------
We need some tests where the select's true value is the -1.


https://reviews.llvm.org/D48301





More information about the llvm-commits mailing list