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