blob: dd32b1fe10fc36eda00f901642bc7ba0cce8bb16 [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.
30 DefaultActions[TargetOpcode::G_ANYEXTEND] = Legal;
31 DefaultActions[TargetOpcode::G_TRUNC] = Legal;
32
Tim Northover75ad0772016-07-20 21:13:29 +000033 DefaultActions[TargetOpcode::G_ADD] = NarrowScalar;
34}
35
Tim Northover75ad0772016-07-20 21:13:29 +000036void MachineLegalizer::computeTables() {
37 for (auto &Op : Actions) {
Tim Northovera01bece2016-08-23 19:30:42 +000038 LLT Ty = Op.first.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000039 if (!Ty.isVector())
40 continue;
41
Tim Northovera01bece2016-08-23 19:30:42 +000042 auto &Entry = MaxLegalVectorElts[std::make_pair(Op.first.Opcode,
43 Ty.getElementType())];
Tim Northover75ad0772016-07-20 21:13:29 +000044 Entry = std::max(Entry, Ty.getNumElements());
45 }
46
47 TablesInitialized = true;
48}
49
50// FIXME: inefficient implementation for now. Without ComputeValueVTs we're
51// probably going to need specialized lookup structures for various types before
52// we have any hope of doing well with something like <13 x i3>. Even the common
53// cases should do better than what we have now.
54std::pair<MachineLegalizer::LegalizeAction, LLT>
Tim Northovera01bece2016-08-23 19:30:42 +000055MachineLegalizer::getAction(const InstrAspect &Aspect) const {
Tim Northover75ad0772016-07-20 21:13:29 +000056 assert(TablesInitialized && "backend forgot to call computeTables");
57 // These *have* to be implemented for now, they're the fundamental basis of
58 // how everything else is transformed.
59
Tim Northover33b07d62016-07-22 20:03:43 +000060 // FIXME: the long-term plan calls for expansion in terms of load/store (if
61 // they're not legal).
Tim Northovera01bece2016-08-23 19:30:42 +000062 if (Aspect.Opcode == TargetOpcode::G_SEQUENCE ||
63 Aspect.Opcode == TargetOpcode::G_EXTRACT)
64 return std::make_pair(Legal, Aspect.Type);
Tim Northover33b07d62016-07-22 20:03:43 +000065
Tim Northovera01bece2016-08-23 19:30:42 +000066 auto ActionIt = Actions.find(Aspect);
Tim Northover75ad0772016-07-20 21:13:29 +000067 if (ActionIt != Actions.end())
Tim Northovera01bece2016-08-23 19:30:42 +000068 return findLegalAction(Aspect, ActionIt->second);
Tim Northover75ad0772016-07-20 21:13:29 +000069
Tim Northovera01bece2016-08-23 19:30:42 +000070 unsigned Opcode = Aspect.Opcode;
71 LLT Ty = Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +000072 if (!Ty.isVector()) {
Tim Northovera01bece2016-08-23 19:30:42 +000073 auto DefaultAction = DefaultActions.find(Aspect.Opcode);
Tim Northover75ad0772016-07-20 21:13:29 +000074 if (DefaultAction != DefaultActions.end() && DefaultAction->second == Legal)
75 return std::make_pair(Legal, Ty);
76
77 assert(DefaultAction->second == NarrowScalar && "unexpected default");
Tim Northovera01bece2016-08-23 19:30:42 +000078 return findLegalAction(Aspect, NarrowScalar);
Tim Northover75ad0772016-07-20 21:13:29 +000079 }
80
81 LLT EltTy = Ty.getElementType();
82 int NumElts = Ty.getNumElements();
83
84 auto ScalarAction = ScalarInVectorActions.find(std::make_pair(Opcode, EltTy));
85 if (ScalarAction != ScalarInVectorActions.end() &&
86 ScalarAction->second != Legal)
Tim Northovera01bece2016-08-23 19:30:42 +000087 return findLegalAction(Aspect, ScalarAction->second);
Tim Northover75ad0772016-07-20 21:13:29 +000088
89 // The element type is legal in principle, but the number of elements is
90 // wrong.
91 auto MaxLegalElts = MaxLegalVectorElts.lookup(std::make_pair(Opcode, EltTy));
92 if (MaxLegalElts > NumElts)
Tim Northovera01bece2016-08-23 19:30:42 +000093 return findLegalAction(Aspect, MoreElements);
Tim Northover75ad0772016-07-20 21:13:29 +000094
95 if (MaxLegalElts == 0) {
96 // Scalarize if there's no legal vector type, which is just a special case
97 // of FewerElements.
98 return std::make_pair(FewerElements, EltTy);
99 }
100
Tim Northovera01bece2016-08-23 19:30:42 +0000101 return findLegalAction(Aspect, FewerElements);
Tim Northover75ad0772016-07-20 21:13:29 +0000102}
103
Tim Northovera01bece2016-08-23 19:30:42 +0000104std::tuple<MachineLegalizer::LegalizeAction, unsigned, LLT>
Ahmed Bougachaf49ab9a2016-08-02 11:41:03 +0000105MachineLegalizer::getAction(const MachineInstr &MI) const {
Tim Northovera01bece2016-08-23 19:30:42 +0000106 for (unsigned i = 0; i < MI.getNumTypes(); ++i) {
107 auto Action = getAction({MI.getOpcode(), i, MI.getType(i)});
108 if (Action.first != Legal)
109 return {Action.first, i, Action.second};
110 }
111 return {Legal, 0, LLT{}};
Tim Northover75ad0772016-07-20 21:13:29 +0000112}
113
Ahmed Bougachaf49ab9a2016-08-02 11:41:03 +0000114bool MachineLegalizer::isLegal(const MachineInstr &MI) const {
Tim Northovera01bece2016-08-23 19:30:42 +0000115 return std::get<0>(getAction(MI)) == Legal;
Tim Northover75ad0772016-07-20 21:13:29 +0000116}
117
Tim Northovera01bece2016-08-23 19:30:42 +0000118LLT MachineLegalizer::findLegalType(const InstrAspect &Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000119 LegalizeAction Action) const {
120 switch(Action) {
121 default:
122 llvm_unreachable("Cannot find legal type");
123 case Legal:
Tim Northovera01bece2016-08-23 19:30:42 +0000124 return Aspect.Type;
Tim Northover75ad0772016-07-20 21:13:29 +0000125 case NarrowScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000126 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000127 [&](LLT Ty) -> LLT { return Ty.halfScalarSize(); });
128 }
129 case WidenScalar: {
Tim Northovera01bece2016-08-23 19:30:42 +0000130 return findLegalType(Aspect, [&](LLT Ty) -> LLT {
Tim Northoverea904f92016-08-19 22:40:00 +0000131 return Ty.getSizeInBits() < 8 ? LLT::scalar(8) : Ty.doubleScalarSize();
132 });
Tim Northover75ad0772016-07-20 21:13:29 +0000133 }
134 case FewerElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000135 return findLegalType(Aspect,
Tim Northover75ad0772016-07-20 21:13:29 +0000136 [&](LLT Ty) -> LLT { return Ty.halfElements(); });
137 }
138 case MoreElements: {
Tim Northovera01bece2016-08-23 19:30:42 +0000139 return findLegalType(Aspect,
140 [&](LLT Ty) -> LLT { return Ty.doubleElements(); });
Tim Northover75ad0772016-07-20 21:13:29 +0000141 }
142 }
143}