-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[InstCombine] Lower flag check pattern to use a bitmask-shift #169557
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
Open
bababuck
wants to merge
7
commits into
llvm:main
Choose a base branch
from
bababuck:bababuck/FlagCheck
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5d4f0f7
[RISCV] Add new test for RISCV flag check optimization
bababuck 95514cc
[RISCV] Lower flag check pattern to use a bitmask-shift
bababuck ccfc7a8
[RISCV] Revert implementation in RISCV
bababuck 3f57238
[InstCombine] Add new tests for flag check optimization
bababuck 14c6bad
[InstCombine] Lower flag check pattern to use a bitmask-shift
bababuck 4352ec0
[InstCombine] Expand flag check patterns
bababuck 2090e8f
Remove undef in comment to pass formatter
bababuck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3119,6 +3119,127 @@ static Instruction *matchFunnelShift(Instruction &Or, InstCombinerImpl &IC) { | |
| return nullptr; | ||
| } | ||
|
|
||
| static Value *combineOrOfImmCmpToBitExtract(Instruction &Or, | ||
| InstCombiner::BuilderTy &Builder, | ||
| const DataLayout &DL) { | ||
|
|
||
| auto isICmpEqImm = [](Value *N, ConstantInt *&Imm, Value *&X) -> bool { | ||
| if (X) | ||
| return match(N, m_OneUse(m_SpecificICmp(ICmpInst::ICMP_EQ, m_Specific(X), | ||
| m_ConstantInt(Imm)))); | ||
|
|
||
| return match(N, m_OneUse(m_SpecificICmp(ICmpInst::ICMP_EQ, m_Value(X), | ||
| m_ConstantInt(Imm)))); | ||
| }; | ||
|
|
||
| // %srl = lshr %bitmap, %X | ||
| // %icmp = icmp ult %X, %max_value | ||
| // %trunc = trunc %srl to i1 | ||
| // %sel = select %icmp, %trunc, false | ||
| auto CreateBitExtractSeq = [&](APInt BitMap, APInt MaxValue, | ||
| Value *X) -> Value * { | ||
| LLVMContext &Context = Or.getContext(); | ||
|
|
||
| // %srl = lshr %bitmap, %X | ||
| // It is okay for the shift amount to be truncated because | ||
| // if information is lost then it is garunteed to fail the bounds | ||
| // check and the shift result will be discarded | ||
| ConstantInt *BitMapConst = ConstantInt::get(Context, BitMap); | ||
| Value *ShiftAmt = | ||
| Builder.CreateZExtOrTrunc(X, BitMapConst->getIntegerType()); | ||
| Value *LShr = Builder.CreateLShr(BitMapConst, ShiftAmt); | ||
|
|
||
| // %icmp = icmp ult %X, %max_value | ||
| // Use the type that is the larger of 'X' and the bounds integer | ||
| // so that no information is lost | ||
| Value *MaxVal = ConstantInt::get(Context, MaxValue); | ||
| if (MaxVal->getType()->getIntegerBitWidth() > | ||
| X->getType()->getIntegerBitWidth()) | ||
| X = Builder.CreateZExt(X, MaxVal->getType()); | ||
| else | ||
| MaxVal = Builder.CreateZExt(MaxVal, X->getType()); | ||
| Value *BoundsCheck = Builder.CreateICmp(ICmpInst::ICMP_ULT, X, MaxVal); | ||
|
|
||
| // %trunc = trunc %srl to i1 | ||
| // Only care about the low bit | ||
| Value *ShrTrunc = Builder.CreateTrunc(LShr, IntegerType::get(Context, 1)); | ||
|
|
||
| // %sel = select %icmp, %trunc, false | ||
| return Builder.CreateSelect(BoundsCheck, ShrTrunc, | ||
| ConstantInt::getFalse(Context)); | ||
| }; | ||
|
|
||
| // Our BitMap should be able to fit into a single arch register | ||
| // otherwise the tranformation won't be profitable | ||
| unsigned XLen = DL.getLargestLegalIntTypeSizeInBits(); | ||
| auto validImm = [&](APInt APImm) -> bool { | ||
| auto Imm = APImm.tryZExtValue(); | ||
| return Imm && (*Imm < XLen); | ||
| }; | ||
|
|
||
| // Match (or (icmp eq X, Imm0), (icmp eq X, Imm1)) | ||
| ConstantInt *LHS, *RHS; | ||
| Value *X = nullptr; | ||
| if (isICmpEqImm(Or.getOperand(0), LHS, X) && | ||
| isICmpEqImm(Or.getOperand(1), RHS, X)) { | ||
| // The Shr with become poison when shifted by Undef | ||
| if (!isGuaranteedNotToBeUndefOrPoison(X)) | ||
| return nullptr; | ||
|
|
||
| APInt LHSAP = LHS->getValue(); | ||
| APInt RHSAP = RHS->getValue(); | ||
| if (!validImm(LHSAP) || !validImm(RHSAP)) | ||
| return nullptr; | ||
| LHSAP = LHSAP.zextOrTrunc(XLen); | ||
| RHSAP = RHSAP.zextOrTrunc(XLen); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if this is the best way to wrangle various APInt's that may have differing BitWidth's- effectively I ensure they can be represented within XLen and then force them all to have a BitWidth or XLen. |
||
|
|
||
| // Create the BitMap and Bounds check immediates | ||
| // +1 to bound becuase strictly less than | ||
| APInt BitMap = (APInt(XLen, 1) << LHSAP) | (APInt(XLen, 1) << RHSAP); | ||
| APInt Bound = RHSAP.ugt(LHSAP) ? RHSAP : LHSAP; | ||
| return CreateBitExtractSeq(BitMap, Bound + 1, X); | ||
| } | ||
|
|
||
| // Expand an already existing BitMap sequence | ||
| // Match: (or (%BitMapSeq(X)), (icmp eq X, Imm)) | ||
| ConstantInt *BitMap, *Bound, *CmpImm; | ||
| Value *Cmp; | ||
| if (match(&Or, m_OneUse(m_c_Or(m_Value(Cmp), | ||
| m_OneUse(m_Select( | ||
| m_SpecificICmp(ICmpInst::ICMP_ULT, | ||
| m_ZExtOrSelf(m_Value(X)), | ||
| m_ConstantInt(Bound)), | ||
| m_OneUse(m_Trunc(m_OneUse(m_Shr( | ||
| m_ConstantInt(BitMap), | ||
| m_ZExtOrTruncOrSelf(m_Deferred(X)))))), | ||
| m_Zero()))))) && | ||
| isICmpEqImm(Cmp, CmpImm, X)) { | ||
| if (!isGuaranteedNotToBeUndefOrPoison(X)) | ||
| return nullptr; | ||
|
|
||
| APInt NewAP = CmpImm->getValue(); | ||
| APInt BitMapAP = BitMap->getValue(); | ||
| APInt BoundAP = Bound->getValue().zextOrTrunc(XLen); | ||
| // BitMap must fit in native arch register | ||
| if (!validImm(NewAP) || !DL.fitsInLegalInteger(BitMapAP.getActiveBits())) | ||
| return nullptr; | ||
|
|
||
| NewAP = NewAP.zextOrTrunc(XLen); | ||
| BitMapAP = BitMapAP.zextOrTrunc(XLen); | ||
|
|
||
| // Bounding immediate must be greater than the largest bit in the BitMap | ||
| // and less then XLen | ||
| if (BoundAP.ult(BitMapAP.getActiveBits()) || BoundAP.ugt(XLen)) | ||
| return nullptr; | ||
|
|
||
| if (NewAP.uge(BoundAP)) | ||
| BoundAP = NewAP + 1; | ||
| BitMapAP |= (APInt(XLen, 1) << NewAP); | ||
| return CreateBitExtractSeq(BitMapAP, BoundAP, X); | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| /// Attempt to combine or(zext(x),shl(zext(y),bw/2) concat packing patterns. | ||
| static Value *matchOrConcat(Instruction &Or, InstCombiner::BuilderTy &Builder) { | ||
| assert(Or.getOpcode() == Instruction::Or && "bswap requires an 'or'"); | ||
|
|
@@ -4084,6 +4205,10 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) { | |
| if (Instruction *Funnel = matchFunnelShift(I, *this)) | ||
| return Funnel; | ||
|
|
||
| if (Value *BitExtract = | ||
| combineOrOfImmCmpToBitExtract(I, Builder, getDataLayout())) | ||
| return replaceInstUsesWith(I, BitExtract); | ||
|
|
||
| if (Value *Concat = matchOrConcat(I, Builder)) | ||
| return replaceInstUsesWith(I, Concat); | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 transform with just two options is about a net neutral (at least going to the RISCV backend). However, the benefit comes once we can expand to combining 3 or more comparisons. From a code standpoint, the implementation seemed cleaner to operate on a single
orat a time though.