Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 1 | //===---- lib/CodeGen/GlobalISel/LegalizerInfo.cpp - Legalizer -------==// |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Implement an interface to specify and query how an illegal operation on a |
| 11 | // given type should be expanded. |
| 12 | // |
| 13 | // Issues to be resolved: |
| 14 | // + Make it fast. |
| 15 | // + Support weird types like i3, <7 x i3>, ... |
| 16 | // + Operations with more than one type (ICMP, CMPXCHG, intrinsics, ...) |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/GlobalISel/LegalizerInfo.h" |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 21 | |
| 22 | #include "llvm/ADT/SmallBitVector.h" |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstr.h" |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/ValueTypes.h" |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Type.h" |
| 27 | #include "llvm/Target/TargetOpcodes.h" |
| 28 | using namespace llvm; |
| 29 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 30 | LegalizerInfo::LegalizerInfo() : TablesInitialized(false) { |
Tim Northover | 3233581 | 2016-08-04 18:35:11 +0000 | [diff] [blame] | 31 | // FIXME: these two can be legalized to the fundamental load/store Jakob |
| 32 | // proposed. Once loads & stores are supported. |
Tim Northover | bdf67c9 | 2016-08-23 21:01:33 +0000 | [diff] [blame] | 33 | DefaultActions[TargetOpcode::G_ANYEXT] = Legal; |
Tim Northover | 3233581 | 2016-08-04 18:35:11 +0000 | [diff] [blame] | 34 | DefaultActions[TargetOpcode::G_TRUNC] = Legal; |
| 35 | |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 36 | DefaultActions[TargetOpcode::G_INTRINSIC] = Legal; |
| 37 | DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal; |
| 38 | |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 39 | DefaultActions[TargetOpcode::G_ADD] = NarrowScalar; |
Tim Northover | cdf23f1 | 2016-10-31 18:30:59 +0000 | [diff] [blame] | 40 | DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar; |
| 41 | DefaultActions[TargetOpcode::G_STORE] = NarrowScalar; |
Tim Northover | b3a0be4 | 2016-08-23 21:01:20 +0000 | [diff] [blame] | 42 | |
| 43 | DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar; |
Tim Northover | 0e6afbd | 2017-02-06 21:56:47 +0000 | [diff] [blame] | 44 | DefaultActions[TargetOpcode::G_INSERT] = NarrowScalar; |
Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 45 | DefaultActions[TargetOpcode::G_FNEG] = Lower; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 48 | void LegalizerInfo::computeTables() { |
Tim Northover | f8bab1c | 2016-08-29 21:00:00 +0000 | [diff] [blame] | 49 | for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) { |
| 50 | for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) { |
| 51 | for (auto &Action : Actions[Opcode][Idx]) { |
| 52 | LLT Ty = Action.first; |
| 53 | if (!Ty.isVector()) |
| 54 | continue; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 55 | |
Tim Northover | f8bab1c | 2016-08-29 21:00:00 +0000 | [diff] [blame] | 56 | auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp, |
| 57 | Ty.getElementType())]; |
| 58 | Entry = std::max(Entry, Ty.getNumElements()); |
| 59 | } |
| 60 | } |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | TablesInitialized = true; |
| 64 | } |
| 65 | |
| 66 | // FIXME: inefficient implementation for now. Without ComputeValueVTs we're |
| 67 | // probably going to need specialized lookup structures for various types before |
| 68 | // we have any hope of doing well with something like <13 x i3>. Even the common |
| 69 | // cases should do better than what we have now. |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 70 | std::pair<LegalizerInfo::LegalizeAction, LLT> |
| 71 | LegalizerInfo::getAction(const InstrAspect &Aspect) const { |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 72 | assert(TablesInitialized && "backend forgot to call computeTables"); |
| 73 | // These *have* to be implemented for now, they're the fundamental basis of |
| 74 | // how everything else is transformed. |
| 75 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 76 | // FIXME: the long-term plan calls for expansion in terms of load/store (if |
| 77 | // they're not legal). |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 78 | if (Aspect.Opcode == TargetOpcode::G_SEQUENCE || |
Tim Northover | bf01729 | 2017-03-03 22:46:09 +0000 | [diff] [blame] | 79 | Aspect.Opcode == TargetOpcode::G_EXTRACT || |
| 80 | Aspect.Opcode == TargetOpcode::G_MERGE_VALUES || |
| 81 | Aspect.Opcode == TargetOpcode::G_UNMERGE_VALUES) |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 82 | return std::make_pair(Legal, Aspect.Type); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 83 | |
Volkan Keles | 64ad85f | 2017-04-11 10:10:14 +0000 | [diff] [blame] | 84 | LLT Ty = Aspect.Type; |
Tim Northover | f8bab1c | 2016-08-29 21:00:00 +0000 | [diff] [blame] | 85 | LegalizeAction Action = findInActions(Aspect); |
Volkan Keles | 64ad85f | 2017-04-11 10:10:14 +0000 | [diff] [blame] | 86 | // LegalizerHelper is not able to handle non-power-of-2 types right now, so do |
| 87 | // not try to legalize them unless they are marked as Legal or Custom. |
| 88 | // FIXME: This is a temporary hack until the general non-power-of-2 |
| 89 | // legalization works. |
| 90 | if (!isPowerOf2_64(Ty.getSizeInBits()) && |
| 91 | !(Action == Legal || Action == Custom)) |
| 92 | return std::make_pair(Unsupported, LLT()); |
| 93 | |
Tim Northover | f8bab1c | 2016-08-29 21:00:00 +0000 | [diff] [blame] | 94 | if (Action != NotFound) |
| 95 | return findLegalAction(Aspect, Action); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 96 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 97 | unsigned Opcode = Aspect.Opcode; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 98 | if (!Ty.isVector()) { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 99 | auto DefaultAction = DefaultActions.find(Aspect.Opcode); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 100 | if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal) |
| 101 | return std::make_pair(Legal, Ty); |
| 102 | |
Volkan Keles | 5698b2a | 2017-03-08 18:09:14 +0000 | [diff] [blame] | 103 | if (DefaultAction != DefaultActions.end() && DefaultAction->second == Lower) |
| 104 | return std::make_pair(Lower, Ty); |
| 105 | |
Tim Northover | 0a683e7 | 2016-12-06 19:02:15 +0000 | [diff] [blame] | 106 | if (DefaultAction == DefaultActions.end() || |
| 107 | DefaultAction->second != NarrowScalar) |
| 108 | return std::make_pair(Unsupported, LLT()); |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 109 | return findLegalAction(Aspect, NarrowScalar); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | LLT EltTy = Ty.getElementType(); |
| 113 | int NumElts = Ty.getNumElements(); |
| 114 | |
| 115 | auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy)); |
| 116 | if (ScalarAction != ScalarInVectorActions.end() && |
| 117 | ScalarAction->second != Legal) |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 118 | return findLegalAction(Aspect, ScalarAction->second); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 119 | |
| 120 | // The element type is legal in principle, but the number of elements is |
| 121 | // wrong. |
| 122 | auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy)); |
| 123 | if (MaxLegalElts > NumElts) |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 124 | return findLegalAction(Aspect, MoreElements); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 125 | |
| 126 | if (MaxLegalElts == 0) { |
| 127 | // Scalarize if there's no legal vector type, which is just a special case |
| 128 | // of FewerElements. |
| 129 | return std::make_pair(FewerElements, EltTy); |
| 130 | } |
| 131 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 132 | return findLegalAction(Aspect, FewerElements); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 135 | std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT> |
| 136 | LegalizerInfo::getAction(const MachineInstr &MI, |
| 137 | const MachineRegisterInfo &MRI) const { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 138 | SmallBitVector SeenTypes(8); |
| 139 | const MCOperandInfo *OpInfo = MI.getDesc().OpInfo; |
| 140 | for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) { |
| 141 | if (!OpInfo[i].isGenericType()) |
| 142 | continue; |
| 143 | |
| 144 | // We don't want to repeatedly check the same operand index, that |
| 145 | // could get expensive. |
| 146 | unsigned TypeIdx = OpInfo[i].getGenericTypeIndex(); |
| 147 | if (SeenTypes[TypeIdx]) |
| 148 | continue; |
| 149 | |
| 150 | SeenTypes.set(TypeIdx); |
| 151 | |
| 152 | LLT Ty = MRI.getType(MI.getOperand(i).getReg()); |
| 153 | auto Action = getAction({MI.getOpcode(), TypeIdx, Ty}); |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 154 | if (Action.first != Legal) |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 155 | return std::make_tuple(Action.first, TypeIdx, Action.second); |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 156 | } |
Richard Smith | 84c4cc4 | 2016-08-23 22:21:58 +0000 | [diff] [blame] | 157 | return std::make_tuple(Legal, 0, LLT{}); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 160 | bool LegalizerInfo::isLegal(const MachineInstr &MI, |
| 161 | const MachineRegisterInfo &MRI) const { |
Tim Northover | 0f140c7 | 2016-09-09 11:46:34 +0000 | [diff] [blame] | 162 | return std::get<0>(getAction(MI, MRI)) == Legal; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Tim Northover | 69fa84a | 2016-10-14 22:18:18 +0000 | [diff] [blame] | 165 | LLT LegalizerInfo::findLegalType(const InstrAspect &Aspect, |
| 166 | LegalizeAction Action) const { |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 167 | switch(Action) { |
| 168 | default: |
| 169 | llvm_unreachable("Cannot find legal type"); |
| 170 | case Legal: |
Tim Northover | cecee56 | 2016-08-26 17:46:13 +0000 | [diff] [blame] | 171 | case Lower: |
Tim Northover | edb3c8c | 2016-08-29 19:07:16 +0000 | [diff] [blame] | 172 | case Libcall: |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 173 | case Custom: |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 174 | return Aspect.Type; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 175 | case NarrowScalar: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 176 | return findLegalType(Aspect, |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 177 | [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); }); |
| 178 | } |
| 179 | case WidenScalar: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 180 | return findLegalType(Aspect, [&](LLT Ty) -> LLT { |
Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 181 | return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize(); |
| 182 | }); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 183 | } |
| 184 | case FewerElements: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 185 | return findLegalType(Aspect, |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 186 | [&](LLT Ty) -> LLT { return Ty.halfElements(); }); |
| 187 | } |
| 188 | case MoreElements: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame] | 189 | return findLegalType(Aspect, |
| 190 | [&](LLT Ty) -> LLT { return Ty.doubleElements(); }); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
Tim Northover | 9136617 | 2017-02-15 23:22:50 +0000 | [diff] [blame] | 194 | |
| 195 | bool LegalizerInfo::legalizeCustom(MachineInstr &MI, |
| 196 | MachineRegisterInfo &MRI, |
| 197 | MachineIRBuilder &MIRBuilder) const { |
| 198 | return false; |
| 199 | } |