aboutsummaryrefslogtreecommitdiff
path: root/utils/TableGen/DAGISelMatcherEmitter.cpp
blob: 1a41713c220e34fd19cd6d95d02524d6d315d2e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===- DAGISelMatcherEmitter.cpp - Matcher Emitter ------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains code to generate C++ code a matcher.
//
//===----------------------------------------------------------------------===//

#include "DAGISelMatcher.h"
#include "CodeGenDAGPatterns.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/FormattedStream.h"
using namespace llvm;

namespace {
enum {
  CommentIndent = 25
};
}

static unsigned EmitMatcherAndChildren(const MatcherNode *N,
                                       formatted_raw_ostream &FOS,
                                       unsigned Indent);

/// ClassifyInt - Classify an integer by size, return '1','2','4','8' if this
/// fits in 1, 2, 4, or 8 sign extended bytes.
static char ClassifyInt(int64_t Val) {
  if (Val == int8_t(Val))  return '1';
  if (Val == int16_t(Val)) return '2';
  if (Val == int32_t(Val)) return '4';
  return '8';
}

/// EmitInt - Emit the specified integer, returning the number of bytes emitted.
static unsigned EmitInt(int64_t Val, formatted_raw_ostream &OS) {
  unsigned BytesEmitted = 1;
  OS << (int)(unsigned char)Val << ", ";
  if (Val == int8_t(Val)) {
    OS << "\n";
    return BytesEmitted;
  }
  
  OS << (int)(unsigned char)(Val >> 8) << ", ";
  ++BytesEmitted;
  
  if (Val != int16_t(Val)) {
    OS << (int)(unsigned char)(Val >> 16) << ','
       << (int)(unsigned char)(Val >> 24) << ',';
    BytesEmitted += 2;
    
    if (Val != int32_t(Val)) {
      OS << (int)(unsigned char)(Val >> 32) << ','
         << (int)(unsigned char)(Val >> 40) << ','
         << (int)(unsigned char)(Val >> 48) << ','
         << (int)(unsigned char)(Val >> 56) << ',';
      BytesEmitted += 4;
    }   
  }
  
  OS.PadToColumn(CommentIndent) << "// " << Val << '\n';
  return BytesEmitted;
}

/// EmitMatcherOpcodes - Emit bytes for the specified matcher and return
/// the number of bytes emitted.
static unsigned EmitMatcher(const MatcherNode *N, formatted_raw_ostream &OS,
                            unsigned Indent) {
  OS.PadToColumn(Indent*2);
  
  switch (N->getKind()) {
  case MatcherNode::Push: assert(0 && "Should be handled by caller");
  case MatcherNode::EmitNode:
    OS << "OPC_Emit, /*XXX*/";
    OS.PadToColumn(CommentIndent) << "// Src: "
      << *cast<EmitNodeMatcherNode>(N)->getPattern().getSrcPattern() << '\n';
    OS.PadToColumn(CommentIndent) << "// Dst: "
      << *cast<EmitNodeMatcherNode>(N)->getPattern().getDstPattern() << '\n';
    return 1;
  case MatcherNode::Record:
    OS << "OPC_Record,\n";
    return 1;
  case MatcherNode::MoveChild:
    OS << "OPC_MoveChild, "
       << cast<MoveChildMatcherNode>(N)->getChildNo() << ",\n";
    return 2;
      
  case MatcherNode::MoveParent:
    OS << "OPC_MoveParent,\n";
    return 1;
      
  case MatcherNode::CheckSame:
    OS << "OPC_CheckSame, "
       << cast<CheckSameMatcherNode>(N)->getMatchNumber() << ",\n";
    return 2;

  case MatcherNode::CheckPatternPredicate:
    OS << "OPC_CheckPatternPredicate, /*XXX*/0,";
    OS.PadToColumn(CommentIndent) << "// "
      << cast<CheckPatternPredicateMatcherNode>(N)->getPredicate() << '\n';
    return 2;
    
  case MatcherNode::CheckPredicate:
    OS << "OPC_CheckPredicate, /*XXX*/0,";
    OS.PadToColumn(CommentIndent) << "// "
      << cast<CheckPredicateMatcherNode>(N)->getPredicateName() << '\n';
    return 2;
      
  case MatcherNode::CheckOpcode:
    OS << "OPC_CheckOpcode, "
       << cast<CheckOpcodeMatcherNode>(N)->getOpcodeName() << ",\n";
    return 2;
      
  case MatcherNode::CheckType:
    OS << "OPC_CheckType, "
       << getEnumName(cast<CheckTypeMatcherNode>(N)->getType()) << ",\n";
    return 2;

  case MatcherNode::CheckInteger: {
    int64_t Val = cast<CheckIntegerMatcherNode>(N)->getValue();
    OS << "OPC_CheckInteger" << ClassifyInt(Val) << ", ";
    return EmitInt(Val, OS)+1;
  }   
  case MatcherNode::CheckCondCode:
    OS << "OPC_CheckCondCode, ISD::"
       << cast<CheckCondCodeMatcherNode>(N)->getCondCodeName() << ",\n";
    return 2;
      
  case MatcherNode::CheckValueType:
    OS << "OPC_CheckValueType, MVT::"
       << cast<CheckValueTypeMatcherNode>(N)->getTypeName() << ",\n";
    return 2;

  case MatcherNode::CheckComplexPat:
    OS << "OPC_CheckComplexPat, 0/*XXX*/,\n";
    return 2;
      
  case MatcherNode::CheckAndImm: {
    int64_t Val = cast<CheckAndImmMatcherNode>(N)->getValue();
    OS << "OPC_CheckAndImm" << ClassifyInt(Val) << ", ";
    return EmitInt(Val, OS)+1;
  }

  case MatcherNode::CheckOrImm: {
    int64_t Val = cast<CheckOrImmMatcherNode>(N)->getValue();
    OS << "OPC_CheckOrImm" << ClassifyInt(Val) << ", ";
    return EmitInt(Val, OS)+1;
  }
  }
  assert(0 && "Unreachable");
  return 0;
}

