blob: 492024ecd1a594b3b8b39c44cd1372cb4ae3f624 [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.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
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
15#include "AArch64InstructionSelector.h"
16#include "AArch64InstrInfo.h"
17#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/IR/Type.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28
29#define DEBUG_TYPE "aarch64-isel"
30
31using namespace llvm;
32
33#ifndef LLVM_BUILD_GLOBAL_ISEL
34#error "You shouldn't build this"
35#endif
36
37AArch64InstructionSelector::AArch64InstructionSelector(
38 const AArch64Subtarget &STI, const AArch64RegisterBankInfo &RBI)
39 : InstructionSelector(), TII(*STI.getInstrInfo()),
40 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
41
42/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
43/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
44/// and of size \p OpSize.
45/// \returns \p GenericOpc if the combination is unsupported.
46static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
47 unsigned OpSize) {
48 switch (RegBankID) {
49 case AArch64::GPRRegBankID:
50 switch (OpSize) {
51 case 32:
52 switch (GenericOpc) {
53 case TargetOpcode::G_OR:
54 return AArch64::ORRWrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +000055 case TargetOpcode::G_AND:
56 return AArch64::ANDWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000057 case TargetOpcode::G_ADD:
58 return AArch64::ADDWrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +000059 case TargetOpcode::G_SUB:
60 return AArch64::SUBWrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000061 default:
62 return GenericOpc;
63 }
64 case 64:
65 switch (GenericOpc) {
66 case TargetOpcode::G_OR:
67 return AArch64::ORRXrr;
Ahmed Bougacha61a79282016-07-28 16:58:31 +000068 case TargetOpcode::G_AND:
69 return AArch64::ANDXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000070 case TargetOpcode::G_ADD:
71 return AArch64::ADDXrr;
Ahmed Bougachad7748d62016-07-28 16:58:35 +000072 case TargetOpcode::G_SUB:
73 return AArch64::SUBXrr;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +000074 default:
75 return GenericOpc;
76 }
77 }
78 };
79 return GenericOpc;
80}
81
82bool AArch64InstructionSelector::select(MachineInstr &I) const {
83 assert(I.getParent() && "Instruction should be in a basic block!");
84 assert(I.getParent()->getParent() && "Instruction should be in a function!");
85
86 MachineBasicBlock &MBB = *I.getParent();
87 MachineFunction &MF = *MBB.getParent();
88 MachineRegisterInfo &MRI = MF.getRegInfo();
89
90 // FIXME: Is there *really* nothing to be done here? This assumes that
91 // no upstream pass introduces things like generic vreg on copies or
92 // target-specific instructions.
93 // We should document (and verify) that assumption.
94 if (!isPreISelGenericOpcode(I.getOpcode()))
95 return true;
96
97 if (I.getNumOperands() != I.getNumExplicitOperands()) {
98 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
99 return false;
100 }
101
102 LLT Ty = I.getType();
103 assert(Ty.isValid() && "Generic instruction doesn't have a type");
104
Ahmed Bougacha85505092016-07-28 17:15:15 +0000105 switch (I.getOpcode()) {
106 case TargetOpcode::G_BR: {
107 I.setDesc(TII.get(AArch64::B));
108 I.removeTypes();
109 return true;
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000110 }
111
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000112 case TargetOpcode::G_OR:
Ahmed Bougacha61a79282016-07-28 16:58:31 +0000113 case TargetOpcode::G_AND:
Ahmed Bougachad7748d62016-07-28 16:58:35 +0000114 case TargetOpcode::G_ADD:
115 case TargetOpcode::G_SUB: {
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000116 DEBUG(dbgs() << "AArch64: Selecting: binop\n");
117
Ahmed Bougacha85505092016-07-28 17:15:15 +0000118 if (!Ty.isSized()) {
119 DEBUG(dbgs() << "Generic binop should be sized\n");
120 return false;
121 }
122
123 // The size (in bits) of the operation, or 0 for the label type.
124 const unsigned OpSize = Ty.getSizeInBits();
125
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000126 // Reject the various things we don't support yet.
127 {
128 const RegisterBank *PrevOpBank = nullptr;
129 for (auto &MO : I.operands()) {
130 // FIXME: Support non-register operands.
131 if (!MO.isReg()) {
132 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
133 return false;
134 }
135
136 // FIXME: Can generic operations have physical registers operands? If
137 // so, this will need to be taught about that, and we'll need to get the
138 // bank out of the minimal class for the register.
139 // Either way, this needs to be documented (and possibly verified).
140 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
141 DEBUG(dbgs() << "Generic inst has physical register operand\n");
142 return false;
143 }
144
145 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
146 if (!OpBank) {
147 DEBUG(dbgs() << "Generic register has no bank or class\n");
148 return false;
149 }
150
151 if (PrevOpBank && OpBank != PrevOpBank) {
152 DEBUG(dbgs() << "Generic inst operands have different banks\n");
153 return false;
154 }
155 PrevOpBank = OpBank;
156 }
157 }
158
159 const unsigned DefReg = I.getOperand(0).getReg();
160 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
161
162 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
163 if (NewOpc == I.getOpcode())
164 return false;
165
166 I.setDesc(TII.get(NewOpc));
167 // FIXME: Should the type be always reset in setDesc?
Ahmed Bougacha46c05fc2016-07-28 16:58:27 +0000168 I.removeTypes();
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +0000169
170 // Now that we selected an opcode, we need to constrain the register
171 // operands to use appropriate classes.
172 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
173 }
174 }
175
176 return false;
177}