[PATCH] D99207: [ValueTracking] Teach canCreateUndefOrPoison that ctpop does not create undef or poison.

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 23 12:23:51 PDT 2021


craig.topper created this revision.
craig.topper added reviewers: lebedev.ri, spatel, RKSimon, aqjune.
Herald added a subscriber: hiraditya.
craig.topper requested review of this revision.
Herald added a project: LLVM.

This select of ctpop with 0 pattern can get left behind after
loop idiom recognize converts a loop to ctpop. LLVM 10 was able
to optimize this, but LLVM 11 and later is not. The difference
seems to be that some select transforms are now limited based
on canCreateUndefOrPoison.

Teaching canCreateUndefOrPoison about ctpop restores the
LLVM 10 codegen.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99207

Files:
  llvm/lib/Analysis/ValueTracking.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
@@ -1015,6 +1015,20 @@
   ret i32 %sel
 }
 
+define i32 @select_ctpop_zero(i32 %x) {
+; CHECK-LABEL: @select_ctpop_zero(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[TMP0:%.*]] = call i32 @llvm.ctpop.i32(i32 [[X:%.*]])
+; CHECK-NEXT:    ret i32 [[TMP0]]
+;
+entry:
+  %0 = icmp eq i32 %x, 0
+  %1 = call i32 @llvm.ctpop.i32(i32 %x)
+  %sel = select i1 %0, i32 0, i32 %1
+  ret i32 %sel
+}
+declare i32 @llvm.ctpop.i32(i32)
+
 ; TODO: these can be optimized more
 
 define i32 @poison(i32 %x, i32 %y) {
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -4794,6 +4794,14 @@
     // destination type.
     return true;
   case Instruction::Call:
+    if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
+      switch (II->getIntrinsicID()) {
+      // TODO: Add more intrinsics.
+      case Intrinsic::ctpop:
+        return false;
+      }
+    }
+    LLVM_FALLTHROUGH;
   case Instruction::CallBr:
   case Instruction::Invoke: {
     const auto *CB = cast<CallBase>(Op);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99207.332754.patch
Type: text/x-patch
Size: 1321 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210323/77aeca5d/attachment.bin>


More information about the llvm-commits mailing list