Implement Regression/TableGen/DagDefSubst.ll
[oota-llvm.git] / utils / TableGen / DAGISelEmitter.h
index 84985b45ae5ffb7c100463ed3541669acce82d91..8ed66be800a214826ccfb320c3ab92b54c5c6ae5 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "TableGenBackend.h"
 #include "CodeGenTarget.h"
+#include "CodeGenIntrinsics.h"
+#include <set>
 
 namespace llvm {
   class Record;
@@ -45,8 +47,8 @@ namespace llvm {
     
     unsigned OperandNo;   // The operand # this constraint applies to.
     enum { 
-      SDTCisVT, SDTCisInt, SDTCisFP, SDTCisSameAs, SDTCisVTSmallerThanOp,
-      SDTCisOpSmallerThanOp
+      SDTCisVT, SDTCisPtrTy, SDTCisInt, SDTCisFP, SDTCisSameAs, 
+      SDTCisVTSmallerThanOp, SDTCisOpSmallerThanOp, SDTCisIntVectorOfSameSize
     } ConstraintType;
     
     union {   // The discriminated union.
@@ -62,6 +64,9 @@ namespace llvm {
       struct {
         unsigned BigOperandNum;
       } SDTCisOpSmallerThanOp_Info;
+      struct {
+        unsigned OtherOperandNum;
+      } SDTCisIntVectorOfSameSize_Info;
     } x;
 
     /// ApplyTypeConstraint - Given a node in a pattern, apply this type
@@ -102,7 +107,8 @@ namespace llvm {
     }
     
     // SelectionDAG node properties.
-    enum SDNP { SDNPCommutative, SDNPAssociative, SDNPHasChain };
+    enum SDNP { SDNPCommutative, SDNPAssociative, SDNPHasChain,
+                SDNPOutFlag, SDNPInFlag, SDNPOptInFlag  };
 
     /// hasProperty - Return true if this node has the specified property.
     ///
@@ -124,9 +130,9 @@ namespace llvm {
   /// patterns), and as such should be ref counted.  We currently just leak all
   /// TreePatternNode objects!
   class TreePatternNode {
-    /// The inferred type for this node, or MVT::LAST_VALUETYPE if it hasn't
+    /// The inferred type for this node, or MVT::isUnknown if it hasn't
     /// been determined yet.
-    unsigned char Ty;
+    std::vector<unsigned char> Types;
     
     /// Operator - The Record for the operator if this is an interior node (not
     /// a leaf).
@@ -151,26 +157,34 @@ namespace llvm {
     std::vector<TreePatternNode*> Children;
   public:
     TreePatternNode(Record *Op, const std::vector<TreePatternNode*> &Ch) 
-      : Ty(MVT::isUnknown), Operator(Op), Val(0), TransformFn(0),
-        Children(Ch) {}
+      : Types(), Operator(Op), Val(0), TransformFn(0),
+      Children(Ch) { Types.push_back(MVT::isUnknown); }
     TreePatternNode(Init *val)    // leaf ctor
-      : Ty(MVT::isUnknown), Operator(0), Val(val), TransformFn(0) {}
+      : Types(), Operator(0), Val(val), TransformFn(0) {
+      Types.push_back(MVT::isUnknown);
+    }
     ~TreePatternNode();
     
     const std::string &getName() const { return Name; }
     void setName(const std::string &N) { Name = N; }
     
     bool isLeaf() const { return Val != 0; }
-    bool hasTypeSet() const { return Ty < MVT::LAST_VALUETYPE; }
+    bool hasTypeSet() const { return Types[0] < MVT::LAST_VALUETYPE; }
     bool isTypeCompletelyUnknown() const {
-      return Ty == MVT::isUnknown;
+      return Types[0] == MVT::isUnknown;
     }
-    MVT::ValueType getType() const {
+    MVT::ValueType getTypeNum(unsigned Num) const {
       assert(hasTypeSet() && "Doesn't have a type yet!");
-      return (MVT::ValueType)Ty;
+      assert(Types.size() > Num && "Type num out of range!");
+      return (MVT::ValueType)Types[Num];
+    }
+    unsigned char getExtTypeNum(unsigned Num) const { 
+      assert(Types.size() > Num && "Extended type num out of range!");
+      return Types[Num]; 
     }
-    unsigned char getExtType() const { return Ty; }
-    void setType(unsigned char VT) { Ty = VT; }
+    const std::vector<unsigned char> &getExtTypes() const { return Types; }
+    void setTypes(const std::vector<unsigned char> &T) { Types = T; }
+    void removeTypes() { Types = std::vector<unsigned char>(1,MVT::isUnknown); }
     
     Init *getLeafValue() const { assert(isLeaf()); return Val; }
     Record *getOperator() const { assert(!isLeaf()); return Operator; }
@@ -181,6 +195,7 @@ namespace llvm {
       Children[i] = N;
     }
     
+    
     const std::string &getPredicateFn() const { return PredicateFn; }
     void setPredicateFn(const std::string &Fn) { PredicateFn = Fn; }
 
@@ -222,7 +237,12 @@ namespace llvm {
     /// information.  If N already contains a conflicting type, then throw an
     /// exception.  This returns true if any information was updated.
     ///
-    bool UpdateNodeType(unsigned char EVT, TreePattern &TP);
+    bool UpdateNodeType(const std::vector<unsigned char> &ExtVTs,
+                        TreePattern &TP);
+    bool UpdateNodeType(unsigned char ExtVT, TreePattern &TP) {
+      std::vector<unsigned char> ExtVTs(1, ExtVT);
+      return UpdateNodeType(ExtVTs, TP);
+    }
     
     /// ContainsUnresolvedType - Return true if this tree contains any
     /// unresolved types.
@@ -327,17 +347,24 @@ namespace llvm {
     TreePattern *Pattern;
     std::vector<Record*> Results;
     std::vector<Record*> Operands;
+    std::vector<Record*> ImpResults;
+    std::vector<Record*> ImpOperands;
     TreePatternNode *ResultPattern;
   public:
     DAGInstruction(TreePattern *TP,
                    const std::vector<Record*> &results,
-                   const std::vector<Record*> &operands)
+                   const std::vector<Record*> &operands,
+                   const std::vector<Record*> &impresults,
+                   const std::vector<Record*> &impoperands)
       : Pattern(TP), Results(results), Operands(operands), 
+        ImpResults(impresults), ImpOperands(impoperands),
         ResultPattern(0) {}
 
     TreePattern *getPattern() const { return Pattern; }
     unsigned getNumResults() const { return Results.size(); }
     unsigned getNumOperands() const { return Operands.size(); }
+    unsigned getNumImpResults() const { return ImpResults.size(); }
+    unsigned getNumImpOperands() const { return ImpOperands.size(); }
     
     void setResultPattern(TreePatternNode *R) { ResultPattern = R; }
     
@@ -350,26 +377,54 @@ namespace llvm {
       assert(ON < Operands.size());
       return Operands[ON];
     }
+
+    Record *getImpResult(unsigned RN) const {
+      assert(RN < ImpResults.size());
+      return ImpResults[RN];
+    }
+    
+    Record *getImpOperand(unsigned ON) const {
+      assert(ON < ImpOperands.size());
+      return ImpOperands[ON];
+    }
+
     TreePatternNode *getResultPattern() const { return ResultPattern; }
   };
   
-  
-/// InstrSelectorEmitter - The top-level class which coordinates construction
+/// PatternToMatch - Used by DAGISelEmitter to keep tab of patterns processed
+/// to produce isel.
+struct PatternToMatch {
+  PatternToMatch(ListInit *preds, TreePatternNode *src, TreePatternNode *dst):
+    Predicates(preds), SrcPattern(src), DstPattern(dst) {};
+
+  ListInit        *Predicates;  // Top level predicate conditions to match.
+  TreePatternNode *SrcPattern;  // Source pattern to match.
+  TreePatternNode *DstPattern;  // Resulting pattern.
+
+  ListInit        *getPredicates() const { return Predicates; }
+  TreePatternNode *getSrcPattern() const { return SrcPattern; }
+  TreePatternNode *getDstPattern() const { return DstPattern; }
+};
+
+/// DAGISelEmitter - The top-level class which coordinates construction
 /// and emission of the instruction selector.
 ///
 class DAGISelEmitter : public TableGenBackend {
-public:
-  typedef std::pair<TreePatternNode*, TreePatternNode*> PatternToMatch;
 private:
   RecordKeeper &Records;
   CodeGenTarget Target;
-
+  std::vector<CodeGenIntrinsic> Intrinsics;
+  
   std::map<Record*, SDNodeInfo> SDNodes;
   std::map<Record*, std::pair<Record*, std::string> > SDNodeXForms;
   std::map<Record*, ComplexPattern> ComplexPatterns;
   std::map<Record*, TreePattern*> PatternFragments;
   std::map<Record*, DAGInstruction> Instructions;
   
+  // Specific SDNode definitions:
+  Record *intrinsic_void_sdnode;
+  Record *intrinsic_w_chain_sdnode, *intrinsic_wo_chain_sdnode;
+  
   /// PatternsToMatch - All of the things we are matching on the DAG.  The first
   /// value is the pattern to match, the second pattern is the result to
   /// emit.
@@ -399,6 +454,25 @@ public:
     return ComplexPatterns.find(R)->second;
   }
   
+  const CodeGenIntrinsic &getIntrinsic(Record *R) const {
+    for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
+      if (Intrinsics[i].TheDef == R) return Intrinsics[i];
+    assert(0 && "Unknown intrinsic!");
+    abort();
+  }
+  
+  const CodeGenIntrinsic &getIntrinsicInfo(unsigned IID) const {
+    assert(IID-1 < Intrinsics.size() && "Bad intrinsic ID!");
+    return Intrinsics[IID-1];
+  }
+  
+  unsigned getIntrinsicID(Record *R) const {
+    for (unsigned i = 0, e = Intrinsics.size(); i != e; ++i)
+      if (Intrinsics[i].TheDef == R) return i;
+    assert(0 && "Unknown intrinsic!");
+    abort();
+  }
+  
   TreePattern *getPatternFragment(Record *R) const {
     assert(PatternFragments.count(R) && "Invalid pattern fragment request!");
     return PatternFragments.find(R)->second;
@@ -409,6 +483,17 @@ public:
     return Instructions.find(R)->second;
   }
   
+  Record *get_intrinsic_void_sdnode() const {
+    return intrinsic_void_sdnode;
+  }
+  Record *get_intrinsic_w_chain_sdnode() const {
+    return intrinsic_w_chain_sdnode;
+  }
+  Record *get_intrinsic_wo_chain_sdnode() const {
+    return intrinsic_wo_chain_sdnode;
+  }
+
+  
 private:
   void ParseNodeInfo();
   void ParseNodeTransforms(std::ostream &OS);
@@ -420,21 +505,17 @@ private:
   void FindPatternInputsAndOutputs(TreePattern *I, TreePatternNode *Pat,
                                    std::map<std::string,
                                             TreePatternNode*> &InstInputs,
-                                   std::map<std::string, Record*> &InstResults);
-  void EmitMatchForPattern(TreePatternNode *N, const std::string &RootName,
-                           std::map<std::string,std::string> &VarMap,
-                           unsigned PatternNo, std::ostream &OS,
-                           std::string &ChainName,
-                           bool &HasChain, bool &InFlag, bool isRoot = false);
-  void EmitCopyToRegsForPattern(TreePatternNode *N, const std::string &RootName,
-                                std::ostream &OS, bool HasChain);
-  std::pair<unsigned, unsigned>
-  CodeGenPatternResult(TreePatternNode *M, TreePatternNode *N, unsigned &Ctr,
-                       std::string &ChainName,
-                       std::map<std::string,std::string> &VariableMap, 
-                       unsigned PatternNo, std::ostream &OS,
-                       bool InFlag, bool isRoot = false);
-  void EmitCodeForPattern(PatternToMatch &Pattern, std::ostream &OS);
+                                   std::map<std::string,
+                                            TreePatternNode*> &InstResults,
+                                   std::vector<Record*> &InstImpInputs,
+                                   std::vector<Record*> &InstImpResults);
+  void GenerateCodeForPattern(PatternToMatch &Pattern,
+                      std::vector<std::pair<bool, std::string> > &GeneratedCode,
+                         std::set<std::pair<bool, std::string> > &GeneratedDecl,
+                              bool UseGoto);
+  void EmitPatterns(std::vector<std::pair<PatternToMatch*, 
+                    std::vector<std::pair<bool, std::string> > > > &Patterns, 
+                    unsigned Indent, std::ostream &OS);
   void EmitInstructionSelector(std::ostream &OS);
 };