Index: lib/CodeGen/TailDuplication.cpp =================================================================== --- lib/CodeGen/TailDuplication.cpp +++ lib/CodeGen/TailDuplication.cpp @@ -352,6 +352,18 @@ return false; } +static bool isKilledInBB(unsigned Reg, MachineBasicBlock *BB, + const MachineRegisterInfo *MRI) +{ + for (MachineOperand &MO : MRI->use_operands(Reg)) { + if (MO.getParent()->getParent() != BB) + continue; + if (MO.isKill()) + return true; + } + return false; +} + static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB) { for (unsigned i = 1, e = MI->getNumOperands(); i != e; i += 2) if (MI->getOperand(i+1).getMBB() == SrcBB) @@ -408,7 +420,8 @@ // Insert a copy from source to the end of the block. The def register is the // available value liveout of the block. unsigned NewDef = MRI->createVirtualRegister(RC); - Copies.push_back(std::make_pair(NewDef, SrcReg)); + if (!isKilledInBB(DefReg, TailBB, MRI)) + Copies.push_back(std::make_pair(NewDef, SrcReg)); if (isDefLiveOut(DefReg, TailBB, MRI) || RegsUsedByPhi.count(DefReg)) AddSSAUpdateEntry(DefReg, NewDef, PredBB); Index: test/CodeGen/Generic/taildup-kill.ll =================================================================== --- /dev/null +++ test/CodeGen/Generic/taildup-kill.ll @@ -0,0 +1,48 @@ +; RUN: llc < %s -fast-isel -mcpu=core2 -O3 -verify-machineinstrs +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-apple-darwin14.0.0" + +%jl_value_t = type { %jl_value_t* } + +declare %jl_value_t* @jl_apply_generic() + +define void @julia_anonymous1421() { +pass25: ; preds = %L22 + br i1 undef, label %L31, label %if23 + +if23: ; preds = %pass25 + br i1 undef, label %ok27, label %fail17 + +ok27: ; preds = %if23 + br i1 undef, label %ok29, label %fail17 + +ok29: ; preds = %ok27 + br i1 undef, label %isf, label %notf + +isf: ; preds = %ok29 + %0 = getelementptr %jl_value_t** undef, i64 4 + br label %mergef + +fail17: ; preds = %ok16 + unreachable + +notf: ; preds = %ok29 + %1 = call %jl_value_t* @jl_apply_generic() + br label %mergef + +mergef: ; preds = %notf, %isf + %2 = phi %jl_value_t* [ undef, %isf ], [ %1, %notf ] + store %jl_value_t* %2, %jl_value_t** undef + br label %L34 + +L31: ; preds = %pass25 + br i1 undef, label %ok33, label %fail17 + +ok33: ; preds = %L31 + store %jl_value_t* undef, %jl_value_t** undef + br label %L34 + +L34: ; preds = %ok33, %mergef + unreachable + +}