blob: 76917aa9660d4d9192033f5474602530d12b264a [file] [log] [blame]
Eugene Zelenko76bf48d2017-06-26 22:44:03 +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#include "llvm/ADT/SmallBitVector.h"
Tim Northover75ad0772016-07-20 21:13:29 +000022#include "llvm/CodeGen/MachineInstr.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000023#include "llvm/CodeGen/MachineOperand.h"
Tim Northover0f140c72016-09-09 11:46:34 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000025#include "llvm/MC/MCInstrDesc.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/LowLevelTypeImpl.h"
28#include "llvm/Support/MathExtras.h"
Tim Northover75ad0772016-07-20 21:13:29 +000029#include "llvm/Target/TargetOpcodes.h"
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000030#include <algorithm>
31#include <cassert>
32#include <tuple>
33#include <utility>
34
Tim Northover75ad0772016-07-20 21:13:29 +000035using namespace llvm;
36
Eugene Zelenko76bf48d2017-06-26 22:44:03 +000037LegalizerInfo::LegalizerInfo() {
Tim Northoverff5e7e12017-06-30 20:27:36 +000038 DefaultActions[TargetOpcode::G_IMPLICIT_DEF] = NarrowScalar;
39
Tim Northover32335812016-08-04 18:35:11 +000040 // FIXME: these two can be legalized to the fundamental load/store Jakob
41 // proposed. Once loads & stores are supported.
Tim Northoverbdf67c92016-08-23 21:01:33 +000042 DefaultActions[TargetOpcode::G_ANYEXT] = Legal;
Tim Northover32335812016-08-04 18:35:11 +000043 DefaultActions[TargetOpcode::G_TRUNC] = Legal;
44
Tim Northoverb3a0be42016-08-23 21:01:20 +000045 DefaultActions[TargetOpcode::G_INTRINSIC] = Legal;
46 DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal;
47
Tim Northover75ad0772016-07-20 21:13:29 +000048 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
Tim Northovercdf23f12016-10-31 18:30:59 +000049 DefaultActions[TargetOpcode::G_LOAD] = NarrowScalar;
50 DefaultActions[TargetOpcode::G_STORE] = NarrowScalar;
Tim Northoverb3a0be42016-08-23 21:01:20 +000051
52 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
Tim Northover0e6afbd2017-02-06 21:56:47 +000053 DefaultActions[TargetOpcode::G_INSERT] = NarrowScalar;
Tim Northoverc2d5e6d2017-06-26 20:34:13 +000054 DefaultActions[TargetOpcode::G_EXTRACT] = NarrowScalar;
Volkan Keles5698b2a2017-03-08 18:09:14 +000055 DefaultActions[TargetOpcode::G_FNEG] = Lower;
Tim Northover75ad0772016-07-20 21:13:29 +000056}
57
Tim Northover69fa84a2016-10-14 22:18:18 +000058void LegalizerInfo::computeTables() {
Tim Northoverf8bab1c2016-08-29 21:00:00 +000059 for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) {
60 for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) {
61 for (auto &Action : Actions[Opcode][Idx]) {
62 LLT Ty = Action.first;
63 if (!Ty.isVector())
64 continue;
Tim Northover75ad0772016-07-20 21:13:29 +000065
Tim Northoverf8bab1c2016-08-29 21:00:00 +000066 auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp,
67 Ty.getElementType())];
68 Entry = std::max(Entry, Ty.getNumElements());
69 }
70 }
Tim Northover75ad0772016-07-20 21:13:29 +000071 }
72
73 TablesInitialized = true;
74}
75
76// FIXME: inefficient implementation for now. Without ComputeValueVTs we're
77// probably going to need specialized lookup structures for various types before
78// we have any hope of doing well with something like <13 x i3>. Even the common
79// cases should do better than what we have now.
Tim Northover69fa84a2016-10-14 22:18:18 +000080std::pair<LegalizerInfo::LegalizeAction, LLT>
81LegalizerInfo::getAction(const InstrAspect &Aspect) const {
Tim Northover75ad0772016-07-20 21:13:29 +000082 assert(TablesInitialized && "backend forgot to call computeTables");
83 // These *have* to be implemented for now, they're the fundamental basis of
84 // how everything else is transformed.
85
Tim Northover33b07d62016-07-22 20:03:43 +000086 // FIXME: the long-term plan calls for expansion in terms of load/store (if
87 // they're not legal).
Tim Northoverc2d5e6d2017-06-26 20:34:13 +000088 if (Aspect.Opcode == TargetOpcode::G_MERGE_VALUES ||
Tim Northoverbf017292017-03-03 22:46:09 +000089 Aspect.Opcode == TargetOpcode::G_UNMERGE_VALUES)
Tim Northovera01bece2016-08-23 19:30:42 +000090 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000091
Volkan Keles64ad85f2017-04-11 10:10:14 +000092 LLT Ty = Aspect.Type;
Tim Northoverf8bab1c2016-08-29 21:00:00 +000093 LegalizeAction Action = findInActions(Aspect);
Volkan Keles64ad85f2017-04-11 10:10:14 +000094 // LegalizerHelper is not able to handle non-power-of-2 types right now, so do
95 // not try to legalize them unless they are marked as Legal or Custom.
96 // FIXME: This is a temporary hack until the general non-power-of-2
97 // legalization works.
98 if (!isPowerOf2_64(Ty.getSizeInBits()) &&
99 !(Action == Legal || Action == Custom))
100 return std::make_pair(Unsupported, LLT());
101
Tim Northoverf8bab1c2016-08-29 21:00:00 +0000102 if (Action != NotFound)
103 return findLegalAction(Aspect, Action);
Tim Northover75ad0772016-07-20 21:13:29 +0000104
Tim Northovera01bece2016-08-23 19:30:42 +0000105 unsigned Opcode = Aspect.Opcode;
Tim Northover75ad0772016-07-20 21:13:29 +0000106 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +0000107 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +0000108 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
109 return std::make_pair(Legal, Ty);
110
Volkan Keles5698b2a2017-03-08 18:09:14 +0000111 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Lower)
112 return std::make_pair(Lower, Ty);
113
Tim Northover0a683e72016-12-06 19:02:15 +0000114 if (DefaultAction == DefaultActions.end() ||
115 DefaultAction->second != NarrowScalar)
116 return std::make_pair(Unsupported, LLT());
Tim Northovera01bece2016-08-23 19:30:42 +0000117 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +0000118 }
119
120 LLT EltTy = Ty.getElementType();
121 int NumElts = Ty.getNumElements();
122
123 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
124 if (ScalarAction != ScalarInVectorActions.end() &&
125 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +0000126 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +0000127
128 // The element type is legal in principle, but the number of elements is
129 // wrong.
130 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
131 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +0000132 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000133
134 if (MaxLegalElts == 0) {
135 // Scalarize if there's no legal vector type, which is just a special case
136 // of FewerElements.
137 return std::make_pair(FewerElements, EltTy);
138 }
139
Tim Northovera01bece2016-08-23 19:30:42 +0000140 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000141}
142
Tim Northover69fa84a2016-10-14 22:18:18 +0000143std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT>
144LegalizerInfo::getAction(const MachineInstr &MI,
145 const MachineRegisterInfo &MRI) const {
Tim Northover0f140c72016-09-09 11:46:34 +0000146 SmallBitVector SeenTypes(8);
147 const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
148 for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
149 if (!OpInfo[i].isGenericType())
150 continue;
151
152 // We don't want to repeatedly check the same operand index, that
153 // could get expensive.
154 unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
155 if (SeenTypes[TypeIdx])
156 continue;
157
158 SeenTypes.set(TypeIdx);
159
160 LLT Ty = MRI.getType(MI.getOperand(i).getReg());
161 auto Action = getAction({MI.getOpcode(), TypeIdx, Ty});
Tim Northovera01bece2016-08-23 19:30:42 +0000162 if (Action.first != Legal)
Tim Northover0f140c72016-09-09 11:46:34 +0000163 return std::make_tuple(Action.first, TypeIdx, Action.second);
Tim Northovera01bece2016-08-23 19:30:42 +0000164 }
Richard Smith84c4cc42016-08-23 22:21:58 +0000165 return std::make_tuple(Legal, 0, LLT{});
Tim Northover75ad0772016-07-20 21:13:29 +0000166}
167
Tim Northover69fa84a2016-10-14 22:18:18 +0000168bool LegalizerInfo::isLegal(const MachineInstr &MI,
169 const MachineRegisterInfo &MRI) const {
Tim Northover0f140c72016-09-09 11:46:34 +0000170 return std::get<0>(getAction(MI, MRI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000171}
172
Aditya Nandakumar479ddd22017-05-12 22:43:58 +0000173Optional<LLT> LegalizerInfo::findLegalType(const InstrAspect &Aspect,
Tim Northover69fa84a2016-10-14 22:18:18 +0000174 LegalizeAction Action) const {
Tim Northover75ad0772016-07-20 21:13:29 +0000175 switch(Action) {
176 default:
177 llvm_unreachable("Cannot find legal type");
178 case Legal:
Tim Northovercecee562016-08-26 17:46:13 +0000179 case Lower:
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000180 case Libcall:
Tim Northover91366172017-02-15 23:22:50 +0000181 case Custom:
Tim Northovera01bece2016-08-23 19:30:42 +0000182 return Aspect.Type;
Kristof Beylsb539ea52017-06-30 08:26:20 +0000183 case NarrowScalar: {
184 return findLegalizableSize(
185 Aspect, [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
186 }
187 case WidenScalar: {
188 return findLegalizableSize(Aspect, [&](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000189 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
190 });
Kristof Beylsb539ea52017-06-30 08:26:20 +0000191 }
192 case FewerElements: {
193 return findLegalizableSize(
194 Aspect, [&](LLT Ty) -> LLT { return Ty.halfElements(); });
195 }
196 case MoreElements: {
197 return findLegalizableSize(
198 Aspect, [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
199 }
Tim Northover75ad0772016-07-20 21:13:29 +0000200 }
Tim Northover75ad0772016-07-20 21:13:29 +0000201}
Tim Northover91366172017-02-15 23:22:50 +0000202
203bool LegalizerInfo::legalizeCustom(MachineInstr &MI,
204 MachineRegisterInfo &MRI,
205 MachineIRBuilder &MIRBuilder) const {
206 return false;
207}