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