/ Check-in [b7e39851]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Be more aggressive in optimizing constant conditional expressions.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b7e39851a75b87ebca747b26a39989560fd6362b
User & Date: drh 2014-01-02 17:57:16
Context
2014-01-02
19:35
Avoid unnecessary affinity transformations when building indices using data from a table. check-in: 10d85135 user: drh tags: trunk
17:57
Be more aggressive in optimizing constant conditional expressions. check-in: b7e39851 user: drh tags: trunk
2014-01-01
15:18
Try to detect process ID changes due to fork() calls in os_unix.c and reset the PRNG when a process ID change is detected. check-in: e1eba1fb user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/expr.c.

   519    519     if( p ) {
   520    520       sqlite3ExprCheckHeight(pParse, p->nHeight);
   521    521     }
   522    522     return p;
   523    523   }
   524    524   
   525    525   /*
   526         -** Return 1 if an expression must be FALSE in all cases and 0 if the
   527         -** expression might be true.  This is an optimization.  If is OK to
   528         -** return 0 here even if the expression really is always false (a 
   529         -** false negative).  But it is a bug to return 1 if the expression
   530         -** might be true in some rare circumstances (a false positive.)
          526  +** If the expression is always either TRUE or FALSE (respectively),
          527  +** then return 1.  If one cannot determine the truth value of the
          528  +** expression at compile-time return 0.
          529  +**
          530  +** This is an optimization.  If is OK to return 0 here even if
          531  +** the expression really is always false or false (a false negative).
          532  +** But it is a bug to return 1 if the expression might have different
          533  +** boolean values in different circumstances (a false positive.)
   531    534   **
   532    535   ** Note that if the expression is part of conditional for a
   533    536   ** LEFT JOIN, then we cannot determine at compile-time whether or not
   534    537   ** is it true or false, so always return 0.
   535    538   */
          539  +static int exprAlwaysTrue(Expr *p){
          540  +  int v = 0;
          541  +  if( ExprHasProperty(p, EP_FromJoin) ) return 0;
          542  +  if( !sqlite3ExprIsInteger(p, &v) ) return 0;
          543  +  return v!=0;
          544  +}
   536    545   static int exprAlwaysFalse(Expr *p){
   537    546     int v = 0;
   538    547     if( ExprHasProperty(p, EP_FromJoin) ) return 0;
   539    548     if( !sqlite3ExprIsInteger(p, &v) ) return 0;
   540    549     return v==0;
   541    550   }
   542    551   
................................................................................
  3610   3619         sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
  3611   3620         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
  3612   3621         sqlite3VdbeResolveLabel(v, destIfFalse);
  3613   3622         break;
  3614   3623       }
  3615   3624   #endif
  3616   3625       default: {
  3617         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  3618         -      sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
  3619         -      testcase( regFree1==0 );
  3620         -      testcase( jumpIfNull==0 );
         3626  +      if( exprAlwaysTrue(pExpr) ){
         3627  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
         3628  +      }else if( exprAlwaysFalse(pExpr) ){
         3629  +        /* No-op */
         3630  +      }else{
         3631  +        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
         3632  +        sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
         3633  +        testcase( regFree1==0 );
         3634  +        testcase( jumpIfNull==0 );
         3635  +      }
  3621   3636         break;
  3622   3637       }
  3623   3638     }
  3624   3639     sqlite3ReleaseTempReg(pParse, regFree1);
  3625   3640     sqlite3ReleaseTempReg(pParse, regFree2);  
  3626   3641   }
  3627   3642   
................................................................................
  3755   3770           sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
  3756   3771           sqlite3VdbeResolveLabel(v, destIfNull);
  3757   3772         }
  3758   3773         break;
  3759   3774       }
  3760   3775   #endif
  3761   3776       default: {
  3762         -      r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
  3763         -      sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
  3764         -      testcase( regFree1==0 );
  3765         -      testcase( jumpIfNull==0 );
         3777  +      if( exprAlwaysFalse(pExpr) ){
         3778  +        sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
         3779  +      }else if( exprAlwaysTrue(pExpr) ){
         3780  +        /* no-op */
         3781  +      }else{
         3782  +        r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
         3783  +        sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
         3784  +        testcase( regFree1==0 );
         3785  +        testcase( jumpIfNull==0 );
         3786  +      }
  3766   3787         break;
  3767   3788       }
  3768   3789     }
  3769   3790     sqlite3ReleaseTempReg(pParse, regFree1);
  3770   3791     sqlite3ReleaseTempReg(pParse, regFree2);
  3771   3792   }
  3772   3793