ir = a->array->as_dereference();
ir_rvalue *idx = a->array_index;
- if (idx->as_dereference_variable()) {
- ir_variable *var = idx->variable_referenced();
+ ir_variable *var = idx->variable_referenced();
- /* If the index is read only it cannot change so there is no need
- * to copy it.
- */
- if (var->data.read_only || var->data.memory_read_only)
- return;
-
- ir_variable *tmp = new(d->mem_ctx) ir_variable(idx->type, "idx_tmp",
- ir_var_temporary);
- d->before_instructions->push_tail(tmp);
-
- ir_dereference_variable *const deref_tmp_1 =
- new(d->mem_ctx) ir_dereference_variable(tmp);
- ir_assignment *const assignment =
- new(d->mem_ctx) ir_assignment(deref_tmp_1,
- idx->clone(d->mem_ctx, NULL));
- d->before_instructions->push_tail(assignment);
-
- /* Replace the array index with a dereference of the new temporary */
- ir_dereference_variable *const deref_tmp_2 =
- new(d->mem_ctx) ir_dereference_variable(tmp);
- a->array_index = deref_tmp_2;
- }
+ /* If the index is read only it cannot change so there is no need
+ * to copy it.
+ */
+ if (!var || var->data.read_only || var->data.memory_read_only)
+ return;
+
+ ir_variable *tmp = new(d->mem_ctx) ir_variable(idx->type, "idx_tmp",
+ ir_var_temporary);
+ d->before_instructions->push_tail(tmp);
+
+ ir_dereference_variable *const deref_tmp_1 =
+ new(d->mem_ctx) ir_dereference_variable(tmp);
+ ir_assignment *const assignment =
+ new(d->mem_ctx) ir_assignment(deref_tmp_1,
+ idx->clone(d->mem_ctx, NULL));
+ d->before_instructions->push_tail(assignment);
+
+ /* Replace the array index with a dereference of the new temporary */
+ ir_dereference_variable *const deref_tmp_2 =
+ new(d->mem_ctx) ir_dereference_variable(tmp);
+ a->array_index = deref_tmp_2;
}
}