@@ -12,87 +12,10 @@ private import semmle.code.cpp.ir.ValueNumbering
12
12
module SemanticExprConfig {
13
13
class Location = Cpp:: Location ;
14
14
15
- /** A `ConvertInstruction` or a `CopyValueInstruction`. */
16
- private class Conversion extends IR:: UnaryInstruction {
17
- Conversion ( ) {
18
- this instanceof IR:: CopyValueInstruction
19
- or
20
- this instanceof IR:: ConvertInstruction
21
- }
22
-
23
- /** Holds if this instruction converts a value of type `tFrom` to a value of type `tTo`. */
24
- predicate converts ( SemType tFrom , SemType tTo ) {
25
- tFrom = getSemanticType ( this .getUnary ( ) .getResultIRType ( ) ) and
26
- tTo = getSemanticType ( this .getResultIRType ( ) )
27
- }
28
- }
29
-
30
- /**
31
- * Gets a conversion-like instruction that consumes `op`, and
32
- * which is guaranteed to not overflow.
33
- */
34
- private IR:: Instruction safeConversion ( IR:: Operand op ) {
35
- exists ( Conversion conv , SemType tFrom , SemType tTo |
36
- conv .converts ( tFrom , tTo ) and
37
- conversionCannotOverflow ( tFrom , tTo ) and
38
- conv .getUnaryOperand ( ) = op and
39
- result = conv
40
- )
41
- }
42
-
43
- /** Holds if `i1 = i2` or if `i2` is a safe conversion that consumes `i1`. */
44
- private predicate idOrSafeConversion ( IR:: Instruction i1 , IR:: Instruction i2 ) {
45
- not i1 .getResultIRType ( ) instanceof IR:: IRVoidType and
46
- (
47
- i1 = i2
48
- or
49
- i2 = safeConversion ( i1 .getAUse ( ) ) and
50
- i1 .getBlock ( ) = i2 .getBlock ( )
51
- )
52
- }
53
-
54
- module Equiv = QlBuiltins:: EquivalenceRelation< IR:: Instruction , idOrSafeConversion / 2 > ;
55
-
56
15
/**
57
16
* The expressions on which we perform range analysis.
58
17
*/
59
- class Expr extends Equiv:: EquivalenceClass {
60
- /** Gets the n'th instruction in this equivalence class. */
61
- private IR:: Instruction getInstruction ( int n ) {
62
- result =
63
- rank [ n + 1 ] ( IR:: Instruction instr , int i , IR:: IRBlock block |
64
- this = Equiv:: getEquivalenceClass ( instr ) and block .getInstruction ( i ) = instr
65
- |
66
- instr order by i
67
- )
68
- }
69
-
70
- /** Gets a textual representation of this element. */
71
- string toString ( ) { result = this .getUnconverted ( ) .toString ( ) }
72
-
73
- /** Gets the basic block of this expression. */
74
- IR:: IRBlock getBlock ( ) { result = this .getUnconverted ( ) .getBlock ( ) }
75
-
76
- /** Gets the unconverted instruction associated with this expression. */
77
- IR:: Instruction getUnconverted ( ) { result = this .getInstruction ( 0 ) }
78
-
79
- /**
80
- * Gets the final instruction associated with this expression. This
81
- * represents the result after applying all the safe conversions.
82
- */
83
- IR:: Instruction getConverted ( ) {
84
- exists ( int n |
85
- result = this .getInstruction ( n ) and
86
- not exists ( this .getInstruction ( n + 1 ) )
87
- )
88
- }
89
-
90
- /** Gets the type of the result produced by this instruction. */
91
- IR:: IRType getResultIRType ( ) { result = this .getConverted ( ) .getResultIRType ( ) }
92
-
93
- /** Gets the location of the source code for this expression. */
94
- Location getLocation ( ) { result = this .getUnconverted ( ) .getLocation ( ) }
95
- }
18
+ class Expr = IR:: Instruction ;
96
19
97
20
SemBasicBlock getExprBasicBlock ( Expr e ) { result = getSemanticBasicBlock ( e .getBlock ( ) ) }
98
21
@@ -139,12 +62,12 @@ module SemanticExprConfig {
139
62
140
63
predicate stringLiteral ( Expr expr , SemType type , string value ) {
141
64
anyConstantExpr ( expr , type , value ) and
142
- expr . getUnconverted ( ) instanceof IR:: StringConstantInstruction
65
+ expr instanceof IR:: StringConstantInstruction
143
66
}
144
67
145
68
predicate binaryExpr ( Expr expr , Opcode opcode , SemType type , Expr leftOperand , Expr rightOperand ) {
146
69
exists ( IR:: BinaryInstruction instr |
147
- instr = expr . getUnconverted ( ) and
70
+ instr = expr and
148
71
type = getSemanticType ( instr .getResultIRType ( ) ) and
149
72
leftOperand = getSemanticExpr ( instr .getLeft ( ) ) and
150
73
rightOperand = getSemanticExpr ( instr .getRight ( ) ) and
@@ -154,14 +77,14 @@ module SemanticExprConfig {
154
77
}
155
78
156
79
predicate unaryExpr ( Expr expr , Opcode opcode , SemType type , Expr operand ) {
157
- exists ( IR:: UnaryInstruction instr | instr = expr . getUnconverted ( ) |
80
+ exists ( IR:: UnaryInstruction instr | instr = expr |
158
81
type = getSemanticType ( instr .getResultIRType ( ) ) and
159
82
operand = getSemanticExpr ( instr .getUnary ( ) ) and
160
83
// REVIEW: Merge the two operand types.
161
84
opcode .toString ( ) = instr .getOpcode ( ) .toString ( )
162
85
)
163
86
or
164
- exists ( IR:: StoreInstruction instr | instr = expr . getUnconverted ( ) |
87
+ exists ( IR:: StoreInstruction instr | instr = expr |
165
88
type = getSemanticType ( instr .getResultIRType ( ) ) and
166
89
operand = getSemanticExpr ( instr .getSourceValue ( ) ) and
167
90
opcode instanceof Opcode:: Store
@@ -170,13 +93,13 @@ module SemanticExprConfig {
170
93
171
94
predicate nullaryExpr ( Expr expr , Opcode opcode , SemType type ) {
172
95
exists ( IR:: LoadInstruction load |
173
- load = expr . getUnconverted ( ) and
96
+ load = expr and
174
97
type = getSemanticType ( load .getResultIRType ( ) ) and
175
98
opcode instanceof Opcode:: Load
176
99
)
177
100
or
178
101
exists ( IR:: InitializeParameterInstruction init |
179
- init = expr . getUnconverted ( ) and
102
+ init = expr and
180
103
type = getSemanticType ( init .getResultIRType ( ) ) and
181
104
opcode instanceof Opcode:: InitializeParameter
182
105
)
@@ -290,9 +213,9 @@ module SemanticExprConfig {
290
213
}
291
214
292
215
Expr getAUse ( SsaVariable v ) {
293
- result .getUnconverted ( ) . ( IR:: LoadInstruction ) .getSourceValue ( ) = v .asInstruction ( )
216
+ result .( IR:: LoadInstruction ) .getSourceValue ( ) = v .asInstruction ( )
294
217
or
295
- result . getUnconverted ( ) = v .asPointerArithGuard ( ) .getAnInstruction ( )
218
+ result = v .asPointerArithGuard ( ) .getAnInstruction ( )
296
219
}
297
220
298
221
SemType getSsaVariableType ( SsaVariable v ) {
@@ -433,7 +356,7 @@ module SemanticExprConfig {
433
356
}
434
357
435
358
/** Gets the expression associated with `instr`. */
436
- SemExpr getSemanticExpr ( IR:: Instruction instr ) { result = Equiv :: getEquivalenceClass ( instr ) }
359
+ SemExpr getSemanticExpr ( IR:: Instruction instr ) { result = instr }
437
360
}
438
361
439
362
predicate getSemanticExpr = SemanticExprConfig:: getSemanticExpr / 1 ;
0 commit comments