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