ó
Û³Y]c           @   s(  d  d l  Z d  d l Z d  d l Z d  d l m Z m Z m Z m Z m Z m	 Z	 m  Z  m
 Z
 d  d l m Z d  d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m  Z  d  d l! m" Z" m# Z# m$ Z$ d  d l% m& Z& d  d l' m( Z) d  d l* Z+ d  d l, Z, e- Z. d e/ f d	 „  ƒ  YZ0 d
 „  Z1 d d d d d d „ Z3 d „  Z4 d „  Z5 d „  Z6 d „  Z7 d „  Z8 d „  Z9 d „  Z: d „  Z; d „  Z< e= d „ Z> d „  Z? d „  Z@ d „  ZA e	 jB d ƒ d e	 jC f d „  ƒ  Yƒ ZD d „  ZE d S(   iÿÿÿÿN(   t   configt   irt   ir_utilst   utilst   pranget   rewritest   typest   typing(   t   internal_prange(   t   mk_unique_vart
   next_labelt   add_offset_to_labelst   replace_varst   remove_delst   remove_deadt   rename_labelst   find_topo_ordert   merge_adjacent_blockst   GuardExceptiont   requiret   guardt   get_definitiont   find_callnamet   find_build_sequencet   get_np_ufunc_typt   get_ir_of_codet   simplify_CFGt   canonicalize_array_math(   t   compute_cfg_from_blockst   compute_use_defst   compute_live_variables(   t   range_iter_len(   t   empty_inferredt   InlineClosureCallPassc           B   sP   e  Z d  Z i  d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s—   InlineClosureCallPass class looks for direct calls to locally defined
    closures, and inlines the body of the closure function to the call site.
    c         C   s(   | |  _  | |  _ | |  _ g  |  _ d  S(   N(   t   func_irt   parallel_optionst   swappedt   _processed_stencils(   t   selfR"   R#   R$   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   __init__0   s    			c      	   C   sÒ  t  } t |  j j j ƒ  ƒ } t d ƒ } | d ƒ x"| rX| j ƒ  \ } } xt | j ƒ D]ò \ } } t	 | t
 j ƒ r_ | j } | j }	 t	 |	 t
 j ƒ rQ|	 j d k rQt t |  j |	 ƒ }
 t t |  j |	 j ƒ } t |  j | | | |	 |
 ƒ rt } Pn  t |  j | | | | ƒ r*t } Pn  t |  j | |
 | ƒ rNt } qNqQq_ q_ Wq7 Wt rk| rxt |  j j ƒ n  t |  j j ƒ } | d ƒ t | ƒ | j ƒ  } g  | j ƒ  D] } | t | | j ƒ f ^ q·} g  } xm t | d d „  d t ƒD]P \ } } | j  | ƒ t t! |  j | | | | |  j" |  j# j$ ƒ rþt } qþqþW| rkt% |  j ƒ qkn  | rÄt& |  j j ƒ x% t' |  j j |  j j( |  j ƒ r¨q„Wt) |  j j ƒ |  j _ n  | d ƒ d	 S(
   s&   Run inline closure call pass.
        R!   t   STARTt   calls   start inline arraycallt   keyc         S   s   |  d S(   Ni   (    (   t   tup(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   <lambda>a   t    t   reverset   ENDN(*   t   Falset   listR"   t   blockst   itemst   _make_debug_printt   popt	   enumeratet   bodyt
   isinstanceR   t   Assignt   targett   valuet   Exprt   opR   R   R   t   funct   _inline_reductiont   Truet   _inline_closuret   _inline_stencilt   enable_inline_arraycallR   R   t   _debug_dumpt   loopst   keyst   lent   sortedt   appendt   _inline_arraycallR$   R#   t   comprehensiont   _fix_nested_arrayR   R   t	   arg_namesR   (   R&   t   modifiedt	   work_listt   debug_printt   labelt   blockt   it   instrt   lhst   exprt	   call_namet   func_deft   cfgRE   t   kt   sized_loopst   visitedt   s(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   run6   s^    
			!			

2(c         C   s    t  |  j j ƒ t  | d	 k p) | d
 k ƒ t | j ƒ d k rQ t d ƒ ‚ n  t |  j | j d ƒ d „  } t |  j |  j j	 j
 j | | | d | ƒt S(   Nt   reducet   builtinst
   _functoolsi   sE   invalid reduce call, three arguments including initial value requiredi    c         S   s6   | } t  | ƒ } x | D] } |  | | ƒ } q W| S(   N(   t   iter(   t   ft   At   vR]   t   itt   a(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   reduce_func~   s
    RO   (   R_   R`   (   R_   Ra   (   R   R#   t	   reductionRG   t   argst	   TypeErrort   check_reduce_funcR"   t   inline_closure_callt   func_idR>   t   __globals__R@   (   R&   RO   RR   RS   RV   RW   Rh   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR?   s   s    		c         C   s  d d l  m } | j } | j } t | t j ƒ r‰ | j d k r‰ t | j | ƒ r‰ | j rv | j | j j 7_ n | j j | _ t	 St
 | d k p¡ | d k ƒ t
 | |  j k ƒ |  j j | ƒ t | j ƒ d k sì t d ƒ ‚ n  t t |  j | j d ƒ } t
 t | t j ƒ o)| j d	 k ƒ t |  j j j j | j ƒ } t | j ƒ }	 d
 |	 k rt |  j |	 ƒ }
 |
 st d ƒ ‚ qn  d |	 k rÆt |  j |	 ƒ }
 |
 sÆt d ƒ ‚ qÆn  | | d |	 ƒ } | j | _ t j d | | j ƒ } | g |  j j | j <| | _ t	 S(   Niÿÿÿÿ(   t   StencilFunct   stencils   numba.stencilt   numbai   s5   As a minimum Stencil requires a kernel as an argumenti    t   make_functiont   neighborhoodsX   stencil neighborhood option should be a tuple with constant structure such as ((-w, w),)t   index_offsetssY   stencil index_offsets option should be a tuple with constant structure such as (offset, )t   constant(   Rq   s   numba.stencil(   Rq   Rr   (   t   numba.stencilRp   R:   R;   R8   R   t   Globalt   namet   kwsR@   R   R%   RI   RG   Rj   t
   ValueErrorR   R   R"   R<   R=   R   Rn   R>   Ro   t   codet   dictt   _fix_stencil_neighborhoodt   _fix_stencil_index_offsetst   loct   _definitions(   R&   RT   RW   RX   Rp   RU   RV   t   stencil_deft	   kernel_irt   optionst   fixedt   sft	   sf_global(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRB   ‰   sH    				c         C   s’   t  |  j | d ƒ } t t | d ƒ ƒ g  } xL | j D]A } t  |  j | ƒ } t t | d ƒ ƒ | j t | j ƒ ƒ q9 Wt | ƒ | d <t S(   s’   
        Extract the two-level tuple representing the stencil neighborhood
        from the program IR to provide a tuple to StencilFunc.
        Rt   R3   (   R   R"   R   t   hasattrR3   RI   t   tupleR@   (   R&   R„   t   dims_build_tuplet   rest
   window_vart   win_build_tuple(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR~   ·   s    c         C   s@   t  |  j | d ƒ } t t | d ƒ ƒ t | j ƒ | d <t S(   s   
        Extract the tuple representing the stencil index offsets
        from the program IR to provide to StencilFunc.
        Ru   R3   (   R   R"   R   Rˆ   R‰   R3   R@   (   R&   R„   t   offset_tuple(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR   Ç   s    c         C   sT   t  t | t j ƒ o! | j d k ƒ t |  j |  j j j j	 | | | d | ƒt
 S(   NRs   RO   (   R   R8   R   R<   R=   Rm   R"   Rn   R>   Ro   R@   (   R&   RO   RR   RS   RX   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRA   Ñ   s    	(
   t   __name__t
   __module__t   __doc__R'   R^   R?   RB   R~   R   RA   (    (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR!   +   s   	=		.		
c         C   s   t  t |  | ƒ } | d  k r- t d ƒ ‚ n  t | d ƒ pH t | d ƒ sZ t d ƒ ‚ n  t | d ƒ rr | j n | j } | j d k s™ t d ƒ ‚ n  d  S(   NsM   Reduce function cannot be found for njit                             analysisR|   t   __code__s   Invalid reduction functioni   s*   Reduction function should take 2 arguments(	   R   R   t   NoneR{   Rˆ   R|   R’   t   co_argcountRk   (   R"   t   func_varRh   t   f_code(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRl   Ù   s    	c
   ,         sN  | j  }
 | j | } | j } t d ƒ } | d | d | ƒ t | d ƒ rV | j n | j } t | d ƒ rw | j n | j } t | d ƒ r˜ | j	 n | j
 } t | | ƒ } | j } t t j t |  j j ƒ  ƒ ƒ } t | | d ƒ } t | ƒ } | | _ t | j ƒ  ƒ } t | j ƒ  ƒ } | t _ | d ƒ t | ƒ t | ƒ } | d	 | ƒ t | ƒ d k stt ‚ | d
 } i  } x] | j j j ƒ  D]I } | j | j k r—|
 j t | j ƒ d | j ƒ} | | | j <q—q—W| d | ƒ t  | | ƒ | d ƒ t | ƒ t! | j" ƒ } | rô| d | ƒ t# | t$ ƒ rV| t! | ƒ } qôt# | t% j& ƒ swt# | t' ƒ rÜ|  j( | ƒ } t# | t% j) ƒ sžt ‚ | j } | g  | j D] } t% j) d | d | ƒ ^ q´} qôt* d j+ | ƒ ƒ ‚ n  | d ƒ t | ƒ | rô|  j( | ƒ } | d | ƒ t# | t$ ƒ rt, j- j. ‰  t, j/ ˆ  _0 t, j/ f ˆ  _1 t$ ‡  f d †  | Dƒ ƒ } n0 t# | t% j2 ƒ r | j3 d k s¦t ‚ | j4 } t | j ƒ t | ƒ k sÐt ‚ t5 | | ƒ | d ƒ t | ƒ n  | rÚd d l6 m7 }  y% |  j8 | | | d ƒ \ }! }" }# Wn2 t: k
 rc|  j8 | | | d ƒ \ }! }" }# n Xt; | |! |# | ƒ g  |! D] }$ |$ j< d ƒ r~|$ ^ q~}% x |% D] }& |! j= |& ƒ q¦W| j> |! ƒ | j> |# ƒ n  t? | | ƒ g  }' t% j@ |
 | j ƒ }( | j | d |( _ tA ƒ  }) |( |  j |) <|' jB |) |( f ƒ | j |  | _ | j jB t% jC | | j ƒ ƒ tD | ƒ }* tE | | jF |) ƒ | jF j |  jG k r¸g  |  jG | jF j <n  xN |* D]F }+ | |+ } |
 | _  tH |  | ƒ | |  j |+ <|' jB |+ | f ƒ q¿W| d ƒ t |  ƒ |	 d k rJx |' D] } |	 jB | ƒ q0Wn  | S(   sB  Inline the body of `callee` at its callsite (`i`-th instruction of `block`)

    `func_ir` is the func_ir object of the caller function and `glbls` is its
    global variable environment (func_ir.func_id.func.__globals__).
    `block` is the IR block of the callsite and `i` is the index of the
    callsite's node. `callee` is either the called function or a
    make_function node. `typingctx`, `typemap` and `calltypes` are typing
    data structures of the caller, available if we are in a typed pass.
    `arg_typs` includes the types of the arguments at the callsite.
    Rm   s   Found closure call: s    with callee = R|   t   defaultst   closurei   s   After relabels   callee_scopes = i    R€   s   var_dict = s   After local var renames   defaults = R;   s)   Unsupported defaults to make_function: {}s   After arguments rename: s   callee's closure = c         3   s   |  ] } ˆ  | ƒ Vq d  S(   N(    (   t   .0t   x(   t   cellget(    s7   lib/python2.7/site-packages/numba/inline_closurecall.pys	   <genexpr>8  s    t   build_tuples   After closure renameiÿÿÿÿ(   t   compilers   arg.s   After merge inN(I   t   scopeR7   R;   R4   Rˆ   R|   R’   R—   t   __defaults__R˜   t   __closure__R   R2   t   maxR   t
   _max_labelRF   R   R   t   minRD   t   _get_all_scopesRG   t   AssertionErrort	   localvarst   _cont   valuesRy   t   co_freevarst   defineR	   R€   R   R1   Rj   R8   R‰   R   t   Vart   strR   t   Constt   NotImplementedErrort   formatt   ctypest	   pythonapit
   PyCell_Gett	   py_objectt   restypet   argtypesR<   R=   R3   t   _replace_freevarsRr   R   t   type_inference_stageR“   t   BaseExceptionR   t
   startswithR5   t   updatet   _replace_args_witht   BlockR
   RI   t   JumpR   t   _replace_returnsR:   R   t   _add_definitions(,   R"   t   glblsRR   RS   t   calleet	   typingctxt   arg_typst   typemapt	   calltypesRO   Rž   RT   t	   call_exprRP   t   callee_codet   callee_defaultst   callee_closuret	   callee_irt   callee_blockst	   max_labelt	   min_labelt   callee_scopest   callee_scopet   var_dictt   vart   new_varRj   R—   R€   Re   R˜   R3   R   t	   f_typemapt   f_return_typet   f_calltypest   vnameRM   Rg   t
   new_blockst	   new_blockt	   new_labelt
   topo_orderRQ   (    (   R›   s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRm   è   sÎ    		!!!	!		


!

!	/

	!
	
(	
	

c            s   ‡  f d †  } | S(   Nc             s0   t  j r, ˆ  d d j d „  |  Dƒ ƒ GHn  d  S(   Ns   : R-   c         s   s   |  ] } t  | ƒ Vq d  S(   N(   R¬   (   R™   Rš   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pys	   <genexpr>y  s    (   R    t   DEBUG_INLINE_CLOSUREt   join(   Rj   (   t   prefix(    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRP   w  s    	(    (   RÝ   RP   (    (   RÝ   s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR4   v  s    c         C   s   t  j r |  j ƒ  n  d  S(   N(   R    RÛ   t   dump(   R"   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRD   |  s    	c         C   sI   g  } x< |  j  ƒ  D]. \ } } | j | k r | j | j ƒ q q W| S(   s+   Get all block-local scopes from an IR.
    (   R3   Rž   RI   (   R2   t
   all_scopesRQ   RR   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR¤     s
    c         C   s   x† |  j  ƒ  D]x \ } } | j t j ƒ } xW | D]O } t | j t j ƒ r2 | j j } | t | ƒ k  sq t	 ‚ | | | _ q2 q2 Wq Wd S(   s@   
    Replace ir.Arg(...) with real arguments from call site
    N(
   R3   t
   find_instsR   R9   R8   R;   t   Argt   indexRG   R¥   (   R2   Rj   RQ   RR   t   assignst   stmtt   idx(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR»   ‹  s    c         C   sÂ   x» |  j  ƒ  D]­ \ } } | j t j ƒ } xŒ | D]„ } t | j t j ƒ r2 | j j } | t | ƒ k  sq t	 ‚ t | | t j
 ƒ r— | | | _ q¶ t j | | | j ƒ | _ q2 q2 Wq Wd S(   sJ   
    Replace ir.FreeVar(...) with real variables from parent function
    N(   R3   Rà   R   R9   R8   R;   t   FreeVarRâ   RG   R¥   R«   R­   R€   (   R2   Rj   RQ   RR   Rã   Rä   Rå   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR¶   ˜  s    c   	      C   sM  xF|  j  ƒ  D]8\ } } g  } x#t t | j ƒ ƒ D]} | j | } t | t j ƒ rø | d t | j ƒ k sy t ‚ t j | j	 | | j
 ƒ | j | <| j j t j | | j
 ƒ ƒ x„ | D]0 } | j j | j	 j k rÁ | j	 j	 | _	 qÁ qÁ Wq5 t | t j ƒ r5 t | j	 t j ƒ r5 | j	 j d k r5 | j | ƒ q5 q5 Wq Wd S(   sN   
    Return return statement by assigning directly to target, and a jump.
    i   t   castN(   R3   t   rangeRG   R7   R8   R   t   ReturnR¥   R9   R;   R€   RI   R½   R:   Ry   R<   R=   (	   R2   R:   t   return_labelRQ   RR   t   castsRS   Rä   Rç   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR¾   ¨  s    "9c         C   sJ   |  j  } | j t j ƒ } x( | D]  } | | j j j | j ƒ q" Wd S(   sF   
    Add variable definitions found in a block to parent func_ir.
    N(   R   Rà   R   R9   R:   Ry   RI   R;   (   R"   RR   t   definitionsRã   Rä   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR¿   »  s    	c         C   sº  d } d } t } d } d } xe| t | j ƒ k  r…| j | } t | t j ƒ r€ | rx| rx| j | j k rxt	 } Pqxnø t | t j
 ƒ r| j } | j }	 t t |  |	 ƒ d k rxt |	 j d t j ƒ rx|	 j d } | } | }
 t |	 j ƒ } qxnu t | t j ƒ rwt | j t j ƒ rw| rw| j } | j } t |  | ƒ } t t t |  | ƒ ƒ | }
 i  } n P| d } q! Wt | o’| ƒ t d ƒ | j |
 ƒ | |
 | f S(   sá   Look for statement like "x = numpy.array(y)" or "x[..] = y"
    immediately after the closure call that creates list y (the i-th
    statement in block).  Return the statement index if found, or
    raise GuardException.
    i    t   arrayt   numpyi   t   find_array_callN(   Rí   Rî   (   R“   R0   RG   R7   R8   R   t   DelR;   Ry   R@   R9   R:   R   R   Rj   R«   R}   Rz   t   SetItemR   R   t   _find_unsafe_empty_inferredR4   (   R"   RR   t	   array_vart   array_call_indext   list_var_dead_after_array_callt   list_varRS   RT   RU   RV   t   array_stmt_indext	   array_kwst	   array_def(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   _find_arraycallÄ  sF    					c         C   sË  t  d ƒ } t |  | ƒ } | d | d | ƒ t t | t j ƒ oO | j d k ƒ | j } t |  | ƒ } | d | d | ƒ t t | t j ƒ oŸ | j d k ƒ | j } t |  | ƒ } | d | d	 | ƒ t t | t j	 ƒ oï | j t
 k ƒ t | j ƒ }	 d | j j g }
 |	 d k r`|
 | | j j <t |  | j d d t ƒ} d | j d | f S|	 d k rÁ|
 | | j j <t |  | j d d t ƒ} t |  | j d d t ƒ} | | | f St ‚ d S(   sz   Find the iterator's actual range if it is either range(n), or range(m, n),
    otherwise return raise GuardException.
    t   find_iter_ranges   range_iter_var = s    def = t   getiters   range_var = s    range_def = R)   s   func_var = s    func_def = s   "array comprehension"s
   closure ofi   i    t   lhs_onlyi   N(   s   "array comprehension"s
   closure of(   R4   R   R   R8   R   R<   R=   R;   R>   Rx   Rè   RG   Rj   R€   Ry   R@   R   (   R"   t   range_iter_varR$   RP   t   range_iter_deft	   range_vart	   range_defR•   RX   t   nargst   swappingt   stopt   start(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   _find_iter_rangeö  s0    %	%	%c   <      C   sI  t  d ƒ } t t | j ƒ d k ƒ t t | j ƒ ƒ } t |  |  j | ƒ \ } }	 }
 d+ } d+ } d |
 k rØ t t	 |
 d t
 j ƒ ƒ t |  |
 d ƒ } t t	 | t
 j ƒ o¿ | j d k ƒ t |  | j ƒ } n  t |  | ƒ } | d | d | ƒ t	 | t
 j ƒ r0| j d k r0t |  | j ƒ } n  t t	 | t
 j ƒ oQ| j d k ƒ g  } xM| j D]B} g  | j | ƒ D] } | j | k ^ q{} t | ƒ s¨qen  |  j | } | d	 | ƒ xâ | j t
 j ƒ D]Î } | j } | j } t	 | t
 j ƒ rÕ| j d
 k rÕt |  | j ƒ } t	 | t
 j ƒ r£| j d k r£| j d k r£t |  | j ƒ } | d | | | k ƒ | | k r | j | | | f ƒ q q£qÕqÕWqeWt t | ƒ d k ƒ | d \ } } } t d „  | j | j ƒ Dƒ ƒ } | d | | j t | g ƒ Bƒ t | | j t | g ƒ Bk ƒ g  } g  } |  j | j } x› | j t
 j ƒ D]‡ } | j } t	 | t
 j ƒ re| j d k rÇt |  | j ƒ } | d | ƒ | j | j ƒ qì| j d k rì| j | j ƒ qìqeqeWt t | ƒ d k ot | ƒ d k ƒ | d }  | d }! t t | j ƒ d k ƒ |  j t t | j ƒ ƒ }" |" j }# |" j }$ |" j }% g  }& g  }' d „  }( x t  t |" j ƒ d ƒ D]f }) |" j |) } t	 | t
 j ƒ r| j | k sñ|( | j |' ƒ r|' j | j ƒ q«|& j | ƒ q«W| d |' ƒ t! t" |  |  | ƒ }* t
 j |$ t# d ƒ |% ƒ }+ |* rq|* d d k rq|! }+ n. |& j t$ |  |+ t
 j% d d d |% ƒ |% ƒ ƒ t
 j |$ t# d ƒ |% ƒ }, |* r>|* \ }- }. }/ |- d k rä|. }0 n* t
 j j& d t' j( d |. d |- d |% ƒ }0 | r¨t	 |/ t
 j) ƒ r¨d |/ _* t+ |/ _ q¨nj t
 j |$ t# d ƒ |% ƒ }1 |& j t$ |  |1 t
 j) d t, d |% ƒ|% ƒ ƒ t
 j j- |1 |  f d, d |% ƒ}0 |& j t$ |  |, |0 |% ƒ ƒ t
 j |$ t# d  ƒ |% ƒ }2 |& j t$ |  |2 t
 j j. d! |, g d |% ƒ |% ƒ ƒ t
 j |$ t# d" ƒ |% ƒ }3 t
 j |$ t# d# ƒ |% ƒ }4 | r| rt
 j |$ t# d$ ƒ |% ƒ }5 t
 j |$ t# d ƒ |% ƒ }6 |& j t$ |  |5 | |% ƒ ƒ |& j t$ |  |6 t
 j j/ |5 | j |% ƒ |% ƒ ƒ |& j t$ |  |4 t
 j) d% t0 j1 d |% ƒ|% ƒ ƒ d |6 f g }
 n4 |& j t$ |  |4 t
 j) d& t2 d |% ƒ|% ƒ ƒ g  }
 |& j t$ |  |3 t
 j j- |4 |2 f t3 |
 ƒ d |% ƒ|% ƒ ƒ x* |' D]" }7 |& j t$ |  |7 |3 |% ƒ ƒ q“W|& j |# ƒ |& |" _ |* ru	|* d d k rm
