1 //===---- lib/CodeGen/GlobalISel/LegalizerInfo.cpp - Legalizer -------==// 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/GlobalISel/LegalizerInfo.h" 21 22 #include "llvm/ADT/SmallBitVector.h" 23 #include "llvm/CodeGen/MachineInstr.h" 24 #include "llvm/CodeGen/MachineRegisterInfo.h" 25 #include "llvm/CodeGen/ValueTypes.h" 26 #include "llvm/IR/Type.h" 27 #include "llvm/Target/TargetOpcodes.h" 28 using namespace llvm; 29 30 LegalizerInfo::LegalizerInfo() : TablesInitialized(false) { 31 // FIXME: these two can be legalized to the fundamental load/store Jakob 32 // proposed. Once loads & stores are supported. 33 DefaultActions[TargetOpcode::G_ANYEXT] = Legal; 34 DefaultActions[TargetOpcode::G_TRUNC] = Legal; 35 36 DefaultActions[TargetOpcode::G_INTRINSIC] = Legal; 37 DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal; 38 39 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar; 40 DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar; 41 DefaultActions[TargetOpcode::G_STORE] = NarrowScalar; 42 43 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar; 44 DefaultActions[TargetOpcode::G_INSERT] = NarrowScalar; 45 DefaultActions[TargetOpcode::G_FNEG] = Lower; 46 } 47 48 void LegalizerInfo::computeTables() { 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; 55 56 auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp, 57 Ty.getElementType())]; 58 Entry = std::max(Entry, Ty.getNumElements()); 59 } 60 } 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. 70 std::pair<LegalizerInfo::LegalizeAction, LLT> 71 LegalizerInfo::getAction(const InstrAspect &Aspect) const { 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 76 // Nothing is going to go well with types that aren't a power of 2 yet, so 77 // don't even try because we might make things worse. 78 if (!isPowerOf2_64(Aspect.Type.getSizeInBits())) 79 return std::make_pair(Unsupported, LLT()); 80 81 // FIXME: the long-term plan calls for expansion in terms of load/store (if 82 // they're not legal). 83 if (Aspect.Opcode == TargetOpcode::G_SEQUENCE || 84 Aspect.Opcode == TargetOpcode::G_EXTRACT || 85 Aspect.Opcode == TargetOpcode::G_MERGE_VALUES || 86 Aspect.Opcode == TargetOpcode::G_UNMERGE_VALUES) 87 return std::make_pair(Legal, Aspect.Type); 88 89 LegalizeAction Action = findInActions(Aspect); 90 if (Action != NotFound) 91 return findLegalAction(Aspect, Action); 92 93 unsigned Opcode = Aspect.Opcode; 94 LLT Ty = Aspect.Type; 95 if (!Ty.isVector()) { 96 auto DefaultAction = DefaultActions.find(Aspect.Opcode); 97 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal) 98 return std::make_pair(Legal, Ty); 99 100 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Lower) 101 return std::make_pair(Lower, Ty); 102 103 if (DefaultAction == DefaultActions.end() || 104 DefaultAction->second != NarrowScalar) 105 return std::make_pair(Unsupported, LLT()); 106 return findLegalAction(Aspect, NarrowScalar); 107 } 108 109 LLT EltTy = Ty.getElementType(); 110 int NumElts = Ty.getNumElements(); 111 112 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy)); 113 if (ScalarAction != ScalarInVectorActions.end() && 114 ScalarAction->second != Legal) 115 return findLegalAction(Aspect, ScalarAction->second); 116 117 // The element type is legal in principle, but the number of elements is 118 // wrong. 119 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy)); 120 if (MaxLegalElts > NumElts) 121 return findLegalAction(Aspect, MoreElements); 122 123 if (MaxLegalElts == 0) { 124 // Scalarize if there's no legal vector type, which is just a special case 125 // of FewerElements. 126 return std::make_pair(FewerElements, EltTy); 127 } 128 129 return findLegalAction(Aspect, FewerElements); 130 } 131 132 std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT> 133 LegalizerInfo::getAction(const MachineInstr &MI, 134 const MachineRegisterInfo &MRI) const { 135 SmallBitVector SeenTypes(8); 136 const MCOperandInfo *OpInfo = MI.getDesc().OpInfo; 137 for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) { 138 if (!OpInfo[i].isGenericType()) 139 continue; 140 141 // We don't want to repeatedly check the same operand index, that 142 // could get expensive. 143 unsigned TypeIdx = OpInfo[i].getGenericTypeIndex(); 144 if (SeenTypes[TypeIdx]) 145 continue; 146 147 SeenTypes.set(TypeIdx); 148 149 LLT Ty = MRI.getType(MI.getOperand(i).getReg()); 150 auto Action = getAction({MI.getOpcode(), TypeIdx, Ty}); 151 if (Action.first != Legal) 152 return std::make_tuple(Action.first, TypeIdx, Action.second); 153 } 154 return std::make_tuple(Legal, 0, LLT{}); 155 } 156 157 bool LegalizerInfo::isLegal(const MachineInstr &MI, 158 const MachineRegisterInfo &MRI) const { 159 return std::get<0>(getAction(MI, MRI)) == Legal; 160 } 161 162 LLT LegalizerInfo::findLegalType(const InstrAspect &Aspect, 163 LegalizeAction Action) const { 164 switch(Action) { 165 default: 166 llvm_unreachable("Cannot find legal type"); 167 case Legal: 168 case Lower: 169 case Libcall: 170 case Custom: 171 return Aspect.Type; 172 case NarrowScalar: { 173 return findLegalType(Aspect, 174 [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); }); 175 } 176 case WidenScalar: { 177 return findLegalType(Aspect, [&](LLT Ty) -> LLT { 178 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize(); 179 }); 180 } 181 case FewerElements: { 182 return findLegalType(Aspect, 183 [&](LLT Ty) -> LLT { return Ty.halfElements(); }); 184 } 185 case MoreElements: { 186 return findLegalType(Aspect, 187 [&](LLT Ty) -> LLT { return Ty.doubleElements(); }); 188 } 189 } 190 } 191 192 bool LegalizerInfo::legalizeCustom(MachineInstr &MI, 193 MachineRegisterInfo &MRI, 194 MachineIRBuilder &MIRBuilder) const { 195 return false; 196 } 197