lower_dw.c 73.1 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Michael Beck's avatar
Michael Beck committed
20
21
22
23
24
25
/**
 * @file
 * @brief   Lower Double word operations, ie 64bit -> 32bit, 32bit -> 16bit etc.
 * @date    8.10.2004
 * @author  Michael Beck
 * @version $Id$
26
 */
Matthias Braun's avatar
Matthias Braun committed
27
#include "config.h"
28

29
#include <string.h>
30
#include <stdlib.h>
31
32
#include <assert.h>

33
#include "error.h"
34
#include "lowering.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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 "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
53
#include "irdump.h"
54
#include "array_t.h"
55

Michael Beck's avatar
Michael Beck committed
56
57
58
59
/** A map from mode to a primitive type. */
static pmap *prim_types;

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

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

65
66
67
68
/** 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
69
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;
70
71
72
73
74

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

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

Michael Beck's avatar
Michael Beck committed
84
85
86
87
88
89
90
91
92
/**
 * 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;

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

enum lower_flags {
103
104
	MUST_BE_LOWERED = 1,  /**< graph must be lowered */
	CF_CHANGED      = 2,  /**< control flow was changed */
105
106
107
108
109
110
111
112
};

/**
 * 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
113
114
	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 */
115
116
117
118
119
120
121
	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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * 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;
138
}  /* get_primitive_type */
Michael Beck's avatar
Michael Beck committed
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
165
166
167

/**
 * 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) {
168
			set_method_param_type(mtd, n_param++, tp_u);
Michael Beck's avatar
Michael Beck committed
169
			set_method_param_type(mtd, n_param++, tp_s);
Michael Beck's avatar
BugFix:    
Michael Beck committed
170
		} else if (imode == env->params->high_unsigned) {
Michael Beck's avatar
Michael Beck committed
171
172
			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
173
		} else {
Michael Beck's avatar
Michael Beck committed
174
175
			ir_type *tp = get_primitive_type(imode);
			set_method_param_type(mtd, n_param++, tp);
Michael Beck's avatar
BugFix:    
Michael Beck committed
176
		}  /* if */
Michael Beck's avatar
Michael Beck committed
177
178
179

		n_res = 0;
		if (omode == env->params->high_signed) {
180
			set_method_res_type(mtd, n_res++, tp_u);
Michael Beck's avatar
Michael Beck committed
181
			set_method_res_type(mtd, n_res++, tp_s);
Michael Beck's avatar
BugFix:    
Michael Beck committed
182
		} else if (omode == env->params->high_unsigned) {
Michael Beck's avatar
Michael Beck committed
183
184
			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
185
		} else {
Michael Beck's avatar
Michael Beck committed
186
187
			ir_type *tp = get_primitive_type(omode);
			set_method_res_type(mtd, n_res++, tp);
Michael Beck's avatar
BugFix:    
Michael Beck committed
188
		}  /* if */
Michael Beck's avatar
Michael Beck committed
189
		entry->mtd = mtd;
Michael Beck's avatar
BugFix:    
Michael Beck committed
190
	} else {
Michael Beck's avatar
Michael Beck committed
191
		mtd = entry->mtd;
Michael Beck's avatar
BugFix:    
Michael Beck committed
192
	}  /* if */
Michael Beck's avatar
Michael Beck committed
193
	return mtd;
194
}  /* get_conv_type */
Michael Beck's avatar
Michael Beck committed
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);

215
	for (phi = get_Block_phis(block); phi != NULL; phi = get_Phi_next(phi)) {
216
217
218
219
		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
220
	}  /* for */
221
}  /* add_block_cf_input_nr */
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

/**
 * 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
237
238
		}  /* if */
	}  /* for */
239
240
	assert(i < arity);
	add_block_cf_input_nr(block, nr, cf);
241
}  /* add_block_cf_input */
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

/**
 * 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
263
	}  /* switch */
264
}  /* get_irn_op_mode */
265
266

/**
267
 * Walker, prepare the node links.
268
269
270
271
272
273
 */
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;
Michael Beck's avatar
Michael Beck committed
274
	int          i, idx;
275
276
277
278
279
280
281
282

	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));

