o
    4ήc	<                     @   s  d dl mZmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlZd dlmZ d dlmZ edd	ZG d
d deZG dd deZd ddZdd Zdd Zdd Zdd ZG dd de
ZG dd dejZG dd deZG dd deZejej j!_"ej#gej j$_%ejej j$_"ejgej j&_%ejej#gej j'_%eej j'_"ejej#eeegej j(_%eej j(_"ejgej j)_%eej j)_"ejgej j*_%eej j*_"ejej+gej j,_%eej j,_"ejej+eeegej j-_%eej j-_"ejgej j._%ejgej j/_%ejgej j0_%ejegej j1_%ejgej j2_%ejgej j3_%ejgej j4_%ejgej j5_%ejgej j6_%ejgej j7_%ejgej j8_%ejgej j9_%ejgej j:_%ejgej j;_%ejgej j<_%ejgej j=_%ejeegej j>_%dS )!    )c_boolc_char_pc_intc_size_t	Structurebyref)
namedtuple)IntFlag)ffiN)mkstemp)_encode_string
PruneStatsz&basicblock diamond fanout fanout_raisec                   @   s    e Zd ZdZdd Zdd ZdS )r   z4 Holds statistics from reference count pruning.
    c                 C   sN   t |tsd}t|t|t| j|j | j|j | j|j | j|j S )Nz;PruneStats can only be added to another PruneStats, got {}.	
isinstancer   	TypeErrorformattype
basicblockdiamondfanoutfanout_raiseselfothermsg r   D/tmp/pip-target-vg8gfxp4/lib/python/llvmlite/binding/passmanagers.py__add__   s   



zPruneStats.__add__c                 C   sN   t |tsd}t|t|t| j|j | j|j | j|j | j|j S )NzBPruneStats can only be subtracted from another PruneStats, got {}.r   r   r   r   r   __sub__   s   



zPruneStats.__sub__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    	c                   @   s(   e Zd ZdefdefdefdefgZdS )_c_PruneStatsr   r   r   r   N)r   r    r!   r   _fields_r   r   r   r   r#   %   s    r#   Fc                 C   s>   t dddd}t| }tjt|| t|j|j|j	|j
S )z Returns a namedtuple containing the current values for the refop pruning
    statistics. If kwarg `printout` is True the stats are printed to stderr,
    default is False.
    r   )r#   r   r
   libLLVMPY_DumpRefPruneStatsr   r   r   r   r   r   )printoutstatsdo_printr   r   r   dump_refprune_stats-   s   r*   c                 C   s   t jt|  dS )zEnable or disable the pass timers.

    Parameters
    ----------
    enable : bool
        Set to True to enable the pass timers.
        Set to False to disable the pass timers.
    N)r
   r%   LLVMPY_SetTimePassesr   )enabler   r   r   set_time_passes;   s   	r-   c                  C   s@   t  } t j|  t| W  d   S 1 sw   Y  dS )a  Returns the pass timings report and resets the LLVM internal timers.

    Pass timers are enabled by ``set_time_passes()``. If the timers are not
    enabled, this function will return an empty string.

    Returns
    -------
    res : str
        LLVM generated timing report.
    N)r
   OutputStringr%   LLVMPY_ReportAndResetTimingsstr)bufr   r   r   report_and_reset_timingsG   s   
