1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -triple i686-pc-linux-gnu %s -o - -emit-llvm -verify | FileCheck %s
2eaea8c4c23b6892c82640c6f67034d9ed4498d45Eli Friedman
36ec278d1a354517e20f13a877481453ee7940c78John McCalltypedef __typeof(sizeof(int)) size_t;
4eaea8c4c23b6892c82640c6f67034d9ed4498d45Eli Friedman
56ec278d1a354517e20f13a877481453ee7940c78John McCallnamespace test1 {
66ec278d1a354517e20f13a877481453ee7940c78John McCall  struct A { void operator delete(void*,size_t); int x; };
76ec278d1a354517e20f13a877481453ee7940c78John McCall
86ec278d1a354517e20f13a877481453ee7940c78John McCall  // CHECK: define void @_ZN5test11aEPNS_1AE(
96ec278d1a354517e20f13a877481453ee7940c78John McCall  void a(A *x) {
106ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK:      load
116ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: icmp eq {{.*}}, null
126ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: br i1
136ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK:      call void @_ZN5test11AdlEPvj(i8* %{{.*}}, i32 4)
146ec278d1a354517e20f13a877481453ee7940c78John McCall    delete x;
156ec278d1a354517e20f13a877481453ee7940c78John McCall  }
166ec278d1a354517e20f13a877481453ee7940c78John McCall}
176ec278d1a354517e20f13a877481453ee7940c78John McCall
186ec278d1a354517e20f13a877481453ee7940c78John McCall// Check that we make cookies for the two-arg delete even when using
196ec278d1a354517e20f13a877481453ee7940c78John McCall// the global allocator and deallocator.
206ec278d1a354517e20f13a877481453ee7940c78John McCallnamespace test2 {
216ec278d1a354517e20f13a877481453ee7940c78John McCall  struct A {
226ec278d1a354517e20f13a877481453ee7940c78John McCall    int x;
236ec278d1a354517e20f13a877481453ee7940c78John McCall    void *operator new[](size_t);
246ec278d1a354517e20f13a877481453ee7940c78John McCall    void operator delete[](void *, size_t);
256ec278d1a354517e20f13a877481453ee7940c78John McCall  };
266ec278d1a354517e20f13a877481453ee7940c78John McCall
276ec278d1a354517e20f13a877481453ee7940c78John McCall  // CHECK: define [[A:%.*]]* @_ZN5test24testEv()
286ec278d1a354517e20f13a877481453ee7940c78John McCall  A *test() {
296ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK:      [[NEW:%.*]] = call noalias i8* @_Znaj(i32 44)
306ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T0:%.*]] = bitcast i8* [[NEW]] to i32*
316ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: store i32 10, i32* [[T0]]
326ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T1:%.*]] = getelementptr inbounds i8* [[NEW]], i64 4
336ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T2:%.*]] = bitcast i8* [[T1]] to [[A]]*
346ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: ret [[A]]* [[T2]]
356ec278d1a354517e20f13a877481453ee7940c78John McCall    return ::new A[10];
366ec278d1a354517e20f13a877481453ee7940c78John McCall  }
376ec278d1a354517e20f13a877481453ee7940c78John McCall
386ec278d1a354517e20f13a877481453ee7940c78John McCall  // CHECK: define void @_ZN5test24testEPNS_1AE(
396ec278d1a354517e20f13a877481453ee7940c78John McCall  void test(A *p) {
406ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK:      [[P:%.*]] = alloca [[A]]*, align 4
416ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: store [[A]]* {{%.*}}, [[A]]** [[P]], align 4
426ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T0:%.*]] = load [[A]]** [[P]], align 4
436ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T1:%.*]] = icmp eq [[A]]* [[T0]], null
446ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: br i1 [[T1]],
456ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK:      [[T2:%.*]] = bitcast [[A]]* [[T0]] to i8*
466ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T3:%.*]] = getelementptr inbounds i8* [[T2]], i64 -4
476ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T4:%.*]] = bitcast i8* [[T3]] to i32*
486ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: [[T5:%.*]] = load i32* [[T4]]
496ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: call void @_ZdaPv(i8* [[T3]])
506ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: br label
516ec278d1a354517e20f13a877481453ee7940c78John McCall    ::delete[] p;
526ec278d1a354517e20f13a877481453ee7940c78John McCall  }
536ec278d1a354517e20f13a877481453ee7940c78John McCall}
546ec278d1a354517e20f13a877481453ee7940c78John McCall
556ec278d1a354517e20f13a877481453ee7940c78John McCall// rdar://problem/8913519
566ec278d1a354517e20f13a877481453ee7940c78John McCallnamespace test3 {
576ec278d1a354517e20f13a877481453ee7940c78John McCall  struct A {
586ec278d1a354517e20f13a877481453ee7940c78John McCall    int x;
596ec278d1a354517e20f13a877481453ee7940c78John McCall    void operator delete[](void *, size_t);
606ec278d1a354517e20f13a877481453ee7940c78John McCall  };
616ec278d1a354517e20f13a877481453ee7940c78John McCall  struct B : A {};
626ec278d1a354517e20f13a877481453ee7940c78John McCall
636ec278d1a354517e20f13a877481453ee7940c78John McCall  // CHECK: define void @_ZN5test34testEv()
646ec278d1a354517e20f13a877481453ee7940c78John McCall  void test() {
656ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK:      call noalias i8* @_Znaj(i32 24)
666ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: bitcast
676ec278d1a354517e20f13a877481453ee7940c78John McCall    // CHECK-NEXT: store i32 5
686ec278d1a354517e20f13a877481453ee7940c78John McCall    (void) new B[5];
696ec278d1a354517e20f13a877481453ee7940c78John McCall  }
706ec278d1a354517e20f13a877481453ee7940c78John McCall}
71