[PATCH] D134142: [InstSimplify] Fold `select C, not(C), C` to zero

Markus Böck via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 18 12:03:15 PDT 2022


zero9178 created this revision.
zero9178 added reviewers: spatel, RKSimon, nlopes.
Herald added a subscriber: hiraditya.
Herald added a project: All.
zero9178 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This is a simple fold that is always zero, as the values are always guaranteed to be zero when selected. The implementation also makes sure `zext` and `sext` of the true or false values are ignored, since zero extended remains zero.

PoC Alive2 link:
https://alive2.llvm.org/ce/z/ZJTNuy

-----

Note: I wasn't sure whether I should or can precommit tests without review, so please do tell whether you'd prefer me to precommit the tests and rebase on top of that.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134142

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/select.ll


Index: llvm/test/Transforms/InstSimplify/select.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select.ll
+++ llvm/test/Transforms/InstSimplify/select.ll
@@ -1066,3 +1066,56 @@
   %v = select <2 x i1> %cond, <2 x i32> %x, <2 x i32> poison
   ret <2 x i32> %v
 }
+
+define i32 @select_cond_not_cond_cond1(i1 %cond) {
+; CHECK-LABEL: @select_cond_not_cond_cond1(
+; CHECK-NEXT:    ret i32 0
+;
+  %z = zext i1 %cond to i32
+  %not_cond = xor i1 %cond, true
+  %s = sext i1 %not_cond to i32
+  %v = select i1 %cond, i32 %s, i32 %z
+  ret i32 %v
+}
+
+define i32 @select_cond_not_cond_cond2(i1 %cond) {
+; CHECK-LABEL: @select_cond_not_cond_cond2(
+; CHECK-NEXT:    ret i32 0
+;
+  %z = sext i1 %cond to i32
+  %not_cond = xor i1 %cond, true
+  %s = sext i1 %not_cond to i32
+  %v = select i1 %cond, i32 %s, i32 %z
+  ret i32 %v
+}
+
+define i32 @select_cond_not_cond_cond3(i1 %cond) {
+; CHECK-LABEL: @select_cond_not_cond_cond3(
+; CHECK-NEXT:    ret i32 0
+;
+  %z = sext i1 %cond to i32
+  %not_cond = xor i1 %cond, true
+  %s = zext i1 %not_cond to i32
+  %v = select i1 %cond, i32 %s, i32 %z
+  ret i32 %v
+}
+
+define i32 @select_cond_not_cond_cond4(i1 %cond) {
+; CHECK-LABEL: @select_cond_not_cond_cond4(
+; CHECK-NEXT:    ret i32 0
+;
+  %z = zext i1 %cond to i32
+  %not_cond = xor i1 %cond, true
+  %s = zext i1 %not_cond to i32
+  %v = select i1 %cond, i32 %s, i32 %z
+  ret i32 %v
+}
+
+define i1 @select_cond_not_cond_cond5(i1 %cond) {
+; CHECK-LABEL: @select_cond_not_cond_cond5(
+; CHECK-NEXT:    ret i1 false
+;
+  %not_cond = xor i1 %cond, true
+  %v = select i1 %cond, i1 %not_cond, i1 %cond
+  ret i1 %v
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4479,6 +4479,11 @@
       return ConstantVector::get(NewC);
   }
 
+  // select C, not(C), C -> 0
+  if (match(TrueVal, m_ZExtOrSExtOrSelf(m_Not(m_Specific(Cond)))) &&
+      match(FalseVal, m_ZExtOrSExtOrSelf(m_Specific(Cond))))
+    return Constant::getNullValue(TrueVal->getType());
+
   if (Value *V =
           simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
     return V;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134142.461090.patch
Type: text/x-patch
Size: 2298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220918/c8f64248/attachment.bin>


More information about the llvm-commits mailing list