U
    ºmœdå!  ã                   @   sÚ   d Z zddlmZ W n ek
r(   Y nX ddlmZ dZdd„ Zdd„ Zd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ ZG d!d"„ d"ƒZd#d$„ Zd%d&„ Zed'krÖeƒ  d(S ))zc
Contains tests and a prototype implementation for the fanout algorithm in
the LLVM refprune pass.
é    )ÚDigraph)ÚdefaultdictÚAc                  C   sŒ   dgddgg ddgdgg ddgddgd	d
gddgg dgdddgdgdgg dœ} t tƒ}dg|d< dg|d< ddg|d< dddhi}|| |fS )NÚBÚCÚDÚEÚFÚGÚHÚIÚJÚKÚLÚMÚZÚOÚP)r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   ÚincrefÚdecref©r   Úlist©ÚedgesÚnodesÚexpected© r   úV/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/llvmlite/tests/refprune_proto.pyÚcase1   s.    ð

r   c                  C   sJ   ddgdgg dœ} t tƒ}dg|d< dg|d< dg|d< dd i}|| |fS ©Nr   r   )r   r   r   r   r   r   r   r   r   r   r   Úcase22   s    ý


r    c                  C   s,   t ƒ \} }}|d  d¡ dd i}| ||fS )Nr   r	   r   ©r   Úappend©r   r   Ú_r   r   r   r   Úcase3@   s    r%   c                  C   s,   t ƒ \} }}|d  d¡ dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   Úcase4H   s    r&   c                  C   s,   t ƒ \} }}|d  d¡ dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   Úcase5P   s    r'   c                  C   s,   t ƒ \} }}|d  d¡ dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   Úcase6X   s    r(   c                  C   s,   t ƒ \} }}|d  d¡ dd i}| ||fS )Nr   r   r   r!   r#   r   r   r   Úcase7`   s    r)   c                  C   sB   ddgdgg dœ} t tƒ}dg|d< dg|d< ddhi}|| |fS r   r   r   r   r   r   Úcase8h   s    ý


r*   c                  C   s,   t ƒ \} }}|d  d¡ dd i}| ||fS )Nr   r   r   ©r*   r"   r#   r   r   r   Úcase9u   s    r,   c                  C   s.   t ƒ \} }}|d  d¡ ddhi}| ||fS )Nr   r   r+   r#   r   r   r   Úcase10}   s    
r-   c                  C   s6   t ƒ \} }}|d  d¡ g |d< ddhi}| ||fS ©Nr   r   r   r+   r#   r   r   r   Úcase11…   s
    
r/   c                  C   s8   t ƒ \} }}|d  d¡ dg|d< ddhi}| ||fS r.   r+   r#   r   r   r   Úcase12�   s
    

r0   c                  C   s6   t ƒ \} }}|d  d¡ dg|d< dd i}| ||fS )Nr   r   r   r   r+   r#   r   r   r   Úcase13•   s
    
r1   c                 C   s6   t tƒ}|  ¡ D ] \}}|D ]}||  |¡ qq|S ©N)r   ÚsetÚitemsÚadd)r   ÚdÚsrcZ	outgoingsÚdstr   r   r   Úmake_predecessor_map�   s
    r9   c                   @   sh   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zddd„Zdd„ ZdS )ÚFanoutAlgorithmFc                 C   s*   || _ || _t|ƒ| _|rtn| j| _d S r2   )r   r   r9   Ú	rev_edgesÚprintÚ_null_print)Úselfr   r   Úverboser   r   r   Ú__init__¦   s    
zFanoutAlgorithm.__init__c                 C   s   |   ¡ S r2   )Úfind_fanout_in_function)r>   r   r   r   Úrun¬   s    zFanoutAlgorithm.runc                 O   s   d S r2   r   )r>   ÚargsÚkwargsr   r   r   r=   ¯   s    zFanoutAlgorithm._null_printc                 C   sP   i }| j D ]@}dd„ | j| D ƒD ]&}|  |¡}|  d|d|¡ |||< q"q
|S )Nc                 s   s   | ]}|d kr|V  qdS )r   Nr   )Ú.0Úxr   r   r   Ú	<genexpr>µ   s      z:FanoutAlgorithm.find_fanout_in_function.<locals>.<genexpr>z>>z===)r   r   Úfind_fanoutr<   )r>   ÚgotÚcur_noder   Údecref_blocksr   r   r   rA   ²   s    

