51#define DEBUG_TYPE "safepoint-ir-verifier"
77 auto &PU = PredIt.
getUse();
83 bool hasLiveIncomingEdge(
const PHINode *PN,
const BasicBlock *InBB)
const {
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 {
106 "edge must be operand of terminator");
108 "edge must refer to basic block");
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))
129 for (
const BasicBlock &BB :
F)
130 if (!DT.isReachableFromEntry(&BB))
131 DeadBlocks.insert(&BB);
134 ReversePostOrderTraversal<const Function *> RPOT(&
F);
135 for (
const BasicBlock *BB : RPOT) {
137 assert(TI &&
"blocks must be well formed");
158 void addDeadBlock(
const BasicBlock *BB) {
162 while (!NewDead.
empty()) {
168 SmallVector<BasicBlock *, 8> Dom;
169 DT->getDescendants(
const_cast<BasicBlock*
>(
D), Dom);
173 DeadBlocks.insert_range(Dom);
176 for (BasicBlock *
B : Dom)
178 if (!isDeadBlock(S) && !hasLiveIncomingEdges(S))
183 void addDeadEdge(
const Use &DeadEdge) {
184 if (!DeadEdges.insert(&DeadEdge))
188 if (hasLiveIncomingEdges(BB))
197 const CFGDeadness &CD);
204 CD.processFunction(
F, DT);
219 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
221 CD.processFunction(
F, DT);
226 void getAnalysisUsage(AnalysisUsage &AU)
const override {
231 StringRef getPassName()
const override {
return "safepoint verifier"; }
236 SafepointIRVerifier
pass;
237 pass.runOnFunction(
F);
240char SafepointIRVerifier::ID = 0;
243 return new SafepointIRVerifier();
247 "Safepoint IR Verifier",
false,
false)
257 return (1 == PT->getAddressSpace());
274template<
typename IteratorTy>
277 while (Begin != End) {
278 OS << **Begin <<
" ";
293struct BasicBlockState {
306 bool Cleared =
false;
331 bool isExclusivelyDerivedFromNull =
true;
336 while(!Worklist.
empty()) {
338 if (!Visited.
insert(V).second)
342 Worklist.
push_back(CI->stripPointerCasts());
364 Worklist.
push_back(GCRelocate->getDerivedPtr());
376 isExclusivelyDerivedFromNull =
false;
396class InstructionVerifier;
456 const CFGDeadness &CD;
457 SpecificBumpPtrAllocator<BasicBlockState> BSAllocator;
458 DenseMap<const BasicBlock *, BasicBlockState *> BlockMap;
461 DenseSet<const Instruction *> ValidUnrelocatedDefs;
464 DenseSet<const Value *> PoisonedDefs;
467 GCPtrTracker(
const Function &F,
const DominatorTree &DT,
468 const CFGDeadness &CD);
470 bool hasLiveIncomingEdge(
const PHINode *PN,
const BasicBlock *InBB)
const {
471 return CD.hasLiveIncomingEdge(PN, InBB);
474 BasicBlockState *getBasicBlockState(
const BasicBlock *BB);
475 const BasicBlockState *getBasicBlockState(
const BasicBlock *BB)
const;
477 bool isValuePoisoned(
const Value *V)
const {
return PoisonedDefs.count(V); }
489 bool isMapped(
const BasicBlock *BB)
const {
return BlockMap.contains(BB); }
493 bool instructionMayBeSkipped(
const Instruction *
I)
const;
497 void recalculateBBsStates();
505 bool removeValidUnrelocatedDefs(
const BasicBlock *BB,
506 const BasicBlockState *BBS,
513 const DominatorTree &DT);
520 static void transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
521 bool ContributionChanged);
524 static void transferInstruction(
const Instruction &
I,
bool &Cleared,
531class InstructionVerifier {
532 bool AnyInvalidUses =
false;
535 void verifyInstruction(
const GCPtrTracker *Tracker,
const Instruction &
I,
538 bool hasAnyInvalidUses()
const {
return AnyInvalidUses; }
541 void reportInvalidUse(
const Value &V,
const Instruction &
I);
546 const CFGDeadness &CD) :
F(
F), CD(CD) {
550 if (!CD.isDeadBlock(&BB)) {
551 BasicBlockState *BBS = new (BSAllocator.Allocate()) BasicBlockState;
552 for (const auto &I : BB)
553 transferInstruction(I, BBS->Cleared, BBS->Contribution);
559 for (
auto &BBI : BlockMap) {
560 gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
561 transferBlock(BBI.first, *BBI.second,
true);
567 recalculateBBsStates();
570BasicBlockState *GCPtrTracker::getBasicBlockState(
const BasicBlock *BB) {
571 return BlockMap.
lookup(BB);
574const BasicBlockState *GCPtrTracker::getBasicBlockState(
575 const BasicBlock *BB)
const {
576 return const_cast<GCPtrTracker *
>(
this)->getBasicBlockState(BB);
579bool GCPtrTracker::instructionMayBeSkipped(
const Instruction *
I)
const {
582 return ValidUnrelocatedDefs.
count(
I) || PoisonedDefs.
count(
I);
585void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
589 ReversePostOrderTraversal<const Function *> RPOT(&Tracker.F);
590 for (
const BasicBlock *BB : RPOT) {
591 BasicBlockState *BBS = Tracker.getBasicBlockState(BB);
598 for (
const Instruction &
I : *BB) {
599 if (Tracker.instructionMayBeSkipped(&
I))
602 Verifier.verifyInstruction(&Tracker,
I, AvailableSet);
606 bool Cleared =
false;
607 transferInstruction(
I, Cleared, AvailableSet);
613void GCPtrTracker::recalculateBBsStates() {
621 while (!Worklist.empty()) {
622 const BasicBlock *BB = Worklist.pop_back_val();
623 BasicBlockState *BBS = getBasicBlockState(BB);
627 size_t OldInCount = BBS->AvailableIn.
size();
630 BasicBlockState *PBBS = getBasicBlockState(PBB);
631 if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
635 assert(OldInCount >= BBS->AvailableIn.
size() &&
"invariant!");
637 bool InputsChanged = OldInCount != BBS->AvailableIn.
size();
638 bool ContributionChanged =
639 removeValidUnrelocatedDefs(BB, BBS, BBS->Contribution);
640 if (!InputsChanged && !ContributionChanged)
643 size_t OldOutCount = BBS->AvailableOut.
size();
644 transferBlock(BB, *BBS, ContributionChanged);
645 if (OldOutCount != BBS->AvailableOut.
size()) {
646 assert(OldOutCount > BBS->AvailableOut.
size() &&
"invariant!");
652bool GCPtrTracker::removeValidUnrelocatedDefs(
const BasicBlock *BB,
653 const BasicBlockState *BBS,
655 assert(&BBS->Contribution == &Contribution &&
656 "Passed Contribution should be from the passed BasicBlockState!");
658 bool ContributionChanged =
false;
661 for (
const Instruction &
I : *BB) {
662 bool ValidUnrelocatedPointerDef =
false;
663 bool PoisonedPointerDef =
false;
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;
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,
742 const DominatorTree &DT) {
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");
750 const auto &Defs = BBS->Contribution;
751 Result.insert_range(Defs);
765void GCPtrTracker::transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
766 bool ContributionChanged) {
772 if (ContributionChanged)
773 AvailableOut = BBS.Contribution;
779 AvailableOut = std::move(Temp);
789void GCPtrTracker::transferInstruction(
const Instruction &
I,
bool &Cleared,
798void InstructionVerifier::verifyInstruction(
799 const GCPtrTracker *Tracker,
const Instruction &
I,
805 const BasicBlockState *InBBS = Tracker->getBasicBlockState(InBB);
807 !Tracker->hasLiveIncomingEdge(PN, InBB))
813 !InBBS->AvailableOut.
count(InValue))
814 reportInvalidUse(*InValue, *PN);
824 auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
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,
881 const Instruction &
I) {
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);
902 InstructionVerifier Verifier;
903 GCPtrTracker::verifyFunction(std::move(Tracker), Verifier);
905 if (
PrintOnly && !Verifier.hasAnyInvalidUses()) {
906 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.
global merge Global merge function 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
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)
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 if the block is well formed or null if the block is not well forme...
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
static LLVM_ABI 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...
Implements a dense probed hash-table based set.
DomTreeNodeBase * getIDom() const
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
FunctionPass class - This class is used to implement most global optimizations.
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 LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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.
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ BasicBlock
Various leaf nodes.
initializer< Ty > init(const Ty &Val)
NodeAddr< UseNode * > Use
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
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)
void verifySafepointIR(Function &F)
Run the safepoint verifier over a single function. Crashes on failure.
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.
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.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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.
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.
LLVM_ABI void initializeSafepointIRVerifierPass(PassRegistry &)