1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/// Performs common subexpression elimination. The implementation performs
/// local common subexpression elimination, but will result in a global
/// common subexpression elimination, when it is executed after the
/// `EarliestPlacement` optimization.
///
/// Keep in mind that global common subexpression elimination is a trade-off.
/// It's not always better to merge common subexpressions. This is especially
/// true if you merge two common subexpression A and B, where neither A
/// dominates B, nor B dominates A.
use super::Outcome;
use crate::{dot::*, optimization};
use libfirm_rs::{
    bindings,
    nodes::{Node, NodeTrait},
    Graph,
};

use std::{
    cmp::Ordering,
    collections::{HashSet, VecDeque},
    hash::{Hash, Hasher},
};

struct SubExprNode(Node);

impl Hash for SubExprNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // this has to reflect the contents of PartialEq::eq for SubExprNode
        // The following invariant has to hold: k1 == k2 -> hash(k1) == hash(k2)
        debug_assert!(CommonSubExpr::node_qualifies_for_elim(self.0));
        self.0.block().hash(state);

        let op_eq = unsafe { bindings::get_irn_op(self.0.internal_ir_node()) };
        op_eq.hash(state);

        self.0.mode().hash(state);

        for pred in self.0.in_nodes() {
            pred.hash(state);
        }

        // hash attributes of each node
        if let Node::Const(c) = self.0 {
            c.tarval().kind().hash(state);
        }
    }
}

impl PartialEq for SubExprNode {
    fn eq(&self, other: &Self) -> bool {
        debug_assert!(CommonSubExpr::node_qualifies_for_elim(self.0));
        debug_assert!(CommonSubExpr::node_qualifies_for_elim(other.0));
        debug_assert!(!Node::is_block(self.0));
        debug_assert!(!Node::is_block(other.0));

        // test if it describes the same node variant,
        // e.g. is both a "Const Is" node?
        if (unsafe {
            bindings::get_irn_op(self.0.internal_ir_node())
                != bindings::get_irn_op(other.0.internal_ir_node())
        }) || self.0.mode() != other.0.mode()
        {
            return false;
        }

        if self.0 == other.0 {
            return true;
        }

        if self.0.block() != other.0.block() {
            return false;
        }

        // check if the predecessors are the same
        //
        // Speed up the common case of iterators with unequal
        // length. (Iterator::eq does not optimize this case)
        let in_nodes_self = self.0.in_nodes();
        let in_nodes_other = other.0.in_nodes();

        if in_nodes_self.len() != in_nodes_other.len() {
            return false;
        }

        if !Iterator::eq(in_nodes_self, in_nodes_other) {
            return false;
        }

        // Some nodes have attributes that have to be identical, e.g.
        // - Const nodes have their tarval as attribute
        // - ASM nodes have the assembly as their attributes
        //
        // Internally, the equivalence of node attributes can be checked using
        // `node->op->ops.attrs_equal(a,b)`, but this is not part of the public API.
        //
        // Since libfirm does not expose shit regarding this API, we employ a whitelist.
        // In case you want to compare this to the actual implementation:
        //
        // - Nodes default to no attributes, therefore they are by default 'always
        //   equal'
        // - A list of overrides for middle end nodes can be found in:
        //   83e6f63dba4f83f743b7f5d383af08454f95e90d/ir/ir/irop.c#L580
        // - Backend specific nodes for the amd64 target default to 'always unequal' by
        //   default
        // - Overrides for backend nodes are implemented in:
        //   fa4fea6c01a13e4cb7bfbffb018b9407f531f80b/ir/be/benode.c#L652

        // performing GCSE on const like nodes, e.g. Const and Address, that do not
        // result in instructions is still desired as we expect predecessors to
        // be indentical (pointer equality)
        // TODO: only match one, assert the other.0
        match (self.0, other.0) {
            (Node::Const(a), Node::Const(b)) => {
                // TODO: add asserts for unknown and bad tarval
                a.tarval() == b.tarval()
            }
            (Node::Or(_), Node::Or(_))
            | (Node::Add(_), Node::Add(_))
            | (Node::Sub(_), Node::Sub(_))
            | (Node::Mul(_), Node::Mul(_))
            | (Node::And(_), Node::And(_))
            | (Node::Not(_), Node::Not(_))
            | (Node::Minus(_), Node::Minus(_)) => true,
            unknown => {
                log::debug!("missing attribute equality for {:?}", unknown);
                false
            }
        }
    }
}

