aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/cil32/peephole.c
blob: 046e13ac9e4cbb33f09e659f7b97c0a33e340bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* This pass applies a few simple peephole optimizations.

   Copyright (C) 2006-2009 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING.  If not, write to the Free
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.

Authors:
   Andrea Ornstein
   Erven Rohou
   Gabriele Svelto

Contact information at STMicroelectronics:
Andrea C. Ornstein      <andrea.ornstein@st.com>
Contact information at INRIA:
Erven Rohou             <erven.rohou@inria.fr>
*/

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "flags.h"
#include "timevar.h"
#include "tree.h"
#include "tree-pass.h"
#include "cil-stmt.h"
#include "cil-types.h"

/******************************************************************************
 * Local functions prototypes                                                 *
 ******************************************************************************/

static unsigned int cil_peephole (void);
static bool cil_peephole_gate (void);

/******************************************************************************
 * Pass implementation                                                        *
 ******************************************************************************/

/* Main function of this pass, do some peephole optimizations:
   LDLOCA/LDIND -> LDLOC
   LDARGA/LDIND -> LDARG
   LDLOCA/<instr>/STIND -> <instr>/STLOC
   LDARGA/<instr>/STIND -> <instr>/STLOC */

static unsigned int
cil_peephole (void)
{
  basic_block bb;
  cil_stmt_iterator csi;
  cil_stmt stmt;
  enum cil_opcode opcode;
  tree var;
  bool loc;

  FOR_EACH_BB (bb)
    {
      csi = csi_start_bb (bb);

      while (!csi_end_p (csi))
	{
	  stmt = csi_stmt (csi);
	  opcode = cil_opcode (stmt);

	  if (((opcode == CIL_LDLOCA) || (opcode == CIL_LDARGA))
	      && !csi_one_before_end_p (csi))
	    {
	      var = cil_var (stmt);
	      loc = (opcode == CIL_LDLOCA);
	      csi_next (&csi);
	      stmt = csi_stmt (csi);

	      if (cil_ldind_p (stmt) && !cil_prefix_volatile (stmt))
		{
		  csi_prev (&csi);
		  csi_remove (&csi);
		  opcode = loc ? CIL_LDLOC : CIL_LDARG;
		  csi_replace (&csi, cil_build_stmt_arg (opcode, var));
		}
	      else if (cil_push_p (stmt) && !csi_one_before_end_p (csi))
		{
		  csi_next (&csi);
		  stmt = csi_stmt (csi);

		  if (cil_stind_p (stmt) && !cil_prefix_volatile (stmt))
		    {
		      csi_prev (&csi);
		      csi_prev (&csi);
		      csi_remove (&csi);
		      csi_next (&csi);
		      opcode = loc ? CIL_STLOC : CIL_STARG;
		      csi_replace (&csi, cil_build_stmt_arg (opcode, var));
		    }
		}
	    }
	  else
	    csi_next (&csi);
	}
    }

  return 0;
}

/* Gate function of the peephole-optimizations pass.  */

static bool
cil_peephole_gate (void)
{
  return true;
}

/* Define the parameters of the peephole-optimizations pass.  */

struct gimple_opt_pass pass_cil_peephole =
{
 {
  GIMPLE_PASS,                          /* type */
  "cil_peephole",                       /* name */
  cil_peephole_gate,                    /* gate */
  cil_peephole,                         /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_CIL_PEEPHOLE,                      /* tv_id */
  PROP_cfg,                             /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
  TODO_ggc_collect                      /* todo_flags_finish */
 }
};

/*
 * Local variables:
 * eval: (c-set-style "gnu")
 * End:
 */