[llvm] [clang] Missing opt with ctlz and shifts of power of 2 constants (#41333) (PR #74175)

2023-12-02 Thread Sizov Nikita via cfe-commits

https://github.com/snikitav updated 
https://github.com/llvm/llvm-project/pull/74175

>From cb2bdf4a4cb9db2262920a0a474e2024e7a1406a Mon Sep 17 00:00:00 2001
From: Sizov Nikita 
Date: Sat, 2 Dec 2023 04:53:32 +0300
Subject: [PATCH] Missing opt with ctlz and shifts of power of 2 constants
 (#41333)

---
 clang/test/CXX/drs/dr2xx.cpp  |   2 +-
 .../InstCombine/InstCombineCalls.cpp  |  33 +++
 .../InstCombine/ctlz-cttz-shifts.ll   | 243 ++
 3 files changed, 277 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Transforms/InstCombine/ctlz-cttz-shifts.ll

diff --git a/clang/test/CXX/drs/dr2xx.cpp b/clang/test/CXX/drs/dr2xx.cpp
index 4dd6d7599f2a0..170753e5efce4 100644
--- a/clang/test/CXX/drs/dr2xx.cpp
+++ b/clang/test/CXX/drs/dr2xx.cpp
@@ -1298,7 +1298,7 @@ namespace dr299 { // dr299: 2.8 c++11
   // cxx98-11-error@#dr299-q {{ambiguous conversion of array size expression 
of type 'T' to an integral or enumeration type}}
   //  cxx98-11-note@#dr299-int {{conversion to integral type 'int' declared 
here}}
   //  cxx98-11-note@#dr299-ushort {{conversion to integral type 'unsigned 
short' declared here}}
-  // since-cxx14-error-re@#dr299-q conversion from 'T' to 'unsigned 
(long|int)' is ambiguous
+  // since-cxx14-error-re@#dr299-q {{conversion from 'T' to 'unsigned 
(long|int)' is ambiguous}}
   //  since-cxx14-note@#dr299-int {{candidate function}}
   //  since-cxx14-note@#dr299-ushort {{candidate function}}
 }
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index a991f0906052a..f6322b3f4f415 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -514,6 +514,8 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, 
InstCombinerImpl &IC) {
 return IC.replaceInstUsesWith(II, ConstantInt::getNullValue(II.getType()));
   }
 
