blob: 6785743824cae1169d3d3bf1448eec9677007ab3 [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
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000083namespace llvm {
84 void initializePPCCTRLoopsPass(PassRegistry&);
Hal Finkel8ca38842013-05-20 16:08:17 +000085#ifndef NDEBUG
86 void initializePPCCTRLoopsVerifyPass(PassRegistry&);
87#endif
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +000088}
89
Hal Finkel96c2d4d2012-06-08 15:38:21 +000090namespace {
Hal Finkel25c19922013-05-15 21:37:41 +000091 struct PPCCTRLoops : public FunctionPass {
92
93#ifndef NDEBUG
94 static int Counter;
95#endif
Hal Finkel96c2d4d2012-06-08 15:38:21 +000096
97 public:
Hal Finkel25c19922013-05-15 21:37:41 +000098 static char ID;
Hal Finkel96c2d4d2012-06-08 15:38:21 +000099
Eric Christopherb16eacf2017-06-29 23:28:45 +0000100 PPCCTRLoops() : FunctionPass(ID) {
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +0000101 initializePPCCTRLoopsPass(*PassRegistry::getPassRegistry());
102 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000103
Craig Topper0d3fa922014-04-29 07:57:37 +0000104 bool runOnFunction(Function &F) override;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000105
Craig Topper0d3fa922014-04-29 07:57:37 +0000106 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000107 AU.addRequired<LoopInfoWrapperPass>();
108 AU.addPreserved<LoopInfoWrapperPass>();
Chandler Carruth73523022014-01-13 13:07:17 +0000109 AU.addRequired<DominatorTreeWrapperPass>();
110 AU.addPreserved<DominatorTreeWrapperPass>();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000111 AU.addRequired<ScalarEvolutionWrapperPass>();
Lei Huang0724fea2017-10-12 16:43:33 +0000112 AU.addRequired<AssumptionCacheTracker>();
113 AU.addRequired<TargetTransformInfoWrapperPass>();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000114 }
115
116 private:
Eric Christopherb16eacf2017-06-29 23:28:45 +0000117 bool mightUseCTR(BasicBlock *BB);
Hal Finkel25c19922013-05-15 21:37:41 +0000118 bool convertToCTRLoop(Loop *L);
Hal Finkele6d7c282013-05-20 16:47:10 +0000119
Hal Finkel25c19922013-05-15 21:37:41 +0000120 private:
Eric Christopherb16eacf2017-06-29 23:28:45 +0000121 const PPCTargetMachine *TM;
122 const PPCSubtarget *STI;
123 const PPCTargetLowering *TLI;
124 const DataLayout *DL;
125 const TargetLibraryInfo *LibInfo;
Lei Huang0724fea2017-10-12 16:43:33 +0000126 const TargetTransformInfo *TTI;
Hal Finkel25c19922013-05-15 21:37:41 +0000127 LoopInfo *LI;
128 ScalarEvolution *SE;
Hal Finkel25c19922013-05-15 21:37:41 +0000129 DominatorTree *DT;
Justin Bogner843fb202015-12-15 19:40:57 +0000130 bool PreserveLCSSA;
Lei Huang0724fea2017-10-12 16:43:33 +0000131 TargetSchedModel SchedModel;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000132 };
133
134 char PPCCTRLoops::ID = 0;
Hal Finkel25c19922013-05-15 21:37:41 +0000135#ifndef NDEBUG
136 int PPCCTRLoops::Counter = 0;
137#endif
Hal Finkel8ca38842013-05-20 16:08:17 +0000138
139#ifndef NDEBUG
140 struct PPCCTRLoopsVerify : public MachineFunctionPass {
141 public:
142 static char ID;
143
144 PPCCTRLoopsVerify() : MachineFunctionPass(ID) {
145 initializePPCCTRLoopsVerifyPass(*PassRegistry::getPassRegistry());
146 }
147
Craig Topper0d3fa922014-04-29 07:57:37 +0000148 void getAnalysisUsage(AnalysisUsage &AU) const override {
Hal Finkel8ca38842013-05-20 16:08:17 +0000149 AU.addRequired<MachineDominatorTree>();
150 MachineFunctionPass::getAnalysisUsage(AU);
151 }
152
Craig Topper0d3fa922014-04-29 07:57:37 +0000153 bool runOnMachineFunction(MachineFunction &MF) override;
Hal Finkel8ca38842013-05-20 16:08:17 +0000154
155 private:
156 MachineDominatorTree *MDT;
157 };
158
159 char PPCCTRLoopsVerify::ID = 0;
160#endif // NDEBUG
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000161} // end anonymous namespace
162
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +0000163INITIALIZE_PASS_BEGIN(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
164 false, false)
Chandler Carruth73523022014-01-13 13:07:17 +0000165INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000166INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000167INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
Krzysztof Parzyszek2680b532013-02-13 17:40:07 +0000168INITIALIZE_PASS_END(PPCCTRLoops, "ppc-ctr-loops", "PowerPC CTR Loops",
169 false, false)
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000170
Eric Christopherb16eacf2017-06-29 23:28:45 +0000171FunctionPass *llvm::createPPCCTRLoops() { return new PPCCTRLoops(); }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000172
Hal Finkel8ca38842013-05-20 16:08:17 +0000173#ifndef NDEBUG
174INITIALIZE_PASS_BEGIN(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
175 "PowerPC CTR Loops Verify", false, false)
176INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
177INITIALIZE_PASS_END(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
178 "PowerPC CTR Loops Verify", false, false)
179
180FunctionPass *llvm::createPPCCTRLoopsVerify() {
181 return new PPCCTRLoopsVerify();
182}
183#endif // NDEBUG
184
Hal Finkel25c19922013-05-15 21:37:41 +0000185bool PPCCTRLoops::runOnFunction(Function &F) {
Andrew Kaylor289bd5f2016-04-27 19:39:32 +0000186 if (skipFunction(F))
187 return false;
188
Eric Christopherb16eacf2017-06-29 23:28:45 +0000189 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
190 if (!TPC)
191 return false;
192
193 TM = &TPC->getTM<PPCTargetMachine>();
194 STI = TM->getSubtargetImpl(F);
195 TLI = STI->getTargetLowering();
196
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000197 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000198 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruth73523022014-01-13 13:07:17 +0000199 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Lei Huang0724fea2017-10-12 16:43:33 +0000200 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
Mehdi Amini46a43552015-03-04 18:43:29 +0000201 DL = &F.getParent()->getDataLayout();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000202 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
203 LibInfo = TLIP ? &TLIP->getTLI() : nullptr;
Justin Bogner843fb202015-12-15 19:40:57 +0000204 PreserveLCSSA = mustPreserveAnalysisID(LCSSAID);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000205
Hal Finkel25c19922013-05-15 21:37:41 +0000206 bool MadeChange = false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000207
Hal Finkel25c19922013-05-15 21:37:41 +0000208 for (LoopInfo::iterator I = LI->begin(), E = LI->end();
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000209 I != E; ++I) {
Hal Finkel25c19922013-05-15 21:37:41 +0000210 Loop *L = *I;
211 if (!L->getParentLoop())
212 MadeChange |= convertToCTRLoop(L);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000213 }
214
Hal Finkel25c19922013-05-15 21:37:41 +0000215 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000216}
217
Hal Finkel22304042014-02-25 20:51:50 +0000218static bool isLargeIntegerTy(bool Is32Bit, Type *Ty) {
219 if (IntegerType *ITy = dyn_cast<IntegerType>(Ty))
Aaron Ballman3d69c5c2014-02-26 20:22:20 +0000220 return ITy->getBitWidth() > (Is32Bit ? 32U : 64U);
Hal Finkel22304042014-02-25 20:51:50 +0000221
222 return false;
223}
224
David Majnemerd0bcef22014-12-27 19:45:38 +0000225// Determining the address of a TLS variable results in a function call in
226// certain TLS models.
Eric Christopherb16eacf2017-06-29 23:28:45 +0000227static bool memAddrUsesCTR(const PPCTargetMachine &TM, const Value *MemAddr) {
David Majnemerd0bcef22014-12-27 19:45:38 +0000228 const auto *GV = dyn_cast<GlobalValue>(MemAddr);
Hal Finkel7d0e34e2015-10-28 23:43:00 +0000229 if (!GV) {
230 // Recurse to check for constants that refer to TLS global variables.
231 if (const auto *CV = dyn_cast<Constant>(MemAddr))
232 for (const auto &CO : CV->operands())
233 if (memAddrUsesCTR(TM, CO))
234 return true;
235
David Majnemerd0bcef22014-12-27 19:45:38 +0000236 return false;
Hal Finkel7d0e34e2015-10-28 23:43:00 +0000237 }
238
David Majnemerd0bcef22014-12-27 19:45:38 +0000239 if (!GV->isThreadLocal())
240 return false;
Eric Christopherb16eacf2017-06-29 23:28:45 +0000241 TLSModel::Model Model = TM.getTLSModel(GV);
David Majnemerd0bcef22014-12-27 19:45:38 +0000242 return Model == TLSModel::GeneralDynamic || Model == TLSModel::LocalDynamic;
243}
244
Eric Christopher56f481b2017-06-29 23:28:47 +0000245// Loop through the inline asm constraints and look for something that clobbers
246// ctr.
247static bool asmClobbersCTR(InlineAsm *IA) {
248 InlineAsm::ConstraintInfoVector CIV = IA->ParseConstraints();
249 for (unsigned i = 0, ie = CIV.size(); i < ie; ++i) {
250 InlineAsm::ConstraintInfo &C = CIV[i];
251 if (C.Type != InlineAsm::isInput)
252 for (unsigned j = 0, je = C.Codes.size(); j < je; ++j)
253 if (StringRef(C.Codes[j]).equals_lower("{ctr}"))
254 return true;
255 }
256 return false;
257}
258
Eric Christopherb16eacf2017-06-29 23:28:45 +0000259bool PPCCTRLoops::mightUseCTR(BasicBlock *BB) {
Hal Finkel5f587c52013-05-16 19:58:38 +0000260 for (BasicBlock::iterator J = BB->begin(), JE = BB->end();
261 J != JE; ++J) {
262 if (CallInst *CI = dyn_cast<CallInst>(J)) {
Eric Christopher56f481b2017-06-29 23:28:47 +0000263 // Inline ASM is okay, unless it clobbers the ctr register.
Hal Finkel2f474f02013-05-18 09:20:39 +0000264 if (InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
NAKAMURA Takumia1e97a72017-08-28 06:47:47 +0000265 if (asmClobbersCTR(IA))
266 return true;
Hal Finkel2f474f02013-05-18 09:20:39 +0000267 continue;
268 }
269
Hal Finkel5f587c52013-05-16 19:58:38 +0000270 if (Function *F = CI->getCalledFunction()) {
271 // Most intrinsics don't become function calls, but some might.
272 // sin, cos, exp and log are always calls.
Hal Finkel0b371752016-03-27 05:40:56 +0000273 unsigned Opcode = 0;
Hal Finkel5f587c52013-05-16 19:58:38 +0000274 if (F->getIntrinsicID() != Intrinsic::not_intrinsic) {
275 switch (F->getIntrinsicID()) {
276 default: continue;
Eric Christopher71f6e2f2015-09-08 22:14:58 +0000277 // If we have a call to ppc_is_decremented_ctr_nonzero, or ppc_mtctr
278 // we're definitely using CTR.
279 case Intrinsic::ppc_is_decremented_ctr_nonzero:
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +0000280 case Intrinsic::ppc_mtctr:
281 return true;
Hal Finkel5f587c52013-05-16 19:58:38 +0000282
283// VisualStudio defines setjmp as _setjmp
284#if defined(_MSC_VER) && defined(setjmp) && \
285 !defined(setjmp_undefined_for_msvc)
286# pragma push_macro("setjmp")
287# undef setjmp
288# define setjmp_undefined_for_msvc
289#endif
290
291 case Intrinsic::setjmp:
292
293#if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
294 // let's return it to _setjmp state
295# pragma pop_macro("setjmp")
296# undef setjmp_undefined_for_msvc
297#endif
298
299 case Intrinsic::longjmp:
Hal Finkel40f76d52013-07-17 05:35:44 +0000300
301 // Exclude eh_sjlj_setjmp; we don't need to exclude eh_sjlj_longjmp
302 // because, although it does clobber the counter register, the
303 // control can't then return to inside the loop unless there is also
304 // an eh_sjlj_setjmp.
305 case Intrinsic::eh_sjlj_setjmp:
306
Hal Finkel5f587c52013-05-16 19:58:38 +0000307 case Intrinsic::memcpy:
308 case Intrinsic::memmove:
309 case Intrinsic::memset:
310 case Intrinsic::powi:
311 case Intrinsic::log:
312 case Intrinsic::log2:
313 case Intrinsic::log10:
314 case Intrinsic::exp:
315 case Intrinsic::exp2:
316 case Intrinsic::pow:
317 case Intrinsic::sin:
318 case Intrinsic::cos:
319 return true;
Hal Finkel0c5c01aa2013-08-19 23:35:46 +0000320 case Intrinsic::copysign:
321 if (CI->getArgOperand(0)->getType()->getScalarType()->
322 isPPC_FP128Ty())
323 return true;
324 else
325 continue; // ISD::FCOPYSIGN is never a library call.
Hal Finkelcef9e522017-04-11 02:03:17 +0000326 case Intrinsic::sqrt: Opcode = ISD::FSQRT; break;
327 case Intrinsic::floor: Opcode = ISD::FFLOOR; break;
328 case Intrinsic::ceil: Opcode = ISD::FCEIL; break;
329 case Intrinsic::trunc: Opcode = ISD::FTRUNC; break;
330 case Intrinsic::rint: Opcode = ISD::FRINT; break;
331 case Intrinsic::nearbyint: Opcode = ISD::FNEARBYINT; break;
332 case Intrinsic::round: Opcode = ISD::FROUND; break;
333 case Intrinsic::minnum: Opcode = ISD::FMINNUM; break;
334 case Intrinsic::maxnum: Opcode = ISD::FMAXNUM; break;
335 case Intrinsic::umul_with_overflow: Opcode = ISD::UMULO; break;
336 case Intrinsic::smul_with_overflow: Opcode = ISD::SMULO; break;
Hal Finkel5f587c52013-05-16 19:58:38 +0000337 }
338 }
339
340 // PowerPC does not use [US]DIVREM or other library calls for
341 // operations on regular types which are not otherwise library calls
342 // (i.e. soft float or atomics). If adapting for targets that do,
343 // additional care is required here.
344
David L. Jonesd21529f2017-01-23 23:16:46 +0000345 LibFunc Func;
Hal Finkel5f587c52013-05-16 19:58:38 +0000346 if (!F->hasLocalLinkage() && F->hasName() && LibInfo &&
347 LibInfo->getLibFunc(F->getName(), Func) &&
348 LibInfo->hasOptimizedCodeGen(Func)) {
349 // Non-read-only functions are never treated as intrinsics.
350 if (!CI->onlyReadsMemory())
351 return true;
352
353 // Conversion happens only for FP calls.
354 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
355 return true;
356
357 switch (Func) {
358 default: return true;
David L. Jonesd21529f2017-01-23 23:16:46 +0000359 case LibFunc_copysign:
360 case LibFunc_copysignf:
Hal Finkel5f587c52013-05-16 19:58:38 +0000361 continue; // ISD::FCOPYSIGN is never a library call.
David L. Jonesd21529f2017-01-23 23:16:46 +0000362 case LibFunc_copysignl:
Hal Finkel1cf48ab2013-08-19 23:35:24 +0000363 return true;
David L. Jonesd21529f2017-01-23 23:16:46 +0000364 case LibFunc_fabs:
365 case LibFunc_fabsf:
366 case LibFunc_fabsl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000367 continue; // ISD::FABS is never a library call.
David L. Jonesd21529f2017-01-23 23:16:46 +0000368 case LibFunc_sqrt:
369 case LibFunc_sqrtf:
370 case LibFunc_sqrtl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000371 Opcode = ISD::FSQRT; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000372 case LibFunc_floor:
373 case LibFunc_floorf:
374 case LibFunc_floorl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000375 Opcode = ISD::FFLOOR; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000376 case LibFunc_nearbyint:
377 case LibFunc_nearbyintf:
378 case LibFunc_nearbyintl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000379 Opcode = ISD::FNEARBYINT; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000380 case LibFunc_ceil:
381 case LibFunc_ceilf:
382 case LibFunc_ceill:
Hal Finkel5f587c52013-05-16 19:58:38 +0000383 Opcode = ISD::FCEIL; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000384 case LibFunc_rint:
385 case LibFunc_rintf:
386 case LibFunc_rintl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000387 Opcode = ISD::FRINT; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000388 case LibFunc_round:
389 case LibFunc_roundf:
390 case LibFunc_roundl:
Hal Finkel171817e2013-08-07 22:49:12 +0000391 Opcode = ISD::FROUND; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000392 case LibFunc_trunc:
393 case LibFunc_truncf:
394 case LibFunc_truncl:
Hal Finkel5f587c52013-05-16 19:58:38 +0000395 Opcode = ISD::FTRUNC; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000396 case LibFunc_fmin:
397 case LibFunc_fminf:
398 case LibFunc_fminl:
Hal Finkel0b371752016-03-27 05:40:56 +0000399 Opcode = ISD::FMINNUM; break;
David L. Jonesd21529f2017-01-23 23:16:46 +0000400 case LibFunc_fmax:
401 case LibFunc_fmaxf:
402 case LibFunc_fmaxl:
Hal Finkel0b371752016-03-27 05:40:56 +0000403 Opcode = ISD::FMAXNUM; break;
Hal Finkel5f587c52013-05-16 19:58:38 +0000404 }
Hal Finkel0b371752016-03-27 05:40:56 +0000405 }
Hal Finkel5f587c52013-05-16 19:58:38 +0000406
Hal Finkel0b371752016-03-27 05:40:56 +0000407 if (Opcode) {
Sean Fertile33a17762018-01-09 03:03:41 +0000408 EVT EVTy =
409 TLI->getValueType(*DL, CI->getArgOperand(0)->getType(), true);
410
411 if (EVTy == MVT::Other)
Hal Finkel5f587c52013-05-16 19:58:38 +0000412 return true;
NAKAMURA Takumia9cb5382015-09-22 11:14:39 +0000413
Sean Fertile33a17762018-01-09 03:03:41 +0000414 if (TLI->isOperationLegalOrCustom(Opcode, EVTy))
Hal Finkel5f587c52013-05-16 19:58:38 +0000415 continue;
Sean Fertile33a17762018-01-09 03:03:41 +0000416 else if (EVTy.isVector() &&
417 TLI->isOperationLegalOrCustom(Opcode, EVTy.getScalarType()))
Hal Finkel5f587c52013-05-16 19:58:38 +0000418 continue;
419
420 return true;
421 }
422 }
423
424 return true;
425 } else if (isa<BinaryOperator>(J) &&
426 J->getType()->getScalarType()->isPPC_FP128Ty()) {
427 // Most operations on ppc_f128 values become calls.
428 return true;
429 } else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
430 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
431 CastInst *CI = cast<CastInst>(J);
432 if (CI->getSrcTy()->getScalarType()->isPPC_FP128Ty() ||
433 CI->getDestTy()->getScalarType()->isPPC_FP128Ty() ||
Eric Christopherb16eacf2017-06-29 23:28:45 +0000434 isLargeIntegerTy(!TM->isPPC64(), CI->getSrcTy()->getScalarType()) ||
435 isLargeIntegerTy(!TM->isPPC64(), CI->getDestTy()->getScalarType()))
Hal Finkel5f587c52013-05-16 19:58:38 +0000436 return true;
Eric Christopherb16eacf2017-06-29 23:28:45 +0000437 } else if (isLargeIntegerTy(!TM->isPPC64(),
Hal Finkel22304042014-02-25 20:51:50 +0000438 J->getType()->getScalarType()) &&
Hal Finkelfa5f6f72013-06-07 22:16:19 +0000439 (J->getOpcode() == Instruction::UDiv ||
440 J->getOpcode() == Instruction::SDiv ||
441 J->getOpcode() == Instruction::URem ||
442 J->getOpcode() == Instruction::SRem)) {
443 return true;
Eric Christopherb16eacf2017-06-29 23:28:45 +0000444 } else if (!TM->isPPC64() &&
Hal Finkelc4c6c872014-05-11 16:23:29 +0000445 isLargeIntegerTy(false, J->getType()->getScalarType()) &&
446 (J->getOpcode() == Instruction::Shl ||
447 J->getOpcode() == Instruction::AShr ||
448 J->getOpcode() == Instruction::LShr)) {
449 // Only on PPC32, for 128-bit integers (specifically not 64-bit
450 // integers), these might be runtime calls.
451 return true;
Hal Finkel5f587c52013-05-16 19:58:38 +0000452 } else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
453 // On PowerPC, indirect jumps use the counter register.
454 return true;
455 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
Eric Christopher79cc1e32014-09-02 22:28:02 +0000456 if (SI->getNumCases() + 1 >= (unsigned)TLI->getMinimumJumpTableEntries())
Hal Finkel5f587c52013-05-16 19:58:38 +0000457 return true;
458 }
Petar Jovanovic0b44f242016-03-17 17:11:33 +0000459
Nemanja Ivanovice54a9ee2018-02-22 03:02:41 +0000460 // FREM is always a call.
461 if (J->getOpcode() == Instruction::FRem)
462 return true;
463
Eric Christopherb16eacf2017-06-29 23:28:45 +0000464 if (STI->useSoftFloat()) {
Petar Jovanovic0b44f242016-03-17 17:11:33 +0000465 switch(J->getOpcode()) {
466 case Instruction::FAdd:
467 case Instruction::FSub:
468 case Instruction::FMul:
469 case Instruction::FDiv:
Petar Jovanovic0b44f242016-03-17 17:11:33 +0000470 case Instruction::FPTrunc:
471 case Instruction::FPExt:
472 case Instruction::FPToUI:
473 case Instruction::FPToSI:
474 case Instruction::UIToFP:
475 case Instruction::SIToFP:
476 case Instruction::FCmp:
477 return true;
478 }
479 }
480
David Majnemerd0bcef22014-12-27 19:45:38 +0000481 for (Value *Operand : J->operands())
Eric Christopherb16eacf2017-06-29 23:28:45 +0000482 if (memAddrUsesCTR(*TM, Operand))
David Majnemerd0bcef22014-12-27 19:45:38 +0000483 return true;
Hal Finkel5f587c52013-05-16 19:58:38 +0000484 }
485
486 return false;
487}
Hal Finkel25c19922013-05-15 21:37:41 +0000488bool PPCCTRLoops::convertToCTRLoop(Loop *L) {
489 bool MadeChange = false;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000490
Lei Huang0724fea2017-10-12 16:43:33 +0000491 // Do not convert small short loops to CTR loop.
492 unsigned ConstTripCount = SE->getSmallConstantTripCount(L);
493 if (ConstTripCount && ConstTripCount < SmallCTRLoopThreshold) {
494 SmallPtrSet<const Value *, 32> EphValues;
495 auto AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
496 *L->getHeader()->getParent());
497 CodeMetrics::collectEphemeralValues(L, &AC, EphValues);
498 CodeMetrics Metrics;
499 for (BasicBlock *BB : L->blocks())
500 Metrics.analyzeBasicBlock(BB, *TTI, EphValues);
501 // 6 is an approximate latency for the mtctr instruction.
502 if (Metrics.NumInsts <= (6 * SchedModel.getIssueWidth()))
503 return false;
504 }
505
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000506 // Process nested loops first.
Hal Finkel25c19922013-05-15 21:37:41 +0000507 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) {
508 MadeChange |= convertToCTRLoop(*I);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000509 LLVM_DEBUG(dbgs() << "Nested loop converted\n");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000510 }
Hal Finkel25c19922013-05-15 21:37:41 +0000511
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000512 // If a nested loop has been converted, then we can't convert this loop.
Hal Finkel25c19922013-05-15 21:37:41 +0000513 if (MadeChange)
514 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000515
Nemanja Ivanovic2139e992018-05-02 22:56:04 +0000516 // Bail out if the loop has irreducible control flow.
517 LoopBlocksRPO RPOT(L);
518 RPOT.perform(LI);
519 if (containsIrreducibleCFG<const BasicBlock *>(RPOT, *LI))
520 return false;
521
Hal Finkel25c19922013-05-15 21:37:41 +0000522#ifndef NDEBUG
523 // Stop trying after reaching the limit (if any).
524 int Limit = CTRLoopLimit;
525 if (Limit >= 0) {
526 if (Counter >= CTRLoopLimit)
Hal Finkel21f2a432013-03-18 17:40:44 +0000527 return false;
Hal Finkel25c19922013-05-15 21:37:41 +0000528 Counter++;
Hal Finkel21f2a432013-03-18 17:40:44 +0000529 }
Hal Finkel25c19922013-05-15 21:37:41 +0000530#endif
Hal Finkel21f2a432013-03-18 17:40:44 +0000531
Hal Finkel25c19922013-05-15 21:37:41 +0000532 // We don't want to spill/restore the counter register, and so we don't
533 // want to use the counter register if the loop contains calls.
534 for (Loop::block_iterator I = L->block_begin(), IE = L->block_end();
Hal Finkel5f587c52013-05-16 19:58:38 +0000535 I != IE; ++I)
Eric Christopherb16eacf2017-06-29 23:28:45 +0000536 if (mightUseCTR(*I))
Hal Finkel5f587c52013-05-16 19:58:38 +0000537 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000538
Hal Finkel25c19922013-05-15 21:37:41 +0000539 SmallVector<BasicBlock*, 4> ExitingBlocks;
540 L->getExitingBlocks(ExitingBlocks);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000541
Hiroshi Inouec5ab1ab2018-02-05 12:25:29 +0000542 // If there is an exit edge known to be frequently taken,
543 // we should not transform this loop.
544 for (auto &BB : ExitingBlocks) {
545 Instruction *TI = BB->getTerminator();
546 if (!TI) continue;
547
548 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
Hiroshi Inouead48d2fe2018-02-06 11:34:16 +0000549 uint64_t TrueWeight = 0, FalseWeight = 0;
Hiroshi Inouec5ab1ab2018-02-05 12:25:29 +0000550 if (!BI->isConditional() ||
551 !BI->extractProfMetadata(TrueWeight, FalseWeight))
552 continue;
553
554 // If the exit path is more frequent than the loop path,
555 // we return here without further analysis for this loop.
556 bool TrueIsExit = !L->contains(BI->getSuccessor(0));
557 if (( TrueIsExit && FalseWeight < TrueWeight) ||
558 (!TrueIsExit && FalseWeight > TrueWeight))
559 return MadeChange;
560 }
561 }
562
Craig Topper062a2ba2014-04-25 05:30:21 +0000563 BasicBlock *CountedExitBlock = nullptr;
564 const SCEV *ExitCount = nullptr;
565 BranchInst *CountedExitBranch = nullptr;
Craig Topperaf0dea12013-07-04 01:31:24 +0000566 for (SmallVectorImpl<BasicBlock *>::iterator I = ExitingBlocks.begin(),
Hal Finkel25c19922013-05-15 21:37:41 +0000567 IE = ExitingBlocks.end(); I != IE; ++I) {
568 const SCEV *EC = SE->getExitCount(L, *I);
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000569 LLVM_DEBUG(dbgs() << "Exit Count for " << *L << " from block "
570 << (*I)->getName() << ": " << *EC << "\n");
Hal Finkel25c19922013-05-15 21:37:41 +0000571 if (isa<SCEVCouldNotCompute>(EC))
572 continue;
573 if (const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
574 if (ConstEC->getValue()->isZero())
575 continue;
576 } else if (!SE->isLoopInvariant(EC, L))
577 continue;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000578
Eric Christopherb16eacf2017-06-29 23:28:45 +0000579 if (SE->getTypeSizeInBits(EC->getType()) > (TM->isPPC64() ? 64 : 32))
Hal Finkel25e4a0d2013-07-01 19:34:59 +0000580 continue;
581
Nemanja Ivanovic2139e992018-05-02 22:56:04 +0000582 // If this exiting block is contained in a nested loop, it is not eligible
583 // for insertion of the branch-and-decrement since the inner loop would
584 // end up messing up the value in the CTR.
585 if (LI->getLoopFor(*I) != L)
586 continue;
587
Hal Finkel25c19922013-05-15 21:37:41 +0000588 // We now have a loop-invariant count of loop iterations (which is not the
589 // constant zero) for which we know that this loop will not exit via this
Hiroshi Inoue0f7f59f2018-06-13 08:54:13 +0000590 // existing block.
Hal Finkel6261c2d2012-06-16 20:34:07 +0000591
Hal Finkel25c19922013-05-15 21:37:41 +0000592 // We need to make sure that this block will run on every loop iteration.
593 // For this to be true, we must dominate all blocks with backedges. Such
594 // blocks are in-loop predecessors to the header block.
595 bool NotAlways = false;
596 for (pred_iterator PI = pred_begin(L->getHeader()),
597 PIE = pred_end(L->getHeader()); PI != PIE; ++PI) {
598 if (!L->contains(*PI))
599 continue;
600
601 if (!DT->dominates(*I, *PI)) {
602 NotAlways = true;
603 break;
604 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000605 }
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000606
Hal Finkel25c19922013-05-15 21:37:41 +0000607 if (NotAlways)
608 continue;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000609
Hal Finkel25c19922013-05-15 21:37:41 +0000610 // Make sure this blocks ends with a conditional branch.
611 Instruction *TI = (*I)->getTerminator();
612 if (!TI)
613 continue;
614
615 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
616 if (!BI->isConditional())
617 continue;
618
619 CountedExitBranch = BI;
620 } else
621 continue;
622
623 // Note that this block may not be the loop latch block, even if the loop
624 // has a latch block.
625 CountedExitBlock = *I;
626 ExitCount = EC;
627 break;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000628 }
629
Hal Finkel25c19922013-05-15 21:37:41 +0000630 if (!CountedExitBlock)
631 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000632
Hal Finkel25c19922013-05-15 21:37:41 +0000633 BasicBlock *Preheader = L->getLoopPreheader();
Hal Finkel5f587c52013-05-16 19:58:38 +0000634
635 // If we don't have a preheader, then insert one. If we already have a
636 // preheader, then we can use it (except if the preheader contains a use of
637 // the CTR register because some such uses might be reordered by the
638 // selection DAG after the mtctr instruction).
Eric Christopherb16eacf2017-06-29 23:28:45 +0000639 if (!Preheader || mightUseCTR(Preheader))
Justin Bogner843fb202015-12-15 19:40:57 +0000640 Preheader = InsertPreheaderForLoop(L, DT, LI, PreserveLCSSA);
Hal Finkel25c19922013-05-15 21:37:41 +0000641 if (!Preheader)
642 return MadeChange;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000643
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000644 LLVM_DEBUG(dbgs() << "Preheader for exit count: " << Preheader->getName()
645 << "\n");
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000646
Hal Finkel25c19922013-05-15 21:37:41 +0000647 // Insert the count into the preheader and replace the condition used by the
648 // selected branch.
649 MadeChange = true;
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000650
Eric Christopherb16eacf2017-06-29 23:28:45 +0000651 SCEVExpander SCEVE(*SE, *DL, "loopcnt");
Hal Finkel25c19922013-05-15 21:37:41 +0000652 LLVMContext &C = SE->getContext();
Eric Christopherb16eacf2017-06-29 23:28:45 +0000653 Type *CountType = TM->isPPC64() ? Type::getInt64Ty(C) : Type::getInt32Ty(C);
Hal Finkel25c19922013-05-15 21:37:41 +0000654 if (!ExitCount->getType()->isPointerTy() &&
655 ExitCount->getType() != CountType)
656 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000657 ExitCount = SE->getAddExpr(ExitCount, SE->getOne(CountType));
NAKAMURA Takumi70ad98a2015-09-22 11:13:55 +0000658 Value *ECValue =
659 SCEVE.expandCodeFor(ExitCount, CountType, Preheader->getTerminator());
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000660
Hal Finkel25c19922013-05-15 21:37:41 +0000661 IRBuilder<> CountBuilder(Preheader->getTerminator());
662 Module *M = Preheader->getParent()->getParent();
663 Value *MTCTRFunc = Intrinsic::getDeclaration(M, Intrinsic::ppc_mtctr,
664 CountType);
665 CountBuilder.CreateCall(MTCTRFunc, ECValue);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000666
Hal Finkel25c19922013-05-15 21:37:41 +0000667 IRBuilder<> CondBuilder(CountedExitBranch);
668 Value *DecFunc =
669 Intrinsic::getDeclaration(M, Intrinsic::ppc_is_decremented_ctr_nonzero);
David Blaikieff6409d2015-05-18 22:13:54 +0000670 Value *NewCond = CondBuilder.CreateCall(DecFunc, {});
Hal Finkel25c19922013-05-15 21:37:41 +0000671 Value *OldCond = CountedExitBranch->getCondition();
672 CountedExitBranch->setCondition(NewCond);
673
674 // The false branch must exit the loop.
675 if (!L->contains(CountedExitBranch->getSuccessor(0)))
676 CountedExitBranch->swapSuccessors();
677
678 // The old condition may be dead now, and may have even created a dead PHI
679 // (the original induction variable).
680 RecursivelyDeleteTriviallyDeadInstructions(OldCond);
Sean Fertiled44cb182017-07-05 17:57:57 +0000681 // Run through the basic blocks of the loop and see if any of them have dead
682 // PHIs that can be removed.
683 for (auto I : L->blocks())
684 DeleteDeadPHIs(I);
Hal Finkel96c2d4d2012-06-08 15:38:21 +0000685
686 ++NumCTRLoops;
Hal Finkel25c19922013-05-15 21:37:41 +0000687 return MadeChange;
688}
689
Hal Finkel8ca38842013-05-20 16:08:17 +0000690#ifndef NDEBUG
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000691static bool clobbersCTR(const MachineInstr &MI) {
692 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
693 const MachineOperand &MO = MI.getOperand(i);
Hal Finkel8ca38842013-05-20 16:08:17 +0000694 if (MO.isReg()) {
695 if (MO.isDef() && (MO.getReg() == PPC::CTR || MO.getReg() == PPC::CTR8))
696 return true;
697 } else if (MO.isRegMask()) {
698 if (MO.clobbersPhysReg(PPC::CTR) || MO.clobbersPhysReg(PPC::CTR8))
699 return true;
700 }
701 }
702
703 return false;
704}
705
706static bool verifyCTRBranch(MachineBasicBlock *MBB,
707 MachineBasicBlock::iterator I) {
708 MachineBasicBlock::iterator BI = I;
709 SmallSet<MachineBasicBlock *, 16> Visited;
710 SmallVector<MachineBasicBlock *, 8> Preds;
711 bool CheckPreds;
712
713 if (I == MBB->begin()) {
714 Visited.insert(MBB);
715 goto queue_preds;
716 } else
717 --I;
718
719check_block:
720 Visited.insert(MBB);
721 if (I == MBB->end())
722 goto queue_preds;
723
724 CheckPreds = true;
725 for (MachineBasicBlock::iterator IE = MBB->begin();; --I) {
726 unsigned Opc = I->getOpcode();
Hal Finkel0859ef22013-05-20 16:08:37 +0000727 if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
Hal Finkel8ca38842013-05-20 16:08:17 +0000728 CheckPreds = false;
729 break;
730 }
731
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000732 if (I != BI && clobbersCTR(*I)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000733 LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << " (" << MBB->getFullName()
734 << ") instruction " << *I
735 << " clobbers CTR, invalidating "
736 << printMBBReference(*BI->getParent()) << " ("
737 << BI->getParent()->getFullName() << ") instruction "
738 << *BI << "\n");
Hal Finkel8ca38842013-05-20 16:08:17 +0000739 return false;
740 }
741
742 if (I == IE)
743 break;
744 }
745
746 if (!CheckPreds && Preds.empty())
747 return true;
748
749 if (CheckPreds) {
750queue_preds:
751 if (MachineFunction::iterator(MBB) == MBB->getParent()->begin()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000752 LLVM_DEBUG(dbgs() << "Unable to find a MTCTR instruction for "
753 << printMBBReference(*BI->getParent()) << " ("
754 << BI->getParent()->getFullName() << ") instruction "
755 << *BI << "\n");
Hal Finkel8ca38842013-05-20 16:08:17 +0000756 return false;
757 }
758
759 for (MachineBasicBlock::pred_iterator PI = MBB->pred_begin(),
760 PIE = MBB->pred_end(); PI != PIE; ++PI)
761 Preds.push_back(*PI);
762 }
763
764 do {
765 MBB = Preds.pop_back_val();
766 if (!Visited.count(MBB)) {
767 I = MBB->getLastNonDebugInstr();
768 goto check_block;
769 }
770 } while (!Preds.empty());
771
772 return true;
773}
774
775bool PPCCTRLoopsVerify::runOnMachineFunction(MachineFunction &MF) {
776 MDT = &getAnalysis<MachineDominatorTree>();
777
778 // Verify that all bdnz/bdz instructions are dominated by a loop mtctr before
779 // any other instructions that might clobber the ctr register.
780 for (MachineFunction::iterator I = MF.begin(), IE = MF.end();
781 I != IE; ++I) {
Duncan P. N. Exon Smithac65b4c2015-10-20 01:07:37 +0000782 MachineBasicBlock *MBB = &*I;
Hal Finkel8ca38842013-05-20 16:08:17 +0000783 if (!MDT->isReachableFromEntry(MBB))
784 continue;
785
786 for (MachineBasicBlock::iterator MII = MBB->getFirstTerminator(),
787 MIIE = MBB->end(); MII != MIIE; ++MII) {
788 unsigned Opc = MII->getOpcode();
789 if (Opc == PPC::BDNZ8 || Opc == PPC::BDNZ ||
790 Opc == PPC::BDZ8 || Opc == PPC::BDZ)
791 if (!verifyCTRBranch(MBB, MII))
792 llvm_unreachable("Invalid PPC CTR loop!");
793 }
794 }
795
796 return false;
797}
798#endif // NDEBUG