49#define DEBUG_TYPE "canon-freeze"
53class CanonicalizeFreezeInLoops :
public LoopPass {
57 CanonicalizeFreezeInLoops();
64class CanonicalizeFreezeInLoopsImpl {
73 auto Opc =
I->getOpcode();
75 return Opc == Instruction::Add ||
Opc == Instruction::Sub ||
76 Opc == Instruction::Mul;
79 void InsertFreezeAndForgetFromSCEV(
Use &U);
83 : L(L), SE(SE), DT(DT) {}
123 return LHS.FI == RHS.FI;
131void CanonicalizeFreezeInLoopsImpl::InsertFreezeAndForgetFromSCEV(
Use &U) {
135 auto *ValueToFr =
U.get();
137 "Should not process an instruction that isn't inside the loop");
145 U.set(
new FreezeInst(ValueToFr, ValueToFr->getName() +
".frozen",
146 PH->getTerminator()->getIterator()));
151bool CanonicalizeFreezeInLoopsImpl::run() {
156 SmallSetVector<FrozenIndPHIInfo, 4> Candidates;
159 InductionDescriptor
ID;
164 FrozenIndPHIInfo
Info(&
PHI,
ID.getInductionBinOp());
165 if (!
Info.StepInst || !canHandleInst(
Info.StepInst)) {
171 Info.StepValIdx =
Info.StepInst->getOperand(0) == &
PHI;
174 if (L->
contains(StepI->getParent())) {
181 auto Visit = [&](
User *
U) {
192 if (Candidates.
empty())
195 SmallPtrSet<PHINode *, 8> ProcessedPHIs;
196 for (
const auto &
Info : Candidates) {
201 BinaryOperator *StepI =
Info.StepInst;
202 assert(StepI &&
"Step instruction should have been found");
213 unsigned OperandIdx =
214 PHI->getOperandNumForIncomingValue(
PHI->getIncomingValue(0) == StepI);
215 InsertFreezeAndForgetFromSCEV(
PHI->getOperandUse(OperandIdx));
219 for (
const auto &Item : Candidates) {
223 FI->replaceAllUsesWith(FI->getOperand(0));
224 FI->eraseFromParent();
230CanonicalizeFreezeInLoops::CanonicalizeFreezeInLoops() : LoopPass(
ID) {
234void CanonicalizeFreezeInLoops::getAnalysisUsage(
AnalysisUsage &AU)
const {
249 auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
250 auto &DT = getAnalysis<DominatorTreeWrapperPass>().
getDomTree();
251 return CanonicalizeFreezeInLoopsImpl(L, SE, DT).run();
258 if (!CanonicalizeFreezeInLoopsImpl(&L, AR.
SE, AR.
DT).run())
265 "Canonicalize Freeze Instructions in Loops",
false,
false)
273 return new CanonicalizeFreezeInLoops();
276char CanonicalizeFreezeInLoops::ID = 0;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
This file defines DenseMapInfo traits for DenseMap.
This header provides classes for managing per-loop analyses.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file implements a set that has insertion order iteration characteristics.
Represent the analysis usage information of a pass.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
Legacy analysis pass which computes a DominatorTree.
DominatorTree & getDomTree()
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
This class represents a freeze function that returns random concrete value if an operand is either a ...
static LLVM_ABI bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr, SmallVectorImpl< Instruction * > *CastsToIgnore=nullptr)
Returns true if Phi is an induction in the loop L.
LLVM_ABI void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
BlockT * getHeader() const
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
bool isLoopSimplifyForm() const
Return true if the Loop is in the form that the LoopSimplify form transforms loops to,...
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Pass interface - Implemented by all 'passes'.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
The main scalar evolution driver.
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
A Use represents the edge between a Value definition and its users.
const Use & getOperandUse(unsigned i) const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ User
could "use" a pointer
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void initializeCanonicalizeFreezeInLoopsPass(PassRegistry &)
LLVM_ABI char & LoopSimplifyID
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
LLVM_ABI Pass * createCanonicalizeFreezeInLoopsPass()
static FrozenIndPHIInfo getEmptyKey()
static FrozenIndPHIInfo getTombstoneKey()
static unsigned getHashValue(const FrozenIndPHIInfo &Val)
static bool isEqual(const FrozenIndPHIInfo &LHS, const FrozenIndPHIInfo &RHS)
An information struct used to provide DenseMap with the various necessary components for a given valu...
FrozenIndPHIInfo(PHINode *PHI, BinaryOperator *StepInst)
bool operator==(const FrozenIndPHIInfo &Other)
BinaryOperator * StepInst
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...