1 1.1 mrg /* Passes for transactional memory support. 2 1.1 mrg Copyright (C) 2008-2022 Free Software Foundation, Inc. 3 1.1 mrg Contributed by Richard Henderson <rth (at) redhat.com> 4 1.1 mrg and Aldy Hernandez <aldyh (at) redhat.com>. 5 1.1 mrg 6 1.1 mrg This file is part of GCC. 7 1.1 mrg 8 1.1 mrg GCC is free software; you can redistribute it and/or modify it under 9 1.1 mrg the terms of the GNU General Public License as published by the Free 10 1.1 mrg Software Foundation; either version 3, or (at your option) any later 11 1.1 mrg version. 12 1.1 mrg 13 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY 14 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 1.1 mrg for more details. 17 1.1 mrg 18 1.1 mrg You should have received a copy of the GNU General Public License 19 1.1 mrg along with GCC; see the file COPYING3. If not see 20 1.1 mrg <http://www.gnu.org/licenses/>. */ 21 1.1 mrg 22 1.1 mrg #include "config.h" 23 1.1 mrg #include "system.h" 24 1.1 mrg #include "coretypes.h" 25 1.1 mrg #include "backend.h" 26 1.1 mrg #include "target.h" 27 1.1 mrg #include "rtl.h" 28 1.1 mrg #include "tree.h" 29 1.1 mrg #include "gimple.h" 30 1.1 mrg #include "cfghooks.h" 31 1.1 mrg #include "tree-pass.h" 32 1.1 mrg #include "ssa.h" 33 1.1 mrg #include "cgraph.h" 34 1.1 mrg #include "gimple-pretty-print.h" 35 1.1 mrg #include "diagnostic-core.h" 36 1.1 mrg #include "fold-const.h" 37 1.1 mrg #include "tree-eh.h" 38 1.1 mrg #include "calls.h" 39 1.1 mrg #include "gimplify.h" 40 1.1 mrg #include "gimple-iterator.h" 41 1.1 mrg #include "gimplify-me.h" 42 1.1 mrg #include "gimple-walk.h" 43 1.1 mrg #include "tree-cfg.h" 44 1.1 mrg #include "tree-into-ssa.h" 45 1.1 mrg #include "tree-inline.h" 46 1.1 mrg #include "demangle.h" 47 1.1 mrg #include "output.h" 48 1.1 mrg #include "trans-mem.h" 49 1.1 mrg #include "langhooks.h" 50 1.1 mrg #include "cfgloop.h" 51 1.1 mrg #include "tree-ssa-address.h" 52 1.1 mrg #include "stringpool.h" 53 1.1 mrg #include "attribs.h" 54 1.1 mrg #include "alloc-pool.h" 55 1.1 mrg #include "symbol-summary.h" 56 1.1 mrg #include "symtab-thunks.h" 57 1.1 mrg 58 1.1 mrg #define A_RUNINSTRUMENTEDCODE 0x0001 59 1.1 mrg #define A_RUNUNINSTRUMENTEDCODE 0x0002 60 1.1 mrg #define A_SAVELIVEVARIABLES 0x0004 61 1.1 mrg #define A_RESTORELIVEVARIABLES 0x0008 62 1.1 mrg #define A_ABORTTRANSACTION 0x0010 63 1.1 mrg 64 1.1 mrg #define AR_USERABORT 0x0001 65 1.1 mrg #define AR_USERRETRY 0x0002 66 1.1 mrg #define AR_TMCONFLICT 0x0004 67 1.1 mrg #define AR_EXCEPTIONBLOCKABORT 0x0008 68 1.1 mrg #define AR_OUTERABORT 0x0010 69 1.1 mrg 70 1.1 mrg #define MODE_SERIALIRREVOCABLE 0x0000 71 1.1 mrg 72 1.1 mrg 73 1.1 mrg /* The representation of a transaction changes several times during the 74 1.1 mrg lowering process. In the beginning, in the front-end we have the 75 1.1 mrg GENERIC tree TRANSACTION_EXPR. For example, 76 1.1 mrg 77 1.1 mrg __transaction { 78 1.1 mrg local++; 79 1.1 mrg if (++global == 10) 80 1.1 mrg __tm_abort; 81 1.1 mrg } 82 1.1 mrg 83 1.1 mrg During initial gimplification (gimplify.cc) the TRANSACTION_EXPR node is 84 1.1 mrg trivially replaced with a GIMPLE_TRANSACTION node. 85 1.1 mrg 86 1.1 mrg During pass_lower_tm, we examine the body of transactions looking 87 1.1 mrg for aborts. Transactions that do not contain an abort may be 88 1.1 mrg merged into an outer transaction. We also add a TRY-FINALLY node 89 1.1 mrg to arrange for the transaction to be committed on any exit. 90 1.1 mrg 91 1.1 mrg [??? Think about how this arrangement affects throw-with-commit 92 1.1 mrg and throw-with-abort operations. In this case we want the TRY to 93 1.1 mrg handle gotos, but not to catch any exceptions because the transaction 94 1.1 mrg will already be closed.] 95 1.1 mrg 96 1.1 mrg GIMPLE_TRANSACTION [label=NULL] { 97 1.1 mrg try { 98 1.1 mrg local = local + 1; 99 1.1 mrg t0 = global; 100 1.1 mrg t1 = t0 + 1; 101 1.1 mrg global = t1; 102 1.1 mrg if (t1 == 10) 103 1.1 mrg __builtin___tm_abort (); 104 1.1 mrg } finally { 105 1.1 mrg __builtin___tm_commit (); 106 1.1 mrg } 107 1.1 mrg } 108 1.1 mrg 109 1.1 mrg During pass_lower_eh, we create EH regions for the transactions, 110 1.1 mrg intermixed with the regular EH stuff. This gives us a nice persistent 111 1.1 mrg mapping (all the way through rtl) from transactional memory operation 112 1.1 mrg back to the transaction, which allows us to get the abnormal edges 113 1.1 mrg correct to model transaction aborts and restarts: 114 1.1 mrg 115 1.1 mrg GIMPLE_TRANSACTION [label=over] 116 1.1 mrg local = local + 1; 117 1.1 mrg t0 = global; 118 1.1 mrg t1 = t0 + 1; 119 1.1 mrg global = t1; 120 1.1 mrg if (t1 == 10) 121 1.1 mrg __builtin___tm_abort (); 122 1.1 mrg __builtin___tm_commit (); 123 1.1 mrg over: 124 1.1 mrg 125 1.1 mrg This is the end of all_lowering_passes, and so is what is present 126 1.1 mrg during the IPA passes, and through all of the optimization passes. 127 1.1 mrg 128 1.1 mrg During pass_ipa_tm, we examine all GIMPLE_TRANSACTION blocks in all 129 1.1 mrg functions and mark functions for cloning. 130 1.1 mrg 131 1.1 mrg At the end of gimple optimization, before exiting SSA form, 132 1.1 mrg pass_tm_edges replaces statements that perform transactional 133 1.1 mrg memory operations with the appropriate TM builtins, and swap 134 1.1 mrg out function calls with their transactional clones. At this 135 1.1 mrg point we introduce the abnormal transaction restart edges and 136 1.1 mrg complete lowering of the GIMPLE_TRANSACTION node. 137 1.1 mrg 138 1.1 mrg x = __builtin___tm_start (MAY_ABORT); 139 1.1 mrg eh_label: 140 1.1 mrg if (x & abort_transaction) 141 1.1 mrg goto over; 142 1.1 mrg local = local + 1; 143 1.1 mrg t0 = __builtin___tm_load (global); 144 1.1 mrg t1 = t0 + 1; 145 1.1 mrg __builtin___tm_store (&global, t1); 146 1.1 mrg if (t1 == 10) 147 1.1 mrg __builtin___tm_abort (); 148 1.1 mrg __builtin___tm_commit (); 149 1.1 mrg over: 150 1.1 mrg */ 151 1.1 mrg 152 1.1 mrg static void *expand_regions (struct tm_region *, 153 1.1 mrg void *(*callback)(struct tm_region *, void *), 154 1.1 mrg void *, bool); 155 1.1 mrg 156 1.1 mrg 157 1.1 mrg /* Return the attributes we want to examine for X, or NULL if it's not 159 1.1 mrg something we examine. We look at function types, but allow pointers 160 1.1 mrg to function types and function decls and peek through. */ 161 1.1 mrg 162 1.1 mrg static tree 163 1.1 mrg get_attrs_for (const_tree x) 164 1.1 mrg { 165 1.1 mrg if (x == NULL_TREE) 166 1.1 mrg return NULL_TREE; 167 1.1 mrg 168 1.1 mrg switch (TREE_CODE (x)) 169 1.1 mrg { 170 1.1 mrg case FUNCTION_DECL: 171 1.1 mrg return TYPE_ATTRIBUTES (TREE_TYPE (x)); 172 1.1 mrg 173 1.1 mrg default: 174 1.1 mrg if (TYPE_P (x)) 175 1.1 mrg return NULL_TREE; 176 1.1 mrg x = TREE_TYPE (x); 177 1.1 mrg if (TREE_CODE (x) != POINTER_TYPE) 178 1.1 mrg return NULL_TREE; 179 1.1 mrg /* FALLTHRU */ 180 1.1 mrg 181 1.1 mrg case POINTER_TYPE: 182 1.1 mrg x = TREE_TYPE (x); 183 1.1 mrg if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE) 184 1.1 mrg return NULL_TREE; 185 1.1 mrg /* FALLTHRU */ 186 1.1 mrg 187 1.1 mrg case FUNCTION_TYPE: 188 1.1 mrg case METHOD_TYPE: 189 1.1 mrg return TYPE_ATTRIBUTES (x); 190 1.1 mrg } 191 1.1 mrg } 192 1.1 mrg 193 1.1 mrg /* Return true if X has been marked TM_PURE. */ 194 1.1 mrg 195 1.1 mrg bool 196 1.1 mrg is_tm_pure (const_tree x) 197 1.1 mrg { 198 1.1 mrg unsigned flags; 199 1.1 mrg 200 1.1 mrg switch (TREE_CODE (x)) 201 1.1 mrg { 202 1.1 mrg case FUNCTION_DECL: 203 1.1 mrg case FUNCTION_TYPE: 204 1.1 mrg case METHOD_TYPE: 205 1.1 mrg break; 206 1.1 mrg 207 1.1 mrg default: 208 1.1 mrg if (TYPE_P (x)) 209 1.1 mrg return false; 210 1.1 mrg x = TREE_TYPE (x); 211 1.1 mrg if (TREE_CODE (x) != POINTER_TYPE) 212 1.1 mrg return false; 213 1.1 mrg /* FALLTHRU */ 214 1.1 mrg 215 1.1 mrg case POINTER_TYPE: 216 1.1 mrg x = TREE_TYPE (x); 217 1.1 mrg if (TREE_CODE (x) != FUNCTION_TYPE && TREE_CODE (x) != METHOD_TYPE) 218 1.1 mrg return false; 219 1.1 mrg break; 220 1.1 mrg } 221 1.1 mrg 222 1.1 mrg flags = flags_from_decl_or_type (x); 223 1.1 mrg return (flags & ECF_TM_PURE) != 0; 224 1.1 mrg } 225 1.1 mrg 226 1.1 mrg /* Return true if X has been marked TM_IRREVOCABLE. */ 227 1.1 mrg 228 1.1 mrg static bool 229 1.1 mrg is_tm_irrevocable (tree x) 230 1.1 mrg { 231 1.1 mrg tree attrs = get_attrs_for (x); 232 1.1 mrg 233 1.1 mrg if (attrs && lookup_attribute ("transaction_unsafe", attrs)) 234 1.1 mrg return true; 235 1.1 mrg 236 1.1 mrg /* A call to the irrevocable builtin is by definition, 237 1.1 mrg irrevocable. */ 238 1.1 mrg if (TREE_CODE (x) == ADDR_EXPR) 239 1.1 mrg x = TREE_OPERAND (x, 0); 240 1.1 mrg if (TREE_CODE (x) == FUNCTION_DECL 241 1.1 mrg && fndecl_built_in_p (x, BUILT_IN_TM_IRREVOCABLE)) 242 1.1 mrg return true; 243 1.1 mrg 244 1.1 mrg return false; 245 1.1 mrg } 246 1.1 mrg 247 1.1 mrg /* Return true if X has been marked TM_SAFE. */ 248 1.1 mrg 249 1.1 mrg bool 250 1.1 mrg is_tm_safe (const_tree x) 251 1.1 mrg { 252 1.1 mrg if (flag_tm) 253 1.1 mrg { 254 1.1 mrg tree attrs = get_attrs_for (x); 255 1.1 mrg if (attrs) 256 1.1 mrg { 257 1.1 mrg if (lookup_attribute ("transaction_safe", attrs)) 258 1.1 mrg return true; 259 1.1 mrg if (lookup_attribute ("transaction_may_cancel_outer", attrs)) 260 1.1 mrg return true; 261 1.1 mrg } 262 1.1 mrg } 263 1.1 mrg return false; 264 1.1 mrg } 265 1.1 mrg 266 1.1 mrg /* Return true if CALL is const, or tm_pure. */ 267 1.1 mrg 268 1.1 mrg static bool 269 1.1 mrg is_tm_pure_call (gimple *call) 270 1.1 mrg { 271 1.1 mrg return (gimple_call_flags (call) & (ECF_CONST | ECF_TM_PURE)) != 0; 272 1.1 mrg } 273 1.1 mrg 274 1.1 mrg /* Return true if X has been marked TM_CALLABLE. */ 275 1.1 mrg 276 1.1 mrg static bool 277 1.1 mrg is_tm_callable (tree x) 278 1.1 mrg { 279 1.1 mrg tree attrs = get_attrs_for (x); 280 1.1 mrg if (attrs) 281 1.1 mrg { 282 1.1 mrg if (lookup_attribute ("transaction_callable", attrs)) 283 1.1 mrg return true; 284 1.1 mrg if (lookup_attribute ("transaction_safe", attrs)) 285 1.1 mrg return true; 286 1.1 mrg if (lookup_attribute ("transaction_may_cancel_outer", attrs)) 287 1.1 mrg return true; 288 1.1 mrg } 289 1.1 mrg return false; 290 1.1 mrg } 291 1.1 mrg 292 1.1 mrg /* Return true if X has been marked TRANSACTION_MAY_CANCEL_OUTER. */ 293 1.1 mrg 294 1.1 mrg bool 295 1.1 mrg is_tm_may_cancel_outer (tree x) 296 1.1 mrg { 297 1.1 mrg tree attrs = get_attrs_for (x); 298 1.1 mrg if (attrs) 299 1.1 mrg return lookup_attribute ("transaction_may_cancel_outer", attrs) != NULL; 300 1.1 mrg return false; 301 1.1 mrg } 302 1.1 mrg 303 1.1 mrg /* Return true for built in functions that "end" a transaction. */ 304 1.1 mrg 305 1.1 mrg bool 306 1.1 mrg is_tm_ending_fndecl (tree fndecl) 307 1.1 mrg { 308 1.1 mrg if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) 309 1.1 mrg switch (DECL_FUNCTION_CODE (fndecl)) 310 1.1 mrg { 311 1.1 mrg case BUILT_IN_TM_COMMIT: 312 1.1 mrg case BUILT_IN_TM_COMMIT_EH: 313 1.1 mrg case BUILT_IN_TM_ABORT: 314 1.1 mrg case BUILT_IN_TM_IRREVOCABLE: 315 1.1 mrg return true; 316 1.1 mrg default: 317 1.1 mrg break; 318 1.1 mrg } 319 1.1 mrg 320 1.1 mrg return false; 321 1.1 mrg } 322 1.1 mrg 323 1.1 mrg /* Return true if STMT is a built in function call that "ends" a 324 1.1 mrg transaction. */ 325 1.1 mrg 326 1.1 mrg bool 327 1.1 mrg is_tm_ending (gimple *stmt) 328 1.1 mrg { 329 1.1 mrg tree fndecl; 330 1.1 mrg 331 1.1 mrg if (gimple_code (stmt) != GIMPLE_CALL) 332 1.1 mrg return false; 333 1.1 mrg 334 1.1 mrg fndecl = gimple_call_fndecl (stmt); 335 1.1 mrg return (fndecl != NULL_TREE 336 1.1 mrg && is_tm_ending_fndecl (fndecl)); 337 1.1 mrg } 338 1.1 mrg 339 1.1 mrg /* Return true if STMT is a TM load. */ 340 1.1 mrg 341 1.1 mrg static bool 342 1.1 mrg is_tm_load (gimple *stmt) 343 1.1 mrg { 344 1.1 mrg tree fndecl; 345 1.1 mrg 346 1.1 mrg if (gimple_code (stmt) != GIMPLE_CALL) 347 1.1 mrg return false; 348 1.1 mrg 349 1.1 mrg fndecl = gimple_call_fndecl (stmt); 350 1.1 mrg return (fndecl 351 1.1 mrg && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) 352 1.1 mrg && BUILTIN_TM_LOAD_P (DECL_FUNCTION_CODE (fndecl))); 353 1.1 mrg } 354 1.1 mrg 355 1.1 mrg /* Same as above, but for simple TM loads, that is, not the 356 1.1 mrg after-write, after-read, etc optimized variants. */ 357 1.1 mrg 358 1.1 mrg static bool 359 1.1 mrg is_tm_simple_load (gimple *stmt) 360 1.1 mrg { 361 1.1 mrg tree fndecl; 362 1.1 mrg 363 1.1 mrg if (gimple_code (stmt) != GIMPLE_CALL) 364 1.1 mrg return false; 365 1.1 mrg 366 1.1 mrg fndecl = gimple_call_fndecl (stmt); 367 1.1 mrg if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) 368 1.1 mrg { 369 1.1 mrg enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); 370 1.1 mrg return (fcode == BUILT_IN_TM_LOAD_1 371 1.1 mrg || fcode == BUILT_IN_TM_LOAD_2 372 1.1 mrg || fcode == BUILT_IN_TM_LOAD_4 373 1.1 mrg || fcode == BUILT_IN_TM_LOAD_8 374 1.1 mrg || fcode == BUILT_IN_TM_LOAD_FLOAT 375 1.1 mrg || fcode == BUILT_IN_TM_LOAD_DOUBLE 376 1.1 mrg || fcode == BUILT_IN_TM_LOAD_LDOUBLE 377 1.1 mrg || fcode == BUILT_IN_TM_LOAD_M64 378 1.1 mrg || fcode == BUILT_IN_TM_LOAD_M128 379 1.1 mrg || fcode == BUILT_IN_TM_LOAD_M256); 380 1.1 mrg } 381 1.1 mrg return false; 382 1.1 mrg } 383 1.1 mrg 384 1.1 mrg /* Return true if STMT is a TM store. */ 385 1.1 mrg 386 1.1 mrg static bool 387 1.1 mrg is_tm_store (gimple *stmt) 388 1.1 mrg { 389 1.1 mrg tree fndecl; 390 1.1 mrg 391 1.1 mrg if (gimple_code (stmt) != GIMPLE_CALL) 392 1.1 mrg return false; 393 1.1 mrg 394 1.1 mrg fndecl = gimple_call_fndecl (stmt); 395 1.1 mrg return (fndecl 396 1.1 mrg && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) 397 1.1 mrg && BUILTIN_TM_STORE_P (DECL_FUNCTION_CODE (fndecl))); 398 1.1 mrg } 399 1.1 mrg 400 1.1 mrg /* Same as above, but for simple TM stores, that is, not the 401 1.1 mrg after-write, after-read, etc optimized variants. */ 402 1.1 mrg 403 1.1 mrg static bool 404 1.1 mrg is_tm_simple_store (gimple *stmt) 405 1.1 mrg { 406 1.1 mrg tree fndecl; 407 1.1 mrg 408 1.1 mrg if (gimple_code (stmt) != GIMPLE_CALL) 409 1.1 mrg return false; 410 1.1 mrg 411 1.1 mrg fndecl = gimple_call_fndecl (stmt); 412 1.1 mrg if (fndecl 413 1.1 mrg && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) 414 1.1 mrg { 415 1.1 mrg enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl); 416 1.1 mrg return (fcode == BUILT_IN_TM_STORE_1 417 1.1 mrg || fcode == BUILT_IN_TM_STORE_2 418 1.1 mrg || fcode == BUILT_IN_TM_STORE_4 419 1.1 mrg || fcode == BUILT_IN_TM_STORE_8 420 1.1 mrg || fcode == BUILT_IN_TM_STORE_FLOAT 421 1.1 mrg || fcode == BUILT_IN_TM_STORE_DOUBLE 422 1.1 mrg || fcode == BUILT_IN_TM_STORE_LDOUBLE 423 1.1 mrg || fcode == BUILT_IN_TM_STORE_M64 424 1.1 mrg || fcode == BUILT_IN_TM_STORE_M128 425 1.1 mrg || fcode == BUILT_IN_TM_STORE_M256); 426 1.1 mrg } 427 1.1 mrg return false; 428 1.1 mrg } 429 1.1 mrg 430 1.1 mrg /* Return true if FNDECL is BUILT_IN_TM_ABORT. */ 431 1.1 mrg 432 1.1 mrg static bool 433 1.1 mrg is_tm_abort (tree fndecl) 434 1.1 mrg { 435 1.1 mrg return (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_TM_ABORT)); 436 1.1 mrg } 437 1.1 mrg 438 1.1 mrg /* Build a GENERIC tree for a user abort. This is called by front ends 439 1.1 mrg while transforming the __tm_abort statement. */ 440 1.1 mrg 441 1.1 mrg tree 442 1.1 mrg build_tm_abort_call (location_t loc, bool is_outer) 443 1.1 mrg { 444 1.1 mrg return build_call_expr_loc (loc, builtin_decl_explicit (BUILT_IN_TM_ABORT), 1, 445 1.1 mrg build_int_cst (integer_type_node, 446 1.1 mrg AR_USERABORT 447 1.1 mrg | (is_outer ? AR_OUTERABORT : 0))); 448 1.1 mrg } 449 1.1 mrg 450 1.1 mrg /* Map for arbitrary function replacement under TM, as created 452 1.1 mrg by the tm_wrap attribute. */ 453 1.1 mrg 454 1.1 mrg struct tm_wrapper_hasher : ggc_cache_ptr_hash<tree_map> 455 1.1 mrg { 456 1.1 mrg static inline hashval_t hash (tree_map *m) { return m->hash; } 457 1.1 mrg static inline bool 458 1.1 mrg equal (tree_map *a, tree_map *b) 459 1.1 mrg { 460 1.1 mrg return a->base.from == b->base.from; 461 1.1 mrg } 462 1.1 mrg 463 1.1 mrg static int 464 1.1 mrg keep_cache_entry (tree_map *&m) 465 1.1 mrg { 466 1.1 mrg return ggc_marked_p (m->base.from); 467 1.1 mrg } 468 1.1 mrg }; 469 1.1 mrg 470 1.1 mrg static GTY((cache)) hash_table<tm_wrapper_hasher> *tm_wrap_map; 471 1.1 mrg 472 1.1 mrg void 473 1.1 mrg record_tm_replacement (tree from, tree to) 474 1.1 mrg { 475 1.1 mrg struct tree_map **slot, *h; 476 1.1 mrg 477 1.1 mrg /* Do not inline wrapper functions that will get replaced in the TM 478 1.1 mrg pass. 479 1.1 mrg 480 1.1 mrg Suppose you have foo() that will get replaced into tmfoo(). Make 481 1.1 mrg sure the inliner doesn't try to outsmart us and inline foo() 482 1.1 mrg before we get a chance to do the TM replacement. */ 483 1.1 mrg DECL_UNINLINABLE (from) = 1; 484 1.1 mrg 485 1.1 mrg if (tm_wrap_map == NULL) 486 1.1 mrg tm_wrap_map = hash_table<tm_wrapper_hasher>::create_ggc (32); 487 1.1 mrg 488 1.1 mrg h = ggc_alloc<tree_map> (); 489 1.1 mrg h->hash = htab_hash_pointer (from); 490 1.1 mrg h->base.from = from; 491 1.1 mrg h->to = to; 492 1.1 mrg 493 1.1 mrg slot = tm_wrap_map->find_slot_with_hash (h, h->hash, INSERT); 494 1.1 mrg *slot = h; 495 1.1 mrg } 496 1.1 mrg 497 1.1 mrg /* Return a TM-aware replacement function for DECL. */ 498 1.1 mrg 499 1.1 mrg static tree 500 1.1 mrg find_tm_replacement_function (tree fndecl) 501 1.1 mrg { 502 1.1 mrg if (tm_wrap_map) 503 1.1 mrg { 504 1.1 mrg struct tree_map *h, in; 505 1.1 mrg 506 1.1 mrg in.base.from = fndecl; 507 1.1 mrg in.hash = htab_hash_pointer (fndecl); 508 1.1 mrg h = tm_wrap_map->find_with_hash (&in, in.hash); 509 1.1 mrg if (h) 510 1.1 mrg return h->to; 511 1.1 mrg } 512 1.1 mrg 513 1.1 mrg /* ??? We may well want TM versions of most of the common <string.h> 514 1.1 mrg functions. For now, we've already these two defined. */ 515 1.1 mrg /* Adjust expand_call_tm() attributes as necessary for the cases 516 1.1 mrg handled here: */ 517 1.1 mrg if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL) 518 1.1 mrg switch (DECL_FUNCTION_CODE (fndecl)) 519 1.1 mrg { 520 1.1 mrg case BUILT_IN_MEMCPY: 521 1.1 mrg return builtin_decl_explicit (BUILT_IN_TM_MEMCPY); 522 1.1 mrg case BUILT_IN_MEMMOVE: 523 1.1 mrg return builtin_decl_explicit (BUILT_IN_TM_MEMMOVE); 524 1.1 mrg case BUILT_IN_MEMSET: 525 1.1 mrg return builtin_decl_explicit (BUILT_IN_TM_MEMSET); 526 1.1 mrg default: 527 1.1 mrg return NULL; 528 1.1 mrg } 529 1.1 mrg 530 1.1 mrg return NULL; 531 1.1 mrg } 532 1.1 mrg 533 1.1 mrg /* When appropriate, record TM replacement for memory allocation functions. 534 1.1 mrg 535 1.1 mrg FROM is the FNDECL to wrap. */ 536 1.1 mrg void 537 1.1 mrg tm_malloc_replacement (tree from) 538 1.1 mrg { 539 1.1 mrg const char *str; 540 1.1 mrg tree to; 541 1.1 mrg 542 1.1 mrg if (TREE_CODE (from) != FUNCTION_DECL) 543 1.1 mrg return; 544 1.1 mrg 545 1.1 mrg /* If we have a previous replacement, the user must be explicitly 546 1.1 mrg wrapping malloc/calloc/free. They better know what they're 547 1.1 mrg doing... */ 548 1.1 mrg if (find_tm_replacement_function (from)) 549 1.1 mrg return; 550 1.1 mrg 551 1.1 mrg str = IDENTIFIER_POINTER (DECL_NAME (from)); 552 1.1 mrg 553 1.1 mrg if (!strcmp (str, "malloc")) 554 1.1 mrg to = builtin_decl_explicit (BUILT_IN_TM_MALLOC); 555 1.1 mrg else if (!strcmp (str, "calloc")) 556 1.1 mrg to = builtin_decl_explicit (BUILT_IN_TM_CALLOC); 557 1.1 mrg else if (!strcmp (str, "free")) 558 1.1 mrg to = builtin_decl_explicit (BUILT_IN_TM_FREE); 559 1.1 mrg else 560 1.1 mrg return; 561 1.1 mrg 562 1.1 mrg TREE_NOTHROW (to) = 0; 563 1.1 mrg 564 1.1 mrg record_tm_replacement (from, to); 565 1.1 mrg } 566 1.1 mrg 567 1.1 mrg /* Diagnostics for tm_safe functions/regions. Called by the front end 569 1.1 mrg once we've lowered the function to high-gimple. */ 570 1.1 mrg 571 1.1 mrg /* Subroutine of diagnose_tm_safe_errors, called through walk_gimple_seq. 572 1.1 mrg Process exactly one statement. WI->INFO is set to non-null when in 573 1.1 mrg the context of a tm_safe function, and null for a __transaction block. */ 574 1.1 mrg 575 1.1 mrg #define DIAG_TM_OUTER 1 576 1.1 mrg #define DIAG_TM_SAFE 2 577 1.1 mrg #define DIAG_TM_RELAXED 4 578 1.1 mrg 579 1.1 mrg struct diagnose_tm 580 1.1 mrg { 581 1.1 mrg unsigned int summary_flags : 8; 582 1.1 mrg unsigned int block_flags : 8; 583 1.1 mrg unsigned int func_flags : 8; 584 1.1 mrg unsigned int saw_volatile : 1; 585 1.1 mrg gimple *stmt; 586 1.1 mrg }; 587 1.1 mrg 588 1.1 mrg /* Return true if T is a volatile lvalue of some kind. */ 589 1.1 mrg 590 1.1 mrg static bool 591 1.1 mrg volatile_lvalue_p (tree t) 592 1.1 mrg { 593 1.1 mrg return ((SSA_VAR_P (t) || REFERENCE_CLASS_P (t)) 594 1.1 mrg && TREE_THIS_VOLATILE (TREE_TYPE (t))); 595 1.1 mrg } 596 1.1 mrg 597 1.1 mrg /* Tree callback function for diagnose_tm pass. */ 598 1.1 mrg 599 1.1 mrg static tree 600 1.1 mrg diagnose_tm_1_op (tree *tp, int *walk_subtrees, void *data) 601 1.1 mrg { 602 1.1 mrg struct walk_stmt_info *wi = (struct walk_stmt_info *) data; 603 1.1 mrg struct diagnose_tm *d = (struct diagnose_tm *) wi->info; 604 1.1 mrg 605 1.1 mrg if (TYPE_P (*tp)) 606 1.1 mrg *walk_subtrees = false; 607 1.1 mrg else if (volatile_lvalue_p (*tp) 608 1.1 mrg && !d->saw_volatile) 609 1.1 mrg { 610 1.1 mrg d->saw_volatile = 1; 611 1.1 mrg if (d->block_flags & DIAG_TM_SAFE) 612 1.1 mrg error_at (gimple_location (d->stmt), 613 1.1 mrg "invalid use of volatile lvalue inside transaction"); 614 1.1 mrg else if (d->func_flags & DIAG_TM_SAFE) 615 1.1 mrg error_at (gimple_location (d->stmt), 616 1.1 mrg "invalid use of volatile lvalue inside %<transaction_safe%> " 617 1.1 mrg "function"); 618 1.1 mrg } 619 1.1 mrg 620 1.1 mrg return NULL_TREE; 621 1.1 mrg } 622 1.1 mrg 623 1.1 mrg static inline bool 624 1.1 mrg is_tm_safe_or_pure (const_tree x) 625 1.1 mrg { 626 1.1 mrg return is_tm_safe (x) || is_tm_pure (x); 627 1.1 mrg } 628 1.1 mrg 629 1.1 mrg static tree 630 1.1 mrg diagnose_tm_1 (gimple_stmt_iterator *gsi, bool *handled_ops_p, 631 1.1 mrg struct walk_stmt_info *wi) 632 1.1 mrg { 633 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 634 1.1 mrg struct diagnose_tm *d = (struct diagnose_tm *) wi->info; 635 1.1 mrg 636 1.1 mrg /* Save stmt for use in leaf analysis. */ 637 1.1 mrg d->stmt = stmt; 638 1.1 mrg 639 1.1 mrg switch (gimple_code (stmt)) 640 1.1 mrg { 641 1.1 mrg case GIMPLE_CALL: 642 1.1 mrg { 643 1.1 mrg tree fn = gimple_call_fn (stmt); 644 1.1 mrg 645 1.1 mrg if ((d->summary_flags & DIAG_TM_OUTER) == 0 646 1.1 mrg && is_tm_may_cancel_outer (fn)) 647 1.1 mrg error_at (gimple_location (stmt), 648 1.1 mrg "%<transaction_may_cancel_outer%> function call not within" 649 1.1 mrg " outer transaction or %<transaction_may_cancel_outer%>"); 650 1.1 mrg 651 1.1 mrg if (d->summary_flags & DIAG_TM_SAFE) 652 1.1 mrg { 653 1.1 mrg bool is_safe, direct_call_p; 654 1.1 mrg tree replacement; 655 1.1 mrg 656 1.1 mrg if (TREE_CODE (fn) == ADDR_EXPR 657 1.1 mrg && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL) 658 1.1 mrg { 659 1.1 mrg direct_call_p = true; 660 1.1 mrg replacement = TREE_OPERAND (fn, 0); 661 1.1 mrg replacement = find_tm_replacement_function (replacement); 662 1.1 mrg if (replacement) 663 1.1 mrg fn = replacement; 664 1.1 mrg } 665 1.1 mrg else 666 1.1 mrg { 667 1.1 mrg direct_call_p = false; 668 1.1 mrg replacement = NULL_TREE; 669 1.1 mrg } 670 1.1 mrg 671 1.1 mrg if (is_tm_safe_or_pure (fn)) 672 1.1 mrg is_safe = true; 673 1.1 mrg else if (is_tm_callable (fn) || is_tm_irrevocable (fn)) 674 1.1 mrg { 675 1.1 mrg /* A function explicitly marked transaction_callable as 676 1.1 mrg opposed to transaction_safe is being defined to be 677 1.1 mrg unsafe as part of its ABI, regardless of its contents. */ 678 1.1 mrg is_safe = false; 679 1.1 mrg } 680 1.1 mrg else if (direct_call_p) 681 1.1 mrg { 682 1.1 mrg if (IS_TYPE_OR_DECL_P (fn) 683 1.1 mrg && flags_from_decl_or_type (fn) & ECF_TM_BUILTIN) 684 1.1 mrg is_safe = true; 685 1.1 mrg else if (replacement) 686 1.1 mrg { 687 1.1 mrg /* ??? At present we've been considering replacements 688 1.1 mrg merely transaction_callable, and therefore might 689 1.1 mrg enter irrevocable. The tm_wrap attribute has not 690 1.1 mrg yet made it into the new language spec. */ 691 1.1 mrg is_safe = false; 692 1.1 mrg } 693 1.1 mrg else 694 1.1 mrg { 695 1.1 mrg /* ??? Diagnostics for unmarked direct calls moved into 696 1.1 mrg the IPA pass. Section 3.2 of the spec details how 697 1.1 mrg functions not marked should be considered "implicitly 698 1.1 mrg safe" based on having examined the function body. */ 699 1.1 mrg is_safe = true; 700 1.1 mrg } 701 1.1 mrg } 702 1.1 mrg else 703 1.1 mrg { 704 1.1 mrg /* An unmarked indirect call. Consider it unsafe even 705 1.1 mrg though optimization may yet figure out how to inline. */ 706 1.1 mrg is_safe = false; 707 1.1 mrg } 708 1.1 mrg 709 1.1 mrg if (!is_safe) 710 1.1 mrg { 711 1.1 mrg if (TREE_CODE (fn) == ADDR_EXPR) 712 1.1 mrg fn = TREE_OPERAND (fn, 0); 713 1.1 mrg if (d->block_flags & DIAG_TM_SAFE) 714 1.1 mrg { 715 1.1 mrg if (direct_call_p) 716 1.1 mrg error_at (gimple_location (stmt), 717 1.1 mrg "unsafe function call %qD within " 718 1.1 mrg "atomic transaction", fn); 719 1.1 mrg else 720 1.1 mrg { 721 1.1 mrg if ((!DECL_P (fn) || DECL_NAME (fn)) 722 1.1 mrg && TREE_CODE (fn) != SSA_NAME) 723 1.1 mrg error_at (gimple_location (stmt), 724 1.1 mrg "unsafe function call %qE within " 725 1.1 mrg "atomic transaction", fn); 726 1.1 mrg else 727 1.1 mrg error_at (gimple_location (stmt), 728 1.1 mrg "unsafe indirect function call within " 729 1.1 mrg "atomic transaction"); 730 1.1 mrg } 731 1.1 mrg } 732 1.1 mrg else 733 1.1 mrg { 734 1.1 mrg if (direct_call_p) 735 1.1 mrg error_at (gimple_location (stmt), 736 1.1 mrg "unsafe function call %qD within " 737 1.1 mrg "%<transaction_safe%> function", fn); 738 1.1 mrg else 739 1.1 mrg { 740 1.1 mrg if ((!DECL_P (fn) || DECL_NAME (fn)) 741 1.1 mrg && TREE_CODE (fn) != SSA_NAME) 742 1.1 mrg error_at (gimple_location (stmt), 743 1.1 mrg "unsafe function call %qE within " 744 1.1 mrg "%<transaction_safe%> function", fn); 745 1.1 mrg else 746 1.1 mrg error_at (gimple_location (stmt), 747 1.1 mrg "unsafe indirect function call within " 748 1.1 mrg "%<transaction_safe%> function"); 749 1.1 mrg } 750 1.1 mrg } 751 1.1 mrg } 752 1.1 mrg } 753 1.1 mrg } 754 1.1 mrg break; 755 1.1 mrg 756 1.1 mrg case GIMPLE_ASM: 757 1.1 mrg /* ??? We ought to come up with a way to add attributes to 758 1.1 mrg asm statements, and then add "transaction_safe" to it. 759 1.1 mrg Either that or get the language spec to resurrect __tm_waiver. */ 760 1.1 mrg if (d->block_flags & DIAG_TM_SAFE) 761 1.1 mrg error_at (gimple_location (stmt), 762 1.1 mrg "%<asm%> not allowed in atomic transaction"); 763 1.1 mrg else if (d->func_flags & DIAG_TM_SAFE) 764 1.1 mrg error_at (gimple_location (stmt), 765 1.1 mrg "%<asm%> not allowed in %<transaction_safe%> function"); 766 1.1 mrg break; 767 1.1 mrg 768 1.1 mrg case GIMPLE_TRANSACTION: 769 1.1 mrg { 770 1.1 mrg gtransaction *trans_stmt = as_a <gtransaction *> (stmt); 771 1.1 mrg unsigned char inner_flags = DIAG_TM_SAFE; 772 1.1 mrg 773 1.1 mrg if (gimple_transaction_subcode (trans_stmt) & GTMA_IS_RELAXED) 774 1.1 mrg { 775 1.1 mrg if (d->block_flags & DIAG_TM_SAFE) 776 1.1 mrg error_at (gimple_location (stmt), 777 1.1 mrg "relaxed transaction in atomic transaction"); 778 1.1 mrg else if (d->func_flags & DIAG_TM_SAFE) 779 1.1 mrg error_at (gimple_location (stmt), 780 1.1 mrg "relaxed transaction in %<transaction_safe%> function"); 781 1.1 mrg inner_flags = DIAG_TM_RELAXED; 782 1.1 mrg } 783 1.1 mrg else if (gimple_transaction_subcode (trans_stmt) & GTMA_IS_OUTER) 784 1.1 mrg { 785 1.1 mrg if (d->block_flags) 786 1.1 mrg error_at (gimple_location (stmt), 787 1.1 mrg "outer transaction in transaction"); 788 1.1 mrg else if (d->func_flags & DIAG_TM_OUTER) 789 1.1 mrg error_at (gimple_location (stmt), 790 1.1 mrg "outer transaction in " 791 1.1 mrg "%<transaction_may_cancel_outer%> function"); 792 1.1 mrg else if (d->func_flags & DIAG_TM_SAFE) 793 1.1 mrg error_at (gimple_location (stmt), 794 1.1 mrg "outer transaction in %<transaction_safe%> function"); 795 1.1 mrg inner_flags |= DIAG_TM_OUTER; 796 1.1 mrg } 797 1.1 mrg 798 1.1 mrg *handled_ops_p = true; 799 1.1 mrg if (gimple_transaction_body (trans_stmt)) 800 1.1 mrg { 801 1.1 mrg struct walk_stmt_info wi_inner; 802 1.1 mrg struct diagnose_tm d_inner; 803 1.1 mrg 804 1.1 mrg memset (&d_inner, 0, sizeof (d_inner)); 805 1.1 mrg d_inner.func_flags = d->func_flags; 806 1.1 mrg d_inner.block_flags = d->block_flags | inner_flags; 807 1.1 mrg d_inner.summary_flags = d_inner.func_flags | d_inner.block_flags; 808 1.1 mrg 809 1.1 mrg memset (&wi_inner, 0, sizeof (wi_inner)); 810 1.1 mrg wi_inner.info = &d_inner; 811 1.1 mrg 812 1.1 mrg walk_gimple_seq (gimple_transaction_body (trans_stmt), 813 1.1 mrg diagnose_tm_1, diagnose_tm_1_op, &wi_inner); 814 1.1 mrg } 815 1.1 mrg } 816 1.1 mrg break; 817 1.1 mrg 818 1.1 mrg default: 819 1.1 mrg break; 820 1.1 mrg } 821 1.1 mrg 822 1.1 mrg return NULL_TREE; 823 1.1 mrg } 824 1.1 mrg 825 1.1 mrg static unsigned int 826 1.1 mrg diagnose_tm_blocks (void) 827 1.1 mrg { 828 1.1 mrg struct walk_stmt_info wi; 829 1.1 mrg struct diagnose_tm d; 830 1.1 mrg 831 1.1 mrg memset (&d, 0, sizeof (d)); 832 1.1 mrg if (is_tm_may_cancel_outer (current_function_decl)) 833 1.1 mrg d.func_flags = DIAG_TM_OUTER | DIAG_TM_SAFE; 834 1.1 mrg else if (is_tm_safe (current_function_decl)) 835 1.1 mrg d.func_flags = DIAG_TM_SAFE; 836 1.1 mrg d.summary_flags = d.func_flags; 837 1.1 mrg 838 1.1 mrg memset (&wi, 0, sizeof (wi)); 839 1.1 mrg wi.info = &d; 840 1.1 mrg 841 1.1 mrg walk_gimple_seq (gimple_body (current_function_decl), 842 1.1 mrg diagnose_tm_1, diagnose_tm_1_op, &wi); 843 1.1 mrg 844 1.1 mrg return 0; 845 1.1 mrg } 846 1.1 mrg 847 1.1 mrg namespace { 848 1.1 mrg 849 1.1 mrg const pass_data pass_data_diagnose_tm_blocks = 850 1.1 mrg { 851 1.1 mrg GIMPLE_PASS, /* type */ 852 1.1 mrg "*diagnose_tm_blocks", /* name */ 853 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 854 1.1 mrg TV_TRANS_MEM, /* tv_id */ 855 1.1 mrg PROP_gimple_any, /* properties_required */ 856 1.1 mrg 0, /* properties_provided */ 857 1.1 mrg 0, /* properties_destroyed */ 858 1.1 mrg 0, /* todo_flags_start */ 859 1.1 mrg 0, /* todo_flags_finish */ 860 1.1 mrg }; 861 1.1 mrg 862 1.1 mrg class pass_diagnose_tm_blocks : public gimple_opt_pass 863 1.1 mrg { 864 1.1 mrg public: 865 1.1 mrg pass_diagnose_tm_blocks (gcc::context *ctxt) 866 1.1 mrg : gimple_opt_pass (pass_data_diagnose_tm_blocks, ctxt) 867 1.1 mrg {} 868 1.1 mrg 869 1.1 mrg /* opt_pass methods: */ 870 1.1 mrg virtual bool gate (function *) { return flag_tm; } 871 1.1 mrg virtual unsigned int execute (function *) { return diagnose_tm_blocks (); } 872 1.1 mrg 873 1.1 mrg }; // class pass_diagnose_tm_blocks 874 1.1 mrg 875 1.1 mrg } // anon namespace 876 1.1 mrg 877 1.1 mrg gimple_opt_pass * 878 1.1 mrg make_pass_diagnose_tm_blocks (gcc::context *ctxt) 879 1.1 mrg { 880 1.1 mrg return new pass_diagnose_tm_blocks (ctxt); 881 1.1 mrg } 882 1.1 mrg 883 1.1 mrg /* Instead of instrumenting thread private memory, we save the 885 1.1 mrg addresses in a log which we later use to save/restore the addresses 886 1.1 mrg upon transaction start/restart. 887 1.1 mrg 888 1.1 mrg The log is keyed by address, where each element contains individual 889 1.1 mrg statements among different code paths that perform the store. 890 1.1 mrg 891 1.1 mrg This log is later used to generate either plain save/restore of the 892 1.1 mrg addresses upon transaction start/restart, or calls to the ITM_L* 893 1.1 mrg logging functions. 894 1.1 mrg 895 1.1 mrg So for something like: 896 1.1 mrg 897 1.1 mrg struct large { int x[1000]; }; 898 1.1 mrg struct large lala = { 0 }; 899 1.1 mrg __transaction { 900 1.1 mrg lala.x[i] = 123; 901 1.1 mrg ... 902 1.1 mrg } 903 1.1 mrg 904 1.1 mrg We can either save/restore: 905 1.1 mrg 906 1.1 mrg lala = { 0 }; 907 1.1 mrg trxn = _ITM_startTransaction (); 908 1.1 mrg if (trxn & a_saveLiveVariables) 909 1.1 mrg tmp_lala1 = lala.x[i]; 910 1.1 mrg else if (a & a_restoreLiveVariables) 911 1.1 mrg lala.x[i] = tmp_lala1; 912 1.1 mrg 913 1.1 mrg or use the logging functions: 914 1.1 mrg 915 1.1 mrg lala = { 0 }; 916 1.1 mrg trxn = _ITM_startTransaction (); 917 1.1 mrg _ITM_LU4 (&lala.x[i]); 918 1.1 mrg 919 1.1 mrg Obviously, if we use _ITM_L* to log, we prefer to call _ITM_L* as 920 1.1 mrg far up the dominator tree to shadow all of the writes to a given 921 1.1 mrg location (thus reducing the total number of logging calls), but not 922 1.1 mrg so high as to be called on a path that does not perform a 923 1.1 mrg write. */ 924 1.1 mrg 925 1.1 mrg /* One individual log entry. We may have multiple statements for the 926 1.1 mrg same location if neither dominate each other (on different 927 1.1 mrg execution paths). */ 928 1.1 mrg struct tm_log_entry 929 1.1 mrg { 930 1.1 mrg /* Address to save. */ 931 1.1 mrg tree addr; 932 1.1 mrg /* Entry block for the transaction this address occurs in. */ 933 1.1 mrg basic_block entry_block; 934 1.1 mrg /* Dominating statements the store occurs in. */ 935 1.1 mrg vec<gimple *> stmts; 936 1.1 mrg /* Initially, while we are building the log, we place a nonzero 937 1.1 mrg value here to mean that this address *will* be saved with a 938 1.1 mrg save/restore sequence. Later, when generating the save sequence 939 1.1 mrg we place the SSA temp generated here. */ 940 1.1 mrg tree save_var; 941 1.1 mrg }; 942 1.1 mrg 943 1.1 mrg 944 1.1 mrg /* Log entry hashtable helpers. */ 945 1.1 mrg 946 1.1 mrg struct log_entry_hasher : pointer_hash <tm_log_entry> 947 1.1 mrg { 948 1.1 mrg static inline hashval_t hash (const tm_log_entry *); 949 1.1 mrg static inline bool equal (const tm_log_entry *, const tm_log_entry *); 950 1.1 mrg static inline void remove (tm_log_entry *); 951 1.1 mrg }; 952 1.1 mrg 953 1.1 mrg /* Htab support. Return hash value for a `tm_log_entry'. */ 954 1.1 mrg inline hashval_t 955 1.1 mrg log_entry_hasher::hash (const tm_log_entry *log) 956 1.1 mrg { 957 1.1 mrg return iterative_hash_expr (log->addr, 0); 958 1.1 mrg } 959 1.1 mrg 960 1.1 mrg /* Htab support. Return true if two log entries are the same. */ 961 1.1 mrg inline bool 962 1.1 mrg log_entry_hasher::equal (const tm_log_entry *log1, const tm_log_entry *log2) 963 1.1 mrg { 964 1.1 mrg /* FIXME: 965 1.1 mrg 966 1.1 mrg rth: I suggest that we get rid of the component refs etc. 967 1.1 mrg I.e. resolve the reference to base + offset. 968 1.1 mrg 969 1.1 mrg We may need to actually finish a merge with mainline for this, 970 1.1 mrg since we'd like to be presented with Richi's MEM_REF_EXPRs more 971 1.1 mrg often than not. But in the meantime your tm_log_entry could save 972 1.1 mrg the results of get_inner_reference. 973 1.1 mrg 974 1.1 mrg See: g++.dg/tm/pr46653.C 975 1.1 mrg */ 976 1.1 mrg 977 1.1 mrg /* Special case plain equality because operand_equal_p() below will 978 1.1 mrg return FALSE if the addresses are equal but they have 979 1.1 mrg side-effects (e.g. a volatile address). */ 980 1.1 mrg if (log1->addr == log2->addr) 981 1.1 mrg return true; 982 1.1 mrg 983 1.1 mrg return operand_equal_p (log1->addr, log2->addr, 0); 984 1.1 mrg } 985 1.1 mrg 986 1.1 mrg /* Htab support. Free one tm_log_entry. */ 987 1.1 mrg inline void 988 1.1 mrg log_entry_hasher::remove (tm_log_entry *lp) 989 1.1 mrg { 990 1.1 mrg lp->stmts.release (); 991 1.1 mrg free (lp); 992 1.1 mrg } 993 1.1 mrg 994 1.1 mrg 995 1.1 mrg /* The actual log. */ 996 1.1 mrg static hash_table<log_entry_hasher> *tm_log; 997 1.1 mrg 998 1.1 mrg /* Addresses to log with a save/restore sequence. These should be in 999 1.1 mrg dominator order. */ 1000 1.1 mrg static vec<tree> tm_log_save_addresses; 1001 1.1 mrg 1002 1.1 mrg enum thread_memory_type 1003 1.1 mrg { 1004 1.1 mrg mem_non_local = 0, 1005 1.1 mrg mem_thread_local, 1006 1.1 mrg mem_transaction_local, 1007 1.1 mrg mem_max 1008 1.1 mrg }; 1009 1.1 mrg 1010 1.1 mrg struct tm_new_mem_map 1011 1.1 mrg { 1012 1.1 mrg /* SSA_NAME being dereferenced. */ 1013 1.1 mrg tree val; 1014 1.1 mrg enum thread_memory_type local_new_memory; 1015 1.1 mrg }; 1016 1.1 mrg 1017 1.1 mrg /* Hashtable helpers. */ 1018 1.1 mrg 1019 1.1 mrg struct tm_mem_map_hasher : free_ptr_hash <tm_new_mem_map> 1020 1.1 mrg { 1021 1.1 mrg static inline hashval_t hash (const tm_new_mem_map *); 1022 1.1 mrg static inline bool equal (const tm_new_mem_map *, const tm_new_mem_map *); 1023 1.1 mrg }; 1024 1.1 mrg 1025 1.1 mrg inline hashval_t 1026 1.1 mrg tm_mem_map_hasher::hash (const tm_new_mem_map *v) 1027 1.1 mrg { 1028 1.1 mrg return (intptr_t)v->val >> 4; 1029 1.1 mrg } 1030 1.1 mrg 1031 1.1 mrg inline bool 1032 1.1 mrg tm_mem_map_hasher::equal (const tm_new_mem_map *v, const tm_new_mem_map *c) 1033 1.1 mrg { 1034 1.1 mrg return v->val == c->val; 1035 1.1 mrg } 1036 1.1 mrg 1037 1.1 mrg /* Map for an SSA_NAME originally pointing to a non aliased new piece 1038 1.1 mrg of memory (malloc, alloc, etc). */ 1039 1.1 mrg static hash_table<tm_mem_map_hasher> *tm_new_mem_hash; 1040 1.1 mrg 1041 1.1 mrg /* Initialize logging data structures. */ 1042 1.1 mrg static void 1043 1.1 mrg tm_log_init (void) 1044 1.1 mrg { 1045 1.1 mrg tm_log = new hash_table<log_entry_hasher> (10); 1046 1.1 mrg tm_new_mem_hash = new hash_table<tm_mem_map_hasher> (5); 1047 1.1 mrg tm_log_save_addresses.create (5); 1048 1.1 mrg } 1049 1.1 mrg 1050 1.1 mrg /* Free logging data structures. */ 1051 1.1 mrg static void 1052 1.1 mrg tm_log_delete (void) 1053 1.1 mrg { 1054 1.1 mrg delete tm_log; 1055 1.1 mrg tm_log = NULL; 1056 1.1 mrg delete tm_new_mem_hash; 1057 1.1 mrg tm_new_mem_hash = NULL; 1058 1.1 mrg tm_log_save_addresses.release (); 1059 1.1 mrg } 1060 1.1 mrg 1061 1.1 mrg /* Return true if MEM is a transaction invariant memory for the TM 1062 1.1 mrg region starting at REGION_ENTRY_BLOCK. */ 1063 1.1 mrg static bool 1064 1.1 mrg transaction_invariant_address_p (const_tree mem, basic_block region_entry_block) 1065 1.1 mrg { 1066 1.1 mrg if ((TREE_CODE (mem) == INDIRECT_REF || TREE_CODE (mem) == MEM_REF) 1067 1.1 mrg && TREE_CODE (TREE_OPERAND (mem, 0)) == SSA_NAME) 1068 1.1 mrg { 1069 1.1 mrg basic_block def_bb; 1070 1.1 mrg 1071 1.1 mrg def_bb = gimple_bb (SSA_NAME_DEF_STMT (TREE_OPERAND (mem, 0))); 1072 1.1 mrg return def_bb != region_entry_block 1073 1.1 mrg && dominated_by_p (CDI_DOMINATORS, region_entry_block, def_bb); 1074 1.1 mrg } 1075 1.1 mrg 1076 1.1 mrg mem = strip_invariant_refs (mem); 1077 1.1 mrg return mem && (CONSTANT_CLASS_P (mem) || decl_address_invariant_p (mem)); 1078 1.1 mrg } 1079 1.1 mrg 1080 1.1 mrg /* Given an address ADDR in STMT, find it in the memory log or add it, 1081 1.1 mrg making sure to keep only the addresses highest in the dominator 1082 1.1 mrg tree. 1083 1.1 mrg 1084 1.1 mrg ENTRY_BLOCK is the entry_block for the transaction. 1085 1.1 mrg 1086 1.1 mrg If we find the address in the log, make sure it's either the same 1087 1.1 mrg address, or an equivalent one that dominates ADDR. 1088 1.1 mrg 1089 1.1 mrg If we find the address, but neither ADDR dominates the found 1090 1.1 mrg address, nor the found one dominates ADDR, we're on different 1091 1.1 mrg execution paths. Add it. 1092 1.1 mrg 1093 1.1 mrg If known, ENTRY_BLOCK is the entry block for the region, otherwise 1094 1.1 mrg NULL. */ 1095 1.1 mrg static void 1096 1.1 mrg tm_log_add (basic_block entry_block, tree addr, gimple *stmt) 1097 1.1 mrg { 1098 1.1 mrg tm_log_entry **slot; 1099 1.1 mrg struct tm_log_entry l, *lp; 1100 1.1 mrg 1101 1.1 mrg l.addr = addr; 1102 1.1 mrg slot = tm_log->find_slot (&l, INSERT); 1103 1.1 mrg if (!*slot) 1104 1.1 mrg { 1105 1.1 mrg tree type = TREE_TYPE (addr); 1106 1.1 mrg 1107 1.1 mrg lp = XNEW (struct tm_log_entry); 1108 1.1 mrg lp->addr = addr; 1109 1.1 mrg *slot = lp; 1110 1.1 mrg 1111 1.1 mrg /* Small invariant addresses can be handled as save/restores. */ 1112 1.1 mrg if (entry_block 1113 1.1 mrg && transaction_invariant_address_p (lp->addr, entry_block) 1114 1.1 mrg && TYPE_SIZE_UNIT (type) != NULL 1115 1.1 mrg && tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)) 1116 1.1 mrg && ((HOST_WIDE_INT) tree_to_uhwi (TYPE_SIZE_UNIT (type)) 1117 1.1 mrg < param_tm_max_aggregate_size) 1118 1.1 mrg /* We must be able to copy this type normally. I.e., no 1119 1.1 mrg special constructors and the like. */ 1120 1.1 mrg && !TREE_ADDRESSABLE (type)) 1121 1.1 mrg { 1122 1.1 mrg lp->save_var = create_tmp_reg (TREE_TYPE (lp->addr), "tm_save"); 1123 1.1 mrg lp->stmts.create (0); 1124 1.1 mrg lp->entry_block = entry_block; 1125 1.1 mrg /* Save addresses separately in dominator order so we don't 1126 1.1 mrg get confused by overlapping addresses in the save/restore 1127 1.1 mrg sequence. */ 1128 1.1 mrg tm_log_save_addresses.safe_push (lp->addr); 1129 1.1 mrg } 1130 1.1 mrg else 1131 1.1 mrg { 1132 1.1 mrg /* Use the logging functions. */ 1133 1.1 mrg lp->stmts.create (5); 1134 1.1 mrg lp->stmts.quick_push (stmt); 1135 1.1 mrg lp->save_var = NULL; 1136 1.1 mrg } 1137 1.1 mrg } 1138 1.1 mrg else 1139 1.1 mrg { 1140 1.1 mrg size_t i; 1141 1.1 mrg gimple *oldstmt; 1142 1.1 mrg 1143 1.1 mrg lp = *slot; 1144 1.1 mrg 1145 1.1 mrg /* If we're generating a save/restore sequence, we don't care 1146 1.1 mrg about statements. */ 1147 1.1 mrg if (lp->save_var) 1148 1.1 mrg return; 1149 1.1 mrg 1150 1.1 mrg for (i = 0; lp->stmts.iterate (i, &oldstmt); ++i) 1151 1.1 mrg { 1152 1.1 mrg if (stmt == oldstmt) 1153 1.1 mrg return; 1154 1.1 mrg /* We already have a store to the same address, higher up the 1155 1.1 mrg dominator tree. Nothing to do. */ 1156 1.1 mrg if (dominated_by_p (CDI_DOMINATORS, 1157 1.1 mrg gimple_bb (stmt), gimple_bb (oldstmt))) 1158 1.1 mrg return; 1159 1.1 mrg /* We should be processing blocks in dominator tree order. */ 1160 1.1 mrg gcc_assert (!dominated_by_p (CDI_DOMINATORS, 1161 1.1 mrg gimple_bb (oldstmt), gimple_bb (stmt))); 1162 1.1 mrg } 1163 1.1 mrg /* Store is on a different code path. */ 1164 1.1 mrg lp->stmts.safe_push (stmt); 1165 1.1 mrg } 1166 1.1 mrg } 1167 1.1 mrg 1168 1.1 mrg /* Gimplify the address of a TARGET_MEM_REF. Return the SSA_NAME 1169 1.1 mrg result, insert the new statements before GSI. */ 1170 1.1 mrg 1171 1.1 mrg static tree 1172 1.1 mrg gimplify_addr (gimple_stmt_iterator *gsi, tree x) 1173 1.1 mrg { 1174 1.1 mrg if (TREE_CODE (x) == TARGET_MEM_REF) 1175 1.1 mrg x = tree_mem_ref_addr (build_pointer_type (TREE_TYPE (x)), x); 1176 1.1 mrg else 1177 1.1 mrg x = build_fold_addr_expr (x); 1178 1.1 mrg return force_gimple_operand_gsi (gsi, x, true, NULL, true, GSI_SAME_STMT); 1179 1.1 mrg } 1180 1.1 mrg 1181 1.1 mrg /* Instrument one address with the logging functions. 1182 1.1 mrg ADDR is the address to save. 1183 1.1 mrg STMT is the statement before which to place it. */ 1184 1.1 mrg static void 1185 1.1 mrg tm_log_emit_stmt (tree addr, gimple *stmt) 1186 1.1 mrg { 1187 1.1 mrg tree type = TREE_TYPE (addr); 1188 1.1 mrg gimple_stmt_iterator gsi = gsi_for_stmt (stmt); 1189 1.1 mrg gimple *log; 1190 1.1 mrg enum built_in_function code = BUILT_IN_TM_LOG; 1191 1.1 mrg 1192 1.1 mrg if (type == float_type_node) 1193 1.1 mrg code = BUILT_IN_TM_LOG_FLOAT; 1194 1.1 mrg else if (type == double_type_node) 1195 1.1 mrg code = BUILT_IN_TM_LOG_DOUBLE; 1196 1.1 mrg else if (type == long_double_type_node) 1197 1.1 mrg code = BUILT_IN_TM_LOG_LDOUBLE; 1198 1.1 mrg else if (TYPE_SIZE (type) != NULL 1199 1.1 mrg && tree_fits_uhwi_p (TYPE_SIZE (type))) 1200 1.1 mrg { 1201 1.1 mrg unsigned HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type)); 1202 1.1 mrg 1203 1.1 mrg if (TREE_CODE (type) == VECTOR_TYPE) 1204 1.1 mrg { 1205 1.1 mrg switch (type_size) 1206 1.1 mrg { 1207 1.1 mrg case 64: 1208 1.1 mrg code = BUILT_IN_TM_LOG_M64; 1209 1.1 mrg break; 1210 1.1 mrg case 128: 1211 1.1 mrg code = BUILT_IN_TM_LOG_M128; 1212 1.1 mrg break; 1213 1.1 mrg case 256: 1214 1.1 mrg code = BUILT_IN_TM_LOG_M256; 1215 1.1 mrg break; 1216 1.1 mrg default: 1217 1.1 mrg goto unhandled_vec; 1218 1.1 mrg } 1219 1.1 mrg if (!builtin_decl_explicit_p (code)) 1220 1.1 mrg goto unhandled_vec; 1221 1.1 mrg } 1222 1.1 mrg else 1223 1.1 mrg { 1224 1.1 mrg unhandled_vec: 1225 1.1 mrg switch (type_size) 1226 1.1 mrg { 1227 1.1 mrg case 8: 1228 1.1 mrg code = BUILT_IN_TM_LOG_1; 1229 1.1 mrg break; 1230 1.1 mrg case 16: 1231 1.1 mrg code = BUILT_IN_TM_LOG_2; 1232 1.1 mrg break; 1233 1.1 mrg case 32: 1234 1.1 mrg code = BUILT_IN_TM_LOG_4; 1235 1.1 mrg break; 1236 1.1 mrg case 64: 1237 1.1 mrg code = BUILT_IN_TM_LOG_8; 1238 1.1 mrg break; 1239 1.1 mrg } 1240 1.1 mrg } 1241 1.1 mrg } 1242 1.1 mrg 1243 1.1 mrg if (code != BUILT_IN_TM_LOG && !builtin_decl_explicit_p (code)) 1244 1.1 mrg code = BUILT_IN_TM_LOG; 1245 1.1 mrg tree decl = builtin_decl_explicit (code); 1246 1.1 mrg 1247 1.1 mrg addr = gimplify_addr (&gsi, addr); 1248 1.1 mrg if (code == BUILT_IN_TM_LOG) 1249 1.1 mrg log = gimple_build_call (decl, 2, addr, TYPE_SIZE_UNIT (type)); 1250 1.1 mrg else 1251 1.1 mrg log = gimple_build_call (decl, 1, addr); 1252 1.1 mrg gsi_insert_before (&gsi, log, GSI_SAME_STMT); 1253 1.1 mrg } 1254 1.1 mrg 1255 1.1 mrg /* Go through the log and instrument address that must be instrumented 1256 1.1 mrg with the logging functions. Leave the save/restore addresses for 1257 1.1 mrg later. */ 1258 1.1 mrg static void 1259 1.1 mrg tm_log_emit (void) 1260 1.1 mrg { 1261 1.1 mrg hash_table<log_entry_hasher>::iterator hi; 1262 1.1 mrg struct tm_log_entry *lp; 1263 1.1 mrg 1264 1.1 mrg FOR_EACH_HASH_TABLE_ELEMENT (*tm_log, lp, tm_log_entry_t, hi) 1265 1.1 mrg { 1266 1.1 mrg size_t i; 1267 1.1 mrg gimple *stmt; 1268 1.1 mrg 1269 1.1 mrg if (dump_file) 1270 1.1 mrg { 1271 1.1 mrg fprintf (dump_file, "TM thread private mem logging: "); 1272 1.1 mrg print_generic_expr (dump_file, lp->addr); 1273 1.1 mrg fprintf (dump_file, "\n"); 1274 1.1 mrg } 1275 1.1 mrg 1276 1.1 mrg if (lp->save_var) 1277 1.1 mrg { 1278 1.1 mrg if (dump_file) 1279 1.1 mrg fprintf (dump_file, "DUMPING to variable\n"); 1280 1.1 mrg continue; 1281 1.1 mrg } 1282 1.1 mrg else 1283 1.1 mrg { 1284 1.1 mrg if (dump_file) 1285 1.1 mrg fprintf (dump_file, "DUMPING with logging functions\n"); 1286 1.1 mrg for (i = 0; lp->stmts.iterate (i, &stmt); ++i) 1287 1.1 mrg tm_log_emit_stmt (lp->addr, stmt); 1288 1.1 mrg } 1289 1.1 mrg } 1290 1.1 mrg } 1291 1.1 mrg 1292 1.1 mrg /* Emit the save sequence for the corresponding addresses in the log. 1293 1.1 mrg ENTRY_BLOCK is the entry block for the transaction. 1294 1.1 mrg BB is the basic block to insert the code in. */ 1295 1.1 mrg static void 1296 1.1 mrg tm_log_emit_saves (basic_block entry_block, basic_block bb) 1297 1.1 mrg { 1298 1.1 mrg size_t i; 1299 1.1 mrg gimple_stmt_iterator gsi = gsi_last_bb (bb); 1300 1.1 mrg gimple *stmt; 1301 1.1 mrg struct tm_log_entry l, *lp; 1302 1.1 mrg 1303 1.1 mrg for (i = 0; i < tm_log_save_addresses.length (); ++i) 1304 1.1 mrg { 1305 1.1 mrg l.addr = tm_log_save_addresses[i]; 1306 1.1 mrg lp = *(tm_log->find_slot (&l, NO_INSERT)); 1307 1.1 mrg gcc_assert (lp->save_var != NULL); 1308 1.1 mrg 1309 1.1 mrg /* We only care about variables in the current transaction. */ 1310 1.1 mrg if (lp->entry_block != entry_block) 1311 1.1 mrg continue; 1312 1.1 mrg 1313 1.1 mrg stmt = gimple_build_assign (lp->save_var, unshare_expr (lp->addr)); 1314 1.1 mrg 1315 1.1 mrg /* Make sure we can create an SSA_NAME for this type. For 1316 1.1 mrg instance, aggregates aren't allowed, in which case the system 1317 1.1 mrg will create a VOP for us and everything will just work. */ 1318 1.1 mrg if (is_gimple_reg_type (TREE_TYPE (lp->save_var))) 1319 1.1 mrg { 1320 1.1 mrg lp->save_var = make_ssa_name (lp->save_var, stmt); 1321 1.1 mrg gimple_assign_set_lhs (stmt, lp->save_var); 1322 1.1 mrg } 1323 1.1 mrg 1324 1.1 mrg gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); 1325 1.1 mrg } 1326 1.1 mrg } 1327 1.1 mrg 1328 1.1 mrg /* Emit the restore sequence for the corresponding addresses in the log. 1329 1.1 mrg ENTRY_BLOCK is the entry block for the transaction. 1330 1.1 mrg BB is the basic block to insert the code in. */ 1331 1.1 mrg static void 1332 1.1 mrg tm_log_emit_restores (basic_block entry_block, basic_block bb) 1333 1.1 mrg { 1334 1.1 mrg int i; 1335 1.1 mrg struct tm_log_entry l, *lp; 1336 1.1 mrg gimple_stmt_iterator gsi; 1337 1.1 mrg gimple *stmt; 1338 1.1 mrg 1339 1.1 mrg for (i = tm_log_save_addresses.length () - 1; i >= 0; i--) 1340 1.1 mrg { 1341 1.1 mrg l.addr = tm_log_save_addresses[i]; 1342 1.1 mrg lp = *(tm_log->find_slot (&l, NO_INSERT)); 1343 1.1 mrg gcc_assert (lp->save_var != NULL); 1344 1.1 mrg 1345 1.1 mrg /* We only care about variables in the current transaction. */ 1346 1.1 mrg if (lp->entry_block != entry_block) 1347 1.1 mrg continue; 1348 1.1 mrg 1349 1.1 mrg /* Restores are in LIFO order from the saves in case we have 1350 1.1 mrg overlaps. */ 1351 1.1 mrg gsi = gsi_start_bb (bb); 1352 1.1 mrg 1353 1.1 mrg stmt = gimple_build_assign (unshare_expr (lp->addr), lp->save_var); 1354 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 1355 1.1 mrg } 1356 1.1 mrg } 1357 1.1 mrg 1358 1.1 mrg 1359 1.1 mrg static tree lower_sequence_tm (gimple_stmt_iterator *, bool *, 1361 1.1 mrg struct walk_stmt_info *); 1362 1.1 mrg static tree lower_sequence_no_tm (gimple_stmt_iterator *, bool *, 1363 1.1 mrg struct walk_stmt_info *); 1364 1.1 mrg 1365 1.1 mrg /* Evaluate an address X being dereferenced and determine if it 1366 1.1 mrg originally points to a non aliased new chunk of memory (malloc, 1367 1.1 mrg alloca, etc). 1368 1.1 mrg 1369 1.1 mrg Return MEM_THREAD_LOCAL if it points to a thread-local address. 1370 1.1 mrg Return MEM_TRANSACTION_LOCAL if it points to a transaction-local address. 1371 1.1 mrg Return MEM_NON_LOCAL otherwise. 1372 1.1 mrg 1373 1.1 mrg ENTRY_BLOCK is the entry block to the transaction containing the 1374 1.1 mrg dereference of X. */ 1375 1.1 mrg static enum thread_memory_type 1376 1.1 mrg thread_private_new_memory (basic_block entry_block, tree x) 1377 1.1 mrg { 1378 1.1 mrg gimple *stmt = NULL; 1379 1.1 mrg enum tree_code code; 1380 1.1 mrg tm_new_mem_map **slot; 1381 1.1 mrg tm_new_mem_map elt, *elt_p; 1382 1.1 mrg tree val = x; 1383 1.1 mrg enum thread_memory_type retval = mem_transaction_local; 1384 1.1 mrg 1385 1.1 mrg if (!entry_block 1386 1.1 mrg || TREE_CODE (x) != SSA_NAME 1387 1.1 mrg /* Possible uninitialized use, or a function argument. In 1388 1.1 mrg either case, we don't care. */ 1389 1.1 mrg || SSA_NAME_IS_DEFAULT_DEF (x)) 1390 1.1 mrg return mem_non_local; 1391 1.1 mrg 1392 1.1 mrg /* Look in cache first. */ 1393 1.1 mrg elt.val = x; 1394 1.1 mrg slot = tm_new_mem_hash->find_slot (&elt, INSERT); 1395 1.1 mrg elt_p = *slot; 1396 1.1 mrg if (elt_p) 1397 1.1 mrg return elt_p->local_new_memory; 1398 1.1 mrg 1399 1.1 mrg /* Optimistically assume the memory is transaction local during 1400 1.1 mrg processing. This catches recursion into this variable. */ 1401 1.1 mrg *slot = elt_p = XNEW (tm_new_mem_map); 1402 1.1 mrg elt_p->val = val; 1403 1.1 mrg elt_p->local_new_memory = mem_transaction_local; 1404 1.1 mrg 1405 1.1 mrg /* Search DEF chain to find the original definition of this address. */ 1406 1.1 mrg do 1407 1.1 mrg { 1408 1.1 mrg if (ptr_deref_may_alias_global_p (x, true)) 1409 1.1 mrg { 1410 1.1 mrg /* Address escapes. This is not thread-private. */ 1411 1.1 mrg retval = mem_non_local; 1412 1.1 mrg goto new_memory_ret; 1413 1.1 mrg } 1414 1.1 mrg 1415 1.1 mrg stmt = SSA_NAME_DEF_STMT (x); 1416 1.1 mrg 1417 1.1 mrg /* If the malloc call is outside the transaction, this is 1418 1.1 mrg thread-local. */ 1419 1.1 mrg if (retval != mem_thread_local 1420 1.1 mrg && !dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt), entry_block)) 1421 1.1 mrg retval = mem_thread_local; 1422 1.1 mrg 1423 1.1 mrg if (is_gimple_assign (stmt)) 1424 1.1 mrg { 1425 1.1 mrg code = gimple_assign_rhs_code (stmt); 1426 1.1 mrg /* x = foo ==> foo */ 1427 1.1 mrg if (code == SSA_NAME) 1428 1.1 mrg x = gimple_assign_rhs1 (stmt); 1429 1.1 mrg /* x = foo + n ==> foo */ 1430 1.1 mrg else if (code == POINTER_PLUS_EXPR) 1431 1.1 mrg x = gimple_assign_rhs1 (stmt); 1432 1.1 mrg /* x = (cast*) foo ==> foo */ 1433 1.1 mrg else if (code == VIEW_CONVERT_EXPR || CONVERT_EXPR_CODE_P (code)) 1434 1.1 mrg x = gimple_assign_rhs1 (stmt); 1435 1.1 mrg /* x = c ? op1 : op2 == > op1 or op2 just like a PHI */ 1436 1.1 mrg else if (code == COND_EXPR) 1437 1.1 mrg { 1438 1.1 mrg tree op1 = gimple_assign_rhs2 (stmt); 1439 1.1 mrg tree op2 = gimple_assign_rhs3 (stmt); 1440 1.1 mrg enum thread_memory_type mem; 1441 1.1 mrg retval = thread_private_new_memory (entry_block, op1); 1442 1.1 mrg if (retval == mem_non_local) 1443 1.1 mrg goto new_memory_ret; 1444 1.1 mrg mem = thread_private_new_memory (entry_block, op2); 1445 1.1 mrg retval = MIN (retval, mem); 1446 1.1 mrg goto new_memory_ret; 1447 1.1 mrg } 1448 1.1 mrg else 1449 1.1 mrg { 1450 1.1 mrg retval = mem_non_local; 1451 1.1 mrg goto new_memory_ret; 1452 1.1 mrg } 1453 1.1 mrg } 1454 1.1 mrg else 1455 1.1 mrg { 1456 1.1 mrg if (gimple_code (stmt) == GIMPLE_PHI) 1457 1.1 mrg { 1458 1.1 mrg unsigned int i; 1459 1.1 mrg enum thread_memory_type mem; 1460 1.1 mrg tree phi_result = gimple_phi_result (stmt); 1461 1.1 mrg 1462 1.1 mrg /* If any of the ancestors are non-local, we are sure to 1463 1.1 mrg be non-local. Otherwise we can avoid doing anything 1464 1.1 mrg and inherit what has already been generated. */ 1465 1.1 mrg retval = mem_max; 1466 1.1 mrg for (i = 0; i < gimple_phi_num_args (stmt); ++i) 1467 1.1 mrg { 1468 1.1 mrg tree op = PHI_ARG_DEF (stmt, i); 1469 1.1 mrg 1470 1.1 mrg /* Exclude self-assignment. */ 1471 1.1 mrg if (phi_result == op) 1472 1.1 mrg continue; 1473 1.1 mrg 1474 1.1 mrg mem = thread_private_new_memory (entry_block, op); 1475 1.1 mrg if (mem == mem_non_local) 1476 1.1 mrg { 1477 1.1 mrg retval = mem; 1478 1.1 mrg goto new_memory_ret; 1479 1.1 mrg } 1480 1.1 mrg retval = MIN (retval, mem); 1481 1.1 mrg } 1482 1.1 mrg goto new_memory_ret; 1483 1.1 mrg } 1484 1.1 mrg break; 1485 1.1 mrg } 1486 1.1 mrg } 1487 1.1 mrg while (TREE_CODE (x) == SSA_NAME); 1488 1.1 mrg 1489 1.1 mrg if (stmt && is_gimple_call (stmt) && gimple_call_flags (stmt) & ECF_MALLOC) 1490 1.1 mrg /* Thread-local or transaction-local. */ 1491 1.1 mrg ; 1492 1.1 mrg else 1493 1.1 mrg retval = mem_non_local; 1494 1.1 mrg 1495 1.1 mrg new_memory_ret: 1496 1.1 mrg elt_p->local_new_memory = retval; 1497 1.1 mrg return retval; 1498 1.1 mrg } 1499 1.1 mrg 1500 1.1 mrg /* Determine whether X has to be instrumented using a read 1501 1.1 mrg or write barrier. 1502 1.1 mrg 1503 1.1 mrg ENTRY_BLOCK is the entry block for the region where stmt resides 1504 1.1 mrg in. NULL if unknown. 1505 1.1 mrg 1506 1.1 mrg STMT is the statement in which X occurs in. It is used for thread 1507 1.1 mrg private memory instrumentation. If no TPM instrumentation is 1508 1.1 mrg desired, STMT should be null. */ 1509 1.1 mrg static bool 1510 1.1 mrg requires_barrier (basic_block entry_block, tree x, gimple *stmt) 1511 1.1 mrg { 1512 1.1 mrg tree orig = x; 1513 1.1 mrg while (handled_component_p (x)) 1514 1.1 mrg x = TREE_OPERAND (x, 0); 1515 1.1 mrg 1516 1.1 mrg switch (TREE_CODE (x)) 1517 1.1 mrg { 1518 1.1 mrg case INDIRECT_REF: 1519 1.1 mrg case MEM_REF: 1520 1.1 mrg { 1521 1.1 mrg enum thread_memory_type ret; 1522 1.1 mrg 1523 1.1 mrg ret = thread_private_new_memory (entry_block, TREE_OPERAND (x, 0)); 1524 1.1 mrg if (ret == mem_non_local) 1525 1.1 mrg return true; 1526 1.1 mrg if (stmt && ret == mem_thread_local) 1527 1.1 mrg /* ?? Should we pass `orig', or the INDIRECT_REF X. ?? */ 1528 1.1 mrg tm_log_add (entry_block, orig, stmt); 1529 1.1 mrg 1530 1.1 mrg /* Transaction-locals require nothing at all. For malloc, a 1531 1.1 mrg transaction restart frees the memory and we reallocate. 1532 1.1 mrg For alloca, the stack pointer gets reset by the retry and 1533 1.1 mrg we reallocate. */ 1534 1.1 mrg return false; 1535 1.1 mrg } 1536 1.1 mrg 1537 1.1 mrg case TARGET_MEM_REF: 1538 1.1 mrg if (TREE_CODE (TMR_BASE (x)) != ADDR_EXPR) 1539 1.1 mrg return true; 1540 1.1 mrg x = TREE_OPERAND (TMR_BASE (x), 0); 1541 1.1 mrg if (TREE_CODE (x) == PARM_DECL) 1542 1.1 mrg return false; 1543 1.1 mrg gcc_assert (VAR_P (x)); 1544 1.1 mrg /* FALLTHRU */ 1545 1.1 mrg 1546 1.1 mrg case PARM_DECL: 1547 1.1 mrg case RESULT_DECL: 1548 1.1 mrg case VAR_DECL: 1549 1.1 mrg if (DECL_BY_REFERENCE (x)) 1550 1.1 mrg { 1551 1.1 mrg /* ??? This value is a pointer, but aggregate_value_p has been 1552 1.1 mrg jigged to return true which confuses needs_to_live_in_memory. 1553 1.1 mrg This ought to be cleaned up generically. 1554 1.1 mrg 1555 1.1 mrg FIXME: Verify this still happens after the next mainline 1556 1.1 mrg merge. Testcase ie g++.dg/tm/pr47554.C. 1557 1.1 mrg */ 1558 1.1 mrg return false; 1559 1.1 mrg } 1560 1.1 mrg 1561 1.1 mrg if (is_global_var (x)) 1562 1.1 mrg return !TREE_READONLY (x); 1563 1.1 mrg if (/* FIXME: This condition should actually go below in the 1564 1.1 mrg tm_log_add() call, however is_call_clobbered() depends on 1565 1.1 mrg aliasing info which is not available during 1566 1.1 mrg gimplification. Since requires_barrier() gets called 1567 1.1 mrg during lower_sequence_tm/gimplification, leave the call 1568 1.1 mrg to needs_to_live_in_memory until we eliminate 1569 1.1 mrg lower_sequence_tm altogether. */ 1570 1.1 mrg needs_to_live_in_memory (x)) 1571 1.1 mrg return true; 1572 1.1 mrg else 1573 1.1 mrg { 1574 1.1 mrg /* For local memory that doesn't escape (aka thread private 1575 1.1 mrg memory), we can either save the value at the beginning of 1576 1.1 mrg the transaction and restore on restart, or call a tm 1577 1.1 mrg function to dynamically save and restore on restart 1578 1.1 mrg (ITM_L*). */ 1579 1.1 mrg if (stmt) 1580 1.1 mrg tm_log_add (entry_block, orig, stmt); 1581 1.1 mrg return false; 1582 1.1 mrg } 1583 1.1 mrg 1584 1.1 mrg default: 1585 1.1 mrg return false; 1586 1.1 mrg } 1587 1.1 mrg } 1588 1.1 mrg 1589 1.1 mrg /* Mark the GIMPLE_ASSIGN statement as appropriate for being inside 1590 1.1 mrg a transaction region. */ 1591 1.1 mrg 1592 1.1 mrg static void 1593 1.1 mrg examine_assign_tm (unsigned *state, gimple_stmt_iterator *gsi) 1594 1.1 mrg { 1595 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 1596 1.1 mrg 1597 1.1 mrg if (requires_barrier (/*entry_block=*/NULL, gimple_assign_rhs1 (stmt), NULL)) 1598 1.1 mrg *state |= GTMA_HAVE_LOAD; 1599 1.1 mrg if (requires_barrier (/*entry_block=*/NULL, gimple_assign_lhs (stmt), NULL)) 1600 1.1 mrg *state |= GTMA_HAVE_STORE; 1601 1.1 mrg } 1602 1.1 mrg 1603 1.1 mrg /* Mark a GIMPLE_CALL as appropriate for being inside a transaction. */ 1604 1.1 mrg 1605 1.1 mrg static void 1606 1.1 mrg examine_call_tm (unsigned *state, gimple_stmt_iterator *gsi) 1607 1.1 mrg { 1608 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 1609 1.1 mrg tree fn; 1610 1.1 mrg 1611 1.1 mrg if (is_tm_pure_call (stmt)) 1612 1.1 mrg return; 1613 1.1 mrg 1614 1.1 mrg /* Check if this call is a transaction abort. */ 1615 1.1 mrg fn = gimple_call_fndecl (stmt); 1616 1.1 mrg if (is_tm_abort (fn)) 1617 1.1 mrg *state |= GTMA_HAVE_ABORT; 1618 1.1 mrg 1619 1.1 mrg /* Note that something may happen. */ 1620 1.1 mrg *state |= GTMA_HAVE_LOAD | GTMA_HAVE_STORE; 1621 1.1 mrg } 1622 1.1 mrg 1623 1.1 mrg /* Iterate through the statements in the sequence, moving labels 1624 1.1 mrg (and thus edges) of transactions from "label_norm" to "label_uninst". */ 1625 1.1 mrg 1626 1.1 mrg static tree 1627 1.1 mrg make_tm_uninst (gimple_stmt_iterator *gsi, bool *handled_ops_p, 1628 1.1 mrg struct walk_stmt_info *) 1629 1.1 mrg { 1630 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 1631 1.1 mrg 1632 1.1 mrg if (gtransaction *txn = dyn_cast <gtransaction *> (stmt)) 1633 1.1 mrg { 1634 1.1 mrg *handled_ops_p = true; 1635 1.1 mrg txn->label_uninst = txn->label_norm; 1636 1.1 mrg txn->label_norm = NULL; 1637 1.1 mrg } 1638 1.1 mrg else 1639 1.1 mrg *handled_ops_p = !gimple_has_substatements (stmt); 1640 1.1 mrg 1641 1.1 mrg return NULL_TREE; 1642 1.1 mrg } 1643 1.1 mrg 1644 1.1 mrg /* Lower a GIMPLE_TRANSACTION statement. */ 1645 1.1 mrg 1646 1.1 mrg static void 1647 1.1 mrg lower_transaction (gimple_stmt_iterator *gsi, struct walk_stmt_info *wi) 1648 1.1 mrg { 1649 1.1 mrg gimple *g; 1650 1.1 mrg gtransaction *stmt = as_a <gtransaction *> (gsi_stmt (*gsi)); 1651 1.1 mrg unsigned int *outer_state = (unsigned int *) wi->info; 1652 1.1 mrg unsigned int this_state = 0; 1653 1.1 mrg struct walk_stmt_info this_wi; 1654 1.1 mrg 1655 1.1 mrg /* First, lower the body. The scanning that we do inside gives 1656 1.1 mrg us some idea of what we're dealing with. */ 1657 1.1 mrg memset (&this_wi, 0, sizeof (this_wi)); 1658 1.1 mrg this_wi.info = (void *) &this_state; 1659 1.1 mrg walk_gimple_seq_mod (gimple_transaction_body_ptr (stmt), 1660 1.1 mrg lower_sequence_tm, NULL, &this_wi); 1661 1.1 mrg 1662 1.1 mrg /* If there was absolutely nothing transaction related inside the 1663 1.1 mrg transaction, we may elide it. Likewise if this is a nested 1664 1.1 mrg transaction and does not contain an abort. */ 1665 1.1 mrg if (this_state == 0 1666 1.1 mrg || (!(this_state & GTMA_HAVE_ABORT) && outer_state != NULL)) 1667 1.1 mrg { 1668 1.1 mrg if (outer_state) 1669 1.1 mrg *outer_state |= this_state; 1670 1.1 mrg 1671 1.1 mrg gsi_insert_seq_before (gsi, gimple_transaction_body (stmt), 1672 1.1 mrg GSI_SAME_STMT); 1673 1.1 mrg gimple_transaction_set_body (stmt, NULL); 1674 1.1 mrg 1675 1.1 mrg gsi_remove (gsi, true); 1676 1.1 mrg wi->removed_stmt = true; 1677 1.1 mrg return; 1678 1.1 mrg } 1679 1.1 mrg 1680 1.1 mrg /* Wrap the body of the transaction in a try-finally node so that 1681 1.1 mrg the commit call is always properly called. */ 1682 1.1 mrg g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT), 0); 1683 1.1 mrg if (flag_exceptions) 1684 1.1 mrg { 1685 1.1 mrg tree ptr; 1686 1.1 mrg gimple_seq n_seq, e_seq; 1687 1.1 mrg 1688 1.1 mrg n_seq = gimple_seq_alloc_with_stmt (g); 1689 1.1 mrg e_seq = NULL; 1690 1.1 mrg 1691 1.1 mrg g = gimple_build_call (builtin_decl_explicit (BUILT_IN_EH_POINTER), 1692 1.1 mrg 1, integer_zero_node); 1693 1.1 mrg ptr = create_tmp_var (ptr_type_node); 1694 1.1 mrg gimple_call_set_lhs (g, ptr); 1695 1.1 mrg gimple_seq_add_stmt (&e_seq, g); 1696 1.1 mrg 1697 1.1 mrg g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_COMMIT_EH), 1698 1.1 mrg 1, ptr); 1699 1.1 mrg gimple_seq_add_stmt (&e_seq, g); 1700 1.1 mrg 1701 1.1 mrg g = gimple_build_eh_else (n_seq, e_seq); 1702 1.1 mrg } 1703 1.1 mrg 1704 1.1 mrg g = gimple_build_try (gimple_transaction_body (stmt), 1705 1.1 mrg gimple_seq_alloc_with_stmt (g), GIMPLE_TRY_FINALLY); 1706 1.1 mrg 1707 1.1 mrg /* For a (potentially) outer transaction, create two paths. */ 1708 1.1 mrg gimple_seq uninst = NULL; 1709 1.1 mrg if (outer_state == NULL) 1710 1.1 mrg { 1711 1.1 mrg uninst = copy_gimple_seq_and_replace_locals (g); 1712 1.1 mrg /* In the uninstrumented copy, reset inner transactions to have only 1713 1.1 mrg an uninstrumented code path. */ 1714 1.1 mrg memset (&this_wi, 0, sizeof (this_wi)); 1715 1.1 mrg walk_gimple_seq (uninst, make_tm_uninst, NULL, &this_wi); 1716 1.1 mrg } 1717 1.1 mrg 1718 1.1 mrg tree label1 = create_artificial_label (UNKNOWN_LOCATION); 1719 1.1 mrg gsi_insert_after (gsi, gimple_build_label (label1), GSI_CONTINUE_LINKING); 1720 1.1 mrg gsi_insert_after (gsi, g, GSI_CONTINUE_LINKING); 1721 1.1 mrg gimple_transaction_set_label_norm (stmt, label1); 1722 1.1 mrg 1723 1.1 mrg /* If the transaction calls abort or if this is an outer transaction, 1724 1.1 mrg add an "over" label afterwards. */ 1725 1.1 mrg tree label3 = NULL; 1726 1.1 mrg if ((this_state & GTMA_HAVE_ABORT) 1727 1.1 mrg || outer_state == NULL 1728 1.1 mrg || (gimple_transaction_subcode (stmt) & GTMA_IS_OUTER)) 1729 1.1 mrg { 1730 1.1 mrg label3 = create_artificial_label (UNKNOWN_LOCATION); 1731 1.1 mrg gimple_transaction_set_label_over (stmt, label3); 1732 1.1 mrg } 1733 1.1 mrg 1734 1.1 mrg if (uninst != NULL) 1735 1.1 mrg { 1736 1.1 mrg gsi_insert_after (gsi, gimple_build_goto (label3), GSI_CONTINUE_LINKING); 1737 1.1 mrg 1738 1.1 mrg tree label2 = create_artificial_label (UNKNOWN_LOCATION); 1739 1.1 mrg gsi_insert_after (gsi, gimple_build_label (label2), GSI_CONTINUE_LINKING); 1740 1.1 mrg gsi_insert_seq_after (gsi, uninst, GSI_CONTINUE_LINKING); 1741 1.1 mrg gimple_transaction_set_label_uninst (stmt, label2); 1742 1.1 mrg } 1743 1.1 mrg 1744 1.1 mrg if (label3 != NULL) 1745 1.1 mrg gsi_insert_after (gsi, gimple_build_label (label3), GSI_CONTINUE_LINKING); 1746 1.1 mrg 1747 1.1 mrg gimple_transaction_set_body (stmt, NULL); 1748 1.1 mrg 1749 1.1 mrg /* Record the set of operations found for use later. */ 1750 1.1 mrg this_state |= gimple_transaction_subcode (stmt) & GTMA_DECLARATION_MASK; 1751 1.1 mrg gimple_transaction_set_subcode (stmt, this_state); 1752 1.1 mrg } 1753 1.1 mrg 1754 1.1 mrg /* Iterate through the statements in the sequence, lowering them all 1755 1.1 mrg as appropriate for being in a transaction. */ 1756 1.1 mrg 1757 1.1 mrg static tree 1758 1.1 mrg lower_sequence_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p, 1759 1.1 mrg struct walk_stmt_info *wi) 1760 1.1 mrg { 1761 1.1 mrg unsigned int *state = (unsigned int *) wi->info; 1762 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 1763 1.1 mrg 1764 1.1 mrg *handled_ops_p = true; 1765 1.1 mrg switch (gimple_code (stmt)) 1766 1.1 mrg { 1767 1.1 mrg case GIMPLE_ASSIGN: 1768 1.1 mrg /* Only memory reads/writes need to be instrumented. */ 1769 1.1 mrg if (gimple_assign_single_p (stmt)) 1770 1.1 mrg examine_assign_tm (state, gsi); 1771 1.1 mrg break; 1772 1.1 mrg 1773 1.1 mrg case GIMPLE_CALL: 1774 1.1 mrg examine_call_tm (state, gsi); 1775 1.1 mrg break; 1776 1.1 mrg 1777 1.1 mrg case GIMPLE_ASM: 1778 1.1 mrg *state |= GTMA_MAY_ENTER_IRREVOCABLE; 1779 1.1 mrg break; 1780 1.1 mrg 1781 1.1 mrg case GIMPLE_TRANSACTION: 1782 1.1 mrg lower_transaction (gsi, wi); 1783 1.1 mrg break; 1784 1.1 mrg 1785 1.1 mrg default: 1786 1.1 mrg *handled_ops_p = !gimple_has_substatements (stmt); 1787 1.1 mrg break; 1788 1.1 mrg } 1789 1.1 mrg 1790 1.1 mrg return NULL_TREE; 1791 1.1 mrg } 1792 1.1 mrg 1793 1.1 mrg /* Iterate through the statements in the sequence, lowering them all 1794 1.1 mrg as appropriate for being outside of a transaction. */ 1795 1.1 mrg 1796 1.1 mrg static tree 1797 1.1 mrg lower_sequence_no_tm (gimple_stmt_iterator *gsi, bool *handled_ops_p, 1798 1.1 mrg struct walk_stmt_info * wi) 1799 1.1 mrg { 1800 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 1801 1.1 mrg 1802 1.1 mrg if (gimple_code (stmt) == GIMPLE_TRANSACTION) 1803 1.1 mrg { 1804 1.1 mrg *handled_ops_p = true; 1805 1.1 mrg lower_transaction (gsi, wi); 1806 1.1 mrg } 1807 1.1 mrg else 1808 1.1 mrg *handled_ops_p = !gimple_has_substatements (stmt); 1809 1.1 mrg 1810 1.1 mrg return NULL_TREE; 1811 1.1 mrg } 1812 1.1 mrg 1813 1.1 mrg /* Main entry point for flattening GIMPLE_TRANSACTION constructs. After 1814 1.1 mrg this, GIMPLE_TRANSACTION nodes still exist, but the nested body has 1815 1.1 mrg been moved out, and all the data required for constructing a proper 1816 1.1 mrg CFG has been recorded. */ 1817 1.1 mrg 1818 1.1 mrg static unsigned int 1819 1.1 mrg execute_lower_tm (void) 1820 1.1 mrg { 1821 1.1 mrg struct walk_stmt_info wi; 1822 1.1 mrg gimple_seq body; 1823 1.1 mrg 1824 1.1 mrg /* Transactional clones aren't created until a later pass. */ 1825 1.1 mrg gcc_assert (!decl_is_tm_clone (current_function_decl)); 1826 1.1 mrg 1827 1.1 mrg body = gimple_body (current_function_decl); 1828 1.1 mrg memset (&wi, 0, sizeof (wi)); 1829 1.1 mrg walk_gimple_seq_mod (&body, lower_sequence_no_tm, NULL, &wi); 1830 1.1 mrg gimple_set_body (current_function_decl, body); 1831 1.1 mrg 1832 1.1 mrg return 0; 1833 1.1 mrg } 1834 1.1 mrg 1835 1.1 mrg namespace { 1836 1.1 mrg 1837 1.1 mrg const pass_data pass_data_lower_tm = 1838 1.1 mrg { 1839 1.1 mrg GIMPLE_PASS, /* type */ 1840 1.1 mrg "tmlower", /* name */ 1841 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 1842 1.1 mrg TV_TRANS_MEM, /* tv_id */ 1843 1.1 mrg PROP_gimple_lcf, /* properties_required */ 1844 1.1 mrg 0, /* properties_provided */ 1845 1.1 mrg 0, /* properties_destroyed */ 1846 1.1 mrg 0, /* todo_flags_start */ 1847 1.1 mrg 0, /* todo_flags_finish */ 1848 1.1 mrg }; 1849 1.1 mrg 1850 1.1 mrg class pass_lower_tm : public gimple_opt_pass 1851 1.1 mrg { 1852 1.1 mrg public: 1853 1.1 mrg pass_lower_tm (gcc::context *ctxt) 1854 1.1 mrg : gimple_opt_pass (pass_data_lower_tm, ctxt) 1855 1.1 mrg {} 1856 1.1 mrg 1857 1.1 mrg /* opt_pass methods: */ 1858 1.1 mrg virtual bool gate (function *) { return flag_tm; } 1859 1.1 mrg virtual unsigned int execute (function *) { return execute_lower_tm (); } 1860 1.1 mrg 1861 1.1 mrg }; // class pass_lower_tm 1862 1.1 mrg 1863 1.1 mrg } // anon namespace 1864 1.1 mrg 1865 1.1 mrg gimple_opt_pass * 1866 1.1 mrg make_pass_lower_tm (gcc::context *ctxt) 1867 1.1 mrg { 1868 1.1 mrg return new pass_lower_tm (ctxt); 1869 1.1 mrg } 1870 1.1 mrg 1871 1.1 mrg /* Collect region information for each transaction. */ 1873 1.1 mrg 1874 1.1 mrg struct tm_region 1875 1.1 mrg { 1876 1.1 mrg public: 1877 1.1 mrg 1878 1.1 mrg /* The field "transaction_stmt" is initially a gtransaction *, 1879 1.1 mrg but eventually gets lowered to a gcall *(to BUILT_IN_TM_START). 1880 1.1 mrg 1881 1.1 mrg Helper method to get it as a gtransaction *, with code-checking 1882 1.1 mrg in a checked-build. */ 1883 1.1 mrg 1884 1.1 mrg gtransaction * 1885 1.1 mrg get_transaction_stmt () const 1886 1.1 mrg { 1887 1.1 mrg return as_a <gtransaction *> (transaction_stmt); 1888 1.1 mrg } 1889 1.1 mrg 1890 1.1 mrg public: 1891 1.1 mrg 1892 1.1 mrg /* Link to the next unnested transaction. */ 1893 1.1 mrg struct tm_region *next; 1894 1.1 mrg 1895 1.1 mrg /* Link to the next inner transaction. */ 1896 1.1 mrg struct tm_region *inner; 1897 1.1 mrg 1898 1.1 mrg /* Link to the next outer transaction. */ 1899 1.1 mrg struct tm_region *outer; 1900 1.1 mrg 1901 1.1 mrg /* The GIMPLE_TRANSACTION statement beginning this transaction. 1902 1.1 mrg After TM_MARK, this gets replaced by a call to 1903 1.1 mrg BUILT_IN_TM_START. 1904 1.1 mrg Hence this will be either a gtransaction *or a gcall *. */ 1905 1.1 mrg gimple *transaction_stmt; 1906 1.1 mrg 1907 1.1 mrg /* After TM_MARK expands the GIMPLE_TRANSACTION into a call to 1908 1.1 mrg BUILT_IN_TM_START, this field is true if the transaction is an 1909 1.1 mrg outer transaction. */ 1910 1.1 mrg bool original_transaction_was_outer; 1911 1.1 mrg 1912 1.1 mrg /* Return value from BUILT_IN_TM_START. */ 1913 1.1 mrg tree tm_state; 1914 1.1 mrg 1915 1.1 mrg /* The entry block to this region. This will always be the first 1916 1.1 mrg block of the body of the transaction. */ 1917 1.1 mrg basic_block entry_block; 1918 1.1 mrg 1919 1.1 mrg /* The first block after an expanded call to _ITM_beginTransaction. */ 1920 1.1 mrg basic_block restart_block; 1921 1.1 mrg 1922 1.1 mrg /* The set of all blocks that end the region; NULL if only EXIT_BLOCK. 1923 1.1 mrg These blocks are still a part of the region (i.e., the border is 1924 1.1 mrg inclusive). Note that this set is only complete for paths in the CFG 1925 1.1 mrg starting at ENTRY_BLOCK, and that there is no exit block recorded for 1926 1.1 mrg the edge to the "over" label. */ 1927 1.1 mrg bitmap exit_blocks; 1928 1.1 mrg 1929 1.1 mrg /* The set of all blocks that have an TM_IRREVOCABLE call. */ 1930 1.1 mrg bitmap irr_blocks; 1931 1.1 mrg }; 1932 1.1 mrg 1933 1.1 mrg /* True if there are pending edge statements to be committed for the 1934 1.1 mrg current function being scanned in the tmmark pass. */ 1935 1.1 mrg bool pending_edge_inserts_p; 1936 1.1 mrg 1937 1.1 mrg static struct tm_region *all_tm_regions; 1938 1.1 mrg static bitmap_obstack tm_obstack; 1939 1.1 mrg 1940 1.1 mrg 1941 1.1 mrg /* A subroutine of tm_region_init. Record the existence of the 1942 1.1 mrg GIMPLE_TRANSACTION statement in a tree of tm_region elements. */ 1943 1.1 mrg 1944 1.1 mrg static struct tm_region * 1945 1.1 mrg tm_region_init_0 (struct tm_region *outer, basic_block bb, 1946 1.1 mrg gtransaction *stmt) 1947 1.1 mrg { 1948 1.1 mrg struct tm_region *region; 1949 1.1 mrg 1950 1.1 mrg region = (struct tm_region *) 1951 1.1 mrg obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region)); 1952 1.1 mrg 1953 1.1 mrg if (outer) 1954 1.1 mrg { 1955 1.1 mrg region->next = outer->inner; 1956 1.1 mrg outer->inner = region; 1957 1.1 mrg } 1958 1.1 mrg else 1959 1.1 mrg { 1960 1.1 mrg region->next = all_tm_regions; 1961 1.1 mrg all_tm_regions = region; 1962 1.1 mrg } 1963 1.1 mrg region->inner = NULL; 1964 1.1 mrg region->outer = outer; 1965 1.1 mrg 1966 1.1 mrg region->transaction_stmt = stmt; 1967 1.1 mrg region->original_transaction_was_outer = false; 1968 1.1 mrg region->tm_state = NULL; 1969 1.1 mrg 1970 1.1 mrg /* There are either one or two edges out of the block containing 1971 1.1 mrg the GIMPLE_TRANSACTION, one to the actual region and one to the 1972 1.1 mrg "over" label if the region contains an abort. The former will 1973 1.1 mrg always be the one marked FALLTHRU. */ 1974 1.1 mrg region->entry_block = FALLTHRU_EDGE (bb)->dest; 1975 1.1 mrg 1976 1.1 mrg region->exit_blocks = BITMAP_ALLOC (&tm_obstack); 1977 1.1 mrg region->irr_blocks = BITMAP_ALLOC (&tm_obstack); 1978 1.1 mrg 1979 1.1 mrg return region; 1980 1.1 mrg } 1981 1.1 mrg 1982 1.1 mrg /* A subroutine of tm_region_init. Record all the exit and 1983 1.1 mrg irrevocable blocks in BB into the region's exit_blocks and 1984 1.1 mrg irr_blocks bitmaps. Returns the new region being scanned. */ 1985 1.1 mrg 1986 1.1 mrg static struct tm_region * 1987 1.1 mrg tm_region_init_1 (struct tm_region *region, basic_block bb) 1988 1.1 mrg { 1989 1.1 mrg gimple_stmt_iterator gsi; 1990 1.1 mrg gimple *g; 1991 1.1 mrg 1992 1.1 mrg if (!region 1993 1.1 mrg || (!region->irr_blocks && !region->exit_blocks)) 1994 1.1 mrg return region; 1995 1.1 mrg 1996 1.1 mrg /* Check to see if this is the end of a region by seeing if it 1997 1.1 mrg contains a call to __builtin_tm_commit{,_eh}. Note that the 1998 1.1 mrg outermost region for DECL_IS_TM_CLONE need not collect this. */ 1999 1.1 mrg for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi)) 2000 1.1 mrg { 2001 1.1 mrg g = gsi_stmt (gsi); 2002 1.1 mrg if (gimple_code (g) == GIMPLE_CALL) 2003 1.1 mrg { 2004 1.1 mrg tree fn = gimple_call_fndecl (g); 2005 1.1 mrg if (fn && fndecl_built_in_p (fn, BUILT_IN_NORMAL)) 2006 1.1 mrg { 2007 1.1 mrg if ((DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT 2008 1.1 mrg || DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_COMMIT_EH) 2009 1.1 mrg && region->exit_blocks) 2010 1.1 mrg { 2011 1.1 mrg bitmap_set_bit (region->exit_blocks, bb->index); 2012 1.1 mrg region = region->outer; 2013 1.1 mrg break; 2014 1.1 mrg } 2015 1.1 mrg if (DECL_FUNCTION_CODE (fn) == BUILT_IN_TM_IRREVOCABLE) 2016 1.1 mrg bitmap_set_bit (region->irr_blocks, bb->index); 2017 1.1 mrg } 2018 1.1 mrg } 2019 1.1 mrg } 2020 1.1 mrg return region; 2021 1.1 mrg } 2022 1.1 mrg 2023 1.1 mrg /* Collect all of the transaction regions within the current function 2024 1.1 mrg and record them in ALL_TM_REGIONS. The REGION parameter may specify 2025 1.1 mrg an "outermost" region for use by tm clones. */ 2026 1.1 mrg 2027 1.1 mrg static void 2028 1.1 mrg tm_region_init (struct tm_region *region) 2029 1.1 mrg { 2030 1.1 mrg gimple *g; 2031 1.1 mrg edge_iterator ei; 2032 1.1 mrg edge e; 2033 1.1 mrg basic_block bb; 2034 1.1 mrg auto_vec<basic_block> queue; 2035 1.1 mrg bitmap visited_blocks = BITMAP_ALLOC (NULL); 2036 1.1 mrg struct tm_region *old_region; 2037 1.1 mrg auto_vec<tm_region *> bb_regions; 2038 1.1 mrg 2039 1.1 mrg /* We could store this information in bb->aux, but we may get called 2040 1.1 mrg through get_all_tm_blocks() from another pass that may be already 2041 1.1 mrg using bb->aux. */ 2042 1.1 mrg bb_regions.safe_grow_cleared (last_basic_block_for_fn (cfun), true); 2043 1.1 mrg 2044 1.1 mrg all_tm_regions = region; 2045 1.1 mrg bb = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)); 2046 1.1 mrg queue.safe_push (bb); 2047 1.1 mrg bitmap_set_bit (visited_blocks, bb->index); 2048 1.1 mrg bb_regions[bb->index] = region; 2049 1.1 mrg 2050 1.1 mrg do 2051 1.1 mrg { 2052 1.1 mrg bb = queue.pop (); 2053 1.1 mrg region = bb_regions[bb->index]; 2054 1.1 mrg bb_regions[bb->index] = NULL; 2055 1.1 mrg 2056 1.1 mrg /* Record exit and irrevocable blocks. */ 2057 1.1 mrg region = tm_region_init_1 (region, bb); 2058 1.1 mrg 2059 1.1 mrg /* Check for the last statement in the block beginning a new region. */ 2060 1.1 mrg g = last_stmt (bb); 2061 1.1 mrg old_region = region; 2062 1.1 mrg if (g) 2063 1.1 mrg if (gtransaction *trans_stmt = dyn_cast <gtransaction *> (g)) 2064 1.1 mrg region = tm_region_init_0 (region, bb, trans_stmt); 2065 1.1 mrg 2066 1.1 mrg /* Process subsequent blocks. */ 2067 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 2068 1.1 mrg if (!bitmap_bit_p (visited_blocks, e->dest->index)) 2069 1.1 mrg { 2070 1.1 mrg bitmap_set_bit (visited_blocks, e->dest->index); 2071 1.1 mrg queue.safe_push (e->dest); 2072 1.1 mrg 2073 1.1 mrg /* If the current block started a new region, make sure that only 2074 1.1 mrg the entry block of the new region is associated with this region. 2075 1.1 mrg Other successors are still part of the old region. */ 2076 1.1 mrg if (old_region != region && e->dest != region->entry_block) 2077 1.1 mrg bb_regions[e->dest->index] = old_region; 2078 1.1 mrg else 2079 1.1 mrg bb_regions[e->dest->index] = region; 2080 1.1 mrg } 2081 1.1 mrg } 2082 1.1 mrg while (!queue.is_empty ()); 2083 1.1 mrg BITMAP_FREE (visited_blocks); 2084 1.1 mrg } 2085 1.1 mrg 2086 1.1 mrg /* The "gate" function for all transactional memory expansion and optimization 2087 1.1 mrg passes. We collect region information for each top-level transaction, and 2088 1.1 mrg if we don't find any, we skip all of the TM passes. Each region will have 2089 1.1 mrg all of the exit blocks recorded, and the originating statement. */ 2090 1.1 mrg 2091 1.1 mrg static bool 2092 1.1 mrg gate_tm_init (void) 2093 1.1 mrg { 2094 1.1 mrg if (!flag_tm) 2095 1.1 mrg return false; 2096 1.1 mrg 2097 1.1 mrg calculate_dominance_info (CDI_DOMINATORS); 2098 1.1 mrg bitmap_obstack_initialize (&tm_obstack); 2099 1.1 mrg 2100 1.1 mrg /* If the function is a TM_CLONE, then the entire function is the region. */ 2101 1.1 mrg if (decl_is_tm_clone (current_function_decl)) 2102 1.1 mrg { 2103 1.1 mrg struct tm_region *region = (struct tm_region *) 2104 1.1 mrg obstack_alloc (&tm_obstack.obstack, sizeof (struct tm_region)); 2105 1.1 mrg memset (region, 0, sizeof (*region)); 2106 1.1 mrg region->entry_block = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)); 2107 1.1 mrg /* For a clone, the entire function is the region. But even if 2108 1.1 mrg we don't need to record any exit blocks, we may need to 2109 1.1 mrg record irrevocable blocks. */ 2110 1.1 mrg region->irr_blocks = BITMAP_ALLOC (&tm_obstack); 2111 1.1 mrg 2112 1.1 mrg tm_region_init (region); 2113 1.1 mrg } 2114 1.1 mrg else 2115 1.1 mrg { 2116 1.1 mrg tm_region_init (NULL); 2117 1.1 mrg 2118 1.1 mrg /* If we didn't find any regions, cleanup and skip the whole tree 2119 1.1 mrg of tm-related optimizations. */ 2120 1.1 mrg if (all_tm_regions == NULL) 2121 1.1 mrg { 2122 1.1 mrg bitmap_obstack_release (&tm_obstack); 2123 1.1 mrg return false; 2124 1.1 mrg } 2125 1.1 mrg } 2126 1.1 mrg 2127 1.1 mrg return true; 2128 1.1 mrg } 2129 1.1 mrg 2130 1.1 mrg namespace { 2131 1.1 mrg 2132 1.1 mrg const pass_data pass_data_tm_init = 2133 1.1 mrg { 2134 1.1 mrg GIMPLE_PASS, /* type */ 2135 1.1 mrg "*tminit", /* name */ 2136 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 2137 1.1 mrg TV_TRANS_MEM, /* tv_id */ 2138 1.1 mrg ( PROP_ssa | PROP_cfg ), /* properties_required */ 2139 1.1 mrg 0, /* properties_provided */ 2140 1.1 mrg 0, /* properties_destroyed */ 2141 1.1 mrg 0, /* todo_flags_start */ 2142 1.1 mrg 0, /* todo_flags_finish */ 2143 1.1 mrg }; 2144 1.1 mrg 2145 1.1 mrg class pass_tm_init : public gimple_opt_pass 2146 1.1 mrg { 2147 1.1 mrg public: 2148 1.1 mrg pass_tm_init (gcc::context *ctxt) 2149 1.1 mrg : gimple_opt_pass (pass_data_tm_init, ctxt) 2150 1.1 mrg {} 2151 1.1 mrg 2152 1.1 mrg /* opt_pass methods: */ 2153 1.1 mrg virtual bool gate (function *) { return gate_tm_init (); } 2154 1.1 mrg 2155 1.1 mrg }; // class pass_tm_init 2156 1.1 mrg 2157 1.1 mrg } // anon namespace 2158 1.1 mrg 2159 1.1 mrg gimple_opt_pass * 2160 1.1 mrg make_pass_tm_init (gcc::context *ctxt) 2161 1.1 mrg { 2162 1.1 mrg return new pass_tm_init (ctxt); 2163 1.1 mrg } 2164 1.1 mrg 2165 1.1 mrg /* Add FLAGS to the GIMPLE_TRANSACTION subcode for the transaction region 2167 1.1 mrg represented by STATE. */ 2168 1.1 mrg 2169 1.1 mrg static inline void 2170 1.1 mrg transaction_subcode_ior (struct tm_region *region, unsigned flags) 2171 1.1 mrg { 2172 1.1 mrg if (region && region->transaction_stmt) 2173 1.1 mrg { 2174 1.1 mrg gtransaction *transaction_stmt = region->get_transaction_stmt (); 2175 1.1 mrg flags |= gimple_transaction_subcode (transaction_stmt); 2176 1.1 mrg gimple_transaction_set_subcode (transaction_stmt, flags); 2177 1.1 mrg } 2178 1.1 mrg } 2179 1.1 mrg 2180 1.1 mrg /* Construct a memory load in a transactional context. Return the 2181 1.1 mrg gimple statement performing the load, or NULL if there is no 2182 1.1 mrg TM_LOAD builtin of the appropriate size to do the load. 2183 1.1 mrg 2184 1.1 mrg LOC is the location to use for the new statement(s). */ 2185 1.1 mrg 2186 1.1 mrg static gcall * 2187 1.1 mrg build_tm_load (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi) 2188 1.1 mrg { 2189 1.1 mrg tree t, type = TREE_TYPE (rhs); 2190 1.1 mrg gcall *gcall; 2191 1.1 mrg 2192 1.1 mrg built_in_function code; 2193 1.1 mrg if (type == float_type_node) 2194 1.1 mrg code = BUILT_IN_TM_LOAD_FLOAT; 2195 1.1 mrg else if (type == double_type_node) 2196 1.1 mrg code = BUILT_IN_TM_LOAD_DOUBLE; 2197 1.1 mrg else if (type == long_double_type_node) 2198 1.1 mrg code = BUILT_IN_TM_LOAD_LDOUBLE; 2199 1.1 mrg else 2200 1.1 mrg { 2201 1.1 mrg if (TYPE_SIZE (type) == NULL || !tree_fits_uhwi_p (TYPE_SIZE (type))) 2202 1.1 mrg return NULL; 2203 1.1 mrg unsigned HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type)); 2204 1.1 mrg 2205 1.1 mrg if (TREE_CODE (type) == VECTOR_TYPE) 2206 1.1 mrg { 2207 1.1 mrg switch (type_size) 2208 1.1 mrg { 2209 1.1 mrg case 64: 2210 1.1 mrg code = BUILT_IN_TM_LOAD_M64; 2211 1.1 mrg break; 2212 1.1 mrg case 128: 2213 1.1 mrg code = BUILT_IN_TM_LOAD_M128; 2214 1.1 mrg break; 2215 1.1 mrg case 256: 2216 1.1 mrg code = BUILT_IN_TM_LOAD_M256; 2217 1.1 mrg break; 2218 1.1 mrg default: 2219 1.1 mrg goto unhandled_vec; 2220 1.1 mrg } 2221 1.1 mrg if (!builtin_decl_explicit_p (code)) 2222 1.1 mrg goto unhandled_vec; 2223 1.1 mrg } 2224 1.1 mrg else 2225 1.1 mrg { 2226 1.1 mrg unhandled_vec: 2227 1.1 mrg switch (type_size) 2228 1.1 mrg { 2229 1.1 mrg case 8: 2230 1.1 mrg code = BUILT_IN_TM_LOAD_1; 2231 1.1 mrg break; 2232 1.1 mrg case 16: 2233 1.1 mrg code = BUILT_IN_TM_LOAD_2; 2234 1.1 mrg break; 2235 1.1 mrg case 32: 2236 1.1 mrg code = BUILT_IN_TM_LOAD_4; 2237 1.1 mrg break; 2238 1.1 mrg case 64: 2239 1.1 mrg code = BUILT_IN_TM_LOAD_8; 2240 1.1 mrg break; 2241 1.1 mrg default: 2242 1.1 mrg return NULL; 2243 1.1 mrg } 2244 1.1 mrg } 2245 1.1 mrg } 2246 1.1 mrg 2247 1.1 mrg tree decl = builtin_decl_explicit (code); 2248 1.1 mrg gcc_assert (decl); 2249 1.1 mrg 2250 1.1 mrg t = gimplify_addr (gsi, rhs); 2251 1.1 mrg gcall = gimple_build_call (decl, 1, t); 2252 1.1 mrg gimple_set_location (gcall, loc); 2253 1.1 mrg 2254 1.1 mrg t = TREE_TYPE (TREE_TYPE (decl)); 2255 1.1 mrg if (useless_type_conversion_p (type, t)) 2256 1.1 mrg { 2257 1.1 mrg gimple_call_set_lhs (gcall, lhs); 2258 1.1 mrg gsi_insert_before (gsi, gcall, GSI_SAME_STMT); 2259 1.1 mrg } 2260 1.1 mrg else 2261 1.1 mrg { 2262 1.1 mrg gimple *g; 2263 1.1 mrg tree temp; 2264 1.1 mrg 2265 1.1 mrg temp = create_tmp_reg (t); 2266 1.1 mrg gimple_call_set_lhs (gcall, temp); 2267 1.1 mrg gsi_insert_before (gsi, gcall, GSI_SAME_STMT); 2268 1.1 mrg 2269 1.1 mrg t = fold_build1 (VIEW_CONVERT_EXPR, type, temp); 2270 1.1 mrg g = gimple_build_assign (lhs, t); 2271 1.1 mrg gsi_insert_before (gsi, g, GSI_SAME_STMT); 2272 1.1 mrg } 2273 1.1 mrg 2274 1.1 mrg return gcall; 2275 1.1 mrg } 2276 1.1 mrg 2277 1.1 mrg 2278 1.1 mrg /* Similarly for storing TYPE in a transactional context. */ 2279 1.1 mrg 2280 1.1 mrg static gcall * 2281 1.1 mrg build_tm_store (location_t loc, tree lhs, tree rhs, gimple_stmt_iterator *gsi) 2282 1.1 mrg { 2283 1.1 mrg tree t, fn, type = TREE_TYPE (rhs), simple_type; 2284 1.1 mrg gcall *gcall; 2285 1.1 mrg 2286 1.1 mrg built_in_function code; 2287 1.1 mrg if (type == float_type_node) 2288 1.1 mrg code = BUILT_IN_TM_STORE_FLOAT; 2289 1.1 mrg else if (type == double_type_node) 2290 1.1 mrg code = BUILT_IN_TM_STORE_DOUBLE; 2291 1.1 mrg else if (type == long_double_type_node) 2292 1.1 mrg code = BUILT_IN_TM_STORE_LDOUBLE; 2293 1.1 mrg else 2294 1.1 mrg { 2295 1.1 mrg if (TYPE_SIZE (type) == NULL || !tree_fits_uhwi_p (TYPE_SIZE (type))) 2296 1.1 mrg return NULL; 2297 1.1 mrg unsigned HOST_WIDE_INT type_size = tree_to_uhwi (TYPE_SIZE (type)); 2298 1.1 mrg 2299 1.1 mrg if (TREE_CODE (type) == VECTOR_TYPE) 2300 1.1 mrg { 2301 1.1 mrg switch (type_size) 2302 1.1 mrg { 2303 1.1 mrg case 64: 2304 1.1 mrg code = BUILT_IN_TM_STORE_M64; 2305 1.1 mrg break; 2306 1.1 mrg case 128: 2307 1.1 mrg code = BUILT_IN_TM_STORE_M128; 2308 1.1 mrg break; 2309 1.1 mrg case 256: 2310 1.1 mrg code = BUILT_IN_TM_STORE_M256; 2311 1.1 mrg break; 2312 1.1 mrg default: 2313 1.1 mrg goto unhandled_vec; 2314 1.1 mrg } 2315 1.1 mrg if (!builtin_decl_explicit_p (code)) 2316 1.1 mrg goto unhandled_vec; 2317 1.1 mrg } 2318 1.1 mrg else 2319 1.1 mrg { 2320 1.1 mrg unhandled_vec: 2321 1.1 mrg switch (type_size) 2322 1.1 mrg { 2323 1.1 mrg case 8: 2324 1.1 mrg code = BUILT_IN_TM_STORE_1; 2325 1.1 mrg break; 2326 1.1 mrg case 16: 2327 1.1 mrg code = BUILT_IN_TM_STORE_2; 2328 1.1 mrg break; 2329 1.1 mrg case 32: 2330 1.1 mrg code = BUILT_IN_TM_STORE_4; 2331 1.1 mrg break; 2332 1.1 mrg case 64: 2333 1.1 mrg code = BUILT_IN_TM_STORE_8; 2334 1.1 mrg break; 2335 1.1 mrg default: 2336 1.1 mrg return NULL; 2337 1.1 mrg } 2338 1.1 mrg } 2339 1.1 mrg } 2340 1.1 mrg 2341 1.1 mrg fn = builtin_decl_explicit (code); 2342 1.1 mrg gcc_assert (fn); 2343 1.1 mrg 2344 1.1 mrg simple_type = TREE_VALUE (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)))); 2345 1.1 mrg 2346 1.1 mrg if (TREE_CODE (rhs) == CONSTRUCTOR) 2347 1.1 mrg { 2348 1.1 mrg /* Handle the easy initialization to zero. */ 2349 1.1 mrg if (!CONSTRUCTOR_ELTS (rhs)) 2350 1.1 mrg rhs = build_int_cst (simple_type, 0); 2351 1.1 mrg else 2352 1.1 mrg { 2353 1.1 mrg /* ...otherwise punt to the caller and probably use 2354 1.1 mrg BUILT_IN_TM_MEMMOVE, because we can't wrap a 2355 1.1 mrg VIEW_CONVERT_EXPR around a CONSTRUCTOR (below) and produce 2356 1.1 mrg valid gimple. */ 2357 1.1 mrg return NULL; 2358 1.1 mrg } 2359 1.1 mrg } 2360 1.1 mrg else if (!useless_type_conversion_p (simple_type, type)) 2361 1.1 mrg { 2362 1.1 mrg gimple *g; 2363 1.1 mrg tree temp; 2364 1.1 mrg 2365 1.1 mrg temp = create_tmp_reg (simple_type); 2366 1.1 mrg t = fold_build1 (VIEW_CONVERT_EXPR, simple_type, rhs); 2367 1.1 mrg g = gimple_build_assign (temp, t); 2368 1.1 mrg gimple_set_location (g, loc); 2369 1.1 mrg gsi_insert_before (gsi, g, GSI_SAME_STMT); 2370 1.1 mrg 2371 1.1 mrg rhs = temp; 2372 1.1 mrg } 2373 1.1 mrg 2374 1.1 mrg t = gimplify_addr (gsi, lhs); 2375 1.1 mrg gcall = gimple_build_call (fn, 2, t, rhs); 2376 1.1 mrg gimple_set_location (gcall, loc); 2377 1.1 mrg gsi_insert_before (gsi, gcall, GSI_SAME_STMT); 2378 1.1 mrg 2379 1.1 mrg return gcall; 2380 1.1 mrg } 2381 1.1 mrg 2382 1.1 mrg 2383 1.1 mrg /* Expand an assignment statement into transactional builtins. */ 2384 1.1 mrg 2385 1.1 mrg static void 2386 1.1 mrg expand_assign_tm (struct tm_region *region, gimple_stmt_iterator *gsi) 2387 1.1 mrg { 2388 1.1 mrg gimple *stmt = gsi_stmt (*gsi); 2389 1.1 mrg location_t loc = gimple_location (stmt); 2390 1.1 mrg tree lhs = gimple_assign_lhs (stmt); 2391 1.1 mrg tree rhs = gimple_assign_rhs1 (stmt); 2392 1.1 mrg bool store_p = requires_barrier (region->entry_block, lhs, NULL); 2393 1.1 mrg bool load_p = requires_barrier (region->entry_block, rhs, NULL); 2394 1.1 mrg gimple *gcall = NULL; 2395 1.1 mrg 2396 1.1 mrg if (!load_p && !store_p) 2397 1.1 mrg { 2398 1.1 mrg /* Add thread private addresses to log if applicable. */ 2399 1.1 mrg requires_barrier (region->entry_block, lhs, stmt); 2400 1.1 mrg gsi_next (gsi); 2401 1.1 mrg return; 2402 1.1 mrg } 2403 1.1 mrg 2404 1.1 mrg if (load_p) 2405 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_LOAD); 2406 1.1 mrg if (store_p) 2407 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_STORE); 2408 1.1 mrg 2409 1.1 mrg // Remove original load/store statement. 2410 1.1 mrg gsi_remove (gsi, true); 2411 1.1 mrg 2412 1.1 mrg // Attempt to use a simple load/store helper function. 2413 1.1 mrg if (load_p && !store_p) 2414 1.1 mrg gcall = build_tm_load (loc, lhs, rhs, gsi); 2415 1.1 mrg else if (store_p && !load_p) 2416 1.1 mrg gcall = build_tm_store (loc, lhs, rhs, gsi); 2417 1.1 mrg 2418 1.1 mrg // If gcall has not been set, then we do not have a simple helper 2419 1.1 mrg // function available for the type. This may be true of larger 2420 1.1 mrg // structures, vectors, and non-standard float types. 2421 1.1 mrg if (!gcall) 2422 1.1 mrg { 2423 1.1 mrg tree lhs_addr, rhs_addr, ltmp = NULL, copy_fn; 2424 1.1 mrg 2425 1.1 mrg // If this is a type that we couldn't handle above, but it's 2426 1.1 mrg // in a register, we must spill it to memory for the copy. 2427 1.1 mrg if (is_gimple_reg (lhs)) 2428 1.1 mrg { 2429 1.1 mrg ltmp = create_tmp_var (TREE_TYPE (lhs)); 2430 1.1 mrg lhs_addr = build_fold_addr_expr (ltmp); 2431 1.1 mrg } 2432 1.1 mrg else 2433 1.1 mrg lhs_addr = gimplify_addr (gsi, lhs); 2434 1.1 mrg if (is_gimple_reg (rhs)) 2435 1.1 mrg { 2436 1.1 mrg tree rtmp = create_tmp_var (TREE_TYPE (rhs)); 2437 1.1 mrg TREE_ADDRESSABLE (rtmp) = 1; 2438 1.1 mrg rhs_addr = build_fold_addr_expr (rtmp); 2439 1.1 mrg gcall = gimple_build_assign (rtmp, rhs); 2440 1.1 mrg gsi_insert_before (gsi, gcall, GSI_SAME_STMT); 2441 1.1 mrg } 2442 1.1 mrg else 2443 1.1 mrg rhs_addr = gimplify_addr (gsi, rhs); 2444 1.1 mrg 2445 1.1 mrg // Choose the appropriate memory transfer function. 2446 1.1 mrg if (load_p && store_p) 2447 1.1 mrg { 2448 1.1 mrg // ??? Figure out if there's any possible overlap between 2449 1.1 mrg // the LHS and the RHS and if not, use MEMCPY. 2450 1.1 mrg copy_fn = builtin_decl_explicit (BUILT_IN_TM_MEMMOVE); 2451 1.1 mrg } 2452 1.1 mrg else if (load_p) 2453 1.1 mrg { 2454 1.1 mrg // Note that the store is non-transactional and cannot overlap. 2455 1.1 mrg copy_fn = builtin_decl_explicit (BUILT_IN_TM_MEMCPY_RTWN); 2456 1.1 mrg } 2457 1.1 mrg else 2458 1.1 mrg { 2459 1.1 mrg // Note that the load is non-transactional and cannot overlap. 2460 1.1 mrg copy_fn = builtin_decl_explicit (BUILT_IN_TM_MEMCPY_RNWT); 2461 1.1 mrg } 2462 1.1 mrg 2463 1.1 mrg gcall = gimple_build_call (copy_fn, 3, lhs_addr, rhs_addr, 2464 1.1 mrg TYPE_SIZE_UNIT (TREE_TYPE (lhs))); 2465 1.1 mrg gimple_set_location (gcall, loc); 2466 1.1 mrg gsi_insert_before (gsi, gcall, GSI_SAME_STMT); 2467 1.1 mrg 2468 1.1 mrg if (ltmp) 2469 1.1 mrg { 2470 1.1 mrg gcall = gimple_build_assign (lhs, ltmp); 2471 1.1 mrg gsi_insert_before (gsi, gcall, GSI_SAME_STMT); 2472 1.1 mrg } 2473 1.1 mrg } 2474 1.1 mrg 2475 1.1 mrg // Now that we have the load/store in its instrumented form, add 2476 1.1 mrg // thread private addresses to the log if applicable. 2477 1.1 mrg if (!store_p) 2478 1.1 mrg requires_barrier (region->entry_block, lhs, gcall); 2479 1.1 mrg } 2480 1.1 mrg 2481 1.1 mrg 2482 1.1 mrg /* Expand a call statement as appropriate for a transaction. That is, 2483 1.1 mrg either verify that the call does not affect the transaction, or 2484 1.1 mrg redirect the call to a clone that handles transactions, or change 2485 1.1 mrg the transaction state to IRREVOCABLE. Return true if the call is 2486 1.1 mrg one of the builtins that end a transaction. */ 2487 1.1 mrg 2488 1.1 mrg static bool 2489 1.1 mrg expand_call_tm (struct tm_region *region, 2490 1.1 mrg gimple_stmt_iterator *gsi) 2491 1.1 mrg { 2492 1.1 mrg gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); 2493 1.1 mrg tree lhs = gimple_call_lhs (stmt); 2494 1.1 mrg tree fn_decl; 2495 1.1 mrg struct cgraph_node *node; 2496 1.1 mrg bool retval = false; 2497 1.1 mrg 2498 1.1 mrg fn_decl = gimple_call_fndecl (stmt); 2499 1.1 mrg 2500 1.1 mrg if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMCPY) 2501 1.1 mrg || fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMMOVE)) 2502 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_STORE | GTMA_HAVE_LOAD); 2503 1.1 mrg if (fn_decl == builtin_decl_explicit (BUILT_IN_TM_MEMSET)) 2504 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_STORE); 2505 1.1 mrg 2506 1.1 mrg if (is_tm_pure_call (stmt)) 2507 1.1 mrg return false; 2508 1.1 mrg 2509 1.1 mrg if (fn_decl) 2510 1.1 mrg retval = is_tm_ending_fndecl (fn_decl); 2511 1.1 mrg if (!retval) 2512 1.1 mrg { 2513 1.1 mrg /* Assume all non-const/pure calls write to memory, except 2514 1.1 mrg transaction ending builtins. */ 2515 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_STORE); 2516 1.1 mrg } 2517 1.1 mrg 2518 1.1 mrg /* For indirect calls, we already generated a call into the runtime. */ 2519 1.1 mrg if (!fn_decl) 2520 1.1 mrg { 2521 1.1 mrg tree fn = gimple_call_fn (stmt); 2522 1.1 mrg 2523 1.1 mrg /* We are guaranteed never to go irrevocable on a safe or pure 2524 1.1 mrg call, and the pure call was handled above. */ 2525 1.1 mrg if (is_tm_safe (fn)) 2526 1.1 mrg return false; 2527 1.1 mrg else 2528 1.1 mrg transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE); 2529 1.1 mrg 2530 1.1 mrg return false; 2531 1.1 mrg } 2532 1.1 mrg 2533 1.1 mrg node = cgraph_node::get (fn_decl); 2534 1.1 mrg /* All calls should have cgraph here. */ 2535 1.1 mrg if (!node) 2536 1.1 mrg { 2537 1.1 mrg /* We can have a nodeless call here if some pass after IPA-tm 2538 1.1 mrg added uninstrumented calls. For example, loop distribution 2539 1.1 mrg can transform certain loop constructs into __builtin_mem* 2540 1.1 mrg calls. In this case, see if we have a suitable TM 2541 1.1 mrg replacement and fill in the gaps. */ 2542 1.1 mrg gcc_assert (DECL_BUILT_IN_CLASS (fn_decl) == BUILT_IN_NORMAL); 2543 1.1 mrg enum built_in_function code = DECL_FUNCTION_CODE (fn_decl); 2544 1.1 mrg gcc_assert (code == BUILT_IN_MEMCPY 2545 1.1 mrg || code == BUILT_IN_MEMMOVE 2546 1.1 mrg || code == BUILT_IN_MEMSET); 2547 1.1 mrg 2548 1.1 mrg tree repl = find_tm_replacement_function (fn_decl); 2549 1.1 mrg if (repl) 2550 1.1 mrg { 2551 1.1 mrg gimple_call_set_fndecl (stmt, repl); 2552 1.1 mrg update_stmt (stmt); 2553 1.1 mrg node = cgraph_node::create (repl); 2554 1.1 mrg node->tm_may_enter_irr = false; 2555 1.1 mrg return expand_call_tm (region, gsi); 2556 1.1 mrg } 2557 1.1 mrg gcc_unreachable (); 2558 1.1 mrg } 2559 1.1 mrg if (node->tm_may_enter_irr) 2560 1.1 mrg transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE); 2561 1.1 mrg 2562 1.1 mrg if (is_tm_abort (fn_decl)) 2563 1.1 mrg { 2564 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_ABORT); 2565 1.1 mrg return true; 2566 1.1 mrg } 2567 1.1 mrg 2568 1.1 mrg /* Instrument the store if needed. 2569 1.1 mrg 2570 1.1 mrg If the assignment happens inside the function call (return slot 2571 1.1 mrg optimization), there is no instrumentation to be done, since 2572 1.1 mrg the callee should have done the right thing. */ 2573 1.1 mrg if (lhs && requires_barrier (region->entry_block, lhs, stmt) 2574 1.1 mrg && !gimple_call_return_slot_opt_p (stmt)) 2575 1.1 mrg { 2576 1.1 mrg tree tmp = create_tmp_reg (TREE_TYPE (lhs)); 2577 1.1 mrg location_t loc = gimple_location (stmt); 2578 1.1 mrg edge fallthru_edge = NULL; 2579 1.1 mrg gassign *assign_stmt; 2580 1.1 mrg 2581 1.1 mrg /* Remember if the call was going to throw. */ 2582 1.1 mrg if (stmt_can_throw_internal (cfun, stmt)) 2583 1.1 mrg { 2584 1.1 mrg edge_iterator ei; 2585 1.1 mrg edge e; 2586 1.1 mrg basic_block bb = gimple_bb (stmt); 2587 1.1 mrg 2588 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 2589 1.1 mrg if (e->flags & EDGE_FALLTHRU) 2590 1.1 mrg { 2591 1.1 mrg fallthru_edge = e; 2592 1.1 mrg break; 2593 1.1 mrg } 2594 1.1 mrg } 2595 1.1 mrg 2596 1.1 mrg gimple_call_set_lhs (stmt, tmp); 2597 1.1 mrg update_stmt (stmt); 2598 1.1 mrg assign_stmt = gimple_build_assign (lhs, tmp); 2599 1.1 mrg gimple_set_location (assign_stmt, loc); 2600 1.1 mrg 2601 1.1 mrg /* We cannot throw in the middle of a BB. If the call was going 2602 1.1 mrg to throw, place the instrumentation on the fallthru edge, so 2603 1.1 mrg the call remains the last statement in the block. */ 2604 1.1 mrg if (fallthru_edge) 2605 1.1 mrg { 2606 1.1 mrg gimple_seq fallthru_seq = gimple_seq_alloc_with_stmt (assign_stmt); 2607 1.1 mrg gimple_stmt_iterator fallthru_gsi = gsi_start (fallthru_seq); 2608 1.1 mrg expand_assign_tm (region, &fallthru_gsi); 2609 1.1 mrg gsi_insert_seq_on_edge (fallthru_edge, fallthru_seq); 2610 1.1 mrg pending_edge_inserts_p = true; 2611 1.1 mrg } 2612 1.1 mrg else 2613 1.1 mrg { 2614 1.1 mrg gsi_insert_after (gsi, assign_stmt, GSI_CONTINUE_LINKING); 2615 1.1 mrg expand_assign_tm (region, gsi); 2616 1.1 mrg } 2617 1.1 mrg 2618 1.1 mrg transaction_subcode_ior (region, GTMA_HAVE_STORE); 2619 1.1 mrg } 2620 1.1 mrg 2621 1.1 mrg return retval; 2622 1.1 mrg } 2623 1.1 mrg 2624 1.1 mrg 2625 1.1 mrg /* Expand all statements in BB as appropriate for being inside 2626 1.1 mrg a transaction. */ 2627 1.1 mrg 2628 1.1 mrg static void 2629 1.1 mrg expand_block_tm (struct tm_region *region, basic_block bb) 2630 1.1 mrg { 2631 1.1 mrg gimple_stmt_iterator gsi; 2632 1.1 mrg 2633 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); ) 2634 1.1 mrg { 2635 1.1 mrg gimple *stmt = gsi_stmt (gsi); 2636 1.1 mrg switch (gimple_code (stmt)) 2637 1.1 mrg { 2638 1.1 mrg case GIMPLE_ASSIGN: 2639 1.1 mrg /* Only memory reads/writes need to be instrumented. */ 2640 1.1 mrg if (gimple_assign_single_p (stmt) 2641 1.1 mrg && !gimple_clobber_p (stmt)) 2642 1.1 mrg { 2643 1.1 mrg expand_assign_tm (region, &gsi); 2644 1.1 mrg continue; 2645 1.1 mrg } 2646 1.1 mrg break; 2647 1.1 mrg 2648 1.1 mrg case GIMPLE_CALL: 2649 1.1 mrg if (expand_call_tm (region, &gsi)) 2650 1.1 mrg return; 2651 1.1 mrg break; 2652 1.1 mrg 2653 1.1 mrg case GIMPLE_ASM: 2654 1.1 mrg gcc_unreachable (); 2655 1.1 mrg 2656 1.1 mrg default: 2657 1.1 mrg break; 2658 1.1 mrg } 2659 1.1 mrg if (!gsi_end_p (gsi)) 2660 1.1 mrg gsi_next (&gsi); 2661 1.1 mrg } 2662 1.1 mrg } 2663 1.1 mrg 2664 1.1 mrg /* Return the list of basic-blocks in REGION. 2665 1.1 mrg 2666 1.1 mrg STOP_AT_IRREVOCABLE_P is true if caller is uninterested in blocks 2667 1.1 mrg following a TM_IRREVOCABLE call. 2668 1.1 mrg 2669 1.1 mrg INCLUDE_UNINSTRUMENTED_P is TRUE if we should include the 2670 1.1 mrg uninstrumented code path blocks in the list of basic blocks 2671 1.1 mrg returned, false otherwise. */ 2672 1.1 mrg 2673 1.1 mrg static vec<basic_block> 2674 1.1 mrg get_tm_region_blocks (basic_block entry_block, 2675 1.1 mrg bitmap exit_blocks, 2676 1.1 mrg bitmap irr_blocks, 2677 1.1 mrg bitmap all_region_blocks, 2678 1.1 mrg bool stop_at_irrevocable_p, 2679 1.1 mrg bool include_uninstrumented_p = true) 2680 1.1 mrg { 2681 1.1 mrg vec<basic_block> bbs = vNULL; 2682 1.1 mrg unsigned i; 2683 1.1 mrg edge e; 2684 1.1 mrg edge_iterator ei; 2685 1.1 mrg bitmap visited_blocks = BITMAP_ALLOC (NULL); 2686 1.1 mrg 2687 1.1 mrg i = 0; 2688 1.1 mrg bbs.safe_push (entry_block); 2689 1.1 mrg bitmap_set_bit (visited_blocks, entry_block->index); 2690 1.1 mrg 2691 1.1 mrg do 2692 1.1 mrg { 2693 1.1 mrg basic_block bb = bbs[i++]; 2694 1.1 mrg 2695 1.1 mrg if (exit_blocks && 2696 1.1 mrg bitmap_bit_p (exit_blocks, bb->index)) 2697 1.1 mrg continue; 2698 1.1 mrg 2699 1.1 mrg if (stop_at_irrevocable_p 2700 1.1 mrg && irr_blocks 2701 1.1 mrg && bitmap_bit_p (irr_blocks, bb->index)) 2702 1.1 mrg continue; 2703 1.1 mrg 2704 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 2705 1.1 mrg if ((include_uninstrumented_p 2706 1.1 mrg || !(e->flags & EDGE_TM_UNINSTRUMENTED)) 2707 1.1 mrg && !bitmap_bit_p (visited_blocks, e->dest->index)) 2708 1.1 mrg { 2709 1.1 mrg bitmap_set_bit (visited_blocks, e->dest->index); 2710 1.1 mrg bbs.safe_push (e->dest); 2711 1.1 mrg } 2712 1.1 mrg } 2713 1.1 mrg while (i < bbs.length ()); 2714 1.1 mrg 2715 1.1 mrg if (all_region_blocks) 2716 1.1 mrg bitmap_ior_into (all_region_blocks, visited_blocks); 2717 1.1 mrg 2718 1.1 mrg BITMAP_FREE (visited_blocks); 2719 1.1 mrg return bbs; 2720 1.1 mrg } 2721 1.1 mrg 2722 1.1 mrg // Callback data for collect_bb2reg. 2723 1.1 mrg struct bb2reg_stuff 2724 1.1 mrg { 2725 1.1 mrg vec<tm_region *> *bb2reg; 2726 1.1 mrg bool include_uninstrumented_p; 2727 1.1 mrg }; 2728 1.1 mrg 2729 1.1 mrg // Callback for expand_regions, collect innermost region data for each bb. 2730 1.1 mrg static void * 2731 1.1 mrg collect_bb2reg (struct tm_region *region, void *data) 2732 1.1 mrg { 2733 1.1 mrg struct bb2reg_stuff *stuff = (struct bb2reg_stuff *)data; 2734 1.1 mrg vec<tm_region *> *bb2reg = stuff->bb2reg; 2735 1.1 mrg vec<basic_block> queue; 2736 1.1 mrg unsigned int i; 2737 1.1 mrg basic_block bb; 2738 1.1 mrg 2739 1.1 mrg queue = get_tm_region_blocks (region->entry_block, 2740 1.1 mrg region->exit_blocks, 2741 1.1 mrg region->irr_blocks, 2742 1.1 mrg NULL, 2743 1.1 mrg /*stop_at_irr_p=*/true, 2744 1.1 mrg stuff->include_uninstrumented_p); 2745 1.1 mrg 2746 1.1 mrg // We expect expand_region to perform a post-order traversal of the region 2747 1.1 mrg // tree. Therefore the last region seen for any bb is the innermost. 2748 1.1 mrg FOR_EACH_VEC_ELT (queue, i, bb) 2749 1.1 mrg (*bb2reg)[bb->index] = region; 2750 1.1 mrg 2751 1.1 mrg queue.release (); 2752 1.1 mrg return NULL; 2753 1.1 mrg } 2754 1.1 mrg 2755 1.1 mrg // Returns a vector, indexed by BB->INDEX, of the innermost tm_region to 2756 1.1 mrg // which a basic block belongs. Note that we only consider the instrumented 2757 1.1 mrg // code paths for the region; the uninstrumented code paths are ignored if 2758 1.1 mrg // INCLUDE_UNINSTRUMENTED_P is false. 2759 1.1 mrg // 2760 1.1 mrg // ??? This data is very similar to the bb_regions array that is collected 2761 1.1 mrg // during tm_region_init. Or, rather, this data is similar to what could 2762 1.1 mrg // be used within tm_region_init. The actual computation in tm_region_init 2763 1.1 mrg // begins and ends with bb_regions entirely full of NULL pointers, due to 2764 1.1 mrg // the way in which pointers are swapped in and out of the array. 2765 1.1 mrg // 2766 1.1 mrg // ??? Our callers expect that blocks are not shared between transactions. 2767 1.1 mrg // When the optimizers get too smart, and blocks are shared, then during 2768 1.1 mrg // the tm_mark phase we'll add log entries to only one of the two transactions, 2769 1.1 mrg // and in the tm_edge phase we'll add edges to the CFG that create invalid 2770 1.1 mrg // cycles. The symptom being SSA defs that do not dominate their uses. 2771 1.1 mrg // Note that the optimizers were locally correct with their transformation, 2772 1.1 mrg // as we have no info within the program that suggests that the blocks cannot 2773 1.1 mrg // be shared. 2774 1.1 mrg // 2775 1.1 mrg // ??? There is currently a hack inside tree-ssa-pre.cc to work around the 2776 1.1 mrg // only known instance of this block sharing. 2777 1.1 mrg 2778 1.1 mrg static vec<tm_region *> 2779 1.1 mrg get_bb_regions_instrumented (bool traverse_clones, 2780 1.1 mrg bool include_uninstrumented_p) 2781 1.1 mrg { 2782 1.1 mrg unsigned n = last_basic_block_for_fn (cfun); 2783 1.1 mrg struct bb2reg_stuff stuff; 2784 1.1 mrg vec<tm_region *> ret; 2785 1.1 mrg 2786 1.1 mrg ret.create (n); 2787 1.1 mrg ret.safe_grow_cleared (n, true); 2788 1.1 mrg stuff.bb2reg = &ret; 2789 1.1 mrg stuff.include_uninstrumented_p = include_uninstrumented_p; 2790 1.1 mrg expand_regions (all_tm_regions, collect_bb2reg, &stuff, traverse_clones); 2791 1.1 mrg 2792 1.1 mrg return ret; 2793 1.1 mrg } 2794 1.1 mrg 2795 1.1 mrg /* Set the IN_TRANSACTION for all gimple statements that appear in a 2796 1.1 mrg transaction. */ 2797 1.1 mrg 2798 1.1 mrg void 2799 1.1 mrg compute_transaction_bits (void) 2800 1.1 mrg { 2801 1.1 mrg struct tm_region *region; 2802 1.1 mrg vec<basic_block> queue; 2803 1.1 mrg unsigned int i; 2804 1.1 mrg basic_block bb; 2805 1.1 mrg 2806 1.1 mrg /* ?? Perhaps we need to abstract gate_tm_init further, because we 2807 1.1 mrg certainly don't need it to calculate CDI_DOMINATOR info. */ 2808 1.1 mrg gate_tm_init (); 2809 1.1 mrg 2810 1.1 mrg FOR_EACH_BB_FN (bb, cfun) 2811 1.1 mrg bb->flags &= ~BB_IN_TRANSACTION; 2812 1.1 mrg 2813 1.1 mrg for (region = all_tm_regions; region; region = region->next) 2814 1.1 mrg { 2815 1.1 mrg queue = get_tm_region_blocks (region->entry_block, 2816 1.1 mrg region->exit_blocks, 2817 1.1 mrg region->irr_blocks, 2818 1.1 mrg NULL, 2819 1.1 mrg /*stop_at_irr_p=*/true); 2820 1.1 mrg for (i = 0; queue.iterate (i, &bb); ++i) 2821 1.1 mrg bb->flags |= BB_IN_TRANSACTION; 2822 1.1 mrg queue.release (); 2823 1.1 mrg } 2824 1.1 mrg 2825 1.1 mrg if (all_tm_regions) 2826 1.1 mrg bitmap_obstack_release (&tm_obstack); 2827 1.1 mrg } 2828 1.1 mrg 2829 1.1 mrg /* Replace the GIMPLE_TRANSACTION in this region with the corresponding 2830 1.1 mrg call to BUILT_IN_TM_START. */ 2831 1.1 mrg 2832 1.1 mrg static void * 2833 1.1 mrg expand_transaction (struct tm_region *region, void *data ATTRIBUTE_UNUSED) 2834 1.1 mrg { 2835 1.1 mrg tree tm_start = builtin_decl_explicit (BUILT_IN_TM_START); 2836 1.1 mrg basic_block transaction_bb = gimple_bb (region->transaction_stmt); 2837 1.1 mrg tree tm_state = region->tm_state; 2838 1.1 mrg tree tm_state_type = TREE_TYPE (tm_state); 2839 1.1 mrg edge abort_edge = NULL; 2840 1.1 mrg edge inst_edge = NULL; 2841 1.1 mrg edge uninst_edge = NULL; 2842 1.1 mrg edge fallthru_edge = NULL; 2843 1.1 mrg 2844 1.1 mrg // Identify the various successors of the transaction start. 2845 1.1 mrg { 2846 1.1 mrg edge_iterator i; 2847 1.1 mrg edge e; 2848 1.1 mrg FOR_EACH_EDGE (e, i, transaction_bb->succs) 2849 1.1 mrg { 2850 1.1 mrg if (e->flags & EDGE_TM_ABORT) 2851 1.1 mrg abort_edge = e; 2852 1.1 mrg else if (e->flags & EDGE_TM_UNINSTRUMENTED) 2853 1.1 mrg uninst_edge = e; 2854 1.1 mrg else 2855 1.1 mrg inst_edge = e; 2856 1.1 mrg if (e->flags & EDGE_FALLTHRU) 2857 1.1 mrg fallthru_edge = e; 2858 1.1 mrg } 2859 1.1 mrg } 2860 1.1 mrg 2861 1.1 mrg /* ??? There are plenty of bits here we're not computing. */ 2862 1.1 mrg { 2863 1.1 mrg int subcode = gimple_transaction_subcode (region->get_transaction_stmt ()); 2864 1.1 mrg int flags = 0; 2865 1.1 mrg if (subcode & GTMA_DOES_GO_IRREVOCABLE) 2866 1.1 mrg flags |= PR_DOESGOIRREVOCABLE; 2867 1.1 mrg if ((subcode & GTMA_MAY_ENTER_IRREVOCABLE) == 0) 2868 1.1 mrg flags |= PR_HASNOIRREVOCABLE; 2869 1.1 mrg /* If the transaction does not have an abort in lexical scope and is not 2870 1.1 mrg marked as an outer transaction, then it will never abort. */ 2871 1.1 mrg if ((subcode & GTMA_HAVE_ABORT) == 0 && (subcode & GTMA_IS_OUTER) == 0) 2872 1.1 mrg flags |= PR_HASNOABORT; 2873 1.1 mrg if ((subcode & GTMA_HAVE_STORE) == 0) 2874 1.1 mrg flags |= PR_READONLY; 2875 1.1 mrg if (inst_edge && !(subcode & GTMA_HAS_NO_INSTRUMENTATION)) 2876 1.1 mrg flags |= PR_INSTRUMENTEDCODE; 2877 1.1 mrg if (uninst_edge) 2878 1.1 mrg flags |= PR_UNINSTRUMENTEDCODE; 2879 1.1 mrg if (subcode & GTMA_IS_OUTER) 2880 1.1 mrg region->original_transaction_was_outer = true; 2881 1.1 mrg tree t = build_int_cst (tm_state_type, flags); 2882 1.1 mrg gcall *call = gimple_build_call (tm_start, 1, t); 2883 1.1 mrg gimple_call_set_lhs (call, tm_state); 2884 1.1 mrg gimple_set_location (call, gimple_location (region->transaction_stmt)); 2885 1.1 mrg 2886 1.1 mrg // Replace the GIMPLE_TRANSACTION with the call to BUILT_IN_TM_START. 2887 1.1 mrg gimple_stmt_iterator gsi = gsi_last_bb (transaction_bb); 2888 1.1 mrg gcc_assert (gsi_stmt (gsi) == region->transaction_stmt); 2889 1.1 mrg gsi_insert_before (&gsi, call, GSI_SAME_STMT); 2890 1.1 mrg gsi_remove (&gsi, true); 2891 1.1 mrg region->transaction_stmt = call; 2892 1.1 mrg } 2893 1.1 mrg 2894 1.1 mrg // Generate log saves. 2895 1.1 mrg if (!tm_log_save_addresses.is_empty ()) 2896 1.1 mrg tm_log_emit_saves (region->entry_block, transaction_bb); 2897 1.1 mrg 2898 1.1 mrg // In the beginning, we've no tests to perform on transaction restart. 2899 1.1 mrg // Note that after this point, transaction_bb becomes the "most recent 2900 1.1 mrg // block containing tests for the transaction". 2901 1.1 mrg region->restart_block = region->entry_block; 2902 1.1 mrg 2903 1.1 mrg // Generate log restores. 2904 1.1 mrg if (!tm_log_save_addresses.is_empty ()) 2905 1.1 mrg { 2906 1.1 mrg basic_block test_bb = create_empty_bb (transaction_bb); 2907 1.1 mrg basic_block code_bb = create_empty_bb (test_bb); 2908 1.1 mrg basic_block join_bb = create_empty_bb (code_bb); 2909 1.1 mrg add_bb_to_loop (test_bb, transaction_bb->loop_father); 2910 1.1 mrg add_bb_to_loop (code_bb, transaction_bb->loop_father); 2911 1.1 mrg add_bb_to_loop (join_bb, transaction_bb->loop_father); 2912 1.1 mrg if (region->restart_block == region->entry_block) 2913 1.1 mrg region->restart_block = test_bb; 2914 1.1 mrg 2915 1.1 mrg tree t1 = create_tmp_reg (tm_state_type); 2916 1.1 mrg tree t2 = build_int_cst (tm_state_type, A_RESTORELIVEVARIABLES); 2917 1.1 mrg gimple *stmt = gimple_build_assign (t1, BIT_AND_EXPR, tm_state, t2); 2918 1.1 mrg gimple_stmt_iterator gsi = gsi_last_bb (test_bb); 2919 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 2920 1.1 mrg 2921 1.1 mrg t2 = build_int_cst (tm_state_type, 0); 2922 1.1 mrg stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL); 2923 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 2924 1.1 mrg 2925 1.1 mrg tm_log_emit_restores (region->entry_block, code_bb); 2926 1.1 mrg 2927 1.1 mrg edge ei = make_edge (transaction_bb, test_bb, EDGE_FALLTHRU); 2928 1.1 mrg edge et = make_edge (test_bb, code_bb, EDGE_TRUE_VALUE); 2929 1.1 mrg edge ef = make_edge (test_bb, join_bb, EDGE_FALSE_VALUE); 2930 1.1 mrg redirect_edge_pred (fallthru_edge, join_bb); 2931 1.1 mrg 2932 1.1 mrg join_bb->count = test_bb->count = transaction_bb->count; 2933 1.1 mrg 2934 1.1 mrg ei->probability = profile_probability::always (); 2935 1.1 mrg et->probability = profile_probability::likely (); 2936 1.1 mrg ef->probability = profile_probability::unlikely (); 2937 1.1 mrg 2938 1.1 mrg code_bb->count = et->count (); 2939 1.1 mrg 2940 1.1 mrg transaction_bb = join_bb; 2941 1.1 mrg } 2942 1.1 mrg 2943 1.1 mrg // If we have an ABORT edge, create a test to perform the abort. 2944 1.1 mrg if (abort_edge) 2945 1.1 mrg { 2946 1.1 mrg basic_block test_bb = create_empty_bb (transaction_bb); 2947 1.1 mrg add_bb_to_loop (test_bb, transaction_bb->loop_father); 2948 1.1 mrg if (region->restart_block == region->entry_block) 2949 1.1 mrg region->restart_block = test_bb; 2950 1.1 mrg 2951 1.1 mrg tree t1 = create_tmp_reg (tm_state_type); 2952 1.1 mrg tree t2 = build_int_cst (tm_state_type, A_ABORTTRANSACTION); 2953 1.1 mrg gimple *stmt = gimple_build_assign (t1, BIT_AND_EXPR, tm_state, t2); 2954 1.1 mrg gimple_stmt_iterator gsi = gsi_last_bb (test_bb); 2955 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 2956 1.1 mrg 2957 1.1 mrg t2 = build_int_cst (tm_state_type, 0); 2958 1.1 mrg stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL); 2959 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 2960 1.1 mrg 2961 1.1 mrg edge ei = make_edge (transaction_bb, test_bb, EDGE_FALLTHRU); 2962 1.1 mrg test_bb->count = transaction_bb->count; 2963 1.1 mrg ei->probability = profile_probability::always (); 2964 1.1 mrg 2965 1.1 mrg // Not abort edge. If both are live, chose one at random as we'll 2966 1.1 mrg // we'll be fixing that up below. 2967 1.1 mrg redirect_edge_pred (fallthru_edge, test_bb); 2968 1.1 mrg fallthru_edge->flags = EDGE_FALSE_VALUE; 2969 1.1 mrg fallthru_edge->probability = profile_probability::very_likely (); 2970 1.1 mrg 2971 1.1 mrg // Abort/over edge. 2972 1.1 mrg redirect_edge_pred (abort_edge, test_bb); 2973 1.1 mrg abort_edge->flags = EDGE_TRUE_VALUE; 2974 1.1 mrg abort_edge->probability = profile_probability::unlikely (); 2975 1.1 mrg 2976 1.1 mrg transaction_bb = test_bb; 2977 1.1 mrg } 2978 1.1 mrg 2979 1.1 mrg // If we have both instrumented and uninstrumented code paths, select one. 2980 1.1 mrg if (inst_edge && uninst_edge) 2981 1.1 mrg { 2982 1.1 mrg basic_block test_bb = create_empty_bb (transaction_bb); 2983 1.1 mrg add_bb_to_loop (test_bb, transaction_bb->loop_father); 2984 1.1 mrg if (region->restart_block == region->entry_block) 2985 1.1 mrg region->restart_block = test_bb; 2986 1.1 mrg 2987 1.1 mrg tree t1 = create_tmp_reg (tm_state_type); 2988 1.1 mrg tree t2 = build_int_cst (tm_state_type, A_RUNUNINSTRUMENTEDCODE); 2989 1.1 mrg 2990 1.1 mrg gimple *stmt = gimple_build_assign (t1, BIT_AND_EXPR, tm_state, t2); 2991 1.1 mrg gimple_stmt_iterator gsi = gsi_last_bb (test_bb); 2992 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 2993 1.1 mrg 2994 1.1 mrg t2 = build_int_cst (tm_state_type, 0); 2995 1.1 mrg stmt = gimple_build_cond (NE_EXPR, t1, t2, NULL, NULL); 2996 1.1 mrg gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING); 2997 1.1 mrg 2998 1.1 mrg // Create the edge into test_bb first, as we want to copy values 2999 1.1 mrg // out of the fallthru edge. 3000 1.1 mrg edge e = make_edge (transaction_bb, test_bb, fallthru_edge->flags); 3001 1.1 mrg e->probability = fallthru_edge->probability; 3002 1.1 mrg test_bb->count = fallthru_edge->count (); 3003 1.1 mrg 3004 1.1 mrg // Now update the edges to the inst/uninist implementations. 3005 1.1 mrg // For now assume that the paths are equally likely. When using HTM, 3006 1.1 mrg // we'll try the uninst path first and fallback to inst path if htm 3007 1.1 mrg // buffers are exceeded. Without HTM we start with the inst path and 3008 1.1 mrg // use the uninst path when falling back to serial mode. 3009 1.1 mrg redirect_edge_pred (inst_edge, test_bb); 3010 1.1 mrg inst_edge->flags = EDGE_FALSE_VALUE; 3011 1.1 mrg inst_edge->probability = profile_probability::even (); 3012 1.1 mrg 3013 1.1 mrg redirect_edge_pred (uninst_edge, test_bb); 3014 1.1 mrg uninst_edge->flags = EDGE_TRUE_VALUE; 3015 1.1 mrg uninst_edge->probability = profile_probability::even (); 3016 1.1 mrg } 3017 1.1 mrg 3018 1.1 mrg // If we have no previous special cases, and we have PHIs at the beginning 3019 1.1 mrg // of the atomic region, this means we have a loop at the beginning of the 3020 1.1 mrg // atomic region that shares the first block. This can cause problems with 3021 1.1 mrg // the transaction restart abnormal edges to be added in the tm_edges pass. 3022 1.1 mrg // Solve this by adding a new empty block to receive the abnormal edges. 3023 1.1 mrg if (region->restart_block == region->entry_block 3024 1.1 mrg && phi_nodes (region->entry_block)) 3025 1.1 mrg { 3026 1.1 mrg basic_block empty_bb = create_empty_bb (transaction_bb); 3027 1.1 mrg region->restart_block = empty_bb; 3028 1.1 mrg add_bb_to_loop (empty_bb, transaction_bb->loop_father); 3029 1.1 mrg 3030 1.1 mrg redirect_edge_pred (fallthru_edge, empty_bb); 3031 1.1 mrg make_edge (transaction_bb, empty_bb, EDGE_FALLTHRU); 3032 1.1 mrg } 3033 1.1 mrg 3034 1.1 mrg return NULL; 3035 1.1 mrg } 3036 1.1 mrg 3037 1.1 mrg /* Generate the temporary to be used for the return value of 3038 1.1 mrg BUILT_IN_TM_START. */ 3039 1.1 mrg 3040 1.1 mrg static void * 3041 1.1 mrg generate_tm_state (struct tm_region *region, void *data ATTRIBUTE_UNUSED) 3042 1.1 mrg { 3043 1.1 mrg tree tm_start = builtin_decl_explicit (BUILT_IN_TM_START); 3044 1.1 mrg region->tm_state = 3045 1.1 mrg create_tmp_reg (TREE_TYPE (TREE_TYPE (tm_start)), "tm_state"); 3046 1.1 mrg 3047 1.1 mrg // Reset the subcode, post optimizations. We'll fill this in 3048 1.1 mrg // again as we process blocks. 3049 1.1 mrg if (region->exit_blocks) 3050 1.1 mrg { 3051 1.1 mrg gtransaction *transaction_stmt = region->get_transaction_stmt (); 3052 1.1 mrg unsigned int subcode = gimple_transaction_subcode (transaction_stmt); 3053 1.1 mrg 3054 1.1 mrg if (subcode & GTMA_DOES_GO_IRREVOCABLE) 3055 1.1 mrg subcode &= (GTMA_DECLARATION_MASK | GTMA_DOES_GO_IRREVOCABLE 3056 1.1 mrg | GTMA_MAY_ENTER_IRREVOCABLE 3057 1.1 mrg | GTMA_HAS_NO_INSTRUMENTATION); 3058 1.1 mrg else 3059 1.1 mrg subcode &= GTMA_DECLARATION_MASK; 3060 1.1 mrg gimple_transaction_set_subcode (transaction_stmt, subcode); 3061 1.1 mrg } 3062 1.1 mrg 3063 1.1 mrg return NULL; 3064 1.1 mrg } 3065 1.1 mrg 3066 1.1 mrg // Propagate flags from inner transactions outwards. 3067 1.1 mrg static void 3068 1.1 mrg propagate_tm_flags_out (struct tm_region *region) 3069 1.1 mrg { 3070 1.1 mrg if (region == NULL) 3071 1.1 mrg return; 3072 1.1 mrg propagate_tm_flags_out (region->inner); 3073 1.1 mrg 3074 1.1 mrg if (region->outer && region->outer->transaction_stmt) 3075 1.1 mrg { 3076 1.1 mrg unsigned s 3077 1.1 mrg = gimple_transaction_subcode (region->get_transaction_stmt ()); 3078 1.1 mrg s &= (GTMA_HAVE_ABORT | GTMA_HAVE_LOAD | GTMA_HAVE_STORE 3079 1.1 mrg | GTMA_MAY_ENTER_IRREVOCABLE); 3080 1.1 mrg s |= gimple_transaction_subcode (region->outer->get_transaction_stmt ()); 3081 1.1 mrg gimple_transaction_set_subcode (region->outer->get_transaction_stmt (), 3082 1.1 mrg s); 3083 1.1 mrg } 3084 1.1 mrg 3085 1.1 mrg propagate_tm_flags_out (region->next); 3086 1.1 mrg } 3087 1.1 mrg 3088 1.1 mrg /* Entry point to the MARK phase of TM expansion. Here we replace 3089 1.1 mrg transactional memory statements with calls to builtins, and function 3090 1.1 mrg calls with their transactional clones (if available). But we don't 3091 1.1 mrg yet lower GIMPLE_TRANSACTION or add the transaction restart back-edges. */ 3092 1.1 mrg 3093 1.1 mrg static unsigned int 3094 1.1 mrg execute_tm_mark (void) 3095 1.1 mrg { 3096 1.1 mrg pending_edge_inserts_p = false; 3097 1.1 mrg 3098 1.1 mrg expand_regions (all_tm_regions, generate_tm_state, NULL, 3099 1.1 mrg /*traverse_clones=*/true); 3100 1.1 mrg 3101 1.1 mrg tm_log_init (); 3102 1.1 mrg 3103 1.1 mrg vec<tm_region *> bb_regions 3104 1.1 mrg = get_bb_regions_instrumented (/*traverse_clones=*/true, 3105 1.1 mrg /*include_uninstrumented_p=*/false); 3106 1.1 mrg struct tm_region *r; 3107 1.1 mrg unsigned i; 3108 1.1 mrg 3109 1.1 mrg // Expand memory operations into calls into the runtime. 3110 1.1 mrg // This collects log entries as well. 3111 1.1 mrg FOR_EACH_VEC_ELT (bb_regions, i, r) 3112 1.1 mrg { 3113 1.1 mrg if (r != NULL) 3114 1.1 mrg { 3115 1.1 mrg if (r->transaction_stmt) 3116 1.1 mrg { 3117 1.1 mrg unsigned sub 3118 1.1 mrg = gimple_transaction_subcode (r->get_transaction_stmt ()); 3119 1.1 mrg 3120 1.1 mrg /* If we're sure to go irrevocable, there won't be 3121 1.1 mrg anything to expand, since the run-time will go 3122 1.1 mrg irrevocable right away. */ 3123 1.1 mrg if (sub & GTMA_DOES_GO_IRREVOCABLE 3124 1.1 mrg && sub & GTMA_MAY_ENTER_IRREVOCABLE) 3125 1.1 mrg continue; 3126 1.1 mrg } 3127 1.1 mrg expand_block_tm (r, BASIC_BLOCK_FOR_FN (cfun, i)); 3128 1.1 mrg } 3129 1.1 mrg } 3130 1.1 mrg 3131 1.1 mrg bb_regions.release (); 3132 1.1 mrg 3133 1.1 mrg // Propagate flags from inner transactions outwards. 3134 1.1 mrg propagate_tm_flags_out (all_tm_regions); 3135 1.1 mrg 3136 1.1 mrg // Expand GIMPLE_TRANSACTIONs into calls into the runtime. 3137 1.1 mrg expand_regions (all_tm_regions, expand_transaction, NULL, 3138 1.1 mrg /*traverse_clones=*/false); 3139 1.1 mrg 3140 1.1 mrg tm_log_emit (); 3141 1.1 mrg tm_log_delete (); 3142 1.1 mrg 3143 1.1 mrg if (pending_edge_inserts_p) 3144 1.1 mrg gsi_commit_edge_inserts (); 3145 1.1 mrg free_dominance_info (CDI_DOMINATORS); 3146 1.1 mrg return 0; 3147 1.1 mrg } 3148 1.1 mrg 3149 1.1 mrg namespace { 3150 1.1 mrg 3151 1.1 mrg const pass_data pass_data_tm_mark = 3152 1.1 mrg { 3153 1.1 mrg GIMPLE_PASS, /* type */ 3154 1.1 mrg "tmmark", /* name */ 3155 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 3156 1.1 mrg TV_TRANS_MEM, /* tv_id */ 3157 1.1 mrg ( PROP_ssa | PROP_cfg ), /* properties_required */ 3158 1.1 mrg 0, /* properties_provided */ 3159 1.1 mrg 0, /* properties_destroyed */ 3160 1.1 mrg 0, /* todo_flags_start */ 3161 1.1 mrg TODO_update_ssa, /* todo_flags_finish */ 3162 1.1 mrg }; 3163 1.1 mrg 3164 1.1 mrg class pass_tm_mark : public gimple_opt_pass 3165 1.1 mrg { 3166 1.1 mrg public: 3167 1.1 mrg pass_tm_mark (gcc::context *ctxt) 3168 1.1 mrg : gimple_opt_pass (pass_data_tm_mark, ctxt) 3169 1.1 mrg {} 3170 1.1 mrg 3171 1.1 mrg /* opt_pass methods: */ 3172 1.1 mrg virtual unsigned int execute (function *) { return execute_tm_mark (); } 3173 1.1 mrg 3174 1.1 mrg }; // class pass_tm_mark 3175 1.1 mrg 3176 1.1 mrg } // anon namespace 3177 1.1 mrg 3178 1.1 mrg gimple_opt_pass * 3179 1.1 mrg make_pass_tm_mark (gcc::context *ctxt) 3180 1.1 mrg { 3181 1.1 mrg return new pass_tm_mark (ctxt); 3182 1.1 mrg } 3183 1.1 mrg 3184 1.1 mrg 3186 1.1 mrg /* Create an abnormal edge from STMT at iter, splitting the block 3187 1.1 mrg as necessary. Adjust *PNEXT as needed for the split block. */ 3188 1.1 mrg 3189 1.1 mrg static inline void 3190 1.1 mrg split_bb_make_tm_edge (gimple *stmt, basic_block dest_bb, 3191 1.1 mrg gimple_stmt_iterator iter, gimple_stmt_iterator *pnext) 3192 1.1 mrg { 3193 1.1 mrg basic_block bb = gimple_bb (stmt); 3194 1.1 mrg if (!gsi_one_before_end_p (iter)) 3195 1.1 mrg { 3196 1.1 mrg edge e = split_block (bb, stmt); 3197 1.1 mrg *pnext = gsi_start_bb (e->dest); 3198 1.1 mrg } 3199 1.1 mrg edge e = make_edge (bb, dest_bb, EDGE_ABNORMAL); 3200 1.1 mrg if (e) 3201 1.1 mrg e->probability = profile_probability::guessed_never (); 3202 1.1 mrg 3203 1.1 mrg // Record the need for the edge for the benefit of the rtl passes. 3204 1.1 mrg if (cfun->gimple_df->tm_restart == NULL) 3205 1.1 mrg cfun->gimple_df->tm_restart 3206 1.1 mrg = hash_table<tm_restart_hasher>::create_ggc (31); 3207 1.1 mrg 3208 1.1 mrg struct tm_restart_node dummy; 3209 1.1 mrg dummy.stmt = stmt; 3210 1.1 mrg dummy.label_or_list = gimple_block_label (dest_bb); 3211 1.1 mrg 3212 1.1 mrg tm_restart_node **slot = cfun->gimple_df->tm_restart->find_slot (&dummy, 3213 1.1 mrg INSERT); 3214 1.1 mrg struct tm_restart_node *n = *slot; 3215 1.1 mrg if (n == NULL) 3216 1.1 mrg { 3217 1.1 mrg n = ggc_alloc<tm_restart_node> (); 3218 1.1 mrg *n = dummy; 3219 1.1 mrg } 3220 1.1 mrg else 3221 1.1 mrg { 3222 1.1 mrg tree old = n->label_or_list; 3223 1.1 mrg if (TREE_CODE (old) == LABEL_DECL) 3224 1.1 mrg old = tree_cons (NULL, old, NULL); 3225 1.1 mrg n->label_or_list = tree_cons (NULL, dummy.label_or_list, old); 3226 1.1 mrg } 3227 1.1 mrg } 3228 1.1 mrg 3229 1.1 mrg /* Split block BB as necessary for every builtin function we added, and 3230 1.1 mrg wire up the abnormal back edges implied by the transaction restart. */ 3231 1.1 mrg 3232 1.1 mrg static void 3233 1.1 mrg expand_block_edges (struct tm_region *const region, basic_block bb) 3234 1.1 mrg { 3235 1.1 mrg gimple_stmt_iterator gsi, next_gsi; 3236 1.1 mrg 3237 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi = next_gsi) 3238 1.1 mrg { 3239 1.1 mrg gimple *stmt = gsi_stmt (gsi); 3240 1.1 mrg gcall *call_stmt; 3241 1.1 mrg 3242 1.1 mrg next_gsi = gsi; 3243 1.1 mrg gsi_next (&next_gsi); 3244 1.1 mrg 3245 1.1 mrg // ??? Shouldn't we split for any non-pure, non-irrevocable function? 3246 1.1 mrg call_stmt = dyn_cast <gcall *> (stmt); 3247 1.1 mrg if ((!call_stmt) 3248 1.1 mrg || (gimple_call_flags (call_stmt) & ECF_TM_BUILTIN) == 0) 3249 1.1 mrg continue; 3250 1.1 mrg 3251 1.1 mrg if (gimple_call_builtin_p (call_stmt, BUILT_IN_TM_ABORT)) 3252 1.1 mrg { 3253 1.1 mrg // If we have a ``_transaction_cancel [[outer]]'', there is only 3254 1.1 mrg // one abnormal edge: to the transaction marked OUTER. 3255 1.1 mrg // All compiler-generated instances of BUILT_IN_TM_ABORT have a 3256 1.1 mrg // constant argument, which we can examine here. Users invoking 3257 1.1 mrg // TM_ABORT directly get what they deserve. 3258 1.1 mrg tree arg = gimple_call_arg (call_stmt, 0); 3259 1.1 mrg if (TREE_CODE (arg) == INTEGER_CST 3260 1.1 mrg && (TREE_INT_CST_LOW (arg) & AR_OUTERABORT) != 0 3261 1.1 mrg && !decl_is_tm_clone (current_function_decl)) 3262 1.1 mrg { 3263 1.1 mrg // Find the GTMA_IS_OUTER transaction. 3264 1.1 mrg for (struct tm_region *o = region; o; o = o->outer) 3265 1.1 mrg if (o->original_transaction_was_outer) 3266 1.1 mrg { 3267 1.1 mrg split_bb_make_tm_edge (call_stmt, o->restart_block, 3268 1.1 mrg gsi, &next_gsi); 3269 1.1 mrg break; 3270 1.1 mrg } 3271 1.1 mrg 3272 1.1 mrg // Otherwise, the front-end should have semantically checked 3273 1.1 mrg // outer aborts, but in either case the target region is not 3274 1.1 mrg // within this function. 3275 1.1 mrg continue; 3276 1.1 mrg } 3277 1.1 mrg 3278 1.1 mrg // Non-outer, TM aborts have an abnormal edge to the inner-most 3279 1.1 mrg // transaction, the one being aborted; 3280 1.1 mrg split_bb_make_tm_edge (call_stmt, region->restart_block, gsi, 3281 1.1 mrg &next_gsi); 3282 1.1 mrg } 3283 1.1 mrg 3284 1.1 mrg // All TM builtins have an abnormal edge to the outer-most transaction. 3285 1.1 mrg // We never restart inner transactions. For tm clones, we know a-priori 3286 1.1 mrg // that the outer-most transaction is outside the function. 3287 1.1 mrg if (decl_is_tm_clone (current_function_decl)) 3288 1.1 mrg continue; 3289 1.1 mrg 3290 1.1 mrg if (cfun->gimple_df->tm_restart == NULL) 3291 1.1 mrg cfun->gimple_df->tm_restart 3292 1.1 mrg = hash_table<tm_restart_hasher>::create_ggc (31); 3293 1.1 mrg 3294 1.1 mrg // All TM builtins have an abnormal edge to the outer-most transaction. 3295 1.1 mrg // We never restart inner transactions. 3296 1.1 mrg for (struct tm_region *o = region; o; o = o->outer) 3297 1.1 mrg if (!o->outer) 3298 1.1 mrg { 3299 1.1 mrg split_bb_make_tm_edge (call_stmt, o->restart_block, gsi, &next_gsi); 3300 1.1 mrg break; 3301 1.1 mrg } 3302 1.1 mrg 3303 1.1 mrg // Delete any tail-call annotation that may have been added. 3304 1.1 mrg // The tail-call pass may have mis-identified the commit as being 3305 1.1 mrg // a candidate because we had not yet added this restart edge. 3306 1.1 mrg gimple_call_set_tail (call_stmt, false); 3307 1.1 mrg } 3308 1.1 mrg } 3309 1.1 mrg 3310 1.1 mrg /* Entry point to the final expansion of transactional nodes. */ 3311 1.1 mrg 3312 1.1 mrg namespace { 3313 1.1 mrg 3314 1.1 mrg const pass_data pass_data_tm_edges = 3315 1.1 mrg { 3316 1.1 mrg GIMPLE_PASS, /* type */ 3317 1.1 mrg "tmedge", /* name */ 3318 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 3319 1.1 mrg TV_TRANS_MEM, /* tv_id */ 3320 1.1 mrg ( PROP_ssa | PROP_cfg ), /* properties_required */ 3321 1.1 mrg 0, /* properties_provided */ 3322 1.1 mrg 0, /* properties_destroyed */ 3323 1.1 mrg 0, /* todo_flags_start */ 3324 1.1 mrg TODO_update_ssa, /* todo_flags_finish */ 3325 1.1 mrg }; 3326 1.1 mrg 3327 1.1 mrg class pass_tm_edges : public gimple_opt_pass 3328 1.1 mrg { 3329 1.1 mrg public: 3330 1.1 mrg pass_tm_edges (gcc::context *ctxt) 3331 1.1 mrg : gimple_opt_pass (pass_data_tm_edges, ctxt) 3332 1.1 mrg {} 3333 1.1 mrg 3334 1.1 mrg /* opt_pass methods: */ 3335 1.1 mrg virtual unsigned int execute (function *); 3336 1.1 mrg 3337 1.1 mrg }; // class pass_tm_edges 3338 1.1 mrg 3339 1.1 mrg unsigned int 3340 1.1 mrg pass_tm_edges::execute (function *fun) 3341 1.1 mrg { 3342 1.1 mrg vec<tm_region *> bb_regions 3343 1.1 mrg = get_bb_regions_instrumented (/*traverse_clones=*/false, 3344 1.1 mrg /*include_uninstrumented_p=*/true); 3345 1.1 mrg struct tm_region *r; 3346 1.1 mrg unsigned i; 3347 1.1 mrg 3348 1.1 mrg FOR_EACH_VEC_ELT (bb_regions, i, r) 3349 1.1 mrg if (r != NULL) 3350 1.1 mrg expand_block_edges (r, BASIC_BLOCK_FOR_FN (fun, i)); 3351 1.1 mrg 3352 1.1 mrg bb_regions.release (); 3353 1.1 mrg 3354 1.1 mrg /* We've got to release the dominance info now, to indicate that it 3355 1.1 mrg must be rebuilt completely. Otherwise we'll crash trying to update 3356 1.1 mrg the SSA web in the TODO section following this pass. */ 3357 1.1 mrg free_dominance_info (CDI_DOMINATORS); 3358 1.1 mrg /* We'ge also wrecked loops badly with inserting of abnormal edges. */ 3359 1.1 mrg loops_state_set (LOOPS_NEED_FIXUP); 3360 1.1 mrg bitmap_obstack_release (&tm_obstack); 3361 1.1 mrg all_tm_regions = NULL; 3362 1.1 mrg 3363 1.1 mrg return 0; 3364 1.1 mrg } 3365 1.1 mrg 3366 1.1 mrg } // anon namespace 3367 1.1 mrg 3368 1.1 mrg gimple_opt_pass * 3369 1.1 mrg make_pass_tm_edges (gcc::context *ctxt) 3370 1.1 mrg { 3371 1.1 mrg return new pass_tm_edges (ctxt); 3372 1.1 mrg } 3373 1.1 mrg 3374 1.1 mrg /* Helper function for expand_regions. Expand REGION and recurse to 3376 1.1 mrg the inner region. Call CALLBACK on each region. CALLBACK returns 3377 1.1 mrg NULL to continue the traversal, otherwise a non-null value which 3378 1.1 mrg this function will return as well. TRAVERSE_CLONES is true if we 3379 1.1 mrg should traverse transactional clones. */ 3380 1.1 mrg 3381 1.1 mrg static void * 3382 1.1 mrg expand_regions_1 (struct tm_region *region, 3383 1.1 mrg void *(*callback)(struct tm_region *, void *), 3384 1.1 mrg void *data, 3385 1.1 mrg bool traverse_clones) 3386 1.1 mrg { 3387 1.1 mrg void *retval = NULL; 3388 1.1 mrg if (region->exit_blocks 3389 1.1 mrg || (traverse_clones && decl_is_tm_clone (current_function_decl))) 3390 1.1 mrg { 3391 1.1 mrg retval = callback (region, data); 3392 1.1 mrg if (retval) 3393 1.1 mrg return retval; 3394 1.1 mrg } 3395 1.1 mrg if (region->inner) 3396 1.1 mrg { 3397 1.1 mrg retval = expand_regions (region->inner, callback, data, traverse_clones); 3398 1.1 mrg if (retval) 3399 1.1 mrg return retval; 3400 1.1 mrg } 3401 1.1 mrg return retval; 3402 1.1 mrg } 3403 1.1 mrg 3404 1.1 mrg /* Traverse the regions enclosed and including REGION. Execute 3405 1.1 mrg CALLBACK for each region, passing DATA. CALLBACK returns NULL to 3406 1.1 mrg continue the traversal, otherwise a non-null value which this 3407 1.1 mrg function will return as well. TRAVERSE_CLONES is true if we should 3408 1.1 mrg traverse transactional clones. */ 3409 1.1 mrg 3410 1.1 mrg static void * 3411 1.1 mrg expand_regions (struct tm_region *region, 3412 1.1 mrg void *(*callback)(struct tm_region *, void *), 3413 1.1 mrg void *data, 3414 1.1 mrg bool traverse_clones) 3415 1.1 mrg { 3416 1.1 mrg void *retval = NULL; 3417 1.1 mrg while (region) 3418 1.1 mrg { 3419 1.1 mrg retval = expand_regions_1 (region, callback, data, traverse_clones); 3420 1.1 mrg if (retval) 3421 1.1 mrg return retval; 3422 1.1 mrg region = region->next; 3423 1.1 mrg } 3424 1.1 mrg return retval; 3425 1.1 mrg } 3426 1.1 mrg 3427 1.1 mrg 3428 1.1 mrg /* A unique TM memory operation. */ 3430 1.1 mrg struct tm_memop 3431 1.1 mrg { 3432 1.1 mrg /* Unique ID that all memory operations to the same location have. */ 3433 1.1 mrg unsigned int value_id; 3434 1.1 mrg /* Address of load/store. */ 3435 1.1 mrg tree addr; 3436 1.1 mrg }; 3437 1.1 mrg 3438 1.1 mrg /* TM memory operation hashtable helpers. */ 3439 1.1 mrg 3440 1.1 mrg struct tm_memop_hasher : free_ptr_hash <tm_memop> 3441 1.1 mrg { 3442 1.1 mrg static inline hashval_t hash (const tm_memop *); 3443 1.1 mrg static inline bool equal (const tm_memop *, const tm_memop *); 3444 1.1 mrg }; 3445 1.1 mrg 3446 1.1 mrg /* Htab support. Return a hash value for a `tm_memop'. */ 3447 1.1 mrg inline hashval_t 3448 1.1 mrg tm_memop_hasher::hash (const tm_memop *mem) 3449 1.1 mrg { 3450 1.1 mrg tree addr = mem->addr; 3451 1.1 mrg /* We drill down to the SSA_NAME/DECL for the hash, but equality is 3452 1.1 mrg actually done with operand_equal_p (see tm_memop_eq). */ 3453 1.1 mrg if (TREE_CODE (addr) == ADDR_EXPR) 3454 1.1 mrg addr = TREE_OPERAND (addr, 0); 3455 1.1 mrg return iterative_hash_expr (addr, 0); 3456 1.1 mrg } 3457 1.1 mrg 3458 1.1 mrg /* Htab support. Return true if two tm_memop's are the same. */ 3459 1.1 mrg inline bool 3460 1.1 mrg tm_memop_hasher::equal (const tm_memop *mem1, const tm_memop *mem2) 3461 1.1 mrg { 3462 1.1 mrg return operand_equal_p (mem1->addr, mem2->addr, 0); 3463 1.1 mrg } 3464 1.1 mrg 3465 1.1 mrg /* Sets for solving data flow equations in the memory optimization pass. */ 3466 1.1 mrg struct tm_memopt_bitmaps 3467 1.1 mrg { 3468 1.1 mrg /* Stores available to this BB upon entry. Basically, stores that 3469 1.1 mrg dominate this BB. */ 3470 1.1 mrg bitmap store_avail_in; 3471 1.1 mrg /* Stores available at the end of this BB. */ 3472 1.1 mrg bitmap store_avail_out; 3473 1.1 mrg bitmap store_antic_in; 3474 1.1 mrg bitmap store_antic_out; 3475 1.1 mrg /* Reads available to this BB upon entry. Basically, reads that 3476 1.1 mrg dominate this BB. */ 3477 1.1 mrg bitmap read_avail_in; 3478 1.1 mrg /* Reads available at the end of this BB. */ 3479 1.1 mrg bitmap read_avail_out; 3480 1.1 mrg /* Reads performed in this BB. */ 3481 1.1 mrg bitmap read_local; 3482 1.1 mrg /* Writes performed in this BB. */ 3483 1.1 mrg bitmap store_local; 3484 1.1 mrg 3485 1.1 mrg /* Temporary storage for pass. */ 3486 1.1 mrg /* Is the current BB in the worklist? */ 3487 1.1 mrg bool avail_in_worklist_p; 3488 1.1 mrg /* Have we visited this BB? */ 3489 1.1 mrg bool visited_p; 3490 1.1 mrg }; 3491 1.1 mrg 3492 1.1 mrg static bitmap_obstack tm_memopt_obstack; 3493 1.1 mrg 3494 1.1 mrg /* Unique counter for TM loads and stores. Loads and stores of the 3495 1.1 mrg same address get the same ID. */ 3496 1.1 mrg static unsigned int tm_memopt_value_id; 3497 1.1 mrg static hash_table<tm_memop_hasher> *tm_memopt_value_numbers; 3498 1.1 mrg 3499 1.1 mrg #define STORE_AVAIL_IN(BB) \ 3500 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_in 3501 1.1 mrg #define STORE_AVAIL_OUT(BB) \ 3502 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_avail_out 3503 1.1 mrg #define STORE_ANTIC_IN(BB) \ 3504 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_in 3505 1.1 mrg #define STORE_ANTIC_OUT(BB) \ 3506 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_antic_out 3507 1.1 mrg #define READ_AVAIL_IN(BB) \ 3508 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_in 3509 1.1 mrg #define READ_AVAIL_OUT(BB) \ 3510 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_avail_out 3511 1.1 mrg #define READ_LOCAL(BB) \ 3512 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->read_local 3513 1.1 mrg #define STORE_LOCAL(BB) \ 3514 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->store_local 3515 1.1 mrg #define AVAIL_IN_WORKLIST_P(BB) \ 3516 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->avail_in_worklist_p 3517 1.1 mrg #define BB_VISITED_P(BB) \ 3518 1.1 mrg ((struct tm_memopt_bitmaps *) ((BB)->aux))->visited_p 3519 1.1 mrg 3520 1.1 mrg /* Given a TM load/store in STMT, return the value number for the address 3521 1.1 mrg it accesses. */ 3522 1.1 mrg 3523 1.1 mrg static unsigned int 3524 1.1 mrg tm_memopt_value_number (gimple *stmt, enum insert_option op) 3525 1.1 mrg { 3526 1.1 mrg struct tm_memop tmpmem, *mem; 3527 1.1 mrg tm_memop **slot; 3528 1.1 mrg 3529 1.1 mrg gcc_assert (is_tm_load (stmt) || is_tm_store (stmt)); 3530 1.1 mrg tmpmem.addr = gimple_call_arg (stmt, 0); 3531 1.1 mrg slot = tm_memopt_value_numbers->find_slot (&tmpmem, op); 3532 1.1 mrg if (*slot) 3533 1.1 mrg mem = *slot; 3534 1.1 mrg else if (op == INSERT) 3535 1.1 mrg { 3536 1.1 mrg mem = XNEW (struct tm_memop); 3537 1.1 mrg *slot = mem; 3538 1.1 mrg mem->value_id = tm_memopt_value_id++; 3539 1.1 mrg mem->addr = tmpmem.addr; 3540 1.1 mrg } 3541 1.1 mrg else 3542 1.1 mrg gcc_unreachable (); 3543 1.1 mrg return mem->value_id; 3544 1.1 mrg } 3545 1.1 mrg 3546 1.1 mrg /* Accumulate TM memory operations in BB into STORE_LOCAL and READ_LOCAL. */ 3547 1.1 mrg 3548 1.1 mrg static void 3549 1.1 mrg tm_memopt_accumulate_memops (basic_block bb) 3550 1.1 mrg { 3551 1.1 mrg gimple_stmt_iterator gsi; 3552 1.1 mrg 3553 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 3554 1.1 mrg { 3555 1.1 mrg gimple *stmt = gsi_stmt (gsi); 3556 1.1 mrg bitmap bits; 3557 1.1 mrg unsigned int loc; 3558 1.1 mrg 3559 1.1 mrg if (is_tm_store (stmt)) 3560 1.1 mrg bits = STORE_LOCAL (bb); 3561 1.1 mrg else if (is_tm_load (stmt)) 3562 1.1 mrg bits = READ_LOCAL (bb); 3563 1.1 mrg else 3564 1.1 mrg continue; 3565 1.1 mrg 3566 1.1 mrg loc = tm_memopt_value_number (stmt, INSERT); 3567 1.1 mrg bitmap_set_bit (bits, loc); 3568 1.1 mrg if (dump_file) 3569 1.1 mrg { 3570 1.1 mrg fprintf (dump_file, "TM memopt (%s): value num=%d, BB=%d, addr=", 3571 1.1 mrg is_tm_load (stmt) ? "LOAD" : "STORE", loc, 3572 1.1 mrg gimple_bb (stmt)->index); 3573 1.1 mrg print_generic_expr (dump_file, gimple_call_arg (stmt, 0)); 3574 1.1 mrg fprintf (dump_file, "\n"); 3575 1.1 mrg } 3576 1.1 mrg } 3577 1.1 mrg } 3578 1.1 mrg 3579 1.1 mrg /* Prettily dump one of the memopt sets. BITS is the bitmap to dump. */ 3580 1.1 mrg 3581 1.1 mrg static void 3582 1.1 mrg dump_tm_memopt_set (const char *set_name, bitmap bits) 3583 1.1 mrg { 3584 1.1 mrg unsigned i; 3585 1.1 mrg bitmap_iterator bi; 3586 1.1 mrg const char *comma = ""; 3587 1.1 mrg 3588 1.1 mrg fprintf (dump_file, "TM memopt: %s: [", set_name); 3589 1.1 mrg EXECUTE_IF_SET_IN_BITMAP (bits, 0, i, bi) 3590 1.1 mrg { 3591 1.1 mrg hash_table<tm_memop_hasher>::iterator hi; 3592 1.1 mrg struct tm_memop *mem = NULL; 3593 1.1 mrg 3594 1.1 mrg /* Yeah, yeah, yeah. Whatever. This is just for debugging. */ 3595 1.1 mrg FOR_EACH_HASH_TABLE_ELEMENT (*tm_memopt_value_numbers, mem, tm_memop_t, hi) 3596 1.1 mrg if (mem->value_id == i) 3597 1.1 mrg break; 3598 1.1 mrg gcc_assert (mem->value_id == i); 3599 1.1 mrg fprintf (dump_file, "%s", comma); 3600 1.1 mrg comma = ", "; 3601 1.1 mrg print_generic_expr (dump_file, mem->addr); 3602 1.1 mrg } 3603 1.1 mrg fprintf (dump_file, "]\n"); 3604 1.1 mrg } 3605 1.1 mrg 3606 1.1 mrg /* Prettily dump all of the memopt sets in BLOCKS. */ 3607 1.1 mrg 3608 1.1 mrg static void 3609 1.1 mrg dump_tm_memopt_sets (vec<basic_block> blocks) 3610 1.1 mrg { 3611 1.1 mrg size_t i; 3612 1.1 mrg basic_block bb; 3613 1.1 mrg 3614 1.1 mrg for (i = 0; blocks.iterate (i, &bb); ++i) 3615 1.1 mrg { 3616 1.1 mrg fprintf (dump_file, "------------BB %d---------\n", bb->index); 3617 1.1 mrg dump_tm_memopt_set ("STORE_LOCAL", STORE_LOCAL (bb)); 3618 1.1 mrg dump_tm_memopt_set ("READ_LOCAL", READ_LOCAL (bb)); 3619 1.1 mrg dump_tm_memopt_set ("STORE_AVAIL_IN", STORE_AVAIL_IN (bb)); 3620 1.1 mrg dump_tm_memopt_set ("STORE_AVAIL_OUT", STORE_AVAIL_OUT (bb)); 3621 1.1 mrg dump_tm_memopt_set ("READ_AVAIL_IN", READ_AVAIL_IN (bb)); 3622 1.1 mrg dump_tm_memopt_set ("READ_AVAIL_OUT", READ_AVAIL_OUT (bb)); 3623 1.1 mrg } 3624 1.1 mrg } 3625 1.1 mrg 3626 1.1 mrg /* Compute {STORE,READ}_AVAIL_IN for the basic block BB. */ 3627 1.1 mrg 3628 1.1 mrg static void 3629 1.1 mrg tm_memopt_compute_avin (basic_block bb) 3630 1.1 mrg { 3631 1.1 mrg edge e; 3632 1.1 mrg unsigned ix; 3633 1.1 mrg 3634 1.1 mrg /* Seed with the AVOUT of any predecessor. */ 3635 1.1 mrg for (ix = 0; ix < EDGE_COUNT (bb->preds); ix++) 3636 1.1 mrg { 3637 1.1 mrg e = EDGE_PRED (bb, ix); 3638 1.1 mrg /* Make sure we have already visited this BB, and is thus 3639 1.1 mrg initialized. 3640 1.1 mrg 3641 1.1 mrg If e->src->aux is NULL, this predecessor is actually on an 3642 1.1 mrg enclosing transaction. We only care about the current 3643 1.1 mrg transaction, so ignore it. */ 3644 1.1 mrg if (e->src->aux && BB_VISITED_P (e->src)) 3645 1.1 mrg { 3646 1.1 mrg bitmap_copy (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src)); 3647 1.1 mrg bitmap_copy (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src)); 3648 1.1 mrg break; 3649 1.1 mrg } 3650 1.1 mrg } 3651 1.1 mrg 3652 1.1 mrg for (; ix < EDGE_COUNT (bb->preds); ix++) 3653 1.1 mrg { 3654 1.1 mrg e = EDGE_PRED (bb, ix); 3655 1.1 mrg if (e->src->aux && BB_VISITED_P (e->src)) 3656 1.1 mrg { 3657 1.1 mrg bitmap_and_into (STORE_AVAIL_IN (bb), STORE_AVAIL_OUT (e->src)); 3658 1.1 mrg bitmap_and_into (READ_AVAIL_IN (bb), READ_AVAIL_OUT (e->src)); 3659 1.1 mrg } 3660 1.1 mrg } 3661 1.1 mrg 3662 1.1 mrg BB_VISITED_P (bb) = true; 3663 1.1 mrg } 3664 1.1 mrg 3665 1.1 mrg /* Compute the STORE_ANTIC_IN for the basic block BB. */ 3666 1.1 mrg 3667 1.1 mrg static void 3668 1.1 mrg tm_memopt_compute_antin (basic_block bb) 3669 1.1 mrg { 3670 1.1 mrg edge e; 3671 1.1 mrg unsigned ix; 3672 1.1 mrg 3673 1.1 mrg /* Seed with the ANTIC_OUT of any successor. */ 3674 1.1 mrg for (ix = 0; ix < EDGE_COUNT (bb->succs); ix++) 3675 1.1 mrg { 3676 1.1 mrg e = EDGE_SUCC (bb, ix); 3677 1.1 mrg /* Make sure we have already visited this BB, and is thus 3678 1.1 mrg initialized. */ 3679 1.1 mrg if (BB_VISITED_P (e->dest)) 3680 1.1 mrg { 3681 1.1 mrg bitmap_copy (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest)); 3682 1.1 mrg break; 3683 1.1 mrg } 3684 1.1 mrg } 3685 1.1 mrg 3686 1.1 mrg for (; ix < EDGE_COUNT (bb->succs); ix++) 3687 1.1 mrg { 3688 1.1 mrg e = EDGE_SUCC (bb, ix); 3689 1.1 mrg if (BB_VISITED_P (e->dest)) 3690 1.1 mrg bitmap_and_into (STORE_ANTIC_IN (bb), STORE_ANTIC_OUT (e->dest)); 3691 1.1 mrg } 3692 1.1 mrg 3693 1.1 mrg BB_VISITED_P (bb) = true; 3694 1.1 mrg } 3695 1.1 mrg 3696 1.1 mrg /* Compute the AVAIL sets for every basic block in BLOCKS. 3697 1.1 mrg 3698 1.1 mrg We compute {STORE,READ}_AVAIL_{OUT,IN} as follows: 3699 1.1 mrg 3700 1.1 mrg AVAIL_OUT[bb] = union (AVAIL_IN[bb], LOCAL[bb]) 3701 1.1 mrg AVAIL_IN[bb] = intersect (AVAIL_OUT[predecessors]) 3702 1.1 mrg 3703 1.1 mrg This is basically what we do in lcm's compute_available(), but here 3704 1.1 mrg we calculate two sets of sets (one for STOREs and one for READs), 3705 1.1 mrg and we work on a region instead of the entire CFG. 3706 1.1 mrg 3707 1.1 mrg REGION is the TM region. 3708 1.1 mrg BLOCKS are the basic blocks in the region. */ 3709 1.1 mrg 3710 1.1 mrg static void 3711 1.1 mrg tm_memopt_compute_available (struct tm_region *region, 3712 1.1 mrg vec<basic_block> blocks) 3713 1.1 mrg { 3714 1.1 mrg edge e; 3715 1.1 mrg basic_block *worklist, *qin, *qout, *qend, bb; 3716 1.1 mrg unsigned int qlen, i; 3717 1.1 mrg edge_iterator ei; 3718 1.1 mrg bool changed; 3719 1.1 mrg 3720 1.1 mrg /* Allocate a worklist array/queue. Entries are only added to the 3721 1.1 mrg list if they were not already on the list. So the size is 3722 1.1 mrg bounded by the number of basic blocks in the region. */ 3723 1.1 mrg gcc_assert (!blocks.is_empty ()); 3724 1.1 mrg qlen = blocks.length () - 1; 3725 1.1 mrg qin = qout = worklist = XNEWVEC (basic_block, qlen); 3726 1.1 mrg 3727 1.1 mrg /* Put every block in the region on the worklist. */ 3728 1.1 mrg for (i = 0; blocks.iterate (i, &bb); ++i) 3729 1.1 mrg { 3730 1.1 mrg /* Seed AVAIL_OUT with the LOCAL set. */ 3731 1.1 mrg bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_LOCAL (bb)); 3732 1.1 mrg bitmap_ior_into (READ_AVAIL_OUT (bb), READ_LOCAL (bb)); 3733 1.1 mrg 3734 1.1 mrg AVAIL_IN_WORKLIST_P (bb) = true; 3735 1.1 mrg /* No need to insert the entry block, since it has an AVIN of 3736 1.1 mrg null, and an AVOUT that has already been seeded in. */ 3737 1.1 mrg if (bb != region->entry_block) 3738 1.1 mrg *qin++ = bb; 3739 1.1 mrg } 3740 1.1 mrg 3741 1.1 mrg /* The entry block has been initialized with the local sets. */ 3742 1.1 mrg BB_VISITED_P (region->entry_block) = true; 3743 1.1 mrg 3744 1.1 mrg qin = worklist; 3745 1.1 mrg qend = &worklist[qlen]; 3746 1.1 mrg 3747 1.1 mrg /* Iterate until the worklist is empty. */ 3748 1.1 mrg while (qlen) 3749 1.1 mrg { 3750 1.1 mrg /* Take the first entry off the worklist. */ 3751 1.1 mrg bb = *qout++; 3752 1.1 mrg qlen--; 3753 1.1 mrg 3754 1.1 mrg if (qout >= qend) 3755 1.1 mrg qout = worklist; 3756 1.1 mrg 3757 1.1 mrg /* This block can be added to the worklist again if necessary. */ 3758 1.1 mrg AVAIL_IN_WORKLIST_P (bb) = false; 3759 1.1 mrg tm_memopt_compute_avin (bb); 3760 1.1 mrg 3761 1.1 mrg /* Note: We do not add the LOCAL sets here because we already 3762 1.1 mrg seeded the AVAIL_OUT sets with them. */ 3763 1.1 mrg changed = bitmap_ior_into (STORE_AVAIL_OUT (bb), STORE_AVAIL_IN (bb)); 3764 1.1 mrg changed |= bitmap_ior_into (READ_AVAIL_OUT (bb), READ_AVAIL_IN (bb)); 3765 1.1 mrg if (changed 3766 1.1 mrg && (region->exit_blocks == NULL 3767 1.1 mrg || !bitmap_bit_p (region->exit_blocks, bb->index))) 3768 1.1 mrg /* If the out state of this block changed, then we need to add 3769 1.1 mrg its successors to the worklist if they are not already in. */ 3770 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 3771 1.1 mrg if (!AVAIL_IN_WORKLIST_P (e->dest) 3772 1.1 mrg && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) 3773 1.1 mrg { 3774 1.1 mrg *qin++ = e->dest; 3775 1.1 mrg AVAIL_IN_WORKLIST_P (e->dest) = true; 3776 1.1 mrg qlen++; 3777 1.1 mrg 3778 1.1 mrg if (qin >= qend) 3779 1.1 mrg qin = worklist; 3780 1.1 mrg } 3781 1.1 mrg } 3782 1.1 mrg 3783 1.1 mrg free (worklist); 3784 1.1 mrg 3785 1.1 mrg if (dump_file) 3786 1.1 mrg dump_tm_memopt_sets (blocks); 3787 1.1 mrg } 3788 1.1 mrg 3789 1.1 mrg /* Compute ANTIC sets for every basic block in BLOCKS. 3790 1.1 mrg 3791 1.1 mrg We compute STORE_ANTIC_OUT as follows: 3792 1.1 mrg 3793 1.1 mrg STORE_ANTIC_OUT[bb] = union(STORE_ANTIC_IN[bb], STORE_LOCAL[bb]) 3794 1.1 mrg STORE_ANTIC_IN[bb] = intersect(STORE_ANTIC_OUT[successors]) 3795 1.1 mrg 3796 1.1 mrg REGION is the TM region. 3797 1.1 mrg BLOCKS are the basic blocks in the region. */ 3798 1.1 mrg 3799 1.1 mrg static void 3800 1.1 mrg tm_memopt_compute_antic (struct tm_region *region, 3801 1.1 mrg vec<basic_block> blocks) 3802 1.1 mrg { 3803 1.1 mrg edge e; 3804 1.1 mrg basic_block *worklist, *qin, *qout, *qend, bb; 3805 1.1 mrg unsigned int qlen; 3806 1.1 mrg int i; 3807 1.1 mrg edge_iterator ei; 3808 1.1 mrg 3809 1.1 mrg /* Allocate a worklist array/queue. Entries are only added to the 3810 1.1 mrg list if they were not already on the list. So the size is 3811 1.1 mrg bounded by the number of basic blocks in the region. */ 3812 1.1 mrg qin = qout = worklist = XNEWVEC (basic_block, blocks.length ()); 3813 1.1 mrg 3814 1.1 mrg for (qlen = 0, i = blocks.length () - 1; i >= 0; --i) 3815 1.1 mrg { 3816 1.1 mrg bb = blocks[i]; 3817 1.1 mrg 3818 1.1 mrg /* Seed ANTIC_OUT with the LOCAL set. */ 3819 1.1 mrg bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_LOCAL (bb)); 3820 1.1 mrg 3821 1.1 mrg /* Put every block in the region on the worklist. */ 3822 1.1 mrg AVAIL_IN_WORKLIST_P (bb) = true; 3823 1.1 mrg /* No need to insert exit blocks, since their ANTIC_IN is NULL, 3824 1.1 mrg and their ANTIC_OUT has already been seeded in. */ 3825 1.1 mrg if (region->exit_blocks 3826 1.1 mrg && !bitmap_bit_p (region->exit_blocks, bb->index)) 3827 1.1 mrg { 3828 1.1 mrg qlen++; 3829 1.1 mrg *qin++ = bb; 3830 1.1 mrg } 3831 1.1 mrg } 3832 1.1 mrg 3833 1.1 mrg /* The exit blocks have been initialized with the local sets. */ 3834 1.1 mrg if (region->exit_blocks) 3835 1.1 mrg { 3836 1.1 mrg unsigned int i; 3837 1.1 mrg bitmap_iterator bi; 3838 1.1 mrg EXECUTE_IF_SET_IN_BITMAP (region->exit_blocks, 0, i, bi) 3839 1.1 mrg BB_VISITED_P (BASIC_BLOCK_FOR_FN (cfun, i)) = true; 3840 1.1 mrg } 3841 1.1 mrg 3842 1.1 mrg qin = worklist; 3843 1.1 mrg qend = &worklist[qlen]; 3844 1.1 mrg 3845 1.1 mrg /* Iterate until the worklist is empty. */ 3846 1.1 mrg while (qlen) 3847 1.1 mrg { 3848 1.1 mrg /* Take the first entry off the worklist. */ 3849 1.1 mrg bb = *qout++; 3850 1.1 mrg qlen--; 3851 1.1 mrg 3852 1.1 mrg if (qout >= qend) 3853 1.1 mrg qout = worklist; 3854 1.1 mrg 3855 1.1 mrg /* This block can be added to the worklist again if necessary. */ 3856 1.1 mrg AVAIL_IN_WORKLIST_P (bb) = false; 3857 1.1 mrg tm_memopt_compute_antin (bb); 3858 1.1 mrg 3859 1.1 mrg /* Note: We do not add the LOCAL sets here because we already 3860 1.1 mrg seeded the ANTIC_OUT sets with them. */ 3861 1.1 mrg if (bitmap_ior_into (STORE_ANTIC_OUT (bb), STORE_ANTIC_IN (bb)) 3862 1.1 mrg && bb != region->entry_block) 3863 1.1 mrg /* If the out state of this block changed, then we need to add 3864 1.1 mrg its predecessors to the worklist if they are not already in. */ 3865 1.1 mrg FOR_EACH_EDGE (e, ei, bb->preds) 3866 1.1 mrg if (!AVAIL_IN_WORKLIST_P (e->src)) 3867 1.1 mrg { 3868 1.1 mrg *qin++ = e->src; 3869 1.1 mrg AVAIL_IN_WORKLIST_P (e->src) = true; 3870 1.1 mrg qlen++; 3871 1.1 mrg 3872 1.1 mrg if (qin >= qend) 3873 1.1 mrg qin = worklist; 3874 1.1 mrg } 3875 1.1 mrg } 3876 1.1 mrg 3877 1.1 mrg free (worklist); 3878 1.1 mrg 3879 1.1 mrg if (dump_file) 3880 1.1 mrg dump_tm_memopt_sets (blocks); 3881 1.1 mrg } 3882 1.1 mrg 3883 1.1 mrg /* Offsets of load variants from TM_LOAD. For example, 3884 1.1 mrg BUILT_IN_TM_LOAD_RAR* is an offset of 1 from BUILT_IN_TM_LOAD*. 3885 1.1 mrg See gtm-builtins.def. */ 3886 1.1 mrg #define TRANSFORM_RAR 1 3887 1.1 mrg #define TRANSFORM_RAW 2 3888 1.1 mrg #define TRANSFORM_RFW 3 3889 1.1 mrg /* Offsets of store variants from TM_STORE. */ 3890 1.1 mrg #define TRANSFORM_WAR 1 3891 1.1 mrg #define TRANSFORM_WAW 2 3892 1.1 mrg 3893 1.1 mrg /* Inform about a load/store optimization. */ 3894 1.1 mrg 3895 1.1 mrg static void 3896 1.1 mrg dump_tm_memopt_transform (gimple *stmt) 3897 1.1 mrg { 3898 1.1 mrg if (dump_file) 3899 1.1 mrg { 3900 1.1 mrg fprintf (dump_file, "TM memopt: transforming: "); 3901 1.1 mrg print_gimple_stmt (dump_file, stmt, 0); 3902 1.1 mrg fprintf (dump_file, "\n"); 3903 1.1 mrg } 3904 1.1 mrg } 3905 1.1 mrg 3906 1.1 mrg /* Perform a read/write optimization. Replaces the TM builtin in STMT 3907 1.1 mrg by a builtin that is OFFSET entries down in the builtins table in 3908 1.1 mrg gtm-builtins.def. */ 3909 1.1 mrg 3910 1.1 mrg static void 3911 1.1 mrg tm_memopt_transform_stmt (unsigned int offset, 3912 1.1 mrg gcall *stmt, 3913 1.1 mrg gimple_stmt_iterator *gsi) 3914 1.1 mrg { 3915 1.1 mrg tree fn = gimple_call_fn (stmt); 3916 1.1 mrg gcc_assert (TREE_CODE (fn) == ADDR_EXPR); 3917 1.1 mrg TREE_OPERAND (fn, 0) 3918 1.1 mrg = builtin_decl_explicit ((enum built_in_function) 3919 1.1 mrg (DECL_FUNCTION_CODE (TREE_OPERAND (fn, 0)) 3920 1.1 mrg + offset)); 3921 1.1 mrg gimple_call_set_fn (stmt, fn); 3922 1.1 mrg gsi_replace (gsi, stmt, true); 3923 1.1 mrg dump_tm_memopt_transform (stmt); 3924 1.1 mrg } 3925 1.1 mrg 3926 1.1 mrg /* Perform the actual TM memory optimization transformations in the 3927 1.1 mrg basic blocks in BLOCKS. */ 3928 1.1 mrg 3929 1.1 mrg static void 3930 1.1 mrg tm_memopt_transform_blocks (vec<basic_block> blocks) 3931 1.1 mrg { 3932 1.1 mrg size_t i; 3933 1.1 mrg basic_block bb; 3934 1.1 mrg gimple_stmt_iterator gsi; 3935 1.1 mrg 3936 1.1 mrg for (i = 0; blocks.iterate (i, &bb); ++i) 3937 1.1 mrg { 3938 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 3939 1.1 mrg { 3940 1.1 mrg gimple *stmt = gsi_stmt (gsi); 3941 1.1 mrg bitmap read_avail = READ_AVAIL_IN (bb); 3942 1.1 mrg bitmap store_avail = STORE_AVAIL_IN (bb); 3943 1.1 mrg bitmap store_antic = STORE_ANTIC_OUT (bb); 3944 1.1 mrg unsigned int loc; 3945 1.1 mrg 3946 1.1 mrg if (is_tm_simple_load (stmt)) 3947 1.1 mrg { 3948 1.1 mrg gcall *call_stmt = as_a <gcall *> (stmt); 3949 1.1 mrg loc = tm_memopt_value_number (stmt, NO_INSERT); 3950 1.1 mrg if (store_avail && bitmap_bit_p (store_avail, loc)) 3951 1.1 mrg tm_memopt_transform_stmt (TRANSFORM_RAW, call_stmt, &gsi); 3952 1.1 mrg else if (store_antic && bitmap_bit_p (store_antic, loc)) 3953 1.1 mrg { 3954 1.1 mrg tm_memopt_transform_stmt (TRANSFORM_RFW, call_stmt, &gsi); 3955 1.1 mrg bitmap_set_bit (store_avail, loc); 3956 1.1 mrg } 3957 1.1 mrg else if (read_avail && bitmap_bit_p (read_avail, loc)) 3958 1.1 mrg tm_memopt_transform_stmt (TRANSFORM_RAR, call_stmt, &gsi); 3959 1.1 mrg else 3960 1.1 mrg bitmap_set_bit (read_avail, loc); 3961 1.1 mrg } 3962 1.1 mrg else if (is_tm_simple_store (stmt)) 3963 1.1 mrg { 3964 1.1 mrg gcall *call_stmt = as_a <gcall *> (stmt); 3965 1.1 mrg loc = tm_memopt_value_number (stmt, NO_INSERT); 3966 1.1 mrg if (store_avail && bitmap_bit_p (store_avail, loc)) 3967 1.1 mrg tm_memopt_transform_stmt (TRANSFORM_WAW, call_stmt, &gsi); 3968 1.1 mrg else 3969 1.1 mrg { 3970 1.1 mrg if (read_avail && bitmap_bit_p (read_avail, loc)) 3971 1.1 mrg tm_memopt_transform_stmt (TRANSFORM_WAR, call_stmt, &gsi); 3972 1.1 mrg bitmap_set_bit (store_avail, loc); 3973 1.1 mrg } 3974 1.1 mrg } 3975 1.1 mrg } 3976 1.1 mrg } 3977 1.1 mrg } 3978 1.1 mrg 3979 1.1 mrg /* Return a new set of bitmaps for a BB. */ 3980 1.1 mrg 3981 1.1 mrg static struct tm_memopt_bitmaps * 3982 1.1 mrg tm_memopt_init_sets (void) 3983 1.1 mrg { 3984 1.1 mrg struct tm_memopt_bitmaps *b 3985 1.1 mrg = XOBNEW (&tm_memopt_obstack.obstack, struct tm_memopt_bitmaps); 3986 1.1 mrg b->store_avail_in = BITMAP_ALLOC (&tm_memopt_obstack); 3987 1.1 mrg b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack); 3988 1.1 mrg b->store_antic_in = BITMAP_ALLOC (&tm_memopt_obstack); 3989 1.1 mrg b->store_antic_out = BITMAP_ALLOC (&tm_memopt_obstack); 3990 1.1 mrg b->store_avail_out = BITMAP_ALLOC (&tm_memopt_obstack); 3991 1.1 mrg b->read_avail_in = BITMAP_ALLOC (&tm_memopt_obstack); 3992 1.1 mrg b->read_avail_out = BITMAP_ALLOC (&tm_memopt_obstack); 3993 1.1 mrg b->read_local = BITMAP_ALLOC (&tm_memopt_obstack); 3994 1.1 mrg b->store_local = BITMAP_ALLOC (&tm_memopt_obstack); 3995 1.1 mrg return b; 3996 1.1 mrg } 3997 1.1 mrg 3998 1.1 mrg /* Free sets computed for each BB. */ 3999 1.1 mrg 4000 1.1 mrg static void 4001 1.1 mrg tm_memopt_free_sets (vec<basic_block> blocks) 4002 1.1 mrg { 4003 1.1 mrg size_t i; 4004 1.1 mrg basic_block bb; 4005 1.1 mrg 4006 1.1 mrg for (i = 0; blocks.iterate (i, &bb); ++i) 4007 1.1 mrg bb->aux = NULL; 4008 1.1 mrg } 4009 1.1 mrg 4010 1.1 mrg /* Clear the visited bit for every basic block in BLOCKS. */ 4011 1.1 mrg 4012 1.1 mrg static void 4013 1.1 mrg tm_memopt_clear_visited (vec<basic_block> blocks) 4014 1.1 mrg { 4015 1.1 mrg size_t i; 4016 1.1 mrg basic_block bb; 4017 1.1 mrg 4018 1.1 mrg for (i = 0; blocks.iterate (i, &bb); ++i) 4019 1.1 mrg BB_VISITED_P (bb) = false; 4020 1.1 mrg } 4021 1.1 mrg 4022 1.1 mrg /* Replace TM load/stores with hints for the runtime. We handle 4023 1.1 mrg things like read-after-write, write-after-read, read-after-read, 4024 1.1 mrg read-for-write, etc. */ 4025 1.1 mrg 4026 1.1 mrg static unsigned int 4027 1.1 mrg execute_tm_memopt (void) 4028 1.1 mrg { 4029 1.1 mrg struct tm_region *region; 4030 1.1 mrg vec<basic_block> bbs; 4031 1.1 mrg 4032 1.1 mrg tm_memopt_value_id = 0; 4033 1.1 mrg tm_memopt_value_numbers = new hash_table<tm_memop_hasher> (10); 4034 1.1 mrg 4035 1.1 mrg for (region = all_tm_regions; region; region = region->next) 4036 1.1 mrg { 4037 1.1 mrg /* All the TM stores/loads in the current region. */ 4038 1.1 mrg size_t i; 4039 1.1 mrg basic_block bb; 4040 1.1 mrg 4041 1.1 mrg bitmap_obstack_initialize (&tm_memopt_obstack); 4042 1.1 mrg 4043 1.1 mrg /* Save all BBs for the current region. */ 4044 1.1 mrg bbs = get_tm_region_blocks (region->entry_block, 4045 1.1 mrg region->exit_blocks, 4046 1.1 mrg region->irr_blocks, 4047 1.1 mrg NULL, 4048 1.1 mrg false); 4049 1.1 mrg 4050 1.1 mrg /* Collect all the memory operations. */ 4051 1.1 mrg for (i = 0; bbs.iterate (i, &bb); ++i) 4052 1.1 mrg { 4053 1.1 mrg bb->aux = tm_memopt_init_sets (); 4054 1.1 mrg tm_memopt_accumulate_memops (bb); 4055 1.1 mrg } 4056 1.1 mrg 4057 1.1 mrg /* Solve data flow equations and transform each block accordingly. */ 4058 1.1 mrg tm_memopt_clear_visited (bbs); 4059 1.1 mrg tm_memopt_compute_available (region, bbs); 4060 1.1 mrg tm_memopt_clear_visited (bbs); 4061 1.1 mrg tm_memopt_compute_antic (region, bbs); 4062 1.1 mrg tm_memopt_transform_blocks (bbs); 4063 1.1 mrg 4064 1.1 mrg tm_memopt_free_sets (bbs); 4065 1.1 mrg bbs.release (); 4066 1.1 mrg bitmap_obstack_release (&tm_memopt_obstack); 4067 1.1 mrg tm_memopt_value_numbers->empty (); 4068 1.1 mrg } 4069 1.1 mrg 4070 1.1 mrg delete tm_memopt_value_numbers; 4071 1.1 mrg tm_memopt_value_numbers = NULL; 4072 1.1 mrg return 0; 4073 1.1 mrg } 4074 1.1 mrg 4075 1.1 mrg namespace { 4076 1.1 mrg 4077 1.1 mrg const pass_data pass_data_tm_memopt = 4078 1.1 mrg { 4079 1.1 mrg GIMPLE_PASS, /* type */ 4080 1.1 mrg "tmmemopt", /* name */ 4081 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 4082 1.1 mrg TV_TRANS_MEM, /* tv_id */ 4083 1.1 mrg ( PROP_ssa | PROP_cfg ), /* properties_required */ 4084 1.1 mrg 0, /* properties_provided */ 4085 1.1 mrg 0, /* properties_destroyed */ 4086 1.1 mrg 0, /* todo_flags_start */ 4087 1.1 mrg 0, /* todo_flags_finish */ 4088 1.1 mrg }; 4089 1.1 mrg 4090 1.1 mrg class pass_tm_memopt : public gimple_opt_pass 4091 1.1 mrg { 4092 1.1 mrg public: 4093 1.1 mrg pass_tm_memopt (gcc::context *ctxt) 4094 1.1 mrg : gimple_opt_pass (pass_data_tm_memopt, ctxt) 4095 1.1 mrg {} 4096 1.1 mrg 4097 1.1 mrg /* opt_pass methods: */ 4098 1.1 mrg virtual bool gate (function *) { return flag_tm && optimize > 0; } 4099 1.1 mrg virtual unsigned int execute (function *) { return execute_tm_memopt (); } 4100 1.1 mrg 4101 1.1 mrg }; // class pass_tm_memopt 4102 1.1 mrg 4103 1.1 mrg } // anon namespace 4104 1.1 mrg 4105 1.1 mrg gimple_opt_pass * 4106 1.1 mrg make_pass_tm_memopt (gcc::context *ctxt) 4107 1.1 mrg { 4108 1.1 mrg return new pass_tm_memopt (ctxt); 4109 1.1 mrg } 4110 1.1 mrg 4111 1.1 mrg 4112 1.1 mrg /* Interprocedual analysis for the creation of transactional clones. 4114 1.1 mrg The aim of this pass is to find which functions are referenced in 4115 1.1 mrg a non-irrevocable transaction context, and for those over which 4116 1.1 mrg we have control (or user directive), create a version of the 4117 1.1 mrg function which uses only the transactional interface to reference 4118 1.1 mrg protected memories. This analysis proceeds in several steps: 4119 1.1 mrg 4120 1.1 mrg (1) Collect the set of all possible transactional clones: 4121 1.1 mrg 4122 1.1 mrg (a) For all local public functions marked tm_callable, push 4123 1.1 mrg it onto the tm_callee queue. 4124 1.1 mrg 4125 1.1 mrg (b) For all local functions, scan for calls in transaction blocks. 4126 1.1 mrg Push the caller and callee onto the tm_caller and tm_callee 4127 1.1 mrg queues. Count the number of callers for each callee. 4128 1.1 mrg 4129 1.1 mrg (c) For each local function on the callee list, assume we will 4130 1.1 mrg create a transactional clone. Push *all* calls onto the 4131 1.1 mrg callee queues; count the number of clone callers separately 4132 1.1 mrg to the number of original callers. 4133 1.1 mrg 4134 1.1 mrg (2) Propagate irrevocable status up the dominator tree: 4135 1.1 mrg 4136 1.1 mrg (a) Any external function on the callee list that is not marked 4137 1.1 mrg tm_callable is irrevocable. Push all callers of such onto 4138 1.1 mrg a worklist. 4139 1.1 mrg 4140 1.1 mrg (b) For each function on the worklist, mark each block that 4141 1.1 mrg contains an irrevocable call. Use the AND operator to 4142 1.1 mrg propagate that mark up the dominator tree. 4143 1.1 mrg 4144 1.1 mrg (c) If we reach the entry block for a possible transactional 4145 1.1 mrg clone, then the transactional clone is irrevocable, and 4146 1.1 mrg we should not create the clone after all. Push all 4147 1.1 mrg callers onto the worklist. 4148 1.1 mrg 4149 1.1 mrg (d) Place tm_irrevocable calls at the beginning of the relevant 4150 1.1 mrg blocks. Special case here is the entry block for the entire 4151 1.1 mrg transaction region; there we mark it GTMA_DOES_GO_IRREVOCABLE for 4152 1.1 mrg the library to begin the region in serial mode. Decrement 4153 1.1 mrg the call count for all callees in the irrevocable region. 4154 1.1 mrg 4155 1.1 mrg (3) Create the transactional clones: 4156 1.1 mrg 4157 1.1 mrg Any tm_callee that still has a non-zero call count is cloned. 4158 1.1 mrg */ 4159 1.1 mrg 4160 1.1 mrg /* This structure is stored in the AUX field of each cgraph_node. */ 4161 1.1 mrg struct tm_ipa_cg_data 4162 1.1 mrg { 4163 1.1 mrg /* The clone of the function that got created. */ 4164 1.1 mrg struct cgraph_node *clone; 4165 1.1 mrg 4166 1.1 mrg /* The tm regions in the normal function. */ 4167 1.1 mrg struct tm_region *all_tm_regions; 4168 1.1 mrg 4169 1.1 mrg /* The blocks of the normal/clone functions that contain irrevocable 4170 1.1 mrg calls, or blocks that are post-dominated by irrevocable calls. */ 4171 1.1 mrg bitmap irrevocable_blocks_normal; 4172 1.1 mrg bitmap irrevocable_blocks_clone; 4173 1.1 mrg 4174 1.1 mrg /* The blocks of the normal function that are involved in transactions. */ 4175 1.1 mrg bitmap transaction_blocks_normal; 4176 1.1 mrg 4177 1.1 mrg /* The number of callers to the transactional clone of this function 4178 1.1 mrg from normal and transactional clones respectively. */ 4179 1.1 mrg unsigned tm_callers_normal; 4180 1.1 mrg unsigned tm_callers_clone; 4181 1.1 mrg 4182 1.1 mrg /* True if all calls to this function's transactional clone 4183 1.1 mrg are irrevocable. Also automatically true if the function 4184 1.1 mrg has no transactional clone. */ 4185 1.1 mrg bool is_irrevocable; 4186 1.1 mrg 4187 1.1 mrg /* Flags indicating the presence of this function in various queues. */ 4188 1.1 mrg bool in_callee_queue; 4189 1.1 mrg bool in_worklist; 4190 1.1 mrg 4191 1.1 mrg /* Flags indicating the kind of scan desired while in the worklist. */ 4192 1.1 mrg bool want_irr_scan_normal; 4193 1.1 mrg }; 4194 1.1 mrg 4195 1.1 mrg typedef vec<cgraph_node *> cgraph_node_queue; 4196 1.1 mrg 4197 1.1 mrg /* Return the ipa data associated with NODE, allocating zeroed memory 4198 1.1 mrg if necessary. TRAVERSE_ALIASES is true if we must traverse aliases 4199 1.1 mrg and set *NODE accordingly. */ 4200 1.1 mrg 4201 1.1 mrg static struct tm_ipa_cg_data * 4202 1.1 mrg get_cg_data (struct cgraph_node **node, bool traverse_aliases) 4203 1.1 mrg { 4204 1.1 mrg struct tm_ipa_cg_data *d; 4205 1.1 mrg 4206 1.1 mrg if (traverse_aliases && (*node)->alias) 4207 1.1 mrg *node = (*node)->get_alias_target (); 4208 1.1 mrg 4209 1.1 mrg d = (struct tm_ipa_cg_data *) (*node)->aux; 4210 1.1 mrg 4211 1.1 mrg if (d == NULL) 4212 1.1 mrg { 4213 1.1 mrg d = (struct tm_ipa_cg_data *) 4214 1.1 mrg obstack_alloc (&tm_obstack.obstack, sizeof (*d)); 4215 1.1 mrg (*node)->aux = (void *) d; 4216 1.1 mrg memset (d, 0, sizeof (*d)); 4217 1.1 mrg } 4218 1.1 mrg 4219 1.1 mrg return d; 4220 1.1 mrg } 4221 1.1 mrg 4222 1.1 mrg /* Add NODE to the end of QUEUE, unless IN_QUEUE_P indicates that 4223 1.1 mrg it is already present. */ 4224 1.1 mrg 4225 1.1 mrg static void 4226 1.1 mrg maybe_push_queue (struct cgraph_node *node, 4227 1.1 mrg cgraph_node_queue *queue_p, bool *in_queue_p) 4228 1.1 mrg { 4229 1.1 mrg if (!*in_queue_p) 4230 1.1 mrg { 4231 1.1 mrg *in_queue_p = true; 4232 1.1 mrg queue_p->safe_push (node); 4233 1.1 mrg } 4234 1.1 mrg } 4235 1.1 mrg 4236 1.1 mrg /* A subroutine of ipa_tm_scan_calls_transaction and ipa_tm_scan_calls_clone. 4237 1.1 mrg Queue all callees within block BB. */ 4238 1.1 mrg 4239 1.1 mrg static void 4240 1.1 mrg ipa_tm_scan_calls_block (cgraph_node_queue *callees_p, 4241 1.1 mrg basic_block bb, bool for_clone) 4242 1.1 mrg { 4243 1.1 mrg gimple_stmt_iterator gsi; 4244 1.1 mrg 4245 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 4246 1.1 mrg { 4247 1.1 mrg gimple *stmt = gsi_stmt (gsi); 4248 1.1 mrg if (is_gimple_call (stmt) && !is_tm_pure_call (stmt)) 4249 1.1 mrg { 4250 1.1 mrg tree fndecl = gimple_call_fndecl (stmt); 4251 1.1 mrg if (fndecl) 4252 1.1 mrg { 4253 1.1 mrg struct tm_ipa_cg_data *d; 4254 1.1 mrg unsigned *pcallers; 4255 1.1 mrg struct cgraph_node *node; 4256 1.1 mrg 4257 1.1 mrg if (is_tm_ending_fndecl (fndecl)) 4258 1.1 mrg continue; 4259 1.1 mrg if (find_tm_replacement_function (fndecl)) 4260 1.1 mrg continue; 4261 1.1 mrg 4262 1.1 mrg node = cgraph_node::get (fndecl); 4263 1.1 mrg gcc_assert (node != NULL); 4264 1.1 mrg d = get_cg_data (&node, true); 4265 1.1 mrg 4266 1.1 mrg pcallers = (for_clone ? &d->tm_callers_clone 4267 1.1 mrg : &d->tm_callers_normal); 4268 1.1 mrg *pcallers += 1; 4269 1.1 mrg 4270 1.1 mrg maybe_push_queue (node, callees_p, &d->in_callee_queue); 4271 1.1 mrg } 4272 1.1 mrg } 4273 1.1 mrg } 4274 1.1 mrg } 4275 1.1 mrg 4276 1.1 mrg /* Scan all calls in NODE that are within a transaction region, 4277 1.1 mrg and push the resulting nodes into the callee queue. */ 4278 1.1 mrg 4279 1.1 mrg static void 4280 1.1 mrg ipa_tm_scan_calls_transaction (struct tm_ipa_cg_data *d, 4281 1.1 mrg cgraph_node_queue *callees_p) 4282 1.1 mrg { 4283 1.1 mrg d->transaction_blocks_normal = BITMAP_ALLOC (&tm_obstack); 4284 1.1 mrg d->all_tm_regions = all_tm_regions; 4285 1.1 mrg 4286 1.1 mrg for (tm_region *r = all_tm_regions; r; r = r->next) 4287 1.1 mrg { 4288 1.1 mrg vec<basic_block> bbs; 4289 1.1 mrg basic_block bb; 4290 1.1 mrg unsigned i; 4291 1.1 mrg 4292 1.1 mrg bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks, NULL, 4293 1.1 mrg d->transaction_blocks_normal, false, false); 4294 1.1 mrg 4295 1.1 mrg FOR_EACH_VEC_ELT (bbs, i, bb) 4296 1.1 mrg ipa_tm_scan_calls_block (callees_p, bb, false); 4297 1.1 mrg 4298 1.1 mrg bbs.release (); 4299 1.1 mrg } 4300 1.1 mrg } 4301 1.1 mrg 4302 1.1 mrg /* Scan all calls in NODE as if this is the transactional clone, 4303 1.1 mrg and push the destinations into the callee queue. */ 4304 1.1 mrg 4305 1.1 mrg static void 4306 1.1 mrg ipa_tm_scan_calls_clone (struct cgraph_node *node, 4307 1.1 mrg cgraph_node_queue *callees_p) 4308 1.1 mrg { 4309 1.1 mrg struct function *fn = DECL_STRUCT_FUNCTION (node->decl); 4310 1.1 mrg basic_block bb; 4311 1.1 mrg 4312 1.1 mrg FOR_EACH_BB_FN (bb, fn) 4313 1.1 mrg ipa_tm_scan_calls_block (callees_p, bb, true); 4314 1.1 mrg } 4315 1.1 mrg 4316 1.1 mrg /* The function NODE has been detected to be irrevocable. Push all 4317 1.1 mrg of its callers onto WORKLIST for the purpose of re-scanning them. */ 4318 1.1 mrg 4319 1.1 mrg static void 4320 1.1 mrg ipa_tm_note_irrevocable (struct cgraph_node *node, 4321 1.1 mrg cgraph_node_queue *worklist_p) 4322 1.1 mrg { 4323 1.1 mrg struct tm_ipa_cg_data *d = get_cg_data (&node, true); 4324 1.1 mrg struct cgraph_edge *e; 4325 1.1 mrg 4326 1.1 mrg d->is_irrevocable = true; 4327 1.1 mrg 4328 1.1 mrg for (e = node->callers; e ; e = e->next_caller) 4329 1.1 mrg { 4330 1.1 mrg basic_block bb; 4331 1.1 mrg struct cgraph_node *caller; 4332 1.1 mrg 4333 1.1 mrg /* Don't examine recursive calls. */ 4334 1.1 mrg if (e->caller == node) 4335 1.1 mrg continue; 4336 1.1 mrg /* Even if we think we can go irrevocable, believe the user 4337 1.1 mrg above all. */ 4338 1.1 mrg if (is_tm_safe_or_pure (e->caller->decl)) 4339 1.1 mrg continue; 4340 1.1 mrg 4341 1.1 mrg caller = e->caller; 4342 1.1 mrg d = get_cg_data (&caller, true); 4343 1.1 mrg 4344 1.1 mrg /* Check if the callee is in a transactional region. If so, 4345 1.1 mrg schedule the function for normal re-scan as well. */ 4346 1.1 mrg bb = gimple_bb (e->call_stmt); 4347 1.1 mrg gcc_assert (bb != NULL); 4348 1.1 mrg if (d->transaction_blocks_normal 4349 1.1 mrg && bitmap_bit_p (d->transaction_blocks_normal, bb->index)) 4350 1.1 mrg d->want_irr_scan_normal = true; 4351 1.1 mrg 4352 1.1 mrg maybe_push_queue (caller, worklist_p, &d->in_worklist); 4353 1.1 mrg } 4354 1.1 mrg } 4355 1.1 mrg 4356 1.1 mrg /* A subroutine of ipa_tm_scan_irr_blocks; return true iff any statement 4357 1.1 mrg within the block is irrevocable. */ 4358 1.1 mrg 4359 1.1 mrg static bool 4360 1.1 mrg ipa_tm_scan_irr_block (basic_block bb) 4361 1.1 mrg { 4362 1.1 mrg gimple_stmt_iterator gsi; 4363 1.1 mrg tree fn; 4364 1.1 mrg 4365 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 4366 1.1 mrg { 4367 1.1 mrg gimple *stmt = gsi_stmt (gsi); 4368 1.1 mrg switch (gimple_code (stmt)) 4369 1.1 mrg { 4370 1.1 mrg case GIMPLE_ASSIGN: 4371 1.1 mrg if (gimple_assign_single_p (stmt)) 4372 1.1 mrg { 4373 1.1 mrg tree lhs = gimple_assign_lhs (stmt); 4374 1.1 mrg tree rhs = gimple_assign_rhs1 (stmt); 4375 1.1 mrg if (volatile_lvalue_p (lhs) || volatile_lvalue_p (rhs)) 4376 1.1 mrg return true; 4377 1.1 mrg } 4378 1.1 mrg break; 4379 1.1 mrg 4380 1.1 mrg case GIMPLE_CALL: 4381 1.1 mrg { 4382 1.1 mrg tree lhs = gimple_call_lhs (stmt); 4383 1.1 mrg if (lhs && volatile_lvalue_p (lhs)) 4384 1.1 mrg return true; 4385 1.1 mrg 4386 1.1 mrg if (is_tm_pure_call (stmt)) 4387 1.1 mrg break; 4388 1.1 mrg 4389 1.1 mrg fn = gimple_call_fn (stmt); 4390 1.1 mrg 4391 1.1 mrg /* Functions with the attribute are by definition irrevocable. */ 4392 1.1 mrg if (is_tm_irrevocable (fn)) 4393 1.1 mrg return true; 4394 1.1 mrg 4395 1.1 mrg /* For direct function calls, go ahead and check for replacement 4396 1.1 mrg functions, or transitive irrevocable functions. For indirect 4397 1.1 mrg functions, we'll ask the runtime. */ 4398 1.1 mrg if (TREE_CODE (fn) == ADDR_EXPR) 4399 1.1 mrg { 4400 1.1 mrg struct tm_ipa_cg_data *d; 4401 1.1 mrg struct cgraph_node *node; 4402 1.1 mrg 4403 1.1 mrg fn = TREE_OPERAND (fn, 0); 4404 1.1 mrg if (is_tm_ending_fndecl (fn)) 4405 1.1 mrg break; 4406 1.1 mrg if (find_tm_replacement_function (fn)) 4407 1.1 mrg break; 4408 1.1 mrg 4409 1.1 mrg node = cgraph_node::get (fn); 4410 1.1 mrg d = get_cg_data (&node, true); 4411 1.1 mrg 4412 1.1 mrg /* Return true if irrevocable, but above all, believe 4413 1.1 mrg the user. */ 4414 1.1 mrg if (d->is_irrevocable 4415 1.1 mrg && !is_tm_safe_or_pure (fn)) 4416 1.1 mrg return true; 4417 1.1 mrg } 4418 1.1 mrg break; 4419 1.1 mrg } 4420 1.1 mrg 4421 1.1 mrg case GIMPLE_ASM: 4422 1.1 mrg /* ??? The Approved Method of indicating that an inline 4423 1.1 mrg assembly statement is not relevant to the transaction 4424 1.1 mrg is to wrap it in a __tm_waiver block. This is not 4425 1.1 mrg yet implemented, so we can't check for it. */ 4426 1.1 mrg if (is_tm_safe (current_function_decl)) 4427 1.1 mrg error_at (gimple_location (stmt), 4428 1.1 mrg "%<asm%> not allowed in %<transaction_safe%> function"); 4429 1.1 mrg return true; 4430 1.1 mrg 4431 1.1 mrg default: 4432 1.1 mrg break; 4433 1.1 mrg } 4434 1.1 mrg } 4435 1.1 mrg 4436 1.1 mrg return false; 4437 1.1 mrg } 4438 1.1 mrg 4439 1.1 mrg /* For each of the blocks seeded witin PQUEUE, walk the CFG looking 4440 1.1 mrg for new irrevocable blocks, marking them in NEW_IRR. Don't bother 4441 1.1 mrg scanning past OLD_IRR or EXIT_BLOCKS. */ 4442 1.1 mrg 4443 1.1 mrg static bool 4444 1.1 mrg ipa_tm_scan_irr_blocks (vec<basic_block> *pqueue, bitmap new_irr, 4445 1.1 mrg bitmap old_irr, bitmap exit_blocks) 4446 1.1 mrg { 4447 1.1 mrg bool any_new_irr = false; 4448 1.1 mrg edge e; 4449 1.1 mrg edge_iterator ei; 4450 1.1 mrg bitmap visited_blocks = BITMAP_ALLOC (NULL); 4451 1.1 mrg 4452 1.1 mrg do 4453 1.1 mrg { 4454 1.1 mrg basic_block bb = pqueue->pop (); 4455 1.1 mrg 4456 1.1 mrg /* Don't re-scan blocks we know already are irrevocable. */ 4457 1.1 mrg if (old_irr && bitmap_bit_p (old_irr, bb->index)) 4458 1.1 mrg continue; 4459 1.1 mrg 4460 1.1 mrg if (ipa_tm_scan_irr_block (bb)) 4461 1.1 mrg { 4462 1.1 mrg bitmap_set_bit (new_irr, bb->index); 4463 1.1 mrg any_new_irr = true; 4464 1.1 mrg } 4465 1.1 mrg else if (exit_blocks == NULL || !bitmap_bit_p (exit_blocks, bb->index)) 4466 1.1 mrg { 4467 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 4468 1.1 mrg if (!bitmap_bit_p (visited_blocks, e->dest->index)) 4469 1.1 mrg { 4470 1.1 mrg bitmap_set_bit (visited_blocks, e->dest->index); 4471 1.1 mrg pqueue->safe_push (e->dest); 4472 1.1 mrg } 4473 1.1 mrg } 4474 1.1 mrg } 4475 1.1 mrg while (!pqueue->is_empty ()); 4476 1.1 mrg 4477 1.1 mrg BITMAP_FREE (visited_blocks); 4478 1.1 mrg 4479 1.1 mrg return any_new_irr; 4480 1.1 mrg } 4481 1.1 mrg 4482 1.1 mrg /* Propagate the irrevocable property both up and down the dominator tree. 4483 1.1 mrg BB is the current block being scanned; EXIT_BLOCKS are the edges of the 4484 1.1 mrg TM regions; OLD_IRR are the results of a previous scan of the dominator 4485 1.1 mrg tree which has been fully propagated; NEW_IRR is the set of new blocks 4486 1.1 mrg which are gaining the irrevocable property during the current scan. */ 4487 1.1 mrg 4488 1.1 mrg static void 4489 1.1 mrg ipa_tm_propagate_irr (basic_block entry_block, bitmap new_irr, 4490 1.1 mrg bitmap old_irr, bitmap exit_blocks) 4491 1.1 mrg { 4492 1.1 mrg vec<basic_block> bbs; 4493 1.1 mrg bitmap all_region_blocks; 4494 1.1 mrg 4495 1.1 mrg /* If this block is in the old set, no need to rescan. */ 4496 1.1 mrg if (old_irr && bitmap_bit_p (old_irr, entry_block->index)) 4497 1.1 mrg return; 4498 1.1 mrg 4499 1.1 mrg all_region_blocks = BITMAP_ALLOC (&tm_obstack); 4500 1.1 mrg bbs = get_tm_region_blocks (entry_block, exit_blocks, NULL, 4501 1.1 mrg all_region_blocks, false); 4502 1.1 mrg do 4503 1.1 mrg { 4504 1.1 mrg basic_block bb = bbs.pop (); 4505 1.1 mrg bool this_irr = bitmap_bit_p (new_irr, bb->index); 4506 1.1 mrg bool all_son_irr = false; 4507 1.1 mrg edge_iterator ei; 4508 1.1 mrg edge e; 4509 1.1 mrg 4510 1.1 mrg /* Propagate up. If my children are, I am too, but we must have 4511 1.1 mrg at least one child that is. */ 4512 1.1 mrg if (!this_irr) 4513 1.1 mrg { 4514 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 4515 1.1 mrg { 4516 1.1 mrg if (!bitmap_bit_p (new_irr, e->dest->index)) 4517 1.1 mrg { 4518 1.1 mrg all_son_irr = false; 4519 1.1 mrg break; 4520 1.1 mrg } 4521 1.1 mrg else 4522 1.1 mrg all_son_irr = true; 4523 1.1 mrg } 4524 1.1 mrg if (all_son_irr) 4525 1.1 mrg { 4526 1.1 mrg /* Add block to new_irr if it hasn't already been processed. */ 4527 1.1 mrg if (!old_irr || !bitmap_bit_p (old_irr, bb->index)) 4528 1.1 mrg { 4529 1.1 mrg bitmap_set_bit (new_irr, bb->index); 4530 1.1 mrg this_irr = true; 4531 1.1 mrg } 4532 1.1 mrg } 4533 1.1 mrg } 4534 1.1 mrg 4535 1.1 mrg /* Propagate down to everyone we immediately dominate. */ 4536 1.1 mrg if (this_irr) 4537 1.1 mrg { 4538 1.1 mrg basic_block son; 4539 1.1 mrg for (son = first_dom_son (CDI_DOMINATORS, bb); 4540 1.1 mrg son; 4541 1.1 mrg son = next_dom_son (CDI_DOMINATORS, son)) 4542 1.1 mrg { 4543 1.1 mrg /* Make sure block is actually in a TM region, and it 4544 1.1 mrg isn't already in old_irr. */ 4545 1.1 mrg if ((!old_irr || !bitmap_bit_p (old_irr, son->index)) 4546 1.1 mrg && bitmap_bit_p (all_region_blocks, son->index)) 4547 1.1 mrg bitmap_set_bit (new_irr, son->index); 4548 1.1 mrg } 4549 1.1 mrg } 4550 1.1 mrg } 4551 1.1 mrg while (!bbs.is_empty ()); 4552 1.1 mrg 4553 1.1 mrg BITMAP_FREE (all_region_blocks); 4554 1.1 mrg bbs.release (); 4555 1.1 mrg } 4556 1.1 mrg 4557 1.1 mrg static void 4558 1.1 mrg ipa_tm_decrement_clone_counts (basic_block bb, bool for_clone) 4559 1.1 mrg { 4560 1.1 mrg gimple_stmt_iterator gsi; 4561 1.1 mrg 4562 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 4563 1.1 mrg { 4564 1.1 mrg gimple *stmt = gsi_stmt (gsi); 4565 1.1 mrg if (is_gimple_call (stmt) && !is_tm_pure_call (stmt)) 4566 1.1 mrg { 4567 1.1 mrg tree fndecl = gimple_call_fndecl (stmt); 4568 1.1 mrg if (fndecl) 4569 1.1 mrg { 4570 1.1 mrg struct tm_ipa_cg_data *d; 4571 1.1 mrg unsigned *pcallers; 4572 1.1 mrg struct cgraph_node *tnode; 4573 1.1 mrg 4574 1.1 mrg if (is_tm_ending_fndecl (fndecl)) 4575 1.1 mrg continue; 4576 1.1 mrg if (find_tm_replacement_function (fndecl)) 4577 1.1 mrg continue; 4578 1.1 mrg 4579 1.1 mrg tnode = cgraph_node::get (fndecl); 4580 1.1 mrg d = get_cg_data (&tnode, true); 4581 1.1 mrg 4582 1.1 mrg pcallers = (for_clone ? &d->tm_callers_clone 4583 1.1 mrg : &d->tm_callers_normal); 4584 1.1 mrg 4585 1.1 mrg gcc_assert (*pcallers > 0); 4586 1.1 mrg *pcallers -= 1; 4587 1.1 mrg } 4588 1.1 mrg } 4589 1.1 mrg } 4590 1.1 mrg } 4591 1.1 mrg 4592 1.1 mrg /* (Re-)Scan the transaction blocks in NODE for calls to irrevocable functions, 4593 1.1 mrg as well as other irrevocable actions such as inline assembly. Mark all 4594 1.1 mrg such blocks as irrevocable and decrement the number of calls to 4595 1.1 mrg transactional clones. Return true if, for the transactional clone, the 4596 1.1 mrg entire function is irrevocable. */ 4597 1.1 mrg 4598 1.1 mrg static bool 4599 1.1 mrg ipa_tm_scan_irr_function (struct cgraph_node *node, bool for_clone) 4600 1.1 mrg { 4601 1.1 mrg struct tm_ipa_cg_data *d; 4602 1.1 mrg bitmap new_irr, old_irr; 4603 1.1 mrg bool ret = false; 4604 1.1 mrg 4605 1.1 mrg /* Builtin operators (operator new, and such). */ 4606 1.1 mrg if (DECL_STRUCT_FUNCTION (node->decl) == NULL 4607 1.1 mrg || DECL_STRUCT_FUNCTION (node->decl)->cfg == NULL) 4608 1.1 mrg return false; 4609 1.1 mrg 4610 1.1 mrg push_cfun (DECL_STRUCT_FUNCTION (node->decl)); 4611 1.1 mrg calculate_dominance_info (CDI_DOMINATORS); 4612 1.1 mrg 4613 1.1 mrg d = get_cg_data (&node, true); 4614 1.1 mrg auto_vec<basic_block, 10> queue; 4615 1.1 mrg new_irr = BITMAP_ALLOC (&tm_obstack); 4616 1.1 mrg 4617 1.1 mrg /* Scan each tm region, propagating irrevocable status through the tree. */ 4618 1.1 mrg if (for_clone) 4619 1.1 mrg { 4620 1.1 mrg old_irr = d->irrevocable_blocks_clone; 4621 1.1 mrg queue.quick_push (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))); 4622 1.1 mrg if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr, NULL)) 4623 1.1 mrg { 4624 1.1 mrg ipa_tm_propagate_irr (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), 4625 1.1 mrg new_irr, 4626 1.1 mrg old_irr, NULL); 4627 1.1 mrg ret = bitmap_bit_p (new_irr, 4628 1.1 mrg single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))->index); 4629 1.1 mrg } 4630 1.1 mrg } 4631 1.1 mrg else 4632 1.1 mrg { 4633 1.1 mrg struct tm_region *region; 4634 1.1 mrg 4635 1.1 mrg old_irr = d->irrevocable_blocks_normal; 4636 1.1 mrg for (region = d->all_tm_regions; region; region = region->next) 4637 1.1 mrg { 4638 1.1 mrg queue.quick_push (region->entry_block); 4639 1.1 mrg if (ipa_tm_scan_irr_blocks (&queue, new_irr, old_irr, 4640 1.1 mrg region->exit_blocks)) 4641 1.1 mrg ipa_tm_propagate_irr (region->entry_block, new_irr, old_irr, 4642 1.1 mrg region->exit_blocks); 4643 1.1 mrg } 4644 1.1 mrg } 4645 1.1 mrg 4646 1.1 mrg /* If we found any new irrevocable blocks, reduce the call count for 4647 1.1 mrg transactional clones within the irrevocable blocks. Save the new 4648 1.1 mrg set of irrevocable blocks for next time. */ 4649 1.1 mrg if (!bitmap_empty_p (new_irr)) 4650 1.1 mrg { 4651 1.1 mrg bitmap_iterator bmi; 4652 1.1 mrg unsigned i; 4653 1.1 mrg 4654 1.1 mrg EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi) 4655 1.1 mrg ipa_tm_decrement_clone_counts (BASIC_BLOCK_FOR_FN (cfun, i), 4656 1.1 mrg for_clone); 4657 1.1 mrg 4658 1.1 mrg if (old_irr) 4659 1.1 mrg { 4660 1.1 mrg bitmap_ior_into (old_irr, new_irr); 4661 1.1 mrg BITMAP_FREE (new_irr); 4662 1.1 mrg } 4663 1.1 mrg else if (for_clone) 4664 1.1 mrg d->irrevocable_blocks_clone = new_irr; 4665 1.1 mrg else 4666 1.1 mrg d->irrevocable_blocks_normal = new_irr; 4667 1.1 mrg 4668 1.1 mrg if (dump_file && new_irr) 4669 1.1 mrg { 4670 1.1 mrg const char *dname; 4671 1.1 mrg bitmap_iterator bmi; 4672 1.1 mrg unsigned i; 4673 1.1 mrg 4674 1.1 mrg dname = lang_hooks.decl_printable_name (current_function_decl, 2); 4675 1.1 mrg EXECUTE_IF_SET_IN_BITMAP (new_irr, 0, i, bmi) 4676 1.1 mrg fprintf (dump_file, "%s: bb %d goes irrevocable\n", dname, i); 4677 1.1 mrg } 4678 1.1 mrg } 4679 1.1 mrg else 4680 1.1 mrg BITMAP_FREE (new_irr); 4681 1.1 mrg 4682 1.1 mrg pop_cfun (); 4683 1.1 mrg 4684 1.1 mrg return ret; 4685 1.1 mrg } 4686 1.1 mrg 4687 1.1 mrg /* Return true if, for the transactional clone of NODE, any call 4688 1.1 mrg may enter irrevocable mode. */ 4689 1.1 mrg 4690 1.1 mrg static bool 4691 1.1 mrg ipa_tm_mayenterirr_function (struct cgraph_node *node) 4692 1.1 mrg { 4693 1.1 mrg struct tm_ipa_cg_data *d; 4694 1.1 mrg tree decl; 4695 1.1 mrg unsigned flags; 4696 1.1 mrg 4697 1.1 mrg d = get_cg_data (&node, true); 4698 1.1 mrg decl = node->decl; 4699 1.1 mrg flags = flags_from_decl_or_type (decl); 4700 1.1 mrg 4701 1.1 mrg /* Handle some TM builtins. Ordinarily these aren't actually generated 4702 1.1 mrg at this point, but handling these functions when written in by the 4703 1.1 mrg user makes it easier to build unit tests. */ 4704 1.1 mrg if (flags & ECF_TM_BUILTIN) 4705 1.1 mrg return false; 4706 1.1 mrg 4707 1.1 mrg /* Filter out all functions that are marked. */ 4708 1.1 mrg if (flags & ECF_TM_PURE) 4709 1.1 mrg return false; 4710 1.1 mrg if (is_tm_safe (decl)) 4711 1.1 mrg return false; 4712 1.1 mrg if (is_tm_irrevocable (decl)) 4713 1.1 mrg return true; 4714 1.1 mrg if (is_tm_callable (decl)) 4715 1.1 mrg return true; 4716 1.1 mrg if (find_tm_replacement_function (decl)) 4717 1.1 mrg return true; 4718 1.1 mrg 4719 1.1 mrg /* If we aren't seeing the final version of the function we don't 4720 1.1 mrg know what it will contain at runtime. */ 4721 1.1 mrg if (node->get_availability () < AVAIL_AVAILABLE) 4722 1.1 mrg return true; 4723 1.1 mrg 4724 1.1 mrg /* If the function must go irrevocable, then of course true. */ 4725 1.1 mrg if (d->is_irrevocable) 4726 1.1 mrg return true; 4727 1.1 mrg 4728 1.1 mrg /* If there are any blocks marked irrevocable, then the function 4729 1.1 mrg as a whole may enter irrevocable. */ 4730 1.1 mrg if (d->irrevocable_blocks_clone) 4731 1.1 mrg return true; 4732 1.1 mrg 4733 1.1 mrg /* We may have previously marked this function as tm_may_enter_irr; 4734 1.1 mrg see pass_diagnose_tm_blocks. */ 4735 1.1 mrg if (node->tm_may_enter_irr) 4736 1.1 mrg return true; 4737 1.1 mrg 4738 1.1 mrg /* Recurse on the main body for aliases. In general, this will 4739 1.1 mrg result in one of the bits above being set so that we will not 4740 1.1 mrg have to recurse next time. */ 4741 1.1 mrg if (node->alias) 4742 1.1 mrg return ipa_tm_mayenterirr_function 4743 1.1 mrg (cgraph_node::get (thunk_info::get (node)->alias)); 4744 1.1 mrg 4745 1.1 mrg /* What remains is unmarked local functions without items that force 4746 1.1 mrg the function to go irrevocable. */ 4747 1.1 mrg return false; 4748 1.1 mrg } 4749 1.1 mrg 4750 1.1 mrg /* Diagnose calls from transaction_safe functions to unmarked 4751 1.1 mrg functions that are determined to not be safe. */ 4752 1.1 mrg 4753 1.1 mrg static void 4754 1.1 mrg ipa_tm_diagnose_tm_safe (struct cgraph_node *node) 4755 1.1 mrg { 4756 1.1 mrg struct cgraph_edge *e; 4757 1.1 mrg 4758 1.1 mrg for (e = node->callees; e ; e = e->next_callee) 4759 1.1 mrg if (!is_tm_callable (e->callee->decl) 4760 1.1 mrg && e->callee->tm_may_enter_irr) 4761 1.1 mrg error_at (gimple_location (e->call_stmt), 4762 1.1 mrg "unsafe function call %qD within " 4763 1.1 mrg "%<transaction_safe%> function", e->callee->decl); 4764 1.1 mrg } 4765 1.1 mrg 4766 1.1 mrg /* Diagnose call from atomic transactions to unmarked functions 4767 1.1 mrg that are determined to not be safe. */ 4768 1.1 mrg 4769 1.1 mrg static void 4770 1.1 mrg ipa_tm_diagnose_transaction (struct cgraph_node *node, 4771 1.1 mrg struct tm_region *all_tm_regions) 4772 1.1 mrg { 4773 1.1 mrg struct tm_region *r; 4774 1.1 mrg 4775 1.1 mrg for (r = all_tm_regions; r ; r = r->next) 4776 1.1 mrg if (gimple_transaction_subcode (r->get_transaction_stmt ()) 4777 1.1 mrg & GTMA_IS_RELAXED) 4778 1.1 mrg { 4779 1.1 mrg /* Atomic transactions can be nested inside relaxed. */ 4780 1.1 mrg if (r->inner) 4781 1.1 mrg ipa_tm_diagnose_transaction (node, r->inner); 4782 1.1 mrg } 4783 1.1 mrg else 4784 1.1 mrg { 4785 1.1 mrg vec<basic_block> bbs; 4786 1.1 mrg gimple_stmt_iterator gsi; 4787 1.1 mrg basic_block bb; 4788 1.1 mrg size_t i; 4789 1.1 mrg 4790 1.1 mrg bbs = get_tm_region_blocks (r->entry_block, r->exit_blocks, 4791 1.1 mrg r->irr_blocks, NULL, false); 4792 1.1 mrg 4793 1.1 mrg for (i = 0; bbs.iterate (i, &bb); ++i) 4794 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 4795 1.1 mrg { 4796 1.1 mrg gimple *stmt = gsi_stmt (gsi); 4797 1.1 mrg tree fndecl; 4798 1.1 mrg 4799 1.1 mrg if (gimple_code (stmt) == GIMPLE_ASM) 4800 1.1 mrg { 4801 1.1 mrg error_at (gimple_location (stmt), 4802 1.1 mrg "%<asm%> not allowed in atomic transaction"); 4803 1.1 mrg continue; 4804 1.1 mrg } 4805 1.1 mrg 4806 1.1 mrg if (!is_gimple_call (stmt)) 4807 1.1 mrg continue; 4808 1.1 mrg fndecl = gimple_call_fndecl (stmt); 4809 1.1 mrg 4810 1.1 mrg /* Indirect function calls have been diagnosed already. */ 4811 1.1 mrg if (!fndecl) 4812 1.1 mrg continue; 4813 1.1 mrg 4814 1.1 mrg /* Stop at the end of the transaction. */ 4815 1.1 mrg if (is_tm_ending_fndecl (fndecl)) 4816 1.1 mrg { 4817 1.1 mrg if (bitmap_bit_p (r->exit_blocks, bb->index)) 4818 1.1 mrg break; 4819 1.1 mrg continue; 4820 1.1 mrg } 4821 1.1 mrg 4822 1.1 mrg /* Marked functions have been diagnosed already. */ 4823 1.1 mrg if (is_tm_pure_call (stmt)) 4824 1.1 mrg continue; 4825 1.1 mrg if (is_tm_callable (fndecl)) 4826 1.1 mrg continue; 4827 1.1 mrg 4828 1.1 mrg if (cgraph_node::local_info_node (fndecl)->tm_may_enter_irr) 4829 1.1 mrg error_at (gimple_location (stmt), 4830 1.1 mrg "unsafe function call %qD within " 4831 1.1 mrg "atomic transaction", fndecl); 4832 1.1 mrg } 4833 1.1 mrg 4834 1.1 mrg bbs.release (); 4835 1.1 mrg } 4836 1.1 mrg } 4837 1.1 mrg 4838 1.1 mrg /* Return a transactional mangled name for the DECL_ASSEMBLER_NAME in 4839 1.1 mrg OLD_DECL. The returned value is a freshly malloced pointer that 4840 1.1 mrg should be freed by the caller. */ 4841 1.1 mrg 4842 1.1 mrg static tree 4843 1.1 mrg tm_mangle (tree old_asm_id) 4844 1.1 mrg { 4845 1.1 mrg const char *old_asm_name; 4846 1.1 mrg char *tm_name; 4847 1.1 mrg void *alloc = NULL; 4848 1.1 mrg struct demangle_component *dc; 4849 1.1 mrg tree new_asm_id; 4850 1.1 mrg 4851 1.1 mrg /* Determine if the symbol is already a valid C++ mangled name. Do this 4852 1.1 mrg even for C, which might be interfacing with C++ code via appropriately 4853 1.1 mrg ugly identifiers. */ 4854 1.1 mrg /* ??? We could probably do just as well checking for "_Z" and be done. */ 4855 1.1 mrg old_asm_name = IDENTIFIER_POINTER (old_asm_id); 4856 1.1 mrg dc = cplus_demangle_v3_components (old_asm_name, DMGL_NO_OPTS, &alloc); 4857 1.1 mrg 4858 1.1 mrg if (dc == NULL) 4859 1.1 mrg { 4860 1.1 mrg char length[12]; 4861 1.1 mrg 4862 1.1 mrg do_unencoded: 4863 1.1 mrg sprintf (length, "%u", IDENTIFIER_LENGTH (old_asm_id)); 4864 1.1 mrg tm_name = concat ("_ZGTt", length, old_asm_name, NULL); 4865 1.1 mrg } 4866 1.1 mrg else 4867 1.1 mrg { 4868 1.1 mrg old_asm_name += 2; /* Skip _Z */ 4869 1.1 mrg 4870 1.1 mrg switch (dc->type) 4871 1.1 mrg { 4872 1.1 mrg case DEMANGLE_COMPONENT_TRANSACTION_CLONE: 4873 1.1 mrg case DEMANGLE_COMPONENT_NONTRANSACTION_CLONE: 4874 1.1 mrg /* Don't play silly games, you! */ 4875 1.1 mrg goto do_unencoded; 4876 1.1 mrg 4877 1.1 mrg case DEMANGLE_COMPONENT_HIDDEN_ALIAS: 4878 1.1 mrg /* I'd really like to know if we can ever be passed one of 4879 1.1 mrg these from the C++ front end. The Logical Thing would 4880 1.1 mrg seem that hidden-alias should be outer-most, so that we 4881 1.1 mrg get hidden-alias of a transaction-clone and not vice-versa. */ 4882 1.1 mrg old_asm_name += 2; 4883 1.1 mrg break; 4884 1.1 mrg 4885 1.1 mrg default: 4886 1.1 mrg break; 4887 1.1 mrg } 4888 1.1 mrg 4889 1.1 mrg tm_name = concat ("_ZGTt", old_asm_name, NULL); 4890 1.1 mrg } 4891 1.1 mrg free (alloc); 4892 1.1 mrg 4893 1.1 mrg new_asm_id = get_identifier (tm_name); 4894 1.1 mrg free (tm_name); 4895 1.1 mrg 4896 1.1 mrg return new_asm_id; 4897 1.1 mrg } 4898 1.1 mrg 4899 1.1 mrg static inline void 4900 1.1 mrg ipa_tm_mark_force_output_node (struct cgraph_node *node) 4901 1.1 mrg { 4902 1.1 mrg node->mark_force_output (); 4903 1.1 mrg node->analyzed = true; 4904 1.1 mrg } 4905 1.1 mrg 4906 1.1 mrg static inline void 4907 1.1 mrg ipa_tm_mark_forced_by_abi_node (struct cgraph_node *node) 4908 1.1 mrg { 4909 1.1 mrg node->forced_by_abi = true; 4910 1.1 mrg node->analyzed = true; 4911 1.1 mrg } 4912 1.1 mrg 4913 1.1 mrg /* Callback data for ipa_tm_create_version_alias. */ 4914 1.1 mrg struct create_version_alias_info 4915 1.1 mrg { 4916 1.1 mrg struct cgraph_node *old_node; 4917 1.1 mrg tree new_decl; 4918 1.1 mrg }; 4919 1.1 mrg 4920 1.1 mrg /* A subroutine of ipa_tm_create_version, called via 4921 1.1 mrg cgraph_for_node_and_aliases. Create new tm clones for each of 4922 1.1 mrg the existing aliases. */ 4923 1.1 mrg static bool 4924 1.1 mrg ipa_tm_create_version_alias (struct cgraph_node *node, void *data) 4925 1.1 mrg { 4926 1.1 mrg struct create_version_alias_info *info 4927 1.1 mrg = (struct create_version_alias_info *)data; 4928 1.1 mrg tree old_decl, new_decl, tm_name; 4929 1.1 mrg struct cgraph_node *new_node; 4930 1.1 mrg 4931 1.1 mrg if (!node->cpp_implicit_alias) 4932 1.1 mrg return false; 4933 1.1 mrg 4934 1.1 mrg old_decl = node->decl; 4935 1.1 mrg tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl)); 4936 1.1 mrg new_decl = build_decl (DECL_SOURCE_LOCATION (old_decl), 4937 1.1 mrg TREE_CODE (old_decl), tm_name, 4938 1.1 mrg TREE_TYPE (old_decl)); 4939 1.1 mrg 4940 1.1 mrg SET_DECL_ASSEMBLER_NAME (new_decl, tm_name); 4941 1.1 mrg SET_DECL_RTL (new_decl, NULL); 4942 1.1 mrg 4943 1.1 mrg /* Based loosely on C++'s make_alias_for(). */ 4944 1.1 mrg TREE_PUBLIC (new_decl) = TREE_PUBLIC (old_decl); 4945 1.1 mrg DECL_CONTEXT (new_decl) = DECL_CONTEXT (old_decl); 4946 1.1 mrg DECL_LANG_SPECIFIC (new_decl) = DECL_LANG_SPECIFIC (old_decl); 4947 1.1 mrg TREE_READONLY (new_decl) = TREE_READONLY (old_decl); 4948 1.1 mrg DECL_EXTERNAL (new_decl) = 0; 4949 1.1 mrg DECL_ARTIFICIAL (new_decl) = 1; 4950 1.1 mrg TREE_ADDRESSABLE (new_decl) = 1; 4951 1.1 mrg TREE_USED (new_decl) = 1; 4952 1.1 mrg TREE_SYMBOL_REFERENCED (tm_name) = 1; 4953 1.1 mrg 4954 1.1 mrg /* Perform the same remapping to the comdat group. */ 4955 1.1 mrg if (DECL_ONE_ONLY (new_decl)) 4956 1.1 mrg varpool_node::get (new_decl)->set_comdat_group 4957 1.1 mrg (tm_mangle (decl_comdat_group_id (old_decl))); 4958 1.1 mrg 4959 1.1 mrg new_node = cgraph_node::create_same_body_alias (new_decl, info->new_decl); 4960 1.1 mrg new_node->tm_clone = true; 4961 1.1 mrg new_node->externally_visible = info->old_node->externally_visible; 4962 1.1 mrg new_node->no_reorder = info->old_node->no_reorder; 4963 1.1 mrg /* ?? Do not traverse aliases here. */ 4964 1.1 mrg get_cg_data (&node, false)->clone = new_node; 4965 1.1 mrg 4966 1.1 mrg record_tm_clone_pair (old_decl, new_decl); 4967 1.1 mrg 4968 1.1 mrg if (info->old_node->force_output 4969 1.1 mrg || info->old_node->ref_list.first_referring ()) 4970 1.1 mrg ipa_tm_mark_force_output_node (new_node); 4971 1.1 mrg if (info->old_node->forced_by_abi) 4972 1.1 mrg ipa_tm_mark_forced_by_abi_node (new_node); 4973 1.1 mrg return false; 4974 1.1 mrg } 4975 1.1 mrg 4976 1.1 mrg /* Create a copy of the function (possibly declaration only) of OLD_NODE, 4977 1.1 mrg appropriate for the transactional clone. */ 4978 1.1 mrg 4979 1.1 mrg static void 4980 1.1 mrg ipa_tm_create_version (struct cgraph_node *old_node) 4981 1.1 mrg { 4982 1.1 mrg tree new_decl, old_decl, tm_name; 4983 1.1 mrg struct cgraph_node *new_node; 4984 1.1 mrg 4985 1.1 mrg old_decl = old_node->decl; 4986 1.1 mrg new_decl = copy_node (old_decl); 4987 1.1 mrg 4988 1.1 mrg /* DECL_ASSEMBLER_NAME needs to be set before we call 4989 1.1 mrg cgraph_copy_node_for_versioning below, because cgraph_node will 4990 1.1 mrg fill the assembler_name_hash. */ 4991 1.1 mrg tm_name = tm_mangle (DECL_ASSEMBLER_NAME (old_decl)); 4992 1.1 mrg SET_DECL_ASSEMBLER_NAME (new_decl, tm_name); 4993 1.1 mrg SET_DECL_RTL (new_decl, NULL); 4994 1.1 mrg TREE_SYMBOL_REFERENCED (tm_name) = 1; 4995 1.1 mrg 4996 1.1 mrg /* Perform the same remapping to the comdat group. */ 4997 1.1 mrg if (DECL_ONE_ONLY (new_decl)) 4998 1.1 mrg varpool_node::get (new_decl)->set_comdat_group 4999 1.1 mrg (tm_mangle (DECL_COMDAT_GROUP (old_decl))); 5000 1.1 mrg 5001 1.1 mrg gcc_assert (!old_node->ipa_transforms_to_apply.exists ()); 5002 1.1 mrg new_node = old_node->create_version_clone (new_decl, vNULL, NULL); 5003 1.1 mrg new_node->local = false; 5004 1.1 mrg new_node->externally_visible = old_node->externally_visible; 5005 1.1 mrg new_node->lowered = true; 5006 1.1 mrg new_node->tm_clone = 1; 5007 1.1 mrg if (!old_node->implicit_section) 5008 1.1 mrg new_node->set_section (*old_node); 5009 1.1 mrg get_cg_data (&old_node, true)->clone = new_node; 5010 1.1 mrg 5011 1.1 mrg if (old_node->get_availability () >= AVAIL_INTERPOSABLE) 5012 1.1 mrg { 5013 1.1 mrg /* Remap extern inline to static inline. */ 5014 1.1 mrg /* ??? Is it worth trying to use make_decl_one_only? */ 5015 1.1 mrg if (DECL_DECLARED_INLINE_P (new_decl) && DECL_EXTERNAL (new_decl)) 5016 1.1 mrg { 5017 1.1 mrg DECL_EXTERNAL (new_decl) = 0; 5018 1.1 mrg TREE_PUBLIC (new_decl) = 0; 5019 1.1 mrg DECL_WEAK (new_decl) = 0; 5020 1.1 mrg } 5021 1.1 mrg 5022 1.1 mrg tree_function_versioning (old_decl, new_decl, 5023 1.1 mrg NULL, NULL, false, NULL, NULL); 5024 1.1 mrg } 5025 1.1 mrg 5026 1.1 mrg record_tm_clone_pair (old_decl, new_decl); 5027 1.1 mrg 5028 1.1 mrg symtab->call_cgraph_insertion_hooks (new_node); 5029 1.1 mrg if (old_node->force_output 5030 1.1 mrg || old_node->ref_list.first_referring ()) 5031 1.1 mrg ipa_tm_mark_force_output_node (new_node); 5032 1.1 mrg if (old_node->forced_by_abi) 5033 1.1 mrg ipa_tm_mark_forced_by_abi_node (new_node); 5034 1.1 mrg 5035 1.1 mrg /* Do the same thing, but for any aliases of the original node. */ 5036 1.1 mrg { 5037 1.1 mrg struct create_version_alias_info data; 5038 1.1 mrg data.old_node = old_node; 5039 1.1 mrg data.new_decl = new_decl; 5040 1.1 mrg old_node->call_for_symbol_thunks_and_aliases (ipa_tm_create_version_alias, 5041 1.1 mrg &data, true); 5042 1.1 mrg } 5043 1.1 mrg } 5044 1.1 mrg 5045 1.1 mrg /* Construct a call to TM_IRREVOCABLE and insert it at the beginning of BB. */ 5046 1.1 mrg 5047 1.1 mrg static void 5048 1.1 mrg ipa_tm_insert_irr_call (struct cgraph_node *node, struct tm_region *region, 5049 1.1 mrg basic_block bb) 5050 1.1 mrg { 5051 1.1 mrg gimple_stmt_iterator gsi; 5052 1.1 mrg gcall *g; 5053 1.1 mrg 5054 1.1 mrg transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE); 5055 1.1 mrg 5056 1.1 mrg g = gimple_build_call (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE), 5057 1.1 mrg 1, build_int_cst (NULL_TREE, MODE_SERIALIRREVOCABLE)); 5058 1.1 mrg 5059 1.1 mrg split_block_after_labels (bb); 5060 1.1 mrg gsi = gsi_after_labels (bb); 5061 1.1 mrg gsi_insert_before (&gsi, g, GSI_SAME_STMT); 5062 1.1 mrg 5063 1.1 mrg node->create_edge (cgraph_node::get_create 5064 1.1 mrg (builtin_decl_explicit (BUILT_IN_TM_IRREVOCABLE)), 5065 1.1 mrg g, gimple_bb (g)->count); 5066 1.1 mrg } 5067 1.1 mrg 5068 1.1 mrg /* Construct a call to TM_GETTMCLONE and insert it before GSI. */ 5069 1.1 mrg 5070 1.1 mrg static bool 5071 1.1 mrg ipa_tm_insert_gettmclone_call (struct cgraph_node *node, 5072 1.1 mrg struct tm_region *region, 5073 1.1 mrg gimple_stmt_iterator *gsi, gcall *stmt) 5074 1.1 mrg { 5075 1.1 mrg tree gettm_fn, ret, old_fn, callfn; 5076 1.1 mrg gcall *g; 5077 1.1 mrg gassign *g2; 5078 1.1 mrg bool safe; 5079 1.1 mrg 5080 1.1 mrg old_fn = gimple_call_fn (stmt); 5081 1.1 mrg 5082 1.1 mrg if (TREE_CODE (old_fn) == ADDR_EXPR) 5083 1.1 mrg { 5084 1.1 mrg tree fndecl = TREE_OPERAND (old_fn, 0); 5085 1.1 mrg tree clone = get_tm_clone_pair (fndecl); 5086 1.1 mrg 5087 1.1 mrg /* By transforming the call into a TM_GETTMCLONE, we are 5088 1.1 mrg technically taking the address of the original function and 5089 1.1 mrg its clone. Explain this so inlining will know this function 5090 1.1 mrg is needed. */ 5091 1.1 mrg cgraph_node::get (fndecl)->mark_address_taken () ; 5092 1.1 mrg if (clone) 5093 1.1 mrg cgraph_node::get (clone)->mark_address_taken (); 5094 1.1 mrg } 5095 1.1 mrg 5096 1.1 mrg safe = is_tm_safe (TREE_TYPE (old_fn)); 5097 1.1 mrg gettm_fn = builtin_decl_explicit (safe ? BUILT_IN_TM_GETTMCLONE_SAFE 5098 1.1 mrg : BUILT_IN_TM_GETTMCLONE_IRR); 5099 1.1 mrg ret = create_tmp_var (ptr_type_node); 5100 1.1 mrg 5101 1.1 mrg if (!safe) 5102 1.1 mrg transaction_subcode_ior (region, GTMA_MAY_ENTER_IRREVOCABLE); 5103 1.1 mrg 5104 1.1 mrg /* Discard OBJ_TYPE_REF, since we weren't able to fold it. */ 5105 1.1 mrg if (TREE_CODE (old_fn) == OBJ_TYPE_REF) 5106 1.1 mrg old_fn = OBJ_TYPE_REF_EXPR (old_fn); 5107 1.1 mrg 5108 1.1 mrg g = gimple_build_call (gettm_fn, 1, old_fn); 5109 1.1 mrg ret = make_ssa_name (ret, g); 5110 1.1 mrg gimple_call_set_lhs (g, ret); 5111 1.1 mrg 5112 1.1 mrg gsi_insert_before (gsi, g, GSI_SAME_STMT); 5113 1.1 mrg 5114 1.1 mrg node->create_edge (cgraph_node::get_create (gettm_fn), g, gimple_bb (g)->count); 5115 1.1 mrg 5116 1.1 mrg /* Cast return value from tm_gettmclone* into appropriate function 5117 1.1 mrg pointer. */ 5118 1.1 mrg callfn = create_tmp_var (TREE_TYPE (old_fn)); 5119 1.1 mrg g2 = gimple_build_assign (callfn, 5120 1.1 mrg fold_build1 (NOP_EXPR, TREE_TYPE (callfn), ret)); 5121 1.1 mrg callfn = make_ssa_name (callfn, g2); 5122 1.1 mrg gimple_assign_set_lhs (g2, callfn); 5123 1.1 mrg gsi_insert_before (gsi, g2, GSI_SAME_STMT); 5124 1.1 mrg 5125 1.1 mrg /* ??? This is a hack to preserve the NOTHROW bit on the call, 5126 1.1 mrg which we would have derived from the decl. Failure to save 5127 1.1 mrg this bit means we might have to split the basic block. */ 5128 1.1 mrg if (gimple_call_nothrow_p (stmt)) 5129 1.1 mrg gimple_call_set_nothrow (stmt, true); 5130 1.1 mrg 5131 1.1 mrg gimple_call_set_fn (stmt, callfn); 5132 1.1 mrg 5133 1.1 mrg /* Discarding OBJ_TYPE_REF above may produce incompatible LHS and RHS 5134 1.1 mrg for a call statement. Fix it. */ 5135 1.1 mrg { 5136 1.1 mrg tree lhs = gimple_call_lhs (stmt); 5137 1.1 mrg tree rettype = TREE_TYPE (gimple_call_fntype (stmt)); 5138 1.1 mrg if (lhs 5139 1.1 mrg && !useless_type_conversion_p (TREE_TYPE (lhs), rettype)) 5140 1.1 mrg { 5141 1.1 mrg tree temp; 5142 1.1 mrg 5143 1.1 mrg temp = create_tmp_reg (rettype); 5144 1.1 mrg gimple_call_set_lhs (stmt, temp); 5145 1.1 mrg 5146 1.1 mrg g2 = gimple_build_assign (lhs, 5147 1.1 mrg fold_build1 (VIEW_CONVERT_EXPR, 5148 1.1 mrg TREE_TYPE (lhs), temp)); 5149 1.1 mrg gsi_insert_after (gsi, g2, GSI_SAME_STMT); 5150 1.1 mrg } 5151 1.1 mrg } 5152 1.1 mrg 5153 1.1 mrg update_stmt (stmt); 5154 1.1 mrg cgraph_edge *e = cgraph_node::get (current_function_decl)->get_edge (stmt); 5155 1.1 mrg if (e && e->indirect_info) 5156 1.1 mrg e->indirect_info->polymorphic = false; 5157 1.1 mrg 5158 1.1 mrg return true; 5159 1.1 mrg } 5160 1.1 mrg 5161 1.1 mrg /* Helper function for ipa_tm_transform_calls*. Given a call 5162 1.1 mrg statement in GSI which resides inside transaction REGION, redirect 5163 1.1 mrg the call to either its wrapper function, or its clone. */ 5164 1.1 mrg 5165 1.1 mrg static void 5166 1.1 mrg ipa_tm_transform_calls_redirect (struct cgraph_node *node, 5167 1.1 mrg struct tm_region *region, 5168 1.1 mrg gimple_stmt_iterator *gsi, 5169 1.1 mrg bool *need_ssa_rename_p) 5170 1.1 mrg { 5171 1.1 mrg gcall *stmt = as_a <gcall *> (gsi_stmt (*gsi)); 5172 1.1 mrg struct cgraph_node *new_node; 5173 1.1 mrg struct cgraph_edge *e = node->get_edge (stmt); 5174 1.1 mrg tree fndecl = gimple_call_fndecl (stmt); 5175 1.1 mrg 5176 1.1 mrg /* For indirect calls, pass the address through the runtime. */ 5177 1.1 mrg if (fndecl == NULL) 5178 1.1 mrg { 5179 1.1 mrg *need_ssa_rename_p |= 5180 1.1 mrg ipa_tm_insert_gettmclone_call (node, region, gsi, stmt); 5181 1.1 mrg return; 5182 1.1 mrg } 5183 1.1 mrg 5184 1.1 mrg /* Handle some TM builtins. Ordinarily these aren't actually generated 5185 1.1 mrg at this point, but handling these functions when written in by the 5186 1.1 mrg user makes it easier to build unit tests. */ 5187 1.1 mrg if (flags_from_decl_or_type (fndecl) & ECF_TM_BUILTIN) 5188 1.1 mrg return; 5189 1.1 mrg 5190 1.1 mrg /* Fixup recursive calls inside clones. */ 5191 1.1 mrg /* ??? Why did cgraph_copy_node_for_versioning update the call edges 5192 1.1 mrg for recursion but not update the call statements themselves? */ 5193 1.1 mrg if (e->caller == e->callee && decl_is_tm_clone (current_function_decl)) 5194 1.1 mrg { 5195 1.1 mrg gimple_call_set_fndecl (stmt, current_function_decl); 5196 1.1 mrg return; 5197 1.1 mrg } 5198 1.1 mrg 5199 1.1 mrg /* If there is a replacement, use it. */ 5200 1.1 mrg fndecl = find_tm_replacement_function (fndecl); 5201 1.1 mrg if (fndecl) 5202 1.1 mrg { 5203 1.1 mrg new_node = cgraph_node::get_create (fndecl); 5204 1.1 mrg 5205 1.1 mrg /* ??? Mark all transaction_wrap functions tm_may_enter_irr. 5206 1.1 mrg 5207 1.1 mrg We can't do this earlier in record_tm_replacement because 5208 1.1 mrg cgraph_remove_unreachable_nodes is called before we inject 5209 1.1 mrg references to the node. Further, we can't do this in some 5210 1.1 mrg nice central place in ipa_tm_execute because we don't have 5211 1.1 mrg the exact list of wrapper functions that would be used. 5212 1.1 mrg Marking more wrappers than necessary results in the creation 5213 1.1 mrg of unnecessary cgraph_nodes, which can cause some of the 5214 1.1 mrg other IPA passes to crash. 5215 1.1 mrg 5216 1.1 mrg We do need to mark these nodes so that we get the proper 5217 1.1 mrg result in expand_call_tm. */ 5218 1.1 mrg /* ??? This seems broken. How is it that we're marking the 5219 1.1 mrg CALLEE as may_enter_irr? Surely we should be marking the 5220 1.1 mrg CALLER. Also note that find_tm_replacement_function also 5221 1.1 mrg contains mappings into the TM runtime, e.g. memcpy. These 5222 1.1 mrg we know won't go irrevocable. */ 5223 1.1 mrg new_node->tm_may_enter_irr = 1; 5224 1.1 mrg } 5225 1.1 mrg else 5226 1.1 mrg { 5227 1.1 mrg struct tm_ipa_cg_data *d; 5228 1.1 mrg struct cgraph_node *tnode = e->callee; 5229 1.1 mrg 5230 1.1 mrg d = get_cg_data (&tnode, true); 5231 1.1 mrg new_node = d->clone; 5232 1.1 mrg 5233 1.1 mrg /* As we've already skipped pure calls and appropriate builtins, 5234 1.1 mrg and we've already marked irrevocable blocks, if we can't come 5235 1.1 mrg up with a static replacement, then ask the runtime. */ 5236 1.1 mrg if (new_node == NULL) 5237 1.1 mrg { 5238 1.1 mrg *need_ssa_rename_p |= 5239 1.1 mrg ipa_tm_insert_gettmclone_call (node, region, gsi, stmt); 5240 1.1 mrg return; 5241 1.1 mrg } 5242 1.1 mrg 5243 1.1 mrg fndecl = new_node->decl; 5244 1.1 mrg } 5245 1.1 mrg 5246 1.1 mrg e->redirect_callee (new_node); 5247 1.1 mrg gimple_call_set_fndecl (stmt, fndecl); 5248 1.1 mrg } 5249 1.1 mrg 5250 1.1 mrg /* Helper function for ipa_tm_transform_calls. For a given BB, 5251 1.1 mrg install calls to tm_irrevocable when IRR_BLOCKS are reached, 5252 1.1 mrg redirect other calls to the generated transactional clone. */ 5253 1.1 mrg 5254 1.1 mrg static bool 5255 1.1 mrg ipa_tm_transform_calls_1 (struct cgraph_node *node, struct tm_region *region, 5256 1.1 mrg basic_block bb, bitmap irr_blocks) 5257 1.1 mrg { 5258 1.1 mrg gimple_stmt_iterator gsi; 5259 1.1 mrg bool need_ssa_rename = false; 5260 1.1 mrg 5261 1.1 mrg if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index)) 5262 1.1 mrg { 5263 1.1 mrg ipa_tm_insert_irr_call (node, region, bb); 5264 1.1 mrg return true; 5265 1.1 mrg } 5266 1.1 mrg 5267 1.1 mrg for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) 5268 1.1 mrg { 5269 1.1 mrg gimple *stmt = gsi_stmt (gsi); 5270 1.1 mrg 5271 1.1 mrg if (!is_gimple_call (stmt)) 5272 1.1 mrg continue; 5273 1.1 mrg if (is_tm_pure_call (stmt)) 5274 1.1 mrg continue; 5275 1.1 mrg 5276 1.1 mrg /* Redirect edges to the appropriate replacement or clone. */ 5277 1.1 mrg ipa_tm_transform_calls_redirect (node, region, &gsi, &need_ssa_rename); 5278 1.1 mrg } 5279 1.1 mrg 5280 1.1 mrg return need_ssa_rename; 5281 1.1 mrg } 5282 1.1 mrg 5283 1.1 mrg /* Walk the CFG for REGION, beginning at BB. Install calls to 5284 1.1 mrg tm_irrevocable when IRR_BLOCKS are reached, redirect other calls to 5285 1.1 mrg the generated transactional clone. */ 5286 1.1 mrg 5287 1.1 mrg static bool 5288 1.1 mrg ipa_tm_transform_calls (struct cgraph_node *node, struct tm_region *region, 5289 1.1 mrg basic_block bb, bitmap irr_blocks) 5290 1.1 mrg { 5291 1.1 mrg bool need_ssa_rename = false; 5292 1.1 mrg edge e; 5293 1.1 mrg edge_iterator ei; 5294 1.1 mrg auto_vec<basic_block> queue; 5295 1.1 mrg bitmap visited_blocks = BITMAP_ALLOC (NULL); 5296 1.1 mrg 5297 1.1 mrg queue.safe_push (bb); 5298 1.1 mrg do 5299 1.1 mrg { 5300 1.1 mrg bb = queue.pop (); 5301 1.1 mrg 5302 1.1 mrg need_ssa_rename |= 5303 1.1 mrg ipa_tm_transform_calls_1 (node, region, bb, irr_blocks); 5304 1.1 mrg 5305 1.1 mrg if (irr_blocks && bitmap_bit_p (irr_blocks, bb->index)) 5306 1.1 mrg continue; 5307 1.1 mrg 5308 1.1 mrg if (region && bitmap_bit_p (region->exit_blocks, bb->index)) 5309 1.1 mrg continue; 5310 1.1 mrg 5311 1.1 mrg FOR_EACH_EDGE (e, ei, bb->succs) 5312 1.1 mrg if (!bitmap_bit_p (visited_blocks, e->dest->index)) 5313 1.1 mrg { 5314 1.1 mrg bitmap_set_bit (visited_blocks, e->dest->index); 5315 1.1 mrg queue.safe_push (e->dest); 5316 1.1 mrg } 5317 1.1 mrg } 5318 1.1 mrg while (!queue.is_empty ()); 5319 1.1 mrg 5320 1.1 mrg BITMAP_FREE (visited_blocks); 5321 1.1 mrg 5322 1.1 mrg return need_ssa_rename; 5323 1.1 mrg } 5324 1.1 mrg 5325 1.1 mrg /* Transform the calls within the TM regions within NODE. */ 5326 1.1 mrg 5327 1.1 mrg static void 5328 1.1 mrg ipa_tm_transform_transaction (struct cgraph_node *node) 5329 1.1 mrg { 5330 1.1 mrg struct tm_ipa_cg_data *d; 5331 1.1 mrg struct tm_region *region; 5332 1.1 mrg bool need_ssa_rename = false; 5333 1.1 mrg 5334 1.1 mrg d = get_cg_data (&node, true); 5335 1.1 mrg 5336 1.1 mrg push_cfun (DECL_STRUCT_FUNCTION (node->decl)); 5337 1.1 mrg calculate_dominance_info (CDI_DOMINATORS); 5338 1.1 mrg 5339 1.1 mrg for (region = d->all_tm_regions; region; region = region->next) 5340 1.1 mrg { 5341 1.1 mrg /* If we're sure to go irrevocable, don't transform anything. */ 5342 1.1 mrg if (d->irrevocable_blocks_normal 5343 1.1 mrg && bitmap_bit_p (d->irrevocable_blocks_normal, 5344 1.1 mrg region->entry_block->index)) 5345 1.1 mrg { 5346 1.1 mrg transaction_subcode_ior (region, GTMA_DOES_GO_IRREVOCABLE 5347 1.1 mrg | GTMA_MAY_ENTER_IRREVOCABLE 5348 1.1 mrg | GTMA_HAS_NO_INSTRUMENTATION); 5349 1.1 mrg continue; 5350 1.1 mrg } 5351 1.1 mrg 5352 1.1 mrg need_ssa_rename |= 5353 1.1 mrg ipa_tm_transform_calls (node, region, region->entry_block, 5354 1.1 mrg d->irrevocable_blocks_normal); 5355 1.1 mrg } 5356 1.1 mrg 5357 1.1 mrg if (need_ssa_rename) 5358 1.1 mrg update_ssa (TODO_update_ssa_only_virtuals); 5359 1.1 mrg 5360 1.1 mrg pop_cfun (); 5361 1.1 mrg } 5362 1.1 mrg 5363 1.1 mrg /* Transform the calls within the transactional clone of NODE. */ 5364 1.1 mrg 5365 1.1 mrg static void 5366 1.1 mrg ipa_tm_transform_clone (struct cgraph_node *node) 5367 1.1 mrg { 5368 1.1 mrg struct tm_ipa_cg_data *d; 5369 1.1 mrg bool need_ssa_rename; 5370 1.1 mrg 5371 1.1 mrg d = get_cg_data (&node, true); 5372 1.1 mrg 5373 1.1 mrg /* If this function makes no calls and has no irrevocable blocks, 5374 1.1 mrg then there's nothing to do. */ 5375 1.1 mrg /* ??? Remove non-aborting top-level transactions. */ 5376 1.1 mrg if (!node->callees && !node->indirect_calls && !d->irrevocable_blocks_clone) 5377 1.1 mrg return; 5378 1.1 mrg 5379 1.1 mrg push_cfun (DECL_STRUCT_FUNCTION (d->clone->decl)); 5380 1.1 mrg calculate_dominance_info (CDI_DOMINATORS); 5381 1.1 mrg 5382 1.1 mrg need_ssa_rename = 5383 1.1 mrg ipa_tm_transform_calls (d->clone, NULL, 5384 1.1 mrg single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)), 5385 1.1 mrg d->irrevocable_blocks_clone); 5386 1.1 mrg 5387 1.1 mrg if (need_ssa_rename) 5388 1.1 mrg update_ssa (TODO_update_ssa_only_virtuals); 5389 1.1 mrg 5390 1.1 mrg pop_cfun (); 5391 1.1 mrg } 5392 1.1 mrg 5393 1.1 mrg /* Main entry point for the transactional memory IPA pass. */ 5394 1.1 mrg 5395 1.1 mrg static unsigned int 5396 1.1 mrg ipa_tm_execute (void) 5397 1.1 mrg { 5398 1.1 mrg cgraph_node_queue tm_callees = cgraph_node_queue (); 5399 1.1 mrg /* List of functions that will go irrevocable. */ 5400 1.1 mrg cgraph_node_queue irr_worklist = cgraph_node_queue (); 5401 1.1 mrg 5402 1.1 mrg struct cgraph_node *node; 5403 1.1 mrg struct tm_ipa_cg_data *d; 5404 1.1 mrg enum availability a; 5405 1.1 mrg unsigned int i; 5406 1.1 mrg 5407 1.1 mrg cgraph_node::checking_verify_cgraph_nodes (); 5408 1.1 mrg 5409 1.1 mrg bitmap_obstack_initialize (&tm_obstack); 5410 1.1 mrg initialize_original_copy_tables (); 5411 1.1 mrg 5412 1.1 mrg /* For all local functions marked tm_callable, queue them. */ 5413 1.1 mrg FOR_EACH_DEFINED_FUNCTION (node) 5414 1.1 mrg if (is_tm_callable (node->decl) 5415 1.1 mrg && node->get_availability () >= AVAIL_INTERPOSABLE) 5416 1.1 mrg { 5417 1.1 mrg d = get_cg_data (&node, true); 5418 1.1 mrg maybe_push_queue (node, &tm_callees, &d->in_callee_queue); 5419 1.1 mrg } 5420 1.1 mrg 5421 1.1 mrg /* For all local reachable functions... */ 5422 1.1 mrg FOR_EACH_DEFINED_FUNCTION (node) 5423 1.1 mrg if (node->lowered 5424 1.1 mrg && node->get_availability () >= AVAIL_INTERPOSABLE) 5425 1.1 mrg { 5426 1.1 mrg /* ... marked tm_pure, record that fact for the runtime by 5427 1.1 mrg indicating that the pure function is its own tm_callable. 5428 1.1 mrg No need to do this if the function's address can't be taken. */ 5429 1.1 mrg if (is_tm_pure (node->decl)) 5430 1.1 mrg { 5431 1.1 mrg if (!node->local) 5432 1.1 mrg record_tm_clone_pair (node->decl, node->decl); 5433 1.1 mrg continue; 5434 1.1 mrg } 5435 1.1 mrg 5436 1.1 mrg push_cfun (DECL_STRUCT_FUNCTION (node->decl)); 5437 1.1 mrg calculate_dominance_info (CDI_DOMINATORS); 5438 1.1 mrg 5439 1.1 mrg tm_region_init (NULL); 5440 1.1 mrg if (all_tm_regions) 5441 1.1 mrg { 5442 1.1 mrg d = get_cg_data (&node, true); 5443 1.1 mrg 5444 1.1 mrg /* Scan for calls that are in each transaction, and 5445 1.1 mrg generate the uninstrumented code path. */ 5446 1.1 mrg ipa_tm_scan_calls_transaction (d, &tm_callees); 5447 1.1 mrg 5448 1.1 mrg /* Put it in the worklist so we can scan the function 5449 1.1 mrg later (ipa_tm_scan_irr_function) and mark the 5450 1.1 mrg irrevocable blocks. */ 5451 1.1 mrg maybe_push_queue (node, &irr_worklist, &d->in_worklist); 5452 1.1 mrg d->want_irr_scan_normal = true; 5453 1.1 mrg } 5454 1.1 mrg 5455 1.1 mrg pop_cfun (); 5456 1.1 mrg } 5457 1.1 mrg 5458 1.1 mrg /* For every local function on the callee list, scan as if we will be 5459 1.1 mrg creating a transactional clone, queueing all new functions we find 5460 1.1 mrg along the way. */ 5461 1.1 mrg for (i = 0; i < tm_callees.length (); ++i) 5462 1.1 mrg { 5463 1.1 mrg node = tm_callees[i]; 5464 1.1 mrg a = node->get_availability (); 5465 1.1 mrg d = get_cg_data (&node, true); 5466 1.1 mrg 5467 1.1 mrg /* Put it in the worklist so we can scan the function later 5468 1.1 mrg (ipa_tm_scan_irr_function) and mark the irrevocable 5469 1.1 mrg blocks. */ 5470 1.1 mrg maybe_push_queue (node, &irr_worklist, &d->in_worklist); 5471 1.1 mrg 5472 1.1 mrg /* Some callees cannot be arbitrarily cloned. These will always be 5473 1.1 mrg irrevocable. Mark these now, so that we need not scan them. */ 5474 1.1 mrg if (is_tm_irrevocable (node->decl)) 5475 1.1 mrg ipa_tm_note_irrevocable (node, &irr_worklist); 5476 1.1 mrg else if (a <= AVAIL_NOT_AVAILABLE 5477 1.1 mrg && !is_tm_safe_or_pure (node->decl)) 5478 1.1 mrg ipa_tm_note_irrevocable (node, &irr_worklist); 5479 1.1 mrg else if (a >= AVAIL_INTERPOSABLE) 5480 1.1 mrg { 5481 1.1 mrg if (!tree_versionable_function_p (node->decl)) 5482 1.1 mrg ipa_tm_note_irrevocable (node, &irr_worklist); 5483 1.1 mrg else if (!d->is_irrevocable) 5484 1.1 mrg { 5485 1.1 mrg /* If this is an alias, make sure its base is queued as well. 5486 1.1 mrg we need not scan the callees now, as the base will do. */ 5487 1.1 mrg if (node->alias) 5488 1.1 mrg { 5489 1.1 mrg node = cgraph_node::get (thunk_info::get (node)->alias); 5490 1.1 mrg d = get_cg_data (&node, true); 5491 1.1 mrg maybe_push_queue (node, &tm_callees, &d->in_callee_queue); 5492 1.1 mrg continue; 5493 1.1 mrg } 5494 1.1 mrg 5495 1.1 mrg /* Add all nodes called by this function into 5496 1.1 mrg tm_callees as well. */ 5497 1.1 mrg ipa_tm_scan_calls_clone (node, &tm_callees); 5498 1.1 mrg } 5499 1.1 mrg } 5500 1.1 mrg } 5501 1.1 mrg 5502 1.1 mrg /* Iterate scans until no more work to be done. Prefer not to use 5503 1.1 mrg vec::pop because the worklist tends to follow a breadth-first 5504 1.1 mrg search of the callgraph, which should allow convergance with a 5505 1.1 mrg minimum number of scans. But we also don't want the worklist 5506 1.1 mrg array to grow without bound, so we shift the array up periodically. */ 5507 1.1 mrg for (i = 0; i < irr_worklist.length (); ++i) 5508 1.1 mrg { 5509 1.1 mrg if (i > 256 && i == irr_worklist.length () / 8) 5510 1.1 mrg { 5511 1.1 mrg irr_worklist.block_remove (0, i); 5512 1.1 mrg i = 0; 5513 1.1 mrg } 5514 1.1 mrg 5515 1.1 mrg node = irr_worklist[i]; 5516 1.1 mrg d = get_cg_data (&node, true); 5517 1.1 mrg d->in_worklist = false; 5518 1.1 mrg 5519 1.1 mrg if (d->want_irr_scan_normal) 5520 1.1 mrg { 5521 1.1 mrg d->want_irr_scan_normal = false; 5522 1.1 mrg ipa_tm_scan_irr_function (node, false); 5523 1.1 mrg } 5524 1.1 mrg if (d->in_callee_queue && ipa_tm_scan_irr_function (node, true)) 5525 1.1 mrg ipa_tm_note_irrevocable (node, &irr_worklist); 5526 1.1 mrg } 5527 1.1 mrg 5528 1.1 mrg /* For every function on the callee list, collect the tm_may_enter_irr 5529 1.1 mrg bit on the node. */ 5530 1.1 mrg irr_worklist.truncate (0); 5531 1.1 mrg for (i = 0; i < tm_callees.length (); ++i) 5532 1.1 mrg { 5533 1.1 mrg node = tm_callees[i]; 5534 1.1 mrg if (ipa_tm_mayenterirr_function (node)) 5535 1.1 mrg { 5536 1.1 mrg d = get_cg_data (&node, true); 5537 1.1 mrg gcc_assert (d->in_worklist == false); 5538 1.1 mrg maybe_push_queue (node, &irr_worklist, &d->in_worklist); 5539 1.1 mrg } 5540 1.1 mrg } 5541 1.1 mrg 5542 1.1 mrg /* Propagate the tm_may_enter_irr bit to callers until stable. */ 5543 1.1 mrg for (i = 0; i < irr_worklist.length (); ++i) 5544 1.1 mrg { 5545 1.1 mrg struct cgraph_node *caller; 5546 1.1 mrg struct cgraph_edge *e; 5547 1.1 mrg struct ipa_ref *ref; 5548 1.1 mrg 5549 1.1 mrg if (i > 256 && i == irr_worklist.length () / 8) 5550 1.1 mrg { 5551 1.1 mrg irr_worklist.block_remove (0, i); 5552 1.1 mrg i = 0; 5553 1.1 mrg } 5554 1.1 mrg 5555 1.1 mrg node = irr_worklist[i]; 5556 1.1 mrg d = get_cg_data (&node, true); 5557 1.1 mrg d->in_worklist = false; 5558 1.1 mrg node->tm_may_enter_irr = true; 5559 1.1 mrg 5560 1.1 mrg /* Propagate back to normal callers. */ 5561 1.1 mrg for (e = node->callers; e ; e = e->next_caller) 5562 1.1 mrg { 5563 1.1 mrg caller = e->caller; 5564 1.1 mrg if (!is_tm_safe_or_pure (caller->decl) 5565 1.1 mrg && !caller->tm_may_enter_irr) 5566 1.1 mrg { 5567 1.1 mrg d = get_cg_data (&caller, true); 5568 1.1 mrg maybe_push_queue (caller, &irr_worklist, &d->in_worklist); 5569 1.1 mrg } 5570 1.1 mrg } 5571 1.1 mrg 5572 1.1 mrg /* Propagate back to referring aliases as well. */ 5573 1.1 mrg FOR_EACH_ALIAS (node, ref) 5574 1.1 mrg { 5575 1.1 mrg caller = dyn_cast<cgraph_node *> (ref->referring); 5576 1.1 mrg if (!caller->tm_may_enter_irr) 5577 1.1 mrg { 5578 1.1 mrg /* ?? Do not traverse aliases here. */ 5579 1.1 mrg d = get_cg_data (&caller, false); 5580 1.1 mrg maybe_push_queue (caller, &irr_worklist, &d->in_worklist); 5581 1.1 mrg } 5582 1.1 mrg } 5583 1.1 mrg } 5584 1.1 mrg 5585 1.1 mrg /* Now validate all tm_safe functions, and all atomic regions in 5586 1.1 mrg other functions. */ 5587 1.1 mrg FOR_EACH_DEFINED_FUNCTION (node) 5588 1.1 mrg if (node->lowered 5589 1.1 mrg && node->get_availability () >= AVAIL_INTERPOSABLE) 5590 1.1 mrg { 5591 1.1 mrg d = get_cg_data (&node, true); 5592 1.1 mrg if (is_tm_safe (node->decl)) 5593 1.1 mrg ipa_tm_diagnose_tm_safe (node); 5594 1.1 mrg else if (d->all_tm_regions) 5595 1.1 mrg ipa_tm_diagnose_transaction (node, d->all_tm_regions); 5596 1.1 mrg } 5597 1.1 mrg 5598 1.1 mrg /* Create clones. Do those that are not irrevocable and have a 5599 1.1 mrg positive call count. Do those publicly visible functions that 5600 1.1 mrg the user directed us to clone. */ 5601 1.1 mrg for (i = 0; i < tm_callees.length (); ++i) 5602 1.1 mrg { 5603 1.1 mrg bool doit = false; 5604 1.1 mrg 5605 1.1 mrg node = tm_callees[i]; 5606 1.1 mrg if (node->cpp_implicit_alias) 5607 1.1 mrg continue; 5608 1.1 mrg 5609 1.1 mrg a = node->get_availability (); 5610 1.1 mrg d = get_cg_data (&node, true); 5611 1.1 mrg 5612 1.1 mrg if (a <= AVAIL_NOT_AVAILABLE) 5613 1.1 mrg doit = is_tm_callable (node->decl); 5614 1.1 mrg else if (a <= AVAIL_AVAILABLE && is_tm_callable (node->decl)) 5615 1.1 mrg doit = true; 5616 1.1 mrg else if (!d->is_irrevocable 5617 1.1 mrg && d->tm_callers_normal + d->tm_callers_clone > 0) 5618 1.1 mrg doit = true; 5619 1.1 mrg 5620 1.1 mrg if (doit) 5621 1.1 mrg ipa_tm_create_version (node); 5622 1.1 mrg } 5623 1.1 mrg 5624 1.1 mrg /* Redirect calls to the new clones, and insert irrevocable marks. */ 5625 1.1 mrg for (i = 0; i < tm_callees.length (); ++i) 5626 1.1 mrg { 5627 1.1 mrg node = tm_callees[i]; 5628 1.1 mrg if (node->analyzed) 5629 1.1 mrg { 5630 1.1 mrg d = get_cg_data (&node, true); 5631 1.1 mrg if (d->clone) 5632 1.1 mrg ipa_tm_transform_clone (node); 5633 1.1 mrg } 5634 1.1 mrg } 5635 1.1 mrg FOR_EACH_DEFINED_FUNCTION (node) 5636 1.1 mrg if (node->lowered 5637 1.1 mrg && node->get_availability () >= AVAIL_INTERPOSABLE) 5638 1.1 mrg { 5639 1.1 mrg d = get_cg_data (&node, true); 5640 1.1 mrg if (d->all_tm_regions) 5641 1.1 mrg ipa_tm_transform_transaction (node); 5642 1.1 mrg } 5643 1.1 mrg 5644 1.1 mrg /* Free and clear all data structures. */ 5645 1.1 mrg tm_callees.release (); 5646 1.1 mrg irr_worklist.release (); 5647 1.1 mrg bitmap_obstack_release (&tm_obstack); 5648 1.1 mrg free_original_copy_tables (); 5649 1.1 mrg 5650 1.1 mrg FOR_EACH_FUNCTION (node) 5651 1.1 mrg node->aux = NULL; 5652 1.1 mrg 5653 1.1 mrg cgraph_node::checking_verify_cgraph_nodes (); 5654 1.1 mrg 5655 1.1 mrg return 0; 5656 1.1 mrg } 5657 1.1 mrg 5658 1.1 mrg namespace { 5659 1.1 mrg 5660 1.1 mrg const pass_data pass_data_ipa_tm = 5661 1.1 mrg { 5662 1.1 mrg SIMPLE_IPA_PASS, /* type */ 5663 1.1 mrg "tmipa", /* name */ 5664 1.1 mrg OPTGROUP_NONE, /* optinfo_flags */ 5665 1.1 mrg TV_TRANS_MEM, /* tv_id */ 5666 1.1 mrg ( PROP_ssa | PROP_cfg ), /* properties_required */ 5667 1.1 mrg 0, /* properties_provided */ 5668 1.1 mrg 0, /* properties_destroyed */ 5669 1.1 mrg 0, /* todo_flags_start */ 5670 1.1 mrg 0, /* todo_flags_finish */ 5671 1.1 mrg }; 5672 1.1 mrg 5673 1.1 mrg class pass_ipa_tm : public simple_ipa_opt_pass 5674 1.1 mrg { 5675 1.1 mrg public: 5676 1.1 mrg pass_ipa_tm (gcc::context *ctxt) 5677 1.1 mrg : simple_ipa_opt_pass (pass_data_ipa_tm, ctxt) 5678 1.1 mrg {} 5679 1.1 mrg 5680 1.1 mrg /* opt_pass methods: */ 5681 1.1 mrg virtual bool gate (function *) { return flag_tm; } 5682 1.1 mrg virtual unsigned int execute (function *) { return ipa_tm_execute (); } 5683 1.1 mrg 5684 }; // class pass_ipa_tm 5685 5686 } // anon namespace 5687 5688 simple_ipa_opt_pass * 5689 make_pass_ipa_tm (gcc::context *ctxt) 5690 { 5691 return new pass_ipa_tm (ctxt); 5692 } 5693 5694 #include "gt-trans-mem.h" 5695