Index: lib/Transforms/Scalar/Sink.cpp =================================================================== --- lib/Transforms/Scalar/Sink.cpp +++ lib/Transforms/Scalar/Sink.cpp @@ -173,11 +173,14 @@ 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; + + if (Inst->mayReadFromMemory() && !Stores.empty()) + return false; } return true; Index: test/Transforms/Sink/call.ll =================================================================== --- /dev/null +++ test/Transforms/Sink/call.ll @@ -0,0 +1,68 @@ +; RUN: opt < %s -basicaa -sink -S | FileCheck %s + +declare i32 @f_load_global() nounwind readonly +declare void @f_store_global(i32) nounwind +declare i32 @f_readnone(i32) nounwind readnone + +@A = external global i32 + +; Sink readonly call if no stores are in the way. +; +; CHECK-LABEL: @test1( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_load_global +; CHECK-NEXT: ret i32 %l +define i32 @test1(i1 %z) { + %l = call i32 @f_load_global() + 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: @test2( +; CHECK: call i32 @f_load_global +; CHECK-NEXT: store i32 +define i32 @test2(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 +} + +; ... or a non-readonly call +; +; CHECK-LABEL: @test3( +; CHECK: call i32 @f_load_global +; CHECK-NEXT: call void @f_store_global +define i32 @test3(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: @test4( +; CHECK: true: +; CHECK-NEXT: %l = call i32 @f_readnone( +; CHECK-NEXT: ret i32 %l +define i32 @test4(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 +}