# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0.  If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright 1997 - July 2008 CWI, August 2008 - 2016 MonetDB B.V.

module optimizer;

#opt_aliases.mal

pattern optimizer.aliases():str
address OPTwrapper;
pattern optimizer.aliases(mod:str, fcn:str):str
address OPTwrapper
comment "Alias removal optimizer";

#opt_coercions.mal

pattern optimizer.coercions():str
address OPTwrapper;
pattern optimizer.coercions(mod:str, fcn:str):str
address OPTwrapper
comment "Handle simple type coercions";

#opt_commonTerms.mal

pattern optimizer.commonTerms():str
address OPTwrapper;
pattern optimizer.commonTerms(mod:any_1, fcn:any_2):str
address OPTwrapper
comment "Common sub-expression optimizer"

#opt_candidates
pattern optimizer.candidates():str
address OPTwrapper;
pattern optimizer.candidates(mod:str, fcn:str):str 
address OPTwrapper
comment "Mark candidate list variables";

#opt_volcano
pattern optimizer.volcano():str
address OPTwrapper;
pattern optimizer.volcano(mod:str, fcn:str):str 
address OPTwrapper
comment "Simulate volcano style execution";

#opt_constants
pattern optimizer.constants():str
address OPTwrapper;
pattern optimizer.constants(mod:str, fcn:str):str 
address OPTwrapper
comment "Duplicate constant removal optimizer";

#opt_profiler
pattern optimizer.profiler():str
address OPTwrapper;
pattern optimizer.profiler(mod:str, fcn:str):str 
address OPTwrapper
comment "Collect properties for the profiler";

#opt_costModel.mal

pattern optimizer.costModel():str
address OPTwrapper;
pattern optimizer.costModel(mod:str, fcn:str):str
address OPTwrapper
comment "Estimate the cost of a relational expression";

#opt_crackers.mal

pattern optimizer.crackers():str
address OPTwrapper;
pattern optimizer.crackers(mod:str, fcn:str):str
address OPTwrapper
comment "Replace algebra select with crackers select";

#opt_dataflow.mal

pattern optimizer.dataflow():str
address OPTwrapper;
pattern optimizer.dataflow(mod:str, fcn:str):str
address OPTwrapper
comment "Dataflow bracket code injection";

#opt_deadcode.mal

pattern optimizer.deadcode():str
address OPTwrapper;
pattern optimizer.deadcode(mod:str, fcn:str):str
address OPTwrapper
comment "Dead code optimizer";

#opt_derivepath.mal

pattern optimizer.derivePath():str
address OPTwrapper;
pattern optimizer.derivePath(mod:str, fcn:str):str
address OPTwrapper
comment "Join path constructor";

#opt_emptybind.mal

pattern optimizer.emptybind():str
address OPTwrapper;
pattern optimizer.emptybind(mod:str, fcn:str):str
address OPTwrapper
comment "Evaluate empty set expressions.";

#opt_jit.mal

pattern optimizer.jit():str
address OPTwrapper;
pattern optimizer.jit(mod:str, fcn:str):str
address OPTwrapper
comment "Propagate candidate lists in just-in-time optimization";

#opt_evaluate.mal

pattern optimizer.evaluate():str
address OPTwrapper;
pattern optimizer.evaluate(mod:str, fcn:str):str
address OPTwrapper
comment "Evaluate constant expressions once.";

#opt_factorize.mal

pattern optimizer.factorize():str
address OPTwrapper;
pattern optimizer.factorize(mod:str, fcn:str):str
address OPTwrapper
comment "Turn function into a factory";

#opt_garbagecollector.mal

pattern optimizer.garbageCollector():str
address OPTwrapper;
pattern optimizer.garbageCollector(mod:str, fcn:str):str
address OPTwrapper
comment "Garbage collector optimizer";

#opt_generator.mal
pattern optimizer.generator():str
address OPTwrapper;
pattern optimizer.generator(mod:str, fcn:str):str
address OPTwrapper
comment "Sequence generator optimizer";

#opt_querylog
pattern optimizer.querylog():str
address OPTwrapper;
pattern optimizer.querylog(mod:str, fcn:str):str
address OPTwrapper
comment "Collect SQL query statistics";

module optimizer;
pattern prelude()
address optimizer_prelude
comment "Initialize the optimizer";

pattern optimize(mod:str, fcn:str)
address QOToptimize
comment "Optimize a specific operation";

pattern showFlowGraph(M:str,F:str,s:str):void 
address QOTshowFlowGraph
comment "Dump the data flow of the function 
	M.F in a format recognizable by the 
	command 'dot' on the file s";

pattern showPlan(M:str,F:str,s:str):void 
address QOTshowPlan;
pattern showPlan()
address QOTshowPlan
comment "Illustrate the plan derived so far";

#opt_inline.mal

pattern optimizer.inline():str
address OPTwrapper;
pattern optimizer.inline(mod:str, fcn:str):str
address OPTwrapper
comment "Expand inline functions";

#opt_projectionpath.mal
pattern optimizer.projectionpath():str
address OPTwrapper;
pattern optimizer.projectionpath(mod:str, fcn:str):str
address OPTwrapper
comment "Join path constructor";

