Skip to content

Commit d4f1cf9

Browse files
committed
C++: Optimize SubBasicBlock::getNode(int)
1 parent be24c64 commit d4f1cf9

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,24 @@ class SubBasicBlock extends ControlFlowNodeBase {
9797
* start from 0, and the node at position 0 always exists and compares equal
9898
* to `this`.
9999
*/
100+
pragma[nomagic]
100101
ControlFlowNode getNode(int pos) {
101-
exists(BasicBlock bb | bb = this.getBasicBlock() |
102-
exists(int thisPos | this = bb.getNode(thisPos) |
103-
result = bb.getNode(thisPos + pos) and
104-
pos >= 0 and
105-
pos < this.getNumberOfNodes()
102+
exists(BasicBlock bb |
103+
exists(int outerPos |
104+
result = bb.getNode(outerPos) and
105+
pos = outerPosToInnerPos(bb, outerPos)
106106
)
107107
)
108108
}
109109

110+
pragma[nomagic]
111+
private int outerPosToInnerPos(BasicBlock bb, int posInBB) {
112+
exists(int thisPosInBB | this = bb.getNode(thisPosInBB) |
113+
posInBB = result + thisPosInBB and
114+
result = [ 0 .. this.getNumberOfNodes() - 1 ]
115+
)
116+
}
117+
110118
/** Gets a control-flow node in this `SubBasicBlock`. */
111119
ControlFlowNode getANode() {
112120
result = this.getNode(_)

0 commit comments

Comments
 (0)