123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293(* Js_of_ocaml compiler
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2010 Jérôme Vouillon
* Laboratoire PPS - CNRS Université Paris Diderot
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)open!StdlibopenCodeletoptimizableblockspc_=Code.traverseCode.fold_children(funpcacc->ifnotaccthenaccelseletb=Addr.Map.findpcblocksinmatchbwith|{handler=Some_;_}|{branch=Pushtrap_;_}|{branch=Poptrap_;_}->false|_->List.for_allb.body~f:(function|Let(_,Prim(Extern"caml_js_eval_string",_))->false|Let(_,Prim(Extern"debugger",_))->false|Let(_,Prim(Extern("caml_js_var"|"caml_js_expr"|"caml_pure_js_expr"),_))->(* TODO: we should smarter here and look the generated js *)(* let's consider it this opmiziable *)true|_->true))pcblockstrueletrecfollow_branch_recseenblocks=function|(pc,[])ask->(letseen=Addr.Set.addpcseenintrymatchAddr.Map.findpcblockswith|{body=[];branch=Branch(pc,[]);_}whennot(Addr.Set.mempcseen)->follow_branch_recseenblocks(pc,[])|_->kwithNot_found->k)|k->kletfollow_branch=follow_branch_recAddr.Set.emptyletget_closures(_,blocks,_)=Addr.Map.fold(fun_blockclosures->List.fold_leftblock.body~init:closures~f:(funclosuresi->matchiwith|Let(x,Closure(l,cont))->letcont=follow_branchblockscontin(* we can compute this once during the pass
as the property won't change with inlining *)letf_optimizable=optimizableblocks(fstcont)trueinVar.Map.addx(l,cont,f_optimizable)closures|_->closures))blocksVar.Map.empty(****)letrewrite_block(pc',handler)pcblocks=letblock=Addr.Map.findpcblocksinassert(Option.is_noneblock.handler);letblock={blockwithhandler}inletblock=matchblock.branch,pc'with|Returny,Somepc'->{blockwithbranch=Branch(pc',[y])}|_->blockinAddr.Map.addpcblockblockslet(>>)xf=fx(* Skip try body *)letfold_childrenblockspcfaccu=letblock=Addr.Map.findpcblocksinmatchblock.branchwith|Return_|Raise_|Stop->accu|Branch(pc',_)|Poptrap((pc',_),_)->fpc'accu|Pushtrap(_,_,(pc1,_),pcs)->fpc1(Addr.Set.foldfpcsaccu)|Cond(_,_,(pc1,_),(pc2,_))->accu>>fpc1>>fpc2|Switch(_,a1,a2)->letaccu=Array.fold_righta1~init:accu~f:(fun(pc,_)accu->fpcaccu)inletaccu=Array.fold_righta2~init:accu~f:(fun(pc,_)accu->fpcaccu)inacculetrewrite_closureblockscont_pcclos_pchandler=Code.traversefold_children(rewrite_block(cont_pc,handler))clos_pcblocksblocks(****)(*
get new location
put continuation at new location
update closure body to return to this location
make current block continuation jump to closure body
*)letrecfind_mappingmappingx=matchmappingwith|[]->x|([],[])::rest->find_mappingrestx|(a::_,b::_)::restwhenCode.Var.compareax=0->find_mappingrestb|(_::ax,_::bx)::rest->find_mapping((ax,bx)::rest)x|([],_|_,[])::_->assertfalseletsimpleblockscontmapping=letmap_varmappingx=letx'=find_mappingmappingxinifVar.equalxx'thenraiseNot_foundelsex'inletmap_prim_argmapping=function|Pcc->Pcc|Pvx->Pv(map_varmappingx)inletrecfollow(pc,args)(instr:[`Empty|`Okof'a])mapping=letb=Addr.Map.findpcblocksinletmapping=(b.params,args)::mappinginletinstr:[`Empty|`Okof'a|`Fail]=matchb.body,instrwith|[],_->(instr:>[`Empty|`Okof'a|`Fail])|[Let(y,exp)],`Empty->`Ok(y,exp)|_,_->`Failinmatchinstr,b.branchwith|`Fail,_->`Fail|`Empty,Returnret->`Alias(map_varmappingret)|`Ok(x,exp),ReturnretwhenCode.Var.comparex(find_mappingmappingret)=0->(matchexpwith|Const_->`Expexp|Constant(Float_|Int64_|Int_|IString_)->`Expexp|Apply(f,args,true)->`Exp(Apply(map_varmappingf,List.mapargs~f:(map_varmapping),true))|Prim(prim,args)->`Exp(Prim(prim,List.mapargs~f:(map_prim_argmapping)))|Block(tag,args,aon)->`Exp(Block(tag,Array.mapargs~f:(map_varmapping),aon))|Field(x,i)->`Exp(Field(map_varmappingx,i))|Closure_->`Fail|Constant_->`Fail|Apply_->`Fail)|((`Empty|`Ok_)asinstr),Branchcont->followcontinstrmapping|(`Empty|`Ok_),_->`Failintryfollowcont`EmptymappingwithNot_found->`Failletrecargs_equalxsys=matchxs,yswith|[],[]->true|x::xs,Pvy::ys->Code.Var.comparexy=0&&args_equalxsys|_->falseletinlineclosureslive_varsouter_optimizablepc(blocks,free_pc)=letblock=Addr.Map.findpcblocksinletbody,(branch,blocks,free_pc)=List.fold_rightblock.body~init:([],(block.branch,blocks,free_pc))~f:(funi(rem,state)->matchiwith|Let(x,Apply(f,args,true))whenVar.Map.memfclosures->(letbranch,blocks,free_pc=stateinletparams,clos_cont,f_optimizable=Var.Map.findfclosuresinmatchsimpleblocksclos_cont[params,args]with|`Aliasarg->(matchrem,branchwith|[],ReturnywhenVar.comparexy=0->[],(Returnarg,blocks,free_pc)|_->letblocks=Addr.Map.addfree_pc{params=[x];handler=block.handler;body=rem;branch}blocksin[],(Branch(free_pc,[arg]),blocks,free_pc+1))|`Expexp->Let(x,exp)::rem,state|`Fail->iflive_vars.(Var.idxf)=1&&Bool.equalouter_optimizablef_optimizable(* inlining the code of an optimizable function could make
this code unoptimized. (wrt to Jit compilers)
At the moment, V8 doesn't optimize function containing try..catch.
We disable inlining if the inner and outer functions don't have
the same "contain_try_catch" property *)thenletblocks,cont_pc=matchrem,branchwith|[],ReturnywhenVar.comparexy=0->(* We do not need a continuation block for tail calls *)blocks,None|_->(Addr.Map.addfree_pc{params=[x];handler=block.handler;body=rem;branch}blocks,Somefree_pc)inletblocks=rewrite_closureblockscont_pc(fstclos_cont)block.handlerin(* We do not really need this intermediate block. It
just avoid the need to find which function parameters
are used in the function body. *)letblocks=Addr.Map.add(free_pc+1){params;handler=block.handler;body=[];branch=Branchclos_cont}blocksin[],(Branch(free_pc+1,args),blocks,free_pc+2)else(* Format.eprintf "Do not inline because inner:%b outer:%b@." f_has_handler outer_has_handler; *)i::rem,state)|Let(x,Closure(l,(pc,[])))->(letblock=Addr.Map.findpcblocksinmatchblockwith|{body=[Let(y,Prim(Externprim,args))];branch=Returny';handler=None;params=[]}->letlen=List.lengthlinifCode.Var.compareyy'=0&&Primitive.has_arityprimlen&&args_equallargsthenLet(x,Prim(Extern"%closure",[Pc(IStringprim)]))::rem,stateelsei::rem,state|_->i::rem,state)|_->i::rem,state)inAddr.Map.addpc{blockwithbody;branch}blocks,free_pc(****)lettimes=Debug.find"times"letf((pc,blocks,free_pc)asp)live_vars=Code.invariantp;lett=Timer.make()inletclosures=get_closurespinletblocks,free_pc=Code.fold_closuresp(funname_(pc,_)(blocks,free_pc)->letouter_optimizable=matchnamewith|None->optimizableblockspctrue|Somex->let_,_,b=Var.Map.findxclosuresinbinCode.traverseCode.fold_children(inlineclosureslive_varsouter_optimizable)pcblocks(blocks,free_pc))(blocks,free_pc)iniftimes()thenFormat.eprintf" inlining: %a@."Timer.printt;letp=pc,blocks,free_pcinCode.invariantp;p