i915: Remove most of the code under gen >= 4 checks.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_eu_emit.c
index 5caebfc5a90bbf2d5e929b03fdfe3835c3591e4b..f2cacd17762f8d6ab31d75ca2961edd5740391b0 100644 (file)
@@ -67,6 +67,9 @@ gen6_resolve_implied_move(struct brw_compile *p,
    if (intel->gen < 6)
       return;
 
+   if (src->file == BRW_MESSAGE_REGISTER_FILE)
+      return;
+
    if (src->file != BRW_ARCHITECTURE_REGISTER_FILE || src->nr != BRW_ARF_NULL) {
       brw_push_insn_state(p);
       brw_set_mask_control(p, BRW_MASK_DISABLE);
@@ -81,10 +84,18 @@ gen6_resolve_implied_move(struct brw_compile *p,
 static void
 gen7_convert_mrf_to_grf(struct brw_compile *p, struct brw_reg *reg)
 {
+   /* From the BSpec / ISA Reference / send - [DevIVB+]:
+    * "The send with EOT should use register space R112-R127 for <src>. This is
+    *  to enable loading of a new thread into the same slot while the message
+    *  with EOT for current thread is pending dispatch."
+    *
+    * Since we're pretending to have 16 MRFs anyway, we may as well use the
+    * registers required for messages with EOT.
+    */
    struct intel_context *intel = &p->brw->intel;
    if (intel->gen == 7 && reg->file == BRW_MESSAGE_REGISTER_FILE) {
       reg->file = BRW_GENERAL_REGISTER_FILE;
-      reg->nr += 111;
+      reg->nr += GEN7_MRF_HACK_START;
    }
 }
 
@@ -115,7 +126,10 @@ brw_set_dest(struct brw_compile *p, struct brw_instruction *insn,
       else {
         insn->bits1.da16.dest_subreg_nr = dest.subnr / 16;
         insn->bits1.da16.dest_writemask = dest.dw1.bits.writemask;
-        /* even ignored in da16, still need to set as '01' */
+        /* From the Ivybridge PRM, Vol 4, Part 3, Section 5.2.4.1:
+         *    Although Dst.HorzStride is a don't care for Align16, HW needs
+         *    this to be programmed as "01".
+         */
         insn->bits1.da16.dest_horiz_stride = 1;
       }
    }
@@ -225,11 +239,25 @@ void
 brw_set_src0(struct brw_compile *p, struct brw_instruction *insn,
             struct brw_reg reg)
 {
+   struct brw_context *brw = p->brw;
+   struct intel_context *intel = &brw->intel;
+
    if (reg.type != BRW_ARCHITECTURE_REGISTER_FILE)
       assert(reg.nr < 128);
 
    gen7_convert_mrf_to_grf(p, &reg);
 
+   if (intel->gen >= 6 && (insn->header.opcode == BRW_OPCODE_SEND ||
+                           insn->header.opcode == BRW_OPCODE_SENDC)) {
+      /* Any source modifiers or regions will be ignored, since this just
+       * identifies the MRF/GRF to start reading the message contents from.
+       * Check for some likely failures.
+       */
+      assert(!reg.negate);
+      assert(!reg.abs);
+      assert(reg.address_mode == BRW_ADDRESS_DIRECT);
+   }
+
    validate_reg(insn, reg);
 
    insn->bits1.da1.src0_reg_file = reg.file;
@@ -306,7 +334,8 @@ void brw_set_src1(struct brw_compile *p,
 {
    assert(reg.file != BRW_MESSAGE_REGISTER_FILE);
 
-   assert(reg.nr < 128);
+   if (reg.type != BRW_ARCHITECTURE_REGISTER_FILE)
+      assert(reg.nr < 128);
 
    gen7_convert_mrf_to_grf(p, &reg);
 
@@ -370,14 +399,54 @@ void brw_set_src1(struct brw_compile *p,
    }
 }
 
+/**
+ * Set the Message Descriptor and Extended Message Descriptor fields
+ * for SEND messages.
+ *
+ * \note This zeroes out the Function Control bits, so it must be called
+ *       \b before filling out any message-specific data.  Callers can
+ *       choose not to fill in irrelevant bits; they will be zero.
+ */
+static void
+brw_set_message_descriptor(struct brw_compile *p,
+                          struct brw_instruction *inst,
+                          enum brw_message_target sfid,
+                          unsigned msg_length,
+                          unsigned response_length,
+                          bool header_present,
+                          bool end_of_thread)
+{
+   struct intel_context *intel = &p->brw->intel;
+
+   brw_set_src1(p, inst, brw_imm_d(0));
 
+   if (intel->gen >= 5) {
+      inst->bits3.generic_gen5.header_present = header_present;
+      inst->bits3.generic_gen5.response_length = response_length;
+      inst->bits3.generic_gen5.msg_length = msg_length;
+      inst->bits3.generic_gen5.end_of_thread = end_of_thread;
+
+      if (intel->gen >= 6) {
+        /* On Gen6+ Message target/SFID goes in bits 27:24 of the header */
+        inst->header.destreg__conditionalmod = sfid;
+      } else {
+        /* Set Extended Message Descriptor (ex_desc) */
+        inst->bits2.send_gen5.sfid = sfid;
+        inst->bits2.send_gen5.end_of_thread = end_of_thread;
+      }
+   } else {
+      inst->bits3.generic.response_length = response_length;
+      inst->bits3.generic.msg_length = msg_length;
+      inst->bits3.generic.msg_target = sfid;
+      inst->bits3.generic.end_of_thread = end_of_thread;
+   }
+}
 
 static void brw_set_math_message( struct brw_compile *p,
                                  struct brw_instruction *insn,
                                  GLuint function,
                                  GLuint integer_type,
-                                 GLboolean low_precision,
-                                 GLboolean saturate,
+                                 bool low_precision,
                                  GLuint dataType )
 {
    struct brw_context *brw = p->brw;
@@ -409,77 +478,59 @@ static void brw_set_math_message( struct brw_compile *p,
       break;
    }
 
-   brw_set_src1(p, insn, brw_imm_d(0));
+
+   brw_set_message_descriptor(p, insn, BRW_SFID_MATH,
+                             msg_length, response_length, false, false);
    if (intel->gen == 5) {
       insn->bits3.math_gen5.function = function;
       insn->bits3.math_gen5.int_type = integer_type;
       insn->bits3.math_gen5.precision = low_precision;
-      insn->bits3.math_gen5.saturate = saturate;
+      insn->bits3.math_gen5.saturate = insn->header.saturate;
       insn->bits3.math_gen5.data_type = dataType;
       insn->bits3.math_gen5.snapshot = 0;
-      insn->bits3.math_gen5.header_present = 0;
-      insn->bits3.math_gen5.response_length = response_length;
-      insn->bits3.math_gen5.msg_length = msg_length;
-      insn->bits3.math_gen5.end_of_thread = 0;
-      insn->bits2.send_gen5.sfid = BRW_MESSAGE_TARGET_MATH;
-      insn->bits2.send_gen5.end_of_thread = 0;
    } else {
       insn->bits3.math.function = function;
       insn->bits3.math.int_type = integer_type;
       insn->bits3.math.precision = low_precision;
-      insn->bits3.math.saturate = saturate;
+      insn->bits3.math.saturate = insn->header.saturate;
       insn->bits3.math.data_type = dataType;
-      insn->bits3.math.response_length = response_length;
-      insn->bits3.math.msg_length = msg_length;
-      insn->bits3.math.msg_target = BRW_MESSAGE_TARGET_MATH;
-      insn->bits3.math.end_of_thread = 0;
    }
+   insn->header.saturate = 0;
 }
 
 
 static void brw_set_ff_sync_message(struct brw_compile *p,
                                    struct brw_instruction *insn,
-                                   GLboolean allocate,
+                                   bool allocate,
                                    GLuint response_length,
-                                   GLboolean end_of_thread)
+                                   bool end_of_thread)
 {
-   struct brw_context *brw = p->brw;
-   struct intel_context *intel = &brw->intel;
-   brw_set_src1(p, insn, brw_imm_d(0));
-
+   brw_set_message_descriptor(p, insn, BRW_SFID_URB,
+                             1, response_length, true, end_of_thread);
    insn->bits3.urb_gen5.opcode = 1; /* FF_SYNC */
    insn->bits3.urb_gen5.offset = 0; /* Not used by FF_SYNC */
    insn->bits3.urb_gen5.swizzle_control = 0; /* Not used by FF_SYNC */
    insn->bits3.urb_gen5.allocate = allocate;
    insn->bits3.urb_gen5.used = 0; /* Not used by FF_SYNC */
    insn->bits3.urb_gen5.complete = 0; /* Not used by FF_SYNC */
-   insn->bits3.urb_gen5.header_present = 1;
-   insn->bits3.urb_gen5.response_length = response_length; /* may be 1 or 0 */
-   insn->bits3.urb_gen5.msg_length = 1;
-   insn->bits3.urb_gen5.end_of_thread = end_of_thread;
-   if (intel->gen >= 6) {
-      insn->header.destreg__conditionalmod = BRW_MESSAGE_TARGET_URB;
-   } else {
-      insn->bits2.send_gen5.sfid = BRW_MESSAGE_TARGET_URB;
-      insn->bits2.send_gen5.end_of_thread = end_of_thread;
-   }
 }
 
 static void brw_set_urb_message( struct brw_compile *p,
                                 struct brw_instruction *insn,
-                                GLboolean allocate,
-                                GLboolean used,
+                                bool allocate,
+                                bool used,
                                 GLuint msg_length,
                                 GLuint response_length,
-                                GLboolean end_of_thread,
-                                GLboolean complete,
+                                bool end_of_thread,
+                                bool complete,
                                 GLuint offset,
                                 GLuint swizzle_control )
 {
    struct brw_context *brw = p->brw;
    struct intel_context *intel = &brw->intel;
-   brw_set_src1(p, insn, brw_imm_d(0));
 
+   brw_set_message_descriptor(p, insn, BRW_SFID_URB,
+                             msg_length, response_length, true, end_of_thread);
    if (intel->gen == 7) {
       insn->bits3.urb_gen7.opcode = 0; /* URB_WRITE_HWORD */
       insn->bits3.urb_gen7.offset = offset;
@@ -488,11 +539,6 @@ static void brw_set_urb_message( struct brw_compile *p,
       /* per_slot_offset = 0 makes it ignore offsets in message header */
       insn->bits3.urb_gen7.per_slot_offset = 0;
       insn->bits3.urb_gen7.complete = complete;
-      insn->bits3.urb_gen7.header_present = 1;
-      insn->bits3.urb_gen7.response_length = response_length;
-      insn->bits3.urb_gen7.msg_length = msg_length;
-      insn->bits3.urb_gen7.end_of_thread = end_of_thread;
-      insn->header.destreg__conditionalmod = BRW_MESSAGE_TARGET_URB;
    } else if (intel->gen >= 5) {
       insn->bits3.urb_gen5.opcode = 0; /* URB_WRITE */
       insn->bits3.urb_gen5.offset = offset;
@@ -500,20 +546,6 @@ static void brw_set_urb_message( struct brw_compile *p,
       insn->bits3.urb_gen5.allocate = allocate;
       insn->bits3.urb_gen5.used = used;        /* ? */
       insn->bits3.urb_gen5.complete = complete;
-      insn->bits3.urb_gen5.header_present = 1;
-      insn->bits3.urb_gen5.response_length = response_length;
-      insn->bits3.urb_gen5.msg_length = msg_length;
-      insn->bits3.urb_gen5.end_of_thread = end_of_thread;
-      if (intel->gen >= 6) {
-        /* For SNB, the SFID bits moved to the condmod bits, and
-         * EOT stayed in bits3 above.  Does the EOT bit setting
-         * below on Ironlake even do anything?
-         */
-        insn->header.destreg__conditionalmod = BRW_MESSAGE_TARGET_URB;
-      } else {
-        insn->bits2.send_gen5.sfid = BRW_MESSAGE_TARGET_URB;
-        insn->bits2.send_gen5.end_of_thread = end_of_thread;
-      }
    } else {
       insn->bits3.urb.opcode = 0;      /* ? */
       insn->bits3.urb.offset = offset;
@@ -521,10 +553,6 @@ static void brw_set_urb_message( struct brw_compile *p,
       insn->bits3.urb.allocate = allocate;
       insn->bits3.urb.used = used;     /* ? */
       insn->bits3.urb.complete = complete;
-      insn->bits3.urb.response_length = response_length;
-      insn->bits3.urb.msg_length = msg_length;
-      insn->bits3.urb.msg_target = BRW_MESSAGE_TARGET_URB;
-      insn->bits3.urb.end_of_thread = end_of_thread;
    }
 }
 
@@ -535,63 +563,55 @@ brw_set_dp_write_message(struct brw_compile *p,
                         GLuint msg_control,
                         GLuint msg_type,
                         GLuint msg_length,
-                        GLboolean header_present,
-                        GLuint pixel_scoreboard_clear,
+                        bool header_present,
+                        GLuint last_render_target,
                         GLuint response_length,
                         GLuint end_of_thread,
                         GLuint send_commit_msg)
 {
    struct brw_context *brw = p->brw;
    struct intel_context *intel = &brw->intel;
-   brw_set_src1(p, insn, brw_imm_ud(0));
+   unsigned sfid;
+
+   if (intel->gen >= 7) {
+      /* Use the Render Cache for RT writes; otherwise use the Data Cache */
+      if (msg_type == GEN6_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE)
+        sfid = GEN6_SFID_DATAPORT_RENDER_CACHE;
+      else
+        sfid = GEN7_SFID_DATAPORT_DATA_CACHE;
+   } else if (intel->gen == 6) {
+      /* Use the render cache for all write messages. */
+      sfid = GEN6_SFID_DATAPORT_RENDER_CACHE;
+   } else {
+      sfid = BRW_SFID_DATAPORT_WRITE;
+   }
+
+   brw_set_message_descriptor(p, insn, sfid, msg_length, response_length,
+                             header_present, end_of_thread);
 
    if (intel->gen >= 7) {
       insn->bits3.gen7_dp.binding_table_index = binding_table_index;
       insn->bits3.gen7_dp.msg_control = msg_control;
-      insn->bits3.gen7_dp.pixel_scoreboard_clear = pixel_scoreboard_clear;
+      insn->bits3.gen7_dp.last_render_target = last_render_target;
       insn->bits3.gen7_dp.msg_type = msg_type;
-      insn->bits3.gen7_dp.header_present = header_present;
-      insn->bits3.gen7_dp.response_length = response_length;
-      insn->bits3.gen7_dp.msg_length = msg_length;
-      insn->bits3.gen7_dp.end_of_thread = end_of_thread;
-
-      /* We always use the render cache for write messages */
-      insn->header.destreg__conditionalmod = GEN6_MESSAGE_TARGET_DP_RENDER_CACHE;
    } else if (intel->gen == 6) {
       insn->bits3.gen6_dp.binding_table_index = binding_table_index;
       insn->bits3.gen6_dp.msg_control = msg_control;
-      insn->bits3.gen6_dp.pixel_scoreboard_clear = pixel_scoreboard_clear;
+      insn->bits3.gen6_dp.last_render_target = last_render_target;
       insn->bits3.gen6_dp.msg_type = msg_type;
       insn->bits3.gen6_dp.send_commit_msg = send_commit_msg;
-      insn->bits3.gen6_dp.header_present = header_present;
-      insn->bits3.gen6_dp.response_length = response_length;
-      insn->bits3.gen6_dp.msg_length = msg_length;
-      insn->bits3.gen6_dp.end_of_thread = end_of_thread;
-
-      /* We always use the render cache for write messages */
-      insn->header.destreg__conditionalmod = GEN6_MESSAGE_TARGET_DP_RENDER_CACHE;
    } else if (intel->gen == 5) {
       insn->bits3.dp_write_gen5.binding_table_index = binding_table_index;
       insn->bits3.dp_write_gen5.msg_control = msg_control;
-      insn->bits3.dp_write_gen5.pixel_scoreboard_clear = pixel_scoreboard_clear;
+      insn->bits3.dp_write_gen5.last_render_target = last_render_target;
       insn->bits3.dp_write_gen5.msg_type = msg_type;
       insn->bits3.dp_write_gen5.send_commit_msg = send_commit_msg;
-      insn->bits3.dp_write_gen5.header_present = header_present;
-      insn->bits3.dp_write_gen5.response_length = response_length;
-      insn->bits3.dp_write_gen5.msg_length = msg_length;
-      insn->bits3.dp_write_gen5.end_of_thread = end_of_thread;
-      insn->bits2.send_gen5.sfid = BRW_MESSAGE_TARGET_DATAPORT_WRITE;
-      insn->bits2.send_gen5.end_of_thread = end_of_thread;
    } else {
       insn->bits3.dp_write.binding_table_index = binding_table_index;
       insn->bits3.dp_write.msg_control = msg_control;
-      insn->bits3.dp_write.pixel_scoreboard_clear = pixel_scoreboard_clear;
+      insn->bits3.dp_write.last_render_target = last_render_target;
       insn->bits3.dp_write.msg_type = msg_type;
       insn->bits3.dp_write.send_commit_msg = send_commit_msg;
-      insn->bits3.dp_write.response_length = response_length;
-      insn->bits3.dp_write.msg_length = msg_length;
-      insn->bits3.dp_write.msg_target = BRW_MESSAGE_TARGET_DATAPORT_WRITE;
-      insn->bits3.dp_write.end_of_thread = end_of_thread;
    }
 }
 
@@ -603,133 +623,93 @@ brw_set_dp_read_message(struct brw_compile *p,
                        GLuint msg_type,
                        GLuint target_cache,
                        GLuint msg_length,
+                        bool header_present,
                        GLuint response_length)
 {
    struct brw_context *brw = p->brw;
    struct intel_context *intel = &brw->intel;
-   brw_set_src1(p, insn, brw_imm_d(0));
+   unsigned sfid;
+
+   if (intel->gen >= 7) {
+      sfid = GEN7_SFID_DATAPORT_DATA_CACHE;
+   } else if (intel->gen == 6) {
+      if (target_cache == BRW_DATAPORT_READ_TARGET_RENDER_CACHE)
+        sfid = GEN6_SFID_DATAPORT_RENDER_CACHE;
+      else
+        sfid = GEN6_SFID_DATAPORT_SAMPLER_CACHE;
+   } else {
+      sfid = BRW_SFID_DATAPORT_READ;
+   }
+
+   brw_set_message_descriptor(p, insn, sfid, msg_length, response_length,
+                             header_present, false);
 
    if (intel->gen >= 7) {
       insn->bits3.gen7_dp.binding_table_index = binding_table_index;
       insn->bits3.gen7_dp.msg_control = msg_control;
-      insn->bits3.gen7_dp.pixel_scoreboard_clear = 0;
+      insn->bits3.gen7_dp.last_render_target = 0;
       insn->bits3.gen7_dp.msg_type = msg_type;
-      insn->bits3.gen7_dp.header_present = 1;
-      insn->bits3.gen7_dp.response_length = response_length;
-      insn->bits3.gen7_dp.msg_length = msg_length;
-      insn->bits3.gen7_dp.end_of_thread = 0;
-      insn->header.destreg__conditionalmod = GEN6_MESSAGE_TARGET_DP_CONST_CACHE;
    } else if (intel->gen == 6) {
-      uint32_t target_function;
-
-      if (target_cache == BRW_DATAPORT_READ_TARGET_DATA_CACHE)
-        target_function = GEN6_MESSAGE_TARGET_DP_SAMPLER_CACHE;
-      else
-        target_function = GEN6_MESSAGE_TARGET_DP_RENDER_CACHE;
-
       insn->bits3.gen6_dp.binding_table_index = binding_table_index;
       insn->bits3.gen6_dp.msg_control = msg_control;
-      insn->bits3.gen6_dp.pixel_scoreboard_clear = 0;
+      insn->bits3.gen6_dp.last_render_target = 0;
       insn->bits3.gen6_dp.msg_type = msg_type;
       insn->bits3.gen6_dp.send_commit_msg = 0;
-      insn->bits3.gen6_dp.header_present = 1;
-      insn->bits3.gen6_dp.response_length = response_length;
-      insn->bits3.gen6_dp.msg_length = msg_length;
-      insn->bits3.gen6_dp.end_of_thread = 0;
-      insn->header.destreg__conditionalmod = target_function;
    } else if (intel->gen == 5) {
       insn->bits3.dp_read_gen5.binding_table_index = binding_table_index;
       insn->bits3.dp_read_gen5.msg_control = msg_control;
       insn->bits3.dp_read_gen5.msg_type = msg_type;
       insn->bits3.dp_read_gen5.target_cache = target_cache;
-      insn->bits3.dp_read_gen5.header_present = 1;
-      insn->bits3.dp_read_gen5.response_length = response_length;
-      insn->bits3.dp_read_gen5.msg_length = msg_length;
-      insn->bits3.dp_read_gen5.pad1 = 0;
-      insn->bits3.dp_read_gen5.end_of_thread = 0;
-      insn->bits2.send_gen5.sfid = BRW_MESSAGE_TARGET_DATAPORT_READ;
-      insn->bits2.send_gen5.end_of_thread = 0;
    } else if (intel->is_g4x) {
       insn->bits3.dp_read_g4x.binding_table_index = binding_table_index; /*0:7*/
       insn->bits3.dp_read_g4x.msg_control = msg_control;  /*8:10*/
       insn->bits3.dp_read_g4x.msg_type = msg_type;  /*11:13*/
       insn->bits3.dp_read_g4x.target_cache = target_cache;  /*14:15*/
-      insn->bits3.dp_read_g4x.response_length = response_length;  /*16:19*/
-      insn->bits3.dp_read_g4x.msg_length = msg_length;  /*20:23*/
-      insn->bits3.dp_read_g4x.msg_target = BRW_MESSAGE_TARGET_DATAPORT_READ; /*24:27*/
-      insn->bits3.dp_read_g4x.pad1 = 0;
-      insn->bits3.dp_read_g4x.end_of_thread = 0;
    } else {
       insn->bits3.dp_read.binding_table_index = binding_table_index; /*0:7*/
       insn->bits3.dp_read.msg_control = msg_control;  /*8:11*/
       insn->bits3.dp_read.msg_type = msg_type;  /*12:13*/
       insn->bits3.dp_read.target_cache = target_cache;  /*14:15*/
-      insn->bits3.dp_read.response_length = response_length;  /*16:19*/
-      insn->bits3.dp_read.msg_length = msg_length;  /*20:23*/
-      insn->bits3.dp_read.msg_target = BRW_MESSAGE_TARGET_DATAPORT_READ; /*24:27*/
-      insn->bits3.dp_read.pad1 = 0;  /*28:30*/
-      insn->bits3.dp_read.end_of_thread = 0;  /*31*/
-   }
-}
-
-static void brw_set_sampler_message(struct brw_compile *p,
-                                    struct brw_instruction *insn,
-                                    GLuint binding_table_index,
-                                    GLuint sampler,
-                                    GLuint msg_type,
-                                    GLuint response_length,
-                                    GLuint msg_length,
-                                    GLboolean eot,
-                                    GLuint header_present,
-                                    GLuint simd_mode)
+   }
+}
+
+void
+brw_set_sampler_message(struct brw_compile *p,
+                        struct brw_instruction *insn,
+                        GLuint binding_table_index,
+                        GLuint sampler,
+                        GLuint msg_type,
+                        GLuint response_length,
+                        GLuint msg_length,
+                        GLuint header_present,
+                        GLuint simd_mode,
+                        GLuint return_format)
 {
    struct brw_context *brw = p->brw;
    struct intel_context *intel = &brw->intel;
-   assert(eot == 0);
-   brw_set_src1(p, insn, brw_imm_d(0));
+
+   brw_set_message_descriptor(p, insn, BRW_SFID_SAMPLER, msg_length,
+                             response_length, header_present, false);
 
    if (intel->gen >= 7) {
       insn->bits3.sampler_gen7.binding_table_index = binding_table_index;
       insn->bits3.sampler_gen7.sampler = sampler;
       insn->bits3.sampler_gen7.msg_type = msg_type;
       insn->bits3.sampler_gen7.simd_mode = simd_mode;
-      insn->bits3.sampler_gen7.header_present = header_present;
-      insn->bits3.sampler_gen7.response_length = response_length;
-      insn->bits3.sampler_gen7.msg_length = msg_length;
-      insn->bits3.sampler_gen7.end_of_thread = eot;
-      insn->header.destreg__conditionalmod = BRW_MESSAGE_TARGET_SAMPLER;
    } else if (intel->gen >= 5) {
       insn->bits3.sampler_gen5.binding_table_index = binding_table_index;
       insn->bits3.sampler_gen5.sampler = sampler;
       insn->bits3.sampler_gen5.msg_type = msg_type;
       insn->bits3.sampler_gen5.simd_mode = simd_mode;
-      insn->bits3.sampler_gen5.header_present = header_present;
-      insn->bits3.sampler_gen5.response_length = response_length;
-      insn->bits3.sampler_gen5.msg_length = msg_length;
-      insn->bits3.sampler_gen5.end_of_thread = eot;
-      if (intel->gen >= 6)
-         insn->header.destreg__conditionalmod = BRW_MESSAGE_TARGET_SAMPLER;
-      else {
-         insn->bits2.send_gen5.sfid = BRW_MESSAGE_TARGET_SAMPLER;
-         insn->bits2.send_gen5.end_of_thread = eot;
-      }
    } else if (intel->is_g4x) {
       insn->bits3.sampler_g4x.binding_table_index = binding_table_index;
       insn->bits3.sampler_g4x.sampler = sampler;
       insn->bits3.sampler_g4x.msg_type = msg_type;
-      insn->bits3.sampler_g4x.response_length = response_length;
-      insn->bits3.sampler_g4x.msg_length = msg_length;
-      insn->bits3.sampler_g4x.end_of_thread = eot;
-      insn->bits3.sampler_g4x.msg_target = BRW_MESSAGE_TARGET_SAMPLER;
    } else {
       insn->bits3.sampler.binding_table_index = binding_table_index;
       insn->bits3.sampler.sampler = sampler;
       insn->bits3.sampler.msg_type = msg_type;
-      insn->bits3.sampler.return_format = BRW_SAMPLER_RETURN_FORMAT_FLOAT32;
-      insn->bits3.sampler.response_length = response_length;
-      insn->bits3.sampler.msg_length = msg_length;
-      insn->bits3.sampler.end_of_thread = eot;
-      insn->bits3.sampler.msg_target = BRW_MESSAGE_TARGET_SAMPLER;
+      insn->bits3.sampler.return_format = return_format;
    }
 }
 
@@ -740,8 +720,17 @@ brw_next_insn(struct brw_compile *p, GLuint opcode)
 {
    struct brw_instruction *insn;
 
-   assert(p->nr_insn + 1 < BRW_EU_MAX_INSN);
+   if (p->nr_insn + 1 > p->store_size) {
+      if (0)
+         printf("incresing the store size to %d\n", p->store_size << 1);
+      p->store_size <<= 1;
+      p->store = reralloc(p->mem_ctx, p->store,
+                          struct brw_instruction, p->store_size);
+      if (!p->store)
+         assert(!"realloc eu store memeory failed");
+   }
 
+   p->next_insn_offset += 16;
    insn = &p->store[p->nr_insn++];
    memcpy(insn, p->current, sizeof(*insn));
 
@@ -781,6 +770,101 @@ static struct brw_instruction *brw_alu2(struct brw_compile *p,
    return insn;
 }
 
+static int
+get_3src_subreg_nr(struct brw_reg reg)
+{
+   if (reg.vstride == BRW_VERTICAL_STRIDE_0) {
+      assert(brw_is_single_value_swizzle(reg.dw1.bits.swizzle));
+      return reg.subnr / 4 + BRW_GET_SWZ(reg.dw1.bits.swizzle, 0);
+   } else {
+      return reg.subnr / 4;
+   }
+}
+
+static struct brw_instruction *brw_alu3(struct brw_compile *p,
+                                       GLuint opcode,
+                                       struct brw_reg dest,
+                                       struct brw_reg src0,
+                                       struct brw_reg src1,
+                                       struct brw_reg src2)
+{
+   struct intel_context *intel = &p->brw->intel;
+   struct brw_instruction *insn = next_insn(p, opcode);
+
+   gen7_convert_mrf_to_grf(p, &dest);
+
+   assert(insn->header.access_mode == BRW_ALIGN_16);
+
+   assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
+         dest.file == BRW_MESSAGE_REGISTER_FILE);
+   assert(dest.nr < 128);
+   assert(dest.address_mode == BRW_ADDRESS_DIRECT);
+   assert(dest.type == BRW_REGISTER_TYPE_F ||
+          dest.type == BRW_REGISTER_TYPE_D ||
+          dest.type == BRW_REGISTER_TYPE_UD);
+   insn->bits1.da3src.dest_reg_file = (dest.file == BRW_MESSAGE_REGISTER_FILE);
+   insn->bits1.da3src.dest_reg_nr = dest.nr;
+   insn->bits1.da3src.dest_subreg_nr = dest.subnr / 16;
+   insn->bits1.da3src.dest_writemask = dest.dw1.bits.writemask;
+   guess_execution_size(p, insn, dest);
+
+   assert(src0.file == BRW_GENERAL_REGISTER_FILE);
+   assert(src0.address_mode == BRW_ADDRESS_DIRECT);
+   assert(src0.nr < 128);
+   insn->bits2.da3src.src0_swizzle = src0.dw1.bits.swizzle;
+   insn->bits2.da3src.src0_subreg_nr = get_3src_subreg_nr(src0);
+   insn->bits2.da3src.src0_reg_nr = src0.nr;
+   insn->bits1.da3src.src0_abs = src0.abs;
+   insn->bits1.da3src.src0_negate = src0.negate;
+   insn->bits2.da3src.src0_rep_ctrl = src0.vstride == BRW_VERTICAL_STRIDE_0;
+
+   assert(src1.file == BRW_GENERAL_REGISTER_FILE);
+   assert(src1.address_mode == BRW_ADDRESS_DIRECT);
+   assert(src1.nr < 128);
+   insn->bits2.da3src.src1_swizzle = src1.dw1.bits.swizzle;
+   insn->bits2.da3src.src1_subreg_nr_low = get_3src_subreg_nr(src1) & 0x3;
+   insn->bits3.da3src.src1_subreg_nr_high = get_3src_subreg_nr(src1) >> 2;
+   insn->bits2.da3src.src1_rep_ctrl = src1.vstride == BRW_VERTICAL_STRIDE_0;
+   insn->bits3.da3src.src1_reg_nr = src1.nr;
+   insn->bits1.da3src.src1_abs = src1.abs;
+   insn->bits1.da3src.src1_negate = src1.negate;
+
+   assert(src2.file == BRW_GENERAL_REGISTER_FILE);
+   assert(src2.address_mode == BRW_ADDRESS_DIRECT);
+   assert(src2.nr < 128);
+   insn->bits3.da3src.src2_swizzle = src2.dw1.bits.swizzle;
+   insn->bits3.da3src.src2_subreg_nr = get_3src_subreg_nr(src2);
+   insn->bits3.da3src.src2_rep_ctrl = src2.vstride == BRW_VERTICAL_STRIDE_0;
+   insn->bits3.da3src.src2_reg_nr = src2.nr;
+   insn->bits1.da3src.src2_abs = src2.abs;
+   insn->bits1.da3src.src2_negate = src2.negate;
+
+   if (intel->gen >= 7) {
+      /* Set both the source and destination types based on dest.type,
+       * ignoring the source register types.  The MAD and LRP emitters ensure
+       * that all four types are float.  The BFE and BFI2 emitters, however,
+       * may send us mixed D and UD types and want us to ignore that and use
+       * the destination type.
+       */
+      switch (dest.type) {
+      case BRW_REGISTER_TYPE_F:
+         insn->bits1.da3src.src_type = BRW_3SRC_TYPE_F;
+         insn->bits1.da3src.dst_type = BRW_3SRC_TYPE_F;
+         break;
+      case BRW_REGISTER_TYPE_D:
+         insn->bits1.da3src.src_type = BRW_3SRC_TYPE_D;
+         insn->bits1.da3src.dst_type = BRW_3SRC_TYPE_D;
+         break;
+      case BRW_REGISTER_TYPE_UD:
+         insn->bits1.da3src.src_type = BRW_3SRC_TYPE_UD;
+         insn->bits1.da3src.dst_type = BRW_3SRC_TYPE_UD;
+         break;
+      }
+   }
+
+   return insn;
+}
+
 
 /***********************************************************************
  * Convenience routines.
@@ -802,6 +886,30 @@ struct brw_instruction *brw_##OP(struct brw_compile *p,    \
    return brw_alu2(p, BRW_OPCODE_##OP, dest, src0, src1);      \
 }
 
+#define ALU3(OP)                                       \
+struct brw_instruction *brw_##OP(struct brw_compile *p,        \
+             struct brw_reg dest,                      \
+             struct brw_reg src0,                      \
+             struct brw_reg src1,                      \
+             struct brw_reg src2)                      \
+{                                                      \
+   return brw_alu3(p, BRW_OPCODE_##OP, dest, src0, src1, src2);        \
+}
+
+#define ALU3F(OP)                                               \
+struct brw_instruction *brw_##OP(struct brw_compile *p,         \
+                                 struct brw_reg dest,           \
+                                 struct brw_reg src0,           \
+                                 struct brw_reg src1,           \
+                                 struct brw_reg src2)           \
+{                                                               \
+   assert(dest.type == BRW_REGISTER_TYPE_F);                    \
+   assert(src0.type == BRW_REGISTER_TYPE_F);                    \
+   assert(src1.type == BRW_REGISTER_TYPE_F);                    \
+   assert(src2.type == BRW_REGISTER_TYPE_F);                    \
+   return brw_alu3(p, BRW_OPCODE_##OP, dest, src0, src1, src2); \
+}
+
 /* Rounding operations (other than RNDD) require two instructions - the first
  * stores a rounded value (possibly the wrong way) in the dest register, but
  * also sets a per-channel "increment bit" in the flag register.  A predicated
@@ -839,6 +947,8 @@ ALU2(SHL)
 ALU2(RSR)
 ALU2(RSL)
 ALU2(ASR)
+ALU1(F32TO16)
+ALU1(F16TO32)
 ALU1(FRC)
 ALU1(RNDD)
 ALU2(MAC)
@@ -850,7 +960,15 @@ ALU2(DP3)
 ALU2(DP2)
 ALU2(LINE)
 ALU2(PLN)
-
+ALU3F(MAD)
+ALU3F(LRP)
+ALU1(BFREV)
+ALU3(BFE)
+ALU2(BFI1)
+ALU3(BFI2)
+ALU1(FBH)
+ALU1(FBL)
+ALU1(CBIT)
 
 ROUND(RNDZ)
 ROUND(RNDE)
@@ -879,6 +997,28 @@ struct brw_instruction *brw_ADD(struct brw_compile *p,
    return brw_alu2(p, BRW_OPCODE_ADD, dest, src0, src1);
 }
 
+struct brw_instruction *brw_AVG(struct brw_compile *p,
+                                struct brw_reg dest,
+                                struct brw_reg src0,
+                                struct brw_reg src1)
+{
+   assert(dest.type == src0.type);
+   assert(src0.type == src1.type);
+   switch (src0.type) {
+   case BRW_REGISTER_TYPE_B:
+   case BRW_REGISTER_TYPE_UB:
+   case BRW_REGISTER_TYPE_W:
+   case BRW_REGISTER_TYPE_UW:
+   case BRW_REGISTER_TYPE_D:
+   case BRW_REGISTER_TYPE_UD:
+      break;
+   default:
+      assert(!"Bad type for brw_AVG");
+   }
+
+   return brw_alu2(p, BRW_OPCODE_AVG, dest, src0, src1);
+}
+
 struct brw_instruction *brw_MUL(struct brw_compile *p,
                                struct brw_reg dest,
                                struct brw_reg src0,
@@ -950,16 +1090,45 @@ struct brw_instruction *brw_JMPI(struct brw_compile *p,
 static void
 push_if_stack(struct brw_compile *p, struct brw_instruction *inst)
 {
-   p->if_stack[p->if_stack_depth] = inst;
+   p->if_stack[p->if_stack_depth] = inst - p->store;
 
    p->if_stack_depth++;
    if (p->if_stack_array_size <= p->if_stack_depth) {
       p->if_stack_array_size *= 2;
-      p->if_stack = reralloc(p->mem_ctx, p->if_stack, struct brw_instruction *,
+      p->if_stack = reralloc(p->mem_ctx, p->if_stack, int,
                             p->if_stack_array_size);
    }
 }
 
+static struct brw_instruction *
+pop_if_stack(struct brw_compile *p)
+{
+   p->if_stack_depth--;
+   return &p->store[p->if_stack[p->if_stack_depth]];
+}
+
+static void
+push_loop_stack(struct brw_compile *p, struct brw_instruction *inst)
+{
+   if (p->loop_stack_array_size < p->loop_stack_depth) {
+      p->loop_stack_array_size *= 2;
+      p->loop_stack = reralloc(p->mem_ctx, p->loop_stack, int,
+                              p->loop_stack_array_size);
+      p->if_depth_in_loop = reralloc(p->mem_ctx, p->if_depth_in_loop, int,
+                                    p->loop_stack_array_size);
+   }
+
+   p->loop_stack[p->loop_stack_depth] = inst - p->store;
+   p->loop_stack_depth++;
+   p->if_depth_in_loop[p->loop_stack_depth] = 0;
+}
+
+static struct brw_instruction *
+get_inner_do_insn(struct brw_compile *p)
+{
+   return &p->store[p->loop_stack[p->loop_stack_depth - 1]];
+}
+
 /* EU takes the value from the flag register and pushes it onto some
  * sort of a stack (presumably merging with any flag value already on
  * the stack).  Within an if block, the flags at the top of the stack
@@ -990,11 +1159,11 @@ brw_IF(struct brw_compile *p, GLuint execute_size)
    } else if (intel->gen == 6) {
       brw_set_dest(p, insn, brw_imm_w(0));
       insn->bits1.branch_gen6.jump_count = 0;
-      brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
-      brw_set_src1(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
+      brw_set_src0(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
+      brw_set_src1(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
    } else {
-      brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
-      brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
+      brw_set_dest(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
+      brw_set_src0(p, insn, vec1(retype(brw_null_reg(), BRW_REGISTER_TYPE_D)));
       brw_set_src1(p, insn, brw_imm_ud(0));
       insn->bits3.break_cont.jip = 0;
       insn->bits3.break_cont.uip = 0;
@@ -1010,6 +1179,7 @@ brw_IF(struct brw_compile *p, GLuint execute_size)
    p->current->header.predicate_control = BRW_PREDICATE_NONE;
 
    push_if_stack(p, insn);
+   p->if_depth_in_loop[p->loop_stack_depth]++;
    return insn;
 }
 
@@ -1096,7 +1266,21 @@ patch_IF_ELSE(struct brw_compile *p,
 {
    struct intel_context *intel = &p->brw->intel;
 
-   assert(!p->single_program_flow);
+   /* We shouldn't be patching IF and ELSE instructions in single program flow
+    * mode when gen < 6, because in single program flow mode on those
+    * platforms, we convert flow control instructions to conditional ADDs that
+    * operate on IP (see brw_ENDIF).
+    *
+    * However, on Gen6, writing to IP doesn't work in single program flow mode
+    * (see the SandyBridge PRM, Volume 4 part 2, p79: "When SPF is ON, IP may
+    * not be updated by non-flow control instructions.").  And on later
+    * platforms, there is no significant benefit to converting control flow
+    * instructions to conditional ADDs.  So we do patch IF and ELSE
+    * instructions in single program flow mode on those platforms.
+    */
+   if (intel->gen < 6)
+      assert(!p->single_program_flow);
+
    assert(if_inst != NULL && if_inst->header.opcode == BRW_OPCODE_IF);
    assert(endif_inst != NULL);
    assert(else_inst == NULL || else_inst->header.opcode == BRW_OPCODE_ELSE);
@@ -1198,26 +1382,50 @@ void
 brw_ENDIF(struct brw_compile *p)
 {
    struct intel_context *intel = &p->brw->intel;
-   struct brw_instruction *insn;
+   struct brw_instruction *insn = NULL;
    struct brw_instruction *else_inst = NULL;
    struct brw_instruction *if_inst = NULL;
+   struct brw_instruction *tmp;
+   bool emit_endif = true;
+
+   /* In single program flow mode, we can express IF and ELSE instructions
+    * equivalently as ADD instructions that operate on IP.  On platforms prior
+    * to Gen6, flow control instructions cause an implied thread switch, so
+    * this is a significant savings.
+    *
+    * However, on Gen6, writing to IP doesn't work in single program flow mode
+    * (see the SandyBridge PRM, Volume 4 part 2, p79: "When SPF is ON, IP may
+    * not be updated by non-flow control instructions.").  And on later
+    * platforms, there is no significant benefit to converting control flow
+    * instructions to conditional ADDs.  So we only do this trick on Gen4 and
+    * Gen5.
+    */
+   if (intel->gen < 6 && p->single_program_flow)
+      emit_endif = false;
+
+   /*
+    * A single next_insn() may change the base adress of instruction store
+    * memory(p->store), so call it first before referencing the instruction
+    * store pointer from an index
+    */
+   if (emit_endif)
+      insn = next_insn(p, BRW_OPCODE_ENDIF);
 
    /* Pop the IF and (optional) ELSE instructions from the stack */
-   p->if_stack_depth--;
-   if (p->if_stack[p->if_stack_depth]->header.opcode == BRW_OPCODE_ELSE) {
-      else_inst = p->if_stack[p->if_stack_depth];
-      p->if_stack_depth--;
+   p->if_depth_in_loop[p->loop_stack_depth]--;
+   tmp = pop_if_stack(p);
+   if (tmp->header.opcode == BRW_OPCODE_ELSE) {
+      else_inst = tmp;
+      tmp = pop_if_stack(p);
    }
-   if_inst = p->if_stack[p->if_stack_depth];
+   if_inst = tmp;
 
-   if (p->single_program_flow) {
+   if (!emit_endif) {
       /* ENDIF is useless; don't bother emitting it. */
       convert_IF_ELSE_to_ADD(p, if_inst, else_inst);
       return;
    }
 
-   insn = next_insn(p, BRW_OPCODE_ENDIF);
-
    if (intel->gen < 6) {
       brw_set_dest(p, insn, retype(brw_vec4_grf(0,0), BRW_REGISTER_TYPE_UD));
       brw_set_src0(p, insn, retype(brw_vec4_grf(0,0), BRW_REGISTER_TYPE_UD));
@@ -1249,7 +1457,7 @@ brw_ENDIF(struct brw_compile *p)
    patch_IF_ELSE(p, if_inst, else_inst, insn);
 }
 
-struct brw_instruction *brw_BREAK(struct brw_compile *p, int pop_count)
+struct brw_instruction *brw_BREAK(struct brw_compile *p)
 {
    struct intel_context *intel = &p->brw->intel;
    struct brw_instruction *insn;
@@ -1264,7 +1472,7 @@ struct brw_instruction *brw_BREAK(struct brw_compile *p, int pop_count)
       brw_set_src0(p, insn, brw_ip_reg());
       brw_set_src1(p, insn, brw_imm_d(0x0));
       insn->bits3.if_else.pad0 = 0;
-      insn->bits3.if_else.pop_count = pop_count;
+      insn->bits3.if_else.pop_count = p->if_depth_in_loop[p->loop_stack_depth];
    }
    insn->header.compression_control = BRW_COMPRESSION_NONE;
    insn->header.execution_size = BRW_EXECUTE_8;
@@ -1272,8 +1480,7 @@ struct brw_instruction *brw_BREAK(struct brw_compile *p, int pop_count)
    return insn;
 }
 
-struct brw_instruction *gen6_CONT(struct brw_compile *p,
-                                 struct brw_instruction *do_insn)
+struct brw_instruction *gen6_CONT(struct brw_compile *p)
 {
    struct brw_instruction *insn;
 
@@ -1289,7 +1496,7 @@ struct brw_instruction *gen6_CONT(struct brw_compile *p,
    return insn;
 }
 
-struct brw_instruction *brw_CONT(struct brw_compile *p, int pop_count)
+struct brw_instruction *brw_CONT(struct brw_compile *p)
 {
    struct brw_instruction *insn;
    insn = next_insn(p, BRW_OPCODE_CONTINUE);
@@ -1300,7 +1507,25 @@ struct brw_instruction *brw_CONT(struct brw_compile *p, int pop_count)
    insn->header.execution_size = BRW_EXECUTE_8;
    /* insn->header.mask_control = BRW_MASK_DISABLE; */
    insn->bits3.if_else.pad0 = 0;
-   insn->bits3.if_else.pop_count = pop_count;
+   insn->bits3.if_else.pop_count = p->if_depth_in_loop[p->loop_stack_depth];
+   return insn;
+}
+
+struct brw_instruction *gen6_HALT(struct brw_compile *p)
+{
+   struct brw_instruction *insn;
+
+   insn = next_insn(p, BRW_OPCODE_HALT);
+   brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
+   brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
+   brw_set_src1(p, insn, brw_imm_d(0x0)); /* UIP and JIP, updated later. */
+
+   if (p->compressed) {
+      insn->header.execution_size = BRW_EXECUTE_16;
+   } else {
+      insn->header.compression_control = BRW_COMPRESSION_NONE;
+      insn->header.execution_size = BRW_EXECUTE_8;
+   }
    return insn;
 }
 
@@ -1325,10 +1550,13 @@ struct brw_instruction *brw_DO(struct brw_compile *p, GLuint execute_size)
    struct intel_context *intel = &p->brw->intel;
 
    if (intel->gen >= 6 || p->single_program_flow) {
+      push_loop_stack(p, &p->store[p->nr_insn]);
       return &p->store[p->nr_insn];
    } else {
       struct brw_instruction *insn = next_insn(p, BRW_OPCODE_DO);
 
+      push_loop_stack(p, insn);
+
       /* Override the defaults for this instruction:
        */
       brw_set_dest(p, insn, brw_null_reg());
@@ -1345,13 +1573,40 @@ struct brw_instruction *brw_DO(struct brw_compile *p, GLuint execute_size)
    }
 }
 
+/**
+ * For pre-gen6, we patch BREAK/CONT instructions to point at the WHILE
+ * instruction here.
+ *
+ * For gen6+, see brw_set_uip_jip(), which doesn't care so much about the loop
+ * nesting, since it can always just point to the end of the block/current loop.
+ */
+static void
+brw_patch_break_cont(struct brw_compile *p, struct brw_instruction *while_inst)
+{
+   struct intel_context *intel = &p->brw->intel;
+   struct brw_instruction *do_inst = get_inner_do_insn(p);
+   struct brw_instruction *inst;
+   int br = (intel->gen == 5) ? 2 : 1;
+
+   for (inst = while_inst - 1; inst != do_inst; inst--) {
+      /* If the jump count is != 0, that means that this instruction has already
+       * been patched because it's part of a loop inside of the one we're
+       * patching.
+       */
+      if (inst->header.opcode == BRW_OPCODE_BREAK &&
+         inst->bits3.if_else.jump_count == 0) {
+        inst->bits3.if_else.jump_count = br * ((while_inst - inst) + 1);
+      } else if (inst->header.opcode == BRW_OPCODE_CONTINUE &&
+                inst->bits3.if_else.jump_count == 0) {
+        inst->bits3.if_else.jump_count = br * (while_inst - inst);
+      }
+   }
+}
 
-
-struct brw_instruction *brw_WHILE(struct brw_compile *p, 
-                                  struct brw_instruction *do_insn)
+struct brw_instruction *brw_WHILE(struct brw_compile *p)
 {
    struct intel_context *intel = &p->brw->intel;
-   struct brw_instruction *insn;
+   struct brw_instruction *insn, *do_insn;
    GLuint br = 1;
 
    if (intel->gen >= 5)
@@ -1359,6 +1614,7 @@ struct brw_instruction *brw_WHILE(struct brw_compile *p,
 
    if (intel->gen >= 7) {
       insn = next_insn(p, BRW_OPCODE_WHILE);
+      do_insn = get_inner_do_insn(p);
 
       brw_set_dest(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
       brw_set_src0(p, insn, retype(brw_null_reg(), BRW_REGISTER_TYPE_D));
@@ -1368,6 +1624,7 @@ struct brw_instruction *brw_WHILE(struct brw_compile *p,
       insn->header.execution_size = BRW_EXECUTE_8;
    } else if (intel->gen == 6) {
       insn = next_insn(p, BRW_OPCODE_WHILE);
+      do_insn = get_inner_do_insn(p);
 
       brw_set_dest(p, insn, brw_imm_w(0));
       insn->bits1.branch_gen6.jump_count = br * (do_insn - insn);
@@ -1378,6 +1635,7 @@ struct brw_instruction *brw_WHILE(struct brw_compile *p,
    } else {
       if (p->single_program_flow) {
         insn = next_insn(p, BRW_OPCODE_ADD);
+         do_insn = get_inner_do_insn(p);
 
         brw_set_dest(p, insn, brw_ip_reg());
         brw_set_src0(p, insn, brw_ip_reg());
@@ -1385,6 +1643,7 @@ struct brw_instruction *brw_WHILE(struct brw_compile *p,
         insn->header.execution_size = BRW_EXECUTE_1;
       } else {
         insn = next_insn(p, BRW_OPCODE_WHILE);
+         do_insn = get_inner_do_insn(p);
 
         assert(do_insn->header.opcode == BRW_OPCODE_DO);
 
@@ -1396,22 +1655,25 @@ struct brw_instruction *brw_WHILE(struct brw_compile *p,
         insn->bits3.if_else.jump_count = br * (do_insn - insn + 1);
         insn->bits3.if_else.pop_count = 0;
         insn->bits3.if_else.pad0 = 0;
+
+        brw_patch_break_cont(p, insn);
       }
    }
    insn->header.compression_control = BRW_COMPRESSION_NONE;
    p->current->header.predicate_control = BRW_PREDICATE_NONE;
 
+   p->loop_stack_depth--;
+
    return insn;
 }
 
 
 /* FORWARD JUMPS:
  */
-void brw_land_fwd_jump(struct brw_compile *p, 
-                      struct brw_instruction *jmp_insn)
+void brw_land_fwd_jump(struct brw_compile *p, int jmp_insn_idx)
 {
    struct intel_context *intel = &p->brw->intel;
-   struct brw_instruction *landing = &p->store[p->nr_insn];
+   struct brw_instruction *jmp_insn = &p->store[jmp_insn_idx];
    GLuint jmpi = 1;
 
    if (intel->gen >= 5)
@@ -1420,7 +1682,7 @@ void brw_land_fwd_jump(struct brw_compile *p,
    assert(jmp_insn->header.opcode == BRW_OPCODE_JMPI);
    assert(jmp_insn->bits1.da1.src1_reg_file == BRW_IMMEDIATE_VALUE);
 
-   jmp_insn->bits3.ud = jmpi * ((landing - jmp_insn) - 1);
+   jmp_insn->bits3.ud = jmpi * (p->nr_insn - jmp_insn_idx - 1);
 }
 
 
@@ -1435,6 +1697,7 @@ void brw_CMP(struct brw_compile *p,
             struct brw_reg src0,
             struct brw_reg src1)
 {
+   struct intel_context *intel = &p->brw->intel;
    struct brw_instruction *insn = next_insn(p, BRW_OPCODE_CMP);
 
    insn->header.destreg__conditionalmod = conditional;
@@ -1454,6 +1717,20 @@ void brw_CMP(struct brw_compile *p,
       p->current->header.predicate_control = BRW_PREDICATE_NORMAL;
       p->flag_value = 0xff;
    }
+
+   /* Item WaCMPInstNullDstForcesThreadSwitch in the Haswell Bspec workarounds
+    * page says:
+    *    "Any CMP instruction with a null destination must use a {switch}."
+    *
+    * It also applies to other Gen7 platforms (IVB, BYT) even though it isn't
+    * mentioned on their work-arounds pages.
+    */
+   if (intel->gen == 7) {
+      if (dest.file == BRW_ARCHITECTURE_REGISTER_FILE &&
+          dest.nr == BRW_ARF_NULL) {
+         insn->header.thread_control = BRW_THREAD_SWITCH;
+      }
+   }
 }
 
 /* Issue 'wait' instruction for n1, host could program MMIO
@@ -1481,7 +1758,6 @@ void brw_WAIT (struct brw_compile *p)
 void brw_math( struct brw_compile *p,
               struct brw_reg dest,
               GLuint function,
-              GLuint saturate,
               GLuint msg_reg_nr,
               struct brw_reg src,
               GLuint data_type,
@@ -1492,15 +1768,19 @@ void brw_math( struct brw_compile *p,
    if (intel->gen >= 6) {
       struct brw_instruction *insn = next_insn(p, BRW_OPCODE_MATH);
 
-      assert(dest.file == BRW_GENERAL_REGISTER_FILE);
+      assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
+             (intel->gen >= 7 && dest.file == BRW_MESSAGE_REGISTER_FILE));
       assert(src.file == BRW_GENERAL_REGISTER_FILE);
 
       assert(dest.hstride == BRW_HORIZONTAL_STRIDE_1);
-      assert(src.hstride == BRW_HORIZONTAL_STRIDE_1);
+      if (intel->gen == 6)
+        assert(src.hstride == BRW_HORIZONTAL_STRIDE_1);
 
-      /* Source modifiers are ignored for extended math instructions. */
-      assert(!src.negate);
-      assert(!src.abs);
+      /* Source modifiers are ignored for extended math instructions on Gen6. */
+      if (intel->gen == 6) {
+        assert(!src.negate);
+        assert(!src.abs);
+      }
 
       if (function == BRW_MATH_FUNCTION_INT_DIV_QUOTIENT ||
          function == BRW_MATH_FUNCTION_INT_DIV_REMAINDER ||
@@ -1514,7 +1794,6 @@ void brw_math( struct brw_compile *p,
        * becomes FC[3:0] and ThreadCtrl becomes FC[5:4].
        */
       insn->header.destreg__conditionalmod = function;
-      insn->header.saturate = saturate;
 
       brw_set_dest(p, insn, dest);
       brw_set_src0(p, insn, src);
@@ -1535,7 +1814,6 @@ void brw_math( struct brw_compile *p,
                           function,
                           src.type == BRW_REGISTER_TYPE_D,
                           precision,
-                          saturate,
                           data_type);
    }
 }
@@ -1555,13 +1833,16 @@ void brw_math2(struct brw_compile *p,
    (void) intel;
 
 
-   assert(dest.file == BRW_GENERAL_REGISTER_FILE);
+   assert(dest.file == BRW_GENERAL_REGISTER_FILE ||
+          (intel->gen >= 7 && dest.file == BRW_MESSAGE_REGISTER_FILE));
    assert(src0.file == BRW_GENERAL_REGISTER_FILE);
    assert(src1.file == BRW_GENERAL_REGISTER_FILE);
 
    assert(dest.hstride == BRW_HORIZONTAL_STRIDE_1);
-   assert(src0.hstride == BRW_HORIZONTAL_STRIDE_1);
-   assert(src1.hstride == BRW_HORIZONTAL_STRIDE_1);
+   if (intel->gen == 6) {
+      assert(src0.hstride == BRW_HORIZONTAL_STRIDE_1);
+      assert(src1.hstride == BRW_HORIZONTAL_STRIDE_1);
+   }
 
    if (function == BRW_MATH_FUNCTION_INT_DIV_QUOTIENT ||
        function == BRW_MATH_FUNCTION_INT_DIV_REMAINDER ||
@@ -1573,11 +1854,13 @@ void brw_math2(struct brw_compile *p,
       assert(src1.type == BRW_REGISTER_TYPE_F);
    }
 
-   /* Source modifiers are ignored for extended math instructions. */
-   assert(!src0.negate);
-   assert(!src0.abs);
-   assert(!src1.negate);
-   assert(!src1.abs);
+   /* Source modifiers are ignored for extended math instructions on Gen6. */
+   if (intel->gen == 6) {
+      assert(!src0.negate);
+      assert(!src0.abs);
+      assert(!src1.negate);
+      assert(!src1.abs);
+   }
 
    /* Math is the same ISA format as other opcodes, except that CondModifier
     * becomes FC[3:0] and ThreadCtrl becomes FC[5:4].
@@ -1589,78 +1872,6 @@ void brw_math2(struct brw_compile *p,
    brw_set_src1(p, insn, src1);
 }
 
-/**
- * Extended math function, float[16].
- * Use 2 send instructions.
- */
-void brw_math_16( struct brw_compile *p,
-                 struct brw_reg dest,
-                 GLuint function,
-                 GLuint saturate,
-                 GLuint msg_reg_nr,
-                 struct brw_reg src,
-                 GLuint precision )
-{
-   struct intel_context *intel = &p->brw->intel;
-   struct brw_instruction *insn;
-
-   if (intel->gen >= 6) {
-      insn = next_insn(p, BRW_OPCODE_MATH);
-
-      /* Math is the same ISA format as other opcodes, except that CondModifier
-       * becomes FC[3:0] and ThreadCtrl becomes FC[5:4].
-       */
-      insn->header.destreg__conditionalmod = function;
-      insn->header.saturate = saturate;
-
-      /* Source modifiers are ignored for extended math instructions. */
-      assert(!src.negate);
-      assert(!src.abs);
-
-      brw_set_dest(p, insn, dest);
-      brw_set_src0(p, insn, src);
-      brw_set_src1(p, insn, brw_null_reg());
-      return;
-   }
-
-   /* First instruction:
-    */
-   brw_push_insn_state(p);
-   brw_set_predicate_control_flag_value(p, 0xff);
-   brw_set_compression_control(p, BRW_COMPRESSION_NONE);
-
-   insn = next_insn(p, BRW_OPCODE_SEND);
-   insn->header.destreg__conditionalmod = msg_reg_nr;
-
-   brw_set_dest(p, insn, dest);
-   brw_set_src0(p, insn, src);
-   brw_set_math_message(p,
-                       insn, 
-                       function,
-                       BRW_MATH_INTEGER_UNSIGNED,
-                       precision,
-                       saturate,
-                       BRW_MATH_DATA_VECTOR);
-
-   /* Second instruction:
-    */
-   insn = next_insn(p, BRW_OPCODE_SEND);
-   insn->header.compression_control = BRW_COMPRESSION_2NDHALF;
-   insn->header.destreg__conditionalmod = msg_reg_nr+1;
-
-   brw_set_dest(p, insn, offset(dest,1));
-   brw_set_src0(p, insn, src);
-   brw_set_math_message(p, 
-                       insn, 
-                       function,
-                       BRW_MATH_INTEGER_UNSIGNED,
-                       precision,
-                       saturate,
-                       BRW_MATH_DATA_VECTOR);
-
-   brw_pop_insn_state(p);
-}
-
 
 /**
  * Write a block of OWORDs (half a GRF each) from the scratch buffer,
@@ -1763,8 +1974,8 @@ void brw_oword_block_write_scratch(struct brw_compile *p,
                               msg_control,
                               msg_type,
                               mlen,
-                              GL_TRUE, /* header_present */
-                              0, /* pixel scoreboard */
+                              true, /* header_present */
+                              0, /* not a render target */
                               send_commit_msg, /* response_length */
                               0, /* eot */
                               send_commit_msg);
@@ -1842,6 +2053,7 @@ brw_oword_block_read_scratch(struct brw_compile *p,
                              BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ, /* msg_type */
                              BRW_DATAPORT_READ_TARGET_RENDER_CACHE,
                              1, /* msg_length */
+                              true, /* header_present */
                              rlen);
    }
 }
@@ -1899,175 +2111,27 @@ void brw_oword_block_read(struct brw_compile *p,
                           BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ,
                           BRW_DATAPORT_READ_TARGET_DATA_CACHE,
                           1, /* msg_length */
+                           true, /* header_present */
                           1); /* response_length (1 reg, 2 owords!) */
 
    brw_pop_insn_state(p);
 }
 
-/**
- * Read a set of dwords from the data port Data Cache (const buffer).
- *
- * Location (in buffer) appears as UD offsets in the register after
- * the provided mrf header reg.
- */
-void brw_dword_scattered_read(struct brw_compile *p,
-                             struct brw_reg dest,
-                             struct brw_reg mrf,
-                             uint32_t bind_table_index)
-{
-   mrf = retype(mrf, BRW_REGISTER_TYPE_UD);
-
-   brw_push_insn_state(p);
-   brw_set_predicate_control(p, BRW_PREDICATE_NONE);
-   brw_set_compression_control(p, BRW_COMPRESSION_NONE);
-   brw_set_mask_control(p, BRW_MASK_DISABLE);
-   brw_MOV(p, mrf, retype(brw_vec8_grf(0, 0), BRW_REGISTER_TYPE_UD));
-   brw_pop_insn_state(p);
-
-   struct brw_instruction *insn = next_insn(p, BRW_OPCODE_SEND);
-   insn->header.destreg__conditionalmod = mrf.nr;
-
-   /* cast dest to a uword[8] vector */
-   dest = retype(vec8(dest), BRW_REGISTER_TYPE_UW);
-
-   brw_set_dest(p, insn, dest);
-   brw_set_src0(p, insn, brw_null_reg());
-
-   brw_set_dp_read_message(p,
-                          insn,
-                          bind_table_index,
-                          BRW_DATAPORT_DWORD_SCATTERED_BLOCK_8DWORDS,
-                          BRW_DATAPORT_READ_MESSAGE_DWORD_SCATTERED_READ,
-                          BRW_DATAPORT_READ_TARGET_DATA_CACHE,
-                          2, /* msg_length */
-                          1); /* response_length */
-}
-
-
-
-/**
- * Read float[4] constant(s) from VS constant buffer.
- * For relative addressing, two float[4] constants will be read into 'dest'.
- * Otherwise, one float[4] constant will be read into the lower half of 'dest'.
- */
-void brw_dp_READ_4_vs(struct brw_compile *p,
-                      struct brw_reg dest,
-                      GLuint location,
-                      GLuint bind_table_index)
-{
-   struct intel_context *intel = &p->brw->intel;
-   struct brw_instruction *insn;
-   GLuint msg_reg_nr = 1;
-
-   if (intel->gen >= 6)
-      location /= 16;
-
-   /* Setup MRF[1] with location/offset into const buffer */
-   brw_push_insn_state(p);
-   brw_set_access_mode(p, BRW_ALIGN_1);
-   brw_set_compression_control(p, BRW_COMPRESSION_NONE);
-   brw_set_mask_control(p, BRW_MASK_DISABLE);
-   brw_set_predicate_control(p, BRW_PREDICATE_NONE);
-   brw_MOV(p, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, msg_reg_nr, 2),
-                    BRW_REGISTER_TYPE_UD),
-          brw_imm_ud(location));
-   brw_pop_insn_state(p);
-
-   insn = next_insn(p, BRW_OPCODE_SEND);
-
-   insn->header.predicate_control = BRW_PREDICATE_NONE;
-   insn->header.compression_control = BRW_COMPRESSION_NONE;
-   insn->header.destreg__conditionalmod = msg_reg_nr;
-   insn->header.mask_control = BRW_MASK_DISABLE;
-
-   brw_set_dest(p, insn, dest);
-   if (intel->gen >= 6) {
-      brw_set_src0(p, insn, brw_message_reg(msg_reg_nr));
-   } else {
-      brw_set_src0(p, insn, brw_null_reg());
-   }
-
-   brw_set_dp_read_message(p,
-                          insn,
-                          bind_table_index,
-                          0,
-                          BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ, /* msg_type */
-                          BRW_DATAPORT_READ_TARGET_DATA_CACHE,
-                          1, /* msg_length */
-                          1); /* response_length (1 Oword) */
-}
-
-/**
- * Read a float[4] constant per vertex from VS constant buffer, with
- * relative addressing.
- */
-void brw_dp_READ_4_vs_relative(struct brw_compile *p,
-                              struct brw_reg dest,
-                              struct brw_reg addr_reg,
-                              GLuint offset,
-                              GLuint bind_table_index)
-{
-   struct intel_context *intel = &p->brw->intel;
-   struct brw_reg src = brw_vec8_grf(0, 0);
-   int msg_type;
-
-   /* Setup MRF[1] with offset into const buffer */
-   brw_push_insn_state(p);
-   brw_set_access_mode(p, BRW_ALIGN_1);
-   brw_set_compression_control(p, BRW_COMPRESSION_NONE);
-   brw_set_mask_control(p, BRW_MASK_DISABLE);
-   brw_set_predicate_control(p, BRW_PREDICATE_NONE);
-
-   /* M1.0 is block offset 0, M1.4 is block offset 1, all other
-    * fields ignored.
-    */
-   brw_ADD(p, retype(brw_message_reg(1), BRW_REGISTER_TYPE_D),
-          addr_reg, brw_imm_d(offset));
-   brw_pop_insn_state(p);
-
-   gen6_resolve_implied_move(p, &src, 0);
-   struct brw_instruction *insn = next_insn(p, BRW_OPCODE_SEND);
-
-   insn->header.predicate_control = BRW_PREDICATE_NONE;
-   insn->header.compression_control = BRW_COMPRESSION_NONE;
-   insn->header.destreg__conditionalmod = 0;
-   insn->header.mask_control = BRW_MASK_DISABLE;
-
-   brw_set_dest(p, insn, dest);
-   brw_set_src0(p, insn, src);
-
-   if (intel->gen >= 6)
-      msg_type = GEN6_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
-   else if (intel->gen == 5 || intel->is_g4x)
-      msg_type = G45_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
-   else
-      msg_type = BRW_DATAPORT_READ_MESSAGE_OWORD_DUAL_BLOCK_READ;
-
-   brw_set_dp_read_message(p,
-                          insn,
-                          bind_table_index,
-                          BRW_DATAPORT_OWORD_DUAL_BLOCK_1OWORD,
-                          msg_type,
-                          BRW_DATAPORT_READ_TARGET_DATA_CACHE,
-                          2, /* msg_length */
-                          1); /* response_length */
-}
-
-
 
 void brw_fb_WRITE(struct brw_compile *p,
                  int dispatch_width,
                   GLuint msg_reg_nr,
                   struct brw_reg src0,
+                  GLuint msg_control,
                   GLuint binding_table_index,
                   GLuint msg_length,
                   GLuint response_length,
-                  GLboolean eot,
-                  GLboolean header_present)
+                  bool eot,
+                  bool header_present)
 {
    struct intel_context *intel = &p->brw->intel;
    struct brw_instruction *insn;
-   GLuint msg_control, msg_type;
+   GLuint msg_type;
    struct brw_reg dest;
 
    if (dispatch_width == 16)
@@ -2075,7 +2139,7 @@ void brw_fb_WRITE(struct brw_compile *p,
    else
       dest = retype(vec8(brw_null_reg()), BRW_REGISTER_TYPE_UW);
 
-   if (intel->gen >= 6 && binding_table_index == 0) {
+   if (intel->gen >= 6) {
       insn = next_insn(p, BRW_OPCODE_SENDC);
    } else {
       insn = next_insn(p, BRW_OPCODE_SEND);
@@ -2095,11 +2159,6 @@ void brw_fb_WRITE(struct brw_compile *p,
       msg_type = BRW_DATAPORT_WRITE_MESSAGE_RENDER_TARGET_WRITE;
    }
 
-   if (dispatch_width == 16)
-      msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD16_SINGLE_SOURCE;
-   else
-      msg_control = BRW_DATAPORT_RENDER_TARGET_WRITE_SIMD8_SINGLE_SOURCE_SUBSPAN01;
-
    brw_set_dest(p, insn, dest);
    brw_set_src0(p, insn, src0);
    brw_set_dp_write_message(p,
@@ -2109,7 +2168,7 @@ void brw_fb_WRITE(struct brw_compile *p,
                            msg_type,
                            msg_length,
                            header_present,
-                           1,  /* pixel scoreboard */
+                           eot, /* last render target write */
                            response_length,
                            eot,
                            0 /* send_commit_msg */);
@@ -2127,122 +2186,35 @@ void brw_SAMPLE(struct brw_compile *p,
                struct brw_reg src0,
                GLuint binding_table_index,
                GLuint sampler,
-               GLuint writemask,
                GLuint msg_type,
                GLuint response_length,
                GLuint msg_length,
-               GLboolean eot,
                GLuint header_present,
-               GLuint simd_mode)
+               GLuint simd_mode,
+               GLuint return_format)
 {
    struct intel_context *intel = &p->brw->intel;
-   GLboolean need_stall = 0;
-
-   if (writemask == 0) {
-      /*printf("%s: zero writemask??\n", __FUNCTION__); */
-      return;
-   }
-   
-   /* Hardware doesn't do destination dependency checking on send
-    * instructions properly.  Add a workaround which generates the
-    * dependency by other means.  In practice it seems like this bug
-    * only crops up for texture samples, and only where registers are
-    * written by the send and then written again later without being
-    * read in between.  Luckily for us, we already track that
-    * information and use it to modify the writemask for the
-    * instruction, so that is a guide for whether a workaround is
-    * needed.
-    */
-   if (writemask != WRITEMASK_XYZW) {
-      GLuint dst_offset = 0;
-      GLuint i, newmask = 0, len = 0;
-
-      for (i = 0; i < 4; i++) {
-        if (writemask & (1<<i))
-           break;
-        dst_offset += 2;
-      }
-      for (; i < 4; i++) {
-        if (!(writemask & (1<<i)))
-           break;
-        newmask |= 1<<i;
-        len++;
-      }
-
-      if (newmask != writemask) {
-        need_stall = 1;
-         /* printf("need stall %x %x\n", newmask , writemask); */
-      }
-      else {
-        GLboolean dispatch_16 = GL_FALSE;
-
-        struct brw_reg m1 = brw_message_reg(msg_reg_nr);
-
-        guess_execution_size(p, p->current, dest);
-        if (p->current->header.execution_size == BRW_EXECUTE_16)
-           dispatch_16 = GL_TRUE;
-
-        newmask = ~newmask & WRITEMASK_XYZW;
-
-        brw_push_insn_state(p);
-
-        brw_set_compression_control(p, BRW_COMPRESSION_NONE);
-        brw_set_mask_control(p, BRW_MASK_DISABLE);
-
-        brw_MOV(p, retype(m1, BRW_REGISTER_TYPE_UD),
-                retype(brw_vec8_grf(0,0), BRW_REGISTER_TYPE_UD));
-        brw_MOV(p, get_element_ud(m1, 2), brw_imm_ud(newmask << 12)); 
-
-        brw_pop_insn_state(p);
-
-        src0 = retype(brw_null_reg(), BRW_REGISTER_TYPE_UW); 
-        dest = offset(dest, dst_offset);
-
-        /* For 16-wide dispatch, masked channels are skipped in the
-         * response.  For 8-wide, masked channels still take up slots,
-         * and are just not written to.
-         */
-        if (dispatch_16)
-           response_length = len * 2;
-      }
-   }
-
-   {
-      struct brw_instruction *insn;
-   
-      gen6_resolve_implied_move(p, &src0, msg_reg_nr);
+   struct brw_instruction *insn;
 
-      insn = next_insn(p, BRW_OPCODE_SEND);
-      insn->header.predicate_control = 0; /* XXX */
-      insn->header.compression_control = BRW_COMPRESSION_NONE;
-      if (intel->gen < 6)
-         insn->header.destreg__conditionalmod = msg_reg_nr;
+   gen6_resolve_implied_move(p, &src0, msg_reg_nr);
 
-      brw_set_dest(p, insn, dest);
-      brw_set_src0(p, insn, src0);
-      brw_set_sampler_message(p, insn,
-                             binding_table_index,
-                             sampler,
-                             msg_type,
-                             response_length, 
-                             msg_length,
-                             eot,
-                             header_present,
-                             simd_mode);
-   }
-
-   if (need_stall) {
-      struct brw_reg reg = vec8(offset(dest, response_length-1));
-
-      /*  mov (8) r9.0<1>:f    r9.0<8;8,1>:f    { Align1 }
-       */
-      brw_push_insn_state(p);
-      brw_set_compression_control(p, BRW_COMPRESSION_NONE);
-      brw_MOV(p, retype(reg, BRW_REGISTER_TYPE_UD),
-             retype(reg, BRW_REGISTER_TYPE_UD));
-      brw_pop_insn_state(p);
-   }
+   insn = next_insn(p, BRW_OPCODE_SEND);
+   insn->header.predicate_control = 0; /* XXX */
+   insn->header.compression_control = BRW_COMPRESSION_NONE;
+   if (intel->gen < 6)
+      insn->header.destreg__conditionalmod = msg_reg_nr;
 
+   brw_set_dest(p, insn, dest);
+   brw_set_src0(p, insn, src0);
+   brw_set_sampler_message(p, insn,
+                           binding_table_index,
+                           sampler,
+                           msg_type,
+                           response_length,
+                           msg_length,
+                           header_present,
+                           simd_mode,
+                           return_format);
 }
 
 /* All these variables are pretty confusing - we might be better off
@@ -2253,12 +2225,12 @@ void brw_urb_WRITE(struct brw_compile *p,
                   struct brw_reg dest,
                   GLuint msg_reg_nr,
                   struct brw_reg src0,
-                  GLboolean allocate,
-                  GLboolean used,
+                  bool allocate,
+                  bool used,
                   GLuint msg_length,
                   GLuint response_length,
-                  GLboolean eot,
-                  GLboolean writes_complete,
+                  bool eot,
+                  bool writes_complete,
                   GLuint offset,
                   GLuint swizzle)
 {
@@ -2271,6 +2243,7 @@ void brw_urb_WRITE(struct brw_compile *p,
       /* Enable Channel Masks in the URB_WRITE_HWORD message header */
       brw_push_insn_state(p);
       brw_set_access_mode(p, BRW_ALIGN_1);
+      brw_set_mask_control(p, BRW_MASK_DISABLE);
       brw_OR(p, retype(brw_vec1_reg(BRW_MESSAGE_REGISTER_FILE, msg_reg_nr, 5),
                       BRW_REGISTER_TYPE_UD),
                retype(brw_vec1_grf(0, 5), BRW_REGISTER_TYPE_UD),
@@ -2301,23 +2274,36 @@ void brw_urb_WRITE(struct brw_compile *p,
                       swizzle);
 }
 
+static int
+next_ip(struct brw_compile *p, int ip)
+{
+   struct brw_instruction *insn = (void *)p->store + ip;
+
+   if (insn->header.cmpt_control)
+      return ip + 8;
+   else
+      return ip + 16;
+}
+
 static int
 brw_find_next_block_end(struct brw_compile *p, int start)
 {
    int ip;
+   void *store = p->store;
 
-   for (ip = start + 1; ip < p->nr_insn; ip++) {
-      struct brw_instruction *insn = &p->store[ip];
+   for (ip = next_ip(p, start); ip < p->next_insn_offset; ip = next_ip(p, ip)) {
+      struct brw_instruction *insn = store + ip;
 
       switch (insn->header.opcode) {
       case BRW_OPCODE_ENDIF:
       case BRW_OPCODE_ELSE:
       case BRW_OPCODE_WHILE:
+      case BRW_OPCODE_HALT:
         return ip;
       }
    }
-   assert(!"not reached");
-   return start + 1;
+
+   return 0;
 }
 
 /* There is no DO instruction on gen6, so to find the end of the loop
@@ -2329,49 +2315,95 @@ brw_find_loop_end(struct brw_compile *p, int start)
 {
    struct intel_context *intel = &p->brw->intel;
    int ip;
-   int br = 2;
+   int scale = 8;
+   void *store = p->store;
 
-   for (ip = start + 1; ip < p->nr_insn; ip++) {
-      struct brw_instruction *insn = &p->store[ip];
+   /* Always start after the instruction (such as a WHILE) we're trying to fix
+    * up.
+    */
+   for (ip = next_ip(p, start); ip < p->next_insn_offset; ip = next_ip(p, ip)) {
+      struct brw_instruction *insn = store + ip;
 
       if (insn->header.opcode == BRW_OPCODE_WHILE) {
         int jip = intel->gen == 6 ? insn->bits1.branch_gen6.jump_count
                                   : insn->bits3.break_cont.jip;
-        if (ip + jip / br <= start)
+        if (ip + jip * scale <= start)
            return ip;
       }
    }
    assert(!"not reached");
-   return start + 1;
+   return start;
 }
 
 /* After program generation, go back and update the UIP and JIP of
- * BREAK and CONT instructions to their correct locations.
+ * BREAK, CONT, and HALT instructions to their correct locations.
  */
 void
 brw_set_uip_jip(struct brw_compile *p)
 {
    struct intel_context *intel = &p->brw->intel;
    int ip;
-   int br = 2;
+   int scale = 8;
+   void *store = p->store;
 
    if (intel->gen < 6)
       return;
 
-   for (ip = 0; ip < p->nr_insn; ip++) {
-      struct brw_instruction *insn = &p->store[ip];
+   for (ip = 0; ip < p->next_insn_offset; ip = next_ip(p, ip)) {
+      struct brw_instruction *insn = store + ip;
 
+      if (insn->header.cmpt_control) {
+        /* Fixups for compacted BREAK/CONTINUE not supported yet. */
+        assert(insn->header.opcode != BRW_OPCODE_BREAK &&
+               insn->header.opcode != BRW_OPCODE_CONTINUE &&
+               insn->header.opcode != BRW_OPCODE_HALT);
+        continue;
+      }
+
+      int block_end_ip = brw_find_next_block_end(p, ip);
       switch (insn->header.opcode) {
       case BRW_OPCODE_BREAK:
-        insn->bits3.break_cont.jip = br * (brw_find_next_block_end(p, ip) - ip);
+         assert(block_end_ip != 0);
+        insn->bits3.break_cont.jip = (block_end_ip - ip) / scale;
         /* Gen7 UIP points to WHILE; Gen6 points just after it */
         insn->bits3.break_cont.uip =
-           br * (brw_find_loop_end(p, ip) - ip + (intel->gen == 6 ? 1 : 0));
+           (brw_find_loop_end(p, ip) - ip +
+             (intel->gen == 6 ? 16 : 0)) / scale;
         break;
       case BRW_OPCODE_CONTINUE:
-        insn->bits3.break_cont.jip = br * (brw_find_next_block_end(p, ip) - ip);
-        insn->bits3.break_cont.uip = br * (brw_find_loop_end(p, ip) - ip);
+         assert(block_end_ip != 0);
+        insn->bits3.break_cont.jip = (block_end_ip - ip) / scale;
+        insn->bits3.break_cont.uip =
+            (brw_find_loop_end(p, ip) - ip) / scale;
+
+        assert(insn->bits3.break_cont.uip != 0);
+        assert(insn->bits3.break_cont.jip != 0);
+        break;
+
+      case BRW_OPCODE_ENDIF:
+         if (block_end_ip == 0)
+            insn->bits3.break_cont.jip = 2;
+         else
+            insn->bits3.break_cont.jip = (block_end_ip - ip) / scale;
+        break;
 
+      case BRW_OPCODE_HALT:
+        /* From the Sandy Bridge PRM (volume 4, part 2, section 8.3.19):
+         *
+         *    "In case of the halt instruction not inside any conditional
+         *     code block, the value of <JIP> and <UIP> should be the
+         *     same. In case of the halt instruction inside conditional code
+         *     block, the <UIP> should be the end of the program, and the
+         *     <JIP> should be end of the most inner conditional code block."
+         *
+         * The uip will have already been set by whoever set up the
+         * instruction.
+         */
+        if (block_end_ip == 0) {
+           insn->bits3.break_cont.jip = insn->bits3.break_cont.uip;
+        } else {
+           insn->bits3.break_cont.jip = (block_end_ip - ip) / scale;
+        }
         assert(insn->bits3.break_cont.uip != 0);
         assert(insn->bits3.break_cont.jip != 0);
         break;
@@ -2383,9 +2415,9 @@ void brw_ff_sync(struct brw_compile *p,
                   struct brw_reg dest,
                   GLuint msg_reg_nr,
                   struct brw_reg src0,
-                  GLboolean allocate,
+                  bool allocate,
                   GLuint response_length,
-                  GLboolean eot)
+                  bool eot)
 {
    struct intel_context *intel = &p->brw->intel;
    struct brw_instruction *insn;
@@ -2406,3 +2438,101 @@ void brw_ff_sync(struct brw_compile *p,
                           response_length,
                           eot);
 }
+
+/**
+ * Emit the SEND instruction necessary to generate stream output data on Gen6
+ * (for transform feedback).
+ *
+ * If send_commit_msg is true, this is the last piece of stream output data
+ * from this thread, so send the data as a committed write.  According to the
+ * Sandy Bridge PRM (volume 2 part 1, section 4.5.1):
+ *
+ *   "Prior to End of Thread with a URB_WRITE, the kernel must ensure all
+ *   writes are complete by sending the final write as a committed write."
+ */
+void
+brw_svb_write(struct brw_compile *p,
+              struct brw_reg dest,
+              GLuint msg_reg_nr,
+              struct brw_reg src0,
+              GLuint binding_table_index,
+              bool   send_commit_msg)
+{
+   struct brw_instruction *insn;
+
+   gen6_resolve_implied_move(p, &src0, msg_reg_nr);
+
+   insn = next_insn(p, BRW_OPCODE_SEND);
+   brw_set_dest(p, insn, dest);
+   brw_set_src0(p, insn, src0);
+   brw_set_src1(p, insn, brw_imm_d(0));
+   brw_set_dp_write_message(p, insn,
+                            binding_table_index,
+                            0, /* msg_control: ignored */
+                            GEN6_DATAPORT_WRITE_MESSAGE_STREAMED_VB_WRITE,
+                            1, /* msg_length */
+                            true, /* header_present */
+                            0, /* last_render_target: ignored */
+                            send_commit_msg, /* response_length */
+                            0, /* end_of_thread */
+                            send_commit_msg); /* send_commit_msg */
+}
+
+/**
+ * This instruction is generated as a single-channel align1 instruction by
+ * both the VS and FS stages when using INTEL_DEBUG=shader_time.
+ *
+ * We can't use the typed atomic op in the FS because that has the execution
+ * mask ANDed with the pixel mask, but we just want to write the one dword for
+ * all the pixels.
+ *
+ * We don't use the SIMD4x2 atomic ops in the VS because want to just write
+ * one u32.  So we use the same untyped atomic write message as the pixel
+ * shader.
+ *
+ * The untyped atomic operation requires a BUFFER surface type with RAW
+ * format, and is only accessible through the legacy DATA_CACHE dataport
+ * messages.
+ */
+void brw_shader_time_add(struct brw_compile *p,
+                         struct brw_reg payload,
+                         uint32_t surf_index)
+{
+   struct intel_context *intel = &p->brw->intel;
+   assert(intel->gen >= 7);
+
+   brw_push_insn_state(p);
+   brw_set_access_mode(p, BRW_ALIGN_1);
+   brw_set_mask_control(p, BRW_MASK_DISABLE);
+   struct brw_instruction *send = brw_next_insn(p, BRW_OPCODE_SEND);
+   brw_pop_insn_state(p);
+
+   /* We use brw_vec1_reg and unmasked because we want to increment the given
+    * offset only once.
+    */
+   brw_set_dest(p, send, brw_vec1_reg(BRW_ARCHITECTURE_REGISTER_FILE,
+                                      BRW_ARF_NULL, 0));
+   brw_set_src0(p, send, brw_vec1_reg(payload.file,
+                                      payload.nr, 0));
+
+   uint32_t sfid, msg_type;
+   if (intel->is_haswell) {
+      sfid = HSW_SFID_DATAPORT_DATA_CACHE_1;
+      msg_type = HSW_DATAPORT_DC_PORT1_UNTYPED_ATOMIC_OP;
+   } else {
+      sfid = GEN7_SFID_DATAPORT_DATA_CACHE;
+      msg_type = GEN7_DATAPORT_DC_UNTYPED_ATOMIC_OP;
+   }
+
+   bool header_present = false;
+   bool eot = false;
+   uint32_t mlen = 2; /* offset, value */
+   uint32_t rlen = 0;
+   brw_set_message_descriptor(p, send, sfid, mlen, rlen, header_present, eot);
+
+   send->bits3.ud |= msg_type << 14;
+   send->bits3.ud |= 0 << 13; /* no return data */
+   send->bits3.ud |= 1 << 12; /* SIMD8 mode */
+   send->bits3.ud |= BRW_AOP_ADD << 8;
+   send->bits3.ud |= surf_index << 0;
+}