Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1 | //===- 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 Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame^] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | |
| 21 | #define DEBUG_TYPE "arm-isel" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 26 | #error "You shouldn't build this" |
| 27 | #endif |
| 28 | |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 29 | ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI, |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 30 | const ARMRegisterBankInfo &RBI) |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 31 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame^] | 32 | TRI(*STI.getRegisterInfo()), RBI(RBI) {} |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 33 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame^] | 34 | static 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); |
| 42 | assert(RegBank && "Can't get reg bank for virtual register"); |
| 43 | |
| 44 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
| 45 | unsigned SrcReg = I.getOperand(1).getReg(); |
| 46 | const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI); |
| 47 | (void)SrcSize; |
| 48 | assert(DstSize == SrcSize && "Copy with different width?!"); |
| 49 | |
| 50 | assert(RegBank->getID() == ARM::GPRRegBankID && "Unsupported reg bank"); |
| 51 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
| 52 | |
| 53 | // No need to constrain SrcReg. It will get constrained when |
| 54 | // we hit another of its uses or its defs. |
| 55 | // Copies do not have constraints. |
| 56 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 57 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 58 | << " operand\n"); |
| 59 | return false; |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | bool ARMInstructionSelector::select(MachineInstr &I) const { |
| 65 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 66 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 67 | |
| 68 | auto &MBB = *I.getParent(); |
| 69 | auto &MF = *MBB.getParent(); |
| 70 | auto &MRI = MF.getRegInfo(); |
| 71 | |
| 72 | if (!isPreISelGenericOpcode(I.getOpcode())) { |
| 73 | if (I.isCopy()) |
| 74 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | if (I.getOpcode() == TargetOpcode::G_ADD) { |
| 80 | I.setDesc(TII.get(ARM::ADDrr)); |
| 81 | AddDefaultCC(AddDefaultPred(MachineInstrBuilder(MF, I))); |
| 82 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
| 83 | } |
| 84 | |
| 85 | return false; |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 86 | } |