lower_softfloat.c 29.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
 *
 * 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.
 */

/**
 * @file
 * @brief   Lower floating point operations to function calls
 * @author  Sebastian Buchwald
 */
#include "config.h"

#include <stdbool.h>

#include "be.h"
#include "dbginfo_t.h"
#include "debug.h"
#include "error.h"
#include "ircons.h"
#include "iredges.h"
#include "irgmod.h"
#include "irnodeset.h"
#include "irgwalk.h"
#include "irmode.h"
#include "iropt_dbg.h"
#include "irprog_t.h"
#include "lower_softfloat.h"
#include "lowering.h"
#include "pmap.h"
#include "type_t.h"
#include "tv_t.h"

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

typedef void (*lower_softfloat_func)(ir_node *node);

static ir_type *binop_tp_d;
static ir_type *binop_tp_f;
static ir_type *cmp_tp_d;
static ir_type *cmp_tp_f;
static ir_type *unop_tp_d;
static ir_type *unop_tp_f;
static ir_type *unop_tp_d_f;
static ir_type *unop_tp_d_is;
static ir_type *unop_tp_d_iu;
static ir_type *unop_tp_d_ls;
static ir_type *unop_tp_d_lu;
static ir_type *unop_tp_f_d;
static ir_type *unop_tp_f_is;
static ir_type *unop_tp_f_iu;
static ir_type *unop_tp_f_ls;
static ir_type *unop_tp_f_lu;
static ir_type *unop_tp_is_d;
static ir_type *unop_tp_is_f;
static ir_type *unop_tp_iu_d;
static ir_type *unop_tp_iu_f;
static ir_type *unop_tp_ls_d;
static ir_type *unop_tp_ls_f;
static ir_type *unop_tp_lu_d;
static ir_type *unop_tp_lu_f;

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

80
static ir_nodeset_t created_mux_nodes;
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

/**
 * @return The lowered (floating point) mode.
 */
static ir_mode *get_lowered_mode(ir_mode *mode)
{
	if (! mode_is_float(mode))
		return mode;

	if (mode == mode_F)
		return mode_Iu;
	else if (mode == mode_D)
		return mode_Lu;

	panic("Unsupported floating point type");
}

/**
 * Adapts the mode of the given node.
 */
static void lower_mode(ir_node *n, void *env)
{
	ir_op                *op         = get_irn_op(n);
	lower_softfloat_func  lower_func = (lower_softfloat_func) op->ops.generic;
	ir_mode              *mode       = get_irn_mode(n);

	(void) env;

	if (lower_func != NULL) {
		lower_func(n);
		return;
	}

	set_irn_mode(n, get_lowered_mode(mode));
}

/**
 * Wrapper for specific lower function.
 */
static void lower_node(ir_node *n, void *env)
{
	ir_op                *op         = get_irn_op(n);
	lower_softfloat_func  lower_func = (lower_softfloat_func) op->ops.generic;

	(void) env;

	if (lower_func != NULL)
		lower_func(n);
}

/**
 * @return The type of the function replacing the given node.
 */
