ó
\K]c           @   s‹   d  Z  d d l Z d d l m Z m Z d d l m Z d d l m	 Z	 e j
 d ƒ Z e j
 d ƒ Z e j
 d ƒ Z d	 „  Z d
 „  Z d S(   s   
NRT specific optimizations
iÿÿÿÿN(   t   defaultdictt   deque(   t   binding(   t   cgutilss,   \s*(?:tail)?\s*call void @NRT_incref\((.*)\)s,   \s*(?:tail)?\s*call void @NRT_decref\((.*)\)s@   ([\'"]?[-a-zA-Z$._][-a-zA-Z$._0-9]*[\'"]?:)|^define|^;\s*<label>c            s¦   d „  } ‡ ‡ f d †  } d „  ‰ ‡ ‡ f d †  ‰ d „  ‰  ‡  f d †  ‰ d „  ‰ g  } x9 | |  ƒ D]+ \ } } | r‹ | | ƒ } n  | | 7} qj Wd j  | ƒ S(	   Nc         s   s³   g  } x¦ t  |  ƒ j ƒ  D]’ } | j d ƒ rK | s; t ‚ | j | ƒ q | j d ƒ r‡ | sf t ‚ | j | ƒ t | f Vg  } q | r | j | ƒ q t | g f Vq Wd  S(   Nt   definet   }(   t   strt
   splitlinest
   startswitht   AssertionErrort   appendt   Truet   False(   t   modulet   curt   line(    (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyt   _extract_functions   s    	c            sL   g  } x? ˆ  |  ƒ D]1 \ } } | r: | r: ˆ | ƒ } n  | | 7} q W| S(   N(    (   t
   func_linest   outt   is_bbt   bb_lines(   t   _extract_basic_blockst   _process_basic_block(    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyt   _process_function$   s    c         s   sÖ   |  d j  d ƒ s t ‚ |  d j  d ƒ s2 t ‚ t |  d g f Vg  } xh |  d d !D]Y } t j | ƒ } | d  k	 r› t | f Vg  } t | g f VqX | rX | j | ƒ qX qX Wt | f Vt |  d g f Vd  S(   Ni    R   iÿÿÿÿR   i   (   R   R	   R   t	   _regex_bbt   matcht   NoneR   R
   (   R   R   t   lnt   m(    (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyR   ,   s    c            s   ˆ  |  ƒ }  ˆ |  ƒ }  |  S(   N(    (   R   (   t(   _move_and_group_decref_after_all_increfst   _prune_redundant_refct_ops(    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyR   ?   s    c         s   sŸ   x˜ t  |  ƒ D]Š \ } } t j | ƒ } | d  k	 rQ | | j d ƒ d  f Vq n  t j | ƒ } | d  k	 r‰ | d  | j d ƒ f Vq n  | d  d  f Vq Wd  S(   Ni   (   t	   enumeratet   _regex_increfR   R   t   groupt   _regex_decref(   R   t   numR   R   (    (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyt   _examine_refct_opD   s    c            sn  t  t ƒ } t  t ƒ } t ƒ  } xœ ˆ  |  ƒ D]Ž \ } } } | oF | sP t ‚ | r† | d k rr | j | ƒ q¼ | | j | ƒ q. | r. | d k r¨ | j | ƒ q¼ | | j | ƒ q. q. Wx| | j ƒ  D]n \ } } | | }	 t t |	 ƒ t | ƒ ƒ }
 x: t	 |
 ƒ D], } | j |	 j
 ƒ  ƒ | j | j ƒ  ƒ qWqÍ Wg  t |  ƒ D] \ } } | | k rL| ^ qLS(   Ns   i8* null(   R    R   t   setR	   t   addR
   t   itemst   mint   lent   ranget   popt   popleftR   (   R   t
   incref_mapt
   decref_mapt	   to_removeR#   t
   incref_vart
   decref_vart   vart   decopst   incopst   ctt   _R   (   R$   (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyR   R   s*    	
c         S   sü   d } x< t  |  ƒ D]. \ } } t j | ƒ d  k	 r | d } q q Wd } x< t  |  ƒ D]. \ } } t j | ƒ d  k	 rX | d } qX qX Wt | | ƒ } g  } g  } xD |  |  D]8 } t j | ƒ d  k	 rÛ | j | ƒ q° | j | ƒ q° W| | |  | S(   Ni    i   (   R   R    R   R   R"   t   maxR
   (   R   t   last_incref_post   posR   t   last_decref_post   last_post   decrefst   head(    (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyR   m   s     s   
(   t   join(   t   llvmirR   R   t	   processedt   is_funct   lines(    (   R$   R   R   R   R   s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyt   _remove_redundant_nrt_refct   s    				c         C   sf   y |  j  d ƒ Wn t k
 r% |  SX|  j } t t |  ƒ ƒ } t j | ƒ } t j | ƒ | _ | S(   s¼  
    Remove redundant reference count operations from the
    `llvmlite.binding.ModuleRef`. This parses the ll_module as a string and
    line by line to remove the unnecessary nrt refct pairs within each block.
    Decref calls are moved after the last incref call in the block to avoid
    temporarily decref'ing to zero (which can happen due to hidden decref from
    alias).

    Note: non-threadsafe due to usage of global LLVMcontext
    t
   NRT_incref(	   t   get_functiont	   NameErrort   nameRC   R   t   llt   parse_assemblyR   t   normalize_ir_text(   t	   ll_moduleRG   t   newllt   new_mod(    (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyt   remove_redundant_nrt_refct”   s    	(   t   __doc__t   ret   collectionsR    R   t   llvmliteR   RH   t   numbaR   t   compileR    R"   R   RC   RN   (    (    (    s3   lib/python2.7/site-packages/numba/runtime/nrtopt.pyt   <module>   s   	†