aboutsummaryrefslogtreecommitdiff
path: root/contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp')
-rw-r--r--contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp91
1 files changed, 44 insertions, 47 deletions
diff --git a/contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp b/contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp
index bbba76a5c5ef..7df8651ede15 100644
--- a/contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp
+++ b/contrib/llvm-project/llvm/lib/Transforms/Utils/LoopUtils.cpp
@@ -12,7 +12,6 @@
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/ADT/DenseSet.h"
-#include "llvm/ADT/Optional.h"
#include "llvm/ADT/PriorityWorklist.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SetVector.h"
@@ -38,6 +37,7 @@
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PatternMatch.h"
+#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
@@ -246,27 +246,27 @@ void llvm::addStringMetadataToLoop(Loop *TheLoop, const char *StringMD,
TheLoop->setLoopID(NewLoopID);
}
-Optional<ElementCount>
+std::optional<ElementCount>
llvm::getOptionalElementCountLoopAttribute(const Loop *TheLoop) {
- Optional<int> Width =
+ std::optional<int> Width =
getOptionalIntLoopAttribute(TheLoop, "llvm.loop.vectorize.width");
if (Width) {
- Optional<int> IsScalable = getOptionalIntLoopAttribute(
+ std::optional<int> IsScalable = getOptionalIntLoopAttribute(
TheLoop, "llvm.loop.vectorize.scalable.enable");
return ElementCount::get(*Width, IsScalable.value_or(false));
}
- return None;
+ return std::nullopt;
}
-Optional<MDNode *> llvm::makeFollowupLoopID(
+std::optional<MDNode *> llvm::makeFollowupLoopID(
MDNode *OrigLoopID, ArrayRef<StringRef> FollowupOptions,
const char *InheritOptionsExceptPrefix, bool AlwaysNew) {
if (!OrigLoopID) {
if (AlwaysNew)
return nullptr;
- return None;
+ return std::nullopt;
}
assert(OrigLoopID->getOperand(0) == OrigLoopID);
@@ -325,7 +325,7 @@ Optional<MDNode *> llvm::makeFollowupLoopID(
// Attributes of the followup loop not specified explicity, so signal to the
// transformation pass to add suitable attributes.
if (!AlwaysNew && !HasAnyFollowup)
- return None;
+ return std::nullopt;
// If no attributes were added or remove, the previous loop Id can be reused.
if (!AlwaysNew && !Changed)
@@ -353,10 +353,10 @@ TransformationMode llvm::hasUnrollTransformation(const Loop *L) {
if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
return TM_SuppressedByUser;
- Optional<int> Count =
+ std::optional<int> Count =
getOptionalIntLoopAttribute(L, "llvm.loop.unroll.count");
if (Count)
- return Count.value() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
+ return *Count == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
if (getBooleanLoopAttribute(L, "llvm.loop.unroll.enable"))
return TM_ForcedByUser;
@@ -374,10 +374,10 @@ TransformationMode llvm::hasUnrollAndJamTransformation(const Loop *L) {
if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.disable"))
return TM_SuppressedByUser;
- Optional<int> Count =
+ std::optional<int> Count =
getOptionalIntLoopAttribute(L, "llvm.loop.unroll_and_jam.count");
if (Count)
- return Count.value() == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
+ return *Count == 1 ? TM_SuppressedByUser : TM_ForcedByUser;
if (getBooleanLoopAttribute(L, "llvm.loop.unroll_and_jam.enable"))
return TM_ForcedByUser;
@@ -389,15 +389,15 @@ TransformationMode llvm::hasUnrollAndJamTransformation(const Loop *L) {
}
TransformationMode llvm::hasVectorizeTransformation(const Loop *L) {
- Optional<bool> Enable =
+ std::optional<bool> Enable =
getOptionalBoolLoopAttribute(L, "llvm.loop.vectorize.enable");
if (Enable == false)
return TM_SuppressedByUser;
- Optional<ElementCount> VectorizeWidth =
+ std::optional<ElementCount> VectorizeWidth =
getOptionalElementCountLoopAttribute(L);
- Optional<int> InterleaveCount =
+ std::optional<int> InterleaveCount =
getOptionalIntLoopAttribute(L, "llvm.loop.interleave.count");
// 'Forcing' vector width and interleave count to one effectively disables
@@ -485,8 +485,10 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
// Tell ScalarEvolution that the loop is deleted. Do this before
// deleting the loop so that ScalarEvolution can look at the loop
// to determine what it needs to clean up.
- if (SE)
+ if (SE) {
SE->forgetLoop(L);
+ SE->forgetBlockAndLoopDispositions();
+ }
Instruction *OldTerm = Preheader->getTerminator();
assert(!OldTerm->mayHaveSideEffects() &&
@@ -591,7 +593,7 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
}
// Use a map to unique and a vector to guarantee deterministic ordering.
- llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet;
+ llvm::SmallDenseSet<DebugVariable, 4> DeadDebugSet;
llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;
if (ExitBlock) {
@@ -620,11 +622,8 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I);
if (!DVI)
continue;
- auto Key =
- DeadDebugSet.find({DVI->getVariable(), DVI->getExpression()});
- if (Key != DeadDebugSet.end())
+ if (!DeadDebugSet.insert(DebugVariable(DVI)).second)
continue;
- DeadDebugSet.insert({DVI->getVariable(), DVI->getExpression()});
DeadDebugInst.push_back(DVI);
}
@@ -633,15 +632,14 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
// Since debug values in the loop have been deleted, inserting an undef
// dbg.value truncates the range of any dbg.value before the loop where the
// loop used to be. This is particularly important for constant values.
- DIBuilder DIB(*ExitBlock->getModule());
Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI();
assert(InsertDbgValueBefore &&
"There should be a non-PHI instruction in exit block, else these "
"instructions will have no parent.");
- for (auto *DVI : DeadDebugInst)
- DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()),
- DVI->getVariable(), DVI->getExpression(),
- DVI->getDebugLoc(), InsertDbgValueBefore);
+ for (auto *DVI : DeadDebugInst) {
+ DVI->setKillLocation();
+ DVI->moveBefore(InsertDbgValueBefore);
+ }
}
// Remove the block from the reference counting scheme, so that we can
@@ -693,6 +691,7 @@ void llvm::breakLoopBackedge(Loop *L, DominatorTree &DT, ScalarEvolution &SE,
Loop *OutermostLoop = L->getOutermostLoop();
SE.forgetLoop(L);
+ SE.forgetBlockAndLoopDispositions();
std::unique_ptr<MemorySSAUpdater> MSSAU;
if (MSSA)
@@ -782,22 +781,22 @@ static BranchInst *getExpectedExitLoopLatchBranch(Loop *L) {
/// Return the estimated trip count for any exiting branch which dominates
/// the loop latch.
-static Optional<uint64_t>
-getEstimatedTripCount(BranchInst *ExitingBranch, Loop *L,
- uint64_t &OrigExitWeight) {
+static std::optional<uint64_t> getEstimatedTripCount(BranchInst *ExitingBranch,
+ Loop *L,
+ uint64_t &OrigExitWeight) {
// To estimate the number of times the loop body was executed, we want to
// know the number of times the backedge was taken, vs. the number of times
// we exited the loop.
uint64_t LoopWeight, ExitWeight;
- if (!ExitingBranch->extractProfMetadata(LoopWeight, ExitWeight))
- return None;
+ if (!extractBranchWeights(*ExitingBranch, LoopWeight, ExitWeight))
+ return std::nullopt;
if (L->contains(ExitingBranch->getSuccessor(1)))
std::swap(LoopWeight, ExitWeight);
if (!ExitWeight)
// Don't have a way to return predicated infinite
- return None;
+ return std::nullopt;
OrigExitWeight = ExitWeight;
@@ -808,7 +807,7 @@ getEstimatedTripCount(BranchInst *ExitingBranch, Loop *L,
return ExitCount + 1;
}
-Optional<unsigned>
+std::optional<unsigned>
llvm::getLoopEstimatedTripCount(Loop *L,
unsigned *EstimatedLoopInvocationWeight) {
// Currently we take the estimate exit count only from the loop latch,
@@ -817,14 +816,14 @@ llvm::getLoopEstimatedTripCount(Loop *L,
// TODO: incorporate information from other exits
if (BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L)) {
uint64_t ExitWeight;
- if (Optional<uint64_t> EstTripCount =
- getEstimatedTripCount(LatchBranch, L, ExitWeight)) {
+ if (std::optional<uint64_t> EstTripCount =
+ getEstimatedTripCount(LatchBranch, L, ExitWeight)) {
if (EstimatedLoopInvocationWeight)
*EstimatedLoopInvocationWeight = ExitWeight;
return *EstTripCount;
}
}
- return None;
+ return std::nullopt;
}
bool llvm::setLoopEstimatedTripCount(Loop *L, unsigned EstimatedTripCount,
@@ -1165,7 +1164,7 @@ static bool hasHardUserWithinLoop(const Loop *L, const Instruction *I) {
if (Curr->mayHaveSideEffects())
return true;
// Otherwise, add all its users to worklist.
- for (auto U : Curr->users()) {
+ for (const auto *U : Curr->users()) {
auto *UI = cast<Instruction>(U);
if (Visited.insert(UI).second)
WorkList.push_back(UI);
@@ -1477,7 +1476,7 @@ void llvm::setProfileInfoAfterUnrolling(Loop *OrigLoop, Loop *UnrolledLoop,
// Get number of iterations in the original scalar loop.
unsigned OrigLoopInvocationWeight = 0;
- Optional<unsigned> OrigAverageTripCount =
+ std::optional<unsigned> OrigAverageTripCount =
getLoopEstimatedTripCount(OrigLoop, &OrigLoopInvocationWeight);
if (!OrigAverageTripCount)
return;
@@ -1667,8 +1666,7 @@ Value *llvm::addRuntimeChecks(
}
Value *llvm::addDiffRuntimeChecks(
- Instruction *Loc, Loop *TheLoop, ArrayRef<PointerDiffInfo> Checks,
- SCEVExpander &Expander,
+ Instruction *Loc, ArrayRef<PointerDiffInfo> Checks, SCEVExpander &Expander,
function_ref<Value *(IRBuilderBase &, unsigned)> GetVF, unsigned IC) {
LLVMContext &Ctx = Loc->getContext();
@@ -1678,7 +1676,7 @@ Value *llvm::addDiffRuntimeChecks(
// Our instructions might fold to a constant.
Value *MemoryRuntimeCheck = nullptr;
- for (auto &C : Checks) {
+ for (const auto &C : Checks) {
Type *Ty = C.SinkStart->getType();
// Compute VF * IC * AccessSize.
auto *VFTimesUFTimesSize =
@@ -1705,10 +1703,9 @@ Value *llvm::addDiffRuntimeChecks(
return MemoryRuntimeCheck;
}
-Optional<IVConditionInfo> llvm::hasPartialIVCondition(Loop &L,
- unsigned MSSAThreshold,
- MemorySSA &MSSA,
- AAResults &AA) {
+std::optional<IVConditionInfo>
+llvm::hasPartialIVCondition(const Loop &L, unsigned MSSAThreshold,
+ const MemorySSA &MSSA, AAResults &AA) {
auto *TI = dyn_cast<BranchInst>(L.getHeader()->getTerminator());
if (!TI || !TI->isConditional())
return {};
@@ -1765,7 +1762,7 @@ Optional<IVConditionInfo> llvm::hasPartialIVCondition(Loop &L,
[&L, &AA, &AccessedLocs, &ExitingBlocks, &InstToDuplicate,
MSSAThreshold](BasicBlock *Succ, BasicBlock *Header,
SmallVector<MemoryAccess *, 4> AccessesToCheck)
- -> Optional<IVConditionInfo> {
+ -> std::optional<IVConditionInfo> {
IVConditionInfo Info;
// First, collect all blocks in the loop that are on a patch from Succ
// to the header.
@@ -1843,7 +1840,7 @@ Optional<IVConditionInfo> llvm::hasPartialIVCondition(Loop &L,
if (L.contains(Succ))
continue;
- Info.PathIsNoop &= llvm::empty(Succ->phis()) &&
+ Info.PathIsNoop &= Succ->phis().empty() &&
(!Info.ExitForPath || Info.ExitForPath == Succ);
if (!Info.PathIsNoop)
break;