lower_dw.c 68.6 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2010 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
#include <stdbool.h>
32
33
#include <assert.h>

34
#include "error.h"
35
#include "lowering.h"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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
54
#include "irdump.h"
55
#include "array_t.h"
Michael Beck's avatar
Michael Beck committed
56
#include "irpass_t.h"
57

58
59
60
61
62
63
64
65
66
67
typedef struct lower_env_t lower_env_t;

/**
 * The type of a lower function.
 *
 * @param node   the node to be lowered
 * @param env    the lower environment
 */
typedef void (*lower_func)(ir_node *node, ir_mode *mode, lower_env_t *env);

Michael Beck's avatar
Michael Beck committed
68
/** A map from (op, imode, omode) to Intrinsic functions entities. */
69
70
static set *intrinsic_fkt;

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

74
75
76
77
/** 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
78
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;
79
80
81
82
83

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

/**
Michael Beck's avatar
Michael Beck committed
84
 * An entry in the (op, imode, omode) -> entity map.
85
 */
86
typedef struct op_mode_entry {
87
	const ir_op   *op;    /**< the op */
Michael Beck's avatar
Michael Beck committed
88
89
	const ir_mode *imode; /**< the input mode */
	const ir_mode *omode; /**< the output mode */
90
	ir_entity     *ent;   /**< the associated entity of this (op, imode, omode) triple */
91
92
} op_mode_entry_t;

Michael Beck's avatar
Michael Beck committed
93
94
95
/**
 * An entry in the (imode, omode) -> tp map.
 */
96
typedef struct conv_tp_entry {
Michael Beck's avatar
Michael Beck committed
97
98
99
100
101
	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;

102
103
104
105
/**
 * Every double word node will be replaced,
 * we need some store to hold the replacement:
 */
106
typedef struct node_entry_t {
107
108
	ir_node *low_word;    /**< the low word */
	ir_node *high_word;   /**< the high word */
109
110
111
} node_entry_t;

enum lower_flags {
112
113
	MUST_BE_LOWERED = 1,  /**< graph must be lowered */
	CF_CHANGED      = 2,  /**< control flow was changed */
114
115
116
117
118
};

/**
 * The lower environment.
 */
119
struct lower_env_t {
120
	node_entry_t **entries;       /**< entries per node */
121
	ir_graph      *irg;
122
	struct obstack obst;          /**< an obstack holding the temporary data */
Matthias Braun's avatar
Matthias Braun committed
123
124
125
126
127
128
129
130
131
132
133
134
	ir_type   *l_mtp;              /**< lowered method type of the current method */
	ir_tarval *tv_mode_bytes;     /**< a tarval containing the number of bytes in the lowered modes */
	ir_tarval *tv_mode_bits;      /**< a tarval containing the number of bits in the lowered modes */
	pdeq      *waitq;              /**< a wait queue of all nodes that must be handled later */
	ir_node  **lowered_phis;       /**< list of lowered phis */
	pmap      *proj_2_block;       /**< a map from ProjX to its destination blocks */
	ir_mode   *high_signed;        /**< doubleword signed type */
	ir_mode   *high_unsigned;      /**< doubleword unsigned type */
	ir_mode   *low_signed;         /**< word signed type */
	ir_mode   *low_unsigned;       /**< word unsigned type */
	ident     *first_id;           /**< .l for little and .h for big endian */
	ident     *next_id;            /**< .h for little and .l for big endian */
135
136
	const lwrdw_param_t *params;  /**< transformation parameter */
	unsigned flags;               /**< some flags */
137
	unsigned n_entries;           /**< number of entries */
138
	ir_type  *value_param_tp;     /**< the old value param type */
139
};
140

Michael Beck's avatar
Michael Beck committed
141
142
143
/**
 * Create a method type for a Conv emulation from imode to omode.
 */
144
145
static ir_type *get_conv_type(ir_mode *imode, ir_mode *omode, lower_env_t *env)
{
Michael Beck's avatar
Michael Beck committed
146
147
148
149
150
151
152
	conv_tp_entry_t key, *entry;
	ir_type *mtd;

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

153
	entry = (conv_tp_entry_t*)set_insert(conv_types, &key, sizeof(key), HASH_PTR(imode) ^ HASH_PTR(omode));
Michael Beck's avatar
Michael Beck committed
154
155
156
	if (! entry->mtd) {
		int n_param = 1, n_res = 1;

157
		if (imode == env->high_signed || imode == env->high_unsigned)
Michael Beck's avatar
Michael Beck committed
158
			n_param = 2;
159
		if (omode == env->high_signed || omode == env->high_unsigned)
Michael Beck's avatar
Michael Beck committed
160
161
162
			n_res = 2;

		/* create a new one */
163
		mtd = new_type_method(n_param, n_res);
Michael Beck's avatar
Michael Beck committed
164
165
166

		/* set param types and result types */
		n_param = 0;
167
		if (imode == env->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);
170
		} else if (imode == env->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 {
174
			ir_type *tp = get_type_for_mode(imode);
Michael Beck's avatar
Michael Beck committed
175
			set_method_param_type(mtd, n_param++, tp);
176
		}
Michael Beck's avatar
Michael Beck committed
177
178

		n_res = 0;
179
		if (omode == env->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);
182
		} else if (omode == env->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 {
186
			ir_type *tp = get_type_for_mode(omode);
Michael Beck's avatar
Michael Beck committed
187
			set_method_res_type(mtd, n_res++, tp);
188
		}
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;
192
	}
Michael Beck's avatar
Michael Beck committed
193
	return mtd;
194
}
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);
220
221
	}
}
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;
237
238
		}
	}
