This is for an experimental option -mips-os16. The idea is to compile all
authorReed Kotler <rkotler@mips.com>
Wed, 10 Apr 2013 16:58:04 +0000 (16:58 +0000)
committerReed Kotler <rkotler@mips.com>
Wed, 10 Apr 2013 16:58:04 +0000 (16:58 +0000)
Mips32 code as Mips16 unless it can't be compiled as Mips 16. For now this
would happen as long as floating point instructions are not needed.
Probably it would also make sense to compile as mips32 if atomic operations
are needed too. There may be other cases too.

A module pass prescans the IR and adds the mips16 or nomips16 attribute
to functions depending on the functions needs.

Mips 16 mode can result in a 40% code compression by utililizing 16 bit
encoding of many instructions.

The hope is for this to replace the traditional gcc way of dealing with
Mips16 code using floating point which involves essentially using soft float
but with a library implemented using mips32 floating point. This gcc
method also requires creating stubs so that Mips32 code can interact with
these Mips 16 functions that have floating point needs. My conjecture is
that in reality this traditional gcc method would never win over this
new method.

I will be implementing the traditional gcc method also. Some of it is already
done but I needed to do the stubs to finish the work and those required
this mips16/32 mixed mode capability.

I have more ideas for to make this new method much better and I think the old
method will just live in llvm for anyone that needs the backward compatibility
but I don't for what reason that would be needed.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@179185 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/Mips/CMakeLists.txt
lib/Target/Mips/MipsOs16.cpp [new file with mode: 0644]
lib/Target/Mips/MipsOs16.h [new file with mode: 0644]
lib/Target/Mips/MipsSubtarget.cpp
lib/Target/Mips/MipsSubtarget.h
lib/Target/Mips/MipsTargetMachine.cpp
test/CodeGen/Mips/fpneeded.ll [new file with mode: 0644]
test/CodeGen/Mips/fpnotneeded.ll [new file with mode: 0644]

index a75e8bd4dee17e90f889ee73a37d72e5ee963698..78a9f70c66817cf20b7ea9d11b6203c6c22927d3 100644 (file)
@@ -33,6 +33,7 @@ add_llvm_target(MipsCodeGen
   MipsMCInstLower.cpp
   MipsMachineFunction.cpp
   MipsModuleISelDAGToDAG.cpp
+  MipsOs16.cpp
   MipsRegisterInfo.cpp
   MipsSEFrameLowering.cpp
   MipsSEInstrInfo.cpp
diff --git a/lib/Target/Mips/MipsOs16.cpp b/lib/Target/Mips/MipsOs16.cpp
new file mode 100644 (file)
index 0000000..aabc466
--- /dev/null
@@ -0,0 +1,113 @@
+//===---- MipsOs16.cpp for Mips Option -Os16                         --------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines an optimization phase for the MIPS target.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "mips-os16"
+#include "MipsOs16.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace {
+
+  // Figure out if we need float point based on the function signature.
+  // We need to move variables in and/or out of floating point
+  // registers because of the ABI
+  //
+  bool needsFPFromSig(Function &F) {
+    Type* RetType = F.getReturnType();
+    switch (RetType->getTypeID()) {
+    case Type::FloatTyID:
+    case Type::DoubleTyID:
+      return true;
+    default:
+      ;
+    }
+    if (F.arg_size() >=1) {
+      Argument &Arg = F.getArgumentList().front();
+      switch (Arg.getType()->getTypeID()) {
+        case Type::FloatTyID:
+        case Type::DoubleTyID:
+          return true;
+        default:
+          ;
+      }
+    }
+    return false;
+  }
+
+  // Figure out if the function will need floating point operations
+  //
+  bool needsFP(Function &F) {
+    if (needsFPFromSig(F))
+      return true;
+    for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
+      for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
+         I != E; ++I) {
+        const Instruction &Inst = *I;
+        switch (Inst.getOpcode()) {
+        case Instruction::FAdd:
+        case Instruction::FSub:
+        case Instruction::FMul:
+        case Instruction::FDiv:
+        case Instruction::FRem:
+        case Instruction::FPToUI:
+        case Instruction::FPToSI:
+        case Instruction::UIToFP:
+        case Instruction::SIToFP:
+        case Instruction::FPTrunc:
+        case Instruction::FPExt:
+        case Instruction::FCmp:
+          return true;
+        default:
+          ;
+        }
+        if (const CallInst *CI = dyn_cast<CallInst>(I)) {
+          DEBUG(dbgs() << "Working on call" << "\n");
+          Function &F_ =  *CI->getCalledFunction();
+          if (needsFPFromSig(F_))
+            return true;
+        }
+      }
+    return false;
+  }
+}
+namespace llvm {
+
+
+bool MipsOs16::runOnModule(Module &M) {
+  DEBUG(errs() << "Run on Module MipsOs16\n");
+  bool modified = false;
+  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
+    if (F->isDeclaration()) continue;
+    DEBUG(dbgs() << "Working on " << F->getName() << "\n");
+    if (needsFP(*F)) {
+      DEBUG(dbgs() << " need to compile as nomips16 \n");
+      F->addFnAttr("nomips16");
+    }
+    else {
+      F->addFnAttr("mips16");
+      DEBUG(dbgs() << " no need to compile as nomips16 \n");
+    }
+  }
+  return modified;
+}
+
+char MipsOs16::ID = 0;
+
+}
+
+ModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) {
+  return new MipsOs16;
+}
+
+
diff --git a/lib/Target/Mips/MipsOs16.h b/lib/Target/Mips/MipsOs16.h
new file mode 100644 (file)
index 0000000..21beef8
--- /dev/null
@@ -0,0 +1,49 @@
+//===---- MipsOs16.h for Mips Option -Os16                         --------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines an optimization phase for the MIPS target.
+//
+//===----------------------------------------------------------------------===//
+
+#include "MCTargetDesc/MipsMCTargetDesc.h"
+#include "MipsTargetMachine.h"
+#include "llvm/Pass.h"
+#include "llvm/Target/TargetMachine.h"
+
+
+
+#ifndef MIPSOS16_H
+#define MIPSOS16_H
+
+using namespace llvm;
+
+namespace llvm {
+
+class MipsOs16 : public ModulePass {
+
+public:
+  static char ID;
+
+  MipsOs16() : ModulePass(ID) {
+
+  }
+
+  virtual const char *getPassName() const {
+    return "MIPS Os16 Optimization";
+  }
+
+  virtual bool runOnModule(Module &M);
+
+};
+
+ModulePass *createMipsOs16(MipsTargetMachine &TM);
+
+}
+
+#endif
index b91f5472c8ba9ee324c2d52d5ab0c804b0b3bc6e..14a2b27795122fde18fe91e2390a5f72f1b0b45e 100644 (file)
@@ -41,6 +41,13 @@ static cl::opt<bool> Mixed16_32(
            "and Mips32 code in a single source file"),
   cl::Hidden);
 
