LCOV - code coverage report
Current view: top level - monetdb5/optimizer - opt_constants.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 49 52 94.2 %
Date: 2021-10-13 02:24:04 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * Constant Duplicate Removal
      11             :  * The compilers may generate an abundance of constants on
      12             :  * the stack. This simple optimizer merges them into a single reference.
      13             :  * This makes it easier to search for statement duplicates
      14             :  * and alias their variables.
      15             :  */
      16             : /* We should not look at constants in simple, side-effect functions, because
      17             :  * they can not be removed later on.
      18             : */
      19             : /*
      20             :  * We have to keep an alias table to reorganize the program
      21             :  * after the variable stack has changed.
      22             :  * The plan may contain many constants and to check them all would be quadratic
      23             :  * in the size of the constant list.
      24             :  * The heuristic is to look back into the list only partially.
      25             :  * A hash structure could help out with further reduction.
      26             :  */
      27             : #include "monetdb_config.h"
      28             : #include "mal_instruction.h"
      29             : #include "opt_constants.h"
      30             : 
      31             : str
      32      353095 : OPTconstantsImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci)
      33             : {
      34             :         int i, j, k = 1, n  = 0, fnd = 0, actions  = 0, limit = 0;
      35             :         int *alias = NULL, *index = NULL, *cand = NULL;
      36             :         VarPtr x,y, *cst = NULL;
      37             :         str msg = MAL_SUCCEED;
      38             :         InstrPtr p, q;
      39             : 
      40      353095 :         if( isSimpleSQL(mb)){
      41      203899 :                 goto wrapup;
      42             :         }
      43      149196 :         alias= (int*) GDKzalloc(sizeof(int) * mb->vtop);
      44      149196 :         cand= (int*) GDKzalloc(sizeof(int) * mb->vtop);
      45      149196 :         cst= (VarPtr*) GDKzalloc(sizeof(VarPtr) * mb->vtop);
      46      149196 :         index= (int*) GDKzalloc(sizeof(int) * mb->vtop);
      47             : 
      48      149196 :         if ( alias == NULL || cst == NULL || index == NULL || cand == NULL){
      49           0 :                 msg = createException(MAL,"optimizer.constants", SQLSTATE(HY013) MAL_MALLOC_FAIL);
      50           0 :                 goto wrapup;
      51             :         }
      52             : 
      53             :         (void) stk;
      54             :         (void) cntxt;
      55             : 
      56    16227480 :         for(i=0; i<mb->stop; i++){
      57    16078285 :                 q = getInstrPtr(mb,i);
      58    16078285 :                 if ( !q) {
      59           0 :                         continue;
      60             :                 }
      61    16078285 :                 if ( getModuleId(q) == sqlRef && getFunctionId(q) != tidRef) {
      62     3146652 :                         continue;
      63             :                 }
      64    12931633 :                 if( hasSideEffects(mb, q, 1) )
      65     5305192 :                         continue;
      66    27931957 :                 for(k= q->retc; k < q->argc; k++){
      67    20305517 :                         j = getArg(q,k);
      68    20305517 :                         if( cand[j] == 0) {
      69    30532391 :                                 cand[j] = isVarConstant(mb, j)  && isVarFixed(mb, j)  && getVarType(mb, j) != TYPE_ptr;
      70             :                         }
      71             :                 }
      72             :         }
      73             : 
      74    32269383 :         for (i=0; i< mb->vtop; i++)
      75    32120188 :                 alias[ i]= i;
      76    32269463 :         for (i=0; i< mb->vtop; i++)
      77    32120267 :                 if ( cand[i]) {
      78     2528941 :                         x= getVar(mb,i);
      79             :                         fnd = 0;
      80     2528941 :                         limit = n - 128; // don't look to far back
      81     2528941 :                         if ( x->type && x->value.vtype)
      82    59625089 :                         for( k = n-1; k >= 0 && k > limit; k--){
      83    57993798 :                                 y= cst[k];
      84    57993798 :                                 if ( x->type == y->type &&
      85    36498434 :                                          x->rowcnt == y->rowcnt &&
      86    72996869 :                                          x->value.vtype == y->value.vtype &&
      87    36498434 :                                         ATOMcmp(x->value.vtype, VALptr(&x->value), VALptr(&y->value)) == 0){
      88             : 
      89             :                                         /* re-use a constant */
      90      897588 :                                         alias[i]= index[k];
      91             :                                         fnd=1;
      92      897588 :                                         actions++;
      93             :                                         break;
      94             :                                 }
      95             :                         }
      96             :                         if ( fnd == 0){
      97     1631354 :                                 cst[n]= x;
      98     1631354 :                                 index[n]= i;
      99     1631354 :                                 n++;
     100             :                         }
     101             :                 }
     102             : 
     103      149196 :         if (actions)
     104    12068842 :                 for (i = 0; i < mb->stop; i++){
     105    12019092 :                         p= getInstrPtr(mb,i);
     106    60721198 :                         for (k=0; k < p->argc; k++)
     107    48702106 :                                 getArg(p,k) = alias[getArg(p,k)];
     108             :                 }
     109             : 
     110             :         /* Defense line against incorrect plans */
     111             :         /* Plan remains unaffected */
     112             :         // msg = chkTypes(cntxt->usermodule, mb, FALSE);
     113             :         // if (!msg)
     114             :         //      msg = chkFlow(mb);
     115             :         // if(!msg)
     116             :         //      msg = chkDeclarations(mb);
     117             :         /* keep all actions taken as a post block comment */
     118      149196 : wrapup:
     119             :         /* keep actions taken as a fake argument*/
     120      353095 :         (void) pushInt(mb, pci, actions);
     121             : 
     122      353095 :         if( cand) GDKfree(cand);
     123      353095 :         if( alias) GDKfree(alias);
     124      353095 :         if( cst) GDKfree(cst);
     125      353095 :         if( index) GDKfree(index);
     126      353095 :         return msg;
     127             : }

Generated by: LCOV version 1.14