Akira Hatanaka | d8fb032 | 2013-04-22 20:13:37 +0000 | [diff] [blame] | 1 | //===---- MipsOs16.cpp for Mips Option -Os16 --------===// |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 14 | #include "Mips.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Instructions.h" |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Module.h" |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CommandLine.h" |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 20 | |
Benjamin Kramer | a52f696 | 2015-03-09 15:50:58 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 22 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "mips-os16" |
| 24 | |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 25 | static cl::opt<std::string> Mips32FunctionMask( |
| 26 | "mips32-function-mask", |
| 27 | cl::init(""), |
| 28 | cl::desc("Force function to be mips32"), |
| 29 | cl::Hidden); |
| 30 | |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 31 | namespace { |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 32 | class MipsOs16 : public ModulePass { |
| 33 | public: |
| 34 | static char ID; |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 35 | |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 36 | MipsOs16() : ModulePass(ID) {} |
| 37 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 38 | StringRef getPassName() const override { return "MIPS Os16 Optimization"; } |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 39 | |
| 40 | bool runOnModule(Module &M) override; |
| 41 | }; |
| 42 | |
| 43 | char MipsOs16::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 44 | } |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 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) { |
Reid Kleckner | 45707d4 | 2017-03-16 22:59:15 +0000 | [diff] [blame] | 60 | Argument &Arg = *F.arg_begin(); |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 61 | switch (Arg.getType()->getTypeID()) { |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 62 | case Type::FloatTyID: |
| 63 | case Type::DoubleTyID: |
| 64 | return true; |
| 65 | default: |
| 66 | ; |
| 67 | } |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 68 | } |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 69 | return false; |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 70 | } |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 71 | |
Vasileios Kalintiris | 6312f51 | 2015-03-14 08:34:25 +0000 | [diff] [blame] | 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 | } |
Benjamin Kramer | a52f696 | 2015-03-09 15:50:58 +0000 | [diff] [blame] | 107 | |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 108 | |
| 109 | bool MipsOs16::runOnModule(Module &M) { |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 110 | bool usingMask = Mips32FunctionMask.length() > 0; |
Reed Kotler | e883f50 | 2013-09-23 22:36:11 +0000 | [diff] [blame] | 111 | bool doneUsingMask = false; // this will make it stop repeating |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 112 | |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 113 | DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n"); |
| 114 | if (usingMask) |
| 115 | DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n"); |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 116 | |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 117 | unsigned int functionIndex = 0; |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 118 | bool modified = false; |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 119 | |
| 120 | for (auto &F : M) { |
| 121 | if (F.isDeclaration()) |
| 122 | continue; |
| 123 | |
| 124 | DEBUG(dbgs() << "Working on " << F.getName() << "\n"); |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 125 | if (usingMask) { |
Reed Kotler | e883f50 | 2013-09-23 22:36:11 +0000 | [diff] [blame] | 126 | if (!doneUsingMask) { |
| 127 | if (functionIndex == Mips32FunctionMask.length()) |
| 128 | functionIndex = 0; |
| 129 | switch (Mips32FunctionMask[functionIndex]) { |
| 130 | case '1': |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 131 | DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n"); |
| 132 | F.addFnAttr("nomips16"); |
Reed Kotler | e883f50 | 2013-09-23 22:36:11 +0000 | [diff] [blame] | 133 | break; |
| 134 | case '.': |
| 135 | doneUsingMask = true; |
| 136 | break; |
| 137 | default: |
| 138 | break; |
| 139 | } |
| 140 | functionIndex++; |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 141 | } |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 142 | } |
| 143 | else { |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 144 | if (needsFP(F)) { |
| 145 | DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n"); |
| 146 | F.addFnAttr("nomips16"); |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 147 | } |
| 148 | else { |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 149 | DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n"); |
| 150 | F.addFnAttr("mips16"); |
Reed Kotler | d8f3362 | 2013-08-20 20:53:09 +0000 | [diff] [blame] | 151 | } |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
Vasileios Kalintiris | 957d849 | 2016-04-08 10:33:00 +0000 | [diff] [blame] | 154 | |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 155 | return modified; |
| 156 | } |
| 157 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 158 | ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); } |