Index: lib/CodeGen/CodeGenPrepare.cpp =================================================================== --- lib/CodeGen/CodeGenPrepare.cpp +++ lib/CodeGen/CodeGenPrepare.cpp @@ -668,6 +668,20 @@ return MadeChange; } +/// Check to see if all of the operands of the PHI are not simple constants +/// (constantint/constantfp/undef). +static bool HasAllNonCstPhiOps(BasicBlock *DestBB) { + BasicBlock::const_iterator DestBBI = DestBB->begin(); + while (const PHINode *DestPN = dyn_cast(DestBBI++)) { + for (unsigned I = 0, E = DestPN->getNumIncomingValues(); I != E; ++I) { + if (isa(DestPN->getIncomingValue(I)) && + !isa(DestPN->getIncomingValue(I))) + return false; + } + } + return true; +} + bool CodeGenPrepare::isMergingEmptyBlockProfitable(BasicBlock *BB, BasicBlock *DestBB, bool isPreheader) { @@ -740,6 +754,12 @@ if (SameIncomingValueBBs.count(Pred)) return true; + // Check to see if phi operands are of constants, in such case extra COPYs + // are likely needed. If all the operands of the phis are not constant there + // is no reason to skip merging. + if (HasAllNonCstPhiOps(DestBB)) + return true; + if (!BFI) { Function &F = *BB->getParent(); LoopInfo LI{DominatorTree(F)};