On Tue, 2013-04-23 at 15:24 -0600, Jeff Law wrote: > Well, you have to copy the blocks, adjust the edges and rewrite the SSA > graph. I'd use duplicate_block to help. > > You really want to look at tree-ssa-threadupdate.c. There's a nice big > block comment which gives the high level view of what needs to happen > when you copy a block for this kind of optimization. Feel free to > ignore the implementation which has to be fairly efficient when there's > a large number of edges to update. > > Jeff
I think I understand the high level work, it is mapping that hight level description to the low level calls that I am having trouble with. Lets say I have basic blocks A, B, and C, and edges from A to B and B to C. There may also be other edges leading into B and C that I do not want to change. Now I want to make a copy of B (B') and change the A->B edge to be A->B'. I think this would be: B_prime = duplicate_block (B, find_edge (A, B), B); This leads to several questions. Why does it matter what basic block I put this new block 'after' (the 3rd arg to duplicate_block). Do all blocks have at least one edge leading out of them or will a block 'fall' into the next block if there is no succ edges? Or does 'after' just affect where the block shows up in debug tree listings? Looking at the code in tree-ssa-threadupdate.c and trans-mem.c I am guessing I need to call initialize_original_copy_tables before calling duplicate_block and free_original_copy_tables after it, right? I am also assuming I can do a series of duplicate_block calls between the initialize and free calls if I need to. tree-ssa-threadupdate.c has a static routine called update_destination_phis to update the phi's in a new block, I made a copy of this routine and used it in my code to try and update the phi nodes in my new block B'. I am not sure if that is sufficient or not for updating the phi nodes in my new block. Do I also need to do something to adjust the phi nodes in the block I copied (B) So I basically am duplicating blocks with: initialize_original_copy_tables (); B_prime = duplicate_block (B, find_edge (A, B), A); update_destination_phis (B, B_prime); free_original_copy_tables (); But my code segfaults in nearest_common_dominator, called by update_ssa after my pass has changed the code. I think I need to do more to fix any PHI nodes in B and B' but I am not sure what. I also notice that copy_bbs in cfghooks (which calls duplicate_block) has calls to get_immediate_dominator and set_immediate_dominator, maybe I need to copy that code as well? Steve Ellcey sell...@imgtec.com