69 import org.graalvm.compiler.nodes.calc.IntegerBelowNode; |
69 import org.graalvm.compiler.nodes.calc.IntegerBelowNode; |
70 import org.graalvm.compiler.nodes.calc.SubNode; |
70 import org.graalvm.compiler.nodes.calc.SubNode; |
71 import org.graalvm.compiler.nodes.extended.OpaqueNode; |
71 import org.graalvm.compiler.nodes.extended.OpaqueNode; |
72 import org.graalvm.compiler.nodes.memory.MemoryPhiNode; |
72 import org.graalvm.compiler.nodes.memory.MemoryPhiNode; |
73 import org.graalvm.compiler.nodes.util.GraphUtil; |
73 import org.graalvm.compiler.nodes.util.GraphUtil; |
74 |
74 import org.graalvm.compiler.nodes.util.IntegerHelper; |
75 import jdk.vm.ci.code.CodeUtil; |
|
76 |
75 |
77 public class LoopFragmentInside extends LoopFragment { |
76 public class LoopFragmentInside extends LoopFragment { |
78 |
77 |
79 /** |
78 /** |
80 * mergedInitializers. When an inside fragment's (loop)ends are merged to create a unique exit |
79 * mergedInitializers. When an inside fragment's (loop)ends are merged to create a unique exit |
204 if (opaque == null) { |
203 if (opaque == null) { |
205 opaque = new OpaqueNode(AddNode.add(counterStride, counterStride, NodeView.DEFAULT)); |
204 opaque = new OpaqueNode(AddNode.add(counterStride, counterStride, NodeView.DEFAULT)); |
206 ValueNode limit = counted.getLimit(); |
205 ValueNode limit = counted.getLimit(); |
207 int bits = ((IntegerStamp) limit.stamp(NodeView.DEFAULT)).getBits(); |
206 int bits = ((IntegerStamp) limit.stamp(NodeView.DEFAULT)).getBits(); |
208 ValueNode newLimit = SubNode.create(limit, opaque, NodeView.DEFAULT); |
207 ValueNode newLimit = SubNode.create(limit, opaque, NodeView.DEFAULT); |
|
208 IntegerHelper helper = counted.getCounterIntegerHelper(); |
209 LogicNode overflowCheck; |
209 LogicNode overflowCheck; |
210 ConstantNode extremum; |
210 ConstantNode extremum; |
211 if (counted.getDirection() == InductionVariable.Direction.Up) { |
211 if (counted.getDirection() == InductionVariable.Direction.Up) { |
212 // limit - counterStride could overflow negatively if limit - min < |
212 // limit - counterStride could overflow negatively if limit - min < |
213 // counterStride |
213 // counterStride |
214 extremum = ConstantNode.forIntegerBits(bits, CodeUtil.minValue(bits)); |
214 extremum = ConstantNode.forIntegerBits(bits, helper.minValue()); |
215 overflowCheck = IntegerBelowNode.create(SubNode.create(limit, extremum, NodeView.DEFAULT), opaque, NodeView.DEFAULT); |
215 overflowCheck = IntegerBelowNode.create(SubNode.create(limit, extremum, NodeView.DEFAULT), opaque, NodeView.DEFAULT); |
216 } else { |
216 } else { |
217 assert counted.getDirection() == InductionVariable.Direction.Down; |
217 assert counted.getDirection() == InductionVariable.Direction.Down; |
218 // limit - counterStride could overflow if max - limit < -counterStride |
218 // limit - counterStride could overflow if max - limit < -counterStride |
219 // i.e., counterStride < limit - max |
219 // i.e., counterStride < limit - max |
220 extremum = ConstantNode.forIntegerBits(bits, CodeUtil.maxValue(bits)); |
220 extremum = ConstantNode.forIntegerBits(bits, helper.maxValue()); |
221 overflowCheck = IntegerBelowNode.create(opaque, SubNode.create(limit, extremum, NodeView.DEFAULT), NodeView.DEFAULT); |
221 overflowCheck = IntegerBelowNode.create(opaque, SubNode.create(limit, extremum, NodeView.DEFAULT), NodeView.DEFAULT); |
222 } |
222 } |
223 newLimit = ConditionalNode.create(overflowCheck, extremum, newLimit, NodeView.DEFAULT); |
223 newLimit = ConditionalNode.create(overflowCheck, extremum, newLimit, NodeView.DEFAULT); |
224 CompareNode compareNode = (CompareNode) counted.getLimitTest().condition(); |
224 CompareNode compareNode = (CompareNode) counted.getLimitTest().condition(); |
225 compareNode.replaceFirstInput(limit, graph.addOrUniqueWithInputs(newLimit)); |
225 compareNode.replaceFirstInput(limit, graph.addOrUniqueWithInputs(newLimit)); |