@@ -12,87 +12,10 @@ private import semmle.code.cpp.ir.ValueNumbering
1212module SemanticExprConfig {
1313 class Location = Cpp:: Location ;
1414
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-
5615 /**
5716 * The expressions on which we perform range analysis.
5817 */
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 ;
9619
9720 SemBasicBlock getExprBasicBlock ( Expr e ) { result = getSemanticBasicBlock ( e .getBlock ( ) ) }
9821
@@ -139,12 +62,12 @@ module SemanticExprConfig {
13962
14063 predicate stringLiteral ( Expr expr , SemType type , string value ) {
14164 anyConstantExpr ( expr , type , value ) and
142- expr . getUnconverted ( ) instanceof IR:: StringConstantInstruction
65+ expr instanceof IR:: StringConstantInstruction
14366 }
14467
14568 predicate binaryExpr ( Expr expr , Opcode opcode , SemType type , Expr leftOperand , Expr rightOperand ) {
14669 exists ( IR:: BinaryInstruction instr |
147- instr = expr . getUnconverted ( ) and
70+ instr = expr and
14871 type = getSemanticType ( instr .getResultIRType ( ) ) and
14972 leftOperand = getSemanticExpr ( instr .getLeft ( ) ) and
15073 rightOperand = getSemanticExpr ( instr .getRight ( ) ) and
@@ -154,14 +77,14 @@ module SemanticExprConfig {
15477 }
15578
15679 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 |
15881 type = getSemanticType ( instr .getResultIRType ( ) ) and
15982 operand = getSemanticExpr ( instr .getUnary ( ) ) and
16083 // REVIEW: Merge the two operand types.
16184 opcode .toString ( ) = instr .getOpcode ( ) .toString ( )
16285 )
16386 or
164- exists ( IR:: StoreInstruction instr | instr = expr . getUnconverted ( ) |
87+ exists ( IR:: StoreInstruction instr | instr = expr |
16588 type = getSemanticType ( instr .getResultIRType ( ) ) and
16689 operand = getSemanticExpr ( instr .getSourceValue ( ) ) and
16790 opcode instanceof Opcode:: Store
@@ -170,13 +93,13 @@ module SemanticExprConfig {
17093
17194 predicate nullaryExpr ( Expr expr , Opcode opcode , SemType type ) {
17295 exists ( IR:: LoadInstruction load |
173- load = expr . getUnconverted ( ) and
96+ load = expr and
17497 type = getSemanticType ( load .getResultIRType ( ) ) and
17598 opcode instanceof Opcode:: Load
17699 )
177100 or
178101 exists ( IR:: InitializeParameterInstruction init |
179- init = expr . getUnconverted ( ) and
102+ init = expr and
180103 type = getSemanticType ( init .getResultIRType ( ) ) and
181104 opcode instanceof Opcode:: InitializeParameter
182105 )
@@ -199,8 +122,6 @@ module SemanticExprConfig {
199122 dominator .dominates ( dominated )
200123 }
201124
202- predicate hasDominanceInformation ( BasicBlock block ) { any ( ) }
203-
204125 private predicate id ( Cpp:: Locatable x , Cpp:: Locatable y ) { x = y }
205126
206127 private predicate idOf ( Cpp:: Locatable x , int y ) = equivalenceRelation( id / 2 ) ( x , y )
@@ -209,17 +130,7 @@ module SemanticExprConfig {
209130
210131 newtype TSsaVariable =
211132 TSsaInstruction ( IR:: Instruction instr ) { instr .hasMemoryResult ( ) } or
212- TSsaOperand ( IR:: Operand op ) { op .isDefinitionInexact ( ) } or
213- TSsaPointerArithmeticGuard ( ValueNumber instr ) {
214- exists ( Guard g , IR:: Operand use |
215- use = instr .getAUse ( ) and use .getIRType ( ) instanceof IR:: IRAddressType
216- |
217- g .comparesLt ( use , _, _, _, _) or
218- g .comparesLt ( _, use , _, _, _) or
219- g .comparesEq ( use , _, _, _, _) or
220- g .comparesEq ( _, use , _, _, _)
221- )
222- }
133+ TSsaOperand ( IR:: Operand op ) { op .isDefinitionInexact ( ) }
223134
224135 class SsaVariable extends TSsaVariable {
225136 string toString ( ) { none ( ) }
@@ -228,8 +139,6 @@ module SemanticExprConfig {
228139
229140 IR:: Instruction asInstruction ( ) { none ( ) }
230141
231- ValueNumber asPointerArithGuard ( ) { none ( ) }
232-
233142 IR:: Operand asOperand ( ) { none ( ) }
234143 }
235144
@@ -245,18 +154,6 @@ module SemanticExprConfig {
245154 final override IR:: Instruction asInstruction ( ) { result = instr }
246155 }
247156
248- class SsaPointerArithmeticGuard extends SsaVariable , TSsaPointerArithmeticGuard {
249- ValueNumber vn ;
250-
251- SsaPointerArithmeticGuard ( ) { this = TSsaPointerArithmeticGuard ( vn ) }
252-
253- final override string toString ( ) { result = vn .toString ( ) }
254-
255- final override Location getLocation ( ) { result = vn .getLocation ( ) }
256-
257- final override ValueNumber asPointerArithGuard ( ) { result = vn }
258- }
259-
260157 class SsaOperand extends SsaVariable , TSsaOperand {
261158 IR:: Operand op ;
262159
@@ -289,11 +186,7 @@ module SemanticExprConfig {
289186 )
290187 }
291188
292- Expr getAUse ( SsaVariable v ) {
293- result .getUnconverted ( ) .( IR:: LoadInstruction ) .getSourceValue ( ) = v .asInstruction ( )
294- or
295- result .getUnconverted ( ) = v .asPointerArithGuard ( ) .getAnInstruction ( )
296- }
189+ Expr getAUse ( SsaVariable v ) { result .( IR:: LoadInstruction ) .getSourceValue ( ) = v .asInstruction ( ) }
297190
298191 SemType getSsaVariableType ( SsaVariable v ) {
299192 result = getSemanticType ( v .asInstruction ( ) .getResultIRType ( ) )
@@ -332,10 +225,7 @@ module SemanticExprConfig {
332225 final override Location getLocation ( ) { result = block .getLocation ( ) }
333226
334227 final override predicate hasRead ( SsaVariable v ) {
335- exists ( IR:: Operand operand |
336- operand .getDef ( ) = v .asInstruction ( ) or
337- operand .getDef ( ) = v .asPointerArithGuard ( ) .getAnInstruction ( )
338- |
228+ exists ( IR:: Operand operand | operand .getDef ( ) = v .asInstruction ( ) |
339229 not operand instanceof IR:: PhiInputOperand and
340230 operand .getUse ( ) .getBlock ( ) = block
341231 )
@@ -353,10 +243,7 @@ module SemanticExprConfig {
353243 final override Location getLocation ( ) { result = succ .getLocation ( ) }
354244
355245 final override predicate hasRead ( SsaVariable v ) {
356- exists ( IR:: PhiInputOperand operand |
357- operand .getDef ( ) = v .asInstruction ( ) or
358- operand .getDef ( ) = v .asPointerArithGuard ( ) .getAnInstruction ( )
359- |
246+ exists ( IR:: PhiInputOperand operand | operand .getDef ( ) = v .asInstruction ( ) |
360247 operand .getPredecessorBlock ( ) = pred and
361248 operand .getUse ( ) .getBlock ( ) = succ
362249 )
@@ -433,7 +320,7 @@ module SemanticExprConfig {
433320 }
434321
435322 /** Gets the expression associated with `instr`. */
436- SemExpr getSemanticExpr ( IR:: Instruction instr ) { result = Equiv :: getEquivalenceClass ( instr ) }
323+ SemExpr getSemanticExpr ( IR:: Instruction instr ) { result = instr }
437324}
438325
439326predicate getSemanticExpr = SemanticExprConfig:: getSemanticExpr / 1 ;
0 commit comments