[llvm] [SimplifyCFG] Convert switch to cmp/select sequence (PR #82795)
via llvm-commits
llvm-commits at lists.llvm.org
Thu May 23 05:22:14 PDT 2024
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff 84729c9df30b29d5f4e903ad71235a6aa0c764d6 4b32267d0b52a0b44ffd62b6faaa72ab08a9940f -- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
``````````
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 808f590267..55e49f6362 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6174,25 +6174,27 @@ static bool trySwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
// The first field contains the value that the switch produces when a certain
// case group is selected, and the second field is a vector containing the
// cases composing the case group.
-using SwitchCaseResultVectorTy2 = SmallVector<std::pair<Value *, SmallVector<Value *, 4>>, 2>;
+using SwitchCaseResultVectorTy2 =
+ SmallVector<std::pair<Value *, SmallVector<Value *, 4>>, 2>;
// The first field contains the phi node that generates a result of the switch
// and the second field contains the value generated for a certain case in the
// switch for that PHI.
using SwitchCaseResultsTy2 = SmallVector<std::pair<PHINode *, Value *>, 4>;
-using PHINodeToCaseEntryValueMapTy = std::map<PHINode *, SmallVector<std::pair<ConstantInt *, Value *>, 4>>;
+using PHINodeToCaseEntryValueMapTy =
+ std::map<PHINode *, SmallVector<std::pair<ConstantInt *, Value *>, 4>>;
// Helper function that will return true if there is Default Value for each PHI
-bool allPHINodesHaveDefaultValue(const SwitchCaseResultsTy2& Results) {
- for (const auto& Pair : Results)
- if (Pair.second == nullptr)
- return false;
- return true;
+bool allPHINodesHaveDefaultValue(const SwitchCaseResultsTy2 &Results) {
+ for (const auto &Pair : Results)
+ if (Pair.second == nullptr)
+ return false;
+ return true;
}
-//Helper function that will return true if there is no space for
-// optimization
+// Helper function that will return true if there is no space for
+// optimization
bool isUnableToOptimize(PHINodeToCaseEntryValueMapTy &Map) {
for (const auto &Entry : Map) {
for (const auto &ValuePair : Entry.second) {
@@ -6203,17 +6205,21 @@ bool isUnableToOptimize(PHINodeToCaseEntryValueMapTy &Map) {
return true;
}
-static bool getCaseResultsWithoutConstants(SwitchInst *SI, BasicBlock *CaseDest, BasicBlock **CommonDest, SmallVectorImpl<std::pair<PHINode *, Value *>> &Res, bool IsDefault = false) {
+static bool getCaseResultsWithoutConstants(
+ SwitchInst *SI, BasicBlock *CaseDest, BasicBlock **CommonDest,
+ SmallVectorImpl<std::pair<PHINode *, Value *>> &Res,
+ bool IsDefault = false) {
BasicBlock *Pred = SI->getParent();
// Pred ->dump();
int NumOfInsts = 0;
- /// Check if there is only one instruction per block, except from default block
+ /// Check if there is only one instruction per block, except from default
+ /// block
if (!IsDefault) {
for (Instruction &I : CaseDest->instructionsWithoutDebug(false)) {
-
+
if (I.isTerminator()) {
// If the terminator is a simple branch, continue to the next block.
if (I.getNumSuccessors() != 1 || I.isSpecialTerminator())
@@ -6222,10 +6228,11 @@ static bool getCaseResultsWithoutConstants(SwitchInst *SI, BasicBlock *CaseDest,
Pred = CaseDest;
CaseDest = I.getSuccessor(0);
} else {
- // if there is more then one instruction in the block, we can not process that case
+ // if there is more then one instruction in the block, we can not
+ // process that case
if (++NumOfInsts > 1)
return false;
-
+
for (auto &Use : I.uses()) {
User *User = Use.getUser();
@@ -6236,7 +6243,7 @@ static bool getCaseResultsWithoutConstants(SwitchInst *SI, BasicBlock *CaseDest,
if (PHINode *Phi = dyn_cast<PHINode>(User))
if (Phi->getIncomingBlock(Use) == CaseDest)
continue;
-
+
return false;
}
}
@@ -6266,8 +6273,13 @@ static bool getCaseResultsWithoutConstants(SwitchInst *SI, BasicBlock *CaseDest,
return !Res.empty();
}
-// Helper function that fills in map, that for each PHI Node has pair CaseValue-ReturnValue
-static void mapCaseToResultWithMap(SwitchCaseResultsTy2 &Results, PHINodeToCaseEntryValueMapTy &Map, ConstantInt *CaseVal, SwitchCaseResultVectorTy2 &UniqueResults, Value *Result) {
+// Helper function that fills in map, that for each PHI Node has pair
+// CaseValue-ReturnValue
+static void mapCaseToResultWithMap(SwitchCaseResultsTy2 &Results,
+ PHINodeToCaseEntryValueMapTy &Map,
+ ConstantInt *CaseVal,
+ SwitchCaseResultVectorTy2 &UniqueResults,
+ Value *Result) {
// fill in PHINodeToCaseEntryValueMapTy &Map
for (const auto &Pair : Results) {
PHINode *PHI = Pair.first;
@@ -6282,35 +6294,41 @@ static void mapCaseToResultWithMap(SwitchCaseResultsTy2 &Results, PHINodeToCaseE
return;
}
}
- UniqueResults.push_back({Result, SmallVector<Value *, 4>{1, CaseVal}});
-
+ UniqueResults.push_back({Result, SmallVector<Value *, 4>{1, CaseVal}});
}
-static bool initializeUniqueCasesWithoutConstants(SwitchInst *SI, BasicBlock *&CommonDest, PHINodeToCaseEntryValueMapTy &Map, SwitchCaseResultVectorTy2 &UniqueResults, PHINode *&PHI, SwitchCaseResultsTy2 &DefaultResults) {
+static bool initializeUniqueCasesWithoutConstants(
+ SwitchInst *SI, BasicBlock *&CommonDest, PHINodeToCaseEntryValueMapTy &Map,
+ SwitchCaseResultVectorTy2 &UniqueResults, PHINode *&PHI,
+ SwitchCaseResultsTy2 &DefaultResults) {
for (const auto &Case : SI->cases()) {
SwitchCaseResultsTy2 Results;
ConstantInt *CaseVal = Case.getCaseValue();
- if(!getCaseResultsWithoutConstants(SI, Case.getCaseSuccessor(), &CommonDest, Results))
+ if (!getCaseResultsWithoutConstants(SI, Case.getCaseSuccessor(),
+ &CommonDest, Results))
return false;
-
- mapCaseToResultWithMap(Results, Map, CaseVal, UniqueResults,Results.begin()->second);
- // Check the PHI consistency.
+ mapCaseToResultWithMap(Results, Map, CaseVal, UniqueResults,
+ Results.begin()->second);
+
+ // Check the PHI consistency.
if (!PHI)
PHI = Results[0].first;
else if (PHI != Results[0].first)
return false;
}
-
- BasicBlock *DefaultDest = SI->getDefaultDest();
- if (!getCaseResultsWithoutConstants(SI, DefaultDest, &CommonDest, DefaultResults, true))
+ BasicBlock *DefaultDest = SI->getDefaultDest();
+
+ if (!getCaseResultsWithoutConstants(SI, DefaultDest, &CommonDest,
+ DefaultResults, true))
return false;
- if (!allPHINodesHaveDefaultValue(DefaultResults) && !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()))
+ if (!allPHINodesHaveDefaultValue(DefaultResults) &&
+ !isa<UnreachableInst>(DefaultDest->getFirstNonPHIOrDbg()))
return false;
-
+
return true;
}
@@ -6343,11 +6361,10 @@ Value *createSelectChain(Value *Condition, Value *DefaultResult,
}
return SelectedValue;
-
}
// Move to the next case if CurrentCase is not a constant
return createSelectChain(Condition, DefaultResult, ResultVector,
- StartIndex + 1, PHI, SI, Builder, DTU);
+ StartIndex + 1, PHI, SI, Builder, DTU);
}
static Value *foldSwitchToSelect2(const SwitchCaseResultVectorTy2 &ResultVector,
@@ -6376,8 +6393,9 @@ static bool convertSwitchToCmpAndSelect(SwitchInst *SI, IRBuilder<> &Builder,
SwitchCaseResultVectorTy2 UniqueResults;
PHINode *PHI = nullptr;
SwitchCaseResultsTy2 DefaultResults;
-
- if (!initializeUniqueCasesWithoutConstants(SI, CommonDest, Map, UniqueResults, PHI, DefaultResults))
+
+ if (!initializeUniqueCasesWithoutConstants(SI, CommonDest, Map, UniqueResults,
+ PHI, DefaultResults))
return false;
if (isUnableToOptimize(Map))
@@ -6406,7 +6424,8 @@ static bool convertSwitchToCmpAndSelect(SwitchInst *SI, IRBuilder<> &Builder,
assert(PHI != nullptr && "PHI for value select not found");
assert(DefaultResult != nullptr && "Default value for switch not found");
- Value *SelectValue = foldSwitchToSelect2(ResultVector, DefaultResult, SI->getCondition(), PHI, SI, Builder,DTU);
+ Value *SelectValue = foldSwitchToSelect2(
+ ResultVector, DefaultResult, SI->getCondition(), PHI, SI, Builder, DTU);
if (!SelectValue)
return false;
@@ -6446,7 +6465,6 @@ static bool convertSwitchToCmpAndSelect(SwitchInst *SI, IRBuilder<> &Builder,
SI->removeCase(Case);
break;
}
-
}
return true;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/82795
More information about the llvm-commits
mailing list