summaryrefslogtreecommitdiffstats
path: root/llvm/lib/CodeGen/OptimizeCmps.cpp
diff options
context:
space:
mode:
authorBill Wendling <isanbard@gmail.com>2010-08-06 01:32:48 +0000
committerBill Wendling <isanbard@gmail.com>2010-08-06 01:32:48 +0000
commit7de9d52c139e43469f1e07bb204c2da613fb18bb (patch)
tree7f33694bf3ee29fc7faff2e099a85c43dbb3a930 /llvm/lib/CodeGen/OptimizeCmps.cpp
parent957fc3df529b108b24548a60cf702f42b78a799c (diff)
downloadbcm5719-llvm-7de9d52c139e43469f1e07bb204c2da613fb18bb.tar.gz
bcm5719-llvm-7de9d52c139e43469f1e07bb204c2da613fb18bb.zip
Add the Optimize Compares pass (disabled by default).
This pass tries to remove comparison instructions when possible. For instance, if you have this code: sub r1, 1 cmp r1, 0 bz L1 and "sub" either sets the same flag as the "cmp" instruction or could be converted to set the same flag, then we can eliminate the "cmp" instruction all together. This is a important for ARM where the ALU instructions could set the CPSR flag, but need a special suffix ('s') to do so. llvm-svn: 110423
Diffstat (limited to 'llvm/lib/CodeGen/OptimizeCmps.cpp')
-rw-r--r--llvm/lib/CodeGen/OptimizeCmps.cpp112
1 files changed, 112 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/OptimizeCmps.cpp b/llvm/lib/CodeGen/OptimizeCmps.cpp
new file mode 100644
index 00000000000..cd396cc2eae
--- /dev/null
+++ b/llvm/lib/CodeGen/OptimizeCmps.cpp
@@ -0,0 +1,112 @@
+//===-- OptimizeCmps.cpp - Optimize comparison instrs ---------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass performs optimization of comparison instructions. For instance, in
+// this code:
+//
+// sub r1, 1
+// cmp r1, 0
+// bz L1
+//
+// If the "sub" instruction all ready sets (or could be modified to set) the
+// same flag that the "cmp" instruction sets and that "bz" uses, then we can
+// eliminate the "cmp" instruction.
+//
+//===----------------------------------------------------------------------===//
+
+#define DEBUG_TYPE "opt-compares"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/Target/TargetInstrInfo.h"
+#include "llvm/Target/TargetRegisterInfo.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/ADT/Statistic.h"
+using namespace llvm;
+
+STATISTIC(NumEliminated, "Number of compares eliminated");
+
+static cl::opt<bool>
+EnableOptCmps("enable-optimize-cmps", cl::init(false), cl::Hidden);
+
+namespace {
+ class OptimizeCmps : public MachineFunctionPass {
+ const TargetMachine *TM;
+ const TargetInstrInfo *TII;
+ MachineRegisterInfo *MRI;
+
+ bool OptimizeCmpInstr(MachineInstr *MI, MachineBasicBlock *MBB);
+
+ public:
+ static char ID; // Pass identification
+ OptimizeCmps() : MachineFunctionPass(&ID) {}
+
+ virtual bool runOnMachineFunction(MachineFunction &MF);
+
+ virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+ AU.setPreservesCFG();
+ MachineFunctionPass::getAnalysisUsage(AU);
+ }
+ };
+}
+
+char OptimizeCmps::ID = 0;
+INITIALIZE_PASS(OptimizeCmps, "opt-cmps",
+ "Optimize comparison instrs", false, false);
+
+FunctionPass *llvm::createOptimizeCmpsPass() { return new OptimizeCmps(); }
+
+/// OptimizeCmpInstr - If the instruction is a compare and the previous
+/// instruction it's comparing against all ready sets (or could be modified to
+/// set) the same flag as the compare, then we can remove the comparison and use
+/// the flag from the previous instruction.
+bool OptimizeCmps::OptimizeCmpInstr(MachineInstr *MI, MachineBasicBlock *MBB) {
+ // If this instruction is a comparison against zero and isn't comparing a
+ // physical register, we can try to optimize it.
+ unsigned SrcReg;
+ int CmpValue;
+ if (!TII->isCompareInstr(MI, SrcReg, CmpValue) ||
+ TargetRegisterInfo::isPhysicalRegister(SrcReg) ||
+ CmpValue != 0)
+ return false;
+
+ MachineRegisterInfo::def_iterator DI = MRI->def_begin(SrcReg);
+ if (llvm::next(DI) != MRI->def_end())
+ // Only support one definition.
+ return false;
+
+ // Attempt to convert the defining instruction to set the "zero" flag.
+ if (TII->convertToSetZeroFlag(&*DI, MI)) {
+ ++NumEliminated;
+ return true;
+ }
+
+ return false;
+}
+
+bool OptimizeCmps::runOnMachineFunction(MachineFunction &MF) {
+ TM = &MF.getTarget();
+ TII = TM->getInstrInfo();
+ MRI = &MF.getRegInfo();
+
+ if (!EnableOptCmps) return false;
+
+ bool Changed = false;
+ for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
+ MachineBasicBlock *MBB = &*I;
+ for (MachineBasicBlock::iterator
+ MII = MBB->begin(), ME = MBB->end(); MII != ME; ) {
+ MachineInstr *MI = &*MII++;
+ Changed |= OptimizeCmpInstr(MI, MBB);
+ }
+ }
+
+ return Changed;
+}
OpenPOWER on IntegriCloud