Michael Beck's avatar
Michael Beck committed
283
284
285
286
287
288
289
290
291
292
293
		idx = get_irn_idx(node);
		if (idx >= lenv->n_entries) {
			/* enlarge: this happens only for Rotl nodes which is RARELY */
			int old = lenv->n_entries;
			int n_idx = idx + (idx >> 3);

			ARR_RESIZE(node_entry_t *, lenv->entries, n_idx);
			memset(&lenv->entries[old], 0, (n_idx - old) * sizeof(lenv->entries[0]));
			lenv->n_entries = n_idx;
		}
		lenv->entries[idx] = link;
294
		lenv->flags |= MUST_BE_LOWERED;
295
	} else if (is_Conv(node)) {
296
297
298
299
300
301
		/* 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
302
			/* must lower this node either but don't need a link */
303
			lenv->flags |= MUST_BE_LOWERED;
Michael Beck's avatar
BugFix:    
Michael Beck committed
304
		}  /* if */
305
		return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
306
	}  /* if */
307
308
309
310
311
312
313
314

	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
315
	} else if (is_Phi(node)) {
316
317
		/* link all Phi nodes to its block */
		ir_node *block = get_nodes_block(node);
318
		add_Block_phi(block, node);
Michael Beck's avatar
BugFix:    
Michael Beck committed
319
	} else if (is_Block(node)) {
320
321
322
323
324
325
		/* 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
326
327
		}  /* for */
	}  /* if */
328
}  /* prepare_links */
329
330
331
332
333
334
335
336
337

/**
 * 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);
	int      idx;
338
	ir_graph *irg = current_ir_graph;
339
	ir_mode  *low_mode = env->params->low_unsigned;
340
341
342

	tv   = get_Const_tarval(node);

343
	tv_l = tarval_convert_to(tv, low_mode);
344
	low  = new_rd_Const(dbg, irg, tv_l);
345

Michael Beck's avatar
BugFix:    
Michael Beck committed
346
	tv_h = tarval_convert_to(tarval_shrs(tv, env->tv_mode_bits), mode);
347
	high = new_rd_Const(dbg, irg, tv_h);
348
349
350
351
352

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	env->entries[idx]->low_word  = low;
	env->entries[idx]->high_word = high;
353
}  /* lower_Const */
354
355
356
357
358

/**
 * Translate a Load: create two.
 */
static void lower_Load(ir_node *node, ir_mode *mode, lower_env_t *env) {
359
360
361
362
363
364
365
366
	ir_mode    *low_mode = env->params->low_unsigned;
	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;
367
	ir_cons_flags volatility = get_Load_volatility(node) == volatility_is_volatile
368
	                         ? cons_volatile : 0;
369
370
371
372

	if (env->params->little_endian) {
		low  = adr;
		high = new_r_Add(irg, block, adr,
373
			new_r_Const(irg, env->tv_mode_bytes),
374
			get_irn_mode(adr));
Michael Beck's avatar
BugFix:    
Michael Beck committed
375
	} else {
376
		low  = new_r_Add(irg, block, adr,
377
			new_r_Const(irg, env->tv_mode_bytes),
378
379
			get_irn_mode(adr));
		high = adr;
Michael Beck's avatar
BugFix:    
Michael Beck committed
380
	}  /* if */
381
382
383

	/* create two loads */
	dbg  = get_irn_dbg_info(node);
384
	low  = new_rd_Load(dbg, irg, block, mem,  low,  low_mode, volatility);
385
	proj = new_r_Proj(irg, block, low, mode_M, pn_Load_M);
386
	high = new_rd_Load(dbg, irg, block, proj, high, mode, volatility);
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406

	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);
407
408
			env->entries[idx]->low_word  = new_r_Proj(irg, block, low,  low_mode, pn_Load_res);
			env->entries[idx]->high_word = new_r_Proj(irg, block, high, mode,     pn_Load_res);
409
410
411
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
412
		}  /* switch */
413
414
415
		/* 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
416
	}  /* for */
417
}  /* lower_Load */
418
419
420
421
422

/**
 * Translate a Store: create two.
 */
static void lower_Store(ir_node *node, ir_mode *mode, lower_env_t *env) {
423
424
425
426
427
	ir_graph     *irg;
	ir_node      *block, *adr, *mem;
	ir_node      *low, *high, *irn, *proj;
	dbg_info     *dbg;
	int           idx;
428
	node_entry_t *entry;
429
	ir_cons_flags    volatility = get_Store_volatility(node) == volatility_is_volatile
430
	                           ? cons_volatile : 0;
Matthias Braun's avatar
Matthias Braun committed
431
	(void) mode;
432
433
434
435
436
437
438
439
440

	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
441
	}  /* if */
