Lably
Skip to content

Use-define chain

  • 13 min read
  • 5 chapters

Related on Lably

Through Computer science, Data structure and Variable (high-level programming language)

A counterpart of a UD Chain is a definition-use chain (or DU chain), which consists of a definition D of a variable and all the uses U reachable from that definition without any other intervening definitions.

Both UD and DU chains are created by using a form of static code analysis known as data flow analysis. Knowing the use-def and def-use chains for a program or subprogram is a prerequisite for many compiler optimizations, including constant propagation and common subexpression elimination.

Purpose

Making the use-define or define-use chains is a step in liveness analysis, so that logical representations of all the variables can be identified and tracked through the code.

Consider the following snippet of code:

<span id="mwKA"></span><span class="w" id="mwKQ"> </span><span class="kt" id="mwKg">int</span><span class="w" id="mwKw"> </span><span class="n" id="mwLA">x</span><span class="w" id="mwLQ"> </span><span class="o" id="mwLg">=</span><span class="w" id="mwLw"> </span><span class="mi" id="mwMA">0</span><span class="p" id="mwMQ">;</span><span class="w" id="mwMg"> </span><span class="cm" id="mwMw">/* A */</span> <span class="w" id="mwNA"> </span><span class="n" id="mwNQ">x</span><span class="w" id="mwNg"> </span><span class="o" id="mwNw">=</span><span class="w" id="mwOA"> </span><span class="n" id="mwOQ">x</span><span class="w" id="mwOg"> </span><span class="o" id="mwOw">+</span><span class="w" id="mwPA"> </span><span class="n" id="mwPQ">y</span><span class="p" id="mwPg">;</span><span class="w" id="mwPw"> </span><span class="cm" id="mwQA">/* B */</span> <span class="w" id="mwQQ"> </span><span class="cm" id="mwQg">/* 1, some uses of x */</span> <span class="w" id="mwQw"> </span><span class="n" id="mwRA">x</span><span class="w" id="mwRQ"> </span><span class="o" id="mwRg">=</span><span class="w" id="mwRw"> </span><span class="mi" id="mwSA">35</span><span class="p" id="mwSQ">;</span><span class="w" id="mwSg"> </span><span class="cm" id="mwSw">/* C */</span> <span class="w" id="mwTA"> </span><span class="cm" id="mwTQ">/* 2, some more uses of x */</span>

Notice that x is assigned a value at three points (marked A, B, and C). However, at the point marked "1", the use-def chain for x should indicate that its current value must have come from line B (and its value at line B must have come from line A). Contrariwise, at the point marked "2", the use-def chain for x indicates that its current value must have come from line C. Since the value of the x in block 2 does not depend on any definitions in block 1 or earlier, x might as well be a different variable there; practically speaking, it is a different variable — call it x2.

<span id="mwWQ"></span><span class="w" id="mwWg"> </span><span class="kt" id="mwWw">int</span><span class="w" id="mwXA"> </span><span class="n" id="mwXQ">x</span><span class="w" id="mwXg"> </span><span class="o" id="mwXw">=</span><span class="w" id="mwYA"> </span><span class="mi" id="mwYQ">0</span><span class="p" id="mwYg">;</span><span class="w" id="mwYw"> </span><span class="cm" id="mwZA">/* A */</span> <span class="w" id="mwZQ"> </span><span class="n" id="mwZg">x</span><span class="w" id="mwZw"> </span><span class="o" id="mwaA">=</span><span class="w" id="mwaQ"> </span><span class="n" id="mwag">x</span><span class="w" id="mwaw"> </span><span class="o" id="mwbA">+</span><span class="w" id="mwbQ"> </span><span class="n" id="mwbg">y</span><span class="p" id="mwbw">;</span><span class="w" id="mwcA"> </span><span class="cm" id="mwcQ">/* B */</span> <span class="w" id="mwcg"> </span><span class="cm" id="mwcw">/* 1, some uses of x */</span> <span class="w" id="mwdA"> </span><span class="kt" id="mwdQ">int</span><span class="w" id="mwdg"> </span><span class="n" id="mwdw">x2</span><span class="w" id="mweA"> </span><span class="o" id="mweQ">=</span><span class="w" id="mweg"> </span><span class="mi" id="mwew">35</span><span class="p" id="mwfA">;</span><span class="w" id="mwfQ"> </span><span class="cm" id="mwfg">/* C */</span> <span class="w" id="mwfw"> </span><span class="cm" id="mwgA">/* 2, some uses of x2 */</span>

The process of splitting x into two separate variables is called live range splitting. See also static single assignment form.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication