[llvm] [SimplifyCFG] Use indexType from data layout in switch to table conversion (PR #146207)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 28 03:43:43 PDT 2025


================
@@ -6575,16 +6576,12 @@ Value *SwitchLookupTable::buildLookup(Value *Index, IRBuilder<> &Builder) {
     return Builder.CreateTrunc(DownShifted, BitMapElementTy, "switch.masked");
   }
   case ArrayKind: {
-    // Make sure the table index will not overflow when treated as signed.
-    IntegerType *IT = cast<IntegerType>(Index->getType());
-    uint64_t TableSize =
-        Array->getInitializer()->getType()->getArrayNumElements();
-    if (TableSize > (1ULL << std::min(IT->getBitWidth() - 1, 63u)))
-      Index = Builder.CreateZExt(
-          Index, IntegerType::get(IT->getContext(), IT->getBitWidth() + 1),
-          "switch.tableidx.zext");
-
-    Value *GEPIndices[] = {Builder.getInt32(0), Index};
+    Type *IndexTy = DL.getIndexType(Array->getType());
+
+    if (Index->getType() != IndexTy)
+      Index = Builder.CreateIntCast(Index, IndexTy, false);
----------------
nikic wrote:

```suggestion
      Index = Builder.CreateZExtOrTrunc(Index, IndexTy);
```
is a bit clearer imho.

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


More information about the llvm-commits mailing list