17d523365SDimitry Andric //===- OrderedBasicBlock.cpp --------------------------------- -*- C++ -*-===//
27d523365SDimitry Andric //
37d523365SDimitry Andric //                     The LLVM Compiler Infrastructure
47d523365SDimitry Andric //
57d523365SDimitry Andric // This file is distributed under the University of Illinois Open Source
67d523365SDimitry Andric // License. See LICENSE.TXT for details.
77d523365SDimitry Andric //
87d523365SDimitry Andric //===----------------------------------------------------------------------===//
97d523365SDimitry Andric //
107d523365SDimitry Andric // This file implements the OrderedBasicBlock class. OrderedBasicBlock
117d523365SDimitry Andric // maintains an interface where clients can query if one instruction comes
127d523365SDimitry Andric // before another in a BasicBlock. Since BasicBlock currently lacks a reliable
137d523365SDimitry Andric // way to query relative position between instructions one can use
147d523365SDimitry Andric // OrderedBasicBlock to do such queries. OrderedBasicBlock is lazily built on a
157d523365SDimitry Andric // source BasicBlock and maintains an internal Instruction -> Position map. A
167d523365SDimitry Andric // OrderedBasicBlock instance should be discarded whenever the source
177d523365SDimitry Andric // BasicBlock changes.
187d523365SDimitry Andric //
197d523365SDimitry Andric // It's currently used by the CaptureTracker in order to find relative
207d523365SDimitry Andric // positions of a pair of instructions inside a BasicBlock.
217d523365SDimitry Andric //
227d523365SDimitry Andric //===----------------------------------------------------------------------===//
237d523365SDimitry Andric 
247d523365SDimitry Andric #include "llvm/Analysis/OrderedBasicBlock.h"
257d523365SDimitry Andric #include "llvm/IR/Instruction.h"
267d523365SDimitry Andric using namespace llvm;
277d523365SDimitry Andric 
OrderedBasicBlock(const BasicBlock * BasicB)287d523365SDimitry Andric OrderedBasicBlock::OrderedBasicBlock(const BasicBlock *BasicB)
297d523365SDimitry Andric     : NextInstPos(0), BB(BasicB) {
307d523365SDimitry Andric   LastInstFound = BB->end();
317d523365SDimitry Andric }
327d523365SDimitry Andric 
334ba319b5SDimitry Andric /// Given no cached results, find if \p A comes before \p B in \p BB.
347d523365SDimitry Andric /// Cache and number out instruction while walking \p BB.
comesBefore(const Instruction * A,const Instruction * B)357d523365SDimitry Andric bool OrderedBasicBlock::comesBefore(const Instruction *A,
367d523365SDimitry Andric                                     const Instruction *B) {
377d523365SDimitry Andric   const Instruction *Inst = nullptr;
387d523365SDimitry Andric   assert(!(LastInstFound == BB->end() && NextInstPos != 0) &&
397d523365SDimitry Andric          "Instruction supposed to be in NumberedInsts");
40*b5893f02SDimitry Andric   assert(A->getParent() == BB && "Instruction supposed to be in the block!");
41*b5893f02SDimitry Andric   assert(B->getParent() == BB && "Instruction supposed to be in the block!");
427d523365SDimitry Andric 
437d523365SDimitry Andric   // Start the search with the instruction found in the last lookup round.
447d523365SDimitry Andric   auto II = BB->begin();
457d523365SDimitry Andric   auto IE = BB->end();
467d523365SDimitry Andric   if (LastInstFound != IE)
477d523365SDimitry Andric     II = std::next(LastInstFound);
487d523365SDimitry Andric 
497d523365SDimitry Andric   // Number all instructions up to the point where we find 'A' or 'B'.
507d523365SDimitry Andric   for (; II != IE; ++II) {
517d523365SDimitry Andric     Inst = cast<Instruction>(II);
527d523365SDimitry Andric     NumberedInsts[Inst] = NextInstPos++;
537d523365SDimitry Andric     if (Inst == A || Inst == B)
547d523365SDimitry Andric       break;
557d523365SDimitry Andric   }
567d523365SDimitry Andric 
577d523365SDimitry Andric   assert(II != IE && "Instruction not found?");
587d523365SDimitry Andric   assert((Inst == A || Inst == B) && "Should find A or B");
597d523365SDimitry Andric   LastInstFound = II;
606d97bb29SDimitry Andric   return Inst != B;
617d523365SDimitry Andric }
627d523365SDimitry Andric 
634ba319b5SDimitry Andric /// Find out whether \p A dominates \p B, meaning whether \p A
647d523365SDimitry Andric /// comes before \p B in \p BB. This is a simplification that considers
657d523365SDimitry Andric /// cached instruction positions and ignores other basic blocks, being
667d523365SDimitry Andric /// only relevant to compare relative instructions positions inside \p BB.
dominates(const Instruction * A,const Instruction * B)677d523365SDimitry Andric bool OrderedBasicBlock::dominates(const Instruction *A, const Instruction *B) {
687d523365SDimitry Andric   assert(A->getParent() == B->getParent() &&
697d523365SDimitry Andric          "Instructions must be in the same basic block!");
70*b5893f02SDimitry Andric   assert(A->getParent() == BB && "Instructions must be in the tracked block!");
717d523365SDimitry Andric 
727d523365SDimitry Andric   // First we lookup the instructions. If they don't exist, lookup will give us
737d523365SDimitry Andric   // back ::end(). If they both exist, we compare the numbers. Otherwise, if NA
747d523365SDimitry Andric   // exists and NB doesn't, it means NA must come before NB because we would
757d523365SDimitry Andric   // have numbered NB as well if it didn't. The same is true for NB. If it
767d523365SDimitry Andric   // exists, but NA does not, NA must come after it. If neither exist, we need
777d523365SDimitry Andric   // to number the block and cache the results (by calling comesBefore).
787d523365SDimitry Andric   auto NAI = NumberedInsts.find(A);
797d523365SDimitry Andric   auto NBI = NumberedInsts.find(B);
807d523365SDimitry Andric   if (NAI != NumberedInsts.end() && NBI != NumberedInsts.end())
817d523365SDimitry Andric     return NAI->second < NBI->second;
827d523365SDimitry Andric   if (NAI != NumberedInsts.end())
837d523365SDimitry Andric     return true;
847d523365SDimitry Andric   if (NBI != NumberedInsts.end())
857d523365SDimitry Andric     return false;
867d523365SDimitry Andric 
877d523365SDimitry Andric   return comesBefore(A, B);
887d523365SDimitry Andric }
89