From patchwork Mon Nov 26 23:04:50 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 152098 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp494356ljp; Mon, 26 Nov 2018 15:07:39 -0800 (PST) X-Google-Smtp-Source: AFSGD/XmuyvF0AWquSOrGPhCRoRZPJg2S5S7agY6HN2STNaBEze6lu/hkY7uGEvLmGgHZzYdJy/9 X-Received: by 2002:a25:a091:: with SMTP id y17-v6mr29941254ybh.157.1543273659751; Mon, 26 Nov 2018 15:07:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543273659; cv=none; d=google.com; s=arc-20160816; b=x5eoYF7dL2SQPXWaUOjVT+43aYHh57E3aIXAtG2xGfl9N+WuEve/N8nqMi6Hzkobw8 H8U47jnPOcrFZaZHpIPyaM3MCgD2dMwmKOiVxVbRGqzBfpvC/RBIeyoteLOxMf7yGWUk vHLiSm3HAGZAeHK/glz8xFCY248ZTp8rsCZeaniPEfp7hsKTFa1uRcdwO25ElgIhcZFk AMMOWLBAYZmt5Sw6D8zc9KfgDpQopHO08QsXbsq/MWNInPl4WgAf/50a0x5TI19aZljX AvpfUr6HqSXyEKI5QVrrv1hdrs2K48XBkbXgw7O8xoxCMprbyY7GzUOgSPOeFqtjxr6/ b7jQ== 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:subject:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=a0XirnQ7KM4RULalH+OyIr7voieSl1aNO3zcM/erWq4=; b=fSoRuJAtne+WR+15FNnyLfk8JAx8NbIvCk1qO7EHiCFu8Bygr0LJ2CKZFuXiJzxaUC O5SsJR4Yzeeb4RYoj94ElQlzJ9VfEKszKXQCsSBXIHqmifWmGI7OIUFy6VGskXLIsbxo bUgoJPJI05sHPT9dxWaP1mNadNcwVgU5wqDP20f3vG31p+48fcqM+be8QEmYKSJgoRLX 26u8MAw31v7Oemjw+M6H5Eewa/DH5xjAna55ONKLjc4WJfSANW328IGOlf5Fbk52hF7k 9RrqIyRe7sRz4ezgN33G+H6I+2aE1gFF8f++fovKOg8SjJenBmVOhRXUkLq6TshGFiWa WMuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=NdyQs752; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 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. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 131-v6si1245318ybv.124.2018.11.26.15.07.39 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 26 Nov 2018 15:07:39 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=NdyQs752; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 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 ([::1]:39246 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRPyd-0005Cd-6T for patch@linaro.org; Mon, 26 Nov 2018 18:07:39 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43094) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gRPwH-0002oJ-Ir for qemu-devel@nongnu.org; Mon, 26 Nov 2018 18:05:16 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gRPwC-0000Px-PD for qemu-devel@nongnu.org; Mon, 26 Nov 2018 18:05:13 -0500 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:33436) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gRPw5-0000H5-NP for qemu-devel@nongnu.org; Mon, 26 Nov 2018 18:05:03 -0500 Received: by mail-pf1-x442.google.com with SMTP id c123so6626614pfb.0 for ; Mon, 26 Nov 2018 15:05:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references; bh=a0XirnQ7KM4RULalH+OyIr7voieSl1aNO3zcM/erWq4=; b=NdyQs7524wesy0vigNHMby5pjG+QxRG9kxgP3PK8vJeFL/IJSCjPcV8B0UKIWrvmBI 0PkBmSvPgKyD5KseCTJVcfpVi4U1P7c2mlXstMzDiFNcwW36YJBxXjJ7Oe5CR1EprdGl 7F6INDx0qz3WDrcESXdI0m3ZVjihRfW+2+3/w= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=a0XirnQ7KM4RULalH+OyIr7voieSl1aNO3zcM/erWq4=; b=hR7xk7pwz7TPQ70HxdTZJTqXRR/p3kywIBXOpL5BBDFqA3dnVuEfXk2tv0rvHbYARJ D79ZGWGp5LgMbLgwEwa1CzuYkZxHJibVJ9vBRGa+FzQgnKF/M9roAWhY331nUJ7HT2cs 0cswQuJPaB1RhpQh7oka2DqSYDr/DamXt9DatQJvKCW14/n2Iy4nmhmgx2eVy9PDZ3Wp d0traMKvTUzl2vIwDwOoy/nwBVNNWo8PifXLiLIyizyOIpZVTVBhOROD9YWijBlyFkLK iXh97vS1pYw/nc/rmUSf7ervf3rGvoSzLPysiUm66ieCQV2Ei/ZSfbDfuzmWg9S6Fdry B/JQ== X-Gm-Message-State: AA+aEWaNNPb6m4VSs3MaJVAlzLquueyHqyd8fxnpOtvGI45oof+uNmPI O+5/WXAdG0+0MiYDCHMm4Tt/+I2o76o= X-Received: by 2002:a65:534b:: with SMTP id w11mr26930014pgr.125.1543273498747; Mon, 26 Nov 2018 15:04:58 -0800 (PST) Received: from cloudburst.twiddle.net (97-113-170-180.tukw.qwest.net. [97.113.170.180]) by smtp.gmail.com with ESMTPSA id o13sm1798703pfk.57.2018.11.26.15.04.57 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 26 Nov 2018 15:04:57 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 26 Nov 2018 15:04:50 -0800 Message-Id: <20181126230450.672-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20181126230450.672-1-richard.henderson@linaro.org> References: <20181126230450.672-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::442 Subject: [Qemu-devel] [PATCH 4/4] 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: , 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. Signed-off-by: Richard Henderson --- tcg/tcg.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) -- 2.17.2 Reviewed-by: Emilio G. Cota diff --git a/tcg/tcg.c b/tcg/tcg.c index 31b9b58240..ffbf8f01ad 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -2301,6 +2301,81 @@ static void tcg_la_bb_end(TCGContext *s) } } +/* 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 preceeded 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); + } + } +} + /* Liveness analysis : update the opc_arg_life array to tell if a given input arguments is dead. Instructions updating dead temporaries are removed. */ @@ -3537,6 +3612,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) {