impl Eq for SubExprNode {}

pub struct CommonSubExpr {
    graph: Graph,
    // TODO: throw away data for a block when it is done
    similarity: HashSet<SubExprNode>,
    worklist: VecDeque<Node>,
    // we could use the same hashset for visited and eliminated, but
    // that makes debugging really hard!
    visited: HashSet<Node>,
    eliminated: HashSet<Node>,
    num_changed: usize,
}

impl optimization::Local for CommonSubExpr {
    fn optimize_function(graph: Graph) -> Outcome {
        Self::new(graph).run()
    }
}

impl CommonSubExpr {
    fn new(graph: Graph) -> Self {
        Self {
            graph,
            similarity: HashSet::new(),
            worklist: VecDeque::new(),
            visited: HashSet::new(),
            eliminated: HashSet::new(),
            num_changed: 0,
        }
    }

    fn run(&mut self) -> Outcome {
        let end_block = self.graph.end_block();
        self.worklist.push_back(Node::Block(end_block));

        self.graph.assure_outs();

        while let Some(current_node) = self.worklist.pop_front() {
            self.visit_node(current_node, "worklist");
        }

        if self.num_changed > 0 {
            self.graph.remove_unreachable_code();
            self.graph.remove_bads();
            Outcome::Changed
        } else {
            Outcome::Unchanged
        }
    }

    fn visit_node(&mut self, current_node: Node, debug_context: &str) -> Outcome {
        if self.visited.contains(&current_node) {
            breakpoint!(
                &format!(
                    "Earliest Placement: ignoring second visit of {:?} via '{}'",
                    current_node, debug_context
                ),
                self.graph,
                &|node: &Node| label_with_cse_info(node, &current_node)
            );
            return Outcome::Unchanged;
        }

        self.visited.insert(current_node);

        breakpoint!(
            &format!("CSE: visiting {:?} {}", current_node, debug_context),
            self.graph,
            &|node: &Node| label_with_cse_info(node, &current_node)
        );

        self.attempt_cse(current_node, debug_context)
    }

    fn attempt_cse(&mut self, current_node: Node, debug_context: &str) -> Outcome {
        if !Self::node_qualifies_for_elim(current_node) {
            // its only important to move depth first over the predecessors
            // of a chunk of nodes that all qualify for CSE. Since
            // the current node cannot be eliminated, we can push it onto
            // the worklist to keep the stack small.
            for pred in current_node.in_nodes() {
                self.worklist.push_back(pred);
                if !Node::is_block(pred) {
                    self.worklist.push_back(Node::Block(pred.block()));
                }
            }

            return Outcome::Unchanged;
        }

        if self.eliminated.contains(&current_node) {
            breakpoint!(
                &format!("CSE: ignoring visit of eliminated {:?}", current_node),
                self.graph,
                &|node: &Node| label_with_cse_info(node, &current_node)
            );

            return Outcome::Unchanged;
        }

        // we want to visit the predecessors of each node first. This allows us
        // to simplify the predecessor comparison in CSE to a simple pointer
        // comparison => move depth first over chunk that qualifies for elim.
        self.visit_node(
            Node::Block(current_node.block()),
            &format!("block of {:?}", current_node),
        );

        for pred in current_node.in_nodes() {
            self.visit_node(pred, &format!("pred of {:?}", current_node));
        }

        breakpoint!(
            &format!(
                "CSE: checking {:?} for congruence as '{}'",
                current_node, debug_context
            ),
            self.graph,
            &|node: &Node| {
                let mut label = label_with_cse_info(node, &current_node);

                if current_node == *node {
                    label = label
                        .add_style(Style::Filled)
                        .fillcolor(X11Color::Red)
                        .fontcolor(X11Color::White);
                }

                label
            }
        );

        // normalize node, this allows us to detect commutative expressions,
        // e.g. 'x * 5' and '5 * x'
        self.normalize_node(current_node);

        // at this point, predecessors were processed by CSE, the predecessor
        // equality can now be solved using simple pointer equality, process
        // the current node
        if let Some(SubExprNode(reprasentative)) = self.similarity.get(&SubExprNode(current_node)) {
            // we found a reprasentative node that is congruent to the current node,
            // use the reprasentative instead
            //log::error!(
            //"CSE: {:?} and {:?} are congruent",
            //current_node,
            //reprasentative
            //);

            breakpoint!(
                &format!(
                    "CSE: {:?} and {:?} are congruent",
                    current_node, reprasentative
                ),
                self.graph,
                &|node: &Node| {
                    let mut label = label_with_cse_info(node, &current_node);

                    if reprasentative == node {
                        label = label
                            .add_style(Style::Filled)
                            .fillcolor(X11Color::Green)
                            .fontcolor(X11Color::White);
                    }

                    label
                }
            );

            self.num_changed += 1;
            self.eliminated.insert(current_node);

            // remove the current node from the graph
            for (consumer, idx) in current_node.out_nodes_ex() {
                consumer.set_input_at(idx, *reprasentative);
            }

            current_node.set_in_nodes(&[]);

            // In this method, we
            // - generate unreachable code (nodes removed by merging)
            // - invalide outs (merged node gets additional inputs, inputs are reordered)
            Outcome::Changed
        } else {
            // no congruent node yet, use the current node as representative
            self.similarity.insert(SubExprNode(current_node));
            Outcome::Unchanged
        }
    }

