public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: dodji@redhat.com
To: gcc-patches@gcc.gnu.org
Cc: dnovillo@google.com, jakub@redhat.com, wmi@google.com,
	davidxl@google.com,        konstantin.s.serebryany@gmail.com
Subject: [PATCH 11/13] Factorize condition insertion code out of build_check_stmt
Date: Thu, 01 Nov 2012 19:53:00 -0000	[thread overview]
Message-ID: <1351799566-31447-12-git-send-email-dodji@redhat.com> (raw)
In-Reply-To: <1351799566-31447-1-git-send-email-dodji@redhat.com>

From: dodji <dodji@138bc75d-0d04-0410-961f-82ee72b054a4>

This patch splits a new create_cond_insert_point_before_iter function
out of build_check_stmt, to be used by a later patch.

Tested by running cc1 -fasan on the test program below with and
without the patch and by inspecting the gimple output to see that
there is no change.

void
foo ()
{
  char foo[1] = {0};

  foo[0] = 1;
}

gcc/

	* asan.c (create_cond_insert_point_before_iter): Factorize out of ...
	(build_check_stmt): ... here.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/asan@192844 138bc75d-0d04-0410-961f-82ee72b054a4
---
 gcc/ChangeLog.asan |   3 ++
 gcc/asan.c         | 120 +++++++++++++++++++++++++++++++++--------------------
 2 files changed, 79 insertions(+), 44 deletions(-)

diff --git a/gcc/ChangeLog.asan b/gcc/ChangeLog.asan
index 9159b3f..0e0b9b8 100644
--- a/gcc/ChangeLog.asan
+++ b/gcc/ChangeLog.asan
@@ -1,5 +1,8 @@
 2012-10-26  Dodji Seketeli  <dodji@redhat.com>
 
+	* asan.c (create_cond_insert_point_before_iter): Factorize out of ...
+	(build_check_stmt): ... here.
+
 	* asan.c (build_check_stmt): Accept the memory access to be
 	represented by an SSA_NAME.
 
diff --git a/gcc/asan.c b/gcc/asan.c
index b43f03b..736286e 100644
--- a/gcc/asan.c
+++ b/gcc/asan.c
@@ -397,6 +397,75 @@ asan_init_func (void)
 #define PROB_VERY_UNLIKELY	(REG_BR_PROB_BASE / 2000 - 1)
 #define PROB_ALWAYS		(REG_BR_PROB_BASE)
 
