From fe4432b105665badd6a88a120767864e4a2efbae Mon Sep 17 00:00:00 2001 From: Oliver Stannard Date: Mon, 17 Oct 2016 12:00:24 +0000 Subject: [PATCH] [SimplifyCFG] Don't lower complex ConstantExprs to lookup tables Not all ConstantExprs can be represented by a global variable, for example most pointer arithmetic other than addition of a constant, so we can't convert these values from switch statements to lookup tables. Differential Revision: https://reviews.llvm.org/D25550 llvm-svn: 284379 --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 5 ++- .../switch-to-lookup-table-constant-expr.ll | 40 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 llvm/test/Transforms/SimplifyCFG/ARM/switch-to-lookup-table-constant-expr.ll diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 21bb87b84fcc..c7c66fd9d74c 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -4449,9 +4449,12 @@ static bool ValidLookupTableConstant(Constant *C, const TargetTransformInfo &TTI !isa(C) && !isa(C)) return false; - if (ConstantExpr *CE = dyn_cast(C)) + if (ConstantExpr *CE = dyn_cast(C)) { if (!CE->isGEPWithNoNotionalOverIndexing()) return false; + if (!ValidLookupTableConstant(CE->getOperand(0), TTI)) + return false; + } if (!TTI.shouldBuildLookupTablesForConstant(C)) return false; diff --git a/llvm/test/Transforms/SimplifyCFG/ARM/switch-to-lookup-table-constant-expr.ll b/llvm/test/Transforms/SimplifyCFG/ARM/switch-to-lookup-table-constant-expr.ll new file mode 100644 index 000000000000..25c4987bfb18 --- /dev/null +++ b/llvm/test/Transforms/SimplifyCFG/ARM/switch-to-lookup-table-constant-expr.ll @@ -0,0 +1,40 @@ +; RUN: opt -S -simplifycfg < %s | FileCheck %s --check-prefix=CHECK + +target datalayout = "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64" +target triple = "armv7a--none-eabi" + +; One of the phi node's values is too complex to be represented by a global +; variable, so we can't convert to a lookup table. + +; CHECK-NOT: @switch.table +; CHECK-NOT: load + +@g1 = external global i32, align 4 +@g2 = external global i32, align 4 +@g3 = external global i32, align 4 +@g4 = external thread_local global i32, align 4 + +define i32* @test3(i32 %n) { +entry: + switch i32 %n, label %sw.default [ + i32 0, label %sw.bb + i32 1, label %sw.bb1 + i32 2, label %sw.bb2 + ] + +sw.bb: + br label %return + +sw.bb1: + br label %return + +sw.bb2: + br label %return + +sw.default: + br label %return + +return: + %retval.0 = phi i32* [ @g4, %sw.default ], [ getelementptr inbounds (i32, i32* inttoptr (i32 mul (i32 ptrtoint (i32* @g3 to i32), i32 2) to i32*), i32 1), %sw.bb2 ], [ @g2, %sw.bb1 ], [ @g1, %sw.bb ] + ret i32* %retval.0 +}