442
443
444
445
446
447
448
449
450

	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,
451
			new_r_Const(irg, env->tv_mode_bytes),
452
			get_irn_mode(adr));
Michael Beck's avatar
BugFix:    
Michael Beck committed
453
	} else {
454
		low  = new_r_Add(irg, block, adr,
455
			new_r_Const(irg, env->tv_mode_bytes),
456
457
			get_irn_mode(adr));
		high = adr;
Michael Beck's avatar
BugFix:    
Michael Beck committed
458
	}  /* if */
459
460
461

	/* create two Stores */
	dbg = get_irn_dbg_info(node);
462
	low  = new_rd_Store(dbg, irg, block, mem, low,  entry->low_word, volatility);
463
	proj = new_r_Proj(irg, block, low, mode_M, pn_Store_M);
464
	high = new_rd_Store(dbg, irg, block, proj, high, entry->high_word, volatility);
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484

	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
485
		}  /* switch */
486
487
488
		/* 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
489
	}  /* for */
490
}  /* lower_Store */
491
492
493

/**
 * Return a node containing the address of the intrinsic emulation function.
Michael Beck's avatar
Michael Beck committed
494
495
496
497
498
499
500
 *
 * @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
501
 */
Michael Beck's avatar
Michael Beck committed
502
503
504
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) {
505
	symconst_symbol sym;
506
	ir_entity *ent;
507
508
	op_mode_entry_t key, *entry;

Michael Beck's avatar
Michael Beck committed
509
510
511
512
	key.op    = op;
	key.imode = imode;
	key.omode = omode;
	key.ent   = NULL;
513

Michael Beck's avatar
Michael Beck committed
514
515
	entry = set_insert(intrinsic_fkt, &key, sizeof(key),
				HASH_PTR(op) ^ HASH_PTR(imode) ^ (HASH_PTR(omode) << 8));
516
517
	if (! entry->ent) {
		/* create a new one */
Michael Beck's avatar
Michael Beck committed
518
		ent = env->params->create_intrinsic(method, op, imode, omode, env->params->ctx);
519
520
521

		assert(ent && "Intrinsic creator must return an entity");
		entry->ent = ent;
Michael Beck's avatar
BugFix:    
Michael Beck committed
522
	} else {
523
		ent = entry->ent;
Michael Beck's avatar
BugFix:    
Michael Beck committed
524
	}  /* if */
525
	sym.entity_p = ent;
526
	return new_r_SymConst(current_ir_graph, block, mode_P_code, sym, symconst_addr_ent);
527
}  /* get_intrinsic_address */
528
529
530
531
532
533
534
535
536

/**
 * 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
537
	ir_mode  *opmode;
538
539
540
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
541
	ir_graph *irg;
542
543
544
545
546
547
548
549
550
551
	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
552
	}  /* if */
553
554
555
556
557
558
559
560
561
562
563
564

	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
565
	}  /* if */
566
567
568
569

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

570
	dbg   = get_irn_dbg_info(node);
571
	block = get_nodes_block(node);
572
	irg   = current_ir_graph;
573
574

	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
Michael Beck's avatar
Michael Beck committed
575
576
	opmode = get_irn_op_mode(node);
	irn = get_intrinsic_address(mtp, get_irn_op(node), opmode, opmode, block, env);
577
	call = new_rd_Call(dbg, irg, block, get_Div_mem(node),
578
579
		irn, 4, in, mtp);
	set_irn_pinned(call, get_irn_pinned(node));
580
	irn = new_r_Proj(irg, block, call, mode_T, pn_Call_T_result);
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596

	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);
597
598
			env->entries[idx]->low_word  = new_r_Proj(current_ir_graph, block, irn, env->params->low_unsigned, 0);
			env->entries[idx]->high_word = new_r_Proj(current_ir_graph, block, irn, mode,                      1);
599
600
601
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
602
		}  /* switch */
603
604
605
		/* 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
606
	}  /* for */
607
}  /* lower_Div */
608
609
610
611
612
613
614
615
616

/**
 * 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
617
	ir_mode  *opmode;
618
619
620
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
621
	ir_graph *irg;
622
623
624
625
626
627
628
629
630
631
	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
632
	}  /* if */
