+ const ir_node *check = node;
+ const ir_node *schedpoint = node;
+ unsigned tries = 0;
+ /* currently we don't track which registers are still alive, so we can't
+ * pick any other instructions other than the one directly preceding */
+ static const unsigned PICK_DELAY_SLOT_MAX_DISTANCE = 1;
+
+ assert(has_delay_slot(node));
+
+ if (is_sparc_Call(node)) {
+ const sparc_attr_t *attr = get_sparc_attr_const(node);
+ ir_entity *entity = attr->immediate_value_entity;
+ if (entity != NULL) {
+ check = NULL; /* pick any instruction, dependencies on Call
+ don't matter */
+ } else {
+ /* we only need to check the value for the call destination */
+ check = get_irn_n(node, get_sparc_Call_dest_addr_pos(node));
+ }
+
+ /* the Call also destroys the value of %o7, but since this is currently
+ * marked as ignore register in the backend, it should never be used by
+ * the instruction in the delay slot. */
+ } else if (be_is_Return(node)) {
+ /* we only have to check the jump destination value */
+ int arity = get_irn_arity(node);
+ int i;
+
+ check = NULL;
+ for (i = 0; i < arity; ++i) {
+ ir_node *in = get_irn_n(node, i);
+ const arch_register_t *reg = arch_get_irn_register(in);
+ if (reg == &sparc_registers[REG_O7]) {
+ check = skip_Proj(in);
+ break;
+ }
+ }
+ } else {
+ check = node;
+ }
+
+ while (sched_has_prev(schedpoint)) {
+ schedpoint = sched_prev(schedpoint);
+
+ if (has_delay_slot(schedpoint))
+ break;
+
+ /* skip things which don't really result in instructions */
+ if (is_no_instruction(schedpoint))
+ continue;
+
+ if (tries++ >= PICK_DELAY_SLOT_MAX_DISTANCE)
+ break;
+
+ if (emits_multiple_instructions(schedpoint))
+ continue;
+
+ /* allowed for delayslot: any instruction which is not necessary to
+ * compute an input to the branch. */
+ if (check != NULL
+ && heights_reachable_in_block(heights, check, schedpoint))
+ continue;
+
+ /* found something */
+ return schedpoint;
+ }
+
+ return NULL;