239
240
	assert(i < arity);
	add_block_cf_input_nr(block, nr, cf);
241
}
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);
263
264
	}
}
265
266

/**
267
268
 * Walker, prepare the node links and determine which nodes need to be lowered
 * at all.
269
 */
270
static void prepare_links(lower_env_t *env, ir_node *node)
271
272
273
{
	ir_mode      *mode = get_irn_op_mode(node);
	node_entry_t *link;
274
	int           i;
275

276
	if (mode == env->high_signed || mode == env->high_unsigned) {
277
		unsigned idx = get_irn_idx(node);
278
		/* ok, found a node that will be lowered */
279
		link = OALLOCZ(&env->obst, node_entry_t);
280

281
		if (idx >= env->n_entries) {
Michael Beck's avatar
Michael Beck committed
282
			/* enlarge: this happens only for Rotl nodes which is RARELY */
283
			unsigned old   = env->n_entries;
284
			unsigned n_idx = idx + (idx >> 3);
Michael Beck's avatar
Michael Beck committed
285

286
287
288
			ARR_RESIZE(node_entry_t *, env->entries, n_idx);
			memset(&env->entries[old], 0, (n_idx - old) * sizeof(env->entries[0]));
			env->n_entries = n_idx;
Michael Beck's avatar
Michael Beck committed
289
		}
290
291
		env->entries[idx] = link;
		env->flags |= MUST_BE_LOWERED;
292
	} else if (is_Conv(node)) {
293
294
295
296
		/* Conv nodes have two modes */
		ir_node *pred = get_Conv_op(node);
		mode = get_irn_mode(pred);

297
		if (mode == env->high_signed || mode == env->high_unsigned) {
Michael Beck's avatar
BugFix:    
Michael Beck committed
298
			/* must lower this node either but don't need a link */
299
			env->flags |= MUST_BE_LOWERED;
300
		}
301
		return;
302
	}
303
304
305
306
307
308
309
310

	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
311
	} else if (is_Phi(node)) {
312
313
		/* link all Phi nodes to its block */
		ir_node *block = get_nodes_block(node);
314
		add_Block_phi(block, node);
Michael Beck's avatar
BugFix:    
Michael Beck committed
315
	} else if (is_Block(node)) {
316
317
318
319
320
		/* 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))
321
				pmap_insert(env->proj_2_block, pred, node);
322
323
324
		}
	}
}
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
static node_entry_t *get_node_entry(lower_env_t *env, ir_node *node)
{
	unsigned idx = get_irn_idx(node);
	assert(idx < env->n_entries);
	return env->entries[idx];
}

static void set_lowered(lower_env_t *env, ir_node *old,
                        ir_node *new_low, ir_node *new_high)
{
	node_entry_t *entry = get_node_entry(env, old);
	entry->low_word  = new_low;
	entry->high_word = new_high;
}

341
342
343
/**
 * Translate a Constant: create two.
 */
344
345
static void lower_Const(ir_node *node, ir_mode *mode, lower_env_t *env)
{
Matthias Braun's avatar
Matthias Braun committed
346
347
348
349
350
351
352
353
354
	ir_graph  *irg      = get_irn_irg(node);
	dbg_info  *dbg      = get_irn_dbg_info(node);
	ir_mode   *low_mode = env->low_unsigned;
	ir_tarval *tv       = get_Const_tarval(node);
	ir_tarval *tv_l     = tarval_convert_to(tv, low_mode);
	ir_node   *res_low  = new_rd_Const(dbg, irg, tv_l);
	ir_tarval *tv_shrs  = tarval_shrs(tv, env->tv_mode_bits);
	ir_tarval *tv_h     = tarval_convert_to(tv_shrs, mode);
	ir_node   *res_high = new_rd_Const(dbg, irg, tv_h);
355
356

	set_lowered(env, node, res_low, res_high);
357
}
358
359
360
361

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

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

	/* create two loads */
	dbg  = get_irn_dbg_info(node);
384
	low  = new_rd_Load(dbg, block, mem,  low,  low_mode, volatility);
385
	proj = new_r_Proj(low, mode_M, pn_Load_M);
386
	high = new_rd_Load(dbg, block, proj, high, mode, volatility);
387

388
	set_lowered(env, node, low, high);
389

390
391
	for (proj = (ir_node*)get_irn_link(node); proj;
	     proj = (ir_node*)get_irn_link(proj)) {
392
393
394
395
396
397
398
399
400
		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;
401
402
403
404
		case pn_Load_res: {       /* Result of load operation. */
			ir_node *res_low  = new_r_Proj(low,  low_mode, pn_Load_res);
			ir_node *res_high = new_r_Proj(high, mode,     pn_Load_res);
			set_lowered(env, proj, res_low, res_high);
405
			break;
406
		}
407
408
		default:
			assert(0 && "unexpected Proj number");
409
		}
410
411
		/* mark this proj: we have handled it already, otherwise we might fall
		 * into out new nodes. */
412
		mark_irn_visited(proj);
413
414
	}
}
415
416
417
418

