LLVM  mainline
ObjCARCAliasAnalysis.h
Go to the documentation of this file.
00001 //===- ObjCARCAliasAnalysis.h - ObjC ARC Alias Analysis ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 /// This file declares a simple ARC-aware AliasAnalysis using special knowledge
00011 /// of Objective C to enhance other optimization passes which rely on the Alias
00012 /// Analysis infrastructure.
00013 ///
00014 /// WARNING: This file knows about certain library functions. It recognizes them
00015 /// by name, and hardwires knowledge of their semantics.
00016 ///
00017 /// WARNING: This file knows about how certain Objective-C library functions are
00018 /// used. Naive LLVM IR transformations which would otherwise be
00019 /// behavior-preserving may break these assumptions.
00020 ///
00021 //===----------------------------------------------------------------------===//
00022 
00023 #ifndef LLVM_ANALYSIS_OBJCARCALIASANALYSIS_H
00024 #define LLVM_ANALYSIS_OBJCARCALIASANALYSIS_H
00025 
00026 #include "llvm/Analysis/AliasAnalysis.h"
00027 #include "llvm/Analysis/TargetLibraryInfo.h"
00028 #include "llvm/Pass.h"
00029 
00030 namespace llvm {
00031 namespace objcarc {
00032 
00033 /// \brief This is a simple alias analysis implementation that uses knowledge
00034 /// of ARC constructs to answer queries.
00035 ///
00036 /// TODO: This class could be generalized to know about other ObjC-specific
00037 /// tricks. Such as knowing that ivars in the non-fragile ABI are non-aliasing
00038 /// even though their offsets are dynamic.
00039 class ObjCARCAAResult : public AAResultBase<ObjCARCAAResult> {
00040   friend AAResultBase<ObjCARCAAResult>;
00041 
00042   const DataLayout &DL;
00043 
00044 public:
00045   explicit ObjCARCAAResult(const DataLayout &DL, const TargetLibraryInfo &TLI)
00046       : AAResultBase(TLI), DL(DL) {}
00047   ObjCARCAAResult(ObjCARCAAResult &&Arg)
00048       : AAResultBase(std::move(Arg)), DL(Arg.DL) {}
00049 
00050   /// Handle invalidation events from the new pass manager.
00051   ///
00052   /// By definition, this result is stateless and so remains valid.
00053   bool invalidate(Function &, const PreservedAnalyses &) { return false; }
00054 
00055   AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB);
00056   bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal);
00057 
00058   using AAResultBase::getModRefBehavior;
00059   FunctionModRefBehavior getModRefBehavior(const Function *F);
00060 
00061   using AAResultBase::getModRefInfo;
00062   ModRefInfo getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc);
00063 };
00064 
00065 /// Analysis pass providing a never-invalidated alias analysis result.
00066 class ObjCARCAA {
00067 public:
00068   typedef ObjCARCAAResult Result;
00069 
00070   /// \brief Opaque, unique identifier for this analysis pass.
00071   static void *ID() { return (void *)&PassID; }
00072 
00073   ObjCARCAAResult run(Function &F, AnalysisManager<Function> *AM);
00074 
00075   /// \brief Provide access to a name for this pass for debugging purposes.
00076   static StringRef name() { return "ObjCARCAA"; }
00077 
00078 private:
00079   static char PassID;
00080 };
00081 
00082 /// Legacy wrapper pass to provide the ObjCARCAAResult object.
00083 class ObjCARCAAWrapperPass : public ImmutablePass {
00084   std::unique_ptr<ObjCARCAAResult> Result;
00085 
00086 public:
00087   static char ID;
00088 
00089   ObjCARCAAWrapperPass();
00090 
00091   ObjCARCAAResult &getResult() { return *Result; }
00092   const ObjCARCAAResult &getResult() const { return *Result; }
00093 
00094   bool doInitialization(Module &M) override;
00095   bool doFinalization(Module &M) override;
00096   void getAnalysisUsage(AnalysisUsage &AU) const override;
00097 };
00098 
00099 } // namespace objcarc
00100 } // namespace llvm
00101 
00102 #endif