1 // Copyright (c) 2020 Google LLC
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #ifndef SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_
16 #define SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/opt/ir_context.h"
24 // Comparator for blocks, comparing them based on how deep they are nested
25 // inside selection or loop constructs. Deeper blocks are considered less than
26 // ones that are not as deep. The blocks are required to be in the same
28 class ComparatorDeepBlocksFirst
{
30 explicit ComparatorDeepBlocksFirst(opt::IRContext
* ir_context
)
31 : ir_context_(ir_context
) {}
33 bool operator()(uint32_t bb1
, uint32_t bb2
) const {
34 return this->operator()(fuzzerutil::MaybeFindBlock(ir_context_
, bb1
),
35 fuzzerutil::MaybeFindBlock(ir_context_
, bb2
));
38 bool operator()(const opt::BasicBlock
* bb1
, opt::BasicBlock
* bb2
) const {
39 assert(bb1
&& bb2
&& "The blocks must exist.");
40 assert(bb1
->GetParent() == bb2
->GetParent() &&
41 "The blocks must be in the same functions.");
42 return ir_context_
->GetStructuredCFGAnalysis()->NestingDepth(bb1
->id()) >
43 ir_context_
->GetStructuredCFGAnalysis()->NestingDepth(bb2
->id());
47 opt::IRContext
* ir_context_
;
51 } // namespace spvtools
53 #endif // SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_