Hi,<div><br></div><div>This asserts in cases where memset is given a pointer to incomplete type. Testcase:</div><div><br></div><div><div>typedef unsigned long size_t;</div><div>typedef struct _snd_pcm_hw_params snd_pcm_hw_params_t;</div>
<div>size_t snd_pcm_hw_params_sizeof(void);</div><div>#define snd_pcm_hw_params_alloca(ptr) __snd_alloca(ptr, snd_pcm_hw_params)</div><div>#define __snd_alloca(ptr,type) do { *ptr = (type##_t *) alloca(type##_sizeof()); memset(*ptr, 0, type##_sizeof()); } while (0)</div>
<div>int sa_stream_open() {</div><div> snd_pcm_hw_params_t* hwparams;</div><div> snd_pcm_hw_params_alloca(&hwparams);</div><div>}</div><div><br></div><div>- Richard</div><br><div class="gmail_quote">On Fri, Mar 2, 2012 at 10:34 AM, Jay Foad <span dir="ltr"><<a href="mailto:jay.foad@gmail.com">jay.foad@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: foad<br>
Date: Fri Mar 2 12:34:30 2012<br>
New Revision: 151927<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=151927&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=151927&view=rev</a><br>
Log:<br>
PR12094: Set the alignment of memory intrinsic instructions based on the<br>
types of the pointer arguments.<br>
<br>
Modified:<br>
cfe/trunk/lib/CodeGen/CGBuiltin.cpp<br>
cfe/trunk/lib/CodeGen/CodeGenFunction.h<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=151927&r1=151926&r2=151927&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=151927&r1=151926&r2=151927&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)<br>
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Fri Mar 2 12:34:30 2012<br>
@@ -540,7 +540,8 @@<br>
case Builtin::BI__builtin_bzero: {<br>
Value *Address = EmitScalarExpr(E->getArg(0));<br>
Value *SizeVal = EmitScalarExpr(E->getArg(1));<br>
- Builder.CreateMemSet(Address, Builder.getInt8(0), SizeVal, 1, false);<br>
+ unsigned Align = GetPointeeAlignment(E->getArg(0));<br>
+ Builder.CreateMemSet(Address, Builder.getInt8(0), SizeVal, Align, false);<br>
return RValue::get(Address);<br>
}<br>
case Builtin::BImemcpy:<br>
@@ -548,7 +549,9 @@<br>
Value *Address = EmitScalarExpr(E->getArg(0));<br>
Value *SrcAddr = EmitScalarExpr(E->getArg(1));<br>
Value *SizeVal = EmitScalarExpr(E->getArg(2));<br>
- Builder.CreateMemCpy(Address, SrcAddr, SizeVal, 1, false);<br>
+ unsigned Align = std::min(GetPointeeAlignment(E->getArg(0)),<br>
+ GetPointeeAlignment(E->getArg(1)));<br>
+ Builder.CreateMemCpy(Address, SrcAddr, SizeVal, Align, false);<br>
return RValue::get(Address);<br>
}<br>
<br>
@@ -563,7 +566,9 @@<br>
Value *Dest = EmitScalarExpr(E->getArg(0));<br>
Value *Src = EmitScalarExpr(E->getArg(1));<br>
Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);<br>
- Builder.CreateMemCpy(Dest, Src, SizeVal, 1, false);<br>
+ unsigned Align = std::min(GetPointeeAlignment(E->getArg(0)),<br>
+ GetPointeeAlignment(E->getArg(1)));<br>
+ Builder.CreateMemCpy(Dest, Src, SizeVal, Align, false);<br>
return RValue::get(Dest);<br>
}<br>
<br>
@@ -587,7 +592,9 @@<br>
Value *Dest = EmitScalarExpr(E->getArg(0));<br>
Value *Src = EmitScalarExpr(E->getArg(1));<br>
Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);<br>
- Builder.CreateMemMove(Dest, Src, SizeVal, 1, false);<br>
+ unsigned Align = std::min(GetPointeeAlignment(E->getArg(0)),<br>
+ GetPointeeAlignment(E->getArg(1)));<br>
+ Builder.CreateMemMove(Dest, Src, SizeVal, Align, false);<br>
return RValue::get(Dest);<br>
}<br>
<br>
@@ -596,7 +603,9 @@<br>
Value *Address = EmitScalarExpr(E->getArg(0));<br>
Value *SrcAddr = EmitScalarExpr(E->getArg(1));<br>
Value *SizeVal = EmitScalarExpr(E->getArg(2));<br>
- Builder.CreateMemMove(Address, SrcAddr, SizeVal, 1, false);<br>
+ unsigned Align = std::min(GetPointeeAlignment(E->getArg(0)),<br>
+ GetPointeeAlignment(E->getArg(1)));<br>
+ Builder.CreateMemMove(Address, SrcAddr, SizeVal, Align, false);<br>
return RValue::get(Address);<br>
}<br>
case Builtin::BImemset:<br>
@@ -605,7 +614,8 @@<br>
Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),<br>
Builder.getInt8Ty());<br>
Value *SizeVal = EmitScalarExpr(E->getArg(2));<br>
- Builder.CreateMemSet(Address, ByteVal, SizeVal, 1, false);<br>
+ unsigned Align = GetPointeeAlignment(E->getArg(0));<br>
+ Builder.CreateMemSet(Address, ByteVal, SizeVal, Align, false);<br>
return RValue::get(Address);<br>
}<br>
case Builtin::BI__builtin___memset_chk: {<br>
@@ -620,7 +630,8 @@<br>
Value *ByteVal = Builder.CreateTrunc(EmitScalarExpr(E->getArg(1)),<br>
Builder.getInt8Ty());<br>
Value *SizeVal = llvm::ConstantInt::get(Builder.getContext(), Size);<br>
- Builder.CreateMemSet(Address, ByteVal, SizeVal, 1, false);<br>
+ unsigned Align = GetPointeeAlignment(E->getArg(0));<br>
+ Builder.CreateMemSet(Address, ByteVal, SizeVal, Align, false);<br>
<br>
return RValue::get(Address);<br>
}<br>
@@ -1248,12 +1259,12 @@<br>
/// GetPointeeAlignment - Given an expression with a pointer type, find the<br>
/// alignment of the type referenced by the pointer. Skip over implicit<br>
/// casts.<br>
-static Value *GetPointeeAlignment(CodeGenFunction &CGF, const Expr *Addr) {<br>
+unsigned CodeGenFunction::GetPointeeAlignment(const Expr *Addr) {<br>
unsigned Align = 1;<br>
// Check if the type is a pointer. The implicit cast operand might not be.<br>
while (Addr->getType()->isPointerType()) {<br>
QualType PtTy = Addr->getType()->getPointeeType();<br>
- unsigned NewA = CGF.getContext().getTypeAlignInChars(PtTy).getQuantity();<br>
+ unsigned NewA = getContext().getTypeAlignInChars(PtTy).getQuantity();<br>
if (NewA > Align)<br>
Align = NewA;<br>
<br>
@@ -1264,7 +1275,14 @@<br>
}<br>
break;<br>
}<br>
- return llvm::ConstantInt::get(CGF.Int32Ty, Align);<br>
+ return Align;<br>
+}<br>
+<br>
+/// GetPointeeAlignmentValue - Given an expression with a pointer type, find<br>
+/// the alignment of the type referenced by the pointer. Skip over implicit<br>
+/// casts. Return the alignment as an llvm::Value.<br>
+Value *CodeGenFunction::GetPointeeAlignmentValue(const Expr *Addr) {<br>
+ return llvm::ConstantInt::get(Int32Ty, GetPointeeAlignment(Addr));<br>
}<br>
<br>
Value *CodeGenFunction::EmitARMBuiltinExpr(unsigned BuiltinID,<br>
@@ -1510,7 +1528,7 @@<br>
return EmitNeonCall(CGM.getIntrinsic(Int, Ty), Ops, "vhsub");<br>
case ARM::BI__builtin_neon_vld1_v:<br>
case ARM::BI__builtin_neon_vld1q_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vld1, Ty),<br>
Ops, "vld1");<br>
case ARM::BI__builtin_neon_vld1_lane_v:<br>
@@ -1519,7 +1537,7 @@<br>
Ty = llvm::PointerType::getUnqual(VTy->getElementType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
LoadInst *Ld = Builder.CreateLoad(Ops[0]);<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(0));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(0));<br>
Ld->setAlignment(cast<ConstantInt>(Align)->getZExtValue());<br>
return Builder.CreateInsertElement(Ops[1], Ld, Ops[2], "vld1_lane");<br>
}<br>
@@ -1529,7 +1547,7 @@<br>
Ty = llvm::PointerType::getUnqual(VTy->getElementType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
LoadInst *Ld = Builder.CreateLoad(Ops[0]);<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(0));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(0));<br>
Ld->setAlignment(cast<ConstantInt>(Align)->getZExtValue());<br>
llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);<br>
Ops[0] = Builder.CreateInsertElement(V, Ld, CI);<br>
@@ -1538,7 +1556,7 @@<br>
case ARM::BI__builtin_neon_vld2_v:<br>
case ARM::BI__builtin_neon_vld2q_v: {<br>
Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2, Ty);<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(1));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(1));<br>
Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld2");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1547,7 +1565,7 @@<br>
case ARM::BI__builtin_neon_vld3_v:<br>
case ARM::BI__builtin_neon_vld3q_v: {<br>
Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld3, Ty);<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(1));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(1));<br>
Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld3");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1556,7 +1574,7 @@<br>
case ARM::BI__builtin_neon_vld4_v:<br>
case ARM::BI__builtin_neon_vld4q_v: {<br>
Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld4, Ty);<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(1));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(1));<br>
Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld4");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1567,7 +1585,7 @@<br>
Function *F = CGM.getIntrinsic(Intrinsic::arm_neon_vld2lane, Ty);<br>
Ops[2] = Builder.CreateBitCast(Ops[2], Ty);<br>
Ops[3] = Builder.CreateBitCast(Ops[3], Ty);<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(1)));<br>
Ops[1] = Builder.CreateCall(F, makeArrayRef(Ops).slice(1), "vld2_lane");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1579,7 +1597,7 @@<br>
Ops[2] = Builder.CreateBitCast(Ops[2], Ty);<br>
Ops[3] = Builder.CreateBitCast(Ops[3], Ty);<br>
Ops[4] = Builder.CreateBitCast(Ops[4], Ty);<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(1)));<br>
Ops[1] = Builder.CreateCall(F, makeArrayRef(Ops).slice(1), "vld3_lane");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1592,7 +1610,7 @@<br>
Ops[3] = Builder.CreateBitCast(Ops[3], Ty);<br>
Ops[4] = Builder.CreateBitCast(Ops[4], Ty);<br>
Ops[5] = Builder.CreateBitCast(Ops[5], Ty);<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(1)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(1)));<br>
Ops[1] = Builder.CreateCall(F, makeArrayRef(Ops).slice(1), "vld3_lane");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1616,7 +1634,7 @@<br>
default: llvm_unreachable("unknown vld_dup intrinsic?");<br>
}<br>
Function *F = CGM.getIntrinsic(Int, Ty);<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(1));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(1));<br>
Ops[1] = Builder.CreateCall2(F, Ops[1], Align, "vld_dup");<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
Ops[0] = Builder.CreateBitCast(Ops[0], Ty);<br>
@@ -1643,7 +1661,7 @@<br>
<br>
llvm::Constant *CI = ConstantInt::get(Int32Ty, 0);<br>
Args.push_back(CI);<br>
- Args.push_back(GetPointeeAlignment(*this, E->getArg(1)));<br>
+ Args.push_back(GetPointeeAlignmentValue(E->getArg(1)));<br>
<br>
Ops[1] = Builder.CreateCall(F, Args, "vld_dup");<br>
// splat lane 0 to all elts in each vector of the result.<br>
@@ -1876,7 +1894,7 @@<br>
return Builder.CreateAdd(Ops[0], Ops[1]);<br>
case ARM::BI__builtin_neon_vst1_v:<br>
case ARM::BI__builtin_neon_vst1q_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst1, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vst1_lane_v:<br>
@@ -1886,38 +1904,38 @@<br>
Ty = llvm::PointerType::getUnqual(Ops[1]->getType());<br>
StoreInst *St = Builder.CreateStore(Ops[1],<br>
Builder.CreateBitCast(Ops[0], Ty));<br>
- Value *Align = GetPointeeAlignment(*this, E->getArg(0));<br>
+ Value *Align = GetPointeeAlignmentValue(E->getArg(0));<br>
St->setAlignment(cast<ConstantInt>(Align)->getZExtValue());<br>
return St;<br>
}<br>
case ARM::BI__builtin_neon_vst2_v:<br>
case ARM::BI__builtin_neon_vst2q_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vst2_lane_v:<br>
case ARM::BI__builtin_neon_vst2q_lane_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst2lane, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vst3_v:<br>
case ARM::BI__builtin_neon_vst3q_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vst3_lane_v:<br>
case ARM::BI__builtin_neon_vst3q_lane_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst3lane, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vst4_v:<br>
case ARM::BI__builtin_neon_vst4q_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vst4_lane_v:<br>
case ARM::BI__builtin_neon_vst4q_lane_v:<br>
- Ops.push_back(GetPointeeAlignment(*this, E->getArg(0)));<br>
+ Ops.push_back(GetPointeeAlignmentValue(E->getArg(0)));<br>
return EmitNeonCall(CGM.getIntrinsic(Intrinsic::arm_neon_vst4lane, Ty),<br>
Ops, "");<br>
case ARM::BI__builtin_neon_vsubhn_v:<br>
<br>
Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=151927&r1=151926&r2=151927&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=151927&r1=151926&r2=151927&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)<br>
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Mar 2 12:34:30 2012<br>
@@ -2581,6 +2581,16 @@<br>
const AutoVarEmission &emission);<br>
<br>
void AddObjCARCExceptionMetadata(llvm::Instruction *Inst);<br>
+<br>
+ /// GetPointeeAlignment - Given an expression with a pointer type, find the<br>
+ /// alignment of the type referenced by the pointer. Skip over implicit<br>
+ /// casts.<br>
+ unsigned GetPointeeAlignment(const Expr *Addr);<br>
+<br>
+ /// GetPointeeAlignmentValue - Given an expression with a pointer type, find<br>
+ /// the alignment of the type referenced by the pointer. Skip over implicit<br>
+ /// casts. Return the alignment as an llvm::Value.<br>
+ llvm::Value *GetPointeeAlignmentValue(const Expr *Addr);<br>
};<br>
<br>
/// Helper class with most of the code for saving a value for a<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>