[cfe-commits] r117456 - in /cfe/trunk: lib/CodeGen/CGBuiltin.cpp lib/CodeGen/CGExpr.cpp lib/CodeGen/CodeGenFunction.h test/CodeGen/atomic.c

Rafael Espindola rafael.espindola at gmail.com
Wed Oct 27 10:13:49 PDT 2010


Author: rafael
Date: Wed Oct 27 12:13:49 2010
New Revision: 117456

URL: http://llvm.org/viewvc/llvm-project?rev=117456&view=rev
Log:
Revert r117403 as it caused PR8480.

Modified:
    cfe/trunk/lib/CodeGen/CGBuiltin.cpp
    cfe/trunk/lib/CodeGen/CGExpr.cpp
    cfe/trunk/lib/CodeGen/CodeGenFunction.h
    cfe/trunk/test/CodeGen/atomic.c

Modified: cfe/trunk/lib/CodeGen/CGBuiltin.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGBuiltin.cpp?rev=117456&r1=117455&r2=117456&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGBuiltin.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGBuiltin.cpp Wed Oct 27 12:13:49 2010
@@ -41,28 +41,29 @@
                          C, C + 5);
 }
 
-/// Emit the conversions required to turn the given value into an
-/// integer of the given size.
-static Value *EmitToInt(CodeGenFunction &CGF, llvm::Value *V,
-                        QualType T, const llvm::IntegerType *IntType) {
-  V = CGF.EmitToMemory(V, T);
-
-  if (V->getType()->isPointerTy())
-    return CGF.Builder.CreatePtrToInt(V, IntType);
-
-  assert(V->getType() == IntType);
-  return V;
+static Value *EmitCastToInt(CodeGenFunction &CGF,
+                            const llvm::Type *ToType, Value *Val) {
+  if (Val->getType()->isPointerTy())
+    return CGF.Builder.CreatePtrToInt(Val, ToType);
+
+  assert(Val->getType()->isIntegerTy() &&
+         "Used a non-integer and non-pointer type with atomic builtin");
+  assert(Val->getType()->getScalarSizeInBits() <=
+         ToType->getScalarSizeInBits() && "Integer type too small");
+  return CGF.Builder.CreateSExtOrBitCast(Val, ToType);
 }
 
-static Value *EmitFromInt(CodeGenFunction &CGF, llvm::Value *V,
-                          QualType T, const llvm::Type *ResultType) {
-  V = CGF.EmitFromMemory(V, T);
-
-  if (ResultType->isPointerTy())
-    return CGF.Builder.CreateIntToPtr(V, ResultType);
-
-  assert(V->getType() == ResultType);
-  return V;
+static Value *EmitCastFromInt(CodeGenFunction &CGF, QualType ToQualType,
+                              Value *Val) {
+  const llvm::Type *ToType = CGF.ConvertType(ToQualType);
+  if (ToType->isPointerTy()) {
+    return CGF.Builder.CreateIntToPtr(Val, ToType);
+  }
+  assert(Val->getType()->isIntegerTy() &&
+         "Used a non-integer and non-pointer type with atomic builtin");
+  assert(Val->getType()->getScalarSizeInBits() >=
+         ToType->getScalarSizeInBits() && "Integer type too small");
+  return CGF.Builder.CreateTruncOrBitCast(Val, ToType);
 }
 
 // The atomic builtins are also full memory barriers. This is a utility for
@@ -84,69 +85,48 @@
 /// and the expression node.
 static RValue EmitBinaryAtomic(CodeGenFunction &CGF,
                                Intrinsic::ID Id, const CallExpr *E) {
-  QualType T = E->getType();
-  assert(E->getArg(0)->getType()->isPointerType());
-  assert(CGF.getContext().hasSameUnqualifiedType(T,
-                                  E->getArg(0)->getType()->getPointeeType()));
-  assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
-
   llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
   unsigned AddrSpace =
     cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
-
-  const llvm::IntegerType *IntType =
+  const llvm::Type *ValueType =
     llvm::IntegerType::get(CGF.getLLVMContext(),
-                           CGF.getContext().getTypeSize(T));
-  const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
-
-  const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
-  llvm::Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
-
-  llvm::Value *Args[2];
-  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
-  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
-  const llvm::Type *ValueType = Args[1]->getType();
-  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
-
-  llvm::Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
-  Result = EmitFromInt(CGF, Result, T, ValueType);
-  return RValue::get(Result);
+                           CGF.getContext().getTypeSize(E->getType()));
+  const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
+  const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
+  Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
+
+  Value *Args[2] = { CGF.Builder.CreateBitCast(DestPtr, PtrType),
+                     EmitCastToInt(CGF, ValueType,
+                                   CGF.EmitScalarExpr(E->getArg(1))) };
+  return RValue::get(EmitCastFromInt(CGF, E->getType(),
+                                     EmitCallWithBarrier(CGF, AtomF, Args,
+                                                         Args + 2)));
 }
 
 /// Utility to insert an atomic instruction based Instrinsic::ID and
-/// the expression node, where the return value is the result of the
-/// operation.
+// the expression node, where the return value is the result of the
+// operation.
 static RValue EmitBinaryAtomicPost(CodeGenFunction &CGF,
                                    Intrinsic::ID Id, const CallExpr *E,
                                    Instruction::BinaryOps Op) {
-  QualType T = E->getType();
-  assert(E->getArg(0)->getType()->isPointerType());
-  assert(CGF.getContext().hasSameUnqualifiedType(T,
-                                  E->getArg(0)->getType()->getPointeeType()));
-  assert(CGF.getContext().hasSameUnqualifiedType(T, E->getArg(1)->getType()));
-
   llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
   unsigned AddrSpace =
     cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
-
-  const llvm::IntegerType *IntType =
+  
+  const llvm::Type *ValueType =
     llvm::IntegerType::get(CGF.getLLVMContext(),
-                           CGF.getContext().getTypeSize(T));
-  const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
-
-  const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
-  llvm::Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
-
-  llvm::Value *Args[2];
-  Args[1] = CGF.EmitScalarExpr(E->getArg(1));
-  const llvm::Type *ValueType = Args[1]->getType();
-  Args[1] = EmitToInt(CGF, Args[1], T, IntType);
-  Args[0] = CGF.Builder.CreateBitCast(DestPtr, IntPtrType);
-
-  llvm::Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
-  Result = CGF.Builder.CreateBinOp(Op, Result, Args[1]);
-  Result = EmitFromInt(CGF, Result, T, ValueType);
-  return RValue::get(Result);
+                           CGF.getContext().getTypeSize(E->getType()));
+  const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
+  const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
+  Value *AtomF = CGF.CGM.getIntrinsic(Id, IntrinsicTypes, 2);
+
+  Value *Args[2] = { CGF.Builder.CreateBitCast(DestPtr, PtrType),
+                     EmitCastToInt(CGF, ValueType,
+                                   CGF.EmitScalarExpr(E->getArg(1))) };
+  Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 2);
+  return RValue::get(EmitCastFromInt(CGF, E->getType(),
+                                     CGF.Builder.CreateBinOp(Op, Result,
+                                                             Args[1])));
 }
 
 /// EmitFAbs - Emit a call to fabs/fabsf/fabsl, depending on the type of ValTy,
