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 }
46 
47 void LegalizerInfo::computeTables() {
48   for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) {
49     for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) {
50       for (auto &Action : Actions[Opcode][Idx]) {
51         LLT Ty = Action.first;
52         if (!Ty.isVector())
53           continue;
54 
55         auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp,
56                                                         Ty.getElementType())];
57         Entry = std::max(Entry, Ty.getNumElements());
58       }
59     }
60   }
61 
62   TablesInitialized = true;
63 }
64 
65 // FIXME: inefficient implementation for now. Without ComputeValueVTs we're
66 // probably going to need specialized lookup structures for various types before
67 // we have any hope of doing well with something like <13 x i3>. Even the common
68 // cases should do better than what we have now.
69 std::pair<LegalizerInfo::LegalizeAction, LLT>
70 LegalizerInfo::getAction(const InstrAspect &Aspect) const {
71   assert(TablesInitialized && "backend forgot to call computeTables");
72   // These *have* to be implemented for now, they're the fundamental basis of
73   // how everything else is transformed.
74 
75   // Nothing is going to go well with types that aren't a power of 2 yet, so
76   // don't even try because we might make things worse.
77   if (!isPowerOf2_64(Aspect.Type.getSizeInBits()))
78       return std::make_pair(Unsupported, LLT());
79 
80   // FIXME: the long-term plan calls for expansion in terms of load/store (if
81   // they're not legal).
82   if (Aspect.Opcode == TargetOpcode::G_SEQUENCE ||
83       Aspect.Opcode == TargetOpcode::G_EXTRACT)
84     return std::make_pair(Legal, Aspect.Type);
85 
86   LegalizeAction Action = findInActions(Aspect);
87   if (Action != NotFound)
88     return findLegalAction(Aspect, Action);
89 
90   unsigned Opcode = Aspect.Opcode;
91   LLT Ty = Aspect.Type;
92   if (!Ty.isVector()) {
93     auto DefaultAction = DefaultActions.find(Aspect.Opcode);
94     if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
95       return std::make_pair(Legal, Ty);
96 
97     if (DefaultAction == DefaultActions.end() ||
98         DefaultAction->second != NarrowScalar)
99       return std::make_pair(Unsupported, LLT());
100     return findLegalAction(Aspect, NarrowScalar);
101   }
102 
103   LLT EltTy = Ty.getElementType();
104   int NumElts = Ty.getNumElements();
105 
106   auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
107   if (ScalarAction != ScalarInVectorActions.end() &&
108       ScalarAction->second != Legal)
109     return findLegalAction(Aspect, ScalarAction->second);
110 
111   // The element type is legal in principle, but the number of elements is
112   // wrong.
113   auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
114   if (MaxLegalElts > NumElts)
115     return findLegalAction(Aspect, MoreElements);
116 
117   if (MaxLegalElts == 0) {
118     // Scalarize if there's no legal vector type, which is just a special case
119     // of FewerElements.
120     return std::make_pair(FewerElements, EltTy);
121   }
122 
123   return findLegalAction(Aspect, FewerElements);
124 }
125 
126 std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT>
127 LegalizerInfo::getAction(const MachineInstr &MI,
128                          const MachineRegisterInfo &MRI) const {
129   SmallBitVector SeenTypes(8);
130   const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
131   for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
132     if (!OpInfo[i].isGenericType())
133       continue;
134 
135     // We don't want to repeatedly check the same operand index, that
136     // could get expensive.
137     unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
138     if (SeenTypes[TypeIdx])
139       continue;
140 
141     SeenTypes.set(TypeIdx);
142 
143     LLT Ty = MRI.getType(MI.getOperand(i).getReg());
144     auto Action = getAction({MI.getOpcode(), TypeIdx, Ty});
145     if (Action.first != Legal)
146       return std::make_tuple(Action.first, TypeIdx, Action.second);
147   }
148   return std::make_tuple(Legal, 0, LLT{});
149 }
150 
151 bool LegalizerInfo::isLegal(const MachineInstr &MI,
152                             const MachineRegisterInfo &MRI) const {
153   return std::get<0>(getAction(MI, MRI)) == Legal;
154 }
155 
156 LLT LegalizerInfo::findLegalType(const InstrAspect &Aspect,
157                                  LegalizeAction Action) const {
158   switch(Action) {
159   default:
160     llvm_unreachable("Cannot find legal type");
161   case Legal:
162   case Lower:
163   case Libcall:
164   case Custom:
165     return Aspect.Type;
166   case NarrowScalar: {
167     return findLegalType(Aspect,
168                          [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
169   }
170   case WidenScalar: {
171     return findLegalType(Aspect, [&](LLT Ty) -> LLT {
172       return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
173     });
174   }
175   case FewerElements: {
176     return findLegalType(Aspect,
177                          [&](LLT Ty) -> LLT { return Ty.halfElements(); });
178   }
179   case MoreElements: {
180     return findLegalType(Aspect,
181                          [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
182   }
183   }
184 }
185 
186 bool LegalizerInfo::legalizeCustom(MachineInstr &MI,
187                                    MachineRegisterInfo &MRI,
188                                    MachineIRBuilder &MIRBuilder) const {
189   return false;
190 }
191