1 // { dg-do compile }
2 // { dg-options "-O2 -fdump-tree-fre3 -fdump-tree-optimized -fdelete-null-pointer-checks" }
3 
4 #define assume(x) if(!(x))__builtin_unreachable()
5 
new(__SIZE_TYPE__ n)6 inline void* operator new(__SIZE_TYPE__ n){ return __builtin_malloc(n); }
delete(void * p)7 inline void operator delete(void *p) { __builtin_free(p); }
8 // C++14 sized deallocation function
delete(void * p,__SIZE_TYPE__)9 inline void operator delete(void *p, __SIZE_TYPE__) { __builtin_free(p); }
10 struct O {
11     double num;
12     int count;
13 };
14 struct I {
15     O *o;
oI16     I(double d = 0) : o (new O) { o->num = d; o->count = 1; }
II17     I(I const&i) { assume(i.o->count >= 1); o = i.o; ++o->count; }
18     I& operator=(I const&i) { I(i).swap(*this); return *this; }
~II19     ~I() { if (--o->count == 0) delete o; }
swapI20     void swap(I& i) { O *tmp = o; o = i.o; i.o = tmp; }
21     I& operator*= (I const&i) {
22 	if (o->count > 1) *this = I(o->num);
23 	o->num *= i.o->num;
24 	return *this;
25     }
26     I& operator-= (I const&i) {
27 	if (o->count > 1) *this = I(o->num);
28 	o->num -= i.o->num;
29 	return *this;
30     }
31 };
32 inline I operator* (I a, I const&b) { return a *= b; }
33 inline I operator- (I a, I const&b) { return a -= b; }
34 inline bool operator< (I const&a, I const&b) { return a.o->num < b.o->num; }
35 
f(I a,I b,I c,I d)36 bool f(I a, I b, I c, I d) {
37     I tmp = (a * d - b * c) * (a * b - c * d);
38     return tmp < 42;
39 }
40 
41 // We should be able to CSE most references to count and thus remove
42 // a bunch of conditional free()s and unreachable()s.
43 // This works only if everything is inlined into 'f'.
44 
45 // { dg-final { scan-tree-dump-times ";; Function" 1 "fre3" } }
46 // { dg-final { scan-tree-dump-times "unreachable" 11 "fre3" } }
47 
48 // Note that depending on PUSH_ARGS_REVERSED we are presented with
49 // a different initial CFG and thus the final outcome is different
50 
51 // { dg-final { scan-tree-dump-times "free" 10 "fre3" { target x86_64-*-* i?86-*-* } } }
52 // { dg-final { scan-tree-dump-times "free" 14 "fre3" { target aarch64-*-* ia64-*-* arm-*-* hppa*-*-* sparc*-*-* powerpc*-*-* alpha*-*-* } } }
53 // { dg-final { scan-tree-dump-times "free" 0 "optimized" } }
54