blob: 6c00810fb684bc90c21c1b794b78a29ce9e6fbfc [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
20#include "llvm/CodeGen/MachineInstr.h"
21#include "llvm/CodeGen/ValueTypes.h"
22#include "llvm/CodeGen/GlobalISel/MachineLegalizer.h"
23#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 Northoverb3a0be42016-08-23 21:01:20 +000033 DefaultActions[TargetOpcode::G_INTRINSIC] = Legal;
34 DefaultActions[TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS] = Legal;
35
Tim Northover75ad0772016-07-20 21:13:29 +000036 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
Tim Northoverb3a0be42016-08-23 21:01:20 +000037
38 DefaultActions[TargetOpcode::G_BRCOND] = WidenScalar;
Tim Northover75ad0772016-07-20 21:13:29 +000039}
40
Tim Northover75ad0772016-07-20 21:13:29 +000041void MachineLegalizer::computeTables() {
42 for (auto &Op : Actions) {
Tim Northovera01bece2016-08-23 19:30:42 +000043 LLT Ty = Op.first.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000044 if (!Ty.isVector())
45 continue;
46
Tim Northovera01bece2016-08-23 19:30:42 +000047 auto &Entry = MaxLegalVectorElts[std::make_pair(Op.first.Opcode,
48 Ty.getElementType())];
Tim Northover75ad0772016-07-20 21:13:29 +000049 Entry = std::max(Entry, Ty.getNumElements());
50 }
51
52 TablesInitialized = true;
53}
54
55// FIXME: inefficient implementation for now. Without ComputeValueVTs we're
56// probably going to need specialized lookup structures for various types before
57// we have any hope of doing well with something like <13 x i3>. Even the common
58// cases should do better than what we have now.
59std::pair<MachineLegalizer::LegalizeAction, LLT>
Tim Northovera01bece2016-08-23 19:30:42 +000060MachineLegalizer::getAction(const InstrAspect &Aspect) const {
Tim Northover75ad0772016-07-20 21:13:29 +000061 assert(TablesInitialized && "backend forgot to call computeTables");
62 // These *have* to be implemented for now, they're the fundamental basis of
63 // how everything else is transformed.
64
Tim Northover33b07d62016-07-22 20:03:43 +000065 // FIXME: the long-term plan calls for expansion in terms of load/store (if
66 // they're not legal).
Tim Northovera01bece2016-08-23 19:30:42 +000067 if (Aspect.Opcode == TargetOpcode::G_SEQUENCE ||
68 Aspect.Opcode == TargetOpcode::G_EXTRACT)
69 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000070
Tim Northovera01bece2016-08-23 19:30:42 +000071 auto ActionIt = Actions.find(Aspect);
Tim Northover75ad0772016-07-20 21:13:29 +000072 if (ActionIt != Actions.end())
Tim Northovera01bece2016-08-23 19:30:42 +000073 return findLegalAction(Aspect, ActionIt->second);
Tim Northover75ad0772016-07-20 21:13:29 +000074
Tim Northovera01bece2016-08-23 19:30:42 +000075 unsigned Opcode = Aspect.Opcode;
76 LLT Ty = Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000077 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +000078 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +000079 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
80 return std::make_pair(Legal, Ty);
81
82 assert(DefaultAction->second == NarrowScalar && "unexpected default");
Tim Northovera01bece2016-08-23 19:30:42 +000083 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +000084 }
85
86 LLT EltTy = Ty.getElementType();
87 int NumElts = Ty.getNumElements();
88
89 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
90 if (ScalarAction != ScalarInVectorActions.end() &&
91 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +000092 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +000093
94 // The element type is legal in principle, but the number of elements is
95 // wrong.
96 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
97 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +000098 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +000099
100 if (MaxLegalElts == 0) {
101 // Scalarize if there's no legal vector type, which is just a special case
102 // of FewerElements.
103 return std::make_pair(FewerElements, EltTy);
104 }
105
Tim Northovera01bece2016-08-23 19:30:42 +0000106 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000107}
108
Tim Northovera01bece2016-08-23 19:30:42 +0000109std::tuple<MachineLegalizer::LegalizeAction, unsigned, LLT>
Ahmed Bougachaf49ab9a2016-08-02 11:41:03 +0000110MachineLegalizer::getAction(const MachineInstr &MI) const {
Tim Northovera01bece2016-08-23 19:30:42 +0000111 for (unsigned i = 0; i < MI.getNumTypes(); ++i) {
112 auto Action = getAction({MI.getOpcode(), i, MI.getType(i)});
113 if (Action.first != Legal)
114 return {Action.first, i, Action.second};
115 }
116 return {Legal, 0, LLT{}};
Tim Northover75ad0772016-07-20 21:13:29 +0000117}
118
Ahmed Bougachaf49ab9a2016-08-02 11:41:03 +0000119bool MachineLegalizer::isLegal(const MachineInstr &MI) const {
Tim Northovera01bece2016-08-23 19:30:42 +0000120 return std::get<0>(getAction(MI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000121}
122
Tim Northovera01bece2016-08-23 19:30:42 +0000123LLT MachineLegalizer::findLegalType(const InstrAspect &Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000124 LegalizeAction Action) const {
125 switch(Action) {
126 default:
127 llvm_unreachable("Cannot find legal type");
128 case Legal:
Tim Northovera01bece2016-08-23 19:30:42 +0000129 return Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +0000130 case NarrowScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000131 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000132 [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
133 }
134 case WidenScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000135 return findLegalType(Aspect, [&](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000136 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
137 });
Tim Northover75ad0772016-07-20 21:13:29 +0000138 }
139 case FewerElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000140 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000141 [&](LLT Ty) -> LLT { return Ty.halfElements(); });
142 }
143 case MoreElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000144 return findLegalType(Aspect,
145 [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000146 }
147 }
148}