<div dir="ltr">Hi David,<div><br></div><div>I think this broke the Green Dragon ThinLTO Bot:</div><div><a href="http://green.lab.llvm.org/green/job/clang-stage2-Rthinlto/5938/">http://green.lab.llvm.org/green/job/clang-stage2-Rthinlto/5938/</a><br></div><div><br></div><div>Could you please take a look?</div><div><br></div><div>Thanks</div><div><br></div><div>Cheers,</div><div>Juergen</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 4, 2017 at 1:17 PM, Xinliang David Li via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: davidxl<br>
Date: Wed Oct 4 13:17:55 2017<br>
New Revision: 314928<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=314928&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=314928&view=rev</a><br>
Log:<br>
Recommit r314561 after fixing msan build failure<br>
<br>
(trial 2) Incoming val defined by terminator instruction which<br>
also requires bitcasts can not be handled.<br>
<br>
<br>
Added:<br>
llvm/trunk/test/Transforms/<wbr>InstCombine/intptr1.ll<br>
- copied unchanged from r314621, llvm/trunk/test/Transforms/<wbr>InstCombine/intptr1.ll<br>
llvm/trunk/test/Transforms/<wbr>InstCombine/intptr2.ll<br>
- copied unchanged from r314621, llvm/trunk/test/Transforms/<wbr>InstCombine/intptr2.ll<br>
llvm/trunk/test/Transforms/<wbr>InstCombine/intptr3.ll<br>
- copied unchanged from r314621, llvm/trunk/test/Transforms/<wbr>InstCombine/intptr3.ll<br>
llvm/trunk/test/Transforms/<wbr>InstCombine/intptr4.ll<br>
- copied unchanged from r314621, llvm/trunk/test/Transforms/<wbr>InstCombine/intptr4.ll<br>
Modified:<br>
llvm/trunk/lib/Transforms/<wbr>InstCombine/<wbr>InstCombineInternal.h<br>
llvm/trunk/lib/Transforms/<wbr>InstCombine/InstCombinePHI.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/<wbr>InstCombine/<wbr>InstCombineInternal.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=314928&r1=314927&r2=314928&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/<wbr>Transforms/InstCombine/<wbr>InstCombineInternal.h?rev=<wbr>314928&r1=314927&r2=314928&<wbr>view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Transforms/<wbr>InstCombine/<wbr>InstCombineInternal.h (original)<br>
+++ llvm/trunk/lib/Transforms/<wbr>InstCombine/<wbr>InstCombineInternal.h Wed Oct 4 13:17:55 2017<br>
@@ -670,6 +670,10 @@ private:<br>
Instruction *FoldPHIArgGEPIntoPHI(PHINode &PN);<br>
Instruction *FoldPHIArgLoadIntoPHI(PHINode &PN);<br>
Instruction *FoldPHIArgZextsIntoPHI(<wbr>PHINode &PN);<br>
+ /// If an integer typed PHI has only one use which is an IntToPtr operation,<br>
+ /// replace the PHI with an existing pointer typed PHI if it exists. Otherwise<br>
+ /// insert a new pointer typed PHI and replace the original one.<br>
+ Instruction *FoldIntegerTypedPHI(PHINode &PN);<br>
<br>
/// Helper function for FoldPHIArgXIntoPHI() to set debug location for the<br>
/// folded operation.<br>
<br>
Modified: llvm/trunk/lib/Transforms/<wbr>InstCombine/InstCombinePHI.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombinePHI.cpp?rev=314928&r1=314927&r2=314928&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/llvm/trunk/lib/<wbr>Transforms/InstCombine/<wbr>InstCombinePHI.cpp?rev=314928&<wbr>r1=314927&r2=314928&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- llvm/trunk/lib/Transforms/<wbr>InstCombine/InstCombinePHI.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/<wbr>InstCombine/InstCombinePHI.cpp Wed Oct 4 13:17:55 2017<br>
@@ -40,6 +40,233 @@ void InstCombiner::<wbr>PHIArgMergedDebugLoc(<br>
}<br>
}<br>
<br>
+// Replace Integer typed PHI PN if the PHI's value is used as a pointer value.<br>
+// If there is an existing pointer typed PHI that produces the same value as PN,<br>
+// replace PN and the IntToPtr operation with it. Otherwise, synthesize a new<br>
+// PHI node:<br>
+//<br>
+// Case-1:<br>
+// bb1:<br>
+// int_init = PtrToInt(ptr_init)<br>
+// br label %bb2<br>
+// bb2:<br>
+// int_val = PHI([int_init, %bb1], [int_val_inc, %bb2]<br>
+// ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]<br>
+// ptr_val2 = IntToPtr(int_val)<br>
+// ...<br>
+// use(ptr_val2)<br>
+// ptr_val_inc = ...<br>
+// inc_val_inc = PtrToInt(ptr_val_inc)<br>
+//<br>
+// ==><br>
+// bb1:<br>
+// br label %bb2<br>
+// bb2:<br>
+// ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]<br>
+// ...<br>
+// use(ptr_val)<br>
+// ptr_val_inc = ...<br>
+//<br>
+// Case-2:<br>
+// bb1:<br>
+// int_ptr = BitCast(ptr_ptr)<br>
+// int_init = Load(int_ptr)<br>
+// br label %bb2<br>
+// bb2:<br>
+// int_val = PHI([int_init, %bb1], [int_val_inc, %bb2]<br>
+// ptr_val2 = IntToPtr(int_val)<br>
+// ...<br>
+// use(ptr_val2)<br>
+// ptr_val_inc = ...<br>
+// inc_val_inc = PtrToInt(ptr_val_inc)<br>
+// ==><br>
+// bb1:<br>
+// ptr_init = Load(ptr_ptr)<br>
+// br label %bb2<br>
+// bb2:<br>
+// ptr_val = PHI([ptr_init, %bb1], [ptr_val_inc, %bb2]<br>
+// ...<br>
+// use(ptr_val)<br>
+// ptr_val_inc = ...<br>
+// ...<br>
+//<br>
+Instruction *InstCombiner::<wbr>FoldIntegerTypedPHI(PHINode &PN) {<br>
+ if (!PN.getType()->isIntegerTy())<br>
+ return nullptr;<br>
+ if (!PN.hasOneUse())<br>
+ return nullptr;<br>
+<br>
+ auto *IntToPtr = dyn_cast<IntToPtrInst>(PN.<wbr>user_back());<br>
+ if (!IntToPtr)<br>
+ return nullptr;<br>
+<br>
+ // Check if the pointer is actually used as pointer:<br>
+ auto HasPointerUse = [](Instruction *IIP) {<br>
+ for (User *U : IIP->users()) {<br>
+ Value *Ptr = nullptr;<br>
+ if (LoadInst *LoadI = dyn_cast<LoadInst>(U)) {<br>
+ Ptr = LoadI->getPointerOperand();<br>
+ } else if (StoreInst *SI = dyn_cast<StoreInst>(U)) {<br>
+ Ptr = SI->getPointerOperand();<br>
+ } else if (GetElementPtrInst *GI = dyn_cast<GetElementPtrInst>(U)<wbr>) {<br>
+ Ptr = GI->getPointerOperand();<br>
+ }<br>
+<br>
+ if (Ptr && Ptr == IIP)<br>
+ return true;<br>
+ }<br>
+ return false;<br>
+ };<br>
+<br>
+ if (!HasPointerUse(IntToPtr))<br>
+ return nullptr;<br>
+<br>
+ if (DL.getPointerSizeInBits(<wbr>IntToPtr->getAddressSpace()) !=<br>
+ DL.getTypeSizeInBits(IntToPtr-<wbr>>getOperand(0)->getType()))<br>
+ return nullptr;<br>
+<br>
+ SmallVector<Value *, 4> AvailablePtrVals;<br>
+ for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) {<br>
+ Value *Arg = PN.getIncomingValue(i);<br>
+<br>
+ // First look backward:<br>
+ if (auto *PI = dyn_cast<PtrToIntInst>(Arg)) {<br>
+ AvailablePtrVals.emplace_back(<wbr>PI->getOperand(0));<br>
+ continue;<br>
+ }<br>
+<br>
+ // Next look forward:<br>
+ Value *ArgIntToPtr = nullptr;<br>
+ for (User *U : Arg->users()) {<br>
+ if (isa<IntToPtrInst>(U) && U->getType() == IntToPtr->getType() &&<br>
+ (DT.dominates(cast<<wbr>Instruction>(U), PN.getIncomingBlock(i)) ||<br>
+ cast<Instruction>(U)-><wbr>getParent() == PN.getIncomingBlock(i))) {<br>
+ ArgIntToPtr = U;<br>
+ break;<br>
+ }<br>
+ }<br>
+<br>
+ if (ArgIntToPtr) {<br>
+ AvailablePtrVals.emplace_back(<wbr>ArgIntToPtr);<br>
+ continue;<br>
+ }<br>
+<br>
+ // If Arg is defined by a PHI, allow it. This will also create<br>
+ // more opportunities iteratively.<br>
+ if (isa<PHINode>(Arg)) {<br>
+ AvailablePtrVals.emplace_back(<wbr>Arg);<br>
+ continue;<br>
+ }<br>
+<br>
+ // For a single use integer load:<br>
+ auto *LoadI = dyn_cast<LoadInst>(Arg);<br>
+ if (!LoadI)<br>
+ return nullptr;<br>
+<br>
+ if (!LoadI->hasOneUse())<br>
+ return nullptr;<br>
+<br>
+ // Push the integer typed Load instruction into the available<br>
+ // value set, and fix it up later when the pointer typed PHI<br>
+ // is synthesized.<br>
+ AvailablePtrVals.emplace_back(<wbr>LoadI);<br>
+ }<br>
+<br>
+ // Now search for a matching PHI<br>
+ auto *BB = PN.getParent();<br>
+ assert(AvailablePtrVals.size() == PN.getNumIncomingValues() &&<br>
+ "Not enough available ptr typed incoming values");<br>
+ PHINode *MatchingPtrPHI = nullptr;<br>
+ for (auto II = BB->begin(), EI = BasicBlock::iterator(BB-><wbr>getFirstNonPHI());<br>
+ II != EI; II++) {<br>
+ PHINode *PtrPHI = dyn_cast<PHINode>(II);<br>
+ if (!PtrPHI || PtrPHI == &PN || PtrPHI->getType() != IntToPtr->getType())<br>
+ continue;<br>
+ MatchingPtrPHI = PtrPHI;<br>
+ for (unsigned i = 0; i != PtrPHI->getNumIncomingValues()<wbr>; ++i) {<br>
+ if (AvailablePtrVals[i] != PtrPHI->getIncomingValue(i)) {<br>
+ MatchingPtrPHI = nullptr;<br>
+ break;<br>
+ }<br>
+ }<br>
+<br>
+ if (MatchingPtrPHI)<br>
+ break;<br>
+ }<br>
+<br>
+ if (MatchingPtrPHI) {<br>
+ assert(MatchingPtrPHI-><wbr>getType() == IntToPtr->getType() &&<br>
+ "Phi's Type does not match with IntToPtr");<br>
+ // The PtrToCast + IntToPtr will be simplified later<br>
+ return CastInst::<wbr>CreateBitOrPointerCast(<wbr>MatchingPtrPHI,<br>
+ IntToPtr->getOperand(0)-><wbr>getType());<br>
+ }<br>
+<br>
+ // If it requires a conversion for every PHI operand, do not do it.<br>
+ if (std::all_of(AvailablePtrVals.<wbr>begin(), AvailablePtrVals.end(),<br>
+ [&](Value *V) {<br>
+ return (V->getType() != IntToPtr->getType()) ||<br>
+ isa<IntToPtrInst>(V);<br>
+ }))<br>
+ return nullptr;<br>
+<br>
+ // If any of the operand that requires casting is a terminator<br>
+ // instruction, do not do it.<br>
+ if (std::any_of(AvailablePtrVals.<wbr>begin(), AvailablePtrVals.end(),<br>
+ [&](Value *V) {<br>
+ return (V->getType() != IntToPtr->getType()) &&<br>
+ isa<TerminatorInst>(V);<br>
+ }))<br>
+ return nullptr;<br>
+<br>
+ PHINode *NewPtrPHI = PHINode::Create(<br>
+ IntToPtr->getType(), PN.getNumIncomingValues(), PN.getName() + ".ptr");<br>
+<br>
+ InsertNewInstBefore(NewPtrPHI, PN);<br>
+ for (unsigned i = 0; i != PN.getNumIncomingValues(); ++i) {<br>
+ auto *IncomingBB = PN.getIncomingBlock(i);<br>
+ auto *IncomingVal = AvailablePtrVals[i];<br>
+<br>
+ if (IncomingVal->getType() == IntToPtr->getType()) {<br>
+ NewPtrPHI->addIncoming(<wbr>IncomingVal, IncomingBB);<br>
+ continue;<br>
+ }<br>
+<br>
+#ifndef NDEBUG<br>
+ LoadInst *LoadI = dyn_cast<LoadInst>(<wbr>IncomingVal);<br>
+ assert((isa<PHINode>(<wbr>IncomingVal) ||<br>
+ IncomingVal->getType()-><wbr>isPointerTy() ||<br>
+ (LoadI && LoadI->hasOneUse())) &&<br>
+ "Can not replace LoadInst with multiple uses");<br>
+#endif<br>
+ // Need to insert a BitCast.<br>
+ // For an integer Load instruction with a single use, the load + IntToPtr<br>
+ // cast will be simplified into a pointer load:<br>
+ // %v = load i64, i64* %a.ip, align 8<br>
+ // %v.cast = inttoptr i64 %v to float **<br>
+ // ==><br>
+ // %v.ptrp = bitcast i64 * %a.ip to float **<br>
+ // %v.cast = load float *, float ** %v.ptrp, align 8<br>
+ auto *CI = CastInst::<wbr>CreateBitOrPointerCast(<br>
+ IncomingVal, IntToPtr->getType(), IncomingVal->getName() + ".ptr");<br>
+ if (auto *IncomingI = dyn_cast<Instruction>(<wbr>IncomingVal)) {<br>
+ BasicBlock::iterator InsertPos(IncomingI);<br>
+ InsertPos++;<br>
+ if (isa<PHINode>(IncomingI))<br>
+ InsertPos = IncomingI->getParent()-><wbr>getFirstInsertionPt();<br>
+ InsertNewInstBefore(CI, *InsertPos);<br>
+ } else {<br>
+ auto *InsertBB = &IncomingBB->getParent()-><wbr>getEntryBlock();<br>
+ InsertNewInstBefore(CI, *InsertBB-><wbr>getFirstInsertionPt());<br>
+ }<br>
+ NewPtrPHI->addIncoming(CI, IncomingBB);<br>
+ }<br>
+<br>
+ // The PtrToCast + IntToPtr will be simplified later<br>
+ return CastInst::<wbr>CreateBitOrPointerCast(<wbr>NewPtrPHI,<br>
+ IntToPtr->getOperand(0)-><wbr>getType());<br>
+}<br>
+<br>
/// If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the<br>
/// adds all have a single use, turn this into a phi and a single binop.<br>
Instruction *InstCombiner::<wbr>FoldPHIArgBinOpIntoPHI(PHINode &PN) {<br>
@@ -903,6 +1130,9 @@ Instruction *InstCombiner::visitPHINode(<br>
// this PHI only has a single use (a PHI), and if that PHI only has one use (a<br>
// PHI)... break the cycle.<br>
if (PN.hasOneUse()) {<br>
+ if (Instruction *Result = FoldIntegerTypedPHI(PN))<br>
+ return Result;<br>
+<br>
Instruction *PHIUser = cast<Instruction>(PN.user_<wbr>back());<br>
if (PHINode *PU = dyn_cast<PHINode>(PHIUser)) {<br>
SmallPtrSet<PHINode*, 16> PotentiallyDeadPHIs;<br>
<br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div>