blob: 786deccbd0536d032a594ba484ebe3bd947b0923 [file] [log] [blame]
Tim Northover75ad0772016-07-20 21:13:29 +00001//===---- lib/CodeGen/GlobalISel/MachineLegalizer.cpp - IRTranslator -------==//
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
Tim Northoverf8bab1c2016-08-29 21:00:00 +000020#include "llvm/CodeGen/GlobalISel/MachineLegalizer.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
30MachineLegalizer::MachineLegalizer() : 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 Northover8d8812c2016-09-01 20:45:41 +000036 // G_TYPE and G_PHI are essentially an annotated COPY/PHI instructions so
37 // they're always legal.
Tim Northover11a23542016-08-31 21:24:02 +000038 DefaultActions[TargetOpcode::G_TYPE] = Legal;
Tim Northover8d8812c2016-09-01 20:45:41 +000039 DefaultActions[TargetOpcode::G_PHI] = Legal;
Tim Northover11a23542016-08-31 21:24:02 +000040
Tim Northoverb3a0be42016-08-23 21:01:20 +000041 DefaultActions[TargetOpcode::G_INTRINSIC] = Legal;
42 DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal;
43
Tim Northover75ad0772016-07-20 21:13:29 +000044 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
Tim Northoverb3a0be42016-08-23 21:01:20 +000045
46 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
Tim Northover75ad0772016-07-20 21:13:29 +000047}
48
Tim Northover75ad0772016-07-20 21:13:29 +000049void MachineLegalizer::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.
71std::pair<MachineLegalizer::LegalizeAction, LLT>
Tim Northovera01bece2016-08-23 19:30:42 +000072MachineLegalizer::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 Northovera01bece2016-08-23 19:30:42 +000079 if (Aspect.Opcode == TargetOpcode::G_SEQUENCE ||
80 Aspect.Opcode == TargetOpcode::G_EXTRACT)
81 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000082
Tim Northoverf8bab1c2016-08-29 21:00:00 +000083 LegalizeAction Action = findInActions(Aspect);
84 if (Action != NotFound)
85 return findLegalAction(Aspect, Action);
Tim Northover75ad0772016-07-20 21:13:29 +000086
Tim Northovera01bece2016-08-23 19:30:42 +000087 unsigned Opcode = Aspect.Opcode;
88 LLT Ty = Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000089 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +000090 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +000091 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
92 return std::make_pair(Legal, Ty);
93
94 assert(DefaultAction->second == NarrowScalar && "unexpected default");
Tim Northovera01bece2016-08-23 19:30:42 +000095 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +000096 }
97
98 LLT EltTy = Ty.getElementType();
99 int NumElts = Ty.getNumElements();
100
101 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
102 if (ScalarAction != ScalarInVectorActions.end() &&
103 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +0000104 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +0000105
106 // The element type is legal in principle, but the number of elements is
107 // wrong.
108 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
109 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +0000110 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000111
112 if (MaxLegalElts == 0) {
113 // Scalarize if there's no legal vector type, which is just a special case
114 // of FewerElements.
115 return std::make_pair(FewerElements, EltTy);
116 }
117
Tim Northovera01bece2016-08-23 19:30:42 +0000118 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000119}
120
Tim Northovera01bece2016-08-23 19:30:42 +0000121std::tuple<MachineLegalizer::LegalizeAction, unsigned, LLT>
Tim Northover0f140c72016-09-09 11:46:34 +0000122MachineLegalizer::getAction(const MachineInstr &MI,
123 const MachineRegisterInfo &MRI) const {
124 SmallBitVector SeenTypes(8);
125 const MCOperandInfo *OpInfo = MI.getDesc().OpInfo;
126 for (unsigned i = 0; i < MI.getDesc().getNumOperands(); ++i) {
127 if (!OpInfo[i].isGenericType())
128 continue;
129
130 // We don't want to repeatedly check the same operand index, that
131 // could get expensive.
132 unsigned TypeIdx = OpInfo[i].getGenericTypeIndex();
133 if (SeenTypes[TypeIdx])
134 continue;
135
136 SeenTypes.set(TypeIdx);
137
138 LLT Ty = MRI.getType(MI.getOperand(i).getReg());
139 auto Action = getAction({MI.getOpcode(), TypeIdx, Ty});
Tim Northovera01bece2016-08-23 19:30:42 +0000140 if (Action.first != Legal)
Tim Northover0f140c72016-09-09 11:46:34 +0000141 return std::make_tuple(Action.first, TypeIdx, Action.second);
Tim Northovera01bece2016-08-23 19:30:42 +0000142 }
Richard Smith84c4cc42016-08-23 22:21:58 +0000143 return std::make_tuple(Legal, 0, LLT{});
Tim Northover75ad0772016-07-20 21:13:29 +0000144}
145
Tim Northover0f140c72016-09-09 11:46:34 +0000146bool MachineLegalizer::isLegal(const MachineInstr &MI,
147 const MachineRegisterInfo &MRI) const {
148 return std::get<0>(getAction(MI, MRI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000149}
150
Tim Northovera01bece2016-08-23 19:30:42 +0000151LLT MachineLegalizer::findLegalType(const InstrAspect &Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000152 LegalizeAction Action) const {
153 switch(Action) {
154 default:
155 llvm_unreachable("Cannot find legal type");
156 case Legal:
Tim Northovercecee562016-08-26 17:46:13 +0000157 case Lower:
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000158 case Libcall:
Tim Northovera01bece2016-08-23 19:30:42 +0000159 return Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +0000160 case NarrowScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000161 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000162 [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
163 }
164 case WidenScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000165 return findLegalType(Aspect, [&](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000166 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
167 });
Tim Northover75ad0772016-07-20 21:13:29 +0000168 }
169 case FewerElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000170 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000171 [&](LLT Ty) -> LLT { return Ty.halfElements(); });
172 }
173 case MoreElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000174 return findLegalType(Aspect,
175 [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000176 }
177 }
178}