/**
 * Translate a Store: create two.
 */
419
420
static void lower_Store(ir_node *node, ir_mode *mode, lower_env_t *env)
{
421
422
	ir_graph     *irg;
	ir_node      *block, *adr, *mem;
423
	ir_node      *low, *high, *proj;
424
	dbg_info     *dbg;
425
426
427
	ir_node            *value = get_Store_value(node);
	const node_entry_t *entry = get_node_entry(env, value);
	ir_cons_flags volatility = get_Store_volatility(node) == volatility_is_volatile
428
	                           ? cons_volatile : cons_none;
Matthias Braun's avatar
Matthias Braun committed
429
	(void) mode;
430
431
432
433
434
435
436

	assert(entry);

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

439
	irg = get_irn_irg(node);
440
441
442
443
444
445
	adr = get_Store_ptr(node);
	mem = get_Store_mem(node);
	block = get_nodes_block(node);

	if (env->params->little_endian) {
		low  = adr;
446
		high = new_r_Add(block, adr, new_r_Const(irg, env->tv_mode_bytes), get_irn_mode(adr));
Michael Beck's avatar
BugFix:    
Michael Beck committed
447
	} else {
448
		low  = new_r_Add(block, adr, new_r_Const(irg, env->tv_mode_bytes), get_irn_mode(adr));
449
		high = adr;
450
	}
451
452
453

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

458
	set_lowered(env, node, low, high);
459

460
461
	for (proj = (ir_node*)get_irn_link(node); proj;
	     proj = (ir_node*)get_irn_link(proj)) {
462
463
464
465
466
467
468
469
470
471
472
		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");
473
		}
474
475
476
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
477
478
	}
}
479
480
481

/**
 * Return a node containing the address of the intrinsic emulation function.
Michael Beck's avatar
Michael Beck committed
482
483
484
485
486
487
 *
 * @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 env     the lower environment
488
 */
Michael Beck's avatar
Michael Beck committed
489
490
static ir_node *get_intrinsic_address(ir_type *method, ir_op *op,
                                      ir_mode *imode, ir_mode *omode,
491
492
                                      lower_env_t *env)
{
493
	symconst_symbol sym;
494
	ir_entity *ent;
495
496
	op_mode_entry_t key, *entry;

Michael Beck's avatar
Michael Beck committed
497
498
499
500
	key.op    = op;
	key.imode = imode;
	key.omode = omode;
	key.ent   = NULL;
501

502
	entry = (op_mode_entry_t*)set_insert(intrinsic_fkt, &key, sizeof(key),
Michael Beck's avatar
Michael Beck committed
503
				HASH_PTR(op) ^ HASH_PTR(imode) ^ (HASH_PTR(omode) << 8));
504
505
	if (! entry->ent) {
		/* create a new one */
Michael Beck's avatar
Michael Beck committed
506
		ent = env->params->create_intrinsic(method, op, imode, omode, env->params->ctx);
507
508
509

		assert(ent && "Intrinsic creator must return an entity");
		entry->ent = ent;
Michael Beck's avatar
BugFix:    
Michael Beck committed
510
	} else {
511
		ent = entry->ent;
512
	}
513
	sym.entity_p = ent;
514
	return new_r_SymConst(env->irg, mode_P_code, sym, symconst_addr_ent);
515
}
516
517
518
519
520
521

/**
 * Translate a Div.
 *
 * Create an intrinsic Call.
 */
