blob: cc3d4ecd7c6faf33800297e5259668b6609b4a36 [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 Northover75ad0772016-07-20 21:13:29 +000021#include "llvm/CodeGen/MachineInstr.h"
22#include "llvm/CodeGen/ValueTypes.h"
Tim Northover75ad0772016-07-20 21:13:29 +000023#include "llvm/IR/Type.h"
24#include "llvm/Target/TargetOpcodes.h"
25using namespace llvm;
26
27MachineLegalizer::MachineLegalizer() : TablesInitialized(false) {
Tim Northover32335812016-08-04 18:35:11 +000028 // FIXME: these two can be legalized to the fundamental load/store Jakob
29 // proposed. Once loads & stores are supported.
Tim Northoverbdf67c92016-08-23 21:01:33 +000030 DefaultActions[TargetOpcode::G_ANYEXT] = Legal;
Tim Northover32335812016-08-04 18:35:11 +000031 DefaultActions[TargetOpcode::G_TRUNC] = Legal;
32
Tim Northover11a23542016-08-31 21:24:02 +000033 // G_TYPE is essentially an annotated COPY so it's always legal.
34 DefaultActions[TargetOpcode::G_TYPE] = 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 Northoverb3a0be42016-08-23 21:01:20 +000040
41 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
Tim Northover75ad0772016-07-20 21:13:29 +000042}
43
Tim Northover75ad0772016-07-20 21:13:29 +000044void MachineLegalizer::computeTables() {
Tim Northoverf8bab1c2016-08-29 21:00:00 +000045 for (unsigned Opcode = 0; Opcode <= LastOp - FirstOp; ++Opcode) {
46 for (unsigned Idx = 0; Idx != Actions[Opcode].size(); ++Idx) {
47 for (auto &Action : Actions[Opcode][Idx]) {
48 LLT Ty = Action.first;
49 if (!Ty.isVector())
50 continue;
Tim Northover75ad0772016-07-20 21:13:29 +000051
Tim Northoverf8bab1c2016-08-29 21:00:00 +000052 auto &Entry = MaxLegalVectorElts[std::make_pair(Opcode + FirstOp,
53 Ty.getElementType())];
54 Entry = std::max(Entry, Ty.getNumElements());
55 }
56 }
Tim Northover75ad0772016-07-20 21:13:29 +000057 }
58
59 TablesInitialized = true;
60}
61
62// FIXME: inefficient implementation for now. Without ComputeValueVTs we're
63// probably going to need specialized lookup structures for various types before
64// we have any hope of doing well with something like <13 x i3>. Even the common
65// cases should do better than what we have now.
66std::pair<MachineLegalizer::LegalizeAction, LLT>
Tim Northovera01bece2016-08-23 19:30:42 +000067MachineLegalizer::getAction(const InstrAspect &Aspect) const {
Tim Northover75ad0772016-07-20 21:13:29 +000068 assert(TablesInitialized && "backend forgot to call computeTables");
69 // These *have* to be implemented for now, they're the fundamental basis of
70 // how everything else is transformed.
71
Tim Northover33b07d62016-07-22 20:03:43 +000072 // FIXME: the long-term plan calls for expansion in terms of load/store (if
73 // they're not legal).
Tim Northovera01bece2016-08-23 19:30:42 +000074 if (Aspect.Opcode == TargetOpcode::G_SEQUENCE ||
75 Aspect.Opcode == TargetOpcode::G_EXTRACT)
76 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000077
Tim Northoverf8bab1c2016-08-29 21:00:00 +000078 LegalizeAction Action = findInActions(Aspect);
79 if (Action != NotFound)
80 return findLegalAction(Aspect, Action);
Tim Northover75ad0772016-07-20 21:13:29 +000081
Tim Northovera01bece2016-08-23 19:30:42 +000082 unsigned Opcode = Aspect.Opcode;
83 LLT Ty = Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000084 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +000085 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +000086 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
87 return std::make_pair(Legal, Ty);
88
89 assert(DefaultAction->second == NarrowScalar && "unexpected default");
Tim Northovera01bece2016-08-23 19:30:42 +000090 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +000091 }
92
93 LLT EltTy = Ty.getElementType();
94 int NumElts = Ty.getNumElements();
95
96 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
97 if (ScalarAction != ScalarInVectorActions.end() &&
98 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +000099 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +0000100
101 // The element type is legal in principle, but the number of elements is
102 // wrong.
103 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
104 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +0000105 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000106
107 if (MaxLegalElts == 0) {
108 // Scalarize if there's no legal vector type, which is just a special case
109 // of FewerElements.
110 return std::make_pair(FewerElements, EltTy);
111 }
112
Tim Northovera01bece2016-08-23 19:30:42 +0000113 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000114}
115
Tim Northovera01bece2016-08-23 19:30:42 +0000116std::tuple<MachineLegalizer::LegalizeAction, unsigned, LLT>
Ahmed Bougachaf49ab9a2016-08-02 11:41:03 +0000117MachineLegalizer::getAction(const MachineInstr &MI) const {
Tim Northovera01bece2016-08-23 19:30:42 +0000118 for (unsigned i = 0; i < MI.getNumTypes(); ++i) {
119 auto Action = getAction({MI.getOpcode(), i, MI.getType(i)});
120 if (Action.first != Legal)
Richard Smith84c4cc42016-08-23 22:21:58 +0000121 return std::make_tuple(Action.first, i, Action.second);
Tim Northovera01bece2016-08-23 19:30:42 +0000122 }
Richard Smith84c4cc42016-08-23 22:21:58 +0000123 return std::make_tuple(Legal, 0, LLT{});
Tim Northover75ad0772016-07-20 21:13:29 +0000124}
125
Ahmed Bougachaf49ab9a2016-08-02 11:41:03 +0000126bool MachineLegalizer::isLegal(const MachineInstr &MI) const {
Tim Northovera01bece2016-08-23 19:30:42 +0000127 return std::get<0>(getAction(MI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000128}
129
Tim Northovera01bece2016-08-23 19:30:42 +0000130LLT MachineLegalizer::findLegalType(const InstrAspect &Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000131 LegalizeAction Action) const {
132 switch(Action) {
133 default:
134 llvm_unreachable("Cannot find legal type");
135 case Legal:
Tim Northovercecee562016-08-26 17:46:13 +0000136 case Lower:
Tim Northoveredb3c8c2016-08-29 19:07:16 +0000137 case Libcall:
Tim Northovera01bece2016-08-23 19:30:42 +0000138 return Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +0000139 case NarrowScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000140 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000141 [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
142 }
143 case WidenScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000144 return findLegalType(Aspect, [&](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000145 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
146 });
Tim Northover75ad0772016-07-20 21:13:29 +0000147 }
148 case FewerElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000149 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000150 [&](LLT Ty) -> LLT { return Ty.halfElements(); });
151 }
152 case MoreElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000153 return findLegalType(Aspect,
154 [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000155 }
156 }
157}