Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 1 | //===---- lib/CodeGen/GlobalISel/MachineLegalizer.cpp - IRTranslator -------==// |
| 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 | |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
| 21 | #include "llvm/CodeGen/ValueTypes.h" |
| 22 | #include "llvm/CodeGen/GlobalISel/MachineLegalizer.h" |
| 23 | #include "llvm/IR/Type.h" |
| 24 | #include "llvm/Target/TargetOpcodes.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | MachineLegalizer::MachineLegalizer() : TablesInitialized(false) { |
Tim Northover | 3233581 | 2016-08-04 18:35:11 +0000 | [diff] [blame] | 28 | // FIXME: these two can be legalized to the fundamental load/store Jakob |
| 29 | // proposed. Once loads & stores are supported. |
| 30 | DefaultActions[TargetOpcode::G_ANYEXTEND] = Legal; |
| 31 | DefaultActions[TargetOpcode::G_TRUNC] = Legal; |
| 32 | |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 33 | DefaultActions[TargetOpcode::G_ADD] = NarrowScalar; |
| 34 | } |
| 35 | |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 36 | void MachineLegalizer::computeTables() { |
| 37 | for (auto &Op : Actions) { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 38 | LLT Ty = Op.first.Type; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 39 | if (!Ty.isVector()) |
| 40 | continue; |
| 41 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 42 | auto &Entry = MaxLegalVectorElts[std::make_pair(Op.first.Opcode, |
| 43 | Ty.getElementType())]; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 44 | Entry = std::max(Entry, Ty.getNumElements()); |
| 45 | } |
| 46 | |
| 47 | TablesInitialized = true; |
| 48 | } |
| 49 | |
| 50 | // FIXME: inefficient implementation for now. Without ComputeValueVTs we're |
| 51 | // probably going to need specialized lookup structures for various types before |
| 52 | // we have any hope of doing well with something like <13 x i3>. Even the common |
| 53 | // cases should do better than what we have now. |
| 54 | std::pair<MachineLegalizer::LegalizeAction, LLT> |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 55 | MachineLegalizer::getAction(const InstrAspect &Aspect) const { |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 56 | assert(TablesInitialized && "backend forgot to call computeTables"); |
| 57 | // These *have* to be implemented for now, they're the fundamental basis of |
| 58 | // how everything else is transformed. |
| 59 | |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 60 | // FIXME: the long-term plan calls for expansion in terms of load/store (if |
| 61 | // they're not legal). |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 62 | if (Aspect.Opcode == TargetOpcode::G_SEQUENCE || |
| 63 | Aspect.Opcode == TargetOpcode::G_EXTRACT) |
| 64 | return std::make_pair(Legal, Aspect.Type); |
Tim Northover | 33b07d6 | 2016-07-22 20:03:43 +0000 | [diff] [blame] | 65 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 66 | auto ActionIt = Actions.find(Aspect); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 67 | if (ActionIt != Actions.end()) |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 68 | return findLegalAction(Aspect, ActionIt->second); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 69 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 70 | unsigned Opcode = Aspect.Opcode; |
| 71 | LLT Ty = Aspect.Type; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 72 | if (!Ty.isVector()) { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 73 | auto DefaultAction = DefaultActions.find(Aspect.Opcode); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 74 | if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal) |
| 75 | return std::make_pair(Legal, Ty); |
| 76 | |
| 77 | assert(DefaultAction->second == NarrowScalar && "unexpected default"); |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 78 | return findLegalAction(Aspect, NarrowScalar); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | LLT EltTy = Ty.getElementType(); |
| 82 | int NumElts = Ty.getNumElements(); |
| 83 | |
| 84 | auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy)); |
| 85 | if (ScalarAction != ScalarInVectorActions.end() && |
| 86 | ScalarAction->second != Legal) |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 87 | return findLegalAction(Aspect, ScalarAction->second); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 88 | |
| 89 | // The element type is legal in principle, but the number of elements is |
| 90 | // wrong. |
| 91 | auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy)); |
| 92 | if (MaxLegalElts > NumElts) |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 93 | return findLegalAction(Aspect, MoreElements); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 94 | |
| 95 | if (MaxLegalElts == 0) { |
| 96 | // Scalarize if there's no legal vector type, which is just a special case |
| 97 | // of FewerElements. |
| 98 | return std::make_pair(FewerElements, EltTy); |
| 99 | } |
| 100 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 101 | return findLegalAction(Aspect, FewerElements); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 104 | std::tuple<MachineLegalizer::LegalizeAction, unsigned, LLT> |
Ahmed Bougacha | f49ab9a | 2016-08-02 11:41:03 +0000 | [diff] [blame] | 105 | MachineLegalizer::getAction(const MachineInstr &MI) const { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 106 | for (unsigned i = 0; i < MI.getNumTypes(); ++i) { |
| 107 | auto Action = getAction({MI.getOpcode(), i, MI.getType(i)}); |
| 108 | if (Action.first != Legal) |
| 109 | return {Action.first, i, Action.second}; |
| 110 | } |
| 111 | return {Legal, 0, LLT{}}; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Ahmed Bougacha | f49ab9a | 2016-08-02 11:41:03 +0000 | [diff] [blame] | 114 | bool MachineLegalizer::isLegal(const MachineInstr &MI) const { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 115 | return std::get<0>(getAction(MI)) == Legal; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 118 | LLT MachineLegalizer::findLegalType(const InstrAspect &Aspect, |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 119 | LegalizeAction Action) const { |
| 120 | switch(Action) { |
| 121 | default: |
| 122 | llvm_unreachable("Cannot find legal type"); |
| 123 | case Legal: |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 124 | return Aspect.Type; |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 125 | case NarrowScalar: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 126 | return findLegalType(Aspect, |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 127 | [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); }); |
| 128 | } |
| 129 | case WidenScalar: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 130 | return findLegalType(Aspect, [&](LLT Ty) -> LLT { |
Tim Northover | ea904f9 | 2016-08-19 22:40:00 +0000 | [diff] [blame] | 131 | return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize(); |
| 132 | }); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 133 | } |
| 134 | case FewerElements: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 135 | return findLegalType(Aspect, |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 136 | [&](LLT Ty) -> LLT { return Ty.halfElements(); }); |
| 137 | } |
| 138 | case MoreElements: { |
Tim Northover | a01bece | 2016-08-23 19:30:42 +0000 | [diff] [blame^] | 139 | return findLegalType(Aspect, |
| 140 | [&](LLT Ty) -> LLT { return Ty.doubleElements(); }); |
Tim Northover | 75ad077 | 2016-07-20 21:13:29 +0000 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |