NormalSsaInsn.java revision 579d7739c53a2707ad711a2d2cae46d7d782f061
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.dx.ssa;
18
19import com.android.dx.rop.code.*;
20
21/**
22 * A "normal" (non-phi) instruction in SSA form. Always wraps a rop insn.
23 */
24public final class NormalSsaInsn extends SsaInsn implements Cloneable {
25    /** {@code non-null;} rop insn that we're wrapping */
26    private Insn insn;
27
28    /**
29     * Creates an instance.
30     *
31     * @param insn Rop insn to wrap
32     * @param block block that contains this insn
33     */
34    NormalSsaInsn(final Insn insn, final SsaBasicBlock block) {
35        super(insn.getResult(), block);
36        this.insn = insn;
37    }
38
39    /** {@inheritDoc} */
40    @Override
41    public final void mapSourceRegisters(RegisterMapper mapper) {
42        RegisterSpecList oldSources = insn.getSources();
43        RegisterSpecList newSources = mapper.map(oldSources);
44
45        if (newSources != oldSources) {
46            insn = insn.withNewRegisters(getResult(), newSources);
47            getBlock().getParent().onSourcesChanged(this, oldSources);
48        }
49    }
50
51    /**
52     * Changes one of the insn's sources. New source should be of same type
53     * and category.
54     *
55     * @param index {@code >=0;} index of source to change
56     * @param newSpec spec for new source
57     */
58    public final void changeOneSource(int index, RegisterSpec newSpec) {
59        RegisterSpecList origSources = insn.getSources();
60        int sz = origSources.size();
61        RegisterSpecList newSources = new RegisterSpecList(sz);
62
63        for (int i = 0; i < sz; i++) {
64            newSources.set(i, i == index ? newSpec : origSources.get(i));
65        }
66
67        newSources.setImmutable();
68
69        RegisterSpec origSpec = origSources.get(index);
70        if (origSpec.getReg() != newSpec.getReg()) {
71            /*
72             * If the register remains unchanged, we're only changing
73             * the type or local var name so don't update use list
74             */
75            getBlock().getParent().onSourceChanged(this, origSpec, newSpec);
76        }
77
78        insn = insn.withNewRegisters(getResult(), newSources);
79    }
80
81    /**
82     * Changes the source list of the insn. New source list should be the
83     * same size and consist of sources of identical types.
84     *
85     * @param newSources non-null new sources list.
86     */
87    public final void setNewSources (RegisterSpecList newSources) {
88        RegisterSpecList origSources = insn.getSources();
89
90        if (origSources.size() != newSources.size()) {
91            throw new RuntimeException("Sources counts don't match");
92        }
93
94        insn = insn.withNewRegisters(getResult(), newSources);
95    }
96
97    /** {@inheritDoc} */
98    @Override
99    public NormalSsaInsn clone() {
100        return (NormalSsaInsn) super.clone();
101    }
102
103    /**
104     * Like rop.Insn.getSources().
105     *
106     * @return {@code null-ok;} sources list
107     */
108    @Override
109    public RegisterSpecList getSources() {
110        return insn.getSources();
111    }
112
113    /** {@inheritDoc} */
114    public String toHuman() {
115        return toRopInsn().toHuman();
116    }
117
118    /** {@inheritDoc} */
119    @Override
120    public Insn toRopInsn() {
121        return insn.withNewRegisters(getResult(), insn.getSources());
122    }
123
124    /**
125     * @return the Rop opcode for this insn
126     */
127    @Override
128    public Rop getOpcode() {
129        return insn.getOpcode();
130    }
131
132    /** {@inheritDoc} */
133    @Override
134    public Insn getOriginalRopInsn() {
135        return insn;
136    }
137
138    /** {@inheritDoc} */
139    @Override
140    public RegisterSpec getLocalAssignment() {
141        RegisterSpec assignment;
142
143        if (insn.getOpcode().getOpcode() == RegOps.MARK_LOCAL) {
144            assignment = insn.getSources().get(0);
145        } else {
146            assignment = getResult();
147        }
148
149        if (assignment == null) {
150            return null;
151        }
152
153        LocalItem local = assignment.getLocalItem();
154
155        if (local == null) {
156            return null;
157        }
158
159        return assignment;
160    }
161
162    /**
163     * Upgrades this insn to a version that represents the constant source
164     * literally. If the upgrade is not possible, this does nothing.
165     *
166     * @see Insn#withSourceLiteral
167     */
168    public void upgradeToLiteral() {
169        RegisterSpecList oldSources = insn.getSources();
170
171        insn = insn.withSourceLiteral();
172        getBlock().getParent().onSourcesChanged(this, oldSources);
173    }
174
175    /**
176     * @return true if this is a move (but not a move-operand) instruction
177     */
178    @Override
179    public boolean isNormalMoveInsn() {
180        return insn.getOpcode().getOpcode() == RegOps.MOVE;
181    }
182
183    /** {@inheritDoc} */
184    @Override
185    public boolean isMoveException() {
186        return insn.getOpcode().getOpcode() == RegOps.MOVE_EXCEPTION;
187    }
188
189    /** {@inheritDoc} */
190    @Override
191    public boolean canThrow() {
192        return insn.canThrow();
193    }
194
195    /** {@inheritDoc} */
196    @Override
197    public void accept(Visitor v) {
198        if (isNormalMoveInsn()) {
199            v.visitMoveInsn(this);
200        } else {
201            v.visitNonMoveInsn(this);
202        }
203    }
204
205    /** {@inheritDoc} */
206    @Override
207    public  boolean isPhiOrMove() {
208        return isNormalMoveInsn();
209    }
210
211    /**
212     * {@inheritDoc}
213     *
214     * TODO: Increase the scope of this.
215     */
216    @Override
217    public boolean hasSideEffect() {
218        Rop opcode = getOpcode();
219
220        if (opcode.getBranchingness() != Rop.BRANCH_NONE) {
221            return true;
222        }
223
224        boolean hasLocalSideEffect
225            = Optimizer.getPreserveLocals() && getLocalAssignment() != null;
226
227        switch (opcode.getOpcode()) {
228            case RegOps.MOVE_RESULT:
229            case RegOps.MOVE:
230            case RegOps.CONST:
231                return hasLocalSideEffect;
232            default:
233                return true;
234        }
235    }
236}
237