diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -922,6 +922,12 @@ Known, TLO, Depth + 1)) return true; + // Try shrinking the operation as long as the shift amount will still be + // in range. + if ((ShAmt < DemandedBits.getActiveBits()) && + ShrinkDemandedOp(Op, BitWidth, DemandedBits, TLO)) + return true; + // Convert (shl (anyext x, c)) to (anyext (shl x, c)) if the high bits // are not demanded. This will likely allow the anyext to be folded away. if (Op0.getOpcode() == ISD::ANY_EXTEND) { diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp --- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp +++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp @@ -11038,6 +11038,12 @@ return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG); } + // (tbz (any_ext x), b) -> (tbz x, b) if we don't use the extended bits. + if (Op->getOpcode() == ISD::ANY_EXTEND && + Bit < Op->getOperand(0).getValueSizeInBits()) { + return getTestBitOperand(Op->getOperand(0), Bit, Invert, DAG); + } + if (Op->getNumOperands() != 2) return Op; diff --git a/llvm/test/CodeGen/X86/narrow-shl-cst.ll b/llvm/test/CodeGen/X86/narrow-shl-cst.ll --- a/llvm/test/CodeGen/X86/narrow-shl-cst.ll +++ b/llvm/test/CodeGen/X86/narrow-shl-cst.ll @@ -152,9 +152,8 @@ define i64 @test13(i64 %x, i64* %y) nounwind { ; CHECK-LABEL: test13: ; CHECK: # %bb.0: -; CHECK-NEXT: movq %rdi, %rax -; CHECK-NEXT: andl $127, %eax -; CHECK-NEXT: addq %rax, %rax +; CHECK-NEXT: addl %edi, %edi +; CHECK-NEXT: movzbl %dil, %eax ; CHECK-NEXT: movq %rax, (%rsi) ; CHECK-NEXT: retq %and = shl i64 %x, 1 diff --git a/llvm/test/CodeGen/X86/zext-logicop-shift-load.ll b/llvm/test/CodeGen/X86/zext-logicop-shift-load.ll --- a/llvm/test/CodeGen/X86/zext-logicop-shift-load.ll +++ b/llvm/test/CodeGen/X86/zext-logicop-shift-load.ll @@ -5,8 +5,8 @@ define i64 @test1(i8* %data) { ; CHECK-LABEL: test1: ; CHECK: # %bb.0: # %entry -; CHECK-NEXT: movzbl (%rdi), %eax -; CHECK-NEXT: shlq $2, %rax +; CHECK-NEXT: movl (%rdi), %eax +; CHECK-NEXT: shll $2, %eax ; CHECK-NEXT: andl $60, %eax ; CHECK-NEXT: retq entry: @@ -20,9 +20,10 @@ define i8* @test2(i8* %data) { ; CHECK-LABEL: test2: ; CHECK: # %bb.0: # %entry -; CHECK-NEXT: movzbl (%rdi), %eax -; CHECK-NEXT: andl $15, %eax -; CHECK-NEXT: leaq (%rdi,%rax,4), %rax +; CHECK-NEXT: movl (%rdi), %eax +; CHECK-NEXT: shll $2, %eax +; CHECK-NEXT: andl $60, %eax +; CHECK-NEXT: addq %rdi, %rax ; CHECK-NEXT: retq entry: %bf.load = load i8, i8* %data, align 4