| j }# t	 |# t
 j4 ƒ s	t5 ‚ |# j6 }8 |  j |8 }9 |9 j }% |9 j j7 d t$ |  |+ t
 j j& d t' j( d |! d |* d d |% ƒ |% ƒ ƒ qm
nø | j }% | j }# | j d d !}& t
 j |$ t# d' ƒ |% ƒ }: t
 j |$ t# d( ƒ |% ƒ }; |& j t$ |  |; t
 j% d d d |% ƒ |% ƒ ƒ |& j t$ |  |: t
 j j& d t' j8 d |+ d |; d |% ƒ |% ƒ ƒ |& j t$ |  |+ |: |% ƒ ƒ |& j |# ƒ |& | _ xu t  t | j ƒ ƒ D]^ }) | j |) | k rƒ
| d) ƒ t
 j9 d* |3 d |+ d | j j: d d | j ƒ | j |) <qƒ
qƒ
W|  j | j |	 } t	 | t
 j ƒ rEt	 | j t
 j ƒ rE|3 | _ | j g |  j; | j j* <n  t< S(-   sã  Look for array(list) call in the exit block of a given loop, and turn list operations into
    array operations in the loop if the following conditions are met:
      1. The exit block contains an array call on the list;
      2. The list variable is no longer live after array call;
      3. The list is created in the loop entry block;
      4. The loop is created from an range iterator whose length is known prior to the loop;
      5. There is only one list_append operation on the list variable in the loop body;
      6. The block that contains list_append dominates the loop head, which ensures list
         length is the same as loop length;
    If any condition check fails, no modification will be made to the incoming IR.
    t   inline_arraycalli   t   dtypet   getattrs   list_var = s    def = Rç   t
   build_lists   check loop body block R)   RI   s   list_def = i    c         s   s   |  ] \ } } | Vq d  S(   N(    (   R™   t   lt   b(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pys	   <genexpr>Y  s    s   preds = t   iternexts   iter_def = t
   pair_firstc         S   s@   t  |  t j ƒ r< x' | D] } | j |  j k r t Sq Wn  t S(   N(   R8   R   R«   Ry   R@   R0   (   t   valt   removedRš   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt
   is_removedz  s
    s   removed variables: Râ   R;   iÿÿÿÿR€   t   sizet   fnRU   t   rhsR   t   len_funcR   t
   size_tupleR3   Rí   t
   empty_funct	   dtype_modt   emptyt   unsafe_empty_inferredt
   next_indext   ones   Replace append with SetItemR:   N(    (=   R4   R   RG   t   exitst   nextRb   Rú   R2   R“   R8   R   R«   R   R<   R=   R;   R7   t   in_loopst   headert   allRà   R9   R:   R>   t   attrRI   t   sett   predecessorst   entriest
   terminatorRž   R€   Rè   R   R  R	   t   _new_definitionR­   t   binopt   operatort   subRx   Ry   R   R   R)   Rœ   R	  t   npR  R  R1   t   BranchR¥   t   truebrt   insertt   addRñ   Rj   R   R@   (<   R"   RY   R\   t   loopR$   t   enable_prangeRP   t
   exit_blockRö   Rô   Rø   t	   dtype_deft   dtype_mod_deft   list_var_deft   list_append_stmtsRQ   R  t   in_visited_loopsRR   Rä   RU   RV   RX   t   list_deft   append_block_labelt   append_blockt   append_stmtt   predst	   iter_varst   iter_first_varst   loop_headert   iter_deft   iter_vart   iter_first_vart
   loop_entryR&  Rž   R€   t   stmtsR  R  RS   R  t	   index_vart   size_varR  R  t   range_func_deft   size_valt   len_func_vart   size_tuple_varRó   R  t   dtype_mod_vart	   dtype_varRÑ   t   block_idt   blkt   next_index_varR  (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRJ     s    %!%+		!!'"  	(

				 3	.	*	!%""	. 						1	
*'	c         C   sw   t  t t | t j ƒ o% | j d k ƒ | j } t |  | ƒ } t t | t j ƒ ƒ t	 d ƒ | j
 ƒ | j
 t  k S(   NR)   Rò   (   R  R   R8   R   R<   R=   R>   R   Rx   R4   R;   (   R"   RV   RÁ   t
   callee_def(    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRò     s    %	c            s  ˆ j  ‰  t ˆ  ƒ } t ˆ  ƒ ‰ t g  ˆ  j ƒ  D] } | t ƒ  f ^ q1 ƒ } t | ˆ  ˆ j | ƒ ‰ ‡ ‡ f d †  ‰ ‡  ‡ ‡ ‡ f d †  ‰ ‡ ‡ ‡ f d †  } xZ t ˆ j  ƒ D]I } ˆ j  | } x3 | j	 D]( } t
 | | ƒ rÓ | j	 j | ƒ qÓ qÓ Wq¶ Wd S(   s—   Look for assignment like: a[..] = b, where both a and b are numpy arrays, and
    try to eliminate array b by expanding a with an extra dimension.
    c            ss   ˆ j  |  ƒ } t d ƒ |  | ƒ t | t j ƒ ri t t ˆ | ƒ rJ | S| j d k ri ˆ  | j ƒ Sn  t	 ‚ d S(   s¯   Find numpy array definition such as
            arr = numba.unsafe.ndarray.empty_inferred(...).
        If it is arr = b[...], find array definition of b recursively.
        t   find_array_deft   getitemN(
   R   R4   R8   R   R<   R   Rò   R=   R;   R   (   t   arrt   arr_def(   RQ  R"   (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRQ    s    c            sõ  t  d ƒ } xÜˆ  j ƒ  D]Î\ } } | j } | j } t ƒ  } x¤t t | ƒ ƒ D]} | | }	 t |	 t j	 ƒ rS | j
 |	 j j ƒ |	 j |  k rãg  }
 x<| D]4} | j | k sâ | j ˆ | k r| j ˆ j | k r| | j d ƒ |
 j | ƒ q¤ | | j d ƒ t ˆ | j ƒ } t | t j ƒ rÒ| j } t j | t d ƒ | ƒ } t j | j | ƒ } t ˆ | | | ƒ } g  } | j | |  ƒ | j | ƒ | j | | ƒ | | _ |
 j | ƒ q¤ t ‚ q¤ W|
 SqS qS Wq Wt ‚ d S(   sÒ   Double check if all variables in varlist are defined before
        expr is used. Try to move constant definition when the check fails.
        Bails out by raising GuardException if it can't be moved.
        t   fix_dependenciess    already defineds    not yet definedRÒ   N(   R4   R3   Rž   R7   R#  Rè   RG   R8   R   R9   R/  R:   Ry   R;   t   defmapRI   R   R­   R€   R«   R	   R'  t   extendR   (   RV   t   varlistRP   RQ   RR   Rž   R7   t   definedRS   t   instt   new_varlistRÑ   t   var_defR€   RÒ   t	   new_constt
   new_vardeft   new_body(   R2   R"   t   livemapt   usedefs(    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRU  #  sD    			
		
c   	         s+  t  t |  t j ƒ ƒ t  t |  j t j ƒ ƒ t d ƒ } | d |  ƒ |  j } ˆ  | ƒ } | d | ƒ t ˆ |  j ƒ } | d | ƒ t  t | t j	 ƒ ƒ | j
 d k rÙ t ˆ | j ƒ } t  t | t j	 ƒ ƒ n  t  t ˆ | ƒ ƒ t ˆ | j d ƒ } t  t | t j	 ƒ o#| j
 d k ƒ | d | ƒ g  | j D] } t ˆ | d	 t ƒ^ q>} | d
 | ƒ t ˆ | j d ƒ } t  t | t j	 ƒ o£| j
 d k ƒ | d | ƒ ˆ | | ƒ } | j | 7_ d | _
 t ˆ | d	 t ƒ| _ |  j | _ | j d =| j d =| j d =| j d =t S(   s‘  For assignment like lhs[idx] = rhs, where both lhs and rhs are arrays, do the
        following:
        1. find the definition of rhs, which has to be a call to numba.unsafe.ndarray.empty_inferred
        2. find the source array creation for lhs, insert an extra dimension of size of b.
        3. replace the definition of rhs = numba.unsafe.ndarray.empty_inferred(...) with rhs = lhs[idx]
        t   fix_array_assigns   found SetItem: s   found lhs_def: s   found rhs_def: Rç   i    Rœ   s
   dim_def = Rý   s   extra_dims = s   size_tuple_def = RR  R>   Rj   t   varargRz   (   R   R8   R   Rñ   R;   R«   R4   R:   R   R<   R=   Rò   Rj   R3   R@   Râ   t   _kws(	   Rä   RP   RU   t   lhs_deft   rhs_deft   dim_defRš   t
   extra_dimst   size_tuple_def(   RQ  RU  R"   (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRb  P  s@    	%+%	



N(   R2   R   R   R}   RF   R#  R   RV  R   R7   R   t   remove(   R"   RY   RQ   t   empty_deadmapRb  RR   Rä   (    (   R2   RQ  RU  R"   R`  Ra  s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRL     s    	.--c         C   s/   | g |  j  | j <t j d | d | d | ƒ S(   NR;   R:   R€   (   R   Ry   R   R9   (   R"   RÑ   R;   R€   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR'  ƒ  s    s   after-inferencet   RewriteArrayOfConstsc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sÈ   The RewriteArrayOfConsts class is responsible for finding
    1D array creations from a constant list, and rewriting it into
    direct initialization of array elements without creating the list.
    c         O   s,   | j  |  _  t t |  ƒ j | | | Ž d  S(   N(   RÂ   t   superRl  R'   (   R&   t   pipelineRj   Rz   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyR'     s    c         C   sM   t  | ƒ d k r t S| |  _ t t | | |  j | | ƒ |  _ |  j d  k S(   Ni    (   RG   R0   t
   crnt_blockR   t   _inline_const_arraycallRÂ   R_  R“   (   R&   R"   RR   RÄ   RÅ   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   match‘  s    	c         C   s   |  j  |  j _ |  j S(   N(   R_  Ro  R7   (   R&   (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   apply™  s    (   R   R   R‘   R'   Rq  Rr  (    (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRl  ‡  s   		c            s  t  d ƒ ‰ |  j ‰ ‡  ‡ ‡ ‡ ‡ ‡ f d †  } g  } g  } g  } g  }	 g  }
 t } xš|  j D]} t | t j ƒ rŽt | j t j ƒ rÉ | j j	 | k r‹| j
 | j j	 ƒ |	 j
 | ƒ qa q‹q”t | j t j ƒ r”| j } | j d k r7| j j	 g } g  | j D] } | j	 ^ q} |	 j
 | ƒ qa q‹| j d k r‹| ˆ  k r‹| j } t | | j | |	 | |
 ƒ rˆt } qa qˆq‹q”nt | t j ƒ r”| j } | | k rÈ|
 j
 | ƒ qa q”| | k r”| j
 | ƒ | j | ƒ |	 j
 | ƒ | g  k r‘g  } xf |	 D]^ } t | t j ƒ r>| j j	 | k st | t j ƒ re| j | k reqn  | j
 | ƒ qW| }	 g  } t } qa q‘q”n  |	 j
 | ƒ t g  | j ƒ  D] } | j	 | k ^ q±ƒ } | ra g  } g  } g  } g  }
 qa qa W| rþ|	 Sd S(   s|  Look for array(list) call where list is a constant list created by build_list,
    and turn them into direct array creation and initialization, if the following
    conditions are met:
      1. The build_list call immediate preceeds the array call;
      2. The list variable is no longer live after array call;
    If any condition check fails, no modification will be made.
    t   inline_const_arraycallc       	      sL  t  t ˆ | ƒ } t | o8 | d d k o8 | d d k ƒ t | j d j | k ƒ ˆ  | j } t t | t j ƒ o„ | j	 d k ƒ | j
 } | j d } ˆ |  j }	 ˆ d |  d | ƒ |	 j }
 t ˆ | ƒ \ } } t | ƒ } t j ˆ t d ƒ | ƒ } t j ˆ t d ƒ | ƒ } t j } t j | d ƒ } | ˆ | j <| ˆ | j <| j t ˆ | t j | d	 | ƒ| ƒ ƒ | j t ˆ | t j j d
 | g d	 | ƒ | ƒ ƒ t j |
 ƒ } t j ˆ t d ƒ | ƒ } t t j ƒ } ˆ j | | f i | d 6ƒ } | ˆ | j <| j t ˆ | t j d t j d	 | ƒ| ƒ ƒ t j ˆ t d ƒ | ƒ } t j j t ƒ ˆ | j <t j d t | ƒ } | j t ˆ | | | ƒ ƒ t j ˆ t d ƒ | ƒ } | ˆ | j <t  |
 ƒ } | d k röd } n  t j j! | | | ƒ } | j t ˆ | | | ƒ ƒ t j j" | | | g i  d	 | ƒ} t# j$ |	 | | ƒ ˆ  | <| j t ˆ |  | | ƒ ƒ xµ t% | ƒ D]§ } t j ˆ t d ƒ | ƒ } t j } | ˆ | j <| j t ˆ | t j | | ƒ | ƒ ƒ t j& |  | | | | ƒ } t# j$ t j' |	 | |
 ƒ ˆ  | <| j | ƒ qW| j( | ƒ t) S(   sâ   Check to see if the given "array_var" is created from a list
        of constants, and try to inline the list definition as array
        initialization.

        Extra statements produced with be appended to "stmts".
        i   Rî   i    Rí   s   inline array_var = s    list_var = R  R  R€   R3   R  R  R  s	   $np_g_varR+  s   $np_typ_vart   boolt   bool_Râ   (*   R   R   R   Rj   Ry   t   return_typeR8   R   t   ArrayCompatiblet   ndimR€   R  R   RG   R   R«   R	   t   intpt   UniTupleRI   R'  R­   R<   Rœ   t   DTypeR   R+  R  t   resolve_function_typeRx   t   misct   ModuleR¬   R	  R)   R   t	   signatureRè   Rñ   t   noneRW  R@   (    Ró   RV   RD  t	   list_varst   delst   callnamet   ret_typeR€   Rö   t	   array_typR  t   seqt   _R  RF  RJ  t   size_typt   size_tuple_typt   nptypeR  t   fntyt   sigt   g_np_vart   g_npt   typ_vart	   dtype_strt   np_typ_getattrt
   empty_callRS   RE  t	   index_typt   setitem(   RÅ   t   contextRP   R"   Rž   RÄ   (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   inline_array©  sp    *			%"	$	R
  R)   N(   R4   Rž   R0   R7   R8   R   R9   R;   R«   Ry   RI   R:   R<   R=   R3   R   R@   Rð   Rj  t   anyR  R“   (   RR   R"   R•  RÄ   RÅ   R–  R  t	   dead_varst
   list_itemsRD  R‚  RN   RZ  RV   Rš   t   arr_vart   removed_varR7   t   list_var_used(    (   RÅ   R•  RP   R"   Rž   RÄ   s7   lib/python2.7/site-packages/numba/inline_closurecall.pyRp  ž  sv    	^				.(F   R   t   pytypesR°   Rr   R    R   R   R   R   R   R   t   numba.parforR   t   numba.ir_utilsR	   R
   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   numba.analysisR   R   R   t   numba.targets.rangeobjR   t   numba.unsafe.ndarrayR    R  Rî   R+  R)  R@   RC   t   objectR!   Rl   R“   Rm   R4   RD   R¤   R»   R¶   R¾   R¿   Rú   R  R0   RJ   Rò   RL   R'  t   register_rewritet   RewriteRl  Rp  (    (    (    s7   lib/python2.7/site-packages/numba/inline_closurecall.pyt   <module>   s@   :|®		Œ			
						2	í	
	x	