#ifndef LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
#define LLVM_TRANSFORMS_UTILS_PREDICATEINFO_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/ilist.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Pass.h"
namespace llvm {
class AssumptionCache;
class DominatorTree;
class Function;
class Value;
class IntrinsicInst;
class raw_ostream;
enum PredicateType { PT_Branch, PT_Assume, PT_Switch };
struct PredicateConstraint {
CmpInst::Predicate Predicate;
Value *OtherOp;
};
class PredicateBase : public ilist_node<PredicateBase> {
public:
PredicateType Type;
Value *OriginalOp;
Value *RenamedOp;
Value *Condition;
PredicateBase(const PredicateBase &) = delete;
PredicateBase &operator=(const PredicateBase &) = delete;
PredicateBase() = delete;
virtual ~PredicateBase() = default;
static bool classof(const PredicateBase *PB) {
return PB->Type == PT_Assume || PB->Type == PT_Branch ||
PB->Type == PT_Switch;
}
Optional<PredicateConstraint> getConstraint() const;
protected:
PredicateBase(PredicateType PT, Value *Op, Value *Condition)
: Type(PT), OriginalOp(Op), Condition(Condition) {}
};
class PredicateAssume : public PredicateBase {
public:
IntrinsicInst *AssumeInst;
PredicateAssume(Value *Op, IntrinsicInst *AssumeInst, Value *Condition)
: PredicateBase(PT_Assume, Op, Condition), AssumeInst(AssumeInst) {}
PredicateAssume() = delete;
static bool classof(const PredicateBase *PB) {
return PB->Type == PT_Assume;
}
};
class PredicateWithEdge : public PredicateBase {
public:
BasicBlock *From;
BasicBlock *To;
PredicateWithEdge() = delete;
static bool classof(const PredicateBase *PB) {
return PB->Type == PT_Branch || PB->Type == PT_Switch;
}
protected:
PredicateWithEdge(PredicateType PType, Value *Op, BasicBlock *From,
BasicBlock *To, Value *Cond)
: PredicateBase(PType, Op, Cond), From(From), To(To) {}
};
class PredicateBranch : public PredicateWithEdge {
public:
bool TrueEdge;
PredicateBranch(Value *Op, BasicBlock *BranchBB, BasicBlock *SplitBB,
Value *Condition, bool TakenEdge)
: PredicateWithEdge(PT_Branch, Op, BranchBB, SplitBB, Condition),
TrueEdge(TakenEdge) {}
PredicateBranch() = delete;
static bool classof(const PredicateBase *PB) {
return PB->Type == PT_Branch;
}
};
class PredicateSwitch : public PredicateWithEdge {
public:
Value *CaseValue;
SwitchInst *Switch;
PredicateSwitch(Value *Op, BasicBlock *SwitchBB, BasicBlock *TargetBB,
Value *CaseValue, SwitchInst *SI)
: PredicateWithEdge(PT_Switch, Op, SwitchBB, TargetBB,
SI->getCondition()),
CaseValue(CaseValue), Switch(SI) {}
PredicateSwitch() = delete;
static bool classof(const PredicateBase *PB) {
return PB->Type == PT_Switch;
}
};
class PredicateInfo {
public:
PredicateInfo(Function &, DominatorTree &, AssumptionCache &);
~PredicateInfo();
void verifyPredicateInfo() const;
void dump() const;
void print(raw_ostream &) const;
const PredicateBase *getPredicateInfoFor(const Value *V) const {
return PredicateMap.lookup(V);
}
protected:
friend class PredicateInfoAnnotatedWriter;
friend class PredicateInfoPrinterLegacyPass;
friend class PredicateInfoBuilder;
private:
Function &F;
iplist<PredicateBase> AllInfos;
DenseMap<const Value *, const PredicateBase *> PredicateMap;
SmallSet<AssertingVH<Function>, 20> CreatedDeclarations;
};
class PredicateInfoPrinterLegacyPass : public FunctionPass {
public:
PredicateInfoPrinterLegacyPass();
static char ID;
bool runOnFunction(Function &) override;
void getAnalysisUsage(AnalysisUsage &AU) const override;
};
class PredicateInfoPrinterPass
: public PassInfoMixin<PredicateInfoPrinterPass> {
raw_ostream &OS;
public:
explicit PredicateInfoPrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};
struct PredicateInfoVerifierPass : PassInfoMixin<PredicateInfoVerifierPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};
}
#endif