1193323Sed//===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements the LibCallAliasAnalysis class. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#include "llvm/Analysis/LibCallAliasAnalysis.h" 15249423Sdim#include "llvm/Analysis/LibCallSemantics.h" 16193323Sed#include "llvm/Analysis/Passes.h" 17249423Sdim#include "llvm/IR/Function.h" 18193323Sed#include "llvm/Pass.h" 19193323Sedusing namespace llvm; 20193323Sed 21193323Sed// Register this pass... 22193323Sedchar LibCallAliasAnalysis::ID = 0; 23212904SdimINITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis, "libcall-aa", 24218893Sdim "LibCall Alias Analysis", false, true, false) 25193323Sed 26193323SedFunctionPass *llvm::createLibCallAliasAnalysisPass(LibCallInfo *LCI) { 27193323Sed return new LibCallAliasAnalysis(LCI); 28193323Sed} 29193323Sed 30193323SedLibCallAliasAnalysis::~LibCallAliasAnalysis() { 31193323Sed delete LCI; 32193323Sed} 33193323Sed 34193323Sedvoid LibCallAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 35193323Sed AliasAnalysis::getAnalysisUsage(AU); 36193323Sed AU.setPreservesAll(); // Does not transform code 37193323Sed} 38193323Sed 39193323Sed 40193323Sed 41193323Sed/// AnalyzeLibCallDetails - Given a call to a function with the specified 42193323Sed/// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call 43193323Sed/// vs the specified pointer/size. 44193323SedAliasAnalysis::ModRefResult 45193323SedLibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI, 46218893Sdim ImmutableCallSite CS, 47218893Sdim const Location &Loc) { 48193323Sed // If we have a function, check to see what kind of mod/ref effects it 49193323Sed // has. Start by including any info globally known about the function. 50193323Sed AliasAnalysis::ModRefResult MRInfo = FI->UniversalBehavior; 51193323Sed if (MRInfo == NoModRef) return MRInfo; 52193323Sed 53193323Sed // If that didn't tell us that the function is 'readnone', check to see 54193323Sed // if we have detailed info and if 'P' is any of the locations we know 55193323Sed // about. 56193323Sed const LibCallFunctionInfo::LocationMRInfo *Details = FI->LocationDetails; 57193323Sed if (Details == 0) 58193323Sed return MRInfo; 59193323Sed 60193323Sed // If the details array is of the 'DoesNot' kind, we only know something if 61193323Sed // the pointer is a match for one of the locations in 'Details'. If we find a 62193323Sed // match, we can prove some interactions cannot happen. 63193323Sed // 64193323Sed if (FI->DetailsType == LibCallFunctionInfo::DoesNot) { 65193323Sed // Find out if the pointer refers to a known location. 66193323Sed for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) { 67218893Sdim const LibCallLocationInfo &LocInfo = 68193323Sed LCI->getLocationInfo(Details[i].LocationID); 69218893Sdim LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc); 70193323Sed if (Res != LibCallLocationInfo::Yes) continue; 71193323Sed 72193323Sed // If we find a match against a location that we 'do not' interact with, 73193323Sed // learn this info into MRInfo. 74193323Sed return ModRefResult(MRInfo & ~Details[i].MRInfo); 75193323Sed } 76193323Sed return MRInfo; 77193323Sed } 78193323Sed 79193323Sed // If the details are of the 'DoesOnly' sort, we know something if the pointer 80193323Sed // is a match for one of the locations in 'Details'. Also, if we can prove 81193323Sed // that the pointers is *not* one of the locations in 'Details', we know that 82193323Sed // the call is NoModRef. 83193323Sed assert(FI->DetailsType == LibCallFunctionInfo::DoesOnly); 84193323Sed 85193323Sed // Find out if the pointer refers to a known location. 86193323Sed bool NoneMatch = true; 87193323Sed for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) { 88218893Sdim const LibCallLocationInfo &LocInfo = 89193323Sed LCI->getLocationInfo(Details[i].LocationID); 90218893Sdim LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc); 91193323Sed if (Res == LibCallLocationInfo::No) continue; 92193323Sed 93193323Sed // If we don't know if this pointer points to the location, then we have to 94193323Sed // assume it might alias in some case. 95193323Sed if (Res == LibCallLocationInfo::Unknown) { 96193323Sed NoneMatch = false; 97193323Sed continue; 98193323Sed } 99193323Sed 100193323Sed // If we know that this pointer definitely is pointing into the location, 101193323Sed // merge in this information. 102193323Sed return ModRefResult(MRInfo & Details[i].MRInfo); 103193323Sed } 104193323Sed 105193323Sed // If we found that the pointer is guaranteed to not match any of the 106193323Sed // locations in our 'DoesOnly' rule, then we know that the pointer must point 107193323Sed // to some other location. Since the libcall doesn't mod/ref any other 108193323Sed // locations, return NoModRef. 109193323Sed if (NoneMatch) 110193323Sed return NoModRef; 111193323Sed 112193323Sed // Otherwise, return any other info gained so far. 113193323Sed return MRInfo; 114193323Sed} 115193323Sed 116193323Sed// getModRefInfo - Check to see if the specified callsite can clobber the 117193323Sed// specified memory object. 118193323Sed// 119193323SedAliasAnalysis::ModRefResult 120212904SdimLibCallAliasAnalysis::getModRefInfo(ImmutableCallSite CS, 121218893Sdim const Location &Loc) { 122193323Sed ModRefResult MRInfo = ModRef; 123193323Sed 124193323Sed // If this is a direct call to a function that LCI knows about, get the 125193323Sed // information about the runtime function. 126193323Sed if (LCI) { 127212904Sdim if (const Function *F = CS.getCalledFunction()) { 128193323Sed if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) { 129218893Sdim MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc)); 130193323Sed if (MRInfo == NoModRef) return NoModRef; 131193323Sed } 132193323Sed } 133193323Sed } 134193323Sed 135193323Sed // The AliasAnalysis base class has some smarts, lets use them. 136218893Sdim return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc)); 137193323Sed} 138