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 : str 21 1432330 : OPTaliasesImplementation(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) 22 : { 23 1432330 : int i, j, k = 1, limit, actions = 0; 24 1432330 : int *alias = 0; 25 1432330 : str msg = MAL_SUCCEED; 26 1432330 : InstrPtr p; 27 : 28 1432330 : (void) stk; 29 1432330 : (void) cntxt; 30 : 31 1432330 : limit = mb->stop; 32 47151471 : for (i = 1; i < limit; i++) { 33 46230328 : p = getInstrPtr(mb, i); 34 46230328 : if (OPTisAlias(p)) 35 : break; 36 : } 37 1432330 : if (i == limit) { 38 : // we didn't found a simple assignment that warrants a rewrite 39 921127 : goto wrapup; 40 : } 41 511203 : k = i; 42 511203 : if (i < limit) { 43 511203 : alias = GDKzalloc(sizeof(int) * mb->vtop); 44 511217 : if (alias == NULL) 45 0 : throw(MAL, "optimizer.aliases", SQLSTATE(HY013) MAL_MALLOC_FAIL); 46 511217 : setVariableScope(mb); 47 46953625 : for (j = 1; j < mb->vtop; j++) 48 45931318 : alias[j] = j; 49 : } 50 27019775 : for (; i < limit; i++) { 51 26508508 : p = getInstrPtr(mb, i); 52 26508508 : mb->stmt[k++] = p; 53 26508508 : if (OPTisAlias(p) && getLastUpdate(mb, getArg(p, 0)) == i 54 2942704 : && getBeginScope(mb, getArg(p, 0)) == i 55 2941658 : && getLastUpdate(mb, getArg(p, 1)) <= i) { 56 2941294 : alias[getArg(p, 0)] = alias[getArg(p, 1)]; 57 2941294 : freeInstruction(p); 58 2941471 : actions++; 59 2941471 : k--; 60 2941471 : mb->stmt[k] = 0; 61 : } else { 62 80637441 : for (int i = 0; i < p->argc; i++) 63 57070227 : getArg(p, i) = alias[getArg(p, i)]; 64 : } 65 : } 66 : 67 3452267 : for (i = k; i < limit; i++) 68 2941000 : mb->stmt[i] = NULL; 69 : 70 511267 : mb->stop = k; 71 511267 : GDKfree(alias); 72 : 73 : /* Defense line against incorrect plans */ 74 : /* Plan is unaffected */ 75 : // msg = chkTypes(cntxt->usermodule, mb, FALSE); 76 : // if ( msg == MAL_SUCCEED) 77 : // msg = chkFlow(mb); 78 : // if ( msg == MAL_SUCCEED) 79 : // msg = chkDeclarations(mb); 80 1432344 : wrapup: 81 : /* keep actions taken as a fake argument */ 82 1432344 : (void) pushInt(mb, pci, actions); 83 1432344 : return msg; 84 : }