+/**
+ * Return the const entity that is accessed through the pointer ptr or
+ * NULL if there is no entity (or the entity is not constant).
+ *
+ * @param ptr the pointer
+ */
+static ir_entity *get_const_entity(ir_node *ptr) {
+ /* FIXME: this cannot handle constant strings inside struct initializers yet */
+ if (is_Global(ptr)) {
+ ir_entity *ent = get_Global_entity(ptr);
+
+ if (get_entity_variability(ent) == variability_constant) {
+ /* a constant entity */
+ return ent;
+ }
+ }
+ return NULL;
+} /* get_const_entity */
+
+/**
+ * Calculate the value of strlen if possible.
+ *
+ * @param ent the entity
+ * @param res_tp the result type
+ *
+ * @return a Const node containing the strlen() result or NULL
+ * if the evaluation fails
+ */
+static ir_node *eval_strlen(ir_entity *ent, ir_type *res_tp) {
+ ir_type *tp = get_entity_type(ent);
+ ir_mode *mode;
+ int i, n, len = -1;
+
+ if (! is_Array_type(tp))
+ return NULL;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return NULL;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return NULL;
+
+ n = get_compound_ent_n_values(ent);
+ for (i = 0; i < n; ++i) {
+ ir_node *irn = get_compound_ent_value(ent, i);
+
+ if (! is_Const(irn))
+ return NULL;
+
+ if (is_Const_null(irn)) {
+ /* found the length */
+ len = i;
+ break;
+ }
+ }
+ if (len >= 0) {
+ tarval *tv = new_tarval_from_long(len, get_type_mode(res_tp));
+ return new_Const_type(tv, res_tp);
+ }
+ return NULL;
+} /* eval_strlen */
+
+/* A mapper for strlen */
+int i_mapper_strlen(ir_node *call, void *ctx) {
+ ir_node *s = get_Call_param(call, 0);
+ ir_entity *ent = get_const_entity(s);
+
+ (void) ctx;
+
+ /* FIXME: this cannot handle constant strings inside struct initializers yet */
+ if (ent != NULL) {
+ /* a constant entity */
+ ir_type *tp = get_Call_type(call);
+ ir_node *irn;
+
+ tp = get_method_res_type(tp, 0);
+ irn = eval_strlen(ent, tp);
+
+ if (irn) {
+ ir_node *mem = get_Call_mem(call);
+ DBG_OPT_ALGSIM0(call, irn, FS_OPT_RTS_STRLEN);
+ replace_call(irn, call, mem, NULL, NULL);
+ return 1;
+ }
+ }
+ return 0;
+} /* i_mapper_strlen */
+
+/**
+ * Calculate the value of strlen if possible.
+ *
+ * @param left the left entity
+ * @param right the right entity
+ * @param res_tp the result type
+ *
+ * @return a Const node containing the strcmp() result or NULL
+ * if the evaluation fails
+ */
+static ir_node *eval_strcmp(ir_entity *left, ir_entity *right, ir_type *res_tp) {
+ ir_type *tp;
+ ir_mode *mode;
+ int i, n, n_r, res;
+
+ tp = get_entity_type(left);
+ if (! is_Array_type(tp))
+ return NULL;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return NULL;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return NULL;
+
+ tp = get_entity_type(right);
+ if (! is_Array_type(tp))
+ return NULL;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return NULL;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return NULL;
+
+ n = get_compound_ent_n_values(left);
+ n_r = get_compound_ent_n_values(right);
+ if (n_r < n)
+ n = n_r;
+ for (i = 0; i < n; ++i) {
+ ir_node *irn;
+ long v_l, v_r;
+ tarval *tv;
+
+ irn = get_compound_ent_value(left, i);
+ if (! is_Const(irn))
+ return NULL;
+ tv = get_Const_tarval(irn);
+ v_l = get_tarval_long(tv);
+
+ irn = get_compound_ent_value(right, i);
+ if (! is_Const(irn))
+ return NULL;
+ tv = get_Const_tarval(irn);
+ v_r = get_tarval_long(tv);
+
+ if (v_l < v_r) {
+ res = -1;
+ break;
+ }
+ if (v_l > v_r) {
+ res = +1;
+ break;
+ }
+
+ if (v_l == 0) {
+ res = 0;
+ break;
+ }
+ }
+ if (i < n) {
+ /* we found an end */
+ tarval *tv = new_tarval_from_long(res, get_type_mode(res_tp));
+ return new_Const_type(tv, res_tp);
+ }
+ return NULL;
+} /* eval_strcmp */
+
+/**
+ * Checks if an entity represents the empty string.
+ *
+ * @param ent the entity
+ *
+ * @return non-zero if ent represents the empty string
+ */
+static int is_empty_string(ir_entity *ent) {
+ ir_type *tp = get_entity_type(ent);
+ ir_mode *mode;
+ int n;
+ ir_node *irn;
+
+ if (! is_Array_type(tp))
+ return 0;
+ tp = get_array_element_type(tp);
+ if (! is_Primitive_type(tp))
+ return 0;
+ mode = get_type_mode(tp);
+
+ /* FIXME: This is too restrict, as the type char might be more the 8bits */
+ if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
+ return 0;
+
+ n = get_compound_ent_n_values(ent);
+ if (n < 1)
+ return 0;
+ irn = get_compound_ent_value(ent, 0);
+
+ return is_Const(irn) && is_Const_null(irn);
+} /* is_empty_string */
+