1 //===- DAGISelMatcher.cpp - Representation of DAG pattern matcher ---------===// 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 #include "DAGISelMatcher.h" 11 #include "CodeGenDAGPatterns.h" 12 #include "CodeGenTarget.h" 13 #include "llvm/Support/raw_ostream.h" 14 #include "llvm/TableGen/Record.h" 15 using namespace llvm; 16 17 void Matcher::anchor() { } 18 19 void Matcher::dump() const { 20 print(errs(), 0); 21 } 22 23 void Matcher::print(raw_ostream &OS, unsigned indent) const { 24 printImpl(OS, indent); 25 if (Next) 26 return Next->print(OS, indent); 27 } 28 29 void Matcher::printOne(raw_ostream &OS) const { 30 printImpl(OS, 0); 31 } 32 33 /// unlinkNode - Unlink the specified node from this chain. If Other == this, 34 /// we unlink the next pointer and return it. Otherwise we unlink Other from 35 /// the list and return this. 36 Matcher *Matcher::unlinkNode(Matcher *Other) { 37 if (this == Other) 38 return takeNext(); 39 40 // Scan until we find the predecessor of Other. 41 Matcher *Cur = this; 42 for (; Cur && Cur->getNext() != Other; Cur = Cur->getNext()) 43 /*empty*/; 44 45 if (!Cur) return nullptr; 46 Cur->takeNext(); 47 Cur->setNext(Other->takeNext()); 48 return this; 49 } 50 51 /// canMoveBefore - Return true if this matcher is the same as Other, or if 52 /// we can move this matcher past all of the nodes in-between Other and this 53 /// node. Other must be equal to or before this. 54 bool Matcher::canMoveBefore(const Matcher *Other) const { 55 for (;; Other = Other->getNext()) { 56 assert(Other && "Other didn't come before 'this'?"); 57 if (this == Other) return true; 58 59 // We have to be able to move this node across the Other node. 60 if (!canMoveBeforeNode(Other)) 61 return false; 62 } 63 } 64 65 /// canMoveBeforeNode - Return true if it is safe to move the current matcher 66 /// across the specified one. 67 bool Matcher::canMoveBeforeNode(const Matcher *Other) const { 68 // We can move simple predicates before record nodes. 69 if (isSimplePredicateNode()) 70 return Other->isSimplePredicateOrRecordNode(); 71 72 // We can move record nodes across simple predicates. 73 if (isSimplePredicateOrRecordNode()) 74 return isSimplePredicateNode(); 75 76 // We can't move record nodes across each other etc. 77 return false; 78 } 79 80 81 ScopeMatcher::~ScopeMatcher() { 82 for (Matcher *C : Children) 83 delete C; 84 } 85 86 SwitchOpcodeMatcher::~SwitchOpcodeMatcher() { 87 for (auto &C : Cases) 88 delete C.second; 89 } 90 91 SwitchTypeMatcher::~SwitchTypeMatcher() { 92 for (auto &C : Cases) 93 delete C.second; 94 } 95 96 CheckPredicateMatcher::CheckPredicateMatcher( 97 const TreePredicateFn &pred, const SmallVectorImpl<unsigned> &Ops) 98 : Matcher(CheckPredicate), Pred(pred.getOrigPatFragRecord()), 99 Operands(Ops.begin(), Ops.end()) {} 100 101 TreePredicateFn CheckPredicateMatcher::getPredicate() const { 102 return TreePredicateFn(Pred); 103 } 104 105 unsigned CheckPredicateMatcher::getNumOperands() const { 106 return Operands.size(); 107 } 108 109 unsigned CheckPredicateMatcher::getOperandNo(unsigned i) const { 110 assert(i < Operands.size()); 111 return Operands[i]; 112 } 113 114 115 // printImpl methods. 116 117 void ScopeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 118 OS.indent(indent) << "Scope\n"; 119 for (const Matcher *C : Children) { 120 if (!C) 121 OS.indent(indent+1) << "NULL POINTER\n"; 122 else 123 C->print(OS, indent+2); 124 } 125 } 126 127 void RecordMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 128 OS.indent(indent) << "Record\n"; 129 } 130 131 void RecordChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 132 OS.indent(indent) << "RecordChild: " << ChildNo << '\n'; 133 } 134 135 void RecordMemRefMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 136 OS.indent(indent) << "RecordMemRef\n"; 137 } 138 139 void CaptureGlueInputMatcher::printImpl(raw_ostream &OS, unsigned indent) const{ 140 OS.indent(indent) << "CaptureGlueInput\n"; 141 } 142 143 void MoveChildMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 144 OS.indent(indent) << "MoveChild " << ChildNo << '\n'; 145 } 146 147 void MoveParentMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 148 OS.indent(indent) << "MoveParent\n"; 149 } 150 151 void CheckSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 152 OS.indent(indent) << "CheckSame " << MatchNumber << '\n'; 153 } 154 155 void CheckChildSameMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 156 OS.indent(indent) << "CheckChild" << ChildNo << "Same\n"; 157 } 158 159 void CheckPatternPredicateMatcher:: 160 printImpl(raw_ostream &OS, unsigned indent) const { 161 OS.indent(indent) << "CheckPatternPredicate " << Predicate << '\n'; 162 } 163 164 void CheckPredicateMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 165 OS.indent(indent) << "CheckPredicate " << getPredicate().getFnName() << '\n'; 166 } 167 168 void CheckOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 169 OS.indent(indent) << "CheckOpcode " << Opcode.getEnumName() << '\n'; 170 } 171 172 void SwitchOpcodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 173 OS.indent(indent) << "SwitchOpcode: {\n"; 174 for (const auto &C : Cases) { 175 OS.indent(indent) << "case " << C.first->getEnumName() << ":\n"; 176 C.second->print(OS, indent+2); 177 } 178 OS.indent(indent) << "}\n"; 179 } 180 181 182 void CheckTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 183 OS.indent(indent) << "CheckType " << getEnumName(Type) << ", ResNo=" 184 << ResNo << '\n'; 185 } 186 187 void SwitchTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 188 OS.indent(indent) << "SwitchType: {\n"; 189 for (const auto &C : Cases) { 190 OS.indent(indent) << "case " << getEnumName(C.first) << ":\n"; 191 C.second->print(OS, indent+2); 192 } 193 OS.indent(indent) << "}\n"; 194 } 195 196 void CheckChildTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 197 OS.indent(indent) << "CheckChildType " << ChildNo << " " 198 << getEnumName(Type) << '\n'; 199 } 200 201 202 void CheckIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 203 OS.indent(indent) << "CheckInteger " << Value << '\n'; 204 } 205 206 void CheckChildIntegerMatcher::printImpl(raw_ostream &OS, 207 unsigned indent) const { 208 OS.indent(indent) << "CheckChildInteger " << ChildNo << " " << Value << '\n'; 209 } 210 211 void CheckCondCodeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 212 OS.indent(indent) << "CheckCondCode ISD::" << CondCodeName << '\n'; 213 } 214 215 void CheckValueTypeMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 216 OS.indent(indent) << "CheckValueType MVT::" << TypeName << '\n'; 217 } 218 219 void CheckComplexPatMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 220 OS.indent(indent) << "CheckComplexPat " << Pattern.getSelectFunc() << '\n'; 221 } 222 223 void CheckAndImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 224 OS.indent(indent) << "CheckAndImm " << Value << '\n'; 225 } 226 227 void CheckOrImmMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 228 OS.indent(indent) << "CheckOrImm " << Value << '\n'; 229 } 230 231 void CheckFoldableChainNodeMatcher::printImpl(raw_ostream &OS, 232 unsigned indent) const { 233 OS.indent(indent) << "CheckFoldableChainNode\n"; 234 } 235 236 void EmitIntegerMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 237 OS.indent(indent) << "EmitInteger " << Val << " VT=" << getEnumName(VT) 238 << '\n'; 239 } 240 241 void EmitStringIntegerMatcher:: 242 printImpl(raw_ostream &OS, unsigned indent) const { 243 OS.indent(indent) << "EmitStringInteger " << Val << " VT=" << getEnumName(VT) 244 << '\n'; 245 } 246 247 void EmitRegisterMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 248 OS.indent(indent) << "EmitRegister "; 249 if (Reg) 250 OS << Reg->getName(); 251 else 252 OS << "zero_reg"; 253 OS << " VT=" << getEnumName(VT) << '\n'; 254 } 255 256 void EmitConvertToTargetMatcher:: 257 printImpl(raw_ostream &OS, unsigned indent) const { 258 OS.indent(indent) << "EmitConvertToTarget " << Slot << '\n'; 259 } 260 261 void EmitMergeInputChainsMatcher:: 262 printImpl(raw_ostream &OS, unsigned indent) const { 263 OS.indent(indent) << "EmitMergeInputChains <todo: args>\n"; 264 } 265 266 void EmitCopyToRegMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 267 OS.indent(indent) << "EmitCopyToReg <todo: args>\n"; 268 } 269 270 void EmitNodeXFormMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 271 OS.indent(indent) << "EmitNodeXForm " << NodeXForm->getName() 272 << " Slot=" << Slot << '\n'; 273 } 274 275 276 void EmitNodeMatcherCommon::printImpl(raw_ostream &OS, unsigned indent) const { 277 OS.indent(indent); 278 OS << (isa<MorphNodeToMatcher>(this) ? "MorphNodeTo: " : "EmitNode: ") 279 << OpcodeName << ": <todo flags> "; 280 281 for (unsigned i = 0, e = VTs.size(); i != e; ++i) 282 OS << ' ' << getEnumName(VTs[i]); 283 OS << '('; 284 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 285 OS << Operands[i] << ' '; 286 OS << ")\n"; 287 } 288 289 void CompleteMatchMatcher::printImpl(raw_ostream &OS, unsigned indent) const { 290 OS.indent(indent) << "CompleteMatch <todo args>\n"; 291 OS.indent(indent) << "Src = " << *Pattern.getSrcPattern() << "\n"; 292 OS.indent(indent) << "Dst = " << *Pattern.getDstPattern() << "\n"; 293 } 294 295 bool CheckOpcodeMatcher::isEqualImpl(const Matcher *M) const { 296 // Note: pointer equality isn't enough here, we have to check the enum names 297 // to ensure that the nodes are for the same opcode. 298 return cast<CheckOpcodeMatcher>(M)->Opcode.getEnumName() == 299 Opcode.getEnumName(); 300 } 301 302 bool EmitNodeMatcherCommon::isEqualImpl(const Matcher *m) const { 303 const EmitNodeMatcherCommon *M = cast<EmitNodeMatcherCommon>(m); 304 return M->OpcodeName == OpcodeName && M->VTs == VTs && 305 M->Operands == Operands && M->HasChain == HasChain && 306 M->HasInGlue == HasInGlue && M->HasOutGlue == HasOutGlue && 307 M->HasMemRefs == HasMemRefs && 308 M->NumFixedArityOperands == NumFixedArityOperands; 309 } 310 311 void EmitNodeMatcher::anchor() { } 312 313 void MorphNodeToMatcher::anchor() { } 314 315 // isContradictoryImpl Implementations. 316 317 static bool TypesAreContradictory(MVT::SimpleValueType T1, 318 MVT::SimpleValueType T2) { 319 // If the two types are the same, then they are the same, so they don't 320 // contradict. 321 if (T1 == T2) return false; 322 323 // If either type is about iPtr, then they don't conflict unless the other 324 // one is not a scalar integer type. 325 if (T1 == MVT::iPTR) 326 return !MVT(T2).isInteger() || MVT(T2).isVector(); 327 328 if (T2 == MVT::iPTR) 329 return !MVT(T1).isInteger() || MVT(T1).isVector(); 330 331 // Otherwise, they are two different non-iPTR types, they conflict. 332 return true; 333 } 334 335 bool CheckOpcodeMatcher::isContradictoryImpl(const Matcher *M) const { 336 if (const CheckOpcodeMatcher *COM = dyn_cast<CheckOpcodeMatcher>(M)) { 337 // One node can't have two different opcodes! 338 // Note: pointer equality isn't enough here, we have to check the enum names 339 // to ensure that the nodes are for the same opcode. 340 return COM->getOpcode().getEnumName() != getOpcode().getEnumName(); 341 } 342 343 // If the node has a known type, and if the type we're checking for is 344 // different, then we know they contradict. For example, a check for 345 // ISD::STORE will never be true at the same time a check for Type i32 is. 346 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) { 347 // If checking for a result the opcode doesn't have, it can't match. 348 if (CT->getResNo() >= getOpcode().getNumResults()) 349 return true; 350 351 MVT::SimpleValueType NodeType = getOpcode().getKnownType(CT->getResNo()); 352 if (NodeType != MVT::Other) 353 return TypesAreContradictory(NodeType, CT->getType()); 354 } 355 356 return false; 357 } 358 359 bool CheckTypeMatcher::isContradictoryImpl(const Matcher *M) const { 360 if (const CheckTypeMatcher *CT = dyn_cast<CheckTypeMatcher>(M)) 361 return TypesAreContradictory(getType(), CT->getType()); 362 return false; 363 } 364 365 bool CheckChildTypeMatcher::isContradictoryImpl(const Matcher *M) const { 366 if (const CheckChildTypeMatcher *CC = dyn_cast<CheckChildTypeMatcher>(M)) { 367 // If the two checks are about different nodes, we don't know if they 368 // conflict! 369 if (CC->getChildNo() != getChildNo()) 370 return false; 371 372 return TypesAreContradictory(getType(), CC->getType()); 373 } 374 return false; 375 } 376 377 bool CheckIntegerMatcher::isContradictoryImpl(const Matcher *M) const { 378 if (const CheckIntegerMatcher *CIM = dyn_cast<CheckIntegerMatcher>(M)) 379 return CIM->getValue() != getValue(); 380 return false; 381 } 382 383 bool CheckChildIntegerMatcher::isContradictoryImpl(const Matcher *M) const { 384 if (const CheckChildIntegerMatcher *CCIM = dyn_cast<CheckChildIntegerMatcher>(M)) { 385 // If the two checks are about different nodes, we don't know if they 386 // conflict! 387 if (CCIM->getChildNo() != getChildNo()) 388 return false; 389 390 return CCIM->getValue() != getValue(); 391 } 392 return false; 393 } 394 395 bool CheckValueTypeMatcher::isContradictoryImpl(const Matcher *M) const { 396 if (const CheckValueTypeMatcher *CVT = dyn_cast<CheckValueTypeMatcher>(M)) 397 return CVT->getTypeName() != getTypeName(); 398 return false; 399 } 400 401