Path: blob/main/contrib/llvm-project/llvm/lib/Target/ARM/ARMInstrInfo.cpp
35269 views
//===-- ARMInstrInfo.cpp - ARM Instruction Information --------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains the ARM implementation of the TargetInstrInfo class.9//10//===----------------------------------------------------------------------===//1112#include "ARMInstrInfo.h"13#include "ARM.h"14#include "ARMConstantPoolValue.h"15#include "ARMMachineFunctionInfo.h"16#include "ARMTargetMachine.h"17#include "MCTargetDesc/ARMAddressingModes.h"18#include "llvm/ADT/STLExtras.h"19#include "llvm/CodeGen/LiveVariables.h"20#include "llvm/CodeGen/MachineFrameInfo.h"21#include "llvm/CodeGen/MachineInstrBuilder.h"22#include "llvm/CodeGen/MachineJumpTableInfo.h"23#include "llvm/CodeGen/MachineRegisterInfo.h"24#include "llvm/IR/Function.h"25#include "llvm/IR/GlobalVariable.h"26#include "llvm/IR/Module.h"27#include "llvm/MC/MCAsmInfo.h"28#include "llvm/MC/MCInst.h"29using namespace llvm;3031ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI) : ARMBaseInstrInfo(STI) {}3233/// Return the noop instruction to use for a noop.34MCInst ARMInstrInfo::getNop() const {35MCInst NopInst;36if (hasNOP()) {37NopInst.setOpcode(ARM::HINT);38NopInst.addOperand(MCOperand::createImm(0));39NopInst.addOperand(MCOperand::createImm(ARMCC::AL));40NopInst.addOperand(MCOperand::createReg(0));41} else {42NopInst.setOpcode(ARM::MOVr);43NopInst.addOperand(MCOperand::createReg(ARM::R0));44NopInst.addOperand(MCOperand::createReg(ARM::R0));45NopInst.addOperand(MCOperand::createImm(ARMCC::AL));46NopInst.addOperand(MCOperand::createReg(0));47NopInst.addOperand(MCOperand::createReg(0));48}49return NopInst;50}5152unsigned ARMInstrInfo::getUnindexedOpcode(unsigned Opc) const {53switch (Opc) {54default:55break;56case ARM::LDR_PRE_IMM:57case ARM::LDR_PRE_REG:58case ARM::LDR_POST_IMM:59case ARM::LDR_POST_REG:60return ARM::LDRi12;61case ARM::LDRH_PRE:62case ARM::LDRH_POST:63return ARM::LDRH;64case ARM::LDRB_PRE_IMM:65case ARM::LDRB_PRE_REG:66case ARM::LDRB_POST_IMM:67case ARM::LDRB_POST_REG:68return ARM::LDRBi12;69case ARM::LDRSH_PRE:70case ARM::LDRSH_POST:71return ARM::LDRSH;72case ARM::LDRSB_PRE:73case ARM::LDRSB_POST:74return ARM::LDRSB;75case ARM::STR_PRE_IMM:76case ARM::STR_PRE_REG:77case ARM::STR_POST_IMM:78case ARM::STR_POST_REG:79return ARM::STRi12;80case ARM::STRH_PRE:81case ARM::STRH_POST:82return ARM::STRH;83case ARM::STRB_PRE_IMM:84case ARM::STRB_PRE_REG:85case ARM::STRB_POST_IMM:86case ARM::STRB_POST_REG:87return ARM::STRBi12;88}8990return 0;91}9293void ARMInstrInfo::expandLoadStackGuard(MachineBasicBlock::iterator MI) const {94MachineFunction &MF = *MI->getParent()->getParent();95const ARMSubtarget &Subtarget = MF.getSubtarget<ARMSubtarget>();96const TargetMachine &TM = MF.getTarget();97Module &M = *MF.getFunction().getParent();9899if (M.getStackProtectorGuard() == "tls") {100expandLoadStackGuardBase(MI, ARM::MRC, ARM::LDRi12);101return;102}103104const GlobalValue *GV =105cast<GlobalValue>((*MI->memoperands_begin())->getValue());106107bool ForceELFGOTPIC = Subtarget.isTargetELF() && !GV->isDSOLocal();108if (!Subtarget.useMovt() || ForceELFGOTPIC) {109// For ELF non-PIC, use GOT PIC code sequence as well because R_ARM_GOT_ABS110// does not have assembler support.111if (TM.isPositionIndependent() || ForceELFGOTPIC)112expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_pcrel, ARM::LDRi12);113else114expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_abs, ARM::LDRi12);115return;116}117118if (!TM.isPositionIndependent()) {119expandLoadStackGuardBase(MI, ARM::MOVi32imm, ARM::LDRi12);120return;121}122123if (!Subtarget.isGVIndirectSymbol(GV)) {124expandLoadStackGuardBase(MI, ARM::MOV_ga_pcrel, ARM::LDRi12);125return;126}127128MachineBasicBlock &MBB = *MI->getParent();129DebugLoc DL = MI->getDebugLoc();130Register Reg = MI->getOperand(0).getReg();131MachineInstrBuilder MIB;132133MIB = BuildMI(MBB, MI, DL, get(ARM::MOV_ga_pcrel_ldr), Reg)134.addGlobalAddress(GV, 0, ARMII::MO_NONLAZY);135auto Flags = MachineMemOperand::MOLoad |136MachineMemOperand::MODereferenceable |137MachineMemOperand::MOInvariant;138MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand(139MachinePointerInfo::getGOT(*MBB.getParent()), Flags, 4, Align(4));140MIB.addMemOperand(MMO);141BuildMI(MBB, MI, DL, get(ARM::LDRi12), Reg)142.addReg(Reg, RegState::Kill)143.addImm(0)144.cloneMemRefs(*MI)145.add(predOps(ARMCC::AL));146}147148149