[LoongArch] Use register R0 (ZERO) for constant 0
authorWeining Lu <luweining@loongson.cn>
Thu, 16 Jun 2022 05:33:43 +0000 (13:33 +0800)
committerWeining Lu <luweining@loongson.cn>
Thu, 16 Jun 2022 05:43:11 +0000 (13:43 +0800)
Differential Revision: https://reviews.llvm.org/D127205

llvm/lib/Target/LoongArch/LoongArchISelDAGToDAG.cpp
llvm/test/CodeGen/LoongArch/imm.ll

index 13d7218..2e5b3ea 100644 (file)
@@ -37,9 +37,14 @@ void LoongArchDAGToDAGISel::Select(SDNode *Node) {
     break;
   case ISD::Constant: {
     int64_t Imm = cast<ConstantSDNode>(Node)->getSExtValue();
+    if (Imm == 0 && Node->getSimpleValueType(0) == GRLenVT) {
+      SDValue New = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL,
+                                           LoongArch::R0, GRLenVT);
+      ReplaceNode(Node, New.getNode());
+      return;
+    }
     SDNode *Result = nullptr;
     SDValue SrcReg = CurDAG->getRegister(LoongArch::R0, GRLenVT);
-
     // The instructions in the sequence are handled here.
     for (LoongArchMatInt::Inst &Inst : LoongArchMatInt::generateInstSeq(Imm)) {
       SDValue SDImm = CurDAG->getTargetConstant(Inst.Imm, DL, GRLenVT);
index 3d6cfce..fb0dcf2 100644 (file)
@@ -1,5 +1,13 @@
 ; RUN: llc --mtriple=loongarch64 < %s | FileCheck %s
 
+define i64 @imm0() {
+; CHECK-LABEL: imm0:
+; CHECK:       # %bb.0:
+; CHECK-NEXT:    move $a0, $zero
+; CHECK-NEXT:    jirl $zero, $ra, 0
+  ret i64 0
+}
+
 define i64 @imm7ff0000000000000() {
 ; CHECK-LABEL: imm7ff0000000000000:
 ; CHECK:       # %bb.0: