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 //===----------------------------------------------------------------------===//
15 #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"
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"),
32 // Figure out if we need float point based on the function signature.
33 // We need to move variables in and/or out of floating point
34 // registers because of the ABI
36 bool needsFPFromSig(Function &F) {
37 Type* RetType = F.getReturnType();
38 switch (RetType->getTypeID()) {
40 case Type::DoubleTyID:
45 if (F.arg_size() >=1) {
46 Argument &Arg = F.getArgumentList().front();
47 switch (Arg.getType()->getTypeID()) {
49 case Type::DoubleTyID:
58 // Figure out if the function will need floating point operations
60 bool needsFP(Function &F) {
61 if (needsFPFromSig(F))
63 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
64 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
66 const Instruction &Inst = *I;
67 switch (Inst.getOpcode()) {
68 case Instruction::FAdd:
69 case Instruction::FSub:
70 case Instruction::FMul:
71 case Instruction::FDiv:
72 case Instruction::FRem:
73 case Instruction::FPToUI:
74 case Instruction::FPToSI:
75 case Instruction::UIToFP:
76 case Instruction::SIToFP:
77 case Instruction::FPTrunc:
78 case Instruction::FPExt:
79 case Instruction::FCmp:
84 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
85 DEBUG(dbgs() << "Working on call" << "\n");
86 Function &F_ = *CI->getCalledFunction();
87 if (needsFPFromSig(F_))
97 class MipsOs16 : public ModulePass {
101 MipsOs16() : ModulePass(ID) {}
103 const char *getPassName() const override { return "MIPS Os16 Optimization"; }
104 bool runOnModule(Module &M) override;
108 bool MipsOs16::runOnModule(Module &M) {
109 bool usingMask = Mips32FunctionMask.length() > 0;
110 bool doneUsingMask = false; // this will make it stop repeating
111 DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
113 DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
114 unsigned int functionIndex = 0;
115 bool modified = false;
116 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
117 if (F->isDeclaration()) continue;
118 DEBUG(dbgs() << "Working on " << F->getName() << "\n");
120 if (!doneUsingMask) {
121 if (functionIndex == Mips32FunctionMask.length())
123 switch (Mips32FunctionMask[functionIndex]) {
125 DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
126 F->addFnAttr("nomips16");
129 doneUsingMask = true;
139 DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
140 F->addFnAttr("nomips16");
143 DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
144 F->addFnAttr("mips16");
151 char MipsOs16::ID = 0;
153 ModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) {