[Alignment] Move OffsetToAlignment to Alignment.h
[llvm-complete.git] / lib / Target / Mips / MipsOs16.cpp
blobac4e55f8a1f58cb2adf6da680dedb58dba08b89b
1 //===---- MipsOs16.cpp for Mips Option -Os16 --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines an optimization phase for the MIPS target.
11 //===----------------------------------------------------------------------===//
13 #include "Mips.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
22 #define DEBUG_TYPE "mips-os16"
24 static cl::opt<std::string> Mips32FunctionMask(
25 "mips32-function-mask",
26 cl::init(""),
27 cl::desc("Force function to be mips32"),
28 cl::Hidden);
30 namespace {
31 class MipsOs16 : public ModulePass {
32 public:
33 static char ID;
35 MipsOs16() : ModulePass(ID) {}
37 StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
39 bool runOnModule(Module &M) override;
42 char MipsOs16::ID = 0;
45 // Figure out if we need float point based on the function signature.
46 // We need to move variables in and/or out of floating point
47 // registers because of the ABI
49 static bool needsFPFromSig(Function &F) {
50 Type* RetType = F.getReturnType();
51 switch (RetType->getTypeID()) {
52 case Type::FloatTyID:
53 case Type::DoubleTyID:
54 return true;
55 default:
58 if (F.arg_size() >=1) {
59 Argument &Arg = *F.arg_begin();
60 switch (Arg.getType()->getTypeID()) {
61 case Type::FloatTyID:
62 case Type::DoubleTyID:
63 return true;
64 default:
68 return false;
71 // Figure out if the function will need floating point operations
73 static bool needsFP(Function &F) {
74 if (needsFPFromSig(F))
75 return true;
76 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
77 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
78 I != E; ++I) {
79 const Instruction &Inst = *I;
80 switch (Inst.getOpcode()) {
81 case Instruction::FAdd:
82 case Instruction::FSub:
83 case Instruction::FMul:
84 case Instruction::FDiv:
85 case Instruction::FRem:
86 case Instruction::FPToUI:
87 case Instruction::FPToSI:
88 case Instruction::UIToFP:
89 case Instruction::SIToFP:
90 case Instruction::FPTrunc:
91 case Instruction::FPExt:
92 case Instruction::FCmp:
93 return true;
94 default:
97 if (const CallInst *CI = dyn_cast<CallInst>(I)) {
98 LLVM_DEBUG(dbgs() << "Working on call"
99 << "\n");
100 Function &F_ = *CI->getCalledFunction();
101 if (needsFPFromSig(F_))
102 return true;
105 return false;
109 bool MipsOs16::runOnModule(Module &M) {
110 bool usingMask = Mips32FunctionMask.length() > 0;
111 bool doneUsingMask = false; // this will make it stop repeating
113 LLVM_DEBUG(dbgs() << "Run on Module MipsOs16 \n"
114 << Mips32FunctionMask << "\n");
115 if (usingMask)
116 LLVM_DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
118 unsigned int functionIndex = 0;
119 bool modified = false;
121 for (auto &F : M) {
122 if (F.isDeclaration())
123 continue;
125 LLVM_DEBUG(dbgs() << "Working on " << F.getName() << "\n");
126 if (usingMask) {
127 if (!doneUsingMask) {
128 if (functionIndex == Mips32FunctionMask.length())
129 functionIndex = 0;
130 switch (Mips32FunctionMask[functionIndex]) {
131 case '1':
132 LLVM_DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
133 F.addFnAttr("nomips16");
134 break;
135 case '.':
136 doneUsingMask = true;
137 break;
138 default:
139 break;
141 functionIndex++;
144 else {
145 if (needsFP(F)) {
146 LLVM_DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
147 F.addFnAttr("nomips16");
149 else {
150 LLVM_DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
151 F.addFnAttr("mips16");
156 return modified;
159 ModulePass *llvm::createMipsOs16Pass() { return new MipsOs16(); }