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 @@ -40,9 +40,24 @@ bool isEmpty() const { return Worklist.empty(); } - /// Add - Add the specified instruction to the worklist if it isn't already - /// in it. - void Add(Instruction *I) { + /// Add instruction to the worklist. + /// Instructions will be visited in the order they are added. + /// You likely want to use this method. + void add(Instruction *I) { + if (Deferred.insert(I)) + LLVM_DEBUG(dbgs() << "IC: ADD DEFERRED: " << *I << '\n'); + } + + /// Add value to the worklist if it is an instruction. + /// Instructions will be visited in the order they are added. + void addValue(Value *V) { + if (Instruction *I = dyn_cast(V)) + add(I); + } + + /// Push the instruction onto the worklist stack. + /// Instructions that have been added first will be visited last. + void push(Instruction *I) { assert(I); assert(I->getParent() && "Instruction not inserted yet?"); @@ -52,26 +67,21 @@ } } - void AddValue(Value *V) { + void pushValue(Value *V) { if (Instruction *I = dyn_cast(V)) - Add(I); - } - - void AddDeferred(Instruction *I) { - if (Deferred.insert(I)) - LLVM_DEBUG(dbgs() << "IC: ADD DEFERRED: " << *I << '\n'); + push(I); } - void AddDeferredInstructions() { + void addDeferredInstructions() { for (Instruction *I : reverse(Deferred)) - Add(I); + push(I); Deferred.clear(); } /// AddInitialGroup - Add the specified batch of stuff in reverse order. /// which should only be done when the worklist is empty and when the group /// has no duplicates. - void AddInitialGroup(ArrayRef List) { + void addInitialGroup(ArrayRef List) { assert(Worklist.empty() && "Worklist must be empty to add initial group"); Worklist.reserve(List.size()+16); WorklistMap.reserve(List.size()); @@ -84,8 +94,8 @@ } } - // Remove - remove I from the worklist if it exists. - void Remove(Instruction *I) { + /// Remove I from the worklist if it exists. + void remove(Instruction *I) { DenseMap::iterator It = WorklistMap.find(I); if (It == WorklistMap.end()) return; // Not in worklist. @@ -96,25 +106,22 @@ Deferred.remove(I); } - Instruction *RemoveOne() { + Instruction *removeOne() { Instruction *I = Worklist.pop_back_val(); WorklistMap.erase(I); return I; } - /// AddUsersToWorkList - When an instruction is simplified, add all users of - /// the instruction to the work lists because they might get more simplified - /// now. - /// - void AddUsersToWorkList(Instruction &I) { + /// When an instruction is simplified, add all users of the instruction + /// to the work lists because they might get more simplified now. + void pushUsersToWorkList(Instruction &I) { for (User *U : I.users()) - Add(cast(U)); + push(cast(U)); } - /// Zap - check that the worklist is empty and nuke the backing store for - /// the map if it is large. - void Zap() { + /// Check that the worklist is empty and nuke the backing store for the map. + void zap() { assert(WorklistMap.empty() && "Worklist empty, but map not?"); assert(Deferred.empty() && "Deferred instructions left over"); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp @@ -2015,7 +2015,7 @@ LastInst->removeFromParent(); for (auto *Inst : Insts) - Worklist.Add(Inst); + Worklist.push(Inst); return LastInst; } @@ -3109,7 +3109,7 @@ if (match(Op0, m_Or(m_Value(X), m_APInt(C))) && MaskedValueIsZero(X, *C, 0, &I)) { Constant *NewC = ConstantInt::get(I.getType(), *C ^ *RHSC); - Worklist.Add(cast(Op0)); + Worklist.push(cast(Op0)); I.setOperand(0, X); I.setOperand(1, NewC); return &I; diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -4818,7 +4818,7 @@ // Otherwise, it's a call, just insert cast right after the call. InsertNewInstBefore(NC, *Caller); } - Worklist.AddUsersToWorkList(*Caller); + Worklist.pushUsersToWorkList(*Caller); } else { NV = UndefValue::get(Caller->getType()); } 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 @@ -1821,7 +1821,7 @@ // Changing the cast operand is usually not a good idea but it is safe // here because the pointer operand is being replaced with another // pointer operand so the opcode doesn't need to change. - Worklist.Add(GEP); + Worklist.push(GEP); CI.setOperand(0, GEP->getOperand(0)); return &CI; } @@ -2363,7 +2363,7 @@ auto *NewBC = cast(Builder.CreateBitCast(NewPN, SrcTy)); SI->setOperand(0, NewBC); - Worklist.Add(SI); + Worklist.push(SI); assert(hasStoreUsersOnly(*NewBC)); } else if (auto *BCI = dyn_cast(V)) { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -1577,7 +1577,7 @@ // the operation, just stop using the Xor. if (!XorC->isNegative()) { Cmp.setOperand(0, X); - Worklist.Add(Xor); + Worklist.push(Xor); return &Cmp; } @@ -1687,7 +1687,7 @@ APInt NewAndCst = IsShl ? C2.lshr(*C3) : C2.shl(*C3); And->setOperand(1, ConstantInt::get(And->getType(), NewAndCst)); And->setOperand(0, Shift->getOperand(0)); - Worklist.Add(Shift); // Shift is dead. + Worklist.push(Shift); // Shift is dead. return &Cmp; } } @@ -4658,7 +4658,7 @@ Function *F = Intrinsic::getDeclaration( I.getModule(), Intrinsic::umul_with_overflow, MulType); CallInst *Call = Builder.CreateCall(F, {MulA, MulB}, "umul"); - IC.Worklist.Add(MulInstr); + IC.Worklist.push(MulInstr); // If there are uses of mul result other than the comparison, we know that // they are truncation or binary AND. Change them to use result of @@ -4685,11 +4685,11 @@ } else { llvm_unreachable("Unexpected Binary operation"); } - IC.Worklist.Add(cast(U)); + IC.Worklist.push(cast(U)); } } if (isa(OtherVal)) - IC.Worklist.Add(cast(OtherVal)); + IC.Worklist.push(cast(OtherVal)); // The original icmp gets replaced with the overflow value, maybe inverted // depending on predicate. diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -648,7 +648,7 @@ "New instruction already inserted into a basic block!"); BasicBlock *BB = Old.getParent(); BB->getInstList().insert(Old.getIterator(), New); // Insert inst - Worklist.Add(New); + Worklist.push(New); return New; } @@ -669,7 +669,7 @@ // no changes were made to the program. if (I.use_empty()) return nullptr; - Worklist.AddUsersToWorkList(I); // Add all modified instrs to worklist. + Worklist.pushUsersToWorkList(I); // Add all modified instrs to worklist. // If we are replacing the instruction with itself, this must be in a // segment of unreachable code, so just clobber the instruction. @@ -718,9 +718,9 @@ if (I.getNumOperands() < 8) { for (Use &Operand : I.operands()) if (auto *Inst = dyn_cast(Operand)) - Worklist.Add(Inst); + Worklist.push(Inst); } - Worklist.Remove(&I); + Worklist.remove(&I); I.eraseFromParent(); MadeIRChange = true; return nullptr; // Don't do anything with FI diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -973,7 +973,7 @@ // Replace GEP indices if possible. if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Op, LI)) { - Worklist.Add(NewGEPI); + Worklist.push(NewGEPI); return &LI; } @@ -1384,7 +1384,7 @@ // Replace GEP indices if possible. if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) { - Worklist.Add(NewGEPI); + Worklist.push(NewGEPI); return &SI; } @@ -1434,7 +1434,7 @@ // Manually add back the original store to the worklist now, so it will // be processed after the operands of the removed store, as this may // expose additional DSE opportunities. - Worklist.Add(&SI); + Worklist.push(&SI); eraseInstFromFunction(*PrevSI); return nullptr; } @@ -1466,7 +1466,7 @@ if (!isa(Val)) { SI.setOperand(0, UndefValue::get(Val->getType())); if (Instruction *U = dyn_cast(Val)) - Worklist.Add(U); // Dropped a use. + Worklist.push(U); // Dropped a use. } return nullptr; // Do not modify these! } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp @@ -621,11 +621,11 @@ I != E; ++I) { if (*I == SI) { *I = SI->getOperand(NonNullOperand); - Worklist.Add(&*BBI); + Worklist.push(&*BBI); } else if (*I == SelectCond) { *I = NonNullOperand == 1 ? ConstantInt::getTrue(CondTy) : ConstantInt::getFalse(CondTy); - Worklist.Add(&*BBI); + Worklist.push(&*BBI); } } @@ -1418,7 +1418,7 @@ const APInt *Y; // X % -Y -> X % Y if (match(Op1, m_Negative(Y)) && !Y->isMinSignedValue()) { - Worklist.AddValue(I.getOperand(1)); + Worklist.pushValue(I.getOperand(1)); I.setOperand(1, ConstantInt::get(I.getType(), -*Y)); return &I; } @@ -1469,7 +1469,7 @@ Constant *NewRHSV = ConstantVector::get(Elts); if (NewRHSV != C) { // Don't loop on -MININT - Worklist.AddValue(I.getOperand(1)); + Worklist.pushValue(I.getOperand(1)); I.setOperand(1, NewRHSV); return &I; } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -2404,7 +2404,7 @@ SI.setOperand(1, FalseVal); SI.setOperand(2, TrueVal); SI.swapProfMetadata(); - Worklist.Add(Cond); + Worklist.push(Cond); return &SI; } @@ -2747,14 +2747,14 @@ if (auto *TrueBOSI = dyn_cast(TrueBO->getOperand(0))) { if (TrueBOSI->getCondition() == CondVal) { TrueBO->setOperand(0, TrueBOSI->getTrueValue()); - Worklist.Add(TrueBO); + Worklist.push(TrueBO); return &SI; } } if (auto *TrueBOSI = dyn_cast(TrueBO->getOperand(1))) { if (TrueBOSI->getCondition() == CondVal) { TrueBO->setOperand(1, TrueBOSI->getTrueValue()); - Worklist.Add(TrueBO); + Worklist.push(TrueBO); return &SI; } } @@ -2767,14 +2767,14 @@ if (auto *FalseBOSI = dyn_cast(FalseBO->getOperand(0))) { if (FalseBOSI->getCondition() == CondVal) { FalseBO->setOperand(0, FalseBOSI->getFalseValue()); - Worklist.Add(FalseBO); + Worklist.push(FalseBO); return &SI; } } if (auto *FalseBOSI = dyn_cast(FalseBO->getOperand(1))) { if (FalseBOSI->getCondition() == CondVal) { FalseBO->setOperand(1, FalseBOSI->getFalseValue()); - Worklist.Add(FalseBO); + Worklist.push(FalseBO); return &SI; } } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp @@ -605,7 +605,7 @@ } Instruction *I = cast(V); - IC.Worklist.Add(I); + IC.Worklist.push(I); switch (I->getOpcode()) { default: llvm_unreachable("Inconsistency with CanEvaluateShifted"); diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -1309,7 +1309,7 @@ // If this is inserting an element that isn't demanded, remove this // insertelement. if (IdxNo >= VWidth || !DemandedElts[IdxNo]) { - Worklist.Add(I); + Worklist.push(I); return I->getOperand(0); } @@ -1590,7 +1590,7 @@ // use Arg0 if DemandedElts[0] is clear like we do for other intrinsics. // Instead we should return a zero vector. if (!DemandedElts[0]) { - Worklist.Add(II); + Worklist.push(II); return ConstantAggregateZero::get(II->getType()); } @@ -1609,7 +1609,7 @@ // If lowest element of a scalar op isn't used then use Arg0. if (!DemandedElts[0]) { - Worklist.Add(II); + Worklist.push(II); return II->getArgOperand(0); } // TODO: If only low elt lower SQRT to FSQRT (with rounding/exceptions @@ -1629,7 +1629,7 @@ // If lowest element of a scalar op isn't used then use Arg0. if (!DemandedElts[0]) { - Worklist.Add(II); + Worklist.push(II); return II->getArgOperand(0); } @@ -1656,7 +1656,7 @@ // If lowest element of a scalar op isn't used then use Arg0. if (!DemandedElts[0]) { - Worklist.Add(II); + Worklist.push(II); return II->getArgOperand(0); } @@ -1690,7 +1690,7 @@ // If lowest element of a scalar op isn't used then use Arg0. if (!DemandedElts[0]) { - Worklist.Add(II); + Worklist.push(II); return II->getArgOperand(0); } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -400,7 +400,7 @@ // If the inserted and extracted elements are constants, they must not // be the same value, extract from the pre-inserted value instead. if (isa(IE->getOperand(2)) && IndexC) { - Worklist.AddValue(SrcVec); + Worklist.pushValue(SrcVec); EI.setOperand(0, IE->getOperand(0)); return &EI; } 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 @@ -1486,7 +1486,7 @@ assert(Op != Parent.first->getOperand(Parent.second) && "Descaling was a no-op?"); Parent.first->setOperand(Parent.second, Op); - Worklist.Add(Parent.first); + Worklist.push(Parent.first); // Now work back up the expression correcting nsw flags. The logic is based // on the following observation: if X * Y is known not to overflow as a signed @@ -1504,7 +1504,7 @@ NoSignedWrap &= OpNoSignedWrap; if (NoSignedWrap != OpNoSignedWrap) { BO->setHasNoSignedWrap(NoSignedWrap); - Worklist.Add(Ancestor); + Worklist.push(Ancestor); } } else if (Ancestor->getOpcode() == Instruction::Trunc) { // The fact that the descaled input to the trunc has smaller absolute @@ -2741,7 +2741,7 @@ // determine the value. If so, constant fold it. KnownBits Known = computeKnownBits(ResultOp, 0, &RI); if (Known.isConstant()) { - Worklist.AddValue(ResultOp); + Worklist.pushValue(ResultOp); RI.setOperand(0, Constant::getIntegerValue(VTy, Known.getConstant())); return &RI; } @@ -2777,7 +2777,7 @@ CmpInst *Cond = cast(BI.getCondition()); Cond->setPredicate(CmpInst::getInversePredicate(Pred)); BI.swapSuccessors(); - Worklist.Add(Cond); + Worklist.push(Cond); return &BI; } @@ -3409,7 +3409,7 @@ bool InstCombiner::run() { while (!Worklist.isEmpty()) { - Instruction *I = Worklist.RemoveOne(); + Instruction *I = Worklist.removeOne(); if (I == nullptr) continue; // skip null values. // Check to see if we can DCE the instruction. @@ -3495,7 +3495,7 @@ // worklist for (Use &U : I->operands()) if (Instruction *OpI = dyn_cast(U.get())) - Worklist.Add(OpI); + Worklist.push(OpI); } } } @@ -3538,8 +3538,8 @@ InstParent->getInstList().insert(InsertPos, Result); // Push the new instruction and any users onto the worklist. - Worklist.AddUsersToWorkList(*Result); - Worklist.Add(Result); + Worklist.pushUsersToWorkList(*Result); + Worklist.push(Result); eraseInstFromFunction(*I); } else { @@ -3551,16 +3551,16 @@ if (isInstructionTriviallyDead(I, &TLI)) { eraseInstFromFunction(*I); } else { - Worklist.AddUsersToWorkList(*I); - Worklist.Add(I); + Worklist.pushUsersToWorkList(*I); + Worklist.push(I); } } MadeIRChange = true; } - Worklist.AddDeferredInstructions(); + Worklist.addDeferredInstructions(); } - Worklist.Zap(); + Worklist.zap(); return MadeIRChange; } @@ -3684,7 +3684,7 @@ // of the function down. This jives well with the way that it adds all uses // of instructions to the worklist after doing a transformation, thus avoiding // some N^2 behavior in pathological cases. - ICWorklist.AddInitialGroup(InstrsForInstCombineWorklist); + ICWorklist.addInitialGroup(InstrsForInstCombineWorklist); return MadeIRChange; } @@ -3737,7 +3737,7 @@ IRBuilder Builder( F.getContext(), TargetFolder(DL), IRBuilderCallbackInserter([&Worklist, &AC](Instruction *I) { - Worklist.AddDeferred(I); + Worklist.add(I); if (match(I, m_Intrinsic())) AC.registerAssumption(cast(I)); }));