182 */ 183 private static void optimizeAdd(ZeroExtendNode zeroExtendNode, ConstantNode constant, ValueNode other, LoopEx loop) { 184 StructuredGraph graph = zeroExtendNode.graph(); 185 AddNode addNode = graph.unique(new AddNode(signExtend(other, loop), ConstantNode.forLong(constant.asJavaConstant().asInt(), graph))); 186 zeroExtendNode.replaceAtUsages(addNode); 187 } 188 189 /** 190 * Create a sign extend for {@code input}, or zero extend if {@code input} can be proven 191 * positive. 192 */ 193 private static ValueNode signExtend(ValueNode input, LoopEx loop) { 194 StructuredGraph graph = input.graph(); 195 if (input instanceof PhiNode) { 196 EconomicMap<Node, InductionVariable> ivs = loop.getInductionVariables(); 197 InductionVariable inductionVariable = ivs.get(input); 198 if (inductionVariable != null && inductionVariable instanceof BasicInductionVariable) { 199 CountedLoopInfo countedLoopInfo = loop.counted(); 200 IntegerStamp initStamp = (IntegerStamp) inductionVariable.initNode().stamp(NodeView.DEFAULT); 201 if (initStamp.isPositive()) { 202 if (inductionVariable.isConstantExtremum()) { 203 long init = inductionVariable.constantInit(); 204 long stride = inductionVariable.constantStride(); 205 long extremum = inductionVariable.constantExtremum(); 206 207 if (init >= 0 && extremum >= 0) { 208 long shortestTrip = (extremum - init) / stride + 1; 209 if (countedLoopInfo.constantMaxTripCount().equals(shortestTrip)) { 210 return graph.unique(new ZeroExtendNode(input, INT_BITS, ADDRESS_BITS, true)); 211 } 212 } 213 } 214 if (countedLoopInfo.getCounter() == inductionVariable && inductionVariable.direction() == InductionVariable.Direction.Up && countedLoopInfo.getOverFlowGuard() != null) { 215 return graph.unique(new ZeroExtendNode(input, INT_BITS, ADDRESS_BITS, true)); 216 } 217 } 218 } 219 } 220 return input.graph().maybeAddOrUnique(SignExtendNode.create(input, ADDRESS_BITS, NodeView.DEFAULT)); 221 } 222 223 private static boolean applicableToImplicitZeroExtend(ZeroExtendNode zeroExtendNode) { 224 return zeroExtendNode.isInputAlwaysPositive() && zeroExtendNode.getInputBits() == INT_BITS && zeroExtendNode.getResultBits() == ADDRESS_BITS; 225 } 226 227 private static ValueNode tryImplicitZeroExtend(ValueNode input) { 228 if (input instanceof ZeroExtendNode) { 229 ZeroExtendNode zeroExtendNode = (ZeroExtendNode) input; 230 if (applicableToImplicitZeroExtend(zeroExtendNode)) { 231 return zeroExtendNode.getValue(); 232 } 233 } 234 return input; | 182 */ 183 private static void optimizeAdd(ZeroExtendNode zeroExtendNode, ConstantNode constant, ValueNode other, LoopEx loop) { 184 StructuredGraph graph = zeroExtendNode.graph(); 185 AddNode addNode = graph.unique(new AddNode(signExtend(other, loop), ConstantNode.forLong(constant.asJavaConstant().asInt(), graph))); 186 zeroExtendNode.replaceAtUsages(addNode); 187 } 188 189 /** 190 * Create a sign extend for {@code input}, or zero extend if {@code input} can be proven 191 * positive. 192 */ 193 private static ValueNode signExtend(ValueNode input, LoopEx loop) { 194 StructuredGraph graph = input.graph(); 195 if (input instanceof PhiNode) { 196 EconomicMap<Node, InductionVariable> ivs = loop.getInductionVariables(); 197 InductionVariable inductionVariable = ivs.get(input); 198 if (inductionVariable != null && inductionVariable instanceof BasicInductionVariable) { 199 CountedLoopInfo countedLoopInfo = loop.counted(); 200 IntegerStamp initStamp = (IntegerStamp) inductionVariable.initNode().stamp(NodeView.DEFAULT); 201 if (initStamp.isPositive()) { 202 if (inductionVariable.isConstantExtremum() && countedLoopInfo.counterNeverOverflows()) { 203 long init = inductionVariable.constantInit(); 204 long stride = inductionVariable.constantStride(); 205 long extremum = inductionVariable.constantExtremum(); 206 207 if (init >= 0 && extremum >= 0) { 208 long shortestTrip = (extremum - init) / stride + 1; 209 if (countedLoopInfo.constantMaxTripCount().equals(shortestTrip)) { 210 return graph.unique(new ZeroExtendNode(input, INT_BITS, ADDRESS_BITS, true)); 211 } 212 } 213 } 214 if (countedLoopInfo.getCounter() == inductionVariable && 215 inductionVariable.direction() == InductionVariable.Direction.Up && 216 (countedLoopInfo.getOverFlowGuard() != null || countedLoopInfo.counterNeverOverflows())) { 217 return graph.unique(new ZeroExtendNode(input, INT_BITS, ADDRESS_BITS, true)); 218 } 219 } 220 } 221 } 222 return input.graph().maybeAddOrUnique(SignExtendNode.create(input, ADDRESS_BITS, NodeView.DEFAULT)); 223 } 224 225 private static boolean applicableToImplicitZeroExtend(ZeroExtendNode zeroExtendNode) { 226 return zeroExtendNode.isInputAlwaysPositive() && zeroExtendNode.getInputBits() == INT_BITS && zeroExtendNode.getResultBits() == ADDRESS_BITS; 227 } 228 229 private static ValueNode tryImplicitZeroExtend(ValueNode input) { 230 if (input instanceof ZeroExtendNode) { 231 ZeroExtendNode zeroExtendNode = (ZeroExtendNode) input; 232 if (applicableToImplicitZeroExtend(zeroExtendNode)) { 233 return zeroExtendNode.getValue(); 234 } 235 } 236 return input; |