From patchwork Thu Nov 29 16:48:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 30395 Received: (qmail 34045 invoked by alias); 29 Nov 2018 16:50:06 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 27906 invoked by uid 89); 29 Nov 2018 16:48:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-wr1-f66.google.com Received: from mail-wr1-f66.google.com (HELO mail-wr1-f66.google.com) (209.85.221.66) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Nov 2018 16:48:25 +0000 Received: by mail-wr1-f66.google.com with SMTP id 96so2613960wrb.2 for ; Thu, 29 Nov 2018 08:48:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=3zJaGYUjj7rw/MUThcauU5fyT/4YZcFZ53+IxSxKATQ=; b=XC8PXbBLDD9tXE7QchZX8QXwYFXt9ZDwsafeWOXG9QR9I9OqngGib44Ul94XioIbAW 2PK3iGCzQDm4ZUaWNlqrAsGnHZKcuZspzShTCaMdBV2VONDHaHrF5A/mPup7EPlMdk3w 7tjEQvWEmGUm4rx6N+zkbFw0KmUR9a0G9kbkhgDtCTMAkq1cTS8M8UrQPBkQHG4piZb/ OpLKgnWP7A/mc6RsGhNRefpnEpCldTRMkxkudE/4m0PfMukUnMahaLZEF04xIqeyn1zC kzk6hC8u2M4PIJH5VZ1UkX56HXGa6tgq7ZbQEutDBNDtuY4RMS6nFSFJqI5Q8JjZYdSo Lv7A== Return-Path: Received: from localhost (host86-156-236-171.range86-156.btcentralplus.com. [86.156.236.171]) by smtp.gmail.com with ESMTPSA id l37sm3349746wre.69.2018.11.29.08.48.21 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Nov 2018 08:48:22 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: jimw@sifive.com, palmer@sifive.com, jhb@FreeBSD.org, Andrew Burgess Subject: [PATCH 3/4] gdb/riscv: Create each unique target description only once Date: Thu, 29 Nov 2018 16:48:12 +0000 Message-Id: <84ce42bd83dc3d67bd312c4c8772171dd5bf1182.1543509416.git.andrew.burgess@embecosm.com> In-Reply-To: References: In-Reply-To: References: X-IsSubscribed: yes GDB relies on the fact that if two target descriptions have the same contents, then they will be the same object instance (having the same address). One place where this is a requirement is in GDBARCH_LIST_LOOKUP_BY_INFO which is used to find previously created gdbarch objects. In GDBARCH_LIST_LOOKUP_BY_INFO a pointer comparison is made on the gdbarch's target description, if the pointers are different then it is assumed the gdbarches have different, non-compatible target descriptions. Previously we would create duplicate target descriptions in the belief that RISCV_GDBARCH_INIT would spot this duplication and discard the second instance. However, this was incorrect, and instead we ended up creating duplicate gdbarch objects. With this commit every unique feature set will create one and only one target description, the feature set and resulting target description is then cached so that the same target description object can be returned later. Many other target avoid this problem by creating a small number of named target descriptions, and returning one of these. However, we currently have 8 possible target descriptions (32 vs 64 bit for x-reg and f-reg, and h/w or s/w float abi) and creating each of these just to avoid a dynamic cache seems pointless. gdb/ChangeLog: * arch/riscv.c (class riscv_target_desc_cache): New. (riscv_tdesc_cache): New global. (riscv_create_target_description): Look in the cache before creating a new target description. --- gdb/ChangeLog | 7 +++++++ gdb/arch/riscv.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/gdb/arch/riscv.c b/gdb/arch/riscv.c index cb715fabb1f..fef0c403e7b 100644 --- a/gdb/arch/riscv.c +++ b/gdb/arch/riscv.c @@ -24,11 +24,65 @@ #include "../features/riscv/32bit-fpu.c" #include "../features/riscv/64bit-fpu.c" +/* Class used to cache previously created RISC-V target descriptions. */ + +class riscv_target_desc_cache +{ +public: + + /* Lookup a previously created target description for FEATURES, or + return nullptr if no matching target description has ever been + created. */ + const target_desc *lookup (struct riscv_gdbarch_features features) + { + for (auto p : m_tdesc_list) + { + if (p.first == features) + return p.second; + } + + return nullptr; + } + + /* Add TDESC into the cache, a target description created to match + FEATURES. */ + void add (const struct riscv_gdbarch_features features, + const target_desc *tdesc) + { + feature_tdesc_pair p (features, tdesc); + m_tdesc_list.push_back (p); + } + +private: + + /* Map from a feature set to the corresponding target description. */ + typedef std::pair feature_tdesc_pair; + + /* List of all target descriptions we've previously seen. */ + std::vector m_tdesc_list; +}; + +/* Cache of previously seen target descriptions, indexed by the feature set + that created them. */ +static riscv_target_desc_cache riscv_tdesc_cache; + /* See arch/riscv.h. */ const target_desc * riscv_create_target_description (struct riscv_gdbarch_features features) { + /* Have we seen this feature set before? If we have return the same + target description. GDB expects that if two target descriptions are + the same (in content terms) then they will actually be the same + instance. This is important when trying to lookup gdbarch objects as + GDBARCH_LIST_LOOKUP_BY_INFO performs a pointer comparison on target + descriptions to find candidate gdbarch objects. */ + const target_desc *prev_tdesc = riscv_tdesc_cache.lookup (features); + if (prev_tdesc != nullptr) + return prev_tdesc; + + /* Now we should create a new target description. */ target_desc *tdesc = allocate_target_description (); #ifndef IN_PROCESS_AGENT @@ -65,5 +119,8 @@ riscv_create_target_description (struct riscv_gdbarch_features features) else if (features.flen == 8) regnum = create_feature_riscv_64bit_fpu (tdesc, regnum); + /* Add to the cache. */ + riscv_tdesc_cache.add (features, tdesc); + return tdesc; }