186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//===- PseudoLoweringEmitter.cpp - PseudoLowering Generator -----*- C++ -*-===//
286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//
386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//                     The LLVM Compiler Infrastructure
486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//
586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach// This file is distributed under the University of Illinois Open Source
686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach// License. See LICENSE.TXT for details.
786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//
886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//===----------------------------------------------------------------------===//
986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
1086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach#include "CodeGenInstruction.h"
116f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen#include "CodeGenTarget.h"
1286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach#include "llvm/ADT/IndexedMap.h"
136f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen#include "llvm/ADT/SmallVector.h"
1486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach#include "llvm/ADT/StringMap.h"
1586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach#include "llvm/Support/Debug.h"
166f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen#include "llvm/Support/ErrorHandling.h"
176f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen#include "llvm/TableGen/Error.h"
186f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen#include "llvm/TableGen/Record.h"
196f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen#include "llvm/TableGen/TableGenBackend.h"
2086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach#include <vector>
2186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbachusing namespace llvm;
2286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
23dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "pseudo-lowering"
24dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
256f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesennamespace {
266f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesenclass PseudoLoweringEmitter {
276f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  struct OpData {
286f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    enum MapKind { Operand, Imm, Reg };
296f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    MapKind Kind;
306f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    union {
316f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen      unsigned Operand;   // Operand number mapped to.
326f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen      uint64_t Imm;       // Integer immedate value.
336f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen      Record *Reg;        // Physical register.
346f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    } Data;
356f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  };
366f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  struct PseudoExpansion {
376f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    CodeGenInstruction Source;  // The source pseudo instruction definition.
386f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    CodeGenInstruction Dest;    // The destination instruction to lower to.
396f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    IndexedMap<OpData> OperandMap;
406f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
416f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen    PseudoExpansion(CodeGenInstruction &s, CodeGenInstruction &d,
426f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen                    IndexedMap<OpData> &m) :
436f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen      Source(s), Dest(d), OperandMap(m) {}
446f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  };
456f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
466f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  RecordKeeper &Records;
476f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
486f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  // It's overkill to have an instance of the full CodeGenTarget object,
496f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  // but it loads everything on demand, not in the constructor, so it's
506f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  // lightweight in performance, so it works out OK.
516f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  CodeGenTarget Target;
526f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
536f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  SmallVector<PseudoExpansion, 64> Expansions;
546f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
556f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  unsigned addDagOperandMapping(Record *Rec, DagInit *Dag,
566f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen                                CodeGenInstruction &Insn,
576f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen                                IndexedMap<OpData> &OperandMap,
586f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen                                unsigned BaseIdx);
596f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  void evaluateExpansion(Record *Pseudo);
606f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  void emitLoweringEmitter(raw_ostream &o);
616f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesenpublic:
626f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  PseudoLoweringEmitter(RecordKeeper &R) : Records(R), Target(R) {}
636f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
646f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  /// run - Output the pseudo-lowerings.
656f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  void run(raw_ostream &o);
666f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen};
676f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen} // End anonymous namespace
686f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
6986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach// FIXME: This pass currently can only expand a pseudo to a single instruction.
7086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//        The pseudo expansion really should take a list of dags, not just
7186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach//        a single dag, so we can do fancier things.
7286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
7386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbachunsigned PseudoLoweringEmitter::
7405bce0beee87512e52428d4b80f5a8e79a949576David GreeneaddDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Insn,
7586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach                     IndexedMap<OpData> &OperandMap, unsigned BaseIdx) {
7686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  unsigned OpsAdded = 0;
7786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
786cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva    if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i))) {
7986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // Physical register reference. Explicit check for the special case
8086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // "zero_reg" definition.
8186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      if (DI->getDef()->isSubClassOf("Register") ||
8286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach          DI->getDef()->getName() == "zero_reg") {
8386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        OperandMap[BaseIdx + i].Kind = OpData::Reg;
8486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        OperandMap[BaseIdx + i].Data.Reg = DI->getDef();
8586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        ++OpsAdded;
8686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        continue;
8786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      }
8886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
8986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // Normal operands should always have the same type, or we have a
9086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // problem.
9186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // FIXME: We probably shouldn't ever get a non-zero BaseIdx here.
9286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      assert(BaseIdx == 0 && "Named subargument in pseudo expansion?!");
9386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      if (DI->getDef() != Insn.Operands[BaseIdx + i].Rec)
9461131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger        PrintFatalError(Rec->getLoc(),
9586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach                      "Pseudo operand type '" + DI->getDef()->getName() +
9686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach                      "' does not match expansion operand type '" +
9786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach                      Insn.Operands[BaseIdx + i].Rec->getName() + "'");
9886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // Source operand maps to destination operand. The Data element
9986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // will be filled in later, just set the Kind for now. Do it
10086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // for each corresponding MachineInstr operand, not just the first.
10186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
10286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        OperandMap[BaseIdx + i + I].Kind = OpData::Operand;
10386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      OpsAdded += Insn.Operands[i].MINumOperands;
1046cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva    } else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i))) {
10586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      OperandMap[BaseIdx + i].Kind = OpData::Imm;
10686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      OperandMap[BaseIdx + i].Data.Imm = II->getValue();
10786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      ++OpsAdded;
1086cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva    } else if (DagInit *SubDag = dyn_cast<DagInit>(Dag->getArg(i))) {
10986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // Just add the operands recursively. This is almost certainly
11086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // a constant value for a complex operand (> 1 MI operand).
11186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      unsigned NewOps =
11286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        addDagOperandMapping(Rec, SubDag, Insn, OperandMap, BaseIdx + i);
11386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      OpsAdded += NewOps;
11486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      // Since we added more than one, we also need to adjust the base.
11586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      BaseIdx += NewOps - 1;
11686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    } else
117655b8de7b2ab773a977e0c524307e71354d8af29Craig Topper      llvm_unreachable("Unhandled pseudo-expansion argument type!");
11886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  }
11986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  return OpsAdded;
12086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach}
12186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
12286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbachvoid PseudoLoweringEmitter::evaluateExpansion(Record *Rec) {
12386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  DEBUG(dbgs() << "Pseudo definition: " << Rec->getName() << "\n");
12486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
12586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Validate that the result pattern has the corrent number and types
12686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // of arguments for the instruction it references.
12705bce0beee87512e52428d4b80f5a8e79a949576David Greene  DagInit *Dag = Rec->getValueAsDag("ResultInst");
12886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  assert(Dag && "Missing result instruction in pseudo expansion!");
12986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  DEBUG(dbgs() << "  Result: " << *Dag << "\n");
13086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
1316cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva  DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
13286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  if (!OpDef)
13361131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(Rec->getLoc(), Rec->getName() +
13486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach                  " has unexpected operator type!");
13586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  Record *Operator = OpDef->getDef();
13686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  if (!Operator->isSubClassOf("Instruction"))
13761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
13861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                    "' is not an instruction!");
13986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
14086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  CodeGenInstruction Insn(Operator);
14186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
14286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  if (Insn.isCodeGenOnly || Insn.isPseudo)
14361131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
14461131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                    "' cannot be another pseudo instruction!");
14586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
14686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  if (Insn.Operands.size() != Dag->getNumArgs())
14761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger    PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
14861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                    "' operand count mismatch");
14986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
150aa7b3df1788e6ffdaa9b661cfdccdc41ad181567Evan Cheng  unsigned NumMIOperands = 0;
151aa7b3df1788e6ffdaa9b661cfdccdc41ad181567Evan Cheng  for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i)
152aa7b3df1788e6ffdaa9b661cfdccdc41ad181567Evan Cheng    NumMIOperands += Insn.Operands[i].MINumOperands;
15386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  IndexedMap<OpData> OperandMap;
154aa7b3df1788e6ffdaa9b661cfdccdc41ad181567Evan Cheng  OperandMap.grow(NumMIOperands);
15586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
15686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  addDagOperandMapping(Rec, Dag, Insn, OperandMap, 0);
15786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
15886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // If there are more operands that weren't in the DAG, they have to
15986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // be operands that have default values, or we have an error. Currently,
160dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  // Operands that are a subclass of OperandWithDefaultOp have default values.
16186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
16286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Validate that each result pattern argument has a matching (by name)
16386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // argument in the source instruction, in either the (outs) or (ins) list.
16486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Also check that the type of the arguments match.
16586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  //
16686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Record the mapping of the source to result arguments for use by
16786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // the lowering emitter.
16886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  CodeGenInstruction SourceInsn(Rec);
16986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  StringMap<unsigned> SourceOperands;
17086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  for (unsigned i = 0, e = SourceInsn.Operands.size(); i != e; ++i)
17186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    SourceOperands[SourceInsn.Operands[i].Name] = i;
17286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
17386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  DEBUG(dbgs() << "  Operand mapping:\n");
17486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i) {
17586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    // We've already handled constant values. Just map instruction operands
17686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    // here.
17786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    if (OperandMap[Insn.Operands[i].MIOperandNo].Kind != OpData::Operand)
17886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      continue;
17986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    StringMap<unsigned>::iterator SourceOp =
18086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      SourceOperands.find(Dag->getArgName(i));
18186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    if (SourceOp == SourceOperands.end())
18261131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger      PrintFatalError(Rec->getLoc(),
18361131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                      "Pseudo output operand '" + Dag->getArgName(i) +
18461131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger                      "' has no matching source operand.");
18586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    // Map the source operand to the destination operand index for each
18686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    // MachineInstr operand.
18786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
18886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      OperandMap[Insn.Operands[i].MIOperandNo + I].Data.Operand =
18986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        SourceOp->getValue();
19086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
19186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    DEBUG(dbgs() << "    " << SourceOp->getValue() << " ==> " << i << "\n");
19286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  }
19386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
19486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  Expansions.push_back(PseudoExpansion(SourceInsn, Insn, OperandMap));
19586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach}
19686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
19786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbachvoid PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
19886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Emit file header.
1996f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  emitSourceFileHeader("Pseudo-instruction MC lowering Source Fragment", o);
20086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
20186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  o << "bool " << Target.getName() + "AsmPrinter" << "::\n"
20286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    << "emitPseudoExpansionLowering(MCStreamer &OutStreamer,\n"
203dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    << "                            const MachineInstr *MI) {\n";
20486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
205dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!Expansions.empty()) {
206dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    o << "  switch (MI->getOpcode()) {\n"
207dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      << "    default: return false;\n";
208dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    for (auto &Expansion : Expansions) {
209dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      CodeGenInstruction &Source = Expansion.Source;
210dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      CodeGenInstruction &Dest = Expansion.Dest;
211dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      o << "    case " << Source.Namespace << "::"
212dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << Source.TheDef->getName() << ": {\n"
213dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << "      MCInst TmpInst;\n"
214dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << "      MCOperand MCOp;\n"
215dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << "      TmpInst.setOpcode(" << Dest.Namespace << "::"
216dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << Dest.TheDef->getName() << ");\n";
217dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
218dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // Copy the operands from the source instruction.
219dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      // FIXME: Instruction operands with defaults values (predicates and cc_out
220dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      //        in ARM, for example shouldn't need explicit values in the
221dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      //        expansion DAG.
222dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      unsigned MIOpNo = 0;
223dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      for (const auto &DestOperand : Dest.Operands) {
224dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        o << "      // Operand: " << DestOperand.Name << "\n";
225dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        for (unsigned i = 0, e = DestOperand.MINumOperands; i != e; ++i) {
226dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines          switch (Expansion.OperandMap[MIOpNo + i].Kind) {
227dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            case OpData::Operand:
228dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            o << "      lowerOperand(MI->getOperand("
229dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              << Source.Operands[Expansion.OperandMap[MIOpNo].Data
230dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              .Operand].MIOperandNo + i
231dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              << "), MCOp);\n"
232dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              << "      TmpInst.addOperand(MCOp);\n";
233dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            break;
234dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            case OpData::Imm:
235dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            o << "      TmpInst.addOperand(MCOperand::CreateImm("
236dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              << Expansion.OperandMap[MIOpNo + i].Data.Imm << "));\n";
237dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            break;
238dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            case OpData::Reg: {
239dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              Record *Reg = Expansion.OperandMap[MIOpNo + i].Data.Reg;
240dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              o << "      TmpInst.addOperand(MCOperand::CreateReg(";
241dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              // "zero_reg" is special.
242dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              if (Reg->getName() == "zero_reg")
243dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                o << "0";
244dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              else
245dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                o << Reg->getValueAsString("Namespace") << "::"
246dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                  << Reg->getName();
247dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              o << "));\n";
248dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines              break;
249dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines            }
250dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines          }
25186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        }
252dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        MIOpNo += DestOperand.MINumOperands;
25386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      }
254dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      if (Dest.Operands.isVariadic) {
255dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        MIOpNo = Source.Operands.size() + 1;
256dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        o << "      // variable_ops\n";
257dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        o << "      for (unsigned i = " << MIOpNo
258dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines          << ", e = MI->getNumOperands(); i != e; ++i)\n"
259dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines          << "        if (lowerOperand(MI->getOperand(i), MCOp))\n"
260dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines          << "          TmpInst.addOperand(MCOp);\n";
261dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      }
262dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      o << "      EmitToStreamer(OutStreamer, TmpInst);\n"
263dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << "      break;\n"
264dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        << "    }\n";
26586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    }
266dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    o << "  }\n  return true;";
267dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  } else
268dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    o << "  return false;";
269dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
270dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  o << "\n}\n\n";
27186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach}
27286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
27386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbachvoid PseudoLoweringEmitter::run(raw_ostream &o) {
27486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  Record *ExpansionClass = Records.getClass("PseudoInstExpansion");
275efd841cdc5ba7e3b8e7299e0523cf80d460b44baMichael Liao  Record *InstructionClass = Records.getClass("Instruction");
27686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  assert(ExpansionClass && "PseudoInstExpansion class definition missing!");
27786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  assert(InstructionClass && "Instruction class definition missing!");
27886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
27986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  std::vector<Record*> Insts;
28086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  for (std::map<std::string, Record*>::const_iterator I =
28186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach         Records.getDefs().begin(), E = Records.getDefs().end(); I != E; ++I) {
28286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    if (I->second->isSubClassOf(ExpansionClass) &&
28386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach        I->second->isSubClassOf(InstructionClass))
28486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach      Insts.push_back(I->second);
28586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  }
28686f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
28786f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Process the pseudo expansion definitions, validating them as we do so.
28886f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  for (unsigned i = 0, e = Insts.size(); i != e; ++i)
28986f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach    evaluateExpansion(Insts[i]);
29086f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
29186f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // Generate expansion code to lower the pseudo to an MCInst of the real
29286f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  // instruction.
29386f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach  emitLoweringEmitter(o);
29486f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach}
29586f9adb8becf5da6962bd89301e96bccba26f72aJim Grosbach
2966f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesennamespace llvm {
2976f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
2986f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesenvoid EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS) {
2996f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen  PseudoLoweringEmitter(RK).run(OS);
3006f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen}
3016f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen
3026f36fa981a59461466e12e5056ba209d289b81b1Jakob Stoklund Olesen} // End llvm namespace
303