llvm.org GIT mirror llvm / a80a391
Add "const" in GetUnderlyingObjects. NFC Summary: Both the input Value pointer and the returned Value pointers in GetUnderlyingObjects are now declared as const. It turned out that all current (in-tree) uses of GetUnderlyingObjects were trivial to update, being satisfied with have those Value pointers declared as const. Actually, in the past several of the users had to use const_cast, just because of ValueTracking not providing a version of GetUnderlyingObjects with "const" Value pointers. With this patch we get rid of those const casts. Reviewers: hfinkel, materi, jkorous Reviewed By: jkorous Subscribers: dexonsmith, jkorous, jholewinski, sdardis, eraman, hiraditya, jrtc27, atanasyan, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61038 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@359072 91177308-0d34-0410-b5e6-96231b3b80d8 Bjorn Pettersson 1 year, 7 months ago
13 changed file(s) with 59 addition(s) and 57 deletion(s). Raw diff Collapse all Expand all
350350 /// Since A[i] and A[i-1] are independent pointers, getUnderlyingObjects
351351 /// should not assume that Curr and Prev share the same underlying object thus
352352 /// it shouldn't look through the phi above.
353 void GetUnderlyingObjects(Value *V, SmallVectorImpl &Objects,
353 void GetUnderlyingObjects(const Value *V,
354 SmallVectorImpl &Objects,
354355 const DataLayout &DL, LoopInfo *LI = nullptr,
355356 unsigned MaxLookup = 6);
356357
895895 // Iterate through all the arguments to the called function. If any argument
896896 // is based on GV, return the conservative result.
897897 for (auto &A : Call->args()) {
898 SmallVector<Value*, 4> Objects;
898 SmallVector<const Value*, 4> Objects;
899899 GetUnderlyingObjects(A, Objects, DL);
900900
901901 // All objects must be identified.
902902 if (!all_of(Objects, isIdentifiedObject) &&
903903 // Try ::alias to see if all objects are known not to alias GV.
904 !all_of(Objects, [&](Value *V) {
904 !all_of(Objects, [&](const Value *V) {
905905 return this->alias(MemoryLocation(V), MemoryLocation(GV), AAQI) ==
906906 NoAlias;
907907 }))
22802280 // come from a pointer that cannot overlap with dynamically-allocated
22812281 // memory within the lifetime of the current function (allocas, byval
22822282 // arguments, globals), then determine the comparison result here.
2283 SmallVector<Value *, 8> LHSUObjs, RHSUObjs;
2283 SmallVector<const Value *, 8> LHSUObjs, RHSUObjs;
22842284 GetUnderlyingObjects(LHS, LHSUObjs, DL);
22852285 GetUnderlyingObjects(RHS, RHSUObjs, DL);
22862286
22872287 // Is the set of underlying objects all noalias calls?
2288 auto IsNAC = [](ArrayRef<Value *> Objects) {
2288 auto IsNAC = [](ArrayRef<const Value *> Objects) {
22892289 return all_of(Objects, isNoAliasCall);
22902290 };
22912291
22952295 // live with the compared-to allocation). For globals, we exclude symbols
22962296 // that might be resolve lazily to symbols in another dynamically-loaded
22972297 // library (and, thus, could be malloc'ed by the implementation).
2298 auto IsAllocDisjoint = [](ArrayRef Objects) {
2299 return all_of(Objects, [](Value *V) {
2298 auto IsAllocDisjoint = [](ArrayRef Objects) {
2299 return all_of(Objects, [](const Value *V) {
23002300 if (const AllocaInst *AI = dyn_cast(V))
23012301 return AI->getParent() && AI->getFunction() && AI->isStaticAlloca();
23022302 if (const GlobalValue *GV = dyn_cast(V))
841841 bool SetHasWrite = false;
842842
843843 // Map of pointers to last access encountered.
844 typedef DenseMap<Value*, MemAccessInfo> UnderlyingObjToAccessMap;
844 typedef DenseMap<const Value*, MemAccessInfo> UnderlyingObjToAccessMap;
845845 UnderlyingObjToAccessMap ObjToLastAccess;
846846
847847 // Set of access to check after all writes have been processed.
902902
903903 // Create sets of pointers connected by a shared alias set and
904904 // underlying object.
905 typedef SmallVector<Value *, 16> ValueVector;
905 typedef SmallVector<const Value *, 16> ValueVector;
906906 ValueVector TempObjects;
907907
908908 GetUnderlyingObjects(Ptr, TempObjects, DL, LI);
909909 LLVM_DEBUG(dbgs()
910910 << "Underlying objects for pointer " << *Ptr << "\n");
911 for (Value *UnderlyingObj : TempObjects) {
911 for (const Value *UnderlyingObj : TempObjects) {
912912 // nullptr never alias, don't join sets for pointer that have "null"
913913 // in their UnderlyingObjects list.
914914 if (isa(UnderlyingObj) &&
37703770 return V;
37713771 }
37723772
3773 void llvm::GetUnderlyingObjects(Value *V, SmallVectorImpl &Objects,
3773 void llvm::GetUnderlyingObjects(const Value *V,
3774 SmallVectorImpl &Objects,
37743775 const DataLayout &DL, LoopInfo *LI,
37753776 unsigned MaxLookup) {
3776 SmallPtrSet Visited;
3777 SmallVector Worklist;
3777 SmallPtrSet Visited;
3778 SmallVector Worklist;
37783779 Worklist.push_back(V);
37793780 do {
3780 Value *P = Worklist.pop_back_val();
3781 const Value *P = Worklist.pop_back_val();
37813782 P = GetUnderlyingObject(P, DL, MaxLookup);
37823783
37833784 if (!Visited.insert(P).second)
37843785 continue;
37853786
3786 if (SelectInst *SI = dyn_cast(P)) {
3787 if (auto *SI = dyn_cast(P)) {
37873788 Worklist.push_back(SI->getTrueValue());
37883789 Worklist.push_back(SI->getFalseValue());
37893790 continue;
37903791 }
37913792
3792 if (PHINode *PN = dyn_cast(P)) {
3793 if (auto *PN = dyn_cast(P)) {
37933794 // If this PHI changes the underlying object in every iteration of the
37943795 // loop, don't look through it. Consider:
37953796 // int **A;
38503851 do {
38513852 V = Working.pop_back_val();
38523853
3853 SmallVector Objs;
3854 GetUnderlyingObjects(const_cast(V), Objs, DL);
3855
3856 for (Value *V : Objs) {
3854 SmallVector Objs;
3855 GetUnderlyingObjects(V, Objs, DL);
3856
3857 for (const Value *V : Objs) {
38573858 if (!Visited.insert(V).second)
38583859 continue;
38593860 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
845845
846846 // Get the underlying objects for the location passed on the lifetime
847847 // marker.
848 SmallVector<Value *, 4> Allocas;
848 SmallVector<const Value *, 4> Allocas;
849849 GetUnderlyingObjects(CI.getArgOperand(1), Allocas, *DL);
850850
851851 // Iterate over each underlying object, creating lifetime markers for each
852852 // static alloca. Quit if we find a non-static alloca.
853 for (Value *V : Allocas) {
854 AllocaInst *AI = dyn_cast(V);
853 for (const Value *V : Allocas) {
854 const AllocaInst *AI = dyn_cast(V);
855855 if (!AI)
856856 continue;
857857
540540 /// Return the underlying objects for the memory references of an instruction.
541541 /// This function calls the code in ValueTracking, but first checks that the
542542 /// instruction has a memory operand.
543 static void getUnderlyingObjects(MachineInstr *MI,
544 SmallVectorImpl &Objs,
543 static void getUnderlyingObjects(const MachineInstr *MI,
544 SmallVectorImpl &Objs,
545545 const DataLayout &DL) {
546546 if (!MI->hasOneMemOperand())
547547 return;
548548 MachineMemOperand *MM = *MI->memoperands_begin();
549549 if (!MM->getValue())
550550 return;
551 GetUnderlyingObjects(const_cast(MM->getValue()), Objs, DL);
552 for (Value *V : Objs) {
551 GetUnderlyingObjects(MM->getValue(), Objs, DL);
552 for (const Value *V : Objs) {
553553 if (!isIdentifiedObject(V)) {
554554 Objs.clear();
555555 return;
563563 /// dependence. This code is very similar to the code in ScheduleDAGInstrs
564564 /// but that code doesn't create loop carried dependences.
565565 void SwingSchedulerDAG::addLoopCarriedDependences(AliasAnalysis *AA) {
566 MapVector<Value *, SmallVector> PendingLoads;
566 MapVector<const Value *, SmallVector> PendingLoads;
567567 Value *UnknownValue =
568568 UndefValue::get(Type::getVoidTy(MF.getFunction().getContext()));
569569 for (auto &SU : SUnits) {
571571 if (isDependenceBarrier(MI, AA))
572572 PendingLoads.clear();
573573 else if (MI.mayLoad()) {
574 SmallVector<Value *, 4> Objs;
574 SmallVector<const Value *, 4> Objs;
575575 getUnderlyingObjects(&MI, Objs, MF.getDataLayout());
576576 if (Objs.empty())
577577 Objs.push_back(UnknownValue);
580580 SUs.push_back(&SU);
581581 }
582582 } else if (MI.mayStore()) {
583 SmallVector<Value *, 4> Objs;
583 SmallVector<const Value *, 4> Objs;
584584 getUnderlyingObjects(&MI, Objs, MF.getDataLayout());
585585 if (Objs.empty())
586586 Objs.push_back(UnknownValue);
587587 for (auto V : Objs) {
588 MapVector<Value *, SmallVector>::iterator I =
588 MapVector<const Value *, SmallVector>::iterator I =
589589 PendingLoads.find(V);
590590 if (I == PendingLoads.end())
591591 continue;
64626462 const int64_t ObjectSize =
64636463 cast(I.getArgOperand(0))->getSExtValue();
64646464 Value *const ObjectPtr = I.getArgOperand(1);
6465 SmallVector<Value *, 4> Allocas;
6465 SmallVector<const Value *, 4> Allocas;
64666466 GetUnderlyingObjects(ObjectPtr, Allocas, *DL);
64676467
6468 for (SmallVectorImpl<Value*>::iterator Object = Allocas.begin(),
6468 for (SmallVectorImpl<const Value*>::iterator Object = Allocas.begin(),
64696469 E = Allocas.end(); Object != E; ++Object) {
6470 AllocaInst *LifetimeObject = dyn_cast_or_null(*Object);
6470 const AllocaInst *LifetimeObject = dyn_cast_or_null(*Object);
64716471
64726472 // Could not find an Alloca.
64736473 if (!LifetimeObject)
539539
540540 const Value *V = (*MI.memoperands_begin())->getValue();
541541
542 SmallVector Objs;
543 GetUnderlyingObjects(const_cast(V), Objs, DL);
544
545 for (SmallVectorImpl::iterator I = Objs.begin(), E = Objs.end();
542 SmallVector Objs;
543 GetUnderlyingObjects(V, Objs, DL);
544
545 for (SmallVectorImpl::iterator I = Objs.begin(), E = Objs.end();
546546 I != E; ++I) {
547547 if (!isIdentifiedObject(V))
548548 return false;
700700 // We use GetUnderlyingObjects() here instead of GetUnderlyingObject() mainly
701701 // because the former looks through phi nodes while the latter does not. We
702702 // need to look through phi nodes to handle pointer induction variables.
703 SmallVector Objs;
704 GetUnderlyingObjects(const_cast(N->getMemOperand()->getValue()),
703 SmallVector Objs;
704 GetUnderlyingObjects(N->getMemOperand()->getValue(),
705705 Objs, F->getDataLayout());
706706
707 return all_of(Objs, [&](Value *V) {
707 return all_of(Objs, [&](const Value *V) {
708708 if (auto *A = dyn_cast(V))
709709 return IsKernelFn && A->onlyReadsMemory() && A->hasNoAliasAttr();
710710 if (auto *GV = dyn_cast(V))
11891189 }
11901190
11911191 uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
1192 SmallVector<Value *, 2> Objs;
1192 SmallVector<const Value *, 2> Objs;
11931193 GetUnderlyingObjects(Addr, Objs, Pos->getModule()->getDataLayout());
11941194 bool AllConstants = true;
1195 for (Value *Obj : Objs) {
1195 for (const Value *Obj : Objs) {
11961196 if (isa(Obj) || isa(Obj))
11971197 continue;
11981198 if (isa(Obj) && cast(Obj)->isConstant())
9898 deleteDeadInstruction(Instruction *I, BasicBlock::iterator *BBI,
9999 MemoryDependenceResults &MD, const TargetLibraryInfo &TLI,
100100 InstOverlapIntervalsTy &IOL, OrderedBasicBlock &OBB,
101 SmallSetVector<Value *, 16> *ValueSet = nullptr) {
101 SmallSetVector<const Value *, 16> *ValueSet = nullptr) {
102102 SmallVector NowDeadInsts;
103103
104104 NowDeadInsts.push_back(I);
712712 /// the DeadStackObjects set. If so, they become live because the location is
713713 /// being loaded.
714714 static void removeAccessedObjects(const MemoryLocation &LoadedLoc,
715 SmallSetVector<Value *, 16> &DeadStackObjects,
715 SmallSetVector<const Value *, 16> &DeadStackObjects,
716716 const DataLayout &DL, AliasAnalysis *AA,
717717 const TargetLibraryInfo *TLI,
718718 const Function *F) {
725725 // If the kill pointer can be easily reduced to an alloca, don't bother doing
726726 // extraneous AA queries.
727727 if (isa(UnderlyingPointer) || isa(UnderlyingPointer)) {
728 DeadStackObjects.remove(const_cast(UnderlyingPointer));
728 DeadStackObjects.remove(UnderlyingPointer);
729729 return;
730730 }
731731
732732 // Remove objects that could alias LoadedLoc.
733 DeadStackObjects.remove_if([&](Value *I) {
733 DeadStackObjects.remove_if([&](const Value *I) {
734734 // See if the loaded location could alias the stack location.
735735 MemoryLocation StackLoc(I, getPointerSize(I, DL, *TLI, F));
736736 return !AA->isNoAlias(StackLoc, LoadedLoc);
752752
753753 // Keep track of all of the stack objects that are dead at the end of the
754754 // function.
755 SmallSetVector<Value*, 16> DeadStackObjects;
755 SmallSetVector<const Value*, 16> DeadStackObjects;
756756
757757 // Find all of the alloca'd pointers in the entry block.
758758 BasicBlock &Entry = BB.getParent()->front();
781781 // If we find a store, check to see if it points into a dead stack value.
782782 if (hasAnalyzableMemoryWrite(&*BBI, *TLI) && isRemovable(&*BBI)) {
783783 // See through pointer-to-pointer bitcasts
784 SmallVector<Value *, 4> Pointers;
784 SmallVector<const Value *, 4> Pointers;
785785 GetUnderlyingObjects(getStoredPointerOperand(&*BBI), Pointers, DL);
786786
787787 // Stores to stack values are valid candidates for removal.
788788 bool AllDead = true;
789 for (Value *Pointer : Pointers)
789 for (const Value *Pointer : Pointers)
790790 if (!DeadStackObjects.count(Pointer)) {
791791 AllDead = false;
792792 break;
797797
798798 LLVM_DEBUG(dbgs() << "DSE: Dead Store at End of Block:\n DEAD: "
799799 << *Dead << "\n Objects: ";
800 for (SmallVectorImpl<Value *>::iterator I = Pointers.begin(),
800 for (SmallVectorImpl<const Value *>::iterator I =
801 Pointers.begin(),
801802 E = Pointers.end();
802803 I != E; ++I) {
803804 dbgs() << **I;
846847
847848 // If the call might load from any of our allocas, then any store above
848849 // the call is live.
849 DeadStackObjects.remove_if([&](Value *I) {
850 DeadStackObjects.remove_if([&](const Value *I) {
850851 // See if the call site touches the value.
851852 return isRefSet(AA->getModRefInfo(
852853 Call, I, getPointerSize(I, DL, *TLI, BB.getParent())));
10401040
10411041 SmallSetVector NAPtrArgs;
10421042 for (const Value *V : PtrArgs) {
1043 SmallVector Objects;
1044 GetUnderlyingObjects(const_cast(V),
1045 Objects, DL, /* LI = */ nullptr);
1046
1047 for (Value *O : Objects)
1043 SmallVector Objects;
1044 GetUnderlyingObjects(V, Objects, DL, /* LI = */ nullptr);
1045
1046 for (const Value *O : Objects)
10481047 ObjSet.insert(O);
10491048 }
10501049