Implementing an llvm.setreturnaddress intrinsic

Hi,

I’ve been having some trouble implementing a new intrinsic for LLVM and I was wondering if someone might know the answer.

LLVM has an llvm.returnaddress intrinsic, but for a specific project, I need to be able to modify the return address of a function as well, so I’m implementing a new intrinsic:
def int_setreturnaddress : Intrinsic<[], [llvm_i32_ty, llvm_ptr_ty], []>;

And I’m using it as follows:

B.CreateCall2(
Intrinsic::getDeclaration(M, Intrinsic::setreturnaddress),
ConstantInt::get(Type::getInt32Ty(RI->getContext()), 0),
B.CreateIntToPtr(ConstantInt::get(IntPtrTy, 0 /* testing */)), RA->getReturnType()));

The problem I’m facing is that for some reason, my following line inside X86TargetLowering::LowerOperation is never getting called:

case ISD::SETRETURNADDR: return LowerSETRETURNADDR(Op, DAG);

This is despite the fact that I can confirm the following line inside SelectionDAGBuilder::visitIntrinsicCall is getting called:

case Intrinsic::setreturnaddress:
setValue(&I, DAG.getNode(ISD::SETRETURNADDR, sdl, TLI->getPointerTy(),
getValue(I.getArgOperand(0)),
getValue(I.getArgOperand(1))));

return 0;

My best guess is that my code is being optimized out – but I’m not sure where or why.

Does anyone know why this might be happening?

Thank you!
Mehrdad