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,43 @@ }; ///} +/// The data structure for the nodes of a dependency graph +struct AADepGraphNode { +public: + virtual ~AADepGraphNode(){}; + +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(); } + static AbstractAttribute *DepGetValAA(DepTy &DT) { + return dyn_cast(DT.getPointer()); + } + + operator AbstractAttribute *() { return dyn_cast(this); } + +public: + using iterator = + mapped_iterator::iterator, decltype(&DepGetVal)>; + using aaiterator = + mapped_iterator::iterator, decltype(&DepGetValAA)>; + + aaiterator begin() { return aaiterator(Deps.begin(), &DepGetValAA); } + aaiterator end() { return aaiterator(Deps.end(), &DepGetValAA); } + 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; } + + friend struct Attributor; + friend struct AADepGraph; +}; + /// 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 @@ -987,7 +1030,8 @@ assert(!AAPtr && "Attribute already in map!"); AAPtr = &AA; - AllAbstractAttributes.push_back(&AA); + SymDGN->Deps.push_back( + AADepGraphNode::DepTy(&AA, unsigned(DepClassTy::REQUIRED))); return AA; } @@ -1288,12 +1332,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. @@ -1408,6 +1460,8 @@ SmallPtrSet ToBeDeletedFunctions; SmallPtrSet ToBeDeletedBlocks; SmallDenseSet ToBeDeletedInsts; + + friend AADepGraph; ///} }; @@ -1456,6 +1510,32 @@ 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; + + 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 @@ -1981,7 +2061,7 @@ /// 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) {} @@ -2010,12 +2090,18 @@ /// 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 true; } + /// Allow the Attributor access to the protected methods. friend struct Attributor; @@ -2051,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. @@ -2128,6 +2208,9 @@ static AAReturnedValues &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAReturnedValues"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2146,6 +2229,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoUnwind &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoUnwind"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2164,6 +2250,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoSync &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoSync"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2183,6 +2272,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANonNull &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANonNull"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2202,6 +2294,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoRecurse &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoRecurse"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2221,6 +2316,9 @@ /// Create an abstract attribute view for the position \p IRP. static AAWillReturn &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAWillReturn"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2247,6 +2345,9 @@ static AAUndefinedBehavior &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAUndefinedBehavior"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2275,6 +2376,9 @@ static AAReachability &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAReachability"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2294,6 +2398,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoAlias &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoAlias"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2313,6 +2420,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoFree &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoFree"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2332,6 +2442,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoReturn &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoReturn"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2386,6 +2499,9 @@ return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F); } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAIsDead"; } + /// Unique ID (due to the unique address) static const char ID; @@ -2576,6 +2692,9 @@ static AADereferenceable &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AADereferenceable"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2594,6 +2713,9 @@ /// Return known alignment. unsigned getKnownAlign() const { return getKnown(); } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAAlign"; } + /// Create an abstract attribute view for the position \p IRP. static AAAlign &createForPosition(const IRPosition &IRP, Attributor &A); @@ -2648,6 +2770,9 @@ /// Create an abstract attribute view for the position \p IRP. static AANoCapture &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AANoCapture"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2666,6 +2791,9 @@ static AAValueSimplify &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAValueSimplify"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2683,6 +2811,9 @@ /// Create an abstract attribute view for the position \p IRP. static AAHeapToStack &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAHeapToStack"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2716,6 +2847,9 @@ static AAPrivatizablePtr &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAPrivatizablePtr"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2767,6 +2901,9 @@ static AAMemoryBehavior &createForPosition(const IRPosition &IRP, Attributor &A); + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAMemoryBehavior"; } + /// Unique ID (due to the unique address) static const char ID; }; @@ -2925,6 +3062,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; }; @@ -2970,6 +3110,9 @@ return nullptr; } + /// See AbstractAttribute::getName() + const std::string getName() const override { return "AAValueConstantRange"; } + /// 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" @@ -24,10 +27,15 @@ #include "llvm/IR/Verifier.h" #include "llvm/InitializePasses.h" #include "llvm/Support/CommandLine.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; @@ -78,6 +86,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. /// ///{ @@ -491,8 +512,10 @@ Attributor::~Attributor() { // The abstract attributes are allocated via the BumpPtrAllocator Allocator, // thus we cannot delete them. We can, and want to, destruct them though. - for (AbstractAttribute *AA : AllAbstractAttributes) + for (auto depAA : SymDGN->Deps) { + AbstractAttribute *AA = dyn_cast(depAA.getPointer()); AA->~AbstractAttribute(); + } } bool Attributor::isAssumedDead(const AbstractAttribute &AA, @@ -897,8 +920,7 @@ void Attributor::runTillFixpoint() { LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized " - << AllAbstractAttributes.size() - << " abstract attributes.\n"); + << SymDGN->Deps.size() << " abstract attributes.\n"); // Now that all abstract attributes are collected and initialized we start // the abstract analysis. @@ -907,11 +929,11 @@ SmallVector ChangedAAs; SetVector Worklist, InvalidAAs; - Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end()); + Worklist.insert(SymDGN->begin(), SymDGN->end()); do { // Remember the size to determine new attributes. - size_t NumAAs = AllAbstractAttributes.size(); + size_t NumAAs = SymDGN->Deps.size(); LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter << ", Worklist size: " << Worklist.size() << "\n"); @@ -928,7 +950,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; @@ -946,7 +969,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(); } @@ -974,8 +998,7 @@ // Add attributes to the changed set if they have been created in the last // iteration. - ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs, - AllAbstractAttributes.end()); + ChangedAAs.append(SymDGN->begin() + NumAAs, SymDGN->end()); // Reset the work list and repopulate with the changed abstract attributes. // Note that dependent ones are added above. @@ -1008,7 +1031,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(); } } @@ -1030,12 +1054,13 @@ } ChangeStatus Attributor::manifestAttributes() { - size_t NumFinalAAs = AllAbstractAttributes.size(); + size_t NumFinalAAs = SymDGN->Deps.size(); unsigned NumManifested = 0; unsigned NumAtFixpoint = 0; ChangeStatus ManifestChange = ChangeStatus::UNCHANGED; - for (AbstractAttribute *AA : AllAbstractAttributes) { + for (auto depAA : SymDGN->Deps) { + AbstractAttribute *AA = dyn_cast(depAA.getPointer()); AbstractState &State = AA->getState(); // If there is not already a fixpoint reached, we can now take the @@ -1075,11 +1100,14 @@ NumAttributesValidFixpoint += NumAtFixpoint; (void)NumFinalAAs; - if (NumFinalAAs != AllAbstractAttributes.size()) { - for (unsigned u = NumFinalAAs; u < AllAbstractAttributes.size(); ++u) - errs() << "Unexpected abstract attribute: " << *AllAbstractAttributes[u] + if (NumFinalAAs != SymDGN->Deps.size()) { + for (unsigned u = NumFinalAAs; u < SymDGN->Deps.size(); ++u) + errs() << "Unexpected abstract attribute: " + << dyn_cast(SymDGN->Deps[u].getPointer()) << " :: " - << AllAbstractAttributes[u]->getIRPosition().getAssociatedValue() + << dyn_cast(SymDGN->Deps[u].getPointer()) + ->getIRPosition() + .getAssociatedValue() << "\n"; llvm_unreachable("Expected the final number of abstract attributes to " "remain unchanged!"); @@ -1251,6 +1279,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 +2047,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 << " updates "; + AA->print(OS); + } + + OS << '\n'; } ///} @@ -2031,6 +2093,9 @@ // while we identify default attribute opportunities. Attributor A(Functions, InfoCache, CGUpdater); + A.DG = new AADepGraph(A); + A.SymDGN = new AADepGraphNode(); + // Create shallow wrappers for all functions that are not IPO amendable if (AllowShallowWrappers) for (Function *F : Functions) @@ -2044,8 +2109,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 +2126,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 (auto AA : SymDGN->Deps) { + dyn_cast(AA.getPointer())->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.SymDGN->Deps.size()); + + for (auto tAA : A.SymDGN->Deps) { + AAs.push_back(dyn_cast(tAA.getPointer())); + } + + 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 +2217,51 @@ return PreservedAnalyses::all(); } +namespace llvm { + +template <> struct 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 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 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 @@ -1052,9 +1052,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; @@ -3425,7 +3426,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. Index: llvm/test/Transforms/Attributor/depgraph.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/Attributor/depgraph.ll @@ -0,0 +1,150 @@ +; 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=9 -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=9 -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 --check-prefixes=GRAPH + +; 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) { +; CHECK-LABEL: @checkAndAdvance( +; CHECK-NEXT: [[TMP2:%.*]] = load i32, i32* [[TMP0:%.*]], align 16 +; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[TMP2]], 0 +; CHECK-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP7:%.*]] +; CHECK: 4: +; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds i32, i32* [[TMP0]], i64 4 +; CHECK-NEXT: [[TMP6:%.*]] = call nonnull align 16 i32* @checkAndAdvance(i32* nofree nonnull readonly align 16 "no-capture-maybe-returned" [[TMP5]]) #1 +; CHECK-NEXT: br label [[TMP8:%.*]] +; CHECK: 7: +; CHECK-NEXT: br label [[TMP8]] +; CHECK: 8: +; CHECK-NEXT: [[DOT0:%.*]] = phi i32* [ [[TMP6]], [[TMP4]] ], [ [[TMP0]], [[TMP7]] ] +; CHECK-NEXT: ret i32* [[DOT0]] +; + %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 +} + +; GRAPH: [AAAlign] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AAAlign] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AAAlign] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} + +; GRAPH: [AAAlign] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AAAlign] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAIsDead] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} + +; GRAPH: [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; GRAPH: updates [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AAMemoryBehavior] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} + +; GRAPH: [AAMemoryLocation] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AAMemoryLocation] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AAMemoryLocation] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAMemoryLocation] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} + +; GRAPH: [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} + +; GRAPH: [AANoCapture] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} + +; GRAPH: [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} + +; GRAPH: [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AANoFree] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoFree] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} + +; GRAPH: [AANoSync] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AANoSync] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoSync] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} + +; GRAPH: [AANoSync] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoSync] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AANoUnwind] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} +; GRAPH: updates [AANoCapture] for CtxI ' %2 = load i32, i32* %0, align 4' at position {arg: [@0]} + +; GRAPH: [AANoUnwind] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs: [@-1]} +; GRAPH: updates [AAIsDead] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AAIsDead] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AANoUnwind] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AANonNull] for CtxI ' %5 = getelementptr inbounds i32, i32* %0, i64 4' at position {flt: [@-1]} +; GRAPH: updates [AANonNull] for CtxI ' %5 = getelementptr inbounds i32, i32* %0, i64 4' at position {flt: [@-1]} +; GRAPH: updates [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_arg: [@0]} +; GRAPH: updates [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} + +; GRAPH: [AANonNull] for CtxI ' %2 = load i32, i32* %0, align 4' at position {fn_ret:checkAndAdvance [checkAndAdvance@-1]} +; GRAPH: updates [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]} +; GRAPH: updates [AANonNull] for CtxI ' %6 = call i32* @checkAndAdvance(i32* %5)' at position {cs_ret: [@-1]}