/// EmitMatcherAndChildren - Emit the bytes for the specified matcher subtree.
static unsigned EmitMatcherAndChildren(const MatcherNode *N,
                                       formatted_raw_ostream &OS,
                                       unsigned Indent) {
  unsigned Size = 0;
  while (1) {
    // Push is a special case since it is binary.
    if (const PushMatcherNode *PMN = dyn_cast<PushMatcherNode>(N)) {
      // We need to encode the child and the offset of the failure code before
      // emitting either of them.  Handle this by buffering the output into a
      // string while we get the size.
      SmallString<128> TmpBuf;
      unsigned ChildSize;
      {
        raw_svector_ostream OS(TmpBuf);
        formatted_raw_ostream FOS(OS);
        ChildSize = 
          EmitMatcherAndChildren(cast<PushMatcherNode>(N)->getChild(), FOS,
                                 Indent+1);
      }
      
      if (ChildSize > 255) {
        errs() <<
          "Tblgen internal error: can't handle predicate this complex yet\n";
        exit(1);
      }
      
      OS.PadToColumn(Indent*2);
      OS << "OPC_Push, " << ChildSize << ",\n";
      OS << TmpBuf.str();
      
      Size += 2 + ChildSize;
      
      N = PMN->getFailure();
      continue;
    }
  
    Size += EmitMatcher(N, OS, Indent);
    
    // If there are children of this node, iterate to them, otherwise we're
    // done.
    if (const MatcherNodeWithChild *MNWC = dyn_cast<MatcherNodeWithChild>(N))
      N = MNWC->getChild();
    else
      return Size;
  }
}

void llvm::EmitMatcherTable(const MatcherNode *Matcher, raw_ostream &O) {
  formatted_raw_ostream OS(O);
  
  OS << "// The main instruction selector code.\n";
  OS << "SDNode *SelectCode2(SDNode *N) {\n";

  OS << "  static const unsigned char MatcherTable[] = {\n";
  unsigned TotalSize = EmitMatcherAndChildren(Matcher, OS, 2);
  OS << "    0\n  }; // Total Array size is " << (TotalSize+1) << " bytes\n\n";
 OS << "  return SelectCodeCommon(N, MatcherTable, sizeof(MatcherTable));\n}\n";
}