U
    ,‰d_$  ã                   @   sd   d dl mZmZmZmZmZ G dd„ deƒZG dd„ deƒZG dd„ deƒZ	i Z
G dd	„ d	eƒZd
S )é    )ÚutilsÚirÚanalysisÚ
transformsÚir_utilsc                   @   s   e Zd Zdd„ ZdS )Ú
YieldPointc                 C   s<   t |tjƒst‚t |tjƒs t‚|| _|| _d | _d | _d S ©N)	Ú
isinstancer   ZBlockÚAssertionErrorÚYieldÚblockÚinstÚ	live_varsÚweak_live_vars)Úselfr   r   © r   ú7/tmp/pip-unpacked-wheel-eu7e0c37/numba/core/postproc.pyÚ__init__   s    zYieldPoint.__init__N)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚGeneratorInfoc                 C   s   i | _ g | _d S r   )Úyield_pointsÚ
state_vars©r   r   r   r   r      s    zGeneratorInfo.__init__c                 C   s
   | j  ¡ S )z=
        Return an iterable of YieldPoint instances.
        )r   Úvaluesr   r   r   r   Úget_yield_points   s    zGeneratorInfo.get_yield_pointsN)r   r   r   r   r   r   r   r   r   r      s   r   c                   @   sP   e Zd ZdZdd„ Zejdd„ ƒZejdd„ ƒZejdd	„ ƒZ	ejd
d„ ƒZ
dS )ÚVariableLifetimez>
    For lazily building information of variable lifetime
    c                 C   s
   || _ d S r   )Ú_blocks)r   Úblocksr   r   r   r   "   s    zVariableLifetime.__init__c                 C   s   t  | j¡S r   )r   Zcompute_cfg_from_blocksr   r   r   r   r   Úcfg%   s    zVariableLifetime.cfgc                 C   s   t  | j¡S r   )r   Zcompute_use_defsr   r   r   r   r   Úusedefs)   s    zVariableLifetime.usedefsc                 C   s   t  | j| j| jj| jj¡S r   )r   Zcompute_live_mapr    r   r!   ZusemapÚdefmapr   r   r   r   Úlivemap-   s    þzVariableLifetime.livemapc                 C   s   t  | j| j| j| jj¡S r   )r   Zcompute_dead_mapsr    r   r#   r!   r"   r   r   r   r   Údeadmaps3   s    ÿzVariableLifetime.deadmapsN)r   r   r   Ú__doc__r   r   Úcached_propertyr    r!   r#   r$   r   r   r   r   r      s   


r   c                   @   sV   e Zd ZdZdd„ Zdeedœdd„Zdd	„ Zd
d„ Zddd„Z	ddd„Z
dd„ ZdS )ÚPostProcessorz(
    A post-processor for Numba IR.
    c                 C   s
   || _ d S r   )Úfunc_ir)r   r(   r   r   r   r   C   s    zPostProcessor.__init__F)Ú	emit_delsÚextend_lifetimesc                 C   s¢   t  | jj¡| j_t| jjƒ}|| j_t |j| jj|j	j
|jj¡}| jj ¡ D ]\}}|| | jj|< qP| jjr†tƒ | j_|  ¡  nd| j_|rž| j|d dS )a   
        Run the following passes over Numba IR:
        - canonicalize the CFG
        - emit explicit `del` instructions for variables
        - compute lifetime of variables
        - compute generator info (if function is a generator function)
        N©r*   )r   Zcanonicalize_cfgr(   r   r   Úvariable_lifetimer   Zcompute_live_variablesr    r!   r"   r$   ZcombinedÚitemsZblock_entry_varsZis_generatorr   Úgenerator_infoÚ_compute_generator_infoÚ_insert_var_dels)r   r)   r*   ÚvltZbevÚoffsetÚir_blockr   r   r   ÚrunF   s    þ

zPostProcessor.runc                 C   s|   | j jj}|rtdƒ‚| j j ¡ D ]T}|jD ]H}t|tj	ƒr,|j
}t|tjƒr,t|ƒd }||_t||ƒ}|||j< q,q"dS )zP
        Fill `index` for the Yield instruction and create YieldPoints.
        z"rerunning _populate_generator_infoé   N)r(   r.   r   r
   r   r   Úbodyr	   r   ÚAssignÚvaluer   ÚlenÚindexr   )r   Údctr   r   Z	yieldinstr:   Úypr   r   r   Ú_populate_generator_infoc   s    


