From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by sourceware.org (Postfix) with ESMTPS id B45D0385608C for ; Wed, 1 Jun 2022 13:58:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B45D0385608C Received: by mail-wr1-x42c.google.com with SMTP id s24so2468627wrb.10 for ; Wed, 01 Jun 2022 06:58:14 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=YGdELvmZ0t7UryOD9uPIB7PEgotUb1/4Ks9DXhJ0F/c=; b=EPPZa1wT80YmsXq63nemw5UsNn/D1CtdzAdKMJd/sc30nTdSdDN7d99TzgAggsitYE jVpA+WzneLaF0PSQoE9A5uXVb6YblTtRIMvs9b93gNg+b48Xlhxu1SYAIo1rvT946pBw 0jOhYTUu32fTSyIC3dbyDvUWF55pIINllauL6PzWw6yQvpA+qXcD1KmIcKONpyPu9CaP 9xelP3ILi/w2Qf19bbotxX/erD9aeqaL/Mg+9nKeCek56/TUYt7Mtx87yIb2jzaO8nPj dxQMN0dqqrjv21c+aiOBJvLAcB70BEvV5Sz5oUfUsWKYgXIGYrNp/qo6apZWendpNO4w CVGQ== X-Gm-Message-State: AOAM533RI/wQuPqggQj4lrmmQ/dUvMZZHMobeWPoSO7PVwZl0kquiRzX Qx1ykvLoc0aDB0sUBFJzYGvAAEC9iW1xUrU2nLKZ8rSq0Y8= X-Google-Smtp-Source: ABdhPJyWZGdmos2cRR0+jLB5BQ/FivDilYnqO5k9VsJkm8Bka1OuDD6Y2XdIXo+ltlGY5Eupw/Ip+4VO4o7pvyLLooQ= X-Received: by 2002:adf:f704:0:b0:211:7eed:4412 with SMTP id r4-20020adff704000000b002117eed4412mr1814929wrp.165.1654091893078; Wed, 01 Jun 2022 06:58:13 -0700 (PDT) MIME-Version: 1.0 From: Shubham Narlawar Date: Wed, 1 Jun 2022 19:27:59 +0530 Message-ID: Subject: Hoisting built-in call out of loop - LIM pass [tree-ssa-loop-im.c] To: GCC Development X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 01 Jun 2022 13:58:16 -0000 Hello, [1] I want to achieve hosting of a custom built-in call out of loop that loads an immediate value. for (int i = 0; i < 4000; i++) { _9 = (unsigned int) _1; slli_6 = _9 << 8; srli_36 = slli_6 >> 8; li_37 = __builtin_load_immediate (15); //I want to hoist this out of loop which will cause hoisting of add_39 stmt also add_39 = srli_36 + li_37; _10 = __builtin_foo (add_39, 1, _9); } First 5 instructions in the above loop are being inserted by a custom gimple plugin whenever it sees a __builtin_foo(). LICM pass () could hoist below instructions out of loop - _9 = (unsigned int) _1; slli_6 = _9 << 8; srli_36 = slli_6 >> 8; I want to hoist below gimple statement out of loop li_22 = __builtin_riscv_load_immediate(15); //BUILTIN_MD I am generating above builtin call using a custom GCC gimple plugin using - tree li_tmp_name = make_temp_ssa_name (integer_type_node, NULL, "li"); tree load = build_int_cst (unsigned_type_node, 15); gcall *li_stmt = gimple_build_call (decl, 1); //where decl is function_decl gimple_call_set_lhs (li_stmt, li_tmp_name); gimple_call_set_arg (li_stmt, 0, load); gsi_insert_after (&gsi, li_stmt, GSI_NEW_STMT); How do I make a GIMPLE function call as const such that loop invariant code motion hoist it out of the loop. [2] I tried below approaches to make it happen - a. I made load_immediate decl as constant but that did not help TREE_CONSTANT(decl) = 1 b. I teached licm pass to forcefull move out __builtin_load_immediate() by introducing various checks but in the end, it alters function cfg and some other pass like ""tree-ssa-loop-manip.c"" crashes here - /* We should have met DEF_BB along the way. */ gcc_assert (pred != ENTRY_BLOCK_PTR_FOR_FN (cfun)); >From above approaches, making changes in licm pass is proving to be costly and inappropriate. Is there any other way I can achieve it? I am guessing making it a constant gimple function call with no side effects will be hoisted automatically by LICM. If yes, how can I make a gimple call as constant so that it is hoisted out by LICM. Also, please suggest any other approaches to achieve it. Thanks and Regards, Shubham