From patchwork Tue Dec 25 20:55:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 154510 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp5226318ljp; Tue, 25 Dec 2018 13:28:03 -0800 (PST) X-Google-Smtp-Source: AFSGD/X4QmDR0AFMfBl8RG7w7570FBB+FC7Ziq0ynSFueWFf48fCcUFoNy5tIrLW+UAuoOyl7bUM X-Received: by 2002:ac8:3f2d:: with SMTP id c42mr16513809qtk.33.1545773282980; Tue, 25 Dec 2018 13:28:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545773282; cv=none; d=google.com; s=arc-20160816; b=dTfXt9NFZPb5TZZya5Ok1FoVqNK3n+EO1pRgmolFZ9fQiqxPVgcLllQLb4smp9Ryi6 /ZgvZmC2h+MJMbG0EnbZEVrJwVxj4bLHsAlYhpZi5eaKZAP0PW8/iiw7dq2NBm02Dv2o i7fqjkZ6qy744SEf0PuRYg/Zoy/42/IBDK9qxhLYpgu1L6HrVQ9EF5+SCqeTUn2y1Pj2 pEhCxAVX8CYOa27PqEz3UTSCZzFt5dWWjhsrhL4HGr9ykzA0eOKXbVuBl9BvM7aUC+Y2 KHUyc8J7/3vezZTTITKuJhC31DZw6ub4PXvm7C9UEgqIN7xLQpzGWC9UfSN6vX5yR/JZ uajw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=vWAeq5ZoD7ZS2xyd8XFJnj7+EV8VE96ympvHEkdeGZ8=; b=qRyeP2D7qm2OkZMpQip1AT7fPJkGzYP43pF5pNZ8hwOhQdyzxvdWasUuldXTlfRfQx t7B6hU4Sx8iLhw9Vxkv6ur4MayQfGstBK2izPFryy0W2RrFKajoI8OFnaX66nno2JQfc zsmX1NsLHMnqBuM/O3UA9MH+bL2kzqMxJIiF2dr6B9YXysktM2B+tS2jPZA2h+tYHfr8 jXCFFKrckiNLq14I7b9Y3BRgiOs520ZktjVZ4f8vRs9hj2troOm2dsbqqfZ4t8uQUyTR zXMv0XgEEXgnJ9T0Ol9k2O5Y/+noO86FDFTU1KyFIW6PW4jf39FjbX5OrYZNv92Q+sJd iGiA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=bO8Cc3Qr; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [208.118.235.17]) by mx.google.com with ESMTPS id w64si326484qte.374.2018.12.25.13.28.02 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 25 Dec 2018 13:28:02 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=bO8Cc3Qr; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:43371 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbuF8-0001jK-HB for patch@linaro.org; Tue, 25 Dec 2018 16:28:02 -0500 Received: from eggs.gnu.org ([208.118.235.92]:50543) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gbtnH-0007Pi-SC for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:59:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gbtnF-000161-Vw for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:59:15 -0500 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]:33443) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gbtnF-00013F-Px for qemu-devel@nongnu.org; Tue, 25 Dec 2018 15:59:13 -0500 Received: by mail-pf1-x436.google.com with SMTP id c123so7088468pfb.0 for ; Tue, 25 Dec 2018 12:59:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=vWAeq5ZoD7ZS2xyd8XFJnj7+EV8VE96ympvHEkdeGZ8=; b=bO8Cc3QrSrdvtHVCz2SWPYGass0kq++UmVjfL91PbDjfm26g26DN/0f1SlesXRwWKR lsfimpFfpwUBkEo4u+zqkr+NzuR30NFfv5leOoYuuoU9OcE3OjMrSDmy+azW5VXSbX0N g6DOG5yaRmicg55Y6GAMw/c8HERIUpm9RuHUI= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=vWAeq5ZoD7ZS2xyd8XFJnj7+EV8VE96ympvHEkdeGZ8=; b=KXRmvJU+J/908xXKmi9HVLFlwPjqzI4cEKobuCmKuM0txLTf1KD3TqQwJ3soaf4Kjg RT+c2zS3Cac/MTC7RZFgdJlIOvJ5+Iws4YTZT7s/P9se3CDnJL5vwX4NJ7EFJGNkv3Y+ 4cC0Mw+x3wsvXRrcxSqk2wLFEyMg38HHXetQC8A1iS8CzaJjWPjczJMFXekrSmrleqz0 ubM2lxfe3ZAW7NO+PhRifvH/qTkR/cNHrDzQ9n1KWLhQEabV4nH1U/5KMnFxJnQ4s++u eVwdCggDDgbJ+tG3j6NH6HeJktVh0Ji/ESQAIsiFrdWcGQ/X0jHonnNEFk/iS0kNJUrP GE2g== X-Gm-Message-State: AJcUukfuWQy0lXiXHopDoNBcZMr+JOmxkNgI0iVN3ksBi/qecqlQ+qxH LoQoxUQHz5YCU0pd4TRx601GW2o8j9A= X-Received: by 2002:a63:104d:: with SMTP id 13mr16701824pgq.303.1545771551111; Tue, 25 Dec 2018 12:59:11 -0800 (PST) Received: from cloudburst.home (c211-28-135-144.sunsh3.vic.optusnet.com.au. [211.28.135.144]) by smtp.gmail.com with ESMTPSA id t21sm48501628pgg.24.2018.12.25.12.59.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 25 Dec 2018 12:59:10 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 26 Dec 2018 07:55:16 +1100 Message-Id: <20181225205529.10874-30-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181225205529.10874-1-richard.henderson@linaro.org> References: <20181225205529.10874-1-richard.henderson@linaro.org> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::436 Subject: [Qemu-devel] [PULL 29/42] tcg: Add reachable_code_pass X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Delete trivially dead code that follows unconditional branches and noreturn helpers. These can occur either via optimization or via the structure of a target's translator following an exception. Reviewed-by: Emilio G. Cota Signed-off-by: Richard Henderson --- tcg/tcg.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) -- 2.17.2 diff --git a/tcg/tcg.c b/tcg/tcg.c index 99afc65126..d2be550ab4 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -2239,6 +2239,81 @@ TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc) return new_op; } +/* Reachable analysis : remove unreachable code. */ +static void reachable_code_pass(TCGContext *s) +{ + TCGOp *op, *op_next; + bool dead = false; + + QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { + bool remove = dead; + TCGLabel *label; + int call_flags; + + switch (op->opc) { + case INDEX_op_set_label: + label = arg_label(op->args[0]); + if (label->refs == 0) { + /* + * While there is an occasional backward branch, virtually + * all branches generated by the translators are forward. + * Which means that generally we will have already removed + * all references to the label that will be, and there is + * little to be gained by iterating. + */ + remove = true; + } else { + /* Once we see a label, insns become live again. */ + dead = false; + remove = false; + + /* + * Optimization can fold conditional branches to unconditional. + * If we find a label with one reference which is preceded by + * an unconditional branch to it, remove both. This needed to + * wait until the dead code in between them was removed. + */ + if (label->refs == 1) { + TCGOp *op_prev = QTAILQ_PREV(op, TCGOpHead, link); + if (op_prev->opc == INDEX_op_br && + label == arg_label(op_prev->args[0])) { + tcg_op_remove(s, op_prev); + remove = true; + } + } + } + break; + + case INDEX_op_br: + case INDEX_op_exit_tb: + case INDEX_op_goto_ptr: + /* Unconditional branches; everything following is dead. */ + dead = true; + break; + + case INDEX_op_call: + /* Notice noreturn helper calls, raising exceptions. */ + call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1]; + if (call_flags & TCG_CALL_NO_RETURN) { + dead = true; + } + break; + + case INDEX_op_insn_start: + /* Never remove -- we need to keep these for unwind. */ + remove = false; + break; + + default: + break; + } + + if (remove) { + tcg_op_remove(s, op); + } + } +} + #define TS_DEAD 1 #define TS_MEM 2 @@ -3515,6 +3590,7 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb) atomic_set(&prof->la_time, prof->la_time - profile_getclock()); #endif + reachable_code_pass(s); liveness_pass_1(s); if (s->nb_indirects > 0) {