1//===- SSAUpdaterBulk.h - Unstructured SSA Update Tool ----------*- C++ -*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the SSAUpdaterBulk class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATERBULK_H
14#define LLVM_TRANSFORMS_UTILS_SSAUPDATERBULK_H
15
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/PredIteratorCache.h"
19
20namespace llvm {
21
22class BasicBlock;
23class PHINode;
24template <typename T> class SmallVectorImpl;
25class Type;
26class Use;
27class Value;
28class DominatorTree;
29
30/// Helper class for SSA formation on a set of values defined in multiple
31/// blocks.
32///
33/// This is used when code duplication or another unstructured transformation
34/// wants to rewrite a set of uses of one value with uses of a set of values.
35/// The update is done only when RewriteAllUses is called, all other methods are
36/// used for book-keeping. That helps to share some common computations between
37/// updates of different uses (which is not the case when traditional SSAUpdater
38/// is used).
39class SSAUpdaterBulk {
40 struct RewriteInfo {
41 DenseMap<BasicBlock *, Value *> Defines;
42 SmallVector<Use *, 4> Uses;
43 StringRef Name;
44 Type *Ty;
45 RewriteInfo(){};
46 RewriteInfo(StringRef &N, Type *T) : Name(N), Ty(T){};
47 };
48 SmallVector<RewriteInfo, 4> Rewrites;
49
50 PredIteratorCache PredCache;
51
52 Value *computeValueAt(BasicBlock *BB, RewriteInfo &R, DominatorTree *DT);
53
54public:
55 explicit SSAUpdaterBulk(){};
56 SSAUpdaterBulk(const SSAUpdaterBulk &) = delete;
57 SSAUpdaterBulk &operator=(const SSAUpdaterBulk &) = delete;
58 ~SSAUpdaterBulk(){};
59
60 /// Add a new variable to the SSA rewriter. This needs to be called before
61 /// AddAvailableValue or AddUse calls. The return value is the variable ID,
62 /// which needs to be passed to AddAvailableValue and AddUse.
63 unsigned AddVariable(StringRef Name, Type *Ty);
64
65 /// Indicate that a rewritten value is available in the specified block with
66 /// the specified value.
67 void AddAvailableValue(unsigned Var, BasicBlock *BB, Value *V);
68
69 /// Record a use of the symbolic value. This use will be updated with a
70 /// rewritten value when RewriteAllUses is called.
71 void AddUse(unsigned Var, Use *U);
72
73 /// Return true if the SSAUpdater already has a value for the specified
74 /// variable in the specified block.
75 bool HasValueForBlock(unsigned Var, BasicBlock *BB);
76
77 /// Perform all the necessary updates, including new PHI-nodes insertion and
78 /// the requested uses update.
79 ///
80 /// The function requires dominator tree DT, which is used for computing
81 /// locations for new phi-nodes insertions. If a nonnull pointer to a vector
82 /// InsertedPHIs is passed, all the new phi-nodes will be added to this
83 /// vector.
84 void RewriteAllUses(DominatorTree *DT,
85 SmallVectorImpl<PHINode *> *InsertedPHIs = nullptr);
86};
87
88} // end namespace llvm
89
90#endif // LLVM_TRANSFORMS_UTILS_SSAUPDATERBULK_H
91