Frequently, we might have an instance of the Value Class and we want to determine which Users use the Value. The list of all Users of a particular Value is called a def-use chain.
1. 函数的def-use
Function *F = ...;
for (User *U : F->users()) {
if (Instruction *Inst = dyn_cast<Instruction>(U)) {
errs() << "F is used in instruction:\n";
errs() << *Inst << "\n";
}
或者
Function *F = ...;
for (Value::use_iterator U = F->use_begin(), e = F->use_end(); U != e; ++U) {
if (Instruction *Inst = dyn_cast<Instruction>(&*(U->getUser()))) {
errs() << "F is used in instruction:\n";
errs() << *Inst << "\n";
}
2. 指令的Def-Use
Instruction *A = ...;
for (User *U : A->users()) {
if (Instruction *Inst = dyn_cast<Instruction>(U)) {
errs() << "A is used in instruction:\n";
errs() << *Inst << "\n";
}
或
Instruction *A = ...;
for (Value::use_iterator i = A->use_begin(), e = A->use_end(); i != e; ++i) {
if (Instruction *U = dyn_cast<Instruction>(&*(i->getUser()))) {
...
}
}