static ir_type *get_softfloat_type(const ir_node *n)
{
	unsigned opcode       = get_irn_opcode(n);
	ir_mode *mode         = get_irn_mode(n);
	ir_node *operand      = get_irn_n(n, 0);
	ir_mode *operand_mode = get_irn_mode(operand);

	switch (opcode) {
	case iro_Div:
		mode         = get_Div_resmode(n);
		operand_mode = get_irn_mode(get_Div_left(n));
		/* fall through */
	case iro_Add:
	case iro_Mul:
	case iro_Sub:
		if (operand_mode == mode_F)
			return binop_tp_f;
		else if (operand_mode == mode_D)
			return binop_tp_d;
		break;
	case iro_Cmp:
		if (operand_mode == mode_F)
			return cmp_tp_f;
		else if (operand_mode == mode_D)
			return cmp_tp_d;
		break;
	case iro_Conv:
		if (operand_mode == mode_D) {
			if (mode == mode_F)
				return unop_tp_d_f;
			else if (mode == mode_Is || mode == mode_Hs || mode == mode_Bs)
				return unop_tp_d_is;
			else if (mode == mode_Iu || mode == mode_Hu || mode == mode_Bu)
				return unop_tp_d_iu;
			else if (mode == mode_Ls)
				return unop_tp_d_ls;
			else if (mode == mode_Lu)
				return unop_tp_d_lu;
		}
		else if (operand_mode == mode_F) {
			if (mode == mode_D)
				return unop_tp_f_d;
			else if (mode == mode_Is || mode == mode_Hs || mode == mode_Bs)
				return unop_tp_f_is;
			else if (mode == mode_Iu || mode == mode_Hu || mode == mode_Bu)
				return unop_tp_f_iu;
			else if (mode == mode_Ls)
				return unop_tp_f_ls;
			else if (mode == mode_Lu)
				return unop_tp_f_lu;
		}
		else if (operand_mode == mode_Is || operand_mode == mode_Hs || operand_mode == mode_Bs) {
			if (mode == mode_D)
				return unop_tp_is_d;
			else if (mode == mode_F)
				return unop_tp_is_f;
		}
		else if (operand_mode == mode_Iu || operand_mode == mode_Hu || operand_mode == mode_Bu) {
			if (mode == mode_D)
				return unop_tp_iu_d;
			else if (mode == mode_F)
				return unop_tp_iu_f;
		}
		else if (operand_mode == mode_Ls) {
			if (mode == mode_D)
				return unop_tp_ls_d;
			else if (mode == mode_F)
				return unop_tp_ls_f;
		}
		else if (operand_mode == mode_Lu) {
			if (mode == mode_D)
				return unop_tp_lu_d;
			else if (mode == mode_F)
				return unop_tp_lu_f;
		}
		break;
	case iro_Minus:
		if (operand_mode == mode_F)
			return unop_tp_f;
		else if (operand_mode == mode_D)
			return unop_tp_d;
		break;
	default: break;
	}

	assert(0 && "Could not determine a suitable type");

	return NULL;
}

/**
 * @return A SymConst representing the function that replaces the given node.
 */
static ir_node *create_softfloat_symconst(const ir_node *n, const char *name)
{
	char             buf[16];
	const char      *first_param = "";
	const char      *second_param = "";
	const char      *result = "";
	ir_entity       *ent;
	size_t           n_params;
	ident           *id;
	symconst_symbol  sym;
	unsigned         float_types  = 0;
	unsigned         double_types  = 0;
	ir_graph        *irg    = get_irn_irg(n);
	ir_type         *method = get_softfloat_type(n);

	n_params = get_method_n_params(method);

	/* Parameter types. */
	switch (n_params) {
	case 2:
		{
			ir_type *param_type = get_method_param_type(method, 1);
			ir_mode *mode       = get_type_mode(param_type);

			if (mode == mode_F) {
				second_param = "sf";
				float_types++;
			}
			else if (mode == mode_D) {
				second_param = "df";
				double_types++;
			}
			else if (mode == mode_Iu || mode == mode_Is)
				second_param = "si";
			else if (mode == mode_Lu || mode == mode_Ls)
				second_param = "di";
		}
		/* fall through */
	case 1:
		{
			ir_type *param_type = get_method_param_type(method, 0);
			ir_mode *mode       = get_type_mode(param_type);

			if (mode == mode_F) {
				first_param = float_types > 0 ? "" : "sf";
				float_types++;
			}
			else if (mode == mode_D) {
				first_param = double_types > 0 ? "" : "df";
				double_types++;
			}
			else if (mode == mode_Iu || mode == mode_Is)
				first_param = "si";
			else if (mode == mode_Lu || mode == mode_Ls)
				first_param = "di";
		}
		break;
	default:
		break;
	}

	/* Result type. */
	{
		ir_mode *mode;

		if (is_Div(n))
			mode = get_Div_resmode(n);
		else
			mode = get_irn_mode(n);

		if (mode == mode_F) {
			result = float_types > 0 ? "" : "sf";
			float_types++;
		}
		else if (mode == mode_D) {
			result = double_types > 0 ? "" : "df";
			double_types++;
		}
		else if (mode == mode_Iu || mode == mode_Hu || mode == mode_Bu
				|| mode == mode_Is || mode == mode_Hs || mode == mode_Bs)
			result = "si";
		else if (mode == mode_Lu || mode == mode_Ls)
			result = "di";
	}

	assert(float_types <= 3);
	assert(double_types <= 3);

	if (float_types + double_types > 1)
316
		snprintf(buf, sizeof(buf), "__%s%s%s%s%u", name, first_param, second_param, result, float_types + double_types);
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
	else
		snprintf(buf, sizeof(buf), "__%s%s%s%s", name, first_param, second_param, result);

	id  = new_id_from_str(buf);
	ent = new_entity(get_glob_type(), id, method);

	set_entity_ld_ident(ent, get_entity_ident(ent));
	sym.entity_p = ent;

	return new_r_SymConst(irg, mode_P_code, sym, symconst_addr_ent);
}

