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