diff options
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 23 |
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. |