[clang-tools-extra] [llvm] [SimplifyCFG] Find the minimal table considering overflow in `switchToLookupTable` (PR #67885)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 22 06:29:52 PST 2023


================
@@ -6519,17 +6518,55 @@ static bool SwitchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
   SmallDenseMap<PHINode *, Type *> ResultTypes;
   SmallVector<PHINode *, 4> PHIs;
 
-  for (SwitchInst::CaseIt E = SI->case_end(); CI != E; ++CI) {
-    ConstantInt *CaseVal = CI->getCaseValue();
-    if (CaseVal->getValue().slt(MinCaseVal->getValue()))
-      MinCaseVal = CaseVal;
-    if (CaseVal->getValue().sgt(MaxCaseVal->getValue()))
-      MaxCaseVal = CaseVal;
+  SmallVector<ConstantInt *, 8> CaseVals;
+  for (const auto &CI : SI->cases())
+    CaseVals.push_back(CI.getCaseValue());
+
+  // We want to find a range of indexes that will create the minimal table.
+  // When considering overflow, the next indexed value of the maximum signed
+  // integer is the minimum signed integer.
+  llvm::sort(CaseVals, [](const auto *L, const auto *R) {
+    return L->getValue().slt(R->getValue());
+  });
+  auto *CaseValIter = CaseVals.begin();
+  // We start by using the minimum signed integer and maximum signed as the
+  // minimal table. It's the most common case.
+  ConstantInt *BeginCaseVal = *CaseValIter;
+  ConstantInt *EndCaseVal = CaseVals.back();
+  bool RangeOverflow = false;
+  uint64_t MinTableSize = EndCaseVal->getValue()
+                              .ssub_ov(BeginCaseVal->getValue(), RangeOverflow)
+                              .getLimitedValue() +
+                          1;
+  // If there is no overflow, then this must be the minimal table.
+  if (RangeOverflow) {
----------------
DianQK wrote:

The selection of the index is no longer important.
When there exists more than one range satisfying the minimum lookup table, this must be a cover table. I will create another PR to address this issue.


https://github.com/llvm/llvm-project/pull/67885


More information about the cfe-commits mailing list