[llvm] [SimplifyCFG] Convert switch to cmp/select sequence (PR #82795)
Thomas Symalla via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 28 05:50:21 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())
+ return false;
+ Pred = CaseDest;
+ CaseDest = I.getSuccessor(0);
+ } else {
+ // samo jedna instrukcija se hendluje po bloku
+ if (++NumOfInsts > 1)
+ return false;
+ for (auto &Use : I.uses()) {
+ User *User = Use.getUser();
+ if (Instruction *I = dyn_cast<Instruction>(User))
+ if (I->getParent() == CaseDest)
+ continue;
+ if (PHINode *Phi = dyn_cast<PHINode>(User))
+ if (Phi->getIncomingBlock(Use) == CaseDest)
+ continue;
+ return false;
+ }
+ }
+ }
+ }
+
+ // If we did not have a CommonDest before, use the current one.
+ if (!*CommonDest)
+ *CommonDest = CaseDest;
+ // If the destination isn't the common one, abort.
+ if (CaseDest != *CommonDest)
+ return false;
+
+ // Get the values for this case from phi nodes in the destination block.
+ for (PHINode &PHI : (*CommonDest)->phis()) {
+ int Idx = PHI.getBasicBlockIndex(Pred);
+ if (Idx == -1)
+ continue;
+
+ Value *Val = PHI.getIncomingValue(Idx);
+
+ if (!Val)
+ return false;
+
+ Res.push_back(std::make_pair(&PHI, Val));
+ }
+
+ return Res.size() > 0;
----------------
tsymalla wrote:
`!Res.empty()`
https://github.com/llvm/llvm-project/pull/82795
More information about the llvm-commits
mailing list