diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index 69703a7401d9f4a72cec4e8548aa8577da3dc3b4..d26bf109f11d1824dd3f0fa43766ce9834ea10b0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,16 @@
+2007-09-10  Trevor Smigiel  <trevor_smigiel@playstation.sony.com>
+	    Revital Eres  <eres@il.ibm.com>
+
+	* target.h (struct gcc_target.sched): New field: sms_res_mii.
+	(struct ddg): Define.
+	* target-def.h (TARGET_SCHED_SMS_RES_MII): Define.
+	(TARGET_SCHED): Add TARGET_SCHED_SMS_RES_MII.
+	* config/spu/spu.c: Include ddg.h.
+	(TARGET_SCHED_SMS_RES_MII): Define.
+	(spu_sms_res_mii): New function to calculate mii.
+	* modulo-sched (res_MII): Use it.
+	* doc/tm.texi: Document TARGET_SCHED_SMS_RES_MII.
+
 2007-09-10  Andreas Krebbel  <krebbel1@de.ibm.com>
 
 	* config/s390/s390.c (s390_dump_pool): Create copy of constant
diff --git a/gcc/config/spu/spu.c b/gcc/config/spu/spu.c
index 00df45b4683fba85f6898fa3e8195696bf5c2d51..3423cef42669bf1b8118f7e77d289b56720bce1e 100644
--- a/gcc/config/spu/spu.c
+++ b/gcc/config/spu/spu.c
@@ -53,6 +53,7 @@
 #include "tree-gimple.h"
 #include "tm-constrs.h"
 #include "spu-builtins.h"
+#include "ddg.h"
 
 /* Builtin types, data and prototypes. */
 struct spu_builtin_range
@@ -136,6 +137,7 @@ static tree spu_builtin_mul_widen_odd (tree);
 static tree spu_builtin_mask_for_load (void);
 static int spu_builtin_vectorization_cost (bool);
 static bool spu_vector_alignment_reachable (const_tree, bool);
+static int spu_sms_res_mii (struct ddg *g);
 
 extern const char *reg_names[];
 rtx spu_compare_op0, spu_compare_op1;
@@ -287,6 +289,9 @@ const struct attribute_spec spu_attribute_table[];
 #undef TARGET_LIBGCC_SHIFT_COUNT_MODE
 #define TARGET_LIBGCC_SHIFT_COUNT_MODE spu_libgcc_shift_count_mode
 
+#undef TARGET_SCHED_SMS_RES_MII
+#define TARGET_SCHED_SMS_RES_MII spu_sms_res_mii
+
 struct gcc_target targetm = TARGET_INITIALIZER;
 
 void
@@ -5506,6 +5511,38 @@ spu_vector_alignment_reachable (const_tree type ATTRIBUTE_UNUSED, bool is_packed
   return true;
 }
 
+/* Count the total number of instructions in each pipe and return the
+   maximum, which is used as the Minimum Iteration Interval (MII)
+   in the modulo scheduler.  get_pipe() will return -2, -1, 0, or 1.
+   -2 are instructions that can go in pipe0 or pipe1.  */
+static int
+spu_sms_res_mii (struct ddg *g)
+{
+  int i;
+  unsigned t[4] = {0, 0, 0, 0};
+
+  for (i = 0; i < g->num_nodes; i++)
+    {
+      rtx insn = g->nodes[i].insn;
+      int p = get_pipe (insn) + 2;
+
+      assert (p >= 0);
+      assert (p < 4);
+
+      t[p]++;
+      if (dump_file && INSN_P (insn))
+            fprintf (dump_file, "i%d %s %d %d\n",
+                     INSN_UID (insn),
+                     insn_data[INSN_CODE(insn)].name,
+                     p, t[p]);
+    }
+  if (dump_file)
+    fprintf (dump_file, "%d %d %d %d\n", t[0], t[1], t[2], t[3]);
+
+  return MAX ((t[0] + t[2] + t[3] + 1) / 2, MAX (t[2], t[3]));
+}
+
+
 void
 spu_init_expanders (void)
 {   
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index 4f8c0297d1ea132f3ae05e1a4718ac79c3e6031d..deb3abc2e87197e291a94270f009fbaca8a0d788 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -6342,6 +6342,15 @@ an additional structure @var{spec_info} should be filled by the target.
 The structure describes speculation types that can be used in the scheduler.
 @end deftypefn
 
+@deftypefn {Target Hook} int TARGET_SCHED_SMS_RES_MII (struct ddg *@var{g})
+This hook is called by the swing modulo scheduler to calculate a
+resource-based lower bound which is based on the resources available in
+the machine and the resources required by each instruction.  The target
+backend can use @var{g} to calculate such bound.  A very simple lower
+bound will be used in case this hook is not implemented: the total number
+of instructions divided by the issue rate.
+@end deftypefn
+
 @node Sections
 @section Dividing the Output into Sections (Texts, Data, @dots{})
 @c the above section title is WAY too long.  maybe cut the part between
diff --git a/gcc/modulo-sched.c b/gcc/modulo-sched.c
index 075fe2eae9cac1aa88fa10e6a8d7f07af59b90b7..9a806c106e9f6e75c6c396df57fbc0fa893b2d7e 100644
--- a/gcc/modulo-sched.c
+++ b/gcc/modulo-sched.c
@@ -374,6 +374,9 @@ const_iteration_count (rtx count_reg, basic_block pre_header,
 static int
 res_MII (ddg_ptr g)
 {
+  if (targetm.sched.sms_res_mii)
+    return targetm.sched.sms_res_mii (g); 
+  
   return (g->num_nodes / issue_rate);
 }
 
diff --git a/gcc/target-def.h b/gcc/target-def.h
index c195af2004e10f72f91944206b5d99952da8358c..5f5c6f3ae273ac600bf744a9dc5e8e8639f0e64c 100644
--- a/gcc/target-def.h
+++ b/gcc/target-def.h
@@ -322,7 +322,7 @@
 #define TARGET_SCHED_GEN_CHECK 0
 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD_SPEC 0
 #define TARGET_SCHED_SET_SCHED_FLAGS 0
-
+#define TARGET_SCHED_SMS_RES_MII 0
 
 #define TARGET_SCHED						\
   {TARGET_SCHED_ADJUST_COST,					\
@@ -351,7 +351,8 @@
    TARGET_SCHED_NEEDS_BLOCK_P,                                  \
    TARGET_SCHED_GEN_CHECK,                                      \
    TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD_GUARD_SPEC, \
-   TARGET_SCHED_SET_SCHED_FLAGS}
+   TARGET_SCHED_SET_SCHED_FLAGS,                                \
+   TARGET_SCHED_SMS_RES_MII}
 
 #define TARGET_VECTORIZE_BUILTIN_MASK_FOR_LOAD 0
 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
diff --git a/gcc/target.h b/gcc/target.h
index bf76402eaa5fb787b2f555a3cf7ed5bd823fc7ce..4d8cd0122203eb24c1bbd34ae0b09b101f857520 100644
--- a/gcc/target.h
+++ b/gcc/target.h
@@ -88,6 +88,9 @@ typedef struct secondary_reload_info
 /* This is defined in sched-int.h .  */
 struct _dep;
 
+/* This is defined in ddg.h .  */
+struct ddg;
+
 struct gcc_target
 {
   /* Functions that output assembler for the target.  */
@@ -397,6 +400,12 @@ struct gcc_target
        information about the speculation capabilities of the target.
        The parameter is a pointer to spec_info variable.  */
     void (* set_sched_flags) (struct spec_info_def *);
+
+    /* The following member value is a pointer to a function that provides
+       information about the target resource-based lower bound which is
+       used by the swing modulo scheduler.  The parameter is a pointer
+       to ddg variable.  */
+    int (* sms_res_mii) (struct ddg *);
   } sched;
 
   /* Functions relating to vectorization.  */