blob: 4e68bd64b324201b1efa54a208f9ab7f9bd6348a [file] [log] [blame]
George Burgess IVe1919962016-07-06 00:47:21 +00001//=====- CFLSummary.h - Abstract stratified sets implementation. --------=====//
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/// \file
10/// This file defines various utility types and functions useful to
11/// summary-based alias analysis.
12///
13/// Summary-based analysis, also known as bottom-up analysis, is a style of
14/// interprocedrual static analysis that tries to analyze the callees before the
15/// callers get analyzed. The key idea of summary-based analysis is to first
16/// process each function indepedently, outline its behavior in a condensed
17/// summary, and then instantiate the summary at the callsite when the said
18/// function is called elsewhere. This is often in contrast to another style
19/// called top-down analysis, in which callers are always analyzed first before
20/// the callees.
21///
22/// In a summary-based analysis, functions must be examined independently and
23/// out-of-context. We have no information on the state of the memory, the
24/// arguments, the global values, and anything else external to the function. To
25/// carry out the analysis conservative assumptions have to be made about those
26/// external states. In exchange for the potential loss of precision, the
27/// summary we obtain this way is highly reusable, which makes the analysis
28/// easier to scale to large programs even if carried out context-sensitively.
29///
30/// Currently, all CFL-based alias analyses adopt the summary-based approach
31/// and therefore heavily rely on this header.
32///
33//===----------------------------------------------------------------------===//
34
35#ifndef LLVM_ANALYSIS_ALIASANALYSISSUMMARY_H
36#define LLVM_ANALYSIS_ALIASANALYSISSUMMARY_H
37
38#include "llvm/ADT/Optional.h"
George Burgess IVc294d0d2016-07-09 02:54:42 +000039#include "llvm/ADT/SmallVector.h"
George Burgess IVe1919962016-07-06 00:47:21 +000040#include "llvm/IR/CallSite.h"
41#include <bitset>
42
43namespace llvm {
44namespace cflaa {
45
46//===----------------------------------------------------------------------===//
47// AliasAttr related stuffs
48//===----------------------------------------------------------------------===//
49
50/// The number of attributes that AliasAttr should contain. Attributes are
51/// described below, and 32 was an arbitrary choice because it fits nicely in 32
52/// bits (because we use a bitset for AliasAttr).
53static const unsigned NumAliasAttrs = 32;
54
55/// These are attributes that an alias analysis can use to mark certain special
56/// properties of a given pointer. Refer to the related functions below to see
57/// what kinds of attributes are currently defined.
58typedef std::bitset<NumAliasAttrs> AliasAttrs;
59
60/// Attr represent whether the said pointer comes from an unknown source
61/// (such as opaque memory or an integer cast).
62AliasAttrs getAttrNone();
63
64/// AttrUnknown represent whether the said pointer comes from a source not known
65/// to alias analyses (such as opaque memory or an integer cast).
66AliasAttrs getAttrUnknown();
67bool hasUnknownAttr(AliasAttrs);
68
69/// AttrCaller represent whether the said pointer comes from a source not known
70/// to the current function but known to the caller. Values pointed to by the
71/// arguments of the current function have this attribute set
72AliasAttrs getAttrCaller();
73bool hasCallerAttr(AliasAttrs);
74bool hasUnknownOrCallerAttr(AliasAttrs);
75
76/// AttrEscaped represent whether the said pointer comes from a known source but
77/// escapes to the unknown world (e.g. casted to an integer, or passed as an
78/// argument to opaque function). Unlike non-escaped pointers, escaped ones may
79/// alias pointers coming from unknown sources.
80AliasAttrs getAttrEscaped();
81bool hasEscapedAttr(AliasAttrs);
82
83/// AttrGlobal represent whether the said pointer is a global value.
84/// AttrArg represent whether the said pointer is an argument, and if so, what
85/// index the argument has.
86AliasAttrs getGlobalOrArgAttrFromValue(const Value &);
87bool isGlobalOrArgAttr(AliasAttrs);
88
89/// Given an AliasAttrs, return a new AliasAttrs that only contains attributes
90/// meaningful to the caller. This function is primarily used for
91/// interprocedural analysis
92/// Currently, externally visible AliasAttrs include AttrUnknown, AttrGlobal,
93/// and AttrEscaped
94AliasAttrs getExternallyVisibleAttrs(AliasAttrs);
95
96//===----------------------------------------------------------------------===//
97// Function summary related stuffs
98//===----------------------------------------------------------------------===//
99
George Burgess IVc294d0d2016-07-09 02:54:42 +0000100/// The maximum number of arguments we can put into a summary.
George Burgess IV53b195c2016-07-09 03:21:25 +0000101LLVM_CONSTEXPR static unsigned MaxSupportedArgsInSummary = 50;
George Burgess IVc294d0d2016-07-09 02:54:42 +0000102
George Burgess IVe1919962016-07-06 00:47:21 +0000103/// We use InterfaceValue to describe parameters/return value, as well as
104/// potential memory locations that are pointed to by parameters/return value,
105/// of a function.
106/// Index is an integer which represents a single parameter or a return value.
107/// When the index is 0, it refers to the return value. Non-zero index i refers
108/// to the i-th parameter.
109/// DerefLevel indicates the number of dereferences one must perform on the
110/// parameter/return value to get this InterfaceValue.
111struct InterfaceValue {
112 unsigned Index;
113 unsigned DerefLevel;
114};
115
116inline bool operator==(InterfaceValue lhs, InterfaceValue rhs) {
117 return lhs.Index == rhs.Index && lhs.DerefLevel == rhs.DerefLevel;
118}
119inline bool operator!=(InterfaceValue lhs, InterfaceValue rhs) {
120 return !(lhs == rhs);
121}
122
123/// We use ExternalRelation to describe an externally visible aliasing relations
124/// between parameters/return value of a function.
125struct ExternalRelation {
126 InterfaceValue From, To;
127};
128
129/// We use ExternalAttribute to describe an externally visible AliasAttrs
130/// for parameters/return value.
131struct ExternalAttribute {
132 InterfaceValue IValue;
133 AliasAttrs Attr;
134};
135
George Burgess IVc294d0d2016-07-09 02:54:42 +0000136/// AliasSummary is just a collection of ExternalRelation and ExternalAttribute
137struct AliasSummary {
138 // RetParamRelations is a collection of ExternalRelations.
139 SmallVector<ExternalRelation, 8> RetParamRelations;
140
141 // RetParamAttributes is a collection of ExternalAttributes.
142 SmallVector<ExternalAttribute, 8> RetParamAttributes;
143};
144
George Burgess IVe1919962016-07-06 00:47:21 +0000145/// This is the result of instantiating InterfaceValue at a particular callsite
146struct InstantiatedValue {
147 Value *Val;
148 unsigned DerefLevel;
149};
150Optional<InstantiatedValue> instantiateInterfaceValue(InterfaceValue, CallSite);
151
152/// This is the result of instantiating ExternalRelation at a particular
153/// callsite
154struct InstantiatedRelation {
155 InstantiatedValue From, To;
156};
157Optional<InstantiatedRelation> instantiateExternalRelation(ExternalRelation,
158 CallSite);
159
160/// This is the result of instantiating ExternalAttribute at a particular
161/// callsite
162struct InstantiatedAttr {
163 InstantiatedValue IValue;
164 AliasAttrs Attr;
165};
166Optional<InstantiatedAttr> instantiateExternalAttribute(ExternalAttribute,
167 CallSite);
168}
169}
170
171#endif