From 4db18f279ba2760d77a79587fac5fd69d68dc4ed Mon Sep 17 00:00:00 2001 From: Gabor Greif Date: Fri, 13 Jul 2007 23:33:18 +0000 Subject: [PATCH] implement _Complex * == and != git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39841 91177308-0d34-0410-b5e6-96231b3b80d8 --- CodeGen/CGExpr.cpp | 36 +++++++++++++++++++++++++++++++++--- test/CodeGen/complex.c | 2 +- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp index 98fae6d2b3..2f9d0ea072 100644 --- a/CodeGen/CGExpr.cpp +++ b/CodeGen/CGExpr.cpp @@ -1070,7 +1070,23 @@ RValue CodeGenFunction::EmitMul(RValue LHS, RValue RHS, QualType ResTy) { if (LHS.isScalar()) return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul")); - assert(0 && "FIXME: This doesn't handle complex operands yet"); + // Otherwise, this must be a complex number. + llvm::Value *LHSR, *LHSI, *RHSR, *RHSI; + + EmitLoadOfComplex(LHS, LHSR, LHSI); + EmitLoadOfComplex(RHS, RHSR, RHSI); + + llvm::Value *ResRl = Builder.CreateMul(LHSR, RHSR, "mul.rl"); + llvm::Value *ResRr = Builder.CreateMul(LHSI, RHSI, "mul.rr"); + llvm::Value *ResR = Builder.CreateSub(ResRl, ResRr, "mul.r"); + + llvm::Value *ResIl = Builder.CreateMul(LHSI, RHSR, "mul.il"); + llvm::Value *ResIr = Builder.CreateMul(LHSR, RHSI, "mul.ir"); + llvm::Value *ResI = Builder.CreateAdd(ResIl, ResIr, "mul.i"); + + llvm::Value *Res = CreateTempAlloca(ConvertType(ResTy)); + EmitStoreOfComplex(ResR, ResI, Res); + return RValue::getAggregate(Res); } RValue CodeGenFunction::EmitDiv(RValue LHS, RValue RHS, QualType ResTy) { @@ -1231,9 +1247,23 @@ RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E, } } else { // Struct/union/complex - assert(0 && "Aggregate comparisons not implemented yet!"); + llvm::Value *LHSR, *LHSI, *RHSR, *RHSI, *ResultR, *ResultI; + EmitLoadOfComplex(LHS, LHSR, LHSI); + EmitLoadOfComplex(RHS, RHSR, RHSI); + + ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, + LHSR, RHSR, "cmp.r"); + ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc, + LHSI, RHSI, "cmp.i"); + if (BinaryOperator::EQ == E->getOpcode()) { + Result = Builder.CreateAnd(ResultR, ResultI, "and.ri"); + } else if (BinaryOperator::NE == E->getOpcode()) { + Result = Builder.CreateOr(ResultR, ResultI, "or.ri"); + } else { + assert(0 && "Complex comparison other than == or != ?"); + } } - + // ZExt result to int. return RValue::get(Builder.CreateZExt(Result, LLVMIntTy, "cmp.ext")); } diff --git a/test/CodeGen/complex.c b/test/CodeGen/complex.c index 001d148bc5..30793aaf9e 100644 --- a/test/CodeGen/complex.c +++ b/test/CodeGen/complex.c @@ -6,5 +6,5 @@ void main(void) double _Complex a = 5; double _Complex b = 42; - return a * b == 123; + return a * b == b * a; }