522
523
static void lower_Div(ir_node *node, ir_mode *mode, lower_env_t *env)
{
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
	ir_node            *left        = get_Div_left(node);
	ir_node            *right       = get_Div_right(node);
	const node_entry_t *left_entry  = get_node_entry(env, left);
	const node_entry_t *right_entry = get_node_entry(env, right);
	ir_node            *block       = get_nodes_block(node);
	dbg_info           *dbgi        = get_irn_dbg_info(node);
	ir_type            *mtp
		= mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
	ir_mode            *opmode = get_irn_op_mode(node);
	ir_node            *addr
		= get_intrinsic_address(mtp, get_irn_op(node), opmode, opmode, env);
	ir_node  *in[4] = {
		left_entry->low_word, left_entry->high_word,
		right_entry->low_word, right_entry->high_word };
	ir_node            *call
		= new_rd_Call(dbgi, block, get_Div_mem(node), addr, 4, in, mtp);
	ir_node            *resproj = new_r_Proj(call, mode_T, pn_Call_T_result);
	ir_node            *proj;
542
543
544

	set_irn_pinned(call, get_irn_pinned(node));

545
546
	for (proj = (ir_node*)get_irn_link(node); proj;
	     proj = (ir_node*)get_irn_link(proj)) {
547
548
549
550
		switch (get_Proj_proj(proj)) {
		case pn_Div_M:         /* Memory result. */
			/* reroute to the call */
			set_Proj_pred(proj, call);
551
			set_Proj_proj(proj, pn_Call_M);
552
553
554
555
556
557
			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;
558
559
560
561
562
		case pn_Div_res: {
			/* Result of computation. */
			ir_node *res_low  = new_r_Proj(resproj, env->low_unsigned, 0);
			ir_node *res_high = new_r_Proj(resproj, mode,              1);
			set_lowered(env, proj, res_low, res_high);
563
			break;
564
		}
565
566
		default:
			assert(0 && "unexpected Proj number");
567
		}
568
569
570
		/* mark this proj: we have handled it already, otherwise we might fall into
		 * out new nodes. */
		mark_irn_visited(proj);
571
572
	}
}
573
574
575
576
577
578

/**
 * Translate a Mod.
 *
 * Create an intrinsic Call.
 */
579
580
static void lower_Mod(ir_node *node, ir_mode *mode, lower_env_t *env)
{
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
	ir_node            *left        = get_Mod_left(node);
	ir_node            *right       = get_Mod_right(node);
	const node_entry_t *left_entry  = get_node_entry(env, left);
	const node_entry_t *right_entry = get_node_entry(env, right);
	ir_node            *in[4]       = {
		left_entry->low_word, left_entry->high_word,
		right_entry->low_word, right_entry->high_word
	};
	dbg_info           *dbgi        = get_irn_dbg_info(node);
	ir_node            *block       = get_nodes_block(node);
	ir_type            *mtp
		= mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
	ir_mode            *opmode = get_irn_op_mode(node);
	ir_node            *addr
		= get_intrinsic_address(mtp, get_irn_op(node), opmode, opmode, env);
	ir_node            *call
		= new_rd_Call(dbgi, block, get_Mod_mem(node), addr, 4, in, mtp);
	ir_node            *resproj = new_r_Proj(call, mode_T, pn_Call_T_result);
	ir_node            *proj;
600
601
	set_irn_pinned(call, get_irn_pinned(node));

602
603
	for (proj = (ir_node*)get_irn_link(node); proj;
	     proj = (ir_node*)get_irn_link(proj)) {
604
605
606
607
		switch (get_Proj_proj(proj)) {
		case pn_Mod_M:         /* Memory result. */
			/* reroute to the call */
			set_Proj_pred(proj, call);
608
			set_Proj_proj(proj, pn_Call_M);
609
610
611
612
613
614
			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;
615
616
617
618
619
		case pn_Mod_res: {
			/* Result of computation. */
			ir_node *res_low  = new_r_Proj(resproj, env->low_unsigned, 0);
			ir_node *res_high = new_r_Proj(resproj, mode,              1);
			set_lowered(env, proj, res_low, res_high);
620
			break;
621
		}
622
623
		default:
			assert(0 && "unexpected Proj number");
624
		}
625
626
		/* mark this proj: we have handled it already, otherwise we might fall
		 * into out new nodes. */
627
		mark_irn_visited(proj);
628
629
	}
}
630

631
632
static void lower_DivMod(ir_node *node, ir_mode *mode, lower_env_t *env)
{
633
634
635
636
	(void) node;
	(void) mode;
	(void) env;
	panic("DivMod is deprecated, no doubleword lowering");
637
}
638
639