@@ -813,29 +793,25 @@
   case Builtin::BI__sync_val_compare_and_swap_4:
   case Builtin::BI__sync_val_compare_and_swap_8:
   case Builtin::BI__sync_val_compare_and_swap_16: {
-    QualType T = E->getType();
     llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
     unsigned AddrSpace =
       cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
-    
-    const llvm::IntegerType *IntType =
+    const llvm::Type *ValueType =
       llvm::IntegerType::get(CGF.getLLVMContext(),
-                             CGF.getContext().getTypeSize(T));
-    const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
-    const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
+                             CGF.getContext().getTypeSize(E->getType()));
+    const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
+    const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
     Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap,
                                     IntrinsicTypes, 2);
 
-    Value *Args[3];
-    Args[0] = Builder.CreateBitCast(DestPtr, IntPtrType);
-    Args[1] = CGF.EmitScalarExpr(E->getArg(1));
-    const llvm::Type *ValueType = Args[1]->getType();
-    Args[1] = EmitToInt(CGF, Args[1], T, IntType);
-    Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
-
-    Value *Result = EmitCallWithBarrier(CGF, AtomF, Args, Args + 3);
-    Result = EmitFromInt(CGF, Result, T, ValueType);
-    return RValue::get(Result);
+    Value *Args[3] = { Builder.CreateBitCast(DestPtr, PtrType),
+                       EmitCastToInt(CGF, ValueType,
+                                     CGF.EmitScalarExpr(E->getArg(1))),
+                       EmitCastToInt(CGF, ValueType,
+                                     CGF.EmitScalarExpr(E->getArg(2))) };
+    return RValue::get(EmitCastFromInt(CGF, E->getType(),
+                                       EmitCallWithBarrier(CGF, AtomF, Args,
+                                                           Args + 3)));
   }
 
   case Builtin::BI__sync_bool_compare_and_swap_1:
