51#define DEBUG_TYPE "safepoint-ir-verifier"
77 auto &PU = PredIt.
getUse();
84 assert(!isDeadBlock(InBB) &&
"block must be live");
88 if (InBB == *PredIt) {
89 if (!isDeadEdge(&getEdge(PredIt)))
95 assert(Listed &&
"basic block is not found among incoming blocks");
100 bool isDeadBlock(
const BasicBlock *BB)
const {
101 return DeadBlocks.
count(BB);
104 bool isDeadEdge(
const Use *U)
const {
105 assert(cast<Instruction>(
U->getUser())->isTerminator() &&
106 "edge must be operand of terminator");
107 assert(cast_or_null<BasicBlock>(
U->get()) &&
108 "edge must refer to basic block");
109 assert(!isDeadBlock(cast<Instruction>(
U->getUser())->getParent()) &&
110 "isDeadEdge() must be applied to edge from live block");
111 return DeadEdges.
count(U);
114 bool hasLiveIncomingEdges(
const BasicBlock *BB)
const {
117 auto &PU = PredIt.
getUse();
118 const Use &
U = PU.getUser()->getOperandUse(PU.getOperandNo());
119 if (!isDeadBlock(*PredIt) && !isDeadEdge(&U))
137 assert(TI &&
"blocks must be well formed");
141 const BranchInst *BI = dyn_cast<BranchInst>(TI);
163 while (!NewDead.
empty()) {
179 if (!isDeadBlock(S) && !hasLiveIncomingEdges(S))
184 void addDeadEdge(
const Use &DeadEdge) {
185 if (!DeadEdges.
insert(&DeadEdge))
189 if (hasLiveIncomingEdges(BB))
198 const CFGDeadness &CD);
205 CD.processFunction(
F, DT);
220 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
222 CD.processFunction(
F, DT);
237 SafepointIRVerifier
pass;
238 pass.runOnFunction(
F);
241char SafepointIRVerifier::ID = 0;
244 return new SafepointIRVerifier();
248 "Safepoint IR Verifier",
false,
false)
254 if (
auto *PT = dyn_cast<PointerType>(
T))
258 return (1 == PT->getAddressSpace());
265 if (
VectorType *VT = dyn_cast<VectorType>(Ty))
267 if (
ArrayType *AT = dyn_cast<ArrayType>(Ty))
269 if (
StructType *ST = dyn_cast<StructType>(Ty))
275template<
typename IteratorTy>
278 while (Begin !=
End) {
279 OS << **Begin <<
" ";
294struct BasicBlockState {
307 bool Cleared =
false;
332 bool isExclusivelyDerivedFromNull =
true;
337 while(!Worklist.
empty()) {
339 if (!Visited.
insert(V).second)
342 if (
const auto *CI = dyn_cast<CastInst>(V)) {
343 Worklist.
push_back(CI->stripPointerCasts());
346 if (
const auto *
GEP = dyn_cast<GetElementPtrInst>(V)) {
352 if (
const auto *PN = dyn_cast<PHINode>(V)) {
356 if (
const auto *SI = dyn_cast<SelectInst>(V)) {
362 if (
const auto *GCRelocate = dyn_cast<GCRelocateInst>(V)) {
365 Worklist.
push_back(GCRelocate->getDerivedPtr());
368 if (
const auto *FI = dyn_cast<FreezeInst>(V)) {
373 if (isa<Constant>(V)) {
377 isExclusivelyDerivedFromNull =
false;
397class InstructionVerifier;
457 const CFGDeadness &CD;
469 const CFGDeadness &CD);
472 return CD.hasLiveIncomingEdge(PN, InBB);
475 BasicBlockState *getBasicBlockState(
const BasicBlock *BB);
476 const BasicBlockState *getBasicBlockState(
const BasicBlock *BB)
const;
478 bool isValuePoisoned(
const Value *V)
const {
return PoisonedDefs.
count(V); }
494 bool instructionMayBeSkipped(
const Instruction *
I)
const;
498 void recalculateBBsStates();
506 bool removeValidUnrelocatedDefs(
const BasicBlock *BB,
507 const BasicBlockState *BBS,
521 static void transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
522 bool ContributionChanged);
525 static void transferInstruction(
const Instruction &
I,
bool &Cleared,
532class InstructionVerifier {
533 bool AnyInvalidUses =
false;
536 void verifyInstruction(
const GCPtrTracker *Tracker,
const Instruction &
I,
539 bool hasAnyInvalidUses()
const {
return AnyInvalidUses; }
547 const CFGDeadness &CD) :
F(
F), CD(CD) {
551 if (!CD.isDeadBlock(&BB)) {
552 BasicBlockState *BBS =
new (BSAllocator.
Allocate()) BasicBlockState;
553 for (
const auto &
I : BB)
554 transferInstruction(
I, BBS->Cleared, BBS->Contribution);
560 for (
auto &BBI : BlockMap) {
561 gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
562 transferBlock(BBI.first, *BBI.second,
true);
568 recalculateBBsStates();
571BasicBlockState *GCPtrTracker::getBasicBlockState(
const BasicBlock *BB) {
572 return BlockMap.
lookup(BB);
575const BasicBlockState *GCPtrTracker::getBasicBlockState(
577 return const_cast<GCPtrTracker *
>(
this)->getBasicBlockState(BB);
580bool GCPtrTracker::instructionMayBeSkipped(
const Instruction *
I)
const {
583 return ValidUnrelocatedDefs.
count(
I) || PoisonedDefs.
count(
I);
586void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
592 BasicBlockState *BBS = Tracker.getBasicBlockState(BB);
600 if (Tracker.instructionMayBeSkipped(&
I))
603 Verifier.verifyInstruction(&Tracker,
I, AvailableSet);
607 bool Cleared =
false;
608 transferInstruction(
I, Cleared, AvailableSet);
614void GCPtrTracker::recalculateBBsStates() {
618 for (
auto &BBI : BlockMap)
619 Worklist.
insert(BBI.first);
623 while (!Worklist.
empty()) {
625 BasicBlockState *BBS = getBasicBlockState(BB);
629 size_t OldInCount = BBS->AvailableIn.size();
632 BasicBlockState *PBBS = getBasicBlockState(PBB);
633 if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
637 assert(OldInCount >= BBS->AvailableIn.size() &&
"invariant!");
639 bool InputsChanged = OldInCount != BBS->AvailableIn.size();
640 bool ContributionChanged =
641 removeValidUnrelocatedDefs(BB, BBS, BBS->Contribution);
642 if (!InputsChanged && !ContributionChanged)
645 size_t OldOutCount = BBS->AvailableOut.size();
646 transferBlock(BB, *BBS, ContributionChanged);
647 if (OldOutCount != BBS->AvailableOut.size()) {
648 assert(OldOutCount > BBS->AvailableOut.size() &&
"invariant!");
654bool GCPtrTracker::removeValidUnrelocatedDefs(
const BasicBlock *BB,
655 const BasicBlockState *BBS,
657 assert(&BBS->Contribution == &Contribution &&
658 "Passed Contribution should be from the passed BasicBlockState!");
660 bool ContributionChanged =
false;
664 bool ValidUnrelocatedPointerDef =
false;
665 bool PoisonedPointerDef =
false;
667 if (
const PHINode *PN = dyn_cast<PHINode>(&
I)) {
670 bool HasRelocatedInputs =
false;
671 bool HasUnrelocatedInputs =
false;
674 if (!isMapped(InBB) ||
675 !CD.hasLiveIncomingEdge(PN, InBB))
681 if (isValuePoisoned(InValue)) {
683 HasRelocatedInputs =
true;
684 HasUnrelocatedInputs =
true;
687 if (BlockMap[InBB]->AvailableOut.count(InValue))
688 HasRelocatedInputs =
true;
690 HasUnrelocatedInputs =
true;
693 if (HasUnrelocatedInputs) {
694 if (HasRelocatedInputs)
695 PoisonedPointerDef =
true;
697 ValidUnrelocatedPointerDef =
true;
700 }
else if ((isa<GetElementPtrInst>(
I) || isa<BitCastInst>(
I)) &&
704 for (
const Value *V :
I.operands())
707 if (isValuePoisoned(V))
708 PoisonedPointerDef =
true;
710 ValidUnrelocatedPointerDef =
true;
714 assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
715 "Value cannot be both unrelocated and poisoned!");
716 if (ValidUnrelocatedPointerDef) {
721 ValidUnrelocatedDefs.
insert(&
I);
723 <<
" from Contribution of " << BB->getName() <<
"\n");
724 ContributionChanged =
true;
725 }
else if (PoisonedPointerDef) {
730 LLVM_DEBUG(
dbgs() <<
"Removing poisoned " <<
I <<
" from Contribution of "
731 << BB->getName() <<
"\n");
732 ContributionChanged =
true;
734 bool Cleared =
false;
735 transferInstruction(
I, Cleared, AvailableSet);
739 return ContributionChanged;
742void GCPtrTracker::gatherDominatingDefs(
const BasicBlock *BB,
747 assert(DTN &&
"Unreachable blocks are ignored");
750 auto BBS = getBasicBlockState(DTN->
getBlock());
751 assert(BBS &&
"immediate dominator cannot be dead for a live block");
752 const auto &Defs = BBS->Contribution;
753 Result.insert(Defs.begin(), Defs.end());
767void GCPtrTracker::transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
768 bool ContributionChanged) {
774 if (ContributionChanged)
775 AvailableOut = BBS.Contribution;
781 AvailableOut = std::move(Temp);
791void GCPtrTracker::transferInstruction(
const Instruction &
I,
bool &Cleared,
793 if (isa<GCStatepointInst>(
I)) {
800void InstructionVerifier::verifyInstruction(
803 if (
const PHINode *PN = dyn_cast<PHINode>(&
I)) {
807 const BasicBlockState *InBBS = Tracker->getBasicBlockState(InBB);
809 !Tracker->hasLiveIncomingEdge(PN, InBB))
815 !InBBS->AvailableOut.count(InValue))
816 reportInvalidUse(*InValue, *PN);
818 }
else if (isa<CmpInst>(
I) &&
826 auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
835 if (AvailableSet.
count(LHS) || AvailableSet.
count(RHS))
866 if (!hasValidUnrelocatedUse()) {
870 reportInvalidUse(*LHS,
I);
872 reportInvalidUse(*RHS,
I);
875 for (
const Value *V :
I.operands())
878 reportInvalidUse(*V,
I);
882void InstructionVerifier::reportInvalidUse(
const Value &V,
884 errs() <<
"Illegal use of unrelocated value found!\n";
885 errs() <<
"Def: " <<
V <<
"\n";
886 errs() <<
"Use: " <<
I <<
"\n";
889 AnyInvalidUses =
true;
893 const CFGDeadness &CD) {
894 LLVM_DEBUG(
dbgs() <<
"Verifying gc pointers in function: " <<
F.getName()
897 dbgs() <<
"Verifying gc pointers in function: " <<
F.getName() <<
"\n";
899 GCPtrTracker Tracker(
F, DT, CD);
905 GCPtrTracker::verifyFunction(std::move(Tracker),
Verifier);
908 dbgs() <<
"No illegal uses found by SafepointIRVerifier in: " <<
F.getName()
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
This file defines the DenseSet and SmallDenseSet classes.
@ Available
We know the block is fully available. This is a fixpoint.
global merge Global merge function pass
Legalize the Machine IR a function s Machine IR
ppc ctr loops PowerPC CTR Loops Verify
#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 builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static cl::opt< bool > PrintOnly("safepoint-ir-verifier-print-only", cl::init(false))
This option is used for writing test cases.
verify safepoint Safepoint IR static false bool isGCPointerType(Type *T)
static bool isNotExclusivelyConstantDerived(const Value *V)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
static bool containsGCPtrType(Type *Ty)
static void PrintValueSet(raw_ostream &OS, IteratorTy Begin, IteratorTy End)
verify safepoint Safepoint IR Verifier
@ ExclusivelySomeConstant
This file defines generic set operations that may be used on set's of different types,...
This file implements a set that has insertion order iteration characteristics.
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequiredID(const void *ID)
void setPreservesAll()
Set by analyses that do not transform their input at all.
This class represents an incoming formal argument to a Function.
LLVM Basic Block Representation.
const Function * getParent() const
Return the enclosing method, or null if none.
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...
Conditional or Unconditional Branch instruction.
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
This is the shared class of boolean and integer constants.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Implements a dense probed hash-table based set.
DomTreeNodeBase * getIDom() const
Analysis pass which computes a DominatorTree.
void getDescendants(NodeT *R, SmallVectorImpl< NodeT * > &Result) const
Get all nodes dominated by R, including R itself.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
FunctionPass class - This class is used to implement most global optimizations.
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
iterator_range< arg_iterator > args()
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Use & getUse() const
getUse - Return the operand Use in the predecessor's terminator of the successor.
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.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A vector that has set insertion semantics.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
value_type pop_back_val()
A SetVector that performs no allocations if smaller than a certain size.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A BumpPtrAllocator that allows only elements of a specific type to be allocated.
T * Allocate(size_t num=1)
Allocate space for an array of objects without constructing them.
StringRef - Represent a constant reference to a string, i.e.
Class to represent struct types.
The instances of the Type class are immutable: once they are created, they are never changed.
A Use represents the edge between a Value definition and its users.
User * getUser() const
Returns the User that contains this Use.
const Use & getOperandUse(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
StringRef getName() const
Return a constant reference to the value's name.
std::pair< iterator, bool > insert(const ValueT &V)
bool erase(const ValueT &V)
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
const ParentTy * getParent() const
This class implements an extremely fast bulk output stream that can only output to a stream.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
void initializeSafepointIRVerifierPass(PassRegistry &)
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<>...
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
auto successors(const MachineBasicBlock *BB)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
void verifySafepointIR(Function &F)
Run the safepoint verifier over a single function. Crashes on failure.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createSafepointIRVerifierPass()
Create an instance of the safepoint verifier pass which can be added to a pass pipeline to check for ...
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)