z'FanoutAlgorithm.find_fanout_in_functionc                 C   s:   |   |¡}|  d|¡ |sd S | j||td�s2d S t|ƒS )NÚ
candidates)Úentry)Úfind_decref_candidatesr<   Úverify_non_overlappingÚENTRYr3   )r>   Ú	head_noderK   r   r   r   rH   »   s    
  ÿzFanoutAlgorithm.find_fanoutc           	      C   sÚ   |   d dd¡¡ t|ƒ}|rÖ| ¡ }tƒ }|g}~|r| ¡ }|   d|d|¡ ||krZq4||krr|   d|¡ dS | |¡ |   d|› d	|  |¡› �¡ |  |¡D ].}||kr¾|   d
¡  dS ||kr¢| |¡ q¢q4qdS )NrO   éP   ú-rJ   ú|z%!! failed because we arrived at entryFz   z preds z.!! reject because predecessor in decref_blocksT)r<   Úcenterr   Úpopr3   r5   Úget_predecessorsr"   )	r>   rQ   rK   rM   ÚtodorJ   ÚvisitedZ	workstackÚpredr   r   r   rO   Æ   s>     ÿ
ÿÿz&FanoutAlgorithm.verify_non_overlappingc                 C   s   t | j| ƒS r2   )Útupler   ©r>   Únoder   r   r   Úget_successorsî   s    zFanoutAlgorithm.get_successorsc                 C   s   t | j| ƒS r2   )r[   r;   r\   r   r   r   rW   ñ   s    z FanoutAlgorithm.get_predecessorsc                 C   s   d| j | kS )Nr   )r   r\   r   r   r   Ú
has_decrefô   s    zFanoutAlgorithm.has_decrefé
   c                 C   sÀ   dt |ƒ }|  |d||¡ |dkr(dS ||krD||d kr@dS dS |  |¡rh| |¡ |  |d¡ dS |d8 }||f7 }d}|  |¡D ] }|  |||¡s¤d} qªqˆd}qˆ|  |d|› �¡ |S )	Nú Úwalkr   FTzfound decrefé   zret )Úlenr<   r_   r5   r^   Úwalk_child_for_decref)r>   rJ   Ú
path_stackrK   ÚdepthÚindentÚfoundÚchildr   r   r   re   ÷   s4    


  ÿz%FanoutAlgorithm.walk_child_for_decrefc                 C   s`   |   d dd¡¡ |f}d}tƒ }|  |¡D ] }|  |||¡sHd} qNq,d}q,|sXtƒ S |S d S )NrN   rR   rS   FT)r<   rU   r3   r^   re   )r>   rJ   rf   ri   rK   rj   r   r   r   rN     s       ÿz&FanoutAlgorithm.find_decref_candidatesN)F)r`   )Ú__name__Ú
__module__Ú__qualname__r@   rB   r=   rA   rH   rO   r^   rW   r_   re   rN   r   r   r   r   r:   ¥   s   
	( ÿ
r:   c            	   	   C   s–   t ƒ \} }}tƒ }|D ](}|j|d|› d�d | | ¡ d� q| ¡ D ]\}}|D ]}| ||¡ qTqH| ¡  t| |dd�}| ¡ }||ks’t	‚d S )NÚrectÚ
z\l)ÚshapeÚlabelT)r?   )
r1   r   r]   Újoinr4   ÚedgeÚviewr:   rB   ÚAssertionError)	r   r   r   r
   r]   Úchildrenrj   ÚalgorI   r   r   r   Ú
check_once*  s    &rx   c                  C   sj   t tƒ  ¡ ƒD ]N\} }|  d¡rt|›  dd¡ƒ |ƒ \}}}t||ƒ}| ¡ }||kst‚qtdƒ d S )NÚcaserR   rS   z
ALL PASSED)	r   Úglobalsr4   Ú
startswithr<   rU   r:   rB   ru   )ÚkÚfnr   r   r   rw   rI   r   r   r   Ú	check_all<  s    

r~   Ú__main__N)Ú__doc__Úgraphvizr   ÚImportErrorÚcollectionsr   rP   r   r    r%   r&   r'   r(   r)   r*   r,   r-   r/   r0   r1   r9   r:   rx   r~   rk   r   r   r   r   Ú<module>   s4   
 