/**
 * Transforms an Add into the appropriate soft float function.
 */
static void lower_Add(ir_node *n)
{
	ir_node         *symconst;
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_node         *left        = get_Add_left(n);
	ir_node         *right       = get_Add_right(n);
	ir_mode         *mode        = get_irn_mode(n);

	if (! mode_is_float(mode))
		return;

	symconst = create_softfloat_symconst(n, "add");

	ir_node *call;
	ir_node *call_results;
	ir_node *in[2]    = {left, right};
	ir_node *nomem    = get_irg_no_mem(irg);
	ir_type *type     = get_softfloat_type(n);
	ir_mode *res_mode = get_type_mode(get_method_res_type(type, 0));

	call         = new_rd_Call(dbgi, block, nomem, symconst, 2, in, type);
	call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
	call_result  = new_r_Proj(call_results, res_mode, 0);

	exchange(n, call_result);
}

/**
 * @return The lowered (floating point) type.
 */
static ir_type *lower_type(ir_type *tp)
{
	ir_mode *mode         = get_type_mode(tp);
	ir_mode *lowered_mode = get_lowered_mode(mode);

	return get_type_for_mode(lowered_mode);
}

/**
 * @return The lowered method type.
 */
static ir_type *lower_method_type(ir_type *mtp)
{
	ir_type *res;
	size_t   i;
	size_t   n_param;
	size_t   n_res;

	res = (ir_type*)pmap_get(lowered_type, mtp);
	if (res != NULL)
		return res;

	n_param = get_method_n_params(mtp);
	n_res   = get_method_n_ress(mtp);

	res = new_type_method(n_param, n_res);

	/* set param types and result types */
	for (i = 0; i < n_param; ++i) {
		ir_type *ptp   = get_method_param_type(mtp, i);
		ir_mode *pmode = get_type_mode(ptp);

		if (pmode != NULL && mode_is_float(pmode)) {
			ptp = lower_type(ptp);
		}

		set_method_param_type(res, i, ptp);
	}
	for (i = 0; i < n_res; ++i) {
		ir_type *rtp   = get_method_res_type(mtp, i);
		ir_mode *rmode = get_type_mode(rtp);

		if (rmode != NULL && mode_is_float(rmode)) {
			rtp = lower_type(rtp);
		}

		set_method_res_type(res, i, rtp);
	}

	set_method_variadicity(res, get_method_variadicity(mtp));
	set_method_calling_convention(res, get_method_calling_convention(mtp));
	set_method_additional_properties(res, get_method_additional_properties(mtp));

	set_lowered_type(mtp, res);

	pmap_insert(lowered_type, mtp, res);
	return res;
}

/**
 * Adapts the method type of a Call.
 */
static void lower_Call(ir_node *node)
{
	ir_type  *tp = get_Call_type(node);
	size_t   n_params;
	size_t   n_res;
	bool     need_lower = false;
	size_t   i;
	size_t   p;

	n_params = get_method_n_params(tp);

	for (p = 0; p < n_params; ++p) {
		ir_type *ptp   = get_method_param_type(tp, p);
		ir_mode *pmode = get_type_mode(ptp);

		if (pmode == NULL)
			continue;

		if (mode_is_float(pmode)) {
			need_lower = true;
			break;
		}
	}

	n_res = get_method_n_ress(tp);

	for (i = 0; i < n_res; ++i) {
		ir_type *rtp   = get_method_res_type(tp, i);
		ir_mode *rmode = get_type_mode(rtp);

		if (rmode == NULL)
			continue;

		if (mode_is_float(rmode)) {
			need_lower = true;
			break;
		}
	}

	if (! need_lower)
		return;

	tp = lower_method_type(tp);
	set_Call_type(node, tp);
}

/**
 * Transforms a Cmp into the appropriate soft float function.
 */
