From 64f27e78c4af3b69930b1d05b13b80cb025f4079 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Mon, 1 Oct 2012 11:31:48 +0000 Subject: [PATCH] SimplifyCFG: Don't crash when forming a switch bitmap with an undef default value. Fixes PR13985. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@164934 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/SimplifyCFG.cpp | 7 ++++-- .../SimplifyCFG/switch_to_lookup_table.ll | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index a5e4d44b85e..6da791bb1cb 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3351,8 +3351,11 @@ SwitchLookupTable::SwitchLookupTable(Module &M, APInt TableInt(TableSize * IT->getBitWidth(), 0); for (uint64_t I = TableSize; I > 0; --I) { TableInt <<= IT->getBitWidth(); - ConstantInt *Val = cast(TableContents[I - 1]); - TableInt |= Val->getValue().zext(TableInt.getBitWidth()); + // Insert values into the bitmap. Undef values are set to zero. + if (!isa(TableContents[I - 1])) { + ConstantInt *Val = cast(TableContents[I - 1]); + TableInt |= Val->getValue().zext(TableInt.getBitWidth()); + } } BitMap = ConstantInt::get(M.getContext(), TableInt); BitMapElementTy = IT; diff --git a/test/Transforms/SimplifyCFG/switch_to_lookup_table.ll b/test/Transforms/SimplifyCFG/switch_to_lookup_table.ll index 134ac4eeb14..aa48ec6481f 100644 --- a/test/Transforms/SimplifyCFG/switch_to_lookup_table.ll +++ b/test/Transforms/SimplifyCFG/switch_to_lookup_table.ll @@ -269,3 +269,27 @@ if.end: ; CHECK: switch ; CHECK: phi } + +; PR13985 +define i1 @undef(i32 %tmp) uwtable ssp { +bb: + switch i32 %tmp, label %bb3 [ + i32 0, label %bb1 + i32 1, label %bb1 + i32 7, label %bb2 + i32 8, label %bb2 + ] + +bb1: ; preds = %bb, %bb + br label %bb3 + +bb2: ; preds = %bb, %bb + br label %bb3 + +bb3: ; preds = %bb2, %bb1, %bb + %tmp4 = phi i1 [ undef, %bb ], [ false, %bb2 ], [ true, %bb1 ] + ret i1 %tmp4 +; CHECK: define i1 @undef +; CHECK: %switch.cast = trunc i32 %switch.tableidx to i9 +; CHECK: %switch.downshift = lshr i9 3, %switch.shiftamt +} -- 2.34.1