[llvm-commits] [llvm] r122236 - in /llvm/trunk: lib/Transforms/Utils/InlineFunction.cpp test/Transforms/Inline/byval.ll

Chris Lattner sabre at nondot.org
Mon Dec 20 00:10:40 PST 2010


Author: lattner
Date: Mon Dec 20 02:10:40 2010
New Revision: 122236

URL: http://llvm.org/viewvc/llvm-project?rev=122236&view=rev
Log:
when eliding a byval copy due to inlining a readonly function, we have
to make sure that the reused alloca has sufficient alignment.

Modified:
    llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp
    llvm/trunk/test/Transforms/Inline/byval.ll

Modified: llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp?rev=122236&r1=122235&r2=122236&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/InlineFunction.cpp Mon Dec 20 02:10:40 2010
@@ -229,17 +229,56 @@
   CallerNode->removeCallEdgeFor(CS);
 }
 
+/// HandleByValArgument - When inlining a call site that has a byval argument,
+/// we have to make the implicit memcpy explicit by adding it.
 static Value *HandleByValArgument(Value *Arg, Instruction *TheCall,
                                   const Function *CalledFunc,
                                   InlineFunctionInfo &IFI,
                                   unsigned ByValAlignment) {
-  if (CalledFunc->onlyReadsMemory())
-    return Arg;
+  const Type *AggTy = cast<PointerType>(Arg->getType())->getElementType();
+
+  // If the called function is readonly, then it could not mutate the caller's
+  // copy of the byval'd memory.  In this case, it is safe to elide the copy and
+  // temporary.
+  if (CalledFunc->onlyReadsMemory()) {
+    // If the byval argument has a specified alignment that is greater than the
+    // passed in pointer, then we either have to round up the input pointer or
+    // give up on this transformation.
+    if (ByValAlignment <= 1)  // 0 = unspecified, 1 = no particular alignment.
+      return Arg;
+
+    // See if the argument is a (bitcasted) pointer to an alloca.  If so, we can
+    // round up the alloca if needed.
+    if (AllocaInst *AI = dyn_cast<AllocaInst>(Arg->stripPointerCasts())) {
+      unsigned AIAlign = AI->getAlignment();
+      
+      // If the alloca is known at least aligned as much as the byval, we can do
+      // this optimization.
+      if (AIAlign >= ByValAlignment)
+        return Arg;
+      
+      // If the alloca has a specified alignment that is less than the byval,
+      // then we can safely bump it up.
+      if (AIAlign) {
+        AI->setAlignment(ByValAlignment);
+        return Arg;
+      }
+      
+      // If the alignment has an unspecified alignment, then we can only modify
+      // it if we have TD information.  Doing so without TD info could end up
+      // with us rounding the alignment *down* accidentally, which is badness.
+      if (IFI.TD) {
+        AIAlign = std::max(ByValAlignment, IFI.TD->getPrefTypeAlignment(AggTy));
+        AI->setAlignment(AIAlign);
+        return Arg;
+      }
+    }
+    
+    // Otherwise, we have to make a memcpy to get a safe alignment, pretty lame.
+  }
   
   LLVMContext &Context = Arg->getContext();
 
-  
-  const Type *AggTy = cast<PointerType>(Arg->getType())->getElementType();
   const Type *VoidPtrTy = Type::getInt8PtrTy(Context);
   
   // Create the alloca.  If we have TargetData, use nice alignment.

Modified: llvm/trunk/test/Transforms/Inline/byval.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Inline/byval.ll?rev=122236&r1=122235&r2=122236&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Inline/byval.ll (original)
+++ llvm/trunk/test/Transforms/Inline/byval.ll Mon Dec 20 02:10:40 2010
@@ -81,3 +81,26 @@
 ; CHECK: call void @g3(%struct.ss* %S1)
 ; CHECK: ret void
 }
+
+
+; Inlining a byval struct should NOT cause an explicit copy 
+; into an alloca if the function is readonly, but should increase an alloca's
+; alignment to satisfy an explicit alignment request.
+
+define internal i32 @f4(%struct.ss* byval align 64 %b) nounwind readonly {
+        call void @g3(%struct.ss* %b)
+	ret i32 4
+}
+
+define i32 @test4() nounwind  {
+entry:
+	%S = alloca %struct.ss, align 2		; <%struct.ss*> [#uses=4]
+	%X = call i32 @f4( %struct.ss* byval align 64 %S ) nounwind 
+	ret i32 %X
+; CHECK: @test4()
+; CHECK: %S = alloca %struct.ss, align 64
+; CHECK-NOT: call void @llvm.memcpy
+; CHECK: call void @g3
+; CHECK: ret i32 4
+}
+





More information about the llvm-commits mailing list