static void lower_Cmp(ir_node *n)
{
478
	ir_node         *symconst    = NULL;
479
480
481
482
483
484
485
486
487
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_node         *left        = get_Cmp_left(n);
	ir_relation      relation    = get_Cmp_relation(n);
	ir_mode         *op_mode     = get_irn_mode(left);
	ir_node         *right       = get_Cmp_right(n);
	ir_node         *symconst2   = NULL;
488
	ir_node         *zero        = new_rd_Const(dbgi, irg, get_mode_null(mode_Is));
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
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
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
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
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106

	if (! mode_is_float(op_mode))
		return;

	switch (relation) {
	case ir_relation_false:
		call_result = zero;
		break;
	case ir_relation_equal:
		symconst = create_softfloat_symconst(n, "eq");
		break;
	case ir_relation_less:
		symconst = create_softfloat_symconst(n, "lt");
		break;
	case ir_relation_greater:
		symconst = create_softfloat_symconst(n, "gt");
		break;
	case ir_relation_unordered:
		symconst = create_softfloat_symconst(n, "unord");
		relation = ir_relation_less_greater;
		break;
	case ir_relation_less_equal:
		symconst = create_softfloat_symconst(n, "le");
		break;
	case ir_relation_greater_equal:
		symconst = create_softfloat_symconst(n, "ge");
		break;
	case ir_relation_less_greater:
		symconst  = create_softfloat_symconst(n, "unord");
		symconst2 = create_softfloat_symconst(n, "ne");
		break;
	case ir_relation_less_equal_greater:
		symconst = create_softfloat_symconst(n, "unord");
		relation = ir_relation_equal;
		break;
	case ir_relation_unordered_equal:
		symconst  = create_softfloat_symconst(n, "unord");
		relation  = ir_relation_less_greater;
		symconst2 = create_softfloat_symconst(n, "ne");
		break;
	case ir_relation_unordered_less:
		symconst = create_softfloat_symconst(n, "ge");
		relation = ir_relation_less;
		break;
	case ir_relation_unordered_less_equal:
		symconst = create_softfloat_symconst(n, "gt");
		relation = ir_relation_less_equal;
		break;
	case ir_relation_unordered_greater:
		symconst = create_softfloat_symconst(n, "le");
		relation = ir_relation_greater;
		break;
	case ir_relation_unordered_greater_equal:
		symconst = create_softfloat_symconst(n, "lt");
		relation = ir_relation_greater_equal;
		break;
	case ir_relation_unordered_less_greater:
		symconst = create_softfloat_symconst(n, "eq");
		relation = ir_relation_less_greater;
		break;
	case ir_relation_true:
		call_result = zero;
		break;
	}

	if (call_result == NULL) {
		ir_node *call;
		ir_node *call_results;
		ir_node *in[2] = {left, right};
		ir_node *nomem = get_irg_no_mem(irg);
		ir_type *type  = get_softfloat_type(n);

		call         = new_rd_Call(dbgi, block, nomem, symconst, 2, in, type);
		call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
		call_result  = new_r_Proj(call_results, mode_Is, 0);
	}

	ir_node *cmp = new_r_Cmp(block, call_result, zero, relation);

	/* We need two calls into the softfloat library */
	if (symconst2 != NULL) {
		ir_node *call;
		ir_node *call_results;
		ir_node *mux;
		arch_allow_ifconv_func allow_ifconv = be_get_backend_param()->allow_ifconv;
		ir_node *in[2]                      = {left, right};
		ir_node *nomem                      = get_irg_no_mem(irg);
		ir_type *type                       = get_softfloat_type(n);

		call         = new_rd_Call(dbgi, block, nomem, symconst2, 2, in, type);
		call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
		call_result  = new_r_Proj(call_results, mode_Is, 0);
		relation     = get_Cmp_relation(n);

		mux = new_rd_Mux(dbgi, block, cmp, call_result, zero, mode_Is);

		if (! allow_ifconv(cmp, call_result, zero))
			ir_nodeset_insert(&created_mux_nodes, mux);

		cmp = new_r_Cmp(block, mux, zero, relation);
	}

	exchange(n, cmp);
}

static const tarval_mode_info hex_output = {
	TVO_HEX,
	"0x",
	NULL,
};

/**
 * Adapts floating point constants.
 */
static void lower_Const(ir_node *n)
{
	ir_mode   *lowered_mode;
	ir_mode   *mode         = get_irn_mode(n);
	ir_tarval *tv = get_Const_tarval(n);
	char       buf[100];
	size_t     len;

	if (! mode_is_float(mode))
		return;

	lowered_mode = get_lowered_mode(mode);
	set_irn_mode(n, lowered_mode);

	set_tarval_mode_output_option(mode, &hex_output);
	tarval_snprintf(buf, 100, get_Const_tarval(n));

	len = strlen(buf);
	tv  = new_tarval_from_str(buf, len, lowered_mode);
	set_Const_tarval(n, tv);
}

