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