Don't create unnecessary PHIs
[oota-llvm.git] / lib / Transforms / Utils / SimplifyCFG.cpp
index cbb8cf234aa7514c6e05d55b9724c1ea41d2dd7a..78088e72009f450dd725a83b0aca131edc20c7f6 100644 (file)
@@ -2385,6 +2385,11 @@ static Value *ensureValueAvailableInSuccessor(Value *V, BasicBlock *BB,
   // If AlternativeV is not nullptr, we care about both incoming values in PHI.
   // PHI must be exactly: phi <ty> [ %BB, %V ], [ %OtherBB, %AlternativeV]
   // where OtherBB is the single other predecessor of BB's only successor.
+
+  // If V is not an instruction defined in BB, just return it.
+  if (!isa<Instruction>(V) || cast<Instruction>(V)->getParent() != BB)
+    return V;
+
   PHINode *PHI = nullptr;
   BasicBlock *Succ = BB->getSingleSuccessor();