Skip to content

Commit e368d5d

Browse files
author
Robert Marsh
committed
C++: simplify getDisplayOrderInBlock
1 parent 60b384a commit e368d5d

File tree

7 files changed

+84
-114
lines changed

7 files changed

+84
-114
lines changed

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12-
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1312

1413
module InstructionSanity {
1514
/**
@@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction {
351350
*/
352351
int getDisplayIndexInBlock() {
353352
exists(IRBlock block |
354-
block = getBlock() and
355-
(
356-
exists(int index, int phiCount |
357-
phiCount = count(block.getAPhiInstruction()) and
358-
this = block.getInstruction(index) and
359-
result = index + phiCount
353+
this = block.getInstruction(result)
354+
or
355+
this = rank[-result - 1](PhiInstruction phiInstr |
356+
phiInstr = block.getAPhiInstruction()
357+
|
358+
phiInstr order by phiInstr.getUniqueId()
360359
)
361-
or
362-
this instanceof PhiInstruction and
363-
this = rank[result + 1](PhiInstruction phiInstr |
364-
phiInstr = block.getAPhiInstruction()
365-
|
366-
phiInstr order by phiInstr.getUniqueId()
367-
)
368-
)
369360
)
370361
}
371362

372363
int getLineRank() {
373-
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374-
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375-
)
364+
this = rank[result](Instruction instr |
365+
instr.getAST().getFile() = getAST().getFile() and
366+
instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine()
367+
|
368+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
369+
)
376370
}
377371

378372
/**

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12-
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1312

1413
module InstructionSanity {
1514
/**
@@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction {
351350
*/
352351
int getDisplayIndexInBlock() {
353352
exists(IRBlock block |
354-
block = getBlock() and
355-
(
356-
exists(int index, int phiCount |
357-
phiCount = count(block.getAPhiInstruction()) and
358-
this = block.getInstruction(index) and
359-
result = index + phiCount
353+
this = block.getInstruction(result)
354+
or
355+
this = rank[-result - 1](PhiInstruction phiInstr |
356+
phiInstr = block.getAPhiInstruction()
357+
|
358+
phiInstr order by phiInstr.getUniqueId()
360359
)
361-
or
362-
this instanceof PhiInstruction and
363-
this = rank[result + 1](PhiInstruction phiInstr |
364-
phiInstr = block.getAPhiInstruction()
365-
|
366-
phiInstr order by phiInstr.getUniqueId()
367-
)
368-
)
369360
)
370361
}
371362

372363
int getLineRank() {
373-
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374-
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375-
)
364+
this = rank[result](Instruction instr |
365+
instr.getAST().getFile() = getAST().getFile() and
366+
instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine()
367+
|
368+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
369+
)
376370
}
377371

378372
/**

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12-
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1312

1413
module InstructionSanity {
1514
/**
@@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction {
351350
*/
352351
int getDisplayIndexInBlock() {
353352
exists(IRBlock block |
354-
block = getBlock() and
355-
(
356-
exists(int index, int phiCount |
357-
phiCount = count(block.getAPhiInstruction()) and
358-
this = block.getInstruction(index) and
359-
result = index + phiCount
353+
this = block.getInstruction(result)
354+
or
355+
this = rank[-result - 1](PhiInstruction phiInstr |
356+
phiInstr = block.getAPhiInstruction()
357+
|
358+
phiInstr order by phiInstr.getUniqueId()
360359
)
361-
or
362-
this instanceof PhiInstruction and
363-
this = rank[result + 1](PhiInstruction phiInstr |
364-
phiInstr = block.getAPhiInstruction()
365-
|
366-
phiInstr order by phiInstr.getUniqueId()
367-
)
368-
)
369360
)
370361
}
371362

372363
int getLineRank() {
373-
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374-
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375-
)
364+
this = rank[result](Instruction instr |
365+
instr.getAST().getFile() = getAST().getFile() and
366+
instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine()
367+
|
368+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
369+
)
376370
}
377371

378372
/**

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,26 @@ ssa.cpp:
211211
# 70| Block 1
212212
# 70| r70_1(char) = Constant[0] :
213213
# 70| r70_2(glval<char *>) = VariableAddress[p] :
214-
# 70| r70_3(char *) = Load : &:r70_2, m69_3
214+
# 70| r70_3(char *) = Load : &:r70_2, m69_1
215215
# 70| r70_4(int) = Constant[1] :
216216
# 70| r70_5(char *) = PointerAdd[1] : r70_3, r70_4
217217
# 70| m70_6(char *) = Store : &:r70_2, r70_5
218218
# 70| r70_7(glval<char>) = CopyValue : r70_3
219219
# 70| m70_8(char) = Store : &:r70_7, r70_1
220-
# 70| m70_9(unknown) = Chi : total:m69_1, partial:m70_8
220+
# 70| m70_9(unknown) = Chi : total:m69_3, partial:m70_8
221221
#-----| Goto (back edge) -> Block 3
222222

223223
# 71| Block 2
224224
# 71| v71_1(void) = NoOp :
225225
# 68| v68_8(void) = ReturnVoid :
226226
# 68| v68_9(void) = UnmodeledUse : mu*
227-
# 68| v68_10(void) = AliasedUse : ~m69_1
227+
# 68| v68_10(void) = AliasedUse : ~m69_3
228228
# 68| v68_11(void) = ExitFunction :
229229

230230
# 69| Block 3
231-
# 69| m69_1(unknown) = Phi : from 0:~m68_2, from 1:~m70_9
231+
# 69| m69_1(char *) = Phi : from 0:m68_7, from 1:m70_6
232232
# 69| m69_2(int) = Phi : from 0:m68_5, from 1:m69_8
233-
# 69| m69_3(char *) = Phi : from 0:m68_7, from 1:m70_6
233+
# 69| m69_3(unknown) = Phi : from 0:~m68_2, from 1:~m70_9
234234
# 69| r69_4(glval<int>) = VariableAddress[n] :
235235
# 69| r69_5(int) = Load : &:r69_4, m69_2
236236
# 69| r69_6(int) = Constant[1] :
@@ -280,15 +280,15 @@ ssa.cpp:
280280
#-----| Goto -> Block 3
281281

282282
# 86| Block 3
283-
# 86| m86_1(int) = Phi : from 1:m80_3, from 2:m84_3
284-
# 86| m86_2(int) = Phi : from 1:m81_3, from 2:m77_3
283+
# 86| m86_1(int) = Phi : from 1:m81_3, from 2:m77_3
284+
# 86| m86_2(int) = Phi : from 1:m80_3, from 2:m84_3
285285
# 86| r86_3(glval<int>) = VariableAddress[x_merge] :
286286
# 86| r86_4(glval<int>) = VariableAddress[x] :
287-
# 86| r86_5(int) = Load : &:r86_4, m86_1
287+
# 86| r86_5(int) = Load : &:r86_4, m86_2
288288
# 86| m86_6(int) = Store : &:r86_3, r86_5
289289
# 87| r87_1(glval<int>) = VariableAddress[y_merge] :
290290
# 87| r87_2(glval<int>) = VariableAddress[y] :
291-
# 87| r87_3(int) = Load : &:r87_2, m86_2
291+
# 87| r87_3(int) = Load : &:r87_2, m86_1
292292
# 87| m87_4(int) = Store : &:r87_1, r87_3
293293
# 88| r88_1(glval<int>) = VariableAddress[z_merge] :
294294
# 88| r88_2(glval<int>) = VariableAddress[z] :
@@ -521,16 +521,16 @@ ssa.cpp:
521521
#-----| Goto -> Block 3
522522

523523
# 130| Block 3
524-
# 130| m130_1(Point) = Phi : from 1:~m125_6, from 2:~m128_6
525-
# 130| m130_2(int) = Phi : from 1:m125_5, from 2:m128_5
524+
# 130| m130_1(int) = Phi : from 1:m125_5, from 2:m128_5
525+
# 130| m130_2(Point) = Phi : from 1:~m125_6, from 2:~m128_6
526526
# 130| r130_3(glval<int>) = VariableAddress[x] :
527527
# 130| r130_4(glval<Point>) = VariableAddress[a] :
528528
# 130| r130_5(glval<int>) = FieldAddress[x] : r130_4
529-
# 130| r130_6(int) = Load : &:r130_5, m130_2
529+
# 130| r130_6(int) = Load : &:r130_5, m130_1
530530
# 130| m130_7(int) = Store : &:r130_3, r130_6
531531
# 131| r131_1(glval<Point>) = VariableAddress[b] :
532532
# 131| r131_2(glval<Point>) = VariableAddress[a] :
533-
# 131| r131_3(Point) = Load : &:r131_2, m130_1
533+
# 131| r131_3(Point) = Load : &:r131_2, m130_2
534534
# 131| m131_4(Point) = Store : &:r131_1, r131_3
535535
# 132| v132_1(void) = NoOp :
536536
# 122| v122_10(void) = ReturnVoid :
@@ -582,12 +582,12 @@ ssa.cpp:
582582
#-----| Goto -> Block 3
583583

584584
# 142| Block 3
585-
# 142| m142_1(Point) = Phi : from 1:~m137_6, from 2:m140_4
586-
# 142| m142_2(int) = Phi : from 1:m137_5, from 2:~m140_4
585+
# 142| m142_1(int) = Phi : from 1:m137_5, from 2:~m140_4
586+
# 142| m142_2(Point) = Phi : from 1:~m137_6, from 2:m140_4
587587
# 142| r142_3(glval<int>) = VariableAddress[x] :
588588
# 142| r142_4(glval<Point>) = VariableAddress[a] :
589589
# 142| r142_5(glval<int>) = FieldAddress[x] : r142_4
590-
# 142| r142_6(int) = Load : &:r142_5, m142_2
590+
# 142| r142_6(int) = Load : &:r142_5, m142_1
591591
# 142| m142_7(int) = Store : &:r142_3, r142_6
592592
# 143| v143_1(void) = NoOp :
593593
# 134| v134_10(void) = ReturnVoid :

cpp/ql/test/library-tests/ir/ssa/unaliased_ssa_ir.expected

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ ssa.cpp:
214214
# 70| Block 1
215215
# 70| r70_1(char) = Constant[0] :
216216
# 70| r70_2(glval<char *>) = VariableAddress[p] :
217-
# 70| r70_3(char *) = Load : &:r70_2, m69_2
217+
# 70| r70_3(char *) = Load : &:r70_2, m69_1
218218
# 70| r70_4(int) = Constant[1] :
219219
# 70| r70_5(char *) = PointerAdd[1] : r70_3, r70_4
220220
# 70| m70_6(char *) = Store : &:r70_2, r70_5
@@ -230,10 +230,10 @@ ssa.cpp:
230230
# 68| v68_11(void) = ExitFunction :
231231

232232
# 69| Block 3
233-
# 69| m69_1(int) = Phi : from 0:m68_5, from 1:m69_7
234-
# 69| m69_2(char *) = Phi : from 0:m68_7, from 1:m70_6
233+
# 69| m69_1(char *) = Phi : from 0:m68_7, from 1:m70_6
234+
# 69| m69_2(int) = Phi : from 0:m68_5, from 1:m69_7
235235
# 69| r69_3(glval<int>) = VariableAddress[n] :
236-
# 69| r69_4(int) = Load : &:r69_3, m69_1
236+
# 69| r69_4(int) = Load : &:r69_3, m69_2
237237
# 69| r69_5(int) = Constant[1] :
238238
# 69| r69_6(int) = Sub : r69_4, r69_5
239239
# 69| m69_7(int) = Store : &:r69_3, r69_6
@@ -281,15 +281,15 @@ ssa.cpp:
281281
#-----| Goto -> Block 3
282282

283283
# 86| Block 3
284-
# 86| m86_1(int) = Phi : from 1:m80_3, from 2:m84_3
285-
# 86| m86_2(int) = Phi : from 1:m81_3, from 2:m77_3
284+
# 86| m86_1(int) = Phi : from 1:m81_3, from 2:m77_3
285+
# 86| m86_2(int) = Phi : from 1:m80_3, from 2:m84_3
286286
# 86| r86_3(glval<int>) = VariableAddress[x_merge] :
287287
# 86| r86_4(glval<int>) = VariableAddress[x] :
288-
# 86| r86_5(int) = Load : &:r86_4, m86_1
288+
# 86| r86_5(int) = Load : &:r86_4, m86_2
289289
# 86| m86_6(int) = Store : &:r86_3, r86_5
290290
# 87| r87_1(glval<int>) = VariableAddress[y_merge] :
291291
# 87| r87_2(glval<int>) = VariableAddress[y] :
292-
# 87| r87_3(int) = Load : &:r87_2, m86_2
292+
# 87| r87_3(int) = Load : &:r87_2, m86_1
293293
# 87| m87_4(int) = Store : &:r87_1, r87_3
294294
# 88| r88_1(glval<int>) = VariableAddress[z_merge] :
295295
# 88| r88_2(glval<int>) = VariableAddress[z] :

csharp/ql/src/semmle/code/csharp/ir/implementation/raw/Instruction.qll

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12-
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1312

1413
module InstructionSanity {
1514
/**
@@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction {
351350
*/
352351
int getDisplayIndexInBlock() {
353352
exists(IRBlock block |
354-
block = getBlock() and
355-
(
356-
exists(int index, int phiCount |
357-
phiCount = count(block.getAPhiInstruction()) and
358-
this = block.getInstruction(index) and
359-
result = index + phiCount
353+
this = block.getInstruction(result)
354+
or
355+
this = rank[-result - 1](PhiInstruction phiInstr |
356+
phiInstr = block.getAPhiInstruction()
357+
|
358+
phiInstr order by phiInstr.getUniqueId()
360359
)
361-
or
362-
this instanceof PhiInstruction and
363-
this = rank[result + 1](PhiInstruction phiInstr |
364-
phiInstr = block.getAPhiInstruction()
365-
|
366-
phiInstr order by phiInstr.getUniqueId()
367-
)
368-
)
369360
)
370361
}
371362

372363
int getLineRank() {
373-
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374-
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375-
)
364+
this = rank[result](Instruction instr |
365+
instr.getAST().getFile() = getAST().getFile() and
366+
instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine()
367+
|
368+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
369+
)
376370
}
377371

378372
/**

csharp/ql/src/semmle/code/csharp/ir/implementation/unaliased_ssa/Instruction.qll

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Imports::IRType
99
import Imports::MemoryAccessKind
1010
import Imports::Opcode
1111
private import Imports::OperandTag
12-
import semmle.code.cpp.ir.implementation.internal.PrintSupport
1312

1413
module InstructionSanity {
1514
/**
@@ -351,28 +350,23 @@ class Instruction extends Construction::TInstruction {
351350
*/
352351
int getDisplayIndexInBlock() {
353352
exists(IRBlock block |
354-
block = getBlock() and
355-
(
356-
exists(int index, int phiCount |
357-
phiCount = count(block.getAPhiInstruction()) and
358-
this = block.getInstruction(index) and
359-
result = index + phiCount
353+
this = block.getInstruction(result)
354+
or
355+
this = rank[-result - 1](PhiInstruction phiInstr |
356+
phiInstr = block.getAPhiInstruction()
357+
|
358+
phiInstr order by phiInstr.getUniqueId()
360359
)
361-
or
362-
this instanceof PhiInstruction and
363-
this = rank[result + 1](PhiInstruction phiInstr |
364-
phiInstr = block.getAPhiInstruction()
365-
|
366-
phiInstr order by phiInstr.getUniqueId()
367-
)
368-
)
369360
)
370361
}
371362

372363
int getLineRank() {
373-
this = rank[result](Instruction instr | instr.getAST().getFile() = getAST().getFile() and instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine() | instr
374-
order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
375-
)
364+
this = rank[result](Instruction instr |
365+
instr.getAST().getFile() = getAST().getFile() and
366+
instr.getAST().getLocation().getStartLine() = getAST().getLocation().getStartLine()
367+
|
368+
instr order by instr.getBlock().getDisplayIndex(), instr.getDisplayIndexInBlock()
369+
)
376370
}
377371

378372
/**

0 commit comments

Comments
 (0)