Index: llvm/lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- llvm/lib/CodeGen/CodeGenPrepare.cpp +++ llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -6962,6 +6962,13 @@ Instruction *InsertPoint = I; for (Use *U : ToReplace) { auto *UI = cast(U->get()); + for (auto *U1 : UI->users()) { + auto *UI1 = cast(U1); + if (UI1->getParent() == TargetBB) { + InsertPoint = UI1; + break; + } + } Instruction *NI = UI->clone(); NewInstructions[UI] = NI; MaybeDead.insert(UI); Index: llvm/test/Transforms/CodeGenPrepare/AArch64/sink-free-instructions-1.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/CodeGenPrepare/AArch64/sink-free-instructions-1.ll @@ -0,0 +1,34 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -S -codegenprepare -mtriple=aarch64-unknown-linux-gnu < %s -o - | FileCheck %s + +target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128" +target triple = "aarch64-unknown-linux-gnu" + +define void @c(i16 %e, i8 %f) { +; CHECK-LABEL: @c( +; CHECK-NEXT: for.cond4.preheader.lr.ph: +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[F:%.*]], 0 +; CHECK-NEXT: [[CONV25:%.*]] = sext i16 [[E:%.*]] to i32 +; CHECK-NEXT: br i1 [[CMP]], label [[FOR_COND4_PREHEADER_US_PREHEADER:%.*]], label [[FOR_COND4_PREHEADER_PREHEADER:%.*]] +; CHECK: for.cond4.preheader.us.preheader: +; CHECK-NEXT: [[TMP0:%.*]] = insertelement <4 x i32> poison, i32 [[CONV25]], i32 0 +; CHECK-NEXT: [[BROADCAST_SPLAT144:%.*]] = shufflevector <4 x i32> [[TMP0]], <4 x i32> poison, <4 x i32> zeroinitializer +; CHECK-NEXT: [[TMP1:%.*]] = mul <4 x i32> zeroinitializer, [[BROADCAST_SPLAT144]] +; CHECK-NEXT: ret void +; CHECK: for.cond4.preheader.preheader: +; CHECK-NEXT: ret void +; +for.cond4.preheader.lr.ph: + %cmp = icmp slt i8 %f, 0 + %conv25 = sext i16 %e to i32 + %broadcast.splatinsert143 = insertelement <4 x i32> poison, i32 %conv25, i32 0 + br i1 %cmp, label %for.cond4.preheader.us.preheader, label %for.cond4.preheader.preheader + +for.cond4.preheader.us.preheader: ; preds = %for.cond4.preheader.lr.ph + %broadcast.splat144 = shufflevector <4 x i32> %broadcast.splatinsert143, <4 x i32> poison, <4 x i32> zeroinitializer + %0 = mul <4 x i32> zeroinitializer, %broadcast.splat144 + ret void + +for.cond4.preheader.preheader: ; preds = %for.cond4.preheader.lr.ph + ret void +}