Explained: Custom Selection DAG Node in Target Description

245 阅读1分钟

Preface

During Instruction Selection, llvm will construction a Selection DAG (Direct Acyclic Graph) for a basic block with each IR with one Selection DAG Node (usually). Then doing pattern matching for tiling that Selection DAG for Instruction Selection.

However, when there is a llvm-provided SDNode which is not we want, we need to offer our own SDNode, then we have to construct that node in our td file.

Custom SDNode

For example, we want a CLR (clear) operation which clear all the status bits, and there is no corresponding SDNode in llvm yet, so let us create a custom one.

Just define a record derived from SDNode class like this.

def clr : SDNode<"M88kISD::CLR", SDTIntBinOp>;

Then in ISelLowering, we can use this custom SDNode.

But what does that mean SDNode<"M88kISD::CLR", SDTIntBinOp>?

SDNode class explained

1. "M88kISD::CLR" is the name of the opcode of this custom SDNode, which will be in M88kISD namespace, and CLR is an enum value inside that namespace.

2. SDTIntBinOp is the profile of this node, saying how many results, operands of this SDNode and some constraints for those results and operands, because a SDNode represents an operation in Selection DAG. In those constraints, the results and operands are referenced by position number, which numbering started from results then to operands, from left to right.

def SDTIntBinOp : SDTypeProfile<1, 2, [     // add, and, or, xor, udiv, etc.
  SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>, SDTCisInt<0>
]>;

 This means, SDTIntBinOp is a SDTypeProfile, that has 1 result and 2 operands, where the type of result are the same as the first operand and the second operand, moreove, is Int.

i.e. op #0 #1 #2. where #0 is the result, #1 is first operand, #2 is second operand in the constraint list.