blob: 15d6a05a0078c136d15a6187921821f2a06ab91f [file] [log] [blame]
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +00001//===- BitTracker.cpp -----------------------------------------------------===//
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00002//
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
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000021// %2 = ASL %1, 1
22// assuming that nothing is known about bits of %1, bit 1 of %2
23// will be a "ref" to (%1, 0). If there is a subsequent instruction
24// %3 = ASL %2, 2
25// then bit 3 of %3 will be a "ref" to (%1, 0) as well.
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000026// 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
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000029// to self", so for the %1 above, the bit 0 of it will be a "ref" to
30// (%1, 0), bit 1 will be a "ref" to (%1, 1), etc.
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000031//
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
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000056#include "BitTracker.h"
57#include "llvm/ADT/APInt.h"
58#include "llvm/ADT/BitVector.h"
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000059#include "llvm/CodeGen/MachineBasicBlock.h"
60#include "llvm/CodeGen/MachineFunction.h"
61#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000062#include "llvm/CodeGen/MachineOperand.h"
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000063#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000064#include "llvm/CodeGen/TargetRegisterInfo.h"
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000065#include "llvm/IR/Constants.h"
66#include "llvm/Support/Debug.h"
67#include "llvm/Support/raw_ostream.h"
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000068#include <cassert>
69#include <cstdint>
Chandler Carruth6bda14b2017-06-06 11:49:48 +000070#include <iterator>
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000071
72using namespace llvm;
73
Eugene Zelenkoe4fc6ee2017-07-26 23:20:35 +000074using BT = BitTracker;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000075
76namespace {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000077
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000078 // Local trickery to pretty print a register (without the whole "%number"
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000079 // business).
80 struct printv {
81 printv(unsigned r) : R(r) {}
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000082
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000083 unsigned R;
84 };
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000085
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000086 raw_ostream &operator<< (raw_ostream &OS, const printv &PV) {
87 if (PV.R)
88 OS << 'v' << TargetRegisterInfo::virtReg2Index(PV.R);
89 else
90 OS << 's';
91 return OS;
92 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000093
94} // end anonymous namespace
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +000095
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +000096namespace llvm {
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +000097
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +000098 raw_ostream &operator<<(raw_ostream &OS, const BT::BitValue &BV) {
99 switch (BV.Type) {
100 case BT::BitValue::Top:
101 OS << 'T';
102 break;
103 case BT::BitValue::Zero:
104 OS << '0';
105 break;
106 case BT::BitValue::One:
107 OS << '1';
108 break;
109 case BT::BitValue::Ref:
110 OS << printv(BV.RefI.Reg) << '[' << BV.RefI.Pos << ']';
111 break;
112 }
113 return OS;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000114 }
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000115
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000116 raw_ostream &operator<<(raw_ostream &OS, const BT::RegisterCell &RC) {
117 unsigned n = RC.Bits.size();
118 OS << "{ w:" << n;
119 // Instead of printing each bit value individually, try to group them
120 // into logical segments, such as sequences of 0 or 1 bits or references
121 // to consecutive bits (e.g. "bits 3-5 are same as bits 7-9 of reg xyz").
122 // "Start" will be the index of the beginning of the most recent segment.
123 unsigned Start = 0;
124 bool SeqRef = false; // A sequence of refs to consecutive bits.
125 bool ConstRef = false; // A sequence of refs to the same bit.
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000126
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000127 for (unsigned i = 1, n = RC.Bits.size(); i < n; ++i) {
128 const BT::BitValue &V = RC[i];
129 const BT::BitValue &SV = RC[Start];
130 bool IsRef = (V.Type == BT::BitValue::Ref);
131 // If the current value is the same as Start, skip to the next one.
132 if (!IsRef && V == SV)
133 continue;
134 if (IsRef && SV.Type == BT::BitValue::Ref && V.RefI.Reg == SV.RefI.Reg) {
135 if (Start+1 == i) {
136 SeqRef = (V.RefI.Pos == SV.RefI.Pos+1);
137 ConstRef = (V.RefI.Pos == SV.RefI.Pos);
138 }
139 if (SeqRef && V.RefI.Pos == SV.RefI.Pos+(i-Start))
140 continue;
141 if (ConstRef && V.RefI.Pos == SV.RefI.Pos)
142 continue;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000143 }
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000144
145 // The current value is different. Print the previous one and reset
146 // the Start.
147 OS << " [" << Start;
148 unsigned Count = i - Start;
149 if (Count == 1) {
150 OS << "]:" << SV;
151 } else {
152 OS << '-' << i-1 << "]:";
153 if (SV.Type == BT::BitValue::Ref && SeqRef)
154 OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
155 << SV.RefI.Pos+(Count-1) << ']';
156 else
157 OS << SV;
158 }
159 Start = i;
160 SeqRef = ConstRef = false;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000161 }
162
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000163 OS << " [" << Start;
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000164 unsigned Count = n - Start;
165 if (n-Start == 1) {
166 OS << "]:" << RC[Start];
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000167 } else {
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000168 OS << '-' << n-1 << "]:";
169 const BT::BitValue &SV = RC[Start];
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000170 if (SV.Type == BT::BitValue::Ref && SeqRef)
171 OS << printv(SV.RefI.Reg) << '[' << SV.RefI.Pos << '-'
172 << SV.RefI.Pos+(Count-1) << ']';
173 else
174 OS << SV;
175 }
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000176 OS << " }";
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000177
Krzysztof Parzyszek754bad82016-02-18 16:10:27 +0000178 return OS;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000179 }
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000180
181} // end namespace llvm
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000182
Krzysztof Parzyszek623afbd2016-08-03 18:13:32 +0000183void BitTracker::print_cells(raw_ostream &OS) const {
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +0000184 for (const std::pair<unsigned, RegisterCell> P : Map)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000185 dbgs() << printReg(P.first, &ME.TRI) << " -> " << P.second << "\n";
Krzysztof Parzyszek623afbd2016-08-03 18:13:32 +0000186}
187
Benjamin Kramerd8861512015-07-13 20:38:16 +0000188BitTracker::BitTracker(const MachineEvaluator &E, MachineFunction &F)
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +0000189 : ME(E), MF(F), MRI(F.getRegInfo()), Map(*new CellMapType), Trace(false) {
190}
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000191
192BitTracker::~BitTracker() {
193 delete &Map;
194}
195
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000196// If we were allowed to update a cell for a part of a register, the meet
197// operation would need to be parametrized by the register number and the
198// exact part of the register, so that the computer BitRefs correspond to
199// the actual bits of the "self" register.
200// While this cannot happen in the current implementation, I'm not sure
201// if this should be ruled out in the future.
202bool BT::RegisterCell::meet(const RegisterCell &RC, unsigned SelfR) {
203 // An example when "meet" can be invoked with SelfR == 0 is a phi node
204 // with a physical register as an operand.
205 assert(SelfR == 0 || TargetRegisterInfo::isVirtualRegister(SelfR));
206 bool Changed = false;
207 for (uint16_t i = 0, n = Bits.size(); i < n; ++i) {
208 const BitValue &RCV = RC[i];
209 Changed |= Bits[i].meet(RCV, BitRef(SelfR, i));
210 }
211 return Changed;
212}
213
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000214// Insert the entire cell RC into the current cell at position given by M.
215BT::RegisterCell &BT::RegisterCell::insert(const BT::RegisterCell &RC,
216 const BitMask &M) {
217 uint16_t B = M.first(), E = M.last(), W = width();
218 // Sanity: M must be a valid mask for *this.
219 assert(B < W && E < W);
220 // Sanity: the masked part of *this must have the same number of bits
221 // as the source.
222 assert(B > E || E-B+1 == RC.width()); // B <= E => E-B+1 = |RC|.
223 assert(B <= E || E+(W-B)+1 == RC.width()); // E < B => E+(W-B)+1 = |RC|.
224 if (B <= E) {
225 for (uint16_t i = 0; i <= E-B; ++i)
226 Bits[i+B] = RC[i];
227 } else {
228 for (uint16_t i = 0; i < W-B; ++i)
229 Bits[i+B] = RC[i];
230 for (uint16_t i = 0; i <= E; ++i)
231 Bits[i] = RC[i+(W-B)];
232 }
233 return *this;
234}
235
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000236BT::RegisterCell BT::RegisterCell::extract(const BitMask &M) const {
237 uint16_t B = M.first(), E = M.last(), W = width();
238 assert(B < W && E < W);
239 if (B <= E) {
240 RegisterCell RC(E-B+1);
241 for (uint16_t i = B; i <= E; ++i)
242 RC.Bits[i-B] = Bits[i];
243 return RC;
244 }
245
246 RegisterCell RC(E+(W-B)+1);
247 for (uint16_t i = 0; i < W-B; ++i)
248 RC.Bits[i] = Bits[i+B];
249 for (uint16_t i = 0; i <= E; ++i)
250 RC.Bits[i+(W-B)] = Bits[i];
251 return RC;
252}
253
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000254BT::RegisterCell &BT::RegisterCell::rol(uint16_t Sh) {
255 // Rotate left (i.e. towards increasing bit indices).
256 // Swap the two parts: [0..W-Sh-1] [W-Sh..W-1]
257 uint16_t W = width();
258 Sh = Sh % W;
259 if (Sh == 0)
260 return *this;
261
262 RegisterCell Tmp(W-Sh);
263 // Tmp = [0..W-Sh-1].
264 for (uint16_t i = 0; i < W-Sh; ++i)
265 Tmp[i] = Bits[i];
266 // Shift [W-Sh..W-1] to [0..Sh-1].
267 for (uint16_t i = 0; i < Sh; ++i)
268 Bits[i] = Bits[W-Sh+i];
269 // Copy Tmp to [Sh..W-1].
270 for (uint16_t i = 0; i < W-Sh; ++i)
271 Bits[i+Sh] = Tmp.Bits[i];
272 return *this;
273}
274
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000275BT::RegisterCell &BT::RegisterCell::fill(uint16_t B, uint16_t E,
276 const BitValue &V) {
277 assert(B <= E);
278 while (B < E)
279 Bits[B++] = V;
280 return *this;
281}
282
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000283BT::RegisterCell &BT::RegisterCell::cat(const RegisterCell &RC) {
284 // Append the cell given as the argument to the "this" cell.
285 // Bit 0 of RC becomes bit W of the result, where W is this->width().
286 uint16_t W = width(), WRC = RC.width();
287 Bits.resize(W+WRC);
288 for (uint16_t i = 0; i < WRC; ++i)
289 Bits[i+W] = RC.Bits[i];
290 return *this;
291}
292
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000293uint16_t BT::RegisterCell::ct(bool B) const {
294 uint16_t W = width();
295 uint16_t C = 0;
296 BitValue V = B;
297 while (C < W && Bits[C] == V)
298 C++;
299 return C;
300}
301
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000302uint16_t BT::RegisterCell::cl(bool B) const {
303 uint16_t W = width();
304 uint16_t C = 0;
305 BitValue V = B;
306 while (C < W && Bits[W-(C+1)] == V)
307 C++;
308 return C;
309}
310
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000311bool BT::RegisterCell::operator== (const RegisterCell &RC) const {
312 uint16_t W = Bits.size();
313 if (RC.Bits.size() != W)
314 return false;
315 for (uint16_t i = 0; i < W; ++i)
316 if (Bits[i] != RC[i])
317 return false;
318 return true;
319}
320
Krzysztof Parzyszek998e49e2017-02-23 22:08:50 +0000321BT::RegisterCell &BT::RegisterCell::regify(unsigned R) {
322 for (unsigned i = 0, n = width(); i < n; ++i) {
323 const BitValue &V = Bits[i];
324 if (V.Type == BitValue::Ref && V.RefI.Reg == 0)
325 Bits[i].RefI = BitRef(R, i);
326 }
327 return *this;
328}
329
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000330uint16_t BT::MachineEvaluator::getRegBitWidth(const RegisterRef &RR) const {
331 // The general problem is with finding a register class that corresponds
332 // to a given reference reg:sub. There can be several such classes, and
333 // since we only care about the register size, it does not matter which
334 // such class we would find.
335 // The easiest way to accomplish what we want is to
336 // 1. find a physical register PhysR from the same class as RR.Reg,
337 // 2. find a physical register PhysS that corresponds to PhysR:RR.Sub,
338 // 3. find a register class that contains PhysS.
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000339 if (TargetRegisterInfo::isVirtualRegister(RR.Reg)) {
Krzysztof Parzyszek7e604de2017-09-25 19:12:55 +0000340 const auto &VC = composeWithSubRegIndex(*MRI.getRegClass(RR.Reg), RR.Sub);
341 return TRI.getRegSizeInBits(VC);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000342 }
Krzysztof Parzyszek7e604de2017-09-25 19:12:55 +0000343 assert(TargetRegisterInfo::isPhysicalRegister(RR.Reg));
344 unsigned PhysR = (RR.Sub == 0) ? RR.Reg : TRI.getSubReg(RR.Reg, RR.Sub);
345 return getPhysRegBitWidth(PhysR);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000346}
347
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000348BT::RegisterCell BT::MachineEvaluator::getCell(const RegisterRef &RR,
349 const CellMapType &M) const {
350 uint16_t BW = getRegBitWidth(RR);
351
352 // Physical registers are assumed to be present in the map with an unknown
353 // value. Don't actually insert anything in the map, just return the cell.
354 if (TargetRegisterInfo::isPhysicalRegister(RR.Reg))
355 return RegisterCell::self(0, BW);
356
357 assert(TargetRegisterInfo::isVirtualRegister(RR.Reg));
358 // For virtual registers that belong to a class that is not tracked,
359 // generate an "unknown" value as well.
360 const TargetRegisterClass *C = MRI.getRegClass(RR.Reg);
361 if (!track(C))
362 return RegisterCell::self(0, BW);
363
364 CellMapType::const_iterator F = M.find(RR.Reg);
365 if (F != M.end()) {
366 if (!RR.Sub)
367 return F->second;
368 BitMask M = mask(RR.Reg, RR.Sub);
369 return F->second.extract(M);
370 }
371 // If not found, create a "top" entry, but do not insert it in the map.
372 return RegisterCell::top(BW);
373}
374
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000375void BT::MachineEvaluator::putCell(const RegisterRef &RR, RegisterCell RC,
376 CellMapType &M) const {
377 // While updating the cell map can be done in a meaningful way for
378 // a part of a register, it makes little sense to implement it as the
379 // SSA representation would never contain such "partial definitions".
380 if (!TargetRegisterInfo::isVirtualRegister(RR.Reg))
381 return;
382 assert(RR.Sub == 0 && "Unexpected sub-register in definition");
383 // Eliminate all ref-to-reg-0 bit values: replace them with "self".
Krzysztof Parzyszek998e49e2017-02-23 22:08:50 +0000384 M[RR.Reg] = RC.regify(RR.Reg);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000385}
386
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000387// Check if the cell represents a compile-time integer value.
388bool BT::MachineEvaluator::isInt(const RegisterCell &A) const {
389 uint16_t W = A.width();
390 for (uint16_t i = 0; i < W; ++i)
391 if (!A[i].is(0) && !A[i].is(1))
392 return false;
393 return true;
394}
395
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000396// Convert a cell to the integer value. The result must fit in uint64_t.
397uint64_t BT::MachineEvaluator::toInt(const RegisterCell &A) const {
398 assert(isInt(A));
399 uint64_t Val = 0;
400 uint16_t W = A.width();
401 for (uint16_t i = 0; i < W; ++i) {
402 Val <<= 1;
403 Val |= A[i].is(1);
404 }
405 return Val;
406}
407
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000408// Evaluator helper functions. These implement some common operation on
409// register cells that can be used to implement target-specific instructions
410// in a target-specific evaluator.
411
412BT::RegisterCell BT::MachineEvaluator::eIMM(int64_t V, uint16_t W) const {
413 RegisterCell Res(W);
414 // For bits beyond the 63rd, this will generate the sign bit of V.
415 for (uint16_t i = 0; i < W; ++i) {
416 Res[i] = BitValue(V & 1);
417 V >>= 1;
418 }
419 return Res;
420}
421
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000422BT::RegisterCell BT::MachineEvaluator::eIMM(const ConstantInt *CI) const {
Benjamin Kramer46e38f32016-06-08 10:01:20 +0000423 const APInt &A = CI->getValue();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000424 uint16_t BW = A.getBitWidth();
425 assert((unsigned)BW == A.getBitWidth() && "BitWidth overflow");
426 RegisterCell Res(BW);
427 for (uint16_t i = 0; i < BW; ++i)
428 Res[i] = A[i];
429 return Res;
430}
431
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000432BT::RegisterCell BT::MachineEvaluator::eADD(const RegisterCell &A1,
433 const RegisterCell &A2) const {
434 uint16_t W = A1.width();
435 assert(W == A2.width());
436 RegisterCell Res(W);
437 bool Carry = false;
438 uint16_t I;
439 for (I = 0; I < W; ++I) {
440 const BitValue &V1 = A1[I];
441 const BitValue &V2 = A2[I];
442 if (!V1.num() || !V2.num())
443 break;
444 unsigned S = bool(V1) + bool(V2) + Carry;
445 Res[I] = BitValue(S & 1);
446 Carry = (S > 1);
447 }
448 for (; I < W; ++I) {
449 const BitValue &V1 = A1[I];
450 const BitValue &V2 = A2[I];
451 // If the next bit is same as Carry, the result will be 0 plus the
452 // other bit. The Carry bit will remain unchanged.
453 if (V1.is(Carry))
454 Res[I] = BitValue::ref(V2);
455 else if (V2.is(Carry))
456 Res[I] = BitValue::ref(V1);
457 else
458 break;
459 }
460 for (; I < W; ++I)
461 Res[I] = BitValue::self();
462 return Res;
463}
464
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000465BT::RegisterCell BT::MachineEvaluator::eSUB(const RegisterCell &A1,
466 const RegisterCell &A2) const {
467 uint16_t W = A1.width();
468 assert(W == A2.width());
469 RegisterCell Res(W);
470 bool Borrow = false;
471 uint16_t I;
472 for (I = 0; I < W; ++I) {
473 const BitValue &V1 = A1[I];
474 const BitValue &V2 = A2[I];
475 if (!V1.num() || !V2.num())
476 break;
477 unsigned S = bool(V1) - bool(V2) - Borrow;
478 Res[I] = BitValue(S & 1);
479 Borrow = (S > 1);
480 }
481 for (; I < W; ++I) {
482 const BitValue &V1 = A1[I];
483 const BitValue &V2 = A2[I];
484 if (V1.is(Borrow)) {
485 Res[I] = BitValue::ref(V2);
486 break;
487 }
488 if (V2.is(Borrow))
489 Res[I] = BitValue::ref(V1);
490 else
491 break;
492 }
493 for (; I < W; ++I)
494 Res[I] = BitValue::self();
495 return Res;
496}
497
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000498BT::RegisterCell BT::MachineEvaluator::eMLS(const RegisterCell &A1,
499 const RegisterCell &A2) const {
500 uint16_t W = A1.width() + A2.width();
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000501 uint16_t Z = A1.ct(false) + A2.ct(false);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000502 RegisterCell Res(W);
503 Res.fill(0, Z, BitValue::Zero);
504 Res.fill(Z, W, BitValue::self());
505 return Res;
506}
507
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000508BT::RegisterCell BT::MachineEvaluator::eMLU(const RegisterCell &A1,
509 const RegisterCell &A2) const {
510 uint16_t W = A1.width() + A2.width();
Eugene Zelenkob2ca1b32017-01-04 02:02:05 +0000511 uint16_t Z = A1.ct(false) + A2.ct(false);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000512 RegisterCell Res(W);
513 Res.fill(0, Z, BitValue::Zero);
514 Res.fill(Z, W, BitValue::self());
515 return Res;
516}
517
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000518BT::RegisterCell BT::MachineEvaluator::eASL(const RegisterCell &A1,
519 uint16_t Sh) const {
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000520 assert(Sh <= A1.width());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000521 RegisterCell Res = RegisterCell::ref(A1);
522 Res.rol(Sh);
523 Res.fill(0, Sh, BitValue::Zero);
524 return Res;
525}
526
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000527BT::RegisterCell BT::MachineEvaluator::eLSR(const RegisterCell &A1,
528 uint16_t Sh) const {
529 uint16_t W = A1.width();
530 assert(Sh <= W);
531 RegisterCell Res = RegisterCell::ref(A1);
532 Res.rol(W-Sh);
533 Res.fill(W-Sh, W, BitValue::Zero);
534 return Res;
535}
536
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000537BT::RegisterCell BT::MachineEvaluator::eASR(const RegisterCell &A1,
538 uint16_t Sh) const {
539 uint16_t W = A1.width();
540 assert(Sh <= W);
541 RegisterCell Res = RegisterCell::ref(A1);
542 BitValue Sign = Res[W-1];
543 Res.rol(W-Sh);
544 Res.fill(W-Sh, W, Sign);
545 return Res;
546}
547
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000548BT::RegisterCell BT::MachineEvaluator::eAND(const RegisterCell &A1,
549 const RegisterCell &A2) const {
550 uint16_t W = A1.width();
551 assert(W == A2.width());
552 RegisterCell Res(W);
553 for (uint16_t i = 0; i < W; ++i) {
554 const BitValue &V1 = A1[i];
555 const BitValue &V2 = A2[i];
556 if (V1.is(1))
557 Res[i] = BitValue::ref(V2);
558 else if (V2.is(1))
559 Res[i] = BitValue::ref(V1);
560 else if (V1.is(0) || V2.is(0))
561 Res[i] = BitValue::Zero;
562 else if (V1 == V2)
563 Res[i] = V1;
564 else
565 Res[i] = BitValue::self();
566 }
567 return Res;
568}
569
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000570BT::RegisterCell BT::MachineEvaluator::eORL(const RegisterCell &A1,
571 const RegisterCell &A2) const {
572 uint16_t W = A1.width();
573 assert(W == A2.width());
574 RegisterCell Res(W);
575 for (uint16_t i = 0; i < W; ++i) {
576 const BitValue &V1 = A1[i];
577 const BitValue &V2 = A2[i];
578 if (V1.is(1) || V2.is(1))
579 Res[i] = BitValue::One;
580 else if (V1.is(0))
581 Res[i] = BitValue::ref(V2);
582 else if (V2.is(0))
583 Res[i] = BitValue::ref(V1);
584 else if (V1 == V2)
585 Res[i] = V1;
586 else
587 Res[i] = BitValue::self();
588 }
589 return Res;
590}
591
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000592BT::RegisterCell BT::MachineEvaluator::eXOR(const RegisterCell &A1,
593 const RegisterCell &A2) const {
594 uint16_t W = A1.width();
595 assert(W == A2.width());
596 RegisterCell Res(W);
597 for (uint16_t i = 0; i < W; ++i) {
598 const BitValue &V1 = A1[i];
599 const BitValue &V2 = A2[i];
600 if (V1.is(0))
601 Res[i] = BitValue::ref(V2);
602 else if (V2.is(0))
603 Res[i] = BitValue::ref(V1);
604 else if (V1 == V2)
605 Res[i] = BitValue::Zero;
606 else
607 Res[i] = BitValue::self();
608 }
609 return Res;
610}
611
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000612BT::RegisterCell BT::MachineEvaluator::eNOT(const RegisterCell &A1) const {
613 uint16_t W = A1.width();
614 RegisterCell Res(W);
615 for (uint16_t i = 0; i < W; ++i) {
616 const BitValue &V = A1[i];
617 if (V.is(0))
618 Res[i] = BitValue::One;
619 else if (V.is(1))
620 Res[i] = BitValue::Zero;
621 else
622 Res[i] = BitValue::self();
623 }
624 return Res;
625}
626
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000627BT::RegisterCell BT::MachineEvaluator::eSET(const RegisterCell &A1,
628 uint16_t BitN) const {
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000629 assert(BitN < A1.width());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000630 RegisterCell Res = RegisterCell::ref(A1);
631 Res[BitN] = BitValue::One;
632 return Res;
633}
634
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000635BT::RegisterCell BT::MachineEvaluator::eCLR(const RegisterCell &A1,
636 uint16_t BitN) const {
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000637 assert(BitN < A1.width());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000638 RegisterCell Res = RegisterCell::ref(A1);
639 Res[BitN] = BitValue::Zero;
640 return Res;
641}
642
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000643BT::RegisterCell BT::MachineEvaluator::eCLB(const RegisterCell &A1, bool B,
644 uint16_t W) const {
645 uint16_t C = A1.cl(B), AW = A1.width();
646 // If the last leading non-B bit is not a constant, then we don't know
647 // the real count.
648 if ((C < AW && A1[AW-1-C].num()) || C == AW)
649 return eIMM(C, W);
650 return RegisterCell::self(0, W);
651}
652
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000653BT::RegisterCell BT::MachineEvaluator::eCTB(const RegisterCell &A1, bool B,
654 uint16_t W) const {
655 uint16_t C = A1.ct(B), AW = A1.width();
656 // If the last trailing non-B bit is not a constant, then we don't know
657 // the real count.
658 if ((C < AW && A1[C].num()) || C == AW)
659 return eIMM(C, W);
660 return RegisterCell::self(0, W);
661}
662
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000663BT::RegisterCell BT::MachineEvaluator::eSXT(const RegisterCell &A1,
664 uint16_t FromN) const {
665 uint16_t W = A1.width();
666 assert(FromN <= W);
667 RegisterCell Res = RegisterCell::ref(A1);
668 BitValue Sign = Res[FromN-1];
669 // Sign-extend "inreg".
670 Res.fill(FromN, W, Sign);
671 return Res;
672}
673
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000674BT::RegisterCell BT::MachineEvaluator::eZXT(const RegisterCell &A1,
675 uint16_t FromN) const {
676 uint16_t W = A1.width();
677 assert(FromN <= W);
678 RegisterCell Res = RegisterCell::ref(A1);
679 Res.fill(FromN, W, BitValue::Zero);
680 return Res;
681}
682
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000683BT::RegisterCell BT::MachineEvaluator::eXTR(const RegisterCell &A1,
684 uint16_t B, uint16_t E) const {
685 uint16_t W = A1.width();
686 assert(B < W && E <= W);
687 if (B == E)
688 return RegisterCell(0);
689 uint16_t Last = (E > 0) ? E-1 : W-1;
690 RegisterCell Res = RegisterCell::ref(A1).extract(BT::BitMask(B, Last));
691 // Return shorter cell.
692 return Res;
693}
694
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000695BT::RegisterCell BT::MachineEvaluator::eINS(const RegisterCell &A1,
696 const RegisterCell &A2, uint16_t AtN) const {
697 uint16_t W1 = A1.width(), W2 = A2.width();
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +0000698 (void)W1;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000699 assert(AtN < W1 && AtN+W2 <= W1);
700 // Copy bits from A1, insert A2 at position AtN.
701 RegisterCell Res = RegisterCell::ref(A1);
702 if (W2 > 0)
703 Res.insert(RegisterCell::ref(A2), BT::BitMask(AtN, AtN+W2-1));
704 return Res;
705}
706
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000707BT::BitMask BT::MachineEvaluator::mask(unsigned Reg, unsigned Sub) const {
708 assert(Sub == 0 && "Generic BitTracker::mask called for Sub != 0");
709 uint16_t W = getRegBitWidth(Reg);
710 assert(W > 0 && "Cannot generate mask for empty register");
711 return BitMask(0, W-1);
712}
713
Krzysztof Parzyszek7e604de2017-09-25 19:12:55 +0000714uint16_t BT::MachineEvaluator::getPhysRegBitWidth(unsigned Reg) const {
715 assert(TargetRegisterInfo::isPhysicalRegister(Reg));
716 const TargetRegisterClass &PC = *TRI.getMinimalPhysRegClass(Reg);
717 return TRI.getRegSizeInBits(PC);
718}
719
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000720bool BT::MachineEvaluator::evaluate(const MachineInstr &MI,
721 const CellMapType &Inputs,
722 CellMapType &Outputs) const {
723 unsigned Opc = MI.getOpcode();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000724 switch (Opc) {
725 case TargetOpcode::REG_SEQUENCE: {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000726 RegisterRef RD = MI.getOperand(0);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000727 assert(RD.Sub == 0);
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000728 RegisterRef RS = MI.getOperand(1);
729 unsigned SS = MI.getOperand(2).getImm();
730 RegisterRef RT = MI.getOperand(3);
731 unsigned ST = MI.getOperand(4).getImm();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000732 assert(SS != ST);
733
734 uint16_t W = getRegBitWidth(RD);
735 RegisterCell Res(W);
736 Res.insert(RegisterCell::ref(getCell(RS, Inputs)), mask(RD.Reg, SS));
737 Res.insert(RegisterCell::ref(getCell(RT, Inputs)), mask(RD.Reg, ST));
738 putCell(RD, Res, Outputs);
739 break;
740 }
741
742 case TargetOpcode::COPY: {
743 // COPY can transfer a smaller register into a wider one.
744 // If that is the case, fill the remaining high bits with 0.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000745 RegisterRef RD = MI.getOperand(0);
746 RegisterRef RS = MI.getOperand(1);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000747 assert(RD.Sub == 0);
748 uint16_t WD = getRegBitWidth(RD);
749 uint16_t WS = getRegBitWidth(RS);
750 assert(WD >= WS);
751 RegisterCell Src = getCell(RS, Inputs);
752 RegisterCell Res(WD);
753 Res.insert(Src, BitMask(0, WS-1));
754 Res.fill(WS, WD, BitValue::Zero);
755 putCell(RD, Res, Outputs);
756 break;
757 }
758
759 default:
760 return false;
761 }
762
763 return true;
764}
765
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +0000766bool BT::UseQueueType::Cmp::operator()(const MachineInstr *InstA,
767 const MachineInstr *InstB) const {
768 // This is a comparison function for a priority queue: give higher priority
769 // to earlier instructions.
770 // This operator is used as "less", so returning "true" gives InstB higher
771 // priority (because then InstA < InstB).
772 if (InstA == InstB)
773 return false;
774 const MachineBasicBlock *BA = InstA->getParent();
775 const MachineBasicBlock *BB = InstB->getParent();
776 if (BA != BB) {
777 // If the blocks are different, ideally the dominating block would
778 // have a higher priority, but it may be too expensive to check.
779 return BA->getNumber() > BB->getNumber();
780 }
781
782 MachineBasicBlock::const_iterator ItA = InstA->getIterator();
783 MachineBasicBlock::const_iterator ItB = InstB->getIterator();
784 MachineBasicBlock::const_iterator End = BA->end();
785 while (ItA != End) {
786 if (ItA == ItB)
787 return false; // ItA was before ItB.
788 ++ItA;
789 }
790 return true;
791}
792
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000793// Main W-Z implementation.
794
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000795void BT::visitPHI(const MachineInstr &PI) {
796 int ThisN = PI.getParent()->getNumber();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000797 if (Trace)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000798 dbgs() << "Visit FI(" << printMBBReference(*PI.getParent()) << "): " << PI;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000799
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000800 const MachineOperand &MD = PI.getOperand(0);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000801 assert(MD.getSubReg() == 0 && "Unexpected sub-register in definition");
802 RegisterRef DefRR(MD);
803 uint16_t DefBW = ME.getRegBitWidth(DefRR);
804
805 RegisterCell DefC = ME.getCell(DefRR, Map);
806 if (DefC == RegisterCell::self(DefRR.Reg, DefBW)) // XXX slow
807 return;
808
809 bool Changed = false;
810
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000811 for (unsigned i = 1, n = PI.getNumOperands(); i < n; i += 2) {
812 const MachineBasicBlock *PB = PI.getOperand(i + 1).getMBB();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000813 int PredN = PB->getNumber();
814 if (Trace)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000815 dbgs() << " edge " << printMBBReference(*PB) << "->"
816 << printMBBReference(*PI.getParent());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000817 if (!EdgeExec.count(CFGEdge(PredN, ThisN))) {
818 if (Trace)
819 dbgs() << " not executable\n";
820 continue;
821 }
822
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000823 RegisterRef RU = PI.getOperand(i);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000824 RegisterCell ResC = ME.getCell(RU, Map);
825 if (Trace)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000826 dbgs() << " input reg: " << printReg(RU.Reg, &ME.TRI, RU.Sub)
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000827 << " cell: " << ResC << "\n";
828 Changed |= DefC.meet(ResC, DefRR.Reg);
829 }
830
831 if (Changed) {
832 if (Trace)
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000833 dbgs() << "Output: " << printReg(DefRR.Reg, &ME.TRI, DefRR.Sub)
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000834 << " cell: " << DefC << "\n";
835 ME.putCell(DefRR, DefC, Map);
836 visitUsesOf(DefRR.Reg);
837 }
838}
839
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000840void BT::visitNonBranch(const MachineInstr &MI) {
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000841 if (Trace)
842 dbgs() << "Visit MI(" << printMBBReference(*MI.getParent()) << "): " << MI;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000843 if (MI.isDebugValue())
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000844 return;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000845 assert(!MI.isBranch() && "Unexpected branch instruction");
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000846
847 CellMapType ResMap;
848 bool Eval = ME.evaluate(MI, Map, ResMap);
849
850 if (Trace && Eval) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000851 for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i) {
852 const MachineOperand &MO = MI.getOperand(i);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000853 if (!MO.isReg() || !MO.isUse())
854 continue;
855 RegisterRef RU(MO);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000856 dbgs() << " input reg: " << printReg(RU.Reg, &ME.TRI, RU.Sub)
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000857 << " cell: " << ME.getCell(RU, Map) << "\n";
858 }
859 dbgs() << "Outputs:\n";
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +0000860 for (const std::pair<unsigned, RegisterCell> &P : ResMap) {
861 RegisterRef RD(P.first);
Francis Visoiu Mistrih9d419d32017-11-28 12:42:37 +0000862 dbgs() << " " << printReg(P.first, &ME.TRI) << " cell: "
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000863 << ME.getCell(RD, ResMap) << "\n";
864 }
865 }
866
867 // Iterate over all definitions of the instruction, and update the
868 // cells accordingly.
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +0000869 for (const MachineOperand &MO : MI.operands()) {
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000870 // Visit register defs only.
871 if (!MO.isReg() || !MO.isDef())
872 continue;
873 RegisterRef RD(MO);
874 assert(RD.Sub == 0 && "Unexpected sub-register in definition");
875 if (!TargetRegisterInfo::isVirtualRegister(RD.Reg))
876 continue;
877
878 bool Changed = false;
Benjamin Kramer9a5d7882015-07-18 17:43:23 +0000879 if (!Eval || ResMap.count(RD.Reg) == 0) {
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000880 // Set to "ref" (aka "bottom").
881 uint16_t DefBW = ME.getRegBitWidth(RD);
882 RegisterCell RefC = RegisterCell::self(RD.Reg, DefBW);
883 if (RefC != ME.getCell(RD, Map)) {
884 ME.putCell(RD, RefC, Map);
885 Changed = true;
886 }
887 } else {
888 RegisterCell DefC = ME.getCell(RD, Map);
889 RegisterCell ResC = ME.getCell(RD, ResMap);
890 // This is a non-phi instruction, so the values of the inputs come
891 // from the same registers each time this instruction is evaluated.
892 // During the propagation, the values of the inputs can become lowered
893 // in the sense of the lattice operation, which may cause different
894 // results to be calculated in subsequent evaluations. This should
895 // not cause the bottoming of the result in the map, since the new
896 // result is already reflecting the lowered inputs.
897 for (uint16_t i = 0, w = DefC.width(); i < w; ++i) {
898 BitValue &V = DefC[i];
899 // Bits that are already "bottom" should not be updated.
900 if (V.Type == BitValue::Ref && V.RefI.Reg == RD.Reg)
901 continue;
902 // Same for those that are identical in DefC and ResC.
903 if (V == ResC[i])
904 continue;
905 V = ResC[i];
906 Changed = true;
907 }
908 if (Changed)
909 ME.putCell(RD, DefC, Map);
910 }
911 if (Changed)
912 visitUsesOf(RD.Reg);
913 }
914}
915
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000916void BT::visitBranchesFrom(const MachineInstr &BI) {
917 const MachineBasicBlock &B = *BI.getParent();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000918 MachineBasicBlock::const_iterator It = BI, End = B.end();
919 BranchTargetList Targets, BTs;
920 bool FallsThrough = true, DefaultToAll = false;
921 int ThisN = B.getNumber();
922
923 do {
924 BTs.clear();
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000925 const MachineInstr &MI = *It;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000926 if (Trace)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000927 dbgs() << "Visit BR(" << printMBBReference(B) << "): " << MI;
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +0000928 assert(MI.isBranch() && "Expecting branch instruction");
929 InstrExec.insert(&MI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000930 bool Eval = ME.evaluate(MI, Map, BTs, FallsThrough);
931 if (!Eval) {
932 // If the evaluation failed, we will add all targets. Keep going in
933 // the loop to mark all executable branches as such.
934 DefaultToAll = true;
935 FallsThrough = true;
936 if (Trace)
937 dbgs() << " failed to evaluate: will add all CFG successors\n";
938 } else if (!DefaultToAll) {
939 // If evaluated successfully add the targets to the cumulative list.
940 if (Trace) {
941 dbgs() << " adding targets:";
942 for (unsigned i = 0, n = BTs.size(); i < n; ++i)
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000943 dbgs() << " " << printMBBReference(*BTs[i]);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000944 if (FallsThrough)
945 dbgs() << "\n falls through\n";
946 else
947 dbgs() << "\n does not fall through\n";
948 }
949 Targets.insert(BTs.begin(), BTs.end());
950 }
951 ++It;
952 } while (FallsThrough && It != End);
953
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000954 if (!DefaultToAll) {
955 // Need to add all CFG successors that lead to EH landing pads.
956 // There won't be explicit branches to these blocks, but they must
957 // be processed.
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +0000958 for (const MachineBasicBlock *SB : B.successors()) {
Reid Kleckner0e288232015-08-27 23:27:47 +0000959 if (SB->isEHPad())
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000960 Targets.insert(SB);
961 }
962 if (FallsThrough) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +0000963 MachineFunction::const_iterator BIt = B.getIterator();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000964 MachineFunction::const_iterator Next = std::next(BIt);
965 if (Next != MF.end())
966 Targets.insert(&*Next);
967 }
968 } else {
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +0000969 for (const MachineBasicBlock *SB : B.successors())
970 Targets.insert(SB);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000971 }
972
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +0000973 for (const MachineBasicBlock *TB : Targets)
974 FlowQ.push(CFGEdge(ThisN, TB->getNumber()));
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000975}
976
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000977void BT::visitUsesOf(unsigned Reg) {
978 if (Trace)
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +0000979 dbgs() << "queuing uses of modified reg " << printReg(Reg, &ME.TRI)
980 << " cell: " << ME.getCell(Reg, Map) << '\n';
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000981
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +0000982 for (MachineInstr &UseI : MRI.use_nodbg_instructions(Reg))
983 UseQ.push(&UseI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000984}
985
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000986BT::RegisterCell BT::get(RegisterRef RR) const {
987 return ME.getCell(RR, Map);
988}
989
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000990void BT::put(RegisterRef RR, const RegisterCell &RC) {
991 ME.putCell(RR, RC, Map);
992}
993
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000994// Replace all references to bits from OldRR with the corresponding bits
995// in NewRR.
996void BT::subst(RegisterRef OldRR, RegisterRef NewRR) {
Benjamin Kramer9a5d7882015-07-18 17:43:23 +0000997 assert(Map.count(OldRR.Reg) > 0 && "OldRR not present in map");
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +0000998 BitMask OM = ME.mask(OldRR.Reg, OldRR.Sub);
999 BitMask NM = ME.mask(NewRR.Reg, NewRR.Sub);
1000 uint16_t OMB = OM.first(), OME = OM.last();
1001 uint16_t NMB = NM.first(), NME = NM.last();
Krzysztof Parzyszeka45971a2015-07-07 16:02:11 +00001002 (void)NME;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001003 assert((OME-OMB == NME-NMB) &&
1004 "Substituting registers of different lengths");
Krzysztof Parzyszek02893de2017-10-16 18:43:08 +00001005 for (std::pair<const unsigned, RegisterCell> &P : Map) {
1006 RegisterCell &RC = P.second;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001007 for (uint16_t i = 0, w = RC.width(); i < w; ++i) {
1008 BitValue &V = RC[i];
1009 if (V.Type != BitValue::Ref || V.RefI.Reg != OldRR.Reg)
1010 continue;
1011 if (V.RefI.Pos < OMB || V.RefI.Pos > OME)
1012 continue;
1013 V.RefI.Reg = NewRR.Reg;
1014 V.RefI.Pos += NMB-OMB;
1015 }
1016 }
1017}
1018
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001019// Check if the block has been "executed" during propagation. (If not, the
1020// block is dead, but it may still appear to be reachable.)
1021bool BT::reached(const MachineBasicBlock *B) const {
1022 int BN = B->getNumber();
1023 assert(BN >= 0);
Krzysztof Parzyszek5bfaf562017-04-19 15:08:31 +00001024 return ReachedBB.count(BN);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001025}
1026
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001027// Visit an individual instruction. This could be a newly added instruction,
1028// or one that has been modified by an optimization.
Krzysztof Parzyszek9273ecc2016-08-19 14:10:57 +00001029void BT::visit(const MachineInstr &MI) {
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001030 assert(!MI.isBranch() && "Only non-branches are allowed");
1031 InstrExec.insert(&MI);
1032 visitNonBranch(MI);
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +00001033 // Make sure to flush all the pending use updates.
1034 runUseQueue();
Krzysztof Parzyszekb558ae22016-09-13 14:36:55 +00001035 // The call to visitNonBranch could propagate the changes until a branch
1036 // is actually visited. This could result in adding CFG edges to the flow
1037 // queue. Since the queue won't be processed, clear it.
1038 while (!FlowQ.empty())
1039 FlowQ.pop();
Krzysztof Parzyszek6eba5b82016-07-26 19:08:45 +00001040}
1041
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001042void BT::reset() {
1043 EdgeExec.clear();
1044 InstrExec.clear();
1045 Map.clear();
Krzysztof Parzyszek5bfaf562017-04-19 15:08:31 +00001046 ReachedBB.clear();
1047 ReachedBB.reserve(MF.size());
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001048}
1049
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +00001050void BT::runEdgeQueue(BitVector &BlockScanned) {
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001051 while (!FlowQ.empty()) {
1052 CFGEdge Edge = FlowQ.front();
1053 FlowQ.pop();
1054
1055 if (EdgeExec.count(Edge))
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +00001056 return;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001057 EdgeExec.insert(Edge);
Krzysztof Parzyszek5bfaf562017-04-19 15:08:31 +00001058 ReachedBB.insert(Edge.second);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001059
1060 const MachineBasicBlock &B = *MF.getBlockNumbered(Edge.second);
1061 MachineBasicBlock::const_iterator It = B.begin(), End = B.end();
1062 // Visit PHI nodes first.
1063 while (It != End && It->isPHI()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001064 const MachineInstr &PI = *It++;
1065 InstrExec.insert(&PI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001066 visitPHI(PI);
1067 }
1068
1069 // If this block has already been visited through a flow graph edge,
1070 // then the instructions have already been processed. Any updates to
1071 // the cells would now only happen through visitUsesOf...
1072 if (BlockScanned[Edge.second])
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +00001073 return;
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001074 BlockScanned[Edge.second] = true;
1075
1076 // Visit non-branch instructions.
1077 while (It != End && !It->isBranch()) {
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001078 const MachineInstr &MI = *It++;
1079 InstrExec.insert(&MI);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001080 visitNonBranch(MI);
1081 }
1082 // If block end has been reached, add the fall-through edge to the queue.
1083 if (It == End) {
Duncan P. N. Exon Smitha72c6e22015-10-20 00:46:39 +00001084 MachineFunction::const_iterator BIt = B.getIterator();
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001085 MachineFunction::const_iterator Next = std::next(BIt);
Duncan P. N. Exon Smith83c4b682015-11-07 00:01:16 +00001086 if (Next != MF.end() && B.isSuccessor(&*Next)) {
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001087 int ThisN = B.getNumber();
1088 int NextN = Next->getNumber();
1089 FlowQ.push(CFGEdge(ThisN, NextN));
1090 }
1091 } else {
1092 // Handle the remaining sequence of branches. This function will update
1093 // the work queue.
Duncan P. N. Exon Smith98226e32016-07-12 01:55:32 +00001094 visitBranchesFrom(*It);
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001095 }
1096 } // while (!FlowQ->empty())
Krzysztof Parzyszek058d3ce2017-12-15 21:34:05 +00001097}
1098
1099void BT::runUseQueue() {
1100 while (!UseQ.empty()) {
1101 MachineInstr &UseI = *UseQ.front();
1102 UseQ.pop();
1103
1104 if (!InstrExec.count(&UseI))
1105 continue;
1106 if (UseI.isPHI())
1107 visitPHI(UseI);
1108 else if (!UseI.isBranch())
1109 visitNonBranch(UseI);
1110 else
1111 visitBranchesFrom(UseI);
1112 }
1113}
1114
1115void BT::run() {
1116 reset();
1117 assert(FlowQ.empty());
1118
1119 using MachineFlowGraphTraits = GraphTraits<const MachineFunction*>;
1120 const MachineBasicBlock *Entry = MachineFlowGraphTraits::getEntryNode(&MF);
1121
1122 unsigned MaxBN = 0;
1123 for (const MachineBasicBlock &B : MF) {
1124 assert(B.getNumber() >= 0 && "Disconnected block");
1125 unsigned BN = B.getNumber();
1126 if (BN > MaxBN)
1127 MaxBN = BN;
1128 }
1129
1130 // Keep track of visited blocks.
1131 BitVector BlockScanned(MaxBN+1);
1132
1133 int EntryN = Entry->getNumber();
1134 // Generate a fake edge to get something to start with.
1135 FlowQ.push(CFGEdge(-1, EntryN));
1136
1137 while (!FlowQ.empty() || !UseQ.empty()) {
1138 runEdgeQueue(BlockScanned);
1139 runUseQueue();
1140 }
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001141
Krzysztof Parzyszek623afbd2016-08-03 18:13:32 +00001142 if (Trace)
1143 print_cells(dbgs() << "Cells after propagation:\n");
Krzysztof Parzyszeke53b31a2015-07-07 15:16:42 +00001144}