2 * UnSSA - translate the SSA back to normal form.
4 * For now it's done by replacing to set of copies:
5 * 1) For each phi-node, replace all their phisrc by copies to a common
7 * 2) Replace all the phi-nodes by copies of the temporaries to the phi-node target.
8 * This is node to preserve the semantic of the phi-node (they should all "execute"
9 * simultaneously on entry in the basic block in which they belong).
11 * This is similar to the "Sreedhar method I" except that the copies to the
12 * temporaries are not placed at the end of the predecessor basic blocks, but
13 * at the place where the phi-node operands are defined (the same place where the
14 * phisrc were present).
17 * While very simple this method create a lot more copies that really necessary.
18 * Ideally, "Sreedhar method III" should be used:
19 * "Translating Out of Static Single Assignment Form", V. C. Sreedhar, R. D.-C. Ju,
20 * D. M. Gillies and V. Santhanam. SAS'99, Vol. 1694 of Lecture Notes in Computer
21 * Science, Springer-Verlag, pp. 194-210, 1999.
23 * Copyright (C) 2005 Luc Van Oostenryck
27 #include "linearize.h"
31 static void replace_phi_node(struct instruction
*phi
)
35 tmp
= alloc_pseudo(NULL
);
36 tmp
->type
= phi
->target
->type
;
37 tmp
->def
= NULL
; // defined by all the phisrc
39 phi
->opcode
= OP_COPY
;
42 // FIXME: free phi->phi_list;
43 // FIXME: remove the %phi from bb->needs
44 // FIXME: add tmp to bb->needs
45 // FIXME: same but more tricky for bb->defines
48 static void rewrite_phi_bb(struct basic_block
*bb
)
50 struct instruction
*insn
;
52 // Replace all the phi-nodes by copies of a temporary
53 // (which represent the set of all the %phi that feed them).
54 // The target pseudo doesn't change.
55 FOR_EACH_PTR(bb
->insns
, insn
) {
58 if (insn
->opcode
!= OP_PHI
)
60 replace_phi_node(insn
);
61 } END_FOR_EACH_PTR(insn
);
64 static void rewrite_phisrc_bb(struct basic_block
*bb
)
66 struct instruction
*insn
;
68 // Replace all the phisrc by one or several copies to
69 // the temporaries associated to each phi-node it defines.
70 FOR_EACH_PTR_REVERSE(bb
->insns
, insn
) {
71 struct instruction
*phi
;
76 if (insn
->opcode
!= OP_PHISOURCE
)
80 FOR_EACH_PTR(insn
->phi_users
, phi
) {
81 pseudo_t tmp
= phi
->src
;
82 pseudo_t src
= insn
->phi_src
;
84 if (i
== 0) { // first phi: we overwrite the phisrc
85 insn
->opcode
= OP_COPY
;
89 struct instruction
*copy
= __alloc_instruction(0);
92 copy
->opcode
= OP_COPY
;
93 copy
->size
= insn
->size
;
94 copy
->pos
= insn
->pos
;
98 INSERT_CURRENT(copy
, insn
);
102 } END_FOR_EACH_PTR(phi
);
104 } END_FOR_EACH_PTR_REVERSE(insn
);
107 int unssa(struct entrypoint
*ep
)
109 struct basic_block
*bb
;
111 FOR_EACH_PTR(ep
->bbs
, bb
) {
113 } END_FOR_EACH_PTR(bb
);
115 FOR_EACH_PTR(ep
->bbs
, bb
) {
116 rewrite_phisrc_bb(bb
);
117 } END_FOR_EACH_PTR(bb
);