31 #define DEBUG_TYPE "ssaupdater"
39 : AV(nullptr), ProtoType(nullptr), ProtoName(), InsertedPHIs(NewPHI) {}
59 assert(ProtoType &&
"Need to initialize SSAUpdater");
60 assert(ProtoType == V->
getType() &&
61 "All rewritten values must have the same type");
68 if (PHINumValues != ValueMapping.
size())
72 for (
unsigned i = 0, e = PHINumValues; i != e; ++i)
82 Value *Res = GetValueAtEndOfBlockInternal(BB);
95 Value *SingularValue =
nullptr;
100 if (
PHINode *SomePhi = dyn_cast<PHINode>(BB->
begin())) {
101 for (
unsigned i = 0, e = SomePhi->getNumIncomingValues(); i != e; ++i) {
102 BasicBlock *PredBB = SomePhi->getIncomingBlock(i);
104 PredValues.
push_back(std::make_pair(PredBB, PredVal));
108 SingularValue = PredVal;
109 else if (PredVal != SingularValue)
110 SingularValue =
nullptr;
113 bool isFirstPred =
true;
117 PredValues.
push_back(std::make_pair(PredBB, PredVal));
121 SingularValue = PredVal;
123 }
else if (PredVal != SingularValue)
124 SingularValue =
nullptr;
129 if (PredValues.
empty())
134 return SingularValue;
138 if (isa<PHINode>(BB->
begin())) {
151 ProtoName, &BB->
front());
154 for (
const auto &PredValue : PredValues)
155 InsertedPHI->
addIncoming(PredValue.second, PredValue.first);
168 DL =
I->getDebugLoc();
172 if (InsertedPHIs) InsertedPHIs->push_back(InsertedPHI);
174 DEBUG(
dbgs() <<
" Inserted PHI: " << *InsertedPHI <<
"\n");
182 if (
PHINode *UserPN = dyn_cast<PHINode>(User))
189 if (OldVal != V && OldVal->hasValueHandle())
199 if (
PHINode *UserPN = dyn_cast<PHINode>(User))
228 :
PHI(P), idx(
PHI->getNumIncomingValues()) {}
231 bool operator==(
const PHI_iterator& x)
const {
return idx == x.idx; }
239 return PHI_iterator(PHI,
true);
249 if (
PHINode *SomePhi = dyn_cast<PHINode>(BB->
begin())) {
250 Preds->
append(SomePhi->block_begin(), SomePhi->block_end());
268 Updater->ProtoName, &BB->
front());
313 if (
Value *V = AvailableVals[BB])
317 return Impl.GetValue(BB);
327 if (Insts.
empty())
return;
329 const Value *SomeVal;
330 if (
const LoadInst *LI = dyn_cast<LoadInst>(Insts[0]))
333 SomeVal = cast<StoreInst>(Insts[0])->getOperand(0);
335 if (BaseName.
empty())
350 UsesByBlock[
User->getParent()].push_back(
User);
363 if (BlockUses.
empty())
continue;
367 if (BlockUses.
size() == 1) {
380 bool HasStore =
false;
382 if (isa<StoreInst>(
I)) {
403 Value *StoredValue =
nullptr;
405 if (
LoadInst *L = dyn_cast<LoadInst>(&
I)) {
413 L->replaceAllUsesWith(StoredValue);
414 ReplacedLoads[L] = StoredValue;
427 StoredValue =
SI->getOperand(0);
432 assert(StoredValue &&
"Already checked that there is a store in block");
439 for (
LoadInst *ALoad : LiveInLoads) {
446 ReplacedLoads[ALoad] = NewVal;
461 assert(NewVal &&
"not a replaced load?");
467 while (RLI != ReplacedLoads.
end()) {
468 NewVal = RLI->second;
469 RLI = ReplacedLoads.
find(NewVal);
477 User->eraseFromParent();
485 return std::find(Insts.
begin(), Insts.
end(),
I) != Insts.
end();
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
void push_back(const T &Elt)
Helper class for SSA formation on a set of values defined in multiple blocks.
static BlkSucc_iterator BlkSucc_end(BlkT *BB)
void addIncoming(Value *V, BasicBlock *BB)
addIncoming - Add an incoming value to the end of the PHI list
static void AddPHIOperand(PHINode *PHI, Value *Val, BasicBlock *Pred)
AddPHIOperand - Add the specified value as an operand of the PHI for the specified predecessor block...
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
succ_iterator BlkSucc_iterator
static BlkSucc_iterator BlkSucc_begin(BlkT *BB)
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
static Value * CreateEmptyPHI(BasicBlock *BB, unsigned NumPreds, SSAUpdater *Updater)
CreateEmptyPHI - Create a new PHI instruction in the specified block.
const Instruction & front() const
LoadInst - an instruction for reading from memory.
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
static PHINode * InstrIsPHI(Instruction *I)
InstrIsPHI - Check if an instruction is a PHI.
A Use represents the edge between a Value definition and its users.
PHI_iterator(PHINode *P, bool)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
SSAUpdater(SmallVectorImpl< PHINode * > *InsertedPHIs=nullptr)
If InsertedPHIs is specified, it will be filled in with all PHI Nodes created by rewriting.
static PHI_iterator PHI_begin(PhiT *PHI)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
bool operator==(const PHI_iterator &x) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
StoreInst - an instruction for storing to memory.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static void ValueIsRAUWd(Value *Old, Value *New)
DenseMap< BasicBlock *, Value * > AvailableValsTy
static Value * GetUndefVal(BasicBlock *BB, SSAUpdater *Updater)
GetUndefVal - Get an undefined value of the same type as the value being handled. ...
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
getNumIncomingValues - Return the number of incoming edges
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block...
void setDebugLoc(DebugLoc Loc)
setDebugLoc - Set the debug location information for this instruction.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
void RewriteUseAfterInsertions(Use &U)
Rewrite a use like RewriteUse but handling in-block definitions.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
BasicBlock * getIncomingBlock()
BasicBlock * getIncomingBlock(unsigned i) const
getIncomingBlock - Return incoming basic block number i.
User * getUser() const
Returns the User that contains this Use.
Interval::pred_iterator pred_end(Interval *I)
bool HasValueForBlock(BasicBlock *BB) const
Return true if the SSAUpdater already has a value for the specified block.
bool empty() const
empty - Check if the array is empty.
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
static AvailableValsTy & getAvailableVals(void *AV)
static PHINode * ValueIsPHI(Value *Val, SSAUpdater *Updater)
ValueIsPHI - Check if a value is a PHI.
Value * getIncomingValue(unsigned i) const
getIncomingValue - Return incoming value number x
Value * getIncomingValue()
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
static void FindPredecessorBlocks(BasicBlock *BB, SmallVectorImpl< BasicBlock * > *Preds)
FindPredecessorBlocks - Put the predecessors of Info->BB into the Preds vector, set Info->NumPreds...
static bool IsEquivalentPHI(PHINode *PHI, SmallDenseMap< BasicBlock *, Value *, 8 > &ValueMapping)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
size_type count(const KeyT &Val) const
Return 1 if the specified key is in the map, 0 otherwise.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
iterator find(const KeyT &Val)
LLVM Value Representation.
static PHI_iterator PHI_end(PhiT *PHI)
StringRef - Represent a constant reference to a string, i.e.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
bool operator==(uint64_t V1, const APInt &V2)
Value * GetValueAtEndOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live at the end of the specified block...
bool operator!=(const PHI_iterator &x) const
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
static Value * GetPHIValue(PHINode *PHI)
GetPHIValue - For the specified PHI instruction, return the value that it defines.
static PHINode * ValueIsNewPHI(Value *Val, SSAUpdater *Updater)
ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source operands, i...
const BasicBlock * getParent() const
bool empty() const
empty - Check if the string is empty.
PHI_iterator & operator++()