bepeephole.h 2.53 KB
Newer Older
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
4
5
6
7
 */

/**
 * @file
8
 * @brief       peephole optimization framework
9
10
 * @author      Matthias Braun
 */
11
12
13
#ifndef BEPEEPHOLE_H
#define BEPEEPHOLE_H

14
#include "bearch.h"
15

16
extern ir_node **register_values;
17

18
static inline ir_node *be_peephole_get_value(unsigned register_idx)
19
{
20
	return register_values[register_idx];
21
22
}

23
static inline ir_node *be_peephole_get_reg_value(const arch_register_t *reg)
24
{
25
26
	unsigned register_idx = reg->global_index;
	return be_peephole_get_value(register_idx);
27
28
}

Matthias Braun's avatar
Matthias Braun committed
29
/**
30
 * Datatype of the generic op handler for optimization.
Matthias Braun's avatar
Matthias Braun committed
31
 */
32
33
typedef void (*peephole_opt_func) (ir_node *node);

34
/**
35
 * When doing peephole optimization use this function instead of plain
36
37
38
 * exchange(), so it can update its internal state.  This function also removes
 * the old node from the schedule.
 */
Christoph Mallon's avatar
Christoph Mallon committed
39
void be_peephole_exchange(ir_node *old, ir_node *nw);
40

41
42
43
44
45
46
/**
 * Same as be_peephole_exchange(), but also replace @p old in the schedule
 * by @p nw.
 */
void be_peephole_replace(ir_node *old, ir_node *nw);

47
48
ir_node *be_peephole_to_tuple(ir_node *node);

49
/**
50
 * Tries to optimize a beIncSp node with its previous IncSP node.
51
52
53
 * Must be run from a be_peephole_opt() context.
 *
 * @param node  a be_IncSP node
54
 *
55
 * @return whether @p node was removed
56
 */
57
bool be_peephole_IncSP_IncSP(ir_node *node);
58

59
60
bool be_has_only_one_user(ir_node *node);

61
62
/**
 * In a scheduled program with registers assigned,
yb9976's avatar
yb9976 committed
63
 * checks whether @p node can be moved before @p before without changing program
64
65
66
67
68
 * semantics.
 *
 * Note: It is allowed to use this function without being in a peephole
 * optimization phase.
 */
69
bool be_can_move_down(ir_heights_t *heights, const ir_node *node,
70
71
                      const ir_node *before,
                      get_frame_entity_func get_frame_entity);
72
73
74

bool be_can_move_up(ir_heights_t *heights, const ir_node *node,
                    const ir_node *after);
75

76
77
78
79
80
81
82
83
84
/**
 * Register a peephole optimization function.
 */
static inline void register_peephole_optimization(ir_op *const op, peephole_opt_func const func)
{
	assert(!op->ops.generic);
	op->ops.generic = (op_func)func;
}

85
/**
86
 * Do peephole optimizations. It traverses the schedule of all blocks in
Matthias Braun's avatar
Matthias Braun committed
87
88
89
 * backward direction. The register_values variable indicates which (live)
 * values are stored in which register.
 * The generic op handler is called for each node if it exists. That's where
90
 * backend specific optimizations should be performed based on the
Matthias Braun's avatar
Matthias Braun committed
91
 * register-liveness information.
92
 */
93
void be_peephole_opt(ir_graph *irg);
94
95

#endif