Skip to main content
Redhat Developers  Logo
  • Products

    Platforms

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat AI
      Red Hat AI
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • View All Red Hat Products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat Developer Hub
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat OpenShift Local
    • Red Hat Developer Sandbox

      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Secure Development & Architectures

      • Security
      • Secure coding
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • Product Documentation
    • API Catalog
    • Legacy Documentation
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

A gentle introduction to jump threading optimizations

March 13, 2019
Aldy Hernandez
Related topics:
C, C#, C++

Share:

    As part of the GCC developers' on-demand range work for GCC 10, I've been playing with improving the backward jump threader so it can thread paths that are range-dependent. This, in turn, had me looking at the jump threader, which is a part of the compiler I've been carefully avoiding for years. If, like me, you're curious about compiler optimizations, but are jump-threading-agnostic, perhaps you'll be interested in this short introduction.

    At the highest level, jump threading's major goal is to reduce the number of dynamically executed jumps on different paths through the program's control flow graph. Often this results in improved performance due to the reduction of conditionals, which in turn enables further optimizations. Typically, for every runtime branch eliminated by jump threading, two or three other runtime instructions are eliminated.

    Simplification of control flow also dramatically reduces the false-positive rates from warnings such as -Wuninitialized. False positives from -Wuninitialized typically occur because there are paths through the control flow graph that cannot occur at runtime, but remain in the internal representation of the code.

    GCC developers have found a strong correlation between false positives from -Wuninitialized and missed optimization opportunities. Thus, the GCC developers are keenly interested in any false-positive report for -Wuninitialized.

    The classic jump thread example is a simple jump to jump optimization. For instance, it can transform the following:

      if (a > 5)
        goto j;
      stuff ();
      stuff ();
    j:
      goto somewhere;
    

    into the more optimized sequence below:

      if (a > 5)
        goto somewhere;
      stuff ();
      stuff ();
    j:
      goto somewhere;
    

    However, jump threading can also thread two partial conditions that are known to overlap:

    void foo(int a, int b, int c)
    {
      if (a && b)
        foo ();
      if (b || c)
        bar ();
    }

    The above is transformed into:

    void foo(int a, int b, int c)
    {
      if (a && b) {
        foo ();
        goto skip;
      }
      if (b || c) {
    skip:
        bar ();
      }
    }
    

    An even more interesting sequence is when jump threading duplicates blocks to avoid branching. Consider a slightly tweaked version of the above:

    void foo(int a, int b, int c)
    {
      if (a && b)
        foo ();
      tweak ();
      if (b || c)
      bar ();
    }
    

    The compiler cannot easily thread the above, unless it duplicates tweak(), making the resulting code larger:

    void foo(int a, int b, int c)
    {
      if (a && b) {
        foo ();
        tweak ();
        goto skip;
      }
      tweak ();
      if (b || c) {
    skip:
        bar ();
      }
    }
    

    Thanks to the code duplication, the compiler is able to join the two overlapping conditionals with no change in semantics. By the way, this is the ultimate goal of jump threading: avoiding expensive conditional branches, even though it may come at the expense of more code.

    GCC does have a limit for how many instructions or basic blocks it is willing to duplicate in its quest for faster run speeds. Various compilation tweaks force the jump threader to consider longer sequences. One such option is --param max-fsm-paths-insns=500, which causes the threader to thread sequences that could potentially duplicate up to 500 instructions per sequence (as opposed to the 100 default). Also there is --param max-fsm-thread-length, which similarly expands the threader maximum, but by basic block length instead of instruction length. As with all --param options, use them for self-amusement and clever party tricks, as they are subject to change without notice.

    Jump threading is enabled by default for -O2 and above, but unfortunately, it is intertwined with the various value range propagation (VRP) passes and there is no independent way of turning it off. The deceptive -fno-thread-jumps flag turns off jump threading only in the low-level RTL optimizers, which handle only a minuscule number of jump threads in a typical compilation. Making -fno-thread-jumps applicable to all jump threading throughout the compiler, as well as disentangling VRP from jump threading as a whole, are on our to-do list.

    If you'd like to see the jump threader in action, compile a sufficiently complex program with -fdump-tree-all-details -O2 and look at *.c*{ethread, thread1, thread2, thread3, thread4} as well as the VRP dumps (*.c*{vrp1, vrp2}). You should see things like Threaded jump 3 --> 4 to 7.

    Enjoy!

    Also read

    • Understanding GCC warnings

    More articles for C/C++ developers

    • Usability improvements in GCC 9 (GCC 9 is scheduled to be available in Fedora 30)
    • Usability improvements in GCC 8 (GCC 8 is available now for Red Hat Enterprise Linux 6, 7, and 8 Beta.)
    • How to install GCC 8 and Clang/LLVM 6 on Red Hat Enterprise Linux 7
    • Recommended compiler and linker flags for GCC
    • Getting started with Clang/LLVM
    • Detecting String Truncation with GCC 8
    • Implicit fall through detection with GCC 7
    • Memory error detection using GCC 7
    • Diagnosing Function Pointer Security Flaws with a GCC plugin
    • Toward a Better Use of C11 Atomics – Part 1
    Last updated: March 11, 2019

    Recent Posts

    • What qualifies for Red Hat Developer Subscription for Teams?

    • How to run OpenAI's gpt-oss models locally with RamaLama

    • Using DNS over TLS in OpenShift to secure communications

    • Scaling DeepSeek and Sparse MoE models in vLLM with llm-d

    • Multicluster authentication with Ansible Automation Platform

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2025 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue