From f192a27ed3bacdf727f27857adeb6d3762383295 Mon Sep 17 00:00:00 2001 From: Changpeng Fang Date: Fri, 9 Oct 2020 16:20:26 -0700 Subject: [PATCH] Sink: Handle instruction sink when a user is dead Summary: The current instruction sink pass uses findNearestCommonDominator of all users to find block to sink the instruction to. However, a user may be in a dead block, which will result in unexpected behavior. This patch handles such cases by skipping dead blocks. This patch fixes: https://bugs.llvm.org/show_bug.cgi?id=47415 Reviewers: MaskRay, arsenm Differential Revision: https://reviews.llvm.org/D89166 --- llvm/lib/Transforms/Scalar/Sink.cpp | 3 +++ llvm/test/Transforms/Sink/dead-user.ll | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 llvm/test/Transforms/Sink/dead-user.ll diff --git a/llvm/lib/Transforms/Scalar/Sink.cpp b/llvm/lib/Transforms/Scalar/Sink.cpp index c430724..89cfbe3 100644 --- a/llvm/lib/Transforms/Scalar/Sink.cpp +++ b/llvm/lib/Transforms/Scalar/Sink.cpp @@ -135,6 +135,9 @@ static bool SinkInstruction(Instruction *Inst, for (Use &U : Inst->uses()) { Instruction *UseInst = cast(U.getUser()); BasicBlock *UseBlock = UseInst->getParent(); + // Don't worry about dead users. + if (!DT.isReachableFromEntry(UseBlock)) + continue; if (PHINode *PN = dyn_cast(UseInst)) { // PHI nodes use the operand in the predecessor block, not the block with // the PHI. diff --git a/llvm/test/Transforms/Sink/dead-user.ll b/llvm/test/Transforms/Sink/dead-user.ll new file mode 100644 index 0000000..2d21def --- /dev/null +++ b/llvm/test/Transforms/Sink/dead-user.ll @@ -0,0 +1,42 @@ +; Compiler should not be broken with a dead user. +; RUN: opt -sink -S < %s | FileCheck %s + +; CHECK-LABEL: @test( +; CHECK: bb.0: +; CHECK-NEXT: %conv = sext i16 %p1 to i32 +; CHECK-NEXT: br i1 undef, label %bb.1, label %bb.3 + +; CHECK: bb.1: ; preds = %bb.0 +; CHECK-NEXT: br label %bb.2 + +; CHECK: bb.2: ; preds = %bb.2, %bb.1 +; CHECK-NEXT: %and.2 = and i32 undef, %conv +; CHECK-NEXT: br label %bb.2 + +; CHECK: bb.3: ; preds = %bb.3, %bb.0 +; CHECK-NEXT: %and.3 = and i32 undef, %conv +; CHECK-NEXT: br label %bb.3 + +; CHECK: dead: ; preds = %dead +; CHECK-NEXT: %and.dead = and i32 undef, %conv +; CHECK-NEXT: br label %dead +define void @test(i16 %p1) { +bb.0: + %conv = sext i16 %p1 to i32 + br i1 undef, label %bb.1, label %bb.3 + +bb.1: ; preds = %bb.0 + br label %bb.2 + +bb.2: ; preds = %bb.2, %bb.1 + %and.2 = and i32 undef, %conv + br label %bb.2 + +bb.3: ; preds = %bb.3, %bb.0 + %and.3 = and i32 undef, %conv + br label %bb.3 + +dead: ; preds = %dead + %and.dead = and i32 undef, %conv + br label %dead +} -- 2.7.4