@@ -525,23 +525,56 @@ bool GenericAddressDynamicResolution::allowArithmeticOnGenericAddressSpace(Funct
525525 // iterate for all addrspacecast to generic pointers
526526 for (inst_iterator i = inst_begin (F); i != inst_end (F); ++i)
527527 {
528- AddrSpaceCastInst* addrSpaceCastInst = llvm::dyn_cast<AddrSpaceCastInst>(&*i);
529- IntToPtrInst* intToPtrInst = llvm::dyn_cast<IntToPtrInst>(&*i);
528+ AddrSpaceCastInst* addrSpaceCastInst = dyn_cast<AddrSpaceCastInst>(&*i);
529+ IntToPtrInst* intToPtrInst = dyn_cast<IntToPtrInst>(&*i);
530+ unsigned totalNumUses = 0 ;
531+ unsigned numUsesArith = 0 ;
532+
530533 if (addrSpaceCastInst && addrSpaceCastInst->getDestAddressSpace () == ADDRESS_SPACE_GENERIC)
531534 {
535+ totalNumUses = addrSpaceCastInst->getNumUses ();
532536 for (auto ui : addrSpaceCastInst->users ())
533537 {
534538 Instruction* useInst = dyn_cast<Instruction>(ui);
535- PtrToIntInst* ptiInst = llvm::dyn_cast<PtrToIntInst>(&*useInst);
539+ PHINode* phi = dyn_cast<PHINode>(useInst);
540+ GetElementPtrInst* gepInst = dyn_cast<GetElementPtrInst>(useInst);
541+
542+ if (phi)
543+ {
544+ useInst = useInst->user_back ();
545+ gepInst = dyn_cast<GetElementPtrInst>(useInst);
546+ if (gepInst)
547+ useInst = useInst->user_back ();
548+ }
549+ else if (gepInst)
550+ {
551+ useInst = useInst->user_back ();
552+ phi = dyn_cast<PHINode>(useInst);
553+ if (phi)
554+ useInst = useInst->user_back ();
555+ }
556+
557+ PtrToIntInst* ptiInst = dyn_cast<PtrToIntInst>(useInst);
536558 if (ptiInst && ptiInst->getPointerAddressSpace () == ADDRESS_SPACE_GENERIC)
537559 {
538- // add metadata to avoid tagging when emitting
539- MDNode* N = MDNode::get (C, MDString::get (C, " generic.arith" ));
540- addrSpaceCastInst->setMetadata (" generic.arith" , N);
541- ASCInsts.push_back (addrSpaceCastInst);
542- modified = true ;
560+ Instruction* ptiUser = ptiInst->user_back ();
561+ // We only skip tags on generic pointers if there is an arithmetic operation
562+ // after the addrspacecast->ptrtoint.
563+ if (ptiUser->isBinaryOp () || ptiUser->isShift () || ptiUser->isBitwiseLogicOp ())
564+ {
565+ numUsesArith++;
566+ }
543567 }
544568 }
569+
570+ if (numUsesArith > 0 && numUsesArith == totalNumUses)
571+ {
572+ // Add metadata to avoid tagging when emitting addrspacecast
573+ MDNode* N = MDNode::get (C, MDString::get (C, " generic.arith" ));
574+ addrSpaceCastInst->setMetadata (" generic.arith" , N);
575+ ASCInsts.push_back (addrSpaceCastInst);
576+ modified = true ;
577+ }
545578 }
546579 else if (intToPtrInst && intToPtrInst->getAddressSpace () == ADDRESS_SPACE_GENERIC)
547580 {
0 commit comments