@@ -12587,16 +12587,37 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1258712587 }
1258812588
1258912589 ChangeStatus updateImpl (Attributor &A) override {
12590+ assert (A.getInfoCache ().getDL ().getFlatAddressSpace ().has_value ());
12591+ unsigned FlatAS = A.getInfoCache ().getDL ().getFlatAddressSpace ().value ();
1259012592 uint32_t OldAddressSpace = AssumedAddressSpace;
12591- auto *AUO = A.getOrCreateAAFor <AAUnderlyingObjects>(getIRPosition (), this ,
12592- DepClassTy::REQUIRED);
12593- auto Pred = [&](Value &Obj) {
12593+
12594+ auto CheckAddressSpace = [&](Value &Obj) {
1259412595 if (isa<UndefValue>(&Obj))
1259512596 return true ;
12597+ // If an argument in flat address space only has addrspace cast uses, and
12598+ // those casts are same, then we take the dst addrspace.
12599+ if (auto *Arg = dyn_cast<Argument>(&Obj)) {
12600+ if (Arg->getType ()->getPointerAddressSpace () == FlatAS) {
12601+ unsigned CastAddrSpace = FlatAS;
12602+ for (auto *U : Arg->users ()) {
12603+ auto *ASCI = dyn_cast<AddrSpaceCastInst>(U);
12604+ if (!ASCI)
12605+ return takeAddressSpace (Obj.getType ()->getPointerAddressSpace ());
12606+ if (CastAddrSpace != FlatAS &&
12607+ CastAddrSpace != ASCI->getDestAddressSpace ())
12608+ return false ;
12609+ CastAddrSpace = ASCI->getDestAddressSpace ();
12610+ }
12611+ if (CastAddrSpace != FlatAS)
12612+ return takeAddressSpace (CastAddrSpace);
12613+ }
12614+ }
1259612615 return takeAddressSpace (Obj.getType ()->getPointerAddressSpace ());
1259712616 };
1259812617
12599- if (!AUO->forallUnderlyingObjects (Pred))
12618+ auto *AUO = A.getOrCreateAAFor <AAUnderlyingObjects>(getIRPosition (), this ,
12619+ DepClassTy::REQUIRED);
12620+ if (!AUO->forallUnderlyingObjects (CheckAddressSpace))
1260012621 return indicatePessimisticFixpoint ();
1260112622
1260212623 return OldAddressSpace == AssumedAddressSpace ? ChangeStatus::UNCHANGED
@@ -12605,17 +12626,21 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1260512626
1260612627 // / See AbstractAttribute::manifest(...).
1260712628 ChangeStatus manifest (Attributor &A) override {
12608- if (getAddressSpace () == InvalidAddressSpace ||
12609- getAddressSpace () == getAssociatedType ()->getPointerAddressSpace ())
12629+ unsigned NewAS = getAddressSpace ();
12630+
12631+ if (NewAS == InvalidAddressSpace ||
12632+ NewAS == getAssociatedType ()->getPointerAddressSpace ())
1261012633 return ChangeStatus::UNCHANGED;
1261112634
12635+ unsigned FlatAS = A.getInfoCache ().getDL ().getFlatAddressSpace ().value ();
12636+
1261212637 Value *AssociatedValue = &getAssociatedValue ();
12613- Value *OriginalValue = peelAddrspacecast (AssociatedValue);
12638+ Value *OriginalValue = peelAddrspacecast (AssociatedValue, FlatAS );
1261412639
1261512640 PointerType *NewPtrTy =
12616- PointerType::get (getAssociatedType ()->getContext (), getAddressSpace () );
12641+ PointerType::get (getAssociatedType ()->getContext (), NewAS );
1261712642 bool UseOriginalValue =
12618- OriginalValue->getType ()->getPointerAddressSpace () == getAddressSpace () ;
12643+ OriginalValue->getType ()->getPointerAddressSpace () == NewAS ;
1261912644
1262012645 bool Changed = false ;
1262112646
@@ -12675,12 +12700,19 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1267512700 return AssumedAddressSpace == AS;
1267612701 }
1267712702
12678- static Value *peelAddrspacecast (Value *V) {
12679- if (auto *I = dyn_cast<AddrSpaceCastInst>(V))
12680- return peelAddrspacecast (I->getPointerOperand ());
12703+ static Value *peelAddrspacecast (Value *V, unsigned FlatAS) {
12704+ if (auto *I = dyn_cast<AddrSpaceCastInst>(V)) {
12705+ assert (I->getSrcAddressSpace () != FlatAS &&
12706+ " there should not be flat AS -> non-flat AS" );
12707+ return I->getPointerOperand ();
12708+ }
1268112709 if (auto *C = dyn_cast<ConstantExpr>(V))
12682- if (C->getOpcode () == Instruction::AddrSpaceCast)
12683- return peelAddrspacecast (C->getOperand (0 ));
12710+ if (C->getOpcode () == Instruction::AddrSpaceCast) {
12711+ assert (C->getOperand (0 )->getType ()->getPointerAddressSpace () !=
12712+ FlatAS &&
12713+ " there should not be flat AS -> non-flat AS X" );
12714+ return C->getOperand (0 );
12715+ }
1268412716 return V;
1268512717 }
1268612718};
0 commit comments