/**
640
 * Translate a binop.
641
642
643
 *
 * Create an intrinsic Call.
 */
644
static void lower_binop(ir_node *node, ir_mode *mode, lower_env_t *env)
645
{
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
	ir_node            *left        = get_binop_left(node);
	ir_node            *right       = get_binop_right(node);
	const node_entry_t *left_entry  = get_node_entry(env, left);
	const node_entry_t *right_entry = get_node_entry(env, right);
	ir_node            *in[4]       = {
		left_entry->low_word, left_entry->high_word,
		right_entry->low_word, right_entry->high_word
	};
	dbg_info           *dbgi        = get_irn_dbg_info(node);
	ir_node            *block       = get_nodes_block(node);
	ir_graph           *irg         = get_irn_irg(block);
	ir_type            *mtp
		= mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
	ir_node            *addr
		= get_intrinsic_address(mtp, get_irn_op(node), mode, mode, env);
	ir_node            *call
		= new_rd_Call(dbgi, block, get_irg_no_mem(irg), addr, 4, in, mtp);
	ir_node            *resproj  = new_r_Proj(call, mode_T, pn_Call_T_result);
	ir_node            *res_low  = new_r_Proj(resproj, env->low_unsigned, 0);
	ir_node            *res_high = new_r_Proj(resproj, mode,              1);
	set_irn_pinned(call, get_irn_pinned(node));
	set_lowered(env, node, res_low, res_high);
668
}
669

Christian Würdig's avatar
Christian Würdig committed
670
671
672
673
674
/**
 * Translate a Shiftop.
 *
 * Create an intrinsic Call.
 */
675
676
static void lower_Shiftop(ir_node *node, ir_mode *mode, lower_env_t *env)
{
677
	ir_node            *block      = get_nodes_block(node);
678
679
680
681
	ir_node            *left       = get_binop_left(node);
	const node_entry_t *left_entry = get_node_entry(env, left);
	ir_node            *right      = get_binop_right(node);
	ir_node            *in[3]      = {
682
683
684
		left_entry->low_word, left_entry->high_word,
		/* it should be safe to conv to low_unsigned */
		new_r_Conv(block, right, env->low_unsigned)
685
686
687
688
689
690
691
692
693
694
695
696
	};
	dbg_info           *dbgi       = get_irn_dbg_info(node);
	ir_graph           *irg        = get_irn_irg(block);
	ir_type            *mtp
		= mode_is_signed(mode) ? shiftop_tp_s : shiftop_tp_u;
	ir_node            *addr
		= get_intrinsic_address(mtp, get_irn_op(node), mode, mode, env);
	ir_node            *call
		= new_rd_Call(dbgi, block, get_irg_no_mem(irg), addr, 3, in, mtp);
	ir_node            *resproj  = new_r_Proj(call, mode_T, pn_Call_T_result);
	ir_node            *res_low  = new_r_Proj(resproj, env->low_unsigned, 0);
	ir_node            *res_high = new_r_Proj(resproj, mode,              1);
Christian Würdig's avatar
Christian Würdig committed
697

698
699
	set_irn_pinned(call, get_irn_pinned(node));
	set_lowered(env, node, res_low, res_high);
700
}
Christian Würdig's avatar
Christian Würdig committed
701

Michael Beck's avatar
Michael Beck committed
702
703
704
/**
 * Translate a Shr and handle special cases.
 */
705
706
static void lower_Shr(ir_node *node, ir_mode *mode, lower_env_t *env)
{
707
	ir_graph *irg   = get_irn_irg(node);
708
	ir_node  *right = get_Shr_right(node);
Michael Beck's avatar
Michael Beck committed
709
710

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
711
		ir_tarval *tv = get_Const_tarval(right);
Michael Beck's avatar
Michael Beck committed
712
713

		if (tarval_is_long(tv) &&
714
		    get_tarval_long(tv) >= (long)get_mode_size_bits(mode)) {
715
716
			ir_node *block        = get_nodes_block(node);
			ir_node *left         = get_Shr_left(node);
717
			ir_mode *low_unsigned = env->low_unsigned;
718
			long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
719
720
721
			const node_entry_t *left_entry = get_node_entry(env, left);
			ir_node *res_low;
			ir_node *res_high;
Michael Beck's avatar
Michael Beck committed
722

723
			left = left_entry->high_word;
Matthias Braun's avatar
Matthias Braun committed
724

725
726
727
			/* convert high word into low_unsigned mode if necessary */
			if (get_irn_mode(left) != low_unsigned)
				left = new_r_Conv(block, left, low_unsigned);
Michael Beck's avatar
Michael Beck committed
728

729
			if (shf_cnt > 0) {
730
731
				ir_node *c = new_r_Const_long(irg, low_unsigned, shf_cnt);
				res_low = new_r_Shr(block, left, c, low_unsigned);
Michael Beck's avatar
BugFix:    
Michael Beck committed
732
			} else {
733
				res_low = left;
734
			}
735
736
			res_high = new_r_Const(irg, get_mode_null(mode));
			set_lowered(env, node, res_low, res_high);
Michael Beck's avatar
Michael Beck committed
737
738

			return;
739
740
		}
	}
