[llvm] [SelectionDAG] Add support for the 3-way comparison intrinsics [US]CMP (PR #91871)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 11:52:04 PDT 2024


================
@@ -4426,6 +4511,56 @@ SDValue DAGTypeLegalizer::WidenVecRes_Binary(SDNode *N) {
                      {InOp1, InOp2, Mask, N->getOperand(3)}, N->getFlags());
 }
 
+SDValue DAGTypeLegalizer::WidenVecRes_CMP(SDNode *N) {
+  LLVMContext &Ctxt = *DAG.getContext();
+  SDLoc dl(N);
+
+  SDValue LHS = N->getOperand(0);
+  SDValue RHS = N->getOperand(1);
+  EVT OpVT = LHS.getValueType();
+  EVT TransformedOpVT = TLI.getTypeToTransformTo(Ctxt, OpVT);
+  if (TransformedOpVT.isVector() &&
+      TransformedOpVT.getVectorNumElements() > OpVT.getVectorNumElements()) {
+    LHS = GetWidenedVector(LHS);
+    RHS = GetWidenedVector(RHS);
+  }
+
+  EVT WidenResVT = TLI.getTypeToTransformTo(Ctxt, N->getValueType(0));
+  ElementCount WidenResEC = WidenResVT.getVectorElementCount();
+  EVT WidenResElementVT = WidenResVT.getVectorElementType();
+
+  // At this point we know that the type of operands should be legal,
----------------
Poseydon42 wrote:

> This means that nodes using a value are legalized before nodes producing the value.

I don't think I agree with this -- as far as I'm aware it's the direct opposite, a node can only be legalized after all of its operands are legalized. That's why when you run `llc` with debug loggging enabled the first nodes to be legalized are things like target registers and constants, and the last node is always return. This is what allows me to use the approach I'm using, as I know that inside `WidenVecRes_CMP` the two operands have already been widened, but the CMP node itself still points to the old, unwidened operands.

If you also look into the implementation of `DAGTypeLegalizer::run`, the one of the first things it does is populate the worklist of nodes to be processed with those that have no operands, and then updates the worklist any time all arguments of some node are processed.

https://github.com/llvm/llvm-project/pull/91871


More information about the llvm-commits mailing list