Move unreferenced passes into the cpp file
[oota-llvm.git] / lib / Target / Mips / MipsOs16.cpp
1 //===---- MipsOs16.cpp for Mips Option -Os16                       --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an optimization phase for the MIPS target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "MipsOs16.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 #define DEBUG_TYPE "mips-os16"
23
24 static cl::opt<std::string> Mips32FunctionMask(
25   "mips32-function-mask",
26   cl::init(""),
27   cl::desc("Force function to be mips32"),
28   cl::Hidden);
29
30 namespace {
31
32   // Figure out if we need float point based on the function signature.
33   // We need to move variables in and/or out of floating point
34   // registers because of the ABI
35   //
36   bool needsFPFromSig(Function &F) {
37     Type* RetType = F.getReturnType();
38     switch (RetType->getTypeID()) {
39     case Type::FloatTyID:
40     case Type::DoubleTyID:
41       return true;
42     default:
43       ;
44     }
45     if (F.arg_size() >=1) {
46       Argument &Arg = F.getArgumentList().front();
47       switch (Arg.getType()->getTypeID()) {
48         case Type::FloatTyID:
49         case Type::DoubleTyID:
50           return true;
51         default:
52           ;
53       }
54     }
55     return false;
56   }
57
58   // Figure out if the function will need floating point operations
59   //
60   bool needsFP(Function &F) {
61     if (needsFPFromSig(F))
62       return true;
63     for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
64       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
65          I != E; ++I) {
66         const Instruction &Inst = *I;
67         switch (Inst.getOpcode()) {
68         case Instruction::FAdd:
69         case Instruction::FSub:
70         case Instruction::FMul:
71         case Instruction::FDiv:
72         case Instruction::FRem:
73         case Instruction::FPToUI:
74         case Instruction::FPToSI:
75         case Instruction::UIToFP:
76         case Instruction::SIToFP:
77         case Instruction::FPTrunc:
78         case Instruction::FPExt:
79         case Instruction::FCmp:
80           return true;
81         default:
82           ;
83         }
84         if (const CallInst *CI = dyn_cast<CallInst>(I)) {
85           DEBUG(dbgs() << "Working on call" << "\n");
86           Function &F_ =  *CI->getCalledFunction();
87           if (needsFPFromSig(F_))
88             return true;
89         }
90       }
91     return false;
92   }
93 }
94
95 namespace {
96
97 class MipsOs16 : public ModulePass {
98 public:
99   static char ID;
100
101   MipsOs16() : ModulePass(ID) {}
102
103   const char *getPassName() const override { return "MIPS Os16 Optimization"; }
104   bool runOnModule(Module &M) override;
105 };
106 } // namespace
107
108 bool MipsOs16::runOnModule(Module &M) {
109   bool usingMask = Mips32FunctionMask.length() > 0;
110   bool doneUsingMask = false; // this will make it stop repeating
111   DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
112   if (usingMask)
113     DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
114   unsigned int functionIndex = 0;
115   bool modified = false;
116   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
117     if (F->isDeclaration()) continue;
118     DEBUG(dbgs() << "Working on " << F->getName() << "\n");
119     if (usingMask) {
120       if (!doneUsingMask) {
121         if (functionIndex == Mips32FunctionMask.length())
122           functionIndex = 0;
123         switch (Mips32FunctionMask[functionIndex]) {
124         case '1':
125           DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
126           F->addFnAttr("nomips16");
127           break;
128         case '.':
129           doneUsingMask = true;
130           break;
131         default:
132           break;
133         }
134         functionIndex++;
135       }
136     }
137     else {
138       if (needsFP(*F)) {
139         DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
140         F->addFnAttr("nomips16");
141       }
142       else {
143         DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
144         F->addFnAttr("mips16");
145       }
146     }
147   }
148   return modified;
149 }
150
151 char MipsOs16::ID = 0;
152
153 ModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) {
154   return new MipsOs16;
155 }