[llvm] [SimplifyCFG] Improve linear mapping in switch lookup tables (PR #67881)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 23 09:50:57 PDT 2023


================
@@ -6139,46 +6141,80 @@ SwitchLookupTable::SwitchLookupTable(
   // Check if we can derive the value with a linear transformation from the
   // table index.
   if (isa<IntegerType>(ValueType)) {
-    bool LinearMappingPossible = true;
-    APInt PrevVal;
-    APInt DistToPrev;
-    // When linear map is monotonic and signed overflow doesn't happen on
-    // maximum index, we can attach nsw on Add and Mul.
-    bool NonMonotonic = false;
-    assert(TableSize >= 2 && "Should be a SingleValue table.");
-    // Check if there is the same distance between two consecutive values.
-    for (uint64_t I = 0; I < TableSize; ++I) {
-      ConstantInt *ConstVal = dyn_cast<ConstantInt>(TableContents[I]);
-      if (!ConstVal) {
-        // This is an undef. We could deal with it, but undefs in lookup tables
-        // are very seldom. It's probably not worth the additional complexity.
-        LinearMappingPossible = false;
-        break;
-      }
-      const APInt &Val = ConstVal->getValue();
-      if (I != 0) {
-        APInt Dist = Val - PrevVal;
-        if (I == 1) {
-          DistToPrev = Dist;
-        } else if (Dist != DistToPrev) {
-          LinearMappingPossible = false;
-          break;
+    auto MatchLinearMapping = [&](unsigned *LowBits) {
----------------
goldsteinn wrote:

Might be clearer to pass explicit bool for the matching mode i.e
`bool MatchWithMaskAndHighbits`. Otherwise its unclear from just the code what the difference is.

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


More information about the llvm-commits mailing list