aboutsummaryrefslogtreecommitdiffstats
path: root/lib/CodeGen
diff options
context:
space:
mode:
authorBob Wilson <bob.wilson@apple.com>2009-10-27 23:49:38 +0000
committerBob Wilson <bob.wilson@apple.com>2009-10-27 23:49:38 +0000
commitcd4f04d6bcb7aefa24d92582fbadfe17519f4756 (patch)
tree154762d67c0c4a8d677aeef3bd023b5a1f0f719a /lib/CodeGen
parent03236140fa4ef316a605717e090276d6a0d42828 (diff)
downloadexternal_llvm-cd4f04d6bcb7aefa24d92582fbadfe17519f4756.zip
external_llvm-cd4f04d6bcb7aefa24d92582fbadfe17519f4756.tar.gz
external_llvm-cd4f04d6bcb7aefa24d92582fbadfe17519f4756.tar.bz2
Record CodeGen optimization level in the BranchFolding pass so that we can
use it to control tail merging when there is a tradeoff between performance and code size. When there is only 1 instruction in the common tail, we have been merging. That can be good for code size but is a definite loss for performance. Now we will avoid tail merging in that case when the optimization level is "Aggressive", i.e., "-O3". Radar 7338114. Since the IfConversion pass invokes BranchFolding, it too needs to know the optimization level. Note that I removed the RegisterPass instantiation for IfConversion because it required a default constructor. If someone wants to keep that for some reason, we can add a default constructor with a hard-wired optimization level. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85346 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen')
-rw-r--r--lib/CodeGen/BranchFolding.cpp14
-rw-r--r--lib/CodeGen/BranchFolding.h10
-rw-r--r--lib/CodeGen/IfConversion.cpp13
-rw-r--r--lib/CodeGen/LLVMTargetMachine.cpp2
4 files changed, 24 insertions, 15 deletions
diff --git a/lib/CodeGen/BranchFolding.cpp b/lib/CodeGen/BranchFolding.cpp
index 66c5aa5..11777d5 100644
--- a/lib/CodeGen/BranchFolding.cpp
+++ b/lib/CodeGen/BranchFolding.cpp
@@ -50,8 +50,9 @@ TailMergeThreshold("tail-merge-threshold",
char BranchFolderPass::ID = 0;
-FunctionPass *llvm::createBranchFoldingPass(bool DefaultEnableTailMerge) {
- return new BranchFolderPass(DefaultEnableTailMerge);
+FunctionPass *llvm::createBranchFoldingPass(bool DefaultEnableTailMerge,
+ CodeGenOpt::Level OptLevel) {
+ return new BranchFolderPass(DefaultEnableTailMerge, OptLevel);
}
bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) {
@@ -63,7 +64,8 @@ bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) {
-BranchFolder::BranchFolder(bool defaultEnableTailMerge) {
+BranchFolder::BranchFolder(bool defaultEnableTailMerge, CodeGenOpt::Level OL) {
+ OptLevel = OL;
switch (FlagEnableTailMerge) {
case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge; break;
case cl::BOU_TRUE: EnableTailMerge = true; break;
@@ -470,7 +472,8 @@ unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
I->second,
TrialBBI1, TrialBBI2);
// If we will have to split a block, there should be at least
- // minCommonTailLength instructions in common; if not, at worst
+ // minCommonTailLength instructions in common; if not, and if we are not
+ // optimizing for performance at the expense of code size, at worst
// we will be replacing a fallthrough into the common tail with a
// branch, which at worst breaks even with falling through into
// the duplicated common tail, so 1 instruction in common is enough.
@@ -478,7 +481,8 @@ unsigned BranchFolder::ComputeSameTails(unsigned CurHash,
// tail if there is one.
// (Empty blocks will get forwarded and need not be considered.)
if (CommonTailLen >= minCommonTailLength ||
- (CommonTailLen > 0 &&
+ (OptLevel != CodeGenOpt::Aggressive &&
+ CommonTailLen > 0 &&
(TrialBBI1==CurMPIter->second->begin() ||
TrialBBI2==I->second->begin()))) {
if (CommonTailLen > maxCommonTailLength) {
diff --git a/lib/CodeGen/BranchFolding.h b/lib/CodeGen/BranchFolding.h
index 9763e33..5d35525 100644
--- a/lib/CodeGen/BranchFolding.h
+++ b/lib/CodeGen/BranchFolding.h
@@ -12,6 +12,7 @@
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/Target/TargetMachine.h"
#include <vector>
namespace llvm {
@@ -23,7 +24,7 @@ namespace llvm {
class BranchFolder {
public:
- explicit BranchFolder(bool defaultEnableTailMerge);
+ explicit BranchFolder(bool defaultEnableTailMerge, CodeGenOpt::Level OL);
bool OptimizeFunction(MachineFunction &MF,
const TargetInstrInfo *tii,
@@ -37,6 +38,7 @@ namespace llvm {
typedef std::pair<MPIterator, MachineBasicBlock::iterator> SameTailElt;
std::vector<SameTailElt> SameTails;
+ CodeGenOpt::Level OptLevel;
bool EnableTailMerge;
const TargetInstrInfo *TII;
const TargetRegisterInfo *TRI;
@@ -73,8 +75,10 @@ namespace llvm {
public BranchFolder {
public:
static char ID;
- explicit BranchFolderPass(bool defaultEnableTailMerge)
- : MachineFunctionPass(&ID), BranchFolder(defaultEnableTailMerge) {}
+ explicit BranchFolderPass(bool defaultEnableTailMerge,
+ CodeGenOpt::Level OptLevel)
+ : MachineFunctionPass(&ID),
+ BranchFolder(defaultEnableTailMerge, OptLevel) {}
virtual bool runOnMachineFunction(MachineFunction &MF);
virtual const char *getPassName() const { return "Control Flow Optimizer"; }
diff --git a/lib/CodeGen/IfConversion.cpp b/lib/CodeGen/IfConversion.cpp
index 45f08b1..be9e1f1 100644
--- a/lib/CodeGen/IfConversion.cpp
+++ b/lib/CodeGen/IfConversion.cpp
@@ -148,9 +148,11 @@ namespace {
const TargetInstrInfo *TII;
bool MadeChange;
int FnNum;
+ CodeGenOpt::Level OptLevel;
public:
static char ID;
- IfConverter() : MachineFunctionPass(&ID), FnNum(-1) {}
+ IfConverter(CodeGenOpt::Level OL) :
+ MachineFunctionPass(&ID), FnNum(-1), OptLevel(OL) {}
virtual bool runOnMachineFunction(MachineFunction &MF);
virtual const char *getPassName() const { return "If Converter"; }
@@ -219,10 +221,9 @@ namespace {
char IfConverter::ID = 0;
}
-static RegisterPass<IfConverter>
-X("if-converter", "If Converter");
-
-FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
+FunctionPass *llvm::createIfConverterPass(CodeGenOpt::Level OptLevel) {
+ return new IfConverter(OptLevel);
+}
bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
TLI = MF.getTarget().getTargetLowering();
@@ -362,7 +363,7 @@ bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
BBAnalysis.clear();
if (MadeChange) {
- BranchFolder BF(false);
+ BranchFolder BF(false, OptLevel);
BF.OptimizeFunction(MF, TII,
MF.getTarget().getRegisterInfo(),
getAnalysisIfAvailable<MachineModuleInfo>());
diff --git a/lib/CodeGen/LLVMTargetMachine.cpp b/lib/CodeGen/LLVMTargetMachine.cpp
index e58a9ca..1a2daef 100644
--- a/lib/CodeGen/LLVMTargetMachine.cpp
+++ b/lib/CodeGen/LLVMTargetMachine.cpp
@@ -329,7 +329,7 @@ bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
// Branch folding must be run after regalloc and prolog/epilog insertion.
if (OptLevel != CodeGenOpt::None) {
- PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
+ PM.add(createBranchFoldingPass(getEnableTailMergeDefault(), OptLevel));
printAndVerify(PM);
}