@@ -208,7 +208,7 @@ private predicate usedAsCondition(Expr expr) {
208208 * AST as an lvalue-to-rvalue conversion, but the IR represents both a function
209209 * lvalue and a function pointer prvalue the same.
210210 */
211- predicate ignoreLoad ( Expr expr ) {
211+ private predicate ignoreLoad ( Expr expr ) {
212212 expr .hasLValueToRValueConversion ( ) and
213213 (
214214 expr instanceof ThisExpr or
@@ -220,6 +220,34 @@ predicate ignoreLoad(Expr expr) {
220220 )
221221}
222222
223+ /**
224+ * Holds if `expr` should have a load on it because it will be loaded as part
225+ * of the translation of its parent. We want to associate this load with `expr`
226+ * itself rather than its parent since in practical applications like data flow
227+ * we maintain that the value of the `x` in `x++` should be what's loaded from
228+ * `x`.
229+ */
230+ private predicate needsLoadForParentExpr ( Expr expr ) {
231+ exists ( CrementOperation crement | expr = crement .getOperand ( ) .getFullyConverted ( ) )
232+ or
233+ exists ( AssignOperation ao | expr = ao .getLValue ( ) .getFullyConverted ( ) )
234+ }
235+
236+ /**
237+ * Holds if `expr` should have a `TranslatedLoad` on it.
238+ */
239+ predicate hasTranslatedLoad ( Expr expr ) {
240+ (
241+ expr .hasLValueToRValueConversion ( )
242+ or
243+ needsLoadForParentExpr ( expr )
244+ ) and
245+ not ignoreExpr ( expr ) and
246+ not isNativeCondition ( expr ) and
247+ not isFlexibleCondition ( expr ) and
248+ not ignoreLoad ( expr )
249+ }
250+
223251newtype TTranslatedElement =
224252 // An expression that is not being consumed as a condition
225253 TTranslatedValueExpr ( Expr expr ) {
@@ -229,21 +257,12 @@ newtype TTranslatedElement =
229257 } or
230258 // A separate element to handle the lvalue-to-rvalue conversion step of an
231259 // expression.
232- TTranslatedLoad ( Expr expr ) {
233- not ignoreExpr ( expr ) and
234- not isNativeCondition ( expr ) and
235- not isFlexibleCondition ( expr ) and
236- expr .hasLValueToRValueConversion ( ) and
237- not ignoreLoad ( expr )
238- } or
260+ TTranslatedLoad ( Expr expr ) { hasTranslatedLoad ( expr ) } or
261+ // For expressions that would not otherwise generate an instruction.
239262 TTranslatedResultCopy ( Expr expr ) {
240263 not ignoreExpr ( expr ) and
241264 exprNeedsCopyIfNotLoaded ( expr ) and
242- // Doesn't have a TTranslatedLoad
243- not (
244- expr .hasLValueToRValueConversion ( ) and
245- not ignoreLoad ( expr )
246- )
265+ not hasTranslatedLoad ( expr )
247266 } or
248267 // An expression most naturally translated as control flow.
249268 TTranslatedNativeCondition ( Expr expr ) {
0 commit comments