blob: 967b146b52debd6744531b3f0d615ca9220aa04c [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/Transforms/Utils/OrderedInstructions.h -------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an efficient way to check for dominance relation between 2
10// instructions.
11//
12// This interface dispatches to appropriate dominance check given 2
13// instructions, i.e. in case the instructions are in the same basic block,
14// OrderedBasicBlock (with instruction numbering and caching) are used.
15// Otherwise, dominator tree is used.
16//
17//===----------------------------------------------------------------------===//
18
Andrew Scull0372a572018-11-16 15:47:06 +000019#ifndef LLVM_ANALYSIS_ORDEREDINSTRUCTIONS_H
20#define LLVM_ANALYSIS_ORDEREDINSTRUCTIONS_H
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010021
22#include "llvm/ADT/DenseMap.h"
23#include "llvm/Analysis/OrderedBasicBlock.h"
24#include "llvm/IR/Dominators.h"
25#include "llvm/IR/Operator.h"
26
27namespace llvm {
28
29class OrderedInstructions {
30 /// Used to check dominance for instructions in same basic block.
31 mutable DenseMap<const BasicBlock *, std::unique_ptr<OrderedBasicBlock>>
32 OBBMap;
33
34 /// The dominator tree of the parent function.
35 DominatorTree *DT;
36
Andrew Scullcdfcccc2018-10-05 20:58:37 +010037 /// Return true if the first instruction comes before the second in the
38 /// same basic block. It will create an ordered basic block, if it does
39 /// not yet exist in OBBMap.
40 bool localDominates(const Instruction *, const Instruction *) const;
41
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042public:
43 /// Constructor.
44 OrderedInstructions(DominatorTree *DT) : DT(DT) {}
45
46 /// Return true if first instruction dominates the second.
47 bool dominates(const Instruction *, const Instruction *) const;
48
Andrew Scullcdfcccc2018-10-05 20:58:37 +010049 /// Return true if the first instruction comes before the second in the
50 /// dominator tree DFS traversal if they are in different basic blocks,
51 /// or if the first instruction comes before the second in the same basic
52 /// block.
53 bool dfsBefore(const Instruction *, const Instruction *) const;
54
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055 /// Invalidate the OrderedBasicBlock cache when its basic block changes.
56 /// i.e. If an instruction is deleted or added to the basic block, the user
57 /// should call this function to invalidate the OrderedBasicBlock cache for
58 /// this basic block.
59 void invalidateBlock(const BasicBlock *BB) { OBBMap.erase(BB); }
60};
61
62} // end namespace llvm
63
Andrew Scull0372a572018-11-16 15:47:06 +000064#endif // LLVM_ANALYSIS_ORDEREDINSTRUCTIONS_H