[mips] Add itineraries for ext and ins instructions.
[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 "llvm/IR/Instructions.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Debug.h"
18 #include "Mips.h"
19
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   class MipsOs16 : public ModulePass {
32   public:
33     static char ID;
34
35     MipsOs16() : ModulePass(ID) {}
36
37     const char *getPassName() const override {
38       return "MIPS Os16 Optimization";
39     }
40
41     bool runOnModule(Module &M) override;
42   };
43
44   char MipsOs16::ID = 0;
45 }
46
47 // Figure out if we need float point based on the function signature.
48 // We need to move variables in and/or out of floating point
49 // registers because of the ABI
50 //
51 static  bool needsFPFromSig(Function &F) {
52   Type* RetType = F.getReturnType();
53   switch (RetType->getTypeID()) {
54   case Type::FloatTyID:
55   case Type::DoubleTyID:
56     return true;
57   default:
58     ;
59   }
60   if (F.arg_size() >=1) {
61     Argument &Arg = F.getArgumentList().front();
62     switch (Arg.getType()->getTypeID()) {
63     case Type::FloatTyID:
64     case Type::DoubleTyID:
65       return true;
66     default:
67       ;
68     }
69   }
70   return false;
71 }
72
73 // Figure out if the function will need floating point operations
74 //
75 static bool needsFP(Function &F) {
76   if (needsFPFromSig(F))
77     return true;
78   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
79     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
80          I != E; ++I) {
81       const Instruction &Inst = *I;
82       switch (Inst.getOpcode()) {
83       case Instruction::FAdd:
84       case Instruction::FSub:
85       case Instruction::FMul:
86       case Instruction::FDiv:
87       case Instruction::FRem:
88       case Instruction::FPToUI:
89       case Instruction::FPToSI:
90       case Instruction::UIToFP:
91       case Instruction::SIToFP:
92       case Instruction::FPTrunc:
93       case Instruction::FPExt:
94       case Instruction::FCmp:
95         return true;
96       default:
97         ;
98       }
99       if (const CallInst *CI = dyn_cast<CallInst>(I)) {
100         DEBUG(dbgs() << "Working on call" << "\n");
101         Function &F_ =  *CI->getCalledFunction();
102         if (needsFPFromSig(F_))
103           return true;
104       }
105     }
106   return false;
107 }
108
109
110 bool MipsOs16::runOnModule(Module &M) {
111   bool usingMask = Mips32FunctionMask.length() > 0;
112   bool doneUsingMask = false; // this will make it stop repeating
113   DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
114   if (usingMask)
115     DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
116   unsigned int functionIndex = 0;
117   bool modified = false;
118   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
119     if (F->isDeclaration()) continue;
120     DEBUG(dbgs() << "Working on " << F->getName() << "\n");
121     if (usingMask) {
122       if (!doneUsingMask) {
123         if (functionIndex == Mips32FunctionMask.length())
124           functionIndex = 0;
125         switch (Mips32FunctionMask[functionIndex]) {
126         case '1':
127           DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
128           F->addFnAttr("nomips16");
129           break;
130         case '.':
131           doneUsingMask = true;
132           break;
133         default:
134           break;
135         }
136         functionIndex++;
137       }
138     }
139     else {
140       if (needsFP(*F)) {
141         DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
142         F->addFnAttr("nomips16");
143       }
144       else {
145         DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
146         F->addFnAttr("mips16");
147       }
148     }
149   }
150   return modified;
151 }
152
153 ModulePass *llvm::createMipsOs16Pass(MipsTargetMachine &TM) {
154   return new MipsOs16;
155 }