Search code examples
clangllvmllvm-ir

what is LLVM ExtractValueInst?


I was studying one llvm code, where i have found a line,

const ExtractValueInst *EI = cast<ExtractValueInst>(I);
st.setValue(I, st.getValue(EI->getAggregateOperand()));

Now, I understand why cast<...> is being used, but I can't relate with the ExtarctValueInst, Can you give me one example what is this instruction in IR and what is the equivalence C code? and also I want to know about getAggregateOperand() function also. Thank you in advance.


Solution

  • Suppose you have a function that returns a 32-bit integer that isn't really one integer, but rather a set of smaller bitfields and/or bools. Perhaps the bottom six bits are an integer in the range 0-63, the seventh bit is a boolean, etc.

    Somewhere you have a call to that function, and a little below the call, you have code that uses the i6 that's a part of the return value. So you create an extractvalue to extract a value from the composite return value. (If the composite were in main memory you'd probably create a getelementptr/load pair, but since it's most likely in a CPU register you create an extractvalue.)

    This is quite often used e.g. in exception handling; a catch clause has a single parameter which is a composite of two things, and the catch clause tests one of the two components to determine whether to catch the exception or pass it on.