2013-04-23 04:13:37 +08:00
|
|
|
//===---- MipsOs16.cpp for Mips Option -Os16 --------===//
|
2013-04-11 00:58:04 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines an optimization phase for the MIPS target.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-09 23:50:58 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "Mips.h"
|
2013-04-11 00:58:04 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2013-08-21 04:53:09 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2013-04-11 00:58:04 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-03-14 16:34:25 +08:00
|
|
|
|
2015-03-09 23:50:58 +08:00
|
|
|
using namespace llvm;
|
2013-04-11 00:58:04 +08:00
|
|
|
|
2014-04-22 10:41:26 +08:00
|
|
|
#define DEBUG_TYPE "mips-os16"
|
|
|
|
|
2013-08-21 04:53:09 +08:00
|
|
|
static cl::opt<std::string> Mips32FunctionMask(
|
|
|
|
"mips32-function-mask",
|
|
|
|
cl::init(""),
|
|
|
|
cl::desc("Force function to be mips32"),
|
|
|
|
cl::Hidden);
|
|
|
|
|
2013-04-11 00:58:04 +08:00
|
|
|
namespace {
|
2015-03-14 16:34:25 +08:00
|
|
|
class MipsOs16 : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
MipsOs16() : ModulePass(ID) {}
|
|
|
|
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "MIPS Os16 Optimization"; }
|
2015-03-14 16:34:25 +08:00
|
|
|
|
|
|
|
bool runOnModule(Module &M) override;
|
|
|
|
};
|
|
|
|
|
|
|
|
char MipsOs16::ID = 0;
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2013-04-11 00:58:04 +08:00
|
|
|
|
2015-03-14 16:34:25 +08:00
|
|
|
// Figure out if we need float point based on the function signature.
|
|
|
|
// We need to move variables in and/or out of floating point
|
|
|
|
// registers because of the ABI
|
|
|
|
//
|
|
|
|
static bool needsFPFromSig(Function &F) {
|
|
|
|
Type* RetType = F.getReturnType();
|
|
|
|
switch (RetType->getTypeID()) {
|
|
|
|
case Type::FloatTyID:
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (F.arg_size() >=1) {
|
|
|
|
Argument &Arg = F.getArgumentList().front();
|
|
|
|
switch (Arg.getType()->getTypeID()) {
|
2013-04-11 00:58:04 +08:00
|
|
|
case Type::FloatTyID:
|
|
|
|
case Type::DoubleTyID:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 16:34:25 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-11 00:58:04 +08:00
|
|
|
|
2015-03-14 16:34:25 +08:00
|
|
|
// Figure out if the function will need floating point operations
|
|
|
|
//
|
|
|
|
static bool needsFP(Function &F) {
|
|
|
|
if (needsFPFromSig(F))
|
|
|
|
return true;
|
|
|
|
for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
|
|
|
|
for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
|
2013-04-11 00:58:04 +08:00
|
|
|
I != E; ++I) {
|
2015-03-14 16:34:25 +08:00
|
|
|
const Instruction &Inst = *I;
|
|
|
|
switch (Inst.getOpcode()) {
|
|
|
|
case Instruction::FAdd:
|
|
|
|
case Instruction::FSub:
|
|
|
|
case Instruction::FMul:
|
|
|
|
case Instruction::FDiv:
|
|
|
|
case Instruction::FRem:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::FCmp:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I)) {
|
|
|
|
DEBUG(dbgs() << "Working on call" << "\n");
|
|
|
|
Function &F_ = *CI->getCalledFunction();
|
|
|
|
if (needsFPFromSig(F_))
|
2013-04-11 00:58:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-14 16:34:25 +08:00
|
|
|
}
|
|
|
|
return false;
|
2013-04-11 00:58:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MipsOs16::runOnModule(Module &M) {
|
2013-08-21 04:53:09 +08:00
|
|
|
bool usingMask = Mips32FunctionMask.length() > 0;
|
2013-09-24 06:36:11 +08:00
|
|
|
bool doneUsingMask = false; // this will make it stop repeating
|
2016-04-08 18:33:00 +08:00
|
|
|
|
2013-08-21 04:53:09 +08:00
|
|
|
DEBUG(dbgs() << "Run on Module MipsOs16 \n" << Mips32FunctionMask << "\n");
|
|
|
|
if (usingMask)
|
|
|
|
DEBUG(dbgs() << "using mask \n" << Mips32FunctionMask << "\n");
|
2016-04-08 18:33:00 +08:00
|
|
|
|
2013-08-21 04:53:09 +08:00
|
|
|
unsigned int functionIndex = 0;
|
2013-04-11 00:58:04 +08:00
|
|
|
bool modified = false;
|
2016-04-08 18:33:00 +08:00
|
|
|
|
|
|
|
for (auto &F : M) {
|
|
|
|
if (F.isDeclaration())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Working on " << F.getName() << "\n");
|
2013-08-21 04:53:09 +08:00
|
|
|
if (usingMask) {
|
2013-09-24 06:36:11 +08:00
|
|
|
if (!doneUsingMask) {
|
|
|
|
if (functionIndex == Mips32FunctionMask.length())
|
|
|
|
functionIndex = 0;
|
|
|
|
switch (Mips32FunctionMask[functionIndex]) {
|
|
|
|
case '1':
|
2016-04-08 18:33:00 +08:00
|
|
|
DEBUG(dbgs() << "mask forced mips32: " << F.getName() << "\n");
|
|
|
|
F.addFnAttr("nomips16");
|
2013-09-24 06:36:11 +08:00
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
doneUsingMask = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
functionIndex++;
|
2013-08-21 04:53:09 +08:00
|
|
|
}
|
2013-04-11 00:58:04 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-04-08 18:33:00 +08:00
|
|
|
if (needsFP(F)) {
|
|
|
|
DEBUG(dbgs() << "os16 forced mips32: " << F.getName() << "\n");
|
|
|
|
F.addFnAttr("nomips16");
|
2013-08-21 04:53:09 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-04-08 18:33:00 +08:00
|
|
|
DEBUG(dbgs() << "os16 forced mips16: " << F.getName() << "\n");
|
|
|
|
F.addFnAttr("mips16");
|
2013-08-21 04:53:09 +08:00
|
|
|
}
|
2013-04-11 00:58:04 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-08 18:33:00 +08:00
|
|
|
|
2013-04-11 00:58:04 +08:00
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
2015-03-14 16:34:25 +08:00
|
|
|
ModulePass *llvm::createMipsOs16Pass(MipsTargetMachine &TM) {
|
2013-04-11 00:58:04 +08:00
|
|
|
return new MipsOs16;
|
|
|
|
}
|