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.
14 * This is particulary easy since these copies are essentialy already present
15 * as the corresponding OP_PHISOURCE.
17 * While very simple this method create a lot more copies that really necessary.
18 * We eliminate some of these copies but most probably most of them are still
20 * Ideally, "Sreedhar method III" should be used:
21 * "Translating Out of Static Single Assignment Form", V. C. Sreedhar, R. D.-C. Ju,
22 * D. M. Gillies and V. Santhanam. SAS'99, Vol. 1694 of Lecture Notes in Computer
23 * Science, Springer-Verlag, pp. 194-210, 1999.
24 * But for this we need precise liveness, on each %phi and not only on OP_PHI's
27 * Copyright (C) 2005 Luc Van Oostenryck
31 #include "linearize.h"
37 static int simplify_phi_node(struct instruction
*phi
, pseudo_t tmp
)
39 pseudo_t target
= phi
->target
;
40 struct pseudo_user
*pu
;
43 // verify if this phi can be simplified
44 FOR_EACH_PTR(phi
->phi_list
, src
) {
45 struct instruction
*def
= src
->def
;
49 if (def
->bb
== phi
->bb
)
51 } END_FOR_EACH_PTR(src
);
53 // no need to make a copy of this one
54 // -> replace the target pseudo by the tmp
55 FOR_EACH_PTR(target
->users
, pu
) {
56 use_pseudo(pu
->insn
, tmp
, pu
->userp
);
57 } END_FOR_EACH_PTR(pu
);
63 static void replace_phi_node(struct instruction
*phi
)
68 tmp
= alloc_pseudo(NULL
);
69 tmp
->type
= phi
->target
->type
;
70 tmp
->ident
= phi
->target
->ident
;
71 tmp
->def
= NULL
; // defined by all the phisrc
73 // can we avoid to make of copy?
74 simplify_phi_node(phi
, tmp
);
76 // rewrite all it's phi_src to copy to a new tmp
77 FOR_EACH_PTR(phi
->phi_list
, p
) {
78 struct instruction
*def
= p
->def
;
84 assert(def
->opcode
== OP_PHISOURCE
);
86 def
->opcode
= OP_COPY
;
89 // can we eliminate the copy?
91 if (src
->type
!= PSEUDO_REG
)
93 switch (nbr_users(src
)) {
94 struct instruction
*insn
;
101 kill_instruction(def
);
104 } END_FOR_EACH_PTR(p
);
109 // rewrite the phi node:
113 phi
->opcode
= OP_COPY
;
114 use_pseudo(phi
, tmp
, &phi
->src
);
117 static void rewrite_phi_bb(struct basic_block
*bb
)
119 struct instruction
*insn
;
121 // Replace all the phi-nodes by copies of a temporary
122 // (which represent the set of all the %phi that feed them).
123 // The target pseudo doesn't change.
124 FOR_EACH_PTR(bb
->insns
, insn
) {
127 if (insn
->opcode
!= OP_PHI
)
129 replace_phi_node(insn
);
130 } END_FOR_EACH_PTR(insn
);
133 int unssa(struct entrypoint
*ep
)
135 struct basic_block
*bb
;
137 FOR_EACH_PTR(ep
->bbs
, bb
) {
139 } END_FOR_EACH_PTR(bb
);