diff --git a/llvm/include/llvm/Transforms/InstCombine/InstCombineWorklist.h b/llvm/include/llvm/Transforms/InstCombine/InstCombineWorklist.h --- a/llvm/include/llvm/Transforms/InstCombine/InstCombineWorklist.h +++ b/llvm/include/llvm/Transforms/InstCombine/InstCombineWorklist.h @@ -24,8 +24,8 @@ /// InstCombineWorklist - This is the worklist management logic for /// InstCombine. class InstCombineWorklist { - SmallVector Worklist; - DenseMap WorklistMap; + SmallVector Worklist; + DenseMap WorklistMap; public: InstCombineWorklist() = default; @@ -38,6 +38,9 @@ /// Add - Add the specified instruction to the worklist if it isn't already /// in it. void Add(Instruction *I) { + assert(I); + assert(I->getParent() && "Instruction not inserted yet?"); + if (WorklistMap.insert(std::make_pair(I, Worklist.size())).second) { LLVM_DEBUG(dbgs() << "IC: ADD: " << *I << '\n'); Worklist.push_back(I); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp @@ -1189,7 +1189,9 @@ // zext (or icmp, icmp) -> or (zext icmp), (zext icmp) Value *LCast = Builder.CreateZExt(LHS, CI.getType(), LHS->getName()); Value *RCast = Builder.CreateZExt(RHS, CI.getType(), RHS->getName()); - BinaryOperator *Or = BinaryOperator::Create(Instruction::Or, LCast, RCast); + Value *Or = Builder.CreateOr(LCast, RCast, CI.getName()); + if (auto *OrInst = dyn_cast(Or)) + Builder.SetInsertPoint(OrInst); // Perform the elimination. if (auto *LZExt = dyn_cast(LCast)) @@ -1197,7 +1199,7 @@ if (auto *RZExt = dyn_cast(RCast)) transformZExtICmp(RHS, *RZExt); - return Or; + return replaceInstUsesWith(CI, Or); } } diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -3361,10 +3361,6 @@ // Move the name to the new instruction first. Result->takeName(I); - // Push the new instruction and any users onto the worklist. - Worklist.AddUsersToWorkList(*Result); - Worklist.Add(Result); - // Insert the new instruction into the basic block... BasicBlock *InstParent = I->getParent(); BasicBlock::iterator InsertPos = I->getIterator(); @@ -3376,6 +3372,10 @@ InstParent->getInstList().insert(InsertPos, Result); + // Push the new instruction and any users onto the worklist. + Worklist.AddUsersToWorkList(*Result); + Worklist.Add(Result); + eraseInstFromFunction(*I); } else { LLVM_DEBUG(dbgs() << "IC: Mod = " << OrigI << '\n' diff --git a/llvm/test/Transforms/InstCombine/zext-or-icmp.ll b/llvm/test/Transforms/InstCombine/zext-or-icmp.ll --- a/llvm/test/Transforms/InstCombine/zext-or-icmp.ll +++ b/llvm/test/Transforms/InstCombine/zext-or-icmp.ll @@ -15,7 +15,7 @@ ; CHECK-NEXT: %toBool2 = icmp eq i8 %b, 0 ; CHECK-NEXT: %toBool22 = zext i1 %toBool2 to i8 ; CHECK-NEXT: %1 = xor i8 %mask, 1 -; CHECK-NEXT: %zext = or i8 %1, %toBool22 +; CHECK-NEXT: %zext3 = or i8 %1, %toBool22 ; CHECK-NEXT: ret i8 %zext }