+/* Split the current basic block and create a condition statement
+   insertion point right before the statement pointed to by ITER.
+   Return an iterator to the point at which the caller might safely
+   insert the condition statement.
+
+   THEN_BLOCK must be set to the address of an uninitialized instance
+   of basic_block.  The function will then set *THEN_BLOCK to the
+   'then block' of the condition statement to be inserted by the
+   caller.
+
+   Similarly, the function will set *FALLTRHOUGH_BLOCK to the 'else
+   block' of the condition statement to be inserted by the caller.
+
+   Note that *FALLTHROUGH_BLOCK is a new block that contains the
+   statements starting from *ITER, and *THEN_BLOCK is a new empty
+   block.
+
+   *ITER is adjusted to still point to the same statement it was
+   *pointing to initially.  */
+
+static gimple_stmt_iterator
+create_cond_insert_point_before_iter (gimple_stmt_iterator *iter,
+				      bool then_more_likely_p,
+				      basic_block *then_block,
+				      basic_block *fallthrough_block)
+{
+  gimple_stmt_iterator gsi = *iter;
+
+  if (!gsi_end_p (gsi))
+    gsi_prev (&gsi);
+
+  basic_block cur_bb = gsi_bb (*iter);
+
+  edge e = split_block (cur_bb, gsi_stmt (gsi));
+
+  /* Get a hold on the 'condition block', the 'then block' and the
+     'else block'.  */
+  basic_block cond_bb = e->src;
+  basic_block fallthru_bb = e->dest;
+  basic_block then_bb = create_empty_bb (cond_bb);
+
+  /* Set up the newly created 'then block'.  */
+  e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
+  int fallthrough_probability =
+    then_more_likely_p
+    ? PROB_VERY_UNLIKELY
+    : PROB_ALWAYS - PROB_VERY_UNLIKELY;
+  e->probability = PROB_ALWAYS - fallthrough_probability;
+  make_single_succ_edge (then_bb, fallthru_bb, EDGE_FALLTHRU);
+
+  /* Set up the fallthrough basic block.  */
+  e = find_edge (cond_bb, fallthru_bb);
+  e->flags = EDGE_FALSE_VALUE;
+  e->count = cond_bb->count;
+  e->probability = fallthrough_probability;
+
+  /* Update dominance info for the newly created then_bb; note that
+     fallthru_bb's dominance info has already been updated by
+     split_bock.  */
+  if (dom_info_available_p (CDI_DOMINATORS))
+    set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
+
+  *then_block = then_bb;
+  *fallthrough_block = fallthru_bb;
+  *iter = gsi_start_bb (fallthru_bb);
+
+  return gsi_last_bb (cond_bb);
+}
+
 /* Instrument the memory access instruction BASE.  Insert new
    statements before ITER.
 
@@ -411,8 +480,7 @@ build_check_stmt (tree base, gimple_stmt_iterator *iter,
 		  int size_in_bytes)
 {
   gimple_stmt_iterator gsi;
-  basic_block cond_bb, then_bb, else_bb;
-  edge e;
+  basic_block then_bb, else_bb;
   tree t, base_addr, shadow;
   gimple g;
   tree shadow_ptr_type = shadow_ptr_types[size_in_bytes == 16 ? 1 : 0];
@@ -421,51 +489,15 @@ build_check_stmt (tree base, gimple_stmt_iterator *iter,
     = build_nonstandard_integer_type (TYPE_PRECISION (TREE_TYPE (base)), 1);
   tree base_ssa = base;
 
-  /* We first need to split the current basic block, and start altering
-     the CFG.  This allows us to insert the statements we're about to
-     construct into the right basic blocks.  */
-
-  cond_bb = gimple_bb (gsi_stmt (*iter));
-  gsi = *iter;
-  gsi_prev (&gsi);
-  if (!gsi_end_p (gsi))
-    e = split_block (cond_bb, gsi_stmt (gsi));
-  else
-    e = split_block_after_labels (cond_bb);
-  cond_bb = e->src;
-  else_bb = e->dest;
-
-  /* A recap at this point: else_bb is the basic block at whose head
-     is the gimple statement for which this check expression is being
-     built.  cond_bb is the (possibly new, synthetic) basic block the
-     end of which will contain the cache-lookup code, and a
-     conditional that jumps to the cache-miss code or, much more
-     likely, over to else_bb.  */
-
-  /* Create the bb that contains the crash block.  */
-  then_bb = create_empty_bb (cond_bb);
-  e = make_edge (cond_bb, then_bb, EDGE_TRUE_VALUE);
-  e->probability = PROB_VERY_UNLIKELY;
-  make_single_succ_edge (then_bb, else_bb, EDGE_FALLTHRU);
-
-  /* Mark the pseudo-fallthrough edge from cond_bb to else_bb.  */
-  e = find_edge (cond_bb, else_bb);
-  e->flags = EDGE_FALSE_VALUE;
-  e->count = cond_bb->count;
-  e->probability = PROB_ALWAYS - PROB_VERY_UNLIKELY;
-
-  /* Update dominance info.  Note that bb_join's data was
-     updated by split_block.  */
-  if (dom_info_available_p (CDI_DOMINATORS))
-    {
-      set_immediate_dominator (CDI_DOMINATORS, then_bb, cond_bb);
-      set_immediate_dominator (CDI_DOMINATORS, else_bb, cond_bb);
-    }
+  /* Get an iterator on the point where we can add the condition
+     statement for the instrumentation.  */
+  gsi = create_cond_insert_point_before_iter (iter,
+					      /*then_more_likely_p=*/false,
+					      &then_bb,
+					      &else_bb);
 
   base = unshare_expr (base);
 
-  gsi = gsi_last_bb (cond_bb);
-
   /* BASE can already be an SSA_NAME; in that case, do not create a
      new SSA_NAME for it.  */
   if (TREE_CODE (base) != SSA_NAME)
-- 
1.7.11.7

  parent reply	other threads:[~2012-11-01 19:53 UTC|newest]