/**
 * Transforms a Conv into the appropriate soft float function.
 */
static void lower_Conv(ir_node *n)
{
	ir_node         *symconst;
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_node         *op          = get_Conv_op(n);
	ir_mode         *mode        = get_irn_mode(n);
	ir_mode         *op_mode     = get_irn_mode(op);

	if (! mode_is_float(mode) && ! mode_is_float(op_mode))
		return;

	/* Remove unnecessary Convs. */
	if (op_mode == mode) {
		exchange(n, op);
		return;
	}
	else if (op_mode == mode_Hs || op_mode == mode_Bs) {
		op_mode = mode_Is;
		op     = new_rd_Conv(dbgi, block, op, op_mode);
	}
	else if (op_mode == mode_Hu || op_mode == mode_Bu) {
		op_mode = mode_Iu;
		op     = new_rd_Conv(dbgi, block, op, op_mode);
	}

	if (mode_is_float(op_mode) && mode_is_float(mode)) {
		if (get_mode_size_bits(op_mode) > get_mode_size_bits(mode))
			symconst = create_softfloat_symconst(n, "trunc");
		else
			symconst = create_softfloat_symconst(n, "extend");
	}
	else if (mode_is_float(op_mode)) {
		if (mode_is_signed(mode))
			symconst = create_softfloat_symconst(n, "fix");
		else
			symconst = create_softfloat_symconst(n, "fixuns");
	}
	else {
		if (mode_is_signed(op_mode))
			symconst = create_softfloat_symconst(n, "float");
		else
			symconst = create_softfloat_symconst(n, "floatun");
	}

	ir_node *call;
	ir_node *call_results;
	ir_node *in[1]    = {op};
	ir_node *nomem    = get_irg_no_mem(irg);
	ir_type *type     = get_softfloat_type(n);
	ir_mode *res_mode = get_type_mode(get_method_res_type(type, 0));

	call         = new_rd_Call(dbgi, block, nomem, symconst, 1, in, type);
	call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
	call_result  = new_r_Proj(call_results, res_mode, 0);

	/* Check whether we need a Conv for the result. */
	if (res_mode != mode)
		call_result = new_rd_Conv(dbgi, block, call_result, mode);

	exchange(n, call_result);
}

/**
 * Transforms a Div into the appropriate soft float function.
 */
static void lower_Div(ir_node *n)
{
	ir_node         *symconst;
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_node         *left        = get_Div_left(n);
	ir_mode         *mode        = get_Div_resmode(n);
	ir_node         *right       = get_Div_right(n);
	const ir_edge_t *edge;
	const ir_edge_t *next;

	if (! mode_is_float(mode))
		return;

	symconst = create_softfloat_symconst(n, "div");

	ir_node *call;
	ir_node *call_results;
	ir_node *in[2]    = {left, right};
	ir_node *nomem    = get_irg_no_mem(irg);
	ir_type *type     = get_softfloat_type(n);
	ir_mode *res_mode = get_type_mode(get_method_res_type(type, 0));

	call         = new_rd_Call(dbgi, block, nomem, symconst, 2, in, type);
	call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
	call_result  = new_r_Proj(call_results, res_mode, 0);

	set_irn_pinned(call, get_irn_pinned(n));

	foreach_out_edge_safe(n, edge, next) {
		ir_node *proj = get_edge_src_irn(edge);
		if (! is_Proj(proj))
			continue;

		switch (get_Proj_proj(proj)) {
		case pn_Div_M:
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_M);
			break;
		case pn_Div_X_regular:
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_X_regular);
			break;
		case pn_Div_X_except:
			set_Proj_pred(proj, call);
			set_Proj_proj(proj, pn_Call_X_except);
			break;
		case pn_Div_res:
			exchange(proj, call_result);
			break;
		default:
			assert(0 && "unexpected Proj number");
		}
	}

}

/**
 * Adapts the resmode of a Div.
 */
static void lower_Div_mode(ir_node *n)
{
	ir_mode *res_mode         = get_Div_resmode(n);
	ir_mode *lowered_res_mode = get_lowered_mode(res_mode);
	ir_mode *mode             = get_irn_mode(n);
	ir_mode *lowered_mode     = get_lowered_mode(mode);

	set_irn_mode(n, lowered_mode);
	set_Div_resmode(n, lowered_res_mode);
}

/**
 * Adapts the ls_mode of a Load.
 */
