Skip to content

Commit 69962bd

Browse files
authored
Merge pull request #203 from dave-bartolomeo/dave/GVN
C++: Initial attempt at IR-based value numbering
2 parents f146e34 + 5a25602 commit 69962bd

28 files changed

+1815
-2
lines changed

config/identical-files.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,10 @@
5454
"C++ SSA SSAConstruction": [
5555
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/SSAConstruction.qll",
5656
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll"
57+
],
58+
"C++ IR ValueNumber": [
59+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/gvn/ValueNumbering.qll",
60+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/gvn/ValueNumbering.qll",
61+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/gvn/ValueNumbering.qll"
5762
]
5863
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import implementation.aliased_ssa.PrintIR
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import implementation.aliased_ssa.gvn.ValueNumbering

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/IR.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,30 @@ import IRVariable
55
import OperandTag
66
import semmle.code.cpp.ir.implementation.EdgeKind
77
import semmle.code.cpp.ir.implementation.MemoryAccessKind
8+
9+
private newtype TIRPropertyProvider = MkIRPropertyProvider()
10+
11+
/**
12+
* Class that provides additional properties to be dumped for IR instructions and blocks when using
13+
* the PrintIR module. Libraries that compute additional facts about IR elements can extend the
14+
* single instance of this class to specify the additional properties computed by the library.
15+
*/
16+
class IRPropertyProvider extends TIRPropertyProvider {
17+
string toString() {
18+
result = "IRPropertyProvider"
19+
}
20+
21+
/**
22+
* Gets the value of the property named `key` for the specified instruction.
23+
*/
24+
string getInstructionProperty(Instruction instruction, string key) {
25+
none()
26+
}
27+
28+
/**
29+
* Gets the value of the property named `key` for the specified block.
30+
*/
31+
string getBlockProperty(IRBlock block, string key) {
32+
none()
33+
}
34+
}

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ module InstructionSanity {
107107
operand = op.getOperand(tag) and
108108
operand.getFunctionIR() != op.getFunctionIR()
109109
}
110+
111+
/**
112+
* Holds if instruction `instr` is not in exactly one block.
113+
*/
114+
query predicate instructionWithoutUniqueBlock(Instruction instr, int blockCount) {
115+
blockCount = count(instr.getBlock()) and
116+
blockCount != 1
117+
}
110118
}
111119

112120
/**
@@ -301,6 +309,13 @@ class Instruction extends Construction::TInstruction {
301309
result = ast.getLocation()
302310
}
303311

312+
/**
313+
* Gets the `Expr` whose results is computed by this instruction, if any.
314+
*/
315+
final Expr getResultExpression() {
316+
result = Construction::getInstructionResultExpression(this)
317+
}
318+
304319
/**
305320
* Gets the type of the result produced by this instruction. If the
306321
* instruction does not produce a result, its result type will be `VoidType`.
@@ -554,6 +569,15 @@ class InitializeParameterInstruction extends VariableInstruction {
554569
}
555570
}
556571

572+
/**
573+
* An instruction that initializes the `this` pointer parameter of the enclosing function.
574+
*/
575+
class InitializeThisInstruction extends Instruction {
576+
InitializeThisInstruction() {
577+
opcode instanceof Opcode::InitializeThis
578+
}
579+
}
580+
557581
class FieldAddressInstruction extends FieldInstruction {
558582
FieldAddressInstruction() {
559583
opcode instanceof Opcode::FieldAddress

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
private import IR
22
import cpp
33

4+
private string getAdditionalInstructionProperty(Instruction instr, string key) {
5+
exists(IRPropertyProvider provider |
6+
result = provider.getInstructionProperty(instr, key)
7+
)
8+
}
9+
10+
private string getAdditionalBlockProperty(IRBlock block, string key) {
11+
exists(IRPropertyProvider provider |
12+
result = provider.getBlockProperty(block, key)
13+
)
14+
}
15+
416
private newtype TPrintableIRNode =
517
TPrintableFunctionIR(FunctionIR funcIR) or
618
TPrintableIRBlock(IRBlock block) or
@@ -135,6 +147,11 @@ class PrintableIRBlock extends PrintableIRNode, TPrintableIRBlock {
135147
result.getFunctionIR() = block.getFunctionIR()
136148
}
137149

150+
override string getProperty(string key) {
151+
result = PrintableIRNode.super.getProperty(key) or
152+
result = getAdditionalBlockProperty(block, key)
153+
}
154+
138155
final IRBlock getBlock() {
139156
result = block
140157
}
@@ -185,6 +202,11 @@ class PrintableInstruction extends PrintableIRNode, TPrintableInstruction {
185202
final Instruction getInstruction() {
186203
result = instr
187204
}
205+
206+
override string getProperty(string key) {
207+
result = PrintableIRNode.super.getProperty(key) or
208+
result = getAdditionalInstructionProperty(instr, key)
209+
}
188210
}
189211

190212
private predicate columnWidths(IRBlock block, int resultWidth, int operationWidth) {

0 commit comments

Comments
 (0)