Add a thumb2 pass to insert IT blocks.
authorEvan Cheng <evan.cheng@apple.com>
Fri, 10 Jul 2009 01:54:42 +0000 (01:54 +0000)
committerEvan Cheng <evan.cheng@apple.com>
Fri, 10 Jul 2009 01:54:42 +0000 (01:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75218 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/ARM/ARM.h
lib/Target/ARM/ARMInstrThumb.td
lib/Target/ARM/ARMInstrThumb2.td
lib/Target/ARM/ARMTargetMachine.cpp
lib/Target/ARM/AsmPrinter/ARMAsmPrinter.cpp
lib/Target/ARM/Thumb2ITBlockPass.cpp [new file with mode: 0644]
test/CodeGen/Thumb2/thumb2-select.ll
test/CodeGen/Thumb2/thumb2-select_xform.ll

index 12c26e8c7955f42bcbcc8df82b9ea5c75ee300a9..0e654d8e64d61c98cb5b219afa041b0ea772c449 100644 (file)
@@ -108,6 +108,8 @@ FunctionPass *createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM,
 FunctionPass *createARMLoadStoreOptimizationPass(bool PreAlloc = false);
 FunctionPass *createARMConstantIslandPass();
 
+FunctionPass *createThumb2ITBlockPass();
+
 } // end namespace llvm;
 
 // Defines symbolic names for ARM registers.  This defines a mapping from
index 5642710b5caab9a03ed226379cd3d66d62274ddf..fb7453a912b30cb4a4fa2cca3ffb5070c96def73 100644 (file)
@@ -66,11 +66,6 @@ def thumb_immshifted_shamt : SDNodeXForm<imm, [{
   return CurDAG->getTargetConstant(V, MVT::i32);
 }]>;
 
-// IT block condition mask
-def it_mask : Operand<i32> {
-  let PrintMethod = "printThumbITMask";
-}
-
 // Define Thumb specific addressing modes.
 
 // t_addrmode_rr := reg + reg
@@ -212,10 +207,6 @@ let isBranch = 1, isTerminator = 1 in
   def tBcc : T1I<(outs), (ins brtarget:$target, pred:$cc), "b$cc $target",
                  [/*(ARMbrcond bb:$target, imm:$cc)*/]>;
 
-// IT block
-def tIT : TI<(outs), (ins pred:$cc, it_mask:$mask),
-             "it$mask $cc", []>;
-
 //===----------------------------------------------------------------------===//
 //  Load Store Instructions.
 //
index be738310729b93c2139470d931b03607ee4b5801..34ce53f97f13127165614c0ca651f3cfc247efa1 100644 (file)
 //
 //===----------------------------------------------------------------------===//
 
+// IT block predicate field
+def it_pred : Operand<i32> {
+  let PrintMethod = "printPredicateOperand";
+}
+
+// IT block condition mask
+def it_mask : Operand<i32> {
+  let PrintMethod = "printThumbITMask";
+}
+
 // Shifted operands. No register controlled shifts for Thumb2.
 // Note: We do not support rrx shifted operands yet.
 def t2_so_reg : Operand<i32>,    // reg imm
@@ -1121,6 +1131,12 @@ def t2Bcc : T2I<(outs), (ins brtarget:$target),
                 "b", " $target",
                 [/*(ARMbrcond bb:$target, imm:$cc)*/]>;
 
+
+// IT block
+def t2IT : Thumb2XI<(outs), (ins it_pred:$cc, it_mask:$mask),
+                    AddrModeNone, Size2Bytes,
+                    "it$mask $cc", "", []>;
+
 //===----------------------------------------------------------------------===//
 // Non-Instruction Patterns
 //
index b3faadafc5a6fdaa9ed01e1ca720eb2ae2cdaef3..a827784383336e1119913f10bf2eaae5ba1b10d4 100644 (file)
@@ -173,6 +173,9 @@ bool ARMBaseTargetMachine::addPreEmitPass(PassManagerBase &PM,
       !DisableIfConversion && !Subtarget.isThumb())
     PM.add(createIfConverterPass());
 
+  if (Subtarget.isThumb2())
+    PM.add(createThumb2ITBlockPass());
+
   PM.add(createARMConstantIslandPass());
   return true;
 }
