| 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 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #include "monetdb_config.h" |
| 10 | #include "mal_instruction.h" |
| 11 | #include "opt_aliases.h" |
| 12 | |
| 13 | /* an alias is recognized by a simple assignment */ |
| 14 | #define OPTisAlias(X) (X->argc == 2 && X->token == ASSIGNsymbol && X->barrier == 0 ) |
| 15 | |
| 16 | void |
| 17 | OPTaliasRemap(InstrPtr p, int *alias){ |
| 18 | int i; |
| 19 | for(i=0; i<p->argc; i++) |
| 20 | getArg(p,i) = alias[getArg(p,i)]; |
| 21 | } |
| 22 | |
| 23 | str |
| 24 | OPTaliasesImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p) |
| 25 | { |
| 26 | int i,j,k=1, limit, actions=0; |
| 27 | int *alias = 0; |
| 28 | char buf[256]; |
| 29 | lng usec = GDKusec(); |
| 30 | |
| 31 | (void) stk; |
| 32 | (void) cntxt; |
| 33 | |
| 34 | limit = mb->stop; |
| 35 | for (i = 1; i < limit; i++){ |
| 36 | p= getInstrPtr(mb,i); |
| 37 | if (OPTisAlias(p)) |
| 38 | break; |
| 39 | } |
| 40 | k = i; |
| 41 | if( i < limit){ |
| 42 | alias= (int*) GDKzalloc(sizeof(int)* mb->vtop); |
| 43 | if (alias == NULL) |
| 44 | throw(MAL,"optimizer.aliases" , SQLSTATE(HY001) MAL_MALLOC_FAIL); |
| 45 | setVariableScope(mb); |
| 46 | for(j=1; j<mb->vtop; j++) alias[j]=j; |
| 47 | } |
| 48 | for (; i < limit; i++){ |
| 49 | p= getInstrPtr(mb,i); |
| 50 | mb->stmt[k++] = p; |
| 51 | if (OPTisAlias(p)){ |
| 52 | if( getLastUpdate(mb,getArg(p,0)) == i && |
| 53 | getBeginScope(mb,getArg(p,0)) == i && |
| 54 | getLastUpdate(mb,getArg(p,1)) <= i ){ |
| 55 | alias[getArg(p,0)]= alias[getArg(p,1)]; |
| 56 | freeInstruction(p); |
| 57 | actions++; |
| 58 | k--; |
| 59 | mb->stmt[k]= 0; |
| 60 | } else |
| 61 | OPTaliasRemap(p,alias); |
| 62 | } else |
| 63 | OPTaliasRemap(p,alias); |
| 64 | } |
| 65 | |
| 66 | for(i=k; i<limit; i++) |
| 67 | mb->stmt[i]= NULL; |
| 68 | |
| 69 | mb->stop= k; |
| 70 | if( alias) |
| 71 | GDKfree(alias); |
| 72 | |
| 73 | /* Defense line against incorrect plans */ |
| 74 | /* Plan is unaffected */ |
| 75 | //chkTypes(cntxt->usermodule, mb, FALSE); |
| 76 | //chkFlow(mb); |
| 77 | //chkDeclarations(mb); |
| 78 | // |
| 79 | /* keep all actions taken as a post block comment |
| 80 | * and update statics */ |
| 81 | usec= GDKusec() - usec; |
| 82 | snprintf(buf,256,"%-20s actions=%2d time=" LLFMT " usec" ,"aliases" ,actions,usec); |
| 83 | newComment(mb,buf); |
| 84 | if( actions >= 0) |
| 85 | addtoMalBlkHistory(mb); |
| 86 | |
| 87 | if( OPTdebug & OPTaliases){ |
| 88 | fprintf(stderr, "#ALIASES optimizer result\n" ); |
| 89 | fprintFunction(stderr, mb, 0, LIST_MAL_ALL); |
| 90 | } |
| 91 | return MAL_SUCCEED; |
| 92 | } |
| 93 | |