1 // Copyright (c) 2020 Google LLC 2 // 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 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 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. 14 15 #ifndef SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_ 16 #define SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_ 17 18 #include "source/fuzz/fuzzer_util.h" 19 #include "source/opt/ir_context.h" 20 21 namespace spvtools { 22 namespace fuzz { 23 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 27 // function. 28 class ComparatorDeepBlocksFirst { 29 public: ComparatorDeepBlocksFirst(opt::IRContext * ir_context)30 explicit ComparatorDeepBlocksFirst(opt::IRContext* ir_context) 31 : ir_context_(ir_context) {} 32 operator()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)); 36 } 37 operator()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()); 44 } 45 46 private: 47 opt::IRContext* ir_context_; 48 }; 49 50 } // namespace fuzz 51 } // namespace spvtools 52 53 #endif // SOURCE_FUZZ_COMPARATOR_BLOCKS_DEEP_FIRST_H_ 54