@@ -780,7 +780,8 @@ static void HandleInlinedEHPad(InvokeInst *II, BasicBlock *FirstNewBlock,
780780// / When inlining a call site that has !llvm.mem.parallel_loop_access,
781781// / !llvm.access.group, !alias.scope or !noalias metadata, that metadata should
782782// / be propagated to all memory-accessing cloned instructions.
783- static void PropagateCallSiteMetadata (CallBase &CB, ValueToValueMapTy &VMap) {
783+ static void PropagateCallSiteMetadata (CallBase &CB, Function::iterator FStart,
784+ Function::iterator FEnd) {
784785 MDNode *MemParallelLoopAccess =
785786 CB.getMetadata (LLVMContext::MD_mem_parallel_loop_access);
786787 MDNode *AccessGroup = CB.getMetadata (LLVMContext::MD_access_group);
@@ -789,41 +790,33 @@ static void PropagateCallSiteMetadata(CallBase &CB, ValueToValueMapTy &VMap) {
789790 if (!MemParallelLoopAccess && !AccessGroup && !AliasScope && !NoAlias)
790791 return ;
791792
792- for (ValueToValueMapTy::iterator VMI = VMap.begin (), VMIE = VMap.end ();
793- VMI != VMIE; ++VMI) {
794- // Check that key is an instruction, to skip the Argument mapping, which
795- // points to an instruction in the original function, not the inlined one.
796- if (!VMI->second || !isa<Instruction>(VMI->first ))
797- continue ;
798-
799- Instruction *NI = dyn_cast<Instruction>(VMI->second );
800- if (!NI)
801- continue ;
802-
803- // This metadata is only relevant for instructions that access memory.
804- if (!NI->mayReadOrWriteMemory ())
805- continue ;
793+ for (BasicBlock &BB : make_range (FStart, FEnd)) {
794+ for (Instruction &I : BB) {
795+ // This metadata is only relevant for instructions that access memory.
796+ if (!I.mayReadOrWriteMemory ())
797+ continue ;
806798
807- if (MemParallelLoopAccess) {
808- // TODO: This probably should not overwrite MemParalleLoopAccess.
809- MemParallelLoopAccess = MDNode::concatenate (
810- NI-> getMetadata (LLVMContext::MD_mem_parallel_loop_access),
811- MemParallelLoopAccess);
812- NI-> setMetadata (LLVMContext::MD_mem_parallel_loop_access,
799+ if (MemParallelLoopAccess) {
800+ // TODO: This probably should not overwrite MemParalleLoopAccess.
801+ MemParallelLoopAccess = MDNode::concatenate (
802+ I. getMetadata (LLVMContext::MD_mem_parallel_loop_access),
803+ MemParallelLoopAccess);
804+ I. setMetadata (LLVMContext::MD_mem_parallel_loop_access,
813805 MemParallelLoopAccess);
814- }
806+ }
815807
816- if (AccessGroup)
817- NI-> setMetadata (LLVMContext::MD_access_group, uniteAccessGroups (
818- NI-> getMetadata (LLVMContext::MD_access_group), AccessGroup));
808+ if (AccessGroup)
809+ I. setMetadata (LLVMContext::MD_access_group, uniteAccessGroups (
810+ I. getMetadata (LLVMContext::MD_access_group), AccessGroup));
819811
820- if (AliasScope)
821- NI-> setMetadata (LLVMContext::MD_alias_scope, MDNode::concatenate (
822- NI-> getMetadata (LLVMContext::MD_alias_scope), AliasScope));
812+ if (AliasScope)
813+ I. setMetadata (LLVMContext::MD_alias_scope, MDNode::concatenate (
814+ I. getMetadata (LLVMContext::MD_alias_scope), AliasScope));
823815
824- if (NoAlias)
825- NI->setMetadata (LLVMContext::MD_noalias, MDNode::concatenate (
826- NI->getMetadata (LLVMContext::MD_noalias), NoAlias));
816+ if (NoAlias)
817+ I.setMetadata (LLVMContext::MD_noalias, MDNode::concatenate (
818+ I.getMetadata (LLVMContext::MD_noalias), NoAlias));
819+ }
827820 }
828821}
829822
@@ -844,9 +837,9 @@ class ScopedAliasMetadataDeepCloner {
844837 // / subsequent remap() calls.
845838 void clone ();
846839
847- // / Remap instructions in the given VMap from the original to the cloned
840+ // / Remap instructions in the given range from the original to the cloned
848841 // / metadata.
849- void remap (ValueToValueMapTy &VMap );
842+ void remap (Function::iterator FStart, Function::iterator FEnd );
850843};
851844
852845ScopedAliasMetadataDeepCloner::ScopedAliasMetadataDeepCloner (
@@ -907,34 +900,27 @@ void ScopedAliasMetadataDeepCloner::clone() {
907900 }
908901}
909902
910- void ScopedAliasMetadataDeepCloner::remap (ValueToValueMapTy &VMap) {
903+ void ScopedAliasMetadataDeepCloner::remap (Function::iterator FStart,
904+ Function::iterator FEnd) {
911905 if (MDMap.empty ())
912906 return ; // Nothing to do.
913907
914- for (auto Entry : VMap) {
915- // Check that key is an instruction, to skip the Argument mapping, which
916- // points to an instruction in the original function, not the inlined one.
917- if (!Entry->second || !isa<Instruction>(Entry->first ))
918- continue ;
919-
920- Instruction *I = dyn_cast<Instruction>(Entry->second );
921- if (!I)
922- continue ;
923-
924- // Only update scopes when we find them in the map. If they are not, it is
925- // because we already handled that instruction before. This is faster than
926- // tracking which instructions we already updated.
927- if (MDNode *M = I->getMetadata (LLVMContext::MD_alias_scope))
928- if (MDNode *MNew = MDMap.lookup (M))
929- I->setMetadata (LLVMContext::MD_alias_scope, MNew);
930-
931- if (MDNode *M = I->getMetadata (LLVMContext::MD_noalias))
932- if (MDNode *MNew = MDMap.lookup (M))
933- I->setMetadata (LLVMContext::MD_noalias, MNew);
934-
935- if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(I))
936- if (MDNode *MNew = MDMap.lookup (Decl->getScopeList ()))
937- Decl->setScopeList (MNew);
908+ for (BasicBlock &BB : make_range (FStart, FEnd)) {
909+ for (Instruction &I : BB) {
910+ // TODO: The null checks for the MDMap.lookup() results should no longer
911+ // be necessary.
912+ if (MDNode *M = I.getMetadata (LLVMContext::MD_alias_scope))
913+ if (MDNode *MNew = MDMap.lookup (M))
914+ I.setMetadata (LLVMContext::MD_alias_scope, MNew);
915+
916+ if (MDNode *M = I.getMetadata (LLVMContext::MD_noalias))
917+ if (MDNode *MNew = MDMap.lookup (M))
918+ I.setMetadata (LLVMContext::MD_noalias, MNew);
919+
920+ if (auto *Decl = dyn_cast<NoAliasScopeDeclInst>(&I))
921+ if (MDNode *MNew = MDMap.lookup (Decl->getScopeList ()))
922+ Decl->setScopeList (MNew);
923+ }
938924 }
939925}
940926
@@ -1926,7 +1912,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
19261912
19271913 // Now clone the inlined noalias scope metadata.
19281914 SAMetadataCloner.clone ();
1929- SAMetadataCloner.remap (VMap );
1915+ SAMetadataCloner.remap (FirstNewBlock, Caller-> end () );
19301916
19311917 // Add noalias metadata if necessary.
19321918 AddAliasScopeMetadata (CB, VMap, DL, CalleeAAR);
@@ -1936,7 +1922,7 @@ llvm::InlineResult llvm::InlineFunction(CallBase &CB, InlineFunctionInfo &IFI,
19361922 AddReturnAttributes (CB, VMap);
19371923
19381924 // Propagate metadata on the callsite if necessary.
1939- PropagateCallSiteMetadata (CB, VMap );
1925+ PropagateCallSiteMetadata (CB, FirstNewBlock, Caller-> end () );
19401926
19411927 // Register any cloned assumptions.
19421928 if (IFI.GetAssumptionCache )
0 commit comments