Commit 1966adc7 authored by Christoph Mallon's avatar Christoph Mallon
Browse files

all: Add and use QSORT() and QSORT_ARR().

parent 5512229a
......@@ -45,4 +45,8 @@
*/
#define PTR_TO_INT(v) (((char *)(v) - (char *)0))
#define QSORT(base, n, cmp) (qsort((base), (n), sizeof(*(base)), cmp))
#define QSORT_ARR(base, cmp) QSORT((base), ARR_LEN((base)), (cmp))
#endif
......@@ -228,7 +228,7 @@ void dfs_dump(const dfs_t *dfs, FILE *file)
nodes[n++] = node;
}
qsort(nodes, n, sizeof(nodes[0]), node_level_cmp);
QSORT(nodes, n, node_level_cmp);
int i = 0;
while (i < n) {
......
......@@ -11,6 +11,7 @@
#include <limits.h>
#include <stdbool.h>
#include "../../adt/util.h"
#include "bearch_arm_t.h"
#include "xmalloc.h"
#include "tv.h"
......@@ -529,7 +530,7 @@ static void emit_arm_CopyB(const ir_node *irn)
tmpregs[3] = &arm_registers[REG_R12];
/* Note: R12 is always the last register because the RA did not assign higher ones */
qsort((void *)tmpregs, 3, sizeof(tmpregs[0]), reg_cmp);
QSORT(tmpregs, 3, reg_cmp);
if (be_options.verbose_asm) {
arm_emitf(irn, "/* MemCopy (%S1)->(%S0) [%u bytes], Uses %r, %r, %r and %r */",
......
......@@ -915,7 +915,7 @@ static void process_ops_in_block(ir_node *bl, void *data)
int i;
/* order the call nodes according to data dependency */
qsort(nodes, n_nodes, sizeof(nodes[0]), cmp_call_dependency);
QSORT(nodes, n_nodes, cmp_call_dependency);
for (i = n_nodes - 1; i >= 0; --i) {
ir_node *irn = nodes[i];
......@@ -1057,7 +1057,7 @@ static void reg_map_to_arr(arch_register_t const **const res,
}
size_t n = pmap_count(reg_map);
qsort(res, n, sizeof(res[0]), cmp_regs);
QSORT(res, n, cmp_regs);
}
/**
......
......@@ -9,6 +9,8 @@
* selection phase.
* @author Matthias Braun
*/
#include "../../adt/util.h"
#include "beabihelper.h"
#include "bearch.h"
#include "beirg.h"
......@@ -600,7 +602,7 @@ static void process_ops_in_block(ir_node *block, void *data)
assert(n == n_nodes);
/* order nodes according to their data dependencies */
qsort(nodes, n_nodes, sizeof(nodes[0]), cmp_call_dependency);
QSORT(nodes, n_nodes, cmp_call_dependency);
/* remember the calculated dependency into a phase */
for (n = n_nodes-1; n > 0; --n) {
......
......@@ -23,6 +23,7 @@
#include <stdlib.h>
#include "../../adt/util.h"
#include "array.h"
#include "pdeq.h"
#include "beirg.h"
......@@ -256,7 +257,7 @@ static void coalesce_blocks(blocksched_env_t *env)
edge_t *edges = env->edges;
/* sort interblock edges by execution frequency */
qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
QSORT_ARR(edges, cmp_edges);
/* run1: only look at jumps */
for (i = 0; i < edge_count; ++i) {
......@@ -295,7 +296,7 @@ static void coalesce_blocks(blocksched_env_t *env)
/* run2: pick loop fallthroughs */
clear_loop_links(get_irg_loop(env->irg));
qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges_outedge_penalty);
QSORT_ARR(edges, cmp_edges_outedge_penalty);
for (i = 0; i < edge_count; ++i) {
const edge_t *edge = &edges[i];
ir_node *block = edge->block;
......@@ -341,7 +342,7 @@ static void coalesce_blocks(blocksched_env_t *env)
}
/* sort interblock edges by execution frequency */
qsort(edges, ARR_LEN(edges), sizeof(edges[0]), cmp_edges);
QSORT_ARR(edges, cmp_edges);
/* run3: remaining edges */
for (i = 0; i < edge_count; ++i) {
......
......@@ -462,7 +462,7 @@ static int change_color_not(co2_t *env, const ir_node *irn, col_t not_col, struc
csts[not_col].costs = INT_MAX;
/* sort the colors according costs, cheapest first. */
qsort(csts, n_regs, sizeof(csts[0]), col_cost_pair_lt);
QSORT(csts, n_regs, col_cost_pair_lt);
/* Try recoloring the node using the color list. */
res = recolor(env, irn, csts, parent_changed, depth);
......@@ -637,7 +637,7 @@ static int coalesce_top_down(co2_cloud_irn_t *ci, int child_nr, int depth)
seq[parent_col].costs = min_badness - 1;
/* Sort the colors. The will be processed in that ordering. */
qsort(seq, env->n_regs, sizeof(seq[0]), col_cost_pair_lt);
QSORT(seq, env->n_regs, col_cost_pair_lt);
DBG((env->dbg, LEVEL_2, "\t%2{firm:indent}starting top-down coalesce for %+F\n", depth, ci->inh.irn));
INIT_LIST_HEAD(&changed);
......@@ -823,7 +823,7 @@ static void process_cloud(co2_cloud_t *cloud)
}
}
edges = (edge_t*)obstack_finish(&cloud->obst);
qsort(edges, n_edges, sizeof(edges[0]), cmp_edges);
QSORT(edges, n_edges, cmp_edges);
/* Compute the maximum spanning tree using Kruskal/Union-Find */
DBG((env->dbg, LEVEL_2, "computing spanning tree of cloud with master %+F\n", cloud->master->inh.irn));
......@@ -973,7 +973,7 @@ static void process(co2_t *env)
clouds = XMALLOCN(co2_cloud_t*, n_clouds);
list_for_each_entry(co2_cloud_t, pos, &env->cloud_head, list)
clouds[i++] = pos;
qsort(clouds, n_clouds, sizeof(clouds[0]), cmp_clouds_gt);
QSORT(clouds, n_clouds, cmp_clouds_gt);
for (i = 0; i < n_clouds; ++i) {
DEBUG_ONLY(init_costs += cloud_costs(clouds[i]);)
......
......@@ -664,9 +664,8 @@ static void build_affinity_chunks(co_mst_env_t *env)
}
/* now: sort edges and build the affinity chunks */
size_t const len = ARR_LEN(edges);
qsort(edges, len, sizeof(edges[0]), cmp_aff_edge);
for (size_t i = 0; i < len; ++i) {
QSORT_ARR(edges, cmp_aff_edge);
for (size_t i = 0, len = ARR_LEN(edges); i < len; ++i) {
DBG((dbg, LEVEL_1, "edge (%u,%u) %f\n", edges[i].src->node_idx, edges[i].tgt->node_idx, edges[i].weight));
(void)aff_chunk_absorb(env, edges[i].src, edges[i].tgt);
......@@ -988,7 +987,7 @@ static int change_node_color_excluded(co_mst_env_t *env, co_mst_irn_t *node, int
costs[exclude_col].cost = REAL(0.0);
/* sort the colors according costs, cheapest first. */
qsort(costs, env->n_regs, sizeof(costs[0]), cmp_col_cost_gt);
QSORT(costs, env->n_regs, cmp_col_cost_gt);
/* Try recoloring the node using the color list. */
res = recolor_nodes(env, node, costs, changed, depth + 1, max_depth, trip);
......@@ -1188,7 +1187,7 @@ static void color_aff_chunk(co_mst_env_t *env, aff_chunk_t *c)
order[i].cost = (REAL(1.0) - dislike_influence) * c->color_affinity[i].cost + dislike_influence * dislike;
}
qsort(order, env->n_regs, sizeof(order[0]), cmp_col_cost_gt);
QSORT(order, env->n_regs, cmp_col_cost_gt);
DBG_COL_COST(env, LEVEL_2, order);
DB((dbg, LEVEL_2, "\n"));
......
......@@ -569,7 +569,7 @@ static void co_sort_units(copy_opt_t *co)
for (i=0; i<count; ++i)
ir_printf("%+F\n", ous[i]->nodes[0]);
qsort(ous, count, sizeof(*ous), compare_ous);
QSORT(ous, count, compare_ous);
ir_printf("\n\n");
for (i=0; i<count; ++i)
......
......@@ -16,6 +16,7 @@
*/
#include <stdlib.h>
#include "../../adt/util.h"
#include "hashptr.h"
#include "debug.h"
#include "error.h"
......@@ -97,7 +98,7 @@ ir_node *insert_Perm_before(ir_graph *irg, const arch_register_class_t *cls,
}
ir_nodeset_destroy(&live);
/* make the input order deterministic */
qsort(nodes, n, sizeof(nodes[0]), cmp_node_nr);
QSORT(nodes, n, cmp_node_nr);
ir_node *const bl = get_nodes_block(pos);
perm = be_new_Perm(cls, bl, n, nodes);
......
......@@ -538,7 +538,7 @@ static void fill_sort_candidates(reg_pref_t *regprefs,
regprefs[r].pref = pref;
}
/* TODO: use a stable sort here to avoid unnecessary register jumping */
qsort(regprefs, n_regs, sizeof(regprefs[0]), compare_reg_pref);
QSORT(regprefs, n_regs, compare_reg_pref);
}
static bool try_optimistic_split(ir_node *to_split, ir_node *before,
......@@ -1696,7 +1696,7 @@ static void determine_block_order(void)
}
/* sort array by block costs */
qsort(blocklist, n_blocks, sizeof(blocklist[0]), cmp_block_costs);
QSORT_ARR(blocklist, cmp_block_costs);
ir_reserve_resources(irg, IR_RESOURCE_BLOCK_VISITED);
inc_irg_block_visited(irg);
......
......@@ -162,7 +162,7 @@ static int normal_tree_cost(ir_node* irn, instance_t *inst)
costs[i].cost = cost;
}
qsort(costs, arity, sizeof(*costs), cost_cmp);
QSORT(costs, arity, cost_cmp);
set_irn_link(irn, fc);
}
......@@ -312,7 +312,7 @@ static void normal_sched_block(ir_node* block, void* env)
ir_fprintf(stderr, "height of %+F is %u\n", roots[i], root_costs[i].cost);
#endif
}
qsort(root_costs, root_count, sizeof(*root_costs), root_cmp);
QSORT(root_costs, root_count, root_cmp);
#if defined NORMAL_DBG
{
int n = root_count;
......
......@@ -203,7 +203,7 @@ static int loc_compare(const void *a, const void *b)
static void workset_sort(workset_t *workset)
{
qsort(workset->vals, workset->len, sizeof(workset->vals[0]), loc_compare);
QSORT(workset->vals, workset->len, loc_compare);
}
static inline unsigned workset_get_time(const workset_t *workset, unsigned idx)
......@@ -560,7 +560,7 @@ static void decide_start_workset(ir_node *const block)
DB((dbg, DBG_START, "Loop pressure %d, taking %d delayed vals\n",
pressure, free_slots));
if (free_slots > 0) {
qsort(delayed, ARR_LEN(delayed), sizeof(delayed[0]), loc_compare);
QSORT_ARR(delayed, loc_compare);
for (size_t i = 0; i < ARR_LEN(delayed) && free_slots > 0; ++i) {
loc_t *loc = & delayed[i];
......@@ -605,7 +605,7 @@ static void decide_start_workset(ir_node *const block)
DEL_ARR_F(delayed);
/* Sort start values by first use */
qsort(starters, ARR_LEN(starters), sizeof(starters[0]), loc_compare);
QSORT_ARR(starters, loc_compare);
/* Copy the best ones from starters to start workset */
unsigned ws_count = MIN((unsigned) ARR_LEN(starters), n_regs);
......
......@@ -18,6 +18,8 @@
* Spilling in this context means placing a spill instruction behind the
* definition of the value and a reload before each usage.
*/
#include "../../adt/util.h"
#include "debug.h"
#include "irnodeset.h"
......@@ -169,8 +171,7 @@ static void do_spilling(ir_nodeset_t *live_nodes, ir_node *node)
assert(c == n_live_nodes);
/* sort spill candidates */
qsort(candidates, n_live_nodes, sizeof(candidates[0]),
compare_spill_candidates_desc);
QSORT(candidates, n_live_nodes, compare_spill_candidates_desc);
/* spill cheapest ones */
size_t cand_idx = 0;
......
......@@ -11,6 +11,7 @@
*/
#include <stdlib.h>
#include "../../adt/util.h"
#include "set.h"
#include "array.h"
#include "irgwalk.h"
......@@ -394,12 +395,10 @@ static void do_greedy_coalescing(be_fec_env_t *env)
}
/* sort affinity edges */
size_t affinity_edge_count = ARR_LEN(env->affinity_edges);
qsort(env->affinity_edges, affinity_edge_count,
sizeof(env->affinity_edges[0]), cmp_affinity);
QSORT_ARR(env->affinity_edges, cmp_affinity);
/* try to merge affine nodes */
for (size_t i = 0; i < affinity_edge_count; ++i) {
for (size_t i = 0, n = ARR_LEN(env->affinity_edges); i < n; ++i) {
const affinity_edge_t *edge = env->affinity_edges[i];
int s1 = uf_find(spillslot_unionfind, edge->slot1);
int s2 = uf_find(spillslot_unionfind, edge->slot2);
......
......@@ -28,6 +28,7 @@
*/
#include <limits.h>
#include "../../adt/util.h"
#include "xmalloc.h"
#include "tv.h"
#include "iredges.h"
......@@ -1742,7 +1743,7 @@ void ia32_emit_function(ir_graph *irg)
/* Sort the exception table using the exception label id's.
Those are ascending with ascending addresses. */
qsort(exc_list, ARR_LEN(exc_list), sizeof(exc_list[0]), cmp_exc_entry);
QSORT_ARR(exc_list, cmp_exc_entry);
{
size_t e;
......
......@@ -10,6 +10,7 @@
*/
#include <limits.h>
#include "../../adt/util.h"
#include "beutil.h"
#include "bitfiddle.h"
#include "xmalloc.h"
......@@ -1463,8 +1464,7 @@ static void pick_delay_slots(size_t n_blocks, ir_node **blocks)
/* create blocklist sorted by execution frequency */
ir_node **sorted_blocks = XMALLOCN(ir_node*, n_blocks);
memcpy(sorted_blocks, blocks, n_blocks*sizeof(sorted_blocks[0]));
qsort(sorted_blocks, n_blocks, sizeof(sorted_blocks[0]),
cmp_block_execfreqs);
QSORT(sorted_blocks, n_blocks, cmp_block_execfreqs);
for (size_t i = 0; i < n_blocks; ++i) {
sched_foreach(sorted_blocks[i], node) {
......
......@@ -7221,7 +7221,7 @@ void ir_normalize_node(ir_node *n)
ir_node **ins = get_irn_in(n)+1;
ir_node **new_ins = XMALLOCN(ir_node*, arity);
memcpy(new_ins, ins, arity*sizeof(ins[0]));
qsort(new_ins, arity, sizeof(new_ins[0]), cmp_node_nr);
QSORT(new_ins, arity, cmp_node_nr);
set_irn_in(n, arity, new_ins);
free(new_ins);
}
......
......@@ -11,6 +11,7 @@
#include <limits.h>
#include <stdbool.h>
#include "../../adt/util.h"
#include "array.h"
#include "ircons.h"
#include "irgopt.h"
......@@ -133,7 +134,7 @@ static void analyse_switch1(switch_info_t *info)
* Switch should be transformed into an if cascade.
* So first order the cases, so we can do a binary search on them.
*/
qsort(cases, num_cases, sizeof(cases[0]), casecmp);
QSORT(cases, num_cases, casecmp);
info->default_block = targets[pn_Switch_default];
info->cases = cases;
......
......@@ -662,7 +662,7 @@ void matrix_optimize(sp_matrix_t *m)
matrix_foreach(m, e)
c[e->row]++;
qsort(c, size, sizeof(*c), cmp_count);
QSORT(c, size, cmp_count);
for (i = 0; i < size; ++i) {
if (! bitset_is_set(fullrow, i))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment