@@ -193,6 +193,7 @@ PIPE_OPERATOR(AAAssumptionInfo)
193193PIPE_OPERATOR(AAUnderlyingObjects)
194194PIPE_OPERATOR(AAInvariantLoadPointer)
195195PIPE_OPERATOR(AAAddressSpace)
196+ PIPE_OPERATOR(AANoAliasAddrSpace)
196197PIPE_OPERATOR(AAAllocationInfo)
197198PIPE_OPERATOR(AAIndirectCallInfo)
198199PIPE_OPERATOR(AAGlobalValueInfo)
@@ -13146,6 +13147,197 @@ struct AAAddressSpaceCallSiteArgument final : AAAddressSpaceImpl {
1314613147};
1314713148} // namespace
1314813149
13150+ /// ------------------------ No Alias Address Space ---------------------------
13151+ // This attribute assumes flat address space can alias all other address space
13152+
13153+ // TODO: this is similar to AAAddressSpace, most of the code should be merged.
13154+ // But merging it created failing cased on gateway test that cannot be
13155+ // reproduced locally. So should open a seperated PR to hande the merge of
13156+ // AANoAliasAddrSpace and AAAddressSpace attribute
13157+
13158+ namespace {
13159+ struct AANoAliasAddrSpaceImpl : public AANoAliasAddrSpace {
13160+ AANoAliasAddrSpaceImpl(const IRPosition &IRP, Attributor &A)
13161+ : AANoAliasAddrSpace(IRP, A) {}
13162+
13163+ void initialize(Attributor &A) override {
13164+ assert(getAssociatedType()->isPtrOrPtrVectorTy() &&
13165+ "Associated value is not a pointer");
13166+
13167+ resetASRanges(A);
13168+
13169+ std::optional<unsigned> FlatAS = A.getInfoCache().getFlatAddressSpace();
13170+ if (!FlatAS.has_value()) {
13171+ indicatePessimisticFixpoint();
13172+ return;
13173+ }
13174+
13175+ removeAS(*FlatAS);
13176+
13177+ unsigned AS = getAssociatedType()->getPointerAddressSpace();
13178+ if (AS != *FlatAS) {
13179+ removeAS(AS);
13180+ indicateOptimisticFixpoint();
13181+ }
13182+ }
13183+
13184+ ChangeStatus updateImpl(Attributor &A) override {
13185+ unsigned FlatAS = A.getInfoCache().getFlatAddressSpace().value();
13186+ uint32_t OldAssumed = getAssumed();
13187+
13188+ auto CheckAddressSpace = [&](Value &Obj) {
13189+ if (isa<PoisonValue>(&Obj))
13190+ return true;
13191+
13192+ unsigned AS = Obj.getType()->getPointerAddressSpace();
13193+ if (AS == FlatAS)
13194+ return false;
13195+
13196+ removeAS(Obj.getType()->getPointerAddressSpace());
13197+ return true;
13198+ };
13199+
13200+ const AAUnderlyingObjects *AUO = A.getOrCreateAAFor<AAUnderlyingObjects>(
13201+ getIRPosition(), this, DepClassTy::REQUIRED);
13202+ if (!AUO->forallUnderlyingObjects(CheckAddressSpace))
13203+ return indicatePessimisticFixpoint();
13204+
13205+ return OldAssumed == getAssumed() ? ChangeStatus::UNCHANGED
13206+ : ChangeStatus::CHANGED;
13207+ }
13208+
13209+ /// See AbstractAttribute::manifest(...).
13210+ ChangeStatus manifest(Attributor &A) override {
13211+ unsigned FlatAS = A.getInfoCache().getFlatAddressSpace().value();
13212+
13213+ unsigned AS = getAssociatedType()->getPointerAddressSpace();
13214+ if (AS != FlatAS || Map.empty())
13215+ return ChangeStatus::UNCHANGED;
13216+
13217+ LLVMContext &Ctx = getAssociatedValue().getContext();
13218+ MDNode *NoAliasASNode = nullptr;
13219+ MDBuilder MDB(Ctx);
13220+ // Has to use iterator to get the range info.
13221+ for (RangeMap::const_iterator I = Map.begin(); I != Map.end(); I++) {
13222+ if (!I.value())
13223+ continue;
13224+ unsigned Upper = I.stop();
13225+ unsigned Lower = I.start();
13226+ if (!NoAliasASNode) {
13227+ NoAliasASNode = MDB.createRange(APInt(32, Lower), APInt(32, Upper + 1));
13228+ continue;
13229+ }
13230+ MDNode *ASRange = MDB.createRange(APInt(32, Lower), APInt(32, Upper + 1));
13231+ NoAliasASNode = MDNode::getMostGenericRange(NoAliasASNode, ASRange);
13232+ }
13233+
13234+ Value *AssociatedValue = &getAssociatedValue();
13235+ bool Changed = false;
13236+
13237+ auto AddNoAliasAttr = [&](const Use &U, bool &) {
13238+ if (U.get() != AssociatedValue)
13239+ return true;
13240+ Instruction *Inst = dyn_cast<Instruction>(U.getUser());
13241+ if (!Inst || Inst->hasMetadata(LLVMContext::MD_noalias_addrspace))
13242+ return true;
13243+ if (!isa<LoadInst>(Inst) && !isa<StoreInst>(Inst) &&
13244+ !isa<AtomicCmpXchgInst>(Inst) && !isa<AtomicRMWInst>(Inst))
13245+ return true;
13246+ if (!A.isRunOn(Inst->getFunction()))
13247+ return true;
13248+ Inst->setMetadata(LLVMContext::MD_noalias_addrspace, NoAliasASNode);
13249+ Changed = true;
13250+ return true;
13251+ };
13252+ (void)A.checkForAllUses(AddNoAliasAttr, *this, *AssociatedValue,
13253+ /*CheckBBLivenessOnly=*/true);
13254+ return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
13255+ }
13256+
13257+ /// See AbstractAttribute::getAsStr().
13258+ const std::string getAsStr(Attributor *A) const override {
13259+ if (!isValidState())
13260+ return "<invalid>";
13261+ std::string Str;
13262+ raw_string_ostream OS(Str);
13263+ OS << "CanNotBeAddrSpace(";
13264+ for (RangeMap::const_iterator I = Map.begin(); I != Map.end(); I++) {
13265+ unsigned Upper = I.stop();
13266+ unsigned Lower = I.start();
13267+ OS << ' ' << '[' << Upper << ',' << Lower + 1 << ')';
13268+ }
13269+ OS << " )";
13270+ return OS.str();
13271+ }
13272+
13273+ private:
13274+ void removeAS(unsigned AS) {
13275+ RangeMap::iterator I = Map.find(AS);
13276+
13277+ if (I != Map.end()) {
13278+ unsigned Upper = I.stop();
13279+ unsigned Lower = I.start();
13280+ I.erase();
13281+ if (Upper == Lower)
13282+ return;
13283+ if (AS != ~((unsigned)0) && AS + 1 <= Upper)
13284+ Map.insert(AS + 1, Upper, /*what ever this variable name is=*/true);
13285+ if (AS != 0 && Lower <= AS - 1)
13286+ Map.insert(Lower, AS - 1, true);
13287+ }
13288+ }
13289+
13290+ void resetASRanges(Attributor &A) {
13291+ Map.clear();
13292+ Map.insert(0, A.getInfoCache().getMaxAddrSpace(), true);
13293+ }
13294+ };
13295+
13296+ struct AANoAliasAddrSpaceFloating final : AANoAliasAddrSpaceImpl {
13297+ AANoAliasAddrSpaceFloating(const IRPosition &IRP, Attributor &A)
13298+ : AANoAliasAddrSpaceImpl(IRP, A) {}
13299+
13300+ void trackStatistics() const override {
13301+ STATS_DECLTRACK_FLOATING_ATTR(noaliasaddrspace);
13302+ }
13303+ };
13304+
13305+ struct AANoAliasAddrSpaceReturned final : AANoAliasAddrSpaceImpl {
13306+ AANoAliasAddrSpaceReturned(const IRPosition &IRP, Attributor &A)
13307+ : AANoAliasAddrSpaceImpl(IRP, A) {}
13308+
13309+ void trackStatistics() const override {
13310+ STATS_DECLTRACK_FNRET_ATTR(noaliasaddrspace);
13311+ }
13312+ };
13313+
13314+ struct AANoAliasAddrSpaceCallSiteReturned final : AANoAliasAddrSpaceImpl {
13315+ AANoAliasAddrSpaceCallSiteReturned(const IRPosition &IRP, Attributor &A)
13316+ : AANoAliasAddrSpaceImpl(IRP, A) {}
13317+
13318+ void trackStatistics() const override {
13319+ STATS_DECLTRACK_CSRET_ATTR(noaliasaddrspace);
13320+ }
13321+ };
13322+
13323+ struct AANoAliasAddrSpaceArgument final : AANoAliasAddrSpaceImpl {
13324+ AANoAliasAddrSpaceArgument(const IRPosition &IRP, Attributor &A)
13325+ : AANoAliasAddrSpaceImpl(IRP, A) {}
13326+
13327+ void trackStatistics() const override {
13328+ STATS_DECLTRACK_ARG_ATTR(noaliasaddrspace);
13329+ }
13330+ };
13331+
13332+ struct AANoAliasAddrSpaceCallSiteArgument final : AANoAliasAddrSpaceImpl {
13333+ AANoAliasAddrSpaceCallSiteArgument(const IRPosition &IRP, Attributor &A)
13334+ : AANoAliasAddrSpaceImpl(IRP, A) {}
13335+
13336+ void trackStatistics() const override {
13337+ STATS_DECLTRACK_CSARG_ATTR(noaliasaddrspace);
13338+ }
13339+ };
13340+ } // namespace
1314913341/// ----------- Allocation Info ----------
1315013342namespace {
1315113343struct AAAllocationInfoImpl : public AAAllocationInfo {
@@ -13400,6 +13592,7 @@ const char AAAssumptionInfo::ID = 0;
1340013592const char AAUnderlyingObjects::ID = 0;
1340113593const char AAInvariantLoadPointer::ID = 0;
1340213594const char AAAddressSpace::ID = 0;
13595+ const char AANoAliasAddrSpace::ID = 0;
1340313596const char AAAllocationInfo::ID = 0;
1340413597const char AAIndirectCallInfo::ID = 0;
1340513598const char AAGlobalValueInfo::ID = 0;
@@ -13535,6 +13728,7 @@ CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFPClass)
1353513728CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPointerInfo)
1353613729CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAInvariantLoadPointer)
1353713730CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAddressSpace)
13731+ CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAliasAddrSpace)
1353813732CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAllocationInfo)
1353913733
1354013734CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
0 commit comments