@@ -10,99 +10,56 @@ private import ssa0.SsaInternals as SsaInternals0
1010import SsaInternalsCommon
1111
1212private module SourceVariables {
13- int getMaxIndirectionForIRVariable ( IRVariable var ) {
14- exists ( Type type , boolean isGLValue |
15- var .getLanguageType ( ) .hasType ( type , isGLValue ) and
16- if isGLValue = true
17- then result = 1 + getMaxIndirectionsForType ( type )
18- else result = getMaxIndirectionsForType ( type )
19- )
20- }
21-
2213 cached
2314 private newtype TSourceVariable =
24- TSourceIRVariable ( BaseIRVariable baseVar , int ind ) {
25- ind = [ 0 .. getMaxIndirectionForIRVariable ( baseVar .getIRVariable ( ) ) ]
26- } or
27- TCallVariable ( AllocationInstruction call , int ind ) {
28- ind = [ 0 .. countIndirectionsForCppType ( getResultLanguageType ( call ) ) ]
15+ TMkSourceVariable ( SsaInternals0:: SourceVariable base , int ind ) {
16+ ind = [ 0 .. countIndirectionsForCppType ( base .getLanguageType ( ) ) + 1 ]
2917 }
3018
31- abstract class SourceVariable extends TSourceVariable {
19+ class SourceVariable extends TSourceVariable {
20+ SsaInternals0:: SourceVariable base ;
3221 int ind ;
3322
34- bindingset [ ind]
35- SourceVariable ( ) { any ( ) }
23+ SourceVariable ( ) { this = TMkSourceVariable ( base , ind ) }
24+
25+ /** Gets the IR variable associated with this `SourceVariable`, if any. */
26+ IRVariable getIRVariable ( ) { result = base .( BaseIRVariable ) .getIRVariable ( ) }
27+
28+ /**
29+ * Gets the base source variable (i.e., the variable without any
30+ * indirections) of this source variable.
31+ */
32+ SsaInternals0:: SourceVariable getBaseVariable ( ) { result = base }
3633
3734 /** Gets a textual representation of this element. */
38- abstract string toString ( ) ;
35+ string toString ( ) {
36+ ind = 0 and
37+ result = this .getBaseVariable ( ) .toString ( )
38+ or
39+ ind > 0 and
40+ result = this .getBaseVariable ( ) .toString ( ) + " indirection"
41+ }
3942
4043 /**
4144 * Gets the number of loads performed on the base source variable
4245 * to reach the value of this source variable.
4346 */
4447 int getIndirection ( ) { result = ind }
4548
46- /**
47- * Gets the base source variable (i.e., the variable without any
48- * indirections) of this source variable.
49- */
50- abstract BaseSourceVariable getBaseVariable ( ) ;
51-
5249 /** Holds if this variable is a glvalue. */
53- predicate isGLValue ( ) { none ( ) }
50+ predicate isGLValue ( ) { ind = 0 }
5451
5552 /**
5653 * Gets the type of this source variable. If `isGLValue()` holds, then
5754 * the type of this source variable should be thought of as "pointer
5855 * to `getType()`".
5956 */
60- abstract DataFlowType getType ( ) ;
61- }
62-
63- class SourceIRVariable extends SourceVariable , TSourceIRVariable {
64- BaseIRVariable var ;
65-
66- SourceIRVariable ( ) { this = TSourceIRVariable ( var , ind ) }
67-
68- IRVariable getIRVariable ( ) { result = var .getIRVariable ( ) }
69-
70- override BaseIRVariable getBaseVariable ( ) { result .getIRVariable ( ) = this .getIRVariable ( ) }
71-
72- override string toString ( ) {
73- ind = 0 and
74- result = this .getIRVariable ( ) .toString ( )
75- or
76- ind > 0 and
77- result = this .getIRVariable ( ) .toString ( ) + " indirection"
78- }
79-
80- override predicate isGLValue ( ) { ind = 0 }
81-
82- override DataFlowType getType ( ) {
83- if ind = 0 then result = var .getType ( ) else result = getTypeImpl ( var .getType ( ) , ind - 1 )
57+ DataFlowType getType ( ) {
58+ if this .isGLValue ( )
59+ then result = base .getType ( )
60+ else result = getTypeImpl ( base .getType ( ) , ind - 1 )
8461 }
8562 }
86-
87- class CallVariable extends SourceVariable , TCallVariable {
88- AllocationInstruction call ;
89-
90- CallVariable ( ) { this = TCallVariable ( call , ind ) }
91-
92- AllocationInstruction getCall ( ) { result = call }
93-
94- override BaseCallVariable getBaseVariable ( ) { result .getCallInstruction ( ) = call }
95-
96- override string toString ( ) {
97- ind = 0 and
98- result = "Call"
99- or
100- ind > 0 and
101- result = "Call indirection"
102- }
103-
104- override DataFlowType getType ( ) { result = getTypeImpl ( call .getResultType ( ) , ind ) }
105- }
10663}
10764
10865import SourceVariables
@@ -137,8 +94,9 @@ predicate hasRawIndirectInstruction(Instruction instr, int indirectionIndex) {
13794
13895cached
13996private newtype TDefOrUseImpl =
140- TDefImpl ( Operand address , int indirectionIndex ) {
141- exists ( Instruction base | isDef ( _, _, address , base , _, indirectionIndex ) |
97+ TDefImpl ( BaseSourceVariableInstruction base , Operand address , int indirectionIndex ) {
98+ isDef ( _, _, address , base , _, indirectionIndex ) and
99+ (
142100 // We only include the definition if the SSA pruning stage
143101 // concluded that the definition is live after the write.
144102 any ( SsaInternals0:: Def def ) .getAddressOperand ( ) = address
@@ -148,8 +106,8 @@ private newtype TDefOrUseImpl =
148106 base .( VariableAddressInstruction ) .getAstVariable ( ) instanceof GlobalLikeVariable
149107 )
150108 } or
151- TUseImpl ( Operand operand , int indirectionIndex ) {
152- isUse ( _, operand , _ , _, indirectionIndex ) and
109+ TUseImpl ( BaseSourceVariableInstruction base , Operand operand , int indirectionIndex ) {
110+ isUse ( _, operand , base , _, indirectionIndex ) and
153111 not isDef ( _, _, operand , _, _, _)
154112 } or
155113 TGlobalUse ( GlobalLikeVariable v , IRFunction f , int indirectionIndex ) {
@@ -236,7 +194,7 @@ abstract private class DefOrUseImpl extends TDefOrUseImpl {
236194
237195 /**
238196 * Gets the instruction that computes the base of this definition or use.
239- * This is always a `VariableAddressInstruction` or an `AllocationInstruction `.
197+ * This is always a `VariableAddressInstruction` or an `CallInstruction `.
240198 */
241199 abstract BaseSourceVariableInstruction getBase ( ) ;
242200
@@ -308,15 +266,17 @@ abstract class DefImpl extends DefOrUseImpl {
308266}
309267
310268private class DirectDef extends DefImpl , TDefImpl {
311- DirectDef ( ) { this = TDefImpl ( address , ind ) }
269+ BaseSourceVariableInstruction base ;
270+
271+ DirectDef ( ) { this = TDefImpl ( base , address , ind ) }
312272
313- override BaseSourceVariableInstruction getBase ( ) { isDef ( _ , _ , address , result , _ , _ ) }
273+ override BaseSourceVariableInstruction getBase ( ) { result = base }
314274
315- override int getIndirection ( ) { isDef ( _, _, address , _ , result , ind ) }
275+ override int getIndirection ( ) { isDef ( _, _, address , base , result , ind ) }
316276
317- override Node0Impl getValue ( ) { isDef ( _, result , address , _ , _, _) }
277+ override Node0Impl getValue ( ) { isDef ( _, result , address , base , _, _) }
318278
319- override predicate isCertain ( ) { isDef ( true , _, address , _ , _, ind ) }
279+ override predicate isCertain ( ) { isDef ( true , _, address , base , _, ind ) }
320280}
321281
322282private class IteratorDef extends DefImpl , TIteratorDef {
@@ -359,57 +319,52 @@ abstract class UseImpl extends DefOrUseImpl {
359319
360320abstract private class OperandBasedUse extends UseImpl {
361321 Operand operand ;
322+ BaseSourceVariableInstruction base ;
362323
363324 bindingset [ ind]
364325 OperandBasedUse ( ) { any ( ) }
365326
366327 final override predicate hasIndexInBlock ( IRBlock block , int index ) {
367328 // See the comment in `ssa0`'s `OperandBasedUse` for an explanation of this
368329 // predicate's implementation.
369- exists ( BaseSourceVariableInstruction base | base = this .getBase ( ) |
370- if base .getAst ( ) = any ( Cpp:: PostfixCrementOperation c ) .getOperand ( )
371- then
372- exists ( Operand op , int indirectionIndex , int indirection |
373- indirectionIndex = this .getIndirectionIndex ( ) and
374- indirection = this .getIndirection ( ) and
375- op =
376- min ( Operand cand , int i |
377- isUse ( _, cand , base , indirection , indirectionIndex ) and
378- block .getInstruction ( i ) = cand .getUse ( )
379- |
380- cand order by i
381- ) and
382- block .getInstruction ( index ) = op .getUse ( )
383- )
384- else operand .getUse ( ) = block .getInstruction ( index )
385- )
330+ if base .getAst ( ) = any ( Cpp:: PostfixCrementOperation c ) .getOperand ( )
331+ then
332+ exists ( Operand op , int indirectionIndex , int indirection |
333+ indirectionIndex = this .getIndirectionIndex ( ) and
334+ indirection = this .getIndirection ( ) and
335+ op =
336+ min ( Operand cand , int i |
337+ isUse ( _, cand , base , indirection , indirectionIndex ) and
338+ block .getInstruction ( i ) = cand .getUse ( )
339+ |
340+ cand order by i
341+ ) and
342+ block .getInstruction ( index ) = op .getUse ( )
343+ )
344+ else operand .getUse ( ) = block .getInstruction ( index )
386345 }
387346
347+ final override BaseSourceVariableInstruction getBase ( ) { result = base }
348+
388349 final Operand getOperand ( ) { result = operand }
389350
390351 final override Cpp:: Location getLocation ( ) { result = operand .getLocation ( ) }
391352}
392353
393354private class DirectUse extends OperandBasedUse , TUseImpl {
394- DirectUse ( ) { this = TUseImpl ( operand , ind ) }
395-
396- override int getIndirection ( ) { isUse ( _, operand , _, result , ind ) }
355+ DirectUse ( ) { this = TUseImpl ( base , operand , ind ) }
397356
398- override BaseSourceVariableInstruction getBase ( ) { isUse ( _, operand , result , _ , ind ) }
357+ override int getIndirection ( ) { isUse ( _, operand , base , result , ind ) }
399358
400- override predicate isCertain ( ) { isUse ( true , operand , _ , _, ind ) }
359+ override predicate isCertain ( ) { isUse ( true , operand , base , _, ind ) }
401360
402361 override Node getNode ( ) { nodeHasOperand ( result , operand , ind ) }
403362}
404363
405364private class IteratorUse extends OperandBasedUse , TIteratorUse {
406- BaseSourceVariableInstruction container ;
365+ IteratorUse ( ) { this = TIteratorUse ( operand , base , ind ) }
407366
408- IteratorUse ( ) { this = TIteratorUse ( operand , container , ind ) }
409-
410- override int getIndirection ( ) { isIteratorUse ( container , operand , result , ind ) }
411-
412- override BaseSourceVariableInstruction getBase ( ) { result = container }
367+ override int getIndirection ( ) { isIteratorUse ( base , operand , result , ind ) }
413368
414369 override predicate isCertain ( ) { none ( ) }
415370
0 commit comments