Skip to content

Commit bb5a78d

Browse files
committed
C++: Factor the IPA body of 'TGlobalUse' and 'TGlobalDef' out into predicates.
1 parent 0963af2 commit bb5a78d

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/SsaInternals.qll

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,12 @@ private newtype TDefOrUseImpl =
113113
TGlobalUse(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
114114
// Represents a final "use" of a global variable to ensure that
115115
// the assignment to a global variable isn't ruled out as dead.
116-
exists(VariableAddressInstruction vai, int defIndex |
117-
vai.getEnclosingIRFunction() = f and
118-
vai.getAstVariable() = v and
119-
isDef(_, _, _, vai, _, defIndex) and
120-
indirectionIndex = [0 .. defIndex] + 1
121-
)
116+
isGlobalUse(v, f, _, indirectionIndex)
122117
} or
123118
TGlobalDefImpl(GlobalLikeVariable v, IRFunction f, int indirectionIndex) {
124119
// Represents the initial "definition" of a global variable when entering
125120
// a function body.
126-
exists(VariableAddressInstruction vai |
127-
vai.getEnclosingIRFunction() = f and
128-
vai.getAstVariable() = v and
129-
isUse(_, _, vai, _, indirectionIndex) and
130-
not isDef(_, _, vai.getAUse(), _, _, _)
131-
)
121+
isGlobalDefImpl(v, f, _, indirectionIndex)
132122
} or
133123
TIteratorDef(
134124
Operand iteratorDerefAddress, BaseSourceVariableInstruction container, int indirectionIndex
@@ -150,6 +140,28 @@ private newtype TDefOrUseImpl =
150140
)
151141
}
152142

143+
private predicate isGlobalUse(
144+
GlobalLikeVariable v, IRFunction f, int indirection, int indirectionIndex
145+
) {
146+
exists(VariableAddressInstruction vai, int defIndex |
147+
vai.getEnclosingIRFunction() = f and
148+
vai.getAstVariable() = v and
149+
isDef(_, _, _, vai, indirection, defIndex) and
150+
indirectionIndex = [0 .. defIndex] + 1
151+
)
152+
}
153+
154+
private predicate isGlobalDefImpl(
155+
GlobalLikeVariable v, IRFunction f, int indirection, int indirectionIndex
156+
) {
157+
exists(VariableAddressInstruction vai |
158+
vai.getEnclosingIRFunction() = f and
159+
vai.getAstVariable() = v and
160+
isUse(_, _, vai, indirection, indirectionIndex) and
161+
not isDef(_, _, vai.getAUse(), _, _, _)
162+
)
163+
}
164+
153165
private predicate unspecifiedTypeIsModifiableAt(Type unspecified, int indirectionIndex) {
154166
indirectionIndex = [1 .. getIndirectionForUnspecifiedType(unspecified).getNumberOfIndirections()] and
155167
exists(CppType cppType |
@@ -438,7 +450,7 @@ class GlobalUse extends UseImpl, TGlobalUse {
438450

439451
override FinalGlobalValue getNode() { result.getGlobalUse() = this }
440452

441-
override int getIndirection() { result = ind + 1 }
453+
override int getIndirection() { isGlobalUse(global, f, result, ind) }
442454

443455
/** Gets the global variable associated with this use. */
444456
GlobalLikeVariable getVariable() { result = global }

0 commit comments

Comments
 (0)