Line data Source code
1 : /* 2 : * SPDX-License-Identifier: MPL-2.0 3 : * 4 : * This Source Code Form is subject to the terms of the Mozilla Public 5 : * License, v. 2.0. If a copy of the MPL was not distributed with this 6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 : * 8 : * Copyright 2024 MonetDB Foundation; 9 : * Copyright August 2008 - 2023 MonetDB B.V.; 10 : * Copyright 1997 - July 2008 CWI. 11 : */ 12 : 13 : #include "monetdb_config.h" 14 : #include "mal_instruction.h" 15 : #include "opt_aliases.h" 16 : 17 : /* an alias is recognized by a simple assignment */ 18 : #define OPTisAlias(X) (X->argc == 2 && X->token == ASSIGNsymbol && X->barrier == 0 ) 19 : 20 : static inline void 21 23183945 : OPTaliasRemap(InstrPtr p, int *alias) 22 : { 23 79282323 : for (int i = 0; i < p->argc; i++) 24 56098378 : getArg(p, i) = alias[getArg(p, i)]; 25 23183945 : } 26 : 27 : str 28 1403004 : OPTaliasesImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, 29 : InstrPtr pci) 30 : { 31 1403004 : int i, j, k = 1, limit, actions = 0; 32 1403004 : int *alias = 0; 33 1403004 : str msg = MAL_SUCCEED; 34 1403004 : InstrPtr p; 35 : 36 1403004 : (void) stk; 37 1403004 : (void) cntxt; 38 : 39 1403004 : limit = mb->stop; 40 41866158 : for (i = 1; i < limit; i++) { 41 40963685 : p = getInstrPtr(mb, i); 42 40963685 : if (OPTisAlias(p)) 43 : break; 44 : } 45 1403004 : if (i == limit) { 46 : // we didn't found a simple assignment that warrants a rewrite 47 902472 : goto wrapup; 48 : } 49 500532 : k = i; 50 500532 : if (i < limit) { 51 500532 : alias = GDKzalloc(sizeof(int) * mb->vtop); 52 500532 : if (alias == NULL) 53 0 : throw(MAL, "optimizer.aliases", SQLSTATE(HY013) MAL_MALLOC_FAIL); 54 500532 : setVariableScope(mb); 55 45648719 : for (j = 1; j < mb->vtop; j++) 56 44647655 : alias[j] = j; 57 : } 58 26711919 : for (; i < limit; i++) { 59 26211397 : p = getInstrPtr(mb, i); 60 26211397 : mb->stmt[k++] = p; 61 26211397 : if (OPTisAlias(p) && getLastUpdate(mb, getArg(p, 0)) == i 62 3028926 : && getBeginScope(mb, getArg(p, 0)) == i 63 3027811 : && getLastUpdate(mb, getArg(p, 1)) <= i) { 64 3027433 : alias[getArg(p, 0)] = alias[getArg(p, 1)]; 65 3027433 : freeInstruction(p); 66 3027461 : actions++; 67 3027461 : k--; 68 3027461 : mb->stmt[k] = 0; 69 : } else { 70 23183964 : OPTaliasRemap(p, alias); 71 : } 72 : } 73 : 74 3527881 : for (i = k; i < limit; i++) 75 3027359 : mb->stmt[i] = NULL; 76 : 77 500522 : mb->stop = k; 78 500522 : GDKfree(alias); 79 : 80 : /* Defense line against incorrect plans */ 81 : /* Plan is unaffected */ 82 : // msg = chkTypes(cntxt->usermodule, mb, FALSE); 83 : // if ( msg == MAL_SUCCEED) 84 : // msg = chkFlow(mb); 85 : // if ( msg == MAL_SUCCEED) 86 : // msg = chkDeclarations(mb); 87 1403005 : wrapup: 88 : /* keep actions taken as a fake argument */ 89 1403005 : (void) pushInt(mb, pci, actions); 90 1403005 : return msg; 91 : }