public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Bill Schmidt <wschmidt@linux.ibm.com>
To: gcc-patches@gcc.gnu.org
Cc: segher@kernel.crashing.org
Subject: [PATCH 09/55] rs6000: Red-black tree implementation for balanced tree search
Date: Thu, 17 Jun 2021 10:18:53 -0500	[thread overview]
Message-ID: <84213d566e6ce0f25de703c6aed8e9fd2a8207d7.1623941441.git.wschmidt@linux.ibm.com> (raw)
In-Reply-To: <cover.1623941441.git.wschmidt@linux.ibm.com>
In-Reply-To: <cover.1623941441.git.wschmidt@linux.ibm.com>

2021-06-08  Bill Schmidt  <wschmidt@linux.ibm.com>

gcc/
	* config/rs6000/rbtree.c: New file.
	* config/rs6000/rbtree.h: New file.
---
 gcc/config/rs6000/rbtree.c | 242 +++++++++++++++++++++++++++++++++++++
 gcc/config/rs6000/rbtree.h |  52 ++++++++
 2 files changed, 294 insertions(+)
 create mode 100644 gcc/config/rs6000/rbtree.c
 create mode 100644 gcc/config/rs6000/rbtree.h

diff --git a/gcc/config/rs6000/rbtree.c b/gcc/config/rs6000/rbtree.c
new file mode 100644
index 00000000000..37a559c1fbc
--- /dev/null
+++ b/gcc/config/rs6000/rbtree.c
@@ -0,0 +1,242 @@
+/* Partial red-black tree implementation for rs6000-gen-builtins.c.
+   Copyright (C) 2020-21 Free Software Foundation, Inc.
+   Contributed by Bill Schmidt, IBM <wschmidt@linux.ibm.com>
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include "rbtree.h"
+
+/* Initialize a red-black tree.  */
+void
+rbt_new (struct rbt_strings *t)
+{
+  t->rbt_nil = (rbt_string_node *) malloc (sizeof (rbt_string_node));
+  t->rbt_nil->color = RBT_BLACK;
+  t->rbt_root = t->rbt_nil;
+}
+
+/* Create a new node to be inserted into the red-black tree.  An inserted
+   node starts out red.  */
+static struct rbt_string_node *
+rbt_create_node (struct rbt_strings *t, char *str)
+{
+  struct rbt_string_node *nodeptr
+    = (struct rbt_string_node *) malloc (sizeof (rbt_string_node));
+  nodeptr->str = str;
+  nodeptr->left = t->rbt_nil;
+  nodeptr->right = t->rbt_nil;
+  nodeptr->par = NULL;
+  nodeptr->color = RBT_RED;
+  return nodeptr;
+}
+
+/* Perform a left-rotate operation on NODE in the red-black tree.  */
+static void
+rbt_left_rotate (struct rbt_strings *t, struct rbt_string_node *node)
+{
+  struct rbt_string_node *right = node->right;
+  assert (right);
+
+  /* Turn RIGHT's left subtree into NODE's right subtree.  */
+  node->right = right->left;
+  if (right->left != t->rbt_nil)
+    right->left->par = node;
+
+  /* Link NODE's parent to RIGHT.  */
+  right->par = node->par;
+
+  if (node->par == t->rbt_nil)
+    t->rbt_root = right;
+  else if (node == node->par->left)
+    node->par->left = right;
+  else
+    node->par->right = right;
+
+  /* Put NODE on RIGHT's left.  */
+  right->left = node;
+  node->par = right;
+}
+
+/* Perform a right-rotate operation on NODE in the red-black tree.  */
+static void
+rbt_right_rotate (struct rbt_strings *t, struct rbt_string_node *node)
+{
+  struct rbt_string_node *left = node->left;
+  assert (left);
+
+  /* Turn LEFT's right subtree into NODE's left subtree.  */
+  node->left = left->right;
+  if (left->right != t->rbt_nil)
+    left->right->par = node;
+
+  /* Link NODE's parent to LEFT.  */
+  left->par = node->par;
+
+  if (node->par == t->rbt_nil)
+    t->rbt_root = left;
+  else if (node == node->par->right)
+    node->par->right = left;
+  else
+    node->par->left = left;
+
+  /* Put NODE on LEFT's right.  */
+  left->right = node;
+  node->par = left;
+}
+
+/* Insert STR into the tree, returning 1 for success and 0 if STR already
+   appears in the tree.  */
+int
+rbt_insert (struct rbt_strings *t, char *str)
+{
+  struct rbt_string_node *curr = t->rbt_root;
+  struct rbt_string_node *trail = t->rbt_nil;
+
+  while (curr != t->rbt_nil)
+    {
+      trail = curr;
+      int cmp = strcmp (str, curr->str);
+      if (cmp < 0)
+	curr = curr->left;
+      else if (cmp > 0)
+	curr = curr->right;
+      else
+	return 0;
+    }
+
+  struct rbt_string_node *fresh = rbt_create_node (t, str);
+  fresh->par = trail;
+
+  if (trail == t->rbt_nil)
+    t->rbt_root = fresh;
+  else if (strcmp (fresh->str, trail->str) < 0)
+    trail->left = fresh;
+  else
+    trail->right = fresh;
+
+  fresh->left = t->rbt_nil;
+  fresh->right = t->rbt_nil;
+
+  /* FRESH has now been inserted as a red leaf.  If we have invalidated
+     one of the following preconditions, we must fix things up:
+      (a) If a node is red, both of its children are black.
+      (b) The root must be black.
+     Note that only (a) or (b) applies at any given time during the
+     process.  This algorithm works up the tree from NEW looking
+     for a red child with a red parent, and cleaning that up.  If the
+     root ends up red, it gets turned black at the end.  */
+  curr = fresh;
+  while (curr->par->color == RBT_RED)
+    if (curr->par == curr->par->par->left)
+      {
+	struct rbt_string_node *uncle = curr->par->par->right;
+	if (uncle->color == RBT_RED)
+	  {
+	    curr->par->color = RBT_BLACK;
+	    uncle->color = RBT_BLACK;
+	    curr->par->par->color = RBT_RED;
+	    curr = curr->par->par;
+	  }
+	else if (curr == curr->par->right)
+	  {
+	    curr = curr->par;
+	    rbt_left_rotate (t, curr);
+	  }
+	else
+	  {
+	    curr->par->color = RBT_BLACK;
+	    curr->par->par->color = RBT_RED;
+	    rbt_right_rotate (t, curr->par->par);
+	  }
+      }
+    else /* curr->par == curr->par->par->right  */
+      {
+	/* Gender-neutral formations are awkward, so let's be fair. ;-)
+	   ("Parent-sibling" is just awful.)  */
+	struct rbt_string_node *aunt = curr->par->par->left;
+	if (aunt->color == RBT_RED)
+	  {
+	    curr->par->color = RBT_BLACK;
+	    aunt->color = RBT_BLACK;
+	    curr->par->par->color = RBT_RED;
+	    curr = curr->par->par;
+	  }
+	else if (curr == curr->par->left)
+	  {
+	    curr = curr->par;
+	    rbt_right_rotate (t, curr);
+	  }
+	else
+	  {
+	    curr->par->color = RBT_BLACK;
+	    curr->par->par->color = RBT_RED;
+	    rbt_left_rotate (t, curr->par->par);
+	  }
+      }
+
+  t->rbt_root->color = RBT_BLACK;
+  return 1;
+}
+
+/* Return 1 if STR is in the red-black tree, else 0.  */
+int
+rbt_find (struct rbt_strings *t, char *str)
+{
+  struct rbt_string_node *curr = t->rbt_root;
+
+  while (curr != t->rbt_nil)
+    {
+      int cmp = strcmp (str, curr->str);
+      if (cmp < 0)
+	curr = curr->left;
+      else if (cmp > 0)
+	curr = curr->right;
+      else
+	return 1;
+    }
+
+  return 0;
+}
+
+/* Inorder dump of the binary search tree.  */
+void
+rbt_dump (struct rbt_strings *t, struct rbt_string_node *subtree)
+{
+  if (subtree != t->rbt_nil)
+    {
+      rbt_dump (t, subtree->left);
+      fprintf (stderr, "%s\n", subtree->str);
+      rbt_dump (t, subtree->right);
+    }
+}
+
+/* Inorder call-back for iteration over the tree.  */
+void
+rbt_inorder_callback (struct rbt_strings *t, struct rbt_string_node *subtree,
+		      void (*fn) (char *))
+{
+  if (subtree != t->rbt_nil)
+    {
+      rbt_inorder_callback (t, subtree->left, fn);
+      (*fn) (subtree->str);
+      rbt_inorder_callback (t, subtree->right, fn);
+    }
+}
diff --git a/gcc/config/rs6000/rbtree.h b/gcc/config/rs6000/rbtree.h
new file mode 100644
index 00000000000..fab0001ffde
--- /dev/null
+++ b/gcc/config/rs6000/rbtree.h
@@ -0,0 +1,52 @@
+/* Partial red-black tree implementation for rs6000-gen-builtins.c.
+   Copyright (C) 2020-21 Free Software Foundation, Inc.
+   Contributed by Bill Schmidt, IBM <wschmidt@linux.ibm.com>
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+/* Red-black binary search tree on strings.  Presently we don't support
+   deletes; only insert/find operations are implemented.  */
+enum rbt_color
+  {
+    RBT_BLACK,
+    RBT_RED
+  };
+
+struct rbt_string_node {
+  char *str;
+  struct rbt_string_node *left;
+  struct rbt_string_node *right;
+  struct rbt_string_node *par;
+  enum rbt_color color;
+};
+
+/* Root and sentinel nodes of a red-black tree.
+   rbt_nil points to a sentinel node, which is the parent of root
+   and the child of every node without a "real" left or right child.
+   rbt_root points to the root of the tree, if it exists yet.  The
+   root and sentinel nodes are always black.  */
+struct rbt_strings {
+  struct rbt_string_node *rbt_nil;
+  struct rbt_string_node *rbt_root;
+};
+
+void rbt_new (struct rbt_strings *);
+int rbt_insert (struct rbt_strings *, char *);
+int rbt_find (struct rbt_strings *, char *);
+void rbt_dump (struct rbt_strings *, struct rbt_string_node *);
+void rbt_inorder_callback (struct rbt_strings *, struct rbt_string_node *,
+			   void (*) (char *));
-- 
2.27.0


  parent reply	other threads:[~2021-06-17 15:20 UTC|newest]

