Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 1 | //===-- OrderedInstructions.cpp - Instruction dominance function ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines utility to check dominance relation of 2 instructions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Max Kazantsev | d3a4cbe | 2018-08-30 04:49:03 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/OrderedInstructions.h" |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 14 | using namespace llvm; |
| 15 | |
Florian Hahn | 5ac2629 | 2018-06-20 17:42:01 +0000 | [diff] [blame] | 16 | bool OrderedInstructions::localDominates(const Instruction *InstA, |
| 17 | const Instruction *InstB) const { |
| 18 | assert(InstA->getParent() == InstB->getParent() && |
| 19 | "Instructions must be in the same basic block"); |
| 20 | |
| 21 | const BasicBlock *IBB = InstA->getParent(); |
| 22 | auto OBB = OBBMap.find(IBB); |
| 23 | if (OBB == OBBMap.end()) |
| 24 | OBB = OBBMap.insert({IBB, make_unique<OrderedBasicBlock>(IBB)}).first; |
| 25 | return OBB->second->dominates(InstA, InstB); |
| 26 | } |
| 27 | |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 28 | /// Given 2 instructions, use OrderedBasicBlock to check for dominance relation |
| 29 | /// if the instructions are in the same basic block, Otherwise, use dominator |
| 30 | /// tree. |
| 31 | bool OrderedInstructions::dominates(const Instruction *InstA, |
| 32 | const Instruction *InstB) const { |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 33 | // Use ordered basic block to do dominance check in case the 2 instructions |
| 34 | // are in the same basic block. |
Florian Hahn | 5ac2629 | 2018-06-20 17:42:01 +0000 | [diff] [blame] | 35 | if (InstA->getParent() == InstB->getParent()) |
| 36 | return localDominates(InstA, InstB); |
Daniel Berlin | 7c757ae | 2017-06-29 17:01:03 +0000 | [diff] [blame] | 37 | return DT->dominates(InstA->getParent(), InstB->getParent()); |
Xin Tong | 9d6f08a | 2017-06-06 02:34:41 +0000 | [diff] [blame] | 38 | } |
Florian Hahn | 5ac2629 | 2018-06-20 17:42:01 +0000 | [diff] [blame] | 39 | |
| 40 | bool OrderedInstructions::dfsBefore(const Instruction *InstA, |
| 41 | const Instruction *InstB) const { |
| 42 | // Use ordered basic block in case the 2 instructions are in the same basic |
| 43 | // block. |
| 44 | if (InstA->getParent() == InstB->getParent()) |
| 45 | return localDominates(InstA, InstB); |
| 46 | |
| 47 | DomTreeNode *DA = DT->getNode(InstA->getParent()); |
| 48 | DomTreeNode *DB = DT->getNode(InstB->getParent()); |
| 49 | return DA->getDFSNumIn() < DB->getDFSNumIn(); |
| 50 | } |