static void lower_Load(ir_node *n)
{
	ir_mode *ls_mode         = get_Load_mode(n);
	ir_mode *lowered_ls_mode = get_lowered_mode(ls_mode);
	ir_mode *mode            = get_irn_mode(n);
	ir_mode *lowered_mode    = get_lowered_mode(mode);

	set_irn_mode(n, lowered_mode);
	set_Load_mode(n, lowered_ls_mode);
}

/**
 * Transforms a Minus into the appropriate soft float function.
 */
static void lower_Minus(ir_node *n)
{
	ir_node         *symconst;
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_mode         *mode        = get_irn_mode(n);
	ir_node         *op          = get_Minus_op(n);

	if (! mode_is_float(mode))
		return;

	symconst = create_softfloat_symconst(n, "neg");

	ir_node *call;
	ir_node *call_results;
	ir_node *in[1]    = {op};
	ir_node *nomem    = get_irg_no_mem(irg);
	ir_type *type     = get_softfloat_type(n);
	ir_mode *res_mode = get_type_mode(get_method_res_type(type, 0));

	call         = new_rd_Call(dbgi, block, nomem, symconst, 1, in, type);
	call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
	call_result  = new_r_Proj(call_results, res_mode, 0);

	exchange(n, call_result);
}

/**
 * Transforms a Mul into the appropriate soft float function.
 */
static void lower_Mul(ir_node *n)
{
	ir_node         *symconst;
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_node         *left        = get_Mul_left(n);
	ir_mode         *mode        = get_irn_mode(n);
	ir_node         *right       = get_Mul_right(n);

	if (! mode_is_float(mode))
		return;

	symconst = create_softfloat_symconst(n, "mul");

	ir_node *call;
	ir_node *call_results;
	ir_node *in[2]    = {left, right};
	ir_node *nomem    = get_irg_no_mem(irg);
	ir_type *type     = get_softfloat_type(n);
	ir_mode *res_mode = get_type_mode(get_method_res_type(type, 0));

	call         = new_rd_Call(dbgi, block, nomem, symconst, 2, in, type);
	call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
	call_result  = new_r_Proj(call_results, res_mode, 0);

	exchange(n, call_result);
}

/**
 * Transforms a Sub into the appropriate soft float function.
 */
static void lower_Sub(ir_node *n)
{
	ir_node         *symconst;
	ir_node         *block       = get_nodes_block(n);
	ir_node         *call_result = NULL;
	dbg_info        *dbgi        = get_irn_dbg_info(n);
	ir_graph        *irg         = get_irn_irg(n);
	ir_node         *left        = get_Sub_left(n);
	ir_mode         *mode        = get_irn_mode(n);
	ir_node         *right       = get_Sub_right(n);

	if (! mode_is_float(mode))
		return;

	symconst = create_softfloat_symconst(n, "sub");

	ir_node *call;
	ir_node *call_results;
	ir_node *in[2]    = {left, right};
	ir_node *nomem    = get_irg_no_mem(irg);
	ir_type *type     = get_softfloat_type(n);
	ir_mode *res_mode = get_type_mode(get_method_res_type(type, 0));

	call         = new_rd_Call(dbgi, block, nomem, symconst, 2, in, type);
	call_results = new_r_Proj(call, mode_T, pn_Call_T_result);
	call_result  = new_r_Proj(call_results, res_mode, 0);

	exchange(n, call_result);
}

/**
 * Enter a lowering function into an ir_op.
 */
static void ir_register_softloat_lower_function(ir_op *op, lower_softfloat_func func)
{
	op->ops.generic = (op_func)func;
}

/*
 * Initializes softfloat lowering.
 */
