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