Thumb2InstrInfo.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. //===- Thumb2InstrInfo.cpp - Thumb-2 Instruction Information --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the Thumb-2 implementation of the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "Thumb2InstrInfo.h"
  13. #include "ARMMachineFunctionInfo.h"
  14. #include "ARMSubtarget.h"
  15. #include "MCTargetDesc/ARMAddressingModes.h"
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. #include "llvm/CodeGen/MachineFrameInfo.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineInstr.h"
  20. #include "llvm/CodeGen/MachineInstrBuilder.h"
  21. #include "llvm/CodeGen/MachineMemOperand.h"
  22. #include "llvm/CodeGen/MachineOperand.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/TargetRegisterInfo.h"
  25. #include "llvm/IR/DebugLoc.h"
  26. #include "llvm/MC/MCInst.h"
  27. #include "llvm/MC/MCInstBuilder.h"
  28. #include "llvm/MC/MCInstrDesc.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/MathExtras.h"
  32. #include "llvm/Target/TargetMachine.h"
  33. #include <cassert>
  34. using namespace llvm;
  35. static cl::opt<bool>
  36. OldT2IfCvt("old-thumb2-ifcvt", cl::Hidden,
  37. cl::desc("Use old-style Thumb2 if-conversion heuristics"),
  38. cl::init(false));
  39. static cl::opt<bool>
  40. PreferNoCSEL("prefer-no-csel", cl::Hidden,
  41. cl::desc("Prefer predicated Move to CSEL"),
  42. cl::init(false));
  43. Thumb2InstrInfo::Thumb2InstrInfo(const ARMSubtarget &STI)
  44. : ARMBaseInstrInfo(STI) {}
  45. /// Return the noop instruction to use for a noop.
  46. MCInst Thumb2InstrInfo::getNop() const {
  47. return MCInstBuilder(ARM::tHINT).addImm(0).addImm(ARMCC::AL).addReg(0);
  48. }
  49. unsigned Thumb2InstrInfo::getUnindexedOpcode(unsigned Opc) const {
  50. // FIXME
  51. return 0;
  52. }
  53. void
  54. Thumb2InstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
  55. MachineBasicBlock *NewDest) const {
  56. MachineBasicBlock *MBB = Tail->getParent();
  57. ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
  58. if (!AFI->hasITBlocks() || Tail->isBranch()) {
  59. TargetInstrInfo::ReplaceTailWithBranchTo(Tail, NewDest);
  60. return;
  61. }
  62. // If the first instruction of Tail is predicated, we may have to update
  63. // the IT instruction.
  64. Register PredReg;
  65. ARMCC::CondCodes CC = getInstrPredicate(*Tail, PredReg);
  66. MachineBasicBlock::iterator MBBI = Tail;
  67. if (CC != ARMCC::AL)
  68. // Expecting at least the t2IT instruction before it.
  69. --MBBI;
  70. // Actually replace the tail.
  71. TargetInstrInfo::ReplaceTailWithBranchTo(Tail, NewDest);
  72. // Fix up IT.
  73. if (CC != ARMCC::AL) {
  74. MachineBasicBlock::iterator E = MBB->begin();
  75. unsigned Count = 4; // At most 4 instructions in an IT block.
  76. while (Count && MBBI != E) {
  77. if (MBBI->isDebugInstr()) {
  78. --MBBI;
  79. continue;
  80. }
  81. if (MBBI->getOpcode() == ARM::t2IT) {
  82. unsigned Mask = MBBI->getOperand(1).getImm();
  83. if (Count == 4)
  84. MBBI->eraseFromParent();
  85. else {
  86. unsigned MaskOn = 1 << Count;
  87. unsigned MaskOff = ~(MaskOn - 1);
  88. MBBI->getOperand(1).setImm((Mask & MaskOff) | MaskOn);
  89. }
  90. return;
  91. }
  92. --MBBI;
  93. --Count;
  94. }
  95. // Ctrl flow can reach here if branch folding is run before IT block
  96. // formation pass.
  97. }
  98. }
  99. bool
  100. Thumb2InstrInfo::isLegalToSplitMBBAt(MachineBasicBlock &MBB,
  101. MachineBasicBlock::iterator MBBI) const {
  102. while (MBBI->isDebugInstr()) {
  103. ++MBBI;
  104. if (MBBI == MBB.end())
  105. return false;
  106. }
  107. Register PredReg;
  108. return getITInstrPredicate(*MBBI, PredReg) == ARMCC::AL;
  109. }
  110. MachineInstr *
  111. Thumb2InstrInfo::optimizeSelect(MachineInstr &MI,
  112. SmallPtrSetImpl<MachineInstr *> &SeenMIs,
  113. bool PreferFalse) const {
  114. // Try to use the base optimizeSelect, which uses canFoldIntoMOVCC to fold the
  115. // MOVCC into another instruction. If that fails on 8.1-M fall back to using a
  116. // CSEL.
  117. MachineInstr *RV = ARMBaseInstrInfo::optimizeSelect(MI, SeenMIs, PreferFalse);
  118. if (!RV && getSubtarget().hasV8_1MMainlineOps() && !PreferNoCSEL) {
  119. Register DestReg = MI.getOperand(0).getReg();
  120. if (!DestReg.isVirtual())
  121. return nullptr;
  122. MachineInstrBuilder NewMI = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
  123. get(ARM::t2CSEL), DestReg)
  124. .add(MI.getOperand(2))
  125. .add(MI.getOperand(1))
  126. .add(MI.getOperand(3));
  127. SeenMIs.insert(NewMI);
  128. return NewMI;
  129. }
  130. return RV;
  131. }
  132. void Thumb2InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
  133. MachineBasicBlock::iterator I,
  134. const DebugLoc &DL, MCRegister DestReg,
  135. MCRegister SrcReg, bool KillSrc) const {
  136. // Handle SPR, DPR, and QPR copies.
  137. if (!ARM::GPRRegClass.contains(DestReg, SrcReg))
  138. return ARMBaseInstrInfo::copyPhysReg(MBB, I, DL, DestReg, SrcReg, KillSrc);
  139. BuildMI(MBB, I, DL, get(ARM::tMOVr), DestReg)
  140. .addReg(SrcReg, getKillRegState(KillSrc))
  141. .add(predOps(ARMCC::AL));
  142. }
  143. void Thumb2InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
  144. MachineBasicBlock::iterator I,
  145. Register SrcReg, bool isKill, int FI,
  146. const TargetRegisterClass *RC,
  147. const TargetRegisterInfo *TRI,
  148. Register VReg) const {
  149. DebugLoc DL;
  150. if (I != MBB.end()) DL = I->getDebugLoc();
  151. MachineFunction &MF = *MBB.getParent();
  152. MachineFrameInfo &MFI = MF.getFrameInfo();
  153. MachineMemOperand *MMO = MF.getMachineMemOperand(
  154. MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOStore,
  155. MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
  156. if (ARM::GPRRegClass.hasSubClassEq(RC)) {
  157. BuildMI(MBB, I, DL, get(ARM::t2STRi12))
  158. .addReg(SrcReg, getKillRegState(isKill))
  159. .addFrameIndex(FI)
  160. .addImm(0)
  161. .addMemOperand(MMO)
  162. .add(predOps(ARMCC::AL));
  163. return;
  164. }
  165. if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
  166. // Thumb2 STRD expects its dest-registers to be in rGPR. Not a problem for
  167. // gsub_0, but needs an extra constraint for gsub_1 (which could be sp
  168. // otherwise).
  169. if (SrcReg.isVirtual()) {
  170. MachineRegisterInfo *MRI = &MF.getRegInfo();
  171. MRI->constrainRegClass(SrcReg, &ARM::GPRPairnospRegClass);
  172. }
  173. MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::t2STRDi8));
  174. AddDReg(MIB, SrcReg, ARM::gsub_0, getKillRegState(isKill), TRI);
  175. AddDReg(MIB, SrcReg, ARM::gsub_1, 0, TRI);
  176. MIB.addFrameIndex(FI).addImm(0).addMemOperand(MMO).add(predOps(ARMCC::AL));
  177. return;
  178. }
  179. ARMBaseInstrInfo::storeRegToStackSlot(MBB, I, SrcReg, isKill, FI, RC, TRI,
  180. Register());
  181. }
  182. void Thumb2InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
  183. MachineBasicBlock::iterator I,
  184. Register DestReg, int FI,
  185. const TargetRegisterClass *RC,
  186. const TargetRegisterInfo *TRI,
  187. Register VReg) const {
  188. MachineFunction &MF = *MBB.getParent();
  189. MachineFrameInfo &MFI = MF.getFrameInfo();
  190. MachineMemOperand *MMO = MF.getMachineMemOperand(
  191. MachinePointerInfo::getFixedStack(MF, FI), MachineMemOperand::MOLoad,
  192. MFI.getObjectSize(FI), MFI.getObjectAlign(FI));
  193. DebugLoc DL;
  194. if (I != MBB.end()) DL = I->getDebugLoc();
  195. if (ARM::GPRRegClass.hasSubClassEq(RC)) {
  196. BuildMI(MBB, I, DL, get(ARM::t2LDRi12), DestReg)
  197. .addFrameIndex(FI)
  198. .addImm(0)
  199. .addMemOperand(MMO)
  200. .add(predOps(ARMCC::AL));
  201. return;
  202. }
  203. if (ARM::GPRPairRegClass.hasSubClassEq(RC)) {
  204. // Thumb2 LDRD expects its dest-registers to be in rGPR. Not a problem for
  205. // gsub_0, but needs an extra constraint for gsub_1 (which could be sp
  206. // otherwise).
  207. if (DestReg.isVirtual()) {
  208. MachineRegisterInfo *MRI = &MF.getRegInfo();
  209. MRI->constrainRegClass(DestReg, &ARM::GPRPairnospRegClass);
  210. }
  211. MachineInstrBuilder MIB = BuildMI(MBB, I, DL, get(ARM::t2LDRDi8));
  212. AddDReg(MIB, DestReg, ARM::gsub_0, RegState::DefineNoRead, TRI);
  213. AddDReg(MIB, DestReg, ARM::gsub_1, RegState::DefineNoRead, TRI);
  214. MIB.addFrameIndex(FI).addImm(0).addMemOperand(MMO).add(predOps(ARMCC::AL));
  215. if (DestReg.isPhysical())
  216. MIB.addReg(DestReg, RegState::ImplicitDefine);
  217. return;
  218. }
  219. ARMBaseInstrInfo::loadRegFromStackSlot(MBB, I, DestReg, FI, RC, TRI,
  220. Register());
  221. }
  222. void Thumb2InstrInfo::expandLoadStackGuard(
  223. MachineBasicBlock::iterator MI) const {
  224. MachineFunction &MF = *MI->getParent()->getParent();
  225. Module &M = *MF.getFunction().getParent();
  226. if (M.getStackProtectorGuard() == "tls") {
  227. expandLoadStackGuardBase(MI, ARM::t2MRC, ARM::t2LDRi12);
  228. return;
  229. }
  230. const GlobalValue *GV =
  231. cast<GlobalValue>((*MI->memoperands_begin())->getValue());
  232. if (MF.getSubtarget<ARMSubtarget>().isGVInGOT(GV))
  233. expandLoadStackGuardBase(MI, ARM::t2LDRLIT_ga_pcrel, ARM::t2LDRi12);
  234. else if (MF.getTarget().isPositionIndependent())
  235. expandLoadStackGuardBase(MI, ARM::t2MOV_ga_pcrel, ARM::t2LDRi12);
  236. else
  237. expandLoadStackGuardBase(MI, ARM::t2MOVi32imm, ARM::t2LDRi12);
  238. }
  239. MachineInstr *Thumb2InstrInfo::commuteInstructionImpl(MachineInstr &MI,
  240. bool NewMI,
  241. unsigned OpIdx1,
  242. unsigned OpIdx2) const {
  243. switch (MI.getOpcode()) {
  244. case ARM::MVE_VMAXNMAf16:
  245. case ARM::MVE_VMAXNMAf32:
  246. case ARM::MVE_VMINNMAf16:
  247. case ARM::MVE_VMINNMAf32:
  248. // Don't allow predicated instructions to be commuted.
  249. if (getVPTInstrPredicate(MI) != ARMVCC::None)
  250. return nullptr;
  251. }
  252. return ARMBaseInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
  253. }
  254. void llvm::emitT2RegPlusImmediate(MachineBasicBlock &MBB,
  255. MachineBasicBlock::iterator &MBBI,
  256. const DebugLoc &dl, Register DestReg,
  257. Register BaseReg, int NumBytes,
  258. ARMCC::CondCodes Pred, Register PredReg,
  259. const ARMBaseInstrInfo &TII,
  260. unsigned MIFlags) {
  261. if (NumBytes == 0 && DestReg != BaseReg) {
  262. BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
  263. .addReg(BaseReg, RegState::Kill)
  264. .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
  265. return;
  266. }
  267. bool isSub = NumBytes < 0;
  268. if (isSub) NumBytes = -NumBytes;
  269. // If profitable, use a movw or movt to materialize the offset.
  270. // FIXME: Use the scavenger to grab a scratch register.
  271. if (DestReg != ARM::SP && DestReg != BaseReg &&
  272. NumBytes >= 4096 &&
  273. ARM_AM::getT2SOImmVal(NumBytes) == -1) {
  274. bool Fits = false;
  275. if (NumBytes < 65536) {
  276. // Use a movw to materialize the 16-bit constant.
  277. BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVi16), DestReg)
  278. .addImm(NumBytes)
  279. .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
  280. Fits = true;
  281. } else if ((NumBytes & 0xffff) == 0) {
  282. // Use a movt to materialize the 32-bit constant.
  283. BuildMI(MBB, MBBI, dl, TII.get(ARM::t2MOVTi16), DestReg)
  284. .addReg(DestReg)
  285. .addImm(NumBytes >> 16)
  286. .addImm((unsigned)Pred).addReg(PredReg).setMIFlags(MIFlags);
  287. Fits = true;
  288. }
  289. if (Fits) {
  290. if (isSub) {
  291. BuildMI(MBB, MBBI, dl, TII.get(ARM::t2SUBrr), DestReg)
  292. .addReg(BaseReg)
  293. .addReg(DestReg, RegState::Kill)
  294. .add(predOps(Pred, PredReg))
  295. .add(condCodeOp())
  296. .setMIFlags(MIFlags);
  297. } else {
  298. // Here we know that DestReg is not SP but we do not
  299. // know anything about BaseReg. t2ADDrr is an invalid
  300. // instruction is SP is used as the second argument, but
  301. // is fine if SP is the first argument. To be sure we
  302. // do not generate invalid encoding, put BaseReg first.
  303. BuildMI(MBB, MBBI, dl, TII.get(ARM::t2ADDrr), DestReg)
  304. .addReg(BaseReg)
  305. .addReg(DestReg, RegState::Kill)
  306. .add(predOps(Pred, PredReg))
  307. .add(condCodeOp())
  308. .setMIFlags(MIFlags);
  309. }
  310. return;
  311. }
  312. }
  313. while (NumBytes) {
  314. unsigned ThisVal = NumBytes;
  315. unsigned Opc = 0;
  316. if (DestReg == ARM::SP && BaseReg != ARM::SP) {
  317. // mov sp, rn. Note t2MOVr cannot be used.
  318. BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
  319. .addReg(BaseReg)
  320. .setMIFlags(MIFlags)
  321. .add(predOps(ARMCC::AL));
  322. BaseReg = ARM::SP;
  323. continue;
  324. }
  325. assert((DestReg != ARM::SP || BaseReg == ARM::SP) &&
  326. "Writing to SP, from other register.");
  327. // Try to use T1, as it smaller
  328. if ((DestReg == ARM::SP) && (ThisVal < ((1 << 7) - 1) * 4)) {
  329. assert((ThisVal & 3) == 0 && "Stack update is not multiple of 4?");
  330. Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
  331. BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
  332. .addReg(BaseReg)
  333. .addImm(ThisVal / 4)
  334. .setMIFlags(MIFlags)
  335. .add(predOps(ARMCC::AL));
  336. break;
  337. }
  338. bool HasCCOut = true;
  339. int ImmIsT2SO = ARM_AM::getT2SOImmVal(ThisVal);
  340. bool ToSP = DestReg == ARM::SP;
  341. unsigned t2SUB = ToSP ? ARM::t2SUBspImm : ARM::t2SUBri;
  342. unsigned t2ADD = ToSP ? ARM::t2ADDspImm : ARM::t2ADDri;
  343. unsigned t2SUBi12 = ToSP ? ARM::t2SUBspImm12 : ARM::t2SUBri12;
  344. unsigned t2ADDi12 = ToSP ? ARM::t2ADDspImm12 : ARM::t2ADDri12;
  345. Opc = isSub ? t2SUB : t2ADD;
  346. // Prefer T2: sub rd, rn, so_imm | sub sp, sp, so_imm
  347. if (ImmIsT2SO != -1) {
  348. NumBytes = 0;
  349. } else if (ThisVal < 4096) {
  350. // Prefer T3 if can make it in a single go: subw rd, rn, imm12 | subw sp,
  351. // sp, imm12
  352. Opc = isSub ? t2SUBi12 : t2ADDi12;
  353. HasCCOut = false;
  354. NumBytes = 0;
  355. } else {
  356. // Use one T2 instruction to reduce NumBytes
  357. // FIXME: Move this to ARMAddressingModes.h?
  358. unsigned RotAmt = countLeadingZeros(ThisVal);
  359. ThisVal = ThisVal & ARM_AM::rotr32(0xff000000U, RotAmt);
  360. NumBytes &= ~ThisVal;
  361. assert(ARM_AM::getT2SOImmVal(ThisVal) != -1 &&
  362. "Bit extraction didn't work?");
  363. }
  364. // Build the new ADD / SUB.
  365. MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
  366. .addReg(BaseReg, RegState::Kill)
  367. .addImm(ThisVal)
  368. .add(predOps(ARMCC::AL))
  369. .setMIFlags(MIFlags);
  370. if (HasCCOut)
  371. MIB.add(condCodeOp());
  372. BaseReg = DestReg;
  373. }
  374. }
  375. static unsigned
  376. negativeOffsetOpcode(unsigned opcode)
  377. {
  378. switch (opcode) {
  379. case ARM::t2LDRi12: return ARM::t2LDRi8;
  380. case ARM::t2LDRHi12: return ARM::t2LDRHi8;
  381. case ARM::t2LDRBi12: return ARM::t2LDRBi8;
  382. case ARM::t2LDRSHi12: return ARM::t2LDRSHi8;
  383. case ARM::t2LDRSBi12: return ARM::t2LDRSBi8;
  384. case ARM::t2STRi12: return ARM::t2STRi8;
  385. case ARM::t2STRBi12: return ARM::t2STRBi8;
  386. case ARM::t2STRHi12: return ARM::t2STRHi8;
  387. case ARM::t2PLDi12: return ARM::t2PLDi8;
  388. case ARM::t2PLDWi12: return ARM::t2PLDWi8;
  389. case ARM::t2PLIi12: return ARM::t2PLIi8;
  390. case ARM::t2LDRi8:
  391. case ARM::t2LDRHi8:
  392. case ARM::t2LDRBi8:
  393. case ARM::t2LDRSHi8:
  394. case ARM::t2LDRSBi8:
  395. case ARM::t2STRi8:
  396. case ARM::t2STRBi8:
  397. case ARM::t2STRHi8:
  398. case ARM::t2PLDi8:
  399. case ARM::t2PLDWi8:
  400. case ARM::t2PLIi8:
  401. return opcode;
  402. default:
  403. llvm_unreachable("unknown thumb2 opcode.");
  404. }
  405. }
  406. static unsigned
  407. positiveOffsetOpcode(unsigned opcode)
  408. {
  409. switch (opcode) {
  410. case ARM::t2LDRi8: return ARM::t2LDRi12;
  411. case ARM::t2LDRHi8: return ARM::t2LDRHi12;
  412. case ARM::t2LDRBi8: return ARM::t2LDRBi12;
  413. case ARM::t2LDRSHi8: return ARM::t2LDRSHi12;
  414. case ARM::t2LDRSBi8: return ARM::t2LDRSBi12;
  415. case ARM::t2STRi8: return ARM::t2STRi12;
  416. case ARM::t2STRBi8: return ARM::t2STRBi12;
  417. case ARM::t2STRHi8: return ARM::t2STRHi12;
  418. case ARM::t2PLDi8: return ARM::t2PLDi12;
  419. case ARM::t2PLDWi8: return ARM::t2PLDWi12;
  420. case ARM::t2PLIi8: return ARM::t2PLIi12;
  421. case ARM::t2LDRi12:
  422. case ARM::t2LDRHi12:
  423. case ARM::t2LDRBi12:
  424. case ARM::t2LDRSHi12:
  425. case ARM::t2LDRSBi12:
  426. case ARM::t2STRi12:
  427. case ARM::t2STRBi12:
  428. case ARM::t2STRHi12:
  429. case ARM::t2PLDi12:
  430. case ARM::t2PLDWi12:
  431. case ARM::t2PLIi12:
  432. return opcode;
  433. default:
  434. llvm_unreachable("unknown thumb2 opcode.");
  435. }
  436. }
  437. static unsigned
  438. immediateOffsetOpcode(unsigned opcode)
  439. {
  440. switch (opcode) {
  441. case ARM::t2LDRs: return ARM::t2LDRi12;
  442. case ARM::t2LDRHs: return ARM::t2LDRHi12;
  443. case ARM::t2LDRBs: return ARM::t2LDRBi12;
  444. case ARM::t2LDRSHs: return ARM::t2LDRSHi12;
  445. case ARM::t2LDRSBs: return ARM::t2LDRSBi12;
  446. case ARM::t2STRs: return ARM::t2STRi12;
  447. case ARM::t2STRBs: return ARM::t2STRBi12;
  448. case ARM::t2STRHs: return ARM::t2STRHi12;
  449. case ARM::t2PLDs: return ARM::t2PLDi12;
  450. case ARM::t2PLDWs: return ARM::t2PLDWi12;
  451. case ARM::t2PLIs: return ARM::t2PLIi12;
  452. case ARM::t2LDRi12:
  453. case ARM::t2LDRHi12:
  454. case ARM::t2LDRBi12:
  455. case ARM::t2LDRSHi12:
  456. case ARM::t2LDRSBi12:
  457. case ARM::t2STRi12:
  458. case ARM::t2STRBi12:
  459. case ARM::t2STRHi12:
  460. case ARM::t2PLDi12:
  461. case ARM::t2PLDWi12:
  462. case ARM::t2PLIi12:
  463. case ARM::t2LDRi8:
  464. case ARM::t2LDRHi8:
  465. case ARM::t2LDRBi8:
  466. case ARM::t2LDRSHi8:
  467. case ARM::t2LDRSBi8:
  468. case ARM::t2STRi8:
  469. case ARM::t2STRBi8:
  470. case ARM::t2STRHi8:
  471. case ARM::t2PLDi8:
  472. case ARM::t2PLDWi8:
  473. case ARM::t2PLIi8:
  474. return opcode;
  475. default:
  476. llvm_unreachable("unknown thumb2 opcode.");
  477. }
  478. }
  479. bool llvm::rewriteT2FrameIndex(MachineInstr &MI, unsigned FrameRegIdx,
  480. Register FrameReg, int &Offset,
  481. const ARMBaseInstrInfo &TII,
  482. const TargetRegisterInfo *TRI) {
  483. unsigned Opcode = MI.getOpcode();
  484. const MCInstrDesc &Desc = MI.getDesc();
  485. unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
  486. bool isSub = false;
  487. MachineFunction &MF = *MI.getParent()->getParent();
  488. const TargetRegisterClass *RegClass =
  489. TII.getRegClass(Desc, FrameRegIdx, TRI, MF);
  490. // Memory operands in inline assembly always use AddrModeT2_i12.
  491. if (Opcode == ARM::INLINEASM || Opcode == ARM::INLINEASM_BR)
  492. AddrMode = ARMII::AddrModeT2_i12; // FIXME. mode for thumb2?
  493. const bool IsSP = Opcode == ARM::t2ADDspImm12 || Opcode == ARM::t2ADDspImm;
  494. if (IsSP || Opcode == ARM::t2ADDri || Opcode == ARM::t2ADDri12) {
  495. Offset += MI.getOperand(FrameRegIdx+1).getImm();
  496. Register PredReg;
  497. if (Offset == 0 && getInstrPredicate(MI, PredReg) == ARMCC::AL &&
  498. !MI.definesRegister(ARM::CPSR)) {
  499. // Turn it into a move.
  500. MI.setDesc(TII.get(ARM::tMOVr));
  501. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  502. // Remove offset and remaining explicit predicate operands.
  503. do MI.removeOperand(FrameRegIdx+1);
  504. while (MI.getNumOperands() > FrameRegIdx+1);
  505. MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI);
  506. MIB.add(predOps(ARMCC::AL));
  507. return true;
  508. }
  509. bool HasCCOut = (Opcode != ARM::t2ADDspImm12 && Opcode != ARM::t2ADDri12);
  510. if (Offset < 0) {
  511. Offset = -Offset;
  512. isSub = true;
  513. MI.setDesc(IsSP ? TII.get(ARM::t2SUBspImm) : TII.get(ARM::t2SUBri));
  514. } else {
  515. MI.setDesc(IsSP ? TII.get(ARM::t2ADDspImm) : TII.get(ARM::t2ADDri));
  516. }
  517. // Common case: small offset, fits into instruction.
  518. if (ARM_AM::getT2SOImmVal(Offset) != -1) {
  519. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  520. MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
  521. // Add cc_out operand if the original instruction did not have one.
  522. if (!HasCCOut)
  523. MI.addOperand(MachineOperand::CreateReg(0, false));
  524. Offset = 0;
  525. return true;
  526. }
  527. // Another common case: imm12.
  528. if (Offset < 4096 &&
  529. (!HasCCOut || MI.getOperand(MI.getNumOperands()-1).getReg() == 0)) {
  530. unsigned NewOpc = isSub ? IsSP ? ARM::t2SUBspImm12 : ARM::t2SUBri12
  531. : IsSP ? ARM::t2ADDspImm12 : ARM::t2ADDri12;
  532. MI.setDesc(TII.get(NewOpc));
  533. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  534. MI.getOperand(FrameRegIdx+1).ChangeToImmediate(Offset);
  535. // Remove the cc_out operand.
  536. if (HasCCOut)
  537. MI.removeOperand(MI.getNumOperands()-1);
  538. Offset = 0;
  539. return true;
  540. }
  541. // Otherwise, extract 8 adjacent bits from the immediate into this
  542. // t2ADDri/t2SUBri.
  543. unsigned RotAmt = countLeadingZeros<unsigned>(Offset);
  544. unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xff000000U, RotAmt);
  545. // We will handle these bits from offset, clear them.
  546. Offset &= ~ThisImmVal;
  547. assert(ARM_AM::getT2SOImmVal(ThisImmVal) != -1 &&
  548. "Bit extraction didn't work?");
  549. MI.getOperand(FrameRegIdx+1).ChangeToImmediate(ThisImmVal);
  550. // Add cc_out operand if the original instruction did not have one.
  551. if (!HasCCOut)
  552. MI.addOperand(MachineOperand::CreateReg(0, false));
  553. } else {
  554. // AddrMode4 and AddrMode6 cannot handle any offset.
  555. if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
  556. return false;
  557. // AddrModeT2_so cannot handle any offset. If there is no offset
  558. // register then we change to an immediate version.
  559. unsigned NewOpc = Opcode;
  560. if (AddrMode == ARMII::AddrModeT2_so) {
  561. Register OffsetReg = MI.getOperand(FrameRegIdx + 1).getReg();
  562. if (OffsetReg != 0) {
  563. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  564. return Offset == 0;
  565. }
  566. MI.removeOperand(FrameRegIdx+1);
  567. MI.getOperand(FrameRegIdx+1).ChangeToImmediate(0);
  568. NewOpc = immediateOffsetOpcode(Opcode);
  569. AddrMode = ARMII::AddrModeT2_i12;
  570. }
  571. unsigned NumBits = 0;
  572. unsigned Scale = 1;
  573. if (AddrMode == ARMII::AddrModeT2_i8neg ||
  574. AddrMode == ARMII::AddrModeT2_i12) {
  575. // i8 supports only negative, and i12 supports only positive, so
  576. // based on Offset sign convert Opcode to the appropriate
  577. // instruction
  578. Offset += MI.getOperand(FrameRegIdx+1).getImm();
  579. if (Offset < 0) {
  580. NewOpc = negativeOffsetOpcode(Opcode);
  581. NumBits = 8;
  582. isSub = true;
  583. Offset = -Offset;
  584. } else {
  585. NewOpc = positiveOffsetOpcode(Opcode);
  586. NumBits = 12;
  587. }
  588. } else if (AddrMode == ARMII::AddrMode5) {
  589. // VFP address mode.
  590. const MachineOperand &OffOp = MI.getOperand(FrameRegIdx+1);
  591. int InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
  592. if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
  593. InstrOffs *= -1;
  594. NumBits = 8;
  595. Scale = 4;
  596. Offset += InstrOffs * 4;
  597. assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
  598. if (Offset < 0) {
  599. Offset = -Offset;
  600. isSub = true;
  601. }
  602. } else if (AddrMode == ARMII::AddrMode5FP16) {
  603. // VFP address mode.
  604. const MachineOperand &OffOp = MI.getOperand(FrameRegIdx+1);
  605. int InstrOffs = ARM_AM::getAM5FP16Offset(OffOp.getImm());
  606. if (ARM_AM::getAM5FP16Op(OffOp.getImm()) == ARM_AM::sub)
  607. InstrOffs *= -1;
  608. NumBits = 8;
  609. Scale = 2;
  610. Offset += InstrOffs * 2;
  611. assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
  612. if (Offset < 0) {
  613. Offset = -Offset;
  614. isSub = true;
  615. }
  616. } else if (AddrMode == ARMII::AddrModeT2_i7s4 ||
  617. AddrMode == ARMII::AddrModeT2_i7s2 ||
  618. AddrMode == ARMII::AddrModeT2_i7) {
  619. Offset += MI.getOperand(FrameRegIdx + 1).getImm();
  620. unsigned OffsetMask;
  621. switch (AddrMode) {
  622. case ARMII::AddrModeT2_i7s4: NumBits = 9; OffsetMask = 0x3; break;
  623. case ARMII::AddrModeT2_i7s2: NumBits = 8; OffsetMask = 0x1; break;
  624. default: NumBits = 7; OffsetMask = 0x0; break;
  625. }
  626. // MCInst operand expects already scaled value.
  627. Scale = 1;
  628. assert((Offset & OffsetMask) == 0 && "Can't encode this offset!");
  629. (void)OffsetMask; // squash unused-variable warning at -NDEBUG
  630. } else if (AddrMode == ARMII::AddrModeT2_i8s4) {
  631. Offset += MI.getOperand(FrameRegIdx + 1).getImm();
  632. NumBits = 8 + 2;
  633. // MCInst operand expects already scaled value.
  634. Scale = 1;
  635. assert((Offset & 3) == 0 && "Can't encode this offset!");
  636. } else if (AddrMode == ARMII::AddrModeT2_ldrex) {
  637. Offset += MI.getOperand(FrameRegIdx + 1).getImm() * 4;
  638. NumBits = 8; // 8 bits scaled by 4
  639. Scale = 4;
  640. assert((Offset & 3) == 0 && "Can't encode this offset!");
  641. } else {
  642. llvm_unreachable("Unsupported addressing mode!");
  643. }
  644. if (NewOpc != Opcode)
  645. MI.setDesc(TII.get(NewOpc));
  646. MachineOperand &ImmOp = MI.getOperand(FrameRegIdx+1);
  647. // Attempt to fold address computation
  648. // Common case: small offset, fits into instruction. We need to make sure
  649. // the register class is correct too, for instructions like the MVE
  650. // VLDRH.32, which only accepts low tGPR registers.
  651. int ImmedOffset = Offset / Scale;
  652. unsigned Mask = (1 << NumBits) - 1;
  653. if ((unsigned)Offset <= Mask * Scale &&
  654. (FrameReg.isVirtual() || RegClass->contains(FrameReg))) {
  655. if (FrameReg.isVirtual()) {
  656. // Make sure the register class for the virtual register is correct
  657. MachineRegisterInfo *MRI = &MF.getRegInfo();
  658. if (!MRI->constrainRegClass(FrameReg, RegClass))
  659. llvm_unreachable("Unable to constrain virtual register class.");
  660. }
  661. // Replace the FrameIndex with fp/sp
  662. MI.getOperand(FrameRegIdx).ChangeToRegister(FrameReg, false);
  663. if (isSub) {
  664. if (AddrMode == ARMII::AddrMode5 || AddrMode == ARMII::AddrMode5FP16)
  665. // FIXME: Not consistent.
  666. ImmedOffset |= 1 << NumBits;
  667. else
  668. ImmedOffset = -ImmedOffset;
  669. }
  670. ImmOp.ChangeToImmediate(ImmedOffset);
  671. Offset = 0;
  672. return true;
  673. }
  674. // Otherwise, offset doesn't fit. Pull in what we can to simplify
  675. ImmedOffset = ImmedOffset & Mask;
  676. if (isSub) {
  677. if (AddrMode == ARMII::AddrMode5 || AddrMode == ARMII::AddrMode5FP16)
  678. // FIXME: Not consistent.
  679. ImmedOffset |= 1 << NumBits;
  680. else {
  681. ImmedOffset = -ImmedOffset;
  682. if (ImmedOffset == 0)
  683. // Change the opcode back if the encoded offset is zero.
  684. MI.setDesc(TII.get(positiveOffsetOpcode(NewOpc)));
  685. }
  686. }
  687. ImmOp.ChangeToImmediate(ImmedOffset);
  688. Offset &= ~(Mask*Scale);
  689. }
  690. Offset = (isSub) ? -Offset : Offset;
  691. return Offset == 0 && (FrameReg.isVirtual() || RegClass->contains(FrameReg));
  692. }
  693. ARMCC::CondCodes llvm::getITInstrPredicate(const MachineInstr &MI,
  694. Register &PredReg) {
  695. unsigned Opc = MI.getOpcode();
  696. if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
  697. return ARMCC::AL;
  698. return getInstrPredicate(MI, PredReg);
  699. }
  700. int llvm::findFirstVPTPredOperandIdx(const MachineInstr &MI) {
  701. const MCInstrDesc &MCID = MI.getDesc();
  702. for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i)
  703. if (ARM::isVpred(MCID.operands()[i].OperandType))
  704. return i;
  705. return -1;
  706. }
  707. ARMVCC::VPTCodes llvm::getVPTInstrPredicate(const MachineInstr &MI,
  708. Register &PredReg) {
  709. int PIdx = findFirstVPTPredOperandIdx(MI);
  710. if (PIdx == -1) {
  711. PredReg = 0;
  712. return ARMVCC::None;
  713. }
  714. PredReg = MI.getOperand(PIdx+1).getReg();
  715. return (ARMVCC::VPTCodes)MI.getOperand(PIdx).getImm();
  716. }
  717. void llvm::recomputeVPTBlockMask(MachineInstr &Instr) {
  718. assert(isVPTOpcode(Instr.getOpcode()) && "Not a VPST or VPT Instruction!");
  719. MachineOperand &MaskOp = Instr.getOperand(0);
  720. assert(MaskOp.isImm() && "Operand 0 is not the block mask of the VPT/VPST?!");
  721. MachineBasicBlock::iterator Iter = ++Instr.getIterator(),
  722. End = Instr.getParent()->end();
  723. while (Iter != End && Iter->isDebugInstr())
  724. ++Iter;
  725. // Verify that the instruction after the VPT/VPST is predicated (it should
  726. // be), and skip it.
  727. assert(Iter != End && "Expected some instructions in any VPT block");
  728. assert(
  729. getVPTInstrPredicate(*Iter) == ARMVCC::Then &&
  730. "VPT/VPST should be followed by an instruction with a 'then' predicate!");
  731. ++Iter;
  732. // Iterate over the predicated instructions, updating the BlockMask as we go.
  733. ARM::PredBlockMask BlockMask = ARM::PredBlockMask::T;
  734. while (Iter != End) {
  735. if (Iter->isDebugInstr()) {
  736. ++Iter;
  737. continue;
  738. }
  739. ARMVCC::VPTCodes Pred = getVPTInstrPredicate(*Iter);
  740. if (Pred == ARMVCC::None)
  741. break;
  742. BlockMask = expandPredBlockMask(BlockMask, Pred);
  743. ++Iter;
  744. }
  745. // Rewrite the BlockMask.
  746. MaskOp.setImm((int64_t)(BlockMask));
  747. }