@@ -12562,7 +12562,7 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1256212562 AAAddressSpaceImpl (const IRPosition &IRP, Attributor &A)
1256312563 : AAAddressSpace(IRP, A) {}
1256412564
12565- int32_t getAddressSpace () const override {
12565+ uint32_t getAddressSpace () const override {
1256612566 assert (isValidState () && " the AA is invalid" );
1256712567 return AssumedAddressSpace;
1256812568 }
@@ -12571,12 +12571,37 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1257112571 void initialize (Attributor &A) override {
1257212572 assert (getAssociatedType ()->isPtrOrPtrVectorTy () &&
1257312573 " Associated value is not a pointer" );
12574- if (getAssociatedType ()->getPointerAddressSpace ())
12574+ // If the pointer already has non-generic address space, we assume it is the
12575+ // correct one.
12576+ if (getAssociatedType ()->getPointerAddressSpace ()) {
12577+ [[maybe_unused]] bool R =
12578+ takeAddressSpace (getAssociatedType ()->getPointerAddressSpace ());
12579+ assert (R && " the take should happen" );
1257512580 indicateOptimisticFixpoint ();
12581+ return ;
12582+ }
12583+ // If the pointer is an addrspacecast, we assume the source address space is
12584+ // the correct one.
12585+ Value *V = &getAssociatedValue ();
12586+ if (auto *ASC = dyn_cast<AddrSpaceCastInst>(V)) {
12587+ [[maybe_unused]] bool R = takeAddressSpace (ASC->getSrcAddressSpace ());
12588+ assert (R && " the take should happen" );
12589+ indicateOptimisticFixpoint ();
12590+ return ;
12591+ }
12592+ if (auto *C = dyn_cast<ConstantExpr>(V)) {
12593+ if (C->getOpcode () == Instruction::AddrSpaceCast) {
12594+ [[maybe_unused]] bool R = takeAddressSpace (
12595+ C->getOperand (0 )->getType ()->getPointerAddressSpace ());
12596+ assert (R && " the take should happen" );
12597+ indicateOptimisticFixpoint ();
12598+ return ;
12599+ }
12600+ }
1257612601 }
1257712602
1257812603 ChangeStatus updateImpl (Attributor &A) override {
12579- int32_t OldAddressSpace = AssumedAddressSpace;
12604+ uint32_t OldAddressSpace = AssumedAddressSpace;
1258012605 auto *AUO = A.getOrCreateAAFor <AAUnderlyingObjects>(getIRPosition (), this ,
1258112606 DepClassTy::REQUIRED);
1258212607 auto Pred = [&](Value &Obj) {
@@ -12594,19 +12619,17 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1259412619
1259512620 // / See AbstractAttribute::manifest(...).
1259612621 ChangeStatus manifest (Attributor &A) override {
12597- Value *AssociatedValue = &getAssociatedValue ();
12598- Value *OriginalValue = peelAddrspacecast (AssociatedValue);
12599- if (getAddressSpace () == NoAddressSpace ||
12600- static_cast <uint32_t >(getAddressSpace ()) ==
12601- getAssociatedType ()->getPointerAddressSpace ())
12622+ unsigned NewAS = getAddressSpace ();
12623+ if (NewAS == NoAddressSpace ||
12624+ NewAS == getAssociatedType ()->getPointerAddressSpace ())
1260212625 return ChangeStatus::UNCHANGED;
1260312626
12604- PointerType *NewPtrTy =
12605- PointerType::get (getAssociatedType ()->getContext (),
12606- static_cast <uint32_t >(getAddressSpace ()));
12627+ Value *AssociatedValue = &getAssociatedValue ();
12628+ Value *OriginalValue = peelAddrspacecast (AssociatedValue);
1260712629 bool UseOriginalValue =
12608- OriginalValue->getType ()->getPointerAddressSpace () ==
12609- static_cast <uint32_t >(getAddressSpace ());
12630+ OriginalValue->getType ()->getPointerAddressSpace () == NewAS;
12631+ PointerType *NewPtrTy =
12632+ PointerType::get (getAssociatedType ()->getContext (), NewAS);
1261012633
1261112634 bool Changed = false ;
1261212635
@@ -12656,9 +12679,9 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1265612679 }
1265712680
1265812681private:
12659- int32_t AssumedAddressSpace = NoAddressSpace;
12682+ uint32_t AssumedAddressSpace = NoAddressSpace;
1266012683
12661- bool takeAddressSpace (int32_t AS) {
12684+ bool takeAddressSpace (uint32_t AS) {
1266212685 if (AssumedAddressSpace == NoAddressSpace) {
1266312686 AssumedAddressSpace = AS;
1266412687 return true ;
@@ -12667,11 +12690,16 @@ struct AAAddressSpaceImpl : public AAAddressSpace {
1266712690 }
1266812691
1266912692 static Value *peelAddrspacecast (Value *V) {
12670- if (auto *I = dyn_cast<AddrSpaceCastInst>(V))
12671- return peelAddrspacecast (I->getPointerOperand ());
12693+ if (auto *I = dyn_cast<AddrSpaceCastInst>(V)) {
12694+ assert (I->getSrcAddressSpace () && " there should not be AS 0 -> AS X" );
12695+ return I->getPointerOperand ();
12696+ }
1267212697 if (auto *C = dyn_cast<ConstantExpr>(V))
12673- if (C->getOpcode () == Instruction::AddrSpaceCast)
12674- return peelAddrspacecast (C->getOperand (0 ));
12698+ if (C->getOpcode () == Instruction::AddrSpaceCast) {
12699+ assert (C->getOperand (0 )->getType ()->getPointerAddressSpace () &&
12700+ " there should not be AS 0 -> AS X" );
12701+ return C->getOperand (0 );
12702+ }
1267512703 return V;
1267612704 }
1267712705};
0 commit comments