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 MachineBasicBlock::iterator
39 SplitT2MOV32imm(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
40 MachineInstr *MI, DebugLoc dl,
41 unsigned PredReg, ARMCC::CondCodes CC);
42 bool InsertITBlocks(MachineBasicBlock &MBB);
44 char Thumb2ITBlockPass::ID = 0;
47 static ARMCC::CondCodes getPredicate(const MachineInstr *MI, unsigned &PredReg){
48 unsigned Opc = MI->getOpcode();
49 if (Opc == ARM::tBcc || Opc == ARM::t2Bcc)
51 return llvm::getInstrPredicate(MI, PredReg);
54 MachineBasicBlock::iterator
55 Thumb2ITBlockPass::SplitT2MOV32imm(MachineBasicBlock &MBB,
56 MachineBasicBlock::iterator MBBI,
58 DebugLoc dl, unsigned PredReg,
59 ARMCC::CondCodes CC) {
60 // Splitting t2MOVi32imm into a pair of t2MOVi16 + t2MOVTi16 here.
61 // The only reason it was a single instruction was so it could be
62 // re-materialized. We want to split it before this and the thumb2
63 // size reduction pass to make sure the IT mask is correct and expose
64 // width reduction opportunities. It doesn't make sense to do this in a
65 // separate pass so here it is.
66 unsigned DstReg = MI->getOperand(0).getReg();
67 bool DstDead = MI->getOperand(0).isDead(); // Is this possible?
68 unsigned Imm = MI->getOperand(1).getImm();
69 unsigned Lo16 = Imm & 0xffff;
70 unsigned Hi16 = (Imm >> 16) & 0xffff;
71 BuildMI(MBB, MBBI, dl, TII->get(ARM::t2MOVi16), DstReg)
72 .addImm(Lo16).addImm(CC).addReg(PredReg);
73 BuildMI(MBB, MBBI, dl, TII->get(ARM::t2MOVTi16))
74 .addReg(DstReg, getDefRegState(true) | getDeadRegState(DstDead))
75 .addReg(DstReg).addImm(Hi16).addImm(CC).addReg(PredReg);
78 MI->eraseFromParent();
82 bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
83 bool Modified = false;
85 MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
87 MachineInstr *MI = &*MBBI;
88 DebugLoc dl = MI->getDebugLoc();
90 ARMCC::CondCodes CC = getPredicate(MI, PredReg);
92 if (MI->getOpcode() == ARM::t2MOVi32imm) {
93 MBBI = SplitT2MOV32imm(MBB, MBBI, MI, dl, PredReg, CC);
97 if (CC == ARMCC::AL) {
102 // Insert an IT instruction.
103 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
108 ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
109 unsigned Mask = 0, Pos = 3;
110 while (MBBI != E && Pos) {
111 MachineInstr *NMI = &*MBBI;
112 DebugLoc ndl = NMI->getDebugLoc();
113 unsigned NPredReg = 0;
114 ARMCC::CondCodes NCC = getPredicate(NMI, NPredReg);
115 if (NMI->getOpcode() == ARM::t2MOVi32imm) {
116 MBBI = SplitT2MOV32imm(MBB, MBBI, NMI, ndl, NPredReg, NCC);
122 } else if (NCC != CC)
136 bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
137 const TargetMachine &TM = Fn.getTarget();
138 AFI = Fn.getInfo<ARMFunctionInfo>();
139 TII = static_cast<const Thumb2InstrInfo*>(TM.getInstrInfo());
141 if (!AFI->isThumbFunction())
144 bool Modified = false;
145 for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
147 MachineBasicBlock &MBB = *MFI;
148 Modified |= InsertITBlocks(MBB);
154 /// createThumb2ITBlockPass - Returns an instance of the Thumb2 IT blocks
156 FunctionPass *llvm::createThumb2ITBlockPass() {
157 return new Thumb2ITBlockPass();