LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LibCallAliasAnalysis.cpp
Go to the documentation of this file.
1 //===- LibCallAliasAnalysis.cpp - Implement AliasAnalysis for libcalls ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the LibCallAliasAnalysis class.
11 //
12 //===----------------------------------------------------------------------===//
13 
16 #include "llvm/Analysis/Passes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/Pass.h"
19 using namespace llvm;
20 
21 // Register this pass...
24  "LibCall Alias Analysis", false, true, false)
25 
27  return new LibCallAliasAnalysis(LCI);
28 }
29 
31  delete LCI;
32 }
33 
36  AU.setPreservesAll(); // Does not transform code
37 }
38 
39 
40 
41 /// AnalyzeLibCallDetails - Given a call to a function with the specified
42 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
43 /// vs the specified pointer/size.
45 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
47  const Location &Loc) {
48  // If we have a function, check to see what kind of mod/ref effects it
49  // has. Start by including any info globally known about the function.
51  if (MRInfo == NoModRef) return MRInfo;
52 
53  // If that didn't tell us that the function is 'readnone', check to see
54  // if we have detailed info and if 'P' is any of the locations we know
55  // about.
57  if (Details == 0)
58  return MRInfo;
59 
60  // If the details array is of the 'DoesNot' kind, we only know something if
61  // the pointer is a match for one of the locations in 'Details'. If we find a
62  // match, we can prove some interactions cannot happen.
63  //
65  // Find out if the pointer refers to a known location.
66  for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
67  const LibCallLocationInfo &LocInfo =
68  LCI->getLocationInfo(Details[i].LocationID);
69  LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
70  if (Res != LibCallLocationInfo::Yes) continue;
71 
72  // If we find a match against a location that we 'do not' interact with,
73  // learn this info into MRInfo.
74  return ModRefResult(MRInfo & ~Details[i].MRInfo);
75  }
76  return MRInfo;
77  }
78 
79  // If the details are of the 'DoesOnly' sort, we know something if the pointer
80  // is a match for one of the locations in 'Details'. Also, if we can prove
81  // that the pointers is *not* one of the locations in 'Details', we know that
82  // the call is NoModRef.
84 
85  // Find out if the pointer refers to a known location.
86  bool NoneMatch = true;
87  for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
88  const LibCallLocationInfo &LocInfo =
89  LCI->getLocationInfo(Details[i].LocationID);
90  LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
91  if (Res == LibCallLocationInfo::No) continue;
92 
93  // If we don't know if this pointer points to the location, then we have to
94  // assume it might alias in some case.
95  if (Res == LibCallLocationInfo::Unknown) {
96  NoneMatch = false;
97  continue;
98  }
99 
100  // If we know that this pointer definitely is pointing into the location,
101  // merge in this information.
102  return ModRefResult(MRInfo & Details[i].MRInfo);
103  }
104 
105  // If we found that the pointer is guaranteed to not match any of the
106  // locations in our 'DoesOnly' rule, then we know that the pointer must point
107  // to some other location. Since the libcall doesn't mod/ref any other
108  // locations, return NoModRef.
109  if (NoneMatch)
110  return NoModRef;
111 
112  // Otherwise, return any other info gained so far.
113  return MRInfo;
114 }
115 
116 // getModRefInfo - Check to see if the specified callsite can clobber the
117 // specified memory object.
118 //
121  const Location &Loc) {
122  ModRefResult MRInfo = ModRef;
123 
124  // If this is a direct call to a function that LCI knows about, get the
125  // information about the runtime function.
126  if (LCI) {
127  if (const Function *F = CS.getCalledFunction()) {
128  if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
129  MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
130  if (MRInfo == NoModRef) return NoModRef;
131  }
132  }
133  }
134 
135  // The AliasAnalysis base class has some smarts, lets use them.
136  return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
137 }
const LibCallFunctionInfo * getFunctionInfo(const Function *F) const
ModRefResult getModRefInfo(const Instruction *I, const Location &Loc)
const LocationMRInfo * LocationDetails
F(f)
INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis,"libcall-aa","LibCall Alias Analysis", false, true, false) FunctionPass *llvm
LocResult(* isLocation)(ImmutableCallSite CS, const AliasAnalysis::Location &Loc)
FunctionPass * createLibCallAliasAnalysisPass(LibCallInfo *LCI)
unsigned LocationID
LocationID - ID # of the accessed location or ~0U for array end.
enum llvm::LibCallFunctionInfo::@26 DetailsType
AliasAnalysis::ModRefResult UniversalBehavior
TODO: Constant folding function: Constant* vector -> Constant*.
Location - A description of a memory location.
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:318
ModRefResult getModRefInfo(ImmutableCallSite CS, const Location &Loc)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
const LibCallLocationInfo & getLocationInfo(unsigned LocID) const
getLocationInfo - Return information about the specified LocationID.
LibCallAliasAnalysis - Alias analysis driven from LibCallInfo.
virtual void getAnalysisUsage(AnalysisUsage &AU) const
FunTy * getCalledFunction() const
Definition: CallSite.h:93