simplify some code. If we can infer alignment for source and dest that are
authorChris Lattner <sabre@nondot.org>
Sun, 13 Jan 2008 22:30:28 +0000 (22:30 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 13 Jan 2008 22:30:28 +0000 (22:30 +0000)
greater than memcpy alignment, and if we lower to load/store, use the best
alignment info we have.

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

lib/Transforms/Scalar/InstructionCombining.cpp

index 82df3fc7a4ac60ee42a0c23450bc97deb7f59787..fcca06dd12bdaaf7f7483c259a0af0136f0713db 100644 (file)
@@ -7854,34 +7854,35 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
     // If we can determine a pointer alignment that is bigger than currently
     // set, update the alignment.
     if (isa<MemCpyInst>(MI) || isa<MemMoveInst>(MI)) {
-      unsigned Alignment1 = GetOrEnforceKnownAlignment(MI->getOperand(1), TD);
-      unsigned Alignment2 = GetOrEnforceKnownAlignment(MI->getOperand(2), TD);
-      unsigned Align = std::min(Alignment1, Alignment2);
-      if (MI->getAlignment()->getZExtValue() < Align) {
-        MI->setAlignment(ConstantInt::get(Type::Int32Ty, Align));
+      unsigned DstAlign = GetOrEnforceKnownAlignment(MI->getOperand(1), TD);
+      unsigned SrcAlign = GetOrEnforceKnownAlignment(MI->getOperand(2), TD);
+      unsigned MinAlign = std::min(DstAlign, SrcAlign);
+      unsigned CopyAlign = MI->getAlignment()->getZExtValue();
+      if (CopyAlign < MinAlign) {
+        MI->setAlignment(ConstantInt::get(Type::Int32Ty, MinAlign));
         Changed = true;
       }
-
+      
       // If MemCpyInst length is 1/2/4/8 bytes then replace memcpy with
       // load/store.
-      ConstantInt *MemOpLength = dyn_cast<ConstantInt>(CI.getOperand(3));
-      if (MemOpLength) {
+      if (ConstantInt *MemOpLength = dyn_cast<ConstantInt>(CI.getOperand(3))) {
+        // Source and destination pointer types are always "i8*" for intrinsic.
+        //   If Size is 8 then use Int64Ty
+        //   If Size is 4 then use Int32Ty
+        //   If Size is 2 then use Int16Ty
+        //   If Size is 1 then use Int8Ty
         unsigned Size = MemOpLength->getZExtValue();
-        unsigned Align = cast<ConstantInt>(CI.getOperand(4))->getZExtValue();
-        PointerType *NewPtrTy = NULL;
-        // Destination pointer type is always i8 *
-        // If Size is 8 then use Int64Ty
-        // If Size is 4 then use Int32Ty
-        // If Size is 2 then use Int16Ty
-        // If Size is 1 then use Int8Ty
-        if (Size && Size <=8 && !(Size&(Size-1)))
-          NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3));
-
-        if (NewPtrTy) {
+        if (Size && Size <= 8 && !(Size&(Size-1))) {
+          Type *NewPtrTy = PointerType::getUnqual(IntegerType::get(Size<<3));
+          // If the memcpy/memmove provides better alignment info than we can
+          // infer, use it.
+          SrcAlign = std::max(SrcAlign, CopyAlign);
+          DstAlign = std::max(DstAlign, CopyAlign);
+          
           Value *Src = InsertBitCastBefore(CI.getOperand(2), NewPtrTy, CI);
           Value *Dest = InsertBitCastBefore(CI.getOperand(1), NewPtrTy, CI);
-          Value *L = new LoadInst(Src, "tmp", false, Align, &CI);
-          Value *NS = new StoreInst(L, Dest, false, Align, &CI);
+          Value *L = new LoadInst(Src, "tmp", false, SrcAlign, &CI);
+          Value *NS = new StoreInst(L, Dest, false, DstAlign, &CI);
           CI.replaceAllUsesWith(NS);
           Changed = true;
           return EraseInstFromFunction(CI);