llvm.org GIT mirror llvm / c93adca
When inlining a functino with a byval argument, make an explicit copy of it in case the callee modifies the struct. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45853 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
2 changed file(s) with 85 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
1717 #include "llvm/Module.h"
1818 #include "llvm/Instructions.h"
1919 #include "llvm/Intrinsics.h"
20 #include "llvm/ParameterAttributes.h"
2021 #include "llvm/Analysis/CallGraph.h"
22 #include "llvm/Target/TargetData.h"
2123 #include "llvm/ADT/SmallVector.h"
2224 #include "llvm/Support/CallSite.h"
2325 using namespace llvm;
200202 BasicBlock *OrigBB = TheCall->getParent();
201203 Function *Caller = OrigBB->getParent();
202204
203
204205 // GC poses two hazards to inlining, which only occur when the callee has GC:
205206 // 1. If the caller has no GC, then the callee's GC must be propagated to the
206207 // caller.
212213 return false;
213214 }
214215
215
216216 // Get an iterator to the last basic block in the function, which will have
217217 // the new function inlined after it.
218218 //
227227 { // Scope to destroy ValueMap after cloning.
228228 DenseMap ValueMap;
229229
230 // Calculate the vector of arguments to pass into the function cloner, which
231 // matches up the formal to the actual argument values.
232230 assert(std::distance(CalledFunc->arg_begin(), CalledFunc->arg_end()) ==
233231 std::distance(CS.arg_begin(), CS.arg_end()) &&
234232 "No varargs calls can be inlined!");
233
234 // Calculate the vector of arguments to pass into the function cloner, which
235 // matches up the formal to the actual argument values.
235236 CallSite::arg_iterator AI = CS.arg_begin();
237 unsigned ArgNo = 0;
236238 for (Function::const_arg_iterator I = CalledFunc->arg_begin(),
237 E = CalledFunc->arg_end(); I != E; ++I, ++AI)
238 ValueMap[I] = *AI;
239 E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) {
240 Value *ActualArg = *AI;
241
242 // When byval arguments actually inlined, we need to make the copy implied
243 // by them actually explicit.
244 // TODO: If we know that the callee never modifies the struct, we can
245 // remove this copy.
246 if (CalledFunc->paramHasAttr(ArgNo+1, ParamAttr::ByVal)) {
247 const Type *AggTy = cast(I->getType())->getElementType();
248 const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty);
249
250 // Create the alloca. If we have TargetData, use nice alignment.
251 unsigned Align = 1;
252 if (TD) Align = TD->getPrefTypeAlignment(AggTy);
253 Value *NewAlloca = new AllocaInst(AggTy, 0, Align, I->getName(),
254 Caller->begin()->begin());
255 // Emit a memcpy.
256 Function *MemCpyFn = Intrinsic::getDeclaration(Caller->getParent(),
257 Intrinsic::memcpy_i64);
258 Value *DestCast = new BitCastInst(NewAlloca, VoidPtrTy, "tmp", TheCall);
259 Value *SrcCast = new BitCastInst(*AI, VoidPtrTy, "tmp", TheCall);
260
261 Value *Size;
262 if (TD == 0)
263 Size = ConstantExpr::getSizeOf(AggTy);
264 else
265 Size = ConstantInt::get(Type::Int64Ty, TD->getTypeStoreSize(AggTy));
266
267 // Always generate a memcpy of alignment 1 here because we don't know
268 // the alignment of the src pointer. Other optimizations can infer
269 // better alignment.
270 Value *CallArgs[] = {
271 DestCast, SrcCast, Size, ConstantInt::get(Type::Int32Ty, 1)
272 };
273 CallInst *TheMemCpy =
274 new CallInst(MemCpyFn, CallArgs, CallArgs+4, "", TheCall);
275
276 // If we have a call graph, update it.
277 if (CG) {
278 CallGraphNode *MemCpyCGN = CG->getOrInsertFunction(MemCpyFn);
279 CallGraphNode *CallerNode = (*CG)[Caller];
280 CallerNode->addCalledFunction(TheMemCpy, MemCpyCGN);
281 }
282
283 // Uses of the argument in the function should use our new alloca
284 // instead.
285 ActualArg = NewAlloca;
286 }
287
288 ValueMap[I] = ActualArg;
289 }
239290
240291 // We want the inliner to prune the code as it copies. We would LOVE to
241292 // have no dead or constant instructions leftover after inlining occurs
0 ; RUN: llvm-as < %s | opt -inline | llvm-dis | grep {llvm.memcpy}
1
2 ; Inlining a byval struct should cause an explicit copy into an alloca.
3
4 %struct.ss = type { i32, i64 }
5 @.str = internal constant [10 x i8] c"%d, %lld\0A\00" ; <[10 x i8]*> [#uses=1]
6
7 define internal void @f(%struct.ss* byval %b) nounwind {
8 entry:
9 %tmp = getelementptr %struct.ss* %b, i32 0, i32 0 ; [#uses=2]
10 %tmp1 = load i32* %tmp, align 4 ; [#uses=1]
11 %tmp2 = add i32 %tmp1, 1 ; [#uses=1]
12 store i32 %tmp2, i32* %tmp, align 4
13 ret void
14 }
15
16 declare i32 @printf(i8*, ...) nounwind
17
18 define i32 @main() nounwind {
19 entry:
20 %S = alloca %struct.ss ; <%struct.ss*> [#uses=4]
21 %tmp1 = getelementptr %struct.ss* %S, i32 0, i32 0 ; [#uses=1]
22 store i32 1, i32* %tmp1, align 8
23 %tmp4 = getelementptr %struct.ss* %S, i32 0, i32 1 ; [#uses=1]
24 store i64 2, i64* %tmp4, align 4
25 call void @f( %struct.ss* byval %S ) nounwind
26 ret i32 0
27 }