1. Gyanastra
  2. Compiler Design
  3. Data Flow Analysis
  4. DEF and USE Sets of a Basic Block
Free lesson, no account needed 8 min read 2 practice questions after it

Compiler Design · Data Flow Analysis

DEF and USE Sets of a Basic Block

Overview

DEF(B) and USE(B) are the two sets computed for every basic block B and fed as inputs to the data-flow algorithm that computes the IN and OUT sets.
DEF(B) is simple to compute; USE(B) requires care because a definition inside the block can kill an incoming value and remove a variable from the set.

Key concepts

  • DEF(B) — definition set of a basic block: the set of all variables defined inside basic block B. A variable is defined if it appears on the left-hand side of an assignment in B. Every variable seen on the left-hand side goes into DEF(B); there is no ordering issue.
  • USE(B) — use set of a basic block: the set of all variables whose values may be used in B prior to any definition of that variable inside B. A variable on the right-hand side is not automatically in USE(B): if the variable is defined earlier inside the same block, that earlier definition kills the incoming value, so the variable is not included.
  • Role in data-flow analysis: DEF(B) and USE(B) are the building blocks / inputs of the data-flow algorithm; the algorithm takes them for every basic block and computes the IN and OUT sets.
  • Killing: a definition inside the block kills the value of that variable coming from outside the block, so a later use of it does not put it in USE (the variable is not "live-in" because of that use).
  • Use before definition: if a variable is used first and defined later in the block, the use is counted — the value used is the one coming from outside the block.
  • Read-only variables: variables that are only read and never defined in the block are always in USE(B).

How it works

Computing DEF(B)

  1. Scan every statement of the block.
  2. Every variable on the left-hand side of an assignment is added to DEF(B).
  3. Nothing else matters (no ordering issue).

Computing USE(B)

  1. Scan the statements of the block in order.
  2. A variable on the right-hand side is added to USE(B) only if it has not already been defined earlier in the block (its use comes before any definition of it inside B).
  3. If a variable is defined earlier in the block and used later, its use is not counted.
  4. If a variable is used first and defined later in the block, the use is counted.
  5. Variables that are only read and never defined in the block are always in USE(B).
Figure. Q remains in USE because statement 1 uses it before statement 2 defines it; statement 1 defines P before statement 3 uses P, so P is not added. USE is Q R S T and DEF is P Q T.

Formulas

  • DEF(B)={ x∣x appears on the left-hand side of some statement in B }\text{DEF}(B) = \{\, x \mid x \text{ appears on the left-hand side of some statement in } B \,\}

  • USE(B)={ x∣x is used (right-hand side) in B before any definition of x in B }\text{USE}(B) = \{\, x \mid x \text{ is used (right-hand side) in } B \text{ before any definition of } x \text{ in } B \,\}

  • Here xx is a program variable and BB is a basic block.

Worked examples

Example 1 — DEF only

P = R + S
R = P + Q
Q = A + B

Variables on the left-hand side: P, R, Q. DEF(B1)={P,R,Q}\text{DEF}(B_1) = \{P, R, Q\}

Example 2 — USE with a definition before use

P = Q + R
Q = S + T
T = P + Q

Naive (wrong) answer — every right-hand-side variable: {Q,R,S,T,P}\{Q, R, S, T, P\}.

Correct analysis, statement by statement:

  • P = Q + R: Q and R are used and not yet defined → Q, R enter USE. P is defined.
  • Q = S + T: S and T are used and not yet defined → S, T enter USE. Q is defined (but Q was already used earlier, so Q stays in USE).
  • T = P + Q: P is used, but P was defined earlier (statement 1) → P is not included. Q is used, but Q was defined earlier (statement 2) → Q is already in USE from its earlier use, so it remains.
    T is used, but T was not defined earlier (it is first used in statement 2) → T stays in USE (it is already in USE from statement 2).

USE(B1)={Q,R,S,T}\text{USE}(B_1) = \{Q, R, S, T\}, DEF(B1)={P,Q,T}\text{DEF}(B_1) = \{P, Q, T\}

