diff mbox

GNU Superopt patches 3/6

Message ID 201009071002.o87A2jx1025121@brsbs01.icerasemi.com
State New
Headers show

Commit Message

Joe Seymour Sept. 7, 2010, 10:02 a.m. UTC
This one:

Prints the target architecture as well as goal function/name code.

Prints a "Still Searching" message every 4bn loops to let users know
it's still doing something useful. Message is printed from
test_sequence.

Adds -min-cost flag. Instead of starting loop in main_synth from 1,
start from new parameter (min_cost).

Adds -limit flag, superopt checks that it hasn't already found n
results before entering synth or test_sequence.

Tues Aug 31 2010  Joseph Seymour  <Seymour@IceraSemi.com>

	* superopt.c (main): Print target name with output. Added
	-min-cost and -limit flags.
	(main_synth): Added min_const. Made cases in KNOW_START
	conditional.
	(SYNTH): Added -limit check.
	(recurse_last): Added -limit check.
	(test_sequence): Print message every 4bn tested sequences.
diff mbox

Patch

diff -Nup /home/seymour/testpatch/superopt.c ./superopt.c
--- /home/seymour/testpatch/superopt.c	2010-08-27 17:06:53.199654000 +0100
+++ ./superopt.c	2010-08-27 17:01:19.046285000 +0100
@@ -39,6 +39,10 @@  int flag_use_carry = 1;
 int flag_shifts = 0;
 int flag_extracts = 0;
 int flag_nl = 0;
+int flag_result_lim = 0;
+unsigned int n_reassure_after = 4000000000UL;
+unsigned int cycle_count      = 4000000000UL;
+unsigned int reassure_count = 0;
 
 /* Counts the number of solutions found.  Flags to top loop that it should
    not go deeper.  */
@@ -208,7 +212,10 @@  xmalloc (size)
 #if HAS_NULLIFICATION
 #define SYNTH(sequence, n_insns, values, n_values, goal_value, \
 	      allowed_cost, cy, prune_flags, nullify_flag)		\
-  do {									\
+  do {                                                                  \
+    /* Don't test any more sequences if we have enough results */       \
+    if (flag_result_lim > 0 && success >= flag_result_lim)              \
+      return;								\
     if (allowed_cost == 1)						\
       synth_last(sequence, n_insns, values, n_values, goal_value,	\
 		 allowed_cost, cy, prune_flags, nullify_flag);		\
@@ -220,6 +227,9 @@  xmalloc (size)
 #define SYNTH(sequence, n_insns, values, n_values, goal_value, \
 	      allowed_cost, cy, prune_flags, nullify_flag)		\
   do {									\
+    /* Don't test any more sequences if we have enough results */       \
+    if (flag_result_lim > 0 && success >= flag_result_lim)              \
+      return;								\
     if (allowed_cost == 1)						\
       synth_last(sequence, n_insns, values, n_values, goal_value,	\
 		 allowed_cost, cy, prune_flags);			\
@@ -375,6 +385,9 @@  recurse_last(opcode_t opcode,
 {
   insn_t insn;
 
+  /* Don't test any more sequences if we have enough results */
+  if (flag_result_lim > 0 && success >= flag_result_lim) return;
+
   if (goal_value == v)
     {
       /* We are at a leaf node and got the right answer for the
@@ -2517,6 +2530,14 @@  test_sequence(insn_t *sequence, int n_in
   word *test_set = test_sets;
   const int arity = goal_function_arity;
 
+  /* Print a message every n_reassure_after, to reassure users that
+     we're still doing something useful. */
+  if (cycle_count-- == 0)
+    {
+      fprintf(stderr,"Still Searching #%d\n",reassure_count++);
+      cycle_count = n_reassure_after;
+    }
+
   /* Test each of the precomputed values.  */
   for (j = n_test_sets; j > 0; j--)
     {
@@ -2662,7 +2683,7 @@  test_sequence(insn_t *sequence, int n_in
 
    MAXMAX_COST is the maximum cost for any sequence we will accept.  */
 void
-main_synth(int maxmax_cost, int allowed_extra_cost)
+main_synth(int min_cost, int maxmax_cost, int allowed_extra_cost)
 {
   int max_cost;
   word values[0x100];
@@ -2689,27 +2710,27 @@  main_synth(int maxmax_cost, int allowed_
 #if KNOW_START
   switch (goal_function)
     {
-    case FFS:
 #if M88000
+    case FFS:
       /* Best ffs starting place.  */
       sequence[ii++] = (insn_t) { ADC_CO, CNST(0), 0, 1 };
       sequence[ii++] = (insn_t) { AND, 0, 1, 2 };
-#endif
       break;
+#endif
 
-    case ZDEPI_FOR_MOVSI:
 #if SPARC
+    case ZDEPI_FOR_MOVSI:
       sequence[ii++] = (insn_t) { SUB, CNST(0), 0, 1 };
       sequence[ii++] = (insn_t) { AND, 0, 1, 2 };
-#endif
       break;
+#endif
     default: break;
     }
 #endif
 
   printf("Superoptimizing at cost");
   success = 0;
-  for (max_cost = 1; max_cost <= maxmax_cost; max_cost++)
+  for (max_cost = min_cost; max_cost <= maxmax_cost; max_cost++)
     {
 #if TIMING
       for (i = 0; i <= max_cost; i++)
@@ -2835,6 +2856,7 @@  int
 main(int argc, char **argv)
 {
   int maxmax_cost = 4;
+  int min_cost = 1;
   int allowed_extra_cost = 0;
   int flag_all = 0;
   char *program = strrchr (argv[0], '/');
@@ -2876,6 +2898,31 @@  main(int argc, char **argv)
 	flag_shifts = 1;
       else if (!strncmp(arg, "-extracts", arglen))
 	flag_extracts = 1;
+      else if (!strncmp(arg, "-limit", arglen))
+	{
+	  argv++;
+	  argc--;
+	  if (argc == 0)
+	    {
+	      fprintf(stderr, "superoptimizer: argument to `-limit' expected\n");
+	      exit(-1);
+	    }
+	  flag_result_lim = atoi(argv[0]);
+	}
+      else if (!strncmp(arg, "-min-cost", arglen))
+	{
+	  argv++;
+	  argc--;
+	  if (argc == 0)
+	    {
+              if ( atoi(argv[0]) < 1 ) {
+                fprintf(stderr,"superoptimizer: -min-cost must be at least 1\n");
+              }
+	      fprintf(stderr, "superoptimizer: argument to `-min-cost' expected\n");
+	      exit(-1);
+	    }
+	  min_cost = atoi(argv[0]);
+	}
       else if (!strncmp(arg, "-max-cost", arglen))
 	{
 	  argv++;
@@ -2922,7 +2969,7 @@  main(int argc, char **argv)
 	  int i, len, maxlen, cols, maxcols;
 	  char *prefix;
 	  fprintf(stderr, "Calling sequence:\n\n");
-	  fprintf(stderr, "\t%s -f<goal-function> [-assembly] [-max-cost n] \\\n", program);
+	  fprintf(stderr, "\t%s -f<goal-function> [-assembly] [-max-cost n] [-min-cost n]\\\n", program);
 	  fprintf(stderr, "\t\t[-no-carry-insns] [-extra-cost n] [-nl]\n\n");
 	  fprintf(stderr, "Target machine: %s\n\n", TARGET_STRING);
 	  fprintf(stderr, "Supported goal functions:\n\n");
@@ -2968,13 +3015,14 @@  main(int argc, char **argv)
       for (goal_function = 0; goal_function < LAST_AND_UNUSED_GOAL_CODE;
 	   goal_function++)
 	{
-	  printf("Searching for goal %s: ",
-		  GET_GOAL_NAME (goal_function));
+	  printf("Searching for goal %s on %s: ",
+		  GET_GOAL_NAME (goal_function),
+                 TARGET_STRING);
 	  printf("%s\n", GET_GOAL_C_CODE(goal_function));
 
 	  goal_function_arity = GET_GOAL_ARITY(goal_function);
 	  eval_goal_function = GET_GOAL_FUNCTION(goal_function);
-	  main_synth(maxmax_cost, allowed_extra_cost);
+	  main_synth(min_cost,maxmax_cost, allowed_extra_cost);
 	}
       exit (0);
     }
@@ -2985,7 +3033,10 @@  main(int argc, char **argv)
       exit(-1);
     }
 
-  printf("Searching for %s\n", GET_GOAL_C_CODE(goal_function));
-  main_synth(maxmax_cost, allowed_extra_cost);
+  printf("Searching for %s : %s on %s\n",
+         GET_GOAL_NAME (goal_function),
+         GET_GOAL_C_CODE (goal_function),
+         TARGET_STRING);
+  main_synth(min_cost, maxmax_cost, allowed_extra_cost);
   exit (!success);
 }