Skip to content

Commit be5ac2f

Browse files
Merge pull request #648 from dave-bartolomeo/dave/UnreachableIR
C++: Remove unreachable IR
2 parents ce905e7 + 0140cd2 commit be5ac2f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1422
-543
lines changed

config/identical-files.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,27 @@
7272
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/gvn/ValueNumbering.qll",
7373
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/gvn/ValueNumbering.qll",
7474
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/gvn/ValueNumbering.qll"
75+
],
76+
"C++ IR ConstantAnalysis": [
77+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/constant/ConstantAnalysis.qll",
78+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/constant/ConstantAnalysis.qll",
79+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/constant/ConstantAnalysis.qll"
80+
],
81+
"C++ IR PrintConstantAnalysis": [
82+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/constant/PrintConstantAnalysis.qll",
83+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/constant/PrintConstantAnalysis.qll",
84+
"cpp/ql/src/semmle/code/cpp/ir/implementation/aliased_ssa/constant/PrintConstantAnalysis.qll"
85+
],
86+
"C++ IR ReachableBlock": [
87+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/reachability/ReachableBlock.qll",
88+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/reachability/ReachableBlock.qll"
89+
],
90+
"C++ IR PrintReachableBlock": [
91+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/reachability/PrintReachableBlock.qll",
92+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/reachability/PrintReachableBlock.qll"
93+
],
94+
"C++ IR Dominance": [
95+
"cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/reachability/Dominance.qll",
96+
"cpp/ql/src/semmle/code/cpp/ir/implementation/unaliased_ssa/internal/reachability/Dominance.qll"
7597
]
7698
}

cpp/ql/src/semmle/code/cpp/ir/implementation/Opcode.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ private newtype TOpcode =
6868
TBufferReadSideEffect() or
6969
TBufferWriteSideEffect() or
7070
TBufferMayWriteSideEffect() or
71-
TChi()
71+
TChi() or
72+
TUnreached()
7273