Reasoning per variable:

  • P: used in statement 3, but defined in statement 1 before its use → the definition inside the block kills the incoming value of P, so P is removed from USE.
  • Q: no definition of Q before its use in statement 1 (its definition comes after the use) → Q stays in USE.
  • R: used, never defined before the use → R stays in USE.
  • S: used, never defined before the use → S stays in USE.
  • T: defined in statement 3, but that definition comes after the use of T in statement 2's right-hand side → the value of T from outside the block is used → T stays in USE.

Example 3 — changing one statement Same block but statement 2 replaced:

P = Q + R
T = A + B
T = P + Q

Now T is defined (statement 2) before its use (statement 3), so T is removed from USE. A and B are used and never defined → they enter USE. USE={Q,R,A,B}\text{USE} = \{Q, R, A, B\}, DEF={P,T}\text{DEF} = \{P, T\}

Example 4 — single-line basic blocks If each basic block contains only one statement:

  • USE = variables on the right-hand side
  • DEF = variables on the left-hand side

Example 5 — self-referential statement

T = T + S

T is both used and defined. To assign a value to T, the compiler first computes T + S, so T is used before it is defined. Therefore USE={T,S}\text{USE} = \{T, S\}, DEF={T}\text{DEF} = \{T\}.

Comparison

DEF(B)USE(B)
MeaningVariables defined inside BVariables used in B before any definition in B
Where to lookLeft-hand side of assignmentsRight-hand side of assignments
Ordering matters?NoYes — a prior definition in B removes the variable
DifficultySimple, no confusionMust be handled carefully

Revision summary

  • DEF(B) and USE(B) are the two inputs on which the IN/OUT data-flow algorithm is built.
  • DEF(B): any variable on the left-hand side goes in — no exceptions, no ordering issue.
  • USE(B): a right-hand-side variable counts only if it is used before any definition of it inside the block.
  • A definition inside the block kills the incoming value, so a later use of that variable does not put it in USE.
  • A use that comes before the definition in the block keeps the variable in USE (the outside value is consumed).
  • For one-line blocks: USE = right-hand side, DEF = left-hand side.
  • For T = T + S: the right-hand side is evaluated first, so T is used before it is defined → T and S in USE, T in DEF.
  • Trap: listing every right-hand-side variable as USE is wrong — check for an earlier definition in the same block.

Previous-year exam insights

  • For reaching definitions of a basic block, GEN(B) is the set of definitions (statements) in B that reach the end of B (the last definition of each variable assigned in B), and KILL(B) is the set of all other definitions in the program of the variables defined in B;
    the equations are IN(B) = union of OUT(P) over predecessors P and OUT(B) = GEN(B) ∪ (IN(B) − KILL(B)).
    GEN is a set of definitions, not the set DEF(B) of variables used for liveness.
  • USE(B) is the set of variables used in B before any definition in B (upward-exposed uses) and DEF(B) the set of variables defined in B; USE(B) is only part of the live-in set.
    For liveness, IN(B) = USE(B) ∪ (OUT(B) − DEF(B)) and OUT(B) = union of IN(S) over successors S, so IN(B) can contain variables outside USE(B) that are live through B.
  • A variable that is defined in B but whose definition is not used before being redefined in B is still in DEF(B) — DEF is order-independent and includes every left-hand-side variable, even dead definitions.
  • In three-address code, array accesses like x = a[i] use both a and i (and a[i] = x uses a, i, and x), and pointer dereferences *p = x / x = *p use p; such operands must be counted in USE/DEF, not just simple variable names.
  • Procedure calls in a block: actual parameters and globally visible variables are treated as uses, and variables that may be modified by the call are treated as definitions (unless interprocedural analysis proves otherwise).
  • A use of a variable on the right-hand side counts in USE(B) only if no definition of that variable precedes it in the block; the first such use is what matters — later uses after a definition do not re-add it, and a variable already in USE stays in USE regardless of later definitions.
Pages 1–2 of 2