blob: 7ee45c28a7d09db8b4b69fa2b2564e1ada65fcd5 [file] [log] [blame]
Akira Hatanakad8fb0322013-04-22 20:13:37 +00001//===---- MipsOs16.cpp for Mips Option -Os16 --------===//
Reed Kotlerfe94cc32013-04-10 16:58:04 +00002//
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
Benjamin Kramer799003b2015-03-23 19:32:43 +000014#include "Mips.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/Instructions.h"
Reed Kotlerfe94cc32013-04-10 16:58:04 +000016#include "llvm/IR/Module.h"
Reed Kotlerd8f33622013-08-20 20:53:09 +000017#include "llvm/Support/CommandLine.h"
Reed Kotlerfe94cc32013-04-10 16:58:04 +000018#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000019#include "llvm/Support/raw_ostream.h"
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000020
Benjamin Kramera52f6962015-03-09 15:50:58 +000021using namespace llvm;
Reed Kotlerfe94cc32013-04-10 16:58:04 +000022
Chandler Carruth84e68b22014-04-22 02:41:26 +000023#define DEBUG_TYPE "mips-os16"
24
Reed Kotlerd8f33622013-08-20 20:53:09 +000025static cl::opt<std::string> Mips32FunctionMask(
26 "mips32-function-mask",
27 cl::init(""),
28 cl::desc("Force function to be mips32"),
29 cl::Hidden);
30
Reed Kotlerfe94cc32013-04-10 16:58:04 +000031namespace {
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000032 class MipsOs16 : public ModulePass {
33 public:
34 static char ID;
Reed Kotlerfe94cc32013-04-10 16:58:04 +000035
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000036 MipsOs16() : ModulePass(ID) {}
37
Mehdi Amini117296c2016-10-01 02:56:57 +000038 StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000039
40 bool runOnModule(Module &M) override;
41 };
42
43 char MipsOs16::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000044}
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000045
46// Figure out if we need float point based on the function signature.
47// We need to move variables in and/or out of floating point
48// registers because of the ABI
49//
50static bool needsFPFromSig(Function &F) {
51 Type* RetType = F.getReturnType();
52 switch (RetType->getTypeID()) {
53 case Type::FloatTyID:
54 case Type::DoubleTyID:
55 return true;
56 default:
57 ;
58 }
59 if (F.arg_size() >=1) {
Reid Kleckner45707d42017-03-16 22:59:15 +000060 Argument &Arg = *F.arg_begin();
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000061 switch (Arg.getType()->getTypeID()) {
Reed Kotlerfe94cc32013-04-10 16:58:04 +000062 case Type::FloatTyID:
63 case Type::DoubleTyID:
64 return true;
65 default:
66 ;
67 }
Reed Kotlerfe94cc32013-04-10 16:58:04 +000068 }
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000069 return false;
Reed Kotlerfe94cc32013-04-10 16:58:04 +000070}
Reed Kotlerfe94cc32013-04-10 16:58:04 +000071
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000072// Figure out if the function will need floating point operations
73//
74static bool needsFP(Function &F) {
75 if (needsFPFromSig(F))
76 return true;
77 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
79 I != E; ++I) {
80 const Instruction &Inst = *I;
81 switch (Inst.getOpcode()) {
82 case Instruction::FAdd:
83 case Instruction::FSub:
84 case Instruction::FMul:
85 case Instruction::FDiv:
86 case Instruction::FRem:
87 case Instruction::FPToUI:
88 case Instruction::FPToSI:
89 case Instruction::UIToFP:
90 case Instruction::SIToFP:
91 case Instruction::FPTrunc:
92 case Instruction::FPExt:
93 case Instruction::FCmp:
94 return true;
95 default:
96 ;
97 }
98 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
99 DEBUG(dbgs() << "Working on call" << "\n");
100 Function &F_ = *CI->getCalledFunction();
101 if (needsFPFromSig(F_))
102 return true;
103 }
104 }
105 return false;
106}
Benjamin Kramera52f6962015-03-09 15:50:58 +0000107
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000108
109bool MipsOs16::runOnModule(Module &M) {
Reed Kotlerd8f33622013-08-20 20:53:09 +0000110 bool usingMask = Mips32FunctionMask.length() > 0;
Reed Kotlere883f502013-09-23 22:36:11 +0000111 bool doneUsingMask = false; // this will make it stop repeating
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000112
Reed Kotlerd8f33622013-08-20 20:53:09 +0000113 DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
114 if (usingMask)
115 DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000116
Reed Kotlerd8f33622013-08-20 20:53:09 +0000117 unsigned int functionIndex = 0;
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000118 bool modified = false;
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000119
120 for (auto &F : M) {
121 if (F.isDeclaration())
122 continue;
123
124 DEBUG(dbgs() << "Working on " << F.getName() << "\n");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000125 if (usingMask) {
Reed Kotlere883f502013-09-23 22:36:11 +0000126 if (!doneUsingMask) {
127 if (functionIndex == Mips32FunctionMask.length())
128 functionIndex = 0;
129 switch (Mips32FunctionMask[functionIndex]) {
130 case '1':
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000131 DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
132 F.addFnAttr("nomips16");
Reed Kotlere883f502013-09-23 22:36:11 +0000133 break;
134 case '.':
135 doneUsingMask = true;
136 break;
137 default:
138 break;
139 }
140 functionIndex++;
Reed Kotlerd8f33622013-08-20 20:53:09 +0000141 }
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000142 }
143 else {
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000144 if (needsFP(F)) {
145 DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
146 F.addFnAttr("nomips16");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000147 }
148 else {
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000149 DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
150 F.addFnAttr("mips16");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000151 }
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000152 }
153 }
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000154
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000155 return modified;
156}
157
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000158ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); }