blob: 4aa433c7ca531c36b32461d90b2530c15e59def8 [file] [log] [blame]
Charles Davis3a811f12010-05-25 19:52:27 +00001//===------- ItaniumCXXABI.cpp - Emit LLVM Code from ASTs for a Module ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides C++ code generation targetting the Itanium C++ ABI. The class
11// in this file generates structures that follow the Itanium C++ ABI, which is
12// documented at:
13// http://www.codesourcery.com/public/cxx-abi/abi.html
14// http://www.codesourcery.com/public/cxx-abi/abi-eh.html
John McCallee79a4c2010-08-21 22:46:04 +000015//
16// It also supports the closely-related ARM ABI, documented at:
17// http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041c/IHI0041C_cppabi.pdf
18//
Charles Davis3a811f12010-05-25 19:52:27 +000019//===----------------------------------------------------------------------===//
20
21#include "CGCXXABI.h"
John McCall93d557b2010-08-22 00:05:51 +000022#include "CodeGenFunction.h"
Charles Davis3a811f12010-05-25 19:52:27 +000023#include "CodeGenModule.h"
24#include "Mangle.h"
John McCall93d557b2010-08-22 00:05:51 +000025#include <clang/AST/Type.h>
26#include <llvm/Value.h>
Charles Davis3a811f12010-05-25 19:52:27 +000027
28using namespace clang;
John McCall93d557b2010-08-22 00:05:51 +000029using namespace CodeGen;
Charles Davis3a811f12010-05-25 19:52:27 +000030
31namespace {
Charles Davis071cc7d2010-08-16 03:33:14 +000032class ItaniumCXXABI : public CodeGen::CGCXXABI {
John McCall93d557b2010-08-22 00:05:51 +000033protected:
Charles Davis3a811f12010-05-25 19:52:27 +000034 CodeGen::MangleContext MangleCtx;
John McCallbabc9a92010-08-22 00:59:17 +000035 bool IsARM;
Charles Davis3a811f12010-05-25 19:52:27 +000036public:
John McCallbabc9a92010-08-22 00:59:17 +000037 ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
John McCalld608cdb2010-08-22 10:59:02 +000038 CGCXXABI(CGM), MangleCtx(CGM.getContext(), CGM.getDiags()), IsARM(IsARM) { }
Charles Davis3a811f12010-05-25 19:52:27 +000039
40 CodeGen::MangleContext &getMangleContext() {
41 return MangleCtx;
42 }
John McCall93d557b2010-08-22 00:05:51 +000043
John McCallcf2c85e2010-08-22 04:16:24 +000044 bool RequiresNonZeroInitializer(QualType T);
45 bool RequiresNonZeroInitializer(const CXXRecordDecl *D);
46
John McCall93d557b2010-08-22 00:05:51 +000047 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
48 llvm::Value *&This,
49 llvm::Value *MemFnPtr,
50 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000051
John McCalld608cdb2010-08-22 10:59:02 +000052 llvm::Value *EmitMemberFunctionPointerConversion(CodeGenFunction &CGF,
53 const CastExpr *E,
54 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000055
56 llvm::Constant *EmitMemberFunctionPointerConversion(llvm::Constant *C,
57 const CastExpr *E);
58
John McCallcf2c85e2010-08-22 04:16:24 +000059 llvm::Constant *EmitNullMemberFunctionPointer(const MemberPointerType *MPT);
60
John McCall875ab102010-08-22 06:43:33 +000061 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
62
John McCalle9fd7eb2010-08-22 08:30:07 +000063 llvm::Value *EmitMemberFunctionPointerComparison(CodeGenFunction &CGF,
64 llvm::Value *L,
65 llvm::Value *R,
66 const MemberPointerType *MPT,
67 bool Inequality);
68
69 llvm::Value *EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF,
70 llvm::Value *Addr,
71 const MemberPointerType *MPT);
72
John McCall875ab102010-08-22 06:43:33 +000073private:
74 void GetMemberFunctionPointer(const CXXMethodDecl *MD,
75 llvm::Constant *(&Array)[2]);
Charles Davis3a811f12010-05-25 19:52:27 +000076};
John McCallee79a4c2010-08-21 22:46:04 +000077
78class ARMCXXABI : public ItaniumCXXABI {
79public:
John McCallbabc9a92010-08-22 00:59:17 +000080 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCallee79a4c2010-08-21 22:46:04 +000081};
Charles Davis3a811f12010-05-25 19:52:27 +000082}
83
Charles Davis071cc7d2010-08-16 03:33:14 +000084CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +000085 return new ItaniumCXXABI(CGM);
86}
87
John McCallee79a4c2010-08-21 22:46:04 +000088CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
89 return new ARMCXXABI(CGM);
90}
91
John McCall875ab102010-08-22 06:43:33 +000092void ItaniumCXXABI::GetMemberFunctionPointer(const CXXMethodDecl *MD,
93 llvm::Constant *(&MemPtr)[2]) {
John McCall875ab102010-08-22 06:43:33 +000094}
95
96
John McCallbabc9a92010-08-22 00:59:17 +000097/// In the Itanium and ARM ABIs, method pointers have the form:
98/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
99///
100/// In the Itanium ABI:
101/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
102/// - the this-adjustment is (memptr.adj)
103/// - the virtual offset is (memptr.ptr - 1)
104///
105/// In the ARM ABI:
106/// - method pointers are virtual if (memptr.adj & 1) is nonzero
107/// - the this-adjustment is (memptr.adj >> 1)
108/// - the virtual offset is (memptr.ptr)
109/// ARM uses 'adj' for the virtual flag because Thumb functions
110/// may be only single-byte aligned.
111///
112/// If the member is virtual, the adjusted 'this' pointer points
113/// to a vtable pointer from which the virtual offset is applied.
114///
115/// If the member is non-virtual, memptr.ptr is the address of
116/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000117llvm::Value *
118ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
119 llvm::Value *&This,
120 llvm::Value *MemFnPtr,
121 const MemberPointerType *MPT) {
122 CGBuilderTy &Builder = CGF.Builder;
123
124 const FunctionProtoType *FPT =
125 MPT->getPointeeType()->getAs<FunctionProtoType>();
126 const CXXRecordDecl *RD =
127 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
128
129 const llvm::FunctionType *FTy =
130 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
131 FPT->isVariadic());
132
John McCallbabc9a92010-08-22 00:59:17 +0000133 const llvm::IntegerType *ptrdiff = CGF.IntPtrTy;
134 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000135
John McCallbabc9a92010-08-22 00:59:17 +0000136 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
137 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
138 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
139
John McCalld608cdb2010-08-22 10:59:02 +0000140 // Extract memptr.adj, which is in the second field.
141 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000142
143 // Compute the true adjustment.
144 llvm::Value *Adj = RawAdj;
145 if (IsARM)
146 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000147
148 // Apply the adjustment and cast back to the original struct type
149 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000150 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
151 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
152 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000153
154 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000155 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000156
157 // If the LSB in the function pointer is 1, the function pointer points to
158 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000159 llvm::Value *IsVirtual;
160 if (IsARM)
161 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
162 else
163 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
164 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000165 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
166
167 // In the virtual path, the adjustment left 'This' pointing to the
168 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000169 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000170 CGF.EmitBlock(FnVirtual);
171
172 // Cast the adjusted this to a pointer to vtable pointer and load.
173 const llvm::Type *VTableTy = Builder.getInt8PtrTy();
174 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000175 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000176
177 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000178 llvm::Value *VTableOffset = FnAsInt;
179 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
180 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000181
182 // Load the virtual function to call.
183 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000184 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000185 CGF.EmitBranch(FnEnd);
186
187 // In the non-virtual path, the function pointer is actually a
188 // function pointer.
189 CGF.EmitBlock(FnNonVirtual);
190 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000191 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000192
193 // We're done.
194 CGF.EmitBlock(FnEnd);
195 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
196 Callee->reserveOperandSpace(2);
197 Callee->addIncoming(VirtualFn, FnVirtual);
198 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
199 return Callee;
200}
John McCall3023def2010-08-22 03:04:22 +0000201
202/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCalld608cdb2010-08-22 10:59:02 +0000203llvm::Value *
204ItaniumCXXABI::EmitMemberFunctionPointerConversion(CodeGenFunction &CGF,
205 const CastExpr *E,
206 llvm::Value *Src) {
John McCall3023def2010-08-22 03:04:22 +0000207 assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer ||
208 E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer);
209
John McCalld608cdb2010-08-22 10:59:02 +0000210 if (isa<llvm::Constant>(Src))
211 return EmitMemberFunctionPointerConversion(cast<llvm::Constant>(Src), E);
212
John McCall3023def2010-08-22 03:04:22 +0000213 CGBuilderTy &Builder = CGF.Builder;
214
215 const MemberPointerType *SrcTy =
216 E->getSubExpr()->getType()->getAs<MemberPointerType>();
217 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
218
219 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
220 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
221
John McCall3023def2010-08-22 03:04:22 +0000222 bool DerivedToBase =
223 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
224
225 const CXXRecordDecl *BaseDecl, *DerivedDecl;
226 if (DerivedToBase)
227 DerivedDecl = SrcDecl, BaseDecl = DestDecl;
228 else
229 BaseDecl = SrcDecl, DerivedDecl = DestDecl;
230
John McCalld608cdb2010-08-22 10:59:02 +0000231 llvm::Constant *Adj =
232 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
233 E->path_begin(),
234 E->path_end());
235 if (!Adj) return Src;
John McCall875ab102010-08-22 06:43:33 +0000236
John McCalld608cdb2010-08-22 10:59:02 +0000237 // The this-adjustment is left-shifted by 1 on ARM.
238 if (IsARM) {
239 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
240 Offset <<= 1;
241 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
242 }
243
John McCalle14add42010-08-22 11:04:31 +0000244 llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
John McCalld608cdb2010-08-22 10:59:02 +0000245 llvm::Value *DstAdj;
246 if (DerivedToBase)
247 DstAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
248 else
249 DstAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
250
John McCalle14add42010-08-22 11:04:31 +0000251 return Builder.CreateInsertValue(Src, DstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000252}
John McCallcf2c85e2010-08-22 04:16:24 +0000253
254llvm::Constant *
255ItaniumCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C,
256 const CastExpr *E) {
257 const MemberPointerType *SrcTy =
258 E->getSubExpr()->getType()->getAs<MemberPointerType>();
259 const MemberPointerType *DestTy =
260 E->getType()->getAs<MemberPointerType>();
261
262 bool DerivedToBase =
263 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
264
265 const CXXRecordDecl *DerivedDecl;
266 if (DerivedToBase)
267 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
268 else
269 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
270
271 // Calculate the offset to the base class.
272 llvm::Constant *Offset =
273 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
274 E->path_begin(),
275 E->path_end());
276 // If there's no offset, we're done.
277 if (!Offset) return C;
278
John McCall875ab102010-08-22 06:43:33 +0000279 // The this-adjustment is left-shifted by 1 on ARM.
280 if (IsARM) {
281 uint64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getZExtValue();
282 OffsetV <<= 1;
283 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
284 }
285
John McCallcf2c85e2010-08-22 04:16:24 +0000286 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
287
288 llvm::Constant *Values[2] = {
289 CS->getOperand(0),
290 llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset)
291 };
292 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
293 /*Packed=*/false);
294}
295
296
John McCallcf2c85e2010-08-22 04:16:24 +0000297llvm::Constant *
298ItaniumCXXABI::EmitNullMemberFunctionPointer(const MemberPointerType *MPT) {
John McCalld608cdb2010-08-22 10:59:02 +0000299 const llvm::Type *ptrdiff_t =
300 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
301
302 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
303 llvm::Constant *Values[2] = { Zero, Zero };
304 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
305 /*Packed=*/false);
John McCallcf2c85e2010-08-22 04:16:24 +0000306}
307
John McCall875ab102010-08-22 06:43:33 +0000308llvm::Constant *
309ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
John McCalld608cdb2010-08-22 10:59:02 +0000310 assert(MD->isInstance() && "Member function must not be static!");
311 MD = MD->getCanonicalDecl();
312
313 CodeGenTypes &Types = CGM.getTypes();
314 const llvm::Type *ptrdiff_t =
315 Types.ConvertType(CGM.getContext().getPointerDiffType());
316
317 // Get the function pointer (or index if this is a virtual function).
318 llvm::Constant *MemPtr[2];
319 if (MD->isVirtual()) {
320 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
321
322 // FIXME: We shouldn't use / 8 here.
323 uint64_t PointerWidthInBytes =
324 CGM.getContext().Target.getPointerWidth(0) / 8;
325 uint64_t VTableOffset = (Index * PointerWidthInBytes);
326
327 if (IsARM) {
328 // ARM C++ ABI 3.2.1:
329 // This ABI specifies that adj contains twice the this
330 // adjustment, plus 1 if the member function is virtual. The
331 // least significant bit of adj then makes exactly the same
332 // discrimination as the least significant bit of ptr does for
333 // Itanium.
334 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
335 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
336 } else {
337 // Itanium C++ ABI 2.3:
338 // For a virtual function, [the pointer field] is 1 plus the
339 // virtual table offset (in bytes) of the function,
340 // represented as a ptrdiff_t.
341 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
342 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
343 }
344 } else {
345 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
346 const llvm::Type *Ty;
347 // Check whether the function has a computable LLVM signature.
348 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
349 // The function has a computable LLVM signature; use the correct type.
350 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
351 } else {
352 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
353 // function type is incomplete.
354 Ty = ptrdiff_t;
355 }
356
357 llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
358 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
359 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
360 }
John McCall875ab102010-08-22 06:43:33 +0000361
362 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
John McCalld608cdb2010-08-22 10:59:02 +0000363 MemPtr, 2, /*Packed=*/false);
John McCall875ab102010-08-22 06:43:33 +0000364}
365
John McCalle9fd7eb2010-08-22 08:30:07 +0000366/// The comparison algorithm is pretty easy: the member pointers are
367/// the same if they're either bitwise identical *or* both null.
368///
369/// ARM is different here only because null-ness is more complicated.
370llvm::Value *
371ItaniumCXXABI::EmitMemberFunctionPointerComparison(CodeGenFunction &CGF,
372 llvm::Value *L,
373 llvm::Value *R,
374 const MemberPointerType *MPT,
375 bool Inequality) {
376 CGBuilderTy &Builder = CGF.Builder;
377
John McCalld608cdb2010-08-22 10:59:02 +0000378 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
379 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000380
381 // The Itanium tautology is:
382 // (L == R) <==> (L.ptr == R.ptr /\ (L.ptr == 0 \/ L.adj == R.adj))
383 // The ARM tautology is:
384 // (L == R) <==> (L.ptr == R.ptr /\
385 // (L.adj == R.adj \/
386 // (L.ptr == 0 /\ ((L.adj|R.adj) & 1) == 0)))
387 // The inequality tautologies have exactly the same structure, except
388 // applying De Morgan's laws.
389
390 llvm::ICmpInst::Predicate Eq;
391 llvm::Instruction::BinaryOps And, Or;
392 if (Inequality) {
393 Eq = llvm::ICmpInst::ICMP_NE;
394 And = llvm::Instruction::Or;
395 Or = llvm::Instruction::And;
396 } else {
397 Eq = llvm::ICmpInst::ICMP_EQ;
398 And = llvm::Instruction::And;
399 Or = llvm::Instruction::Or;
400 }
401
402 // This condition tests whether L.ptr == R.ptr. This must always be
403 // true for equality to hold.
404 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
405
406 // This condition, together with the assumption that L.ptr == R.ptr,
407 // tests whether the pointers are both null. ARM imposes an extra
408 // condition.
409 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
410 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
411
412 // This condition tests whether L.adj == R.adj. If this isn't
413 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000414 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
415 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000416 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
417
418 // Null member function pointers on ARM clear the low bit of Adj,
419 // so the zero condition has to check that neither low bit is set.
420 if (IsARM) {
421 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
422
423 // Compute (l.adj | r.adj) & 1 and test it against zero.
424 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
425 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
426 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
427 "cmp.or.adj");
428 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
429 }
430
431 // Tie together all our conditions.
432 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
433 Result = Builder.CreateBinOp(And, PtrEq, Result,
434 Inequality ? "memptr.ne" : "memptr.eq");
435 return Result;
436}
437
438llvm::Value *
439ItaniumCXXABI::EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF,
440 llvm::Value *MemPtr,
441 const MemberPointerType *MPT) {
442 CGBuilderTy &Builder = CGF.Builder;
443
444 // In Itanium, a member function pointer is null if 'ptr' is null.
John McCalld608cdb2010-08-22 10:59:02 +0000445 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000446
447 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
448 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
449
450 // In ARM, it's that, plus the low bit of 'adj' must be zero.
451 if (IsARM) {
452 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000453 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000454 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
455 llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
456 "memptr.notvirtual");
457 Result = Builder.CreateAnd(Result, IsNotVirtual);
458 }
459
460 return Result;
461}
John McCall875ab102010-08-22 06:43:33 +0000462
John McCallcf2c85e2010-08-22 04:16:24 +0000463bool ItaniumCXXABI::RequiresNonZeroInitializer(QualType T) {
464 return CGM.getTypes().ContainsPointerToDataMember(T);
465}
466
467bool ItaniumCXXABI::RequiresNonZeroInitializer(const CXXRecordDecl *D) {
468 return CGM.getTypes().ContainsPointerToDataMember(D);
469}