Index: llvm/lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- llvm/lib/CodeGen/CodeGenPrepare.cpp +++ llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -6959,9 +6959,18 @@ SetVector MaybeDead; DenseMap NewInstructions; - Instruction *InsertPoint = I; + + Instruction *InsertPoint = nullptr; for (Use *U : ToReplace) { auto *UI = cast(U->get()); + InsertPoint = I; + 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-linux < %s -o - | FileCheck %s + +; ModuleID = '' +source_filename = "" +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 i64 @c(i16 %e) { +; CHECK-LABEL: @c( +; CHECK-NEXT: for.cond4.preheader.lr.ph: +; CHECK-NEXT: [[CONV25:%.*]] = sext i16 [[E:%.*]] to i32 +; CHECK-NEXT: br i1 undef, 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> undef, [[BROADCAST_SPLAT144]] +; CHECK-NEXT: ret i64 undef +; CHECK: for.cond4.preheader.preheader: +; CHECK-NEXT: ret i64 undef +; +for.cond4.preheader.lr.ph: + %conv25 = sext i16 %e to i32 + %broadcast.splatinsert143 = insertelement <4 x i32> poison, i32 %conv25, i32 0 + br i1 undef, 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> undef, %broadcast.splat144 + ret i64 undef + +for.cond4.preheader.preheader: ; preds = %for.cond4.preheader.lr.ph + ret i64 undef +}