blob: e49662075ed5e10cd70b251fc4ade320ec9a7465 [file] [log] [blame]
Tim Northover69fa84a2016-10-14 22:18:18 +00001//===---- lib/CodeGen/GlobalISel/LegalizerInfo.cpp - Legalizer -------==//
Tim Northover75ad0772016-07-20 21:13:29 +00002//
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
Tim Northover69fa84a2016-10-14 22:18:18 +000020#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
Tim Northover0f140c72016-09-09 11:46:34 +000021
22#include "llvm/ADT/SmallBitVector.h"
Tim Northover75ad0772016-07-20 21:13:29 +000023#include "llvm/CodeGen/MachineInstr.h"
Tim Northover0f140c72016-09-09 11:46:34 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Tim Northover75ad0772016-07-20 21:13:29 +000025#include "llvm/CodeGen/ValueTypes.h"
Tim Northover75ad0772016-07-20 21:13:29 +000026#include "llvm/IR/Type.h"
27#include "llvm/Target/TargetOpcodes.h"
28using namespace llvm;
29
Tim Northover69fa84a2016-10-14 22:18:18 +000030LegalizerInfo::LegalizerInfo() : TablesInitialized(false) {
Tim Northover32335812016-08-04 18:35:11 +000031 // FIXME: these two can be legalized to the fundamental load/store Jakob
32 // proposed. Once loads & stores are supported.
Tim Northoverbdf67c92016-08-23 21:01:33 +000033 DefaultActions[TargetOpcode::G_ANYEXT] = Legal;
Tim Northover32335812016-08-04 18:35:11 +000034 DefaultActions[TargetOpcode::G_TRUNC] = Legal;
35
Tim Northoverb3a0be42016-08-23 21:01:20 +000036 DefaultActions[TargetOpcode::G_INTRINSIC] = Legal;
37 DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal;
38
Tim Northover75ad0772016-07-20 21:13:29 +000039 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
Tim Northovercdf23f12016-10-31 18:30:59 +000040 DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar;
41 DefaultActions[TargetOpcode::G_STORE] = NarrowScalar;
Tim Northoverb3a0be42016-08-23 21:01:20 +000042
43 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
Tim Northover75ad0772016-07-20 21:13:29 +000044}
45
Tim Northover69fa84a2016-10-14 22:18:18 +000046void LegalizerInfo::computeTables() {
Tim Northoverf8bab1c2016-08-29 21:00:00 +000047 for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) {
48 for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) {
49 for (auto &Action : Actions[Opcode][Idx]) {
50 LLT Ty = Action.first;
51 if (!Ty.isVector())
52 continue;
Tim Northover75ad0772016-07-20 21:13:29 +000053
Tim Northoverf8bab1c2016-08-29 21:00:00 +000054 auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp,
55 Ty.getElementType())];
56 Entry = std::max(Entry, Ty.getNumElements());
57 }
58 }
Tim Northover75ad0772016-07-20 21:13:29 +000059 }
60
61 TablesInitialized = true;
62}
63
64// FIXME: inefficient implementation for now. Without ComputeValueVTs we're
65// probably going to need specialized lookup structures for various types before
66// we have any hope of doing well with something like <13 x i3>. Even the common
67// cases should do better than what we have now.
Tim Northover69fa84a2016-10-14 22:18:18 +000068std::pair<LegalizerInfo::LegalizeAction, LLT>
69LegalizerInfo::getAction(const InstrAspect &Aspect) const {
Tim Northover75ad0772016-07-20 21:13:29 +000070 assert(TablesInitialized && "backend forgot to call computeTables");
71 // These *have* to be implemented for now, they're the fundamental basis of
72 // how everything else is transformed.
73
Tim Northover405e25c2016-12-06 18:38:29 +000074 // Nothing is going to go well with types that aren't a power of 2 yet, so
75 // don't even try because we might make things worse.
76 if (!isPowerOf2_64(Aspect.Type.getSizeInBits()))
77 return std::make_pair(Unsupported, LLT());
78
Tim Northover33b07d62016-07-22 20:03:43 +000079 // FIXME: the long-term plan calls for expansion in terms of load/store (if
80 // they're not legal).
Tim Northovera01bece2016-08-23 19:30:42 +000081 if (Aspect.Opcode == TargetOpcode::G_SEQUENCE ||
82 Aspect.Opcode == TargetOpcode::G_EXTRACT)
83 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000084
Tim Northoverf8bab1c2016-08-29 21:00:00 +000085 LegalizeAction Action = findInActions(Aspect);
86 if (Action != NotFound)
87 return findLegalAction(Aspect, Action);
Tim Northover75ad0772016-07-20 21:13:29 +000088
Tim Northovera01bece2016-08-23 19:30:42 +000089 unsigned Opcode = Aspect.Opcode;
90 LLT Ty = Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000091 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +000092 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +000093 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
94 return std::make_pair(Legal, Ty);
95
Tim Northover0a683e72016-12-06 19:02:15 +000096 if (DefaultAction == DefaultActions.end() ||
97 DefaultAction->second != NarrowScalar)
98 return std::make_pair(Unsupported, LLT());
Tim Northovera01bece2016-08-23 19:30:42 +000099 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +0000100 }
101
102 LLT EltTy = Ty.getElementType();
103 int NumElts = Ty.getNumElements();
104
105 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
106 if (ScalarAction != ScalarInVectorActions.end() &&
107 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +0000108 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +0000109
110 // The element type is legal in principle, but the number of elements is
111 // wrong.
112 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
113 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +0000114 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000115
116 if (MaxLegalElts == 0) {
117 // Scalarize if there's no legal vector type, which is just a special case
118 // of FewerElements.
119 return std::make_pair(FewerElements, EltTy);
120 }
121
Tim Northovera01bece2016-08-23 19:30:42 +0000122 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000123}
124
Tim Northover69fa84a2016-10-14 22:18:18 +0000125std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT>
126LegalizerInfo::getAction(const MachineInstr &MI,
127 const MachineRegisterInfo &MRI) const {
Tim Northover0f140c72016-09-09 11:46:34 +0000128 SmallBitVector SeenTypes(8);
129 const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
130 for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
131 if (!OpInfo[i].isGenericType())
132 continue;
133
134 // We don't want to repeatedly check the same operand index, that
135 // could get expensive.
136 unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
137 if (SeenTypes[TypeIdx])
138 continue;
139
140 SeenTypes.set(TypeIdx);
141
142 LLT Ty = MRI.getType(MI.getOperand(i).getReg());
143 auto Action = getAction({MI.getOpcode(), TypeIdx, Ty});
Tim Northovera01bece2016-08-23 19:30:42 +0000144 if (Action.first != Legal)
Tim Northover0f140c72016-09-09 11:46:34 +0000145 return std::make_tuple(Action.first, TypeIdx, Action.second);
Tim Northovera01bece2016-08-23 19:30:42 +0000146 }
Richard Smith84c4cc42016-08-23 22:21:58 +0000147 return std::make_tuple(Legal, 0, LLT{});
Tim Northover75ad0772016-07-20 21:13:29 +0000148}
149
Tim Northover69fa84a2016-10-14 22:18:18 +0000150bool LegalizerInfo::isLegal(const MachineInstr &MI,
151 const MachineRegisterInfo &MRI) const {
Tim Northover0f140c72016-09-09 11:46:34 +0000152 return std::get<0>(getAction(MI, MRI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000153}
154
Tim Northover69fa84a2016-10-14 22:18:18 +0000155LLT LegalizerInfo::findLegalType(const InstrAspect &Aspect,
156 LegalizeAction Action) const {
Tim Northover75ad0772016-07-20 21:13:29 +0000157 switch(Action) {
158 default:
159 llvm_unreachable("Cannot find legal type");
160 case Legal:
Tim Northovercecee562016-08-26 17:46:13 +0000161 case Lower:
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000162 case Libcall:
Tim Northovera01bece2016-08-23 19:30:42 +0000163 return Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +0000164 case NarrowScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000165 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000166 [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
167 }
168 case WidenScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000169 return findLegalType(Aspect, [&](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000170 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
171 });
Tim Northover75ad0772016-07-20 21:13:29 +0000172 }
173 case FewerElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000174 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000175 [&](LLT Ty) -> LLT { return Ty.halfElements(); });
176 }
177 case MoreElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000178 return findLegalType(Aspect,
179 [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000180 }
181 }
182}