$r2   c                   C   s   t  S N)ModulePassManagerr   r   r   r   create_module_pass_managerW   s   r5   c                 C   s   t | S r3   )FunctionPassManager)moduler   r   r   create_function_pass_manager[   s   r8   c                   @   s,   e Zd ZdZdZdZdZeeB eB eB ZdS )RefPruneSubpasses            N)r   r    r!   PER_BBDIAMONDFANOUTFANOUT_RAISEALLr   r   r   r   r9   _   s    r9   c                   @   s   e Zd 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dd Zd d! Zd"d# Zd$d% Zejd&fd'd(Zd)S )*PassManagerzPassManager
    c                 C   s   | j |  d S r3   )_capiLLVMPY_DisposePassManagerr   r   r   r   _disposek      zPassManager._disposec                 C      t j|  dS )zQSee http://llvm.org/docs/Passes.html#constmerge-merge-duplicate-global-constants.N)r
   r%   LLVMPY_AddConstantMergePassrF   r   r   r   add_constant_merge_passn      z#PassManager.add_constant_merge_passc                 C   rI   )zKSee http://llvm.org/docs/Passes.html#deadargelim-dead-argument-elimination.N)r
   r%    LLVMPY_AddDeadArgEliminationPassrF   r   r   r   add_dead_arg_elimination_passr   rL   z)PassManager.add_dead_arg_elimination_passc                 C   rI   )zNSee http://llvm.org/docs/Passes.html#functionattrs-deduce-function-attributes.N)r
   r%   LLVMPY_AddFunctionAttrsPassrF   r   r   r   add_function_attrs_passv   rL   z#PassManager.add_function_attrs_passc                 C   s   t j| | dS )zJSee http://llvm.org/docs/Passes.html#inline-function-integration-inlining.N)r
   r%   LLVMPY_AddFunctionInliningPass)r   	thresholdr   r   r   add_function_inlining_passz   s   z&PassManager.add_function_inlining_passc                 C   rI   )zGSee http://llvm.org/docs/Passes.html#globaldce-dead-global-elimination.N)r
   r%   LLVMPY_AddGlobalDCEPassrF   r   r   r   add_global_dce_pass~   rL   zPassManager.add_global_dce_passc                 C   rI   )zISee http://llvm.org/docs/Passes.html#globalopt-global-variable-optimizer.N)r
   r%   LLVMPY_AddGlobalOptimizerPassrF   r   r   r   add_global_optimizer_pass   rL   z%PassManager.add_global_optimizer_passc                 C   rI   )zdSee http://llvm.org/docs/Passes.html#ipsccp-interprocedural-sparse-conditional-constant-propagation.N)r
   r%   LLVMPY_AddIPSCCPPassrF   r   r   r   add_ipsccp_pass   rL   zPassManager.add_ipsccp_passc                 C   rI   )z?See http://llvm.org/docs/Passes.html#dce-dead-code-elimination.N)r
   r%   !LLVMPY_AddDeadCodeEliminationPassrF   r   r   r   add_dead_code_elimination_pass   rL   z*PassManager.add_dead_code_elimination_passc                 C   rI   )zBSee http://llvm.org/docs/Passes.html#simplifycfg-simplify-the-cfg.N)r
   r%   LLVMPY_AddCFGSimplificationPassrF   r   r   r   add_cfg_simplification_pass   rL   z'PassManager.add_cfg_simplification_passc                 C   rI   )z@See http://llvm.org/docs/Passes.html#gvn-global-value-numbering.N)r
   r%   LLVMPY_AddGVNPassrF   r   r   r   add_gvn_pass   rL   zPassManager.add_gvn_passc                 C   rI   )z8See http://llvm.org/docs/Passes.html#passes-instcombine.N)r
   r%   "LLVMPY_AddInstructionCombiningPassrF   r   r   r   add_instruction_combining_pass   rL   z*PassManager.add_instruction_combining_passc                 C   rI   )zESee http://llvm.org/docs/Passes.html#licm-loop-invariant-code-motion.N)r
   r%   LLVMPY_AddLICMPassrF   r   r   r   add_licm_pass   rL   zPassManager.add_licm_passc                 C   rI   )zRSee http://llvm.org/docs/Passes.html#sccp-sparse-conditional-constant-propagation.N)r
   r%   LLVMPY_AddSCCPPassrF   r   r   r   add_sccp_pass   rL   zPassManager.add_sccp_passc                 C   rI   )zSee http://llvm.org/docs/Passes.html#scalarrepl-scalar-replacement-of-aggregates-dt.
        Note that this pass corresponds to the ``opt -sroa`` command-line option,
        despite the link above.N)r
   r%   LLVMPY_AddSROAPassrF   r   r   r   add_sroa_pass   s   zPassManager.add_sroa_passc                 C   s   t j|  d S r3   )r
   r%   $LLVMPY_AddTypeBasedAliasAnalysisPassrF   r   r   r   "add_type_based_alias_analysis_pass   rH   z.PassManager.add_type_based_alias_analysis_passc                 C   rI   )z=See http://llvm.org/docs/AliasAnalysis.html#the-basicaa-pass.N)r
   r%    LLVMPY_AddBasicAliasAnalysisPassrF   r   r   r   add_basic_alias_analysis_pass   rL   z)PassManager.add_basic_alias_analysis_passc                 C   rI   )z:http://llvm.org/docs/Passes.html#loop-rotate-rotate-loops.N)r
   r%   LLVMPY_LLVMAddLoopRotatePassrF   r   r   r   add_loop_rotate_pass   rL   z PassManager.add_loop_rotate_passi  c                 C   s   t |}tj| || dS )a  Add Numba specific Reference count pruning pass.

        Parameters
        ----------
        subpasses_flags : RefPruneSubpasses
            A bitmask to control the subpasses to be enabled.
        subgraph_limit : int
            Limit the fanout pruners to working on a subgraph no bigger than
            this number of basic-blocks to avoid spending too much time in very
            large graphs. Default is 1000. Subject to change in future
            versions.
        N)r9   r
   r%   LLVMPY_AddRefPrunePass)r   subpasses_flagssubgraph_limitiflagsr   r   r   add_refprune_pass   s   zPassManager.add_refprune_passN)r   r    r!   r"   rG   rK   rN   rP   rS   rU   rW   rY   r[   r]   r_   ra   rc   re   rg   ri   rk   rm   r9   rB   rr   r   r   r   r   rC   g   s.    rC   c                   @   s2   e Zd Zd
