From e368d5dda09bb02823ad662c26bc92c8b94a3aa7 Mon Sep 17 00:00:00 2001 From: Robert Marsh Date: Tue, 26 Nov 2019 16:02:30 -0500 Subject: [PATCH] C++: simplify getDisplayOrderInBlock --- .../aliased_ssa/Instruction.qll | 30 +++++++---------- .../cpp/ir/implementation/raw/Instruction.qll | 30 +++++++---------- .../unaliased_ssa/Instruction.qll | 30 +++++++---------- .../ir/ssa/aliased_ssa_ir.expected | 32 +++++++++---------- .../ir/ssa/unaliased_ssa_ir.expected | 16 +++++----- .../ir/implementation/raw/Instruction.qll | 30 +++++++---------- .../unaliased_ssa/Instruction.qll | 30 +++++++---------- 7 files changed, 84 insertions(+), 114 deletions(-) diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll index cc3e757889a..b93fad439dc 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll @@ -9,7 +9,6 @@ import Imports::IRType import Imports::MemoryAccessKind import Imports::Opcode private import Imports::OperandTag -import semmle.code.cpp.ir.implementation.internal.PrintSupport module InstructionSanity { /** @@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction { */ int getDisplayIndexInBlock() { exists(IRBlock block | - block = getBlock() and - ( - exists(int index, int phiCount | - phiCount = count(block.getAPhiInstruction()) and - this = block.getInstruction(index) and - result = index + phiCount + this = block.getInstruction(result) + or + this = rank[-result - 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | + phiInstr order by phiInstr.getUniqueId() ) - or - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() - | - phiInstr order by phiInstr.getUniqueId() - ) - ) ) } int getLineRank() { - this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr - order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() - ) + this = rank[result](Instruction instr | + instr.getAST().getFile() = getAST().getFile() and + instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() + | + instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() + ) } /** diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll index cc3e757889a..b93fad439dc 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/raw/Instruction.qll @@ -9,7 +9,6 @@ import Imports::IRType import Imports::MemoryAccessKind import Imports::Opcode private import Imports::OperandTag -import semmle.code.cpp.ir.implementation.internal.PrintSupport module InstructionSanity { /** @@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction { */ int getDisplayIndexInBlock() { exists(IRBlock block | - block = getBlock() and - ( - exists(int index, int phiCount | - phiCount = count(block.getAPhiInstruction()) and - this = block.getInstruction(index) and - result = index + phiCount + this = block.getInstruction(result) + or + this = rank[-result - 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | + phiInstr order by phiInstr.getUniqueId() ) - or - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() - | - phiInstr order by phiInstr.getUniqueId() - ) - ) ) } int getLineRank() { - this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr - order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() - ) + this = rank[result](Instruction instr | + instr.getAST().getFile() = getAST().getFile() and + instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() + | + instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() + ) } /** diff --git a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll index cc3e757889a..b93fad439dc 100644 --- a/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll +++ b/cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll @@ -9,7 +9,6 @@ import Imports::IRType import Imports::MemoryAccessKind import Imports::Opcode private import Imports::OperandTag -import semmle.code.cpp.ir.implementation.internal.PrintSupport module InstructionSanity { /** @@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction { */ int getDisplayIndexInBlock() { exists(IRBlock block | - block = getBlock() and - ( - exists(int index, int phiCount | - phiCount = count(block.getAPhiInstruction()) and - this = block.getInstruction(index) and - result = index + phiCount + this = block.getInstruction(result) + or + this = rank[-result - 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | + phiInstr order by phiInstr.getUniqueId() ) - or - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() - | - phiInstr order by phiInstr.getUniqueId() - ) - ) ) } int getLineRank() { - this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr - order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() - ) + this = rank[result](Instruction instr | + instr.getAST().getFile() = getAST().getFile() and + instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() + | + instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() + ) } /** diff --git a/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected b/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected index 5823ddf69ea..7e1c4e108cf 100644 --- a/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected +++ b/cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected @@ -211,26 +211,26 @@ ssa.cpp: # 70| Block 1 # 70| r70_1(char) = Constant[0] : # 70| r70_2(glval) = VariableAddress[p] : -# 70| r70_3(char *) = Load : &:r70_2, m69_3 +# 70| r70_3(char *) = Load : &:r70_2, m69_1 # 70| r70_4(int) = Constant[1] : # 70| r70_5(char *) = PointerAdd[1] : r70_3, r70_4 # 70| m70_6(char *) = Store : &:r70_2, r70_5 # 70| r70_7(glval) = CopyValue : r70_3 # 70| m70_8(char) = Store : &:r70_7, r70_1 -# 70| m70_9(unknown) = Chi : total:m69_1, partial:m70_8 +# 70| m70_9(unknown) = Chi : total:m69_3, partial:m70_8 #-----| Goto (back edge) -> Block 3 # 71| Block 2 # 71| v71_1(void) = NoOp : # 68| v68_8(void) = ReturnVoid : # 68| v68_9(void) = UnmodeledUse : mu* -# 68| v68_10(void) = AliasedUse : ~m69_1 +# 68| v68_10(void) = AliasedUse : ~m69_3 # 68| v68_11(void) = ExitFunction : # 69| Block 3 -# 69| m69_1(unknown) = Phi : from 0:~m68_2, from 1:~m70_9 +# 69| m69_1(char *) = Phi : from 0:m68_7, from 1:m70_6 # 69| m69_2(int) = Phi : from 0:m68_5, from 1:m69_8 -# 69| m69_3(char *) = Phi : from 0:m68_7, from 1:m70_6 +# 69| m69_3(unknown) = Phi : from 0:~m68_2, from 1:~m70_9 # 69| r69_4(glval) = VariableAddress[n] : # 69| r69_5(int) = Load : &:r69_4, m69_2 # 69| r69_6(int) = Constant[1] : @@ -280,15 +280,15 @@ ssa.cpp: #-----| Goto -> Block 3 # 86| Block 3 -# 86| m86_1(int) = Phi : from 1:m80_3, from 2:m84_3 -# 86| m86_2(int) = Phi : from 1:m81_3, from 2:m77_3 +# 86| m86_1(int) = Phi : from 1:m81_3, from 2:m77_3 +# 86| m86_2(int) = Phi : from 1:m80_3, from 2:m84_3 # 86| r86_3(glval) = VariableAddress[x_merge] : # 86| r86_4(glval) = VariableAddress[x] : -# 86| r86_5(int) = Load : &:r86_4, m86_1 +# 86| r86_5(int) = Load : &:r86_4, m86_2 # 86| m86_6(int) = Store : &:r86_3, r86_5 # 87| r87_1(glval) = VariableAddress[y_merge] : # 87| r87_2(glval) = VariableAddress[y] : -# 87| r87_3(int) = Load : &:r87_2, m86_2 +# 87| r87_3(int) = Load : &:r87_2, m86_1 # 87| m87_4(int) = Store : &:r87_1, r87_3 # 88| r88_1(glval) = VariableAddress[z_merge] : # 88| r88_2(glval) = VariableAddress[z] : @@ -521,16 +521,16 @@ ssa.cpp: #-----| Goto -> Block 3 # 130| Block 3 -# 130| m130_1(Point) = Phi : from 1:~m125_6, from 2:~m128_6 -# 130| m130_2(int) = Phi : from 1:m125_5, from 2:m128_5 +# 130| m130_1(int) = Phi : from 1:m125_5, from 2:m128_5 +# 130| m130_2(Point) = Phi : from 1:~m125_6, from 2:~m128_6 # 130| r130_3(glval) = VariableAddress[x] : # 130| r130_4(glval) = VariableAddress[a] : # 130| r130_5(glval) = FieldAddress[x] : r130_4 -# 130| r130_6(int) = Load : &:r130_5, m130_2 +# 130| r130_6(int) = Load : &:r130_5, m130_1 # 130| m130_7(int) = Store : &:r130_3, r130_6 # 131| r131_1(glval) = VariableAddress[b] : # 131| r131_2(glval) = VariableAddress[a] : -# 131| r131_3(Point) = Load : &:r131_2, m130_1 +# 131| r131_3(Point) = Load : &:r131_2, m130_2 # 131| m131_4(Point) = Store : &:r131_1, r131_3 # 132| v132_1(void) = NoOp : # 122| v122_10(void) = ReturnVoid : @@ -582,12 +582,12 @@ ssa.cpp: #-----| Goto -> Block 3 # 142| Block 3 -# 142| m142_1(Point) = Phi : from 1:~m137_6, from 2:m140_4 -# 142| m142_2(int) = Phi : from 1:m137_5, from 2:~m140_4 +# 142| m142_1(int) = Phi : from 1:m137_5, from 2:~m140_4 +# 142| m142_2(Point) = Phi : from 1:~m137_6, from 2:m140_4 # 142| r142_3(glval) = VariableAddress[x] : # 142| r142_4(glval) = VariableAddress[a] : # 142| r142_5(glval) = FieldAddress[x] : r142_4 -# 142| r142_6(int) = Load : &:r142_5, m142_2 +# 142| r142_6(int) = Load : &:r142_5, m142_1 # 142| m142_7(int) = Store : &:r142_3, r142_6 # 143| v143_1(void) = NoOp : # 134| v134_10(void) = ReturnVoid : diff --git a/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected b/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected index ad3333ca697..b44a576518a 100644 --- a/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected +++ b/cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected @@ -214,7 +214,7 @@ ssa.cpp: # 70| Block 1 # 70| r70_1(char) = Constant[0] : # 70| r70_2(glval) = VariableAddress[p] : -# 70| r70_3(char *) = Load : &:r70_2, m69_2 +# 70| r70_3(char *) = Load : &:r70_2, m69_1 # 70| r70_4(int) = Constant[1] : # 70| r70_5(char *) = PointerAdd[1] : r70_3, r70_4 # 70| m70_6(char *) = Store : &:r70_2, r70_5 @@ -230,10 +230,10 @@ ssa.cpp: # 68| v68_11(void) = ExitFunction : # 69| Block 3 -# 69| m69_1(int) = Phi : from 0:m68_5, from 1:m69_7 -# 69| m69_2(char *) = Phi : from 0:m68_7, from 1:m70_6 +# 69| m69_1(char *) = Phi : from 0:m68_7, from 1:m70_6 +# 69| m69_2(int) = Phi : from 0:m68_5, from 1:m69_7 # 69| r69_3(glval) = VariableAddress[n] : -# 69| r69_4(int) = Load : &:r69_3, m69_1 +# 69| r69_4(int) = Load : &:r69_3, m69_2 # 69| r69_5(int) = Constant[1] : # 69| r69_6(int) = Sub : r69_4, r69_5 # 69| m69_7(int) = Store : &:r69_3, r69_6 @@ -281,15 +281,15 @@ ssa.cpp: #-----| Goto -> Block 3 # 86| Block 3 -# 86| m86_1(int) = Phi : from 1:m80_3, from 2:m84_3 -# 86| m86_2(int) = Phi : from 1:m81_3, from 2:m77_3 +# 86| m86_1(int) = Phi : from 1:m81_3, from 2:m77_3 +# 86| m86_2(int) = Phi : from 1:m80_3, from 2:m84_3 # 86| r86_3(glval) = VariableAddress[x_merge] : # 86| r86_4(glval) = VariableAddress[x] : -# 86| r86_5(int) = Load : &:r86_4, m86_1 +# 86| r86_5(int) = Load : &:r86_4, m86_2 # 86| m86_6(int) = Store : &:r86_3, r86_5 # 87| r87_1(glval) = VariableAddress[y_merge] : # 87| r87_2(glval) = VariableAddress[y] : -# 87| r87_3(int) = Load : &:r87_2, m86_2 +# 87| r87_3(int) = Load : &:r87_2, m86_1 # 87| m87_4(int) = Store : &:r87_1, r87_3 # 88| r88_1(glval) = VariableAddress[z_merge] : # 88| r88_2(glval) = VariableAddress[z] : diff --git a/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll b/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll index cc3e757889a..b93fad439dc 100644 --- a/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll +++ b/csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll @@ -9,7 +9,6 @@ import Imports::IRType import Imports::MemoryAccessKind import Imports::Opcode private import Imports::OperandTag -import semmle.code.cpp.ir.implementation.internal.PrintSupport module InstructionSanity { /** @@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction { */ int getDisplayIndexInBlock() { exists(IRBlock block | - block = getBlock() and - ( - exists(int index, int phiCount | - phiCount = count(block.getAPhiInstruction()) and - this = block.getInstruction(index) and - result = index + phiCount + this = block.getInstruction(result) + or + this = rank[-result - 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | + phiInstr order by phiInstr.getUniqueId() ) - or - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() - | - phiInstr order by phiInstr.getUniqueId() - ) - ) ) } int getLineRank() { - this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr - order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() - ) + this = rank[result](Instruction instr | + instr.getAST().getFile() = getAST().getFile() and + instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() + | + instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() + ) } /** diff --git a/csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Instruction.qll b/csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Instruction.qll index cc3e757889a..b93fad439dc 100644 --- a/csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Instruction.qll +++ b/csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Instruction.qll @@ -9,7 +9,6 @@ import Imports::IRType import Imports::MemoryAccessKind import Imports::Opcode private import Imports::OperandTag -import semmle.code.cpp.ir.implementation.internal.PrintSupport module InstructionSanity { /** @@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction { */ int getDisplayIndexInBlock() { exists(IRBlock block | - block = getBlock() and - ( - exists(int index, int phiCount | - phiCount = count(block.getAPhiInstruction()) and - this = block.getInstruction(index) and - result = index + phiCount + this = block.getInstruction(result) + or + this = rank[-result - 1](PhiInstruction phiInstr | + phiInstr = block.getAPhiInstruction() + | + phiInstr order by phiInstr.getUniqueId() ) - or - this instanceof PhiInstruction and - this = rank[result + 1](PhiInstruction phiInstr | - phiInstr = block.getAPhiInstruction() - | - phiInstr order by phiInstr.getUniqueId() - ) - ) ) } int getLineRank() { - this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr - order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() - ) + this = rank[result](Instruction instr | + instr.getAST().getFile() = getAST().getFile() and + instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() + | + instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock() + ) } /**