llvm.org GIT mirror llvm / 399b4d0
Rename WeakVH to WeakTrackingVH; NFC This relands r301424. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301812 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 3 years ago
40 changed file(s) with 204 addition(s) and 210 deletion(s). Raw diff Collapse all Expand all
120120 AliasSet *Forward;
121121
122122 /// All instructions without a specific address in this alias set.
123 /// In rare cases this vector can have a null'ed out WeakVH
123 /// In rare cases this vector can have a null'ed out WeakTrackingVH
124124 /// instances (can happen if some other loop pass deletes an
125125 /// instruction in this list).
126 std::vectorVH> UnknownInsts;
126 std::vectorTrackingVH> UnknownInsts;
127127
128128 /// Number of nodes pointing to this AliasSet plus the number of AliasSets
129129 /// forwarding to it.
4242
4343 /// \brief Vector of weak value handles to calls of the @llvm.assume
4444 /// intrinsic.
45 SmallVectorVH, 4> AssumeHandles;
45 SmallVectorTrackingVH, 4> AssumeHandles;
4646
4747 class AffectedValueCallbackVH final : public CallbackVH {
4848 AssumptionCache *AC;
6161 /// \brief A map of values about which an assumption might be providing
6262 /// information to the relevant set of assumptions.
6363 using AffectedValuesMap =
64 DenseMap,
65 AffectedValueCallbackVH::DMI>;
64 DenseMap,
65 AffectedValueCallbackVH::DMI>;
6666 AffectedValuesMap AffectedValues;
6767
6868 /// Get the vector of assumptions which affect a value from the cache.
69 SmallVectorVH, 1> &getOrInsertAffectedValues(Value *V);
69 SmallVectorTrackingVH, 1> &getOrInsertAffectedValues(Value *V);
7070
7171 /// Copy affected values in the cache for OV to be affected values for NV.
7272 void copyAffectedValuesInCache(Value *OV, Value *NV);
119119 /// FIXME: We should replace this with pointee_iterator>
120120 /// when we can write that to filter out the null values. Then caller code
121121 /// will become simpler.
122 MutableArrayRefVH> assumptions() {
122 MutableArrayRefTrackingVH> assumptions() {
123123 if (!Scanned)
124124 scanFunction();
125125 return AssumeHandles;
126126 }
127127
128128 /// \brief Access the list of assumptions which affect this value.
129 MutableArrayRefVH> assumptionsFor(const Value *V) {
129 MutableArrayRefTrackingVH> assumptionsFor(const Value *V) {
130130 if (!Scanned)
131131 scanFunction();
132132
133133 auto AVI = AffectedValues.find_as(const_cast(V));
134134 if (AVI == AffectedValues.end())
135 return MutableArrayRefVH>();
135 return MutableArrayRefTrackingVH>();
136136
137137 return AVI->second;
138138 }
645645 LazyCallGraph::SCC *C = &InitialC;
646646
647647 // Collect value handles for all of the indirect call sites.
648 SmallVectorVH, 8> CallHandles;
648 SmallVectorTrackingVH, 8> CallHandles;
649649
650650 // Struct to track the counts of direct and indirect calls in each function
651651 // of the SCC.
657657 // Put value handles on all of the indirect calls and return the number of
658658 // direct calls for each function in the SCC.
659659 auto ScanSCC = [](LazyCallGraph::SCC &C,
660 SmallVectorImplVH> &CallHandles) {
660 SmallVectorImplTrackingVH> &CallHandles) {
661661 assert(CallHandles.empty() && "Must start with a clear set of handles.");
662662
663663 SmallVector CallCounts;
670670 ++Count.Direct;
671671 } else {
672672 ++Count.Indirect;
673 CallHandles.push_back(WeakVH(&I));
673 CallHandles.push_back(WeakTrackingVH(&I));
674674 }
675675 }
676676 }
698698 "Cannot have changed the size of the SCC!");
699699
700700 // Check whether any of the handles were devirtualized.
701 auto IsDevirtualizedHandle = [&](WeakVH &CallH) {
701 auto IsDevirtualizedHandle = [&](WeakTrackingVH &CallH) {
702702 if (!CallH)
703703 return false;
704704 auto CS = CallSite(CallH);
171171 public:
172172 /// \brief A pair of the calling instruction (a call or invoke)
173173 /// and the call graph node being called.
174 typedef std::pairVH, CallGraphNode *> CallRecord;
174 typedef std::pairTrackingVH, CallGraphNode *> CallRecord;
175175
176176 public:
177177 typedef std::vector CalledFunctionsVector;
7979
8080 /// OperandValToReplace - The Value of the operand in the user instruction
8181 /// that this IVStrideUse is representing.
82 WeakVH OperandValToReplace;
82 WeakTrackingVH OperandValToReplace;
8383
8484 /// PostIncLoops - The set of loops for which Expr has been adjusted to
8585 /// use post-inc mode. This corresponds with SCEVExpander's post-inc concept.
234234 : public InstVisitor {
235235
236236 typedef IRBuilder BuilderTy;
237 typedef std::pairVH, WeakVH> WeakEvalType;
237 typedef std::pairTrackingVH, WeakTrackingVH> WeakEvalType;
238238 typedef DenseMap CacheMapTy;
239239 typedef SmallPtrSet PtrSetTy;
240240
188188 /// replace congruent phis with their most canonical representative. Return
189189 /// the number of phis eliminated.
190190 unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
191 SmallVectorImplVH> &DeadInsts,
191 SmallVectorImplTrackingVH> &DeadInsts,
192192 const TargetTransformInfo *TTI = nullptr);
193193
194194 /// Insert code to directly compute the specified SCEV expression into the
3333 ///
3434 /// This is to avoid having a vtable for the light-weight handle pointers. The
3535 /// fully general Callback version does have a vtable.
36 enum HandleBaseKind {
37 Assert,
38 Callback,
39 Weak
40 };
36 enum HandleBaseKind { Assert, Callback, WeakTracking };
4137
4238 ValueHandleBase(const ValueHandleBase &RHS)
4339 : ValueHandleBase(RHS.PrevPair.getInt(), RHS) {}
144140 /// is useful for advisory sorts of information, but should not be used as the
145141 /// key of a map (since the map would have to rearrange itself when the pointer
146142 /// changes).
147 class WeakVH : public ValueHandleBase {
148 public:
149 WeakVH() : ValueHandleBase(Weak) {}
150 WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
151 WeakVH(const WeakVH &RHS)
152 : ValueHandleBase(Weak, RHS) {}
153
154 WeakVH &operator=(const WeakVH &RHS) = default;
143 class WeakTrackingVH : public ValueHandleBase {
144 public:
145 WeakTrackingVH() : ValueHandleBase(WeakTracking) {}
146 WeakTrackingVH(Value *P) : ValueHandleBase(WeakTracking, P) {}
147 WeakTrackingVH(const WeakTrackingVH &RHS)
148 : ValueHandleBase(WeakTracking, RHS) {}
149
150 WeakTrackingVH &operator=(const WeakTrackingVH &RHS) = default;
155151
156152 Value *operator=(Value *RHS) {
157153 return ValueHandleBase::operator=(RHS);
169165 }
170166 };
171167
172 // Specialize simplify_type to allow WeakVH to participate in
168 // Specialize simplify_type to allow WeakTrackingVH to participate in
173169 // dyn_cast, isa, etc.
174 template <> struct simplify_typeVH> {
170 template <> struct simplify_typeTrackingVH> {
175171 typedef Value *SimpleType;
176 static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
177 };
178 template <> struct simplify_type {
172 static SimpleType getSimplifiedValue(WeakTrackingVH &WVH) { return WVH; }
173 };
174 template <> struct simplify_type {
179175 typedef Value *SimpleType;
180 static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
176 static SimpleType getSimplifiedValue(const WeakTrackingVH &WVH) {
177 return WVH;
178 }
181179 };
182180
183181 /// \brief Value handle that asserts if the Value is deleted.
293291 /// Assigning a value to a TrackingVH is always allowed, even if said TrackingVH
294292 /// no longer points to a valid value.
295293 template class TrackingVH {
296 WeakVH InnerHandle;
294 WeakTrackingVH InnerHandle;
297295
298296 public:
299297 ValueTy *getValPtr() const {
367365 ///
368366 /// Called when this->getValPtr() is destroyed, inside ~Value(), so you
369367 /// may call any non-virtual Value method on getValPtr(), but no subclass
370 /// methods. If WeakVH were implemented as a CallbackVH, it would use this
368 /// methods. If WeakTrackingVH were implemented as a CallbackVH, it would use
369 /// this
371370 /// method to call setValPtr(NULL). AssertingVH would use this method to
372371 /// cause an assertion failure.
373372 ///
378377 /// \brief Callback for Value RAUW.
379378 ///
380379 /// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
381 /// _before_ any of the uses have actually been replaced. If WeakVH were
380 /// _before_ any of the uses have actually been replaced. If WeakTrackingVH
381 /// were
382382 /// implemented as a CallbackVH, it would use this method to call
383383 /// setValPtr(new_value). AssertingVH would do nothing in this method.
384384 virtual void allUsesReplacedWith(Value *) {}
166166 // foo(a + b);
167167 // if (p2)
168168 // bar(a + b);
169 DenseMapVH, 2>> SeenExprs;
169 DenseMapTrackingVH, 2>> SeenExprs;
170170 };
171171 } // namespace llvm
172172
7373 /// All cloned call sites that have operand bundles attached are appended to
7474 /// this vector. This vector may contain nulls or undefs if some of the
7575 /// originally inserted callsites were DCE'ed after they were cloned.
76 std::vectorVH> OperandBundleCallSites;
76 std::vectorTrackingVH> OperandBundleCallSites;
7777
7878 ClonedCodeInfo() = default;
7979 };
191191
192192 /// InlinedCalls - InlineFunction fills this in with callsites that were
193193 /// inlined from the callee. This is only filled in if CG is non-null.
194 SmallVectorVH, 8> InlinedCalls;
194 SmallVectorTrackingVH, 8> InlinedCalls;
195195
196196 /// All of the new call sites inlined into the caller.
197197 ///
4545 /// simplifyUsersOfIV - Simplify instructions that use this induction variable
4646 /// by using ScalarEvolution to analyze the IV's recurrence.
4747 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
48 LoopInfo *LI, SmallVectorImplVH> &Dead,
48 LoopInfo *LI, SmallVectorImplTrackingVH> &Dead,
4949 IVVisitor *V = nullptr);
5050
5151 /// SimplifyLoopIVs - Simplify users of induction variables within this
5252 /// loop. This does not actually change or add IVs.
5353 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
54 LoopInfo *LI, SmallVectorImplVH> &Dead);
54 LoopInfo *LI, SmallVectorImplTrackingVH> &Dead);
5555
5656 } // end namespace llvm
5757
2222
2323 class Value;
2424 class Instruction;
25 typedef ValueMapVH> ValueToValueMapTy;
25 typedef ValueMapTrackingVH> ValueToValueMapTy;
2626
2727 /// This is a class that can be implemented by clients to remap types when
2828 /// cloning constants and instructions.
3939 struct SLPVectorizerPass : public PassInfoMixin {
4040 typedef SmallVector StoreList;
4141 typedef MapVector StoreListMap;
42 typedef SmallVector WeakVHList;
43 typedef MapVector WeakVHListMap;
42 typedef SmallVector WeakTrackingVHList;
43 typedef MapVector WeakTrackingVHListMap;
4444
4545 ScalarEvolution *SE = nullptr;
4646 TargetTransformInfo *TTI = nullptr;
110110 StoreListMap Stores;
111111
112112 /// The getelementptr instructions in a basic block organized by base pointer.
113 WeakVHListMap GEPs;
113 WeakTrackingVHListMap GEPs;
114114 };
115115 }
116116
2828 cl::desc("Enable verification of assumption cache"),
2929 cl::init(false));
3030
31 SmallVectorVH, 1> &AssumptionCache::getOrInsertAffectedValues(Value *V) {
31 SmallVectorTrackingVH, 1> &
32 AssumptionCache::getOrInsertAffectedValues(Value *V) {
3233 // Try using find_as first to avoid creating extra value handles just for the
3334 // purpose of doing the lookup.
3435 auto AVI = AffectedValues.find_as(V);
3536 if (AVI != AffectedValues.end())
3637 return AVI->second;
3738
38 auto AVIP = AffectedValues.insert({
39 AffectedValueCallbackVH(V, this), SmallVector()});
39 auto AVIP = AffectedValues.insert(
40 {AffectedValueCallbackVH(V, this), SmallVector()});
4041 return AVIP.first->second;
4142 }
4243
203203 // Get the set of call sites currently in the function.
204204 for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
205205 // If this call site is null, then the function pass deleted the call
206 // entirely and the WeakVH nulled it out.
206 // entirely and the WeakTrackingVH nulled it out.
207207 if (!I->first ||
208208 // If we've already seen this call site, then the FunctionPass RAUW'd
209209 // one call with another, which resulted in two "uses" in the edge
346346 DevirtualizedCall = true;
347347
348348 // After scanning this function, if we still have entries in callsites, then
349 // they are dangling pointers. WeakVH should save us for this, so abort if
349 // they are dangling pointers. WeakTrackingVH should save us for this, so
350 // abort if
350351 // this happens.
351352 assert(CallSites.empty() && "Dangling pointers found in call sites map");
352353
17711771 ///
17721772 /// This does not depend on any SCEVExpander state but should be used in
17731773 /// the same context that SCEVExpander is used.
1774 unsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1775 SmallVectorImpl &DeadInsts,
1776 const TargetTransformInfo *TTI) {
1774 unsigned
1775 SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1776 SmallVectorImpl &DeadInsts,
1777 const TargetTransformInfo *TTI) {
17771778 // Find integer phis in order of increasing width.
17781779 SmallVector Phis;
17791780 for (auto &I : *L->getHeader()) {
5757 if (Idx >= size())
5858 resize(Idx + 1);
5959
60 WeakVH &OldV = ValuePtrs[Idx];
60 WeakTrackingVH &OldV = ValuePtrs[Idx];
6161 if (!OldV) {
6262 OldV = V;
6363 return;
1919 class Constant;
2020
2121 class BitcodeReaderValueList {
22 std::vectorVH> ValuePtrs;
22 std::vectorTrackingVH> ValuePtrs;
2323
2424 /// As we resolve forward-referenced constants, we add information about them
2525 /// to this vector. This allows us to resolve them in bulk instead of
22252225 ConstantInt *RetVal =
22262226 lowerObjectSizeCall(II, *DL, TLInfo, /*MustSucceed=*/true);
22272227 // Substituting this can cause recursive simplifications, which can
2228 // invalidate our iterator. Use a WeakVH to hold onto it in case this
2228 // invalidate our iterator. Use a WeakTrackingVH to hold onto it in case
2229 // this
22292230 // happens.
22302231 Value *CurValue = &*CurInstIterator;
2231 WeakVH IterHandle(CurValue);
2232 WeakTrackingVH IterHandle(CurValue);
22322233
22332234 replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr);
22342235
44414442 // using it.
44424443 if (Repl->use_empty()) {
44434444 // This can cause recursive deletion, which can invalidate our iterator.
4444 // Use a WeakVH to hold onto it in case this happens.
4445 // Use a WeakTrackingVH to hold onto it in case this happens.
44454446 Value *CurValue = &*CurInstIterator;
4446 WeakVH IterHandle(CurValue);
4447 WeakTrackingVH IterHandle(CurValue);
44474448 BasicBlock *BB = CurInstIterator->getParent();
44484449
44494450 RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo);
819819 switch (Entry->getKind()) {
820820 case Assert:
821821 break;
822 case Weak:
823 // Weak just goes to null, which will unlink it from the list.
822 case WeakTracking:
823 // WeakTracking just goes to null, which will unlink it from the list.
824824 Entry->operator=(nullptr);
825825 break;
826826 case Callback:
870870 case Assert:
871871 // Asserting handle does not follow RAUW implicitly.
872872 break;
873 case Weak:
873 case WeakTracking:
874874 // Weak goes to the new value, which will unlink it from Old's list.
875875 Entry->operator=(New);
876876 break;
887887 if (Old->HasValueHandle)
888888 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
889889 switch (Entry->getKind()) {
890 case Weak:
890 case WeakTracking:
891891 dbgs() << "After RAUW from " << *Old->getType() << " %"
892892 << Old->getName() << " to " << *New->getType() << " %"
893893 << New->getName() << "\n";
894894 llvm_unreachable(
895 "A weak value handle still pointed to the old value!\n");
895 "A weak tracking value handle still pointed to the old value!\n");
896896 default:
897897 break;
898898 }
7676
7777 void insertElse(BranchInst *Term);
7878
79 Value *handleLoopCondition(Value *Cond, PHINode *Broken,
80 llvm::Loop *L, BranchInst *Term,
81 SmallVectorImpl &LoopPhiConditions);
79 Value *
80 handleLoopCondition(Value *Cond, PHINode *Broken, llvm::Loop *L,
81 BranchInst *Term,
82 SmallVectorImpl &LoopPhiConditions);
8283
8384 void handleLoop(BranchInst *Term);
8485
211212
212213 /// \brief Recursively handle the condition leading to a loop
213214 Value *SIAnnotateControlFlow::handleLoopCondition(
214 Value *Cond, PHINode *Broken,
215 llvm::Loop *L, BranchInst *Term,
216 SmallVectorImpl &LoopPhiConditions) {
215 Value *Cond, PHINode *Broken, llvm::Loop *L, BranchInst *Term,
216 SmallVectorImpl &LoopPhiConditions) {
217217
218218 // Only search through PHI nodes which are inside the loop. If we try this
219219 // with PHI nodes that are outside of the loop, we end up inserting new PHI
280280 NewPhi->setIncomingValue(i, PhiArg);
281281 }
282282
283 LoopPhiConditions.push_back(WeakVH(Phi));
283 LoopPhiConditions.push_back(WeakTrackingVH(Phi));
284284 return Ret;
285285 }
286286
322322 BasicBlock *Target = Term->getSuccessor(1);
323323 PHINode *Broken = PHINode::Create(Int64, 0, "phi.broken", &Target->front());
324324
325 SmallVectorVH, 8> LoopPhiConditions;
325 SmallVectorTrackingVH, 8> LoopPhiConditions;
326326 Value *Cond = Term->getCondition();
327327 Term->setCondition(BoolTrue);
328328 Value *Arg = handleLoopCondition(Cond, Broken, L, Term, LoopPhiConditions);
332332
333333 Term->setCondition(CallInst::Create(Loop, Arg, "", Term));
334334
335 for (WeakVH Val : reverse(LoopPhiConditions)) {
335 for (WeakTrackingVH Val : reverse(LoopPhiConditions)) {
336336 if (PHINode *Cond = cast_or_null(Val))
337337 eraseIfUnused(Cond);
338338 }
127127
128128 static bool replaceConstantExprOp(ConstantExpr *CE, Pass *P) {
129129 do {
130 SmallVectorVH,8> WUsers(CE->user_begin(), CE->user_end());
130 SmallVectorTrackingVH, 8> WUsers(CE->user_begin(), CE->user_end());
131131 std::sort(WUsers.begin(), WUsers.end());
132132 WUsers.erase(std::unique(WUsers.begin(), WUsers.end()), WUsers.end());
133133 while (!WUsers.empty())
134 if (WeakVH WU = WUsers.pop_back_val()) {
134 if (WeakTrackingVH WU = WUsers.pop_back_val()) {
135135 if (PHINode *PN = dyn_cast(WU)) {
136136 for (int I = 0, E = PN->getNumIncomingValues(); I < E; ++I)
137137 if (PN->getIncomingValue(I) == CE) {
158158 }
159159
160160 static bool rewriteNonInstructionUses(GlobalVariable *GV, Pass *P) {
161 SmallVectorVH,8> WUsers;
161 SmallVectorTrackingVH, 8> WUsers;
162162 for (User *U : GV->users())
163163 if (!isa(U))
164 WUsers.push_back(WeakVH(U));
164 WUsers.push_back(WeakTrackingVH(U));
165165 while (!WUsers.empty())
166 if (WeakVH WU = WUsers.pop_back_val()) {
166 if (WeakTrackingVH WU = WUsers.pop_back_val()) {
167167 ConstantExpr *CE = dyn_cast(WU);
168168 if (!CE || !replaceConstantExprOp(CE, P))
169169 return false;
238238 // we delete a constant array, we may also be holding pointer to one of its
239239 // elements (or an element of one of its elements if we're dealing with an
240240 // array of arrays) in the worklist.
241 SmallVectorVH, 8> WorkList(V->user_begin(), V->user_end());
241 SmallVectorTrackingVH, 8> WorkList(V->user_begin(), V->user_end());
242242 while (!WorkList.empty()) {
243243 Value *UV = WorkList.pop_back_val();
244244 if (!UV)
206206
207207 /// A work queue of functions that may have been modified and should be
208208 /// analyzed again.
209 std::vectorVH> Deferred;
209 std::vectorTrackingVH> Deferred;
210210
211211 /// Checks the rules of order relation introduced among functions set.
212212 /// Returns true, if sanity check has been passed, and false if failed.
213213 #ifndef NDEBUG
214 bool doSanityCheck(std::vectorVH> &Worklist);
214 bool doSanityCheck(std::vectorTrackingVH> &Worklist);
215215 #endif
216216
217217 /// Insert a ComparableFunction into the FnTree, or merge it away if it's
285285 }
286286
287287 #ifndef NDEBUG
288 bool MergeFunctions::doSanityCheck(std::vectorVH> &Worklist) {
288 bool MergeFunctions::doSanityCheck(std::vectorTrackingVH> &Worklist) {
289289 if (const unsigned Max = NumFunctionsForSanityCheck) {
290290 unsigned TripleNumber = 0;
291291 bool Valid = true;
293293 dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n";
294294
295295 unsigned i = 0;
296 for (std::vectorVH>::iterator I = Worklist.begin(), E = Worklist.end();
296 for (std::vectorTrackingVH>::iterator I = Worklist.begin(),
297 E = Worklist.end();
297298 I != E && i < Max; ++I, ++i) {
298299 unsigned j = i;
299 for (std::vectorVH>::iterator J = I; J != E && j < Max; ++J, ++j) {
300 for (std::vectorTrackingVH>::iterator J = I; J != E && j < Max;
301 ++J, ++j) {
300302 Function *F1 = cast(*I);
301303 Function *F2 = cast(*J);
302304 int Res1 = FunctionComparator(F1, F2, &GlobalNumbers).compare();
314316 continue;
315317
316318 unsigned k = j;
317 for (std::vectorVH>::iterator K = J; K != E && k < Max;
319 for (std::vectorTrackingVH>::iterator K = J; K != E && k < Max;
318320 ++k, ++K, ++TripleNumber) {
319321 if (K == J)
320322 continue;
384386 // consider merging it. Otherwise it is dropped and never considered again.
385387 if ((I != S && std::prev(I)->first == I->first) ||
386388 (std::next(I) != IE && std::next(I)->first == I->first) ) {
387 Deferred.push_back(WeakVH(I->second));
389 Deferred.push_back(WeakTrackingVH(I->second));
388390 }
389391 }
390392
391393 do {
392 std::vectorVH> Worklist;
394 std::vectorTrackingVH> Worklist;
393395 Deferred.swap(Worklist);
394396
395397 DEBUG(doSanityCheck(Worklist));
398400 DEBUG(dbgs() << "size of worklist: " << Worklist.size() << '\n');
399401
400402 // Insert functions and merge them.
401 for (WeakVH &I : Worklist) {
403 for (WeakTrackingVH &I : Worklist) {
402404 if (!I)
403405 continue;
404406 Function *F = cast(I);
19471947 return isAllocLikeFn(V, TLI) && V != AI;
19481948 }
19491949
1950 static bool
1951 isAllocSiteRemovable(Instruction *AI, SmallVectorImpl &Users,
1952 const TargetLibraryInfo *TLI) {
1950 static bool isAllocSiteRemovable(Instruction *AI,
1951 SmallVectorImpl &Users,
1952 const TargetLibraryInfo *TLI) {
19531953 SmallVector Worklist;
19541954 Worklist.push_back(AI);
19551955
20332033 // If we have a malloc call which is only used in any amount of comparisons
20342034 // to null and free calls, delete the calls and replace the comparisons with
20352035 // true or false as appropriate.
2036 SmallVectorVH, 64> Users;
2036 SmallVectorTrackingVH, 64> Users;
20372037 if (isAllocSiteRemovable(&MI, Users, &TLI)) {
20382038 for (unsigned i = 0, e = Users.size(); i != e; ++i) {
20392039 // Lowering all @llvm.objectsize calls first because they may
9696 TargetLibraryInfo *TLI;
9797 const TargetTransformInfo *TTI;
9898
99 SmallVectorVH, 16> DeadInsts;
99 SmallVectorTrackingVH, 16> DeadInsts;
100100 bool Changed = false;
101101
102102 bool isValidRewrite(Value *FromVal, Value *ToVal);
414414 Compare->getName());
415415
416416 // In the following deletions, PN may become dead and may be deleted.
417 // Use a WeakVH to observe whether this happens.
418 WeakVH WeakPH = PN;
417 // Use a WeakTrackingVH to observe whether this happens.
418 WeakTrackingVH WeakPH = PN;
419419
420420 // Delete the old floating point exit comparison. The branch starts using the
421421 // new comparison.
450450 //
451451 BasicBlock *Header = L->getHeader();
452452
453 SmallVectorVH, 8> PHIs;
453 SmallVectorTrackingVH, 8> PHIs;
454454 for (BasicBlock::iterator I = Header->begin();
455455 PHINode *PN = dyn_cast(I); ++I)
456456 PHIs.push_back(PN);
900900 PHINode *WidePhi;
901901 Instruction *WideInc;
902902 const SCEV *WideIncExpr;
903 SmallVectorImplVH> &DeadInsts;
903 SmallVectorImplTrackingVH> &DeadInsts;
904904
905905 SmallPtrSet Widened;
906906 SmallVector NarrowIVUsers;
940940 }
941941
942942 public:
943 WidenIV(const WideIVInfo &WI, LoopInfo *LInfo,
944 ScalarEvolution *SEv, DominatorTree *DTree,
945 SmallVectorImpl &DI, bool HasGuards) :
946 OrigPhi(WI.NarrowIV),
947 WideType(WI.WidestNativeType),
948 LI(LInfo),
949 L(LI->getLoopFor(OrigPhi->getParent())),
950 SE(SEv),
951 DT(DTree),
952 HasGuards(HasGuards),
953 WidePhi(nullptr),
954 WideInc(nullptr),
955 WideIncExpr(nullptr),
956 DeadInsts(DI) {
943 WidenIV(const WideIVInfo &WI, LoopInfo *LInfo, ScalarEvolution *SEv,
944 DominatorTree *DTree, SmallVectorImpl &DI,
945 bool HasGuards)
946 : OrigPhi(WI.NarrowIV), WideType(WI.WidestNativeType), LI(LInfo),
947 L(LI->getLoopFor(OrigPhi->getParent())), SE(SEv), DT(DTree),
948 HasGuards(HasGuards), WidePhi(nullptr), WideInc(nullptr),
949 WideIncExpr(nullptr), DeadInsts(DI) {
957950 assert(L->getHeader() == OrigPhi->getParent() && "Phi must be an IV");
958951 ExtendKindMap[OrigPhi] = WI.IsSigned ? SignExtended : ZeroExtended;
959952 }
137137
138138 // Tries to infer the specific address space of each address expression in
139139 // Postorder.
140 void inferAddressSpaces(ArrayRefVH> Postorder,
140 void inferAddressSpaces(ArrayRefTrackingVH> Postorder,
141141 ValueToAddrSpaceMapTy *InferredAddrSpace) const;
142142
143143 bool isSafeToCastConstAddrSpace(Constant *C, unsigned NewAS) const;
146146 // address spaces if InferredAddrSpace says so. Postorder is the postorder of
147147 // all flat expressions in the use-def graph of function F.
148148 bool
149 rewriteWithNewAddressSpaces(ArrayRefVH> Postorder,
149 rewriteWithNewAddressSpaces(ArrayRefTrackingVH> Postorder,
150150 const ValueToAddrSpaceMapTy &InferredAddrSpace,
151151 Function *F) const;
152152
161161 std::vector> &PostorderStack,
162162 DenseSet &Visited) const;
163163
164 std::vectorVH> collectFlatAddressExpressions(Function &F) const;
164 std::vectorTrackingVH> collectFlatAddressExpressions(Function &F) const;
165165
166166 Value *cloneValueWithNewAddressSpace(
167167 Value *V, unsigned NewAddrSpace,
302302
303303 // Returns all flat address expressions in function F. The elements are ordered
304304 // ordered in postorder.
305 std::vectorVH>
305 std::vectorTrackingVH>
306306 InferAddressSpaces::collectFlatAddressExpressions(Function &F) const {
307307 // This function implements a non-recursive postorder traversal of a partial
308308 // use-def graph of function F.
351351 }
352352 }
353353
354 std::vectorVH> Postorder; // The resultant postorder.
354 std::vectorTrackingVH> Postorder; // The resultant postorder.
355355 while (!PostorderStack.empty()) {
356356 Value *TopVal = PostorderStack.back().first;
357357 // If the operands of the expression on the top are already explored,
582582 return false;
583583
584584 // Collects all flat address expressions in postorder.
585 std::vectorVH> Postorder = collectFlatAddressExpressions(F);
585 std::vectorTrackingVH> Postorder = collectFlatAddressExpressions(F);
586586
587587 // Runs a data-flow analysis to refine the address spaces of every expression
588588 // in Postorder.
595595 }
596596
597597 // Constants need to be tracked through RAUW to handle cases with nested
598 // constant expressions, so wrap values in WeakVH.
598 // constant expressions, so wrap values in WeakTrackingVH.
599599 void InferAddressSpaces::inferAddressSpaces(
600 ArrayRefVH> Postorder,
600 ArrayRefTrackingVH> Postorder,
601601 ValueToAddrSpaceMapTy *InferredAddrSpace) const {
602602 SetVector Worklist(Postorder.begin(), Postorder.end());
603603 // Initially, all expressions are in the uninitialized address space.
809809 }
810810
811811 bool InferAddressSpaces::rewriteWithNewAddressSpaces(
812 ArrayRef Postorder,
813 const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const {
812 ArrayRef Postorder,
813 const ValueToAddrSpaceMapTy &InferredAddrSpace, Function *F) const {
814814 // For each address expression to be modified, creates a clone of it with its
815815 // pointer operands converted to the new address space. Since the pointer
816816 // operands are converted, the clone is naturally in the new address space by
840840 SmallVector DeadInstructions;
841841
842842 // Replaces the uses of the old address expressions with the new ones.
843 for (const WeakVH &WVH : Postorder) {
843 for (const WeakTrackingVH &WVH : Postorder) {
844844 assert(WVH && "value was unexpectedly deleted");
845845 Value *V = WVH;
846846 Value *NewV = ValueWithNewAddrSpace.lookup(V);
498498 Instruction *Inst = &*I++;
499499 // Look for memset instructions, which may be optimized to a larger memset.
500500 if (MemSetInst *MSI = dyn_cast(Inst)) {
501 WeakVH InstPtr(&*I);
501 WeakTrackingVH InstPtr(&*I);
502502 if (!processLoopMemSet(MSI, BECount))
503503 continue;
504504 MadeChange = true;
3939 bool Changed = false;
4040 // Copy blocks into a temporary array to avoid iterator invalidation issues
4141 // as we remove them.
42 SmallVectorVH, 16> Blocks(L.blocks());
42 SmallVectorTrackingVH, 16> Blocks(L.blocks());
4343
4444 for (auto &Block : Blocks) {
4545 // Attempt to merge blocks in the trivial case. Don't modify blocks which
899899 /// If any of the instructions is the specified set are trivially dead, delete
900900 /// them and see if this makes any of their operands subsequently dead.
901901 static bool
902 DeleteTriviallyDeadInstructions(SmallVectorImplVH> &DeadInsts) {
902 DeleteTriviallyDeadInstructions(SmallVectorImplTrackingVH> &DeadInsts) {
903903 bool Changed = false;
904904
905905 while (!DeadInsts.empty()) {
18441844 void FinalizeChain(IVChain &Chain);
18451845 void CollectChains();
18461846 void GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
1847 SmallVectorImplVH> &DeadInsts);
1847 SmallVectorImplTrackingVH> &DeadInsts);
18481848
18491849 void CollectInterestingTypesAndFactors();
18501850 void CollectFixupsAndInitialFormulae();
19191919 const LSRUse &LU,
19201920 SCEVExpander &Rewriter) const;
19211921
1922 Value *Expand(const LSRUse &LU, const LSRFixup &LF,
1923 const Formula &F,
1924 BasicBlock::iterator IP,
1925 SCEVExpander &Rewriter,
1926 SmallVectorImpl &DeadInsts) const;
1922 Value *Expand(const LSRUse &LU, const LSRFixup &LF, const Formula &F,
1923 BasicBlock::iterator IP, SCEVExpander &Rewriter,
1924 SmallVectorImpl &DeadInsts) const;
19271925 void RewriteForPHI(PHINode *PN, const LSRUse &LU, const LSRFixup &LF,
1928 const Formula &F,
1929 SCEVExpander &Rewriter,
1930 SmallVectorImpl &DeadInsts) const;
1931 void Rewrite(const LSRUse &LU, const LSRFixup &LF,
1932 const Formula &F,
1926 const Formula &F, SCEVExpander &Rewriter,
1927 SmallVectorImpl &DeadInsts) const;
1928 void Rewrite(const LSRUse &LU, const LSRFixup &LF, const Formula &F,
19331929 SCEVExpander &Rewriter,
1934 SmallVectorImplVH> &DeadInsts) const;
1930 SmallVectorImplTrackingVH> &DeadInsts) const;
19351931 void ImplementSolution(const SmallVectorImpl &Solution);
19361932
19371933 public:
30133009 /// Generate an add or subtract for each IVInc in a chain to materialize the IV
30143010 /// user's operand from the previous IV user's operand.
30153011 void LSRInstance::GenerateIVChain(const IVChain &Chain, SCEVExpander &Rewriter,
3016 SmallVectorImplVH> &DeadInsts) {
3012 SmallVectorImplTrackingVH> &DeadInsts) {
30173013 // Find the new IVOperand for the head of the chain. It may have been replaced
30183014 // by LSR.
30193015 const IVInc &Head = Chain.Incs[0];
47584754
47594755 /// Emit instructions for the leading candidate expression for this LSRUse (this
47604756 /// is called "expanding").
4761 Value *LSRInstance::Expand(const LSRUse &LU,
4762 const LSRFixup &LF,
4763 const Formula &F,
4764 BasicBlock::iterator IP,
4757 Value *LSRInstance::Expand(const LSRUse &LU, const LSRFixup &LF,
4758 const Formula &F, BasicBlock::iterator IP,
47654759 SCEVExpander &Rewriter,
4766 SmallVectorImplVH> &DeadInsts) const {
4760 SmallVectorImplTrackingVH> &DeadInsts) const {
47674761 if (LU.RigidFormula)
47684762 return LF.OperandValToReplace;
47694763
49384932 /// Helper for Rewrite. PHI nodes are special because the use of their operands
49394933 /// effectively happens in their predecessor blocks, so the expression may need
49404934 /// to be expanded in multiple places.
4941 void LSRInstance::RewriteForPHI(PHINode *PN,
4942 const LSRUse &LU,
4943 const LSRFixup &LF,
4944 const Formula &F,
4945 SCEVExpander &Rewriter,
4946 SmallVectorImpl &DeadInsts) const {
4935 void LSRInstance::RewriteForPHI(
4936 PHINode *PN, const LSRUse &LU, const LSRFixup &LF, const Formula &F,
4937 SCEVExpander &Rewriter, SmallVectorImpl &DeadInsts) const {
49474938 DenseMap Inserted;
49484939 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
49494940 if (PN->getIncomingValue(i) == LF.OperandValToReplace) {
50155006 /// Emit instructions for the leading candidate expression for this LSRUse (this
50165007 /// is called "expanding"), and update the UserInst to reference the newly
50175008 /// expanded value.
5018 void LSRInstance::Rewrite(const LSRUse &LU,
5019 const LSRFixup &LF,
5020 const Formula &F,
5021 SCEVExpander &Rewriter,
5022 SmallVectorImpl &DeadInsts) const {
5009 void LSRInstance::Rewrite(const LSRUse &LU, const LSRFixup &LF,
5010 const Formula &F, SCEVExpander &Rewriter,
5011 SmallVectorImpl &DeadInsts) const {
50235012 // First, find an insertion point that dominates UserInst. For PHI nodes,
50245013 // find the nearest block which dominates all the relevant uses.
50255014 if (PHINode *PN = dyn_cast(LF.UserInst)) {
50575046 const SmallVectorImpl &Solution) {
50585047 // Keep track of instructions we may have made dead, so that
50595048 // we can remove them after we are done working.
5060 SmallVectorVH, 16> DeadInsts;
5049 SmallVectorTrackingVH, 16> DeadInsts;
50615050
50625051 SCEVExpander Rewriter(SE, L->getHeader()->getModule()->getDataLayout(),
50635052 "lsr");
53075296 // Remove any extra phis created by processing inner loops.
53085297 Changed |= DeleteDeadPHIs(L->getHeader());
53095298 if (EnablePhiElim && L->isLoopSimplifyForm()) {
5310 SmallVectorVH, 16> DeadInsts;
5299 SmallVectorTrackingVH, 16> DeadInsts;
53115300 const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
53125301 SCEVExpander Rewriter(SE, DL, "lsr");
53135302 #ifndef NDEBUG
12301230 LoopProcessWorklist.push_back(NewLoop);
12311231 redoLoop = true;
12321232
1233 // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody
1233 // Keep a WeakTrackingVH holding onto LIC. If the first call to
1234 // RewriteLoopBody
12341235 // deletes the instruction (for example by simplifying a PHI that feeds into
12351236 // the condition that we're unswitching on), we don't rewrite the second
12361237 // iteration.
1237 WeakVH LICHandle(LIC);
1238 WeakTrackingVH LICHandle(LIC);
12381239
12391240 // Now we rewrite the original code to know that the condition is true and the
12401241 // new code to know that the condition is false.
210210 Changed = true;
211211 SE->forgetValue(&*I);
212212 I->replaceAllUsesWith(NewI);
213 // If SeenExprs constains I's WeakVH, that entry will be replaced with
213 // If SeenExprs constains I's WeakTrackingVH, that entry will be
214 // replaced with
214215 // nullptr.
215216 RecursivelyDeleteTriviallyDeadInstructions(&*I, TLI);
216217 I = NewI->getIterator();
218219 // Add the rewritten instruction to SeenExprs; the original instruction
219220 // is deleted.
220221 const SCEV *NewSCEV = SE->getSCEV(&*I);
221 SeenExprs[NewSCEV].push_back(WeakVH(&*I));
222 SeenExprs[NewSCEV].push_back(WeakTrackingVH(&*I));
222223 // Ideally, NewSCEV should equal OldSCEV because tryReassociate(I)
223224 // is equivalent to I. However, ScalarEvolution::getSCEV may
224225 // weaken nsw causing NewSCEV not to equal OldSCEV. For example, suppose
238239 //
239240 // This improvement is exercised in @reassociate_gep_nsw in nary-gep.ll.
240241 if (NewSCEV != OldSCEV)
241 SeenExprs[OldSCEV].push_back(WeakVH(&*I));
242 SeenExprs[OldSCEV].push_back(WeakTrackingVH(&*I));
242243 }
243244 }
244245 }
493494 // future instruction either. Therefore, we pop it out of the stack. This
494495 // optimization makes the algorithm O(n).
495496 while (!Candidates.empty()) {
496 // Candidates stores WeakVHs, so a candidate can be nullptr if it's removed
497 // Candidates stores WeakTrackingVHs, so a candidate can be nullptr if it's
498 // removed
497499 // during rewriting.
498500 if (Value *Candidate = Candidates.back()) {
499501 Instruction *CandidateInstruction = cast(Candidate);
981981 /// Emit a tree of add instructions, summing Ops together
982982 /// and returning the result. Insert the tree before I.
983983 static Value *EmitAddTreeOfValues(Instruction *I,
984 SmallVectorImplVH> &Ops){
984 SmallVectorImplTrackingVH> &Ops) {
985985 if (Ops.size() == 1) return Ops.back();
986986
987987 Value *V1 = Ops.back();
15581558 ? BinaryOperator::CreateAdd(MaxOccVal, MaxOccVal)
15591559 : BinaryOperator::CreateFAdd(MaxOccVal, MaxOccVal);
15601560
1561 SmallVectorVH, 4> NewMulOps;
1561 SmallVectorTrackingVH, 4> NewMulOps;
15621562 for (unsigned i = 0; i != Ops.size(); ++i) {
15631563 // Only try to remove factors from expressions we're allowed to.
15641564 BinaryOperator *BOp =
7777
7878 bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI) {
7979 // Recursively deleting a PHI may cause multiple PHIs to be deleted
80 // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
81 SmallVector PHIs;
80 // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
81 SmallVector PHIs;
8282 for (BasicBlock::iterator I = BB->begin();
8383 PHINode *PN = dyn_cast(I); ++I)
8484 PHIs.push_back(PN);
244244 void PruningFunctionCloner::CloneBlock(const BasicBlock *BB,
245245 BasicBlock::const_iterator StartingInst,
246246 std::vector &ToClone){
247 WeakVH &BBEntry = VMap[BB];
247 WeakTrackingVH &BBEntry = VMap[BB];
248248
249249 // Have we already cloned this block?
250250 if (BBEntry) return;
546546 // Make a second pass over the PHINodes now that all of them have been
547547 // remapped into the new function, simplifying the PHINode and performing any
548548 // recursive simplifications exposed. This will transparently update the
549 // WeakVH in the VMap. Notably, we rely on that so that if we coalesce
549 // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
550550 // two PHINodes, the iteration over the old PHIs remains valid, and the
551551 // mapping will just map us to the new node (which may not even be a PHI
552552 // node).
561561 // that can be removed.
562562 BB->removePredecessor(Pred, true);
563563
564 WeakVH PhiIt = &BB->front();
564 WeakTrackingVH PhiIt = &BB->front();
565565 while (PHINode *PN = dyn_cast(PhiIt)) {
566566 PhiIt = &*++BasicBlock::iterator(cast(PhiIt));
567567 Value *OldPhiIt = PhiIt;
14751475 II->setAttributes(CI->getAttributes());
14761476
14771477 // Make sure that anything using the call now uses the invoke! This also
1478 // updates the CallGraph if present, because it uses a WeakVH.
1478 // updates the CallGraph if present, because it uses a WeakTrackingVH.
14791479 CI->replaceAllUsesWith(II);
14801480
14811481 // Delete the original call
756756
757757 // Simplify any new induction variables in the partially unrolled loop.
758758 if (SE && !CompletelyUnroll && Count > 1) {
759 SmallVectorVH, 16> DeadInsts;
759 SmallVectorTrackingVH, 16> DeadInsts;
760760 simplifyLoopIVs(L, SE, DT, LI, DeadInsts);
761761
762762 // Aggressively clean up dead instructions that simplifyLoopIVs already
5050 ScalarEvolution *SE;
5151 DominatorTree *DT;
5252
53 SmallVectorImplVH> &DeadInsts;
53 SmallVectorImplTrackingVH> &DeadInsts;
5454
5555 bool Changed;
5656
5757 public:
5858 SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
59 LoopInfo *LI,SmallVectorImplVH> &Dead)
59 LoopInfo *LI, SmallVectorImplVH> &Dead)
6060 : L(Loop), LI(LI), SE(SE), DT(DT), DeadInsts(Dead), Changed(false) {
6161 assert(LI && "IV simplification requires LoopInfo");
6262 }
700700 /// Simplify instructions that use this induction variable
701701 /// by using ScalarEvolution to analyze the IV's recurrence.
702702 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
703 LoopInfo *LI, SmallVectorImplVH> &Dead,
703 LoopInfo *LI, SmallVectorImplTrackingVH> &Dead,
704704 IVVisitor *V) {
705705 SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Dead);
706706 SIV.simplifyUsers(CurrIV, V);
710710 /// Simplify users of induction variables within this
711711 /// loop. This does not actually change or add IVs.
712712 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
713 LoopInfo *LI, SmallVectorImplVH> &Dead) {
713 LoopInfo *LI, SmallVectorImplTrackingVH> &Dead) {
714714 bool Changed = false;
715715 for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ++I) {
716716 Changed |= simplifyUsersOfIV(cast(I), SE, DT, LI, Dead);
38983898 }
38993899
39003900 /// \brief Check that the Values in the slice in VL array are still existent in
3901 /// the WeakVH array.
3901 /// the WeakTrackingVH array.
39023902 /// Vectorization of part of the VL array may cause later values in the VL array
3903 /// to become invalid. We track when this has happened in the WeakVH array.
3904 static bool hasValueBeenRAUWed(ArrayRef VL, ArrayRef VH,
3905 unsigned SliceBegin, unsigned SliceSize) {
3903 /// to become invalid. We track when this has happened in the WeakTrackingVH
3904 /// array.
3905 static bool hasValueBeenRAUWed(ArrayRef VL,
3906 ArrayRef VH, unsigned SliceBegin,
3907 unsigned SliceSize) {
39063908 VL = VL.slice(SliceBegin, SliceSize);
39073909 VH = VH.slice(SliceBegin, SliceSize);
39083910 return !std::equal(VL.begin(), VL.end(), VH.begin());
39203922 return false;
39213923
39223924 // Keep track of values that were deleted by vectorizing in the loop below.
3923 SmallVectorVH, 8> TrackValues(Chain.begin(), Chain.end());
3925 SmallVectorTrackingVH, 8> TrackValues(Chain.begin(), Chain.end());
39243926
39253927 bool Changed = false;
39263928 // Look for profitable vectorizable trees at all offsets, starting at zero.
41064108 bool Changed = false;
41074109
41084110 // Keep track of values that were deleted by vectorizing in the loop below.
4109 SmallVectorVH, 8> TrackValues(VL.begin(), VL.end());
4111 SmallVectorTrackingVH, 8> TrackValues(VL.begin(), VL.end());
41104112
41114113 unsigned NextInst = 0, MaxInst = VL.size();
41124114 for (unsigned VF = MaxVF; NextInst + 1 < MaxInst && VF >= MinVF;
47334735
47344736 namespace {
47354737 /// Tracks instructons and its children.
4736 class WeakVHWithLevel final : public CallbackVH {
4738 class WeakTrackingVHWithLevel final : public CallbackVH {
47374739 /// Operand index of the instruction currently beeing analized.
47384740 unsigned Level = 0;
47394741 /// Is this the instruction that should be vectorized, or are we now
47424744 bool IsInitial = true;
47434745
47444746 public:
4745 explicit WeakVHWithLevel() = default;
4746 WeakVHWithLevel(Value *V) : CallbackVH(V){};
4747 explicit WeakTrackingVHWithLevel() = default;
4748 WeakTrackingVHWithLevel(Value *V) : CallbackVH(V){};
47474749 /// Restart children analysis each time it is repaced by the new instruction.
47484750 void allUsesReplacedWith(Value *New) override {
47494751 setValPtr(New);
47704772 cast(getValPtr())->getNumOperands() > Level);
47714773 return cast(getValPtr())->getOperand(Level++);
47724774 }
4773 virtual ~WeakVHWithLevel() = default;
4775 virtual ~WeakTrackingVHWithLevel() = default;
47744776 };
47754777 } // namespace
47764778
47924794
47934795 if (Root->getParent() != BB)
47944796 return false;
4795 SmallVectorVHWithLevel, 8> Stack(1, Root);
4797 SmallVectorTrackingVHWithLevel, 8> Stack(1, Root);
47964798 SmallSet VisitedInstrs;
47974799 bool Res = false;
47984800 while (!Stack.empty()) {
50685070 SetVector Candidates(GEPList.begin(), GEPList.end());
50695071
50705072 // Some of the candidates may have already been vectorized after we
5071 // initially collected them. If so, the WeakVHs will have nullified the
5073 // initially collected them. If so, the WeakTrackingVHs will have
5074 // nullified the
50725075 // values, so remove them from the set of candidates.
50735076 Candidates.remove(nullptr);
50745077
3333 ConcreteCallbackVH(Value *V) : CallbackVH(V) {}
3434 };
3535
36 TEST_F(ValueHandle, WeakVH_BasicOperation) {
37 WeakVH WVH(BitcastV.get());
36 TEST_F(ValueHandle, WeakTrackingVH_BasicOperation) {
37 WeakTrackingVH WVH(BitcastV.get());
3838 EXPECT_EQ(BitcastV.get(), WVH);
3939 WVH = ConstantV;
4040 EXPECT_EQ(ConstantV, WVH);
4545 EXPECT_EQ(Type::getInt32Ty(Context), (*WVH).getType());
4646 }
4747
48 TEST_F(ValueHandle, WeakVH_Comparisons) {
49 WeakVH BitcastWVH(BitcastV.get());
50 WeakVH ConstantWVH(ConstantV);
48 TEST_F(ValueHandle, WeakTrackingVH_Comparisons) {
49 WeakTrackingVH BitcastWVH(BitcastV.get());
50 WeakTrackingVH ConstantWVH(ConstantV);
5151
5252 EXPECT_TRUE(BitcastWVH == BitcastWVH);
5353 EXPECT_TRUE(BitcastV.get() == BitcastWVH);
7878 EXPECT_EQ(BV >= CV, BitcastWVH >= ConstantV);
7979 }
8080
81 TEST_F(ValueHandle, WeakVH_FollowsRAUW) {
82 WeakVH WVH(BitcastV.get());
83 WeakVH WVH_Copy(WVH);
84 WeakVH WVH_Recreated(BitcastV.get());
81 TEST_F(ValueHandle, WeakTrackingVH_FollowsRAUW) {
82 WeakTrackingVH WVH(BitcastV.get());
83 WeakTrackingVH WVH_Copy(WVH);
84 WeakTrackingVH WVH_Recreated(BitcastV.get());
8585 BitcastV->replaceAllUsesWith(ConstantV);
8686 EXPECT_EQ(ConstantV, WVH);
8787 EXPECT_EQ(ConstantV, WVH_Copy);
8888 EXPECT_EQ(ConstantV, WVH_Recreated);
8989 }
9090
91 TEST_F(ValueHandle, WeakVH_NullOnDeletion) {
92 WeakVH WVH(BitcastV.get());
93 WeakVH WVH_Copy(WVH);
94 WeakVH WVH_Recreated(BitcastV.get());
91 TEST_F(ValueHandle, WeakTrackingVH_NullOnDeletion) {
92 WeakTrackingVH WVH(BitcastV.get());
93 WeakTrackingVH WVH_Copy(WVH);
94 WeakTrackingVH WVH_Recreated(BitcastV.get());
9595 BitcastV.reset();
9696 Value *null_value = nullptr;
9797 EXPECT_EQ(null_value, WVH);
342342
343343 class DestroyingVH final : public CallbackVH {
344344 public:
345 std::unique_ptrVH> ToClear[2];
345 std::unique_ptrTrackingVH> ToClear[2];
346346 DestroyingVH(Value *V) {
347 ToClear[0].reset(new WeakVH(V));
347 ToClear[0].reset(new WeakTrackingVH(V));
348348 setValPtr(V);
349 ToClear[1].reset(new WeakVH(V));
349 ToClear[1].reset(new WeakTrackingVH(V));
350350 }
351351 void deleted() override {
352352 ToClear[0].reset();
360360 };
361361
362362 {
363 WeakVH ShouldBeVisited1(BitcastV.get());
363 WeakTrackingVH ShouldBeVisited1(BitcastV.get());
364364 DestroyingVH C(BitcastV.get());
365 WeakVH ShouldBeVisited2(BitcastV.get());
365 WeakTrackingVH ShouldBeVisited2(BitcastV.get());
366366
367367 BitcastV->replaceAllUsesWith(ConstantV);
368368 EXPECT_EQ(ConstantV, static_cast(ShouldBeVisited1));
370370 }
371371
372372 {
373 WeakVH ShouldBeVisited1(BitcastV.get());
373 WeakTrackingVH ShouldBeVisited1(BitcastV.get());
374374 DestroyingVH C(BitcastV.get());
375 WeakVH ShouldBeVisited2(BitcastV.get());
375 WeakTrackingVH ShouldBeVisited2(BitcastV.get());
376376
377377 BitcastV.reset();
378378 EXPECT_EQ(nullptr, static_cast(ShouldBeVisited1));