Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- ARMInstrInfo.cpp - ARM Instruction Information --------------------===// |
Rafael Espindola | ffdc24b | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
Rafael Espindola | ffdc24b | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the ARM implementation of the TargetInstrInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ARMInstrInfo.h" |
| 15 | #include "ARM.h" |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 16 | #include "ARMConstantPoolValue.h" |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 17 | #include "ARMMachineFunctionInfo.h" |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 18 | #include "ARMTargetMachine.h" |
Evan Cheng | a20cde3 | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/ARMAddressingModes.h" |
Owen Anderson | e2f23a3 | 2007-09-07 04:06:50 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/LiveVariables.h" |
Owen Anderson | 6bb0c52 | 2008-01-04 23:57:37 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Evan Cheng | 760c68b | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Jim Grosbach | 08aa534 | 2013-08-26 20:07:25 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/Function.h" |
| 27 | #include "llvm/IR/GlobalVariable.h" |
Chris Lattner | 7b26fce | 2009-08-22 20:48:53 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCAsmInfo.h" |
Jim Grosbach | 617f84dd | 2012-02-28 23:53:30 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCInst.h" |
Rafael Espindola | ffdc24b | 2006-05-14 22:18:28 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Anton Korobeynikov | 99152f3 | 2009-06-26 21:28:53 +0000 | [diff] [blame] | 32 | ARMInstrInfo::ARMInstrInfo(const ARMSubtarget &STI) |
Eric Christopher | 3408583 | 2015-03-12 05:12:31 +0000 | [diff] [blame^] | 33 | : ARMBaseInstrInfo(STI), RI() {} |
Rafael Espindola | 8c41f99 | 2006-08-08 20:35:03 +0000 | [diff] [blame] | 34 | |
Jim Grosbach | 617f84dd | 2012-02-28 23:53:30 +0000 | [diff] [blame] | 35 | /// getNoopForMachoTarget - Return the noop instruction to use for a noop. |
| 36 | void ARMInstrInfo::getNoopForMachoTarget(MCInst &NopInst) const { |
| 37 | if (hasNOP()) { |
Jim Grosbach | cb540f5 | 2012-06-18 19:45:50 +0000 | [diff] [blame] | 38 | NopInst.setOpcode(ARM::HINT); |
| 39 | NopInst.addOperand(MCOperand::CreateImm(0)); |
Jim Grosbach | 617f84dd | 2012-02-28 23:53:30 +0000 | [diff] [blame] | 40 | NopInst.addOperand(MCOperand::CreateImm(ARMCC::AL)); |
| 41 | NopInst.addOperand(MCOperand::CreateReg(0)); |
| 42 | } else { |
| 43 | NopInst.setOpcode(ARM::MOVr); |
| 44 | NopInst.addOperand(MCOperand::CreateReg(ARM::R0)); |
| 45 | NopInst.addOperand(MCOperand::CreateReg(ARM::R0)); |
| 46 | NopInst.addOperand(MCOperand::CreateImm(ARMCC::AL)); |
| 47 | NopInst.addOperand(MCOperand::CreateReg(0)); |
| 48 | NopInst.addOperand(MCOperand::CreateReg(0)); |
| 49 | } |
| 50 | } |
| 51 | |
Chris Lattner | e98a3c3 | 2009-08-02 05:20:37 +0000 | [diff] [blame] | 52 | unsigned ARMInstrInfo::getUnindexedOpcode(unsigned Opc) const { |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 53 | switch (Opc) { |
| 54 | default: break; |
Owen Anderson | 16d33f3 | 2011-08-26 20:43:14 +0000 | [diff] [blame] | 55 | case ARM::LDR_PRE_IMM: |
| 56 | case ARM::LDR_PRE_REG: |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 57 | case ARM::LDR_POST_IMM: |
| 58 | case ARM::LDR_POST_REG: |
Jim Grosbach | 1e4d9a1 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 59 | return ARM::LDRi12; |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 60 | case ARM::LDRH_PRE: |
| 61 | case ARM::LDRH_POST: |
| 62 | return ARM::LDRH; |
Owen Anderson | 16d33f3 | 2011-08-26 20:43:14 +0000 | [diff] [blame] | 63 | case ARM::LDRB_PRE_IMM: |
| 64 | case ARM::LDRB_PRE_REG: |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 65 | case ARM::LDRB_POST_IMM: |
| 66 | case ARM::LDRB_POST_REG: |
Jim Grosbach | 5a7c715 | 2010-10-27 00:19:44 +0000 | [diff] [blame] | 67 | return ARM::LDRBi12; |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 68 | case ARM::LDRSH_PRE: |
| 69 | case ARM::LDRSH_POST: |
| 70 | return ARM::LDRSH; |
| 71 | case ARM::LDRSB_PRE: |
| 72 | case ARM::LDRSB_POST: |
| 73 | return ARM::LDRSB; |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 74 | case ARM::STR_PRE_IMM: |
| 75 | case ARM::STR_PRE_REG: |
| 76 | case ARM::STR_POST_IMM: |
| 77 | case ARM::STR_POST_REG: |
Jim Grosbach | 338de3e | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 78 | return ARM::STRi12; |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 79 | case ARM::STRH_PRE: |
| 80 | case ARM::STRH_POST: |
| 81 | return ARM::STRH; |
Owen Anderson | 2aedba6 | 2011-07-26 20:54:26 +0000 | [diff] [blame] | 82 | case ARM::STRB_PRE_IMM: |
| 83 | case ARM::STRB_PRE_REG: |
| 84 | case ARM::STRB_POST_IMM: |
| 85 | case ARM::STRB_POST_REG: |
Jim Grosbach | 338de3e | 2010-10-27 23:12:14 +0000 | [diff] [blame] | 86 | return ARM::STRBi12; |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 87 | } |
David Goodwin | af7451b | 2009-07-08 16:09:28 +0000 | [diff] [blame] | 88 | |
Evan Cheng | 10043e2 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 89 | return 0; |
| 90 | } |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 91 | |
Akira Hatanaka | e5b6e0d | 2014-07-25 19:31:34 +0000 | [diff] [blame] | 92 | void ARMInstrInfo::expandLoadStackGuard(MachineBasicBlock::iterator MI, |
| 93 | Reloc::Model RM) const { |
Akira Hatanaka | 2ee0e9e | 2014-10-23 04:17:05 +0000 | [diff] [blame] | 94 | MachineFunction &MF = *MI->getParent()->getParent(); |
Eric Christopher | 22b2ad2 | 2015-02-20 08:24:37 +0000 | [diff] [blame] | 95 | const ARMSubtarget &Subtarget = MF.getSubtarget<ARMSubtarget>(); |
Akira Hatanaka | 2ee0e9e | 2014-10-23 04:17:05 +0000 | [diff] [blame] | 96 | |
| 97 | if (!Subtarget.useMovt(MF)) { |
| 98 | if (RM == Reloc::PIC_) |
| 99 | expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_pcrel, ARM::LDRi12, RM); |
| 100 | else |
| 101 | expandLoadStackGuardBase(MI, ARM::LDRLIT_ga_abs, ARM::LDRi12, RM); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (RM != Reloc::PIC_) { |
| 106 | expandLoadStackGuardBase(MI, ARM::MOVi32imm, ARM::LDRi12, RM); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | const GlobalValue *GV = |
| 111 | cast<GlobalValue>((*MI->memoperands_begin())->getValue()); |
| 112 | |
| 113 | if (!Subtarget.GVIsIndirectSymbol(GV, RM)) { |
| 114 | expandLoadStackGuardBase(MI, ARM::MOV_ga_pcrel, ARM::LDRi12, RM); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | MachineBasicBlock &MBB = *MI->getParent(); |
| 119 | DebugLoc DL = MI->getDebugLoc(); |
| 120 | unsigned Reg = MI->getOperand(0).getReg(); |
| 121 | MachineInstrBuilder MIB; |
| 122 | |
| 123 | MIB = BuildMI(MBB, MI, DL, get(ARM::MOV_ga_pcrel_ldr), Reg) |
| 124 | .addGlobalAddress(GV, 0, ARMII::MO_NONLAZY); |
| 125 | unsigned Flag = MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant; |
| 126 | MachineMemOperand *MMO = MBB.getParent()->getMachineMemOperand( |
| 127 | MachinePointerInfo::getGOT(), Flag, 4, 4); |
| 128 | MIB.addMemOperand(MMO); |
| 129 | MIB = BuildMI(MBB, MI, DL, get(ARM::LDRi12), Reg); |
| 130 | MIB.addReg(Reg, RegState::Kill).addImm(0); |
| 131 | MIB.setMemRefs(MI->memoperands_begin(), MI->memoperands_end()); |
| 132 | AddDefaultPred(MIB); |
Akira Hatanaka | e5b6e0d | 2014-07-25 19:31:34 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 135 | namespace { |
| 136 | /// ARMCGBR - Create Global Base Reg pass. This initializes the PIC |
| 137 | /// global base register for ARM ELF. |
| 138 | struct ARMCGBR : public MachineFunctionPass { |
| 139 | static char ID; |
| 140 | ARMCGBR() : MachineFunctionPass(ID) {} |
| 141 | |
Craig Topper | 6bc27bf | 2014-03-10 02:09:33 +0000 | [diff] [blame] | 142 | bool runOnMachineFunction(MachineFunction &MF) override { |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 143 | ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 144 | if (AFI->getGlobalBaseReg() == 0) |
| 145 | return false; |
Eric Christopher | 2a0bc68 | 2015-01-30 01:30:01 +0000 | [diff] [blame] | 146 | const ARMSubtarget &STI = |
| 147 | static_cast<const ARMSubtarget &>(MF.getSubtarget()); |
Eric Christopher | 63b4488 | 2015-03-05 00:23:40 +0000 | [diff] [blame] | 148 | // Don't do this for Thumb1. |
| 149 | if (STI.isThumb1Only()) |
| 150 | return false; |
| 151 | |
Eric Christopher | 2a0bc68 | 2015-01-30 01:30:01 +0000 | [diff] [blame] | 152 | const TargetMachine &TM = MF.getTarget(); |
| 153 | if (TM.getRelocationModel() != Reloc::PIC_) |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 154 | return false; |
| 155 | |
Chandler Carruth | 26ad41e | 2013-07-27 11:58:26 +0000 | [diff] [blame] | 156 | LLVMContext *Context = &MF.getFunction()->getContext(); |
| 157 | unsigned ARMPCLabelIndex = AFI->createPICLabelUId(); |
Eric Christopher | 2a0bc68 | 2015-01-30 01:30:01 +0000 | [diff] [blame] | 158 | unsigned PCAdj = STI.isThumb() ? 4 : 8; |
Chandler Carruth | 26ad41e | 2013-07-27 11:58:26 +0000 | [diff] [blame] | 159 | ARMConstantPoolValue *CPV = ARMConstantPoolSymbol::Create( |
| 160 | *Context, "_GLOBAL_OFFSET_TABLE_", ARMPCLabelIndex, PCAdj); |
| 161 | |
Eric Christopher | 2a0bc68 | 2015-01-30 01:30:01 +0000 | [diff] [blame] | 162 | unsigned Align = TM.getDataLayout()->getPrefTypeAlignment( |
Eric Christopher | 8b77065 | 2015-01-26 19:03:15 +0000 | [diff] [blame] | 163 | Type::getInt32PtrTy(*Context)); |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 164 | unsigned Idx = MF.getConstantPool()->getConstantPoolIndex(CPV, Align); |
| 165 | |
| 166 | MachineBasicBlock &FirstMBB = MF.front(); |
| 167 | MachineBasicBlock::iterator MBBI = FirstMBB.begin(); |
| 168 | DebugLoc DL = FirstMBB.findDebugLoc(MBBI); |
Jim Grosbach | 08aa534 | 2013-08-26 20:07:25 +0000 | [diff] [blame] | 169 | unsigned TempReg = |
| 170 | MF.getRegInfo().createVirtualRegister(&ARM::rGPRRegClass); |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 171 | unsigned Opc = STI.isThumb2() ? ARM::t2LDRpci : ARM::LDRcp; |
| 172 | const TargetInstrInfo &TII = *STI.getInstrInfo(); |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 173 | MachineInstrBuilder MIB = BuildMI(FirstMBB, MBBI, DL, |
Jim Grosbach | 08aa534 | 2013-08-26 20:07:25 +0000 | [diff] [blame] | 174 | TII.get(Opc), TempReg) |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 175 | .addConstantPoolIndex(Idx); |
| 176 | if (Opc == ARM::LDRcp) |
| 177 | MIB.addImm(0); |
| 178 | AddDefaultPred(MIB); |
| 179 | |
Benjamin Kramer | 3092066 | 2013-08-16 12:52:08 +0000 | [diff] [blame] | 180 | // Fix the GOT address by adding pc. |
Jim Grosbach | 08aa534 | 2013-08-26 20:07:25 +0000 | [diff] [blame] | 181 | unsigned GlobalBaseReg = AFI->getGlobalBaseReg(); |
Eric Christopher | 1b21f00 | 2015-01-29 00:19:33 +0000 | [diff] [blame] | 182 | Opc = STI.isThumb2() ? ARM::tPICADD : ARM::PICADD; |
Jim Grosbach | 19ae779 | 2013-09-10 17:21:39 +0000 | [diff] [blame] | 183 | MIB = BuildMI(FirstMBB, MBBI, DL, TII.get(Opc), GlobalBaseReg) |
| 184 | .addReg(TempReg) |
| 185 | .addImm(ARMPCLabelIndex); |
| 186 | if (Opc == ARM::PICADD) |
| 187 | AddDefaultPred(MIB); |
| 188 | |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 189 | return true; |
| 190 | } |
| 191 | |
Craig Topper | 6bc27bf | 2014-03-10 02:09:33 +0000 | [diff] [blame] | 192 | const char *getPassName() const override { |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 193 | return "ARM PIC Global Base Reg Initialization"; |
| 194 | } |
| 195 | |
Craig Topper | 6bc27bf | 2014-03-10 02:09:33 +0000 | [diff] [blame] | 196 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Jush Lu | 47172a0 | 2012-09-27 05:21:41 +0000 | [diff] [blame] | 197 | AU.setPreservesCFG(); |
| 198 | MachineFunctionPass::getAnalysisUsage(AU); |
| 199 | } |
| 200 | }; |
| 201 | } |
| 202 | |
| 203 | char ARMCGBR::ID = 0; |
| 204 | FunctionPass* |
| 205 | llvm::createARMGlobalBaseRegPass() { return new ARMCGBR(); } |