[LoongArch] Replace assertion by error message while lowering RETURNADDR and FRAMEADDR
authorgonglingqin <gonglingqin@loongson.cn>
Mon, 31 Oct 2022 01:18:20 +0000 (09:18 +0800)
committergonglingqin <gonglingqin@loongson.cn>
Mon, 31 Oct 2022 01:19:00 +0000 (09:19 +0800)
If `__builtin_frame_address` or `__builtin_return_address` is invoked with
non-zero argument, show an error message instead of a crash.

Reference: https://reviews.llvm.org/rG83b88441ad951fe99c30402930ef3cd661f2fd2b

Differential Revision: https://reviews.llvm.org/D136917

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
llvm/test/CodeGen/LoongArch/frameaddr-returnaddr-error.ll

index 1c58fa0..94f0286 100644 (file)
@@ -239,11 +239,11 @@ SDValue LoongArchTargetLowering::lowerFRAMEADDR(SDValue Op,
   }
 
   // Currently only support lowering frame address for current frame.
-  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
-  assert((Depth == 0) &&
-         "Frame address can only be determined for current frame.");
-  if (Depth != 0)
+  if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
+    DAG.getContext()->emitError(
+        "frame address can only be determined for the current frame");
     return SDValue();
+  }
 
   MachineFunction &MF = DAG.getMachineFunction();
   MF.getFrameInfo().setFrameAddressIsTaken(true);
@@ -259,11 +259,11 @@ SDValue LoongArchTargetLowering::lowerRETURNADDR(SDValue Op,
     return SDValue();
 
   // Currently only support lowering return address for current frame.
-  unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
-  assert((Depth == 0) &&
-         "Return address can only be determined for current frame.");
-  if (Depth != 0)
+  if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() != 0) {
+    DAG.getContext()->emitError(
+        "return address can only be determined for the current frame");
     return SDValue();
+  }
 
   MachineFunction &MF = DAG.getMachineFunction();
   MF.getFrameInfo().setReturnAddressIsTaken(true);
index 6adb2e5..2ac837c 100644 (file)
@@ -16,3 +16,16 @@ define ptr @non_const_depth_returnaddress(i32 %x) nounwind {
   ret ptr %1
 }
 
+define ptr @non_zero_frameaddress() nounwind {
+; CHECK: frame address can only be determined for the current frame
+  %1 = call ptr @llvm.frameaddress(i32 1)
+  ret ptr %1
+}
+
+
+define ptr @non_zero_returnaddress() nounwind {
+; CHECK: return address can only be determined for the current frame
+  %1 = call ptr @llvm.returnaddress(i32 1)
+  ret ptr %1
+}
+