blob: 559d660897d9dc283385176ec4b6b28b49310230 [file] [log] [blame]
Hal Finkel96c2d4d2012-06-08 15:38:21 +00001//===-- PPCCTRLoops.cpp - Identify and generate CTR loops -----------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Hal Finkel96c2d4d2012-06-08 15:38:21 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This pass identifies loops where we can generate the PPC branch instructions
10// that decrement and test the count register (CTR) (bdnz and friends).
Hal Finkel96c2d4d2012-06-08 15:38:21 +000011//
12// The pattern that defines the induction variable can changed depending on
13// prior optimizations. For example, the IndVarSimplify phase run by 'opt'
14// normalizes induction variables, and the Loop Strength Reduction pass
15// run by 'llc' may also make changes to the induction variable.
Hal Finkel96c2d4d2012-06-08 15:38:21 +000016//
17// Criteria for CTR loops:
18// - Countable loops (w/ ind. var for a trip count)
Hal Finkel96c2d4d2012-06-08 15:38:21 +000019// - Try inner-most loops first
20// - No nested CTR loops.
21// - No function calls in loops.
22//
Hal Finkel96c2d4d2012-06-08 15:38:21 +000023//===----------------------------------------------------------------------===//
24
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000025#include "PPC.h"
Eric Christopherb16eacf2017-06-29 23:28:45 +000026#include "PPCSubtarget.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000027#include "PPCTargetMachine.h"
Lei Huang0724fea2017-10-12 16:43:33 +000028#include "PPCTargetTransformInfo.h"
Hal Finkel25c19922013-05-15 21:37:41 +000029#include "llvm/ADT/STLExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000030#include "llvm/ADT/Statistic.h"
Lei Huang0724fea2017-10-12 16:43:33 +000031#include "llvm/Analysis/AssumptionCache.h"
Nemanja Ivanovic2139e992018-05-02 22:56:04 +000032#include "llvm/Analysis/CFG.h"
Lei Huang0724fea2017-10-12 16:43:33 +000033#include "llvm/Analysis/CodeMetrics.h"
Hal Finkel25c19922013-05-15 21:37:41 +000034#include "llvm/Analysis/LoopInfo.h"
Nemanja Ivanovic2139e992018-05-02 22:56:04 +000035#include "llvm/Analysis/LoopIterator.h"
Hal Finkel25c19922013-05-15 21:37:41 +000036#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Lei Huang0724fea2017-10-12 16:43:33 +000038#include "llvm/Analysis/TargetTransformInfo.h"
David Blaikie31b98d22018-06-04 21:23:21 +000039#include "llvm/Transforms/Utils/Local.h"
Eric Christopherb16eacf2017-06-29 23:28:45 +000040#include "llvm/CodeGen/TargetPassConfig.h"
Lei Huang0724fea2017-10-12 16:43:33 +000041#include "llvm/CodeGen/TargetSchedule.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000042#include "llvm/IR/Constants.h"
Hal Finkel25c19922013-05-15 21:37:41 +000043#include "llvm/IR/DerivedTypes.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000044#include "llvm/IR/Dominators.h"
Hal Finkel2f474f02013-05-18 09:20:39 +000045#include "llvm/IR/InlineAsm.h"
Hal Finkel25c19922013-05-15 21:37:41 +000046#include "llvm/IR/Instructions.h"
47#include "llvm/IR/IntrinsicInst.h"
48#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000049#include "llvm/IR/ValueHandle.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/PassSupport.h"
Hal Finkel25c19922013-05-15 21:37:41 +000051#include "llvm/Support/CommandLine.h"
Hal Finkel96c2d4d2012-06-08 15:38:21 +000052#include "llvm/Support/Debug.h"
53#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000054#include "llvm/Transforms/Scalar.h"
David Blaikiea373d182018-03-28 17:44:36 +000055#include "llvm/Transforms/Utils.h"
Hal Finkel25c19922013-05-15 21:37:41 +000056#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hal Finkela969df82013-05-20 20:46:30 +000057#include "llvm/Transforms/Utils/LoopUtils.h"
Hal Finkel25c19922013-05-15 21:37:41 +000058
Hal Finkel8ca38842013-05-20 16:08:17 +000059#ifndef NDEBUG
60#include "llvm/CodeGen/MachineDominators.h"
61#include "llvm/CodeGen/MachineFunction.h"
62#include "llvm/CodeGen/MachineFunctionPass.h"
63#include "llvm/CodeGen/MachineRegisterInfo.h"
64#endif
65
Hal Finkel96c2d4d2012-06-08 15:38:21 +000066using namespace llvm;
67
Chandler Carruth84e68b22014-04-22 02:41:26 +000068#define DEBUG_TYPE "ctrloops"
69
Hal Finkel25c19922013-05-15 21:37:41 +000070#ifndef NDEBUG
71static cl::opt<int> CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1));
72#endif
73
Lei Huang0724fea2017-10-12 16:43:33 +000074// The latency of mtctr is only justified if there are more than 4
75// comparisons that will be removed as a result.
76static cl::opt<unsigned>
77SmallCTRLoopThreshold("min-ctr-loop-threshold", cl::init(4), cl::Hidden,
78 cl::desc("Loops with a constant trip count smaller than "
79 "this value will not use the count register."));
80
Hal Finkel96c2d4d2012-06-08 15:38:21 +000081STATISTIC(NumCTRLoops, "Number of loops converted to CTR loops");
82
83namespace {
Hal Finkel25c19922013-05-15 21:37:41 +000084 struct PPCCTRLoops : public FunctionPass {
85
86#ifndef NDEBUG
87 static int Counter;
88#endif
Hal Finkel96c2d4d2012-06-08 15:38:21 +000089
90 public:
Hal Finkel25c19922013-05-15 21:37:41 +000091 static char ID;
Hal Finkel96c2d4d2012-06-08 15:38:21 +000092
Eric Christopherb16eacf2017-06-29 23:28:45 +000093 PPCCTRLoops() : FunctionPass(ID) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000094 initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
95 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +000096
Craig Topper0d3fa922014-04-29 07:57:37 +000097 bool runOnFunction(Function &F) override;
Hal Finkel96c2d4d2012-06-08 15:38:21 +000098
Craig Topper0d3fa922014-04-29 07:57:37 +000099 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000100 AU.addRequired<LoopInfoWrapperPass>();
101 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000102 AU.addRequired<DominatorTreeWrapperPass>();
103 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000104 AU.addRequired<ScalarEvolutionWrapperPass>();
Lei Huang0724fea2017-10-12 16:43:33 +0000105 AU.addRequired<AssumptionCacheTracker>();
106 AU.addRequired<TargetTransformInfoWrapperPass>();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000107 }
108
109 private:
Eric Christopherb16eacf2017-06-29 23:28:45 +0000110 bool mightUseCTR(BasicBlock *BB);
Hal Finkel25c19922013-05-15 21:37:41 +0000111 bool convertToCTRLoop(Loop *L);
Hal Finkele6d7c282013-05-20 16:47:10 +0000112
Hal Finkel25c19922013-05-15 21:37:41 +0000113 private:
Eric Christopherb16eacf2017-06-29 23:28:45 +0000114 const PPCTargetMachine *TM;
115 const PPCSubtarget *STI;
116 const PPCTargetLowering *TLI;
117 const DataLayout *DL;
118 const TargetLibraryInfo *LibInfo;
Lei Huang0724fea2017-10-12 16:43:33 +0000119 const TargetTransformInfo *TTI;
Hal Finkel25c19922013-05-15 21:37:41 +0000120 LoopInfo *LI;
121 ScalarEvolution *SE;
Hal Finkel25c19922013-05-15 21:37:41 +0000122 DominatorTree *DT;
Justin Bogner843fb202015-12-15 19:40:57 +0000123 bool PreserveLCSSA;
Lei Huang0724fea2017-10-12 16:43:33 +0000124 TargetSchedModel SchedModel;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000125 };
126
127 char PPCCTRLoops::ID = 0;
Hal Finkel25c19922013-05-15 21:37:41 +0000128#ifndef NDEBUG
129 int PPCCTRLoops::Counter = 0;
130#endif
Hal Finkel8ca38842013-05-20 16:08:17 +0000131
132#ifndef NDEBUG
133 struct PPCCTRLoopsVerify : public MachineFunctionPass {
134 public:
135 static char ID;
136
137 PPCCTRLoopsVerify() : MachineFunctionPass(ID) {
138 initializePPCCTRLoopsVerifyPass(*PassRegistry::getPassRegistry());
139 }
140
Craig Topper0d3fa922014-04-29 07:57:37 +0000141 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel8ca38842013-05-20 16:08:17 +0000142 AU.addRequired<MachineDominatorTree>();
143 MachineFunctionPass::getAnalysisUsage(AU);
144 }
145
Craig Topper0d3fa922014-04-29 07:57:37 +0000146 bool runOnMachineFunction(MachineFunction &MF) override;
Hal Finkel8ca38842013-05-20 16:08:17 +0000147
148 private:
149 MachineDominatorTree *MDT;
150 };
151
152 char PPCCTRLoopsVerify::ID = 0;
153#endif // NDEBUG
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000154} // end anonymous namespace
155
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +0000156INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
157 false, false)
Chandler Carruth73523022014-01-13 13:07:17 +0000158INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000159INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000160INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +0000161INITIALIZE_PASS_END(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
162 false, false)
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000163
Eric Christopherb16eacf2017-06-29 23:28:45 +0000164FunctionPass *llvm::createPPCCTRLoops() { return new PPCCTRLoops(); }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000165
Hal Finkel8ca38842013-05-20 16:08:17 +0000166#ifndef NDEBUG
167INITIALIZE_PASS_BEGIN(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
168 "PowerPC CTR Loops Verify", false, false)
169INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
170INITIALIZE_PASS_END(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
171 "PowerPC CTR Loops Verify", false, false)
172
173FunctionPass *llvm::createPPCCTRLoopsVerify() {
174 return new PPCCTRLoopsVerify();
175}
176#endif // NDEBUG
177
Hal Finkel25c19922013-05-15 21:37:41 +0000178bool PPCCTRLoops::runOnFunction(Function &F) {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000179 if (skipFunction(F))
180 return false;
181
Eric Christopherb16eacf2017-06-29 23:28:45 +0000182 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
183 if (!TPC)
184 return false;
185
186 TM = &TPC->getTM<PPCTargetMachine>();
187 STI = TM->getSubtargetImpl(F);
188 TLI = STI->getTargetLowering();
189
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000190 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000191 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth73523022014-01-13 13:07:17 +0000192 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Lei Huang0724fea2017-10-12 16:43:33 +0000193 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Mehdi Amini46a43552015-03-04 18:43:29 +0000194 DL = &F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000195 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
196 LibInfo = TLIP ? &TLIP->getTLI() : nullptr;
Justin Bogner843fb202015-12-15 19:40:57 +0000197 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
Chen Zheng19ce6712019-04-09 01:25:25 +0000198 SchedModel.init(STI);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000199
Hal Finkel25c19922013-05-15 21:37:41 +0000200 bool MadeChange = false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000201
Hal Finkel25c19922013-05-15 21:37:41 +0000202 for (LoopInfo::iterator I = LI->begin(), E = LI->end();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000203 I != E; ++I) {
Hal Finkel25c19922013-05-15 21:37:41 +0000204 Loop *L = *I;
205 if (!L->getParentLoop())
206 MadeChange |= convertToCTRLoop(L);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000207 }
208
Hal Finkel25c19922013-05-15 21:37:41 +0000209 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000210}
211
Hal Finkel22304042014-02-25 20:51:50 +0000212static bool isLargeIntegerTy(bool Is32Bit, Type *Ty) {
213 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Aaron Ballman3d69c5c2014-02-26 20:22:20 +0000214 return ITy->getBitWidth() > (Is32Bit ? 32U : 64U);
Hal Finkel22304042014-02-25 20:51:50 +0000215
216 return false;
217}
218
David Majnemerd0bcef22014-12-27 19:45:38 +0000219// Determining the address of a TLS variable results in a function call in
220// certain TLS models.
Eric Christopherb16eacf2017-06-29 23:28:45 +0000221static bool memAddrUsesCTR(const PPCTargetMachine &TM, const Value *MemAddr) {
David Majnemerd0bcef22014-12-27 19:45:38 +0000222 const auto *GV = dyn_cast<GlobalValue>(MemAddr);
Hal Finkel7d0e34e2015-10-28 23:43:00 +0000223 if (!GV) {
224 // Recurse to check for constants that refer to TLS global variables.
225 if (const auto *CV = dyn_cast<Constant>(MemAddr))
226 for (const auto &CO : CV->operands())
227 if (memAddrUsesCTR(TM, CO))
228 return true;
229
David Majnemerd0bcef22014-12-27 19:45:38 +0000230 return false;
Hal Finkel7d0e34e2015-10-28 23:43:00 +0000231 }
232
David Majnemerd0bcef22014-12-27 19:45:38 +0000233 if (!GV->isThreadLocal())
234 return false;
Eric Christopherb16eacf2017-06-29 23:28:45 +0000235 TLSModel::Model Model = TM.getTLSModel(GV);
David Majnemerd0bcef22014-12-27 19:45:38 +0000236 return Model == TLSModel::GeneralDynamic || Model == TLSModel::LocalDynamic;
237}
238
Eric Christopher56f481b2017-06-29 23:28:47 +0000239// Loop through the inline asm constraints and look for something that clobbers
240// ctr.
241static bool asmClobbersCTR(InlineAsm *IA) {
242 InlineAsm::ConstraintInfoVector CIV = IA->ParseConstraints();
243 for (unsigned i = 0, ie = CIV.size(); i < ie; ++i) {
244 InlineAsm::ConstraintInfo &C = CIV[i];
245 if (C.Type != InlineAsm::isInput)
246 for (unsigned j = 0, je = C.Codes.size(); j < je; ++j)
247 if (StringRef(C.Codes[j]).equals_lower("{ctr}"))
248 return true;
249 }
250 return false;
251}
252
Eric Christopherb16eacf2017-06-29 23:28:45 +0000253bool PPCCTRLoops::mightUseCTR(BasicBlock *BB) {
Hal Finkel5f587c52013-05-16 19:58:38 +0000254 for (BasicBlock::iterator J = BB->begin(), JE = BB->end();
255 J != JE; ++J) {
256 if (CallInst *CI = dyn_cast<CallInst>(J)) {
Eric Christopher56f481b2017-06-29 23:28:47 +0000257 // Inline ASM is okay, unless it clobbers the ctr register.
Hal Finkel2f474f02013-05-18 09:20:39 +0000258 if (InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000259 if (asmClobbersCTR(IA))
260 return true;
Hal Finkel2f474f02013-05-18 09:20:39 +0000261 continue;
262 }
263
Hal Finkel5f587c52013-05-16 19:58:38 +0000264 if (Function *F = CI->getCalledFunction()) {
265 // Most intrinsics don't become function calls, but some might.
266 // sin, cos, exp and log are always calls.
Hal Finkel0b371752016-03-27 05:40:56 +0000267 unsigned Opcode = 0;
Hal Finkel5f587c52013-05-16 19:58:38 +0000268 if (F->getIntrinsicID() != Intrinsic::not_intrinsic) {
269 switch (F->getIntrinsicID()) {
270 default: continue;
Eric Christopher71f6e2f2015-09-08 22:14:58 +0000271 // If we have a call to ppc_is_decremented_ctr_nonzero, or ppc_mtctr
272 // we're definitely using CTR.
273 case Intrinsic::ppc_is_decremented_ctr_nonzero:
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000274 case Intrinsic::ppc_mtctr:
275 return true;
Hal Finkel5f587c52013-05-16 19:58:38 +0000276
277// VisualStudio defines setjmp as _setjmp
278#if defined(_MSC_VER) && defined(setjmp) && \
279 !defined(setjmp_undefined_for_msvc)
280# pragma push_macro("setjmp")
281# undef setjmp
282# define setjmp_undefined_for_msvc
283#endif
284
285 case Intrinsic::setjmp:
286
287#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
288 // let's return it to _setjmp state
289# pragma pop_macro("setjmp")
290# undef setjmp_undefined_for_msvc
291#endif
292
293 case Intrinsic::longjmp:
Hal Finkel40f76d52013-07-17 05:35:44 +0000294
295 // Exclude eh_sjlj_setjmp; we don't need to exclude eh_sjlj_longjmp
296 // because, although it does clobber the counter register, the
297 // control can't then return to inside the loop unless there is also
298 // an eh_sjlj_setjmp.
299 case Intrinsic::eh_sjlj_setjmp:
300
Hal Finkel5f587c52013-05-16 19:58:38 +0000301 case Intrinsic::memcpy:
302 case Intrinsic::memmove:
303 case Intrinsic::memset:
304 case Intrinsic::powi:
305 case Intrinsic::log:
306 case Intrinsic::log2:
307 case Intrinsic::log10:
308 case Intrinsic::exp:
309 case Intrinsic::exp2:
310 case Intrinsic::pow:
311 case Intrinsic::sin:
312 case Intrinsic::cos:
313 return true;
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000314 case Intrinsic::copysign:
315 if (CI->getArgOperand(0)->getType()->getScalarType()->
316 isPPC_FP128Ty())
317 return true;
318 else
319 continue; // ISD::FCOPYSIGN is never a library call.
Hal Finkelcef9e522017-04-11 02:03:17 +0000320 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
321 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
322 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
323 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
324 case Intrinsic::rint: Opcode = ISD::FRINT; break;
325 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
326 case Intrinsic::round: Opcode = ISD::FROUND; break;
327 case Intrinsic::minnum: Opcode = ISD::FMINNUM; break;
328 case Intrinsic::maxnum: Opcode = ISD::FMAXNUM; break;
329 case Intrinsic::umul_with_overflow: Opcode = ISD::UMULO; break;
330 case Intrinsic::smul_with_overflow: Opcode = ISD::SMULO; break;
Hal Finkel5f587c52013-05-16 19:58:38 +0000331 }
332 }
333
334 // PowerPC does not use [US]DIVREM or other library calls for
335 // operations on regular types which are not otherwise library calls
336 // (i.e. soft float or atomics). If adapting for targets that do,
337 // additional care is required here.
338
David L. Jonesd21529f2017-01-23 23:16:46 +0000339 LibFunc Func;
Hal Finkel5f587c52013-05-16 19:58:38 +0000340 if (!F->hasLocalLinkage() && F->hasName() && LibInfo &&
341 LibInfo->getLibFunc(F->getName(), Func) &&
342 LibInfo->hasOptimizedCodeGen(Func)) {
343 // Non-read-only functions are never treated as intrinsics.
344 if (!CI->onlyReadsMemory())
345 return true;
346
347 // Conversion happens only for FP calls.
348 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
349 return true;
350
351 switch (Func) {
352 default: return true;
David L. Jonesd21529f2017-01-23 23:16:46 +0000353 case LibFunc_copysign:
354 case LibFunc_copysignf:
Hal Finkel5f587c52013-05-16 19:58:38 +0000355 continue; // ISD::FCOPYSIGN is never a library call.
David L. Jonesd21529f2017-01-23 23:16:46 +0000356 case LibFunc_copysignl:
Hal Finkel1cf48ab2013-08-19 23:35:24 +0000357 return true;
David L. Jonesd21529f2017-01-23 23:16:46 +0000358 case LibFunc_fabs:
359 case LibFunc_fabsf:
360 case LibFunc_fabsl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000361 continue; // ISD::FABS is never a library call.
David L. Jonesd21529f2017-01-23 23:16:46 +0000362 case LibFunc_sqrt:
363 case LibFunc_sqrtf:
364 case LibFunc_sqrtl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000365 Opcode = ISD::FSQRT; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000366 case LibFunc_floor:
367 case LibFunc_floorf:
368 case LibFunc_floorl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000369 Opcode = ISD::FFLOOR; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000370 case LibFunc_nearbyint:
371 case LibFunc_nearbyintf:
372 case LibFunc_nearbyintl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000373 Opcode = ISD::FNEARBYINT; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000374 case LibFunc_ceil:
375 case LibFunc_ceilf:
376 case LibFunc_ceill:
Hal Finkel5f587c52013-05-16 19:58:38 +0000377 Opcode = ISD::FCEIL; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000378 case LibFunc_rint:
379 case LibFunc_rintf:
380 case LibFunc_rintl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000381 Opcode = ISD::FRINT; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000382 case LibFunc_round:
383 case LibFunc_roundf:
384 case LibFunc_roundl:
Hal Finkel171817e2013-08-07 22:49:12 +0000385 Opcode = ISD::FROUND; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000386 case LibFunc_trunc:
387 case LibFunc_truncf:
388 case LibFunc_truncl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000389 Opcode = ISD::FTRUNC; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000390 case LibFunc_fmin:
391 case LibFunc_fminf:
392 case LibFunc_fminl:
Hal Finkel0b371752016-03-27 05:40:56 +0000393 Opcode = ISD::FMINNUM; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000394 case LibFunc_fmax:
395 case LibFunc_fmaxf:
396 case LibFunc_fmaxl:
Hal Finkel0b371752016-03-27 05:40:56 +0000397 Opcode = ISD::FMAXNUM; break;
Hal Finkel5f587c52013-05-16 19:58:38 +0000398 }
Hal Finkel0b371752016-03-27 05:40:56 +0000399 }
Hal Finkel5f587c52013-05-16 19:58:38 +0000400
Hal Finkel0b371752016-03-27 05:40:56 +0000401 if (Opcode) {
Sean Fertile33a17762018-01-09 03:03:41 +0000402 EVT EVTy =
403 TLI->getValueType(*DL, CI->getArgOperand(0)->getType(), true);
404
405 if (EVTy == MVT::Other)
Hal Finkel5f587c52013-05-16 19:58:38 +0000406 return true;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000407
Sean Fertile33a17762018-01-09 03:03:41 +0000408 if (TLI->isOperationLegalOrCustom(Opcode, EVTy))
Hal Finkel5f587c52013-05-16 19:58:38 +0000409 continue;
Sean Fertile33a17762018-01-09 03:03:41 +0000410 else if (EVTy.isVector() &&
411 TLI->isOperationLegalOrCustom(Opcode, EVTy.getScalarType()))
Hal Finkel5f587c52013-05-16 19:58:38 +0000412 continue;
413
414 return true;
415 }
416 }
417
418 return true;
419 } else if (isa<BinaryOperator>(J) &&
420 J->getType()->getScalarType()->isPPC_FP128Ty()) {
421 // Most operations on ppc_f128 values become calls.
422 return true;
423 } else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
424 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
425 CastInst *CI = cast<CastInst>(J);
426 if (CI->getSrcTy()->getScalarType()->isPPC_FP128Ty() ||
427 CI->getDestTy()->getScalarType()->isPPC_FP128Ty() ||
Eric Christopherb16eacf2017-06-29 23:28:45 +0000428 isLargeIntegerTy(!TM->isPPC64(), CI->getSrcTy()->getScalarType()) ||
429 isLargeIntegerTy(!TM->isPPC64(), CI->getDestTy()->getScalarType()))
Hal Finkel5f587c52013-05-16 19:58:38 +0000430 return true;
Eric Christopherb16eacf2017-06-29 23:28:45 +0000431 } else if (isLargeIntegerTy(!TM->isPPC64(),
Hal Finkel22304042014-02-25 20:51:50 +0000432 J->getType()->getScalarType()) &&
Hal Finkelfa5f6f72013-06-07 22:16:19 +0000433 (J->getOpcode() == Instruction::UDiv ||
434 J->getOpcode() == Instruction::SDiv ||
435 J->getOpcode() == Instruction::URem ||
436 J->getOpcode() == Instruction::SRem)) {
437 return true;
Eric Christopherb16eacf2017-06-29 23:28:45 +0000438 } else if (!TM->isPPC64() &&
Hal Finkelc4c6c872014-05-11 16:23:29 +0000439 isLargeIntegerTy(false, J->getType()->getScalarType()) &&
440 (J->getOpcode() == Instruction::Shl ||
441 J->getOpcode() == Instruction::AShr ||
442 J->getOpcode() == Instruction::LShr)) {
443 // Only on PPC32, for 128-bit integers (specifically not 64-bit
444 // integers), these might be runtime calls.
445 return true;
Hal Finkel5f587c52013-05-16 19:58:38 +0000446 } else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
447 // On PowerPC, indirect jumps use the counter register.
448 return true;
449 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
Eric Christopher79cc1e32014-09-02 22:28:02 +0000450 if (SI->getNumCases() + 1 >= (unsigned)TLI->getMinimumJumpTableEntries())
Hal Finkel5f587c52013-05-16 19:58:38 +0000451 return true;
452 }
Petar Jovanovic0b44f242016-03-17 17:11:33 +0000453
Nemanja Ivanovice54a9ee2018-02-22 03:02:41 +0000454 // FREM is always a call.
455 if (J->getOpcode() == Instruction::FRem)
456 return true;
457
Eric Christopherb16eacf2017-06-29 23:28:45 +0000458 if (STI->useSoftFloat()) {
Petar Jovanovic0b44f242016-03-17 17:11:33 +0000459 switch(J->getOpcode()) {
460 case Instruction::FAdd:
461 case Instruction::FSub:
462 case Instruction::FMul:
463 case Instruction::FDiv:
Petar Jovanovic0b44f242016-03-17 17:11:33 +0000464 case Instruction::FPTrunc:
465 case Instruction::FPExt:
466 case Instruction::FPToUI:
467 case Instruction::FPToSI:
468 case Instruction::UIToFP:
469 case Instruction::SIToFP:
470 case Instruction::FCmp:
471 return true;
472 }
473 }
474
David Majnemerd0bcef22014-12-27 19:45:38 +0000475 for (Value *Operand : J->operands())
Eric Christopherb16eacf2017-06-29 23:28:45 +0000476 if (memAddrUsesCTR(*TM, Operand))
David Majnemerd0bcef22014-12-27 19:45:38 +0000477 return true;
Hal Finkel5f587c52013-05-16 19:58:38 +0000478 }
479
480 return false;
481}
Hal Finkel25c19922013-05-15 21:37:41 +0000482bool PPCCTRLoops::convertToCTRLoop(Loop *L) {
483 bool MadeChange = false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000484
Lei Huang0724fea2017-10-12 16:43:33 +0000485 // Do not convert small short loops to CTR loop.
486 unsigned ConstTripCount = SE->getSmallConstantTripCount(L);
487 if (ConstTripCount && ConstTripCount < SmallCTRLoopThreshold) {
488 SmallPtrSet<const Value *, 32> EphValues;
489 auto AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
490 *L->getHeader()->getParent());
491 CodeMetrics::collectEphemeralValues(L, &AC, EphValues);
492 CodeMetrics Metrics;
493 for (BasicBlock *BB : L->blocks())
494 Metrics.analyzeBasicBlock(BB, *TTI, EphValues);
495 // 6 is an approximate latency for the mtctr instruction.
496 if (Metrics.NumInsts <= (6 * SchedModel.getIssueWidth()))
497 return false;
498 }
499
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000500 // Process nested loops first.
Hal Finkel25c19922013-05-15 21:37:41 +0000501 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) {
502 MadeChange |= convertToCTRLoop(*I);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000503 LLVM_DEBUG(dbgs() << "Nested loop converted\n");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000504 }
Hal Finkel25c19922013-05-15 21:37:41 +0000505
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000506 // If a nested loop has been converted, then we can't convert this loop.
Hal Finkel25c19922013-05-15 21:37:41 +0000507 if (MadeChange)
508 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000509
Nemanja Ivanovic2139e992018-05-02 22:56:04 +0000510 // Bail out if the loop has irreducible control flow.
511 LoopBlocksRPO RPOT(L);
512 RPOT.perform(LI);
513 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, *LI))
514 return false;
515
Hal Finkel25c19922013-05-15 21:37:41 +0000516#ifndef NDEBUG
517 // Stop trying after reaching the limit (if any).
518 int Limit = CTRLoopLimit;
519 if (Limit >= 0) {
520 if (Counter >= CTRLoopLimit)
Hal Finkel21f2a432013-03-18 17:40:44 +0000521 return false;
Hal Finkel25c19922013-05-15 21:37:41 +0000522 Counter++;
Hal Finkel21f2a432013-03-18 17:40:44 +0000523 }
Hal Finkel25c19922013-05-15 21:37:41 +0000524#endif
Hal Finkel21f2a432013-03-18 17:40:44 +0000525
Hal Finkel25c19922013-05-15 21:37:41 +0000526 // We don't want to spill/restore the counter register, and so we don't
527 // want to use the counter register if the loop contains calls.
528 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
Hal Finkel5f587c52013-05-16 19:58:38 +0000529 I != IE; ++I)
Eric Christopherb16eacf2017-06-29 23:28:45 +0000530 if (mightUseCTR(*I))
Hal Finkel5f587c52013-05-16 19:58:38 +0000531 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000532
Hal Finkel25c19922013-05-15 21:37:41 +0000533 SmallVector<BasicBlock*, 4> ExitingBlocks;
534 L->getExitingBlocks(ExitingBlocks);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000535
Hiroshi Inouec5ab1ab2018-02-05 12:25:29 +0000536 // If there is an exit edge known to be frequently taken,
537 // we should not transform this loop.
538 for (auto &BB : ExitingBlocks) {
539 Instruction *TI = BB->getTerminator();
540 if (!TI) continue;
541
542 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
Hiroshi Inouead48d2fe2018-02-06 11:34:16 +0000543 uint64_t TrueWeight = 0, FalseWeight = 0;
Hiroshi Inouec5ab1ab2018-02-05 12:25:29 +0000544 if (!BI->isConditional() ||
545 !BI->extractProfMetadata(TrueWeight, FalseWeight))
546 continue;
547
548 // If the exit path is more frequent than the loop path,
549 // we return here without further analysis for this loop.
550 bool TrueIsExit = !L->contains(BI->getSuccessor(0));
551 if (( TrueIsExit && FalseWeight < TrueWeight) ||
552 (!TrueIsExit && FalseWeight > TrueWeight))
553 return MadeChange;
554 }
555 }
556
Craig Topper062a2ba2014-04-25 05:30:21 +0000557 BasicBlock *CountedExitBlock = nullptr;
558 const SCEV *ExitCount = nullptr;
559 BranchInst *CountedExitBranch = nullptr;
Craig Topperaf0dea12013-07-04 01:31:24 +0000560 for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(),
Hal Finkel25c19922013-05-15 21:37:41 +0000561 IE = ExitingBlocks.end(); I != IE; ++I) {
562 const SCEV *EC = SE->getExitCount(L, *I);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000563 LLVM_DEBUG(dbgs() << "Exit Count for " << *L << " from block "
564 << (*I)->getName() << ": " << *EC << "\n");
Hal Finkel25c19922013-05-15 21:37:41 +0000565 if (isa<SCEVCouldNotCompute>(EC))
566 continue;
567 if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
568 if (ConstEC->getValue()->isZero())
569 continue;
570 } else if (!SE->isLoopInvariant(EC, L))
571 continue;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000572
Eric Christopherb16eacf2017-06-29 23:28:45 +0000573 if (SE->getTypeSizeInBits(EC->getType()) > (TM->isPPC64() ? 64 : 32))
Hal Finkel25e4a0d2013-07-01 19:34:59 +0000574 continue;
575
Nemanja Ivanovic2139e992018-05-02 22:56:04 +0000576 // If this exiting block is contained in a nested loop, it is not eligible
577 // for insertion of the branch-and-decrement since the inner loop would
578 // end up messing up the value in the CTR.
579 if (LI->getLoopFor(*I) != L)
580 continue;
581
Hal Finkel25c19922013-05-15 21:37:41 +0000582 // We now have a loop-invariant count of loop iterations (which is not the
583 // constant zero) for which we know that this loop will not exit via this
Hiroshi Inoue0f7f59f2018-06-13 08:54:13 +0000584 // existing block.
Hal Finkel6261c2d2012-06-16 20:34:07 +0000585
Hal Finkel25c19922013-05-15 21:37:41 +0000586 // We need to make sure that this block will run on every loop iteration.
587 // For this to be true, we must dominate all blocks with backedges. Such
588 // blocks are in-loop predecessors to the header block.
589 bool NotAlways = false;
590 for (pred_iterator PI = pred_begin(L->getHeader()),
591 PIE = pred_end(L->getHeader()); PI != PIE; ++PI) {
592 if (!L->contains(*PI))
593 continue;
594
595 if (!DT->dominates(*I, *PI)) {
596 NotAlways = true;
597 break;
598 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000599 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000600
Hal Finkel25c19922013-05-15 21:37:41 +0000601 if (NotAlways)
602 continue;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000603
Hal Finkel25c19922013-05-15 21:37:41 +0000604 // Make sure this blocks ends with a conditional branch.
605 Instruction *TI = (*I)->getTerminator();
606 if (!TI)
607 continue;
608
609 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
610 if (!BI->isConditional())
611 continue;
612
613 CountedExitBranch = BI;
614 } else
615 continue;
616
617 // Note that this block may not be the loop latch block, even if the loop
618 // has a latch block.
619 CountedExitBlock = *I;
620 ExitCount = EC;
621 break;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000622 }
623
Hal Finkel25c19922013-05-15 21:37:41 +0000624 if (!CountedExitBlock)
625 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000626
Hal Finkel25c19922013-05-15 21:37:41 +0000627 BasicBlock *Preheader = L->getLoopPreheader();
Hal Finkel5f587c52013-05-16 19:58:38 +0000628
629 // If we don't have a preheader, then insert one. If we already have a
630 // preheader, then we can use it (except if the preheader contains a use of
631 // the CTR register because some such uses might be reordered by the
632 // selection DAG after the mtctr instruction).
Eric Christopherb16eacf2017-06-29 23:28:45 +0000633 if (!Preheader || mightUseCTR(Preheader))
Alina Sbirleaf31eba62019-05-08 17:05:36 +0000634 Preheader = InsertPreheaderForLoop(L, DT, LI, nullptr, PreserveLCSSA);
Hal Finkel25c19922013-05-15 21:37:41 +0000635 if (!Preheader)
636 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000637
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000638 LLVM_DEBUG(dbgs() << "Preheader for exit count: " << Preheader->getName()
639 << "\n");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000640
Hal Finkel25c19922013-05-15 21:37:41 +0000641 // Insert the count into the preheader and replace the condition used by the
642 // selected branch.
643 MadeChange = true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000644
Eric Christopherb16eacf2017-06-29 23:28:45 +0000645 SCEVExpander SCEVE(*SE, *DL, "loopcnt");
Hal Finkel25c19922013-05-15 21:37:41 +0000646 LLVMContext &C = SE->getContext();
Eric Christopherb16eacf2017-06-29 23:28:45 +0000647 Type *CountType = TM->isPPC64() ? Type::getInt64Ty(C) : Type::getInt32Ty(C);
Hal Finkel25c19922013-05-15 21:37:41 +0000648 if (!ExitCount->getType()->isPointerTy() &&
649 ExitCount->getType() != CountType)
650 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000651 ExitCount = SE->getAddExpr(ExitCount, SE->getOne(CountType));
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000652 Value *ECValue =
653 SCEVE.expandCodeFor(ExitCount, CountType, Preheader->getTerminator());
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000654
Hal Finkel25c19922013-05-15 21:37:41 +0000655 IRBuilder<> CountBuilder(Preheader->getTerminator());
656 Module *M = Preheader->getParent()->getParent();
James Y Knight7976eb52019-02-01 20:43:25 +0000657 Function *MTCTRFunc =
658 Intrinsic::getDeclaration(M, Intrinsic::ppc_mtctr, CountType);
Hal Finkel25c19922013-05-15 21:37:41 +0000659 CountBuilder.CreateCall(MTCTRFunc, ECValue);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000660
Hal Finkel25c19922013-05-15 21:37:41 +0000661 IRBuilder<> CondBuilder(CountedExitBranch);
James Y Knight7976eb52019-02-01 20:43:25 +0000662 Function *DecFunc =
663 Intrinsic::getDeclaration(M, Intrinsic::ppc_is_decremented_ctr_nonzero);
David Blaikieff6409d2015-05-18 22:13:54 +0000664 Value *NewCond = CondBuilder.CreateCall(DecFunc, {});
Hal Finkel25c19922013-05-15 21:37:41 +0000665 Value *OldCond = CountedExitBranch->getCondition();
666 CountedExitBranch->setCondition(NewCond);
667
668 // The false branch must exit the loop.
669 if (!L->contains(CountedExitBranch->getSuccessor(0)))
670 CountedExitBranch->swapSuccessors();
671
672 // The old condition may be dead now, and may have even created a dead PHI
673 // (the original induction variable).
674 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
Sean Fertiled44cb182017-07-05 17:57:57 +0000675 // Run through the basic blocks of the loop and see if any of them have dead
676 // PHIs that can be removed.
677 for (auto I : L->blocks())
678 DeleteDeadPHIs(I);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000679
680 ++NumCTRLoops;
Hal Finkel25c19922013-05-15 21:37:41 +0000681 return MadeChange;
682}
683
Hal Finkel8ca38842013-05-20 16:08:17 +0000684#ifndef NDEBUG
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000685static bool clobbersCTR(const MachineInstr &MI) {
686 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
687 const MachineOperand &MO = MI.getOperand(i);
Hal Finkel8ca38842013-05-20 16:08:17 +0000688 if (MO.isReg()) {
689 if (MO.isDef() && (MO.getReg() == PPC::CTR || MO.getReg() == PPC::CTR8))
690 return true;
691 } else if (MO.isRegMask()) {
692 if (MO.clobbersPhysReg(PPC::CTR) || MO.clobbersPhysReg(PPC::CTR8))
693 return true;
694 }
695 }
696
697 return false;
698}
699
700static bool verifyCTRBranch(MachineBasicBlock *MBB,
701 MachineBasicBlock::iterator I) {
702 MachineBasicBlock::iterator BI = I;
703 SmallSet<MachineBasicBlock *, 16> Visited;
704 SmallVector<MachineBasicBlock *, 8> Preds;
705 bool CheckPreds;
706
707 if (I == MBB->begin()) {
708 Visited.insert(MBB);
709 goto queue_preds;
710 } else
711 --I;
712
713check_block:
714 Visited.insert(MBB);
715 if (I == MBB->end())
716 goto queue_preds;
717
718 CheckPreds = true;
719 for (MachineBasicBlock::iterator IE = MBB->begin();; --I) {
720 unsigned Opc = I->getOpcode();
Hal Finkel0859ef22013-05-20 16:08:37 +0000721 if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
Hal Finkel8ca38842013-05-20 16:08:17 +0000722 CheckPreds = false;
723 break;
724 }
725
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000726 if (I != BI && clobbersCTR(*I)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000727 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " (" << MBB->getFullName()
728 << ") instruction " << *I
729 << " clobbers CTR, invalidating "
730 << printMBBReference(*BI->getParent()) << " ("
731 << BI->getParent()->getFullName() << ") instruction "
732 << *BI << "\n");
Hal Finkel8ca38842013-05-20 16:08:17 +0000733 return false;
734 }
735
736 if (I == IE)
737 break;
738 }
739
740 if (!CheckPreds && Preds.empty())
741 return true;
742
743 if (CheckPreds) {
744queue_preds:
745 if (MachineFunction::iterator(MBB) == MBB->getParent()->begin()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000746 LLVM_DEBUG(dbgs() << "Unable to find a MTCTR instruction for "
747 << printMBBReference(*BI->getParent()) << " ("
748 << BI->getParent()->getFullName() << ") instruction "
749 << *BI << "\n");
Hal Finkel8ca38842013-05-20 16:08:17 +0000750 return false;
751 }
752
753 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
754 PIE = MBB->pred_end(); PI != PIE; ++PI)
755 Preds.push_back(*PI);
756 }
757
758 do {
759 MBB = Preds.pop_back_val();
760 if (!Visited.count(MBB)) {
761 I = MBB->getLastNonDebugInstr();
762 goto check_block;
763 }
764 } while (!Preds.empty());
765
766 return true;
767}
768
769bool PPCCTRLoopsVerify::runOnMachineFunction(MachineFunction &MF) {
770 MDT = &getAnalysis<MachineDominatorTree>();
771
772 // Verify that all bdnz/bdz instructions are dominated by a loop mtctr before
773 // any other instructions that might clobber the ctr register.
774 for (MachineFunction::iterator I = MF.begin(), IE = MF.end();
775 I != IE; ++I) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +0000776 MachineBasicBlock *MBB = &*I;
Hal Finkel8ca38842013-05-20 16:08:17 +0000777 if (!MDT->isReachableFromEntry(MBB))
778 continue;
779
780 for (MachineBasicBlock::iterator MII = MBB->getFirstTerminator(),
781 MIIE = MBB->end(); MII != MIIE; ++MII) {
782 unsigned Opc = MII->getOpcode();
783 if (Opc == PPC::BDNZ8 || Opc == PPC::BDNZ ||
784 Opc == PPC::BDZ8 || Opc == PPC::BDZ)
785 if (!verifyCTRBranch(MBB, MII))
786 llvm_unreachable("Invalid PPC CTR loop!");
787 }
788 }
789
790 return false;
791}
792#endif // NDEBUG