Skip to content

Commit a139474

Browse files
committed
C++, C# IR: Stabilize sort order for basic blocks.
1 parent 6ff939d commit a139474

File tree

18 files changed

+213
-127
lines changed

18 files changed

+213
-127
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ class IRBlockBase extends TIRBlock {
2626
/** Gets the source location of the first non-`Phi` instruction in this block. */
2727
final Language::Location getLocation() { result = getFirstInstruction().getLocation() }
2828

29-
/**
30-
* INTERNAL: Do not use.
31-
*
32-
* Gets a string that uniquely identifies this block within its enclosing function.
33-
*
34-
* This predicate is used by debugging and printing code only.
35-
*/
36-
final string getUniqueId() { result = getFirstInstruction(this).getUniqueId() }
37-
3829
/**
3930
* INTERNAL: Do not use.
4031
*
@@ -47,14 +38,15 @@ class IRBlockBase extends TIRBlock {
4738
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
4839
) and
4940
this =
50-
rank[result + 1](IRBlock funcBlock, int sortOverride |
41+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
5142
funcBlock.getEnclosingFunction() = getEnclosingFunction() and
43+
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
5244
// Ensure that the block containing `EnterFunction` always comes first.
5345
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
5446
then sortOverride = 0
5547
else sortOverride = 1
5648
|
57-
funcBlock order by sortOverride, funcBlock.getUniqueId()
49+
funcBlock order by sortOverride, sortKey1, sortKey2
5850
)
5951
}
6052

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ class Instruction extends Construction::TStageInstruction {
171171
*/
172172
final string getUniqueId() { result = Construction::getInstructionUniqueId(this) }
173173

174+
/**
175+
* INTERNAL: Do not use.
176+
*
177+
* Gets two sort keys for this instruction - used to order instructions for printing
178+
* in test outputs.
179+
*/
180+
final predicate hasSortKeys(int key1, int key2) {
181+
Construction::instructionHasSortKeys(this, key1, key2)
182+
}
183+
174184
/**
175185
* Gets the basic block that contains this instruction.
176186
*/

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ private module CachedForDebugging {
856856
exists(Alias::MemoryLocation location, OldBlock phiBlock, string specificity |
857857
instr = getPhi(phiBlock, location) and
858858
result =
859-
"Phi Block(" + phiBlock.getUniqueId() + ")[" + specificity + "]: " + location.getUniqueId() and
859+
"Phi Block(" + phiBlock.getFirstInstruction().getUniqueId() + ")[" + specificity + "]: " + location.getUniqueId() and
860860
if location instanceof Alias::VirtualVariable
861861
then
862862
// Sort Phi nodes for virtual variables before Phi nodes for member locations.
@@ -873,6 +873,24 @@ private module CachedForDebugging {
873873
result.getAST() = var.getAST() and
874874
result.getTag() = var.getTag()
875875
}
876+
877+
cached
878+
predicate instructionHasSortKeys(Instruction instr, int key1, int key2) {
879+
exists(OldInstruction oldInstr |
880+
oldInstr = getOldInstruction(instr) and
881+
oldInstr.hasSortKeys(key1, key2)
882+
)
883+
or
884+
instr instanceof TUnreachedInstruction and
885+
key1 = maxValue() and
886+
key2 = maxValue()
887+
}
888+
889+
/**
890+
* Returns the value of the maximum representable integer.
891+
*/
892+
cached
893+
int maxValue() { result = 2147483647 }
876894
}
877895

878896
module SSAConsistency {

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ class IRBlockBase extends TIRBlock {
2626
/** Gets the source location of the first non-`Phi` instruction in this block. */
2727
final Language::Location getLocation() { result = getFirstInstruction().getLocation() }
2828

29-
/**
30-
* INTERNAL: Do not use.
31-
*
32-
* Gets a string that uniquely identifies this block within its enclosing function.
33-
*
34-
* This predicate is used by debugging and printing code only.
35-
*/
36-
final string getUniqueId() { result = getFirstInstruction(this).getUniqueId() }
37-
3829
/**
3930
* INTERNAL: Do not use.
4031
*
@@ -47,14 +38,15 @@ class IRBlockBase extends TIRBlock {
4738
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
4839
) and
4940
this =
50-
rank[result + 1](IRBlock funcBlock, int sortOverride |
41+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
5142
funcBlock.getEnclosingFunction() = getEnclosingFunction() and
43+
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
5244
// Ensure that the block containing `EnterFunction` always comes first.
5345
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
5446
then sortOverride = 0
5547
else sortOverride = 1
5648
|
57-
funcBlock order by sortOverride, funcBlock.getUniqueId()
49+
funcBlock order by sortOverride, sortKey1, sortKey2
5850
)
5951
}
6052

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ class Instruction extends Construction::TStageInstruction {
171171
*/
172172
final string getUniqueId() { result = Construction::getInstructionUniqueId(this) }
173173

174+
/**
175+
* INTERNAL: Do not use.
176+
*
177+
* Gets two sort keys for this instruction - used to order instructions for printing
178+
* in test outputs.
179+
*/
180+
final predicate hasSortKeys(int key1, int key2) {
181+
Construction::instructionHasSortKeys(this, key1, key2)
182+
}
183+
174184
/**
175185
* Gets the basic block that contains this instruction.
176186
*/

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,21 @@ private module CachedForDebugging {
380380
string getTempVariableUniqueId(IRTempVariable var) {
381381
exists(TranslatedElement element |
382382
var = element.getTempVariable(_) and
383-
result = element.getId() + ":" + getTempVariableTagId(var.getTag())
383+
result = element.getId().toString() + ":" + getTempVariableTagId(var.getTag())
384384
)
385385
}
386386

387+
cached
388+
predicate instructionHasSortKeys(Instruction instruction, int key1, int key2) {
389+
key1 = getInstructionTranslatedElement(instruction).getId() and
390+
getInstructionTag(instruction) =
391+
rank[key2](InstructionTag tag, string tagId |
392+
tagId = getInstructionTagId(tag)
393+
|
394+
tag order by tagId
395+
)
396+
}
397+
387398
cached
388399
string getInstructionUniqueId(Instruction instruction) {
389400
result =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -752,10 +752,10 @@ abstract class TranslatedElement extends TTranslatedElement {
752752
abstract TranslatedElement getChild(int id);
753753

754754
/**
755-
* Gets the an identifier string for the element. This string is unique within
755+
* Gets the an identifier string for the element. This id is unique within
756756
* the scope of the element's function.
757757
*/
758-
final string getId() { result = getUniqueId().toString() }
758+
final int getId() { result = getUniqueId() }
759759

760760
private TranslatedElement getChildByRank(int rankIndex) {
761761
result =

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,6 @@ class IRBlockBase extends TIRBlock {
2626
/** Gets the source location of the first non-`Phi` instruction in this block. */
2727
final Language::Location getLocation() { result = getFirstInstruction().getLocation() }
2828

29-
/**
30-
* INTERNAL: Do not use.
31-
*
32-
* Gets a string that uniquely identifies this block within its enclosing function.
33-
*
34-
* This predicate is used by debugging and printing code only.
35-
*/
36-
final string getUniqueId() { result = getFirstInstruction(this).getUniqueId() }
37-
3829
/**
3930
* INTERNAL: Do not use.
4031
*
@@ -47,14 +38,15 @@ class IRBlockBase extends TIRBlock {
4738
config.shouldEvaluateDebugStringsForFunction(this.getEnclosingFunction())
4839
) and
4940
this =
50-
rank[result + 1](IRBlock funcBlock, int sortOverride |
41+
rank[result + 1](IRBlock funcBlock, int sortOverride, int sortKey1, int sortKey2 |
5142
funcBlock.getEnclosingFunction() = getEnclosingFunction() and
43+
funcBlock.getFirstInstruction().hasSortKeys(sortKey1, sortKey2) and
5244
// Ensure that the block containing `EnterFunction` always comes first.
5345
if funcBlock.getFirstInstruction() instanceof EnterFunctionInstruction
5446
then sortOverride = 0
5547
else sortOverride = 1
5648
|
57-
funcBlock order by sortOverride, funcBlock.getUniqueId()
49+
funcBlock order by sortOverride, sortKey1, sortKey2
5850
)
5951
}
6052

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ class Instruction extends Construction::TStageInstruction {
171171
*/
172172
final string getUniqueId() { result = Construction::getInstructionUniqueId(this) }
173173

174+
/**
175+
* INTERNAL: Do not use.
176+
*
177+
* Gets two sort keys for this instruction - used to order instructions for printing
178+
* in test outputs.
179+
*/
180+
final predicate hasSortKeys(int key1, int key2) {
181+
Construction::instructionHasSortKeys(this, key1, key2)
182+
}
183+
174184
/**
175185
* Gets the basic block that contains this instruction.
176186
*/

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ private module CachedForDebugging {
856856
exists(Alias::MemoryLocation location, OldBlock phiBlock, string specificity |
857857
instr = getPhi(phiBlock, location) and
858858
result =
859-
"Phi Block(" + phiBlock.getUniqueId() + ")[" + specificity + "]: " + location.getUniqueId() and
859+
"Phi Block(" + phiBlock.getFirstInstruction().getUniqueId() + ")[" + specificity + "]: " + location.getUniqueId() and
860860
if location instanceof Alias::VirtualVariable
861861
then
862862
// Sort Phi nodes for virtual variables before Phi nodes for member locations.
@@ -873,6 +873,24 @@ private module CachedForDebugging {
873873
result.getAST() = var.getAST() and
874874
result.getTag() = var.getTag()
875875
}
876+
877+
cached
878+
predicate instructionHasSortKeys(Instruction instr, int key1, int key2) {
879+
exists(OldInstruction oldInstr |
880+
oldInstr = getOldInstruction(instr) and
881+
oldInstr.hasSortKeys(key1, key2)
882+
)
883+
or
884+
instr instanceof TUnreachedInstruction and
885+
key1 = maxValue() and
886+
key2 = maxValue()
887+
}
888+
889+
/**
890+
* Returns the value of the maximum representable integer.
891+
*/
892+
cached
893+
int maxValue() { result = 2147483647 }
876894
}
877895

878896
module SSAConsistency {

0 commit comments

Comments
 (0)