Index: include/llvm/IR/Attributes.td =================================================================== --- include/llvm/IR/Attributes.td +++ include/llvm/IR/Attributes.td @@ -182,6 +182,7 @@ def NoNansFPMath : StrBoolAttr<"no-nans-fp-math">; def UnsafeFPMath : StrBoolAttr<"unsafe-fp-math">; def NoJumpTables : StrBoolAttr<"no-jump-tables">; +def NoLookupTables : StrBoolAttr<"no-lookup-tables">; class CompatRule { // The name of the function called to check the attribute of the caller and @@ -213,6 +214,7 @@ def : MergeRule<"setAND">; def : MergeRule<"setOR">; def : MergeRule<"setOR">; +def : MergeRule<"setOR">; def : MergeRule<"adjustCallerSSPLevel">; def : MergeRule<"adjustCallerStackProbes">; def : MergeRule<"adjustCallerStackProbeSize">; Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -5151,8 +5151,12 @@ const TargetTransformInfo &TTI) { assert(SI->getNumCases() > 1 && "Degenerate switch?"); - // Only build lookup table when we have a target that supports it. - if (!TTI.shouldBuildLookupTables()) + Function *Fn = SI->getParent()->getParent(); + StringRef FuncName = Fn->getName(); + // Only build lookup table when we have a target that supports it or the + // attribute is not set. + if (!TTI.shouldBuildLookupTables() || + (Fn->getFnAttribute("no-lookup-tables").getValueAsString() == "true")) return false; // FIXME: If the switch is too sparse for a lookup table, perhaps we could @@ -5333,7 +5337,6 @@ // If using a bitmask, use any value to fill the lookup table holes. Constant *DV = NeedMask ? ResultLists[PHI][0].second : DefaultResults[PHI]; - StringRef FuncName = SI->getParent()->getParent()->getName(); SwitchLookupTable Table(Mod, TableSize, MinCaseVal, ResultList, DV, DL, FuncName); Index: test/Transforms/Inline/attributes.ll =================================================================== --- test/Transforms/Inline/attributes.ll +++ test/Transforms/Inline/attributes.ll @@ -303,7 +303,50 @@ ; CHECK-NEXT: ret i32 } +; Check that no-lookup-tables flag propagates from inlined callee to caller + +define i32 @no-use-lookup-tables_callee0(i32 %i) { + ret i32 %i +; CHECK: @no-use-lookup-tables_callee0(i32 %i) { +; CHECK-NEXT: ret i32 +} + +define i32 @no-use-lookup-tables_callee1(i32 %i) "no-lookup-tables"="true" { + ret i32 %i +; CHECK: @no-use-lookup-tables_callee1(i32 %i) [[NOUSELOOKUPTABLES:#[0-9]+]] { +; CHECK-NEXT: ret i32 +} + +define i32 @test_no-use-lookup-tables0(i32 %i) { + %1 = call i32 @no-use-lookup-tables_callee0(i32 %i) + ret i32 %1 +; CHECK: @test_no-use-lookup-tables0(i32 %i) { +; CHECK-NEXT: ret i32 +} + +define i32 @test_no-use-lookup-tables1(i32 %i) { + %1 = call i32 @no-use-lookup-tables_callee1(i32 %i) + ret i32 %1 +; CHECK: @test_no-use-lookup-tables1(i32 %i) [[NOUSELOOKUPTABLES]] { +; CHECK-NEXT: ret i32 +} + +define i32 @test_no-use-lookup-tables2(i32 %i) "no-lookup-tables"="true" { + %1 = call i32 @no-use-lookup-tables_callee0(i32 %i) + ret i32 %1 +; CHECK: @test_no-use-lookup-tables2(i32 %i) [[NOUSELOOKUPTABLES]] { +; CHECK-NEXT: ret i32 +} + +define i32 @test_no-use-lookup-tables3(i32 %i) "no-lookup-tables"="true" { + %1 = call i32 @no-use-lookup-tables_callee1(i32 %i) + ret i32 %1 +; CHECK: @test_no-use-lookup-tables3(i32 %i) [[NOUSELOOKUPTABLES]] { +; CHECK-NEXT: ret i32 +} + ; CHECK: attributes [[FPMAD_FALSE]] = { "less-precise-fpmad"="false" } ; CHECK: attributes [[FPMAD_TRUE]] = { "less-precise-fpmad"="true" } ; CHECK: attributes [[NOIMPLICITFLOAT]] = { noimplicitfloat } ; CHECK: attributes [[NOUSEJUMPTABLES]] = { "no-jump-tables"="true" } +; CHECK: attributes [[NOUSELOOKUPTABLES]] = { "no-lookup-tables"="true" } Index: test/Transforms/SimplifyCFG/Hexagon/disable-lookup-table.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/Hexagon/disable-lookup-table.ll @@ -0,0 +1,36 @@ +; RUN: opt -S -O2 < %s | FileCheck %s -check-prefix=DISABLE +; RUN: opt -S -hexagon-emit-lookup-tables=true -O2 < %s | FileCheck %s -check-prefix=DISABLE +; RUN: opt -S -hexagon-emit-lookup-tables=false -O2 < %s | FileCheck %s -check-prefix=DISABLE +; The attribute "no-lookup-table"="true" disables the generation of switch generated lookup tables + +; DISABLE-NOT: @{{.*}} = private unnamed_addr constant [6 x i32] [i32 9, i32 20, i32 14, i32 22, i32 12, i32 5] + +target datalayout = "e-m:e-p:32:32:32-a:0-n16:32-i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048" +target triple = "hexagon-unknown--elf" + +; Function Attrs: noinline nounwind +define i32 @foo(i32 %c) #0 { +entry: + switch i32 %c, label %sw.default [ + i32 42, label %return + i32 43, label %sw.bb1 + i32 44, label %sw.bb2 + i32 45, label %sw.bb3 + i32 46, label %sw.bb4 + i32 47, label %sw.bb5 + i32 48, label %sw.bb6 + ] + +sw.bb1: br label %return +sw.bb2: br label %return +sw.bb3: br label %return +sw.bb4: br label %return +sw.bb5: br label %return +sw.bb6: br label %return +sw.default: br label %return +return: + %retval.0 = phi i32 [ 15, %sw.default ], [ 1, %sw.bb6 ], [ 62, %sw.bb5 ], [ 27, %sw.bb4 ], [ -1, %sw.bb3 ], [ 0, %sw.bb2 ], [ 123, %sw.bb1 ], [ 55, %entry ] + ret i32 %retval.0 +} + +attributes #0 = { noinline nounwind "no-lookup-tables"="true"} Index: test/Transforms/SimplifyCFG/disable-lookup-table.ll =================================================================== --- /dev/null +++ test/Transforms/SimplifyCFG/disable-lookup-table.ll @@ -0,0 +1,44 @@ +; RUN: opt < %s -latesimplifycfg -S -mtriple=x86_64-unknown-linux-gnu | FileCheck %s +; In the presence of "-no-lookup-tables"="true", late simplifycfg should not +; convert any switch cases to lookup tables. +; CHECK: @switch.table.bar = private unnamed_addr constant [4 x i32] [i32 55, i32 123, i32 0, i32 -1] +; CHECK-LABEL: foo +; CHECK-NOT: @switch.table.foo = private unnamed_addr constant [4 x i32] [i32 55, i32 123, i32 0, i32 -1] + +define i32 @foo(i32 %c) "no-lookup-tables"="true" { +entry: + switch i32 %c, label %sw.default [ + i32 42, label %return + i32 43, label %sw.bb1 + i32 44, label %sw.bb2 + i32 45, label %sw.bb3 + ] + +sw.bb1: br label %return +sw.bb2: br label %return +sw.bb3: br label %return +sw.default: br label %return +return: + %retval.0 = phi i32 [ 15, %sw.default ], [ -1, %sw.bb3 ], [ 0, %sw.bb2 ], [ 123, %sw.bb1 ], [ 55, %entry ] + ret i32 %retval.0 +} + + +define i32 @bar(i32 %c) { +entry: + switch i32 %c, label %sw.default [ + i32 42, label %return + i32 43, label %sw.bb1 + i32 44, label %sw.bb2 + i32 45, label %sw.bb3 + ] + +sw.bb1: br label %return +sw.bb2: br label %return +sw.bb3: br label %return +sw.default: br label %return +return: + %retval.0 = phi i32 [ 15, %sw.default ], [ -1, %sw.bb3 ], [ 0, %sw.bb2 ], [ 123, %sw.bb1 ], [ 55, %entry ] + ret i32 %retval.0 +} +