| 1 | //===----------------------------------------------------------------------===// |
|---|---|
| 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 | // Out-of-line implementations for PatternMatch.h. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/IR/PatternMatch.h" |
| 14 | #include "llvm/ADT/SmallPtrSet.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
| 16 | #include "llvm/IR/Constant.h" |
| 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Value.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | bool llvm::PatternMatch::undef_match::checkAggregate( |
| 23 | const ConstantAggregate *CA) { |
| 24 | SmallPtrSet<const ConstantAggregate *, 8> Seen; |
| 25 | SmallVector<const ConstantAggregate *, 8> Worklist; |
| 26 | |
| 27 | // Either UndefValue, PoisonValue, or an aggregate that only contains |
| 28 | // these is accepted by matcher. |
| 29 | // CheckValue returns false if CA cannot satisfy this constraint. |
| 30 | auto CheckValue = [&](const ConstantAggregate *CA) { |
| 31 | for (const Value *Op : CA->operand_values()) { |
| 32 | if (isa<UndefValue>(Val: Op)) |
| 33 | continue; |
| 34 | |
| 35 | const auto *CA = dyn_cast<ConstantAggregate>(Val: Op); |
| 36 | if (!CA) |
| 37 | return false; |
| 38 | if (Seen.insert(Ptr: CA).second) |
| 39 | Worklist.emplace_back(Args&: CA); |
| 40 | } |
| 41 | |
| 42 | return true; |
| 43 | }; |
| 44 | |
| 45 | if (!CheckValue(CA)) |
| 46 | return false; |
| 47 | |
| 48 | while (!Worklist.empty()) { |
| 49 | if (!CheckValue(Worklist.pop_back_val())) |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 |