================
@@ -3187,6 +3194,45 @@ static void adjustICmpTruncate(SelectionDAG &DAG, const 
SDLoc &DL,
   }
 }
 
+// Adjust if a given Compare is a check of the stack guard against a stack
+// guard instance on the stack. Specifically, this checks if:
+// - The operands are a load of the stack guard, and a load from a stack slot
+// - Those operand values are not used elsewhere <-- asserts if this is not
+//   true!
+static void adjustForStackGuardCompare(SelectionDAG &DAG, const SDLoc &DL,
+                                       Comparison &C) {
+  SDValue StackGuardLoad;
+  LoadSDNode *FILoad;
+
+  if (C.Op0.isMachineOpcode() &&
+      C.Op0.getMachineOpcode() == SystemZ::LOAD_STACK_GUARD &&
+      ISD::isNormalLoad(C.Op1.getNode()) &&
+      dyn_cast<FrameIndexSDNode>(C.Op1.getOperand(1))) {
+    StackGuardLoad = C.Op0;
+    FILoad = cast<LoadSDNode>(C.Op1);
+  } else if ((C.Op1.isMachineOpcode() &&
+              C.Op1.getMachineOpcode() == SystemZ::LOAD_STACK_GUARD &&
+              ISD::isNormalLoad(C.Op0.getNode()) &&
+              dyn_cast<FrameIndexSDNode>(C.Op0.getOperand(1)))) {
+    StackGuardLoad = C.Op1;
+    FILoad = cast<LoadSDNode>(C.Op0);
+  } else {
+    return;
+  }
+  // Assert that the values of the loads are not used elsewhere.
+  // Bail for now. TODO: What is the proper response here?
+  assert(
+      SDValue(FILoad, 0).hasOneUse() &&
+      "Value of stackguard loaded from stack must be used for compare only!");
+  assert(StackGuardLoad.hasOneUse() &&
+         "Value of reference stackguard must be used for compare only!");
+
+  // At this point we are sure that this is a proper compare_stack_guard
+  // case, update the opcode to reflect this.
+  C.Opcode = SystemZISD::COMPARE_STACKGUARD;
+  C.CCValid = SystemZ::CCMASK_ICMP;
----------------
uweigand wrote:

However, some checks are missing here for correctness: we shouldn't just 
override `CCValid`.  Rather, we should explicitly verify that our original 
opcode here is still ICMP, and also that the ICmpType is compatible with an 
unsigned comparison.  Otherwise, the transformation would be invalid.

https://github.com/llvm/llvm-project/pull/169317
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to