On Mon, Jan 15, 2024 at 02:54:26PM +0000, Qing Zhao wrote: > So, before gimplification, when inserting tree node, we don’t need manually > add unshare_expr since the gimplification will automatically unshare nodes.
There are cases where unshare_expr is needed even then, such as the uses in the sanitizer, because code is then modifying suboperands in place later on and if things are shared bad things happen. If trees can be shared until they are unshared before gimplification, one doesn't need to worry about it, sure. > However, during or after gimplfication, when inserting nodes, we should > manually > add unshare_expr when we put the same “tree” into multiple operands. Yes. > > Using a SAVE_EXPR avoids redundant code but it also requires > > that the SAVE_EXPR uses are ordered. > > “Require the SAVE_EXPR uses are ordered”, does this mean that > SAVE_EXPRs for the same node should be in a correct order? Or something else? The basic requirement is that SAVE_EXPR is evaluated somewhere in a code which dominates all other uses of the SAVE_EXPR. Say SAVE_EXPR <something_complex>, if (x) use1 (SAVE_EXPR <something_complex>); else use2 (SAVE_EXPR <something_complex>); is fine, but if (x) use1 (SAVE_EXPR <something_complex>); else use2 (SAVE_EXPR <something_complex>); is not. Because in the latter case, it will be gimplified into evaluating the complex expression in the conditional code guarded on if (x != 0), save into some temporary variable and then in the else code just use that temporary variable, except it is uninitialized then. Jakub