[cfe-commits] r84308 - in /cfe/trunk: lib/CodeGen/CGExprScalar.cpp test/CodeGenCXX/expr.cpp

Chris Lattner sabre at nondot.org
Fri Oct 16 21:24:21 PDT 2009


Author: lattner
Date: Fri Oct 16 23:24:20 2009
New Revision: 84308

URL: http://llvm.org/viewvc/llvm-project?rev=84308&view=rev
Log:
Fix PR5211: codegen shouldn't assume that the result of ||/&& is int 
anymore.  In C++ it is bool.

Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/test/CodeGenCXX/expr.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Fri Oct 16 23:24:20 2009
@@ -1379,18 +1379,20 @@
 }
 
 Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
+  const llvm::Type *ResTy = ConvertType(E->getType());
+  
   // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
   // If we have 1 && X, just emit X without inserting the control flow.
   if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
     if (Cond == 1) { // If we have 1 && X, just emit X.
       Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
-      // ZExt result to int.
-      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "land.ext");
+      // ZExt result to int or bool.
+      return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "land.ext");
     }
 
-    // 0 && RHS: If it is safe, just elide the RHS, and return 0.
+    // 0 && RHS: If it is safe, just elide the RHS, and return 0/false.
     if (!CGF.ContainsLabel(E->getRHS()))
-      return llvm::Constant::getNullValue(CGF.LLVMIntTy);
+      return llvm::Constant::getNullValue(ResTy);
   }
 
   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("land.end");
@@ -1423,22 +1425,24 @@
   PN->addIncoming(RHSCond, RHSBlock);
 
   // ZExt result to int.
-  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "land.ext");
+  return Builder.CreateZExtOrBitCast(PN, ResTy, "land.ext");
 }
 
 Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
+  const llvm::Type *ResTy = ConvertType(E->getType());
+  
   // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
   // If we have 0 || X, just emit X without inserting the control flow.
   if (int Cond = CGF.ConstantFoldsToSimpleInteger(E->getLHS())) {
     if (Cond == -1) { // If we have 0 || X, just emit X.
       Value *RHSCond = CGF.EvaluateExprAsBool(E->getRHS());
-      // ZExt result to int.
-      return Builder.CreateZExt(RHSCond, CGF.LLVMIntTy, "lor.ext");
+      // ZExt result to int or bool.
+      return Builder.CreateZExtOrBitCast(RHSCond, ResTy, "lor.ext");
     }
 
-    // 1 || RHS: If it is safe, just elide the RHS, and return 1.
+    // 1 || RHS: If it is safe, just elide the RHS, and return 1/true.
     if (!CGF.ContainsLabel(E->getRHS()))
-      return llvm::ConstantInt::get(CGF.LLVMIntTy, 1);
+      return llvm::ConstantInt::get(ResTy, 1);
   }
 
   llvm::BasicBlock *ContBlock = CGF.createBasicBlock("lor.end");
@@ -1474,7 +1478,7 @@
   PN->addIncoming(RHSCond, RHSBlock);
 
   // ZExt result to int.
-  return Builder.CreateZExt(PN, CGF.LLVMIntTy, "lor.ext");
+  return Builder.CreateZExtOrBitCast(PN, ResTy, "lor.ext");
 }
 
 Value *ScalarExprEmitter::VisitBinComma(const BinaryOperator *E) {

Modified: cfe/trunk/test/CodeGenCXX/expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/expr.cpp?rev=84308&r1=84307&r2=84308&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/expr.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/expr.cpp Fri Oct 16 23:24:20 2009
@@ -1,5 +1,12 @@
 // RUN: clang-cc -emit-llvm -x c++ < %s
 
-void f(int x) {
+void test0(int x) {
           if (x != 0) return;
 }
+
+
+// PR5211
+void test1() {
+  char *xpto;
+  while ( true && xpto[0] );
+}





More information about the cfe-commits mailing list