Index: llvm/include/llvm/Transforms/IPO/Attributor.h =================================================================== --- llvm/include/llvm/Transforms/IPO/Attributor.h +++ llvm/include/llvm/Transforms/IPO/Attributor.h @@ -97,8 +97,10 @@ #ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H #define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H +#include "llvm/ADT/GraphTraits.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/SCCIterator.h" +#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SetVector.h" #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/AssumeBundleQueries.h" @@ -116,10 +118,14 @@ #include "llvm/IR/ConstantRange.h" #include "llvm/IR/PassManager.h" #include "llvm/Support/Allocator.h" +#include "llvm/Support/DOTGraphTraits.h" +#include "llvm/Support/GraphWriter.h" #include "llvm/Transforms/Utils/CallGraphUpdater.h" namespace llvm { +struct AADepGraphNode; +struct AADepGraph; struct Attributor; struct AbstractAttribute; struct InformationCache; @@ -144,6 +150,41 @@ }; ///} +/// The data structure for the nodes of a dependency graph +struct AADepGraphNode { +public: + enum NodeKind { NK_AA, NK_SN }; + + AADepGraphNode(NodeKind K) : NKind(K) {} + virtual ~AADepGraphNode(){}; + +private: + const NodeKind NKind; + +protected: + /// Set of dependency graph nodes which this one depends on. + /// The bit encodes if it is optional. + using DepTy = PointerIntPair; + TinyPtrVector Deps; + + static AADepGraphNode *DepGetVal(DepTy &DT) { return DT.getPointer(); } + +public: + using iterator = + mapped_iterator::iterator, decltype(&DepGetVal)>; + + iterator child_begin() { return iterator(Deps.begin(), &DepGetVal); } + iterator child_end() { return iterator(Deps.end(), &DepGetVal); } + + virtual void print(raw_ostream &OS) const { OS << "AADepNode Impl\n"; } + + TinyPtrVector &getDeps() { return Deps; } + + NodeKind getKind() const { return NKind; } + + friend struct Attributor; +}; + /// Helper to describe and deal with positions in the LLVM-IR. /// /// A position in the IR is described by an anchor value and an "offset" that @@ -906,6 +947,8 @@ AAPtr = &AA; AllAbstractAttributes.push_back(&AA); + SymDGN->Deps.push_back( + AADepGraphNode::DepTy(&AA, unsigned(DepClassTy::REQUIRED))); return AA; } @@ -1206,12 +1249,20 @@ bool checkForAllReadWriteInstructions(function_ref Pred, AbstractAttribute &QueryingAA); + /// Print All dependencies for every AbstractAttribute in the AA list + /// For debug use only. + void printAllDependency(raw_ostream &); + /// Return the data layout associated with the anchor scope. const DataLayout &getDataLayout() const { return InfoCache.DL; } /// The allocator used to allocate memory, e.g. for `AbstractAttribute`s. BumpPtrAllocator &Allocator; + AADepGraph *DG; + + AADepGraphNode *SymDGN; + private: /// This method will do fixpoint iteration until fixpoint or the /// maximum iteration count is reached. @@ -1405,6 +1456,8 @@ SmallPtrSet ToBeDeletedFunctions; SmallPtrSet ToBeDeletedBlocks; SmallDenseSet ToBeDeletedInsts; + + friend AADepGraph; ///} }; @@ -1453,6 +1506,33 @@ virtual ChangeStatus indicatePessimisticFixpoint() = 0; }; +/// The data structure for the dependency graph +struct AADepGraph { + AADepGraph(Attributor &A) : A(A) {} + ~AADepGraph() {} + + using DepTy = PointerIntPair; + static AADepGraphNode *DepGetVal(DepTy &DT) { return DT.getPointer(); } + using iterator = + mapped_iterator::iterator, decltype(&DepGetVal)>; + + Attributor &A; + + // FIXME + AADepGraphNode *GetEntryNode() const { return A.SymDGN; } + + iterator begin() { return A.SymDGN->child_begin(); } + iterator end() { return A.SymDGN->child_end(); } + + void viewGraph(); + + /// Dump graph to file + void dumpGraph(); + + /// Print dependency graph + void print(); +}; + /// Simple state with integers encoding. /// /// The interface ensures that the assumed bits are always a subset of the known @@ -1978,10 +2058,11 @@ /// both directions will be added in the future. /// NOTE: The mechanics of adding a new "concrete" abstract attribute are /// described in the file comment. -struct AbstractAttribute : public IRPosition { +struct AbstractAttribute : public IRPosition, public AADepGraphNode { using StateType = AbstractState; - AbstractAttribute(const IRPosition &IRP) : IRPosition(IRP) {} + AbstractAttribute(const IRPosition &IRP) + : IRPosition(IRP), AADepGraphNode(NK_AA) {} /// Virtual destructor. virtual ~AbstractAttribute() {} @@ -2007,12 +2088,20 @@ /// Helper functions, for debug purposes only. ///{ virtual void print(raw_ostream &OS) const; + virtual void printDeps(raw_ostream &OS) const; void dump() const { print(dbgs()); } /// This function should return the "summarized" assumed state as string. virtual const std::string getAsStr() const = 0; + + /// This function should return the corrsponding IR attribute name + virtual const std::string getName() const = 0; ///} + static bool classof(const AADepGraphNode *DGN) { + return DGN->getKind() == NK_AA; + } + /// Allow the Attributor access to the protected methods. friend struct Attributor; @@ -2048,12 +2137,6 @@ /// /// \Return CHANGED if the internal state changed, otherwise UNCHANGED. virtual ChangeStatus updateImpl(Attributor &A) = 0; - -private: - /// Set of abstract attributes which were queried by this one. The bit encodes - /// if there is an optional of required dependence. - using DepTy = PointerIntPair; - TinyPtrVector Deps; }; /// Forward declarations of output streams for debug purposes. @@ -2922,6 +3005,9 @@ return getMemoryLocationsAsStr(getAssumedNotAccessedLocation()); } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAMemoryLocation"; } + /// Unique ID (due to the unique address) static const char ID; }; Index: llvm/lib/Transforms/IPO/Attributor.cpp =================================================================== --- llvm/lib/Transforms/IPO/Attributor.cpp +++ llvm/lib/Transforms/IPO/Attributor.cpp @@ -15,7 +15,10 @@ #include "llvm/Transforms/IPO/Attributor.h" +#include "llvm/ADT/GraphTraits.h" +#include "llvm/ADT/PointerIntPair.h" #include "llvm/ADT/Statistic.h" +#include "llvm/ADT/TinyPtrVector.h" #include "llvm/Analysis/LazyValueInfo.h" #include "llvm/Analysis/MustExecute.h" #include "llvm/Analysis/ValueTracking.h" @@ -23,10 +26,15 @@ #include "llvm/IR/NoFolder.h" #include "llvm/IR/Verifier.h" #include "llvm/InitializePasses.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/GraphWriter.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Transforms/Utils/BasicBlockUtils.h" #include "llvm/Transforms/Utils/Local.h" #include +#include using namespace llvm; @@ -77,6 +85,19 @@ "wrappers for non-exact definitions."), cl::init(false)); +static cl::opt + DumpDepGraph("attributor-dump-dep-graph", cl::Hidden, + cl::desc("Dump the dependency graph to dot files."), + cl::init(false)); + +static cl::opt ViewDepGraph("attributor-view-dep-graph", cl::Hidden, + cl::desc("View the dependency graph."), + cl::init(false)); + +static cl::opt PrintDependencies("attributor-print-dep", cl::Hidden, + cl::desc("Print attribute dependencies"), + cl::init(false)); + /// Logic operators for the change status enum class. /// ///{ @@ -927,7 +948,8 @@ while (!InvalidAA->Deps.empty()) { const auto &Dep = InvalidAA->Deps.back(); InvalidAA->Deps.pop_back(); - AbstractAttribute *DepAA = Dep.getPointer(); + AbstractAttribute *DepAA = + dyn_cast(Dep.getPointer()); if (Dep.getInt() == unsigned(DepClassTy::OPTIONAL)) { Worklist.insert(DepAA); continue; @@ -945,7 +967,8 @@ // changed to the work list. for (AbstractAttribute *ChangedAA : ChangedAAs) while (!ChangedAA->Deps.empty()) { - Worklist.insert(ChangedAA->Deps.back().getPointer()); + Worklist.insert( + dyn_cast(ChangedAA->Deps.back().getPointer())); ChangedAA->Deps.pop_back(); } @@ -1007,7 +1030,8 @@ } while (!ChangedAA->Deps.empty()) { - ChangedAAs.push_back(ChangedAA->Deps.back().getPointer()); + ChangedAAs.push_back( + dyn_cast(ChangedAA->Deps.back().getPointer())); ChangedAA->Deps.pop_back(); } } @@ -1088,155 +1112,154 @@ ChangeStatus Attributor::cleanupIR() { // Delete stuff at the end to avoid invalid references and a nice order. - LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " - << ToBeDeletedFunctions.size() << " functions and " - << ToBeDeletedBlocks.size() << " blocks and " - << ToBeDeletedInsts.size() << " instructions and " - << ToBeChangedUses.size() << " uses\n"); - - SmallVector DeadInsts; - SmallVector TerminatorsToFold; - - for (auto &It : ToBeChangedUses) { - Use *U = It.first; - Value *NewV = It.second; - Value *OldV = U->get(); - - // Do not replace uses in returns if the value is a must-tail call we will - // not delete. - if (isa(U->getUser())) - if (auto *CI = dyn_cast(OldV->stripPointerCasts())) - if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI)) - continue; - - LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() - << " instead of " << *OldV << "\n"); - U->set(NewV); - // Do not modify call instructions outside the SCC. - if (auto *CB = dyn_cast(OldV)) - if (!Functions.count(CB->getCaller())) + LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least " + << ToBeDeletedFunctions.size() << " functions and " + << ToBeDeletedBlocks.size() << " blocks and " + << ToBeDeletedInsts.size() << " instructions and " + << ToBeChangedUses.size() << " uses\n"); + + SmallVector DeadInsts; + SmallVector TerminatorsToFold; + + for (auto &It : ToBeChangedUses) { + Use *U = It.first; + Value *NewV = It.second; + Value *OldV = U->get(); + + // Do not replace uses in returns if the value is a must-tail call we will + // not delete. + if (isa(U->getUser())) + if (auto *CI = dyn_cast(OldV->stripPointerCasts())) + if (CI->isMustTailCall() && !ToBeDeletedInsts.count(CI)) continue; - if (Instruction *I = dyn_cast(OldV)) { - CGModifiedFunctions.insert(I->getFunction()); - if (!isa(I) && !ToBeDeletedInsts.count(I) && - isInstructionTriviallyDead(I)) - DeadInsts.push_back(I); - } - if (isa(NewV) && isa(U->getUser())) { - Instruction *UserI = cast(U->getUser()); - if (isa(NewV)) { - ToBeChangedToUnreachableInsts.insert(UserI); - } else { - TerminatorsToFold.push_back(UserI); - } + + LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser() + << " instead of " << *OldV << "\n"); + U->set(NewV); + // Do not modify call instructions outside the SCC. + if (auto *CB = dyn_cast(OldV)) + if (!Functions.count(CB->getCaller())) + continue; + if (Instruction *I = dyn_cast(OldV)) { + CGModifiedFunctions.insert(I->getFunction()); + if (!isa(I) && !ToBeDeletedInsts.count(I) && + isInstructionTriviallyDead(I)) + DeadInsts.push_back(I); + } + if (isa(NewV) && isa(U->getUser())) { + Instruction *UserI = cast(U->getUser()); + if (isa(NewV)) { + ToBeChangedToUnreachableInsts.insert(UserI); + } else { + TerminatorsToFold.push_back(UserI); } } - for (auto &V : InvokeWithDeadSuccessor) - if (InvokeInst *II = dyn_cast_or_null(V)) { - bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); - bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); - bool Invoke2CallAllowed = - !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction()); - assert((UnwindBBIsDead || NormalBBIsDead) && - "Invoke does not have dead successors!"); - BasicBlock *BB = II->getParent(); - BasicBlock *NormalDestBB = II->getNormalDest(); - if (UnwindBBIsDead) { - Instruction *NormalNextIP = &NormalDestBB->front(); - if (Invoke2CallAllowed) { - changeToCall(II); - NormalNextIP = BB->getTerminator(); - } - if (NormalBBIsDead) - ToBeChangedToUnreachableInsts.insert(NormalNextIP); - } else { - assert(NormalBBIsDead && "Broken invariant!"); - if (!NormalDestBB->getUniquePredecessor()) - NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); - ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); + } + for (auto &V : InvokeWithDeadSuccessor) + if (InvokeInst *II = dyn_cast_or_null(V)) { + bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind); + bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn); + bool Invoke2CallAllowed = + !AAIsDead::mayCatchAsynchronousExceptions(*II->getFunction()); + assert((UnwindBBIsDead || NormalBBIsDead) && + "Invoke does not have dead successors!"); + BasicBlock *BB = II->getParent(); + BasicBlock *NormalDestBB = II->getNormalDest(); + if (UnwindBBIsDead) { + Instruction *NormalNextIP = &NormalDestBB->front(); + if (Invoke2CallAllowed) { + changeToCall(II); + NormalNextIP = BB->getTerminator(); } + if (NormalBBIsDead) + ToBeChangedToUnreachableInsts.insert(NormalNextIP); + } else { + assert(NormalBBIsDead && "Broken invariant!"); + if (!NormalDestBB->getUniquePredecessor()) + NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead"); + ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front()); } - for (Instruction *I : TerminatorsToFold) { + } + for (Instruction *I : TerminatorsToFold) { + CGModifiedFunctions.insert(I->getFunction()); + ConstantFoldTerminator(I->getParent()); + } + for (auto &V : ToBeChangedToUnreachableInsts) + if (Instruction *I = dyn_cast_or_null(V)) { CGModifiedFunctions.insert(I->getFunction()); - ConstantFoldTerminator(I->getParent()); + changeToUnreachable(I, /* UseLLVMTrap */ false); } - for (auto &V : ToBeChangedToUnreachableInsts) - if (Instruction *I = dyn_cast_or_null(V)) { - CGModifiedFunctions.insert(I->getFunction()); - changeToUnreachable(I, /* UseLLVMTrap */ false); - } - for (auto &V : ToBeDeletedInsts) { - if (Instruction *I = dyn_cast_or_null(V)) { - I->dropDroppableUses(); - CGModifiedFunctions.insert(I->getFunction()); - if (!I->getType()->isVoidTy()) - I->replaceAllUsesWith(UndefValue::get(I->getType())); - if (!isa(I) && isInstructionTriviallyDead(I)) - DeadInsts.push_back(I); - else - I->eraseFromParent(); - } + for (auto &V : ToBeDeletedInsts) { + if (Instruction *I = dyn_cast_or_null(V)) { + I->dropDroppableUses(); + CGModifiedFunctions.insert(I->getFunction()); + if (!I->getType()->isVoidTy()) + I->replaceAllUsesWith(UndefValue::get(I->getType())); + if (!isa(I) && isInstructionTriviallyDead(I)) + DeadInsts.push_back(I); + else + I->eraseFromParent(); } + } - RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); + RecursivelyDeleteTriviallyDeadInstructions(DeadInsts); - if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { - SmallVector ToBeDeletedBBs; - ToBeDeletedBBs.reserve(NumDeadBlocks); - for (BasicBlock *BB : ToBeDeletedBlocks) { - CGModifiedFunctions.insert(BB->getParent()); - ToBeDeletedBBs.push_back(BB); - } - // Actually we do not delete the blocks but squash them into a single - // unreachable but untangling branches that jump here is something we need - // to do in a more generic way. - DetatchDeadBlocks(ToBeDeletedBBs, nullptr); + if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) { + SmallVector ToBeDeletedBBs; + ToBeDeletedBBs.reserve(NumDeadBlocks); + for (BasicBlock *BB : ToBeDeletedBlocks) { + CGModifiedFunctions.insert(BB->getParent()); + ToBeDeletedBBs.push_back(BB); } + // Actually we do not delete the blocks but squash them into a single + // unreachable but untangling branches that jump here is something we need + // to do in a more generic way. + DetatchDeadBlocks(ToBeDeletedBBs, nullptr); + } - // Identify dead internal functions and delete them. This happens outside - // the other fixpoint analysis as we might treat potentially dead functions - // as live to lower the number of iterations. If they happen to be dead, the - // below fixpoint loop will identify and eliminate them. - SmallVector InternalFns; - for (Function *F : Functions) - if (F->hasLocalLinkage()) - InternalFns.push_back(F); - - bool FoundDeadFn = true; - while (FoundDeadFn) { - FoundDeadFn = false; - for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { - Function *F = InternalFns[u]; - if (!F) - continue; + // Identify dead internal functions and delete them. This happens outside + // the other fixpoint analysis as we might treat potentially dead functions + // as live to lower the number of iterations. If they happen to be dead, the + // below fixpoint loop will identify and eliminate them. + SmallVector InternalFns; + for (Function *F : Functions) + if (F->hasLocalLinkage()) + InternalFns.push_back(F); + + bool FoundDeadFn = true; + while (FoundDeadFn) { + FoundDeadFn = false; + for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) { + Function *F = InternalFns[u]; + if (!F) + continue; - bool AllCallSitesKnown; - if (!checkForAllCallSites( - [this](AbstractCallSite ACS) { - return ToBeDeletedFunctions.count( - ACS.getInstruction()->getFunction()); - }, - *F, true, nullptr, AllCallSitesKnown)) - continue; + bool AllCallSitesKnown; + if (!checkForAllCallSites( + [this](AbstractCallSite ACS) { + return ToBeDeletedFunctions.count( + ACS.getInstruction()->getFunction()); + }, + *F, true, nullptr, AllCallSitesKnown)) + continue; - ToBeDeletedFunctions.insert(F); - InternalFns[u] = nullptr; - FoundDeadFn = true; - } + ToBeDeletedFunctions.insert(F); + InternalFns[u] = nullptr; + FoundDeadFn = true; } + } // Rewrite the functions as requested during manifest. - ChangeStatus ManifestChange = - rewriteFunctionSignatures(CGModifiedFunctions); + ChangeStatus ManifestChange = rewriteFunctionSignatures(CGModifiedFunctions); - for (Function *Fn : CGModifiedFunctions) - CGUpdater.reanalyzeFunction(*Fn); + for (Function *Fn : CGModifiedFunctions) + CGUpdater.reanalyzeFunction(*Fn); - for (Function *Fn : ToBeDeletedFunctions) - CGUpdater.removeFunction(*Fn); + for (Function *Fn : ToBeDeletedFunctions) + CGUpdater.removeFunction(*Fn); - NumFnDeleted += ToBeDeletedFunctions.size(); + NumFnDeleted += ToBeDeletedFunctions.size(); #ifdef EXPENSIVE_CHECKS for (Function *F : Functions) { @@ -1251,6 +1274,17 @@ ChangeStatus Attributor::run() { runTillFixpoint(); + + // dump graphs on demand + if (DumpDepGraph) + DG->dumpGraph(); + + if (ViewDepGraph) + DG->viewGraph(); + + if (PrintDependencies) + DG->print(); + ChangeStatus ManifestChange = manifestAttributes(); ChangeStatus CleanupChange = cleanupIR(); return ManifestChange | CleanupChange; @@ -2008,8 +2042,31 @@ } void AbstractAttribute::print(raw_ostream &OS) const { - OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState() - << "]"; + OS << "["; + OS << getName(); + OS << "] for CtxI "; + + if (auto *I = getCtxI()) { + OS << "'"; + I->print(OS); + OS << "'"; + } else + OS << "<>"; + + OS << " at position " << getIRPosition() << " with state " << getAsStr() + << '\n'; +} + +void AbstractAttribute::printDeps(raw_ostream &OS) const { + print(OS); + + for (const auto DepAA : Deps) { + auto *AA = DepAA.getPointer(); + OS << " depends on "; + AA->print(OS); + } + + OS << '\n'; } ///} @@ -2031,6 +2088,9 @@ // while we identify default attribute opportunities. Attributor A(Functions, InfoCache, CGUpdater); + A.DG = new AADepGraph(A); + A.SymDGN = new AADepGraphNode(AADepGraphNode::NK_SN); + // Create shallow wrappers for all functions that are not IPO amendable if (AllowShallowWrappers) for (Function *F : Functions) @@ -2044,8 +2104,8 @@ NumFnWithoutExactDefinition++; // We look at internal functions only on-demand but if any use is not a - // direct call or outside the current set of analyzed functions, we have to - // do it eagerly. + // direct call or outside the current set of analyzed functions, we have + // to do it eagerly. if (F->hasLocalLinkage()) { if (llvm::all_of(F->uses(), [&Functions](const Use &U) { const auto *CB = dyn_cast(U.getUser()); @@ -2061,11 +2121,51 @@ } ChangeStatus Changed = A.run(); + LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size() << " functions, result: " << Changed << ".\n"); return Changed == ChangeStatus::CHANGED; } +void Attributor::printAllDependency(raw_ostream &OS) { + for (AbstractAttribute *AA : AllAbstractAttributes) { + AA->printDeps(OS); + } +} + +void AADepGraph::viewGraph() { llvm::ViewGraph(this, "Dependency Graph"); } + +void AADepGraph::dumpGraph() { + static int CallTimes = 0; + std::string Filename = "dot_file_" + std::to_string(CallTimes) + ".dot"; + + errs() << "Dependency graph dump to " << Filename << ".\n"; + + std::error_code EC; + + raw_fd_ostream File(Filename, EC, sys::fs::OF_Text); + if (!EC) + llvm::WriteGraph(File, this); + + CallTimes++; +} + +void AADepGraph::print() { + SmallVector AAs; + AAs.reserve(A.AllAbstractAttributes.size()); + + for (auto tAA : A.AllAbstractAttributes) { + AAs.push_back(tAA); + } + + llvm::sort(AAs, [](AbstractAttribute *LHS, AbstractAttribute *RHS) { + return LHS->getName() < RHS->getName(); + }); + + for (AbstractAttribute *AA : AAs) + AA->printDeps(errs()); +} + PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) { FunctionAnalysisManager &FAM = AM.getResult(M).getManager(); @@ -2112,6 +2212,52 @@ return PreservedAnalyses::all(); } +namespace llvm { + +template <> struct llvm::GraphTraits { + using NodeRef = AADepGraphNode *; + using DepTy = PointerIntPair; + using EdgeRef = PointerIntPair; + + static NodeRef getEntryNode(AADepGraphNode *DGN) { return DGN; } + static NodeRef DepGetVal(DepTy &DT) { return DT.getPointer(); } + + using ChildIteratorType = + mapped_iterator::iterator, decltype(&DepGetVal)>; + using ChildEdgeIteratorType = TinyPtrVector::iterator; + + static ChildIteratorType child_begin(NodeRef N) { return N->child_begin(); } + + static ChildIteratorType child_end(NodeRef N) { return N->child_end(); } +}; + +template <> +struct llvm::GraphTraits : public GraphTraits { + static NodeRef getEntryNode(AADepGraph *DG) { return DG->GetEntryNode(); } + + using nodes_iterator = + mapped_iterator::iterator, decltype(&DepGetVal)>; + + static nodes_iterator nodes_begin(AADepGraph *DG) { return DG->begin(); } + + static nodes_iterator nodes_end(AADepGraph *DG) { return DG->end(); } +}; + +template <> +struct llvm::DOTGraphTraits : public DefaultDOTGraphTraits { + DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} + + static std::string getNodeLabel(const AADepGraphNode *Node, + const AADepGraph *DG) { + std::string AAString = ""; + raw_string_ostream O(AAString); + Node->print(O); + return AAString; + } +}; + +} // end namespace llvm + namespace { struct AttributorLegacyPass : public ModulePass { Index: llvm/lib/Transforms/IPO/AttributorAttributes.cpp =================================================================== --- llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -678,6 +678,9 @@ return getAssumed() ? "nounwind" : "may-unwind"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoUnwind"; } + /// See AbstractAttribute::updateImpl(...). ChangeStatus updateImpl(Attributor &A) override { auto Opcodes = { @@ -851,6 +854,9 @@ /// Pretty print the attribute similar to the IR representation. const std::string getAsStr() const override; + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAReturnedValues"; } + /// See AbstractState::isAtFixpoint(). bool isAtFixpoint() const override { return IsFixed; } @@ -1051,9 +1057,10 @@ // map, NewRVsMap. decltype(ReturnedValues) NewRVsMap; - auto HandleReturnValue = [&](Value *RV, SmallSetVector &RIs) { - LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *RV - << " by #" << RIs.size() << " RIs\n"); + auto HandleReturnValue = [&](Value *RV, + SmallSetVector &RIs) { + LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *RV << " by #" + << RIs.size() << " RIs\n"); CallBase *CB = dyn_cast(RV); if (!CB || UnresolvedCalls.count(CB)) return; @@ -1208,6 +1215,9 @@ return getAssumed() ? "nosync" : "may-sync"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoSync"; } + /// See AbstractAttribute::updateImpl(...). ChangeStatus updateImpl(Attributor &A) override; @@ -1419,6 +1429,9 @@ const std::string getAsStr() const override { return getAssumed() ? "nofree" : "may-free"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoFree"; } }; struct AANoFreeFunction final : public AANoFreeImpl { @@ -1704,6 +1717,9 @@ return getAssumed() ? "nonnull" : "may-null"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANonNull"; } + /// Flag to determine if the underlying value can be null and still allow /// valid accesses. const bool NullIsDefined; @@ -1807,6 +1823,9 @@ const std::string getAsStr() const override { return getAssumed() ? "norecurse" : "may-recurse"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoRecurse"; } }; struct AANoRecurseFunction final : AANoRecurseImpl { @@ -2033,6 +2052,9 @@ return getAssumed() ? "undefined-behavior" : "no-ub"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAUndefinedBehavior"; } + /// Note: The correctness of this analysis depends on the fact that the /// following 2 sets will stop changing after some point. /// "Change" here means that their size changes. @@ -2179,6 +2201,9 @@ const std::string getAsStr() const override { return getAssumed() ? "willreturn" : "may-noreturn"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAWillReturn"; } }; struct AAWillReturnFunction final : AAWillReturnImpl { @@ -2231,6 +2256,9 @@ return "reachable"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAReachability"; } + /// See AbstractAttribute::initialize(...). void initialize(Attributor &A) override { indicatePessimisticFixpoint(); } @@ -2259,6 +2287,9 @@ const std::string getAsStr() const override { return getAssumed() ? "noalias" : "may-alias"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoAlias"; } }; /// NoAlias attribute for a floating value. @@ -2647,6 +2678,9 @@ return isAssumedDead() ? "assumed-dead" : "assumed-live"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAIsDeadValue"; } + /// Check if all uses are assumed dead. bool areAllUsesAssumedDead(Attributor &A, Value &V) { auto UsePred = [&](const Use &U, bool &Follow) { return false; }; @@ -2866,6 +2900,11 @@ : (getAssumed() ? "assumed-dead-users" : "assumed-live"); } + /// See AbstractAttribute::getName() + const std::string getName() const override { + return "AAIsDeadCallSiteReturned"; + } + private: bool IsAssumedSideEffectFree; }; @@ -2933,6 +2972,9 @@ std::to_string(KnownDeadEnds.size()) + "]"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAIsDeadFunction"; } + /// See AbstractAttribute::manifest(...). ChangeStatus manifest(Attributor &A) override { assert(getState().isValidState() && @@ -3390,6 +3432,9 @@ std::to_string(getKnownDereferenceableBytes()) + "-" + std::to_string(getAssumedDereferenceableBytes()) + ">"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AADereferenceable"; } }; /// Dereferenceable attribute for a floating value. @@ -3424,7 +3469,6 @@ T.GlobalState &= DS.GlobalState; } - // For now we do not try to "increase" dereferenceability due to negative // indices as we first have to come up with code to deal with loops and // for overflows of the dereferenceable bytes. @@ -3682,6 +3726,9 @@ "-" + std::to_string(getAssumedAlign()) + ">") : "unknown-align"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAAlign"; } }; /// Align attribute for a floating value. @@ -3826,6 +3873,9 @@ return getAssumed() ? "noreturn" : "may-return"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoReturn"; } + /// See AbstractAttribute::updateImpl(Attributor &A). virtual ChangeStatus updateImpl(Attributor &A) override { auto CheckForNoReturn = [](Instruction &) { return false; }; @@ -3975,6 +4025,9 @@ return "assumed not-captured-maybe-returned"; return "assumed-captured"; } + + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoCapture"; } }; /// Attributor-aware capture tracker. @@ -4316,6 +4369,9 @@ : "not-simple"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAValueSimplify"; } + /// See AbstractAttribute::trackStatistics() void trackStatistics() const override {} @@ -4683,6 +4739,9 @@ return "[H2S] Mallocs: " + std::to_string(MallocCalls.size()); } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAHeapToStack"; } + ChangeStatus manifest(Attributor &A) override { assert(getState().isValidState() && "Attempted to manifest an invalid state!"); @@ -4956,6 +5015,9 @@ return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAPrivatizablePtr"; } + protected: Optional PrivatizableType; }; @@ -5594,6 +5656,9 @@ return "may-read/write"; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAMemoryBehavior"; } + /// The set of IR attributes AAMemoryBehavior deals with. static const Attribute::AttrKind AttrKinds[3]; }; @@ -6574,6 +6639,9 @@ return OS.str(); } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAValueConstantRange"; } + /// Helper function to get a SCEV expr for the associated value at program /// point \p I. const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const { Index: llvm/test/Transforms/Attributor/depgraph.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/Attributor/depgraph.ll @@ -0,0 +1,130 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_NPM,NOT_CGSCC_OPM,NOT_TUNIT_NPM,IS__TUNIT____,IS________OPM,IS__TUNIT_OPM +; RUN: opt -aa-pipeline=basic-aa -passes=attributor -attributor-manifest-internal -attributor-max-iterations-verify -attributor-annotate-decl-cs -attributor-max-iterations=3 -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_CGSCC_OPM,NOT_CGSCC_NPM,NOT_TUNIT_OPM,IS__TUNIT____,IS________NPM,IS__TUNIT_NPM +; RUN: opt -attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_NPM,IS__CGSCC____,IS________OPM,IS__CGSCC_OPM +; RUN: opt -aa-pipeline=basic-aa -passes=attributor-cgscc -attributor-manifest-internal -attributor-annotate-decl-cs -S < %s | FileCheck %s --check-prefixes=CHECK,NOT_TUNIT_NPM,NOT_TUNIT_OPM,NOT_CGSCC_OPM,IS__CGSCC____,IS________NPM,IS__CGSCC_NPM +; RUN: opt -passes=attributor-cgscc -disable-output -attributor-print-dep < %s 2>&1 | FileCheck %s + +; Test 0 +; +; test copied from the attributor introduction video: checkAndAdvance(), and the C code is: +; int *checkAndAdvance(int * __attribute__((aligned(16))) p) { +; if (*p == 0) +; return checkAndAdvance(p + 4); +; return p; +; } +; +define i32* @checkAndAdvance(i32* align 16 %0) { + %2 = load i32, i32* %0, align 4 + %3 = icmp eq i32 %2, 0 + br i1 %3, label %4, label %7 + +4: ; preds = %1 + %5 = getelementptr inbounds i32, i32* %0, i64 4 + %6 = call i32* @checkAndAdvance(i32* %5) + br label %8 + +7: ; preds = %1 + br label %8 + +8: ; preds = %7, %4 + %.0 = phi i32* [ %6, %4 ], [ %0, %7 ] + ret i32* %.0 +} + +; +; AAAlign +; + +; CHECK: [AAAlign] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; CHECK: depends on [AAAlign] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AAAlign] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AAAlign] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; CHECK: depends on [AAAlign] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} + +; CHECK: [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} + +; CHECK: [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} + +; CHECK: [AAMemoryLocation] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AAMemoryLocation] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AAMemoryLocation] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} + +; CHECK: [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} + +; CHECK: [AANoCapture] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} + +; CHECK: [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} + +; CHECK: [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} + +; CHECK: [AANoSync] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AANoSync] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoSync] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} + +; CHECK: [AANoSync] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoSync] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AAIsDeadCallSiteReturned] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; CHECK: depends on [AAIsDeadCallSiteReturned] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; CHECK: depends on [AANoUnwind] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AANoUnwind] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; CHECK: depends on [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} + +; CHECK: [AANonNull] for CtxI ' %5 = getelementptr inbounds i32, i32* %0, i64 4' at position {flt: [@-1]} +; CHECK: depends on [AANonNull] for CtxI ' %5 = getelementptr inbounds i32, i32* %0, i64 4' at position {flt: [@-1]} +; CHECK: depends on [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; CHECK: depends on [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; CHECK: depends on [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} + +; CHECK: [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} +; CHECK: depends on [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; CHECK: depends on [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]}