00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #define __STDC_LIMIT_MACROS
00039 #include <stdint.h>
00040
00041 #include "ipo_defs.h"
00042 #include "ipo_inline.h"
00043 #include "ipo_alias.h"
00044 #include "ipo_alias_class.h"
00045 #include "ipo_parent.h"
00046
00047 #include <deque>
00048 #include <queue>
00049
00050 struct order_by_callsite_id
00051 {
00052 bool operator() (IPA_EDGE * first, IPA_EDGE * second)
00053 {
00054 return first->Callsite_Id() > second->Callsite_Id();
00055 }
00056 };
00057
00058 static BOOL
00059 Is_Return_Store_Stmt (WN * wn)
00060 {
00061 if (wn && WN_operator (wn) == OPR_STID)
00062 {
00063 WN *val = WN_kid (wn, 0);
00064 if (WN_operator (val) == OPR_LDID)
00065 {
00066 ST *st = WN_st (val);
00067 if (ST_sym_class (st) == CLASS_PREG
00068 && (st == Return_Val_Preg))
00069 return TRUE;
00070 }
00071 }
00072 return FALSE;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 static void
00082 Convert_Icall (WN * wn, IPA_EDGE * edge)
00083 {
00084 IPA_NODE * caller = IPA_Call_Graph->Caller (edge);
00085 IPA_NODE * callee = IPA_Call_Graph->Callee (edge);
00086 ST * st_callee = callee->Func_ST();
00087 TY_IDX ty_callee = ST_pu_type (st_callee);
00088
00089
00090 WN* tmpkid0 = WN_CreateLda (Use_32_Bit_Pointers ? OPC_U4LDA : OPC_U8LDA,
00091 0, Make_Pointer_Type(ty_callee), st_callee);
00092
00093 WN* tmpkid1 = WN_COPY_Tree_With_Map(WN_kid (wn, WN_kid_count(wn) - 1));
00094
00095
00096 WN* test = WN_Create (Use_32_Bit_Pointers ? OPC_U4U4EQ : OPC_U8U8EQ, 2);
00097
00098 WN_kid0(test) = tmpkid0;
00099 WN_kid1(test) = tmpkid1;
00100
00101
00102 WN* if_then = WN_Create (WN_opcode(wn), WN_kid_count(wn) - 1);
00103 WN* if_then_block = WN_CreateBlock();
00104 WN_set_operator (if_then, OPR_CALL);
00105
00106
00107 edge->Set_Whirl_Node (if_then);
00108
00109
00110 for (int i = 0; i < WN_kid_count (if_then); i++)
00111 WN_kid (if_then, i) = WN_COPY_Tree_With_Map (WN_kid (wn, i));
00112
00113 WN_st_idx(if_then) = ST_st_idx(st_callee);
00114
00115 WN_INSERT_BlockLast (if_then_block, if_then);
00116
00117
00118 WN* if_else = WN_COPY_Tree_With_Map (wn);
00119 WN* if_else_block = WN_CreateBlock();
00120 WN_INSERT_BlockLast (if_else_block, if_else);
00121
00122 WN * block = WN_Get_Parent (wn, caller->Parent_Map(), caller->Map_Table());
00123
00124 for (WN* stmt = WN_next(wn);
00125 stmt != NULL && Is_Return_Store_Stmt (stmt);)
00126 {
00127 WN_INSERT_BlockLast (if_then_block, WN_COPY_Tree(stmt));
00128 WN_INSERT_BlockLast (if_else_block, WN_COPY_Tree(stmt));
00129
00130
00131 WN * ret_wn = stmt;
00132 stmt = WN_next (stmt);
00133
00134 WN_EXTRACT_FromBlock (block, ret_wn);
00135 }
00136
00137 WN_Parentize (if_then_block, caller->Parent_Map(), caller->Map_Table());
00138 WN_Parentize (if_else_block, caller->Parent_Map(), caller->Map_Table());
00139
00140 WN* wn_if = WN_CreateIf (test, if_then_block, if_else_block);
00141
00142
00143
00144 if (Cur_PU_Feedback)
00145 {
00146 Cur_PU_Feedback->FB_lower_icall (wn, if_else, if_then, wn_if);
00147
00148
00149 Cur_PU_Feedback->Delete(wn);
00150 }
00151
00152
00153 WN_INSERT_BlockAfter (block, wn, wn_if);
00154 WN_EXTRACT_FromBlock (block, wn);
00155 WN_Parentize (block, caller->Parent_Map(), caller->Map_Table());
00156 }
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166 void
00167 IPO_Process_Icalls (IPA_NODE * node)
00168 {
00169 FmtAssert (!node->Is_Visited(),
00170 ("Node is getting visited NOT for the first time"));
00171
00172 node->Set_Visited();
00173
00174 if (node->Total_Succ() == 0) return;
00175
00176
00177 std::deque<WN*> callsite_map;
00178
00179 std::priority_queue<IPA_EDGE *,
00180 vector<IPA_EDGE *>,
00181 order_by_callsite_id> edge_order;
00182
00183
00184 for (WN_ITER* wni = WN_WALK_TreeIter(node->Whirl_Tree(FALSE));
00185 wni != NULL;
00186 wni = WN_WALK_TreeNext(wni))
00187 {
00188 WN* wn = WN_ITER_wn (wni);
00189
00190 switch (WN_operator(wn))
00191 {
00192 case OPR_CALL:
00193 if (WN_opcode(wn) == OPC_VCALL &&
00194 WN_Fake_Call_EH_Region (wn, Parent_Map))
00195 break;
00196
00197 case OPR_ICALL:
00198 case OPR_INTRINSIC_CALL:
00199 callsite_map.push_back (wn);
00200 break;
00201 }
00202 }
00203
00204 IPA_SUCC_ITER succ_iter (node);
00205
00206
00207 for (succ_iter.First(); !succ_iter.Is_Empty(); succ_iter.Next())
00208 {
00209 IPA_EDGE *edge = succ_iter.Current_Edge();
00210 if (edge)
00211 edge_order.push (edge);
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224 UINT32 callsite_idx = 0;
00225 while (!edge_order.empty())
00226 {
00227
00228 IPA_EDGE * edge = edge_order.top();
00229 edge_order.pop();
00230
00231
00232 for (; callsite_idx < edge->Callsite_Id(); callsite_idx++)
00233 callsite_map.pop_front();
00234
00235 FmtAssert (callsite_idx == edge->Callsite_Id(),
00236 ("IPO_Process_Icalls: Invalid callsite index"));
00237
00238 WN * w = callsite_map.front();
00239 if (WN_operator (w) == OPR_CALL)
00240 edge->Set_Whirl_Node (w);
00241 else
00242 {
00243 FmtAssert (WN_operator (w) == OPR_ICALL,
00244 ("IPO_Process_Icalls: Expected ICALL"));
00245 IPA_EDGE * next_edge = (!edge_order.empty()) ?
00246 edge_order.top() : NULL;
00247
00248 if (edge->Summary_Callsite()->Is_func_ptr())
00249 {
00250
00251 edge->Set_Whirl_Node (w);
00252 }
00253 else if (next_edge && next_edge->Summary_Callsite()->Is_func_ptr() &&
00254 (edge->Callsite_Id() + 1 == next_edge->Callsite_Id()))
00255 {
00256
00257
00258
00259 IPA_Call_Graph->Graph()->Delete_Edge (edge->Edge_Index());
00260 callsite_idx++;
00261
00262
00263 next_edge->Set_Whirl_Node (w);
00264 edge_order.pop();
00265 }
00266 else
00267 {
00268
00269 Convert_Icall (w, edge);
00270 callsite_idx++;
00271 }
00272 }
00273
00274 callsite_map.pop_front();
00275 callsite_idx++;
00276 }
00277 }