LLVM  6.0.0svn
MipsOs16.cpp
Go to the documentation of this file.
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 #include "Mips.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/Module.h"
18 #include "llvm/Support/Debug.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "mips-os16"
24 
26  "mips32-function-mask",
27  cl::init(""),
28  cl::desc("Force function to be mips32"),
29  cl::Hidden);
30 
31 namespace {
32  class MipsOs16 : public ModulePass {
33  public:
34  static char ID;
35 
36  MipsOs16() : ModulePass(ID) {}
37 
38  StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
39 
40  bool runOnModule(Module &M) override;
41  };
42 
43  char MipsOs16::ID = 0;
44 }
45 
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
49 //
50 static bool needsFPFromSig(Function &F) {
51  Type* RetType = F.getReturnType();
52  switch (RetType->getTypeID()) {
53  case Type::FloatTyID:
54  case Type::DoubleTyID:
55  return true;
56  default:
57  ;
58  }
59  if (F.arg_size() >=1) {
60  Argument &Arg = *F.arg_begin();
61  switch (Arg.getType()->getTypeID()) {
62  case Type::FloatTyID:
63  case Type::DoubleTyID:
64  return true;
65  default:
66  ;
67  }
68  }
69  return false;
70 }
71 
72 // Figure out if the function will need floating point operations
73 //
74 static bool needsFP(Function &F) {
75  if (needsFPFromSig(F))
76  return true;
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();
79  I != E; ++I) {
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:
94  return true;
95  default:
96  ;
97  }
98  if (const CallInst *CI = dyn_cast<CallInst>(I)) {
99  DEBUG(dbgs() << "Working on call" << "\n");
100  Function &F_ = *CI->getCalledFunction();
101  if (needsFPFromSig(F_))
102  return true;
103  }
104  }
105  return false;
106 }
107 
108 
109 bool MipsOs16::runOnModule(Module &M) {
110  bool usingMask = Mips32FunctionMask.length() > 0;
111  bool doneUsingMask = false; // this will make it stop repeating
112 
113  DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
114  if (usingMask)
115  DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
116 
117  unsigned int functionIndex = 0;
118  bool modified = false;
119 
120  for (auto &F : M) {
121  if (F.isDeclaration())
122  continue;
123 
124  DEBUG(dbgs() << "Working on " << F.getName() << "\n");
125  if (usingMask) {
126  if (!doneUsingMask) {
127  if (functionIndex == Mips32FunctionMask.length())
128  functionIndex = 0;
129  switch (Mips32FunctionMask[functionIndex]) {
130  case '1':
131  DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
132  F.addFnAttr("nomips16");
133  break;
134  case '.':
135  doneUsingMask = true;
136  break;
137  default:
138  break;
139  }
140  functionIndex++;
141  }
142  }
143  else {
144  if (needsFP(F)) {
145  DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
146  F.addFnAttr("nomips16");
147  }
148  else {
149  DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
150  F.addFnAttr("mips16");
151  }
152  }
153  }
154 
155  return modified;
156 }
157 
158 ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); }
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
2: 32-bit floating point type
Definition: Type.h:59
iterator end()
Definition: Function.h:590
This class represents a function call, abstracting a target machine&#39;s calling convention.
F(f)
static cl::opt< std::string > Mips32FunctionMask("mips32-function-mask", cl::init(""), cl::desc("Force function to be mips32"), cl::Hidden)
TypeID getTypeID() const
Return the type id for the type.
Definition: Type.h:138
static bool needsFPFromSig(Function &F)
Definition: MipsOs16.cpp:50
static bool needsFP(Function &F)
Definition: MipsOs16.cpp:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:125
iterator begin()
Definition: Function.h:588
ModulePass * createMipsOs16Pass()
Definition: MipsOs16.cpp:158
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
Type * getReturnType() const
Returns the type of the ret val.
Definition: Function.h:150
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
size_t arg_size() const
Definition: Function.h:630
arg_iterator arg_begin()
Definition: Function.h:603
Iterator for intrusive lists based on ilist_node.
Module.h This file contains the declarations for the Module class.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
amdgpu Simplify well known AMD library false Value Value * Arg
#define I(x, y, z)
Definition: MD5.cpp:58
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
3: 64-bit floating point type
Definition: Type.h:60
#define DEBUG(X)
Definition: Debug.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49