59 #define DEBUG_TYPE "ctrloops"
65 STATISTIC(NumCTRLoops,
"Number of loops converted to CTR loops");
103 bool convertToCTRLoop(
Loop *
L);
153 return new PPCCTRLoops(TM);
158 "PowerPC CTR Loops Verify",
false,
false)
164 return new PPCCTRLoopsVerify();
168 bool PPCCTRLoops::runOnFunction(
Function &
F) {
172 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
173 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
174 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
176 auto *TLIP = getAnalysisIfAvailable<TargetLibraryInfoWrapperPass>();
177 LibInfo = TLIP ? &TLIP->getTLI() :
nullptr;
178 PreserveLCSSA = mustPreserveAnalysisID(
LCSSAID);
180 bool MadeChange =
false;
186 MadeChange |= convertToCTRLoop(L);
194 return ITy->getBitWidth() > (Is32Bit ? 32U : 64U);
202 const Value *MemAddr) {
206 if (
const auto *CV = dyn_cast<Constant>(MemAddr))
207 for (
const auto &CO : CV->operands())
214 if (!GV->isThreadLocal())
225 if (
CallInst *CI = dyn_cast<CallInst>(J)) {
226 if (
InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue())) {
229 for (
unsigned i = 0, ie = CIV.size();
i < ie; ++
i) {
232 for (
unsigned j = 0, je = C.
Codes.size(); j < je; ++j)
243 TM->getSubtargetImpl(*BB->
getParent())->getTargetLowering();
245 if (
Function *F = CI->getCalledFunction()) {
254 case Intrinsic::ppc_is_decremented_ctr_nonzero:
255 case Intrinsic::ppc_mtctr:
259 #if defined(_MSC_VER) && defined(setjmp) && \
260 !defined(setjmp_undefined_for_msvc)
261 # pragma push_macro("setjmp")
263 # define setjmp_undefined_for_msvc
266 case Intrinsic::setjmp:
268 #if defined(_MSC_VER) && defined(setjmp_undefined_for_msvc)
270 # pragma pop_macro("setjmp")
271 # undef setjmp_undefined_for_msvc
274 case Intrinsic::longjmp:
280 case Intrinsic::eh_sjlj_setjmp:
282 case Intrinsic::memcpy:
283 case Intrinsic::memmove:
284 case Intrinsic::memset:
285 case Intrinsic::powi:
287 case Intrinsic::log2:
288 case Intrinsic::log10:
290 case Intrinsic::exp2:
295 case Intrinsic::copysign:
296 if (CI->getArgOperand(0)->getType()->getScalarType()->
301 case Intrinsic::sqrt: Opcode =
ISD::FSQRT;
break;
302 case Intrinsic::floor: Opcode =
ISD::FFLOOR;
break;
303 case Intrinsic::ceil: Opcode =
ISD::FCEIL;
break;
304 case Intrinsic::trunc: Opcode =
ISD::FTRUNC;
break;
305 case Intrinsic::rint: Opcode =
ISD::FRINT;
break;
321 LibInfo->hasOptimizedCodeGen(Func)) {
323 if (!CI->onlyReadsMemory())
327 if (!CI->getArgOperand(0)->getType()->isFloatingPointTy())
331 default:
return true;
332 case LibFunc::copysign:
333 case LibFunc::copysignf:
335 case LibFunc::copysignl:
346 case LibFunc::floorf:
347 case LibFunc::floorl:
349 case LibFunc::nearbyint:
350 case LibFunc::nearbyintf:
351 case LibFunc::nearbyintl:
362 case LibFunc::roundf:
363 case LibFunc::roundl:
366 case LibFunc::truncf:
367 case LibFunc::truncl:
381 auto &
DL = CI->getModule()->getDataLayout();
398 }
else if (isa<BinaryOperator>(J) &&
399 J->getType()->getScalarType()->isPPC_FP128Ty()) {
402 }
else if (isa<UIToFPInst>(J) || isa<SIToFPInst>(J) ||
403 isa<FPToUIInst>(J) || isa<FPToSIInst>(J)) {
411 J->getType()->getScalarType()) &&
412 (J->getOpcode() == Instruction::UDiv ||
413 J->getOpcode() == Instruction::SDiv ||
414 J->getOpcode() == Instruction::URem ||
415 J->getOpcode() == Instruction::SRem)) {
419 (J->getOpcode() == Instruction::Shl ||
420 J->getOpcode() == Instruction::AShr ||
421 J->getOpcode() == Instruction::LShr)) {
425 }
else if (isa<IndirectBrInst>(J) || isa<InvokeInst>(J)) {
428 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(J)) {
432 TM->getSubtargetImpl(*BB->
getParent())->getTargetLowering();
438 if (
TM->getSubtargetImpl(*BB->
getParent())->getTargetLowering()->useSoftFloat()) {
439 switch(J->getOpcode()) {
440 case Instruction::FAdd:
441 case Instruction::FSub:
442 case Instruction::FMul:
443 case Instruction::FDiv:
444 case Instruction::FRem:
445 case Instruction::FPTrunc:
446 case Instruction::FPExt:
447 case Instruction::FPToUI:
448 case Instruction::FPToSI:
449 case Instruction::UIToFP:
450 case Instruction::SIToFP:
451 case Instruction::FCmp:
456 for (
Value *Operand : J->operands())
464 bool PPCCTRLoops::convertToCTRLoop(
Loop *L) {
465 bool MadeChange =
false;
474 MadeChange |= convertToCTRLoop(*
I);
475 DEBUG(
dbgs() <<
"Nested loop converted\n");
496 if (mightUseCTR(TT, *
I))
503 const SCEV *ExitCount =
nullptr;
506 IE = ExitingBlocks.
end();
I !=
IE; ++
I) {
507 const SCEV *
EC = SE->getExitCount(L, *
I);
508 DEBUG(
dbgs() <<
"Exit Count for " << *L <<
" from block " <<
509 (*I)->
getName() <<
": " << *EC <<
"\n");
510 if (isa<SCEVCouldNotCompute>(EC))
512 if (
const SCEVConstant *ConstEC = dyn_cast<SCEVConstant>(EC)) {
513 if (ConstEC->getValue()->isZero())
515 }
else if (!SE->isLoopInvariant(EC, L))
528 bool NotAlways =
false;
534 if (!DT->dominates(*
I, *PI)) {
548 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
549 if (!BI->isConditional())
552 CountedExitBranch = BI;
558 CountedExitBlock = *
I;
563 if (!CountedExitBlock)
572 if (!Preheader || mightUseCTR(TT, Preheader))
577 DEBUG(
dbgs() <<
"Preheader for exit count: " << Preheader->
getName() <<
"\n");
588 ExitCount->
getType() != CountType)
589 ExitCount = SE->getZeroExtendExpr(ExitCount, CountType);
590 ExitCount = SE->getAddExpr(ExitCount, SE->getOne(CountType));
592 SCEVE.expandCodeFor(ExitCount, CountType, Preheader->
getTerminator());
598 CountBuilder.CreateCall(MTCTRFunc, ECValue);
603 Value *NewCond = CondBuilder.CreateCall(DecFunc, {});
643 if (I == MBB->
begin()) {
656 unsigned Opc = I->getOpcode();
657 if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
665 " clobbers CTR, invalidating " <<
"BB#" <<
666 BI->getParent()->getNumber() <<
" (" <<
667 BI->getParent()->getFullName() <<
") instruction " <<
676 if (!CheckPreds && Preds.
empty())
682 DEBUG(
dbgs() <<
"Unable to find a MTCTR instruction for BB#" <<
683 BI->getParent()->getNumber() <<
" (" <<
684 BI->getParent()->getFullName() <<
") instruction " <<
690 PIE = MBB->
pred_end(); PI != PIE; ++PI)
696 if (!Visited.
count(MBB)) {
700 }
while (!Preds.
empty());
706 MDT = &getAnalysis<MachineDominatorTree>();
713 if (!MDT->isReachableFromEntry(MBB))
717 MIIE = MBB->
end(); MII != MIIE; ++MII) {
718 unsigned Opc = MII->getOpcode();
719 if (Opc == PPC::BDNZ8 || Opc ==
PPC::BDNZ ||
720 Opc == PPC::BDZ8 || Opc ==
PPC::BDZ)
void push_back(const T &Elt)
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
A parsed version of the target data layout string in and methods for querying it. ...
FMINNUM/FMAXNUM - Perform floating-point minimum or maximum on two values.
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
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
A Module instance is used to store all the information related to an LLVM module. ...
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
The main scalar evolution driver.
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.
StringRef getName() const
static uint64_t round(uint64_t Acc, uint64_t Input)
LLVM_NODISCARD bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
BasicBlock * InsertPreheaderForLoop(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
InsertPreheaderForLoop - Once we discover that a loop doesn't have a preheader, this method is called...
LoopT * getParentLoop() const
const Function * getParent() const
Return the enclosing method, or null if none.
static IntegerType * getInt64Ty(LLVMContext &C)
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...
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...
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
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...
LLVM_NODISCARD bool empty() const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
bool isArch64Bit() const
Test whether the architecture is 64-bit.
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)
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()
Returns an iterator to the last non-debug instruction in the basic block, or end().
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...
Type * getScalarType() const LLVM_READONLY
If this is a vector type, return the element type, otherwise return 'this'.
bool isPPC_FP128Ty() const
Return true if this is powerpc long double.
ppc ctr PowerPC CTR false
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
initializer< Ty > init(const Ty &Val)
BlockT * getLoopPreheader() const
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
Return the LLVM type of this SCEV expression.
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...
unsigned getMinimumJumpTableEntries() const
Return lower limit for number of blocks in a jump table.
bool isVector() const
isVector - Return true if this is a vector value type.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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
Return true if the specified loop is contained within in this loop.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
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.
Common code between 32-bit and 64-bit PowerPC targets.
bool isPointerTy() const
True if this is an instance of PointerType.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
If the specified value is a trivially dead instruction, delete it.
pred_iterator pred_begin()
INITIALIZE_PASS_BEGIN(PPCCTRLoops,"ppc-ctr-loops","PowerPC CTR Loops", false, false) INITIALIZE_PASS_END(PPCCTRLoops
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_READONLY APFloat maxnum(const APFloat &A, const APFloat &B)
Implements IEEE maxNum semantics.
static bool memAddrUsesCTR(const PPCTargetMachine *TM, const Value *MemAddr)
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
Return a formatted string to identify this block and its parent function.
Iterator for intrusive lists based on ilist_node.
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.
LLVM_NODISCARD T pop_back_val()
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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...
std::vector< BlockT * >::const_iterator block_iterator
block_iterator block_end() const
Representation of each machine instruction.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Value * getCondition() const
This class represents an analyzed expression in the program.
Represents a single loop in the control flow graph.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
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.
unsigned getReg() const
getReg - Returns the register number.
static bool clobbersCTR(const MachineInstr &MI)
static bool isLargeIntegerTy(bool Is32Bit, Type *Ty)
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
block_iterator block_begin() const
The legacy pass manager's analysis pass to compute loop information.
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...
LLVM_READONLY APFloat minnum(const APFloat &A, const APFloat &B)
Implements IEEE minNum semantics.
ppc ctr PowerPC CTR Loops
This class represents a constant integer value.