633
634
635
636
637
638
639
640
641
642
643
644

	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
645
	}  /* if */
646
647
648
649

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

650
	dbg   = get_irn_dbg_info(node);
651
	block = get_nodes_block(node);
652
	irg   = current_ir_graph;
653
654

	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
Michael Beck's avatar
Michael Beck committed
655
656
	opmode = get_irn_op_mode(node);
	irn = get_intrinsic_address(mtp, get_irn_op(node), opmode, opmode, block, env);
657
	call = new_rd_Call(dbg, irg, block, get_Mod_mem(node),
658
659
		irn, 4, in, mtp);
	set_irn_pinned(call, get_irn_pinned(node));
660
	irn = new_r_Proj(irg, block, call, mode_T, pn_Call_T_result);
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676

	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);
677
678
			env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, env->params->low_unsigned, 0);
			env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode,                      1);
679
680
681
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
682
		}  /* switch */
683
684
685
		/* 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
686
	}  /* for */
687
}  /* lower_Mod */
688
689
690
691
692
693
694

/**
 * Translate a DivMod.
 *
 * Create two intrinsic Calls.
 */
static void lower_DivMod(ir_node *node, ir_mode *mode, lower_env_t *env) {
Matthias Braun's avatar
Matthias Braun committed
695
696
697
	ir_node  *block, *proj, *irn, *mem, *callDiv, *callMod;
	ir_node  *resDiv = NULL;
	ir_node  *resMod = NULL;
698
	ir_node  *in[4];
Michael Beck's avatar
Michael Beck committed
699
	ir_mode  *opmode;
700
701
702
703
704
	dbg_info *dbg;
	ir_type  *mtp;
	int      idx;
	node_entry_t *entry;
	unsigned flags = 0;
705
	ir_graph *irg;
706
707
708
709
710
711
712

	/* 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
713
714
		}  /* switch */
	}  /* for */
715
716
717
718
719
720
721
722
723

	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
724
	}  /* if */
725
726
727
728
729
730
731
732
733
734
735
736

	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
737
	}  /* if */
738
739
740
741

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

742
	dbg   = get_irn_dbg_info(node);
743
	block = get_nodes_block(node);
744
	irg   = current_ir_graph;
745
746
747
748
749
750

	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
751
752
		opmode = get_irn_op_mode(node);
		irn = get_intrinsic_address(mtp, op_Div, opmode, opmode, block, env);
753
		callDiv = new_rd_Call(dbg, irg, block, mem,
754
755
			irn, 4, in, mtp);
		set_irn_pinned(callDiv, get_irn_pinned(node));
756
		resDiv = new_r_Proj(irg, block, callDiv, mode_T, pn_Call_T_result);
Michael Beck's avatar
BugFix:    
Michael Beck committed
757
	}  /* if */
758
759
	if (flags & 2) {
		if (flags & 1)
760
			mem = new_r_Proj(irg, block, callDiv, mode_M, pn_Call_M);
Michael Beck's avatar
Michael Beck committed
761
762
		opmode = get_irn_op_mode(node);
		irn = get_intrinsic_address(mtp, op_Mod, opmode, opmode, block, env);
763
		callMod = new_rd_Call(dbg, irg, block, mem,
764
765
			irn, 4, in, mtp);
		set_irn_pinned(callMod, get_irn_pinned(node));
766
		resMod = new_r_Proj(irg, block, callMod, mode_T, pn_Call_T_result);
Michael Beck's avatar
BugFix:    
Michael Beck committed
767
	}  /* if */
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783

	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);
784
785
			env->entries[idx]->low_word  = new_r_Proj(irg, block, resDiv, env->params->low_unsigned, 0);
			env->entries[idx]->high_word = new_r_Proj(irg, block, resDiv, mode,                      1);
786
787
788
			break;
		case pn_DivMod_res_mod:   /* Result of Mod. */
			idx = get_irn_idx(proj);
789
790
			env->entries[idx]->low_word  = new_r_Proj(irg, block, resMod, env->params->low_unsigned, 0);
			env->entries[idx]->high_word = new_r_Proj(irg, block, resMod, mode,                      1);
791
792
793
			break;
		default:
			assert(0 && "unexpected Proj number");
