Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 1 | //===-- MCInstrDescView.cpp -------------------------------------*- C++ -*-===// |
| 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 |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "MCInstrDescView.h" |
| 10 | |
| 11 | #include <iterator> |
| 12 | #include <map> |
| 13 | #include <tuple> |
| 14 | |
| 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 17 | namespace llvm { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 18 | namespace exegesis { |
| 19 | |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 20 | unsigned Variable::getIndex() const { |
Guillaume Chatelet | fe5b0b4 | 2018-10-09 10:06:19 +0000 | [diff] [blame] | 21 | assert(Index >= 0 && "Index must be set"); |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 22 | return Index; |
| 23 | } |
Guillaume Chatelet | fe5b0b4 | 2018-10-09 10:06:19 +0000 | [diff] [blame] | 24 | |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 25 | unsigned Variable::getPrimaryOperandIndex() const { |
| 26 | assert(!TiedOperands.empty()); |
| 27 | return TiedOperands[0]; |
| 28 | } |
| 29 | |
Guillaume Chatelet | fcbb6f3 | 2018-10-17 11:37:28 +0000 | [diff] [blame] | 30 | bool Variable::hasTiedOperands() const { |
| 31 | assert(TiedOperands.size() <= 2 && |
| 32 | "No more than two operands can be tied together"); |
| 33 | // By definition only Use and Def operands can be tied together. |
| 34 | // TiedOperands[0] is the Def operand (LLVM stores defs first). |
| 35 | // TiedOperands[1] is the Use operand. |
| 36 | return TiedOperands.size() > 1; |
| 37 | } |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 38 | |
Guillaume Chatelet | 547d2dd | 2018-10-09 14:51:29 +0000 | [diff] [blame] | 39 | unsigned Operand::getIndex() const { |
| 40 | assert(Index >= 0 && "Index must be set"); |
| 41 | return Index; |
| 42 | } |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 43 | |
| 44 | bool Operand::isExplicit() const { return Info; } |
| 45 | |
| 46 | bool Operand::isImplicit() const { return !Info; } |
| 47 | |
| 48 | bool Operand::isImplicitReg() const { return ImplicitReg; } |
| 49 | |
| 50 | bool Operand::isDef() const { return IsDef; } |
| 51 | |
| 52 | bool Operand::isUse() const { return !IsDef; } |
| 53 | |
| 54 | bool Operand::isReg() const { return Tracker; } |
| 55 | |
| 56 | bool Operand::isTied() const { return TiedToIndex >= 0; } |
| 57 | |
| 58 | bool Operand::isVariable() const { return VariableIndex >= 0; } |
| 59 | |
| 60 | bool Operand::isMemory() const { |
| 61 | return isExplicit() && |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 62 | getExplicitOperandInfo().OperandType == MCOI::OPERAND_MEMORY; |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | bool Operand::isImmediate() const { |
| 66 | return isExplicit() && |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 67 | getExplicitOperandInfo().OperandType == MCOI::OPERAND_IMMEDIATE; |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guillaume Chatelet | 547d2dd | 2018-10-09 14:51:29 +0000 | [diff] [blame] | 70 | unsigned Operand::getTiedToIndex() const { |
| 71 | assert(isTied() && "Operand must be tied to get the tied index"); |
| 72 | assert(TiedToIndex >= 0 && "TiedToIndex must be set"); |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 73 | return TiedToIndex; |
| 74 | } |
| 75 | |
Guillaume Chatelet | 547d2dd | 2018-10-09 14:51:29 +0000 | [diff] [blame] | 76 | unsigned Operand::getVariableIndex() const { |
| 77 | assert(isVariable() && "Operand must be variable to get the Variable index"); |
| 78 | assert(VariableIndex >= 0 && "VariableIndex must be set"); |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 79 | return VariableIndex; |
| 80 | } |
| 81 | |
| 82 | unsigned Operand::getImplicitReg() const { |
| 83 | assert(ImplicitReg); |
| 84 | return *ImplicitReg; |
| 85 | } |
| 86 | |
| 87 | const RegisterAliasingTracker &Operand::getRegisterAliasing() const { |
| 88 | assert(Tracker); |
| 89 | return *Tracker; |
| 90 | } |
| 91 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 92 | const MCOperandInfo &Operand::getExplicitOperandInfo() const { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 93 | assert(Info); |
| 94 | return *Info; |
| 95 | } |
| 96 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 97 | Instruction::Instruction(const MCInstrInfo &InstrInfo, |
Guillaume Chatelet | da11b85 | 2018-10-24 11:55:06 +0000 | [diff] [blame] | 98 | const RegisterAliasingTrackerCache &RATC, |
| 99 | unsigned Opcode) |
| 100 | : Description(&InstrInfo.get(Opcode)), Name(InstrInfo.getName(Opcode)) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 101 | unsigned OpIndex = 0; |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 102 | for (; OpIndex < Description->getNumOperands(); ++OpIndex) { |
| 103 | const auto &OpInfo = Description->opInfo_begin()[OpIndex]; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 104 | Operand Operand; |
| 105 | Operand.Index = OpIndex; |
Guillaume Chatelet | ee9c2a17 | 2018-10-10 14:22:48 +0000 | [diff] [blame] | 106 | Operand.IsDef = (OpIndex < Description->getNumDefs()); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 107 | // TODO(gchatelet): Handle isLookupPtrRegClass. |
| 108 | if (OpInfo.RegClass >= 0) |
| 109 | Operand.Tracker = &RATC.getRegisterClass(OpInfo.RegClass); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 110 | Operand.TiedToIndex = |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 111 | Description->getOperandConstraint(OpIndex, MCOI::TIED_TO); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 112 | Operand.Info = &OpInfo; |
| 113 | Operands.push_back(Operand); |
| 114 | } |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 115 | for (const MCPhysReg *MCPhysReg = Description->getImplicitDefs(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 116 | MCPhysReg && *MCPhysReg; ++MCPhysReg, ++OpIndex) { |
| 117 | Operand Operand; |
| 118 | Operand.Index = OpIndex; |
| 119 | Operand.IsDef = true; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 120 | Operand.Tracker = &RATC.getRegister(*MCPhysReg); |
| 121 | Operand.ImplicitReg = MCPhysReg; |
| 122 | Operands.push_back(Operand); |
| 123 | } |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 124 | for (const MCPhysReg *MCPhysReg = Description->getImplicitUses(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 125 | MCPhysReg && *MCPhysReg; ++MCPhysReg, ++OpIndex) { |
| 126 | Operand Operand; |
| 127 | Operand.Index = OpIndex; |
| 128 | Operand.IsDef = false; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 129 | Operand.Tracker = &RATC.getRegister(*MCPhysReg); |
| 130 | Operand.ImplicitReg = MCPhysReg; |
| 131 | Operands.push_back(Operand); |
| 132 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 133 | // Assigning Variables to non tied explicit operands. |
| 134 | Variables.reserve(Operands.size()); // Variables.size() <= Operands.size() |
| 135 | for (auto &Op : Operands) |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 136 | if (Op.isExplicit() && !Op.isTied()) { |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 137 | const size_t VariableIndex = Variables.size(); |
| 138 | Op.VariableIndex = VariableIndex; |
| 139 | Variables.emplace_back(); |
| 140 | Variables.back().Index = VariableIndex; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 141 | } |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 142 | // Assigning Variables to tied operands. |
| 143 | for (auto &Op : Operands) |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 144 | if (Op.isTied()) |
| 145 | Op.VariableIndex = Operands[Op.getTiedToIndex()].getVariableIndex(); |
Guillaume Chatelet | c9f727b | 2018-06-13 13:24:41 +0000 | [diff] [blame] | 146 | // Assigning Operands to Variables. |
| 147 | for (auto &Op : Operands) |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 148 | if (Op.isVariable()) |
| 149 | Variables[Op.getVariableIndex()].TiedOperands.push_back(Op.getIndex()); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 150 | // Processing Aliasing. |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 151 | ImplDefRegs = RATC.emptyRegisters(); |
| 152 | ImplUseRegs = RATC.emptyRegisters(); |
| 153 | AllDefRegs = RATC.emptyRegisters(); |
| 154 | AllUseRegs = RATC.emptyRegisters(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 155 | for (const auto &Op : Operands) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 156 | if (Op.isReg()) { |
| 157 | const auto &AliasingBits = Op.getRegisterAliasing().aliasedBits(); |
| 158 | if (Op.isDef()) |
| 159 | AllDefRegs |= AliasingBits; |
| 160 | if (Op.isUse()) |
| 161 | AllUseRegs |= AliasingBits; |
| 162 | if (Op.isDef() && Op.isImplicit()) |
| 163 | ImplDefRegs |= AliasingBits; |
| 164 | if (Op.isUse() && Op.isImplicit()) |
| 165 | ImplUseRegs |= AliasingBits; |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 170 | const Operand &Instruction::getPrimaryOperand(const Variable &Var) const { |
| 171 | const auto PrimaryOperandIndex = Var.getPrimaryOperandIndex(); |
| 172 | assert(PrimaryOperandIndex < Operands.size()); |
| 173 | return Operands[PrimaryOperandIndex]; |
| 174 | } |
| 175 | |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 176 | bool Instruction::hasMemoryOperands() const { |
Fangrui Song | 2e83b2e | 2018-10-19 06:12:02 +0000 | [diff] [blame] | 177 | return any_of(Operands, [](const Operand &Op) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 178 | return Op.isReg() && Op.isExplicit() && Op.isMemory(); |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | bool Instruction::hasAliasingImplicitRegisters() const { |
| 183 | return ImplDefRegs.anyCommon(ImplUseRegs); |
| 184 | } |
| 185 | |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 186 | // Returns true if there are registers that are both in `A` and `B` but not in |
| 187 | // `Forbidden`. |
| 188 | static bool anyCommonExcludingForbidden(const BitVector &A, const BitVector &B, |
| 189 | const BitVector &Forbidden) { |
| 190 | assert(A.size() == B.size() && B.size() == Forbidden.size()); |
| 191 | const auto Size = A.size(); |
| 192 | for (int AIndex = A.find_first(); AIndex != -1;) { |
| 193 | const int BIndex = B.find_first_in(AIndex, Size); |
| 194 | if (BIndex == -1) |
| 195 | return false; |
| 196 | if (AIndex == BIndex && !Forbidden.test(AIndex)) |
| 197 | return true; |
| 198 | AIndex = A.find_first_in(BIndex + 1, Size); |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 202 | |
Guillaume Chatelet | 0c17cbf | 2018-10-10 09:12:36 +0000 | [diff] [blame] | 203 | bool Instruction::hasAliasingRegistersThrough( |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 204 | const Instruction &OtherInstr, const BitVector &ForbiddenRegisters) const { |
| 205 | return anyCommonExcludingForbidden(AllDefRegs, OtherInstr.AllUseRegs, |
| 206 | ForbiddenRegisters) && |
| 207 | anyCommonExcludingForbidden(OtherInstr.AllDefRegs, AllUseRegs, |
| 208 | ForbiddenRegisters); |
Guillaume Chatelet | 0c17cbf | 2018-10-10 09:12:36 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 211 | bool Instruction::hasTiedRegisters() const { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 212 | return any_of(Variables, |
| 213 | [](const Variable &Var) { return Var.hasTiedOperands(); }); |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 216 | bool Instruction::hasAliasingRegisters( |
| 217 | const BitVector &ForbiddenRegisters) const { |
| 218 | return anyCommonExcludingForbidden(AllDefRegs, AllUseRegs, |
| 219 | ForbiddenRegisters); |
Guillaume Chatelet | fb94354 | 2018-08-01 14:41:45 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Guillaume Chatelet | fcbb6f3 | 2018-10-17 11:37:28 +0000 | [diff] [blame] | 222 | bool Instruction::hasOneUseOrOneDef() const { |
| 223 | return AllDefRegs.count() || AllUseRegs.count(); |
| 224 | } |
| 225 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 226 | void Instruction::dump(const MCRegisterInfo &RegInfo, |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 227 | const RegisterAliasingTrackerCache &RATC, |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 228 | raw_ostream &Stream) const { |
Guillaume Chatelet | 9b59238 | 2018-10-10 14:57:32 +0000 | [diff] [blame] | 229 | Stream << "- " << Name << "\n"; |
Guillaume Chatelet | 547d2dd | 2018-10-09 14:51:29 +0000 | [diff] [blame] | 230 | for (const auto &Op : Operands) { |
| 231 | Stream << "- Op" << Op.getIndex(); |
| 232 | if (Op.isExplicit()) |
| 233 | Stream << " Explicit"; |
| 234 | if (Op.isImplicit()) |
| 235 | Stream << " Implicit"; |
| 236 | if (Op.isUse()) |
| 237 | Stream << " Use"; |
| 238 | if (Op.isDef()) |
| 239 | Stream << " Def"; |
| 240 | if (Op.isImmediate()) |
| 241 | Stream << " Immediate"; |
| 242 | if (Op.isMemory()) |
| 243 | Stream << " Memory"; |
| 244 | if (Op.isReg()) { |
| 245 | if (Op.isImplicitReg()) |
| 246 | Stream << " Reg(" << RegInfo.getName(Op.getImplicitReg()) << ")"; |
| 247 | else |
| 248 | Stream << " RegClass(" |
| 249 | << RegInfo.getRegClassName( |
| 250 | &RegInfo.getRegClass(Op.Info->RegClass)) |
| 251 | << ")"; |
| 252 | } |
| 253 | if (Op.isTied()) |
| 254 | Stream << " TiedToOp" << Op.getTiedToIndex(); |
| 255 | Stream << "\n"; |
| 256 | } |
| 257 | for (const auto &Var : Variables) { |
| 258 | Stream << "- Var" << Var.getIndex(); |
Guillaume Chatelet | 9b59238 | 2018-10-10 14:57:32 +0000 | [diff] [blame] | 259 | Stream << " ["; |
| 260 | bool IsFirst = true; |
| 261 | for (auto OperandIndex : Var.TiedOperands) { |
| 262 | if (!IsFirst) |
| 263 | Stream << ","; |
Guillaume Chatelet | 0c17cbf | 2018-10-10 09:12:36 +0000 | [diff] [blame] | 264 | Stream << "Op" << OperandIndex; |
Guillaume Chatelet | 9b59238 | 2018-10-10 14:57:32 +0000 | [diff] [blame] | 265 | IsFirst = false; |
| 266 | } |
| 267 | Stream << "]"; |
Guillaume Chatelet | 547d2dd | 2018-10-09 14:51:29 +0000 | [diff] [blame] | 268 | Stream << "\n"; |
| 269 | } |
| 270 | if (hasMemoryOperands()) |
| 271 | Stream << "- hasMemoryOperands\n"; |
| 272 | if (hasAliasingImplicitRegisters()) |
| 273 | Stream << "- hasAliasingImplicitRegisters (execution is always serial)\n"; |
| 274 | if (hasTiedRegisters()) |
| 275 | Stream << "- hasTiedRegisters (execution is always serial)\n"; |
Clement Courbet | 8ef97e1 | 2019-09-27 08:04:10 +0000 | [diff] [blame] | 276 | if (hasAliasingRegisters(RATC.emptyRegisters())) |
Guillaume Chatelet | 547d2dd | 2018-10-09 14:51:29 +0000 | [diff] [blame] | 277 | Stream << "- hasAliasingRegisters\n"; |
| 278 | } |
| 279 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 280 | InstructionsCache::InstructionsCache(const MCInstrInfo &InstrInfo, |
Guillaume Chatelet | da11b85 | 2018-10-24 11:55:06 +0000 | [diff] [blame] | 281 | const RegisterAliasingTrackerCache &RATC) |
| 282 | : InstrInfo(InstrInfo), RATC(RATC) {} |
| 283 | |
| 284 | const Instruction &InstructionsCache::getInstr(unsigned Opcode) const { |
| 285 | auto &Found = Instructions[Opcode]; |
| 286 | if (!Found) |
| 287 | Found.reset(new Instruction(InstrInfo, RATC, Opcode)); |
| 288 | return *Found; |
| 289 | } |
| 290 | |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 291 | bool RegisterOperandAssignment:: |
| 292 | operator==(const RegisterOperandAssignment &Other) const { |
| 293 | return std::tie(Op, Reg) == std::tie(Other.Op, Other.Reg); |
| 294 | } |
| 295 | |
| 296 | bool AliasingRegisterOperands:: |
| 297 | operator==(const AliasingRegisterOperands &Other) const { |
| 298 | return std::tie(Defs, Uses) == std::tie(Other.Defs, Other.Uses); |
| 299 | } |
| 300 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 301 | static void |
| 302 | addOperandIfAlias(const MCPhysReg Reg, bool SelectDef, |
| 303 | ArrayRef<Operand> Operands, |
| 304 | SmallVectorImpl<RegisterOperandAssignment> &OperandValues) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 305 | for (const auto &Op : Operands) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 306 | if (Op.isReg() && Op.isDef() == SelectDef) { |
| 307 | const int SourceReg = Op.getRegisterAliasing().getOrigin(Reg); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 308 | if (SourceReg >= 0) |
| 309 | OperandValues.emplace_back(&Op, SourceReg); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | bool AliasingRegisterOperands::hasImplicitAliasing() const { |
| 315 | const auto HasImplicit = [](const RegisterOperandAssignment &ROV) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 316 | return ROV.Op->isImplicit(); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 317 | }; |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 318 | return any_of(Defs, HasImplicit) && any_of(Uses, HasImplicit); |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | bool AliasingConfigurations::empty() const { return Configurations.empty(); } |
| 322 | |
| 323 | bool AliasingConfigurations::hasImplicitAliasing() const { |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 324 | return any_of(Configurations, [](const AliasingRegisterOperands &ARO) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 325 | return ARO.hasImplicitAliasing(); |
| 326 | }); |
| 327 | } |
| 328 | |
| 329 | AliasingConfigurations::AliasingConfigurations( |
Guillaume Chatelet | fcbb6f3 | 2018-10-17 11:37:28 +0000 | [diff] [blame] | 330 | const Instruction &DefInstruction, const Instruction &UseInstruction) { |
Guillaume Chatelet | 09c2839 | 2018-10-09 08:59:10 +0000 | [diff] [blame] | 331 | if (UseInstruction.AllUseRegs.anyCommon(DefInstruction.AllDefRegs)) { |
| 332 | auto CommonRegisters = UseInstruction.AllUseRegs; |
| 333 | CommonRegisters &= DefInstruction.AllDefRegs; |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 334 | for (const MCPhysReg Reg : CommonRegisters.set_bits()) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 335 | AliasingRegisterOperands ARO; |
| 336 | addOperandIfAlias(Reg, true, DefInstruction.Operands, ARO.Defs); |
| 337 | addOperandIfAlias(Reg, false, UseInstruction.Operands, ARO.Uses); |
| 338 | if (!ARO.Defs.empty() && !ARO.Uses.empty() && |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 339 | !is_contained(Configurations, ARO)) |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 340 | Configurations.push_back(std::move(ARO)); |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 345 | void DumpMCOperand(const MCRegisterInfo &MCRegisterInfo, const MCOperand &Op, |
| 346 | raw_ostream &OS) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 347 | if (!Op.isValid()) |
| 348 | OS << "Invalid"; |
| 349 | else if (Op.isReg()) |
| 350 | OS << MCRegisterInfo.getName(Op.getReg()); |
| 351 | else if (Op.isImm()) |
| 352 | OS << Op.getImm(); |
| 353 | else if (Op.isFPImm()) |
| 354 | OS << Op.getFPImm(); |
| 355 | else if (Op.isExpr()) |
| 356 | OS << "Expr"; |
| 357 | else if (Op.isInst()) |
| 358 | OS << "SubInst"; |
| 359 | } |
| 360 | |
Clement Courbet | 50cdd56 | 2019-10-09 11:58:42 +0000 | [diff] [blame^] | 361 | void DumpMCInst(const MCRegisterInfo &MCRegisterInfo, |
| 362 | const MCInstrInfo &MCInstrInfo, const MCInst &MCInst, |
| 363 | raw_ostream &OS) { |
Clement Courbet | 0e69e2d | 2018-05-17 10:52:18 +0000 | [diff] [blame] | 364 | OS << MCInstrInfo.getName(MCInst.getOpcode()); |
| 365 | for (unsigned I = 0, E = MCInst.getNumOperands(); I < E; ++I) { |
| 366 | if (I > 0) |
| 367 | OS << ','; |
| 368 | OS << ' '; |
| 369 | DumpMCOperand(MCRegisterInfo, MCInst.getOperand(I), OS); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | } // namespace exegesis |
Fangrui Song | 32401af | 2018-10-22 17:10:47 +0000 | [diff] [blame] | 374 | } // namespace llvm |