Index: lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- lib/Transforms/InstCombine/InstructionCombining.cpp +++ lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2484,7 +2484,8 @@ // 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()) { + 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"); Index: test/Transforms/InstCombine/narrow-switch.ll =================================================================== --- test/Transforms/InstCombine/narrow-switch.ll +++ test/Transforms/InstCombine/narrow-switch.ll @@ -105,10 +105,14 @@ 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 +210,54 @@ 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 + +;