z&PostProcessor._populate_generator_infoc           	      C   s,  |   ¡  |  ¡  | jj}| ¡ D ]Ì}t| j |j¡ƒ}tƒ }t|jj	ƒ}|D ]H}t
|tjƒr||j|jkrl q¢| |jj¡ qLt
|tjƒrL| |j¡ qLds¢tdƒ‚|D ]8}t
|tjƒrÚ|j}||krÞ| |¡ | |¡ q¦ qàq¦||_||_q tƒ }| ¡ D ]}||jO }||jO }qüt|ƒ|_|  ¡  dS )zu
        Compute the generator's state variables as the union of live variables
        at all yield points.
        r   zcouldn't find yield pointN)r0   r=   r(   r.   r   ÚsetZget_block_entry_varsr   Úiterr6   r	   r   r7   r8   r   ÚaddÚtargetÚnameÚDelÚremover
   r   r   Úsortedr   Úremove_dels)	r   Zgir<   r   r   ZstmtsÚstmtrB   Ústr   r   r   r/   s   s<    


z%PostProcessor._compute_generator_infoc                 C   s$   | j j}| j|jj|jj|d dS )a®  
        Insert del statements for each variable.
        Returns a 2-tuple of (variable definition map, variable deletion map)
        which indicates variables defined and deleted in each block.

        The algorithm avoids relying on explicit knowledge on loops and
        distinguish between variables that are defined locally vs variables that
        come from incoming blocks.
        We start with simple usage (variable reference) and definition (variable
        creation) maps on each block. Propagate the liveness info to predecessor
        blocks until it stabilize, at which point we know which variables must
        exist before entering each block. Then, we compute the end of variable
        lives and insert del statements accordingly. Variables are deleted after
        the last use. Variable referenced by terminators (e.g. conditional
        branch and return) are deleted by the successors or the caller.
        r+   N)r(   r,   Ú_patch_var_delsr$   ZinternalZescaping)r   r*   r1   r   r   r   r0      s    ÿzPostProcessor._insert_var_delsc                 C   s”  | j j ¡ D ]€\}}||  ¡ }g }t|jdd… ƒD ]n}tdd„ | ¡ D ƒƒ}	|	|@ }
t ¡ D ],\}}t	||ƒrb|||
ƒ}|
|8 }
||8 }qb| 
||
f¡ ||
8 }q8g }|j}g }t|ƒD ]t\}}|rØ|jd j}n|j}t	|tjƒsô| 
|¡ t|ddD ]0}tj||d}|r$| 
|¡ n
| 
|¡ q q¾|rD| |¡ | 
|jd ¡ ||_|| }t|ƒD ]"}| tj||jd jd¡ qjqdS )	z-
        Insert delete in each block
        Néÿÿÿÿc                 s   s   | ]}|j V  qd S r   )rB   )Ú.0Úvr   r   r   Ú	<genexpr>¾   s     z0PostProcessor._patch_var_dels.<locals>.<genexpr>T)Úreverse)Úlocr   )r(   r   r-   ÚcopyÚreversedr6   r>   Z	list_varsÚir_extension_insert_delsr	   ÚappendrO   r   rC   rE   ÚextendÚprepend)r   Zinternal_dead_mapZescaping_dead_mapr*   r2   r3   Zinternal_dead_setZ
delete_ptsrG   Zlive_setZdead_setÚTZdef_funcZ	done_delsr6   ZlastlocZ	del_storeZ
delete_setÚvar_nameZdelnodeZescape_dead_setr   r   r   rI   ²   sD    





zPostProcessor._patch_var_delsc                 C   s   t  | jj¡ dS )z,
        Strips the IR of Del nodes
        N)r   rF   r(   r   r   r   r   r   rF   ê   s    zPostProcessor.remove_delsN)FF)F)F)r   r   r   r%   r   Úboolr4   r=   r/   r0   rI   rF   r   r   r   r   r'   >   s   *
 ÿ
8r'   N)Z
numba.corer   r   r   r   r   Úobjectr   r   r   rR   r'   r   r   r   r   Ú<module>   s
   