Thread overview: 110+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-17 15:18 [PATCHv3 00/55] Replace the Power target-specific builtin machinery Bill Schmidt
2021-06-17 15:18 ` [PATCH 01/55] Support scanning of build-time GC roots in gengtype Bill Schmidt
2021-06-17 15:18 ` [PATCH 02/55] rs6000: Initial create of rs6000-gen-builtins.c Bill Schmidt
2021-06-17 15:18 ` [PATCH 03/55] rs6000: Add initial input files Bill Schmidt
2021-06-17 15:18 ` [PATCH 04/55] rs6000: Add file support and functions for diagnostic support Bill Schmidt
2021-06-17 15:18 ` [PATCH 05/55] rs6000: Add helper functions for parsing Bill Schmidt
2021-07-09 19:32   ` will schmidt
2021-07-14 22:58     ` Segher Boessenkool
2021-07-14 23:32   ` Segher Boessenkool
2021-06-17 15:18 ` [PATCH 06/55] rs6000: Add functions for matching types, part 1 of 3 Bill Schmidt
2021-06-17 15:18 ` [PATCH 07/55] rs6000: Add functions for matching types, part 2 " Bill Schmidt
2021-06-17 15:18 ` [PATCH 08/55] rs6000: Add functions for matching types, part 3 " Bill Schmidt
2021-06-17 15:18 ` Bill Schmidt [this message]
2021-06-17 15:18 ` [PATCH 10/55] rs6000: Main function with stubs for parsing and output Bill Schmidt
2021-07-19 19:15   ` Segher Boessenkool
2021-07-20 22:19     ` Bill Schmidt
2021-07-20 23:22       ` Segher Boessenkool
2021-07-21  1:51         ` Bill Schmidt
2021-07-21 15:43           ` Segher Boessenkool
2021-07-21 16:08             ` Bill Schmidt
2021-07-21 16:16               ` Bill Schmidt
2021-06-17 15:18 ` [PATCH 11/55] rs6000: Parsing built-in input file, part 1 of 3 Bill Schmidt
2021-07-19 20:39   ` Segher Boessenkool
2021-06-17 15:18 ` [PATCH 12/55] rs6000: Parsing built-in input file, part 2 " Bill Schmidt
2021-07-19 22:07   ` Segher Boessenkool
2021-06-17 15:18 ` [PATCH 13/55] rs6000: Parsing built-in input file, part 3 " Bill Schmidt
2021-07-19 22:13   ` Segher Boessenkool
2021-06-17 15:18 ` [PATCH 14/55] rs6000: Parsing of overload input file Bill Schmidt
2021-07-19 23:09   ` Segher Boessenkool
2021-06-17 15:18 ` [PATCH 15/55] rs6000: Build and store function type identifiers Bill Schmidt
2021-07-20  0:04   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 16/55] rs6000: Write output to the builtin definition include file Bill Schmidt
2021-07-20 23:27   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 17/55] rs6000: Write output to the builtins header file Bill Schmidt
2021-07-20 23:40   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 18/55] rs6000: Write output to the builtins init file, part 1 of 3 Bill Schmidt
2021-07-20 23:51   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 19/55] rs6000: Write output to the builtins init file, part 2 " Bill Schmidt
2021-07-20 23:53   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 20/55] rs6000: Write output to the builtins init file, part 3 " Bill Schmidt
2021-07-21 17:08   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 21/55] rs6000: Write static initializations for built-in table Bill Schmidt
2021-07-21 17:14   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 22/55] rs6000: Write static initializations for overload tables Bill Schmidt
2021-07-21 17:40   ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 23/55] rs6000: Incorporate new builtins code into the build machinery Bill Schmidt
2021-07-21 18:58   ` Segher Boessenkool
2021-07-27  3:26     ` Bill Schmidt
2021-07-27 14:23       ` Segher Boessenkool
2021-07-27 17:38         ` Bill Schmidt
2021-06-17 15:19 ` [PATCH 24/55] rs6000: Add gengtype handling to " Bill Schmidt
2021-06-17 15:19 ` [PATCH 25/55] rs6000: Add the rest of the [altivec] stanza to the builtins file Bill Schmidt
2021-06-17 15:19 ` [PATCH 26/55] rs6000: Add VSX builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 27/55] rs6000: Add available-everywhere and ancient builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 28/55] rs6000: Add power7 and power7-64 builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 29/55] rs6000: Add power8-vector builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 30/55] rs6000: Add Power9 builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 31/55] rs6000: Add more type nodes to support builtin processing Bill Schmidt
2021-06-17 15:19 ` [PATCH 32/55] rs6000: Add Power10 builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 33/55] rs6000: Add MMA builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 34/55] rs6000: Add miscellaneous builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 35/55] rs6000: Add Cell builtins Bill Schmidt
2021-06-17 15:19 ` [PATCH 36/55] rs6000: Add remaining overloads Bill Schmidt
2021-06-17 15:19 ` [PATCH 37/55] rs6000: Execute the automatic built-in initialization code Bill Schmidt
2021-06-17 15:19 ` [PATCH 38/55] rs6000: Darwin builtin support Bill Schmidt
2021-06-17 15:19 ` [PATCH 39/55] rs6000: Add sanity to V2DI_type_node definitions Bill Schmidt
2021-06-17 15:19 ` [PATCH 40/55] rs6000: Always initialize vector_pair and vector_quad nodes Bill Schmidt
2021-06-17 15:19 ` [PATCH 41/55] rs6000: Handle overloads during program parsing Bill Schmidt
2021-06-17 15:19 ` [PATCH 42/55] rs6000: Handle gimple folding of target built-ins Bill Schmidt
2021-07-28 21:21   ` will schmidt
2021-07-29 12:42     ` Bill Schmidt
2021-08-02 13:31       ` Bill Schmidt
2021-08-02 23:43         ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 43/55] rs6000: Support for vectorizing built-in functions Bill Schmidt
2021-06-17 15:19 ` [PATCH 44/55] rs6000: Builtin expansion, part 1 Bill Schmidt
2021-07-27 21:06   ` will schmidt
2021-07-28  3:30     ` Bill Schmidt
2021-06-17 15:19 ` [PATCH 45/55] rs6000: Builtin expansion, part 2 Bill Schmidt
2021-07-27 21:06   ` will schmidt
2021-06-17 15:19 ` [PATCH 46/55] rs6000: Builtin expansion, part 3 Bill Schmidt
2021-07-27 21:06   ` will schmidt
2021-08-03 23:40     ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 47/55] rs6000: Builtin expansion, part 4 Bill Schmidt
2021-07-27 21:06   ` will schmidt
2021-08-03 23:46     ` Segher Boessenkool
2021-08-04  0:34     ` Segher Boessenkool
2021-08-12 16:17       ` Bill Schmidt
2021-06-17 15:19 ` [PATCH 48/55] rs6000: Builtin expansion, part 5 Bill Schmidt
2021-07-27 21:07   ` will schmidt
2021-06-17 15:19 ` [PATCH 49/55] rs6000: Builtin expansion, part 6 Bill Schmidt
2021-07-27 21:07   ` will schmidt
2021-07-28 20:38     ` Bill Schmidt
2021-06-17 15:19 ` [PATCH 50/55] rs6000: Update rs6000_builtin_decl Bill Schmidt
2021-07-27 21:08   ` will schmidt
2021-08-04  0:38     ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 51/55] rs6000: Miscellaneous uses of rs6000_builtin_decls_x Bill Schmidt
2021-07-27 21:08   ` will schmidt
2021-06-17 15:19 ` [PATCH 52/55] rs6000: Debug support Bill Schmidt
2021-07-27 21:07   ` will schmidt
2021-08-04  0:49     ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 53/55] rs6000: Update altivec.h for automated interfaces Bill Schmidt
2021-07-27 21:07   ` will schmidt
2021-07-28 20:58     ` Bill Schmidt
2021-08-04  0:58       ` Segher Boessenkool
2021-06-17 15:19 ` [PATCH 54/55] rs6000: Test case adjustments Bill Schmidt
2021-06-17 15:19 ` [PATCH 55/55] rs6000: Enable the new builtin support Bill Schmidt
2021-07-27 21:07   ` will schmidt
2021-06-25 15:25 ` [PATCHv3 00/55] Replace the Power target-specific builtin machinery Bill Schmidt
2021-07-13 13:52   ` Bill Schmidt
  -- strict thread matches above, loose matches on Subject: below --
2021-06-08 18:26 [PATCHv2 " Bill Schmidt
2021-06-08 18:26 ` [PATCH 09/55] rs6000: Red-black tree implementation for balanced tree search Bill Schmidt

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=84213d566e6ce0f25de703c6aed8e9fd2a8207d7.1623941441.git.wschmidt@linux.ibm.com \
    --to=wschmidt@linux.ibm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=segher@kernel.crashing.org \
    /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).