Skip to content

Commit 5bc844c

Browse files
authored
Merge pull request github#13207 from MathiasVP/use-equiv-class-in-getInstruction
C++: Reduce memory pressure from `getInstruction`
2 parents b2fb2aa + 9e05569 commit 5bc844c

File tree

5 files changed

+90
-20
lines changed

5 files changed

+90
-20
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/IRBlock.qll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,28 @@ private module Cached {
255255
cached
256256
newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) }
257257

258-
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
259-
private Instruction getInstructionFromFirst(Instruction first, int index) =
260-
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
258+
/** Gets the index of `i` in its `IRBlock`. */
259+
private int getMemberIndex(Instruction i) {
260+
startsBasicBlock(i) and
261+
result = 0
262+
or
263+
exists(Instruction iPrev |
264+
adjacentInBlock(iPrev, i) and
265+
result = getMemberIndex(iPrev) + 1
266+
)
267+
}
268+
269+
private module BlockAdjacency = QlBuiltins::EquivalenceRelation<Instruction, adjacentInBlock/2>;
261270

262271
/** Holds if `i` is the `index`th instruction in `block`. */
263272
cached
264273
Instruction getInstruction(TIRBlock block, int index) {
265-
result = getInstructionFromFirst(getFirstInstruction(block), index)
274+
exists(Instruction first | block = MkIRBlock(first) |
275+
first = result and index = 0
276+
or
277+
index = getMemberIndex(result) and
278+
BlockAdjacency::getEquivalenceClass(first) = BlockAdjacency::getEquivalenceClass(result)
279+
)
266280
}
267281

268282
cached

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/IRBlock.qll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,28 @@ private module Cached {
255255
cached
256256
newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) }
257257

258-
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
259-
private Instruction getInstructionFromFirst(Instruction first, int index) =
260-
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
258+
/** Gets the index of `i` in its `IRBlock`. */
259+
private int getMemberIndex(Instruction i) {
260+
startsBasicBlock(i) and
261+
result = 0
262+
or
263+
exists(Instruction iPrev |
264+
adjacentInBlock(iPrev, i) and
265+
result = getMemberIndex(iPrev) + 1
266+
)
267+
}
268+
269+
private module BlockAdjacency = QlBuiltins::EquivalenceRelation<Instruction, adjacentInBlock/2>;
261270

262271
/** Holds if `i` is the `index`th instruction in `block`. */
263272
cached
264273
Instruction getInstruction(TIRBlock block, int index) {
265-
result = getInstructionFromFirst(getFirstInstruction(block), index)
274+
exists(Instruction first | block = MkIRBlock(first) |
275+
first = result and index = 0
276+
or
277+
index = getMemberIndex(result) and
278+
BlockAdjacency::getEquivalenceClass(first) = BlockAdjacency::getEquivalenceClass(result)
279+
)
266280
}
267281

268282
cached

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/IRBlock.qll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,28 @@ private module Cached {
255255
cached
256256
newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) }
257257

258-
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
259-
private Instruction getInstructionFromFirst(Instruction first, int index) =
260-
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
258+
/** Gets the index of `i` in its `IRBlock`. */
259+
private int getMemberIndex(Instruction i) {
260+
startsBasicBlock(i) and
261+
result = 0
262+
or
263+
exists(Instruction iPrev |
264+
adjacentInBlock(iPrev, i) and
265+
result = getMemberIndex(iPrev) + 1
266+
)
267+
}
268+
269+
private module BlockAdjacency = QlBuiltins::EquivalenceRelation<Instruction, adjacentInBlock/2>;
261270

262271
/** Holds if `i` is the `index`th instruction in `block`. */
263272
cached
264273
Instruction getInstruction(TIRBlock block, int index) {
265-
result = getInstructionFromFirst(getFirstInstruction(block), index)
274+
exists(Instruction first | block = MkIRBlock(first) |
275+
first = result and index = 0
276+
or
277+
index = getMemberIndex(result) and
278+
BlockAdjacency::getEquivalenceClass(first) = BlockAdjacency::getEquivalenceClass(result)
279+
)
266280
}
267281

268282
cached

csharp/ql/src/experimental/ir/implementation/raw/IRBlock.qll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,28 @@ private module Cached {
255255
cached
256256
newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) }
257257

258-
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
259-
private Instruction getInstructionFromFirst(Instruction first, int index) =
260-
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
258+
/** Gets the index of `i` in its `IRBlock`. */
259+
private int getMemberIndex(Instruction i) {
260+
startsBasicBlock(i) and
261+
result = 0
262+
or
263+
exists(Instruction iPrev |
264+
adjacentInBlock(iPrev, i) and
265+
result = getMemberIndex(iPrev) + 1
266+
)
267+
}
268+
269+
private module BlockAdjacency = QlBuiltins::EquivalenceRelation<Instruction, adjacentInBlock/2>;
261270

262271
/** Holds if `i` is the `index`th instruction in `block`. */
263272
cached
264273
Instruction getInstruction(TIRBlock block, int index) {
265-
result = getInstructionFromFirst(getFirstInstruction(block), index)
274+
exists(Instruction first | block = MkIRBlock(first) |
275+
first = result and index = 0
276+
or
277+
index = getMemberIndex(result) and
278+
BlockAdjacency::getEquivalenceClass(first) = BlockAdjacency::getEquivalenceClass(result)
279+
)
266280
}
267281

268282
cached

csharp/ql/src/experimental/ir/implementation/unaliased_ssa/IRBlock.qll

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,28 @@ private module Cached {
255255
cached
256256
newtype TIRBlock = MkIRBlock(Instruction firstInstr) { startsBasicBlock(firstInstr) }
257257

258-
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
259-
private Instruction getInstructionFromFirst(Instruction first, int index) =
260-
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
258+
/** Gets the index of `i` in its `IRBlock`. */
259+
private int getMemberIndex(Instruction i) {
260+
startsBasicBlock(i) and
261+
result = 0
262+
or
263+
exists(Instruction iPrev |
264+
adjacentInBlock(iPrev, i) and
265+
result = getMemberIndex(iPrev) + 1
266+
)
267+
}
268+
269+
private module BlockAdjacency = QlBuiltins::EquivalenceRelation<Instruction, adjacentInBlock/2>;
261270

262271
/** Holds if `i` is the `index`th instruction in `block`. */
263272
cached
264273
Instruction getInstruction(TIRBlock block, int index) {
265-
result = getInstructionFromFirst(getFirstInstruction(block), index)
274+
exists(Instruction first | block = MkIRBlock(first) |
275+
first = result and index = 0
276+
or
277+
index = getMemberIndex(result) and
278+
BlockAdjacency::getEquivalenceClass(first) = BlockAdjacency::getEquivalenceClass(result)
279+
)
266280
}
267281

268282
cached

0 commit comments

Comments
 (0)