This is an archive of the discontinued LLVM Phabricator instance.

[scan-build] fix dead store warnings emitted on LLVM Hexagon code base
ClosedPublic

Authored by apelete on May 3 2016, 10:26 PM.

Details

Summary

This patch fixes a logic error warning of the type "assigned value is
garbage or undefined" reported by Clang Static Analyzer on the
following file:

  • lib/Target/Hexagon/HexagonOptAddrMode.cpp.

It also addresses dead store warnings of the type "dead assignment" on
the following files:

  • lib/Target/Hexagon/MCTargetDesc/HexagonMCShuffler.cpp,
  • lib/Target/Hexagon/MCTargetDesc/HexagonInstPrinter.cpp,
  • lib/Target/Hexagon/HexagonFixupHwLoops.cpp,
  • lib/Target/Hexagon/HexagonCommonGEP.cpp.

Signed-off-by: Apelete Seketeli <apelete@seketeli.net>

Diff Detail

Repository
rL LLVM

Event Timeline

apelete updated this revision to Diff 56093.May 3 2016, 10:26 PM
apelete retitled this revision from to [scan-build] fix dead store warnings emitted on LLVM Hexagon code base.
apelete added reviewers: kparzysz, colinl.
apelete updated this object.
apelete added a subscriber: llvm-commits.
kparzysz added inline comments.May 4 2016, 6:46 AM
lib/Target/Hexagon/HexagonOptAddrMode.cpp
399 ↗(On Diff #56093)

This is not strictly necessary since OpStart is only used when Changed it "true". That happens iff OpStart is assigned a value.

apelete added inline comments.May 4 2016, 8:10 AM
lib/Target/Hexagon/HexagonOptAddrMode.cpp
399 ↗(On Diff #56093)

Not strictly necessary, it just helps suppressing a logic error warning, I agree.
To be more precise, in the following code path:

  1. if (ImmOpNum == 0) {
  2. if (HII->getAddrMode(OldMI) == HexagonII::BaseRegOffset) {

...

  1. OpStart = 4;
  2. } else if (HII->getAddrMode(OldMI) == HexagonII::BaseImmOffset) {

...

  1. OpStart = 3;
  2. }
  3. Changed = true;

...

  1. } else if (ImmOpNum == 1 && OldMI->getOperand(2).getImm() == 0) {

...

  1. }

From a static analysis point of view, if the branches at line 407 and 416 evaluate to 'false', then OpStart would be left un-initialized.
Then, when "Changed" is 'true':

  1. if (Changed)
  2. for (unsigned i = OpStart; i < OpEnd; ++i)
  3. MIB.addOperand(OldMI->getOperand(i));

'i' would be assigned the value of 'OpStart' which hasn't been initialized, thus the warning "assigned value is
garbage or undefined" from a static analysis point of view.

I understand that exact code path is unlikely to be taken at runtime.
Would you prefer we ignore the warning then (instead of trying to suppress it) ?

kparzysz edited edge metadata.EditedMay 4 2016, 9:55 AM

You're right. Maybe the best thing to do would be to initialize OpStart to OpEnd, and add an assert:

if (Changed) {
  assert(OpStart < OpEnd);
  for (...) ...
}
apelete updated this revision to Diff 56170.May 4 2016, 10:32 AM
apelete edited edge metadata.

[scan-build] fix dead store warnings emitted on LLVM Hexagon code base

Changes since last revision:

  • lib/Target/Hexagon/HexagonOptAddrMode.cpp: initialize 'OpStart' with 'OpEnd' value and assert if number of operands has not been computed before changing instruction store.
kparzysz accepted this revision.May 4 2016, 10:34 AM
kparzysz edited edge metadata.

LGTM. Thanks!

This revision is now accepted and ready to land.May 4 2016, 10:34 AM
apelete updated this revision to Diff 57119.May 12 2016, 5:06 PM
apelete edited edge metadata.

[scan-build] fix dead store warnings emitted on LLVM Hexagon code base

  • rebased patch on trunk and validated against test suite,
  • lib/Target/Hexagon/HexagonOptAddrMode.cpp: removed changes as they fail to pass the following tests:
    • LLVM :: CodeGen/Hexagon/opt-addr-mode.ll,
    • LLVM :: CodeGen/Hexagon/zextloadi1.ll.

LGTM. Thanks!

Removed some changes in last revision since they fail to pass some tests, will look into it when I get the time.
If current changes are good for you please go ahead and commit this for me when you're ready as I don't have commit access.

Thanks.

This revision was automatically updated to reflect the committed changes.