nullprop.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 499 行 · 第 1/2 页

C
499
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Propagate null pointer comparisons.
*
****************************************************************************/


#include <assert.h>
#include "standard.h"
#include "coderep.h"
#include "conflict.h"
#include "sysmacro.h"
#include "opcodes.h"
#include "cgdefs.h"
#include "model.h"

extern  bool            ReDefinedBy( instruction *, name * );
extern  name            *AllocS32Const( signed_32 );
extern void             KillCondBlk( block *blk, instruction *ins, int dest );
extern  bool            SideEffect( instruction * );
extern  pointer         SafeRecurse( pointer (* rtn)( pointer ), pointer arg );
extern  bool            BlockTrim( void );

extern  block           *HeadBlock;

extern  void            ClearBlockBits( block_class mask )
/********************************************************/
{
    block               *blk;

    mask = ~mask;
    for( blk = HeadBlock; blk != NULL; blk = blk->next_block ) {
        blk->class &= mask;
    }
}

static  instruction     *CompareIns( block *blk )
/***********************************************/
{
    instruction         *last;

    if( blk->class & CONDITIONAL ) {
        last = blk->ins.hd.prev;
        while( last->head.opcode != OP_BLOCK ) {
            if( _OpIsCompare( last->head.opcode ) ) return( last );
            last = last->head.prev;
        }
    }
    return( NULL );
}

static  bool            IsZero( name *op )
/****************************************/
{
    return( op->n.class == N_CONSTANT && op->c.const_type == CONS_ABSOLUTE && op->c.int_value == 0 );
}


typedef struct edge_entry {
    struct edge_entry   *next;
    block_edge          *edge;
} edge_entry;

typedef struct edge_stack {
    edge_entry          *top;
} edge_stack;

static  edge_stack      *InitStack( void )
/****************************************/
{
    edge_stack          *stk;

    _Alloc( stk, sizeof( edge_stack ) );
    stk->top = NULL;
    return( stk );
}

static  bool            Empty( edge_stack *stk )
/**********************************************/
{
    return( stk->top == NULL );
}

static  void            Push( edge_stack *stk, block_edge *edge )
/***************************************************************/
{
    edge_entry          *new_entry;

    _Alloc( new_entry, sizeof( edge_entry ) );
    new_entry->edge = edge;
    new_entry->next = stk->top;
    stk->top = new_entry;
}

static  block_edge      *Pop( edge_stack *stk )
/*********************************************/
{
    edge_entry          *top;
    block_edge          *edge;

    if( stk->top != NULL ) {
        top = stk->top;
        edge = top->edge;
        stk->top = top->next;
        _Free( top, sizeof( edge_entry ) );
        return( edge );
    }
    return( NULL );
}

static  void            FiniStack( edge_stack *stk )
/**************************************************/
{
    while( !Empty( stk ) ) {
        Pop( stk );
    }
    _Free( stk, sizeof( edge_stack ) );
}

static  bool            DereferencedBy( instruction *ins, name *ptr )
/********************************************************************
    Return TRUE if the instruction dereferences the given name. Any use as
    an index (with op as index), providing the base is NULL, is considered a deref.
*/
{
    int                 i;
    name                *op;

    op = ins->result;
    for( i = 0; i <= ins->num_operands; i++ ) {
        if( op != NULL && op->n.class == N_INDEXED ) {
            if( ptr == op->i.index ) {
                if( op->i.base == NULL ) {
                    return( TRUE );
                }
            }
        }
        op = ins->operands[ i ];
    }
    return( FALSE );
}

static  void            PushTargets( edge_stack *stk, block *blk, bool forward )
/******************************************************************************/
{
    block_num           i;
    block_edge          *edge;

    if( forward ) {
        for( i = 0; i < blk->targets; i++ ) {
            Push( stk, &blk->edge[ i ] );
        }
    } else {
        edge = blk->input_edges;
        while( edge != NULL ) {
            Push( stk, edge );
            edge = edge->next_source;
        }
    }
}

static  instruction     *NextIns( instruction *ins, bool forward )
/****************************************************************/
{
    instruction         *next;

    if( forward ) {
        next = ins->head.next;
    } else {
        next = ins->head.prev;
    }
    return( next );
}

static  instruction     *FirstIns( block *blk, bool forward )
/***********************************************************/
{
    instruction         *first;

    if( forward ) {
        first = blk->ins.hd.next;
    } else {
        first = blk->ins.hd.prev;
    }
    return( first );
}

static  block           *EdgeBlock( block_edge *edge, bool forward )
/******************************************************************/
{
    if( forward ) {
        return( edge->destination );
    } else {
        return( edge->source );
    }
}

static  bool            LastBlock( block *blk, bool forward )
/***********************************************************/
{
    if( forward ) {
        if( blk->class & RETURN ) return( TRUE );
    } else {
        if( blk == HeadBlock ) return( TRUE );
    }
    return( FALSE );
}

enum {
    BLOCK_DEREFS,
    BLOCK_REDEFS,
    BLOCK_NOTHING
};

typedef struct goofy_struct_so_we_can_use_saferecurse {
    block       *blk;
    instruction *ins;
    name        *op;
    bool        forward;
} parm_struct;

static  void            *DominatingDeref( parm_struct * );

static  int             BlockSearch( block *blk, instruction *ins, name *op, bool forward )
/******************************************************************************************
    Search the block for something interesting - either a dereference or a

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?