Index: lib/Transforms/Scalar/RewriteStatepointsForGC.cpp =================================================================== --- lib/Transforms/Scalar/RewriteStatepointsForGC.cpp +++ lib/Transforms/Scalar/RewriteStatepointsForGC.cpp @@ -1976,7 +1976,8 @@ // 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) { Instruction *LastClonedValue = nullptr; Instruction *LastValue = nullptr; for (Instruction *Instr: ChainToBase) { @@ -1996,13 +1997,24 @@ assert(LastValue); ClonedValue->replaceUsesOfWith(LastValue, LastClonedValue); #ifndef NDEBUG - // Assert that cloned instruction does not use any instructions from - // this chain other than LastClonedValue for (auto OpValue : ClonedValue->operand_values()) { + // Assert that cloned instruction does not use any instructions from + // this chain other than LastClonedValue assert(!is_contained(ChainToBase, OpValue) && "incorrect use in rematerialization chain"); + // Assert that the cloned instruction does not use the RootOfChain + // or the AlternateLiveBase. + assert(OpValue != RootOfChain && OpValue != AlternateLiveBase); } #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 in the ChainToBase list is + // the first Value in the list. + if (RootOfChain != AlternateLiveBase) + ClonedValue->replaceUsesOfWith(RootOfChain, AlternateLiveBase); } LastClonedValue = ClonedValue; @@ -2017,7 +2029,8 @@ if (CS.isCall()) { Instruction *InsertBefore = CS.getInstruction()->getNextNode(); assert(InsertBefore); - Instruction *RematerializedValue = rematerializeChain(InsertBefore); + Instruction *RematerializedValue = rematerializeChain( + InsertBefore, RootOfChain, Info.PointerToBase[LiveValue]); Info.RematerializedValues[RematerializedValue] = LiveValue; } else { InvokeInst *Invoke = cast(CS.getInstruction()); @@ -2027,10 +2040,10 @@ Instruction *UnwindInsertBefore = &*Invoke->getUnwindDest()->getFirstInsertionPt(); - Instruction *NormalRematerializedValue = - rematerializeChain(NormalInsertBefore); - Instruction *UnwindRematerializedValue = - rematerializeChain(UnwindInsertBefore); + Instruction *NormalRematerializedValue = rematerializeChain( + NormalInsertBefore, RootOfChain, Info.PointerToBase[LiveValue]); + Instruction *UnwindRematerializedValue = rematerializeChain( + UnwindInsertBefore, RootOfChain, Info.PointerToBase[LiveValue]); 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 +}