LLVM API Documentation

MipsOs16.cpp
Go to the documentation of this file.
00001 //===---- MipsOs16.cpp for Mips Option -Os16                       --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines an optimization phase for the MIPS target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MipsOs16.h"
00015 #include "llvm/IR/Module.h"
00016 #include "llvm/Support/CommandLine.h"
00017 #include "llvm/Support/Debug.h"
00018 #include "llvm/Support/raw_ostream.h"
00019 
00020 #define DEBUG_TYPE "mips-os16"
00021 
00022 
00023 static cl::opt<std::string> Mips32FunctionMask(
00024   "mips32-function-mask",
00025   cl::init(""),
00026   cl::desc("Force function to be mips32"),
00027   cl::Hidden);
00028 
00029 namespace {
00030 
00031   // Figure out if we need float point based on the function signature.
00032   // We need to move variables in and/or out of floating point
00033   // registers because of the ABI
00034   //
00035   bool needsFPFromSig(Function &F) {
00036     Type* RetType = F.getReturnType();
00037     switch (RetType->getTypeID()) {
00038     case Type::FloatTyID:
00039     case Type::DoubleTyID:
00040       return true;
00041     default:
00042       ;
00043     }
00044     if (F.arg_size() >=1) {
00045       Argument &Arg = F.getArgumentList().front();
00046       switch (Arg.getType()->getTypeID()) {
00047         case Type::FloatTyID:
00048         case Type::DoubleTyID:
00049           return true;
00050         default:
00051           ;
00052       }
00053     }
00054     return false;
00055   }
00056 
00057   // Figure out if the function will need floating point operations
00058   //
00059   bool needsFP(Function &F) {
00060     if (needsFPFromSig(F))
00061       return true;
00062     for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
00063       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
00064          I != E; ++I) {
00065         const Instruction &Inst = *I;
00066         switch (Inst.getOpcode()) {
00067         case Instruction::FAdd:
00068         case Instruction::FSub:
00069         case Instruction::FMul:
00070         case Instruction::FDiv:
00071         case Instruction::FRem:
00072         case Instruction::FPToUI:
00073         case Instruction::FPToSI:
00074         case Instruction::UIToFP:
00075         case Instruction::SIToFP:
00076         case Instruction::FPTrunc:
00077         case Instruction::FPExt:
00078         case Instruction::FCmp:
00079           return true;
00080         default:
00081           ;
00082         }
00083         if (const CallInst *CI = dyn_cast<CallInst>(I)) {
00084           DEBUG(dbgs() << "Working on call" << "\n");
00085           Function &F_ =  *CI->getCalledFunction();
00086           if (needsFPFromSig(F_))
00087             return true;
00088         }
00089       }
00090     return false;
00091   }
00092 }
00093 namespace llvm {
00094 
00095 
00096 bool MipsOs16::runOnModule(Module &M) {
00097   bool usingMask = Mips32FunctionMask.length() > 0;
00098   bool doneUsingMask = false; // this will make it stop repeating
00099   DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
00100   if (usingMask)
00101     DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
00102   unsigned int functionIndex = 0;
00103   bool modified = false;
00104   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
00105     if (F->isDeclaration()) continue;
00106     DEBUG(dbgs() << "Working on " << F->getName() << "\n");
00107     if (usingMask) {
00108       if (!doneUsingMask) {
00109         if (functionIndex == Mips32FunctionMask.length())
00110           functionIndex = 0;
00111         switch (Mips32FunctionMask[functionIndex]) {
00112         case '1':
00113           DEBUG(dbgs() << "mask forced mips32: " << F->getName() << "\n");
00114           F->addFnAttr("nomips16");
00115           break;
00116         case '.':
00117           doneUsingMask = true;
00118           break;
00119         default:
00120           break;
00121         }
00122         functionIndex++;
00123       }
00124     }
00125     else {
00126       if (needsFP(*F)) {
00127         DEBUG(dbgs() << "os16 forced mips32: " << F->getName() << "\n");
00128         F->addFnAttr("nomips16");
00129       }
00130       else {
00131         DEBUG(dbgs() << "os16 forced mips16: " << F->getName() << "\n");
00132         F->addFnAttr("mips16");
00133       }
00134     }
00135   }
00136   return modified;
00137 }
00138 
00139 char MipsOs16::ID = 0;
00140 
00141 }
00142 
00143 ModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) {
00144   return new MipsOs16;
00145 }
00146 
00147