42 #include "llvm/Config/llvm-config.h" 100 using namespace llvm;
103 #define DEBUG_TYPE "codegenprepare" 105 STATISTIC(NumBlocksElim,
"Number of blocks eliminated");
106 STATISTIC(NumPHIsElim,
"Number of trivial PHIs eliminated");
107 STATISTIC(NumGEPsElim,
"Number of GEPs converted to casts");
108 STATISTIC(NumCmpUses,
"Number of uses of Cmp expressions replaced with uses of " 110 STATISTIC(NumCastUses,
"Number of uses of Cast expressions replaced with uses " 112 STATISTIC(NumMemoryInsts,
"Number of memory instructions whose address " 113 "computations were sunk");
115 "Number of phis created when address " 116 "computations were sunk to memory instructions");
118 "Number of select created when address " 119 "computations were sunk to memory instructions");
120 STATISTIC(NumExtsMoved,
"Number of [s|z]ext instructions combined with loads");
121 STATISTIC(NumExtUses,
"Number of uses of [s|z]ext instructions optimized");
123 "Number of and mask instructions added to form ext loads");
124 STATISTIC(NumAndUses,
"Number of uses of and mask instructions optimized");
125 STATISTIC(NumRetsDup,
"Number of return instructions duplicated");
126 STATISTIC(NumDbgValueMoved,
"Number of debug value instructions moved");
127 STATISTIC(NumSelectsExpanded,
"Number of selects turned into branches");
128 STATISTIC(NumStoreExtractExposed,
"Number of store(extractelement) exposed");
132 cl::desc(
"Disable branch optimizations in CodeGenPrepare"));
136 cl::desc(
"Disable GC optimizations in CodeGenPrepare"));
140 cl::desc(
"Disable select to branch conversion."));
144 cl::desc(
"Address sinking in CGP using GEPs."));
148 cl::desc(
"Enable sinkinig and/cmp into branches."));
152 cl::desc(
"Disable store(extract) optimizations in CodeGenPrepare"));
156 cl::desc(
"Stress test store(extract) optimizations in CodeGenPrepare"));
160 cl::desc(
"Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in " 165 cl::desc(
"Stress test ext(promotable(ld)) -> promoted(ext(ld)) " 166 "optimization in CodeGenPrepare"));
170 cl::desc(
"Disable protection against removing loop preheaders"));
174 cl::desc(
"Use profile info to add section prefix for hot/cold functions"));
178 cl::desc(
"Skip merging empty blocks if (frequency of empty block) / " 179 "(frequency of destination block) is greater than this ratio"));
183 cl::desc(
"Force store splitting no matter what the target query says."));
187 cl::desc(
"Enable merging of redundant sexts when one is dominating" 192 cl::desc(
"Disables combining addressing modes with different parts " 193 "in optimizeMemoryInst."));
197 cl::desc(
"Allow creation of Phis in Address sinking."));
201 cl::desc(
"Allow creation of selects in Address sinking."));
205 cl::desc(
"Allow combining of BaseReg field in Address sinking."));
209 cl::desc(
"Allow combining of BaseGV field in Address sinking."));
213 cl::desc(
"Allow combining of BaseOffs field in Address sinking."));
217 cl::desc(
"Allow combining of ScaledReg field in Address sinking."));
222 cl::desc(
"Enable splitting large offset of GEP."));
241 class TypePromotionTransaction;
251 std::unique_ptr<BlockFrequencyInfo>
BFI;
252 std::unique_ptr<BranchProbabilityInfo> BPI;
266 SetOfInstrs InsertedInsts;
270 InstrToOrigTy PromotedInsts;
273 SetOfInstrs RemovedInsts;
293 ValueToSExts ValToSExtendedUses;
313 StringRef getPassName()
const override {
return "CodeGen Prepare"; }
324 template <
typename F>
325 void resetIteratorIfInvalidatedWhileCalling(
BasicBlock *BB, F f) {
329 Value *CurValue = &*CurInstIterator;
336 if (IterHandle != CurValue) {
337 CurInstIterator = BB->
begin();
342 bool eliminateFallThrough(
Function &F);
343 bool eliminateMostlyEmptyBlocks(
Function &F);
346 void eliminateMostlyEmptyBlock(
BasicBlock *BB);
349 bool optimizeBlock(
BasicBlock &BB,
bool &ModifiedDT);
352 Type *AccessTy,
unsigned AddrSpace);
353 bool optimizeInlineAsmInst(
CallInst *CS);
354 bool optimizeCallInst(
CallInst *CI,
bool &ModifiedDT);
361 bool optimizeExtractElementInst(
Instruction *Inst);
362 bool dupRetToEnableTailCallOpts(
BasicBlock *BB);
366 bool tryToPromoteExts(TypePromotionTransaction &TPT,
369 unsigned CreatedInstsCost = 0);
371 bool splitLargeGEPOffsets();
372 bool performAddressTypePromotion(
374 bool AllowPromotionWithoutCommonHeader,
375 bool HasPromoted, TypePromotionTransaction &TPT,
377 bool splitBranchCondition(
Function &F);
386 "Optimize for code generation",
false,
false)
399 bool EverMadeChange =
false;
401 InsertedInsts.clear();
402 PromotedInsts.clear();
405 if (
auto *TPC = getAnalysisIfAvailable<TargetPassConfig>()) {
407 SubtargetInfo =
TM->getSubtargetImpl(F);
408 TLI = SubtargetInfo->getTargetLowering();
409 TRI = SubtargetInfo->getRegisterInfo();
411 TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
412 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
413 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
419 &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
430 TLI->isSlowDivBypassed()) {
432 TLI->getBypassSlowDivWidths();
434 while (BB !=
nullptr) {
445 EverMadeChange |= eliminateMostlyEmptyBlocks(F);
448 EverMadeChange |= splitBranchCondition(F);
454 bool MadeChange =
true;
459 bool ModifiedDTOnIteration =
false;
460 MadeChange |= optimizeBlock(*BB, ModifiedDTOnIteration);
463 if (ModifiedDTOnIteration)
467 MadeChange |= mergeSExts(F);
468 if (!LargeOffsetGEPMap.empty())
469 MadeChange |= splitLargeGEPOffsets();
475 EverMadeChange |= MadeChange;
476 SeenChainsForSExt.clear();
477 ValToSExtendedUses.clear();
478 RemovedInsts.clear();
479 LargeOffsetGEPMap.clear();
480 LargeOffsetGEPID.clear();
494 if (!MadeChange)
continue;
497 II = Successors.
begin(),
IE = Successors.
end(); II !=
IE; ++II)
503 MadeChange |= !WorkList.
empty();
504 while (!WorkList.
empty()) {
511 II = Successors.
begin(),
IE = Successors.
end(); II !=
IE; ++II)
518 if (EverMadeChange || MadeChange)
519 MadeChange |= eliminateFallThrough(F);
521 EverMadeChange |= MadeChange;
530 for (
auto &
I : Statepoints)
531 EverMadeChange |= simplifyOffsetableRelocate(*
I);
536 EverMadeChange |= placeDbgValues(F);
538 return EverMadeChange;
544 bool CodeGenPrepare::eliminateFallThrough(
Function &F) {
545 bool Changed =
false;
553 for (
auto &Block : Blocks) {
554 auto *BB = cast_or_null<BasicBlock>(Block);
562 if (!SinglePred || SinglePred == BB || BB->
hasAddressTaken())
continue;
586 if (BBI != BB->
begin()) {
588 while (isa<DbgInfoIntrinsic>(BBI)) {
589 if (BBI == BB->
begin())
593 if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
602 if (!canMergeBlocks(BB, DestBB))
612 bool CodeGenPrepare::eliminateMostlyEmptyBlocks(
Function &F) {
615 while (!LoopList.empty()) {
616 Loop *L = LoopList.pop_back_val();
617 LoopList.insert(LoopList.end(), L->
begin(), L->
end());
619 Preheaders.
insert(Preheader);
622 bool MadeChange =
false;
630 for (
auto &Block : Blocks) {
631 BasicBlock *BB = cast_or_null<BasicBlock>(Block);
634 BasicBlock *DestBB = findDestBlockOfMergeableEmptyBlock(BB);
636 !isMergingEmptyBlockProfitable(BB, DestBB, Preheaders.
count(BB)))
639 eliminateMostlyEmptyBlock(BB);
645 bool CodeGenPrepare::isMergingEmptyBlockProfitable(
BasicBlock *BB,
685 if (!isa<PHINode>(DestBB->
begin()))
695 if (DestBBPred == BB)
699 return DestPN.getIncomingValueForBlock(BB) ==
700 DestPN.getIncomingValueForBlock(DestBBPred);
702 SameIncomingValueBBs.
insert(DestBBPred);
708 if (SameIncomingValueBBs.
count(Pred))
714 for (
auto SameValueBB : SameIncomingValueBBs)
715 if (SameValueBB->getUniquePredecessor() == Pred &&
716 DestBB == findDestBlockOfMergeableEmptyBlock(SameValueBB))
717 BBFreq +=
BFI->getBlockFreq(SameValueBB);
726 bool CodeGenPrepare::canMergeBlocks(
const BasicBlock *BB,
734 if (UI->
getParent() != DestBB || !isa<PHINode>(UI))
740 if (
const PHINode *UPN = dyn_cast<PHINode>(UI))
741 for (
unsigned I = 0,
E = UPN->getNumIncomingValues();
I !=
E; ++
I) {
744 Insn->
getParent() != UPN->getIncomingBlock(
I))
755 if (!DestBBPN)
return true;
759 if (
const PHINode *BBPN = dyn_cast<PHINode>(BB->
begin())) {
761 for (
unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
762 BBPreds.
insert(BBPN->getIncomingBlock(i));
770 if (BBPreds.
count(Pred)) {
772 const Value *V1 = PN.getIncomingValueForBlock(Pred);
773 const Value *
V2 = PN.getIncomingValueForBlock(BB);
776 if (
const PHINode *V2PN = dyn_cast<PHINode>(V2))
777 if (V2PN->getParent() == BB)
778 V2 = V2PN->getIncomingValueForBlock(Pred);
781 if (V1 != V2)
return false;
791 void CodeGenPrepare::eliminateMostlyEmptyBlock(
BasicBlock *BB) {
801 if (SinglePred != DestBB) {
802 assert(SinglePred == BB &&
803 "Single predecessor not the same as predecessor");
815 for (
PHINode &PN : DestBB->phis()) {
817 Value *InVal = PN.removeIncomingValue(BB,
false);
822 if (InValPhi && InValPhi->
getParent() == BB) {
831 for (
unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
832 PN.addIncoming(InVal, BBPN->getIncomingBlock(i));
835 PN.addIncoming(InVal, *PI);
859 for (
auto *ThisRelocate : AllRelocateCalls) {
860 auto K = std::make_pair(ThisRelocate->getBasePtrIndex(),
861 ThisRelocate->getDerivedPtrIndex());
862 RelocateIdxMap.
insert(std::make_pair(K, ThisRelocate));
864 for (
auto &Item : RelocateIdxMap) {
865 std::pair<unsigned, unsigned>
Key = Item.first;
866 if (Key.first == Key.second)
871 auto BaseKey = std::make_pair(Key.first, Key.first);
874 auto MaybeBase = RelocateIdxMap.find(BaseKey);
875 if (MaybeBase == RelocateIdxMap.end())
880 RelocateInstMap[MaybeBase->second].push_back(I);
891 if (!
Op ||
Op->getZExtValue() > 20)
905 bool MadeChange =
false;
913 &*R != RelocatedBase; ++R)
914 if (
auto RI = dyn_cast<GCRelocateInst>(R))
923 "Not relocating a derived object of the original base object");
924 if (ToReplace->getBasePtrIndex() == ToReplace->getDerivedPtrIndex()) {
939 if (!Derived || Derived->getPointerOperand() != Base)
948 "Should always have one since it's not a terminator");
975 Value *ActualRelocatedBase = RelocatedBase;
977 ActualRelocatedBase =
978 Builder.CreateBitCast(RelocatedBase, Base->
getType());
980 Value *Replacement = Builder.CreateGEP(
981 Derived->getSourceElementType(), ActualRelocatedBase,
makeArrayRef(OffsetV));
985 Value *ActualReplacement = Replacement;
986 if (Replacement->
getType() != ToReplace->getType()) {
988 Builder.CreateBitCast(Replacement, ToReplace->
getType());
991 ToReplace->eraseFromParent();
1015 bool CodeGenPrepare::simplifyOffsetableRelocate(
Instruction &
I) {
1016 bool MadeChange =
false;
1019 for (
auto *U : I.
users())
1026 if (AllRelocateCalls.
size() < 2)
1033 if (RelocateInstMap.
empty())
1036 for (
auto &Item : RelocateInstMap)
1050 bool MadeChange =
false;
1053 Use &TheUse = UI.getUse();
1059 if (
PHINode *PN = dyn_cast<PHINode>(User)) {
1060 UserBB = PN->getIncomingBlock(TheUse);
1078 if (UserBB == DefBB)
continue;
1081 CastInst *&InsertedCast = InsertedCasts[UserBB];
1083 if (!InsertedCast) {
1087 CI->
getType(),
"", &*InsertPt);
1092 TheUse = InsertedCast;
1116 if (
auto *ASC = dyn_cast<AddrSpaceCastInst>(CI)) {
1118 ASC->getDestAddressSpace()))
1132 if (SrcVT.
bitsLT(DstVT))
return false;
1163 if (!isa<IntegerType>(Ty))
1183 auto *InsertPt = AddI->
hasOneUse() ? CI : AddI;
1186 auto *UAddWithOverflow =
1190 UAdd->setDebugLoc(Loc);
1193 Overflow->setDebugLoc(Loc);
1218 bool MadeChange =
false;
1221 Use &TheUse = UI.getUse();
1228 if (isa<PHINode>(User))
1235 if (UserBB == DefBB)
continue;
1238 CmpInst *&InsertedCmp = InsertedCmps[UserBB];
1251 TheUse = InsertedCmp;
1282 SetOfInstrs &InsertedInsts) {
1285 assert(!InsertedInsts.count(AndI) &&
1286 "Attempting to optimize already optimized and instruction");
1287 (void) InsertedInsts;
1296 if (!isa<ConstantInt>(AndI->
getOperand(0)) &&
1301 for (
auto *U : AndI->
users()) {
1305 if (!isa<ICmpInst>(User))
1309 if (!CmpC || !CmpC->isZero())
1324 Use &TheUse = UI.getUse();
1342 TheUse = InsertedAnd;
1358 if (!isa<TruncInst>(User)) {
1359 if (User->
getOpcode() != Instruction::And ||
1363 const APInt &Cimm = cast<ConstantInt>(User->
getOperand(1))->getValue();
1365 if ((Cimm & (Cimm + 1)).getBoolValue())
1379 bool MadeChange =
false;
1383 TruncUI != TruncE;) {
1385 Use &TruncTheUse = TruncUI.getUse();
1386 Instruction *TruncUser = cast<Instruction>(*TruncUI);
1405 if (isa<PHINode>(TruncUser))
1410 if (UserBB == TruncUserBB)
1414 CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB];
1416 if (!InsertedShift && !InsertedTrunc) {
1420 if (ShiftI->
getOpcode() == Instruction::AShr)
1421 InsertedShift = BinaryOperator::CreateAShr(ShiftI->
getOperand(0), CI,
1424 InsertedShift = BinaryOperator::CreateLShr(ShiftI->
getOperand(0), CI,
1431 assert(TruncInsertPt != TruncUserBB->
end());
1434 TruncI->
getType(),
"", &*TruncInsertPt);
1439 TruncTheUse = InsertedTrunc;
1472 bool MadeChange =
false;
1475 Use &TheUse = UI.getUse();
1481 if (isa<PHINode>(User))
1489 if (UserBB == DefBB) {
1504 if (isa<TruncInst>(User) && shiftIsLegal
1517 if (!InsertedShift) {
1521 if (ShiftI->
getOpcode() == Instruction::AShr)
1522 InsertedShift = BinaryOperator::CreateAShr(ShiftI->
getOperand(0), CI,
1525 InsertedShift = BinaryOperator::CreateLShr(ShiftI->
getOperand(0), CI,
1533 TheUse = InsertedShift;
1598 Builder.SetCurrentDebugLocation(CountZeros->
getDebugLoc());
1603 Value *Cmp = Builder.CreateICmpEQ(CountZeros->
getOperand(0), Zero,
"cmpz");
1604 Builder.CreateCondBr(Cmp, EndBlock, CallBlock);
1609 Builder.SetInsertPoint(&EndBlock->
front());
1610 PHINode *PN = Builder.CreatePHI(Ty, 2,
"ctz");
1612 Value *BitWidth = Builder.getInt(
APInt(SizeInBits, SizeInBits));
1624 bool CodeGenPrepare::optimizeCallInst(
CallInst *CI,
bool &ModifiedDT) {
1631 if (TLI->ExpandInlineAsm(CI)) {
1633 CurInstIterator = BB->
begin();
1640 if (optimizeInlineAsmInst(CI))
1646 unsigned MinSize, PrefAlign;
1647 if (TLI && TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) {
1656 cast<PointerType>(
Arg->
getType())->getAddressSpace()),
1660 if ((Offset2 & (PrefAlign-1)) != 0)
1663 if ((AI = dyn_cast<AllocaInst>(Val)) && AI->
getAlignment() < PrefAlign &&
1681 if (DestAlign >
MI->getDestAlignment())
1682 MI->setDestAlignment(DestAlign);
1685 if (SrcAlign > MTI->getSourceAlignment())
1686 MTI->setSourceAlignment(SrcAlign);
1705 switch (II->getIntrinsicID()) {
1712 resetIteratorIfInvalidatedWhileCalling(BB, [&]() {
1720 resetIteratorIfInvalidatedWhileCalling(BB, [&]() {
1735 InsertedInsts.insert(ExtVal);
1740 Value *ArgVal = II->getArgOperand(0);
1741 auto it = LargeOffsetGEPMap.find(II);
1742 if (it != LargeOffsetGEPMap.end()) {
1746 auto GEPs = std::move(it->second);
1747 LargeOffsetGEPMap[ArgVal].append(GEPs.begin(), GEPs.end());
1748 LargeOffsetGEPMap.erase(II);
1751 II->replaceAllUsesWith(ArgVal);
1752 II->eraseFromParent();
1764 if (TLI->getAddrModeArguments(II, PtrOps, AccessTy))
1765 while (!PtrOps.
empty()) {
1768 if (optimizeMemoryInst(II, PtrVal, AccessTy, AS))
1821 bool CodeGenPrepare::dupRetToEnableTailCallOpts(
BasicBlock *BB) {
1849 do { ++BI; }
while (isa<DbgInfoIntrinsic>(BI));
1857 while (isa<DbgInfoIntrinsic>(BI)) ++BI;
1871 TLI->mayBeEmittedAsTailCall(CI) &&
1878 if (!VisitedBBs.
insert(*PI).second)
1884 do { ++RI; }
while (RI != RE && isa<DbgInfoIntrinsic>(&*RI));
1889 if (CI && CI->
use_empty() && TLI->mayBeEmittedAsTailCall(CI) &&
1895 bool Changed =
false;
1896 for (
unsigned i = 0, e = TailCalls.
size(); i != e; ++i) {
1909 ModifiedDT = Changed =
true;
1929 Value *BaseReg =
nullptr;
1930 Value *ScaledReg =
nullptr;
1931 Value *OriginalValue =
nullptr;
1935 BaseRegField = 0x01,
1937 BaseOffsField = 0x04,
1938 ScaledRegField = 0x08,
1940 MultipleFields = 0xff
1943 ExtAddrMode() =
default;
1948 FieldName
compare(
const ExtAddrMode &other) {
1951 if (BaseReg && other.BaseReg &&
1952 BaseReg->
getType() != other.BaseReg->getType())
1953 return MultipleFields;
1954 if (BaseGV && other.BaseGV &&
1955 BaseGV->getType() != other.BaseGV->getType())
1956 return MultipleFields;
1957 if (ScaledReg && other.ScaledReg &&
1958 ScaledReg->
getType() != other.ScaledReg->getType())
1959 return MultipleFields;
1962 unsigned Result = NoField;
1963 if (BaseReg != other.BaseReg)
1964 Result |= BaseRegField;
1965 if (BaseGV != other.BaseGV)
1966 Result |= BaseGVField;
1967 if (BaseOffs != other.BaseOffs)
1968 Result |= BaseOffsField;
1969 if (ScaledReg != other.ScaledReg)
1970 Result |= ScaledRegField;
1973 if (Scale && other.Scale && Scale != other.Scale)
1974 Result |= ScaleField;
1977 return MultipleFields;
1979 return static_cast<FieldName
>(Result);
1989 return !BaseOffs && !Scale && !(BaseGV && BaseReg);
2000 case ScaledRegField:
2007 void SetCombinedField(FieldName Field,
Value *V,
2013 case ExtAddrMode::BaseRegField:
2016 case ExtAddrMode::BaseGVField:
2019 assert(BaseReg ==
nullptr);
2023 case ExtAddrMode::ScaledRegField:
2028 for (
const ExtAddrMode &AM : AddrModes)
2034 case ExtAddrMode::BaseOffsField:
2037 assert(ScaledReg ==
nullptr);
2055 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2057 bool NeedPlus =
false;
2060 OS << (NeedPlus ?
" + " :
"")
2062 BaseGV->printAsOperand(OS,
false);
2067 OS << (NeedPlus ?
" + " :
"")
2073 OS << (NeedPlus ?
" + " :
"")
2075 BaseReg->printAsOperand(OS,
false);
2079 OS << (NeedPlus ?
" + " :
"")
2081 ScaledReg->printAsOperand(OS,
false);
2098 class TypePromotionTransaction {
2102 class TypePromotionAction {
2110 TypePromotionAction(
Instruction *Inst) : Inst(Inst) {}
2112 virtual ~TypePromotionAction() =
default;
2119 virtual void undo() = 0;
2124 virtual void commit() {
2130 class InsertionHandler {
2141 bool HasPrevInstruction;
2148 if (HasPrevInstruction)
2149 Point.PrevInst = &*--It;
2156 if (HasPrevInstruction) {
2171 class InstructionMoveBefore :
public TypePromotionAction {
2178 : TypePromotionAction(Inst),
Position(Inst) {
2179 LLVM_DEBUG(
dbgs() <<
"Do: move: " << *Inst <<
"\nbefore: " << *Before
2185 void undo()
override {
2187 Position.insert(Inst);
2192 class OperandSetter :
public TypePromotionAction {
2202 : TypePromotionAction(Inst), Idx(Idx) {
2204 <<
"for:" << *Inst <<
"\n" 2205 <<
"with:" << *NewVal <<
"\n");
2211 void undo()
override {
2213 <<
"for: " << *Inst <<
"\n" 2214 <<
"with: " << *Origin <<
"\n");
2221 class OperandsHider :
public TypePromotionAction {
2227 OperandsHider(
Instruction *Inst) : TypePromotionAction(Inst) {
2230 OriginalValues.
reserve(NumOpnds);
2231 for (
unsigned It = 0; It < NumOpnds; ++It) {
2243 void undo()
override {
2245 for (
unsigned It = 0, EndIt = OriginalValues.
size(); It != EndIt; ++It)
2251 class TruncBuilder :
public TypePromotionAction {
2258 TruncBuilder(
Instruction *Opnd,
Type *Ty) : TypePromotionAction(Opnd) {
2265 Value *getBuiltValue() {
return Val; }
2268 void undo()
override {
2270 if (
Instruction *IVal = dyn_cast<Instruction>(Val))
2271 IVal->eraseFromParent();
2276 class SExtBuilder :
public TypePromotionAction {
2284 : TypePromotionAction(InsertPt) {
2286 Val = Builder.
CreateSExt(Opnd, Ty,
"promoted");
2291 Value *getBuiltValue() {
return Val; }
2294 void undo()
override {
2296 if (
Instruction *IVal = dyn_cast<Instruction>(Val))
2297 IVal->eraseFromParent();
2302 class ZExtBuilder :
public TypePromotionAction {
2310 : TypePromotionAction(InsertPt) {
2312 Val = Builder.
CreateZExt(Opnd, Ty,
"promoted");
2317 Value *getBuiltValue() {
return Val; }
2320 void undo()
override {
2322 if (
Instruction *IVal = dyn_cast<Instruction>(Val))
2323 IVal->eraseFromParent();
2328 class TypeMutator :
public TypePromotionAction {
2335 : TypePromotionAction(Inst), OrigTy(Inst->
getType()) {
2336 LLVM_DEBUG(
dbgs() <<
"Do: MutateType: " << *Inst <<
" with " << *NewTy
2342 void undo()
override {
2343 LLVM_DEBUG(
dbgs() <<
"Undo: MutateType: " << *Inst <<
" with " << *OrigTy
2350 class UsesReplacer :
public TypePromotionAction {
2352 struct InstructionAndIdx {
2359 InstructionAndIdx(
Instruction *Inst,
unsigned Idx)
2360 : Inst(Inst), Idx(Idx) {}
2373 LLVM_DEBUG(
dbgs() <<
"Do: UsersReplacer: " << *Inst <<
" with " << *New
2377 Instruction *UserI = cast<Instruction>(U.getUser());
2378 OriginalUses.
push_back(InstructionAndIdx(UserI, U.getOperandNo()));
2389 void undo()
override {
2391 for (use_iterator UseIt = OriginalUses.
begin(),
2392 EndIt = OriginalUses.
end();
2393 UseIt != EndIt; ++UseIt) {
2394 UseIt->Inst->setOperand(UseIt->Idx, Inst);
2400 for (
auto *DVI: DbgValues) {
2403 DVI->setOperand(0, MV);
2409 class InstructionRemover :
public TypePromotionAction {
2415 OperandsHider Hider;
2418 UsesReplacer *Replacer =
nullptr;
2421 SetOfInstrs &RemovedInsts;
2428 InstructionRemover(
Instruction *Inst, SetOfInstrs &RemovedInsts,
2429 Value *New =
nullptr)
2430 : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst),
2431 RemovedInsts(RemovedInsts) {
2433 Replacer =
new UsesReplacer(Inst, New);
2434 LLVM_DEBUG(
dbgs() <<
"Do: InstructionRemover: " << *Inst <<
"\n");
2435 RemovedInsts.insert(Inst);
2442 ~InstructionRemover()
override {
delete Replacer; }
2446 void undo()
override {
2447 LLVM_DEBUG(
dbgs() <<
"Undo: InstructionRemover: " << *Inst <<
"\n");
2448 Inserter.insert(Inst);
2452 RemovedInsts.erase(Inst);
2460 using ConstRestorationPt =
const TypePromotionAction *;
2462 TypePromotionTransaction(SetOfInstrs &RemovedInsts)
2463 : RemovedInsts(RemovedInsts) {}
2469 void rollback(ConstRestorationPt Point);
2472 ConstRestorationPt getRestorationPoint()
const;
2507 SetOfInstrs &RemovedInsts;
2512 void TypePromotionTransaction::setOperand(
Instruction *Inst,
unsigned Idx,
2514 Actions.
push_back(llvm::make_unique<TypePromotionTransaction::OperandSetter>(
2515 Inst, Idx, NewVal));
2521 llvm::make_unique<TypePromotionTransaction::InstructionRemover>(
2522 Inst, RemovedInsts, NewVal));
2525 void TypePromotionTransaction::replaceAllUsesWith(
Instruction *Inst,
2528 llvm::make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New));
2531 void TypePromotionTransaction::mutateType(
Instruction *Inst,
Type *NewTy) {
2533 llvm::make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy));
2538 std::unique_ptr<TruncBuilder> Ptr(
new TruncBuilder(Opnd, Ty));
2539 Value *Val = Ptr->getBuiltValue();
2540 Actions.push_back(std::move(Ptr));
2546 std::unique_ptr<SExtBuilder> Ptr(
new SExtBuilder(Inst, Opnd, Ty));
2547 Value *Val = Ptr->getBuiltValue();
2548 Actions.push_back(std::move(Ptr));
2554 std::unique_ptr<ZExtBuilder> Ptr(
new ZExtBuilder(Inst, Opnd, Ty));
2555 Value *Val = Ptr->getBuiltValue();
2556 Actions.push_back(std::move(Ptr));
2560 void TypePromotionTransaction::moveBefore(
Instruction *Inst,
2563 llvm::make_unique<TypePromotionTransaction::InstructionMoveBefore>(
2567 TypePromotionTransaction::ConstRestorationPt
2568 TypePromotionTransaction::getRestorationPoint()
const {
2569 return !Actions.empty() ? Actions.back().get() :
nullptr;
2572 void TypePromotionTransaction::commit() {
2573 for (CommitPt It = Actions.begin(), EndIt = Actions.end(); It != EndIt;
2579 void TypePromotionTransaction::rollback(
2580 TypePromotionTransaction::ConstRestorationPt Point) {
2581 while (!Actions.empty() && Point != Actions.back().get()) {
2582 std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val();
2592 class AddressingModeMatcher {
2609 const SetOfInstrs &InsertedInsts;
2612 InstrToOrigTy &PromotedInsts;
2615 TypePromotionTransaction &TPT;
2618 std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP;
2622 bool IgnoreProfitability;
2624 AddressingModeMatcher(
2627 ExtAddrMode &AM,
const SetOfInstrs &InsertedInsts,
2628 InstrToOrigTy &PromotedInsts, TypePromotionTransaction &TPT,
2630 : AddrModeInsts(AMI), TLI(TLI),
TRI(TRI),
2632 MemoryInst(MI),
AddrMode(AM), InsertedInsts(InsertedInsts),
2633 PromotedInsts(PromotedInsts), TPT(TPT), LargeOffsetGEP(LargeOffsetGEP) {
2634 IgnoreProfitability =
false;
2649 const SetOfInstrs &InsertedInsts, InstrToOrigTy &PromotedInsts,
2650 TypePromotionTransaction &TPT,
2654 bool Success = AddressingModeMatcher(AddrModeInsts, TLI, TRI, AccessTy, AS,
2655 MemoryInst, Result, InsertedInsts,
2656 PromotedInsts, TPT, LargeOffsetGEP)
2658 (void)Success;
assert(Success &&
"Couldn't select *anything*?");
2663 bool matchScaledValue(
Value *ScaleReg, int64_t Scale,
unsigned Depth);
2664 bool matchAddr(
Value *Addr,
unsigned Depth);
2665 bool matchOperationAddr(
User *AddrInst,
unsigned Opcode,
unsigned Depth,
2666 bool *MovedAway =
nullptr);
2667 bool isProfitableToFoldIntoAddressingMode(
Instruction *I,
2668 ExtAddrMode &AMBefore,
2669 ExtAddrMode &AMAfter);
2670 bool valueAlreadyLiveAtInst(
Value *Val,
Value *KnownLive1,
Value *KnownLive2);
2671 bool isPromotionProfitable(
unsigned NewCost,
unsigned OldCost,
2672 Value *PromotedOperand)
const;
2678 class PhiNodeSetIterator {
2679 PhiNodeSet *
const Set;
2680 size_t CurrentIndex = 0;
2685 PhiNodeSetIterator(PhiNodeSet *
const Set,
size_t Start);
2687 PhiNodeSetIterator& operator++();
2688 bool operator==(
const PhiNodeSetIterator &RHS)
const;
2689 bool operator!=(
const PhiNodeSetIterator &RHS)
const;
2703 friend class PhiNodeSetIterator;
2706 using iterator = PhiNodeSetIterator;
2721 size_t FirstValidElement = 0;
2728 if (NodeMap.insert(std::make_pair(Ptr, NodeList.
size())).second) {
2739 auto it = NodeMap.find(Ptr);
2740 if (it != NodeMap.end()) {
2742 SkipRemovedElements(FirstValidElement);
2752 FirstValidElement = 0;
2758 if (FirstValidElement == 0)
2759 SkipRemovedElements(FirstValidElement);
2760 return PhiNodeSetIterator(
this, FirstValidElement);
2764 iterator
end() {
return PhiNodeSetIterator(
this, NodeList.
size()); }
2767 size_t size()
const {
2768 return NodeMap.size();
2773 return NodeMap.count(Ptr);
2782 void SkipRemovedElements(
size_t &CurrentIndex) {
2783 while (CurrentIndex < NodeList.
size()) {
2784 auto it = NodeMap.find(NodeList[CurrentIndex]);
2787 if (it != NodeMap.end() && it->second == CurrentIndex)
2794 PhiNodeSetIterator::PhiNodeSetIterator(PhiNodeSet *
const Set,
size_t Start)
2795 : Set(Set), CurrentIndex(Start) {}
2799 "PhiNodeSet access out of range");
2800 return Set->NodeList[CurrentIndex];
2803 PhiNodeSetIterator& PhiNodeSetIterator::operator++() {
2805 "PhiNodeSet access out of range");
2807 Set->SkipRemovedElements(CurrentIndex);
2812 return CurrentIndex == RHS.CurrentIndex;
2816 return !((*this) == RHS);
2822 class SimplificationTracker {
2827 PhiNodeSet AllPhiNodes;
2837 auto SV = Storage.
find(V);
2838 if (SV == Storage.
end())
2848 while (!WorkList.
empty()) {
2850 if (!Visited.
insert(
P).second)
2852 if (
auto *PI = dyn_cast<Instruction>(
P))
2854 for (
auto *U : PI->users())
2857 PI->replaceAllUsesWith(V);
2858 if (
auto *PHI = dyn_cast<PHINode>(PI))
2859 AllPhiNodes.erase(PHI);
2860 if (
auto *
Select = dyn_cast<SelectInst>(PI))
2862 PI->eraseFromParent();
2873 Value* OldReplacement = Get(From);
2874 while (OldReplacement != From) {
2877 OldReplacement = Get(From);
2879 assert(Get(To) == To &&
"Replacement PHI node is already replaced.");
2882 AllPhiNodes.erase(From);
2886 PhiNodeSet& newPhiNodes() {
return AllPhiNodes; }
2888 void insertNewPhi(
PHINode *PN) { AllPhiNodes.insert(PN); }
2892 unsigned countNewPhiNodes()
const {
return AllPhiNodes.size(); }
2894 unsigned countNewSelectNodes()
const {
return AllSelectNodes.
size(); }
2896 void destroyNewNodes(
Type *CommonType) {
2899 for (
auto I : AllPhiNodes) {
2903 AllPhiNodes.
clear();
2904 for (
auto I : AllSelectNodes) {
2908 AllSelectNodes.
clear();
2913 class AddressingModeCombiner {
2915 typedef std::pair<PHINode *, PHINode *> PHIPair;
2922 ExtAddrMode::FieldName DifferentField = ExtAddrMode::NoField;
2925 bool AllAddrModesTrivial =
true;
2938 : CommonType(
nullptr), SQ(_SQ), Original(OriginalValue) {}
2942 return AddrModes[0];
2948 bool addNewAddrMode(ExtAddrMode &NewAddrMode) {
2952 AllAddrModesTrivial = AllAddrModesTrivial && NewAddrMode.isTrivial();
2955 if (AddrModes.
empty()) {
2963 ExtAddrMode::FieldName ThisDifferentField =
2964 AddrModes[0].compare(NewAddrMode);
2965 if (DifferentField == ExtAddrMode::NoField)
2966 DifferentField = ThisDifferentField;
2967 else if (DifferentField != ThisDifferentField)
2968 DifferentField = ExtAddrMode::MultipleFields;
2971 bool CanHandle = DifferentField != ExtAddrMode::MultipleFields;
2974 CanHandle = CanHandle && DifferentField != ExtAddrMode::ScaleField;
2979 CanHandle = CanHandle && (DifferentField != ExtAddrMode::BaseOffsField ||
2980 !NewAddrMode.ScaledReg);
2984 CanHandle = CanHandle && (DifferentField != ExtAddrMode::BaseGVField ||
2985 !NewAddrMode.HasBaseReg);
3002 bool combineAddrModes() {
3004 if (AddrModes.
size() == 0)
3008 if (AddrModes.
size() == 1 || DifferentField == ExtAddrMode::NoField)
3013 if (AllAddrModesTrivial)
3016 if (!addrModeCombiningAllowed())
3022 FoldAddrToValueMapping Map;
3023 if (!initializeMap(Map))
3026 Value *CommonValue = findCommon(Map);
3028 AddrModes[0].SetCombinedField(DifferentField, CommonValue, AddrModes);
3029 return CommonValue !=
nullptr;
3038 bool initializeMap(FoldAddrToValueMapping &Map) {
3043 for (
auto &AM : AddrModes) {
3044 Value *DV = AM.GetFieldAsValue(DifferentField, IntPtrTy);
3047 if (CommonType && CommonType !=
Type)
3050 Map[AM.OriginalValue] = DV;
3055 assert(CommonType &&
"At least one non-null value must be!");
3056 for (
auto *V : NullValue)
3084 Value *findCommon(FoldAddrToValueMapping &Map) {
3092 SimplificationTracker
ST(SQ);
3097 InsertPlaceholders(Map, TraverseOrder, ST);
3100 FillPlaceholders(Map, TraverseOrder, ST);
3103 ST.destroyNewNodes(CommonType);
3108 unsigned PhiNotMatchedCount = 0;
3110 ST.destroyNewNodes(CommonType);
3114 auto *Result = ST.Get(Map.find(Original)->second);
3116 NumMemoryInstsPhiCreated += ST.countNewPhiNodes() + PhiNotMatchedCount;
3117 NumMemoryInstsSelectCreated += ST.countNewSelectNodes();
3126 PhiNodeSet &PhiNodesToMatch) {
3128 Matcher.
insert({ PHI, Candidate });
3131 while (!WorkList.
empty()) {
3133 if (!Visited.
insert(Item).second)
3140 for (
auto B : Item.first->blocks()) {
3141 Value *FirstValue = Item.first->getIncomingValueForBlock(
B);
3142 Value *SecondValue = Item.second->getIncomingValueForBlock(
B);
3143 if (FirstValue == SecondValue)
3153 if (!FirstPhi || !SecondPhi || !PhiNodesToMatch.count(FirstPhi) ||
3158 if (Matcher.
count({ FirstPhi, SecondPhi }))
3162 Matcher.
insert({ FirstPhi, SecondPhi });
3164 WorkList.
push_back({ FirstPhi, SecondPhi });
3173 bool MatchPhiSet(SimplificationTracker &
ST,
bool AllowNewPhiNodes,
3174 unsigned &PhiNotMatchedCount) {
3180 PhiNodeSet &PhiNodesToMatch = ST.newPhiNodes();
3181 while (PhiNodesToMatch.size()) {
3182 PHINode *PHI = *PhiNodesToMatch.begin();
3185 WillNotMatch.
clear();
3186 WillNotMatch.
insert(PHI);
3189 bool IsMatched =
false;
3193 if ((IsMatched = MatchPhiNode(PHI, &
P, Matched, PhiNodesToMatch)))
3198 for (
auto M : Matched)
3199 WillNotMatch.
insert(M.first);
3204 for (
auto MV : Matched)
3205 ST.ReplacePhi(MV.first, MV.second);
3210 if (!AllowNewPhiNodes)
3213 PhiNotMatchedCount += WillNotMatch.
size();
3214 for (
auto *
P : WillNotMatch)
3215 PhiNodesToMatch.erase(
P);
3220 void FillPlaceholders(FoldAddrToValueMapping &Map,
3222 SimplificationTracker &ST) {
3223 while (!TraverseOrder.
empty()) {
3225 assert(Map.find(Current) != Map.end() &&
"No node to fill!!!");
3226 Value *V = Map[Current];
3230 auto *CurrentSelect = cast<SelectInst>(Current);
3231 auto *TrueValue = CurrentSelect->getTrueValue();
3232 assert(Map.find(TrueValue) != Map.end() &&
"No True Value!");
3233 Select->setTrueValue(ST.Get(Map[TrueValue]));
3234 auto *FalseValue = CurrentSelect->getFalseValue();
3235 assert(Map.find(FalseValue) != Map.end() &&
"No False Value!");
3236 Select->setFalseValue(ST.Get(Map[FalseValue]));
3239 PHINode *PHI = cast<PHINode>(V);
3243 Value *PV = CurrentPhi->getIncomingValueForBlock(
B);
3244 assert(Map.find(PV) != Map.end() &&
"No predecessor Value!");
3248 Map[Current] = ST.Simplify(V);
3257 void InsertPlaceholders(FoldAddrToValueMapping &Map,
3259 SimplificationTracker &ST) {
3261 assert((isa<PHINode>(Original) || isa<SelectInst>(Original)) &&
3262 "Address must be a Phi or Select node");
3265 while (!Worklist.
empty()) {
3268 if (Map.find(Current) != Map.end())
3274 if (
SelectInst *CurrentSelect = dyn_cast<SelectInst>(Current)) {
3279 CurrentSelect->getName(), CurrentSelect, CurrentSelect);
3281 ST.insertNewSelect(Select);
3283 Worklist.
push_back(CurrentSelect->getTrueValue());
3284 Worklist.
push_back(CurrentSelect->getFalseValue());
3287 PHINode *CurrentPhi = cast<PHINode>(Current);
3292 ST.insertNewPhi(PHI);
3299 bool addrModeCombiningAllowed() {
3302 switch (DifferentField) {
3305 case ExtAddrMode::BaseRegField:
3307 case ExtAddrMode::BaseGVField:
3309 case ExtAddrMode::BaseOffsField:
3311 case ExtAddrMode::ScaledRegField:
3321 bool AddressingModeMatcher::matchScaledValue(
Value *ScaleReg, int64_t Scale,
3326 return matchAddr(ScaleReg, Depth);
3337 ExtAddrMode TestAddrMode =
AddrMode;
3341 TestAddrMode.Scale += Scale;
3342 TestAddrMode.ScaledReg = ScaleReg;
3345 if (!TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace))
3355 if (isa<Instruction>(ScaleReg) &&
3357 TestAddrMode.ScaledReg = AddLHS;
3358 TestAddrMode.BaseOffs += CI->
getSExtValue()*TestAddrMode.Scale;
3362 if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) {
3363 AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
3379 case Instruction::BitCast:
3380 case Instruction::AddrSpaceCast:
3385 case Instruction::PtrToInt:
3388 case Instruction::IntToPtr:
3393 case Instruction::Mul:
3394 case Instruction::Shl:
3397 case Instruction::GetElementPtr:
3425 class TypePromotionHelper {
3428 static void addPromotedInst(InstrToOrigTy &PromotedInsts,
3431 ExtType ExtTy = IsSExt ? SignExtension : ZeroExtension;
3432 InstrToOrigTy::iterator It = PromotedInsts.find(ExtOpnd);
3433 if (It != PromotedInsts.end()) {
3436 if (It->second.getInt() == ExtTy)
3442 ExtTy = BothExtension;
3444 PromotedInsts[ExtOpnd] = TypeIsSExt(ExtOpnd->
getType(), ExtTy);
3451 static const Type *getOrigType(
const InstrToOrigTy &PromotedInsts,
3454 ExtType ExtTy = IsSExt ? SignExtension : ZeroExtension;
3455 InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd);
3456 if (It != PromotedInsts.end() && It->second.getInt() == ExtTy)
3457 return It->second.getPointer();
3472 static bool canGetThrough(
const Instruction *Inst,
Type *ConsideredExtType,
3473 const InstrToOrigTy &PromotedInsts,
bool IsSExt);
3477 static bool shouldExtOperand(
const Instruction *Inst,
int OpIdx) {
3478 return !(isa<SelectInst>(Inst) && OpIdx == 0);
3490 static Value *promoteOperandForTruncAndAnyExt(
3492 InstrToOrigTy &PromotedInsts,
unsigned &CreatedInstsCost,
3506 TypePromotionTransaction &TPT,
3507 InstrToOrigTy &PromotedInsts,
3508 unsigned &CreatedInstsCost,
3514 static Value *signExtendOperandForOther(
3516 InstrToOrigTy &PromotedInsts,
unsigned &CreatedInstsCost,
3519 return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
3520 Exts, Truncs, TLI,
true);
3524 static Value *zeroExtendOperandForOther(
3526 InstrToOrigTy &PromotedInsts,
unsigned &CreatedInstsCost,
3529 return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
3530 Exts, Truncs, TLI,
false);
3536 InstrToOrigTy &PromotedInsts,
3537 unsigned &CreatedInstsCost,
3551 static Action getAction(
Instruction *Ext,
const SetOfInstrs &InsertedInsts,
3553 const InstrToOrigTy &PromotedInsts);
3558 bool TypePromotionHelper::canGetThrough(
const Instruction *Inst,
3559 Type *ConsideredExtType,
3560 const InstrToOrigTy &PromotedInsts,
3569 if (isa<ZExtInst>(Inst))
3573 if (IsSExt && isa<SExtInst>(Inst))
3579 if (BinOp && isa<OverflowingBinaryOperator>(BinOp) &&
3585 if ((Inst->
getOpcode() == Instruction::And ||
3590 if (Inst->
getOpcode() == Instruction::Xor) {
3602 if (Inst->
getOpcode() == Instruction::LShr && !IsSExt)
3616 if (AndInst && AndInst->
getOpcode() == Instruction::And) {
3627 if (!isa<TruncInst>(Inst))
3649 const Type *OpndType = getOrigType(PromotedInsts, Opnd, IsSExt);
3652 else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd)))
3662 TypePromotionHelper::Action TypePromotionHelper::getAction(
3665 assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
3666 "Unexpected instruction type");
3669 bool IsSExt = isa<SExtInst>(
Ext);
3673 if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt))
3679 if (isa<TruncInst>(ExtOpnd) && InsertedInsts.count(ExtOpnd))
3684 if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) ||
3685 isa<ZExtInst>(ExtOpnd))
3686 return promoteOperandForTruncAndAnyExt;
3692 return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther;
3695 Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt(
3697 InstrToOrigTy &PromotedInsts,
unsigned &CreatedInstsCost,
3703 Value *ExtVal = SExt;
3704 bool HasMergedNonFreeExt =
false;
3705 if (isa<ZExtInst>(SExtOpnd)) {
3708 HasMergedNonFreeExt = !TLI.
isExtFree(SExtOpnd);
3711 TPT.replaceAllUsesWith(SExt, ZExt);
3712 TPT.eraseInstruction(SExt);
3717 TPT.setOperand(SExt, 0, SExtOpnd->
getOperand(0));
3719 CreatedInstsCost = 0;
3723 TPT.eraseInstruction(SExtOpnd);
3731 CreatedInstsCost = !TLI.
isExtFree(ExtInst) && !HasMergedNonFreeExt;
3739 TPT.eraseInstruction(ExtInst, NextVal);
3743 Value *TypePromotionHelper::promoteOperandForOther(
3745 InstrToOrigTy &PromotedInsts,
unsigned &CreatedInstsCost,
3752 CreatedInstsCost = 0;
3758 Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->
getType());
3759 if (
Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) {
3761 ITrunc->moveAfter(ExtOpnd);
3766 TPT.replaceAllUsesWith(ExtOpnd, Trunc);
3769 TPT.setOperand(Ext, 0, ExtOpnd);
3779 addPromotedInst(PromotedInsts, ExtOpnd, IsSExt);
3781 TPT.mutateType(ExtOpnd, Ext->
getType());
3783 TPT.replaceAllUsesWith(Ext, ExtOpnd);
3788 for (
int OpIdx = 0, EndOpIdx = ExtOpnd->
getNumOperands(); OpIdx != EndOpIdx;
3792 !shouldExtOperand(ExtOpnd, OpIdx)) {
3798 if (
const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) {
3801 APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth)
3802 : Cst->getValue().zext(BitWidth);
3807 if (isa<UndefValue>(Opnd)) {
3818 Value *ValForExtOpnd = IsSExt ? TPT.createSExt(Ext, Opnd, Ext->
getType())
3819 : TPT.createZExt(Ext, Opnd, Ext->
getType());
3820 if (!isa<Instruction>(ValForExtOpnd)) {
3821 TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd);
3824 ExtForOpnd = cast<Instruction>(ValForExtOpnd);
3828 TPT.setOperand(ExtForOpnd, 0, Opnd);
3831 TPT.moveBefore(ExtForOpnd, ExtOpnd);
3832 TPT.setOperand(ExtOpnd, OpIdx, ExtForOpnd);
3833 CreatedInstsCost += !TLI.
isExtFree(ExtForOpnd);
3835 ExtForOpnd =
nullptr;
3837 if (ExtForOpnd == Ext) {
3839 TPT.eraseInstruction(Ext);
3852 bool AddressingModeMatcher::isPromotionProfitable(
3853 unsigned NewCost,
unsigned OldCost,
Value *PromotedOperand)
const {
3854 LLVM_DEBUG(
dbgs() <<
"OldCost: " << OldCost <<
"\tNewCost: " << NewCost
3859 if (NewCost > OldCost)
3861 if (NewCost < OldCost)
3880 bool AddressingModeMatcher::matchOperationAddr(
User *AddrInst,
unsigned Opcode,
3884 if (Depth >= 5)
return false;
3891 case Instruction::PtrToInt:
3894 case Instruction::IntToPtr: {
3902 case Instruction::BitCast:
3912 case Instruction::AddrSpaceCast: {
3922 ExtAddrMode BackupAddrMode =
AddrMode;
3923 unsigned OldSize = AddrModeInsts.size();
3928 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
3929 TPT.getRestorationPoint();
3931 if (matchAddr(AddrInst->
getOperand(1), Depth+1) &&
3937 AddrModeInsts.resize(OldSize);
3938 TPT.rollback(LastKnownGood);
3941 if (matchAddr(AddrInst->
getOperand(0), Depth+1) &&
3947 AddrModeInsts.resize(OldSize);
3948 TPT.rollback(LastKnownGood);
3954 case Instruction::Mul:
3955 case Instruction::Shl: {
3961 if (Opcode == Instruction::Shl)
3962 Scale = 1LL << Scale;
3966 case Instruction::GetElementPtr: {
3969 int VariableOperand = -1;
3970 unsigned VariableScale = 0;
3972 int64_t ConstantOffset = 0;
3974 for (
unsigned i = 1, e = AddrInst->
getNumOperands(); i != e; ++i, ++GTI) {
3978 cast<ConstantInt>(AddrInst->
getOperand(i))->getZExtValue();
3983 const APInt &CVal = CI->getValue();
3991 if (VariableOperand != -1)
3995 VariableOperand = i;
3996 VariableScale = TypeSize;
4003 if (VariableOperand == -1) {
4004 AddrMode.BaseOffs += ConstantOffset;
4005 if (ConstantOffset == 0 ||
4008 if (matchAddr(AddrInst->
getOperand(0), Depth+1))
4012 ConstantOffset > 0) {
4019 auto *
GEP = cast<GetElementPtrInst>(AddrInst);
4020 if (isa<Argument>(Base) || isa<GlobalValue>(Base) ||
4021 (BaseI && !isa<CastInst>(BaseI) &&
4022 !isa<GetElementPtrInst>(BaseI))) {
4032 LargeOffsetGEP = std::make_pair(GEP, ConstantOffset);
4035 AddrMode.BaseOffs -= ConstantOffset;
4040 ExtAddrMode BackupAddrMode =
AddrMode;
4041 unsigned OldSize = AddrModeInsts.size();
4044 AddrMode.BaseOffs += ConstantOffset;
4047 if (!matchAddr(AddrInst->
getOperand(0), Depth+1)) {
4051 AddrModeInsts.resize(OldSize);
4059 if (!matchScaledValue(AddrInst->
getOperand(VariableOperand), VariableScale,
4064 AddrModeInsts.resize(OldSize);
4069 AddrMode.BaseOffs += ConstantOffset;
4070 if (!matchScaledValue(AddrInst->
getOperand(VariableOperand),
4071 VariableScale,
Depth)) {
4074 AddrModeInsts.resize(OldSize);
4081 case Instruction::SExt:
4082 case Instruction::ZExt: {
4089 TypePromotionHelper::Action TPH =
4090 TypePromotionHelper::getAction(Ext, InsertedInsts, TLI, PromotedInsts);
4094 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4095 TPT.getRestorationPoint();
4096 unsigned CreatedInstsCost = 0;
4098 Value *PromotedOperand =
4099 TPH(Ext, TPT, PromotedInsts, CreatedInstsCost,
nullptr,
nullptr, TLI);
4114 assert(PromotedOperand &&
4115 "TypePromotionHelper should have filtered out those cases");
4117 ExtAddrMode BackupAddrMode =
AddrMode;
4118 unsigned OldSize = AddrModeInsts.size();
4120 if (!matchAddr(PromotedOperand, Depth) ||
4125 !isPromotionProfitable(CreatedInstsCost,
4126 ExtCost + (AddrModeInsts.size() - OldSize),
4129 AddrModeInsts.resize(OldSize);
4130 LLVM_DEBUG(
dbgs() <<
"Sign extension does not pay off: rollback\n");
4131 TPT.rollback(LastKnownGood);
4145 bool AddressingModeMatcher::matchAddr(
Value *Addr,
unsigned Depth) {
4148 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4149 TPT.getRestorationPoint();
4150 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
4152 AddrMode.BaseOffs += CI->getSExtValue();
4155 AddrMode.BaseOffs -= CI->getSExtValue();
4156 }
else if (
GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
4164 }
else if (
Instruction *I = dyn_cast<Instruction>(Addr)) {
4165 ExtAddrMode BackupAddrMode =
AddrMode;
4166 unsigned OldSize = AddrModeInsts.size();
4169 bool MovedAway =
false;
4179 isProfitableToFoldIntoAddressingMode(I, BackupAddrMode,
AddrMode)) {
4180 AddrModeInsts.push_back(I);
4187 AddrModeInsts.resize(OldSize);
4188 TPT.rollback(LastKnownGood);
4190 }
else if (
ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
4191 if (matchOperationAddr(CE, CE->getOpcode(),
Depth))
4193 TPT.rollback(LastKnownGood);
4194 }
else if (isa<ConstantPointerNull>(Addr)) {
4220 TPT.rollback(LastKnownGood);
4234 for (
unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
4264 if (!ConsideredInsts.
insert(I).second)
4277 if (SeenInsts++ >= MaxMemoryUsesToScan)
4280 Instruction *UserI = cast<Instruction>(U.getUser());
4281 if (
LoadInst *LI = dyn_cast<LoadInst>(UserI)) {
4282 MemoryUses.push_back(std::make_pair(LI, U.getOperandNo()));
4287 unsigned opNo = U.getOperandNo();
4290 MemoryUses.push_back(std::make_pair(
SI, opNo));
4295 unsigned opNo = U.getOperandNo();
4298 MemoryUses.push_back(std::make_pair(RMW, opNo));
4303 unsigned opNo = U.getOperandNo();
4306 MemoryUses.push_back(std::make_pair(CmpX, opNo));
4310 if (
CallInst *CI = dyn_cast<CallInst>(UserI)) {
4317 if (!IA)
return true;
4337 bool AddressingModeMatcher::valueAlreadyLiveAtInst(
Value *Val,
Value *KnownLive1,
4338 Value *KnownLive2) {
4340 if (Val ==
nullptr || Val == KnownLive1 || Val == KnownLive2)
4344 if (!isa<Instruction>(Val) && !isa<Argument>(Val))
return true;
4349 if (
AllocaInst *AI = dyn_cast<AllocaInst>(Val))
4350 if (AI->isStaticAlloca())
4380 bool AddressingModeMatcher::
4381 isProfitableToFoldIntoAddressingMode(
Instruction *I, ExtAddrMode &AMBefore,
4382 ExtAddrMode &AMAfter) {
4383 if (IgnoreProfitability)
return true;
4394 Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
4398 if (valueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
4400 if (valueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
4401 ScaledReg =
nullptr;
4405 if (!BaseReg && !ScaledReg)
4427 for (
unsigned i = 0, e = MemoryUses.
size(); i != e; ++i) {
4429 unsigned OpNo = MemoryUses[i].second;
4444 std::pair<AssertingVH<GetElementPtrInst>, int64_t> LargeOffsetGEP(
nullptr,
4446 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4447 TPT.getRestorationPoint();
4448 AddressingModeMatcher Matcher(
4449 MatchedAddrModeInsts, TLI,
TRI, AddressAccessTy, AS, MemoryInst, Result,
4450 InsertedInsts, PromotedInsts, TPT, LargeOffsetGEP);
4451 Matcher.IgnoreProfitability =
true;
4452 bool Success = Matcher.matchAddr(Address, 0);
4453 (void)Success;
assert(Success &&
"Couldn't select *anything*?");
4458 TPT.rollback(LastKnownGood);
4464 MatchedAddrModeInsts.
clear();
4497 bool CodeGenPrepare::optimizeMemoryInst(
Instruction *MemoryInst,
Value *Addr,
4498 Type *AccessTy,
unsigned AddrSpace) {
4510 bool PhiOrSelectSeen =
false;
4513 AddressingModeCombiner AddrModes(SQ, Addr);
4514 TypePromotionTransaction TPT(RemovedInsts);
4515 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4516 TPT.getRestorationPoint();
4517 while (!worklist.
empty()) {
4530 if (!Visited.
insert(V).second)
4534 if (
PHINode *
P = dyn_cast<PHINode>(V)) {
4535 for (
Value *IncValue :
P->incoming_values())
4537 PhiOrSelectSeen =
true;
4544 PhiOrSelectSeen =
true;
4551 AddrModeInsts.
clear();
4552 std::pair<AssertingVH<GetElementPtrInst>, int64_t> LargeOffsetGEP(
nullptr,
4555 V, AccessTy, AddrSpace, MemoryInst, AddrModeInsts, *TLI, *
TRI,
4556 InsertedInsts, PromotedInsts, TPT, LargeOffsetGEP);
4560 !NewGEPBases.count(GEP)) {
4565 if (LargeOffsetGEPID.find(GEP) == LargeOffsetGEPID.end())
4566 LargeOffsetGEPID[GEP] = LargeOffsetGEPID.size();
4569 NewAddrMode.OriginalValue = V;
4570 if (!AddrModes.addNewAddrMode(NewAddrMode))
4577 if (!AddrModes.combineAddrModes()) {
4578 TPT.rollback(LastKnownGood);
4584 ExtAddrMode
AddrMode = AddrModes.getAddrMode();
4590 if (!PhiOrSelectSeen &&
none_of(AddrModeInsts, [&](
Value *V) {
4613 LLVM_DEBUG(
dbgs() <<
"CGP: Reusing nonlocal addrmode: " << AddrMode
4614 <<
" for " << *MemoryInst <<
"\n");
4621 LLVM_DEBUG(
dbgs() <<
"CGP: SINKING nonlocal addrmode: " << AddrMode
4622 <<
" for " << *MemoryInst <<
"\n");
4623 Type *IntPtrTy = DL->getIntPtrType(Addr->
getType());
4624 Value *ResultPtr =
nullptr, *ResultIndex =
nullptr;
4627 if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) {
4628 ResultPtr = AddrMode.BaseReg;
4629 AddrMode.BaseReg =
nullptr;
4632 if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) {
4635 if (ResultPtr || AddrMode.Scale != 1)
4638 ResultPtr = AddrMode.ScaledReg;
4649 if (AddrMode.Scale) {
4650 Type *ScaledRegTy = AddrMode.ScaledReg->getType();
4656 if (AddrMode.BaseGV) {
4660 ResultPtr = AddrMode.BaseGV;
4666 if (!DL->isNonIntegralPointerType(Addr->
getType())) {
4667 if (!ResultPtr && AddrMode.BaseReg) {
4670 AddrMode.BaseReg =
nullptr;
4671 }
else if (!ResultPtr && AddrMode.Scale == 1) {
4679 !AddrMode.BaseReg && !AddrMode.Scale && !AddrMode.BaseOffs) {
4681 }
else if (!ResultPtr) {
4693 if (AddrMode.BaseReg) {
4694 Value *V = AddrMode.BaseReg;
4702 if (AddrMode.Scale) {
4703 Value *V = AddrMode.ScaledReg;
4704 if (V->
getType() == IntPtrTy) {
4709 "We can't transform if ScaledReg is too narrow");
4713 if (AddrMode.Scale != 1)
4717 ResultIndex = Builder.
CreateAdd(ResultIndex, V,
"sunkaddr");
4723 if (AddrMode.BaseOffs) {
4728 if (ResultPtr->
getType() != I8PtrTy)
4730 ResultPtr = Builder.
CreateGEP(I8Ty, ResultPtr, ResultIndex,
"sunkaddr");
4737 SunkAddr = ResultPtr;
4739 if (ResultPtr->
getType() != I8PtrTy)
4741 SunkAddr = Builder.
CreateGEP(I8Ty, ResultPtr, ResultIndex,
"sunkaddr");
4750 Type *BaseTy = AddrMode.BaseReg ? AddrMode.BaseReg->getType() :
nullptr;
4751 Type *ScaleTy = AddrMode.Scale ? AddrMode.ScaledReg->getType() :
nullptr;
4752 PointerType *BasePtrTy = dyn_cast_or_null<PointerType>(BaseTy);
4753 PointerType *ScalePtrTy = dyn_cast_or_null<PointerType>(ScaleTy);
4754 if (DL->isNonIntegralPointerType(Addr->
getType()) ||
4755 (BasePtrTy && DL->isNonIntegralPointerType(BasePtrTy)) ||
4756 (ScalePtrTy && DL->isNonIntegralPointerType(ScalePtrTy)) ||
4758 DL->isNonIntegralPointerType(AddrMode.BaseGV->getType())))
4761 LLVM_DEBUG(
dbgs() <<
"CGP: SINKING nonlocal addrmode: " << AddrMode
4762 <<
" for " << *MemoryInst <<
"\n");
4763 Type *IntPtrTy = DL->getIntPtrType(Addr->
getType());
4764 Value *Result =
nullptr;
4771 if (AddrMode.BaseReg) {
4772 Value *V = AddrMode.BaseReg;
4781 if (AddrMode.Scale) {
4782 Value *V = AddrMode.ScaledReg;
4783 if (V->
getType() == IntPtrTy) {
4787 }
else if (cast<IntegerType>(IntPtrTy)->
getBitWidth() <
4796 Instruction *I = dyn_cast_or_null<Instruction>(Result);
4797 if (I && (Result != AddrMode.BaseReg))
4801 if (AddrMode.Scale != 1)
4805 Result = Builder.
CreateAdd(Result, V,
"sunkaddr");
4811 if (AddrMode.BaseGV) {
4814 Result = Builder.
CreateAdd(Result, V,
"sunkaddr");
4820 if (AddrMode.BaseOffs) {
4823 Result = Builder.
CreateAdd(Result, V,
"sunkaddr");
4844 Value *CurValue = &*CurInstIterator;
4850 if (IterHandle != CurValue) {
4853 CurInstIterator = BB->
begin();
4863 bool CodeGenPrepare::optimizeInlineAsmInst(
CallInst *CS) {
4864 bool MadeChange =
false;
4867 TM->getSubtargetImpl(*CS->
getFunction())->getRegisterInfo();
4871 for (
unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
4880 MadeChange |= optimizeMemoryInst(CS, OpVal, OpVal->
getType(), ~0u);
4893 bool IsSExt = isa<SExtInst>(FirstUser);
4894 Type *ExtTy = FirstUser->getType();
4897 if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI)))
4920 if (ExtTy->getScalarType()->getIntegerBitWidth() >
4943 bool CodeGenPrepare::tryToPromoteExts(
4946 unsigned CreatedInstsCost) {
4947 bool Promoted =
false;
4950 for (
auto I : Exts) {
4965 TypePromotionHelper::Action TPH =
4966 TypePromotionHelper::getAction(I, InsertedInsts, *TLI, PromotedInsts);
4975 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
4976 TPT.getRestorationPoint();
4978 unsigned NewCreatedInstsCost = 0;
4981 Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost,
4982 &NewExts,
nullptr, *TLI);
4984 "TypePromotionHelper should have filtered out those cases");
4992 long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost;
4995 TotalCreatedInstsCost =
4996 std::max((
long long)0, (TotalCreatedInstsCost - ExtCost));
4998 (TotalCreatedInstsCost > 1 ||
5003 TPT.rollback(LastKnownGood);
5009 (void)tryToPromoteExts(TPT, NewExts, NewlyMovedExts, TotalCreatedInstsCost);
5010 bool NewPromoted =
false;
5011 for (
auto ExtInst : NewlyMovedExts) {
5012 Instruction *MovedExt = cast<Instruction>(ExtInst);
5016 if (isa<LoadInst>(ExtOperand) &&
5018 (ExtOperand->hasOneUse() ||
hasSameExtUse(ExtOperand, *TLI))))
5021 ProfitablyMovedExts.
push_back(MovedExt);
5028 TPT.rollback(LastKnownGood);
5039 bool CodeGenPrepare::mergeSExts(
Function &F) {
5041 bool Changed =
false;
5042 for (
auto &Entry : ValToSExtendedUses) {
5043 SExts &Insts = Entry.second;
5046 if (RemovedInsts.count(Inst) || !isa<SExtInst>(Inst) ||
5049 bool inserted =
false;
5050 for (
auto &Pt : CurPts) {
5052 Pt->replaceAllUsesWith(Inst);
5053 RemovedInsts.insert(Pt);
5054 Pt->removeFromParent();
5065 RemovedInsts.insert(Inst);
5072 CurPts.push_back(Inst);
5114 bool CodeGenPrepare::splitLargeGEPOffsets() {
5115 bool Changed =
false;
5116 for (
auto &Entry : LargeOffsetGEPMap) {
5117 Value *OldBase = Entry.first;
5119 &LargeOffsetGEPs = Entry.second;
5120 auto compareGEPOffset =
5121 [&](
const std::pair<GetElementPtrInst *, int64_t> &LHS,
5122 const std::pair<GetElementPtrInst *, int64_t> &RHS) {
5123 if (LHS.first == RHS.first)
5125 if (LHS.second != RHS.second)
5126 return LHS.second < RHS.second;
5127 return LargeOffsetGEPID[LHS.first] < LargeOffsetGEPID[RHS.first];
5130 llvm::sort(LargeOffsetGEPs, compareGEPOffset);
5131 LargeOffsetGEPs.
erase(
5132 std::unique(LargeOffsetGEPs.
begin(), LargeOffsetGEPs.
end()),
5133 LargeOffsetGEPs.
end());
5135 if (LargeOffsetGEPs.
front().second == LargeOffsetGEPs.
back().second)
5138 int64_t BaseOffset = LargeOffsetGEPs.
begin()->second;
5139 Value *NewBaseGEP =
nullptr;
5141 auto LargeOffsetGEP = LargeOffsetGEPs.
begin();
5142 while (LargeOffsetGEP != LargeOffsetGEPs.
end()) {
5144 int64_t
Offset = LargeOffsetGEP->second;
5145 if (Offset != BaseOffset) {
5147 AddrMode.BaseOffs = Offset - BaseOffset;
5158 NewBaseGEP =
nullptr;
5164 Type *IntPtrTy = DL->getIntPtrType(GEP->
getType());
5174 if (
auto *BaseI = dyn_cast<Instruction>(OldBase)) {
5178 if (isa<PHINode>(BaseI))
5180 else if (
InvokeInst *Invoke = dyn_cast<InvokeInst>(BaseI)) {
5182 SplitEdge(NewBaseInsertBB, Invoke->getNormalDest());
5185 NewBaseInsertPt = std::next(BaseI->getIterator());
5192 IRBuilder<> NewBaseBuilder(NewBaseInsertBB, NewBaseInsertPt);
5195 NewBaseGEP = OldBase;
5196 if (NewBaseGEP->
getType() != I8PtrTy)
5199 NewBaseBuilder.
CreateGEP(I8Ty, NewBaseGEP, BaseIndex,
"splitgep");
5200 NewGEPBases.insert(NewBaseGEP);
5204 Value *NewGEP = NewBaseGEP;
5205 if (Offset == BaseOffset) {
5206 if (GEP->
getType() != I8PtrTy)
5211 NewGEP = Builder.
CreateGEP(I8Ty, NewBaseGEP, Index);
5213 if (GEP->
getType() != I8PtrTy)
5217 LargeOffsetGEPID.erase(GEP);
5218 LargeOffsetGEP = LargeOffsetGEPs.
erase(LargeOffsetGEP);
5228 bool CodeGenPrepare::canFormExtLd(
5231 for (
auto *MovedExtInst : MovedExts) {
5232 if (isa<LoadInst>(MovedExtInst->getOperand(0))) {
5233 LI = cast<LoadInst>(MovedExtInst->getOperand(0));
5234 Inst = MovedExtInst;
5286 bool CodeGenPrepare::optimizeExt(
Instruction *&Inst) {
5292 bool AllowPromotionWithoutCommonHeader =
false;
5296 bool ATPConsiderable = TTI->shouldConsiderAddressTypePromotion(
5297 *Inst, AllowPromotionWithoutCommonHeader);
5298 TypePromotionTransaction TPT(RemovedInsts);
5299 TypePromotionTransaction::ConstRestorationPt LastKnownGood =
5300 TPT.getRestorationPoint();
5305 bool HasPromoted = tryToPromoteExts(TPT, Exts, SpeculativelyMovedExts);
5313 if (canFormExtLd(SpeculativelyMovedExts, LI, ExtFedByLoad, HasPromoted)) {
5314 assert(LI && ExtFedByLoad &&
"Expect a valid load and extension");
5327 Inst = ExtFedByLoad;
5332 if (ATPConsiderable &&
5333 performAddressTypePromotion(Inst, AllowPromotionWithoutCommonHeader,
5334 HasPromoted, TPT, SpeculativelyMovedExts))
5337 TPT.rollback(LastKnownGood);
5346 bool CodeGenPrepare::performAddressTypePromotion(
5347 Instruction *&Inst,
bool AllowPromotionWithoutCommonHeader,
5348 bool HasPromoted, TypePromotionTransaction &TPT,
5350 bool Promoted =
false;
5352 bool AllSeenFirst =
true;
5353 for (
auto I : SpeculativelyMovedExts) {
5356 SeenChainsForSExt.
find(HeadOfChain);
5359 if (AlreadySeen != SeenChainsForSExt.
end()) {
5360 if (AlreadySeen->second !=
nullptr)
5361 UnhandledExts.
insert(AlreadySeen->second);
5362 AllSeenFirst =
false;
5366 if (!AllSeenFirst || (AllowPromotionWithoutCommonHeader &&
5367 SpeculativelyMovedExts.size() == 1)) {
5371 for (
auto I : SpeculativelyMovedExts) {
5373 SeenChainsForSExt[HeadOfChain] =
nullptr;
5374 ValToSExtendedUses[HeadOfChain].push_back(I);
5377 Inst = SpeculativelyMovedExts.pop_back_val();
5382 for (
auto I : SpeculativelyMovedExts) {
5384 SeenChainsForSExt[HeadOfChain] = Inst;
5389 if (!AllSeenFirst && !UnhandledExts.
empty())
5390 for (
auto VisitedSExt : UnhandledExts) {
5391 if (RemovedInsts.count(VisitedSExt))
5393 TypePromotionTransaction TPT(RemovedInsts);
5397 bool HasPromoted = tryToPromoteExts(TPT, Exts, Chains);
5401 for (
auto I : Chains) {
5404 SeenChainsForSExt[HeadOfChain] =
nullptr;
5405 ValToSExtendedUses[HeadOfChain].push_back(I);
5411 bool CodeGenPrepare::optimizeExtUses(
Instruction *I) {
5426 if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->
getParent())
5429 bool DefIsLiveOut =
false;
5435 if (UserBB == DefBB)
continue;
5436 DefIsLiveOut =
true;
5446 if (UserBB == DefBB)
continue;
5449 if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI))
5456 bool MadeChange =
false;
5462 if (UserBB == DefBB)
continue;
5465 Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
5467 if (!InsertedTrunc) {
5469 assert(InsertPt != UserBB->end());
5471 InsertedInsts.insert(InsertedTrunc);
5540 InsertedInsts.count(cast<Instruction>(*Load->
user_begin())))
5548 for (
auto *U : Load->
users())
5549 WorkList.
push_back(cast<Instruction>(U));
5553 APInt DemandBits(BitWidth, 0);
5554 APInt WidestAndBits(BitWidth, 0);
5556 while (!WorkList.
empty()) {
5561 if (!Visited.
insert(I).second)
5565 if (
auto *Phi = dyn_cast<PHINode>(I)) {
5566 for (
auto *U : Phi->users())
5567 WorkList.
push_back(cast<Instruction>(U));
5572 case Instruction::And: {
5576 APInt AndBits = AndC->getValue();
5577 DemandBits |= AndBits;
5579 if (AndBits.
ugt(WidestAndBits))
5580 WidestAndBits = AndBits;
5586 case Instruction::Shl: {
5595 case Instruction::Trunc: {
5619 if (ActiveBits <= 1 || !DemandBits.
isMask(ActiveBits) ||
5620 WidestAndBits != DemandBits)
5637 InsertedInsts.insert(NewAnd);
5642 NewAnd->setOperand(0, Load);
5645 for (
auto *And : AndsToMaybeRemove)
5648 if (cast<ConstantInt>(And->getOperand(1))->getValue() == DemandBits) {
5649 And->replaceAllUsesWith(NewAnd);
5650 if (&*CurInstIterator == And)
5651 CurInstIterator = std::next(And->getIterator());
5652 And->eraseFromParent();
5683 uint64_t TrueWeight, FalseWeight;
5685 uint64_t Max =
std::max(TrueWeight, FalseWeight);
5686 uint64_t Sum = TrueWeight + FalseWeight;
5720 for (
SelectInst *DefSI = SI; DefSI !=
nullptr && Selects.
count(DefSI);
5723 "The condition of DefSI does not match with SI");
5724 V = (isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue());
5731 bool CodeGenPrepare::optimizeSelectInst(
SelectInst *
SI) {
5752 CurInstIterator = std::next(LastSI->
getIterator());
5816 if (TrueBlock ==
nullptr) {
5822 auto *TrueInst = cast<Instruction>(SI->
getTrueValue());
5823 TrueInst->moveBefore(TrueBranch);
5826 if (FalseBlock ==
nullptr) {
5833 FalseInst->moveBefore(FalseBranch);
5839 if (TrueBlock == FalseBlock) {
5840 assert(TrueBlock ==
nullptr &&
5841 "Unexpected basic block transform while optimizing select");
5855 if (TrueBlock ==
nullptr) {
5858 TrueBlock = StartBlock;
5859 }
else if (FalseBlock ==
nullptr) {
5862 FalseBlock = StartBlock;
5870 INS.insert(ASI.begin(), ASI.end());
5874 for (
auto It = ASI.rbegin(); It != ASI.rend(); ++It) {
5886 ++NumSelectsExpanded;
5890 CurInstIterator = StartBlock->
end();
5897 for (
unsigned i = 0; i <
Mask.size(); ++i) {
5898 if (SplatElem != -1 &&
Mask[i] != -1 &&
Mask[i] != SplatElem)
5900 SplatElem =
Mask[i];
5925 bool MadeChange =
false;
5931 if (UserBB == DefBB)
continue;
5938 Instruction *&InsertedShuffle = InsertedShuffles[UserBB];
5940 if (!InsertedShuffle) {
5942 assert(InsertPt != UserBB->end());
5961 bool CodeGenPrepare::optimizeSwitchInst(
SwitchInst *SI) {
5971 if (RegWidth <= cast<IntegerType>(OldType)->
getBitWidth())
5987 if (
auto *
Arg = dyn_cast<Argument>(Cond))
5988 if (
Arg->hasSExtAttr())
5989 ExtType = Instruction::SExt;
5992 ExtInst->insertBefore(SI);
5995 for (
auto Case : SI->
cases()) {
5996 APInt NarrowConst = Case.getCaseValue()->getValue();
5997 APInt WideConst = (ExtType == Instruction::ZExt) ?
5998 NarrowConst.
zext(RegWidth) : NarrowConst.
sext(RegWidth);
6023 class VectorPromoteHelper {
6040 unsigned StoreExtractCombineCost;
6049 if (InstsToBePromoted.
empty())
6051 return InstsToBePromoted.
back();
6057 unsigned getTransitionOriginalValueIdx()
const {
6058 assert(isa<ExtractElementInst>(Transition) &&
6059 "Other kind of transitions are not supported yet");
6066 unsigned getTransitionIdx()
const {
6067 assert(isa<ExtractElementInst>(Transition) &&
6068 "Other kind of transitions are not supported yet");
6076 Type *getTransitionType()
const {
6091 bool isProfitableToPromote() {
6092 Value *ValIdx = Transition->
getOperand(getTransitionOriginalValueIdx());
6093 unsigned Index = isa<ConstantInt>(ValIdx)
6094 ? cast<ConstantInt>(ValIdx)->getZExtValue()
6096 Type *PromotedType = getTransitionType();
6113 uint64_t ScalarCost =
6115 uint64_t VectorCost = StoreExtractCombineCost;
6116 for (
const auto &Inst : InstsToBePromoted) {
6122 bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) ||
6123 isa<ConstantFP>(Arg0);
6136 dbgs() <<
"Estimated cost of computation to be promoted:\nScalar: " 6137 << ScalarCost <<
"\nVector: " << VectorCost <<
'\n');
6138 return ScalarCost > VectorCost;
6155 if (
ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx))
6156 ExtractIdx = CstVal->getSExtValue();
6161 unsigned End = getTransitionType()->getVectorNumElements();
6167 for (
unsigned Idx = 0; Idx != End; ++Idx) {
6168 if (Idx == ExtractIdx)
6179 unsigned OperandIdx) {
6182 if (OperandIdx != 1)
6187 case Instruction::SDiv:
6188 case Instruction::UDiv:
6189 case Instruction::SRem:
6190 case Instruction::URem:
6192 case Instruction::FDiv:
6193 case Instruction::FRem:
6202 unsigned CombineCost)
6203 : DL(DL), TLI(TLI), TTI(TTI), Transition(Transition),
6204 StoreExtractCombineCost(CombineCost) {
6205 assert(Transition &&
"Do not know how to promote null");
6209 bool canPromote(
const Instruction *ToBePromoted)
const {
6211 return isa<BinaryOperator>(ToBePromoted);
6219 for (
const Use &U : ToBePromoted->
operands()) {
6220 const Value *Val = U.get();
6221 if (Val == getEndOfTransition()) {
6225 if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()))
6229 if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) &&
6230 !isa<ConstantFP>(Val))
6239 ISDOpcode, TLI.
getValueType(DL, getTransitionType(),
true));
6248 void enqueueForPromotion(
Instruction *ToBePromoted) {
6249 InstsToBePromoted.
push_back(ToBePromoted);
6253 void recordCombineInstruction(
Instruction *ToBeCombined) {
6255 CombineInst = ToBeCombined;
6265 if (InstsToBePromoted.
empty() || !CombineInst)
6273 for (
auto &ToBePromoted : InstsToBePromoted)
6274 promoteImpl(ToBePromoted);
6275 InstsToBePromoted.clear();
6282 void VectorPromoteHelper::promoteImpl(
Instruction *ToBePromoted) {
6292 "The type of the result of the transition does not match " 6297 Type *TransitionTy = getTransitionType();
6303 Value *Val = U.get();
6304 Value *NewVal =
nullptr;
6305 if (Val == Transition)
6306 NewVal = Transition->getOperand(getTransitionOriginalValueIdx());
6307 else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) ||
6308 isa<ConstantFP>(Val)) {
6311 cast<Constant>(Val),
6312 isa<UndefValue>(Val) ||
6313 canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()));
6317 ToBePromoted->
setOperand(U.getOperandNo(), NewVal);
6319 Transition->moveAfter(ToBePromoted);
6320 Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted);
6326 bool CodeGenPrepare::optimizeExtractElementInst(
Instruction *Inst) {
6342 LLVM_DEBUG(
dbgs() <<
"Found an interesting transition: " << *Inst <<
'\n');
6343 VectorPromoteHelper VPH(*DL, *TLI, *TTI, Inst, CombineCost);
6350 if (ToBePromoted->
getParent() != Parent) {
6351 LLVM_DEBUG(
dbgs() <<
"Instruction to promote is in a different block (" 6353 <<
") than the transition (" << Parent->
getName()
6358 if (VPH.canCombine(ToBePromoted)) {
6360 <<
"will be combined with: " << *ToBePromoted <<
'\n');
6361 VPH.recordCombineInstruction(ToBePromoted);
6362 bool Changed = VPH.promote();
6363 NumStoreExtractExposed += Changed;
6368 if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted))
6371 LLVM_DEBUG(
dbgs() <<
"Promoting is possible... Enqueue for promotion!\n");
6373 VPH.enqueueForPromotion(ToBePromoted);
6374 Inst = ToBePromoted;
6433 Value *LValue, *HValue;
6464 if (LBC && LBC->getParent() != SI.
getParent())
6465 LValue = Builder.CreateBitCast(LBC->getOperand(0), LBC->getType());
6466 if (HBC && HBC->getParent() != SI.
getParent())
6467 HValue = Builder.CreateBitCast(HBC->getOperand(0), HBC->getType());
6470 auto CreateSplitStore = [&](
Value *V,
bool Upper) {
6471 V = Builder.CreateZExtOrBitCast(V, SplitStoreType);
6472 Value *Addr = Builder.CreateBitCast(
6476 Addr = Builder.CreateGEP(
6477 SplitStoreType, Addr,
6479 Builder.CreateAlignedStore(
6483 CreateSplitStore(LValue,
false);
6484 CreateSplitStore(HValue,
true);
6574 if (!isa<Instruction>(GEPIOp))
6576 auto *GEPIOpI = cast<Instruction>(GEPIOp);
6577 if (GEPIOpI->getParent() != SrcBlock)
6582 if (
auto *I = dyn_cast<Instruction>(Usr)) {
6588 }) == GEPI->
users().end())
6591 std::vector<GetElementPtrInst *> UGEPIs;
6595 if (Usr == GEPI)
continue;
6597 if (!isa<Instruction>(Usr))
6599 auto *UI = cast<Instruction>(Usr);
6601 if (UI->getParent() == SrcBlock)
6604 if (!isa<GetElementPtrInst>(Usr))
6606 auto *UGEPI = cast<GetElementPtrInst>(Usr);
6612 if (UGEPI->getOperand(0) != GEPIOp)
6615 cast<ConstantInt>(UGEPI->getOperand(1))->
getType())
6617 ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
6621 UGEPIs.push_back(UGEPI);
6623 if (UGEPIs.size() == 0)
6627 ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
6635 UGEPI->setOperand(0, GEPI);
6636 ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
6644 UGEPI->setIsInBounds(
false);
6650 return cast<Instruction>(Usr)->
getParent() != SrcBlock;
6651 }) == GEPIOp->
users().end() &&
"GEPIOp is used outside SrcBlock");
6655 bool CodeGenPrepare::optimizeInst(
Instruction *I,
bool &ModifiedDT) {
6658 if (InsertedInsts.count(I))
6661 if (
PHINode *
P = dyn_cast<PHINode>(I)) {
6666 P->replaceAllUsesWith(V);
6667 P->eraseFromParent();
6674 if (
CastInst *CI = dyn_cast<CastInst>(I)) {
6687 if (isa<ZExtInst>(I) || isa<SExtInst>(
I)) {
6696 bool MadeChange = optimizeExt(I);
6697 return MadeChange | optimizeExtUses(I);
6703 if (
CmpInst *CI = dyn_cast<CmpInst>(I))
6707 if (
LoadInst *LI = dyn_cast<LoadInst>(I)) {
6710 bool Modified = optimizeLoadExt(LI);
6718 if (
StoreInst *SI = dyn_cast<StoreInst>(I)) {
6723 unsigned AS = SI->getPointerAddressSpace();
6724 return optimizeMemoryInst(I, SI->
getOperand(1),
6731 unsigned AS = RMW->getPointerAddressSpace();
6732 return optimizeMemoryInst(I, RMW->getPointerOperand(),
6737 unsigned AS = CmpX->getPointerAddressSpace();
6738 return optimizeMemoryInst(I, CmpX->getPointerOperand(),
6739 CmpX->getCompareOperand()->
getType(), AS);
6744 if (BinOp && (BinOp->getOpcode() == Instruction::And) &&
6748 if (BinOp && (BinOp->getOpcode() == Instruction::AShr ||
6749 BinOp->getOpcode() == Instruction::LShr)) {
6758 if (GEPI->hasAllZeroIndices()) {
6761 GEPI->getName(), GEPI);
6763 GEPI->replaceAllUsesWith(NC);
6764 GEPI->eraseFromParent();
6766 optimizeInst(NC, ModifiedDT);
6775 if (
CallInst *CI = dyn_cast<CallInst>(I))
6776 return optimizeCallInst(CI, ModifiedDT);
6778 if (
SelectInst *SI = dyn_cast<SelectInst>(I))
6779 return optimizeSelectInst(SI);
6782 return optimizeShuffleVectorInst(SVI);
6784 if (
auto *Switch = dyn_cast<SwitchInst>(I))
6785 return optimizeSwitchInst(Switch);
6787 if (isa<ExtractElementInst>(I))
6788 return optimizeExtractElementInst(I);
6814 bool CodeGenPrepare::optimizeBlock(
BasicBlock &BB,
bool &ModifiedDT) {
6816 bool MadeChange =
false;
6818 CurInstIterator = BB.
begin();
6819 while (CurInstIterator != BB.
end()) {
6820 MadeChange |= optimizeInst(&*CurInstIterator++, ModifiedDT);
6825 bool MadeBitReverse =
true;
6826 while (TLI && MadeBitReverse) {
6827 MadeBitReverse =
false;
6830 MadeBitReverse = MadeChange =
true;
6836 MadeChange |= dupRetToEnableTailCallOpts(&BB);
6844 bool CodeGenPrepare::placeDbgValues(
Function &F) {
6845 bool MadeChange =
false;
6857 PrevNonDbgInst = Insn;
6862 if (VI && VI != PrevNonDbgInst && !VI->
isTerminator()) {
6868 << *DVI <<
' ' << *VI);
6870 if (isa<PHINode>(VI))
6884 uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
6886 NewTrue = NewTrue / Scale;
6887 NewFalse = NewFalse / Scale;
6912 bool CodeGenPrepare::splitBranchCondition(
Function &F) {
6916 bool MadeChange =
false;
6917 for (
auto &BB : F) {
6933 Value *Cond1, *Cond2;
6936 Opc = Instruction::And;
6939 Opc = Instruction::Or;
6956 Br1->setCondition(Cond1);
6961 if (Opc == Instruction::And)
6962 Br1->setSuccessor(0, TmpBB);
6964 Br1->setSuccessor(1, TmpBB);
6967 auto *Br2 =
IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB);
6968 if (
auto *I = dyn_cast<Instruction>(Cond2)) {
6982 if (Opc == Instruction::Or)
6988 while ((i = PN.getBasicBlockIndex(&BB)) >= 0)
6989 PN.setIncomingBlock(i, TmpBB);
6994 auto *Val = PN.getIncomingValueForBlock(&BB);
6995 PN.addIncoming(Val, TmpBB);
7000 if (Opc == Instruction::Or) {
7020 uint64_t TrueWeight, FalseWeight;
7021 if (Br1->extractProfMetadata(TrueWeight, FalseWeight)) {
7022 uint64_t NewTrueWeight = TrueWeight;
7023 uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight;
7026 .createBranchWeights(TrueWeight, FalseWeight));
7028 NewTrueWeight = TrueWeight;
7029 NewFalseWeight = 2 * FalseWeight;
7032 .createBranchWeights(TrueWeight, FalseWeight));
7053 uint64_t TrueWeight, FalseWeight;
7054 if (Br1->extractProfMetadata(TrueWeight, FalseWeight)) {
7055 uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight;
7056 uint64_t NewFalseWeight = FalseWeight;
7059 .createBranchWeights(TrueWeight, FalseWeight));
7061 NewTrueWeight = 2 * TrueWeight;
7062 NewFalseWeight = FalseWeight;
7065 .createBranchWeights(TrueWeight, FalseWeight));
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
const Function & getFunction() const
static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, AliasSetTracker *AST, MemorySSAUpdater *MSSAU)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
static bool GEPSequentialConstIndexed(GetElementPtrInst *GEP)
Return a value (possibly void), from a function.
Value * getValueOperand()
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
static MVT getIntegerVT(unsigned BitWidth)
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
A parsed version of the target data layout string in and methods for querying it. ...
const_iterator end(StringRef path)
Get end iterator over path.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
uint64_t getTypeStoreSizeInBits(Type *Ty) const
Returns the maximum number of bits that may be overwritten by storing the specified type; always a mu...
Value * getPointerOperand()
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
This class is the base class for the comparison instructions.
unsigned getAddrMode(MCInstrInfo const &MCII, MCInst const &MCI)
static Constant * getConstantVector(MVT VT, const APInt &SplatValue, unsigned SplatBitSize, LLVMContext &C)
iterator_range< use_iterator > uses()
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
virtual bool isCheapAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast from SrcAS to DestAS is "cheap", such that e.g.
static bool SinkCmpExpression(CmpInst *CI, const TargetLowering *TLI)
Sink the given CmpInst into user blocks to reduce the number of virtual registers that must be create...
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
APInt sext(unsigned width) const
Sign extend to a new width.
static bool isPromotedInstructionLegal(const TargetLowering &TLI, const DataLayout &DL, Value *Val)
Check whether or not Val is a legal instruction for TLI.
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
static cl::opt< bool > ForceSplitStore("force-split-store", cl::Hidden, cl::init(false), cl::desc("Force store splitting no matter what the target query says."))
static Value * getTrueOrFalseValue(SelectInst *SI, bool isTrue, const SmallPtrSet< const Instruction *, 2 > &Selects)
If isTrue is true, return the true value of SI, otherwise return false value of SI.
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
LLVM_NODISCARD T pop_back_val()
This class represents lattice values for constants.
BinaryOps getOpcode() const
static bool MightBeFoldableInst(Instruction *I)
This is a little filter, which returns true if an addressing computation involving I might be folded ...
static bool sinkSelectOperand(const TargetTransformInfo *TTI, Value *V)
Check if V (an operand of a select instruction) is an expensive instruction that is only used once...
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
Value * optimizeCall(CallInst *CI)
Take the given call instruction and return a more optimal value to replace the instruction with or 0 ...
A Module instance is used to store all the information related to an LLVM module. ...
bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, MemoryDependenceResults *MemDep=nullptr)
Attempts to merge a block into its predecessor, if possible.
virtual bool isMaskAndCmp0FoldingBeneficial(const Instruction &AndI) const
Return if the target supports combining a chain like:
void setAlignment(unsigned Align)
an instruction that atomically checks whether a specified value is in a memory location, and, if it is, stores a new value there.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This class represents zero extension of integer types.
void push_back(const T &Elt)
static cl::opt< bool > EnableGEPOffsetSplit("cgp-split-large-offset-gep", cl::Hidden, cl::init(true), cl::desc("Enable splitting large offset of GEP."))
Analysis providing profile information.
APInt zext(unsigned width) const
Zero extend to a new width.
This class represents a function call, abstracting a target machine's calling convention.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Value * getCondition() const
const Value * getTrueValue() const
static cl::opt< bool > EnableTypePromotionMerge("cgp-type-promotion-merge", cl::Hidden, cl::desc("Enable merging of redundant sexts when one is dominating" " the other."), cl::init(true))
virtual bool isSelectSupported(SelectSupportKind) const
bool salvageDebugInfo(Instruction &I)
Assuming the instruction I is going to be deleted, attempt to salvage debug users of I by writing the...
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
static cl::opt< bool > AddrSinkCombineBaseOffs("addr-sink-combine-base-offs", cl::Hidden, cl::init(true), cl::desc("Allow combining of BaseOffs field in Address sinking."))
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
Delete the specified block, which must have no predecessors.
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
LLVMContext & getContext() const
All values hold a context through their type.
bool isTerminator() const
void setSectionPrefix(StringRef Prefix)
Set the section prefix for this function.
This class implements a map that also provides access to all stored values in a deterministic order...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
static void dump(StringRef Title, SpillInfo const &Spills)
BasicBlock * getSuccessor(unsigned i) const
Value * CreateSExt(Value *V, Type *DestTy, const Twine &Name="")
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
void setArgOperand(unsigned i, Value *v)
virtual AsmOperandInfoVector ParseConstraints(const DataLayout &DL, const TargetRegisterInfo *TRI, ImmutableCallSite CS) const
Split up the constraint string from the inline assembly value into the specific constraints and their...
bool isInteger() const
Return true if this is an integer or a vector integer type.
static cl::opt< bool > StressExtLdPromotion("stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) " "optimization in CodeGenPrepare"))
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool hasMultipleConditionRegisters() const
Return true if multiple condition registers are available.
An instruction for reading from memory.
bool hasExtractBitsInsn() const
Return true if the target has BitExtract instructions.
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isVectorTy() const
True if this is an instance of VectorType.
virtual bool isZExtFree(Type *FromTy, Type *ToTy) const
Return true if any actual instruction that defines a value of type FromTy implicitly zero-extends the...
This defines the Use class.
void reserve(size_type N)
Value * CallOperandVal
If this is the result output operand or a clobber, this is null, otherwise it is the incoming operand...
void setAlignment(unsigned Align)
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 hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
LLVMContext & getContext() const
Get the context in which this basic block lives.
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
static bool despeculateCountZeros(IntrinsicInst *CountZeros, const TargetLowering *TLI, const DataLayout *DL, bool &ModifiedDT)
If counting leading or trailing zeros is an expensive operation and a zero input is defined...
unsigned getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static cl::opt< unsigned > FreqRatioToSkipMerge("cgp-freq-ratio-to-skip-merge", cl::Hidden, cl::init(2), cl::desc("Skip merging empty blocks if (frequency of empty block) / " "(frequency of destination block) is greater than this ratio"))
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
iterator begin()
Instruction iterator methods.
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
Split the edge connecting specified block.
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
Value * getArgOperand(unsigned i) const
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void dump() const
Support for debugging, callable in GDB: V->dump()
bool match(Val *V, const Pattern &P)
AnalysisUsage & addRequired()
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isFunctionHotInCallGraph(const Function *F, BlockFrequencyInfo &BFI)
Returns true if F contains hot code.
This class represents the LLVM 'select' instruction.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
unsigned getAlignment() const
Return the alignment of the memory that is being allocated by the instruction.
An analysis pass based on legacy pass manager to deliver ProfileSummaryInfo.
This is the base class for all instructions that perform data casts.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
'undef' values are things that do not have specified contents.
bool bypassSlowDivision(BasicBlock *BB, const DenseMap< unsigned int, unsigned int > &BypassWidth)
This optimization identifies DIV instructions in a BB that can be profitably bypassed and carried out...
static cl::opt< bool > AddrSinkUsingGEPs("addr-sink-using-gep", cl::Hidden, cl::init(true), cl::desc("Address sinking in CGP using GEPs."))
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
PointerType * getPointerTo(unsigned AddrSpace=0) const
Return a pointer to the current type.
TargetLowering::ConstraintType ConstraintType
Information about the constraint code, e.g.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
bool isIntegerTy() const
True if this is an instance of IntegerType.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static cl::opt< bool > DisableBranchOpts("disable-cgp-branch-opts", cl::Hidden, cl::init(false), cl::desc("Disable branch optimizations in CodeGenPrepare"))
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
This file contains the simple types necessary to represent the attributes associated with functions a...
virtual bool isVectorShiftByScalarCheap(Type *Ty) const
Return true if it's significantly cheaper to shift a vector by a uniform scalar than by an amount whi...
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
virtual bool isMultiStoresCheaperThanBitsMerge(EVT LTy, EVT HTy) const
Return true if it is cheaper to split the store of a merged int val from a pair of smaller values int...
Position
Position to insert a new instruction relative to an existing instruction.
This class defines information used to lower LLVM code to legal SelectionDAG operators that the targe...
APInt operator*(APInt a, uint64_t RHS)
This file implements a class to represent arbitrary precision integral constant values and operations...
static cl::opt< bool > DisableStoreExtract("disable-cgp-store-extract", cl::Hidden, cl::init(false), cl::desc("Disable store(extract) optimizations in CodeGenPrepare"))
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
virtual bool isTruncateFree(Type *FromTy, Type *ToTy) const
Return true if it's free to truncate a value of type FromTy to type ToTy.
static cl::opt< bool > DisablePreheaderProtect("disable-preheader-prot", cl::Hidden, cl::init(false), cl::desc("Disable protection against removing loop preheaders"))
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
unsigned getSizeInBits() const
ReturnInst * FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, BasicBlock *Pred, DomTreeUpdater *DTU=nullptr)
This method duplicates the specified return instruction into a predecessor which ends in an unconditi...
static bool shouldPromote(Value *V)
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
unsigned getActiveBits() const
Compute the number of active bits in the value.
static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI, const DataLayout &DL)
If the specified cast instruction is a noop copy (e.g.
OtherOps getOpcode() const
Get the opcode casted to the right type.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
A constant value that is initialized with an expression using other constant values.
void findDbgValues(SmallVectorImpl< DbgValueInst *> &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
unsigned getSizeInBits() const
Return the size of the specified value type in bits.
int64_t getSExtValue() const
Get sign extended value.
Instruction::CastOps getOpcode() const
Return the opcode of this CastInst.
Type * getType() const
All values are typed, get the type of this value.
Value handle that is nullable, but tries to track the Value.
bool insert(const value_type &X)
Insert a new element into the SetVector.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
static cl::opt< bool > AddrSinkCombineBaseReg("addr-sink-combine-base-reg", cl::Hidden, cl::init(true), cl::desc("Allow combining of BaseReg field in Address sinking."))
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
Matches ZExt.
static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP, SmallVectorImpl< Value *> &OffsetV)
const BasicBlock * getUniquePredecessor() const
Return the predecessor of this block if it has a unique predecessor block.
This contains information for each constraint that we are lowering.
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
virtual bool isCheapToSpeculateCtlz() const
Return true if it is cheap to speculate a call to intrinsic ctlz.
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
This class represents a no-op cast from one type to another.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static CmpInst * Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
const APInt & getValue() const
Return the constant as an APInt value reference.
bool isLittleEndian() const
Layout endianness...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
static bool hasSameExtUse(Value *Val, const TargetLowering &TLI)
Check if all the uses of Val are equivalent (or free) zero or sign extensions.
iterator_range< User::op_iterator > arg_operands()
An instruction for storing to memory.
static bool canCombine(MachineBasicBlock &MBB, MachineOperand &MO, unsigned CombineOpc, unsigned ZeroReg=0, bool CheckZeroReg=false)
void SetCurrentDebugLocation(DebugLoc L)
Set location information used by debugging information.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
virtual bool isCheapToSpeculateCttz() const
Return true if it is cheap to speculate a call to intrinsic cttz.
virtual bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, unsigned AddrSpace, Instruction *I=nullptr) const
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
Value * CreateZExt(Value *V, Type *DestTy, const Twine &Name="")
ConstraintPrefix Type
Type - The basic type of the constraint: input/output/clobber.
bool pointsToAliveValue() const
Optimize for code generation
void takeName(Value *V)
Transfer the name from V to this value.
This class implements simplifications for calls to fortified library functions (__st*cpy_chk, __memcpy_chk, __memmove_chk, __memset_chk), to, when possible, replace them with their non-checking counterparts.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
This class represents a truncation of integer types.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
static cl::opt< bool > AddrSinkNewPhis("addr-sink-new-phis", cl::Hidden, cl::init(false), cl::desc("Allow creation of Phis in Address sinking."))
static cl::opt< bool > AddrSinkNewSelects("addr-sink-new-select", cl::Hidden, cl::init(true), cl::desc("Allow creation of selects in Address sinking."))
Value * getOperand(unsigned i) const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Class to represent pointers.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Interval::succ_iterator succ_end(Interval *I)
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
bool isJumpExpensive() const
Return true if Flow Control is an expensive operation that should be avoided.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
static cl::opt< bool > StressStoreExtract("stress-cgp-store-extract", cl::Hidden, cl::init(false), cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"))
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
iterator find(const_arg_type_t< KeyT > Val)
static bool isExtractBitsCandidateUse(Instruction *User)
Check if the candidates could be combined with a shift instruction, which includes: ...
bool bitsGT(EVT VT) const
Return true if this has more bits than VT.
static bool OptimizeCmpExpression(CmpInst *CI, const TargetLowering *TLI)
static bool simplifyRelocatesOffABase(GCRelocateInst *RelocatedBase, const SmallVectorImpl< GCRelocateInst *> &Targets)
virtual bool canCombineStoreAndExtract(Type *VectorTy, Value *Idx, unsigned &Cost) const
Return true if the target can combine store(extractelement VectorTy, Idx).
const Instruction * getStatepoint() const
The statepoint with which this gc.relocate is associated.
const BasicBlock & getEntryBlock() const
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
OneUse_match< T > m_OneUse(const T &SubPattern)
static bool runOnFunction(Function &F, bool PostInlining)
static unsigned getPointerOperandIndex()
initializer< Ty > init(const Ty &Val)
std::vector< AsmOperandInfo > AsmOperandInfoVector
static cl::opt< bool > DisableExtLdPromotion("disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false), cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in " "CodeGenPrepare"))
bool isAllOnesValue() const
Determine if all bits are set.
bool canIncreaseAlignment() const
static bool isBroadcastShuffle(ShuffleVectorInst *SVI)
bool operator!=(const UnitT &, const UnitT &)
static bool CombineUAddWithOverflow(CmpInst *CI)
Try to combine CI into a call to the llvm.uadd.with.overflow intrinsic if possible.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
static bool FindAllMemoryUses(Instruction *I, SmallVectorImpl< std::pair< Instruction *, unsigned >> &MemoryUses, SmallPtrSetImpl< Instruction *> &ConsideredInsts, const TargetLowering &TLI, const TargetRegisterInfo &TRI, int SeenInsts=0)
Recursively walk all the uses of I until we find a memory use.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
Value * getCalledValue() const
ConstantInt * lowerObjectSizeCall(IntrinsicInst *ObjectSize, const DataLayout &DL, const TargetLibraryInfo *TLI, bool MustSucceed)
Try to turn a call to @llvm.objectsize into an integer value of the given Type.
LLVM Basic Block Representation.
PointerIntPair - This class implements a pair of a pointer and small integer.
The instances of the Type class are immutable: once they are created, they are never changed...
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
static unsigned getPointerOperandIndex()
This is an important class for using LLVM in a threaded context.
Conditional or Unconditional Branch instruction.
bool isIndirect
isIndirect - True if this operand is an indirect operand.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
LLVM_NODISCARD bool empty() const
bool isRound() const
Return true if the size is a power-of-two number of bytes.
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...
bool isPointerTy() const
True if this is an instance of PointerType.
const Instruction & front() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool isMask(unsigned numBits) const
static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI, const TargetLowering *TLI, SelectInst *SI)
Returns true if a SelectInst should be turned into an explicit branch.
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...
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
brc_match< Cond_t > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Represent the analysis usage information of a pass.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
unsigned getLargestLegalIntTypeSizeInBits() const
Returns the size of largest legal integer type size, or 0 if none are set.
static bool SinkCast(CastInst *CI)
SinkCast - Sink the specified cast instruction into its user blocks.
static cl::opt< bool > AddrSinkCombineScaledReg("addr-sink-combine-scaled-reg", cl::Hidden, cl::init(true), cl::desc("Allow combining of ScaledReg field in Address sinking."))
FunctionPass class - This class is used to implement most global optimizations.
Interval::pred_iterator pred_end(Interval *I)
unsigned getAddressSpace() const
Return the address space of the Pointer type.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
self_iterator getIterator()
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
const Function * getFunction() const
Return the function this instruction belongs to.
const Value * getCondition() const
static cl::opt< bool > DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false), cl::desc("Disable GC optimizations in CodeGenPrepare"))
unsigned getBasePtrIndex() const
The index into the associate statepoint's argument list which contains the base pointer of the pointe...
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Type * getIndexedType() const
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
iterator erase(const_iterator CI)
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
static wasm::ValType getType(const TargetRegisterClass *RC)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
PointerType * getInt8PtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer to an 8-bit integer value.
bool isExtFree(const Instruction *I) const
Return true if the extension represented by I is free.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
EVT getValueType(const DataLayout &DL, Type *Ty, bool AllowUnknown=false) const
Return the EVT corresponding to this LLVM type.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
void sort(IteratorTy Start, IteratorTy End)
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Value * CreateGEP(Value *Ptr, ArrayRef< Value *> IdxList, const Twine &Name="")
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static Constant * getSplat(unsigned NumElts, Constant *Elt)
Return a ConstantVector with the specified constant in each element.
INITIALIZE_PASS_BEGIN(CodeGenPrepare, DEBUG_TYPE, "Optimize for code generation", false, false) INITIALIZE_PASS_END(CodeGenPrepare
void initializeCodeGenPreparePass(PassRegistry &)
A SetVector that performs no allocations if smaller than a certain size.
This is the common base class for memset/memcpy/memmove.
Iterator for intrusive lists based on ilist_node.
StructType * getStructTypeOrNull() const
unsigned getNumOperands() const
static bool IsNonLocalValue(Value *V, BasicBlock *BB)
Return true if the specified values are defined in a different basic block than BB.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
unsigned countPopulation(T Value)
Count the number of set bits in a value.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
This is the shared class of boolean and integer constants.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
BlockVerifier::State From
bool erase(PtrType Ptr)
erase - If the set contains the specified pointer, remove it and return true, otherwise return false...
virtual bool allowsMisalignedMemoryAccesses(EVT, unsigned AddrSpace=0, unsigned Align=1, bool *=nullptr) const
Determine if the target supports unaligned memory accesses.
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Module.h This file contains the declarations for the Module class.
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
Provides information about what library functions are available for the current target.
virtual bool shouldConsiderGEPOffsetSplit() const
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static cl::opt< bool > DisableSelectToBranch("disable-cgp-select2branch", cl::Hidden, cl::init(false), cl::desc("Disable select to branch conversion."))
bool isFunctionColdInCallGraph(const Function *F, BlockFrequencyInfo &BFI)
Returns true if F contains only cold code.
LLVM_NODISCARD T pop_back_val()
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static bool tryUnmergingGEPsAcrossIndirectBr(GetElementPtrInst *GEPI, const TargetTransformInfo *TTI)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
bool isConditional() const
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Value handle that asserts if the Value is deleted.
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool enableExtLdPromotion() const
Return true if the target wants to use the optimization that turns ext(promotableInst1(...(promotableInstN(load)))) into promotedInst1(...(promotedInstN(ext(load)))).
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
Accumulate offsets from stripInBoundsConstantOffsets().
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
bool isTypeLegal(EVT VT) const
Return true if the target has native support for the specified value type.
Class for arbitrary precision integers.
static cl::opt< bool > AddrSinkCombineBaseGV("addr-sink-combine-base-gv", cl::Hidden, cl::init(true), cl::desc("Allow combining of BaseGV field in Address sinking."))
virtual void ComputeConstraintToUse(AsmOperandInfo &OpInfo, SDValue Op, SelectionDAG *DAG=nullptr) const
Determines the constraint code and constraint type to use for the specific AsmOperandInfo, setting OpInfo.ConstraintCode and OpInfo.ConstraintType.
bool bitsLT(EVT VT) const
Return true if this has less bits than VT.
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
typename SuperClass::iterator iterator
iterator_range< user_iterator > users()
InstListType::iterator iterator
Instruction iterators...
IntegerType * getInt8Ty()
Fetch the type representing an 8-bit integer.
static void clear(coro::Shape &Shape)
const Value * getFalseValue() const
int InstructionOpcodeToISD(unsigned Opcode) const
Get the ISD node that corresponds to the Instruction class opcode.
Value * CreatePointerCast(Value *V, Type *DestTy, const Twine &Name="")
SmallVector< NodeAddr< NodeBase * >, 4 > NodeList
FunctionPass * createCodeGenPreparePass()
createCodeGenPreparePass - Transform the code to expose more pattern matching during instruction sele...
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
amdgpu Simplify well known AMD library false Value Value * Arg
bool isSequential() const
TargetSubtargetInfo - Generic base class for all target subtargets.
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
virtual bool useSoftFloat() const
bool operator!=(uint64_t V1, const APInt &V2)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass's ...
bool ugt(const APInt &RHS) const
Unsigned greather than comparison.
Predicate getPredicate() const
Return the predicate for this instruction.
This class wraps the llvm.memcpy/memmove intrinsics.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
static bool isTrivial(const DICompositeType *DCTy)
Analysis providing branch probability information.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
uint64_t getElementOffset(unsigned Idx) const
void emplace_back(ArgTypes &&... Args)
SelectSupportKind
Enum that describes what type of support for selects the target has.
static IntegerType * getInt32Ty(LLVMContext &C)
void setCondition(Value *V)
unsigned getIntegerBitWidth() const
LLVM_NODISCARD bool empty() const
bool SplitIndirectBrCriticalEdges(Function &F, BranchProbabilityInfo *BPI=nullptr, BlockFrequencyInfo *BFI=nullptr)
This represents the llvm.dbg.value instruction.
Represents a single loop in the control flow graph.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value...
StringRef getName() const
Return a constant reference to the value's name.
Establish a view to a call site for examination.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
LegalizeTypeAction getTypeAction(LLVMContext &Context, EVT VT) const
Return how we should legalize values of this type, either it is already legal (return 'Legal') or we ...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
const Function * getParent() const
Return the enclosing method, or null if none.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
SymbolTableList< BasicBlock >::iterator eraseFromParent()
Unlink 'this' from the containing function and delete it.
bool empty() const
Determine if the SetVector is empty or not.
user_iterator_impl< User > user_iterator
Type * getResultElementType() const
static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI, const TargetLowering &TLI, const DataLayout &DL)
Sink the shift right instruction into user blocks if the uses could potentially be combined with this...
unsigned getKnownAlignment(Value *V, const DataLayout &DL, const Instruction *CxtI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr)
Try to infer an alignment for the specified pointer.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Type * getValueType() const
static bool splitMergedValStore(StoreInst &SI, const DataLayout &DL, const TargetLowering &TLI)
For the instruction sequence of store below, F and I values are bundled together as an i64 value befo...
static unsigned getPointerOperandIndex()
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
bool isUnconditional() const
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
static EVT getEVT(Type *Ty, bool HandleUnknown=false)
Return the value type corresponding to the specified type.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
unsigned getAlignment() const
Return the alignment of the access that is being performed.
LLVM_NODISCARD bool empty() const
void mutateType(Type *Ty)
Mutate the type of this Value to be of the specified type.
Value * CreatePtrToInt(Value *V, Type *DestTy, const Twine &Name="")
bool isStatepoint(ImmutableCallSite CS)
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse)
Scale down both weights to fit into uint32_t.
void moveAfter(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
unsigned getMinSignedBits() const
Get the minimum bit size for this signed APInt.
user_iterator user_begin()
bool hasHugeWorkingSetSize()
Returns true if the working set size of the code is considered huge.
bool isSafeToSpeculativelyExecute(const Value *V, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
Represents calls to the gc.relocate intrinsic.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Module * getParent()
Get the module that this global value is contained inside of...
static cl::opt< bool > DisableComplexAddrModes("disable-complex-addr-modes", cl::Hidden, cl::init(false), cl::desc("Disables combining addressing modes with different parts " "in optimizeMemoryInst."))
LLVM Value Representation.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
static cl::opt< bool > EnableAndCmpSinking("enable-andcmp-sinking", cl::Hidden, cl::init(true), cl::desc("Enable sinkinig and/cmp into branches."))
static constexpr int MaxMemoryUsesToScan
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
static const Function * getParent(const Value *V)
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
This class implements an extremely fast bulk output stream that can only output to a stream...
bool isLoadExtLegal(unsigned ExtType, EVT ValVT, EVT MemVT) const
Return true if the specified load with extension is legal on this target.
Primary interface to the complete machine description for the target machine.
static cl::opt< bool > ProfileGuidedSectionPrefix("profile-guided-section-prefix", cl::Hidden, cl::init(true), cl::ZeroOrMore, cl::desc("Use profile info to add section prefix for hot/cold functions"))
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
The legacy pass manager's analysis pass to compute loop information.
bool hasNoNaNs() const
Determine whether the no-NaNs flag is set.
bool hasOneUse() const
Return true if there is exactly one user of this value.
StringRef - Represent a constant reference to a string, i.e.
const Instruction * getFirstNonPHIOrDbg() const
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
bool recognizeBSwapOrBitReverseIdiom(Instruction *I, bool MatchBSwaps, bool MatchBitReversals, SmallVectorImpl< Instruction *> &InsertedInsts)
Try to match a bswap or bitreverse idiom.
static bool makeBitReverse(Instruction &I, const DataLayout &DL, const TargetLowering &TLI)
Given an OR instruction, check to see if this is a bitreverse idiom.
bool operator==(uint64_t V1, const APInt &V2)
specific_intval m_SpecificInt(uint64_t V)
Match a specific integer value or vector with all elements equal to the value.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
virtual BranchProbability getPredictableBranchThreshold() const
If a branch or a select condition is skewed in one direction by more than this factor, it is very likely to be predicted correctly.
bool replaceAndRecursivelySimplify(Instruction *I, Value *SimpleV, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Replace all uses of 'I' with 'SimpleV' and simplify the uses recursively.
bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const
Retrieve the raw weight values of a conditional branch or select.
op_range incoming_values()
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
bool attributesPermitTailCall(const Function *F, const Instruction *I, const ReturnInst *Ret, const TargetLoweringBase &TLI, bool *AllowDifferingSizes=nullptr)
Test if given that the input instruction is in the tail call position, if there is an attribute misma...
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
VectorType * getType() const
Overload to return most specific vector type.
EVT getTypeToTransformTo(LLVMContext &Context, EVT VT) const
For types supported by the target, this is an identity function.
virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const
Returns true if a cast between SrcAS and DestAS is a noop.
base_list_type::reverse_iterator reverse_iterator
static IntegerType * getInt8Ty(LLVMContext &C)
static Constant * get(ArrayRef< Constant *> V)
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Type * getElementType() const
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
static void computeBaseDerivedRelocateMap(const SmallVectorImpl< GCRelocateInst *> &AllRelocateCalls, DenseMap< GCRelocateInst *, SmallVector< GCRelocateInst *, 2 >> &RelocateInstMap)
UAddWithOverflow_match< LHS_t, RHS_t, Sum_t > m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S)
Match an icmp instruction checking for unsigned overflow on addition.
A wrapper class for inspecting calls to intrinsic functions.
This file describes how to lower LLVM code to machine code.
const BasicBlock * getParent() const
an instruction to allocate memory on the stack
static bool sinkAndCmp0Expression(Instruction *AndI, const TargetLowering &TLI, SetOfInstrs &InsertedInsts)
Duplicate and sink the given 'and' instruction into user blocks where it is used in a compare to allo...
gep_type_iterator gep_type_begin(const User *GEP)
static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal, const TargetLowering &TLI, const TargetRegisterInfo &TRI)
Check to see if all uses of OpVal by the specified inline asm call are due to memory operands...
bool isPredictableSelectExpensive() const
Return true if selects are only cheaper than branches if the branch is unlikely to be predicted right...
bool isExtLoad(const LoadInst *Load, const Instruction *Ext, const DataLayout &DL) const
Return true if Load and Ext can form an ExtLoad.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
static bool SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI, DenseMap< BasicBlock *, BinaryOperator *> &InsertedShifts, const TargetLowering &TLI, const DataLayout &DL)
Sink both shift and truncate instruction to the use of truncate's BB.