diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -2445,6 +2445,14 @@ if (Sel.getOpcode() != ISD::SELECT || !Sel.hasOneUse()) { SelOpNo = 1; Sel = BO->getOperand(1); + + // Peek through any trunc/zext to shift amount type. + if ((BinOpcode == ISD::SHL || BinOpcode == ISD::SRA || + BinOpcode == ISD::SRL) && + (Sel.getOpcode() == ISD::TRUNCATE || + Sel.getOpcode() == ISD::ZERO_EXTEND) && + Sel.hasOneUse()) + Sel = Sel.getOperand(0); } if (Sel.getOpcode() != ISD::SELECT || !Sel.hasOneUse()) diff --git a/llvm/test/CodeGen/PowerPC/select_const.ll b/llvm/test/CodeGen/PowerPC/select_const.ll --- a/llvm/test/CodeGen/PowerPC/select_const.ll +++ b/llvm/test/CodeGen/PowerPC/select_const.ll @@ -610,13 +610,24 @@ } define i8 @shl_constant_sel_constants(i1 %cond) { -; ALL-LABEL: shl_constant_sel_constants: -; ALL: # %bb.0: -; ALL-NEXT: clrlwi 3, 3, 31 -; ALL-NEXT: li 4, 1 -; ALL-NEXT: xori 3, 3, 3 -; ALL-NEXT: slw 3, 4, 3 -; ALL-NEXT: blr +; ISEL-LABEL: shl_constant_sel_constants: +; ISEL: # %bb.0: +; ISEL-NEXT: andi. 3, 3, 1 +; ISEL-NEXT: li 4, 4 +; ISEL-NEXT: li 3, 8 +; ISEL-NEXT: iselgt 3, 4, 3 +; ISEL-NEXT: blr +; +; NO_ISEL-LABEL: shl_constant_sel_constants: +; NO_ISEL: # %bb.0: +; NO_ISEL-NEXT: andi. 3, 3, 1 +; NO_ISEL-NEXT: li 4, 4 +; NO_ISEL-NEXT: li 3, 8 +; NO_ISEL-NEXT: bc 12, 1, .LBB37_1 +; NO_ISEL-NEXT: blr +; NO_ISEL-NEXT: .LBB37_1: +; NO_ISEL-NEXT: addi 3, 4, 0 +; NO_ISEL-NEXT: blr %sel = select i1 %cond, i8 2, i8 3 %bo = shl i8 1, %sel ret i8 %bo @@ -647,13 +658,24 @@ } define i8 @lshr_constant_sel_constants(i1 %cond) { -; ALL-LABEL: lshr_constant_sel_constants: -; ALL: # %bb.0: -; ALL-NEXT: clrlwi 3, 3, 31 -; ALL-NEXT: li 4, 64 -; ALL-NEXT: xori 3, 3, 3 -; ALL-NEXT: srw 3, 4, 3 -; ALL-NEXT: blr +; ISEL-LABEL: lshr_constant_sel_constants: +; ISEL: # %bb.0: +; ISEL-NEXT: andi. 3, 3, 1 +; ISEL-NEXT: li 4, 16 +; ISEL-NEXT: li 3, 8 +; ISEL-NEXT: iselgt 3, 4, 3 +; ISEL-NEXT: blr +; +; NO_ISEL-LABEL: lshr_constant_sel_constants: +; NO_ISEL: # %bb.0: +; NO_ISEL-NEXT: andi. 3, 3, 1 +; NO_ISEL-NEXT: li 4, 16 +; NO_ISEL-NEXT: li 3, 8 +; NO_ISEL-NEXT: bc 12, 1, .LBB39_1 +; NO_ISEL-NEXT: blr +; NO_ISEL-NEXT: .LBB39_1: +; NO_ISEL-NEXT: addi 3, 4, 0 +; NO_ISEL-NEXT: blr %sel = select i1 %cond, i8 2, i8 3 %bo = lshr i8 64, %sel ret i8 %bo @@ -672,13 +694,24 @@ } define i8 @ashr_constant_sel_constants(i1 %cond) { -; ALL-LABEL: ashr_constant_sel_constants: -; ALL: # %bb.0: -; ALL-NEXT: clrlwi 3, 3, 31 -; ALL-NEXT: li 4, -128 -; ALL-NEXT: xori 3, 3, 3 -; ALL-NEXT: sraw 3, 4, 3 -; ALL-NEXT: blr +; ISEL-LABEL: ashr_constant_sel_constants: +; ISEL: # %bb.0: +; ISEL-NEXT: andi. 3, 3, 1 +; ISEL-NEXT: li 4, -32 +; ISEL-NEXT: li 3, -16 +; ISEL-NEXT: iselgt 3, 4, 3 +; ISEL-NEXT: blr +; +; NO_ISEL-LABEL: ashr_constant_sel_constants: +; NO_ISEL: # %bb.0: +; NO_ISEL-NEXT: andi. 3, 3, 1 +; NO_ISEL-NEXT: li 4, -32 +; NO_ISEL-NEXT: li 3, -16 +; NO_ISEL-NEXT: bc 12, 1, .LBB41_1 +; NO_ISEL-NEXT: blr +; NO_ISEL-NEXT: .LBB41_1: +; NO_ISEL-NEXT: addi 3, 4, 0 +; NO_ISEL-NEXT: blr %sel = select i1 %cond, i8 2, i8 3 %bo = ashr i8 128, %sel ret i8 %bo diff --git a/llvm/test/CodeGen/X86/dagcombine-select.ll b/llvm/test/CodeGen/X86/dagcombine-select.ll --- a/llvm/test/CodeGen/X86/dagcombine-select.ll +++ b/llvm/test/CodeGen/X86/dagcombine-select.ll @@ -194,12 +194,10 @@ define i32 @shl_constant_sel_constants(i1 %cond) { ; CHECK-LABEL: shl_constant_sel_constants: ; CHECK: # %bb.0: -; CHECK-NEXT: movl %edi, %ecx -; CHECK-NEXT: andb $1, %cl -; CHECK-NEXT: xorb $3, %cl -; CHECK-NEXT: movl $1, %eax -; CHECK-NEXT: # kill: def $cl killed $cl killed $ecx -; CHECK-NEXT: shll %cl, %eax +; CHECK-NEXT: notb %dil +; CHECK-NEXT: movzbl %dil, %eax +; CHECK-NEXT: andl $1, %eax +; CHECK-NEXT: leal 4(,%rax,4), %eax ; CHECK-NEXT: retq %sel = select i1 %cond, i32 2, i32 3 %bo = shl i32 1, %sel @@ -209,12 +207,9 @@ define i32 @lshr_constant_sel_constants(i1 %cond) { ; CHECK-LABEL: lshr_constant_sel_constants: ; CHECK: # %bb.0: -; CHECK-NEXT: movl %edi, %ecx -; CHECK-NEXT: andb $1, %cl -; CHECK-NEXT: xorb $3, %cl -; CHECK-NEXT: movl $64, %eax -; CHECK-NEXT: # kill: def $cl killed $cl killed $ecx -; CHECK-NEXT: shrl %cl, %eax +; CHECK-NEXT: # kill: def $edi killed $edi def $rdi +; CHECK-NEXT: andl $1, %edi +; CHECK-NEXT: leal 8(,%rdi,8), %eax ; CHECK-NEXT: retq %sel = select i1 %cond, i32 2, i32 3 %bo = lshr i32 64, %sel @@ -224,12 +219,10 @@ define i32 @ashr_constant_sel_constants(i1 %cond) { ; CHECK-LABEL: ashr_constant_sel_constants: ; CHECK: # %bb.0: -; CHECK-NEXT: movl %edi, %ecx -; CHECK-NEXT: andb $1, %cl -; CHECK-NEXT: xorb $3, %cl -; CHECK-NEXT: movl $128, %eax -; CHECK-NEXT: # kill: def $cl killed $cl killed $ecx -; CHECK-NEXT: shrl %cl, %eax +; CHECK-NEXT: # kill: def $edi killed $edi def $rdi +; CHECK-NEXT: andl $1, %edi +; CHECK-NEXT: shll $4, %edi +; CHECK-NEXT: leal 16(%rdi), %eax ; CHECK-NEXT: retq %sel = select i1 %cond, i32 2, i32 3 %bo = ashr i32 128, %sel