{"id":15069410,"url":"https://github.com/engineeringsoftware/jog","last_synced_at":"2025-09-03T12:46:19.791Z","repository":{"id":182471419,"uuid":"639050714","full_name":"EngineeringSoftware/jog","owner":"EngineeringSoftware","description":"Pattern-Based Peephole Optimizations with Java JIT Tests","archived":false,"fork":false,"pushed_at":"2024-04-25T06:32:19.000Z","size":186,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-10T17:56:04.783Z","etag":null,"topics":["compiler","java","jit","jit-compiler","openjdk","optimization","pattern","peephole","shadow","test"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/EngineeringSoftware.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-10T16:47:41.000Z","updated_at":"2025-03-30T06:56:27.000Z","dependencies_parsed_at":"2023-07-20T06:01:16.439Z","dependency_job_id":"c8813e0b-3a0d-4feb-b324-19f60224de0b","html_url":"https://github.com/EngineeringSoftware/jog","commit_stats":null,"previous_names":["engineeringsoftware/jog"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/EngineeringSoftware/jog","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fjog","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fjog/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fjog/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fjog/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EngineeringSoftware","download_url":"https://codeload.github.com/EngineeringSoftware/jog/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EngineeringSoftware%2Fjog/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273445913,"owners_count":25107150,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compiler","java","jit","jit-compiler","openjdk","optimization","pattern","peephole","shadow","test"],"created_at":"2024-09-25T01:42:19.309Z","updated_at":"2025-09-03T12:46:19.693Z","avatar_url":"https://github.com/EngineeringSoftware.png","language":"Java","readme":"# JOG\n\nJOG is a framework that facilitates developing Java JIT peephole\noptimizations. JOG enables developers to write a pattern, in Java\nitself, that specifies desired code transformations by writing code\nbefore and after the optimization, as well as any necessary\npreconditions. Such patterns can be written in the same way that tests\nof the optimization are already written in OpenJDK. JOG translates\neach pattern into C/C++ code that can be integrated as a JIT\noptimization pass. JOG also generates Java tests for optimizations\nfrom patterns. Furthermore, JOG can automatically detect possible\nshadow relation between a pair of optimizations where the effect of\nthe shadowed optimization is overridden by another.\n\n## Table of contents\n\n1. [Requirements](#Requirements)\n2. [Example](#Example)\n3. [Hall of Fame](#Hall-of-Fame)\n4. [Citation](#Citation)\n5. [Contact](#Contact)\n\n## Requirements\n\n- Linux with GNU Bash (tested on Ubuntu 20.04 with GNU Bash 5.0.17(1)-release (x86_64-pc-linux-gnu))\n- JDK \u003e=11\n\nWe also provide a docker image with pre-built OpenJDK and cloned `jog`\nrepository.\n```bash\ndocke pull zzqut/jog:latest\n```\n\n## Example\n\nDevelopers write Java JIT peephole optimizations in _patterns_, using\nJOG's DSL fully embedded in Java. For example, `Example.java` contains\ntwo patterns `ADD2` and `ADD7`. `ADD2` represents a peephole\noptimization that transforms `(a - b) + (c - d)` to `(a + c) - (b +\nd)`, and `ADD7` expresses a peephole optimization that transforms\n`(a - b) + (b - c)` to `(a - c)`.\n\n```java\nimport jog.api.*;\n\nimport static jog.api.Action.*;\n\npublic class Example {\n\n    @Pattern\n    public void ADD2(long a, long b, long c, long d) {\n        before((a - b) + (c - d));\n        after((a + c) - (b + d));\n    }\n\n    @Pattern\n    public void ADD7(long a, long b, long c) {\n        before((a - b) + (b - c));\n        after(a - c);\n    }\n}\n```\n\nFrom the patterns, JOG\n\n1. Generates C/C++ code that can be integrated as JIT optimization\n   pass.\n\n   JOG translates every pattern into corresponding optimization pass\n   in C/C++.\n\n   `gen-code/addnode.cpp`\n   ```cpp\n   /* Automatically generated by jog from patterns: \n   ADD2,\n   ADD7. */\n   Node* AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {\n   // ADD2\n   {\n   Node* _JOG_in1 = in(1);\n   Node* _JOG_in11 = _JOG_in1 != NULL \u0026\u0026 1 \u003c _JOG_in1-\u003ereq() ? _JOG_in1-\u003ein(1) : NULL;\n   Node* _JOG_in12 = _JOG_in1 != NULL \u0026\u0026 2 \u003c _JOG_in1-\u003ereq() ? _JOG_in1-\u003ein(2) : NULL;\n   Node* _JOG_in2 = in(2);\n   Node* _JOG_in21 = _JOG_in2 != NULL \u0026\u0026 1 \u003c _JOG_in2-\u003ereq() ? _JOG_in2-\u003ein(1) : NULL;\n   Node* _JOG_in22 = _JOG_in2 != NULL \u0026\u0026 2 \u003c _JOG_in2-\u003ereq() ? _JOG_in2-\u003ein(2) : NULL;\n   if (_JOG_in1-\u003eOpcode() == Op_SubL\n       \u0026\u0026 _JOG_in2-\u003eOpcode() == Op_SubL) {\n   return new SubLNode(phase-\u003etransform(new AddLNode(_JOG_in11, _JOG_in21)), phase-\u003etransform(new AddLNode(_JOG_in12, _JOG_in22)));\n   }\n   }\n   \n   // ADD7\n   {\n   Node* _JOG_in1 = in(1);\n   Node* _JOG_in11 = _JOG_in1 != NULL \u0026\u0026 1 \u003c _JOG_in1-\u003ereq() ? _JOG_in1-\u003ein(1) : NULL;\n   Node* _JOG_in12 = _JOG_in1 != NULL \u0026\u0026 2 \u003c _JOG_in1-\u003ereq() ? _JOG_in1-\u003ein(2) : NULL;\n   Node* _JOG_in2 = in(2);\n   Node* _JOG_in21 = _JOG_in2 != NULL \u0026\u0026 1 \u003c _JOG_in2-\u003ereq() ? _JOG_in2-\u003ein(1) : NULL;\n   Node* _JOG_in22 = _JOG_in2 != NULL \u0026\u0026 2 \u003c _JOG_in2-\u003ereq() ? _JOG_in2-\u003ein(2) : NULL;\n   if (_JOG_in1-\u003eOpcode() == Op_SubL\n       \u0026\u0026 _JOG_in2-\u003eOpcode() == Op_SubL\n       \u0026\u0026 _JOG_in12 == _JOG_in21) {\n   return new SubLNode(_JOG_in11, _JOG_in22);\n   }\n   }\n   }\n   ```\n\n2. Generates Java tests that can be used to test such optimizations.\n\n   The IR test, written in Java using\n   [IR test framework](https://github.com/openjdk/jdk/tree/master/test/hotspot/jtreg/compiler/lib/ir_framework),\n   is a recommended approach in OpenJDK to testing JIT peephole\n   optimizations. JOG can automatically generate a IR test from every\n   pattern.\n\n   `gen-tests/TestAddLNode.java`\n   ```java\n   /*Automatically generated by jog from patterns: \n   ADD2,\n   ADD7.*/\n   package compiler.c2.irTests;\n   \n   import jdk.test.lib.Asserts;\n   import compiler.lib.ir_framework.*;\n   \n   /*@test\n   @library /test/lib /\n   @run driver compiler.c2.irTests.TestAddLNode*/\n   public class TestAddLNode {\n   \n       public static void main(String[] args) {\n           TestFramework.run();\n       }\n   \n       @Run(test = { \"testADD2\", \"testADD7\" })\n       public void runMethod() {\n           long a = RunInfo.getRandom().nextLong();\n           long b = RunInfo.getRandom().nextLong();\n           long c = RunInfo.getRandom().nextLong();\n           long d = RunInfo.getRandom().nextLong();\n           long min = Long.MIN_VALUE;\n           long max = Long.MAX_VALUE;\n           assertResult(0, 0, 0, 0);\n           assertResult(a, b, c, d);\n           assertResult(min, min, min, min);\n           assertResult(max, max, max, max);\n       }\n   \n       @DontCompile\n       public void assertResult(long a, long b, long c, long d) {\n           Asserts.assertEQ((a - b) + (c - d), testADD2(a, b, c, d));\n           Asserts.assertEQ((a - b) + (b - c), testADD7(a, b, c));\n       }\n   \n       // Checks (a - b) + (c - d) =\u003e (a + c) - (b + d)\n       @Test\n       @IR(counts = { IRNode.ADD, \"2\", IRNode.SUB, \"1\" })\n       public long testADD2(long a, long b, long c, long d) {\n           return (a - b) + (c - d);\n       }\n   \n       // Checks (a - b) + (b - c) =\u003e a - c\n       @Test\n       @IR(failOn = { IRNode.ADD })\n       @IR(counts = { IRNode.SUB, \"1\" })\n       public long testADD7(long a, long b, long c) {\n           return (a - b) + (b - c);\n       }\n   }\n   ```\n\n3. Detects any possible shadow relation between a pair of\n   optimizations.\n\n   Note that any expression matching `(a - b) + (c - a)` (`ADD2`) also\n   matches `(a - b) + (b - c)` (`ADD7`), which means `ADD2` can be\n   applied wherever `ADD7` can be applied, so the effect of `ADD2`\n   will shadow `ADD7` if `ADD2` is always applied before `ADD7` in a\n   compiler pass. JOG can automatically report the shadow relations.\n\n   `shadows.yml`\n   ```yaml\n   -\n     shadowing:\n       name: ADD2\n       before: \"(a - b) + (c - d)\"\n       precondition: []\n     shadowed:\n       -\n         name: ADD7\n         before: \"(a - b) + (b - c)\"\n         precondition: []\n   ```\n\nTo run the example, please run `./demo.sh`:\n```bash\n$ ./demo.sh\nBuilding JOG...\nReading the patterns from file Example.java...\nSee gen-code/ for generated compiler pass in C/C++.\nSee gen-tests/ for generated JIT optimization tests in Java.\nShadow relations:\nPattern ADD2 shadows ADD7\n```\n\n# Hall Of Fame\n\nHere is the list of pull requests we opened for OpenJDK:\n\nNew optimizations:\n\n- [#6441](https://github.com/openjdk/jdk/pull/6441):\n  8277882: New subnode ideal optimization: converting \"c0 - (x + c1)\"\n  into \"(c0 - c1) - x\".\n\n- [#6675](https://github.com/openjdk/jdk/pull/6675):\n  8278114: New addnode ideal optimization: converting \"x + x\" into \"x\n  \u003c\u003c 1\".\n\n- [#6858](https://github.com/openjdk/jdk/pull/6858):\n  8279607: Existing optimization \"~x+1\" -\u003e \"- x\" can be generalized to\n  \"~x+c\" -\u003e \"(c-1)-x\".\n\n- [#7795](https://github.com/openjdk/jdk/pull/7795):\n  8283094: Add Ideal transformation: x + (con - y) -\u003e (x - y) + con.\n\n- [#7376](https://github.com/openjdk/jdk/pull/7376):\n  8281453: New optimization: convert ~x into -1-x when ~x is used in\n  an arithmetic expression.\n\n- [#7395](https://github.com/openjdk/jdk/pull/7395):\n  8281518: New optimization: convert \"(x|y)-(x^y)\" into \"x\u0026y\".\n\n- [#16333](https://github.com/openjdk/jdk/pull/16333):\n  Add Ideal transformation: (~a) \u0026 (~b) =\u003e ~(a | b)\n\n- [#16334](https://github.com/openjdk/jdk/pull/16334):\n  Add Ideal transformation: (~a) | (~b) =\u003e ~(a \u0026 b)\n\nNew tests:\n\n- [#11049](https://github.com/openjdk/jdk/pull/11049):\n  8297384: Add IR tests for existing idealizations of arithmetic\n  nodes.\n\nFix detected shadowed optimizations:\n\n- [#6752](https://github.com/openjdk/jdk/pull/6752):\n  8278471: Remove unreached rules in AddNode::IdealIL.\n\n## Citation\n\nIf you use JOG in your research, please cite our [ISSTA'23\npaper](https://cptgit.github.io/dl/papers/zang23jog.pdf) and [ICSE'24 Demo paper](https://zzqpro.net/dl/papers/zang24jogtool.pdf).\n\n```bibtex\n@inproceedings{zang23jog,\n  author = {Zang, Zhiqiang and Thimmaiah, Aditya and Gligoric, Milos},\n  title = {Pattern-Based Peephole Optimizations with {J}ava {JIT} Tests},\n  booktitle = {International Symposium on Software Testing and Analysis},\n  pages = {64--75},\n  year= {2023},\n  doi = {10.1145/3597926.3598038},\n}\n@inproceedings{zang24jogtool,\n  title = {{JOG}: Java {JIT} Peephole Optimizations and Tests from Patterns},\n  author = {Zang, Zhiqiang and Thimmaiah, Aditya and Gligoric, Milos},\n  booktitle = {International Conference on Software Engineering, Tool Demonstrations Track},\n  pages = {to apear},\n  year= {2024},\n  doi = {10.1145/3639478.3640040}\n}\n```\n\n## Contact\n\nLet me ([Zhiqiang Zang](https://github.com/CptGit)) know if you have\nany questions.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineeringsoftware%2Fjog","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fengineeringsoftware%2Fjog","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengineeringsoftware%2Fjog/lists"}