r228061 - CodeGen: Copy-ctorm must obey the destination's alignment requirement

David Majnemer david.majnemer at gmail.com
Tue Feb 3 15:04:06 PST 2015


Author: majnemer
Date: Tue Feb  3 17:04:06 2015
New Revision: 228061

URL: http://llvm.org/viewvc/llvm-project?rev=228061&view=rev
Log:
CodeGen: Copy-ctorm must obey the destination's alignment requirement

We would synthesize memcpy intrinsics when emitting calls to trivial C++
constructors but we wouldn't take into account the alignment of the
destination.

Modified:
    cfe/trunk/lib/CodeGen/CGClass.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp

Modified: cfe/trunk/lib/CodeGen/CGClass.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGClass.cpp?rev=228061&r1=228060&r2=228061&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGClass.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGClass.cpp Tue Feb  3 17:04:06 2015
@@ -1747,9 +1747,10 @@ void CodeGenFunction::EmitCXXConstructor
            "trivial 1-arg ctor not a copy/move ctor");
 
     const Expr *Arg = E->getArg(0);
-    QualType Ty = Arg->getType();
+    QualType SrcTy = Arg->getType();
     llvm::Value *Src = EmitLValue(Arg).getAddress();
-    EmitAggregateCopy(This, Src, Ty);
+    QualType DestTy = getContext().getTypeDeclType(D->getParent());
+    EmitAggregateCopyCtor(This, Src, DestTy, SrcTy);
     return;
   }
 
@@ -1789,7 +1790,9 @@ CodeGenFunction::EmitSynthesizedCXXCopyC
     assert(E->getNumArgs() == 1 && "unexpected argcount for trivial ctor");
     assert(D->isCopyOrMoveConstructor() &&
            "trivial 1-arg ctor not a copy/move ctor");
-    EmitAggregateCopy(This, Src, E->arg_begin()->getType());
+    EmitAggregateCopyCtor(This, Src,
+                          getContext().getTypeDeclType(D->getParent()),
+                          E->arg_begin()->getType());
     return;
   }
   llvm::Value *Callee = CGM.getAddrOfCXXStructor(D, StructorType::Complete);

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=228061&r1=228060&r2=228061&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Tue Feb  3 17:04:06 2015
@@ -1559,6 +1559,15 @@ public:
                       true);
   }
 
+  void EmitAggregateCopyCtor(llvm::Value *DestPtr, llvm::Value *SrcPtr,
+                           QualType DestTy, QualType SrcTy) {
+    CharUnits DestTypeAlign = getContext().getTypeAlignInChars(DestTy);
+    CharUnits SrcTypeAlign = getContext().getTypeAlignInChars(SrcTy);
+    EmitAggregateCopy(DestPtr, SrcPtr, SrcTy, /*IsVolatile=*/false,
+                      std::min(DestTypeAlign, SrcTypeAlign),
+                      /*IsAssignment=*/false);
+  }
+
   /// EmitAggregateCopy - Emit an aggregate copy.
   ///
   /// \param isVolatile - True iff either the source or the destination is

Modified: cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp?rev=228061&r1=228060&r2=228061&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/copy-constructor-elim.cpp Tue Feb  3 17:04:06 2015
@@ -43,3 +43,17 @@ int main() {
   S s;
   Call(s);
 }
+
+struct V {
+  int x;
+};
+
+typedef V V_over_aligned __attribute((aligned(8)));
+extern const V_over_aligned gv1 = {};
+
+extern "C" V f() { return gv1; }
+
+// Make sure that we obey the destination's alignment requirements when emitting
+// the copy.
+// CHECK-LABEL: define i32 @f()
+// CHECK:   call void @llvm.memcpy.p0i8.p0i8.i64({{.*}}, i8* bitcast (%struct.V* @gv1 to i8*), i64 4, i32 4, i1 false)





More information about the cfe-commits mailing list