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 #define DEBUG_TYPE "mips-os16"
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"
22 static cl::opt<std::string> Mips32FunctionMask(
23 "mips32-function-mask",
25 cl::desc("Force function to be mips32"),
30 // Figure out if we need float point based on the function signature.
31 // We need to move variables in and/or out of floating point
32 // registers because of the ABI
34 bool needsFPFromSig(Function &F) {
35 Type* RetType = F.getReturnType();
36 switch (RetType->getTypeID()) {
38 case Type::DoubleTyID:
43 if (F.arg_size() >=1) {
44 Argument &Arg = F.getArgumentList().front();
45 switch (Arg.getType()->getTypeID()) {
47 case Type::DoubleTyID:
56 // Figure out if the function will need floating point operations
58 bool needsFP(Function &F) {
59 if (needsFPFromSig(F))
61 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
62 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
64 const Instruction &Inst = *I;
65 switch (Inst.getOpcode()) {
66 case Instruction::FAdd:
67 case Instruction::FSub:
68 case Instruction::FMul:
69 case Instruction::FDiv:
70 case Instruction::FRem:
71 case Instruction::FPToUI:
72 case Instruction::FPToSI:
73 case Instruction::UIToFP:
74 case Instruction::SIToFP:
75 case Instruction::FPTrunc:
76 case Instruction::FPExt:
77 case Instruction::FCmp:
82 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
83 DEBUG(dbgs() << "Working on call" << "\n");
84 Function &F_ = *CI->getCalledFunction();
85 if (needsFPFromSig(F_))
95 bool MipsOs16::runOnModule(Module &M) {
96 bool usingMask = Mips32FunctionMask.length() > 0;
97 bool doneUsingMask = false; // this will make it stop repeating
98 DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
100 DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
101 unsigned int functionIndex = 0;
102 bool modified = false;
103 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
104 if (F->isDeclaration()) continue;
105 DEBUG(dbgs() << "Working on " << F->getName() << "\n");
107 if (!doneUsingMask) {
108 if (functionIndex == Mips32FunctionMask.length())
110 switch (Mips32FunctionMask[functionIndex]) {
112 DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
113 F->addFnAttr("nomips16");
116 doneUsingMask = true;
126 DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
127 F->addFnAttr("nomips16");
130 DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
131 F->addFnAttr("mips16");
138 char MipsOs16::ID = 0;
142 ModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) {