Add movabs -> mov shortening optimization. Add peephole optimization pass that does instruction shortening.

Summary:
Shorten when a mov instruction has a 64-bit immediate that can be repesented as
a sign extended 32-bit number, use the smaller mov instruction (MOV64ri -> MOV64ri32).

Add peephole optimization pass that does instruction shortening.

(cherry picked from FBD3603099)
This commit is contained in:
Bill Nell 2016-07-21 16:40:06 -07:00 committed by Maksim Panchenko
parent c6d0c568d4
commit ea53cffb2d
4 changed files with 51 additions and 0 deletions

View file

@ -36,6 +36,12 @@ SimplifyConditionalTailCalls("simplify-conditional-tail-calls",
"by removing unnecessary jumps"),
llvm::cl::Optional);
static llvm::cl::opt<bool>
Peepholes("peepholes",
llvm::cl::desc("run peephole optimizations"),
llvm::cl::init(true),
llvm::cl::Optional);
} // namespace opts
namespace llvm {
@ -82,6 +88,8 @@ void BinaryFunctionPassManager::runAllPasses(
Manager.registerPass(std::move(llvm::make_unique<FixupFunctions>()));
Manager.registerPass(llvm::make_unique<Peepholes>(), opts::Peepholes);
Manager.runPasses();
}

View file

@ -21,6 +21,7 @@ extern llvm::cl::opt<bool> DumpDotAll;
extern llvm::cl::opt<bool> PrintReordered;
extern llvm::cl::opt<bool> PrintEHRanges;
extern llvm::cl::opt<bool> PrintUCE;
extern llvm::cl::opt<bool> PrintPeepholes;
extern llvm::cl::opt<llvm::bolt::BinaryFunction::SplittingType> SplitFunctions;
extern bool shouldProcess(const llvm::bolt::BinaryFunction &Function);
@ -538,5 +539,33 @@ void SimplifyConditionalTailCalls::runOnFunctions(
<< " from a total of " << NumTailCallCandidates << "\n";
}
void Peepholes::shortenInstructions(BinaryContext &BC,
BinaryFunction &Function) {
for (auto &BB : Function) {
for (auto &Inst : BB) {
BC.MIA->shortenInstruction(Inst);
}
}
}
void Peepholes::runOnFunctions(BinaryContext &BC,
std::map<uint64_t, BinaryFunction> &BFs,
std::set<uint64_t> &LargeFunctions) {
for (auto &It : BFs) {
auto &Function = It.second;
if (Function.isSimple() && opts::shouldProcess(Function)) {
shortenInstructions(BC, Function);
if (opts::PrintAll || opts::PrintPeepholes) {
Function.print(errs(), "after peepholes", true);
}
if (opts::DumpDotAll) {
Function.dumpGraphForPass("peepholes");
}
}
}
}
} // namespace bolt
} // namespace llvm

View file

@ -148,6 +148,15 @@ class SimplifyConditionalTailCalls : public BinaryFunctionPass {
std::set<uint64_t> &LargeFunctions) override;
};
/// Perform simple peephole optimizations.
class Peepholes : public BinaryFunctionPass {
void shortenInstructions(BinaryContext &BC, BinaryFunction &Function);
public:
void runOnFunctions(BinaryContext &BC,
std::map<uint64_t, BinaryFunction> &BFs,
std::set<uint64_t> &LargeFunctions) override;
};
} // namespace bolt
} // namespace llvm

View file

@ -157,6 +157,11 @@ PrintUCE("print-uce",
cl::desc("print functions after unreachable code elimination"),
cl::Hidden);
cl::opt<bool>
PrintPeepholes("print-peepholes",
cl::desc("print functions after peephole optimization"),
cl::Hidden);
static cl::opt<bool>
PrintDisasm("print-disasm", cl::desc("print function after disassembly"),
cl::Hidden);