From patchwork Tue Apr 16 21:16:47 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Diego Novillo X-Patchwork-Id: 237111 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 34AE12C010B for ; Wed, 17 Apr 2013 07:16:59 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; q=dns; s= default; b=dP/PevT96es17cqtgdsfnmynPiEzpoKbn4hDMlSE+U2lvRcA0bbns Yib5/Dx8gPPFxbMdCbCwoSZ2+cwGddwZbqldcAw/vny7g9W6w5X+7VBAGg1f4m1M MHGjd2fy2hs+EFgMw67o1CwSnkDD27UDVnhgoSRTKXzDEDUeR/iPq4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; s=default; bh=s8c9bBEex+SVy39Qb+hqv4oREfI=; b=XzbNUtkvlYGM0eenkckL90aP45em IVBwLylxsN6ujwbp4Zt6HzvfkmuzXLR8kz3E8e6BzSOiraU0dzPC1Ti29LLE180Q u+nx4cEbm9mPZ6YjHcc6MjfCMW+tgl5HqbGmuISikbb16ZwuHnzlH+i90rh17/sp 928aAk1QwtNxJ5M= Received: (qmail 31298 invoked by alias); 16 Apr 2013 21:16:52 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 31287 invoked by uid 89); 16 Apr 2013 21:16:52 -0000 X-Spam-SWARE-Status: No, score=-5.7 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_TRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, TW_TM autolearn=ham version=3.3.1 Received: from mail-ie0-f169.google.com (HELO mail-ie0-f169.google.com) (209.85.223.169) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Tue, 16 Apr 2013 21:16:51 +0000 Received: by mail-ie0-f169.google.com with SMTP id ar20so1164066iec.0 for ; Tue, 16 Apr 2013 14:16:49 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:message-id:date:from:organization:user-agent :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding:x-gm-message-state; bh=+A/LEvlwDjQHlDHvwLMXowe1mScr5Hx1AIAiJN0mC5A=; b=l53Sns3VoKKWR5LA//vUn/3LK4INpVUeKhQDE1u6tG/KAfh+0fYEGFTYV/LKxpbOv4 sr3PqWNHa7NiKQ1gxEJjoMgoVX+EGUH7gQDT+RldJjLiyrBswreJvUx+CIdsexcWC1sl hFotuGvCQmUHIchBxv87TZ1xyi2hLHxHsjSq3HX5btsSwo9ZhKpm+Xcuw8HzzKntpN24 E4Mxz4paPqnS+DNcfbHoQlGd3D53tBuau7HdVdsDh9Ch/hE8pZNqieB4wU+kYza31PO3 7/jfSY91OYp/s6nIfhEEVoRZdvVPSWITRqMu8Yn0awvZSkJxahOOnZeygJ7XPJcpEzc7 x68w== X-Received: by 10.50.7.3 with SMTP id f3mr1423106iga.50.1366147009565; Tue, 16 Apr 2013 14:16:49 -0700 (PDT) Received: from dnovillo-macbookpro.local (CPE0026f3355b70-CM0026f3355b6d.cpe.net.cable.rogers.com. [99.230.121.179]) by mx.google.com with ESMTPS id ua6sm17255338igb.0.2013.04.16.14.16.48 (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 16 Apr 2013 14:16:48 -0700 (PDT) Message-ID: <516DBFBF.50608@google.com> Date: Tue, 16 Apr 2013 17:16:47 -0400 From: Diego Novillo User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: Richard Biener , gcc-patches@gcc.gnu.org, Lawrence Crowl , David Li , Michael Matz Subject: Re: [cxx-conversion] RFC - Helper types for building GIMPLE References: <20130313215516.GA29289@google.com> In-Reply-To: X-Gm-Message-State: ALoCoQkmRPKAUNKUCJ/ypDo4FQVqRG89vobO57m2NViQ7s0ctk9PmTb2WZHJI3Mb3Xs3VIbjtCNLzOlANEfCRIp8GXCRLdxhn9QVJ5ILG0GpHWWG8okQ8h3aiI39cibjzokADEw3SZeucA7eUJe6ODW37bFoYHO32667vTerHW+SYJXiT+cZpAuqAKINWNbeZ/SQOxvRQV0o Thanks for the feedback, folks. I've removed the builder type and added some overloads to simplify the creation of gimple assignments. I have only added exactly the functions I needed to simplify a bit of gcc/asan.c. I plan to continue adding and tweaking as I change client code. Some things to note: - The builder type gave us some more abstraction that would be nice to put in gimple itself. However, gimple is now a union and gimple_seq is just a typedef for gimple. So, adding behaviour to them will need to wait until we convert gimple into a proper class. - This variant does not yield as much code savings as the original one, but it can be improved once gimple is a proper class. - I will continue working in trunk. This is not something that can be easily done in a branch since I will be touching a whole bunch of client code and I expect to make incremental changes for the next little while. Tested on x86_64. 2013-04-16 Diego Novillo * gimple.c (create_gimple_tmp): New. (get_expr_type): New. (build_assign): New. (build_type_cast): New. * gimple.h (enum ssa_mode): Define. (gimple_seq_set_location): New. * asan.c (build_check_stmt): Change some gimple_build_* calls to use build_assign and build_type_cast. commit a9c165448358a920e5756881e016865a812a5d81 Author: Diego Novillo Date: Tue Apr 16 14:29:09 2013 -0400 Simplified GIMPLE IL builder functions. + gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING); } else t = shadow; diff --git a/gcc/gimple.c b/gcc/gimple.c index 8bd80c8..64f7b1a 100644 --- a/gcc/gimple.c +++ b/gcc/gimple.c @@ -4207,4 +4207,105 @@ gimple_asm_clobbers_memory_p (const_gimple stmt) return false; } + + +/* Create and return an unnamed temporary. MODE indicates whether + this should be an SSA or NORMAL temporary. TYPE is the type to use + for the new temporary. */ + +tree +create_gimple_tmp (tree type, enum ssa_mode mode) +{ + return (mode == M_SSA) + ? make_ssa_name (type, NULL) + : create_tmp_var (type, NULL); +} + + +/* Return the expression type to use based on the CODE and type of + the given operand OP. If the expression CODE is a comparison, + the returned type is boolean_type_node. Otherwise, it returns + the type of OP. */ + +static tree +get_expr_type (enum tree_code code, tree op) +{ + return (TREE_CODE_CLASS (code) == tcc_comparison) + ? boolean_type_node + : TREE_TYPE (op); +} + + +/* Build a new gimple assignment. The LHS of the assignment is a new + temporary whose type matches the given expression. MODE indicates + whether the LHS should be an SSA or a normal temporary. CODE is + the expression code for the RHS. OP1 is the first operand and VAL + is an integer value to be used as the second operand. */ + +gimple +build_assign (enum tree_code code, tree op1, int val, enum ssa_mode mode) +{ + tree op2 = build_int_cst (TREE_TYPE (op1), val); + tree lhs = create_gimple_tmp (get_expr_type (code, op1), mode); + return gimple_build_assign_with_ops (code, lhs, op1, op2); +} + +gimple +build_assign (enum tree_code code, gimple g, int val, enum ssa_mode mode) +{ + return build_assign (code, gimple_assign_lhs (g), val, mode); +} + + +/* Build and return a new GIMPLE assignment. The new assignment will + have the opcode CODE and operands OP1 and OP2. The type of the + expression on the RHS is inferred to be the type of OP1. + + The LHS of the statement will be an SSA name or a GIMPLE temporary + in normal form depending on the type of builder invoking this + function. */ + +gimple +build_assign (enum tree_code code, tree op1, tree op2, enum ssa_mode mode) +{ + tree lhs = create_gimple_tmp (get_expr_type (code, op1), mode); + return gimple_build_assign_with_ops (code, lhs, op1, op2); +} + +gimple +build_assign (enum tree_code code, gimple op1, tree op2, enum ssa_mode mode) +{ + return build_assign (code, gimple_assign_lhs (op1), op2, mode); +} + +gimple +build_assign (enum tree_code code, tree op1, gimple op2, enum ssa_mode mode) +{ + return build_assign (code, op1, gimple_assign_lhs (op2), mode); +} + +gimple +build_assign (enum tree_code code, gimple op1, gimple op2, enum ssa_mode mode) +{ + return build_assign (code, gimple_assign_lhs (op1), gimple_assign_lhs (op2), + mode); +} + + +/* Create and return a type cast assignment. This creates a NOP_EXPR + that converts OP to TO_TYPE. */ + +gimple +build_type_cast (tree to_type, tree op, enum ssa_mode mode) +{ + tree lhs = create_gimple_tmp (to_type, mode); + return gimple_build_assign_with_ops (NOP_EXPR, lhs, op, NULL_TREE); +} + +gimple +build_type_cast (tree to_type, gimple op, enum ssa_mode mode) +{ + return build_type_cast (to_type, gimple_assign_lhs (op), mode); +} + #include "gt-gimple.h" diff --git a/gcc/gimple.h b/gcc/gimple.h index 475d2ea..3a65e3c 100644 --- a/gcc/gimple.h +++ b/gcc/gimple.h @@ -33,6 +33,15 @@ along with GCC; see the file COPYING3. If not see typedef gimple gimple_seq_node; +/* Types of supported temporaries. GIMPLE temporaries may be symbols + in normal form (i.e., regular decls) or SSA names. This enum is + used by create_gimple_tmp to tell it what kind of temporary the + caller wants. */ +enum ssa_mode { + M_SSA = 0, + M_NORMAL +}; + /* For each block, the PHI nodes that need to be rewritten are stored into these vectors. */ typedef vec gimple_vec; @@ -720,6 +729,17 @@ union GTY ((desc ("gimple_statement_structure (&%h)"), /* In gimple.c. */ +/* Helper functions to build GIMPLE statements. */ +tree create_gimple_tmp (tree, enum ssa_mode = M_SSA); +gimple build_assign (enum tree_code, tree, int, enum ssa_mode = M_SSA); +gimple build_assign (enum tree_code, gimple, int, enum ssa_mode = M_SSA); +gimple build_assign (enum tree_code, tree, tree, enum ssa_mode = M_SSA); +gimple build_assign (enum tree_code, gimple, tree, enum ssa_mode = M_SSA); +gimple build_assign (enum tree_code, tree, gimple, enum ssa_mode = M_SSA); +gimple build_assign (enum tree_code, gimple, gimple, enum ssa_mode = M_SSA); +gimple build_type_cast (tree, tree, enum ssa_mode = M_SSA); +gimple build_type_cast (tree, gimple, enum ssa_mode = M_SSA); + /* Offset in bytes to the location of the operand vector. Zero if there is no operand vector for this tuple structure. */ extern size_t const gimple_ops_offset_[]; @@ -1096,7 +1116,6 @@ gimple_seq_empty_p (gimple_seq s) return s == NULL; } - void gimple_seq_add_stmt (gimple_seq *, gimple); /* Link gimple statement GS to the end of the sequence *SEQ_P. If @@ -5326,4 +5345,15 @@ extern tree maybe_fold_or_comparisons (enum tree_code, tree, tree, enum tree_code, tree, tree); bool gimple_val_nonnegative_real_p (tree); + + +/* Set the location of all statements in SEQ to LOC. */ + +static inline void +gimple_seq_set_location (gimple_seq seq, location_t loc) +{ + for (gimple_stmt_iterator i = gsi_start (seq); !gsi_end_p (i); gsi_next (&i)) + gimple_set_location (gsi_stmt (i), loc); +} + #endif /* GCC_GIMPLE_H */ diff --git a/gcc/asan.c b/gcc/asan.c index 36eccf9..b8acaf7 100644 --- a/gcc/asan.c +++ b/gcc/asan.c @@ -1380,57 +1380,23 @@ build_check_stmt (location_t location, tree base, gimple_stmt_iterator *iter, /* Slow path for 1, 2 and 4 byte accesses. Test (shadow != 0) & ((base_addr & 7) + (size_in_bytes - 1)) >= shadow). */ - g = gimple_build_assign_with_ops (NE_EXPR, - make_ssa_name (boolean_type_node, - NULL), - shadow, - build_int_cst (shadow_type, 0)); - gimple_set_location (g, location); - gsi_insert_after (&gsi, g, GSI_NEW_STMT); - t = gimple_assign_lhs (g); - - g = gimple_build_assign_with_ops (BIT_AND_EXPR, - make_ssa_name (uintptr_type, - NULL), - base_addr, - build_int_cst (uintptr_type, 7)); - gimple_set_location (g, location); - gsi_insert_after (&gsi, g, GSI_NEW_STMT); - - g = gimple_build_assign_with_ops (NOP_EXPR, - make_ssa_name (shadow_type, - NULL), - gimple_assign_lhs (g), NULL_TREE); - gimple_set_location (g, location); - gsi_insert_after (&gsi, g, GSI_NEW_STMT); - + gimple_seq seq = NULL; + gimple shadow_test = build_assign (NE_EXPR, shadow, 0); + gimple_seq_add_stmt (&seq, shadow_test); + gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, base_addr, 7)); + gimple_seq_add_stmt (&seq, build_type_cast (shadow_type, + gimple_seq_last (seq))); if (size_in_bytes > 1) - { - g = gimple_build_assign_with_ops (PLUS_EXPR, - make_ssa_name (shadow_type, - NULL), - gimple_assign_lhs (g), - build_int_cst (shadow_type, - size_in_bytes - 1)); - gimple_set_location (g, location); - gsi_insert_after (&gsi, g, GSI_NEW_STMT); - } - - g = gimple_build_assign_with_ops (GE_EXPR, - make_ssa_name (boolean_type_node, - NULL), - gimple_assign_lhs (g), - shadow); - gimple_set_location (g, location); - gsi_insert_after (&gsi, g, GSI_NEW_STMT); - - g = gimple_build_assign_with_ops (BIT_AND_EXPR, - make_ssa_name (boolean_type_node, - NULL), - t, gimple_assign_lhs (g)); - gimple_set_location (g, location); - gsi_insert_after (&gsi, g, GSI_NEW_STMT); - t = gimple_assign_lhs (g); + gimple_seq_add_stmt (&seq, + build_assign (PLUS_EXPR, gimple_seq_last (seq), + size_in_bytes - 1)); + gimple_seq_add_stmt (&seq, build_assign (GE_EXPR, gimple_seq_last (seq), + shadow)); + gimple_seq_add_stmt (&seq, build_assign (BIT_AND_EXPR, shadow_test, + gimple_seq_last (seq))); + t = gimple_assign_lhs (gimple_seq_last (seq)); + gimple_seq_set_location (seq, location);