Michael Beck's avatar
Michael Beck committed
741
	lower_Shiftop(node, mode, env);
742
}
Michael Beck's avatar
Michael Beck committed
743
744
745
746

/**
 * Translate a Shl and handle special cases.
 */
747
748
static void lower_Shl(ir_node *node, ir_mode *mode, lower_env_t *env)
{
749
	ir_graph *irg   = get_irn_irg(node);
750
	ir_node  *right = get_Shl_right(node);
Michael Beck's avatar
Michael Beck committed
751
752

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
753
		ir_tarval *tv = get_Const_tarval(right);
Michael Beck's avatar
Michael Beck committed
754

755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
		if (tarval_is_long(tv)) {
			long value = get_tarval_long(tv);
		    if (value >= (long)get_mode_size_bits(mode)) {
				/* simple case: shift above the lower word */
				ir_mode *mode_l;
				ir_node *block = get_nodes_block(node);
				ir_node *left = get_Shl_left(node);
				ir_node *c;
				long shf_cnt = get_tarval_long(tv) - get_mode_size_bits(mode);
				const node_entry_t *left_entry = get_node_entry(env, left);
				ir_node  *res_low;
				ir_node  *res_high;

				left = left_entry->low_word;
				left = new_r_Conv(block, left, mode);

				mode_l = env->low_unsigned;
				if (shf_cnt > 0) {
					c        = new_r_Const_long(irg, mode_l, shf_cnt);
					res_high = new_r_Shl(block, left, c, mode);
				} else {
					res_high = left;
				}
				res_low = new_r_Const(irg, get_mode_null(mode_l));
				set_lowered(env, node, res_low, res_high);

				return;
			}
			if (value == 1) {
				/* left << 1 == left + left */
				ir_node            *left        = get_binop_left(node);
				const node_entry_t *left_entry  = get_node_entry(env, left);
				ir_node            *in[4]       = {
					left_entry->low_word, left_entry->high_word,
					left_entry->low_word, left_entry->high_word,
				};
				dbg_info           *dbgi        = get_irn_dbg_info(node);
				ir_node            *block       = get_nodes_block(node);
				ir_graph           *irg         = get_irn_irg(block);
				ir_type            *mtp
					= mode_is_signed(mode) ? binop_tp_s : binop_tp_u;
				ir_node            *addr
					= get_intrinsic_address(mtp, op_Add, mode, mode, env);
				ir_node            *call
					= new_rd_Call(dbgi, block, get_irg_no_mem(irg), addr, 4, in, mtp);
				ir_node            *resproj  = new_r_Proj(call, mode_T, pn_Call_T_result);
				ir_node            *res_low  = new_r_Proj(resproj, env->low_unsigned, 0);
				ir_node            *res_high = new_r_Proj(resproj, mode,              1);
				set_irn_pinned(call, get_irn_pinned(node));
				set_lowered(env, node, res_low, res_high);

				return;
807
808
809
			}
		}
	}
Michael Beck's avatar
Michael Beck committed
810
	lower_Shiftop(node, mode, env);
811
}
Michael Beck's avatar
Michael Beck committed
812
813
814
815

/**
 * Translate a Shrs and handle special cases.
 */
