blob: 7e3850b87c57541a71fffd2f270d3227d15a4d44 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Transforms/Utils/OrderedInstructions.h -------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an efficient way to check for dominance relation between 2
11// instructions.
12//
13// This interface dispatches to appropriate dominance check given 2
14// instructions, i.e. in case the instructions are in the same basic block,
15// OrderedBasicBlock (with instruction numbering and caching) are used.
16// Otherwise, dominator tree is used.
17//
18//===----------------------------------------------------------------------===//
19
Andrew Scull0372a572018-11-16 15:47:06 +000020#ifndef LLVM_ANALYSIS_ORDEREDINSTRUCTIONS_H
21#define LLVM_ANALYSIS_ORDEREDINSTRUCTIONS_H
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010022
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/Analysis/OrderedBasicBlock.h"
25#include "llvm/IR/Dominators.h"
26#include "llvm/IR/Operator.h"
27
28namespace llvm {
29
30class OrderedInstructions {
31 /// Used to check dominance for instructions in same basic block.
32 mutable DenseMap<const BasicBlock *, std::unique_ptr<OrderedBasicBlock>>
33 OBBMap;
34
35 /// The dominator tree of the parent function.
36 DominatorTree *DT;
37
Andrew Scullcdfcccc2018-10-05 20:58:37 +010038 /// Return true if the first instruction comes before the second in the
39 /// same basic block. It will create an ordered basic block, if it does
40 /// not yet exist in OBBMap.
41 bool localDominates(const Instruction *, const Instruction *) const;
42
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043public:
44 /// Constructor.
45 OrderedInstructions(DominatorTree *DT) : DT(DT) {}
46
47 /// Return true if first instruction dominates the second.
48 bool dominates(const Instruction *, const Instruction *) const;
49
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// Return true if the first instruction comes before the second in the
51 /// dominator tree DFS traversal if they are in different basic blocks,
52 /// or if the first instruction comes before the second in the same basic
53 /// block.
54 bool dfsBefore(const Instruction *, const Instruction *) const;
55
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010056 /// Invalidate the OrderedBasicBlock cache when its basic block changes.
57 /// i.e. If an instruction is deleted or added to the basic block, the user
58 /// should call this function to invalidate the OrderedBasicBlock cache for
59 /// this basic block.
60 void invalidateBlock(const BasicBlock *BB) { OBBMap.erase(BB); }
61};
62
63} // end namespace llvm
64
Andrew Scull0372a572018-11-16 15:47:06 +000065#endif // LLVM_ANALYSIS_ORDEREDINSTRUCTIONS_H