Example #1
0
/**
 * Process a call node.
 *
 * @param call    A ir_node to be checked.
 * @param callee  The entity of the callee
 * @param hmap    The quadruple-set containing the calls with constant parameters
 */
static void process_call(ir_node *call, ir_entity *callee, q_set *hmap)
{
	/* TODO
	 * Beware: We cannot clone variadic parameters as well as the
	 * last non-variadic one, which might be needed for the va_start()
	 * magic. */

	/* In this for loop we collect the calls, that have
	   a constant parameter. */
	size_t const n_params = get_Call_n_params(call);
	for (size_t i = n_params; i-- > 0;) {
		ir_node *const call_param = get_Call_param(call, i);
		if (is_Const(call_param)) {
			/* we have found a Call to collect and we save the information
			 * we need.*/
			if (!hmap->map)
				hmap->map = new_pset(entry_cmp, 8);

			entry_t *const key = OALLOC(&hmap->obst, entry_t);
			key->q.ent   = callee;
			key->q.pos   = i;
			key->q.tv    = get_Const_tarval(call_param);
			key->q.calls = NULL;
			key->weight  = 0.0F;
			key->next    = NULL;

			/* Insert entry or get existing equivalent entry */
			entry_t *const entry = (entry_t*)pset_insert(hmap->map, key, hash_entry(key));
			/* Free memory if entry already is in set */
			if (entry != key)
				obstack_free(&hmap->obst, key);

			/* add the call to the list */
			if (!entry->q.calls) {
				entry->q.calls = NEW_ARR_F(ir_node*, 1);
				entry->q.calls[0] = call;
			} else {
				ARR_APP1(ir_node *, entry->q.calls, call);
			}
		}
Example #2
0
/**
 * Walk recursive the successors of a graph argument
 * with mode reference and mark if it will be read,
 * written or stored.
 *
 * @param arg   The graph argument with mode reference,
 *             that must be checked.
 */
static ptr_access_kind analyze_arg(ir_node *arg, ptr_access_kind bits)
{
	/* We must visit a node once to avoid endless recursion.*/
	mark_irn_visited(arg);

	for (int i = get_irn_n_outs(arg); i-- > 0; ) {
		ir_node *succ = get_irn_out(arg, i);
		if (irn_visited(succ))
			continue;

		/* We should not walk over the memory edge.*/
		if (get_irn_mode(succ) == mode_M)
			continue;

		/* If we reach with the recursion a Call node and our reference
		   isn't the address of this Call we accept that the reference will
		   be read and written if the graph of the method represented by
		   "Call" isn't computed else we analyze that graph. If our
		   reference is the address of this
		   Call node that mean the reference will be read.*/
		switch (get_irn_opcode(succ)) {

		case iro_Call: {
			ir_node *ptr  = get_Call_ptr(succ);

			if (ptr == arg) {
				/* Hmm: not sure what this is, most likely a read */
				bits |= ptr_access_read;
			} else {
				ir_entity *meth_ent;

				if (is_SymConst_addr_ent(ptr)) {
					meth_ent = get_SymConst_entity(ptr);

					for (int p = get_Call_n_params(succ); p-- > 0; ) {
						if (get_Call_param(succ, p) == arg) {
							/* an arg can be used more than once ! */
							bits |= get_method_param_access(meth_ent, p);
						}
					}
				} else if (is_Sel(ptr) && get_irp_callee_info_state() == irg_callee_info_consistent) {
					/* is be a polymorphic call but callee information is available */
					size_t n_params = get_Call_n_params(succ);

					/* simply look into ALL possible callees */
					for (int c = get_Call_n_callees(succ); c-- > 0; ) {
						meth_ent = get_Call_callee(succ, c);

						/* unknown_entity is used to signal that we don't know what is called */
						if (is_unknown_entity(meth_ent)) {
							bits |= ptr_access_all;
							break;
						}

						for (size_t p = n_params; p-- > 0; ) {
							if (get_Call_param(succ, p) == arg) {
								/* an arg can be used more than once ! */
								bits |= get_method_param_access(meth_ent, p);
							}
						}
					}
				} else /* can do anything */
					bits |= ptr_access_all;
			}

			/* search stops here anyway */
			continue;
		}
		case iro_Store:
			/* We have reached a Store node => the reference is written or stored. */
			if (get_Store_ptr(succ) == arg) {
				/* written to */
				bits |= ptr_access_write;
			} else {
				/* stored itself */
				bits |= ptr_access_store;
			}

			/* search stops here anyway */
			continue;

		case iro_Load:
			/* We have reached a Load node => the reference is read. */
			bits |= ptr_access_read;

			/* search stops here anyway */
			continue;

		case iro_Conv:
			/* our address is casted into something unknown. Break our search. */
			bits = ptr_access_all;
			break;

		default:
			break;
		}

		/* If we know that, the argument will be read, write and stored, we
		   can break the recursion.*/
		if (bits == ptr_access_all) {
			bits = ptr_access_all;
			break;
		}

		/*
		 * A calculation that do not lead to a reference mode ends our search.
		 * This is dangerous: It would allow to cast into integer and that cast back ...
		 * so, when we detect a Conv we go mad, see the Conv case above.
		 */
		if (!mode_is_reference(get_irn_mode(succ)))
			continue;

		/* follow further the address calculation */
		bits = analyze_arg(succ, bits);
	}
	set_irn_link(arg, NULL);
	return bits;
}