blob: ef1ce7f87ebc287be46a09e17d8cd87d6e5a8a75 [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 Northover0e6afbd2017-02-06 21:56:47 +000044 DefaultActions[TargetOpcode::G_INSERT] = NarrowScalar;
Tim Northoverc2d5e6d2017-06-26 20:34:13 +000045 DefaultActions[TargetOpcode::G_EXTRACT] = NarrowScalar;
Volkan Keles5698b2a2017-03-08 18:09:14 +000046 DefaultActions[TargetOpcode::G_FNEG] = Lower;
Tim Northover75ad0772016-07-20 21:13:29 +000047}
48
Tim Northover69fa84a2016-10-14 22:18:18 +000049void LegalizerInfo::computeTables() {
Tim Northoverf8bab1c2016-08-29 21:00:00 +000050 for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) {
51 for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) {
52 for (auto &Action : Actions[Opcode][Idx]) {
53 LLT Ty = Action.first;
54 if (!Ty.isVector())
55 continue;
Tim Northover75ad0772016-07-20 21:13:29 +000056
Tim Northoverf8bab1c2016-08-29 21:00:00 +000057 auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp,
58 Ty.getElementType())];
59 Entry = std::max(Entry, Ty.getNumElements());
60 }
61 }
Tim Northover75ad0772016-07-20 21:13:29 +000062 }
63
64 TablesInitialized = true;
65}
66
67// FIXME: inefficient implementation for now. Without ComputeValueVTs we're
68// probably going to need specialized lookup structures for various types before
69// we have any hope of doing well with something like <13 x i3>. Even the common
70// cases should do better than what we have now.
Tim Northover69fa84a2016-10-14 22:18:18 +000071std::pair<LegalizerInfo::LegalizeAction, LLT>
72LegalizerInfo::getAction(const InstrAspect &Aspect) const {
Tim Northover75ad0772016-07-20 21:13:29 +000073 assert(TablesInitialized && "backend forgot to call computeTables");
74 // These *have* to be implemented for now, they're the fundamental basis of
75 // how everything else is transformed.
76
Tim Northover33b07d62016-07-22 20:03:43 +000077 // FIXME: the long-term plan calls for expansion in terms of load/store (if
78 // they're not legal).
Tim Northoverc2d5e6d2017-06-26 20:34:13 +000079 if (Aspect.Opcode == TargetOpcode::G_MERGE_VALUES ||
Tim Northoverbf017292017-03-03 22:46:09 +000080 Aspect.Opcode == TargetOpcode::G_UNMERGE_VALUES)
Tim Northovera01bece2016-08-23 19:30:42 +000081 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000082
Volkan Keles64ad85f2017-04-11 10:10:14 +000083 LLT Ty = Aspect.Type;
Tim Northoverf8bab1c2016-08-29 21:00:00 +000084 LegalizeAction Action = findInActions(Aspect);
Volkan Keles64ad85f2017-04-11 10:10:14 +000085 // LegalizerHelper is not able to handle non-power-of-2 types right now, so do
86 // not try to legalize them unless they are marked as Legal or Custom.
87 // FIXME: This is a temporary hack until the general non-power-of-2
88 // legalization works.
89 if (!isPowerOf2_64(Ty.getSizeInBits()) &&
90 !(Action == Legal || Action == Custom))
91 return std::make_pair(Unsupported, LLT());
92
Tim Northoverf8bab1c2016-08-29 21:00:00 +000093 if (Action != NotFound)
94 return findLegalAction(Aspect, Action);
Tim Northover75ad0772016-07-20 21:13:29 +000095
Tim Northovera01bece2016-08-23 19:30:42 +000096 unsigned Opcode = Aspect.Opcode;
Tim Northover75ad0772016-07-20 21:13:29 +000097 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +000098 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +000099 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
100 return std::make_pair(Legal, Ty);
101
Volkan Keles5698b2a2017-03-08 18:09:14 +0000102 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Lower)
103 return std::make_pair(Lower, Ty);
104
Tim Northover0a683e72016-12-06 19:02:15 +0000105 if (DefaultAction == DefaultActions.end() ||
106 DefaultAction->second != NarrowScalar)
107 return std::make_pair(Unsupported, LLT());
Tim Northovera01bece2016-08-23 19:30:42 +0000108 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +0000109 }
110
111 LLT EltTy = Ty.getElementType();
112 int NumElts = Ty.getNumElements();
113
114 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
115 if (ScalarAction != ScalarInVectorActions.end() &&
116 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +0000117 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +0000118
119 // The element type is legal in principle, but the number of elements is
120 // wrong.
121 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
122 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +0000123 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000124
125 if (MaxLegalElts == 0) {
126 // Scalarize if there's no legal vector type, which is just a special case
127 // of FewerElements.
128 return std::make_pair(FewerElements, EltTy);
129 }
130
Tim Northovera01bece2016-08-23 19:30:42 +0000131 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000132}
133
Tim Northover69fa84a2016-10-14 22:18:18 +0000134std::tuple<LegalizerInfo::LegalizeAction, unsigned, LLT>
135LegalizerInfo::getAction(const MachineInstr &MI,
136 const MachineRegisterInfo &MRI) const {
Tim Northover0f140c72016-09-09 11:46:34 +0000137 SmallBitVector SeenTypes(8);
138 const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
139 for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
140 if (!OpInfo[i].isGenericType())
141 continue;
142
143 // We don't want to repeatedly check the same operand index, that
144 // could get expensive.
145 unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
146 if (SeenTypes[TypeIdx])
147 continue;
148
149 SeenTypes.set(TypeIdx);
150
151 LLT Ty = MRI.getType(MI.getOperand(i).getReg());
152 auto Action = getAction({MI.getOpcode(), TypeIdx, Ty});
Tim Northovera01bece2016-08-23 19:30:42 +0000153 if (Action.first != Legal)
Tim Northover0f140c72016-09-09 11:46:34 +0000154 return std::make_tuple(Action.first, TypeIdx, Action.second);
Tim Northovera01bece2016-08-23 19:30:42 +0000155 }
Richard Smith84c4cc42016-08-23 22:21:58 +0000156 return std::make_tuple(Legal, 0, LLT{});
Tim Northover75ad0772016-07-20 21:13:29 +0000157}
158
Tim Northover69fa84a2016-10-14 22:18:18 +0000159bool LegalizerInfo::isLegal(const MachineInstr &MI,
160 const MachineRegisterInfo &MRI) const {
Tim Northover0f140c72016-09-09 11:46:34 +0000161 return std::get<0>(getAction(MI, MRI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000162}
163
Aditya Nandakumar479ddd22017-05-12 22:43:58 +0000164Optional<LLT> LegalizerInfo::findLegalType(const InstrAspect &Aspect,
Tim Northover69fa84a2016-10-14 22:18:18 +0000165 LegalizeAction Action) const {
Tim Northover75ad0772016-07-20 21:13:29 +0000166 switch(Action) {
167 default:
168 llvm_unreachable("Cannot find legal type");
169 case Legal:
Tim Northovercecee562016-08-26 17:46:13 +0000170 case Lower:
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000171 case Libcall:
Tim Northover91366172017-02-15 23:22:50 +0000172 case Custom:
Tim Northovera01bece2016-08-23 19:30:42 +0000173 return Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +0000174 case NarrowScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000175 return findLegalType(Aspect,
Aditya Nandakumarfd484c42017-05-11 21:56:51 +0000176 [](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000177 }
178 case WidenScalar: {
Aditya Nandakumarfd484c42017-05-11 21:56:51 +0000179 return findLegalType(Aspect, [](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000180 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
181 });
Tim Northover75ad0772016-07-20 21:13:29 +0000182 }
183 case FewerElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000184 return findLegalType(Aspect,
Aditya Nandakumarfd484c42017-05-11 21:56:51 +0000185 [](LLT Ty) -> LLT { return Ty.halfElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000186 }
187 case MoreElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000188 return findLegalType(Aspect,
Aditya Nandakumarfd484c42017-05-11 21:56:51 +0000189 [](LLT Ty) -> LLT { return Ty.doubleElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000190 }
191 }
192}
Tim Northover91366172017-02-15 23:22:50 +0000193
194bool LegalizerInfo::legalizeCustom(MachineInstr &MI,
195 MachineRegisterInfo &MRI,
196 MachineIRBuilder &MIRBuilder) const {
197 return false;
198}