aboutsummaryrefslogtreecommitdiff
path: root/gcc/config/cil32/simp-cond.c
blob: a5bda65acdc3eb42a9c9a3a409d7e49e9bfd9db0 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* This pass tries to simplify conditional expression made of a couple of
   conditional/unconditional branches if one of the two outgoing paths falls
   through to the next basic block.

   Copyright (C) 2006-2010 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
   Thierry Lafage

Contact information at STMicroelectronics:
Andrea C. Ornstein      <andrea.ornstein@st.com>
Contact information at INRIA:
Erven Rohou             <erven.rohou@inria.fr>
Thierry Lafage          <thierry.lafage@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-flow.h"
#include "tree-pass.h"
#include "cil-stack.h"
#include "cil-stmt.h"
#include "cil-types.h"

/* These 2 lines avoid including options.h */
extern int flag_cil32_simp_cond;
extern int flag_cil32_simp_cond_float;

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

static inline enum cil_opcode int_condbr_invert (enum cil_opcode);
static inline enum cil_opcode float_condbr_invert (enum cil_opcode);
static void simplify_cond_branch (cil_stmt_iterator *, cil_stack);
static unsigned int simp_cond (void);
static bool simp_cond_gate (void);

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

static inline enum cil_opcode
int_condbr_invert (enum cil_opcode condbr)
{
  switch (condbr)
  {
    case CIL_BEQ:     return CIL_BNE_UN;
    case CIL_BGE:     return CIL_BLT;
    case CIL_BGE_UN:  return CIL_BLT_UN;
    case CIL_BGT:     return CIL_BLE;
    case CIL_BGT_UN:  return CIL_BLE_UN;
    case CIL_BLE:     return CIL_BGT;
    case CIL_BLE_UN:  return CIL_BGT_UN;
    case CIL_BLT:     return CIL_BGE;
    case CIL_BLT_UN:  return CIL_BGE_UN;
    case CIL_BNE_UN:  return CIL_BEQ;
    case CIL_BRTRUE:  return CIL_BRFALSE;
    case CIL_BRFALSE: return CIL_BRTRUE;
    default:
      return condbr;  /* Do nothing, we cannot change this branch.  */
  }
}

static inline enum cil_opcode
float_condbr_invert (enum cil_opcode condbr)
{
  switch (condbr)
  {
    case CIL_BEQ:     return CIL_BNE_UN;
    case CIL_BGE:     return CIL_BLT_UN;
    case CIL_BGE_UN:  return CIL_BLT;
    case CIL_BGT:     return CIL_BLE_UN;
    case CIL_BGT_UN:  return CIL_BLE;
    case CIL_BLE:     return CIL_BGT_UN;
    case CIL_BLE_UN:  return CIL_BGT;
    case CIL_BLT:     return CIL_BGE_UN;
    case CIL_BLT_UN:  return CIL_BGE;
    case CIL_BNE_UN:  return CIL_BEQ;
    default:
      return condbr;  /* Do nothing, we cannot change this branch.  */
  }
}

static void
simplify_cond_branch (cil_stmt_iterator *csi, cil_stack stack)
{
  cil_stmt stmt;
  cil_type_t type;
  enum cil_opcode opcode, condbr;
  tree label_then, label_else;
  basic_block then_bb;
  edge true_edge, false_edge;

  /* Extract the condition's edges.  */
  extract_true_false_edges_from_block (csi_bb (*csi), &true_edge, &false_edge);
  label_then = gimple_block_label (true_edge->dest);
  label_else = gimple_block_label (false_edge->dest);

  then_bb = label_to_block (label_then);

  /* Simplify the condition only if the 'then' edge can be turned into a
     fall through to the next basic block.  */
  if (csi_bb (*csi)->next_bb == then_bb)
    {
      type = cil_stack_top (stack);
      opcode = condbr = cil_opcode (csi_stmt (*csi));

      if (flag_cil32_simp_cond && (cil_integer_p (type) || cil_pointer_p (type)))
	  opcode = int_condbr_invert (condbr);
      else if (flag_cil32_simp_cond_float && cil_float_p (type))
	  opcode = float_condbr_invert (condbr);

      if (opcode == condbr)
	return; /* No opcode change */

      stmt = cil_build_stmt_arg (opcode, label_else);
      csi_replace (csi, stmt);
      csi_next (csi);
      csi_remove (csi);

      /* Invert the out-going edges */
      true_edge->flags ^= (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
      false_edge->flags ^= (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
    }
}

/* Main function of this pass.  Looks for conditional expressions and
   inverts their condition if it allows removing the unconditional branch
   following it.  */

static unsigned int
simp_cond (void)
{
  basic_block bb;
  cil_stmt_iterator csi;
  cil_stmt stmt;
  enum cil_opcode opcode;
  cil_bb_stacks bbs;
  cil_stack stack;
  edge_iterator ei;
  edge e;

  bbs = cil_bb_stacks_create ();

  FOR_EACH_BB (bb)
    {
      stack = cil_stack_for_bb (bbs, bb);
      csi = csi_start_bb (bb);

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

	  if (cil_cond_branch_p (stmt) && !csi_one_before_end_p (csi))
	    {
	      csi_next (&csi);

	      if (cil_opcode (csi_stmt (csi)) == CIL_BR)
		{
		  csi_prev (&csi);
		  simplify_cond_branch (&csi, stack);
		  break;
		}
	    }

	  cil_stack_after_stmt (stack, stmt);
	  csi_next (&csi);
	}

      FOR_EACH_EDGE (e, ei, bb->succs)
	{
	  cil_set_stack_for_bb (bbs, e->dest, stack);
	}
    }

  cil_bb_stacks_destroy (bbs);
  return 0;
}

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

static bool
simp_cond_gate (void)
{
  return flag_cil32_simp_cond != 0 || flag_cil32_simp_cond_float != 0;
}

/* Define the parameters of the cond-simp pass.  */

struct gimple_opt_pass pass_simp_cond =
{
 {
  GIMPLE_PASS,                          /* type */
  "cond_simp",                          /* name */
  simp_cond_gate,                       /* gate */
  simp_cond,                            /* execute */
  NULL,                                 /* sub */
  NULL,                                 /* next */
  0,                                    /* static_pass_number */
  TV_SIMP_COND,                         /* tv_id */
  PROP_cfg|PROP_cil,                    /* properties_required */
  0,                                    /* properties_provided */
  0,                                    /* properties_destroyed */
  0,                                    /* todo_flags_start */
  TODO_ggc_collect|TODO_dump_func       /* todo_flags_finish */
 }
};

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