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 #include "llvm/ADT/SmallBitVector.h" 22 #include "llvm/CodeGen/MachineInstr.h" 23 #include "llvm/CodeGen/MachineOperand.h" 24 #include "llvm/CodeGen/MachineRegisterInfo.h" 25 #include "llvm/MC/MCInstrDesc.h" 26 #include "llvm/Support/ErrorHandling.h" 27 #include "llvm/Support/LowLevelTypeImpl.h" 28 #include "llvm/Support/MathExtras.h" 29 #include "llvm/Target/TargetOpcodes.h" 30 #include <algorithm> 31 #include <cassert> 32 #include <tuple> 33 #include <utility> 34 35 using namespace llvm; 36 37 LegalizerInfo::LegalizerInfo() { 38 // FIXME: these two can be legalized to the fundamental load/store Jakob 39 // proposed. Once loads & stores are supported. 40 DefaultActions[TargetOpcode::G_ANYEXT] = Legal; 41 DefaultActions[TargetOpcode::G_TRUNC] = Legal; 42 43 DefaultActions[TargetOpcode::G_INTRINSIC] = Legal; 44 DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal; 45 46 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar; 47 DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar; 48 DefaultActions[TargetOpcode::G_STORE] = NarrowScalar; 49 50 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar; 51 DefaultActions[TargetOpcode::G_INSERT] = NarrowScalar; 52 DefaultActions[TargetOpcode::G_EXTRACT] = NarrowScalar; 53 DefaultActions[TargetOpcode::G_FNEG] = Lower; 54 } 55 56 void LegalizerInfo::computeTables() { 57 for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) { 58 for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) { 59 for (auto &Action : Actions[Opcode][Idx]) { 60 LLT Ty = Action.first; 61 if (!Ty.isVector()) 62 continue; 63 64 auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp, 65 Ty.getElementType())]; 66 Entry = std::max(Entry, Ty.getNumElements()); 67 } 68 } 69 } 70 71 TablesInitialized = true; 72 } 73 74 // FIXME: inefficient implementation for now. Without ComputeValueVTs we're 75 // probably going to need specialized lookup structures for various types before 76 // we have any hope of doing well with something like <13 x i3>. Even the common 77 // cases should do better than what we have now. 78 std::pair<LegalizerInfo::LegalizeAction, LLT> 79 LegalizerInfo::getAction(const InstrAspect &Aspect) const { 80 assert(TablesInitialized && "backend forgot to call computeTables"); 81 // These *have* to be implemented for now, they're the fundamental basis of 82 // how everything else is transformed. 83 84 // FIXME: the long-term plan calls for expansion in terms of load/store (if 85 // they're not legal). 86 if (Aspect.Opcode == TargetOpcode::G_MERGE_VALUES || 87 Aspect.Opcode == TargetOpcode::G_UNMERGE_VALUES) 88 return std::make_pair(Legal, Aspect.Type); 89 90 LLT Ty = Aspect.Type; 91 LegalizeAction Action = findInActions(Aspect); 92 // LegalizerHelper is not able to handle non-power-of-2 types right now, so do 93 // not try to legalize them unless they are marked as Legal or Custom. 94 // FIXME: This is a temporary hack until the general non-power-of-2 95 // legalization works. 96 if (!isPowerOf2_64(Ty.getSizeInBits()) && 97 !(Action == Legal || Action == Custom)) 98 return std::make_pair(Unsupported, LLT()); 99 100 if (Action != NotFound) 101 return findLegalAction(Aspect, Action); 102 103 unsigned Opcode = Aspect.Opcode; 104 if (!Ty.isVector()) { 105 auto DefaultAction = DefaultActions.find(Aspect.Opcode); 106 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal) 107 return std::make_pair(Legal, Ty); 108 109 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Lower) 110 return std::make_pair(Lower, Ty); 111 112 if (DefaultAction == DefaultActions.end() || 113 DefaultAction->second != NarrowScalar) 114 return std::make_pair(Unsupported, LLT()); 115 return findLegalAction(Aspect, NarrowScalar); 116 } 117 118 LLT EltTy = Ty.getElementType(); 119 int NumElts = Ty.getNumElements(); 120 121 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy)); 122 if (ScalarAction != ScalarInVectorActions.end() && 123 ScalarAction->second != Legal) 124 return findLegalAction(Aspect, ScalarAction->second); 125 126 // The element type is legal in principle, but the number of elements is 127 // wrong. 128 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy)); 129 if (MaxLegalElts > NumElts) 130 return findLegalAction(Aspect, MoreElements); 131 132 if (MaxLegalElts == 0) { 133 // Scalarize if there's no legal vector type, which is just a special case 134 // of FewerElements. 135 return std::make_pair(FewerElements, EltTy); 136 } 137 138 return findLegalAction(Aspect, FewerElements); 139 } 140 141 std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT> 142 LegalizerInfo::getAction(const MachineInstr &MI, 143 const MachineRegisterInfo &MRI) const { 144 SmallBitVector SeenTypes(8); 145 const MCOperandInfo *OpInfo = MI.getDesc().OpInfo; 146 for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) { 147 if (!OpInfo[i].isGenericType()) 148 continue; 149 150 // We don't want to repeatedly check the same operand index, that 151 // could get expensive. 152 unsigned TypeIdx = OpInfo[i].getGenericTypeIndex(); 153 if (SeenTypes[TypeIdx]) 154 continue; 155 156 SeenTypes.set(TypeIdx); 157 158 LLT Ty = MRI.getType(MI.getOperand(i).getReg()); 159 auto Action = getAction({MI.getOpcode(), TypeIdx, Ty}); 160 if (Action.first != Legal) 161 return std::make_tuple(Action.first, TypeIdx, Action.second); 162 } 163 return std::make_tuple(Legal, 0, LLT{}); 164 } 165 166 bool LegalizerInfo::isLegal(const MachineInstr &MI, 167 const MachineRegisterInfo &MRI) const { 168 return std::get<0>(getAction(MI, MRI)) == Legal; 169 } 170 171 Optional<LLT> LegalizerInfo::findLegalType(const InstrAspect &Aspect, 172 LegalizeAction Action) const { 173 switch(Action) { 174 default: 175 llvm_unreachable("Cannot find legal type"); 176 case Legal: 177 case Lower: 178 case Libcall: 179 case Custom: 180 return Aspect.Type; 181 case NarrowScalar: 182 return findLegalType(Aspect, 183 [](LLT Ty) -> LLT { return Ty.halfScalarSize(); }); 184 case WidenScalar: 185 return findLegalType(Aspect, [](LLT Ty) -> LLT { 186 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize(); 187 }); 188 case FewerElements: 189 return findLegalType(Aspect, 190 [](LLT Ty) -> LLT { return Ty.halfElements(); }); 191 case MoreElements: 192 return findLegalType(Aspect, 193 [](LLT Ty) -> LLT { return Ty.doubleElements(); }); 194 } 195 } 196 197 bool LegalizerInfo::legalizeCustom(MachineInstr &MI, 198 MachineRegisterInfo &MRI, 199 MachineIRBuilder &MIRBuilder) const { 200 return false; 201 } 202