| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1 | //===- HexagonConstPropagation.cpp ----------------------------------------===// |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #define DEBUG_TYPE "hcp" |
| 10 | |
| 11 | #include "HexagonInstrInfo.h" |
| 12 | #include "HexagonRegisterInfo.h" |
| 13 | #include "HexagonSubtarget.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/APFloat.h" |
| 15 | #include "llvm/ADT/APInt.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/PostOrderIterator.h" |
| 17 | #include "llvm/ADT/SetVector.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstr.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineOperand.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 28 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Constants.h" |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 30 | #include "llvm/IR/Type.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
| 32 | #include "llvm/Support/Casting.h" |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/MathExtras.h" |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 38 | #include <cassert> |
| 39 | #include <cstdint> |
| 40 | #include <cstring> |
| 41 | #include <iterator> |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 42 | #include <map> |
| 43 | #include <queue> |
| 44 | #include <set> |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 45 | #include <utility> |
| 46 | #include <vector> |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 47 | |
| 48 | using namespace llvm; |
| 49 | |
| 50 | namespace { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 51 | |
| 52 | // Properties of a value that are tracked by the propagation. |
| 53 | // A property that is marked as present (i.e. bit is set) dentes that the |
| 54 | // value is known (proven) to have this property. Not all combinations |
| 55 | // of bits make sense, for example Zero and NonZero are mutually exclusive, |
| 56 | // but on the other hand, Zero implies Finite. In this case, whenever |
| 57 | // the Zero property is present, Finite should also be present. |
| 58 | class ConstantProperties { |
| 59 | public: |
| 60 | enum { |
| 61 | Unknown = 0x0000, |
| 62 | Zero = 0x0001, |
| 63 | NonZero = 0x0002, |
| 64 | Finite = 0x0004, |
| 65 | Infinity = 0x0008, |
| 66 | NaN = 0x0010, |
| 67 | SignedZero = 0x0020, |
| 68 | NumericProperties = (Zero|NonZero|Finite|Infinity|NaN|SignedZero), |
| 69 | PosOrZero = 0x0100, |
| 70 | NegOrZero = 0x0200, |
| 71 | SignProperties = (PosOrZero|NegOrZero), |
| 72 | Everything = (NumericProperties|SignProperties) |
| 73 | }; |
| 74 | |
| 75 | // For a given constant, deduce the set of trackable properties that this |
| 76 | // constant has. |
| 77 | static uint32_t deduce(const Constant *C); |
| 78 | }; |
| 79 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 80 | // A representation of a register as it can appear in a MachineOperand, |
| 81 | // i.e. a pair register:subregister. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 82 | |
| 83 | // FIXME: Use TargetInstrInfo::RegSubRegPair. Also duplicated in |
| 84 | // HexagonGenPredicate |
| 85 | struct RegisterSubReg { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 86 | unsigned Reg, SubReg; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 87 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 88 | explicit RegisterSubReg(unsigned R, unsigned SR = 0) : Reg(R), SubReg(SR) {} |
| 89 | explicit RegisterSubReg(const MachineOperand &MO) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 90 | : Reg(MO.getReg()), SubReg(MO.getSubReg()) {} |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 91 | |
| 92 | void print(const TargetRegisterInfo *TRI = nullptr) const { |
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 93 | dbgs() << printReg(Reg, TRI, SubReg); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 94 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 95 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 96 | bool operator== (const RegisterSubReg &R) const { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 97 | return (Reg == R.Reg) && (SubReg == R.SubReg); |
| 98 | } |
| 99 | }; |
| 100 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 101 | // Lattice cell, based on that was described in the W-Z paper on constant |
| 102 | // propagation. |
| 103 | // Latice cell will be allowed to hold multiple constant values. While |
| 104 | // multiple values would normally indicate "bottom", we can still derive |
| 105 | // some useful information from them. For example, comparison X > 0 |
| 106 | // could be folded if all the values in the cell associated with X are |
| 107 | // positive. |
| 108 | class LatticeCell { |
| 109 | private: |
| 110 | enum { Normal, Top, Bottom }; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 111 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 112 | static const unsigned MaxCellSize = 4; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 113 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 114 | unsigned Kind:2; |
| 115 | unsigned Size:3; |
| 116 | unsigned IsSpecial:1; |
| 117 | unsigned :0; |
| 118 | |
| 119 | public: |
| 120 | union { |
| 121 | uint32_t Properties; |
| 122 | const Constant *Value; |
| 123 | const Constant *Values[MaxCellSize]; |
| 124 | }; |
| 125 | |
| 126 | LatticeCell() : Kind(Top), Size(0), IsSpecial(false) { |
| 127 | for (unsigned i = 0; i < MaxCellSize; ++i) |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 128 | Values[i] = nullptr; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | bool meet(const LatticeCell &L); |
| 132 | bool add(const Constant *C); |
| 133 | bool add(uint32_t Property); |
| 134 | uint32_t properties() const; |
| 135 | unsigned size() const { return Size; } |
| 136 | |
| Dávid Bolvanský | 745b6de | 2019-11-23 23:08:22 +0100 | [diff] [blame] | 137 | LatticeCell(const LatticeCell &L) { |
| 138 | // This memcpy also copies Properties (when L.Size == 0). |
| 139 | uint32_t N = |
| 140 | L.IsSpecial ? sizeof L.Properties : L.Size * sizeof(const Constant *); |
| 141 | memcpy(Values, L.Values, N); |
| 142 | Kind = L.Kind; |
| 143 | Size = L.Size; |
| 144 | IsSpecial = L.IsSpecial; |
| 145 | } |
| 146 | |
| 147 | LatticeCell &operator=(const LatticeCell &L) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 148 | if (this != &L) { |
| 149 | // This memcpy also copies Properties (when L.Size == 0). |
| 150 | uint32_t N = L.IsSpecial ? sizeof L.Properties |
| Dávid Bolvanský | 745b6de | 2019-11-23 23:08:22 +0100 | [diff] [blame] | 151 | : L.Size * sizeof(const Constant *); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 152 | memcpy(Values, L.Values, N); |
| 153 | Kind = L.Kind; |
| 154 | Size = L.Size; |
| 155 | IsSpecial = L.IsSpecial; |
| 156 | } |
| 157 | return *this; |
| 158 | } |
| 159 | |
| 160 | bool isSingle() const { return size() == 1; } |
| 161 | bool isProperty() const { return IsSpecial; } |
| 162 | bool isTop() const { return Kind == Top; } |
| 163 | bool isBottom() const { return Kind == Bottom; } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 164 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 165 | bool setBottom() { |
| 166 | bool Changed = (Kind != Bottom); |
| 167 | Kind = Bottom; |
| 168 | Size = 0; |
| 169 | IsSpecial = false; |
| 170 | return Changed; |
| 171 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 172 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 173 | void print(raw_ostream &os) const; |
| 174 | |
| 175 | private: |
| 176 | void setProperty() { |
| 177 | IsSpecial = true; |
| 178 | Size = 0; |
| 179 | Kind = Normal; |
| 180 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 181 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 182 | bool convertToProperty(); |
| 183 | }; |
| 184 | |
| Florian Hahn | a3ad61d | 2017-07-31 16:44:28 +0000 | [diff] [blame] | 185 | #ifndef NDEBUG |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 186 | raw_ostream &operator<< (raw_ostream &os, const LatticeCell &L) { |
| 187 | L.print(os); |
| 188 | return os; |
| 189 | } |
| Florian Hahn | a3ad61d | 2017-07-31 16:44:28 +0000 | [diff] [blame] | 190 | #endif |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 191 | |
| 192 | class MachineConstEvaluator; |
| 193 | |
| 194 | class MachineConstPropagator { |
| 195 | public: |
| 196 | MachineConstPropagator(MachineConstEvaluator &E) : MCE(E) { |
| 197 | Bottom.setBottom(); |
| 198 | } |
| 199 | |
| 200 | // Mapping: vreg -> cell |
| 201 | // The keys are registers _without_ subregisters. This won't allow |
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 202 | // definitions in the form of "vreg:subreg = ...". Such definitions |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 203 | // would be questionable from the point of view of SSA, since the "vreg" |
| 204 | // could not be initialized in its entirety (specifically, an instruction |
| 205 | // defining the "other part" of "vreg" would also count as a definition |
| 206 | // of "vreg", which would violate the SSA). |
| 207 | // If a value of a pair vreg:subreg needs to be obtained, the cell for |
| 208 | // "vreg" needs to be looked up, and then the value of subregister "subreg" |
| 209 | // needs to be evaluated. |
| 210 | class CellMap { |
| 211 | public: |
| 212 | CellMap() { |
| 213 | assert(Top.isTop()); |
| 214 | Bottom.setBottom(); |
| 215 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 216 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 217 | void clear() { Map.clear(); } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 218 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 219 | bool has(unsigned R) const { |
| 220 | // All non-virtual registers are considered "bottom". |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 221 | if (!Register::isVirtualRegister(R)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 222 | return true; |
| 223 | MapType::const_iterator F = Map.find(R); |
| 224 | return F != Map.end(); |
| 225 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 226 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 227 | const LatticeCell &get(unsigned R) const { |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 228 | if (!Register::isVirtualRegister(R)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 229 | return Bottom; |
| 230 | MapType::const_iterator F = Map.find(R); |
| 231 | if (F != Map.end()) |
| 232 | return F->second; |
| 233 | return Top; |
| 234 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 235 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 236 | // Invalidates any const references. |
| 237 | void update(unsigned R, const LatticeCell &L) { |
| 238 | Map[R] = L; |
| 239 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 240 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 241 | void print(raw_ostream &os, const TargetRegisterInfo &TRI) const; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 242 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 243 | private: |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 244 | using MapType = std::map<unsigned, LatticeCell>; |
| 245 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 246 | MapType Map; |
| 247 | // To avoid creating "top" entries, return a const reference to |
| 248 | // this cell in "get". Also, have a "Bottom" cell to return from |
| 249 | // get when a value of a physical register is requested. |
| 250 | LatticeCell Top, Bottom; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 251 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 252 | public: |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 253 | using const_iterator = MapType::const_iterator; |
| 254 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 255 | const_iterator begin() const { return Map.begin(); } |
| 256 | const_iterator end() const { return Map.end(); } |
| 257 | }; |
| 258 | |
| 259 | bool run(MachineFunction &MF); |
| 260 | |
| 261 | private: |
| 262 | void visitPHI(const MachineInstr &PN); |
| 263 | void visitNonBranch(const MachineInstr &MI); |
| 264 | void visitBranchesFrom(const MachineInstr &BrI); |
| 265 | void visitUsesOf(unsigned R); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 266 | bool computeBlockSuccessors(const MachineBasicBlock *MB, |
| 267 | SetVector<const MachineBasicBlock*> &Targets); |
| 268 | void removeCFGEdge(MachineBasicBlock *From, MachineBasicBlock *To); |
| 269 | |
| 270 | void propagate(MachineFunction &MF); |
| 271 | bool rewrite(MachineFunction &MF); |
| 272 | |
| Simon Pilgrim | 6039e54 | 2019-11-14 13:47:22 +0000 | [diff] [blame] | 273 | MachineRegisterInfo *MRI = nullptr; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 274 | MachineConstEvaluator &MCE; |
| 275 | |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 276 | using CFGEdge = std::pair<unsigned, unsigned>; |
| 277 | using SetOfCFGEdge = std::set<CFGEdge>; |
| 278 | using SetOfInstr = std::set<const MachineInstr *>; |
| 279 | using QueueOfCFGEdge = std::queue<CFGEdge>; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 280 | |
| 281 | LatticeCell Bottom; |
| 282 | CellMap Cells; |
| 283 | SetOfCFGEdge EdgeExec; |
| 284 | SetOfInstr InstrExec; |
| 285 | QueueOfCFGEdge FlowQ; |
| 286 | }; |
| 287 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 288 | // The "evaluator/rewriter" of machine instructions. This is an abstract |
| 289 | // base class that provides the interface that the propagator will use, |
| 290 | // as well as some helper functions that are target-independent. |
| 291 | class MachineConstEvaluator { |
| 292 | public: |
| 293 | MachineConstEvaluator(MachineFunction &Fn) |
| 294 | : TRI(*Fn.getSubtarget().getRegisterInfo()), |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 295 | MF(Fn), CX(Fn.getFunction().getContext()) {} |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 296 | virtual ~MachineConstEvaluator() = default; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 297 | |
| 298 | // The required interface: |
| 299 | // - A set of three "evaluate" functions. Each returns "true" if the |
| 300 | // computation succeeded, "false" otherwise. |
| 301 | // (1) Given an instruction MI, and the map with input values "Inputs", |
| 302 | // compute the set of output values "Outputs". An example of when |
| 303 | // the computation can "fail" is if MI is not an instruction that |
| 304 | // is recognized by the evaluator. |
| 305 | // (2) Given a register R (as reg:subreg), compute the cell that |
| 306 | // corresponds to the "subreg" part of the given register. |
| 307 | // (3) Given a branch instruction BrI, compute the set of target blocks. |
| 308 | // If the branch can fall-through, add null (0) to the list of |
| 309 | // possible targets. |
| 310 | // - A function "rewrite", that given the cell map after propagation, |
| 311 | // could rewrite instruction MI in a more beneficial form. Return |
| 312 | // "true" if a change has been made, "false" otherwise. |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 313 | using CellMap = MachineConstPropagator::CellMap; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 314 | virtual bool evaluate(const MachineInstr &MI, const CellMap &Inputs, |
| 315 | CellMap &Outputs) = 0; |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 316 | virtual bool evaluate(const RegisterSubReg &R, const LatticeCell &SrcC, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 317 | LatticeCell &Result) = 0; |
| 318 | virtual bool evaluate(const MachineInstr &BrI, const CellMap &Inputs, |
| 319 | SetVector<const MachineBasicBlock*> &Targets, |
| 320 | bool &CanFallThru) = 0; |
| 321 | virtual bool rewrite(MachineInstr &MI, const CellMap &Inputs) = 0; |
| 322 | |
| 323 | const TargetRegisterInfo &TRI; |
| 324 | |
| 325 | protected: |
| 326 | MachineFunction &MF; |
| 327 | LLVMContext &CX; |
| 328 | |
| 329 | struct Comparison { |
| 330 | enum { |
| 331 | Unk = 0x00, |
| 332 | EQ = 0x01, |
| 333 | NE = 0x02, |
| 334 | L = 0x04, // Less-than property. |
| 335 | G = 0x08, // Greater-than property. |
| 336 | U = 0x40, // Unsigned property. |
| 337 | LTs = L, |
| 338 | LEs = L | EQ, |
| 339 | GTs = G, |
| 340 | GEs = G | EQ, |
| 341 | LTu = L | U, |
| 342 | LEu = L | EQ | U, |
| 343 | GTu = G | U, |
| 344 | GEu = G | EQ | U |
| 345 | }; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 346 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 347 | static uint32_t negate(uint32_t Cmp) { |
| 348 | if (Cmp == EQ) |
| 349 | return NE; |
| 350 | if (Cmp == NE) |
| 351 | return EQ; |
| 352 | assert((Cmp & (L|G)) != (L|G)); |
| 353 | return Cmp ^ (L|G); |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | // Helper functions. |
| 358 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 359 | bool getCell(const RegisterSubReg &R, const CellMap &Inputs, LatticeCell &RC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 360 | bool constToInt(const Constant *C, APInt &Val) const; |
| 361 | bool constToFloat(const Constant *C, APFloat &Val) const; |
| 362 | const ConstantInt *intToConst(const APInt &Val) const; |
| 363 | |
| 364 | // Compares. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 365 | bool evaluateCMPrr(uint32_t Cmp, const RegisterSubReg &R1, const RegisterSubReg &R2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 366 | const CellMap &Inputs, bool &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 367 | bool evaluateCMPri(uint32_t Cmp, const RegisterSubReg &R1, const APInt &A2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 368 | const CellMap &Inputs, bool &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 369 | bool evaluateCMPrp(uint32_t Cmp, const RegisterSubReg &R1, uint64_t Props2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 370 | const CellMap &Inputs, bool &Result); |
| 371 | bool evaluateCMPii(uint32_t Cmp, const APInt &A1, const APInt &A2, |
| 372 | bool &Result); |
| 373 | bool evaluateCMPpi(uint32_t Cmp, uint32_t Props, const APInt &A2, |
| 374 | bool &Result); |
| 375 | bool evaluateCMPpp(uint32_t Cmp, uint32_t Props1, uint32_t Props2, |
| 376 | bool &Result); |
| 377 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 378 | bool evaluateCOPY(const RegisterSubReg &R1, const CellMap &Inputs, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 379 | LatticeCell &Result); |
| 380 | |
| 381 | // Logical operations. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 382 | bool evaluateANDrr(const RegisterSubReg &R1, const RegisterSubReg &R2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 383 | const CellMap &Inputs, LatticeCell &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 384 | bool evaluateANDri(const RegisterSubReg &R1, const APInt &A2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 385 | const CellMap &Inputs, LatticeCell &Result); |
| 386 | bool evaluateANDii(const APInt &A1, const APInt &A2, APInt &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 387 | bool evaluateORrr(const RegisterSubReg &R1, const RegisterSubReg &R2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 388 | const CellMap &Inputs, LatticeCell &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 389 | bool evaluateORri(const RegisterSubReg &R1, const APInt &A2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 390 | const CellMap &Inputs, LatticeCell &Result); |
| 391 | bool evaluateORii(const APInt &A1, const APInt &A2, APInt &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 392 | bool evaluateXORrr(const RegisterSubReg &R1, const RegisterSubReg &R2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 393 | const CellMap &Inputs, LatticeCell &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 394 | bool evaluateXORri(const RegisterSubReg &R1, const APInt &A2, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 395 | const CellMap &Inputs, LatticeCell &Result); |
| 396 | bool evaluateXORii(const APInt &A1, const APInt &A2, APInt &Result); |
| 397 | |
| 398 | // Extensions. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 399 | bool evaluateZEXTr(const RegisterSubReg &R1, unsigned Width, unsigned Bits, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 400 | const CellMap &Inputs, LatticeCell &Result); |
| 401 | bool evaluateZEXTi(const APInt &A1, unsigned Width, unsigned Bits, |
| 402 | APInt &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 403 | bool evaluateSEXTr(const RegisterSubReg &R1, unsigned Width, unsigned Bits, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 404 | const CellMap &Inputs, LatticeCell &Result); |
| 405 | bool evaluateSEXTi(const APInt &A1, unsigned Width, unsigned Bits, |
| 406 | APInt &Result); |
| 407 | |
| 408 | // Leading/trailing bits. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 409 | bool evaluateCLBr(const RegisterSubReg &R1, bool Zeros, bool Ones, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 410 | const CellMap &Inputs, LatticeCell &Result); |
| 411 | bool evaluateCLBi(const APInt &A1, bool Zeros, bool Ones, APInt &Result); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 412 | bool evaluateCTBr(const RegisterSubReg &R1, bool Zeros, bool Ones, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 413 | const CellMap &Inputs, LatticeCell &Result); |
| 414 | bool evaluateCTBi(const APInt &A1, bool Zeros, bool Ones, APInt &Result); |
| 415 | |
| 416 | // Bitfield extract. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 417 | bool evaluateEXTRACTr(const RegisterSubReg &R1, unsigned Width, unsigned Bits, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 418 | unsigned Offset, bool Signed, const CellMap &Inputs, |
| 419 | LatticeCell &Result); |
| 420 | bool evaluateEXTRACTi(const APInt &A1, unsigned Bits, unsigned Offset, |
| 421 | bool Signed, APInt &Result); |
| 422 | // Vector operations. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 423 | bool evaluateSplatr(const RegisterSubReg &R1, unsigned Bits, unsigned Count, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 424 | const CellMap &Inputs, LatticeCell &Result); |
| 425 | bool evaluateSplati(const APInt &A1, unsigned Bits, unsigned Count, |
| 426 | APInt &Result); |
| 427 | }; |
| 428 | |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 429 | } // end anonymous namespace |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 430 | |
| 431 | uint32_t ConstantProperties::deduce(const Constant *C) { |
| 432 | if (isa<ConstantInt>(C)) { |
| 433 | const ConstantInt *CI = cast<ConstantInt>(C); |
| 434 | if (CI->isZero()) |
| 435 | return Zero | PosOrZero | NegOrZero | Finite; |
| 436 | uint32_t Props = (NonZero | Finite); |
| 437 | if (CI->isNegative()) |
| 438 | return Props | NegOrZero; |
| 439 | return Props | PosOrZero; |
| 440 | } |
| 441 | |
| 442 | if (isa<ConstantFP>(C)) { |
| 443 | const ConstantFP *CF = cast<ConstantFP>(C); |
| 444 | uint32_t Props = CF->isNegative() ? (NegOrZero|NonZero) |
| 445 | : PosOrZero; |
| 446 | if (CF->isZero()) |
| 447 | return (Props & ~NumericProperties) | (Zero|Finite); |
| 448 | Props = (Props & ~NumericProperties) | NonZero; |
| 449 | if (CF->isNaN()) |
| 450 | return (Props & ~NumericProperties) | NaN; |
| 451 | const APFloat &Val = CF->getValueAPF(); |
| 452 | if (Val.isInfinity()) |
| 453 | return (Props & ~NumericProperties) | Infinity; |
| 454 | Props |= Finite; |
| 455 | return Props; |
| 456 | } |
| 457 | |
| 458 | return Unknown; |
| 459 | } |
| 460 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 461 | // Convert a cell from a set of specific values to a cell that tracks |
| 462 | // properties. |
| 463 | bool LatticeCell::convertToProperty() { |
| 464 | if (isProperty()) |
| 465 | return false; |
| 466 | // Corner case: converting a fresh (top) cell to "special". |
| 467 | // This can happen, when adding a property to a top cell. |
| 468 | uint32_t Everything = ConstantProperties::Everything; |
| 469 | uint32_t Ps = !isTop() ? properties() |
| 470 | : Everything; |
| 471 | if (Ps != ConstantProperties::Unknown) { |
| 472 | Properties = Ps; |
| 473 | setProperty(); |
| 474 | } else { |
| 475 | setBottom(); |
| 476 | } |
| 477 | return true; |
| 478 | } |
| 479 | |
| Florian Hahn | a3ad61d | 2017-07-31 16:44:28 +0000 | [diff] [blame] | 480 | #ifndef NDEBUG |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 481 | void LatticeCell::print(raw_ostream &os) const { |
| 482 | if (isProperty()) { |
| 483 | os << "{ "; |
| 484 | uint32_t Ps = properties(); |
| 485 | if (Ps & ConstantProperties::Zero) |
| 486 | os << "zero "; |
| 487 | if (Ps & ConstantProperties::NonZero) |
| 488 | os << "nonzero "; |
| 489 | if (Ps & ConstantProperties::Finite) |
| 490 | os << "finite "; |
| 491 | if (Ps & ConstantProperties::Infinity) |
| 492 | os << "infinity "; |
| 493 | if (Ps & ConstantProperties::NaN) |
| 494 | os << "nan "; |
| 495 | if (Ps & ConstantProperties::PosOrZero) |
| 496 | os << "poz "; |
| 497 | if (Ps & ConstantProperties::NegOrZero) |
| 498 | os << "nez "; |
| 499 | os << '}'; |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | os << "{ "; |
| 504 | if (isBottom()) { |
| 505 | os << "bottom"; |
| 506 | } else if (isTop()) { |
| 507 | os << "top"; |
| 508 | } else { |
| 509 | for (unsigned i = 0; i < size(); ++i) { |
| 510 | const Constant *C = Values[i]; |
| 511 | if (i != 0) |
| 512 | os << ", "; |
| 513 | C->print(os); |
| 514 | } |
| 515 | } |
| 516 | os << " }"; |
| 517 | } |
| Florian Hahn | a3ad61d | 2017-07-31 16:44:28 +0000 | [diff] [blame] | 518 | #endif |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 519 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 520 | // "Meet" operation on two cells. This is the key of the propagation |
| 521 | // algorithm. |
| 522 | bool LatticeCell::meet(const LatticeCell &L) { |
| 523 | bool Changed = false; |
| 524 | if (L.isBottom()) |
| 525 | Changed = setBottom(); |
| 526 | if (isBottom() || L.isTop()) |
| 527 | return Changed; |
| 528 | if (isTop()) { |
| 529 | *this = L; |
| 530 | // L can be neither Top nor Bottom, so *this must have changed. |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | // Top/bottom cases covered. Need to integrate L's set into ours. |
| 535 | if (L.isProperty()) |
| 536 | return add(L.properties()); |
| 537 | for (unsigned i = 0; i < L.size(); ++i) { |
| 538 | const Constant *LC = L.Values[i]; |
| 539 | Changed |= add(LC); |
| 540 | } |
| 541 | return Changed; |
| 542 | } |
| 543 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 544 | // Add a new constant to the cell. This is actually where the cell update |
| 545 | // happens. If a cell has room for more constants, the new constant is added. |
| 546 | // Otherwise, the cell is converted to a "property" cell (i.e. a cell that |
| 547 | // will track properties of the associated values, and not the values |
| 548 | // themselves. Care is taken to handle special cases, like "bottom", etc. |
| 549 | bool LatticeCell::add(const Constant *LC) { |
| 550 | assert(LC); |
| 551 | if (isBottom()) |
| 552 | return false; |
| 553 | |
| 554 | if (!isProperty()) { |
| 555 | // Cell is not special. Try to add the constant here first, |
| 556 | // if there is room. |
| 557 | unsigned Index = 0; |
| 558 | while (Index < Size) { |
| 559 | const Constant *C = Values[Index]; |
| 560 | // If the constant is already here, no change is needed. |
| 561 | if (C == LC) |
| 562 | return false; |
| 563 | Index++; |
| 564 | } |
| 565 | if (Index < MaxCellSize) { |
| 566 | Values[Index] = LC; |
| 567 | Kind = Normal; |
| 568 | Size++; |
| 569 | return true; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | bool Changed = false; |
| 574 | |
| 575 | // This cell is special, or is not special, but is full. After this |
| 576 | // it will be special. |
| 577 | Changed = convertToProperty(); |
| 578 | uint32_t Ps = properties(); |
| 579 | uint32_t NewPs = Ps & ConstantProperties::deduce(LC); |
| 580 | if (NewPs == ConstantProperties::Unknown) { |
| 581 | setBottom(); |
| 582 | return true; |
| 583 | } |
| 584 | if (Ps != NewPs) { |
| 585 | Properties = NewPs; |
| 586 | Changed = true; |
| 587 | } |
| 588 | return Changed; |
| 589 | } |
| 590 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 591 | // Add a property to the cell. This will force the cell to become a property- |
| 592 | // tracking cell. |
| 593 | bool LatticeCell::add(uint32_t Property) { |
| 594 | bool Changed = convertToProperty(); |
| 595 | uint32_t Ps = properties(); |
| 596 | if (Ps == (Ps & Property)) |
| 597 | return Changed; |
| 598 | Properties = Property & Ps; |
| 599 | return true; |
| 600 | } |
| 601 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 602 | // Return the properties of the values in the cell. This is valid for any |
| 603 | // cell, and does not alter the cell itself. |
| 604 | uint32_t LatticeCell::properties() const { |
| 605 | if (isProperty()) |
| 606 | return Properties; |
| 607 | assert(!isTop() && "Should not call this for a top cell"); |
| 608 | if (isBottom()) |
| 609 | return ConstantProperties::Unknown; |
| 610 | |
| 611 | assert(size() > 0 && "Empty cell"); |
| 612 | uint32_t Ps = ConstantProperties::deduce(Values[0]); |
| 613 | for (unsigned i = 1; i < size(); ++i) { |
| 614 | if (Ps == ConstantProperties::Unknown) |
| 615 | break; |
| 616 | Ps &= ConstantProperties::deduce(Values[i]); |
| 617 | } |
| 618 | return Ps; |
| 619 | } |
| 620 | |
| Florian Hahn | 6b3216a | 2017-07-31 10:07:49 +0000 | [diff] [blame] | 621 | #ifndef NDEBUG |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 622 | void MachineConstPropagator::CellMap::print(raw_ostream &os, |
| 623 | const TargetRegisterInfo &TRI) const { |
| 624 | for (auto &I : Map) |
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 625 | dbgs() << " " << printReg(I.first, &TRI) << " -> " << I.second << '\n'; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 626 | } |
| Florian Hahn | 6b3216a | 2017-07-31 10:07:49 +0000 | [diff] [blame] | 627 | #endif |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 628 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 629 | void MachineConstPropagator::visitPHI(const MachineInstr &PN) { |
| 630 | const MachineBasicBlock *MB = PN.getParent(); |
| 631 | unsigned MBN = MB->getNumber(); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 632 | LLVM_DEBUG(dbgs() << "Visiting FI(" << printMBBReference(*MB) << "): " << PN); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 633 | |
| 634 | const MachineOperand &MD = PN.getOperand(0); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 635 | RegisterSubReg DefR(MD); |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 636 | assert(Register::isVirtualRegister(DefR.Reg)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 637 | |
| 638 | bool Changed = false; |
| 639 | |
| 640 | // If the def has a sub-register, set the corresponding cell to "bottom". |
| 641 | if (DefR.SubReg) { |
| 642 | Bottomize: |
| 643 | const LatticeCell &T = Cells.get(DefR.Reg); |
| 644 | Changed = !T.isBottom(); |
| 645 | Cells.update(DefR.Reg, Bottom); |
| 646 | if (Changed) |
| 647 | visitUsesOf(DefR.Reg); |
| 648 | return; |
| 649 | } |
| 650 | |
| 651 | LatticeCell DefC = Cells.get(DefR.Reg); |
| 652 | |
| 653 | for (unsigned i = 1, n = PN.getNumOperands(); i < n; i += 2) { |
| 654 | const MachineBasicBlock *PB = PN.getOperand(i+1).getMBB(); |
| 655 | unsigned PBN = PB->getNumber(); |
| 656 | if (!EdgeExec.count(CFGEdge(PBN, MBN))) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 657 | LLVM_DEBUG(dbgs() << " edge " << printMBBReference(*PB) << "->" |
| 658 | << printMBBReference(*MB) << " not executable\n"); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 659 | continue; |
| 660 | } |
| 661 | const MachineOperand &SO = PN.getOperand(i); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 662 | RegisterSubReg UseR(SO); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 663 | // If the input is not a virtual register, we don't really know what |
| 664 | // value it holds. |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 665 | if (!Register::isVirtualRegister(UseR.Reg)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 666 | goto Bottomize; |
| 667 | // If there is no cell for an input register, it means top. |
| 668 | if (!Cells.has(UseR.Reg)) |
| 669 | continue; |
| 670 | |
| 671 | LatticeCell SrcC; |
| 672 | bool Eval = MCE.evaluate(UseR, Cells.get(UseR.Reg), SrcC); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 673 | LLVM_DEBUG(dbgs() << " edge from " << printMBBReference(*PB) << ": " |
| 674 | << printReg(UseR.Reg, &MCE.TRI, UseR.SubReg) << SrcC |
| 675 | << '\n'); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 676 | Changed |= Eval ? DefC.meet(SrcC) |
| 677 | : DefC.setBottom(); |
| 678 | Cells.update(DefR.Reg, DefC); |
| 679 | if (DefC.isBottom()) |
| 680 | break; |
| 681 | } |
| 682 | if (Changed) |
| 683 | visitUsesOf(DefR.Reg); |
| 684 | } |
| 685 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 686 | void MachineConstPropagator::visitNonBranch(const MachineInstr &MI) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 687 | LLVM_DEBUG(dbgs() << "Visiting MI(" << printMBBReference(*MI.getParent()) |
| 688 | << "): " << MI); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 689 | CellMap Outputs; |
| 690 | bool Eval = MCE.evaluate(MI, Cells, Outputs); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 691 | LLVM_DEBUG({ |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 692 | if (Eval) { |
| 693 | dbgs() << " outputs:"; |
| 694 | for (auto &I : Outputs) |
| 695 | dbgs() << ' ' << I.second; |
| 696 | dbgs() << '\n'; |
| 697 | } |
| 698 | }); |
| 699 | |
| 700 | // Update outputs. If the value was not computed, set all the |
| 701 | // def cells to bottom. |
| 702 | for (const MachineOperand &MO : MI.operands()) { |
| 703 | if (!MO.isReg() || !MO.isDef()) |
| 704 | continue; |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 705 | RegisterSubReg DefR(MO); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 706 | // Only track virtual registers. |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 707 | if (!Register::isVirtualRegister(DefR.Reg)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 708 | continue; |
| 709 | bool Changed = false; |
| 710 | // If the evaluation failed, set cells for all output registers to bottom. |
| 711 | if (!Eval) { |
| 712 | const LatticeCell &T = Cells.get(DefR.Reg); |
| 713 | Changed = !T.isBottom(); |
| 714 | Cells.update(DefR.Reg, Bottom); |
| 715 | } else { |
| 716 | // Find the corresponding cell in the computed outputs. |
| 717 | // If it's not there, go on to the next def. |
| 718 | if (!Outputs.has(DefR.Reg)) |
| 719 | continue; |
| 720 | LatticeCell RC = Cells.get(DefR.Reg); |
| 721 | Changed = RC.meet(Outputs.get(DefR.Reg)); |
| 722 | Cells.update(DefR.Reg, RC); |
| 723 | } |
| 724 | if (Changed) |
| 725 | visitUsesOf(DefR.Reg); |
| 726 | } |
| 727 | } |
| 728 | |
| Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 729 | // Starting at a given branch, visit remaining branches in the block. |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 730 | // Traverse over the subsequent branches for as long as the preceding one |
| 731 | // can fall through. Add all the possible targets to the flow work queue, |
| 732 | // including the potential fall-through to the layout-successor block. |
| 733 | void MachineConstPropagator::visitBranchesFrom(const MachineInstr &BrI) { |
| 734 | const MachineBasicBlock &B = *BrI.getParent(); |
| 735 | unsigned MBN = B.getNumber(); |
| 736 | MachineBasicBlock::const_iterator It = BrI.getIterator(); |
| 737 | MachineBasicBlock::const_iterator End = B.end(); |
| 738 | |
| 739 | SetVector<const MachineBasicBlock*> Targets; |
| 740 | bool EvalOk = true, FallsThru = true; |
| 741 | while (It != End) { |
| 742 | const MachineInstr &MI = *It; |
| 743 | InstrExec.insert(&MI); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 744 | LLVM_DEBUG(dbgs() << "Visiting " << (EvalOk ? "BR" : "br") << "(" |
| 745 | << printMBBReference(B) << "): " << MI); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 746 | // Do not evaluate subsequent branches if the evaluation of any of the |
| 747 | // previous branches failed. Keep iterating over the branches only |
| 748 | // to mark them as executable. |
| 749 | EvalOk = EvalOk && MCE.evaluate(MI, Cells, Targets, FallsThru); |
| 750 | if (!EvalOk) |
| 751 | FallsThru = true; |
| 752 | if (!FallsThru) |
| 753 | break; |
| 754 | ++It; |
| 755 | } |
| 756 | |
| 757 | if (EvalOk) { |
| 758 | // Need to add all CFG successors that lead to EH landing pads. |
| 759 | // There won't be explicit branches to these blocks, but they must |
| 760 | // be processed. |
| 761 | for (const MachineBasicBlock *SB : B.successors()) { |
| 762 | if (SB->isEHPad()) |
| 763 | Targets.insert(SB); |
| 764 | } |
| 765 | if (FallsThru) { |
| 766 | const MachineFunction &MF = *B.getParent(); |
| 767 | MachineFunction::const_iterator BI = B.getIterator(); |
| 768 | MachineFunction::const_iterator Next = std::next(BI); |
| 769 | if (Next != MF.end()) |
| 770 | Targets.insert(&*Next); |
| 771 | } |
| 772 | } else { |
| 773 | // If the evaluation of the branches failed, make "Targets" to be the |
| 774 | // set of all successors of the block from the CFG. |
| 775 | // If the evaluation succeeded for all visited branches, then if the |
| 776 | // last one set "FallsThru", then add an edge to the layout successor |
| 777 | // to the targets. |
| 778 | Targets.clear(); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 779 | LLVM_DEBUG(dbgs() << " failed to evaluate a branch...adding all CFG " |
| 780 | "successors\n"); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 781 | for (const MachineBasicBlock *SB : B.successors()) |
| 782 | Targets.insert(SB); |
| 783 | } |
| 784 | |
| 785 | for (const MachineBasicBlock *TB : Targets) { |
| 786 | unsigned TBN = TB->getNumber(); |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 787 | LLVM_DEBUG(dbgs() << " pushing edge " << printMBBReference(B) << " -> " |
| 788 | << printMBBReference(*TB) << "\n"); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 789 | FlowQ.push(CFGEdge(MBN, TBN)); |
| 790 | } |
| 791 | } |
| 792 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 793 | void MachineConstPropagator::visitUsesOf(unsigned Reg) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 794 | LLVM_DEBUG(dbgs() << "Visiting uses of " << printReg(Reg, &MCE.TRI) |
| 795 | << Cells.get(Reg) << '\n'); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 796 | for (MachineInstr &MI : MRI->use_nodbg_instructions(Reg)) { |
| 797 | // Do not process non-executable instructions. They can become exceutable |
| 798 | // later (via a flow-edge in the work queue). In such case, the instruc- |
| 799 | // tion will be visited at that time. |
| 800 | if (!InstrExec.count(&MI)) |
| 801 | continue; |
| 802 | if (MI.isPHI()) |
| 803 | visitPHI(MI); |
| 804 | else if (!MI.isBranch()) |
| 805 | visitNonBranch(MI); |
| 806 | else |
| 807 | visitBranchesFrom(MI); |
| 808 | } |
| 809 | } |
| 810 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 811 | bool MachineConstPropagator::computeBlockSuccessors(const MachineBasicBlock *MB, |
| 812 | SetVector<const MachineBasicBlock*> &Targets) { |
| 813 | MachineBasicBlock::const_iterator FirstBr = MB->end(); |
| 814 | for (const MachineInstr &MI : *MB) { |
| Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 815 | if (MI.isDebugInstr()) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 816 | continue; |
| 817 | if (MI.isBranch()) { |
| 818 | FirstBr = MI.getIterator(); |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | Targets.clear(); |
| 824 | MachineBasicBlock::const_iterator End = MB->end(); |
| 825 | |
| 826 | bool DoNext = true; |
| 827 | for (MachineBasicBlock::const_iterator I = FirstBr; I != End; ++I) { |
| 828 | const MachineInstr &MI = *I; |
| 829 | // Can there be debug instructions between branches? |
| Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 830 | if (MI.isDebugInstr()) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 831 | continue; |
| 832 | if (!InstrExec.count(&MI)) |
| 833 | continue; |
| 834 | bool Eval = MCE.evaluate(MI, Cells, Targets, DoNext); |
| 835 | if (!Eval) |
| 836 | return false; |
| 837 | if (!DoNext) |
| 838 | break; |
| 839 | } |
| 840 | // If the last branch could fall-through, add block's layout successor. |
| 841 | if (DoNext) { |
| 842 | MachineFunction::const_iterator BI = MB->getIterator(); |
| 843 | MachineFunction::const_iterator NextI = std::next(BI); |
| 844 | if (NextI != MB->getParent()->end()) |
| 845 | Targets.insert(&*NextI); |
| 846 | } |
| 847 | |
| 848 | // Add all the EH landing pads. |
| 849 | for (const MachineBasicBlock *SB : MB->successors()) |
| 850 | if (SB->isEHPad()) |
| 851 | Targets.insert(SB); |
| 852 | |
| 853 | return true; |
| 854 | } |
| 855 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 856 | void MachineConstPropagator::removeCFGEdge(MachineBasicBlock *From, |
| 857 | MachineBasicBlock *To) { |
| 858 | // First, remove the CFG successor/predecessor information. |
| 859 | From->removeSuccessor(To); |
| 860 | // Remove all corresponding PHI operands in the To block. |
| 861 | for (auto I = To->begin(), E = To->getFirstNonPHI(); I != E; ++I) { |
| 862 | MachineInstr *PN = &*I; |
| 863 | // reg0 = PHI reg1, bb2, reg3, bb4, ... |
| 864 | int N = PN->getNumOperands()-2; |
| 865 | while (N > 0) { |
| 866 | if (PN->getOperand(N+1).getMBB() == From) { |
| 867 | PN->RemoveOperand(N+1); |
| 868 | PN->RemoveOperand(N); |
| 869 | } |
| 870 | N -= 2; |
| 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 875 | void MachineConstPropagator::propagate(MachineFunction &MF) { |
| 876 | MachineBasicBlock *Entry = GraphTraits<MachineFunction*>::getEntryNode(&MF); |
| 877 | unsigned EntryNum = Entry->getNumber(); |
| 878 | |
| 879 | // Start with a fake edge, just to process the entry node. |
| 880 | FlowQ.push(CFGEdge(EntryNum, EntryNum)); |
| 881 | |
| 882 | while (!FlowQ.empty()) { |
| 883 | CFGEdge Edge = FlowQ.front(); |
| 884 | FlowQ.pop(); |
| 885 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 886 | LLVM_DEBUG( |
| 887 | dbgs() << "Picked edge " |
| 888 | << printMBBReference(*MF.getBlockNumbered(Edge.first)) << "->" |
| 889 | << printMBBReference(*MF.getBlockNumbered(Edge.second)) << '\n'); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 890 | if (Edge.first != EntryNum) |
| 891 | if (EdgeExec.count(Edge)) |
| 892 | continue; |
| 893 | EdgeExec.insert(Edge); |
| 894 | MachineBasicBlock *SB = MF.getBlockNumbered(Edge.second); |
| 895 | |
| 896 | // Process the block in three stages: |
| 897 | // - visit all PHI nodes, |
| 898 | // - visit all non-branch instructions, |
| 899 | // - visit block branches. |
| 900 | MachineBasicBlock::const_iterator It = SB->begin(), End = SB->end(); |
| 901 | |
| 902 | // Visit PHI nodes in the successor block. |
| 903 | while (It != End && It->isPHI()) { |
| 904 | InstrExec.insert(&*It); |
| 905 | visitPHI(*It); |
| 906 | ++It; |
| 907 | } |
| 908 | |
| 909 | // If the successor block just became executable, visit all instructions. |
| 910 | // To see if this is the first time we're visiting it, check the first |
| 911 | // non-debug instruction to see if it is executable. |
| Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 912 | while (It != End && It->isDebugInstr()) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 913 | ++It; |
| 914 | assert(It == End || !It->isPHI()); |
| 915 | // If this block has been visited, go on to the next one. |
| 916 | if (It != End && InstrExec.count(&*It)) |
| 917 | continue; |
| 918 | // For now, scan all non-branch instructions. Branches require different |
| 919 | // processing. |
| 920 | while (It != End && !It->isBranch()) { |
| Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 921 | if (!It->isDebugInstr()) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 922 | InstrExec.insert(&*It); |
| 923 | visitNonBranch(*It); |
| 924 | } |
| 925 | ++It; |
| 926 | } |
| 927 | |
| 928 | // Time to process the end of the block. This is different from |
| 929 | // processing regular (non-branch) instructions, because there can |
| 930 | // be multiple branches in a block, and they can cause the block to |
| 931 | // terminate early. |
| 932 | if (It != End) { |
| 933 | visitBranchesFrom(*It); |
| 934 | } else { |
| 935 | // If the block didn't have a branch, add all successor edges to the |
| 936 | // work queue. (There should really be only one successor in such case.) |
| 937 | unsigned SBN = SB->getNumber(); |
| 938 | for (const MachineBasicBlock *SSB : SB->successors()) |
| 939 | FlowQ.push(CFGEdge(SBN, SSB->getNumber())); |
| 940 | } |
| 941 | } // while (FlowQ) |
| 942 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 943 | LLVM_DEBUG({ |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 944 | dbgs() << "Cells after propagation:\n"; |
| 945 | Cells.print(dbgs(), MCE.TRI); |
| 946 | dbgs() << "Dead CFG edges:\n"; |
| 947 | for (const MachineBasicBlock &B : MF) { |
| 948 | unsigned BN = B.getNumber(); |
| 949 | for (const MachineBasicBlock *SB : B.successors()) { |
| 950 | unsigned SN = SB->getNumber(); |
| 951 | if (!EdgeExec.count(CFGEdge(BN, SN))) |
| Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 952 | dbgs() << " " << printMBBReference(B) << " -> " |
| 953 | << printMBBReference(*SB) << '\n'; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | }); |
| 957 | } |
| 958 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 959 | bool MachineConstPropagator::rewrite(MachineFunction &MF) { |
| 960 | bool Changed = false; |
| 961 | // Rewrite all instructions based on the collected cell information. |
| 962 | // |
| 963 | // Traverse the instructions in a post-order, so that rewriting an |
| 964 | // instruction can make changes "downstream" in terms of control-flow |
| 965 | // without affecting the rewriting process. (We should not change |
| 966 | // instructions that have not yet been visited by the rewriter.) |
| 967 | // The reason for this is that the rewriter can introduce new vregs, |
| 968 | // and replace uses of old vregs (which had corresponding cells |
| 969 | // computed during propagation) with these new vregs (which at this |
| 970 | // point would not have any cells, and would appear to be "top"). |
| 971 | // If an attempt was made to evaluate an instruction with a fresh |
| 972 | // "top" vreg, it would cause an error (abend) in the evaluator. |
| 973 | |
| 974 | // Collect the post-order-traversal block ordering. The subsequent |
| 975 | // traversal/rewrite will update block successors, so it's safer |
| 976 | // if the visiting order it computed ahead of time. |
| 977 | std::vector<MachineBasicBlock*> POT; |
| 978 | for (MachineBasicBlock *B : post_order(&MF)) |
| 979 | if (!B->empty()) |
| 980 | POT.push_back(B); |
| 981 | |
| 982 | for (MachineBasicBlock *B : POT) { |
| 983 | // Walk the block backwards (which usually begin with the branches). |
| 984 | // If any branch is rewritten, we may need to update the successor |
| 985 | // information for this block. Unless the block's successors can be |
| 986 | // precisely determined (which may not be the case for indirect |
| 987 | // branches), we cannot modify any branch. |
| 988 | |
| 989 | // Compute the successor information. |
| 990 | SetVector<const MachineBasicBlock*> Targets; |
| 991 | bool HaveTargets = computeBlockSuccessors(B, Targets); |
| 992 | // Rewrite the executable instructions. Skip branches if we don't |
| 993 | // have block successor information. |
| 994 | for (auto I = B->rbegin(), E = B->rend(); I != E; ++I) { |
| 995 | MachineInstr &MI = *I; |
| 996 | if (InstrExec.count(&MI)) { |
| 997 | if (MI.isBranch() && !HaveTargets) |
| 998 | continue; |
| 999 | Changed |= MCE.rewrite(MI, Cells); |
| 1000 | } |
| 1001 | } |
| 1002 | // The rewriting could rewrite PHI nodes to non-PHI nodes, causing |
| 1003 | // regular instructions to appear in between PHI nodes. Bring all |
| 1004 | // the PHI nodes to the beginning of the block. |
| 1005 | for (auto I = B->begin(), E = B->end(); I != E; ++I) { |
| 1006 | if (I->isPHI()) |
| 1007 | continue; |
| 1008 | // I is not PHI. Find the next PHI node P. |
| 1009 | auto P = I; |
| 1010 | while (++P != E) |
| 1011 | if (P->isPHI()) |
| 1012 | break; |
| 1013 | // Not found. |
| 1014 | if (P == E) |
| 1015 | break; |
| 1016 | // Splice P right before I. |
| 1017 | B->splice(I, B, P); |
| 1018 | // Reset I to point at the just spliced PHI node. |
| 1019 | --I; |
| 1020 | } |
| 1021 | // Update the block successor information: remove unnecessary successors. |
| 1022 | if (HaveTargets) { |
| 1023 | SmallVector<MachineBasicBlock*,2> ToRemove; |
| 1024 | for (MachineBasicBlock *SB : B->successors()) { |
| 1025 | if (!Targets.count(SB)) |
| 1026 | ToRemove.push_back(const_cast<MachineBasicBlock*>(SB)); |
| 1027 | Targets.remove(SB); |
| 1028 | } |
| 1029 | for (unsigned i = 0, n = ToRemove.size(); i < n; ++i) |
| 1030 | removeCFGEdge(B, ToRemove[i]); |
| 1031 | // If there are any blocks left in the computed targets, it means that |
| 1032 | // we think that the block could go somewhere, but the CFG does not. |
| 1033 | // This could legitimately happen in blocks that have non-returning |
| 1034 | // calls---we would think that the execution can continue, but the |
| 1035 | // CFG will not have a successor edge. |
| 1036 | } |
| 1037 | } |
| 1038 | // Need to do some final post-processing. |
| 1039 | // If a branch was not executable, it will not get rewritten, but should |
| 1040 | // be removed (or replaced with something equivalent to a A2_nop). We can't |
| 1041 | // erase instructions during rewriting, so this needs to be delayed until |
| 1042 | // now. |
| 1043 | for (MachineBasicBlock &B : MF) { |
| 1044 | MachineBasicBlock::iterator I = B.begin(), E = B.end(); |
| 1045 | while (I != E) { |
| 1046 | auto Next = std::next(I); |
| 1047 | if (I->isBranch() && !InstrExec.count(&*I)) |
| 1048 | B.erase(I); |
| 1049 | I = Next; |
| 1050 | } |
| 1051 | } |
| 1052 | return Changed; |
| 1053 | } |
| 1054 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1055 | // This is the constant propagation algorithm as described by Wegman-Zadeck. |
| 1056 | // Most of the terminology comes from there. |
| 1057 | bool MachineConstPropagator::run(MachineFunction &MF) { |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1058 | LLVM_DEBUG(MF.print(dbgs() << "Starting MachineConstPropagator\n", nullptr)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1059 | |
| 1060 | MRI = &MF.getRegInfo(); |
| 1061 | |
| 1062 | Cells.clear(); |
| 1063 | EdgeExec.clear(); |
| 1064 | InstrExec.clear(); |
| 1065 | assert(FlowQ.empty()); |
| 1066 | |
| 1067 | propagate(MF); |
| 1068 | bool Changed = rewrite(MF); |
| 1069 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1070 | LLVM_DEBUG({ |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1071 | dbgs() << "End of MachineConstPropagator (Changed=" << Changed << ")\n"; |
| 1072 | if (Changed) |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1073 | MF.print(dbgs(), nullptr); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1074 | }); |
| 1075 | return Changed; |
| 1076 | } |
| 1077 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1078 | // -------------------------------------------------------------------- |
| 1079 | // Machine const evaluator. |
| 1080 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1081 | bool MachineConstEvaluator::getCell(const RegisterSubReg &R, const CellMap &Inputs, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1082 | LatticeCell &RC) { |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 1083 | if (!Register::isVirtualRegister(R.Reg)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1084 | return false; |
| 1085 | const LatticeCell &L = Inputs.get(R.Reg); |
| 1086 | if (!R.SubReg) { |
| 1087 | RC = L; |
| 1088 | return !RC.isBottom(); |
| 1089 | } |
| 1090 | bool Eval = evaluate(R, L, RC); |
| 1091 | return Eval && !RC.isBottom(); |
| 1092 | } |
| 1093 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1094 | bool MachineConstEvaluator::constToInt(const Constant *C, |
| 1095 | APInt &Val) const { |
| 1096 | const ConstantInt *CI = dyn_cast<ConstantInt>(C); |
| 1097 | if (!CI) |
| 1098 | return false; |
| 1099 | Val = CI->getValue(); |
| 1100 | return true; |
| 1101 | } |
| 1102 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1103 | const ConstantInt *MachineConstEvaluator::intToConst(const APInt &Val) const { |
| 1104 | return ConstantInt::get(CX, Val); |
| 1105 | } |
| 1106 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1107 | bool MachineConstEvaluator::evaluateCMPrr(uint32_t Cmp, const RegisterSubReg &R1, |
| 1108 | const RegisterSubReg &R2, const CellMap &Inputs, bool &Result) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1109 | assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg)); |
| 1110 | LatticeCell LS1, LS2; |
| 1111 | if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2)) |
| 1112 | return false; |
| 1113 | |
| 1114 | bool IsProp1 = LS1.isProperty(); |
| 1115 | bool IsProp2 = LS2.isProperty(); |
| 1116 | if (IsProp1) { |
| 1117 | uint32_t Prop1 = LS1.properties(); |
| 1118 | if (IsProp2) |
| 1119 | return evaluateCMPpp(Cmp, Prop1, LS2.properties(), Result); |
| 1120 | uint32_t NegCmp = Comparison::negate(Cmp); |
| 1121 | return evaluateCMPrp(NegCmp, R2, Prop1, Inputs, Result); |
| 1122 | } |
| 1123 | if (IsProp2) { |
| 1124 | uint32_t Prop2 = LS2.properties(); |
| 1125 | return evaluateCMPrp(Cmp, R1, Prop2, Inputs, Result); |
| 1126 | } |
| 1127 | |
| 1128 | APInt A; |
| 1129 | bool IsTrue = true, IsFalse = true; |
| 1130 | for (unsigned i = 0; i < LS2.size(); ++i) { |
| 1131 | bool Res; |
| 1132 | bool Computed = constToInt(LS2.Values[i], A) && |
| 1133 | evaluateCMPri(Cmp, R1, A, Inputs, Res); |
| 1134 | if (!Computed) |
| 1135 | return false; |
| 1136 | IsTrue &= Res; |
| 1137 | IsFalse &= !Res; |
| 1138 | } |
| 1139 | assert(!IsTrue || !IsFalse); |
| 1140 | // The actual logical value of the comparison is same as IsTrue. |
| 1141 | Result = IsTrue; |
| 1142 | // Return true if the result was proven to be true or proven to be false. |
| 1143 | return IsTrue || IsFalse; |
| 1144 | } |
| 1145 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1146 | bool MachineConstEvaluator::evaluateCMPri(uint32_t Cmp, const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1147 | const APInt &A2, const CellMap &Inputs, bool &Result) { |
| 1148 | assert(Inputs.has(R1.Reg)); |
| 1149 | LatticeCell LS; |
| 1150 | if (!getCell(R1, Inputs, LS)) |
| 1151 | return false; |
| 1152 | if (LS.isProperty()) |
| 1153 | return evaluateCMPpi(Cmp, LS.properties(), A2, Result); |
| 1154 | |
| 1155 | APInt A; |
| 1156 | bool IsTrue = true, IsFalse = true; |
| 1157 | for (unsigned i = 0; i < LS.size(); ++i) { |
| 1158 | bool Res; |
| 1159 | bool Computed = constToInt(LS.Values[i], A) && |
| 1160 | evaluateCMPii(Cmp, A, A2, Res); |
| 1161 | if (!Computed) |
| 1162 | return false; |
| 1163 | IsTrue &= Res; |
| 1164 | IsFalse &= !Res; |
| 1165 | } |
| 1166 | assert(!IsTrue || !IsFalse); |
| 1167 | // The actual logical value of the comparison is same as IsTrue. |
| 1168 | Result = IsTrue; |
| 1169 | // Return true if the result was proven to be true or proven to be false. |
| 1170 | return IsTrue || IsFalse; |
| 1171 | } |
| 1172 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1173 | bool MachineConstEvaluator::evaluateCMPrp(uint32_t Cmp, const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1174 | uint64_t Props2, const CellMap &Inputs, bool &Result) { |
| 1175 | assert(Inputs.has(R1.Reg)); |
| 1176 | LatticeCell LS; |
| 1177 | if (!getCell(R1, Inputs, LS)) |
| 1178 | return false; |
| 1179 | if (LS.isProperty()) |
| 1180 | return evaluateCMPpp(Cmp, LS.properties(), Props2, Result); |
| 1181 | |
| 1182 | APInt A; |
| 1183 | uint32_t NegCmp = Comparison::negate(Cmp); |
| 1184 | bool IsTrue = true, IsFalse = true; |
| 1185 | for (unsigned i = 0; i < LS.size(); ++i) { |
| 1186 | bool Res; |
| 1187 | bool Computed = constToInt(LS.Values[i], A) && |
| 1188 | evaluateCMPpi(NegCmp, Props2, A, Res); |
| 1189 | if (!Computed) |
| 1190 | return false; |
| 1191 | IsTrue &= Res; |
| 1192 | IsFalse &= !Res; |
| 1193 | } |
| 1194 | assert(!IsTrue || !IsFalse); |
| 1195 | Result = IsTrue; |
| 1196 | return IsTrue || IsFalse; |
| 1197 | } |
| 1198 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1199 | bool MachineConstEvaluator::evaluateCMPii(uint32_t Cmp, const APInt &A1, |
| 1200 | const APInt &A2, bool &Result) { |
| 1201 | // NE is a special kind of comparison (not composed of smaller properties). |
| 1202 | if (Cmp == Comparison::NE) { |
| 1203 | Result = !APInt::isSameValue(A1, A2); |
| 1204 | return true; |
| 1205 | } |
| 1206 | if (Cmp == Comparison::EQ) { |
| 1207 | Result = APInt::isSameValue(A1, A2); |
| 1208 | return true; |
| 1209 | } |
| 1210 | if (Cmp & Comparison::EQ) { |
| 1211 | if (APInt::isSameValue(A1, A2)) |
| 1212 | return (Result = true); |
| 1213 | } |
| 1214 | assert((Cmp & (Comparison::L | Comparison::G)) && "Malformed comparison"); |
| 1215 | Result = false; |
| 1216 | |
| 1217 | unsigned W1 = A1.getBitWidth(); |
| 1218 | unsigned W2 = A2.getBitWidth(); |
| 1219 | unsigned MaxW = (W1 >= W2) ? W1 : W2; |
| 1220 | if (Cmp & Comparison::U) { |
| 1221 | const APInt Zx1 = A1.zextOrSelf(MaxW); |
| 1222 | const APInt Zx2 = A2.zextOrSelf(MaxW); |
| 1223 | if (Cmp & Comparison::L) |
| 1224 | Result = Zx1.ult(Zx2); |
| 1225 | else if (Cmp & Comparison::G) |
| 1226 | Result = Zx2.ult(Zx1); |
| 1227 | return true; |
| 1228 | } |
| 1229 | |
| 1230 | // Signed comparison. |
| 1231 | const APInt Sx1 = A1.sextOrSelf(MaxW); |
| 1232 | const APInt Sx2 = A2.sextOrSelf(MaxW); |
| 1233 | if (Cmp & Comparison::L) |
| 1234 | Result = Sx1.slt(Sx2); |
| 1235 | else if (Cmp & Comparison::G) |
| 1236 | Result = Sx2.slt(Sx1); |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1240 | bool MachineConstEvaluator::evaluateCMPpi(uint32_t Cmp, uint32_t Props, |
| 1241 | const APInt &A2, bool &Result) { |
| 1242 | if (Props == ConstantProperties::Unknown) |
| 1243 | return false; |
| 1244 | |
| 1245 | // Should never see NaN here, but check for it for completeness. |
| 1246 | if (Props & ConstantProperties::NaN) |
| 1247 | return false; |
| 1248 | // Infinity could theoretically be compared to a number, but the |
| 1249 | // presence of infinity here would be very suspicious. If we don't |
| 1250 | // know for sure that the number is finite, bail out. |
| 1251 | if (!(Props & ConstantProperties::Finite)) |
| 1252 | return false; |
| 1253 | |
| 1254 | // Let X be a number that has properties Props. |
| 1255 | |
| 1256 | if (Cmp & Comparison::U) { |
| 1257 | // In case of unsigned comparisons, we can only compare against 0. |
| 1258 | if (A2 == 0) { |
| 1259 | // Any x!=0 will be considered >0 in an unsigned comparison. |
| 1260 | if (Props & ConstantProperties::Zero) |
| 1261 | Result = (Cmp & Comparison::EQ); |
| 1262 | else if (Props & ConstantProperties::NonZero) |
| 1263 | Result = (Cmp & Comparison::G) || (Cmp == Comparison::NE); |
| 1264 | else |
| 1265 | return false; |
| 1266 | return true; |
| 1267 | } |
| 1268 | // A2 is not zero. The only handled case is if X = 0. |
| 1269 | if (Props & ConstantProperties::Zero) { |
| 1270 | Result = (Cmp & Comparison::L) || (Cmp == Comparison::NE); |
| 1271 | return true; |
| 1272 | } |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | // Signed comparisons are different. |
| 1277 | if (Props & ConstantProperties::Zero) { |
| 1278 | if (A2 == 0) |
| 1279 | Result = (Cmp & Comparison::EQ); |
| 1280 | else |
| 1281 | Result = (Cmp == Comparison::NE) || |
| 1282 | ((Cmp & Comparison::L) && !A2.isNegative()) || |
| 1283 | ((Cmp & Comparison::G) && A2.isNegative()); |
| 1284 | return true; |
| 1285 | } |
| 1286 | if (Props & ConstantProperties::PosOrZero) { |
| 1287 | // X >= 0 and !(A2 < 0) => cannot compare |
| 1288 | if (!A2.isNegative()) |
| 1289 | return false; |
| 1290 | // X >= 0 and A2 < 0 |
| 1291 | Result = (Cmp & Comparison::G) || (Cmp == Comparison::NE); |
| 1292 | return true; |
| 1293 | } |
| 1294 | if (Props & ConstantProperties::NegOrZero) { |
| 1295 | // X <= 0 and Src1 < 0 => cannot compare |
| 1296 | if (A2 == 0 || A2.isNegative()) |
| 1297 | return false; |
| 1298 | // X <= 0 and A2 > 0 |
| 1299 | Result = (Cmp & Comparison::L) || (Cmp == Comparison::NE); |
| 1300 | return true; |
| 1301 | } |
| 1302 | |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1306 | bool MachineConstEvaluator::evaluateCMPpp(uint32_t Cmp, uint32_t Props1, |
| 1307 | uint32_t Props2, bool &Result) { |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1308 | using P = ConstantProperties; |
| 1309 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1310 | if ((Props1 & P::NaN) && (Props2 & P::NaN)) |
| 1311 | return false; |
| 1312 | if (!(Props1 & P::Finite) || !(Props2 & P::Finite)) |
| 1313 | return false; |
| 1314 | |
| 1315 | bool Zero1 = (Props1 & P::Zero), Zero2 = (Props2 & P::Zero); |
| 1316 | bool NonZero1 = (Props1 & P::NonZero), NonZero2 = (Props2 & P::NonZero); |
| 1317 | if (Zero1 && Zero2) { |
| 1318 | Result = (Cmp & Comparison::EQ); |
| 1319 | return true; |
| 1320 | } |
| 1321 | if (Cmp == Comparison::NE) { |
| 1322 | if ((Zero1 && NonZero2) || (NonZero1 && Zero2)) |
| 1323 | return (Result = true); |
| 1324 | return false; |
| 1325 | } |
| 1326 | |
| 1327 | if (Cmp & Comparison::U) { |
| 1328 | // In unsigned comparisons, we can only compare against a known zero, |
| 1329 | // or a known non-zero. |
| 1330 | if (Zero1 && NonZero2) { |
| 1331 | Result = (Cmp & Comparison::L); |
| 1332 | return true; |
| 1333 | } |
| 1334 | if (NonZero1 && Zero2) { |
| 1335 | Result = (Cmp & Comparison::G); |
| 1336 | return true; |
| 1337 | } |
| 1338 | return false; |
| 1339 | } |
| 1340 | |
| 1341 | // Signed comparison. The comparison is not NE. |
| 1342 | bool Poz1 = (Props1 & P::PosOrZero), Poz2 = (Props2 & P::PosOrZero); |
| 1343 | bool Nez1 = (Props1 & P::NegOrZero), Nez2 = (Props2 & P::NegOrZero); |
| 1344 | if (Nez1 && Poz2) { |
| 1345 | if (NonZero1 || NonZero2) { |
| 1346 | Result = (Cmp & Comparison::L); |
| 1347 | return true; |
| 1348 | } |
| 1349 | // Either (or both) could be zero. Can only say that X <= Y. |
| 1350 | if ((Cmp & Comparison::EQ) && (Cmp & Comparison::L)) |
| 1351 | return (Result = true); |
| 1352 | } |
| 1353 | if (Poz1 && Nez2) { |
| 1354 | if (NonZero1 || NonZero2) { |
| 1355 | Result = (Cmp & Comparison::G); |
| 1356 | return true; |
| 1357 | } |
| 1358 | // Either (or both) could be zero. Can only say that X >= Y. |
| 1359 | if ((Cmp & Comparison::EQ) && (Cmp & Comparison::G)) |
| 1360 | return (Result = true); |
| 1361 | } |
| 1362 | |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1366 | bool MachineConstEvaluator::evaluateCOPY(const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1367 | const CellMap &Inputs, LatticeCell &Result) { |
| 1368 | return getCell(R1, Inputs, Result); |
| 1369 | } |
| 1370 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1371 | bool MachineConstEvaluator::evaluateANDrr(const RegisterSubReg &R1, |
| 1372 | const RegisterSubReg &R2, const CellMap &Inputs, LatticeCell &Result) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1373 | assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg)); |
| 1374 | const LatticeCell &L1 = Inputs.get(R2.Reg); |
| 1375 | const LatticeCell &L2 = Inputs.get(R2.Reg); |
| 1376 | // If both sources are bottom, exit. Otherwise try to evaluate ANDri |
| 1377 | // with the non-bottom argument passed as the immediate. This is to |
| 1378 | // catch cases of ANDing with 0. |
| 1379 | if (L2.isBottom()) { |
| 1380 | if (L1.isBottom()) |
| 1381 | return false; |
| 1382 | return evaluateANDrr(R2, R1, Inputs, Result); |
| 1383 | } |
| 1384 | LatticeCell LS2; |
| 1385 | if (!evaluate(R2, L2, LS2)) |
| 1386 | return false; |
| 1387 | if (LS2.isBottom() || LS2.isProperty()) |
| 1388 | return false; |
| 1389 | |
| 1390 | APInt A; |
| 1391 | for (unsigned i = 0; i < LS2.size(); ++i) { |
| 1392 | LatticeCell RC; |
| 1393 | bool Eval = constToInt(LS2.Values[i], A) && |
| 1394 | evaluateANDri(R1, A, Inputs, RC); |
| 1395 | if (!Eval) |
| 1396 | return false; |
| 1397 | Result.meet(RC); |
| 1398 | } |
| 1399 | return !Result.isBottom(); |
| 1400 | } |
| 1401 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1402 | bool MachineConstEvaluator::evaluateANDri(const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1403 | const APInt &A2, const CellMap &Inputs, LatticeCell &Result) { |
| 1404 | assert(Inputs.has(R1.Reg)); |
| 1405 | if (A2 == -1) |
| 1406 | return getCell(R1, Inputs, Result); |
| 1407 | if (A2 == 0) { |
| 1408 | LatticeCell RC; |
| 1409 | RC.add(intToConst(A2)); |
| 1410 | // Overwrite Result. |
| 1411 | Result = RC; |
| 1412 | return true; |
| 1413 | } |
| 1414 | LatticeCell LS1; |
| 1415 | if (!getCell(R1, Inputs, LS1)) |
| 1416 | return false; |
| 1417 | if (LS1.isBottom() || LS1.isProperty()) |
| 1418 | return false; |
| 1419 | |
| 1420 | APInt A, ResA; |
| 1421 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1422 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1423 | evaluateANDii(A, A2, ResA); |
| 1424 | if (!Eval) |
| 1425 | return false; |
| 1426 | const Constant *C = intToConst(ResA); |
| 1427 | Result.add(C); |
| 1428 | } |
| 1429 | return !Result.isBottom(); |
| 1430 | } |
| 1431 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1432 | bool MachineConstEvaluator::evaluateANDii(const APInt &A1, |
| 1433 | const APInt &A2, APInt &Result) { |
| 1434 | Result = A1 & A2; |
| 1435 | return true; |
| 1436 | } |
| 1437 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1438 | bool MachineConstEvaluator::evaluateORrr(const RegisterSubReg &R1, |
| 1439 | const RegisterSubReg &R2, const CellMap &Inputs, LatticeCell &Result) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1440 | assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg)); |
| 1441 | const LatticeCell &L1 = Inputs.get(R2.Reg); |
| 1442 | const LatticeCell &L2 = Inputs.get(R2.Reg); |
| 1443 | // If both sources are bottom, exit. Otherwise try to evaluate ORri |
| 1444 | // with the non-bottom argument passed as the immediate. This is to |
| 1445 | // catch cases of ORing with -1. |
| 1446 | if (L2.isBottom()) { |
| 1447 | if (L1.isBottom()) |
| 1448 | return false; |
| 1449 | return evaluateORrr(R2, R1, Inputs, Result); |
| 1450 | } |
| 1451 | LatticeCell LS2; |
| 1452 | if (!evaluate(R2, L2, LS2)) |
| 1453 | return false; |
| 1454 | if (LS2.isBottom() || LS2.isProperty()) |
| 1455 | return false; |
| 1456 | |
| 1457 | APInt A; |
| 1458 | for (unsigned i = 0; i < LS2.size(); ++i) { |
| 1459 | LatticeCell RC; |
| 1460 | bool Eval = constToInt(LS2.Values[i], A) && |
| 1461 | evaluateORri(R1, A, Inputs, RC); |
| 1462 | if (!Eval) |
| 1463 | return false; |
| 1464 | Result.meet(RC); |
| 1465 | } |
| 1466 | return !Result.isBottom(); |
| 1467 | } |
| 1468 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1469 | bool MachineConstEvaluator::evaluateORri(const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1470 | const APInt &A2, const CellMap &Inputs, LatticeCell &Result) { |
| 1471 | assert(Inputs.has(R1.Reg)); |
| 1472 | if (A2 == 0) |
| 1473 | return getCell(R1, Inputs, Result); |
| 1474 | if (A2 == -1) { |
| 1475 | LatticeCell RC; |
| 1476 | RC.add(intToConst(A2)); |
| 1477 | // Overwrite Result. |
| 1478 | Result = RC; |
| 1479 | return true; |
| 1480 | } |
| 1481 | LatticeCell LS1; |
| 1482 | if (!getCell(R1, Inputs, LS1)) |
| 1483 | return false; |
| 1484 | if (LS1.isBottom() || LS1.isProperty()) |
| 1485 | return false; |
| 1486 | |
| 1487 | APInt A, ResA; |
| 1488 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1489 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1490 | evaluateORii(A, A2, ResA); |
| 1491 | if (!Eval) |
| 1492 | return false; |
| 1493 | const Constant *C = intToConst(ResA); |
| 1494 | Result.add(C); |
| 1495 | } |
| 1496 | return !Result.isBottom(); |
| 1497 | } |
| 1498 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1499 | bool MachineConstEvaluator::evaluateORii(const APInt &A1, |
| 1500 | const APInt &A2, APInt &Result) { |
| 1501 | Result = A1 | A2; |
| 1502 | return true; |
| 1503 | } |
| 1504 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1505 | bool MachineConstEvaluator::evaluateXORrr(const RegisterSubReg &R1, |
| 1506 | const RegisterSubReg &R2, const CellMap &Inputs, LatticeCell &Result) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1507 | assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg)); |
| 1508 | LatticeCell LS1, LS2; |
| 1509 | if (!getCell(R1, Inputs, LS1) || !getCell(R2, Inputs, LS2)) |
| 1510 | return false; |
| 1511 | if (LS1.isProperty()) { |
| 1512 | if (LS1.properties() & ConstantProperties::Zero) |
| 1513 | return !(Result = LS2).isBottom(); |
| 1514 | return false; |
| 1515 | } |
| 1516 | if (LS2.isProperty()) { |
| 1517 | if (LS2.properties() & ConstantProperties::Zero) |
| 1518 | return !(Result = LS1).isBottom(); |
| 1519 | return false; |
| 1520 | } |
| 1521 | |
| 1522 | APInt A; |
| 1523 | for (unsigned i = 0; i < LS2.size(); ++i) { |
| 1524 | LatticeCell RC; |
| 1525 | bool Eval = constToInt(LS2.Values[i], A) && |
| 1526 | evaluateXORri(R1, A, Inputs, RC); |
| 1527 | if (!Eval) |
| 1528 | return false; |
| 1529 | Result.meet(RC); |
| 1530 | } |
| 1531 | return !Result.isBottom(); |
| 1532 | } |
| 1533 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1534 | bool MachineConstEvaluator::evaluateXORri(const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1535 | const APInt &A2, const CellMap &Inputs, LatticeCell &Result) { |
| 1536 | assert(Inputs.has(R1.Reg)); |
| 1537 | LatticeCell LS1; |
| 1538 | if (!getCell(R1, Inputs, LS1)) |
| 1539 | return false; |
| 1540 | if (LS1.isProperty()) { |
| 1541 | if (LS1.properties() & ConstantProperties::Zero) { |
| 1542 | const Constant *C = intToConst(A2); |
| 1543 | Result.add(C); |
| 1544 | return !Result.isBottom(); |
| 1545 | } |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
| 1549 | APInt A, XA; |
| 1550 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1551 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1552 | evaluateXORii(A, A2, XA); |
| 1553 | if (!Eval) |
| 1554 | return false; |
| 1555 | const Constant *C = intToConst(XA); |
| 1556 | Result.add(C); |
| 1557 | } |
| 1558 | return !Result.isBottom(); |
| 1559 | } |
| 1560 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1561 | bool MachineConstEvaluator::evaluateXORii(const APInt &A1, |
| 1562 | const APInt &A2, APInt &Result) { |
| 1563 | Result = A1 ^ A2; |
| 1564 | return true; |
| 1565 | } |
| 1566 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1567 | bool MachineConstEvaluator::evaluateZEXTr(const RegisterSubReg &R1, unsigned Width, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1568 | unsigned Bits, const CellMap &Inputs, LatticeCell &Result) { |
| 1569 | assert(Inputs.has(R1.Reg)); |
| 1570 | LatticeCell LS1; |
| 1571 | if (!getCell(R1, Inputs, LS1)) |
| 1572 | return false; |
| 1573 | if (LS1.isProperty()) |
| 1574 | return false; |
| 1575 | |
| 1576 | APInt A, XA; |
| 1577 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1578 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1579 | evaluateZEXTi(A, Width, Bits, XA); |
| 1580 | if (!Eval) |
| 1581 | return false; |
| 1582 | const Constant *C = intToConst(XA); |
| 1583 | Result.add(C); |
| 1584 | } |
| 1585 | return true; |
| 1586 | } |
| 1587 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1588 | bool MachineConstEvaluator::evaluateZEXTi(const APInt &A1, unsigned Width, |
| 1589 | unsigned Bits, APInt &Result) { |
| 1590 | unsigned BW = A1.getBitWidth(); |
| Krzysztof Parzyszek | 6400dec | 2016-07-28 20:25:21 +0000 | [diff] [blame] | 1591 | (void)BW; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1592 | assert(Width >= Bits && BW >= Bits); |
| 1593 | APInt Mask = APInt::getLowBitsSet(Width, Bits); |
| 1594 | Result = A1.zextOrTrunc(Width) & Mask; |
| 1595 | return true; |
| 1596 | } |
| 1597 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1598 | bool MachineConstEvaluator::evaluateSEXTr(const RegisterSubReg &R1, unsigned Width, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1599 | unsigned Bits, const CellMap &Inputs, LatticeCell &Result) { |
| 1600 | assert(Inputs.has(R1.Reg)); |
| 1601 | LatticeCell LS1; |
| 1602 | if (!getCell(R1, Inputs, LS1)) |
| 1603 | return false; |
| 1604 | if (LS1.isBottom() || LS1.isProperty()) |
| 1605 | return false; |
| 1606 | |
| 1607 | APInt A, XA; |
| 1608 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1609 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1610 | evaluateSEXTi(A, Width, Bits, XA); |
| 1611 | if (!Eval) |
| 1612 | return false; |
| 1613 | const Constant *C = intToConst(XA); |
| 1614 | Result.add(C); |
| 1615 | } |
| 1616 | return true; |
| 1617 | } |
| 1618 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1619 | bool MachineConstEvaluator::evaluateSEXTi(const APInt &A1, unsigned Width, |
| 1620 | unsigned Bits, APInt &Result) { |
| 1621 | unsigned BW = A1.getBitWidth(); |
| 1622 | assert(Width >= Bits && BW >= Bits); |
| 1623 | // Special case to make things faster for smaller source widths. |
| 1624 | // Sign extension of 0 bits generates 0 as a result. This is consistent |
| 1625 | // with what the HW does. |
| 1626 | if (Bits == 0) { |
| 1627 | Result = APInt(Width, 0); |
| 1628 | return true; |
| 1629 | } |
| 1630 | // In C, shifts by 64 invoke undefined behavior: handle that case in APInt. |
| 1631 | if (BW <= 64 && Bits != 0) { |
| 1632 | int64_t V = A1.getSExtValue(); |
| 1633 | switch (Bits) { |
| 1634 | case 8: |
| 1635 | V = static_cast<int8_t>(V); |
| 1636 | break; |
| 1637 | case 16: |
| 1638 | V = static_cast<int16_t>(V); |
| 1639 | break; |
| 1640 | case 32: |
| 1641 | V = static_cast<int32_t>(V); |
| 1642 | break; |
| 1643 | default: |
| 1644 | // Shift left to lose all bits except lower "Bits" bits, then shift |
| 1645 | // the value back, replicating what was a sign bit after the first |
| 1646 | // shift. |
| 1647 | V = (V << (64-Bits)) >> (64-Bits); |
| 1648 | break; |
| 1649 | } |
| 1650 | // V is a 64-bit sign-extended value. Convert it to APInt of desired |
| 1651 | // width. |
| 1652 | Result = APInt(Width, V, true); |
| 1653 | return true; |
| 1654 | } |
| 1655 | // Slow case: the value doesn't fit in int64_t. |
| 1656 | if (Bits < BW) |
| 1657 | Result = A1.trunc(Bits).sext(Width); |
| 1658 | else // Bits == BW |
| 1659 | Result = A1.sext(Width); |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1663 | bool MachineConstEvaluator::evaluateCLBr(const RegisterSubReg &R1, bool Zeros, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1664 | bool Ones, const CellMap &Inputs, LatticeCell &Result) { |
| 1665 | assert(Inputs.has(R1.Reg)); |
| 1666 | LatticeCell LS1; |
| 1667 | if (!getCell(R1, Inputs, LS1)) |
| 1668 | return false; |
| 1669 | if (LS1.isBottom() || LS1.isProperty()) |
| 1670 | return false; |
| 1671 | |
| 1672 | APInt A, CA; |
| 1673 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1674 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1675 | evaluateCLBi(A, Zeros, Ones, CA); |
| 1676 | if (!Eval) |
| 1677 | return false; |
| 1678 | const Constant *C = intToConst(CA); |
| 1679 | Result.add(C); |
| 1680 | } |
| 1681 | return true; |
| 1682 | } |
| 1683 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1684 | bool MachineConstEvaluator::evaluateCLBi(const APInt &A1, bool Zeros, |
| 1685 | bool Ones, APInt &Result) { |
| 1686 | unsigned BW = A1.getBitWidth(); |
| 1687 | if (!Zeros && !Ones) |
| 1688 | return false; |
| 1689 | unsigned Count = 0; |
| 1690 | if (Zeros && (Count == 0)) |
| 1691 | Count = A1.countLeadingZeros(); |
| 1692 | if (Ones && (Count == 0)) |
| 1693 | Count = A1.countLeadingOnes(); |
| 1694 | Result = APInt(BW, static_cast<uint64_t>(Count), false); |
| 1695 | return true; |
| 1696 | } |
| 1697 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1698 | bool MachineConstEvaluator::evaluateCTBr(const RegisterSubReg &R1, bool Zeros, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1699 | bool Ones, const CellMap &Inputs, LatticeCell &Result) { |
| 1700 | assert(Inputs.has(R1.Reg)); |
| 1701 | LatticeCell LS1; |
| 1702 | if (!getCell(R1, Inputs, LS1)) |
| 1703 | return false; |
| 1704 | if (LS1.isBottom() || LS1.isProperty()) |
| 1705 | return false; |
| 1706 | |
| 1707 | APInt A, CA; |
| 1708 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1709 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1710 | evaluateCTBi(A, Zeros, Ones, CA); |
| 1711 | if (!Eval) |
| 1712 | return false; |
| 1713 | const Constant *C = intToConst(CA); |
| 1714 | Result.add(C); |
| 1715 | } |
| 1716 | return true; |
| 1717 | } |
| 1718 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1719 | bool MachineConstEvaluator::evaluateCTBi(const APInt &A1, bool Zeros, |
| 1720 | bool Ones, APInt &Result) { |
| 1721 | unsigned BW = A1.getBitWidth(); |
| 1722 | if (!Zeros && !Ones) |
| 1723 | return false; |
| 1724 | unsigned Count = 0; |
| 1725 | if (Zeros && (Count == 0)) |
| 1726 | Count = A1.countTrailingZeros(); |
| 1727 | if (Ones && (Count == 0)) |
| 1728 | Count = A1.countTrailingOnes(); |
| 1729 | Result = APInt(BW, static_cast<uint64_t>(Count), false); |
| 1730 | return true; |
| 1731 | } |
| 1732 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1733 | bool MachineConstEvaluator::evaluateEXTRACTr(const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1734 | unsigned Width, unsigned Bits, unsigned Offset, bool Signed, |
| 1735 | const CellMap &Inputs, LatticeCell &Result) { |
| 1736 | assert(Inputs.has(R1.Reg)); |
| 1737 | assert(Bits+Offset <= Width); |
| 1738 | LatticeCell LS1; |
| 1739 | if (!getCell(R1, Inputs, LS1)) |
| 1740 | return false; |
| 1741 | if (LS1.isBottom()) |
| 1742 | return false; |
| 1743 | if (LS1.isProperty()) { |
| 1744 | uint32_t Ps = LS1.properties(); |
| 1745 | if (Ps & ConstantProperties::Zero) { |
| 1746 | const Constant *C = intToConst(APInt(Width, 0, false)); |
| 1747 | Result.add(C); |
| 1748 | return true; |
| 1749 | } |
| 1750 | return false; |
| 1751 | } |
| 1752 | |
| 1753 | APInt A, CA; |
| 1754 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1755 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1756 | evaluateEXTRACTi(A, Bits, Offset, Signed, CA); |
| 1757 | if (!Eval) |
| 1758 | return false; |
| 1759 | const Constant *C = intToConst(CA); |
| 1760 | Result.add(C); |
| 1761 | } |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1765 | bool MachineConstEvaluator::evaluateEXTRACTi(const APInt &A1, unsigned Bits, |
| 1766 | unsigned Offset, bool Signed, APInt &Result) { |
| 1767 | unsigned BW = A1.getBitWidth(); |
| 1768 | assert(Bits+Offset <= BW); |
| 1769 | // Extracting 0 bits generates 0 as a result (as indicated by the HW people). |
| 1770 | if (Bits == 0) { |
| 1771 | Result = APInt(BW, 0); |
| 1772 | return true; |
| 1773 | } |
| 1774 | if (BW <= 64) { |
| 1775 | int64_t V = A1.getZExtValue(); |
| 1776 | V <<= (64-Bits-Offset); |
| 1777 | if (Signed) |
| 1778 | V >>= (64-Bits); |
| 1779 | else |
| 1780 | V = static_cast<uint64_t>(V) >> (64-Bits); |
| 1781 | Result = APInt(BW, V, Signed); |
| 1782 | return true; |
| 1783 | } |
| 1784 | if (Signed) |
| 1785 | Result = A1.shl(BW-Bits-Offset).ashr(BW-Bits); |
| 1786 | else |
| 1787 | Result = A1.shl(BW-Bits-Offset).lshr(BW-Bits); |
| 1788 | return true; |
| 1789 | } |
| 1790 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1791 | bool MachineConstEvaluator::evaluateSplatr(const RegisterSubReg &R1, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1792 | unsigned Bits, unsigned Count, const CellMap &Inputs, |
| 1793 | LatticeCell &Result) { |
| 1794 | assert(Inputs.has(R1.Reg)); |
| 1795 | LatticeCell LS1; |
| 1796 | if (!getCell(R1, Inputs, LS1)) |
| 1797 | return false; |
| 1798 | if (LS1.isBottom() || LS1.isProperty()) |
| 1799 | return false; |
| 1800 | |
| 1801 | APInt A, SA; |
| 1802 | for (unsigned i = 0; i < LS1.size(); ++i) { |
| 1803 | bool Eval = constToInt(LS1.Values[i], A) && |
| 1804 | evaluateSplati(A, Bits, Count, SA); |
| 1805 | if (!Eval) |
| 1806 | return false; |
| 1807 | const Constant *C = intToConst(SA); |
| 1808 | Result.add(C); |
| 1809 | } |
| 1810 | return true; |
| 1811 | } |
| 1812 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1813 | bool MachineConstEvaluator::evaluateSplati(const APInt &A1, unsigned Bits, |
| 1814 | unsigned Count, APInt &Result) { |
| 1815 | assert(Count > 0); |
| 1816 | unsigned BW = A1.getBitWidth(), SW = Count*Bits; |
| 1817 | APInt LoBits = (Bits < BW) ? A1.trunc(Bits) : A1.zextOrSelf(Bits); |
| 1818 | if (Count > 1) |
| 1819 | LoBits = LoBits.zext(SW); |
| 1820 | |
| 1821 | APInt Res(SW, 0, false); |
| 1822 | for (unsigned i = 0; i < Count; ++i) { |
| 1823 | Res <<= Bits; |
| 1824 | Res |= LoBits; |
| 1825 | } |
| 1826 | Result = Res; |
| 1827 | return true; |
| 1828 | } |
| 1829 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1830 | // ---------------------------------------------------------------------- |
| 1831 | // Hexagon-specific code. |
| 1832 | |
| 1833 | namespace llvm { |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1834 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1835 | FunctionPass *createHexagonConstPropagationPass(); |
| 1836 | void initializeHexagonConstPropagationPass(PassRegistry &Registry); |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1837 | |
| 1838 | } // end namespace llvm |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1839 | |
| 1840 | namespace { |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1841 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1842 | class HexagonConstEvaluator : public MachineConstEvaluator { |
| 1843 | public: |
| 1844 | HexagonConstEvaluator(MachineFunction &Fn); |
| 1845 | |
| 1846 | bool evaluate(const MachineInstr &MI, const CellMap &Inputs, |
| 1847 | CellMap &Outputs) override; |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1848 | bool evaluate(const RegisterSubReg &R, const LatticeCell &SrcC, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1849 | LatticeCell &Result) override; |
| 1850 | bool evaluate(const MachineInstr &BrI, const CellMap &Inputs, |
| 1851 | SetVector<const MachineBasicBlock*> &Targets, bool &FallsThru) |
| 1852 | override; |
| 1853 | bool rewrite(MachineInstr &MI, const CellMap &Inputs) override; |
| 1854 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1855 | private: |
| 1856 | unsigned getRegBitWidth(unsigned Reg) const; |
| 1857 | |
| 1858 | static uint32_t getCmp(unsigned Opc); |
| 1859 | static APInt getCmpImm(unsigned Opc, unsigned OpX, |
| 1860 | const MachineOperand &MO); |
| 1861 | void replaceWithNop(MachineInstr &MI); |
| 1862 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1863 | bool evaluateHexRSEQ32(RegisterSubReg RL, RegisterSubReg RH, const CellMap &Inputs, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1864 | LatticeCell &Result); |
| 1865 | bool evaluateHexCompare(const MachineInstr &MI, const CellMap &Inputs, |
| 1866 | CellMap &Outputs); |
| 1867 | // This is suitable to be called for compare-and-jump instructions. |
| 1868 | bool evaluateHexCompare2(uint32_t Cmp, const MachineOperand &Src1, |
| 1869 | const MachineOperand &Src2, const CellMap &Inputs, bool &Result); |
| 1870 | bool evaluateHexLogical(const MachineInstr &MI, const CellMap &Inputs, |
| 1871 | CellMap &Outputs); |
| 1872 | bool evaluateHexCondMove(const MachineInstr &MI, const CellMap &Inputs, |
| 1873 | CellMap &Outputs); |
| 1874 | bool evaluateHexExt(const MachineInstr &MI, const CellMap &Inputs, |
| 1875 | CellMap &Outputs); |
| 1876 | bool evaluateHexVector1(const MachineInstr &MI, const CellMap &Inputs, |
| 1877 | CellMap &Outputs); |
| 1878 | bool evaluateHexVector2(const MachineInstr &MI, const CellMap &Inputs, |
| 1879 | CellMap &Outputs); |
| 1880 | |
| 1881 | void replaceAllRegUsesWith(unsigned FromReg, unsigned ToReg); |
| 1882 | bool rewriteHexBranch(MachineInstr &BrI, const CellMap &Inputs); |
| 1883 | bool rewriteHexConstDefs(MachineInstr &MI, const CellMap &Inputs, |
| 1884 | bool &AllDefs); |
| 1885 | bool rewriteHexConstUses(MachineInstr &MI, const CellMap &Inputs); |
| 1886 | |
| 1887 | MachineRegisterInfo *MRI; |
| 1888 | const HexagonInstrInfo &HII; |
| 1889 | const HexagonRegisterInfo &HRI; |
| 1890 | }; |
| 1891 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1892 | class HexagonConstPropagation : public MachineFunctionPass { |
| 1893 | public: |
| 1894 | static char ID; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1895 | |
| Krzysztof Parzyszek | 96690ce | 2018-02-23 20:33:26 +0000 | [diff] [blame] | 1896 | HexagonConstPropagation() : MachineFunctionPass(ID) {} |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1897 | |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 1898 | StringRef getPassName() const override { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1899 | return "Hexagon Constant Propagation"; |
| 1900 | } |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1901 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1902 | bool runOnMachineFunction(MachineFunction &MF) override { |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1903 | const Function &F = MF.getFunction(); |
| 1904 | if (skipFunction(F)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1905 | return false; |
| 1906 | |
| 1907 | HexagonConstEvaluator HCE(MF); |
| 1908 | return MachineConstPropagator(HCE).run(MF); |
| 1909 | } |
| 1910 | }; |
| 1911 | |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 1912 | } // end anonymous namespace |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1913 | |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 1914 | char HexagonConstPropagation::ID = 0; |
| 1915 | |
| Krzysztof Parzyszek | 96690ce | 2018-02-23 20:33:26 +0000 | [diff] [blame] | 1916 | INITIALIZE_PASS(HexagonConstPropagation, "hexagon-constp", |
| 1917 | "Hexagon Constant Propagation", false, false) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1918 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1919 | HexagonConstEvaluator::HexagonConstEvaluator(MachineFunction &Fn) |
| 1920 | : MachineConstEvaluator(Fn), |
| 1921 | HII(*Fn.getSubtarget<HexagonSubtarget>().getInstrInfo()), |
| 1922 | HRI(*Fn.getSubtarget<HexagonSubtarget>().getRegisterInfo()) { |
| 1923 | MRI = &Fn.getRegInfo(); |
| 1924 | } |
| 1925 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1926 | bool HexagonConstEvaluator::evaluate(const MachineInstr &MI, |
| 1927 | const CellMap &Inputs, CellMap &Outputs) { |
| 1928 | if (MI.isCall()) |
| 1929 | return false; |
| 1930 | if (MI.getNumOperands() == 0 || !MI.getOperand(0).isReg()) |
| 1931 | return false; |
| 1932 | const MachineOperand &MD = MI.getOperand(0); |
| 1933 | if (!MD.isDef()) |
| 1934 | return false; |
| 1935 | |
| 1936 | unsigned Opc = MI.getOpcode(); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1937 | RegisterSubReg DefR(MD); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1938 | assert(!DefR.SubReg); |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 1939 | if (!Register::isVirtualRegister(DefR.Reg)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1940 | return false; |
| 1941 | |
| 1942 | if (MI.isCopy()) { |
| 1943 | LatticeCell RC; |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1944 | RegisterSubReg SrcR(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1945 | bool Eval = evaluateCOPY(SrcR, Inputs, RC); |
| 1946 | if (!Eval) |
| 1947 | return false; |
| 1948 | Outputs.update(DefR.Reg, RC); |
| 1949 | return true; |
| 1950 | } |
| 1951 | if (MI.isRegSequence()) { |
| 1952 | unsigned Sub1 = MI.getOperand(2).getImm(); |
| 1953 | unsigned Sub2 = MI.getOperand(4).getImm(); |
| Krzysztof Parzyszek | d72bd83 | 2017-09-25 18:49:42 +0000 | [diff] [blame] | 1954 | const TargetRegisterClass &DefRC = *MRI->getRegClass(DefR.Reg); |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1955 | unsigned SubLo = HRI.getHexagonSubRegIndex(DefRC, Hexagon::ps_sub_lo); |
| 1956 | unsigned SubHi = HRI.getHexagonSubRegIndex(DefRC, Hexagon::ps_sub_hi); |
| 1957 | if (Sub1 != SubLo && Sub1 != SubHi) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1958 | return false; |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1959 | if (Sub2 != SubLo && Sub2 != SubHi) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1960 | return false; |
| 1961 | assert(Sub1 != Sub2); |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 1962 | bool LoIs1 = (Sub1 == SubLo); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1963 | const MachineOperand &OpLo = LoIs1 ? MI.getOperand(1) : MI.getOperand(3); |
| 1964 | const MachineOperand &OpHi = LoIs1 ? MI.getOperand(3) : MI.getOperand(1); |
| 1965 | LatticeCell RC; |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 1966 | RegisterSubReg SrcRL(OpLo), SrcRH(OpHi); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1967 | bool Eval = evaluateHexRSEQ32(SrcRL, SrcRH, Inputs, RC); |
| 1968 | if (!Eval) |
| 1969 | return false; |
| 1970 | Outputs.update(DefR.Reg, RC); |
| 1971 | return true; |
| 1972 | } |
| 1973 | if (MI.isCompare()) { |
| 1974 | bool Eval = evaluateHexCompare(MI, Inputs, Outputs); |
| 1975 | return Eval; |
| 1976 | } |
| 1977 | |
| 1978 | switch (Opc) { |
| 1979 | default: |
| 1980 | return false; |
| 1981 | case Hexagon::A2_tfrsi: |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1982 | case Hexagon::A2_tfrpi: |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 1983 | case Hexagon::CONST32: |
| 1984 | case Hexagon::CONST64: |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1985 | { |
| 1986 | const MachineOperand &VO = MI.getOperand(1); |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 1987 | // The operand of CONST32 can be a blockaddress, e.g. |
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 1988 | // %0 = CONST32 <blockaddress(@eat, %l)> |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 1989 | // Do this check for all instructions for safety. |
| 1990 | if (!VO.isImm()) |
| 1991 | return false; |
| 1992 | int64_t V = MI.getOperand(1).getImm(); |
| 1993 | unsigned W = getRegBitWidth(DefR.Reg); |
| 1994 | if (W != 32 && W != 64) |
| 1995 | return false; |
| 1996 | IntegerType *Ty = (W == 32) ? Type::getInt32Ty(CX) |
| 1997 | : Type::getInt64Ty(CX); |
| 1998 | const ConstantInt *CI = ConstantInt::get(Ty, V, true); |
| 1999 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2000 | RC.add(CI); |
| 2001 | Outputs.update(DefR.Reg, RC); |
| 2002 | break; |
| 2003 | } |
| 2004 | |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 2005 | case Hexagon::PS_true: |
| 2006 | case Hexagon::PS_false: |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2007 | { |
| 2008 | LatticeCell RC = Outputs.get(DefR.Reg); |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 2009 | bool NonZero = (Opc == Hexagon::PS_true); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2010 | uint32_t P = NonZero ? ConstantProperties::NonZero |
| 2011 | : ConstantProperties::Zero; |
| 2012 | RC.add(P); |
| 2013 | Outputs.update(DefR.Reg, RC); |
| 2014 | break; |
| 2015 | } |
| 2016 | |
| 2017 | case Hexagon::A2_and: |
| 2018 | case Hexagon::A2_andir: |
| 2019 | case Hexagon::A2_andp: |
| 2020 | case Hexagon::A2_or: |
| 2021 | case Hexagon::A2_orir: |
| 2022 | case Hexagon::A2_orp: |
| 2023 | case Hexagon::A2_xor: |
| 2024 | case Hexagon::A2_xorp: |
| 2025 | { |
| 2026 | bool Eval = evaluateHexLogical(MI, Inputs, Outputs); |
| 2027 | if (!Eval) |
| 2028 | return false; |
| 2029 | break; |
| 2030 | } |
| 2031 | |
| 2032 | case Hexagon::A2_combineii: // combine(#s8Ext, #s8) |
| 2033 | case Hexagon::A4_combineii: // combine(#s8, #u6Ext) |
| 2034 | { |
| Krzysztof Parzyszek | 96690ce | 2018-02-23 20:33:26 +0000 | [diff] [blame] | 2035 | if (!MI.getOperand(1).isImm() || !MI.getOperand(2).isImm()) |
| 2036 | return false; |
| Benjamin Kramer | afff73c | 2016-07-30 13:25:37 +0000 | [diff] [blame] | 2037 | uint64_t Hi = MI.getOperand(1).getImm(); |
| 2038 | uint64_t Lo = MI.getOperand(2).getImm(); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2039 | uint64_t Res = (Hi << 32) | (Lo & 0xFFFFFFFF); |
| 2040 | IntegerType *Ty = Type::getInt64Ty(CX); |
| 2041 | const ConstantInt *CI = ConstantInt::get(Ty, Res, false); |
| 2042 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2043 | RC.add(CI); |
| 2044 | Outputs.update(DefR.Reg, RC); |
| 2045 | break; |
| 2046 | } |
| 2047 | |
| 2048 | case Hexagon::S2_setbit_i: |
| 2049 | { |
| 2050 | int64_t B = MI.getOperand(2).getImm(); |
| 2051 | assert(B >=0 && B < 32); |
| Simon Pilgrim | 0aaf6ba | 2016-07-29 10:03:39 +0000 | [diff] [blame] | 2052 | APInt A(32, (1ull << B), false); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2053 | RegisterSubReg R(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2054 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2055 | bool Eval = evaluateORri(R, A, Inputs, RC); |
| 2056 | if (!Eval) |
| 2057 | return false; |
| 2058 | Outputs.update(DefR.Reg, RC); |
| 2059 | break; |
| 2060 | } |
| 2061 | |
| 2062 | case Hexagon::C2_mux: |
| 2063 | case Hexagon::C2_muxir: |
| 2064 | case Hexagon::C2_muxri: |
| 2065 | case Hexagon::C2_muxii: |
| 2066 | { |
| 2067 | bool Eval = evaluateHexCondMove(MI, Inputs, Outputs); |
| 2068 | if (!Eval) |
| 2069 | return false; |
| 2070 | break; |
| 2071 | } |
| 2072 | |
| 2073 | case Hexagon::A2_sxtb: |
| 2074 | case Hexagon::A2_sxth: |
| 2075 | case Hexagon::A2_sxtw: |
| 2076 | case Hexagon::A2_zxtb: |
| 2077 | case Hexagon::A2_zxth: |
| 2078 | { |
| 2079 | bool Eval = evaluateHexExt(MI, Inputs, Outputs); |
| 2080 | if (!Eval) |
| 2081 | return false; |
| 2082 | break; |
| 2083 | } |
| 2084 | |
| 2085 | case Hexagon::S2_ct0: |
| 2086 | case Hexagon::S2_ct0p: |
| 2087 | case Hexagon::S2_ct1: |
| 2088 | case Hexagon::S2_ct1p: |
| 2089 | { |
| 2090 | using namespace Hexagon; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 2091 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2092 | bool Ones = (Opc == S2_ct1) || (Opc == S2_ct1p); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2093 | RegisterSubReg R1(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2094 | assert(Inputs.has(R1.Reg)); |
| 2095 | LatticeCell T; |
| 2096 | bool Eval = evaluateCTBr(R1, !Ones, Ones, Inputs, T); |
| 2097 | if (!Eval) |
| 2098 | return false; |
| 2099 | // All of these instructions return a 32-bit value. The evaluate |
| 2100 | // will generate the same type as the operand, so truncate the |
| 2101 | // result if necessary. |
| 2102 | APInt C; |
| 2103 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2104 | for (unsigned i = 0; i < T.size(); ++i) { |
| 2105 | const Constant *CI = T.Values[i]; |
| 2106 | if (constToInt(CI, C) && C.getBitWidth() > 32) |
| 2107 | CI = intToConst(C.trunc(32)); |
| 2108 | RC.add(CI); |
| 2109 | } |
| 2110 | Outputs.update(DefR.Reg, RC); |
| 2111 | break; |
| 2112 | } |
| 2113 | |
| 2114 | case Hexagon::S2_cl0: |
| 2115 | case Hexagon::S2_cl0p: |
| 2116 | case Hexagon::S2_cl1: |
| 2117 | case Hexagon::S2_cl1p: |
| 2118 | case Hexagon::S2_clb: |
| 2119 | case Hexagon::S2_clbp: |
| 2120 | { |
| 2121 | using namespace Hexagon; |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 2122 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2123 | bool OnlyZeros = (Opc == S2_cl0) || (Opc == S2_cl0p); |
| 2124 | bool OnlyOnes = (Opc == S2_cl1) || (Opc == S2_cl1p); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2125 | RegisterSubReg R1(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2126 | assert(Inputs.has(R1.Reg)); |
| 2127 | LatticeCell T; |
| 2128 | bool Eval = evaluateCLBr(R1, !OnlyOnes, !OnlyZeros, Inputs, T); |
| 2129 | if (!Eval) |
| 2130 | return false; |
| 2131 | // All of these instructions return a 32-bit value. The evaluate |
| 2132 | // will generate the same type as the operand, so truncate the |
| 2133 | // result if necessary. |
| 2134 | APInt C; |
| 2135 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2136 | for (unsigned i = 0; i < T.size(); ++i) { |
| 2137 | const Constant *CI = T.Values[i]; |
| 2138 | if (constToInt(CI, C) && C.getBitWidth() > 32) |
| 2139 | CI = intToConst(C.trunc(32)); |
| 2140 | RC.add(CI); |
| 2141 | } |
| 2142 | Outputs.update(DefR.Reg, RC); |
| 2143 | break; |
| 2144 | } |
| 2145 | |
| 2146 | case Hexagon::S4_extract: |
| 2147 | case Hexagon::S4_extractp: |
| 2148 | case Hexagon::S2_extractu: |
| 2149 | case Hexagon::S2_extractup: |
| 2150 | { |
| 2151 | bool Signed = (Opc == Hexagon::S4_extract) || |
| 2152 | (Opc == Hexagon::S4_extractp); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2153 | RegisterSubReg R1(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2154 | unsigned BW = getRegBitWidth(R1.Reg); |
| 2155 | unsigned Bits = MI.getOperand(2).getImm(); |
| 2156 | unsigned Offset = MI.getOperand(3).getImm(); |
| 2157 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2158 | if (Offset >= BW) { |
| 2159 | APInt Zero(BW, 0, false); |
| 2160 | RC.add(intToConst(Zero)); |
| 2161 | break; |
| 2162 | } |
| 2163 | if (Offset+Bits > BW) { |
| 2164 | // If the requested bitfield extends beyond the most significant bit, |
| 2165 | // the extra bits are treated as 0s. To emulate this behavior, reduce |
| 2166 | // the number of requested bits, and make the extract unsigned. |
| 2167 | Bits = BW-Offset; |
| 2168 | Signed = false; |
| 2169 | } |
| 2170 | bool Eval = evaluateEXTRACTr(R1, BW, Bits, Offset, Signed, Inputs, RC); |
| 2171 | if (!Eval) |
| 2172 | return false; |
| 2173 | Outputs.update(DefR.Reg, RC); |
| 2174 | break; |
| 2175 | } |
| 2176 | |
| 2177 | case Hexagon::S2_vsplatrb: |
| 2178 | case Hexagon::S2_vsplatrh: |
| 2179 | // vabsh, vabsh:sat |
| 2180 | // vabsw, vabsw:sat |
| 2181 | // vconj:sat |
| 2182 | // vrndwh, vrndwh:sat |
| 2183 | // vsathb, vsathub, vsatwuh |
| 2184 | // vsxtbh, vsxthw |
| 2185 | // vtrunehb, vtrunohb |
| 2186 | // vzxtbh, vzxthw |
| 2187 | { |
| 2188 | bool Eval = evaluateHexVector1(MI, Inputs, Outputs); |
| 2189 | if (!Eval) |
| 2190 | return false; |
| 2191 | break; |
| 2192 | } |
| 2193 | |
| 2194 | // TODO: |
| 2195 | // A2_vaddh |
| 2196 | // A2_vaddhs |
| 2197 | // A2_vaddw |
| 2198 | // A2_vaddws |
| 2199 | } |
| 2200 | |
| 2201 | return true; |
| 2202 | } |
| 2203 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2204 | bool HexagonConstEvaluator::evaluate(const RegisterSubReg &R, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2205 | const LatticeCell &Input, LatticeCell &Result) { |
| 2206 | if (!R.SubReg) { |
| 2207 | Result = Input; |
| 2208 | return true; |
| 2209 | } |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2210 | const TargetRegisterClass *RC = MRI->getRegClass(R.Reg); |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 2211 | if (RC != &Hexagon::DoubleRegsRegClass) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2212 | return false; |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 2213 | if (R.SubReg != Hexagon::isub_lo && R.SubReg != Hexagon::isub_hi) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2214 | return false; |
| 2215 | |
| 2216 | assert(!Input.isTop()); |
| 2217 | if (Input.isBottom()) |
| 2218 | return false; |
| 2219 | |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 2220 | using P = ConstantProperties; |
| 2221 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2222 | if (Input.isProperty()) { |
| 2223 | uint32_t Ps = Input.properties(); |
| 2224 | if (Ps & (P::Zero|P::NaN)) { |
| 2225 | uint32_t Ns = (Ps & (P::Zero|P::NaN|P::SignProperties)); |
| 2226 | Result.add(Ns); |
| 2227 | return true; |
| 2228 | } |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 2229 | if (R.SubReg == Hexagon::isub_hi) { |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2230 | uint32_t Ns = (Ps & P::SignProperties); |
| 2231 | Result.add(Ns); |
| 2232 | return true; |
| 2233 | } |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
| 2237 | // The Input cell contains some known values. Pick the word corresponding |
| 2238 | // to the subregister. |
| 2239 | APInt A; |
| 2240 | for (unsigned i = 0; i < Input.size(); ++i) { |
| 2241 | const Constant *C = Input.Values[i]; |
| 2242 | if (!constToInt(C, A)) |
| 2243 | return false; |
| 2244 | if (!A.isIntN(64)) |
| 2245 | return false; |
| 2246 | uint64_t U = A.getZExtValue(); |
| Krzysztof Parzyszek | a540997 | 2016-11-09 16:19:08 +0000 | [diff] [blame] | 2247 | if (R.SubReg == Hexagon::isub_hi) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2248 | U >>= 32; |
| 2249 | U &= 0xFFFFFFFFULL; |
| 2250 | uint32_t U32 = Lo_32(U); |
| 2251 | int32_t V32; |
| 2252 | memcpy(&V32, &U32, sizeof V32); |
| 2253 | IntegerType *Ty = Type::getInt32Ty(CX); |
| 2254 | const ConstantInt *C32 = ConstantInt::get(Ty, static_cast<int64_t>(V32)); |
| 2255 | Result.add(C32); |
| 2256 | } |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2260 | bool HexagonConstEvaluator::evaluate(const MachineInstr &BrI, |
| 2261 | const CellMap &Inputs, SetVector<const MachineBasicBlock*> &Targets, |
| 2262 | bool &FallsThru) { |
| 2263 | // We need to evaluate one branch at a time. TII::analyzeBranch checks |
| 2264 | // all the branches in a basic block at once, so we cannot use it. |
| 2265 | unsigned Opc = BrI.getOpcode(); |
| 2266 | bool SimpleBranch = false; |
| 2267 | bool Negated = false; |
| 2268 | switch (Opc) { |
| 2269 | case Hexagon::J2_jumpf: |
| 2270 | case Hexagon::J2_jumpfnew: |
| 2271 | case Hexagon::J2_jumpfnewpt: |
| 2272 | Negated = true; |
| Simon Pilgrim | adb80fb | 2017-07-07 10:04:12 +0000 | [diff] [blame] | 2273 | LLVM_FALLTHROUGH; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2274 | case Hexagon::J2_jumpt: |
| 2275 | case Hexagon::J2_jumptnew: |
| 2276 | case Hexagon::J2_jumptnewpt: |
| 2277 | // Simple branch: if([!]Pn) jump ... |
| 2278 | // i.e. Op0 = predicate, Op1 = branch target. |
| 2279 | SimpleBranch = true; |
| 2280 | break; |
| 2281 | case Hexagon::J2_jump: |
| 2282 | Targets.insert(BrI.getOperand(0).getMBB()); |
| 2283 | FallsThru = false; |
| 2284 | return true; |
| 2285 | default: |
| 2286 | Undetermined: |
| 2287 | // If the branch is of unknown type, assume that all successors are |
| 2288 | // executable. |
| 2289 | FallsThru = !BrI.isUnconditionalBranch(); |
| 2290 | return false; |
| 2291 | } |
| 2292 | |
| 2293 | if (SimpleBranch) { |
| 2294 | const MachineOperand &MD = BrI.getOperand(0); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2295 | RegisterSubReg PR(MD); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2296 | // If the condition operand has a subregister, this is not something |
| 2297 | // we currently recognize. |
| 2298 | if (PR.SubReg) |
| 2299 | goto Undetermined; |
| 2300 | assert(Inputs.has(PR.Reg)); |
| 2301 | const LatticeCell &PredC = Inputs.get(PR.Reg); |
| 2302 | if (PredC.isBottom()) |
| 2303 | goto Undetermined; |
| 2304 | |
| 2305 | uint32_t Props = PredC.properties(); |
| Mandeep Singh Grang | 5e1697e | 2017-06-06 05:08:36 +0000 | [diff] [blame] | 2306 | bool CTrue = false, CFalse = false; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2307 | if (Props & ConstantProperties::Zero) |
| 2308 | CFalse = true; |
| 2309 | else if (Props & ConstantProperties::NonZero) |
| 2310 | CTrue = true; |
| 2311 | // If the condition is not known to be either, bail out. |
| 2312 | if (!CTrue && !CFalse) |
| 2313 | goto Undetermined; |
| 2314 | |
| 2315 | const MachineBasicBlock *BranchTarget = BrI.getOperand(1).getMBB(); |
| 2316 | |
| 2317 | FallsThru = false; |
| 2318 | if ((!Negated && CTrue) || (Negated && CFalse)) |
| 2319 | Targets.insert(BranchTarget); |
| 2320 | else if ((!Negated && CFalse) || (Negated && CTrue)) |
| 2321 | FallsThru = true; |
| 2322 | else |
| 2323 | goto Undetermined; |
| 2324 | } |
| 2325 | |
| 2326 | return true; |
| 2327 | } |
| 2328 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2329 | bool HexagonConstEvaluator::rewrite(MachineInstr &MI, const CellMap &Inputs) { |
| 2330 | if (MI.isBranch()) |
| 2331 | return rewriteHexBranch(MI, Inputs); |
| 2332 | |
| 2333 | unsigned Opc = MI.getOpcode(); |
| 2334 | switch (Opc) { |
| 2335 | default: |
| 2336 | break; |
| 2337 | case Hexagon::A2_tfrsi: |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2338 | case Hexagon::A2_tfrpi: |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 2339 | case Hexagon::CONST32: |
| 2340 | case Hexagon::CONST64: |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 2341 | case Hexagon::PS_true: |
| 2342 | case Hexagon::PS_false: |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2343 | return false; |
| 2344 | } |
| 2345 | |
| 2346 | unsigned NumOp = MI.getNumOperands(); |
| 2347 | if (NumOp == 0) |
| 2348 | return false; |
| 2349 | |
| 2350 | bool AllDefs, Changed; |
| 2351 | Changed = rewriteHexConstDefs(MI, Inputs, AllDefs); |
| 2352 | // If not all defs have been rewritten (i.e. the instruction defines |
| 2353 | // a register that is not compile-time constant), then try to rewrite |
| 2354 | // register operands that are known to be constant with immediates. |
| 2355 | if (!AllDefs) |
| 2356 | Changed |= rewriteHexConstUses(MI, Inputs); |
| 2357 | |
| 2358 | return Changed; |
| 2359 | } |
| 2360 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2361 | unsigned HexagonConstEvaluator::getRegBitWidth(unsigned Reg) const { |
| 2362 | const TargetRegisterClass *RC = MRI->getRegClass(Reg); |
| 2363 | if (Hexagon::IntRegsRegClass.hasSubClassEq(RC)) |
| 2364 | return 32; |
| 2365 | if (Hexagon::DoubleRegsRegClass.hasSubClassEq(RC)) |
| 2366 | return 64; |
| 2367 | if (Hexagon::PredRegsRegClass.hasSubClassEq(RC)) |
| 2368 | return 8; |
| 2369 | llvm_unreachable("Invalid register"); |
| 2370 | return 0; |
| 2371 | } |
| 2372 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2373 | uint32_t HexagonConstEvaluator::getCmp(unsigned Opc) { |
| 2374 | switch (Opc) { |
| 2375 | case Hexagon::C2_cmpeq: |
| 2376 | case Hexagon::C2_cmpeqp: |
| 2377 | case Hexagon::A4_cmpbeq: |
| 2378 | case Hexagon::A4_cmpheq: |
| 2379 | case Hexagon::A4_cmpbeqi: |
| 2380 | case Hexagon::A4_cmpheqi: |
| 2381 | case Hexagon::C2_cmpeqi: |
| 2382 | case Hexagon::J4_cmpeqn1_t_jumpnv_nt: |
| 2383 | case Hexagon::J4_cmpeqn1_t_jumpnv_t: |
| 2384 | case Hexagon::J4_cmpeqi_t_jumpnv_nt: |
| 2385 | case Hexagon::J4_cmpeqi_t_jumpnv_t: |
| 2386 | case Hexagon::J4_cmpeq_t_jumpnv_nt: |
| 2387 | case Hexagon::J4_cmpeq_t_jumpnv_t: |
| 2388 | return Comparison::EQ; |
| 2389 | |
| 2390 | case Hexagon::C4_cmpneq: |
| 2391 | case Hexagon::C4_cmpneqi: |
| 2392 | case Hexagon::J4_cmpeqn1_f_jumpnv_nt: |
| 2393 | case Hexagon::J4_cmpeqn1_f_jumpnv_t: |
| 2394 | case Hexagon::J4_cmpeqi_f_jumpnv_nt: |
| 2395 | case Hexagon::J4_cmpeqi_f_jumpnv_t: |
| 2396 | case Hexagon::J4_cmpeq_f_jumpnv_nt: |
| 2397 | case Hexagon::J4_cmpeq_f_jumpnv_t: |
| 2398 | return Comparison::NE; |
| 2399 | |
| 2400 | case Hexagon::C2_cmpgt: |
| 2401 | case Hexagon::C2_cmpgtp: |
| 2402 | case Hexagon::A4_cmpbgt: |
| 2403 | case Hexagon::A4_cmphgt: |
| 2404 | case Hexagon::A4_cmpbgti: |
| 2405 | case Hexagon::A4_cmphgti: |
| 2406 | case Hexagon::C2_cmpgti: |
| 2407 | case Hexagon::J4_cmpgtn1_t_jumpnv_nt: |
| 2408 | case Hexagon::J4_cmpgtn1_t_jumpnv_t: |
| 2409 | case Hexagon::J4_cmpgti_t_jumpnv_nt: |
| 2410 | case Hexagon::J4_cmpgti_t_jumpnv_t: |
| 2411 | case Hexagon::J4_cmpgt_t_jumpnv_nt: |
| 2412 | case Hexagon::J4_cmpgt_t_jumpnv_t: |
| 2413 | return Comparison::GTs; |
| 2414 | |
| 2415 | case Hexagon::C4_cmplte: |
| 2416 | case Hexagon::C4_cmpltei: |
| 2417 | case Hexagon::J4_cmpgtn1_f_jumpnv_nt: |
| 2418 | case Hexagon::J4_cmpgtn1_f_jumpnv_t: |
| 2419 | case Hexagon::J4_cmpgti_f_jumpnv_nt: |
| 2420 | case Hexagon::J4_cmpgti_f_jumpnv_t: |
| 2421 | case Hexagon::J4_cmpgt_f_jumpnv_nt: |
| 2422 | case Hexagon::J4_cmpgt_f_jumpnv_t: |
| 2423 | return Comparison::LEs; |
| 2424 | |
| 2425 | case Hexagon::C2_cmpgtu: |
| 2426 | case Hexagon::C2_cmpgtup: |
| 2427 | case Hexagon::A4_cmpbgtu: |
| 2428 | case Hexagon::A4_cmpbgtui: |
| 2429 | case Hexagon::A4_cmphgtu: |
| 2430 | case Hexagon::A4_cmphgtui: |
| 2431 | case Hexagon::C2_cmpgtui: |
| 2432 | case Hexagon::J4_cmpgtui_t_jumpnv_nt: |
| 2433 | case Hexagon::J4_cmpgtui_t_jumpnv_t: |
| 2434 | case Hexagon::J4_cmpgtu_t_jumpnv_nt: |
| 2435 | case Hexagon::J4_cmpgtu_t_jumpnv_t: |
| 2436 | return Comparison::GTu; |
| 2437 | |
| 2438 | case Hexagon::J4_cmpltu_f_jumpnv_nt: |
| 2439 | case Hexagon::J4_cmpltu_f_jumpnv_t: |
| 2440 | return Comparison::GEu; |
| 2441 | |
| 2442 | case Hexagon::J4_cmpltu_t_jumpnv_nt: |
| 2443 | case Hexagon::J4_cmpltu_t_jumpnv_t: |
| 2444 | return Comparison::LTu; |
| 2445 | |
| 2446 | case Hexagon::J4_cmplt_f_jumpnv_nt: |
| 2447 | case Hexagon::J4_cmplt_f_jumpnv_t: |
| 2448 | return Comparison::GEs; |
| 2449 | |
| 2450 | case Hexagon::C4_cmplteu: |
| 2451 | case Hexagon::C4_cmplteui: |
| 2452 | case Hexagon::J4_cmpgtui_f_jumpnv_nt: |
| 2453 | case Hexagon::J4_cmpgtui_f_jumpnv_t: |
| 2454 | case Hexagon::J4_cmpgtu_f_jumpnv_nt: |
| 2455 | case Hexagon::J4_cmpgtu_f_jumpnv_t: |
| 2456 | return Comparison::LEu; |
| 2457 | |
| 2458 | case Hexagon::J4_cmplt_t_jumpnv_nt: |
| 2459 | case Hexagon::J4_cmplt_t_jumpnv_t: |
| 2460 | return Comparison::LTs; |
| 2461 | |
| 2462 | default: |
| 2463 | break; |
| 2464 | } |
| 2465 | return Comparison::Unk; |
| 2466 | } |
| 2467 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2468 | APInt HexagonConstEvaluator::getCmpImm(unsigned Opc, unsigned OpX, |
| 2469 | const MachineOperand &MO) { |
| 2470 | bool Signed = false; |
| 2471 | switch (Opc) { |
| 2472 | case Hexagon::A4_cmpbgtui: // u7 |
| 2473 | case Hexagon::A4_cmphgtui: // u7 |
| 2474 | break; |
| 2475 | case Hexagon::A4_cmpheqi: // s8 |
| 2476 | case Hexagon::C4_cmpneqi: // s8 |
| 2477 | Signed = true; |
| Reid Kleckner | 4dc0b1a | 2018-11-01 19:54:45 +0000 | [diff] [blame] | 2478 | break; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2479 | case Hexagon::A4_cmpbeqi: // u8 |
| 2480 | break; |
| 2481 | case Hexagon::C2_cmpgtui: // u9 |
| 2482 | case Hexagon::C4_cmplteui: // u9 |
| 2483 | break; |
| 2484 | case Hexagon::C2_cmpeqi: // s10 |
| 2485 | case Hexagon::C2_cmpgti: // s10 |
| 2486 | case Hexagon::C4_cmpltei: // s10 |
| 2487 | Signed = true; |
| 2488 | break; |
| 2489 | case Hexagon::J4_cmpeqi_f_jumpnv_nt: // u5 |
| 2490 | case Hexagon::J4_cmpeqi_f_jumpnv_t: // u5 |
| 2491 | case Hexagon::J4_cmpeqi_t_jumpnv_nt: // u5 |
| 2492 | case Hexagon::J4_cmpeqi_t_jumpnv_t: // u5 |
| 2493 | case Hexagon::J4_cmpgti_f_jumpnv_nt: // u5 |
| 2494 | case Hexagon::J4_cmpgti_f_jumpnv_t: // u5 |
| 2495 | case Hexagon::J4_cmpgti_t_jumpnv_nt: // u5 |
| 2496 | case Hexagon::J4_cmpgti_t_jumpnv_t: // u5 |
| 2497 | case Hexagon::J4_cmpgtui_f_jumpnv_nt: // u5 |
| 2498 | case Hexagon::J4_cmpgtui_f_jumpnv_t: // u5 |
| 2499 | case Hexagon::J4_cmpgtui_t_jumpnv_nt: // u5 |
| 2500 | case Hexagon::J4_cmpgtui_t_jumpnv_t: // u5 |
| 2501 | break; |
| 2502 | default: |
| 2503 | llvm_unreachable("Unhandled instruction"); |
| 2504 | break; |
| 2505 | } |
| 2506 | |
| 2507 | uint64_t Val = MO.getImm(); |
| 2508 | return APInt(32, Val, Signed); |
| 2509 | } |
| 2510 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2511 | void HexagonConstEvaluator::replaceWithNop(MachineInstr &MI) { |
| 2512 | MI.setDesc(HII.get(Hexagon::A2_nop)); |
| 2513 | while (MI.getNumOperands() > 0) |
| 2514 | MI.RemoveOperand(0); |
| 2515 | } |
| 2516 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2517 | bool HexagonConstEvaluator::evaluateHexRSEQ32(RegisterSubReg RL, RegisterSubReg RH, |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2518 | const CellMap &Inputs, LatticeCell &Result) { |
| 2519 | assert(Inputs.has(RL.Reg) && Inputs.has(RH.Reg)); |
| 2520 | LatticeCell LSL, LSH; |
| 2521 | if (!getCell(RL, Inputs, LSL) || !getCell(RH, Inputs, LSH)) |
| 2522 | return false; |
| 2523 | if (LSL.isProperty() || LSH.isProperty()) |
| 2524 | return false; |
| 2525 | |
| 2526 | unsigned LN = LSL.size(), HN = LSH.size(); |
| 2527 | SmallVector<APInt,4> LoVs(LN), HiVs(HN); |
| 2528 | for (unsigned i = 0; i < LN; ++i) { |
| 2529 | bool Eval = constToInt(LSL.Values[i], LoVs[i]); |
| 2530 | if (!Eval) |
| 2531 | return false; |
| 2532 | assert(LoVs[i].getBitWidth() == 32); |
| 2533 | } |
| 2534 | for (unsigned i = 0; i < HN; ++i) { |
| 2535 | bool Eval = constToInt(LSH.Values[i], HiVs[i]); |
| 2536 | if (!Eval) |
| 2537 | return false; |
| 2538 | assert(HiVs[i].getBitWidth() == 32); |
| 2539 | } |
| 2540 | |
| 2541 | for (unsigned i = 0; i < HiVs.size(); ++i) { |
| 2542 | APInt HV = HiVs[i].zextOrSelf(64) << 32; |
| 2543 | for (unsigned j = 0; j < LoVs.size(); ++j) { |
| 2544 | APInt LV = LoVs[j].zextOrSelf(64); |
| 2545 | const Constant *C = intToConst(HV | LV); |
| 2546 | Result.add(C); |
| 2547 | if (Result.isBottom()) |
| 2548 | return false; |
| 2549 | } |
| 2550 | } |
| 2551 | return !Result.isBottom(); |
| 2552 | } |
| 2553 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2554 | bool HexagonConstEvaluator::evaluateHexCompare(const MachineInstr &MI, |
| 2555 | const CellMap &Inputs, CellMap &Outputs) { |
| 2556 | unsigned Opc = MI.getOpcode(); |
| 2557 | bool Classic = false; |
| 2558 | switch (Opc) { |
| 2559 | case Hexagon::C2_cmpeq: |
| 2560 | case Hexagon::C2_cmpeqp: |
| 2561 | case Hexagon::C2_cmpgt: |
| 2562 | case Hexagon::C2_cmpgtp: |
| 2563 | case Hexagon::C2_cmpgtu: |
| 2564 | case Hexagon::C2_cmpgtup: |
| 2565 | case Hexagon::C2_cmpeqi: |
| 2566 | case Hexagon::C2_cmpgti: |
| 2567 | case Hexagon::C2_cmpgtui: |
| 2568 | // Classic compare: Dst0 = CMP Src1, Src2 |
| 2569 | Classic = true; |
| 2570 | break; |
| 2571 | default: |
| 2572 | // Not handling other compare instructions now. |
| 2573 | return false; |
| 2574 | } |
| 2575 | |
| 2576 | if (Classic) { |
| 2577 | const MachineOperand &Src1 = MI.getOperand(1); |
| 2578 | const MachineOperand &Src2 = MI.getOperand(2); |
| 2579 | |
| 2580 | bool Result; |
| 2581 | unsigned Opc = MI.getOpcode(); |
| 2582 | bool Computed = evaluateHexCompare2(Opc, Src1, Src2, Inputs, Result); |
| 2583 | if (Computed) { |
| 2584 | // Only create a zero/non-zero cell. At this time there isn't really |
| 2585 | // much need for specific values. |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2586 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2587 | LatticeCell L = Outputs.get(DefR.Reg); |
| 2588 | uint32_t P = Result ? ConstantProperties::NonZero |
| 2589 | : ConstantProperties::Zero; |
| 2590 | L.add(P); |
| 2591 | Outputs.update(DefR.Reg, L); |
| 2592 | return true; |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | return false; |
| 2597 | } |
| 2598 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2599 | bool HexagonConstEvaluator::evaluateHexCompare2(unsigned Opc, |
| 2600 | const MachineOperand &Src1, const MachineOperand &Src2, |
| 2601 | const CellMap &Inputs, bool &Result) { |
| 2602 | uint32_t Cmp = getCmp(Opc); |
| 2603 | bool Reg1 = Src1.isReg(), Reg2 = Src2.isReg(); |
| 2604 | bool Imm1 = Src1.isImm(), Imm2 = Src2.isImm(); |
| 2605 | if (Reg1) { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2606 | RegisterSubReg R1(Src1); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2607 | if (Reg2) { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2608 | RegisterSubReg R2(Src2); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2609 | return evaluateCMPrr(Cmp, R1, R2, Inputs, Result); |
| 2610 | } else if (Imm2) { |
| 2611 | APInt A2 = getCmpImm(Opc, 2, Src2); |
| 2612 | return evaluateCMPri(Cmp, R1, A2, Inputs, Result); |
| 2613 | } |
| 2614 | } else if (Imm1) { |
| 2615 | APInt A1 = getCmpImm(Opc, 1, Src1); |
| 2616 | if (Reg2) { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2617 | RegisterSubReg R2(Src2); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2618 | uint32_t NegCmp = Comparison::negate(Cmp); |
| 2619 | return evaluateCMPri(NegCmp, R2, A1, Inputs, Result); |
| 2620 | } else if (Imm2) { |
| 2621 | APInt A2 = getCmpImm(Opc, 2, Src2); |
| 2622 | return evaluateCMPii(Cmp, A1, A2, Result); |
| 2623 | } |
| 2624 | } |
| 2625 | // Unknown kind of comparison. |
| 2626 | return false; |
| 2627 | } |
| 2628 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2629 | bool HexagonConstEvaluator::evaluateHexLogical(const MachineInstr &MI, |
| 2630 | const CellMap &Inputs, CellMap &Outputs) { |
| 2631 | unsigned Opc = MI.getOpcode(); |
| 2632 | if (MI.getNumOperands() != 3) |
| 2633 | return false; |
| 2634 | const MachineOperand &Src1 = MI.getOperand(1); |
| 2635 | const MachineOperand &Src2 = MI.getOperand(2); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2636 | RegisterSubReg R1(Src1); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2637 | bool Eval = false; |
| 2638 | LatticeCell RC; |
| 2639 | switch (Opc) { |
| 2640 | default: |
| 2641 | return false; |
| 2642 | case Hexagon::A2_and: |
| 2643 | case Hexagon::A2_andp: |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2644 | Eval = evaluateANDrr(R1, RegisterSubReg(Src2), Inputs, RC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2645 | break; |
| 2646 | case Hexagon::A2_andir: { |
| Krzysztof Parzyszek | 96690ce | 2018-02-23 20:33:26 +0000 | [diff] [blame] | 2647 | if (!Src2.isImm()) |
| 2648 | return false; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2649 | APInt A(32, Src2.getImm(), true); |
| 2650 | Eval = evaluateANDri(R1, A, Inputs, RC); |
| 2651 | break; |
| 2652 | } |
| 2653 | case Hexagon::A2_or: |
| 2654 | case Hexagon::A2_orp: |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2655 | Eval = evaluateORrr(R1, RegisterSubReg(Src2), Inputs, RC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2656 | break; |
| 2657 | case Hexagon::A2_orir: { |
| Krzysztof Parzyszek | 96690ce | 2018-02-23 20:33:26 +0000 | [diff] [blame] | 2658 | if (!Src2.isImm()) |
| 2659 | return false; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2660 | APInt A(32, Src2.getImm(), true); |
| 2661 | Eval = evaluateORri(R1, A, Inputs, RC); |
| 2662 | break; |
| 2663 | } |
| 2664 | case Hexagon::A2_xor: |
| 2665 | case Hexagon::A2_xorp: |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2666 | Eval = evaluateXORrr(R1, RegisterSubReg(Src2), Inputs, RC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2667 | break; |
| 2668 | } |
| 2669 | if (Eval) { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2670 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2671 | Outputs.update(DefR.Reg, RC); |
| 2672 | } |
| 2673 | return Eval; |
| 2674 | } |
| 2675 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2676 | bool HexagonConstEvaluator::evaluateHexCondMove(const MachineInstr &MI, |
| 2677 | const CellMap &Inputs, CellMap &Outputs) { |
| 2678 | // Dst0 = Cond1 ? Src2 : Src3 |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2679 | RegisterSubReg CR(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2680 | assert(Inputs.has(CR.Reg)); |
| 2681 | LatticeCell LS; |
| 2682 | if (!getCell(CR, Inputs, LS)) |
| 2683 | return false; |
| 2684 | uint32_t Ps = LS.properties(); |
| 2685 | unsigned TakeOp; |
| 2686 | if (Ps & ConstantProperties::Zero) |
| 2687 | TakeOp = 3; |
| 2688 | else if (Ps & ConstantProperties::NonZero) |
| 2689 | TakeOp = 2; |
| 2690 | else |
| 2691 | return false; |
| 2692 | |
| 2693 | const MachineOperand &ValOp = MI.getOperand(TakeOp); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2694 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2695 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2696 | |
| 2697 | if (ValOp.isImm()) { |
| 2698 | int64_t V = ValOp.getImm(); |
| 2699 | unsigned W = getRegBitWidth(DefR.Reg); |
| 2700 | APInt A(W, V, true); |
| 2701 | const Constant *C = intToConst(A); |
| 2702 | RC.add(C); |
| 2703 | Outputs.update(DefR.Reg, RC); |
| 2704 | return true; |
| 2705 | } |
| 2706 | if (ValOp.isReg()) { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2707 | RegisterSubReg R(ValOp); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2708 | const LatticeCell &LR = Inputs.get(R.Reg); |
| 2709 | LatticeCell LSR; |
| 2710 | if (!evaluate(R, LR, LSR)) |
| 2711 | return false; |
| 2712 | RC.meet(LSR); |
| 2713 | Outputs.update(DefR.Reg, RC); |
| 2714 | return true; |
| 2715 | } |
| 2716 | return false; |
| 2717 | } |
| 2718 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2719 | bool HexagonConstEvaluator::evaluateHexExt(const MachineInstr &MI, |
| 2720 | const CellMap &Inputs, CellMap &Outputs) { |
| 2721 | // Dst0 = ext R1 |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2722 | RegisterSubReg R1(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2723 | assert(Inputs.has(R1.Reg)); |
| 2724 | |
| 2725 | unsigned Opc = MI.getOpcode(); |
| 2726 | unsigned Bits; |
| 2727 | switch (Opc) { |
| 2728 | case Hexagon::A2_sxtb: |
| 2729 | case Hexagon::A2_zxtb: |
| 2730 | Bits = 8; |
| 2731 | break; |
| 2732 | case Hexagon::A2_sxth: |
| 2733 | case Hexagon::A2_zxth: |
| 2734 | Bits = 16; |
| 2735 | break; |
| 2736 | case Hexagon::A2_sxtw: |
| 2737 | Bits = 32; |
| 2738 | break; |
| Simon Pilgrim | e7c5113 | 2019-05-11 15:24:34 +0000 | [diff] [blame] | 2739 | default: |
| 2740 | llvm_unreachable("Unhandled extension opcode"); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | bool Signed = false; |
| 2744 | switch (Opc) { |
| 2745 | case Hexagon::A2_sxtb: |
| 2746 | case Hexagon::A2_sxth: |
| 2747 | case Hexagon::A2_sxtw: |
| 2748 | Signed = true; |
| 2749 | break; |
| 2750 | } |
| 2751 | |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2752 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2753 | unsigned BW = getRegBitWidth(DefR.Reg); |
| 2754 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2755 | bool Eval = Signed ? evaluateSEXTr(R1, BW, Bits, Inputs, RC) |
| 2756 | : evaluateZEXTr(R1, BW, Bits, Inputs, RC); |
| 2757 | if (!Eval) |
| 2758 | return false; |
| 2759 | Outputs.update(DefR.Reg, RC); |
| 2760 | return true; |
| 2761 | } |
| 2762 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2763 | bool HexagonConstEvaluator::evaluateHexVector1(const MachineInstr &MI, |
| 2764 | const CellMap &Inputs, CellMap &Outputs) { |
| 2765 | // DefR = op R1 |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2766 | RegisterSubReg DefR(MI.getOperand(0)); |
| 2767 | RegisterSubReg R1(MI.getOperand(1)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2768 | assert(Inputs.has(R1.Reg)); |
| 2769 | LatticeCell RC = Outputs.get(DefR.Reg); |
| 2770 | bool Eval; |
| 2771 | |
| 2772 | unsigned Opc = MI.getOpcode(); |
| 2773 | switch (Opc) { |
| 2774 | case Hexagon::S2_vsplatrb: |
| 2775 | // Rd = 4 times Rs:0..7 |
| 2776 | Eval = evaluateSplatr(R1, 8, 4, Inputs, RC); |
| 2777 | break; |
| 2778 | case Hexagon::S2_vsplatrh: |
| 2779 | // Rdd = 4 times Rs:0..15 |
| 2780 | Eval = evaluateSplatr(R1, 16, 4, Inputs, RC); |
| 2781 | break; |
| 2782 | default: |
| 2783 | return false; |
| 2784 | } |
| 2785 | |
| 2786 | if (!Eval) |
| 2787 | return false; |
| 2788 | Outputs.update(DefR.Reg, RC); |
| 2789 | return true; |
| 2790 | } |
| 2791 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2792 | bool HexagonConstEvaluator::rewriteHexConstDefs(MachineInstr &MI, |
| 2793 | const CellMap &Inputs, bool &AllDefs) { |
| 2794 | AllDefs = false; |
| 2795 | |
| 2796 | // Some diagnostics. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2797 | // LLVM_DEBUG({...}) gets confused with all this code as an argument. |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2798 | #ifndef NDEBUG |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 2799 | bool Debugging = DebugFlag && isCurrentDebugType(DEBUG_TYPE); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2800 | if (Debugging) { |
| 2801 | bool Const = true, HasUse = false; |
| 2802 | for (const MachineOperand &MO : MI.operands()) { |
| 2803 | if (!MO.isReg() || !MO.isUse() || MO.isImplicit()) |
| 2804 | continue; |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2805 | RegisterSubReg R(MO); |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 2806 | if (!Register::isVirtualRegister(R.Reg)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2807 | continue; |
| 2808 | HasUse = true; |
| 2809 | // PHIs can legitimately have "top" cells after propagation. |
| 2810 | if (!MI.isPHI() && !Inputs.has(R.Reg)) { |
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 2811 | dbgs() << "Top " << printReg(R.Reg, &HRI, R.SubReg) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2812 | << " in MI: " << MI; |
| 2813 | continue; |
| 2814 | } |
| 2815 | const LatticeCell &L = Inputs.get(R.Reg); |
| 2816 | Const &= L.isSingle(); |
| 2817 | if (!Const) |
| 2818 | break; |
| 2819 | } |
| 2820 | if (HasUse && Const) { |
| 2821 | if (!MI.isCopy()) { |
| 2822 | dbgs() << "CONST: " << MI; |
| 2823 | for (const MachineOperand &MO : MI.operands()) { |
| 2824 | if (!MO.isReg() || !MO.isUse() || MO.isImplicit()) |
| 2825 | continue; |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2826 | Register R = MO.getReg(); |
| Francis Visoiu Mistrih | 9d419d3 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 2827 | dbgs() << printReg(R, &TRI) << ": " << Inputs.get(R) << "\n"; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2828 | } |
| 2829 | } |
| 2830 | } |
| 2831 | } |
| 2832 | #endif |
| 2833 | |
| 2834 | // Avoid generating TFRIs for register transfers---this will keep the |
| 2835 | // coalescing opportunities. |
| 2836 | if (MI.isCopy()) |
| 2837 | return false; |
| 2838 | |
| Krzysztof Parzyszek | 305bf5b | 2020-01-13 16:07:30 -0600 | [diff] [blame] | 2839 | MachineFunction *MF = MI.getParent()->getParent(); |
| 2840 | auto &HST = MF->getSubtarget<HexagonSubtarget>(); |
| 2841 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2842 | // Collect all virtual register-def operands. |
| 2843 | SmallVector<unsigned,2> DefRegs; |
| 2844 | for (const MachineOperand &MO : MI.operands()) { |
| 2845 | if (!MO.isReg() || !MO.isDef()) |
| 2846 | continue; |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2847 | Register R = MO.getReg(); |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 2848 | if (!Register::isVirtualRegister(R)) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2849 | continue; |
| 2850 | assert(!MO.getSubReg()); |
| 2851 | assert(Inputs.has(R)); |
| 2852 | DefRegs.push_back(R); |
| 2853 | } |
| 2854 | |
| 2855 | MachineBasicBlock &B = *MI.getParent(); |
| 2856 | const DebugLoc &DL = MI.getDebugLoc(); |
| 2857 | unsigned ChangedNum = 0; |
| 2858 | #ifndef NDEBUG |
| 2859 | SmallVector<const MachineInstr*,4> NewInstrs; |
| 2860 | #endif |
| 2861 | |
| 2862 | // For each defined register, if it is a constant, create an instruction |
| 2863 | // NewR = const |
| 2864 | // and replace all uses of the defined register with NewR. |
| 2865 | for (unsigned i = 0, n = DefRegs.size(); i < n; ++i) { |
| 2866 | unsigned R = DefRegs[i]; |
| 2867 | const LatticeCell &L = Inputs.get(R); |
| 2868 | if (L.isBottom()) |
| 2869 | continue; |
| 2870 | const TargetRegisterClass *RC = MRI->getRegClass(R); |
| 2871 | MachineBasicBlock::iterator At = MI.getIterator(); |
| 2872 | |
| 2873 | if (!L.isSingle()) { |
| 2874 | // If this a zero/non-zero cell, we can fold a definition |
| 2875 | // of a predicate register. |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 2876 | using P = ConstantProperties; |
| 2877 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2878 | uint64_t Ps = L.properties(); |
| 2879 | if (!(Ps & (P::Zero|P::NonZero))) |
| 2880 | continue; |
| 2881 | const TargetRegisterClass *PredRC = &Hexagon::PredRegsRegClass; |
| 2882 | if (RC != PredRC) |
| 2883 | continue; |
| 2884 | const MCInstrDesc *NewD = (Ps & P::Zero) ? |
| Krzysztof Parzyszek | 1d01a79 | 2016-08-16 18:08:40 +0000 | [diff] [blame] | 2885 | &HII.get(Hexagon::PS_false) : |
| 2886 | &HII.get(Hexagon::PS_true); |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2887 | Register NewR = MRI->createVirtualRegister(PredRC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2888 | const MachineInstrBuilder &MIB = BuildMI(B, At, DL, *NewD, NewR); |
| 2889 | (void)MIB; |
| 2890 | #ifndef NDEBUG |
| 2891 | NewInstrs.push_back(&*MIB); |
| 2892 | #endif |
| 2893 | replaceAllRegUsesWith(R, NewR); |
| 2894 | } else { |
| 2895 | // This cell has a single value. |
| 2896 | APInt A; |
| 2897 | if (!constToInt(L.Value, A) || !A.isSignedIntN(64)) |
| 2898 | continue; |
| 2899 | const TargetRegisterClass *NewRC; |
| 2900 | const MCInstrDesc *NewD; |
| 2901 | |
| 2902 | unsigned W = getRegBitWidth(R); |
| 2903 | int64_t V = A.getSExtValue(); |
| 2904 | assert(W == 32 || W == 64); |
| 2905 | if (W == 32) |
| 2906 | NewRC = &Hexagon::IntRegsRegClass; |
| 2907 | else |
| 2908 | NewRC = &Hexagon::DoubleRegsRegClass; |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 2909 | Register NewR = MRI->createVirtualRegister(NewRC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2910 | const MachineInstr *NewMI; |
| 2911 | |
| 2912 | if (W == 32) { |
| 2913 | NewD = &HII.get(Hexagon::A2_tfrsi); |
| 2914 | NewMI = BuildMI(B, At, DL, *NewD, NewR) |
| 2915 | .addImm(V); |
| 2916 | } else { |
| 2917 | if (A.isSignedIntN(8)) { |
| 2918 | NewD = &HII.get(Hexagon::A2_tfrpi); |
| 2919 | NewMI = BuildMI(B, At, DL, *NewD, NewR) |
| 2920 | .addImm(V); |
| 2921 | } else { |
| 2922 | int32_t Hi = V >> 32; |
| 2923 | int32_t Lo = V & 0xFFFFFFFFLL; |
| 2924 | if (isInt<8>(Hi) && isInt<8>(Lo)) { |
| 2925 | NewD = &HII.get(Hexagon::A2_combineii); |
| 2926 | NewMI = BuildMI(B, At, DL, *NewD, NewR) |
| 2927 | .addImm(Hi) |
| 2928 | .addImm(Lo); |
| Krzysztof Parzyszek | 305bf5b | 2020-01-13 16:07:30 -0600 | [diff] [blame] | 2929 | } else if (MF->getFunction().hasOptSize() || !HST.isTinyCore()) { |
| 2930 | // Disable CONST64 for tiny core since it takes a LD resource. |
| Krzysztof Parzyszek | a338650 | 2016-08-10 16:46:36 +0000 | [diff] [blame] | 2931 | NewD = &HII.get(Hexagon::CONST64); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2932 | NewMI = BuildMI(B, At, DL, *NewD, NewR) |
| 2933 | .addImm(V); |
| Krzysztof Parzyszek | 305bf5b | 2020-01-13 16:07:30 -0600 | [diff] [blame] | 2934 | } else |
| 2935 | return false; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2936 | } |
| 2937 | } |
| 2938 | (void)NewMI; |
| 2939 | #ifndef NDEBUG |
| 2940 | NewInstrs.push_back(NewMI); |
| 2941 | #endif |
| 2942 | replaceAllRegUsesWith(R, NewR); |
| 2943 | } |
| 2944 | ChangedNum++; |
| 2945 | } |
| 2946 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 2947 | LLVM_DEBUG({ |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2948 | if (!NewInstrs.empty()) { |
| 2949 | MachineFunction &MF = *MI.getParent()->getParent(); |
| Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 2950 | dbgs() << "In function: " << MF.getName() << "\n"; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2951 | dbgs() << "Rewrite: for " << MI << " created " << *NewInstrs[0]; |
| 2952 | for (unsigned i = 1; i < NewInstrs.size(); ++i) |
| 2953 | dbgs() << " " << *NewInstrs[i]; |
| 2954 | } |
| 2955 | }); |
| 2956 | |
| 2957 | AllDefs = (ChangedNum == DefRegs.size()); |
| 2958 | return ChangedNum > 0; |
| 2959 | } |
| 2960 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2961 | bool HexagonConstEvaluator::rewriteHexConstUses(MachineInstr &MI, |
| 2962 | const CellMap &Inputs) { |
| 2963 | bool Changed = false; |
| 2964 | unsigned Opc = MI.getOpcode(); |
| 2965 | MachineBasicBlock &B = *MI.getParent(); |
| 2966 | const DebugLoc &DL = MI.getDebugLoc(); |
| 2967 | MachineBasicBlock::iterator At = MI.getIterator(); |
| Eugene Zelenko | f9f8c68 | 2016-12-14 22:50:46 +0000 | [diff] [blame] | 2968 | MachineInstr *NewMI = nullptr; |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2969 | |
| 2970 | switch (Opc) { |
| 2971 | case Hexagon::M2_maci: |
| 2972 | // Convert DefR += mpyi(R2, R3) |
| 2973 | // to DefR += mpyi(R, #imm), |
| 2974 | // or DefR -= mpyi(R, #imm). |
| 2975 | { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2976 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2977 | assert(!DefR.SubReg); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2978 | RegisterSubReg R2(MI.getOperand(2)); |
| 2979 | RegisterSubReg R3(MI.getOperand(3)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2980 | assert(Inputs.has(R2.Reg) && Inputs.has(R3.Reg)); |
| 2981 | LatticeCell LS2, LS3; |
| 2982 | // It is enough to get one of the input cells, since we will only try |
| 2983 | // to replace one argument---whichever happens to be a single constant. |
| 2984 | bool HasC2 = getCell(R2, Inputs, LS2), HasC3 = getCell(R3, Inputs, LS3); |
| 2985 | if (!HasC2 && !HasC3) |
| 2986 | return false; |
| 2987 | bool Zero = ((HasC2 && (LS2.properties() & ConstantProperties::Zero)) || |
| 2988 | (HasC3 && (LS3.properties() & ConstantProperties::Zero))); |
| 2989 | // If one of the operands is zero, eliminate the multiplication. |
| 2990 | if (Zero) { |
| 2991 | // DefR == R1 (tied operands). |
| 2992 | MachineOperand &Acc = MI.getOperand(1); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 2993 | RegisterSubReg R1(Acc); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 2994 | unsigned NewR = R1.Reg; |
| 2995 | if (R1.SubReg) { |
| 2996 | // Generate COPY. FIXME: Replace with the register:subregister. |
| 2997 | const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg); |
| 2998 | NewR = MRI->createVirtualRegister(RC); |
| 2999 | NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR) |
| 3000 | .addReg(R1.Reg, getRegState(Acc), R1.SubReg); |
| 3001 | } |
| 3002 | replaceAllRegUsesWith(DefR.Reg, NewR); |
| 3003 | MRI->clearKillFlags(NewR); |
| 3004 | Changed = true; |
| 3005 | break; |
| 3006 | } |
| 3007 | |
| 3008 | bool Swap = false; |
| 3009 | if (!LS3.isSingle()) { |
| 3010 | if (!LS2.isSingle()) |
| 3011 | return false; |
| 3012 | Swap = true; |
| 3013 | } |
| 3014 | const LatticeCell &LI = Swap ? LS2 : LS3; |
| 3015 | const MachineOperand &OpR2 = Swap ? MI.getOperand(3) |
| 3016 | : MI.getOperand(2); |
| 3017 | // LI is single here. |
| 3018 | APInt A; |
| 3019 | if (!constToInt(LI.Value, A) || !A.isSignedIntN(8)) |
| 3020 | return false; |
| 3021 | int64_t V = A.getSExtValue(); |
| 3022 | const MCInstrDesc &D = (V >= 0) ? HII.get(Hexagon::M2_macsip) |
| 3023 | : HII.get(Hexagon::M2_macsin); |
| 3024 | if (V < 0) |
| 3025 | V = -V; |
| 3026 | const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg); |
| Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 3027 | Register NewR = MRI->createVirtualRegister(RC); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3028 | const MachineOperand &Src1 = MI.getOperand(1); |
| 3029 | NewMI = BuildMI(B, At, DL, D, NewR) |
| 3030 | .addReg(Src1.getReg(), getRegState(Src1), Src1.getSubReg()) |
| 3031 | .addReg(OpR2.getReg(), getRegState(OpR2), OpR2.getSubReg()) |
| 3032 | .addImm(V); |
| 3033 | replaceAllRegUsesWith(DefR.Reg, NewR); |
| 3034 | Changed = true; |
| 3035 | break; |
| 3036 | } |
| 3037 | |
| 3038 | case Hexagon::A2_and: |
| 3039 | { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 3040 | RegisterSubReg R1(MI.getOperand(1)); |
| 3041 | RegisterSubReg R2(MI.getOperand(2)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3042 | assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg)); |
| 3043 | LatticeCell LS1, LS2; |
| 3044 | unsigned CopyOf = 0; |
| 3045 | // Check if any of the operands is -1 (i.e. all bits set). |
| 3046 | if (getCell(R1, Inputs, LS1) && LS1.isSingle()) { |
| 3047 | APInt M1; |
| 3048 | if (constToInt(LS1.Value, M1) && !~M1) |
| 3049 | CopyOf = 2; |
| 3050 | } |
| 3051 | else if (getCell(R2, Inputs, LS2) && LS2.isSingle()) { |
| 3052 | APInt M1; |
| 3053 | if (constToInt(LS2.Value, M1) && !~M1) |
| 3054 | CopyOf = 1; |
| 3055 | } |
| 3056 | if (!CopyOf) |
| 3057 | return false; |
| 3058 | MachineOperand &SO = MI.getOperand(CopyOf); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 3059 | RegisterSubReg SR(SO); |
| 3060 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3061 | unsigned NewR = SR.Reg; |
| 3062 | if (SR.SubReg) { |
| 3063 | const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg); |
| 3064 | NewR = MRI->createVirtualRegister(RC); |
| 3065 | NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR) |
| 3066 | .addReg(SR.Reg, getRegState(SO), SR.SubReg); |
| 3067 | } |
| 3068 | replaceAllRegUsesWith(DefR.Reg, NewR); |
| 3069 | MRI->clearKillFlags(NewR); |
| 3070 | Changed = true; |
| 3071 | } |
| 3072 | break; |
| 3073 | |
| 3074 | case Hexagon::A2_or: |
| 3075 | { |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 3076 | RegisterSubReg R1(MI.getOperand(1)); |
| 3077 | RegisterSubReg R2(MI.getOperand(2)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3078 | assert(Inputs.has(R1.Reg) && Inputs.has(R2.Reg)); |
| 3079 | LatticeCell LS1, LS2; |
| 3080 | unsigned CopyOf = 0; |
| Eugene Zelenko | e4fc6ee | 2017-07-26 23:20:35 +0000 | [diff] [blame] | 3081 | |
| 3082 | using P = ConstantProperties; |
| 3083 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3084 | if (getCell(R1, Inputs, LS1) && (LS1.properties() & P::Zero)) |
| 3085 | CopyOf = 2; |
| 3086 | else if (getCell(R2, Inputs, LS2) && (LS2.properties() & P::Zero)) |
| 3087 | CopyOf = 1; |
| 3088 | if (!CopyOf) |
| 3089 | return false; |
| 3090 | MachineOperand &SO = MI.getOperand(CopyOf); |
| Matt Arsenault | ae171f1 | 2019-06-24 16:16:19 +0000 | [diff] [blame] | 3091 | RegisterSubReg SR(SO); |
| 3092 | RegisterSubReg DefR(MI.getOperand(0)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3093 | unsigned NewR = SR.Reg; |
| 3094 | if (SR.SubReg) { |
| 3095 | const TargetRegisterClass *RC = MRI->getRegClass(DefR.Reg); |
| 3096 | NewR = MRI->createVirtualRegister(RC); |
| 3097 | NewMI = BuildMI(B, At, DL, HII.get(TargetOpcode::COPY), NewR) |
| 3098 | .addReg(SR.Reg, getRegState(SO), SR.SubReg); |
| 3099 | } |
| 3100 | replaceAllRegUsesWith(DefR.Reg, NewR); |
| 3101 | MRI->clearKillFlags(NewR); |
| 3102 | Changed = true; |
| 3103 | } |
| 3104 | break; |
| 3105 | } |
| 3106 | |
| 3107 | if (NewMI) { |
| 3108 | // clear all the kill flags of this new instruction. |
| 3109 | for (MachineOperand &MO : NewMI->operands()) |
| 3110 | if (MO.isReg() && MO.isUse()) |
| 3111 | MO.setIsKill(false); |
| 3112 | } |
| 3113 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3114 | LLVM_DEBUG({ |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3115 | if (NewMI) { |
| 3116 | dbgs() << "Rewrite: for " << MI; |
| 3117 | if (NewMI != &MI) |
| 3118 | dbgs() << " created " << *NewMI; |
| 3119 | else |
| 3120 | dbgs() << " modified the instruction itself and created:" << *NewMI; |
| 3121 | } |
| 3122 | }); |
| 3123 | |
| 3124 | return Changed; |
| 3125 | } |
| 3126 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3127 | void HexagonConstEvaluator::replaceAllRegUsesWith(unsigned FromReg, |
| 3128 | unsigned ToReg) { |
| Daniel Sanders | 2bea69b | 2019-08-01 23:27:28 +0000 | [diff] [blame] | 3129 | assert(Register::isVirtualRegister(FromReg)); |
| 3130 | assert(Register::isVirtualRegister(ToReg)); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3131 | for (auto I = MRI->use_begin(FromReg), E = MRI->use_end(); I != E;) { |
| 3132 | MachineOperand &O = *I; |
| 3133 | ++I; |
| 3134 | O.setReg(ToReg); |
| 3135 | } |
| 3136 | } |
| 3137 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3138 | bool HexagonConstEvaluator::rewriteHexBranch(MachineInstr &BrI, |
| 3139 | const CellMap &Inputs) { |
| 3140 | MachineBasicBlock &B = *BrI.getParent(); |
| 3141 | unsigned NumOp = BrI.getNumOperands(); |
| 3142 | if (!NumOp) |
| 3143 | return false; |
| 3144 | |
| 3145 | bool FallsThru; |
| 3146 | SetVector<const MachineBasicBlock*> Targets; |
| 3147 | bool Eval = evaluate(BrI, Inputs, Targets, FallsThru); |
| 3148 | unsigned NumTargets = Targets.size(); |
| 3149 | if (!Eval || NumTargets > 1 || (NumTargets == 1 && FallsThru)) |
| 3150 | return false; |
| 3151 | if (BrI.getOpcode() == Hexagon::J2_jump) |
| 3152 | return false; |
| 3153 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 3154 | LLVM_DEBUG(dbgs() << "Rewrite(" << printMBBReference(B) << "):" << BrI); |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3155 | bool Rewritten = false; |
| 3156 | if (NumTargets > 0) { |
| 3157 | assert(!FallsThru && "This should have been checked before"); |
| 3158 | // MIB.addMBB needs non-const pointer. |
| 3159 | MachineBasicBlock *TargetB = const_cast<MachineBasicBlock*>(Targets[0]); |
| 3160 | bool Moot = B.isLayoutSuccessor(TargetB); |
| 3161 | if (!Moot) { |
| 3162 | // If we build a branch here, we must make sure that it won't be |
| 3163 | // erased as "non-executable". We can't mark any new instructions |
| 3164 | // as executable here, so we need to overwrite the BrI, which we |
| 3165 | // know is executable. |
| 3166 | const MCInstrDesc &JD = HII.get(Hexagon::J2_jump); |
| 3167 | auto NI = BuildMI(B, BrI.getIterator(), BrI.getDebugLoc(), JD) |
| 3168 | .addMBB(TargetB); |
| 3169 | BrI.setDesc(JD); |
| 3170 | while (BrI.getNumOperands() > 0) |
| 3171 | BrI.RemoveOperand(0); |
| Francis Visoiu Mistrih | a8a83d1 | 2017-12-07 10:40:31 +0000 | [diff] [blame] | 3172 | // This ensures that all implicit operands (e.g. implicit-def %r31, etc) |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3173 | // are present in the rewritten branch. |
| 3174 | for (auto &Op : NI->operands()) |
| 3175 | BrI.addOperand(Op); |
| 3176 | NI->eraseFromParent(); |
| 3177 | Rewritten = true; |
| 3178 | } |
| 3179 | } |
| 3180 | |
| 3181 | // Do not erase instructions. A newly created instruction could get |
| 3182 | // the same address as an instruction marked as executable during the |
| 3183 | // propagation. |
| 3184 | if (!Rewritten) |
| 3185 | replaceWithNop(BrI); |
| 3186 | return true; |
| 3187 | } |
| 3188 | |
| Krzysztof Parzyszek | 167d918 | 2016-07-28 20:01:59 +0000 | [diff] [blame] | 3189 | FunctionPass *llvm::createHexagonConstPropagationPass() { |
| 3190 | return new HexagonConstPropagation(); |
| 3191 | } |