-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[InstCombine] Missed optimization: Fold (sext(a) & c1) == c2 to (a & c3) == trunc(c2) #112646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: Lee Wei (leewei05) ChangesAlive2: https://alive2.llvm.org/ce/z/eHAC-5 The original Alive2 proof from the issue has three code patterns and the second pattern is already being optimized by LLVM. Since this is my first PR to LLVM, I was thinking that I can add a follow-up PR for the third pattern after merging this one. Please let me know where I can improve, thanks! Full diff: https://github.com/llvm/llvm-project/pull/112646.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 18a6fdcec1728e..8a79c44777c1a4 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -24,6 +24,7 @@
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/InstrTypes.h"
+#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/KnownBits.h"
@@ -7687,6 +7688,29 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
return Res;
+ {
+ Value *A, *B;
+ const APInt *C;
+ ICmpInst::Predicate PredEq = ICmpInst::ICMP_EQ;
+ if (I.getPredicate() == PredEq) {
+ if (match(Op0, m_And(m_Value(A), m_APInt(C))) && match(Op1, m_One())) {
+ if (*C == APInt::getSignedMinValue(C->getBitWidth()) + 1) {
+ if (match(A, m_SExt(m_Value(B))) &&
+ B->getType()->getIntegerBitWidth() > 2) {
+ auto *InputTy = B->getType();
+ Value *AndInst = Builder.CreateAnd(
+ B,
+ ConstantInt::get(InputTy, APInt::getSignedMinValue(
+ InputTy->getIntegerBitWidth()) +
+ 1));
+ return CmpInst::Create(Instruction::ICmp, PredEq, AndInst,
+ ConstantInt::get(InputTy, 1));
+ }
+ }
+ }
+ }
+ }
+
return Changed ? &I : nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/sext-and.ll b/llvm/test/Transforms/InstCombine/sext-and.ll
new file mode 100644
index 00000000000000..a4ce0f41a5afb8
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/sext-and.ll
@@ -0,0 +1,49 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+declare void @use(i8)
+
+define i1 @fold_sext_to_and(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP1]], 1
+; CHECK-NEXT: ret i1 [[TMP3]]
+;
+ %1 = sext i8 %x to i32
+ %2 = and i32 %1, -2147483647
+ %3 = icmp eq i32 %2, 1
+ ret i1 %3
+}
+
+define i1 @fold_sext_to_and_multi_use(i8 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and_multi_use(
+; CHECK-SAME: i8 [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = sext i8 [[X]] to i32
+; CHECK-NEXT: call void @use(i32 [[TMP1]])
+; CHECK-NEXT: [[TMP2:%.*]] = and i8 [[X]], -127
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i8 [[TMP2]], 1
+; CHECK-NEXT: ret i1 [[TMP3]]
+;
+ %1 = sext i8 %x to i32
+ call void @use(i32 %1)
+ %2 = and i32 %1, -2147483647
+ %3 = icmp eq i32 %2, 1
+ ret i1 %3
+}
+
+; Negative tests
+
+define i1 @fold_sext_to_and_wrong(i2 %x) {
+; CHECK-LABEL: define i1 @fold_sext_to_and_wrong(
+; CHECK-SAME: i2 [[X:%.*]]) {
+; CHECK-NEXT: [[TMP1:%.*]] = sext i2 [[X]] to i32
+; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], -2147483647
+; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 1
+; CHECK-NEXT: ret i1 [[TMP3]]
+;
+ %1 = sext i2 %x to i32
+ %2 = and i32 %1, -2147483647
+ %3 = icmp eq i32 %2, 1
+ ret i1 %3
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pattern you implemented looks way too specific to me. You are checking for specific constants C1 and C2, but this should be implemented for any C1/C2 that satisfy the necessary preconditions from the proof.
Welcome and thank you for the contribution! To expand on how to make it generic, see: https://alive2.llvm.org/ce/z/RWYisS So for your checks on what are valid for C1/C2, you can use |
const APInt *C; | ||
ICmpInst::Predicate PredEq = ICmpInst::ICMP_EQ; | ||
if (I.getPredicate() == PredEq) { | ||
if (match(Op0, m_And(m_Value(A), m_APInt(C))) && match(Op1, m_One())) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just matching m_And(m_SExtLike(m_Value(A)...)
seems simpler.
Note m_SExtLike
just matches sext
or a zext
that is provably equivilent to a sext
due to the nneg
IR flag.
@goldsteinn I still cannot understand how to make my implementation more generic from your Alive2 example. Can you shed some light on this? Is there any existing code that you can reference me to? Thank you! |
The Instead you can do |
@goldsteinn Also, I printed the bit value for the following example.
Compare directly with
|
I've clarified the matching pattern of this problem with @XChy and updated the changes based on the providing proof. Thanks again! Here's an updated Alive2 proof: https://alive2.llvm.org/ce/z/iQUQfE |
// c2 must be positive | ||
// sext(a) & c1 == c2 --> a & c3 == trunc(c2) | ||
if (match(Op0, m_And(m_SExtLike(m_Value(A)), m_APInt(C1))) && match(Op1, m_APInt(C2))) { | ||
if (C2->isNonNegative()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c2
must be non-negative at the bitwidth of a
. For example, if a
is an 8-bit integer and c2
is an 32-bit integer, the leading 25 bits of c2
should be zeros.
Here you are actually checking if c2
is non-negative at the bitwidth of c2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just C2.ult(APInt::getOneBitSet(C2.getBitWidth(), InputBitWidth)
%2 = and i32 %1, -2147483647 | ||
%3 = icmp eq i32 %2, -1 | ||
ret i1 %3 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some negative tests, such as:
- c2 = 0x80
- c2 = 0x80000000
- c1 = 0x80
- c1 = 0xffffff00
{ | ||
Value *A; | ||
const APInt *C1, *C2; | ||
ICmpInst::Predicate PredEq = ICmpInst::ICMP_EQ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about icmp ne
cases? See https://alive2.llvm.org/ce/z/MUHREP
if (Leading1sCount > 0) { | ||
TruncC1.setBit(TargetBitSize - 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Leading1sCount > 0) { | |
TruncC1.setBit(TargetBitSize - 1); | |
} | |
if (Leading1sCount > 0) | |
TruncC1.setBit(TargetBitSize - 1); |
Type *InputTy = A->getType(); | ||
unsigned TargetBitSize = InputTy->getScalarSizeInBits(); | ||
// Count the number of 1s in C1 high bits of size TargetBitSize. | ||
unsigned Leading1sCount = C1->lshr(C1->getBitWidth() - TargetBitSize).popcount(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can Leading1sCount
just be replaced with !C1->lshr(C1->getBitWidth() - TargetBitSize).isZero()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it might be easier to just do C1->uge(APInt::getOneBitWidth(C1->getBitWidth(), InputBitWdith)
if (match(Op0, m_And(m_SExtLike(m_Value(A)), m_APInt(C1))) && match(Op1, m_APInt(C2))) { | ||
if (C2->isNonNegative()) { | ||
Type *InputTy = A->getType(); | ||
unsigned TargetBitSize = InputTy->getScalarSizeInBits(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Imo TargetBitSize
-> InputBitSize
. Target...
implies you are refering to the compilation target.
Sorry for missing this message. The PR is coming along, think you have the right approach now and after another cycle or so if reviews should be good to merge. |
7f82444
to
3d97acf
Compare
@XChy @goldsteinn |
%2 = and i32 %1, -256 | ||
%3 = icmp ne i32 %2, 1 | ||
ret i1 %3 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use ne
for some of the positive tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added positive tests for ne
. Please take another look! Thanks!
8e912fc
to
9110bbd
Compare
ping @goldsteinn |
Need to fixup tests:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@leewei05 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…m#112646) Fixes llvm#85830. Updated Alive proof: https://alive2.llvm.org/ce/z/KnvoP5
Fixes #85830
Updated Alive proof https://alive2.llvm.org/ce/z/KnvoP5
Since this is my first PR to LLVM, please let me know where I can improve, thanks!