+  Constant *C;
+
   if (IsTZ) {
 // cttz(-x) -> cttz(x)
 if (match(Op0, m_Neg(m_Value(X
@@ -549,6 +551,37 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, 
InstCombinerImpl &IC) {
 
 if (match(Op0, m_Intrinsic(m_Value(X
   return IC.replaceOperand(II, 0, X);
+
+// cttz(shl(%const, %val), 1) --> add(cttz(%const, 1), %val)
+if (match(Op0, m_Shl(m_Constant(C), m_Value(X))) && match(Op1, m_One())) {
+  Value *ConstCttz =
+  IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
+  return BinaryOperator::CreateAdd(ConstCttz, X);
+}
+
+// cttz(lshr exact (%const, %val), 0) --> sub(cttz(%const, 0), %val)
+if (match(Op0, m_Exact(m_LShr(m_Constant(C), m_Value(X) {
+  Value *ConstCttz =
+  IC.Builder.CreateBinaryIntrinsic(Intrinsic::cttz, C, Op1);
+  return BinaryOperator::CreateSub(ConstCttz, X);
+}
+  } else {
+// ctlz(lshr(%const, %val), 1) --> add(ctlz(%const, 1), %val)
+if (match(Op0, m_LShr(m_Constant(C), m_Value(X))) && match(Op1, m_One())) {
+  Value *ConstCtlz =
+  IC.Builder.CreateBinaryIntrinsic(Intrinsic::ctlz, C, Op1);
+  return BinaryOperator::CreateAdd(ConstCtlz, X);
+}
+
+// ctlz(shl nuw (%const, %val), 0) |
+// ctlz(shl nsw (%const, %val), 0) |--> sub(ctlz(%const, 0), %val)
+// ctlz(shl nuw nsw (%const, %val), 0) |
+if (match(Op0, m_NUWShl(m_Constant(C), m_Value(X))) ||
+match(Op0, m_NSWShl(m_Constant(C), m_Value(X {
+  Value *ConstCtlz =
+  IC.Builder.CreateBinaryIntrinsic(Intrinsic::ctlz, C, Op1);
+  return BinaryOperator::CreateSub(ConstCtlz, X);
+}
   }
 
   KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
diff --git a/llvm/test/Transforms/InstCombine/ctlz-cttz-shifts.ll 
b/llvm/test/Transforms/InstCombine/ctlz-cttz-shifts.ll
new file mode 100644
index 0..5abe5ab6c9310
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/ctlz-cttz-shifts.ll
@@ -0,0 +1,243 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 4
+; RUN: opt -passes=instcombine -S < %s | FileCheck %s
+
+declare i32 @llvm.ctlz.i32(i32, i1)
+declare i32 @llvm.cttz.i32(i32, i1)
+declare <2 x i32> @llvm.ctlz.v2i32(<2 x i32>, i1)
+declare <2 x i32> @llvm.cttz.v2i32(<2 x i32>, i1)
+
+define i32 @lshr_ctlz_true(i32) {
+; CHECK-LABEL: define i32 @lshr_ctlz_true(
+; CHECK-SAME: i32 [[TMP0:%.*]]) {
+; CHECK-NEXT:[[CTLZ:%.*]] = add i32 [[TMP0]], 9
+; CHECK-NEXT:ret i32 [[CTLZ]]
+;
+  %lshr = lshr i32 8387584, %0
+  %ctlz = call i32 @llvm.ctlz.i32(i32 %lshr, i1 true)
+  ret i32 %ctlz
+}
+
+define i32 @shl_nuw_ctlz_false(i32) {
+; CHECK-LABEL: define i32 @shl_nuw_ctlz_false(
+; CHECK-SAME: i32 [[TMP0:%.*]]) {
+; CHECK-NEXT:[[CTLZ:%.*]] = sub i32 9, [[TMP0]]
+; CHECK-NEXT:ret i32 [[CTLZ]]
+;
+  %shl = shl nuw i32 8387584, %0
+  %ctlz = call i32 @llvm.ctlz.i32(i32 %shl, i1 false)
+  ret i32 %ctlz
+}
+
+define i32 @shl_nsw_ctlz_false(i32) {
+; CHECK-LABEL: define i32 @shl_

[clang] Fix dr2xx clang test (PR #74223)

2023-12-02 Thread Sizov Nikita via cfe-commits

https://github.com/snikitav created 
https://github.com/llvm/llvm-project/pull/74223

Currently windows build fails with:

```
# | error: 'cxx98-14-error' diagnostics expected but not seen: 
# |   File C:\ws\src\clang\test\CXX\drs\dr2xx.cpp Line 1297 (directive at 
C:\ws\src\clang\test\CXX\drs\dr2xx.cpp:1301): {{conversion from 'T' to 
'unsigned (long|int)' is ambiguous}}
```

Obviously, there is an excessive pair of brackets

>From 17cb937a233290627ea90778a0fafb968f03091b Mon Sep 17 00:00:00 2001
From: Sizov Nikita 
Date: Sun, 3 Dec 2023 06:56:08 +0300
Subject: [PATCH] Fix crash for windows clang unittest

---
 clang/test/CXX/drs/dr2xx.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CXX/drs/dr2xx.cpp b/clang/test/CXX/drs/dr2xx.cpp
index 4dd6d7599f2a0..170753e5efce4 100644
--- a/clang/test/CXX/drs/dr2xx.cpp
+++ b/clang/test/CXX/drs/dr2xx.cpp
@@ -1298,7 +1298,7 @@ namespace dr299 { // dr299: 2.8 c++11
   // cxx98-11-error@#dr299-q {{ambiguous conversion of array size expression 
of type 'T' to an integral or enumeration type}}
   //  cxx98-11-note@#dr299-int {{conversion to integral type 'int' declared 
here}}
   //  cxx98-11-note@#dr299-ushort {{conversion to integral type 'unsigned 
short' declared here}}
-  // since-cxx14-error-re@#dr299-q conversion from 'T' to 'unsigned 
(long|int)' is ambiguous
+  // since-cxx14-error-re@#dr299-q {{conversion from 'T' to 'unsigned 
(long|int)' is ambiguous}}
   //  since-cxx14-note@#dr299-int {{candidate function}}
   //  since-cxx14-note@#dr299-ushort {{candidate function}}
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix dr2xx clang test (PR #74223)

2023-12-02 Thread Sizov Nikita via cfe-commits

snikitav wrote:

@Endilll Im'sorry, not sure what to do in this situation, but I think current 
master CI is broken and my patch might fix it. Is it ok to tag you or other 
people, or should I wait for autoassigned reviewers?

https://github.com/llvm/llvm-project/pull/74223
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix dr2xx clang test (PR #74223)

2023-12-02 Thread Sizov Nikita via cfe-commits

https://github.com/snikitav edited 
https://github.com/llvm/llvm-project/pull/74223
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix dr2xx clang test (PR #74223)

2023-12-02 Thread Sizov Nikita via cfe-commits

https://github.com/snikitav updated 
https://github.com/llvm/llvm-project/pull/74223

>From 661c42716c494bb27449c2dd81e03cf0945f1cf1 Mon Sep 17 00:00:00 2001
From: Sizov Nikita 
Date: Sun, 3 Dec 2023 06:56:08 +0300
Subject: [PATCH] Fix crash for windows clang unittest

---
 clang/test/CXX/drs/dr2xx.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CXX/drs/dr2xx.cpp b/clang/test/CXX/drs/dr2xx.cpp
index 4dd6d7599f2a..4167b4363648 100644
--- a/clang/test/CXX/drs/dr2xx.cpp
+++ b/clang/test/CXX/drs/dr2xx.cpp
@@ -1298,7 +1298,7 @@ namespace dr299 { // dr299: 2.8 c++11
   // cxx98-11-error@#dr299-q {{ambiguous conversion of array size expression 
of type 'T' to an integral or enumeration type}}
   //  cxx98-11-note@#dr299-int {{conversion to integral type 'int' declared 
here}}
   //  cxx98-11-note@#dr299-ushort {{conversion to integral type 'unsigned 
short' declared here}}
-  // since-cxx14-error-re@#dr299-q conversion from 'T' to 'unsigned 
(long|int)' is ambiguous
+  // since-cxx14-error-re@#dr299-q {{conversion from 'T' to 'unsigned 
{{long|int}}' is ambiguous}}
   //  since-cxx14-note@#dr299-int {{candidate function}}
   //  since-cxx14-note@#dr299-ushort {{candidate function}}
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix dr2xx clang test (PR #74223)

2023-12-02 Thread Sizov Nikita via cfe-commits

https://github.com/snikitav edited 
https://github.com/llvm/llvm-project/pull/74223
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Fix dr2xx clang test (PR #74223)

2023-12-02 Thread Sizov Nikita via cfe-commits

https://github.com/snikitav updated 
https://github.com/llvm/llvm-project/pull/74223

>From 41bae56752441a2b2e23ff04e3f0a03ef0d95743 Mon Sep 17 00:00:00 2001
From: Sizov Nikita 
Date: Sun, 3 Dec 2023 06:56:08 +0300
Subject: [PATCH] Fix crash for windows clang unittest

---
 clang/test/CXX/drs/dr2xx.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CXX/drs/dr2xx.cpp b/clang/test/CXX/drs/dr2xx.cpp
index 4dd6d7599f2a0..582993ba7fe71 100644
--- a/clang/test/CXX/drs/dr2xx.cpp
+++ b/clang/test/CXX/drs/dr2xx.cpp
@@ -1298,7 +1298,7 @@ namespace dr299 { // dr299: 2.8 c++11
   // cxx98-11-error@#dr299-q {{ambiguous conversion of array size expression 
of type 'T' to an integral or enumeration type}}
   //  cxx98-11-note@#dr299-int {{conversion to integral type 'int' declared 
here}}
   //  cxx98-11-note@#dr299-ushort {{conversion to integral type 'unsigned 
short' declared here}}
-  // since-cxx14-error-re@#dr299-q conversion from 'T' to 'unsigned 
(long|int)' is ambiguous
+  // since-cxx14-error-re@#dr299-q {{conversion from 'T' to 'unsigned 
{{(long|int)}}' is ambiguous}}
   //  since-cxx14-note@#dr299-int {{candidate function}}
   //  since-cxx14-note@#dr299-ushort {{candidate function}}
 }

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits