Skip to content

Commit 283bb2f

Browse files
committed
C++: Factor out ConstantAnalysis BinaryInstruction
This speeds up comdb2 constant analysis from 6.5s to 4.5s.
1 parent d66578e commit 283bb2f

File tree

3 files changed

+78
-51
lines changed

3 files changed

+78
-51
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/constant/ConstantAnalysis.qll

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,7 @@ private import IR
55
language[monotonicAggregates]
66
int getConstantValue(Instruction instr) {
77
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8-
exists(BinaryInstruction binInstr, int left, int right |
9-
binInstr = instr and
10-
left = getConstantValue(binInstr.getLeftOperand()) and
11-
right = getConstantValue(binInstr.getRightOperand()) and
12-
(
13-
binInstr instanceof AddInstruction and result = add(left, right) or
14-
binInstr instanceof SubInstruction and result = sub(left, right) or
15-
binInstr instanceof MulInstruction and result = mul(left, right) or
16-
binInstr instanceof DivInstruction and result = div(left, right) or
17-
binInstr instanceof CompareEQInstruction and result = compareEQ(left, right) or
18-
binInstr instanceof CompareNEInstruction and result = compareNE(left, right) or
19-
binInstr instanceof CompareLTInstruction and result = compareLT(left, right) or
20-
binInstr instanceof CompareGTInstruction and result = compareGT(left, right) or
21-
binInstr instanceof CompareLEInstruction and result = compareLE(left, right) or
22-
binInstr instanceof CompareGEInstruction and result = compareGE(left, right)
23-
)
24-
) or
8+
result = getBinaryInstructionValue(instr) or
259
exists(UnaryInstruction unaryInstr, int src |
2610
unaryInstr = instr and
2711
src = getConstantValue(unaryInstr.getOperand()) and
@@ -36,3 +20,28 @@ int getConstantValue(Instruction instr) {
3620
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
3721
)
3822
}
23+
24+
pragma[noinline]
25+
private predicate binaryInstructionOperands(BinaryInstruction instr, int left, int right) {
26+
left = getConstantValue(instr.getLeftOperand()) and
27+
right = getConstantValue(instr.getRightOperand())
28+
}
29+
30+
pragma[noinline]
31+
private int getBinaryInstructionValue(BinaryInstruction instr) {
32+
exists(int left, int right |
33+
binaryInstructionOperands(instr, left, right) and
34+
(
35+
instr instanceof AddInstruction and result = add(left, right) or
36+
instr instanceof SubInstruction and result = sub(left, right) or
37+
instr instanceof MulInstruction and result = mul(left, right) or
38+
instr instanceof DivInstruction and result = div(left, right) or
39+
instr instanceof CompareEQInstruction and result = compareEQ(left, right) or
40+
instr instanceof CompareNEInstruction and result = compareNE(left, right) or
41+
instr instanceof CompareLTInstruction and result = compareLT(left, right) or
42+
instr instanceof CompareGTInstruction and result = compareGT(left, right) or
43+
instr instanceof CompareLEInstruction and result = compareLE(left, right) or
44+
instr instanceof CompareGEInstruction and result = compareGE(left, right)
45+
)
46+
)
47+
}

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,7 @@ private import IR
55
language[monotonicAggregates]
66
int getConstantValue(Instruction instr) {
77
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8-
exists(BinaryInstruction binInstr, int left, int right |
9-
binInstr = instr and
10-
left = getConstantValue(binInstr.getLeftOperand()) and
11-
right = getConstantValue(binInstr.getRightOperand()) and
12-
(
13-
binInstr instanceof AddInstruction and result = add(left, right) or
14-
binInstr instanceof SubInstruction and result = sub(left, right) or
15-
binInstr instanceof MulInstruction and result = mul(left, right) or
16-
binInstr instanceof DivInstruction and result = div(left, right) or
17-
binInstr instanceof CompareEQInstruction and result = compareEQ(left, right) or
18-
binInstr instanceof CompareNEInstruction and result = compareNE(left, right) or
19-
binInstr instanceof CompareLTInstruction and result = compareLT(left, right) or
20-
binInstr instanceof CompareGTInstruction and result = compareGT(left, right) or
21-
binInstr instanceof CompareLEInstruction and result = compareLE(left, right) or
22-
binInstr instanceof CompareGEInstruction and result = compareGE(left, right)
23-
)
24-
) or
8+
result = getBinaryInstructionValue(instr) or
259
exists(UnaryInstruction unaryInstr, int src |
2610
unaryInstr = instr and
2711
src = getConstantValue(unaryInstr.getOperand()) and
@@ -36,3 +20,28 @@ int getConstantValue(Instruction instr) {
3620
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
3721
)
3822
}
23+
24+
pragma[noinline]
25+
private predicate binaryInstructionOperands(BinaryInstruction instr, int left, int right) {
26+
left = getConstantValue(instr.getLeftOperand()) and
27+
right = getConstantValue(instr.getRightOperand())
28+
}
29+
30+
pragma[noinline]
31+
private int getBinaryInstructionValue(BinaryInstruction instr) {
32+
exists(int left, int right |
33+
binaryInstructionOperands(instr, left, right) and
34+
(
35+
instr instanceof AddInstruction and result = add(left, right) or
36+
instr instanceof SubInstruction and result = sub(left, right) or
37+
instr instanceof MulInstruction and result = mul(left, right) or
38+
instr instanceof DivInstruction and result = div(left, right) or
39+
instr instanceof CompareEQInstruction and result = compareEQ(left, right) or
40+
instr instanceof CompareNEInstruction and result = compareNE(left, right) or
41+
instr instanceof CompareLTInstruction and result = compareLT(left, right) or
42+
instr instanceof CompareGTInstruction and result = compareGT(left, right) or
43+
instr instanceof CompareLEInstruction and result = compareLE(left, right) or
44+
instr instanceof CompareGEInstruction and result = compareGE(left, right)
45+
)
46+
)
47+
}

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/constant/ConstantAnalysis.qll

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,7 @@ private import IR
55
language[monotonicAggregates]
66
int getConstantValue(Instruction instr) {
77
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8-
exists(BinaryInstruction binInstr, int left, int right |
9-
binInstr = instr and
10-
left = getConstantValue(binInstr.getLeftOperand()) and
11-
right = getConstantValue(binInstr.getRightOperand()) and
12-
(
13-
binInstr instanceof AddInstruction and result = add(left, right) or
14-
binInstr instanceof SubInstruction and result = sub(left, right) or
15-
binInstr instanceof MulInstruction and result = mul(left, right) or
16-
binInstr instanceof DivInstruction and result = div(left, right) or
17-
binInstr instanceof CompareEQInstruction and result = compareEQ(left, right) or
18-
binInstr instanceof CompareNEInstruction and result = compareNE(left, right) or
19-
binInstr instanceof CompareLTInstruction and result = compareLT(left, right) or
20-
binInstr instanceof CompareGTInstruction and result = compareGT(left, right) or
21-
binInstr instanceof CompareLEInstruction and result = compareLE(left, right) or
22-
binInstr instanceof CompareGEInstruction and result = compareGE(left, right)
23-
)
24-
) or
8+
result = getBinaryInstructionValue(instr) or
259
exists(UnaryInstruction unaryInstr, int src |
2610
unaryInstr = instr and
2711
src = getConstantValue(unaryInstr.getOperand()) and
@@ -36,3 +20,28 @@ int getConstantValue(Instruction instr) {
3620
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
3721
)
3822
}
23+
24+
pragma[noinline]
25+
private predicate binaryInstructionOperands(BinaryInstruction instr, int left, int right) {
26+
left = getConstantValue(instr.getLeftOperand()) and
27+
right = getConstantValue(instr.getRightOperand())
28+
}
29+
30+
pragma[noinline]
31+
private int getBinaryInstructionValue(BinaryInstruction instr) {
32+
exists(int left, int right |
33+
binaryInstructionOperands(instr, left, right) and
34+
(
35+
instr instanceof AddInstruction and result = add(left, right) or
36+
instr instanceof SubInstruction and result = sub(left, right) or
37+
instr instanceof MulInstruction and result = mul(left, right) or
38+
instr instanceof DivInstruction and result = div(left, right) or
39+
instr instanceof CompareEQInstruction and result = compareEQ(left, right) or
40+
instr instanceof CompareNEInstruction and result = compareNE(left, right) or
41+
instr instanceof CompareLTInstruction and result = compareLT(left, right) or
42+
instr instanceof CompareGTInstruction and result = compareGT(left, right) or
43+
instr instanceof CompareLEInstruction and result = compareLE(left, right) or
44+
instr instanceof CompareGEInstruction and result = compareGE(left, right)
45+
)
46+
)
47+
}

0 commit comments

Comments
 (0)