blob: ac4e55f8a1f58cb2adf6da680dedb58dba08b89b [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Reed Kotlerfe94cc32013-04-10 16:58:04 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an optimization phase for the MIPS target.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramer799003b2015-03-23 19:32:43 +000013#include "Mips.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/IR/Instructions.h"
Reed Kotlerfe94cc32013-04-10 16:58:04 +000015#include "llvm/IR/Module.h"
Reed Kotlerd8f33622013-08-20 20:53:09 +000016#include "llvm/Support/CommandLine.h"
Reed Kotlerfe94cc32013-04-10 16:58:04 +000017#include "llvm/Support/Debug.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000018#include "llvm/Support/raw_ostream.h"
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000019
Benjamin Kramera52f6962015-03-09 15:50:58 +000020using namespace llvm;
Reed Kotlerfe94cc32013-04-10 16:58:04 +000021
Chandler Carruth84e68b22014-04-22 02:41:26 +000022#define DEBUG_TYPE "mips-os16"
23
Reed Kotlerd8f33622013-08-20 20:53:09 +000024static cl::opt<std::string> Mips32FunctionMask(
25 "mips32-function-mask",
26 cl::init(""),
27 cl::desc("Force function to be mips32"),
28 cl::Hidden);
29
Reed Kotlerfe94cc32013-04-10 16:58:04 +000030namespace {
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000031 class MipsOs16 : public ModulePass {
32 public:
33 static char ID;
Reed Kotlerfe94cc32013-04-10 16:58:04 +000034
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000035 MipsOs16() : ModulePass(ID) {}
36
Mehdi Amini117296c2016-10-01 02:56:57 +000037 StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000038
39 bool runOnModule(Module &M) override;
40 };
41
42 char MipsOs16::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000043}
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000044
45// Figure out if we need float point based on the function signature.
46// We need to move variables in and/or out of floating point
47// registers because of the ABI
48//
49static bool needsFPFromSig(Function &F) {
50 Type* RetType = F.getReturnType();
51 switch (RetType->getTypeID()) {
52 case Type::FloatTyID:
53 case Type::DoubleTyID:
54 return true;
55 default:
56 ;
57 }
58 if (F.arg_size() >=1) {
Reid Kleckner45707d42017-03-16 22:59:15 +000059 Argument &Arg = *F.arg_begin();
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000060 switch (Arg.getType()->getTypeID()) {
Reed Kotlerfe94cc32013-04-10 16:58:04 +000061 case Type::FloatTyID:
62 case Type::DoubleTyID:
63 return true;
64 default:
65 ;
66 }
Reed Kotlerfe94cc32013-04-10 16:58:04 +000067 }
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000068 return false;
Reed Kotlerfe94cc32013-04-10 16:58:04 +000069}
Reed Kotlerfe94cc32013-04-10 16:58:04 +000070
Vasileios Kalintiris6312f512015-03-14 08:34:25 +000071// Figure out if the function will need floating point operations
72//
73static bool needsFP(Function &F) {
74 if (needsFPFromSig(F))
75 return true;
76 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
77 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
78 I != E; ++I) {
79 const Instruction &Inst = *I;
80 switch (Inst.getOpcode()) {
81 case Instruction::FAdd:
82 case Instruction::FSub:
83 case Instruction::FMul:
84 case Instruction::FDiv:
85 case Instruction::FRem:
86 case Instruction::FPToUI:
87 case Instruction::FPToSI:
88 case Instruction::UIToFP:
89 case Instruction::SIToFP:
90 case Instruction::FPTrunc:
91 case Instruction::FPExt:
92 case Instruction::FCmp:
93 return true;
94 default:
95 ;
96 }
97 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000098 LLVM_DEBUG(dbgs() << "Working on call"
99 << "\n");
Vasileios Kalintiris6312f512015-03-14 08:34:25 +0000100 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
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000113 LLVM_DEBUG(dbgs() << "Run on Module MipsOs16 \n"
114 << Mips32FunctionMask << "\n");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000115 if (usingMask)
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000116 LLVM_DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000117
Reed Kotlerd8f33622013-08-20 20:53:09 +0000118 unsigned int functionIndex = 0;
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000119 bool modified = false;
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000120
121 for (auto &F : M) {
122 if (F.isDeclaration())
123 continue;
124
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000125 LLVM_DEBUG(dbgs() << "Working on " << F.getName() << "\n");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000126 if (usingMask) {
Reed Kotlere883f502013-09-23 22:36:11 +0000127 if (!doneUsingMask) {
128 if (functionIndex == Mips32FunctionMask.length())
129 functionIndex = 0;
130 switch (Mips32FunctionMask[functionIndex]) {
131 case '1':
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000132 LLVM_DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000133 F.addFnAttr("nomips16");
Reed Kotlere883f502013-09-23 22:36:11 +0000134 break;
135 case '.':
136 doneUsingMask = true;
137 break;
138 default:
139 break;
140 }
141 functionIndex++;
Reed Kotlerd8f33622013-08-20 20:53:09 +0000142 }
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000143 }
144 else {
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000145 if (needsFP(F)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000146 LLVM_DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000147 F.addFnAttr("nomips16");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000148 }
149 else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000150 LLVM_DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000151 F.addFnAttr("mips16");
Reed Kotlerd8f33622013-08-20 20:53:09 +0000152 }
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000153 }
154 }
Vasileios Kalintiris957d8492016-04-08 10:33:00 +0000155
Reed Kotlerfe94cc32013-04-10 16:58:04 +0000156 return modified;
157}
158
Francis Visoiu Mistrih8b617642017-05-18 17:21:13 +0000159ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); }