Skip to content

Commit

Permalink
Add movabs -> mov shortening optimization. Add peephole optimization …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
Bill Nell authored and maksfb committed Jul 21, 2016
1 parent c6d0c56 commit ea53cff
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bolt/BinaryPassManager.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}

Expand Down
29 changes: 29 additions & 0 deletions bolt/BinaryPasses.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions bolt/BinaryPasses.h
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions bolt/RewriteInstance.cpp
Expand Up @@ -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);
Expand Down

0 comments on commit ea53cff

Please sign in to comment.