2018-01-16 16:15:06 +00:00
|
|
|
// Copyright (c) 2018 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/dominator_analysis.h"
|
2018-01-16 16:15:06 +00:00
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/ir_context.h"
|
2018-03-21 03:33:24 +00:00
|
|
|
|
2018-01-16 16:15:06 +00:00
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* DominatorAnalysisBase::CommonDominator(BasicBlock* b1,
|
|
|
|
BasicBlock* b2) const {
|
2018-01-16 16:15:06 +00:00
|
|
|
if (!b1 || !b2) return nullptr;
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_set<BasicBlock*> seen;
|
|
|
|
BasicBlock* block = b1;
|
2018-01-16 16:15:06 +00:00
|
|
|
while (block && seen.insert(block).second) {
|
|
|
|
block = ImmediateDominator(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
block = b2;
|
|
|
|
while (block && !seen.count(block)) {
|
|
|
|
block = ImmediateDominator(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
bool DominatorAnalysisBase::Dominates(Instruction* a, Instruction* b) const {
|
2018-03-21 03:33:24 +00:00
|
|
|
if (!a || !b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a == b) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* bb_a = a->context()->get_instr_block(a);
|
|
|
|
BasicBlock* bb_b = b->context()->get_instr_block(b);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
if (bb_a != bb_b) {
|
|
|
|
return tree_.Dominates(bb_a, bb_b);
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:59:35 +00:00
|
|
|
const Instruction* current = a;
|
|
|
|
const Instruction* other = b;
|
|
|
|
|
|
|
|
if (tree_.IsPostDominator()) {
|
|
|
|
std::swap(current, other);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We handle OpLabel instructions explicitly since they are not stored in the
|
|
|
|
// instruction list.
|
|
|
|
if (current->opcode() == SpvOpLabel) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((current = current->NextNode())) {
|
|
|
|
if (current == other) {
|
2018-03-21 03:33:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 11:59:35 +00:00
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-16 16:15:06 +00:00
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|