From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 168753858C52 for ; Sat, 16 Mar 2024 17:35:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 168753858C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 168753858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::634 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710610534; cv=none; b=pZc/72+qMN2+jvjUrgusYkoBEWf7a0ZeuLn5QHXBfOhg8Jh1Wq8nefLTR7ObyWFQOnGdGP/LdFGodcZxaqFMLWRF1dJYwMVB2yfAz1LMw1eKEsuuckFQ07z8MILz6s1tSq6USHkC+YcjC5NcrxQN9GDebfeUU1bRH1e4lmyqXv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710610534; c=relaxed/simple; bh=7B5PoFYLrsadiNUXVMPB3I23l5jGZ+zFWpCo2K4jXiQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=WyO4Ba92wC55Dedu2diTXOTA2kV0aMDB4+99Bh0rGI30RlgviBnlgYKZyGGfljSQ5tVRrapnacr4+70iEduO9MfUFU0iIUy5KQW5/eOIEV05DiCH7rwTCZqbT7iZviqi+uRrkT/PUxwFEMxOQgO43pkuzGRSGbB+oEYww0ZNBBY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1def2a1aafaso11133075ad.3 for ; Sat, 16 Mar 2024 10:35:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1710610531; x=1711215331; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aIk2t6DazPhPAajEAmnNjdrJrgPym60LE4hKNdSyHEk=; b=BAmt7W+bsNHaVT+8MCF50ybO1LdDDWxvLD5V+3LgN0Ng5pC6S5E2YTwTKDxFJ068Wv 6C/nB6IItDss+RBO/H57nloNmJ6rj3z839p+97BQf3IJj9R5p8owh99u8IqLQhptP6KS KsW+Ze3vxX0y1QzpiQZ/7Eh8qV1tZOY8r5O5Dg23642FaksBViEgX6dmDU+4uKJlHH1w hlf0uPAo5oj6iz0M64QVEcZepBCkkxoIEBJs74YTacMBj8dC2tYHa9MLnTlkypys/eyg NnL8YBt+rXGgqGKWy0V1Xs9dj+gxZ182Rkk48m/sdw1LpsRjxxvx7kWyMPzydJADm91+ 5kaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710610531; x=1711215331; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aIk2t6DazPhPAajEAmnNjdrJrgPym60LE4hKNdSyHEk=; b=cRLV4/nBUYMMlq5qjAX8QhFRL2AhXLHZzs1HuGTNm7dkzQ/VQ8uhcw+ekhiAIkUJPx kfPdQija/OI045QyOJuB4pinRCFVkcWd2vCdPLslFLo1laW9gBmcZKnY5qvlCxsIQprT JewEiKRSkmSM6qCxSwo2MSITypLpWQxxcwGQhqUDqbK6NlmyaosbmKS0VK6/goQ6aOMm r0myCKCcz0Qug7nE8w/oHgCspBBQuEkz92uh7GAaYQ74LaKlF7KiXkNDdAGuN6FOfvzD 3LctDLWAG6nrCD1Ene3Sye7LqGIJFABIobYRuDCwkBt/OXDTx/vR2vgZ8UvZ4K9YOs8U a/0A== X-Gm-Message-State: AOJu0Yy8bwZQQZfbTqIGXIMJumKuqrL28cJfepL7qzGiIIMjfB4BQ0EI rvkNPTvTlsQXU+JMcvkhFpK+PDlAsSlf48QQhglCeyiGS8tq0iFhMe3ne8W4jur0cgwYnZaIiTg 9 X-Google-Smtp-Source: AGHT+IFhdjkW2in4mDmj5X+DJl+7xmkvNdTq8B3LRDfldaAMIBFBXoDLuhe36cFpRpPuPHsNti7iiw== X-Received: by 2002:a17:902:ea08:b0:1db:55cc:d226 with SMTP id s8-20020a170902ea0800b001db55ccd226mr6187966plg.66.1710610530702; Sat, 16 Mar 2024 10:35:30 -0700 (PDT) Received: from vineet-framework.ba.rivosinc.com (c-24-5-188-125.hsd1.ca.comcast.net. [24.5.188.125]) by smtp.gmail.com with ESMTPSA id z7-20020a170903018700b001def777afc5sm3128185plg.77.2024.03.16.10.35.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Mar 2024 10:35:30 -0700 (PDT) From: Vineet Gupta To: gcc-patches@gcc.gnu.org Cc: Jeff Law , kito.cheng@gmail.com, Palmer Dabbelt , gnu-toolchain@rivosinc.com, Robin Dapp , Vineet Gupta Subject: [gcc-15 2/3] RISC-V: avoid LUI based const mat: keep stack offsets aligned Date: Sat, 16 Mar 2024 10:35:23 -0700 Message-Id: <20240316173524.1147760-3-vineetg@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240316173524.1147760-1-vineetg@rivosinc.com> References: <20240316173524.1147760-1-vineetg@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,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 List-Id: Noticed that new sum of two s12 splitter was generating following: | 059930 : | 59930: add sp,sp,-16 | 59934: lui t0,0xfffff | 59938: sd s0,0(sp) | 5993c: sd ra,8(sp) | 59940: add sp,sp,t0 | 59944: add s0,sp,2047 <---- | 59948: mv a2,a0 | 5994c: mv a3,a1 | 59950: mv a0,sp | 59954: li a4,1 | 59958: lui a1,0x1 | 5995c: add s0,s0,1 <--- | 59960: jal 59a3c SP here becomes unaligned, even if transitively which is undesirable as well as incorrect: - ABI requires stack to be 8 byte aligned - asm code looks weird and unexpected - to the user it might falsely seem like a compiler bug even when not, specially when staring at asm for debugging unrelated issue. Fix this by using 2032+addend idiom when handling register operands related to stack. This only affects positive S12 values, negative values are already -2048 based. Unfortunately implementation requires making a copy of splitter, since it needs different varaints of predicate and constraint which cant be done conditionally in the same MD pattern (constraint with restricted range prevents LRA from allowing such insn despite new predicate) With the patch, we get following correct code instead: | .. | 59944: add s0,sp,2032 | .. | 5995c: add s0,s0,16 gcc/Changelog: * config/riscv/riscv.h: Add alignment arg to new macros. * config/riscv/constraints.md: Variant of new constraint. * config/riscv/predicates.md: Variant of new predicate. * config/riscv/riscv.md: Variant of new splitter which offsets off of 2032 (vs. 2047). Gate existing splitter behind riscv_reg_frame_related. * config/riscv/riscv.cc (riscv_reg_frame_related): New helper to conditionalize the existing and new spitters. * config/riscv/riscv-protos.h: Add new prototype. Signed-off-by: Vineet Gupta --- gcc/config/riscv/constraints.md | 6 ++++++ gcc/config/riscv/predicates.md | 8 ++++++- gcc/config/riscv/riscv-protos.h | 1 + gcc/config/riscv/riscv.cc | 11 ++++++++++ gcc/config/riscv/riscv.h | 15 ++++++++----- gcc/config/riscv/riscv.md | 37 ++++++++++++++++++++++++++++++--- 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/gcc/config/riscv/constraints.md b/gcc/config/riscv/constraints.md index 435461180c7e..a9446c54ee45 100644 --- a/gcc/config/riscv/constraints.md +++ b/gcc/config/riscv/constraints.md @@ -86,6 +86,12 @@ (ior (match_test "IN_RANGE (ival, 2048, 4094)") (match_test "IN_RANGE (ival, -4096, -2049)")))) +(define_constraint "MiA" + "const can be represented as sum of two S12 values with first aligned." + (and (match_code "const_int") + (ior (match_test "IN_RANGE (ival, 2033, 4064)") + (match_test "IN_RANGE (ival, -4096, -2049)")))) + (define_constraint "Ds3" "@internal 1, 2 or 3 immediate" diff --git a/gcc/config/riscv/predicates.md b/gcc/config/riscv/predicates.md index 89490339c2da..56f9919daafa 100644 --- a/gcc/config/riscv/predicates.md +++ b/gcc/config/riscv/predicates.md @@ -423,7 +423,13 @@ (define_predicate "const_two_s12" (match_code "const_int") { - return SUM_OF_TWO_S12 (INTVAL (op)); + return SUM_OF_TWO_S12 (INTVAL (op), false); +}) + +(define_predicate "const_two_s12_algn" + (match_code "const_int") +{ + return SUM_OF_TWO_S12 (INTVAL (op), true); }) ;; CORE-V Predicates: diff --git a/gcc/config/riscv/riscv-protos.h b/gcc/config/riscv/riscv-protos.h index b87355938052..f9e407bf5768 100644 --- a/gcc/config/riscv/riscv-protos.h +++ b/gcc/config/riscv/riscv-protos.h @@ -164,6 +164,7 @@ extern bool riscv_shamt_matches_mask_p (int, HOST_WIDE_INT); extern void riscv_subword_address (rtx, rtx *, rtx *, rtx *, rtx *); extern void riscv_lshift_subword (machine_mode, rtx, rtx, rtx *); extern enum memmodel riscv_union_memmodels (enum memmodel, enum memmodel); +extern bool riscv_reg_frame_related (rtx); /* Routines implemented in riscv-c.cc. */ void riscv_cpu_cpp_builtins (cpp_reader *); diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 680c4a728e92..38aebefa2590 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -6667,6 +6667,17 @@ riscv_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to) return (to == HARD_FRAME_POINTER_REGNUM || to == STACK_POINTER_REGNUM); } +/* Helper to determine if reg X pertains to stack. */ +bool +riscv_reg_frame_related (rtx x) +{ + return REG_P (x) + && (REGNO (x) == FRAME_POINTER_REGNUM + || REGNO (x) == HARD_FRAME_POINTER_REGNUM + || REGNO (x) == ARG_POINTER_REGNUM + || REGNO (x) == VIRTUAL_STACK_VARS_REGNUM); +} + /* Implement INITIAL_ELIMINATION_OFFSET. FROM is either the frame pointer or argument pointer. TO is either the stack pointer or hard frame pointer. */ diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h index 817661058896..00964ccd81db 100644 --- a/gcc/config/riscv/riscv.h +++ b/gcc/config/riscv/riscv.h @@ -633,11 +633,16 @@ enum reg_class #define SUM_OF_TWO_S12_N(VALUE) \ (((VALUE) >= (-2048 * 2)) && ((VALUE) <= (-2048 - 1))) -#define SUM_OF_TWO_S12_P(VALUE) \ - (((VALUE) >= ( 2047 + 1)) && ((VALUE) <= ( 2047 * 2))) - -#define SUM_OF_TWO_S12(VALUE) \ - (SUM_OF_TWO_S12_N (VALUE) || SUM_OF_TWO_S12_P (VALUE)) +/* Variant with first value 8 byte aligned if involving stack regs. */ +#define SUM_OF_TWO_S12_P(VALUE, WANT_ALIGN) \ + ((WANT_ALIGN) \ + ? (((VALUE) >= (2032 + 1)) && ((VALUE) <= (2032 * 2))) \ + : ((VALUE >= (2047 + 1)) && ((VALUE) <= (2047 * 2)))) + +#define SUM_OF_TWO_S12(VALUE, WANT_ALIGN) \ + (SUM_OF_TWO_S12_N (VALUE) \ + || ((SUM_OF_TWO_S12_P (VALUE, false) && !(WANT_ALIGN)) \ + || (SUM_OF_TWO_S12_P (VALUE, true) && (WANT_ALIGN)))) /* If this is a single bit mask, then we can load it with bseti. Special handling of SImode 0x80000000 on RV64 is done in riscv_build_integer_1. */ diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md index 79fe861ef91f..cc8c3c653f3e 100644 --- a/gcc/config/riscv/riscv.md +++ b/gcc/config/riscv/riscv.md @@ -760,16 +760,17 @@ [(set (match_operand:P 0 "register_operand" "=r,r") (plus:P (match_operand:P 1 "register_operand" " r,r") (match_operand:P 2 "const_two_s12" " MiG,r")))] - "" + "!riscv_reg_frame_related (operands[0]) + && !riscv_reg_frame_related (operands[1])" "add %0,%1,%2" - "" + "&& 1" [(set (match_dup 0) (plus:P (match_dup 1) (match_dup 3))) (set (match_dup 0) (plus:P (match_dup 0) (match_dup 4)))] { int val = INTVAL (operands[2]); - if (SUM_OF_TWO_S12_P (val)) + if (SUM_OF_TWO_S12_P (val, false)) { operands[3] = GEN_INT (2047); operands[4] = GEN_INT (val - 2047); @@ -785,6 +786,36 @@ [(set_attr "type" "arith") (set_attr "mode" "")]) +(define_insn_and_split "*add3_const_sum_of_two_s12_stack" + [(set (match_operand:P 0 "register_operand" "=r,r") + (plus:P (match_operand:P 1 "register_operand" " r,r") + (match_operand:P 2 "const_two_s12_algn" " MiA,r")))] + "riscv_reg_frame_related (operands[0]) + || riscv_reg_frame_related (operands[1])" + "add %0,%1,%2" + "&& 1" + [(set (match_dup 0) + (plus:P (match_dup 1) (match_dup 3))) + (set (match_dup 0) + (plus:P (match_dup 0) (match_dup 4)))] +{ + int val = INTVAL (operands[2]); + if (SUM_OF_TWO_S12_P (val, true)) + { + operands[3] = GEN_INT (2032); + operands[4] = GEN_INT (val - 2032); + } + else if (SUM_OF_TWO_S12_N (val)) + { + operands[3] = GEN_INT (-2048); + operands[4] = GEN_INT (val + 2048); + } + else + gcc_unreachable (); +} + [(set_attr "type" "arith") + (set_attr "mode" "")]) + (define_expand "addv4" [(set (match_operand:GPR 0 "register_operand" "=r,r") (plus:GPR (match_operand:GPR 1 "register_operand" " r,r") -- 2.34.1