[cfe-commits] r81611 - in /cfe/trunk/lib/CodeGen: CGCXXClass.cpp CGExprScalar.cpp

Anders Carlsson andersca at mac.com
Fri Sep 11 23:04:24 PDT 2009


Author: andersca
Date: Sat Sep 12 01:04:24 2009
New Revision: 81611

URL: http://llvm.org/viewvc/llvm-project?rev=81611&view=rev
Log:
When necessary, null check the base value in GetAddressCXXOfBaseClass.

Modified:
    cfe/trunk/lib/CodeGen/CGCXXClass.cpp
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXXClass.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXXClass.cpp Sat Sep 12 01:04:24 2009
@@ -81,6 +81,22 @@
   uint64_t Offset = ComputeBaseClassOffset(getContext(), 
                                            ClassDecl, BaseClassDecl);
 
+  llvm::BasicBlock *CastNull = 0;
+  llvm::BasicBlock *CastNotNull = 0;
+  llvm::BasicBlock *CastEnd = 0;
+  
+  if (NullCheckValue) {
+    CastNull = createBasicBlock("cast.null");
+    CastNotNull = createBasicBlock("cast.notnull");
+    CastEnd = createBasicBlock("cast.end");
+    
+    llvm::Value *IsNull = 
+      Builder.CreateICmpEQ(BaseValue,
+                           llvm::Constant::getNullValue(BaseValue->getType()));
+    Builder.CreateCondBr(IsNull, CastNull, CastNotNull);
+    EmitBlock(CastNotNull);
+  }
+  
   const llvm::Type *LongTy = 
     CGM.getTypes().ConvertType(CGM.getContext().LongTy);
   const llvm::Type *Int8PtrTy = 
@@ -99,6 +115,20 @@
   // Cast back.
   const llvm::Type *BasePtr = llvm::PointerType::getUnqual(ConvertType(BTy));
   BaseValue = Builder.CreateBitCast(BaseValue, BasePtr);
+ 
+  if (NullCheckValue) {
+    Builder.CreateBr(CastEnd);
+    EmitBlock(CastNull);
+    Builder.CreateBr(CastEnd);
+    EmitBlock(CastEnd);
+    
+    llvm::PHINode *PHI = Builder.CreatePHI(BaseValue->getType());
+    PHI->reserveOperandSpace(2);
+    PHI->addIncoming(BaseValue, CastNotNull);
+    PHI->addIncoming(llvm::Constant::getNullValue(BaseValue->getType()), 
+                     CastNull);
+    BaseValue = PHI;
+  }
   
   return BaseValue;
 }

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Sat Sep 12 01:04:24 2009
@@ -669,8 +669,16 @@
     CXXRecordDecl *BaseClassDecl = cast<CXXRecordDecl>(BaseClassTy->getDecl());
     
     Value *Src = Visit(const_cast<Expr*>(E));
+
+    // FIXME: This should be true, but that leads to a failure in virt.cpp
+    bool NullCheckValue = false;
+    
+    // We always assume that 'this' is never null.
+    if (isa<CXXThisExpr>(E))
+      NullCheckValue = false;
+    
     return CGF.GetAddressCXXOfBaseClass(Src, DerivedClassDecl, BaseClassDecl,
-                                        /*NullCheckValue=*/true);
+                                        NullCheckValue);
   }
 
   }





More information about the cfe-commits mailing list