blob: dccb8578acd4029983dc8362b4ac53f17cd5559e [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 Picus812caee2016-12-16 12:54:46 +000088bool ARMInstructionSelector::select(MachineInstr &I) const {
89 assert(I.getParent() && "Instruction should be in a basic block!");
90 assert(I.getParent()->getParent() && "Instruction should be in a function!");
91
92 auto &MBB = *I.getParent();
93 auto &MF = *MBB.getParent();
94 auto &MRI = MF.getRegInfo();
95
96 if (!isPreISelGenericOpcode(I.getOpcode())) {
97 if (I.isCopy())
98 return selectCopy(I, TII, MRI, TRI, RBI);
99
100 return true;
101 }
102
Diana Picus519807f2016-12-19 11:26:31 +0000103 MachineInstrBuilder MIB{MF, I};
104
105 using namespace TargetOpcode;
106 switch (I.getOpcode()) {
Diana Picus8b6c6be2017-01-25 08:10:40 +0000107 case G_SEXT:
108 case G_ZEXT: {
109 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
110 // FIXME: Smaller destination sizes coming soon!
111 if (DstTy.getSizeInBits() != 32) {
112 DEBUG(dbgs() << "Unsupported destination size for extension");
113 return false;
114 }
115
116 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
117 unsigned SrcSize = SrcTy.getSizeInBits();
118 switch (SrcSize) {
119 case 8:
120 case 16: {
121 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
122 I.setDesc(TII.get(NewOpc));
123 MIB.addImm(0).add(predOps(ARMCC::AL));
124 break;
125 }
126 default:
127 DEBUG(dbgs() << "Unsupported source size for extension");
128 return false;
129 }
130 break;
131 }
Diana Picus519807f2016-12-19 11:26:31 +0000132 case G_ADD:
Diana Picus812caee2016-12-16 12:54:46 +0000133 I.setDesc(TII.get(ARM::ADDrr));
Diana Picus8a73f552017-01-13 10:18:01 +0000134 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000135 break;
136 case G_FRAME_INDEX:
137 // Add 0 to the given frame index and hope it will eventually be folded into
138 // the user(s).
139 I.setDesc(TII.get(ARM::ADDri));
Diana Picus8a73f552017-01-13 10:18:01 +0000140 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
Diana Picus519807f2016-12-19 11:26:31 +0000141 break;
142 case G_LOAD:
143 I.setDesc(TII.get(ARM::LDRi12));
Diana Picus4f8c3e12017-01-13 09:37:56 +0000144 MIB.addImm(0).add(predOps(ARMCC::AL));
Diana Picus519807f2016-12-19 11:26:31 +0000145 break;
146 default:
147 return false;
Diana Picus812caee2016-12-16 12:54:46 +0000148 }
149
Diana Picus519807f2016-12-19 11:26:31 +0000150 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Diana Picus22274932016-11-11 08:27:37 +0000151}