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