ó
\K]c           @@ sw   d  Z  d d l m Z m Z d d l m Z d d l m Z d e f d „  ƒ  YZ d „  Z	 d Z
 d
 „  Z d „  Z d S(   sS   
Implement a rewrite pass on LLVM module to remove unnecessary refcount
operation.
i    (   t   absolute_importt   print_function(   t   CallVisitor(   t   typest   _MarkNrtCallVisitorc           B@ s    e  Z d  Z d „  Z d „  Z RS(   s7   
    A pass to mark all NRT_incref and NRT_decref.
    c         C@ s   t  ƒ  |  _ d  S(   N(   t   sett   marked(   t   self(    (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt   __init__   s    c         C@ s2   t  | j d d ƒ d k r. |  j j | ƒ n  d  S(   Nt   namet    t
   NRT_increft
   NRT_decref(   R   R   (   t   getattrt   calleeR   t   add(   R   t   instr(    (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt
   visit_Call   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyR      s   	c         C@ sp   t  ƒ  } | j |  ƒ | j } xJ |  j D]? } x6 t | j ƒ D]% } | | k r? | j j | ƒ q? q? Wq) Wd  S(   N(   R   t   visit_FunctionR   t   basic_blockst   listt   instructionst   remove(   t   functiont   markpassR   t   bbt   inst(    (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt   _rewrite_function   s    		R   R   c   
      @ sá   ‡  f d †  ‰ ‡ f d †  } | j  } | j } | j } x | D] } | | ƒ s@ t Sq@ Wˆ | ƒ sj t Sx4 | j ƒ  D]& } | d k	 rw ˆ | j ƒ rw t Sqw Wx9 |  j D]. }	 |	 j j	 d ƒ r« |	 j t
 k rÙ t Sq« q« Wt S(   s   
    Legalize the code in the module.
    Returns True if the module is legal for the rewrite pass that remove
    unnecessary refcount.
    c         @ s   ˆ  |  } | j  ƒ  S(   sG   
        Valid output are any type that does not need refcount
        (   t   contains_nrt_meminfo(   t   tyt   model(   t   dmm(    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt   valid_output.   s    
c         @ s   ˆ  |  ƒ p t  |  t j ƒ S(   sT   
        Valid input are any type that does not need refcount except Array.
        (   t
   isinstanceR   t   Array(   R    (   R#   (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt   valid_input5   s    t   NRT_N(   t   argtypest   restypet	   calltypest   Falset   valuest   Nonet   return_typet	   functionsR	   t
   startswitht   _accepted_nrtfnst   True(
   t   moduleR"   t   fndescR&   R(   R)   R*   t   argtyt   calltyt   fn(    (   R"   R#   s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt	   _legalize'   s$    			c         C@ s4   | j  } t |  j | | ƒ r, t |  ƒ t St Sd S(   sS  
    Remove unnecessary NRT incref/decref in the given LLVM function.
    It uses highlevel type info to determine if the function does not need NRT.
    Such a function does not:

    - return array object;
    - take arguments that need refcount except array;
    - call function that return refcounted object.

    In effect, the function will not capture or create references that extend
    the lifetime of any refcounted objects beyound the lifetime of the
    function.

    The rewrite performs inplace.
    If rewrite has happen, this function return True. Otherwise, return False.
    N(   t   data_model_managerR8   R3   R   R2   R+   (   R   t   contextR4   R"   (    (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt   remove_unnecessary_nrt_usageV   s
    	
N(   R   R   (   R   t
   __future__R    R   t   llvmlite.ir.transformsR   t   numbaR   R   R   R1   R8   R;   (    (    (    s<   lib/python2.7/site-packages/numba/targets/removerefctpass.pyt   <module>   s   		/