Thread overview: 80+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-01 19:53 [PATCH 00/13] Request to merge Address Sanitizer in dodji
2012-11-01 19:53 ` [PATCH 08/13] Fix a couple of ICEs dodji
2012-11-01 19:53 ` [PATCH 06/13] Implement protection of stack variables dodji
     [not found]   ` <CAGQ9bdweH8Pn=8vLTNa8FSzAh92OYrWScxK78n9znCodADJUvw@mail.gmail.com>
2012-11-02  4:35     ` Xinliang David Li
2012-11-02 15:25       ` Dodji Seketeli
2012-11-02 14:44     ` Dodji Seketeli
     [not found]       ` <CAGQ9bdxQG3i=BrSYmaN-ssdv4omW6F5VTg50viskKNcYrF-8BQ@mail.gmail.com>
2012-11-02 16:02         ` Dodji Seketeli
2012-11-01 19:53 ` [PATCH 05/13] Allow asan at -O0 dodji
2012-11-01 19:53 ` [PATCH 03/13] Initial asan cleanups dodji
2012-11-01 19:53 ` [PATCH 10/13] Make build_check_stmt accept an SSA_NAME for its base dodji
2012-11-01 19:53 ` [PATCH 09/13] Don't forget to protect 32 bytes aligned global variables dodji
2012-11-01 19:53 ` [PATCH 12/13] Instrument built-in memory access function calls dodji
2012-11-01 19:53 ` dodji [this message]
2012-11-01 19:53 ` [PATCH 01/13] Initial import of asan from the Google branch dodji
2012-11-01 19:53 ` [PATCH 02/13] Rename tree-asan.[ch] to asan.[ch] dodji
2012-11-01 21:54   ` Joseph S. Myers
2012-11-02 22:44     ` Dodji Seketeli
2012-11-01 19:53 ` [PATCH 07/13] Implement protection of global variables dodji
2012-11-01 19:54 ` [PATCH 04/13] Emit GIMPLE directly instead of gimplifying GENERIC dodji
2012-11-02 22:53 ` [PATCH 00/13] Request to merge Address Sanitizer in Dodji Seketeli
2012-11-02 22:56   ` [PATCH 01/10] Initial import of asan from the Google branch into trunk Dodji Seketeli
2012-11-06 17:04     ` Diego Novillo
2012-11-09 13:14     ` Tobias Burnus
2012-11-09 13:58       ` Jakub Jelinek
2012-11-09 16:53         ` Xinliang David Li
2012-11-09 17:13         ` Tobias Burnus
2012-11-09 17:18       ` Wei Mi
2012-11-12 11:09       ` [PATCH 03/11] Emit GIMPLE directly instead of gimplifying GENERIC Dodji Seketeli
2012-11-12 11:20       ` [PATCH 01/10] Initial import of asan from the Google branch into trunk Dodji Seketeli
2012-11-02 22:57   ` [PATCH 02/10] Initial asan cleanups Dodji Seketeli
2012-11-06 17:04     ` Diego Novillo
2012-11-12 11:12       ` Dodji Seketeli
2012-11-02 22:58   ` [PATCH 03/10] Emit GIMPLE directly instead of gimplifying GENERIC Dodji Seketeli
2012-11-06 17:08     ` Diego Novillo
2012-11-02 22:59   ` [PATCH 04/10] Allow asan at -O0 Dodji Seketeli
2012-11-06 17:12     ` Diego Novillo
2012-11-02 23:00   ` [PATCH 05/10] Implement protection of stack variables Dodji Seketeli
2012-11-06 17:22     ` Diego Novillo
2012-11-12 11:31       ` Dodji Seketeli
2012-11-12 11:51         ` Jakub Jelinek
2012-11-12 16:08           ` Dodji Seketeli
2012-11-02 23:01   ` [PATCH 06/10] Implement protection of global variables Dodji Seketeli
2012-11-06 17:27     ` Diego Novillo
2012-11-12 11:32       ` Dodji Seketeli
2012-11-02 23:02   ` [PATCH 07/10] Make build_check_stmt accept an SSA_NAME for its base Dodji Seketeli
2012-11-06 17:28     ` Diego Novillo
2012-11-02 23:03   ` [PATCH 08/10] Factorize condition insertion code out of build_check_stmt Dodji Seketeli
2012-11-05 15:50     ` Jakub Jelinek
2012-11-05 20:25       ` Dodji Seketeli
2012-11-06 17:30     ` Diego Novillo
2012-11-02 23:05   ` [PATCH 09/10] Instrument built-in memory access function calls Dodji Seketeli
2012-11-06 17:37     ` Diego Novillo
2012-11-12 11:40       ` Dodji Seketeli
2012-11-03  8:22   ` [PATCH 10/10] Import the asan runtime library into GCC tree Dodji Seketeli
     [not found]   ` <87fw4r7g8w.fsf_-_@redhat.com>
2012-11-06 17:41     ` Diego Novillo
2012-11-12 11:47       ` Dodji Seketeli
2012-11-12 18:59         ` H.J. Lu
2012-11-14 11:11           ` H.J. Lu
2012-11-14 11:42             ` H.J. Lu
2012-11-12 16:07   ` [PATCH 00/13] Request to merge Address Sanitizer in Dodji Seketeli
2012-11-12 16:21     ` Jakub Jelinek
2012-11-12 16:45       ` Tobias Burnus
2012-11-12 16:51         ` Konstantin Serebryany
2012-11-12 17:20     ` Jack Howarth
2012-11-12 17:34       ` Jack Howarth
2012-11-12 17:37         ` Tobias Burnus
2012-11-12 17:57           ` Jack Howarth
2012-11-12 17:55         ` Dodji Seketeli
2012-11-12 18:40           ` Jack Howarth
2012-11-12 20:39 ` H.J. Lu
2012-11-12 22:15   ` Ian Lance Taylor
2012-11-15 19:42 ` Jack Howarth
2012-11-15 23:42   ` Konstantin Serebryany
2012-11-16  8:27     ` Dodji Seketeli
2012-11-16 14:03       ` Jack Howarth
2012-11-16 15:57       ` Jack Howarth
2012-11-16 16:02         ` Jakub Jelinek
2012-11-16 16:47           ` Jack Howarth
2012-11-16 16:56       ` Alexander Potapenko
2012-11-16 17:06         ` Jack Howarth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1351799566-31447-12-git-send-email-dodji@redhat.com \
    --to=dodji@redhat.com \
    --cc=davidxl@google.com \
    --cc=dnovillo@google.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=konstantin.s.serebryany@gmail.com \
    --cc=wmi@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).