This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Fix for PR34219: Preserve alignment after merging conditional stores.
ClosedPublic

Authored by ABataev on Aug 17 2017, 11:53 AM.

Details

Summary

If SimplifyCFG pass is able to merge conditional stores into single one,
it loses the alignment. This may lead to incorrect codegen. Patch
sets the alignment of the new instruction if it is set in the original
one.

Diff Detail

Repository
rL LLVM

Event Timeline

ABataev created this revision.Aug 17 2017, 11:53 AM
efriedma added inline comments.
lib/Transforms/Utils/SimplifyCFG.cpp
2969 ↗(On Diff #111539)

This looks weird; why are we calling "PStore->getAAMetadata" twice?

2972 ↗(On Diff #111539)

What happens if PStore and QStore have different alignment?

ABataev added inline comments.Aug 25 2017, 6:05 AM
lib/Transforms/Utils/SimplifyCFG.cpp
2972 ↗(On Diff #111539)

Generally speaking, they must be the same.

efriedma added inline comments.Aug 25 2017, 12:44 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2972 ↗(On Diff #111539)

I don't see any code to ensure they are the same. (Or do you mean we shouldn't do this transform if the alignment is different?)

ABataev added inline comments.Aug 25 2017, 1:02 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2972 ↗(On Diff #111539)

Actually, PStore and QStore work with the same values, so they must have the same alignment because it is the same operations. We just merge them into the single one. I'm not sure, if they may have the different alignments. If they may have the different alignments, we can choose the one that is greater.

efriedma added inline comments.Aug 25 2017, 1:33 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2972 ↗(On Diff #111539)

I think you have to take the smaller of the two alignments? Even if the pointer value is the same "Value*", it could have different alignment along different codepaths.

ABataev added inline comments.Aug 25 2017, 1:38 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2972 ↗(On Diff #111539)

Why? If have 2 stores with different alignments, we know that it is safe to use both of them. We should use the biggest one.

Why? If have 2 stores with different alignments, we know that it is safe to use both of them. We should use the biggest one.

If we could prove both stores execute, we could use biggest one. In this case, though, we only know that one of the stores executes (assuming I'm reading this code correctly). And we don't know it's safe to take the alignment from a store that doesn't execute. So we have to use the smaller alignment.

ABataev updated this revision to Diff 112895.Aug 28 2017, 7:02 AM

Update after review

efriedma added inline comments.Aug 28 2017, 12:41 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2976 ↗(On Diff #112895)

This isn't really handling zero alignment correctly; to get the right number here, you have to convert to the actual alignment using datalayout. (I'm planning to write a patch soon to get rid of this whole zero alignment crap; it's unnecessary given that datalayout is now mandatory, and it's bitten us too many times.)

It would be nice to have a comment briefly explaining why we pick the minimum.

2969 ↗(On Diff #111539)

No comment on the getAAMetadata() calls?

ABataev added inline comments.Aug 28 2017, 1:21 PM
lib/Transforms/Utils/SimplifyCFG.cpp
2976 ↗(On Diff #112895)

Ok, will do this, but tomorrow.

2969 ↗(On Diff #111539)

I don't know what's going here, it is another problem. I just want to fix one PR here.

ABataev updated this revision to Diff 113093.Aug 29 2017, 7:49 AM

Update after review.

ABataev added inline comments.Aug 29 2017, 7:53 AM
lib/Transforms/Utils/SimplifyCFG.cpp
2969 ↗(On Diff #111539)

Most probably, it is a copy-paste error. Must be

PStore->getAAMetadata(AAMD, /*Merge=*/false);
QStore->getAAMetadata(AAMD, /*Merge=*/true);
This revision is now accepted and ready to land.Aug 29 2017, 12:03 PM
This revision was automatically updated to reflect the committed changes.