#ifndef LLVM_ANALYSIS_IVUSERS_H
#define LLVM_ANALYSIS_IVUSERS_H
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionNormalization.h"
#include "llvm/IR/ValueHandle.h"
namespace llvm {
class AssumptionCache;
class DominatorTree;
class ScalarEvolution;
class SCEV;
class IVUsers;
class IVStrideUse final : public CallbackVH, public ilist_node<IVStrideUse> {
friend class IVUsers;
public:
IVStrideUse(IVUsers *P, Instruction* U, Value *O)
: CallbackVH(U), Parent(P), OperandValToReplace(O) {
}
Instruction *getUser() const {
return cast<Instruction>(getValPtr());
}
void setUser(Instruction *NewUser) {
setValPtr(NewUser);
}
Value *getOperandValToReplace() const {
return OperandValToReplace;
}
void setOperandValToReplace(Value *Op) {
OperandValToReplace = Op;
}
const PostIncLoopSet &getPostIncLoops() const {
return PostIncLoops;
}
void transformToPostInc(const Loop *L);
private:
IVUsers *Parent;
WeakTrackingVH OperandValToReplace;
PostIncLoopSet PostIncLoops;
void deleted() override;
};
class IVUsers {
friend class IVStrideUse;
Loop *L;
AssumptionCache *AC;
LoopInfo *LI;
DominatorTree *DT;
ScalarEvolution *SE;
SmallPtrSet<Instruction*, 16> Processed;
ilist<IVStrideUse> IVUses;
SmallPtrSet<const Value *, 32> EphValues;
public:
IVUsers(Loop *L, AssumptionCache *AC, LoopInfo *LI, DominatorTree *DT,
ScalarEvolution *SE);
IVUsers(IVUsers &&X)
: L(std::move(X.L)), AC(std::move(X.AC)), DT(std::move(X.DT)),
SE(std::move(X.SE)), Processed(std::move(X.Processed)),
IVUses(std::move(X.IVUses)), EphValues(std::move(X.EphValues)) {
for (IVStrideUse &U : IVUses)
U.Parent = this;
}
IVUsers(const IVUsers &) = delete;
IVUsers &operator=(IVUsers &&) = delete;
IVUsers &operator=(const IVUsers &) = delete;
Loop *getLoop() const { return L; }
bool AddUsersIfInteresting(Instruction *I);
IVStrideUse &AddUser(Instruction *User, Value *Operand);
const SCEV *getReplacementExpr(const IVStrideUse &IU) const;
const SCEV *getExpr(const IVStrideUse &IU) const;
const SCEV *getStride(const IVStrideUse &IU, const Loop *L) const;
typedef ilist<IVStrideUse>::iterator iterator;
typedef ilist<IVStrideUse>::const_iterator const_iterator;
iterator begin() { return IVUses.begin(); }
iterator end() { return IVUses.end(); }
const_iterator begin() const { return IVUses.begin(); }
const_iterator end() const { return IVUses.end(); }
bool empty() const { return IVUses.empty(); }
bool isIVUserOrOperand(Instruction *Inst) const {
return Processed.count(Inst);
}
void releaseMemory();
void print(raw_ostream &OS, const Module * = nullptr) const;
void dump() const;
};
Pass *createIVUsersPass();
class IVUsersWrapperPass : public LoopPass {
std::unique_ptr<IVUsers> IU;
public:
static char ID;
IVUsersWrapperPass();
IVUsers &getIU() { return *IU; }
const IVUsers &getIU() const { return *IU; }
void getAnalysisUsage(AnalysisUsage &AU) const override;
bool runOnLoop(Loop *L, LPPassManager &LPM) override;
void releaseMemory() override;
void print(raw_ostream &OS, const Module * = nullptr) const override;
};
class IVUsersAnalysis : public AnalysisInfoMixin<IVUsersAnalysis> {
friend AnalysisInfoMixin<IVUsersAnalysis>;
static AnalysisKey Key;
public:
typedef IVUsers Result;
IVUsers run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR);
};
}
#endif