+ E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
+ Value *ActualArg = *AI;
+
+ // When byval arguments actually inlined, we need to make the copy implied
+ // by them explicit. However, we don't do this if the callee is readonly
+ // or readnone, because the copy would be unneeded: the callee doesn't
+ // modify the struct.
+ if (CalledFunc->paramHasAttr(ArgNo+1, ParamAttr::ByVal) &&
+ !CalledFunc->onlyReadsMemory()) {
+ const Type *AggTy = cast<PointerType>(I->getType())->getElementType();
+ const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
+
+ // Create the alloca. If we have TargetData, use nice alignment.
+ unsigned Align = 1;
+ if (TD) Align = TD->getPrefTypeAlignment(AggTy);
+ Value *NewAlloca = new AllocaInst(AggTy, 0, Align, I->getName(),
+ Caller->begin()->begin());
+ // Emit a memcpy.
+ Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
+ Intrinsic::memcpy_i64);
+ Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
+ Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
+
+ Value *Size;
+ if (TD == 0)
+ Size = ConstantExpr::getSizeOf(AggTy);
+ else
+ Size = ConstantInt::get(Type::Int64Ty, TD->getTypeStoreSize(AggTy));
+
+ // Always generate a memcpy of alignment 1 here because we don't know
+ // the alignment of the src pointer. Other optimizations can infer
+ // better alignment.
+ Value *CallArgs[] = {
+ DestCast, SrcCast, Size, ConstantInt::get(Type::Int32Ty, 1)
+ };
+ CallInst *TheMemCpy =
+ new CallInst(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
+
+ // If we have a call graph, update it.
+ if (CG) {
+ CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn);
+ CallGraphNode *CallerNode = (*CG)[Caller];
+ CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN);
+ }
+
+ // Uses of the argument in the function should use our new alloca
+ // instead.
+ ActualArg = NewAlloca;
+ }
+
+ ValueMap[I] = ActualArg;
+ }