llvm.org GIT mirror llvm / b8e9164
[InstSimplify] Don't constant fold or DCE calls that are marked nobuiltin Differential Revision: https://reviews.llvm.org/D33737 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305132 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Kaylor 2 years ago
11 changed file(s) with 71 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
3030 class Function;
3131 class GlobalValue;
3232 class Instruction;
33 class ImmutableCallSite;
3334 class TargetLibraryInfo;
3435 class Type;
3536
124125
125126 /// canConstantFoldCallTo - Return true if its even possible to fold a call to
126127 /// the specified function.
127 bool canConstantFoldCallTo(const Function *F);
128 bool canConstantFoldCallTo(ImmutableCallSite CS, const Function *F);
128129
129130 /// ConstantFoldCall - Attempt to constant fold a call to the specified function
130131 /// with the specified arguments, returning null if unsuccessful.
131 Constant *ConstantFoldCall(Function *F, ArrayRef Operands,
132 Constant *ConstantFoldCall(ImmutableCallSite CS, Function *F,
133 ArrayRef Operands,
132134 const TargetLibraryInfo *TLI = nullptr);
133135
134136 /// \brief Check whether the given call has no side-effects.
4040 class AssumptionCache;
4141 class DominatorTree;
4242 class Instruction;
43 class ImmutableCallSite;
4344 class DataLayout;
4445 class FastMathFlags;
4546 struct LoopStandardAnalysisResults;
193194
194195 /// Given a function and iterators over arguments, fold the result or return
195196 /// null.
196 Value *SimplifyCall(Value *V, User::op_iterator ArgBegin,
197 Value *SimplifyCall(ImmutableCallSite CS, Value *V, User::op_iterator ArgBegin,
197198 User::op_iterator ArgEnd, const SimplifyQuery &Q);
198199
199200 /// Given a function and set of arguments, fold the result or return null.
200 Value *SimplifyCall(Value *V, ArrayRef Args, const SimplifyQuery &Q);
201 Value *SimplifyCall(ImmutableCallSite CS, Value *V, ArrayRef Args,
202 const SimplifyQuery &Q);
201203
202204 /// See if we can compute a simplified version of this instruction. If not,
203205 /// return null.
10141014 case Instruction::ICmp:
10151015 case Instruction::FCmp: llvm_unreachable("Invalid for compares");
10161016 case Instruction::Call:
1017 if (auto *F = dyn_cast(Ops.back()))
1018 if (canConstantFoldCallTo(F))
1019 return ConstantFoldCall(F, Ops.slice(0, Ops.size() - 1), TLI);
1017 if (auto *F = dyn_cast(Ops.back())) {
1018 ImmutableCallSite CS(cast(InstOrCE));
1019 if (canConstantFoldCallTo(CS, F))
1020 return ConstantFoldCall(CS, F, Ops.slice(0, Ops.size() - 1), TLI);
1021 }
10201022 return nullptr;
10211023 case Instruction::Select:
10221024 return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]);
13551357 // Constant Folding for Calls
13561358 //
13571359
1358 bool llvm::canConstantFoldCallTo(const Function *F) {
1360 bool llvm::canConstantFoldCallTo(ImmutableCallSite CS, const Function *F) {
1361 if (CS.isNoBuiltin())
1362 return false;
13591363 switch (F->getIntrinsicID()) {
13601364 case Intrinsic::fabs:
13611365 case Intrinsic::minnum:
20582062 } // end anonymous namespace
20592063
20602064 Constant *
2061 llvm::ConstantFoldCall(Function *F, ArrayRef Operands,
2065 llvm::ConstantFoldCall(ImmutableCallSite CS, Function *F,
2066 ArrayRef Operands,
20622067 const TargetLibraryInfo *TLI) {
2068 if (CS.isNoBuiltin())
2069 return nullptr;
20632070 if (!F->hasName())
20642071 return nullptr;
20652072 StringRef Name = F->getName();
20762083 bool llvm::isMathLibCallNoop(CallSite CS, const TargetLibraryInfo *TLI) {
20772084 // FIXME: Refactor this code; this duplicates logic in LibCallsShrinkWrap
20782085 // (and to some extent ConstantFoldScalarCall).
2086 if (CS.isNoBuiltin())
2087 return false;
20792088 Function *F = CS.getCalledFunction();
20802089 if (!F)
20812090 return false;
868868 // because we have to continually rebuild the argument list even when no
869869 // simplifications can be performed. Until that is fixed with remapping
870870 // inside of instsimplify, directly constant fold calls here.
871 if (!canConstantFoldCallTo(F))
871 if (!canConstantFoldCallTo(CS, F))
872872 return false;
873873
874874 // Try to re-map the arguments to constants.
884884
885885 ConstantArgs.push_back(C);
886886 }
887 if (Constant *C = ConstantFoldCall(F, ConstantArgs)) {
887 if (Constant *C = ConstantFoldCall(CS, F, ConstantArgs)) {
888888 SimplifiedValues[CS.getInstruction()] = C;
889889 return true;
890890 }
44724472 }
44734473
44744474 template
4475 static Value *SimplifyCall(Value *V, IterTy ArgBegin, IterTy ArgEnd,
4476 const SimplifyQuery &Q, unsigned MaxRecurse) {
4475 static Value *SimplifyCall(ImmutableCallSite CS, Value *V, IterTy ArgBegin,
4476 IterTy ArgEnd, const SimplifyQuery &Q,
4477 unsigned MaxRecurse) {
44774478 Type *Ty = V->getType();
44784479 if (PointerType *PTy = dyn_cast(Ty))
44794480 Ty = PTy->getElementType();
44924493 if (Value *Ret = SimplifyIntrinsic(F, ArgBegin, ArgEnd, Q, MaxRecurse))
44934494 return Ret;
44944495
4495 if (!canConstantFoldCallTo(F))
4496 if (!canConstantFoldCallTo(CS, F))
44964497 return nullptr;
44974498
44984499 SmallVector ConstantArgs;
45044505 ConstantArgs.push_back(C);
45054506 }
45064507
4507 return ConstantFoldCall(F, ConstantArgs, Q.TLI);
4508 }
4509
4510 Value *llvm::SimplifyCall(Value *V, User::op_iterator ArgBegin,
4511 User::op_iterator ArgEnd, const SimplifyQuery &Q) {
4512 return ::SimplifyCall(V, ArgBegin, ArgEnd, Q, RecursionLimit);
4513 }
4514
4515 Value *llvm::SimplifyCall(Value *V, ArrayRef Args,
4508 return ConstantFoldCall(CS, F, ConstantArgs, Q.TLI);
4509 }
4510
4511 Value *llvm::SimplifyCall(ImmutableCallSite CS, Value *V,
4512 User::op_iterator ArgBegin, User::op_iterator ArgEnd,
45164513 const SimplifyQuery &Q) {
4517 return ::SimplifyCall(V, Args.begin(), Args.end(), Q, RecursionLimit);
4514 return ::SimplifyCall(CS, V, ArgBegin, ArgEnd, Q, RecursionLimit);
4515 }
4516
4517 Value *llvm::SimplifyCall(ImmutableCallSite CS, Value *V,
4518 ArrayRef Args, const SimplifyQuery &Q) {
4519 return ::SimplifyCall(CS, V, Args.begin(), Args.end(), Q, RecursionLimit);
45184520 }
45194521
45204522 /// See if we can compute a simplified version of this instruction.
46454647 break;
46464648 case Instruction::Call: {
46474649 CallSite CS(cast(I));
4648 Result = SimplifyCall(CS.getCalledValue(), CS.arg_begin(), CS.arg_end(), Q);
4650 Result = SimplifyCall(CS, CS.getCalledValue(), CS.arg_begin(), CS.arg_end(),
4651 Q);
46494652 break;
46504653 }
46514654 #define HANDLE_CAST_INST(num, opc, clas) case Instruction::opc:
67926792
67936793 if (const CallInst *CI = dyn_cast(I))
67946794 if (const Function *F = CI->getCalledFunction())
6795 return canConstantFoldCallTo(F);
6795 return canConstantFoldCallTo(CI, F);
67966796 return false;
67976797 }
67986798
18131813 /// lifting.
18141814 Instruction *InstCombiner::visitCallInst(CallInst &CI) {
18151815 auto Args = CI.arg_operands();
1816 if (Value *V = SimplifyCall(CI.getCalledValue(), Args.begin(), Args.end(),
1817 SQ.getWithInstruction(&CI)))
1816 if (Value *V = SimplifyCall(&CI, CI.getCalledValue(), Args.begin(),
1817 Args.end(), SQ.getWithInstruction(&CI)))
18181818 return replaceInstUsesWith(CI, V);
18191819
18201820 if (isFreeCall(&CI, &TLI))
11161116 // Otherwise, if we have a single return value case, and if the function is
11171117 // a declaration, maybe we can constant fold it.
11181118 if (F && F->isDeclaration() && !I->getType()->isStructTy() &&
1119 canConstantFoldCallTo(F)) {
1119 canConstantFoldCallTo(CS, F)) {
11201120
11211121 SmallVector Operands;
11221122 for (CallSite::arg_iterator AI = CS.arg_begin(), E = CS.arg_end();
11361136
11371137 // If we can constant fold this, mark the result of the call as a
11381138 // constant.
1139 if (Constant *C = ConstantFoldCall(F, Operands, TLI)) {
1139 if (Constant *C = ConstantFoldCall(CS, F, Operands, TLI)) {
11401140 // call -> undef.
11411141 if (isa(C))
11421142 return;
438438
439439 if (Callee->isDeclaration()) {
440440 // If this is a function we can constant fold, do it.
441 if (Constant *C = ConstantFoldCall(Callee, Formals, TLI)) {
441 if (Constant *C = ConstantFoldCall(CS, Callee, Formals, TLI)) {
442442 InstResult = C;
443443 DEBUG(dbgs() << "Constant folded function call. Result: " <<
444444 *InstResult << "\n");
7171 ; CHECK-NEXT: %cos2 = call double @cos(double 0x7FF0000000000000)
7272 %cos2 = call double @cos(double 0x7FF0000000000000)
7373
74 ; cos(0) nobuiltin may have side effects
75 ; CHECK-NEXT: %cos3 = call double @cos(double 0.000000e+00)
76 %cos3 = call double @cos(double 0.000000e+00) nobuiltin
77
7478 ; pow(0, 1) is 0
7579 %pow1 = call double @pow(double 0x7FF0000000000000, double 1.000000e+00)
7680
0 ; RUN: opt < %s -instcombine -S | FileCheck %s
1
2 declare double @acos(double)
3
4 ; Check that functions without any function attributes are simplified.
5
6 define double @test_simplify_acos() {
7 ; CHECK-LABEL: @test_simplify_acos
8 %pi = call double @acos(double -1.000000e+00)
9 ; CHECK-NOT: call double @acos
10 ; CHECK: ret double 0x400921FB54442D18
11 ret double %pi
12 }
13
14 define double @test_acos_nobuiltin() {
15 ; CHECK-LABEL: @test_acos_nobuiltin
16 %pi = call double @acos(double -1.000000e+00) nobuiltin
17 ; CHECK: call double @acos(double -1.000000e+00)
18 ret double %pi
19 }