Value liveout (uses)

Hi,

How can i know, if a value have uses outside of the current basic block (liveout), without iterating through all the basic block ?

Thanks.

Try use_iterator. See llvm/Value.h.

-Eli

If the value is created within the basic block in question, and the block doesn't loop to itself, then you can just iterate through the uses and note if the use is an instruction in a different block:

bool isLiveOut(Instruction* I) {
  BasicBlock* I_BB = I->getParent():
  for (Value::use_iterator ui = I->use_begin(), ue = I->use_end(); ui != ue; ++ui)
    if (cast<Instruction>(ui)->getParent() != I_BB)
      return true;
  }
}

If the value is created within the block but the block loops to itself, you can get away with a very slight adjustment:

    if (cast<Instruction>(ui)->getParent() != I_BB && !isa<PHINode>(ui))

If the value is created in one block and you want to know if it's live out of some dominated block, that's a lot more complicated (and expensive).

John.

Correction: this should read:

    if (cast<Instruction>(ui)->getParent() != I_BB || isa<PHINode>(ui))

John.

Thank you.

Is it possible to determine the liveout of the operands (see example bellow) ?

%5 = add i32 %4, %3

For ‘%5’: i can simply use " i->isUsedOutsideOfBlock() "
For ‘%3’ and ‘%4’ : this is the question …

From your answer, is it possible to determine which value is liveout ( in binary instruction)?

Is it possible to determine the liveout of the operands (see example bellow) ?

%5 = add i32 %4, %3

For ‘%5’: i can simply use " i->isUsedOutsideOfBlock() "
For ‘%3’ and ‘%4’ : this is the question …

By definition, operands are live out if they’re used in any block that’s reachable from
the block containing the instruction. Barring a few special cases we’ve already
covered, the easiest way I know to compute that in general is to first calculate the
set of reachable blocks, then iterate through the uses, checking whether that use
is in a reachable block (if the use is in a PHI node, only count it if the
corresponding incoming block is reachable).

From your answer, is it possible to determine which value is liveout ( in binary instruction)?

All of these answers are things you would do for an individual LLVM “Value”. I don’t
know what you mean by “which value” beyond that, and I don’t know what you mean
by “in binary instruction”.

John.

I believe Dan has added a pass to compute livein / liveout values.

Evan

The pass you're referring to is in include/llvm/Analysis/LiveValues.h
and lib/Analysis/LiveValues.cpp. It computes conservative
approximations for specific liveness queries, rather than full
livein/liveout information. It's intended to be used as a heuristic.

Dan

" All of these answers are things you would do for an individual LLVM “Value”. "

I am sorry, but may be i am missing something here.
In the previous email you suggested the function "bool isLiveOut(Instruction* I) ".
So, if you please, explain how i can find out if a llvm::Value (not an llvm::Instruction) is live out or not.
As i understand it, isLiveOut can help me ONLY if i have an llvm::Instruction (not a llvm:Value).

Thanks.

P.S
I am truly grateful for your help.

Hi,

Do i need to set the “LiveValues” pass in the PassManager as a prerequisite? if so how do i get the results ?
Can i simply use its public function ?
Can you explain what exactly do mean by “conservative approximations” ? Can it determine the live out of llvm:Value accurately?

Thank you very much.

" All of these answers are things you would do for an individual LLVM “Value”. "

I am sorry, but may be i am missing something here.
In the previous email you suggested the function "bool isLiveOut(Instruction* I) ".
So, if you please, explain how i can find out if a llvm::Value (not an llvm::Instruction) is live out or not.
As i understand it, isLiveOut can help me ONLY if i have an llvm::Instruction (not a llvm:Value).

Yes, sorry, I used the term Value in response to your question about “specific value[s]”.
These are operations that only make sense for Instructions, not arbitrary values, which
don’t really have liveness in any real sense.

Do i need to set the “LiveValues” pass in the PassManager as a prerequisite? if so how do i get the results ?
Can i simply use its public function ?
Can you explain what exactly do mean by “conservative approximations” ? Can it determine the live out of llvm:Value accurately?

I don’t know much about the pass in question, but a conservative approximation means
that it will sometimes claim that a value is live when it isn’t, but not the other way around.
For most uses of liveness, that’s good enough to guarantee correctness and still get
some useful optimization opportunities without making the entire pass excessively
expensive to run.

John.