-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[RISCV] Undo unprofitable zext of icmp combine #134306
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |||||||||||||||||
| #include "llvm/IR/PatternMatch.h" | ||||||||||||||||||
| #include "llvm/InitializePasses.h" | ||||||||||||||||||
| #include "llvm/Pass.h" | ||||||||||||||||||
| #include "llvm/Transforms/Utils/Local.h" | ||||||||||||||||||
|
|
||||||||||||||||||
| using namespace llvm; | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -62,10 +63,74 @@ class RISCVCodeGenPrepare : public FunctionPass, | |||||||||||||||||
|
|
||||||||||||||||||
| } // end anonymous namespace | ||||||||||||||||||
|
|
||||||||||||||||||
| // Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set, | ||||||||||||||||||
| // but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill | ||||||||||||||||||
| // the upper 32 bits with ones. | ||||||||||||||||||
| // InstCombinerImpl::transformZExtICmp will narrow a zext of an icmp with a | ||||||||||||||||||
| // truncation. But RVV doesn't have truncation instructions for more than twice | ||||||||||||||||||
| // the bitwidth. | ||||||||||||||||||
| // | ||||||||||||||||||
| // E.g. trunc <vscale x 1 x i64> %x to <vscale x 1 x i8> will generate: | ||||||||||||||||||
| // | ||||||||||||||||||
| // vsetvli a0, zero, e32, m2, ta, ma | ||||||||||||||||||
| // vnsrl.wi v12, v8, 0 | ||||||||||||||||||
| // vsetvli zero, zero, e16, m1, ta, ma | ||||||||||||||||||
| // vnsrl.wi v8, v12, 0 | ||||||||||||||||||
| // vsetvli zero, zero, e8, mf2, ta, ma | ||||||||||||||||||
| // vnsrl.wi v8, v8, 0 | ||||||||||||||||||
| // | ||||||||||||||||||
| // So reverse the combine so we generate an vmseq/vmsne again: | ||||||||||||||||||
| // | ||||||||||||||||||
| // and (lshr (trunc X), ShAmt), 1 | ||||||||||||||||||
| // --> | ||||||||||||||||||
| // zext (icmp ne (and X, (1 << ShAmt)), 0) | ||||||||||||||||||
| // | ||||||||||||||||||
| // and (lshr (not (trunc X)), ShAmt), 1 | ||||||||||||||||||
| // --> | ||||||||||||||||||
| // zext (icmp eq (and X, (1 << ShAmt)), 0) | ||||||||||||||||||
| static bool reverseZExtICmpCombine(BinaryOperator &BO) { | ||||||||||||||||||
| using namespace PatternMatch; | ||||||||||||||||||
|
|
||||||||||||||||||
| assert(BO.getOpcode() == BinaryOperator::And); | ||||||||||||||||||
|
||||||||||||||||||
| if (Cmp->isEquality()) { | |
| // Test if a bit is clear/set using a shifted-one mask: | |
| // zext (icmp eq (and X, (1 << ShAmt)), 0) --> and (lshr (not X), ShAmt), 1 | |
| // zext (icmp ne (and X, (1 << ShAmt)), 0) --> and (lshr X, ShAmt), 1 | |
| Value *X, *ShAmt; | |
| if (Cmp->hasOneUse() && match(Cmp->getOperand(1), m_ZeroInt()) && | |
| match(Cmp->getOperand(0), | |
| m_OneUse(m_c_And(m_Shl(m_One(), m_Value(ShAmt)), m_Value(X))))) { |
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.
Should we be checking for that the vector extensions are enabled?