Index: llvm/include/llvm/Target/TargetLowering.h =================================================================== --- llvm/include/llvm/Target/TargetLowering.h +++ llvm/include/llvm/Target/TargetLowering.h @@ -1008,12 +1008,18 @@ return UseUnderscoreLongJmp; } - /// Return integer threshold on number of blocks to use jump tables rather + /// Return lower limit for number of blocks to use jump tables rather /// than if sequence. - int getMinimumJumpTableEntries() const { + unsigned getMinimumJumpTableEntries() const { return MinimumJumpTableEntries; } + /// Return upper limit for number of blocks to use jump tables rather + /// than if sequence. + unsigned getMaximumJumpTableEntries() const { + return MaximumJumpTableEntries; + } + /// If a physical register, this specifies the register that /// llvm.savestack/llvm.restorestack should save and restore. unsigned getStackPointerRegisterToSaveRestore() const { @@ -1341,10 +1347,16 @@ /// Indicate the number of blocks to generate jump tables rather than if /// sequence. - void setMinimumJumpTableEntries(int Val) { + void setMinimumJumpTableEntries(unsigned Val) { MinimumJumpTableEntries = Val; } + /// Indicate the maximum number of blocks in jump tables. + /// Set to zero to generate unlimited jump tables. + void setMaximumJumpTableEntries(unsigned Val) { + MaximumJumpTableEntries = Val ? Val : UINT_MAX; + } + /// If set to a physical register, this specifies the register that /// llvm.savestack/llvm.restorestack should save and restore. void setStackPointerRegisterToSaveRestore(unsigned R) { @@ -1936,8 +1948,11 @@ /// Defaults to false. bool UseUnderscoreLongJmp; - /// Number of blocks threshold to use jump tables. - int MinimumJumpTableEntries; + /// Lower limit for the number of blocks to use jump tables. + unsigned MinimumJumpTableEntries; + + /// Upper limit for the number of blocks to use jump tables. + unsigned MaximumJumpTableEntries; /// Information about the contents of the high-bits in boolean values held in /// a type wider than i1. See getBooleanContents. Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -8222,7 +8222,7 @@ return NumCases * 100 >= Range * Density; } -static inline bool areJTsAllowed(const TargetLowering &TLI, +static inline bool isJTAllowed(const TargetLowering &TLI, const SwitchInst *SI) { const Function *Fn = SI->getParent()->getParent(); if (Fn->getFnAttribute("no-jump-tables").getValueAsString() == "true") @@ -8321,28 +8321,30 @@ #endif const TargetLowering &TLI = DAG.getTargetLoweringInfo(); - if (!areJTsAllowed(TLI, SI)) - return; - const int64_t N = Clusters.size(); - const unsigned MinJumpTableSize = TLI.getMinimumJumpTableEntries(); + bool OptForSize = DefaultMBB->getParent()->getFunction()->optForSize(); + + const auto MinJumpTableSize = TLI.getMinimumJumpTableEntries(); + const auto MaxJumpTableSize = OptForSize ? + UINT_MAX : TLI.getMaximumJumpTableEntries(); + + const auto N = Clusters.size(); + if (N < MinJumpTableSize || !isJTAllowed(TLI, SI)) + return; // TotalCases[i]: Total nbr of cases in Clusters[0..i]. SmallVector TotalCases(N); - for (unsigned i = 0; i < N; ++i) { APInt Hi = Clusters[i].High->getValue(); APInt Lo = Clusters[i].Low->getValue(); TotalCases[i] = (Hi - Lo).getLimitedValue() + 1; - if (i != 0) + if (i > 0) TotalCases[i] += TotalCases[i - 1]; } - unsigned MinDensity = JumpTableDensity; - if (DefaultMBB->getParent()->getFunction()->optForSize()) - MinDensity = OptsizeJumpTableDensity; - if (N >= MinJumpTableSize - && isDense(Clusters, &TotalCases[0], 0, N - 1, MinDensity)) { + unsigned MinDensity = OptForSize ? OptsizeJumpTableDensity : JumpTableDensity; + if (MinJumpTableSize <= N && N <= MaxJumpTableSize && + isDense(Clusters, &TotalCases[0], 0, N - 1, MinDensity)) { // Cheap case: the whole range might be suitable for jump table. CaseCluster JTCluster; if (buildJumpTable(Clusters, 0, N - 1, SI, DefaultMBB, JTCluster)) { @@ -8363,65 +8365,74 @@ // order. In the choice between two optimal partitionings, it picks the one // which yields more jump tables. - // MinPartitions[i] is the minimum nbr of partitions of Clusters[i..N-1]. + // MinPartitions[i] is the minimum number of partitions of Clusters[i..N - 1]. SmallVector MinPartitions(N); // LastElement[i] is the last element of the partition starting at i. SmallVector LastElement(N); - // NumTables[i]: nbr of >= MinJumpTableSize partitions from Clusters[i..N-1]. + // NumTables[i] is the number of partitions of Clusters[i..N - 1] whose size + // is greater than the minimum size. SmallVector NumTables(N); - // Base case: There is only one way to partition Clusters[N-1]. + // Base case: There is only one way to partition Clusters[N - 1]. MinPartitions[N - 1] = 1; LastElement[N - 1] = N - 1; - assert(MinJumpTableSize > 1); NumTables[N - 1] = 0; - // Note: loop indexes are signed to avoid underflow. - for (int64_t i = N - 2; i >= 0; i--) { - // Find optimal partitioning of Clusters[i..N-1]. + assert(1 < MinJumpTableSize && MinJumpTableSize <= MaxJumpTableSize); + for (auto i = N - 1; i > 0; i--) { + auto k = i - 1; + + // Find optimal partitioning of Clusters[i..N - 1]. // Baseline: Put Clusters[i] into a partition on its own. - MinPartitions[i] = MinPartitions[i + 1] + 1; - LastElement[i] = i; - NumTables[i] = NumTables[i + 1]; + MinPartitions[k] = MinPartitions[i] + 1; + LastElement[k] = k; + NumTables[k] = NumTables[i]; // Search for a solution that results in fewer partitions. - for (int64_t j = N - 1; j > i; j--) { + for (auto j = N - 1; j > k; j--) { + auto l = j + 1; + // Try building a partition from Clusters[i..j]. - if (isDense(Clusters, &TotalCases[0], i, j, MinDensity)) { - unsigned NumPartitions = 1 + (j == N - 1 ? 0 : MinPartitions[j + 1]); - bool IsTable = j - i + 1 >= MinJumpTableSize; - unsigned Tables = IsTable + (j == N - 1 ? 0 : NumTables[j + 1]); + if (isDense(Clusters, &TotalCases[0], k, j, MinDensity)) { + unsigned CurPartitions = (l < N) ? MinPartitions[l] : 0; + unsigned NewPartitions = CurPartitions + 1; + + bool MinTable = (l - k) >= MinJumpTableSize; + unsigned MaxTables = (l - i) / MaxJumpTableSize; + unsigned CurTables = (l < N) ? NumTables[l] : 0; + unsigned NewTables = CurTables + MinTable + MaxTables; // If this j leads to fewer partitions, or same number of partitions // with more lookup tables, it is a better partitioning. - if (NumPartitions < MinPartitions[i] || - (NumPartitions == MinPartitions[i] && Tables > NumTables[i])) { - MinPartitions[i] = NumPartitions; - LastElement[i] = j; - NumTables[i] = Tables; + if (NewPartitions < MinPartitions[k] || + (NewPartitions == MinPartitions[k] && NewTables > NumTables[k])) { + MinPartitions[k] = NewPartitions; + LastElement[k] = j - MaxTables * MaxJumpTableSize; + NumTables[k] = NewTables; } } } } - // Iterate over the partitions, replacing some with jump tables in-place. - unsigned DstIndex = 0; + // Iterate over the partitions, replacing some with jump tables. + unsigned Count = 0; for (unsigned First = 0, Last; First < N; First = Last + 1) { Last = LastElement[First]; assert(Last >= First); - assert(DstIndex <= First); + assert(Count <= First); unsigned NumClusters = Last - First + 1; + assert(NumClusters <= MaxJumpTableSize); CaseCluster JTCluster; - if (NumClusters >= MinJumpTableSize && + if (MinJumpTableSize <= NumClusters && buildJumpTable(Clusters, First, Last, SI, DefaultMBB, JTCluster)) { - Clusters[DstIndex++] = JTCluster; + Clusters[Count++] = JTCluster; } else { for (unsigned I = First; I <= Last; ++I) - std::memmove(&Clusters[DstIndex++], &Clusters[I], sizeof(Clusters[I])); + std::memmove(&Clusters[Count++], &Clusters[I], sizeof(Clusters[I])); } } - Clusters.resize(DstIndex); + Clusters.resize(Count); } bool SelectionDAGBuilder::rangeFitsInWord(const APInt &Low, const APInt &High) { Index: llvm/lib/CodeGen/TargetLoweringBase.cpp =================================================================== --- llvm/lib/CodeGen/TargetLoweringBase.cpp +++ llvm/lib/CodeGen/TargetLoweringBase.cpp @@ -826,6 +826,7 @@ GatherAllAliasesMaxDepth = 6; MinStackArgumentAlignment = 1; MinimumJumpTableEntries = 4; + MaximumJumpTableEntries = UINT_MAX; // TODO: the default will be switched to 0 in the next commit, along // with the Target-specific changes necessary. MaxAtomicSizeInBitsSupported = 1024; Index: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp =================================================================== --- llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -523,6 +523,12 @@ setPrefFunctionAlignment(STI.getPrefFunctionAlignment()); setPrefLoopAlignment(STI.getPrefLoopAlignment()); + unsigned MinJT = STI.getMinimumJumpTableEntries(); + unsigned MaxJT = STI.getMaximumJumpTableEntries(); + + if (MinJT) setMinimumJumpTableEntries(MinJT); + if (MaxJT) setMaximumJumpTableEntries(MaxJT); + setHasExtractBitsInsn(true); setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); Index: llvm/lib/Target/AArch64/AArch64Subtarget.h =================================================================== --- llvm/lib/Target/AArch64/AArch64Subtarget.h +++ llvm/lib/Target/AArch64/AArch64Subtarget.h @@ -203,6 +203,9 @@ unsigned getPrefFunctionAlignment() const { return PrefFunctionAlignment; } unsigned getPrefLoopAlignment() const { return PrefLoopAlignment; } + unsigned getMinimumJumpTableEntries() const; + unsigned getMaximumJumpTableEntries() const; + /// CPU has TBI (top byte of addresses is ignored during HW address /// translation) and OS enables it. bool supportsAddressTopByteIgnored() const; Index: llvm/lib/Target/AArch64/AArch64Subtarget.cpp =================================================================== --- llvm/lib/Target/AArch64/AArch64Subtarget.cpp +++ llvm/lib/Target/AArch64/AArch64Subtarget.cpp @@ -26,14 +26,21 @@ #define GET_SUBTARGETINFO_TARGET_DESC #include "AArch64GenSubtargetInfo.inc" -static cl::opt -EnableEarlyIfConvert("aarch64-early-ifcvt", cl::desc("Enable the early if " - "converter pass"), cl::init(true), cl::Hidden); +static cl::opt EnableEarlyIfConvert + ("aarch64-early-ifcvt", cl::init(true), cl::Hidden, + cl::desc("Enable the early if converter pass")); // If OS supports TBI, use this flag to enable it. -static cl::opt -UseAddressTopByteIgnored("aarch64-use-tbi", cl::desc("Assume that top byte of " - "an address is ignored"), cl::init(false), cl::Hidden); +static cl::opt UseAddressTopByteIgnored + ("aarch64-use-tbi", cl::init(false), cl::Hidden, + cl::desc("Assume that top byte of an address is ignored")); + +static cl::opt MinJumpTableEntries + ("aarch64-min-jump-table", cl::init(0), cl::Hidden, + cl::desc("Set minimum number of jump table entries")); +static cl::opt MaxJumpTableEntries + ("aarch64-max-jump-table", cl::init(0), cl::Hidden, + cl::desc("Set maximum number of jump table entries")); AArch64Subtarget & AArch64Subtarget::initializeSubtargetDependencies(StringRef FS) { @@ -154,6 +161,14 @@ return EnableEarlyIfConvert; } +unsigned AArch64Subtarget::getMinimumJumpTableEntries() const { + return MinJumpTableEntries; +} + +unsigned AArch64Subtarget::getMaximumJumpTableEntries() const { + return MaxJumpTableEntries; +} + bool AArch64Subtarget::supportsAddressTopByteIgnored() const { if (!UseAddressTopByteIgnored) return false;