ddZ		dddZ		ddd	ZdS )r4   Nc                 C   s"   |d u r	t j }t| | d S r3   )r
   r%   LLVMPY_CreatePassManagerrC   __init__)r   ptrr   r   r   rt      s   
zModulePassManager.__init__yaml c                 C   sN   |du rt j| |S t j| |t|t|t|}|dkr#td|dkS )a  
        Run optimization passes on the given module.

        Parameters
        ----------
        module : llvmlite.binding.ModuleRef
            The module to be optimized inplace
        remarks_file : str; optional
            If not `None`, it is the file to store the optimization remarks.
        remarks_format : str; optional
            The format to write; YAML is default
        remarks_filter : str; optional
            The filter that should be applied to the remarks output.
        N"Failed to initialize remarks file.r   )r
   r%   LLVMPY_RunPassManager LLVMPY_RunPassManagerWithRemarksr   IOError)r   r7   remarks_fileremarks_formatremarks_filterrr   r   r   run      
zModulePassManager.runc              	   C      t  \}}zPt|d W d   n1 sw   Y  | ||||}|dkr,tdt|}t|| fW  d   W t| S 1 sIw   Y  W t| dS t| w )a  
        Run optimization passes on the given module and returns the result and
        the remarks data.

        Parameters
        ----------
        module : llvmlite.binding.ModuleRef
            The module to be optimized
        remarks_format : str
            The remarks output; YAML is the default
        remarks_filter : str; optional
            The filter that should be applied to the remarks output.
        r   Nrx   ry   	r   osfdopenr   r|   openboolreadunlink)r   r7   r~   r   
remarkdesc
remarkfiler   fr   r   r   run_with_remarks   s   

z"ModulePassManager.run_with_remarksr3   Nrv   rw   rv   rw   )r   r    r!   rt   r   r   r   r   r   r   r4      s    

r4   c                   @   s@   e Zd Zdd Zdd Zdd Z			dd
dZ			dddZdS )r6   c                 C   s(   t j|}|| _d|_t| | d S )NT)r
   r%    LLVMPY_CreateFunctionPassManager_module_ownedrC   rt   )r   r7   ru   r   r   r   rt     s   zFunctionPassManager.__init__c                 C      t j| S )zk
        Initialize the FunctionPassManager.  Returns True if it produced
        any changes (?).
        )r
   r%   $LLVMPY_InitializeFunctionPassManagerrF   r   r   r   
initialize     zFunctionPassManager.initializec                 C   r   )zi
        Finalize the FunctionPassManager.  Returns True if it produced
        any changes (?).
        )r
   r%   "LLVMPY_FinalizeFunctionPassManagerrF   r   r   r   finalize  r   zFunctionPassManager.finalizeNrv   rw   c                 C   sN   |du rt j| |S t j| |t|t|t|}|dkr#tdt|S )a  
        Run optimization passes on the given function.

        Parameters
        ----------
        function : llvmlite.binding.FunctionRef
            The function to be optimized inplace
        remarks_file : str; optional
            If not `None`, it is the file to store the optimization remarks.
        remarks_format : str; optional
            The format of the remarks file; the default is YAML
        remarks_filter : str; optional
            The filter that should be applied to the remarks output.
        Nrx   ry   )r
   r%   LLVMPY_RunFunctionPassManager(LLVMPY_RunFunctionPassManagerWithRemarksr   r|   r   )r   functionr}   r~   r   r   r   r   r   r     r   zFunctionPassManager.runc              	   C   r   )a  
        Run optimization passes on the given function and returns the result
        and the remarks data.

        Parameters
        ----------
        function : llvmlite.binding.FunctionRef
            The function to be optimized inplace
        remarks_format : str; optional
            The format of the remarks file; the default is YAML
        remarks_filter : str; optional
            The filter that should be applied to the remarks output.
        r   Nrx   ry   r   )r   r   r~   r   r   r   r   r   r   r   r   r   6  s   

z$FunctionPassManager.run_with_remarksr   r   )r   r    r!   rt   r   r   r   r   r   r   r   r   r6     s    
r6   )F)?ctypesr   r   r   r   r   r   collectionsr   enumr	   llvmlite.bindingr
   r   tempfiler   llvmlite.binding.commonr   _prunestatsr   r#   r*   r-   r2   r5   r8   r9   	ObjectRefrC   r4   r6   LLVMPassManagerRefr%   rs   restypeLLVMModuleRefr   argtypesrE   rz   r{   r   r   LLVMValueRefr   r   rJ   rM   rO   rQ   rT   rV   rX   rZ   r\   r^   r`   rb   rd   rf   rh   rj   rn   r   r   r   r   <module>   s     
`>T










