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 <<
" ";
330 bool isExclusivelyDerivedFromNull =
true;
335 while(!Worklist.
empty()) {
337 if (!Visited.
insert(V).second)
340 if (
const auto *CI = dyn_cast<CastInst>(V)) {
341 Worklist.
push_back(CI->stripPointerCasts());
344 if (
const auto *
GEP = dyn_cast<GetElementPtrInst>(V)) {
350 if (
const auto *PN = dyn_cast<PHINode>(V)) {
354 if (
const auto *SI = dyn_cast<SelectInst>(V)) {
360 if (
const auto *GCRelocate = dyn_cast<GCRelocateInst>(V)) {
363 Worklist.
push_back(GCRelocate->getDerivedPtr());
366 if (
const auto *FI = dyn_cast<FreezeInst>(V)) {
371 if (isa<Constant>(V)) {
375 isExclusivelyDerivedFromNull =
false;
395class InstructionVerifier;
455 const CFGDeadness &CD;
467 const CFGDeadness &CD);
470 return CD.hasLiveIncomingEdge(PN, InBB);
476 bool isValuePoisoned(
const Value *V)
const {
return PoisonedDefs.
count(V); }
492 bool instructionMayBeSkipped(
const Instruction *
I)
const;
496 void recalculateBBsStates();
504 bool removeValidUnrelocatedDefs(
const BasicBlock *BB,
520 bool ContributionChanged);
523 static void transferInstruction(
const Instruction &
I,
bool &Cleared,
530class InstructionVerifier {
531 bool AnyInvalidUses =
false;
534 void verifyInstruction(
const GCPtrTracker *Tracker,
const Instruction &
I,
537 bool hasAnyInvalidUses()
const {
return AnyInvalidUses; }
545 const CFGDeadness &CD) :
F(
F), CD(CD) {
549 if (!CD.isDeadBlock(&BB)) {
551 for (
const auto &
I : BB)
558 for (
auto &BBI : BlockMap) {
559 gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
560 transferBlock(BBI.first, *BBI.second,
true);
566 recalculateBBsStates();
570 return BlockMap.
lookup(BB);
575 return const_cast<GCPtrTracker *
>(
this)->getBasicBlockState(BB);
578bool GCPtrTracker::instructionMayBeSkipped(
const Instruction *
I)
const {
581 return ValidUnrelocatedDefs.
count(
I) || PoisonedDefs.
count(
I);
584void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
598 if (Tracker.instructionMayBeSkipped(&
I))
601 Verifier.verifyInstruction(&Tracker,
I, AvailableSet);
605 bool Cleared =
false;
606 transferInstruction(
I, Cleared, AvailableSet);
612void GCPtrTracker::recalculateBBsStates() {
616 for (
auto &BBI : BlockMap)
617 Worklist.
insert(BBI.first);
621 while (!Worklist.
empty()) {
631 if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
638 bool ContributionChanged =
640 if (!InputsChanged && !ContributionChanged)
644 transferBlock(BB, *BBS, ContributionChanged);
652bool GCPtrTracker::removeValidUnrelocatedDefs(
const BasicBlock *BB,
656 "Passed Contribution should be from the passed BasicBlockState!");
658 bool ContributionChanged =
false;
662 bool ValidUnrelocatedPointerDef =
false;
663 bool PoisonedPointerDef =
false;
665 if (
const PHINode *PN = dyn_cast<PHINode>(&
I)) {
668 bool HasRelocatedInputs =
false;
669 bool HasUnrelocatedInputs =
false;
672 if (!isMapped(InBB) ||
673 !CD.hasLiveIncomingEdge(PN, InBB))
679 if (isValuePoisoned(InValue)) {
681 HasRelocatedInputs =
true;
682 HasUnrelocatedInputs =
true;
685 if (BlockMap[InBB]->AvailableOut.count(InValue))
686 HasRelocatedInputs =
true;
688 HasUnrelocatedInputs =
true;
691 if (HasUnrelocatedInputs) {
692 if (HasRelocatedInputs)
693 PoisonedPointerDef =
true;
695 ValidUnrelocatedPointerDef =
true;
698 }
else if ((isa<GetElementPtrInst>(
I) || isa<BitCastInst>(
I)) &&
702 for (
const Value *V :
I.operands())
705 if (isValuePoisoned(V))
706 PoisonedPointerDef =
true;
708 ValidUnrelocatedPointerDef =
true;
712 assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
713 "Value cannot be both unrelocated and poisoned!");
714 if (ValidUnrelocatedPointerDef) {
719 ValidUnrelocatedDefs.
insert(&
I);
721 <<
" from Contribution of " << BB->getName() <<
"\n");
722 ContributionChanged =
true;
723 }
else if (PoisonedPointerDef) {
728 LLVM_DEBUG(
dbgs() <<
"Removing poisoned " <<
I <<
" from Contribution of "
729 << BB->getName() <<
"\n");
730 ContributionChanged =
true;
732 bool Cleared =
false;
733 transferInstruction(
I, Cleared, AvailableSet);
737 return ContributionChanged;
740void GCPtrTracker::gatherDominatingDefs(
const BasicBlock *BB,
745 assert(DTN &&
"Unreachable blocks are ignored");
748 auto BBS = getBasicBlockState(DTN->
getBlock());
749 assert(BBS &&
"immediate dominator cannot be dead for a live block");
751 Result.insert(Defs.begin(), Defs.end());
766 bool ContributionChanged) {
772 if (ContributionChanged)
779 AvailableOut = std::move(Temp);
789void GCPtrTracker::transferInstruction(
const Instruction &
I,
bool &Cleared,
791 if (isa<GCStatepointInst>(
I)) {
798void InstructionVerifier::verifyInstruction(
801 if (
const PHINode *PN = dyn_cast<PHINode>(&
I)) {
807 !Tracker->hasLiveIncomingEdge(PN, InBB))
814 reportInvalidUse(*InValue, *PN);
816 }
else if (isa<CmpInst>(
I) &&
824 auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
833 if (AvailableSet.
count(LHS) || AvailableSet.
count(RHS))
864 if (!hasValidUnrelocatedUse()) {
868 reportInvalidUse(*LHS,
I);
870 reportInvalidUse(*RHS,
I);
873 for (
const Value *V :
I.operands())
876 reportInvalidUse(*V,
I);
880void InstructionVerifier::reportInvalidUse(
const Value &V,
882 errs() <<
"Illegal use of unrelocated value found!\n";
883 errs() <<
"Def: " <<
V <<
"\n";
884 errs() <<
"Use: " <<
I <<
"\n";
887 AnyInvalidUses =
true;
891 const CFGDeadness &CD) {
892 LLVM_DEBUG(
dbgs() <<
"Verifying gc pointers in function: " <<
F.getName()
895 dbgs() <<
"Verifying gc pointers in function: " <<
F.getName() <<
"\n";
897 GCPtrTracker Tracker(
F, DT, CD);
903 GCPtrTracker::verifyFunction(std::move(Tracker),
Verifier);
906 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.
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)
State we compute and track per basic block.
AvailableValueSet AvailableOut
AvailableValueSet Contribution
AvailableValueSet AvailableIn