diff --git a/llvm/include/llvm/Transforms/Utils/SCCPSolver.h b/llvm/include/llvm/Transforms/Utils/SCCPSolver.h --- a/llvm/include/llvm/Transforms/Utils/SCCPSolver.h +++ b/llvm/include/llvm/Transforms/Utils/SCCPSolver.h @@ -151,13 +151,14 @@ /// Return a reference to the set of argument tracked functions. SmallPtrSetImpl &getArgumentTrackedFunctions(); - /// Mark the constant argument of a new function specialization. \p F points - /// to the cloned function and \p Arg represents the constant argument as a - /// pair of {formal,actual} values (the formal argument is associated with the - /// original function definition). All other arguments of the specialization - /// inherit the lattice state of their corresponding values in the original - /// function. - void markArgInFuncSpecialization(Function *F, const ArgInfo &Arg); + /// Mark the constant arguments of a new function specialization. \p F points + /// to the cloned function and \p Args contains a list of constant arguments + /// represented as pairs of {formal,actual} values (the formal argument is + /// associated with the original function definition). All other arguments of + /// the specialization inherit the lattice state of their corresponding values + /// in the original function. + void markArgInFuncSpecialization(Function *F, + const SmallVectorImpl &Args); /// Mark all of the blocks in function \p F non-executable. Clients can used /// this method to erase a function from the module (e.g., if it has been diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp --- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -113,17 +113,14 @@ // Bookkeeping struct to pass data from the analysis and profitability phase // to the actual transform helper functions. struct SpecializationInfo { - ArgInfo Arg; // Stores the {formal,actual} argument pair. - InstructionCost Gain; // Profitability: Gain = Bonus - Cost. - - SpecializationInfo(Argument *A, Constant *C, InstructionCost G) - : Arg(A, C), Gain(G){}; + SmallVector Args; // Stores the {formal,actual} argument pairs. + InstructionCost Gain = 0; // Profitability: Gain = Bonus - Cost. }; } // Anonymous namespace using FuncList = SmallVectorImpl; -using ConstList = SmallVector; -using SpecializationList = SmallVector; +using ConstList = SmallVector>; +using SpecializationMap = SmallMapVector; // Helper to check if \p LV is either a constant or a constant // range with a single element. This should cover exactly the same cases as the @@ -303,24 +300,21 @@ auto Cost = getSpecializationCost(F); if (!Cost.isValid()) { LLVM_DEBUG( - dbgs() << "FnSpecialization: Invalid specialisation cost.\n"); + dbgs() << "FnSpecialization: Invalid specialization cost.\n"); continue; } LLVM_DEBUG(dbgs() << "FnSpecialization: Specialization cost for " << F->getName() << " is " << Cost << "\n"); - SpecializationList Specializations; - calculateGains(F, Cost, Specializations); - if (Specializations.empty()) { - LLVM_DEBUG(dbgs() << "FnSpecialization: no possible constants found\n"); + SpecializationMap Specializations; + if (!calculateGains(F, Cost, Specializations)) { + LLVM_DEBUG(dbgs() << "FnSpecialization: No possible constants found\n"); continue; } - for (SpecializationInfo &S : Specializations) { - specializeFunction(F, S, WorkList); - Changed = true; - } + specializeFunction(Specializations, WorkList); + Changed = true; } updateSpecializedFuncs(Candidates, WorkList); @@ -395,14 +389,12 @@ return Clone; } - /// This function decides whether it's worthwhile to specialize function \p F - /// based on the known constant values its arguments can take on, i.e. it - /// calculates a gain and returns a list of actual arguments that are deemed - /// profitable to specialize. Specialization is performed on the first - /// interesting argument. Specializations based on additional arguments will - /// be evaluated on following iterations of the main IPSCCP solve loop. - void calculateGains(Function *F, InstructionCost Cost, - SpecializationList &WorkList) { + /// This function decides whether it's worthwhile to specialize function + /// \p F based on the known constant values its arguments can take on. + /// + /// \returns true if specializations were found. + bool calculateGains(Function *F, InstructionCost Cost, + SpecializationMap &Specializations) { // Determine if we should specialize the function based on the values the // argument can take on. If specialization is not profitable, we continue // on to the next argument. @@ -417,49 +409,61 @@ continue; } - for (auto *ActualArg : ActualArgs) { - InstructionCost Gain = - ForceFunctionSpecialization - ? 1 - : getSpecializationBonus(&FormalArg, ActualArg) - Cost; + for (auto &Entry : ActualArgs) { + CallBase *Call = Entry.first; + Constant *ActualArg = Entry.second; - if (Gain <= 0) - continue; - WorkList.push_back({&FormalArg, ActualArg, Gain}); + SpecializationInfo &S = Specializations[Call]; + if (!ForceFunctionSpecialization) + S.Gain += getSpecializationBonus(&FormalArg, ActualArg); + S.Args.push_back({&FormalArg, ActualArg}); } + } - if (WorkList.empty()) - continue; + if (!ForceFunctionSpecialization) { + // Account the specialization Cost. + for (auto &Entry : Specializations) + Entry.second.Gain -= Cost; - // Sort the candidates in descending order. - llvm::stable_sort(WorkList, [](const SpecializationInfo &L, - const SpecializationInfo &R) { - return L.Gain > R.Gain; + // Remove unprofitable specializations. + Specializations.remove_if([](const auto &Entry) { + return Entry.second.Gain <= 0; }); - // Truncate the worklist to 'MaxClonesThreshold' candidates if - // necessary. - if (WorkList.size() > MaxClonesThreshold) { - LLVM_DEBUG(dbgs() << "FnSpecialization: Number of candidates exceed " - << "the maximum number of clones threshold.\n" - << "FnSpecialization: Truncating worklist to " - << MaxClonesThreshold << " candidates.\n"); - WorkList.erase(WorkList.begin() + MaxClonesThreshold, WorkList.end()); - } + // Sort the candidates in ascending order. + llvm::stable_sort(Specializations, [](const auto &L, + const auto &R) { + return L.second.Gain < R.second.Gain; + }); + } - LLVM_DEBUG(dbgs() << "FnSpecialization: Specializations for function " - << F->getName() << "\n"; - for (SpecializationInfo &S : WorkList) { - dbgs() << "FnSpecialization: FormalArg = " - << S.Arg.Formal->getNameOrAsOperand() - << ", ActualArg = " - << S.Arg.Actual->getNameOrAsOperand() - << ", Gain = " << S.Gain << "\n"; - }); + //Truncate the worklist to 'MaxClonesThreshold' candidates if necessary. + if (Specializations.size() > MaxClonesThreshold) { + LLVM_DEBUG(dbgs() << "FnSpecialization: Number of candidates exceed " + << "the maximum number of clones threshold.\n" + << "FnSpecialization: Truncating worklist to " + << MaxClonesThreshold << " candidates.\n"); - // FIXME: Only one argument per function. - break; + Specializations.remove_if([&Specializations](const auto &Entry) { + auto Iter = Specializations.find(Entry.first); + return std::distance(Iter, Specializations.end()) > MaxClonesThreshold; + }); } + + LLVM_DEBUG(dbgs() << "FnSpecialization: Specializations for function " + << F->getName() << "\n"; + for (auto &Entry : Specializations) { + SpecializationInfo &S = Entry.second; + + dbgs() << "FnSpecialization: Gain = " << S.Gain << "\n"; + for (ArgInfo &Arg : S.Args) + dbgs() << "FnSpecialization: - FormalArg = " + << Arg.Formal->getNameOrAsOperand() + << ", ActualArg = " + << Arg.Actual->getNameOrAsOperand() << "\n"; + }); + + return !Specializations.empty(); } bool isCandidateFunction(Function *F) { @@ -486,32 +490,37 @@ return true; } - void specializeFunction(Function *F, SpecializationInfo &S, + void specializeFunction(SpecializationMap &Specializations, FuncList &WorkList) { - ValueToValueMapTy Mappings; - Function *Clone = cloneCandidateFunction(F, Mappings); - - // Rewrite calls to the function so that they call the clone instead. - rewriteCallSites(Clone, S.Arg, Mappings); - - // Initialize the lattice state of the arguments of the function clone, - // marking the argument on which we specialized the function constant - // with the given value. - Solver.markArgInFuncSpecialization(Clone, S.Arg); - - // Mark all the specialized functions - WorkList.push_back(Clone); - NbFunctionsSpecialized++; - - // If the function has been completely specialized, the original function - // is no longer needed. Mark it unreachable. - if (F->getNumUses() == 0 || all_of(F->users(), [F](User *U) { - if (auto *CS = dyn_cast(U)) - return CS->getFunction() == F; - return false; - })) { - Solver.markFunctionUnreachable(F); - FullySpecialized.insert(F); + for (auto &Entry : Specializations) { + Function *F = Entry.first->getCalledFunction(); + SpecializationInfo &S = Entry.second; + + ValueToValueMapTy Mappings; + Function *Clone = cloneCandidateFunction(F, Mappings); + + // Rewrite calls to the function so that they call the clone instead. + rewriteCallSites(Clone, S.Args, Mappings); + + // Initialize the lattice state of the arguments of the function clone, + // marking the argument on which we specialized the function constant + // with the given value. + Solver.markArgInFuncSpecialization(Clone, S.Args); + + // Mark all the specialized functions + WorkList.push_back(Clone); + NbFunctionsSpecialized++; + + // If the function has been completely specialized, the original function + // is no longer needed. Mark it unreachable. + if (F->getNumUses() == 0 || all_of(F->users(), [F](User *U) { + if (auto *CS = dyn_cast(U)) + return CS->getFunction() == F; + return false; + })) { + Solver.markFunctionUnreachable(F); + FullySpecialized.insert(F); + } } } @@ -725,23 +734,24 @@ if (isa(V) && (Solver.getLatticeValueFor(V).isConstant() || EnableSpecializationForLiteralConstant)) - Constants.push_back(cast(V)); + Constants.push_back(std::make_pair(&CS, cast(V))); } } /// Rewrite calls to function \p F to call function \p Clone instead. /// /// This function modifies calls to function \p F as long as the actual - /// argument matches the one in \p Arg. Note that for recursive calls we - /// need to compare against the cloned formal argument. + /// arguments match those in \p Args. Note that for recursive calls we + /// need to compare against the cloned formal arguments. /// /// Callsites that have been marked with the MinSize function attribute won't /// be specialized and rewritten. - void rewriteCallSites(Function *Clone, const ArgInfo &Arg, + void rewriteCallSites(Function *Clone, const SmallVectorImpl &Args, ValueToValueMapTy &Mappings) { - Function *F = Arg.Formal->getParent(); - unsigned ArgNo = Arg.Formal->getArgNo(); - SmallVector CallSitesToRewrite; + assert(!Args.empty() && "Specialization without arguments"); + Function *F = Args[0].Formal->getParent(); + + SmallVector CallSitesToRewrite; for (auto *U : F->users()) { if (!isa(U) && !isa(U)) continue; @@ -761,9 +771,13 @@ << *CS << "\n"); if (/* recursive call */ (CS->getFunction() == Clone && - CS->getArgOperand(ArgNo) == Mappings[Arg.Formal]) || + all_of(Args, [CS, &Mappings](const ArgInfo &Arg) { + unsigned ArgNo = Arg.Formal->getArgNo(); + return CS->getArgOperand(ArgNo) == Mappings[Arg.Formal]; })) || /* normal call */ - CS->getArgOperand(ArgNo) == Arg.Actual) { + all_of(Args, [CS](const ArgInfo &Arg) { + unsigned ArgNo = Arg.Formal->getArgNo(); + return CS->getArgOperand(ArgNo) == Arg.Actual; })) { CS->setCalledFunction(Clone); Solver.markOverdefined(CS); } diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -450,7 +450,8 @@ return TrackingIncomingArguments; } - void markArgInFuncSpecialization(Function *F, const ArgInfo &Arg); + void markArgInFuncSpecialization(Function *F, + const SmallVectorImpl &Args); void markFunctionUnreachable(Function *F) { for (auto &BB : *F) @@ -524,21 +525,24 @@ return nullptr; } -void SCCPInstVisitor::markArgInFuncSpecialization(Function *F, - const ArgInfo &Arg) { - assert(F->arg_size() == Arg.Formal->getParent()->arg_size() && +void SCCPInstVisitor::markArgInFuncSpecialization( + Function *F, const SmallVectorImpl &Args) { + assert(!Args.empty() && "Specialization without arguments"); + assert(F->arg_size() == Args[0].Formal->getParent()->arg_size() && "Functions should have the same number of arguments"); + auto Iter = Args.begin(); Argument *NewArg = F->arg_begin(); - Argument *OldArg = Arg.Formal->getParent()->arg_begin(); + Argument *OldArg = Args[0].Formal->getParent()->arg_begin(); for (auto End = F->arg_end(); NewArg != End; ++NewArg, ++OldArg) { LLVM_DEBUG(dbgs() << "SCCP: Marking argument " << NewArg->getNameOrAsOperand() << "\n"); - if (OldArg == Arg.Formal) { + if (OldArg == (*Iter).Formal) { // Mark the argument constants in the new function. - markConstant(NewArg, Arg.Actual); + markConstant(NewArg, (*Iter).Actual); + ++Iter; } else if (ValueState.count(OldArg)) { // For the remaining arguments in the new function, copy the lattice state // over from the old function. @@ -1717,8 +1721,9 @@ return Visitor->getArgumentTrackedFunctions(); } -void SCCPSolver::markArgInFuncSpecialization(Function *F, const ArgInfo &Arg) { - Visitor->markArgInFuncSpecialization(F, Arg); +void SCCPSolver::markArgInFuncSpecialization( + Function *F, const SmallVectorImpl &Args) { + Visitor->markArgInFuncSpecialization(F, Args); } void SCCPSolver::markFunctionUnreachable(Function *F) { diff --git a/llvm/test/Transforms/FunctionSpecialization/function-specialization4.ll b/llvm/test/Transforms/FunctionSpecialization/function-specialization4.ll --- a/llvm/test/Transforms/FunctionSpecialization/function-specialization4.ll +++ b/llvm/test/Transforms/FunctionSpecialization/function-specialization4.ll @@ -46,7 +46,7 @@ ; CHECK-NEXT: entry: ; CHECK-NEXT: %0 = load i32, i32* @A, align 4 ; CHECK-NEXT: %add = add nsw i32 %x, %0 -; CHECK-NEXT: %1 = load i32, i32* %c, align 4 +; CHECK-NEXT: %1 = load i32, i32* @C, align 4 ; CHECK-NEXT: %add1 = add nsw i32 %add, %1 ; CHECK-NEXT: ret i32 %add1 ; CHECK-NEXT: } @@ -55,7 +55,7 @@ ; CHECK-NEXT: entry: ; CHECK-NEXT: %0 = load i32, i32* @B, align 4 ; CHECK-NEXT: %add = add nsw i32 %x, %0 -; CHECK-NEXT: %1 = load i32, i32* %c, align 4 +; CHECK-NEXT: %1 = load i32, i32* @D, align 4 ; CHECK-NEXT: %add1 = add nsw i32 %add, %1 ; CHECK-NEXT: ret i32 %add1 ; CHECK-NEXT: } diff --git a/llvm/test/Transforms/FunctionSpecialization/specialize-multiple-arguments.ll b/llvm/test/Transforms/FunctionSpecialization/specialize-multiple-arguments.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/FunctionSpecialization/specialize-multiple-arguments.ll @@ -0,0 +1,172 @@ +; RUN: opt -function-specialization -func-specialization-max-clones=0 -func-specialization-size-threshold=14 -S < %s | FileCheck %s --check-prefix=NONE +; RUN: opt -function-specialization -func-specialization-max-clones=1 -func-specialization-size-threshold=14 -S < %s | FileCheck %s --check-prefixes=ONE +; RUN: opt -function-specialization -func-specialization-max-clones=2 -func-specialization-size-threshold=14 -S < %s | FileCheck %s --check-prefix=TWO +; RUN: opt -function-specialization -func-specialization-max-clones=3 -func-specialization-size-threshold=14 -S < %s | FileCheck %s --check-prefix=THREE + +define i64 @main(i64 %x, i64 %y, i1 %flag) { +; NONE-LABEL: @main( +; NONE-NEXT: entry: +; NONE-NEXT: br i1 [[FLAG:%.*]], label [[PLUS:%.*]], label [[MINUS:%.*]] +; NONE: plus: +; NONE-NEXT: [[TMP0:%.*]] = call i64 @compute(i64 [[X:%.*]], i64 [[Y:%.*]], i64 (i64, i64)* @power, i64 (i64, i64)* @mul) +; NONE-NEXT: br label [[MERGE:%.*]] +; NONE: minus: +; NONE-NEXT: [[TMP1:%.*]] = call i64 @compute(i64 [[X]], i64 [[Y]], i64 (i64, i64)* @minus, i64 (i64, i64)* @power) +; NONE-NEXT: br label [[MERGE]] +; NONE: merge: +; NONE-NEXT: [[TMP2:%.*]] = phi i64 [ [[TMP0]], [[PLUS]] ], [ [[TMP1]], [[MINUS]] ] +; NONE-NEXT: [[TMP3:%.*]] = call i64 @compute(i64 [[TMP2]], i64 42, i64 (i64, i64)* @plus, i64 (i64, i64)* @minus) +; NONE-NEXT: ret i64 [[TMP3]] +; +; ONE-LABEL: @main( +; ONE-NEXT: entry: +; ONE-NEXT: br i1 [[FLAG:%.*]], label [[PLUS:%.*]], label [[MINUS:%.*]] +; ONE: plus: +; ONE-NEXT: [[TMP0:%.*]] = call i64 @compute(i64 [[X:%.*]], i64 [[Y:%.*]], i64 (i64, i64)* @power, i64 (i64, i64)* @mul) +; ONE-NEXT: br label [[MERGE:%.*]] +; ONE: minus: +; ONE-NEXT: [[TMP1:%.*]] = call i64 @compute(i64 [[X]], i64 [[Y]], i64 (i64, i64)* @minus, i64 (i64, i64)* @power) +; ONE-NEXT: br label [[MERGE]] +; ONE: merge: +; ONE-NEXT: [[TMP2:%.*]] = phi i64 [ [[TMP0]], [[PLUS]] ], [ [[TMP1]], [[MINUS]] ] +; ONE-NEXT: [[TMP3:%.*]] = call i64 @compute.1(i64 [[TMP2]], i64 42, i64 (i64, i64)* @plus, i64 (i64, i64)* @minus) +; ONE-NEXT: ret i64 [[TMP3]] +; +; TWO-LABEL: @main( +; TWO-NEXT: entry: +; TWO-NEXT: br i1 [[FLAG:%.*]], label [[PLUS:%.*]], label [[MINUS:%.*]] +; TWO: plus: +; TWO-NEXT: [[TMP0:%.*]] = call i64 @compute(i64 [[X:%.*]], i64 [[Y:%.*]], i64 (i64, i64)* @power, i64 (i64, i64)* @mul) +; TWO-NEXT: br label [[MERGE:%.*]] +; TWO: minus: +; TWO-NEXT: [[TMP1:%.*]] = call i64 @compute.1(i64 [[X]], i64 [[Y]], i64 (i64, i64)* @minus, i64 (i64, i64)* @power) +; TWO-NEXT: br label [[MERGE]] +; TWO: merge: +; TWO-NEXT: [[TMP2:%.*]] = phi i64 [ [[TMP0]], [[PLUS]] ], [ [[TMP1]], [[MINUS]] ] +; TWO-NEXT: [[TMP3:%.*]] = call i64 @compute.2(i64 [[TMP2]], i64 42, i64 (i64, i64)* @plus, i64 (i64, i64)* @minus) +; TWO-NEXT: ret i64 [[TMP3]] +; +; THREE-LABEL: @main( +; THREE-NEXT: entry: +; THREE-NEXT: br i1 [[FLAG:%.*]], label [[PLUS:%.*]], label [[MINUS:%.*]] +; THREE: plus: +; THREE-NEXT: [[TMP0:%.*]] = call i64 @compute.1(i64 [[X:%.*]], i64 [[Y:%.*]], i64 (i64, i64)* @power, i64 (i64, i64)* @mul) +; THREE-NEXT: br label [[MERGE:%.*]] +; THREE: minus: +; THREE-NEXT: [[TMP1:%.*]] = call i64 @compute.2(i64 [[X]], i64 [[Y]], i64 (i64, i64)* @minus, i64 (i64, i64)* @power) +; THREE-NEXT: br label [[MERGE]] +; THREE: merge: +; THREE-NEXT: [[TMP2:%.*]] = phi i64 [ [[TMP0]], [[PLUS]] ], [ [[TMP1]], [[MINUS]] ] +; THREE-NEXT: [[TMP3:%.*]] = call i64 @compute.3(i64 [[TMP2]], i64 42, i64 (i64, i64)* @plus, i64 (i64, i64)* @minus) +; THREE-NEXT: ret i64 [[TMP3]] +; +entry: + br i1 %flag, label %plus, label %minus + +plus: + %tmp0 = call i64 @compute(i64 %x, i64 %y, i64 (i64, i64)* @power, i64 (i64, i64)* @mul) + br label %merge + +minus: + %tmp1 = call i64 @compute(i64 %x, i64 %y, i64 (i64, i64)* @minus, i64 (i64, i64)* @power) + br label %merge + +merge: + %tmp2 = phi i64 [ %tmp0, %plus ], [ %tmp1, %minus] + %tmp3 = call i64 @compute(i64 %tmp2, i64 42, i64 (i64, i64)* @plus, i64 (i64, i64)* @minus) + ret i64 %tmp3 +} + +; THREE-NOT: define internal i64 @compute +; +; THREE-LABEL: define internal i64 @compute.1(i64 %x, i64 %y, i64 (i64, i64)* %binop1, i64 (i64, i64)* %binop2) { +; THREE-NEXT: entry: +; THREE-NEXT: [[TMP0:%.+]] = call i64 @power(i64 %x, i64 %y) +; THREE-NEXT: [[TMP1:%.+]] = call i64 @mul(i64 %x, i64 %y) +; THREE-NEXT: [[TMP2:%.+]] = add i64 [[TMP0]], [[TMP1]] +; THREE-NEXT: [[TMP3:%.+]] = sdiv i64 [[TMP2]], %x +; THREE-NEXT: [[TMP4:%.+]] = sub i64 [[TMP3]], %y +; THREE-NEXT: [[TMP5:%.+]] = mul i64 [[TMP4]], 2 +; THREE-NEXT: ret i64 [[TMP5]] +; THREE-NEXT: } +; +; THREE-LABEL: define internal i64 @compute.2(i64 %x, i64 %y, i64 (i64, i64)* %binop1, i64 (i64, i64)* %binop2) { +; THREE-NEXT: entry: +; THREE-NEXT: [[TMP0:%.+]] = call i64 @minus(i64 %x, i64 %y) +; THREE-NEXT: [[TMP1:%.+]] = call i64 @power(i64 %x, i64 %y) +; THREE-NEXT: [[TMP2:%.+]] = add i64 [[TMP0]], [[TMP1]] +; THREE-NEXT: [[TMP3:%.+]] = sdiv i64 [[TMP2]], %x +; THREE-NEXT: [[TMP4:%.+]] = sub i64 [[TMP3]], %y +; THREE-NEXT: [[TMP5:%.+]] = mul i64 [[TMP4]], 2 +; THREE-NEXT: ret i64 [[TMP5]] +; THREE-NEXT: } +; +; THREE-LABEL: define internal i64 @compute.3(i64 %x, i64 %y, i64 (i64, i64)* %binop1, i64 (i64, i64)* %binop2) { +; THREE-NEXT: entry: +; THREE-NEXT: [[TMP0:%.+]] = call i64 @plus(i64 %x, i64 %y) +; THREE-NEXT: [[TMP1:%.+]] = call i64 @minus(i64 %x, i64 %y) +; THREE-NEXT: [[TMP2:%.+]] = add i64 [[TMP0]], [[TMP1]] +; THREE-NEXT: [[TMP3:%.+]] = sdiv i64 [[TMP2]], %x +; THREE-NEXT: [[TMP4:%.+]] = sub i64 [[TMP3]], %y +; THREE-NEXT: [[TMP5:%.+]] = mul i64 [[TMP4]], 2 +; THREE-NEXT: ret i64 [[TMP5]] +; THREE-NEXT: } +; +define internal i64 @compute(i64 %x, i64 %y, i64 (i64, i64)* %binop1, i64 (i64, i64)* %binop2) { +entry: + %tmp0 = call i64 %binop1(i64 %x, i64 %y) + %tmp1 = call i64 %binop2(i64 %x, i64 %y) + %add = add i64 %tmp0, %tmp1 + %div = sdiv i64 %add, %x + %sub = sub i64 %div, %y + %mul = mul i64 %sub, 2 + ret i64 %mul +} + +define internal i64 @plus(i64 %x, i64 %y) { +entry: + %tmp0 = add i64 %x, %y + ret i64 %tmp0 +} + +define internal i64 @minus(i64 %x, i64 %y) { +entry: + %tmp0 = sub i64 %x, %y + ret i64 %tmp0 +} + +define internal i64 @mul(i64 %x, i64 %n) { +entry: + %cmp6 = icmp sgt i64 %n, 1 + br i1 %cmp6, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + %x.addr.0.lcssa = phi i64 [ %x, %entry ], [ %add, %for.body ] + ret i64 %x.addr.0.lcssa + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 1, %entry ] + %x.addr.07 = phi i64 [ %add, %for.body ], [ %x, %entry ] + %add = shl nsw i64 %x.addr.07, 1 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +} + +define internal i64 @power(i64 %x, i64 %n) { +entry: + %cmp6 = icmp sgt i64 %n, 1 + br i1 %cmp6, label %for.body, label %for.cond.cleanup + +for.cond.cleanup: ; preds = %for.body, %entry + %x.addr.0.lcssa = phi i64 [ %x, %entry ], [ %mul, %for.body ] + ret i64 %x.addr.0.lcssa + +for.body: ; preds = %entry, %for.body + %indvars.iv = phi i64 [ %indvars.iv.next, %for.body ], [ 1, %entry ] + %x.addr.07 = phi i64 [ %mul, %for.body ], [ %x, %entry ] + %mul = mul nsw i64 %x.addr.07, %x.addr.07 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n + br i1 %exitcond.not, label %for.cond.cleanup, label %for.body +}