51#define DEBUG_TYPE "safepoint-ir-verifier"
71 auto &PU = PredIt.
getUse();
78 assert(!isDeadBlock(InBB) &&
"block must be live");
82 if (InBB == *PredIt) {
83 if (!isDeadEdge(&getEdge(PredIt)))
89 assert(Listed &&
"basic block is not found among incoming blocks");
95 return DeadBlocks.
count(BB);
98 bool isDeadEdge(
const Use *U)
const {
100 "edge must be operand of terminator");
102 "edge must refer to basic block");
104 "isDeadEdge() must be applied to edge from live block");
105 return DeadEdges.
count(U);
108 bool hasLiveIncomingEdges(
const BasicBlock *BB)
const {
111 auto &PU = PredIt.
getUse();
113 if (!isDeadBlock(*PredIt) && !isDeadEdge(&U))
155 while (!NewDead.
empty()) {
171 if (!isDeadBlock(S) && !hasLiveIncomingEdges(S))
176 void addDeadEdge(
const Use &DeadEdge) {
177 if (!DeadEdges.
insert(&DeadEdge))
181 if (hasLiveIncomingEdges(BB))
190 const CFGDeadness &CD);
196 CD.processFunction(
F, DT);
208 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
210 CD.processFunction(
F, DT);
215 void getAnalysisUsage(AnalysisUsage &AU)
const override {
220 StringRef getPassName()
const override {
return "safepoint verifier"; }
225 SafepointIRVerifier
pass;
226 pass.runOnFunction(
F);
229char SafepointIRVerifier::ID = 0;
232 return new SafepointIRVerifier();
236 "Safepoint IR Verifier",
false,
false)
246 return (1 == PT->getAddressSpace());
265template <
typename IteratorTy>
269 while (Begin !=
End) {
270 OS << **Begin <<
" ";
285struct BasicBlockState {
298 bool Cleared =
false;
323 bool isExclusivelyDerivedFromNull =
true;
328 while(!Worklist.
empty()) {
330 if (!Visited.
insert(V).second)
334 Worklist.
push_back(CI->stripPointerCasts());
356 Worklist.
push_back(GCRelocate->getDerivedPtr());
368 isExclusivelyDerivedFromNull =
false;
388class InstructionVerifier;
448 const CFGDeadness &CD;
449 SpecificBumpPtrAllocator<BasicBlockState> BSAllocator;
450 DenseMap<const BasicBlock *, BasicBlockState *> BlockMap;
453 DenseSet<const Instruction *> ValidUnrelocatedDefs;
456 DenseSet<const Value *> PoisonedDefs;
459 GCPtrTracker(
const Function &
F,
const DominatorTree &DT,
460 const CFGDeadness &CD);
462 bool hasLiveIncomingEdge(
const PHINode *PN,
const BasicBlock *InBB)
const {
463 return CD.hasLiveIncomingEdge(PN, InBB);
466 BasicBlockState *getBasicBlockState(
const BasicBlock *BB);
467 const BasicBlockState *getBasicBlockState(
const BasicBlock *BB)
const;
469 bool isValuePoisoned(
const Value *V)
const {
return PoisonedDefs.
count(V); }
481 bool isMapped(
const BasicBlock *BB)
const {
return BlockMap.
contains(BB); }
485 bool instructionMayBeSkipped(
const Instruction *
I)
const;
489 void recalculateBBsStates();
497 bool removeValidUnrelocatedDefs(
const BasicBlock *BB,
498 const BasicBlockState *BBS,
505 const DominatorTree &DT);
512 static void transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
513 bool ContributionChanged);
516 static void transferInstruction(
const Instruction &
I,
bool &Cleared,
523class InstructionVerifier {
524 bool AnyInvalidUses =
false;
527 void verifyInstruction(
const GCPtrTracker *Tracker,
const Instruction &
I,
530 bool hasAnyInvalidUses()
const {
return AnyInvalidUses; }
533 void reportInvalidUse(
const Value &V,
const Instruction &
I);
538 const CFGDeadness &CD) :
F(
F), CD(CD) {
542 if (!CD.isDeadBlock(&BB)) {
543 BasicBlockState *BBS = new (BSAllocator.Allocate()) BasicBlockState;
544 for (const auto &I : BB)
545 transferInstruction(I, BBS->Cleared, BBS->Contribution);
551 for (
auto &BBI : BlockMap) {
552 gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
553 transferBlock(BBI.first, *BBI.second,
true);
559 recalculateBBsStates();
562BasicBlockState *GCPtrTracker::getBasicBlockState(
const BasicBlock *BB) {
563 return BlockMap.
lookup(BB);
566const BasicBlockState *GCPtrTracker::getBasicBlockState(
567 const BasicBlock *BB)
const {
568 return const_cast<GCPtrTracker *
>(
this)->getBasicBlockState(BB);
571bool GCPtrTracker::instructionMayBeSkipped(
const Instruction *
I)
const {
574 return ValidUnrelocatedDefs.
count(
I) || PoisonedDefs.
count(
I);
577void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
581 ReversePostOrderTraversal<const Function *> RPOT(&Tracker.F);
582 for (
const BasicBlock *BB : RPOT) {
583 BasicBlockState *BBS = Tracker.getBasicBlockState(BB);
590 for (
const Instruction &
I : *BB) {
591 if (Tracker.instructionMayBeSkipped(&
I))
594 Verifier.verifyInstruction(&Tracker,
I, AvailableSet);
598 bool Cleared =
false;
599 transferInstruction(
I, Cleared, AvailableSet);
605void GCPtrTracker::recalculateBBsStates() {
613 while (!Worklist.empty()) {
614 const BasicBlock *BB = Worklist.pop_back_val();
615 BasicBlockState *BBS = getBasicBlockState(BB);
619 size_t OldInCount = BBS->AvailableIn.
size();
622 BasicBlockState *PBBS = getBasicBlockState(PBB);
623 if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
627 assert(OldInCount >= BBS->AvailableIn.
size() &&
"invariant!");
629 bool InputsChanged = OldInCount != BBS->AvailableIn.
size();
630 bool ContributionChanged =
631 removeValidUnrelocatedDefs(BB, BBS, BBS->Contribution);
632 if (!InputsChanged && !ContributionChanged)
635 size_t OldOutCount = BBS->AvailableOut.
size();
636 transferBlock(BB, *BBS, ContributionChanged);
637 if (OldOutCount != BBS->AvailableOut.
size()) {
638 assert(OldOutCount > BBS->AvailableOut.
size() &&
"invariant!");
644bool GCPtrTracker::removeValidUnrelocatedDefs(
const BasicBlock *BB,
645 const BasicBlockState *BBS,
647 assert(&BBS->Contribution == &Contribution &&
648 "Passed Contribution should be from the passed BasicBlockState!");
650 bool ContributionChanged =
false;
653 for (
const Instruction &
I : *BB) {
654 bool ValidUnrelocatedPointerDef =
false;
655 bool PoisonedPointerDef =
false;
660 bool HasRelocatedInputs =
false;
661 bool HasUnrelocatedInputs =
false;
664 if (!isMapped(InBB) ||
665 !CD.hasLiveIncomingEdge(PN, InBB))
671 if (isValuePoisoned(InValue)) {
673 HasRelocatedInputs =
true;
674 HasUnrelocatedInputs =
true;
677 if (BlockMap[InBB]->AvailableOut.count(InValue))
678 HasRelocatedInputs =
true;
680 HasUnrelocatedInputs =
true;
683 if (HasUnrelocatedInputs) {
684 if (HasRelocatedInputs)
685 PoisonedPointerDef =
true;
687 ValidUnrelocatedPointerDef =
true;
694 for (
const Value *V :
I.operands())
697 if (isValuePoisoned(V))
698 PoisonedPointerDef =
true;
700 ValidUnrelocatedPointerDef =
true;
704 assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
705 "Value cannot be both unrelocated and poisoned!");
706 if (ValidUnrelocatedPointerDef) {
711 ValidUnrelocatedDefs.
insert(&
I);
713 <<
" from Contribution of " << BB->getName() <<
"\n");
714 ContributionChanged =
true;
715 }
else if (PoisonedPointerDef) {
720 LLVM_DEBUG(
dbgs() <<
"Removing poisoned " <<
I <<
" from Contribution of "
721 << BB->getName() <<
"\n");
722 ContributionChanged =
true;
724 bool Cleared =
false;
725 transferInstruction(
I, Cleared, AvailableSet);
729 return ContributionChanged;
732void GCPtrTracker::gatherDominatingDefs(
const BasicBlock *BB,
734 const DominatorTree &DT) {
737 assert(DTN &&
"Unreachable blocks are ignored");
740 auto BBS = getBasicBlockState(DTN->
getBlock());
741 assert(BBS &&
"immediate dominator cannot be dead for a live block");
742 const auto &Defs = BBS->Contribution;
743 Result.insert_range(Defs);
757void GCPtrTracker::transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
758 bool ContributionChanged) {
764 if (ContributionChanged)
765 AvailableOut = BBS.Contribution;
771 AvailableOut = std::move(Temp);
781void GCPtrTracker::transferInstruction(
const Instruction &
I,
bool &Cleared,
790void InstructionVerifier::verifyInstruction(
791 const GCPtrTracker *Tracker,
const Instruction &
I,
797 const BasicBlockState *InBBS = Tracker->getBasicBlockState(InBB);
799 !Tracker->hasLiveIncomingEdge(PN, InBB))
805 !InBBS->AvailableOut.
count(InValue))
806 reportInvalidUse(*InValue, *PN);
816 auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
856 if (!hasValidUnrelocatedUse()) {
860 reportInvalidUse(*
LHS,
I);
862 reportInvalidUse(*
RHS,
I);
865 for (
const Value *V :
I.operands())
868 reportInvalidUse(*V,
I);
872void InstructionVerifier::reportInvalidUse(
const Value &V,
873 const Instruction &
I) {
874 errs() <<
"Illegal use of unrelocated value found!\n";
875 errs() <<
"Def: " <<
V <<
"\n";
876 errs() <<
"Use: " <<
I <<
"\n";
877 if (!
getIROptions(
I.getContext()).safepoint_ir_verifier_print_only)
879 AnyInvalidUses =
true;
883 const CFGDeadness &CD) {
884 LLVM_DEBUG(
dbgs() <<
"Verifying gc pointers in function: " <<
F.getName()
887 getIROptions(
F.getContext()).safepoint_ir_verifier_print_only;
889 dbgs() <<
"Verifying gc pointers in function: " <<
F.getName() <<
"\n";
891 GCPtrTracker Tracker(
F, DT, CD);
896 InstructionVerifier Verifier;
897 GCPtrTracker::verifyFunction(std::move(Tracker), Verifier);
899 if (PrintOnly && !Verifier.hasAnyInvalidUses()) {
900 dbgs() <<
"No illegal uses found by SafepointIRVerifier in: " <<
F.getName()
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
static bool runOnFunction(Function &F, bool PostInlining)
@ Available
We know the block is fully available. This is a fixpoint.
modulo schedule Modulo Schedule test pass
static bool processFunction(Function &F, NVPTXTargetMachine &TM)
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
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)
DenseSet< const Value * > AvailableValueSet
The verifier algorithm is phrased in terms of availability.
static void PrintValueSet(raw_ostream &OS, IteratorTy Begin, IteratorTy End)
verify safepoint Safepoint IR Verifier
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
@ 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.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM_ABI AnalysisUsage & addRequiredID(const void *ID)
void setPreservesAll()
Set by analyses that do not transform their input at all.
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; assumes that the block is well-formed.
Conditional Branch instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
This is the shared class of boolean and integer constants.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
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.
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
FunctionPass class - This class is used to implement most global optimizations.
iterator_range< arg_iterator > args()
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.
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.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A vector that has set insertion semantics.
void insert_range(Range &&R)
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
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.
LLVM_ABI 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.
@ BasicBlock
Various leaf nodes.
This is an optimization pass for GlobalISel generic memory operations.
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<>...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI 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)
constexpr from_range_t from_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
auto cast_or_null(const Y &Val)
LLVM_ABI void verifySafepointIR(Function &F)
Run the safepoint verifier over a single function. Crashes on failure.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
PredIterator< const BasicBlock, Value::const_user_iterator > const_pred_iterator
DomTreeNodeBase< BasicBlock > DomTreeNode
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI FunctionPass * createSafepointIRVerifierPass()
Create an instance of the safepoint verifier pass which can be added to a pass pipeline to check for ...
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
const IROptions & getIROptions(const LLVMContext &C)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
@ End
Run the machine verifier once, at the end of the pipeline.