static void ir_prepare_softfloat_lowering(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.lower.softfloat");

	if (! lowered_type)
		lowered_type = pmap_create();

	if (! binop_tp_d) {
		binop_tp_d = new_type_method(2, 1);
		set_method_param_type(binop_tp_d, 0, get_type_for_mode(mode_D));
		set_method_param_type(binop_tp_d, 1, get_type_for_mode(mode_D));
		set_method_res_type(binop_tp_d, 0, get_type_for_mode(mode_D));
	}

	if (! binop_tp_f) {
		binop_tp_f = new_type_method(2, 1);
		set_method_param_type(binop_tp_f, 0, get_type_for_mode(mode_F));
		set_method_param_type(binop_tp_f, 1, get_type_for_mode(mode_F));
		set_method_res_type(binop_tp_f, 0, get_type_for_mode(mode_F));
	}

	if (! cmp_tp_d) {
		cmp_tp_d = new_type_method(2, 1);
		set_method_param_type(cmp_tp_d, 0, get_type_for_mode(mode_D));
		set_method_param_type(cmp_tp_d, 1, get_type_for_mode(mode_D));
		set_method_res_type(cmp_tp_d, 0, get_type_for_mode(mode_Is));
	}

	if (! cmp_tp_f) {
		cmp_tp_f = new_type_method(2, 1);
		set_method_param_type(cmp_tp_f, 0, get_type_for_mode(mode_F));
		set_method_param_type(cmp_tp_f, 1, get_type_for_mode(mode_F));
		set_method_res_type(cmp_tp_f, 0, get_type_for_mode(mode_Is));
	}

	if (! unop_tp_d) {
		unop_tp_d = new_type_method(1, 1);
		set_method_param_type(unop_tp_d, 0, get_type_for_mode(mode_D));
		set_method_res_type(unop_tp_d, 0, get_type_for_mode(mode_D));
	}

	if (! unop_tp_f) {
		unop_tp_f = new_type_method(1, 1);
		set_method_param_type(unop_tp_f, 0, get_type_for_mode(mode_F));
		set_method_res_type(unop_tp_f, 0, get_type_for_mode(mode_F));
	}

	if (! unop_tp_d_f) {
		unop_tp_d_f = new_type_method(1, 1);
		set_method_param_type(unop_tp_d_f, 0, get_type_for_mode(mode_D));
		set_method_res_type(unop_tp_d_f, 0, get_type_for_mode(mode_F));
	}

	if (! unop_tp_d_is) {
		unop_tp_d_is = new_type_method(1, 1);
		set_method_param_type(unop_tp_d_is, 0, get_type_for_mode(mode_D));
		set_method_res_type(unop_tp_d_is, 0, get_type_for_mode(mode_Is));
	}

	if (! unop_tp_d_iu) {
		unop_tp_d_iu = new_type_method(1, 1);
		set_method_param_type(unop_tp_d_iu, 0, get_type_for_mode(mode_D));
		set_method_res_type(unop_tp_d_iu, 0, get_type_for_mode(mode_Iu));
	}

	if (! unop_tp_d_ls) {
		unop_tp_d_ls = new_type_method(1, 1);
		set_method_param_type(unop_tp_d_ls, 0, get_type_for_mode(mode_D));
		set_method_res_type(unop_tp_d_ls, 0, get_type_for_mode(mode_Ls));
	}

	if (! unop_tp_d_lu) {
		unop_tp_d_lu = new_type_method(1, 1);
		set_method_param_type(unop_tp_d_lu, 0, get_type_for_mode(mode_D));
		set_method_res_type(unop_tp_d_lu, 0, get_type_for_mode(mode_Lu));
	}

	if (! unop_tp_f_d) {
		unop_tp_f_d = new_type_method(1, 1);
		set_method_param_type(unop_tp_f_d, 0, get_type_for_mode(mode_F));
		set_method_res_type(unop_tp_f_d, 0, get_type_for_mode(mode_D));
	}

	if (! unop_tp_f_is) {
		unop_tp_f_is = new_type_method(1, 1);
		set_method_param_type(unop_tp_f_is, 0, get_type_for_mode(mode_F));
		set_method_res_type(unop_tp_f_is, 0, get_type_for_mode(mode_Is));
	}

	if (! unop_tp_f_iu) {
		unop_tp_f_iu = new_type_method(1, 1);
		set_method_param_type(unop_tp_f_iu, 0, get_type_for_mode(mode_F));
		set_method_res_type(unop_tp_f_iu, 0, get_type_for_mode(mode_Iu));
	}

	if (! unop_tp_f_ls) {
		unop_tp_f_ls = new_type_method(1, 1);
		set_method_param_type(unop_tp_f_ls, 0, get_type_for_mode(mode_F));
		set_method_res_type(unop_tp_f_ls, 0, get_type_for_mode(mode_Ls));
	}

	if (! unop_tp_f_lu) {
		unop_tp_f_lu = new_type_method(1, 1);
		set_method_param_type(unop_tp_f_lu, 0, get_type_for_mode(mode_F));
		set_method_res_type(unop_tp_f_lu, 0, get_type_for_mode(mode_Lu));
	}

	if (! unop_tp_is_d) {
		unop_tp_is_d = new_type_method(1, 1);
		set_method_param_type(unop_tp_is_d, 0, get_type_for_mode(mode_Is));
		set_method_res_type(unop_tp_is_d, 0, get_type_for_mode(mode_D));
	}

	if (! unop_tp_is_f) {
		unop_tp_is_f = new_type_method(1, 1);
		set_method_param_type(unop_tp_is_f, 0, get_type_for_mode(mode_Is));
		set_method_res_type(unop_tp_is_f, 0, get_type_for_mode(mode_F));
	}

	if (! unop_tp_iu_d) {
		unop_tp_iu_d = new_type_method(1, 1);
		set_method_param_type(unop_tp_iu_d, 0, get_type_for_mode(mode_Iu));
		set_method_res_type(unop_tp_iu_d, 0, get_type_for_mode(mode_D));
	}

	if (! unop_tp_iu_f) {
		unop_tp_iu_f = new_type_method(1, 1);
		set_method_param_type(unop_tp_iu_f, 0, get_type_for_mode(mode_Iu));
		set_method_res_type(unop_tp_iu_f, 0, get_type_for_mode(mode_F));
	}

	if (! unop_tp_ls_d) {
		unop_tp_ls_d = new_type_method(1, 1);
		set_method_param_type(unop_tp_ls_d, 0, get_type_for_mode(mode_Ls));
		set_method_res_type(unop_tp_ls_d, 0, get_type_for_mode(mode_D));
	}

	if (! unop_tp_ls_f) {
		unop_tp_ls_f = new_type_method(1, 1);
		set_method_param_type(unop_tp_ls_f, 0, get_type_for_mode(mode_Ls));
		set_method_res_type(unop_tp_ls_f, 0, get_type_for_mode(mode_F));
	}

	if (! unop_tp_lu_d) {
		unop_tp_lu_d = new_type_method(1, 1);
		set_method_param_type(unop_tp_lu_d, 0, get_type_for_mode(mode_Lu));
		set_method_res_type(unop_tp_lu_d, 0, get_type_for_mode(mode_D));
	}

	if (! unop_tp_lu_f) {
		unop_tp_lu_f = new_type_method(1, 1);
		set_method_param_type(unop_tp_lu_f, 0, get_type_for_mode(mode_Lu));
		set_method_res_type(unop_tp_lu_f, 0, get_type_for_mode(mode_F));
	}
}

