[cfe-dev] Adding an intrinsic to read the stack pointer, e.g., RSP in x86

Seth Goldstein via cfe-dev cfe-dev at lists.llvm.org
Sun Dec 10 18:58:07 PST 2017

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.

My plan was to create an intrinsic, e.g.,

  let TargetPrefix = "x86" in {
     def int_x86_read_sp : GCCBuiltin<"__builtin_read_sp">,
      Intrinsic<[llvm_i64_ty], [], [IntrNoMem]>;

I thought I would define a PseudoI, e.g.,

  let Uses = [RSP] in
      def READSP64 : PseudoI<(outs GR64:$dst), (ins),
            [(set GR64:$dst, RSP)]>,
But, actually I am not sure how this would come into play.  (Do I need
this?  If so, how do I use it?)

To test it out I added a Builtin to BuiltinsX86.def, e.g.,

  BUILTIN(__builtin_read_sp, "ULLi", "")

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

    %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
actually understand what would go in the intrinsic definition to make
it one or the other.  Any pointers would be appreciated.  Thanks,


Seth Copen Goldstein
Carnegie Mellon University
Computer Science Dept
7111 GHC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20171210/e243d15a/attachment.html>

More information about the cfe-dev mailing list