ItaniumCXXABI.cpp revision 0bab0cdab751248ca389a5592bcb70eac5d39260
1//===------- 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
15//
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//
19//===----------------------------------------------------------------------===//
20
21#include "CGCXXABI.h"
22#include "CGRecordLayout.h"
23#include "CodeGenFunction.h"
24#include "CodeGenModule.h"
25#include "Mangle.h"
26#include <clang/AST/Type.h>
27#include <llvm/Target/TargetData.h>
28#include <llvm/Value.h>
29
30using namespace clang;
31using namespace CodeGen;
32
33namespace {
34class ItaniumCXXABI : public CodeGen::CGCXXABI {
35private:
36  const llvm::IntegerType *PtrDiffTy;
37protected:
38  CodeGen::MangleContext MangleCtx;
39  bool IsARM;
40
41  // It's a little silly for us to cache this.
42  const llvm::IntegerType *getPtrDiffTy() {
43    if (!PtrDiffTy) {
44      QualType T = CGM.getContext().getPointerDiffType();
45      const llvm::Type *Ty = CGM.getTypes().ConvertTypeRecursive(T);
46      PtrDiffTy = cast<llvm::IntegerType>(Ty);
47    }
48    return PtrDiffTy;
49  }
50
51public:
52  ItaniumCXXABI(CodeGen::CodeGenModule &CGM, bool IsARM = false) :
53    CGCXXABI(CGM), PtrDiffTy(0), MangleCtx(CGM.getContext(), CGM.getDiags()),
54    IsARM(IsARM) { }
55
56  CodeGen::MangleContext &getMangleContext() {
57    return MangleCtx;
58  }
59
60  bool isZeroInitializable(const MemberPointerType *MPT);
61
62  const llvm::Type *ConvertMemberPointerType(const MemberPointerType *MPT);
63
64  llvm::Value *EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
65                                               llvm::Value *&This,
66                                               llvm::Value *MemFnPtr,
67                                               const MemberPointerType *MPT);
68
69  llvm::Value *EmitMemberPointerConversion(CodeGenFunction &CGF,
70                                           const CastExpr *E,
71                                           llvm::Value *Src);
72
73  llvm::Constant *EmitMemberPointerConversion(llvm::Constant *C,
74                                              const CastExpr *E);
75
76  llvm::Constant *EmitNullMemberPointer(const MemberPointerType *MPT);
77
78  llvm::Constant *EmitMemberPointer(const CXXMethodDecl *MD);
79  llvm::Constant *EmitMemberPointer(const FieldDecl *FD);
80
81  llvm::Value *EmitMemberPointerComparison(CodeGenFunction &CGF,
82                                           llvm::Value *L,
83                                           llvm::Value *R,
84                                           const MemberPointerType *MPT,
85                                           bool Inequality);
86
87  llvm::Value *EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
88                                          llvm::Value *Addr,
89                                          const MemberPointerType *MPT);
90};
91
92class ARMCXXABI : public ItaniumCXXABI {
93public:
94  ARMCXXABI(CodeGen::CodeGenModule &CGM) : ItaniumCXXABI(CGM, /*ARM*/ true) {}
95};
96}
97
98CodeGen::CGCXXABI *CodeGen::CreateItaniumCXXABI(CodeGenModule &CGM) {
99  return new ItaniumCXXABI(CGM);
100}
101
102CodeGen::CGCXXABI *CodeGen::CreateARMCXXABI(CodeGenModule &CGM) {
103  return new ARMCXXABI(CGM);
104}
105
106const llvm::Type *
107ItaniumCXXABI::ConvertMemberPointerType(const MemberPointerType *MPT) {
108  if (MPT->isMemberDataPointer())
109    return getPtrDiffTy();
110  else
111    return llvm::StructType::get(CGM.getLLVMContext(),
112                                 getPtrDiffTy(), getPtrDiffTy(), NULL);
113}
114
115/// In the Itanium and ARM ABIs, method pointers have the form:
116///   struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
117///
118/// In the Itanium ABI:
119///  - method pointers are virtual if (memptr.ptr & 1) is nonzero
120///  - the this-adjustment is (memptr.adj)
121///  - the virtual offset is (memptr.ptr - 1)
122///
123/// In the ARM ABI:
124///  - method pointers are virtual if (memptr.adj & 1) is nonzero
125///  - the this-adjustment is (memptr.adj >> 1)
126///  - the virtual offset is (memptr.ptr)
127/// ARM uses 'adj' for the virtual flag because Thumb functions
128/// may be only single-byte aligned.
129///
130/// If the member is virtual, the adjusted 'this' pointer points
131/// to a vtable pointer from which the virtual offset is applied.
132///
133/// If the member is non-virtual, memptr.ptr is the address of
134/// the function to call.
135llvm::Value *
136ItaniumCXXABI::EmitLoadOfMemberFunctionPointer(CodeGenFunction &CGF,
137                                               llvm::Value *&This,
138                                               llvm::Value *MemFnPtr,
139                                               const MemberPointerType *MPT) {
140  CGBuilderTy &Builder = CGF.Builder;
141
142  const FunctionProtoType *FPT =
143    MPT->getPointeeType()->getAs<FunctionProtoType>();
144  const CXXRecordDecl *RD =
145    cast<CXXRecordDecl>(MPT->getClass()->getAs<RecordType>()->getDecl());
146
147  const llvm::FunctionType *FTy =
148    CGM.getTypes().GetFunctionType(CGM.getTypes().getFunctionInfo(RD, FPT),
149                                   FPT->isVariadic());
150
151  const llvm::IntegerType *ptrdiff = getPtrDiffTy();
152  llvm::Constant *ptrdiff_1 = llvm::ConstantInt::get(ptrdiff, 1);
153
154  llvm::BasicBlock *FnVirtual = CGF.createBasicBlock("memptr.virtual");
155  llvm::BasicBlock *FnNonVirtual = CGF.createBasicBlock("memptr.nonvirtual");
156  llvm::BasicBlock *FnEnd = CGF.createBasicBlock("memptr.end");
157
158  // Extract memptr.adj, which is in the second field.
159  llvm::Value *RawAdj = Builder.CreateExtractValue(MemFnPtr, 1, "memptr.adj");
160
161  // Compute the true adjustment.
162  llvm::Value *Adj = RawAdj;
163  if (IsARM)
164    Adj = Builder.CreateAShr(Adj, ptrdiff_1, "memptr.adj.shifted");
165
166  // Apply the adjustment and cast back to the original struct type
167  // for consistency.
168  llvm::Value *Ptr = Builder.CreateBitCast(This, Builder.getInt8PtrTy());
169  Ptr = Builder.CreateInBoundsGEP(Ptr, Adj);
170  This = Builder.CreateBitCast(Ptr, This->getType(), "this.adjusted");
171
172  // Load the function pointer.
173  llvm::Value *FnAsInt = Builder.CreateExtractValue(MemFnPtr, 0, "memptr.ptr");
174
175  // If the LSB in the function pointer is 1, the function pointer points to
176  // a virtual function.
177  llvm::Value *IsVirtual;
178  if (IsARM)
179    IsVirtual = Builder.CreateAnd(RawAdj, ptrdiff_1);
180  else
181    IsVirtual = Builder.CreateAnd(FnAsInt, ptrdiff_1);
182  IsVirtual = Builder.CreateIsNotNull(IsVirtual, "memptr.isvirtual");
183  Builder.CreateCondBr(IsVirtual, FnVirtual, FnNonVirtual);
184
185  // In the virtual path, the adjustment left 'This' pointing to the
186  // vtable of the correct base subobject.  The "function pointer" is an
187  // offset within the vtable (+1 for the virtual flag on non-ARM).
188  CGF.EmitBlock(FnVirtual);
189
190  // Cast the adjusted this to a pointer to vtable pointer and load.
191  const llvm::Type *VTableTy = Builder.getInt8PtrTy();
192  llvm::Value *VTable = Builder.CreateBitCast(This, VTableTy->getPointerTo());
193  VTable = Builder.CreateLoad(VTable, "memptr.vtable");
194
195  // Apply the offset.
196  llvm::Value *VTableOffset = FnAsInt;
197  if (!IsARM) VTableOffset = Builder.CreateSub(VTableOffset, ptrdiff_1);
198  VTable = Builder.CreateGEP(VTable, VTableOffset);
199
200  // Load the virtual function to call.
201  VTable = Builder.CreateBitCast(VTable, FTy->getPointerTo()->getPointerTo());
202  llvm::Value *VirtualFn = Builder.CreateLoad(VTable, "memptr.virtualfn");
203  CGF.EmitBranch(FnEnd);
204
205  // In the non-virtual path, the function pointer is actually a
206  // function pointer.
207  CGF.EmitBlock(FnNonVirtual);
208  llvm::Value *NonVirtualFn =
209    Builder.CreateIntToPtr(FnAsInt, FTy->getPointerTo(), "memptr.nonvirtualfn");
210
211  // We're done.
212  CGF.EmitBlock(FnEnd);
213  llvm::PHINode *Callee = Builder.CreatePHI(FTy->getPointerTo());
214  Callee->reserveOperandSpace(2);
215  Callee->addIncoming(VirtualFn, FnVirtual);
216  Callee->addIncoming(NonVirtualFn, FnNonVirtual);
217  return Callee;
218}
219
220/// Perform a derived-to-base or base-to-derived member pointer conversion.
221///
222/// Obligatory offset/adjustment diagram:
223///         <-- offset -->          <-- adjustment -->
224///   |--------------------------|----------------------|--------------------|
225///   ^Derived address point     ^Base address point    ^Member address point
226///
227/// So when converting a base member pointer to a derived member pointer,
228/// we add the offset to the adjustment because the address point has
229/// decreased;  and conversely, when converting a derived MP to a base MP
230/// we subtract the offset from the adjustment because the address point
231/// has increased.
232///
233/// The standard forbids (at compile time) conversion to and from
234/// virtual bases, which is why we don't have to consider them here.
235///
236/// The standard forbids (at run time) casting a derived MP to a base
237/// MP when the derived MP does not point to a member of the base.
238/// This is why -1 is a reasonable choice for null data member
239/// pointers.
240llvm::Value *
241ItaniumCXXABI::EmitMemberPointerConversion(CodeGenFunction &CGF,
242                                           const CastExpr *E,
243                                           llvm::Value *Src) {
244  assert(E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer ||
245         E->getCastKind() == CastExpr::CK_BaseToDerivedMemberPointer);
246
247  if (isa<llvm::Constant>(Src))
248    return EmitMemberPointerConversion(cast<llvm::Constant>(Src), E);
249
250  CGBuilderTy &Builder = CGF.Builder;
251
252  const MemberPointerType *SrcTy =
253    E->getSubExpr()->getType()->getAs<MemberPointerType>();
254  const MemberPointerType *DestTy = E->getType()->getAs<MemberPointerType>();
255
256  const CXXRecordDecl *SrcDecl = SrcTy->getClass()->getAsCXXRecordDecl();
257  const CXXRecordDecl *DestDecl = DestTy->getClass()->getAsCXXRecordDecl();
258
259  bool DerivedToBase =
260    E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
261
262  const CXXRecordDecl *BaseDecl, *DerivedDecl;
263  if (DerivedToBase)
264    DerivedDecl = SrcDecl, BaseDecl = DestDecl;
265  else
266    BaseDecl = SrcDecl, DerivedDecl = DestDecl;
267
268  llvm::Constant *Adj =
269    CGF.CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
270                                         E->path_begin(),
271                                         E->path_end());
272  if (!Adj) return Src;
273
274  // For member data pointers, this is just a matter of adding the
275  // offset if the source is non-null.
276  if (SrcTy->isMemberDataPointer()) {
277    llvm::Value *Dst;
278    if (DerivedToBase)
279      Dst = Builder.CreateNSWSub(Src, Adj, "adj");
280    else
281      Dst = Builder.CreateNSWAdd(Src, Adj, "adj");
282
283    // Null check.
284    llvm::Value *Null = llvm::Constant::getAllOnesValue(Src->getType());
285    llvm::Value *IsNull = Builder.CreateICmpEQ(Src, Null, "memptr.isnull");
286    return Builder.CreateSelect(IsNull, Src, Dst);
287  }
288
289  // The this-adjustment is left-shifted by 1 on ARM.
290  if (IsARM) {
291    uint64_t Offset = cast<llvm::ConstantInt>(Adj)->getZExtValue();
292    Offset <<= 1;
293    Adj = llvm::ConstantInt::get(Adj->getType(), Offset);
294  }
295
296  llvm::Value *SrcAdj = Builder.CreateExtractValue(Src, 1, "src.adj");
297  llvm::Value *DstAdj;
298  if (DerivedToBase)
299    DstAdj = Builder.CreateNSWSub(SrcAdj, Adj, "adj");
300  else
301    DstAdj = Builder.CreateNSWAdd(SrcAdj, Adj, "adj");
302
303  return Builder.CreateInsertValue(Src, DstAdj, 1);
304}
305
306llvm::Constant *
307ItaniumCXXABI::EmitMemberPointerConversion(llvm::Constant *C,
308                                           const CastExpr *E) {
309  const MemberPointerType *SrcTy =
310    E->getSubExpr()->getType()->getAs<MemberPointerType>();
311  const MemberPointerType *DestTy =
312    E->getType()->getAs<MemberPointerType>();
313
314  bool DerivedToBase =
315    E->getCastKind() == CastExpr::CK_DerivedToBaseMemberPointer;
316
317  const CXXRecordDecl *DerivedDecl;
318  if (DerivedToBase)
319    DerivedDecl = SrcTy->getClass()->getAsCXXRecordDecl();
320  else
321    DerivedDecl = DestTy->getClass()->getAsCXXRecordDecl();
322
323  // Calculate the offset to the base class.
324  llvm::Constant *Offset =
325    CGM.GetNonVirtualBaseClassOffset(DerivedDecl,
326                                     E->path_begin(),
327                                     E->path_end());
328  // If there's no offset, we're done.
329  if (!Offset) return C;
330
331  // If the source is a member data pointer, we have to do a null
332  // check and then add the offset.  In the common case, we can fold
333  // away the offset.
334  if (SrcTy->isMemberDataPointer()) {
335    assert(C->getType() == getPtrDiffTy());
336
337    // If it's a constant int, just create a new constant int.
338    if (llvm::ConstantInt *CI = dyn_cast<llvm::ConstantInt>(C)) {
339      int64_t Src = CI->getSExtValue();
340
341      // Null converts to null.
342      if (Src == -1) return CI;
343
344      // Otherwise, just add the offset.
345      int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
346      int64_t Dst = (DerivedToBase ? Src - OffsetV : Src + OffsetV);
347      return llvm::ConstantInt::get(CI->getType(), Dst, /*signed*/ true);
348    }
349
350    // Otherwise, we have to form a constant select expression.
351    llvm::Constant *Null = llvm::Constant::getAllOnesValue(C->getType());
352
353    llvm::Constant *IsNull =
354      llvm::ConstantExpr::getICmp(llvm::ICmpInst::ICMP_EQ, C, Null);
355
356    llvm::Constant *Dst;
357    if (DerivedToBase)
358      Dst = llvm::ConstantExpr::getNSWSub(C, Offset);
359    else
360      Dst = llvm::ConstantExpr::getNSWAdd(C, Offset);
361
362    return llvm::ConstantExpr::getSelect(IsNull, Null, Dst);
363  }
364
365  // The this-adjustment is left-shifted by 1 on ARM.
366  if (IsARM) {
367    int64_t OffsetV = cast<llvm::ConstantInt>(Offset)->getSExtValue();
368    OffsetV <<= 1;
369    Offset = llvm::ConstantInt::get(Offset->getType(), OffsetV);
370  }
371
372  llvm::ConstantStruct *CS = cast<llvm::ConstantStruct>(C);
373
374  llvm::Constant *Values[2] = { CS->getOperand(0), 0 };
375  if (DerivedToBase)
376    Values[1] = llvm::ConstantExpr::getSub(CS->getOperand(1), Offset);
377  else
378    Values[1] = llvm::ConstantExpr::getAdd(CS->getOperand(1), Offset);
379
380  return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
381                                   /*Packed=*/false);
382}
383
384
385llvm::Constant *
386ItaniumCXXABI::EmitNullMemberPointer(const MemberPointerType *MPT) {
387  const llvm::Type *ptrdiff_t = getPtrDiffTy();
388
389  // Itanium C++ ABI 2.3:
390  //   A NULL pointer is represented as -1.
391  if (MPT->isMemberDataPointer())
392    return llvm::ConstantInt::get(ptrdiff_t, -1ULL, /*isSigned=*/true);
393
394  llvm::Constant *Zero = llvm::ConstantInt::get(ptrdiff_t, 0);
395  llvm::Constant *Values[2] = { Zero, Zero };
396  return llvm::ConstantStruct::get(CGM.getLLVMContext(), Values, 2,
397                                   /*Packed=*/false);
398}
399
400llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const FieldDecl *FD) {
401  // Itanium C++ ABI 2.3:
402  //   A pointer to data member is an offset from the base address of
403  //   the class object containing it, represented as a ptrdiff_t
404
405  QualType ClassType = CGM.getContext().getTypeDeclType(FD->getParent());
406  const llvm::StructType *ClassLTy =
407    cast<llvm::StructType>(CGM.getTypes().ConvertType(ClassType));
408
409  const CGRecordLayout &RL = CGM.getTypes().getCGRecordLayout(FD->getParent());
410  unsigned FieldNo = RL.getLLVMFieldNo(FD);
411  uint64_t Offset =
412    CGM.getTargetData().getStructLayout(ClassLTy)->getElementOffset(FieldNo);
413
414  return llvm::ConstantInt::get(getPtrDiffTy(), Offset);
415}
416
417llvm::Constant *ItaniumCXXABI::EmitMemberPointer(const CXXMethodDecl *MD) {
418  assert(MD->isInstance() && "Member function must not be static!");
419  MD = MD->getCanonicalDecl();
420
421  CodeGenTypes &Types = CGM.getTypes();
422  const llvm::Type *ptrdiff_t = getPtrDiffTy();
423
424  // Get the function pointer (or index if this is a virtual function).
425  llvm::Constant *MemPtr[2];
426  if (MD->isVirtual()) {
427    uint64_t Index = CGM.getVTables().getMethodVTableIndex(MD);
428
429    // FIXME: We shouldn't use / 8 here.
430    uint64_t PointerWidthInBytes =
431      CGM.getContext().Target.getPointerWidth(0) / 8;
432    uint64_t VTableOffset = (Index * PointerWidthInBytes);
433
434    if (IsARM) {
435      // ARM C++ ABI 3.2.1:
436      //   This ABI specifies that adj contains twice the this
437      //   adjustment, plus 1 if the member function is virtual. The
438      //   least significant bit of adj then makes exactly the same
439      //   discrimination as the least significant bit of ptr does for
440      //   Itanium.
441      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset);
442      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 1);
443    } else {
444      // Itanium C++ ABI 2.3:
445      //   For a virtual function, [the pointer field] is 1 plus the
446      //   virtual table offset (in bytes) of the function,
447      //   represented as a ptrdiff_t.
448      MemPtr[0] = llvm::ConstantInt::get(ptrdiff_t, VTableOffset + 1);
449      MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
450    }
451  } else {
452    const FunctionProtoType *FPT = MD->getType()->getAs<FunctionProtoType>();
453    const llvm::Type *Ty;
454    // Check whether the function has a computable LLVM signature.
455    if (!CodeGenTypes::VerifyFuncTypeComplete(FPT)) {
456      // The function has a computable LLVM signature; use the correct type.
457      Ty = Types.GetFunctionType(Types.getFunctionInfo(MD), FPT->isVariadic());
458    } else {
459      // Use an arbitrary non-function type to tell GetAddrOfFunction that the
460      // function type is incomplete.
461      Ty = ptrdiff_t;
462    }
463
464    llvm::Constant *Addr = CGM.GetAddrOfFunction(MD, Ty);
465    MemPtr[0] = llvm::ConstantExpr::getPtrToInt(Addr, ptrdiff_t);
466    MemPtr[1] = llvm::ConstantInt::get(ptrdiff_t, 0);
467  }
468
469  return llvm::ConstantStruct::get(CGM.getLLVMContext(),
470                                   MemPtr, 2, /*Packed=*/false);
471}
472
473/// The comparison algorithm is pretty easy: the member pointers are
474/// the same if they're either bitwise identical *or* both null.
475///
476/// ARM is different here only because null-ness is more complicated.
477llvm::Value *
478ItaniumCXXABI::EmitMemberPointerComparison(CodeGenFunction &CGF,
479                                           llvm::Value *L,
480                                           llvm::Value *R,
481                                           const MemberPointerType *MPT,
482                                           bool Inequality) {
483  CGBuilderTy &Builder = CGF.Builder;
484
485  llvm::ICmpInst::Predicate Eq;
486  llvm::Instruction::BinaryOps And, Or;
487  if (Inequality) {
488    Eq = llvm::ICmpInst::ICMP_NE;
489    And = llvm::Instruction::Or;
490    Or = llvm::Instruction::And;
491  } else {
492    Eq = llvm::ICmpInst::ICMP_EQ;
493    And = llvm::Instruction::And;
494    Or = llvm::Instruction::Or;
495  }
496
497  // Member data pointers are easy because there's a unique null
498  // value, so it just comes down to bitwise equality.
499  if (MPT->isMemberDataPointer())
500    return Builder.CreateICmp(Eq, L, R);
501
502  // For member function pointers, the tautologies are more complex.
503  // The Itanium tautology is:
504  //   (L == R) <==> (L.ptr == R.ptr /\ (L.ptr == 0 \/ L.adj == R.adj))
505  // The ARM tautology is:
506  //   (L == R) <==> (L.ptr == R.ptr /\
507  //                  (L.adj == R.adj \/
508  //                   (L.ptr == 0 /\ ((L.adj|R.adj) & 1) == 0)))
509  // The inequality tautologies have exactly the same structure, except
510  // applying De Morgan's laws.
511
512  llvm::Value *LPtr = Builder.CreateExtractValue(L, 0, "lhs.memptr.ptr");
513  llvm::Value *RPtr = Builder.CreateExtractValue(R, 0, "rhs.memptr.ptr");
514
515  // This condition tests whether L.ptr == R.ptr.  This must always be
516  // true for equality to hold.
517  llvm::Value *PtrEq = Builder.CreateICmp(Eq, LPtr, RPtr, "cmp.ptr");
518
519  // This condition, together with the assumption that L.ptr == R.ptr,
520  // tests whether the pointers are both null.  ARM imposes an extra
521  // condition.
522  llvm::Value *Zero = llvm::Constant::getNullValue(LPtr->getType());
523  llvm::Value *EqZero = Builder.CreateICmp(Eq, LPtr, Zero, "cmp.ptr.null");
524
525  // This condition tests whether L.adj == R.adj.  If this isn't
526  // true, the pointers are unequal unless they're both null.
527  llvm::Value *LAdj = Builder.CreateExtractValue(L, 1, "lhs.memptr.adj");
528  llvm::Value *RAdj = Builder.CreateExtractValue(R, 1, "rhs.memptr.adj");
529  llvm::Value *AdjEq = Builder.CreateICmp(Eq, LAdj, RAdj, "cmp.adj");
530
531  // Null member function pointers on ARM clear the low bit of Adj,
532  // so the zero condition has to check that neither low bit is set.
533  if (IsARM) {
534    llvm::Value *One = llvm::ConstantInt::get(LPtr->getType(), 1);
535
536    // Compute (l.adj | r.adj) & 1 and test it against zero.
537    llvm::Value *OrAdj = Builder.CreateOr(LAdj, RAdj, "or.adj");
538    llvm::Value *OrAdjAnd1 = Builder.CreateAnd(OrAdj, One);
539    llvm::Value *OrAdjAnd1EqZero = Builder.CreateICmp(Eq, OrAdjAnd1, Zero,
540                                                      "cmp.or.adj");
541    EqZero = Builder.CreateBinOp(And, EqZero, OrAdjAnd1EqZero);
542  }
543
544  // Tie together all our conditions.
545  llvm::Value *Result = Builder.CreateBinOp(Or, EqZero, AdjEq);
546  Result = Builder.CreateBinOp(And, PtrEq, Result,
547                               Inequality ? "memptr.ne" : "memptr.eq");
548  return Result;
549}
550
551llvm::Value *
552ItaniumCXXABI::EmitMemberPointerIsNotNull(CodeGenFunction &CGF,
553                                          llvm::Value *MemPtr,
554                                          const MemberPointerType *MPT) {
555  CGBuilderTy &Builder = CGF.Builder;
556
557  /// For member data pointers, this is just a check against -1.
558  if (MPT->isMemberDataPointer()) {
559    assert(MemPtr->getType() == getPtrDiffTy());
560    llvm::Value *NegativeOne =
561      llvm::Constant::getAllOnesValue(MemPtr->getType());
562    return Builder.CreateICmpNE(MemPtr, NegativeOne, "memptr.tobool");
563  }
564
565  // In Itanium, a member function pointer is null if 'ptr' is null.
566  llvm::Value *Ptr = Builder.CreateExtractValue(MemPtr, 0, "memptr.ptr");
567
568  llvm::Constant *Zero = llvm::ConstantInt::get(Ptr->getType(), 0);
569  llvm::Value *Result = Builder.CreateICmpNE(Ptr, Zero, "memptr.tobool");
570
571  // In ARM, it's that, plus the low bit of 'adj' must be zero.
572  if (IsARM) {
573    llvm::Constant *One = llvm::ConstantInt::get(Ptr->getType(), 1);
574    llvm::Value *Adj = Builder.CreateExtractValue(MemPtr, 1, "memptr.adj");
575    llvm::Value *VirtualBit = Builder.CreateAnd(Adj, One, "memptr.virtualbit");
576    llvm::Value *IsNotVirtual = Builder.CreateICmpEQ(VirtualBit, Zero,
577                                                     "memptr.notvirtual");
578    Result = Builder.CreateAnd(Result, IsNotVirtual);
579  }
580
581  return Result;
582}
583
584/// The Itanium ABI requires non-zero initialization only for data
585/// member pointers, for which '0' is a valid offset.
586bool ItaniumCXXABI::isZeroInitializable(const MemberPointerType *MPT) {
587  return MPT->getPointeeType()->isFunctionType();
588}
589