2 * Copyright 2011 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include "codegen/nv50_ir_util.h"
29 for (Item
*next
, *item
= head
.next
; item
!= &head
; item
= next
) {
33 head
.next
= head
.prev
= &head
;
37 DLList::Iterator::erase()
49 void DLList::Iterator::moveToList(DLList
& dest
)
53 assert(term
!= &dest
.head
);
59 DLLIST_ADDHEAD(&dest
.head
, item
);
63 DLList::Iterator::insert(void *data
)
65 Item
*ins
= new Item(data
);
67 ins
->next
= pos
->next
;
69 pos
->next
->prev
= ins
;
79 Stack::moveTo(Stack
& that
)
81 unsigned int newSize
= this->size
+ that
.size
;
83 while (newSize
> that
.limit
)
85 memcpy(&that
.array
[that
.size
], &array
[0], this->size
* sizeof(Item
));
91 Interval::Interval(const Interval
& that
) : head(NULL
), tail(NULL
)
104 for (Range
*next
, *r
= head
; r
; r
= next
) {
112 Interval::extend(int a
, int b
)
114 Range
*r
, **nextp
= &head
;
116 // NOTE: we need empty intervals for fixed registers
121 for (r
= head
; r
; r
= r
->next
) {
123 break; // insert before
148 (*nextp
) = new Range(a
, b
);
151 for (r
= (*nextp
); r
->next
; r
= r
->next
);
156 bool Interval::contains(int pos
) const
158 for (Range
*r
= head
; r
&& r
->bgn
<= pos
; r
= r
->next
)
164 bool Interval::overlaps(const Interval
&that
) const
167 Range
*a
= this->head
;
168 Range
*b
= that
.head
;
171 if (b
->bgn
< a
->end
&&
174 if (a
->end
<= b
->bgn
)
180 for (Range
*rA
= this->head
; rA
; rA
= rA
->next
)
181 for (Range
*rB
= iv
.head
; rB
; rB
= rB
->next
)
182 if (rB
->bgn
< rA
->end
&&
189 void Interval::insert(const Interval
&that
)
191 for (Range
*r
= that
.head
; r
; r
= r
->next
)
192 this->extend(r
->bgn
, r
->end
);
195 void Interval::unify(Interval
&that
)
197 assert(this != &that
);
198 for (Range
*next
, *r
= that
.head
; r
; r
= next
) {
200 this->extend(r
->bgn
, r
->end
);
206 int Interval::length() const
209 for (Range
*r
= head
; r
; r
= r
->next
)
210 len
+= r
->bgn
- r
->end
;
214 void Interval::print() const
218 INFO("[%i %i)", head
->bgn
, head
->end
);
219 for (const Range
*r
= head
->next
; r
; r
= r
->next
)
220 INFO(" [%i %i)", r
->bgn
, r
->end
);
225 BitSet::andNot(const BitSet
&set
)
227 assert(data
&& set
.data
);
228 assert(size
>= set
.size
);
229 for (unsigned int i
= 0; i
< (set
.size
+ 31) / 32; ++i
)
230 data
[i
] &= ~set
.data
[i
];
233 BitSet
& BitSet::operator|=(const BitSet
&set
)
235 assert(data
&& set
.data
);
236 assert(size
>= set
.size
);
237 for (unsigned int i
= 0; i
< (set
.size
+ 31) / 32; ++i
)
238 data
[i
] |= set
.data
[i
];
242 bool BitSet::resize(unsigned int nBits
)
245 return allocate(nBits
, true);
246 const unsigned int p
= (size
+ 31) / 32;
247 const unsigned int n
= (nBits
+ 31) / 32;
251 data
= (uint32_t *)REALLOC(data
, 4 * p
, 4 * n
);
257 memset(&data
[p
], 0, (n
- p
) * 4);
258 if (nBits
< size
&& (nBits
% 32))
259 data
[(nBits
+ 31) / 32 - 1] &= (1 << (nBits
% 32)) - 1;
265 bool BitSet::allocate(unsigned int nBits
, bool zero
)
267 if (data
&& size
< nBits
) {
274 data
= reinterpret_cast<uint32_t *>(CALLOC((size
+ 31) / 32, 4));
277 memset(data
, 0, (size
+ 7) / 8);
279 if (size
% 32) // clear unused bits (e.g. for popCount)
280 data
[(size
+ 31) / 32 - 1] &= (1 << (size
% 32)) - 1;
285 unsigned int BitSet::popCount() const
287 unsigned int count
= 0;
289 for (unsigned int i
= 0; i
< (size
+ 31) / 32; ++i
)
291 count
+= util_bitcount(data
[i
]);
295 void BitSet::fill(uint32_t val
)
298 for (i
= 0; i
< (size
+ 31) / 32; ++i
)
301 data
[i
] &= ~(0xffffffff << (size
% 32)); // BE ?
304 void BitSet::setOr(BitSet
*pA
, BitSet
*pB
)
309 for (unsigned int i
= 0; i
< (size
+ 31) / 32; ++i
)
310 data
[i
] = pA
->data
[i
] | pB
->data
[i
];
314 int BitSet::findFreeRange(unsigned int count
) const
316 const uint32_t m
= (1 << count
) - 1;
319 const unsigned int end
= (size
+ 31) / 32;
322 for (i
= 0; i
< end
; ++i
) {
323 pos
= ffs(~data
[i
]) - 1;
329 for (i
= 0; i
< end
; ++i
) {
330 if (data
[i
] != 0xffffffff) {
331 uint32_t b
= data
[i
] | (data
[i
] >> 1) | 0xaaaaaaaa;
338 if (count
== 4 || count
== 3) {
339 for (i
= 0; i
< end
; ++i
) {
340 if (data
[i
] != 0xffffffff) {
342 (data
[i
] >> 0) | (data
[i
] >> 1) |
343 (data
[i
] >> 2) | (data
[i
] >> 3) | 0xeeeeeeee;
358 for (i
= 0; i
< end
; ++i
) {
359 if (data
[i
] != 0xffffffff) {
360 for (pos
= 0; pos
< 32; pos
+= count
)
361 if (!(data
[i
] & (m
<< pos
)))
370 return ((pos
+ count
) <= size
) ? pos
: -1;
373 void BitSet::print() const
376 INFO("BitSet of size %u:\n", size
);
377 for (unsigned int i
= 0; i
< (size
+ 31) / 32; ++i
) {
378 uint32_t bits
= data
[i
];
380 int pos
= ffs(bits
) - 1;
382 INFO(" %i", i
* 32 + pos
);
392 } // namespace nv50_ir