[cfe-commits] r41446 - /cfe/trunk/CodeGen/CGExprScalar.cpp

Chris Lattner sabre at nondot.org
Sun Aug 26 09:42:57 PDT 2007


Author: lattner
Date: Sun Aug 26 11:42:57 2007
New Revision: 41446

URL: http://llvm.org/viewvc/llvm-project?rev=41446&view=rev
Log:
reimplement ConvertScalarValueToBool in CGExprScalar.cpp

Modified:
    cfe/trunk/CodeGen/CGExprScalar.cpp

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

==============================================================================
--- cfe/trunk/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/CodeGen/CGExprScalar.cpp Sun Aug 26 11:42:57 2007
@@ -62,6 +62,10 @@
     return EmitLoadOfLValue(EmitLValue(E), E->getType());
   }
     
+  /// EmitConversionToBool - Convert the specified expression value to a
+  /// boolean (i1) truth value.  This is equivalent to "Val == 0".
+  Value *EmitConversionToBool(Value *Src, QualType DstTy);
+    
   /// EmitScalarConversion - Emit a conversion from the specified type to the
   /// specified destination type, both of which are LLVM scalar types.
   Value *EmitScalarConversion(Value *Src, QualType SrcTy, QualType DstTy);
@@ -247,6 +251,37 @@
 //                                Utilities
 //===----------------------------------------------------------------------===//
 
+/// EmitConversionToBool - Convert the specified expression value to a
+/// boolean (i1) truth value.  This is equivalent to "Val == 0".
+Value *ScalarExprEmitter::EmitConversionToBool(Value *Src, QualType SrcType) {
+  assert(SrcType->isCanonical() && "EmitScalarConversion strips typedefs");
+  
+  if (SrcType->isRealFloatingType()) {
+    // Compare against 0.0 for fp scalars.
+    llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
+    // FIXME: llvm-gcc produces a une comparison: validate this is right.
+    return Builder.CreateFCmpUNE(Src, Zero, "tobool");
+  }
+  
+  assert((SrcType->isIntegerType() || SrcType->isPointerType()) &&
+         "Unknown scalar type to convert");
+  
+  // Because of the type rules of C, we often end up computing a logical value,
+  // then zero extending it to int, then wanting it as a logical value again.
+  // Optimize this common case.
+  if (llvm::ZExtInst *ZI = dyn_cast<llvm::ZExtInst>(Src)) {
+    if (ZI->getOperand(0)->getType() == llvm::Type::Int1Ty) {
+      Value *Result = ZI->getOperand(0);
+      ZI->eraseFromParent();
+      return Result;
+    }
+  }
+  
+  // Compare against an integer or pointer null.
+  llvm::Value *Zero = llvm::Constant::getNullValue(Src->getType());
+  return Builder.CreateICmpNE(Src, Zero, "tobool");
+}
+
 /// EmitScalarConversion - Emit a conversion from the specified type to the
 /// specified destination type, both of which are LLVM scalar types.
 Value *ScalarExprEmitter::EmitScalarConversion(Value *Src, QualType SrcType,
@@ -260,7 +295,7 @@
   // Handle conversions to bool first, they are special: comparisons against 0.
   if (const BuiltinType *DestBT = dyn_cast<BuiltinType>(DstType))
     if (DestBT->getKind() == BuiltinType::Bool)
-      return CGF.ConvertScalarValueToBool(RValue::get(Src), SrcType);
+      return EmitConversionToBool(Src, SrcType);
   
   const llvm::Type *DstTy = ConvertType(DstType);
 





More information about the cfe-commits mailing list