LLVM  3.7.0
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 
40  // set up super class
42  return false;
43 }
44 
45 /// AnalyzeLibCallDetails - Given a call to a function with the specified
46 /// LibCallFunctionInfo, see if we can improve the mod/ref footprint of the call
47 /// vs the specified pointer/size.
49 LibCallAliasAnalysis::AnalyzeLibCallDetails(const LibCallFunctionInfo *FI,
51  const MemoryLocation &Loc) {
52  // If we have a function, check to see what kind of mod/ref effects it
53  // has. Start by including any info globally known about the function.
55  if (MRInfo == NoModRef) return MRInfo;
56 
57  // If that didn't tell us that the function is 'readnone', check to see
58  // if we have detailed info and if 'P' is any of the locations we know
59  // about.
61  if (Details == nullptr)
62  return MRInfo;
63 
64  // If the details array is of the 'DoesNot' kind, we only know something if
65  // the pointer is a match for one of the locations in 'Details'. If we find a
66  // match, we can prove some interactions cannot happen.
67  //
69  // Find out if the pointer refers to a known location.
70  for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
71  const LibCallLocationInfo &LocInfo =
72  LCI->getLocationInfo(Details[i].LocationID);
73  LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
74  if (Res != LibCallLocationInfo::Yes) continue;
75 
76  // If we find a match against a location that we 'do not' interact with,
77  // learn this info into MRInfo.
78  return ModRefResult(MRInfo & ~Details[i].MRInfo);
79  }
80  return MRInfo;
81  }
82 
83  // If the details are of the 'DoesOnly' sort, we know something if the pointer
84  // is a match for one of the locations in 'Details'. Also, if we can prove
85  // that the pointers is *not* one of the locations in 'Details', we know that
86  // the call is NoModRef.
88 
89  // Find out if the pointer refers to a known location.
90  bool NoneMatch = true;
91  for (unsigned i = 0; Details[i].LocationID != ~0U; ++i) {
92  const LibCallLocationInfo &LocInfo =
93  LCI->getLocationInfo(Details[i].LocationID);
94  LibCallLocationInfo::LocResult Res = LocInfo.isLocation(CS, Loc);
95  if (Res == LibCallLocationInfo::No) continue;
96 
97  // If we don't know if this pointer points to the location, then we have to
98  // assume it might alias in some case.
99  if (Res == LibCallLocationInfo::Unknown) {
100  NoneMatch = false;
101  continue;
102  }
103 
104  // If we know that this pointer definitely is pointing into the location,
105  // merge in this information.
106  return ModRefResult(MRInfo & Details[i].MRInfo);
107  }
108 
109  // If we found that the pointer is guaranteed to not match any of the
110  // locations in our 'DoesOnly' rule, then we know that the pointer must point
111  // to some other location. Since the libcall doesn't mod/ref any other
112  // locations, return NoModRef.
113  if (NoneMatch)
114  return NoModRef;
115 
116  // Otherwise, return any other info gained so far.
117  return MRInfo;
118 }
119 
120 // getModRefInfo - Check to see if the specified callsite can clobber the
121 // specified memory object.
122 //
125  const MemoryLocation &Loc) {
126  ModRefResult MRInfo = ModRef;
127 
128  // If this is a direct call to a function that LCI knows about, get the
129  // information about the runtime function.
130  if (LCI) {
131  if (const Function *F = CS.getCalledFunction()) {
132  if (const LibCallFunctionInfo *FI = LCI->getFunctionInfo(F)) {
133  MRInfo = ModRefResult(MRInfo & AnalyzeLibCallDetails(FI, CS, Loc));
134  if (MRInfo == NoModRef) return NoModRef;
135  }
136  }
137  }
138 
139  // The AliasAnalysis base class has some smarts, lets use them.
140  return (ModRefResult)(MRInfo | AliasAnalysis::getModRefInfo(CS, Loc));
141 }
const LibCallFunctionInfo * getFunctionInfo(const Function *F) const
getFunctionInfo - Return the LibCallFunctionInfo object corresponding to the specified function if we...
const LocationMRInfo * LocationDetails
LocationDetails - This is a pointer to an array of LocationMRInfo structs which indicates the behavio...
DoesNot - If DetailsType is set to DoesNot, then the sense of the LocationDetails array is completely...
DoesOnly - If DetailsType is set to DoesOnly, then we know that the only mod/ref behavior of this fun...
F(f)
LibCallInfo - Abstract interface to query about library call information.
INITIALIZE_AG_PASS(LibCallAliasAnalysis, AliasAnalysis,"libcall-aa","LibCall Alias Analysis", false, true, false) FunctionPass *llvm
FunctionPass * createLibCallAliasAnalysisPass(LibCallInfo *LCI)
createLibCallAliasAnalysisPass - Create an alias analysis pass that knows about the semantics of a se...
unsigned LocationID
LocationID - ID # of the accessed location or ~0U for array end.
LibCallFunctionInfo - Each record in the array of FunctionInfo structs records the behavior of one li...
LocResult
isLocation - Return a LocResult if the specified pointer refers to this location for the specified ca...
enum llvm::LibCallFunctionInfo::@26 DetailsType
DetailsType - Indicate the sense of the LocationDetails array.
FunTy * getCalledFunction() const
getCalledFunction - Return the function being called if this is a direct call, otherwise return null ...
Definition: CallSite.h:99
Represent the analysis usage information of a pass.
AliasAnalysis::ModRefResult UniversalBehavior
TODO: Constant folding function: Constant* vector -> Constant*.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
Representation for a specific memory location.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
ModRefResult getModRefInfo(const Instruction *I)
getModRefInfo - Return information about whether or not an instruction may read or write memory (with...
void InitializeAliasAnalysis(Pass *P, const DataLayout *DL)
InitializeAliasAnalysis - Subclasses must call this method to initialize the AliasAnalysis interface ...
ModRefResult getModRefInfo(ImmutableCallSite CS, const MemoryLocation &Loc) override
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
void setPreservesAll()
Set by analyses that do not transform their input at all.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
LocResult(* isLocation)(ImmutableCallSite CS, const MemoryLocation &Loc)
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
ModRefResult
Simple mod/ref information...
virtual void getAnalysisUsage(AnalysisUsage &AU) const
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...
const LibCallLocationInfo & getLocationInfo(unsigned LocID) const
getLocationInfo - Return information about the specified LocationID.
LocationMRInfo - This pair captures info about whether a specific location is modified or referenced ...
LibCallAliasAnalysis - Alias analysis driven from LibCallInfo.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - All alias analysis implementations should invoke this directly (using AliasAnalysi...
LibCallLocationInfo - This struct describes a set of memory locations that are accessed by libcalls...