blob: 1d1996d6b1ccb07a2c4de67a6cccb8261742c725 [file] [log] [blame]
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001//===--- BitTracker.cpp ---------------------------------------------------===//
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// SSA-based bit propagation.
11//
12// The purpose of this code is, for a given virtual register, to provide
13// information about the value of each bit in the register. The values
14// of bits are represented by the class BitValue, and take one of four
15// cases: 0, 1, "ref" and "bottom". The 0 and 1 are rather clear, the
16// "ref" value means that the bit is a copy of another bit (which itself
17// cannot be a copy of yet another bit---such chains are not allowed).
18// A "ref" value is associated with a BitRef structure, which indicates
19// which virtual register, and which bit in that register is the origin
20// of the value. For example, given an instruction
21// vreg2 = ASL vreg1, 1
22// assuming that nothing is known about bits of vreg1, bit 1 of vreg2
23// will be a "ref" to (vreg1, 0). If there is a subsequent instruction
24// vreg3 = ASL vreg2, 2
25// then bit 3 of vreg3 will be a "ref" to (vreg1, 0) as well.
26// The "bottom" case means that the bit's value cannot be determined,
27// and that this virtual register actually defines it. The "bottom" case
28// is discussed in detail in BitTracker.h. In fact, "bottom" is a "ref
29// to self", so for the vreg1 above, the bit 0 of it will be a "ref" to
30// (vreg1, 0), bit 1 will be a "ref" to (vreg1, 1), etc.
31//
32// The tracker implements the Wegman-Zadeck algorithm, originally developed
33// for SSA-based constant propagation. Each register is represented as
34// a sequence of bits, with the convention that bit 0 is the least signi-
35// ficant bit. Each bit is propagated individually. The class RegisterCell
36// implements the register's representation, and is also the subject of
37// the lattice operations in the tracker.
38//
39// The intended usage of the bit tracker is to create a target-specific
40// machine instruction evaluator, pass the evaluator to the BitTracker
41// object, and run the tracker. The tracker will then collect the bit
42// value information for a given machine function. After that, it can be
43// queried for the cells for each virtual register.
44// Sample code:
45// const TargetSpecificEvaluator TSE(TRI, MRI);
46// BitTracker BT(TSE, MF);
47// BT.run();
48// ...
49// unsigned Reg = interestingRegister();
50// RegisterCell RC = BT.get(Reg);
51// if (RC[3].is(1))
52// Reg0bit3 = 1;
53//
54// The code below is intended to be fully target-independent.
55
56#include "llvm/CodeGen/MachineBasicBlock.h"
57#include "llvm/CodeGen/MachineFunction.h"
58#include "llvm/CodeGen/MachineInstr.h"
59#include "llvm/CodeGen/MachineRegisterInfo.h"
60#include "llvm/IR/Constants.h"
61#include "llvm/Support/Debug.h"
62#include "llvm/Support/raw_ostream.h"
63#include "llvm/Target/TargetRegisterInfo.h"
64
65#include "BitTracker.h"
66
67using namespace llvm;
68
69typedef BitTracker BT;
70
71namespace {
72 // Local trickery to pretty print a register (without the whole "%vreg"
73 // business).
74 struct printv {
75 printv(unsigned r) : R(r) {}
76 unsigned R;
77 };
78 raw_ostream &operator<< (raw_ostream &OS, const printv &PV) {
79 if (PV.R)
80 OS << 'v' << TargetRegisterInfo::virtReg2Index(PV.R);
81 else
82 OS << 's';
83 return OS;
84 }
85}
86
87
88raw_ostream &operator<< (raw_ostream &OS, const BT::BitValue &BV) {
89 switch (BV.Type) {
90 case BT::BitValue::Top:
91 OS << 'T';
92 break;
93 case BT::BitValue::Zero:
94 OS << '0';
95 break;
96 case BT::BitValue::One:
97 OS << '1';
98 break;
99 case BT::BitValue::Ref:
100 OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']';
101 break;
102 }
103 return OS;
104}
105
106
107raw_ostream &operator<< (raw_ostream &OS, const BT::RegisterCell &RC) {
108 unsigned n = RC.Bits.size();
109 OS << "{ w:" << n;
110 // Instead of printing each bit value individually, try to group them
111 // into logical segments, such as sequences of 0 or 1 bits or references
112 // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz").
113 // "Start" will be the index of the beginning of the most recent segment.
114 unsigned Start = 0;
115 bool SeqRef = false; // A sequence of refs to consecutive bits.
116 bool ConstRef = false; // A sequence of refs to the same bit.
117
118 for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) {
119 const BT::BitValue &V = RC[i];
120 const BT::BitValue &SV = RC[Start];
121 bool IsRef = (V.Type == BT::BitValue::Ref);
122 // If the current value is the same as Start, skip to the next one.
123 if (!IsRef && V == SV)
124 continue;
125 if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) {
126 if (Start+1 == i) {
127 SeqRef = (V.RefI.Pos == SV.RefI.Pos+1);
128 ConstRef = (V.RefI.Pos == SV.RefI.Pos);
129 }
130 if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start))
131 continue;
132 if (ConstRef && V.RefI.Pos == SV.RefI.Pos)
133 continue;
134 }
135
136 // The current value is different. Print the previous one and reset
137 // the Start.
138 OS << " [" << Start;
139 unsigned Count = i - Start;
140 if (Count == 1) {
141 OS << "]:" << SV;
142 } else {
143 OS << '-' << i-1 << "]:";
144 if (SV.Type == BT::BitValue::Ref && SeqRef)
145 OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
146 << SV.RefI.Pos+(Count-1) << ']';
147 else
148 OS << SV;
149 }
150 Start = i;
151 SeqRef = ConstRef = false;
152 }
153
154 OS << " [" << Start;
155 unsigned Count = n - Start;
156 if (n-Start == 1) {
157 OS << "]:" << RC[Start];
158 } else {
159 OS << '-' << n-1 << "]:";
160 const BT::BitValue &SV = RC[Start];
161 if (SV.Type == BT::BitValue::Ref && SeqRef)
162 OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
163 << SV.RefI.Pos+(Count-1) << ']';
164 else
165 OS << SV;
166 }
167 OS << " }";
168
169 return OS;
170}
171
172
173BitTracker::BitTracker(const MachineEvaluator &E, llvm::MachineFunction &F) :
174 Trace(false), ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType) {
175}
176
177
178BitTracker::~BitTracker() {
179 delete &Map;
180}
181
182
183// If we were allowed to update a cell for a part of a register, the meet
184// operation would need to be parametrized by the register number and the
185// exact part of the register, so that the computer BitRefs correspond to
186// the actual bits of the "self" register.
187// While this cannot happen in the current implementation, I'm not sure
188// if this should be ruled out in the future.
189bool BT::RegisterCell::meet(const RegisterCell &RC, unsigned SelfR) {
190 // An example when "meet" can be invoked with SelfR == 0 is a phi node
191 // with a physical register as an operand.
192 assert(SelfR == 0 || TargetRegisterInfo::isVirtualRegister(SelfR));
193 bool Changed = false;
194 for (uint16_t i = 0, n = Bits.size(); i < n; ++i) {
195 const BitValue &RCV = RC[i];
196 Changed |= Bits[i].meet(RCV, BitRef(SelfR, i));
197 }
198 return Changed;
199}
200
201
202// Insert the entire cell RC into the current cell at position given by M.
203BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC,
204 const BitMask &M) {
205 uint16_t B = M.first(), E = M.last(), W = width();
206 // Sanity: M must be a valid mask for *this.
207 assert(B < W && E < W);
208 // Sanity: the masked part of *this must have the same number of bits
209 // as the source.
210 assert(B > E || E-B+1 == RC.width()); // B <= E => E-B+1 = |RC|.
211 assert(B <= E || E+(W-B)+1 == RC.width()); // E < B => E+(W-B)+1 = |RC|.
212 if (B <= E) {
213 for (uint16_t i = 0; i <= E-B; ++i)
214 Bits[i+B] = RC[i];
215 } else {
216 for (uint16_t i = 0; i < W-B; ++i)
217 Bits[i+B] = RC[i];
218 for (uint16_t i = 0; i <= E; ++i)
219 Bits[i] = RC[i+(W-B)];
220 }
221 return *this;
222}
223
224
225BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const {
226 uint16_t B = M.first(), E = M.last(), W = width();
227 assert(B < W && E < W);
228 if (B <= E) {
229 RegisterCell RC(E-B+1);
230 for (uint16_t i = B; i <= E; ++i)
231 RC.Bits[i-B] = Bits[i];
232 return RC;
233 }
234
235 RegisterCell RC(E+(W-B)+1);
236 for (uint16_t i = 0; i < W-B; ++i)
237 RC.Bits[i] = Bits[i+B];
238 for (uint16_t i = 0; i <= E; ++i)
239 RC.Bits[i+(W-B)] = Bits[i];
240 return RC;
241}
242
243
244BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) {
245 // Rotate left (i.e. towards increasing bit indices).
246 // Swap the two parts: [0..W-Sh-1] [W-Sh..W-1]
247 uint16_t W = width();
248 Sh = Sh % W;
249 if (Sh == 0)
250 return *this;
251
252 RegisterCell Tmp(W-Sh);
253 // Tmp = [0..W-Sh-1].
254 for (uint16_t i = 0; i < W-Sh; ++i)
255 Tmp[i] = Bits[i];
256 // Shift [W-Sh..W-1] to [0..Sh-1].
257 for (uint16_t i = 0; i < Sh; ++i)
258 Bits[i] = Bits[W-Sh+i];
259 // Copy Tmp to [Sh..W-1].
260 for (uint16_t i = 0; i < W-Sh; ++i)
261 Bits[i+Sh] = Tmp.Bits[i];
262 return *this;
263}
264
265
266BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E,
267 const BitValue &V) {
268 assert(B <= E);
269 while (B < E)
270 Bits[B++] = V;
271 return *this;
272}
273
274
275BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) {
276 // Append the cell given as the argument to the "this" cell.
277 // Bit 0 of RC becomes bit W of the result, where W is this->width().
278 uint16_t W = width(), WRC = RC.width();
279 Bits.resize(W+WRC);
280 for (uint16_t i = 0; i < WRC; ++i)
281 Bits[i+W] = RC.Bits[i];
282 return *this;
283}
284
285
286uint16_t BT::RegisterCell::ct(bool B) const {
287 uint16_t W = width();
288 uint16_t C = 0;
289 BitValue V = B;
290 while (C < W && Bits[C] == V)
291 C++;
292 return C;
293}
294
295
296uint16_t BT::RegisterCell::cl(bool B) const {
297 uint16_t W = width();
298 uint16_t C = 0;
299 BitValue V = B;
300 while (C < W && Bits[W-(C+1)] == V)
301 C++;
302 return C;
303}
304
305
306bool BT::RegisterCell::operator== (const RegisterCell &RC) const {
307 uint16_t W = Bits.size();
308 if (RC.Bits.size() != W)
309 return false;
310 for (uint16_t i = 0; i < W; ++i)
311 if (Bits[i] != RC[i])
312 return false;
313 return true;
314}
315
316
317uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const {
318 // The general problem is with finding a register class that corresponds
319 // to a given reference reg:sub. There can be several such classes, and
320 // since we only care about the register size, it does not matter which
321 // such class we would find.
322 // The easiest way to accomplish what we want is to
323 // 1. find a physical register PhysR from the same class as RR.Reg,
324 // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub,
325 // 3. find a register class that contains PhysS.
326 unsigned PhysR;
327 if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
328 const TargetRegisterClass *VC = MRI.getRegClass(RR.Reg);
329 assert(VC->begin() != VC->end() && "Empty register class");
330 PhysR = *VC->begin();
331 } else {
332 assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
333 PhysR = RR.Reg;
334 }
335
336 unsigned PhysS = (RR.Sub == 0) ? PhysR : TRI.getSubReg(PhysR, RR.Sub);
337 const TargetRegisterClass *RC = TRI.getMinimalPhysRegClass(PhysS);
338 uint16_t BW = RC->getSize()*8;
339 return BW;
340}
341
342
343BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR,
344 const CellMapType &M) const {
345 uint16_t BW = getRegBitWidth(RR);
346
347 // Physical registers are assumed to be present in the map with an unknown
348 // value. Don't actually insert anything in the map, just return the cell.
349 if (TargetRegisterInfo::isPhysicalRegister(RR.Reg))
350 return RegisterCell::self(0, BW);
351
352 assert(TargetRegisterInfo::isVirtualRegister(RR.Reg));
353 // For virtual registers that belong to a class that is not tracked,
354 // generate an "unknown" value as well.
355 const TargetRegisterClass *C = MRI.getRegClass(RR.Reg);
356 if (!track(C))
357 return RegisterCell::self(0, BW);
358
359 CellMapType::const_iterator F = M.find(RR.Reg);
360 if (F != M.end()) {
361 if (!RR.Sub)
362 return F->second;
363 BitMask M = mask(RR.Reg, RR.Sub);
364 return F->second.extract(M);
365 }
366 // If not found, create a "top" entry, but do not insert it in the map.
367 return RegisterCell::top(BW);
368}
369
370
371void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC,
372 CellMapType &M) const {
373 // While updating the cell map can be done in a meaningful way for
374 // a part of a register, it makes little sense to implement it as the
375 // SSA representation would never contain such "partial definitions".
376 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
377 return;
378 assert(RR.Sub == 0 && "Unexpected sub-register in definition");
379 // Eliminate all ref-to-reg-0 bit values: replace them with "self".
380 for (unsigned i = 0, n = RC.width(); i < n; ++i) {
381 const BitValue &V = RC[i];
382 if (V.Type == BitValue::Ref && V.RefI.Reg == 0)
383 RC[i].RefI = BitRef(RR.Reg, i);
384 }
385 M[RR.Reg] = RC;
386}
387
388
389// Check if the cell represents a compile-time integer value.
390bool BT::MachineEvaluator::isInt(const RegisterCell &A) const {
391 uint16_t W = A.width();
392 for (uint16_t i = 0; i < W; ++i)
393 if (!A[i].is(0) && !A[i].is(1))
394 return false;
395 return true;
396}
397
398
399// Convert a cell to the integer value. The result must fit in uint64_t.
400uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const {
401 assert(isInt(A));
402 uint64_t Val = 0;
403 uint16_t W = A.width();
404 for (uint16_t i = 0; i < W; ++i) {
405 Val <<= 1;
406 Val |= A[i].is(1);
407 }
408 return Val;
409}
410
411
412// Evaluator helper functions. These implement some common operation on
413// register cells that can be used to implement target-specific instructions
414// in a target-specific evaluator.
415
416BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const {
417 RegisterCell Res(W);
418 // For bits beyond the 63rd, this will generate the sign bit of V.
419 for (uint16_t i = 0; i < W; ++i) {
420 Res[i] = BitValue(V & 1);
421 V >>= 1;
422 }
423 return Res;
424}
425
426
427BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const {
428 APInt A = CI->getValue();
429 uint16_t BW = A.getBitWidth();
430 assert((unsigned)BW == A.getBitWidth() && "BitWidth overflow");
431 RegisterCell Res(BW);
432 for (uint16_t i = 0; i < BW; ++i)
433 Res[i] = A[i];
434 return Res;
435}
436
437
438BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1,
439 const RegisterCell &A2) const {
440 uint16_t W = A1.width();
441 assert(W == A2.width());
442 RegisterCell Res(W);
443 bool Carry = false;
444 uint16_t I;
445 for (I = 0; I < W; ++I) {
446 const BitValue &V1 = A1[I];
447 const BitValue &V2 = A2[I];
448 if (!V1.num() || !V2.num())
449 break;
450 unsigned S = bool(V1) + bool(V2) + Carry;
451 Res[I] = BitValue(S & 1);
452 Carry = (S > 1);
453 }
454 for (; I < W; ++I) {
455 const BitValue &V1 = A1[I];
456 const BitValue &V2 = A2[I];
457 // If the next bit is same as Carry, the result will be 0 plus the
458 // other bit. The Carry bit will remain unchanged.
459 if (V1.is(Carry))
460 Res[I] = BitValue::ref(V2);
461 else if (V2.is(Carry))
462 Res[I] = BitValue::ref(V1);
463 else
464 break;
465 }
466 for (; I < W; ++I)
467 Res[I] = BitValue::self();
468 return Res;
469}
470
471
472BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1,
473 const RegisterCell &A2) const {
474 uint16_t W = A1.width();
475 assert(W == A2.width());
476 RegisterCell Res(W);
477 bool Borrow = false;
478 uint16_t I;
479 for (I = 0; I < W; ++I) {
480 const BitValue &V1 = A1[I];
481 const BitValue &V2 = A2[I];
482 if (!V1.num() || !V2.num())
483 break;
484 unsigned S = bool(V1) - bool(V2) - Borrow;
485 Res[I] = BitValue(S & 1);
486 Borrow = (S > 1);
487 }
488 for (; I < W; ++I) {
489 const BitValue &V1 = A1[I];
490 const BitValue &V2 = A2[I];
491 if (V1.is(Borrow)) {
492 Res[I] = BitValue::ref(V2);
493 break;
494 }
495 if (V2.is(Borrow))
496 Res[I] = BitValue::ref(V1);
497 else
498 break;
499 }
500 for (; I < W; ++I)
501 Res[I] = BitValue::self();
502 return Res;
503}
504
505
506BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1,
507 const RegisterCell &A2) const {
508 uint16_t W = A1.width() + A2.width();
509 uint16_t Z = A1.ct(0) + A2.ct(0);
510 RegisterCell Res(W);
511 Res.fill(0, Z, BitValue::Zero);
512 Res.fill(Z, W, BitValue::self());
513 return Res;
514}
515
516
517BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1,
518 const RegisterCell &A2) const {
519 uint16_t W = A1.width() + A2.width();
520 uint16_t Z = A1.ct(0) + A2.ct(0);
521 RegisterCell Res(W);
522 Res.fill(0, Z, BitValue::Zero);
523 Res.fill(Z, W, BitValue::self());
524 return Res;
525}
526
527
528BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1,
529 uint16_t Sh) const {
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000530 assert(Sh <= A1.width());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000531 RegisterCell Res = RegisterCell::ref(A1);
532 Res.rol(Sh);
533 Res.fill(0, Sh, BitValue::Zero);
534 return Res;
535}
536
537
538BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1,
539 uint16_t Sh) const {
540 uint16_t W = A1.width();
541 assert(Sh <= W);
542 RegisterCell Res = RegisterCell::ref(A1);
543 Res.rol(W-Sh);
544 Res.fill(W-Sh, W, BitValue::Zero);
545 return Res;
546}
547
548
549BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1,
550 uint16_t Sh) const {
551 uint16_t W = A1.width();
552 assert(Sh <= W);
553 RegisterCell Res = RegisterCell::ref(A1);
554 BitValue Sign = Res[W-1];
555 Res.rol(W-Sh);
556 Res.fill(W-Sh, W, Sign);
557 return Res;
558}
559
560
561BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1,
562 const RegisterCell &A2) const {
563 uint16_t W = A1.width();
564 assert(W == A2.width());
565 RegisterCell Res(W);
566 for (uint16_t i = 0; i < W; ++i) {
567 const BitValue &V1 = A1[i];
568 const BitValue &V2 = A2[i];
569 if (V1.is(1))
570 Res[i] = BitValue::ref(V2);
571 else if (V2.is(1))
572 Res[i] = BitValue::ref(V1);
573 else if (V1.is(0) || V2.is(0))
574 Res[i] = BitValue::Zero;
575 else if (V1 == V2)
576 Res[i] = V1;
577 else
578 Res[i] = BitValue::self();
579 }
580 return Res;
581}
582
583
584BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1,
585 const RegisterCell &A2) const {
586 uint16_t W = A1.width();
587 assert(W == A2.width());
588 RegisterCell Res(W);
589 for (uint16_t i = 0; i < W; ++i) {
590 const BitValue &V1 = A1[i];
591 const BitValue &V2 = A2[i];
592 if (V1.is(1) || V2.is(1))
593 Res[i] = BitValue::One;
594 else if (V1.is(0))
595 Res[i] = BitValue::ref(V2);
596 else if (V2.is(0))
597 Res[i] = BitValue::ref(V1);
598 else if (V1 == V2)
599 Res[i] = V1;
600 else
601 Res[i] = BitValue::self();
602 }
603 return Res;
604}
605
606
607BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1,
608 const RegisterCell &A2) const {
609 uint16_t W = A1.width();
610 assert(W == A2.width());
611 RegisterCell Res(W);
612 for (uint16_t i = 0; i < W; ++i) {
613 const BitValue &V1 = A1[i];
614 const BitValue &V2 = A2[i];
615 if (V1.is(0))
616 Res[i] = BitValue::ref(V2);
617 else if (V2.is(0))
618 Res[i] = BitValue::ref(V1);
619 else if (V1 == V2)
620 Res[i] = BitValue::Zero;
621 else
622 Res[i] = BitValue::self();
623 }
624 return Res;
625}
626
627
628BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const {
629 uint16_t W = A1.width();
630 RegisterCell Res(W);
631 for (uint16_t i = 0; i < W; ++i) {
632 const BitValue &V = A1[i];
633 if (V.is(0))
634 Res[i] = BitValue::One;
635 else if (V.is(1))
636 Res[i] = BitValue::Zero;
637 else
638 Res[i] = BitValue::self();
639 }
640 return Res;
641}
642
643
644BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1,
645 uint16_t BitN) const {
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000646 assert(BitN < A1.width());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000647 RegisterCell Res = RegisterCell::ref(A1);
648 Res[BitN] = BitValue::One;
649 return Res;
650}
651
652
653BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1,
654 uint16_t BitN) const {
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000655 assert(BitN < A1.width());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000656 RegisterCell Res = RegisterCell::ref(A1);
657 Res[BitN] = BitValue::Zero;
658 return Res;
659}
660
661
662BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B,
663 uint16_t W) const {
664 uint16_t C = A1.cl(B), AW = A1.width();
665 // If the last leading non-B bit is not a constant, then we don't know
666 // the real count.
667 if ((C < AW && A1[AW-1-C].num()) || C == AW)
668 return eIMM(C, W);
669 return RegisterCell::self(0, W);
670}
671
672
673BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B,
674 uint16_t W) const {
675 uint16_t C = A1.ct(B), AW = A1.width();
676 // If the last trailing non-B bit is not a constant, then we don't know
677 // the real count.
678 if ((C < AW && A1[C].num()) || C == AW)
679 return eIMM(C, W);
680 return RegisterCell::self(0, W);
681}
682
683
684BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1,
685 uint16_t FromN) const {
686 uint16_t W = A1.width();
687 assert(FromN <= W);
688 RegisterCell Res = RegisterCell::ref(A1);
689 BitValue Sign = Res[FromN-1];
690 // Sign-extend "inreg".
691 Res.fill(FromN, W, Sign);
692 return Res;
693}
694
695
696BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1,
697 uint16_t FromN) const {
698 uint16_t W = A1.width();
699 assert(FromN <= W);
700 RegisterCell Res = RegisterCell::ref(A1);
701 Res.fill(FromN, W, BitValue::Zero);
702 return Res;
703}
704
705
706BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1,
707 uint16_t B, uint16_t E) const {
708 uint16_t W = A1.width();
709 assert(B < W && E <= W);
710 if (B == E)
711 return RegisterCell(0);
712 uint16_t Last = (E > 0) ? E-1 : W-1;
713 RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last));
714 // Return shorter cell.
715 return Res;
716}
717
718
719BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1,
720 const RegisterCell &A2, uint16_t AtN) const {
721 uint16_t W1 = A1.width(), W2 = A2.width();
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000722 (void)W1;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000723 assert(AtN < W1 && AtN+W2 <= W1);
724 // Copy bits from A1, insert A2 at position AtN.
725 RegisterCell Res = RegisterCell::ref(A1);
726 if (W2 > 0)
727 Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1));
728 return Res;
729}
730
731
732BT::BitMask BT::MachineEvaluator::mask(unsigned Reg, unsigned Sub) const {
733 assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0");
734 uint16_t W = getRegBitWidth(Reg);
735 assert(W > 0 && "Cannot generate mask for empty register");
736 return BitMask(0, W-1);
737}
738
739
740bool BT::MachineEvaluator::evaluate(const MachineInstr *MI,
741 const CellMapType &Inputs, CellMapType &Outputs) const {
742 unsigned Opc = MI->getOpcode();
743 switch (Opc) {
744 case TargetOpcode::REG_SEQUENCE: {
745 RegisterRef RD = MI->getOperand(0);
746 assert(RD.Sub == 0);
747 RegisterRef RS = MI->getOperand(1);
748 unsigned SS = MI->getOperand(2).getImm();
749 RegisterRef RT = MI->getOperand(3);
750 unsigned ST = MI->getOperand(4).getImm();
751 assert(SS != ST);
752
753 uint16_t W = getRegBitWidth(RD);
754 RegisterCell Res(W);
755 Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS));
756 Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST));
757 putCell(RD, Res, Outputs);
758 break;
759 }
760
761 case TargetOpcode::COPY: {
762 // COPY can transfer a smaller register into a wider one.
763 // If that is the case, fill the remaining high bits with 0.
764 RegisterRef RD = MI->getOperand(0);
765 RegisterRef RS = MI->getOperand(1);
766 assert(RD.Sub == 0);
767 uint16_t WD = getRegBitWidth(RD);
768 uint16_t WS = getRegBitWidth(RS);
769 assert(WD >= WS);
770 RegisterCell Src = getCell(RS, Inputs);
771 RegisterCell Res(WD);
772 Res.insert(Src, BitMask(0, WS-1));
773 Res.fill(WS, WD, BitValue::Zero);
774 putCell(RD, Res, Outputs);
775 break;
776 }
777
778 default:
779 return false;
780 }
781
782 return true;
783}
784
785
786// Main W-Z implementation.
787
788void BT::visitPHI(const MachineInstr *PI) {
789 int ThisN = PI->getParent()->getNumber();
790 if (Trace)
791 dbgs() << "Visit FI(BB#" << ThisN << "): " << *PI;
792
793 const MachineOperand &MD = PI->getOperand(0);
794 assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition");
795 RegisterRef DefRR(MD);
796 uint16_t DefBW = ME.getRegBitWidth(DefRR);
797
798 RegisterCell DefC = ME.getCell(DefRR, Map);
799 if (DefC == RegisterCell::self(DefRR.Reg, DefBW)) // XXX slow
800 return;
801
802 bool Changed = false;
803
804 for (unsigned i = 1, n = PI->getNumOperands(); i < n; i += 2) {
805 const MachineBasicBlock *PB = PI->getOperand(i+1).getMBB();
806 int PredN = PB->getNumber();
807 if (Trace)
808 dbgs() << " edge BB#" << PredN << "->BB#" << ThisN;
809 if (!EdgeExec.count(CFGEdge(PredN, ThisN))) {
810 if (Trace)
811 dbgs() << " not executable\n";
812 continue;
813 }
814
815 RegisterRef RU = PI->getOperand(i);
816 RegisterCell ResC = ME.getCell(RU, Map);
817 if (Trace)
818 dbgs() << " input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
819 << " cell: " << ResC << "\n";
820 Changed |= DefC.meet(ResC, DefRR.Reg);
821 }
822
823 if (Changed) {
824 if (Trace)
825 dbgs() << "Output: " << PrintReg(DefRR.Reg, &ME.TRI, DefRR.Sub)
826 << " cell: " << DefC << "\n";
827 ME.putCell(DefRR, DefC, Map);
828 visitUsesOf(DefRR.Reg);
829 }
830}
831
832
833void BT::visitNonBranch(const MachineInstr *MI) {
834 if (Trace) {
835 int ThisN = MI->getParent()->getNumber();
836 dbgs() << "Visit MI(BB#" << ThisN << "): " << *MI;
837 }
838 if (MI->isDebugValue())
839 return;
840 assert(!MI->isBranch() && "Unexpected branch instruction");
841
842 CellMapType ResMap;
843 bool Eval = ME.evaluate(MI, Map, ResMap);
844
845 if (Trace && Eval) {
846 for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
847 const MachineOperand &MO = MI->getOperand(i);
848 if (!MO.isReg() || !MO.isUse())
849 continue;
850 RegisterRef RU(MO);
851 dbgs() << " input reg: " << PrintReg(RU.Reg, &ME.TRI, RU.Sub)
852 << " cell: " << ME.getCell(RU, Map) << "\n";
853 }
854 dbgs() << "Outputs:\n";
855 for (CellMapType::iterator I = ResMap.begin(), E = ResMap.end();
856 I != E; ++I) {
857 RegisterRef RD(I->first);
858 dbgs() << " " << PrintReg(I->first, &ME.TRI) << " cell: "
859 << ME.getCell(RD, ResMap) << "\n";
860 }
861 }
862
863 // Iterate over all definitions of the instruction, and update the
864 // cells accordingly.
865 for (unsigned i = 0, n = MI->getNumOperands(); i < n; ++i) {
866 const MachineOperand &MO = MI->getOperand(i);
867 // Visit register defs only.
868 if (!MO.isReg() || !MO.isDef())
869 continue;
870 RegisterRef RD(MO);
871 assert(RD.Sub == 0 && "Unexpected sub-register in definition");
872 if (!TargetRegisterInfo::isVirtualRegister(RD.Reg))
873 continue;
874
875 bool Changed = false;
876 if (!Eval || !ResMap.has(RD.Reg)) {
877 // Set to "ref" (aka "bottom").
878 uint16_t DefBW = ME.getRegBitWidth(RD);
879 RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW);
880 if (RefC != ME.getCell(RD, Map)) {
881 ME.putCell(RD, RefC, Map);
882 Changed = true;
883 }
884 } else {
885 RegisterCell DefC = ME.getCell(RD, Map);
886 RegisterCell ResC = ME.getCell(RD, ResMap);
887 // This is a non-phi instruction, so the values of the inputs come
888 // from the same registers each time this instruction is evaluated.
889 // During the propagation, the values of the inputs can become lowered
890 // in the sense of the lattice operation, which may cause different
891 // results to be calculated in subsequent evaluations. This should
892 // not cause the bottoming of the result in the map, since the new
893 // result is already reflecting the lowered inputs.
894 for (uint16_t i = 0, w = DefC.width(); i < w; ++i) {
895 BitValue &V = DefC[i];
896 // Bits that are already "bottom" should not be updated.
897 if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg)
898 continue;
899 // Same for those that are identical in DefC and ResC.
900 if (V == ResC[i])
901 continue;
902 V = ResC[i];
903 Changed = true;
904 }
905 if (Changed)
906 ME.putCell(RD, DefC, Map);
907 }
908 if (Changed)
909 visitUsesOf(RD.Reg);
910 }
911}
912
913
914void BT::visitBranchesFrom(const MachineInstr *BI) {
915 const MachineBasicBlock &B = *BI->getParent();
916 MachineBasicBlock::const_iterator It = BI, End = B.end();
917 BranchTargetList Targets, BTs;
918 bool FallsThrough = true, DefaultToAll = false;
919 int ThisN = B.getNumber();
920
921 do {
922 BTs.clear();
923 const MachineInstr *MI = &*It;
924 if (Trace)
925 dbgs() << "Visit BR(BB#" << ThisN << "): " << *MI;
926 assert(MI->isBranch() && "Expecting branch instruction");
927 InstrExec.insert(MI);
928 bool Eval = ME.evaluate(MI, Map, BTs, FallsThrough);
929 if (!Eval) {
930 // If the evaluation failed, we will add all targets. Keep going in
931 // the loop to mark all executable branches as such.
932 DefaultToAll = true;
933 FallsThrough = true;
934 if (Trace)
935 dbgs() << " failed to evaluate: will add all CFG successors\n";
936 } else if (!DefaultToAll) {
937 // If evaluated successfully add the targets to the cumulative list.
938 if (Trace) {
939 dbgs() << " adding targets:";
940 for (unsigned i = 0, n = BTs.size(); i < n; ++i)
941 dbgs() << " BB#" << BTs[i]->getNumber();
942 if (FallsThrough)
943 dbgs() << "\n falls through\n";
944 else
945 dbgs() << "\n does not fall through\n";
946 }
947 Targets.insert(BTs.begin(), BTs.end());
948 }
949 ++It;
950 } while (FallsThrough && It != End);
951
952 typedef MachineBasicBlock::const_succ_iterator succ_iterator;
953 if (!DefaultToAll) {
954 // Need to add all CFG successors that lead to EH landing pads.
955 // There won't be explicit branches to these blocks, but they must
956 // be processed.
957 for (succ_iterator I = B.succ_begin(), E = B.succ_end(); I != E; ++I) {
958 const MachineBasicBlock *SB = *I;
959 if (SB->isLandingPad())
960 Targets.insert(SB);
961 }
962 if (FallsThrough) {
963 MachineFunction::const_iterator BIt = &B;
964 MachineFunction::const_iterator Next = std::next(BIt);
965 if (Next != MF.end())
966 Targets.insert(&*Next);
967 }
968 } else {
969 for (succ_iterator I = B.succ_begin(), E = B.succ_end(); I != E; ++I)
970 Targets.insert(*I);
971 }
972
973 for (unsigned i = 0, n = Targets.size(); i < n; ++i) {
974 int TargetN = Targets[i]->getNumber();
975 FlowQ.push(CFGEdge(ThisN, TargetN));
976 }
977}
978
979
980void BT::visitUsesOf(unsigned Reg) {
981 if (Trace)
982 dbgs() << "visiting uses of " << PrintReg(Reg, &ME.TRI) << "\n";
983
984 typedef MachineRegisterInfo::use_nodbg_iterator use_iterator;
985 use_iterator End = MRI.use_nodbg_end();
986 for (use_iterator I = MRI.use_nodbg_begin(Reg); I != End; ++I) {
987 MachineInstr *UseI = I->getParent();
988 if (!InstrExec.count(UseI))
989 continue;
990 if (UseI->isPHI())
991 visitPHI(UseI);
992 else if (!UseI->isBranch())
993 visitNonBranch(UseI);
994 else
995 visitBranchesFrom(UseI);
996 }
997}
998
999
1000BT::RegisterCell BT::get(RegisterRef RR) const {
1001 return ME.getCell(RR, Map);
1002}
1003
1004
1005void BT::put(RegisterRef RR, const RegisterCell &RC) {
1006 ME.putCell(RR, RC, Map);
1007}
1008
1009
1010// Replace all references to bits from OldRR with the corresponding bits
1011// in NewRR.
1012void BT::subst(RegisterRef OldRR, RegisterRef NewRR) {
1013 assert(Map.has(OldRR.Reg) && "OldRR not present in map");
1014 BitMask OM = ME.mask(OldRR.Reg, OldRR.Sub);
1015 BitMask NM = ME.mask(NewRR.Reg, NewRR.Sub);
1016 uint16_t OMB = OM.first(), OME = OM.last();
1017 uint16_t NMB = NM.first(), NME = NM.last();
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +00001018 (void)NME;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001019 assert((OME-OMB == NME-NMB) &&
1020 "Substituting registers of different lengths");
1021 for (CellMapType::iterator I = Map.begin(), E = Map.end(); I != E; ++I) {
1022 RegisterCell &RC = I->second;
1023 for (uint16_t i = 0, w = RC.width(); i < w; ++i) {
1024 BitValue &V = RC[i];
1025 if (V.Type != BitValue::Ref || V.RefI.Reg != OldRR.Reg)
1026 continue;
1027 if (V.RefI.Pos < OMB || V.RefI.Pos > OME)
1028 continue;
1029 V.RefI.Reg = NewRR.Reg;
1030 V.RefI.Pos += NMB-OMB;
1031 }
1032 }
1033}
1034
1035
1036// Check if the block has been "executed" during propagation. (If not, the
1037// block is dead, but it may still appear to be reachable.)
1038bool BT::reached(const MachineBasicBlock *B) const {
1039 int BN = B->getNumber();
1040 assert(BN >= 0);
1041 for (EdgeSetType::iterator I = EdgeExec.begin(), E = EdgeExec.end();
1042 I != E; ++I) {
1043 if (I->second == BN)
1044 return true;
1045 }
1046 return false;
1047}
1048
1049
1050void BT::reset() {
1051 EdgeExec.clear();
1052 InstrExec.clear();
1053 Map.clear();
1054}
1055
1056
1057void BT::run() {
1058 reset();
1059 assert(FlowQ.empty());
1060
1061 typedef GraphTraits<const MachineFunction*> MachineFlowGraphTraits;
1062 const MachineBasicBlock *Entry = MachineFlowGraphTraits::getEntryNode(&MF);
1063
1064 unsigned MaxBN = 0;
1065 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
1066 I != E; ++I) {
1067 assert(I->getNumber() >= 0 && "Disconnected block");
1068 unsigned BN = I->getNumber();
1069 if (BN > MaxBN)
1070 MaxBN = BN;
1071 }
1072
1073 // Keep track of visited blocks.
1074 BitVector BlockScanned(MaxBN+1);
1075
1076 int EntryN = Entry->getNumber();
1077 // Generate a fake edge to get something to start with.
1078 FlowQ.push(CFGEdge(-1, EntryN));
1079
1080 while (!FlowQ.empty()) {
1081 CFGEdge Edge = FlowQ.front();
1082 FlowQ.pop();
1083
1084 if (EdgeExec.count(Edge))
1085 continue;
1086 EdgeExec.insert(Edge);
1087
1088 const MachineBasicBlock &B = *MF.getBlockNumbered(Edge.second);
1089 MachineBasicBlock::const_iterator It = B.begin(), End = B.end();
1090 // Visit PHI nodes first.
1091 while (It != End && It->isPHI()) {
1092 const MachineInstr *PI = &*It++;
1093 InstrExec.insert(PI);
1094 visitPHI(PI);
1095 }
1096
1097 // If this block has already been visited through a flow graph edge,
1098 // then the instructions have already been processed. Any updates to
1099 // the cells would now only happen through visitUsesOf...
1100 if (BlockScanned[Edge.second])
1101 continue;
1102 BlockScanned[Edge.second] = true;
1103
1104 // Visit non-branch instructions.
1105 while (It != End && !It->isBranch()) {
1106 const MachineInstr *MI = &*It++;
1107 InstrExec.insert(MI);
1108 visitNonBranch(MI);
1109 }
1110 // If block end has been reached, add the fall-through edge to the queue.
1111 if (It == End) {
1112 MachineFunction::const_iterator BIt = &B;
1113 MachineFunction::const_iterator Next = std::next(BIt);
1114 if (Next != MF.end()) {
1115 int ThisN = B.getNumber();
1116 int NextN = Next->getNumber();
1117 FlowQ.push(CFGEdge(ThisN, NextN));
1118 }
1119 } else {
1120 // Handle the remaining sequence of branches. This function will update
1121 // the work queue.
1122 visitBranchesFrom(It);
1123 }
1124 } // while (!FlowQ->empty())
1125
1126 if (Trace) {
1127 dbgs() << "Cells after propagation:\n";
1128 for (CellMapType::iterator I = Map.begin(), E = Map.end(); I != E; ++I)
1129 dbgs() << PrintReg(I->first, &ME.TRI) << " -> " << I->second << "\n";
1130 }
1131}
1132