return llvm::ConstantInt::get(PtrDiffTy, Offset);
}
-static llvm::Value *GetBaseClassOffset(CodeGenFunction &CGF,
- llvm::Value *BaseValue,
- const CXXRecordDecl *ClassDecl,
- const CXXRecordDecl *BaseClassDecl) {
- CXXBasePaths Paths(/*FindAmbiguities=*/false,
- /*RecordPaths=*/true, /*DetectVirtual=*/false);
- if (!const_cast<CXXRecordDecl *>(ClassDecl)->
- isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClassDecl), Paths)) {
- assert(false && "Class must be derived from the passed in base class!");
- return 0;
- }
-
- unsigned Start = 0;
- llvm::Value *VirtualOffset = 0;
-
- const CXXBasePath &Path = Paths.front();
- const CXXRecordDecl *VBase = 0;
- for (unsigned i = 0, e = Path.size(); i != e; ++i) {
- const CXXBasePathElement& Element = Path[i];
- if (Element.Base->isVirtual()) {
- Start = i+1;
- QualType VBaseType = Element.Base->getType();
- VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
- }
- }
- if (VBase)
- VirtualOffset =
- CGF.GetVirtualBaseClassOffset(BaseValue, ClassDecl, VBase);
-
- uint64_t Offset =
- ComputeNonVirtualBaseClassOffset(CGF.getContext(), Paths, Start);
-
- if (!Offset)
- return VirtualOffset;
-
- const llvm::Type *PtrDiffTy =
- CGF.ConvertType(CGF.getContext().getPointerDiffType());
- llvm::Value *NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, Offset);
-
- if (VirtualOffset)
- return CGF.Builder.CreateAdd(VirtualOffset, NonVirtualOffset);
-
- return NonVirtualOffset;
-}
-
// FIXME: This probably belongs in CGVtable, but it relies on
// the static function ComputeNonVirtualBaseClassOffset, so we should make that
// a CodeGenModule member function as well.
// Just cast back.
return Builder.CreateBitCast(Value, BasePtrTy);
}
+
+ CXXBasePaths Paths(/*FindAmbiguities=*/false,
+ /*RecordPaths=*/true, /*DetectVirtual=*/false);
+ if (!const_cast<CXXRecordDecl *>(Class)->
+ isDerivedFrom(const_cast<CXXRecordDecl *>(BaseClass), Paths)) {
+ assert(false && "Class must be derived from the passed in base class!");
+ return 0;
+ }
+
+ unsigned Start = 0;
+ llvm::Value *VirtualOffset = 0;
+
+ const CXXBasePath &Path = Paths.front();
+ const CXXRecordDecl *VBase = 0;
+ for (unsigned i = 0, e = Path.size(); i != e; ++i) {
+ const CXXBasePathElement& Element = Path[i];
+ if (Element.Base->isVirtual()) {
+ Start = i+1;
+ QualType VBaseType = Element.Base->getType();
+ VBase = cast<CXXRecordDecl>(VBaseType->getAs<RecordType>()->getDecl());
+ }
+ }
+
+ uint64_t Offset =
+ ComputeNonVirtualBaseClassOffset(getContext(), Paths, Start);
+ if (!Offset && !VBase) {
+ // Just cast back.
+ return Builder.CreateBitCast(Value, BasePtrTy);
+ }
+
llvm::BasicBlock *CastNull = 0;
llvm::BasicBlock *CastNotNull = 0;
llvm::BasicBlock *CastEnd = 0;
EmitBlock(CastNotNull);
}
- const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
+ if (VBase)
+ VirtualOffset = GetVirtualBaseClassOffset(Value, Class, VBase);
- llvm::Value *Offset = GetBaseClassOffset(*this, Value, Class, BaseClass);
+ const llvm::Type *PtrDiffTy = ConvertType(getContext().getPointerDiffType());
+ llvm::Value *NonVirtualOffset = 0;
+ if (Offset)
+ NonVirtualOffset = llvm::ConstantInt::get(PtrDiffTy, Offset);
- if (Offset) {
- // Apply the offset.
- Value = Builder.CreateBitCast(Value, Int8PtrTy);
- Value = Builder.CreateGEP(Value, Offset, "add.ptr");
- }
+ llvm::Value *BaseOffset;
+ if (VBase) {
+ if (NonVirtualOffset)
+ BaseOffset = Builder.CreateAdd(VirtualOffset, NonVirtualOffset);
+ else
+ BaseOffset = VirtualOffset;
+ } else
+ BaseOffset = NonVirtualOffset;
+
+ // Apply the base offset.
+ const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
+ Value = Builder.CreateBitCast(Value, Int8PtrTy);
+ Value = Builder.CreateGEP(Value, BaseOffset, "add.ptr");
// Cast back.
Value = Builder.CreateBitCast(Value, BasePtrTy);
// CHECK-LL-NEXT: [[ep:%.*]] = alloca %class.test1_E*, align 8
// CHECK-LL-NEXT: [[vp:%.*]] = alloca i8*, align 8
// CHECK-LL-NEXT: [[cvp:%.*]] = alloca i8*, align 8
-// CHECK-LL-NEXT: br i1 false, label %[[castnull:.*]], label %[[castnotnull:.*]]
-// CHECK-LL: [[castnotnull]]
-// CHECK-LL-NEXT: br label %[[castend:.*]]
-// CHECK-LL: [[castnull]]
-// CHECK-LL-NEXT: br label %[[castend]]
-// CHECK-LL: [[castend]]
-// CHECK-LL-NEXT: [[v0:%.*]] = phi %class.test1_A* [ bitcast (%class.test1_D* @test1_d to %class.test1_A*), %[[castnotnull]] ], [ null, %[[castnull]] ]
-// CHECK-LL-NEXT: store %class.test1_A* [[v0]], %class.test1_A** [[bp]]
+// CHECK-LL-NEXT: store %class.test1_A* bitcast (%class.test1_D* @test1_d to %class.test1_A*), %class.test1_A** [[bp]]
// CHECK-LL-NEXT: br i1 false, label %[[castnull2:.*]], label %[[castnotnull1:.*]]
// CHECK-LL: [[castnotnull1]]
// CHECK-LL-NEXT: [[vtable:%.*]] = load i8** bitcast (%class.test1_D* @test1_d to i8**)
// CHECK-LL-NEXT: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([10 x i8]* @.str1, i32 0, i32 0), i32 12)
// CHECK-LL-NEXT: br label %[[ifend113]]
// CHECK-LL: [[ifend113]]
-// CHECK-LL-NEXT: br i1 false, label %[[castnull116:.*]], label %[[castnotnull115:.*]]
-// CHECK-LL: [[castnotnull115]]
-// CHECK-LL-NEXT: br label %[[castend117:.*]]
-// CHECK-LL: [[castnull116]]
-// CHECK-LL-NEXT: br label %[[castend117]]
-// CHECK-LL: [[castend117]]
-// CHECK-LL-NEXT: [[v62:%.*]] = phi %class.test1_E* [ bitcast (%class.test1_F* @test1_f to %class.test1_E*), %[[castnotnull115]] ], [ null, %[[castnull116]] ]
-// CHECK-LL-NEXT: store %class.test1_E* [[v62]], %class.test1_E** [[ep]]
+// CHECK-LL-NEXT: store %class.test1_E* bitcast (%class.test1_F* @test1_f to %class.test1_E*), %class.test1_E** [[ep]]
// CHECK-LL-NEXT: [[tmp118:%.*]] = load %class.test1_E** [[ep]]
-// CHECK-LL-NEXT: br i1 false, label %[[castnull120:.*]], label %[[castnotnull119:.*]]
-// CHECK-LL: [[castnotnull119]]
-// CHECK-LL-NEXT: br label %[[castend121:.*]]
-// CHECK-LL: [[castnull120]]
-// CHECK-LL-NEXT: br label %[[castend121]]
-// CHECK-LL: [[castend121]]
-// CHECK-LL-NEXT: [[v63:%.*]] = phi %class.test1_E* [ bitcast (%class.test1_F* @test1_f to %class.test1_E*), %[[castnotnull119]] ], [ null, %[[castnull120]] ]
-// CHECK-LL-NEXT: [[cmp122:%.*]] = icmp eq %class.test1_E* [[tmp118]], [[v63]]
+// CHECK-LL-NEXT: [[cmp122:%.*]] = icmp eq %class.test1_E* [[tmp118]], bitcast (%class.test1_F* @test1_f to %class.test1_E*) ; <i1> [#uses=1]
+
// CHECK-LL-NEXT: br i1 [[cmp122]], label %[[ifthen123:.*]], label %[[ifelse125:.*]]
// CHECK-LL: [[ifthen123]]
// CHECK-LL-NEXT: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([10 x i8]* @.str, i32 0, i32 0), i32 13)