[cfe-commits] r51682 - in /cfe/trunk: lib/CodeGen/CGExprConstant.cpp test/CodeGen/constant-comparison.c

Eli Friedman eli.friedman at gmail.com
Thu May 29 08:17:49 PDT 2008


Author: efriedma
Date: Thu May 29 10:17:49 2008
New Revision: 51682

URL: http://llvm.org/viewvc/llvm-project?rev=51682&view=rev
Log:
More complete/correct implementation of the comparison operators for 
constant expressions.


Added:
    cfe/trunk/test/CodeGen/constant-comparison.c
Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprConstant.cpp?rev=51682&r1=51681&r2=51682&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Thu May 29 10:17:49 2008
@@ -385,20 +385,60 @@
     return llvm::ConstantExpr::getAnd(LHS, RHS);
   }
 
-  llvm::Constant *VisitBinNE(const BinaryOperator *E) {
+  llvm::Constant *EmitCmp(const BinaryOperator *E,
+                          llvm::CmpInst::Predicate SignedPred,
+                          llvm::CmpInst::Predicate UnsignedPred,
+                          llvm::CmpInst::Predicate FloatPred) {
     llvm::Constant *LHS = Visit(E->getLHS());
     llvm::Constant *RHS = Visit(E->getRHS());
-
-    const llvm::Type* ResultType = ConvertType(E->getType());
-    if (!ResultType->isInteger()) {
+    llvm::Constant *Result;
+    if (LHS->getType()->isInteger() ||
+        isa<llvm::PointerType>(LHS->getType())) {
+      if (E->getLHS()->getType()->isSignedIntegerType())
+        Result = llvm::ConstantExpr::getICmp(SignedPred, LHS, RHS);
+      else
+        Result = llvm::ConstantExpr::getICmp(UnsignedPred, LHS, RHS);
+    } else if (LHS->getType()->isFloatingPoint()) {
+      Result = llvm::ConstantExpr::getFCmp(FloatPred, LHS, RHS);
+    } else {
       CGM.WarnUnsupported(E, "constant expression");
-      return llvm::Constant::getNullValue(ConvertType(E->getType()));
+      Result = llvm::ConstantInt::getFalse();
     }
-    llvm::Constant *Result =
-        llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_NE, LHS, RHS);
+
+    const llvm::Type* ResultType = ConvertType(E->getType());
     return llvm::ConstantExpr::getZExtOrBitCast(Result, ResultType);
   }
 
+  llvm::Constant *VisitBinNE(const BinaryOperator *E) {
+    return EmitCmp(E, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_NE,
+                   llvm::CmpInst::FCMP_ONE);
+  }
+
+  llvm::Constant *VisitBinEQ(const BinaryOperator *E) {
+    return EmitCmp(E, llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_EQ,
+                   llvm::CmpInst::FCMP_OEQ);
+  }
+
+  llvm::Constant *VisitBinLT(const BinaryOperator *E) {
+    return EmitCmp(E, llvm::CmpInst::ICMP_SLT, llvm::CmpInst::ICMP_ULT,
+                   llvm::CmpInst::FCMP_OLT);
+  }
+
+  llvm::Constant *VisitBinLE(const BinaryOperator *E) {
+    return EmitCmp(E, llvm::CmpInst::ICMP_SLE, llvm::CmpInst::ICMP_ULE,
+                   llvm::CmpInst::FCMP_OLE);
+  }
+
+  llvm::Constant *VisitBinGT(const BinaryOperator *E) {
+    return EmitCmp(E, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_UGT,
+                   llvm::CmpInst::FCMP_OGT);
+  }
+
+  llvm::Constant *VisitBinGE(const BinaryOperator *E) {
+    return EmitCmp(E, llvm::CmpInst::ICMP_SGE, llvm::CmpInst::ICMP_SGE,
+                   llvm::CmpInst::FCMP_OGE);
+  }
+
   llvm::Constant *VisitConditionalOperator(const ConditionalOperator *E) {
     llvm::Constant *Cond = Visit(E->getCond());
     llvm::Constant *CondVal = EmitConversionToBool(Cond, E->getType());

Added: cfe/trunk/test/CodeGen/constant-comparison.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/constant-comparison.c?rev=51682&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/constant-comparison.c (added)
+++ cfe/trunk/test/CodeGen/constant-comparison.c Thu May 29 10:17:49 2008
@@ -0,0 +1,12 @@
+// RUN: clang -emit-llvm %s -o - 2>&1 | not grep warning
+// RUN: clang -emit-llvm %s -o - | grep @b | count 1
+
+int a, b;
+int *c1 = 1 < 2 ? &a : &b;
+int *c2 = 3 != 3LL ? &b : &a;
+int *c3 = !(3 <= 4.0) ? &b : &a;
+int *c4 = &a - (6 * 5 > 30);
+int *c5 = &a + (6 * 5 >= 30);
+int c6 = 44 < 33;
+
+





More information about the cfe-commits mailing list