62 #define DEBUG_TYPE "ctrloops"
68 STATISTIC(NumCTRLoops,
"Number of loops converted to CTR loops");
106 bool convertToCTRLoop(
Loop *L);
119 int PPCCTRLoops::Counter = 0;
155 return new PPCCTRLoops(TM);
160 "PowerPC CTR Loops Verify",
false,
false)
166 return new PPCCTRLoopsVerify();
170 bool PPCCTRLoops::runOnFunction(
Function &
F) {
171 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
172 SE = &getAnalysis<ScalarEvolution>();
173 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
175 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
176 LibInfo = TLIP ? &TLIP->getTLI() :
nullptr;
178 bool MadeChange =
false;
184 MadeChange |= convertToCTRLoop(L);
192 return ITy->getBitWidth() > (Is32Bit ? 32U : 64U);
204 if (!GV->isThreadLocal())
215 if (
CallInst *CI = dyn_cast<CallInst>(J)) {
216 if (
InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
219 for (
unsigned i = 0, ie = CIV.size(); i < ie; ++i) {
222 for (
unsigned j = 0, je = C.
Codes.size(); j < je; ++j)
233 TM->getSubtargetImpl(*BB->
getParent())->getTargetLowering();
235 if (
Function *F = CI->getCalledFunction()) {
244 #if defined(_MSC_VER) && defined(setjmp) && \
245 !defined(setjmp_undefined_for_msvc)
246 # pragma push_macro("setjmp")
248 # define setjmp_undefined_for_msvc
251 case Intrinsic::setjmp:
253 #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
255 # pragma pop_macro("setjmp")
256 # undef setjmp_undefined_for_msvc
259 case Intrinsic::longjmp:
265 case Intrinsic::eh_sjlj_setjmp:
267 case Intrinsic::memcpy:
268 case Intrinsic::memmove:
269 case Intrinsic::memset:
270 case Intrinsic::powi:
272 case Intrinsic::log2:
273 case Intrinsic::log10:
275 case Intrinsic::exp2:
280 case Intrinsic::copysign:
281 if (CI->getArgOperand(0)->getType()->getScalarType()->
286 case Intrinsic::sqrt: Opcode =
ISD::FSQRT;
break;
287 case Intrinsic::floor: Opcode =
ISD::FFLOOR;
break;
288 case Intrinsic::ceil: Opcode =
ISD::FCEIL;
break;
289 case Intrinsic::trunc: Opcode =
ISD::FTRUNC;
break;
290 case Intrinsic::rint: Opcode =
ISD::FRINT;
break;
292 case Intrinsic::round: Opcode =
ISD::FROUND;
break;
304 LibInfo->hasOptimizedCodeGen(Func)) {
306 if (!CI->onlyReadsMemory())
310 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
314 default:
return true;
315 case LibFunc::copysign:
316 case LibFunc::copysignf:
318 case LibFunc::copysignl:
329 case LibFunc::floorf:
330 case LibFunc::floorl:
332 case LibFunc::nearbyint:
333 case LibFunc::nearbyintf:
334 case LibFunc::nearbyintl:
345 case LibFunc::roundf:
346 case LibFunc::roundl:
349 case LibFunc::truncf:
350 case LibFunc::truncl:
354 auto &
DL = CI->getModule()->getDataLayout();
371 }
else if (isa<BinaryOperator>(J) &&
372 J->getType()->getScalarType()->isPPC_FP128Ty()) {
375 }
else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
376 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
384 J->getType()->getScalarType()) &&
385 (J->getOpcode() == Instruction::UDiv ||
386 J->getOpcode() == Instruction::SDiv ||
387 J->getOpcode() == Instruction::URem ||
388 J->getOpcode() == Instruction::SRem)) {
392 (J->getOpcode() == Instruction::Shl ||
393 J->getOpcode() == Instruction::AShr ||
394 J->getOpcode() == Instruction::LShr)) {
398 }
else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
401 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
405 TM->getSubtargetImpl(*BB->
getParent())->getTargetLowering();
410 for (
Value *Operand : J->operands())
418 bool PPCCTRLoops::convertToCTRLoop(
Loop *L) {
419 bool MadeChange =
false;
428 MadeChange |= convertToCTRLoop(*
I);
449 if (mightUseCTR(TT, *
I))
456 const SCEV *ExitCount =
nullptr;
459 IE = ExitingBlocks.
end();
I !=
IE; ++
I) {
460 const SCEV *EC = SE->getExitCount(L, *
I);
461 DEBUG(
dbgs() <<
"Exit Count for " << *L <<
" from block " <<
462 (*I)->getName() <<
": " << *EC <<
"\n");
463 if (isa<SCEVCouldNotCompute>(EC))
465 if (
const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
466 if (ConstEC->getValue()->isZero())
468 }
else if (!SE->isLoopInvariant(EC, L))
481 bool NotAlways =
false;
487 if (!DT->dominates(*
I, *PI)) {
501 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
502 if (!BI->isConditional())
505 CountedExitBranch = BI;
511 CountedExitBlock = *
I;
516 if (!CountedExitBlock)
525 if (!Preheader || mightUseCTR(TT, Preheader))
530 DEBUG(
dbgs() <<
"Preheader for exit count: " << Preheader->
getName() <<
"\n");
541 ExitCount->
getType() != CountType)
542 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
543 ExitCount = SE->getAddExpr(ExitCount,
544 SE->getConstant(CountType, 1));
545 Value *ECValue = SCEVE.expandCodeFor(ExitCount, CountType,
552 CountBuilder.CreateCall(MTCTRFunc, ECValue);
557 Value *NewCond = CondBuilder.CreateCall(DecFunc, {});
597 if (I == MBB->
begin()) {
610 unsigned Opc = I->getOpcode();
611 if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
619 " clobbers CTR, invalidating " <<
"BB#" <<
620 BI->getParent()->getNumber() <<
" (" <<
621 BI->getParent()->getFullName() <<
") instruction " <<
630 if (!CheckPreds && Preds.
empty())
636 DEBUG(
dbgs() <<
"Unable to find a MTCTR instruction for BB#" <<
637 BI->getParent()->getNumber() <<
" (" <<
638 BI->getParent()->getFullName() <<
") instruction " <<
644 PIE = MBB->
pred_end(); PI != PIE; ++PI)
650 if (!Visited.
count(MBB)) {
654 }
while (!Preds.
empty());
660 MDT = &getAnalysis<MachineDominatorTree>();
667 if (!MDT->isReachableFromEntry(MBB))
671 MIIE = MBB->
end(); MII != MIIE; ++MII) {
672 unsigned Opc = MII->getOpcode();
673 if (Opc == PPC::BDNZ8 || Opc ==
PPC::BDNZ ||
674 Opc == PPC::BDZ8 || Opc ==
PPC::BDZ)
void push_back(const T &Elt)
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
A parsed version of the target data layout string in and methods for querying it. ...
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
void swapSuccessors()
Swap the successors of this branch instruction.
void initializePPCCTRLoopsVerifyPass(PassRegistry &)
int getNumber() const
getNumber - MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a M...
A Module instance is used to store all the information related to an LLVM module. ...
iterator getFirstTerminator()
getFirstTerminator - returns an iterator to the first terminator instruction of this basic block...
ScalarEvolution - This class is the main scalar evolution driver.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
MVT getSimpleValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the MVT corresponding to this LLVM type. See getValueType.
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
static IntegerType * getInt64Ty(LLVMContext &C)
static bool memAddrUsesCTR(const PPCTargetMachine *TM, const llvm::Value *MemAddr)
BlockT * getHeader() const
ConstraintCodeVector Codes
Code - The constraint code, either the register name (in braces) or the constraint letter/number...
StringRef getName() const
Return a constant reference to the value's name.
std::vector< LoopT * >::const_iterator iterator
iterator/begin/end - The interface to the top-level loops in the current function.
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
ppc ctr loops PowerPC CTR Loops Verify
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
BasicBlock * InsertPreheaderForLoop(Loop *L, Pass *P)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
This is the base class for all instructions that perform data casts.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
void getExitingBlocks(SmallVectorImpl< BlockT * > &ExitingBlocks) const
getExitingBlocks - Return all blocks inside the loop that have successors outside of the loop...
MVT getScalarType() const
getScalarType - If this is a vector type, return the element type, otherwise return this...
static cl::opt< int > CTRLoopLimit("ppc-max-ctrloop", cl::Hidden, cl::init(-1))
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
unsigned getNumOperands() const
Access to explicit operands of the instruction.
bool isArch64Bit() const
Test whether the architecture is 64-bit.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
CHAIN = BDNZ CHAIN, DESTBB - These are used to create counter-based loops.
BasicBlock * getSuccessor(unsigned i) const
bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
DeleteDeadPHIs - Examine each PHI in the given block and delete it if it is dead. ...
FunctionPass * createPPCCTRLoopsVerify()
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
iterator getLastNonDebugInstr()
getLastNonDebugInstr - returns an iterator to the last non-debug instruction in the basic block...
int getMinimumJumpTableEntries() const
Return integer threshold on number of blocks to use jump tables rather than if sequence.
std::vector< MachineBasicBlock * >::iterator pred_iterator
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
bool isPPC_FP128Ty() const
isPPC_FP128Ty - Return true if this is powerpc long double.
ppc ctr PowerPC CTR false
bundle_iterator< MachineInstr, instr_iterator > iterator
initializer< Ty > init(const Ty &Val)
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
MVT - Machine Value Type.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This is an important class for using LLVM in a threaded context.
Type * getType() const
getType - Return the LLVM type of this SCEV expression.
BranchInst - Conditional or Unconditional Branch instruction.
bool isOperationLegalOrCustom(unsigned Op, EVT VT) const
Return true if the specified operation is legal on this target or can be made legal with custom lower...
bool isVector() const
isVector - Return true if this is a vector value type.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const MachineOperand & getOperand(unsigned i) const
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
Class to represent integer types.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Type * getSrcTy() const
Return the source type, as a convenience.
PPCTargetMachine - Common code between 32-bit and 64-bit PowerPC targets.
static bool clobbersCTR(const MachineInstr *MI)
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a trivially dead instruction...
pred_iterator pred_begin()
INITIALIZE_PASS_BEGIN(PPCCTRLoops,"ppc-ctr-loops","PowerPC CTR Loops", false, false) INITIALIZE_PASS_END(PPCCTRLoops
Triple - Helper class for working with autoconf configuration names.
void initializePPCCTRLoopsPass(PassRegistry &)
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::string getFullName() const
getFullName - Return a formatted string to identify this block and its parent function.
FunctionPass * createPPCCTRLoops(PPCTargetMachine &TM)
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
Type * getDestTy() const
Return the destination type, as a convenience.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
This class uses information about analyze scalars to rewrite expressions in canonical form...
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
std::vector< BlockT * >::const_iterator block_iterator
const Type * getScalarType() const LLVM_READONLY
getScalarType - If this is a vector type, return the element type, otherwise return 'this'...
block_iterator block_end() const
Representation of each machine instruction.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Value * getCondition() const
SCEV - This class represents an analyzed expression in the program.
bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool hasLocalLinkage() const
void setCondition(Value *V)
TLSModel::Model getTLSModel(const GlobalValue *GV) const
Returns the TLS model which should be used for the given global variable.
SwitchInst - Multiway switch.
unsigned getReg() const
getReg - Returns the register number.
static bool isLargeIntegerTy(bool Is32Bit, Type *Ty)
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
BasicBlockListType::iterator iterator
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
C - The default llvm calling convention, compatible with C.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Legacy analysis pass which computes a DominatorTree.
std::vector< LoopT * >::const_iterator iterator
bool isArch32Bit() const
Test whether the architecture is 32-bit.
static bool verifyCTRBranch(MachineBasicBlock *MBB, MachineBasicBlock::iterator I)
std::vector< ConstraintInfo > ConstraintInfoVector
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
ppc ctr PowerPC CTR Loops
SCEVConstant - This class represents a constant integer value.