1*b5893f02SDimitry Andric //===-- OrderedInstructions.cpp - Instruction dominance function ---------===//
2*b5893f02SDimitry Andric //
3*b5893f02SDimitry Andric // The LLVM Compiler Infrastructure
4*b5893f02SDimitry Andric //
5*b5893f02SDimitry Andric // This file is distributed under the University of Illinois Open Source
6*b5893f02SDimitry Andric // License. See LICENSE.TXT for details.
7*b5893f02SDimitry Andric //
8*b5893f02SDimitry Andric //===----------------------------------------------------------------------===//
9*b5893f02SDimitry Andric //
10*b5893f02SDimitry Andric // This file defines utility to check dominance relation of 2 instructions.
11*b5893f02SDimitry Andric //
12*b5893f02SDimitry Andric //===----------------------------------------------------------------------===//
13*b5893f02SDimitry Andric
14*b5893f02SDimitry Andric #include "llvm/Analysis/OrderedInstructions.h"
15*b5893f02SDimitry Andric using namespace llvm;
16*b5893f02SDimitry Andric
localDominates(const Instruction * InstA,const Instruction * InstB) const17*b5893f02SDimitry Andric bool OrderedInstructions::localDominates(const Instruction *InstA,
18*b5893f02SDimitry Andric const Instruction *InstB) const {
19*b5893f02SDimitry Andric assert(InstA->getParent() == InstB->getParent() &&
20*b5893f02SDimitry Andric "Instructions must be in the same basic block");
21*b5893f02SDimitry Andric
22*b5893f02SDimitry Andric const BasicBlock *IBB = InstA->getParent();
23*b5893f02SDimitry Andric auto OBB = OBBMap.find(IBB);
24*b5893f02SDimitry Andric if (OBB == OBBMap.end())
25*b5893f02SDimitry Andric OBB = OBBMap.insert({IBB, make_unique<OrderedBasicBlock>(IBB)}).first;
26*b5893f02SDimitry Andric return OBB->second->dominates(InstA, InstB);
27*b5893f02SDimitry Andric }
28*b5893f02SDimitry Andric
29*b5893f02SDimitry Andric /// Given 2 instructions, use OrderedBasicBlock to check for dominance relation
30*b5893f02SDimitry Andric /// if the instructions are in the same basic block, Otherwise, use dominator
31*b5893f02SDimitry Andric /// tree.
dominates(const Instruction * InstA,const Instruction * InstB) const32*b5893f02SDimitry Andric bool OrderedInstructions::dominates(const Instruction *InstA,
33*b5893f02SDimitry Andric const Instruction *InstB) const {
34*b5893f02SDimitry Andric // Use ordered basic block to do dominance check in case the 2 instructions
35*b5893f02SDimitry Andric // are in the same basic block.
36*b5893f02SDimitry Andric if (InstA->getParent() == InstB->getParent())
37*b5893f02SDimitry Andric return localDominates(InstA, InstB);
38*b5893f02SDimitry Andric return DT->dominates(InstA->getParent(), InstB->getParent());
39*b5893f02SDimitry Andric }
40*b5893f02SDimitry Andric
dfsBefore(const Instruction * InstA,const Instruction * InstB) const41*b5893f02SDimitry Andric bool OrderedInstructions::dfsBefore(const Instruction *InstA,
42*b5893f02SDimitry Andric const Instruction *InstB) const {
43*b5893f02SDimitry Andric // Use ordered basic block in case the 2 instructions are in the same basic
44*b5893f02SDimitry Andric // block.
45*b5893f02SDimitry Andric if (InstA->getParent() == InstB->getParent())
46*b5893f02SDimitry Andric return localDominates(InstA, InstB);
47*b5893f02SDimitry Andric
48*b5893f02SDimitry Andric DomTreeNode *DA = DT->getNode(InstA->getParent());
49*b5893f02SDimitry Andric DomTreeNode *DB = DT->getNode(InstB->getParent());
50*b5893f02SDimitry Andric return DA->getDFSNumIn() < DB->getDFSNumIn();
51*b5893f02SDimitry Andric }
52