1251607Sdim//===---- MipsOs16.cpp for Mips Option -Os16 --------===// 2251607Sdim// 3251607Sdim// The LLVM Compiler Infrastructure 4251607Sdim// 5251607Sdim// This file is distributed under the University of Illinois Open Source 6251607Sdim// License. See LICENSE.TXT for details. 7251607Sdim// 8251607Sdim//===----------------------------------------------------------------------===// 9251607Sdim// 10251607Sdim// This file defines an optimization phase for the MIPS target. 11251607Sdim// 12251607Sdim//===----------------------------------------------------------------------===// 13251607Sdim 14251607Sdim#define DEBUG_TYPE "mips-os16" 15251607Sdim#include "MipsOs16.h" 16251607Sdim#include "llvm/IR/Module.h" 17251607Sdim#include "llvm/Support/Debug.h" 18251607Sdim#include "llvm/Support/raw_ostream.h" 19251607Sdim 20251607Sdimnamespace { 21251607Sdim 22251607Sdim // Figure out if we need float point based on the function signature. 23251607Sdim // We need to move variables in and/or out of floating point 24251607Sdim // registers because of the ABI 25251607Sdim // 26251607Sdim bool needsFPFromSig(Function &F) { 27251607Sdim Type* RetType = F.getReturnType(); 28251607Sdim switch (RetType->getTypeID()) { 29251607Sdim case Type::FloatTyID: 30251607Sdim case Type::DoubleTyID: 31251607Sdim return true; 32251607Sdim default: 33251607Sdim ; 34251607Sdim } 35251607Sdim if (F.arg_size() >=1) { 36251607Sdim Argument &Arg = F.getArgumentList().front(); 37251607Sdim switch (Arg.getType()->getTypeID()) { 38251607Sdim case Type::FloatTyID: 39251607Sdim case Type::DoubleTyID: 40251607Sdim return true; 41251607Sdim default: 42251607Sdim ; 43251607Sdim } 44251607Sdim } 45251607Sdim return false; 46251607Sdim } 47251607Sdim 48251607Sdim // Figure out if the function will need floating point operations 49251607Sdim // 50251607Sdim bool needsFP(Function &F) { 51251607Sdim if (needsFPFromSig(F)) 52251607Sdim return true; 53251607Sdim for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) 54251607Sdim for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 55251607Sdim I != E; ++I) { 56251607Sdim const Instruction &Inst = *I; 57251607Sdim switch (Inst.getOpcode()) { 58251607Sdim case Instruction::FAdd: 59251607Sdim case Instruction::FSub: 60251607Sdim case Instruction::FMul: 61251607Sdim case Instruction::FDiv: 62251607Sdim case Instruction::FRem: 63251607Sdim case Instruction::FPToUI: 64251607Sdim case Instruction::FPToSI: 65251607Sdim case Instruction::UIToFP: 66251607Sdim case Instruction::SIToFP: 67251607Sdim case Instruction::FPTrunc: 68251607Sdim case Instruction::FPExt: 69251607Sdim case Instruction::FCmp: 70251607Sdim return true; 71251607Sdim default: 72251607Sdim ; 73251607Sdim } 74251607Sdim if (const CallInst *CI = dyn_cast<CallInst>(I)) { 75251607Sdim DEBUG(dbgs() << "Working on call" << "\n"); 76251607Sdim Function &F_ = *CI->getCalledFunction(); 77251607Sdim if (needsFPFromSig(F_)) 78251607Sdim return true; 79251607Sdim } 80251607Sdim } 81251607Sdim return false; 82251607Sdim } 83251607Sdim} 84251607Sdimnamespace llvm { 85251607Sdim 86251607Sdim 87251607Sdimbool MipsOs16::runOnModule(Module &M) { 88251607Sdim DEBUG(errs() << "Run on Module MipsOs16\n"); 89251607Sdim bool modified = false; 90251607Sdim for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { 91251607Sdim if (F->isDeclaration()) continue; 92251607Sdim DEBUG(dbgs() << "Working on " << F->getName() << "\n"); 93251607Sdim if (needsFP(*F)) { 94251607Sdim DEBUG(dbgs() << " need to compile as nomips16 \n"); 95251607Sdim F->addFnAttr("nomips16"); 96251607Sdim } 97251607Sdim else { 98251607Sdim F->addFnAttr("mips16"); 99251607Sdim DEBUG(dbgs() << " no need to compile as nomips16 \n"); 100251607Sdim } 101251607Sdim } 102251607Sdim return modified; 103251607Sdim} 104251607Sdim 105251607Sdimchar MipsOs16::ID = 0; 106251607Sdim 107251607Sdim} 108251607Sdim 109251607SdimModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) { 110251607Sdim return new MipsOs16; 111251607Sdim} 112251607Sdim 113251607Sdim 114