index b298ff7715c78b387d1a770c1fa6ad56da66c06b..3b1c491ef1a2d4bee5f573411d5f20e297dabac5 100644 (file)
@@ -642,7 +642,7 @@ ARMAsmPrinter::printThumbITMask(const MachineInstr *MI, int Op) {
   unsigned Mask = MI->getOperand(Op).getImm();
   unsigned NumTZ = CountTrailingZeros_32(Mask);
   assert(NumTZ <= 3 && "Invalid IT mask!");
-  for (unsigned Pos = 3, e = NumTZ; Pos >= e; --Pos) {
+  for (unsigned Pos = 3, e = NumTZ; Pos > e; --Pos) {
     bool T = (Mask & (1 << Pos)) != 0;
     if (T)
       O << 't';
diff --git a/lib/Target/ARM/Thumb2ITBlockPass.cpp b/lib/Target/ARM/Thumb2ITBlockPass.cpp
new file mode 100644 (file)
index 0000000..f6f75a1
--- /dev/null
@@ -0,0 +1,108 @@
+//===-- Thumb2ITBlockPass.cpp - Insert Thumb IT blocks -----------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "thumb2-it"
+#include "ARM.h"
+#include "ARMInstrInfo.h"
+#include "ARMMachineFunctionInfo.h"
+#include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/ADT/Statistic.h"
+using namespace llvm;
+
+STATISTIC(NumITs,     "Number of IT blocks inserted");
+
+namespace {
+  struct VISIBILITY_HIDDEN Thumb2ITBlockPass : public MachineFunctionPass {
+    static char ID;
+    Thumb2ITBlockPass() : MachineFunctionPass(&ID) {}
+
+    const ARMBaseInstrInfo *TII;
+    ARMFunctionInfo *AFI;
+
+    virtual bool runOnMachineFunction(MachineFunction &Fn);
+
+    virtual const char *getPassName() const {
+      return "Thumb IT blocks insertion pass";
+    }
+
+  private:
+    bool InsertITBlocks(MachineBasicBlock &MBB);
+  };
+  char Thumb2ITBlockPass::ID = 0;
+}
+
+bool Thumb2ITBlockPass::InsertITBlocks(MachineBasicBlock &MBB) {
+  bool Modified = false;
+
+  MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
+  while (MBBI != E) {
+    MachineInstr *MI = &*MBBI;
+    ARMCC::CondCodes CC = TII->getPredicate(MI);
+    if (CC == ARMCC::AL) {
+      ++MBBI;
+      continue;
+    }
+
+    // Insert an IT instruction.
+    DebugLoc dl = MI->getDebugLoc();
+    MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII->get(ARM::t2IT))
+      .addImm(CC);
+    ++MBBI;
+
+    // Finalize IT mask. If the following instruction is not predicated or it's
+    // predicated on a condition that's not the same or the opposite of CC, then
+    // the mask is 0x8.
+    ARMCC::CondCodes OCC = ARMCC::getOppositeCondition(CC);
+    unsigned Mask = 0x8;
+    while (MBBI != E || (Mask & 1)) {
+      ARMCC::CondCodes NCC = TII->getPredicate(&*MBBI);
+      if (NCC == CC) {
+        Mask >>= 1;
+        Mask |= 0x8;
+      } else if (NCC == OCC) {
+        Mask >>= 1;
+      } else {
+        break;
+      }
+      ++MBBI;
+    }
+    MIB.addImm(Mask);
+    Modified = true;
+    ++NumITs;
+  }
+
+  return Modified;
+}
+
+bool Thumb2ITBlockPass::runOnMachineFunction(MachineFunction &Fn) {
+  const TargetMachine &TM = Fn.getTarget();
+  AFI = Fn.getInfo<ARMFunctionInfo>();
+  TII = static_cast<const ARMBaseInstrInfo*>(TM.getInstrInfo());
+
+  if (!AFI->isThumbFunction())
+    return false;
+
+  bool Modified = false;
+  for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
+       ++MFI) {
+    MachineBasicBlock &MBB = *MFI;
+    Modified |= InsertITBlocks(MBB);
+  }
+
+  return Modified;
+}
+
+/// createThumb2ITBlockPass - returns and instance of the Thumb IT blocks
+/// insertion pass.
+FunctionPass *llvm::createThumb2ITBlockPass() {
+  return new Thumb2ITBlockPass();
+}
index 57f61569c190f2626b65f3d062cd36ead792a071..1d51bc395612d444bc8534903e98f6c205a0e1c6 100644 (file)
@@ -4,6 +4,7 @@
 ; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep movle | count 1
 ; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep movls | count 1
 ; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep movhi | count 1
+; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep it    | count 6
 
 define i32 @f1(i32 %a.s) {
 entry:
index 9da4ebe17d2eaaa5b579f6bc4619d77eaf84da1a..5332c2d6e9ba22a1599325db436809be9b5e9b12 100644 (file)
@@ -1,5 +1,6 @@
 ; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep mov | count 3
 ; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep mvn | count 1
+; RUN: llvm-as < %s | llc -march=thumb -mattr=+thumb2 | grep it  | count 3
 
 define i32 @t1(i32 %a, i32 %b, i32 %c) nounwind {
         %tmp1 = icmp sgt i32 %c, 10