816
817
static void lower_Shrs(ir_node *node, ir_mode *mode, lower_env_t *env)
{
818
	ir_graph *irg   = get_irn_irg(node);
819
	ir_node  *right = get_Shrs_right(node);
Michael Beck's avatar
Michael Beck committed
820
821

	if (get_mode_arithmetic(mode) == irma_twos_complement && is_Const(right)) {
Matthias Braun's avatar
Matthias Braun committed
822
		ir_tarval *tv = get_Const_tarval(right);
Michael Beck's avatar
Michael Beck committed
823
824

		if (tarval_is_long(tv) &&
825
		    get_tarval_long(tv) >= (long)get_mode_size_bits(mode)) {
826
827
			ir_node *block         = get_nodes_block(node);
			ir_node *left          = get_Shrs_left(node);
828
			ir_mode *low_unsigned  = env->low_unsigned;
829
			long     shf_cnt       = get_tarval_long(tv) - get_mode_size_bits(mode);
830
			const node_entry_t *left_entry = get_node_entry(env, left);
831
			ir_node *left_unsigned = left;
832
833
			ir_node *res_low;
			ir_node *res_high;
Michael Beck's avatar
Michael Beck committed
834
835
			ir_node *c;

836
			left = left_entry->high_word;
Michael Beck's avatar
Michael Beck committed
837

838
839
840
841
			/* convert high word into low_unsigned mode if necessary */
			if (get_irn_mode(left_unsigned) != low_unsigned)
				left_unsigned = new_r_Conv(block, left, low_unsigned);

842
			if (shf_cnt > 0) {
843
844
				c       = new_r_Const_long(irg, low_unsigned, shf_cnt);
				res_low = new_r_Shrs(block, left_unsigned, c, low_unsigned);
Michael Beck's avatar
BugFix:    
Michael Beck committed
845
			} else {
846
				res_low = left_unsigned;
847
			}
Michael Beck's avatar
Michael Beck committed
848

849
850
851
			c        = new_r_Const(irg, get_mode_all_one(low_unsigned));
			res_high = new_r_Shrs(block, left, c, mode);
			set_lowered(env, node, res_low, res_high);
Michael Beck's avatar
Michael Beck committed
852
			return;
853
854
		}
	}
Michael Beck's avatar
Michael Beck committed
855
	lower_Shiftop(node, mode, env);
856
}
Michael Beck's avatar
Michael Beck committed
857
858

/**
Michael Beck's avatar
Michael Beck committed
859
 * Rebuild Rotl nodes into Or(Shl, Shr) and prepare all nodes.
Michael Beck's avatar
Michael Beck committed
860
 */
861
862
static void prepare_links_and_handle_rotl(ir_node *node, void *env)
{
863
	lower_env_t *lenv = (lower_env_t*)env;
Michael Beck's avatar
Michael Beck committed
864
865

	if (is_Rotl(node)) {
866
867
		ir_mode  *mode = get_irn_op_mode(node);
		ir_node  *right;
868
		ir_node  *left, *shl, *shr, *ornode, *block, *sub, *c;
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
		ir_mode  *omode, *rmode;
		ir_graph *irg;
		dbg_info *dbg;
		optimization_state_t state;

		if (mode != lenv->high_signed && mode != lenv->high_unsigned) {
			prepare_links(lenv, node);
			return;
		}

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

		/* switch optimization off here, or we will get the Rotl back */
		save_optimization_state(&state);
		set_opt_algebraic_simplification(0);
895
		ornode = new_rd_Or(dbg, block, shl, shr, omode);
896
897
		restore_optimization_state(&state);

898
		exchange(node, ornode);
899
900
901
902
903
904

		/* do lowering on the new nodes */
		prepare_links(lenv, shl);
		prepare_links(lenv, c);
		prepare_links(lenv, sub);
		prepare_links(lenv, shr);
905
		prepare_links(lenv, ornode);
906
		return;
Michael Beck's avatar
Michael Beck committed
907
	}
908
909

	prepare_links(lenv, node);
Michael Beck's avatar
Michael Beck committed
910
}
Michael Beck's avatar
Michael Beck committed
911

912
913
914
915
916
/**
 * Translate an Unop.
 *
 * Create an intrinsic Call.
 */
917
918
static void lower_Unop(ir_node *node, ir_mode *mode, lower_env_t *env)
{
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
	ir_node            *op       = get_unop_op(node);
	const node_entry_t *op_entry = get_node_entry(env, op);
	ir_node            *in[2]    = { op_entry->low_word, op_entry->high_word };
	dbg_info           *dbgi     = get_irn_dbg_info(node);
	ir_node            *block    = get_nodes_block(node);
	ir_graph           *irg      = get_irn_irg(block);
	ir_type            *mtp      = mode_is_signed(mode) ? unop_tp_s : unop_tp_u;
	ir_op              *irop     = get_irn_op(node);
	ir_node            *addr     = get_intrinsic_address(mtp, irop, mode, mode, env);
	ir_node            *nomem    = get_irg_no_mem(irg);
	ir_node            *call     = new_rd_Call(dbgi, block, nomem, addr, 2, in, mtp);
	ir_node            *resproj  = new_r_Proj(call, mode_T, pn_Call_T_result);
	ir_node            *res_low  = new_r_Proj(resproj, env->low_unsigned, 0);
	ir_node            *res_high = new_r_Proj(resproj, mode,              1);
	set_irn_pinned(call, get_irn_pinned(node));
	set_lowered(env, node, res_low, res_high);
935
}
936
937

