Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -4345,8 +4345,23 @@ if (none_of(AddrModeInsts, [&](Value *V) { return IsNonLocalValue(V, MemoryInst->getParent()); })) { - DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); - return false; + // All instructions are in the same basic block. + // Let's ensure that all of them earlier in basic block than memory + // instruction. + SmallPtrSet AddrModeInstsSet(AddrModeInsts.begin(), + AddrModeInsts.end()); + unsigned Count = AddrModeInstsSet.size(); + for (Instruction &I : *(MemoryInst->getParent())) { + if (&I == MemoryInst) + break; + Count -= AddrModeInstsSet.count(&I); + if (!Count) + break; + } + if (!Count) { + DEBUG(dbgs() << "CGP: Found local addrmode: " << AddrMode << "\n"); + return false; + } } // Insert this computation right after this user. Since our caller is Index: test/CodeGen/X86/sink-gep-before-mem-inst.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/sink-gep-before-mem-inst.ll @@ -0,0 +1,25 @@ +; RUN: opt < %s -S -codegenprepare -mtriple=x86_64-unknown-linux-gnu | FileCheck %s + +define i64 @test.after(i8 addrspace(1)* readonly align 8) { +; CHECK-LABEL: test.after +; CHECK: sunkaddr +entry: + %.0 = getelementptr inbounds i8, i8 addrspace(1)* %0, i64 8 + %addr = bitcast i8 addrspace(1)* %.0 to i32 addrspace(1)* + br label %header + +header: + %addr.in.loop = phi i32 addrspace(1)* [ %addr, %entry ], [ %addr.after, %header ] + %local_2_ = phi i64 [ 0, %entry ], [ %.9, %header ] + %.7 = load i32, i32 addrspace(1)* %addr.in.loop, align 8 + fence acquire + %.1 = getelementptr inbounds i8, i8 addrspace(1)* %0, i64 8 + %addr.after = bitcast i8 addrspace(1)* %.1 to i32 addrspace(1)* + %.8 = sext i32 %.7 to i64 + %.9 = add i64 %local_2_, %.8 + %not. = icmp sgt i64 %.9, 999 + br i1 %not., label %exit, label %header + +exit: + ret i64 %.9 +}