summaryrefslogtreecommitdiff
path: root/src/main/java/com/seaofnodes/simple/node/MulNode.java
blob: bb99cad57cd225732993f29d3a39de68863dab12 (plain)
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
package com.seaofnodes.simple.node;

import com.seaofnodes.simple.codegen.CodeGen;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import java.util.BitSet;

public class MulNode extends ArithNode {
    @Override boolean allowFloat() { return true; }
    public MulNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
    public MulNode(Node lhs, Node rhs, byte mode) { super(null, lhs, rhs, mode); }
    @Override public Tag serialTag() { return Tag.Mul; }

    @Override public String op() { return "*"; }

    @Override long   doOp( long  x,  long  y ) { return x * y; }
    @Override double doOp(double x, double y ) { return x * y; }
    @Override TypeInteger doOp(TypeInteger x, TypeInteger y) {
        return TypeInteger.BOT;
    }

    @Override
    public Node idealize() {
        Node lhs = in(1);
        Node rhs = in(2);
        Type t1 = lhs._type;
        Type t2 = rhs._type;

        // Move constants to RHS: con*arg becomes arg*con
        if ( t1.isConstant() && !t2.isConstant() )
            return swap12();

        // Multiply by constant
        if ( t2.isConstant() && t2 instanceof TypeInteger i ) {
            // Mul of 1.  We do not check for (1*x) because this will already
            // canonicalize to (x*1)
            long c = i.value();
            if( c==1 )  return lhs._type.isa(compute()) ? lhs : super.idealize();
            if( c==0 )  return CodeGen.CODE.ZERO;
            // Mul by a power of 2, +/-1.  Bit patterns more complex than this
            // are unlikely to win on an X86 vs the normal "imul", and so
            // become machine-specific.
            if( (c & (c-1)) == 0 )
                return new ShlNode(null,lhs,con(Long.numberOfTrailingZeros(c)));
            // 2**n + 1, e.g. 9
            long d = c-1;
            if( (d & (d-1)) == 0 )
                return new AddNode(new ShlNode(null,lhs,con(Long.numberOfTrailingZeros(d))).peephole(),lhs);
            // 2**n - 1, e.g. 31
            long e = c+1;
            if( (e & (e-1)) == 0 )
                return new SubNode(new ShlNode(null,lhs,con(Long.numberOfTrailingZeros(e))).peephole(),lhs);

        }

        // Do we have ((x * (phi cons)) * con) ?
        // Do we have ((x * (phi cons)) * (phi cons)) ?
        // Push constant up through the phi: x * (phi con0*con0 con1*con1...)
        Node phicon = AddNode.phiCon(this,true);
        if( phicon!=null ) return phicon;

        return super.idealize();
    }
    @Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs,_mode); }
}