lower_intrinsics.c 22.8 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   lowering of Calls of intrinsic functions
 * @author  Michael Beck
10
 */
11
12
#include <stdbool.h>

13
#include "lowering.h"
Michael Beck's avatar
Michael Beck committed
14
#include "irop_t.h"
15
16
#include "irprog_t.h"
#include "irnode_t.h"
Michael Beck's avatar
Michael Beck committed
17
#include "irprog_t.h"
18
#include "irgwalk.h"
19
#include "ircons_t.h"
20
#include "irgmod.h"
21
#include "irgopt.h"
22
#include "irverify.h"
23
#include "pmap.h"
24
#include "array.h"
25
#include "iropt_dbg.h"
Matthias Braun's avatar
Matthias Braun committed
26
#include "panic.h"
27
#include "be.h"
28
#include "util.h"
29
#include "tv_t.h"
30

31
/** Walker environment. */
Matthias Braun's avatar
Matthias Braun committed
32
33
34
35
36
37
struct ir_intrinsics_map {
	pmap           *c_map;           /**< The intrinsic call map. */
	i_mapper_func **i_map;           /**< The intrinsic instruction map. */
	bool            part_block_used;
	unsigned        n_intrinsics;
};
38

Matthias Braun's avatar
Matthias Braun committed
39
40
ir_intrinsics_map *ir_create_intrinsics_map(i_record *list, size_t length,
                                            int part_block_used)
41
{
Matthias Braun's avatar
Matthias Braun committed
42
	size_t          n_ops = ir_get_n_opcodes();
Michael Beck's avatar
Michael Beck committed
43
	pmap           *c_map = pmap_create_ex(length);
Matthias Braun's avatar
Matthias Braun committed
44
	i_mapper_func **i_map = XMALLOCNZ(i_mapper_func*, n_ops);
Michael Beck's avatar
Michael Beck committed
45
46

	/* fill a map for faster search */
Matthias Braun's avatar
Matthias Braun committed
47
48
49
50
	for (size_t i = 0; i < length; ++i) {
		if (list[i].kind == INTRINSIC_CALL) {
			pmap_insert(c_map, list[i].i_call.i_ent,
			            (void*)list[i].i_call.i_mapper);
Michael Beck's avatar
Michael Beck committed
51
		} else {
Matthias Braun's avatar
Matthias Braun committed
52
			assert(list[i].kind == INTRINSIC_INSTR);
Michael Beck's avatar
Michael Beck committed
53
			ir_op *op = list[i].i_instr.op;
Matthias Braun's avatar
Matthias Braun committed
54
			assert(op->code < n_ops);
Michael Beck's avatar
Michael Beck committed
55

Matthias Braun's avatar
Matthias Braun committed
56
57
			assert(i_map[op->code] == NULL);
			i_map[op->code] = list[i].i_instr.i_mapper;
Michael Beck's avatar
Michael Beck committed
58
59
60
		}
	}

Matthias Braun's avatar
Matthias Braun committed
61
62
63
64
65
66
	ir_intrinsics_map *map = XMALLOCZ(ir_intrinsics_map);
	map->c_map           = c_map;
	map->i_map           = i_map;
	map->part_block_used = part_block_used;
	return map;
}
Michael Beck's avatar
Michael Beck committed
67

Matthias Braun's avatar
Matthias Braun committed
68
69
70
71
72
73
void ir_free_intrinsics_map(ir_intrinsics_map *map)
{
	free(map->i_map);
	pmap_destroy(map->c_map);
	free(map);
}
Michael Beck's avatar
Michael Beck committed
74

Matthias Braun's avatar
Matthias Braun committed
75
76
77
78
79
80
81
/**
 * walker: call all mapper functions
 */
static void call_mapper(ir_node *node, void *data)
{
	ir_intrinsics_map *map = (ir_intrinsics_map*)data;
	ir_op *op = get_irn_op(node);
82

Matthias Braun's avatar
Matthias Braun committed
83
	if (op == op_Call) {
84
		ir_node *const callee = get_Call_ptr(node);
85
		if (!is_Address(callee))
Matthias Braun's avatar
Matthias Braun committed
86
			return;
Michael Beck's avatar
Michael Beck committed
87

88
		ir_entity     *ent = get_Address_entity(callee);
Matthias Braun's avatar
Matthias Braun committed
89
		i_mapper_func *f   = pmap_get(i_mapper_func, map->c_map, ent);
90

Matthias Braun's avatar
Matthias Braun committed
91
92
93
94
95
96
97
		if (f != NULL && f(node))
			++map->n_intrinsics;
	} else {
		/* run all possible mapper */
		i_mapper_func *f = map->i_map[op->code];
		if (f != NULL && f(node)) {
			++map->n_intrinsics;
Michael Beck's avatar
Michael Beck committed
98
99
		}
	}
Matthias Braun's avatar
Matthias Braun committed
100
}
Michael Beck's avatar
Michael Beck committed
101