/**
938
 * Translate a logical binop.
939
 *
940
 * Create two logical binops.
941
 */
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
static void lower_binop_logical(ir_node *node, ir_mode *mode, lower_env_t *env,
								ir_node *(*constr_rd)(dbg_info *db, ir_node *block, ir_node *op1, ir_node *op2, ir_mode *mode) )
{
	ir_node            *left        = get_binop_left(node);
	ir_node            *right       = get_binop_right(node);
	const node_entry_t *left_entry  = get_node_entry(env, left);
	const node_entry_t *right_entry = get_node_entry(env, right);
	dbg_info           *dbgi        = get_irn_dbg_info(node);
	ir_node            *block       = get_nodes_block(node);
	ir_node            *res_low
		= constr_rd(dbgi, block, left_entry->low_word, right_entry->low_word,
		            env->low_unsigned);
	ir_node            *res_high
		= constr_rd(dbgi, block, left_entry->high_word, right_entry->high_word,
		            mode);
	set_lowered(env, node, res_low, res_high);
}
959

960
961
962
static void lower_And(ir_node *node, ir_mode *mode, lower_env_t *env)
{
	lower_binop_logical(node, mode, env, new_rd_And);
963
}
964

965
966
967
static void lower_Or(ir_node *node, ir_mode *mode, lower_env_t *env)
{
	lower_binop_logical(node, mode, env, new_rd_Or);
968
969
}

970
971
972
973
static void lower_Eor(ir_node *node, ir_mode *mode, lower_env_t *env)
{
	lower_binop_logical(node, mode, env, new_rd_Eor);
}
974
975
976
977
978
979

/**
 * Translate a Not.
 *
 * Create two logical Nots.
 */
980
981
static void lower_Not(ir_node *node, ir_mode *mode, lower_env_t *env)
{
982
983
984
985
986
987
988
989
990
	ir_node            *op       = get_Not_op(node);
	const node_entry_t *op_entry = get_node_entry(env, op);
	dbg_info           *dbgi     = get_irn_dbg_info(node);
	ir_node            *block    = get_nodes_block(node);
	ir_node            *res_low
		= new_rd_Not(dbgi, block, op_entry->low_word, env->low_unsigned);
	ir_node            *res_high
		= new_rd_Not(dbgi, block, op_entry->high_word, mode);
	set_lowered(env, node, res_low, res_high);
991
}
992
993
994
995

/**
 * Translate a Cond.
 */
996
997
static void lower_Cond(ir_node *node, ir_mode *mode, lower_env_t *env)
{
998
999
1000
	ir_node *cmp, *left, *right, *block;
	ir_node *sel = get_Cond_selector(node);
	ir_mode *m = get_irn_mode(sel);
1001
1002
1003
1004
1005
1006
1007
1008
1009
	ir_mode *cmp_mode;
	const 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;
	pn_Cmp   pnc;
	ir_graph *irg;
	dbg_info *dbg;
1010

1011
	(void) mode;
1012

1013
1014
1015
1016
1017
1018
1019
	if (m != mode_b) {
		if (m == env->high_signed || m == env->high_unsigned) {
			/* bad we can't really handle Switch with 64bit offsets */
			panic("Cond with 64bit jumptable not supported");
		}
		return;
	}
1020

1021
1022
	if (!is_Proj(sel))
		return;
1023

1024
1025
1026
	cmp = get_Proj_pred(sel);
	if (!is_Cmp(cmp))
		return;
1027

1028
1029
1030
1031
	left     = get_Cmp_left(cmp);
	cmp_mode = get_irn_mode(left);
	if (cmp_mode != env->high_signed && cmp_mode != env->high_unsigned)
		return;
1032

1033
1034
1035
	right  = get_Cmp_right(cmp);
	lentry = get_node_entry(env, left);
	rentry = get_node_entry(env, right);
1036

1037
	/* all right, build the code */
1038
1039
	for (proj = (ir_node*)get_irn_link(node); proj;
	     proj = (ir_node*)get_irn_link(proj)) {
1040
		long proj_nr = get_Proj_proj(proj);
1041

1042
1043
1044
1045
1046
1047
1048
		if (proj_nr == pn_Cond_true) {
			assert(projT == NULL && "more than one Proj(true)");
			projT = proj;
		} else {
			assert(proj_nr == pn_Cond_false);
			assert(projF == NULL && "more than one Proj(false)");
			projF = proj;
1049
		}
1050
1051
1052
		mark_irn_visited(proj);
	}
	assert(projT && projF);
1053

1054
1055
1056
1057
	/* create a new high compare */
	block = get_nodes_block(node);
	irg   = get_Block_irg(block);
	dbg   = get_irn_dbg_info(cmp);
1058
	pnc   = get_Proj_pn_cmp(sel);
1059
1060
1061
1062

	if (is_Const(right) && is_Const_null(right