annotate include/llvm/Analysis/SparsePropagation.h @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children
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 //===- SparsePropagation.h - Sparse Conditional Property Propagation ------===//
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 //===----------------------------------------------------------------------===//
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 // This file implements an abstract sparse conditional propagation algorithm,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 // modeled after SCCP, but with a customizable lattice function.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 //
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
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 #ifndef LLVM_ANALYSIS_SPARSEPROPAGATION_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #define LLVM_ANALYSIS_SPARSEPROPAGATION_H
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
18 #include "llvm/IR/Instructions.h"
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
19 #include "llvm/Support/Debug.h"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include <set>
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
21
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
22 #define DEBUG_TYPE "sparseprop"
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 namespace llvm {
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
25
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
26 /// A template for translating between LLVM Values and LatticeKeys. Clients must
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
27 /// provide a specialization of LatticeKeyInfo for their LatticeKey type.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
28 template <class LatticeKey> struct LatticeKeyInfo {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
29 // static inline Value *getValueFromLatticeKey(LatticeKey Key);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
30 // static inline LatticeKey getLatticeKeyFromValue(Value *V);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
31 };
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
32
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
33 template <class LatticeKey, class LatticeVal,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
34 class KeyInfo = LatticeKeyInfo<LatticeKey>>
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
35 class SparseSolver;
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
36
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 /// AbstractLatticeFunction - This class is implemented by the dataflow instance
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
38 /// to specify what the lattice values are and how they handle merges etc. This
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
39 /// gives the client the power to compute lattice values from instructions,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
40 /// constants, etc. The current requirement is that lattice values must be
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
41 /// copyable. At the moment, nothing tries to avoid copying. Additionally,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
42 /// lattice keys must be able to be used as keys of a mapping data structure.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
43 /// Internally, the generic solver currently uses a DenseMap to map lattice keys
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
44 /// to lattice values. If the lattice key is a non-standard type, a
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
45 /// specialization of DenseMapInfo must be provided.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
46 template <class LatticeKey, class LatticeVal> class AbstractLatticeFunction {
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 private:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 LatticeVal UndefVal, OverdefinedVal, UntrackedVal;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
49
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 public:
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 AbstractLatticeFunction(LatticeVal undefVal, LatticeVal overdefinedVal,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 LatticeVal untrackedVal) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 UndefVal = undefVal;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 OverdefinedVal = overdefinedVal;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 UntrackedVal = untrackedVal;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 }
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
57
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
58 virtual ~AbstractLatticeFunction() = default;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
59
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 LatticeVal getUndefVal() const { return UndefVal; }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 LatticeVal getOverdefinedVal() const { return OverdefinedVal; }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 LatticeVal getUntrackedVal() const { return UntrackedVal; }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
63
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
64 /// IsUntrackedValue - If the specified LatticeKey is obviously uninteresting
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
65 /// to the analysis (i.e., it would always return UntrackedVal), this
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
66 /// function can return true to avoid pointless work.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
67 virtual bool IsUntrackedValue(LatticeKey Key) { return false; }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
68
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
69 /// ComputeLatticeVal - Compute and return a LatticeVal corresponding to the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
70 /// given LatticeKey.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
71 virtual LatticeVal ComputeLatticeVal(LatticeKey Key) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
72 return getOverdefinedVal();
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 /// IsSpecialCasedPHI - Given a PHI node, determine whether this PHI node is
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 /// one that the we want to handle through ComputeInstructionState.
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
77 virtual bool IsSpecialCasedPHI(PHINode *PN) { return false; }
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
78
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 /// MergeValues - Compute and return the merge of the two specified lattice
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 /// values. Merging should only move one direction down the lattice to
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 /// guarantee convergence (toward overdefined).
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 virtual LatticeVal MergeValues(LatticeVal X, LatticeVal Y) {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 return getOverdefinedVal(); // always safe, never useful.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
85
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
86 /// ComputeInstructionState - Compute the LatticeKeys that change as a result
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
87 /// of executing instruction \p I. Their associated LatticeVals are store in
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
88 /// \p ChangedValues.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
89 virtual void
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
90 ComputeInstructionState(Instruction &I,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
91 DenseMap<LatticeKey, LatticeVal> &ChangedValues,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
92 SparseSolver<LatticeKey, LatticeVal> &SS) = 0;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
93
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
94 /// PrintLatticeVal - Render the given LatticeVal to the specified stream.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
95 virtual void PrintLatticeVal(LatticeVal LV, raw_ostream &OS);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
96
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
97 /// PrintLatticeKey - Render the given LatticeKey to the specified stream.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
98 virtual void PrintLatticeKey(LatticeKey Key, raw_ostream &OS);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
99
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
100 /// GetValueFromLatticeVal - If the given LatticeVal is representable as an
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
101 /// LLVM value, return it; otherwise, return nullptr. If a type is given, the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
102 /// returned value must have the same type. This function is used by the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
103 /// generic solver in attempting to resolve branch and switch conditions.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
104 virtual Value *GetValueFromLatticeVal(LatticeVal LV, Type *Ty = nullptr) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
105 return nullptr;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 }
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 };
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 /// SparseSolver - This class is a general purpose solver for Sparse Conditional
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 /// Propagation with a programmable lattice function.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
111 template <class LatticeKey, class LatticeVal, class KeyInfo>
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 class SparseSolver {
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
113
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
114 /// LatticeFunc - This is the object that knows the lattice and how to
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 /// compute transfer functions.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
116 AbstractLatticeFunction<LatticeKey, LatticeVal> *LatticeFunc;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
117
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
118 /// ValueState - Holds the LatticeVals associated with LatticeKeys.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
119 DenseMap<LatticeKey, LatticeVal> ValueState;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
120
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
121 /// BBExecutable - Holds the basic blocks that are executable.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
122 SmallPtrSet<BasicBlock *, 16> BBExecutable;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
123
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
124 /// ValueWorkList - Holds values that should be processed.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
125 SmallVector<Value *, 64> ValueWorkList;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
126
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
127 /// BBWorkList - Holds basic blocks that should be processed.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
128 SmallVector<BasicBlock *, 64> BBWorkList;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
129
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
130 using Edge = std::pair<BasicBlock *, BasicBlock *>;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
131
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 /// KnownFeasibleEdges - Entries in this set are edges which have already had
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 /// PHI nodes retriggered.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134 std::set<Edge> KnownFeasibleEdges;
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 public:
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
137 explicit SparseSolver(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
138 AbstractLatticeFunction<LatticeKey, LatticeVal> *Lattice)
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
139 : LatticeFunc(Lattice) {}
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
140 SparseSolver(const SparseSolver &) = delete;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
141 SparseSolver &operator=(const SparseSolver &) = delete;
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
142
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 /// Solve - Solve for constants and executable blocks.
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
144 void Solve();
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
145
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
146 void Print(raw_ostream &OS) const;
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
148 /// getExistingValueState - Return the LatticeVal object corresponding to the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
149 /// given value from the ValueState map. If the value is not in the map,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
150 /// UntrackedVal is returned, unlike the getValueState method.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
151 LatticeVal getExistingValueState(LatticeKey Key) const {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
152 auto I = ValueState.find(Key);
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 return I != ValueState.end() ? I->second : LatticeFunc->getUntrackedVal();
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
155
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
156 /// getValueState - Return the LatticeVal object corresponding to the given
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
157 /// value from the ValueState map. If the value is not in the map, its state
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
158 /// is initialized.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
159 LatticeVal getValueState(LatticeKey Key);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
160
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 /// isEdgeFeasible - Return true if the control flow edge from the 'From'
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 /// basic block to the 'To' basic block is currently feasible. If
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 /// AggressiveUndef is true, then this treats values with unknown lattice
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 /// values as undefined. This is generally only useful when solving the
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 /// lattice, not when querying it.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 bool isEdgeFeasible(BasicBlock *From, BasicBlock *To,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 bool AggressiveUndef = false);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 /// isBlockExecutable - Return true if there are any known feasible
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 /// edges into the basic block. This is generally only useful when
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 /// querying the lattice.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 bool isBlockExecutable(BasicBlock *BB) const {
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 return BBExecutable.count(BB);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 }
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
175
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 /// MarkBlockExecutable - This method can be used by clients to mark all of
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 /// the blocks that are known to be intrinsically live in the processed unit.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 void MarkBlockExecutable(BasicBlock *BB);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
179
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
180 private:
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
181 /// UpdateState - When the state of some LatticeKey is potentially updated to
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
182 /// the given LatticeVal, this function notices and adds the LLVM value
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
183 /// corresponding the key to the work list, if needed.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
184 void UpdateState(LatticeKey Key, LatticeVal LV);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
185
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 /// markEdgeExecutable - Mark a basic block as executable, adding it to the BB
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 /// work list if it is not already executable.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 void markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
189
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 /// getFeasibleSuccessors - Return a vector of booleans to indicate which
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 /// successors are reachable from a given terminator instruction.
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 void getFeasibleSuccessors(TerminatorInst &TI, SmallVectorImpl<bool> &Succs,
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 bool AggressiveUndef);
95
afa8332a0e37 LLVM 3.8
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents: 83
diff changeset
194
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 void visitInst(Instruction &I);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 void visitPHINode(PHINode &I);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 void visitTerminatorInst(TerminatorInst &TI);
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 };
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
200 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
201 // AbstractLatticeFunction Implementation
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
202 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
203
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
204 template <class LatticeKey, class LatticeVal>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
205 void AbstractLatticeFunction<LatticeKey, LatticeVal>::PrintLatticeVal(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
206 LatticeVal V, raw_ostream &OS) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
207 if (V == UndefVal)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
208 OS << "undefined";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
209 else if (V == OverdefinedVal)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
210 OS << "overdefined";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
211 else if (V == UntrackedVal)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
212 OS << "untracked";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
213 else
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
214 OS << "unknown lattice value";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
215 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
216
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
217 template <class LatticeKey, class LatticeVal>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
218 void AbstractLatticeFunction<LatticeKey, LatticeVal>::PrintLatticeKey(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
219 LatticeKey Key, raw_ostream &OS) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
220 OS << "unknown lattice key";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
221 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
222
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
223 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
224 // SparseSolver Implementation
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
225 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
226
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
227 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
228 LatticeVal
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
229 SparseSolver<LatticeKey, LatticeVal, KeyInfo>::getValueState(LatticeKey Key) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
230 auto I = ValueState.find(Key);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
231 if (I != ValueState.end())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
232 return I->second; // Common case, in the map
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
233
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
234 if (LatticeFunc->IsUntrackedValue(Key))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
235 return LatticeFunc->getUntrackedVal();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
236 LatticeVal LV = LatticeFunc->ComputeLatticeVal(Key);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
237
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
238 // If this value is untracked, don't add it to the map.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
239 if (LV == LatticeFunc->getUntrackedVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
240 return LV;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
241 return ValueState[Key] = LV;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
242 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
243
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
244 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
245 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::UpdateState(LatticeKey Key,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
246 LatticeVal LV) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
247 auto I = ValueState.find(Key);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
248 if (I != ValueState.end() && I->second == LV)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
249 return; // No change.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
250
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
251 // Update the state of the given LatticeKey and add its corresponding LLVM
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
252 // value to the work list.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
253 ValueState[Key] = LV;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
254 if (Value *V = KeyInfo::getValueFromLatticeKey(Key))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
255 ValueWorkList.push_back(V);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
256 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
257
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
258 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
259 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::MarkBlockExecutable(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
260 BasicBlock *BB) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
261 if (!BBExecutable.insert(BB).second)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
262 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
263 DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << "\n");
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
264 BBWorkList.push_back(BB); // Add the block to the work list!
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
265 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
266
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
267 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
268 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::markEdgeExecutable(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
269 BasicBlock *Source, BasicBlock *Dest) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
270 if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
271 return; // This edge is already known to be executable!
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
272
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
273 DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName() << " -> "
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
274 << Dest->getName() << "\n");
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
275
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
276 if (BBExecutable.count(Dest)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
277 // The destination is already executable, but we just made an edge
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
278 // feasible that wasn't before. Revisit the PHI nodes in the block
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
279 // because they have potentially new operands.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
280 for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
281 visitPHINode(*cast<PHINode>(I));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
282 } else {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
283 MarkBlockExecutable(Dest);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
284 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
285 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
286
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
287 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
288 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::getFeasibleSuccessors(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
289 TerminatorInst &TI, SmallVectorImpl<bool> &Succs, bool AggressiveUndef) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
290 Succs.resize(TI.getNumSuccessors());
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
291 if (TI.getNumSuccessors() == 0)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
292 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
293
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
294 if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
295 if (BI->isUnconditional()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
296 Succs[0] = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
297 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
298 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
299
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
300 LatticeVal BCValue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
301 if (AggressiveUndef)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
302 BCValue =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
303 getValueState(KeyInfo::getLatticeKeyFromValue(BI->getCondition()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
304 else
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
305 BCValue = getExistingValueState(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
306 KeyInfo::getLatticeKeyFromValue(BI->getCondition()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
307
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
308 if (BCValue == LatticeFunc->getOverdefinedVal() ||
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
309 BCValue == LatticeFunc->getUntrackedVal()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
310 // Overdefined condition variables can branch either way.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
311 Succs[0] = Succs[1] = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
312 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
313 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
314
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
315 // If undefined, neither is feasible yet.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
316 if (BCValue == LatticeFunc->getUndefVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
317 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
318
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
319 Constant *C =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
320 dyn_cast_or_null<Constant>(LatticeFunc->GetValueFromLatticeVal(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
321 BCValue, BI->getCondition()->getType()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
322 if (!C || !isa<ConstantInt>(C)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
323 // Non-constant values can go either way.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
324 Succs[0] = Succs[1] = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
325 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
326 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
327
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
328 // Constant condition variables mean the branch can only go a single way
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
329 Succs[C->isNullValue()] = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
330 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
331 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
332
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
333 if (TI.isExceptional()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
334 Succs.assign(Succs.size(), true);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
335 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
336 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
337
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
338 if (isa<IndirectBrInst>(TI)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
339 Succs.assign(Succs.size(), true);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
340 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
341 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
342
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
343 SwitchInst &SI = cast<SwitchInst>(TI);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
344 LatticeVal SCValue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
345 if (AggressiveUndef)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
346 SCValue = getValueState(KeyInfo::getLatticeKeyFromValue(SI.getCondition()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
347 else
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
348 SCValue = getExistingValueState(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
349 KeyInfo::getLatticeKeyFromValue(SI.getCondition()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
350
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
351 if (SCValue == LatticeFunc->getOverdefinedVal() ||
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
352 SCValue == LatticeFunc->getUntrackedVal()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
353 // All destinations are executable!
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
354 Succs.assign(TI.getNumSuccessors(), true);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
355 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
356 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
357
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
358 // If undefined, neither is feasible yet.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
359 if (SCValue == LatticeFunc->getUndefVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
360 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
361
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
362 Constant *C = dyn_cast_or_null<Constant>(LatticeFunc->GetValueFromLatticeVal(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
363 SCValue, SI.getCondition()->getType()));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
364 if (!C || !isa<ConstantInt>(C)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
365 // All destinations are executable!
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
366 Succs.assign(TI.getNumSuccessors(), true);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
367 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
368 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
369 SwitchInst::CaseHandle Case = *SI.findCaseValue(cast<ConstantInt>(C));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
370 Succs[Case.getSuccessorIndex()] = true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
371 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
372
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
373 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
374 bool SparseSolver<LatticeKey, LatticeVal, KeyInfo>::isEdgeFeasible(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
375 BasicBlock *From, BasicBlock *To, bool AggressiveUndef) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
376 SmallVector<bool, 16> SuccFeasible;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
377 TerminatorInst *TI = From->getTerminator();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
378 getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
379
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
380 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
381 if (TI->getSuccessor(i) == To && SuccFeasible[i])
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
382 return true;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
383
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
384 return false;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
385 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
386
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
387 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
388 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::visitTerminatorInst(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
389 TerminatorInst &TI) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
390 SmallVector<bool, 16> SuccFeasible;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
391 getFeasibleSuccessors(TI, SuccFeasible, true);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
392
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
393 BasicBlock *BB = TI.getParent();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
394
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
395 // Mark all feasible successors executable...
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
396 for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
397 if (SuccFeasible[i])
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
398 markEdgeExecutable(BB, TI.getSuccessor(i));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
399 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
400
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
401 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
402 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::visitPHINode(PHINode &PN) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
403 // The lattice function may store more information on a PHINode than could be
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
404 // computed from its incoming values. For example, SSI form stores its sigma
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
405 // functions as PHINodes with a single incoming value.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
406 if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
407 DenseMap<LatticeKey, LatticeVal> ChangedValues;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
408 LatticeFunc->ComputeInstructionState(PN, ChangedValues, *this);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
409 for (auto &ChangedValue : ChangedValues)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
410 if (ChangedValue.second != LatticeFunc->getUntrackedVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
411 UpdateState(ChangedValue.first, ChangedValue.second);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
412 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
413 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
414
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
415 LatticeKey Key = KeyInfo::getLatticeKeyFromValue(&PN);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
416 LatticeVal PNIV = getValueState(Key);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
417 LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
418
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
419 // If this value is already overdefined (common) just return.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
420 if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
421 return; // Quick exit
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
422
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
423 // Super-extra-high-degree PHI nodes are unlikely to ever be interesting,
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
424 // and slow us down a lot. Just mark them overdefined.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
425 if (PN.getNumIncomingValues() > 64) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
426 UpdateState(Key, Overdefined);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
427 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
428 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
429
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
430 // Look at all of the executable operands of the PHI node. If any of them
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
431 // are overdefined, the PHI becomes overdefined as well. Otherwise, ask the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
432 // transfer function to give us the merge of the incoming values.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
433 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
434 // If the edge is not yet known to be feasible, it doesn't impact the PHI.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
435 if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
436 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
437
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
438 // Merge in this value.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
439 LatticeVal OpVal =
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
440 getValueState(KeyInfo::getLatticeKeyFromValue(PN.getIncomingValue(i)));
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
441 if (OpVal != PNIV)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
442 PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
443
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
444 if (PNIV == Overdefined)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
445 break; // Rest of input values don't matter.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
446 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
447
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
448 // Update the PHI with the compute value, which is the merge of the inputs.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
449 UpdateState(Key, PNIV);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
450 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
451
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
452 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
453 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::visitInst(Instruction &I) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
454 // PHIs are handled by the propagation logic, they are never passed into the
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
455 // transfer functions.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
456 if (PHINode *PN = dyn_cast<PHINode>(&I))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
457 return visitPHINode(*PN);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
458
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
459 // Otherwise, ask the transfer function what the result is. If this is
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
460 // something that we care about, remember it.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
461 DenseMap<LatticeKey, LatticeVal> ChangedValues;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
462 LatticeFunc->ComputeInstructionState(I, ChangedValues, *this);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
463 for (auto &ChangedValue : ChangedValues)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
464 if (ChangedValue.second != LatticeFunc->getUntrackedVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
465 UpdateState(ChangedValue.first, ChangedValue.second);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
466
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
467 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
468 visitTerminatorInst(*TI);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
469 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
470
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
471 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
472 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::Solve() {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
473 // Process the work lists until they are empty!
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
474 while (!BBWorkList.empty() || !ValueWorkList.empty()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
475 // Process the value work list.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
476 while (!ValueWorkList.empty()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
477 Value *V = ValueWorkList.back();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
478 ValueWorkList.pop_back();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
479
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
480 DEBUG(dbgs() << "\nPopped off V-WL: " << *V << "\n");
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
481
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
482 // "V" got into the work list because it made a transition. See if any
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
483 // users are both live and in need of updating.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
484 for (User *U : V->users())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
485 if (Instruction *Inst = dyn_cast<Instruction>(U))
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
486 if (BBExecutable.count(Inst->getParent())) // Inst is executable?
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
487 visitInst(*Inst);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
488 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
489
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
490 // Process the basic block work list.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
491 while (!BBWorkList.empty()) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
492 BasicBlock *BB = BBWorkList.back();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
493 BBWorkList.pop_back();
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
494
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
495 DEBUG(dbgs() << "\nPopped off BBWL: " << *BB);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
496
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
497 // Notify all instructions in this basic block that they are newly
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
498 // executable.
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
499 for (Instruction &I : *BB)
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
500 visitInst(I);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
501 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
502 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
503 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
504
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
505 template <class LatticeKey, class LatticeVal, class KeyInfo>
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
506 void SparseSolver<LatticeKey, LatticeVal, KeyInfo>::Print(
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
507 raw_ostream &OS) const {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
508 if (ValueState.empty())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
509 return;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
510
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
511 LatticeKey Key;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
512 LatticeVal LV;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
513
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
514 OS << "ValueState:\n";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
515 for (auto &Entry : ValueState) {
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
516 std::tie(Key, LV) = Entry;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
517 if (LV == LatticeFunc->getUntrackedVal())
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
518 continue;
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
519 OS << "\t";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
520 LatticeFunc->PrintLatticeVal(LV, OS);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
521 OS << ": ";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
522 LatticeFunc->PrintLatticeKey(Key, OS);
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
523 OS << "\n";
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
524 }
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
525 }
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
526 } // end namespace llvm
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
527
121
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
528 #undef DEBUG_TYPE
803732b1fca8 LLVM 5.0
kono
parents: 120
diff changeset
529
0
95c75e76d11b LLVM 3.4
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
diff changeset
530 #endif // LLVM_ANALYSIS_SPARSEPROPAGATION_H