From bd10da16fd5320ca47b0cf7edcd88d3eabd6d0fe Mon Sep 17 00:00:00 2001 From: Nicolai Haehnle Date: Mon, 11 Jul 2016 14:11:51 +0000 Subject: [PATCH] [Sink] Don't move calls to readonly functions across stores Summary: Reviewers: hfinkel, majnemer, tstellarAMD, sunfish Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D17279 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275066 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/Sink.cpp | 8 ++- test/Transforms/Sink/call.ll | 112 +++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 test/Transforms/Sink/call.ll diff --git a/lib/Transforms/Scalar/Sink.cpp b/lib/Transforms/Scalar/Sink.cpp index 5fa43bda9d0..d9a296c6312 100644 --- a/lib/Transforms/Scalar/Sink.cpp +++ b/lib/Transforms/Scalar/Sink.cpp @@ -76,11 +76,15 @@ static bool isSafeToMove(Instruction *Inst, AliasAnalysis &AA, Inst->mayThrow()) return false; - // Convergent operations cannot be made control-dependent on additional - // values. if (auto CS = CallSite(Inst)) { + // Convergent operations cannot be made control-dependent on additional + // values. if (CS.hasFnAttr(Attribute::Convergent)) return false; + + for (Instruction *S : Stores) + if (AA.getModRefInfo(S, CS) & MRI_Mod) + return false; } return true; diff --git a/test/Transforms/Sink/call.ll b/test/Transforms/Sink/call.ll new file mode 100644 index 00000000000..5aaad4499f5 --- /dev/null +++ b/test/Transforms/Sink/call.ll @@ -0,0 +1,112 @@ +; RUN: opt < %s -basicaa -sink -S | FileCheck %s + +declare i32 @f_load_global() nounwind readonly +declare i32 @f_load_arg(i32*) nounwind readonly argmemonly +declare void @f_store_global(i32) nounwind +declare void @f_store_arg(i32*) nounwind argmemonly +declare void @f_readonly_arg(i32* readonly, i32*) nounwind argmemonly +declare i32 @f_readnone(i32) nounwind readnone + +@A = external global i32 +@B = external global i32 + +; Sink readonly call if no stores are in the way. +; +; CHECK-LABEL: @test_sink_no_stores( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_load_global +; CHECK-NEXT: ret i32 %l +define i32 @test_sink_no_stores(i1 %z) { + %l = call i32 @f_load_global() + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} + +; CHECK-LABEL: @test_sink_argmem_store( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_load_arg +; CHECK-NEXT: ret i32 %l +define i32 @test_sink_argmem_store(i1 %z) { + %l = call i32 @f_load_arg(i32* @A) + store i32 0, i32* @B + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} + +; CHECK-LABEL: @test_sink_argmem_call( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_load_arg +; CHECK-NEXT: ret i32 %l +define i32 @test_sink_argmem_call(i1 %z) { + %l = call i32 @f_load_arg(i32* @A) + call void @f_store_arg(i32* @B) + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} + +; CHECK-LABEL: @test_sink_argmem_multiple( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_load_arg +; CHECK-NEXT: ret i32 %l +define i32 @test_sink_argmem_multiple(i1 %z) { + %l = call i32 @f_load_arg(i32* @A) + call void @f_readonly_arg(i32* @A, i32* @B) + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} + +; But don't sink if there is a store. +; +; CHECK-LABEL: @test_nosink_store( +; CHECK: call i32 @f_load_global +; CHECK-NEXT: store i32 +define i32 @test_nosink_store(i1 %z) { + %l = call i32 @f_load_global() + store i32 0, i32* @A + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} + +; CHECK-LABEL: @test_nosink_call( +; CHECK: call i32 @f_load_global +; CHECK-NEXT: call void @f_store_global +define i32 @test_nosink_call(i1 %z) { + %l = call i32 @f_load_global() + call void @f_store_global(i32 0) + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} + +; readnone calls are sunk across stores. +; +; CHECK-LABEL: @test_sink_readnone( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_readnone( +; CHECK-NEXT: ret i32 %l +define i32 @test_sink_readnone(i1 %z) { + %l = call i32 @f_readnone(i32 0) + store i32 0, i32* @A + br i1 %z, label %true, label %false +true: + ret i32 %l +false: + ret i32 0 +} -- 2.50.1