2009-10-10 09:04:27 +00:00
|
|
|
//===-- SSAUpdater.h - Unstructured SSA Update Tool -------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file declares the SSAUpdater class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
|
|
|
|
#define LLVM_TRANSFORMS_UTILS_SSAUPDATER_H
|
|
|
|
|
2011-11-14 17:22:45 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2011-11-14 17:22:45 +00:00
|
|
|
|
2009-10-10 09:04:27 +00:00
|
|
|
namespace llvm {
|
|
|
|
class BasicBlock;
|
2011-11-14 17:22:45 +00:00
|
|
|
class Instruction;
|
|
|
|
class LoadInst;
|
2010-05-04 23:18:19 +00:00
|
|
|
template<typename T> class SmallVectorImpl;
|
|
|
|
template<typename T> class SSAUpdaterTraits;
|
2011-11-14 17:22:45 +00:00
|
|
|
class PHINode;
|
|
|
|
class Type;
|
|
|
|
class Use;
|
|
|
|
class Value;
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Helper class for SSA formation on a set of values defined in
|
|
|
|
/// multiple blocks.
|
|
|
|
///
|
|
|
|
/// This is used when code duplication or another unstructured
|
2009-10-10 09:04:27 +00:00
|
|
|
/// transformation wants to rewrite a set of uses of one value with uses of a
|
|
|
|
/// set of values.
|
|
|
|
class SSAUpdater {
|
2010-05-04 23:18:19 +00:00
|
|
|
friend class SSAUpdaterTraits<SSAUpdater>;
|
2010-04-17 03:08:24 +00:00
|
|
|
|
|
|
|
private:
|
2013-07-28 22:00:33 +00:00
|
|
|
/// This keeps track of which value to use on a per-block basis. When we
|
|
|
|
/// insert PHI nodes, we keep track of them here.
|
2010-04-17 03:08:24 +00:00
|
|
|
//typedef DenseMap<BasicBlock*, Value*> AvailableValsTy;
|
2009-10-10 09:04:27 +00:00
|
|
|
void *AV;
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2010-09-02 08:14:03 +00:00
|
|
|
/// ProtoType holds the type of the values being rewritten.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *ProtoType;
|
2010-09-02 08:14:03 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// PHI nodes are given a name based on ProtoName.
|
2010-09-02 08:14:03 +00:00
|
|
|
std::string ProtoName;
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// If this is non-null, the SSAUpdater adds all PHI nodes that it creates to
|
|
|
|
/// the vector.
|
2009-10-10 23:15:24 +00:00
|
|
|
SmallVectorImpl<PHINode*> *InsertedPHIs;
|
2010-05-04 23:18:19 +00:00
|
|
|
|
2009-10-10 09:04:27 +00:00
|
|
|
public:
|
2013-07-28 22:00:33 +00:00
|
|
|
/// If InsertedPHIs is specified, it will be filled
|
2009-10-10 23:15:24 +00:00
|
|
|
/// in with all PHI Nodes created by rewriting.
|
2014-04-14 00:51:57 +00:00
|
|
|
explicit SSAUpdater(SmallVectorImpl<PHINode*> *InsertedPHIs = nullptr);
|
2009-10-10 09:04:27 +00:00
|
|
|
~SSAUpdater();
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Reset this object to get ready for a new set of SSA updates with
|
|
|
|
/// type 'Ty'.
|
|
|
|
///
|
|
|
|
/// PHI nodes get a name based on 'Name'.
|
2011-07-18 04:54:35 +00:00
|
|
|
void Initialize(Type *Ty, StringRef Name);
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Indicate that a rewritten value is available in the specified block
|
|
|
|
/// with the specified value.
|
2009-10-10 09:04:27 +00:00
|
|
|
void AddAvailableValue(BasicBlock *BB, Value *V);
|
2009-10-10 23:41:48 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Return true if the SSAUpdater already has a value for the specified
|
|
|
|
/// block.
|
2009-10-10 23:41:48 +00:00
|
|
|
bool HasValueForBlock(BasicBlock *BB) const;
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Construct SSA form, materializing a value that is live at the end
|
|
|
|
/// of the specified block.
|
2009-10-10 22:41:58 +00:00
|
|
|
Value *GetValueAtEndOfBlock(BasicBlock *BB);
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Construct SSA form, materializing a value that is live in the
|
|
|
|
/// middle of the specified block.
|
2009-10-10 23:00:11 +00:00
|
|
|
///
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \c GetValueInMiddleOfBlock is the same as \c GetValueAtEndOfBlock except
|
|
|
|
/// in one important case: if there is a definition of the rewritten value
|
|
|
|
/// after the 'use' in BB. Consider code like this:
|
2009-10-10 23:00:11 +00:00
|
|
|
///
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \code
|
2009-10-10 23:00:11 +00:00
|
|
|
/// X1 = ...
|
|
|
|
/// SomeBB:
|
|
|
|
/// use(X)
|
|
|
|
/// X2 = ...
|
|
|
|
/// br Cond, SomeBB, OutBB
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \endcode
|
2009-10-10 23:00:11 +00:00
|
|
|
///
|
|
|
|
/// In this case, there are two values (X1 and X2) added to the AvailableVals
|
|
|
|
/// set by the client of the rewriter, and those values are both live out of
|
|
|
|
/// their respective blocks. However, the use of X happens in the *middle* of
|
|
|
|
/// a block. Because of this, we need to insert a new PHI node in SomeBB to
|
|
|
|
/// merge the appropriate values, and this value isn't live out of the block.
|
|
|
|
Value *GetValueInMiddleOfBlock(BasicBlock *BB);
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Rewrite a use of the symbolic value.
|
|
|
|
///
|
|
|
|
/// This handles PHI nodes, which use their value in the corresponding
|
|
|
|
/// predecessor. Note that this will not work if the use is supposed to be
|
|
|
|
/// rewritten to a value defined in the same block as the use, but above it.
|
|
|
|
/// Any 'AddAvailableValue's added for the use's block will be considered to
|
|
|
|
/// be below it.
|
2009-10-10 09:04:27 +00:00
|
|
|
void RewriteUse(Use &U);
|
2009-10-16 15:20:13 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Rewrite a use like \c RewriteUse but handling in-block definitions.
|
|
|
|
///
|
|
|
|
/// This version of the method can rewrite uses in the same block as
|
|
|
|
/// a definition, because it assumes that all uses of a value are below any
|
2010-08-29 04:54:06 +00:00
|
|
|
/// inserted values.
|
|
|
|
void RewriteUseAfterInsertions(Use &U);
|
|
|
|
|
2009-10-10 09:04:27 +00:00
|
|
|
private:
|
2009-10-10 22:41:58 +00:00
|
|
|
Value *GetValueAtEndOfBlockInternal(BasicBlock *BB);
|
2010-04-17 03:08:24 +00:00
|
|
|
|
2012-09-17 07:16:40 +00:00
|
|
|
void operator=(const SSAUpdater&) LLVM_DELETED_FUNCTION;
|
|
|
|
SSAUpdater(const SSAUpdater&) LLVM_DELETED_FUNCTION;
|
2009-10-10 09:04:27 +00:00
|
|
|
};
|
2013-07-28 22:00:33 +00:00
|
|
|
|
|
|
|
/// \brief Helper class for promoting a collection of loads and stores into SSA
|
|
|
|
/// Form using the SSAUpdater.
|
|
|
|
///
|
2011-01-14 19:36:13 +00:00
|
|
|
/// This handles complexities that SSAUpdater doesn't, such as multiple loads
|
|
|
|
/// and stores in one block.
|
|
|
|
///
|
|
|
|
/// Clients of this class are expected to subclass this and implement the
|
|
|
|
/// virtual methods.
|
|
|
|
class LoadAndStorePromoter {
|
2011-01-15 00:12:35 +00:00
|
|
|
protected:
|
|
|
|
SSAUpdater &SSA;
|
2011-01-14 19:36:13 +00:00
|
|
|
public:
|
2011-01-15 00:12:35 +00:00
|
|
|
LoadAndStorePromoter(const SmallVectorImpl<Instruction*> &Insts,
|
2011-07-06 21:09:55 +00:00
|
|
|
SSAUpdater &S, StringRef Name = StringRef());
|
2011-01-14 19:36:13 +00:00
|
|
|
virtual ~LoadAndStorePromoter() {}
|
2014-05-19 04:43:03 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief This does the promotion.
|
|
|
|
///
|
|
|
|
/// Insts is a list of loads and stores to promote, and Name is the basename
|
|
|
|
/// for the PHIs to insert. After this is complete, the loads and stores are
|
|
|
|
/// removed from the code.
|
2011-01-15 00:12:35 +00:00
|
|
|
void run(const SmallVectorImpl<Instruction*> &Insts) const;
|
2014-05-19 04:43:03 +00:00
|
|
|
|
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Return true if the specified instruction is in the Inst list.
|
|
|
|
///
|
|
|
|
/// The Insts list is the one passed into the constructor. Clients should
|
|
|
|
/// implement this with a more efficient version if possible.
|
2011-01-14 19:36:13 +00:00
|
|
|
virtual bool isInstInList(Instruction *I,
|
2011-11-14 17:22:45 +00:00
|
|
|
const SmallVectorImpl<Instruction*> &Insts) const;
|
2014-05-19 04:43:03 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief This hook is invoked after all the stores are found and inserted as
|
|
|
|
/// available values.
|
2011-01-15 00:12:35 +00:00
|
|
|
virtual void doExtraRewritesBeforeFinalDeletion() const {
|
|
|
|
}
|
2014-05-19 04:43:03 +00:00
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Clients can choose to implement this to get notified right before
|
|
|
|
/// a load is RAUW'd another value.
|
2011-01-15 00:12:35 +00:00
|
|
|
virtual void replaceLoadWithValue(LoadInst *LI, Value *V) const {
|
|
|
|
}
|
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Called before each instruction is deleted.
|
2011-01-15 00:12:35 +00:00
|
|
|
virtual void instructionDeleted(Instruction *I) const {
|
|
|
|
}
|
|
|
|
|
2013-07-28 22:00:33 +00:00
|
|
|
/// \brief Called to update debug info associated with the instruction.
|
2011-07-06 21:09:55 +00:00
|
|
|
virtual void updateDebugInfo(Instruction *I) const {
|
|
|
|
}
|
2011-01-14 19:36:13 +00:00
|
|
|
};
|
2009-10-10 09:04:27 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|