LLVM API Documentation

Thumb2SizeReduction.cpp
Go to the documentation of this file.
00001 //===-- Thumb2SizeReduction.cpp - Thumb2 code size reduction pass -*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #define DEBUG_TYPE "t2-reduce-size"
00011 #include "ARM.h"
00012 #include "ARMBaseInstrInfo.h"
00013 #include "ARMBaseRegisterInfo.h"
00014 #include "ARMSubtarget.h"
00015 #include "MCTargetDesc/ARMAddressingModes.h"
00016 #include "Thumb2InstrInfo.h"
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/PostOrderIterator.h"
00019 #include "llvm/ADT/Statistic.h"
00020 #include "llvm/CodeGen/MachineFunctionPass.h"
00021 #include "llvm/CodeGen/MachineInstr.h"
00022 #include "llvm/CodeGen/MachineInstrBuilder.h"
00023 #include "llvm/IR/Function.h"        // To access Function attributes
00024 #include "llvm/Support/CommandLine.h"
00025 #include "llvm/Support/Debug.h"
00026 #include "llvm/Support/raw_ostream.h"
00027 using namespace llvm;
00028 
00029 STATISTIC(NumNarrows,  "Number of 32-bit instrs reduced to 16-bit ones");
00030 STATISTIC(Num2Addrs,   "Number of 32-bit instrs reduced to 2addr 16-bit ones");
00031 STATISTIC(NumLdSts,    "Number of 32-bit load / store reduced to 16-bit ones");
00032 
00033 static cl::opt<int> ReduceLimit("t2-reduce-limit",
00034                                 cl::init(-1), cl::Hidden);
00035 static cl::opt<int> ReduceLimit2Addr("t2-reduce-limit2",
00036                                      cl::init(-1), cl::Hidden);
00037 static cl::opt<int> ReduceLimitLdSt("t2-reduce-limit3",
00038                                      cl::init(-1), cl::Hidden);
00039 
00040 namespace {
00041   /// ReduceTable - A static table with information on mapping from wide
00042   /// opcodes to narrow
00043   struct ReduceEntry {
00044     uint16_t WideOpc;      // Wide opcode
00045     uint16_t NarrowOpc1;   // Narrow opcode to transform to
00046     uint16_t NarrowOpc2;   // Narrow opcode when it's two-address
00047     uint8_t  Imm1Limit;    // Limit of immediate field (bits)
00048     uint8_t  Imm2Limit;    // Limit of immediate field when it's two-address
00049     unsigned LowRegs1 : 1; // Only possible if low-registers are used
00050     unsigned LowRegs2 : 1; // Only possible if low-registers are used (2addr)
00051     unsigned PredCC1  : 2; // 0 - If predicated, cc is on and vice versa.
00052                            // 1 - No cc field.
00053                            // 2 - Always set CPSR.
00054     unsigned PredCC2  : 2;
00055     unsigned PartFlag : 1; // 16-bit instruction does partial flag update
00056     unsigned Special  : 1; // Needs to be dealt with specially
00057     unsigned AvoidMovs: 1; // Avoid movs with shifter operand (for Swift)
00058   };
00059 
00060   static const ReduceEntry ReduceTable[] = {
00061   // Wide,        Narrow1,      Narrow2,     imm1,imm2, lo1, lo2, P/C,PF,S,AM
00062   { ARM::t2ADCrr, 0,            ARM::tADC,     0,   0,   0,   1,  0,0, 0,0,0 },
00063   { ARM::t2ADDri, ARM::tADDi3,  ARM::tADDi8,   3,   8,   1,   1,  0,0, 0,1,0 },
00064   { ARM::t2ADDrr, ARM::tADDrr,  ARM::tADDhirr, 0,   0,   1,   0,  0,1, 0,0,0 },
00065   { ARM::t2ADDSri,ARM::tADDi3,  ARM::tADDi8,   3,   8,   1,   1,  2,2, 0,1,0 },
00066   { ARM::t2ADDSrr,ARM::tADDrr,  0,             0,   0,   1,   0,  2,0, 0,1,0 },
00067   { ARM::t2ANDrr, 0,            ARM::tAND,     0,   0,   0,   1,  0,0, 1,0,0 },
00068   { ARM::t2ASRri, ARM::tASRri,  0,             5,   0,   1,   0,  0,0, 1,0,1 },
00069   { ARM::t2ASRrr, 0,            ARM::tASRrr,   0,   0,   0,   1,  0,0, 1,0,1 },
00070   { ARM::t2BICrr, 0,            ARM::tBIC,     0,   0,   0,   1,  0,0, 1,0,0 },
00071   //FIXME: Disable CMN, as CCodes are backwards from compare expectations
00072   //{ ARM::t2CMNrr, ARM::tCMN,  0,             0,   0,   1,   0,  2,0, 0,0,0 },
00073   { ARM::t2CMNzrr, ARM::tCMNz,  0,             0,   0,   1,   0,  2,0, 0,0,0 },
00074   { ARM::t2CMPri, ARM::tCMPi8,  0,             8,   0,   1,   0,  2,0, 0,0,0 },
00075   { ARM::t2CMPrr, ARM::tCMPhir, 0,             0,   0,   0,   0,  2,0, 0,1,0 },
00076   { ARM::t2EORrr, 0,            ARM::tEOR,     0,   0,   0,   1,  0,0, 1,0,0 },
00077   // FIXME: adr.n immediate offset must be multiple of 4.
00078   //{ ARM::t2LEApcrelJT,ARM::tLEApcrelJT, 0,   0,   0,   1,   0,  1,0, 0,0,0 },
00079   { ARM::t2LSLri, ARM::tLSLri,  0,             5,   0,   1,   0,  0,0, 1,0,1 },
00080   { ARM::t2LSLrr, 0,            ARM::tLSLrr,   0,   0,   0,   1,  0,0, 1,0,1 },
00081   { ARM::t2LSRri, ARM::tLSRri,  0,             5,   0,   1,   0,  0,0, 1,0,1 },
00082   { ARM::t2LSRrr, 0,            ARM::tLSRrr,   0,   0,   0,   1,  0,0, 1,0,1 },
00083   { ARM::t2MOVi,  ARM::tMOVi8,  0,             8,   0,   1,   0,  0,0, 1,0,0 },
00084   { ARM::t2MOVi16,ARM::tMOVi8,  0,             8,   0,   1,   0,  0,0, 1,1,0 },
00085   // FIXME: Do we need the 16-bit 'S' variant?
00086   { ARM::t2MOVr,ARM::tMOVr,     0,             0,   0,   0,   0,  1,0, 0,0,0 },
00087   { ARM::t2MUL,   0,            ARM::tMUL,     0,   0,   0,   1,  0,0, 1,0,0 },
00088   { ARM::t2MVNr,  ARM::tMVN,    0,             0,   0,   1,   0,  0,0, 0,0,0 },
00089   { ARM::t2ORRrr, 0,            ARM::tORR,     0,   0,   0,   1,  0,0, 1,0,0 },
00090   { ARM::t2REV,   ARM::tREV,    0,             0,   0,   1,   0,  1,0, 0,0,0 },
00091   { ARM::t2REV16, ARM::tREV16,  0,             0,   0,   1,   0,  1,0, 0,0,0 },
00092   { ARM::t2REVSH, ARM::tREVSH,  0,             0,   0,   1,   0,  1,0, 0,0,0 },
00093   { ARM::t2RORrr, 0,            ARM::tROR,     0,   0,   0,   1,  0,0, 1,0,0 },
00094   { ARM::t2RSBri, ARM::tRSB,    0,             0,   0,   1,   0,  0,0, 0,1,0 },
00095   { ARM::t2RSBSri,ARM::tRSB,    0,             0,   0,   1,   0,  2,0, 0,1,0 },
00096   { ARM::t2SBCrr, 0,            ARM::tSBC,     0,   0,   0,   1,  0,0, 0,0,0 },
00097   { ARM::t2SUBri, ARM::tSUBi3,  ARM::tSUBi8,   3,   8,   1,   1,  0,0, 0,0,0 },
00098   { ARM::t2SUBrr, ARM::tSUBrr,  0,             0,   0,   1,   0,  0,0, 0,0,0 },
00099   { ARM::t2SUBSri,ARM::tSUBi3,  ARM::tSUBi8,   3,   8,   1,   1,  2,2, 0,0,0 },
00100   { ARM::t2SUBSrr,ARM::tSUBrr,  0,             0,   0,   1,   0,  2,0, 0,0,0 },
00101   { ARM::t2SXTB,  ARM::tSXTB,   0,             0,   0,   1,   0,  1,0, 0,1,0 },
00102   { ARM::t2SXTH,  ARM::tSXTH,   0,             0,   0,   1,   0,  1,0, 0,1,0 },
00103   { ARM::t2TSTrr, ARM::tTST,    0,             0,   0,   1,   0,  2,0, 0,0,0 },
00104   { ARM::t2UXTB,  ARM::tUXTB,   0,             0,   0,   1,   0,  1,0, 0,1,0 },
00105   { ARM::t2UXTH,  ARM::tUXTH,   0,             0,   0,   1,   0,  1,0, 0,1,0 },
00106 
00107   // FIXME: Clean this up after splitting each Thumb load / store opcode
00108   // into multiple ones.
00109   { ARM::t2LDRi12,ARM::tLDRi,   ARM::tLDRspi,  5,   8,   1,   0,  0,0, 0,1,0 },
00110   { ARM::t2LDRs,  ARM::tLDRr,   0,             0,   0,   1,   0,  0,0, 0,1,0 },
00111   { ARM::t2LDRBi12,ARM::tLDRBi, 0,             5,   0,   1,   0,  0,0, 0,1,0 },
00112   { ARM::t2LDRBs, ARM::tLDRBr,  0,             0,   0,   1,   0,  0,0, 0,1,0 },
00113   { ARM::t2LDRHi12,ARM::tLDRHi, 0,             5,   0,   1,   0,  0,0, 0,1,0 },
00114   { ARM::t2LDRHs, ARM::tLDRHr,  0,             0,   0,   1,   0,  0,0, 0,1,0 },
00115   { ARM::t2LDRSBs,ARM::tLDRSB,  0,             0,   0,   1,   0,  0,0, 0,1,0 },
00116   { ARM::t2LDRSHs,ARM::tLDRSH,  0,             0,   0,   1,   0,  0,0, 0,1,0 },
00117   { ARM::t2STRi12,ARM::tSTRi,   ARM::tSTRspi,  5,   8,   1,   0,  0,0, 0,1,0 },
00118   { ARM::t2STRs,  ARM::tSTRr,   0,             0,   0,   1,   0,  0,0, 0,1,0 },
00119   { ARM::t2STRBi12,ARM::tSTRBi, 0,             5,   0,   1,   0,  0,0, 0,1,0 },
00120   { ARM::t2STRBs, ARM::tSTRBr,  0,             0,   0,   1,   0,  0,0, 0,1,0 },
00121   { ARM::t2STRHi12,ARM::tSTRHi, 0,             5,   0,   1,   0,  0,0, 0,1,0 },
00122   { ARM::t2STRHs, ARM::tSTRHr,  0,             0,   0,   1,   0,  0,0, 0,1,0 },
00123 
00124   { ARM::t2LDMIA, ARM::tLDMIA,  0,             0,   0,   1,   1,  1,1, 0,1,0 },
00125   { ARM::t2LDMIA_RET,0,         ARM::tPOP_RET, 0,   0,   1,   1,  1,1, 0,1,0 },
00126   { ARM::t2LDMIA_UPD,ARM::tLDMIA_UPD,ARM::tPOP,0,   0,   1,   1,  1,1, 0,1,0 },
00127   // ARM::t2STM (with no basereg writeback) has no Thumb1 equivalent
00128   { ARM::t2STMIA_UPD,ARM::tSTMIA_UPD, 0,       0,   0,   1,   1,  1,1, 0,1,0 },
00129   { ARM::t2STMDB_UPD, 0,        ARM::tPUSH,    0,   0,   1,   1,  1,1, 0,1,0 }
00130   };
00131 
00132   class Thumb2SizeReduce : public MachineFunctionPass {
00133   public:
00134     static char ID;
00135     Thumb2SizeReduce();
00136 
00137     const Thumb2InstrInfo *TII;
00138     const ARMSubtarget *STI;
00139 
00140     virtual bool runOnMachineFunction(MachineFunction &MF);
00141 
00142     virtual const char *getPassName() const {
00143       return "Thumb2 instruction size reduction pass";
00144     }
00145 
00146   private:
00147     /// ReduceOpcodeMap - Maps wide opcode to index of entry in ReduceTable.
00148     DenseMap<unsigned, unsigned> ReduceOpcodeMap;
00149 
00150     bool canAddPseudoFlagDep(MachineInstr *Use, bool IsSelfLoop);
00151 
00152     bool VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
00153                          bool is2Addr, ARMCC::CondCodes Pred,
00154                          bool LiveCPSR, bool &HasCC, bool &CCDead);
00155 
00156     bool ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
00157                          const ReduceEntry &Entry);
00158 
00159     bool ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
00160                        const ReduceEntry &Entry, bool LiveCPSR, bool IsSelfLoop);
00161 
00162     /// ReduceTo2Addr - Reduce a 32-bit instruction to a 16-bit two-address
00163     /// instruction.
00164     bool ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
00165                        const ReduceEntry &Entry, bool LiveCPSR,
00166                        bool IsSelfLoop);
00167 
00168     /// ReduceToNarrow - Reduce a 32-bit instruction to a 16-bit
00169     /// non-two-address instruction.
00170     bool ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
00171                         const ReduceEntry &Entry, bool LiveCPSR,
00172                         bool IsSelfLoop);
00173 
00174     /// ReduceMI - Attempt to reduce MI, return true on success.
00175     bool ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
00176                   bool LiveCPSR, bool IsSelfLoop);
00177 
00178     /// ReduceMBB - Reduce width of instructions in the specified basic block.
00179     bool ReduceMBB(MachineBasicBlock &MBB);
00180 
00181     bool OptimizeSize;
00182     bool MinimizeSize;
00183 
00184     // Last instruction to define CPSR in the current block.
00185     MachineInstr *CPSRDef;
00186     // Was CPSR last defined by a high latency instruction?
00187     // When CPSRDef is null, this refers to CPSR defs in predecessors.
00188     bool HighLatencyCPSR;
00189 
00190     struct MBBInfo {
00191       // The flags leaving this block have high latency.
00192       bool HighLatencyCPSR;
00193       // Has this block been visited yet?
00194       bool Visited;
00195 
00196       MBBInfo() : HighLatencyCPSR(false), Visited(false) {}
00197     };
00198 
00199     SmallVector<MBBInfo, 8> BlockInfo;
00200   };
00201   char Thumb2SizeReduce::ID = 0;
00202 }
00203 
00204 Thumb2SizeReduce::Thumb2SizeReduce() : MachineFunctionPass(ID) {
00205   OptimizeSize = MinimizeSize = false;
00206   for (unsigned i = 0, e = array_lengthof(ReduceTable); i != e; ++i) {
00207     unsigned FromOpc = ReduceTable[i].WideOpc;
00208     if (!ReduceOpcodeMap.insert(std::make_pair(FromOpc, i)).second)
00209       assert(false && "Duplicated entries?");
00210   }
00211 }
00212 
00213 static bool HasImplicitCPSRDef(const MCInstrDesc &MCID) {
00214   for (const uint16_t *Regs = MCID.getImplicitDefs(); *Regs; ++Regs)
00215     if (*Regs == ARM::CPSR)
00216       return true;
00217   return false;
00218 }
00219 
00220 // Check for a likely high-latency flag def.
00221 static bool isHighLatencyCPSR(MachineInstr *Def) {
00222   switch(Def->getOpcode()) {
00223   case ARM::FMSTAT:
00224   case ARM::tMUL:
00225     return true;
00226   }
00227   return false;
00228 }
00229 
00230 /// canAddPseudoFlagDep - For A9 (and other out-of-order) implementations,
00231 /// the 's' 16-bit instruction partially update CPSR. Abort the
00232 /// transformation to avoid adding false dependency on last CPSR setting
00233 /// instruction which hurts the ability for out-of-order execution engine
00234 /// to do register renaming magic.
00235 /// This function checks if there is a read-of-write dependency between the
00236 /// last instruction that defines the CPSR and the current instruction. If there
00237 /// is, then there is no harm done since the instruction cannot be retired
00238 /// before the CPSR setting instruction anyway.
00239 /// Note, we are not doing full dependency analysis here for the sake of compile
00240 /// time. We're not looking for cases like:
00241 /// r0 = muls ...
00242 /// r1 = add.w r0, ...
00243 /// ...
00244 ///    = mul.w r1
00245 /// In this case it would have been ok to narrow the mul.w to muls since there
00246 /// are indirect RAW dependency between the muls and the mul.w
00247 bool
00248 Thumb2SizeReduce::canAddPseudoFlagDep(MachineInstr *Use, bool FirstInSelfLoop) {
00249   // Disable the check for -Oz (aka OptimizeForSizeHarder).
00250   if (MinimizeSize || !STI->avoidCPSRPartialUpdate())
00251     return false;
00252 
00253   if (!CPSRDef)
00254     // If this BB loops back to itself, conservatively avoid narrowing the
00255     // first instruction that does partial flag update.
00256     return HighLatencyCPSR || FirstInSelfLoop;
00257 
00258   SmallSet<unsigned, 2> Defs;
00259   for (unsigned i = 0, e = CPSRDef->getNumOperands(); i != e; ++i) {
00260     const MachineOperand &MO = CPSRDef->getOperand(i);
00261     if (!MO.isReg() || MO.isUndef() || MO.isUse())
00262       continue;
00263     unsigned Reg = MO.getReg();
00264     if (Reg == 0 || Reg == ARM::CPSR)
00265       continue;
00266     Defs.insert(Reg);
00267   }
00268 
00269   for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) {
00270     const MachineOperand &MO = Use->getOperand(i);
00271     if (!MO.isReg() || MO.isUndef() || MO.isDef())
00272       continue;
00273     unsigned Reg = MO.getReg();
00274     if (Defs.count(Reg))
00275       return false;
00276   }
00277 
00278   // If the current CPSR has high latency, try to avoid the false dependency.
00279   if (HighLatencyCPSR)
00280     return true;
00281 
00282   // tMOVi8 usually doesn't start long dependency chains, and there are a lot
00283   // of them, so always shrink them when CPSR doesn't have high latency.
00284   if (Use->getOpcode() == ARM::t2MOVi ||
00285       Use->getOpcode() == ARM::t2MOVi16)
00286     return false;
00287 
00288   // No read-after-write dependency. The narrowing will add false dependency.
00289   return true;
00290 }
00291 
00292 bool
00293 Thumb2SizeReduce::VerifyPredAndCC(MachineInstr *MI, const ReduceEntry &Entry,
00294                                   bool is2Addr, ARMCC::CondCodes Pred,
00295                                   bool LiveCPSR, bool &HasCC, bool &CCDead) {
00296   if ((is2Addr  && Entry.PredCC2 == 0) ||
00297       (!is2Addr && Entry.PredCC1 == 0)) {
00298     if (Pred == ARMCC::AL) {
00299       // Not predicated, must set CPSR.
00300       if (!HasCC) {
00301         // Original instruction was not setting CPSR, but CPSR is not
00302         // currently live anyway. It's ok to set it. The CPSR def is
00303         // dead though.
00304         if (!LiveCPSR) {
00305           HasCC = true;
00306           CCDead = true;
00307           return true;
00308         }
00309         return false;
00310       }
00311     } else {
00312       // Predicated, must not set CPSR.
00313       if (HasCC)
00314         return false;
00315     }
00316   } else if ((is2Addr  && Entry.PredCC2 == 2) ||
00317              (!is2Addr && Entry.PredCC1 == 2)) {
00318     /// Old opcode has an optional def of CPSR.
00319     if (HasCC)
00320       return true;
00321     // If old opcode does not implicitly define CPSR, then it's not ok since
00322     // these new opcodes' CPSR def is not meant to be thrown away. e.g. CMP.
00323     if (!HasImplicitCPSRDef(MI->getDesc()))
00324       return false;
00325     HasCC = true;
00326   } else {
00327     // 16-bit instruction does not set CPSR.
00328     if (HasCC)
00329       return false;
00330   }
00331 
00332   return true;
00333 }
00334 
00335 static bool VerifyLowRegs(MachineInstr *MI) {
00336   unsigned Opc = MI->getOpcode();
00337   bool isPCOk = (Opc == ARM::t2LDMIA_RET || Opc == ARM::t2LDMIA     ||
00338                  Opc == ARM::t2LDMDB     || Opc == ARM::t2LDMIA_UPD ||
00339                  Opc == ARM::t2LDMDB_UPD);
00340   bool isLROk = (Opc == ARM::t2STMIA_UPD || Opc == ARM::t2STMDB_UPD);
00341   bool isSPOk = isPCOk || isLROk;
00342   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
00343     const MachineOperand &MO = MI->getOperand(i);
00344     if (!MO.isReg() || MO.isImplicit())
00345       continue;
00346     unsigned Reg = MO.getReg();
00347     if (Reg == 0 || Reg == ARM::CPSR)
00348       continue;
00349     if (isPCOk && Reg == ARM::PC)
00350       continue;
00351     if (isLROk && Reg == ARM::LR)
00352       continue;
00353     if (Reg == ARM::SP) {
00354       if (isSPOk)
00355         continue;
00356       if (i == 1 && (Opc == ARM::t2LDRi12 || Opc == ARM::t2STRi12))
00357         // Special case for these ldr / str with sp as base register.
00358         continue;
00359     }
00360     if (!isARMLowRegister(Reg))
00361       return false;
00362   }
00363   return true;
00364 }
00365 
00366 bool
00367 Thumb2SizeReduce::ReduceLoadStore(MachineBasicBlock &MBB, MachineInstr *MI,
00368                                   const ReduceEntry &Entry) {
00369   if (ReduceLimitLdSt != -1 && ((int)NumLdSts >= ReduceLimitLdSt))
00370     return false;
00371 
00372   unsigned Scale = 1;
00373   bool HasImmOffset = false;
00374   bool HasShift = false;
00375   bool HasOffReg = true;
00376   bool isLdStMul = false;
00377   unsigned Opc = Entry.NarrowOpc1;
00378   unsigned OpNum = 3; // First 'rest' of operands.
00379   uint8_t  ImmLimit = Entry.Imm1Limit;
00380 
00381   switch (Entry.WideOpc) {
00382   default:
00383     llvm_unreachable("Unexpected Thumb2 load / store opcode!");
00384   case ARM::t2LDRi12:
00385   case ARM::t2STRi12:
00386     if (MI->getOperand(1).getReg() == ARM::SP) {
00387       Opc = Entry.NarrowOpc2;
00388       ImmLimit = Entry.Imm2Limit;
00389       HasOffReg = false;
00390     }
00391 
00392     Scale = 4;
00393     HasImmOffset = true;
00394     HasOffReg = false;
00395     break;
00396   case ARM::t2LDRBi12:
00397   case ARM::t2STRBi12:
00398     HasImmOffset = true;
00399     HasOffReg = false;
00400     break;
00401   case ARM::t2LDRHi12:
00402   case ARM::t2STRHi12:
00403     Scale = 2;
00404     HasImmOffset = true;
00405     HasOffReg = false;
00406     break;
00407   case ARM::t2LDRs:
00408   case ARM::t2LDRBs:
00409   case ARM::t2LDRHs:
00410   case ARM::t2LDRSBs:
00411   case ARM::t2LDRSHs:
00412   case ARM::t2STRs:
00413   case ARM::t2STRBs:
00414   case ARM::t2STRHs:
00415     HasShift = true;
00416     OpNum = 4;
00417     break;
00418   case ARM::t2LDMIA:
00419   case ARM::t2LDMDB: {
00420     unsigned BaseReg = MI->getOperand(0).getReg();
00421     if (!isARMLowRegister(BaseReg) || Entry.WideOpc != ARM::t2LDMIA)
00422       return false;
00423 
00424     // For the non-writeback version (this one), the base register must be
00425     // one of the registers being loaded.
00426     bool isOK = false;
00427     for (unsigned i = 4; i < MI->getNumOperands(); ++i) {
00428       if (MI->getOperand(i).getReg() == BaseReg) {
00429         isOK = true;
00430         break;
00431       }
00432     }
00433 
00434     if (!isOK)
00435       return false;
00436 
00437     OpNum = 0;
00438     isLdStMul = true;
00439     break;
00440   }
00441   case ARM::t2LDMIA_RET: {
00442     unsigned BaseReg = MI->getOperand(1).getReg();
00443     if (BaseReg != ARM::SP)
00444       return false;
00445     Opc = Entry.NarrowOpc2; // tPOP_RET
00446     OpNum = 2;
00447     isLdStMul = true;
00448     break;
00449   }
00450   case ARM::t2LDMIA_UPD:
00451   case ARM::t2LDMDB_UPD:
00452   case ARM::t2STMIA_UPD:
00453   case ARM::t2STMDB_UPD: {
00454     OpNum = 0;
00455 
00456     unsigned BaseReg = MI->getOperand(1).getReg();
00457     if (BaseReg == ARM::SP &&
00458         (Entry.WideOpc == ARM::t2LDMIA_UPD ||
00459          Entry.WideOpc == ARM::t2STMDB_UPD)) {
00460       Opc = Entry.NarrowOpc2; // tPOP or tPUSH
00461       OpNum = 2;
00462     } else if (!isARMLowRegister(BaseReg) ||
00463                (Entry.WideOpc != ARM::t2LDMIA_UPD &&
00464                 Entry.WideOpc != ARM::t2STMIA_UPD)) {
00465       return false;
00466     }
00467 
00468     isLdStMul = true;
00469     break;
00470   }
00471   }
00472 
00473   unsigned OffsetReg = 0;
00474   bool OffsetKill = false;
00475   if (HasShift) {
00476     OffsetReg  = MI->getOperand(2).getReg();
00477     OffsetKill = MI->getOperand(2).isKill();
00478 
00479     if (MI->getOperand(3).getImm())
00480       // Thumb1 addressing mode doesn't support shift.
00481       return false;
00482   }
00483 
00484   unsigned OffsetImm = 0;
00485   if (HasImmOffset) {
00486     OffsetImm = MI->getOperand(2).getImm();
00487     unsigned MaxOffset = ((1 << ImmLimit) - 1) * Scale;
00488 
00489     if ((OffsetImm & (Scale - 1)) || OffsetImm > MaxOffset)
00490       // Make sure the immediate field fits.
00491       return false;
00492   }
00493 
00494   // Add the 16-bit load / store instruction.
00495   DebugLoc dl = MI->getDebugLoc();
00496   MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, TII->get(Opc));
00497   if (!isLdStMul) {
00498     MIB.addOperand(MI->getOperand(0));
00499     MIB.addOperand(MI->getOperand(1));
00500 
00501     if (HasImmOffset)
00502       MIB.addImm(OffsetImm / Scale);
00503 
00504     assert((!HasShift || OffsetReg) && "Invalid so_reg load / store address!");
00505 
00506     if (HasOffReg)
00507       MIB.addReg(OffsetReg, getKillRegState(OffsetKill));
00508   }
00509 
00510   // Transfer the rest of operands.
00511   for (unsigned e = MI->getNumOperands(); OpNum != e; ++OpNum)
00512     MIB.addOperand(MI->getOperand(OpNum));
00513 
00514   // Transfer memoperands.
00515   MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
00516 
00517   // Transfer MI flags.
00518   MIB.setMIFlags(MI->getFlags());
00519 
00520   DEBUG(errs() << "Converted 32-bit: " << *MI << "       to 16-bit: " << *MIB);
00521 
00522   MBB.erase_instr(MI);
00523   ++NumLdSts;
00524   return true;
00525 }
00526 
00527 bool
00528 Thumb2SizeReduce::ReduceSpecial(MachineBasicBlock &MBB, MachineInstr *MI,
00529                                 const ReduceEntry &Entry,
00530                                 bool LiveCPSR, bool IsSelfLoop) {
00531   unsigned Opc = MI->getOpcode();
00532   if (Opc == ARM::t2ADDri) {
00533     // If the source register is SP, try to reduce to tADDrSPi, otherwise
00534     // it's a normal reduce.
00535     if (MI->getOperand(1).getReg() != ARM::SP) {
00536       if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
00537         return true;
00538       return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
00539     }
00540     // Try to reduce to tADDrSPi.
00541     unsigned Imm = MI->getOperand(2).getImm();
00542     // The immediate must be in range, the destination register must be a low
00543     // reg, the predicate must be "always" and the condition flags must not
00544     // be being set.
00545     if (Imm & 3 || Imm > 1020)
00546       return false;
00547     if (!isARMLowRegister(MI->getOperand(0).getReg()))
00548       return false;
00549     if (MI->getOperand(3).getImm() != ARMCC::AL)
00550       return false;
00551     const MCInstrDesc &MCID = MI->getDesc();
00552     if (MCID.hasOptionalDef() &&
00553         MI->getOperand(MCID.getNumOperands()-1).getReg() == ARM::CPSR)
00554       return false;
00555 
00556     MachineInstrBuilder MIB = BuildMI(MBB, MI, MI->getDebugLoc(),
00557                                       TII->get(ARM::tADDrSPi))
00558       .addOperand(MI->getOperand(0))
00559       .addOperand(MI->getOperand(1))
00560       .addImm(Imm / 4); // The tADDrSPi has an implied scale by four.
00561     AddDefaultPred(MIB);
00562 
00563     // Transfer MI flags.
00564     MIB.setMIFlags(MI->getFlags());
00565 
00566     DEBUG(errs() << "Converted 32-bit: " << *MI << "       to 16-bit: " <<*MIB);
00567 
00568     MBB.erase_instr(MI);
00569     ++NumNarrows;
00570     return true;
00571   }
00572 
00573   if (Entry.LowRegs1 && !VerifyLowRegs(MI))
00574     return false;
00575 
00576   if (MI->mayLoad() || MI->mayStore())
00577     return ReduceLoadStore(MBB, MI, Entry);
00578 
00579   switch (Opc) {
00580   default: break;
00581   case ARM::t2ADDSri:
00582   case ARM::t2ADDSrr: {
00583     unsigned PredReg = 0;
00584     if (getInstrPredicate(MI, PredReg) == ARMCC::AL) {
00585       switch (Opc) {
00586       default: break;
00587       case ARM::t2ADDSri: {
00588         if (ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
00589           return true;
00590         // fallthrough
00591       }
00592       case ARM::t2ADDSrr:
00593         return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
00594       }
00595     }
00596     break;
00597   }
00598   case ARM::t2RSBri:
00599   case ARM::t2RSBSri:
00600   case ARM::t2SXTB:
00601   case ARM::t2SXTH:
00602   case ARM::t2UXTB:
00603   case ARM::t2UXTH:
00604     if (MI->getOperand(2).getImm() == 0)
00605       return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
00606     break;
00607   case ARM::t2MOVi16:
00608     // Can convert only 'pure' immediate operands, not immediates obtained as
00609     // globals' addresses.
00610     if (MI->getOperand(1).isImm())
00611       return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
00612     break;
00613   case ARM::t2CMPrr: {
00614     // Try to reduce to the lo-reg only version first. Why there are two
00615     // versions of the instruction is a mystery.
00616     // It would be nice to just have two entries in the master table that
00617     // are prioritized, but the table assumes a unique entry for each
00618     // source insn opcode. So for now, we hack a local entry record to use.
00619     static const ReduceEntry NarrowEntry =
00620       { ARM::t2CMPrr,ARM::tCMPr, 0, 0, 0, 1, 1,2, 0, 0,1,0 };
00621     if (ReduceToNarrow(MBB, MI, NarrowEntry, LiveCPSR, IsSelfLoop))
00622       return true;
00623     return ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
00624   }
00625   }
00626   return false;
00627 }
00628 
00629 bool
00630 Thumb2SizeReduce::ReduceTo2Addr(MachineBasicBlock &MBB, MachineInstr *MI,
00631                                 const ReduceEntry &Entry,
00632                                 bool LiveCPSR, bool IsSelfLoop) {
00633 
00634   if (ReduceLimit2Addr != -1 && ((int)Num2Addrs >= ReduceLimit2Addr))
00635     return false;
00636 
00637   if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
00638       STI->avoidMOVsShifterOperand())
00639     // Don't issue movs with shifter operand for some CPUs unless we
00640     // are optimizing / minimizing for size.
00641     return false;
00642 
00643   unsigned Reg0 = MI->getOperand(0).getReg();
00644   unsigned Reg1 = MI->getOperand(1).getReg();
00645   // t2MUL is "special". The tied source operand is second, not first.
00646   if (MI->getOpcode() == ARM::t2MUL) {
00647     unsigned Reg2 = MI->getOperand(2).getReg();
00648     // Early exit if the regs aren't all low regs.
00649     if (!isARMLowRegister(Reg0) || !isARMLowRegister(Reg1)
00650         || !isARMLowRegister(Reg2))
00651       return false;
00652     if (Reg0 != Reg2) {
00653       // If the other operand also isn't the same as the destination, we
00654       // can't reduce.
00655       if (Reg1 != Reg0)
00656         return false;
00657       // Try to commute the operands to make it a 2-address instruction.
00658       MachineInstr *CommutedMI = TII->commuteInstruction(MI);
00659       if (!CommutedMI)
00660         return false;
00661     }
00662   } else if (Reg0 != Reg1) {
00663     // Try to commute the operands to make it a 2-address instruction.
00664     unsigned CommOpIdx1, CommOpIdx2;
00665     if (!TII->findCommutedOpIndices(MI, CommOpIdx1, CommOpIdx2) ||
00666         CommOpIdx1 != 1 || MI->getOperand(CommOpIdx2).getReg() != Reg0)
00667       return false;
00668     MachineInstr *CommutedMI = TII->commuteInstruction(MI);
00669     if (!CommutedMI)
00670       return false;
00671   }
00672   if (Entry.LowRegs2 && !isARMLowRegister(Reg0))
00673     return false;
00674   if (Entry.Imm2Limit) {
00675     unsigned Imm = MI->getOperand(2).getImm();
00676     unsigned Limit = (1 << Entry.Imm2Limit) - 1;
00677     if (Imm > Limit)
00678       return false;
00679   } else {
00680     unsigned Reg2 = MI->getOperand(2).getReg();
00681     if (Entry.LowRegs2 && !isARMLowRegister(Reg2))
00682       return false;
00683   }
00684 
00685   // Check if it's possible / necessary to transfer the predicate.
00686   const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc2);
00687   unsigned PredReg = 0;
00688   ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
00689   bool SkipPred = false;
00690   if (Pred != ARMCC::AL) {
00691     if (!NewMCID.isPredicable())
00692       // Can't transfer predicate, fail.
00693       return false;
00694   } else {
00695     SkipPred = !NewMCID.isPredicable();
00696   }
00697 
00698   bool HasCC = false;
00699   bool CCDead = false;
00700   const MCInstrDesc &MCID = MI->getDesc();
00701   if (MCID.hasOptionalDef()) {
00702     unsigned NumOps = MCID.getNumOperands();
00703     HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
00704     if (HasCC && MI->getOperand(NumOps-1).isDead())
00705       CCDead = true;
00706   }
00707   if (!VerifyPredAndCC(MI, Entry, true, Pred, LiveCPSR, HasCC, CCDead))
00708     return false;
00709 
00710   // Avoid adding a false dependency on partial flag update by some 16-bit
00711   // instructions which has the 's' bit set.
00712   if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
00713       canAddPseudoFlagDep(MI, IsSelfLoop))
00714     return false;
00715 
00716   // Add the 16-bit instruction.
00717   DebugLoc dl = MI->getDebugLoc();
00718   MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
00719   MIB.addOperand(MI->getOperand(0));
00720   if (NewMCID.hasOptionalDef()) {
00721     if (HasCC)
00722       AddDefaultT1CC(MIB, CCDead);
00723     else
00724       AddNoT1CC(MIB);
00725   }
00726 
00727   // Transfer the rest of operands.
00728   unsigned NumOps = MCID.getNumOperands();
00729   for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
00730     if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
00731       continue;
00732     if (SkipPred && MCID.OpInfo[i].isPredicate())
00733       continue;
00734     MIB.addOperand(MI->getOperand(i));
00735   }
00736 
00737   // Transfer MI flags.
00738   MIB.setMIFlags(MI->getFlags());
00739 
00740   DEBUG(errs() << "Converted 32-bit: " << *MI << "       to 16-bit: " << *MIB);
00741 
00742   MBB.erase_instr(MI);
00743   ++Num2Addrs;
00744   return true;
00745 }
00746 
00747 bool
00748 Thumb2SizeReduce::ReduceToNarrow(MachineBasicBlock &MBB, MachineInstr *MI,
00749                                  const ReduceEntry &Entry,
00750                                  bool LiveCPSR, bool IsSelfLoop) {
00751   if (ReduceLimit != -1 && ((int)NumNarrows >= ReduceLimit))
00752     return false;
00753 
00754   if (!MinimizeSize && !OptimizeSize && Entry.AvoidMovs &&
00755       STI->avoidMOVsShifterOperand())
00756     // Don't issue movs with shifter operand for some CPUs unless we
00757     // are optimizing / minimizing for size.
00758     return false;
00759 
00760   unsigned Limit = ~0U;
00761   if (Entry.Imm1Limit)
00762     Limit = (1 << Entry.Imm1Limit) - 1;
00763 
00764   const MCInstrDesc &MCID = MI->getDesc();
00765   for (unsigned i = 0, e = MCID.getNumOperands(); i != e; ++i) {
00766     if (MCID.OpInfo[i].isPredicate())
00767       continue;
00768     const MachineOperand &MO = MI->getOperand(i);
00769     if (MO.isReg()) {
00770       unsigned Reg = MO.getReg();
00771       if (!Reg || Reg == ARM::CPSR)
00772         continue;
00773       if (Entry.LowRegs1 && !isARMLowRegister(Reg))
00774         return false;
00775     } else if (MO.isImm() &&
00776                !MCID.OpInfo[i].isPredicate()) {
00777       if (((unsigned)MO.getImm()) > Limit)
00778         return false;
00779     }
00780   }
00781 
00782   // Check if it's possible / necessary to transfer the predicate.
00783   const MCInstrDesc &NewMCID = TII->get(Entry.NarrowOpc1);
00784   unsigned PredReg = 0;
00785   ARMCC::CondCodes Pred = getInstrPredicate(MI, PredReg);
00786   bool SkipPred = false;
00787   if (Pred != ARMCC::AL) {
00788     if (!NewMCID.isPredicable())
00789       // Can't transfer predicate, fail.
00790       return false;
00791   } else {
00792     SkipPred = !NewMCID.isPredicable();
00793   }
00794 
00795   bool HasCC = false;
00796   bool CCDead = false;
00797   if (MCID.hasOptionalDef()) {
00798     unsigned NumOps = MCID.getNumOperands();
00799     HasCC = (MI->getOperand(NumOps-1).getReg() == ARM::CPSR);
00800     if (HasCC && MI->getOperand(NumOps-1).isDead())
00801       CCDead = true;
00802   }
00803   if (!VerifyPredAndCC(MI, Entry, false, Pred, LiveCPSR, HasCC, CCDead))
00804     return false;
00805 
00806   // Avoid adding a false dependency on partial flag update by some 16-bit
00807   // instructions which has the 's' bit set.
00808   if (Entry.PartFlag && NewMCID.hasOptionalDef() && HasCC &&
00809       canAddPseudoFlagDep(MI, IsSelfLoop))
00810     return false;
00811 
00812   // Add the 16-bit instruction.
00813   DebugLoc dl = MI->getDebugLoc();
00814   MachineInstrBuilder MIB = BuildMI(MBB, MI, dl, NewMCID);
00815   MIB.addOperand(MI->getOperand(0));
00816   if (NewMCID.hasOptionalDef()) {
00817     if (HasCC)
00818       AddDefaultT1CC(MIB, CCDead);
00819     else
00820       AddNoT1CC(MIB);
00821   }
00822 
00823   // Transfer the rest of operands.
00824   unsigned NumOps = MCID.getNumOperands();
00825   for (unsigned i = 1, e = MI->getNumOperands(); i != e; ++i) {
00826     if (i < NumOps && MCID.OpInfo[i].isOptionalDef())
00827       continue;
00828     if ((MCID.getOpcode() == ARM::t2RSBSri ||
00829          MCID.getOpcode() == ARM::t2RSBri ||
00830          MCID.getOpcode() == ARM::t2SXTB ||
00831          MCID.getOpcode() == ARM::t2SXTH ||
00832          MCID.getOpcode() == ARM::t2UXTB ||
00833          MCID.getOpcode() == ARM::t2UXTH) && i == 2)
00834       // Skip the zero immediate operand, it's now implicit.
00835       continue;
00836     bool isPred = (i < NumOps && MCID.OpInfo[i].isPredicate());
00837     if (SkipPred && isPred)
00838         continue;
00839     const MachineOperand &MO = MI->getOperand(i);
00840     if (MO.isReg() && MO.isImplicit() && MO.getReg() == ARM::CPSR)
00841       // Skip implicit def of CPSR. Either it's modeled as an optional
00842       // def now or it's already an implicit def on the new instruction.
00843       continue;
00844     MIB.addOperand(MO);
00845   }
00846   if (!MCID.isPredicable() && NewMCID.isPredicable())
00847     AddDefaultPred(MIB);
00848 
00849   // Transfer MI flags.
00850   MIB.setMIFlags(MI->getFlags());
00851 
00852   DEBUG(errs() << "Converted 32-bit: " << *MI << "       to 16-bit: " << *MIB);
00853 
00854   MBB.erase_instr(MI);
00855   ++NumNarrows;
00856   return true;
00857 }
00858 
00859 static bool UpdateCPSRDef(MachineInstr &MI, bool LiveCPSR, bool &DefCPSR) {
00860   bool HasDef = false;
00861   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
00862     const MachineOperand &MO = MI.getOperand(i);
00863     if (!MO.isReg() || MO.isUndef() || MO.isUse())
00864       continue;
00865     if (MO.getReg() != ARM::CPSR)
00866       continue;
00867 
00868     DefCPSR = true;
00869     if (!MO.isDead())
00870       HasDef = true;
00871   }
00872 
00873   return HasDef || LiveCPSR;
00874 }
00875 
00876 static bool UpdateCPSRUse(MachineInstr &MI, bool LiveCPSR) {
00877   for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
00878     const MachineOperand &MO = MI.getOperand(i);
00879     if (!MO.isReg() || MO.isUndef() || MO.isDef())
00880       continue;
00881     if (MO.getReg() != ARM::CPSR)
00882       continue;
00883     assert(LiveCPSR && "CPSR liveness tracking is wrong!");
00884     if (MO.isKill()) {
00885       LiveCPSR = false;
00886       break;
00887     }
00888   }
00889 
00890   return LiveCPSR;
00891 }
00892 
00893 bool Thumb2SizeReduce::ReduceMI(MachineBasicBlock &MBB, MachineInstr *MI,
00894                                 bool LiveCPSR, bool IsSelfLoop) {
00895   unsigned Opcode = MI->getOpcode();
00896   DenseMap<unsigned, unsigned>::iterator OPI = ReduceOpcodeMap.find(Opcode);
00897   if (OPI == ReduceOpcodeMap.end())
00898     return false;
00899   const ReduceEntry &Entry = ReduceTable[OPI->second];
00900 
00901   // Don't attempt normal reductions on "special" cases for now.
00902   if (Entry.Special)
00903     return ReduceSpecial(MBB, MI, Entry, LiveCPSR, IsSelfLoop);
00904 
00905   // Try to transform to a 16-bit two-address instruction.
00906   if (Entry.NarrowOpc2 &&
00907       ReduceTo2Addr(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
00908     return true;
00909 
00910   // Try to transform to a 16-bit non-two-address instruction.
00911   if (Entry.NarrowOpc1 &&
00912       ReduceToNarrow(MBB, MI, Entry, LiveCPSR, IsSelfLoop))
00913     return true;
00914 
00915   return false;
00916 }
00917 
00918 bool Thumb2SizeReduce::ReduceMBB(MachineBasicBlock &MBB) {
00919   bool Modified = false;
00920 
00921   // Yes, CPSR could be livein.
00922   bool LiveCPSR = MBB.isLiveIn(ARM::CPSR);
00923   MachineInstr *BundleMI = 0;
00924 
00925   CPSRDef = 0;
00926   HighLatencyCPSR = false;
00927 
00928   // Check predecessors for the latest CPSRDef.
00929   for (MachineBasicBlock::pred_iterator
00930        I = MBB.pred_begin(), E = MBB.pred_end(); I != E; ++I) {
00931     const MBBInfo &PInfo = BlockInfo[(*I)->getNumber()];
00932     if (!PInfo.Visited) {
00933       // Since blocks are visited in RPO, this must be a back-edge.
00934       continue;
00935     }
00936     if (PInfo.HighLatencyCPSR) {
00937       HighLatencyCPSR = true;
00938       break;
00939     }
00940   }
00941 
00942   // If this BB loops back to itself, conservatively avoid narrowing the
00943   // first instruction that does partial flag update.
00944   bool IsSelfLoop = MBB.isSuccessor(&MBB);
00945   MachineBasicBlock::instr_iterator MII = MBB.instr_begin(),E = MBB.instr_end();
00946   MachineBasicBlock::instr_iterator NextMII;
00947   for (; MII != E; MII = NextMII) {
00948     NextMII = llvm::next(MII);
00949 
00950     MachineInstr *MI = &*MII;
00951     if (MI->isBundle()) {
00952       BundleMI = MI;
00953       continue;
00954     }
00955     if (MI->isDebugValue())
00956       continue;
00957 
00958     LiveCPSR = UpdateCPSRUse(*MI, LiveCPSR);
00959 
00960     // Does NextMII belong to the same bundle as MI?
00961     bool NextInSameBundle = NextMII != E && NextMII->isBundledWithPred();
00962 
00963     if (ReduceMI(MBB, MI, LiveCPSR, IsSelfLoop)) {
00964       Modified = true;
00965       MachineBasicBlock::instr_iterator I = prior(NextMII);
00966       MI = &*I;
00967       // Removing and reinserting the first instruction in a bundle will break
00968       // up the bundle. Fix the bundling if it was broken.
00969       if (NextInSameBundle && !NextMII->isBundledWithPred())
00970         NextMII->bundleWithPred();
00971     }
00972 
00973     if (!NextInSameBundle && MI->isInsideBundle()) {
00974       // FIXME: Since post-ra scheduler operates on bundles, the CPSR kill
00975       // marker is only on the BUNDLE instruction. Process the BUNDLE
00976       // instruction as we finish with the bundled instruction to work around
00977       // the inconsistency.
00978       if (BundleMI->killsRegister(ARM::CPSR))
00979         LiveCPSR = false;
00980       MachineOperand *MO = BundleMI->findRegisterDefOperand(ARM::CPSR);
00981       if (MO && !MO->isDead())
00982         LiveCPSR = true;
00983     }
00984 
00985     bool DefCPSR = false;
00986     LiveCPSR = UpdateCPSRDef(*MI, LiveCPSR, DefCPSR);
00987     if (MI->isCall()) {
00988       // Calls don't really set CPSR.
00989       CPSRDef = 0;
00990       HighLatencyCPSR = false;
00991       IsSelfLoop = false;
00992     } else if (DefCPSR) {
00993       // This is the last CPSR defining instruction.
00994       CPSRDef = MI;
00995       HighLatencyCPSR = isHighLatencyCPSR(CPSRDef);
00996       IsSelfLoop = false;
00997     }
00998   }
00999 
01000   MBBInfo &Info = BlockInfo[MBB.getNumber()];
01001   Info.HighLatencyCPSR = HighLatencyCPSR;
01002   Info.Visited = true;
01003   return Modified;
01004 }
01005 
01006 bool Thumb2SizeReduce::runOnMachineFunction(MachineFunction &MF) {
01007   const TargetMachine &TM = MF.getTarget();
01008   TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
01009   STI = &TM.getSubtarget<ARMSubtarget>();
01010 
01011   // Optimizing / minimizing size?
01012   AttributeSet FnAttrs = MF.getFunction()->getAttributes();
01013   OptimizeSize = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
01014                                       Attribute::OptimizeForSize);
01015   MinimizeSize = FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
01016                                       Attribute::MinSize);
01017 
01018   BlockInfo.clear();
01019   BlockInfo.resize(MF.getNumBlockIDs());
01020 
01021   // Visit blocks in reverse post-order so LastCPSRDef is known for all
01022   // predecessors.
01023   ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
01024   bool Modified = false;
01025   for (ReversePostOrderTraversal<MachineFunction*>::rpo_iterator
01026        I = RPOT.begin(), E = RPOT.end(); I != E; ++I)
01027     Modified |= ReduceMBB(**I);
01028   return Modified;
01029 }
01030 
01031 /// createThumb2SizeReductionPass - Returns an instance of the Thumb2 size
01032 /// reduction pass.
01033 FunctionPass *llvm::createThumb2SizeReductionPass() {
01034   return new Thumb2SizeReduce();
01035 }