Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 59fc77f

Browse files
C++: Simple constant analysis
This change moves the simple constant analysis that was used by the const_func test into a pyrameterized module for use on any stage of the IR. This will be used to detect unreachable code.
1 parent 6a11ef5 commit 59fc77f

11 files changed

Lines changed: 143 additions & 22 deletions

File tree

config/identical-files.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,15 @@
7272
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/gvn/ValueNumbering.qll",
7373
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/gvn/ValueNumbering.qll",
7474
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/gvn/ValueNumbering.qll"
75+
],
76+
"C++ IR ConstantAnalysis": [
77+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll",
78+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/constant/ConstantAnalysis.qll",
79+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/constant/ConstantAnalysis.qll"
80+
],
81+
"C++ IR PrintConstantAnalysis": [
82+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/constant/PrintConstantAnalysis.qll",
83+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/constant/PrintConstantAnalysis.qll",
84+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/constant/PrintConstantAnalysis.qll"
7585
]
7686
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private import internal.ConstantAnalysisInternal
2+
import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import IR
4+
5+
language[monotonicAggregates]
6+
IntValue getConstantValue(Instruction instr) {
7+
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8+
exists(BinaryInstruction binInstr, IntValue left, IntValue 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)
17+
)
18+
) or
19+
exists(UnaryInstruction unaryInstr, IntValue src |
20+
unaryInstr = instr and
21+
src = getConstantValue(unaryInstr.getOperand()) and
22+
(
23+
unaryInstr instanceof NegateInstruction and result = neg(src)
24+
)
25+
) or
26+
result = getConstantValue(instr.(CopyInstruction).getSourceValue()) or
27+
exists(PhiInstruction phi |
28+
phi = instr and
29+
result = max(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction())) and
30+
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
31+
)
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
private import internal.ConstantAnalysisInternal
2+
private import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import ConstantAnalysis
4+
import IR
5+
6+
private class ConstantAnalysisPropertyProvider extends IRPropertyProvider {
7+
override string getInstructionProperty(Instruction instr, string key) {
8+
key = "ConstantValue" and
9+
result = getValue(getConstantValue(instr)).toString()
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import semmle.code.cpp.ir.implementation.aliased_ssa.IR as IR
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private import internal.ConstantAnalysisInternal
2+
import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import IR
4+
5+
language[monotonicAggregates]
6+
IntValue getConstantValue(Instruction instr) {
7+
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8+
exists(BinaryInstruction binInstr, IntValue left, IntValue 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)
17+
)
18+
) or
19+
exists(UnaryInstruction unaryInstr, IntValue src |
20+
unaryInstr = instr and
21+
src = getConstantValue(unaryInstr.getOperand()) and
22+
(
23+
unaryInstr instanceof NegateInstruction and result = neg(src)
24+
)
25+
) or
26+
result = getConstantValue(instr.(CopyInstruction).getSourceValue()) or
27+
exists(PhiInstruction phi |
28+
phi = instr and
29+
result = max(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction())) and
30+
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
31+
)
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
private import internal.ConstantAnalysisInternal
2+
private import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import ConstantAnalysis
4+
import IR
5+
6+
private class ConstantAnalysisPropertyProvider extends IRPropertyProvider {
7+
override string getInstructionProperty(Instruction instr, string key) {
8+
key = "ConstantValue" and
9+
result = getValue(getConstantValue(instr)).toString()
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import semmle.code.cpp.ir.implementation.raw.IR as IR
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
private import internal.ConstantAnalysisInternal
2+
import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import IR
4+
5+
language[monotonicAggregates]
6+
IntValue getConstantValue(Instruction instr) {
7+
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8+
exists(BinaryInstruction binInstr, IntValue left, IntValue 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)
17+
)
18+
) or
19+
exists(UnaryInstruction unaryInstr, IntValue src |
20+
unaryInstr = instr and
21+
src = getConstantValue(unaryInstr.getOperand()) and
22+
(
23+
unaryInstr instanceof NegateInstruction and result = neg(src)
24+
)
25+
) or
26+
result = getConstantValue(instr.(CopyInstruction).getSourceValue()) or
27+
exists(PhiInstruction phi |
28+
phi = instr and
29+
result = max(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction())) and
30+
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
31+
)
32+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
private import internal.ConstantAnalysisInternal
2+
private import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import ConstantAnalysis
4+
import IR
5+
6+
private class ConstantAnalysisPropertyProvider extends IRPropertyProvider {
7+
override string getInstructionProperty(Instruction instr, string key) {
8+
key = "ConstantValue" and
9+
result = getValue(getConstantValue(instr)).toString()
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import semmle.code.cpp.ir.implementation.unaliased_ssa.IR as IR

0 commit comments

Comments
 (0)