aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.8/gcc/testsuite/g++.dg/opt/unroll1.C
blob: fd07f889a1d1c548ba1943ea30338088a2ccde3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// PR optimization/12340
// Origin: Richard Guenther <richard.guenther@uni-tuebingen.de>
// Testcase by Eric Botcazou <ebotcazou@libertysurf.fr>

// This used to segfault on x86 because the loop optimizer wrongly
// interpreted a double assignment to a biv as a double increment,
// which subsequently fooled the unroller.

// { dg-do run }
// { dg-options "-O2 -fno-exceptions -funroll-loops" }

typedef __SIZE_TYPE__ size_t;

inline void* operator new(size_t, void* __p) throw() { return __p; }
inline void operator delete (void*, void*) throw() { };

class Loc;
class Interval;

template<class DT>
class DomainBase
{
public:
  typedef typename DT::Domain_t Domain_t;
  typedef typename DT::Storage_t Storage_t;

  Domain_t &unwrap() { return *static_cast<Domain_t *>(this); }

  const Domain_t &unwrap() const {
    return *static_cast<Domain_t *>(const_cast<DomainBase<DT> *>(this));
  }

protected:
  Storage_t domain_m;
};

template<class DT>
class Domain : public DomainBase<DT>
{
  typedef DomainBase<DT> Base_t;

public:
  typedef typename DT::Size_t Size_t;
  typedef typename DT::Element_t Element_t;
  typedef typename Base_t::Domain_t Domain_t;
  typedef typename Base_t::Storage_t Storage_t;

  Domain_t &operator[](int) { return this->unwrap(); }

  const Domain_t &operator[](int) const { return this->unwrap(); }

  template<class T>
  void setDomain(const T &newdom) {
    DT::setDomain(this->domain_m, newdom);
  }

  Element_t first() const { return DT::first(this->domain_m); }

  Size_t length() const { return DT::length(this->domain_m); }

  Size_t size() const { return length(); }
};

template<class T>
struct DomainTraits;

template<>
struct DomainTraits<Interval>
{
  typedef int Size_t;
  typedef int Element_t;
  typedef Interval Domain_t;
  typedef Interval OneDomain_t;
  typedef Loc AskDomain_t;
  typedef int Storage_t[2];
  enum { dimensions = 1 };
  enum { wildcard = false };

  static int first(const Storage_t &d) { return d[0]; }

  static int length(const Storage_t &d) { return d[1]; }

  static OneDomain_t &getDomain(Domain_t &d, int) { return d; }

  static const OneDomain_t &getDomain(const Domain_t &d, int) { return d; }

  template<class T>
  static void setDomain(Storage_t &dom, const T &newdom) {
    dom[0] = newdom.first();  
    dom[1] = newdom.length();
  }

  template<class T1, class T2>
  static void setDomain(Storage_t &dom, const T1 &begval, const T2 &endval) {
    dom[0] = begval;
    dom[1] = (endval - begval + 1);
  }

};

class Interval : public Domain<DomainTraits<Interval> >
{
public:
  Interval(const Interval &a) : Domain<DomainTraits<Interval> >() {    
    for (int i=0; i < DomainTraits<Interval>::dimensions; ++i)
      DomainTraits<Interval>::getDomain(*this, i).setDomain(
                                DomainTraits<Interval>::getDomain(a, i));
  }

  Interval(int a) : Domain<DomainTraits<Interval> >()
  {
    DomainTraits<Interval>::setDomain(domain_m, 0, a - 1);
  }
};

template<>
struct DomainTraits<Loc>
{
  typedef int Size_t;
  typedef int Element_t;
  typedef Loc Domain_t;
  typedef Loc AskDomain_t;
  typedef Loc MultResult_t;
  typedef int Storage_t;

  static int first(int d) { return d; }

  template<class T>
  static void setDomain(int &dom, const T &newdom) {
    dom = DomainTraits<T>::getFirst(newdom);
  }
};

template<>
struct DomainTraits<int>
 {
  enum { dimensions = 1 };
  enum { wildcard = false };

  static int getPointDomain(int d, int) { return d; }

  static int getFirst(const int &d) { return d; }
};

class Loc : public Domain<DomainTraits<Loc> >
{
public:
  explicit Loc(const int &a) : Domain<DomainTraits<Loc> >() {
    for (int i=0; i < 1; ++i)
      (*this)[i].setDomain(DomainTraits<int>::getPointDomain(a, 0));
  }
};

struct ElementProperties
{
  enum { hasTrivialDefaultConstructor = false };
  enum { hasTrivialDestructor = false };

  static void construct(double* addr)
  {
    new (addr) double();
  }

  static void construct(double* addr, const double& model)
  {
    new (addr) double(model);
  }

  static void destruct(double *addr) {}
};

class RefCounted
{
public:
  RefCounted() : count_m(0) {}

  void addReference() { ++count_m; }
  bool removeRefAndCheckGarbage()
  {
    return (--count_m == 0);
  }

private:
  int count_m;
};

