[LoongArch] Support parsing target specific flags for MIR
authorwanglei <wanglei@loongson.cn>
Thu, 10 Nov 2022 12:28:23 +0000 (20:28 +0800)
committerwanglei <wanglei@loongson.cn>
Thu, 10 Nov 2022 12:53:20 +0000 (20:53 +0800)
These hooks ensure that the LoongArch backend can serialize and parse
MIR correctly.

Reviewed By: MaskRay

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

llvm/lib/Target/LoongArch/LoongArchInstrInfo.cpp
llvm/lib/Target/LoongArch/LoongArchInstrInfo.h
llvm/test/CodeGen/LoongArch/mir-target-flags.ll [new file with mode: 0644]

index 00b806a..eab5cdb 100644 (file)
@@ -434,3 +434,28 @@ bool LoongArchInstrInfo::reverseBranchCondition(
   Cond[0].setImm(getOppositeBranchOpc(Cond[0].getImm()));
   return false;
 }
+
+std::pair<unsigned, unsigned>
+LoongArchInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
+  return std::make_pair(TF, 0u);
+}
+
+ArrayRef<std::pair<unsigned, const char *>>
+LoongArchInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
+  using namespace LoongArchII;
+  // TODO: Add more target flags.
+  static const std::pair<unsigned, const char *> TargetFlags[] = {
+      {MO_CALL, "loongarch-call"},
+      {MO_CALL_PLT, "loongarch-call-plt"},
+      {MO_PCREL_HI, "loongarch-pcrel-hi"},
+      {MO_PCREL_LO, "loongarch-pcrel-lo"},
+      {MO_GOT_PC_HI, "loongarch-got-pc-hi"},
+      {MO_GOT_PC_LO, "loongarch-got-pc-lo"},
+      {MO_LE_HI, "loongarch-le-hi"},
+      {MO_LE_LO, "loongarch-le-lo"},
+      {MO_IE_PC_HI, "loongarch-ie-pc-hi"},
+      {MO_IE_PC_LO, "loongarch-ie-pc-lo"},
+      {MO_LD_PC_HI, "loongarch-ld-pc-hi"},
+      {MO_GD_PC_HI, "loongarch-gd-pc-hi"}};
+  return makeArrayRef(TargetFlags);
+}
index 633d3eb..71bc330 100644 (file)
@@ -74,6 +74,12 @@ public:
   bool
   reverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const override;
 
+  std::pair<unsigned, unsigned>
+  decomposeMachineOperandsTargetFlags(unsigned TF) const override;
+
+  ArrayRef<std::pair<unsigned, const char *>>
+  getSerializableDirectMachineOperandTargetFlags() const override;
+
 protected:
   const LoongArchSubtarget &STI;
 };
diff --git a/llvm/test/CodeGen/LoongArch/mir-target-flags.ll b/llvm/test/CodeGen/LoongArch/mir-target-flags.ll
new file mode 100644 (file)
index 0000000..9f3a061
--- /dev/null
@@ -0,0 +1,44 @@
+; RUN: llc --mtriple=loongarch64 --stop-after loongarch-prera-expand-pseudo \
+; RUN:     --relocation-model=pic %s -o %t.mir
+; RUN: llc --mtriple=loongarch64 --run-pass loongarch-prera-expand-pseudo \
+; RUN:     %t.mir -o - | FileCheck %s
+
+;; This tests the LoongArch-specific serialization and deserialization of
+;; `target-flags(...)`
+
+@g_e = external global i32
+@g_i = internal global i32 0
+@t_un = external thread_local global i32
+@t_ld = external thread_local(localdynamic) global i32
+@t_ie = external thread_local(initialexec) global i32
+@t_le = external thread_local(localexec) global i32
+
+declare void @callee1() nounwind
+declare dso_local void @callee2() nounwind
+
+define void @caller() nounwind {
+; CHECK-LABEL: name: caller
+; CHECK:      target-flags(loongarch-got-pc-hi) @g_e
+; CHECK-NEXT: target-flags(loongarch-got-pc-lo) @g_e
+; CHECK:      target-flags(loongarch-pcrel-hi) @g_i
+; CHECK-NEXT: target-flags(loongarch-pcrel-lo) @g_i
+; CHECK:      target-flags(loongarch-gd-pc-hi) @t_un
+; CHECK-NEXT: target-flags(loongarch-got-pc-lo) @t_un
+; CHECK:      target-flags(loongarch-ld-pc-hi) @t_ld
+; CHECK-NEXT: target-flags(loongarch-got-pc-lo) @t_ld
+; CHECK:      target-flags(loongarch-ie-pc-hi) @t_ie
+; CHECK-NEXT: target-flags(loongarch-ie-pc-lo) @t_ie
+; CHECK:      target-flags(loongarch-le-hi) @t_le
+; CHECK-NEXT: target-flags(loongarch-le-lo) @t_le
+; CHECK:      target-flags(loongarch-call-plt) @callee1
+; CHECK:      target-flags(loongarch-call) @callee2
+  %a = load volatile i32, ptr @g_e
+  %b = load volatile i32, ptr @g_i
+  %c = load volatile i32, ptr @t_un
+  %d = load volatile i32, ptr @t_ld
+  %e = load volatile i32, ptr @t_ie
+  %f = load volatile i32, ptr @t_le
+  call i32 @callee1()
+  call i32 @callee2()
+  ret void
+}