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 80 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 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
|
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2003-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%--------------------------------------------------------------------
%% File : hipe_icode_fp.erl
%% Author : Tobias Lindahl <tobiasl@it.uu.se>
%% Description : One pass analysis to find floating point values.
%% Mapping to FP variables and creation of FP EBBs.
%%
%% Created : 23 Apr 2003 by Tobias Lindahl <tobiasl@it.uu.se>
%%--------------------------------------------------------------------
-module(hipe_icode_fp).
-export([cfg/1]).
-include("hipe_icode.hrl").
-include("../flow/cfg.hrl").
-record(state, {edge_map = gb_trees:empty() :: gb_tree(),
fp_ebb_map = gb_trees:empty() :: gb_tree(),
cfg :: #cfg{}}).
%%--------------------------------------------------------------------
-spec cfg(#cfg{}) -> #cfg{}.
cfg(Cfg) ->
%%hipe_icode_cfg:pp(Cfg),
NewCfg = annotate_fclearerror(Cfg),
State = new_state(NewCfg),
NewState = place_fp_blocks(State),
%% hipe_icode_cfg:pp(state__cfg(NewState)),
NewState2 = finalize(NewState),
NewCfg1 = state__cfg(NewState2),
%% hipe_icode_cfg:pp(NewCfg1),
NewCfg2 = unannotate_fclearerror(NewCfg1),
NewCfg2.
%%--------------------------------------------------------------------
%% Annotate fclearerror with information of the fail label of the
%% corresponding fcheckerror.
%%--------------------------------------------------------------------
annotate_fclearerror(Cfg) ->
Labels = hipe_icode_cfg:reverse_postorder(Cfg),
annotate_fclearerror(Labels, Cfg).
annotate_fclearerror([Label|Left], Cfg) ->
BB = hipe_icode_cfg:bb(Cfg, Label),
Code = hipe_bb:code(BB),
NewCode = annotate_fclearerror1(Code, Label, Cfg, []),
NewBB = hipe_bb:code_update(BB, NewCode),
NewCfg = hipe_icode_cfg:bb_add(Cfg, Label, NewBB),
annotate_fclearerror(Left, NewCfg);
annotate_fclearerror([], Cfg) ->
Cfg.
annotate_fclearerror1([I|Left], Label, Cfg, Acc) ->
case I of
#icode_call{} ->
case hipe_icode:call_fun(I) of
fclearerror ->
Fail = lookahead_for_fcheckerror(Left, Label, Cfg),
NewI = hipe_icode:call_fun_update(I, {fclearerror, Fail}),
annotate_fclearerror1(Left, Label, Cfg, [NewI|Acc]);
_ ->
annotate_fclearerror1(Left, Label, Cfg, [I|Acc])
end;
_ ->
annotate_fclearerror1(Left, Label, Cfg, [I|Acc])
end;
annotate_fclearerror1([], _Label, _Cfg, Acc) ->
lists:reverse(Acc).
lookahead_for_fcheckerror([I|Left], Label, Cfg) ->
case I of
#icode_call{} ->
case hipe_icode:call_fun(I) of
fcheckerror ->
hipe_icode:call_fail_label(I);
_ ->
lookahead_for_fcheckerror(Left, Label, Cfg)
end;
_ ->
lookahead_for_fcheckerror(Left, Label, Cfg)
end;
lookahead_for_fcheckerror([], Label, Cfg) ->
case hipe_icode_cfg:succ(Cfg, Label) of
[] -> exit("Unterminated fp ebb");
SuccList ->
Succ = hd(SuccList),
Code = hipe_bb:code(hipe_icode_cfg:bb(Cfg, Label)),
lookahead_for_fcheckerror(Code, Succ, Cfg)
end.
unannotate_fclearerror(Cfg) ->
Labels = hipe_icode_cfg:reverse_postorder(Cfg),
unannotate_fclearerror(Labels, Cfg).
unannotate_fclearerror([Label|Left], Cfg) ->
BB = hipe_icode_cfg:bb(Cfg, Label),
Code = hipe_bb:code(BB),
NewCode = unannotate_fclearerror1(Code, []),
NewBB = hipe_bb:code_update(BB, NewCode),
NewCfg = hipe_icode_cfg:bb_add(Cfg, Label, NewBB),
unannotate_fclearerror(Left, NewCfg);
unannotate_fclearerror([], Cfg) ->
Cfg.
unannotate_fclearerror1([I|Left], Acc) ->
case I of
#icode_call{} ->
case hipe_icode:call_fun(I) of
{fclearerror, _Fail} ->
NewI = hipe_icode:call_fun_update(I, fclearerror),
unannotate_fclearerror1(Left, [NewI|Acc]);
_ ->
unannotate_fclearerror1(Left, [I|Acc])
end;
_ ->
unannotate_fclearerror1(Left, [I|Acc])
end;
unannotate_fclearerror1([], Acc) ->
lists:reverse(Acc).
%%--------------------------------------------------------------------
%% Make float EBBs
%%--------------------------------------------------------------------
place_fp_blocks(State) ->
WorkList = new_worklist(State),
transform_block(WorkList, State).
transform_block(WorkList, State) ->
case get_work(WorkList) of
none ->
State;
{Label, NewWorkList} ->
%%io:format("Handling ~w \n", [Label]),
BB = state__bb(State, Label),
Code1 = hipe_bb:butlast(BB),
Last = hipe_bb:last(BB),
NofPreds = length(state__pred(State, Label)),
Map = state__map(State, Label),
FilteredMap = filter_map(Map, NofPreds),
{Prelude, NewFilteredMap} = do_prelude(FilteredMap),
%% Take care to have a map without any new bindings from the
%% last instruction if it can fail.
{FailMap, NewCode1} = transform_instrs(Code1, Map, NewFilteredMap, []),
{NewMap, NewCode2} = transform_instrs([Last], Map, FailMap, []),
SuccSet0 = ordsets:from_list(hipe_icode:successors(Last)),
FailSet = ordsets:from_list(hipe_icode:fails_to(Last)),
SuccSet = ordsets:subtract(SuccSet0, FailSet),
NewCode = NewCode1 ++ NewCode2,
NewBB = hipe_bb:code_update(BB, Prelude++NewCode),
NewState = state__bb_add(State, Label, NewBB),
case update_maps(NewState, Label, SuccSet, NewMap, FailSet, FailMap) of
fixpoint ->
transform_block(NewWorkList, NewState);
{NewState1, AddBlocks} ->
NewWorkList1 = add_work(NewWorkList, AddBlocks),
transform_block(NewWorkList1, NewState1)
end
end.
update_maps(State, Label, SuccSet, SuccMap, FailSet, FailMap) ->
{NewState, Add1} = update_maps(State, Label, SuccSet, SuccMap, []),
case update_maps(NewState, Label, FailSet, FailMap, Add1) of
{_NewState1, []} -> fixpoint;
{_NewState1, _Add} = Ret -> Ret
end.
update_maps(State, From, [To|Left], Map, Acc) ->
case state__map_update(State, From, To, Map) of
fixpoint ->
update_maps(State, From, Left, Map, Acc);
NewState ->
update_maps(NewState, From, Left, Map, [To|Acc])
end;
update_maps(State, _From, [], _Map, Acc) ->
{State, Acc}.
transform_instrs([I|Left], PhiMap, Map, Acc) ->
Defines = hipe_icode:defines(I),
NewMap = delete_all(Defines, Map),
NewPhiMap = delete_all(Defines, PhiMap),
case I of
#icode_phi{} ->
Uses = hipe_icode:uses(I),
case [X || X <- Uses, lookup(X, PhiMap) =/= none] of
[] ->
%% No ordinary variables from the argument have been untagged.
transform_instrs(Left, NewPhiMap, NewMap, [I|Acc]);
Uses ->
%% All arguments are untagged. Let's untag the destination.
Dst = hipe_icode:phi_dst(I),
NewDst = hipe_icode:mk_new_fvar(),
NewMap1 = gb_trees:enter(Dst, NewDst, NewMap),
NewI = subst_phi_uncond(I, NewDst, PhiMap),
transform_instrs(Left, NewPhiMap, NewMap1, [NewI|Acc]);
_ ->
%% Some arguments are untagged. Keep the destination.
Dst = hipe_icode:phi_dst(I),
NewI = subst_phi(I, Dst, PhiMap),
transform_instrs(Left, NewPhiMap, NewMap, [NewI|Acc])
end;
#icode_call{} ->
case hipe_icode:call_fun(I) of
X when X =:= unsafe_untag_float orelse X =:= conv_to_float ->
[Dst] = hipe_icode:defines(I),
case hipe_icode:uses(I) of
[] -> %% Constant
transform_instrs(Left, NewPhiMap, NewMap, [I|Acc]);
[Src] ->
case lookup(Src, Map) of
none ->
NewMap1 = gb_trees:enter(Src, {assigned, Dst}, NewMap),
transform_instrs(Left, NewPhiMap, NewMap1, [I|Acc]);
Dst ->
%% This is the instruction that untagged the variable.
%% Use old maps.
transform_instrs(Left, NewPhiMap, Map, [I|Acc]);
FVar ->
%% The variable was already untagged.
%% This instruction can be changed to a move.
NewI = hipe_icode:mk_move(Dst, FVar),
case hipe_icode:call_continuation(I) of
[] ->
transform_instrs(Left,NewPhiMap,NewMap,[NewI|Acc]);
ContLbl ->
Goto = hipe_icode:mk_goto(ContLbl),
transform_instrs(Left, NewPhiMap, NewMap,
[Goto, NewI|Acc])
end
end
end;
unsafe_tag_float ->
[Dst] = hipe_icode:defines(I),
[Src] = hipe_icode:uses(I),
NewMap1 = gb_trees:enter(Dst, {assigned, Src}, NewMap),
transform_instrs(Left, NewPhiMap, NewMap1,[I|Acc]);
_ ->
{NewMap1, NewAcc} = check_for_fop_candidates(I, NewMap, Acc),
transform_instrs(Left, NewPhiMap, NewMap1, NewAcc)
end;
_ ->
NewIns = handle_untagged_arguments(I, NewMap),
transform_instrs(Left, NewPhiMap, NewMap, NewIns ++ Acc)
end;
transform_instrs([], _PhiMap, Map, Acc) ->
{Map, lists:reverse(Acc)}.
check_for_fop_candidates(I, Map, Acc) ->
case is_fop_cand(I) of
false ->
NewIs = handle_untagged_arguments(I, Map),
{Map, NewIs ++ Acc};
true ->
Fail = hipe_icode:call_fail_label(I),
Cont = hipe_icode:call_continuation(I),
Op = fun_to_fop(hipe_icode:call_fun(I)),
case Fail of
[] ->
Args = hipe_icode:args(I),
ConstArgs = [X || X <- Args, hipe_icode:is_const(X)],
try lists:foreach(fun(X) -> float(hipe_icode:const_value(X)) end,
ConstArgs) of
ok ->
%%io:format("Changing ~w to ~w\n", [hipe_icode:call_fun(I), Op]),
Uses = hipe_icode:uses(I),
Defines = hipe_icode:defines(I),
Convs = [X||X <- remove_duplicates(Uses), lookup(X,Map) =:= none],
NewMap0 = add_new_bindings_assigned(Convs, Map),
NewMap = add_new_bindings_unassigned(Defines, NewMap0),
ConvIns = get_conv_instrs(Convs, NewMap),
NewI = hipe_icode:mk_primop(lookup_list(Defines, NewMap), Op,
lookup_list_keep_consts(Args,NewMap),
Cont, Fail),
NewI2 = conv_consts(ConstArgs, NewI),
{NewMap, [NewI2|ConvIns]++Acc}
catch
error:badarg ->
%% This instruction will fail at runtime. The warning
%% should already have happened in hipe_icode_type.
NewIs = handle_untagged_arguments(I, Map),
{Map, NewIs ++ Acc}
end;
_ -> %% Bailing out! Can't handle instructions in catches (yet).
NewIs = handle_untagged_arguments(I, Map),
{Map, NewIs ++ Acc}
end
end.
%% If this is an instruction that needs to operate on tagged values,
%% which currently are untagged, we must tag the values and perhaps
%% end the fp ebb.
handle_untagged_arguments(I, Map) ->
case [X || X <- hipe_icode:uses(I), must_be_tagged(X, Map)] of
[] ->
[I];
Tag ->
TagIntrs =
[hipe_icode:mk_primop([Dst], unsafe_tag_float,
[gb_trees:get(Dst, Map)]) || Dst <- Tag],
[I|TagIntrs]
end.
%% Add phi nodes for untagged fp values.
do_prelude(Map) ->
case gb_trees:lookup(phi, Map) of
none ->
{[], Map};
{value, List} ->
%%io:format("Adding phi: ~w\n", [List]),
Fun = fun ({FVar, Bindings}, Acc) ->
[hipe_icode:mk_phi(FVar, Bindings)|Acc]
end,
{lists:foldl(Fun, [], List), gb_trees:delete(phi, Map)}
end.
split_code(Code) ->
split_code(Code, []).
split_code([I], Acc) ->
{lists:reverse(Acc), I};
split_code([I|Left], Acc) ->
split_code(Left, [I|Acc]).
%% When all code is mapped to fp instructions we must make sure that
%% the fp ebb information going into each block is the same as the
%% information coming out of each predecessor. Otherwise, we must add
%% a block in between.
finalize(State) ->
Worklist = new_worklist(State),
NewState = place_error_handling(Worklist, State),
Edges = needs_fcheckerror(NewState),
finalize(Edges, NewState).
finalize([{From, To}|Left], State) ->
NewState = add_fp_ebb_fixup(From, To, State),
finalize(Left, NewState);
finalize([], State) ->
State.
needs_fcheckerror(State) ->
Cfg = state__cfg(State),
Labels = hipe_icode_cfg:labels(Cfg),
needs_fcheckerror(Labels, State, []).
needs_fcheckerror([Label|Left], State, Acc) ->
case state__get_in_block_in(State, Label) of
{true, _} ->
needs_fcheckerror(Left, State, Acc);
false ->
Pred = state__pred(State, Label),
case [X || X <- Pred, state__get_in_block_out(State, X) =/= false] of
[] ->
needs_fcheckerror(Left, State, Acc);
NeedsFcheck ->
case length(Pred) =:= length(NeedsFcheck) of
true ->
%% All edges need fcheckerror. Add this to the
%% beginning of the block instead.
needs_fcheckerror(Left, State, [{none, Label}|Acc]);
false ->
Edges = [{X, Label} || X <- NeedsFcheck],
needs_fcheckerror(Left, State, Edges ++ Acc)
end
end
end;
needs_fcheckerror([], _State, Acc) ->
Acc.
add_fp_ebb_fixup('none', To, State) ->
%% Add the fcheckerror to the start of the block.
BB = state__bb(State, To),
Code = hipe_bb:code(BB),
Phis = lists:takewhile(fun(X) -> hipe_icode:is_phi(X) end, Code),
TailCode = lists:dropwhile(fun(X) -> hipe_icode:is_phi(X) end, Code),
FC = hipe_icode:mk_primop([], fcheckerror, []),
NewCode = Phis ++ [FC|TailCode],
state__bb_add(State, To, hipe_bb:code_update(BB, NewCode));
add_fp_ebb_fixup(From, To, State) ->
FCCode = [hipe_icode:mk_primop([], fcheckerror, [], To, [])],
FCBB = hipe_bb:mk_bb(FCCode),
FCLabel = hipe_icode:label_name(hipe_icode:mk_new_label()),
NewState = state__bb_add(State, FCLabel, FCBB),
NewState1 = state__redirect(NewState, From, To, FCLabel),
ToBB = state__bb(NewState, To),
ToCode = hipe_bb:code(ToBB),
NewToCode = redirect_phis(ToCode, From, FCLabel),
NewToBB = hipe_bb:code_update(ToBB, NewToCode),
state__bb_add(NewState1, To, NewToBB).
redirect_phis(Code, OldFrom, NewFrom) ->
redirect_phis(Code, OldFrom, NewFrom, []).
redirect_phis([I|Is] = Code, OldFrom, NewFrom, Acc) ->
case I of
#icode_phi{} ->
NewI = hipe_icode:phi_redirect_pred(I, OldFrom, NewFrom),
redirect_phis(Is, OldFrom, NewFrom, [NewI|Acc]);
_ ->
lists:reverse(Acc) ++ Code
end;
redirect_phis([], _OldFrom, _NewFrom, Acc) ->
lists:reverse(Acc).
subst_phi(I, Dst, Map) ->
ArgList = subst_phi_uses0(hipe_icode:phi_arglist(I), Map, []),
hipe_icode:mk_phi(Dst, ArgList).
subst_phi_uses0([{Pred, Var}|Left], Map, Acc) ->
case gb_trees:lookup(Var, Map) of
{value, List} ->
case lists:keyfind(Pred, 1, List) of
{Pred, {assigned, _NewVar}} ->
%% The variable is untagged, but it has been assigned. Keep it!
subst_phi_uses0(Left, Map, [{Pred, Var} | Acc]);
{Pred, _NewVar} = PredNV ->
%% The variable is untagged and it has never been assigned as tagged.
subst_phi_uses0(Left, Map, [PredNV | Acc]);
false ->
%% The variable is not untagged.
subst_phi_uses0(Left, Map, [{Pred, Var} | Acc])
end;
none ->
%% The variable is not untagged.
subst_phi_uses0(Left, Map, [{Pred, Var} | Acc])
end;
subst_phi_uses0([], _Map, Acc) ->
Acc.
subst_phi_uncond(I, Dst, Map) ->
ArgList = subst_phi_uses_uncond0(hipe_icode:phi_arglist(I), Map, []),
hipe_icode:mk_phi(Dst, ArgList).
subst_phi_uses_uncond0([{Pred, Var}|Left], Map, Acc) ->
case gb_trees:lookup(Var, Map) of
{value, List} ->
case lists:keyfind(Pred, 1, List) of
{Pred, {assigned, NewVar}} ->
%% The variable is untagged!
subst_phi_uses_uncond0(Left, Map, [{Pred, NewVar} | Acc]);
{Pred, _NewVar} = PredNV ->
%% The variable is untagged!
subst_phi_uses_uncond0(Left, Map, [PredNV | Acc]);
false ->
%% The variable is not untagged.
subst_phi_uses_uncond0(Left, Map, [{Pred, Var} | Acc])
end;
none ->
%% The variable is not untagged.
subst_phi_uses_uncond0(Left, Map, [{Pred, Var} | Acc])
end;
subst_phi_uses_uncond0([], _Map, Acc) ->
Acc.
place_error_handling(WorkList, State) ->
case get_work(WorkList) of
none ->
State;
{Label, NewWorkList} ->
BB = state__bb(State, Label),
Code = hipe_bb:code(BB),
case state__join_in_block(State, Label) of
fixpoint ->
place_error_handling(NewWorkList, State);
{NewState, NewInBlock} ->
{NewCode1, InBlockOut} = place_error(Code, NewInBlock, []),
Succ = state__succ(NewState, Label),
NewCode2 = handle_unchecked_end(Succ, NewCode1, InBlockOut),
NewBB = hipe_bb:code_update(BB, NewCode2),
NewState1 = state__bb_add(NewState, Label, NewBB),
NewState2 = state__in_block_out_update(NewState1, Label, InBlockOut),
NewWorkList1 = add_work(NewWorkList, Succ),
place_error_handling(NewWorkList1, NewState2)
end
end.
place_error([I|Left], InBlock, Acc) ->
case I of
#icode_call{} ->
case hipe_icode:call_fun(I) of
X when X =:= fp_add; X =:= fp_sub;
X =:= fp_mul; X =:= fp_div; X =:= fnegate ->
case InBlock of
false ->
Clear = hipe_icode:mk_primop([], {fclearerror, []}, []),
place_error(Left, {true, []}, [I, Clear|Acc]);
{true, _} ->
place_error(Left, InBlock, [I|Acc])
end;
unsafe_tag_float ->
case InBlock of
{true, Fail} ->
Check = hipe_icode:mk_primop([], fcheckerror, [], [], Fail),
place_error(Left, false, [I, Check|Acc]);
false ->
place_error(Left, InBlock, [I|Acc])
end;
{fclearerror, Fail} ->
case InBlock of
{true, Fail} ->
%% We can remove this fclearerror!
case hipe_icode:call_continuation(I) of
[] ->
place_error(Left, InBlock, Acc);
Cont ->
place_error(Left, InBlock, [hipe_icode:mk_goto(Cont)|Acc])
end;
{true, _OtherFail} ->
%% TODO: This can be handled but it requires breaking up
%% the BB in two. Currently this should not happen.
exit("Starting fp ebb with different fail label");
false ->
place_error(Left, {true, Fail}, [I|Acc])
end;
fcheckerror ->
case {true, hipe_icode:call_fail_label(I)} of
InBlock ->
%% No problem
place_error(Left, false, [I|Acc]);
NewInblock ->
exit({"Fcheckerror has the wrong fail label",
InBlock, NewInblock})
end;
X when X =:= conv_to_float; X =:= unsafe_untag_float ->
place_error(Left, InBlock, [I|Acc]);
_Other ->
case hipe_icode_primops:fails(hipe_icode:call_fun(I)) of
false ->
place_error(Left, InBlock, [I|Acc]);
true ->
case InBlock of
{true, Fail} ->
Check = hipe_icode:mk_primop([], fcheckerror, [], [], Fail),
place_error(Left, false, [I, Check|Acc]);
false ->
place_error(Left, InBlock, [I|Acc])
end
end
end;
#icode_fail{} ->
place_error_1(I, Left, InBlock, Acc);
#icode_return{} ->
place_error_1(I, Left, InBlock, Acc);
#icode_enter{} ->
place_error_1(I, Left, InBlock, Acc);
Other ->
case instr_allowed_in_fp_ebb(Other) of
true ->
place_error(Left, InBlock, [I|Acc]);
false ->
case InBlock of
{true, []} ->
Check = hipe_icode:mk_primop([], fcheckerror, []),
place_error(Left, false, [I, Check|Acc]);
{true, _} ->
exit({"Illegal instruction in caught fp ebb", I});
false ->
place_error(Left, InBlock, [I|Acc])
end
end
end;
place_error([], InBlock, Acc) ->
{lists:reverse(Acc), InBlock}.
place_error_1(I, Left, InBlock, Acc) ->
case InBlock of
{true, []} ->
Check = hipe_icode:mk_primop([], fcheckerror, []),
place_error(Left, false, [I, Check|Acc]);
{true, _} ->
exit({"End of control flow in caught fp ebb", I});
false ->
place_error(Left, InBlock, [I|Acc])
end.
%% If the block has no successors and we still are in a fp ebb we must
%% end it to make sure we don't have any unchecked fp exceptions.
handle_unchecked_end(Succ, Code, InBlock) ->
case Succ of
[] ->
case InBlock of
{true, []} ->
{TopCode, Last} = split_code(Code),
NewI = hipe_icode:mk_primop([], fcheckerror, []),
TopCode ++ [NewI, Last];
false ->
Code
end;
_ ->
Code
end.
instr_allowed_in_fp_ebb(Instr) ->
case Instr of
#icode_comment{} -> true;
#icode_goto{} -> true;
#icode_if{} -> true;
#icode_move{} -> true;
#icode_phi{} -> true;
#icode_begin_handler{} -> true;
#icode_switch_tuple_arity{} -> true;
#icode_switch_val{} -> true;
#icode_type{} -> true;
_ -> false
end.
%%=============================================================
%% Help functions
%%=============================================================
%% ------------------------------------------------------------
%% Handling the gb_tree
delete_all([Key|Left], Tree) ->
delete_all(Left, gb_trees:delete_any(Key, Tree));
delete_all([], Tree) ->
Tree.
lookup_list(List, Info) ->
lookup_list(List, fun lookup/2, Info, []).
lookup_list([H|T], Fun, Info, Acc) ->
lookup_list(T, Fun, Info, [Fun(H, Info)|Acc]);
lookup_list([], _, _, Acc) ->
lists:reverse(Acc).
lookup(Key, Tree) ->
case hipe_icode:is_const(Key) of
%% This can be true if the same constant has been
%% untagged more than once
true -> none;
false ->
case gb_trees:lookup(Key, Tree) of
none -> none;
{value, {assigned, Val}} -> Val;
{value, Val} -> Val
end
end.
lookup_list_keep_consts(List, Info) ->
lookup_list(List, fun lookup_keep_consts/2, Info, []).
lookup_keep_consts(Key, Tree) ->
case hipe_icode:is_const(Key) of
true -> Key;
false ->
case gb_trees:lookup(Key, Tree) of
none -> none;
{value, {assigned, Val}} -> Val;
{value, Val} -> Val
end
end.
get_type(Var) ->
case hipe_icode:is_const(Var) of
true -> erl_types:t_from_term(hipe_icode:const_value(Var));
false ->
case hipe_icode:is_annotated_variable(Var) of
true ->
{type_anno, Type, _} = hipe_icode:variable_annotation(Var),
Type
%%% false -> erl_types:t_any()
end
end.
%% ------------------------------------------------------------
%% Handling the map from variables to fp-variables
join_maps(Edges, EdgeMap) ->
join_maps(Edges, EdgeMap, gb_trees:empty()).
join_maps([Edge = {Pred, _}|Left], EdgeMap, Map) ->
case gb_trees:lookup(Edge, EdgeMap) of
none ->
%% All predecessors have not been handled. Use empty map.
gb_trees:empty();
{value, OldMap} ->
NewMap = join_maps0(gb_trees:to_list(OldMap), Pred, Map),
join_maps(Left, EdgeMap, NewMap)
end;
join_maps([], _, Map) ->
Map.
join_maps0([{phi, _}|Tail], Pred, Map) ->
join_maps0(Tail, Pred, Map);
join_maps0([{Var, FVar}|Tail], Pred, Map) ->
case gb_trees:lookup(Var, Map) of
none ->
join_maps0(Tail, Pred, gb_trees:enter(Var, [{Pred, FVar}], Map));
{value, List} ->
case lists:keyfind(Pred, 1, List) of
false ->
join_maps0(Tail, Pred, gb_trees:update(Var, [{Pred, FVar}|List], Map));
{Pred, FVar} ->
%% No problem.
join_maps0(Tail, Pred, Map);
_ ->
exit('New binding to same variable')
end
end;
join_maps0([], _, Map) ->
Map.
filter_map(Map, NofPreds) ->
filter_map(gb_trees:to_list(Map), NofPreds, Map).
filter_map([{Var, Bindings}|Left], NofPreds, Map) ->
case length(Bindings) =:= NofPreds of
true ->
case all_args_equal(Bindings) of
true ->
{_, FVar} = hd(Bindings),
filter_map(Left, NofPreds, gb_trees:update(Var, FVar, Map));
false ->
PhiDst = hipe_icode:mk_new_fvar(),
PhiArgs = strip_of_assigned(Bindings),
NewMap =
case gb_trees:lookup(phi, Map) of
none ->
gb_trees:insert(phi, [{PhiDst, PhiArgs}], Map);
{value, Val} ->
gb_trees:update(phi, [{PhiDst, PhiArgs}|Val], Map)
end,
NewBinding =
case bindings_are_assigned(Bindings) of
true -> {assigned, PhiDst};
false -> PhiDst
end,
filter_map(Left, NofPreds, gb_trees:update(Var, NewBinding, NewMap))
end;
false ->
filter_map(Left, NofPreds, gb_trees:delete(Var, Map))
end;
filter_map([], _NofPreds, Map) ->
Map.
bindings_are_assigned([{_, {assigned, _}}|Left]) ->
assert_assigned(Left),
true;
bindings_are_assigned(Bindings) ->
assert_not_assigned(Bindings),
false.
assert_assigned([{_, {assigned, _}}|Left]) ->
assert_assigned(Left);
assert_assigned([]) ->
ok.
assert_not_assigned([{_, FVar}|Left]) ->
true = hipe_icode:is_fvar(FVar),
assert_not_assigned(Left);
assert_not_assigned([]) ->
ok.
%% all_args_equal returns true if the mapping for a variable is the
%% same from all predecessors, i.e., we do not need a phi-node.
all_args_equal([{_, FVar}|Left]) ->
all_args_equal(Left, FVar).
all_args_equal([{_, FVar1}|Left], FVar1) ->
all_args_equal(Left, FVar1);
all_args_equal([], _) ->
true;
all_args_equal(_, _) ->
false.
%% We differentiate between values that have been assigned as
%% tagged variables and those that got a 'virtual' binding.
add_new_bindings_unassigned([Var|Left], Map) ->
FVar = hipe_icode:mk_new_fvar(),
add_new_bindings_unassigned(Left, gb_trees:insert(Var, FVar, Map));
add_new_bindings_unassigned([], Map) ->
Map.
add_new_bindings_assigned([Var|Left], Map) ->
case lookup(Var, Map) of
none ->
FVar = hipe_icode:mk_new_fvar(),
NewMap = gb_trees:insert(Var, {assigned, FVar}, Map),
add_new_bindings_assigned(Left, NewMap);
_ ->
add_new_bindings_assigned(Left, Map)
end;
add_new_bindings_assigned([], Map) ->
Map.
strip_of_assigned(List) ->
strip_of_assigned(List, []).
strip_of_assigned([{Pred, {assigned, Val}}|Left], Acc) ->
strip_of_assigned(Left, [{Pred, Val}|Acc]);
strip_of_assigned([Tuple|Left], Acc) ->
strip_of_assigned(Left, [Tuple|Acc]);
strip_of_assigned([], Acc) ->
Acc.
%% ------------------------------------------------------------
%% Help functions for the transformation from ordinary instruction to
%% fp-instruction
is_fop_cand(I) ->
case hipe_icode:call_fun(I) of
'/' -> true;
Fun ->
case fun_to_fop(Fun) of
false -> false;
_ -> any_is_float(hipe_icode:args(I))
end
end.
any_is_float(Vars) ->
lists:any(fun (V) -> erl_types:t_is_float(get_type(V)) end, Vars).
remove_duplicates(List) ->
remove_duplicates(List, []).
remove_duplicates([X|Left], Acc) ->
case lists:member(X, Acc) of
true ->
remove_duplicates(Left, Acc);
false ->
remove_duplicates(Left, [X|Acc])
end;
remove_duplicates([], Acc) ->
Acc.
fun_to_fop(Fun) ->
case Fun of
'+' -> fp_add;
'-' -> fp_sub;
'*' -> fp_mul;
'/' -> fp_div;
_ -> false
end.
%% If there is a tagged version of this variable available we don't
%% have to tag the untagged version.
must_be_tagged(Var, Map) ->
case gb_trees:lookup(Var, Map) of
none -> false;
{value, {assigned, _}} -> false;
{value, Val} -> hipe_icode:is_fvar(Val)
end.
%% Converting to floating point variables
get_conv_instrs(Vars, Map) ->
get_conv_instrs(Vars, Map, []).
get_conv_instrs([Var|Left], Map, Acc) ->
{_, Dst} = gb_trees:get(Var, Map),
NewI =
case erl_types:t_is_float(get_type(Var)) of
true ->
[hipe_icode:mk_primop([Dst], unsafe_untag_float, [Var])];
false ->
[hipe_icode:mk_primop([Dst], conv_to_float, [Var])]
end,
get_conv_instrs(Left, Map, NewI++Acc);
get_conv_instrs([], _, Acc) ->
Acc.
conv_consts(ConstArgs, I) ->
conv_consts(ConstArgs, I, []).
conv_consts([Const|Left], I, Subst) ->
NewConst = hipe_icode:mk_const(float(hipe_icode:const_value(Const))),
conv_consts(Left, I, [{Const, NewConst}|Subst]);
conv_consts([], I, Subst) ->
hipe_icode:subst_uses(Subst, I).
%% _________________________________________________________________
%%
%% Handling the state
%%
new_state(Cfg) ->
#state{cfg = Cfg}.
state__cfg(#state{cfg = Cfg}) ->
Cfg.
state__succ(#state{cfg = Cfg}, Label) ->
hipe_icode_cfg:succ(Cfg, Label).
state__pred(#state{cfg = Cfg}, Label) ->
hipe_icode_cfg:pred(Cfg, Label).
state__redirect(S = #state{cfg = Cfg}, From, ToOld, ToNew) ->
NewCfg = hipe_icode_cfg:redirect(Cfg, From, ToOld, ToNew),
S#state{cfg=NewCfg}.
state__bb(#state{cfg = Cfg}, Label) ->
hipe_icode_cfg:bb(Cfg, Label).
state__bb_add(S = #state{cfg = Cfg}, Label, BB) ->
NewCfg = hipe_icode_cfg:bb_add(Cfg, Label, BB),
S#state{cfg = NewCfg}.
state__map(S = #state{edge_map = EM}, To) ->
join_maps([{From, To} || From <- state__pred(S, To)], EM).
state__map_update(S = #state{edge_map = EM}, From, To, Map) ->
FromTo = {From, To},
MapChanged =
case gb_trees:lookup(FromTo, EM) of
{value, Map1} -> not match(Map1, Map);
none -> true
end,
case MapChanged of
true ->
NewEM = gb_trees:enter(FromTo, Map, EM),
S#state{edge_map = NewEM};
false ->
fixpoint
end.
state__join_in_block(S = #state{fp_ebb_map = Map}, Label) ->
Pred = state__pred(S, Label),
Edges = [{X, Label} || X <- Pred],
NewInBlock = join_in_block([gb_trees:lookup(X, Map) || X <- Edges]),
InBlockLabel = {inblock_in, Label},
case gb_trees:lookup(InBlockLabel, Map) of
none ->
NewMap = gb_trees:insert(InBlockLabel, NewInBlock, Map),
{S#state{fp_ebb_map = NewMap}, NewInBlock};
{value, NewInBlock} ->
fixpoint;
_Other ->
NewMap = gb_trees:update(InBlockLabel, NewInBlock, Map),
{S#state{fp_ebb_map = NewMap}, NewInBlock}
end.
state__in_block_out_update(S = #state{fp_ebb_map = Map}, Label, NewInBlock) ->
Succ = state__succ(S, Label),
Edges = [{Label, X} || X <- Succ],
NewMap = update_edges(Edges, NewInBlock, Map),
NewMap1 = gb_trees:enter({inblock_out, Label}, NewInBlock, NewMap),
S#state{fp_ebb_map = NewMap1}.
update_edges([Edge|Left], NewInBlock, Map) ->
NewMap = gb_trees:enter(Edge, NewInBlock, Map),
update_edges(Left, NewInBlock, NewMap);
update_edges([], _NewInBlock, NewMap) ->
NewMap.
join_in_block([]) ->
false;
join_in_block([none|_]) ->
false;
join_in_block([{value, InBlock}|Left]) ->
join_in_block(Left, InBlock).
join_in_block([none|_], _Current) ->
false;
join_in_block([{value, InBlock}|Left], Current) ->
if Current =:= InBlock -> join_in_block(Left, Current);
Current =:= false -> false;
InBlock =:= false -> false;
true -> exit("Basic block is in two different fp ebb:s")
end;
join_in_block([], Current) ->
Current.
state__get_in_block_in(#state{fp_ebb_map = Map}, Label) ->
gb_trees:get({inblock_in, Label}, Map).
state__get_in_block_out(#state{fp_ebb_map = Map}, Label) ->
gb_trees:get({inblock_out, Label}, Map).
new_worklist(#state{cfg = Cfg}) ->
Start = hipe_icode_cfg:start_label(Cfg),
{[Start], [], gb_sets:insert(Start, gb_sets:empty())}.
get_work({[Label|Left], List, Set}) ->
{Label, {Left, List, gb_sets:delete(Label, Set)}};
get_work({[], [], _Set}) ->
none;
get_work({[], List, Set}) ->
get_work({lists:reverse(List), [], Set}).
add_work({List1, List2, Set} = Work, [Label|Left]) ->
case gb_sets:is_member(Label, Set) of
true ->
add_work(Work, Left);
false ->
%% io:format("Added work: ~w\n", [Label]),
NewSet = gb_sets:insert(Label, Set),
add_work({List1, [Label|List2], NewSet}, Left)
end;
add_work(WorkList, []) ->
WorkList.
match(Tree1, Tree2) ->
match_1(gb_trees:to_list(Tree1), Tree2) andalso
match_1(gb_trees:to_list(Tree2), Tree1).
match_1([{Key, Val}|Left], Tree2) ->
case gb_trees:lookup(Key, Tree2) of
{value, Val} ->
match_1(Left, Tree2);
_ -> false
end;
match_1([], _) ->
true.
|