[llvm] Promote pseudo opcodes from 32-bit to 64-bit for instructions that infer extsw elimination in PPCMIPeepholes pass (PR #85451)

Lei Huang via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 15 12:02:52 PDT 2024


================
@@ -5234,6 +5234,218 @@ bool PPCInstrInfo::isTOCSaveMI(const MachineInstr &MI) const {
 // We limit the max depth to track incoming values of PHIs or binary ops
 // (e.g. AND) to avoid excessive cost.
 const unsigned MAX_BINOP_DEPTH = 1;
+
+// This function will promote the instruction which defines the register `Reg`
+// in the parameter from a 32-bit to a 64-bit instruction if needed. The logic
+// used to check whether an instruction needs to be promoted or not is similar
+// to the logic used to check whether or not a defined register is sign or zero
+// extended within the function PPCInstrInfo::isSignOrZeroExtended.
+// Additionally, the `promoteInstr32To64ForElimEXTSW` function is recursive.
+// BinOpDepth does not count all of the recursions. The parameter BinOpDepth is
+// incremented  only when `promoteInstr32To64ForElimEXTSW` calls itself more
+// than once. This is done to prevent exponential recursion.
+void PPCInstrInfo::promoteInstr32To64ForElimEXTSW(const Register &Reg,
+                                                  MachineRegisterInfo *MRI,
+                                                  unsigned BinOpDepth,
+                                                  LiveVariables *LV) const {
+  if (!Reg.isVirtual())
+    return;
+
+  MachineInstr *MI = MRI->getVRegDef(Reg);
+  if (!MI)
+    return;
+
+  unsigned Opcode = MI->getOpcode();
+  bool IsNonSignedExtInstrNeedPromoted = false;
+  int NewOpcode = -1;
+
+#define MapOpCode(A)                                                           \
+  case A:                                                                      \
+    NewOpcode = A##8;                                                          \
+    IsNonSignedExtInstrNeedPromoted = true;                                    \
+    break
+
+  switch (Opcode) {
+    MapOpCode(PPC::OR);
+    MapOpCode(PPC::ISEL);
+    MapOpCode(PPC::ORI);
+    MapOpCode(PPC::XORI);
+    MapOpCode(PPC::ORIS);
+    MapOpCode(PPC::XORIS);
+    MapOpCode(PPC::AND);
+  }
+#undef MapOpCode
+
+  switch (Opcode) {
+  case PPC::OR:
+  case PPC::ISEL:
+  case PPC::OR8:
+  case PPC::PHI:
+    if (BinOpDepth < MAX_BINOP_DEPTH) {
----------------
lei137 wrote:

early exit
```suggestion
    if (BinOpDepth >= MAX_BINOP_DEPTH) 
      break;
```

https://github.com/llvm/llvm-project/pull/85451


More information about the llvm-commits mailing list