MipsOs16.cpp revision 74adad6de8cf947257a53bb08364fa0f4f71b10e
1//===---- MipsOs16.cpp for Mips Option -Os16                         --------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an optimization phase for the MIPS target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "mips-os16"
15#include "MipsOs16.h"
16#include "llvm/IR/Module.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19
20namespace {
21
22  // Figure out if we need float point based on the function signature.
23  // We need to move variables in and/or out of floating point
24  // registers because of the ABI
25  //
26  bool needsFPFromSig(Function &F) {
27    Type* RetType = F.getReturnType();
28    switch (RetType->getTypeID()) {
29    case Type::FloatTyID:
30    case Type::DoubleTyID:
31      return true;
32    default:
33      ;
34    }
35    if (F.arg_size() >=1) {
36      Argument &Arg = F.getArgumentList().front();
37      switch (Arg.getType()->getTypeID()) {
38        case Type::FloatTyID:
39        case Type::DoubleTyID:
40          return true;
41        default:
42          ;
43      }
44    }
45    return false;
46  }
47
48  // Figure out if the function will need floating point operations
49  //
50  bool needsFP(Function &F) {
51    if (needsFPFromSig(F))
52      return true;
53    for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
54      for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
55         I != E; ++I) {
56        const Instruction &Inst = *I;
57        switch (Inst.getOpcode()) {
58        case Instruction::FAdd:
59        case Instruction::FSub:
60        case Instruction::FMul:
61        case Instruction::FDiv:
62        case Instruction::FRem:
63        case Instruction::FPToUI:
64        case Instruction::FPToSI:
65        case Instruction::UIToFP:
66        case Instruction::SIToFP:
67        case Instruction::FPTrunc:
68        case Instruction::FPExt:
69        case Instruction::FCmp:
70          return true;
71        default:
72          ;
73        }
74        if (const CallInst *CI = dyn_cast<CallInst>(I)) {
75          DEBUG(dbgs() << "Working on call" << "\n");
76          Function &F_ =  *CI->getCalledFunction();
77          if (needsFPFromSig(F_))
78            return true;
79        }
80      }
81    return false;
82  }
83}
84namespace llvm {
85
86
87bool MipsOs16::runOnModule(Module &M) {
88  DEBUG(errs() << "Run on Module MipsOs16\n");
89  bool modified = false;
90  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
91    if (F->isDeclaration()) continue;
92    DEBUG(dbgs() << "Working on " << F->getName() << "\n");
93    if (needsFP(*F)) {
94      DEBUG(dbgs() << " need to compile as nomips16 \n");
95      F->addFnAttr("nomips16");
96    }
97    else {
98      F->addFnAttr("mips16");
99      DEBUG(dbgs() << " no need to compile as nomips16 \n");
100    }
101  }
102  return modified;
103}
104
105char MipsOs16::ID = 0;
106
107}
108
109ModulePass *llvm::createMipsOs16(MipsTargetMachine &TM) {
110  return new MipsOs16;
111}
112
113
114