001/* 002 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.loop; 024 025import static com.oracle.graal.loop.MathUtil.*; 026import jdk.internal.jvmci.common.*; 027 028import com.oracle.graal.compiler.common.type.*; 029import com.oracle.graal.nodes.*; 030import com.oracle.graal.nodes.calc.*; 031 032public class DerivedOffsetInductionVariable extends DerivedInductionVariable { 033 034 private final ValueNode offset; 035 private final BinaryArithmeticNode<?> value; 036 037 public DerivedOffsetInductionVariable(LoopEx loop, InductionVariable base, ValueNode offset, BinaryArithmeticNode<?> value) { 038 super(loop, base); 039 this.offset = offset; 040 this.value = value; 041 } 042 043 public ValueNode getOffset() { 044 return offset; 045 } 046 047 @Override 048 public Direction direction() { 049 return base.direction(); 050 } 051 052 @Override 053 public ValueNode valueNode() { 054 return value; 055 } 056 057 @Override 058 public boolean isConstantInit() { 059 return offset.isConstant() && base.isConstantInit(); 060 } 061 062 @Override 063 public boolean isConstantStride() { 064 return base.isConstantStride(); 065 } 066 067 @Override 068 public long constantInit() { 069 return op(base.constantInit(), offset.asJavaConstant().asLong()); 070 } 071 072 @Override 073 public long constantStride() { 074 if (value instanceof SubNode && base.valueNode() == value.getY()) { 075 return -base.constantStride(); 076 } 077 return base.constantStride(); 078 } 079 080 @Override 081 public ValueNode initNode() { 082 return op(base.initNode(), offset); 083 } 084 085 @Override 086 public ValueNode strideNode() { 087 if (value instanceof SubNode && base.valueNode() == value.getY()) { 088 return graph().unique(new NegateNode(base.strideNode())); 089 } 090 return base.strideNode(); 091 } 092 093 @Override 094 public ValueNode extremumNode(boolean assumePositiveTripCount, Stamp stamp) { 095 return op(base.extremumNode(assumePositiveTripCount, stamp), IntegerConvertNode.convert(offset, stamp, graph())); 096 } 097 098 @Override 099 public ValueNode exitValueNode() { 100 return op(base.exitValueNode(), offset); 101 } 102 103 @Override 104 public boolean isConstantExtremum() { 105 return offset.isConstant() && base.isConstantExtremum(); 106 } 107 108 @Override 109 public long constantExtremum() { 110 return op(base.constantExtremum(), offset.asJavaConstant().asLong()); 111 } 112 113 private long op(long b, long o) { 114 if (value instanceof AddNode) { 115 return b + o; 116 } 117 if (value instanceof SubNode) { 118 if (base.valueNode() == value.getX()) { 119 return b - o; 120 } else { 121 assert base.valueNode() == value.getY(); 122 return o - b; 123 } 124 } 125 throw JVMCIError.shouldNotReachHere(); 126 } 127 128 private ValueNode op(ValueNode b, ValueNode o) { 129 if (value instanceof AddNode) { 130 return add(graph(), b, o); 131 } 132 if (value instanceof SubNode) { 133 if (base.valueNode() == value.getX()) { 134 return sub(graph(), b, o); 135 } else { 136 assert base.valueNode() == value.getY(); 137 return sub(graph(), o, b); 138 } 139 } 140 throw JVMCIError.shouldNotReachHere(); 141 } 142 143 @Override 144 public void deleteUnusedNodes() { 145 } 146 147 @Override 148 public String toString() { 149 return String.format("DerivedOffsetInductionVariable base (%s) %s %s", base, value.getNodeClass().shortName(), offset); 150 } 151}