1 //===---- MipsOs16.cpp for Mips Option -Os16 --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an optimization phase for the MIPS target.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Debug.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 const char *getPassName() const override {
38 return "MIPS Os16 Optimization";
41 bool runOnModule(Module &M) override;
44 char MipsOs16::ID = 0;
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
51 static bool needsFPFromSig(Function &F) {
52 Type* RetType = F.getReturnType();
53 switch (RetType->getTypeID()) {
55 case Type::DoubleTyID:
60 if (F.arg_size() >=1) {
61 Argument &Arg = F.getArgumentList().front();
62 switch (Arg.getType()->getTypeID()) {
64 case Type::DoubleTyID:
73 // Figure out if the function will need floating point operations
75 static bool needsFP(Function &F) {
76 if (needsFPFromSig(F))
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();
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:
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_))
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");
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");
122 if (!doneUsingMask) {
123 if (functionIndex == Mips32FunctionMask.length())
125 switch (Mips32FunctionMask[functionIndex]) {
127 DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
128 F->addFnAttr("nomips16");
131 doneUsingMask = true;
141 DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
142 F->addFnAttr("nomips16");
145 DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
146 F->addFnAttr("mips16");
153 ModulePass *llvm::createMipsOs16Pass(MipsTargetMachine &TM) {