Matthias Braun's avatar
Matthias Braun committed
102
103
104
105
void ir_lower_intrinsics(ir_graph *irg, ir_intrinsics_map *map)
{
	if (map->part_block_used) {
		ir_reserve_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
106
		collect_phiprojs_and_start_block_nodes(irg);
Matthias Braun's avatar
Matthias Braun committed
107
108
109
110
111
112
113
114
	}
	map->n_intrinsics = 0;
	irg_walk_graph(irg, NULL, call_mapper, map);
	if (map->part_block_used)
		ir_free_resources(irg, IR_RESOURCE_IRN_LINK | IR_RESOURCE_PHI_LIST);
	if (map->n_intrinsics > 0) {
		confirm_irg_properties(irg, IR_GRAPH_PROPERTIES_NONE);
	}
115
}
116

117
118
119
120
121
122
123
124
125
/**
 * Helper function, replace the call by the given node.
 *
 * @param irn      the result node
 * @param call     the call to replace
 * @param mem      the new mem result
 * @param reg_jmp  new regular control flow, if NULL, a Jmp will be used
 * @param exc_jmp  new exception control flow, if reg_jmp == NULL, a Bad will be used
 */
126
127
static void replace_call(ir_node *irn, ir_node *call, ir_node *mem,
                         ir_node *reg_jmp, ir_node *exc_jmp)
128
{
129
130
	ir_node  *block = get_nodes_block(call);
	ir_graph *irg   = get_irn_irg(block);
131
	ir_node  *rest  = new_r_Tuple(block, 1, &irn);
Matthias Braun's avatar
Matthias Braun committed
132

133
134
135
136
137
138
139
	if (ir_throws_exception(call)) {
		if (reg_jmp == NULL) {
			reg_jmp = new_r_Jmp(block);
		}
		if (exc_jmp == NULL) {
			exc_jmp = new_r_Bad(irg, mode_X);
		}
140
141
142
143
144
145
146
		ir_node *const in[] = {
			[pn_Call_M]         = mem,
			[pn_Call_T_result]  = rest,
			[pn_Call_X_regular] = reg_jmp,
			[pn_Call_X_except]  = exc_jmp,
		};
		turn_into_tuple(call, ARRAY_SIZE(in), in);
147
148
149
150
151
152
	} else {
		assert(reg_jmp == NULL);
		assert(exc_jmp == NULL);
		assert(pn_Call_M <= pn_Call_T_result);
		assert(pn_Call_X_regular > pn_Call_T_result);
		assert(pn_Call_X_except > pn_Call_T_result);
153
154
155
156
157
		ir_node *const in[] = {
			[pn_Call_M]         = mem,
			[pn_Call_T_result]  = rest,
		};
		turn_into_tuple(call, ARRAY_SIZE(in), in);
158
	}
159
}
160

Matthias Braun's avatar
Matthias Braun committed
161
int i_mapper_abs(ir_node *call)
162
{
163
164
165
166
	ir_node  *mem      = get_Call_mem(call);
	ir_node  *block    = get_nodes_block(call);
	ir_node  *op       = get_Call_param(call, 0);
	ir_graph *irg      = get_irn_irg(call);
167
	ir_mode  *mode     = get_irn_mode(op);
168
	dbg_info *dbg      = get_irn_dbg_info(call);
169
170
171
172
	/* if we have +0/-0 then we can't implement abs with a simple Mux.
	 * (we should probably introduce a builtin. */
	if (mode_has_signed_zero(mode))
		return 0;
173

174
	ir_node  *zero     = new_r_Const_null(irg, mode);
175
	ir_node  *cmp      = new_rd_Cmp(dbg, block, op, zero, ir_relation_less);
176
177
178
	ir_node  *minus_op = new_rd_Minus(dbg, block, op, mode);
	ir_node  *mux;
	arch_allow_ifconv_func allow_ifconv = be_get_backend_param()->allow_ifconv;
Michael Beck's avatar
Michael Beck committed
179

180
	/* mux allowed by backend? */
181
	if (!allow_ifconv(cmp, op, minus_op))
182
183
184
		return 0;

	/* construct Mux */
185
	mux = new_rd_Mux(dbg, block, cmp, op, minus_op, mode);
Matthias Braun's avatar
Matthias Braun committed
186
	DBG_OPT_ALGSIM0(call, mux);
187
	replace_call(mux, call, mem, NULL, NULL);
Michael Beck's avatar
Michael Beck committed
188
	return 1;
189
}
190

Matthias Braun's avatar
Matthias Braun committed
191
int i_mapper_sqrt(ir_node *call)
192
{
Matthias Braun's avatar
Matthias Braun committed
193
194
195
	ir_node   *mem;
	ir_tarval *tv;
	ir_node   *op = get_Call_param(call, 0);
196
197
198
199
200
201
202
203

	if (!is_Const(op))
		return 0;

	tv = get_Const_tarval(op);
	if (! tarval_is_null(tv) && !tarval_is_one(tv))
		return 0;

204
	mem = get_Call_mem(call);
205
206

	/* sqrt(0) = 0, sqrt(1) = 1 */
Matthias Braun's avatar
Matthias Braun committed
207
	DBG_OPT_ALGSIM0(call, op);
208
	replace_call(op, call, mem, NULL, NULL);
209
	return 1;
210
}
211

