lower_dw.c 68.5 KB
Newer Older
Michael Beck's avatar
BugFix:    
Michael Beck committed
1
2
3
4
5
6
7
8
9
/*
 * Project:     libFIRM
 * File name:   ir/lower/lower_dw.c
 * Purpose:     Lower Double word operations, ie Mode L -> I.
 * Author:      Michael Beck
 * Created:		8.10.2004
 * CVS-ID:      $Id$
 * Copyright:   (c) 1998-2006 Universitt Karlsruhe
 * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif

#include <assert.h>

#include "irnode_t.h"
#include "irgraph_t.h"
#include "irmode_t.h"
#include "iropt_t.h"
#include "irgmod.h"
#include "tv_t.h"
#include "dbginfo_t.h"
#include "iropt_dbg.h"
#include "irflag_t.h"
#include "firmstat.h"
#include "irgwalk.h"
#include "ircons.h"
#include "lower_dw.h"
#include "irflag.h"
#include "irtools.h"
#include "debug.h"
#include "set.h"
#include "pmap.h"
#include "pdeq.h"
Christian Würdig's avatar
Christian Würdig committed
49
#include "irdump.h"
50

Michael Beck's avatar
Michael Beck committed
51
52
53
54
/** A map from mode to a primitive type. */
static pmap *prim_types;

/** A map from (op, imode, omode) to Intrinsic functions entities. */
55
56
static set *intrinsic_fkt;

Michael Beck's avatar
Michael Beck committed
57
58
59
/** A map from (imode, omode) to conv function types. */
static set *conv_types;

60
61
62
63
/** A map from a method type to its lowered type. */
static pmap *lowered_type;

/** The types for the binop and unop intrinsics. */
Christian Würdig's avatar
Christian Würdig committed
64
static ir_type *binop_tp_u, *binop_tp_s, *unop_tp_u, *unop_tp_s, *shiftop_tp_u, *shiftop_tp_s, *tp_s, *tp_u;
65
66
67
68
69

/** the debug handle */
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

/**
Michael Beck's avatar
Michael Beck committed
70
 * An entry in the (op, imode, omode) -> entity map.
71
72
73
 */
typedef struct _op_mode_entry {
	const ir_op   *op;    /**< the op */
Michael Beck's avatar
Michael Beck committed
74
75
76
	const ir_mode *imode; /**< the input mode */
	const ir_mode *omode; /**< the output mode */
	entity        *ent;   /**< the associated entity of this (op, imode, omode) triple */
77
78
} op_mode_entry_t;

Michael Beck's avatar
Michael Beck committed
79
80
81
82
83
84
85
86
87
/**
 * An entry in the (imode, omode) -> tp map.
 */
typedef struct _conv_tp_entry {
	const ir_mode *imode; /**< the input mode */
	const ir_mode *omode; /**< the output mode */
	ir_type       *mtd;   /**< the associated method type of this (imode, omode) pair */
} conv_tp_entry_t;

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * Every double word node will be replaced,
 * we need some store to hold the replacement:
 */
typedef struct _node_entry_t {
	ir_node *low_word;        /**< the low word */
	ir_node *high_word;       /**< the high word */
} node_entry_t;

enum lower_flags {
	MUST_BE_LOWERED = 1,   /**< graph must be lowered */
	CF_CHANGED      = 2,   /**< control flow was changed */
};

/**
 * The lower environment.
 */
typedef struct _lower_env_t {
	node_entry_t **entries;       /**< entries per node */
	struct obstack obst;          /**< an obstack holding the temporary data */
Michael Beck's avatar
BugFix:    
Michael Beck committed
108
109
	tarval   *tv_mode_bytes;      /**< a tarval containing the number of bytes in the lowered modes */
	tarval   *tv_mode_bits;       /**< a tarval containing the number of bits in the lowered modes */
110
111
112
113
114
115
116
	pdeq     *waitq;              /**< a wait queue of all nodes that must be handled later */
	pmap     *proj_2_block;       /**< a map from ProjX to its destination blocks */
	const lwrdw_param_t *params;  /**< transformation parameter */
	unsigned flags;               /**< some flags */
	int      n_entries;           /**< number of entries */
} lower_env_t;

Michael Beck's avatar
Michael Beck committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
 * Get a primitive mode for a mode.
 */
static ir_type *get_primitive_type(ir_mode *mode) {
	pmap_entry *entry = pmap_find(prim_types, mode);
	ir_type *tp;
	char buf[64];

	if (entry)
		return entry->value;

	snprintf(buf, sizeof(buf), "_prim_%s", get_mode_name(mode));
	tp = new_type_primitive(new_id_from_str(buf), mode);

	pmap_insert(prim_types, mode, tp);
	return tp;
133
}  /* get_primitive_type */
Michael Beck's avatar
Michael Beck committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

/**
 * Create a method type for a Conv emulation from imode to omode.
 */
