From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qt1-x832.google.com (mail-qt1-x832.google.com [IPv6:2607:f8b0:4864:20::832]) by sourceware.org (Postfix) with ESMTPS id 2E5C2394B02A for ; Thu, 2 Jun 2022 08:26:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2E5C2394B02A Received: by mail-qt1-x832.google.com with SMTP id hh4so2900495qtb.10 for ; Thu, 02 Jun 2022 01:26:23 -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:references:in-reply-to:from:date :message-id:subject:to:cc; bh=F2ZRuxV+Wxb+JllD/1D2y9TDG+yfrpApBbQg7UjzzSI=; b=aAFs76/A0rLKaQC7qKG/6LJ/cCZPnvBXMfx2X1ksmADKotms0XHyqdMwzRnmjNoImb Ixx5sKvlX8NxThr0IjrB9rr/Bgv6IfCW9i3hVifiZRzEmi3l3Da/xHrnJIGcdtvTLxtX kPqnh6740krwyC2zmkVXtaYJsflmevOsPfW5pmmiRfhwNQ6Z4/553eoTZVWIB0aVx2x5 iIs8ZhtMpqSgHeupQmckWeWu7gQ86SGm9schQx2K+fgI/U2/DHiYYMoG/toJCAG1o5h2 L5NdB395X8ao4s9FajHGXA7BkBxex3la83zTBtvZ6kV9KtFCz9gqJcLfuF1B2tl3UcaX 0J6w== X-Gm-Message-State: AOAM531PrWXdCU/ZIBHoTO5Lo7LNS/ZUXjO25w78cvaTaNoPgnuZE/g5 428J6mVP+BqSWzGNvQM4CmBupmE/wW+FN4jf088eRgIVKME= X-Google-Smtp-Source: ABdhPJwkP6E6KCxIsyrLjesZR+fyEhWP6h43Any0V7PEYNe6817AUEMu9u0LVcRfk68rbJVPOnfVyshYuqfTDjFUTDQ= X-Received: by 2002:ac8:5ccb:0:b0:304:baf5:1990 with SMTP id s11-20020ac85ccb000000b00304baf51990mr2763604qta.224.1654158382421; Thu, 02 Jun 2022 01:26:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Thu, 2 Jun 2022 10:26:11 +0200 Message-ID: Subject: Re: Hoisting built-in call out of loop - LIM pass [tree-ssa-loop-im.c] To: Shubham Narlawar Cc: GCC Development Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, 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 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: Thu, 02 Jun 2022 08:26:25 -0000 On Wed, Jun 1, 2022 at 3:59 PM Shubham Narlawar via Gcc wrote: > > 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. Where you generate the function declaration for the target builtin make sure to mark it as 'const', which can be done by setting TREE_READONLY on the function declaration to 1 (see gcc/c-family/c-attribs.cc:handle_const_attribute). That should do the trick but will also expose the builtin to the subject of CSE and DCE (but I guess that's OK). Richard. > Thanks and Regards, > Shubham