7374
class Opcode extends TOpcode {
7475
string toString() {
@@ -195,5 +196,6 @@ module Opcode {
195196
class BufferReadSideEffect extends ReadSideEffectOpcode, BufferAccessOpcode, TBufferReadSideEffect { override final string toString() { result = "BufferReadSideEffect" } }
196197
class BufferWriteSideEffect extends WriteSideEffectOpcode, BufferAccessOpcode, TBufferWriteSideEffect { override final string toString() { result = "BufferWriteSideEffect" } }
197198
class BufferMayWriteSideEffect extends MayWriteSideEffectOpcode, BufferAccessOpcode, TBufferMayWriteSideEffect { override final string toString() { result = "BufferMayWriteSideEffect" } }
198-
class Chi extends Opcode, TChi {override final string toString() { result = "Chi" } }
199+
class Chi extends Opcode, TChi { override final string toString() { result = "Chi" } }
200+
class Unreached extends Opcode, TUnreached { override final string toString() { result = "Unreached" } }
199201
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
private import internal.IRInternal
22
import Instruction
33
import semmle.code.cpp.ir.implementation.EdgeKind
4-
import Cached
4+
private import Cached
55

66
class IRBlock extends TIRBlock {
77
final string toString() {

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ module InstructionSanity {
102102
not exists(instr.getASuccessor()) and
103103
not instr instanceof ExitFunctionInstruction and
104104
// Phi instructions aren't linked into the instruction-level flow graph.
105-
not instr instanceof PhiInstruction
105+
not instr instanceof PhiInstruction and
106+
not instr instanceof UnreachedInstruction
106107
}
107108

108109
/**
@@ -451,8 +452,7 @@ class Instruction extends Construction::TInstruction {
451452
final predicate isResultModeled() {
452453
// Register results are always in SSA form.
453454
not hasMemoryResult() or
454-
// An unmodeled result will have a use on the `UnmodeledUse` instruction.
455-
not (getAUse() instanceof UnmodeledUseOperand)
455+
Construction::hasModeledMemoryResult(this)
456456
}
457457

458458
/**
@@ -1469,6 +1469,17 @@ class ChiInstruction extends Instruction {
14691469
}
14701470
}
14711471

1472+
/**
1473+
* An instruction representing unreachable code. Inserted in place of the original target
1474+
* instruction of a `ConditionalBranch` or `Switch` instruction where that particular edge is
1475+
* infeasible.
1476+
*/
1477+
class UnreachedInstruction extends Instruction {
1478+
UnreachedInstruction() {
1479+
opcode instanceof Opcode::Unreached
1480+
}
1481+
}
1482+
14721483
/**
14731484
* An instruction representing a built-in operation. This is used to represent
14741485
* operations such as access to variable argument lists.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
private import internal.ConstantAnalysisInternal
2+
import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import IR
4+
5+
language[monotonicAggregates]
6+
IntValue getConstantValue(Instruction instr) {
7+
result = instr.(IntegerConstantInstruction).getValue().toInt() or
8+
exists(BinaryInstruction binInstr, IntValue left, IntValue right |
9+
binInstr = instr and
10+
left = getConstantValue(binInstr.getLeftOperand()) and
11+
right = getConstantValue(binInstr.getRightOperand()) and
12+
(
13+
binInstr instanceof AddInstruction and result = add(left, right) or
14+
binInstr instanceof SubInstruction and result = sub(left, right) or
15+
binInstr instanceof MulInstruction and result = mul(left, right) or
16+
binInstr instanceof DivInstruction and result = div(left, right) or
17+
binInstr instanceof CompareEQInstruction and result = compareEQ(left, right) or
18+
binInstr instanceof CompareNEInstruction and result = compareNE(left, right) or
19+
binInstr instanceof CompareLTInstruction and result = compareLT(left, right) or
20+
binInstr instanceof CompareGTInstruction and result = compareGT(left, right) or
21+
binInstr instanceof CompareLEInstruction and result = compareLE(left, right) or
22+
binInstr instanceof CompareGEInstruction and result = compareGE(left, right)
23+
)
24+
) or
25+
exists(UnaryInstruction unaryInstr, IntValue src |
26+
unaryInstr = instr and
27+
src = getConstantValue(unaryInstr.getOperand()) and
28+
(
29+
unaryInstr instanceof NegateInstruction and result = neg(src)
30+
)
31+
) or
32+
result = getConstantValue(instr.(CopyInstruction).getSourceValue()) or
33+
exists(PhiInstruction phi |
34+
phi = instr and
35+
result = max(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction())) and
36+
result = min(PhiOperand operand | operand = phi.getAnOperand() | getConstantValue(operand.getDefinitionInstruction()))
37+
)
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
private import internal.ConstantAnalysisInternal
2+
private import semmle.code.cpp.ir.internal.IntegerConstant
3+
private import ConstantAnalysis
4+
import IR
5+
6+
private class ConstantAnalysisPropertyProvider extends IRPropertyProvider {
7+
override string getInstructionProperty(Instruction instr, string key) {
8+
key = "ConstantValue" and
9+
result = getValue(getConstantValue(instr)).toString()
10+
}
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import semmle.code.cpp.ir.implementation.aliased_ssa.IR as IR

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ MemoryAccess getOperandMemoryAccess(Operand operand) {
268268
if exists(IRVariable var, IntValue i |
269269
resultPointsTo(operand.getAddressOperand().getDefinitionInstruction(), var, i)
270270
)
271-
then exists(IRVariable var, IntValue i |
271+
then exists(IRVariable var, IntValue i, int size |
272272
resultPointsTo(operand.getAddressOperand().getDefinitionInstruction(), var, i) and
273-
result = getVariableMemoryAccess(var, i, operand.getDefinitionInstruction().getResultSize())
273+
result = getVariableMemoryAccess(var, i, size) and
274+
size = operand.getDefinitionInstruction().getResultSize()
274275
)
275276
else (
276277
result = TUnknownMemoryAccess(TUnknownVirtualVariable(operand.getInstruction().getFunctionIR())) and

0 commit comments

Comments
 (0)