aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
diff options
context:
space:
mode:
authorAndreas Jonson <andjo403@hotmail.com>2025-06-28 21:00:34 +0200
committerGitHub <noreply@github.com>2025-06-28 21:00:34 +0200
commit33c265ddf7f37815d38d742ea27c161aaff8931e (patch)
tree0528ca912f50db5b07dd04c8fbad807a5a6f401d /llvm/lib/Transforms/Utils/SimplifyCFG.cpp
parent344b5b7f9e5bb5c48ee3e9e380706038eaa89044 (diff)
downloadllvm-33c265ddf7f37815d38d742ea27c161aaff8931e.zip
llvm-33c265ddf7f37815d38d742ea27c161aaff8931e.tar.gz
llvm-33c265ddf7f37815d38d742ea27c161aaff8931e.tar.bz2
[SimplifyCFG] Use indexType from data layout in switch to table conversion (#146207)
Generate the GEP with the index type that InstCombine will cast it to but use the knowledge that the index is unsigned.
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyCFG.cpp23
1 files changed, 10 insertions, 13 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index e205551..147d206 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -6347,7 +6347,7 @@ public:
/// Build instructions with Builder to retrieve the value at
/// the position given by Index in the lookup table.
- Value *buildLookup(Value *Index, IRBuilder<> &Builder);
+ Value *buildLookup(Value *Index, IRBuilder<> &Builder, const DataLayout &DL);
/// Return true if a table with TableSize elements of
/// type ElementType would fit in a target-legal register.
@@ -6533,7 +6533,8 @@ SwitchLookupTable::SwitchLookupTable(
Kind = ArrayKind;
}
-Value *SwitchLookupTable::buildLookup(Value *Index, IRBuilder<> &Builder) {
+Value *SwitchLookupTable::buildLookup(Value *Index, IRBuilder<> &Builder,
+ const DataLayout &DL) {
switch (Kind) {
case SingleValueKind:
return SingleValue;
@@ -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.CreateZExtOrTrunc(Index, IndexTy);
+
+ Value *GEPIndices[] = {ConstantInt::get(IndexTy, 0), Index};
Value *GEP = Builder.CreateInBoundsGEP(Array->getValueType(), Array,
GEPIndices, "switch.gep");
return Builder.CreateLoad(
@@ -7064,7 +7061,7 @@ static bool switchToLookupTable(SwitchInst *SI, IRBuilder<> &Builder,
SwitchLookupTable Table(Mod, TableSize, TableIndexOffset, ResultList, DV,
DL, FuncName);
- Value *Result = Table.buildLookup(TableIndex, Builder);
+ Value *Result = Table.buildLookup(TableIndex, Builder, DL);
// Do a small peephole optimization: re-use the switch table compare if
// possible.