static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode, lower_env_t *env) {
	conv_tp_entry_t key, *entry;
	ir_type *mtd;

	key.imode = imode;
	key.omode = omode;
	key.mtd   = NULL;

	entry = set_insert(conv_types, &key, sizeof(key), HASH_PTR(imode) ^ HASH_PTR(omode));
	if (! entry->mtd) {
		int n_param = 1, n_res = 1;
		char buf[64];

		if (imode == env->params->high_signed || imode == env->params->high_unsigned)
			n_param = 2;
		if (omode == env->params->high_signed || omode == env->params->high_unsigned)
			n_res = 2;

		/* create a new one */
		snprintf(buf, sizeof(buf), "LConv%s%s", get_mode_name(imode), get_mode_name(omode));
		mtd = new_type_method(new_id_from_str(buf), n_param, n_res);

		/* set param types and result types */
		n_param = 0;
		if (imode == env->params->high_signed) {
			set_method_param_type(mtd, n_param++, tp_s);
			set_method_param_type(mtd, n_param++, tp_s);
Michael Beck's avatar
BugFix:    
Michael Beck committed
165
		} else if (imode == env->params->high_unsigned) {
Michael Beck's avatar
Michael Beck committed
166
167
			set_method_param_type(mtd, n_param++, tp_u);
			set_method_param_type(mtd, n_param++, tp_u);
Michael Beck's avatar
BugFix:    
Michael Beck committed
168
		} else {
Michael Beck's avatar
Michael Beck committed
169
170
			ir_type *tp = get_primitive_type(imode);
			set_method_param_type(mtd, n_param++, tp);
Michael Beck's avatar
BugFix:    
Michael Beck committed
171
		}  /* if */
Michael Beck's avatar
Michael Beck committed
172
173
174
175
176

		n_res = 0;
		if (omode == env->params->high_signed) {
			set_method_res_type(mtd, n_res++, tp_s);
			set_method_res_type(mtd, n_res++, tp_s);
Michael Beck's avatar
BugFix:    
Michael Beck committed
177
		} else if (omode == env->params->high_unsigned) {
Michael Beck's avatar
Michael Beck committed
178
179
			set_method_res_type(mtd, n_res++, tp_u);
			set_method_res_type(mtd, n_res++, tp_u);
Michael Beck's avatar
BugFix:    
Michael Beck committed
180
		} else {
Michael Beck's avatar
Michael Beck committed
181
182
			ir_type *tp = get_primitive_type(omode);
			set_method_res_type(mtd, n_res++, tp);
Michael Beck's avatar
BugFix:    
Michael Beck committed
183
		}  /* if */
Michael Beck's avatar
Michael Beck committed
184
		entry->mtd = mtd;
Michael Beck's avatar
BugFix:    
Michael Beck committed
185
	} else {
Michael Beck's avatar
Michael Beck committed
186
		mtd = entry->mtd;
Michael Beck's avatar
BugFix:    
Michael Beck committed
187
	}  /* if */
Michael Beck's avatar
Michael Beck committed
188
	return mtd;
189
}  /* get_conv_type */
Michael Beck's avatar
Michael Beck committed
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 * Add an additional control flow input to a block.
 * Patch all Phi nodes. The new Phi inputs are copied from
 * old input number nr.
 */
static void add_block_cf_input_nr(ir_node *block, int nr, ir_node *cf)
{
	int i, arity = get_irn_arity(block);
	ir_node **in, *phi;

	assert(nr < arity);

	NEW_ARR_A(ir_node *, in, arity + 1);
	for (i = 0; i < arity; ++i)
		in[i] = get_irn_n(block, i);
	in[i] = cf;

	set_irn_in(block, i + 1, in);

	for (phi = get_irn_link(block); phi; phi = get_irn_link(phi)) {
		for (i = 0; i < arity; ++i)
			in[i] = get_irn_n(phi, i);
		in[i] = in[nr];
		set_irn_in(phi, i + 1, in);
Michael Beck's avatar
BugFix:    
Michael Beck committed
215
	}  /* for */
216
}  /* add_block_cf_input_nr */
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

/**
 * Add an additional control flow input to a block.
 * Patch all Phi nodes. The new Phi inputs are copied from
 * old input from cf tmpl.
 */
static void add_block_cf_input(ir_node *block, ir_node *tmpl, ir_node *cf)
{
	int i, arity = get_irn_arity(block);
	int nr = 0;

	for (i = 0; i < arity; ++i) {
		if (get_irn_n(block, i) == tmpl) {
			nr = i;
			break;
Michael Beck's avatar
BugFix:    
Michael Beck committed
232
233
		}  /* if */
	}  /* for */
234
235
	assert(i < arity);
	add_block_cf_input_nr(block, nr, cf);
236
}  /* add_block_cf_input */
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

/**
 * Return the "operational" mode of a Firm node.
 */
static ir_mode *get_irn_op_mode(ir_node *node)
{
	switch (get_irn_opcode(node)) {
	case iro_Load:
		return get_Load_mode(node);
	case iro_Store:
		return get_irn_mode(get_Store_value(node));
	case iro_DivMod:
		return get_irn_mode(get_DivMod_left(node));
	case iro_Div:
		return get_irn_mode(get_Div_left(node));
	case iro_Mod:
		return get_irn_mode(get_Mod_left(node));
	case iro_Cmp:
		return get_irn_mode(get_Cmp_left(node));
	default:
		return get_irn_mode(node);
Michael Beck's avatar
BugFix:    
Michael Beck committed
258
	}  /* switch */
259
}  /* get_irn_op_mode */
260
261

/**
262
 * Walker, prepare the node links.
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
 */
static void prepare_links(ir_node *node, void *env)
{
	lower_env_t  *lenv = env;
	ir_mode      *mode = get_irn_op_mode(node);
	node_entry_t *link;
	int          i;

	if (mode == lenv->params->high_signed ||
		mode == lenv->params->high_unsigned) {
		/* ok, found a node that will be lowered */
		link = obstack_alloc(&lenv->obst, sizeof(*link));

		memset(link, 0, sizeof(*link));

		lenv->entries[get_irn_idx(node)] = link;
		lenv->flags |= MUST_BE_LOWERED;
Michael Beck's avatar
BugFix:    
Michael Beck committed
280
	} else if (get_irn_op(node) == op_Conv) {
281
282
283
284
285
286
		/* Conv nodes have two modes */
		ir_node *pred = get_Conv_op(node);
		mode = get_irn_mode(pred);

		if (mode == lenv->params->high_signed ||
			mode == lenv->params->high_unsigned) {
Michael Beck's avatar
BugFix:    
Michael Beck committed
287
			/* must lower this node either but don't need a link */
288
			lenv->flags |= MUST_BE_LOWERED;
Michael Beck's avatar
BugFix:    
Michael Beck committed
289
		}  /* if */
290
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
291
	}  /* if */
292
293
294
295
296
297
298
299

	if (is_Proj(node)) {
		/* link all Proj nodes to its predecessor:
		   Note that Tuple Proj's and its Projs are linked either. */
		ir_node *pred = get_Proj_pred(node);

		set_irn_link(node, get_irn_link(pred));
		set_irn_link(pred, node);
Michael Beck's avatar
BugFix:    
Michael Beck committed
300
	} else if (is_Phi(node)) {
301
302
303
304
305
		/* link all Phi nodes to its block */
		ir_node *block = get_nodes_block(node);

		set_irn_link(node, get_irn_link(block));
		set_irn_link(block, node);
Michael Beck's avatar
BugFix:    
Michael Beck committed
306
	} else if (is_Block(node)) {
307
308
309
310
311
312
		/* fill the Proj -> Block map */
		for (i = get_Block_n_cfgpreds(node) - 1; i >= 0; --i) {
			ir_node *pred = get_Block_cfgpred(node, i);

			if (is_Proj(pred))
				pmap_insert(lenv->proj_2_block, pred, node);
Michael Beck's avatar
BugFix:    
Michael Beck committed
313
314
		}  /* for */
	}  /* if */
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

	/* handle support for Psi nodes */
	if (mode == mode_b || get_irn_op(node) == op_Psi) {
		for (i = get_irn_arity(node) - 1; i >= 0; --i) {
			ir_node *proj = get_irn_n(node, i);

			if (is_Proj(proj)) {
				ir_node *cmp = get_Proj_pred(proj);

				if (is_Cmp(cmp)) {
					ir_node *l = get_Cmp_left(cmp);
					mode = get_irn_mode(l);

					if (mode == lenv->params->high_signed ||
						mode == lenv->params->high_unsigned) {
						/* ok, found a node that will be lowered */
						ir_node *nproj;
						int rem = get_optimize();

						set_optimize(0);
						nproj = new_rd_Proj(
							get_irn_dbg_info(proj),
							current_ir_graph,
							get_nodes_block(proj),
							cmp,
							get_irn_mode(proj),
							get_Proj_proj(proj));
						set_optimize(rem);

						set_irn_n(node, i, nproj);
						lenv->flags |= MUST_BE_LOWERED;
					}  /* if */
				}  /* if */
			}  /* if */
		}  /* for */
	}  /* if */
351
}  /* prepare_links */
352
353
354
355
356
357
358
359
360
361