@@ -843,30 +819,27 @@
   case Builtin::BI__sync_bool_compare_and_swap_4:
   case Builtin::BI__sync_bool_compare_and_swap_8:
   case Builtin::BI__sync_bool_compare_and_swap_16: {
-    QualType T = E->getArg(1)->getType();
     llvm::Value *DestPtr = CGF.EmitScalarExpr(E->getArg(0));
     unsigned AddrSpace =
       cast<llvm::PointerType>(DestPtr->getType())->getAddressSpace();
-    
-    const llvm::IntegerType *IntType =
+    const llvm::Type *ValueType =
       llvm::IntegerType::get(CGF.getLLVMContext(),
-                             CGF.getContext().getTypeSize(T));
-    const llvm::Type *IntPtrType = IntType->getPointerTo(AddrSpace);
-    const llvm::Type *IntrinsicTypes[2] = { IntType, IntPtrType };
+        CGF.getContext().getTypeSize(E->getArg(1)->getType()));
+    const llvm::Type *PtrType = ValueType->getPointerTo(AddrSpace);
+    const llvm::Type *IntrinsicTypes[2] = { ValueType, PtrType };
     Value *AtomF = CGM.getIntrinsic(Intrinsic::atomic_cmp_swap,
                                     IntrinsicTypes, 2);
 
-    Value *Args[3];
-    Args[0] = Builder.CreateBitCast(DestPtr, IntPtrType);
-    Args[1] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(1)), T, IntType);
-    Args[2] = EmitToInt(CGF, CGF.EmitScalarExpr(E->getArg(2)), T, IntType);
-
+    Value *Args[3] = { Builder.CreateBitCast(DestPtr, PtrType),
+                       EmitCastToInt(CGF, ValueType,
+                                     CGF.EmitScalarExpr(E->getArg(1))),
+                       EmitCastToInt(CGF, ValueType,
+                                     CGF.EmitScalarExpr(E->getArg(2))) };
     Value *OldVal = Args[1];
     Value *PrevVal = EmitCallWithBarrier(*this, AtomF, Args, Args + 3);
     Value *Result = Builder.CreateICmpEQ(PrevVal, OldVal);
     // zext bool to int.
-    Result = Builder.CreateZExt(Result, ConvertType(E->getType()));
-    return RValue::get(Result);
+    return RValue::get(Builder.CreateZExt(Result, ConvertType(E->getType())));
   }
 
   case Builtin::BI__sync_lock_test_and_set_1:

Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=117456&r1=117455&r2=117456&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Wed Oct 27 12:13:49 2010
@@ -590,7 +590,13 @@
   if (TBAAInfo)
     CGM.DecorateInstruction(Load, TBAAInfo);
 
-  return EmitFromMemory(Load, Ty);
+  // 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;
 }
 
 static bool isBooleanUnderlyingType(QualType Ty) {
@@ -599,31 +605,17 @@
   return false;
 }
 
-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)) {
-    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);
+
+  if (Ty->isBooleanType() || isBooleanUnderlyingType(Ty)) {
+    // Bool can have different representation in memory than in registers.
+    const llvm::PointerType *DstPtr = cast<llvm::PointerType>(Addr->getType());
+    Value = Builder.CreateIntCast(Value, DstPtr->getElementType(), false);
+  }
+
   llvm::StoreInst *Store = Builder.CreateStore(Value, Addr, Volatile);
   if (Alignment)
     Store->setAlignment(Alignment);

Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=117456&r1=117455&r2=117456&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Wed Oct 27 12:13:49 2010
@@ -1349,14 +1349,6 @@
   /// object.
   LValue EmitCheckedLValue(const Expr *E);
 
-  /// EmitToMemory - Change a scalar value from its value
-  /// representation to its in-memory representation.
-  llvm::Value *EmitToMemory(llvm::Value *Value, QualType Ty);
-
-  /// EmitFromMemory - Change a scalar value from its memory
-  /// representation to its value representation.
-  llvm::Value *EmitFromMemory(llvm::Value *Value, QualType Ty);
-
   /// EmitLoadOfScalar - Load a scalar value from an address, taking
   /// care to appropriately convert from the memory representation to
   /// the LLVM value representation.

Modified: cfe/trunk/test/CodeGen/atomic.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/atomic.c?rev=117456&r1=117455&r2=117456&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/atomic.c (original)
+++ cfe/trunk/test/CodeGen/atomic.c Wed Oct 27 12:13:49 2010
@@ -102,7 +102,8 @@
 
   if ( __sync_val_compare_and_swap(&valb, 0, 1)) {
     // CHECK: call void @llvm.memory.barrier(i1 true, i1 true, i1 true, i1 true, i1 true)
-    // CHECK: call i8 @llvm.atomic.cmp.swap.i8.p0i8(i8* %valb, i8 0, i8 1)
+// FIXME: Doesn't seem right!
+    // CHECK: call i8 @llvm.atomic.cmp.swap.i8.p0i8(i8* %valb, i8 0, i8 -1)
     // CHECK: call void @llvm.memory.barrier(i1 true, i1 true, i1 true, i1 true, i1 true)
     old = 42;
   }





More information about the cfe-commits mailing list