comparison graal/com.oracle.graal.compiler.amd64/src/com/oracle/graal/compiler/amd64/AMD64AddressLowering.java @ 21784:f4e1d958f1c3

[AMD64] Create AMD64 specific address nodes.
author Roland Schatz <roland.schatz@oracle.com>
date Mon, 08 Jun 2015 19:19:45 +0200
parents
children 2766fee1809a
comparison
equal deleted inserted replaced
21783:a858c5f56d8a 21784:f4e1d958f1c3
1 /*
2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package com.oracle.graal.compiler.amd64;
25
26 import com.oracle.graal.asm.*;
27 import com.oracle.graal.asm.amd64.AMD64Address.Scale;
28 import com.oracle.graal.nodes.*;
29 import com.oracle.graal.nodes.calc.*;
30 import com.oracle.graal.nodes.memory.address.*;
31 import com.oracle.graal.phases.common.AddressLoweringPhase.AddressLowering;
32 import com.oracle.jvmci.code.*;
33 import com.oracle.jvmci.meta.*;
34
35 public class AMD64AddressLowering extends AddressLowering {
36
37 private final CodeCacheProvider codeCache;
38
39 public AMD64AddressLowering(CodeCacheProvider codeCache) {
40 this.codeCache = codeCache;
41 }
42
43 @Override
44 public AddressNode lower(ValueNode address) {
45 return lower(address, null);
46 }
47
48 @Override
49 public AddressNode lower(ValueNode base, ValueNode offset) {
50 AMD64AddressNode ret = new AMD64AddressNode(base, offset);
51 boolean changed;
52 do {
53 changed = improve(ret);
54 } while (changed);
55 return base.graph().unique(ret);
56 }
57
58 protected boolean improve(AMD64AddressNode ret) {
59 ValueNode newBase = improveConstDisp(ret, ret.getBase(), 0);
60 if (newBase != ret.getBase()) {
61 ret.setBase(newBase);
62 return true;
63 }
64
65 ValueNode newIdx = improveConstDisp(ret, ret.getIndex(), ret.getScale().log2);
66 if (newIdx != ret.getIndex()) {
67 ret.setIndex(newIdx);
68 return true;
69 }
70
71 if (ret.getIndex() instanceof LeftShiftNode) {
72 LeftShiftNode shift = (LeftShiftNode) ret.getIndex();
73 if (shift.getY().isConstant()) {
74 int amount = ret.getScale().log2 + shift.getY().asJavaConstant().asInt();
75 Scale scale = Scale.fromShift(amount);
76 if (scale != null) {
77 ret.setIndex(shift.getX());
78 ret.setScale(scale);
79 return true;
80 }
81 }
82 }
83
84 if (ret.getScale() == Scale.Times1) {
85 if (ret.getBase() == null || ret.getIndex() == null) {
86 if (ret.getBase() instanceof AddNode) {
87 AddNode add = (AddNode) ret.getBase();
88 ret.setBase(add.getX());
89 ret.setIndex(add.getY());
90 return true;
91 } else if (ret.getIndex() instanceof AddNode) {
92 AddNode add = (AddNode) ret.getIndex();
93 ret.setBase(add.getX());
94 ret.setIndex(add.getY());
95 return true;
96 }
97 }
98
99 if (ret.getBase() instanceof LeftShiftNode && !(ret.getIndex() instanceof LeftShiftNode)) {
100 ValueNode tmp = ret.getBase();
101 ret.setBase(ret.getIndex());
102 ret.setIndex(tmp);
103 return true;
104 }
105 }
106
107 return false;
108 }
109
110 private ValueNode improveConstDisp(AMD64AddressNode address, ValueNode node, int shift) {
111 if (node == null) {
112 return null;
113 }
114
115 if (node.isConstant()) {
116 return improveConstDisp(address, node, node.asJavaConstant(), null, shift);
117 } else if (node instanceof AddNode) {
118 AddNode add = (AddNode) node;
119 if (add.getX().isConstant()) {
120 return improveConstDisp(address, node, add.getX().asJavaConstant(), add.getY(), shift);
121 } else if (add.getY().isConstant()) {
122 return improveConstDisp(address, node, add.getY().asJavaConstant(), add.getX(), shift);
123 }
124 }
125
126 return node;
127 }
128
129 private ValueNode improveConstDisp(AMD64AddressNode address, ValueNode original, JavaConstant c, ValueNode other, int shift) {
130 if (c.getKind().isNumericInteger() && !codeCache.needsDataPatch(c)) {
131 long disp = address.getDisplacement();
132 disp += c.asLong() << shift;
133 if (NumUtil.isInt(disp)) {
134 address.setDisplacement((int) disp);
135 return other;
136 }
137 }
138 return original;
139 }
140 }