Skip to content

Commit 8b4d6a2

Browse files
author
Dave Bartolomeo
committed
Performance improvements for semantic layer construction
1 parent 00ae5de commit 8b4d6a2

3 files changed

Lines changed: 60 additions & 47 deletions

File tree

java/ql/lib/semmle/code/java/semantic/SemanticExprSpecific.qll

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -225,56 +225,52 @@ module SemanticExprConfig {
225225
)
226226
}
227227

228-
predicate unaryExpr(Expr expr, Opcode opcode, SemType type, Expr operand) {
229-
exists(J::Expr javaExpr | expr = TPrimaryExpr(javaExpr) |
230-
type = getSemanticType(javaExpr.getType()) and
228+
private predicate primaryUnaryExpr(J::Expr javaExpr, Opcode opcode, Expr operand) {
229+
exists(J::UnaryExpr unary | unary = javaExpr |
230+
operand = getResultExpr(unary.getExpr()) and
231231
(
232-
exists(J::UnaryExpr unary | unary = javaExpr |
233-
operand = getResultExpr(unary.getExpr()) and
234-
(
235-
unary instanceof J::MinusExpr and opcode instanceof Opcode::Negate
236-
or
237-
unary instanceof J::PlusExpr and opcode instanceof Opcode::CopyValue
238-
or
239-
unary instanceof J::BitNotExpr and opcode instanceof Opcode::BitComplement
240-
or
241-
unary instanceof J::LogNotExpr and opcode instanceof Opcode::LogicalNot
242-
or
243-
unary instanceof J::PreIncExpr and opcode instanceof Opcode::AddOne
244-
or
245-
unary instanceof J::PreDecExpr and opcode instanceof Opcode::SubOne
246-
or
247-
// Post-increment/decrement returns the original value. The actual increment/decrement part
248-
// is a separate `Expr`.
249-
unary instanceof J::PostIncExpr and opcode instanceof Opcode::CopyValue
250-
or
251-
unary instanceof J::PostDecExpr and opcode instanceof Opcode::CopyValue
252-
)
253-
)
232+
unary instanceof J::MinusExpr and opcode instanceof Opcode::Negate
254233
or
255-
exists(J::CastExpr cast | cast = javaExpr |
256-
// TODO: Boolean? Null? Boxing?
257-
type instanceof SemNumericType and
258-
getSemanticType(cast.getExpr().getType()) instanceof SemNumericType and
259-
opcode instanceof Opcode::Convert and
260-
operand = getResultExpr(cast.getExpr())
261-
)
234+
unary instanceof J::PlusExpr and opcode instanceof Opcode::CopyValue
262235
or
263-
exists(J::AssignExpr assign | assign = javaExpr |
264-
opcode instanceof Opcode::CopyValue and
265-
operand = getResultExpr(assign.getSource())
266-
)
236+
unary instanceof J::BitNotExpr and opcode instanceof Opcode::BitComplement
267237
or
268-
exists(J::LocalVariableDeclExpr decl | decl = javaExpr |
269-
opcode instanceof Opcode::CopyValue and
270-
operand = getResultExpr(decl.getInit())
271-
)
238+
unary instanceof J::LogNotExpr and opcode instanceof Opcode::LogicalNot
239+
or
240+
unary instanceof J::PreIncExpr and opcode instanceof Opcode::AddOne
241+
or
242+
unary instanceof J::PreDecExpr and opcode instanceof Opcode::SubOne
243+
or
244+
// Post-increment/decrement returns the original value. The actual increment/decrement part
245+
// is a separate `Expr`.
246+
unary instanceof J::PostIncExpr and opcode instanceof Opcode::CopyValue
247+
or
248+
unary instanceof J::PostDecExpr and opcode instanceof Opcode::CopyValue
272249
)
273250
)
274251
or
275-
exists(J::UnaryAssignExpr javaExpr | javaExpr = expr.(PostUpdateExpr).getExpr() |
276-
type = getSemanticType(javaExpr.getType()) and
277-
operand = getResultExpr(javaExpr.getExpr()) and
252+
exists(J::CastExpr cast | cast = javaExpr |
253+
// TODO: Boolean? Null? Boxing?
254+
getSemanticType(cast.getType()) instanceof SemNumericType and
255+
getSemanticType(cast.getExpr().getType()) instanceof SemNumericType and
256+
opcode instanceof Opcode::Convert and
257+
operand = getResultExpr(cast.getExpr())
258+
)
259+
or
260+
exists(J::AssignExpr assign | assign = javaExpr |
261+
opcode instanceof Opcode::CopyValue and
262+
operand = getResultExpr(assign.getSource())
263+
)
264+
or
265+
exists(J::LocalVariableDeclExpr decl | decl = javaExpr |
266+
opcode instanceof Opcode::CopyValue and
267+
operand = getResultExpr(decl.getInit())
268+
)
269+
}
270+
271+
private predicate postUpdateUnaryExpr(J::UnaryAssignExpr javaExpr, Opcode opcode, Expr operand) {
272+
exists(J::UnaryAssignExpr unaryAssign | unaryAssign = javaExpr |
273+
operand = getResultExpr(unaryAssign.getExpr()) and
278274
(
279275
javaExpr instanceof J::PostIncExpr and opcode instanceof Opcode::AddOne
280276
or
@@ -283,6 +279,22 @@ module SemanticExprConfig {
283279
)
284280
}
285281

282+
private predicate unaryExprWithoutType(Expr expr, Opcode opcode, Expr operand, J::Expr javaExpr) {
283+
primaryUnaryExpr(javaExpr, opcode, operand) and
284+
expr = TPrimaryExpr(javaExpr)
285+
or
286+
postUpdateUnaryExpr(javaExpr, opcode, operand) and
287+
expr = TPostUpdateExpr(javaExpr)
288+
}
289+
290+
predicate unaryExpr(Expr expr, Opcode opcode, SemType type, Expr operand) {
291+
exists(J::Expr javaExpr, J::Type javaType |
292+
unaryExprWithoutType(expr, opcode, operand, javaExpr) and
293+
javaType = javaExpr.getType() and
294+
type = getSemanticType(javaType)
295+
)
296+
}
297+
286298
predicate nullaryExpr(Expr expr, Opcode opcode, SemType type) {
287299
exists(ParameterInitExpr paramInit | paramInit = expr |
288300
opcode instanceof Opcode::InitializeParameter and

java/ql/lib/semmle/code/java/semantic/SemanticType.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ class SemOpaqueType extends SemSizedType, TSemOpaqueType {
224224
final override int getByteSize() { result = byteSize }
225225
}
226226

227+
cached
227228
SemType getSemanticType(Specific::Type type) {
228229
exists(int byteSize |
229230
Specific::booleanType(type, byteSize) and result = TSemBooleanType(byteSize)

java/ql/test/library-tests/dataflow/sign-analysis/diff.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import semmle.code.java.semantic.SemanticType
99
import semmle.code.java.dataflow.SSA
1010
import semmle.code.java.dataflow.internal.rangeanalysis.SsaReadPositionCommon
1111
import SignAnalysisCommonTest
12+
import semmle.code.java.dataflow.RangeUtils as RU
1213

1314
predicate interestingLocation(Location loc) {
14-
// loc.getFile().getBaseName() = "ReplyMessage.java" and
15-
// loc.getStartLine() in [266 .. 266] and
15+
// loc.getFile().getBaseName() = "OpExecutorImplJUnitTest.java" and
16+
// loc.getStartLine() in [457 .. 457] and
1617
any()
1718
}
1819

@@ -25,8 +26,7 @@ query predicate diff_exprSign(SemExpr e, string astSign, string semSign) {
2526
}
2627

2728
query predicate diff_ssaDefSign(SemSsaVariable v, string astSign, string semSign) {
28-
getJavaBasicBlock(v.getBasicBlock()).getEnclosingCallable().fromSource() and
29-
interestingLocation(v.getBasicBlock().getLocation()) and
29+
interestingLocation(v.getLocation()) and
3030
semSign = concat(semSsaDefSign(v).toString(), "") and
3131
astSign = concat(testSsaDefSign(getJavaSsaVariable(v)).toString(), "") and
3232
astSign != semSign

0 commit comments

Comments
 (0)