Matthias Braun's avatar
Matthias Braun committed
212
int i_mapper_cbrt(ir_node *call)
213
{
Matthias Braun's avatar
Matthias Braun committed
214
215
216
	ir_node   *mem;
	ir_tarval *tv;
	ir_node   *op = get_Call_param(call, 0);
Michael Beck's avatar
Michael Beck committed
217
218
219
220
221

	if (!is_Const(op))
		return 0;

	tv = get_Const_tarval(op);
222
	if (!tarval_is_null(tv) && !tarval_is_one(tv) && !tarval_is_minus_one(tv))
Michael Beck's avatar
Michael Beck committed
223
224
225
226
227
		return 0;

	mem = get_Call_mem(call);

	/* cbrt(0) = 0, cbrt(1) = 1, cbrt(-1) = -1 */
Matthias Braun's avatar
Matthias Braun committed
228
	DBG_OPT_ALGSIM0(call, op);
Michael Beck's avatar
Michael Beck committed
229
230
	replace_call(op, call, mem, NULL, NULL);
	return 1;
231
}
Michael Beck's avatar
Michael Beck committed
232

Matthias Braun's avatar
Matthias Braun committed
233
int i_mapper_pow(ir_node *call)
234
{
235
236
237
238
239
240
241
	ir_node  *left    = get_Call_param(call, 0);
	ir_node  *right   = get_Call_param(call, 1);
	ir_node  *block   = get_nodes_block(call);
	ir_graph *irg     = get_irn_irg(block);
	ir_node  *reg_jmp = NULL;
	ir_node  *exc_jmp = NULL;
	ir_node  *irn;
242
	dbg_info *dbg;
243
	ir_node  *mem;
244
245
246
247
248

	if (is_Const(left) && is_Const_one(left)) {
		/* pow (1.0, x) = 1.0 */
		irn = left;
	} else if (is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
249
		ir_tarval *tv = get_Const_tarval(right);
250
251
252
		if (tarval_is_null(tv)) {
			/* pow(x, 0.0) = 1.0 */
			ir_mode *mode = get_tarval_mode(tv);
253
			irn = new_r_Const_one(irg, mode);
254
255
256
257
258
259
260
261
		} else if (tarval_is_one(tv)) {
			/* pow(x, 1.0) = x */
			irn = left;
		} else if (tarval_is_minus_one(tv)) {
			/* pow(x, -1.0) = 1/x */
			irn = NULL;
		} else
			return 0;
262
263
	} else {
		return 0;
264
265
266
267
268
269
	}

	mem = get_Call_mem(call);
	dbg = get_irn_dbg_info(call);

	if (irn == NULL) {
270
		ir_mode *result_mode = get_irn_mode(left);
271
		ir_node *div;
272

273
274
275
276
277
278
279
		ir_mode *mode             = result_mode;
		ir_mode *float_arithmetic = be_get_backend_param()->mode_float_arithmetic;
		if (float_arithmetic != NULL) {
			left = new_r_Conv(block, left, float_arithmetic);
			mode = float_arithmetic;
		}

280
		irn  = new_r_Const_one(irg, mode);
Matthias Braun's avatar
Matthias Braun committed
281
		div  = new_rd_Div(dbg, block, mem, irn, left, mode, true);
282
283
		mem  = new_r_Proj(div, mode_M, pn_Div_M);
		irn  = new_r_Proj(div, mode, pn_Div_res);
284
285
286
287
288
		if (ir_throws_exception(call)) {
			reg_jmp = new_r_Proj(div, mode_X, pn_Div_X_regular);
			exc_jmp = new_r_Proj(div, mode_X, pn_Div_X_except);
			ir_set_throws_exception(div, true);
		}
289
290
291
		if (result_mode != mode) {
			irn = new_r_Conv(block, irn, result_mode);
		}
292
	}
Matthias Braun's avatar
Matthias Braun committed
293
	DBG_OPT_ALGSIM0(call, irn);
294
295
	replace_call(irn, call, mem, reg_jmp, exc_jmp);
	return 1;
296
}
297

Matthias Braun's avatar
Matthias Braun committed
298
int i_mapper_exp(ir_node *call)
299
{
300
301
302
303
	ir_node *val  = get_Call_param(call, 0);

	if (is_Const(val) && is_Const_null(val)) {
		/* exp(0.0) = 1.0 */
304
		ir_graph *irg  = get_irn_irg(val);
305
		ir_mode *mode  = get_irn_mode(val);
306
		ir_node *irn   = new_r_Const_one(irg, mode);
307
		ir_node *mem   = get_Call_mem(call);
Matthias Braun's avatar
Matthias Braun committed
308
		DBG_OPT_ALGSIM0(call, irn);
309
		replace_call(irn, call, mem, NULL, NULL);
Michael Beck's avatar
Michael Beck committed
310
311
312
		return 1;
	}
	return 0;
313
}
Michael Beck's avatar
Michael Beck committed
314

Matthias Braun's avatar
Matthias Braun committed
315
int i_mapper_exp2(ir_node *call)
316
{
Matthias Braun's avatar
Matthias Braun committed
317
	return i_mapper_exp(call);
318
319
}

