2017-06-06 10:34:41 +08:00
|
|
|
//===-- OrderedInstructions.cpp - Instruction dominance function ---------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-06-06 10:34:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines utility to check dominance relation of 2 instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-08-30 12:49:03 +08:00
|
|
|
#include "llvm/Analysis/OrderedInstructions.h"
|
2017-06-06 10:34:41 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2018-06-21 01:42:01 +08:00
|
|
|
bool OrderedInstructions::localDominates(const Instruction *InstA,
|
|
|
|
const Instruction *InstB) const {
|
|
|
|
assert(InstA->getParent() == InstB->getParent() &&
|
|
|
|
"Instructions must be in the same basic block");
|
|
|
|
|
|
|
|
const BasicBlock *IBB = InstA->getParent();
|
|
|
|
auto OBB = OBBMap.find(IBB);
|
|
|
|
if (OBB == OBBMap.end())
|
|
|
|
OBB = OBBMap.insert({IBB, make_unique<OrderedBasicBlock>(IBB)}).first;
|
|
|
|
return OBB->second->dominates(InstA, InstB);
|
|
|
|
}
|
|
|
|
|
2017-06-06 10:34:41 +08:00
|
|
|
/// Given 2 instructions, use OrderedBasicBlock to check for dominance relation
|
|
|
|
/// if the instructions are in the same basic block, Otherwise, use dominator
|
|
|
|
/// tree.
|
|
|
|
bool OrderedInstructions::dominates(const Instruction *InstA,
|
|
|
|
const Instruction *InstB) const {
|
|
|
|
// Use ordered basic block to do dominance check in case the 2 instructions
|
|
|
|
// are in the same basic block.
|
2018-06-21 01:42:01 +08:00
|
|
|
if (InstA->getParent() == InstB->getParent())
|
|
|
|
return localDominates(InstA, InstB);
|
2017-06-30 01:01:03 +08:00
|
|
|
return DT->dominates(InstA->getParent(), InstB->getParent());
|
2017-06-06 10:34:41 +08:00
|
|
|
}
|
2018-06-21 01:42:01 +08:00
|
|
|
|
|
|
|
bool OrderedInstructions::dfsBefore(const Instruction *InstA,
|
|
|
|
const Instruction *InstB) const {
|
|
|
|
// Use ordered basic block in case the 2 instructions are in the same basic
|
|
|
|
// block.
|
|
|
|
if (InstA->getParent() == InstB->getParent())
|
|
|
|
return localDominates(InstA, InstB);
|
|
|
|
|
|
|
|
DomTreeNode *DA = DT->getNode(InstA->getParent());
|
|
|
|
DomTreeNode *DB = DT->getNode(InstB->getParent());
|
|
|
|
return DA->getDFSNumIn() < DB->getDFSNumIn();
|
|
|
|
}
|