+static void create_out_of_bounds_check(cond_env_t *env, ir_node *cond)
+{
+ ir_graph *irg = get_irn_irg(cond);
+ dbg_info *dbgi = get_irn_dbg_info(cond);
+ ir_node *sel = get_Cond_selector(cond);
+ ir_node *block = get_nodes_block(cond);
+ ir_mode *cmp_mode = get_irn_mode(sel);
+ ir_node **default_preds = NEW_ARR_F(ir_node*, 0);
+ long default_pn = get_Cond_default_proj(cond);
+ long delta = 0;
+ ir_node *max_const;
+ ir_node *proj_true;
+ ir_node *proj_false;
+ ir_node *cmp;
+ ir_node *oob_cond;
+ ir_node *in[1];
+ ir_node *new_block;
+ size_t n_default_preds;
+ int i;
+ ir_node *proj;
+
+ if (mode_is_signed(cmp_mode)) {
+ cmp_mode = find_unsigned_mode(cmp_mode);
+ sel = new_r_Conv(block, sel, cmp_mode);
+ }
+
+ /* normalize so switch_min is at 0 */
+ if (env->switch_min != 0) {
+ ir_node *min_const = new_r_Const_long(irg, cmp_mode, env->switch_min);
+ sel = new_rd_Sub(dbgi, block, sel, min_const, cmp_mode);
+
+ delta = env->switch_min;
+ env->switch_min = 0;
+ env->switch_max -= delta;
+ set_Cond_selector(cond, sel);
+ }
+
+ /* check for out-of-bounds */
+ max_const = new_r_Const_long(irg, cmp_mode, env->switch_max);
+ cmp = new_rd_Cmp(dbgi, block, sel, max_const, ir_relation_less_equal);
+ oob_cond = new_rd_Cond(dbgi, block, cmp);
+ proj_true = new_r_Proj(oob_cond, mode_X, pn_Cond_true);
+ proj_false = new_r_Proj(oob_cond, mode_X, pn_Cond_false);
+
+ ARR_APP1(ir_node*, default_preds, proj_false);
+
+ /* create new block containing the cond */
+ in[0] = proj_true;
+ new_block = new_r_Block(irg, 1, in);
+ set_nodes_block(cond, new_block);
+
+ /* adapt projs */
+ foreach_out_irn(cond, i, proj) {
+ long pn = get_Proj_proj(proj);
+ long new_pn = pn - delta;
+ if (pn == default_pn) {
+ /* we might have to choose a new default_pn */
+ if (pn < env->switch_max) {
+ new_pn = env->switch_max + 1;
+ set_Cond_default_proj(cond, new_pn);
+ } else {
+ new_pn = default_pn;
+ }
+ ARR_APP1(ir_node*, default_preds, proj);
+ }
+
+ set_Proj_proj(proj, new_pn);
+ set_nodes_block(proj, new_block);
+ }
+
+ /* adapt default block */
+ n_default_preds = ARR_LEN(default_preds);
+ if (n_default_preds > 1) {
+ size_t i;
+
+ /* create new intermediate blocks so we don't have critical edges */
+ for (i = 0; i < n_default_preds; ++i) {
+ ir_node *proj = default_preds[i];
+ ir_node *block;
+ ir_node *in[1];
+
+ in[0] = proj;
+ block = new_r_Block(irg, 1, in);
+
+ default_preds[i] = new_r_Jmp(block);
+ }
+ }
+ set_irn_in(env->default_block, n_default_preds, default_preds);
+
+ DEL_ARR_F(default_preds);
+}
+