Promote partial-loads after stores.

Swift SVN r14217
This commit is contained in:
Nadav Rotem
2014-02-21 18:41:58 +00:00
parent 0a5f7b57a7
commit 20c8272388
2 changed files with 37 additions and 0 deletions

View File

@@ -144,6 +144,25 @@ bool performLoadStoreOptimizations(SILBasicBlock *BB, AliasAnalysis *AA) {
continue; continue;
} }
// Promote partial loads.
// Check that we are loading a struct element:
if (auto *SEAI = dyn_cast<StructElementAddrInst>(LI->getOperand())) {
// And that the previous store stores into that struct.
if (PrevStore && PrevStore->getDest() == SEAI->getOperand()) {
// And that the stored value is a struct construction instruction:
if (auto *SI = dyn_cast<StructInst>(PrevStore->getSrc())) {
DEBUG(llvm::dbgs() << " Forwarding element store from: " <<
*PrevStore);
unsigned FieldNo = SEAI->getFieldNo();
SILValue(LI, 0).replaceAllUsesWith(SI->getOperand(FieldNo));
recursivelyDeleteTriviallyDeadInstructions(LI, true);
Changed = true;
NumForwardedLoads++;
continue;
}
}
}
// Search the previous loads and replace the current load with one of the // Search the previous loads and replace the current load with one of the
// previous loads. // previous loads.
for (auto PrevLI : Loads) { for (auto PrevLI : Loads) {

View File

@@ -169,3 +169,21 @@ bb0(%0 : $*Agg1):
return %5 : $Builtin.Int32 return %5 : $Builtin.Int32
} }
struct Wrapper {
var value : Int
}
//CHECK-LABEL: promote_partial_load
//CHECK: alloc_stack
//CHECK-NOT: load
//CHECK: return %0
sil @promote_partial_load : $@thin (Int) -> Int {
bb0(%0 : $Int):
%1 = alloc_stack $Wrapper
%2 = struct $Wrapper (%0 : $Int)
store %2 to %1#1 : $*Wrapper
%3 = struct_element_addr %1#1 : $*Wrapper, #value
%4 = load %3 : $*Int
dealloc_stack %1#0 : $*@local_storage Wrapper
return %4 : $Int
}