[llvm] [SimplifyCFG] Convert switch to cmp/select sequence (PR #82795)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 26 10:16:12 PDT 2024
================
@@ -6166,6 +6166,279 @@ static bool trySwitchToSelect(SwitchInst *SI, IRBuilder<> &Builder,
return true;
}
+// 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 PHINodeToCaseEntryValueMapTy = std::map<
+ llvm::PHINode *,
+ llvm::SmallVector<std::pair<llvm::ConstantInt *, llvm::Value *>, 4>>;
+
+// 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>;
+
+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;
+ Value *ReturnValue = Pair.second;
+ Map[PHI].emplace_back(std::make_pair(CaseVal, ReturnValue));
+ }
+
+ // fill in SwitchCaseResultVectorTy2 &UniqueResults
+ for (auto &I : UniqueResults) {
+ if (I.first == Result) {
+ I.second.push_back(CaseVal);
+ return;
+ }
+ }
+ UniqueResults.push_back(
+ std::make_pair(Result, SmallVector<Value *, 4>(1, CaseVal)));
+}
+
+static bool getCaseResultsWithoutConstants(
+ SwitchInst *SI, BasicBlock *CaseDest, BasicBlock **CommonDest,
+ SmallVectorImpl<std::pair<PHINode *, Value *>> &Res,
+ bool IsDefault = false) {
+ BasicBlock *Pred = SI->getParent();
+ int NumOfInsts = 0;
+ // Check if there is only one instruction per block, excepet 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())
----------------
XChy wrote:
```suggestion
if (BranchInst* BI = dyn_cast<BranchInst>(&I)) {
// If the terminator is a simple branch, continue to the next block.
if (!BI->isUnconditional())
```
https://github.com/llvm/llvm-project/pull/82795
More information about the llvm-commits
mailing list