[Transforms] Use make_early_inc_range (NFC)

This commit is contained in:
Kazu Hirata 2021-11-07 17:03:15 -08:00
parent 55e4cd8485
commit 0d182d9d1e
11 changed files with 44 additions and 84 deletions

View File

@ -2245,12 +2245,7 @@ static Value *emitSetAndGetSwiftErrorValueAround(Instruction *Call,
/// intrinsics and attempting to MemToReg the alloca away.
static void eliminateSwiftErrorAlloca(Function &F, AllocaInst *Alloca,
coro::Shape &Shape) {
for (auto UI = Alloca->use_begin(), UE = Alloca->use_end(); UI != UE; ) {
// We're likely changing the use list, so use a mutation-safe
// iteration pattern.
auto &Use = *UI;
++UI;
for (Use &Use : llvm::make_early_inc_range(Alloca->uses())) {
// swifterror values can only be used in very specific ways.
// We take advantage of that here.
auto User = Use.getUser();

View File

@ -2016,10 +2016,9 @@ static void replacePrepare(CallInst *Prepare, CallGraph &CG) {
// %2 = bitcast %1 to [[TYPE]]
// ==>
// %2 = @some_function
for (auto UI = Prepare->use_begin(), UE = Prepare->use_end();
UI != UE; ) {
for (Use &U : llvm::make_early_inc_range(Prepare->uses())) {
// Look for bitcasts back to the original function type.
auto *Cast = dyn_cast<BitCastInst>((UI++)->getUser());
auto *Cast = dyn_cast<BitCastInst>(U.getUser());
if (!Cast || Cast->getType() != Fn->getType()) continue;
// Check whether the replacement will introduce new direct calls.

View File

@ -121,32 +121,27 @@ namespace {
}
// Visit the Aliases.
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E;) {
Module::alias_iterator CurI = I;
++I;
bool Delete = deleteStuff == (bool)Named.count(&*CurI);
makeVisible(*CurI, Delete);
for (GlobalAlias &GA : llvm::make_early_inc_range(M.aliases())) {
bool Delete = deleteStuff == (bool)Named.count(&GA);
makeVisible(GA, Delete);
if (Delete) {
Type *Ty = CurI->getValueType();
Type *Ty = GA.getValueType();
CurI->removeFromParent();
GA.removeFromParent();
llvm::Value *Declaration;
if (FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
Declaration = Function::Create(FTy, GlobalValue::ExternalLinkage,
CurI->getAddressSpace(),
CurI->getName(), &M);
Declaration =
Function::Create(FTy, GlobalValue::ExternalLinkage,
GA.getAddressSpace(), GA.getName(), &M);
} else {
Declaration =
new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
nullptr, CurI->getName());
new GlobalVariable(M, Ty, false, GlobalValue::ExternalLinkage,
nullptr, GA.getName());
}
CurI->replaceAllUsesWith(Declaration);
delete &*CurI;
GA.replaceAllUsesWith(Declaration);
delete &GA;
}
}

View File

@ -2605,12 +2605,11 @@ static bool OptimizeEmptyGlobalCXXDtors(Function *CXAAtExitFn) {
// and remove them.
bool Changed = false;
for (auto I = CXAAtExitFn->user_begin(), E = CXAAtExitFn->user_end();
I != E;) {
for (User *U : llvm::make_early_inc_range(CXAAtExitFn->users())) {
// We're only interested in calls. Theoretically, we could handle invoke
// instructions as well, but neither llvm-gcc nor clang generate invokes
// to __cxa_atexit.
CallInst *CI = dyn_cast<CallInst>(*I++);
CallInst *CI = dyn_cast<CallInst>(U);
if (!CI)
continue;

View File

@ -1786,10 +1786,8 @@ void DevirtModule::scanTypeTestUsers(
// points to a member of the type identifier %md. Group calls by (type ID,
// offset) pair (effectively the identity of the virtual function) and store
// to CallSlots.
for (auto I = TypeTestFunc->use_begin(), E = TypeTestFunc->use_end();
I != E;) {
auto CI = dyn_cast<CallInst>(I->getUser());
++I;
for (Use &U : llvm::make_early_inc_range(TypeTestFunc->uses())) {
auto *CI = dyn_cast<CallInst>(U.getUser());
if (!CI)
continue;
@ -1858,11 +1856,8 @@ void DevirtModule::scanTypeTestUsers(
void DevirtModule::scanTypeCheckedLoadUsers(Function *TypeCheckedLoadFunc) {
Function *TypeTestFunc = Intrinsic::getDeclaration(&M, Intrinsic::type_test);
for (auto I = TypeCheckedLoadFunc->use_begin(),
E = TypeCheckedLoadFunc->use_end();
I != E;) {
auto CI = dyn_cast<CallInst>(I->getUser());
++I;
for (Use &U : llvm::make_early_inc_range(TypeCheckedLoadFunc->uses())) {
auto *CI = dyn_cast<CallInst>(U.getUser());
if (!CI)
continue;

View File

@ -1363,28 +1363,25 @@ bool DataFlowSanitizer::runImpl(Module &M) {
// Give function aliases prefixes when necessary, and build wrappers where the
// instrumentedness is inconsistent.
for (Module::alias_iterator AI = M.alias_begin(), AE = M.alias_end();
AI != AE;) {
GlobalAlias *GA = &*AI;
++AI;
for (GlobalAlias &GA : llvm::make_early_inc_range(M.aliases())) {
// Don't stop on weak. We assume people aren't playing games with the
// instrumentedness of overridden weak aliases.
auto *F = dyn_cast<Function>(GA->getAliaseeObject());
auto *F = dyn_cast<Function>(GA.getAliaseeObject());
if (!F)
continue;
bool GAInst = isInstrumented(GA), FInst = isInstrumented(F);
bool GAInst = isInstrumented(&GA), FInst = isInstrumented(F);
if (GAInst && FInst) {
addGlobalNameSuffix(GA);
addGlobalNameSuffix(&GA);
} else if (GAInst != FInst) {
// Non-instrumented alias of an instrumented function, or vice versa.
// Replace the alias with a native-ABI wrapper of the aliasee. The pass
// below will take care of instrumenting it.
Function *NewF =
buildWrapperFunction(F, "", GA->getLinkage(), F->getFunctionType());
GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA->getType()));
NewF->takeName(GA);
GA->eraseFromParent();
buildWrapperFunction(F, "", GA.getLinkage(), F->getFunctionType());
GA.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA.getType()));
NewF->takeName(&GA);
GA.eraseFromParent();
FnsToInstrument.push_back(NewF);
}
}

View File

@ -1046,9 +1046,8 @@ static void eraseLifetimeMarkersOnInputs(const SetVector<BasicBlock *> &Blocks,
const SetVector<Value *> &SunkAllocas,
SetVector<Value *> &LifetimesStart) {
for (BasicBlock *BB : Blocks) {
for (auto It = BB->begin(), End = BB->end(); It != End;) {
auto *II = dyn_cast<IntrinsicInst>(&*It);
++It;
for (Instruction &I : llvm::make_early_inc_range(*BB)) {
auto *II = dyn_cast<IntrinsicInst>(&I);
if (!II || !II->isLifetimeStartOrEnd())
continue;
@ -1619,11 +1618,8 @@ CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC,
// Remove @llvm.assume calls that will be moved to the new function from the
// old function's assumption cache.
for (BasicBlock *Block : Blocks) {
for (auto It = Block->begin(), End = Block->end(); It != End;) {
Instruction *I = &*It;
++It;
if (auto *AI = dyn_cast<AssumeInst>(I)) {
for (Instruction &I : llvm::make_early_inc_range(*Block)) {
if (auto *AI = dyn_cast<AssumeInst>(&I)) {
if (AC)
AC->unregisterAssumption(AI);
AI->eraseFromParent();

View File

@ -1674,21 +1674,19 @@ inlineRetainOrClaimRVCalls(CallBase &CB, objcarc::ARCInstKind RVCallKind,
for (auto *RI : Returns) {
Value *RetOpnd = objcarc::GetRCIdentityRoot(RI->getOperand(0));
BasicBlock::reverse_iterator I = ++(RI->getIterator().getReverse());
BasicBlock::reverse_iterator EI = RI->getParent()->rend();
bool InsertRetainCall = IsRetainRV;
IRBuilder<> Builder(RI->getContext());
// Walk backwards through the basic block looking for either a matching
// autoreleaseRV call or an unannotated call.
for (; I != EI;) {
auto CurI = I++;
auto InstRange = llvm::make_range(++(RI->getIterator().getReverse()),
RI->getParent()->rend());
for (Instruction &I : llvm::make_early_inc_range(InstRange)) {
// Ignore casts.
if (isa<CastInst>(*CurI))
if (isa<CastInst>(I))
continue;
if (auto *II = dyn_cast<IntrinsicInst>(&*CurI)) {
if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
if (II->getIntrinsicID() != Intrinsic::objc_autoreleaseReturnValue ||
!II->hasNUses(0) ||
objcarc::GetRCIdentityRoot(II->getOperand(0)) != RetOpnd)
@ -1711,7 +1709,7 @@ inlineRetainOrClaimRVCalls(CallBase &CB, objcarc::ARCInstKind RVCallKind,
break;
}
auto *CI = dyn_cast<CallInst>(&*CurI);
auto *CI = dyn_cast<CallInst>(&I);
if (!CI)
break;

View File

@ -134,15 +134,7 @@ static void RewriteUsesOfClonedInstructions(BasicBlock *OrigHeader,
SSA.AddAvailableValue(OrigPreheader, OrigPreHeaderVal);
// Visit each use of the OrigHeader instruction.
for (Value::use_iterator UI = OrigHeaderVal->use_begin(),
UE = OrigHeaderVal->use_end();
UI != UE;) {
// Grab the use before incrementing the iterator.
Use &U = *UI;
// Increment the iterator before removing the use from the list.
++UI;
for (Use &U : llvm::make_early_inc_range(OrigHeaderVal->uses())) {
// SSAUpdater can't handle a non-PHI use in the same block as an
// earlier def. We can easily handle those cases manually.
Instruction *UserInst = cast<Instruction>(U.getUser());

View File

@ -612,10 +612,7 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
for (auto *Block : L->blocks())
for (Instruction &I : *Block) {
auto *Undef = UndefValue::get(I.getType());
for (Value::use_iterator UI = I.use_begin(), E = I.use_end();
UI != E;) {
Use &U = *UI;
++UI;
for (Use &U : llvm::make_early_inc_range(I.uses())) {
if (auto *Usr = dyn_cast<Instruction>(U.getUser()))
if (L->contains(Usr->getParent()))
continue;

View File

@ -4370,13 +4370,10 @@ void InnerLoopVectorizer::fixReduction(VPReductionPHIRecipe *PhiR,
Value *Trunc = Builder.CreateTrunc(RdxParts[Part], RdxVecTy);
Value *Extnd = RdxDesc.isSigned() ? Builder.CreateSExt(Trunc, VecTy)
: Builder.CreateZExt(Trunc, VecTy);
for (Value::user_iterator UI = RdxParts[Part]->user_begin();
UI != RdxParts[Part]->user_end();)
if (*UI != Trunc) {
(*UI++)->replaceUsesOfWith(RdxParts[Part], Extnd);
for (User *U : llvm::make_early_inc_range(RdxParts[Part]->users()))
if (U != Trunc) {
U->replaceUsesOfWith(RdxParts[Part], Extnd);
RdxParts[Part] = Extnd;
} else {
++UI;
}
}
Builder.SetInsertPoint(&*LoopMiddleBlock->getFirstInsertionPt());