[PATCH] D140302: [GlobalISel] [AArch64] Fold G_PTRTOINT(G_CONSTANT)

Andreu Carminati via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 19 06:22:48 PST 2022


andcarminati created this revision.
andcarminati added reviewers: qcolombet, aditya_nandakumar, paquette.
Herald added subscribers: hiraditya, kristof.beyls.
Herald added a project: All.
andcarminati requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

  Fold the following pattern:
  
      %0:_(p0) = G_CONSTANT i32 0
      %1:_(s32) = G_PTRTOINT %0:_(p0)
      $d2 = COPY %1(s32)
      RET implicit $a11, implicit $d2
  
  Into:
      %1:_(s32) = G_CONSTANT i32 0
      $d2 = COPY %1(s32)
      RET implicit $a11, implicit $d2


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D140302

Files:
  llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
  llvm/include/llvm/Target/GlobalISel/Combine.td
  llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
  llvm/test/CodeGen/AArch64/GlobalISel/combine-ptradd-int2ptr.mir


Index: llvm/test/CodeGen/AArch64/GlobalISel/combine-ptradd-int2ptr.mir
===================================================================
--- llvm/test/CodeGen/AArch64/GlobalISel/combine-ptradd-int2ptr.mir
+++ llvm/test/CodeGen/AArch64/GlobalISel/combine-ptradd-int2ptr.mir
@@ -8,9 +8,8 @@
   bb.1:
     liveins: $x0
     ; CHECK-LABEL: name: agc.test_combine_ptradd_constants_intres
-    ; CHECK: [[C:%[0-9]+]]:_(p64) = G_CONSTANT i64 44
-    ; CHECK: [[PTRTOINT:%[0-9]+]]:_(s64) = G_PTRTOINT [[C]](p64)
-    ; CHECK: $x0 = COPY [[PTRTOINT]](s64)
+    ; CHECK: [[C:%[0-9]+]]:_(s64) =  G_CONSTANT i64 44
+    ; CHECK: $x0 = COPY [[C]](s64)
     %1:_(s32) = G_CONSTANT i32 42
     %2:_(s32) = G_CONSTANT i32 2
     %3:_(p64) = G_INTTOPTR %2
Index: llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -2052,6 +2052,25 @@
   MI.eraseFromParent();
 }
 
+bool CombinerHelper::matchCombineP2IToC(MachineInstr &MI, Register &Reg) {
+  assert(MI.getOpcode() == TargetOpcode::G_PTRTOINT && "Expected a G_PTRTOINT");
+  const Register SrcReg = MI.getOperand(1).getReg();
+  const MachineInstr *SrcInstr = MRI.getVRegDef(SrcReg);
+  return SrcInstr->getOpcode() == TargetOpcode::G_CONSTANT;
+}
+
+void CombinerHelper::applyCombineP2IToC(MachineInstr &MI, Register &Reg) {
+  assert(MI.getOpcode() == TargetOpcode::G_PTRTOINT && "Expected a G_PTRTOINT");
+  Register DstReg = MI.getOperand(0).getReg();
+  Register SrcReg = MI.getOperand(1).getReg();
+  MachineInstr *SrcMI = MRI.getVRegDef(SrcReg);
+  const MachineOperand &CstVal = SrcMI->getOperand(1);
+  Builder.setInstr(MI);
+  Builder.buildConstant(DstReg, CstVal.getCImm()->getValue());
+  MI.eraseFromParent();
+  SrcMI->eraseFromParent();
+}
+
 bool CombinerHelper::matchCombineAddP2IToPtrAdd(
     MachineInstr &MI, std::pair<Register, bool> &PtrReg) {
   assert(MI.getOpcode() == TargetOpcode::G_ADD);
Index: llvm/include/llvm/Target/GlobalISel/Combine.td
===================================================================
--- llvm/include/llvm/Target/GlobalISel/Combine.td
+++ llvm/include/llvm/Target/GlobalISel/Combine.td
@@ -462,6 +462,14 @@
   (apply [{ Helper.applyCombineP2IToI2P(*${mi}, ${info}); }])
 >;
 
+// Fold ptr2int(cst) -> cst
+def i2p_to_c: GICombineRule<
+  (defs root:$root, register_matchinfo:$info),
+  (match (wip_match_opcode G_PTRTOINT):$root,
+    [{ return Helper.matchCombineP2IToC(*${root}, ${info}); }]),
+  (apply [{ Helper.applyCombineP2IToC(*${root}, ${info}); }])
+>;
+
 // Fold add ptrtoint(x), y -> ptrtoint (ptr_add x), y
 def add_p2i_to_ptradd_matchinfo : GIDefMatchData<"std::pair<Register, bool>">;
 def add_p2i_to_ptradd : GICombineRule<
@@ -1039,7 +1047,8 @@
                                         add_sub_reg, buildvector_identity_fold,
                                         trunc_buildvector_fold,
                                         trunc_lshr_buildvector_fold,
-                                        bitcast_bitcast_fold]>;
+                                        bitcast_bitcast_fold,
+                                        i2p_to_c]>;
 
 def const_combines : GICombineGroup<[constant_fp_op, const_ptradd_to_i2p,
                                      overlapping_and, mulo_by_2, mulo_by_0,
Index: llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
===================================================================
--- llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
+++ llvm/include/llvm/CodeGen/GlobalISel/CombinerHelper.h
@@ -370,6 +370,10 @@
   /// Transform PtrToInt(IntToPtr(x)) to x.
   void applyCombineP2IToI2P(MachineInstr &MI, Register &Reg);
 
+  /// Transform Fold G_PTRTOINT(G_CONSTANT(x)) to x.
+  bool matchCombineP2IToC(MachineInstr &MI, Register &Reg);
+  void applyCombineP2IToC(MachineInstr &MI, Register &Reg);
+
   /// Transform G_ADD (G_PTRTOINT x), y -> G_PTRTOINT (G_PTR_ADD x, y)
   /// Transform G_ADD y, (G_PTRTOINT x) -> G_PTRTOINT (G_PTR_ADD x, y)
   bool matchCombineAddP2IToPtrAdd(MachineInstr &MI,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140302.483937.patch
Type: text/x-patch
Size: 4123 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221219/7b51de7c/attachment.bin>


More information about the llvm-commits mailing list