Michael Beck's avatar
BugFix:    
Michael Beck committed
794
		}  /* switch */
795
796
797
		/* 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
798
	}  /* for */
799
}  /* lower_DivMod */
800
801
802
803
804
805
806
807
808
809
810
811

/**
 * 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;
812
	ir_graph *irg;
813
814
815
816
817
818
819
820
821
822
	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
823
	}  /* if */
824
825
826
827
828
829
830
831
832
833
834
835

	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
836
	}  /* if */
837
838
839
840

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

841
	dbg   = get_irn_dbg_info(node);
842
	block = get_nodes_block(node);
843
	irg   = current_ir_graph;
844
845

	mtp = mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
Michael Beck's avatar
Michael Beck committed
846
	irn = get_intrinsic_address(mtp, get_irn_op(node), mode, mode, block, env);
847
	irn = new_rd_Call(dbg, irg, block, get_irg_no_mem(current_ir_graph),
848
849
		irn, 4, in, mtp);
	set_irn_pinned(irn, get_irn_pinned(node));
850
	irn = new_r_Proj(irg, block, irn, mode_T, pn_Call_T_result);
851
852
853

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
854
855
	env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, env->params->low_unsigned, 0);
	env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode,                      1);
856
}  /* lower_Binop */
857

Christian Würdig's avatar
Christian Würdig committed
858
859
860
861
862
863
864
865
866
867
868
/**
 * 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;
869
	ir_graph *irg;
Christian Würdig's avatar
Christian Würdig committed
870
871
872
873
874
875
876
877
878
879
	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
880
	}  /* if */
Christian Würdig's avatar
Christian Würdig committed
881
882
883
884
885
886
887
888
889

	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);
890
	irg  = current_ir_graph;
Christian Würdig's avatar
Christian Würdig committed
891
892
893

	mtp = mode_is_signed(mode) ? shiftop_tp_s : shiftop_tp_u;
	irn = get_intrinsic_address(mtp, get_irn_op(node), mode, mode, block, env);
894
	irn = new_rd_Call(dbg, irg, block, get_irg_no_mem(current_ir_graph),
Christian Würdig's avatar
Christian Würdig committed
895
896
		irn, 3, in, mtp);
	set_irn_pinned(irn, get_irn_pinned(node));
897
	irn = new_r_Proj(irg, block, irn, mode_T, pn_Call_T_result);
Christian Würdig's avatar
Christian Würdig committed
898
899
900

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
901
902
	env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, env->params->low_unsigned, 0);
	env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode,                      1);
903
}  /* lower_Shiftop */
Christian Würdig's avatar
Christian Würdig committed
904

Michael Beck's avatar
Michael Beck committed
905
906
907
908
/**
 * Translate a Shr and handle special cases.
 */
static void lower_Shr(ir_node *node, ir_mode *mode, lower_env_t *env) {
909
910
	ir_node  *right = get_Shr_right(node);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
911
912
913
914
915

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

		if (tarval_is_long(tv) &&
916
		    get_tarval_long(tv) >= (long)get_mode_size_bits(mode)) {
Michael Beck's avatar
Michael Beck committed
917
918
			ir_node *block = get_nodes_block(node);
			ir_node *left = get_Shr_left(node);
919
920
			ir_node *c;
			long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
Michael Beck's avatar
Michael Beck committed
921
922
923
924
925
			int idx = get_irn_idx(left);

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

926
			if (shf_cnt > 0) {
927
				c = new_r_Const_long(irg, env->params->low_unsigned, shf_cnt);
928
				env->entries[idx]->low_word = new_r_Shr(irg, block, left, c, mode);
Michael Beck's avatar
BugFix:    
Michael Beck committed
929
			} else {
930
				env->entries[idx]->low_word = left;
Michael Beck's avatar
BugFix:    
Michael Beck committed
931
			}  /* if */
932
			env->entries[idx]->high_word = new_r_Const(irg, get_mode_null(mode));
Michael Beck's avatar
Michael Beck committed
933
934

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
935
936
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
937
	lower_Shiftop(node, mode, env);
938
}  /* lower_Shr */
Michael Beck's avatar
Michael Beck committed
939
940
941
942
943

/**
 * Translate a Shl and handle special cases.
 */
static void lower_Shl(ir_node *node, ir_mode *mode, lower_env_t *env) {
944
945
	ir_node  *right = get_Shl_right(node);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
946
947
948
949
950

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

		if (tarval_is_long(tv) &&
951
		    get_tarval_long(tv) >= (long)get_mode_size_bits(mode)) {
952
			ir_mode *mode_l;
Michael Beck's avatar
Michael Beck committed
953
954
			ir_node *block = get_nodes_block(node);
			ir_node *left = get_Shl_left(node);
955
956
			ir_node *c;
			long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
Michael Beck's avatar
Michael Beck committed
957
958
			int idx = get_irn_idx(left);

959
			left = new_r_Conv(irg, block, env->entries[idx]->low_word, mode);
Michael Beck's avatar
Michael Beck committed
960
961
			idx = get_irn_idx(node);

962
			mode_l = env->params->low_unsigned;
963
			if (shf_cnt > 0) {
964
				c = new_r_Const_long(irg, mode_l, shf_cnt);
965
				env->entries[idx]->high_word = new_r_Shl(irg, block, left, c, mode);
Michael Beck's avatar
BugFix:    
Michael Beck committed
966
			} else {
967
				env->entries[idx]->high_word = left;
Michael Beck's avatar
BugFix:    
Michael Beck committed
968
			}  /* if */
969
			env->entries[idx]->low_word  = new_r_Const(irg, get_mode_null(mode_l));
Michael Beck's avatar
Michael Beck committed
970
971

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
972
973
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
974
	lower_Shiftop(node, mode, env);
975
}  /* lower_Shl */
Michael Beck's avatar
Michael Beck committed
976
977
978
979
980

/**
 * Translate a Shrs and handle special cases.
 */
static void lower_Shrs(ir_node *node, ir_mode *mode, lower_env_t *env) {
981
982
	ir_node  *right = get_Shrs_right(node);
	ir_graph *irg = current_ir_graph;
Michael Beck's avatar
Michael Beck committed
983
984
985
986
987

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

		if (tarval_is_long(tv) &&
988
		    get_tarval_long(tv) >= (long)get_mode_size_bits(mode)) {
989
990
991
992
			ir_node *block   = get_nodes_block(node);
			ir_node *left    = get_Shrs_left(node);
			long     shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
			int      idx     = get_irn_idx(left);
993
			ir_mode *mode_l;
994
			ir_node *low;
Michael Beck's avatar
Michael Beck committed
995
996
997
998
999
			ir_node *c;

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

1000
			mode_l = env->params->low_unsigned;
1001
			if (shf_cnt > 0) {
1002
				c   = new_r_Const_long(irg, mode_l, shf_cnt);
1003
				low = new_r_Shrs(irg, block, left, c, mode);
Michael Beck's avatar
BugFix:    
Michael Beck committed
1004
			} else {
1005
				low = left;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1006
			}  /* if */
1007
1008
			/* low word is expected to have mode_l */
			env->entries[idx]->low_word = new_r_Conv(irg, block, low, mode_l);
1009

1010
			c = new_r_Const_long(irg, mode_l, get_mode_size_bits(mode) - 1);
1011
			env->entries[idx]->high_word = new_r_Shrs(irg, block, left, c, mode);
Michael Beck's avatar
Michael Beck committed
1012
1013

			return;
Michael Beck's avatar
BugFix:    
Michael Beck committed
1014
1015
		}  /* if */
	}  /* if */
Michael Beck's avatar
Michael Beck committed
1016
	lower_Shiftop(node, mode, env);
1017
}  /* lower_Shrs */
Michael Beck's avatar
Michael Beck committed
1018
1019

/**
Michael Beck's avatar
Michael Beck committed
1020
 * Rebuild Rotl nodes into Or(Shl, Shr) and prepare all nodes.
Michael Beck's avatar
Michael Beck committed
1021
 */
Michael Beck's avatar
Michael Beck committed
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
static void prepare_links_and_handle_rotl(ir_node *node, void *env) {
	lower_env_t *lenv = env;

	if (is_Rotl(node)) {
		ir_mode *mode = get_irn_op_mode(node);
			if (mode == lenv->params->high_signed ||
			    mode == lenv->params->high_unsigned) {
				ir_node  *right = get_Rotl_right(node);
				ir_node  *left, *shl, *shr, *or, *block, *sub, *c;
				ir_mode  *omode, *rmode;
				ir_graph *irg;
				dbg_info *dbg;
				optimization_state_t state;

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

					if (tarval_is_long(tv) &&
					    get_tarval_long(tv) == (long)get_mode_size_bits(mode)) {
						/* will be optimized in lower_Rotl() */
						return;
					}
				}

				/* replace the Rotl(x,y) by an Or(Shl(x,y), Shr(x,64-y)) and lower those */
				dbg   = get_irn_dbg_info(node);
				omode = get_irn_mode(node);
				left  = get_Rotl_left(node);
				irg   = current_ir_graph;
				block = get_nodes_block(node);
				shl   = new_rd_Shl(dbg, irg, block, left, right, omode);
				rmode = get_irn_mode(right);
				c     = new_Const_long(rmode, get_mode_size_bits(omode));
				sub   = new_rd_Sub(dbg, irg, block, c, right, rmode);
				shr   = new_rd_Shr(dbg, irg, block, left, sub, omode);

				/* optimization must be switched off here, or we will get the Rotl back */
				save_optimization_state(&state);
				set_opt_algebraic_simplification(0);
				or = new_rd_Or(dbg, irg, block, shl, shr, omode);
				restore_optimization_state(&state);

				exchange(node, or);

				/* do lowering on the new nodes */
				prepare_links(shl, env);
				prepare_links(c, env);
				prepare_links(sub, env);
				prepare_links(shr, env);
				prepare_links(or, env);
			}
	} else {
		prepare_links(node, env);
	}
}
Michael Beck's avatar
Michael Beck committed
1077

