21 unsigned First,
unsigned Last) {
23 const APInt &LowCase = Clusters[First].Low->getValue();
24 const APInt &HighCase = Clusters[Last].High->getValue();
30 return (HighCase - LowCase).getLimitedValue((
UINT64_MAX - 1) / 100) + 1;
35 unsigned First,
unsigned Last) {
37 assert(TotalCases[Last] >= TotalCases[First]);
39 TotalCases[Last] - (First == 0 ? 0 : TotalCases[First - 1]);
53 for (
unsigned i = 1,
e = Clusters.size(); i <
e; ++i)
54 assert(Clusters[i - 1].
High->getValue().slt(Clusters[i].Low->getValue()));
57 assert(TLI &&
"TLI not set!");
61 const unsigned MinJumpTableEntries = TLI->getMinimumJumpTableEntries();
62 const unsigned SmallNumberOfEntries = MinJumpTableEntries / 2;
65 const int64_t
N = Clusters.size();
66 if (N < 2 || N < MinJumpTableEntries)
71 for (
unsigned i = 0; i <
N; ++i) {
72 const APInt &
Hi = Clusters[i].High->getValue();
73 const APInt &
Lo = Clusters[i].Low->getValue();
74 TotalCases[i] = (Hi -
Lo).getLimitedValue() + 1;
76 TotalCases[i] += TotalCases[i - 1];
85 if (TLI->isSuitableForJumpTable(SI, NumCases, Range, PSI, BFI)) {
87 if (buildJumpTable(Clusters, 0, N - 1, SI, DefaultMBB, JTCluster)) {
88 Clusters[0] = JTCluster;
115 enum PartitionScores :
unsigned {
123 MinPartitions[N - 1] = 1;
124 LastElement[N - 1] = N - 1;
125 PartitionsScore[N - 1] = PartitionScores::SingleCase;
128 for (int64_t i = N - 2; i >= 0; i--) {
131 MinPartitions[i] = MinPartitions[i + 1] + 1;
133 PartitionsScore[i] = PartitionsScore[i + 1] + PartitionScores::SingleCase;
136 for (int64_t j = N - 1; j > i; j--) {
141 assert(Range >= NumCases);
143 if (TLI->isSuitableForJumpTable(SI, NumCases, Range, PSI, BFI)) {
144 unsigned NumPartitions = 1 + (j == N - 1 ? 0 : MinPartitions[j + 1]);
145 unsigned Score = j == N - 1 ? 0 : PartitionsScore[j + 1];
146 int64_t NumEntries = j - i + 1;
149 Score += PartitionScores::SingleCase;
150 else if (NumEntries <= SmallNumberOfEntries)
151 Score += PartitionScores::FewCases;
152 else if (NumEntries >= MinJumpTableEntries)
153 Score += PartitionScores::Table;
157 if (NumPartitions < MinPartitions[i] ||
158 (NumPartitions == MinPartitions[i] && Score > PartitionsScore[i])) {
159 MinPartitions[i] = NumPartitions;
161 PartitionsScore[i] = Score;
168 unsigned DstIndex = 0;
169 for (
unsigned First = 0, Last; First <
N; First = Last + 1) {
170 Last = LastElement[First];
172 assert(DstIndex <= First);
173 unsigned NumClusters = Last - First + 1;
176 if (NumClusters >= MinJumpTableEntries &&
177 buildJumpTable(Clusters, First, Last, SI, DefaultMBB, JTCluster)) {
178 Clusters[DstIndex++] = JTCluster;
180 for (
unsigned I = First;
I <= Last; ++
I)
181 std::memmove(&Clusters[DstIndex++], &Clusters[
I],
sizeof(Clusters[I]));
184 Clusters.resize(DstIndex);
188 unsigned First,
unsigned Last,
195 unsigned NumCmps = 0;
196 std::vector<MachineBasicBlock*> Table;
200 for (
unsigned I = First;
I <= Last; ++
I)
203 for (
unsigned I = First;
I <= Last; ++
I) {
205 Prob += Clusters[
I].Prob;
206 const APInt &Low = Clusters[
I].Low->getValue();
207 const APInt &
High = Clusters[
I].High->getValue();
208 NumCmps += (Low ==
High) ? 1 : 2;
211 const APInt &PreviousHigh = Clusters[
I - 1].High->getValue();
213 uint64_t Gap = (Low - PreviousHigh).getLimitedValue() - 1;
214 for (uint64_t J = 0; J < Gap; J++)
215 Table.push_back(DefaultMBB);
217 uint64_t ClusterSize = (High - Low).getLimitedValue() + 1;
218 for (uint64_t J = 0; J < ClusterSize; ++J)
219 Table.push_back(Clusters[
I].MBB);
220 JTProbs[Clusters[
I].MBB] += Clusters[
I].Prob;
223 unsigned NumDests = JTProbs.
size();
224 if (TLI->isSuitableForBitTests(NumDests, NumCmps,
225 Clusters[First].Low->getValue(),
226 Clusters[Last].High->getValue(), *DL)) {
240 if (Done.
count(Succ))
242 addSuccessorWithProb(JumpTableMBB, Succ, JTProbs[Succ]);
248 ->createJumpTableIndex(Table);
255 JTCases.emplace_back(std::move(JTH), std::move(JT));
258 JTCases.size() - 1, Prob);
269 assert(!Clusters.empty());
273 for (
unsigned i = 1; i < Clusters.size(); ++i)
274 assert(Clusters[i-1].
High->getValue().slt(Clusters[i].Low->getValue()));
282 EVT PTy = TLI->getPointerTy(*DL);
283 if (!TLI->isOperationLegal(
ISD::SHL, PTy))
287 const int64_t
N = Clusters.size();
297 MinPartitions[N - 1] = 1;
298 LastElement[N - 1] = N - 1;
301 for (int64_t i = N - 2; i >= 0; --i) {
304 MinPartitions[i] = MinPartitions[i + 1] + 1;
309 for (int64_t j = std::min(N - 1, i + BitWidth - 1); j > i; --j) {
313 if (!TLI->rangeFitsInWord(Clusters[i].Low->getValue(),
314 Clusters[j].High->getValue(), *DL))
319 bool RangesOnly =
true;
320 BitVector Dests(FuncInfo.MF->getNumBlockIDs());
321 for (int64_t k = i; k <= j; k++) {
326 Dests.set(Clusters[k].MBB->getNumber());
328 if (!RangesOnly || Dests.count() > 3)
332 unsigned NumPartitions = 1 + (j == N - 1 ? 0 : MinPartitions[j + 1]);
333 if (NumPartitions < MinPartitions[i]) {
335 MinPartitions[i] = NumPartitions;
342 unsigned DstIndex = 0;
343 for (
unsigned First = 0, Last; First <
N; First = Last + 1) {
344 Last = LastElement[First];
346 assert(DstIndex <= First);
349 if (buildBitTests(Clusters, First, Last, SI, BitTestCluster)) {
350 Clusters[DstIndex++] = BitTestCluster;
352 size_t NumClusters = Last - First + 1;
353 std::memmove(&Clusters[DstIndex], &Clusters[First],
354 sizeof(Clusters[0]) * NumClusters);
355 DstIndex += NumClusters;
358 Clusters.resize(DstIndex);
362 unsigned First,
unsigned Last,
369 BitVector Dests(FuncInfo.MF->getNumBlockIDs());
370 unsigned NumCmps = 0;
371 for (int64_t
I = First;
I <= Last; ++
I) {
373 Dests.set(Clusters[
I].MBB->getNumber());
374 NumCmps += (Clusters[
I].Low == Clusters[
I].High) ? 1 : 2;
376 unsigned NumDests = Dests.count();
378 APInt Low = Clusters[First].Low->getValue();
379 APInt High = Clusters[Last].High->getValue();
382 if (!TLI->isSuitableForBitTests(NumDests, NumCmps, Low, High, *DL))
388 const int BitWidth = TLI->getPointerTy(*DL).getSizeInBits();
389 assert(TLI->rangeFitsInWord(Low, High, *DL) &&
390 "Case range must fit in bit mask!");
394 bool ContiguousRange =
true;
395 for (int64_t
I = First + 1;
I <= Last; ++
I) {
396 if (Clusters[
I].Low->getValue() != Clusters[
I - 1].High->getValue() + 1) {
397 ContiguousRange =
false;
407 ContiguousRange =
false;
410 CmpRange = High - Low;
415 for (
unsigned i = First; i <= Last; ++i) {
418 for (j = 0; j < CBV.size(); ++j)
419 if (CBV[j].BB == Clusters[i].MBB)
427 uint64_t
Lo = (Clusters[i].Low->getValue() - LowBound).getZExtValue();
428 uint64_t
Hi = (Clusters[i].High->getValue() - LowBound).getZExtValue();
429 assert(Hi >= Lo && Hi < 64 &&
"Invalid bit case!");
430 CB->
Mask |= (-1ULL >> (63 - (Hi -
Lo))) << Lo;
431 CB->
Bits += Hi - Lo + 1;
433 TotalProb += Clusters[i].Prob;
446 for (
auto &CB : CBV) {
448 FuncInfo.MF->CreateMachineBasicBlock(SI->
getParent());
451 BitTestCases.emplace_back(std::move(LowBound), std::move(CmpRange),
453 ContiguousRange,
nullptr,
nullptr, std::move(BTI),
457 BitTestCases.size() - 1, TotalProb);
464 assert(CC.Low == CC.High &&
"Input clusters must be single-case");
472 const unsigned N = Clusters.size();
473 unsigned DstIndex = 0;
474 for (
unsigned SrcIndex = 0; SrcIndex <
N; ++SrcIndex) {
479 if (DstIndex != 0 && Clusters[DstIndex - 1].MBB == Succ &&
480 (CaseVal->
getValue() - Clusters[DstIndex - 1].High->getValue()) == 1) {
483 Clusters[DstIndex - 1].High = CaseVal;
484 Clusters[DstIndex - 1].Prob += CC.
Prob;
486 std::memmove(&Clusters[DstIndex++], &Clusters[SrcIndex],
487 sizeof(Clusters[SrcIndex]));
490 Clusters.resize(DstIndex);
This class represents lattice values for constants.
void push_back(const T &Elt)
bool buildBitTests(CaseClusterVector &Clusters, unsigned First, unsigned Last, const SwitchInst *SI, CaseCluster &BTCluster)
Build a bit test cluster from Clusters[First..Last].
Analysis providing profile information.
bool slt(const APInt &RHS) const
Signed less than comparison.
std::vector< CaseBits > CaseBitsVector
Value * getCondition() const
static CaseCluster jumpTable(const ConstantInt *Low, const ConstantInt *High, unsigned JTCasesIndex, BranchProbability Prob)
uint64_t getJumpTableRange(const CaseClusterVector &Clusters, unsigned First, unsigned Last)
Return the range of values within a range.
unsigned getBitWidth() const
Return the number of bits in the APInt.
MachineJumpTableInfo * getOrCreateJumpTableInfo(unsigned JTEntryKind)
getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it does already exist...
bool buildJumpTable(const CaseClusterVector &Clusters, unsigned First, unsigned Last, const SwitchInst *SI, MachineBasicBlock *DefaultMBB, CaseCluster &JTCluster)
Shift and rotation operations.
A cluster of cases suitable for jump table lowering.
std::vector< CaseCluster > CaseClusterVector
void findJumpTables(CaseClusterVector &Clusters, const SwitchInst *SI, MachineBasicBlock *DefaultMBB, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI)
const APInt & getValue() const
Return the constant as an APInt value reference.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
void normalizeSuccProbs()
Normalize probabilities of all successors so that the sum of them becomes one.
static CaseCluster bitTests(const ConstantInt *Low, const ConstantInt *High, unsigned BTCasesIndex, BranchProbability Prob)
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
void sortAndRangeify(CaseClusterVector &Clusters)
Sort Clusters and merge adjacent cases.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
BranchProbability ExtraProb
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
void sort(IteratorTy Start, IteratorTy End)
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
uint64_t getJumpTableNumCases(const SmallVectorImpl< unsigned > &TotalCases, unsigned First, unsigned Last)
Return the number of cases within a range.
Class for arbitrary precision integers.
void findBitTestClusters(CaseClusterVector &Clusters, const SwitchInst *SI)
A cluster of adjacent case labels with the same destination, or just one case.
const Function * getParent() const
Return the enclosing method, or null if none.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static BranchProbability getZero()
static APInt getNullValue(unsigned numBits)
Get the '0' value.
A cluster of case labels.
const BasicBlock * getParent() const