[cfe-commits] r41231 - in /cfe/trunk/CodeGen: CGComplexExpr.cpp CGExpr.cpp CodeGenFunction.h

Chris Lattner sabre at nondot.org
Tue Aug 21 09:57:57 PDT 2007


Author: lattner
Date: Tue Aug 21 11:57:55 2007
New Revision: 41231

URL: http://llvm.org/viewvc/llvm-project?rev=41231&view=rev
Log:
reimplement support for complex comparisons, add support for integer complex compares.

Modified:
    cfe/trunk/CodeGen/CGComplexExpr.cpp
    cfe/trunk/CodeGen/CGExpr.cpp
    cfe/trunk/CodeGen/CodeGenFunction.h

Modified: cfe/trunk/CodeGen/CGComplexExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGComplexExpr.cpp?rev=41231&r1=41230&r2=41231&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGComplexExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGComplexExpr.cpp Tue Aug 21 11:57:55 2007
@@ -24,7 +24,7 @@
 //                        Complex Expression Emitter
 //===----------------------------------------------------------------------===//
 
-typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
+typedef CodeGenFunction::ComplexPairTy ComplexPairTy;
 
 namespace  {
 class VISIBILITY_HIDDEN ComplexExprEmitter
@@ -70,6 +70,8 @@
   ComplexPairTy VisitBinaryOperator(const BinaryOperator *BO);
   ComplexPairTy VisitBinMul        (const BinaryOperator *E);
   ComplexPairTy VisitBinAdd        (const BinaryOperator *E);
+
+  // No comparisons produce a complex result.
   ComplexPairTy VisitBinAssign     (const BinaryOperator *E);
 
   
@@ -319,10 +321,10 @@
 
 /// EmitComplexExpr - Emit the computation of the specified expression of
 /// complex type, ignoring the result.
-void CodeGenFunction::EmitComplexExpr(const Expr *E) {
+ComplexPairTy CodeGenFunction::EmitComplexExpr(const Expr *E) {
   assert(E && E->getType()->isComplexType() &&
          "Invalid complex expression to emit");
   
-  ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E));
+  return ComplexExprEmitter(*this).Visit(const_cast<Expr*>(E));
 }
 

Modified: cfe/trunk/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExpr.cpp?rev=41231&r1=41230&r2=41231&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/CodeGen/CGExpr.cpp Tue Aug 21 11:57:55 2007
@@ -1217,15 +1217,16 @@
 RValue CodeGenFunction::EmitBinaryCompare(const BinaryOperator *E,
                                           unsigned UICmpOpc, unsigned SICmpOpc,
                                           unsigned FCmpOpc) {
-  RValue LHS = EmitExpr(E->getLHS());
-  RValue RHS = EmitExpr(E->getRHS());
-
   llvm::Value *Result;
-  if (LHS.isScalar()) {
-    if (LHS.getVal()->getType()->isFloatingPoint()) {
+  QualType LHSTy = E->getLHS()->getType();
+  if (!LHSTy->isComplexType()) {
+    RValue LHS = EmitExpr(E->getLHS());
+    RValue RHS = EmitExpr(E->getRHS());
+    
+    if (LHSTy->isRealFloatingType()) {
       Result = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
                                   LHS.getVal(), RHS.getVal(), "cmp");
-    } else if (E->getLHS()->getType()->isUnsignedIntegerType()) {
+    } else if (LHSTy->isUnsignedIntegerType()) {
       // FIXME: This check isn't right for "unsigned short < int" where ushort
       // promotes to int and does a signed compare.
       Result = Builder.CreateICmp((llvm::ICmpInst::Predicate)UICmpOpc,
@@ -1236,26 +1237,34 @@
                                   LHS.getVal(), RHS.getVal(), "cmp");
     }
   } else {
-#if 0
-    // Struct/union/complex
-    llvm::Value *LHSR, *LHSI, *RHSR, *RHSI, *ResultR, *ResultI;
-    EmitLoadOfComplex(LHS, LHSR, LHSI);
-    EmitLoadOfComplex(RHS, RHSR, RHSI);
-
-    // FIXME: need to consider _Complex over integers too!
-
-    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()) {
+    // Complex Comparison: can only be an equality comparison.
+    ComplexPairTy LHS = EmitComplexExpr(E->getLHS());
+    ComplexPairTy RHS = EmitComplexExpr(E->getRHS());
+
+    QualType CETy = 
+      cast<ComplexType>(LHSTy.getCanonicalType())->getElementType();
+    
+    llvm::Value *ResultR, *ResultI;
+    if (CETy->isRealFloatingType()) {
+      ResultR = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
+                                   LHS.first, RHS.first, "cmp.r");
+      ResultI = Builder.CreateFCmp((llvm::FCmpInst::Predicate)FCmpOpc,
+                                   LHS.second, RHS.second, "cmp.i");
+    } else {
+      unsigned Opc = CETy->isUnsignedIntegerType() ? UICmpOpc : SICmpOpc;
+      ResultR = Builder.CreateICmp((llvm::ICmpInst::Predicate)Opc,
+                                   LHS.first, RHS.first, "cmp.r");
+      ResultI = Builder.CreateICmp((llvm::ICmpInst::Predicate)Opc,
+                                   LHS.second, RHS.second, "cmp.i");
+    }
+      
+    if (E->getOpcode() == BinaryOperator::EQ) {
       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 != ?");
+      assert(E->getOpcode() == BinaryOperator::NE &&
+             "Complex comparison other than == or != ?");
+      Result = Builder.CreateOr(ResultR, ResultI, "or.ri");
     }
-#endif
   }
 
   // ZExt result to int.

Modified: cfe/trunk/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CodeGenFunction.h?rev=41231&r1=41230&r2=41231&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/CodeGen/CodeGenFunction.h Tue Aug 21 11:57:55 2007
@@ -182,6 +182,7 @@
   CodeGenModule &CGM;  // Per-module state.
   TargetInfo &Target;
 public:
+  typedef std::pair<llvm::Value *, llvm::Value *> ComplexPairTy;
   llvm::LLVMBuilder Builder;
 private:
   
@@ -416,7 +417,7 @@
   
   /// EmitComplexExpr - Emit the computation of the specified expression of
   /// complex type, ignoring the result.
-  void EmitComplexExpr(const Expr *E);
+  ComplexPairTy EmitComplexExpr(const Expr *E);
 };
 }  // end namespace CodeGen
 }  // end namespace clang





More information about the cfe-commits mailing list