+static cl::opt<bool> Mips_Os16(
+  "mips-os16",
+  cl::init(false),
+  cl::desc("Compile all functions that don' use "
+           "floating point as Mips 16"),
+  cl::Hidden);
+
 void MipsSubtarget::anchor() { }
 
 MipsSubtarget::MipsSubtarget(const std::string &TT, const std::string &CPU,
@@ -52,7 +59,7 @@ MipsSubtarget::MipsSubtarget(const std::string &TT, const std::string &CPU,
   IsLinux(true), HasSEInReg(false), HasCondMov(false), HasSwap(false),
   HasBitCount(false), HasFPIdx(false),
   InMips16Mode(false), InMicroMipsMode(false), HasDSP(false), HasDSPR2(false),
-  AllowMixed16_32(Mixed16_32),
+  AllowMixed16_32(Mixed16_32 | Mips_Os16), Os16(Mips_Os16),
   RM(_RM), OverrideMode(NoOverride), TM(_TM)
 {
   std::string CPUName = CPU;
index 5ad627c4c1fbe071d2b7d555bd9372e9f01ec257..f2f0e15887e42307f1d8248f7dbd5f1496b424ab 100644 (file)
@@ -105,6 +105,11 @@ protected:
   // Allow mixed Mips16 and Mips32 in one source file
   bool AllowMixed16_32;
 
+  // Optimize for space by compiling all functions as Mips 16 unless
+  // it needs floating point. Functions needing floating point are
+  // compiled as Mips32
+  bool Os16;
+
   InstrItineraryData InstrItins;
 
   // The instance to the register info section object
@@ -185,6 +190,8 @@ public:
 
   bool allowMixed16_32() const { return AllowMixed16_32;};
 
+  bool os16() const { return Os16;};
+
   // Grab MipsRegInfo object
   const MipsReginfo &getMReginfo() const { return MRI; }
 
index 18c1ccedfd58d2404cf48766c65761f09659ad33..ee28e2a122ddcf842683fb44a77966c4f12256d4 100644 (file)
@@ -16,6 +16,7 @@
 #include "MipsFrameLowering.h"
 #include "MipsInstrInfo.h"
 #include "MipsModuleISelDAGToDAG.h"
+#include "MipsOs16.h"
 #include "MipsSEFrameLowering.h"
 #include "MipsSEInstrInfo.h"
 #include "MipsSEISelLowering.h"
@@ -141,6 +142,7 @@ public:
     return *getMipsTargetMachine().getSubtargetImpl();
   }
 
+  virtual void addIRPasses();
   virtual bool addInstSelector();
   virtual bool addPreEmitPass();
 };
@@ -150,6 +152,11 @@ TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
   return new MipsPassConfig(this, PM);
 }
 
