Enable hoisting load from constant memories.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@89510 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Evan Cheng 2009-11-20 23:31:34 +00:00
parent d9e05cd22a
commit c26abd9487
4 changed files with 13 additions and 30 deletions

View File

@ -34,16 +34,11 @@
#include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/Statistic.h" #include "llvm/ADT/Statistic.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h" #include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
using namespace llvm; using namespace llvm;
static cl::opt<bool> HoistLdConst("licm-const-load",
cl::desc("LICM load from constant memory"),
cl::init(false), cl::Hidden);
STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops"); STATISTIC(NumHoisted, "Number of machine instructions hoisted out of loops");
STATISTIC(NumCSEed, "Number of hoisted machine instructions CSEed"); STATISTIC(NumCSEed, "Number of hoisted machine instructions CSEed");
@ -102,7 +97,7 @@ namespace {
/// IsProfitableToHoist - Return true if it is potentially profitable to /// IsProfitableToHoist - Return true if it is potentially profitable to
/// hoist the given loop invariant. /// hoist the given loop invariant.
bool IsProfitableToHoist(MachineInstr &MI, bool &isConstLd); bool IsProfitableToHoist(MachineInstr &MI);
/// HoistRegion - Walk the specified region of the CFG (defined by all /// HoistRegion - Walk the specified region of the CFG (defined by all
/// blocks dominated by the specified block, and that are in the current /// blocks dominated by the specified block, and that are in the current
@ -367,9 +362,7 @@ bool MachineLICM::isLoadFromConstantMemory(MachineInstr *MI) {
/// IsProfitableToHoist - Return true if it is potentially profitable to hoist /// IsProfitableToHoist - Return true if it is potentially profitable to hoist
/// the given loop invariant. /// the given loop invariant.
bool MachineLICM::IsProfitableToHoist(MachineInstr &MI, bool &isConstLd) { bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
isConstLd = false;
if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF) if (MI.getOpcode() == TargetInstrInfo::IMPLICIT_DEF)
return false; return false;
@ -382,9 +375,8 @@ bool MachineLICM::IsProfitableToHoist(MachineInstr &MI, bool &isConstLd) {
// adding a store in the loop preheader. But the reload is no more expensive. // adding a store in the loop preheader. But the reload is no more expensive.
// The side benefit is these loads are frequently CSE'ed. // The side benefit is these loads are frequently CSE'ed.
if (!TII->isTriviallyReMaterializable(&MI, AA)) { if (!TII->isTriviallyReMaterializable(&MI, AA)) {
if (!HoistLdConst || !isLoadFromConstantMemory(&MI)) if (!isLoadFromConstantMemory(&MI))
return false; return false;
isConstLd = true;
} }
// If result(s) of this instruction is used by PHIs, then don't hoist it. // If result(s) of this instruction is used by PHIs, then don't hoist it.
@ -439,9 +431,7 @@ MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
MBB->insert(MI, NewMIs[1]); MBB->insert(MI, NewMIs[1]);
// If unfolding produced a load that wasn't loop-invariant or profitable to // If unfolding produced a load that wasn't loop-invariant or profitable to
// hoist, discard the new instructions and bail. // hoist, discard the new instructions and bail.
bool isConstLd; if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
if (!IsLoopInvariantInst(*NewMIs[0]) ||
!IsProfitableToHoist(*NewMIs[0], isConstLd)) {
NewMIs[0]->eraseFromParent(); NewMIs[0]->eraseFromParent();
NewMIs[1]->eraseFromParent(); NewMIs[1]->eraseFromParent();
return 0; return 0;
@ -507,9 +497,7 @@ bool MachineLICM::EliminateCSE(MachineInstr *MI,
/// ///
void MachineLICM::Hoist(MachineInstr *MI) { void MachineLICM::Hoist(MachineInstr *MI) {
// First check whether we should hoist this instruction. // First check whether we should hoist this instruction.
bool isConstLd; if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
if (!IsLoopInvariantInst(*MI) ||
!IsProfitableToHoist(*MI, isConstLd)) {
// If not, try unfolding a hoistable load. // If not, try unfolding a hoistable load.
MI = ExtractHoistableLoad(MI); MI = ExtractHoistableLoad(MI);
if (!MI) return; if (!MI) return;
@ -518,10 +506,7 @@ void MachineLICM::Hoist(MachineInstr *MI) {
// Now move the instructions to the predecessor, inserting it before any // Now move the instructions to the predecessor, inserting it before any
// terminator instructions. // terminator instructions.
DEBUG({ DEBUG({
errs() << "Hoisting "; errs() << "Hoisting " << *MI;
if (isConstLd)
errs() << "load from constant mem ";
errs() << *MI;
if (CurPreheader->getBasicBlock()) if (CurPreheader->getBasicBlock())
errs() << " to MachineBasicBlock " errs() << " to MachineBasicBlock "
<< CurPreheader->getName(); << CurPreheader->getName();

View File

@ -18,7 +18,9 @@ entry:
bb.nph: ; preds = %entry bb.nph: ; preds = %entry
; CHECK: BB#1 ; CHECK: BB#1
; CHECK: ldr.n r2, LCPI1_0 ; CHECK: ldr.n r2, LCPI1_0
; CHECK: ldr r{{[0-9]+}}, [r2] ; CHECK: ldr r3, [r2]
; CHECK: ldr r3, [r3]
; CHECK: ldr r2, [r2]
; CHECK: LBB1_2 ; CHECK: LBB1_2
; CHECK: LCPI1_0: ; CHECK: LCPI1_0:
; CHECK-NOT: LCPI1_1: ; CHECK-NOT: LCPI1_1:
@ -27,7 +29,9 @@ bb.nph: ; preds = %entry
; PIC: BB#1 ; PIC: BB#1
; PIC: ldr.n r2, LCPI1_0 ; PIC: ldr.n r2, LCPI1_0
; PIC: add r2, pc ; PIC: add r2, pc
; PIC: ldr r{{[0-9]+}}, [r2] ; PIC: ldr r3, [r2]
; PIC: ldr r3, [r3]
; PIC: ldr r2, [r2]
; PIC: LBB1_2 ; PIC: LBB1_2
; PIC: LCPI1_0: ; PIC: LCPI1_0:
; PIC-NOT: LCPI1_1: ; PIC-NOT: LCPI1_1:

View File

@ -1,4 +1,4 @@
; RUN: llc < %s -mtriple=i386-apple-darwin -relocation-model=pic -stats |& grep {machine-licm} | grep 1 ; RUN: llc < %s -mtriple=i386-apple-darwin -relocation-model=pic -stats |& grep {machine-licm} | grep 2
; rdar://7274692 ; rdar://7274692
%0 = type { [125 x i32] } %0 = type { [125 x i32] }

View File

@ -1,10 +1,4 @@
; RUN: llc < %s -mtriple=i686-apple-darwin -mattr=+sse2 -relocation-model=pic | grep psllw | grep pb ; RUN: llc < %s -mtriple=i686-apple-darwin -mattr=+sse2 -relocation-model=pic | grep psllw | grep pb
; XFAIL: *
; This is XFAIL'd because MachineLICM is now hoisting all of the loads, and the pic
; base appears killed in the entry block when remat is making its decisions. Remat's
; simple heuristic decides against rematting because it doesn't want to extend the
; live-range of the pic base; this isn't necessarily optimal.
define void @f() nounwind { define void @f() nounwind {
entry: entry: