Skip to content

Commit cf8b2d5

Browse files
committed
C++: Hide away the 'multiply by two' hack in a predicate.
1 parent 8db7ece commit cf8b2d5

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,14 @@ private import DefUse
697697
* potentially very sparse.
698698
*/
699699
module DefUse {
700+
bindingset[index, block]
701+
pragma[inline_late]
702+
private int getNonChiOffset(int index, OldBlock block) { result = 2 * index }
703+
704+
bindingset[index, block]
705+
pragma[inline_late]
706+
private int getChiOffset(int index, OldBlock block) { result = getNonChiOffset(index, block) + 1 }
707+
700708
/**
701709
* Gets the `Instruction` for the definition at offset `defOffset` in block `defBlock`.
702710
*/
@@ -709,7 +717,7 @@ module DefUse {
709717
oldOffset >= 0
710718
|
711719
// An odd offset corresponds to the `Chi` instruction.
712-
defOffset = oldOffset * 2 + 1 and
720+
defOffset = getChiOffset(oldOffset, defBlock) and
713721
result = getChi(oldInstr) and
714722
(
715723
defLocation = Alias::getResultMemoryLocation(oldInstr) or
@@ -718,7 +726,7 @@ module DefUse {
718726
actualDefLocation = defLocation.getVirtualVariable()
719727
or
720728
// An even offset corresponds to the original instruction.
721-
defOffset = oldOffset * 2 and
729+
defOffset = getNonChiOffset(oldOffset, defBlock) and
722730
result = getNewInstruction(oldInstr) and
723731
(
724732
defLocation = Alias::getResultMemoryLocation(oldInstr) or
@@ -871,8 +879,8 @@ module DefUse {
871879
block.getInstruction(index) = def and
872880
overlap = Alias::getOverlap(defLocation, useLocation) and
873881
if overlap instanceof MayPartiallyOverlap
874-
then offset = (index * 2) + 1 // The use will be connected to the definition on the `Chi` instruction.
875-
else offset = index * 2 // The use will be connected to the definition on the original instruction.
882+
then offset = getChiOffset(index, block) // The use will be connected to the definition on the `Chi` instruction.
883+
else offset = getNonChiOffset(index, block) // The use will be connected to the definition on the original instruction.
876884
)
877885
}
878886

@@ -933,10 +941,11 @@ module DefUse {
933941
block.getInstruction(index) = use and
934942
(
935943
// A direct use of the location.
936-
useLocation = Alias::getOperandMemoryLocation(use.getAnOperand()) and offset = index * 2
944+
useLocation = Alias::getOperandMemoryLocation(use.getAnOperand()) and
945+
offset = getNonChiOffset(index, block)
937946
or
938947
// A `Chi` instruction will include a use of the virtual variable.
939-
hasChiNode(useLocation, use) and offset = (index * 2) + 1
948+
hasChiNode(useLocation, use) and offset = getChiOffset(index, block)
940949
)
941950
)
942951
}

0 commit comments

Comments
 (0)