blob: 2d490b7c303e9734509937c57c7863bede72d58d [file] [log] [blame]
Diana Picus22274932016-11-11 08:27:37 +00001//===- ARMLegalizerInfo.cpp --------------------------------------*- C++ -*-==//
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/// \file
10/// This file implements the targeting of the Machinelegalizer class for ARM.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
14#include "ARMLegalizerInfo.h"
Diana Picus7cab0782017-02-17 11:25:17 +000015#include "ARMSubtarget.h"
Diana Picusf53865d2017-04-24 09:12:19 +000016#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000018#include "llvm/CodeGen/ValueTypes.h"
19#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Type.h"
21#include "llvm/Target/TargetOpcodes.h"
22
23using namespace llvm;
24
25#ifndef LLVM_BUILD_GLOBAL_ISEL
26#error "You shouldn't build this"
27#endif
28
Diana Picus7cab0782017-02-17 11:25:17 +000029ARMLegalizerInfo::ARMLegalizerInfo(const ARMSubtarget &ST) {
Diana Picus812caee2016-12-16 12:54:46 +000030 using namespace TargetOpcode;
Diana Picus5a724452016-12-19 14:07:56 +000031
Diana Picus519807f2016-12-19 11:26:31 +000032 const LLT p0 = LLT::pointer(0, 32);
Diana Picus5a724452016-12-19 14:07:56 +000033
Diana Picusd83df5d2017-01-25 08:47:40 +000034 const LLT s1 = LLT::scalar(1);
Diana Picus5a724452016-12-19 14:07:56 +000035 const LLT s8 = LLT::scalar(8);
36 const LLT s16 = LLT::scalar(16);
Diana Picus812caee2016-12-16 12:54:46 +000037 const LLT s32 = LLT::scalar(32);
Diana Picus21c3d8e2017-02-16 09:09:49 +000038 const LLT s64 = LLT::scalar(64);
Diana Picus812caee2016-12-16 12:54:46 +000039
Diana Picus519807f2016-12-19 11:26:31 +000040 setAction({G_FRAME_INDEX, p0}, Legal);
41
Diana Picusa2b632a2017-02-24 11:28:24 +000042 for (unsigned Op : {G_LOAD, G_STORE}) {
43 for (auto Ty : {s1, s8, s16, s32, p0})
44 setAction({Op, Ty}, Legal);
45 setAction({Op, 1, p0}, Legal);
46 }
Diana Picus519807f2016-12-19 11:26:31 +000047
Diana Picus01964272017-06-07 11:57:30 +000048 for (unsigned Op : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR}) {
Diana Picus9cfbc6d2017-05-11 09:45:57 +000049 for (auto Ty : {s1, s8, s16})
50 setAction({Op, Ty}, WidenScalar);
51 setAction({Op, s32}, Legal);
52 }
Diana Picus812caee2016-12-16 12:54:46 +000053
Diana Picusb70e88b2017-04-24 08:20:05 +000054 for (unsigned Op : {G_SDIV, G_UDIV}) {
Diana Picusf53865d2017-04-24 09:12:19 +000055 for (auto Ty : {s8, s16})
56 // FIXME: We need WidenScalar here, but in the case of targets with
57 // software division we'll also need Libcall afterwards. Treat as Custom
58 // until we have better support for chaining legalization actions.
59 setAction({Op, Ty}, Custom);
Diana Picusb70e88b2017-04-24 08:20:05 +000060 if (ST.hasDivideInARMMode())
61 setAction({Op, s32}, Legal);
62 else
63 setAction({Op, s32}, Libcall);
64 }
65
Quentin Colombet89dbea02017-01-27 01:30:46 +000066 for (unsigned Op : {G_SEXT, G_ZEXT}) {
Diana Picus8b6c6be2017-01-25 08:10:40 +000067 setAction({Op, s32}, Legal);
Diana Picusd83df5d2017-01-25 08:47:40 +000068 for (auto Ty : {s1, s8, s16})
Diana Picus8b6c6be2017-01-25 08:10:40 +000069 setAction({Op, 1, Ty}, Legal);
70 }
71
Diana Picus8598b172017-02-28 09:02:42 +000072 setAction({G_GEP, p0}, Legal);
73 setAction({G_GEP, 1, s32}, Legal);
74
Diana Picuse6beac62017-02-28 11:33:46 +000075 setAction({G_CONSTANT, s32}, Legal);
76
Diana Picusa5bab612017-04-07 09:41:39 +000077 if (!ST.useSoftFloat() && ST.hasVFP2()) {
Diana Picus7cab0782017-02-17 11:25:17 +000078 setAction({G_FADD, s32}, Legal);
79 setAction({G_FADD, s64}, Legal);
80
81 setAction({G_LOAD, s64}, Legal);
Diana Picusa2b632a2017-02-24 11:28:24 +000082 setAction({G_STORE, s64}, Legal);
Diana Picus1314a282017-04-11 10:52:34 +000083 } else {
84 for (auto Ty : {s32, s64})
85 setAction({G_FADD, Ty}, Libcall);
Diana Picus7cab0782017-02-17 11:25:17 +000086 }
Diana Picus4fa83c02017-02-08 13:23:04 +000087
Diana Picus3ff82c82017-04-10 09:27:39 +000088 for (unsigned Op : {G_FREM, G_FPOW})
89 for (auto Ty : {s32, s64})
90 setAction({Op, Ty}, Libcall);
Diana Picusa5bab612017-04-07 09:41:39 +000091
Diana Picus22274932016-11-11 08:27:37 +000092 computeTables();
93}
Diana Picusf53865d2017-04-24 09:12:19 +000094
95bool ARMLegalizerInfo::legalizeCustom(MachineInstr &MI,
96 MachineRegisterInfo &MRI,
97 MachineIRBuilder &MIRBuilder) const {
98 using namespace TargetOpcode;
99
100 switch (MI.getOpcode()) {
101 default:
102 return false;
103 case G_SDIV:
104 case G_UDIV: {
105 LLT Ty = MRI.getType(MI.getOperand(0).getReg());
106 if (Ty != LLT::scalar(16) && Ty != LLT::scalar(8))
107 return false;
108
109 // We need to widen to 32 bits and then maybe, if the target requires,
110 // transform into a libcall.
111 LegalizerHelper Helper(MIRBuilder.getMF());
112
113 MachineInstr *NewMI = nullptr;
114 Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) {
115 // Store the new, 32-bit div instruction.
116 if (MI->getOpcode() == G_SDIV || MI->getOpcode() == G_UDIV)
117 NewMI = MI;
118 });
119
120 auto Result = Helper.widenScalar(MI, 0, LLT::scalar(32));
121 Helper.MIRBuilder.stopRecordingInsertions();
122 if (Result == LegalizerHelper::UnableToLegalize) {
123 return false;
124 }
125 assert(NewMI && "Couldn't find widened instruction");
126 assert((NewMI->getOpcode() == G_SDIV || NewMI->getOpcode() == G_UDIV) &&
127 "Unexpected widened instruction");
128 assert(MRI.getType(NewMI->getOperand(0).getReg()).getSizeInBits() == 32 &&
129 "Unexpected type for the widened instruction");
130
131 Result = Helper.legalizeInstrStep(*NewMI);
132 if (Result == LegalizerHelper::UnableToLegalize) {
133 return false;
134 }
135 return true;
136 }
137 }
138}