CodeGenDAGPatterns.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. //===- CodeGenDAGPatterns.h - Read DAG patterns from .td file ---*- 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. //
  10. // This file declares the CodeGenDAGPatterns class, which is used to read and
  11. // represent the patterns present in a .td file for instructions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
  15. #define LLVM_UTILS_TABLEGEN_CODEGENDAGPATTERNS_H
  16. #include "CodeGenIntrinsics.h"
  17. #include "CodeGenTarget.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include <algorithm>
  22. #include <map>
  23. #include <set>
  24. #include <vector>
  25. namespace llvm {
  26. class Record;
  27. class Init;
  28. class ListInit;
  29. class DagInit;
  30. class SDNodeInfo;
  31. class TreePattern;
  32. class TreePatternNode;
  33. class CodeGenDAGPatterns;
  34. class ComplexPattern;
  35. /// EEVT::DAGISelGenValueType - These are some extended forms of
  36. /// MVT::SimpleValueType that we use as lattice values during type inference.
  37. /// The existing MVT iAny, fAny and vAny types suffice to represent
  38. /// arbitrary integer, floating-point, and vector types, so only an unknown
  39. /// value is needed.
  40. namespace EEVT {
  41. /// TypeSet - This is either empty if it's completely unknown, or holds a set
  42. /// of types. It is used during type inference because register classes can
  43. /// have multiple possible types and we don't know which one they get until
  44. /// type inference is complete.
  45. ///
  46. /// TypeSet can have three states:
  47. /// Vector is empty: The type is completely unknown, it can be any valid
  48. /// target type.
  49. /// Vector has multiple constrained types: (e.g. v4i32 + v4f32) it is one
  50. /// of those types only.
  51. /// Vector has one concrete type: The type is completely known.
  52. ///
  53. class TypeSet {
  54. SmallVector<MVT::SimpleValueType, 4> TypeVec;
  55. public:
  56. TypeSet() {}
  57. TypeSet(MVT::SimpleValueType VT, TreePattern &TP);
  58. TypeSet(ArrayRef<MVT::SimpleValueType> VTList);
  59. bool isCompletelyUnknown() const { return TypeVec.empty(); }
  60. bool isConcrete() const {
  61. if (TypeVec.size() != 1) return false;
  62. unsigned char T = TypeVec[0]; (void)T;
  63. assert(T < MVT::LAST_VALUETYPE || T == MVT::iPTR || T == MVT::iPTRAny);
  64. return true;
  65. }
  66. MVT::SimpleValueType getConcrete() const {
  67. assert(isConcrete() && "Type isn't concrete yet");
  68. return (MVT::SimpleValueType)TypeVec[0];
  69. }
  70. bool isDynamicallyResolved() const {
  71. return getConcrete() == MVT::iPTR || getConcrete() == MVT::iPTRAny;
  72. }
  73. const SmallVectorImpl<MVT::SimpleValueType> &getTypeList() const {
  74. assert(!TypeVec.empty() && "Not a type list!");
  75. return TypeVec;
  76. }
  77. bool isVoid() const {
  78. return TypeVec.size() == 1 && TypeVec[0] == MVT::isVoid;
  79. }
  80. /// hasIntegerTypes - Return true if this TypeSet contains any integer value
  81. /// types.
  82. bool hasIntegerTypes() const;
  83. /// hasFloatingPointTypes - Return true if this TypeSet contains an fAny or
  84. /// a floating point value type.
  85. bool hasFloatingPointTypes() const;
  86. /// hasScalarTypes - Return true if this TypeSet contains a scalar value
  87. /// type.
  88. bool hasScalarTypes() const;
  89. /// hasVectorTypes - Return true if this TypeSet contains a vector value
  90. /// type.
  91. bool hasVectorTypes() const;
  92. /// getName() - Return this TypeSet as a string.
  93. std::string getName() const;
  94. /// MergeInTypeInfo - This merges in type information from the specified
  95. /// argument. If 'this' changes, it returns true. If the two types are
  96. /// contradictory (e.g. merge f32 into i32) then this flags an error.
  97. bool MergeInTypeInfo(const EEVT::TypeSet &InVT, TreePattern &TP);
  98. bool MergeInTypeInfo(MVT::SimpleValueType InVT, TreePattern &TP) {
  99. return MergeInTypeInfo(EEVT::TypeSet(InVT, TP), TP);
  100. }
  101. /// Force this type list to only contain integer types.
  102. bool EnforceInteger(TreePattern &TP);
  103. /// Force this type list to only contain floating point types.
  104. bool EnforceFloatingPoint(TreePattern &TP);
  105. /// EnforceScalar - Remove all vector types from this type list.
  106. bool EnforceScalar(TreePattern &TP);
  107. /// EnforceVector - Remove all non-vector types from this type list.
  108. bool EnforceVector(TreePattern &TP);
  109. /// EnforceSmallerThan - 'this' must be a smaller VT than Other. Update
  110. /// this an other based on this information.
  111. bool EnforceSmallerThan(EEVT::TypeSet &Other, TreePattern &TP);
  112. /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
  113. /// whose element is VT.
  114. bool EnforceVectorEltTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
  115. /// EnforceVectorEltTypeIs - 'this' is now constrainted to be a vector type
  116. /// whose element is VT.
  117. bool EnforceVectorEltTypeIs(MVT::SimpleValueType VT, TreePattern &TP);
  118. /// EnforceVectorSubVectorTypeIs - 'this' is now constrainted to
  119. /// be a vector type VT.
  120. bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
  121. /// EnforceVectorSameNumElts - 'this' is now constrainted to
  122. /// be a vector with same num elements as VT.
  123. bool EnforceVectorSameNumElts(EEVT::TypeSet &VT, TreePattern &TP);
  124. bool operator!=(const TypeSet &RHS) const { return TypeVec != RHS.TypeVec; }
  125. bool operator==(const TypeSet &RHS) const { return TypeVec == RHS.TypeVec; }
  126. private:
  127. /// FillWithPossibleTypes - Set to all legal types and return true, only
  128. /// valid on completely unknown type sets. If Pred is non-null, only MVTs
  129. /// that pass the predicate are added.
  130. bool FillWithPossibleTypes(TreePattern &TP,
  131. bool (*Pred)(MVT::SimpleValueType) = nullptr,
  132. const char *PredicateName = nullptr);
  133. };
  134. }
  135. /// Set type used to track multiply used variables in patterns
  136. typedef std::set<std::string> MultipleUseVarSet;
  137. /// SDTypeConstraint - This is a discriminated union of constraints,
  138. /// corresponding to the SDTypeConstraint tablegen class in Target.td.
  139. struct SDTypeConstraint {
  140. SDTypeConstraint(Record *R);
  141. unsigned OperandNo; // The operand # this constraint applies to.
  142. enum {
  143. SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisVec, SDTCisSameAs,
  144. SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisEltOfVec,
  145. SDTCisSubVecOfVec, SDTCVecEltisVT, SDTCisSameNumEltsAs
  146. } ConstraintType;
  147. union { // The discriminated union.
  148. struct {
  149. MVT::SimpleValueType VT;
  150. } SDTCisVT_Info;
  151. struct {
  152. unsigned OtherOperandNum;
  153. } SDTCisSameAs_Info;
  154. struct {
  155. unsigned OtherOperandNum;
  156. } SDTCisVTSmallerThanOp_Info;
  157. struct {
  158. unsigned BigOperandNum;
  159. } SDTCisOpSmallerThanOp_Info;
  160. struct {
  161. unsigned OtherOperandNum;
  162. } SDTCisEltOfVec_Info;
  163. struct {
  164. unsigned OtherOperandNum;
  165. } SDTCisSubVecOfVec_Info;
  166. struct {
  167. MVT::SimpleValueType VT;
  168. } SDTCVecEltisVT_Info;
  169. struct {
  170. unsigned OtherOperandNum;
  171. } SDTCisSameNumEltsAs_Info;
  172. } x;
  173. /// ApplyTypeConstraint - Given a node in a pattern, apply this type
  174. /// constraint to the nodes operands. This returns true if it makes a
  175. /// change, false otherwise. If a type contradiction is found, an error
  176. /// is flagged.
  177. bool ApplyTypeConstraint(TreePatternNode *N, const SDNodeInfo &NodeInfo,
  178. TreePattern &TP) const;
  179. };
  180. /// SDNodeInfo - One of these records is created for each SDNode instance in
  181. /// the target .td file. This represents the various dag nodes we will be
  182. /// processing.
  183. class SDNodeInfo {
  184. Record *Def;
  185. std::string EnumName;
  186. std::string SDClassName;
  187. unsigned Properties;
  188. unsigned NumResults;
  189. int NumOperands;
  190. std::vector<SDTypeConstraint> TypeConstraints;
  191. public:
  192. SDNodeInfo(Record *R); // Parse the specified record.
  193. unsigned getNumResults() const { return NumResults; }
  194. /// getNumOperands - This is the number of operands required or -1 if
  195. /// variadic.
  196. int getNumOperands() const { return NumOperands; }
  197. Record *getRecord() const { return Def; }
  198. const std::string &getEnumName() const { return EnumName; }
  199. const std::string &getSDClassName() const { return SDClassName; }
  200. const std::vector<SDTypeConstraint> &getTypeConstraints() const {
  201. return TypeConstraints;
  202. }
  203. /// getKnownType - If the type constraints on this node imply a fixed type
  204. /// (e.g. all stores return void, etc), then return it as an
  205. /// MVT::SimpleValueType. Otherwise, return MVT::Other.
  206. MVT::SimpleValueType getKnownType(unsigned ResNo) const;
  207. /// hasProperty - Return true if this node has the specified property.
  208. ///
  209. bool hasProperty(enum SDNP Prop) const { return Properties & (1 << Prop); }
  210. /// ApplyTypeConstraints - Given a node in a pattern, apply the type
  211. /// constraints for this node to the operands of the node. This returns
  212. /// true if it makes a change, false otherwise. If a type contradiction is
  213. /// found, an error is flagged.
  214. bool ApplyTypeConstraints(TreePatternNode *N, TreePattern &TP) const {
  215. bool MadeChange = false;
  216. for (unsigned i = 0, e = TypeConstraints.size(); i != e; ++i)
  217. MadeChange |= TypeConstraints[i].ApplyTypeConstraint(N, *this, TP);
  218. return MadeChange;
  219. }
  220. };
  221. /// TreePredicateFn - This is an abstraction that represents the predicates on
  222. /// a PatFrag node. This is a simple one-word wrapper around a pointer to
  223. /// provide nice accessors.
  224. class TreePredicateFn {
  225. /// PatFragRec - This is the TreePattern for the PatFrag that we
  226. /// originally came from.
  227. TreePattern *PatFragRec;
  228. public:
  229. /// TreePredicateFn constructor. Here 'N' is a subclass of PatFrag.
  230. TreePredicateFn(TreePattern *N);
  231. TreePattern *getOrigPatFragRecord() const { return PatFragRec; }
  232. /// isAlwaysTrue - Return true if this is a noop predicate.
  233. bool isAlwaysTrue() const;
  234. bool isImmediatePattern() const { return !getImmCode().empty(); }
  235. /// getImmediatePredicateCode - Return the code that evaluates this pattern if
  236. /// this is an immediate predicate. It is an error to call this on a
  237. /// non-immediate pattern.
  238. std::string getImmediatePredicateCode() const {
  239. std::string Result = getImmCode();
  240. assert(!Result.empty() && "Isn't an immediate pattern!");
  241. return Result;
  242. }
  243. bool operator==(const TreePredicateFn &RHS) const {
  244. return PatFragRec == RHS.PatFragRec;
  245. }
  246. bool operator!=(const TreePredicateFn &RHS) const { return !(*this == RHS); }
  247. /// Return the name to use in the generated code to reference this, this is
  248. /// "Predicate_foo" if from a pattern fragment "foo".
  249. std::string getFnName() const;
  250. /// getCodeToRunOnSDNode - Return the code for the function body that
  251. /// evaluates this predicate. The argument is expected to be in "Node",
  252. /// not N. This handles casting and conversion to a concrete node type as
  253. /// appropriate.
  254. std::string getCodeToRunOnSDNode() const;
  255. private:
  256. std::string getPredCode() const;
  257. std::string getImmCode() const;
  258. };
  259. /// FIXME: TreePatternNode's can be shared in some cases (due to dag-shaped
  260. /// patterns), and as such should be ref counted. We currently just leak all
  261. /// TreePatternNode objects!
  262. class TreePatternNode {
  263. /// The type of each node result. Before and during type inference, each
  264. /// result may be a set of possible types. After (successful) type inference,
  265. /// each is a single concrete type.
  266. SmallVector<EEVT::TypeSet, 1> Types;
  267. /// Operator - The Record for the operator if this is an interior node (not
  268. /// a leaf).
  269. Record *Operator;
  270. /// Val - The init value (e.g. the "GPRC" record, or "7") for a leaf.
  271. ///
  272. Init *Val;
  273. /// Name - The name given to this node with the :$foo notation.
  274. ///
  275. std::string Name;
  276. /// PredicateFns - The predicate functions to execute on this node to check
  277. /// for a match. If this list is empty, no predicate is involved.
  278. std::vector<TreePredicateFn> PredicateFns;
  279. /// TransformFn - The transformation function to execute on this node before
  280. /// it can be substituted into the resulting instruction on a pattern match.
  281. Record *TransformFn;
  282. std::vector<TreePatternNode*> Children;
  283. public:
  284. TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch,
  285. unsigned NumResults)
  286. : Operator(Op), Val(nullptr), TransformFn(nullptr), Children(Ch) {
  287. Types.resize(NumResults);
  288. }
  289. TreePatternNode(Init *val, unsigned NumResults) // leaf ctor
  290. : Operator(nullptr), Val(val), TransformFn(nullptr) {
  291. Types.resize(NumResults);
  292. }
  293. ~TreePatternNode();
  294. bool hasName() const { return !Name.empty(); }
  295. const std::string &getName() const { return Name; }
  296. void setName(StringRef N) { Name.assign(N.begin(), N.end()); }
  297. bool isLeaf() const { return Val != nullptr; }
  298. // Type accessors.
  299. unsigned getNumTypes() const { return Types.size(); }
  300. MVT::SimpleValueType getType(unsigned ResNo) const {
  301. return Types[ResNo].getConcrete();
  302. }
  303. const SmallVectorImpl<EEVT::TypeSet> &getExtTypes() const { return Types; }
  304. const EEVT::TypeSet &getExtType(unsigned ResNo) const { return Types[ResNo]; }
  305. EEVT::TypeSet &getExtType(unsigned ResNo) { return Types[ResNo]; }
  306. void setType(unsigned ResNo, const EEVT::TypeSet &T) { Types[ResNo] = T; }
  307. bool hasTypeSet(unsigned ResNo) const {
  308. return Types[ResNo].isConcrete();
  309. }
  310. bool isTypeCompletelyUnknown(unsigned ResNo) const {
  311. return Types[ResNo].isCompletelyUnknown();
  312. }
  313. bool isTypeDynamicallyResolved(unsigned ResNo) const {
  314. return Types[ResNo].isDynamicallyResolved();
  315. }
  316. Init *getLeafValue() const { assert(isLeaf()); return Val; }
  317. Record *getOperator() const { assert(!isLeaf()); return Operator; }
  318. unsigned getNumChildren() const { return Children.size(); }
  319. TreePatternNode *getChild(unsigned N) const { return Children[N]; }
  320. void setChild(unsigned i, TreePatternNode *N) {
  321. Children[i] = N;
  322. }
  323. /// hasChild - Return true if N is any of our children.
  324. bool hasChild(const TreePatternNode *N) const {
  325. for (unsigned i = 0, e = Children.size(); i != e; ++i)
  326. if (Children[i] == N) return true;
  327. return false;
  328. }
  329. bool hasAnyPredicate() const { return !PredicateFns.empty(); }
  330. const std::vector<TreePredicateFn> &getPredicateFns() const {
  331. return PredicateFns;
  332. }
  333. void clearPredicateFns() { PredicateFns.clear(); }
  334. void setPredicateFns(const std::vector<TreePredicateFn> &Fns) {
  335. assert(PredicateFns.empty() && "Overwriting non-empty predicate list!");
  336. PredicateFns = Fns;
  337. }
  338. void addPredicateFn(const TreePredicateFn &Fn) {
  339. assert(!Fn.isAlwaysTrue() && "Empty predicate string!");
  340. if (std::find(PredicateFns.begin(), PredicateFns.end(), Fn) ==
  341. PredicateFns.end())
  342. PredicateFns.push_back(Fn);
  343. }
  344. Record *getTransformFn() const { return TransformFn; }
  345. void setTransformFn(Record *Fn) { TransformFn = Fn; }
  346. /// getIntrinsicInfo - If this node corresponds to an intrinsic, return the
  347. /// CodeGenIntrinsic information for it, otherwise return a null pointer.
  348. const CodeGenIntrinsic *getIntrinsicInfo(const CodeGenDAGPatterns &CDP) const;
  349. /// getComplexPatternInfo - If this node corresponds to a ComplexPattern,
  350. /// return the ComplexPattern information, otherwise return null.
  351. const ComplexPattern *
  352. getComplexPatternInfo(const CodeGenDAGPatterns &CGP) const;
  353. /// Returns the number of MachineInstr operands that would be produced by this
  354. /// node if it mapped directly to an output Instruction's
  355. /// operand. ComplexPattern specifies this explicitly; MIOperandInfo gives it
  356. /// for Operands; otherwise 1.
  357. unsigned getNumMIResults(const CodeGenDAGPatterns &CGP) const;
  358. /// NodeHasProperty - Return true if this node has the specified property.
  359. bool NodeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
  360. /// TreeHasProperty - Return true if any node in this tree has the specified
  361. /// property.
  362. bool TreeHasProperty(SDNP Property, const CodeGenDAGPatterns &CGP) const;
  363. /// isCommutativeIntrinsic - Return true if the node is an intrinsic which is
  364. /// marked isCommutative.
  365. bool isCommutativeIntrinsic(const CodeGenDAGPatterns &CDP) const;
  366. void print(raw_ostream &OS) const;
  367. void dump() const;
  368. public: // Higher level manipulation routines.
  369. /// clone - Return a new copy of this tree.
  370. ///
  371. TreePatternNode *clone() const;
  372. /// RemoveAllTypes - Recursively strip all the types of this tree.
  373. void RemoveAllTypes();
  374. /// isIsomorphicTo - Return true if this node is recursively isomorphic to
  375. /// the specified node. For this comparison, all of the state of the node
  376. /// is considered, except for the assigned name. Nodes with differing names
  377. /// that are otherwise identical are considered isomorphic.
  378. bool isIsomorphicTo(const TreePatternNode *N,
  379. const MultipleUseVarSet &DepVars) const;
  380. /// SubstituteFormalArguments - Replace the formal arguments in this tree
  381. /// with actual values specified by ArgMap.
  382. void SubstituteFormalArguments(std::map<std::string,
  383. TreePatternNode*> &ArgMap);
  384. /// InlinePatternFragments - If this pattern refers to any pattern
  385. /// fragments, inline them into place, giving us a pattern without any
  386. /// PatFrag references.
  387. TreePatternNode *InlinePatternFragments(TreePattern &TP);
  388. /// ApplyTypeConstraints - Apply all of the type constraints relevant to
  389. /// this node and its children in the tree. This returns true if it makes a
  390. /// change, false otherwise. If a type contradiction is found, flag an error.
  391. bool ApplyTypeConstraints(TreePattern &TP, bool NotRegisters);
  392. /// UpdateNodeType - Set the node type of N to VT if VT contains
  393. /// information. If N already contains a conflicting type, then flag an
  394. /// error. This returns true if any information was updated.
  395. ///
  396. bool UpdateNodeType(unsigned ResNo, const EEVT::TypeSet &InTy,
  397. TreePattern &TP) {
  398. return Types[ResNo].MergeInTypeInfo(InTy, TP);
  399. }
  400. bool UpdateNodeType(unsigned ResNo, MVT::SimpleValueType InTy,
  401. TreePattern &TP) {
  402. return Types[ResNo].MergeInTypeInfo(EEVT::TypeSet(InTy, TP), TP);
  403. }
  404. // Update node type with types inferred from an instruction operand or result
  405. // def from the ins/outs lists.
  406. // Return true if the type changed.
  407. bool UpdateNodeTypeFromInst(unsigned ResNo, Record *Operand, TreePattern &TP);
  408. /// ContainsUnresolvedType - Return true if this tree contains any
  409. /// unresolved types.
  410. bool ContainsUnresolvedType() const {
  411. for (unsigned i = 0, e = Types.size(); i != e; ++i)
  412. if (!Types[i].isConcrete()) return true;
  413. for (unsigned i = 0, e = getNumChildren(); i != e; ++i)
  414. if (getChild(i)->ContainsUnresolvedType()) return true;
  415. return false;
  416. }
  417. /// canPatternMatch - If it is impossible for this pattern to match on this
  418. /// target, fill in Reason and return false. Otherwise, return true.
  419. bool canPatternMatch(std::string &Reason, const CodeGenDAGPatterns &CDP);
  420. };
  421. inline raw_ostream &operator<<(raw_ostream &OS, const TreePatternNode &TPN) {
  422. TPN.print(OS);
  423. return OS;
  424. }
  425. /// TreePattern - Represent a pattern, used for instructions, pattern
  426. /// fragments, etc.
  427. ///
  428. class TreePattern {
  429. /// Trees - The list of pattern trees which corresponds to this pattern.
  430. /// Note that PatFrag's only have a single tree.
  431. ///
  432. std::vector<TreePatternNode*> Trees;
  433. /// NamedNodes - This is all of the nodes that have names in the trees in this
  434. /// pattern.
  435. StringMap<SmallVector<TreePatternNode*,1> > NamedNodes;
  436. /// TheRecord - The actual TableGen record corresponding to this pattern.
  437. ///
  438. Record *TheRecord;
  439. /// Args - This is a list of all of the arguments to this pattern (for
  440. /// PatFrag patterns), which are the 'node' markers in this pattern.
  441. std::vector<std::string> Args;
  442. /// CDP - the top-level object coordinating this madness.
  443. ///
  444. CodeGenDAGPatterns &CDP;
  445. /// isInputPattern - True if this is an input pattern, something to match.
  446. /// False if this is an output pattern, something to emit.
  447. bool isInputPattern;
  448. /// hasError - True if the currently processed nodes have unresolvable types
  449. /// or other non-fatal errors
  450. bool HasError;
  451. /// It's important that the usage of operands in ComplexPatterns is
  452. /// consistent: each named operand can be defined by at most one
  453. /// ComplexPattern. This records the ComplexPattern instance and the operand
  454. /// number for each operand encountered in a ComplexPattern to aid in that
  455. /// check.
  456. StringMap<std::pair<Record *, unsigned>> ComplexPatternOperands;
  457. public:
  458. /// TreePattern constructor - Parse the specified DagInits into the
  459. /// current record.
  460. TreePattern(Record *TheRec, ListInit *RawPat, bool isInput,
  461. CodeGenDAGPatterns &ise);
  462. TreePattern(Record *TheRec, DagInit *Pat, bool isInput,
  463. CodeGenDAGPatterns &ise);
  464. TreePattern(Record *TheRec, TreePatternNode *Pat, bool isInput,
  465. CodeGenDAGPatterns &ise);
  466. /// getTrees - Return the tree patterns which corresponds to this pattern.
  467. ///
  468. const std::vector<TreePatternNode*> &getTrees() const { return Trees; }
  469. unsigned getNumTrees() const { return Trees.size(); }
  470. TreePatternNode *getTree(unsigned i) const { return Trees[i]; }
  471. TreePatternNode *getOnlyTree() const {
  472. assert(Trees.size() == 1 && "Doesn't have exactly one pattern!");
  473. return Trees[0];
  474. }
  475. const StringMap<SmallVector<TreePatternNode*,1> > &getNamedNodesMap() {
  476. if (NamedNodes.empty())
  477. ComputeNamedNodes();
  478. return NamedNodes;
  479. }
  480. /// getRecord - Return the actual TableGen record corresponding to this
  481. /// pattern.
  482. ///
  483. Record *getRecord() const { return TheRecord; }
  484. unsigned getNumArgs() const { return Args.size(); }
  485. const std::string &getArgName(unsigned i) const {
  486. assert(i < Args.size() && "Argument reference out of range!");
  487. return Args[i];
  488. }
  489. std::vector<std::string> &getArgList() { return Args; }
  490. CodeGenDAGPatterns &getDAGPatterns() const { return CDP; }
  491. /// InlinePatternFragments - If this pattern refers to any pattern
  492. /// fragments, inline them into place, giving us a pattern without any
  493. /// PatFrag references.
  494. void InlinePatternFragments() {
  495. for (unsigned i = 0, e = Trees.size(); i != e; ++i)
  496. Trees[i] = Trees[i]->InlinePatternFragments(*this);
  497. }
  498. /// InferAllTypes - Infer/propagate as many types throughout the expression
  499. /// patterns as possible. Return true if all types are inferred, false
  500. /// otherwise. Bail out if a type contradiction is found.
  501. bool InferAllTypes(const StringMap<SmallVector<TreePatternNode*,1> >
  502. *NamedTypes=nullptr);
  503. /// error - If this is the first error in the current resolution step,
  504. /// print it and set the error flag. Otherwise, continue silently.
  505. void error(const Twine &Msg);
  506. bool hasError() const {
  507. return HasError;
  508. }
  509. void resetError() {
  510. HasError = false;
  511. }
  512. void print(raw_ostream &OS) const;
  513. void dump() const;
  514. private:
  515. TreePatternNode *ParseTreePattern(Init *DI, StringRef OpName);
  516. void ComputeNamedNodes();
  517. void ComputeNamedNodes(TreePatternNode *N);
  518. };
  519. /// DAGDefaultOperand - One of these is created for each OperandWithDefaultOps
  520. /// that has a set ExecuteAlways / DefaultOps field.
  521. struct DAGDefaultOperand {
  522. std::vector<TreePatternNode*> DefaultOps;
  523. };
  524. class DAGInstruction {
  525. TreePattern *Pattern;
  526. std::vector<Record*> Results;
  527. std::vector<Record*> Operands;
  528. std::vector<Record*> ImpResults;
  529. TreePatternNode *ResultPattern;
  530. public:
  531. DAGInstruction(TreePattern *TP,
  532. const std::vector<Record*> &results,
  533. const std::vector<Record*> &operands,
  534. const std::vector<Record*> &impresults)
  535. : Pattern(TP), Results(results), Operands(operands),
  536. ImpResults(impresults), ResultPattern(nullptr) {}
  537. TreePattern *getPattern() const { return Pattern; }
  538. unsigned getNumResults() const { return Results.size(); }
  539. unsigned getNumOperands() const { return Operands.size(); }
  540. unsigned getNumImpResults() const { return ImpResults.size(); }
  541. const std::vector<Record*>& getImpResults() const { return ImpResults; }
  542. void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
  543. Record *getResult(unsigned RN) const {
  544. assert(RN < Results.size());
  545. return Results[RN];
  546. }
  547. Record *getOperand(unsigned ON) const {
  548. assert(ON < Operands.size());
  549. return Operands[ON];
  550. }
  551. Record *getImpResult(unsigned RN) const {
  552. assert(RN < ImpResults.size());
  553. return ImpResults[RN];
  554. }
  555. TreePatternNode *getResultPattern() const { return ResultPattern; }
  556. };
  557. /// PatternToMatch - Used by CodeGenDAGPatterns to keep tab of patterns
  558. /// processed to produce isel.
  559. class PatternToMatch {
  560. public:
  561. PatternToMatch(Record *srcrecord, ListInit *preds,
  562. TreePatternNode *src, TreePatternNode *dst,
  563. const std::vector<Record*> &dstregs,
  564. int complexity, unsigned uid)
  565. : SrcRecord(srcrecord), Predicates(preds), SrcPattern(src), DstPattern(dst),
  566. Dstregs(dstregs), AddedComplexity(complexity), ID(uid) {}
  567. Record *SrcRecord; // Originating Record for the pattern.
  568. ListInit *Predicates; // Top level predicate conditions to match.
  569. TreePatternNode *SrcPattern; // Source pattern to match.
  570. TreePatternNode *DstPattern; // Resulting pattern.
  571. std::vector<Record*> Dstregs; // Physical register defs being matched.
  572. int AddedComplexity; // Add to matching pattern complexity.
  573. unsigned ID; // Unique ID for the record.
  574. Record *getSrcRecord() const { return SrcRecord; }
  575. ListInit *getPredicates() const { return Predicates; }
  576. TreePatternNode *getSrcPattern() const { return SrcPattern; }
  577. TreePatternNode *getDstPattern() const { return DstPattern; }
  578. const std::vector<Record*> &getDstRegs() const { return Dstregs; }
  579. int getAddedComplexity() const { return AddedComplexity; }
  580. std::string getPredicateCheck() const;
  581. /// Compute the complexity metric for the input pattern. This roughly
  582. /// corresponds to the number of nodes that are covered.
  583. int getPatternComplexity(const CodeGenDAGPatterns &CGP) const;
  584. };
  585. class CodeGenDAGPatterns {
  586. RecordKeeper &Records;
  587. CodeGenTarget Target;
  588. std::vector<CodeGenIntrinsic> Intrinsics;
  589. std::vector<CodeGenIntrinsic> TgtIntrinsics;
  590. std::map<Record*, SDNodeInfo, LessRecordByID> SDNodes;
  591. std::map<Record*, std::pair<Record*, std::string>, LessRecordByID> SDNodeXForms;
  592. std::map<Record*, ComplexPattern, LessRecordByID> ComplexPatterns;
  593. std::map<Record *, std::unique_ptr<TreePattern>, LessRecordByID>
  594. PatternFragments;
  595. std::map<Record*, DAGDefaultOperand, LessRecordByID> DefaultOperands;
  596. std::map<Record*, DAGInstruction, LessRecordByID> Instructions;
  597. // Specific SDNode definitions:
  598. Record *intrinsic_void_sdnode;
  599. Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
  600. /// PatternsToMatch - All of the things we are matching on the DAG. The first
  601. /// value is the pattern to match, the second pattern is the result to
  602. /// emit.
  603. std::vector<PatternToMatch> PatternsToMatch;
  604. public:
  605. CodeGenDAGPatterns(RecordKeeper &R);
  606. CodeGenTarget &getTargetInfo() { return Target; }
  607. const CodeGenTarget &getTargetInfo() const { return Target; }
  608. Record *getSDNodeNamed(const std::string &Name) const;
  609. const SDNodeInfo &getSDNodeInfo(Record *R) const {
  610. assert(SDNodes.count(R) && "Unknown node!");
  611. return SDNodes.find(R)->second;
  612. }
  613. // Node transformation lookups.
  614. typedef std::pair<Record*, std::string> NodeXForm;
  615. const NodeXForm &getSDNodeTransform(Record *R) const {
  616. assert(SDNodeXForms.count(R) && "Invalid transform!");
  617. return SDNodeXForms.find(R)->second;
  618. }
  619. typedef std::map<Record*, NodeXForm, LessRecordByID>::const_iterator
  620. nx_iterator;
  621. nx_iterator nx_begin() const { return SDNodeXForms.begin(); }
  622. nx_iterator nx_end() const { return SDNodeXForms.end(); }
  623. const ComplexPattern &getComplexPattern(Record *R) const {
  624. assert(ComplexPatterns.count(R) && "Unknown addressing mode!");
  625. return ComplexPatterns.find(R)->second;
  626. }
  627. const CodeGenIntrinsic &getIntrinsic(Record *R) const {
  628. for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
  629. if (Intrinsics[i].TheDef == R) return Intrinsics[i];
  630. for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
  631. if (TgtIntrinsics[i].TheDef == R) return TgtIntrinsics[i];
  632. llvm_unreachable("Unknown intrinsic!");
  633. }
  634. const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
  635. if (IID-1 < Intrinsics.size())
  636. return Intrinsics[IID-1];
  637. if (IID-Intrinsics.size()-1 < TgtIntrinsics.size())
  638. return TgtIntrinsics[IID-Intrinsics.size()-1];
  639. llvm_unreachable("Bad intrinsic ID!");
  640. }
  641. unsigned getIntrinsicID(Record *R) const {
  642. for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
  643. if (Intrinsics[i].TheDef == R) return i;
  644. for (unsigned i = 0, e = TgtIntrinsics.size(); i != e; ++i)
  645. if (TgtIntrinsics[i].TheDef == R) return i + Intrinsics.size();
  646. llvm_unreachable("Unknown intrinsic!");
  647. }
  648. const DAGDefaultOperand &getDefaultOperand(Record *R) const {
  649. assert(DefaultOperands.count(R) &&"Isn't an analyzed default operand!");
  650. return DefaultOperands.find(R)->second;
  651. }
  652. // Pattern Fragment information.
  653. TreePattern *getPatternFragment(Record *R) const {
  654. assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
  655. return PatternFragments.find(R)->second.get();
  656. }
  657. TreePattern *getPatternFragmentIfRead(Record *R) const {
  658. if (!PatternFragments.count(R))
  659. return nullptr;
  660. return PatternFragments.find(R)->second.get();
  661. }
  662. typedef std::map<Record *, std::unique_ptr<TreePattern>,
  663. LessRecordByID>::const_iterator pf_iterator;
  664. pf_iterator pf_begin() const { return PatternFragments.begin(); }
  665. pf_iterator pf_end() const { return PatternFragments.end(); }
  666. // Patterns to match information.
  667. typedef std::vector<PatternToMatch>::const_iterator ptm_iterator;
  668. ptm_iterator ptm_begin() const { return PatternsToMatch.begin(); }
  669. ptm_iterator ptm_end() const { return PatternsToMatch.end(); }
  670. /// Parse the Pattern for an instruction, and insert the result in DAGInsts.
  671. typedef std::map<Record*, DAGInstruction, LessRecordByID> DAGInstMap;
  672. const DAGInstruction &parseInstructionPattern(
  673. CodeGenInstruction &CGI, ListInit *Pattern,
  674. DAGInstMap &DAGInsts);
  675. const DAGInstruction &getInstruction(Record *R) const {
  676. assert(Instructions.count(R) && "Unknown instruction!");
  677. return Instructions.find(R)->second;
  678. }
  679. Record *get_intrinsic_void_sdnode() const {
  680. return intrinsic_void_sdnode;
  681. }
  682. Record *get_intrinsic_w_chain_sdnode() const {
  683. return intrinsic_w_chain_sdnode;
  684. }
  685. Record *get_intrinsic_wo_chain_sdnode() const {
  686. return intrinsic_wo_chain_sdnode;
  687. }
  688. bool hasTargetIntrinsics() { return !TgtIntrinsics.empty(); }
  689. private:
  690. void ParseNodeInfo();
  691. void ParseNodeTransforms();
  692. void ParseComplexPatterns();
  693. void ParsePatternFragments(bool OutFrags = false);
  694. void ParseDefaultOperands();
  695. void ParseInstructions();
  696. void ParsePatterns();
  697. void InferInstructionFlags();
  698. void GenerateVariants();
  699. void VerifyInstructionFlags();
  700. void AddPatternToMatch(TreePattern *Pattern, const PatternToMatch &PTM);
  701. void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
  702. std::map<std::string,
  703. TreePatternNode*> &InstInputs,
  704. std::map<std::string,
  705. TreePatternNode*> &InstResults,
  706. std::vector<Record*> &InstImpResults);
  707. };
  708. } // end namespace llvm
  709. #endif