blob: 37244398f335776a11c92857a72c820d60cce77b [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 McCallf16aa102010-08-22 21:01:12 +000044 bool isZeroInitializable(const MemberPointerType *MPT);
John McCallcf2c85e2010-08-22 04:16:24 +000045
John McCall93d557b2010-08-22 00:05:51 +000046 llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
47 llvm::Value *&This,
48 llvm::Value *MemFnPtr,
49 const MemberPointerType *MPT);
John McCall3023def2010-08-22 03:04:22 +000050
John McCalld608cdb2010-08-22 10:59:02 +000051 llvm::Value *EmitMemberFunctionPointerConversion(CodeGenFunction &CGF,
52 const CastExpr *E,
53 llvm::Value *Src);
John McCallcf2c85e2010-08-22 04:16:24 +000054
55 llvm::Constant *EmitMemberFunctionPointerConversion(llvm::Constant *C,
56 const CastExpr *E);
57
John McCallcf2c85e2010-08-22 04:16:24 +000058 llvm::Constant *EmitNullMemberFunctionPointer(const MemberPointerType *MPT);
59
John McCall875ab102010-08-22 06:43:33 +000060 llvm::Constant *EmitMemberFunctionPointer(const CXXMethodDecl *MD);
61
John McCalle9fd7eb2010-08-22 08:30:07 +000062 llvm::Value *EmitMemberFunctionPointerComparison(CodeGenFunction &CGF,
63 llvm::Value *L,
64 llvm::Value *R,
65 const MemberPointerType *MPT,
66 bool Inequality);
67
68 llvm::Value *EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF,
69 llvm::Value *Addr,
70 const MemberPointerType *MPT);
71
John McCall875ab102010-08-22 06:43:33 +000072private:
73 void GetMemberFunctionPointer(const CXXMethodDecl *MD,
74 llvm::Constant *(&Array)[2]);
Charles Davis3a811f12010-05-25 19:52:27 +000075};
John McCallee79a4c2010-08-21 22:46:04 +000076
77class ARMCXXABI : public ItaniumCXXABI {
78public:
John McCallbabc9a92010-08-22 00:59:17 +000079 ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
John McCallee79a4c2010-08-21 22:46:04 +000080};
Charles Davis3a811f12010-05-25 19:52:27 +000081}
82
Charles Davis071cc7d2010-08-16 03:33:14 +000083CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
Charles Davis3a811f12010-05-25 19:52:27 +000084 return new ItaniumCXXABI(CGM);
85}
86
John McCallee79a4c2010-08-21 22:46:04 +000087CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
88 return new ARMCXXABI(CGM);
89}
90
John McCall875ab102010-08-22 06:43:33 +000091void ItaniumCXXABI::GetMemberFunctionPointer(const CXXMethodDecl *MD,
92 llvm::Constant *(&MemPtr)[2]) {
John McCall875ab102010-08-22 06:43:33 +000093}
94
95
John McCallbabc9a92010-08-22 00:59:17 +000096/// In the Itanium and ARM ABIs, method pointers have the form:
97/// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
98///
99/// In the Itanium ABI:
100/// - method pointers are virtual if (memptr.ptr & 1) is nonzero
101/// - the this-adjustment is (memptr.adj)
102/// - the virtual offset is (memptr.ptr - 1)
103///
104/// In the ARM ABI:
105/// - method pointers are virtual if (memptr.adj & 1) is nonzero
106/// - the this-adjustment is (memptr.adj >> 1)
107/// - the virtual offset is (memptr.ptr)
108/// ARM uses 'adj' for the virtual flag because Thumb functions
109/// may be only single-byte aligned.
110///
111/// If the member is virtual, the adjusted 'this' pointer points
112/// to a vtable pointer from which the virtual offset is applied.
113///
114/// If the member is non-virtual, memptr.ptr is the address of
115/// the function to call.
John McCall93d557b2010-08-22 00:05:51 +0000116llvm::Value *
117ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
118 llvm::Value *&This,
119 llvm::Value *MemFnPtr,
120 const MemberPointerType *MPT) {
121 CGBuilderTy &Builder = CGF.Builder;
122
123 const FunctionProtoType *FPT =
124 MPT->getPointeeType()->getAs<FunctionProtoType>();
125 const CXXRecordDecl *RD =
126 cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
127
128 const llvm::FunctionType *FTy =
129 CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
130 FPT->isVariadic());
131
John McCallbabc9a92010-08-22 00:59:17 +0000132 const llvm::IntegerType *ptrdiff = CGF.IntPtrTy;
133 llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
John McCall93d557b2010-08-22 00:05:51 +0000134
John McCallbabc9a92010-08-22 00:59:17 +0000135 llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
136 llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
137 llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
138
John McCalld608cdb2010-08-22 10:59:02 +0000139 // Extract memptr.adj, which is in the second field.
140 llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
John McCallbabc9a92010-08-22 00:59:17 +0000141
142 // Compute the true adjustment.
143 llvm::Value *Adj = RawAdj;
144 if (IsARM)
145 Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
John McCall93d557b2010-08-22 00:05:51 +0000146
147 // Apply the adjustment and cast back to the original struct type
148 // for consistency.
John McCallbabc9a92010-08-22 00:59:17 +0000149 llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
150 Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
151 This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
John McCall93d557b2010-08-22 00:05:51 +0000152
153 // Load the function pointer.
John McCalld608cdb2010-08-22 10:59:02 +0000154 llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
John McCall93d557b2010-08-22 00:05:51 +0000155
156 // If the LSB in the function pointer is 1, the function pointer points to
157 // a virtual function.
John McCallbabc9a92010-08-22 00:59:17 +0000158 llvm::Value *IsVirtual;
159 if (IsARM)
160 IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
161 else
162 IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
163 IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
John McCall93d557b2010-08-22 00:05:51 +0000164 Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
165
166 // In the virtual path, the adjustment left 'This' pointing to the
167 // vtable of the correct base subobject. The "function pointer" is an
John McCallbabc9a92010-08-22 00:59:17 +0000168 // offset within the vtable (+1 for the virtual flag on non-ARM).
John McCall93d557b2010-08-22 00:05:51 +0000169 CGF.EmitBlock(FnVirtual);
170
171 // Cast the adjusted this to a pointer to vtable pointer and load.
172 const llvm::Type *VTableTy = Builder.getInt8PtrTy();
173 llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000174 VTable = Builder.CreateLoad(VTable, "memptr.vtable");
John McCall93d557b2010-08-22 00:05:51 +0000175
176 // Apply the offset.
John McCallbabc9a92010-08-22 00:59:17 +0000177 llvm::Value *VTableOffset = FnAsInt;
178 if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
179 VTable = Builder.CreateGEP(VTable, VTableOffset);
John McCall93d557b2010-08-22 00:05:51 +0000180
181 // Load the virtual function to call.
182 VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
John McCallbabc9a92010-08-22 00:59:17 +0000183 llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000184 CGF.EmitBranch(FnEnd);
185
186 // In the non-virtual path, the function pointer is actually a
187 // function pointer.
188 CGF.EmitBlock(FnNonVirtual);
189 llvm::Value *NonVirtualFn =
John McCallbabc9a92010-08-22 00:59:17 +0000190 Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
John McCall93d557b2010-08-22 00:05:51 +0000191
192 // We're done.
193 CGF.EmitBlock(FnEnd);
194 llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
195 Callee->reserveOperandSpace(2);
196 Callee->addIncoming(VirtualFn, FnVirtual);
197 Callee->addIncoming(NonVirtualFn, FnNonVirtual);
198 return Callee;
199}
John McCall3023def2010-08-22 03:04:22 +0000200
201/// Perform a derived-to-base or base-to-derived member pointer conversion.
John McCalld608cdb2010-08-22 10:59:02 +0000202llvm::Value *
203ItaniumCXXABI::EmitMemberFunctionPointerConversion(CodeGenFunction &CGF,
204 const CastExpr *E,
205 llvm::Value *Src) {
John McCall3023def2010-08-22 03:04:22 +0000206 assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer ||
207 E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer);
208
John McCalld608cdb2010-08-22 10:59:02 +0000209 if (isa<llvm::Constant>(Src))
210 return EmitMemberFunctionPointerConversion(cast<llvm::Constant>(Src), E);
211
John McCall3023def2010-08-22 03:04:22 +0000212 CGBuilderTy &Builder = CGF.Builder;
213
214 const MemberPointerType *SrcTy =
215 E->getSubExpr()->getType()->getAs<MemberPointerType>();
216 const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
217
218 const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
219 const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
220
John McCall3023def2010-08-22 03:04:22 +0000221 bool DerivedToBase =
222 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
223
224 const CXXRecordDecl *BaseDecl, *DerivedDecl;
225 if (DerivedToBase)
226 DerivedDecl = SrcDecl, BaseDecl = DestDecl;
227 else
228 BaseDecl = SrcDecl, DerivedDecl = DestDecl;
229
John McCalld608cdb2010-08-22 10:59:02 +0000230 llvm::Constant *Adj =
231 CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
232 E->path_begin(),
233 E->path_end());
234 if (!Adj) return Src;
John McCall875ab102010-08-22 06:43:33 +0000235
John McCalld608cdb2010-08-22 10:59:02 +0000236 // The this-adjustment is left-shifted by 1 on ARM.
237 if (IsARM) {
238 uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
239 Offset <<= 1;
240 Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
241 }
242
John McCalle14add42010-08-22 11:04:31 +0000243 llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
John McCalld608cdb2010-08-22 10:59:02 +0000244 llvm::Value *DstAdj;
245 if (DerivedToBase)
246 DstAdj = Builder.CreateSub(SrcAdj, Adj, "adj");
247 else
248 DstAdj = Builder.CreateAdd(SrcAdj, Adj, "adj");
249
John McCalle14add42010-08-22 11:04:31 +0000250 return Builder.CreateInsertValue(Src, DstAdj, 1);
John McCall3023def2010-08-22 03:04:22 +0000251}
John McCallcf2c85e2010-08-22 04:16:24 +0000252
253llvm::Constant *
254ItaniumCXXABI::EmitMemberFunctionPointerConversion(llvm::Constant *C,
255 const CastExpr *E) {
256 const MemberPointerType *SrcTy =
257 E->getSubExpr()->getType()->getAs<MemberPointerType>();
258 const MemberPointerType *DestTy =
259 E->getType()->getAs<MemberPointerType>();
260
261 bool DerivedToBase =
262 E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
263
264 const CXXRecordDecl *DerivedDecl;
265 if (DerivedToBase)
266 DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
267 else
268 DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
269
270 // Calculate the offset to the base class.
271 llvm::Constant *Offset =
272 CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
273 E->path_begin(),
274 E->path_end());
275 // If there's no offset, we're done.
276 if (!Offset) return C;
277
John McCall875ab102010-08-22 06:43:33 +0000278 // The this-adjustment is left-shifted by 1 on ARM.
279 if (IsARM) {
280 uint64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getZExtValue();
281 OffsetV <<= 1;
282 Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
283 }
284
John McCallcf2c85e2010-08-22 04:16:24 +0000285 llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
286
287 llvm::Constant *Values[2] = {
288 CS->getOperand(0),
289 llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset)
290 };
291 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
292 /*Packed=*/false);
293}
294
295
John McCallcf2c85e2010-08-22 04:16:24 +0000296llvm::Constant *
297ItaniumCXXABI::EmitNullMemberFunctionPointer(const MemberPointerType *MPT) {
John McCalld608cdb2010-08-22 10:59:02 +0000298 const llvm::Type *ptrdiff_t =
299 CGM.getTypes().ConvertType(CGM.getContext().getPointerDiffType());
300
301 llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
302 llvm::Constant *Values[2] = { Zero, Zero };
303 return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
304 /*Packed=*/false);
John McCallcf2c85e2010-08-22 04:16:24 +0000305}
306
John McCall875ab102010-08-22 06:43:33 +0000307llvm::Constant *
308ItaniumCXXABI::EmitMemberFunctionPointer(const CXXMethodDecl *MD) {
John McCalld608cdb2010-08-22 10:59:02 +0000309 assert(MD->isInstance() && "Member function must not be static!");
310 MD = MD->getCanonicalDecl();
311
312 CodeGenTypes &Types = CGM.getTypes();
313 const llvm::Type *ptrdiff_t =
314 Types.ConvertType(CGM.getContext().getPointerDiffType());
315
316 // Get the function pointer (or index if this is a virtual function).
317 llvm::Constant *MemPtr[2];
318 if (MD->isVirtual()) {
319 uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
320
321 // FIXME: We shouldn't use / 8 here.
322 uint64_t PointerWidthInBytes =
323 CGM.getContext().Target.getPointerWidth(0) / 8;
324 uint64_t VTableOffset = (Index * PointerWidthInBytes);
325
326 if (IsARM) {
327 // ARM C++ ABI 3.2.1:
328 // This ABI specifies that adj contains twice the this
329 // adjustment, plus 1 if the member function is virtual. The
330 // least significant bit of adj then makes exactly the same
331 // discrimination as the least significant bit of ptr does for
332 // Itanium.
333 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
334 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
335 } else {
336 // Itanium C++ ABI 2.3:
337 // For a virtual function, [the pointer field] is 1 plus the
338 // virtual table offset (in bytes) of the function,
339 // represented as a ptrdiff_t.
340 MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
341 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
342 }
343 } else {
344 const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
345 const llvm::Type *Ty;
346 // Check whether the function has a computable LLVM signature.
347 if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
348 // The function has a computable LLVM signature; use the correct type.
349 Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
350 } else {
351 // Use an arbitrary non-function type to tell GetAddrOfFunction that the
352 // function type is incomplete.
353 Ty = ptrdiff_t;
354 }
355
356 llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
357 MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
358 MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
359 }
John McCall875ab102010-08-22 06:43:33 +0000360
361 return llvm::ConstantStruct::get(CGM.getLLVMContext(),
John McCalld608cdb2010-08-22 10:59:02 +0000362 MemPtr, 2, /*Packed=*/false);
John McCall875ab102010-08-22 06:43:33 +0000363}
364
John McCalle9fd7eb2010-08-22 08:30:07 +0000365/// The comparison algorithm is pretty easy: the member pointers are
366/// the same if they're either bitwise identical *or* both null.
367///
368/// ARM is different here only because null-ness is more complicated.
369llvm::Value *
370ItaniumCXXABI::EmitMemberFunctionPointerComparison(CodeGenFunction &CGF,
371 llvm::Value *L,
372 llvm::Value *R,
373 const MemberPointerType *MPT,
374 bool Inequality) {
375 CGBuilderTy &Builder = CGF.Builder;
376
John McCalld608cdb2010-08-22 10:59:02 +0000377 llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
378 llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000379
380 // The Itanium tautology is:
381 // (L == R) <==> (L.ptr == R.ptr /\ (L.ptr == 0 \/ L.adj == R.adj))
382 // The ARM tautology is:
383 // (L == R) <==> (L.ptr == R.ptr /\
384 // (L.adj == R.adj \/
385 // (L.ptr == 0 /\ ((L.adj|R.adj) & 1) == 0)))
386 // The inequality tautologies have exactly the same structure, except
387 // applying De Morgan's laws.
388
389 llvm::ICmpInst::Predicate Eq;
390 llvm::Instruction::BinaryOps And, Or;
391 if (Inequality) {
392 Eq = llvm::ICmpInst::ICMP_NE;
393 And = llvm::Instruction::Or;
394 Or = llvm::Instruction::And;
395 } else {
396 Eq = llvm::ICmpInst::ICMP_EQ;
397 And = llvm::Instruction::And;
398 Or = llvm::Instruction::Or;
399 }
400
401 // This condition tests whether L.ptr == R.ptr. This must always be
402 // true for equality to hold.
403 llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
404
405 // This condition, together with the assumption that L.ptr == R.ptr,
406 // tests whether the pointers are both null. ARM imposes an extra
407 // condition.
408 llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
409 llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
410
411 // This condition tests whether L.adj == R.adj. If this isn't
412 // true, the pointers are unequal unless they're both null.
John McCalld608cdb2010-08-22 10:59:02 +0000413 llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
414 llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000415 llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
416
417 // Null member function pointers on ARM clear the low bit of Adj,
418 // so the zero condition has to check that neither low bit is set.
419 if (IsARM) {
420 llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
421
422 // Compute (l.adj | r.adj) & 1 and test it against zero.
423 llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
424 llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
425 llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
426 "cmp.or.adj");
427 EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
428 }
429
430 // Tie together all our conditions.
431 llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
432 Result = Builder.CreateBinOp(And, PtrEq, Result,
433 Inequality ? "memptr.ne" : "memptr.eq");
434 return Result;
435}
436
437llvm::Value *
438ItaniumCXXABI::EmitMemberFunctionPointerIsNotNull(CodeGenFunction &CGF,
439 llvm::Value *MemPtr,
440 const MemberPointerType *MPT) {
441 CGBuilderTy &Builder = CGF.Builder;
442
443 // In Itanium, a member function pointer is null if 'ptr' is null.
John McCalld608cdb2010-08-22 10:59:02 +0000444 llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
John McCalle9fd7eb2010-08-22 08:30:07 +0000445
446 llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
447 llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
448
449 // In ARM, it's that, plus the low bit of 'adj' must be zero.
450 if (IsARM) {
451 llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
John McCalld608cdb2010-08-22 10:59:02 +0000452 llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
John McCalle9fd7eb2010-08-22 08:30:07 +0000453 llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
454 llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
455 "memptr.notvirtual");
456 Result = Builder.CreateAnd(Result, IsNotVirtual);
457 }
458
459 return Result;
460}
John McCall875ab102010-08-22 06:43:33 +0000461
John McCallf16aa102010-08-22 21:01:12 +0000462/// The Itanium ABI requires non-zero initialization only for data
463/// member pointers, for which '0' is a valid offset.
464bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
465 return MPT->getPointeeType()->isFunctionType();
John McCallcf2c85e2010-08-22 04:16:24 +0000466}