    fn normalize_node(&self, node: Node) {
        // put commutative ops in an arbitrary but stable order
        // Exaustive list of commutative nodes: add, and, eor, mul, mulh, or
        if node.is_commutative() {
            let operand_right =
                Node::wrap(unsafe { bindings::get_binop_right(node.internal_ir_node()) });
            let operand_left =
                Node::wrap(unsafe { bindings::get_binop_left(node.internal_ir_node()) });

            if Self::cmp_node(operand_left, operand_right) == Ordering::Greater {
                breakpoint!(
                    &format!(
                        "CSE: normalizing order of commutative operands {:?} and {:?} of {:?}",
                        operand_left, operand_right, node
                    ),
                    self.graph,
                    &|rendered: &Node| {
                        let mut label = dom_info_box(rendered);

                        if *rendered == node {
                            label = label
                                .add_style(Style::Filled)
                                .fillcolor(X11Color::Blue)
                                .fontcolor(X11Color::White);
                        }

                        if *rendered == operand_right || *rendered == operand_left {
                            label = label
                                .add_style(Style::Filled)
                                .fillcolor(X11Color::Green)
                                .fontcolor(X11Color::White);
                        }
                        label
                    }
                );

                unsafe {
                    bindings::set_binop_right(
                        node.internal_ir_node(),
                        operand_left.internal_ir_node(),
                    )
                };
                unsafe {
                    bindings::set_binop_left(
                        node.internal_ir_node(),
                        operand_right.internal_ir_node(),
                    )
                };
            }
        }
    }

    /// Fast check that is false if the node cannot be equal to anything
    /// => is guranteed to be not subexpression eliminatable
    fn node_qualifies_for_elim(node: Node) -> bool {
        match node {
            Node::Const(_)
            | Node::Or(_)
            | Node::Add(_)
            | Node::Sub(_)
            | Node::Mul(_)
            | Node::And(_)
            | Node::Not(_)
            | Node::Minus(_) => true,
            _ => false,
        }
    }

    fn cmp_node(a: Node, b: Node) -> Ordering {
        // Libfirm sorts by multiple critera:
        // df79debd25b9372f92c416c4d659d2b1cf17009d/ir/opt/iropt.c#L7812
        // df79debd25b9372f92c416c4d659d2b1cf17009d/ir/opt/iropt.c#L2169
        a.node_id().cmp(&b.node_id())
    }
}

pub fn label_with_cse_info(node: &Node, highlight: &Node) -> Label {
    let mut label = default_label(node);

    if node == highlight {
        label = label
            .add_style(Style::Filled)
            .fillcolor(X11Color::Blue)
            .fontcolor(X11Color::White);
    }

    if !CommonSubExpr::node_qualifies_for_elim(*node) {
        label.add_style(Style::Bold)
    } else {
        label.add_style(Style::Dashed)
    }
}