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);
203 CD.processFunction(
F, DT);
217 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
219 CD.processFunction(
F, DT);
224 void getAnalysisUsage(AnalysisUsage &AU)
const override {
229 StringRef getPassName()
const override {
return "safepoint verifier"; }
234 SafepointIRVerifier
pass;
235 pass.runOnFunction(
F);
238char SafepointIRVerifier::ID = 0;
241 return new SafepointIRVerifier();
245 "Safepoint IR Verifier",
false,
false)
255 return (1 == PT->getAddressSpace());
272template<
typename IteratorTy>
275 while (Begin != End) {
276 OS << **Begin <<
" ";
291struct BasicBlockState {
304 bool Cleared =
false;
329 bool isExclusivelyDerivedFromNull =
true;
334 while(!Worklist.
empty()) {
336 if (!Visited.
insert(V).second)
340 Worklist.
push_back(CI->stripPointerCasts());
362 Worklist.
push_back(GCRelocate->getDerivedPtr());
374 isExclusivelyDerivedFromNull =
false;
394class InstructionVerifier;
454 const CFGDeadness &CD;
455 SpecificBumpPtrAllocator<BasicBlockState> BSAllocator;
456 DenseMap<const BasicBlock *, BasicBlockState *> BlockMap;
459 DenseSet<const Instruction *> ValidUnrelocatedDefs;
462 DenseSet<const Value *> PoisonedDefs;
465 GCPtrTracker(
const Function &
F,
const DominatorTree &DT,
466 const CFGDeadness &CD);
468 bool hasLiveIncomingEdge(
const PHINode *PN,
const BasicBlock *InBB)
const {
469 return CD.hasLiveIncomingEdge(PN, InBB);
472 BasicBlockState *getBasicBlockState(
const BasicBlock *BB);
473 const BasicBlockState *getBasicBlockState(
const BasicBlock *BB)
const;
475 bool isValuePoisoned(
const Value *V)
const {
return PoisonedDefs.
count(V); }
487 bool isMapped(
const BasicBlock *BB)
const {
return BlockMap.
contains(BB); }
491 bool instructionMayBeSkipped(
const Instruction *
I)
const;
495 void recalculateBBsStates();
503 bool removeValidUnrelocatedDefs(
const BasicBlock *BB,
504 const BasicBlockState *BBS,
511 const DominatorTree &DT);
518 static void transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
519 bool ContributionChanged);
522 static void transferInstruction(
const Instruction &
I,
bool &Cleared,
529class InstructionVerifier {
530 bool AnyInvalidUses =
false;
533 void verifyInstruction(
const GCPtrTracker *Tracker,
const Instruction &
I,
536 bool hasAnyInvalidUses()
const {
return AnyInvalidUses; }
539 void reportInvalidUse(
const Value &V,
const Instruction &
I);
544 const CFGDeadness &CD) :
F(
F), CD(CD) {
548 if (!CD.isDeadBlock(&BB)) {
549 BasicBlockState *BBS = new (BSAllocator.Allocate()) BasicBlockState;
550 for (const auto &I : BB)
551 transferInstruction(I, BBS->Cleared, BBS->Contribution);
557 for (
auto &BBI : BlockMap) {
558 gatherDominatingDefs(BBI.first, BBI.second->AvailableIn, DT);
559 transferBlock(BBI.first, *BBI.second,
true);
565 recalculateBBsStates();
568BasicBlockState *GCPtrTracker::getBasicBlockState(
const BasicBlock *BB) {
569 return BlockMap.
lookup(BB);
572const BasicBlockState *GCPtrTracker::getBasicBlockState(
573 const BasicBlock *BB)
const {
574 return const_cast<GCPtrTracker *
>(
this)->getBasicBlockState(BB);
577bool GCPtrTracker::instructionMayBeSkipped(
const Instruction *
I)
const {
580 return ValidUnrelocatedDefs.
count(
I) || PoisonedDefs.
count(
I);
583void GCPtrTracker::verifyFunction(GCPtrTracker &&Tracker,
587 ReversePostOrderTraversal<const Function *> RPOT(&Tracker.F);
588 for (
const BasicBlock *BB : RPOT) {
589 BasicBlockState *BBS = Tracker.getBasicBlockState(BB);
596 for (
const Instruction &
I : *BB) {
597 if (Tracker.instructionMayBeSkipped(&
I))
600 Verifier.verifyInstruction(&Tracker,
I, AvailableSet);
604 bool Cleared =
false;
605 transferInstruction(
I, Cleared, AvailableSet);
611void GCPtrTracker::recalculateBBsStates() {
619 while (!Worklist.empty()) {
620 const BasicBlock *BB = Worklist.pop_back_val();
621 BasicBlockState *BBS = getBasicBlockState(BB);
625 size_t OldInCount = BBS->AvailableIn.
size();
628 BasicBlockState *PBBS = getBasicBlockState(PBB);
629 if (PBBS && !CD.isDeadEdge(&CFGDeadness::getEdge(PredIt)))
633 assert(OldInCount >= BBS->AvailableIn.
size() &&
"invariant!");
635 bool InputsChanged = OldInCount != BBS->AvailableIn.
size();
636 bool ContributionChanged =
637 removeValidUnrelocatedDefs(BB, BBS, BBS->Contribution);
638 if (!InputsChanged && !ContributionChanged)
641 size_t OldOutCount = BBS->AvailableOut.
size();
642 transferBlock(BB, *BBS, ContributionChanged);
643 if (OldOutCount != BBS->AvailableOut.
size()) {
644 assert(OldOutCount > BBS->AvailableOut.
size() &&
"invariant!");
650bool GCPtrTracker::removeValidUnrelocatedDefs(
const BasicBlock *BB,
651 const BasicBlockState *BBS,
653 assert(&BBS->Contribution == &Contribution &&
654 "Passed Contribution should be from the passed BasicBlockState!");
656 bool ContributionChanged =
false;
659 for (
const Instruction &
I : *BB) {
660 bool ValidUnrelocatedPointerDef =
false;
661 bool PoisonedPointerDef =
false;
666 bool HasRelocatedInputs =
false;
667 bool HasUnrelocatedInputs =
false;
670 if (!isMapped(InBB) ||
671 !CD.hasLiveIncomingEdge(PN, InBB))
677 if (isValuePoisoned(InValue)) {
679 HasRelocatedInputs =
true;
680 HasUnrelocatedInputs =
true;
683 if (BlockMap[InBB]->AvailableOut.count(InValue))
684 HasRelocatedInputs =
true;
686 HasUnrelocatedInputs =
true;
689 if (HasUnrelocatedInputs) {
690 if (HasRelocatedInputs)
691 PoisonedPointerDef =
true;
693 ValidUnrelocatedPointerDef =
true;
700 for (
const Value *V :
I.operands())
703 if (isValuePoisoned(V))
704 PoisonedPointerDef =
true;
706 ValidUnrelocatedPointerDef =
true;
710 assert(!(ValidUnrelocatedPointerDef && PoisonedPointerDef) &&
711 "Value cannot be both unrelocated and poisoned!");
712 if (ValidUnrelocatedPointerDef) {
717 ValidUnrelocatedDefs.
insert(&
I);
719 <<
" from Contribution of " << BB->getName() <<
"\n");
720 ContributionChanged =
true;
721 }
else if (PoisonedPointerDef) {
726 LLVM_DEBUG(
dbgs() <<
"Removing poisoned " <<
I <<
" from Contribution of "
727 << BB->getName() <<
"\n");
728 ContributionChanged =
true;
730 bool Cleared =
false;
731 transferInstruction(
I, Cleared, AvailableSet);
735 return ContributionChanged;
738void GCPtrTracker::gatherDominatingDefs(
const BasicBlock *BB,
740 const DominatorTree &DT) {
743 assert(DTN &&
"Unreachable blocks are ignored");
746 auto BBS = getBasicBlockState(DTN->
getBlock());
747 assert(BBS &&
"immediate dominator cannot be dead for a live block");
748 const auto &Defs = BBS->Contribution;
749 Result.insert_range(Defs);
763void GCPtrTracker::transferBlock(
const BasicBlock *BB, BasicBlockState &BBS,
764 bool ContributionChanged) {
770 if (ContributionChanged)
771 AvailableOut = BBS.Contribution;
777 AvailableOut = std::move(Temp);
787void GCPtrTracker::transferInstruction(
const Instruction &
I,
bool &Cleared,
796void InstructionVerifier::verifyInstruction(
797 const GCPtrTracker *Tracker,
const Instruction &
I,
803 const BasicBlockState *InBBS = Tracker->getBasicBlockState(InBB);
805 !Tracker->hasLiveIncomingEdge(PN, InBB))
811 !InBBS->AvailableOut.
count(InValue))
812 reportInvalidUse(*InValue, *PN);
822 auto hasValidUnrelocatedUse = [&AvailableSet, Tracker, baseTyLHS, baseTyRHS,
862 if (!hasValidUnrelocatedUse()) {
866 reportInvalidUse(*
LHS,
I);
868 reportInvalidUse(*
RHS,
I);
871 for (
const Value *V :
I.operands())
874 reportInvalidUse(*V,
I);
878void InstructionVerifier::reportInvalidUse(
const Value &V,
879 const Instruction &
I) {
880 errs() <<
"Illegal use of unrelocated value found!\n";
881 errs() <<
"Def: " <<
V <<
"\n";
882 errs() <<
"Use: " <<
I <<
"\n";
885 AnyInvalidUses =
true;
889 const CFGDeadness &CD) {
890 LLVM_DEBUG(
dbgs() <<
"Verifying gc pointers in function: " <<
F.getName()
893 dbgs() <<
"Verifying gc pointers in function: " <<
F.getName() <<
"\n";
895 GCPtrTracker Tracker(
F, DT, CD);
900 InstructionVerifier Verifier;
901 GCPtrTracker::verifyFunction(std::move(Tracker), Verifier);
903 if (
PrintOnly && !Verifier.hasAnyInvalidUses()) {
904 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...
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.
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 &)