[LLVMdev] Inferring dependencies in phi instructions

Anirudh Sivaraman sk.anirudh at gmail.com
Sun Jun 28 23:24:49 PDT 2015


I am trying to infer data dependencies using LLVM's def-use chains and
I am having trouble dealing with 'phi' instructions. Specifically,

If I am given the code snippet:

int foo() {
  int y = 1;
  int x;
  if (y) {
    x = 2;
  } else {
    x = 3;
  }
  return x;
}

Here, x has a data dependence on y (not control because x is assigned
in both halves), but LLVM expresses 'x' as: %x.0 = phi i32 [ 2, %2 ],
[ 3, %3 ] using phi-nodes, omitting the dependence on y.

If I write the function as:

int foo() {
  int y = 1;
  int x = y ? 2 : 3;
  return x;
},

the dependencies work out alright because LLVM now uses a select
instruction, where the dependence on y is explicit:

%y = select i1 %x, i32 2, i32 3

In general, I don't think I can convert phi nodes into select
statements (because a phi node can refer to values from more than two
paths in general, while select statements permit only two options, one
each for a true and a false branch). Any thoughts on how this is
handled in practice would be very helpful.

Anirudh



More information about the llvm-dev mailing list