/**
 * Translate a Constant: create two.
 */
static void lower_Const(ir_node *node, ir_mode *mode, lower_env_t *env) {
	tarval   *tv, *tv_l, *tv_h;
	ir_node  *low, *high;
	dbg_info *dbg = get_irn_dbg_info(node);
	ir_node  *block = get_nodes_block(node);
	int      idx;
362
	ir_graph *irg = current_ir_graph;
363
364
365
366

	tv   = get_Const_tarval(node);

	tv_l = tarval_convert_to(tv, mode);
367
	low  = new_rd_Const(dbg, irg, block, mode, tv_l);
368

Michael Beck's avatar
BugFix:    
Michael Beck committed
369
	tv_h = tarval_convert_to(tarval_shrs(tv, env->tv_mode_bits), mode);
370
	high = new_rd_Const(dbg, irg, block, mode, tv_h);
371
372
373
374
375

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	env->entries[idx]->low_word  = low;
	env->entries[idx]->high_word = high;
376
}  /* lower_Const */
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394

/**
 * Translate a Load: create two.
 */
static void lower_Load(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_graph *irg = current_ir_graph;
	ir_node  *adr = get_Load_ptr(node);
	ir_node  *mem = get_Load_mem(node);
	ir_node  *low, *high, *proj;
	dbg_info *dbg;
	ir_node  *block = get_nodes_block(node);
	int      idx;

	if (env->params->little_endian) {
		low  = adr;
		high = new_r_Add(irg, block, adr,
			new_r_Const(irg, block, get_tarval_mode(env->tv_mode_bytes), env->tv_mode_bytes),
			get_irn_mode(adr));
Michael Beck's avatar
BugFix:    
Michael Beck committed
395
	} else {
396
397
398
399
		low  = new_r_Add(irg, block, adr,
			new_r_Const(irg, block, get_tarval_mode(env->tv_mode_bytes), env->tv_mode_bytes),
			get_irn_mode(adr));
		high = adr;
Michael Beck's avatar
BugFix:    
Michael Beck committed
400
	}  /* if */
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434

	/* create two loads */
	dbg  = get_irn_dbg_info(node);
	low  = new_rd_Load(dbg, irg, block, mem, low,  mode);
	proj = new_r_Proj(irg, block, low, mode_M, pn_Load_M);
	high = new_rd_Load(dbg, irg, block, proj, high, mode);

	set_Load_volatility(low,  get_Load_volatility(node));
	set_Load_volatility(high, get_Load_volatility(node));

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	env->entries[idx]->low_word  = low;
	env->entries[idx]->high_word = high;

	for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
		idx = get_irn_idx(proj);

		switch (get_Proj_proj(proj)) {
		case pn_Load_M:         /* Memory result. */
			/* put it to the second one */
			set_Proj_pred(proj, high);
			break;
		case pn_Load_X_except:  /* Execution result if exception occurred. */
			/* put it to the first one */
			set_Proj_pred(proj, low);
			break;
		case pn_Load_res:       /* Result of load operation. */
			assert(idx < env->n_entries);
			env->entries[idx]->low_word  = new_r_Proj(irg, block, low, mode, pn_Load_res);
			env->entries[idx]->high_word = new_r_Proj(irg, block, high, mode, pn_Load_res);
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
435
		}  /* switch */
436
437
438
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
439
	}  /* for */
440
}  /* lower_Load */
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460

/**
 * Translate a Store: create two.
 */
static void lower_Store(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_graph *irg;
	ir_node  *block, *adr, *mem;
	ir_node  *low, *high, *irn, *proj;
	dbg_info *dbg;
	int      idx;
	node_entry_t *entry;

	irn = get_Store_value(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
461
	}  /* if */
462
463
464
465
466
467
468
469
470
471
472

	irg = current_ir_graph;
	adr = get_Store_ptr(node);
	mem = get_Store_mem(node);
	block = get_nodes_block(node);

	if (env->params->little_endian) {
		low  = adr;
		high = new_r_Add(irg, block, adr,
			new_r_Const(irg, block, get_tarval_mode(env->tv_mode_bytes), env->tv_mode_bytes),
			get_irn_mode(adr));
Michael Beck's avatar
BugFix:    
Michael Beck committed
473
	} else {
474
475
476
477
		low  = new_r_Add(irg, block, adr,
			new_r_Const(irg, block, get_tarval_mode(env->tv_mode_bytes), env->tv_mode_bytes),
			get_irn_mode(adr));
		high = adr;
Michael Beck's avatar
BugFix:    
Michael Beck committed
478
	}  /* if */
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507

	/* create two Stores */
	dbg = get_irn_dbg_info(node);
	low  = new_rd_Store(dbg, irg, block, mem, low,  entry->low_word);
	proj = new_r_Proj(irg, block, low, mode_M, pn_Store_M);
	high = new_rd_Store(dbg, irg, block, proj, high, entry->high_word);

	set_Store_volatility(low,  get_Store_volatility(node));
	set_Store_volatility(high, get_Store_volatility(node));

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	env->entries[idx]->low_word  = low;
	env->entries[idx]->high_word = high;

	for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
		idx = get_irn_idx(proj);

		switch (get_Proj_proj(proj)) {
		case pn_Store_M:         /* Memory result. */
			/* put it to the second one */
			set_Proj_pred(proj, high);
			break;
		case pn_Store_X_except:  /* Execution result if exception occurred. */
			/* put it to the first one */
			set_Proj_pred(proj, low);
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
508
		}  /* switch */
509
510
511
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
512
	}  /* for */
513
}  /* lower_Store */
514
515
516

/**
 * Return a node containing the address of the intrinsic emulation function.
Michael Beck's avatar
Michael Beck committed
517
518
519
520
521
522
523
 *
 * @param method  the method type of the emulation function
 * @param op      the emulated ir_op
 * @param imode   the input mode of the emulated opcode
 * @param omode   the output mode of the emulated opcode
 * @param block   where the new mode is created
 * @param env     the lower environment
524
 */
Michael Beck's avatar
Michael Beck committed
525
526
527
static ir_node *get_intrinsic_address(ir_type *method, ir_op *op,
                                      ir_mode *imode, ir_mode *omode,
                                      ir_node *block, lower_env_t *env) {
528
529
530
531
	symconst_symbol sym;
	entity *ent;
	op_mode_entry_t key, *entry;

Michael Beck's avatar
Michael Beck committed
532
533
534
535
	key.op    = op;
	key.imode = imode;
	key.omode = omode;
	key.ent   = NULL;
536

Michael Beck's avatar
Michael Beck committed
537
538
	entry = set_insert(intrinsic_fkt, &key, sizeof(key),
				HASH_PTR(op) ^ HASH_PTR(imode) ^ (HASH_PTR(omode) << 8));
539
540
	if (! entry->ent) {
		/* create a new one */
Michael Beck's avatar
Michael Beck committed
541
		ent = env->params->create_intrinsic(method, op, imode, omode, env->params->ctx);
542
543
544

		assert(ent && "Intrinsic creator must return an entity");
		entry->ent = ent;
Michael Beck's avatar
BugFix:    
Michael Beck committed
545
	} else {
546
		ent = entry->ent;
Michael Beck's avatar
BugFix:    
Michael Beck committed
547
	}  /* if */
548
549
	sym.entity_p = ent;
	return new_r_SymConst(current_ir_graph, block, sym, symconst_addr_ent);
550
}  /* get_intrinsic_address */
551
552
553
554
555
556
557
558
559

/**
 * Translate a Div.
 *
 * Create an intrinsic Call.
 */
static void lower_Div(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *irn, *call, *proj;
	ir_node  *in[4];
Michael Beck's avatar
Michael Beck committed
560
	ir_mode  *opmode;
561
562
563
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
564
	ir_graph *irg;
565
566
567
568
569
570
571
572
573
574
	node_entry_t *entry;

	irn   = get_Div_left(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
575
	}  /* if */
576
577
578
579
580
581
582
583
584
585
586
587

	in[0] = entry->low_word;
	in[1] = entry->high_word;

	irn   = get_Div_right(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
588
	}  /* if */
589
590
591
592

	in[2] = entry->low_word;
	in[3] = entry->high_word;

593
	dbg   = get_irn_dbg_info(node);
594
	block = get_nodes_block(node);
595
	irg   = current_ir_graph;
596
597

	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
Michael Beck's avatar
Michael Beck committed
598
599
	opmode = get_irn_op_mode(node);
	irn = get_intrinsic_address(mtp, get_irn_op(node), opmode, opmode, block, env);
600
	call = new_rd_Call(dbg, irg, block, get_Div_mem(node),
601
602
		irn, 4, in, mtp);
	set_irn_pinned(call, get_irn_pinned(node));
603
	irn = new_r_Proj(irg, block, call, mode_T, pn_Call_T_result);
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

	for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
		switch (get_Proj_proj(proj)) {
		case pn_Div_M:         /* Memory result. */
			/* reroute to the call */
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_M_except);
			break;
		case pn_Div_X_except:  /* Execution result if exception occurred. */
			/* reroute to the call */
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_X_except);
			break;
		case pn_Div_res:       /* Result of computation. */
			idx = get_irn_idx(proj);
			assert(idx < env->n_entries);
			env->entries[idx]->low_word  = new_r_Proj(current_ir_graph, block, irn, mode, 0);
			env->entries[idx]->high_word = new_r_Proj(current_ir_graph, block, irn, mode, 1);
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
625
		}  /* switch */
626
627
628
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
629
	}  /* for */
630
}  /* lower_Div */
631
632
633
634
635
636
637
638
639

/**
 * Translate a Mod.
 *
 * Create an intrinsic Call.
 */
static void lower_Mod(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *proj, *irn, *call;
	ir_node  *in[4];
Michael Beck's avatar
Michael Beck committed
640
	ir_mode  *opmode;
641
642
643
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
644
	ir_graph *irg;
645
646
647
648
649
650
651
652
653
654
	node_entry_t *entry;

	irn   = get_Mod_left(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
655
	}  /* if */
656
657
658
659
660
661
662
663
664
665
666
667

	in[0] = entry->low_word;
	in[1] = entry->high_word;

	irn   = get_Mod_right(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
668
	}  /* if */
669
670
671
672

	in[2] = entry->low_word;
	in[3] = entry->high_word;

673
	dbg   = get_irn_dbg_info(node);
674
	block = get_nodes_block(node);
675
	irg   = current_ir_graph;
676
677

	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
Michael Beck's avatar
Michael Beck committed
678
679
	opmode = get_irn_op_mode(node);
	irn = get_intrinsic_address(mtp, get_irn_op(node), opmode, opmode, block, env);
680
	call = new_rd_Call(dbg, irg, block, get_Mod_mem(node),
681
682
		irn, 4, in, mtp);
	set_irn_pinned(call, get_irn_pinned(node));
683
	irn = new_r_Proj(irg, block, call, mode_T, pn_Call_T_result);
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699

	for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
		switch (get_Proj_proj(proj)) {
		case pn_Mod_M:         /* Memory result. */
			/* reroute to the call */
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_M_except);
			break;
		case pn_Mod_X_except:  /* Execution result if exception occurred. */
			/* reroute to the call */
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_X_except);
			break;
		case pn_Mod_res:       /* Result of computation. */
			idx = get_irn_idx(proj);
			assert(idx < env->n_entries);
700
701
			env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, mode, 0);
			env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode, 1);
702
703
704
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
705
		}  /* switch */
706
707
708
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
709
	}  /* for */
710
}  /* lower_Mod */
711
712
713
714
715
716
717
718
719

/**
 * Translate a DivMod.
 *
 * Create two intrinsic Calls.
 */
static void lower_DivMod(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *proj, *irn, *mem, *callDiv, *callMod, *resDiv, *resMod;
	ir_node  *in[4];
Michael Beck's avatar
Michael Beck committed
720
	ir_mode  *opmode;
721
722
723
724
725
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
	node_entry_t *entry;
	unsigned flags = 0;
726
	ir_graph *irg;
727
728
729
730
731
732
733

	/* check if both results are needed */
	for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
		switch (get_Proj_proj(proj)) {
		case pn_DivMod_res_div: flags |= 1; break;
		case pn_DivMod_res_mod: flags |= 2; break;
		default: break;
Michael Beck's avatar
BugFix:    
Michael Beck committed
734
735
		}  /* switch */
	}  /* for */
736
737
738
739
740
741
742
743
744

	irn   = get_DivMod_left(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
745
	}  /* if */
746
747
748
749
750
751
752
753
754
755
756
757

	in[0] = entry->low_word;
	in[1] = entry->high_word;

	irn   = get_DivMod_right(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
758
	}  /* if */
759
760
761
762

	in[2] = entry->low_word;
	in[3] = entry->high_word;

763
	dbg   = get_irn_dbg_info(node);
764
	block = get_nodes_block(node);
765
	irg   = current_ir_graph;
766
767
768
769
770
771

	mem = get_DivMod_mem(node);

	callDiv = callMod = NULL;
	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
	if (flags & 1) {
Michael Beck's avatar
Michael Beck committed
772
773
		opmode = get_irn_op_mode(node);
		irn = get_intrinsic_address(mtp, op_Div, opmode, opmode, block, env);
774
		callDiv = new_rd_Call(dbg, irg, block, mem,
775
776
			irn, 4, in, mtp);
		set_irn_pinned(callDiv, get_irn_pinned(node));
777
		resDiv = new_r_Proj(irg, block, callDiv, mode_T, pn_Call_T_result);
Michael Beck's avatar
BugFix:    
Michael Beck committed
778
	}  /* if */
779
780
	if (flags & 2) {
		if (flags & 1)
781
			mem = new_r_Proj(irg, block, callDiv, mode_M, pn_Call_M);
Michael Beck's avatar
Michael Beck committed
782
783
		opmode = get_irn_op_mode(node);
		irn = get_intrinsic_address(mtp, op_Mod, opmode, opmode, block, env);
784
		callMod = new_rd_Call(dbg, irg, block, mem,
785
786
			irn, 4, in, mtp);
		set_irn_pinned(callMod, get_irn_pinned(node));
787
		resMod = new_r_Proj(irg, block, callMod, mode_T, pn_Call_T_result);
Michael Beck's avatar
BugFix:    
Michael Beck committed
788
	}  /* if */
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804

	for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
		switch (get_Proj_proj(proj)) {
		case pn_DivMod_M:         /* Memory result. */
			/* reroute to the first call */
			set_Proj_pred(proj, callDiv ? callDiv : (callMod ? callMod : mem));
			set_Proj_proj(proj, pn_Call_M_except);
			break;
		case pn_DivMod_X_except:  /* Execution result if exception occurred. */
			/* reroute to the first call */
			set_Proj_pred(proj, callDiv ? callDiv : (callMod ? callMod : mem));
			set_Proj_proj(proj, pn_Call_X_except);
			break;
		case pn_DivMod_res_div:   /* Result of Div. */
			idx = get_irn_idx(proj);
			assert(idx < env->n_entries);
805
806
			env->entries[idx]->low_word  = new_r_Proj(irg, block, resDiv, mode, 0);
			env->entries[idx]->high_word = new_r_Proj(irg, block, resDiv, mode, 1);
807
808
809
			break;
		case pn_DivMod_res_mod:   /* Result of Mod. */
			idx = get_irn_idx(proj);
810
811
			env->entries[idx]->low_word  = new_r_Proj(irg, block, resMod, mode, 0);
			env->entries[idx]->high_word = new_r_Proj(irg, block, resMod, mode, 1);
812
813
814
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
815
		}  /* switch */
816
817
818
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
819
	}  /* for */
820
}  /* lower_DivMod */
821
822
823
824
825
826
827
828
829
830
831
832

/**
 * Translate a Binop.
 *
 * Create an intrinsic Call.
 */
static void lower_Binop(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *irn;
	ir_node  *in[4];
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
833
	ir_graph *irg;
834
835
836
837
838
839
840
841
842
843
	node_entry_t *entry;

	irn   = get_binop_left(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
844
	}  /* if */
845
846
847
848
849
850
851
852
853
854
855
856

	in[0] = entry->low_word;
	in[1] = entry->high_word;

	irn   = get_binop_right(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
857
	}  /* if */
858
859
860
861

	in[2] = entry->low_word;
	in[3] = entry->high_word;

862
	dbg   = get_irn_dbg_info(node);
863
	block = get_nodes_block(node);
864
	irg   = current_ir_graph;
865
866

	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
Michael Beck's avatar
Michael Beck committed
867
	irn = get_intrinsic_address(mtp, get_irn_op(node), mode, mode, block, env);
868
	irn = new_rd_Call(dbg, irg, block, get_irg_no_mem(current_ir_graph),
869
870
		irn, 4, in, mtp);
	set_irn_pinned(irn, get_irn_pinned(node));
871
	irn = new_r_Proj(irg, block, irn, mode_T, pn_Call_T_result);
872
873
874

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
875
876
877
	env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, mode, 0);
	env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode, 1);
}  /* lower_Binop */
878