Matthias Braun's avatar
Matthias Braun committed
320
int i_mapper_exp10(ir_node *call)
321
{
Matthias Braun's avatar
Matthias Braun committed
322
	return i_mapper_exp(call);
323
324
}

325
326
327
/**
 * A mapper for mapping f(0.0) to 0.0.
 */
Matthias Braun's avatar
Matthias Braun committed
328
static int i_mapper_zero_to_zero(ir_node *call)
329
{
330
331
332
	ir_node *val  = get_Call_param(call, 0);

	if (is_Const(val) && is_Const_null(val)) {
333
		/* f(0.0) = 0.0 */
334
		ir_node *mem = get_Call_mem(call);
Matthias Braun's avatar
Matthias Braun committed
335
		DBG_OPT_ALGSIM0(call, val);
336
337
338
339
		replace_call(val, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
340
}
341

342
343
344
/**
 * A mapper for mapping f(1.0) to 0.0.
 */
Matthias Braun's avatar
Matthias Braun committed
345
static int i_mapper_one_to_zero(ir_node *call)
346
{
347
348
349
350
	ir_node *val  = get_Call_param(call, 0);

	if (is_Const(val) && is_Const_one(val)) {
		/* acos(1.0) = 0.0 */
351
352
		ir_graph *irg = get_irn_irg(val);
		ir_mode *mode = get_irn_mode(val);
353
		ir_node *irn  = new_r_Const_null(irg, mode);
354
		ir_node *mem  = get_Call_mem(call);
Matthias Braun's avatar
Matthias Braun committed
355
		DBG_OPT_ALGSIM0(call, irn);
356
357
358
359
		replace_call(irn, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
360
}
361
362
363
364
365
366

/**
 * A mapper for mapping a functions with the following characteristics:
 * f(-x)  = f(x).
 * f(0.0) = 1.0
 */
Matthias Braun's avatar
Matthias Braun committed
367
static int i_mapper_symmetric_zero_to_one(ir_node *call)
368
{
369
370
	int      changed = 0;
	ir_node *val     = get_Call_param(call, 0);
371

372
	if (is_Conv(val)) {
373
374
		ir_node *op = get_Conv_op(val);
		if (is_Minus(op)) {
375
			/* f(-x) = f(x) with strictConv */
376
377
378
379
380
			ir_node *block = get_nodes_block(call);
			ir_mode *mode  = get_irn_mode(val);
			dbg_info *dbg  = get_irn_dbg_info(val);

			op = get_Minus_op(op);
381
			val = new_rd_Conv(dbg, block, op, mode);
Matthias Braun's avatar
Matthias Braun committed
382
			DBG_OPT_ALGSIM2(call, op, call);
383
			set_Call_param(call, 0, val);
384
			changed = 1;
385
386
		}
	} else if (is_Minus(val)) {
387
		/* f(-x) = f(x) */
388
		val = get_Minus_op(val);
Matthias Braun's avatar
Matthias Braun committed
389
		DBG_OPT_ALGSIM2(call, val, call);
390
		set_Call_param(call, 0, val);
391
		changed = 1;
392
393
394
	}

	if (is_Const(val) && is_Const_null(val)) {
395
		/* f(0.0) = 1.0 */
396
		ir_graph *irg  = get_irn_irg(val);
397
		ir_mode *mode  = get_irn_mode(val);
398
		ir_node *irn   = new_r_Const_one(irg, mode);
399
		ir_node *mem   = get_Call_mem(call);
Matthias Braun's avatar
Matthias Braun committed
400
		DBG_OPT_ALGSIM0(call, irn);
401
		replace_call(irn, call, mem, NULL, NULL);
402
		changed = 1;
403
	}
404
	return changed;
405
}
406

Matthias Braun's avatar
Matthias Braun committed
407
int i_mapper_log(ir_node *call)
408
{
Michael Beck's avatar
Michael Beck committed
409
	/* log(1.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
410
	return i_mapper_one_to_zero(call);
411
}
Michael Beck's avatar
Michael Beck committed
412

Matthias Braun's avatar
Matthias Braun committed
413
int i_mapper_log2(ir_node *call)
414
415
{
	/* log2(1.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
416
	return i_mapper_one_to_zero(call);
417
418
}

Matthias Braun's avatar
Matthias Braun committed
419
int i_mapper_log10(ir_node *call)
420
421
{
	/* log10(1.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
422
	return i_mapper_one_to_zero(call);
423
424
}

Matthias Braun's avatar
Matthias Braun committed
425
int i_mapper_sin(ir_node *call)
426
{
427
	/* sin(0.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
428
	return i_mapper_zero_to_zero(call);
429
}
430

Matthias Braun's avatar
Matthias Braun committed
431
int i_mapper_cos(ir_node *call)
432
{
433
	/* cos(0.0) = 1.0, cos(-x) = x */
Matthias Braun's avatar
Matthias Braun committed
434
	return i_mapper_symmetric_zero_to_one(call);
435
}
436

Matthias Braun's avatar
Matthias Braun committed
437
int i_mapper_tan(ir_node *call)
438
{
439
	/* tan(0.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
440
	return i_mapper_zero_to_zero(call);
441
}
442

Matthias Braun's avatar
Matthias Braun committed
443
int i_mapper_asin(ir_node *call)
444
{
445
	/* asin(0.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
446
	return i_mapper_zero_to_zero(call);
447
}
448

Matthias Braun's avatar
Matthias Braun committed
449
int i_mapper_acos(ir_node *call)
450
{
451
	/* acos(1.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
452
	return i_mapper_one_to_zero(call);
453
}
454

Matthias Braun's avatar
Matthias Braun committed
455
int i_mapper_atan(ir_node *call)
456
{
457
	/* atan(0.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
458
	return i_mapper_zero_to_zero(call);
459
}
460

Matthias Braun's avatar
Matthias Braun committed
461
int i_mapper_sinh(ir_node *call)
462
{
463
	/* sinh(0.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
464
	return i_mapper_zero_to_zero(call);
465
}
466

Matthias Braun's avatar
Matthias Braun committed
467
int i_mapper_cosh(ir_node *call)
468
{
469
	/* cosh(0.0) = 1.0, cosh(-x) = x */
Matthias Braun's avatar
Matthias Braun committed
470
	return i_mapper_symmetric_zero_to_one(call);
471
}
472

Matthias Braun's avatar
Matthias Braun committed
473
int i_mapper_tanh(ir_node *call)
474
{
475
	/* tanh(0.0) = 0.0 */
Matthias Braun's avatar
Matthias Braun committed
476
	return i_mapper_zero_to_zero(call);
477
}
478

Michael Beck's avatar
Michael Beck committed
479
480
481
482
483
484
/**
 * 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
 */
Matthias Braun's avatar
Matthias Braun committed
485
486
static ir_entity *get_const_entity(ir_node *ptr)
{
487
488
	if (is_Address(ptr)) {
		ir_entity *ent = get_Address_entity(ptr);
489

Matthias Braun's avatar
Matthias Braun committed
490
		if (get_entity_linkage(ent) & IR_LINKAGE_CONSTANT) {
Michael Beck's avatar
Michael Beck committed
491
492
493
			/* a constant entity */
			return ent;
		}
494
	}
Michael Beck's avatar
Michael Beck committed
495
	return NULL;
496
}
497

498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
static ir_tarval *get_initializer_value(ir_initializer_t *const init, ir_mode *const mode)
{
	switch (get_initializer_kind(init)) {
	case IR_INITIALIZER_NULL:
		return get_mode_null(mode);

	case IR_INITIALIZER_TARVAL:
		return get_initializer_tarval_value(init);

	case IR_INITIALIZER_CONST: {
		ir_node *const irn = get_initializer_const_value(init);
		if (is_Const(irn))
			return get_Const_tarval(irn);
		break;
	}

	case IR_INITIALIZER_COMPOUND:
		break;
	}

518
	return get_tarval_unknown();
519
520
}

521
522
static bool initializer_val_is_null(ir_initializer_t *init)
{
Matthias Braun's avatar
Matthias Braun committed
523
	ir_tarval *tv;
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541

	if (get_initializer_kind(init) == IR_INITIALIZER_NULL)
		return true;

	if (get_initializer_kind(init) == IR_INITIALIZER_TARVAL) {
		tv = get_initializer_tarval_value(init);
	} else if (get_initializer_kind(init) == IR_INITIALIZER_CONST) {
		ir_node *irn = get_initializer_const_value(init);
		if (!is_Const(irn))
			return false;
		tv = get_Const_tarval(irn);
	} else {
		return false;
	}

	return tarval_is_null(tv);
}

Michael Beck's avatar
Michael Beck committed
542
543
544
545
546
/**
 * Calculate the value of strlen if possible.
 *
 * @param ent     the entity
 * @param res_tp  the result type
Michael Beck's avatar
Michael Beck committed
547
548
549
 *
 * @return a Const node containing the strlen() result or NULL
 *         if the evaluation fails
Michael Beck's avatar
Michael Beck committed
550
 */
551
static ir_node *eval_strlen(ir_graph *irg, ir_entity *ent, ir_type *res_tp)
552
{
Michael Beck's avatar
Michael Beck committed
553
554
	ir_type *tp = get_entity_type(ent);
	ir_mode *mode;
555
	ir_initializer_t *initializer;
556
557
	size_t            size;
	size_t            i;
Michael Beck's avatar
Michael Beck committed
558
559
560
561
562
563
564
565
566

	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 */
567
	if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
Michael Beck's avatar
Michael Beck committed
568
569
		return NULL;

570
571
572
573
574
575
576
577
	initializer = get_entity_initializer(ent);
	if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
		return NULL;

	size = get_initializer_compound_n_entries(initializer);
	for (i = 0; i < size; ++i) {
		ir_initializer_t *val = get_initializer_compound_value(initializer, i);
		if (initializer_val_is_null(val)) {
578
			return new_r_Const_long(irg, get_type_mode(res_tp), i);
579
		}
Michael Beck's avatar
Michael Beck committed
580
	}
581

Michael Beck's avatar
Michael Beck committed
582
	return NULL;
583
}
Michael Beck's avatar
Michael Beck committed
584

Matthias Braun's avatar
Matthias Braun committed
585
int i_mapper_strlen(ir_node *call)
586
{
Michael Beck's avatar
Michael Beck committed
587
588
	ir_node *s     = get_Call_param(call, 0);
	ir_entity *ent = get_const_entity(s);
Michael Beck's avatar
Michael Beck committed
589
590

	/* FIXME: this cannot handle constant strings inside struct initializers yet */
Michael Beck's avatar
Michael Beck committed
591
592
593
594
595
596
	if (ent != NULL) {
		/* a constant entity */
		ir_type *tp = get_Call_type(call);
		ir_node *irn;

		tp  = get_method_res_type(tp, 0);
597
		irn = eval_strlen(get_irn_irg(call), ent, tp);
Michael Beck's avatar
Michael Beck committed
598
599
600

		if (irn) {
			ir_node *mem = get_Call_mem(call);
Matthias Braun's avatar
Matthias Braun committed
601
			DBG_OPT_ALGSIM0(call, irn);
Michael Beck's avatar
Michael Beck committed
602
603
604
605
606
			replace_call(irn, call, mem, NULL, NULL);
			return 1;
		}
	}
	return 0;
607
}
Michael Beck's avatar
Michael Beck committed
608

Michael Beck's avatar
Michael Beck committed
609
610
611
612
613
614
615
616
617
618
/**
 * 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
 */
619
620
static ir_node *eval_strcmp(ir_graph *irg, ir_entity *left, ir_entity *right,
                            ir_type *res_tp)
621
{
622
623
624
625
626
627
628
629
	ir_type          *tp;
	ir_mode          *mode;
	ir_initializer_t *init_l;
	ir_initializer_t *init_r;
	size_t            size_l;
	size_t            size_r;
	size_t            size;
	size_t            i;
Michael Beck's avatar
Michael Beck committed
630
631
632
633
634
635
636
637
638
639

	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 */
640
	if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
Michael Beck's avatar
Michael Beck committed
641
642
643
644
645
646
647
648
649
650
651
		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 */
652
	if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
Michael Beck's avatar
Michael Beck committed
653
		return NULL;
Michael Beck's avatar
Michael Beck committed
654

655
656
657
658
659
660
661
662
	init_l = get_entity_initializer(left);
	init_r = get_entity_initializer(right);
	if (get_initializer_kind(init_l) != IR_INITIALIZER_COMPOUND ||
	    get_initializer_kind(init_r) != IR_INITIALIZER_COMPOUND)
		return NULL;

	size_l = get_initializer_compound_n_entries(init_l);
	size_r = get_initializer_compound_n_entries(init_r);
Christoph Mallon's avatar
Christoph Mallon committed
663
	size   = MIN(size_l, size_r);
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681

	for (i = 0; i != size; ++i) {
		ir_initializer_t *const val_l = get_initializer_compound_value(init_l, i);
		ir_tarval        *const tv_l  = get_initializer_value(val_l, mode);
		ir_initializer_t *const val_r = get_initializer_compound_value(init_r, i);
		ir_tarval        *const tv_r  = get_initializer_value(val_r, mode);

		if (!tarval_is_constant(tv_l) || !tarval_is_constant(tv_r))
			return NULL;

		if (tv_l != tv_r) {
			ir_mode   *const res_mode = get_type_mode(res_tp);
			ir_tarval *const res_l    = tarval_convert_to(tv_l, res_mode);
			ir_tarval *const res_r    = tarval_convert_to(tv_r, res_mode);
			ir_tarval *const tv       = tarval_sub(res_l, res_r, res_mode);
			return new_r_Const(irg, tv);
		}

682
683
		if (tarval_is_null(tv_l))
			return new_r_Const_null(irg, get_type_mode(res_tp));
684
	}
685

Michael Beck's avatar
Michael Beck committed
686
	return NULL;
687
}
Michael Beck's avatar
Michael Beck committed
688
689
690
691
692
693
694
695

/**
 * Checks if an entity represents the empty string.
 *
 * @param ent     the entity
 *
 * @return non-zero if ent represents the empty string
 */
696
697
static int is_empty_string(ir_entity *ent)
{
698
699
700
701
	ir_type          *tp = get_entity_type(ent);
	ir_mode          *mode;
	ir_initializer_t *initializer;
	ir_initializer_t *init0;
Michael Beck's avatar
Michael Beck committed
702
703
704
705
706
707
708
709
710

	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 */
711
	if (!mode_is_int(mode) || get_mode_size_bits(mode) != 8)
Michael Beck's avatar
Michael Beck committed
712
713
		return 0;

714
715
716
717
718
	initializer = get_entity_initializer(ent);
	if (get_initializer_kind(initializer) != IR_INITIALIZER_COMPOUND)
		return 0;

	if (get_initializer_compound_n_entries(initializer) < 1)
Michael Beck's avatar
Michael Beck committed
719
720
		return 0;

721
	init0 = get_initializer_compound_value(initializer, 0);
722
	return initializer_val_is_null(init0);
723
}
Michael Beck's avatar
Michael Beck committed
724

Matthias Braun's avatar
Matthias Braun committed
725
int i_mapper_strcmp(ir_node *call)
726
{
Michael Beck's avatar
Michael Beck committed
727
728
729
730
731
	ir_node   *left    = get_Call_param(call, 0);
	ir_node   *right   = get_Call_param(call, 1);
	ir_node   *irn     = NULL;
	ir_node   *exc     = NULL;
	ir_node   *reg     = NULL;
732
	ir_type   *call_tp = get_Call_type(call);
Michael Beck's avatar
Michael Beck committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
	ir_type   *res_tp  = get_method_res_type(call_tp, 0);
	ir_entity *ent_l, *ent_r;
	ir_type   *char_tp;
	ir_node   *v;

	/* do some type checks first */
	if (! is_Primitive_type(res_tp))
		return 0;
	char_tp = get_method_param_type(call_tp, 0);
	if (char_tp != get_method_param_type(call_tp, 1))
		return 0;
	if (! is_Pointer_type(char_tp))
		return 0;
	char_tp = get_pointer_points_to_type(char_tp);

748
	ir_node *mem = get_Call_mem(call);
Michael Beck's avatar
Michael Beck committed
749
750
	if (left == right) {
		/* a strcmp(s, s) ==> 0 */
751
752
		ir_graph *irg = get_irn_irg(call);
		ir_mode *mode = get_type_mode(res_tp);
Michael Beck's avatar
Michael Beck committed
753

754
		irn = new_r_Const_null(irg, mode);
Matthias Braun's avatar
Matthias Braun committed
755
		DBG_OPT_ALGSIM0(call, irn);
Michael Beck's avatar
Michael Beck committed
756
757
758
759
760
761
762
763
		replace_call(irn, call, mem, NULL, NULL);
		return 1;
	}
	ent_l = get_const_entity(left);
	ent_r = get_const_entity(right);

	if (ent_l != NULL && ent_r != NULL) {
		/* both entities are const, try to evaluate */
764
		irn = eval_strcmp(get_irn_irg(call), ent_l, ent_r, res_tp);
Michael Beck's avatar
Michael Beck committed
765
766
767
768
769
770
771
772
773
	} else if (ent_l != NULL) {
		if (is_empty_string(ent_l)) {
			/* s strcmp("", s) ==> -(*s)*/
			v = right;
			goto replace_by_call;
		}
	} else if (ent_r != NULL) {
		if (is_empty_string(ent_r)) {
			/* s strcmp(s, "") ==> (*s) */
774
			ir_node  *block;
Michael Beck's avatar
Michael Beck committed
775
776
777
778
779
780
781
782
783
784
785
			dbg_info *dbg;
			ir_mode  *mode;

			v = left;
replace_by_call:
			mem   = get_Call_mem(call);
			block = get_nodes_block(call);
			dbg   = get_irn_dbg_info(call);
			mode  = get_type_mode(char_tp);

			/* replace the strcmp by (*x) */
786
			irn = new_rd_Load(dbg, block, mem, v, mode, char_tp, cons_none);
787
788
			mem = new_r_Proj(irn, mode_M, pn_Load_M);
			irn = new_r_Proj(irn, mode, pn_Load_res);
789
790
791
792
793
794
795
796
			if (ir_throws_exception(call)) {
				exc = new_r_Proj(irn, mode_X, pn_Load_X_except);
				reg = new_r_Proj(irn, mode_X, pn_Load_X_regular);
				ir_set_throws_exception(irn, true);
			} else {
				exc = NULL;
				reg = NULL;
			}
Michael Beck's avatar
Michael Beck committed
797
798
799

			/* conv to the result mode */
			mode = get_type_mode(res_tp);
800
			irn  = new_rd_Conv(dbg, block, irn, mode);
Michael Beck's avatar
Michael Beck committed
801
802
803

			if (v == right) {
				/* negate in the ("", s) case */
804
				irn = new_rd_Minus(dbg, block, irn, mode);
Michael Beck's avatar
Michael Beck committed
805
806
807
			}
		}
	}
Michael Beck's avatar
Michael Beck committed
808
809

	if (irn != NULL) {
Matthias Braun's avatar
Matthias Braun committed
810
		DBG_OPT_ALGSIM0(call, irn);
Michael Beck's avatar
Michael Beck committed
811
812
813
814
		replace_call(irn, call, mem, reg, exc);
		return 1;
	}

Michael Beck's avatar
Michael Beck committed
815
	return 0;
816
}
Michael Beck's avatar
Michael Beck committed
817

Matthias Braun's avatar
Matthias Braun committed
818
int i_mapper_strncmp(ir_node *call)
819
{
Michael Beck's avatar
Michael Beck committed
820
821
822
823
824
825
826
827
	ir_node *left  = get_Call_param(call, 0);
	ir_node *right = get_Call_param(call, 1);
	ir_node *len   = get_Call_param(call, 2);
	ir_node *irn;

	if (left == right || (is_Const(len) && is_Const_null(len))) {
		/* a strncmp(s, s, len) ==> 0 OR
		   a strncmp(a, b, 0) ==> 0 */
828
		ir_graph  *irg     = get_irn_irg(call);
Michael Beck's avatar
Michael Beck committed
829
830
		ir_node   *mem     = get_Call_mem(call);
		ir_node   *adr     = get_Call_ptr(call);
831
		ir_entity *ent     = get_Address_entity(adr);
Michael Beck's avatar
Michael Beck committed
832
833
834
835
		ir_type   *call_tp = get_entity_type(ent);
		ir_type   *res_tp  = get_method_res_type(call_tp, 0);
		ir_mode   *mode    = get_type_mode(res_tp);

836
		irn = new_r_Const_null(irg, mode);
Matthias Braun's avatar
Matthias Braun committed
837
		DBG_OPT_ALGSIM0(call, irn);
Michael Beck's avatar
Michael Beck committed
838
839
840
841
		replace_call(irn, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
842
}
Michael Beck's avatar
Michael Beck committed
843

Matthias Braun's avatar
Matthias Braun committed
844
int i_mapper_strcpy(ir_node *call)
845
{
846
847
848
849
850
851
852
853
	ir_node *dst = get_Call_param(call, 0);
	ir_node *src = get_Call_param(call, 1);

	if (dst == src) {
		/* a strcpy(d, s) ==> d */
		ir_node *mem = get_Call_mem(call);
		ir_node *dst = get_Call_param(call, 0);

Matthias Braun's avatar
Matthias Braun committed
854
		DBG_OPT_ALGSIM0(call, dst);
855
856
857
858
		replace_call(dst, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
859
}
860

Matthias Braun's avatar
Matthias Braun committed
861
int i_mapper_memcpy(ir_node *call)
862
{
863
864
	ir_node *dst = get_Call_param(call, 0);
	ir_node *src = get_Call_param(call, 1);
865
	ir_node *len = get_Call_param(call, 2);
866

867
868
	if (dst == src || (is_Const(len) && is_Const_null(len))) {
		/* a memcpy(d, d, len) ==> d OR
Matthias Braun's avatar
Matthias Braun committed
869
		   a memcpy(d, s, 0) ==> d */
870
871
		ir_node *mem = get_Call_mem(call);

Matthias Braun's avatar
Matthias Braun committed
872
		DBG_OPT_ALGSIM0(call, dst);
873
874
875
876
		replace_call(dst, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
877
}
878

Matthias Braun's avatar
Matthias Braun committed
879
int i_mapper_memmove(ir_node *call)
880
{
881
882
883
884
885
886
	ir_node *dst = get_Call_param(call, 0);
	ir_node *src = get_Call_param(call, 1);
	ir_node *len = get_Call_param(call, 2);

	if (dst == src || (is_Const(len) && is_Const_null(len))) {
		/* a memmove(d, d, len) ==> d OR
Matthias Braun's avatar
Matthias Braun committed
887
		   a memmove(d, s, 0) ==> d */
888
889
		ir_node *mem = get_Call_mem(call);

Matthias Braun's avatar
Matthias Braun committed
890
		DBG_OPT_ALGSIM0(call, dst);
891
892
893
894
		replace_call(dst, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
895
}
896

Matthias Braun's avatar
Matthias Braun committed
897
int i_mapper_memset(ir_node *call)
898
{
899
900
901
902
903
904
905
	ir_node *len = get_Call_param(call, 2);

	if (is_Const(len) && is_Const_null(len)) {
		/* a memset(d, C, 0) ==> d */
		ir_node *mem = get_Call_mem(call);
		ir_node *dst = get_Call_param(call, 0);

Matthias Braun's avatar
Matthias Braun committed
906
		DBG_OPT_ALGSIM0(call, dst);
907
908
909
910
		replace_call(dst, call, mem, NULL, NULL);
		return 1;
	}
	return 0;
911
}
Michael Beck's avatar
Michael Beck committed
912

Matthias Braun's avatar
Matthias Braun committed
913
int i_mapper_memcmp(ir_node *call)
914
{
915
916
917
918
919
920
921
922
	ir_node *left  = get_Call_param(call, 0);
	ir_node *right = get_Call_param(call, 1);
	ir_node *len   = get_Call_param(call, 2);
	ir_node *irn;

	if (left == right || (is_Const(len) && is_Const_null(len))) {
		/* a memcmp(s, s, len) ==> 0 OR
		   a memcmp(a, b, 0) ==> 0 */
923
		ir_graph  *irg     = get_irn_irg(call);
924
925
		ir_node   *mem     = get_Call_mem(call);
		ir_node   *adr     = get_Call_ptr(call);
926
		ir_entity *ent     = get_Address_entity(adr);
927
928
929
930
		ir_type   *call_tp = get_entity_type(ent);
		ir_type   *res_tp  = get_method_res_type(call_tp, 0);
		ir_mode   *mode    = get_type_mode(res_tp);

931
		irn = new_r_Const_null(irg, mode);
Matthias Braun's avatar
Matthias Braun committed
932
		DBG_OPT_ALGSIM0(call, irn);