a
    –§»aå!  ã                   @   sØ   d Z zddlmZ W n ey&   Y n0 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g d¢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)r   Ú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   úW/home/yuhw/wc/pochoir/venv/lib/python3.9/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>µ   ó    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_noderL   r   r   r   rI   »   s    
ÿzFanoutAlgorithm.find_fanoutc           	      C   sÚ   |   d dd¡¡ t|ƒ}|rÖ| ¡ }tƒ }|g}~|r| ¡ }|   d|d|¡ ||v rZq4||krr|   d|¡ dS | |¡ |   d|› d	|  |¡› �¡ |  |¡D ].}||v r¾|   d
¡  dS ||kr¢| |¡ q¢q4qdS )NrP   éP   ú-rK   ú|z%!! failed because we arrived at entryFz   z preds z.!! reject because predecessor in decref_blocksT)r<   Úcenterr   Úpopr3   r5   Úget_predecessorsr"   )	r>   rR   rL   rN   ÚtodorK   ÚvisitedZ	workstackÚpredr   r   r   rP   Æ   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   rX   ñ   s    z FanoutAlgorithm.get_predecessorsc                 C   s   d| j | v 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 ||v 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>   rK   Ú
path_stackrL   ÚdepthÚindentÚfoundÚchildr   r   r   rf   ÷   s0    


ÿz%FanoutAlgorithm.walk_child_for_decrefc                 C   s`   |   d dd¡¡ |f}d}tƒ }|  |¡D ] }|  |||¡sHd} qNq,d}q,|sXtƒ S |S d S )NrO   rS   rT   FT)r<   rV   r3   r_   rf   )r>   rK   rg   rj   rL   rk   r   r   r   rO     s    ÿz&FanoutAlgorithm.find_decref_candidatesN)F)ra   )Ú__name__Ú
__module__Ú__qualname__r@   rB   r=   rA   rI   rP   r_   rX   r`   rf   rO   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’J ‚d S )NZrectÚ
z\l)ÚshapeÚlabelT)r?   )	r1   r   r^   Újoinr4   ZedgeÚviewr:   rB   )	r   r   r   r
   r^   Úchildrenrk   ÚalgorJ   r   r   r   Ú
check_once*  s    &rv   c                  C   sj   t tƒ  ¡ ƒD ]N\} }|  d¡rt|›  dd¡ƒ |ƒ \}}}t||ƒ}| ¡ }||ksJ ‚qtdƒ d S )NZcaserS   rT   z
ALL PASSED)r   Úglobalsr4   Ú
startswithr<   rV   r:   rB   )ÚkÚfnr   r   r   ru   rJ   r   r   r   Ú	check_all<  s    

r{   Ú__main__N)Ú__doc__Zgraphvizr   ÚImportErrorÚcollectionsr   rQ   r   r    r%   r&   r'   r(   r)   r*   r,   r-   r/   r0   r1   r9   r:   rv   r{   rl   r   r   r   r   Ú<module>   s4   
 