blob: 538060d4334b250751fcc98c11d8d690a136aeea [file] [log] [blame]
Diana Picus22274932016-11-11 08:27:37 +00001//===- ARMInstructionSelector.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 InstructionSelector class for ARM.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
14#include "ARMInstructionSelector.h"
15#include "ARMRegisterBankInfo.h"
16#include "ARMSubtarget.h"
17#include "ARMTargetMachine.h"
Diana Picus812caee2016-12-16 12:54:46 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000019#include "llvm/Support/Debug.h"
20
21#define DEBUG_TYPE "arm-isel"
22
23using namespace llvm;
24
25#ifndef LLVM_BUILD_GLOBAL_ISEL
26#error "You shouldn't build this"
27#endif
28
Diana Picus895c6aa2016-11-15 16:42:10 +000029ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI,
Diana Picus22274932016-11-11 08:27:37 +000030 const ARMRegisterBankInfo &RBI)
Diana Picus895c6aa2016-11-15 16:42:10 +000031 : InstructionSelector(), TII(*STI.getInstrInfo()),
Diana Picus812caee2016-12-16 12:54:46 +000032 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
Diana Picus22274932016-11-11 08:27:37 +000033
Diana Picus812caee2016-12-16 12:54:46 +000034static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
35 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
36 const RegisterBankInfo &RBI) {
37 unsigned DstReg = I.getOperand(0).getReg();
38 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
39 return true;
40
41 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
Benjamin Kramer24bf8682016-12-16 13:13:03 +000042 (void)RegBank;
Diana Picus812caee2016-12-16 12:54:46 +000043 assert(RegBank && "Can't get reg bank for virtual register");
44
Diana Picus36aa09f2016-12-19 14:07:50 +000045 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
Daniel Jasper24218d52016-12-19 14:24:22 +000046 (void)DstSize;
Diana Picus36aa09f2016-12-19 14:07:50 +000047 unsigned SrcReg = I.getOperand(1).getReg();
48 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
49 (void)SrcSize;
50 assert((DstSize == SrcSize ||
51 // Copies are a means to setup initial types, the number of
52 // bits may not exactly match.
53 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
54 DstSize <= SrcSize)) &&
Benjamin Kramer24bf8682016-12-16 13:13:03 +000055 "Copy with different width?!");
Diana Picus812caee2016-12-16 12:54:46 +000056
57 assert(RegBank->getID() == ARM::GPRRegBankID && "Unsupported reg bank");
58 const TargetRegisterClass *RC = &ARM::GPRRegClass;
59
60 // No need to constrain SrcReg. It will get constrained when
61 // we hit another of its uses or its defs.
62 // Copies do not have constraints.
63 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
64 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
65 << " operand\n");
66 return false;
67 }
68 return true;
69}
70
Diana Picus8b6c6be2017-01-25 08:10:40 +000071/// Select the opcode for simple extensions (that translate to a single SXT/UXT
72/// instruction). Extension operations more complicated than that should not
73/// invoke this.
74static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) {
75 using namespace TargetOpcode;
76
77 assert((Size == 8 || Size == 16) && "Unsupported size");
78
79 if (Opc == G_SEXT)
80 return Size == 8 ? ARM::SXTB : ARM::SXTH;
81
82 if (Opc == G_ZEXT)
83 return Size == 8 ? ARM::UXTB : ARM::UXTH;
84
85 llvm_unreachable("Unsupported opcode");
86}
87
Diana Picus278c7222017-01-26 09:20:47 +000088/// Select the opcode for simple loads. For types smaller than 32 bits, the
89/// value will be zero extended.
90static unsigned selectLoadOpCode(unsigned Size) {
91 switch (Size) {
92 case 1:
93 case 8:
94 return ARM::LDRBi12;
95 case 16:
96 return ARM::LDRH;
97 case 32:
98 return ARM::LDRi12;
99 }
100
101 llvm_unreachable("Unsupported size");
102}
103
Diana Picus812caee2016-12-16 12:54:46 +0000104bool ARMInstructionSelector::select(MachineInstr &I) const {
105 assert(I.getParent() && "Instruction should be in a basic block!");
106 assert(I.getParent()->getParent() && "Instruction should be in a function!");
107
108 auto &MBB = *I.getParent();
109 auto &MF = *MBB.getParent();
110 auto &MRI = MF.getRegInfo();
111
112 if (!isPreISelGenericOpcode(I.getOpcode())) {
113 if (I.isCopy())
114 return selectCopy(I, TII, MRI, TRI, RBI);
115
116 return true;
117 }
118
Diana Picus519807f2016-12-19 11:26:31 +0000119 MachineInstrBuilder MIB{MF, I};
Diana Picusd83df5d2017-01-25 08:47:40 +0000120 bool isSExt = false;
Diana Picus519807f2016-12-19 11:26:31 +0000121
122 using namespace TargetOpcode;
123 switch (I.getOpcode()) {
Diana Picus8b6c6be2017-01-25 08:10:40 +0000124 case G_SEXT:
Diana Picusd83df5d2017-01-25 08:47:40 +0000125 isSExt = true;
126 LLVM_FALLTHROUGH;
Diana Picus8b6c6be2017-01-25 08:10:40 +0000127 case G_ZEXT: {
128 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
129 // FIXME: Smaller destination sizes coming soon!
130 if (DstTy.getSizeInBits() != 32) {
131 DEBUG(dbgs() << "Unsupported destination size for extension");
132 return false;
133 }
134
135 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
136 unsigned SrcSize = SrcTy.getSizeInBits();
137 switch (SrcSize) {
Diana Picusd83df5d2017-01-25 08:47:40 +0000138 case 1: {
139 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
140 I.setDesc(TII.get(ARM::ANDri));
141 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
142
143 if (isSExt) {
144 unsigned SExtResult = I.getOperand(0).getReg();
145
146 // Use a new virtual register for the result of the AND
147 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
148 I.getOperand(0).setReg(AndResult);
149
150 auto InsertBefore = std::next(I.getIterator());
Martin Bohme8396e142017-01-25 14:28:19 +0000151 auto SubI =
Diana Picusd83df5d2017-01-25 08:47:40 +0000152 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri))
153 .addDef(SExtResult)
154 .addUse(AndResult)
155 .addImm(0)
156 .add(predOps(ARMCC::AL))
157 .add(condCodeOp());
158 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
159 return false;
160 }
161 break;
162 }
Diana Picus8b6c6be2017-01-25 08:10:40 +0000163 case 8:
164 case 16: {
165 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
166 I.setDesc(TII.get(NewOpc));
167 MIB.addImm(0).add(predOps(ARMCC::AL));
168 break;
169 }
170 default:
171 DEBUG(dbgs() << "Unsupported source size for extension");
172 return false;
173 }
174 break;
175 }
Diana Picus519807f2016-12-19 11:26:31 +0000176 case G_ADD:
Diana Picus812caee2016-12-16 12:54:46 +0000177 I.setDesc(TII.get(ARM::ADDrr));
Diana Picus8a73f552017-01-13 10:18:01 +0000178 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000179 break;
180 case G_FRAME_INDEX:
181 // Add 0 to the given frame index and hope it will eventually be folded into
182 // the user(s).
183 I.setDesc(TII.get(ARM::ADDri));
Diana Picus8a73f552017-01-13 10:18:01 +0000184 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000185 break;
Diana Picus278c7222017-01-26 09:20:47 +0000186 case G_LOAD: {
187 LLT ValTy = MRI.getType(I.getOperand(0).getReg());
188 const auto ValSize = ValTy.getSizeInBits();
189
190 if (ValSize != 32 && ValSize != 16 && ValSize != 8 && ValSize != 1)
191 return false;
192
193 const auto NewOpc = selectLoadOpCode(ValSize);
194 I.setDesc(TII.get(NewOpc));
195
196 if (NewOpc == ARM::LDRH)
197 // LDRH has a funny addressing mode (there's already a FIXME for it).
198 MIB.addReg(0);
Diana Picus4f8c3e12017-01-13 09:37:56 +0000199 MIB.addImm(0).add(predOps(ARMCC::AL));
Diana Picus519807f2016-12-19 11:26:31 +0000200 break;
Diana Picus278c7222017-01-26 09:20:47 +0000201 }
Diana Picus519807f2016-12-19 11:26:31 +0000202 default:
203 return false;
Diana Picus812caee2016-12-16 12:54:46 +0000204 }
205
Diana Picus519807f2016-12-19 11:26:31 +0000206 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Diana Picus22274932016-11-11 08:27:37 +0000207}