LLVM API Documentation

Thumb2ITBlockPass.cpp

Go to the documentation of this file.
00001 //===-- Thumb2ITBlockPass.cpp - Insert Thumb IT blocks -----------*- 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 "thumb2-it"
00011 #include "ARM.h"
00012 #include "ARMMachineFunctionInfo.h"
00013 #include "Thumb2InstrInfo.h"
00014 #include "llvm/CodeGen/MachineInstr.h"
00015 #include "llvm/CodeGen/MachineInstrBuilder.h"
00016 #include "llvm/CodeGen/MachineFunctionPass.h"
00017 #include "llvm/Support/Compiler.h"
00018 #include "llvm/ADT/Statistic.h"
00019 using namespace llvm;
00020 
00021 STATISTIC(NumITs,     "Number of IT blocks inserted");
00022 
00023 namespace {
00024   struct VISIBILITY_HIDDEN Thumb2ITBlockPass : public MachineFunctionPass {
00025     static char ID;
00026     Thumb2ITBlockPass() : MachineFunctionPass(&ID) {}
00027 
00028     const Thumb2InstrInfo *TII;
00029     ARMFunctionInfo *AFI;
00030 
00031     virtual bool runOnMachineFunction(MachineFunction &Fn);
00032 
00033     virtual const char *getPassName() const {
00034       return "Thumb IT blocks insertion pass";
00035     }
00036 
00037   private:
00038     bool InsertITBlocks(MachineBasicBlock &MBB);
00039   };
00040   char Thumb2ITBlockPass::ID = 0;
00041 }
00042 
00043 static ARMCC::CondCodes getPredicate(const MachineInstr *MI,
00044                                      const Thumb2InstrInfo *TII) {
00045   unsigned Opc = MI->getOpcode();
00046   if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
00047     return ARMCC::AL;
00048   return TII->getPredicate(MI);
00049 }
00050 
00051 bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
00052   bool Modified = false;
00053 
00054   MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
00055   while (MBBI != E) {
00056     MachineInstr *MI = &*MBBI;
00057     ARMCC::CondCodes CC = getPredicate(MI, TII);
00058     if (CC == ARMCC::AL) {
00059       ++MBBI;
00060       continue;
00061     }
00062 
00063     // Insert an IT instruction.
00064     DebugLoc dl = MI->getDebugLoc();
00065     MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
00066       .addImm(CC);
00067     ++MBBI;
00068 
00069     // Finalize IT mask.
00070     ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
00071     unsigned Mask = 0, Pos = 3;
00072     while (MBBI != E && Pos) {
00073       ARMCC::CondCodes NCC = getPredicate(&*MBBI, TII);
00074       if (NCC == OCC) {
00075         Mask |= (1 << Pos);
00076       } else if (NCC != CC)
00077         break;
00078       --Pos;
00079       ++MBBI;
00080     }
00081     Mask |= (1 << Pos);
00082     MIB.addImm(Mask);
00083     Modified = true;
00084     ++NumITs;
00085   }
00086 
00087   return Modified;
00088 }
00089 
00090 bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
00091   const TargetMachine &TM = Fn.getTarget();
00092   AFI = Fn.getInfo<ARMFunctionInfo>();
00093   TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
00094 
00095   if (!AFI->isThumbFunction())
00096     return false;
00097 
00098   bool Modified = false;
00099   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
00100        ++MFI) {
00101     MachineBasicBlock &MBB = *MFI;
00102     Modified |= InsertITBlocks(MBB);
00103   }
00104 
00105   return Modified;
00106 }
00107 
00108 /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
00109 /// insertion pass.
00110 FunctionPass *llvm::createThumb2ITBlockPass() {
00111   return new Thumb2ITBlockPass();
00112 }