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"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
23 #define DEBUG_TYPE "mips-os16"
25 static cl::opt<std::string> Mips32FunctionMask(
26 "mips32-function-mask",
28 cl::desc("Force function to be mips32"),
32 class MipsOs16 : public ModulePass {
36 MipsOs16() : ModulePass(ID) {}
38 const char *getPassName() const override {
39 return "MIPS Os16 Optimization";
42 bool runOnModule(Module &M) override;
45 char MipsOs16::ID = 0;
48 // Figure out if we need float point based on the function signature.
49 // We need to move variables in and/or out of floating point
50 // registers because of the ABI
52 static bool needsFPFromSig(Function &F) {
53 Type* RetType = F.getReturnType();
54 switch (RetType->getTypeID()) {
56 case Type::DoubleTyID:
61 if (F.arg_size() >=1) {
62 Argument &Arg = F.getArgumentList().front();
63 switch (Arg.getType()->getTypeID()) {
65 case Type::DoubleTyID:
74 // Figure out if the function will need floating point operations
76 static bool needsFP(Function &F) {
77 if (needsFPFromSig(F))
79 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
80 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
82 const Instruction &Inst = *I;
83 switch (Inst.getOpcode()) {
84 case Instruction::FAdd:
85 case Instruction::FSub:
86 case Instruction::FMul:
87 case Instruction::FDiv:
88 case Instruction::FRem:
89 case Instruction::FPToUI:
90 case Instruction::FPToSI:
91 case Instruction::UIToFP:
92 case Instruction::SIToFP:
93 case Instruction::FPTrunc:
94 case Instruction::FPExt:
95 case Instruction::FCmp:
100 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
101 DEBUG(dbgs() << "Working on call" << "\n");
102 Function &F_ = *CI->getCalledFunction();
103 if (needsFPFromSig(F_))
111 bool MipsOs16::runOnModule(Module &M) {
112 bool usingMask = Mips32FunctionMask.length() > 0;
113 bool doneUsingMask = false; // this will make it stop repeating
114 DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
116 DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
117 unsigned int functionIndex = 0;
118 bool modified = false;
119 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
120 if (F->isDeclaration()) continue;
121 DEBUG(dbgs() << "Working on " << F->getName() << "\n");
123 if (!doneUsingMask) {
124 if (functionIndex == Mips32FunctionMask.length())
126 switch (Mips32FunctionMask[functionIndex]) {
128 DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
129 F->addFnAttr("nomips16");
132 doneUsingMask = true;
142 DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
143 F->addFnAttr("nomips16");
146 DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
147 F->addFnAttr("mips16");
154 ModulePass *llvm::createMipsOs16Pass(MipsTargetMachine &TM) {