Index: llvm/lib/Transforms/Utils/Local.cpp =================================================================== --- llvm/lib/Transforms/Utils/Local.cpp +++ llvm/lib/Transforms/Utils/Local.cpp @@ -3053,31 +3053,26 @@ if (match(Condition, m_Not(m_Value(NotCondition)))) return NotCondition; - if (Instruction *Inst = dyn_cast(Condition)) { - // Third: Check all the users for an invert - BasicBlock *Parent = Inst->getParent(); - for (User *U : Condition->users()) - if (Instruction *I = dyn_cast(U)) - if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) - return I; - - // Last option: Create a new instruction - auto Inverted = BinaryOperator::CreateNot(Inst, ""); - if (isa(Inst)) { - // FIXME: This fails if the inversion is to be used in a - // subsequent PHINode in the same basic block. - Inverted->insertBefore(&*Parent->getFirstInsertionPt()); - } else { - Inverted->insertAfter(Inst); - } - return Inverted; - } - - if (Argument *Arg = dyn_cast(Condition)) { - BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock(); - return BinaryOperator::CreateNot(Condition, Arg->getName() + ".inv", - &*EntryBlock.getFirstInsertionPt()); - } - - llvm_unreachable("Unhandled condition to invert"); + BasicBlock *Parent = nullptr; + Instruction *Inst = dyn_cast(Condition); + if (Inst) + Parent = Inst->getParent(); + else if (Argument *Arg = dyn_cast(Condition)) + Parent = &Arg->getParent()->getEntryBlock(); + assert(Parent && "Unsupported condition to invert"); + + // Third: Check all the users for an invert + for (User *U : Condition->users()) + if (Instruction *I = dyn_cast(U)) + if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition)))) + return I; + + // Last option: Create a new instruction + auto *Inverted = + BinaryOperator::CreateNot(Condition, Condition->getName() + ".inv"); + if (Inst && !isa(Inst)) + Inverted->insertAfter(Inst); + else + Inverted->insertBefore(&*Parent->getFirstInsertionPt()); + return Inverted; }