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 @@ -4875,11 +4875,16 @@ return false; // Ensure that this isn't going to produce an unsupported memory access. - if (ShAmt && - !TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, - LDST->getAddressSpace(), ShAmt / 8, - LDST->getMemOperand()->getFlags())) - return false; + if (ShAmt) { + assert(ShAmt % 8 == 0 && "ShAmt is byte offset"); + const unsigned ByteShAmt = ShAmt / 8; + const Align LDSTAlign = LDST->getAlign(); + const Align NarrowAlign = commonAlignment(LDSTAlign, ByteShAmt); + if (!TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT, + LDST->getAddressSpace(), NarrowAlign.value(), + LDST->getMemOperand()->getFlags())) + return false; + } // It's not possible to generate a constant of extended or untyped type. EVT PtrType = LDST->getBasePtr().getValueType(); diff --git a/llvm/test/CodeGen/ARM/and-load-combine.ll b/llvm/test/CodeGen/ARM/and-load-combine.ll --- a/llvm/test/CodeGen/ARM/and-load-combine.ll +++ b/llvm/test/CodeGen/ARM/and-load-combine.ll @@ -1433,12 +1433,9 @@ ; ; THUMB1-LABEL: test23: ; THUMB1: @ %bb.0: -; THUMB1-NEXT: ldrb r1, [r0, #3] -; THUMB1-NEXT: ldrb r0, [r0, #4] -; THUMB1-NEXT: lsls r0, r0, #8 -; THUMB1-NEXT: adds r1, r0, r1 -; THUMB1-NEXT: lsls r0, r1, #24 -; THUMB1-NEXT: lsrs r1, r1, #8 +; THUMB1-NEXT: ldrb r1, [r0, #4] +; THUMB1-NEXT: ldrb r0, [r0, #3] +; THUMB1-NEXT: lsls r0, r0, #24 ; THUMB1-NEXT: bx lr ; ; THUMB2-LABEL: test23: @@ -1498,13 +1495,15 @@ ; ; THUMB1-LABEL: test25: ; THUMB1: @ %bb.0: -; THUMB1-NEXT: ldrb r1, [r0, #5] -; THUMB1-NEXT: ldrb r0, [r0, #6] -; THUMB1-NEXT: lsls r0, r0, #8 -; THUMB1-NEXT: adds r0, r0, r1 -; THUMB1-NEXT: lsls r1, r0, #8 +; THUMB1-NEXT: ldr r0, [r0, #4] +; THUMB1-NEXT: ldr r1, .LCPI37_0 +; THUMB1-NEXT: ands r1, r0 ; THUMB1-NEXT: movs r0, #0 ; THUMB1-NEXT: bx lr +; THUMB1-NEXT: .p2align 2 +; THUMB1-NEXT: @ %bb.1: +; THUMB1-NEXT: .LCPI37_0: +; THUMB1-NEXT: .long 16776960 @ 0xffff00 ; ; THUMB2-LABEL: test25: ; THUMB2: @ %bb.0: