From 238b8867a8e7a18e9cdf1c72b049d8bed200ee7a Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 7 Nov 2018 14:12:41 +0000 Subject: [PATCH] [InstCombine] do not shrink switch conditions to illegal types (PR29009) This patch makes shrinking switch conditions less aggressive which was introduced by: rL274233 Note that we have 2 new bugs to track potential follow-ups that might have solved PR29009 in different ways: https://bugs.llvm.org/show_bug.cgi?id=39569 https://bugs.llvm.org/show_bug.cgi?id=39578 Patch by: @dendibakh (Denis Bakhvalov) Differential Revision: https://reviews.llvm.org/D54115 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346315 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../InstCombine/InstructionCombining.cpp | 8 ++- test/Transforms/InstCombine/narrow-switch.ll | 68 +++++++++++++++++-- 2 files changed, 66 insertions(+), 10 deletions(-) diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index fd64cc58a1dd8..3e72b1da7793b 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2510,9 +2510,11 @@ Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) { unsigned NewWidth = Known.getBitWidth() - std::max(LeadingKnownZeros, LeadingKnownOnes); // Shrink the condition operand if the new type is smaller than the old type. - // This may produce a non-standard type for the switch, but that's ok because - // the backend should extend back to a legal type for the target. - if (NewWidth > 0 && NewWidth < Known.getBitWidth()) { + // But do not shrink to a non-standard type, because backend can't generate + // good code for that yet. + // TODO: We can make it agressive again after fixing PR39569. + if (NewWidth > 0 && NewWidth < Known.getBitWidth() && + shouldChangeType(Known.getBitWidth(), NewWidth)) { IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth); Builder.SetInsertPoint(&SI); Value *NewCond = Builder.CreateTrunc(Cond, Ty, "trunc"); diff --git a/test/Transforms/InstCombine/narrow-switch.ll b/test/Transforms/InstCombine/narrow-switch.ll index 474bd820c8f8e..a8fa3e528db2c 100644 --- a/test/Transforms/InstCombine/narrow-switch.ll +++ b/test/Transforms/InstCombine/narrow-switch.ll @@ -3,9 +3,6 @@ ; RUN: opt < %s -instcombine -S -data-layout=n32 | FileCheck %s --check-prefix=ALL --check-prefix=CHECK32 ; RUN: opt < %s -instcombine -S -data-layout=n32:64 | FileCheck %s --check-prefix=ALL --check-prefix=CHECK64 -; In all cases, the data-layout is irrelevant. We should shrink as much as possible in InstCombine -; and allow the backend to expand as much as needed to ensure optimal codegen for any target. - define i32 @positive1(i64 %a) { ; ALL-LABEL: @positive1( ; ALL: switch i32 @@ -102,13 +99,19 @@ return: ; Make sure to avoid assertion crashes and use the type before ; truncation to generate the sub constant expressions that leads ; to the recomputed condition. +; We allow to truncate from i64 to i59 if in 32-bit mode, +; because both are illegal. define void @trunc64to59(i64 %a) { ; ALL-LABEL: @trunc64to59( -; ALL: switch i59 -; ALL-NEXT: i59 0, label %sw.bb1 -; ALL-NEXT: i59 18717182647723699, label %sw.bb2 -; ALL-NEXT: ] +; ALL-CHECK32: switch i59 +; ALL-CHECK32-NEXT: i59 0, label %sw.bb1 +; ALL-CHECK32-NEXT: i59 18717182647723699, label %sw.bb2 +; ALL-CHECK32-NEXT: ] +; ALL-CHECK64: switch i64 +; ALL-CHECK64-NEXT: i64 0, label %sw.bb1 +; ALL-CHECK64-NEXT: i64 18717182647723699, label %sw.bb2 +; ALL-CHECK64-NEXT: ] ; entry: %tmp0 = and i64 %a, 15 @@ -206,3 +209,54 @@ return: ; preds = %sw.epilog, %sw.bb2, ret i32 %rval } +; https://llvm.org/bugs/show_bug.cgi?id=29009 + +@a = global i32 0, align 4 +@njob = global i32 0, align 4 + +declare i32 @goo() + +; Make sure we do not shrink to illegal types (i3 in this case) +; if original type is legal (i32 in this case) + +define void @PR29009() { +; ALL-LABEL: @PR29009( +; ALL: switch i32 +; ALL-NEXT: i32 0, label +; ALL-NEXT: i32 3, label +; ALL-NEXT: ] +; + br label %1 + +;