1 //===-- Thumb2ITBlockPass.cpp - Insert Thumb IT blocks ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "thumb2-it"
12 #include "ARMMachineFunctionInfo.h"
13 #include "Thumb2InstrInfo.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/ADT/Statistic.h"
20 STATISTIC(NumITs, "Number of IT blocks inserted");
23 struct Thumb2ITBlockPass : public MachineFunctionPass {
25 Thumb2ITBlockPass() : MachineFunctionPass(&ID) {}
27 const Thumb2InstrInfo *TII;
30 virtual bool runOnMachineFunction(MachineFunction &Fn);
32 virtual const char *getPassName() const {
33 return "Thumb IT blocks insertion pass";
37 bool InsertITBlocks(MachineBasicBlock &MBB);
39 char Thumb2ITBlockPass::ID = 0;
42 static ARMCC::CondCodes getPredicate(const MachineInstr *MI, unsigned &PredReg){
43 unsigned Opc = MI->getOpcode();
44 if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
46 return llvm::getInstrPredicate(MI, PredReg);
49 bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
50 bool Modified = false;
52 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
54 MachineInstr *MI = &*MBBI;
55 DebugLoc dl = MI->getDebugLoc();
57 ARMCC::CondCodes CC = getPredicate(MI, PredReg);
59 if (CC == ARMCC::AL) {
64 // Insert an IT instruction.
65 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
70 ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
71 unsigned Mask = 0, Pos = 3;
72 // Branches, including tricky ones like LDM_RET, need to end an IT
73 // block so check the instruction we just put in the block.
74 while (MBBI != E && Pos &&
75 (!MI->getDesc().isBranch() && !MI->getDesc().isReturn())) {
76 MachineInstr *NMI = &*MBBI;
78 DebugLoc ndl = NMI->getDebugLoc();
79 unsigned NPredReg = 0;
80 ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg);
97 bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
98 const TargetMachine &TM = Fn.getTarget();
99 AFI = Fn.getInfo<ARMFunctionInfo>();
100 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
102 if (!AFI->isThumbFunction())
105 bool Modified = false;
106 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
108 MachineBasicBlock &MBB = *MFI;
109 Modified |= InsertITBlocks(MBB);
115 /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
117 FunctionPass *llvm::createThumb2ITBlockPass() {
118 return new Thumb2ITBlockPass();