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/Support/Compiler.h"
18 #include "llvm/ADT/Statistic.h"
21 STATISTIC(NumITs, "Number of IT blocks inserted");
24 struct VISIBILITY_HIDDEN Thumb2ITBlockPass : public MachineFunctionPass {
26 Thumb2ITBlockPass() : MachineFunctionPass(&ID) {}
28 const Thumb2InstrInfo *TII;
31 virtual bool runOnMachineFunction(MachineFunction &Fn);
33 virtual const char *getPassName() const {
34 return "Thumb IT blocks insertion pass";
38 bool InsertITBlocks(MachineBasicBlock &MBB);
40 char Thumb2ITBlockPass::ID = 0;
43 static ARMCC::CondCodes getPredicate(const MachineInstr *MI,
44 const Thumb2InstrInfo *TII) {
45 unsigned Opc = MI->getOpcode();
46 if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
48 return TII->getPredicate(MI);
51 bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
52 bool Modified = false;
54 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
56 MachineInstr *MI = &*MBBI;
57 ARMCC::CondCodes CC = getPredicate(MI, TII);
58 if (CC == ARMCC::AL) {
63 // Insert an IT instruction.
64 DebugLoc dl = MI->getDebugLoc();
65 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
69 // Finalize IT mask. If the following instruction is not predicated or it's
70 // predicated on a condition that's not the same or the opposite of CC, then
72 ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
74 while (MBBI != E || (Mask & 1)) {
75 ARMCC::CondCodes NCC = getPredicate(&*MBBI, TII);
79 } else if (NCC == OCC) {
94 bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
95 const TargetMachine &TM = Fn.getTarget();
96 AFI = Fn.getInfo<ARMFunctionInfo>();
97 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
99 if (!AFI->isThumbFunction())
102 bool Modified = false;
103 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
105 MachineBasicBlock &MBB = *MFI;
106 Modified |= InsertITBlocks(MBB);
112 /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
114 FunctionPass *llvm::createThumb2ITBlockPass() {
115 return new Thumb2ITBlockPass();