annotate lib/Transforms/IPO/FunctionAttrs.cpp @ 125:56c5119fbcd2

fix
author mir3636
date Sun, 03 Dec 2017 20:09:16 +0900
parents 803732b1fca8
children 3a76565eade5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
9 //
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
10 /// \file
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
11 /// This file implements interprocedural passes which walk the
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
12 /// call-graph deducing and/or propagating function attributes.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
13 //
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
16 #include "llvm/Transforms/IPO/FunctionAttrs.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "llvm/ADT/SCCIterator.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
18 #include "llvm/ADT/STLExtras.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/ADT/SetVector.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
20 #include "llvm/ADT/SmallPtrSet.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/ADT/SmallSet.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
22 #include "llvm/ADT/SmallVector.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/ADT/Statistic.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "llvm/Analysis/AliasAnalysis.h"
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
25 #include "llvm/Analysis/AssumptionCache.h"
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
26 #include "llvm/Analysis/BasicAliasAnalysis.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
27 #include "llvm/Analysis/CGSCCPassManager.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #include "llvm/Analysis/CallGraph.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 #include "llvm/Analysis/CallGraphSCCPass.h"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include "llvm/Analysis/CaptureTracking.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
31 #include "llvm/Analysis/LazyCallGraph.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
32 #include "llvm/Analysis/MemoryLocation.h"
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
33 #include "llvm/Analysis/ValueTracking.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
34 #include "llvm/IR/Argument.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
35 #include "llvm/IR/Attributes.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
36 #include "llvm/IR/BasicBlock.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
37 #include "llvm/IR/CallSite.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
38 #include "llvm/IR/Constant.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
39 #include "llvm/IR/Constants.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
40 #include "llvm/IR/Function.h"
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
41 #include "llvm/IR/InstIterator.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
42 #include "llvm/IR/InstrTypes.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
43 #include "llvm/IR/Instruction.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
44 #include "llvm/IR/Instructions.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
45 #include "llvm/IR/Metadata.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
46 #include "llvm/IR/PassManager.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
47 #include "llvm/IR/Type.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
48 #include "llvm/IR/Use.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
49 #include "llvm/IR/User.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
50 #include "llvm/IR/Value.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
51 #include "llvm/Pass.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
52 #include "llvm/Support/Casting.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
53 #include "llvm/Support/CommandLine.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
54 #include "llvm/Support/Compiler.h"
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
55 #include "llvm/Support/Debug.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
56 #include "llvm/Support/ErrorHandling.h"
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
57 #include "llvm/Support/raw_ostream.h"
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
58 #include "llvm/Transforms/IPO.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
59 #include <cassert>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
60 #include <iterator>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
61 #include <map>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
62 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
63
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 using namespace llvm;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
66 #define DEBUG_TYPE "functionattrs"
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
67
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 STATISTIC(NumReadNone, "Number of functions marked readnone");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 STATISTIC(NumReadOnly, "Number of functions marked readonly");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
71 STATISTIC(NumReturned, "Number of arguments marked returned");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 STATISTIC(NumNoAlias, "Number of function returns marked noalias");
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
75 STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
76 STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
78 // FIXME: This is disabled by default to avoid exposing security vulnerabilities
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
79 // in C/C++ code compiled by clang:
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
80 // http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
81 static cl::opt<bool> EnableNonnullArgPropagation(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
82 "enable-nonnull-arg-prop", cl::Hidden,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
83 cl::desc("Try to propagate nonnull argument attributes from callsites to "
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
84 "caller functions."));
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
85
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
86 namespace {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
87
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
88 using SCCNodeSet = SmallSetVector<Function *, 8>;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
89
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
90 } // end anonymous namespace
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
92 /// Returns the memory access attribute for function F using AAR for AA results,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
93 /// where SCCNodes is the current SCC.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
94 ///
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
95 /// If ThisBody is true, this function may examine the function body and will
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
96 /// return a result pertaining to this copy of the function. If it is false, the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
97 /// result will be based only on AA results for the function declaration; it
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
98 /// will be assumed that some other (perhaps less optimized) version of the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
99 /// function may be selected at link time.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
100 static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
101 AAResults &AAR,
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
102 const SCCNodeSet &SCCNodes) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
103 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
104 if (MRB == FMRB_DoesNotAccessMemory)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
105 // Already perfect!
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
106 return MAK_ReadNone;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
107
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
108 if (!ThisBody) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
109 if (AliasAnalysis::onlyReadsMemory(MRB))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
110 return MAK_ReadOnly;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
111
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
112 // Conservatively assume it writes to memory.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
113 return MAK_MayWrite;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
114 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
115
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
116 // Scan the function body for instructions that may read or write memory.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
117 bool ReadsMemory = false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
118 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
119 Instruction *I = &*II;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
120
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
121 // Some instructions can be ignored even if they read or write memory.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
122 // Detect these now, skipping to the next instruction if one is found.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
123 CallSite CS(cast<Value>(I));
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
124 if (CS) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
125 // Ignore calls to functions in the same SCC, as long as the call sites
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
126 // don't have operand bundles. Calls with operand bundles are allowed to
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
127 // have memory effects not described by the memory effects of the call
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
128 // target.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
129 if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
130 SCCNodes.count(CS.getCalledFunction()))
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
131 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
132 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
133
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
134 // If the call doesn't access memory, we're done.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
135 if (!(MRB & MRI_ModRef))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
136 continue;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
137
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
138 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
139 // The call could access any memory. If that includes writes, give up.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
140 if (MRB & MRI_Mod)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
141 return MAK_MayWrite;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
142 // If it reads, note it.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
143 if (MRB & MRI_Ref)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
144 ReadsMemory = true;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
145 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
146 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
147
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
148 // Check whether all pointer arguments point to local memory, and
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
149 // ignore calls that only access local memory.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
150 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
151 CI != CE; ++CI) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
152 Value *Arg = *CI;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
153 if (!Arg->getType()->isPtrOrPtrVectorTy())
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
154 continue;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
155
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
156 AAMDNodes AAInfo;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
157 I->getAAMetadata(AAInfo);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
158 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
159
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
160 // Skip accesses to local or constant memory as they don't impact the
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
161 // externally visible mod/ref behavior.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
162 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
163 continue;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
164
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
165 if (MRB & MRI_Mod)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
166 // Writes non-local memory. Give up.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
167 return MAK_MayWrite;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
168 if (MRB & MRI_Ref)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
169 // Ok, it reads non-local memory.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
170 ReadsMemory = true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
171 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
172 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
173 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
174 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
175 if (!LI->isVolatile()) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
176 MemoryLocation Loc = MemoryLocation::get(LI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
177 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
178 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
179 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
180 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
181 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
182 if (!SI->isVolatile()) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
183 MemoryLocation Loc = MemoryLocation::get(SI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
184 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
185 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
186 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
187 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
188 // Ignore vaargs on local memory.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
189 MemoryLocation Loc = MemoryLocation::get(VI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
190 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
191 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
192 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
193
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
194 // Any remaining instructions need to be taken seriously! Check if they
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
195 // read or write memory.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
196 if (I->mayWriteToMemory())
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
197 // Writes memory. Just give up.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
198 return MAK_MayWrite;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
199
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
200 // If this instruction may read memory, remember that.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
201 ReadsMemory |= I->mayReadFromMemory();
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
202 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
203
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
204 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
205 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
206
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
207 MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
208 AAResults &AAR) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
209 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
210 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
211
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
212 /// Deduce readonly/readnone attributes for the SCC.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
213 template <typename AARGetterT>
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
214 static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 // Check if any of the functions in the SCC read or write memory. If they
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 // write memory then they can't be marked readnone or readonly.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217 bool ReadsMemory = false;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
218 for (Function *F : SCCNodes) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
219 // Call the callable parameter to look up AA results for this function.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
220 AAResults &AAR = AARGetter(*F);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
221
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
222 // Non-exact function definitions may not be selected at link time, and an
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
223 // alternative version that writes to memory may be selected. See the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
224 // comment on GlobalValue::isDefinitionExact for more details.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
225 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
226 AAR, SCCNodes)) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
227 case MAK_MayWrite:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
228 return false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
229 case MAK_ReadOnly:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
230 ReadsMemory = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
231 break;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
232 case MAK_ReadNone:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
233 // Nothing to do!
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
234 break;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 // Success! Functions in this SCC do not access memory, or only read memory.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 // Give them the appropriate attribute.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 bool MadeChange = false;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
241 for (Function *F : SCCNodes) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 if (F->doesNotAccessMemory())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 // Already perfect!
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 if (F->onlyReadsMemory() && ReadsMemory)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 // No change.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 MadeChange = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 // Clear out any existing attributes.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
253 F->removeFnAttr(Attribute::ReadOnly);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
254 F->removeFnAttr(Attribute::ReadNone);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 // Add in the new attribute.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
257 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 if (ReadsMemory)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260 ++NumReadOnly;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 else
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 ++NumReadNone;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 return MadeChange;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268 namespace {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
269
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
270 /// For a given pointer Argument, this retains a list of Arguments of functions
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
271 /// in the same SCC that the pointer data flows into. We use this to build an
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
272 /// SCC of the arguments.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
273 struct ArgumentGraphNode {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
274 Argument *Definition;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
275 SmallVector<ArgumentGraphNode *, 4> Uses;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
276 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
278 class ArgumentGraph {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
279 // We store pointers to ArgumentGraphNode objects, so it's important that
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
280 // that they not move around upon insert.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
281 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
283 ArgumentMapTy ArgumentMap;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
285 // There is no root node for the argument graph, in fact:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
286 // void f(int *x, int *y) { if (...) f(x, y); }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
287 // is an example where the graph is disconnected. The SCCIterator requires a
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
288 // single entry point, so we maintain a fake ("synthetic") root node that
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
289 // uses every node. Because the graph is directed and nothing points into
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
290 // the root, it will not participate in any SCCs (except for its own).
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
291 ArgumentGraphNode SyntheticRoot;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
293 public:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
294 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
296 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
298 iterator begin() { return SyntheticRoot.Uses.begin(); }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
299 iterator end() { return SyntheticRoot.Uses.end(); }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
300 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
302 ArgumentGraphNode *operator[](Argument *A) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
303 ArgumentGraphNode &Node = ArgumentMap[A];
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
304 Node.Definition = A;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
305 SyntheticRoot.Uses.push_back(&Node);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
306 return &Node;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
307 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
308 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
310 /// This tracker checks whether callees are in the SCC, and if so it does not
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
311 /// consider that a capture, instead adding it to the "Uses" list and
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
312 /// continuing with the analysis.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
313 struct ArgumentUsesTracker : public CaptureTracker {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
314 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
316 void tooManyUses() override { Captured = true; }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
318 bool captured(const Use *U) override {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
319 CallSite CS(U->getUser());
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
320 if (!CS.getInstruction()) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
321 Captured = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
322 return true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
323 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
324
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
325 Function *F = CS.getCalledFunction();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
326 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
327 Captured = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
328 return true;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
331 // Note: the callee and the two successor blocks *follow* the argument
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
332 // operands. This means there is no need to adjust UseIndex to account for
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
333 // these.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
334
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
335 unsigned UseIndex =
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
336 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
337
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
338 assert(UseIndex < CS.data_operands_size() &&
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
339 "Indirect function calls should have been filtered above!");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
340
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
341 if (UseIndex >= CS.getNumArgOperands()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
342 // Data operand, but not a argument operand -- must be a bundle operand
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
343 assert(CS.hasOperandBundles() && "Must be!");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
344
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
345 // CaptureTracking told us that we're being captured by an operand bundle
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
346 // use. In this case it does not matter if the callee is within our SCC
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
347 // or not -- we've been captured in some unknown way, and we have to be
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
348 // conservative.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
349 Captured = true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
350 return true;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
351 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
352
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
353 if (UseIndex >= F->arg_size()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
354 assert(F->isVarArg() && "More params than args in non-varargs call");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
355 Captured = true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
356 return true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
357 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
358
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
359 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
360 return false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
361 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
363 // True only if certainly captured (used outside our SCC).
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
364 bool Captured = false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
365
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
366 // Uses within our SCC.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
367 SmallVector<Argument *, 4> Uses;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
368
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
369 const SCCNodeSet &SCCNodes;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
370 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
371
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
372 } // end anonymous namespace
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
373
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
374 namespace llvm {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
375
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
376 template <> struct GraphTraits<ArgumentGraphNode *> {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
377 using NodeRef = ArgumentGraphNode *;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
378 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
379
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
380 static NodeRef getEntryNode(NodeRef A) { return A; }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
381 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
382 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
383 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
384
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
385 template <>
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
386 struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
387 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
388
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
389 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
390 return AG->begin();
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
391 }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
392
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
393 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
394 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
395
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
396 } // end namespace llvm
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
397
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
398 /// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
399 static Attribute::AttrKind
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
400 determinePointerReadAttrs(Argument *A,
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
401 const SmallPtrSet<Argument *, 8> &SCCNodes) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
402 SmallVector<Use *, 32> Worklist;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
403 SmallSet<Use *, 32> Visited;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
404
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
405 // inalloca arguments are always clobbered by the call.
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
406 if (A->hasInAllocaAttr())
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
407 return Attribute::None;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
408
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
409 bool IsRead = false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
410 // We don't need to track IsWritten. If A is written to, return immediately.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
411
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
412 for (Use &U : A->uses()) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
413 Visited.insert(&U);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
414 Worklist.push_back(&U);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
415 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
416
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
417 while (!Worklist.empty()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
418 Use *U = Worklist.pop_back_val();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
419 Instruction *I = cast<Instruction>(U->getUser());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
420
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
421 switch (I->getOpcode()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
422 case Instruction::BitCast:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
423 case Instruction::GetElementPtr:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
424 case Instruction::PHI:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
425 case Instruction::Select:
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
426 case Instruction::AddrSpaceCast:
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
427 // The original value is not read/written via this if the new value isn't.
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
428 for (Use &UU : I->uses())
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
429 if (Visited.insert(&UU).second)
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
430 Worklist.push_back(&UU);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
431 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
432
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
433 case Instruction::Call:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
434 case Instruction::Invoke: {
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
435 bool Captures = true;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
436
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
437 if (I->getType()->isVoidTy())
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
438 Captures = false;
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
439
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
440 auto AddUsersToWorklistIfCapturing = [&] {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
441 if (Captures)
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
442 for (Use &UU : I->uses())
83
60c9769439b8 LLVM 3.7
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 77
diff changeset
443 if (Visited.insert(&UU).second)
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
444 Worklist.push_back(&UU);
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
445 };
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
446
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
447 CallSite CS(I);
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
448 if (CS.doesNotAccessMemory()) {
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
449 AddUsersToWorklistIfCapturing();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
450 continue;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
451 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
452
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
453 Function *F = CS.getCalledFunction();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
454 if (!F) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
455 if (CS.onlyReadsMemory()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
456 IsRead = true;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
457 AddUsersToWorklistIfCapturing();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
458 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
459 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
460 return Attribute::None;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
461 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
462
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
463 // Note: the callee and the two successor blocks *follow* the argument
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
464 // operands. This means there is no need to adjust UseIndex to account
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
465 // for these.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
466
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
467 unsigned UseIndex = std::distance(CS.arg_begin(), U);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
468
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
469 // U cannot be the callee operand use: since we're exploring the
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
470 // transitive uses of an Argument, having such a use be a callee would
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
471 // imply the CallSite is an indirect call or invoke; and we'd take the
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
472 // early exit above.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
473 assert(UseIndex < CS.data_operands_size() &&
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
474 "Data operand use expected!");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
475
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
476 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
477
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
478 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
479 assert(F->isVarArg() && "More params than args in non-varargs call");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
480 return Attribute::None;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
481 }
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
482
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
483 Captures &= !CS.doesNotCapture(UseIndex);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
484
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
485 // Since the optimizer (by design) cannot see the data flow corresponding
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
486 // to a operand bundle use, these cannot participate in the optimistic SCC
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
487 // analysis. Instead, we model the operand bundle uses as arguments in
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
488 // call to a function external to the SCC.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
489 if (IsOperandBundleUse ||
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
490 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
491
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
492 // The accessors used on CallSite here do the right thing for calls and
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
493 // invokes with operand bundles.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
494
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
495 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
496 return Attribute::None;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
497 if (!CS.doesNotAccessMemory(UseIndex))
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
498 IsRead = true;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
499 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
500
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
501 AddUsersToWorklistIfCapturing();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
502 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
503 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
504
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
505 case Instruction::Load:
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
506 // A volatile load has side effects beyond what readonly can be relied
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
507 // upon.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
508 if (cast<LoadInst>(I)->isVolatile())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
509 return Attribute::None;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
510
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
511 IsRead = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
512 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
513
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
514 case Instruction::ICmp:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
515 case Instruction::Ret:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
516 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
517
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
518 default:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
519 return Attribute::None;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
520 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
521 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
522
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
523 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
524 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
525
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
526 /// Deduce returned attributes for the SCC.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
527 static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
528 bool Changed = false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
529
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
530 // Check each function in turn, determining if an argument is always returned.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
531 for (Function *F : SCCNodes) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
532 // We can infer and propagate function attributes only when we know that the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
533 // definition we'll get at link time is *exactly* the definition we see now.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
534 // For more details, see GlobalValue::mayBeDerefined.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
535 if (!F->hasExactDefinition())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
536 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
537
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
538 if (F->getReturnType()->isVoidTy())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
539 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
540
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
541 // There is nothing to do if an argument is already marked as 'returned'.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
542 if (llvm::any_of(F->args(),
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
543 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
544 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
545
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
546 auto FindRetArg = [&]() -> Value * {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
547 Value *RetArg = nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
548 for (BasicBlock &BB : *F)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
549 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
550 // Note that stripPointerCasts should look through functions with
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
551 // returned arguments.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
552 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
553 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
554 return nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
555
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
556 if (!RetArg)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
557 RetArg = RetVal;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
558 else if (RetArg != RetVal)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
559 return nullptr;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
560 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
561
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
562 return RetArg;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
563 };
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
564
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
565 if (Value *RetArg = FindRetArg()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
566 auto *A = cast<Argument>(RetArg);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
567 A->addAttr(Attribute::Returned);
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
568 ++NumReturned;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
569 Changed = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
570 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
571 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
572
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
573 return Changed;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
574 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
575
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
576 /// If a callsite has arguments that are also arguments to the parent function,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
577 /// try to propagate attributes from the callsite's arguments to the parent's
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
578 /// arguments. This may be important because inlining can cause information loss
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
579 /// when attribute knowledge disappears with the inlined call.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
580 static bool addArgumentAttrsFromCallsites(Function &F) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
581 if (!EnableNonnullArgPropagation)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
582 return false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
583
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
584 bool Changed = false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
585
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
586 // For an argument attribute to transfer from a callsite to the parent, the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
587 // call must be guaranteed to execute every time the parent is called.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
588 // Conservatively, just check for calls in the entry block that are guaranteed
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
589 // to execute.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
590 // TODO: This could be enhanced by testing if the callsite post-dominates the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
591 // entry block or by doing simple forward walks or backward walks to the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
592 // callsite.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
593 BasicBlock &Entry = F.getEntryBlock();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
594 for (Instruction &I : Entry) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
595 if (auto CS = CallSite(&I)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
596 if (auto *CalledFunc = CS.getCalledFunction()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
597 for (auto &CSArg : CalledFunc->args()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
598 if (!CSArg.hasNonNullAttr())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
599 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
600
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
601 // If the non-null callsite argument operand is an argument to 'F'
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
602 // (the caller) and the call is guaranteed to execute, then the value
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
603 // must be non-null throughout 'F'.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
604 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
605 if (FArg && !FArg->hasNonNullAttr()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
606 FArg->addAttr(Attribute::NonNull);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
607 Changed = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
608 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
609 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
610 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
611 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
612 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
613 break;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
614 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
615
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
616 return Changed;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
617 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
618
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
619 /// Deduce nocapture attributes for the SCC.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
620 static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
621 bool Changed = false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
622
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
623 ArgumentGraph AG;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
624
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
625 // Check each function in turn, determining which pointer arguments are not
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
626 // captured.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
627 for (Function *F : SCCNodes) {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
628 // We can infer and propagate function attributes only when we know that the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
629 // definition we'll get at link time is *exactly* the definition we see now.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
630 // For more details, see GlobalValue::mayBeDerefined.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
631 if (!F->hasExactDefinition())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
632 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
633
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
634 Changed |= addArgumentAttrsFromCallsites(*F);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
635
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
636 // Functions that are readonly (or readnone) and nounwind and don't return
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
637 // a value can't capture arguments. Don't analyze them.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
638 if (F->onlyReadsMemory() && F->doesNotThrow() &&
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
639 F->getReturnType()->isVoidTy()) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
640 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
641 ++A) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
642 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
643 A->addAttr(Attribute::NoCapture);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
644 ++NumNoCapture;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
645 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
646 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
647 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
648 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
649 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
650
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
651 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
652 ++A) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
653 if (!A->getType()->isPointerTy())
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
654 continue;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
655 bool HasNonLocalUses = false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
656 if (!A->hasNoCaptureAttr()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
657 ArgumentUsesTracker Tracker(SCCNodes);
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
658 PointerMayBeCaptured(&*A, &Tracker);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
659 if (!Tracker.Captured) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
660 if (Tracker.Uses.empty()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
661 // If it's trivially not captured, mark it nocapture now.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
662 A->addAttr(Attribute::NoCapture);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
663 ++NumNoCapture;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
664 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
665 } else {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
666 // If it's not trivially captured and not trivially not captured,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
667 // then it must be calling into another function in our SCC. Save
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
668 // its particulars for Argument-SCC analysis later.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
669 ArgumentGraphNode *Node = AG[&*A];
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
670 for (Argument *Use : Tracker.Uses) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
671 Node->Uses.push_back(AG[Use]);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
672 if (Use != &*A)
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
673 HasNonLocalUses = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
674 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
675 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
676 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
677 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
678 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
679 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
680 // Can we determine that it's readonly/readnone without doing an SCC?
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
681 // Note that we don't allow any calls at all here, or else our result
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
682 // will be dependent on the iteration order through the functions in the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
683 // SCC.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
684 SmallPtrSet<Argument *, 8> Self;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
685 Self.insert(&*A);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
686 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
687 if (R != Attribute::None) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
688 A->addAttr(R);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
689 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
690 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
691 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
692 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
693 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
694 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
695
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
696 // The graph we've collected is partial because we stopped scanning for
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
697 // argument uses once we solved the argument trivially. These partial nodes
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
698 // show up as ArgumentGraphNode objects with an empty Uses list, and for
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
699 // these nodes the final decision about whether they capture has already been
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
700 // made. If the definition doesn't have a 'nocapture' attribute by now, it
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
701 // captures.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
702
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
703 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
704 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
705 if (ArgumentSCC.size() == 1) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
706 if (!ArgumentSCC[0]->Definition)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
707 continue; // synthetic root node
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
708
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
709 // eg. "void f(int* x) { if (...) f(x); }"
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
710 if (ArgumentSCC[0]->Uses.size() == 1 &&
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
711 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
712 Argument *A = ArgumentSCC[0]->Definition;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
713 A->addAttr(Attribute::NoCapture);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
714 ++NumNoCapture;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
715 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
716 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
717 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
718 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
719
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
720 bool SCCCaptured = false;
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
721 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
722 I != E && !SCCCaptured; ++I) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
723 ArgumentGraphNode *Node = *I;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
724 if (Node->Uses.empty()) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
725 if (!Node->Definition->hasNoCaptureAttr())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
726 SCCCaptured = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
727 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
728 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
729 if (SCCCaptured)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
730 continue;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
731
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
732 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
733 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
734 // quickly looking up whether a given Argument is in this ArgumentSCC.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
735 for (ArgumentGraphNode *I : ArgumentSCC) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
736 ArgumentSCCNodes.insert(I->Definition);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
737 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
738
77
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
739 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
54457678186b LLVM 3.6
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 33
diff changeset
740 I != E && !SCCCaptured; ++I) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
741 ArgumentGraphNode *N = *I;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
742 for (ArgumentGraphNode *Use : N->Uses) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
743 Argument *A = Use->Definition;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
744 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
745 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
746 SCCCaptured = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
747 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
748 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
749 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
750 if (SCCCaptured)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
751 continue;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
752
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
753 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
754 Argument *A = ArgumentSCC[i]->Definition;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
755 A->addAttr(Attribute::NoCapture);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
756 ++NumNoCapture;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
757 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
758 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
759
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
760 // We also want to compute readonly/readnone. With a small number of false
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
761 // negatives, we can assume that any pointer which is captured isn't going
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
762 // to be provably readonly or readnone, since by definition we can't
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
763 // analyze all uses of a captured pointer.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
764 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
765 // The false negatives happen when the pointer is captured by a function
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
766 // that promises readonly/readnone behaviour on the pointer, then the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
767 // pointer's lifetime ends before anything that writes to arbitrary memory.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
768 // Also, a readonly/readnone pointer may be returned, but returning a
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
769 // pointer is capturing it.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
770
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
771 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
772 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
773 Argument *A = ArgumentSCC[i]->Definition;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
774 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
775 if (K == Attribute::ReadNone)
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
776 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
777 if (K == Attribute::ReadOnly) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
778 ReadAttr = Attribute::ReadOnly;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
779 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
780 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
781 ReadAttr = K;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
782 break;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
783 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
784
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
785 if (ReadAttr != Attribute::None) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
786 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
787 Argument *A = ArgumentSCC[i]->Definition;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
788 // Clear out existing readonly/readnone attributes
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
789 A->removeAttr(Attribute::ReadOnly);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
790 A->removeAttr(Attribute::ReadNone);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
791 A->addAttr(ReadAttr);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
792 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
793 Changed = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
794 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
795 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
796 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
797
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
798 return Changed;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
799 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
800
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
801 /// Tests whether a function is "malloc-like".
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
802 ///
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
803 /// A function is "malloc-like" if it returns either null or a pointer that
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
804 /// doesn't alias any other pointer visible to the caller.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
805 static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
806 SmallSetVector<Value *, 8> FlowsToReturn;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
807 for (BasicBlock &BB : *F)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
808 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
809 FlowsToReturn.insert(Ret->getReturnValue());
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
810
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
811 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
812 Value *RetVal = FlowsToReturn[i];
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
813
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
814 if (Constant *C = dyn_cast<Constant>(RetVal)) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
815 if (!C->isNullValue() && !isa<UndefValue>(C))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
816 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
817
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
818 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
819 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
820
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
821 if (isa<Argument>(RetVal))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
822 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
823
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
824 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
825 switch (RVI->getOpcode()) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
826 // Extend the analysis by looking upwards.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
827 case Instruction::BitCast:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
828 case Instruction::GetElementPtr:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
829 case Instruction::AddrSpaceCast:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
830 FlowsToReturn.insert(RVI->getOperand(0));
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
831 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
832 case Instruction::Select: {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
833 SelectInst *SI = cast<SelectInst>(RVI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
834 FlowsToReturn.insert(SI->getTrueValue());
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
835 FlowsToReturn.insert(SI->getFalseValue());
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
836 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
837 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
838 case Instruction::PHI: {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
839 PHINode *PN = cast<PHINode>(RVI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
840 for (Value *IncValue : PN->incoming_values())
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
841 FlowsToReturn.insert(IncValue);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
842 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
843 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
844
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
845 // Check whether the pointer came from an allocation.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
846 case Instruction::Alloca:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
847 break;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
848 case Instruction::Call:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
849 case Instruction::Invoke: {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
850 CallSite CS(RVI);
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
851 if (CS.hasRetAttr(Attribute::NoAlias))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
852 break;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
853 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
854 break;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
855 LLVM_FALLTHROUGH;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
856 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
857 default:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
858 return false; // Did not come from an allocation.
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
859 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
860
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
861 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
862 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
863 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
864
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
865 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
866 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
867
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
868 /// Deduce noalias attributes for the SCC.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
869 static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
870 // Check each function in turn, determining which functions return noalias
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
871 // pointers.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
872 for (Function *F : SCCNodes) {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
873 // Already noalias.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
874 if (F->returnDoesNotAlias())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
875 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
876
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
877 // We can infer and propagate function attributes only when we know that the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
878 // definition we'll get at link time is *exactly* the definition we see now.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
879 // For more details, see GlobalValue::mayBeDerefined.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
880 if (!F->hasExactDefinition())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
881 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
882
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
883 // We annotate noalias return values, which are only applicable to
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
884 // pointer types.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
885 if (!F->getReturnType()->isPointerTy())
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
886 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
887
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
888 if (!isFunctionMallocLike(F, SCCNodes))
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
889 return false;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
890 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
891
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
892 bool MadeChange = false;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
893 for (Function *F : SCCNodes) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
894 if (F->returnDoesNotAlias() ||
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
895 !F->getReturnType()->isPointerTy())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
896 continue;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
897
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
898 F->setReturnDoesNotAlias();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
899 ++NumNoAlias;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
900 MadeChange = true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
901 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
902
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
903 return MadeChange;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
904 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
905
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
906 /// Tests whether this function is known to not return null.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
907 ///
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
908 /// Requires that the function returns a pointer.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
909 ///
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
910 /// Returns true if it believes the function will not return a null, and sets
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
911 /// \p Speculative based on whether the returned conclusion is a speculative
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
912 /// conclusion due to SCC calls.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
913 static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
914 bool &Speculative) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
915 assert(F->getReturnType()->isPointerTy() &&
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
916 "nonnull only meaningful on pointer types");
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
917 Speculative = false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
918
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
919 SmallSetVector<Value *, 8> FlowsToReturn;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
920 for (BasicBlock &BB : *F)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
921 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
922 FlowsToReturn.insert(Ret->getReturnValue());
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
923
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
924 auto &DL = F->getParent()->getDataLayout();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
925
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
926 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
927 Value *RetVal = FlowsToReturn[i];
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
928
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
929 // If this value is locally known to be non-null, we're good
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
930 if (isKnownNonZero(RetVal, DL))
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
931 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
932
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
933 // Otherwise, we need to look upwards since we can't make any local
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
934 // conclusions.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
935 Instruction *RVI = dyn_cast<Instruction>(RetVal);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
936 if (!RVI)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
937 return false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
938 switch (RVI->getOpcode()) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
939 // Extend the analysis by looking upwards.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
940 case Instruction::BitCast:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
941 case Instruction::GetElementPtr:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
942 case Instruction::AddrSpaceCast:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
943 FlowsToReturn.insert(RVI->getOperand(0));
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
944 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
945 case Instruction::Select: {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
946 SelectInst *SI = cast<SelectInst>(RVI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
947 FlowsToReturn.insert(SI->getTrueValue());
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
948 FlowsToReturn.insert(SI->getFalseValue());
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
949 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
950 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
951 case Instruction::PHI: {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
952 PHINode *PN = cast<PHINode>(RVI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
953 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
954 FlowsToReturn.insert(PN->getIncomingValue(i));
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
955 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
956 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
957 case Instruction::Call:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
958 case Instruction::Invoke: {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
959 CallSite CS(RVI);
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
960 Function *Callee = CS.getCalledFunction();
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
961 // A call to a node within the SCC is assumed to return null until
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
962 // proven otherwise
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
963 if (Callee && SCCNodes.count(Callee)) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
964 Speculative = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
965 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
966 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
967 return false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
968 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
969 default:
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
970 return false; // Unknown source, may be null
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
971 };
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
972 llvm_unreachable("should have either continued or returned");
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
973 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
974
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
975 return true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
976 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
977
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
978 /// Deduce nonnull attributes for the SCC.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
979 static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
980 // Speculative that all functions in the SCC return only nonnull
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
981 // pointers. We may refute this as we analyze functions.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
982 bool SCCReturnsNonNull = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
983
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
984 bool MadeChange = false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
985
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
986 // Check each function in turn, determining which functions return nonnull
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
987 // pointers.
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
988 for (Function *F : SCCNodes) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
989 // Already nonnull.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
990 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
991 Attribute::NonNull))
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
992 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
993
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
994 // We can infer and propagate function attributes only when we know that the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
995 // definition we'll get at link time is *exactly* the definition we see now.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
996 // For more details, see GlobalValue::mayBeDerefined.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
997 if (!F->hasExactDefinition())
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
998 return false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
999
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1000 // We annotate nonnull return values, which are only applicable to
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1001 // pointer types.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1002 if (!F->getReturnType()->isPointerTy())
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1003 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1004
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1005 bool Speculative = false;
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1006 if (isReturnNonNull(F, SCCNodes, Speculative)) {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1007 if (!Speculative) {
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1008 // Mark the function eagerly since we may discover a function
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1009 // which prevents us from speculating about the entire SCC
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1010 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1011 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1012 ++NumNonNullReturn;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1013 MadeChange = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1014 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1015 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1016 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1017 // At least one function returns something which could be null, can't
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1018 // speculate any more.
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1019 SCCReturnsNonNull = false;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1020 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1021
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1022 if (SCCReturnsNonNull) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1023 for (Function *F : SCCNodes) {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1024 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1025 Attribute::NonNull) ||
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1026 !F->getReturnType()->isPointerTy())
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1027 continue;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1028
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1029 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1030 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1031 ++NumNonNullReturn;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1032 MadeChange = true;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1033 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1034 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1035
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1036 return MadeChange;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1037 }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
1038
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1039 /// Remove the convergent attribute from all functions in the SCC if every
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1040 /// callsite within the SCC is not convergent (except for calls to functions
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1041 /// within the SCC). Returns true if changes were made.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1042 static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1043 // For every function in SCC, ensure that either
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1044 // * it is not convergent, or
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1045 // * we can remove its convergent attribute.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1046 bool HasConvergentFn = false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1047 for (Function *F : SCCNodes) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1048 if (!F->isConvergent()) continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1049 HasConvergentFn = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1050
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1051 // Can't remove convergent from function declarations.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1052 if (F->isDeclaration()) return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1053
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1054 // Can't remove convergent if any of our functions has a convergent call to a
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1055 // function not in the SCC.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1056 for (Instruction &I : instructions(*F)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1057 CallSite CS(&I);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1058 // Bail if CS is a convergent call to a function not in the SCC.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1059 if (CS && CS.isConvergent() &&
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1060 SCCNodes.count(CS.getCalledFunction()) == 0)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1061 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1062 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1063 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1064
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1065 // If the SCC doesn't have any convergent functions, we have nothing to do.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1066 if (!HasConvergentFn) return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1067
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1068 // If we got here, all of the calls the SCC makes to functions not in the SCC
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1069 // are non-convergent. Therefore all of the SCC's functions can also be made
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1070 // non-convergent. We'll remove the attr from the callsites in
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1071 // InstCombineCalls.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1072 for (Function *F : SCCNodes) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1073 if (!F->isConvergent()) continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1074
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1075 DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName()
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1076 << "\n");
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1077 F->setNotConvergent();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1078 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1079 return true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1080 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1081
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1082 static bool setDoesNotRecurse(Function &F) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1083 if (F.doesNotRecurse())
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1084 return false;
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1085 F.setDoesNotRecurse();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1086 ++NumNoRecurse;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1087 return true;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1088 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1089
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1090 static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1091 // Try and identify functions that do not recurse.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1092
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1093 // If the SCC contains multiple nodes we know for sure there is recursion.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1094 if (SCCNodes.size() != 1)
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1095 return false;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1096
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1097 Function *F = *SCCNodes.begin();
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1098 if (!F || F->isDeclaration() || F->doesNotRecurse())
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1099 return false;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1101 // If all of the calls in F are identifiable and are to norecurse functions, F
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1102 // is norecurse. This check also detects self-recursion as F is not currently
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1103 // marked norecurse, so any called from F to F will not be marked norecurse.
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1104 for (Instruction &I : instructions(*F))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1105 if (auto CS = CallSite(&I)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1106 Function *Callee = CS.getCalledFunction();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1107 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1108 // Function calls a potentially recursive function.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1109 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1110 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1111
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1112 // Every call was to a non-recursive function other than this function, and
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1113 // we have no indirect recursion as the SCC size is one. This function cannot
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1114 // recurse.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1115 return setDoesNotRecurse(*F);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1116 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1117
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1118 PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1119 CGSCCAnalysisManager &AM,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1120 LazyCallGraph &CG,
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1121 CGSCCUpdateResult &) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1122 FunctionAnalysisManager &FAM =
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1123 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1124
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1125 // We pass a lambda into functions to wire them up to the analysis manager
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1126 // for getting function analyses.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1127 auto AARGetter = [&](Function &F) -> AAResults & {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1128 return FAM.getResult<AAManager>(F);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1129 };
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1130
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1131 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1132 // any external or opt-none nodes that will prevent us from optimizing any
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1133 // part of the SCC.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1134 SCCNodeSet SCCNodes;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1135 bool HasUnknownCall = false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1136 for (LazyCallGraph::Node &N : C) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1137 Function &F = N.getFunction();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1138 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1139 // Treat any function we're trying not to optimize as if it were an
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1140 // indirect call and omit it from the node set used below.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1141 HasUnknownCall = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1142 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1143 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1144 // Track whether any functions in this SCC have an unknown call edge.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1145 // Note: if this is ever a performance hit, we can common it with
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1146 // subsequent routines which also do scans over the instructions of the
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1147 // function.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1148 if (!HasUnknownCall)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1149 for (Instruction &I : instructions(F))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1150 if (auto CS = CallSite(&I))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1151 if (!CS.getCalledFunction()) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1152 HasUnknownCall = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1153 break;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1154 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1155
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1156 SCCNodes.insert(&F);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1157 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1158
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1159 bool Changed = false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1160 Changed |= addArgumentReturnedAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1161 Changed |= addReadAttrs(SCCNodes, AARGetter);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1162 Changed |= addArgumentAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1163
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1164 // If we have no external nodes participating in the SCC, we can deduce some
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1165 // more precise attributes as well.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1166 if (!HasUnknownCall) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1167 Changed |= addNoAliasAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1168 Changed |= addNonNullAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1169 Changed |= removeConvergentAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1170 Changed |= addNoRecurseAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1171 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1172
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1173 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1174 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1175
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1176 namespace {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1177
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1178 struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1179 // Pass identification, replacement for typeid
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1180 static char ID;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1181
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1182 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1183 initializePostOrderFunctionAttrsLegacyPassPass(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1184 *PassRegistry::getPassRegistry());
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1185 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1186
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1187 bool runOnSCC(CallGraphSCC &SCC) override;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1188
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1189 void getAnalysisUsage(AnalysisUsage &AU) const override {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1190 AU.setPreservesCFG();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1191 AU.addRequired<AssumptionCacheTracker>();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1192 getAAResultsAnalysisUsage(AU);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1193 CallGraphSCCPass::getAnalysisUsage(AU);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1194 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1195 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1196
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1197 } // end anonymous namespace
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1198
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1199 char PostOrderFunctionAttrsLegacyPass::ID = 0;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1200 INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1201 "Deduce function attributes", false, false)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1202 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1203 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1204 INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1205 "Deduce function attributes", false, false)
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1206
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1207 Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1208 return new PostOrderFunctionAttrsLegacyPass();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1209 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1210
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1211 template <typename AARGetterT>
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1212 static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1213 bool Changed = false;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1214
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1215 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1216 // whether a given CallGraphNode is in this SCC. Also track whether there are
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1217 // any external or opt-none nodes that will prevent us from optimizing any
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1218 // part of the SCC.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1219 SCCNodeSet SCCNodes;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1220 bool ExternalNode = false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1221 for (CallGraphNode *I : SCC) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1222 Function *F = I->getFunction();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1223 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1224 // External node or function we're trying not to optimize - we both avoid
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1225 // transform them and avoid leveraging information they provide.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1226 ExternalNode = true;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1227 continue;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1228 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1229
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1230 SCCNodes.insert(F);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1231 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1232
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1233 // Skip it if the SCC only contains optnone functions.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1234 if (SCCNodes.empty())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1235 return Changed;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1236
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1237 Changed |= addArgumentReturnedAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1238 Changed |= addReadAttrs(SCCNodes, AARGetter);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1239 Changed |= addArgumentAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1240
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1241 // If we have no external nodes participating in the SCC, we can deduce some
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1242 // more precise attributes as well.
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1243 if (!ExternalNode) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1244 Changed |= addNoAliasAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1245 Changed |= addNonNullAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1246 Changed |= removeConvergentAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1247 Changed |= addNoRecurseAttrs(SCCNodes);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1248 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1249
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1250 return Changed;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1251 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1252
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1253 bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1254 if (skipSCC(SCC))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1255 return false;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1256 return runImpl(SCC, LegacyAARGetter(*this));
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1257 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1258
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1259 namespace {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1260
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1261 struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1262 // Pass identification, replacement for typeid
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1263 static char ID;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1264
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1265 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1266 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1267 *PassRegistry::getPassRegistry());
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1268 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1269
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1270 bool runOnModule(Module &M) override;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1271
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1272 void getAnalysisUsage(AnalysisUsage &AU) const override {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1273 AU.setPreservesCFG();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1274 AU.addRequired<CallGraphWrapperPass>();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1275 AU.addPreserved<CallGraphWrapperPass>();
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1276 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1277 };
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1278
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1279 } // end anonymous namespace
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1280
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1281 char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1282
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1283 INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1284 "Deduce function attributes in RPO", false, false)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1285 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1286 INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1287 "Deduce function attributes in RPO", false, false)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1288
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1289 Pass *llvm::createReversePostOrderFunctionAttrsPass() {
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1290 return new ReversePostOrderFunctionAttrsLegacyPass();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1291 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1292
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1293 static bool addNoRecurseAttrsTopDown(Function &F) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1294 // We check the preconditions for the function prior to calling this to avoid
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1295 // the cost of building up a reversible post-order list. We assert them here
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1296 // to make sure none of the invariants this relies on were violated.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1297 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1298 assert(!F.doesNotRecurse() &&
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1299 "This function has already been deduced as norecurs!");
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1300 assert(F.hasInternalLinkage() &&
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1301 "Can only do top-down deduction for internal linkage functions!");
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1302
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1303 // If F is internal and all of its uses are calls from a non-recursive
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1304 // functions, then none of its calls could in fact recurse without going
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1305 // through a function marked norecurse, and so we can mark this function too
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1306 // as norecurse. Note that the uses must actually be calls -- otherwise
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1307 // a pointer to this function could be returned from a norecurse function but
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1308 // this function could be recursively (indirectly) called. Note that this
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1309 // also detects if F is directly recursive as F is not yet marked as
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1310 // a norecurse function.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1311 for (auto *U : F.users()) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1312 auto *I = dyn_cast<Instruction>(U);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1313 if (!I)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1314 return false;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1315 CallSite CS(I);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1316 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1317 return false;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1318 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1319 return setDoesNotRecurse(F);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1320 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1321
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1322 static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1323 // We only have a post-order SCC traversal (because SCCs are inherently
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1324 // discovered in post-order), so we accumulate them in a vector and then walk
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1325 // it in reverse. This is simpler than using the RPO iterator infrastructure
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1326 // because we need to combine SCC detection and the PO walk of the call
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1327 // graph. We can also cheat egregiously because we're primarily interested in
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1328 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1329 // with multiple functions in them will clearly be recursive.
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1330 SmallVector<Function *, 16> Worklist;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1331 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1332 if (I->size() != 1)
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1333 continue;
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1334
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1335 Function *F = I->front()->getFunction();
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1336 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1337 F->hasInternalLinkage())
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1338 Worklist.push_back(F);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1339 }
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1340
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1341 bool Changed = false;
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1342 for (auto *F : llvm::reverse(Worklist))
100
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1343 Changed |= addNoRecurseAttrsTopDown(*F);
7d135dc70f03 LLVM 3.9
Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
parents: 95
diff changeset
1344
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1345 return Changed;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1346 }
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1347
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1348 bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1349 if (skipModule(M))
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1350 return false;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1351
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1352 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1353
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1354 return deduceFunctionAttributeInRPO(M, CG);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1355 }
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1356
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1357 PreservedAnalyses
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1358 ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1359 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1360
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1361 if (!deduceFunctionAttributeInRPO(M, CG))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
1362 return PreservedAnalyses::all();
120
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1363
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1364 PreservedAnalyses PA;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1365 PA.preserve<CallGraphAnalysis>();
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1366 return PA;
1172e4bd9c6f update 4.0.0
mir3636
parents: 100
diff changeset
1367 }