Index: lib/Transforms/Scalar/RewriteStatepointsForGC.cpp =================================================================== --- lib/Transforms/Scalar/RewriteStatepointsForGC.cpp +++ lib/Transforms/Scalar/RewriteStatepointsForGC.cpp @@ -1926,6 +1926,7 @@ ChainToBase.size() > ChainLengthThreshold) continue; + bool ReplaceUnrelocatedUse = false; // Handle the scenario where the RootOfChain is not equal to the // Base Value, but they are essentially the same phi values. if (RootOfChain != Info.PointerToBase[LiveValue]) { @@ -1948,6 +1949,7 @@ // findBasePointer's newly generated AlternateRootPhi is present in the // liveset of the call. assert(Info.LiveSet.count(AlternateRootPhi)); + ReplaceUnrelocatedUse = true; } // Compute cost of this chain unsigned Cost = chainToBasePointerCost(ChainToBase, TTI); @@ -1976,7 +1978,9 @@ // Utility function which clones all instructions from "ChainToBase" // and inserts them before "InsertBefore". Returns rematerialized value // which should be used after statepoint. - auto rematerializeChain = [&ChainToBase](Instruction *InsertBefore) { + auto rematerializeChain = [&ChainToBase]( + Instruction *InsertBefore, Value *RootOfChain, Value *AlternateLiveBase, + bool &ReplaceUnrelocatedUse) { Instruction *LastClonedValue = nullptr; Instruction *LastValue = nullptr; for (Instruction *Instr: ChainToBase) { @@ -2003,6 +2007,14 @@ "incorrect use in rematerialization chain"); } #endif + } else { + // For the first instruction, replace the use of unrelocated base i.e. + // RootOfChain/OrigRootPhi, with the corresponding PHI present in the + // live set. They have been proved to be the same PHI nodes. Note + // that the *only* use of the RootOfChain is the first Value in the + // ChainToBase list. + if (ReplaceUnrelocatedUse) + ClonedValue->replaceUsesOfWith(RootOfChain, AlternateLiveBase); } LastClonedValue = ClonedValue; @@ -2017,7 +2029,9 @@ if (CS.isCall()) { Instruction *InsertBefore = CS.getInstruction()->getNextNode(); assert(InsertBefore); - Instruction *RematerializedValue = rematerializeChain(InsertBefore); + Instruction *RematerializedValue = rematerializeChain( + InsertBefore, RootOfChain, Info.PointerToBase[LiveValue], + ReplaceUnrelocatedUse); Info.RematerializedValues[RematerializedValue] = LiveValue; } else { InvokeInst *Invoke = cast(CS.getInstruction()); @@ -2027,10 +2041,12 @@ Instruction *UnwindInsertBefore = &*Invoke->getUnwindDest()->getFirstInsertionPt(); - Instruction *NormalRematerializedValue = - rematerializeChain(NormalInsertBefore); - Instruction *UnwindRematerializedValue = - rematerializeChain(UnwindInsertBefore); + Instruction *NormalRematerializedValue = rematerializeChain( + NormalInsertBefore, RootOfChain, Info.PointerToBase[LiveValue], + ReplaceUnrelocatedUse); + Instruction *UnwindRematerializedValue = rematerializeChain( + UnwindInsertBefore, RootOfChain, Info.PointerToBase[LiveValue], + ReplaceUnrelocatedUse); Info.RematerializedValues[NormalRematerializedValue] = LiveValue; Info.RematerializedValues[UnwindRematerializedValue] = LiveValue; Index: test/Transforms/RewriteStatepointsForGC/rematerialize-derived-pointers.ll =================================================================== --- test/Transforms/RewriteStatepointsForGC/rematerialize-derived-pointers.ll +++ test/Transforms/RewriteStatepointsForGC/rematerialize-derived-pointers.ll @@ -285,7 +285,7 @@ ; CHECK: %statepoint_token = call token (i64, i32, void ()*, i32, i32, ...) @llvm.experimental.gc.statepoint ; CHECK: %basephi.base.relocated = call coldcc i8 addrspace(1)* @llvm.experimental.gc.relocate.p1i8(token %statepoint_token, i32 7, i32 7) ; (%basephi.base, %basephi.base) ; CHECK: %basephi.base.relocated.casted = bitcast i8 addrspace(1)* %basephi.base.relocated to i32 addrspace(1)* - ; CHECK: %ptr.gep.remat = getelementptr i32, i32 addrspace(1)* %basephi, i32 15 + ; CHECK: %ptr.gep.remat = getelementptr i32, i32 addrspace(1)* %basephi.base.relocated.casted, i32 15 ; CHECK: call void @use_obj32(i32 addrspace(1)* %ptr.gep.remat) @@ -296,3 +296,35 @@ call void @use_obj32(i32 addrspace(1)* %ptr.gep) ret void } + + +define void @test_intersecting_chains_with_phi(i1 %cond) gc "statepoint-example" { +; CHECK-LABEL: test_intersecting_chains_with_phi +entry: + %base1 = call i32 addrspace(1)* @new_instance() + %base2 = call i32 addrspace(1)* @new_instance() + br i1 %cond, label %here, label %there + +here: + br label %merge + +there: + br label %merge + +merge: + %basephi = phi i32 addrspace(1)* [ %base1, %here ], [ %base2, %there ] + %ptr.gep = getelementptr i32, i32 addrspace(1)* %basephi, i32 15 + %ptr.cast = bitcast i32 addrspace(1)* %ptr.gep to i64 addrspace(1)* + %ptr.cast2 = bitcast i32 addrspace(1)* %ptr.gep to i16 addrspace(1)* + call void @do_safepoint() [ "deopt"() ] + ; CHECK: statepoint + ; CHECK: %ptr.gep.remat1 = getelementptr i32, i32 addrspace(1)* %basephi.base.relocated.casted, i32 15 + ; CHECK: %ptr.cast.remat = bitcast i32 addrspace(1)* %ptr.gep.remat1 to i64 addrspace(1)* + ; CHECK: %ptr.gep.remat = getelementptr i32, i32 addrspace(1)* %basephi.base.relocated.casted, i32 15 + ; CHECK: %ptr.cast2.remat = bitcast i32 addrspace(1)* %ptr.gep.remat to i16 addrspace(1)* + ; CHECK: call void @use_obj64(i64 addrspace(1)* %ptr.cast.remat) + ; CHECK: call void @use_obj16(i16 addrspace(1)* %ptr.cast2.remat) + call void @use_obj64(i64 addrspace(1)* %ptr.cast) + call void @use_obj16(i16 addrspace(1)* %ptr.cast2) + ret void +}