Fix the other half of the alignment changing issue by making sure that the
authorEric Christopher <echristo@apple.com>
Fri, 1 Oct 2010 09:02:05 +0000 (09:02 +0000)
committerEric Christopher <echristo@apple.com>
Fri, 1 Oct 2010 09:02:05 +0000 (09:02 +0000)
memcpy alignment is the minimum of the incoming alignments.

Fixes PR 8266.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@115305 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/MemCpyOptimizer.cpp

index f94c9e22576eacd9c6f666f5b44081092cdd3180..4172b345c6111e95b9a4647f6fe3dd5cd255588e 100644 (file)
@@ -697,13 +697,18 @@ bool MemCpyOpt::processMemCpy(MemCpyInst *M) {
                                  M->getParent()->getParent()->getParent(),
                                  M->getIntrinsicID(), ArgTys, 3);
     
-  // Make sure to use the alignment of the source since we're changing the
-  // location we're reading from.
+  // Make sure to use the lesser of the alignment of the source and the dest
+  // since we're changing where we're reading from, but don't want to increase
+  // the alignment past what can be read from or written to.
   // TODO: Is this worth it if we're creating a less aligned memcpy? For
   // example we could be moving from movaps -> movq on x86.
+  unsigned Align = std::min(MDep->getAlignmentCst()->getZExtValue(),
+                            M->getAlignmentCst()->getZExtValue());
+  LLVMContext &Context = M->getContext();
+  ConstantInt *AlignCI = ConstantInt::get(Type::getInt32Ty(Context), Align);
   Value *Args[5] = {
     M->getRawDest(), MDep->getRawSource(), M->getLength(),
-    MDep->getAlignmentCst(), M->getVolatileCst()
+    AlignCI, M->getVolatileCst()
   };
   CallInst *C = CallInst::Create(MemCpyFun, Args, Args+5, "", M);