]> granicus.if.org Git - clang/commitdiff
Add lvalue-bitcast support for complex numbers.
authorDouglas Gregor <dgregor@apple.com>
Wed, 14 Jul 2010 21:35:45 +0000 (21:35 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 14 Jul 2010 21:35:45 +0000 (21:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@108363 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprComplex.cpp
test/CodeGenCXX/lvalue-bitcasts.cpp

index 995c9b5c4507f92b4e7723ce872ae4e6930c8553..0927319db776c221558c61c3e23befe4d08a6b1b 100644 (file)
@@ -131,14 +131,14 @@ public:
 
   // FIXME: CompoundLiteralExpr
 
-  ComplexPairTy EmitCast(Expr *Op, QualType DestTy);
+  ComplexPairTy EmitCast(CastExpr::CastKind CK, Expr *Op, QualType DestTy);
   ComplexPairTy VisitImplicitCastExpr(ImplicitCastExpr *E) {
     // Unlike for scalars, we don't have to worry about function->ptr demotion
     // here.
-    return EmitCast(E->getSubExpr(), E->getType());
+    return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
   }
   ComplexPairTy VisitCastExpr(CastExpr *E) {
-    return EmitCast(E->getSubExpr(), E->getType());
+    return EmitCast(E->getCastKind(), E->getSubExpr(), E->getType());
   }
   ComplexPairTy VisitCallExpr(const CallExpr *E);
   ComplexPairTy VisitStmtExpr(const StmtExpr *E);
@@ -339,11 +339,22 @@ ComplexPairTy ComplexExprEmitter::EmitComplexToComplexCast(ComplexPairTy Val,
   return Val;
 }
 
-ComplexPairTy ComplexExprEmitter::EmitCast(Expr *Op, QualType DestTy) {
+ComplexPairTy ComplexExprEmitter::EmitCast(CastExpr::CastKind CK, Expr *Op, 
+                                           QualType DestTy) {
   // Two cases here: cast from (complex to complex) and (scalar to complex).
   if (Op->getType()->isAnyComplexType())
     return EmitComplexToComplexCast(Visit(Op), Op->getType(), DestTy);
 
+  // FIXME: We should be looking at all of the cast kinds here, not
+  // cherry-picking the ones we have test cases for.
+  if (CK == CastExpr::CK_LValueBitCast) {
+    llvm::Value *V = CGF.EmitLValue(Op).getAddress();
+    V = Builder.CreateBitCast(V, 
+                      CGF.ConvertType(CGF.getContext().getPointerType(DestTy)));
+    // FIXME: Are the qualifiers correct here?
+    return EmitLoadOfComplex(V, DestTy.isVolatileQualified());
+  }
+  
   // C99 6.3.1.7: When a value of real type is converted to a complex type, the
   // real part of the complex result value is determined by the rules of
   // conversion to the corresponding real type and the imaginary part of the
@@ -521,7 +532,7 @@ EmitCompoundAssign(const CompoundAssignOperator *E,
   // improve codegen a little.  It is possible for the RHS to be complex or
   // scalar.
   OpInfo.Ty = E->getComputationResultType();
-  OpInfo.RHS = EmitCast(E->getRHS(), OpInfo.Ty);
+  OpInfo.RHS = EmitCast(CastExpr::CK_Unknown, E->getRHS(), OpInfo.Ty);
   
   LValue LHS = CGF.EmitLValue(E->getLHS());
   // We know the LHS is a complex lvalue.
index 5b95b24531c8ca32c60806f7fb73b2affbc0f8f7..8c5fa4ad44bb7ce95f4ffa44046684c9541b6c6d 100644 (file)
@@ -29,6 +29,22 @@ void reinterpret_cast_test(int &ir, float &fr, X &xr) {
   // CHECK: bitcast float*
   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
   xr = reinterpret_cast<X&>(fr);
+  _Complex float cf;
+  _Complex float &cfr = cf;
+  // CHECK: load i32**
+  // CHECK: bitcast i32*
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = reinterpret_cast<_Complex float&>(ir);
+  // CHECK: load float**
+  // CHECK: bitcast float*
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = reinterpret_cast<_Complex float&>(fr);
+  // CHECK: bitcast
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = reinterpret_cast<_Complex float&>(xr);
   // CHECK: ret void
 }
 
@@ -58,6 +74,22 @@ void c_cast(int &ir, float &fr, X &xr) {
   // CHECK: bitcast float*
   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
   xr = (X&)fr;
+  _Complex float cf;
+  _Complex float &cfr = cf;
+  // CHECK: load i32**
+  // CHECK: bitcast i32*
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = (_Complex float&)ir;
+  // CHECK: load float**
+  // CHECK: bitcast float*
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = (_Complex float&)fr;
+  // CHECK: bitcast
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = (_Complex float&)xr;
   // CHECK: ret void
 }
 
@@ -90,6 +122,23 @@ void functional_cast(int &ir, float &fr, X &xr) {
   // CHECK: bitcast float*
   // CHECK: call void @llvm.memcpy.p0i8.p0i8.i64
   xr = Xref(fr);
+  typedef _Complex float &complex_float_ref;
+  _Complex float cf;
+  _Complex float &cfr = cf;
+  // CHECK: load i32**
+  // CHECK: bitcast i32*
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = complex_float_ref(ir);
+  // CHECK: load float**
+  // CHECK: bitcast float*
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = complex_float_ref(fr);
+  // CHECK: bitcast
+  // CHECK: load float*
+  // CHECK: load float*
+  cfr = complex_float_ref(xr);
   // CHECK: ret void
 }