================ @@ -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:
I put it in #73269. https://github.com/llvm/llvm-project/pull/67885 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits