1 //===---- MipsOs16.cpp for Mips Option -Os16 --------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file defines an optimization phase for the MIPS target.
11 //===----------------------------------------------------------------------===//
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 "llvm/Support/raw_ostream.h"
22 #define DEBUG_TYPE "mips-os16"
24 static cl::opt<std::string> Mips32FunctionMask(
25 "mips32-function-mask",
27 cl::desc("Force function to be mips32"),
31 class MipsOs16 : public ModulePass {
35 MipsOs16() : ModulePass(ID) {}
37 StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
39 bool runOnModule(Module &M) override;
42 char MipsOs16::ID = 0;
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
49 static bool needsFPFromSig(Function &F) {
50 Type* RetType = F.getReturnType();
51 switch (RetType->getTypeID()) {
53 case Type::DoubleTyID:
58 if (F.arg_size() >=1) {
59 Argument &Arg = *F.arg_begin();
60 switch (Arg.getType()->getTypeID()) {
62 case Type::DoubleTyID:
71 // Figure out if the function will need floating point operations
73 static bool needsFP(Function &F) {
74 if (needsFPFromSig(F))
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();
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:
97 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
98 LLVM_DEBUG(dbgs() << "Working on call"
100 Function &F_ = *CI->getCalledFunction();
101 if (needsFPFromSig(F_))
109 bool MipsOs16::runOnModule(Module &M) {
110 bool usingMask = Mips32FunctionMask.length() > 0;
111 bool doneUsingMask = false; // this will make it stop repeating
113 LLVM_DEBUG(dbgs() << "Run on Module MipsOs16 \n"
114 << Mips32FunctionMask << "\n");
116 LLVM_DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
118 unsigned int functionIndex = 0;
119 bool modified = false;
122 if (F.isDeclaration())
125 LLVM_DEBUG(dbgs() << "Working on " << F.getName() << "\n");
127 if (!doneUsingMask) {
128 if (functionIndex == Mips32FunctionMask.length())
130 switch (Mips32FunctionMask[functionIndex]) {
132 LLVM_DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
133 F.addFnAttr("nomips16");
136 doneUsingMask = true;
146 LLVM_DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
147 F.addFnAttr("nomips16");
150 LLVM_DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
151 F.addFnAttr("mips16");
159 ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); }