1*9880d681SAndroid Build Coastguard Worker //===- OrderedBasicBlock.cpp --------------------------------- -*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements the OrderedBasicBlock class. OrderedBasicBlock
11*9880d681SAndroid Build Coastguard Worker // maintains an interface where clients can query if one instruction comes
12*9880d681SAndroid Build Coastguard Worker // before another in a BasicBlock. Since BasicBlock currently lacks a reliable
13*9880d681SAndroid Build Coastguard Worker // way to query relative position between instructions one can use
14*9880d681SAndroid Build Coastguard Worker // OrderedBasicBlock to do such queries. OrderedBasicBlock is lazily built on a
15*9880d681SAndroid Build Coastguard Worker // source BasicBlock and maintains an internal Instruction -> Position map. A
16*9880d681SAndroid Build Coastguard Worker // OrderedBasicBlock instance should be discarded whenever the source
17*9880d681SAndroid Build Coastguard Worker // BasicBlock changes.
18*9880d681SAndroid Build Coastguard Worker //
19*9880d681SAndroid Build Coastguard Worker // It's currently used by the CaptureTracker in order to find relative
20*9880d681SAndroid Build Coastguard Worker // positions of a pair of instructions inside a BasicBlock.
21*9880d681SAndroid Build Coastguard Worker //
22*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/OrderedBasicBlock.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.h"
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
OrderedBasicBlock(const BasicBlock * BasicB)28*9880d681SAndroid Build Coastguard Worker OrderedBasicBlock::OrderedBasicBlock(const BasicBlock *BasicB)
29*9880d681SAndroid Build Coastguard Worker : NextInstPos(0), BB(BasicB) {
30*9880d681SAndroid Build Coastguard Worker LastInstFound = BB->end();
31*9880d681SAndroid Build Coastguard Worker }
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker /// \brief Given no cached results, find if \p A comes before \p B in \p BB.
34*9880d681SAndroid Build Coastguard Worker /// Cache and number out instruction while walking \p BB.
comesBefore(const Instruction * A,const Instruction * B)35*9880d681SAndroid Build Coastguard Worker bool OrderedBasicBlock::comesBefore(const Instruction *A,
36*9880d681SAndroid Build Coastguard Worker const Instruction *B) {
37*9880d681SAndroid Build Coastguard Worker const Instruction *Inst = nullptr;
38*9880d681SAndroid Build Coastguard Worker assert(!(LastInstFound == BB->end() && NextInstPos != 0) &&
39*9880d681SAndroid Build Coastguard Worker "Instruction supposed to be in NumberedInsts");
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker // Start the search with the instruction found in the last lookup round.
42*9880d681SAndroid Build Coastguard Worker auto II = BB->begin();
43*9880d681SAndroid Build Coastguard Worker auto IE = BB->end();
44*9880d681SAndroid Build Coastguard Worker if (LastInstFound != IE)
45*9880d681SAndroid Build Coastguard Worker II = std::next(LastInstFound);
46*9880d681SAndroid Build Coastguard Worker
47*9880d681SAndroid Build Coastguard Worker // Number all instructions up to the point where we find 'A' or 'B'.
48*9880d681SAndroid Build Coastguard Worker for (; II != IE; ++II) {
49*9880d681SAndroid Build Coastguard Worker Inst = cast<Instruction>(II);
50*9880d681SAndroid Build Coastguard Worker NumberedInsts[Inst] = NextInstPos++;
51*9880d681SAndroid Build Coastguard Worker if (Inst == A || Inst == B)
52*9880d681SAndroid Build Coastguard Worker break;
53*9880d681SAndroid Build Coastguard Worker }
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker assert(II != IE && "Instruction not found?");
56*9880d681SAndroid Build Coastguard Worker assert((Inst == A || Inst == B) && "Should find A or B");
57*9880d681SAndroid Build Coastguard Worker LastInstFound = II;
58*9880d681SAndroid Build Coastguard Worker return Inst == A;
59*9880d681SAndroid Build Coastguard Worker }
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker /// \brief Find out whether \p A dominates \p B, meaning whether \p A
62*9880d681SAndroid Build Coastguard Worker /// comes before \p B in \p BB. This is a simplification that considers
63*9880d681SAndroid Build Coastguard Worker /// cached instruction positions and ignores other basic blocks, being
64*9880d681SAndroid Build Coastguard Worker /// only relevant to compare relative instructions positions inside \p BB.
dominates(const Instruction * A,const Instruction * B)65*9880d681SAndroid Build Coastguard Worker bool OrderedBasicBlock::dominates(const Instruction *A, const Instruction *B) {
66*9880d681SAndroid Build Coastguard Worker assert(A->getParent() == B->getParent() &&
67*9880d681SAndroid Build Coastguard Worker "Instructions must be in the same basic block!");
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker // First we lookup the instructions. If they don't exist, lookup will give us
70*9880d681SAndroid Build Coastguard Worker // back ::end(). If they both exist, we compare the numbers. Otherwise, if NA
71*9880d681SAndroid Build Coastguard Worker // exists and NB doesn't, it means NA must come before NB because we would
72*9880d681SAndroid Build Coastguard Worker // have numbered NB as well if it didn't. The same is true for NB. If it
73*9880d681SAndroid Build Coastguard Worker // exists, but NA does not, NA must come after it. If neither exist, we need
74*9880d681SAndroid Build Coastguard Worker // to number the block and cache the results (by calling comesBefore).
75*9880d681SAndroid Build Coastguard Worker auto NAI = NumberedInsts.find(A);
76*9880d681SAndroid Build Coastguard Worker auto NBI = NumberedInsts.find(B);
77*9880d681SAndroid Build Coastguard Worker if (NAI != NumberedInsts.end() && NBI != NumberedInsts.end())
78*9880d681SAndroid Build Coastguard Worker return NAI->second < NBI->second;
79*9880d681SAndroid Build Coastguard Worker if (NAI != NumberedInsts.end())
80*9880d681SAndroid Build Coastguard Worker return true;
81*9880d681SAndroid Build Coastguard Worker if (NBI != NumberedInsts.end())
82*9880d681SAndroid Build Coastguard Worker return false;
83*9880d681SAndroid Build Coastguard Worker
84*9880d681SAndroid Build Coastguard Worker return comesBefore(A, B);
85*9880d681SAndroid Build Coastguard Worker }
86