11private import IRInternal
2+ private import IRBlockConstruction
23import Instruction
3- import cpp
44import semmle.code.cpp.ir.EdgeKind
55
6- private predicate startsBasicBlock ( Instruction instr ) {
7- not instr instanceof PhiInstruction and
8- (
9- count ( Instruction predecessor |
10- instr = predecessor .getASuccessor ( )
11- ) != 1 or // Multiple predecessors or no predecessor
12- exists ( Instruction predecessor |
13- instr = predecessor .getASuccessor ( ) and
14- strictcount ( Instruction other |
15- other = predecessor .getASuccessor ( )
16- ) > 1
17- ) or // Predecessor has multiple successors
18- exists ( Instruction predecessor , EdgeKind kind |
19- instr = predecessor .getSuccessor ( kind ) and
20- not kind instanceof GotoEdge
21- ) // Incoming edge is not a GotoEdge
22- )
23- }
24-
25- private newtype TIRBlock =
26- MkIRBlock ( Instruction firstInstr ) {
27- startsBasicBlock ( firstInstr )
28- }
29-
30- cached private predicate isEntryBlock ( IRBlock block ) {
31- block .getFirstInstruction ( ) instanceof EnterFunctionInstruction
32- }
33-
34- cached private predicate blockSuccessor ( IRBlock pred , IRBlock succ ) {
35- succ = pred .getASuccessor ( )
36- }
37-
38- private predicate blockImmediatelyDominates ( IRBlock dominator , IRBlock block ) =
39- idominance( isEntryBlock / 1 , blockSuccessor / 2 ) ( _, dominator , block )
40-
416class IRBlock extends TIRBlock {
42- Instruction firstInstr ;
43-
44- IRBlock ( ) {
45- this = MkIRBlock ( firstInstr )
46- }
47-
487 final string toString ( ) {
49- result = firstInstr .toString ( )
8+ result = getFirstInstruction ( this ) .toString ( )
509 }
5110
5211 final Location getLocation ( ) {
5312 result = getFirstInstruction ( ) .getLocation ( )
5413 }
5514
5615 final string getUniqueId ( ) {
57- result = firstInstr .getUniqueId ( )
16+ result = getFirstInstruction ( this ) .getUniqueId ( )
5817 }
5918
60- final cached Instruction getInstruction ( int index ) {
61- index = 0 and result = firstInstr or
62- (
63- index > 0 and
64- not startsBasicBlock ( result ) and
65- exists ( Instruction predecessor , GotoEdge edge |
66- predecessor = getInstruction ( index - 1 ) and
67- result = predecessor .getSuccessor ( edge )
68- )
69- )
19+ final Instruction getInstruction ( int index ) {
20+ result = getInstruction ( this , index )
7021 }
7122
7223 final PhiInstruction getAPhiInstruction ( ) {
73- Construction:: getPhiInstructionBlockStart ( result ) =
74- getFirstInstruction ( )
24+ Construction:: getPhiInstructionBlockStart ( result ) = getFirstInstruction ( )
7525 }
7626
7727 final Instruction getAnInstruction ( ) {
@@ -80,7 +30,7 @@ class IRBlock extends TIRBlock {
8030 }
8131
8232 final Instruction getFirstInstruction ( ) {
83- result = firstInstr
33+ result = getFirstInstruction ( this )
8434 }
8535
8636 final Instruction getLastInstruction ( ) {
@@ -92,23 +42,23 @@ class IRBlock extends TIRBlock {
9242 }
9343
9444 final FunctionIR getFunctionIR ( ) {
95- result = firstInstr .getFunctionIR ( )
45+ result = getFirstInstruction ( this ) .getFunctionIR ( )
9646 }
9747
9848 final Function getFunction ( ) {
99- result = firstInstr .getFunction ( )
49+ result = getFirstInstruction ( this ) .getFunction ( )
10050 }
10151
10252 final IRBlock getASuccessor ( ) {
103- result . getFirstInstruction ( ) = getLastInstruction ( ) . getASuccessor ( )
53+ blockSuccessor ( this , result )
10454 }
10555
10656 final IRBlock getAPredecessor ( ) {
107- firstInstr = result . getLastInstruction ( ) . getASuccessor ( )
57+ blockSuccessor ( result , this )
10858 }
10959
11060 final IRBlock getSuccessor ( EdgeKind kind ) {
111- result . getFirstInstruction ( ) = getLastInstruction ( ) . getSuccessor ( kind )
61+ blockSuccessor ( this , result , kind )
11262 }
11363
11464 final predicate immediatelyDominates ( IRBlock block ) {
0 commit comments