1 | //===- CalledValuePropagation.cpp - Propagate called values -----*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements a transformation that attaches !callees metadata to |
10 | // indirect call sites. For a given call site, the metadata, if present, |
11 | // indicates the set of functions the call site could possibly target at |
12 | // run-time. This metadata is added to indirect call sites when the set of |
13 | // possible targets can be determined by analysis and is known to be small. The |
14 | // analysis driving the transformation is similar to constant propagation and |
15 | // makes uses of the generic sparse propagation solver. |
16 | // |
17 | //===----------------------------------------------------------------------===// |
18 | |
19 | #include "llvm/Transforms/IPO/CalledValuePropagation.h" |
20 | #include "llvm/Analysis/SparsePropagation.h" |
21 | #include "llvm/Analysis/ValueLatticeUtils.h" |
22 | #include "llvm/IR/Constants.h" |
23 | #include "llvm/IR/MDBuilder.h" |
24 | #include "llvm/IR/Module.h" |
25 | #include "llvm/Support/CommandLine.h" |
26 | #include "llvm/Transforms/IPO.h" |
27 | |
28 | using namespace llvm; |
29 | |
30 | #define DEBUG_TYPE "called-value-propagation" |
31 | |
32 | /// The maximum number of functions to track per lattice value. Once the number |
33 | /// of functions a call site can possibly target exceeds this threshold, it's |
34 | /// lattice value becomes overdefined. The number of possible lattice values is |
35 | /// bounded by Ch(F, M), where F is the number of functions in the module and M |
36 | /// is MaxFunctionsPerValue. As such, this value should be kept very small. We |
37 | /// likely can't do anything useful for call sites with a large number of |
38 | /// possible targets, anyway. |
39 | static cl::opt<unsigned> MaxFunctionsPerValue( |
40 | "cvp-max-functions-per-value" , cl::Hidden, cl::init(Val: 4), |
41 | cl::desc("The maximum number of functions to track per lattice value" )); |
42 | |
43 | namespace { |
44 | /// To enable interprocedural analysis, we assign LLVM values to the following |
45 | /// groups. The register group represents SSA registers, the return group |
46 | /// represents the return values of functions, and the memory group represents |
47 | /// in-memory values. An LLVM Value can technically be in more than one group. |
48 | /// It's necessary to distinguish these groups so we can, for example, track a |
49 | /// global variable separately from the value stored at its location. |
50 | enum class IPOGrouping { Register, Return, Memory }; |
51 | |
52 | /// Our LatticeKeys are PointerIntPairs composed of LLVM values and groupings. |
53 | using CVPLatticeKey = PointerIntPair<Value *, 2, IPOGrouping>; |
54 | |
55 | /// The lattice value type used by our custom lattice function. It holds the |
56 | /// lattice state, and a set of functions. |
57 | class CVPLatticeVal { |
58 | public: |
59 | /// The states of the lattice values. Only the FunctionSet state is |
60 | /// interesting. It indicates the set of functions to which an LLVM value may |
61 | /// refer. |
62 | enum CVPLatticeStateTy { Undefined, FunctionSet, Overdefined, Untracked }; |
63 | |
64 | /// Comparator for sorting the functions set. We want to keep the order |
65 | /// deterministic for testing, etc. |
66 | struct Compare { |
67 | bool operator()(const Function *LHS, const Function *RHS) const { |
68 | return LHS->getName() < RHS->getName(); |
69 | } |
70 | }; |
71 | |
72 | CVPLatticeVal() = default; |
73 | CVPLatticeVal(CVPLatticeStateTy LatticeState) : LatticeState(LatticeState) {} |
74 | CVPLatticeVal(std::vector<Function *> &&Functions) |
75 | : LatticeState(FunctionSet), Functions(std::move(Functions)) { |
76 | assert(llvm::is_sorted(this->Functions, Compare())); |
77 | } |
78 | |
79 | /// Get a reference to the functions held by this lattice value. The number |
80 | /// of functions will be zero for states other than FunctionSet. |
81 | const std::vector<Function *> &getFunctions() const { |
82 | return Functions; |
83 | } |
84 | |
85 | /// Returns true if the lattice value is in the FunctionSet state. |
86 | bool isFunctionSet() const { return LatticeState == FunctionSet; } |
87 | |
88 | bool operator==(const CVPLatticeVal &RHS) const { |
89 | return LatticeState == RHS.LatticeState && Functions == RHS.Functions; |
90 | } |
91 | |
92 | bool operator!=(const CVPLatticeVal &RHS) const { |
93 | return LatticeState != RHS.LatticeState || Functions != RHS.Functions; |
94 | } |
95 | |
96 | private: |
97 | /// Holds the state this lattice value is in. |
98 | CVPLatticeStateTy LatticeState = Undefined; |
99 | |
100 | /// Holds functions indicating the possible targets of call sites. This set |
101 | /// is empty for lattice values in the undefined, overdefined, and untracked |
102 | /// states. The maximum size of the set is controlled by |
103 | /// MaxFunctionsPerValue. Since most LLVM values are expected to be in |
104 | /// uninteresting states (i.e., overdefined), CVPLatticeVal objects should be |
105 | /// small and efficiently copyable. |
106 | // FIXME: This could be a TinyPtrVector and/or merge with LatticeState. |
107 | std::vector<Function *> Functions; |
108 | }; |
109 | |
110 | /// The custom lattice function used by the generic sparse propagation solver. |
111 | /// It handles merging lattice values and computing new lattice values for |
112 | /// constants, arguments, values returned from trackable functions, and values |
113 | /// located in trackable global variables. It also computes the lattice values |
114 | /// that change as a result of executing instructions. |
115 | class CVPLatticeFunc |
116 | : public AbstractLatticeFunction<CVPLatticeKey, CVPLatticeVal> { |
117 | public: |
118 | CVPLatticeFunc() |
119 | : AbstractLatticeFunction(CVPLatticeVal(CVPLatticeVal::Undefined), |
120 | CVPLatticeVal(CVPLatticeVal::Overdefined), |
121 | CVPLatticeVal(CVPLatticeVal::Untracked)) {} |
122 | |
123 | /// Compute and return a CVPLatticeVal for the given CVPLatticeKey. |
124 | CVPLatticeVal ComputeLatticeVal(CVPLatticeKey Key) override { |
125 | switch (Key.getInt()) { |
126 | case IPOGrouping::Register: |
127 | if (isa<Instruction>(Val: Key.getPointer())) { |
128 | return getUndefVal(); |
129 | } else if (auto *A = dyn_cast<Argument>(Val: Key.getPointer())) { |
130 | if (canTrackArgumentsInterprocedurally(F: A->getParent())) |
131 | return getUndefVal(); |
132 | } else if (auto *C = dyn_cast<Constant>(Val: Key.getPointer())) { |
133 | return computeConstant(C); |
134 | } |
135 | return getOverdefinedVal(); |
136 | case IPOGrouping::Memory: |
137 | case IPOGrouping::Return: |
138 | if (auto *GV = dyn_cast<GlobalVariable>(Val: Key.getPointer())) { |
139 | if (canTrackGlobalVariableInterprocedurally(GV)) |
140 | return computeConstant(C: GV->getInitializer()); |
141 | } else if (auto *F = cast<Function>(Val: Key.getPointer())) |
142 | if (canTrackReturnsInterprocedurally(F)) |
143 | return getUndefVal(); |
144 | } |
145 | return getOverdefinedVal(); |
146 | } |
147 | |
148 | /// Merge the two given lattice values. The interesting cases are merging two |
149 | /// FunctionSet values and a FunctionSet value with an Undefined value. For |
150 | /// these cases, we simply union the function sets. If the size of the union |
151 | /// is greater than the maximum functions we track, the merged value is |
152 | /// overdefined. |
153 | CVPLatticeVal MergeValues(CVPLatticeVal X, CVPLatticeVal Y) override { |
154 | if (X == getOverdefinedVal() || Y == getOverdefinedVal()) |
155 | return getOverdefinedVal(); |
156 | if (X == getUndefVal() && Y == getUndefVal()) |
157 | return getUndefVal(); |
158 | std::vector<Function *> Union; |
159 | std::set_union(first1: X.getFunctions().begin(), last1: X.getFunctions().end(), |
160 | first2: Y.getFunctions().begin(), last2: Y.getFunctions().end(), |
161 | result: std::back_inserter(x&: Union), comp: CVPLatticeVal::Compare{}); |
162 | if (Union.size() > MaxFunctionsPerValue) |
163 | return getOverdefinedVal(); |
164 | return CVPLatticeVal(std::move(Union)); |
165 | } |
166 | |
167 | /// Compute the lattice values that change as a result of executing the given |
168 | /// instruction. The changed values are stored in \p ChangedValues. We handle |
169 | /// just a few kinds of instructions since we're only propagating values that |
170 | /// can be called. |
171 | void ComputeInstructionState( |
172 | Instruction &I, DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
173 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) override { |
174 | switch (I.getOpcode()) { |
175 | case Instruction::Call: |
176 | case Instruction::Invoke: |
177 | return visitCallBase(CB&: cast<CallBase>(Val&: I), ChangedValues, SS); |
178 | case Instruction::Load: |
179 | return visitLoad(I&: *cast<LoadInst>(Val: &I), ChangedValues, SS); |
180 | case Instruction::Ret: |
181 | return visitReturn(I&: *cast<ReturnInst>(Val: &I), ChangedValues, SS); |
182 | case Instruction::Select: |
183 | return visitSelect(I&: *cast<SelectInst>(Val: &I), ChangedValues, SS); |
184 | case Instruction::Store: |
185 | return visitStore(I&: *cast<StoreInst>(Val: &I), ChangedValues, SS); |
186 | default: |
187 | return visitInst(I, ChangedValues, SS); |
188 | } |
189 | } |
190 | |
191 | /// Print the given CVPLatticeVal to the specified stream. |
192 | void PrintLatticeVal(CVPLatticeVal LV, raw_ostream &OS) override { |
193 | if (LV == getUndefVal()) |
194 | OS << "Undefined " ; |
195 | else if (LV == getOverdefinedVal()) |
196 | OS << "Overdefined" ; |
197 | else if (LV == getUntrackedVal()) |
198 | OS << "Untracked " ; |
199 | else |
200 | OS << "FunctionSet" ; |
201 | } |
202 | |
203 | /// Print the given CVPLatticeKey to the specified stream. |
204 | void PrintLatticeKey(CVPLatticeKey Key, raw_ostream &OS) override { |
205 | if (Key.getInt() == IPOGrouping::Register) |
206 | OS << "<reg> " ; |
207 | else if (Key.getInt() == IPOGrouping::Memory) |
208 | OS << "<mem> " ; |
209 | else if (Key.getInt() == IPOGrouping::Return) |
210 | OS << "<ret> " ; |
211 | if (isa<Function>(Val: Key.getPointer())) |
212 | OS << Key.getPointer()->getName(); |
213 | else |
214 | OS << *Key.getPointer(); |
215 | } |
216 | |
217 | /// We collect a set of indirect calls when visiting call sites. This method |
218 | /// returns a reference to that set. |
219 | SmallPtrSetImpl<CallBase *> &getIndirectCalls() { return IndirectCalls; } |
220 | |
221 | private: |
222 | /// Holds the indirect calls we encounter during the analysis. We will attach |
223 | /// metadata to these calls after the analysis indicating the functions the |
224 | /// calls can possibly target. |
225 | SmallPtrSet<CallBase *, 32> IndirectCalls; |
226 | |
227 | /// Compute a new lattice value for the given constant. The constant, after |
228 | /// stripping any pointer casts, should be a Function. We ignore null |
229 | /// pointers as an optimization, since calling these values is undefined |
230 | /// behavior. |
231 | CVPLatticeVal computeConstant(Constant *C) { |
232 | if (isa<ConstantPointerNull>(Val: C)) |
233 | return CVPLatticeVal(CVPLatticeVal::FunctionSet); |
234 | if (auto *F = dyn_cast<Function>(Val: C->stripPointerCasts())) |
235 | return CVPLatticeVal({F}); |
236 | return getOverdefinedVal(); |
237 | } |
238 | |
239 | /// Handle return instructions. The function's return state is the merge of |
240 | /// the returned value state and the function's return state. |
241 | void visitReturn(ReturnInst &I, |
242 | DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
243 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) { |
244 | Function *F = I.getParent()->getParent(); |
245 | if (F->getReturnType()->isVoidTy()) |
246 | return; |
247 | auto RegI = CVPLatticeKey(I.getReturnValue(), IPOGrouping::Register); |
248 | auto RetF = CVPLatticeKey(F, IPOGrouping::Return); |
249 | ChangedValues[RetF] = |
250 | MergeValues(X: SS.getValueState(Key: RegI), Y: SS.getValueState(Key: RetF)); |
251 | } |
252 | |
253 | /// Handle call sites. The state of a called function's formal arguments is |
254 | /// the merge of the argument state with the call sites corresponding actual |
255 | /// argument state. The call site state is the merge of the call site state |
256 | /// with the returned value state of the called function. |
257 | void visitCallBase(CallBase &CB, |
258 | DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
259 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) { |
260 | Function *F = CB.getCalledFunction(); |
261 | auto RegI = CVPLatticeKey(&CB, IPOGrouping::Register); |
262 | |
263 | // If this is an indirect call, save it so we can quickly revisit it when |
264 | // attaching metadata. |
265 | if (!F) |
266 | IndirectCalls.insert(Ptr: &CB); |
267 | |
268 | // If we can't track the function's return values, there's nothing to do. |
269 | if (!F || !canTrackReturnsInterprocedurally(F)) { |
270 | // Void return, No need to create and update CVPLattice state as no one |
271 | // can use it. |
272 | if (CB.getType()->isVoidTy()) |
273 | return; |
274 | ChangedValues[RegI] = getOverdefinedVal(); |
275 | return; |
276 | } |
277 | |
278 | // Inform the solver that the called function is executable, and perform |
279 | // the merges for the arguments and return value. |
280 | SS.MarkBlockExecutable(BB: &F->front()); |
281 | auto RetF = CVPLatticeKey(F, IPOGrouping::Return); |
282 | for (Argument &A : F->args()) { |
283 | auto RegFormal = CVPLatticeKey(&A, IPOGrouping::Register); |
284 | auto RegActual = |
285 | CVPLatticeKey(CB.getArgOperand(i: A.getArgNo()), IPOGrouping::Register); |
286 | ChangedValues[RegFormal] = |
287 | MergeValues(X: SS.getValueState(Key: RegFormal), Y: SS.getValueState(Key: RegActual)); |
288 | } |
289 | |
290 | // Void return, No need to create and update CVPLattice state as no one can |
291 | // use it. |
292 | if (CB.getType()->isVoidTy()) |
293 | return; |
294 | |
295 | ChangedValues[RegI] = |
296 | MergeValues(X: SS.getValueState(Key: RegI), Y: SS.getValueState(Key: RetF)); |
297 | } |
298 | |
299 | /// Handle select instructions. The select instruction state is the merge the |
300 | /// true and false value states. |
301 | void visitSelect(SelectInst &I, |
302 | DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
303 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) { |
304 | auto RegI = CVPLatticeKey(&I, IPOGrouping::Register); |
305 | auto RegT = CVPLatticeKey(I.getTrueValue(), IPOGrouping::Register); |
306 | auto RegF = CVPLatticeKey(I.getFalseValue(), IPOGrouping::Register); |
307 | ChangedValues[RegI] = |
308 | MergeValues(X: SS.getValueState(Key: RegT), Y: SS.getValueState(Key: RegF)); |
309 | } |
310 | |
311 | /// Handle load instructions. If the pointer operand of the load is a global |
312 | /// variable, we attempt to track the value. The loaded value state is the |
313 | /// merge of the loaded value state with the global variable state. |
314 | void visitLoad(LoadInst &I, |
315 | DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
316 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) { |
317 | auto RegI = CVPLatticeKey(&I, IPOGrouping::Register); |
318 | if (auto *GV = dyn_cast<GlobalVariable>(Val: I.getPointerOperand())) { |
319 | auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory); |
320 | ChangedValues[RegI] = |
321 | MergeValues(X: SS.getValueState(Key: RegI), Y: SS.getValueState(Key: MemGV)); |
322 | } else { |
323 | ChangedValues[RegI] = getOverdefinedVal(); |
324 | } |
325 | } |
326 | |
327 | /// Handle store instructions. If the pointer operand of the store is a |
328 | /// global variable, we attempt to track the value. The global variable state |
329 | /// is the merge of the stored value state with the global variable state. |
330 | void visitStore(StoreInst &I, |
331 | DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
332 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) { |
333 | auto *GV = dyn_cast<GlobalVariable>(Val: I.getPointerOperand()); |
334 | if (!GV) |
335 | return; |
336 | auto RegI = CVPLatticeKey(I.getValueOperand(), IPOGrouping::Register); |
337 | auto MemGV = CVPLatticeKey(GV, IPOGrouping::Memory); |
338 | ChangedValues[MemGV] = |
339 | MergeValues(X: SS.getValueState(Key: RegI), Y: SS.getValueState(Key: MemGV)); |
340 | } |
341 | |
342 | /// Handle all other instructions. All other instructions are marked |
343 | /// overdefined. |
344 | void visitInst(Instruction &I, |
345 | DenseMap<CVPLatticeKey, CVPLatticeVal> &ChangedValues, |
346 | SparseSolver<CVPLatticeKey, CVPLatticeVal> &SS) { |
347 | // Simply bail if this instruction has no user. |
348 | if (I.use_empty()) |
349 | return; |
350 | auto RegI = CVPLatticeKey(&I, IPOGrouping::Register); |
351 | ChangedValues[RegI] = getOverdefinedVal(); |
352 | } |
353 | }; |
354 | } // namespace |
355 | |
356 | namespace llvm { |
357 | /// A specialization of LatticeKeyInfo for CVPLatticeKeys. The generic solver |
358 | /// must translate between LatticeKeys and LLVM Values when adding Values to |
359 | /// its work list and inspecting the state of control-flow related values. |
360 | template <> struct LatticeKeyInfo<CVPLatticeKey> { |
361 | static inline Value *getValueFromLatticeKey(CVPLatticeKey Key) { |
362 | return Key.getPointer(); |
363 | } |
364 | static inline CVPLatticeKey getLatticeKeyFromValue(Value *V) { |
365 | return CVPLatticeKey(V, IPOGrouping::Register); |
366 | } |
367 | }; |
368 | } // namespace llvm |
369 | |
370 | static bool runCVP(Module &M) { |
371 | // Our custom lattice function and generic sparse propagation solver. |
372 | CVPLatticeFunc Lattice; |
373 | SparseSolver<CVPLatticeKey, CVPLatticeVal> Solver(&Lattice); |
374 | |
375 | // For each function in the module, if we can't track its arguments, let the |
376 | // generic solver assume it is executable. |
377 | for (Function &F : M) |
378 | if (!F.isDeclaration() && !canTrackArgumentsInterprocedurally(F: &F)) |
379 | Solver.MarkBlockExecutable(BB: &F.front()); |
380 | |
381 | // Solver our custom lattice. In doing so, we will also build a set of |
382 | // indirect call sites. |
383 | Solver.Solve(); |
384 | |
385 | // Attach metadata to the indirect call sites that were collected indicating |
386 | // the set of functions they can possibly target. |
387 | bool Changed = false; |
388 | MDBuilder MDB(M.getContext()); |
389 | for (CallBase *C : Lattice.getIndirectCalls()) { |
390 | auto RegI = CVPLatticeKey(C->getCalledOperand(), IPOGrouping::Register); |
391 | CVPLatticeVal LV = Solver.getExistingValueState(Key: RegI); |
392 | if (!LV.isFunctionSet() || LV.getFunctions().empty()) |
393 | continue; |
394 | MDNode *Callees = MDB.createCallees(Callees: LV.getFunctions()); |
395 | C->setMetadata(KindID: LLVMContext::MD_callees, Node: Callees); |
396 | Changed = true; |
397 | } |
398 | |
399 | return Changed; |
400 | } |
401 | |
402 | PreservedAnalyses CalledValuePropagationPass::run(Module &M, |
403 | ModuleAnalysisManager &) { |
404 | runCVP(M); |
405 | return PreservedAnalyses::all(); |
406 | } |
407 | |