From f10dba2c63b39c2e20f1fdbcbc3668336b28525b Mon Sep 17 00:00:00 2001 From: Roman Tereshin Date: Sat, 19 Jan 2019 01:41:03 +0000 Subject: [PATCH] Reapply "[CGP] Check for existing inttotpr before creating new one" Original commit: r351582 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@351618 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CodeGenPrepare.cpp | 21 ++++-- .../X86/sink-addrmode-cse-inttoptrs.ll | 64 +++++++++++++++++++ 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 test/Transforms/CodeGenPrepare/X86/sink-addrmode-cse-inttoptrs.ll diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp index c35f8666fa3..e33178c961d 100644 --- a/lib/CodeGen/CodeGenPrepare.cpp +++ b/lib/CodeGen/CodeGenPrepare.cpp @@ -4664,13 +4664,26 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr, // will look through it and provide only the integer value. In that case, // use it here. if (!DL->isNonIntegralPointerType(Addr->getType())) { + const auto getResultPtr = [MemoryInst, Addr, + &Builder](Value *Reg) -> Value * { + BasicBlock *BB = MemoryInst->getParent(); + for (User *U : Reg->users()) + if (auto *I2P = dyn_cast(U)) + if (I2P->getType() == Addr->getType() && I2P->getParent() == BB) { + if (isa(Reg) && + cast(Reg)->getParent() == BB) + I2P->moveAfter(cast(Reg)); + else + I2P->moveBefore(BB->getFirstNonPHI()); + return I2P; + } + return Builder.CreateIntToPtr(Reg, Addr->getType(), "sunkaddr"); + }; if (!ResultPtr && AddrMode.BaseReg) { - ResultPtr = Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(), - "sunkaddr"); + ResultPtr = getResultPtr(AddrMode.BaseReg); AddrMode.BaseReg = nullptr; } else if (!ResultPtr && AddrMode.Scale == 1) { - ResultPtr = Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(), - "sunkaddr"); + ResultPtr = getResultPtr(AddrMode.ScaledReg); AddrMode.Scale = 0; } } diff --git a/test/Transforms/CodeGenPrepare/X86/sink-addrmode-cse-inttoptrs.ll b/test/Transforms/CodeGenPrepare/X86/sink-addrmode-cse-inttoptrs.ll new file mode 100644 index 00000000000..ad33a2da5de --- /dev/null +++ b/test/Transforms/CodeGenPrepare/X86/sink-addrmode-cse-inttoptrs.ll @@ -0,0 +1,64 @@ +; RUN: opt -mtriple=x86_64-- -codegenprepare %s -S -o - | FileCheck %s --check-prefixes=CGP,COMMON +; RUN: opt -mtriple=x86_64-- -codegenprepare -load-store-vectorizer %s -S -o - | FileCheck %s --check-prefixes=LSV,COMMON + +; Make sure CodeGenPrepare doesn't emit multiple inttoptr instructions +; of the same integer value while sinking address computations, but +; rather CSEs them on the fly: excessive inttoptr's confuse SCEV +; into thinking that related pointers have nothing to do with each other. +; +; Triggering this problem involves having just right addressing modes, +; and verifying that the motivating pass (LoadStoreVectorizer) is able +; to benefit from it - just right LSV-policies. Hence the atypical combination +; of the target and datalayout / address spaces in this test. + +target datalayout = "p1:32:32:32" + +define void @test1(i32 %tmp, i32 %off) { +; COMMON-LABEL: @test1 +; CGP: = inttoptr +; CGP-NOT: = inttoptr +; LSV: = load <2 x float> +; LSV: = load <2 x float> +entry: + %tmp1 = inttoptr i32 %tmp to float addrspace(1)* + %arrayidx.i.7 = getelementptr inbounds float, float addrspace(1)* %tmp1, i32 %off + %add20.i.7 = add i32 %off, 1 + %arrayidx22.i.7 = getelementptr inbounds float, float addrspace(1)* %tmp1, i32 %add20.i.7 + br label %for.body + +for.body: + %tmp8 = phi float [ undef, %entry ], [ %tmp62, %for.body ] + %tmp28 = load float, float addrspace(1)* %arrayidx.i.7 + %tmp29 = load float, float addrspace(1)* %arrayidx22.i.7 + %arrayidx.i321.7 = getelementptr inbounds float, float addrspace(1)* %tmp1, i32 0 + %tmp43 = load float, float addrspace(1)* %arrayidx.i321.7 + %arrayidx22.i327.7 = getelementptr inbounds float, float addrspace(1)* %tmp1, i32 1 + %tmp44 = load float, float addrspace(1)* %arrayidx22.i327.7 + %tmp62 = tail call fast float @foo(float %tmp8, float %tmp44, float %tmp43, float %tmp29, float %tmp28) + br label %for.body +} + +define void @test2(i64 %a, i64 %b, i64 %c) { +; COMMON-LABEL: @test2 +; CGP: loop: +; CGP-NEXT: %mul = +; CGP-NEXT: = inttoptr i64 %mul +; CGP-NOT: = inttoptr +; LSV: store <2 x i64> +entry: + %mul.neg.i630 = add nsw i64 %a, -16 + br label %loop + +loop: + %mul = mul nsw i64 %b, -16 + %sub.i631 = add nsw i64 %mul.neg.i630, %mul + %tmp = inttoptr i64 %sub.i631 to i8* + %tmp1 = inttoptr i64 %sub.i631 to i64* + store i64 %c, i64* %tmp1, align 16 + %arrayidx172 = getelementptr inbounds i8, i8* %tmp, i64 8 + %tmp2 = bitcast i8* %arrayidx172 to i64* + store i64 42, i64* %tmp2, align 8 + br label %loop +} + +declare float @foo(float, float, float, float, float) -- 2.50.1