001/*
002 * Copyright (c) 2014, 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.graph.test;
024
025import static com.oracle.graal.graph.test.matchers.NodeIterableContains.*;
026import static com.oracle.graal.graph.test.matchers.NodeIterableIsEmpty.*;
027import static org.junit.Assert.*;
028
029import org.junit.*;
030
031import com.oracle.graal.graph.*;
032import com.oracle.graal.nodeinfo.*;
033
034public class NodeUsagesTests {
035
036    @NodeInfo
037    static final class Def extends Node {
038        public static final NodeClass<Def> TYPE = NodeClass.create(Def.class);
039
040        protected Def() {
041            super(TYPE);
042        }
043    }
044
045    @NodeInfo
046    static final class Use extends Node {
047        public static final NodeClass<Use> TYPE = NodeClass.create(Use.class);
048        @Input Def in0;
049        @Input Def in1;
050        @Input Def in2;
051
052        public Use(Def in0, Def in1, Def in2) {
053            super(TYPE);
054            this.in0 = in0;
055            this.in1 = in1;
056            this.in2 = in2;
057        }
058
059    }
060
061    @Test
062    public void testReplaceAtUsages() {
063        Graph graph = new Graph();
064        Def def0 = graph.add(new Def());
065        Def def1 = graph.add(new Def());
066        Use use0 = graph.add(new Use(def0, null, null));
067        Use use1 = graph.add(new Use(null, def0, null));
068        Use use2 = graph.add(new Use(null, null, def0));
069
070        assertEquals(3, def0.getUsageCount());
071        assertThat(def0.usages(), contains(use0));
072        assertThat(def0.usages(), contains(use1));
073        assertThat(def0.usages(), contains(use2));
074
075        assertThat(def0.usages(), isNotEmpty());
076        assertThat(def1.usages(), isEmpty());
077
078        def0.replaceAtUsages(def1);
079
080        assertThat(def0.usages(), isEmpty());
081
082        assertEquals(3, def1.getUsageCount());
083        assertThat(def1.usages(), contains(use0));
084        assertThat(def1.usages(), contains(use1));
085        assertThat(def1.usages(), contains(use2));
086
087        assertThat(def1.usages(), isNotEmpty());
088    }
089
090    @Test
091    public void testReplaceAtUsagesWithPredicateAll() {
092        Graph graph = new Graph();
093        Def def0 = graph.add(new Def());
094        Def def1 = graph.add(new Def());
095        Use use0 = graph.add(new Use(def0, null, null));
096        Use use1 = graph.add(new Use(null, def0, null));
097        Use use2 = graph.add(new Use(null, null, def0));
098
099        assertEquals(3, def0.getUsageCount());
100        assertThat(def0.usages(), contains(use0));
101        assertThat(def0.usages(), contains(use1));
102        assertThat(def0.usages(), contains(use2));
103
104        assertThat(def0.usages(), isNotEmpty());
105        assertThat(def1.usages(), isEmpty());
106
107        def0.replaceAtMatchingUsages(def1, u -> true);
108
109        assertThat(def0.usages(), isEmpty());
110
111        assertEquals(3, def1.getUsageCount());
112        assertThat(def1.usages(), contains(use0));
113        assertThat(def1.usages(), contains(use1));
114        assertThat(def1.usages(), contains(use2));
115
116        assertThat(def1.usages(), isNotEmpty());
117    }
118
119    @Test
120    public void testReplaceAtUsagesWithPredicateNone() {
121        Graph graph = new Graph();
122        Def def0 = graph.add(new Def());
123        Def def1 = graph.add(new Def());
124        Use use0 = graph.add(new Use(def0, null, null));
125        Use use1 = graph.add(new Use(null, def0, null));
126        Use use2 = graph.add(new Use(null, null, def0));
127
128        assertEquals(3, def0.getUsageCount());
129        assertThat(def0.usages(), contains(use0));
130        assertThat(def0.usages(), contains(use1));
131        assertThat(def0.usages(), contains(use2));
132
133        assertThat(def0.usages(), isNotEmpty());
134        assertThat(def1.usages(), isEmpty());
135
136        def0.replaceAtMatchingUsages(def1, u -> false);
137
138        assertThat(def1.usages(), isEmpty());
139
140        assertEquals(3, def0.getUsageCount());
141        assertThat(def0.usages(), contains(use0));
142        assertThat(def0.usages(), contains(use1));
143        assertThat(def0.usages(), contains(use2));
144
145        assertThat(def0.usages(), isNotEmpty());
146    }
147
148    @Test
149    public void testReplaceAtUsagesWithPredicate1() {
150        Graph graph = new Graph();
151        Def def0 = graph.add(new Def());
152        Def def1 = graph.add(new Def());
153        Use use0 = graph.add(new Use(def0, null, null));
154        Use use1 = graph.add(new Use(null, def0, null));
155        Use use2 = graph.add(new Use(null, null, def0));
156
157        assertEquals(3, def0.getUsageCount());
158        assertThat(def0.usages(), contains(use0));
159        assertThat(def0.usages(), contains(use1));
160        assertThat(def0.usages(), contains(use2));
161
162        assertThat(def0.usages(), isNotEmpty());
163        assertThat(def1.usages(), isEmpty());
164
165        def0.replaceAtMatchingUsages(def1, u -> u == use1);
166
167        assertEquals(1, def1.getUsageCount());
168        assertThat(def1.usages(), contains(use1));
169
170        assertThat(def1.usages(), isNotEmpty());
171
172        assertEquals(2, def0.getUsageCount());
173        assertThat(def0.usages(), contains(use0));
174        assertThat(def0.usages(), contains(use2));
175
176        assertThat(def0.usages(), isNotEmpty());
177    }
178
179    @Test
180    public void testReplaceAtUsagesWithPredicate2() {
181        Graph graph = new Graph();
182        Def def0 = graph.add(new Def());
183        Def def1 = graph.add(new Def());
184        Use use0 = graph.add(new Use(def0, null, null));
185        Use use1 = graph.add(new Use(null, def0, null));
186        Use use2 = graph.add(new Use(null, null, def0));
187
188        assertEquals(3, def0.getUsageCount());
189        assertThat(def0.usages(), contains(use0));
190        assertThat(def0.usages(), contains(use1));
191        assertThat(def0.usages(), contains(use2));
192
193        assertThat(def0.usages(), isNotEmpty());
194        assertThat(def1.usages(), isEmpty());
195
196        def0.replaceAtMatchingUsages(def1, u -> u == use2);
197
198        assertEquals(1, def1.getUsageCount());
199        assertThat(def1.usages(), contains(use2));
200
201        assertThat(def1.usages(), isNotEmpty());
202
203        assertEquals(2, def0.getUsageCount());
204        assertThat(def0.usages(), contains(use0));
205        assertThat(def0.usages(), contains(use1));
206
207        assertThat(def0.usages(), isNotEmpty());
208    }
209
210    @Test
211    public void testReplaceAtUsagesWithPredicate0() {
212        Graph graph = new Graph();
213        Def def0 = graph.add(new Def());
214        Def def1 = graph.add(new Def());
215        Use use0 = graph.add(new Use(def0, null, null));
216        Use use1 = graph.add(new Use(null, def0, null));
217        Use use2 = graph.add(new Use(null, null, def0));
218
219        assertEquals(3, def0.getUsageCount());
220        assertThat(def0.usages(), contains(use0));
221        assertThat(def0.usages(), contains(use1));
222        assertThat(def0.usages(), contains(use2));
223
224        assertThat(def0.usages(), isNotEmpty());
225        assertThat(def1.usages(), isEmpty());
226
227        def0.replaceAtMatchingUsages(def1, u -> u == use0);
228
229        assertEquals(1, def1.getUsageCount());
230        assertThat(def1.usages(), contains(use0));
231
232        assertThat(def1.usages(), isNotEmpty());
233
234        assertEquals(2, def0.getUsageCount());
235        assertThat(def0.usages(), contains(use1));
236        assertThat(def0.usages(), contains(use2));
237
238        assertThat(def0.usages(), isNotEmpty());
239    }
240
241    @Test
242    public void testReplaceAtUsagesWithPredicate02() {
243        Graph graph = new Graph();
244        Def def0 = graph.add(new Def());
245        Def def1 = graph.add(new Def());
246        Use use0 = graph.add(new Use(def0, null, null));
247        Use use1 = graph.add(new Use(null, def0, null));
248        Use use2 = graph.add(new Use(null, null, def0));
249
250        assertEquals(3, def0.getUsageCount());
251        assertThat(def0.usages(), contains(use0));
252        assertThat(def0.usages(), contains(use1));
253        assertThat(def0.usages(), contains(use2));
254
255        assertThat(def0.usages(), isNotEmpty());
256        assertThat(def1.usages(), isEmpty());
257
258        def0.replaceAtMatchingUsages(def1, u -> u != use1);
259
260        assertEquals(1, def0.getUsageCount());
261        assertThat(def0.usages(), contains(use1));
262
263        assertThat(def0.usages(), isNotEmpty());
264
265        assertEquals(2, def1.getUsageCount());
266        assertThat(def1.usages(), contains(use0));
267        assertThat(def1.usages(), contains(use2));
268
269        assertThat(def1.usages(), isNotEmpty());
270    }
271
272    @Test
273    public void testReplaceAtUsagesWithPredicate023() {
274        Graph graph = new Graph();
275        Def def0 = graph.add(new Def());
276        Def def1 = graph.add(new Def());
277        Use use0 = graph.add(new Use(def0, null, null));
278        Use use1 = graph.add(new Use(null, def0, null));
279        Use use2 = graph.add(new Use(null, null, def0));
280        Use use3 = graph.add(new Use(null, null, def0));
281
282        assertEquals(4, def0.getUsageCount());
283        assertThat(def0.usages(), contains(use0));
284        assertThat(def0.usages(), contains(use1));
285        assertThat(def0.usages(), contains(use2));
286        assertThat(def0.usages(), contains(use3));
287
288        assertThat(def0.usages(), isNotEmpty());
289        assertThat(def1.usages(), isEmpty());
290
291        def0.replaceAtMatchingUsages(def1, u -> u != use1);
292
293        assertEquals(1, def0.getUsageCount());
294        assertThat(def0.usages(), contains(use1));
295
296        assertThat(def0.usages(), isNotEmpty());
297
298        assertEquals(3, def1.getUsageCount());
299        assertThat(def1.usages(), contains(use0));
300        assertThat(def1.usages(), contains(use2));
301        assertThat(def1.usages(), contains(use3));
302
303        assertThat(def1.usages(), isNotEmpty());
304    }
305
306    @Test
307    public void testReplaceAtUsagesWithPredicate013() {
308        Graph graph = new Graph();
309        Def def0 = graph.add(new Def());
310        Def def1 = graph.add(new Def());
311        Use use0 = graph.add(new Use(def0, null, null));
312        Use use1 = graph.add(new Use(null, def0, null));
313        Use use2 = graph.add(new Use(null, null, def0));
314        Use use3 = graph.add(new Use(null, null, def0));
315
316        assertEquals(4, def0.getUsageCount());
317        assertThat(def0.usages(), contains(use0));
318        assertThat(def0.usages(), contains(use1));
319        assertThat(def0.usages(), contains(use2));
320        assertThat(def0.usages(), contains(use3));
321
322        assertThat(def0.usages(), isNotEmpty());
323        assertThat(def1.usages(), isEmpty());
324
325        def0.replaceAtMatchingUsages(def1, u -> u != use2);
326
327        assertEquals(1, def0.getUsageCount());
328        assertThat(def0.usages(), contains(use2));
329
330        assertThat(def0.usages(), isNotEmpty());
331
332        assertEquals(3, def1.getUsageCount());
333        assertThat(def1.usages(), contains(use0));
334        assertThat(def1.usages(), contains(use1));
335        assertThat(def1.usages(), contains(use3));
336
337        assertThat(def1.usages(), isNotEmpty());
338    }
339
340    @Test
341    public void testReplaceAtUsagesWithPredicate203() {
342        Graph graph = new Graph();
343        Def def0 = graph.add(new Def());
344        Def def1 = graph.add(new Def());
345        Use use0 = graph.add(new Use(def0, null, null));
346        Use use1 = graph.add(new Use(null, def0, null));
347        Use use2 = graph.add(new Use(null, null, def0));
348        Use use3 = graph.add(new Use(null, null, def0));
349
350        assertEquals(4, def0.getUsageCount());
351        assertThat(def0.usages(), contains(use0));
352        assertThat(def0.usages(), contains(use1));
353        assertThat(def0.usages(), contains(use2));
354        assertThat(def0.usages(), contains(use3));
355
356        assertThat(def0.usages(), isNotEmpty());
357        assertThat(def1.usages(), isEmpty());
358
359        def0.replaceAtMatchingUsages(def1, u -> u == use2);
360
361        assertEquals(1, def1.getUsageCount());
362        assertThat(def1.usages(), contains(use2));
363
364        assertThat(def1.usages(), isNotEmpty());
365
366        assertEquals(3, def0.getUsageCount());
367        assertThat(def0.usages(), contains(use0));
368        assertThat(def0.usages(), contains(use1));
369        assertThat(def0.usages(), contains(use3));
370
371        assertThat(def0.usages(), isNotEmpty());
372    }
373
374    @Test
375    public void testReplaceAtUsagesWithPredicate01() {
376        Graph graph = new Graph();
377        Def def0 = graph.add(new Def());
378        Def def1 = graph.add(new Def());
379        Use use0 = graph.add(new Use(def0, null, null));
380        Use use1 = graph.add(new Use(null, def0, null));
381        Use use2 = graph.add(new Use(null, null, def0));
382
383        assertEquals(3, def0.getUsageCount());
384        assertThat(def0.usages(), contains(use0));
385        assertThat(def0.usages(), contains(use1));
386        assertThat(def0.usages(), contains(use2));
387
388        assertThat(def0.usages(), isNotEmpty());
389        assertThat(def1.usages(), isEmpty());
390
391        def0.replaceAtMatchingUsages(def1, u -> u != use2);
392
393        assertEquals(1, def0.getUsageCount());
394        assertThat(def0.usages(), contains(use2));
395
396        assertThat(def0.usages(), isNotEmpty());
397
398        assertEquals(2, def1.getUsageCount());
399        assertThat(def1.usages(), contains(use0));
400        assertThat(def1.usages(), contains(use1));
401
402        assertThat(def1.usages(), isNotEmpty());
403    }
404
405    @Test
406    public void testReplaceAtUsagesWithPredicate12() {
407        Graph graph = new Graph();
408        Def def0 = graph.add(new Def());
409        Def def1 = graph.add(new Def());
410        Use use0 = graph.add(new Use(def0, null, null));
411        Use use1 = graph.add(new Use(null, def0, null));
412        Use use2 = graph.add(new Use(null, null, def0));
413
414        assertEquals(3, def0.getUsageCount());
415        assertThat(def0.usages(), contains(use0));
416        assertThat(def0.usages(), contains(use1));
417        assertThat(def0.usages(), contains(use2));
418
419        assertThat(def0.usages(), isNotEmpty());
420        assertThat(def1.usages(), isEmpty());
421
422        def0.replaceAtMatchingUsages(def1, u -> u != use0);
423
424        assertEquals(1, def0.getUsageCount());
425        assertThat(def0.usages(), contains(use0));
426
427        assertThat(def0.usages(), isNotEmpty());
428
429        assertEquals(2, def1.getUsageCount());
430        assertThat(def1.usages(), contains(use1));
431        assertThat(def1.usages(), contains(use2));
432
433        assertThat(def1.usages(), isNotEmpty());
434    }
435}