From be24c6418b7f017546711be2f8b1a441088c947b Mon Sep 17 00:00:00 2001 From: Jonas Jensen Date: Mon, 26 Aug 2019 11:46:17 +0200 Subject: [PATCH 1/3] C++: Optimize SubBasicBlock::getNumberOfNodes() --- .../code/cpp/dataflow/internal/SubBasicBlocks.qll | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll index ad9c3a7331a4..27728a481517 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll @@ -76,7 +76,6 @@ class SubBasicBlock extends ControlFlowNodeBase { */ int getPosInBasicBlock(BasicBlock bb) { exists(int nodePos, int rnk | - bb = this.(ControlFlowNode).getBasicBlock() and this = bb.getNode(nodePos) and nodePos = rank[rnk](int i | exists(SubBasicBlock n | n = bb.getNode(i))) and result = rnk - 1 @@ -147,14 +146,20 @@ class SubBasicBlock extends ControlFlowNodeBase { * Gets the number of control-flow nodes in this `SubBasicBlock`. There is * always at least one. */ + pragma[noopt] int getNumberOfNodes() { exists(BasicBlock bb | bb = this.getBasicBlock() | exists(int thisPos | this = bb.getNode(thisPos) | - this.lastInBB() and - result = bb.length() - thisPos + exists(int bbLength | + this.lastInBB() and + bbLength = bb.length() and + result = bbLength - thisPos + ) or - exists(SubBasicBlock succ, int succPos | - succ.getPosInBasicBlock(bb) = this.getPosInBasicBlock(bb) + 1 and + exists(SubBasicBlock succ, int succPos, int thisRank, int succRank | + thisRank = this.getPosInBasicBlock(bb) and + succRank = thisRank + 1 and + succRank = succ.getPosInBasicBlock(bb) and bb.getNode(succPos) = succ and result = succPos - thisPos ) From d4f1cf97fddb90b3cb0f878a876b34297bcc1a8e Mon Sep 17 00:00:00 2001 From: Jonas Jensen Date: Mon, 26 Aug 2019 12:22:07 +0200 Subject: [PATCH 2/3] C++: Optimize SubBasicBlock::getNode(int) --- .../cpp/dataflow/internal/SubBasicBlocks.qll | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll index 27728a481517..963ed7018ea7 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll @@ -97,16 +97,24 @@ class SubBasicBlock extends ControlFlowNodeBase { * start from 0, and the node at position 0 always exists and compares equal * to `this`. */ + pragma[nomagic] ControlFlowNode getNode(int pos) { - exists(BasicBlock bb | bb = this.getBasicBlock() | - exists(int thisPos | this = bb.getNode(thisPos) | - result = bb.getNode(thisPos + pos) and - pos >= 0 and - pos < this.getNumberOfNodes() + exists(BasicBlock bb | + exists(int outerPos | + result = bb.getNode(outerPos) and + pos = outerPosToInnerPos(bb, outerPos) ) ) } + pragma[nomagic] + private int outerPosToInnerPos(BasicBlock bb, int posInBB) { + exists(int thisPosInBB | this = bb.getNode(thisPosInBB) | + posInBB = result + thisPosInBB and + result = [ 0 .. this.getNumberOfNodes() - 1 ] + ) + } + /** Gets a control-flow node in this `SubBasicBlock`. */ ControlFlowNode getANode() { result = this.getNode(_) From 5e674825e3deb1d2ae898475b236bf76fcacda43 Mon Sep 17 00:00:00 2001 From: Jonas Jensen Date: Mon, 26 Aug 2019 12:47:40 +0200 Subject: [PATCH 3/3] C++: Optimize SubBasicBlock::getPosInBasicBlock() --- .../cpp/dataflow/internal/SubBasicBlocks.qll | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll b/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll index 963ed7018ea7..e6ce8c4ae9a3 100644 --- a/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll +++ b/cpp/ql/src/semmle/code/cpp/dataflow/internal/SubBasicBlocks.qll @@ -75,13 +75,18 @@ class SubBasicBlock extends ControlFlowNodeBase { * `bb`, where `bb` is equal to `getBasicBlock()`. */ int getPosInBasicBlock(BasicBlock bb) { - exists(int nodePos, int rnk | - this = bb.getNode(nodePos) and - nodePos = rank[rnk](int i | exists(SubBasicBlock n | n = bb.getNode(i))) and + exists(int thisIndexInBB, int rnk | + thisIndexInBB = this.getIndexInBasicBlock(bb) and + thisIndexInBB = rank[rnk](int i | i = any(SubBasicBlock n).getIndexInBasicBlock(bb)) and result = rnk - 1 ) } + pragma[noinline] + private int getIndexInBasicBlock(BasicBlock bb) { + this = bb.getNode(result) + } + /** Gets a successor in the control-flow graph of `SubBasicBlock`s. */ SubBasicBlock getASuccessor() { this.lastInBB() and @@ -109,10 +114,8 @@ class SubBasicBlock extends ControlFlowNodeBase { pragma[nomagic] private int outerPosToInnerPos(BasicBlock bb, int posInBB) { - exists(int thisPosInBB | this = bb.getNode(thisPosInBB) | - posInBB = result + thisPosInBB and - result = [ 0 .. this.getNumberOfNodes() - 1 ] - ) + posInBB = result + this.getIndexInBasicBlock(bb) and + result = [ 0 .. this.getNumberOfNodes() - 1 ] } /** Gets a control-flow node in this `SubBasicBlock`. */