Index: include/llvm/CodeGen/CommandFlags.h =================================================================== --- include/llvm/CodeGen/CommandFlags.h +++ include/llvm/CodeGen/CommandFlags.h @@ -237,6 +237,10 @@ cl::desc("Give unique names to every section"), cl::init(true)); +cl::opt NoUseJumpTables("no-jump-tables", + cl::desc("Disables construction of jumptables"), + cl::init(false)); + cl::opt JTableType("jump-table-type", cl::desc("Choose the type of Jump-Instruction Table for jumptable."), @@ -298,6 +302,7 @@ Options.EmulatedTLS = EmulatedTLS; Options.MCOptions = InitMCTargetOptionsFromFlags(); + Options.NoUseJumpTables = NoUseJumpTables; Options.JTType = JTableType; Options.ThreadModel = TMModel; Index: include/llvm/Target/TargetOptions.h =================================================================== --- include/llvm/Target/TargetOptions.h +++ include/llvm/Target/TargetOptions.h @@ -97,15 +97,16 @@ UnsafeFPMath(false), NoInfsFPMath(false), NoNaNsFPMath(false), HonorSignDependentRoundingFPMathOption(false), NoZerosInBSS(false), GuaranteedTailCallOpt(false), StackAlignmentOverride(0), - StackSymbolOrdering(true), - EnableFastISel(false), PositionIndependentExecutable(false), - UseInitArray(false), DisableIntegratedAS(false), - CompressDebugSections(false), FunctionSections(false), - DataSections(false), UniqueSectionNames(true), TrapUnreachable(false), - EmulatedTLS(false), FloatABIType(FloatABI::Default), + StackSymbolOrdering(true), EnableFastISel(false), + PositionIndependentExecutable(false), UseInitArray(false), + DisableIntegratedAS(false), CompressDebugSections(false), + FunctionSections(false), DataSections(false), + UniqueSectionNames(true), TrapUnreachable(false), EmulatedTLS(false), + FloatABIType(FloatABI::Default), AllowFPOpFusion(FPOpFusion::Standard), Reciprocals(TargetRecip()), - JTType(JumpTable::Single), ThreadModel(ThreadModel::POSIX), - EABIVersion(EABI::Default), DebuggerTuning(DebuggerKind::Default) {} + NoUseJumpTables(false), JTType(JumpTable::Single), + ThreadModel(ThreadModel::POSIX), EABIVersion(EABI::Default), + DebuggerTuning(DebuggerKind::Default) {} /// PrintMachineCode - This flag is enabled when the -print-machineinstrs /// option is specified on the command line, and should enable debugging @@ -241,6 +242,9 @@ /// This class encapsulates options for reciprocal-estimate code generation. TargetRecip Reciprocals; + // Avoid constructing Jump Tables + unsigned NoUseJumpTables : 1; + /// JTType - This flag specifies the type of jump-instruction table to /// create for functions that have the jumptable attribute. JumpTable::JumpTableType JTType; @@ -265,27 +269,18 @@ inline bool operator==(const TargetOptions &LHS, const TargetOptions &RHS) { #define ARE_EQUAL(X) LHS.X == RHS.X - return - ARE_EQUAL(UnsafeFPMath) && - ARE_EQUAL(NoInfsFPMath) && - ARE_EQUAL(NoNaNsFPMath) && - ARE_EQUAL(HonorSignDependentRoundingFPMathOption) && - ARE_EQUAL(NoZerosInBSS) && - ARE_EQUAL(GuaranteedTailCallOpt) && - ARE_EQUAL(StackAlignmentOverride) && - ARE_EQUAL(EnableFastISel) && - ARE_EQUAL(PositionIndependentExecutable) && - ARE_EQUAL(UseInitArray) && - ARE_EQUAL(TrapUnreachable) && - ARE_EQUAL(EmulatedTLS) && - ARE_EQUAL(FloatABIType) && - ARE_EQUAL(AllowFPOpFusion) && - ARE_EQUAL(Reciprocals) && - ARE_EQUAL(JTType) && - ARE_EQUAL(ThreadModel) && - ARE_EQUAL(EABIVersion) && - ARE_EQUAL(DebuggerTuning) && - ARE_EQUAL(MCOptions); + return ARE_EQUAL(UnsafeFPMath) && ARE_EQUAL(NoInfsFPMath) && + ARE_EQUAL(NoNaNsFPMath) && + ARE_EQUAL(HonorSignDependentRoundingFPMathOption) && + ARE_EQUAL(NoZerosInBSS) && ARE_EQUAL(GuaranteedTailCallOpt) && + ARE_EQUAL(StackAlignmentOverride) && ARE_EQUAL(EnableFastISel) && + ARE_EQUAL(PositionIndependentExecutable) && ARE_EQUAL(UseInitArray) && + ARE_EQUAL(TrapUnreachable) && ARE_EQUAL(EmulatedTLS) && + ARE_EQUAL(FloatABIType) && ARE_EQUAL(AllowFPOpFusion) && + ARE_EQUAL(Reciprocals) && ARE_EQUAL(NoUseJumpTables) && + ARE_EQUAL(JTType) && ARE_EQUAL(ThreadModel) && + ARE_EQUAL(EABIVersion) && ARE_EQUAL(DebuggerTuning) && + ARE_EQUAL(MCOptions); #undef ARE_EQUAL } Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp =================================================================== --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -7927,6 +7927,9 @@ CaseCluster &JTCluster) { assert(First <= Last); + if (TM.Options.NoUseJumpTables) + return false; + auto Prob = BranchProbability::getZero(); unsigned NumCmps = 0; std::vector Table; Index: test/CodeGen/X86/switch.ll =================================================================== --- test/CodeGen/X86/switch.ll +++ test/CodeGen/X86/switch.ll @@ -1,5 +1,6 @@ ; RUN: llc -mtriple=x86_64-linux-gnu %s -o - | FileCheck %s ; RUN: llc -mtriple=x86_64-linux-gnu %s -o - -O0 | FileCheck --check-prefix=NOOPT %s +; RUN: llc -mtriple=x86_64-linux-gnu %s -no-jump-tables -o - | FileCheck --check-prefix=NOJMP %s declare void @g(i32) @@ -23,11 +24,8 @@ ; CHECK: ja ; CHECK: jmpq *.LJTI ; NOOPT-LABEL: basic -; NOOPT: decl -; NOOPT: subl $4 -; NOOPT: ja -; NOOPT: movq .LJTI -; NOOPT: jmpq +; NOJMP-LABEL: basic +; NOJMP-NOT: jmpq *.LJTI } @@ -62,6 +60,7 @@ ; NOOPT: addl $-100 ; NOOPT: subl $4 ; NOOPT: jb +; NOJMP-LABEL: simple_ranges } @@ -237,12 +236,14 @@ ; Should pivot around 300 for two subtrees with two jump tables each. ; CHECK-LABEL: optimal_pivot2 +; NOJMP-LABEL: optimal_pivot2 ; CHECK-NOT: cmpl ; CHECK: cmpl $299 ; CHECK: jmpq *.LJTI ; CHECK: jmpq *.LJTI ; CHECK: jmpq *.LJTI ; CHECK: jmpq *.LJTI +; NOJMP-NOT: jmpq *.LJTI } @@ -285,6 +286,10 @@ ; NOOPT: je ; NOOPT: subl $15, %eax ; NOOPT: je + +; NOJMP-LABEL: optimal_jump_table1 +; NOJMP-NOT: jmpq *.LJTI + } @@ -454,7 +459,9 @@ ; Don't infloop on jump tables where the upper bound is the max value of the ; input type (in this case 127). ; CHECK-LABEL: int_max_table_cluster +; NOJMP-LABEL: int_max_table_cluster ; CHECK: jmpq *.LJTI +; NOJMP-NOT: jmpq *.LJTI } @@ -684,11 +691,14 @@ return: ret void ; CHECK-LABEL: jump_table_affects_balance +; NOJMP-LABEL: jump_table_affects_balance ; If the tree were balanced based on number of clusters, {0-3,100} would go on ; the left and {200,300} on the right. However, the jump table weights as much ; as its components, so 100 is selected as the pivot. ; CHECK-NOT: cmpl ; CHECK: cmpl $99 +; NOJMP-NOT: jmpq * + }