Index: llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp =================================================================== --- llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp +++ llvm/lib/CodeGen/SelectionDAG/SelectionDAGAddressAnalysis.cpp @@ -96,16 +96,43 @@ } // Consume constant adds & ors with appropriate masking. - while (Base->getOpcode() == ISD::ADD || Base->getOpcode() == ISD::OR) { - if (auto *C = dyn_cast(Base->getOperand(1))) { + while (true) { + switch (Base->getOpcode()) { + case ISD::OR: // Only consider ORs which act as adds. - if (Base->getOpcode() == ISD::OR && - !DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) - break; - Offset += C->getSExtValue(); - Base = Base->getOperand(0); - continue; + if (auto *C = dyn_cast(Base->getOperand(1))) + if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) { + Offset += C->getSExtValue(); + Base = Base->getOperand(0); + continue; + } + break; + case ISD::ADD: + if (auto *C = dyn_cast(Base->getOperand(1))) { + Offset += C->getSExtValue(); + Base = Base->getOperand(0); + continue; + } + break; + case ISD::LOAD: + case ISD::STORE: { + auto *LSBase = cast(Base.getNode()); + if (LSBase->isIndexed() && + Base.getResNo() == (LSBase->getNumValues() - 2)) + if (auto *C = dyn_cast(LSBase->getOffset())) { + auto Off = C->getSExtValue(); + if (LSBase->getAddressingMode() == ISD::PRE_DEC || + LSBase->getAddressingMode() == ISD::POST_DEC) + Offset -= Off; + else + Offset += Off; + Base = LSBase->getBasePtr(); + continue; + } + break; + } } + // If we get here break out of the loop. break; } Index: llvm/test/CodeGen/AArch64/arm64-abi-varargs.ll =================================================================== --- llvm/test/CodeGen/AArch64/arm64-abi-varargs.ll +++ llvm/test/CodeGen/AArch64/arm64-abi-varargs.ll @@ -13,9 +13,8 @@ ; CHECK: ldr {{w[0-9]+}}, [sp, #72] ; Second vararg ; CHECK: ldr {{w[0-9]+}}, [{{x[0-9]+}}], #8 -; CHECK: add {{x[0-9]+}}, {{x[0-9]+}}, #8 ; Third vararg -; CHECK: ldr {{w[0-9]+}}, [{{x[0-9]+}}] +; CHECK: ldr {{w[0-9]+}}, [{{x[0-9]+}}], #8 %1 = alloca i32, align 4 %2 = alloca i32, align 4 %3 = alloca i32, align 4 Index: llvm/test/CodeGen/AArch64/swifterror.ll =================================================================== --- llvm/test/CodeGen/AArch64/swifterror.ll +++ llvm/test/CodeGen/AArch64/swifterror.ll @@ -316,12 +316,11 @@ ; First vararg ; CHECK-APPLE-DAG: orr {{x[0-9]+}}, [[ARGS]], #0x8 ; CHECK-APPLE-DAG: ldr {{w[0-9]+}}, [{{.*}}[[TMP]], #16] -; CHECK-APPLE-DAG: add {{x[0-9]+}}, {{x[0-9]+}}, #8 ; Second vararg ; CHECK-APPLE-DAG: ldr {{w[0-9]+}}, [{{x[0-9]+}}], #8 ; CHECK-APPLE-DAG: add {{x[0-9]+}}, {{x[0-9]+}}, #16 ; Third vararg -; CHECK-APPLE: ldr {{w[0-9]+}}, [{{x[0-9]+}}] +; CHECK-APPLE: ldr {{w[0-9]+}}, [{{x[0-9]+}}], #8 ; CHECK-APPLE: mov x21, x0 ; CHECK-APPLE-NOT: x21