[ValueTracking] Teach canCreateUndefOrPoison that ctpop does not create undef or...
authorCraig Topper <craig.topper@sifive.com>
Tue, 23 Mar 2021 19:20:04 +0000 (12:20 -0700)
committerCraig Topper <craig.topper@sifive.com>
Tue, 23 Mar 2021 19:42:18 +0000 (12:42 -0700)
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.

Differential Revision: https://reviews.llvm.org/D99207

llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Transforms/InstSimplify/select.ll

index b2f105f..c0f41c7 100644 (file)
@@ -4794,6 +4794,14 @@ static bool canCreateUndefOrPoison(const Operator *Op, bool PoisonOnly) {
     // 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);
index 86d7672..f106b90 100644 (file)
@@ -1015,6 +1015,20 @@ define i32 @select_neutral_sub_lhs(i32 %x, i32 %y) {
   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) {