llvm.org GIT mirror llvm / 07649d9
Make load->store deletion a bit smarter. This allows us to compile this: void test(long long *P) { *P ^= 1; } into just: _test: movl 4(%esp), %eax xorl $1, (%eax) ret instead of code like this: _test: movl 4(%esp), %ecx xorl $1, (%ecx) movl 4(%ecx), %edx movl %edx, 4(%ecx) ret git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45762 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 12 years ago
2 changed file(s) with 44 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
41214121 return SDOperand();
41224122 }
41234123
4124 /// ReachesChainWithoutSideEffects - Do a little local search to see if Src can
4125 /// reach Dest without any side effects like a store, or call. Non-volatile
4126 /// loads are ok though.
4127 static bool ReachesChainWithoutSideEffects(SDOperand Src, SDOperand Dest,
4128 unsigned Depth = 0) {
4129 if (Src == Dest) return true;
4130
4131 // Don't search too deeply, we just want to be able to see through
4132 // TokenFactor's etc.
4133 if (Depth == 2) return false;
4134
4135 // If this is a token factor, all inputs to the TF happen in parallel. If any
4136 // of the operands of the TF reach dest, then we can do the xform.
4137 if (Src.getOpcode() == ISD::TokenFactor) {
4138 for (unsigned i = 0, e = Src.getNumOperands(); i != e; ++i)
4139 if (ReachesChainWithoutSideEffects(Src.getOperand(i), Dest, Depth+1))
4140 return true;
4141 return false;
4142 }
4143
4144 // Loads don't have side effects, look through them.
4145 if (LoadSDNode *Ld = dyn_cast(Src)) {
4146 if (!Ld->isVolatile())
4147 return ReachesChainWithoutSideEffects(Ld->getChain(), Dest, Depth+1);
4148 }
4149 return false;
4150 }
4151
41244152 SDOperand DAGCombiner::visitSTORE(SDNode *N) {
41254153 StoreSDNode *ST = cast(N);
41264154 SDOperand Chain = ST->getChain();
42494277 // If this is a load followed by a store to the same location, then the store
42504278 // is dead/noop.
42514279 if (LoadSDNode *Ld = dyn_cast(Value)) {
4252 if (Chain.Val == Ld && Ld->getBasePtr() == Ptr &&
4280 if (Ld->getBasePtr() == Ptr && ST->getStoredVT() == Ld->getLoadedVT() &&
42534281 ST->getAddressingMode() == ISD::UNINDEXED &&
4254 ST->getStoredVT() == Ld->getLoadedVT() &&
4255 !ST->isVolatile()) {
4282 !ST->isVolatile() &&
4283 // There can't be any side effects between the load and store, such as
4284 // a call or store.
4285 ReachesChainWithoutSideEffects(Chain, SDOperand(Ld, 1))) {
42564286 // The store is dead, remove it.
42574287 return Chain;
42584288 }
0 ; RUN: llvm-as < %s | llc -march=x86 | not grep '4{(%...)}
1 ; This should not load or store the top part of *P.
2
3 define void @test(i64* %P) nounwind {
4 entry:
5 %tmp1 = load i64* %P, align 8 ; [#uses=1]
6 %tmp2 = xor i64 %tmp1, 1 ; [#uses=1]
7 store i64 %tmp2, i64* %P, align 8
8 ret void
9 }
10