Question

I've been trying to make sense of LLVM's instruction combining code and noticed this function:

static void detectLog2OfHalf(Value *&Op, Value *&Y, IntrinsicInst *&Log2) {

   if (!Op->hasOneUse())
     return;

   IntrinsicInst *II = dyn_cast<IntrinsicInst>(Op);
   if (!II)
     return;
   if (II->getIntrinsicID() != Intrinsic::log2 || !II->hasUnsafeAlgebra())
     return;
   Log2 = II;

   Value *OpLog2Of = II->getArgOperand(0);
   if (!OpLog2Of->hasOneUse())
     return;

   Instruction *I = dyn_cast<Instruction>(OpLog2Of);
   if (!I)
     return;
   if (I->getOpcode() != Instruction::FMul || !I->hasUnsafeAlgebra())
     return;

   if (match(I->getOperand(0), m_SpecificFP(0.5)))
     Y = I->getOperand(1);
   else if (match(I->getOperand(1), m_SpecificFP(0.5)))
     Y = I->getOperand(0);
}

Why would *& by used in the parameters? Does it have some meaning or would Op be equivalent to *&Op?

Was it helpful?

Solution

Why would *& by used in the parameters?

In a function parameter list, this parameter

IntrinsicInst *&Log2

is a reference to pointer to Value. It means the function can modify the pointer itself, and this is seen at the caller side. In this case, that happens here:

Log2 = II;

If Log2 was not passed by reference, the above line would only have an effect in the scope of the function.

... would Op be equivalent to *&Op?

Not in this context. In a different one, yes. For example, when * is the de-reference operator and & is the address-of operator, then *&x would mean "de-reference the result of applying the address-of operator to x", that is, de-reference a pointer to x. C++ confusingly re-uses symbols depending on context. Most of this is inherited from C. On top of that, changes to C++ should not break existing code. The easiest way to do this is to re-use symbols.

OTHER TIPS

& means reference. value * means value is a pointer.

value *&op is equalent to passing pointer by reference, so if we change value in the function it will be reflected in the called function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top