From patchwork Thu Jul 4 21:00:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 809966 Delivered-To: patch@linaro.org Received: by 2002:adf:a199:0:b0:367:895a:4699 with SMTP id u25csp1334530wru; Thu, 4 Jul 2024 14:02:15 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW02fBVZ+tmDCmjQyOssq2aSW7vefjzv+w62SIi2vCPQ4XVEkqJE898xZgvq4sCyjJLna/DS0T3seYdImzOkjgC X-Google-Smtp-Source: AGHT+IGTPmUuEtDQ+bL+3emj6oXFMDQGSOUwC8pX+J2gDG1ralmJlWtH08tQcejf8jssEqbexHcH X-Received: by 2002:a05:620a:4005:b0:795:5842:2457 with SMTP id af79cd13be357-79eee19eb19mr373901985a.15.1720126934843; Thu, 04 Jul 2024 14:02:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1720126934; cv=none; d=google.com; s=arc-20160816; b=sZZK34HPCcFcpVggFh52bvkChHnhKYzFvoY4Lc2wVsXbc4epXlZqZTwY+wKfHKuakv xGL42ZWhYjHmVyJyweSWHb45qXzB0C4WRV3U9owWCGW0esW7ScAhLUBCMUZ+iZ/X4A2B S2tEAhGVPWki+eOKJKO+fnUKRlxkceMO3geb828lC3uvd1jZWgbeElUYXyQYDRCQ4joj QQSzL+ZdoUlmcMhGIdKwPHbo+0+kG0ZMASa7RRythPBF4HnPIxzxwhXwWqYwwJ0w8ME6 NMai2BwlzLQPgQ70+ANPVS9GDFHk/kyBOP4GU50jUhsnhd08j3+8KLAKr0NDlbtgobHd LVog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from; bh=c2fuvPEXLT/qBQ7Zr3P5m1AYchzV3q1kTRscXa1aiT0=; fh=Ai/7iYl8PqklBRG+OcSfTczWOCBx0/sAgKz4aM3nNvc=; b=qO9rPYJG97e5JJJsxq48agN+/YKRV/nASPQSPY9cnOJmfVC7CGpvw4TlWZDrYhMBet JtKlkRn5pMkmzE023VJ7Ll1oNofccvHnyDIirRI51h+pcJF+/rcN9SaNWy5guYN2i8ba vdLcraQNy6CNZhQVBBduvyrM7Q0YnzT3ZHU9WuSXs/kEBfK+28BVyeLYc7WAqIi5Gbnv by2s7Q5jqyFO5Tdis0BuQVz7C/7GJdgdrWYgWQWjN7uqkcSy/5EZuDD4vuy7MGLqBWGR ZrrN6hjKvHo9HUMA7tlLp3v+Aa/oVjfelrHCiJGIAwAnPFxvn+lgCqefeRGAbWpa47f5 w0hQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-79d6933a2c4si1547610285a.598.2024.07.04.14.02.14 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 04 Jul 2024 14:02:14 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org" Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sPTZz-0007EI-Ch; Thu, 04 Jul 2024 17:01:23 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sPTZv-000728-Ht; Thu, 04 Jul 2024 17:01:19 -0400 Received: from isrv.corpit.ru ([86.62.121.231]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sPTZt-0004Ll-S5; Thu, 04 Jul 2024 17:01:19 -0400 Received: from tsrv.corpit.ru (tsrv.tls.msk.ru [192.168.177.2]) by isrv.corpit.ru (Postfix) with ESMTP id EE12377567; Fri, 5 Jul 2024 00:00:50 +0300 (MSK) Received: from tls.msk.ru (mjt.wg.tls.msk.ru [192.168.177.130]) by tsrv.corpit.ru (Postfix) with SMTP id 10AAFFECA1; Fri, 5 Jul 2024 00:00:56 +0300 (MSK) Received: (nullmailer pid 1507743 invoked by uid 1000); Thu, 04 Jul 2024 21:00:55 -0000 From: Michael Tokarev To: qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, Richard Henderson , Song Gao , Michael Tokarev Subject: [Stable-9.0.2 10/22] tcg/loongarch64: Fix tcg_out_movi vs some pcrel pointers Date: Fri, 5 Jul 2024 00:00:40 +0300 Message-Id: <20240704210055.1507652-10-mjt@tls.msk.ru> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: pass client-ip=86.62.121.231; envelope-from=mjt@tls.msk.ru; helo=isrv.corpit.ru X-Spam_score_int: -68 X-Spam_score: -6.9 X-Spam_bar: ------ X-Spam_report: (-6.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_HI=-5, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Richard Henderson Simplify the logic for two-part, 32-bit pc-relative addresses. Rather than assume all such fit in int32_t, do some arithmetic and assert a result, do some arithmetic first and then check to see if the pieces are in range. Cc: qemu-stable@nongnu.org Fixes: dacc51720db ("tcg/loongarch64: Implement tcg_out_mov and tcg_out_movi") Reviewed-by: Song Gao Reported-by: Song Gao Signed-off-by: Richard Henderson (cherry picked from commit 521d7fb3ebdf88112ed13556a93e3037742b9eb8) Signed-off-by: Michael Tokarev diff --git a/tcg/loongarch64/tcg-target.c.inc b/tcg/loongarch64/tcg-target.c.inc index 06ca1ab11c..8f68bd3e51 100644 --- a/tcg/loongarch64/tcg-target.c.inc +++ b/tcg/loongarch64/tcg-target.c.inc @@ -366,8 +366,7 @@ static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd, * back to the slow path. */ - intptr_t pc_offset; - tcg_target_long val_lo, val_hi, pc_hi, offset_hi; + intptr_t src_rx, pc_offset; tcg_target_long hi12, hi32, hi52; /* Value fits in signed i32. */ @@ -377,24 +376,23 @@ static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd, } /* PC-relative cases. */ - pc_offset = tcg_pcrel_diff(s, (void *)val); - if (pc_offset == sextreg(pc_offset, 0, 22) && (pc_offset & 3) == 0) { - /* Single pcaddu2i. */ - tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2); - return; + src_rx = (intptr_t)tcg_splitwx_to_rx(s->code_ptr); + if ((val & 3) == 0) { + pc_offset = val - src_rx; + if (pc_offset == sextreg(pc_offset, 0, 22)) { + /* Single pcaddu2i. */ + tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2); + return; + } } - if (pc_offset == (int32_t)pc_offset) { - /* Offset within 32 bits; load with pcalau12i + ori. */ - val_lo = sextreg(val, 0, 12); - val_hi = val >> 12; - pc_hi = (val - pc_offset) >> 12; - offset_hi = val_hi - pc_hi; - - tcg_debug_assert(offset_hi == sextreg(offset_hi, 0, 20)); - tcg_out_opc_pcalau12i(s, rd, offset_hi); + pc_offset = (val >> 12) - (src_rx >> 12); + if (pc_offset == sextreg(pc_offset, 0, 20)) { + /* Load with pcalau12i + ori. */ + tcg_target_long val_lo = val & 0xfff; + tcg_out_opc_pcalau12i(s, rd, pc_offset); if (val_lo != 0) { - tcg_out_opc_ori(s, rd, rd, val_lo & 0xfff); + tcg_out_opc_ori(s, rd, rd, val_lo); } return; }