Skip to content

Commit 46b2c19

Browse files
C++: Initial attempt at IR-based value numbering
1 parent 782e91b commit 46b2c19

File tree

23 files changed

+1689
-0
lines changed

23 files changed

+1689
-0
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/internal/gvn/ValueNumber.qll",
60+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/gvn/ValueNumber.qll",
61+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/gvn/ValueNumber.qll"
5762
]
5863
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The C/C++ Intermediate Representation
2+
3+
## Introduction
4+
5+
The Intermediate Representation (IR) library provides a representation of the semantics of the program, independent of the syntax used to express those semantics. The IR is similar in design to representations used in optimizing compilers, such as LLVM IR.
6+
7+
## Memory Access
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import implementation.aliased_ssa.PrintIR

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,13 @@ class Instruction extends Construction::TInstruction {
301301
result = ast.getLocation()
302302
}
303303

304+
/**
305+
* Gets the `Expr` whose results is computed by this instruction, if any.
306+
*/
307+
final Expr getResultExpression() {
308+
result = Construction::getInstructionResultExpression(this)
309+
}
310+
304311
/**
305312
* Gets the type of the result produced by this instruction. If the
306313
* instruction does not produce a result, its result type will be `VoidType`.
@@ -554,6 +561,15 @@ class InitializeParameterInstruction extends VariableInstruction {
554561
}
555562
}
556563

564+
/**
565+
* An instruction that initializes the `this` pointer parameter of the enclosing function.
566+
*/
567+
class InitializeThisInstruction extends Instruction {
568+
InitializeThisInstruction() {
569+
opcode instanceof Opcode::InitializeThis
570+
}
571+
}
572+
557573
class FieldAddressInstruction extends FieldInstruction {
558574
FieldAddressInstruction() {
559575
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) {

cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/internal/SSAConstruction.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ cached private module Cached {
195195
)
196196
}
197197

198+
cached Expr getInstructionResultExpression(Instruction instruction) {
199+
result = getOldInstruction(instruction).getResultExpression()
200+
}
201+
198202
cached Instruction getInstructionSuccessor(Instruction instruction, EdgeKind kind) {
199203
result = getNewInstruction(getOldInstruction(instruction).getSuccessor(kind))
200204
}

0 commit comments

Comments
 (0)