+void MipsPassConfig::addIRPasses() {
+  TargetPassConfig::addIRPasses();
+  if (getMipsSubtarget().os16())
+    addPass(createMipsOs16(getMipsTargetMachine()));
+}
 // Install an instruction selector pass using
 // the ISelDag to gen Mips code.
 bool MipsPassConfig::addInstSelector() {
diff --git a/test/CodeGen/Mips/fpneeded.ll b/test/CodeGen/Mips/fpneeded.ll
new file mode 100644 (file)
index 0000000..623883a
--- /dev/null
@@ -0,0 +1,149 @@
+; RUN: llc  -march=mipsel -mcpu=mips32 -relocation-model=static -O3 < %s -mips-os16  | FileCheck %s -check-prefix=32
+
+@x = global float 1.000000e+00, align 4
+@y = global float 2.000000e+00, align 4
+@zz = common global float 0.000000e+00, align 4
+@z = common global float 0.000000e+00, align 4
+
+define float @fv() #0 {
+entry:
+  ret float 1.000000e+00
+}
+
+; 32:  .set    nomips16                  # @fv
+; 32:  .ent    fv
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    fv
+
+define double @dv() #0 {
+entry:
+  ret double 2.000000e+00
+}
+
+; 32:  .set    nomips16                  # @dv
+; 32:  .ent    dv
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    dv
+
+define void @vf(float %x) #0 {
+entry:
+  %x.addr = alloca float, align 4
+  store float %x, float* %x.addr, align 4
+  ret void
+}
+
+; 32:  .set    nomips16                  # @vf
+; 32:  .ent    vf
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    vf
+
+define void @vd(double %x) #0 {
+entry:
+  %x.addr = alloca double, align 8
+  store double %x, double* %x.addr, align 8
+  ret void
+}
+
+; 32:  .set    nomips16                  # @vd
+; 32:  .ent    vd
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    vd
+
+define void @foo1() #0 {
+entry:
+  store float 1.000000e+00, float* @zz, align 4
+  %0 = load float* @y, align 4
+  %1 = load float* @x, align 4
+  %add = fadd float %0, %1
+  store float %add, float* @z, align 4
+  ret void
+}
+
+; 32:  .set    nomips16                  # @foo1
+; 32:  .ent    foo1
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    foo1
+
+define void @foo2() #0 {
+entry:
+  %0 = load float* @x, align 4
+  call void @vf(float %0)
+  ret void
+}
+
+
+; 32:  .set    nomips16                  # @foo2
+; 32:  .ent    foo2
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    foo2
+
+define void @foo3() #0 {
+entry:
+  %call = call float @fv()
+  store float %call, float* @x, align 4
+  ret void
+}
+
+; 32:  .set    nomips16                  # @foo3
+; 32:  .ent    foo3
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    foo3
+
+attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+
+define void @vv() #0 {
+entry:
+  ret void
+}
+
+; 32:  .set    mips16                  # @vv
+; 32:  .ent    vv
+
+; 32:  save    {{.+}}
+; 32:  restore {{.+}} 
+; 32:  .end    vv
+
+
+
diff --git a/test/CodeGen/Mips/fpnotneeded.ll b/test/CodeGen/Mips/fpnotneeded.ll
new file mode 100644 (file)
index 0000000..dc2ec10
--- /dev/null
@@ -0,0 +1,77 @@
+; RUN: llc  -march=mipsel -mcpu=mips32 -relocation-model=static -O3 < %s -mips-os16  | FileCheck %s -check-prefix=32
+
+@i = global i32 1, align 4
+@f = global float 1.000000e+00, align 4
+
+define void @vv() #0 {
+entry:
+  ret void
+}
+
+; 32:  .set    mips16                  # @vv
+; 32:  .ent    vv
+
+; 32:  save    {{.+}}
+; 32:  restore {{.+}} 
+; 32:  .end    vv
+
+define i32 @iv() #0 {
+entry:
+  %0 = load i32* @i, align 4
+  ret i32 %0
+}
+
+; 32:  .set    mips16                  # @iv
+; 32:  .ent    iv
+
+; 32:  save    {{.+}}
+; 32:  restore {{.+}} 
+; 32:  .end    iv
+
+define void @vif(i32 %i, float %f) #0 {
+entry:
+  %i.addr = alloca i32, align 4
+  %f.addr = alloca float, align 4
+  store i32 %i, i32* %i.addr, align 4
+  store float %f, float* %f.addr, align 4
+  ret void
+}
+
+; 32:  .set    mips16                  # @vif
+; 32:  .ent    vif
+
+; 32:  save    {{.+}}
+; 32:  restore {{.+}} 
+; 32:  .end    vif
+
+define void @foo() #0 {
+entry:
+  store float 2.000000e+00, float* @f, align 4
+  ret void
+}
+
+; 32:  .set    mips16                  # @foo
+; 32:  .ent    foo
+
+; 32:  save    {{.+}}
+; 32:  restore {{.+}} 
+; 32:  .end    foo
+
+attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
+
+
+define float @fv() #0 {
+entry:
+  ret float 1.000000e+00
+}
+
+; 32:  .set    nomips16                  # @fv
+; 32:  .ent    fv
+; 32:  .set    noreorder
+; 32:  .set    nomacro
+; 32:  .set    noat
+; 32:  jr      $ra
+; 32:  .set    at
+; 32:  .set    macro
+; 32:  .set    reorder
+; 32:  .end    fv