Michael Beck's avatar
Michael Beck committed
1078
1079
1080
1081
1082
1083
1084
1085
/**
 * Translate a special case Rotl(x, sizeof(w)).
 */
static void lower_Rotl(ir_node *node, ir_mode *mode, lower_env_t *env) {
	ir_node *right = get_Rotl_right(node);
	ir_node *left = get_Rotl_left(node);
	ir_node *h, *l;
	int idx = get_irn_idx(left);
Matthias Braun's avatar
Matthias Braun committed
1086
1087
	(void) right;
	(void) mode;
Michael Beck's avatar
Michael Beck committed
1088

Michael Beck's avatar
Michael Beck committed
1089
1090
1091
	assert(get_mode_arithmetic(mode) == irma_twos_complement &&
	       is_Const(right) && tarval_is_long(get_Const_tarval(right)) &&
	       get_tarval_long(get_Const_tarval(right)) == (long)get_mode_size_bits(mode));
Michael Beck's avatar
Michael Beck committed
1092

Michael Beck's avatar
Michael Beck committed
1093
1094
1095
	l = env->entries[idx]->low_word;
	h = env->entries[idx]->high_word;
	idx = get_irn_idx(node);
Michael Beck's avatar
Michael Beck committed
1096

Michael Beck's avatar
Michael Beck committed
1097
1098
1099
	env->entries[idx]->low_word  = h;
	env->entries[idx]->high_word = l;
}  /* lower_Rotl */
1100

1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
/**
 * 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;
1112
	ir_graph *irg;
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
	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
1123
	}  /* if */
1124
1125
1126
1127

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

1128
	dbg   = get_irn_dbg_info(node);
1129
	block = get_nodes_block(node);
1130
	irg   = current_ir_graph;
1131
1132

	mtp = mode_is_signed(mode) ? unop_tp_s : unop_tp_u;
Michael Beck's avatar
Michael Beck committed
1133
	irn = get_intrinsic_address(mtp, get_irn_op(node), mode, mode, block, env);
1134
	irn = new_rd_Call(dbg, irg, block, get_irg_no_mem(current_ir_graph),
1135
1136
		irn, 2, in, mtp);
	set_irn_pinned(irn, get_irn_pinned(node));
1137
	irn = new_r_Proj(irg, block, irn, mode_T, pn_Call_T_result);
1138
1139
1140

	idx = get_irn_idx(node);
	assert(idx < env->n_entries);
1141
1142
	env->entries[idx]->low_word  = new_r_Proj(irg, block, irn, env->params->low_unsigned, 0);
	env->entries[idx]->high_word = new_r_Proj(irg, block, irn, mode,                      1);
1143
}  /* lower_Unop */
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155

/**
 * 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;
1156
	ir_graph *irg;
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
	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
1167
	}  /* if */
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177