This is an archive of the discontinued LLVM Phabricator instance.

[Evaluator] Improve evaluation of call instruction
ClosedPublic

Authored by evgeny777 on May 8 2018, 9:31 AM.

Details

Summary

This allows evaluating calls containing bitcast instruction, which often happens when using ThinLTO.
It's actually a replacement to D43199 and together with D43457 does the same job of optimizing out static ctors, without bothering structure type names.

Diff Detail

Repository
rL LLVM

Event Timeline

evgeny777 created this revision.May 8 2018, 9:31 AM
grimar added inline comments.May 10 2018, 7:21 AM
lib/Transforms/Utils/Evaluator.cpp
243 ↗(On Diff #145704)

Seems it better looks like:

Constant *Evaluator::substCallExprArgument(Value *CallExpr, Constant *RV) {
  ConstantExpr *CE = dyn_cast<ConstantExpr>(CallExpr);
  if (!CE || CE->getOpcode() != Instruction::BitCast)
    return RV;

  if (auto *FT = dyn_cast<FunctionType>(CE->getType()->getPointerElementType()))
    return ConstantFoldLoadThroughBitcast(RV, FT->getReturnType(), DL);
  return RV;
}
evgeny777 updated this revision to Diff 148813.May 28 2018, 7:23 AM

Addressed review comments

Any comments on this?

Sorry I missed this one.

lib/Transforms/Utils/Evaluator.cpp
232 ↗(On Diff #148813)

The name/description seem a little confusing to me. IIUC there isn't any substitution into the function call expression being done here, it is essentially casting the evaluated return value to the call's bitcast type, right?

506 ↗(On Diff #148813)

Is this case being tested by the new test case?

test/Transforms/GlobalOpt/evaluate-call.ll
1 ↗(On Diff #148813)

Needs comment about what is being tested

evgeny777 updated this revision to Diff 152078.Jun 20 2018, 7:03 AM

Thanks for looking at it @tejohnson. Sorry I was too busy to respond earlier.
I've added extra test case to check function calls which can be constant folded.
The substCallExprArgument was changed to castCallResultIfNeeded which might sound more reasonable.

tejohnson accepted this revision.Jun 21 2018, 8:47 AM

LGTM with a couple of nits.

include/llvm/Transforms/Utils/Evaluator.h
78 ↗(On Diff #152078)

Document your new functions.

test/Transforms/GlobalOpt/evaluate-constfold-call.ll
2 ↗(On Diff #152078)

s/ftof/fmodf/?

6 ↗(On Diff #152078)

Should the resulting code in main() also be checked?

This revision is now accepted and ready to land.Jun 21 2018, 8:47 AM
This revision was automatically updated to reflect the committed changes.