/**
 * Callback to lower only the Mux nodes we created.
 */
static int lower_mux_cb(ir_node *mux)
{
	return ir_nodeset_contains(&created_mux_nodes, mux);
}

void lower_floating_point(void)
{
	size_t i;
	size_t n_irgs = get_irp_n_irgs();

	FIRM_DBG_REGISTER(dbg, "firm.lower.softfloat");

	ir_prepare_softfloat_lowering();

	clear_irp_opcodes_generic_func();
	ir_register_softloat_lower_function(op_Add,   lower_Add);
	ir_register_softloat_lower_function(op_Cmp,   lower_Cmp);
	ir_register_softloat_lower_function(op_Conv,  lower_Conv);
	ir_register_softloat_lower_function(op_Div,   lower_Div);
	ir_register_softloat_lower_function(op_Minus, lower_Minus);
	ir_register_softloat_lower_function(op_Mul,   lower_Mul);
	ir_register_softloat_lower_function(op_Sub,   lower_Sub);

	for (i = 0; i < n_irgs; ++i) {
		ir_graph *irg = get_irp_irg(i);

		ir_nodeset_init(&created_mux_nodes);

		edges_assure(irg);

		irg_walk_graph(irg, NULL, lower_node, NULL);

		if (ir_nodeset_size(&created_mux_nodes) > 0)
			lower_mux(irg, lower_mux_cb);

		ir_nodeset_destroy(&created_mux_nodes);
	}

	clear_irp_opcodes_generic_func();
	ir_register_softloat_lower_function(op_Call,  lower_Call);
	ir_register_softloat_lower_function(op_Const, lower_Const);
	ir_register_softloat_lower_function(op_Div,   lower_Div_mode);
	ir_register_softloat_lower_function(op_Load,  lower_Load);

	for (i = 0; i < n_irgs; ++i) {
		ir_graph *irg          = get_irp_irg(i);
		ir_entity *ent         = get_irg_entity(irg);
		ir_type   *mtp         = get_entity_type(ent);
		ir_type   *lowered_mtp = lower_method_type(mtp);

		if (lowered_mtp != mtp)
			set_entity_type(ent, lowered_mtp);

		irg_walk_graph(irg, NULL, lower_mode, NULL);
	}
}