pattern algebra.projectionpath(l:bat[:any]...):bat[:any]
address ALGprojectionpath
comment "Routine to handle join paths.  The type analysis is rather tricky.";

#opt_joinselect
pattern optimizer.joinselect():str
address OPTwrapper;
pattern optimizer.joinselect(mod:str, fcn:str):str
address OPTwrapper
comment "Replace select with join select";

#opt_macro.mal

pattern optimizer.macro(targetmod:str,targetfcn:str):void
address OPTmacro
comment "Inline the code of the target function.";
pattern optimizer.macro(mod:str,fcn:str,targetmod:str,targetfcn:str):void
address OPTmacro
comment "Inline a target function used in a specific function.";

pattern optimizer.orcam(targetmod:str,targetfcn:str):void
address OPTorcam
comment "Inverse macro processor for current function";
pattern optimizer.orcam(mod:str,fcn:str,targetmod:str,targetfcn:str):void
address OPTorcam
comment "Inverse macro, find pattern and replace with a function call.";

#opt_mergetable.mal

pattern optimizer.mergetable():str
address OPTwrapper;
pattern optimizer.mergetable(mod:str, fcn:str):str
address OPTwrapper
comment "Resolve the multi-table definitions";

#opt_mitosis.mal

pattern optimizer.mitosis():str
address OPTwrapper;
pattern optimizer.mitosis(mod:str, fcn:str):str
address OPTwrapper
comment "Modify the plan to exploit parallel processing on multiple cores";

#opt_multiplex.mal
pattern optimizer.multiplex():void
address OPTwrapper
comment "Compiler for multiplexed instructions.";

pattern optimizer.multiplex(mod:str,fcn:str):void
address OPTwrapper
comment "Compiler for multiplexed instructions.";

#opt_matpack
pattern optimizer.matpack():str
address OPTwrapper;
pattern optimizer.matpack(mod:str, fcn:str):str
address OPTwrapper
comment "Unroll the mat.pack operation";

#opt_json
pattern optimizer.json():str
address OPTwrapper;
pattern optimizer.json(mod:str, fcn:str):str
address OPTwrapper
comment "Unroll the mat.pack operation";

#opt_origin
pattern optimizer.origin():str
address OPTwrapper;
pattern optimizer.origin(mod:str, fcn:str):str
address OPTwrapper
comment "OID origin tracker";

#opt_partitions.mal

pattern optimizer.partitions():str
address OPTwrapper;
pattern optimizer.partitions(mod:str, fcn:str):str
address OPTwrapper
comment "Experiment with partitioned databases";

#opt_prejoin.mal
pattern optimizer.prejoin():str
address OPTwrapper;
pattern optimizer.prejoin(mod:str, fcn:str):str
address OPTwrapper
comment "Dead code optimizer";
# command algebra.prejoin(a:bat[:oid],b:bat[:any_1])(c:bat[:oid],d:bat[:any_1])
# address ALGprejoin
# comment "Recluster the table to reduce random access behavior.";

#opt_recycler.mal

pattern optimizer.recycler():str
address OPTwrapper;
pattern optimizer.recycler(mod:str, fcn:str):str
address OPTwrapper
comment "Recycler marking and code injection";

#opt_reduce.mal

pattern optimizer.reduce():str
address OPTwrapper;
pattern optimizer.reduce(mod:str, fcn:str):str
address OPTwrapper
comment "Reduce the stack space claims";

#opt_remap.mal

pattern optimizer.remap():str
address OPTwrapper;
pattern optimizer.remap(mod:str, fcn:str):str
address OPTwrapper
comment "Remapping function calls to a their multiplex variant";

#opt_remoteQueries.mal

pattern optimizer.remoteQueries():str
address OPTwrapper;
pattern optimizer.remoteQueries(mod:str, fcn:str):str
address OPTwrapper
comment "Resolve the multi-table definitions";

#opt_reorder.mal

pattern optimizer.reorder():str
address OPTwrapper;
pattern optimizer.reorder(mod:str, fcn:str):str
address OPTwrapper
comment "Reorder by dataflow dependencies";

#opt_selcrack.mal

pattern optimizer.selcrack():str
address OPTwrapper;
pattern optimizer.selcrack(mod:str, fcn:str):str
address OPTwrapper
comment "Selection cracking";

#opt_statistics.mal

command inspect.getStatistics(nme:str):bat[:any_2]
address QOTgetStatistics
comment "Get optimizer property statistics 
such as #calls, #total actions, #total time";

#opt_trace.mal

pattern optimizer.trace():str
address OPTwrapper;
pattern optimizer.trace(mod:str, fcn:str):str
address OPTwrapper
comment "Collect trace of a specific operation";

# pattern mdb.collect(v:any_1):void
# address OPTtraceCall
# comment "Dump the previous instruction to a temporary file";

#opt_pushselect.mal

pattern optimizer.pushselect():str
address OPTwrapper;
pattern optimizer.pushselect(mod:str, fcn:str):str
address OPTwrapper
comment "Push selects down projections";

optimizer.prelude();

