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 287d523365SDimitry Andric OrderedBasicBlock::OrderedBasicBlock(const BasicBlock *BasicB) 297d523365SDimitry Andric : NextInstPos(0), BB(BasicB) { 307d523365SDimitry Andric LastInstFound = BB->end(); 317d523365SDimitry Andric } 327d523365SDimitry Andric 337d523365SDimitry Andric /// \brief 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. 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"); 407d523365SDimitry Andric 417d523365SDimitry Andric // Start the search with the instruction found in the last lookup round. 427d523365SDimitry Andric auto II = BB->begin(); 437d523365SDimitry Andric auto IE = BB->end(); 447d523365SDimitry Andric if (LastInstFound != IE) 457d523365SDimitry Andric II = std::next(LastInstFound); 467d523365SDimitry Andric 477d523365SDimitry Andric // Number all instructions up to the point where we find 'A' or 'B'. 487d523365SDimitry Andric for (; II != IE; ++II) { 497d523365SDimitry Andric Inst = cast<Instruction>(II); 507d523365SDimitry Andric NumberedInsts[Inst] = NextInstPos++; 517d523365SDimitry Andric if (Inst == A || Inst == B) 527d523365SDimitry Andric break; 537d523365SDimitry Andric } 547d523365SDimitry Andric 557d523365SDimitry Andric assert(II != IE && "Instruction not found?"); 567d523365SDimitry Andric assert((Inst == A || Inst == B) && "Should find A or B"); 577d523365SDimitry Andric LastInstFound = II; 586d97bb29SDimitry Andric return Inst != B; 597d523365SDimitry Andric } 607d523365SDimitry Andric 617d523365SDimitry Andric /// \brief Find out whether \p A dominates \p B, meaning whether \p A 627d523365SDimitry Andric /// comes before \p B in \p BB. This is a simplification that considers 637d523365SDimitry Andric /// cached instruction positions and ignores other basic blocks, being 647d523365SDimitry Andric /// only relevant to compare relative instructions positions inside \p BB. 657d523365SDimitry Andric bool OrderedBasicBlock::dominates(const Instruction *A, const Instruction *B) { 667d523365SDimitry Andric assert(A->getParent() == B->getParent() && 677d523365SDimitry Andric "Instructions must be in the same basic block!"); 687d523365SDimitry Andric 697d523365SDimitry Andric // First we lookup the instructions. If they don't exist, lookup will give us 707d523365SDimitry Andric // back ::end(). If they both exist, we compare the numbers. Otherwise, if NA 717d523365SDimitry Andric // exists and NB doesn't, it means NA must come before NB because we would 727d523365SDimitry Andric // have numbered NB as well if it didn't. The same is true for NB. If it 737d523365SDimitry Andric // exists, but NA does not, NA must come after it. If neither exist, we need 747d523365SDimitry Andric // to number the block and cache the results (by calling comesBefore). 757d523365SDimitry Andric auto NAI = NumberedInsts.find(A); 767d523365SDimitry Andric auto NBI = NumberedInsts.find(B); 777d523365SDimitry Andric if (NAI != NumberedInsts.end() && NBI != NumberedInsts.end()) 787d523365SDimitry Andric return NAI->second < NBI->second; 797d523365SDimitry Andric if (NAI != NumberedInsts.end()) 807d523365SDimitry Andric return true; 817d523365SDimitry Andric if (NBI != NumberedInsts.end()) 827d523365SDimitry Andric return false; 837d523365SDimitry Andric 847d523365SDimitry Andric return comesBefore(A, B); 857d523365SDimitry Andric } 86