blob: 53298f6476d12be54a1a55e8ec7e5c8fff8c8e5d [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
Benjamin Kramer24bf8682016-12-16 13:13:03 +000045 assert(MRI.getType(DstReg).getSizeInBits() ==
46 RBI.getSizeInBits(I.getOperand(1).getReg(), MRI, TRI) &&
47 "Copy with different width?!");
Diana Picus812caee2016-12-16 12:54:46 +000048
49 assert(RegBank->getID() == ARM::GPRRegBankID && "Unsupported reg bank");
50 const TargetRegisterClass *RC = &ARM::GPRRegClass;
51
52 // No need to constrain SrcReg. It will get constrained when
53 // we hit another of its uses or its defs.
54 // Copies do not have constraints.
55 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
56 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
57 << " operand\n");
58 return false;
59 }
60 return true;
61}
62
63bool ARMInstructionSelector::select(MachineInstr &I) const {
64 assert(I.getParent() && "Instruction should be in a basic block!");
65 assert(I.getParent()->getParent() && "Instruction should be in a function!");
66
67 auto &MBB = *I.getParent();
68 auto &MF = *MBB.getParent();
69 auto &MRI = MF.getRegInfo();
70
71 if (!isPreISelGenericOpcode(I.getOpcode())) {
72 if (I.isCopy())
73 return selectCopy(I, TII, MRI, TRI, RBI);
74
75 return true;
76 }
77
78 if (I.getOpcode() == TargetOpcode::G_ADD) {
79 I.setDesc(TII.get(ARM::ADDrr));
80 AddDefaultCC(AddDefaultPred(MachineInstrBuilder(MF, I)));
81 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
82 }
83
84 return false;
Diana Picus22274932016-11-11 08:27:37 +000085}