[llvm] [SelectionDAG][X86] Handle `llvm.type.test` in DAGBuilder (PR #142939)

Abhishek Kaushik via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 05:13:20 PDT 2025


https://github.com/abhishek-kaushik22 updated https://github.com/llvm/llvm-project/pull/142939

>From 736bd91c9c82219514c72ba2772948b99ac33330 Mon Sep 17 00:00:00 2001
From: Abhishek Kaushik <abhishek.kaushik at intel.com>
Date: Thu, 5 Jun 2025 15:35:55 +0530
Subject: [PATCH 1/2] [SelectionDAG][X86] Handle `llvm.type.test` in DAGBuilder

Closes #142937
---
 .../SelectionDAG/SelectionDAGBuilder.cpp      | 16 +++++
 llvm/test/CodeGen/X86/pr142937.ll             | 62 +++++++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/pr142937.ll

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 2460c864b0815..6bb8368252537 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -7384,6 +7384,22 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
     setValue(&I, getValue(I.getOperand(0)));
     return;
 
+  case Intrinsic::type_test:
+  case Intrinsic::public_type_test: {
+    bool AllUsersAreAssume = llvm::all_of(I.users(), [](const User *U) {
+      if (const auto *call = dyn_cast<CallInst>(U)) {
+        return call->getIntrinsicID() == Intrinsic::assume;
+      }
+      return false;
+    });
+
+    if (AllUsersAreAssume)
+      setValue(&I, DAG.getUNDEF(MVT::i1));
+    else
+      setValue(&I, DAG.getConstant(1, sdl, MVT::i1));
+    return;
+  }
+
   case Intrinsic::assume:
   case Intrinsic::experimental_noalias_scope_decl:
   case Intrinsic::var_annotation:
diff --git a/llvm/test/CodeGen/X86/pr142937.ll b/llvm/test/CodeGen/X86/pr142937.ll
new file mode 100644
index 0000000000000..269433d86ba7d
--- /dev/null
+++ b/llvm/test/CodeGen/X86/pr142937.ll
@@ -0,0 +1,62 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+; RUN: llc < %s -mtriple=i686-- -opt-bisect-limit=0   | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-- -opt-bisect-limit=0 | FileCheck %s --check-prefix=X64
+
+define void @public_type_test() {
+; X86-LABEL: public_type_test:
+; X86:       # %bb.0: # %bb
+; X86-NEXT:  # %bb.1: # %bb1
+; X86-NEXT:    retl
+;
+; X64-LABEL: public_type_test:
+; X64:       # %bb.0: # %bb
+; X64-NEXT:  # %bb.1: # %bb1
+; X64-NEXT:    retq
+bb:
+  %call = call i1 @llvm.public.type.test(ptr null, metadata !"typeinfo")
+  br label %bb1
+
+bb1:
+  call void @llvm.assume(i1 %call)
+  ret void
+}
+
+define void @type_test() {
+; X86-LABEL: type_test:
+; X86:       # %bb.0: # %bb
+; X86-NEXT:    movb $1, %al
+; X86-NEXT:    testb $1, %al
+; X86-NEXT:    jne .LBB1_2
+; X86-NEXT:  # %bb.1: # %bb1
+; X86-NEXT:    ud1l 2(%eax), %eax
+; X86-NEXT:  .LBB1_2: # %bb2
+; X86-NEXT:    retl
+;
+; X64-LABEL: type_test:
+; X64:       # %bb.0: # %bb
+; X64-NEXT:    movb $1, %al
+; X64-NEXT:    testb $1, %al
+; X64-NEXT:    jne .LBB1_2
+; X64-NEXT:  # %bb.1: # %bb1
+; X64-NEXT:    ud1l 2(%eax), %eax
+; X64-NEXT:  .LBB1_2: # %bb2
+; X64-NEXT:    retq
+bb:
+  %call = tail call i1 @llvm.type.test(ptr null, metadata !"typeinfo")
+  br i1 %call, label %bb2, label %bb1
+
+bb1:
+  tail call void @llvm.ubsantrap(i8 2)
+  unreachable
+
+bb2:
+  ret void
+}
+
+declare i1 @llvm.public.type.test(ptr, metadata)
+
+declare void @llvm.assume(i1 noundef)
+
+declare i1 @llvm.type.test(ptr, metadata)
+
+declare void @llvm.ubsantrap(i8 immarg)

>From e0d1ed9381e0993e4229c03965143cb5c460b84b Mon Sep 17 00:00:00 2001
From: Abhishek Kaushik <abhishek.kaushik at intel.com>
Date: Thu, 5 Jun 2025 17:43:03 +0530
Subject: [PATCH 2/2] Address reviews

---
 .../CodeGen/SelectionDAG/SelectionDAGBuilder.cpp  | 15 ++-------------
 llvm/test/CodeGen/X86/pr142937.ll                 |  6 ++++--
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
index 6bb8368252537..e6a1dc930685c 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
@@ -7385,20 +7385,9 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
     return;
 
   case Intrinsic::type_test:
-  case Intrinsic::public_type_test: {
-    bool AllUsersAreAssume = llvm::all_of(I.users(), [](const User *U) {
-      if (const auto *call = dyn_cast<CallInst>(U)) {
-        return call->getIntrinsicID() == Intrinsic::assume;
-      }
-      return false;
-    });
-
-    if (AllUsersAreAssume)
-      setValue(&I, DAG.getUNDEF(MVT::i1));
-    else
-      setValue(&I, DAG.getConstant(1, sdl, MVT::i1));
+  case Intrinsic::public_type_test:
+    setValue(&I, getValue(ConstantInt::getTrue(I.getType())));
     return;
-  }
 
   case Intrinsic::assume:
   case Intrinsic::experimental_noalias_scope_decl:
diff --git a/llvm/test/CodeGen/X86/pr142937.ll b/llvm/test/CodeGen/X86/pr142937.ll
index 269433d86ba7d..8be99e102ff5b 100644
--- a/llvm/test/CodeGen/X86/pr142937.ll
+++ b/llvm/test/CodeGen/X86/pr142937.ll
@@ -1,15 +1,17 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
-; RUN: llc < %s -mtriple=i686-- -opt-bisect-limit=0   | FileCheck %s --check-prefix=X86
-; RUN: llc < %s -mtriple=x86_64-- -opt-bisect-limit=0 | FileCheck %s --check-prefix=X64
+; RUN: llc < %s -mtriple=i686-- -O0   | FileCheck %s --check-prefix=X86
+; RUN: llc < %s -mtriple=x86_64-- -O0 | FileCheck %s --check-prefix=X64
 
 define void @public_type_test() {
 ; X86-LABEL: public_type_test:
 ; X86:       # %bb.0: # %bb
+; X86-NEXT:    movb $1, %al
 ; X86-NEXT:  # %bb.1: # %bb1
 ; X86-NEXT:    retl
 ;
 ; X64-LABEL: public_type_test:
 ; X64:       # %bb.0: # %bb
+; X64-NEXT:    movb $1, %al
 ; X64-NEXT:  # %bb.1: # %bb1
 ; X64-NEXT:    retq
 bb:



More information about the llvm-commits mailing list