PseudoLoweringEmitter.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //===- PseudoLoweringEmitter.cpp - PseudoLowering Generator -----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "CodeGenInstruction.h"
  10. #include "CodeGenTarget.h"
  11. #include "llvm/ADT/IndexedMap.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringMap.h"
  14. #include "llvm/Support/Debug.h"
  15. #include "llvm/Support/ErrorHandling.h"
  16. #include "llvm/TableGen/Error.h"
  17. #include "llvm/TableGen/Record.h"
  18. #include "llvm/TableGen/TableGenBackend.h"
  19. #include <vector>
  20. using namespace llvm;
  21. #define DEBUG_TYPE "pseudo-lowering"
  22. namespace {
  23. class PseudoLoweringEmitter {
  24. struct OpData {
  25. enum MapKind { Operand, Imm, Reg };
  26. MapKind Kind;
  27. union {
  28. unsigned Operand; // Operand number mapped to.
  29. uint64_t Imm; // Integer immedate value.
  30. Record *Reg; // Physical register.
  31. } Data;
  32. };
  33. struct PseudoExpansion {
  34. CodeGenInstruction Source; // The source pseudo instruction definition.
  35. CodeGenInstruction Dest; // The destination instruction to lower to.
  36. IndexedMap<OpData> OperandMap;
  37. PseudoExpansion(CodeGenInstruction &s, CodeGenInstruction &d,
  38. IndexedMap<OpData> &m) :
  39. Source(s), Dest(d), OperandMap(m) {}
  40. };
  41. RecordKeeper &Records;
  42. // It's overkill to have an instance of the full CodeGenTarget object,
  43. // but it loads everything on demand, not in the constructor, so it's
  44. // lightweight in performance, so it works out OK.
  45. CodeGenTarget Target;
  46. SmallVector<PseudoExpansion, 64> Expansions;
  47. unsigned addDagOperandMapping(Record *Rec, DagInit *Dag,
  48. CodeGenInstruction &Insn,
  49. IndexedMap<OpData> &OperandMap,
  50. unsigned BaseIdx);
  51. void evaluateExpansion(Record *Pseudo);
  52. void emitLoweringEmitter(raw_ostream &o);
  53. public:
  54. PseudoLoweringEmitter(RecordKeeper &R) : Records(R), Target(R) {}
  55. /// run - Output the pseudo-lowerings.
  56. void run(raw_ostream &o);
  57. };
  58. } // End anonymous namespace
  59. // FIXME: This pass currently can only expand a pseudo to a single instruction.
  60. // The pseudo expansion really should take a list of dags, not just
  61. // a single dag, so we can do fancier things.
  62. unsigned PseudoLoweringEmitter::
  63. addDagOperandMapping(Record *Rec, DagInit *Dag, CodeGenInstruction &Insn,
  64. IndexedMap<OpData> &OperandMap, unsigned BaseIdx) {
  65. unsigned OpsAdded = 0;
  66. for (unsigned i = 0, e = Dag->getNumArgs(); i != e; ++i) {
  67. if (DefInit *DI = dyn_cast<DefInit>(Dag->getArg(i))) {
  68. // Physical register reference. Explicit check for the special case
  69. // "zero_reg" definition.
  70. if (DI->getDef()->isSubClassOf("Register") ||
  71. DI->getDef()->getName() == "zero_reg") {
  72. OperandMap[BaseIdx + i].Kind = OpData::Reg;
  73. OperandMap[BaseIdx + i].Data.Reg = DI->getDef();
  74. ++OpsAdded;
  75. continue;
  76. }
  77. // Normal operands should always have the same type, or we have a
  78. // problem.
  79. // FIXME: We probably shouldn't ever get a non-zero BaseIdx here.
  80. assert(BaseIdx == 0 && "Named subargument in pseudo expansion?!");
  81. if (DI->getDef() != Insn.Operands[BaseIdx + i].Rec)
  82. PrintFatalError(Rec->getLoc(),
  83. "Pseudo operand type '" + DI->getDef()->getName() +
  84. "' does not match expansion operand type '" +
  85. Insn.Operands[BaseIdx + i].Rec->getName() + "'");
  86. // Source operand maps to destination operand. The Data element
  87. // will be filled in later, just set the Kind for now. Do it
  88. // for each corresponding MachineInstr operand, not just the first.
  89. for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
  90. OperandMap[BaseIdx + i + I].Kind = OpData::Operand;
  91. OpsAdded += Insn.Operands[i].MINumOperands;
  92. } else if (IntInit *II = dyn_cast<IntInit>(Dag->getArg(i))) {
  93. OperandMap[BaseIdx + i].Kind = OpData::Imm;
  94. OperandMap[BaseIdx + i].Data.Imm = II->getValue();
  95. ++OpsAdded;
  96. } else if (DagInit *SubDag = dyn_cast<DagInit>(Dag->getArg(i))) {
  97. // Just add the operands recursively. This is almost certainly
  98. // a constant value for a complex operand (> 1 MI operand).
  99. unsigned NewOps =
  100. addDagOperandMapping(Rec, SubDag, Insn, OperandMap, BaseIdx + i);
  101. OpsAdded += NewOps;
  102. // Since we added more than one, we also need to adjust the base.
  103. BaseIdx += NewOps - 1;
  104. } else
  105. llvm_unreachable("Unhandled pseudo-expansion argument type!");
  106. }
  107. return OpsAdded;
  108. }
  109. void PseudoLoweringEmitter::evaluateExpansion(Record *Rec) {
  110. DEBUG(dbgs() << "Pseudo definition: " << Rec->getName() << "\n");
  111. // Validate that the result pattern has the corrent number and types
  112. // of arguments for the instruction it references.
  113. DagInit *Dag = Rec->getValueAsDag("ResultInst");
  114. assert(Dag && "Missing result instruction in pseudo expansion!");
  115. DEBUG(dbgs() << " Result: " << *Dag << "\n");
  116. DefInit *OpDef = dyn_cast<DefInit>(Dag->getOperator());
  117. if (!OpDef)
  118. PrintFatalError(Rec->getLoc(), Rec->getName() +
  119. " has unexpected operator type!");
  120. Record *Operator = OpDef->getDef();
  121. if (!Operator->isSubClassOf("Instruction"))
  122. PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
  123. "' is not an instruction!");
  124. CodeGenInstruction Insn(Operator);
  125. if (Insn.isCodeGenOnly || Insn.isPseudo)
  126. PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
  127. "' cannot be another pseudo instruction!");
  128. if (Insn.Operands.size() != Dag->getNumArgs())
  129. PrintFatalError(Rec->getLoc(), "Pseudo result '" + Operator->getName() +
  130. "' operand count mismatch");
  131. unsigned NumMIOperands = 0;
  132. for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i)
  133. NumMIOperands += Insn.Operands[i].MINumOperands;
  134. IndexedMap<OpData> OperandMap;
  135. OperandMap.grow(NumMIOperands);
  136. addDagOperandMapping(Rec, Dag, Insn, OperandMap, 0);
  137. // If there are more operands that weren't in the DAG, they have to
  138. // be operands that have default values, or we have an error. Currently,
  139. // Operands that are a subclass of OperandWithDefaultOp have default values.
  140. // Validate that each result pattern argument has a matching (by name)
  141. // argument in the source instruction, in either the (outs) or (ins) list.
  142. // Also check that the type of the arguments match.
  143. //
  144. // Record the mapping of the source to result arguments for use by
  145. // the lowering emitter.
  146. CodeGenInstruction SourceInsn(Rec);
  147. StringMap<unsigned> SourceOperands;
  148. for (unsigned i = 0, e = SourceInsn.Operands.size(); i != e; ++i)
  149. SourceOperands[SourceInsn.Operands[i].Name] = i;
  150. DEBUG(dbgs() << " Operand mapping:\n");
  151. for (unsigned i = 0, e = Insn.Operands.size(); i != e; ++i) {
  152. // We've already handled constant values. Just map instruction operands
  153. // here.
  154. if (OperandMap[Insn.Operands[i].MIOperandNo].Kind != OpData::Operand)
  155. continue;
  156. StringMap<unsigned>::iterator SourceOp =
  157. SourceOperands.find(Dag->getArgName(i));
  158. if (SourceOp == SourceOperands.end())
  159. PrintFatalError(Rec->getLoc(),
  160. "Pseudo output operand '" + Dag->getArgName(i) +
  161. "' has no matching source operand.");
  162. // Map the source operand to the destination operand index for each
  163. // MachineInstr operand.
  164. for (unsigned I = 0, E = Insn.Operands[i].MINumOperands; I != E; ++I)
  165. OperandMap[Insn.Operands[i].MIOperandNo + I].Data.Operand =
  166. SourceOp->getValue();
  167. DEBUG(dbgs() << " " << SourceOp->getValue() << " ==> " << i << "\n");
  168. }
  169. Expansions.push_back(PseudoExpansion(SourceInsn, Insn, OperandMap));
  170. }
  171. void PseudoLoweringEmitter::emitLoweringEmitter(raw_ostream &o) {
  172. // Emit file header.
  173. emitSourceFileHeader("Pseudo-instruction MC lowering Source Fragment", o);
  174. o << "bool " << Target.getName() + "AsmPrinter" << "::\n"
  175. << "emitPseudoExpansionLowering(MCStreamer &OutStreamer,\n"
  176. << " const MachineInstr *MI) {\n";
  177. if (!Expansions.empty()) {
  178. o << " switch (MI->getOpcode()) {\n"
  179. << " default: return false;\n";
  180. for (auto &Expansion : Expansions) {
  181. CodeGenInstruction &Source = Expansion.Source;
  182. CodeGenInstruction &Dest = Expansion.Dest;
  183. o << " case " << Source.Namespace << "::"
  184. << Source.TheDef->getName() << ": {\n"
  185. << " MCInst TmpInst;\n"
  186. << " MCOperand MCOp;\n"
  187. << " TmpInst.setOpcode(" << Dest.Namespace << "::"
  188. << Dest.TheDef->getName() << ");\n";
  189. // Copy the operands from the source instruction.
  190. // FIXME: Instruction operands with defaults values (predicates and cc_out
  191. // in ARM, for example shouldn't need explicit values in the
  192. // expansion DAG.
  193. unsigned MIOpNo = 0;
  194. for (const auto &DestOperand : Dest.Operands) {
  195. o << " // Operand: " << DestOperand.Name << "\n";
  196. for (unsigned i = 0, e = DestOperand.MINumOperands; i != e; ++i) {
  197. switch (Expansion.OperandMap[MIOpNo + i].Kind) {
  198. case OpData::Operand:
  199. o << " lowerOperand(MI->getOperand("
  200. << Source.Operands[Expansion.OperandMap[MIOpNo].Data
  201. .Operand].MIOperandNo + i
  202. << "), MCOp);\n"
  203. << " TmpInst.addOperand(MCOp);\n";
  204. break;
  205. case OpData::Imm:
  206. o << " TmpInst.addOperand(MCOperand::createImm("
  207. << Expansion.OperandMap[MIOpNo + i].Data.Imm << "));\n";
  208. break;
  209. case OpData::Reg: {
  210. Record *Reg = Expansion.OperandMap[MIOpNo + i].Data.Reg;
  211. o << " TmpInst.addOperand(MCOperand::createReg(";
  212. // "zero_reg" is special.
  213. if (Reg->getName() == "zero_reg")
  214. o << "0";
  215. else
  216. o << Reg->getValueAsString("Namespace") << "::"
  217. << Reg->getName();
  218. o << "));\n";
  219. break;
  220. }
  221. }
  222. }
  223. MIOpNo += DestOperand.MINumOperands;
  224. }
  225. if (Dest.Operands.isVariadic) {
  226. MIOpNo = Source.Operands.size() + 1;
  227. o << " // variable_ops\n";
  228. o << " for (unsigned i = " << MIOpNo
  229. << ", e = MI->getNumOperands(); i != e; ++i)\n"
  230. << " if (lowerOperand(MI->getOperand(i), MCOp))\n"
  231. << " TmpInst.addOperand(MCOp);\n";
  232. }
  233. o << " EmitToStreamer(OutStreamer, TmpInst);\n"
  234. << " break;\n"
  235. << " }\n";
  236. }
  237. o << " }\n return true;";
  238. } else
  239. o << " return false;";
  240. o << "\n}\n\n";
  241. }
  242. void PseudoLoweringEmitter::run(raw_ostream &o) {
  243. Record *ExpansionClass = Records.getClass("PseudoInstExpansion");
  244. Record *InstructionClass = Records.getClass("Instruction");
  245. assert(ExpansionClass && "PseudoInstExpansion class definition missing!");
  246. assert(InstructionClass && "Instruction class definition missing!");
  247. std::vector<Record*> Insts;
  248. for (const auto &D : Records.getDefs()) {
  249. if (D.second->isSubClassOf(ExpansionClass) &&
  250. D.second->isSubClassOf(InstructionClass))
  251. Insts.push_back(D.second.get());
  252. }
  253. // Process the pseudo expansion definitions, validating them as we do so.
  254. for (unsigned i = 0, e = Insts.size(); i != e; ++i)
  255. evaluateExpansion(Insts[i]);
  256. // Generate expansion code to lower the pseudo to an MCInst of the real
  257. // instruction.
  258. emitLoweringEmitter(o);
  259. }
  260. namespace llvm {
  261. void EmitPseudoLowering(RecordKeeper &RK, raw_ostream &OS) {
  262. PseudoLoweringEmitter(RK).run(OS);
  263. }
  264. } // End llvm namespace