+static int get_loop_weight(ir_node *irn) {
+ int cost = 0;
+ ir_loop *loop = get_irn_loop(get_nodes_block(irn));
+
+ if (loop) {
+ int d = get_loop_depth(loop);
+ cost = d*d;
+ }
+ return cost+1;
+}
+
+#define get_const_weight(irn) (1)
+
+#define get_spill_weight(irn) get_loop_weight(irn)
+#define get_reload_weight(irn) get_loop_weight(irn)
+#define get_affinity_weight(irn) get_loop_weight(irn)
+
+/******************************************************************************
+ _____ _ _____ _
+ / ____| | | / ____| (_)
+ | | ___ _ __ ___| |_ _ __ | | ___ _ __ _ ___ ___
+ | | / _ \| '_ \/ __| __| '__| | | / _ \| '_ \| |/ _ \/ __|
+ | |___| (_) | | | \__ \ |_| | | |___| (_) | |_) | | __/\__ \
+ \_____\___/|_| |_|___/\__|_| \_____\___/| .__/|_|\___||___/
+ | |
+ |_|
+ *****************************************************************************/
+
+static void handle_constraints_walker(ir_node *irn, void *env) {
+ be_raext_env_t *raenv = env;
+ arch_register_req_t req;
+ int pos, max;
+
+ /* handle output constraints
+ * user -> irn becomes user -> cpy -> irn
+ */
+ arch_get_register_req(raenv->aenv, &req, irn, -1);
+ if (arch_register_req_is(&req, limited)) {
+ ir_node *cpy = be_new_Copy(req.cls, raenv->irg, get_nodes_block(irn), irn);
+
+ /* all users of the irn use the copy instead */
+ sched_add_after(irn, cpy);
+ edges_reroute(irn, cpy, raenv->irg);
+ }
+
+
+ /* handle input constraints by converting them into output constraints
+ * of copies of the former argument
+ * irn -> arg becomes irn -> copy -> arg
+ */
+ for (pos = 0, max = get_irn_arity(irn); pos<max; ++pos) {
+ arch_get_register_req(raenv->aenv, &req, irn, pos);
+ if (arch_register_req_is(&req, limited)) {
+ ir_node *arg = get_irn_n(irn, pos);
+ ir_node *cpy = be_new_Copy(req.cls, raenv->irg, get_nodes_block(irn), arg);
+
+ /* use the copy instead */
+ sched_add_before(irn, cpy);
+ set_irn_n(irn, pos, cpy);
+
+ /* set an out constraint for the copy */
+ be_set_constr_limited(cpy, -1, &req);
+ }
+ }
+}
+
+static void handle_constraints(be_raext_env_t *raenv) {
+ irg_walk_graph(raenv->irg, NULL, handle_constraints_walker, raenv);
+}
+