From 789a1597250e57d7f35f253467165913c68979ad Mon Sep 17 00:00:00 2001 From: John McCall Date: Tue, 26 Oct 2010 22:09:15 +0000 Subject: Extract procedures to do scalar-to-memory and memory-to-scalar conversions in IR gen, and use those to fix a correctness issue with bool atomic intrinsics. rdar://problem/8461234 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@117403 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'lib/CodeGen/CGExpr.cpp') diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 7bc8159a3d..85cd0c0c30 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -590,13 +590,7 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(llvm::Value *Addr, bool Volatile, if (TBAAInfo) CGM.DecorateInstruction(Load, TBAAInfo); - // Bool can have different representation in memory than in registers. - llvm::Value *V = Load; - if (Ty->isBooleanType()) - if (V->getType() != llvm::Type::getInt1Ty(VMContext)) - V = Builder.CreateTrunc(V, llvm::Type::getInt1Ty(VMContext), "tobool"); - - return V; + return EmitFromMemory(Load, Ty); } static bool isBooleanUnderlyingType(QualType Ty) { @@ -605,17 +599,31 @@ static bool isBooleanUnderlyingType(QualType Ty) { return false; } -void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, - bool Volatile, unsigned Alignment, - QualType Ty, - llvm::MDNode *TBAAInfo) { +llvm::Value *CodeGenFunction::EmitToMemory(llvm::Value *Value, QualType Ty) { + // Bool has a different representation in memory than in registers. + if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) { + assert(Value->getType()->isIntegerTy(1) && "value rep of bool not i1"); + return Builder.CreateZExt(Value, Builder.getInt8Ty(), "frombool"); + } + return Value; +} + +llvm::Value *CodeGenFunction::EmitFromMemory(llvm::Value *Value, QualType Ty) { + // Bool has a different representation in memory than in registers. if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) { - // Bool can have different representation in memory than in registers. - const llvm::PointerType *DstPtr = cast(Addr->getType()); - Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false); + assert(Value->getType()->isIntegerTy(8) && "memory rep of bool not i8"); + return Builder.CreateTrunc(Value, Builder.getInt1Ty(), "tobool"); } + return Value; +} + +void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, llvm::Value *Addr, + bool Volatile, unsigned Alignment, + QualType Ty, + llvm::MDNode *TBAAInfo) { + Value = EmitToMemory(Value, Ty); llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile); if (Alignment) Store->setAlignment(Alignment); -- cgit v1.2.3