Thumb2SizeReduction.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. //===-- Thumb2SizeReduction.cpp - Thumb2 code size reduction pass -*- 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. #define DEBUG_TYPE "t2-reduce-size"
  10. #include "ARM.h"
  11. #include "ARMBaseInstrInfo.h"
  12. #include "ARMBaseRegisterInfo.h"
  13. #include "ARMSubtarget.h"
  14. #include "MCTargetDesc/ARMAddressingModes.h"
  15. #include "Thumb2InstrInfo.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/PostOrderIterator.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/CodeGen/MachineFunctionPass.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/IR/Function.h" // To access Function attributes
  23. #include "llvm/Support/CommandLine.h"
  24. #include "llvm/Support/Debug.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. using namespace llvm;
  27. STATISTIC(NumNarrows, "Number of 32-bit instrs reduced to 16-bit ones");
  28. STATISTIC(Num2Addrs, "Number of 32-bit instrs reduced to 2addr 16-bit ones");
  29. STATISTIC(NumLdSts, "Number of 32-bit load / store reduced to 16-bit ones");
  30. static cl::opt<int> ReduceLimit("t2-reduce-limit",
  31. cl::init(-1), cl::Hidden);
  32. static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
  33. cl::init(-1), cl::Hidden);
  34. static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
  35. cl::init(-1), cl::Hidden);
  36. namespace {
  37. /// ReduceTable - A static table with information on mapping from wide
  38. /// opcodes to narrow
  39. struct ReduceEntry {
  40. uint16_t WideOpc; // Wide opcode
  41. uint16_t NarrowOpc1; // Narrow opcode to transform to
  42. uint16_t NarrowOpc2; // Narrow opcode when it's two-address
  43. uint8_t Imm1Limit; // Limit of immediate field (bits)
  44. uint8_t Imm2Limit; // Limit of immediate field when it's two-address
  45. unsigned LowRegs1 : 1; // Only possible if low-registers are used
  46. unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
  47. unsigned PredCC1 : 2; // 0 - If predicated, cc is on and vice versa.
  48. // 1 - No cc field.
  49. // 2 - Always set CPSR.
  50. unsigned PredCC2 : 2;
  51. unsigned PartFlag : 1; // 16-bit instruction does partial flag update
  52. unsigned Special : 1; // Needs to be dealt with specially
  53. unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
  54. };
  55. static const ReduceEntry ReduceTable[] = {
  56. // Wide, Narrow1, Narrow2, imm1,imm2, lo1, lo2, P/C,PF,S,AM
  57. { ARM::t2ADCrr, 0, ARM::tADC, 0, 0, 0, 1, 0,0, 0,0,0 },
  58. { ARM::t2ADDri, ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 0,0, 0,1,0 },
  59. { ARM::t2ADDrr, ARM::tADDrr, ARM::tADDhirr, 0, 0, 1, 0, 0,1, 0,0,0 },
  60. { ARM::t2ADDSri,ARM::tADDi3, ARM::tADDi8, 3, 8, 1, 1, 2,2, 0,1,0 },
  61. { ARM::t2ADDSrr,ARM::tADDrr, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
  62. { ARM::t2ANDrr, 0, ARM::tAND, 0, 0, 0, 1, 0,0, 1,0,0 },
  63. { ARM::t2ASRri, ARM::tASRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
  64. { ARM::t2ASRrr, 0, ARM::tASRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
  65. { ARM::t2BICrr, 0, ARM::tBIC, 0, 0, 0, 1, 0,0, 1,0,0 },
  66. //FIXME: Disable CMN, as CCodes are backwards from compare expectations
  67. //{ ARM::t2CMNrr, ARM::tCMN, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
  68. { ARM::t2CMNzrr, ARM::tCMNz, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
  69. { ARM::t2CMPri, ARM::tCMPi8, 0, 8, 0, 1, 0, 2,0, 0,0,0 },
  70. { ARM::t2CMPrr, ARM::tCMPhir, 0, 0, 0, 0, 0, 2,0, 0,1,0 },
  71. { ARM::t2EORrr, 0, ARM::tEOR, 0, 0, 0, 1, 0,0, 1,0,0 },
  72. // FIXME: adr.n immediate offset must be multiple of 4.
  73. //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
  74. { ARM::t2LSLri, ARM::tLSLri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
  75. { ARM::t2LSLrr, 0, ARM::tLSLrr, 0, 0, 0, 1, 0,0, 1,0,1 },
  76. { ARM::t2LSRri, ARM::tLSRri, 0, 5, 0, 1, 0, 0,0, 1,0,1 },
  77. { ARM::t2LSRrr, 0, ARM::tLSRrr, 0, 0, 0, 1, 0,0, 1,0,1 },
  78. { ARM::t2MOVi, ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,0,0 },
  79. { ARM::t2MOVi16,ARM::tMOVi8, 0, 8, 0, 1, 0, 0,0, 1,1,0 },
  80. // FIXME: Do we need the 16-bit 'S' variant?
  81. { ARM::t2MOVr,ARM::tMOVr, 0, 0, 0, 0, 0, 1,0, 0,0,0 },
  82. { ARM::t2MUL, 0, ARM::tMUL, 0, 0, 0, 1, 0,0, 1,0,0 },
  83. { ARM::t2MVNr, ARM::tMVN, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
  84. { ARM::t2ORRrr, 0, ARM::tORR, 0, 0, 0, 1, 0,0, 1,0,0 },
  85. { ARM::t2REV, ARM::tREV, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
  86. { ARM::t2REV16, ARM::tREV16, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
  87. { ARM::t2REVSH, ARM::tREVSH, 0, 0, 0, 1, 0, 1,0, 0,0,0 },
  88. { ARM::t2RORrr, 0, ARM::tROR, 0, 0, 0, 1, 0,0, 1,0,0 },
  89. { ARM::t2RSBri, ARM::tRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  90. { ARM::t2RSBSri,ARM::tRSB, 0, 0, 0, 1, 0, 2,0, 0,1,0 },
  91. { ARM::t2SBCrr, 0, ARM::tSBC, 0, 0, 0, 1, 0,0, 0,0,0 },
  92. { ARM::t2SUBri, ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 0,0, 0,0,0 },
  93. { ARM::t2SUBrr, ARM::tSUBrr, 0, 0, 0, 1, 0, 0,0, 0,0,0 },
  94. { ARM::t2SUBSri,ARM::tSUBi3, ARM::tSUBi8, 3, 8, 1, 1, 2,2, 0,0,0 },
  95. { ARM::t2SUBSrr,ARM::tSUBrr, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
  96. { ARM::t2SXTB, ARM::tSXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
  97. { ARM::t2SXTH, ARM::tSXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
  98. { ARM::t2TSTrr, ARM::tTST, 0, 0, 0, 1, 0, 2,0, 0,0,0 },
  99. { ARM::t2UXTB, ARM::tUXTB, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
  100. { ARM::t2UXTH, ARM::tUXTH, 0, 0, 0, 1, 0, 1,0, 0,1,0 },
  101. // FIXME: Clean this up after splitting each Thumb load / store opcode
  102. // into multiple ones.
  103. { ARM::t2LDRi12,ARM::tLDRi, ARM::tLDRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
  104. { ARM::t2LDRs, ARM::tLDRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  105. { ARM::t2LDRBi12,ARM::tLDRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
  106. { ARM::t2LDRBs, ARM::tLDRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  107. { ARM::t2LDRHi12,ARM::tLDRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
  108. { ARM::t2LDRHs, ARM::tLDRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  109. { ARM::t2LDRSBs,ARM::tLDRSB, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  110. { ARM::t2LDRSHs,ARM::tLDRSH, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  111. { ARM::t2STRi12,ARM::tSTRi, ARM::tSTRspi, 5, 8, 1, 0, 0,0, 0,1,0 },
  112. { ARM::t2STRs, ARM::tSTRr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  113. { ARM::t2STRBi12,ARM::tSTRBi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
  114. { ARM::t2STRBs, ARM::tSTRBr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  115. { ARM::t2STRHi12,ARM::tSTRHi, 0, 5, 0, 1, 0, 0,0, 0,1,0 },
  116. { ARM::t2STRHs, ARM::tSTRHr, 0, 0, 0, 1, 0, 0,0, 0,1,0 },
  117. { ARM::t2LDMIA, ARM::tLDMIA, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
  118. { ARM::t2LDMIA_RET,0, ARM::tPOP_RET, 0, 0, 1, 1, 1,1, 0,1,0 },
  119. { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0, 0, 1, 1, 1,1, 0,1,0 },
  120. // ARM::t2STM (with no basereg writeback) has no Thumb1 equivalent
  121. { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0, 0, 0, 1, 1, 1,1, 0,1,0 },
  122. { ARM::t2STMDB_UPD, 0, ARM::tPUSH, 0, 0, 1, 1, 1,1, 0,1,0 }
  123. };
  124. class Thumb2SizeReduce : public MachineFunctionPass {
  125. public:
  126. static char ID;
  127. Thumb2SizeReduce();
  128. const Thumb2InstrInfo *TII;
  129. const ARMSubtarget *STI;
  130. virtual bool runOnMachineFunction(MachineFunction &MF);
  131. virtual const char *getPassName() const {
  132. return "Thumb2 instruction size reduction pass";
  133. }
  134. private:
  135. /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
  136. DenseMap<unsigned, unsigned> ReduceOpcodeMap;
  137. bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop);
  138. bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
  139. bool is2Addr, ARMCC::CondCodes Pred,
  140. bool LiveCPSR, bool &HasCC, bool &CCDead);
  141. bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
  142. const ReduceEntry &Entry);
  143. bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
  144. const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
  145. /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
  146. /// instruction.
  147. bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
  148. const ReduceEntry &Entry, bool LiveCPSR,
  149. bool IsSelfLoop);
  150. /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
  151. /// non-two-address instruction.
  152. bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
  153. const ReduceEntry &Entry, bool LiveCPSR,
  154. bool IsSelfLoop);
  155. /// ReduceMI - Attempt to reduce MI, return true on success.
  156. bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
  157. bool LiveCPSR, bool IsSelfLoop);
  158. /// ReduceMBB - Reduce width of instructions in the specified basic block.
  159. bool ReduceMBB(MachineBasicBlock &MBB);
  160. bool OptimizeSize;
  161. bool MinimizeSize;
  162. // Last instruction to define CPSR in the current block.
  163. MachineInstr *CPSRDef;
  164. // Was CPSR last defined by a high latency instruction?
  165. // When CPSRDef is null, this refers to CPSR defs in predecessors.
  166. bool HighLatencyCPSR;
  167. struct MBBInfo {
  168. // The flags leaving this block have high latency.
  169. bool HighLatencyCPSR;
  170. // Has this block been visited yet?
  171. bool Visited;
  172. MBBInfo() : HighLatencyCPSR(false), Visited(false) {}
  173. };
  174. SmallVector<MBBInfo, 8> BlockInfo;
  175. };
  176. char Thumb2SizeReduce::ID = 0;
  177. }
  178. Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(ID) {
  179. OptimizeSize = MinimizeSize = false;
  180. for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
  181. unsigned FromOpc = ReduceTable[i].WideOpc;
  182. if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
  183. assert(false && "Duplicated entries?");
  184. }
  185. }
  186. static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
  187. for (const uint16_t *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
  188. if (*Regs == ARM::CPSR)
  189. return true;
  190. return false;
  191. }
  192. // Check for a likely high-latency flag def.
  193. static bool isHighLatencyCPSR(MachineInstr *Def) {
  194. switch(Def->getOpcode()) {
  195. case ARM::FMSTAT:
  196. case ARM::tMUL:
  197. return true;
  198. }
  199. return false;
  200. }
  201. /// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
  202. /// the 's' 16-bit instruction partially update CPSR. Abort the
  203. /// transformation to avoid adding false dependency on last CPSR setting
  204. /// instruction which hurts the ability for out-of-order execution engine
  205. /// to do register renaming magic.
  206. /// This function checks if there is a read-of-write dependency between the
  207. /// last instruction that defines the CPSR and the current instruction. If there
  208. /// is, then there is no harm done since the instruction cannot be retired
  209. /// before the CPSR setting instruction anyway.
  210. /// Note, we are not doing full dependency analysis here for the sake of compile
  211. /// time. We're not looking for cases like:
  212. /// r0 = muls ...
  213. /// r1 = add.w r0, ...
  214. /// ...
  215. /// = mul.w r1
  216. /// In this case it would have been ok to narrow the mul.w to muls since there
  217. /// are indirect RAW dependency between the muls and the mul.w
  218. bool
  219. Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) {
  220. // Disable the check for -Oz (aka OptimizeForSizeHarder).
  221. if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
  222. return false;
  223. if (!CPSRDef)
  224. // If this BB loops back to itself, conservatively avoid narrowing the
  225. // first instruction that does partial flag update.
  226. return HighLatencyCPSR || FirstInSelfLoop;
  227. SmallSet<unsigned, 2> Defs;
  228. for (unsigned i = 0, e = CPSRDef->getNumOperands(); i != e; ++i) {
  229. const MachineOperand &MO = CPSRDef->getOperand(i);
  230. if (!MO.isReg() || MO.isUndef() || MO.isUse())
  231. continue;
  232. unsigned Reg = MO.getReg();
  233. if (Reg == 0 || Reg == ARM::CPSR)
  234. continue;
  235. Defs.insert(Reg);
  236. }
  237. for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
  238. const MachineOperand &MO = Use->getOperand(i);
  239. if (!MO.isReg() || MO.isUndef() || MO.isDef())
  240. continue;
  241. unsigned Reg = MO.getReg();
  242. if (Defs.count(Reg))
  243. return false;
  244. }
  245. // If the current CPSR has high latency, try to avoid the false dependency.
  246. if (HighLatencyCPSR)
  247. return true;
  248. // tMOVi8 usually doesn't start long dependency chains, and there are a lot
  249. // of them, so always shrink them when CPSR doesn't have high latency.
  250. if (Use->getOpcode() == ARM::t2MOVi ||
  251. Use->getOpcode() == ARM::t2MOVi16)
  252. return false;
  253. // No read-after-write dependency. The narrowing will add false dependency.
  254. return true;
  255. }
  256. bool
  257. Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
  258. bool is2Addr, ARMCC::CondCodes Pred,
  259. bool LiveCPSR, bool &HasCC, bool &CCDead) {
  260. if ((is2Addr && Entry.PredCC2 == 0) ||
  261. (!is2Addr && Entry.PredCC1 == 0)) {
  262. if (Pred == ARMCC::AL) {
  263. // Not predicated, must set CPSR.
  264. if (!HasCC) {
  265. // Original instruction was not setting CPSR, but CPSR is not
  266. // currently live anyway. It's ok to set it. The CPSR def is
  267. // dead though.
  268. if (!LiveCPSR) {
  269. HasCC = true;
  270. CCDead = true;
  271. return true;
  272. }
  273. return false;
  274. }
  275. } else {
  276. // Predicated, must not set CPSR.
  277. if (HasCC)
  278. return false;
  279. }
  280. } else if ((is2Addr && Entry.PredCC2 == 2) ||
  281. (!is2Addr && Entry.PredCC1 == 2)) {
  282. /// Old opcode has an optional def of CPSR.
  283. if (HasCC)
  284. return true;
  285. // If old opcode does not implicitly define CPSR, then it's not ok since
  286. // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
  287. if (!HasImplicitCPSRDef(MI->getDesc()))
  288. return false;
  289. HasCC = true;
  290. } else {
  291. // 16-bit instruction does not set CPSR.
  292. if (HasCC)
  293. return false;
  294. }
  295. return true;
  296. }
  297. static bool VerifyLowRegs(MachineInstr *MI) {
  298. unsigned Opc = MI->getOpcode();
  299. bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA ||
  300. Opc == ARM::t2LDMDB || Opc == ARM::t2LDMIA_UPD ||
  301. Opc == ARM::t2LDMDB_UPD);
  302. bool isLROk = (Opc == ARM::t2STMIA_UPD || Opc == ARM::t2STMDB_UPD);
  303. bool isSPOk = isPCOk || isLROk;
  304. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  305. const MachineOperand &MO = MI->getOperand(i);
  306. if (!MO.isReg() || MO.isImplicit())
  307. continue;
  308. unsigned Reg = MO.getReg();
  309. if (Reg == 0 || Reg == ARM::CPSR)
  310. continue;
  311. if (isPCOk && Reg == ARM::PC)
  312. continue;
  313. if (isLROk && Reg == ARM::LR)
  314. continue;
  315. if (Reg == ARM::SP) {
  316. if (isSPOk)
  317. continue;
  318. if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
  319. // Special case for these ldr / str with sp as base register.
  320. continue;
  321. }
  322. if (!isARMLowRegister(Reg))
  323. return false;
  324. }
  325. return true;
  326. }
  327. bool
  328. Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
  329. const ReduceEntry &Entry) {
  330. if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
  331. return false;
  332. unsigned Scale = 1;
  333. bool HasImmOffset = false;
  334. bool HasShift = false;
  335. bool HasOffReg = true;
  336. bool isLdStMul = false;
  337. unsigned Opc = Entry.NarrowOpc1;
  338. unsigned OpNum = 3; // First 'rest' of operands.
  339. uint8_t ImmLimit = Entry.Imm1Limit;
  340. switch (Entry.WideOpc) {
  341. default:
  342. llvm_unreachable("Unexpected Thumb2 load / store opcode!");
  343. case ARM::t2LDRi12:
  344. case ARM::t2STRi12:
  345. if (MI->getOperand(1).getReg() == ARM::SP) {
  346. Opc = Entry.NarrowOpc2;
  347. ImmLimit = Entry.Imm2Limit;
  348. HasOffReg = false;
  349. }
  350. Scale = 4;
  351. HasImmOffset = true;
  352. HasOffReg = false;
  353. break;
  354. case ARM::t2LDRBi12:
  355. case ARM::t2STRBi12:
  356. HasImmOffset = true;
  357. HasOffReg = false;
  358. break;
  359. case ARM::t2LDRHi12:
  360. case ARM::t2STRHi12:
  361. Scale = 2;
  362. HasImmOffset = true;
  363. HasOffReg = false;
  364. break;
  365. case ARM::t2LDRs:
  366. case ARM::t2LDRBs:
  367. case ARM::t2LDRHs:
  368. case ARM::t2LDRSBs:
  369. case ARM::t2LDRSHs:
  370. case ARM::t2STRs:
  371. case ARM::t2STRBs:
  372. case ARM::t2STRHs:
  373. HasShift = true;
  374. OpNum = 4;
  375. break;
  376. case ARM::t2LDMIA:
  377. case ARM::t2LDMDB: {
  378. unsigned BaseReg = MI->getOperand(0).getReg();
  379. if (!isARMLowRegister(BaseReg) || Entry.WideOpc != ARM::t2LDMIA)
  380. return false;
  381. // For the non-writeback version (this one), the base register must be
  382. // one of the registers being loaded.
  383. bool isOK = false;
  384. for (unsigned i = 4; i < MI->getNumOperands(); ++i) {
  385. if (MI->getOperand(i).getReg() == BaseReg) {
  386. isOK = true;
  387. break;
  388. }
  389. }
  390. if (!isOK)
  391. return false;
  392. OpNum = 0;
  393. isLdStMul = true;
  394. break;
  395. }
  396. case ARM::t2LDMIA_RET: {
  397. unsigned BaseReg = MI->getOperand(1).getReg();
  398. if (BaseReg != ARM::SP)
  399. return false;
  400. Opc = Entry.NarrowOpc2; // tPOP_RET
  401. OpNum = 2;
  402. isLdStMul = true;
  403. break;
  404. }
  405. case ARM::t2LDMIA_UPD:
  406. case ARM::t2LDMDB_UPD:
  407. case ARM::t2STMIA_UPD:
  408. case ARM::t2STMDB_UPD: {
  409. OpNum = 0;
  410. unsigned BaseReg = MI->getOperand(1).getReg();
  411. if (BaseReg == ARM::SP &&
  412. (Entry.WideOpc == ARM::t2LDMIA_UPD ||
  413. Entry.WideOpc == ARM::t2STMDB_UPD)) {
  414. Opc = Entry.NarrowOpc2; // tPOP or tPUSH
  415. OpNum = 2;
  416. } else if (!isARMLowRegister(BaseReg) ||
  417. (Entry.WideOpc != ARM::t2LDMIA_UPD &&
  418. Entry.WideOpc != ARM::t2STMIA_UPD)) {
  419. return false;
  420. }
  421. isLdStMul = true;
  422. break;
  423. }
  424. }
  425. unsigned OffsetReg = 0;
  426. bool OffsetKill = false;
  427. if (HasShift) {
  428. OffsetReg = MI->getOperand(2).getReg();
  429. OffsetKill = MI->getOperand(2).isKill();
  430. if (MI->getOperand(3).getImm())
  431. // Thumb1 addressing mode doesn't support shift.
  432. return false;
  433. }
  434. unsigned OffsetImm = 0;
  435. if (HasImmOffset) {
  436. OffsetImm = MI->getOperand(2).getImm();
  437. unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
  438. if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
  439. // Make sure the immediate field fits.
  440. return false;
  441. }
  442. // Add the 16-bit load / store instruction.
  443. DebugLoc dl = MI->getDebugLoc();
  444. MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
  445. if (!isLdStMul) {
  446. MIB.addOperand(MI->getOperand(0));
  447. MIB.addOperand(MI->getOperand(1));
  448. if (HasImmOffset)
  449. MIB.addImm(OffsetImm / Scale);
  450. assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
  451. if (HasOffReg)
  452. MIB.addReg(OffsetReg, getKillRegState(OffsetKill));
  453. }
  454. // Transfer the rest of operands.
  455. for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
  456. MIB.addOperand(MI->getOperand(OpNum));
  457. // Transfer memoperands.
  458. MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
  459. // Transfer MI flags.
  460. MIB.setMIFlags(MI->getFlags());
  461. DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
  462. MBB.erase_instr(MI);
  463. ++NumLdSts;
  464. return true;
  465. }
  466. bool
  467. Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
  468. const ReduceEntry &Entry,
  469. bool LiveCPSR, bool IsSelfLoop) {
  470. unsigned Opc = MI->getOpcode();
  471. if (Opc == ARM::t2ADDri) {
  472. // If the source register is SP, try to reduce to tADDrSPi, otherwise
  473. // it's a normal reduce.
  474. if (MI->getOperand(1).getReg() != ARM::SP) {
  475. if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
  476. return true;
  477. return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
  478. }
  479. // Try to reduce to tADDrSPi.
  480. unsigned Imm = MI->getOperand(2).getImm();
  481. // The immediate must be in range, the destination register must be a low
  482. // reg, the predicate must be "always" and the condition flags must not
  483. // be being set.
  484. if (Imm & 3 || Imm > 1020)
  485. return false;
  486. if (!isARMLowRegister(MI->getOperand(0).getReg()))
  487. return false;
  488. if (MI->getOperand(3).getImm() != ARMCC::AL)
  489. return false;
  490. const MCInstrDesc &MCID = MI->getDesc();
  491. if (MCID.hasOptionalDef() &&
  492. MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
  493. return false;
  494. MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(),
  495. TII->get(ARM::tADDrSPi))
  496. .addOperand(MI->getOperand(0))
  497. .addOperand(MI->getOperand(1))
  498. .addImm(Imm / 4); // The tADDrSPi has an implied scale by four.
  499. AddDefaultPred(MIB);
  500. // Transfer MI flags.
  501. MIB.setMIFlags(MI->getFlags());
  502. DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " <<*MIB);
  503. MBB.erase_instr(MI);
  504. ++NumNarrows;
  505. return true;
  506. }
  507. if (Entry.LowRegs1 && !VerifyLowRegs(MI))
  508. return false;
  509. if (MI->mayLoad() || MI->mayStore())
  510. return ReduceLoadStore(MBB, MI, Entry);
  511. switch (Opc) {
  512. default: break;
  513. case ARM::t2ADDSri:
  514. case ARM::t2ADDSrr: {
  515. unsigned PredReg = 0;
  516. if (getInstrPredicate(MI, PredReg) == ARMCC::AL) {
  517. switch (Opc) {
  518. default: break;
  519. case ARM::t2ADDSri: {
  520. if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
  521. return true;
  522. // fallthrough
  523. }
  524. case ARM::t2ADDSrr:
  525. return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
  526. }
  527. }
  528. break;
  529. }
  530. case ARM::t2RSBri:
  531. case ARM::t2RSBSri:
  532. case ARM::t2SXTB:
  533. case ARM::t2SXTH:
  534. case ARM::t2UXTB:
  535. case ARM::t2UXTH:
  536. if (MI->getOperand(2).getImm() == 0)
  537. return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
  538. break;
  539. case ARM::t2MOVi16:
  540. // Can convert only 'pure' immediate operands, not immediates obtained as
  541. // globals' addresses.
  542. if (MI->getOperand(1).isImm())
  543. return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
  544. break;
  545. case ARM::t2CMPrr: {
  546. // Try to reduce to the lo-reg only version first. Why there are two
  547. // versions of the instruction is a mystery.
  548. // It would be nice to just have two entries in the master table that
  549. // are prioritized, but the table assumes a unique entry for each
  550. // source insn opcode. So for now, we hack a local entry record to use.
  551. static const ReduceEntry NarrowEntry =
  552. { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
  553. if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
  554. return true;
  555. return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
  556. }
  557. }
  558. return false;
  559. }
  560. bool
  561. Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
  562. const ReduceEntry &Entry,
  563. bool LiveCPSR, bool IsSelfLoop) {
  564. if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
  565. return false;
  566. if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
  567. STI->avoidMOVsShifterOperand())
  568. // Don't issue movs with shifter operand for some CPUs unless we
  569. // are optimizing / minimizing for size.
  570. return false;
  571. unsigned Reg0 = MI->getOperand(0).getReg();
  572. unsigned Reg1 = MI->getOperand(1).getReg();
  573. // t2MUL is "special". The tied source operand is second, not first.
  574. if (MI->getOpcode() == ARM::t2MUL) {
  575. unsigned Reg2 = MI->getOperand(2).getReg();
  576. // Early exit if the regs aren't all low regs.
  577. if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
  578. || !isARMLowRegister(Reg2))
  579. return false;
  580. if (Reg0 != Reg2) {
  581. // If the other operand also isn't the same as the destination, we
  582. // can't reduce.
  583. if (Reg1 != Reg0)
  584. return false;
  585. // Try to commute the operands to make it a 2-address instruction.
  586. MachineInstr *CommutedMI = TII->commuteInstruction(MI);
  587. if (!CommutedMI)
  588. return false;
  589. }
  590. } else if (Reg0 != Reg1) {
  591. // Try to commute the operands to make it a 2-address instruction.
  592. unsigned CommOpIdx1, CommOpIdx2;
  593. if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
  594. CommOpIdx1 != 1 || MI->getOperand(CommOpIdx2).getReg() != Reg0)
  595. return false;
  596. MachineInstr *CommutedMI = TII->commuteInstruction(MI);
  597. if (!CommutedMI)
  598. return false;
  599. }
  600. if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
  601. return false;
  602. if (Entry.Imm2Limit) {
  603. unsigned Imm = MI->getOperand(2).getImm();
  604. unsigned Limit = (1 << Entry.Imm2Limit) - 1;
  605. if (Imm > Limit)
  606. return false;
  607. } else {
  608. unsigned Reg2 = MI->getOperand(2).getReg();
  609. if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
  610. return false;
  611. }
  612. // Check if it's possible / necessary to transfer the predicate.
  613. const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
  614. unsigned PredReg = 0;
  615. ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
  616. bool SkipPred = false;
  617. if (Pred != ARMCC::AL) {
  618. if (!NewMCID.isPredicable())
  619. // Can't transfer predicate, fail.
  620. return false;
  621. } else {
  622. SkipPred = !NewMCID.isPredicable();
  623. }
  624. bool HasCC = false;
  625. bool CCDead = false;
  626. const MCInstrDesc &MCID = MI->getDesc();
  627. if (MCID.hasOptionalDef()) {
  628. unsigned NumOps = MCID.getNumOperands();
  629. HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
  630. if (HasCC && MI->getOperand(NumOps-1).isDead())
  631. CCDead = true;
  632. }
  633. if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
  634. return false;
  635. // Avoid adding a false dependency on partial flag update by some 16-bit
  636. // instructions which has the 's' bit set.
  637. if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
  638. canAddPseudoFlagDep(MI, IsSelfLoop))
  639. return false;
  640. // Add the 16-bit instruction.
  641. DebugLoc dl = MI->getDebugLoc();
  642. MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
  643. MIB.addOperand(MI->getOperand(0));
  644. if (NewMCID.hasOptionalDef()) {
  645. if (HasCC)
  646. AddDefaultT1CC(MIB, CCDead);
  647. else
  648. AddNoT1CC(MIB);
  649. }
  650. // Transfer the rest of operands.
  651. unsigned NumOps = MCID.getNumOperands();
  652. for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
  653. if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
  654. continue;
  655. if (SkipPred && MCID.OpInfo[i].isPredicate())
  656. continue;
  657. MIB.addOperand(MI->getOperand(i));
  658. }
  659. // Transfer MI flags.
  660. MIB.setMIFlags(MI->getFlags());
  661. DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
  662. MBB.erase_instr(MI);
  663. ++Num2Addrs;
  664. return true;
  665. }
  666. bool
  667. Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
  668. const ReduceEntry &Entry,
  669. bool LiveCPSR, bool IsSelfLoop) {
  670. if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
  671. return false;
  672. if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
  673. STI->avoidMOVsShifterOperand())
  674. // Don't issue movs with shifter operand for some CPUs unless we
  675. // are optimizing / minimizing for size.
  676. return false;
  677. unsigned Limit = ~0U;
  678. if (Entry.Imm1Limit)
  679. Limit = (1 << Entry.Imm1Limit) - 1;
  680. const MCInstrDesc &MCID = MI->getDesc();
  681. for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
  682. if (MCID.OpInfo[i].isPredicate())
  683. continue;
  684. const MachineOperand &MO = MI->getOperand(i);
  685. if (MO.isReg()) {
  686. unsigned Reg = MO.getReg();
  687. if (!Reg || Reg == ARM::CPSR)
  688. continue;
  689. if (Entry.LowRegs1 && !isARMLowRegister(Reg))
  690. return false;
  691. } else if (MO.isImm() &&
  692. !MCID.OpInfo[i].isPredicate()) {
  693. if (((unsigned)MO.getImm()) > Limit)
  694. return false;
  695. }
  696. }
  697. // Check if it's possible / necessary to transfer the predicate.
  698. const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
  699. unsigned PredReg = 0;
  700. ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
  701. bool SkipPred = false;
  702. if (Pred != ARMCC::AL) {
  703. if (!NewMCID.isPredicable())
  704. // Can't transfer predicate, fail.
  705. return false;
  706. } else {
  707. SkipPred = !NewMCID.isPredicable();
  708. }
  709. bool HasCC = false;
  710. bool CCDead = false;
  711. if (MCID.hasOptionalDef()) {
  712. unsigned NumOps = MCID.getNumOperands();
  713. HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
  714. if (HasCC && MI->getOperand(NumOps-1).isDead())
  715. CCDead = true;
  716. }
  717. if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
  718. return false;
  719. // Avoid adding a false dependency on partial flag update by some 16-bit
  720. // instructions which has the 's' bit set.
  721. if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
  722. canAddPseudoFlagDep(MI, IsSelfLoop))
  723. return false;
  724. // Add the 16-bit instruction.
  725. DebugLoc dl = MI->getDebugLoc();
  726. MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
  727. MIB.addOperand(MI->getOperand(0));
  728. if (NewMCID.hasOptionalDef()) {
  729. if (HasCC)
  730. AddDefaultT1CC(MIB, CCDead);
  731. else
  732. AddNoT1CC(MIB);
  733. }
  734. // Transfer the rest of operands.
  735. unsigned NumOps = MCID.getNumOperands();
  736. for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
  737. if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
  738. continue;
  739. if ((MCID.getOpcode() == ARM::t2RSBSri ||
  740. MCID.getOpcode() == ARM::t2RSBri ||
  741. MCID.getOpcode() == ARM::t2SXTB ||
  742. MCID.getOpcode() == ARM::t2SXTH ||
  743. MCID.getOpcode() == ARM::t2UXTB ||
  744. MCID.getOpcode() == ARM::t2UXTH) && i == 2)
  745. // Skip the zero immediate operand, it's now implicit.
  746. continue;
  747. bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
  748. if (SkipPred && isPred)
  749. continue;
  750. const MachineOperand &MO = MI->getOperand(i);
  751. if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
  752. // Skip implicit def of CPSR. Either it's modeled as an optional
  753. // def now or it's already an implicit def on the new instruction.
  754. continue;
  755. MIB.addOperand(MO);
  756. }
  757. if (!MCID.isPredicable() && NewMCID.isPredicable())
  758. AddDefaultPred(MIB);
  759. // Transfer MI flags.
  760. MIB.setMIFlags(MI->getFlags());
  761. DEBUG(errs() << "Converted 32-bit: " << *MI << " to 16-bit: " << *MIB);
  762. MBB.erase_instr(MI);
  763. ++NumNarrows;
  764. return true;
  765. }
  766. static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
  767. bool HasDef = false;
  768. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  769. const MachineOperand &MO = MI.getOperand(i);
  770. if (!MO.isReg() || MO.isUndef() || MO.isUse())
  771. continue;
  772. if (MO.getReg() != ARM::CPSR)
  773. continue;
  774. DefCPSR = true;
  775. if (!MO.isDead())
  776. HasDef = true;
  777. }
  778. return HasDef || LiveCPSR;
  779. }
  780. static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
  781. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  782. const MachineOperand &MO = MI.getOperand(i);
  783. if (!MO.isReg() || MO.isUndef() || MO.isDef())
  784. continue;
  785. if (MO.getReg() != ARM::CPSR)
  786. continue;
  787. assert(LiveCPSR && "CPSR liveness tracking is wrong!");
  788. if (MO.isKill()) {
  789. LiveCPSR = false;
  790. break;
  791. }
  792. }
  793. return LiveCPSR;
  794. }
  795. bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
  796. bool LiveCPSR, bool IsSelfLoop) {
  797. unsigned Opcode = MI->getOpcode();
  798. DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
  799. if (OPI == ReduceOpcodeMap.end())
  800. return false;
  801. const ReduceEntry &Entry = ReduceTable[OPI->second];
  802. // Don't attempt normal reductions on "special" cases for now.
  803. if (Entry.Special)
  804. return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
  805. // Try to transform to a 16-bit two-address instruction.
  806. if (Entry.NarrowOpc2 &&
  807. ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
  808. return true;
  809. // Try to transform to a 16-bit non-two-address instruction.
  810. if (Entry.NarrowOpc1 &&
  811. ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
  812. return true;
  813. return false;
  814. }
  815. bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
  816. bool Modified = false;
  817. // Yes, CPSR could be livein.
  818. bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
  819. MachineInstr *BundleMI = 0;
  820. CPSRDef = 0;
  821. HighLatencyCPSR = false;
  822. // Check predecessors for the latest CPSRDef.
  823. for (MachineBasicBlock::pred_iterator
  824. I = MBB.pred_begin(), E = MBB.pred_end(); I != E; ++I) {
  825. const MBBInfo &PInfo = BlockInfo[(*I)->getNumber()];
  826. if (!PInfo.Visited) {
  827. // Since blocks are visited in RPO, this must be a back-edge.
  828. continue;
  829. }
  830. if (PInfo.HighLatencyCPSR) {
  831. HighLatencyCPSR = true;
  832. break;
  833. }
  834. }
  835. // If this BB loops back to itself, conservatively avoid narrowing the
  836. // first instruction that does partial flag update.
  837. bool IsSelfLoop = MBB.isSuccessor(&MBB);
  838. MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
  839. MachineBasicBlock::instr_iterator NextMII;
  840. for (; MII != E; MII = NextMII) {
  841. NextMII = std::next(MII);
  842. MachineInstr *MI = &*MII;
  843. if (MI->isBundle()) {
  844. BundleMI = MI;
  845. continue;
  846. }
  847. if (MI->isDebugValue())
  848. continue;
  849. LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
  850. // Does NextMII belong to the same bundle as MI?
  851. bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
  852. if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) {
  853. Modified = true;
  854. MachineBasicBlock::instr_iterator I = std::prev(NextMII);
  855. MI = &*I;
  856. // Removing and reinserting the first instruction in a bundle will break
  857. // up the bundle. Fix the bundling if it was broken.
  858. if (NextInSameBundle && !NextMII->isBundledWithPred())
  859. NextMII->bundleWithPred();
  860. }
  861. if (!NextInSameBundle && MI->isInsideBundle()) {
  862. // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
  863. // marker is only on the BUNDLE instruction. Process the BUNDLE
  864. // instruction as we finish with the bundled instruction to work around
  865. // the inconsistency.
  866. if (BundleMI->killsRegister(ARM::CPSR))
  867. LiveCPSR = false;
  868. MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
  869. if (MO && !MO->isDead())
  870. LiveCPSR = true;
  871. MO = BundleMI->findRegisterUseOperand(ARM::CPSR);
  872. if (MO && !MO->isKill())
  873. LiveCPSR = true;
  874. }
  875. bool DefCPSR = false;
  876. LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
  877. if (MI->isCall()) {
  878. // Calls don't really set CPSR.
  879. CPSRDef = 0;
  880. HighLatencyCPSR = false;
  881. IsSelfLoop = false;
  882. } else if (DefCPSR) {
  883. // This is the last CPSR defining instruction.
  884. CPSRDef = MI;
  885. HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
  886. IsSelfLoop = false;
  887. }
  888. }
  889. MBBInfo &Info = BlockInfo[MBB.getNumber()];
  890. Info.HighLatencyCPSR = HighLatencyCPSR;
  891. Info.Visited = true;
  892. return Modified;
  893. }
  894. bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
  895. const TargetMachine &TM = MF.getTarget();
  896. TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
  897. STI = &TM.getSubtarget<ARMSubtarget>();
  898. // Optimizing / minimizing size?
  899. AttributeSet FnAttrs = MF.getFunction()->getAttributes();
  900. OptimizeSize = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
  901. Attribute::OptimizeForSize);
  902. MinimizeSize = STI->isMinSize();
  903. BlockInfo.clear();
  904. BlockInfo.resize(MF.getNumBlockIDs());
  905. // Visit blocks in reverse post-order so LastCPSRDef is known for all
  906. // predecessors.
  907. ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
  908. bool Modified = false;
  909. for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator
  910. I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
  911. Modified |= ReduceMBB(**I);
  912. return Modified;
  913. }
  914. /// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
  915. /// reduction pass.
  916. FunctionPass *llvm::createThumb2SizeReductionPass() {
  917. return new Thumb2SizeReduce();
  918. }