From 2823c195ee65caf4042dc512c62df811c82dfbc1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 21 Aug 2007 16:34:16 +0000 Subject: [PATCH] reimplement complex mul git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@41226 91177308-0d34-0410-b5e6-96231b3b80d8 --- CodeGen/CGComplexExpr.cpp | 17 +++++++++++- CodeGen/CGExpr.cpp | 58 ++++++++------------------------------- 2 files changed, 28 insertions(+), 47 deletions(-) diff --git a/CodeGen/CGComplexExpr.cpp b/CodeGen/CGComplexExpr.cpp index a6e6c062e0..8c93ca3fb5 100644 --- a/CodeGen/CGComplexExpr.cpp +++ b/CodeGen/CGComplexExpr.cpp @@ -68,6 +68,7 @@ public: // case Expr::CastExprClass: // case Expr::CallExprClass: ComplexPairTy VisitBinaryOperator(const BinaryOperator *BO); + ComplexPairTy VisitBinMul (const BinaryOperator *E); ComplexPairTy VisitBinAdd (const BinaryOperator *E); ComplexPairTy VisitBinAssign (const BinaryOperator *E); @@ -231,7 +232,6 @@ ComplexPairTy ComplexExprEmitter::VisitBinaryOperator(const BinaryOperator *E) { } ComplexPairTy ComplexExprEmitter::VisitBinAdd(const BinaryOperator *E) { - // This must be a complex number. ComplexPairTy LHS = Visit(E->getLHS()); ComplexPairTy RHS = Visit(E->getRHS()); @@ -241,6 +241,21 @@ ComplexPairTy ComplexExprEmitter::VisitBinAdd(const BinaryOperator *E) { return ComplexPairTy(ResR, ResI); } +ComplexPairTy ComplexExprEmitter::VisitBinMul(const BinaryOperator *E) { + ComplexPairTy LHS = Visit(E->getLHS()); + ComplexPairTy RHS = Visit(E->getRHS()); + + llvm::Value *ResRl = CGF.Builder.CreateMul(LHS.first, RHS.first, "mul.rl"); + llvm::Value *ResRr = CGF.Builder.CreateMul(LHS.second, RHS.second, "mul.rr"); + llvm::Value *ResR = CGF.Builder.CreateSub(ResRl, ResRr, "mul.r"); + + llvm::Value *ResIl = CGF.Builder.CreateMul(LHS.second, RHS.first, "mul.il"); + llvm::Value *ResIr = CGF.Builder.CreateMul(LHS.first, RHS.second, "mul.ir"); + llvm::Value *ResI = CGF.Builder.CreateAdd(ResIl, ResIr, "mul.i"); + + return ComplexPairTy(ResR, ResI); +} + ComplexPairTy ComplexExprEmitter::VisitBinAssign(const BinaryOperator *E) { assert(E->getLHS()->getType().getCanonicalType() == E->getRHS()->getType().getCanonicalType() && "Invalid assignment"); diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp index 2b6fd727da..b886c86378 100644 --- a/CodeGen/CGExpr.cpp +++ b/CodeGen/CGExpr.cpp @@ -1105,54 +1105,23 @@ RValue CodeGenFunction::EmitBinaryOperator(const BinaryOperator *E) { RValue CodeGenFunction::EmitMul(RValue LHS, RValue RHS, QualType ResTy) { return RValue::get(Builder.CreateMul(LHS.getVal(), RHS.getVal(), "mul")); - -#if 0 - // 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); -#endif } RValue CodeGenFunction::EmitDiv(RValue LHS, RValue RHS, QualType ResTy) { - if (LHS.isScalar()) { - llvm::Value *RV; - if (LHS.getVal()->getType()->isFloatingPoint()) - RV = Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div"); - else if (ResTy->isUnsignedIntegerType()) - RV = Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div"); - else - RV = Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div"); - return RValue::get(RV); - } - assert(0 && "FIXME: This doesn't handle complex operands yet"); + if (LHS.getVal()->getType()->isFloatingPoint()) + return RValue::get(Builder.CreateFDiv(LHS.getVal(), RHS.getVal(), "div")); + else if (ResTy->isUnsignedIntegerType()) + return RValue::get(Builder.CreateUDiv(LHS.getVal(), RHS.getVal(), "div")); + else + return RValue::get(Builder.CreateSDiv(LHS.getVal(), RHS.getVal(), "div")); } RValue CodeGenFunction::EmitRem(RValue LHS, RValue RHS, QualType ResTy) { - if (LHS.isScalar()) { - llvm::Value *RV; - // Rem in C can't be a floating point type: C99 6.5.5p2. - if (ResTy->isUnsignedIntegerType()) - RV = Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem"); - else - RV = Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem"); - return RValue::get(RV); - } - - assert(0 && "FIXME: This doesn't handle complex operands yet"); + // Rem in C can't be a floating point type: C99 6.5.5p2. + if (ResTy->isUnsignedIntegerType()) + return RValue::get(Builder.CreateURem(LHS.getVal(), RHS.getVal(), "rem")); + else + return RValue::get(Builder.CreateSRem(LHS.getVal(), RHS.getVal(), "rem")); } RValue CodeGenFunction::EmitAdd(RValue LHS, RValue RHS, QualType ResTy) { @@ -1174,10 +1143,7 @@ RValue CodeGenFunction::EmitPointerAdd(RValue LHS, QualType LHSTy, } RValue CodeGenFunction::EmitSub(RValue LHS, RValue RHS, QualType ResTy) { - if (LHS.isScalar()) - return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub")); - - assert(0 && "FIXME: This doesn't handle complex operands yet"); + return RValue::get(Builder.CreateSub(LHS.getVal(), RHS.getVal(), "sub")); } RValue CodeGenFunction::EmitPointerSub(RValue LHS, QualType LHSTy, -- 2.40.0