Christian Würdig's avatar
Christian Würdig committed
879
880
881
882
883
884
885
886
887
888
889
/**
 * Translate a Shiftop.
 *
 * Create an intrinsic Call.
 */
static void lower_Shiftop(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *irn;
	ir_node  *in[3];
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
890
	ir_graph *irg;
Christian Würdig's avatar
Christian Würdig committed
891
892
893
894
895
896
897
898
899
900
	node_entry_t *entry;

	irn   = get_binop_left(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
901
	}  /* if */
Christian Würdig's avatar
Christian Würdig committed
902
903
904
905
906
907
908
909
910

	in[0] = entry->low_word;
	in[1] = entry->high_word;

	/* The shift count is always mode_Iu in firm, so there is no need for lowering */
	in[2] = get_binop_right(node);

	dbg   = get_irn_dbg_info(node);
	block = get_nodes_block(node);
911
	irg  = current_ir_graph;
Christian Würdig's avatar
Christian Würdig committed
912
913
914

	mtp = mode_is_signed(mode) ? shiftop_tp_s : shiftop_tp_u;
	irn = get_intrinsic_address(mtp, get_irn_op(node), mode, mode, block, env);
915
	irn = new_rd_Call(dbg, irg, block, get_irg_no_mem(current_ir_graph),
Christian Würdig's avatar
Christian Würdig committed
916
917
		irn, 3, in, mtp);
	set_irn_pinned(irn, get_irn_pinned(node));
918
	irn = new_r_Proj(irg, block, irn, mode_T, pn_Call_T_result);
Christian Würdig's avatar
Christian Würdig committed
919
920
921

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
922
923
924
	env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, mode, 0);
	env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode, 1);
}  /* lower_Shiftop */
Christian Würdig's avatar
Christian Würdig committed
925

Michael Beck's avatar
Michael Beck committed
926
927
928
929
/**
 * Translate a Shr and handle special cases.
 */
