Adding an architecture specific intrinsic and an annotation

There are three things I write about here. Two work (Adding a calling convention and Using an intrinsic to implement an new target instruction). One I still don't understand how to do (access the SP in the IR).

I started with llvm basic doc on adding intrinsic. All paths are relative to $LLVMBASE.

Reading the sp

I need to use the stack pointer, e.g., RSP on X86, to get some information off the stack. I am not sure what the best way to do this is.

When I try and compile, I get a proper .ll file from clang with the instruction

%0 = call i64 @llvm.x86.read.sp()

However, it fails, with

llc: ./llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp:303: unsigned int llvm::InstrEmitter::getVR(llvm::SDValue, llvm::DenseMap<llvm::SDValue, unsigned int>&): Assertion `I != VRBaseMap.end() && "Node emitted out of order - late"' failed.

I am not sure how to introduce the IR to read the RSP so I can create code to do a calculation on the RSP to retrieve a value buried in the stack. I figure I need to lower the call i64 @llvm.x86.read.sp in X86TargetLowering::LowerOperation. I am not sure if this would be ISD::INTRINSIC_WO_CHAIN or ISD::INTRINSIC_W_CHAIN (and I don't actually understand what would go in the intrinsic definition to make it one or the other. Any pointers would be appreciated. Thanks,

Adding instrinsics which can be mapped to new instructions.

Adding calling convention

I wanted to be able to change the calling convention for a function, so I added an annotation that can do that. This is an architecture specific annotation.

more to write here