52 #define DEBUG_TYPE "safepoint-ir-verifier" 78 auto &PU = PredIt.
getUse();
85 assert(!isDeadBlock(InBB) &&
"block must be live");
89 if (InBB == *PredIt) {
90 if (!isDeadEdge(&getEdge(PredIt)))
96 assert(Listed &&
"basic block is not found among incoming blocks");
101 bool isDeadBlock(
const BasicBlock *BB)
const {
102 return DeadBlocks.
count(BB);
105 bool isDeadEdge(
const Use *U)
const {
106 assert(cast<Instruction>(U->getUser())->isTerminator() &&
107 "edge must be operand of terminator");
108 assert(cast_or_null<BasicBlock>(U->get()) &&
109 "edge must refer to basic block");
111 "isDeadEdge() must be applied to edge from live block");
112 return DeadEdges.
count(U);
115 bool hasLiveIncomingEdges(
const BasicBlock *BB)
const {
118 auto &PU = PredIt.
getUse();
120 if (!isDeadBlock(*PredIt) && !isDeadEdge(&U))
138 assert(TI &&
"blocks must be well formed");
164 while (!NewDead.
empty()) {
180 if (!isDeadBlock(S) && !hasLiveIncomingEdges(S))
185 void addDeadEdge(
const Use &DeadEdge) {
186 if (!DeadEdges.
insert(&DeadEdge))
190 if (hasLiveIncomingEdges(BB))
199 const CFGDeadness &CD);
206 CD.processFunction(F, DT);
221 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
223 CD.processFunction(F, DT);
233 StringRef getPassName()
const override {
return "safepoint verifier"; }
238 SafepointIRVerifier
pass;
239 pass.runOnFunction(F);
245 return new SafepointIRVerifier();
249 "Safepoint IR Verifier",
false,
false)
255 if (
auto *PT = dyn_cast<PointerType>(T))
259 return (1 == PT->getAddressSpace());
266 if (
VectorType *VT = dyn_cast<VectorType>(Ty))
268 if (
ArrayType *AT = dyn_cast<ArrayType>(Ty))
276 template<
typename IteratorTy>
279 while (Begin != End) {
280 OS << **Begin <<
" ";
307 bool Cleared =
false;
331 bool isExclusivelyDerivedFromNull =
true;
336 while(!Worklist.
empty()) {
338 if (!Visited.
insert(V).second)
341 if (
const auto *CI = dyn_cast<CastInst>(V)) {
342 Worklist.
push_back(CI->stripPointerCasts());
345 if (
const auto *
GEP = dyn_cast<GetElementPtrInst>(V)) {
351 if (
const auto *PN = dyn_cast<PHINode>(V)) {
356 if (
const auto *
SI = dyn_cast<SelectInst>(V)) {
362 if (isa<Constant>(V)) {
366 isExclusivelyDerivedFromNull =
false;
386 class InstructionVerifier;
446 const CFGDeadness &CD;
458 const CFGDeadness &CD);
461 return CD.hasLiveIncomingEdge(PN, InBB);
467 bool isValuePoisoned(
const Value *V)
const {
return PoisonedDefs.
count(V); }
480 return BlockMap.
find(BB) != BlockMap.
end();
485 bool instructionMayBeSkipped(
const Instruction *
I)
const;
489 void recalculateBBsStates();
497 bool removeValidUnrelocatedDefs(
const BasicBlock *BB,
513 bool ContributionChanged);
516 static void transferInstruction(
const Instruction &I,
bool &Cleared,
523 class InstructionVerifier {
524 bool AnyInvalidUses =
false;
527 void verifyInstruction(
const GCPtrTracker *Tracker,
const Instruction &
I,
530 bool hasAnyInvalidUses()
const {
return AnyInvalidUses; }
538 const CFGDeadness &CD) :
F(F), CD(CD) {
542 if (!CD.isDeadBlock(&BB)) {
544 for (
const auto &
I : BB)
551 for (
auto &BBI : BlockMap) {
552 gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
553 transferBlock(BBI.first, *BBI.second,
true);
559 recalculateBBsStates();
563 auto it = BlockMap.find(BB);
564 return it != BlockMap.end() ? it->second :
nullptr;
569 return const_cast<GCPtrTracker *
>(
this)->getBasicBlockState(BB);
572 bool GCPtrTracker::instructionMayBeSkipped(
const Instruction *
I)
const {
575 return ValidUnrelocatedDefs.count(I) || PoisonedDefs.count(I);
592 if (Tracker.instructionMayBeSkipped(&I))
595 Verifier.verifyInstruction(&Tracker, I, AvailableSet);
599 bool Cleared =
false;
600 transferInstruction(I, Cleared, AvailableSet);
606 void GCPtrTracker::recalculateBBsStates() {
610 for (
auto &BBI : BlockMap)
611 Worklist.
insert(BBI.first);
615 while (!Worklist.
empty()) {
625 if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
632 bool ContributionChanged =
634 if (!InputsChanged && !ContributionChanged)
638 transferBlock(BB, *BBS, ContributionChanged);
646 bool GCPtrTracker::removeValidUnrelocatedDefs(
const BasicBlock *BB,
650 "Passed Contribution should be from the passed BasicBlockState!");
652 bool ContributionChanged =
false;
656 bool ValidUnrelocatedPointerDef =
false;
657 bool PoisonedPointerDef =
false;
659 if (
const PHINode *PN = dyn_cast<PHINode>(&I)) {
662 bool HasRelocatedInputs =
false;
663 bool HasUnrelocatedInputs =
false;
666 if (!isMapped(InBB) ||
667 !CD.hasLiveIncomingEdge(PN, InBB))
673 if (isValuePoisoned(InValue)) {
675 HasRelocatedInputs =
true;
676 HasUnrelocatedInputs =
true;
679 if (BlockMap[InBB]->AvailableOut.count(InValue))
680 HasRelocatedInputs =
true;
682 HasUnrelocatedInputs =
true;
685 if (HasUnrelocatedInputs) {
686 if (HasRelocatedInputs)
687 PoisonedPointerDef =
true;
689 ValidUnrelocatedPointerDef =
true;
692 }
else if ((isa<GetElementPtrInst>(I) || isa<BitCastInst>(I)) &&
699 if (isValuePoisoned(V))
700 PoisonedPointerDef =
true;
702 ValidUnrelocatedPointerDef =
true;
706 assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
707 "Value cannot be both unrelocated and poisoned!");
708 if (ValidUnrelocatedPointerDef) {
711 Contribution.
erase(&I);
712 PoisonedDefs.erase(&I);
713 ValidUnrelocatedDefs.insert(&I);
715 <<
" from Contribution of " << BB->
getName() <<
"\n");
716 ContributionChanged =
true;
717 }
else if (PoisonedPointerDef) {
720 Contribution.
erase(&I);
721 PoisonedDefs.insert(&I);
722 LLVM_DEBUG(
dbgs() <<
"Removing poisoned " << I <<
" from Contribution of " 724 ContributionChanged =
true;
726 bool Cleared =
false;
727 transferInstruction(I, Cleared, AvailableSet);
731 return ContributionChanged;
734 void GCPtrTracker::gatherDominatingDefs(
const BasicBlock *BB,
739 assert(DTN &&
"Unreachable blocks are ignored");
742 auto BBS = getBasicBlockState(DTN->
getBlock());
743 assert(BBS &&
"immediate dominator cannot be dead for a live block");
745 Result.
insert(Defs.begin(), Defs.end());
760 bool ContributionChanged) {
766 if (ContributionChanged)
773 AvailableOut = std::move(Temp);
783 void GCPtrTracker::transferInstruction(
const Instruction &I,
bool &Cleared,
792 void InstructionVerifier::verifyInstruction(
795 if (
const PHINode *PN = dyn_cast<PHINode>(&I)) {
801 !Tracker->hasLiveIncomingEdge(PN, InBB))
808 reportInvalidUse(*InValue, *PN);
810 }
else if (isa<CmpInst>(I) &&
818 auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
827 if (AvailableSet.
count(LHS) || AvailableSet.
count(RHS))
858 if (!hasValidUnrelocatedUse()) {
862 reportInvalidUse(*LHS, I);
864 reportInvalidUse(*RHS, I);
870 reportInvalidUse(*V, I);
874 void InstructionVerifier::reportInvalidUse(
const Value &V,
876 errs() <<
"Illegal use of unrelocated value found!\n";
877 errs() <<
"Def: " << V <<
"\n";
878 errs() <<
"Use: " << I <<
"\n";
881 AnyInvalidUses =
true;
885 const CFGDeadness &CD) {
889 dbgs() <<
"Verifying gc pointers in function: " << F.
getName() <<
"\n";
891 GCPtrTracker Tracker(F, DT, CD);
899 if (
PrintOnly && !Verifier.hasAnyInvalidUses()) {
900 dbgs() <<
"No illegal uses found by SafepointIRVerifier in: " << F.
getName()
static void PrintValueSet(raw_ostream &OS, IteratorTy Begin, IteratorTy End)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents an incoming formal argument to a Function.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM_NODISCARD T pop_back_val()
This class represents lattice values for constants.
void initializeSafepointIRVerifierPass(PassRegistry &)
Implements a dense probed hash-table based set.
void push_back(const T &Elt)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null, or not exclusively derived from constant.
bool erase(const ValueT &V)
const Use & getOperandUse(unsigned i) const
BasicBlock * getSuccessor(unsigned i) const
Analysis pass which computes a DominatorTree.
static bool isNotExclusivelyConstantDerived(const Value *V)
Value * getCondition() const
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
#define INITIALIZE_PASS_DEPENDENCY(depName)
AvailableValueSet AvailableIn
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Type * getType() const
All values are typed, get the type of this value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
bool isStatepoint(const CallBase *Call)
Class to represent array types.
verify safepoint Safepoint IR static false bool isGCPointerType(Type *T)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Value * getOperand(unsigned i) const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Interval::succ_iterator succ_end(Interval *I)
Use & getUse() const
getUse - Return the operand Use in the predecessor's terminator of the successor. ...
iterator find(const_arg_type_t< KeyT > Val)
AvailableValueSet Contribution
State we compute and track per basic block.
static bool runOnFunction(Function &F, bool PostInlining)
initializer< Ty > init(const Ty &Val)
void verifySafepointIR(Function &F)
Run the safepoint verifier over a single function. Crashes on failure.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
FunctionPass * createSafepointIRVerifierPass()
Create an instance of the safepoint verifier pass which can be added to a pass pipeline to check for ...
void set_intersect(S1Ty &S1, const S2Ty &S2)
set_intersect(A, B) - Compute A := A ^ B Identical to set_intersection, except that it works on set<>...
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Conditional or Unconditional Branch instruction.
void getDescendants(NodeT *R, SmallVectorImpl< NodeT *> &Result) const
Get all nodes dominated by R, including R itself.
DomTreeNodeBase * getIDom() const
std::pair< iterator, bool > insert(const ValueT &V)
static bool containsGCPtrType(Type *Ty)
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
FunctionPass class - This class is used to implement most global optimizations.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
A SetVector that performs no allocations if smaller than a certain size.
This is the shared class of boolean and integer constants.
AnalysisUsage & addRequiredID(const void *ID)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
modulo schedule Modulo Schedule test pass
LLVM_NODISCARD T pop_back_val()
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
bool isConditional() const
unsigned getNumIncomingValues() const
Return the number of incoming edges.
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
AvailableValueSet AvailableOut
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Class to represent vector types.
void setPreservesAll()
Set by analyses that do not transform their input at all.
INITIALIZE_PASS_BEGIN(SafepointIRVerifier, "verify-safepoint-ir", "Safepoint IR Verifier", false, false) INITIALIZE_PASS_END(SafepointIRVerifier
static cl::opt< bool > PrintOnly("safepoint-ir-verifier-print-only", cl::init(false))
This option is used for writing test cases.
LLVM_NODISCARD bool empty() const
verify safepoint Safepoint IR Verifier
StringRef getName() const
Return a constant reference to the value's name.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
bool empty() const
Determine if the SetVector is empty or not.
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
A vector that has set insertion semantics.
succ_range successors(Instruction *I)
static const Function * getParent(const Value *V)
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
op_range incoming_values()
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
Statically lint checks LLVM IR
iterator_range< arg_iterator > args()
const BasicBlock * getParent() const
static void Verify(const Function &F, const DominatorTree &DT, const CFGDeadness &CD)