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"
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 StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
40 bool runOnModule(Module &M) override;
43 char MipsOs16::ID = 0;
46 // Figure out if we need float point based on the function signature.
47 // We need to move variables in and/or out of floating point
48 // registers because of the ABI
50 static bool needsFPFromSig(Function &F) {
51 Type* RetType = F.getReturnType();
52 switch (RetType->getTypeID()) {
54 case Type::DoubleTyID:
59 if (F.arg_size() >=1) {
60 Argument &Arg = *F.arg_begin();
61 switch (Arg.getType()->getTypeID()) {
63 case Type::DoubleTyID:
72 // Figure out if the function will need floating point operations
74 static bool needsFP(Function &F) {
75 if (needsFPFromSig(F))
77 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
80 const Instruction &Inst = *I;
81 switch (Inst.getOpcode()) {
82 case Instruction::FAdd:
83 case Instruction::FSub:
84 case Instruction::FMul:
85 case Instruction::FDiv:
86 case Instruction::FRem:
87 case Instruction::FPToUI:
88 case Instruction::FPToSI:
89 case Instruction::UIToFP:
90 case Instruction::SIToFP:
91 case Instruction::FPTrunc:
92 case Instruction::FPExt:
93 case Instruction::FCmp:
98 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
99 LLVM_DEBUG(dbgs() << "Working on call"
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
114 LLVM_DEBUG(dbgs() << "Run on Module MipsOs16 \n"
115 << Mips32FunctionMask << "\n");
117 LLVM_DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
119 unsigned int functionIndex = 0;
120 bool modified = false;
123 if (F.isDeclaration())
126 LLVM_DEBUG(dbgs() << "Working on " << F.getName() << "\n");
128 if (!doneUsingMask) {
129 if (functionIndex == Mips32FunctionMask.length())
131 switch (Mips32FunctionMask[functionIndex]) {
133 LLVM_DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
134 F.addFnAttr("nomips16");
137 doneUsingMask = true;
147 LLVM_DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
148 F.addFnAttr("nomips16");
151 LLVM_DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
152 F.addFnAttr("mips16");
160 ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); }