llvm.org GIT mirror llvm / 891835a
CloneFunction: Clone all attributes, including the CC Summary: Tested with a unit test because we don't appear to have any transforms that use this other than ASan, I think. Fixes PR17935. Reviewers: nicholas CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D3194 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204866 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 5 years ago
2 changed file(s) with 41 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
8888 assert(VMap.count(I) && "No mapping from source argument specified!");
8989 #endif
9090
91 // Copy all attributes other than those stored in the AttributeSet. We need
92 // to remap the parameter indices of the AttributeSet.
93 AttributeSet NewAttrs = NewFunc->getAttributes();
94 NewFunc->copyAttributesFrom(OldFunc);
95 NewFunc->setAttributes(NewAttrs);
96
9197 AttributeSet OldAttrs = OldFunc->getAttributes();
9298 // Clone any argument attributes that are present in the VMap.
93 for (Function::const_arg_iterator I = OldFunc->arg_begin(),
94 E = OldFunc->arg_end();
95 I != E; ++I)
96 if (Argument *Anew = dyn_cast(VMap[I])) {
99 for (const Argument &OldArg : OldFunc->args())
100 if (Argument *NewArg = dyn_cast(VMap[&OldArg])) {
97101 AttributeSet attrs =
98 OldAttrs.getParamAttributes(I->getArgNo() + 1);
102 OldAttrs.getParamAttributes(OldArg.getArgNo() + 1);
99103 if (attrs.getNumSlots() > 0)
100 Anew->addAttr(attrs);
101 }
102
103 NewFunc->setAttributes(NewFunc->getAttributes()
104 .addAttributes(NewFunc->getContext(),
105 AttributeSet::ReturnIndex,
106 OldAttrs.getRetAttributes()));
107 NewFunc->setAttributes(NewFunc->getAttributes()
108 .addAttributes(NewFunc->getContext(),
109 AttributeSet::FunctionIndex,
110 OldAttrs.getFnAttributes()));
104 NewArg->addAttr(attrs);
105 }
106
107 NewFunc->setAttributes(
108 NewFunc->getAttributes()
109 .addAttributes(NewFunc->getContext(), AttributeSet::ReturnIndex,
110 OldAttrs.getRetAttributes())
111 .addAttributes(NewFunc->getContext(), AttributeSet::FunctionIndex,
112 OldAttrs.getFnAttributes()));
111113
112114 // Loop over all of the basic blocks in the function, cloning them as
113115 // appropriate. Note that we save BE this way in order to handle cloning of
179179 delete F2;
180180 }
181181
182 TEST_F(CloneInstruction, CallingConvention) {
183 Type *ArgTy1[] = { Type::getInt32PtrTy(context) };
184 FunctionType *FT1 = FunctionType::get(Type::getVoidTy(context), ArgTy1, false);
185
186 Function *F1 = Function::Create(FT1, Function::ExternalLinkage);
187 F1->setCallingConv(CallingConv::Cold);
188 BasicBlock *BB = BasicBlock::Create(context, "", F1);
189 IRBuilder<> Builder(BB);
190 Builder.CreateRetVoid();
191
192 Function *F2 = Function::Create(FT1, Function::ExternalLinkage);
193
194 SmallVector Returns;
195 ValueToValueMapTy VMap;
196 VMap[F1->arg_begin()] = F2->arg_begin();
197
198 CloneFunctionInto(F2, F1, VMap, false, Returns);
199 EXPECT_EQ(CallingConv::Cold, F2->getCallingConv());
200
201 delete F1;
202 delete F2;
203 }
204
182205 class CloneFunc : public ::testing::Test {
183206 protected:
184207 virtual void SetUp() {