Skip to content

Commit 411c285

Browse files
authored
Merge pull request #870 from jbj/ir-shortestDistances
C++: Use shortestDistances HOP for IR BB indexes
2 parents 3a092fa + 8368c37 commit 411c285

File tree

3 files changed

+12
-45
lines changed

3 files changed

+12
-45
lines changed

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,13 @@ private cached module Cached {
159159
not startsBasicBlock(i2)
160160
}
161161

162-
/** Gets the index of `i` in its `IRBlock`. */
163-
private int getMemberIndex(Instruction i) {
164-
startsBasicBlock(i) and
165-
result = 0
166-
or
167-
exists(Instruction iPrev |
168-
adjacentInBlock(iPrev, i) and
169-
result = getMemberIndex(iPrev) + 1
170-
)
171-
}
162+
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
163+
private Instruction getInstructionFromFirst(Instruction first, int index) =
164+
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
172165

173166
/** Holds if `i` is the `index`th instruction in `block`. */
174167
cached Instruction getInstruction(TIRBlock block, int index) {
175-
exists(Instruction first |
176-
block = MkIRBlock(first) and
177-
index = getMemberIndex(result) and
178-
adjacentInBlock*(first, result)
179-
)
168+
result = getInstructionFromFirst(getFirstInstruction(block), index)
180169
}
181170

182171
cached int getInstructionCount(TIRBlock block) {

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,13 @@ private cached module Cached {
159159
not startsBasicBlock(i2)
160160
}
161161

162-
/** Gets the index of `i` in its `IRBlock`. */
163-
private int getMemberIndex(Instruction i) {
164-
startsBasicBlock(i) and
165-
result = 0
166-
or
167-
exists(Instruction iPrev |
168-
adjacentInBlock(iPrev, i) and
169-
result = getMemberIndex(iPrev) + 1
170-
)
171-
}
162+
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
163+
private Instruction getInstructionFromFirst(Instruction first, int index) =
164+
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
172165

173166
/** Holds if `i` is the `index`th instruction in `block`. */
174167
cached Instruction getInstruction(TIRBlock block, int index) {
175-
exists(Instruction first |
176-
block = MkIRBlock(first) and
177-
index = getMemberIndex(result) and
178-
adjacentInBlock*(first, result)
179-
)
168+
result = getInstructionFromFirst(getFirstInstruction(block), index)
180169
}
181170

182171
cached int getInstructionCount(TIRBlock block) {

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -159,24 +159,13 @@ private cached module Cached {
159159
not startsBasicBlock(i2)
160160
}
161161

162-
/** Gets the index of `i` in its `IRBlock`. */
163-
private int getMemberIndex(Instruction i) {
164-
startsBasicBlock(i) and
165-
result = 0
166-
or
167-
exists(Instruction iPrev |
168-
adjacentInBlock(iPrev, i) and
169-
result = getMemberIndex(iPrev) + 1
170-
)
171-
}
162+
/** Holds if `i` is the `index`th instruction the block starting with `first`. */
163+
private Instruction getInstructionFromFirst(Instruction first, int index) =
164+
shortestDistances(startsBasicBlock/1, adjacentInBlock/2)(first, result, index)
172165

173166
/** Holds if `i` is the `index`th instruction in `block`. */
174167
cached Instruction getInstruction(TIRBlock block, int index) {
175-
exists(Instruction first |
176-
block = MkIRBlock(first) and
177-
index = getMemberIndex(result) and
178-
adjacentInBlock*(first, result)
179-
)
168+
result = getInstructionFromFirst(getFirstInstruction(block), index)
180169
}
181170

182171
cached int getInstructionCount(TIRBlock block) {

0 commit comments

Comments
 (0)