-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SwitchLowering] Support merging 0 and power-of-2 case. #139736
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
fhahn
wants to merge
3
commits into
llvm:main
Choose a base branch
from
fhahn:SwitchLowerinUtils-AND
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 all commits
Commits
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
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -931,7 +931,14 @@ void IRTranslator::emitSwitchCase(SwitchCG::CaseBlock &CB, | |||||
|
||||||
const LLT i1Ty = LLT::scalar(1); | ||||||
// Build the compare. | ||||||
if (!CB.CmpMHS) { | ||||||
if (CB.EmitAnd) { | ||||||
const LLT Ty = getLLTForType(*CB.CmpRHS->getType(), *DL); | ||||||
Register CondLHS = getOrCreateVReg(*CB.CmpLHS); | ||||||
Register C = getOrCreateVReg(*CB.CmpRHS); | ||||||
Register And = MIB.buildAnd(Ty, CondLHS, C).getReg(0); | ||||||
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.
Suggested change
|
||||||
auto Zero = MIB.buildConstant(Ty, 0); | ||||||
Cond = MIB.buildICmp(CmpInst::ICMP_EQ, i1Ty, And, Zero).getReg(0); | ||||||
} else if (!CB.CmpMHS) { | ||||||
const auto *CI = dyn_cast<ConstantInt>(CB.CmpRHS); | ||||||
// For conditional branch lowering, we might try to do something silly like | ||||||
// emit an G_ICMP to compare an existing G_ICMP i1 result with true. If so, | ||||||
|
@@ -1059,18 +1066,15 @@ bool IRTranslator::lowerJumpTableWorkItem(SwitchCG::SwitchWorkListItem W, | |||||
} | ||||||
return true; | ||||||
} | ||||||
bool IRTranslator::lowerSwitchRangeWorkItem(SwitchCG::CaseClusterIt I, | ||||||
Value *Cond, | ||||||
MachineBasicBlock *Fallthrough, | ||||||
bool FallthroughUnreachable, | ||||||
BranchProbability UnhandledProbs, | ||||||
MachineBasicBlock *CurMBB, | ||||||
MachineIRBuilder &MIB, | ||||||
MachineBasicBlock *SwitchMBB) { | ||||||
bool IRTranslator::lowerSwitchAndOrRangeWorkItem( | ||||||
SwitchCG::CaseClusterIt I, Value *Cond, MachineBasicBlock *Fallthrough, | ||||||
bool FallthroughUnreachable, BranchProbability UnhandledProbs, | ||||||
MachineBasicBlock *CurMBB, MachineIRBuilder &MIB, | ||||||
MachineBasicBlock *SwitchMBB) { | ||||||
using namespace SwitchCG; | ||||||
const Value *RHS, *LHS, *MHS; | ||||||
CmpInst::Predicate Pred; | ||||||
if (I->Low == I->High) { | ||||||
if (I->Low == I->High || I->Kind == CC_And) { | ||||||
// Check Cond == I->Low. | ||||||
Pred = CmpInst::ICMP_EQ; | ||||||
LHS = Cond; | ||||||
|
@@ -1088,6 +1092,7 @@ bool IRTranslator::lowerSwitchRangeWorkItem(SwitchCG::CaseClusterIt I, | |||||
// The false probability is the sum of all unhandled cases. | ||||||
CaseBlock CB(Pred, FallthroughUnreachable, LHS, RHS, MHS, I->MBB, Fallthrough, | ||||||
CurMBB, MIB.getDebugLoc(), I->Prob, UnhandledProbs); | ||||||
CB.EmitAnd = I->Kind == CC_And; | ||||||
|
||||||
emitSwitchCase(CB, SwitchMBB, MIB); | ||||||
return true; | ||||||
|
@@ -1327,10 +1332,11 @@ bool IRTranslator::lowerSwitchWorkItem(SwitchCG::SwitchWorkListItem W, | |||||
} | ||||||
break; | ||||||
} | ||||||
case CC_And: | ||||||
case CC_Range: { | ||||||
if (!lowerSwitchRangeWorkItem(I, Cond, Fallthrough, | ||||||
FallthroughUnreachable, UnhandledProbs, | ||||||
CurMBB, MIB, SwitchMBB)) { | ||||||
if (!lowerSwitchAndOrRangeWorkItem(I, Cond, Fallthrough, | ||||||
FallthroughUnreachable, UnhandledProbs, | ||||||
CurMBB, MIB, SwitchMBB)) { | ||||||
LLVM_DEBUG(dbgs() << "Failed to lower switch range"); | ||||||
return false; | ||||||
} | ||||||
|
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
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
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.
Deserves a short comment explaining when it can be used, like the others.