LLVM API Documentation

LibCallAliasAnalysis.cpp
Go to the documentation of this file.
00001 //===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===//
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 //
00010 // This file implements the LibCallAliasAnalysis class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Analysis/LibCallAliasAnalysis.h"
00015 #include "llvm/Analysis/LibCallSemantics.h"
00016 #include "llvm/Analysis/Passes.h"
00017 #include "llvm/IR/Function.h"
00018 #include "llvm/Pass.h"
00019 using namespace llvm;
00020   
00021 // Register this pass...
00022 char LibCallAliasAnalysis::ID = 0;
00023 INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa",
00024                    "LibCall Alias Analysis", false, true, false)
00025 
00026 FunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) {
00027   return new LibCallAliasAnalysis(LCI);
00028 }
00029 
00030 LibCallAliasAnalysis::~LibCallAliasAnalysis() {
00031   delete LCI;
00032 }
00033 
00034 void LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
00035   AliasAnalysis::getAnalysisUsage(AU);
00036   AU.setPreservesAll();                         // Does not transform code
00037 }
00038 
00039 
00040 
00041 /// AnalyzeLibCallDetails - Given a call to a function with the specified
00042 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
00043 /// vs the specified pointer/size.
00044 AliasAnalysis::ModRefResult
00045 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
00046                                             ImmutableCallSite CS,
00047                                             const Location &Loc) {
00048   // If we have a function, check to see what kind of mod/ref effects it
00049   // has.  Start by including any info globally known about the function.
00050   AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior;
00051   if (MRInfo == NoModRef) return MRInfo;
00052   
00053   // If that didn't tell us that the function is 'readnone', check to see
00054   // if we have detailed info and if 'P' is any of the locations we know
00055   // about.
00056   const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails;
00057   if (Details == nullptr)
00058     return MRInfo;
00059   
00060   // If the details array is of the 'DoesNot' kind, we only know something if
00061   // the pointer is a match for one of the locations in 'Details'.  If we find a
00062   // match, we can prove some interactions cannot happen.
00063   // 
00064   if (FI->DetailsType == LibCallFunctionInfo::DoesNot) {
00065     // Find out if the pointer refers to a known location.
00066     for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
00067       const LibCallLocationInfo &LocInfo =
00068       LCI->getLocationInfo(Details[i].LocationID);
00069       LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
00070       if (Res != LibCallLocationInfo::Yes) continue;
00071       
00072       // If we find a match against a location that we 'do not' interact with,
00073       // learn this info into MRInfo.
00074       return ModRefResult(MRInfo & ~Details[i].MRInfo);
00075     }
00076     return MRInfo;
00077   }
00078   
00079   // If the details are of the 'DoesOnly' sort, we know something if the pointer
00080   // is a match for one of the locations in 'Details'.  Also, if we can prove
00081   // that the pointers is *not* one of the locations in 'Details', we know that
00082   // the call is NoModRef.
00083   assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly);
00084   
00085   // Find out if the pointer refers to a known location.
00086   bool NoneMatch = true;
00087   for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
00088     const LibCallLocationInfo &LocInfo =
00089     LCI->getLocationInfo(Details[i].LocationID);
00090     LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
00091     if (Res == LibCallLocationInfo::No) continue;
00092     
00093     // If we don't know if this pointer points to the location, then we have to
00094     // assume it might alias in some case.
00095     if (Res == LibCallLocationInfo::Unknown) {
00096       NoneMatch = false;
00097       continue;
00098     }
00099     
00100     // If we know that this pointer definitely is pointing into the location,
00101     // merge in this information.
00102     return ModRefResult(MRInfo & Details[i].MRInfo);
00103   }
00104   
00105   // If we found that the pointer is guaranteed to not match any of the
00106   // locations in our 'DoesOnly' rule, then we know that the pointer must point
00107   // to some other location.  Since the libcall doesn't mod/ref any other
00108   // locations, return NoModRef.
00109   if (NoneMatch)
00110     return NoModRef;
00111   
00112   // Otherwise, return any other info gained so far.
00113   return MRInfo;
00114 }
00115 
00116 // getModRefInfo - Check to see if the specified callsite can clobber the
00117 // specified memory object.
00118 //
00119 AliasAnalysis::ModRefResult
00120 LibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
00121                                     const Location &Loc) {
00122   ModRefResult MRInfo = ModRef;
00123   
00124   // If this is a direct call to a function that LCI knows about, get the
00125   // information about the runtime function.
00126   if (LCI) {
00127     if (const Function *F = CS.getCalledFunction()) {
00128       if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
00129         MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
00130         if (MRInfo == NoModRef) return NoModRef;
00131       }
00132     }
00133   }
00134   
00135   // The AliasAnalysis base class has some smarts, lets use them.
00136   return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
00137 }