1 /*
   2  * Copyright (c) 2014, 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 
  25 package org.graalvm.compiler.graph.test;
  26 
  27 import static org.graalvm.compiler.graph.test.matchers.NodeIterableContains.contains;
  28 import static org.graalvm.compiler.graph.test.matchers.NodeIterableIsEmpty.isEmpty;
  29 import static org.graalvm.compiler.graph.test.matchers.NodeIterableIsEmpty.isNotEmpty;
  30 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_IGNORED;
  31 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_IGNORED;
  32 import static org.junit.Assert.assertEquals;
  33 import static org.junit.Assert.assertThat;
  34 
  35 import org.junit.Test;
  36 
  37 import org.graalvm.compiler.graph.Graph;
  38 import org.graalvm.compiler.graph.Node;
  39 import org.graalvm.compiler.graph.NodeClass;
  40 import org.graalvm.compiler.nodeinfo.NodeInfo;
  41 import org.graalvm.compiler.options.OptionValues;
  42 
  43 public class NodeUsagesTests extends GraphTest {
  44 
  45     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  46     static final class Def extends Node {
  47         public static final NodeClass<Def> TYPE = NodeClass.create(Def.class);
  48 
  49         protected Def() {
  50             super(TYPE);
  51         }
  52     }
  53 
  54     @NodeInfo(cycles = CYCLES_IGNORED, size = SIZE_IGNORED)
  55     static final class Use extends Node {
  56         public static final NodeClass<Use> TYPE = NodeClass.create(Use.class);
  57         @Input Def in0;
  58         @Input Def in1;
  59         @Input Def in2;
  60 
  61         protected Use(Def in0, Def in1, Def in2) {
  62             super(TYPE);
  63             this.in0 = in0;
  64             this.in1 = in1;
  65             this.in2 = in2;
  66         }
  67 
  68     }
  69 
  70     @Test
  71     public void testReplaceAtUsages() {
  72         OptionValues options = getOptions();
  73         Graph graph = new Graph(options, getDebug(options));
  74         Def def0 = graph.add(new Def());
  75         Def def1 = graph.add(new Def());
  76         Use use0 = graph.add(new Use(def0, null, null));
  77         Use use1 = graph.add(new Use(null, def0, null));
  78         Use use2 = graph.add(new Use(null, null, def0));
  79 
  80         assertEquals(3, def0.getUsageCount());
  81         assertThat(def0.usages(), contains(use0));
  82         assertThat(def0.usages(), contains(use1));
  83         assertThat(def0.usages(), contains(use2));
  84 
  85         assertThat(def0.usages(), isNotEmpty());
  86         assertThat(def1.usages(), isEmpty());
  87 
  88         def0.replaceAtUsages(def1);
  89 
  90         assertThat(def0.usages(), isEmpty());
  91 
  92         assertEquals(3, def1.getUsageCount());
  93         assertThat(def1.usages(), contains(use0));
  94         assertThat(def1.usages(), contains(use1));
  95         assertThat(def1.usages(), contains(use2));
  96 
  97         assertThat(def1.usages(), isNotEmpty());
  98     }
  99 
 100     @Test
 101     public void testReplaceAtUsagesWithPredicateAll() {
 102         OptionValues options = getOptions();
 103         Graph graph = new Graph(options, getDebug(options));
 104         Def def0 = graph.add(new Def());
 105         Def def1 = graph.add(new Def());
 106         Use use0 = graph.add(new Use(def0, null, null));
 107         Use use1 = graph.add(new Use(null, def0, null));
 108         Use use2 = graph.add(new Use(null, null, def0));
 109 
 110         assertEquals(3, def0.getUsageCount());
 111         assertThat(def0.usages(), contains(use0));
 112         assertThat(def0.usages(), contains(use1));
 113         assertThat(def0.usages(), contains(use2));
 114 
 115         assertThat(def0.usages(), isNotEmpty());
 116         assertThat(def1.usages(), isEmpty());
 117 
 118         def0.replaceAtMatchingUsages(def1, u -> true);
 119 
 120         assertThat(def0.usages(), isEmpty());
 121 
 122         assertEquals(3, def1.getUsageCount());
 123         assertThat(def1.usages(), contains(use0));
 124         assertThat(def1.usages(), contains(use1));
 125         assertThat(def1.usages(), contains(use2));
 126 
 127         assertThat(def1.usages(), isNotEmpty());
 128     }
 129 
 130     @Test
 131     public void testReplaceAtUsagesWithPredicateNone() {
 132         OptionValues options = getOptions();
 133         Graph graph = new Graph(options, getDebug(options));
 134         Def def0 = graph.add(new Def());
 135         Def def1 = graph.add(new Def());
 136         Use use0 = graph.add(new Use(def0, null, null));
 137         Use use1 = graph.add(new Use(null, def0, null));
 138         Use use2 = graph.add(new Use(null, null, def0));
 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         assertThat(def1.usages(), isEmpty());
 147 
 148         def0.replaceAtMatchingUsages(def1, u -> false);
 149 
 150         assertThat(def1.usages(), isEmpty());
 151 
 152         assertEquals(3, def0.getUsageCount());
 153         assertThat(def0.usages(), contains(use0));
 154         assertThat(def0.usages(), contains(use1));
 155         assertThat(def0.usages(), contains(use2));
 156 
 157         assertThat(def0.usages(), isNotEmpty());
 158     }
 159 
 160     @Test
 161     public void testReplaceAtUsagesWithPredicate1() {
 162         OptionValues options = getOptions();
 163         Graph graph = new Graph(options, getDebug(options));
 164         Def def0 = graph.add(new Def());
 165         Def def1 = graph.add(new Def());
 166         Use use0 = graph.add(new Use(def0, null, null));
 167         Use use1 = graph.add(new Use(null, def0, null));
 168         Use use2 = graph.add(new Use(null, null, def0));
 169 
 170         assertEquals(3, def0.getUsageCount());
 171         assertThat(def0.usages(), contains(use0));
 172         assertThat(def0.usages(), contains(use1));
 173         assertThat(def0.usages(), contains(use2));
 174 
 175         assertThat(def0.usages(), isNotEmpty());
 176         assertThat(def1.usages(), isEmpty());
 177 
 178         def0.replaceAtMatchingUsages(def1, u -> u == use1);
 179 
 180         assertEquals(1, def1.getUsageCount());
 181         assertThat(def1.usages(), contains(use1));
 182 
 183         assertThat(def1.usages(), isNotEmpty());
 184 
 185         assertEquals(2, def0.getUsageCount());
 186         assertThat(def0.usages(), contains(use0));
 187         assertThat(def0.usages(), contains(use2));
 188 
 189         assertThat(def0.usages(), isNotEmpty());
 190     }
 191 
 192     @Test
 193     public void testReplaceAtUsagesWithPredicate2() {
 194         OptionValues options = getOptions();
 195         Graph graph = new Graph(options, getDebug(options));
 196         Def def0 = graph.add(new Def());
 197         Def def1 = graph.add(new Def());
 198         Use use0 = graph.add(new Use(def0, null, null));
 199         Use use1 = graph.add(new Use(null, def0, null));
 200         Use use2 = graph.add(new Use(null, null, def0));
 201 
 202         assertEquals(3, def0.getUsageCount());
 203         assertThat(def0.usages(), contains(use0));
 204         assertThat(def0.usages(), contains(use1));
 205         assertThat(def0.usages(), contains(use2));
 206 
 207         assertThat(def0.usages(), isNotEmpty());
 208         assertThat(def1.usages(), isEmpty());
 209 
 210         def0.replaceAtMatchingUsages(def1, u -> u == use2);
 211 
 212         assertEquals(1, def1.getUsageCount());
 213         assertThat(def1.usages(), contains(use2));
 214 
 215         assertThat(def1.usages(), isNotEmpty());
 216 
 217         assertEquals(2, def0.getUsageCount());
 218         assertThat(def0.usages(), contains(use0));
 219         assertThat(def0.usages(), contains(use1));
 220 
 221         assertThat(def0.usages(), isNotEmpty());
 222     }
 223 
 224     @Test
 225     public void testReplaceAtUsagesWithPredicate0() {
 226         OptionValues options = getOptions();
 227         Graph graph = new Graph(options, getDebug(options));
 228         Def def0 = graph.add(new Def());
 229         Def def1 = graph.add(new Def());
 230         Use use0 = graph.add(new Use(def0, null, null));
 231         Use use1 = graph.add(new Use(null, def0, null));
 232         Use use2 = graph.add(new Use(null, null, def0));
 233 
 234         assertEquals(3, def0.getUsageCount());
 235         assertThat(def0.usages(), contains(use0));
 236         assertThat(def0.usages(), contains(use1));
 237         assertThat(def0.usages(), contains(use2));
 238 
 239         assertThat(def0.usages(), isNotEmpty());
 240         assertThat(def1.usages(), isEmpty());
 241 
 242         def0.replaceAtMatchingUsages(def1, u -> u == use0);
 243 
 244         assertEquals(1, def1.getUsageCount());
 245         assertThat(def1.usages(), contains(use0));
 246 
 247         assertThat(def1.usages(), isNotEmpty());
 248 
 249         assertEquals(2, def0.getUsageCount());
 250         assertThat(def0.usages(), contains(use1));
 251         assertThat(def0.usages(), contains(use2));
 252 
 253         assertThat(def0.usages(), isNotEmpty());
 254     }
 255 
 256     @Test
 257     public void testReplaceAtUsagesWithPredicate02() {
 258         OptionValues options = getOptions();
 259         Graph graph = new Graph(options, getDebug(options));
 260         Def def0 = graph.add(new Def());
 261         Def def1 = graph.add(new Def());
 262         Use use0 = graph.add(new Use(def0, null, null));
 263         Use use1 = graph.add(new Use(null, def0, null));
 264         Use use2 = graph.add(new Use(null, null, def0));
 265 
 266         assertEquals(3, def0.getUsageCount());
 267         assertThat(def0.usages(), contains(use0));
 268         assertThat(def0.usages(), contains(use1));
 269         assertThat(def0.usages(), contains(use2));
 270 
 271         assertThat(def0.usages(), isNotEmpty());
 272         assertThat(def1.usages(), isEmpty());
 273 
 274         def0.replaceAtMatchingUsages(def1, u -> u != use1);
 275 
 276         assertEquals(1, def0.getUsageCount());
 277         assertThat(def0.usages(), contains(use1));
 278 
 279         assertThat(def0.usages(), isNotEmpty());
 280 
 281         assertEquals(2, def1.getUsageCount());
 282         assertThat(def1.usages(), contains(use0));
 283         assertThat(def1.usages(), contains(use2));
 284 
 285         assertThat(def1.usages(), isNotEmpty());
 286     }
 287 
 288     @Test
 289     public void testReplaceAtUsagesWithPredicate023() {
 290         OptionValues options = getOptions();
 291         Graph graph = new Graph(options, getDebug(options));
 292         Def def0 = graph.add(new Def());
 293         Def def1 = graph.add(new Def());
 294         Use use0 = graph.add(new Use(def0, null, null));
 295         Use use1 = graph.add(new Use(null, def0, null));
 296         Use use2 = graph.add(new Use(null, null, def0));
 297         Use use3 = graph.add(new Use(null, null, def0));
 298 
 299         assertEquals(4, def0.getUsageCount());
 300         assertThat(def0.usages(), contains(use0));
 301         assertThat(def0.usages(), contains(use1));
 302         assertThat(def0.usages(), contains(use2));
 303         assertThat(def0.usages(), contains(use3));
 304 
 305         assertThat(def0.usages(), isNotEmpty());
 306         assertThat(def1.usages(), isEmpty());
 307 
 308         def0.replaceAtMatchingUsages(def1, u -> u != use1);
 309 
 310         assertEquals(1, def0.getUsageCount());
 311         assertThat(def0.usages(), contains(use1));
 312 
 313         assertThat(def0.usages(), isNotEmpty());
 314 
 315         assertEquals(3, def1.getUsageCount());
 316         assertThat(def1.usages(), contains(use0));
 317         assertThat(def1.usages(), contains(use2));
 318         assertThat(def1.usages(), contains(use3));
 319 
 320         assertThat(def1.usages(), isNotEmpty());
 321     }
 322 
 323     @Test
 324     public void testReplaceAtUsagesWithPredicate013() {
 325         OptionValues options = getOptions();
 326         Graph graph = new Graph(options, getDebug(options));
 327         Def def0 = graph.add(new Def());
 328         Def def1 = graph.add(new Def());
 329         Use use0 = graph.add(new Use(def0, null, null));
 330         Use use1 = graph.add(new Use(null, def0, null));
 331         Use use2 = graph.add(new Use(null, null, def0));
 332         Use use3 = graph.add(new Use(null, null, def0));
 333 
 334         assertEquals(4, def0.getUsageCount());
 335         assertThat(def0.usages(), contains(use0));
 336         assertThat(def0.usages(), contains(use1));
 337         assertThat(def0.usages(), contains(use2));
 338         assertThat(def0.usages(), contains(use3));
 339 
 340         assertThat(def0.usages(), isNotEmpty());
 341         assertThat(def1.usages(), isEmpty());
 342 
 343         def0.replaceAtMatchingUsages(def1, u -> u != use2);
 344 
 345         assertEquals(1, def0.getUsageCount());
 346         assertThat(def0.usages(), contains(use2));
 347 
 348         assertThat(def0.usages(), isNotEmpty());
 349 
 350         assertEquals(3, def1.getUsageCount());
 351         assertThat(def1.usages(), contains(use0));
 352         assertThat(def1.usages(), contains(use1));
 353         assertThat(def1.usages(), contains(use3));
 354 
 355         assertThat(def1.usages(), isNotEmpty());
 356     }
 357 
 358     @Test
 359     public void testReplaceAtUsagesWithPredicate203() {
 360         OptionValues options = getOptions();
 361         Graph graph = new Graph(options, getDebug(options));
 362         Def def0 = graph.add(new Def());
 363         Def def1 = graph.add(new Def());
 364         Use use0 = graph.add(new Use(def0, null, null));
 365         Use use1 = graph.add(new Use(null, def0, null));
 366         Use use2 = graph.add(new Use(null, null, def0));
 367         Use use3 = graph.add(new Use(null, null, def0));
 368 
 369         assertEquals(4, def0.getUsageCount());
 370         assertThat(def0.usages(), contains(use0));
 371         assertThat(def0.usages(), contains(use1));
 372         assertThat(def0.usages(), contains(use2));
 373         assertThat(def0.usages(), contains(use3));
 374 
 375         assertThat(def0.usages(), isNotEmpty());
 376         assertThat(def1.usages(), isEmpty());
 377 
 378         def0.replaceAtMatchingUsages(def1, u -> u == use2);
 379 
 380         assertEquals(1, def1.getUsageCount());
 381         assertThat(def1.usages(), contains(use2));
 382 
 383         assertThat(def1.usages(), isNotEmpty());
 384 
 385         assertEquals(3, def0.getUsageCount());
 386         assertThat(def0.usages(), contains(use0));
 387         assertThat(def0.usages(), contains(use1));
 388         assertThat(def0.usages(), contains(use3));
 389 
 390         assertThat(def0.usages(), isNotEmpty());
 391     }
 392 
 393     @Test
 394     public void testReplaceAtUsagesWithPredicate01() {
 395         OptionValues options = getOptions();
 396         Graph graph = new Graph(options, getDebug(options));
 397         Def def0 = graph.add(new Def());
 398         Def def1 = graph.add(new Def());
 399         Use use0 = graph.add(new Use(def0, null, null));
 400         Use use1 = graph.add(new Use(null, def0, null));
 401         Use use2 = graph.add(new Use(null, null, def0));
 402 
 403         assertEquals(3, def0.getUsageCount());
 404         assertThat(def0.usages(), contains(use0));
 405         assertThat(def0.usages(), contains(use1));
 406         assertThat(def0.usages(), contains(use2));
 407 
 408         assertThat(def0.usages(), isNotEmpty());
 409         assertThat(def1.usages(), isEmpty());
 410 
 411         def0.replaceAtMatchingUsages(def1, u -> u != use2);
 412 
 413         assertEquals(1, def0.getUsageCount());
 414         assertThat(def0.usages(), contains(use2));
 415 
 416         assertThat(def0.usages(), isNotEmpty());
 417 
 418         assertEquals(2, def1.getUsageCount());
 419         assertThat(def1.usages(), contains(use0));
 420         assertThat(def1.usages(), contains(use1));
 421 
 422         assertThat(def1.usages(), isNotEmpty());
 423     }
 424 
 425     @Test
 426     public void testReplaceAtUsagesWithPredicate12() {
 427         OptionValues options = getOptions();
 428         Graph graph = new Graph(options, getDebug(options));
 429         Def def0 = graph.add(new Def());
 430         Def def1 = graph.add(new Def());
 431         Use use0 = graph.add(new Use(def0, null, null));
 432         Use use1 = graph.add(new Use(null, def0, null));
 433         Use use2 = graph.add(new Use(null, null, def0));
 434 
 435         assertEquals(3, def0.getUsageCount());
 436         assertThat(def0.usages(), contains(use0));
 437         assertThat(def0.usages(), contains(use1));
 438         assertThat(def0.usages(), contains(use2));
 439 
 440         assertThat(def0.usages(), isNotEmpty());
 441         assertThat(def1.usages(), isEmpty());
 442 
 443         def0.replaceAtMatchingUsages(def1, u -> u != use0);
 444 
 445         assertEquals(1, def0.getUsageCount());
 446         assertThat(def0.usages(), contains(use0));
 447 
 448         assertThat(def0.usages(), isNotEmpty());
 449 
 450         assertEquals(2, def1.getUsageCount());
 451         assertThat(def1.usages(), contains(use1));
 452         assertThat(def1.usages(), contains(use2));
 453 
 454         assertThat(def1.usages(), isNotEmpty());
 455     }
 456 }