summaryrefslogtreecommitdiff
path: root/gcc/gimple.c
diff options
context:
space:
mode:
authormrs <mrs@138bc75d-0d04-0410-961f-82ee72b054a4>2013-11-16 01:47:58 +0000
committermrs <mrs@138bc75d-0d04-0410-961f-82ee72b054a4>2013-11-16 01:47:58 +0000
commit8370b483d36930e46d3ec5a083702386af597654 (patch)
treec3abfe3ff9a4df7e1545b739c0a7409c09cd6b2d /gcc/gimple.c
parent8f7323c5912412fdde781a7667fbf60046592218 (diff)
parent6ca03b4e86cec2ebd2b61425871730a8e0cd5df9 (diff)
downloadgcc-8370b483d36930e46d3ec5a083702386af597654.tar.gz
Merge in trunk.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/wide-int@204895 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/gimple.c')
-rw-r--r--gcc/gimple.c1143
1 files changed, 286 insertions, 857 deletions
diff --git a/gcc/gimple.c b/gcc/gimple.c
index b041292e952..278135c18d8 100644
--- a/gcc/gimple.c
+++ b/gcc/gimple.c
@@ -29,6 +29,10 @@ along with GCC; see the file COPYING3. If not see
#include "hard-reg-set.h"
#include "basic-block.h"
#include "gimple.h"
+#include "gimple-iterator.h"
+#include "gimple-walk.h"
+#include "gimple.h"
+#include "gimplify.h"
#include "diagnostic.h"
#include "value-prof.h"
#include "flags.h"
@@ -446,24 +450,6 @@ gimple_build_assign_with_ops (enum tree_code subcode, tree lhs, tree op1,
}
-/* Build a new GIMPLE_ASSIGN tuple and append it to the end of *SEQ_P.
-
- DST/SRC are the destination and source respectively. You can pass
- ungimplified trees in DST or SRC, in which case they will be
- converted to a gimple operand if necessary.
-
- This function returns the newly created GIMPLE_ASSIGN tuple. */
-
-gimple
-gimplify_assign (tree dst, tree src, gimple_seq *seq_p)
-{
- tree t = build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
- gimplify_and_add (t, seq_p);
- ggc_free (t);
- return gimple_seq_last_stmt (*seq_p);
-}
-
-
/* Build a GIMPLE_COND statement.
PRED is the condition used to compare LHS and the RHS.
@@ -1172,6 +1158,23 @@ gimple_seq_add_stmt (gimple_seq *seq_p, gimple gs)
gsi_insert_after (&si, gs, GSI_NEW_STMT);
}
+/* Link gimple statement GS to the end of the sequence *SEQ_P. If
+ *SEQ_P is NULL, a new sequence is allocated. This function is
+ similar to gimple_seq_add_stmt, but does not scan the operands.
+ During gimplification, we need to manipulate statement sequences
+ before the def/use vectors have been constructed. */
+
+void
+gimple_seq_add_stmt_without_update (gimple_seq *seq_p, gimple gs)
+{
+ gimple_stmt_iterator si;
+
+ if (gs == NULL)
+ return;
+
+ si = gsi_last (*seq_p);
+ gsi_insert_after_without_update (&si, gs, GSI_NEW_STMT);
+}
/* Append sequence SRC to the end of sequence *DST_P. If *DST_P is
NULL, a new sequence is allocated. */
@@ -1187,6 +1190,64 @@ gimple_seq_add_seq (gimple_seq *dst_p, gimple_seq src)
gsi_insert_seq_after (&si, src, GSI_NEW_STMT);
}
+/* Determine whether to assign a location to the statement GS. */
+
+static bool
+should_carry_location_p (gimple gs)
+{
+ /* Don't emit a line note for a label. We particularly don't want to
+ emit one for the break label, since it doesn't actually correspond
+ to the beginning of the loop/switch. */
+ if (gimple_code (gs) == GIMPLE_LABEL)
+ return false;
+
+ return true;
+}
+
+/* Set the location for gimple statement GS to LOCATION. */
+
+static void
+annotate_one_with_location (gimple gs, location_t location)
+{
+ if (!gimple_has_location (gs)
+ && !gimple_do_not_emit_location_p (gs)
+ && should_carry_location_p (gs))
+ gimple_set_location (gs, location);
+}
+
+/* Set LOCATION for all the statements after iterator GSI in sequence
+ SEQ. If GSI is pointing to the end of the sequence, start with the
+ first statement in SEQ. */
+
+void
+annotate_all_with_location_after (gimple_seq seq, gimple_stmt_iterator gsi,
+ location_t location)
+{
+ if (gsi_end_p (gsi))
+ gsi = gsi_start (seq);
+ else
+ gsi_next (&gsi);
+
+ for (; !gsi_end_p (gsi); gsi_next (&gsi))
+ annotate_one_with_location (gsi_stmt (gsi), location);
+}
+
+/* Set the location for all the statements in a sequence STMT_P to LOCATION. */
+
+void
+annotate_all_with_location (gimple_seq stmt_p, location_t location)
+{
+ gimple_stmt_iterator i;
+
+ if (gimple_seq_empty_p (stmt_p))
+ return;
+
+ for (i = gsi_start (stmt_p); !gsi_end_p (i); gsi_next (&i))
+ {
+ gimple gs = gsi_stmt (i);
+ annotate_one_with_location (gs, location);
+ }
+}
/* Helper function of empty_body_p. Return true if STMT is an empty
statement. */
@@ -1239,600 +1300,6 @@ gimple_seq_copy (gimple_seq src)
}
-/* Walk all the statements in the sequence *PSEQ calling walk_gimple_stmt
- on each one. WI is as in walk_gimple_stmt.
-
- If walk_gimple_stmt returns non-NULL, the walk is stopped, and the
- value is stored in WI->CALLBACK_RESULT. Also, the statement that
- produced the value is returned if this statement has not been
- removed by a callback (wi->removed_stmt). If the statement has
- been removed, NULL is returned.
-
- Otherwise, all the statements are walked and NULL returned. */
-
-gimple
-walk_gimple_seq_mod (gimple_seq *pseq, walk_stmt_fn callback_stmt,
- walk_tree_fn callback_op, struct walk_stmt_info *wi)
-{
- gimple_stmt_iterator gsi;
-
- for (gsi = gsi_start (*pseq); !gsi_end_p (gsi); )
- {
- tree ret = walk_gimple_stmt (&gsi, callback_stmt, callback_op, wi);
- if (ret)
- {
- /* If CALLBACK_STMT or CALLBACK_OP return a value, WI must exist
- to hold it. */
- gcc_assert (wi);
- wi->callback_result = ret;
-
- return wi->removed_stmt ? NULL : gsi_stmt (gsi);
- }
-
- if (!wi->removed_stmt)
- gsi_next (&gsi);
- }
-
- if (wi)
- wi->callback_result = NULL_TREE;
-
- return NULL;
-}
-
-
-/* Like walk_gimple_seq_mod, but ensure that the head of SEQ isn't
- changed by the callbacks. */
-
-gimple
-walk_gimple_seq (gimple_seq seq, walk_stmt_fn callback_stmt,
- walk_tree_fn callback_op, struct walk_stmt_info *wi)
-{
- gimple_seq seq2 = seq;
- gimple ret = walk_gimple_seq_mod (&seq2, callback_stmt, callback_op, wi);
- gcc_assert (seq2 == seq);
- return ret;
-}
-
-
-/* Helper function for walk_gimple_stmt. Walk operands of a GIMPLE_ASM. */
-
-static tree
-walk_gimple_asm (gimple stmt, walk_tree_fn callback_op,
- struct walk_stmt_info *wi)
-{
- tree ret, op;
- unsigned noutputs;
- const char **oconstraints;
- unsigned i, n;
- const char *constraint;
- bool allows_mem, allows_reg, is_inout;
-
- noutputs = gimple_asm_noutputs (stmt);
- oconstraints = (const char **) alloca ((noutputs) * sizeof (const char *));
-
- if (wi)
- wi->is_lhs = true;
-
- for (i = 0; i < noutputs; i++)
- {
- op = gimple_asm_output_op (stmt, i);
- constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
- oconstraints[i] = constraint;
- parse_output_constraint (&constraint, i, 0, 0, &allows_mem, &allows_reg,
- &is_inout);
- if (wi)
- wi->val_only = (allows_reg || !allows_mem);
- ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
- if (ret)
- return ret;
- }
-
- n = gimple_asm_ninputs (stmt);
- for (i = 0; i < n; i++)
- {
- op = gimple_asm_input_op (stmt, i);
- constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (op)));
- parse_input_constraint (&constraint, 0, 0, noutputs, 0,
- oconstraints, &allows_mem, &allows_reg);
- if (wi)
- {
- wi->val_only = (allows_reg || !allows_mem);
- /* Although input "m" is not really a LHS, we need a lvalue. */
- wi->is_lhs = !wi->val_only;
- }
- ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
- if (ret)
- return ret;
- }
-
- if (wi)
- {
- wi->is_lhs = false;
- wi->val_only = true;
- }
-
- n = gimple_asm_nlabels (stmt);
- for (i = 0; i < n; i++)
- {
- op = gimple_asm_label_op (stmt, i);
- ret = walk_tree (&TREE_VALUE (op), callback_op, wi, NULL);
- if (ret)
- return ret;
- }
-
- return NULL_TREE;
-}
-
-
-/* Helper function of WALK_GIMPLE_STMT. Walk every tree operand in
- STMT. CALLBACK_OP and WI are as in WALK_GIMPLE_STMT.
-
- CALLBACK_OP is called on each operand of STMT via walk_tree.
- Additional parameters to walk_tree must be stored in WI. For each operand
- OP, walk_tree is called as:
-
- walk_tree (&OP, CALLBACK_OP, WI, WI->PSET)
-
- If CALLBACK_OP returns non-NULL for an operand, the remaining
- operands are not scanned.
-
- The return value is that returned by the last call to walk_tree, or
- NULL_TREE if no CALLBACK_OP is specified. */
-
-tree
-walk_gimple_op (gimple stmt, walk_tree_fn callback_op,
- struct walk_stmt_info *wi)
-{
- struct pointer_set_t *pset = (wi) ? wi->pset : NULL;
- unsigned i;
- tree ret = NULL_TREE;
-
- switch (gimple_code (stmt))
- {
- case GIMPLE_ASSIGN:
- /* Walk the RHS operands. If the LHS is of a non-renamable type or
- is a register variable, we may use a COMPONENT_REF on the RHS. */
- if (wi)
- {
- tree lhs = gimple_assign_lhs (stmt);
- wi->val_only
- = (is_gimple_reg_type (TREE_TYPE (lhs)) && !is_gimple_reg (lhs))
- || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
- }
-
- for (i = 1; i < gimple_num_ops (stmt); i++)
- {
- ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi,
- pset);
- if (ret)
- return ret;
- }
-
- /* Walk the LHS. If the RHS is appropriate for a memory, we
- may use a COMPONENT_REF on the LHS. */
- if (wi)
- {
- /* If the RHS is of a non-renamable type or is a register variable,
- we may use a COMPONENT_REF on the LHS. */
- tree rhs1 = gimple_assign_rhs1 (stmt);
- wi->val_only
- = (is_gimple_reg_type (TREE_TYPE (rhs1)) && !is_gimple_reg (rhs1))
- || gimple_assign_rhs_class (stmt) != GIMPLE_SINGLE_RHS;
- wi->is_lhs = true;
- }
-
- ret = walk_tree (gimple_op_ptr (stmt, 0), callback_op, wi, pset);
- if (ret)
- return ret;
-
- if (wi)
- {
- wi->val_only = true;
- wi->is_lhs = false;
- }
- break;
-
- case GIMPLE_CALL:
- if (wi)
- {
- wi->is_lhs = false;
- wi->val_only = true;
- }
-
- ret = walk_tree (gimple_call_chain_ptr (stmt), callback_op, wi, pset);
- if (ret)
- return ret;
-
- ret = walk_tree (gimple_call_fn_ptr (stmt), callback_op, wi, pset);
- if (ret)
- return ret;
-
- for (i = 0; i < gimple_call_num_args (stmt); i++)
- {
- if (wi)
- wi->val_only
- = is_gimple_reg_type (TREE_TYPE (gimple_call_arg (stmt, i)));
- ret = walk_tree (gimple_call_arg_ptr (stmt, i), callback_op, wi,
- pset);
- if (ret)
- return ret;
- }
-
- if (gimple_call_lhs (stmt))
- {
- if (wi)
- {
- wi->is_lhs = true;
- wi->val_only
- = is_gimple_reg_type (TREE_TYPE (gimple_call_lhs (stmt)));
- }
-
- ret = walk_tree (gimple_call_lhs_ptr (stmt), callback_op, wi, pset);
- if (ret)
- return ret;
- }
-
- if (wi)
- {
- wi->is_lhs = false;
- wi->val_only = true;
- }
- break;
-
- case GIMPLE_CATCH:
- ret = walk_tree (gimple_catch_types_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_EH_FILTER:
- ret = walk_tree (gimple_eh_filter_types_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_ASM:
- ret = walk_gimple_asm (stmt, callback_op, wi);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_CONTINUE:
- ret = walk_tree (gimple_omp_continue_control_def_ptr (stmt),
- callback_op, wi, pset);
- if (ret)
- return ret;
-
- ret = walk_tree (gimple_omp_continue_control_use_ptr (stmt),
- callback_op, wi, pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_CRITICAL:
- ret = walk_tree (gimple_omp_critical_name_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_FOR:
- ret = walk_tree (gimple_omp_for_clauses_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- for (i = 0; i < gimple_omp_for_collapse (stmt); i++)
- {
- ret = walk_tree (gimple_omp_for_index_ptr (stmt, i), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_for_initial_ptr (stmt, i), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_for_final_ptr (stmt, i), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_for_incr_ptr (stmt, i), callback_op,
- wi, pset);
- }
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_PARALLEL:
- ret = walk_tree (gimple_omp_parallel_clauses_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_parallel_child_fn_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_parallel_data_arg_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_TASK:
- ret = walk_tree (gimple_omp_task_clauses_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_task_child_fn_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_task_data_arg_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_task_copy_fn_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_task_arg_size_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- ret = walk_tree (gimple_omp_task_arg_align_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_SECTIONS:
- ret = walk_tree (gimple_omp_sections_clauses_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
-
- ret = walk_tree (gimple_omp_sections_control_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
-
- break;
-
- case GIMPLE_OMP_SINGLE:
- ret = walk_tree (gimple_omp_single_clauses_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_TARGET:
- ret = walk_tree (gimple_omp_target_clauses_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_TEAMS:
- ret = walk_tree (gimple_omp_teams_clauses_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_ATOMIC_LOAD:
- ret = walk_tree (gimple_omp_atomic_load_lhs_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
-
- ret = walk_tree (gimple_omp_atomic_load_rhs_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_ATOMIC_STORE:
- ret = walk_tree (gimple_omp_atomic_store_val_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_TRANSACTION:
- ret = walk_tree (gimple_transaction_label_ptr (stmt), callback_op,
- wi, pset);
- if (ret)
- return ret;
- break;
-
- case GIMPLE_OMP_RETURN:
- ret = walk_tree (gimple_omp_return_lhs_ptr (stmt), callback_op, wi,
- pset);
- if (ret)
- return ret;
- break;
-
- /* Tuples that do not have operands. */
- case GIMPLE_NOP:
- case GIMPLE_RESX:
- case GIMPLE_PREDICT:
- break;
-
- default:
- {
- enum gimple_statement_structure_enum gss;
- gss = gimple_statement_structure (stmt);
- if (gss == GSS_WITH_OPS || gss == GSS_WITH_MEM_OPS)
- for (i = 0; i < gimple_num_ops (stmt); i++)
- {
- ret = walk_tree (gimple_op_ptr (stmt, i), callback_op, wi, pset);
- if (ret)
- return ret;
- }
- }
- break;
- }
-
- return NULL_TREE;
-}
-
-
-/* Walk the current statement in GSI (optionally using traversal state
- stored in WI). If WI is NULL, no state is kept during traversal.
- The callback CALLBACK_STMT is called. If CALLBACK_STMT indicates
- that it has handled all the operands of the statement, its return
- value is returned. Otherwise, the return value from CALLBACK_STMT
- is discarded and its operands are scanned.
-
- If CALLBACK_STMT is NULL or it didn't handle the operands,
- CALLBACK_OP is called on each operand of the statement via
- walk_gimple_op. If walk_gimple_op returns non-NULL for any
- operand, the remaining operands are not scanned. In this case, the
- return value from CALLBACK_OP is returned.
-
- In any other case, NULL_TREE is returned. */
-
-tree
-walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
- walk_tree_fn callback_op, struct walk_stmt_info *wi)
-{
- gimple ret;
- tree tree_ret;
- gimple stmt = gsi_stmt (*gsi);
-
- if (wi)
- {
- wi->gsi = *gsi;
- wi->removed_stmt = false;
-
- if (wi->want_locations && gimple_has_location (stmt))
- input_location = gimple_location (stmt);
- }
-
- ret = NULL;
-
- /* Invoke the statement callback. Return if the callback handled
- all of STMT operands by itself. */
- if (callback_stmt)
- {
- bool handled_ops = false;
- tree_ret = callback_stmt (gsi, &handled_ops, wi);
- if (handled_ops)
- return tree_ret;
-
- /* If CALLBACK_STMT did not handle operands, it should not have
- a value to return. */
- gcc_assert (tree_ret == NULL);
-
- if (wi && wi->removed_stmt)
- return NULL;
-
- /* Re-read stmt in case the callback changed it. */
- stmt = gsi_stmt (*gsi);
- }
-
- /* If CALLBACK_OP is defined, invoke it on every operand of STMT. */
- if (callback_op)
- {
- tree_ret = walk_gimple_op (stmt, callback_op, wi);
- if (tree_ret)
- return tree_ret;
- }
-
- /* If STMT can have statements inside (e.g. GIMPLE_BIND), walk them. */
- switch (gimple_code (stmt))
- {
- case GIMPLE_BIND:
- ret = walk_gimple_seq_mod (gimple_bind_body_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_CATCH:
- ret = walk_gimple_seq_mod (gimple_catch_handler_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_EH_FILTER:
- ret = walk_gimple_seq_mod (gimple_eh_filter_failure_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_EH_ELSE:
- ret = walk_gimple_seq_mod (gimple_eh_else_n_body_ptr (stmt),
- callback_stmt, callback_op, wi);
- if (ret)
- return wi->callback_result;
- ret = walk_gimple_seq_mod (gimple_eh_else_e_body_ptr (stmt),
- callback_stmt, callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_TRY:
- ret = walk_gimple_seq_mod (gimple_try_eval_ptr (stmt), callback_stmt, callback_op,
- wi);
- if (ret)
- return wi->callback_result;
-
- ret = walk_gimple_seq_mod (gimple_try_cleanup_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_OMP_FOR:
- ret = walk_gimple_seq_mod (gimple_omp_for_pre_body_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
-
- /* FALL THROUGH. */
- case GIMPLE_OMP_CRITICAL:
- case GIMPLE_OMP_MASTER:
- case GIMPLE_OMP_TASKGROUP:
- case GIMPLE_OMP_ORDERED:
- case GIMPLE_OMP_SECTION:
- case GIMPLE_OMP_PARALLEL:
- case GIMPLE_OMP_TASK:
- case GIMPLE_OMP_SECTIONS:
- case GIMPLE_OMP_SINGLE:
- case GIMPLE_OMP_TARGET:
- case GIMPLE_OMP_TEAMS:
- ret = walk_gimple_seq_mod (gimple_omp_body_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_WITH_CLEANUP_EXPR:
- ret = walk_gimple_seq_mod (gimple_wce_cleanup_ptr (stmt), callback_stmt,
- callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- case GIMPLE_TRANSACTION:
- ret = walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt),
- callback_stmt, callback_op, wi);
- if (ret)
- return wi->callback_result;
- break;
-
- default:
- gcc_assert (!gimple_has_substatements (stmt));
- break;
- }
-
- return NULL;
-}
-
/* Return true if calls C1 and C2 are known to go to the same function. */
@@ -2938,251 +2405,6 @@ gimple_get_alias_set (tree t)
}
-/* From a tree operand OP return the base of a load or store operation
- or NULL_TREE if OP is not a load or a store. */
-
-static tree
-get_base_loadstore (tree op)
-{
- while (handled_component_p (op))
- op = TREE_OPERAND (op, 0);
- if (DECL_P (op)
- || INDIRECT_REF_P (op)
- || TREE_CODE (op) == MEM_REF
- || TREE_CODE (op) == TARGET_MEM_REF)
- return op;
- return NULL_TREE;
-}
-
-/* For the statement STMT call the callbacks VISIT_LOAD, VISIT_STORE and
- VISIT_ADDR if non-NULL on loads, store and address-taken operands
- passing the STMT, the base of the operand and DATA to it. The base
- will be either a decl, an indirect reference (including TARGET_MEM_REF)
- or the argument of an address expression.
- Returns the results of these callbacks or'ed. */
-
-bool
-walk_stmt_load_store_addr_ops (gimple stmt, void *data,
- bool (*visit_load)(gimple, tree, void *),
- bool (*visit_store)(gimple, tree, void *),
- bool (*visit_addr)(gimple, tree, void *))
-{
- bool ret = false;
- unsigned i;
- if (gimple_assign_single_p (stmt))
- {
- tree lhs, rhs;
- if (visit_store)
- {
- lhs = get_base_loadstore (gimple_assign_lhs (stmt));
- if (lhs)
- ret |= visit_store (stmt, lhs, data);
- }
- rhs = gimple_assign_rhs1 (stmt);
- while (handled_component_p (rhs))
- rhs = TREE_OPERAND (rhs, 0);
- if (visit_addr)
- {
- if (TREE_CODE (rhs) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
- else if (TREE_CODE (rhs) == TARGET_MEM_REF
- && TREE_CODE (TMR_BASE (rhs)) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (rhs), 0), data);
- else if (TREE_CODE (rhs) == OBJ_TYPE_REF
- && TREE_CODE (OBJ_TYPE_REF_OBJECT (rhs)) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (OBJ_TYPE_REF_OBJECT (rhs),
- 0), data);
- else if (TREE_CODE (rhs) == CONSTRUCTOR)
- {
- unsigned int ix;
- tree val;
-
- FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (rhs), ix, val)
- if (TREE_CODE (val) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (val, 0), data);
- else if (TREE_CODE (val) == OBJ_TYPE_REF
- && TREE_CODE (OBJ_TYPE_REF_OBJECT (val)) == ADDR_EXPR)
- ret |= visit_addr (stmt,
- TREE_OPERAND (OBJ_TYPE_REF_OBJECT (val),
- 0), data);
- }
- lhs = gimple_assign_lhs (stmt);
- if (TREE_CODE (lhs) == TARGET_MEM_REF
- && TREE_CODE (TMR_BASE (lhs)) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (TMR_BASE (lhs), 0), data);
- }
- if (visit_load)
- {
- rhs = get_base_loadstore (rhs);
- if (rhs)
- ret |= visit_load (stmt, rhs, data);
- }
- }
- else if (visit_addr
- && (is_gimple_assign (stmt)
- || gimple_code (stmt) == GIMPLE_COND))
- {
- for (i = 0; i < gimple_num_ops (stmt); ++i)
- {
- tree op = gimple_op (stmt, i);
- if (op == NULL_TREE)
- ;
- else if (TREE_CODE (op) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
- /* COND_EXPR and VCOND_EXPR rhs1 argument is a comparison
- tree with two operands. */
- else if (i == 1 && COMPARISON_CLASS_P (op))
- {
- if (TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 0),
- 0), data);
- if (TREE_CODE (TREE_OPERAND (op, 1)) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (TREE_OPERAND (op, 1),
- 0), data);
- }
- }
- }
- else if (is_gimple_call (stmt))
- {
- if (visit_store)
- {
- tree lhs = gimple_call_lhs (stmt);
- if (lhs)
- {
- lhs = get_base_loadstore (lhs);
- if (lhs)
- ret |= visit_store (stmt, lhs, data);
- }
- }
- if (visit_load || visit_addr)
- for (i = 0; i < gimple_call_num_args (stmt); ++i)
- {
- tree rhs = gimple_call_arg (stmt, i);
- if (visit_addr
- && TREE_CODE (rhs) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (rhs, 0), data);
- else if (visit_load)
- {
- rhs = get_base_loadstore (rhs);
- if (rhs)
- ret |= visit_load (stmt, rhs, data);
- }
- }
- if (visit_addr
- && gimple_call_chain (stmt)
- && TREE_CODE (gimple_call_chain (stmt)) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (gimple_call_chain (stmt), 0),
- data);
- if (visit_addr
- && gimple_call_return_slot_opt_p (stmt)
- && gimple_call_lhs (stmt) != NULL_TREE
- && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
- ret |= visit_addr (stmt, gimple_call_lhs (stmt), data);
- }
- else if (gimple_code (stmt) == GIMPLE_ASM)
- {
- unsigned noutputs;
- const char *constraint;
- const char **oconstraints;
- bool allows_mem, allows_reg, is_inout;
- noutputs = gimple_asm_noutputs (stmt);
- oconstraints = XALLOCAVEC (const char *, noutputs);
- if (visit_store || visit_addr)
- for (i = 0; i < gimple_asm_noutputs (stmt); ++i)
- {
- tree link = gimple_asm_output_op (stmt, i);
- tree op = get_base_loadstore (TREE_VALUE (link));
- if (op && visit_store)
- ret |= visit_store (stmt, op, data);
- if (visit_addr)
- {
- constraint = TREE_STRING_POINTER
- (TREE_VALUE (TREE_PURPOSE (link)));
- oconstraints[i] = constraint;
- parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
- &allows_reg, &is_inout);
- if (op && !allows_reg && allows_mem)
- ret |= visit_addr (stmt, op, data);
- }
- }
- if (visit_load || visit_addr)
- for (i = 0; i < gimple_asm_ninputs (stmt); ++i)
- {
- tree link = gimple_asm_input_op (stmt, i);
- tree op = TREE_VALUE (link);
- if (visit_addr
- && TREE_CODE (op) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
- else if (visit_load || visit_addr)
- {
- op = get_base_loadstore (op);
- if (op)
- {
- if (visit_load)
- ret |= visit_load (stmt, op, data);
- if (visit_addr)
- {
- constraint = TREE_STRING_POINTER
- (TREE_VALUE (TREE_PURPOSE (link)));
- parse_input_constraint (&constraint, 0, 0, noutputs,
- 0, oconstraints,
- &allows_mem, &allows_reg);
- if (!allows_reg && allows_mem)
- ret |= visit_addr (stmt, op, data);
- }
- }
- }
- }
- }
- else if (gimple_code (stmt) == GIMPLE_RETURN)
- {
- tree op = gimple_return_retval (stmt);
- if (op)
- {
- if (visit_addr
- && TREE_CODE (op) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
- else if (visit_load)
- {
- op = get_base_loadstore (op);
- if (op)
- ret |= visit_load (stmt, op, data);
- }
- }
- }
- else if (visit_addr
- && gimple_code (stmt) == GIMPLE_PHI)
- {
- for (i = 0; i < gimple_phi_num_args (stmt); ++i)
- {
- tree op = gimple_phi_arg_def (stmt, i);
- if (TREE_CODE (op) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
- }
- }
- else if (visit_addr
- && gimple_code (stmt) == GIMPLE_GOTO)
- {
- tree op = gimple_goto_dest (stmt);
- if (TREE_CODE (op) == ADDR_EXPR)
- ret |= visit_addr (stmt, TREE_OPERAND (op, 0), data);
- }
-
- return ret;
-}
-
-/* Like walk_stmt_load_store_addr_ops but with NULL visit_addr. IPA-CP
- should make a faster clone for this case. */
-
-bool
-walk_stmt_load_store_ops (gimple stmt, void *data,
- bool (*visit_load)(gimple, tree, void *),
- bool (*visit_store)(gimple, tree, void *))
-{
- return walk_stmt_load_store_addr_ops (stmt, data,
- visit_load, visit_store, NULL);
-}
-
/* Helper for gimple_ior_addresses_taken_1. */
static bool
@@ -3428,3 +2650,210 @@ infer_nonnull_range (gimple stmt, tree op)
return false;
}
+
+/* Compare two case labels. Because the front end should already have
+ made sure that case ranges do not overlap, it is enough to only compare
+ the CASE_LOW values of each case label. */
+
+static int
+compare_case_labels (const void *p1, const void *p2)
+{
+ const_tree const case1 = *(const_tree const*)p1;
+ const_tree const case2 = *(const_tree const*)p2;
+
+ /* The 'default' case label always goes first. */
+ if (!CASE_LOW (case1))
+ return -1;
+ else if (!CASE_LOW (case2))
+ return 1;
+ else
+ return tree_int_cst_compare (CASE_LOW (case1), CASE_LOW (case2));
+}
+
+/* Sort the case labels in LABEL_VEC in place in ascending order. */
+
+void
+sort_case_labels (vec<tree> label_vec)
+{
+ label_vec.qsort (compare_case_labels);
+}
+
+/* Prepare a vector of case labels to be used in a GIMPLE_SWITCH statement.
+
+ LABELS is a vector that contains all case labels to look at.
+
+ INDEX_TYPE is the type of the switch index expression. Case labels
+ in LABELS are discarded if their values are not in the value range
+ covered by INDEX_TYPE. The remaining case label values are folded
+ to INDEX_TYPE.
+
+ If a default case exists in LABELS, it is removed from LABELS and
+ returned in DEFAULT_CASEP. If no default case exists, but the
+ case labels already cover the whole range of INDEX_TYPE, a default
+ case is returned pointing to one of the existing case labels.
+ Otherwise DEFAULT_CASEP is set to NULL_TREE.
+
+ DEFAULT_CASEP may be NULL, in which case the above comment doesn't
+ apply and no action is taken regardless of whether a default case is
+ found or not. */
+
+void
+preprocess_case_label_vec_for_gimple (vec<tree> labels,
+ tree index_type,
+ tree *default_casep)
+{
+ tree min_value, max_value;
+ tree default_case = NULL_TREE;
+ size_t i, len;
+
+ i = 0;
+ min_value = TYPE_MIN_VALUE (index_type);
+ max_value = TYPE_MAX_VALUE (index_type);
+ while (i < labels.length ())
+ {
+ tree elt = labels[i];
+ tree low = CASE_LOW (elt);
+ tree high = CASE_HIGH (elt);
+ bool remove_element = FALSE;
+
+ if (low)
+ {
+ gcc_checking_assert (TREE_CODE (low) == INTEGER_CST);
+ gcc_checking_assert (!high || TREE_CODE (high) == INTEGER_CST);
+
+ /* This is a non-default case label, i.e. it has a value.
+
+ See if the case label is reachable within the range of
+ the index type. Remove out-of-range case values. Turn
+ case ranges into a canonical form (high > low strictly)
+ and convert the case label values to the index type.
+
+ NB: The type of gimple_switch_index() may be the promoted
+ type, but the case labels retain the original type. */
+
+ if (high)
+ {
+ /* This is a case range. Discard empty ranges.
+ If the bounds or the range are equal, turn this
+ into a simple (one-value) case. */
+ int cmp = tree_int_cst_compare (high, low);
+ if (cmp < 0)
+ remove_element = TRUE;
+ else if (cmp == 0)
+ high = NULL_TREE;
+ }
+
+ if (! high)
+ {
+ /* If the simple case value is unreachable, ignore it. */
+ if ((TREE_CODE (min_value) == INTEGER_CST
+ && tree_int_cst_compare (low, min_value) < 0)
+ || (TREE_CODE (max_value) == INTEGER_CST
+ && tree_int_cst_compare (low, max_value) > 0))
+ remove_element = TRUE;
+ else
+ low = fold_convert (index_type, low);
+ }
+ else
+ {
+ /* If the entire case range is unreachable, ignore it. */
+ if ((TREE_CODE (min_value) == INTEGER_CST
+ && tree_int_cst_compare (high, min_value) < 0)
+ || (TREE_CODE (max_value) == INTEGER_CST
+ && tree_int_cst_compare (low, max_value) > 0))
+ remove_element = TRUE;
+ else
+ {
+ /* If the lower bound is less than the index type's
+ minimum value, truncate the range bounds. */
+ if (TREE_CODE (min_value) == INTEGER_CST
+ && tree_int_cst_compare (low, min_value) < 0)
+ low = min_value;
+ low = fold_convert (index_type, low);
+
+ /* If the upper bound is greater than the index type's
+ maximum value, truncate the range bounds. */
+ if (TREE_CODE (max_value) == INTEGER_CST
+ && tree_int_cst_compare (high, max_value) > 0)
+ high = max_value;
+ high = fold_convert (index_type, high);
+
+ /* We may have folded a case range to a one-value case. */
+ if (tree_int_cst_equal (low, high))
+ high = NULL_TREE;
+ }
+ }
+
+ CASE_LOW (elt) = low;
+ CASE_HIGH (elt) = high;
+ }
+ else
+ {
+ gcc_assert (!default_case);
+ default_case = elt;
+ /* The default case must be passed separately to the
+ gimple_build_switch routine. But if DEFAULT_CASEP
+ is NULL, we do not remove the default case (it would
+ be completely lost). */
+ if (default_casep)
+ remove_element = TRUE;
+ }
+
+ if (remove_element)
+ labels.ordered_remove (i);
+ else
+ i++;
+ }
+ len = i;
+
+ if (!labels.is_empty ())
+ sort_case_labels (labels);
+
+ if (default_casep && !default_case)
+ {
+ /* If the switch has no default label, add one, so that we jump
+ around the switch body. If the labels already cover the whole
+ range of the switch index_type, add the default label pointing
+ to one of the existing labels. */
+ if (len
+ && TYPE_MIN_VALUE (index_type)
+ && TYPE_MAX_VALUE (index_type)
+ && tree_int_cst_equal (CASE_LOW (labels[0]),
+ TYPE_MIN_VALUE (index_type)))
+ {
+ tree low, high = CASE_HIGH (labels[len - 1]);
+ if (!high)
+ high = CASE_LOW (labels[len - 1]);
+ if (tree_int_cst_equal (high, TYPE_MAX_VALUE (index_type)))
+ {
+ for (i = 1; i < len; i++)
+ {
+ high = CASE_LOW (labels[i]);
+ low = CASE_HIGH (labels[i - 1]);
+ if (!low)
+ low = CASE_LOW (labels[i - 1]);
+ if (wi::add (low, 1) != high)
+ break;
+ }
+ if (i == len)
+ {
+ tree label = CASE_LABEL (labels[0]);
+ default_case = build_case_label (NULL_TREE, NULL_TREE,
+ label);
+ }
+ }
+ }
+ }
+
+ if (default_casep)
+ *default_casep = default_case;
+}
+
+/* Set the location of all statements in SEQ to LOC. */
+
+void
+gimple_seq_set_location (gimple_seq seq, location_t loc)
+{
+ for (gimple_stmt_iterator i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i))
+ gimple_set_location (gsi_stmt (i), loc);
+}