class RefBlockController : public RefCounted
{
public:
  explicit RefBlockController(unsigned int size)
    : pBegin_m(0), pEnd_m(0), pEndOfStorage_m(0), dealloc_m(false)
  {
    reallocateStorage(size, false);

    if (!ElementProperties::hasTrivialDefaultConstructor)
      {
        for (double * pt = begin(); pt != end(); ++pt)
          ElementProperties::construct(pt);
      }
  }
  
  ~RefBlockController()
  {
    deleteStorage();
  }

  double *begin() const
  {
    return pBegin_m;
  }

  double *end() const
  {
    return pEnd_m;
  }

  bool isMine() const
  {
    return dealloc_m;
  }

private:
  void deleteStorage()
  {
    if (isMine() && pBegin_m != 0)
      {
        if (!ElementProperties::hasTrivialDestructor)
          for (double *pt = begin(); pt != end(); ++pt)
            ElementProperties::destruct(pt);

        char *tmp = reinterpret_cast<char *>(pBegin_m);
        delete [] tmp;
      }
  }

  void reallocateStorage(unsigned int newsize, bool copyold = false)
  {
    double *pBeginNew = 0;
    double *pEndNew = 0;
    double *pEndOfStorageNew = 0;

    if (newsize > 0)
      {
        int nsize = newsize * sizeof(double);
        char *tmp = new char[nsize];
        pBeginNew = reinterpret_cast<double *>(tmp);
        pEndNew = pBeginNew + newsize;
        pEndOfStorageNew = pBeginNew + (nsize / sizeof(double));

        if (copyold)
          {
            double * pOld = begin();
            double * pNew = pBeginNew;
            while (pOld != end() && pNew != pEndNew)
              ElementProperties::construct(pNew++,*pOld++);
          }
      }

    deleteStorage();

    pBegin_m = pBeginNew;
    pEnd_m = pEndNew;
    pEndOfStorage_m = pEndOfStorageNew;
    dealloc_m = true;
  }

  double *pBegin_m;
  double *pEnd_m;
  double *pEndOfStorage_m;
  bool dealloc_m;
};

class DataBlockController : public RefBlockController
{
public:
  explicit
  DataBlockController(unsigned int size)
    : RefBlockController(size), dataObjectPtr_m(new char), owned_m(true) {}

  ~DataBlockController()
  {
    if (owned_m) delete dataObjectPtr_m;
  }

private:
  mutable char *dataObjectPtr_m;
  bool owned_m;
};

class RefCountedPtr
{
public:
  RefCountedPtr(DataBlockController * const pT) : ptr_m(pT)
    { if (isValid()) ptr_m->addReference(); }

  ~RefCountedPtr() { invalidate(); }

  DataBlockController* operator->() const { return ptr_m; }
  void invalidate();
  bool isValid() const { return ptr_m != 0; }

private:
  friend class RefCountedBlockPtr;
  DataBlockController * ptr_m;
};

inline void RefCountedPtr::invalidate()
{
  if ( isValid() && ptr_m->removeRefAndCheckGarbage() )
    delete ptr_m;
  ptr_m = 0;
}

class RefCountedBlockPtr
{
public:
  explicit RefCountedBlockPtr(unsigned int size)
    : offset_m(0),
      blockControllerPtr_m(new DataBlockController(size)) {}

  int offset() const
  {
    return offset_m;
  }

  double *beginPointer() const
  {
    return blockControllerPtr_m->begin();
  }

  double *currentPointer() const
  {
    return beginPointer() + offset();
  }

protected:
  int offset_m;
  RefCountedPtr blockControllerPtr_m;
};

class DataBlockPtr : public RefCountedBlockPtr
{
public:
  explicit DataBlockPtr(unsigned int size) : RefCountedBlockPtr(size) {}
};

class Node
{
public:
  Node(const Interval &owned, const Interval &allocated)
    : domain_m(owned), allocated_m(allocated) {}

  const Interval &allocated() const { return allocated_m; }

private:
  Interval domain_m;
  Interval allocated_m;
};

class DomainLayout
{
public:
  explicit DomainLayout(const Interval &dom) : node_m(0, dom) {}

  const Interval &domain() const
  {
    return node_m.allocated();
  }

private:
  Node node_m;
};

class BrickBase
{
public:
  explicit BrickBase(const Interval &domain);

  int offset(const Loc &dom) const { return off_m + dom[0].first(); }

protected:
  DomainLayout layout_m;
  int firsts_m;
  int off_m;
};

BrickBase::BrickBase(const Interval &dom)
  : layout_m(dom)
{
  firsts_m = layout_m.domain()[0].first();
  off_m = -firsts_m;
}

class Engine : public BrickBase
{
public:
  explicit Engine(const Interval &dom)
  : BrickBase(dom), dataBlock_m(dom.size()), data_m(dataBlock_m.currentPointer()) {}

  double& operator()(const Loc &loc) const
  {
    return data_m[this->offset(loc)];
  }

private:
  DataBlockPtr dataBlock_m;
  double *data_m;
};


int main()
{
  Interval I(10);
  Engine A(I);

  for (int i = 0; i < 10; i++)
    A(Loc(i)) = 2.0 + i - i*i;

  return 0;
}