o
    i!                     @   s   d Z zddlmZ W n	 ey   Y nw 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rle  d(S d(S ))zc
Contains tests and a prototype implementation for the fanout algorithm in
the LLVM refprune pass.
    )Digraph)defaultdictAc                  C   s   i ddgdddgdg dddgddgdg ddd	gd	ddgdd
dgd
ddgdg ddgdg dddgddgdg } t t}dg|d< dg|d< ddg|d< dddhi}|| |fS )Nr   BCDEFGHIJKLMZ)r   OPr   r   increfdecrefr   listedgesnodesexpected r   l/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/llvmlite/tests/refprune_proto.pycase1   sN   	



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   ,   t  \} }}|d d dd i}| ||fS )Nr   r	   r   r   appendr   r   _r   r   r   r   case3@      
r&   c                  C   r!   )Nr   r   r   r"   r$   r   r   r   case4H   r'   r(   c                  C   r!   )Nr   r   r   r"   r$   r   r   r   case5P   r'   r)   c                  C   r!   )Nr   r   r   r"   r$   r   r   r   case6X   r'   r*   c                  C   r!   )Nr   r   r   r"   r$   r   r   r   case7`   r'   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   r!   )Nr   r   r   r,   r#   r$   r   r   r   case9u   r'   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
   

r1   c                  C   s8   t  \} }}|d d dg|d< ddhi}| ||fS r0   r-   r$   r   r   r   case12   s
   


r2   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
   

r3   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   r;   c                   @   sj   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   s0   || _ || _t|| _|rt| _d S | j| _d S r4   )r   r   r;   	rev_edgesprint_null_print)selfr   r   verboser   r   r   __init__   s   
zFanoutAlgorithm.__init__c                 C   s   |   S r4   )find_fanout_in_function)r@   r   r   r   run   s   zFanoutAlgorithm.runc                 O   s   d S r4   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   rC      s   


z'FanoutAlgorithm.find_fanout_in_functionc                 C   s:   |  |}| d| |sd S | j||tdsd S t|S )N
candidates)entry)find_decref_candidatesr>   verify_non_overlappingENTRYr5   )r@   	head_noderM   r   r   r   rJ      s   
zFanoutAlgorithm.find_fanoutc           	      C   s   |  ddd t|}|rm| }t }|g}~|rk| }|  d|d| ||v r-q||kr9|  d| dS || |  d| d	| |  | |D ]}||v r_|  d
  dS ||krh|| qQ|s|sdS )NrQ   P   -rL   |z%!! failed because we arrived at entryFz   z preds z.!! reject because predecessor in decref_blocksT)r>   centerr   popr5   r7   get_predecessorsr#   )	r@   rS   rM   rO   todorL   visitedZ	workstackpredr   r   r   rQ      sB   

!z&FanoutAlgorithm.verify_non_overlappingc                 C      t | j| S r4   )tupler   r@   noder   r   r   get_successors      zFanoutAlgorithm.get_successorsc                 C   r]   r4   )r^   r=   r_   r   r   r   rY      rb   z FanoutAlgorithm.get_predecessorsc                 C   s   d| j | v S )Nr   )r   r_   r   r   r   
has_decref   rb   zFanoutAlgorithm.has_decref
   c                 C   s   dt | }| |d|| |dkrdS ||v r"||d kr dS dS | |r4|| | |d dS |d8 }||f7 }d}| |D ]}| |||sQd} nd}qD| |d|  |S )	N walkr   FTzfound decref   zret )lenr>   rc   r7   ra   walk_child_for_decref)r@   rL   
path_stackrM   depthindentfoundchildr   r   r   ri      s0   


z%FanoutAlgorithm.walk_child_for_decrefc                 C   sZ   |  ddd |f}d}t }| |D ]}| |||s#d} nd}q|s+t S |S )NrP   rT   rU   FT)r>   rW   r5   ra   ri   )r@   rL   rj   rm   rM   rn   r   r   r   rP     s   z&FanoutAlgorithm.find_decref_candidatesN)F)rd   )__name__
__module____qualname__rB   rD   r?   rC   rJ   rQ   ra   rY   rc   ri   rP   r   r   r   r   r<      s    
	(
r<   c            	   	   C   s   t  \} }}t }|D ]}|j|d| dd| |  d q| D ]\}}|D ]}||| q*q$|  t| |dd}| }||ksIJ d S )Nrect
z\l)shapelabelT)rA   )	r3   r   r`   joinr6   edgeviewr<   rD   )	r   r   r   r
   r`   childrenrn   algorK   r   r   r   
check_once*  s   &r{   c                  C   sj   t t  D ]'\} }| dr.t| dd | \}}}t||}| }||ks.J qtd d S )NcaserT   rU   z
ALL PASSED)r   globalsr6   
startswithr>   rW   r<   rD   )kfnr   r   r   rz   rK   r   r   r   	check_all<  s   

r   __main__N)__doc__graphvizr   ImportErrorcollectionsr   rR   r   r    r&   r(   r)   r*   r+   r,   r.   r/   r1   r2   r3   r;   r<   r{   r   ro   r   r   r   r   <module>   s:    
 
