Skip to content

Commit 978275c

Browse files
author
Dave Bartolomeo
committed
C++/C#: Move irFunc out of various TInstruction branches
1 parent 07c1520 commit 978275c

File tree

6 files changed

+106
-115
lines changed

6 files changed

+106
-115
lines changed

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ cached
1818
private module Cached {
1919
cached
2020
predicate hasPhiInstructionCached(
21-
IRFunction irFunc, OldInstruction blockStartInstr, Alias::MemoryLocation defLocation
21+
OldInstruction blockStartInstr, Alias::MemoryLocation defLocation
2222
) {
2323
exists(OldBlock oldBlock |
2424
definitionHasPhiNode(defLocation, oldBlock) and
25-
irFunc = oldBlock.getEnclosingIRFunction() and
2625
blockStartInstr = oldBlock.getFirstInstruction()
2726
)
2827
}
2928

3029
cached
31-
predicate hasChiInstructionCached(IRFunctionBase irFunc, OldInstruction primaryInstruction) {
32-
hasChiNode(_, primaryInstruction) and
33-
irFunc = primaryInstruction.getEnclosingIRFunction()
30+
predicate hasChiInstructionCached(OldInstruction primaryInstruction) {
31+
hasChiNode(_, primaryInstruction)
3432
}
3533

3634
cached
@@ -44,11 +42,6 @@ private module Cached {
4442
class TStageInstruction =
4543
TRawInstruction or TPhiInstruction or TChiInstruction or TUnreachedInstruction;
4644

47-
private TRawInstruction rawInstruction(IRFunctionBase irFunc, Opcode opcode) {
48-
result = TRawInstruction(irFunc, opcode, _, _) and
49-
result instanceof OldInstruction
50-
}
51-
5245
cached
5346
predicate hasInstruction(TStageInstruction instr) {
5447
instr instanceof TRawInstruction and instr instanceof OldInstruction
@@ -268,12 +261,12 @@ private module Cached {
268261
result = getOldInstruction(instr).getAST()
269262
or
270263
exists(RawIR::Instruction blockStartInstr |
271-
instr = phiInstruction(_, blockStartInstr, _) and
264+
instr = phiInstruction(blockStartInstr, _) and
272265
result = blockStartInstr.getAST()
273266
)
274267
or
275268
exists(RawIR::Instruction primaryInstr |
276-
instr = chiInstruction(_, primaryInstr) and
269+
instr = chiInstruction(primaryInstr) and
277270
result = primaryInstr.getAST()
278271
)
279272
or
@@ -287,12 +280,12 @@ private module Cached {
287280
result = instr.(RawIR::Instruction).getResultLanguageType()
288281
or
289282
exists(Alias::MemoryLocation defLocation |
290-
instr = phiInstruction(_, _, defLocation) and
283+
instr = phiInstruction(_, defLocation) and
291284
result = defLocation.getType()
292285
)
293286
or
294287
exists(Instruction primaryInstr, Alias::VirtualVariable vvar |
295-
instr = chiInstruction(_, primaryInstr) and
288+
instr = chiInstruction(primaryInstr) and
296289
hasChiNode(vvar, primaryInstr) and
297290
result = vvar.getType()
298291
)
@@ -302,22 +295,27 @@ private module Cached {
302295

303296
cached
304297
Opcode getInstructionOpcode(Instruction instr) {
305-
instr = rawInstruction(_, result)
298+
result = getOldInstruction(instr).getOpcode()
306299
or
307-
instr = phiInstruction(_, _, _) and result instanceof Opcode::Phi
300+
instr = phiInstruction(_, _) and result instanceof Opcode::Phi
308301
or
309-
instr = chiInstruction(_, _) and result instanceof Opcode::Chi
302+
instr = chiInstruction(_) and result instanceof Opcode::Chi
310303
or
311304
instr = unreachedInstruction(_) and result instanceof Opcode::Unreached
312305
}
313306

314307
cached
315308
IRFunctionBase getInstructionEnclosingIRFunction(Instruction instr) {
316-
instr = rawInstruction(result, _)
309+
result = getOldInstruction(instr).getEnclosingIRFunction()
317310
or
318-
instr = phiInstruction(result, _, _)
311+
exists(OldInstruction blockStartInstr |
312+
instr = phiInstruction(blockStartInstr, _) and
313+
result = blockStartInstr.getEnclosingIRFunction()
314+
)
319315
or
320-
instr = chiInstruction(result, _)
316+
exists(OldInstruction primaryInstr |
317+
instr = chiInstruction(primaryInstr) and result = primaryInstr.getEnclosingIRFunction()
318+
)
321319
or
322320
instr = unreachedInstruction(result)
323321
}
@@ -341,11 +339,11 @@ private Instruction getNewInstruction(OldInstruction instr) { getOldInstruction(
341339
private OldInstruction getOldInstruction(Instruction instr) { instr = result }
342340

343341
private ChiInstruction getChi(OldInstruction primaryInstr) {
344-
result = chiInstruction(_, primaryInstr)
342+
result = chiInstruction(primaryInstr)
345343
}
346344

347345
private PhiInstruction getPhi(OldBlock defBlock, Alias::MemoryLocation defLocation) {
348-
result = phiInstruction(_, defBlock.getFirstInstruction(), defLocation)
346+
result = phiInstruction(defBlock.getFirstInstruction(), defLocation)
349347
}
350348

351349
/**
@@ -910,9 +908,9 @@ module SSAConsistency {
910908
module SSA {
911909
class MemoryLocation = Alias::MemoryLocation;
912910

913-
predicate hasPhiInstruction = Cached::hasPhiInstructionCached/3;
911+
predicate hasPhiInstruction = Cached::hasPhiInstructionCached/2;
914912

915-
predicate hasChiInstruction = Cached::hasChiInstructionCached/2;
913+
predicate hasChiInstruction = Cached::hasChiInstructionCached/1;
916914

917915
predicate hasUnreachedInstruction = Cached::hasUnreachedInstructionCached/1;
918916
}

cpp/ql/src/semmle/code/cpp/ir/implementation/internal/TInstruction.qll

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ private import Imports::Opcode
1515
cached
1616
newtype TInstruction =
1717
TRawInstruction(
18-
IRFunctionBase irFunc, Opcode opcode, IRConstruction::Raw::InstructionTag1 tag1,
18+
Opcode opcode, IRConstruction::Raw::InstructionTag1 tag1,
1919
IRConstruction::Raw::InstructionTag2 tag2
2020
) {
21-
IRConstruction::Raw::hasInstruction(irFunc.getFunction(), opcode, tag1, tag2)
21+
IRConstruction::Raw::hasInstruction(opcode, tag1, tag2)
2222
} or
2323
TUnaliasedSSAPhiInstruction(
24-
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
24+
TRawInstruction blockStartInstr,
2525
UnaliasedSSA::SSA::MemoryLocation memoryLocation
2626
) {
27-
UnaliasedSSA::SSA::hasPhiInstruction(irFunc, blockStartInstr, memoryLocation)
27+
UnaliasedSSA::SSA::hasPhiInstruction(blockStartInstr, memoryLocation)
2828
} or
29-
TUnaliasedSSAChiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) { none() } or
29+
TUnaliasedSSAChiInstruction(TRawInstruction primaryInstruction) { none() } or
3030
TUnaliasedSSAUnreachedInstruction(IRFunctionBase irFunc) {
3131
UnaliasedSSA::SSA::hasUnreachedInstruction(irFunc)
3232
} or
3333
TAliasedSSAPhiInstruction(
34-
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
34+
TRawInstruction blockStartInstr,
3535
AliasedSSA::SSA::MemoryLocation memoryLocation
3636
) {
37-
AliasedSSA::SSA::hasPhiInstruction(irFunc, blockStartInstr, memoryLocation)
37+
AliasedSSA::SSA::hasPhiInstruction(blockStartInstr, memoryLocation)
3838
} or
39-
TAliasedSSAChiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) {
40-
AliasedSSA::SSA::hasChiInstruction(irFunc, primaryInstruction)
39+
TAliasedSSAChiInstruction(TRawInstruction primaryInstruction) {
40+
AliasedSSA::SSA::hasChiInstruction(primaryInstruction)
4141
} or
4242
TAliasedSSAUnreachedInstruction(IRFunctionBase irFunc) {
4343
AliasedSSA::SSA::hasUnreachedInstruction(irFunc)
@@ -53,16 +53,16 @@ module UnaliasedSSAInstructions {
5353
class TPhiInstruction = TUnaliasedSSAPhiInstruction;
5454

5555
TPhiInstruction phiInstruction(
56-
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
56+
TRawInstruction blockStartInstr,
5757
UnaliasedSSA::SSA::MemoryLocation memoryLocation
5858
) {
59-
result = TUnaliasedSSAPhiInstruction(irFunc, blockStartInstr, memoryLocation)
59+
result = TUnaliasedSSAPhiInstruction(blockStartInstr, memoryLocation)
6060
}
6161

6262
class TChiInstruction = TUnaliasedSSAChiInstruction;
6363

64-
TChiInstruction chiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) {
65-
result = TUnaliasedSSAChiInstruction(irFunc, primaryInstruction)
64+
TChiInstruction chiInstruction(TRawInstruction primaryInstruction) {
65+
result = TUnaliasedSSAChiInstruction(primaryInstruction)
6666
}
6767

6868
class TUnreachedInstruction = TUnaliasedSSAUnreachedInstruction;
@@ -82,16 +82,16 @@ module AliasedSSAInstructions {
8282
class TPhiInstruction = TAliasedSSAPhiInstruction;
8383

8484
TPhiInstruction phiInstruction(
85-
IRFunctionBase irFunc, TRawInstruction blockStartInstr,
85+
TRawInstruction blockStartInstr,
8686
AliasedSSA::SSA::MemoryLocation memoryLocation
8787
) {
88-
result = TAliasedSSAPhiInstruction(irFunc, blockStartInstr, memoryLocation)
88+
result = TAliasedSSAPhiInstruction(blockStartInstr, memoryLocation)
8989
}
9090

9191
class TChiInstruction = TAliasedSSAChiInstruction;
9292

93-
TChiInstruction chiInstruction(IRFunctionBase irFunc, TRawInstruction primaryInstruction) {
94-
result = TAliasedSSAChiInstruction(irFunc, primaryInstruction)
93+
TChiInstruction chiInstruction(TRawInstruction primaryInstruction) {
94+
result = TAliasedSSAChiInstruction(primaryInstruction)
9595
}
9696

9797
class TUnreachedInstruction = TAliasedSSAUnreachedInstruction;

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ private import TranslatedStmt
1515
private import TranslatedFunction
1616

1717
TranslatedElement getInstructionTranslatedElement(Instruction instruction) {
18-
instruction = TRawInstruction(_, _, result, _)
18+
instruction = TRawInstruction(_, result, _)
1919
}
2020

2121
InstructionTag getInstructionTag(Instruction instruction) {
22-
instruction = TRawInstruction(_, _, _, result)
22+
instruction = TRawInstruction(_, _, result)
2323
}
2424

2525
pragma[noinline]
@@ -44,11 +44,8 @@ module Raw {
4444
predicate functionHasIR(Function func) { exists(getTranslatedFunction(func)) }
4545

4646
cached
47-
predicate hasInstruction(
48-
Function func, Opcode opcode, TranslatedElement element, InstructionTag tag
49-
) {
50-
element.hasInstruction(opcode, tag, _) and
51-
func = element.getFunction()
47+
predicate hasInstruction(Opcode opcode, TranslatedElement element, InstructionTag tag) {
48+
element.hasInstruction(opcode, tag, _)
5249
}
5350

5451
cached
@@ -382,12 +379,12 @@ private module Cached {
382379

383380
cached
384381
Opcode getInstructionOpcode(TStageInstruction instr) {
385-
instr = TRawInstruction(_, result, _, _)
382+
instr = TRawInstruction(result, _, _)
386383
}
387384

388385
cached
389386
IRFunctionBase getInstructionEnclosingIRFunction(TStageInstruction instr) {
390-
instr = TRawInstruction(result, _, _, _)
387+
result.getFunction() = getInstructionTranslatedElement(instr).getFunction()
391388
}
392389

393390
cached

cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/SSAConstruction.qll

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,17 @@ cached
1818
private module Cached {
1919
cached
2020
predicate hasPhiInstructionCached(
21-
IRFunction irFunc, OldInstruction blockStartInstr, Alias::MemoryLocation defLocation
21+
OldInstruction blockStartInstr, Alias::MemoryLocation defLocation
2222
) {
2323
exists(OldBlock oldBlock |
2424
definitionHasPhiNode(defLocation, oldBlock) and
25-
irFunc = oldBlock.getEnclosingIRFunction() and
2625
blockStartInstr = oldBlock.getFirstInstruction()
2726
)
2827
}
2928

3029
cached
31-
predicate hasChiInstructionCached(IRFunctionBase irFunc, OldInstruction primaryInstruction) {
32-
hasChiNode(_, primaryInstruction) and
33-
irFunc = primaryInstruction.getEnclosingIRFunction()
30+
predicate hasChiInstructionCached(OldInstruction primaryInstruction) {
31+
hasChiNode(_, primaryInstruction)
3432
}
3533

3634
cached
@@ -44,11 +42,6 @@ private module Cached {
4442
class TStageInstruction =
4543
TRawInstruction or TPhiInstruction or TChiInstruction or TUnreachedInstruction;
4644

47-
private TRawInstruction rawInstruction(IRFunctionBase irFunc, Opcode opcode) {
48-
result = TRawInstruction(irFunc, opcode, _, _) and
49-
result instanceof OldInstruction
50-
}
51-
5245
cached
5346
predicate hasInstruction(TStageInstruction instr) {
5447
instr instanceof TRawInstruction and instr instanceof OldInstruction
@@ -268,12 +261,12 @@ private module Cached {
268261
result = getOldInstruction(instr).getAST()
269262
or
270263
exists(RawIR::Instruction blockStartInstr |
271-
instr = phiInstruction(_, blockStartInstr, _) and
264+
instr = phiInstruction(blockStartInstr, _) and
272265
result = blockStartInstr.getAST()
273266
)
274267
or
275268
exists(RawIR::Instruction primaryInstr |
276-
instr = chiInstruction(_, primaryInstr) and
269+
instr = chiInstruction(primaryInstr) and
277270
result = primaryInstr.getAST()
278271
)
279272
or
@@ -287,12 +280,12 @@ private module Cached {
287280
result = instr.(RawIR::Instruction).getResultLanguageType()
288281
or
289282
exists(Alias::MemoryLocation defLocation |
290-
instr = phiInstruction(_, _, defLocation) and
283+
instr = phiInstruction(_, defLocation) and
291284
result = defLocation.getType()
292285
)
293286
or
294287
exists(Instruction primaryInstr, Alias::VirtualVariable vvar |
295-
instr = chiInstruction(_, primaryInstr) and
288+
instr = chiInstruction(primaryInstr) and
296289
hasChiNode(vvar, primaryInstr) and
297290
result = vvar.getType()
298291
)
@@ -302,22 +295,27 @@ private module Cached {
302295

303296
cached
304297
Opcode getInstructionOpcode(Instruction instr) {
305-
instr = rawInstruction(_, result)
298+
result = getOldInstruction(instr).getOpcode()
306299
or
307-
instr = phiInstruction(_, _, _) and result instanceof Opcode::Phi
300+
instr = phiInstruction(_, _) and result instanceof Opcode::Phi
308301
or
309-
instr = chiInstruction(_, _) and result instanceof Opcode::Chi
302+
instr = chiInstruction(_) and result instanceof Opcode::Chi
310303
or
311304
instr = unreachedInstruction(_) and result instanceof Opcode::Unreached
312305
}
313306

314307
cached
315308
IRFunctionBase getInstructionEnclosingIRFunction(Instruction instr) {
316-
instr = rawInstruction(result, _)
309+
result = getOldInstruction(instr).getEnclosingIRFunction()
317310
or
318-
instr = phiInstruction(result, _, _)
311+
exists(OldInstruction blockStartInstr |
312+
instr = phiInstruction(blockStartInstr, _) and
313+
result = blockStartInstr.getEnclosingIRFunction()
314+
)
319315
or
320-
instr = chiInstruction(result, _)
316+
exists(OldInstruction primaryInstr |
317+
instr = chiInstruction(primaryInstr) and result = primaryInstr.getEnclosingIRFunction()
318+
)
321319
or
322320
instr = unreachedInstruction(result)
323321
}
@@ -341,11 +339,11 @@ private Instruction getNewInstruction(OldInstruction instr) { getOldInstruction(
341339
private OldInstruction getOldInstruction(Instruction instr) { instr = result }
342340

343341
private ChiInstruction getChi(OldInstruction primaryInstr) {
344-
result = chiInstruction(_, primaryInstr)
342+
result = chiInstruction(primaryInstr)
345343
}
346344

347345
private PhiInstruction getPhi(OldBlock defBlock, Alias::MemoryLocation defLocation) {
348-
result = phiInstruction(_, defBlock.getFirstInstruction(), defLocation)
346+
result = phiInstruction(defBlock.getFirstInstruction(), defLocation)
349347
}
350348

351349
/**
@@ -910,9 +908,9 @@ module SSAConsistency {
910908
module SSA {
911909
class MemoryLocation = Alias::MemoryLocation;
912910

913-
predicate hasPhiInstruction = Cached::hasPhiInstructionCached/3;
911+
predicate hasPhiInstruction = Cached::hasPhiInstructionCached/2;
914912

915-
predicate hasChiInstruction = Cached::hasChiInstructionCached/2;
913+
predicate hasChiInstruction = Cached::hasChiInstructionCached/1;
916914

917915
predicate hasUnreachedInstruction = Cached::hasUnreachedInstructionCached/1;
918916
}

0 commit comments

Comments
 (0)