static void lower_Shr(ir_node *node, ir_mode *mode, lower_env_t *env) {
930
931
	ir_node  *right = get_Shr_right(node);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
932
933
934
935
936

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
		tarval *tv = get_Const_tarval(right);

		if (tarval_is_long(tv) &&
937
			get_tarval_long(tv) >= get_mode_size_bits(mode)) {
Michael Beck's avatar
Michael Beck committed
938
939
			ir_node *block = get_nodes_block(node);
			ir_node *left = get_Shr_left(node);
940
941
			ir_node *c;
			long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
Michael Beck's avatar
Michael Beck committed
942
943
944
945
946
			int idx = get_irn_idx(left);

			left = env->entries[idx]->high_word;
			idx = get_irn_idx(node);

947
			if (shf_cnt > 0) {
948
949
				c = new_r_Const_long(irg, block, mode_Iu, shf_cnt);
				env->entries[idx]->low_word = new_r_Shr(irg, block, left, c, mode);
Michael Beck's avatar
BugFix:    
Michael Beck committed
950
			} else {
951
				env->entries[idx]->low_word = left;
Michael Beck's avatar
BugFix:    
Michael Beck committed
952
			}  /* if */
953
			env->entries[idx]->high_word = new_r_Const(irg, block, mode, get_mode_null(mode));
Michael Beck's avatar
Michael Beck committed
954
955

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
956
957
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
958
	lower_Shiftop(node, mode, env);
959
}  /* lower_Shr */
Michael Beck's avatar
Michael Beck committed
960
961
962
963
964

/**
 * Translate a Shl and handle special cases.
 */
static void lower_Shl(ir_node *node, ir_mode *mode, lower_env_t *env) {
965
966
	ir_node  *right = get_Shl_right(node);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
967
968
969
970
971

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
		tarval *tv = get_Const_tarval(right);

		if (tarval_is_long(tv) &&
972
			get_tarval_long(tv) >= get_mode_size_bits(mode)) {
Michael Beck's avatar
Michael Beck committed
973
974
			ir_node *block = get_nodes_block(node);
			ir_node *left = get_Shl_left(node);
975
976
			ir_node *c;
			long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
Michael Beck's avatar
Michael Beck committed
977
978
979
980
981
			int idx = get_irn_idx(left);

			left = env->entries[idx]->low_word;
			idx = get_irn_idx(node);

982
			if (shf_cnt > 0) {
983
984
				c = new_r_Const_long(irg, block, mode_Iu, shf_cnt);
				env->entries[idx]->high_word = new_r_Shl(irg, block, left, c, mode);
Michael Beck's avatar
BugFix:    
Michael Beck committed
985
			} else {
986
				env->entries[idx]->high_word = left;
Michael Beck's avatar
BugFix:    
Michael Beck committed
987
			}  /* if */
988
			env->entries[idx]->low_word  = new_r_Const(irg, block, mode, get_mode_null(mode));
Michael Beck's avatar
Michael Beck committed
989
990

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
991
992
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
993
	lower_Shiftop(node, mode, env);
994
}  /* lower_Shl */
Michael Beck's avatar
Michael Beck committed
995
996
997
998
999

/**
 * Translate a Shrs and handle special cases.
 */
static void lower_Shrs(ir_node *node, ir_mode *mode, lower_env_t *env) {
1000
1001
	ir_node  *right = get_Shrs_right(node);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
1002
1003
1004
1005
1006

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
		tarval *tv = get_Const_tarval(right);

		if (tarval_is_long(tv) &&
1007
			get_tarval_long(tv) >= get_mode_size_bits(mode)) {
Michael Beck's avatar
Michael Beck committed
1008
1009
			ir_node *block = get_nodes_block(node);
			ir_node *left = get_Shrs_left(node);
1010
			long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
Michael Beck's avatar
Michael Beck committed
1011
1012
1013
1014
1015
1016
			ir_node *c;
			int idx = get_irn_idx(left);

			left = env->entries[idx]->high_word;
			idx = get_irn_idx(node);

1017
			if (shf_cnt > 0) {
1018
1019
				c = new_r_Const_long(irg, block, mode_Iu, shf_cnt);
				env->entries[idx]->low_word = new_r_Shrs(irg, block, left, c, mode);
Michael Beck's avatar
BugFix:    
Michael Beck committed
1020
			} else {
1021
				env->entries[idx]->low_word = left;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1022
			}  /* if */
1023
1024
			c = new_r_Const_long(irg, block, mode_Iu, get_mode_size_bits(mode) - 1);
			env->entries[idx]->high_word = new_r_Shrs(irg, block, left, c, mode);
Michael Beck's avatar
Michael Beck committed
1025
1026

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1027
1028
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
1029
	lower_Shiftop(node, mode, env);
1030
}  /* lower_Shrs */
Michael Beck's avatar
Michael Beck committed
1031
1032
1033
1034
1035

/**
 * Translate a Rot and handle special cases.
 */
static void lower_Rot(ir_node *node, ir_mode *mode, lower_env_t *env) {
1036
	ir_node  *right = get_Rot_right(node);
Michael Beck's avatar
Michael Beck committed
1037
1038
1039
1040
1041

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
		tarval *tv = get_Const_tarval(right);

		if (tarval_is_long(tv) &&
1042
			get_tarval_long(tv) == get_mode_size_bits(mode)) {
Michael Beck's avatar
Michael Beck committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
			ir_node *left = get_Rot_left(node);
			ir_node *h, *l;
			int idx = get_irn_idx(left);

			l = env->entries[idx]->low_word;
			h = env->entries[idx]->high_word;
			idx = get_irn_idx(node);

			env->entries[idx]->low_word  = h;
			env->entries[idx]->high_word = l;

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1055
1056
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
1057
	lower_Shiftop(node, mode, env);
1058
1059
}  /* lower_Rot */

1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
/**
 * Translate an Unop.
 *
 * Create an intrinsic Call.
 */
static void lower_Unop(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *irn;
	ir_node  *in[2];
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
1071
	ir_graph *irg;
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
	node_entry_t *entry;

	irn   = get_unop_op(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1082
	}  /* if */
1083
1084
1085
1086

	in[0] = entry->low_word;
	in[1] = entry->high_word;

1087
	dbg   = get_irn_dbg_info(node);
1088
	block = get_nodes_block(node);
1089
	irg   = current_ir_graph;
1090
1091

	mtp = mode_is_signed(mode) ? unop_tp_s : unop_tp_u;
Michael Beck's avatar
Michael Beck committed
1092
	irn = get_intrinsic_address(mtp, get_irn_op(node), mode, mode, block, env);
1093
	irn = new_rd_Call(dbg, irg, block, get_irg_no_mem(current_ir_graph),
1094
1095
		irn, 2, in, mtp);
	set_irn_pinned(irn, get_irn_pinned(node));
1096
	irn = new_r_Proj(irg, block, irn, mode_T, pn_Call_T_result);
1097
1098
1099

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
1100
1101
1102
	env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, mode, 0);
	env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode, 1);
}  /* lower_Unop */
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114

/**
 * Translate a logical Binop.
 *
 * Create two logical Binops.
 */
static void lower_Binop_logical(ir_node *node, ir_mode *mode, lower_env_t *env,
								ir_node *(*constr_rd)(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode) ) {
	ir_node  *block, *irn;
	ir_node  *lop_l, *lop_h, *rop_l, *rop_h;
	dbg_info *dbg;
	int      idx;
1115
	ir_graph *irg;
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
	node_entry_t *entry;

	irn   = get_binop_left(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1126
	}  /* if */
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138

	lop_l = entry->low_word;
	lop_h = entry->high_word;

	irn   = get_binop_right(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1139
	}  /* if */
1140
1141
1142
1143
1144
1145
1146
1147
1148

	rop_l = entry->low_word;
	rop_h = entry->high_word;

	dbg = get_irn_dbg_info(node);
	block = get_nodes_block(node);

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
1149
1150
1151
1152
	irg = current_ir_graph;
	env->entries[idx]->low_word  = constr_rd(dbg, irg, block, lop_l, rop_l, mode);
	env->entries[idx]->high_word = constr_rd(dbg, irg, block, lop_h, rop_h, mode);
}  /* lower_Binop_logical */
1153

Michael Beck's avatar
Michael Beck committed
1154
/** create a logical operation tranformation */
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
#define lower_logical(op)                                                \
static void lower_##op(ir_node *node, ir_mode *mode, lower_env_t *env) { \
	lower_Binop_logical(node, mode, env, new_rd_##op);                   \
}

lower_logical(And)
lower_logical(Or)
lower_logical(Eor)

/**
 * Translate a Not.
 *
 * Create two logical Nots.
 */
static void lower_Not(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *irn;
	ir_node  *op_l, *op_h;
	dbg_info *dbg;
	int      idx;
1174
	ir_graph *irg;
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
	node_entry_t *entry;

	irn   = get_Not_op(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1185
	}  /* if */
1186
1187
1188
1189

	op_l = entry->low_word;
	op_h = entry->high_word;

1190
	dbg   = get_irn_dbg_info(node);
1191
	block = get_nodes_block(node);
1192
	irg   = current_ir_graph;
1193
1194
1195
1196
1197

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	env->entries[idx]->low_word  = new_rd_Not(dbg, current_ir_graph, block, op_l, mode);
	env->entries[idx]->high_word = new_rd_Not(dbg, current_ir_graph, block, op_h, mode);
1198
}  /* lower_Not */
1199

1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
/**
 * Translate a Minus.
 *
 * Create two Minus'.
 */
static void lower_Minus(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node  *block, *irn;
	ir_node  *op_l, *op_h;
	dbg_info *dbg;
	int      idx;
	ir_graph *irg;
	node_entry_t *entry;

	irn   = get_Minus_op(node);
	entry = env->entries[get_irn_idx(irn)];
	assert(entry);

	if (! entry->low_word) {
		/* not ready yet, wait */
		pdeq_putr(env->waitq, node);
		return;
	}  /* if */

	op_l = entry->low_word;
	op_h = entry->high_word;

	dbg   = get_irn_dbg_info(node);
	block = get_nodes_block(node);
	irg   = current_ir_graph;

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	env->entries[idx]->low_word  = new_rd_Minus(dbg, current_ir_graph, block, op_l, mode);
	env->entries[idx]->high_word = new_rd_Minus(dbg, current_ir_graph, block, op_h, mode);
}  /* lower_Minus */

1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
/**
 * Translate a Cond.
 */
static void lower_Cond(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node *cmp, *left, *right, *block;
	ir_node *sel = get_Cond_selector(node);
	ir_mode *m = get_irn_mode(sel);
	int     idx;

	if (m == mode_b) {
		node_entry_t *lentry, *rentry;
		ir_node  *proj, *projT = NULL, *projF = NULL;
		ir_node  *new_bl, *cmpH, *cmpL, *irn;
		ir_node  *projHF, *projHT;
		ir_node  *dst_blk;
		ir_graph *irg;
		pn_Cmp   pnc;
		dbg_info *dbg;

		cmp   = get_Proj_pred(sel);
		left  = get_Cmp_left(cmp);
		idx   = get_irn_idx(left);
		lentry = env->entries[idx];

Michael Beck's avatar
BugFix:    
Michael Beck committed
1260
		if (! lentry) {
1261
1262
			/* a normal Cmp */
			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1263
		}  /* if */
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273

		right = get_Cmp_right(cmp);
		idx   = get_irn_idx(right);
		rentry = env->entries[idx];
		assert(rentry);

		if (! lentry->low_word || !rentry->low_word) {
			/* not yet ready */
			pdeq_putr(env->waitq, node);
			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1274
		}  /* if */
1275
1276
1277
1278
1279
1280
1281
1282

		/* all right, build the code */
		for (proj = get_irn_link(node); proj; proj = get_irn_link(proj)) {
			long proj_nr = get_Proj_proj(proj);

			if (proj_nr == pn_Cond_true) {
				assert(projT == NULL && "more than one Proj(true)");
				projT = proj;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1283
			} else {
1284
1285
1286
				assert(proj_nr == pn_Cond_false);
				assert(projF == NULL && "more than one Proj(false)");
				projF = proj;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1287
			}  /* if */
1288
			mark_irn_visited(proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
1289
		}  /* for */
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
		assert(projT && projF);

		/* create a new high compare */
		block = get_nodes_block(cmp);
		dbg   = get_irn_dbg_info(cmp);
		irg   = current_ir_graph;

		cmpH = new_rd_Cmp(dbg, irg, block, lentry->high_word, rentry->high_word);

		pnc = get_Proj_proj(sel);
		if (pnc == pn_Cmp_Eq) {
			/* simple case:a == b <==> a_h == b_h && a_l == b_l */
			pmap_entry *entry = pmap_find(env->proj_2_block, projF);

			assert(entry);
			dst_blk = entry->value;

			irn = new_r_Proj(irg, block, cmpH, mode_b, pn_Cmp_Eq);
			dbg = get_irn_dbg_info(node);
			irn = new_rd_Cond(dbg, irg, block, irn);

			projHF = new_r_Proj(irg, block, irn, mode_X, pn_Cond_false);
			mark_irn_visited(projHF);
			exchange(projF, projHF);

			projHT = new_r_Proj(irg, block, irn, mode_X, pn_Cond_true);
			mark_irn_visited(projHT);

			new_bl = new_r_Block(irg, 1, &projHT);

			dbg   = get_irn_dbg_info(cmp);
			cmpL = new_rd_Cmp(dbg, irg, new_bl, lentry->low_word, rentry->low_word);
			irn = new_r_Proj(irg, new_bl, cmpL, mode_b, pn_Cmp_Eq);
			dbg = get_irn_dbg_info(node);
			irn = new_rd_Cond(dbg, irg, new_bl, irn);

			proj = new_r_Proj(irg, new_bl, irn, mode_X, pn_Cond_false);
			mark_irn_visited(proj);
			add_block_cf_input(dst_blk, projHF, proj);

			proj = new_r_Proj(irg, new_bl, irn, mode_X, pn_Cond_true);
			mark_irn_visited(proj);
			exchange(projT, proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
1333
		} else if (pnc == pn_Cmp_Lg) {
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
			/* simple case:a != b <==> a_h != b_h || a_l != b_l */
			pmap_entry *entry = pmap_find(env->proj_2_block, projT);

			assert(entry);
			dst_blk = entry->value;

			irn = new_r_Proj(irg, block, cmpH, mode_b, pn_Cmp_Lg);
			dbg = get_irn_dbg_info(node);
			irn = new_rd_Cond(dbg, irg, block, irn);

			projHT = new_r_Proj(irg, block, irn, mode_X, pn_Cond_true);
			mark_irn_visited(projHT);
			exchange(projT, projHT);

			projHF = new_r_Proj(irg, block, irn, mode_X, pn_Cond_false);
			mark_irn_visited(projHF);

			new_bl = new_r_Block(irg, 1, &projHF);

			dbg   = get_irn_dbg_info(cmp);
			cmpL = new_rd_Cmp(dbg, irg, new_bl, lentry->low_word, rentry->low_word);
			irn = new_r_Proj(irg, new_bl, cmpL, mode_b, pn_Cmp_Lg);
			dbg = get_irn_dbg_info(node);
			irn = new_rd_Cond(dbg, irg, new_bl, irn);

			proj = new_r_Proj(irg, new_bl, irn, mode_X, pn_Cond_true);
			mark_irn_visited(proj);
			add_block_cf_input(dst_blk, projHT, proj);

			proj = new_r_Proj(irg, new_bl, irn, mode_X, pn_Cond_false);
			mark_irn_visited(proj);
			exchange(projF, proj);
Michael Beck's avatar
BugFix:    
Michael Beck committed
1366
		} else {
1367
			/* a rel b <==> a_h REL b_h || (a_h == b_h && a_l rel b_l) */
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
			ir_node *dstT, *dstF, *newbl_eq, *newbl_l;
			pmap_entry *entry;

			entry = pmap_find(env->proj_2_block, projT);
			assert(entry);
			dstT = entry->value;

			entry = pmap_find(env->proj_2_block, projF);
			assert(entry);
			dstF = entry->value;

1379
			irn = new_r_Proj(irg, block, cmpH, mode_b, pnc & ~pn_Cmp_Eq);