10b57cec5SDimitry Andric //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass munges the code in the input function to better prepare it for
100b57cec5SDimitry Andric // SelectionDAG-based code generation. This works around limitations in it's
110b57cec5SDimitry Andric // basic-block-at-a-time approach. It should eventually be removed.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
15cdc20ff6SDimitry Andric #include "llvm/CodeGen/CodeGenPrepare.h"
160b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
170b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
180b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
190b57cec5SDimitry Andric #include "llvm/ADT/MapVector.h"
200b57cec5SDimitry Andric #include "llvm/ADT/PointerIntPair.h"
210b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
220b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
240b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
250b57cec5SDimitry Andric #include "llvm/Analysis/BlockFrequencyInfo.h"
260b57cec5SDimitry Andric #include "llvm/Analysis/BranchProbabilityInfo.h"
270b57cec5SDimitry Andric #include "llvm/Analysis/InstructionSimplify.h"
280b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
290b57cec5SDimitry Andric #include "llvm/Analysis/ProfileSummaryInfo.h"
300b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
310b57cec5SDimitry Andric #include "llvm/Analysis/TargetTransformInfo.h"
320b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
330b57cec5SDimitry Andric #include "llvm/Analysis/VectorUtils.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h"
3581ad6265SDimitry Andric #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h"
360b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h"
37fe013be4SDimitry Andric #include "llvm/CodeGen/MachineValueType.h"
380b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h"
390b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
400b57cec5SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
410b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
420b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
430b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
440b57cec5SDimitry Andric #include "llvm/IR/Argument.h"
450b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
460b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
470b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
480b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
490b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
50fe6060f1SDimitry Andric #include "llvm/IR/DebugInfo.h"
510b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
520b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
530b57cec5SDimitry Andric #include "llvm/IR/Function.h"
540b57cec5SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h"
550b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
560b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
570b57cec5SDimitry Andric #include "llvm/IR/IRBuilder.h"
580b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
590b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
600b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
610b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
620b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
630b57cec5SDimitry Andric #include "llvm/IR/Intrinsics.h"
64480093f4SDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h"
650b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
660b57cec5SDimitry Andric #include "llvm/IR/MDBuilder.h"
670b57cec5SDimitry Andric #include "llvm/IR/Module.h"
680b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
690b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
70bdd1243dSDimitry Andric #include "llvm/IR/ProfDataUtils.h"
710b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h"
720b57cec5SDimitry Andric #include "llvm/IR/Type.h"
730b57cec5SDimitry Andric #include "llvm/IR/Use.h"
740b57cec5SDimitry Andric #include "llvm/IR/User.h"
750b57cec5SDimitry Andric #include "llvm/IR/Value.h"
760b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h"
770b57cec5SDimitry Andric #include "llvm/IR/ValueMap.h"
78480093f4SDimitry Andric #include "llvm/InitializePasses.h"
790b57cec5SDimitry Andric #include "llvm/Pass.h"
800b57cec5SDimitry Andric #include "llvm/Support/BlockFrequency.h"
810b57cec5SDimitry Andric #include "llvm/Support/BranchProbability.h"
820b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
830b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
840b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
850b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
860b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
870b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
880b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
890b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
900b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
910b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BasicBlockUtils.h"
920b57cec5SDimitry Andric #include "llvm/Transforms/Utils/BypassSlowDivision.h"
93480093f4SDimitry Andric #include "llvm/Transforms/Utils/Local.h"
940b57cec5SDimitry Andric #include "llvm/Transforms/Utils/SimplifyLibCalls.h"
95480093f4SDimitry Andric #include "llvm/Transforms/Utils/SizeOpts.h"
960b57cec5SDimitry Andric #include <algorithm>
970b57cec5SDimitry Andric #include <cassert>
980b57cec5SDimitry Andric #include <cstdint>
990b57cec5SDimitry Andric #include <iterator>
1000b57cec5SDimitry Andric #include <limits>
1010b57cec5SDimitry Andric #include <memory>
102bdd1243dSDimitry Andric #include <optional>
1030b57cec5SDimitry Andric #include <utility>
1040b57cec5SDimitry Andric #include <vector>
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric using namespace llvm;
1070b57cec5SDimitry Andric using namespace llvm::PatternMatch;
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric #define DEBUG_TYPE "codegenprepare"
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric STATISTIC(NumBlocksElim, "Number of blocks eliminated");
1120b57cec5SDimitry Andric STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
1130b57cec5SDimitry Andric STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
1140b57cec5SDimitry Andric STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
1150b57cec5SDimitry Andric                       "sunken Cmps");
1160b57cec5SDimitry Andric STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
1170b57cec5SDimitry Andric                        "of sunken Casts");
1180b57cec5SDimitry Andric STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
1190b57cec5SDimitry Andric                           "computations were sunk");
1200b57cec5SDimitry Andric STATISTIC(NumMemoryInstsPhiCreated,
1210b57cec5SDimitry Andric           "Number of phis created when address "
1220b57cec5SDimitry Andric           "computations were sunk to memory instructions");
1230b57cec5SDimitry Andric STATISTIC(NumMemoryInstsSelectCreated,
1240b57cec5SDimitry Andric           "Number of select created when address "
1250b57cec5SDimitry Andric           "computations were sunk to memory instructions");
1260b57cec5SDimitry Andric STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
1270b57cec5SDimitry Andric STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
1280b57cec5SDimitry Andric STATISTIC(NumAndsAdded,
1290b57cec5SDimitry Andric           "Number of and mask instructions added to form ext loads");
1300b57cec5SDimitry Andric STATISTIC(NumAndUses, "Number of uses of and mask instructions optimized");
1310b57cec5SDimitry Andric STATISTIC(NumRetsDup, "Number of return instructions duplicated");
1320b57cec5SDimitry Andric STATISTIC(NumDbgValueMoved, "Number of debug value instructions moved");
1330b57cec5SDimitry Andric STATISTIC(NumSelectsExpanded, "Number of selects turned into branches");
1340b57cec5SDimitry Andric STATISTIC(NumStoreExtractExposed, "Number of store(extractelement) exposed");
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric static cl::opt<bool> DisableBranchOpts(
1370b57cec5SDimitry Andric     "disable-cgp-branch-opts", cl::Hidden, cl::init(false),
1380b57cec5SDimitry Andric     cl::desc("Disable branch optimizations in CodeGenPrepare"));
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric static cl::opt<bool>
1410b57cec5SDimitry Andric     DisableGCOpts("disable-cgp-gc-opts", cl::Hidden, cl::init(false),
1420b57cec5SDimitry Andric                   cl::desc("Disable GC optimizations in CodeGenPrepare"));
1430b57cec5SDimitry Andric 
144bdd1243dSDimitry Andric static cl::opt<bool>
145bdd1243dSDimitry Andric     DisableSelectToBranch("disable-cgp-select2branch", cl::Hidden,
146bdd1243dSDimitry Andric                           cl::init(false),
1470b57cec5SDimitry Andric                           cl::desc("Disable select to branch conversion."));
1480b57cec5SDimitry Andric 
149bdd1243dSDimitry Andric static cl::opt<bool>
150bdd1243dSDimitry Andric     AddrSinkUsingGEPs("addr-sink-using-gep", cl::Hidden, cl::init(true),
1510b57cec5SDimitry Andric                       cl::desc("Address sinking in CGP using GEPs."));
1520b57cec5SDimitry Andric 
153bdd1243dSDimitry Andric static cl::opt<bool>
154bdd1243dSDimitry Andric     EnableAndCmpSinking("enable-andcmp-sinking", cl::Hidden, cl::init(true),
1550b57cec5SDimitry Andric                         cl::desc("Enable sinkinig and/cmp into branches."));
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric static cl::opt<bool> DisableStoreExtract(
1580b57cec5SDimitry Andric     "disable-cgp-store-extract", cl::Hidden, cl::init(false),
1590b57cec5SDimitry Andric     cl::desc("Disable store(extract) optimizations in CodeGenPrepare"));
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric static cl::opt<bool> StressStoreExtract(
1620b57cec5SDimitry Andric     "stress-cgp-store-extract", cl::Hidden, cl::init(false),
1630b57cec5SDimitry Andric     cl::desc("Stress test store(extract) optimizations in CodeGenPrepare"));
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric static cl::opt<bool> DisableExtLdPromotion(
1660b57cec5SDimitry Andric     "disable-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
1670b57cec5SDimitry Andric     cl::desc("Disable ext(promotable(ld)) -> promoted(ext(ld)) optimization in "
1680b57cec5SDimitry Andric              "CodeGenPrepare"));
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric static cl::opt<bool> StressExtLdPromotion(
1710b57cec5SDimitry Andric     "stress-cgp-ext-ld-promotion", cl::Hidden, cl::init(false),
1720b57cec5SDimitry Andric     cl::desc("Stress test ext(promotable(ld)) -> promoted(ext(ld)) "
1730b57cec5SDimitry Andric              "optimization in CodeGenPrepare"));
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric static cl::opt<bool> DisablePreheaderProtect(
1760b57cec5SDimitry Andric     "disable-preheader-prot", cl::Hidden, cl::init(false),
1770b57cec5SDimitry Andric     cl::desc("Disable protection against removing loop preheaders"));
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric static cl::opt<bool> ProfileGuidedSectionPrefix(
18081ad6265SDimitry Andric     "profile-guided-section-prefix", cl::Hidden, cl::init(true),
1810b57cec5SDimitry Andric     cl::desc("Use profile info to add section prefix for hot/cold functions"));
1820b57cec5SDimitry Andric 
1835ffd83dbSDimitry Andric static cl::opt<bool> ProfileUnknownInSpecialSection(
18481ad6265SDimitry Andric     "profile-unknown-in-special-section", cl::Hidden,
1855ffd83dbSDimitry Andric     cl::desc("In profiling mode like sampleFDO, if a function doesn't have "
1865ffd83dbSDimitry Andric              "profile, we cannot tell the function is cold for sure because "
1875ffd83dbSDimitry Andric              "it may be a function newly added without ever being sampled. "
1885ffd83dbSDimitry Andric              "With the flag enabled, compiler can put such profile unknown "
1895ffd83dbSDimitry Andric              "functions into a special section, so runtime system can choose "
1905ffd83dbSDimitry Andric              "to handle it in a different way than .text section, to save "
1915ffd83dbSDimitry Andric              "RAM for example. "));
1925ffd83dbSDimitry Andric 
19381ad6265SDimitry Andric static cl::opt<bool> BBSectionsGuidedSectionPrefix(
19481ad6265SDimitry Andric     "bbsections-guided-section-prefix", cl::Hidden, cl::init(true),
19581ad6265SDimitry Andric     cl::desc("Use the basic-block-sections profile to determine the text "
19681ad6265SDimitry Andric              "section prefix for hot functions. Functions with "
19781ad6265SDimitry Andric              "basic-block-sections profile will be placed in `.text.hot` "
19881ad6265SDimitry Andric              "regardless of their FDO profile info. Other functions won't be "
19981ad6265SDimitry Andric              "impacted, i.e., their prefixes will be decided by FDO/sampleFDO "
20081ad6265SDimitry Andric              "profiles."));
20181ad6265SDimitry Andric 
202c9157d92SDimitry Andric static cl::opt<uint64_t> FreqRatioToSkipMerge(
2030b57cec5SDimitry Andric     "cgp-freq-ratio-to-skip-merge", cl::Hidden, cl::init(2),
2040b57cec5SDimitry Andric     cl::desc("Skip merging empty blocks if (frequency of empty block) / "
2050b57cec5SDimitry Andric              "(frequency of destination block) is greater than this ratio"));
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric static cl::opt<bool> ForceSplitStore(
2080b57cec5SDimitry Andric     "force-split-store", cl::Hidden, cl::init(false),
2090b57cec5SDimitry Andric     cl::desc("Force store splitting no matter what the target query says."));
2100b57cec5SDimitry Andric 
211bdd1243dSDimitry Andric static cl::opt<bool> EnableTypePromotionMerge(
212bdd1243dSDimitry Andric     "cgp-type-promotion-merge", cl::Hidden,
2130b57cec5SDimitry Andric     cl::desc("Enable merging of redundant sexts when one is dominating"
214bdd1243dSDimitry Andric              " the other."),
215bdd1243dSDimitry Andric     cl::init(true));
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric static cl::opt<bool> DisableComplexAddrModes(
2180b57cec5SDimitry Andric     "disable-complex-addr-modes", cl::Hidden, cl::init(false),
2190b57cec5SDimitry Andric     cl::desc("Disables combining addressing modes with different parts "
2200b57cec5SDimitry Andric              "in optimizeMemoryInst."));
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric static cl::opt<bool>
2230b57cec5SDimitry Andric     AddrSinkNewPhis("addr-sink-new-phis", cl::Hidden, cl::init(false),
2240b57cec5SDimitry Andric                     cl::desc("Allow creation of Phis in Address sinking."));
2250b57cec5SDimitry Andric 
226bdd1243dSDimitry Andric static cl::opt<bool> AddrSinkNewSelects(
227bdd1243dSDimitry Andric     "addr-sink-new-select", cl::Hidden, cl::init(true),
2280b57cec5SDimitry Andric     cl::desc("Allow creation of selects in Address sinking."));
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric static cl::opt<bool> AddrSinkCombineBaseReg(
2310b57cec5SDimitry Andric     "addr-sink-combine-base-reg", cl::Hidden, cl::init(true),
2320b57cec5SDimitry Andric     cl::desc("Allow combining of BaseReg field in Address sinking."));
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric static cl::opt<bool> AddrSinkCombineBaseGV(
2350b57cec5SDimitry Andric     "addr-sink-combine-base-gv", cl::Hidden, cl::init(true),
2360b57cec5SDimitry Andric     cl::desc("Allow combining of BaseGV field in Address sinking."));
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric static cl::opt<bool> AddrSinkCombineBaseOffs(
2390b57cec5SDimitry Andric     "addr-sink-combine-base-offs", cl::Hidden, cl::init(true),
2400b57cec5SDimitry Andric     cl::desc("Allow combining of BaseOffs field in Address sinking."));
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric static cl::opt<bool> AddrSinkCombineScaledReg(
2430b57cec5SDimitry Andric     "addr-sink-combine-scaled-reg", cl::Hidden, cl::init(true),
2440b57cec5SDimitry Andric     cl::desc("Allow combining of ScaledReg field in Address sinking."));
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric static cl::opt<bool>
2470b57cec5SDimitry Andric     EnableGEPOffsetSplit("cgp-split-large-offset-gep", cl::Hidden,
2480b57cec5SDimitry Andric                          cl::init(true),
2490b57cec5SDimitry Andric                          cl::desc("Enable splitting large offset of GEP."));
2500b57cec5SDimitry Andric 
251480093f4SDimitry Andric static cl::opt<bool> EnableICMP_EQToICMP_ST(
252480093f4SDimitry Andric     "cgp-icmp-eq2icmp-st", cl::Hidden, cl::init(false),
253480093f4SDimitry Andric     cl::desc("Enable ICMP_EQ to ICMP_S(L|G)T conversion."));
254480093f4SDimitry Andric 
2555ffd83dbSDimitry Andric static cl::opt<bool>
2565ffd83dbSDimitry Andric     VerifyBFIUpdates("cgp-verify-bfi-updates", cl::Hidden, cl::init(false),
2575ffd83dbSDimitry Andric                      cl::desc("Enable BFI update verification for "
2585ffd83dbSDimitry Andric                               "CodeGenPrepare."));
2595ffd83dbSDimitry Andric 
260bdd1243dSDimitry Andric static cl::opt<bool>
261fe013be4SDimitry Andric     OptimizePhiTypes("cgp-optimize-phi-types", cl::Hidden, cl::init(true),
2625ffd83dbSDimitry Andric                      cl::desc("Enable converting phi types in CodeGenPrepare"));
2635ffd83dbSDimitry Andric 
264bdd1243dSDimitry Andric static cl::opt<unsigned>
265bdd1243dSDimitry Andric     HugeFuncThresholdInCGPP("cgpp-huge-func", cl::init(10000), cl::Hidden,
266bdd1243dSDimitry Andric                             cl::desc("Least BB number of huge function."));
267bdd1243dSDimitry Andric 
268fe013be4SDimitry Andric static cl::opt<unsigned>
269fe013be4SDimitry Andric     MaxAddressUsersToScan("cgp-max-address-users-to-scan", cl::init(100),
270fe013be4SDimitry Andric                           cl::Hidden,
271fe013be4SDimitry Andric                           cl::desc("Max number of address users to look at"));
272c9157d92SDimitry Andric 
273c9157d92SDimitry Andric static cl::opt<bool>
274c9157d92SDimitry Andric     DisableDeletePHIs("disable-cgp-delete-phis", cl::Hidden, cl::init(false),
275c9157d92SDimitry Andric                       cl::desc("Disable elimination of dead PHI nodes."));
276c9157d92SDimitry Andric 
2770b57cec5SDimitry Andric namespace {
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric enum ExtType {
2800b57cec5SDimitry Andric   ZeroExtension, // Zero extension has been seen.
2810b57cec5SDimitry Andric   SignExtension, // Sign extension has been seen.
2820b57cec5SDimitry Andric   BothExtension  // This extension type is used if we saw sext after
2830b57cec5SDimitry Andric                  // ZeroExtension had been set, or if we saw zext after
2840b57cec5SDimitry Andric                  // SignExtension had been set. It makes the type
2850b57cec5SDimitry Andric                  // information of a promoted instruction invalid.
2860b57cec5SDimitry Andric };
2870b57cec5SDimitry Andric 
288bdd1243dSDimitry Andric enum ModifyDT {
289bdd1243dSDimitry Andric   NotModifyDT, // Not Modify any DT.
290bdd1243dSDimitry Andric   ModifyBBDT,  // Modify the Basic Block Dominator Tree.
291bdd1243dSDimitry Andric   ModifyInstDT // Modify the Instruction Dominator in a Basic Block,
292bdd1243dSDimitry Andric                // This usually means we move/delete/insert instruction
293bdd1243dSDimitry Andric                // in a Basic Block. So we should re-iterate instructions
294bdd1243dSDimitry Andric                // in such Basic Block.
295bdd1243dSDimitry Andric };
296bdd1243dSDimitry Andric 
2970b57cec5SDimitry Andric using SetOfInstrs = SmallPtrSet<Instruction *, 16>;
2980b57cec5SDimitry Andric using TypeIsSExt = PointerIntPair<Type *, 2, ExtType>;
2990b57cec5SDimitry Andric using InstrToOrigTy = DenseMap<Instruction *, TypeIsSExt>;
3000b57cec5SDimitry Andric using SExts = SmallVector<Instruction *, 16>;
301bdd1243dSDimitry Andric using ValueToSExts = MapVector<Value *, SExts>;
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric class TypePromotionTransaction;
3040b57cec5SDimitry Andric 
305cdc20ff6SDimitry Andric class CodeGenPrepare {
306cdc20ff6SDimitry Andric   friend class CodeGenPrepareLegacyPass;
3070b57cec5SDimitry Andric   const TargetMachine *TM = nullptr;
308fe013be4SDimitry Andric   const TargetSubtargetInfo *SubtargetInfo = nullptr;
3090b57cec5SDimitry Andric   const TargetLowering *TLI = nullptr;
310fe013be4SDimitry Andric   const TargetRegisterInfo *TRI = nullptr;
3110b57cec5SDimitry Andric   const TargetTransformInfo *TTI = nullptr;
31281ad6265SDimitry Andric   const BasicBlockSectionsProfileReader *BBSectionsProfileReader = nullptr;
313fe013be4SDimitry Andric   const TargetLibraryInfo *TLInfo = nullptr;
314fe013be4SDimitry Andric   LoopInfo *LI = nullptr;
3150b57cec5SDimitry Andric   std::unique_ptr<BlockFrequencyInfo> BFI;
3160b57cec5SDimitry Andric   std::unique_ptr<BranchProbabilityInfo> BPI;
317fe013be4SDimitry Andric   ProfileSummaryInfo *PSI = nullptr;
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   /// As we scan instructions optimizing them, this is the next instruction
3200b57cec5SDimitry Andric   /// to optimize. Transforms that can invalidate this should update it.
3210b57cec5SDimitry Andric   BasicBlock::iterator CurInstIterator;
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   /// Keeps track of non-local addresses that have been sunk into a block.
3240b57cec5SDimitry Andric   /// This allows us to avoid inserting duplicate code for blocks with
3250b57cec5SDimitry Andric   /// multiple load/stores of the same address. The usage of WeakTrackingVH
3260b57cec5SDimitry Andric   /// enables SunkAddrs to be treated as a cache whose entries can be
3270b57cec5SDimitry Andric   /// invalidated if a sunken address computation has been erased.
3280b57cec5SDimitry Andric   ValueMap<Value *, WeakTrackingVH> SunkAddrs;
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   /// Keeps track of all instructions inserted for the current function.
3310b57cec5SDimitry Andric   SetOfInstrs InsertedInsts;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   /// Keeps track of the type of the related instruction before their
3340b57cec5SDimitry Andric   /// promotion for the current function.
3350b57cec5SDimitry Andric   InstrToOrigTy PromotedInsts;
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   /// Keep track of instructions removed during promotion.
3380b57cec5SDimitry Andric   SetOfInstrs RemovedInsts;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   /// Keep track of sext chains based on their initial value.
3410b57cec5SDimitry Andric   DenseMap<Value *, Instruction *> SeenChainsForSExt;
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   /// Keep track of GEPs accessing the same data structures such as structs or
3440b57cec5SDimitry Andric   /// arrays that are candidates to be split later because of their large
3450b57cec5SDimitry Andric   /// size.
346bdd1243dSDimitry Andric   MapVector<AssertingVH<Value>,
3470b57cec5SDimitry Andric             SmallVector<std::pair<AssertingVH<GetElementPtrInst>, int64_t>, 32>>
3480b57cec5SDimitry Andric       LargeOffsetGEPMap;
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric   /// Keep track of new GEP base after splitting the GEPs having large offset.
3510b57cec5SDimitry Andric   SmallSet<AssertingVH<Value>, 2> NewGEPBases;
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   /// Map serial numbers to Large offset GEPs.
3540b57cec5SDimitry Andric   DenseMap<AssertingVH<GetElementPtrInst>, int> LargeOffsetGEPID;
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric   /// Keep track of SExt promoted.
3570b57cec5SDimitry Andric   ValueToSExts ValToSExtendedUses;
3580b57cec5SDimitry Andric 
359480093f4SDimitry Andric   /// True if the function has the OptSize attribute.
3600b57cec5SDimitry Andric   bool OptSize;
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   /// DataLayout for the Function being processed.
3630b57cec5SDimitry Andric   const DataLayout *DL = nullptr;
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   /// Building the dominator tree can be expensive, so we only build it
3660b57cec5SDimitry Andric   /// lazily and update it when required.
3670b57cec5SDimitry Andric   std::unique_ptr<DominatorTree> DT;
3680b57cec5SDimitry Andric 
3690b57cec5SDimitry Andric public:
CodeGenPrepare()370cdc20ff6SDimitry Andric   CodeGenPrepare(){};
CodeGenPrepare(const TargetMachine * TM)371cdc20ff6SDimitry Andric   CodeGenPrepare(const TargetMachine *TM) : TM(TM){};
372bdd1243dSDimitry Andric   /// If encounter huge function, we need to limit the build time.
373bdd1243dSDimitry Andric   bool IsHugeFunc = false;
374bdd1243dSDimitry Andric 
375bdd1243dSDimitry Andric   /// FreshBBs is like worklist, it collected the updated BBs which need
376bdd1243dSDimitry Andric   /// to be optimized again.
377bdd1243dSDimitry Andric   /// Note: Consider building time in this pass, when a BB updated, we need
378bdd1243dSDimitry Andric   /// to insert such BB into FreshBBs for huge function.
379bdd1243dSDimitry Andric   SmallSet<BasicBlock *, 32> FreshBBs;
380bdd1243dSDimitry Andric 
releaseMemory()381cdc20ff6SDimitry Andric   void releaseMemory() {
382fe013be4SDimitry Andric     // Clear per function information.
383fe013be4SDimitry Andric     InsertedInsts.clear();
384fe013be4SDimitry Andric     PromotedInsts.clear();
385fe013be4SDimitry Andric     FreshBBs.clear();
386fe013be4SDimitry Andric     BPI.reset();
387fe013be4SDimitry Andric     BFI.reset();
388fe013be4SDimitry Andric   }
389fe013be4SDimitry Andric 
390cdc20ff6SDimitry Andric   bool run(Function &F, FunctionAnalysisManager &AM);
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric private:
3930b57cec5SDimitry Andric   template <typename F>
resetIteratorIfInvalidatedWhileCalling(BasicBlock * BB,F f)3940b57cec5SDimitry Andric   void resetIteratorIfInvalidatedWhileCalling(BasicBlock *BB, F f) {
3950b57cec5SDimitry Andric     // Substituting can cause recursive simplifications, which can invalidate
3960b57cec5SDimitry Andric     // our iterator.  Use a WeakTrackingVH to hold onto it in case this
3970b57cec5SDimitry Andric     // happens.
3980b57cec5SDimitry Andric     Value *CurValue = &*CurInstIterator;
3990b57cec5SDimitry Andric     WeakTrackingVH IterHandle(CurValue);
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric     f();
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric     // If the iterator instruction was recursively deleted, start over at the
4040b57cec5SDimitry Andric     // start of the block.
4050b57cec5SDimitry Andric     if (IterHandle != CurValue) {
4060b57cec5SDimitry Andric       CurInstIterator = BB->begin();
4070b57cec5SDimitry Andric       SunkAddrs.clear();
4080b57cec5SDimitry Andric     }
4090b57cec5SDimitry Andric   }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric   // Get the DominatorTree, building if necessary.
getDT(Function & F)4120b57cec5SDimitry Andric   DominatorTree &getDT(Function &F) {
4130b57cec5SDimitry Andric     if (!DT)
4148bcb0991SDimitry Andric       DT = std::make_unique<DominatorTree>(F);
4150b57cec5SDimitry Andric     return *DT;
4160b57cec5SDimitry Andric   }
4170b57cec5SDimitry Andric 
418e8d8bef9SDimitry Andric   void removeAllAssertingVHReferences(Value *V);
419fe6060f1SDimitry Andric   bool eliminateAssumptions(Function &F);
420fe013be4SDimitry Andric   bool eliminateFallThrough(Function &F, DominatorTree *DT = nullptr);
4210b57cec5SDimitry Andric   bool eliminateMostlyEmptyBlocks(Function &F);
4220b57cec5SDimitry Andric   BasicBlock *findDestBlockOfMergeableEmptyBlock(BasicBlock *BB);
4230b57cec5SDimitry Andric   bool canMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
4240b57cec5SDimitry Andric   void eliminateMostlyEmptyBlock(BasicBlock *BB);
4250b57cec5SDimitry Andric   bool isMergingEmptyBlockProfitable(BasicBlock *BB, BasicBlock *DestBB,
4260b57cec5SDimitry Andric                                      bool isPreheader);
427e8d8bef9SDimitry Andric   bool makeBitReverse(Instruction &I);
428bdd1243dSDimitry Andric   bool optimizeBlock(BasicBlock &BB, ModifyDT &ModifiedDT);
429bdd1243dSDimitry Andric   bool optimizeInst(Instruction *I, ModifyDT &ModifiedDT);
430bdd1243dSDimitry Andric   bool optimizeMemoryInst(Instruction *MemoryInst, Value *Addr, Type *AccessTy,
431bdd1243dSDimitry Andric                           unsigned AddrSpace);
4325ffd83dbSDimitry Andric   bool optimizeGatherScatterInst(Instruction *MemoryInst, Value *Ptr);
4330b57cec5SDimitry Andric   bool optimizeInlineAsmInst(CallInst *CS);
434bdd1243dSDimitry Andric   bool optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT);
4350b57cec5SDimitry Andric   bool optimizeExt(Instruction *&I);
4360b57cec5SDimitry Andric   bool optimizeExtUses(Instruction *I);
4370b57cec5SDimitry Andric   bool optimizeLoadExt(LoadInst *Load);
4380b57cec5SDimitry Andric   bool optimizeShiftInst(BinaryOperator *BO);
4395ffd83dbSDimitry Andric   bool optimizeFunnelShift(IntrinsicInst *Fsh);
4400b57cec5SDimitry Andric   bool optimizeSelectInst(SelectInst *SI);
4410b57cec5SDimitry Andric   bool optimizeShuffleVectorInst(ShuffleVectorInst *SVI);
44281ad6265SDimitry Andric   bool optimizeSwitchType(SwitchInst *SI);
44381ad6265SDimitry Andric   bool optimizeSwitchPhiConstants(SwitchInst *SI);
4440b57cec5SDimitry Andric   bool optimizeSwitchInst(SwitchInst *SI);
4450b57cec5SDimitry Andric   bool optimizeExtractElementInst(Instruction *Inst);
446bdd1243dSDimitry Andric   bool dupRetToEnableTailCallOpts(BasicBlock *BB, ModifyDT &ModifiedDT);
447480093f4SDimitry Andric   bool fixupDbgValue(Instruction *I);
448c9157d92SDimitry Andric   bool fixupDPValue(DPValue &I);
449c9157d92SDimitry Andric   bool fixupDPValuesOnInst(Instruction &I);
4500b57cec5SDimitry Andric   bool placeDbgValues(Function &F);
451fe6060f1SDimitry Andric   bool placePseudoProbes(Function &F);
4520b57cec5SDimitry Andric   bool canFormExtLd(const SmallVectorImpl<Instruction *> &MovedExts,
4530b57cec5SDimitry Andric                     LoadInst *&LI, Instruction *&Inst, bool HasPromoted);
4540b57cec5SDimitry Andric   bool tryToPromoteExts(TypePromotionTransaction &TPT,
4550b57cec5SDimitry Andric                         const SmallVectorImpl<Instruction *> &Exts,
4560b57cec5SDimitry Andric                         SmallVectorImpl<Instruction *> &ProfitablyMovedExts,
4570b57cec5SDimitry Andric                         unsigned CreatedInstsCost = 0);
4580b57cec5SDimitry Andric   bool mergeSExts(Function &F);
4590b57cec5SDimitry Andric   bool splitLargeGEPOffsets();
4605ffd83dbSDimitry Andric   bool optimizePhiType(PHINode *Inst, SmallPtrSetImpl<PHINode *> &Visited,
4615ffd83dbSDimitry Andric                        SmallPtrSetImpl<Instruction *> &DeletedInstrs);
4625ffd83dbSDimitry Andric   bool optimizePhiTypes(Function &F);
4630b57cec5SDimitry Andric   bool performAddressTypePromotion(
464bdd1243dSDimitry Andric       Instruction *&Inst, bool AllowPromotionWithoutCommonHeader,
4650b57cec5SDimitry Andric       bool HasPromoted, TypePromotionTransaction &TPT,
4660b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> &SpeculativelyMovedExts);
467bdd1243dSDimitry Andric   bool splitBranchCondition(Function &F, ModifyDT &ModifiedDT);
4685ffd83dbSDimitry Andric   bool simplifyOffsetableRelocate(GCStatepointInst &I);
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric   bool tryToSinkFreeOperands(Instruction *I);
471bdd1243dSDimitry Andric   bool replaceMathCmpWithIntrinsic(BinaryOperator *BO, Value *Arg0, Value *Arg1,
472bdd1243dSDimitry Andric                                    CmpInst *Cmp, Intrinsic::ID IID);
473bdd1243dSDimitry Andric   bool optimizeCmp(CmpInst *Cmp, ModifyDT &ModifiedDT);
474bdd1243dSDimitry Andric   bool combineToUSubWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
475bdd1243dSDimitry Andric   bool combineToUAddWithOverflow(CmpInst *Cmp, ModifyDT &ModifiedDT);
4765ffd83dbSDimitry Andric   void verifyBFIUpdates(Function &F);
477cdc20ff6SDimitry Andric   bool _run(Function &F);
478cdc20ff6SDimitry Andric };
479cdc20ff6SDimitry Andric 
480cdc20ff6SDimitry Andric class CodeGenPrepareLegacyPass : public FunctionPass {
481cdc20ff6SDimitry Andric public:
482cdc20ff6SDimitry Andric   static char ID; // Pass identification, replacement for typeid
483cdc20ff6SDimitry Andric 
CodeGenPrepareLegacyPass()484cdc20ff6SDimitry Andric   CodeGenPrepareLegacyPass() : FunctionPass(ID) {
485cdc20ff6SDimitry Andric     initializeCodeGenPrepareLegacyPassPass(*PassRegistry::getPassRegistry());
486cdc20ff6SDimitry Andric   }
487cdc20ff6SDimitry Andric 
488cdc20ff6SDimitry Andric   bool runOnFunction(Function &F) override;
489cdc20ff6SDimitry Andric 
getPassName() const490cdc20ff6SDimitry Andric   StringRef getPassName() const override { return "CodeGen Prepare"; }
491cdc20ff6SDimitry Andric 
getAnalysisUsage(AnalysisUsage & AU) const492cdc20ff6SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
493cdc20ff6SDimitry Andric     // FIXME: When we can selectively preserve passes, preserve the domtree.
494cdc20ff6SDimitry Andric     AU.addRequired<ProfileSummaryInfoWrapperPass>();
495cdc20ff6SDimitry Andric     AU.addRequired<TargetLibraryInfoWrapperPass>();
496cdc20ff6SDimitry Andric     AU.addRequired<TargetPassConfig>();
497cdc20ff6SDimitry Andric     AU.addRequired<TargetTransformInfoWrapperPass>();
498cdc20ff6SDimitry Andric     AU.addRequired<LoopInfoWrapperPass>();
499cdc20ff6SDimitry Andric     AU.addUsedIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
500cdc20ff6SDimitry Andric   }
5010b57cec5SDimitry Andric };
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric } // end anonymous namespace
5040b57cec5SDimitry Andric 
505cdc20ff6SDimitry Andric char CodeGenPrepareLegacyPass::ID = 0;
5060b57cec5SDimitry Andric 
runOnFunction(Function & F)507cdc20ff6SDimitry Andric bool CodeGenPrepareLegacyPass::runOnFunction(Function &F) {
508cdc20ff6SDimitry Andric   if (skipFunction(F))
509cdc20ff6SDimitry Andric     return false;
510cdc20ff6SDimitry Andric   auto TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
511cdc20ff6SDimitry Andric   CodeGenPrepare CGP(TM);
512cdc20ff6SDimitry Andric   CGP.DL = &F.getParent()->getDataLayout();
513cdc20ff6SDimitry Andric   CGP.SubtargetInfo = TM->getSubtargetImpl(F);
514cdc20ff6SDimitry Andric   CGP.TLI = CGP.SubtargetInfo->getTargetLowering();
515cdc20ff6SDimitry Andric   CGP.TRI = CGP.SubtargetInfo->getRegisterInfo();
516cdc20ff6SDimitry Andric   CGP.TLInfo = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
517cdc20ff6SDimitry Andric   CGP.TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
518cdc20ff6SDimitry Andric   CGP.LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
519cdc20ff6SDimitry Andric   CGP.BPI.reset(new BranchProbabilityInfo(F, *CGP.LI));
520cdc20ff6SDimitry Andric   CGP.BFI.reset(new BlockFrequencyInfo(F, *CGP.BPI, *CGP.LI));
521cdc20ff6SDimitry Andric   CGP.PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
522cdc20ff6SDimitry Andric   auto BBSPRWP =
523cdc20ff6SDimitry Andric       getAnalysisIfAvailable<BasicBlockSectionsProfileReaderWrapperPass>();
524cdc20ff6SDimitry Andric   CGP.BBSectionsProfileReader = BBSPRWP ? &BBSPRWP->getBBSPR() : nullptr;
525cdc20ff6SDimitry Andric 
526cdc20ff6SDimitry Andric   return CGP._run(F);
527cdc20ff6SDimitry Andric }
528cdc20ff6SDimitry Andric 
529cdc20ff6SDimitry Andric INITIALIZE_PASS_BEGIN(CodeGenPrepareLegacyPass, DEBUG_TYPE,
5300b57cec5SDimitry Andric                       "Optimize for code generation", false, false)
INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass)531cdc20ff6SDimitry Andric INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass)
532e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
5330b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass)
534e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
535e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
536e8d8bef9SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
537cdc20ff6SDimitry Andric INITIALIZE_PASS_END(CodeGenPrepareLegacyPass, DEBUG_TYPE,
538cdc20ff6SDimitry Andric                     "Optimize for code generation", false, false)
5390b57cec5SDimitry Andric 
540cdc20ff6SDimitry Andric FunctionPass *llvm::createCodeGenPrepareLegacyPass() {
541cdc20ff6SDimitry Andric   return new CodeGenPrepareLegacyPass();
542cdc20ff6SDimitry Andric }
5430b57cec5SDimitry Andric 
run(Function & F,FunctionAnalysisManager & AM)544cdc20ff6SDimitry Andric PreservedAnalyses CodeGenPreparePass::run(Function &F,
545cdc20ff6SDimitry Andric                                           FunctionAnalysisManager &AM) {
546cdc20ff6SDimitry Andric   CodeGenPrepare CGP(TM);
5470b57cec5SDimitry Andric 
548cdc20ff6SDimitry Andric   bool Changed = CGP.run(F, AM);
549cdc20ff6SDimitry Andric   if (!Changed)
550cdc20ff6SDimitry Andric     return PreservedAnalyses::all();
551cdc20ff6SDimitry Andric 
552cdc20ff6SDimitry Andric   PreservedAnalyses PA;
553cdc20ff6SDimitry Andric   PA.preserve<TargetLibraryAnalysis>();
554cdc20ff6SDimitry Andric   PA.preserve<TargetIRAnalysis>();
555cdc20ff6SDimitry Andric   PA.preserve<LoopAnalysis>();
556cdc20ff6SDimitry Andric   return PA;
557cdc20ff6SDimitry Andric }
558cdc20ff6SDimitry Andric 
run(Function & F,FunctionAnalysisManager & AM)559cdc20ff6SDimitry Andric bool CodeGenPrepare::run(Function &F, FunctionAnalysisManager &AM) {
5600b57cec5SDimitry Andric   DL = &F.getParent()->getDataLayout();
5610b57cec5SDimitry Andric   SubtargetInfo = TM->getSubtargetImpl(F);
5620b57cec5SDimitry Andric   TLI = SubtargetInfo->getTargetLowering();
5630b57cec5SDimitry Andric   TRI = SubtargetInfo->getRegisterInfo();
564cdc20ff6SDimitry Andric   TLInfo = &AM.getResult<TargetLibraryAnalysis>(F);
565cdc20ff6SDimitry Andric   TTI = &AM.getResult<TargetIRAnalysis>(F);
566cdc20ff6SDimitry Andric   LI = &AM.getResult<LoopAnalysis>(F);
5670b57cec5SDimitry Andric   BPI.reset(new BranchProbabilityInfo(F, *LI));
5680b57cec5SDimitry Andric   BFI.reset(new BlockFrequencyInfo(F, *BPI, *LI));
569cdc20ff6SDimitry Andric   auto &MAMProxy = AM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
570cdc20ff6SDimitry Andric   PSI = MAMProxy.getCachedResult<ProfileSummaryAnalysis>(*F.getParent());
57181ad6265SDimitry Andric   BBSectionsProfileReader =
572cdc20ff6SDimitry Andric       AM.getCachedResult<BasicBlockSectionsProfileReaderAnalysis>(F);
573cdc20ff6SDimitry Andric   return _run(F);
574cdc20ff6SDimitry Andric }
575cdc20ff6SDimitry Andric 
_run(Function & F)576cdc20ff6SDimitry Andric bool CodeGenPrepare::_run(Function &F) {
577cdc20ff6SDimitry Andric   bool EverMadeChange = false;
578cdc20ff6SDimitry Andric 
5790b57cec5SDimitry Andric   OptSize = F.hasOptSize();
580bdd1243dSDimitry Andric   // Use the basic-block-sections profile to promote hot functions to .text.hot
581bdd1243dSDimitry Andric   // if requested.
58281ad6265SDimitry Andric   if (BBSectionsGuidedSectionPrefix && BBSectionsProfileReader &&
58381ad6265SDimitry Andric       BBSectionsProfileReader->isFunctionHot(F.getName())) {
58481ad6265SDimitry Andric     F.setSectionPrefix("hot");
58581ad6265SDimitry Andric   } else if (ProfileGuidedSectionPrefix) {
586e8d8bef9SDimitry Andric     // The hot attribute overwrites profile count based hotness while profile
587e8d8bef9SDimitry Andric     // counts based hotness overwrite the cold attribute.
588e8d8bef9SDimitry Andric     // This is a conservative behabvior.
589e8d8bef9SDimitry Andric     if (F.hasFnAttribute(Attribute::Hot) ||
590e8d8bef9SDimitry Andric         PSI->isFunctionHotInCallGraph(&F, *BFI))
591e8d8bef9SDimitry Andric       F.setSectionPrefix("hot");
592e8d8bef9SDimitry Andric     // If PSI shows this function is not hot, we will placed the function
593e8d8bef9SDimitry Andric     // into unlikely section if (1) PSI shows this is a cold function, or
594e8d8bef9SDimitry Andric     // (2) the function has a attribute of cold.
595e8d8bef9SDimitry Andric     else if (PSI->isFunctionColdInCallGraph(&F, *BFI) ||
596e8d8bef9SDimitry Andric              F.hasFnAttribute(Attribute::Cold))
597e8d8bef9SDimitry Andric       F.setSectionPrefix("unlikely");
5985ffd83dbSDimitry Andric     else if (ProfileUnknownInSpecialSection && PSI->hasPartialSampleProfile() &&
5995ffd83dbSDimitry Andric              PSI->isFunctionHotnessUnknown(F))
600e8d8bef9SDimitry Andric       F.setSectionPrefix("unknown");
6010b57cec5SDimitry Andric   }
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric   /// This optimization identifies DIV instructions that can be
6040b57cec5SDimitry Andric   /// profitably bypassed and carried out with a shorter, faster divide.
6055ffd83dbSDimitry Andric   if (!OptSize && !PSI->hasHugeWorkingSetSize() && TLI->isSlowDivBypassed()) {
6060b57cec5SDimitry Andric     const DenseMap<unsigned int, unsigned int> &BypassWidths =
6070b57cec5SDimitry Andric         TLI->getBypassSlowDivWidths();
6080b57cec5SDimitry Andric     BasicBlock *BB = &*F.begin();
6090b57cec5SDimitry Andric     while (BB != nullptr) {
6100b57cec5SDimitry Andric       // bypassSlowDivision may create new BBs, but we don't want to reapply the
6110b57cec5SDimitry Andric       // optimization to those blocks.
6120b57cec5SDimitry Andric       BasicBlock *Next = BB->getNextNode();
613480093f4SDimitry Andric       // F.hasOptSize is already checked in the outer if statement.
614480093f4SDimitry Andric       if (!llvm::shouldOptimizeForSize(BB, PSI, BFI.get()))
6150b57cec5SDimitry Andric         EverMadeChange |= bypassSlowDivision(BB, BypassWidths);
6160b57cec5SDimitry Andric       BB = Next;
6170b57cec5SDimitry Andric     }
6180b57cec5SDimitry Andric   }
6190b57cec5SDimitry Andric 
620fe6060f1SDimitry Andric   // Get rid of @llvm.assume builtins before attempting to eliminate empty
621fe6060f1SDimitry Andric   // blocks, since there might be blocks that only contain @llvm.assume calls
622fe6060f1SDimitry Andric   // (plus arguments that we can get rid of).
623fe6060f1SDimitry Andric   EverMadeChange |= eliminateAssumptions(F);
624fe6060f1SDimitry Andric 
6250b57cec5SDimitry Andric   // Eliminate blocks that contain only PHI nodes and an
6260b57cec5SDimitry Andric   // unconditional branch.
6270b57cec5SDimitry Andric   EverMadeChange |= eliminateMostlyEmptyBlocks(F);
6280b57cec5SDimitry Andric 
629bdd1243dSDimitry Andric   ModifyDT ModifiedDT = ModifyDT::NotModifyDT;
6300b57cec5SDimitry Andric   if (!DisableBranchOpts)
6310b57cec5SDimitry Andric     EverMadeChange |= splitBranchCondition(F, ModifiedDT);
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric   // Split some critical edges where one of the sources is an indirect branch,
6340b57cec5SDimitry Andric   // to help generate sane code for PHIs involving such edges.
63581ad6265SDimitry Andric   EverMadeChange |=
63681ad6265SDimitry Andric       SplitIndirectBrCriticalEdges(F, /*IgnoreBlocksWithoutPHI=*/true);
6370b57cec5SDimitry Andric 
638bdd1243dSDimitry Andric   // If we are optimzing huge function, we need to consider the build time.
639bdd1243dSDimitry Andric   // Because the basic algorithm's complex is near O(N!).
640bdd1243dSDimitry Andric   IsHugeFunc = F.size() > HugeFuncThresholdInCGPP;
641bdd1243dSDimitry Andric 
642fe013be4SDimitry Andric   // Transformations above may invalidate dominator tree and/or loop info.
643fe013be4SDimitry Andric   DT.reset();
644fe013be4SDimitry Andric   LI->releaseMemory();
645fe013be4SDimitry Andric   LI->analyze(getDT(F));
646fe013be4SDimitry Andric 
6470b57cec5SDimitry Andric   bool MadeChange = true;
648bdd1243dSDimitry Andric   bool FuncIterated = false;
6490b57cec5SDimitry Andric   while (MadeChange) {
6500b57cec5SDimitry Andric     MadeChange = false;
6510b57cec5SDimitry Andric 
652bdd1243dSDimitry Andric     for (BasicBlock &BB : llvm::make_early_inc_range(F)) {
653bdd1243dSDimitry Andric       if (FuncIterated && !FreshBBs.contains(&BB))
654bdd1243dSDimitry Andric         continue;
655bdd1243dSDimitry Andric 
656bdd1243dSDimitry Andric       ModifyDT ModifiedDTOnIteration = ModifyDT::NotModifyDT;
657bdd1243dSDimitry Andric       bool Changed = optimizeBlock(BB, ModifiedDTOnIteration);
658bdd1243dSDimitry Andric 
659fe013be4SDimitry Andric       if (ModifiedDTOnIteration == ModifyDT::ModifyBBDT)
660fe013be4SDimitry Andric         DT.reset();
661fe013be4SDimitry Andric 
662bdd1243dSDimitry Andric       MadeChange |= Changed;
663bdd1243dSDimitry Andric       if (IsHugeFunc) {
664bdd1243dSDimitry Andric         // If the BB is updated, it may still has chance to be optimized.
665bdd1243dSDimitry Andric         // This usually happen at sink optimization.
666bdd1243dSDimitry Andric         // For example:
667bdd1243dSDimitry Andric         //
668bdd1243dSDimitry Andric         // bb0:
669bdd1243dSDimitry Andric         // %and = and i32 %a, 4
670bdd1243dSDimitry Andric         // %cmp = icmp eq i32 %and, 0
671bdd1243dSDimitry Andric         //
672bdd1243dSDimitry Andric         // If the %cmp sink to other BB, the %and will has chance to sink.
673bdd1243dSDimitry Andric         if (Changed)
674bdd1243dSDimitry Andric           FreshBBs.insert(&BB);
675bdd1243dSDimitry Andric         else if (FuncIterated)
676bdd1243dSDimitry Andric           FreshBBs.erase(&BB);
677bdd1243dSDimitry Andric       } else {
678bdd1243dSDimitry Andric         // For small/normal functions, we restart BB iteration if the dominator
679bdd1243dSDimitry Andric         // tree of the Function was changed.
680bdd1243dSDimitry Andric         if (ModifiedDTOnIteration != ModifyDT::NotModifyDT)
6810b57cec5SDimitry Andric           break;
6820b57cec5SDimitry Andric       }
683bdd1243dSDimitry Andric     }
684bdd1243dSDimitry Andric     // We have iterated all the BB in the (only work for huge) function.
685bdd1243dSDimitry Andric     FuncIterated = IsHugeFunc;
686bdd1243dSDimitry Andric 
6870b57cec5SDimitry Andric     if (EnableTypePromotionMerge && !ValToSExtendedUses.empty())
6880b57cec5SDimitry Andric       MadeChange |= mergeSExts(F);
6890b57cec5SDimitry Andric     if (!LargeOffsetGEPMap.empty())
6900b57cec5SDimitry Andric       MadeChange |= splitLargeGEPOffsets();
6915ffd83dbSDimitry Andric     MadeChange |= optimizePhiTypes(F);
6925ffd83dbSDimitry Andric 
6935ffd83dbSDimitry Andric     if (MadeChange)
694fe013be4SDimitry Andric       eliminateFallThrough(F, DT.get());
695fe013be4SDimitry Andric 
696fe013be4SDimitry Andric #ifndef NDEBUG
697fe013be4SDimitry Andric     if (MadeChange && VerifyLoopInfo)
698fe013be4SDimitry Andric       LI->verify(getDT(F));
699fe013be4SDimitry Andric #endif
7000b57cec5SDimitry Andric 
7010b57cec5SDimitry Andric     // Really free removed instructions during promotion.
7020b57cec5SDimitry Andric     for (Instruction *I : RemovedInsts)
7030b57cec5SDimitry Andric       I->deleteValue();
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric     EverMadeChange |= MadeChange;
7060b57cec5SDimitry Andric     SeenChainsForSExt.clear();
7070b57cec5SDimitry Andric     ValToSExtendedUses.clear();
7080b57cec5SDimitry Andric     RemovedInsts.clear();
7090b57cec5SDimitry Andric     LargeOffsetGEPMap.clear();
7100b57cec5SDimitry Andric     LargeOffsetGEPID.clear();
7110b57cec5SDimitry Andric   }
7120b57cec5SDimitry Andric 
713e8d8bef9SDimitry Andric   NewGEPBases.clear();
7140b57cec5SDimitry Andric   SunkAddrs.clear();
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric   if (!DisableBranchOpts) {
7170b57cec5SDimitry Andric     MadeChange = false;
7180b57cec5SDimitry Andric     // Use a set vector to get deterministic iteration order. The order the
7190b57cec5SDimitry Andric     // blocks are removed may affect whether or not PHI nodes in successors
7200b57cec5SDimitry Andric     // are removed.
7210b57cec5SDimitry Andric     SmallSetVector<BasicBlock *, 8> WorkList;
7220b57cec5SDimitry Andric     for (BasicBlock &BB : F) {
723e8d8bef9SDimitry Andric       SmallVector<BasicBlock *, 2> Successors(successors(&BB));
7240b57cec5SDimitry Andric       MadeChange |= ConstantFoldTerminator(&BB, true);
725bdd1243dSDimitry Andric       if (!MadeChange)
726bdd1243dSDimitry Andric         continue;
7270b57cec5SDimitry Andric 
728fe6060f1SDimitry Andric       for (BasicBlock *Succ : Successors)
729fe6060f1SDimitry Andric         if (pred_empty(Succ))
730fe6060f1SDimitry Andric           WorkList.insert(Succ);
7310b57cec5SDimitry Andric     }
7320b57cec5SDimitry Andric 
7330b57cec5SDimitry Andric     // Delete the dead blocks and any of their dead successors.
7340b57cec5SDimitry Andric     MadeChange |= !WorkList.empty();
7350b57cec5SDimitry Andric     while (!WorkList.empty()) {
7360b57cec5SDimitry Andric       BasicBlock *BB = WorkList.pop_back_val();
737e8d8bef9SDimitry Andric       SmallVector<BasicBlock *, 2> Successors(successors(BB));
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric       DeleteDeadBlock(BB);
7400b57cec5SDimitry Andric 
741fe6060f1SDimitry Andric       for (BasicBlock *Succ : Successors)
742fe6060f1SDimitry Andric         if (pred_empty(Succ))
743fe6060f1SDimitry Andric           WorkList.insert(Succ);
7440b57cec5SDimitry Andric     }
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric     // Merge pairs of basic blocks with unconditional branches, connected by
7470b57cec5SDimitry Andric     // a single edge.
7480b57cec5SDimitry Andric     if (EverMadeChange || MadeChange)
7490b57cec5SDimitry Andric       MadeChange |= eliminateFallThrough(F);
7500b57cec5SDimitry Andric 
7510b57cec5SDimitry Andric     EverMadeChange |= MadeChange;
7520b57cec5SDimitry Andric   }
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric   if (!DisableGCOpts) {
7555ffd83dbSDimitry Andric     SmallVector<GCStatepointInst *, 2> Statepoints;
7560b57cec5SDimitry Andric     for (BasicBlock &BB : F)
7570b57cec5SDimitry Andric       for (Instruction &I : BB)
7585ffd83dbSDimitry Andric         if (auto *SP = dyn_cast<GCStatepointInst>(&I))
7595ffd83dbSDimitry Andric           Statepoints.push_back(SP);
7600b57cec5SDimitry Andric     for (auto &I : Statepoints)
7610b57cec5SDimitry Andric       EverMadeChange |= simplifyOffsetableRelocate(*I);
7620b57cec5SDimitry Andric   }
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric   // Do this last to clean up use-before-def scenarios introduced by other
7650b57cec5SDimitry Andric   // preparatory transforms.
7660b57cec5SDimitry Andric   EverMadeChange |= placeDbgValues(F);
767fe6060f1SDimitry Andric   EverMadeChange |= placePseudoProbes(F);
7680b57cec5SDimitry Andric 
7695ffd83dbSDimitry Andric #ifndef NDEBUG
7705ffd83dbSDimitry Andric   if (VerifyBFIUpdates)
7715ffd83dbSDimitry Andric     verifyBFIUpdates(F);
7725ffd83dbSDimitry Andric #endif
7735ffd83dbSDimitry Andric 
7740b57cec5SDimitry Andric   return EverMadeChange;
7750b57cec5SDimitry Andric }
7760b57cec5SDimitry Andric 
eliminateAssumptions(Function & F)777fe6060f1SDimitry Andric bool CodeGenPrepare::eliminateAssumptions(Function &F) {
778fe6060f1SDimitry Andric   bool MadeChange = false;
779fe6060f1SDimitry Andric   for (BasicBlock &BB : F) {
780fe6060f1SDimitry Andric     CurInstIterator = BB.begin();
781fe6060f1SDimitry Andric     while (CurInstIterator != BB.end()) {
782fe6060f1SDimitry Andric       Instruction *I = &*(CurInstIterator++);
783fe6060f1SDimitry Andric       if (auto *Assume = dyn_cast<AssumeInst>(I)) {
784fe6060f1SDimitry Andric         MadeChange = true;
785fe6060f1SDimitry Andric         Value *Operand = Assume->getOperand(0);
786fe6060f1SDimitry Andric         Assume->eraseFromParent();
787fe6060f1SDimitry Andric 
788fe6060f1SDimitry Andric         resetIteratorIfInvalidatedWhileCalling(&BB, [&]() {
789fe6060f1SDimitry Andric           RecursivelyDeleteTriviallyDeadInstructions(Operand, TLInfo, nullptr);
790fe6060f1SDimitry Andric         });
791fe6060f1SDimitry Andric       }
792fe6060f1SDimitry Andric     }
793fe6060f1SDimitry Andric   }
794fe6060f1SDimitry Andric   return MadeChange;
795fe6060f1SDimitry Andric }
796fe6060f1SDimitry Andric 
797e8d8bef9SDimitry Andric /// An instruction is about to be deleted, so remove all references to it in our
798e8d8bef9SDimitry Andric /// GEP-tracking data strcutures.
removeAllAssertingVHReferences(Value * V)799e8d8bef9SDimitry Andric void CodeGenPrepare::removeAllAssertingVHReferences(Value *V) {
800e8d8bef9SDimitry Andric   LargeOffsetGEPMap.erase(V);
801e8d8bef9SDimitry Andric   NewGEPBases.erase(V);
802e8d8bef9SDimitry Andric 
803e8d8bef9SDimitry Andric   auto GEP = dyn_cast<GetElementPtrInst>(V);
804e8d8bef9SDimitry Andric   if (!GEP)
805e8d8bef9SDimitry Andric     return;
806e8d8bef9SDimitry Andric 
807e8d8bef9SDimitry Andric   LargeOffsetGEPID.erase(GEP);
808e8d8bef9SDimitry Andric 
809e8d8bef9SDimitry Andric   auto VecI = LargeOffsetGEPMap.find(GEP->getPointerOperand());
810e8d8bef9SDimitry Andric   if (VecI == LargeOffsetGEPMap.end())
811e8d8bef9SDimitry Andric     return;
812e8d8bef9SDimitry Andric 
813e8d8bef9SDimitry Andric   auto &GEPVector = VecI->second;
814349cc55cSDimitry Andric   llvm::erase_if(GEPVector, [=](auto &Elt) { return Elt.first == GEP; });
815e8d8bef9SDimitry Andric 
816e8d8bef9SDimitry Andric   if (GEPVector.empty())
817e8d8bef9SDimitry Andric     LargeOffsetGEPMap.erase(VecI);
818e8d8bef9SDimitry Andric }
819e8d8bef9SDimitry Andric 
8205ffd83dbSDimitry Andric // Verify BFI has been updated correctly by recomputing BFI and comparing them.
verifyBFIUpdates(Function & F)8215ffd83dbSDimitry Andric void LLVM_ATTRIBUTE_UNUSED CodeGenPrepare::verifyBFIUpdates(Function &F) {
8225ffd83dbSDimitry Andric   DominatorTree NewDT(F);
8235ffd83dbSDimitry Andric   LoopInfo NewLI(NewDT);
8245ffd83dbSDimitry Andric   BranchProbabilityInfo NewBPI(F, NewLI, TLInfo);
8255ffd83dbSDimitry Andric   BlockFrequencyInfo NewBFI(F, NewBPI, NewLI);
8265ffd83dbSDimitry Andric   NewBFI.verifyMatch(*BFI);
8275ffd83dbSDimitry Andric }
8285ffd83dbSDimitry Andric 
8290b57cec5SDimitry Andric /// Merge basic blocks which are connected by a single edge, where one of the
8300b57cec5SDimitry Andric /// basic blocks has a single successor pointing to the other basic block,
8310b57cec5SDimitry Andric /// which has a single predecessor.
eliminateFallThrough(Function & F,DominatorTree * DT)832fe013be4SDimitry Andric bool CodeGenPrepare::eliminateFallThrough(Function &F, DominatorTree *DT) {
8330b57cec5SDimitry Andric   bool Changed = false;
8340b57cec5SDimitry Andric   // Scan all of the blocks in the function, except for the entry block.
8350b57cec5SDimitry Andric   // Use a temporary array to avoid iterator being invalidated when
8360b57cec5SDimitry Andric   // deleting blocks.
8370b57cec5SDimitry Andric   SmallVector<WeakTrackingVH, 16> Blocks;
838e8d8bef9SDimitry Andric   for (auto &Block : llvm::drop_begin(F))
8390b57cec5SDimitry Andric     Blocks.push_back(&Block);
8400b57cec5SDimitry Andric 
841e8d8bef9SDimitry Andric   SmallSet<WeakTrackingVH, 16> Preds;
8420b57cec5SDimitry Andric   for (auto &Block : Blocks) {
8430b57cec5SDimitry Andric     auto *BB = cast_or_null<BasicBlock>(Block);
8440b57cec5SDimitry Andric     if (!BB)
8450b57cec5SDimitry Andric       continue;
8460b57cec5SDimitry Andric     // If the destination block has a single pred, then this is a trivial
8470b57cec5SDimitry Andric     // edge, just collapse it.
8480b57cec5SDimitry Andric     BasicBlock *SinglePred = BB->getSinglePredecessor();
8490b57cec5SDimitry Andric 
8500b57cec5SDimitry Andric     // Don't merge if BB's address is taken.
851bdd1243dSDimitry Andric     if (!SinglePred || SinglePred == BB || BB->hasAddressTaken())
852bdd1243dSDimitry Andric       continue;
8530b57cec5SDimitry Andric 
854fe013be4SDimitry Andric     // Make an effort to skip unreachable blocks.
855fe013be4SDimitry Andric     if (DT && !DT->isReachableFromEntry(BB))
856fe013be4SDimitry Andric       continue;
857fe013be4SDimitry Andric 
8580b57cec5SDimitry Andric     BranchInst *Term = dyn_cast<BranchInst>(SinglePred->getTerminator());
8590b57cec5SDimitry Andric     if (Term && !Term->isConditional()) {
8600b57cec5SDimitry Andric       Changed = true;
8610b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "To merge:\n" << *BB << "\n\n\n");
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric       // Merge BB into SinglePred and delete it.
864fe013be4SDimitry Andric       MergeBlockIntoPredecessor(BB, /* DTU */ nullptr, LI, /* MSSAU */ nullptr,
865fe013be4SDimitry Andric                                 /* MemDep */ nullptr,
866fe013be4SDimitry Andric                                 /* PredecessorWithTwoSuccessors */ false, DT);
867e8d8bef9SDimitry Andric       Preds.insert(SinglePred);
868bdd1243dSDimitry Andric 
869bdd1243dSDimitry Andric       if (IsHugeFunc) {
870bdd1243dSDimitry Andric         // Update FreshBBs to optimize the merged BB.
871bdd1243dSDimitry Andric         FreshBBs.insert(SinglePred);
872bdd1243dSDimitry Andric         FreshBBs.erase(BB);
873bdd1243dSDimitry Andric       }
8740b57cec5SDimitry Andric     }
8750b57cec5SDimitry Andric   }
876e8d8bef9SDimitry Andric 
877e8d8bef9SDimitry Andric   // (Repeatedly) merging blocks into their predecessors can create redundant
878e8d8bef9SDimitry Andric   // debug intrinsics.
879fcaf7f86SDimitry Andric   for (const auto &Pred : Preds)
880e8d8bef9SDimitry Andric     if (auto *BB = cast_or_null<BasicBlock>(Pred))
881e8d8bef9SDimitry Andric       RemoveRedundantDbgInstrs(BB);
882e8d8bef9SDimitry Andric 
8830b57cec5SDimitry Andric   return Changed;
8840b57cec5SDimitry Andric }
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric /// Find a destination block from BB if BB is mergeable empty block.
findDestBlockOfMergeableEmptyBlock(BasicBlock * BB)8870b57cec5SDimitry Andric BasicBlock *CodeGenPrepare::findDestBlockOfMergeableEmptyBlock(BasicBlock *BB) {
8880b57cec5SDimitry Andric   // If this block doesn't end with an uncond branch, ignore it.
8890b57cec5SDimitry Andric   BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
8900b57cec5SDimitry Andric   if (!BI || !BI->isUnconditional())
8910b57cec5SDimitry Andric     return nullptr;
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   // If the instruction before the branch (skipping debug info) isn't a phi
8940b57cec5SDimitry Andric   // node, then other stuff is happening here.
8950b57cec5SDimitry Andric   BasicBlock::iterator BBI = BI->getIterator();
8960b57cec5SDimitry Andric   if (BBI != BB->begin()) {
8970b57cec5SDimitry Andric     --BBI;
8980b57cec5SDimitry Andric     while (isa<DbgInfoIntrinsic>(BBI)) {
8990b57cec5SDimitry Andric       if (BBI == BB->begin())
9000b57cec5SDimitry Andric         break;
9010b57cec5SDimitry Andric       --BBI;
9020b57cec5SDimitry Andric     }
9030b57cec5SDimitry Andric     if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
9040b57cec5SDimitry Andric       return nullptr;
9050b57cec5SDimitry Andric   }
9060b57cec5SDimitry Andric 
9070b57cec5SDimitry Andric   // Do not break infinite loops.
9080b57cec5SDimitry Andric   BasicBlock *DestBB = BI->getSuccessor(0);
9090b57cec5SDimitry Andric   if (DestBB == BB)
9100b57cec5SDimitry Andric     return nullptr;
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric   if (!canMergeBlocks(BB, DestBB))
9130b57cec5SDimitry Andric     DestBB = nullptr;
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric   return DestBB;
9160b57cec5SDimitry Andric }
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric /// Eliminate blocks that contain only PHI nodes, debug info directives, and an
9190b57cec5SDimitry Andric /// unconditional branch. Passes before isel (e.g. LSR/loopsimplify) often split
9200b57cec5SDimitry Andric /// edges in ways that are non-optimal for isel. Start by eliminating these
9210b57cec5SDimitry Andric /// blocks so we can split them the way we want them.
eliminateMostlyEmptyBlocks(Function & F)9220b57cec5SDimitry Andric bool CodeGenPrepare::eliminateMostlyEmptyBlocks(Function &F) {
9230b57cec5SDimitry Andric   SmallPtrSet<BasicBlock *, 16> Preheaders;
9240b57cec5SDimitry Andric   SmallVector<Loop *, 16> LoopList(LI->begin(), LI->end());
9250b57cec5SDimitry Andric   while (!LoopList.empty()) {
9260b57cec5SDimitry Andric     Loop *L = LoopList.pop_back_val();
927e8d8bef9SDimitry Andric     llvm::append_range(LoopList, *L);
9280b57cec5SDimitry Andric     if (BasicBlock *Preheader = L->getLoopPreheader())
9290b57cec5SDimitry Andric       Preheaders.insert(Preheader);
9300b57cec5SDimitry Andric   }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric   bool MadeChange = false;
9330b57cec5SDimitry Andric   // Copy blocks into a temporary array to avoid iterator invalidation issues
9340b57cec5SDimitry Andric   // as we remove them.
9350b57cec5SDimitry Andric   // Note that this intentionally skips the entry block.
9360b57cec5SDimitry Andric   SmallVector<WeakTrackingVH, 16> Blocks;
937c9157d92SDimitry Andric   for (auto &Block : llvm::drop_begin(F)) {
938c9157d92SDimitry Andric     // Delete phi nodes that could block deleting other empty blocks.
939c9157d92SDimitry Andric     if (!DisableDeletePHIs)
940c9157d92SDimitry Andric       MadeChange |= DeleteDeadPHIs(&Block, TLInfo);
9410b57cec5SDimitry Andric     Blocks.push_back(&Block);
942c9157d92SDimitry Andric   }
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric   for (auto &Block : Blocks) {
9450b57cec5SDimitry Andric     BasicBlock *BB = cast_or_null<BasicBlock>(Block);
9460b57cec5SDimitry Andric     if (!BB)
9470b57cec5SDimitry Andric       continue;
9480b57cec5SDimitry Andric     BasicBlock *DestBB = findDestBlockOfMergeableEmptyBlock(BB);
9490b57cec5SDimitry Andric     if (!DestBB ||
9500b57cec5SDimitry Andric         !isMergingEmptyBlockProfitable(BB, DestBB, Preheaders.count(BB)))
9510b57cec5SDimitry Andric       continue;
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric     eliminateMostlyEmptyBlock(BB);
9540b57cec5SDimitry Andric     MadeChange = true;
9550b57cec5SDimitry Andric   }
9560b57cec5SDimitry Andric   return MadeChange;
9570b57cec5SDimitry Andric }
9580b57cec5SDimitry Andric 
isMergingEmptyBlockProfitable(BasicBlock * BB,BasicBlock * DestBB,bool isPreheader)9590b57cec5SDimitry Andric bool CodeGenPrepare::isMergingEmptyBlockProfitable(BasicBlock *BB,
9600b57cec5SDimitry Andric                                                    BasicBlock *DestBB,
9610b57cec5SDimitry Andric                                                    bool isPreheader) {
9620b57cec5SDimitry Andric   // Do not delete loop preheaders if doing so would create a critical edge.
9630b57cec5SDimitry Andric   // Loop preheaders can be good locations to spill registers. If the
9640b57cec5SDimitry Andric   // preheader is deleted and we create a critical edge, registers may be
9650b57cec5SDimitry Andric   // spilled in the loop body instead.
9660b57cec5SDimitry Andric   if (!DisablePreheaderProtect && isPreheader &&
9670b57cec5SDimitry Andric       !(BB->getSinglePredecessor() &&
9680b57cec5SDimitry Andric         BB->getSinglePredecessor()->getSingleSuccessor()))
9690b57cec5SDimitry Andric     return false;
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric   // Skip merging if the block's successor is also a successor to any callbr
9720b57cec5SDimitry Andric   // that leads to this block.
9730b57cec5SDimitry Andric   // FIXME: Is this really needed? Is this a correctness issue?
974fe6060f1SDimitry Andric   for (BasicBlock *Pred : predecessors(BB)) {
975fe6060f1SDimitry Andric     if (auto *CBI = dyn_cast<CallBrInst>((Pred)->getTerminator()))
9760b57cec5SDimitry Andric       for (unsigned i = 0, e = CBI->getNumSuccessors(); i != e; ++i)
9770b57cec5SDimitry Andric         if (DestBB == CBI->getSuccessor(i))
9780b57cec5SDimitry Andric           return false;
9790b57cec5SDimitry Andric   }
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   // Try to skip merging if the unique predecessor of BB is terminated by a
9820b57cec5SDimitry Andric   // switch or indirect branch instruction, and BB is used as an incoming block
9830b57cec5SDimitry Andric   // of PHIs in DestBB. In such case, merging BB and DestBB would cause ISel to
9840b57cec5SDimitry Andric   // add COPY instructions in the predecessor of BB instead of BB (if it is not
9850b57cec5SDimitry Andric   // merged). Note that the critical edge created by merging such blocks wont be
9860b57cec5SDimitry Andric   // split in MachineSink because the jump table is not analyzable. By keeping
9870b57cec5SDimitry Andric   // such empty block (BB), ISel will place COPY instructions in BB, not in the
9880b57cec5SDimitry Andric   // predecessor of BB.
9890b57cec5SDimitry Andric   BasicBlock *Pred = BB->getUniquePredecessor();
990bdd1243dSDimitry Andric   if (!Pred || !(isa<SwitchInst>(Pred->getTerminator()) ||
9910b57cec5SDimitry Andric                  isa<IndirectBrInst>(Pred->getTerminator())))
9920b57cec5SDimitry Andric     return true;
9930b57cec5SDimitry Andric 
9940b57cec5SDimitry Andric   if (BB->getTerminator() != BB->getFirstNonPHIOrDbg())
9950b57cec5SDimitry Andric     return true;
9960b57cec5SDimitry Andric 
9970b57cec5SDimitry Andric   // We use a simple cost heuristic which determine skipping merging is
9980b57cec5SDimitry Andric   // profitable if the cost of skipping merging is less than the cost of
9990b57cec5SDimitry Andric   // merging : Cost(skipping merging) < Cost(merging BB), where the
10000b57cec5SDimitry Andric   // Cost(skipping merging) is Freq(BB) * (Cost(Copy) + Cost(Branch)), and
10010b57cec5SDimitry Andric   // the Cost(merging BB) is Freq(Pred) * Cost(Copy).
10020b57cec5SDimitry Andric   // Assuming Cost(Copy) == Cost(Branch), we could simplify it to :
10030b57cec5SDimitry Andric   //   Freq(Pred) / Freq(BB) > 2.
10040b57cec5SDimitry Andric   // Note that if there are multiple empty blocks sharing the same incoming
10050b57cec5SDimitry Andric   // value for the PHIs in the DestBB, we consider them together. In such
10060b57cec5SDimitry Andric   // case, Cost(merging BB) will be the sum of their frequencies.
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   if (!isa<PHINode>(DestBB->begin()))
10090b57cec5SDimitry Andric     return true;
10100b57cec5SDimitry Andric 
10110b57cec5SDimitry Andric   SmallPtrSet<BasicBlock *, 16> SameIncomingValueBBs;
10120b57cec5SDimitry Andric 
10130b57cec5SDimitry Andric   // Find all other incoming blocks from which incoming values of all PHIs in
10140b57cec5SDimitry Andric   // DestBB are the same as the ones from BB.
1015fe6060f1SDimitry Andric   for (BasicBlock *DestBBPred : predecessors(DestBB)) {
10160b57cec5SDimitry Andric     if (DestBBPred == BB)
10170b57cec5SDimitry Andric       continue;
10180b57cec5SDimitry Andric 
10190b57cec5SDimitry Andric     if (llvm::all_of(DestBB->phis(), [&](const PHINode &DestPN) {
10200b57cec5SDimitry Andric           return DestPN.getIncomingValueForBlock(BB) ==
10210b57cec5SDimitry Andric                  DestPN.getIncomingValueForBlock(DestBBPred);
10220b57cec5SDimitry Andric         }))
10230b57cec5SDimitry Andric       SameIncomingValueBBs.insert(DestBBPred);
10240b57cec5SDimitry Andric   }
10250b57cec5SDimitry Andric 
10260b57cec5SDimitry Andric   // See if all BB's incoming values are same as the value from Pred. In this
10270b57cec5SDimitry Andric   // case, no reason to skip merging because COPYs are expected to be place in
10280b57cec5SDimitry Andric   // Pred already.
10290b57cec5SDimitry Andric   if (SameIncomingValueBBs.count(Pred))
10300b57cec5SDimitry Andric     return true;
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric   BlockFrequency PredFreq = BFI->getBlockFreq(Pred);
10330b57cec5SDimitry Andric   BlockFrequency BBFreq = BFI->getBlockFreq(BB);
10340b57cec5SDimitry Andric 
10355ffd83dbSDimitry Andric   for (auto *SameValueBB : SameIncomingValueBBs)
10360b57cec5SDimitry Andric     if (SameValueBB->getUniquePredecessor() == Pred &&
10370b57cec5SDimitry Andric         DestBB == findDestBlockOfMergeableEmptyBlock(SameValueBB))
10380b57cec5SDimitry Andric       BBFreq += BFI->getBlockFreq(SameValueBB);
10390b57cec5SDimitry Andric 
1040c9157d92SDimitry Andric   std::optional<BlockFrequency> Limit = BBFreq.mul(FreqRatioToSkipMerge);
1041c9157d92SDimitry Andric   return !Limit || PredFreq <= *Limit;
10420b57cec5SDimitry Andric }
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric /// Return true if we can merge BB into DestBB if there is a single
10450b57cec5SDimitry Andric /// unconditional branch between them, and BB contains no other non-phi
10460b57cec5SDimitry Andric /// instructions.
canMergeBlocks(const BasicBlock * BB,const BasicBlock * DestBB) const10470b57cec5SDimitry Andric bool CodeGenPrepare::canMergeBlocks(const BasicBlock *BB,
10480b57cec5SDimitry Andric                                     const BasicBlock *DestBB) const {
10490b57cec5SDimitry Andric   // We only want to eliminate blocks whose phi nodes are used by phi nodes in
10500b57cec5SDimitry Andric   // the successor.  If there are more complex condition (e.g. preheaders),
10510b57cec5SDimitry Andric   // don't mess around with them.
10520b57cec5SDimitry Andric   for (const PHINode &PN : BB->phis()) {
10530b57cec5SDimitry Andric     for (const User *U : PN.users()) {
10540b57cec5SDimitry Andric       const Instruction *UI = cast<Instruction>(U);
10550b57cec5SDimitry Andric       if (UI->getParent() != DestBB || !isa<PHINode>(UI))
10560b57cec5SDimitry Andric         return false;
10570b57cec5SDimitry Andric       // If User is inside DestBB block and it is a PHINode then check
10580b57cec5SDimitry Andric       // incoming value. If incoming value is not from BB then this is
10590b57cec5SDimitry Andric       // a complex condition (e.g. preheaders) we want to avoid here.
10600b57cec5SDimitry Andric       if (UI->getParent() == DestBB) {
10610b57cec5SDimitry Andric         if (const PHINode *UPN = dyn_cast<PHINode>(UI))
10620b57cec5SDimitry Andric           for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
10630b57cec5SDimitry Andric             Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
10640b57cec5SDimitry Andric             if (Insn && Insn->getParent() == BB &&
10650b57cec5SDimitry Andric                 Insn->getParent() != UPN->getIncomingBlock(I))
10660b57cec5SDimitry Andric               return false;
10670b57cec5SDimitry Andric           }
10680b57cec5SDimitry Andric       }
10690b57cec5SDimitry Andric     }
10700b57cec5SDimitry Andric   }
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric   // If BB and DestBB contain any common predecessors, then the phi nodes in BB
10730b57cec5SDimitry Andric   // and DestBB may have conflicting incoming values for the block.  If so, we
10740b57cec5SDimitry Andric   // can't merge the block.
10750b57cec5SDimitry Andric   const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
1076bdd1243dSDimitry Andric   if (!DestBBPN)
1077bdd1243dSDimitry Andric     return true; // no conflict.
10780b57cec5SDimitry Andric 
10790b57cec5SDimitry Andric   // Collect the preds of BB.
10800b57cec5SDimitry Andric   SmallPtrSet<const BasicBlock *, 16> BBPreds;
10810b57cec5SDimitry Andric   if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
10820b57cec5SDimitry Andric     // It is faster to get preds from a PHI than with pred_iterator.
10830b57cec5SDimitry Andric     for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
10840b57cec5SDimitry Andric       BBPreds.insert(BBPN->getIncomingBlock(i));
10850b57cec5SDimitry Andric   } else {
10860b57cec5SDimitry Andric     BBPreds.insert(pred_begin(BB), pred_end(BB));
10870b57cec5SDimitry Andric   }
10880b57cec5SDimitry Andric 
10890b57cec5SDimitry Andric   // Walk the preds of DestBB.
10900b57cec5SDimitry Andric   for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
10910b57cec5SDimitry Andric     BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
10920b57cec5SDimitry Andric     if (BBPreds.count(Pred)) { // Common predecessor?
10930b57cec5SDimitry Andric       for (const PHINode &PN : DestBB->phis()) {
10940b57cec5SDimitry Andric         const Value *V1 = PN.getIncomingValueForBlock(Pred);
10950b57cec5SDimitry Andric         const Value *V2 = PN.getIncomingValueForBlock(BB);
10960b57cec5SDimitry Andric 
10970b57cec5SDimitry Andric         // If V2 is a phi node in BB, look up what the mapped value will be.
10980b57cec5SDimitry Andric         if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
10990b57cec5SDimitry Andric           if (V2PN->getParent() == BB)
11000b57cec5SDimitry Andric             V2 = V2PN->getIncomingValueForBlock(Pred);
11010b57cec5SDimitry Andric 
11020b57cec5SDimitry Andric         // If there is a conflict, bail out.
1103bdd1243dSDimitry Andric         if (V1 != V2)
1104bdd1243dSDimitry Andric           return false;
11050b57cec5SDimitry Andric       }
11060b57cec5SDimitry Andric     }
11070b57cec5SDimitry Andric   }
11080b57cec5SDimitry Andric 
11090b57cec5SDimitry Andric   return true;
11100b57cec5SDimitry Andric }
11110b57cec5SDimitry Andric 
1112bdd1243dSDimitry Andric /// Replace all old uses with new ones, and push the updated BBs into FreshBBs.
replaceAllUsesWith(Value * Old,Value * New,SmallSet<BasicBlock *,32> & FreshBBs,bool IsHuge)1113bdd1243dSDimitry Andric static void replaceAllUsesWith(Value *Old, Value *New,
1114bdd1243dSDimitry Andric                                SmallSet<BasicBlock *, 32> &FreshBBs,
1115bdd1243dSDimitry Andric                                bool IsHuge) {
1116bdd1243dSDimitry Andric   auto *OldI = dyn_cast<Instruction>(Old);
1117bdd1243dSDimitry Andric   if (OldI) {
1118bdd1243dSDimitry Andric     for (Value::user_iterator UI = OldI->user_begin(), E = OldI->user_end();
1119bdd1243dSDimitry Andric          UI != E; ++UI) {
1120bdd1243dSDimitry Andric       Instruction *User = cast<Instruction>(*UI);
1121bdd1243dSDimitry Andric       if (IsHuge)
1122bdd1243dSDimitry Andric         FreshBBs.insert(User->getParent());
1123bdd1243dSDimitry Andric     }
1124bdd1243dSDimitry Andric   }
1125bdd1243dSDimitry Andric   Old->replaceAllUsesWith(New);
1126bdd1243dSDimitry Andric }
1127bdd1243dSDimitry Andric 
11280b57cec5SDimitry Andric /// Eliminate a basic block that has only phi's and an unconditional branch in
11290b57cec5SDimitry Andric /// it.
eliminateMostlyEmptyBlock(BasicBlock * BB)11300b57cec5SDimitry Andric void CodeGenPrepare::eliminateMostlyEmptyBlock(BasicBlock *BB) {
11310b57cec5SDimitry Andric   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
11320b57cec5SDimitry Andric   BasicBlock *DestBB = BI->getSuccessor(0);
11330b57cec5SDimitry Andric 
11340b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n"
11350b57cec5SDimitry Andric                     << *BB << *DestBB);
11360b57cec5SDimitry Andric 
11370b57cec5SDimitry Andric   // If the destination block has a single pred, then this is a trivial edge,
11380b57cec5SDimitry Andric   // just collapse it.
11390b57cec5SDimitry Andric   if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
11400b57cec5SDimitry Andric     if (SinglePred != DestBB) {
11410b57cec5SDimitry Andric       assert(SinglePred == BB &&
11420b57cec5SDimitry Andric              "Single predecessor not the same as predecessor");
11430b57cec5SDimitry Andric       // Merge DestBB into SinglePred/BB and delete it.
11440b57cec5SDimitry Andric       MergeBlockIntoPredecessor(DestBB);
11450b57cec5SDimitry Andric       // Note: BB(=SinglePred) will not be deleted on this path.
11460b57cec5SDimitry Andric       // DestBB(=its single successor) is the one that was deleted.
11470b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "AFTER:\n" << *SinglePred << "\n\n\n");
1148bdd1243dSDimitry Andric 
1149bdd1243dSDimitry Andric       if (IsHugeFunc) {
1150bdd1243dSDimitry Andric         // Update FreshBBs to optimize the merged BB.
1151bdd1243dSDimitry Andric         FreshBBs.insert(SinglePred);
1152bdd1243dSDimitry Andric         FreshBBs.erase(DestBB);
1153bdd1243dSDimitry Andric       }
11540b57cec5SDimitry Andric       return;
11550b57cec5SDimitry Andric     }
11560b57cec5SDimitry Andric   }
11570b57cec5SDimitry Andric 
11580b57cec5SDimitry Andric   // Otherwise, we have multiple predecessors of BB.  Update the PHIs in DestBB
11590b57cec5SDimitry Andric   // to handle the new incoming edges it is about to have.
11600b57cec5SDimitry Andric   for (PHINode &PN : DestBB->phis()) {
11610b57cec5SDimitry Andric     // Remove the incoming value for BB, and remember it.
11620b57cec5SDimitry Andric     Value *InVal = PN.removeIncomingValue(BB, false);
11630b57cec5SDimitry Andric 
11640b57cec5SDimitry Andric     // Two options: either the InVal is a phi node defined in BB or it is some
11650b57cec5SDimitry Andric     // value that dominates BB.
11660b57cec5SDimitry Andric     PHINode *InValPhi = dyn_cast<PHINode>(InVal);
11670b57cec5SDimitry Andric     if (InValPhi && InValPhi->getParent() == BB) {
11680b57cec5SDimitry Andric       // Add all of the input values of the input PHI as inputs of this phi.
11690b57cec5SDimitry Andric       for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
11700b57cec5SDimitry Andric         PN.addIncoming(InValPhi->getIncomingValue(i),
11710b57cec5SDimitry Andric                        InValPhi->getIncomingBlock(i));
11720b57cec5SDimitry Andric     } else {
11730b57cec5SDimitry Andric       // Otherwise, add one instance of the dominating value for each edge that
11740b57cec5SDimitry Andric       // we will be adding.
11750b57cec5SDimitry Andric       if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
11760b57cec5SDimitry Andric         for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
11770b57cec5SDimitry Andric           PN.addIncoming(InVal, BBPN->getIncomingBlock(i));
11780b57cec5SDimitry Andric       } else {
1179fe6060f1SDimitry Andric         for (BasicBlock *Pred : predecessors(BB))
1180fe6060f1SDimitry Andric           PN.addIncoming(InVal, Pred);
11810b57cec5SDimitry Andric       }
11820b57cec5SDimitry Andric     }
11830b57cec5SDimitry Andric   }
11840b57cec5SDimitry Andric 
11850b57cec5SDimitry Andric   // The PHIs are now updated, change everything that refers to BB to use
11860b57cec5SDimitry Andric   // DestBB and remove BB.
11870b57cec5SDimitry Andric   BB->replaceAllUsesWith(DestBB);
11880b57cec5SDimitry Andric   BB->eraseFromParent();
11890b57cec5SDimitry Andric   ++NumBlocksElim;
11900b57cec5SDimitry Andric 
11910b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
11920b57cec5SDimitry Andric }
11930b57cec5SDimitry Andric 
11940b57cec5SDimitry Andric // Computes a map of base pointer relocation instructions to corresponding
11950b57cec5SDimitry Andric // derived pointer relocation instructions given a vector of all relocate calls
computeBaseDerivedRelocateMap(const SmallVectorImpl<GCRelocateInst * > & AllRelocateCalls,DenseMap<GCRelocateInst *,SmallVector<GCRelocateInst *,2>> & RelocateInstMap)11960b57cec5SDimitry Andric static void computeBaseDerivedRelocateMap(
11970b57cec5SDimitry Andric     const SmallVectorImpl<GCRelocateInst *> &AllRelocateCalls,
11980b57cec5SDimitry Andric     DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>>
11990b57cec5SDimitry Andric         &RelocateInstMap) {
12000b57cec5SDimitry Andric   // Collect information in two maps: one primarily for locating the base object
12010b57cec5SDimitry Andric   // while filling the second map; the second map is the final structure holding
12020b57cec5SDimitry Andric   // a mapping between Base and corresponding Derived relocate calls
12030b57cec5SDimitry Andric   DenseMap<std::pair<unsigned, unsigned>, GCRelocateInst *> RelocateIdxMap;
12040b57cec5SDimitry Andric   for (auto *ThisRelocate : AllRelocateCalls) {
12050b57cec5SDimitry Andric     auto K = std::make_pair(ThisRelocate->getBasePtrIndex(),
12060b57cec5SDimitry Andric                             ThisRelocate->getDerivedPtrIndex());
12070b57cec5SDimitry Andric     RelocateIdxMap.insert(std::make_pair(K, ThisRelocate));
12080b57cec5SDimitry Andric   }
12090b57cec5SDimitry Andric   for (auto &Item : RelocateIdxMap) {
12100b57cec5SDimitry Andric     std::pair<unsigned, unsigned> Key = Item.first;
12110b57cec5SDimitry Andric     if (Key.first == Key.second)
12120b57cec5SDimitry Andric       // Base relocation: nothing to insert
12130b57cec5SDimitry Andric       continue;
12140b57cec5SDimitry Andric 
12150b57cec5SDimitry Andric     GCRelocateInst *I = Item.second;
12160b57cec5SDimitry Andric     auto BaseKey = std::make_pair(Key.first, Key.first);
12170b57cec5SDimitry Andric 
12180b57cec5SDimitry Andric     // We're iterating over RelocateIdxMap so we cannot modify it.
12190b57cec5SDimitry Andric     auto MaybeBase = RelocateIdxMap.find(BaseKey);
12200b57cec5SDimitry Andric     if (MaybeBase == RelocateIdxMap.end())
12210b57cec5SDimitry Andric       // TODO: We might want to insert a new base object relocate and gep off
12220b57cec5SDimitry Andric       // that, if there are enough derived object relocates.
12230b57cec5SDimitry Andric       continue;
12240b57cec5SDimitry Andric 
12250b57cec5SDimitry Andric     RelocateInstMap[MaybeBase->second].push_back(I);
12260b57cec5SDimitry Andric   }
12270b57cec5SDimitry Andric }
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric // Accepts a GEP and extracts the operands into a vector provided they're all
12300b57cec5SDimitry Andric // small integer constants
getGEPSmallConstantIntOffsetV(GetElementPtrInst * GEP,SmallVectorImpl<Value * > & OffsetV)12310b57cec5SDimitry Andric static bool getGEPSmallConstantIntOffsetV(GetElementPtrInst *GEP,
12320b57cec5SDimitry Andric                                           SmallVectorImpl<Value *> &OffsetV) {
12330b57cec5SDimitry Andric   for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
12340b57cec5SDimitry Andric     // Only accept small constant integer operands
12355ffd83dbSDimitry Andric     auto *Op = dyn_cast<ConstantInt>(GEP->getOperand(i));
12360b57cec5SDimitry Andric     if (!Op || Op->getZExtValue() > 20)
12370b57cec5SDimitry Andric       return false;
12380b57cec5SDimitry Andric   }
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric   for (unsigned i = 1; i < GEP->getNumOperands(); i++)
12410b57cec5SDimitry Andric     OffsetV.push_back(GEP->getOperand(i));
12420b57cec5SDimitry Andric   return true;
12430b57cec5SDimitry Andric }
12440b57cec5SDimitry Andric 
12450b57cec5SDimitry Andric // Takes a RelocatedBase (base pointer relocation instruction) and Targets to
12460b57cec5SDimitry Andric // replace, computes a replacement, and affects it.
12470b57cec5SDimitry Andric static bool
simplifyRelocatesOffABase(GCRelocateInst * RelocatedBase,const SmallVectorImpl<GCRelocateInst * > & Targets)12480b57cec5SDimitry Andric simplifyRelocatesOffABase(GCRelocateInst *RelocatedBase,
12490b57cec5SDimitry Andric                           const SmallVectorImpl<GCRelocateInst *> &Targets) {
12500b57cec5SDimitry Andric   bool MadeChange = false;
12510b57cec5SDimitry Andric   // We must ensure the relocation of derived pointer is defined after
12520b57cec5SDimitry Andric   // relocation of base pointer. If we find a relocation corresponding to base
12530b57cec5SDimitry Andric   // defined earlier than relocation of base then we move relocation of base
12540b57cec5SDimitry Andric   // right before found relocation. We consider only relocation in the same
12550b57cec5SDimitry Andric   // basic block as relocation of base. Relocations from other basic block will
12560b57cec5SDimitry Andric   // be skipped by optimization and we do not care about them.
12570b57cec5SDimitry Andric   for (auto R = RelocatedBase->getParent()->getFirstInsertionPt();
12580b57cec5SDimitry Andric        &*R != RelocatedBase; ++R)
12595ffd83dbSDimitry Andric     if (auto *RI = dyn_cast<GCRelocateInst>(R))
12600b57cec5SDimitry Andric       if (RI->getStatepoint() == RelocatedBase->getStatepoint())
12610b57cec5SDimitry Andric         if (RI->getBasePtrIndex() == RelocatedBase->getBasePtrIndex()) {
12620b57cec5SDimitry Andric           RelocatedBase->moveBefore(RI);
1263c9157d92SDimitry Andric           MadeChange = true;
12640b57cec5SDimitry Andric           break;
12650b57cec5SDimitry Andric         }
12660b57cec5SDimitry Andric 
12670b57cec5SDimitry Andric   for (GCRelocateInst *ToReplace : Targets) {
12680b57cec5SDimitry Andric     assert(ToReplace->getBasePtrIndex() == RelocatedBase->getBasePtrIndex() &&
12690b57cec5SDimitry Andric            "Not relocating a derived object of the original base object");
12700b57cec5SDimitry Andric     if (ToReplace->getBasePtrIndex() == ToReplace->getDerivedPtrIndex()) {
12710b57cec5SDimitry Andric       // A duplicate relocate call. TODO: coalesce duplicates.
12720b57cec5SDimitry Andric       continue;
12730b57cec5SDimitry Andric     }
12740b57cec5SDimitry Andric 
12750b57cec5SDimitry Andric     if (RelocatedBase->getParent() != ToReplace->getParent()) {
12760b57cec5SDimitry Andric       // Base and derived relocates are in different basic blocks.
12770b57cec5SDimitry Andric       // In this case transform is only valid when base dominates derived
12780b57cec5SDimitry Andric       // relocate. However it would be too expensive to check dominance
12790b57cec5SDimitry Andric       // for each such relocate, so we skip the whole transformation.
12800b57cec5SDimitry Andric       continue;
12810b57cec5SDimitry Andric     }
12820b57cec5SDimitry Andric 
12830b57cec5SDimitry Andric     Value *Base = ToReplace->getBasePtr();
12845ffd83dbSDimitry Andric     auto *Derived = dyn_cast<GetElementPtrInst>(ToReplace->getDerivedPtr());
12850b57cec5SDimitry Andric     if (!Derived || Derived->getPointerOperand() != Base)
12860b57cec5SDimitry Andric       continue;
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric     SmallVector<Value *, 2> OffsetV;
12890b57cec5SDimitry Andric     if (!getGEPSmallConstantIntOffsetV(Derived, OffsetV))
12900b57cec5SDimitry Andric       continue;
12910b57cec5SDimitry Andric 
12920b57cec5SDimitry Andric     // Create a Builder and replace the target callsite with a gep
12930b57cec5SDimitry Andric     assert(RelocatedBase->getNextNode() &&
12940b57cec5SDimitry Andric            "Should always have one since it's not a terminator");
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric     // Insert after RelocatedBase
12970b57cec5SDimitry Andric     IRBuilder<> Builder(RelocatedBase->getNextNode());
12980b57cec5SDimitry Andric     Builder.SetCurrentDebugLocation(ToReplace->getDebugLoc());
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric     // If gc_relocate does not match the actual type, cast it to the right type.
13010b57cec5SDimitry Andric     // In theory, there must be a bitcast after gc_relocate if the type does not
13020b57cec5SDimitry Andric     // match, and we should reuse it to get the derived pointer. But it could be
13030b57cec5SDimitry Andric     // cases like this:
13040b57cec5SDimitry Andric     // bb1:
13050b57cec5SDimitry Andric     //  ...
1306bdd1243dSDimitry Andric     //  %g1 = call coldcc i8 addrspace(1)*
1307bdd1243dSDimitry Andric     //  @llvm.experimental.gc.relocate.p1i8(...) br label %merge
13080b57cec5SDimitry Andric     //
13090b57cec5SDimitry Andric     // bb2:
13100b57cec5SDimitry Andric     //  ...
1311bdd1243dSDimitry Andric     //  %g2 = call coldcc i8 addrspace(1)*
1312bdd1243dSDimitry Andric     //  @llvm.experimental.gc.relocate.p1i8(...) br label %merge
13130b57cec5SDimitry Andric     //
13140b57cec5SDimitry Andric     // merge:
13150b57cec5SDimitry Andric     //  %p1 = phi i8 addrspace(1)* [ %g1, %bb1 ], [ %g2, %bb2 ]
13160b57cec5SDimitry Andric     //  %cast = bitcast i8 addrspace(1)* %p1 in to i32 addrspace(1)*
13170b57cec5SDimitry Andric     //
1318bdd1243dSDimitry Andric     // In this case, we can not find the bitcast any more. So we insert a new
1319bdd1243dSDimitry Andric     // bitcast no matter there is already one or not. In this way, we can handle
1320bdd1243dSDimitry Andric     // all cases, and the extra bitcast should be optimized away in later
1321bdd1243dSDimitry Andric     // passes.
13220b57cec5SDimitry Andric     Value *ActualRelocatedBase = RelocatedBase;
13230b57cec5SDimitry Andric     if (RelocatedBase->getType() != Base->getType()) {
13240b57cec5SDimitry Andric       ActualRelocatedBase =
13250b57cec5SDimitry Andric           Builder.CreateBitCast(RelocatedBase, Base->getType());
13260b57cec5SDimitry Andric     }
1327bdd1243dSDimitry Andric     Value *Replacement =
1328bdd1243dSDimitry Andric         Builder.CreateGEP(Derived->getSourceElementType(), ActualRelocatedBase,
1329bdd1243dSDimitry Andric                           ArrayRef(OffsetV));
13300b57cec5SDimitry Andric     Replacement->takeName(ToReplace);
1331bdd1243dSDimitry Andric     // If the newly generated derived pointer's type does not match the original
1332bdd1243dSDimitry Andric     // derived pointer's type, cast the new derived pointer to match it. Same
1333bdd1243dSDimitry Andric     // reasoning as above.
13340b57cec5SDimitry Andric     Value *ActualReplacement = Replacement;
13350b57cec5SDimitry Andric     if (Replacement->getType() != ToReplace->getType()) {
13360b57cec5SDimitry Andric       ActualReplacement =
13370b57cec5SDimitry Andric           Builder.CreateBitCast(Replacement, ToReplace->getType());
13380b57cec5SDimitry Andric     }
13390b57cec5SDimitry Andric     ToReplace->replaceAllUsesWith(ActualReplacement);
13400b57cec5SDimitry Andric     ToReplace->eraseFromParent();
13410b57cec5SDimitry Andric 
13420b57cec5SDimitry Andric     MadeChange = true;
13430b57cec5SDimitry Andric   }
13440b57cec5SDimitry Andric   return MadeChange;
13450b57cec5SDimitry Andric }
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric // Turns this:
13480b57cec5SDimitry Andric //
13490b57cec5SDimitry Andric // %base = ...
13500b57cec5SDimitry Andric // %ptr = gep %base + 15
13510b57cec5SDimitry Andric // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
13520b57cec5SDimitry Andric // %base' = relocate(%tok, i32 4, i32 4)
13530b57cec5SDimitry Andric // %ptr' = relocate(%tok, i32 4, i32 5)
13540b57cec5SDimitry Andric // %val = load %ptr'
13550b57cec5SDimitry Andric //
13560b57cec5SDimitry Andric // into this:
13570b57cec5SDimitry Andric //
13580b57cec5SDimitry Andric // %base = ...
13590b57cec5SDimitry Andric // %ptr = gep %base + 15
13600b57cec5SDimitry Andric // %tok = statepoint (%fun, i32 0, i32 0, i32 0, %base, %ptr)
13610b57cec5SDimitry Andric // %base' = gc.relocate(%tok, i32 4, i32 4)
13620b57cec5SDimitry Andric // %ptr' = gep %base' + 15
13630b57cec5SDimitry Andric // %val = load %ptr'
simplifyOffsetableRelocate(GCStatepointInst & I)13645ffd83dbSDimitry Andric bool CodeGenPrepare::simplifyOffsetableRelocate(GCStatepointInst &I) {
13650b57cec5SDimitry Andric   bool MadeChange = false;
13660b57cec5SDimitry Andric   SmallVector<GCRelocateInst *, 2> AllRelocateCalls;
13670b57cec5SDimitry Andric   for (auto *U : I.users())
13680b57cec5SDimitry Andric     if (GCRelocateInst *Relocate = dyn_cast<GCRelocateInst>(U))
13690b57cec5SDimitry Andric       // Collect all the relocate calls associated with a statepoint
13700b57cec5SDimitry Andric       AllRelocateCalls.push_back(Relocate);
13710b57cec5SDimitry Andric 
13720b57cec5SDimitry Andric   // We need at least one base pointer relocation + one derived pointer
13730b57cec5SDimitry Andric   // relocation to mangle
13740b57cec5SDimitry Andric   if (AllRelocateCalls.size() < 2)
13750b57cec5SDimitry Andric     return false;
13760b57cec5SDimitry Andric 
13770b57cec5SDimitry Andric   // RelocateInstMap is a mapping from the base relocate instruction to the
13780b57cec5SDimitry Andric   // corresponding derived relocate instructions
13790b57cec5SDimitry Andric   DenseMap<GCRelocateInst *, SmallVector<GCRelocateInst *, 2>> RelocateInstMap;
13800b57cec5SDimitry Andric   computeBaseDerivedRelocateMap(AllRelocateCalls, RelocateInstMap);
13810b57cec5SDimitry Andric   if (RelocateInstMap.empty())
13820b57cec5SDimitry Andric     return false;
13830b57cec5SDimitry Andric 
13840b57cec5SDimitry Andric   for (auto &Item : RelocateInstMap)
13850b57cec5SDimitry Andric     // Item.first is the RelocatedBase to offset against
13860b57cec5SDimitry Andric     // Item.second is the vector of Targets to replace
13870b57cec5SDimitry Andric     MadeChange = simplifyRelocatesOffABase(Item.first, Item.second);
13880b57cec5SDimitry Andric   return MadeChange;
13890b57cec5SDimitry Andric }
13900b57cec5SDimitry Andric 
13910b57cec5SDimitry Andric /// Sink the specified cast instruction into its user blocks.
SinkCast(CastInst * CI)13920b57cec5SDimitry Andric static bool SinkCast(CastInst *CI) {
13930b57cec5SDimitry Andric   BasicBlock *DefBB = CI->getParent();
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric   /// InsertedCasts - Only insert a cast in each block once.
13960b57cec5SDimitry Andric   DenseMap<BasicBlock *, CastInst *> InsertedCasts;
13970b57cec5SDimitry Andric 
13980b57cec5SDimitry Andric   bool MadeChange = false;
13990b57cec5SDimitry Andric   for (Value::user_iterator UI = CI->user_begin(), E = CI->user_end();
14000b57cec5SDimitry Andric        UI != E;) {
14010b57cec5SDimitry Andric     Use &TheUse = UI.getUse();
14020b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(*UI);
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric     // Figure out which BB this cast is used in.  For PHI's this is the
14050b57cec5SDimitry Andric     // appropriate predecessor block.
14060b57cec5SDimitry Andric     BasicBlock *UserBB = User->getParent();
14070b57cec5SDimitry Andric     if (PHINode *PN = dyn_cast<PHINode>(User)) {
14080b57cec5SDimitry Andric       UserBB = PN->getIncomingBlock(TheUse);
14090b57cec5SDimitry Andric     }
14100b57cec5SDimitry Andric 
14110b57cec5SDimitry Andric     // Preincrement use iterator so we don't invalidate it.
14120b57cec5SDimitry Andric     ++UI;
14130b57cec5SDimitry Andric 
14140b57cec5SDimitry Andric     // The first insertion point of a block containing an EH pad is after the
14150b57cec5SDimitry Andric     // pad.  If the pad is the user, we cannot sink the cast past the pad.
14160b57cec5SDimitry Andric     if (User->isEHPad())
14170b57cec5SDimitry Andric       continue;
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric     // If the block selected to receive the cast is an EH pad that does not
14200b57cec5SDimitry Andric     // allow non-PHI instructions before the terminator, we can't sink the
14210b57cec5SDimitry Andric     // cast.
14220b57cec5SDimitry Andric     if (UserBB->getTerminator()->isEHPad())
14230b57cec5SDimitry Andric       continue;
14240b57cec5SDimitry Andric 
14250b57cec5SDimitry Andric     // If this user is in the same block as the cast, don't change the cast.
1426bdd1243dSDimitry Andric     if (UserBB == DefBB)
1427bdd1243dSDimitry Andric       continue;
14280b57cec5SDimitry Andric 
14290b57cec5SDimitry Andric     // If we have already inserted a cast into this block, use it.
14300b57cec5SDimitry Andric     CastInst *&InsertedCast = InsertedCasts[UserBB];
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric     if (!InsertedCast) {
14330b57cec5SDimitry Andric       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
14340b57cec5SDimitry Andric       assert(InsertPt != UserBB->end());
14350b57cec5SDimitry Andric       InsertedCast = CastInst::Create(CI->getOpcode(), CI->getOperand(0),
1436c9157d92SDimitry Andric                                       CI->getType(), "");
1437c9157d92SDimitry Andric       InsertedCast->insertBefore(*UserBB, InsertPt);
14380b57cec5SDimitry Andric       InsertedCast->setDebugLoc(CI->getDebugLoc());
14390b57cec5SDimitry Andric     }
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric     // Replace a use of the cast with a use of the new cast.
14420b57cec5SDimitry Andric     TheUse = InsertedCast;
14430b57cec5SDimitry Andric     MadeChange = true;
14440b57cec5SDimitry Andric     ++NumCastUses;
14450b57cec5SDimitry Andric   }
14460b57cec5SDimitry Andric 
14470b57cec5SDimitry Andric   // If we removed all uses, nuke the cast.
14480b57cec5SDimitry Andric   if (CI->use_empty()) {
14490b57cec5SDimitry Andric     salvageDebugInfo(*CI);
14500b57cec5SDimitry Andric     CI->eraseFromParent();
14510b57cec5SDimitry Andric     MadeChange = true;
14520b57cec5SDimitry Andric   }
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric   return MadeChange;
14550b57cec5SDimitry Andric }
14560b57cec5SDimitry Andric 
14570b57cec5SDimitry Andric /// If the specified cast instruction is a noop copy (e.g. it's casting from
14580b57cec5SDimitry Andric /// one pointer type to another, i32->i8 on PPC), sink it into user blocks to
14590b57cec5SDimitry Andric /// reduce the number of virtual registers that must be created and coalesced.
14600b57cec5SDimitry Andric ///
14610b57cec5SDimitry Andric /// Return true if any changes are made.
OptimizeNoopCopyExpression(CastInst * CI,const TargetLowering & TLI,const DataLayout & DL)14620b57cec5SDimitry Andric static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI,
14630b57cec5SDimitry Andric                                        const DataLayout &DL) {
14640b57cec5SDimitry Andric   // Sink only "cheap" (or nop) address-space casts.  This is a weaker condition
14650b57cec5SDimitry Andric   // than sinking only nop casts, but is helpful on some platforms.
14660b57cec5SDimitry Andric   if (auto *ASC = dyn_cast<AddrSpaceCastInst>(CI)) {
14670b57cec5SDimitry Andric     if (!TLI.isFreeAddrSpaceCast(ASC->getSrcAddressSpace(),
14680b57cec5SDimitry Andric                                  ASC->getDestAddressSpace()))
14690b57cec5SDimitry Andric       return false;
14700b57cec5SDimitry Andric   }
14710b57cec5SDimitry Andric 
14720b57cec5SDimitry Andric   // If this is a noop copy,
14730b57cec5SDimitry Andric   EVT SrcVT = TLI.getValueType(DL, CI->getOperand(0)->getType());
14740b57cec5SDimitry Andric   EVT DstVT = TLI.getValueType(DL, CI->getType());
14750b57cec5SDimitry Andric 
14760b57cec5SDimitry Andric   // This is an fp<->int conversion?
14770b57cec5SDimitry Andric   if (SrcVT.isInteger() != DstVT.isInteger())
14780b57cec5SDimitry Andric     return false;
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric   // If this is an extension, it will be a zero or sign extension, which
14810b57cec5SDimitry Andric   // isn't a noop.
1482bdd1243dSDimitry Andric   if (SrcVT.bitsLT(DstVT))
1483bdd1243dSDimitry Andric     return false;
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric   // If these values will be promoted, find out what they will be promoted
14860b57cec5SDimitry Andric   // to.  This helps us consider truncates on PPC as noop copies when they
14870b57cec5SDimitry Andric   // are.
14880b57cec5SDimitry Andric   if (TLI.getTypeAction(CI->getContext(), SrcVT) ==
14890b57cec5SDimitry Andric       TargetLowering::TypePromoteInteger)
14900b57cec5SDimitry Andric     SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
14910b57cec5SDimitry Andric   if (TLI.getTypeAction(CI->getContext(), DstVT) ==
14920b57cec5SDimitry Andric       TargetLowering::TypePromoteInteger)
14930b57cec5SDimitry Andric     DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
14940b57cec5SDimitry Andric 
14950b57cec5SDimitry Andric   // If, after promotion, these are the same types, this is a noop copy.
14960b57cec5SDimitry Andric   if (SrcVT != DstVT)
14970b57cec5SDimitry Andric     return false;
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric   return SinkCast(CI);
15000b57cec5SDimitry Andric }
15010b57cec5SDimitry Andric 
1502fe6060f1SDimitry Andric // Match a simple increment by constant operation.  Note that if a sub is
1503fe6060f1SDimitry Andric // matched, the step is negated (as if the step had been canonicalized to
1504fe6060f1SDimitry Andric // an add, even though we leave the instruction alone.)
matchIncrement(const Instruction * IVInc,Instruction * & LHS,Constant * & Step)1505fe6060f1SDimitry Andric bool matchIncrement(const Instruction *IVInc, Instruction *&LHS,
1506fe6060f1SDimitry Andric                     Constant *&Step) {
1507fe6060f1SDimitry Andric   if (match(IVInc, m_Add(m_Instruction(LHS), m_Constant(Step))) ||
1508fe6060f1SDimitry Andric       match(IVInc, m_ExtractValue<0>(m_Intrinsic<Intrinsic::uadd_with_overflow>(
1509fe6060f1SDimitry Andric                        m_Instruction(LHS), m_Constant(Step)))))
1510fe6060f1SDimitry Andric     return true;
1511fe6060f1SDimitry Andric   if (match(IVInc, m_Sub(m_Instruction(LHS), m_Constant(Step))) ||
1512fe6060f1SDimitry Andric       match(IVInc, m_ExtractValue<0>(m_Intrinsic<Intrinsic::usub_with_overflow>(
1513fe6060f1SDimitry Andric                        m_Instruction(LHS), m_Constant(Step))))) {
1514fe6060f1SDimitry Andric     Step = ConstantExpr::getNeg(Step);
1515fe6060f1SDimitry Andric     return true;
1516fe6060f1SDimitry Andric   }
1517fe6060f1SDimitry Andric   return false;
1518fe6060f1SDimitry Andric }
1519fe6060f1SDimitry Andric 
1520fe6060f1SDimitry Andric /// If given \p PN is an inductive variable with value IVInc coming from the
1521fe6060f1SDimitry Andric /// backedge, and on each iteration it gets increased by Step, return pair
1522bdd1243dSDimitry Andric /// <IVInc, Step>. Otherwise, return std::nullopt.
1523bdd1243dSDimitry Andric static std::optional<std::pair<Instruction *, Constant *>>
getIVIncrement(const PHINode * PN,const LoopInfo * LI)1524fe6060f1SDimitry Andric getIVIncrement(const PHINode *PN, const LoopInfo *LI) {
1525fe6060f1SDimitry Andric   const Loop *L = LI->getLoopFor(PN->getParent());
1526fe6060f1SDimitry Andric   if (!L || L->getHeader() != PN->getParent() || !L->getLoopLatch())
1527bdd1243dSDimitry Andric     return std::nullopt;
1528fe6060f1SDimitry Andric   auto *IVInc =
1529fe6060f1SDimitry Andric       dyn_cast<Instruction>(PN->getIncomingValueForBlock(L->getLoopLatch()));
1530fe6060f1SDimitry Andric   if (!IVInc || LI->getLoopFor(IVInc->getParent()) != L)
1531bdd1243dSDimitry Andric     return std::nullopt;
1532fe6060f1SDimitry Andric   Instruction *LHS = nullptr;
1533fe6060f1SDimitry Andric   Constant *Step = nullptr;
1534fe6060f1SDimitry Andric   if (matchIncrement(IVInc, LHS, Step) && LHS == PN)
1535fe6060f1SDimitry Andric     return std::make_pair(IVInc, Step);
1536bdd1243dSDimitry Andric   return std::nullopt;
1537fe6060f1SDimitry Andric }
1538fe6060f1SDimitry Andric 
isIVIncrement(const Value * V,const LoopInfo * LI)1539fe6060f1SDimitry Andric static bool isIVIncrement(const Value *V, const LoopInfo *LI) {
1540fe6060f1SDimitry Andric   auto *I = dyn_cast<Instruction>(V);
1541fe6060f1SDimitry Andric   if (!I)
1542fe6060f1SDimitry Andric     return false;
1543fe6060f1SDimitry Andric   Instruction *LHS = nullptr;
1544fe6060f1SDimitry Andric   Constant *Step = nullptr;
1545fe6060f1SDimitry Andric   if (!matchIncrement(I, LHS, Step))
1546fe6060f1SDimitry Andric     return false;
1547fe6060f1SDimitry Andric   if (auto *PN = dyn_cast<PHINode>(LHS))
1548fe6060f1SDimitry Andric     if (auto IVInc = getIVIncrement(PN, LI))
1549fe6060f1SDimitry Andric       return IVInc->first == I;
1550fe6060f1SDimitry Andric   return false;
1551fe6060f1SDimitry Andric }
1552fe6060f1SDimitry Andric 
replaceMathCmpWithIntrinsic(BinaryOperator * BO,Value * Arg0,Value * Arg1,CmpInst * Cmp,Intrinsic::ID IID)15530b57cec5SDimitry Andric bool CodeGenPrepare::replaceMathCmpWithIntrinsic(BinaryOperator *BO,
15545ffd83dbSDimitry Andric                                                  Value *Arg0, Value *Arg1,
15550b57cec5SDimitry Andric                                                  CmpInst *Cmp,
15560b57cec5SDimitry Andric                                                  Intrinsic::ID IID) {
1557fe6060f1SDimitry Andric   auto IsReplacableIVIncrement = [this, &Cmp](BinaryOperator *BO) {
1558fe6060f1SDimitry Andric     if (!isIVIncrement(BO, LI))
1559fe6060f1SDimitry Andric       return false;
1560fe6060f1SDimitry Andric     const Loop *L = LI->getLoopFor(BO->getParent());
1561fe6060f1SDimitry Andric     assert(L && "L should not be null after isIVIncrement()");
1562fe6060f1SDimitry Andric     // Do not risk on moving increment into a child loop.
1563fe6060f1SDimitry Andric     if (LI->getLoopFor(Cmp->getParent()) != L)
1564fe6060f1SDimitry Andric       return false;
1565fe6060f1SDimitry Andric 
1566fe6060f1SDimitry Andric     // Finally, we need to ensure that the insert point will dominate all
1567fe6060f1SDimitry Andric     // existing uses of the increment.
1568fe6060f1SDimitry Andric 
1569fe6060f1SDimitry Andric     auto &DT = getDT(*BO->getParent()->getParent());
1570fe6060f1SDimitry Andric     if (DT.dominates(Cmp->getParent(), BO->getParent()))
1571fe6060f1SDimitry Andric       // If we're moving up the dom tree, all uses are trivially dominated.
1572fe6060f1SDimitry Andric       // (This is the common case for code produced by LSR.)
1573fe6060f1SDimitry Andric       return true;
1574fe6060f1SDimitry Andric 
1575fe6060f1SDimitry Andric     // Otherwise, special case the single use in the phi recurrence.
1576fe6060f1SDimitry Andric     return BO->hasOneUse() && DT.dominates(Cmp->getParent(), L->getLoopLatch());
1577fe6060f1SDimitry Andric   };
1578fe6060f1SDimitry Andric   if (BO->getParent() != Cmp->getParent() && !IsReplacableIVIncrement(BO)) {
15790b57cec5SDimitry Andric     // We used to use a dominator tree here to allow multi-block optimization.
15800b57cec5SDimitry Andric     // But that was problematic because:
15810b57cec5SDimitry Andric     // 1. It could cause a perf regression by hoisting the math op into the
15820b57cec5SDimitry Andric     //    critical path.
15830b57cec5SDimitry Andric     // 2. It could cause a perf regression by creating a value that was live
15840b57cec5SDimitry Andric     //    across multiple blocks and increasing register pressure.
15850b57cec5SDimitry Andric     // 3. Use of a dominator tree could cause large compile-time regression.
15860b57cec5SDimitry Andric     //    This is because we recompute the DT on every change in the main CGP
15870b57cec5SDimitry Andric     //    run-loop. The recomputing is probably unnecessary in many cases, so if
15880b57cec5SDimitry Andric     //    that was fixed, using a DT here would be ok.
1589fe6060f1SDimitry Andric     //
1590fe6060f1SDimitry Andric     // There is one important particular case we still want to handle: if BO is
1591fe6060f1SDimitry Andric     // the IV increment. Important properties that make it profitable:
1592fe6060f1SDimitry Andric     // - We can speculate IV increment anywhere in the loop (as long as the
1593fe6060f1SDimitry Andric     //   indvar Phi is its only user);
1594fe6060f1SDimitry Andric     // - Upon computing Cmp, we effectively compute something equivalent to the
1595fe6060f1SDimitry Andric     //   IV increment (despite it loops differently in the IR). So moving it up
1596fe6060f1SDimitry Andric     //   to the cmp point does not really increase register pressure.
15970b57cec5SDimitry Andric     return false;
15980b57cec5SDimitry Andric   }
15990b57cec5SDimitry Andric 
16000b57cec5SDimitry Andric   // We allow matching the canonical IR (add X, C) back to (usubo X, -C).
16010b57cec5SDimitry Andric   if (BO->getOpcode() == Instruction::Add &&
16020b57cec5SDimitry Andric       IID == Intrinsic::usub_with_overflow) {
16030b57cec5SDimitry Andric     assert(isa<Constant>(Arg1) && "Unexpected input for usubo");
16040b57cec5SDimitry Andric     Arg1 = ConstantExpr::getNeg(cast<Constant>(Arg1));
16050b57cec5SDimitry Andric   }
16060b57cec5SDimitry Andric 
16070b57cec5SDimitry Andric   // Insert at the first instruction of the pair.
16080b57cec5SDimitry Andric   Instruction *InsertPt = nullptr;
16090b57cec5SDimitry Andric   for (Instruction &Iter : *Cmp->getParent()) {
16105ffd83dbSDimitry Andric     // If BO is an XOR, it is not guaranteed that it comes after both inputs to
16115ffd83dbSDimitry Andric     // the overflow intrinsic are defined.
16125ffd83dbSDimitry Andric     if ((BO->getOpcode() != Instruction::Xor && &Iter == BO) || &Iter == Cmp) {
16130b57cec5SDimitry Andric       InsertPt = &Iter;
16140b57cec5SDimitry Andric       break;
16150b57cec5SDimitry Andric     }
16160b57cec5SDimitry Andric   }
16170b57cec5SDimitry Andric   assert(InsertPt != nullptr && "Parent block did not contain cmp or binop");
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   IRBuilder<> Builder(InsertPt);
16200b57cec5SDimitry Andric   Value *MathOV = Builder.CreateBinaryIntrinsic(IID, Arg0, Arg1);
16215ffd83dbSDimitry Andric   if (BO->getOpcode() != Instruction::Xor) {
16220b57cec5SDimitry Andric     Value *Math = Builder.CreateExtractValue(MathOV, 0, "math");
1623bdd1243dSDimitry Andric     replaceAllUsesWith(BO, Math, FreshBBs, IsHugeFunc);
16245ffd83dbSDimitry Andric   } else
16255ffd83dbSDimitry Andric     assert(BO->hasOneUse() &&
16265ffd83dbSDimitry Andric            "Patterns with XOr should use the BO only in the compare");
16275ffd83dbSDimitry Andric   Value *OV = Builder.CreateExtractValue(MathOV, 1, "ov");
1628bdd1243dSDimitry Andric   replaceAllUsesWith(Cmp, OV, FreshBBs, IsHugeFunc);
16290b57cec5SDimitry Andric   Cmp->eraseFromParent();
16305ffd83dbSDimitry Andric   BO->eraseFromParent();
16310b57cec5SDimitry Andric   return true;
16320b57cec5SDimitry Andric }
16330b57cec5SDimitry Andric 
16340b57cec5SDimitry Andric /// Match special-case patterns that check for unsigned add overflow.
matchUAddWithOverflowConstantEdgeCases(CmpInst * Cmp,BinaryOperator * & Add)16350b57cec5SDimitry Andric static bool matchUAddWithOverflowConstantEdgeCases(CmpInst *Cmp,
16360b57cec5SDimitry Andric                                                    BinaryOperator *&Add) {
16370b57cec5SDimitry Andric   // Add = add A, 1; Cmp = icmp eq A,-1 (overflow if A is max val)
16380b57cec5SDimitry Andric   // Add = add A,-1; Cmp = icmp ne A, 0 (overflow if A is non-zero)
16390b57cec5SDimitry Andric   Value *A = Cmp->getOperand(0), *B = Cmp->getOperand(1);
16400b57cec5SDimitry Andric 
16410b57cec5SDimitry Andric   // We are not expecting non-canonical/degenerate code. Just bail out.
16420b57cec5SDimitry Andric   if (isa<Constant>(A))
16430b57cec5SDimitry Andric     return false;
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   ICmpInst::Predicate Pred = Cmp->getPredicate();
16460b57cec5SDimitry Andric   if (Pred == ICmpInst::ICMP_EQ && match(B, m_AllOnes()))
16470b57cec5SDimitry Andric     B = ConstantInt::get(B->getType(), 1);
16480b57cec5SDimitry Andric   else if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt()))
16490b57cec5SDimitry Andric     B = ConstantInt::get(B->getType(), -1);
16500b57cec5SDimitry Andric   else
16510b57cec5SDimitry Andric     return false;
16520b57cec5SDimitry Andric 
16530b57cec5SDimitry Andric   // Check the users of the variable operand of the compare looking for an add
16540b57cec5SDimitry Andric   // with the adjusted constant.
16550b57cec5SDimitry Andric   for (User *U : A->users()) {
16560b57cec5SDimitry Andric     if (match(U, m_Add(m_Specific(A), m_Specific(B)))) {
16570b57cec5SDimitry Andric       Add = cast<BinaryOperator>(U);
16580b57cec5SDimitry Andric       return true;
16590b57cec5SDimitry Andric     }
16600b57cec5SDimitry Andric   }
16610b57cec5SDimitry Andric   return false;
16620b57cec5SDimitry Andric }
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric /// Try to combine the compare into a call to the llvm.uadd.with.overflow
16650b57cec5SDimitry Andric /// intrinsic. Return true if any changes were made.
combineToUAddWithOverflow(CmpInst * Cmp,ModifyDT & ModifiedDT)16660b57cec5SDimitry Andric bool CodeGenPrepare::combineToUAddWithOverflow(CmpInst *Cmp,
1667bdd1243dSDimitry Andric                                                ModifyDT &ModifiedDT) {
1668fe013be4SDimitry Andric   bool EdgeCase = false;
16690b57cec5SDimitry Andric   Value *A, *B;
16700b57cec5SDimitry Andric   BinaryOperator *Add;
16715ffd83dbSDimitry Andric   if (!match(Cmp, m_UAddWithOverflow(m_Value(A), m_Value(B), m_BinOp(Add)))) {
16720b57cec5SDimitry Andric     if (!matchUAddWithOverflowConstantEdgeCases(Cmp, Add))
16730b57cec5SDimitry Andric       return false;
16745ffd83dbSDimitry Andric     // Set A and B in case we match matchUAddWithOverflowConstantEdgeCases.
16755ffd83dbSDimitry Andric     A = Add->getOperand(0);
16765ffd83dbSDimitry Andric     B = Add->getOperand(1);
1677fe013be4SDimitry Andric     EdgeCase = true;
16785ffd83dbSDimitry Andric   }
16790b57cec5SDimitry Andric 
16800b57cec5SDimitry Andric   if (!TLI->shouldFormOverflowOp(ISD::UADDO,
16815ffd83dbSDimitry Andric                                  TLI->getValueType(*DL, Add->getType()),
1682fe013be4SDimitry Andric                                  Add->hasNUsesOrMore(EdgeCase ? 1 : 2)))
16830b57cec5SDimitry Andric     return false;
16840b57cec5SDimitry Andric 
16850b57cec5SDimitry Andric   // We don't want to move around uses of condition values this late, so we
16860b57cec5SDimitry Andric   // check if it is legal to create the call to the intrinsic in the basic
16870b57cec5SDimitry Andric   // block containing the icmp.
16880b57cec5SDimitry Andric   if (Add->getParent() != Cmp->getParent() && !Add->hasOneUse())
16890b57cec5SDimitry Andric     return false;
16900b57cec5SDimitry Andric 
16915ffd83dbSDimitry Andric   if (!replaceMathCmpWithIntrinsic(Add, A, B, Cmp,
16925ffd83dbSDimitry Andric                                    Intrinsic::uadd_with_overflow))
16930b57cec5SDimitry Andric     return false;
16940b57cec5SDimitry Andric 
16950b57cec5SDimitry Andric   // Reset callers - do not crash by iterating over a dead instruction.
1696bdd1243dSDimitry Andric   ModifiedDT = ModifyDT::ModifyInstDT;
16970b57cec5SDimitry Andric   return true;
16980b57cec5SDimitry Andric }
16990b57cec5SDimitry Andric 
combineToUSubWithOverflow(CmpInst * Cmp,ModifyDT & ModifiedDT)17000b57cec5SDimitry Andric bool CodeGenPrepare::combineToUSubWithOverflow(CmpInst *Cmp,
1701bdd1243dSDimitry Andric                                                ModifyDT &ModifiedDT) {
17020b57cec5SDimitry Andric   // We are not expecting non-canonical/degenerate code. Just bail out.
17030b57cec5SDimitry Andric   Value *A = Cmp->getOperand(0), *B = Cmp->getOperand(1);
17040b57cec5SDimitry Andric   if (isa<Constant>(A) && isa<Constant>(B))
17050b57cec5SDimitry Andric     return false;
17060b57cec5SDimitry Andric 
17070b57cec5SDimitry Andric   // Convert (A u> B) to (A u< B) to simplify pattern matching.
17080b57cec5SDimitry Andric   ICmpInst::Predicate Pred = Cmp->getPredicate();
17090b57cec5SDimitry Andric   if (Pred == ICmpInst::ICMP_UGT) {
17100b57cec5SDimitry Andric     std::swap(A, B);
17110b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_ULT;
17120b57cec5SDimitry Andric   }
17130b57cec5SDimitry Andric   // Convert special-case: (A == 0) is the same as (A u< 1).
17140b57cec5SDimitry Andric   if (Pred == ICmpInst::ICMP_EQ && match(B, m_ZeroInt())) {
17150b57cec5SDimitry Andric     B = ConstantInt::get(B->getType(), 1);
17160b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_ULT;
17170b57cec5SDimitry Andric   }
17180b57cec5SDimitry Andric   // Convert special-case: (A != 0) is the same as (0 u< A).
17190b57cec5SDimitry Andric   if (Pred == ICmpInst::ICMP_NE && match(B, m_ZeroInt())) {
17200b57cec5SDimitry Andric     std::swap(A, B);
17210b57cec5SDimitry Andric     Pred = ICmpInst::ICMP_ULT;
17220b57cec5SDimitry Andric   }
17230b57cec5SDimitry Andric   if (Pred != ICmpInst::ICMP_ULT)
17240b57cec5SDimitry Andric     return false;
17250b57cec5SDimitry Andric 
17260b57cec5SDimitry Andric   // Walk the users of a variable operand of a compare looking for a subtract or
17270b57cec5SDimitry Andric   // add with that same operand. Also match the 2nd operand of the compare to
17280b57cec5SDimitry Andric   // the add/sub, but that may be a negated constant operand of an add.
17290b57cec5SDimitry Andric   Value *CmpVariableOperand = isa<Constant>(A) ? B : A;
17300b57cec5SDimitry Andric   BinaryOperator *Sub = nullptr;
17310b57cec5SDimitry Andric   for (User *U : CmpVariableOperand->users()) {
17320b57cec5SDimitry Andric     // A - B, A u< B --> usubo(A, B)
17330b57cec5SDimitry Andric     if (match(U, m_Sub(m_Specific(A), m_Specific(B)))) {
17340b57cec5SDimitry Andric       Sub = cast<BinaryOperator>(U);
17350b57cec5SDimitry Andric       break;
17360b57cec5SDimitry Andric     }
17370b57cec5SDimitry Andric 
17380b57cec5SDimitry Andric     // A + (-C), A u< C (canonicalized form of (sub A, C))
17390b57cec5SDimitry Andric     const APInt *CmpC, *AddC;
17400b57cec5SDimitry Andric     if (match(U, m_Add(m_Specific(A), m_APInt(AddC))) &&
17410b57cec5SDimitry Andric         match(B, m_APInt(CmpC)) && *AddC == -(*CmpC)) {
17420b57cec5SDimitry Andric       Sub = cast<BinaryOperator>(U);
17430b57cec5SDimitry Andric       break;
17440b57cec5SDimitry Andric     }
17450b57cec5SDimitry Andric   }
17460b57cec5SDimitry Andric   if (!Sub)
17470b57cec5SDimitry Andric     return false;
17480b57cec5SDimitry Andric 
17490b57cec5SDimitry Andric   if (!TLI->shouldFormOverflowOp(ISD::USUBO,
17505ffd83dbSDimitry Andric                                  TLI->getValueType(*DL, Sub->getType()),
1751fe013be4SDimitry Andric                                  Sub->hasNUsesOrMore(1)))
17520b57cec5SDimitry Andric     return false;
17530b57cec5SDimitry Andric 
17545ffd83dbSDimitry Andric   if (!replaceMathCmpWithIntrinsic(Sub, Sub->getOperand(0), Sub->getOperand(1),
17555ffd83dbSDimitry Andric                                    Cmp, Intrinsic::usub_with_overflow))
17560b57cec5SDimitry Andric     return false;
17570b57cec5SDimitry Andric 
17580b57cec5SDimitry Andric   // Reset callers - do not crash by iterating over a dead instruction.
1759bdd1243dSDimitry Andric   ModifiedDT = ModifyDT::ModifyInstDT;
17600b57cec5SDimitry Andric   return true;
17610b57cec5SDimitry Andric }
17620b57cec5SDimitry Andric 
17630b57cec5SDimitry Andric /// Sink the given CmpInst into user blocks to reduce the number of virtual
17640b57cec5SDimitry Andric /// registers that must be created and coalesced. This is a clear win except on
17650b57cec5SDimitry Andric /// targets with multiple condition code registers (PowerPC), where it might
17660b57cec5SDimitry Andric /// lose; some adjustment may be wanted there.
17670b57cec5SDimitry Andric ///
17680b57cec5SDimitry Andric /// Return true if any changes are made.
sinkCmpExpression(CmpInst * Cmp,const TargetLowering & TLI)17690b57cec5SDimitry Andric static bool sinkCmpExpression(CmpInst *Cmp, const TargetLowering &TLI) {
17700b57cec5SDimitry Andric   if (TLI.hasMultipleConditionRegisters())
17710b57cec5SDimitry Andric     return false;
17720b57cec5SDimitry Andric 
17730b57cec5SDimitry Andric   // Avoid sinking soft-FP comparisons, since this can move them into a loop.
17740b57cec5SDimitry Andric   if (TLI.useSoftFloat() && isa<FCmpInst>(Cmp))
17750b57cec5SDimitry Andric     return false;
17760b57cec5SDimitry Andric 
17770b57cec5SDimitry Andric   // Only insert a cmp in each block once.
17780b57cec5SDimitry Andric   DenseMap<BasicBlock *, CmpInst *> InsertedCmps;
17790b57cec5SDimitry Andric 
17800b57cec5SDimitry Andric   bool MadeChange = false;
17810b57cec5SDimitry Andric   for (Value::user_iterator UI = Cmp->user_begin(), E = Cmp->user_end();
17820b57cec5SDimitry Andric        UI != E;) {
17830b57cec5SDimitry Andric     Use &TheUse = UI.getUse();
17840b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(*UI);
17850b57cec5SDimitry Andric 
17860b57cec5SDimitry Andric     // Preincrement use iterator so we don't invalidate it.
17870b57cec5SDimitry Andric     ++UI;
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric     // Don't bother for PHI nodes.
17900b57cec5SDimitry Andric     if (isa<PHINode>(User))
17910b57cec5SDimitry Andric       continue;
17920b57cec5SDimitry Andric 
17930b57cec5SDimitry Andric     // Figure out which BB this cmp is used in.
17940b57cec5SDimitry Andric     BasicBlock *UserBB = User->getParent();
17950b57cec5SDimitry Andric     BasicBlock *DefBB = Cmp->getParent();
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric     // If this user is in the same block as the cmp, don't change the cmp.
1798bdd1243dSDimitry Andric     if (UserBB == DefBB)
1799bdd1243dSDimitry Andric       continue;
18000b57cec5SDimitry Andric 
18010b57cec5SDimitry Andric     // If we have already inserted a cmp into this block, use it.
18020b57cec5SDimitry Andric     CmpInst *&InsertedCmp = InsertedCmps[UserBB];
18030b57cec5SDimitry Andric 
18040b57cec5SDimitry Andric     if (!InsertedCmp) {
18050b57cec5SDimitry Andric       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
18060b57cec5SDimitry Andric       assert(InsertPt != UserBB->end());
1807bdd1243dSDimitry Andric       InsertedCmp = CmpInst::Create(Cmp->getOpcode(), Cmp->getPredicate(),
1808c9157d92SDimitry Andric                                     Cmp->getOperand(0), Cmp->getOperand(1), "");
1809c9157d92SDimitry Andric       InsertedCmp->insertBefore(*UserBB, InsertPt);
18100b57cec5SDimitry Andric       // Propagate the debug info.
18110b57cec5SDimitry Andric       InsertedCmp->setDebugLoc(Cmp->getDebugLoc());
18120b57cec5SDimitry Andric     }
18130b57cec5SDimitry Andric 
18140b57cec5SDimitry Andric     // Replace a use of the cmp with a use of the new cmp.
18150b57cec5SDimitry Andric     TheUse = InsertedCmp;
18160b57cec5SDimitry Andric     MadeChange = true;
18170b57cec5SDimitry Andric     ++NumCmpUses;
18180b57cec5SDimitry Andric   }
18190b57cec5SDimitry Andric 
18200b57cec5SDimitry Andric   // If we removed all uses, nuke the cmp.
18210b57cec5SDimitry Andric   if (Cmp->use_empty()) {
18220b57cec5SDimitry Andric     Cmp->eraseFromParent();
18230b57cec5SDimitry Andric     MadeChange = true;
18240b57cec5SDimitry Andric   }
18250b57cec5SDimitry Andric 
18260b57cec5SDimitry Andric   return MadeChange;
18270b57cec5SDimitry Andric }
18280b57cec5SDimitry Andric 
1829480093f4SDimitry Andric /// For pattern like:
1830480093f4SDimitry Andric ///
1831480093f4SDimitry Andric ///   DomCond = icmp sgt/slt CmpOp0, CmpOp1 (might not be in DomBB)
1832480093f4SDimitry Andric ///   ...
1833480093f4SDimitry Andric /// DomBB:
1834480093f4SDimitry Andric ///   ...
1835480093f4SDimitry Andric ///   br DomCond, TrueBB, CmpBB
1836480093f4SDimitry Andric /// CmpBB: (with DomBB being the single predecessor)
1837480093f4SDimitry Andric ///   ...
1838480093f4SDimitry Andric ///   Cmp = icmp eq CmpOp0, CmpOp1
1839480093f4SDimitry Andric ///   ...
1840480093f4SDimitry Andric ///
1841480093f4SDimitry Andric /// It would use two comparison on targets that lowering of icmp sgt/slt is
1842480093f4SDimitry Andric /// different from lowering of icmp eq (PowerPC). This function try to convert
1843480093f4SDimitry Andric /// 'Cmp = icmp eq CmpOp0, CmpOp1' to ' Cmp = icmp slt/sgt CmpOp0, CmpOp1'.
1844480093f4SDimitry Andric /// After that, DomCond and Cmp can use the same comparison so reduce one
1845480093f4SDimitry Andric /// comparison.
1846480093f4SDimitry Andric ///
1847480093f4SDimitry Andric /// Return true if any changes are made.
foldICmpWithDominatingICmp(CmpInst * Cmp,const TargetLowering & TLI)1848480093f4SDimitry Andric static bool foldICmpWithDominatingICmp(CmpInst *Cmp,
1849480093f4SDimitry Andric                                        const TargetLowering &TLI) {
1850480093f4SDimitry Andric   if (!EnableICMP_EQToICMP_ST && TLI.isEqualityCmpFoldedWithSignedCmp())
1851480093f4SDimitry Andric     return false;
1852480093f4SDimitry Andric 
1853480093f4SDimitry Andric   ICmpInst::Predicate Pred = Cmp->getPredicate();
1854480093f4SDimitry Andric   if (Pred != ICmpInst::ICMP_EQ)
1855480093f4SDimitry Andric     return false;
1856480093f4SDimitry Andric 
1857480093f4SDimitry Andric   // If icmp eq has users other than BranchInst and SelectInst, converting it to
1858480093f4SDimitry Andric   // icmp slt/sgt would introduce more redundant LLVM IR.
1859480093f4SDimitry Andric   for (User *U : Cmp->users()) {
1860480093f4SDimitry Andric     if (isa<BranchInst>(U))
1861480093f4SDimitry Andric       continue;
1862480093f4SDimitry Andric     if (isa<SelectInst>(U) && cast<SelectInst>(U)->getCondition() == Cmp)
1863480093f4SDimitry Andric       continue;
1864480093f4SDimitry Andric     return false;
1865480093f4SDimitry Andric   }
1866480093f4SDimitry Andric 
1867480093f4SDimitry Andric   // This is a cheap/incomplete check for dominance - just match a single
1868480093f4SDimitry Andric   // predecessor with a conditional branch.
1869480093f4SDimitry Andric   BasicBlock *CmpBB = Cmp->getParent();
1870480093f4SDimitry Andric   BasicBlock *DomBB = CmpBB->getSinglePredecessor();
1871480093f4SDimitry Andric   if (!DomBB)
1872480093f4SDimitry Andric     return false;
1873480093f4SDimitry Andric 
1874480093f4SDimitry Andric   // We want to ensure that the only way control gets to the comparison of
1875480093f4SDimitry Andric   // interest is that a less/greater than comparison on the same operands is
1876480093f4SDimitry Andric   // false.
1877480093f4SDimitry Andric   Value *DomCond;
1878480093f4SDimitry Andric   BasicBlock *TrueBB, *FalseBB;
1879480093f4SDimitry Andric   if (!match(DomBB->getTerminator(), m_Br(m_Value(DomCond), TrueBB, FalseBB)))
1880480093f4SDimitry Andric     return false;
1881480093f4SDimitry Andric   if (CmpBB != FalseBB)
1882480093f4SDimitry Andric     return false;
1883480093f4SDimitry Andric 
1884480093f4SDimitry Andric   Value *CmpOp0 = Cmp->getOperand(0), *CmpOp1 = Cmp->getOperand(1);
1885480093f4SDimitry Andric   ICmpInst::Predicate DomPred;
1886480093f4SDimitry Andric   if (!match(DomCond, m_ICmp(DomPred, m_Specific(CmpOp0), m_Specific(CmpOp1))))
1887480093f4SDimitry Andric     return false;
1888480093f4SDimitry Andric   if (DomPred != ICmpInst::ICMP_SGT && DomPred != ICmpInst::ICMP_SLT)
1889480093f4SDimitry Andric     return false;
1890480093f4SDimitry Andric 
1891480093f4SDimitry Andric   // Convert the equality comparison to the opposite of the dominating
1892480093f4SDimitry Andric   // comparison and swap the direction for all branch/select users.
1893480093f4SDimitry Andric   // We have conceptually converted:
1894480093f4SDimitry Andric   // Res = (a < b) ? <LT_RES> : (a == b) ? <EQ_RES> : <GT_RES>;
1895480093f4SDimitry Andric   // to
1896480093f4SDimitry Andric   // Res = (a < b) ? <LT_RES> : (a > b)  ? <GT_RES> : <EQ_RES>;
1897480093f4SDimitry Andric   // And similarly for branches.
1898480093f4SDimitry Andric   for (User *U : Cmp->users()) {
1899480093f4SDimitry Andric     if (auto *BI = dyn_cast<BranchInst>(U)) {
1900480093f4SDimitry Andric       assert(BI->isConditional() && "Must be conditional");
1901480093f4SDimitry Andric       BI->swapSuccessors();
1902480093f4SDimitry Andric       continue;
1903480093f4SDimitry Andric     }
1904480093f4SDimitry Andric     if (auto *SI = dyn_cast<SelectInst>(U)) {
1905480093f4SDimitry Andric       // Swap operands
1906480093f4SDimitry Andric       SI->swapValues();
1907480093f4SDimitry Andric       SI->swapProfMetadata();
1908480093f4SDimitry Andric       continue;
1909480093f4SDimitry Andric     }
1910480093f4SDimitry Andric     llvm_unreachable("Must be a branch or a select");
1911480093f4SDimitry Andric   }
1912480093f4SDimitry Andric   Cmp->setPredicate(CmpInst::getSwappedPredicate(DomPred));
1913480093f4SDimitry Andric   return true;
1914480093f4SDimitry Andric }
1915480093f4SDimitry Andric 
1916fe013be4SDimitry Andric /// Many architectures use the same instruction for both subtract and cmp. Try
1917fe013be4SDimitry Andric /// to swap cmp operands to match subtract operations to allow for CSE.
swapICmpOperandsToExposeCSEOpportunities(CmpInst * Cmp)1918fe013be4SDimitry Andric static bool swapICmpOperandsToExposeCSEOpportunities(CmpInst *Cmp) {
1919fe013be4SDimitry Andric   Value *Op0 = Cmp->getOperand(0);
1920fe013be4SDimitry Andric   Value *Op1 = Cmp->getOperand(1);
1921fe013be4SDimitry Andric   if (!Op0->getType()->isIntegerTy() || isa<Constant>(Op0) ||
1922fe013be4SDimitry Andric       isa<Constant>(Op1) || Op0 == Op1)
1923fe013be4SDimitry Andric     return false;
1924fe013be4SDimitry Andric 
1925fe013be4SDimitry Andric   // If a subtract already has the same operands as a compare, swapping would be
1926fe013be4SDimitry Andric   // bad. If a subtract has the same operands as a compare but in reverse order,
1927fe013be4SDimitry Andric   // then swapping is good.
1928fe013be4SDimitry Andric   int GoodToSwap = 0;
1929fe013be4SDimitry Andric   unsigned NumInspected = 0;
1930fe013be4SDimitry Andric   for (const User *U : Op0->users()) {
1931fe013be4SDimitry Andric     // Avoid walking many users.
1932fe013be4SDimitry Andric     if (++NumInspected > 128)
1933fe013be4SDimitry Andric       return false;
1934fe013be4SDimitry Andric     if (match(U, m_Sub(m_Specific(Op1), m_Specific(Op0))))
1935fe013be4SDimitry Andric       GoodToSwap++;
1936fe013be4SDimitry Andric     else if (match(U, m_Sub(m_Specific(Op0), m_Specific(Op1))))
1937fe013be4SDimitry Andric       GoodToSwap--;
1938fe013be4SDimitry Andric   }
1939fe013be4SDimitry Andric 
1940fe013be4SDimitry Andric   if (GoodToSwap > 0) {
1941fe013be4SDimitry Andric     Cmp->swapOperands();
1942fe013be4SDimitry Andric     return true;
1943fe013be4SDimitry Andric   }
1944fe013be4SDimitry Andric   return false;
1945fe013be4SDimitry Andric }
1946fe013be4SDimitry Andric 
optimizeCmp(CmpInst * Cmp,ModifyDT & ModifiedDT)1947bdd1243dSDimitry Andric bool CodeGenPrepare::optimizeCmp(CmpInst *Cmp, ModifyDT &ModifiedDT) {
19480b57cec5SDimitry Andric   if (sinkCmpExpression(Cmp, *TLI))
19490b57cec5SDimitry Andric     return true;
19500b57cec5SDimitry Andric 
19510b57cec5SDimitry Andric   if (combineToUAddWithOverflow(Cmp, ModifiedDT))
19520b57cec5SDimitry Andric     return true;
19530b57cec5SDimitry Andric 
19540b57cec5SDimitry Andric   if (combineToUSubWithOverflow(Cmp, ModifiedDT))
19550b57cec5SDimitry Andric     return true;
19560b57cec5SDimitry Andric 
1957480093f4SDimitry Andric   if (foldICmpWithDominatingICmp(Cmp, *TLI))
1958480093f4SDimitry Andric     return true;
1959480093f4SDimitry Andric 
1960fe013be4SDimitry Andric   if (swapICmpOperandsToExposeCSEOpportunities(Cmp))
1961fe013be4SDimitry Andric     return true;
1962fe013be4SDimitry Andric 
19630b57cec5SDimitry Andric   return false;
19640b57cec5SDimitry Andric }
19650b57cec5SDimitry Andric 
19660b57cec5SDimitry Andric /// Duplicate and sink the given 'and' instruction into user blocks where it is
19670b57cec5SDimitry Andric /// used in a compare to allow isel to generate better code for targets where
19680b57cec5SDimitry Andric /// this operation can be combined.
19690b57cec5SDimitry Andric ///
19700b57cec5SDimitry Andric /// Return true if any changes are made.
sinkAndCmp0Expression(Instruction * AndI,const TargetLowering & TLI,SetOfInstrs & InsertedInsts)1971bdd1243dSDimitry Andric static bool sinkAndCmp0Expression(Instruction *AndI, const TargetLowering &TLI,
19720b57cec5SDimitry Andric                                   SetOfInstrs &InsertedInsts) {
19730b57cec5SDimitry Andric   // Double-check that we're not trying to optimize an instruction that was
19740b57cec5SDimitry Andric   // already optimized by some other part of this pass.
19750b57cec5SDimitry Andric   assert(!InsertedInsts.count(AndI) &&
19760b57cec5SDimitry Andric          "Attempting to optimize already optimized and instruction");
19770b57cec5SDimitry Andric   (void)InsertedInsts;
19780b57cec5SDimitry Andric 
19790b57cec5SDimitry Andric   // Nothing to do for single use in same basic block.
19800b57cec5SDimitry Andric   if (AndI->hasOneUse() &&
19810b57cec5SDimitry Andric       AndI->getParent() == cast<Instruction>(*AndI->user_begin())->getParent())
19820b57cec5SDimitry Andric     return false;
19830b57cec5SDimitry Andric 
19840b57cec5SDimitry Andric   // Try to avoid cases where sinking/duplicating is likely to increase register
19850b57cec5SDimitry Andric   // pressure.
19860b57cec5SDimitry Andric   if (!isa<ConstantInt>(AndI->getOperand(0)) &&
19870b57cec5SDimitry Andric       !isa<ConstantInt>(AndI->getOperand(1)) &&
19880b57cec5SDimitry Andric       AndI->getOperand(0)->hasOneUse() && AndI->getOperand(1)->hasOneUse())
19890b57cec5SDimitry Andric     return false;
19900b57cec5SDimitry Andric 
19910b57cec5SDimitry Andric   for (auto *U : AndI->users()) {
19920b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(U);
19930b57cec5SDimitry Andric 
19940b57cec5SDimitry Andric     // Only sink 'and' feeding icmp with 0.
19950b57cec5SDimitry Andric     if (!isa<ICmpInst>(User))
19960b57cec5SDimitry Andric       return false;
19970b57cec5SDimitry Andric 
19980b57cec5SDimitry Andric     auto *CmpC = dyn_cast<ConstantInt>(User->getOperand(1));
19990b57cec5SDimitry Andric     if (!CmpC || !CmpC->isZero())
20000b57cec5SDimitry Andric       return false;
20010b57cec5SDimitry Andric   }
20020b57cec5SDimitry Andric 
20030b57cec5SDimitry Andric   if (!TLI.isMaskAndCmp0FoldingBeneficial(*AndI))
20040b57cec5SDimitry Andric     return false;
20050b57cec5SDimitry Andric 
20060b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "found 'and' feeding only icmp 0;\n");
20070b57cec5SDimitry Andric   LLVM_DEBUG(AndI->getParent()->dump());
20080b57cec5SDimitry Andric 
20090b57cec5SDimitry Andric   // Push the 'and' into the same block as the icmp 0.  There should only be
20100b57cec5SDimitry Andric   // one (icmp (and, 0)) in each block, since CSE/GVN should have removed any
20110b57cec5SDimitry Andric   // others, so we don't need to keep track of which BBs we insert into.
20120b57cec5SDimitry Andric   for (Value::user_iterator UI = AndI->user_begin(), E = AndI->user_end();
20130b57cec5SDimitry Andric        UI != E;) {
20140b57cec5SDimitry Andric     Use &TheUse = UI.getUse();
20150b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(*UI);
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric     // Preincrement use iterator so we don't invalidate it.
20180b57cec5SDimitry Andric     ++UI;
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "sinking 'and' use: " << *User << "\n");
20210b57cec5SDimitry Andric 
20220b57cec5SDimitry Andric     // Keep the 'and' in the same place if the use is already in the same block.
20230b57cec5SDimitry Andric     Instruction *InsertPt =
20240b57cec5SDimitry Andric         User->getParent() == AndI->getParent() ? AndI : User;
20250b57cec5SDimitry Andric     Instruction *InsertedAnd =
20260b57cec5SDimitry Andric         BinaryOperator::Create(Instruction::And, AndI->getOperand(0),
20270b57cec5SDimitry Andric                                AndI->getOperand(1), "", InsertPt);
20280b57cec5SDimitry Andric     // Propagate the debug info.
20290b57cec5SDimitry Andric     InsertedAnd->setDebugLoc(AndI->getDebugLoc());
20300b57cec5SDimitry Andric 
20310b57cec5SDimitry Andric     // Replace a use of the 'and' with a use of the new 'and'.
20320b57cec5SDimitry Andric     TheUse = InsertedAnd;
20330b57cec5SDimitry Andric     ++NumAndUses;
20340b57cec5SDimitry Andric     LLVM_DEBUG(User->getParent()->dump());
20350b57cec5SDimitry Andric   }
20360b57cec5SDimitry Andric 
20370b57cec5SDimitry Andric   // We removed all uses, nuke the and.
20380b57cec5SDimitry Andric   AndI->eraseFromParent();
20390b57cec5SDimitry Andric   return true;
20400b57cec5SDimitry Andric }
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric /// Check if the candidates could be combined with a shift instruction, which
20430b57cec5SDimitry Andric /// includes:
20440b57cec5SDimitry Andric /// 1. Truncate instruction
20450b57cec5SDimitry Andric /// 2. And instruction and the imm is a mask of the low bits:
20460b57cec5SDimitry Andric /// imm & (imm+1) == 0
isExtractBitsCandidateUse(Instruction * User)20470b57cec5SDimitry Andric static bool isExtractBitsCandidateUse(Instruction *User) {
20480b57cec5SDimitry Andric   if (!isa<TruncInst>(User)) {
20490b57cec5SDimitry Andric     if (User->getOpcode() != Instruction::And ||
20500b57cec5SDimitry Andric         !isa<ConstantInt>(User->getOperand(1)))
20510b57cec5SDimitry Andric       return false;
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric     const APInt &Cimm = cast<ConstantInt>(User->getOperand(1))->getValue();
20540b57cec5SDimitry Andric 
20550b57cec5SDimitry Andric     if ((Cimm & (Cimm + 1)).getBoolValue())
20560b57cec5SDimitry Andric       return false;
20570b57cec5SDimitry Andric   }
20580b57cec5SDimitry Andric   return true;
20590b57cec5SDimitry Andric }
20600b57cec5SDimitry Andric 
20610b57cec5SDimitry Andric /// Sink both shift and truncate instruction to the use of truncate's BB.
20620b57cec5SDimitry Andric static bool
SinkShiftAndTruncate(BinaryOperator * ShiftI,Instruction * User,ConstantInt * CI,DenseMap<BasicBlock *,BinaryOperator * > & InsertedShifts,const TargetLowering & TLI,const DataLayout & DL)20630b57cec5SDimitry Andric SinkShiftAndTruncate(BinaryOperator *ShiftI, Instruction *User, ConstantInt *CI,
20640b57cec5SDimitry Andric                      DenseMap<BasicBlock *, BinaryOperator *> &InsertedShifts,
20650b57cec5SDimitry Andric                      const TargetLowering &TLI, const DataLayout &DL) {
20660b57cec5SDimitry Andric   BasicBlock *UserBB = User->getParent();
20670b57cec5SDimitry Andric   DenseMap<BasicBlock *, CastInst *> InsertedTruncs;
20688bcb0991SDimitry Andric   auto *TruncI = cast<TruncInst>(User);
20690b57cec5SDimitry Andric   bool MadeChange = false;
20700b57cec5SDimitry Andric 
20710b57cec5SDimitry Andric   for (Value::user_iterator TruncUI = TruncI->user_begin(),
20720b57cec5SDimitry Andric                             TruncE = TruncI->user_end();
20730b57cec5SDimitry Andric        TruncUI != TruncE;) {
20740b57cec5SDimitry Andric 
20750b57cec5SDimitry Andric     Use &TruncTheUse = TruncUI.getUse();
20760b57cec5SDimitry Andric     Instruction *TruncUser = cast<Instruction>(*TruncUI);
20770b57cec5SDimitry Andric     // Preincrement use iterator so we don't invalidate it.
20780b57cec5SDimitry Andric 
20790b57cec5SDimitry Andric     ++TruncUI;
20800b57cec5SDimitry Andric 
20810b57cec5SDimitry Andric     int ISDOpcode = TLI.InstructionOpcodeToISD(TruncUser->getOpcode());
20820b57cec5SDimitry Andric     if (!ISDOpcode)
20830b57cec5SDimitry Andric       continue;
20840b57cec5SDimitry Andric 
20850b57cec5SDimitry Andric     // If the use is actually a legal node, there will not be an
20860b57cec5SDimitry Andric     // implicit truncate.
20870b57cec5SDimitry Andric     // FIXME: always querying the result type is just an
20880b57cec5SDimitry Andric     // approximation; some nodes' legality is determined by the
20890b57cec5SDimitry Andric     // operand or other means. There's no good way to find out though.
20900b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(
20910b57cec5SDimitry Andric             ISDOpcode, TLI.getValueType(DL, TruncUser->getType(), true)))
20920b57cec5SDimitry Andric       continue;
20930b57cec5SDimitry Andric 
20940b57cec5SDimitry Andric     // Don't bother for PHI nodes.
20950b57cec5SDimitry Andric     if (isa<PHINode>(TruncUser))
20960b57cec5SDimitry Andric       continue;
20970b57cec5SDimitry Andric 
20980b57cec5SDimitry Andric     BasicBlock *TruncUserBB = TruncUser->getParent();
20990b57cec5SDimitry Andric 
21000b57cec5SDimitry Andric     if (UserBB == TruncUserBB)
21010b57cec5SDimitry Andric       continue;
21020b57cec5SDimitry Andric 
21030b57cec5SDimitry Andric     BinaryOperator *&InsertedShift = InsertedShifts[TruncUserBB];
21040b57cec5SDimitry Andric     CastInst *&InsertedTrunc = InsertedTruncs[TruncUserBB];
21050b57cec5SDimitry Andric 
21060b57cec5SDimitry Andric     if (!InsertedShift && !InsertedTrunc) {
21070b57cec5SDimitry Andric       BasicBlock::iterator InsertPt = TruncUserBB->getFirstInsertionPt();
21080b57cec5SDimitry Andric       assert(InsertPt != TruncUserBB->end());
21090b57cec5SDimitry Andric       // Sink the shift
21100b57cec5SDimitry Andric       if (ShiftI->getOpcode() == Instruction::AShr)
2111c9157d92SDimitry Andric         InsertedShift =
2112c9157d92SDimitry Andric             BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "");
21130b57cec5SDimitry Andric       else
2114c9157d92SDimitry Andric         InsertedShift =
2115c9157d92SDimitry Andric             BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "");
21160b57cec5SDimitry Andric       InsertedShift->setDebugLoc(ShiftI->getDebugLoc());
2117c9157d92SDimitry Andric       InsertedShift->insertBefore(*TruncUserBB, InsertPt);
21180b57cec5SDimitry Andric 
21190b57cec5SDimitry Andric       // Sink the trunc
21200b57cec5SDimitry Andric       BasicBlock::iterator TruncInsertPt = TruncUserBB->getFirstInsertionPt();
21210b57cec5SDimitry Andric       TruncInsertPt++;
2122c9157d92SDimitry Andric       // It will go ahead of any debug-info.
2123c9157d92SDimitry Andric       TruncInsertPt.setHeadBit(true);
21240b57cec5SDimitry Andric       assert(TruncInsertPt != TruncUserBB->end());
21250b57cec5SDimitry Andric 
21260b57cec5SDimitry Andric       InsertedTrunc = CastInst::Create(TruncI->getOpcode(), InsertedShift,
2127c9157d92SDimitry Andric                                        TruncI->getType(), "");
2128c9157d92SDimitry Andric       InsertedTrunc->insertBefore(*TruncUserBB, TruncInsertPt);
21290b57cec5SDimitry Andric       InsertedTrunc->setDebugLoc(TruncI->getDebugLoc());
21300b57cec5SDimitry Andric 
21310b57cec5SDimitry Andric       MadeChange = true;
21320b57cec5SDimitry Andric 
21330b57cec5SDimitry Andric       TruncTheUse = InsertedTrunc;
21340b57cec5SDimitry Andric     }
21350b57cec5SDimitry Andric   }
21360b57cec5SDimitry Andric   return MadeChange;
21370b57cec5SDimitry Andric }
21380b57cec5SDimitry Andric 
21390b57cec5SDimitry Andric /// Sink the shift *right* instruction into user blocks if the uses could
21400b57cec5SDimitry Andric /// potentially be combined with this shift instruction and generate BitExtract
21410b57cec5SDimitry Andric /// instruction. It will only be applied if the architecture supports BitExtract
21420b57cec5SDimitry Andric /// instruction. Here is an example:
21430b57cec5SDimitry Andric /// BB1:
21440b57cec5SDimitry Andric ///   %x.extract.shift = lshr i64 %arg1, 32
21450b57cec5SDimitry Andric /// BB2:
21460b57cec5SDimitry Andric ///   %x.extract.trunc = trunc i64 %x.extract.shift to i16
21470b57cec5SDimitry Andric /// ==>
21480b57cec5SDimitry Andric ///
21490b57cec5SDimitry Andric /// BB2:
21500b57cec5SDimitry Andric ///   %x.extract.shift.1 = lshr i64 %arg1, 32
21510b57cec5SDimitry Andric ///   %x.extract.trunc = trunc i64 %x.extract.shift.1 to i16
21520b57cec5SDimitry Andric ///
21530b57cec5SDimitry Andric /// CodeGen will recognize the pattern in BB2 and generate BitExtract
21540b57cec5SDimitry Andric /// instruction.
21550b57cec5SDimitry Andric /// Return true if any changes are made.
OptimizeExtractBits(BinaryOperator * ShiftI,ConstantInt * CI,const TargetLowering & TLI,const DataLayout & DL)21560b57cec5SDimitry Andric static bool OptimizeExtractBits(BinaryOperator *ShiftI, ConstantInt *CI,
21570b57cec5SDimitry Andric                                 const TargetLowering &TLI,
21580b57cec5SDimitry Andric                                 const DataLayout &DL) {
21590b57cec5SDimitry Andric   BasicBlock *DefBB = ShiftI->getParent();
21600b57cec5SDimitry Andric 
21610b57cec5SDimitry Andric   /// Only insert instructions in each block once.
21620b57cec5SDimitry Andric   DenseMap<BasicBlock *, BinaryOperator *> InsertedShifts;
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric   bool shiftIsLegal = TLI.isTypeLegal(TLI.getValueType(DL, ShiftI->getType()));
21650b57cec5SDimitry Andric 
21660b57cec5SDimitry Andric   bool MadeChange = false;
21670b57cec5SDimitry Andric   for (Value::user_iterator UI = ShiftI->user_begin(), E = ShiftI->user_end();
21680b57cec5SDimitry Andric        UI != E;) {
21690b57cec5SDimitry Andric     Use &TheUse = UI.getUse();
21700b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(*UI);
21710b57cec5SDimitry Andric     // Preincrement use iterator so we don't invalidate it.
21720b57cec5SDimitry Andric     ++UI;
21730b57cec5SDimitry Andric 
21740b57cec5SDimitry Andric     // Don't bother for PHI nodes.
21750b57cec5SDimitry Andric     if (isa<PHINode>(User))
21760b57cec5SDimitry Andric       continue;
21770b57cec5SDimitry Andric 
21780b57cec5SDimitry Andric     if (!isExtractBitsCandidateUse(User))
21790b57cec5SDimitry Andric       continue;
21800b57cec5SDimitry Andric 
21810b57cec5SDimitry Andric     BasicBlock *UserBB = User->getParent();
21820b57cec5SDimitry Andric 
21830b57cec5SDimitry Andric     if (UserBB == DefBB) {
21840b57cec5SDimitry Andric       // If the shift and truncate instruction are in the same BB. The use of
21850b57cec5SDimitry Andric       // the truncate(TruncUse) may still introduce another truncate if not
21860b57cec5SDimitry Andric       // legal. In this case, we would like to sink both shift and truncate
21870b57cec5SDimitry Andric       // instruction to the BB of TruncUse.
21880b57cec5SDimitry Andric       // for example:
21890b57cec5SDimitry Andric       // BB1:
21900b57cec5SDimitry Andric       // i64 shift.result = lshr i64 opnd, imm
21910b57cec5SDimitry Andric       // trunc.result = trunc shift.result to i16
21920b57cec5SDimitry Andric       //
21930b57cec5SDimitry Andric       // BB2:
21940b57cec5SDimitry Andric       //   ----> We will have an implicit truncate here if the architecture does
21950b57cec5SDimitry Andric       //   not have i16 compare.
21960b57cec5SDimitry Andric       // cmp i16 trunc.result, opnd2
21970b57cec5SDimitry Andric       //
2198bdd1243dSDimitry Andric       if (isa<TruncInst>(User) &&
2199bdd1243dSDimitry Andric           shiftIsLegal
22000b57cec5SDimitry Andric           // If the type of the truncate is legal, no truncate will be
22010b57cec5SDimitry Andric           // introduced in other basic blocks.
2202bdd1243dSDimitry Andric           && (!TLI.isTypeLegal(TLI.getValueType(DL, User->getType()))))
22030b57cec5SDimitry Andric         MadeChange =
22040b57cec5SDimitry Andric             SinkShiftAndTruncate(ShiftI, User, CI, InsertedShifts, TLI, DL);
22050b57cec5SDimitry Andric 
22060b57cec5SDimitry Andric       continue;
22070b57cec5SDimitry Andric     }
22080b57cec5SDimitry Andric     // If we have already inserted a shift into this block, use it.
22090b57cec5SDimitry Andric     BinaryOperator *&InsertedShift = InsertedShifts[UserBB];
22100b57cec5SDimitry Andric 
22110b57cec5SDimitry Andric     if (!InsertedShift) {
22120b57cec5SDimitry Andric       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
22130b57cec5SDimitry Andric       assert(InsertPt != UserBB->end());
22140b57cec5SDimitry Andric 
22150b57cec5SDimitry Andric       if (ShiftI->getOpcode() == Instruction::AShr)
2216c9157d92SDimitry Andric         InsertedShift =
2217c9157d92SDimitry Andric             BinaryOperator::CreateAShr(ShiftI->getOperand(0), CI, "");
22180b57cec5SDimitry Andric       else
2219c9157d92SDimitry Andric         InsertedShift =
2220c9157d92SDimitry Andric             BinaryOperator::CreateLShr(ShiftI->getOperand(0), CI, "");
2221c9157d92SDimitry Andric       InsertedShift->insertBefore(*UserBB, InsertPt);
22220b57cec5SDimitry Andric       InsertedShift->setDebugLoc(ShiftI->getDebugLoc());
22230b57cec5SDimitry Andric 
22240b57cec5SDimitry Andric       MadeChange = true;
22250b57cec5SDimitry Andric     }
22260b57cec5SDimitry Andric 
22270b57cec5SDimitry Andric     // Replace a use of the shift with a use of the new shift.
22280b57cec5SDimitry Andric     TheUse = InsertedShift;
22290b57cec5SDimitry Andric   }
22300b57cec5SDimitry Andric 
22310b57cec5SDimitry Andric   // If we removed all uses, or there are none, nuke the shift.
22320b57cec5SDimitry Andric   if (ShiftI->use_empty()) {
22330b57cec5SDimitry Andric     salvageDebugInfo(*ShiftI);
22340b57cec5SDimitry Andric     ShiftI->eraseFromParent();
22350b57cec5SDimitry Andric     MadeChange = true;
22360b57cec5SDimitry Andric   }
22370b57cec5SDimitry Andric 
22380b57cec5SDimitry Andric   return MadeChange;
22390b57cec5SDimitry Andric }
22400b57cec5SDimitry Andric 
22410b57cec5SDimitry Andric /// If counting leading or trailing zeros is an expensive operation and a zero
22420b57cec5SDimitry Andric /// input is defined, add a check for zero to avoid calling the intrinsic.
22430b57cec5SDimitry Andric ///
22440b57cec5SDimitry Andric /// We want to transform:
22450b57cec5SDimitry Andric ///     %z = call i64 @llvm.cttz.i64(i64 %A, i1 false)
22460b57cec5SDimitry Andric ///
22470b57cec5SDimitry Andric /// into:
22480b57cec5SDimitry Andric ///   entry:
22490b57cec5SDimitry Andric ///     %cmpz = icmp eq i64 %A, 0
22500b57cec5SDimitry Andric ///     br i1 %cmpz, label %cond.end, label %cond.false
22510b57cec5SDimitry Andric ///   cond.false:
22520b57cec5SDimitry Andric ///     %z = call i64 @llvm.cttz.i64(i64 %A, i1 true)
22530b57cec5SDimitry Andric ///     br label %cond.end
22540b57cec5SDimitry Andric ///   cond.end:
22550b57cec5SDimitry Andric ///     %ctz = phi i64 [ 64, %entry ], [ %z, %cond.false ]
22560b57cec5SDimitry Andric ///
22570b57cec5SDimitry Andric /// If the transform is performed, return true and set ModifiedDT to true.
despeculateCountZeros(IntrinsicInst * CountZeros,LoopInfo & LI,const TargetLowering * TLI,const DataLayout * DL,ModifyDT & ModifiedDT,SmallSet<BasicBlock *,32> & FreshBBs,bool IsHugeFunc)22580b57cec5SDimitry Andric static bool despeculateCountZeros(IntrinsicInst *CountZeros,
2259fe013be4SDimitry Andric                                   LoopInfo &LI,
22600b57cec5SDimitry Andric                                   const TargetLowering *TLI,
2261bdd1243dSDimitry Andric                                   const DataLayout *DL, ModifyDT &ModifiedDT,
2262bdd1243dSDimitry Andric                                   SmallSet<BasicBlock *, 32> &FreshBBs,
2263bdd1243dSDimitry Andric                                   bool IsHugeFunc) {
22640b57cec5SDimitry Andric   // If a zero input is undefined, it doesn't make sense to despeculate that.
22650b57cec5SDimitry Andric   if (match(CountZeros->getOperand(1), m_One()))
22660b57cec5SDimitry Andric     return false;
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric   // If it's cheap to speculate, there's nothing to do.
2269bdd1243dSDimitry Andric   Type *Ty = CountZeros->getType();
22700b57cec5SDimitry Andric   auto IntrinsicID = CountZeros->getIntrinsicID();
2271bdd1243dSDimitry Andric   if ((IntrinsicID == Intrinsic::cttz && TLI->isCheapToSpeculateCttz(Ty)) ||
2272bdd1243dSDimitry Andric       (IntrinsicID == Intrinsic::ctlz && TLI->isCheapToSpeculateCtlz(Ty)))
22730b57cec5SDimitry Andric     return false;
22740b57cec5SDimitry Andric 
22750b57cec5SDimitry Andric   // Only handle legal scalar cases. Anything else requires too much work.
2276349cc55cSDimitry Andric   unsigned SizeInBits = Ty->getScalarSizeInBits();
22770b57cec5SDimitry Andric   if (Ty->isVectorTy() || SizeInBits > DL->getLargestLegalIntTypeSizeInBits())
22780b57cec5SDimitry Andric     return false;
22790b57cec5SDimitry Andric 
2280fe6060f1SDimitry Andric   // Bail if the value is never zero.
228181ad6265SDimitry Andric   Use &Op = CountZeros->getOperandUse(0);
228281ad6265SDimitry Andric   if (isKnownNonZero(Op, *DL))
2283fe6060f1SDimitry Andric     return false;
2284fe6060f1SDimitry Andric 
22850b57cec5SDimitry Andric   // The intrinsic will be sunk behind a compare against zero and branch.
22860b57cec5SDimitry Andric   BasicBlock *StartBlock = CountZeros->getParent();
22870b57cec5SDimitry Andric   BasicBlock *CallBlock = StartBlock->splitBasicBlock(CountZeros, "cond.false");
2288bdd1243dSDimitry Andric   if (IsHugeFunc)
2289bdd1243dSDimitry Andric     FreshBBs.insert(CallBlock);
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric   // Create another block after the count zero intrinsic. A PHI will be added
22920b57cec5SDimitry Andric   // in this block to select the result of the intrinsic or the bit-width
22930b57cec5SDimitry Andric   // constant if the input to the intrinsic is zero.
2294c9157d92SDimitry Andric   BasicBlock::iterator SplitPt = std::next(BasicBlock::iterator(CountZeros));
2295c9157d92SDimitry Andric   // Any debug-info after CountZeros should not be included.
2296c9157d92SDimitry Andric   SplitPt.setHeadBit(true);
22970b57cec5SDimitry Andric   BasicBlock *EndBlock = CallBlock->splitBasicBlock(SplitPt, "cond.end");
2298bdd1243dSDimitry Andric   if (IsHugeFunc)
2299bdd1243dSDimitry Andric     FreshBBs.insert(EndBlock);
23000b57cec5SDimitry Andric 
2301fe013be4SDimitry Andric   // Update the LoopInfo. The new blocks are in the same loop as the start
2302fe013be4SDimitry Andric   // block.
2303fe013be4SDimitry Andric   if (Loop *L = LI.getLoopFor(StartBlock)) {
2304fe013be4SDimitry Andric     L->addBasicBlockToLoop(CallBlock, LI);
2305fe013be4SDimitry Andric     L->addBasicBlockToLoop(EndBlock, LI);
2306fe013be4SDimitry Andric   }
2307fe013be4SDimitry Andric 
23080b57cec5SDimitry Andric   // Set up a builder to create a compare, conditional branch, and PHI.
23090b57cec5SDimitry Andric   IRBuilder<> Builder(CountZeros->getContext());
23100b57cec5SDimitry Andric   Builder.SetInsertPoint(StartBlock->getTerminator());
23110b57cec5SDimitry Andric   Builder.SetCurrentDebugLocation(CountZeros->getDebugLoc());
23120b57cec5SDimitry Andric 
23130b57cec5SDimitry Andric   // Replace the unconditional branch that was created by the first split with
23140b57cec5SDimitry Andric   // a compare against zero and a conditional branch.
23150b57cec5SDimitry Andric   Value *Zero = Constant::getNullValue(Ty);
231681ad6265SDimitry Andric   // Avoid introducing branch on poison. This also replaces the ctz operand.
231781ad6265SDimitry Andric   if (!isGuaranteedNotToBeUndefOrPoison(Op))
231881ad6265SDimitry Andric     Op = Builder.CreateFreeze(Op, Op->getName() + ".fr");
231981ad6265SDimitry Andric   Value *Cmp = Builder.CreateICmpEQ(Op, Zero, "cmpz");
23200b57cec5SDimitry Andric   Builder.CreateCondBr(Cmp, EndBlock, CallBlock);
23210b57cec5SDimitry Andric   StartBlock->getTerminator()->eraseFromParent();
23220b57cec5SDimitry Andric 
23230b57cec5SDimitry Andric   // Create a PHI in the end block to select either the output of the intrinsic
23240b57cec5SDimitry Andric   // or the bit width of the operand.
2325c9157d92SDimitry Andric   Builder.SetInsertPoint(EndBlock, EndBlock->begin());
23260b57cec5SDimitry Andric   PHINode *PN = Builder.CreatePHI(Ty, 2, "ctz");
2327bdd1243dSDimitry Andric   replaceAllUsesWith(CountZeros, PN, FreshBBs, IsHugeFunc);
23280b57cec5SDimitry Andric   Value *BitWidth = Builder.getInt(APInt(SizeInBits, SizeInBits));
23290b57cec5SDimitry Andric   PN->addIncoming(BitWidth, StartBlock);
23300b57cec5SDimitry Andric   PN->addIncoming(CountZeros, CallBlock);
23310b57cec5SDimitry Andric 
23320b57cec5SDimitry Andric   // We are explicitly handling the zero case, so we can set the intrinsic's
23330b57cec5SDimitry Andric   // undefined zero argument to 'true'. This will also prevent reprocessing the
23340b57cec5SDimitry Andric   // intrinsic; we only despeculate when a zero input is defined.
23350b57cec5SDimitry Andric   CountZeros->setArgOperand(1, Builder.getTrue());
2336bdd1243dSDimitry Andric   ModifiedDT = ModifyDT::ModifyBBDT;
23370b57cec5SDimitry Andric   return true;
23380b57cec5SDimitry Andric }
23390b57cec5SDimitry Andric 
optimizeCallInst(CallInst * CI,ModifyDT & ModifiedDT)2340bdd1243dSDimitry Andric bool CodeGenPrepare::optimizeCallInst(CallInst *CI, ModifyDT &ModifiedDT) {
23410b57cec5SDimitry Andric   BasicBlock *BB = CI->getParent();
23420b57cec5SDimitry Andric 
23430b57cec5SDimitry Andric   // Lower inline assembly if we can.
23440b57cec5SDimitry Andric   // If we found an inline asm expession, and if the target knows how to
23450b57cec5SDimitry Andric   // lower it to normal LLVM code, do so now.
23465ffd83dbSDimitry Andric   if (CI->isInlineAsm()) {
23470b57cec5SDimitry Andric     if (TLI->ExpandInlineAsm(CI)) {
23480b57cec5SDimitry Andric       // Avoid invalidating the iterator.
23490b57cec5SDimitry Andric       CurInstIterator = BB->begin();
23500b57cec5SDimitry Andric       // Avoid processing instructions out of order, which could cause
23510b57cec5SDimitry Andric       // reuse before a value is defined.
23520b57cec5SDimitry Andric       SunkAddrs.clear();
23530b57cec5SDimitry Andric       return true;
23540b57cec5SDimitry Andric     }
23550b57cec5SDimitry Andric     // Sink address computing for memory operands into the block.
23560b57cec5SDimitry Andric     if (optimizeInlineAsmInst(CI))
23570b57cec5SDimitry Andric       return true;
23580b57cec5SDimitry Andric   }
23590b57cec5SDimitry Andric 
23600b57cec5SDimitry Andric   // Align the pointer arguments to this call if the target thinks it's a good
23610b57cec5SDimitry Andric   // idea
236281ad6265SDimitry Andric   unsigned MinSize;
236381ad6265SDimitry Andric   Align PrefAlign;
23645ffd83dbSDimitry Andric   if (TLI->shouldAlignPointerArgs(CI, MinSize, PrefAlign)) {
2365349cc55cSDimitry Andric     for (auto &Arg : CI->args()) {
23660b57cec5SDimitry Andric       // We want to align both objects whose address is used directly and
23670b57cec5SDimitry Andric       // objects whose address is used in casts and GEPs, though it only makes
23680b57cec5SDimitry Andric       // sense for GEPs if the offset is a multiple of the desired alignment and
23690b57cec5SDimitry Andric       // if size - offset meets the size threshold.
23700b57cec5SDimitry Andric       if (!Arg->getType()->isPointerTy())
23710b57cec5SDimitry Andric         continue;
23720b57cec5SDimitry Andric       APInt Offset(DL->getIndexSizeInBits(
23730b57cec5SDimitry Andric                        cast<PointerType>(Arg->getType())->getAddressSpace()),
23740b57cec5SDimitry Andric                    0);
23750b57cec5SDimitry Andric       Value *Val = Arg->stripAndAccumulateInBoundsConstantOffsets(*DL, Offset);
23760b57cec5SDimitry Andric       uint64_t Offset2 = Offset.getLimitedValue();
237781ad6265SDimitry Andric       if (!isAligned(PrefAlign, Offset2))
23780b57cec5SDimitry Andric         continue;
23790b57cec5SDimitry Andric       AllocaInst *AI;
238081ad6265SDimitry Andric       if ((AI = dyn_cast<AllocaInst>(Val)) && AI->getAlign() < PrefAlign &&
23810b57cec5SDimitry Andric           DL->getTypeAllocSize(AI->getAllocatedType()) >= MinSize + Offset2)
238281ad6265SDimitry Andric         AI->setAlignment(PrefAlign);
23830b57cec5SDimitry Andric       // Global variables can only be aligned if they are defined in this
23840b57cec5SDimitry Andric       // object (i.e. they are uniquely initialized in this object), and
23850b57cec5SDimitry Andric       // over-aligning global variables that have an explicit section is
23860b57cec5SDimitry Andric       // forbidden.
23870b57cec5SDimitry Andric       GlobalVariable *GV;
23880b57cec5SDimitry Andric       if ((GV = dyn_cast<GlobalVariable>(Val)) && GV->canIncreaseAlignment() &&
23890b57cec5SDimitry Andric           GV->getPointerAlignment(*DL) < PrefAlign &&
2390bdd1243dSDimitry Andric           DL->getTypeAllocSize(GV->getValueType()) >= MinSize + Offset2)
239181ad6265SDimitry Andric         GV->setAlignment(PrefAlign);
23920b57cec5SDimitry Andric     }
2393bdd1243dSDimitry Andric   }
23940b57cec5SDimitry Andric   // If this is a memcpy (or similar) then we may be able to improve the
2395bdd1243dSDimitry Andric   // alignment.
23960b57cec5SDimitry Andric   if (MemIntrinsic *MI = dyn_cast<MemIntrinsic>(CI)) {
23975ffd83dbSDimitry Andric     Align DestAlign = getKnownAlignment(MI->getDest(), *DL);
23985ffd83dbSDimitry Andric     MaybeAlign MIDestAlign = MI->getDestAlign();
23995ffd83dbSDimitry Andric     if (!MIDestAlign || DestAlign > *MIDestAlign)
24000b57cec5SDimitry Andric       MI->setDestAlignment(DestAlign);
24010b57cec5SDimitry Andric     if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(MI)) {
24025ffd83dbSDimitry Andric       MaybeAlign MTISrcAlign = MTI->getSourceAlign();
24035ffd83dbSDimitry Andric       Align SrcAlign = getKnownAlignment(MTI->getSource(), *DL);
24045ffd83dbSDimitry Andric       if (!MTISrcAlign || SrcAlign > *MTISrcAlign)
24050b57cec5SDimitry Andric         MTI->setSourceAlignment(SrcAlign);
24060b57cec5SDimitry Andric     }
24070b57cec5SDimitry Andric   }
24080b57cec5SDimitry Andric 
24090b57cec5SDimitry Andric   // If we have a cold call site, try to sink addressing computation into the
24100b57cec5SDimitry Andric   // cold block.  This interacts with our handling for loads and stores to
24110b57cec5SDimitry Andric   // ensure that we can fold all uses of a potential addressing computation
24120b57cec5SDimitry Andric   // into their uses.  TODO: generalize this to work over profiling data
2413bdd1243dSDimitry Andric   if (CI->hasFnAttr(Attribute::Cold) && !OptSize &&
2414bdd1243dSDimitry Andric       !llvm::shouldOptimizeForSize(BB, PSI, BFI.get()))
2415349cc55cSDimitry Andric     for (auto &Arg : CI->args()) {
24160b57cec5SDimitry Andric       if (!Arg->getType()->isPointerTy())
24170b57cec5SDimitry Andric         continue;
24180b57cec5SDimitry Andric       unsigned AS = Arg->getType()->getPointerAddressSpace();
2419fe013be4SDimitry Andric       if (optimizeMemoryInst(CI, Arg, Arg->getType(), AS))
2420fe013be4SDimitry Andric         return true;
24210b57cec5SDimitry Andric     }
24220b57cec5SDimitry Andric 
24230b57cec5SDimitry Andric   IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
24240b57cec5SDimitry Andric   if (II) {
24250b57cec5SDimitry Andric     switch (II->getIntrinsicID()) {
2426bdd1243dSDimitry Andric     default:
2427bdd1243dSDimitry Andric       break;
2428fe6060f1SDimitry Andric     case Intrinsic::assume:
2429fe6060f1SDimitry Andric       llvm_unreachable("llvm.assume should have been removed already");
24300b57cec5SDimitry Andric     case Intrinsic::experimental_widenable_condition: {
24310b57cec5SDimitry Andric       // Give up on future widening oppurtunties so that we can fold away dead
24320b57cec5SDimitry Andric       // paths and merge blocks before going into block-local instruction
24330b57cec5SDimitry Andric       // selection.
24340b57cec5SDimitry Andric       if (II->use_empty()) {
24350b57cec5SDimitry Andric         II->eraseFromParent();
24360b57cec5SDimitry Andric         return true;
24370b57cec5SDimitry Andric       }
24380b57cec5SDimitry Andric       Constant *RetVal = ConstantInt::getTrue(II->getContext());
24390b57cec5SDimitry Andric       resetIteratorIfInvalidatedWhileCalling(BB, [&]() {
24400b57cec5SDimitry Andric         replaceAndRecursivelySimplify(CI, RetVal, TLInfo, nullptr);
24410b57cec5SDimitry Andric       });
24420b57cec5SDimitry Andric       return true;
24430b57cec5SDimitry Andric     }
24448bcb0991SDimitry Andric     case Intrinsic::objectsize:
24458bcb0991SDimitry Andric       llvm_unreachable("llvm.objectsize.* should have been lowered already");
24468bcb0991SDimitry Andric     case Intrinsic::is_constant:
24478bcb0991SDimitry Andric       llvm_unreachable("llvm.is.constant.* should have been lowered already");
24480b57cec5SDimitry Andric     case Intrinsic::aarch64_stlxr:
24490b57cec5SDimitry Andric     case Intrinsic::aarch64_stxr: {
24500b57cec5SDimitry Andric       ZExtInst *ExtVal = dyn_cast<ZExtInst>(CI->getArgOperand(0));
24510b57cec5SDimitry Andric       if (!ExtVal || !ExtVal->hasOneUse() ||
24520b57cec5SDimitry Andric           ExtVal->getParent() == CI->getParent())
24530b57cec5SDimitry Andric         return false;
24540b57cec5SDimitry Andric       // Sink a zext feeding stlxr/stxr before it, so it can be folded into it.
24550b57cec5SDimitry Andric       ExtVal->moveBefore(CI);
24560b57cec5SDimitry Andric       // Mark this instruction as "inserted by CGP", so that other
24570b57cec5SDimitry Andric       // optimizations don't touch it.
24580b57cec5SDimitry Andric       InsertedInsts.insert(ExtVal);
24590b57cec5SDimitry Andric       return true;
24600b57cec5SDimitry Andric     }
24610b57cec5SDimitry Andric 
24620b57cec5SDimitry Andric     case Intrinsic::launder_invariant_group:
24630b57cec5SDimitry Andric     case Intrinsic::strip_invariant_group: {
24640b57cec5SDimitry Andric       Value *ArgVal = II->getArgOperand(0);
24650b57cec5SDimitry Andric       auto it = LargeOffsetGEPMap.find(II);
24660b57cec5SDimitry Andric       if (it != LargeOffsetGEPMap.end()) {
24670b57cec5SDimitry Andric         // Merge entries in LargeOffsetGEPMap to reflect the RAUW.
24680b57cec5SDimitry Andric         // Make sure not to have to deal with iterator invalidation
24690b57cec5SDimitry Andric         // after possibly adding ArgVal to LargeOffsetGEPMap.
24700b57cec5SDimitry Andric         auto GEPs = std::move(it->second);
24710b57cec5SDimitry Andric         LargeOffsetGEPMap[ArgVal].append(GEPs.begin(), GEPs.end());
24720b57cec5SDimitry Andric         LargeOffsetGEPMap.erase(II);
24730b57cec5SDimitry Andric       }
24740b57cec5SDimitry Andric 
2475bdd1243dSDimitry Andric       replaceAllUsesWith(II, ArgVal, FreshBBs, IsHugeFunc);
24760b57cec5SDimitry Andric       II->eraseFromParent();
24770b57cec5SDimitry Andric       return true;
24780b57cec5SDimitry Andric     }
24790b57cec5SDimitry Andric     case Intrinsic::cttz:
24800b57cec5SDimitry Andric     case Intrinsic::ctlz:
24810b57cec5SDimitry Andric       // If counting zeros is expensive, try to avoid it.
2482fe013be4SDimitry Andric       return despeculateCountZeros(II, *LI, TLI, DL, ModifiedDT, FreshBBs,
2483bdd1243dSDimitry Andric                                    IsHugeFunc);
24845ffd83dbSDimitry Andric     case Intrinsic::fshl:
24855ffd83dbSDimitry Andric     case Intrinsic::fshr:
24865ffd83dbSDimitry Andric       return optimizeFunnelShift(II);
2487bdd1243dSDimitry Andric     case Intrinsic::dbg_assign:
2488480093f4SDimitry Andric     case Intrinsic::dbg_value:
2489480093f4SDimitry Andric       return fixupDbgValue(II);
24905ffd83dbSDimitry Andric     case Intrinsic::masked_gather:
24915ffd83dbSDimitry Andric       return optimizeGatherScatterInst(II, II->getArgOperand(0));
24925ffd83dbSDimitry Andric     case Intrinsic::masked_scatter:
24935ffd83dbSDimitry Andric       return optimizeGatherScatterInst(II, II->getArgOperand(1));
24940b57cec5SDimitry Andric     }
24950b57cec5SDimitry Andric 
24960b57cec5SDimitry Andric     SmallVector<Value *, 2> PtrOps;
24970b57cec5SDimitry Andric     Type *AccessTy;
24980b57cec5SDimitry Andric     if (TLI->getAddrModeArguments(II, PtrOps, AccessTy))
24990b57cec5SDimitry Andric       while (!PtrOps.empty()) {
25000b57cec5SDimitry Andric         Value *PtrVal = PtrOps.pop_back_val();
25010b57cec5SDimitry Andric         unsigned AS = PtrVal->getType()->getPointerAddressSpace();
25020b57cec5SDimitry Andric         if (optimizeMemoryInst(II, PtrVal, AccessTy, AS))
25030b57cec5SDimitry Andric           return true;
25040b57cec5SDimitry Andric       }
25050b57cec5SDimitry Andric   }
25060b57cec5SDimitry Andric 
25070b57cec5SDimitry Andric   // From here on out we're working with named functions.
2508bdd1243dSDimitry Andric   if (!CI->getCalledFunction())
2509bdd1243dSDimitry Andric     return false;
25100b57cec5SDimitry Andric 
25110b57cec5SDimitry Andric   // Lower all default uses of _chk calls.  This is very similar
25120b57cec5SDimitry Andric   // to what InstCombineCalls does, but here we are only lowering calls
25130b57cec5SDimitry Andric   // to fortified library functions (e.g. __memcpy_chk) that have the default
25140b57cec5SDimitry Andric   // "don't know" as the objectsize.  Anything else should be left alone.
25150b57cec5SDimitry Andric   FortifiedLibCallSimplifier Simplifier(TLInfo, true);
25165ffd83dbSDimitry Andric   IRBuilder<> Builder(CI);
25175ffd83dbSDimitry Andric   if (Value *V = Simplifier.optimizeCall(CI, Builder)) {
2518bdd1243dSDimitry Andric     replaceAllUsesWith(CI, V, FreshBBs, IsHugeFunc);
25190b57cec5SDimitry Andric     CI->eraseFromParent();
25200b57cec5SDimitry Andric     return true;
25210b57cec5SDimitry Andric   }
25220b57cec5SDimitry Andric 
25230b57cec5SDimitry Andric   return false;
25240b57cec5SDimitry Andric }
25250b57cec5SDimitry Andric 
25260b57cec5SDimitry Andric /// Look for opportunities to duplicate return instructions to the predecessor
25270b57cec5SDimitry Andric /// to enable tail call optimizations. The case it is currently looking for is:
25280b57cec5SDimitry Andric /// @code
25290b57cec5SDimitry Andric /// bb0:
25300b57cec5SDimitry Andric ///   %tmp0 = tail call i32 @f0()
25310b57cec5SDimitry Andric ///   br label %return
25320b57cec5SDimitry Andric /// bb1:
25330b57cec5SDimitry Andric ///   %tmp1 = tail call i32 @f1()
25340b57cec5SDimitry Andric ///   br label %return
25350b57cec5SDimitry Andric /// bb2:
25360b57cec5SDimitry Andric ///   %tmp2 = tail call i32 @f2()
25370b57cec5SDimitry Andric ///   br label %return
25380b57cec5SDimitry Andric /// return:
25390b57cec5SDimitry Andric ///   %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
25400b57cec5SDimitry Andric ///   ret i32 %retval
25410b57cec5SDimitry Andric /// @endcode
25420b57cec5SDimitry Andric ///
25430b57cec5SDimitry Andric /// =>
25440b57cec5SDimitry Andric ///
25450b57cec5SDimitry Andric /// @code
25460b57cec5SDimitry Andric /// bb0:
25470b57cec5SDimitry Andric ///   %tmp0 = tail call i32 @f0()
25480b57cec5SDimitry Andric ///   ret i32 %tmp0
25490b57cec5SDimitry Andric /// bb1:
25500b57cec5SDimitry Andric ///   %tmp1 = tail call i32 @f1()
25510b57cec5SDimitry Andric ///   ret i32 %tmp1
25520b57cec5SDimitry Andric /// bb2:
25530b57cec5SDimitry Andric ///   %tmp2 = tail call i32 @f2()
25540b57cec5SDimitry Andric ///   ret i32 %tmp2
25550b57cec5SDimitry Andric /// @endcode
dupRetToEnableTailCallOpts(BasicBlock * BB,ModifyDT & ModifiedDT)2556bdd1243dSDimitry Andric bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB,
2557bdd1243dSDimitry Andric                                                 ModifyDT &ModifiedDT) {
2558bdd1243dSDimitry Andric   if (!BB->getTerminator())
2559bdd1243dSDimitry Andric     return false;
2560bdd1243dSDimitry Andric 
25610b57cec5SDimitry Andric   ReturnInst *RetI = dyn_cast<ReturnInst>(BB->getTerminator());
25620b57cec5SDimitry Andric   if (!RetI)
25630b57cec5SDimitry Andric     return false;
25640b57cec5SDimitry Andric 
2565fe013be4SDimitry Andric   assert(LI->getLoopFor(BB) == nullptr && "A return block cannot be in a loop");
2566fe013be4SDimitry Andric 
25670b57cec5SDimitry Andric   PHINode *PN = nullptr;
25685ffd83dbSDimitry Andric   ExtractValueInst *EVI = nullptr;
25690b57cec5SDimitry Andric   BitCastInst *BCI = nullptr;
25700b57cec5SDimitry Andric   Value *V = RetI->getReturnValue();
25710b57cec5SDimitry Andric   if (V) {
25720b57cec5SDimitry Andric     BCI = dyn_cast<BitCastInst>(V);
25730b57cec5SDimitry Andric     if (BCI)
25740b57cec5SDimitry Andric       V = BCI->getOperand(0);
25750b57cec5SDimitry Andric 
25765ffd83dbSDimitry Andric     EVI = dyn_cast<ExtractValueInst>(V);
25775ffd83dbSDimitry Andric     if (EVI) {
25785ffd83dbSDimitry Andric       V = EVI->getOperand(0);
2579e8d8bef9SDimitry Andric       if (!llvm::all_of(EVI->indices(), [](unsigned idx) { return idx == 0; }))
25805ffd83dbSDimitry Andric         return false;
25815ffd83dbSDimitry Andric     }
25825ffd83dbSDimitry Andric 
25830b57cec5SDimitry Andric     PN = dyn_cast<PHINode>(V);
25840b57cec5SDimitry Andric     if (!PN)
25850b57cec5SDimitry Andric       return false;
25860b57cec5SDimitry Andric   }
25870b57cec5SDimitry Andric 
25880b57cec5SDimitry Andric   if (PN && PN->getParent() != BB)
25890b57cec5SDimitry Andric     return false;
25900b57cec5SDimitry Andric 
2591fe6060f1SDimitry Andric   auto isLifetimeEndOrBitCastFor = [](const Instruction *Inst) {
2592fe6060f1SDimitry Andric     const BitCastInst *BC = dyn_cast<BitCastInst>(Inst);
2593fe6060f1SDimitry Andric     if (BC && BC->hasOneUse())
2594fe6060f1SDimitry Andric       Inst = BC->user_back();
2595fe6060f1SDimitry Andric 
2596fe6060f1SDimitry Andric     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst))
2597fe6060f1SDimitry Andric       return II->getIntrinsicID() == Intrinsic::lifetime_end;
2598fe6060f1SDimitry Andric     return false;
2599fe6060f1SDimitry Andric   };
2600fe6060f1SDimitry Andric 
2601fe6060f1SDimitry Andric   // Make sure there are no instructions between the first instruction
2602fe6060f1SDimitry Andric   // and return.
2603fe6060f1SDimitry Andric   const Instruction *BI = BB->getFirstNonPHI();
26040b57cec5SDimitry Andric   // Skip over debug and the bitcast.
2605fe6060f1SDimitry Andric   while (isa<DbgInfoIntrinsic>(BI) || BI == BCI || BI == EVI ||
2606fe6060f1SDimitry Andric          isa<PseudoProbeInst>(BI) || isLifetimeEndOrBitCastFor(BI))
2607fe6060f1SDimitry Andric     BI = BI->getNextNode();
2608fe6060f1SDimitry Andric   if (BI != RetI)
26090b57cec5SDimitry Andric     return false;
26100b57cec5SDimitry Andric 
26110b57cec5SDimitry Andric   /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
26120b57cec5SDimitry Andric   /// call.
26130b57cec5SDimitry Andric   const Function *F = BB->getParent();
26148bcb0991SDimitry Andric   SmallVector<BasicBlock *, 4> TailCallBBs;
26150b57cec5SDimitry Andric   if (PN) {
26160b57cec5SDimitry Andric     for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
26170b57cec5SDimitry Andric       // Look through bitcasts.
26180b57cec5SDimitry Andric       Value *IncomingVal = PN->getIncomingValue(I)->stripPointerCasts();
26190b57cec5SDimitry Andric       CallInst *CI = dyn_cast<CallInst>(IncomingVal);
26208bcb0991SDimitry Andric       BasicBlock *PredBB = PN->getIncomingBlock(I);
26210b57cec5SDimitry Andric       // Make sure the phi value is indeed produced by the tail call.
26228bcb0991SDimitry Andric       if (CI && CI->hasOneUse() && CI->getParent() == PredBB &&
26230b57cec5SDimitry Andric           TLI->mayBeEmittedAsTailCall(CI) &&
26240b57cec5SDimitry Andric           attributesPermitTailCall(F, CI, RetI, *TLI))
26258bcb0991SDimitry Andric         TailCallBBs.push_back(PredBB);
26260b57cec5SDimitry Andric     }
26270b57cec5SDimitry Andric   } else {
26280b57cec5SDimitry Andric     SmallPtrSet<BasicBlock *, 4> VisitedBBs;
2629fe6060f1SDimitry Andric     for (BasicBlock *Pred : predecessors(BB)) {
2630fe6060f1SDimitry Andric       if (!VisitedBBs.insert(Pred).second)
26310b57cec5SDimitry Andric         continue;
2632fe6060f1SDimitry Andric       if (Instruction *I = Pred->rbegin()->getPrevNonDebugInstruction(true)) {
2633e8d8bef9SDimitry Andric         CallInst *CI = dyn_cast<CallInst>(I);
26340b57cec5SDimitry Andric         if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI) &&
26350b57cec5SDimitry Andric             attributesPermitTailCall(F, CI, RetI, *TLI))
2636fe6060f1SDimitry Andric           TailCallBBs.push_back(Pred);
26370b57cec5SDimitry Andric       }
26380b57cec5SDimitry Andric     }
2639e8d8bef9SDimitry Andric   }
26400b57cec5SDimitry Andric 
26410b57cec5SDimitry Andric   bool Changed = false;
26428bcb0991SDimitry Andric   for (auto const &TailCallBB : TailCallBBs) {
26430b57cec5SDimitry Andric     // Make sure the call instruction is followed by an unconditional branch to
26440b57cec5SDimitry Andric     // the return block.
26458bcb0991SDimitry Andric     BranchInst *BI = dyn_cast<BranchInst>(TailCallBB->getTerminator());
26460b57cec5SDimitry Andric     if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
26470b57cec5SDimitry Andric       continue;
26480b57cec5SDimitry Andric 
26498bcb0991SDimitry Andric     // Duplicate the return into TailCallBB.
26508bcb0991SDimitry Andric     (void)FoldReturnIntoUncondBranch(RetI, BB, TailCallBB);
26515ffd83dbSDimitry Andric     assert(!VerifyBFIUpdates ||
26525ffd83dbSDimitry Andric            BFI->getBlockFreq(BB) >= BFI->getBlockFreq(TailCallBB));
2653c9157d92SDimitry Andric     BFI->setBlockFreq(BB,
2654c9157d92SDimitry Andric                       (BFI->getBlockFreq(BB) - BFI->getBlockFreq(TailCallBB)));
2655bdd1243dSDimitry Andric     ModifiedDT = ModifyDT::ModifyBBDT;
2656bdd1243dSDimitry Andric     Changed = true;
26570b57cec5SDimitry Andric     ++NumRetsDup;
26580b57cec5SDimitry Andric   }
26590b57cec5SDimitry Andric 
26600b57cec5SDimitry Andric   // If we eliminated all predecessors of the block, delete the block now.
2661e8d8bef9SDimitry Andric   if (Changed && !BB->hasAddressTaken() && pred_empty(BB))
26620b57cec5SDimitry Andric     BB->eraseFromParent();
26630b57cec5SDimitry Andric 
26640b57cec5SDimitry Andric   return Changed;
26650b57cec5SDimitry Andric }
26660b57cec5SDimitry Andric 
26670b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
26680b57cec5SDimitry Andric // Memory Optimization
26690b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
26700b57cec5SDimitry Andric 
26710b57cec5SDimitry Andric namespace {
26720b57cec5SDimitry Andric 
26730b57cec5SDimitry Andric /// This is an extended version of TargetLowering::AddrMode
26740b57cec5SDimitry Andric /// which holds actual Value*'s for register values.
26750b57cec5SDimitry Andric struct ExtAddrMode : public TargetLowering::AddrMode {
26760b57cec5SDimitry Andric   Value *BaseReg = nullptr;
26770b57cec5SDimitry Andric   Value *ScaledReg = nullptr;
26780b57cec5SDimitry Andric   Value *OriginalValue = nullptr;
26790b57cec5SDimitry Andric   bool InBounds = true;
26800b57cec5SDimitry Andric 
26810b57cec5SDimitry Andric   enum FieldName {
26820b57cec5SDimitry Andric     NoField = 0x00,
26830b57cec5SDimitry Andric     BaseRegField = 0x01,
26840b57cec5SDimitry Andric     BaseGVField = 0x02,
26850b57cec5SDimitry Andric     BaseOffsField = 0x04,
26860b57cec5SDimitry Andric     ScaledRegField = 0x08,
26870b57cec5SDimitry Andric     ScaleField = 0x10,
26880b57cec5SDimitry Andric     MultipleFields = 0xff
26890b57cec5SDimitry Andric   };
26900b57cec5SDimitry Andric 
26910b57cec5SDimitry Andric   ExtAddrMode() = default;
26920b57cec5SDimitry Andric 
26930b57cec5SDimitry Andric   void print(raw_ostream &OS) const;
26940b57cec5SDimitry Andric   void dump() const;
26950b57cec5SDimitry Andric 
compare__anon01de4a8e0911::ExtAddrMode26960b57cec5SDimitry Andric   FieldName compare(const ExtAddrMode &other) {
26970b57cec5SDimitry Andric     // First check that the types are the same on each field, as differing types
26980b57cec5SDimitry Andric     // is something we can't cope with later on.
26990b57cec5SDimitry Andric     if (BaseReg && other.BaseReg &&
27000b57cec5SDimitry Andric         BaseReg->getType() != other.BaseReg->getType())
27010b57cec5SDimitry Andric       return MultipleFields;
2702bdd1243dSDimitry Andric     if (BaseGV && other.BaseGV && BaseGV->getType() != other.BaseGV->getType())
27030b57cec5SDimitry Andric       return MultipleFields;
27040b57cec5SDimitry Andric     if (ScaledReg && other.ScaledReg &&
27050b57cec5SDimitry Andric         ScaledReg->getType() != other.ScaledReg->getType())
27060b57cec5SDimitry Andric       return MultipleFields;
27070b57cec5SDimitry Andric 
27080b57cec5SDimitry Andric     // Conservatively reject 'inbounds' mismatches.
27090b57cec5SDimitry Andric     if (InBounds != other.InBounds)
27100b57cec5SDimitry Andric       return MultipleFields;
27110b57cec5SDimitry Andric 
27120b57cec5SDimitry Andric     // Check each field to see if it differs.
27130b57cec5SDimitry Andric     unsigned Result = NoField;
27140b57cec5SDimitry Andric     if (BaseReg != other.BaseReg)
27150b57cec5SDimitry Andric       Result |= BaseRegField;
27160b57cec5SDimitry Andric     if (BaseGV != other.BaseGV)
27170b57cec5SDimitry Andric       Result |= BaseGVField;
27180b57cec5SDimitry Andric     if (BaseOffs != other.BaseOffs)
27190b57cec5SDimitry Andric       Result |= BaseOffsField;
27200b57cec5SDimitry Andric     if (ScaledReg != other.ScaledReg)
27210b57cec5SDimitry Andric       Result |= ScaledRegField;
27220b57cec5SDimitry Andric     // Don't count 0 as being a different scale, because that actually means
27230b57cec5SDimitry Andric     // unscaled (which will already be counted by having no ScaledReg).
27240b57cec5SDimitry Andric     if (Scale && other.Scale && Scale != other.Scale)
27250b57cec5SDimitry Andric       Result |= ScaleField;
27260b57cec5SDimitry Andric 
2727bdd1243dSDimitry Andric     if (llvm::popcount(Result) > 1)
27280b57cec5SDimitry Andric       return MultipleFields;
27290b57cec5SDimitry Andric     else
27300b57cec5SDimitry Andric       return static_cast<FieldName>(Result);
27310b57cec5SDimitry Andric   }
27320b57cec5SDimitry Andric 
27330b57cec5SDimitry Andric   // An AddrMode is trivial if it involves no calculation i.e. it is just a base
27340b57cec5SDimitry Andric   // with no offset.
isTrivial__anon01de4a8e0911::ExtAddrMode27350b57cec5SDimitry Andric   bool isTrivial() {
27360b57cec5SDimitry Andric     // An AddrMode is (BaseGV + BaseReg + BaseOffs + ScaleReg * Scale) so it is
27370b57cec5SDimitry Andric     // trivial if at most one of these terms is nonzero, except that BaseGV and
27380b57cec5SDimitry Andric     // BaseReg both being zero actually means a null pointer value, which we
27390b57cec5SDimitry Andric     // consider to be 'non-zero' here.
27400b57cec5SDimitry Andric     return !BaseOffs && !Scale && !(BaseGV && BaseReg);
27410b57cec5SDimitry Andric   }
27420b57cec5SDimitry Andric 
GetFieldAsValue__anon01de4a8e0911::ExtAddrMode27430b57cec5SDimitry Andric   Value *GetFieldAsValue(FieldName Field, Type *IntPtrTy) {
27440b57cec5SDimitry Andric     switch (Field) {
27450b57cec5SDimitry Andric     default:
27460b57cec5SDimitry Andric       return nullptr;
27470b57cec5SDimitry Andric     case BaseRegField:
27480b57cec5SDimitry Andric       return BaseReg;
27490b57cec5SDimitry Andric     case BaseGVField:
27500b57cec5SDimitry Andric       return BaseGV;
27510b57cec5SDimitry Andric     case ScaledRegField:
27520b57cec5SDimitry Andric       return ScaledReg;
27530b57cec5SDimitry Andric     case BaseOffsField:
27540b57cec5SDimitry Andric       return ConstantInt::get(IntPtrTy, BaseOffs);
27550b57cec5SDimitry Andric     }
27560b57cec5SDimitry Andric   }
27570b57cec5SDimitry Andric 
SetCombinedField__anon01de4a8e0911::ExtAddrMode27580b57cec5SDimitry Andric   void SetCombinedField(FieldName Field, Value *V,
27590b57cec5SDimitry Andric                         const SmallVectorImpl<ExtAddrMode> &AddrModes) {
27600b57cec5SDimitry Andric     switch (Field) {
27610b57cec5SDimitry Andric     default:
27620b57cec5SDimitry Andric       llvm_unreachable("Unhandled fields are expected to be rejected earlier");
27630b57cec5SDimitry Andric       break;
27640b57cec5SDimitry Andric     case ExtAddrMode::BaseRegField:
27650b57cec5SDimitry Andric       BaseReg = V;
27660b57cec5SDimitry Andric       break;
27670b57cec5SDimitry Andric     case ExtAddrMode::BaseGVField:
27680b57cec5SDimitry Andric       // A combined BaseGV is an Instruction, not a GlobalValue, so it goes
27690b57cec5SDimitry Andric       // in the BaseReg field.
27700b57cec5SDimitry Andric       assert(BaseReg == nullptr);
27710b57cec5SDimitry Andric       BaseReg = V;
27720b57cec5SDimitry Andric       BaseGV = nullptr;
27730b57cec5SDimitry Andric       break;
27740b57cec5SDimitry Andric     case ExtAddrMode::ScaledRegField:
27750b57cec5SDimitry Andric       ScaledReg = V;
27760b57cec5SDimitry Andric       // If we have a mix of scaled and unscaled addrmodes then we want scale
27770b57cec5SDimitry Andric       // to be the scale and not zero.
27780b57cec5SDimitry Andric       if (!Scale)
27790b57cec5SDimitry Andric         for (const ExtAddrMode &AM : AddrModes)
27800b57cec5SDimitry Andric           if (AM.Scale) {
27810b57cec5SDimitry Andric             Scale = AM.Scale;
27820b57cec5SDimitry Andric             break;
27830b57cec5SDimitry Andric           }
27840b57cec5SDimitry Andric       break;
27850b57cec5SDimitry Andric     case ExtAddrMode::BaseOffsField:
27860b57cec5SDimitry Andric       // The offset is no longer a constant, so it goes in ScaledReg with a
27870b57cec5SDimitry Andric       // scale of 1.
27880b57cec5SDimitry Andric       assert(ScaledReg == nullptr);
27890b57cec5SDimitry Andric       ScaledReg = V;
27900b57cec5SDimitry Andric       Scale = 1;
27910b57cec5SDimitry Andric       BaseOffs = 0;
27920b57cec5SDimitry Andric       break;
27930b57cec5SDimitry Andric     }
27940b57cec5SDimitry Andric   }
27950b57cec5SDimitry Andric };
27960b57cec5SDimitry Andric 
27970b57cec5SDimitry Andric #ifndef NDEBUG
operator <<(raw_ostream & OS,const ExtAddrMode & AM)27980b57cec5SDimitry Andric static inline raw_ostream &operator<<(raw_ostream &OS, const ExtAddrMode &AM) {
27990b57cec5SDimitry Andric   AM.print(OS);
28000b57cec5SDimitry Andric   return OS;
28010b57cec5SDimitry Andric }
28020b57cec5SDimitry Andric #endif
28030b57cec5SDimitry Andric 
28040b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
print(raw_ostream & OS) const28050b57cec5SDimitry Andric void ExtAddrMode::print(raw_ostream &OS) const {
28060b57cec5SDimitry Andric   bool NeedPlus = false;
28070b57cec5SDimitry Andric   OS << "[";
28080b57cec5SDimitry Andric   if (InBounds)
28090b57cec5SDimitry Andric     OS << "inbounds ";
28100b57cec5SDimitry Andric   if (BaseGV) {
2811fe013be4SDimitry Andric     OS << "GV:";
28120b57cec5SDimitry Andric     BaseGV->printAsOperand(OS, /*PrintType=*/false);
28130b57cec5SDimitry Andric     NeedPlus = true;
28140b57cec5SDimitry Andric   }
28150b57cec5SDimitry Andric 
28160b57cec5SDimitry Andric   if (BaseOffs) {
2817bdd1243dSDimitry Andric     OS << (NeedPlus ? " + " : "") << BaseOffs;
28180b57cec5SDimitry Andric     NeedPlus = true;
28190b57cec5SDimitry Andric   }
28200b57cec5SDimitry Andric 
28210b57cec5SDimitry Andric   if (BaseReg) {
2822bdd1243dSDimitry Andric     OS << (NeedPlus ? " + " : "") << "Base:";
28230b57cec5SDimitry Andric     BaseReg->printAsOperand(OS, /*PrintType=*/false);
28240b57cec5SDimitry Andric     NeedPlus = true;
28250b57cec5SDimitry Andric   }
28260b57cec5SDimitry Andric   if (Scale) {
2827bdd1243dSDimitry Andric     OS << (NeedPlus ? " + " : "") << Scale << "*";
28280b57cec5SDimitry Andric     ScaledReg->printAsOperand(OS, /*PrintType=*/false);
28290b57cec5SDimitry Andric   }
28300b57cec5SDimitry Andric 
28310b57cec5SDimitry Andric   OS << ']';
28320b57cec5SDimitry Andric }
28330b57cec5SDimitry Andric 
dump() const28340b57cec5SDimitry Andric LLVM_DUMP_METHOD void ExtAddrMode::dump() const {
28350b57cec5SDimitry Andric   print(dbgs());
28360b57cec5SDimitry Andric   dbgs() << '\n';
28370b57cec5SDimitry Andric }
28380b57cec5SDimitry Andric #endif
28390b57cec5SDimitry Andric 
2840972a253aSDimitry Andric } // end anonymous namespace
2841972a253aSDimitry Andric 
28420b57cec5SDimitry Andric namespace {
28430b57cec5SDimitry Andric 
28440b57cec5SDimitry Andric /// This class provides transaction based operation on the IR.
28450b57cec5SDimitry Andric /// Every change made through this class is recorded in the internal state and
28460b57cec5SDimitry Andric /// can be undone (rollback) until commit is called.
28475ffd83dbSDimitry Andric /// CGP does not check if instructions could be speculatively executed when
28485ffd83dbSDimitry Andric /// moved. Preserving the original location would pessimize the debugging
28495ffd83dbSDimitry Andric /// experience, as well as negatively impact the quality of sample PGO.
28500b57cec5SDimitry Andric class TypePromotionTransaction {
28510b57cec5SDimitry Andric   /// This represents the common interface of the individual transaction.
28520b57cec5SDimitry Andric   /// Each class implements the logic for doing one specific modification on
28530b57cec5SDimitry Andric   /// the IR via the TypePromotionTransaction.
28540b57cec5SDimitry Andric   class TypePromotionAction {
28550b57cec5SDimitry Andric   protected:
28560b57cec5SDimitry Andric     /// The Instruction modified.
28570b57cec5SDimitry Andric     Instruction *Inst;
28580b57cec5SDimitry Andric 
28590b57cec5SDimitry Andric   public:
28600b57cec5SDimitry Andric     /// Constructor of the action.
28610b57cec5SDimitry Andric     /// The constructor performs the related action on the IR.
TypePromotionAction(Instruction * Inst)28620b57cec5SDimitry Andric     TypePromotionAction(Instruction *Inst) : Inst(Inst) {}
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric     virtual ~TypePromotionAction() = default;
28650b57cec5SDimitry Andric 
28660b57cec5SDimitry Andric     /// Undo the modification done by this action.
28670b57cec5SDimitry Andric     /// When this method is called, the IR must be in the same state as it was
28680b57cec5SDimitry Andric     /// before this action was applied.
28690b57cec5SDimitry Andric     /// \pre Undoing the action works if and only if the IR is in the exact same
28700b57cec5SDimitry Andric     /// state as it was directly after this action was applied.
28710b57cec5SDimitry Andric     virtual void undo() = 0;
28720b57cec5SDimitry Andric 
28730b57cec5SDimitry Andric     /// Advocate every change made by this action.
28740b57cec5SDimitry Andric     /// When the results on the IR of the action are to be kept, it is important
28750b57cec5SDimitry Andric     /// to call this function, otherwise hidden information may be kept forever.
commit()28760b57cec5SDimitry Andric     virtual void commit() {
28770b57cec5SDimitry Andric       // Nothing to be done, this action is not doing anything.
28780b57cec5SDimitry Andric     }
28790b57cec5SDimitry Andric   };
28800b57cec5SDimitry Andric 
28810b57cec5SDimitry Andric   /// Utility to remember the position of an instruction.
28820b57cec5SDimitry Andric   class InsertionHandler {
28830b57cec5SDimitry Andric     /// Position of an instruction.
28840b57cec5SDimitry Andric     /// Either an instruction:
28850b57cec5SDimitry Andric     /// - Is the first in a basic block: BB is used.
28860b57cec5SDimitry Andric     /// - Has a previous instruction: PrevInst is used.
28870b57cec5SDimitry Andric     union {
28880b57cec5SDimitry Andric       Instruction *PrevInst;
28890b57cec5SDimitry Andric       BasicBlock *BB;
28900b57cec5SDimitry Andric     } Point;
2891c9157d92SDimitry Andric     std::optional<DPValue::self_iterator> BeforeDPValue = std::nullopt;
28920b57cec5SDimitry Andric 
28930b57cec5SDimitry Andric     /// Remember whether or not the instruction had a previous instruction.
28940b57cec5SDimitry Andric     bool HasPrevInstruction;
28950b57cec5SDimitry Andric 
28960b57cec5SDimitry Andric   public:
28970b57cec5SDimitry Andric     /// Record the position of \p Inst.
InsertionHandler(Instruction * Inst)28980b57cec5SDimitry Andric     InsertionHandler(Instruction *Inst) {
2899c9157d92SDimitry Andric       HasPrevInstruction = (Inst != &*(Inst->getParent()->begin()));
2900c9157d92SDimitry Andric       BasicBlock *BB = Inst->getParent();
2901c9157d92SDimitry Andric 
2902c9157d92SDimitry Andric       // Record where we would have to re-insert the instruction in the sequence
2903c9157d92SDimitry Andric       // of DPValues, if we ended up reinserting.
2904c9157d92SDimitry Andric       if (BB->IsNewDbgInfoFormat)
2905c9157d92SDimitry Andric         BeforeDPValue = Inst->getDbgReinsertionPosition();
2906c9157d92SDimitry Andric 
2907c9157d92SDimitry Andric       if (HasPrevInstruction) {
2908c9157d92SDimitry Andric         Point.PrevInst = &*std::prev(Inst->getIterator());
2909c9157d92SDimitry Andric       } else {
2910c9157d92SDimitry Andric         Point.BB = BB;
2911c9157d92SDimitry Andric       }
29120b57cec5SDimitry Andric     }
29130b57cec5SDimitry Andric 
29140b57cec5SDimitry Andric     /// Insert \p Inst at the recorded position.
insert(Instruction * Inst)29150b57cec5SDimitry Andric     void insert(Instruction *Inst) {
29160b57cec5SDimitry Andric       if (HasPrevInstruction) {
29170b57cec5SDimitry Andric         if (Inst->getParent())
29180b57cec5SDimitry Andric           Inst->removeFromParent();
2919c9157d92SDimitry Andric         Inst->insertAfter(&*Point.PrevInst);
29200b57cec5SDimitry Andric       } else {
2921c9157d92SDimitry Andric         BasicBlock::iterator Position = Point.BB->getFirstInsertionPt();
29220b57cec5SDimitry Andric         if (Inst->getParent())
2923c9157d92SDimitry Andric           Inst->moveBefore(*Point.BB, Position);
29240b57cec5SDimitry Andric         else
2925c9157d92SDimitry Andric           Inst->insertBefore(*Point.BB, Position);
29260b57cec5SDimitry Andric       }
2927c9157d92SDimitry Andric 
2928c9157d92SDimitry Andric       Inst->getParent()->reinsertInstInDPValues(Inst, BeforeDPValue);
29290b57cec5SDimitry Andric     }
29300b57cec5SDimitry Andric   };
29310b57cec5SDimitry Andric 
29320b57cec5SDimitry Andric   /// Move an instruction before another.
29330b57cec5SDimitry Andric   class InstructionMoveBefore : public TypePromotionAction {
29340b57cec5SDimitry Andric     /// Original position of the instruction.
29350b57cec5SDimitry Andric     InsertionHandler Position;
29360b57cec5SDimitry Andric 
29370b57cec5SDimitry Andric   public:
29380b57cec5SDimitry Andric     /// Move \p Inst before \p Before.
InstructionMoveBefore(Instruction * Inst,Instruction * Before)29390b57cec5SDimitry Andric     InstructionMoveBefore(Instruction *Inst, Instruction *Before)
29400b57cec5SDimitry Andric         : TypePromotionAction(Inst), Position(Inst) {
29410b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: move: " << *Inst << "\nbefore: " << *Before
29420b57cec5SDimitry Andric                         << "\n");
29430b57cec5SDimitry Andric       Inst->moveBefore(Before);
29440b57cec5SDimitry Andric     }
29450b57cec5SDimitry Andric 
29460b57cec5SDimitry Andric     /// Move the instruction back to its original position.
undo()29470b57cec5SDimitry Andric     void undo() override {
29480b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: moveBefore: " << *Inst << "\n");
29490b57cec5SDimitry Andric       Position.insert(Inst);
29500b57cec5SDimitry Andric     }
29510b57cec5SDimitry Andric   };
29520b57cec5SDimitry Andric 
29530b57cec5SDimitry Andric   /// Set the operand of an instruction with a new value.
29540b57cec5SDimitry Andric   class OperandSetter : public TypePromotionAction {
29550b57cec5SDimitry Andric     /// Original operand of the instruction.
29560b57cec5SDimitry Andric     Value *Origin;
29570b57cec5SDimitry Andric 
29580b57cec5SDimitry Andric     /// Index of the modified instruction.
29590b57cec5SDimitry Andric     unsigned Idx;
29600b57cec5SDimitry Andric 
29610b57cec5SDimitry Andric   public:
29620b57cec5SDimitry Andric     /// Set \p Idx operand of \p Inst with \p NewVal.
OperandSetter(Instruction * Inst,unsigned Idx,Value * NewVal)29630b57cec5SDimitry Andric     OperandSetter(Instruction *Inst, unsigned Idx, Value *NewVal)
29640b57cec5SDimitry Andric         : TypePromotionAction(Inst), Idx(Idx) {
29650b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: setOperand: " << Idx << "\n"
29660b57cec5SDimitry Andric                         << "for:" << *Inst << "\n"
29670b57cec5SDimitry Andric                         << "with:" << *NewVal << "\n");
29680b57cec5SDimitry Andric       Origin = Inst->getOperand(Idx);
29690b57cec5SDimitry Andric       Inst->setOperand(Idx, NewVal);
29700b57cec5SDimitry Andric     }
29710b57cec5SDimitry Andric 
29720b57cec5SDimitry Andric     /// Restore the original value of the instruction.
undo()29730b57cec5SDimitry Andric     void undo() override {
29740b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: setOperand:" << Idx << "\n"
29750b57cec5SDimitry Andric                         << "for: " << *Inst << "\n"
29760b57cec5SDimitry Andric                         << "with: " << *Origin << "\n");
29770b57cec5SDimitry Andric       Inst->setOperand(Idx, Origin);
29780b57cec5SDimitry Andric     }
29790b57cec5SDimitry Andric   };
29800b57cec5SDimitry Andric 
29810b57cec5SDimitry Andric   /// Hide the operands of an instruction.
29820b57cec5SDimitry Andric   /// Do as if this instruction was not using any of its operands.
29830b57cec5SDimitry Andric   class OperandsHider : public TypePromotionAction {
29840b57cec5SDimitry Andric     /// The list of original operands.
29850b57cec5SDimitry Andric     SmallVector<Value *, 4> OriginalValues;
29860b57cec5SDimitry Andric 
29870b57cec5SDimitry Andric   public:
29880b57cec5SDimitry Andric     /// Remove \p Inst from the uses of the operands of \p Inst.
OperandsHider(Instruction * Inst)29890b57cec5SDimitry Andric     OperandsHider(Instruction *Inst) : TypePromotionAction(Inst) {
29900b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: OperandsHider: " << *Inst << "\n");
29910b57cec5SDimitry Andric       unsigned NumOpnds = Inst->getNumOperands();
29920b57cec5SDimitry Andric       OriginalValues.reserve(NumOpnds);
29930b57cec5SDimitry Andric       for (unsigned It = 0; It < NumOpnds; ++It) {
29940b57cec5SDimitry Andric         // Save the current operand.
29950b57cec5SDimitry Andric         Value *Val = Inst->getOperand(It);
29960b57cec5SDimitry Andric         OriginalValues.push_back(Val);
29970b57cec5SDimitry Andric         // Set a dummy one.
29980b57cec5SDimitry Andric         // We could use OperandSetter here, but that would imply an overhead
29990b57cec5SDimitry Andric         // that we are not willing to pay.
30000b57cec5SDimitry Andric         Inst->setOperand(It, UndefValue::get(Val->getType()));
30010b57cec5SDimitry Andric       }
30020b57cec5SDimitry Andric     }
30030b57cec5SDimitry Andric 
30040b57cec5SDimitry Andric     /// Restore the original list of uses.
undo()30050b57cec5SDimitry Andric     void undo() override {
30060b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: OperandsHider: " << *Inst << "\n");
30070b57cec5SDimitry Andric       for (unsigned It = 0, EndIt = OriginalValues.size(); It != EndIt; ++It)
30080b57cec5SDimitry Andric         Inst->setOperand(It, OriginalValues[It]);
30090b57cec5SDimitry Andric     }
30100b57cec5SDimitry Andric   };
30110b57cec5SDimitry Andric 
30120b57cec5SDimitry Andric   /// Build a truncate instruction.
30130b57cec5SDimitry Andric   class TruncBuilder : public TypePromotionAction {
30140b57cec5SDimitry Andric     Value *Val;
30150b57cec5SDimitry Andric 
30160b57cec5SDimitry Andric   public:
30170b57cec5SDimitry Andric     /// Build a truncate instruction of \p Opnd producing a \p Ty
30180b57cec5SDimitry Andric     /// result.
30190b57cec5SDimitry Andric     /// trunc Opnd to Ty.
TruncBuilder(Instruction * Opnd,Type * Ty)30200b57cec5SDimitry Andric     TruncBuilder(Instruction *Opnd, Type *Ty) : TypePromotionAction(Opnd) {
30210b57cec5SDimitry Andric       IRBuilder<> Builder(Opnd);
30225ffd83dbSDimitry Andric       Builder.SetCurrentDebugLocation(DebugLoc());
30230b57cec5SDimitry Andric       Val = Builder.CreateTrunc(Opnd, Ty, "promoted");
30240b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: TruncBuilder: " << *Val << "\n");
30250b57cec5SDimitry Andric     }
30260b57cec5SDimitry Andric 
30270b57cec5SDimitry Andric     /// Get the built value.
getBuiltValue()30280b57cec5SDimitry Andric     Value *getBuiltValue() { return Val; }
30290b57cec5SDimitry Andric 
30300b57cec5SDimitry Andric     /// Remove the built instruction.
undo()30310b57cec5SDimitry Andric     void undo() override {
30320b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: TruncBuilder: " << *Val << "\n");
30330b57cec5SDimitry Andric       if (Instruction *IVal = dyn_cast<Instruction>(Val))
30340b57cec5SDimitry Andric         IVal->eraseFromParent();
30350b57cec5SDimitry Andric     }
30360b57cec5SDimitry Andric   };
30370b57cec5SDimitry Andric 
30380b57cec5SDimitry Andric   /// Build a sign extension instruction.
30390b57cec5SDimitry Andric   class SExtBuilder : public TypePromotionAction {
30400b57cec5SDimitry Andric     Value *Val;
30410b57cec5SDimitry Andric 
30420b57cec5SDimitry Andric   public:
30430b57cec5SDimitry Andric     /// Build a sign extension instruction of \p Opnd producing a \p Ty
30440b57cec5SDimitry Andric     /// result.
30450b57cec5SDimitry Andric     /// sext Opnd to Ty.
SExtBuilder(Instruction * InsertPt,Value * Opnd,Type * Ty)30460b57cec5SDimitry Andric     SExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
30470b57cec5SDimitry Andric         : TypePromotionAction(InsertPt) {
30480b57cec5SDimitry Andric       IRBuilder<> Builder(InsertPt);
30490b57cec5SDimitry Andric       Val = Builder.CreateSExt(Opnd, Ty, "promoted");
30500b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: SExtBuilder: " << *Val << "\n");
30510b57cec5SDimitry Andric     }
30520b57cec5SDimitry Andric 
30530b57cec5SDimitry Andric     /// Get the built value.
getBuiltValue()30540b57cec5SDimitry Andric     Value *getBuiltValue() { return Val; }
30550b57cec5SDimitry Andric 
30560b57cec5SDimitry Andric     /// Remove the built instruction.
undo()30570b57cec5SDimitry Andric     void undo() override {
30580b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: SExtBuilder: " << *Val << "\n");
30590b57cec5SDimitry Andric       if (Instruction *IVal = dyn_cast<Instruction>(Val))
30600b57cec5SDimitry Andric         IVal->eraseFromParent();
30610b57cec5SDimitry Andric     }
30620b57cec5SDimitry Andric   };
30630b57cec5SDimitry Andric 
30640b57cec5SDimitry Andric   /// Build a zero extension instruction.
30650b57cec5SDimitry Andric   class ZExtBuilder : public TypePromotionAction {
30660b57cec5SDimitry Andric     Value *Val;
30670b57cec5SDimitry Andric 
30680b57cec5SDimitry Andric   public:
30690b57cec5SDimitry Andric     /// Build a zero extension instruction of \p Opnd producing a \p Ty
30700b57cec5SDimitry Andric     /// result.
30710b57cec5SDimitry Andric     /// zext Opnd to Ty.
ZExtBuilder(Instruction * InsertPt,Value * Opnd,Type * Ty)30720b57cec5SDimitry Andric     ZExtBuilder(Instruction *InsertPt, Value *Opnd, Type *Ty)
30730b57cec5SDimitry Andric         : TypePromotionAction(InsertPt) {
30740b57cec5SDimitry Andric       IRBuilder<> Builder(InsertPt);
30755ffd83dbSDimitry Andric       Builder.SetCurrentDebugLocation(DebugLoc());
30760b57cec5SDimitry Andric       Val = Builder.CreateZExt(Opnd, Ty, "promoted");
30770b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: ZExtBuilder: " << *Val << "\n");
30780b57cec5SDimitry Andric     }
30790b57cec5SDimitry Andric 
30800b57cec5SDimitry Andric     /// Get the built value.
getBuiltValue()30810b57cec5SDimitry Andric     Value *getBuiltValue() { return Val; }
30820b57cec5SDimitry Andric 
30830b57cec5SDimitry Andric     /// Remove the built instruction.
undo()30840b57cec5SDimitry Andric     void undo() override {
30850b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: ZExtBuilder: " << *Val << "\n");
30860b57cec5SDimitry Andric       if (Instruction *IVal = dyn_cast<Instruction>(Val))
30870b57cec5SDimitry Andric         IVal->eraseFromParent();
30880b57cec5SDimitry Andric     }
30890b57cec5SDimitry Andric   };
30900b57cec5SDimitry Andric 
30910b57cec5SDimitry Andric   /// Mutate an instruction to another type.
30920b57cec5SDimitry Andric   class TypeMutator : public TypePromotionAction {
30930b57cec5SDimitry Andric     /// Record the original type.
30940b57cec5SDimitry Andric     Type *OrigTy;
30950b57cec5SDimitry Andric 
30960b57cec5SDimitry Andric   public:
30970b57cec5SDimitry Andric     /// Mutate the type of \p Inst into \p NewTy.
TypeMutator(Instruction * Inst,Type * NewTy)30980b57cec5SDimitry Andric     TypeMutator(Instruction *Inst, Type *NewTy)
30990b57cec5SDimitry Andric         : TypePromotionAction(Inst), OrigTy(Inst->getType()) {
31000b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: MutateType: " << *Inst << " with " << *NewTy
31010b57cec5SDimitry Andric                         << "\n");
31020b57cec5SDimitry Andric       Inst->mutateType(NewTy);
31030b57cec5SDimitry Andric     }
31040b57cec5SDimitry Andric 
31050b57cec5SDimitry Andric     /// Mutate the instruction back to its original type.
undo()31060b57cec5SDimitry Andric     void undo() override {
31070b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: MutateType: " << *Inst << " with " << *OrigTy
31080b57cec5SDimitry Andric                         << "\n");
31090b57cec5SDimitry Andric       Inst->mutateType(OrigTy);
31100b57cec5SDimitry Andric     }
31110b57cec5SDimitry Andric   };
31120b57cec5SDimitry Andric 
31130b57cec5SDimitry Andric   /// Replace the uses of an instruction by another instruction.
31140b57cec5SDimitry Andric   class UsesReplacer : public TypePromotionAction {
31150b57cec5SDimitry Andric     /// Helper structure to keep track of the replaced uses.
31160b57cec5SDimitry Andric     struct InstructionAndIdx {
31170b57cec5SDimitry Andric       /// The instruction using the instruction.
31180b57cec5SDimitry Andric       Instruction *Inst;
31190b57cec5SDimitry Andric 
31200b57cec5SDimitry Andric       /// The index where this instruction is used for Inst.
31210b57cec5SDimitry Andric       unsigned Idx;
31220b57cec5SDimitry Andric 
InstructionAndIdx__anon01de4a8e0a11::TypePromotionTransaction::UsesReplacer::InstructionAndIdx31230b57cec5SDimitry Andric       InstructionAndIdx(Instruction *Inst, unsigned Idx)
31240b57cec5SDimitry Andric           : Inst(Inst), Idx(Idx) {}
31250b57cec5SDimitry Andric     };
31260b57cec5SDimitry Andric 
31270b57cec5SDimitry Andric     /// Keep track of the original uses (pair Instruction, Index).
31280b57cec5SDimitry Andric     SmallVector<InstructionAndIdx, 4> OriginalUses;
31290b57cec5SDimitry Andric     /// Keep track of the debug users.
31300b57cec5SDimitry Andric     SmallVector<DbgValueInst *, 1> DbgValues;
3131c9157d92SDimitry Andric     /// And non-instruction debug-users too.
3132c9157d92SDimitry Andric     SmallVector<DPValue *, 1> DPValues;
31330b57cec5SDimitry Andric 
3134fe6060f1SDimitry Andric     /// Keep track of the new value so that we can undo it by replacing
3135fe6060f1SDimitry Andric     /// instances of the new value with the original value.
3136fe6060f1SDimitry Andric     Value *New;
3137fe6060f1SDimitry Andric 
31380b57cec5SDimitry Andric     using use_iterator = SmallVectorImpl<InstructionAndIdx>::iterator;
31390b57cec5SDimitry Andric 
31400b57cec5SDimitry Andric   public:
31410b57cec5SDimitry Andric     /// Replace all the use of \p Inst by \p New.
UsesReplacer(Instruction * Inst,Value * New)3142fe6060f1SDimitry Andric     UsesReplacer(Instruction *Inst, Value *New)
3143fe6060f1SDimitry Andric         : TypePromotionAction(Inst), New(New) {
31440b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: UsersReplacer: " << *Inst << " with " << *New
31450b57cec5SDimitry Andric                         << "\n");
31460b57cec5SDimitry Andric       // Record the original uses.
31470b57cec5SDimitry Andric       for (Use &U : Inst->uses()) {
31480b57cec5SDimitry Andric         Instruction *UserI = cast<Instruction>(U.getUser());
31490b57cec5SDimitry Andric         OriginalUses.push_back(InstructionAndIdx(UserI, U.getOperandNo()));
31500b57cec5SDimitry Andric       }
31510b57cec5SDimitry Andric       // Record the debug uses separately. They are not in the instruction's
31520b57cec5SDimitry Andric       // use list, but they are replaced by RAUW.
3153c9157d92SDimitry Andric       findDbgValues(DbgValues, Inst, &DPValues);
31540b57cec5SDimitry Andric 
31550b57cec5SDimitry Andric       // Now, we can replace the uses.
31560b57cec5SDimitry Andric       Inst->replaceAllUsesWith(New);
31570b57cec5SDimitry Andric     }
31580b57cec5SDimitry Andric 
31590b57cec5SDimitry Andric     /// Reassign the original uses of Inst to Inst.
undo()31600b57cec5SDimitry Andric     void undo() override {
31610b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: UsersReplacer: " << *Inst << "\n");
3162fe6060f1SDimitry Andric       for (InstructionAndIdx &Use : OriginalUses)
3163fe6060f1SDimitry Andric         Use.Inst->setOperand(Use.Idx, Inst);
31640b57cec5SDimitry Andric       // RAUW has replaced all original uses with references to the new value,
31650b57cec5SDimitry Andric       // including the debug uses. Since we are undoing the replacements,
31660b57cec5SDimitry Andric       // the original debug uses must also be reinstated to maintain the
31670b57cec5SDimitry Andric       // correctness and utility of debug value instructions.
3168fe6060f1SDimitry Andric       for (auto *DVI : DbgValues)
3169fe6060f1SDimitry Andric         DVI->replaceVariableLocationOp(New, Inst);
3170c9157d92SDimitry Andric       // Similar story with DPValues, the non-instruction representation of
3171c9157d92SDimitry Andric       // dbg.values.
3172c9157d92SDimitry Andric       for (DPValue *DPV : DPValues) // tested by transaction-test I'm adding
3173c9157d92SDimitry Andric         DPV->replaceVariableLocationOp(New, Inst);
31740b57cec5SDimitry Andric     }
31750b57cec5SDimitry Andric   };
31760b57cec5SDimitry Andric 
31770b57cec5SDimitry Andric   /// Remove an instruction from the IR.
31780b57cec5SDimitry Andric   class InstructionRemover : public TypePromotionAction {
31790b57cec5SDimitry Andric     /// Original position of the instruction.
31800b57cec5SDimitry Andric     InsertionHandler Inserter;
31810b57cec5SDimitry Andric 
31820b57cec5SDimitry Andric     /// Helper structure to hide all the link to the instruction. In other
31830b57cec5SDimitry Andric     /// words, this helps to do as if the instruction was removed.
31840b57cec5SDimitry Andric     OperandsHider Hider;
31850b57cec5SDimitry Andric 
31860b57cec5SDimitry Andric     /// Keep track of the uses replaced, if any.
31870b57cec5SDimitry Andric     UsesReplacer *Replacer = nullptr;
31880b57cec5SDimitry Andric 
31890b57cec5SDimitry Andric     /// Keep track of instructions removed.
31900b57cec5SDimitry Andric     SetOfInstrs &RemovedInsts;
31910b57cec5SDimitry Andric 
31920b57cec5SDimitry Andric   public:
31930b57cec5SDimitry Andric     /// Remove all reference of \p Inst and optionally replace all its
31940b57cec5SDimitry Andric     /// uses with New.
31950b57cec5SDimitry Andric     /// \p RemovedInsts Keep track of the instructions removed by this Action.
31960b57cec5SDimitry Andric     /// \pre If !Inst->use_empty(), then New != nullptr
InstructionRemover(Instruction * Inst,SetOfInstrs & RemovedInsts,Value * New=nullptr)31970b57cec5SDimitry Andric     InstructionRemover(Instruction *Inst, SetOfInstrs &RemovedInsts,
31980b57cec5SDimitry Andric                        Value *New = nullptr)
31990b57cec5SDimitry Andric         : TypePromotionAction(Inst), Inserter(Inst), Hider(Inst),
32000b57cec5SDimitry Andric           RemovedInsts(RemovedInsts) {
32010b57cec5SDimitry Andric       if (New)
32020b57cec5SDimitry Andric         Replacer = new UsesReplacer(Inst, New);
32030b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Do: InstructionRemover: " << *Inst << "\n");
32040b57cec5SDimitry Andric       RemovedInsts.insert(Inst);
32050b57cec5SDimitry Andric       /// The instructions removed here will be freed after completing
32060b57cec5SDimitry Andric       /// optimizeBlock() for all blocks as we need to keep track of the
32070b57cec5SDimitry Andric       /// removed instructions during promotion.
32080b57cec5SDimitry Andric       Inst->removeFromParent();
32090b57cec5SDimitry Andric     }
32100b57cec5SDimitry Andric 
~InstructionRemover()32110b57cec5SDimitry Andric     ~InstructionRemover() override { delete Replacer; }
32120b57cec5SDimitry Andric 
3213fe013be4SDimitry Andric     InstructionRemover &operator=(const InstructionRemover &other) = delete;
3214fe013be4SDimitry Andric     InstructionRemover(const InstructionRemover &other) = delete;
3215fe013be4SDimitry Andric 
32160b57cec5SDimitry Andric     /// Resurrect the instruction and reassign it to the proper uses if
32170b57cec5SDimitry Andric     /// new value was provided when build this action.
undo()32180b57cec5SDimitry Andric     void undo() override {
32190b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Undo: InstructionRemover: " << *Inst << "\n");
32200b57cec5SDimitry Andric       Inserter.insert(Inst);
32210b57cec5SDimitry Andric       if (Replacer)
32220b57cec5SDimitry Andric         Replacer->undo();
32230b57cec5SDimitry Andric       Hider.undo();
32240b57cec5SDimitry Andric       RemovedInsts.erase(Inst);
32250b57cec5SDimitry Andric     }
32260b57cec5SDimitry Andric   };
32270b57cec5SDimitry Andric 
32280b57cec5SDimitry Andric public:
32290b57cec5SDimitry Andric   /// Restoration point.
32300b57cec5SDimitry Andric   /// The restoration point is a pointer to an action instead of an iterator
32310b57cec5SDimitry Andric   /// because the iterator may be invalidated but not the pointer.
32320b57cec5SDimitry Andric   using ConstRestorationPt = const TypePromotionAction *;
32330b57cec5SDimitry Andric 
TypePromotionTransaction(SetOfInstrs & RemovedInsts)32340b57cec5SDimitry Andric   TypePromotionTransaction(SetOfInstrs &RemovedInsts)
32350b57cec5SDimitry Andric       : RemovedInsts(RemovedInsts) {}
32360b57cec5SDimitry Andric 
32375ffd83dbSDimitry Andric   /// Advocate every changes made in that transaction. Return true if any change
32385ffd83dbSDimitry Andric   /// happen.
32395ffd83dbSDimitry Andric   bool commit();
32400b57cec5SDimitry Andric 
32410b57cec5SDimitry Andric   /// Undo all the changes made after the given point.
32420b57cec5SDimitry Andric   void rollback(ConstRestorationPt Point);
32430b57cec5SDimitry Andric 
32440b57cec5SDimitry Andric   /// Get the current restoration point.
32450b57cec5SDimitry Andric   ConstRestorationPt getRestorationPoint() const;
32460b57cec5SDimitry Andric 
32470b57cec5SDimitry Andric   /// \name API for IR modification with state keeping to support rollback.
32480b57cec5SDimitry Andric   /// @{
32490b57cec5SDimitry Andric   /// Same as Instruction::setOperand.
32500b57cec5SDimitry Andric   void setOperand(Instruction *Inst, unsigned Idx, Value *NewVal);
32510b57cec5SDimitry Andric 
32520b57cec5SDimitry Andric   /// Same as Instruction::eraseFromParent.
32530b57cec5SDimitry Andric   void eraseInstruction(Instruction *Inst, Value *NewVal = nullptr);
32540b57cec5SDimitry Andric 
32550b57cec5SDimitry Andric   /// Same as Value::replaceAllUsesWith.
32560b57cec5SDimitry Andric   void replaceAllUsesWith(Instruction *Inst, Value *New);
32570b57cec5SDimitry Andric 
32580b57cec5SDimitry Andric   /// Same as Value::mutateType.
32590b57cec5SDimitry Andric   void mutateType(Instruction *Inst, Type *NewTy);
32600b57cec5SDimitry Andric 
32610b57cec5SDimitry Andric   /// Same as IRBuilder::createTrunc.
32620b57cec5SDimitry Andric   Value *createTrunc(Instruction *Opnd, Type *Ty);
32630b57cec5SDimitry Andric 
32640b57cec5SDimitry Andric   /// Same as IRBuilder::createSExt.
32650b57cec5SDimitry Andric   Value *createSExt(Instruction *Inst, Value *Opnd, Type *Ty);
32660b57cec5SDimitry Andric 
32670b57cec5SDimitry Andric   /// Same as IRBuilder::createZExt.
32680b57cec5SDimitry Andric   Value *createZExt(Instruction *Inst, Value *Opnd, Type *Ty);
32690b57cec5SDimitry Andric 
32700b57cec5SDimitry Andric private:
32710b57cec5SDimitry Andric   /// The ordered list of actions made so far.
32720b57cec5SDimitry Andric   SmallVector<std::unique_ptr<TypePromotionAction>, 16> Actions;
32730b57cec5SDimitry Andric 
3274bdd1243dSDimitry Andric   using CommitPt =
3275bdd1243dSDimitry Andric       SmallVectorImpl<std::unique_ptr<TypePromotionAction>>::iterator;
32760b57cec5SDimitry Andric 
32770b57cec5SDimitry Andric   SetOfInstrs &RemovedInsts;
32780b57cec5SDimitry Andric };
32790b57cec5SDimitry Andric 
32800b57cec5SDimitry Andric } // end anonymous namespace
32810b57cec5SDimitry Andric 
setOperand(Instruction * Inst,unsigned Idx,Value * NewVal)32820b57cec5SDimitry Andric void TypePromotionTransaction::setOperand(Instruction *Inst, unsigned Idx,
32830b57cec5SDimitry Andric                                           Value *NewVal) {
32848bcb0991SDimitry Andric   Actions.push_back(std::make_unique<TypePromotionTransaction::OperandSetter>(
32850b57cec5SDimitry Andric       Inst, Idx, NewVal));
32860b57cec5SDimitry Andric }
32870b57cec5SDimitry Andric 
eraseInstruction(Instruction * Inst,Value * NewVal)32880b57cec5SDimitry Andric void TypePromotionTransaction::eraseInstruction(Instruction *Inst,
32890b57cec5SDimitry Andric                                                 Value *NewVal) {
32900b57cec5SDimitry Andric   Actions.push_back(
32918bcb0991SDimitry Andric       std::make_unique<TypePromotionTransaction::InstructionRemover>(
32920b57cec5SDimitry Andric           Inst, RemovedInsts, NewVal));
32930b57cec5SDimitry Andric }
32940b57cec5SDimitry Andric 
replaceAllUsesWith(Instruction * Inst,Value * New)32950b57cec5SDimitry Andric void TypePromotionTransaction::replaceAllUsesWith(Instruction *Inst,
32960b57cec5SDimitry Andric                                                   Value *New) {
32970b57cec5SDimitry Andric   Actions.push_back(
32988bcb0991SDimitry Andric       std::make_unique<TypePromotionTransaction::UsesReplacer>(Inst, New));
32990b57cec5SDimitry Andric }
33000b57cec5SDimitry Andric 
mutateType(Instruction * Inst,Type * NewTy)33010b57cec5SDimitry Andric void TypePromotionTransaction::mutateType(Instruction *Inst, Type *NewTy) {
33020b57cec5SDimitry Andric   Actions.push_back(
33038bcb0991SDimitry Andric       std::make_unique<TypePromotionTransaction::TypeMutator>(Inst, NewTy));
33040b57cec5SDimitry Andric }
33050b57cec5SDimitry Andric 
createTrunc(Instruction * Opnd,Type * Ty)3306bdd1243dSDimitry Andric Value *TypePromotionTransaction::createTrunc(Instruction *Opnd, Type *Ty) {
33070b57cec5SDimitry Andric   std::unique_ptr<TruncBuilder> Ptr(new TruncBuilder(Opnd, Ty));
33080b57cec5SDimitry Andric   Value *Val = Ptr->getBuiltValue();
33090b57cec5SDimitry Andric   Actions.push_back(std::move(Ptr));
33100b57cec5SDimitry Andric   return Val;
33110b57cec5SDimitry Andric }
33120b57cec5SDimitry Andric 
createSExt(Instruction * Inst,Value * Opnd,Type * Ty)3313bdd1243dSDimitry Andric Value *TypePromotionTransaction::createSExt(Instruction *Inst, Value *Opnd,
3314bdd1243dSDimitry Andric                                             Type *Ty) {
33150b57cec5SDimitry Andric   std::unique_ptr<SExtBuilder> Ptr(new SExtBuilder(Inst, Opnd, Ty));
33160b57cec5SDimitry Andric   Value *Val = Ptr->getBuiltValue();
33170b57cec5SDimitry Andric   Actions.push_back(std::move(Ptr));
33180b57cec5SDimitry Andric   return Val;
33190b57cec5SDimitry Andric }
33200b57cec5SDimitry Andric 
createZExt(Instruction * Inst,Value * Opnd,Type * Ty)3321bdd1243dSDimitry Andric Value *TypePromotionTransaction::createZExt(Instruction *Inst, Value *Opnd,
3322bdd1243dSDimitry Andric                                             Type *Ty) {
33230b57cec5SDimitry Andric   std::unique_ptr<ZExtBuilder> Ptr(new ZExtBuilder(Inst, Opnd, Ty));
33240b57cec5SDimitry Andric   Value *Val = Ptr->getBuiltValue();
33250b57cec5SDimitry Andric   Actions.push_back(std::move(Ptr));
33260b57cec5SDimitry Andric   return Val;
33270b57cec5SDimitry Andric }
33280b57cec5SDimitry Andric 
33290b57cec5SDimitry Andric TypePromotionTransaction::ConstRestorationPt
getRestorationPoint() const33300b57cec5SDimitry Andric TypePromotionTransaction::getRestorationPoint() const {
33310b57cec5SDimitry Andric   return !Actions.empty() ? Actions.back().get() : nullptr;
33320b57cec5SDimitry Andric }
33330b57cec5SDimitry Andric 
commit()33345ffd83dbSDimitry Andric bool TypePromotionTransaction::commit() {
3335fe6060f1SDimitry Andric   for (std::unique_ptr<TypePromotionAction> &Action : Actions)
3336fe6060f1SDimitry Andric     Action->commit();
33375ffd83dbSDimitry Andric   bool Modified = !Actions.empty();
33380b57cec5SDimitry Andric   Actions.clear();
33395ffd83dbSDimitry Andric   return Modified;
33400b57cec5SDimitry Andric }
33410b57cec5SDimitry Andric 
rollback(TypePromotionTransaction::ConstRestorationPt Point)33420b57cec5SDimitry Andric void TypePromotionTransaction::rollback(
33430b57cec5SDimitry Andric     TypePromotionTransaction::ConstRestorationPt Point) {
33440b57cec5SDimitry Andric   while (!Actions.empty() && Point != Actions.back().get()) {
33450b57cec5SDimitry Andric     std::unique_ptr<TypePromotionAction> Curr = Actions.pop_back_val();
33460b57cec5SDimitry Andric     Curr->undo();
33470b57cec5SDimitry Andric   }
33480b57cec5SDimitry Andric }
33490b57cec5SDimitry Andric 
33500b57cec5SDimitry Andric namespace {
33510b57cec5SDimitry Andric 
33520b57cec5SDimitry Andric /// A helper class for matching addressing modes.
33530b57cec5SDimitry Andric ///
33540b57cec5SDimitry Andric /// This encapsulates the logic for matching the target-legal addressing modes.
33550b57cec5SDimitry Andric class AddressingModeMatcher {
33560b57cec5SDimitry Andric   SmallVectorImpl<Instruction *> &AddrModeInsts;
33570b57cec5SDimitry Andric   const TargetLowering &TLI;
33580b57cec5SDimitry Andric   const TargetRegisterInfo &TRI;
33590b57cec5SDimitry Andric   const DataLayout &DL;
3360fe6060f1SDimitry Andric   const LoopInfo &LI;
3361fe6060f1SDimitry Andric   const std::function<const DominatorTree &()> getDTFn;
33620b57cec5SDimitry Andric 
33630b57cec5SDimitry Andric   /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
33640b57cec5SDimitry Andric   /// the memory instruction that we're computing this address for.
33650b57cec5SDimitry Andric   Type *AccessTy;
33660b57cec5SDimitry Andric   unsigned AddrSpace;
33670b57cec5SDimitry Andric   Instruction *MemoryInst;
33680b57cec5SDimitry Andric 
33690b57cec5SDimitry Andric   /// This is the addressing mode that we're building up. This is
33700b57cec5SDimitry Andric   /// part of the return value of this addressing mode matching stuff.
33710b57cec5SDimitry Andric   ExtAddrMode &AddrMode;
33720b57cec5SDimitry Andric 
33730b57cec5SDimitry Andric   /// The instructions inserted by other CodeGenPrepare optimizations.
33740b57cec5SDimitry Andric   const SetOfInstrs &InsertedInsts;
33750b57cec5SDimitry Andric 
33760b57cec5SDimitry Andric   /// A map from the instructions to their type before promotion.
33770b57cec5SDimitry Andric   InstrToOrigTy &PromotedInsts;
33780b57cec5SDimitry Andric 
33790b57cec5SDimitry Andric   /// The ongoing transaction where every action should be registered.
33800b57cec5SDimitry Andric   TypePromotionTransaction &TPT;
33810b57cec5SDimitry Andric 
33820b57cec5SDimitry Andric   // A GEP which has too large offset to be folded into the addressing mode.
33830b57cec5SDimitry Andric   std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP;
33840b57cec5SDimitry Andric 
33850b57cec5SDimitry Andric   /// This is set to true when we should not do profitability checks.
33860b57cec5SDimitry Andric   /// When true, IsProfitableToFoldIntoAddressingMode always returns true.
33870b57cec5SDimitry Andric   bool IgnoreProfitability;
33880b57cec5SDimitry Andric 
3389480093f4SDimitry Andric   /// True if we are optimizing for size.
3390fe013be4SDimitry Andric   bool OptSize = false;
3391480093f4SDimitry Andric 
3392480093f4SDimitry Andric   ProfileSummaryInfo *PSI;
3393480093f4SDimitry Andric   BlockFrequencyInfo *BFI;
3394480093f4SDimitry Andric 
AddressingModeMatcher(SmallVectorImpl<Instruction * > & AMI,const TargetLowering & TLI,const TargetRegisterInfo & TRI,const LoopInfo & LI,const std::function<const DominatorTree & ()> getDTFn,Type * AT,unsigned AS,Instruction * MI,ExtAddrMode & AM,const SetOfInstrs & InsertedInsts,InstrToOrigTy & PromotedInsts,TypePromotionTransaction & TPT,std::pair<AssertingVH<GetElementPtrInst>,int64_t> & LargeOffsetGEP,bool OptSize,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI)33950b57cec5SDimitry Andric   AddressingModeMatcher(
33960b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> &AMI, const TargetLowering &TLI,
3397fe6060f1SDimitry Andric       const TargetRegisterInfo &TRI, const LoopInfo &LI,
3398bdd1243dSDimitry Andric       const std::function<const DominatorTree &()> getDTFn, Type *AT,
3399bdd1243dSDimitry Andric       unsigned AS, Instruction *MI, ExtAddrMode &AM,
3400fe6060f1SDimitry Andric       const SetOfInstrs &InsertedInsts, InstrToOrigTy &PromotedInsts,
3401fe6060f1SDimitry Andric       TypePromotionTransaction &TPT,
3402480093f4SDimitry Andric       std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP,
3403480093f4SDimitry Andric       bool OptSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI)
34040b57cec5SDimitry Andric       : AddrModeInsts(AMI), TLI(TLI), TRI(TRI),
3405fe6060f1SDimitry Andric         DL(MI->getModule()->getDataLayout()), LI(LI), getDTFn(getDTFn),
3406fe6060f1SDimitry Andric         AccessTy(AT), AddrSpace(AS), MemoryInst(MI), AddrMode(AM),
3407fe6060f1SDimitry Andric         InsertedInsts(InsertedInsts), PromotedInsts(PromotedInsts), TPT(TPT),
3408fe6060f1SDimitry Andric         LargeOffsetGEP(LargeOffsetGEP), OptSize(OptSize), PSI(PSI), BFI(BFI) {
34090b57cec5SDimitry Andric     IgnoreProfitability = false;
34100b57cec5SDimitry Andric   }
34110b57cec5SDimitry Andric 
34120b57cec5SDimitry Andric public:
34130b57cec5SDimitry Andric   /// Find the maximal addressing mode that a load/store of V can fold,
34140b57cec5SDimitry Andric   /// give an access type of AccessTy.  This returns a list of involved
34150b57cec5SDimitry Andric   /// instructions in AddrModeInsts.
34160b57cec5SDimitry Andric   /// \p InsertedInsts The instructions inserted by other CodeGenPrepare
34170b57cec5SDimitry Andric   /// optimizations.
34180b57cec5SDimitry Andric   /// \p PromotedInsts maps the instructions to their type before promotion.
34190b57cec5SDimitry Andric   /// \p The ongoing transaction where every action should be registered.
34200b57cec5SDimitry Andric   static ExtAddrMode
Match(Value * V,Type * AccessTy,unsigned AS,Instruction * MemoryInst,SmallVectorImpl<Instruction * > & AddrModeInsts,const TargetLowering & TLI,const LoopInfo & LI,const std::function<const DominatorTree & ()> getDTFn,const TargetRegisterInfo & TRI,const SetOfInstrs & InsertedInsts,InstrToOrigTy & PromotedInsts,TypePromotionTransaction & TPT,std::pair<AssertingVH<GetElementPtrInst>,int64_t> & LargeOffsetGEP,bool OptSize,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI)34210b57cec5SDimitry Andric   Match(Value *V, Type *AccessTy, unsigned AS, Instruction *MemoryInst,
34220b57cec5SDimitry Andric         SmallVectorImpl<Instruction *> &AddrModeInsts,
3423fe6060f1SDimitry Andric         const TargetLowering &TLI, const LoopInfo &LI,
3424fe6060f1SDimitry Andric         const std::function<const DominatorTree &()> getDTFn,
3425fe6060f1SDimitry Andric         const TargetRegisterInfo &TRI, const SetOfInstrs &InsertedInsts,
3426fe6060f1SDimitry Andric         InstrToOrigTy &PromotedInsts, TypePromotionTransaction &TPT,
3427480093f4SDimitry Andric         std::pair<AssertingVH<GetElementPtrInst>, int64_t> &LargeOffsetGEP,
3428480093f4SDimitry Andric         bool OptSize, ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) {
34290b57cec5SDimitry Andric     ExtAddrMode Result;
34300b57cec5SDimitry Andric 
3431bdd1243dSDimitry Andric     bool Success = AddressingModeMatcher(AddrModeInsts, TLI, TRI, LI, getDTFn,
3432bdd1243dSDimitry Andric                                          AccessTy, AS, MemoryInst, Result,
3433bdd1243dSDimitry Andric                                          InsertedInsts, PromotedInsts, TPT,
3434bdd1243dSDimitry Andric                                          LargeOffsetGEP, OptSize, PSI, BFI)
3435bdd1243dSDimitry Andric                        .matchAddr(V, 0);
3436bdd1243dSDimitry Andric     (void)Success;
3437bdd1243dSDimitry Andric     assert(Success && "Couldn't select *anything*?");
34380b57cec5SDimitry Andric     return Result;
34390b57cec5SDimitry Andric   }
34400b57cec5SDimitry Andric 
34410b57cec5SDimitry Andric private:
34420b57cec5SDimitry Andric   bool matchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
34430b57cec5SDimitry Andric   bool matchAddr(Value *Addr, unsigned Depth);
34440b57cec5SDimitry Andric   bool matchOperationAddr(User *AddrInst, unsigned Opcode, unsigned Depth,
34450b57cec5SDimitry Andric                           bool *MovedAway = nullptr);
34460b57cec5SDimitry Andric   bool isProfitableToFoldIntoAddressingMode(Instruction *I,
34470b57cec5SDimitry Andric                                             ExtAddrMode &AMBefore,
34480b57cec5SDimitry Andric                                             ExtAddrMode &AMAfter);
34490b57cec5SDimitry Andric   bool valueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
34500b57cec5SDimitry Andric   bool isPromotionProfitable(unsigned NewCost, unsigned OldCost,
34510b57cec5SDimitry Andric                              Value *PromotedOperand) const;
34520b57cec5SDimitry Andric };
34530b57cec5SDimitry Andric 
34540b57cec5SDimitry Andric class PhiNodeSet;
34550b57cec5SDimitry Andric 
34560b57cec5SDimitry Andric /// An iterator for PhiNodeSet.
34570b57cec5SDimitry Andric class PhiNodeSetIterator {
34580b57cec5SDimitry Andric   PhiNodeSet *const Set;
34590b57cec5SDimitry Andric   size_t CurrentIndex = 0;
34600b57cec5SDimitry Andric 
34610b57cec5SDimitry Andric public:
34620b57cec5SDimitry Andric   /// The constructor. Start should point to either a valid element, or be equal
34630b57cec5SDimitry Andric   /// to the size of the underlying SmallVector of the PhiNodeSet.
34640b57cec5SDimitry Andric   PhiNodeSetIterator(PhiNodeSet *const Set, size_t Start);
34650b57cec5SDimitry Andric   PHINode *operator*() const;
34660b57cec5SDimitry Andric   PhiNodeSetIterator &operator++();
34670b57cec5SDimitry Andric   bool operator==(const PhiNodeSetIterator &RHS) const;
34680b57cec5SDimitry Andric   bool operator!=(const PhiNodeSetIterator &RHS) const;
34690b57cec5SDimitry Andric };
34700b57cec5SDimitry Andric 
34710b57cec5SDimitry Andric /// Keeps a set of PHINodes.
34720b57cec5SDimitry Andric ///
34730b57cec5SDimitry Andric /// This is a minimal set implementation for a specific use case:
34740b57cec5SDimitry Andric /// It is very fast when there are very few elements, but also provides good
34750b57cec5SDimitry Andric /// performance when there are many. It is similar to SmallPtrSet, but also
34760b57cec5SDimitry Andric /// provides iteration by insertion order, which is deterministic and stable
34770b57cec5SDimitry Andric /// across runs. It is also similar to SmallSetVector, but provides removing
34780b57cec5SDimitry Andric /// elements in O(1) time. This is achieved by not actually removing the element
34790b57cec5SDimitry Andric /// from the underlying vector, so comes at the cost of using more memory, but
34800b57cec5SDimitry Andric /// that is fine, since PhiNodeSets are used as short lived objects.
34810b57cec5SDimitry Andric class PhiNodeSet {
34820b57cec5SDimitry Andric   friend class PhiNodeSetIterator;
34830b57cec5SDimitry Andric 
34840b57cec5SDimitry Andric   using MapType = SmallDenseMap<PHINode *, size_t, 32>;
34850b57cec5SDimitry Andric   using iterator = PhiNodeSetIterator;
34860b57cec5SDimitry Andric 
34870b57cec5SDimitry Andric   /// Keeps the elements in the order of their insertion in the underlying
34880b57cec5SDimitry Andric   /// vector. To achieve constant time removal, it never deletes any element.
34890b57cec5SDimitry Andric   SmallVector<PHINode *, 32> NodeList;
34900b57cec5SDimitry Andric 
34910b57cec5SDimitry Andric   /// Keeps the elements in the underlying set implementation. This (and not the
34920b57cec5SDimitry Andric   /// NodeList defined above) is the source of truth on whether an element
34930b57cec5SDimitry Andric   /// is actually in the collection.
34940b57cec5SDimitry Andric   MapType NodeMap;
34950b57cec5SDimitry Andric 
34960b57cec5SDimitry Andric   /// Points to the first valid (not deleted) element when the set is not empty
34970b57cec5SDimitry Andric   /// and the value is not zero. Equals to the size of the underlying vector
34980b57cec5SDimitry Andric   /// when the set is empty. When the value is 0, as in the beginning, the
34990b57cec5SDimitry Andric   /// first element may or may not be valid.
35000b57cec5SDimitry Andric   size_t FirstValidElement = 0;
35010b57cec5SDimitry Andric 
35020b57cec5SDimitry Andric public:
35030b57cec5SDimitry Andric   /// Inserts a new element to the collection.
35040b57cec5SDimitry Andric   /// \returns true if the element is actually added, i.e. was not in the
35050b57cec5SDimitry Andric   /// collection before the operation.
insert(PHINode * Ptr)35060b57cec5SDimitry Andric   bool insert(PHINode *Ptr) {
35070b57cec5SDimitry Andric     if (NodeMap.insert(std::make_pair(Ptr, NodeList.size())).second) {
35080b57cec5SDimitry Andric       NodeList.push_back(Ptr);
35090b57cec5SDimitry Andric       return true;
35100b57cec5SDimitry Andric     }
35110b57cec5SDimitry Andric     return false;
35120b57cec5SDimitry Andric   }
35130b57cec5SDimitry Andric 
35140b57cec5SDimitry Andric   /// Removes the element from the collection.
35150b57cec5SDimitry Andric   /// \returns whether the element is actually removed, i.e. was in the
35160b57cec5SDimitry Andric   /// collection before the operation.
erase(PHINode * Ptr)35170b57cec5SDimitry Andric   bool erase(PHINode *Ptr) {
3518e8d8bef9SDimitry Andric     if (NodeMap.erase(Ptr)) {
35190b57cec5SDimitry Andric       SkipRemovedElements(FirstValidElement);
35200b57cec5SDimitry Andric       return true;
35210b57cec5SDimitry Andric     }
35220b57cec5SDimitry Andric     return false;
35230b57cec5SDimitry Andric   }
35240b57cec5SDimitry Andric 
35250b57cec5SDimitry Andric   /// Removes all elements and clears the collection.
clear()35260b57cec5SDimitry Andric   void clear() {
35270b57cec5SDimitry Andric     NodeMap.clear();
35280b57cec5SDimitry Andric     NodeList.clear();
35290b57cec5SDimitry Andric     FirstValidElement = 0;
35300b57cec5SDimitry Andric   }
35310b57cec5SDimitry Andric 
35320b57cec5SDimitry Andric   /// \returns an iterator that will iterate the elements in the order of
35330b57cec5SDimitry Andric   /// insertion.
begin()35340b57cec5SDimitry Andric   iterator begin() {
35350b57cec5SDimitry Andric     if (FirstValidElement == 0)
35360b57cec5SDimitry Andric       SkipRemovedElements(FirstValidElement);
35370b57cec5SDimitry Andric     return PhiNodeSetIterator(this, FirstValidElement);
35380b57cec5SDimitry Andric   }
35390b57cec5SDimitry Andric 
35400b57cec5SDimitry Andric   /// \returns an iterator that points to the end of the collection.
end()35410b57cec5SDimitry Andric   iterator end() { return PhiNodeSetIterator(this, NodeList.size()); }
35420b57cec5SDimitry Andric 
35430b57cec5SDimitry Andric   /// Returns the number of elements in the collection.
size() const3544bdd1243dSDimitry Andric   size_t size() const { return NodeMap.size(); }
35450b57cec5SDimitry Andric 
35460b57cec5SDimitry Andric   /// \returns 1 if the given element is in the collection, and 0 if otherwise.
count(PHINode * Ptr) const3547bdd1243dSDimitry Andric   size_t count(PHINode *Ptr) const { return NodeMap.count(Ptr); }
35480b57cec5SDimitry Andric 
35490b57cec5SDimitry Andric private:
35500b57cec5SDimitry Andric   /// Updates the CurrentIndex so that it will point to a valid element.
35510b57cec5SDimitry Andric   ///
35520b57cec5SDimitry Andric   /// If the element of NodeList at CurrentIndex is valid, it does not
35530b57cec5SDimitry Andric   /// change it. If there are no more valid elements, it updates CurrentIndex
35540b57cec5SDimitry Andric   /// to point to the end of the NodeList.
SkipRemovedElements(size_t & CurrentIndex)35550b57cec5SDimitry Andric   void SkipRemovedElements(size_t &CurrentIndex) {
35560b57cec5SDimitry Andric     while (CurrentIndex < NodeList.size()) {
35570b57cec5SDimitry Andric       auto it = NodeMap.find(NodeList[CurrentIndex]);
35580b57cec5SDimitry Andric       // If the element has been deleted and added again later, NodeMap will
35590b57cec5SDimitry Andric       // point to a different index, so CurrentIndex will still be invalid.
35600b57cec5SDimitry Andric       if (it != NodeMap.end() && it->second == CurrentIndex)
35610b57cec5SDimitry Andric         break;
35620b57cec5SDimitry Andric       ++CurrentIndex;
35630b57cec5SDimitry Andric     }
35640b57cec5SDimitry Andric   }
35650b57cec5SDimitry Andric };
35660b57cec5SDimitry Andric 
PhiNodeSetIterator(PhiNodeSet * const Set,size_t Start)35670b57cec5SDimitry Andric PhiNodeSetIterator::PhiNodeSetIterator(PhiNodeSet *const Set, size_t Start)
35680b57cec5SDimitry Andric     : Set(Set), CurrentIndex(Start) {}
35690b57cec5SDimitry Andric 
operator *() const35700b57cec5SDimitry Andric PHINode *PhiNodeSetIterator::operator*() const {
35710b57cec5SDimitry Andric   assert(CurrentIndex < Set->NodeList.size() &&
35720b57cec5SDimitry Andric          "PhiNodeSet access out of range");
35730b57cec5SDimitry Andric   return Set->NodeList[CurrentIndex];
35740b57cec5SDimitry Andric }
35750b57cec5SDimitry Andric 
operator ++()35760b57cec5SDimitry Andric PhiNodeSetIterator &PhiNodeSetIterator::operator++() {
35770b57cec5SDimitry Andric   assert(CurrentIndex < Set->NodeList.size() &&
35780b57cec5SDimitry Andric          "PhiNodeSet access out of range");
35790b57cec5SDimitry Andric   ++CurrentIndex;
35800b57cec5SDimitry Andric   Set->SkipRemovedElements(CurrentIndex);
35810b57cec5SDimitry Andric   return *this;
35820b57cec5SDimitry Andric }
35830b57cec5SDimitry Andric 
operator ==(const PhiNodeSetIterator & RHS) const35840b57cec5SDimitry Andric bool PhiNodeSetIterator::operator==(const PhiNodeSetIterator &RHS) const {
35850b57cec5SDimitry Andric   return CurrentIndex == RHS.CurrentIndex;
35860b57cec5SDimitry Andric }
35870b57cec5SDimitry Andric 
operator !=(const PhiNodeSetIterator & RHS) const35880b57cec5SDimitry Andric bool PhiNodeSetIterator::operator!=(const PhiNodeSetIterator &RHS) const {
35890b57cec5SDimitry Andric   return !((*this) == RHS);
35900b57cec5SDimitry Andric }
35910b57cec5SDimitry Andric 
35920b57cec5SDimitry Andric /// Keep track of simplification of Phi nodes.
35930b57cec5SDimitry Andric /// Accept the set of all phi nodes and erase phi node from this set
35940b57cec5SDimitry Andric /// if it is simplified.
35950b57cec5SDimitry Andric class SimplificationTracker {
35960b57cec5SDimitry Andric   DenseMap<Value *, Value *> Storage;
35970b57cec5SDimitry Andric   const SimplifyQuery &SQ;
35980b57cec5SDimitry Andric   // Tracks newly created Phi nodes. The elements are iterated by insertion
35990b57cec5SDimitry Andric   // order.
36000b57cec5SDimitry Andric   PhiNodeSet AllPhiNodes;
36010b57cec5SDimitry Andric   // Tracks newly created Select nodes.
36020b57cec5SDimitry Andric   SmallPtrSet<SelectInst *, 32> AllSelectNodes;
36030b57cec5SDimitry Andric 
36040b57cec5SDimitry Andric public:
SimplificationTracker(const SimplifyQuery & sq)3605bdd1243dSDimitry Andric   SimplificationTracker(const SimplifyQuery &sq) : SQ(sq) {}
36060b57cec5SDimitry Andric 
Get(Value * V)36070b57cec5SDimitry Andric   Value *Get(Value *V) {
36080b57cec5SDimitry Andric     do {
36090b57cec5SDimitry Andric       auto SV = Storage.find(V);
36100b57cec5SDimitry Andric       if (SV == Storage.end())
36110b57cec5SDimitry Andric         return V;
36120b57cec5SDimitry Andric       V = SV->second;
36130b57cec5SDimitry Andric     } while (true);
36140b57cec5SDimitry Andric   }
36150b57cec5SDimitry Andric 
Simplify(Value * Val)36160b57cec5SDimitry Andric   Value *Simplify(Value *Val) {
36170b57cec5SDimitry Andric     SmallVector<Value *, 32> WorkList;
36180b57cec5SDimitry Andric     SmallPtrSet<Value *, 32> Visited;
36190b57cec5SDimitry Andric     WorkList.push_back(Val);
36200b57cec5SDimitry Andric     while (!WorkList.empty()) {
36215ffd83dbSDimitry Andric       auto *P = WorkList.pop_back_val();
36220b57cec5SDimitry Andric       if (!Visited.insert(P).second)
36230b57cec5SDimitry Andric         continue;
36240b57cec5SDimitry Andric       if (auto *PI = dyn_cast<Instruction>(P))
362581ad6265SDimitry Andric         if (Value *V = simplifyInstruction(cast<Instruction>(PI), SQ)) {
36260b57cec5SDimitry Andric           for (auto *U : PI->users())
36270b57cec5SDimitry Andric             WorkList.push_back(cast<Value>(U));
36280b57cec5SDimitry Andric           Put(PI, V);
36290b57cec5SDimitry Andric           PI->replaceAllUsesWith(V);
36300b57cec5SDimitry Andric           if (auto *PHI = dyn_cast<PHINode>(PI))
36310b57cec5SDimitry Andric             AllPhiNodes.erase(PHI);
36320b57cec5SDimitry Andric           if (auto *Select = dyn_cast<SelectInst>(PI))
36330b57cec5SDimitry Andric             AllSelectNodes.erase(Select);
36340b57cec5SDimitry Andric           PI->eraseFromParent();
36350b57cec5SDimitry Andric         }
36360b57cec5SDimitry Andric     }
36370b57cec5SDimitry Andric     return Get(Val);
36380b57cec5SDimitry Andric   }
36390b57cec5SDimitry Andric 
Put(Value * From,Value * To)3640bdd1243dSDimitry Andric   void Put(Value *From, Value *To) { Storage.insert({From, To}); }
36410b57cec5SDimitry Andric 
ReplacePhi(PHINode * From,PHINode * To)36420b57cec5SDimitry Andric   void ReplacePhi(PHINode *From, PHINode *To) {
36430b57cec5SDimitry Andric     Value *OldReplacement = Get(From);
36440b57cec5SDimitry Andric     while (OldReplacement != From) {
36450b57cec5SDimitry Andric       From = To;
36460b57cec5SDimitry Andric       To = dyn_cast<PHINode>(OldReplacement);
36470b57cec5SDimitry Andric       OldReplacement = Get(From);
36480b57cec5SDimitry Andric     }
36498bcb0991SDimitry Andric     assert(To && Get(To) == To && "Replacement PHI node is already replaced.");
36500b57cec5SDimitry Andric     Put(From, To);
36510b57cec5SDimitry Andric     From->replaceAllUsesWith(To);
36520b57cec5SDimitry Andric     AllPhiNodes.erase(From);
36530b57cec5SDimitry Andric     From->eraseFromParent();
36540b57cec5SDimitry Andric   }
36550b57cec5SDimitry Andric 
newPhiNodes()36560b57cec5SDimitry Andric   PhiNodeSet &newPhiNodes() { return AllPhiNodes; }
36570b57cec5SDimitry Andric 
insertNewPhi(PHINode * PN)36580b57cec5SDimitry Andric   void insertNewPhi(PHINode *PN) { AllPhiNodes.insert(PN); }
36590b57cec5SDimitry Andric 
insertNewSelect(SelectInst * SI)36600b57cec5SDimitry Andric   void insertNewSelect(SelectInst *SI) { AllSelectNodes.insert(SI); }
36610b57cec5SDimitry Andric 
countNewPhiNodes() const36620b57cec5SDimitry Andric   unsigned countNewPhiNodes() const { return AllPhiNodes.size(); }
36630b57cec5SDimitry Andric 
countNewSelectNodes() const36640b57cec5SDimitry Andric   unsigned countNewSelectNodes() const { return AllSelectNodes.size(); }
36650b57cec5SDimitry Andric 
destroyNewNodes(Type * CommonType)36660b57cec5SDimitry Andric   void destroyNewNodes(Type *CommonType) {
36670b57cec5SDimitry Andric     // For safe erasing, replace the uses with dummy value first.
366881ad6265SDimitry Andric     auto *Dummy = PoisonValue::get(CommonType);
36695ffd83dbSDimitry Andric     for (auto *I : AllPhiNodes) {
36700b57cec5SDimitry Andric       I->replaceAllUsesWith(Dummy);
36710b57cec5SDimitry Andric       I->eraseFromParent();
36720b57cec5SDimitry Andric     }
36730b57cec5SDimitry Andric     AllPhiNodes.clear();
36745ffd83dbSDimitry Andric     for (auto *I : AllSelectNodes) {
36750b57cec5SDimitry Andric       I->replaceAllUsesWith(Dummy);
36760b57cec5SDimitry Andric       I->eraseFromParent();
36770b57cec5SDimitry Andric     }
36780b57cec5SDimitry Andric     AllSelectNodes.clear();
36790b57cec5SDimitry Andric   }
36800b57cec5SDimitry Andric };
36810b57cec5SDimitry Andric 
36820b57cec5SDimitry Andric /// A helper class for combining addressing modes.
36830b57cec5SDimitry Andric class AddressingModeCombiner {
36840b57cec5SDimitry Andric   typedef DenseMap<Value *, Value *> FoldAddrToValueMapping;
36850b57cec5SDimitry Andric   typedef std::pair<PHINode *, PHINode *> PHIPair;
36860b57cec5SDimitry Andric 
36870b57cec5SDimitry Andric private:
36880b57cec5SDimitry Andric   /// The addressing modes we've collected.
36890b57cec5SDimitry Andric   SmallVector<ExtAddrMode, 16> AddrModes;
36900b57cec5SDimitry Andric 
36910b57cec5SDimitry Andric   /// The field in which the AddrModes differ, when we have more than one.
36920b57cec5SDimitry Andric   ExtAddrMode::FieldName DifferentField = ExtAddrMode::NoField;
36930b57cec5SDimitry Andric 
36940b57cec5SDimitry Andric   /// Are the AddrModes that we have all just equal to their original values?
36950b57cec5SDimitry Andric   bool AllAddrModesTrivial = true;
36960b57cec5SDimitry Andric 
36970b57cec5SDimitry Andric   /// Common Type for all different fields in addressing modes.
36981fd87a68SDimitry Andric   Type *CommonType = nullptr;
36990b57cec5SDimitry Andric 
37000b57cec5SDimitry Andric   /// SimplifyQuery for simplifyInstruction utility.
37010b57cec5SDimitry Andric   const SimplifyQuery &SQ;
37020b57cec5SDimitry Andric 
37030b57cec5SDimitry Andric   /// Original Address.
37040b57cec5SDimitry Andric   Value *Original;
37050b57cec5SDimitry Andric 
3706fe013be4SDimitry Andric   /// Common value among addresses
3707fe013be4SDimitry Andric   Value *CommonValue = nullptr;
3708fe013be4SDimitry Andric 
37090b57cec5SDimitry Andric public:
AddressingModeCombiner(const SimplifyQuery & _SQ,Value * OriginalValue)37100b57cec5SDimitry Andric   AddressingModeCombiner(const SimplifyQuery &_SQ, Value *OriginalValue)
37111fd87a68SDimitry Andric       : SQ(_SQ), Original(OriginalValue) {}
37120b57cec5SDimitry Andric 
~AddressingModeCombiner()3713fe013be4SDimitry Andric   ~AddressingModeCombiner() { eraseCommonValueIfDead(); }
3714fe013be4SDimitry Andric 
37150b57cec5SDimitry Andric   /// Get the combined AddrMode
getAddrMode() const3716bdd1243dSDimitry Andric   const ExtAddrMode &getAddrMode() const { return AddrModes[0]; }
37170b57cec5SDimitry Andric 
37180b57cec5SDimitry Andric   /// Add a new AddrMode if it's compatible with the AddrModes we already
37190b57cec5SDimitry Andric   /// have.
37200b57cec5SDimitry Andric   /// \return True iff we succeeded in doing so.
addNewAddrMode(ExtAddrMode & NewAddrMode)37210b57cec5SDimitry Andric   bool addNewAddrMode(ExtAddrMode &NewAddrMode) {
37220b57cec5SDimitry Andric     // Take note of if we have any non-trivial AddrModes, as we need to detect
37230b57cec5SDimitry Andric     // when all AddrModes are trivial as then we would introduce a phi or select
37240b57cec5SDimitry Andric     // which just duplicates what's already there.
37250b57cec5SDimitry Andric     AllAddrModesTrivial = AllAddrModesTrivial && NewAddrMode.isTrivial();
37260b57cec5SDimitry Andric 
37270b57cec5SDimitry Andric     // If this is the first addrmode then everything is fine.
37280b57cec5SDimitry Andric     if (AddrModes.empty()) {
37290b57cec5SDimitry Andric       AddrModes.emplace_back(NewAddrMode);
37300b57cec5SDimitry Andric       return true;
37310b57cec5SDimitry Andric     }
37320b57cec5SDimitry Andric 
37330b57cec5SDimitry Andric     // Figure out how different this is from the other address modes, which we
37340b57cec5SDimitry Andric     // can do just by comparing against the first one given that we only care
37350b57cec5SDimitry Andric     // about the cumulative difference.
37360b57cec5SDimitry Andric     ExtAddrMode::FieldName ThisDifferentField =
37370b57cec5SDimitry Andric         AddrModes[0].compare(NewAddrMode);
37380b57cec5SDimitry Andric     if (DifferentField == ExtAddrMode::NoField)
37390b57cec5SDimitry Andric       DifferentField = ThisDifferentField;
37400b57cec5SDimitry Andric     else if (DifferentField != ThisDifferentField)
37410b57cec5SDimitry Andric       DifferentField = ExtAddrMode::MultipleFields;
37420b57cec5SDimitry Andric 
37430b57cec5SDimitry Andric     // If NewAddrMode differs in more than one dimension we cannot handle it.
37440b57cec5SDimitry Andric     bool CanHandle = DifferentField != ExtAddrMode::MultipleFields;
37450b57cec5SDimitry Andric 
37460b57cec5SDimitry Andric     // If Scale Field is different then we reject.
37470b57cec5SDimitry Andric     CanHandle = CanHandle && DifferentField != ExtAddrMode::ScaleField;
37480b57cec5SDimitry Andric 
37490b57cec5SDimitry Andric     // We also must reject the case when base offset is different and
37500b57cec5SDimitry Andric     // scale reg is not null, we cannot handle this case due to merge of
37510b57cec5SDimitry Andric     // different offsets will be used as ScaleReg.
37520b57cec5SDimitry Andric     CanHandle = CanHandle && (DifferentField != ExtAddrMode::BaseOffsField ||
37530b57cec5SDimitry Andric                               !NewAddrMode.ScaledReg);
37540b57cec5SDimitry Andric 
37550b57cec5SDimitry Andric     // We also must reject the case when GV is different and BaseReg installed
37560b57cec5SDimitry Andric     // due to we want to use base reg as a merge of GV values.
37570b57cec5SDimitry Andric     CanHandle = CanHandle && (DifferentField != ExtAddrMode::BaseGVField ||
37580b57cec5SDimitry Andric                               !NewAddrMode.HasBaseReg);
37590b57cec5SDimitry Andric 
37600b57cec5SDimitry Andric     // Even if NewAddMode is the same we still need to collect it due to
37610b57cec5SDimitry Andric     // original value is different. And later we will need all original values
37620b57cec5SDimitry Andric     // as anchors during finding the common Phi node.
37630b57cec5SDimitry Andric     if (CanHandle)
37640b57cec5SDimitry Andric       AddrModes.emplace_back(NewAddrMode);
37650b57cec5SDimitry Andric     else
37660b57cec5SDimitry Andric       AddrModes.clear();
37670b57cec5SDimitry Andric 
37680b57cec5SDimitry Andric     return CanHandle;
37690b57cec5SDimitry Andric   }
37700b57cec5SDimitry Andric 
37710b57cec5SDimitry Andric   /// Combine the addressing modes we've collected into a single
37720b57cec5SDimitry Andric   /// addressing mode.
37730b57cec5SDimitry Andric   /// \return True iff we successfully combined them or we only had one so
37740b57cec5SDimitry Andric   /// didn't need to combine them anyway.
combineAddrModes()37750b57cec5SDimitry Andric   bool combineAddrModes() {
37760b57cec5SDimitry Andric     // If we have no AddrModes then they can't be combined.
37770b57cec5SDimitry Andric     if (AddrModes.size() == 0)
37780b57cec5SDimitry Andric       return false;
37790b57cec5SDimitry Andric 
37800b57cec5SDimitry Andric     // A single AddrMode can trivially be combined.
37810b57cec5SDimitry Andric     if (AddrModes.size() == 1 || DifferentField == ExtAddrMode::NoField)
37820b57cec5SDimitry Andric       return true;
37830b57cec5SDimitry Andric 
37840b57cec5SDimitry Andric     // If the AddrModes we collected are all just equal to the value they are
37850b57cec5SDimitry Andric     // derived from then combining them wouldn't do anything useful.
37860b57cec5SDimitry Andric     if (AllAddrModesTrivial)
37870b57cec5SDimitry Andric       return false;
37880b57cec5SDimitry Andric 
37890b57cec5SDimitry Andric     if (!addrModeCombiningAllowed())
37900b57cec5SDimitry Andric       return false;
37910b57cec5SDimitry Andric 
37920b57cec5SDimitry Andric     // Build a map between <original value, basic block where we saw it> to
37930b57cec5SDimitry Andric     // value of base register.
37940b57cec5SDimitry Andric     // Bail out if there is no common type.
37950b57cec5SDimitry Andric     FoldAddrToValueMapping Map;
37960b57cec5SDimitry Andric     if (!initializeMap(Map))
37970b57cec5SDimitry Andric       return false;
37980b57cec5SDimitry Andric 
3799fe013be4SDimitry Andric     CommonValue = findCommon(Map);
38000b57cec5SDimitry Andric     if (CommonValue)
38010b57cec5SDimitry Andric       AddrModes[0].SetCombinedField(DifferentField, CommonValue, AddrModes);
38020b57cec5SDimitry Andric     return CommonValue != nullptr;
38030b57cec5SDimitry Andric   }
38040b57cec5SDimitry Andric 
38050b57cec5SDimitry Andric private:
3806fe013be4SDimitry Andric   /// `CommonValue` may be a placeholder inserted by us.
3807fe013be4SDimitry Andric   /// If the placeholder is not used, we should remove this dead instruction.
eraseCommonValueIfDead()3808fe013be4SDimitry Andric   void eraseCommonValueIfDead() {
3809fe013be4SDimitry Andric     if (CommonValue && CommonValue->getNumUses() == 0)
3810fe013be4SDimitry Andric       if (Instruction *CommonInst = dyn_cast<Instruction>(CommonValue))
3811fe013be4SDimitry Andric         CommonInst->eraseFromParent();
3812fe013be4SDimitry Andric   }
3813fe013be4SDimitry Andric 
38140b57cec5SDimitry Andric   /// Initialize Map with anchor values. For address seen
38150b57cec5SDimitry Andric   /// we set the value of different field saw in this address.
38160b57cec5SDimitry Andric   /// At the same time we find a common type for different field we will
38170b57cec5SDimitry Andric   /// use to create new Phi/Select nodes. Keep it in CommonType field.
38180b57cec5SDimitry Andric   /// Return false if there is no common type found.
initializeMap(FoldAddrToValueMapping & Map)38190b57cec5SDimitry Andric   bool initializeMap(FoldAddrToValueMapping &Map) {
38200b57cec5SDimitry Andric     // Keep track of keys where the value is null. We will need to replace it
38210b57cec5SDimitry Andric     // with constant null when we know the common type.
38220b57cec5SDimitry Andric     SmallVector<Value *, 2> NullValue;
38230b57cec5SDimitry Andric     Type *IntPtrTy = SQ.DL.getIntPtrType(AddrModes[0].OriginalValue->getType());
38240b57cec5SDimitry Andric     for (auto &AM : AddrModes) {
38250b57cec5SDimitry Andric       Value *DV = AM.GetFieldAsValue(DifferentField, IntPtrTy);
38260b57cec5SDimitry Andric       if (DV) {
38270b57cec5SDimitry Andric         auto *Type = DV->getType();
38280b57cec5SDimitry Andric         if (CommonType && CommonType != Type)
38290b57cec5SDimitry Andric           return false;
38300b57cec5SDimitry Andric         CommonType = Type;
38310b57cec5SDimitry Andric         Map[AM.OriginalValue] = DV;
38320b57cec5SDimitry Andric       } else {
38330b57cec5SDimitry Andric         NullValue.push_back(AM.OriginalValue);
38340b57cec5SDimitry Andric       }
38350b57cec5SDimitry Andric     }
38360b57cec5SDimitry Andric     assert(CommonType && "At least one non-null value must be!");
38370b57cec5SDimitry Andric     for (auto *V : NullValue)
38380b57cec5SDimitry Andric       Map[V] = Constant::getNullValue(CommonType);
38390b57cec5SDimitry Andric     return true;
38400b57cec5SDimitry Andric   }
38410b57cec5SDimitry Andric 
38420b57cec5SDimitry Andric   /// We have mapping between value A and other value B where B was a field in
38430b57cec5SDimitry Andric   /// addressing mode represented by A. Also we have an original value C
38440b57cec5SDimitry Andric   /// representing an address we start with. Traversing from C through phi and
38450b57cec5SDimitry Andric   /// selects we ended up with A's in a map. This utility function tries to find
38460b57cec5SDimitry Andric   /// a value V which is a field in addressing mode C and traversing through phi
38470b57cec5SDimitry Andric   /// nodes and selects we will end up in corresponded values B in a map.
38480b57cec5SDimitry Andric   /// The utility will create a new Phi/Selects if needed.
38490b57cec5SDimitry Andric   // The simple example looks as follows:
38500b57cec5SDimitry Andric   // BB1:
38510b57cec5SDimitry Andric   //   p1 = b1 + 40
38520b57cec5SDimitry Andric   //   br cond BB2, BB3
38530b57cec5SDimitry Andric   // BB2:
38540b57cec5SDimitry Andric   //   p2 = b2 + 40
38550b57cec5SDimitry Andric   //   br BB3
38560b57cec5SDimitry Andric   // BB3:
38570b57cec5SDimitry Andric   //   p = phi [p1, BB1], [p2, BB2]
38580b57cec5SDimitry Andric   //   v = load p
38590b57cec5SDimitry Andric   // Map is
38600b57cec5SDimitry Andric   //   p1 -> b1
38610b57cec5SDimitry Andric   //   p2 -> b2
38620b57cec5SDimitry Andric   // Request is
38630b57cec5SDimitry Andric   //   p -> ?
38640b57cec5SDimitry Andric   // The function tries to find or build phi [b1, BB1], [b2, BB2] in BB3.
findCommon(FoldAddrToValueMapping & Map)38650b57cec5SDimitry Andric   Value *findCommon(FoldAddrToValueMapping &Map) {
38660b57cec5SDimitry Andric     // Tracks the simplification of newly created phi nodes. The reason we use
38670b57cec5SDimitry Andric     // this mapping is because we will add new created Phi nodes in AddrToBase.
38680b57cec5SDimitry Andric     // Simplification of Phi nodes is recursive, so some Phi node may
38690b57cec5SDimitry Andric     // be simplified after we added it to AddrToBase. In reality this
38700b57cec5SDimitry Andric     // simplification is possible only if original phi/selects were not
38710b57cec5SDimitry Andric     // simplified yet.
38720b57cec5SDimitry Andric     // Using this mapping we can find the current value in AddrToBase.
38730b57cec5SDimitry Andric     SimplificationTracker ST(SQ);
38740b57cec5SDimitry Andric 
38750b57cec5SDimitry Andric     // First step, DFS to create PHI nodes for all intermediate blocks.
38760b57cec5SDimitry Andric     // Also fill traverse order for the second step.
38770b57cec5SDimitry Andric     SmallVector<Value *, 32> TraverseOrder;
38780b57cec5SDimitry Andric     InsertPlaceholders(Map, TraverseOrder, ST);
38790b57cec5SDimitry Andric 
38800b57cec5SDimitry Andric     // Second Step, fill new nodes by merged values and simplify if possible.
38810b57cec5SDimitry Andric     FillPlaceholders(Map, TraverseOrder, ST);
38820b57cec5SDimitry Andric 
38830b57cec5SDimitry Andric     if (!AddrSinkNewSelects && ST.countNewSelectNodes() > 0) {
38840b57cec5SDimitry Andric       ST.destroyNewNodes(CommonType);
38850b57cec5SDimitry Andric       return nullptr;
38860b57cec5SDimitry Andric     }
38870b57cec5SDimitry Andric 
38880b57cec5SDimitry Andric     // Now we'd like to match New Phi nodes to existed ones.
38890b57cec5SDimitry Andric     unsigned PhiNotMatchedCount = 0;
38900b57cec5SDimitry Andric     if (!MatchPhiSet(ST, AddrSinkNewPhis, PhiNotMatchedCount)) {
38910b57cec5SDimitry Andric       ST.destroyNewNodes(CommonType);
38920b57cec5SDimitry Andric       return nullptr;
38930b57cec5SDimitry Andric     }
38940b57cec5SDimitry Andric 
38950b57cec5SDimitry Andric     auto *Result = ST.Get(Map.find(Original)->second);
38960b57cec5SDimitry Andric     if (Result) {
38970b57cec5SDimitry Andric       NumMemoryInstsPhiCreated += ST.countNewPhiNodes() + PhiNotMatchedCount;
38980b57cec5SDimitry Andric       NumMemoryInstsSelectCreated += ST.countNewSelectNodes();
38990b57cec5SDimitry Andric     }
39000b57cec5SDimitry Andric     return Result;
39010b57cec5SDimitry Andric   }
39020b57cec5SDimitry Andric 
39030b57cec5SDimitry Andric   /// Try to match PHI node to Candidate.
39040b57cec5SDimitry Andric   /// Matcher tracks the matched Phi nodes.
MatchPhiNode(PHINode * PHI,PHINode * Candidate,SmallSetVector<PHIPair,8> & Matcher,PhiNodeSet & PhiNodesToMatch)39050b57cec5SDimitry Andric   bool MatchPhiNode(PHINode *PHI, PHINode *Candidate,
39060b57cec5SDimitry Andric                     SmallSetVector<PHIPair, 8> &Matcher,
39070b57cec5SDimitry Andric                     PhiNodeSet &PhiNodesToMatch) {
39080b57cec5SDimitry Andric     SmallVector<PHIPair, 8> WorkList;
39090b57cec5SDimitry Andric     Matcher.insert({PHI, Candidate});
39100b57cec5SDimitry Andric     SmallSet<PHINode *, 8> MatchedPHIs;
39110b57cec5SDimitry Andric     MatchedPHIs.insert(PHI);
39120b57cec5SDimitry Andric     WorkList.push_back({PHI, Candidate});
39130b57cec5SDimitry Andric     SmallSet<PHIPair, 8> Visited;
39140b57cec5SDimitry Andric     while (!WorkList.empty()) {
39150b57cec5SDimitry Andric       auto Item = WorkList.pop_back_val();
39160b57cec5SDimitry Andric       if (!Visited.insert(Item).second)
39170b57cec5SDimitry Andric         continue;
39180b57cec5SDimitry Andric       // We iterate over all incoming values to Phi to compare them.
39190b57cec5SDimitry Andric       // If values are different and both of them Phi and the first one is a
39200b57cec5SDimitry Andric       // Phi we added (subject to match) and both of them is in the same basic
39210b57cec5SDimitry Andric       // block then we can match our pair if values match. So we state that
39220b57cec5SDimitry Andric       // these values match and add it to work list to verify that.
3923fcaf7f86SDimitry Andric       for (auto *B : Item.first->blocks()) {
39240b57cec5SDimitry Andric         Value *FirstValue = Item.first->getIncomingValueForBlock(B);
39250b57cec5SDimitry Andric         Value *SecondValue = Item.second->getIncomingValueForBlock(B);
39260b57cec5SDimitry Andric         if (FirstValue == SecondValue)
39270b57cec5SDimitry Andric           continue;
39280b57cec5SDimitry Andric 
39290b57cec5SDimitry Andric         PHINode *FirstPhi = dyn_cast<PHINode>(FirstValue);
39300b57cec5SDimitry Andric         PHINode *SecondPhi = dyn_cast<PHINode>(SecondValue);
39310b57cec5SDimitry Andric 
39320b57cec5SDimitry Andric         // One of them is not Phi or
39330b57cec5SDimitry Andric         // The first one is not Phi node from the set we'd like to match or
39340b57cec5SDimitry Andric         // Phi nodes from different basic blocks then
39350b57cec5SDimitry Andric         // we will not be able to match.
39360b57cec5SDimitry Andric         if (!FirstPhi || !SecondPhi || !PhiNodesToMatch.count(FirstPhi) ||
39370b57cec5SDimitry Andric             FirstPhi->getParent() != SecondPhi->getParent())
39380b57cec5SDimitry Andric           return false;
39390b57cec5SDimitry Andric 
39400b57cec5SDimitry Andric         // If we already matched them then continue.
39410b57cec5SDimitry Andric         if (Matcher.count({FirstPhi, SecondPhi}))
39420b57cec5SDimitry Andric           continue;
39430b57cec5SDimitry Andric         // So the values are different and does not match. So we need them to
39440b57cec5SDimitry Andric         // match. (But we register no more than one match per PHI node, so that
39450b57cec5SDimitry Andric         // we won't later try to replace them twice.)
39468bcb0991SDimitry Andric         if (MatchedPHIs.insert(FirstPhi).second)
39470b57cec5SDimitry Andric           Matcher.insert({FirstPhi, SecondPhi});
39480b57cec5SDimitry Andric         // But me must check it.
39490b57cec5SDimitry Andric         WorkList.push_back({FirstPhi, SecondPhi});
39500b57cec5SDimitry Andric       }
39510b57cec5SDimitry Andric     }
39520b57cec5SDimitry Andric     return true;
39530b57cec5SDimitry Andric   }
39540b57cec5SDimitry Andric 
39550b57cec5SDimitry Andric   /// For the given set of PHI nodes (in the SimplificationTracker) try
39560b57cec5SDimitry Andric   /// to find their equivalents.
39570b57cec5SDimitry Andric   /// Returns false if this matching fails and creation of new Phi is disabled.
MatchPhiSet(SimplificationTracker & ST,bool AllowNewPhiNodes,unsigned & PhiNotMatchedCount)39580b57cec5SDimitry Andric   bool MatchPhiSet(SimplificationTracker &ST, bool AllowNewPhiNodes,
39590b57cec5SDimitry Andric                    unsigned &PhiNotMatchedCount) {
39600b57cec5SDimitry Andric     // Matched and PhiNodesToMatch iterate their elements in a deterministic
39610b57cec5SDimitry Andric     // order, so the replacements (ReplacePhi) are also done in a deterministic
39620b57cec5SDimitry Andric     // order.
39630b57cec5SDimitry Andric     SmallSetVector<PHIPair, 8> Matched;
39640b57cec5SDimitry Andric     SmallPtrSet<PHINode *, 8> WillNotMatch;
39650b57cec5SDimitry Andric     PhiNodeSet &PhiNodesToMatch = ST.newPhiNodes();
39660b57cec5SDimitry Andric     while (PhiNodesToMatch.size()) {
39670b57cec5SDimitry Andric       PHINode *PHI = *PhiNodesToMatch.begin();
39680b57cec5SDimitry Andric 
39690b57cec5SDimitry Andric       // Add us, if no Phi nodes in the basic block we do not match.
39700b57cec5SDimitry Andric       WillNotMatch.clear();
39710b57cec5SDimitry Andric       WillNotMatch.insert(PHI);
39720b57cec5SDimitry Andric 
39730b57cec5SDimitry Andric       // Traverse all Phis until we found equivalent or fail to do that.
39740b57cec5SDimitry Andric       bool IsMatched = false;
39750b57cec5SDimitry Andric       for (auto &P : PHI->getParent()->phis()) {
3976349cc55cSDimitry Andric         // Skip new Phi nodes.
3977349cc55cSDimitry Andric         if (PhiNodesToMatch.count(&P))
39780b57cec5SDimitry Andric           continue;
39790b57cec5SDimitry Andric         if ((IsMatched = MatchPhiNode(PHI, &P, Matched, PhiNodesToMatch)))
39800b57cec5SDimitry Andric           break;
39810b57cec5SDimitry Andric         // If it does not match, collect all Phi nodes from matcher.
39820b57cec5SDimitry Andric         // if we end up with no match, them all these Phi nodes will not match
39830b57cec5SDimitry Andric         // later.
39840b57cec5SDimitry Andric         for (auto M : Matched)
39850b57cec5SDimitry Andric           WillNotMatch.insert(M.first);
39860b57cec5SDimitry Andric         Matched.clear();
39870b57cec5SDimitry Andric       }
39880b57cec5SDimitry Andric       if (IsMatched) {
39890b57cec5SDimitry Andric         // Replace all matched values and erase them.
39900b57cec5SDimitry Andric         for (auto MV : Matched)
39910b57cec5SDimitry Andric           ST.ReplacePhi(MV.first, MV.second);
39920b57cec5SDimitry Andric         Matched.clear();
39930b57cec5SDimitry Andric         continue;
39940b57cec5SDimitry Andric       }
39950b57cec5SDimitry Andric       // If we are not allowed to create new nodes then bail out.
39960b57cec5SDimitry Andric       if (!AllowNewPhiNodes)
39970b57cec5SDimitry Andric         return false;
39980b57cec5SDimitry Andric       // Just remove all seen values in matcher. They will not match anything.
39990b57cec5SDimitry Andric       PhiNotMatchedCount += WillNotMatch.size();
40000b57cec5SDimitry Andric       for (auto *P : WillNotMatch)
40010b57cec5SDimitry Andric         PhiNodesToMatch.erase(P);
40020b57cec5SDimitry Andric     }
40030b57cec5SDimitry Andric     return true;
40040b57cec5SDimitry Andric   }
40050b57cec5SDimitry Andric   /// Fill the placeholders with values from predecessors and simplify them.
FillPlaceholders(FoldAddrToValueMapping & Map,SmallVectorImpl<Value * > & TraverseOrder,SimplificationTracker & ST)40060b57cec5SDimitry Andric   void FillPlaceholders(FoldAddrToValueMapping &Map,
40070b57cec5SDimitry Andric                         SmallVectorImpl<Value *> &TraverseOrder,
40080b57cec5SDimitry Andric                         SimplificationTracker &ST) {
40090b57cec5SDimitry Andric     while (!TraverseOrder.empty()) {
40100b57cec5SDimitry Andric       Value *Current = TraverseOrder.pop_back_val();
4011fe013be4SDimitry Andric       assert(Map.contains(Current) && "No node to fill!!!");
40120b57cec5SDimitry Andric       Value *V = Map[Current];
40130b57cec5SDimitry Andric 
40140b57cec5SDimitry Andric       if (SelectInst *Select = dyn_cast<SelectInst>(V)) {
40150b57cec5SDimitry Andric         // CurrentValue also must be Select.
40160b57cec5SDimitry Andric         auto *CurrentSelect = cast<SelectInst>(Current);
40170b57cec5SDimitry Andric         auto *TrueValue = CurrentSelect->getTrueValue();
4018fe013be4SDimitry Andric         assert(Map.contains(TrueValue) && "No True Value!");
40190b57cec5SDimitry Andric         Select->setTrueValue(ST.Get(Map[TrueValue]));
40200b57cec5SDimitry Andric         auto *FalseValue = CurrentSelect->getFalseValue();
4021fe013be4SDimitry Andric         assert(Map.contains(FalseValue) && "No False Value!");
40220b57cec5SDimitry Andric         Select->setFalseValue(ST.Get(Map[FalseValue]));
40230b57cec5SDimitry Andric       } else {
40240b57cec5SDimitry Andric         // Must be a Phi node then.
40258bcb0991SDimitry Andric         auto *PHI = cast<PHINode>(V);
40260b57cec5SDimitry Andric         // Fill the Phi node with values from predecessors.
40275ffd83dbSDimitry Andric         for (auto *B : predecessors(PHI->getParent())) {
40288bcb0991SDimitry Andric           Value *PV = cast<PHINode>(Current)->getIncomingValueForBlock(B);
4029fe013be4SDimitry Andric           assert(Map.contains(PV) && "No predecessor Value!");
40300b57cec5SDimitry Andric           PHI->addIncoming(ST.Get(Map[PV]), B);
40310b57cec5SDimitry Andric         }
40320b57cec5SDimitry Andric       }
40330b57cec5SDimitry Andric       Map[Current] = ST.Simplify(V);
40340b57cec5SDimitry Andric     }
40350b57cec5SDimitry Andric   }
40360b57cec5SDimitry Andric 
40370b57cec5SDimitry Andric   /// Starting from original value recursively iterates over def-use chain up to
40380b57cec5SDimitry Andric   /// known ending values represented in a map. For each traversed phi/select
40390b57cec5SDimitry Andric   /// inserts a placeholder Phi or Select.
40400b57cec5SDimitry Andric   /// Reports all new created Phi/Select nodes by adding them to set.
40410b57cec5SDimitry Andric   /// Also reports and order in what values have been traversed.
InsertPlaceholders(FoldAddrToValueMapping & Map,SmallVectorImpl<Value * > & TraverseOrder,SimplificationTracker & ST)40420b57cec5SDimitry Andric   void InsertPlaceholders(FoldAddrToValueMapping &Map,
40430b57cec5SDimitry Andric                           SmallVectorImpl<Value *> &TraverseOrder,
40440b57cec5SDimitry Andric                           SimplificationTracker &ST) {
40450b57cec5SDimitry Andric     SmallVector<Value *, 32> Worklist;
40460b57cec5SDimitry Andric     assert((isa<PHINode>(Original) || isa<SelectInst>(Original)) &&
40470b57cec5SDimitry Andric            "Address must be a Phi or Select node");
404881ad6265SDimitry Andric     auto *Dummy = PoisonValue::get(CommonType);
40490b57cec5SDimitry Andric     Worklist.push_back(Original);
40500b57cec5SDimitry Andric     while (!Worklist.empty()) {
40510b57cec5SDimitry Andric       Value *Current = Worklist.pop_back_val();
40520b57cec5SDimitry Andric       // if it is already visited or it is an ending value then skip it.
4053fe013be4SDimitry Andric       if (Map.contains(Current))
40540b57cec5SDimitry Andric         continue;
40550b57cec5SDimitry Andric       TraverseOrder.push_back(Current);
40560b57cec5SDimitry Andric 
40570b57cec5SDimitry Andric       // CurrentValue must be a Phi node or select. All others must be covered
40580b57cec5SDimitry Andric       // by anchors.
40590b57cec5SDimitry Andric       if (SelectInst *CurrentSelect = dyn_cast<SelectInst>(Current)) {
40600b57cec5SDimitry Andric         // Is it OK to get metadata from OrigSelect?!
40610b57cec5SDimitry Andric         // Create a Select placeholder with dummy value.
40620b57cec5SDimitry Andric         SelectInst *Select = SelectInst::Create(
40630b57cec5SDimitry Andric             CurrentSelect->getCondition(), Dummy, Dummy,
40640b57cec5SDimitry Andric             CurrentSelect->getName(), CurrentSelect, CurrentSelect);
40650b57cec5SDimitry Andric         Map[Current] = Select;
40660b57cec5SDimitry Andric         ST.insertNewSelect(Select);
40670b57cec5SDimitry Andric         // We are interested in True and False values.
40680b57cec5SDimitry Andric         Worklist.push_back(CurrentSelect->getTrueValue());
40690b57cec5SDimitry Andric         Worklist.push_back(CurrentSelect->getFalseValue());
40700b57cec5SDimitry Andric       } else {
40710b57cec5SDimitry Andric         // It must be a Phi node then.
40720b57cec5SDimitry Andric         PHINode *CurrentPhi = cast<PHINode>(Current);
40730b57cec5SDimitry Andric         unsigned PredCount = CurrentPhi->getNumIncomingValues();
40740b57cec5SDimitry Andric         PHINode *PHI =
40750b57cec5SDimitry Andric             PHINode::Create(CommonType, PredCount, "sunk_phi", CurrentPhi);
40760b57cec5SDimitry Andric         Map[Current] = PHI;
40770b57cec5SDimitry Andric         ST.insertNewPhi(PHI);
4078e8d8bef9SDimitry Andric         append_range(Worklist, CurrentPhi->incoming_values());
40790b57cec5SDimitry Andric       }
40800b57cec5SDimitry Andric     }
40810b57cec5SDimitry Andric   }
40820b57cec5SDimitry Andric 
addrModeCombiningAllowed()40830b57cec5SDimitry Andric   bool addrModeCombiningAllowed() {
40840b57cec5SDimitry Andric     if (DisableComplexAddrModes)
40850b57cec5SDimitry Andric       return false;
40860b57cec5SDimitry Andric     switch (DifferentField) {
40870b57cec5SDimitry Andric     default:
40880b57cec5SDimitry Andric       return false;
40890b57cec5SDimitry Andric     case ExtAddrMode::BaseRegField:
40900b57cec5SDimitry Andric       return AddrSinkCombineBaseReg;
40910b57cec5SDimitry Andric     case ExtAddrMode::BaseGVField:
40920b57cec5SDimitry Andric       return AddrSinkCombineBaseGV;
40930b57cec5SDimitry Andric     case ExtAddrMode::BaseOffsField:
40940b57cec5SDimitry Andric       return AddrSinkCombineBaseOffs;
40950b57cec5SDimitry Andric     case ExtAddrMode::ScaledRegField:
40960b57cec5SDimitry Andric       return AddrSinkCombineScaledReg;
40970b57cec5SDimitry Andric     }
40980b57cec5SDimitry Andric   }
40990b57cec5SDimitry Andric };
41000b57cec5SDimitry Andric } // end anonymous namespace
41010b57cec5SDimitry Andric 
41020b57cec5SDimitry Andric /// Try adding ScaleReg*Scale to the current addressing mode.
41030b57cec5SDimitry Andric /// Return true and update AddrMode if this addr mode is legal for the target,
41040b57cec5SDimitry Andric /// false if not.
matchScaledValue(Value * ScaleReg,int64_t Scale,unsigned Depth)41050b57cec5SDimitry Andric bool AddressingModeMatcher::matchScaledValue(Value *ScaleReg, int64_t Scale,
41060b57cec5SDimitry Andric                                              unsigned Depth) {
41070b57cec5SDimitry Andric   // If Scale is 1, then this is the same as adding ScaleReg to the addressing
41080b57cec5SDimitry Andric   // mode.  Just process that directly.
41090b57cec5SDimitry Andric   if (Scale == 1)
41100b57cec5SDimitry Andric     return matchAddr(ScaleReg, Depth);
41110b57cec5SDimitry Andric 
41120b57cec5SDimitry Andric   // If the scale is 0, it takes nothing to add this.
41130b57cec5SDimitry Andric   if (Scale == 0)
41140b57cec5SDimitry Andric     return true;
41150b57cec5SDimitry Andric 
41160b57cec5SDimitry Andric   // If we already have a scale of this value, we can add to it, otherwise, we
41170b57cec5SDimitry Andric   // need an available scale field.
41180b57cec5SDimitry Andric   if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg)
41190b57cec5SDimitry Andric     return false;
41200b57cec5SDimitry Andric 
41210b57cec5SDimitry Andric   ExtAddrMode TestAddrMode = AddrMode;
41220b57cec5SDimitry Andric 
41230b57cec5SDimitry Andric   // Add scale to turn X*4+X*3 -> X*7.  This could also do things like
41240b57cec5SDimitry Andric   // [A+B + A*7] -> [B+A*8].
41250b57cec5SDimitry Andric   TestAddrMode.Scale += Scale;
41260b57cec5SDimitry Andric   TestAddrMode.ScaledReg = ScaleReg;
41270b57cec5SDimitry Andric 
41280b57cec5SDimitry Andric   // If the new address isn't legal, bail out.
41290b57cec5SDimitry Andric   if (!TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace))
41300b57cec5SDimitry Andric     return false;
41310b57cec5SDimitry Andric 
41320b57cec5SDimitry Andric   // It was legal, so commit it.
41330b57cec5SDimitry Andric   AddrMode = TestAddrMode;
41340b57cec5SDimitry Andric 
41350b57cec5SDimitry Andric   // Okay, we decided that we can add ScaleReg+Scale to AddrMode.  Check now
41360b57cec5SDimitry Andric   // to see if ScaleReg is actually X+C.  If so, we can turn this into adding
4137fe6060f1SDimitry Andric   // X*Scale + C*Scale to addr mode. If we found available IV increment, do not
4138fe6060f1SDimitry Andric   // go any further: we can reuse it and cannot eliminate it.
4139bdd1243dSDimitry Andric   ConstantInt *CI = nullptr;
4140bdd1243dSDimitry Andric   Value *AddLHS = nullptr;
41410b57cec5SDimitry Andric   if (isa<Instruction>(ScaleReg) && // not a constant expr.
41425ffd83dbSDimitry Andric       match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI))) &&
4143fe6060f1SDimitry Andric       !isIVIncrement(ScaleReg, &LI) && CI->getValue().isSignedIntN(64)) {
41440b57cec5SDimitry Andric     TestAddrMode.InBounds = false;
41450b57cec5SDimitry Andric     TestAddrMode.ScaledReg = AddLHS;
41460b57cec5SDimitry Andric     TestAddrMode.BaseOffs += CI->getSExtValue() * TestAddrMode.Scale;
41470b57cec5SDimitry Andric 
41480b57cec5SDimitry Andric     // If this addressing mode is legal, commit it and remember that we folded
41490b57cec5SDimitry Andric     // this instruction.
41500b57cec5SDimitry Andric     if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace)) {
41510b57cec5SDimitry Andric       AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
41520b57cec5SDimitry Andric       AddrMode = TestAddrMode;
41530b57cec5SDimitry Andric       return true;
41540b57cec5SDimitry Andric     }
4155fe6060f1SDimitry Andric     // Restore status quo.
4156fe6060f1SDimitry Andric     TestAddrMode = AddrMode;
41570b57cec5SDimitry Andric   }
41580b57cec5SDimitry Andric 
4159fe6060f1SDimitry Andric   // If this is an add recurrence with a constant step, return the increment
4160fe6060f1SDimitry Andric   // instruction and the canonicalized step.
4161bdd1243dSDimitry Andric   auto GetConstantStep =
4162bdd1243dSDimitry Andric       [this](const Value *V) -> std::optional<std::pair<Instruction *, APInt>> {
4163fe6060f1SDimitry Andric     auto *PN = dyn_cast<PHINode>(V);
4164fe6060f1SDimitry Andric     if (!PN)
4165bdd1243dSDimitry Andric       return std::nullopt;
4166fe6060f1SDimitry Andric     auto IVInc = getIVIncrement(PN, &LI);
4167fe6060f1SDimitry Andric     if (!IVInc)
4168bdd1243dSDimitry Andric       return std::nullopt;
4169bdd1243dSDimitry Andric     // TODO: The result of the intrinsics above is two-complement. However when
4170fe6060f1SDimitry Andric     // IV inc is expressed as add or sub, iv.next is potentially a poison value.
4171fe6060f1SDimitry Andric     // If it has nuw or nsw flags, we need to make sure that these flags are
4172fe6060f1SDimitry Andric     // inferrable at the point of memory instruction. Otherwise we are replacing
4173bdd1243dSDimitry Andric     // well-defined two-complement computation with poison. Currently, to avoid
4174fe6060f1SDimitry Andric     // potentially complex analysis needed to prove this, we reject such cases.
4175fe6060f1SDimitry Andric     if (auto *OIVInc = dyn_cast<OverflowingBinaryOperator>(IVInc->first))
4176fe6060f1SDimitry Andric       if (OIVInc->hasNoSignedWrap() || OIVInc->hasNoUnsignedWrap())
4177bdd1243dSDimitry Andric         return std::nullopt;
4178fe6060f1SDimitry Andric     if (auto *ConstantStep = dyn_cast<ConstantInt>(IVInc->second))
4179fe6060f1SDimitry Andric       return std::make_pair(IVInc->first, ConstantStep->getValue());
4180bdd1243dSDimitry Andric     return std::nullopt;
4181fe6060f1SDimitry Andric   };
4182fe6060f1SDimitry Andric 
4183fe6060f1SDimitry Andric   // Try to account for the following special case:
4184fe6060f1SDimitry Andric   // 1. ScaleReg is an inductive variable;
4185fe6060f1SDimitry Andric   // 2. We use it with non-zero offset;
4186fe6060f1SDimitry Andric   // 3. IV's increment is available at the point of memory instruction.
4187fe6060f1SDimitry Andric   //
4188fe6060f1SDimitry Andric   // In this case, we may reuse the IV increment instead of the IV Phi to
4189fe6060f1SDimitry Andric   // achieve the following advantages:
4190fe6060f1SDimitry Andric   // 1. If IV step matches the offset, we will have no need in the offset;
4191fe6060f1SDimitry Andric   // 2. Even if they don't match, we will reduce the overlap of living IV
4192fe6060f1SDimitry Andric   //    and IV increment, that will potentially lead to better register
4193fe6060f1SDimitry Andric   //    assignment.
4194fe6060f1SDimitry Andric   if (AddrMode.BaseOffs) {
4195fe6060f1SDimitry Andric     if (auto IVStep = GetConstantStep(ScaleReg)) {
4196fe6060f1SDimitry Andric       Instruction *IVInc = IVStep->first;
4197fe6060f1SDimitry Andric       // The following assert is important to ensure a lack of infinite loops.
4198fe6060f1SDimitry Andric       // This transforms is (intentionally) the inverse of the one just above.
4199fe6060f1SDimitry Andric       // If they don't agree on the definition of an increment, we'd alternate
4200fe6060f1SDimitry Andric       // back and forth indefinitely.
4201fe6060f1SDimitry Andric       assert(isIVIncrement(IVInc, &LI) && "implied by GetConstantStep");
4202fe6060f1SDimitry Andric       APInt Step = IVStep->second;
4203fe6060f1SDimitry Andric       APInt Offset = Step * AddrMode.Scale;
4204fe6060f1SDimitry Andric       if (Offset.isSignedIntN(64)) {
4205fe6060f1SDimitry Andric         TestAddrMode.InBounds = false;
4206fe6060f1SDimitry Andric         TestAddrMode.ScaledReg = IVInc;
4207fe6060f1SDimitry Andric         TestAddrMode.BaseOffs -= Offset.getLimitedValue();
4208fe6060f1SDimitry Andric         // If this addressing mode is legal, commit it..
4209fe6060f1SDimitry Andric         // (Note that we defer the (expensive) domtree base legality check
4210fe6060f1SDimitry Andric         // to the very last possible point.)
4211fe6060f1SDimitry Andric         if (TLI.isLegalAddressingMode(DL, TestAddrMode, AccessTy, AddrSpace) &&
4212fe6060f1SDimitry Andric             getDTFn().dominates(IVInc, MemoryInst)) {
4213fe6060f1SDimitry Andric           AddrModeInsts.push_back(cast<Instruction>(IVInc));
4214fe6060f1SDimitry Andric           AddrMode = TestAddrMode;
4215fe6060f1SDimitry Andric           return true;
4216fe6060f1SDimitry Andric         }
4217fe6060f1SDimitry Andric         // Restore status quo.
4218fe6060f1SDimitry Andric         TestAddrMode = AddrMode;
4219fe6060f1SDimitry Andric       }
4220fe6060f1SDimitry Andric     }
4221fe6060f1SDimitry Andric   }
4222fe6060f1SDimitry Andric 
4223fe6060f1SDimitry Andric   // Otherwise, just return what we have.
42240b57cec5SDimitry Andric   return true;
42250b57cec5SDimitry Andric }
42260b57cec5SDimitry Andric 
42270b57cec5SDimitry Andric /// This is a little filter, which returns true if an addressing computation
42280b57cec5SDimitry Andric /// involving I might be folded into a load/store accessing it.
42290b57cec5SDimitry Andric /// This doesn't need to be perfect, but needs to accept at least
42300b57cec5SDimitry Andric /// the set of instructions that MatchOperationAddr can.
MightBeFoldableInst(Instruction * I)42310b57cec5SDimitry Andric static bool MightBeFoldableInst(Instruction *I) {
42320b57cec5SDimitry Andric   switch (I->getOpcode()) {
42330b57cec5SDimitry Andric   case Instruction::BitCast:
42340b57cec5SDimitry Andric   case Instruction::AddrSpaceCast:
42350b57cec5SDimitry Andric     // Don't touch identity bitcasts.
42360b57cec5SDimitry Andric     if (I->getType() == I->getOperand(0)->getType())
42370b57cec5SDimitry Andric       return false;
42380b57cec5SDimitry Andric     return I->getType()->isIntOrPtrTy();
42390b57cec5SDimitry Andric   case Instruction::PtrToInt:
42400b57cec5SDimitry Andric     // PtrToInt is always a noop, as we know that the int type is pointer sized.
42410b57cec5SDimitry Andric     return true;
42420b57cec5SDimitry Andric   case Instruction::IntToPtr:
42430b57cec5SDimitry Andric     // We know the input is intptr_t, so this is foldable.
42440b57cec5SDimitry Andric     return true;
42450b57cec5SDimitry Andric   case Instruction::Add:
42460b57cec5SDimitry Andric     return true;
42470b57cec5SDimitry Andric   case Instruction::Mul:
42480b57cec5SDimitry Andric   case Instruction::Shl:
42490b57cec5SDimitry Andric     // Can only handle X*C and X << C.
42500b57cec5SDimitry Andric     return isa<ConstantInt>(I->getOperand(1));
42510b57cec5SDimitry Andric   case Instruction::GetElementPtr:
42520b57cec5SDimitry Andric     return true;
42530b57cec5SDimitry Andric   default:
42540b57cec5SDimitry Andric     return false;
42550b57cec5SDimitry Andric   }
42560b57cec5SDimitry Andric }
42570b57cec5SDimitry Andric 
42580b57cec5SDimitry Andric /// Check whether or not \p Val is a legal instruction for \p TLI.
42590b57cec5SDimitry Andric /// \note \p Val is assumed to be the product of some type promotion.
42600b57cec5SDimitry Andric /// Therefore if \p Val has an undefined state in \p TLI, this is assumed
42610b57cec5SDimitry Andric /// to be legal, as the non-promoted value would have had the same state.
isPromotedInstructionLegal(const TargetLowering & TLI,const DataLayout & DL,Value * Val)42620b57cec5SDimitry Andric static bool isPromotedInstructionLegal(const TargetLowering &TLI,
42630b57cec5SDimitry Andric                                        const DataLayout &DL, Value *Val) {
42640b57cec5SDimitry Andric   Instruction *PromotedInst = dyn_cast<Instruction>(Val);
42650b57cec5SDimitry Andric   if (!PromotedInst)
42660b57cec5SDimitry Andric     return false;
42670b57cec5SDimitry Andric   int ISDOpcode = TLI.InstructionOpcodeToISD(PromotedInst->getOpcode());
42680b57cec5SDimitry Andric   // If the ISDOpcode is undefined, it was undefined before the promotion.
42690b57cec5SDimitry Andric   if (!ISDOpcode)
42700b57cec5SDimitry Andric     return true;
42710b57cec5SDimitry Andric   // Otherwise, check if the promoted instruction is legal or not.
42720b57cec5SDimitry Andric   return TLI.isOperationLegalOrCustom(
42730b57cec5SDimitry Andric       ISDOpcode, TLI.getValueType(DL, PromotedInst->getType()));
42740b57cec5SDimitry Andric }
42750b57cec5SDimitry Andric 
42760b57cec5SDimitry Andric namespace {
42770b57cec5SDimitry Andric 
42780b57cec5SDimitry Andric /// Hepler class to perform type promotion.
42790b57cec5SDimitry Andric class TypePromotionHelper {
42800b57cec5SDimitry Andric   /// Utility function to add a promoted instruction \p ExtOpnd to
42810b57cec5SDimitry Andric   /// \p PromotedInsts and record the type of extension we have seen.
addPromotedInst(InstrToOrigTy & PromotedInsts,Instruction * ExtOpnd,bool IsSExt)42820b57cec5SDimitry Andric   static void addPromotedInst(InstrToOrigTy &PromotedInsts,
4283bdd1243dSDimitry Andric                               Instruction *ExtOpnd, bool IsSExt) {
42840b57cec5SDimitry Andric     ExtType ExtTy = IsSExt ? SignExtension : ZeroExtension;
42850b57cec5SDimitry Andric     InstrToOrigTy::iterator It = PromotedInsts.find(ExtOpnd);
42860b57cec5SDimitry Andric     if (It != PromotedInsts.end()) {
42870b57cec5SDimitry Andric       // If the new extension is same as original, the information in
42880b57cec5SDimitry Andric       // PromotedInsts[ExtOpnd] is still correct.
42890b57cec5SDimitry Andric       if (It->second.getInt() == ExtTy)
42900b57cec5SDimitry Andric         return;
42910b57cec5SDimitry Andric 
42920b57cec5SDimitry Andric       // Now the new extension is different from old extension, we make
42930b57cec5SDimitry Andric       // the type information invalid by setting extension type to
42940b57cec5SDimitry Andric       // BothExtension.
42950b57cec5SDimitry Andric       ExtTy = BothExtension;
42960b57cec5SDimitry Andric     }
42970b57cec5SDimitry Andric     PromotedInsts[ExtOpnd] = TypeIsSExt(ExtOpnd->getType(), ExtTy);
42980b57cec5SDimitry Andric   }
42990b57cec5SDimitry Andric 
43000b57cec5SDimitry Andric   /// Utility function to query the original type of instruction \p Opnd
43010b57cec5SDimitry Andric   /// with a matched extension type. If the extension doesn't match, we
43020b57cec5SDimitry Andric   /// cannot use the information we had on the original type.
43030b57cec5SDimitry Andric   /// BothExtension doesn't match any extension type.
getOrigType(const InstrToOrigTy & PromotedInsts,Instruction * Opnd,bool IsSExt)43040b57cec5SDimitry Andric   static const Type *getOrigType(const InstrToOrigTy &PromotedInsts,
4305bdd1243dSDimitry Andric                                  Instruction *Opnd, bool IsSExt) {
43060b57cec5SDimitry Andric     ExtType ExtTy = IsSExt ? SignExtension : ZeroExtension;
43070b57cec5SDimitry Andric     InstrToOrigTy::const_iterator It = PromotedInsts.find(Opnd);
43080b57cec5SDimitry Andric     if (It != PromotedInsts.end() && It->second.getInt() == ExtTy)
43090b57cec5SDimitry Andric       return It->second.getPointer();
43100b57cec5SDimitry Andric     return nullptr;
43110b57cec5SDimitry Andric   }
43120b57cec5SDimitry Andric 
43130b57cec5SDimitry Andric   /// Utility function to check whether or not a sign or zero extension
43140b57cec5SDimitry Andric   /// of \p Inst with \p ConsideredExtType can be moved through \p Inst by
43150b57cec5SDimitry Andric   /// either using the operands of \p Inst or promoting \p Inst.
43160b57cec5SDimitry Andric   /// The type of the extension is defined by \p IsSExt.
43170b57cec5SDimitry Andric   /// In other words, check if:
43180b57cec5SDimitry Andric   /// ext (Ty Inst opnd1 opnd2 ... opndN) to ConsideredExtType.
43190b57cec5SDimitry Andric   /// #1 Promotion applies:
43200b57cec5SDimitry Andric   /// ConsideredExtType Inst (ext opnd1 to ConsideredExtType, ...).
43210b57cec5SDimitry Andric   /// #2 Operand reuses:
43220b57cec5SDimitry Andric   /// ext opnd1 to ConsideredExtType.
43230b57cec5SDimitry Andric   /// \p PromotedInsts maps the instructions to their type before promotion.
43240b57cec5SDimitry Andric   static bool canGetThrough(const Instruction *Inst, Type *ConsideredExtType,
43250b57cec5SDimitry Andric                             const InstrToOrigTy &PromotedInsts, bool IsSExt);
43260b57cec5SDimitry Andric 
43270b57cec5SDimitry Andric   /// Utility function to determine if \p OpIdx should be promoted when
43280b57cec5SDimitry Andric   /// promoting \p Inst.
shouldExtOperand(const Instruction * Inst,int OpIdx)43290b57cec5SDimitry Andric   static bool shouldExtOperand(const Instruction *Inst, int OpIdx) {
43300b57cec5SDimitry Andric     return !(isa<SelectInst>(Inst) && OpIdx == 0);
43310b57cec5SDimitry Andric   }
43320b57cec5SDimitry Andric 
43330b57cec5SDimitry Andric   /// Utility function to promote the operand of \p Ext when this
43340b57cec5SDimitry Andric   /// operand is a promotable trunc or sext or zext.
43350b57cec5SDimitry Andric   /// \p PromotedInsts maps the instructions to their type before promotion.
43360b57cec5SDimitry Andric   /// \p CreatedInstsCost[out] contains the cost of all instructions
43370b57cec5SDimitry Andric   /// created to promote the operand of Ext.
43380b57cec5SDimitry Andric   /// Newly added extensions are inserted in \p Exts.
43390b57cec5SDimitry Andric   /// Newly added truncates are inserted in \p Truncs.
43400b57cec5SDimitry Andric   /// Should never be called directly.
43410b57cec5SDimitry Andric   /// \return The promoted value which is used instead of Ext.
43420b57cec5SDimitry Andric   static Value *promoteOperandForTruncAndAnyExt(
43430b57cec5SDimitry Andric       Instruction *Ext, TypePromotionTransaction &TPT,
43440b57cec5SDimitry Andric       InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
43450b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> *Exts,
43460b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI);
43470b57cec5SDimitry Andric 
43480b57cec5SDimitry Andric   /// Utility function to promote the operand of \p Ext when this
43490b57cec5SDimitry Andric   /// operand is promotable and is not a supported trunc or sext.
43500b57cec5SDimitry Andric   /// \p PromotedInsts maps the instructions to their type before promotion.
43510b57cec5SDimitry Andric   /// \p CreatedInstsCost[out] contains the cost of all the instructions
43520b57cec5SDimitry Andric   /// created to promote the operand of Ext.
43530b57cec5SDimitry Andric   /// Newly added extensions are inserted in \p Exts.
43540b57cec5SDimitry Andric   /// Newly added truncates are inserted in \p Truncs.
43550b57cec5SDimitry Andric   /// Should never be called directly.
43560b57cec5SDimitry Andric   /// \return The promoted value which is used instead of Ext.
43570b57cec5SDimitry Andric   static Value *promoteOperandForOther(Instruction *Ext,
43580b57cec5SDimitry Andric                                        TypePromotionTransaction &TPT,
43590b57cec5SDimitry Andric                                        InstrToOrigTy &PromotedInsts,
43600b57cec5SDimitry Andric                                        unsigned &CreatedInstsCost,
43610b57cec5SDimitry Andric                                        SmallVectorImpl<Instruction *> *Exts,
43620b57cec5SDimitry Andric                                        SmallVectorImpl<Instruction *> *Truncs,
43630b57cec5SDimitry Andric                                        const TargetLowering &TLI, bool IsSExt);
43640b57cec5SDimitry Andric 
43650b57cec5SDimitry Andric   /// \see promoteOperandForOther.
signExtendOperandForOther(Instruction * Ext,TypePromotionTransaction & TPT,InstrToOrigTy & PromotedInsts,unsigned & CreatedInstsCost,SmallVectorImpl<Instruction * > * Exts,SmallVectorImpl<Instruction * > * Truncs,const TargetLowering & TLI)43660b57cec5SDimitry Andric   static Value *signExtendOperandForOther(
43670b57cec5SDimitry Andric       Instruction *Ext, TypePromotionTransaction &TPT,
43680b57cec5SDimitry Andric       InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
43690b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> *Exts,
43700b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
43710b57cec5SDimitry Andric     return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
43720b57cec5SDimitry Andric                                   Exts, Truncs, TLI, true);
43730b57cec5SDimitry Andric   }
43740b57cec5SDimitry Andric 
43750b57cec5SDimitry Andric   /// \see promoteOperandForOther.
zeroExtendOperandForOther(Instruction * Ext,TypePromotionTransaction & TPT,InstrToOrigTy & PromotedInsts,unsigned & CreatedInstsCost,SmallVectorImpl<Instruction * > * Exts,SmallVectorImpl<Instruction * > * Truncs,const TargetLowering & TLI)43760b57cec5SDimitry Andric   static Value *zeroExtendOperandForOther(
43770b57cec5SDimitry Andric       Instruction *Ext, TypePromotionTransaction &TPT,
43780b57cec5SDimitry Andric       InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
43790b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> *Exts,
43800b57cec5SDimitry Andric       SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
43810b57cec5SDimitry Andric     return promoteOperandForOther(Ext, TPT, PromotedInsts, CreatedInstsCost,
43820b57cec5SDimitry Andric                                   Exts, Truncs, TLI, false);
43830b57cec5SDimitry Andric   }
43840b57cec5SDimitry Andric 
43850b57cec5SDimitry Andric public:
43860b57cec5SDimitry Andric   /// Type for the utility function that promotes the operand of Ext.
43870b57cec5SDimitry Andric   using Action = Value *(*)(Instruction *Ext, TypePromotionTransaction &TPT,
43880b57cec5SDimitry Andric                             InstrToOrigTy &PromotedInsts,
43890b57cec5SDimitry Andric                             unsigned &CreatedInstsCost,
43900b57cec5SDimitry Andric                             SmallVectorImpl<Instruction *> *Exts,
43910b57cec5SDimitry Andric                             SmallVectorImpl<Instruction *> *Truncs,
43920b57cec5SDimitry Andric                             const TargetLowering &TLI);
43930b57cec5SDimitry Andric 
43940b57cec5SDimitry Andric   /// Given a sign/zero extend instruction \p Ext, return the appropriate
43950b57cec5SDimitry Andric   /// action to promote the operand of \p Ext instead of using Ext.
43960b57cec5SDimitry Andric   /// \return NULL if no promotable action is possible with the current
43970b57cec5SDimitry Andric   /// sign extension.
43980b57cec5SDimitry Andric   /// \p InsertedInsts keeps track of all the instructions inserted by the
43990b57cec5SDimitry Andric   /// other CodeGenPrepare optimizations. This information is important
44000b57cec5SDimitry Andric   /// because we do not want to promote these instructions as CodeGenPrepare
44010b57cec5SDimitry Andric   /// will reinsert them later. Thus creating an infinite loop: create/remove.
44020b57cec5SDimitry Andric   /// \p PromotedInsts maps the instructions to their type before promotion.
44030b57cec5SDimitry Andric   static Action getAction(Instruction *Ext, const SetOfInstrs &InsertedInsts,
44040b57cec5SDimitry Andric                           const TargetLowering &TLI,
44050b57cec5SDimitry Andric                           const InstrToOrigTy &PromotedInsts);
44060b57cec5SDimitry Andric };
44070b57cec5SDimitry Andric 
44080b57cec5SDimitry Andric } // end anonymous namespace
44090b57cec5SDimitry Andric 
canGetThrough(const Instruction * Inst,Type * ConsideredExtType,const InstrToOrigTy & PromotedInsts,bool IsSExt)44100b57cec5SDimitry Andric bool TypePromotionHelper::canGetThrough(const Instruction *Inst,
44110b57cec5SDimitry Andric                                         Type *ConsideredExtType,
44120b57cec5SDimitry Andric                                         const InstrToOrigTy &PromotedInsts,
44130b57cec5SDimitry Andric                                         bool IsSExt) {
44140b57cec5SDimitry Andric   // The promotion helper does not know how to deal with vector types yet.
44150b57cec5SDimitry Andric   // To be able to fix that, we would need to fix the places where we
44160b57cec5SDimitry Andric   // statically extend, e.g., constants and such.
44170b57cec5SDimitry Andric   if (Inst->getType()->isVectorTy())
44180b57cec5SDimitry Andric     return false;
44190b57cec5SDimitry Andric 
44200b57cec5SDimitry Andric   // We can always get through zext.
44210b57cec5SDimitry Andric   if (isa<ZExtInst>(Inst))
44220b57cec5SDimitry Andric     return true;
44230b57cec5SDimitry Andric 
44240b57cec5SDimitry Andric   // sext(sext) is ok too.
44250b57cec5SDimitry Andric   if (IsSExt && isa<SExtInst>(Inst))
44260b57cec5SDimitry Andric     return true;
44270b57cec5SDimitry Andric 
44280b57cec5SDimitry Andric   // We can get through binary operator, if it is legal. In other words, the
44290b57cec5SDimitry Andric   // binary operator must have a nuw or nsw flag.
443004eeddc0SDimitry Andric   if (const auto *BinOp = dyn_cast<BinaryOperator>(Inst))
443104eeddc0SDimitry Andric     if (isa<OverflowingBinaryOperator>(BinOp) &&
44320b57cec5SDimitry Andric         ((!IsSExt && BinOp->hasNoUnsignedWrap()) ||
44330b57cec5SDimitry Andric          (IsSExt && BinOp->hasNoSignedWrap())))
44340b57cec5SDimitry Andric       return true;
44350b57cec5SDimitry Andric 
44360b57cec5SDimitry Andric   // ext(and(opnd, cst)) --> and(ext(opnd), ext(cst))
44370b57cec5SDimitry Andric   if ((Inst->getOpcode() == Instruction::And ||
44380b57cec5SDimitry Andric        Inst->getOpcode() == Instruction::Or))
44390b57cec5SDimitry Andric     return true;
44400b57cec5SDimitry Andric 
44410b57cec5SDimitry Andric   // ext(xor(opnd, cst)) --> xor(ext(opnd), ext(cst))
44420b57cec5SDimitry Andric   if (Inst->getOpcode() == Instruction::Xor) {
44430b57cec5SDimitry Andric     // Make sure it is not a NOT.
444404eeddc0SDimitry Andric     if (const auto *Cst = dyn_cast<ConstantInt>(Inst->getOperand(1)))
444504eeddc0SDimitry Andric       if (!Cst->getValue().isAllOnes())
44460b57cec5SDimitry Andric         return true;
44470b57cec5SDimitry Andric   }
44480b57cec5SDimitry Andric 
44490b57cec5SDimitry Andric   // zext(shrl(opnd, cst)) --> shrl(zext(opnd), zext(cst))
44500b57cec5SDimitry Andric   // It may change a poisoned value into a regular value, like
44510b57cec5SDimitry Andric   //     zext i32 (shrl i8 %val, 12)  -->  shrl i32 (zext i8 %val), 12
44520b57cec5SDimitry Andric   //          poisoned value                    regular value
44530b57cec5SDimitry Andric   // It should be OK since undef covers valid value.
44540b57cec5SDimitry Andric   if (Inst->getOpcode() == Instruction::LShr && !IsSExt)
44550b57cec5SDimitry Andric     return true;
44560b57cec5SDimitry Andric 
44570b57cec5SDimitry Andric   // and(ext(shl(opnd, cst)), cst) --> and(shl(ext(opnd), ext(cst)), cst)
44580b57cec5SDimitry Andric   // It may change a poisoned value into a regular value, like
44590b57cec5SDimitry Andric   //     zext i32 (shl i8 %val, 12)  -->  shl i32 (zext i8 %val), 12
44600b57cec5SDimitry Andric   //          poisoned value                    regular value
44610b57cec5SDimitry Andric   // It should be OK since undef covers valid value.
44620b57cec5SDimitry Andric   if (Inst->getOpcode() == Instruction::Shl && Inst->hasOneUse()) {
44638bcb0991SDimitry Andric     const auto *ExtInst = cast<const Instruction>(*Inst->user_begin());
44640b57cec5SDimitry Andric     if (ExtInst->hasOneUse()) {
44658bcb0991SDimitry Andric       const auto *AndInst = dyn_cast<const Instruction>(*ExtInst->user_begin());
44660b57cec5SDimitry Andric       if (AndInst && AndInst->getOpcode() == Instruction::And) {
44678bcb0991SDimitry Andric         const auto *Cst = dyn_cast<ConstantInt>(AndInst->getOperand(1));
44680b57cec5SDimitry Andric         if (Cst &&
44690b57cec5SDimitry Andric             Cst->getValue().isIntN(Inst->getType()->getIntegerBitWidth()))
44700b57cec5SDimitry Andric           return true;
44710b57cec5SDimitry Andric       }
44720b57cec5SDimitry Andric     }
44730b57cec5SDimitry Andric   }
44740b57cec5SDimitry Andric 
44750b57cec5SDimitry Andric   // Check if we can do the following simplification.
44760b57cec5SDimitry Andric   // ext(trunc(opnd)) --> ext(opnd)
44770b57cec5SDimitry Andric   if (!isa<TruncInst>(Inst))
44780b57cec5SDimitry Andric     return false;
44790b57cec5SDimitry Andric 
44800b57cec5SDimitry Andric   Value *OpndVal = Inst->getOperand(0);
44810b57cec5SDimitry Andric   // Check if we can use this operand in the extension.
44820b57cec5SDimitry Andric   // If the type is larger than the result type of the extension, we cannot.
44830b57cec5SDimitry Andric   if (!OpndVal->getType()->isIntegerTy() ||
44840b57cec5SDimitry Andric       OpndVal->getType()->getIntegerBitWidth() >
44850b57cec5SDimitry Andric           ConsideredExtType->getIntegerBitWidth())
44860b57cec5SDimitry Andric     return false;
44870b57cec5SDimitry Andric 
44880b57cec5SDimitry Andric   // If the operand of the truncate is not an instruction, we will not have
44890b57cec5SDimitry Andric   // any information on the dropped bits.
44900b57cec5SDimitry Andric   // (Actually we could for constant but it is not worth the extra logic).
44910b57cec5SDimitry Andric   Instruction *Opnd = dyn_cast<Instruction>(OpndVal);
44920b57cec5SDimitry Andric   if (!Opnd)
44930b57cec5SDimitry Andric     return false;
44940b57cec5SDimitry Andric 
44950b57cec5SDimitry Andric   // Check if the source of the type is narrow enough.
44960b57cec5SDimitry Andric   // I.e., check that trunc just drops extended bits of the same kind of
44970b57cec5SDimitry Andric   // the extension.
44980b57cec5SDimitry Andric   // #1 get the type of the operand and check the kind of the extended bits.
44990b57cec5SDimitry Andric   const Type *OpndType = getOrigType(PromotedInsts, Opnd, IsSExt);
45000b57cec5SDimitry Andric   if (OpndType)
45010b57cec5SDimitry Andric     ;
45020b57cec5SDimitry Andric   else if ((IsSExt && isa<SExtInst>(Opnd)) || (!IsSExt && isa<ZExtInst>(Opnd)))
45030b57cec5SDimitry Andric     OpndType = Opnd->getOperand(0)->getType();
45040b57cec5SDimitry Andric   else
45050b57cec5SDimitry Andric     return false;
45060b57cec5SDimitry Andric 
45070b57cec5SDimitry Andric   // #2 check that the truncate just drops extended bits.
45080b57cec5SDimitry Andric   return Inst->getType()->getIntegerBitWidth() >=
45090b57cec5SDimitry Andric          OpndType->getIntegerBitWidth();
45100b57cec5SDimitry Andric }
45110b57cec5SDimitry Andric 
getAction(Instruction * Ext,const SetOfInstrs & InsertedInsts,const TargetLowering & TLI,const InstrToOrigTy & PromotedInsts)45120b57cec5SDimitry Andric TypePromotionHelper::Action TypePromotionHelper::getAction(
45130b57cec5SDimitry Andric     Instruction *Ext, const SetOfInstrs &InsertedInsts,
45140b57cec5SDimitry Andric     const TargetLowering &TLI, const InstrToOrigTy &PromotedInsts) {
45150b57cec5SDimitry Andric   assert((isa<SExtInst>(Ext) || isa<ZExtInst>(Ext)) &&
45160b57cec5SDimitry Andric          "Unexpected instruction type");
45170b57cec5SDimitry Andric   Instruction *ExtOpnd = dyn_cast<Instruction>(Ext->getOperand(0));
45180b57cec5SDimitry Andric   Type *ExtTy = Ext->getType();
45190b57cec5SDimitry Andric   bool IsSExt = isa<SExtInst>(Ext);
45200b57cec5SDimitry Andric   // If the operand of the extension is not an instruction, we cannot
45210b57cec5SDimitry Andric   // get through.
45220b57cec5SDimitry Andric   // If it, check we can get through.
45230b57cec5SDimitry Andric   if (!ExtOpnd || !canGetThrough(ExtOpnd, ExtTy, PromotedInsts, IsSExt))
45240b57cec5SDimitry Andric     return nullptr;
45250b57cec5SDimitry Andric 
45260b57cec5SDimitry Andric   // Do not promote if the operand has been added by codegenprepare.
45270b57cec5SDimitry Andric   // Otherwise, it means we are undoing an optimization that is likely to be
45280b57cec5SDimitry Andric   // redone, thus causing potential infinite loop.
45290b57cec5SDimitry Andric   if (isa<TruncInst>(ExtOpnd) && InsertedInsts.count(ExtOpnd))
45300b57cec5SDimitry Andric     return nullptr;
45310b57cec5SDimitry Andric 
45320b57cec5SDimitry Andric   // SExt or Trunc instructions.
45330b57cec5SDimitry Andric   // Return the related handler.
45340b57cec5SDimitry Andric   if (isa<SExtInst>(ExtOpnd) || isa<TruncInst>(ExtOpnd) ||
45350b57cec5SDimitry Andric       isa<ZExtInst>(ExtOpnd))
45360b57cec5SDimitry Andric     return promoteOperandForTruncAndAnyExt;
45370b57cec5SDimitry Andric 
45380b57cec5SDimitry Andric   // Regular instruction.
45390b57cec5SDimitry Andric   // Abort early if we will have to insert non-free instructions.
45400b57cec5SDimitry Andric   if (!ExtOpnd->hasOneUse() && !TLI.isTruncateFree(ExtTy, ExtOpnd->getType()))
45410b57cec5SDimitry Andric     return nullptr;
45420b57cec5SDimitry Andric   return IsSExt ? signExtendOperandForOther : zeroExtendOperandForOther;
45430b57cec5SDimitry Andric }
45440b57cec5SDimitry Andric 
promoteOperandForTruncAndAnyExt(Instruction * SExt,TypePromotionTransaction & TPT,InstrToOrigTy & PromotedInsts,unsigned & CreatedInstsCost,SmallVectorImpl<Instruction * > * Exts,SmallVectorImpl<Instruction * > * Truncs,const TargetLowering & TLI)45450b57cec5SDimitry Andric Value *TypePromotionHelper::promoteOperandForTruncAndAnyExt(
45460b57cec5SDimitry Andric     Instruction *SExt, TypePromotionTransaction &TPT,
45470b57cec5SDimitry Andric     InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
45480b57cec5SDimitry Andric     SmallVectorImpl<Instruction *> *Exts,
45490b57cec5SDimitry Andric     SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI) {
45500b57cec5SDimitry Andric   // By construction, the operand of SExt is an instruction. Otherwise we cannot
45510b57cec5SDimitry Andric   // get through it and this method should not be called.
45520b57cec5SDimitry Andric   Instruction *SExtOpnd = cast<Instruction>(SExt->getOperand(0));
45530b57cec5SDimitry Andric   Value *ExtVal = SExt;
45540b57cec5SDimitry Andric   bool HasMergedNonFreeExt = false;
45550b57cec5SDimitry Andric   if (isa<ZExtInst>(SExtOpnd)) {
45560b57cec5SDimitry Andric     // Replace s|zext(zext(opnd))
45570b57cec5SDimitry Andric     // => zext(opnd).
45580b57cec5SDimitry Andric     HasMergedNonFreeExt = !TLI.isExtFree(SExtOpnd);
45590b57cec5SDimitry Andric     Value *ZExt =
45600b57cec5SDimitry Andric         TPT.createZExt(SExt, SExtOpnd->getOperand(0), SExt->getType());
45610b57cec5SDimitry Andric     TPT.replaceAllUsesWith(SExt, ZExt);
45620b57cec5SDimitry Andric     TPT.eraseInstruction(SExt);
45630b57cec5SDimitry Andric     ExtVal = ZExt;
45640b57cec5SDimitry Andric   } else {
45650b57cec5SDimitry Andric     // Replace z|sext(trunc(opnd)) or sext(sext(opnd))
45660b57cec5SDimitry Andric     // => z|sext(opnd).
45670b57cec5SDimitry Andric     TPT.setOperand(SExt, 0, SExtOpnd->getOperand(0));
45680b57cec5SDimitry Andric   }
45690b57cec5SDimitry Andric   CreatedInstsCost = 0;
45700b57cec5SDimitry Andric 
45710b57cec5SDimitry Andric   // Remove dead code.
45720b57cec5SDimitry Andric   if (SExtOpnd->use_empty())
45730b57cec5SDimitry Andric     TPT.eraseInstruction(SExtOpnd);
45740b57cec5SDimitry Andric 
45750b57cec5SDimitry Andric   // Check if the extension is still needed.
45760b57cec5SDimitry Andric   Instruction *ExtInst = dyn_cast<Instruction>(ExtVal);
45770b57cec5SDimitry Andric   if (!ExtInst || ExtInst->getType() != ExtInst->getOperand(0)->getType()) {
45780b57cec5SDimitry Andric     if (ExtInst) {
45790b57cec5SDimitry Andric       if (Exts)
45800b57cec5SDimitry Andric         Exts->push_back(ExtInst);
45810b57cec5SDimitry Andric       CreatedInstsCost = !TLI.isExtFree(ExtInst) && !HasMergedNonFreeExt;
45820b57cec5SDimitry Andric     }
45830b57cec5SDimitry Andric     return ExtVal;
45840b57cec5SDimitry Andric   }
45850b57cec5SDimitry Andric 
45860b57cec5SDimitry Andric   // At this point we have: ext ty opnd to ty.
45870b57cec5SDimitry Andric   // Reassign the uses of ExtInst to the opnd and remove ExtInst.
45880b57cec5SDimitry Andric   Value *NextVal = ExtInst->getOperand(0);
45890b57cec5SDimitry Andric   TPT.eraseInstruction(ExtInst, NextVal);
45900b57cec5SDimitry Andric   return NextVal;
45910b57cec5SDimitry Andric }
45920b57cec5SDimitry Andric 
promoteOperandForOther(Instruction * Ext,TypePromotionTransaction & TPT,InstrToOrigTy & PromotedInsts,unsigned & CreatedInstsCost,SmallVectorImpl<Instruction * > * Exts,SmallVectorImpl<Instruction * > * Truncs,const TargetLowering & TLI,bool IsSExt)45930b57cec5SDimitry Andric Value *TypePromotionHelper::promoteOperandForOther(
45940b57cec5SDimitry Andric     Instruction *Ext, TypePromotionTransaction &TPT,
45950b57cec5SDimitry Andric     InstrToOrigTy &PromotedInsts, unsigned &CreatedInstsCost,
45960b57cec5SDimitry Andric     SmallVectorImpl<Instruction *> *Exts,
45970b57cec5SDimitry Andric     SmallVectorImpl<Instruction *> *Truncs, const TargetLowering &TLI,
45980b57cec5SDimitry Andric     bool IsSExt) {
45990b57cec5SDimitry Andric   // By construction, the operand of Ext is an instruction. Otherwise we cannot
46000b57cec5SDimitry Andric   // get through it and this method should not be called.
46010b57cec5SDimitry Andric   Instruction *ExtOpnd = cast<Instruction>(Ext->getOperand(0));
46020b57cec5SDimitry Andric   CreatedInstsCost = 0;
46030b57cec5SDimitry Andric   if (!ExtOpnd->hasOneUse()) {
46040b57cec5SDimitry Andric     // ExtOpnd will be promoted.
46050b57cec5SDimitry Andric     // All its uses, but Ext, will need to use a truncated value of the
46060b57cec5SDimitry Andric     // promoted version.
46070b57cec5SDimitry Andric     // Create the truncate now.
46080b57cec5SDimitry Andric     Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType());
46090b57cec5SDimitry Andric     if (Instruction *ITrunc = dyn_cast<Instruction>(Trunc)) {
46100b57cec5SDimitry Andric       // Insert it just after the definition.
46110b57cec5SDimitry Andric       ITrunc->moveAfter(ExtOpnd);
46120b57cec5SDimitry Andric       if (Truncs)
46130b57cec5SDimitry Andric         Truncs->push_back(ITrunc);
46140b57cec5SDimitry Andric     }
46150b57cec5SDimitry Andric 
46160b57cec5SDimitry Andric     TPT.replaceAllUsesWith(ExtOpnd, Trunc);
46170b57cec5SDimitry Andric     // Restore the operand of Ext (which has been replaced by the previous call
46180b57cec5SDimitry Andric     // to replaceAllUsesWith) to avoid creating a cycle trunc <-> sext.
46190b57cec5SDimitry Andric     TPT.setOperand(Ext, 0, ExtOpnd);
46200b57cec5SDimitry Andric   }
46210b57cec5SDimitry Andric 
46220b57cec5SDimitry Andric   // Get through the Instruction:
46230b57cec5SDimitry Andric   // 1. Update its type.
46240b57cec5SDimitry Andric   // 2. Replace the uses of Ext by Inst.
46250b57cec5SDimitry Andric   // 3. Extend each operand that needs to be extended.
46260b57cec5SDimitry Andric 
46270b57cec5SDimitry Andric   // Remember the original type of the instruction before promotion.
46280b57cec5SDimitry Andric   // This is useful to know that the high bits are sign extended bits.
46290b57cec5SDimitry Andric   addPromotedInst(PromotedInsts, ExtOpnd, IsSExt);
46300b57cec5SDimitry Andric   // Step #1.
46310b57cec5SDimitry Andric   TPT.mutateType(ExtOpnd, Ext->getType());
46320b57cec5SDimitry Andric   // Step #2.
46330b57cec5SDimitry Andric   TPT.replaceAllUsesWith(Ext, ExtOpnd);
46340b57cec5SDimitry Andric   // Step #3.
46350b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Propagate Ext to operands\n");
46360b57cec5SDimitry Andric   for (int OpIdx = 0, EndOpIdx = ExtOpnd->getNumOperands(); OpIdx != EndOpIdx;
46370b57cec5SDimitry Andric        ++OpIdx) {
46380b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Operand:\n" << *(ExtOpnd->getOperand(OpIdx)) << '\n');
46390b57cec5SDimitry Andric     if (ExtOpnd->getOperand(OpIdx)->getType() == Ext->getType() ||
46400b57cec5SDimitry Andric         !shouldExtOperand(ExtOpnd, OpIdx)) {
46410b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "No need to propagate\n");
46420b57cec5SDimitry Andric       continue;
46430b57cec5SDimitry Andric     }
46440b57cec5SDimitry Andric     // Check if we can statically extend the operand.
46450b57cec5SDimitry Andric     Value *Opnd = ExtOpnd->getOperand(OpIdx);
46460b57cec5SDimitry Andric     if (const ConstantInt *Cst = dyn_cast<ConstantInt>(Opnd)) {
46470b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Statically extend\n");
46480b57cec5SDimitry Andric       unsigned BitWidth = Ext->getType()->getIntegerBitWidth();
46490b57cec5SDimitry Andric       APInt CstVal = IsSExt ? Cst->getValue().sext(BitWidth)
46500b57cec5SDimitry Andric                             : Cst->getValue().zext(BitWidth);
46510b57cec5SDimitry Andric       TPT.setOperand(ExtOpnd, OpIdx, ConstantInt::get(Ext->getType(), CstVal));
46520b57cec5SDimitry Andric       continue;
46530b57cec5SDimitry Andric     }
46540b57cec5SDimitry Andric     // UndefValue are typed, so we have to statically sign extend them.
46550b57cec5SDimitry Andric     if (isa<UndefValue>(Opnd)) {
46560b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Statically extend\n");
46570b57cec5SDimitry Andric       TPT.setOperand(ExtOpnd, OpIdx, UndefValue::get(Ext->getType()));
46580b57cec5SDimitry Andric       continue;
46590b57cec5SDimitry Andric     }
46600b57cec5SDimitry Andric 
46610b57cec5SDimitry Andric     // Otherwise we have to explicitly sign extend the operand.
4662c9157d92SDimitry Andric     Value *ValForExtOpnd = IsSExt
4663c9157d92SDimitry Andric                                ? TPT.createSExt(ExtOpnd, Opnd, Ext->getType())
4664c9157d92SDimitry Andric                                : TPT.createZExt(ExtOpnd, Opnd, Ext->getType());
46650b57cec5SDimitry Andric     TPT.setOperand(ExtOpnd, OpIdx, ValForExtOpnd);
4666c9157d92SDimitry Andric     Instruction *InstForExtOpnd = dyn_cast<Instruction>(ValForExtOpnd);
4667c9157d92SDimitry Andric     if (!InstForExtOpnd)
46680b57cec5SDimitry Andric       continue;
46690b57cec5SDimitry Andric 
4670c9157d92SDimitry Andric     if (Exts)
4671c9157d92SDimitry Andric       Exts->push_back(InstForExtOpnd);
4672c9157d92SDimitry Andric 
4673c9157d92SDimitry Andric     CreatedInstsCost += !TLI.isExtFree(InstForExtOpnd);
46740b57cec5SDimitry Andric   }
46750b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Extension is useless now\n");
46760b57cec5SDimitry Andric   TPT.eraseInstruction(Ext);
46770b57cec5SDimitry Andric   return ExtOpnd;
46780b57cec5SDimitry Andric }
46790b57cec5SDimitry Andric 
46800b57cec5SDimitry Andric /// Check whether or not promoting an instruction to a wider type is profitable.
46810b57cec5SDimitry Andric /// \p NewCost gives the cost of extension instructions created by the
46820b57cec5SDimitry Andric /// promotion.
46830b57cec5SDimitry Andric /// \p OldCost gives the cost of extension instructions before the promotion
46840b57cec5SDimitry Andric /// plus the number of instructions that have been
46850b57cec5SDimitry Andric /// matched in the addressing mode the promotion.
46860b57cec5SDimitry Andric /// \p PromotedOperand is the value that has been promoted.
46870b57cec5SDimitry Andric /// \return True if the promotion is profitable, false otherwise.
isPromotionProfitable(unsigned NewCost,unsigned OldCost,Value * PromotedOperand) const46880b57cec5SDimitry Andric bool AddressingModeMatcher::isPromotionProfitable(
46890b57cec5SDimitry Andric     unsigned NewCost, unsigned OldCost, Value *PromotedOperand) const {
46900b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "OldCost: " << OldCost << "\tNewCost: " << NewCost
46910b57cec5SDimitry Andric                     << '\n');
46920b57cec5SDimitry Andric   // The cost of the new extensions is greater than the cost of the
46930b57cec5SDimitry Andric   // old extension plus what we folded.
46940b57cec5SDimitry Andric   // This is not profitable.
46950b57cec5SDimitry Andric   if (NewCost > OldCost)
46960b57cec5SDimitry Andric     return false;
46970b57cec5SDimitry Andric   if (NewCost < OldCost)
46980b57cec5SDimitry Andric     return true;
46990b57cec5SDimitry Andric   // The promotion is neutral but it may help folding the sign extension in
47000b57cec5SDimitry Andric   // loads for instance.
47010b57cec5SDimitry Andric   // Check that we did not create an illegal instruction.
47020b57cec5SDimitry Andric   return isPromotedInstructionLegal(TLI, DL, PromotedOperand);
47030b57cec5SDimitry Andric }
47040b57cec5SDimitry Andric 
47050b57cec5SDimitry Andric /// Given an instruction or constant expr, see if we can fold the operation
47060b57cec5SDimitry Andric /// into the addressing mode. If so, update the addressing mode and return
47070b57cec5SDimitry Andric /// true, otherwise return false without modifying AddrMode.
47080b57cec5SDimitry Andric /// If \p MovedAway is not NULL, it contains the information of whether or
47090b57cec5SDimitry Andric /// not AddrInst has to be folded into the addressing mode on success.
47100b57cec5SDimitry Andric /// If \p MovedAway == true, \p AddrInst will not be part of the addressing
47110b57cec5SDimitry Andric /// because it has been moved away.
47120b57cec5SDimitry Andric /// Thus AddrInst must not be added in the matched instructions.
47130b57cec5SDimitry Andric /// This state can happen when AddrInst is a sext, since it may be moved away.
47140b57cec5SDimitry Andric /// Therefore, AddrInst may not be valid when MovedAway is true and it must
47150b57cec5SDimitry Andric /// not be referenced anymore.
matchOperationAddr(User * AddrInst,unsigned Opcode,unsigned Depth,bool * MovedAway)47160b57cec5SDimitry Andric bool AddressingModeMatcher::matchOperationAddr(User *AddrInst, unsigned Opcode,
47170b57cec5SDimitry Andric                                                unsigned Depth,
47180b57cec5SDimitry Andric                                                bool *MovedAway) {
47190b57cec5SDimitry Andric   // Avoid exponential behavior on extremely deep expression trees.
4720bdd1243dSDimitry Andric   if (Depth >= 5)
4721bdd1243dSDimitry Andric     return false;
47220b57cec5SDimitry Andric 
47230b57cec5SDimitry Andric   // By default, all matched instructions stay in place.
47240b57cec5SDimitry Andric   if (MovedAway)
47250b57cec5SDimitry Andric     *MovedAway = false;
47260b57cec5SDimitry Andric 
47270b57cec5SDimitry Andric   switch (Opcode) {
47280b57cec5SDimitry Andric   case Instruction::PtrToInt:
47290b57cec5SDimitry Andric     // PtrToInt is always a noop, as we know that the int type is pointer sized.
47300b57cec5SDimitry Andric     return matchAddr(AddrInst->getOperand(0), Depth);
47310b57cec5SDimitry Andric   case Instruction::IntToPtr: {
47320b57cec5SDimitry Andric     auto AS = AddrInst->getType()->getPointerAddressSpace();
47330b57cec5SDimitry Andric     auto PtrTy = MVT::getIntegerVT(DL.getPointerSizeInBits(AS));
47340b57cec5SDimitry Andric     // This inttoptr is a no-op if the integer type is pointer sized.
47350b57cec5SDimitry Andric     if (TLI.getValueType(DL, AddrInst->getOperand(0)->getType()) == PtrTy)
47360b57cec5SDimitry Andric       return matchAddr(AddrInst->getOperand(0), Depth);
47370b57cec5SDimitry Andric     return false;
47380b57cec5SDimitry Andric   }
47390b57cec5SDimitry Andric   case Instruction::BitCast:
47400b57cec5SDimitry Andric     // BitCast is always a noop, and we can handle it as long as it is
47410b57cec5SDimitry Andric     // int->int or pointer->pointer (we don't want int<->fp or something).
47420b57cec5SDimitry Andric     if (AddrInst->getOperand(0)->getType()->isIntOrPtrTy() &&
47430b57cec5SDimitry Andric         // Don't touch identity bitcasts.  These were probably put here by LSR,
47440b57cec5SDimitry Andric         // and we don't want to mess around with them.  Assume it knows what it
47450b57cec5SDimitry Andric         // is doing.
47460b57cec5SDimitry Andric         AddrInst->getOperand(0)->getType() != AddrInst->getType())
47470b57cec5SDimitry Andric       return matchAddr(AddrInst->getOperand(0), Depth);
47480b57cec5SDimitry Andric     return false;
47490b57cec5SDimitry Andric   case Instruction::AddrSpaceCast: {
4750bdd1243dSDimitry Andric     unsigned SrcAS =
4751bdd1243dSDimitry Andric         AddrInst->getOperand(0)->getType()->getPointerAddressSpace();
47520b57cec5SDimitry Andric     unsigned DestAS = AddrInst->getType()->getPointerAddressSpace();
4753e8d8bef9SDimitry Andric     if (TLI.getTargetMachine().isNoopAddrSpaceCast(SrcAS, DestAS))
47540b57cec5SDimitry Andric       return matchAddr(AddrInst->getOperand(0), Depth);
47550b57cec5SDimitry Andric     return false;
47560b57cec5SDimitry Andric   }
47570b57cec5SDimitry Andric   case Instruction::Add: {
4758fe013be4SDimitry Andric     // Check to see if we can merge in one operand, then the other.  If so, we
4759fe013be4SDimitry Andric     // win.
47600b57cec5SDimitry Andric     ExtAddrMode BackupAddrMode = AddrMode;
47610b57cec5SDimitry Andric     unsigned OldSize = AddrModeInsts.size();
47620b57cec5SDimitry Andric     // Start a transaction at this point.
47630b57cec5SDimitry Andric     // The LHS may match but not the RHS.
47640b57cec5SDimitry Andric     // Therefore, we need a higher level restoration point to undo partially
47650b57cec5SDimitry Andric     // matched operation.
47660b57cec5SDimitry Andric     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
47670b57cec5SDimitry Andric         TPT.getRestorationPoint();
47680b57cec5SDimitry Andric 
4769fe013be4SDimitry Andric     // Try to match an integer constant second to increase its chance of ending
4770fe013be4SDimitry Andric     // up in `BaseOffs`, resp. decrease its chance of ending up in `BaseReg`.
4771fe013be4SDimitry Andric     int First = 0, Second = 1;
4772fe013be4SDimitry Andric     if (isa<ConstantInt>(AddrInst->getOperand(First))
4773fe013be4SDimitry Andric       && !isa<ConstantInt>(AddrInst->getOperand(Second)))
4774fe013be4SDimitry Andric         std::swap(First, Second);
47750b57cec5SDimitry Andric     AddrMode.InBounds = false;
4776fe013be4SDimitry Andric     if (matchAddr(AddrInst->getOperand(First), Depth + 1) &&
4777fe013be4SDimitry Andric         matchAddr(AddrInst->getOperand(Second), Depth + 1))
47780b57cec5SDimitry Andric       return true;
47790b57cec5SDimitry Andric 
47800b57cec5SDimitry Andric     // Restore the old addr mode info.
47810b57cec5SDimitry Andric     AddrMode = BackupAddrMode;
47820b57cec5SDimitry Andric     AddrModeInsts.resize(OldSize);
47830b57cec5SDimitry Andric     TPT.rollback(LastKnownGood);
47840b57cec5SDimitry Andric 
4785fe013be4SDimitry Andric     // Otherwise this was over-aggressive.  Try merging operands in the opposite
4786fe013be4SDimitry Andric     // order.
4787fe013be4SDimitry Andric     if (matchAddr(AddrInst->getOperand(Second), Depth + 1) &&
4788fe013be4SDimitry Andric         matchAddr(AddrInst->getOperand(First), Depth + 1))
47890b57cec5SDimitry Andric       return true;
47900b57cec5SDimitry Andric 
47910b57cec5SDimitry Andric     // Otherwise we definitely can't merge the ADD in.
47920b57cec5SDimitry Andric     AddrMode = BackupAddrMode;
47930b57cec5SDimitry Andric     AddrModeInsts.resize(OldSize);
47940b57cec5SDimitry Andric     TPT.rollback(LastKnownGood);
47950b57cec5SDimitry Andric     break;
47960b57cec5SDimitry Andric   }
47970b57cec5SDimitry Andric   // case Instruction::Or:
47980b57cec5SDimitry Andric   //  TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD.
47990b57cec5SDimitry Andric   // break;
48000b57cec5SDimitry Andric   case Instruction::Mul:
48010b57cec5SDimitry Andric   case Instruction::Shl: {
48020b57cec5SDimitry Andric     // Can only handle X*C and X << C.
48030b57cec5SDimitry Andric     AddrMode.InBounds = false;
48040b57cec5SDimitry Andric     ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1));
48050b57cec5SDimitry Andric     if (!RHS || RHS->getBitWidth() > 64)
48060b57cec5SDimitry Andric       return false;
480781ad6265SDimitry Andric     int64_t Scale = Opcode == Instruction::Shl
480881ad6265SDimitry Andric                         ? 1LL << RHS->getLimitedValue(RHS->getBitWidth() - 1)
480981ad6265SDimitry Andric                         : RHS->getSExtValue();
48100b57cec5SDimitry Andric 
48110b57cec5SDimitry Andric     return matchScaledValue(AddrInst->getOperand(0), Scale, Depth);
48120b57cec5SDimitry Andric   }
48130b57cec5SDimitry Andric   case Instruction::GetElementPtr: {
48140b57cec5SDimitry Andric     // Scan the GEP.  We check it if it contains constant offsets and at most
48150b57cec5SDimitry Andric     // one variable offset.
48160b57cec5SDimitry Andric     int VariableOperand = -1;
48170b57cec5SDimitry Andric     unsigned VariableScale = 0;
48180b57cec5SDimitry Andric 
48190b57cec5SDimitry Andric     int64_t ConstantOffset = 0;
48200b57cec5SDimitry Andric     gep_type_iterator GTI = gep_type_begin(AddrInst);
48210b57cec5SDimitry Andric     for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) {
48220b57cec5SDimitry Andric       if (StructType *STy = GTI.getStructTypeOrNull()) {
48230b57cec5SDimitry Andric         const StructLayout *SL = DL.getStructLayout(STy);
48240b57cec5SDimitry Andric         unsigned Idx =
48250b57cec5SDimitry Andric             cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue();
48260b57cec5SDimitry Andric         ConstantOffset += SL->getElementOffset(Idx);
48270b57cec5SDimitry Andric       } else {
4828cdc20ff6SDimitry Andric         TypeSize TS = GTI.getSequentialElementStride(DL);
48295ffd83dbSDimitry Andric         if (TS.isNonZero()) {
48305ffd83dbSDimitry Andric           // The optimisations below currently only work for fixed offsets.
48315ffd83dbSDimitry Andric           if (TS.isScalable())
48325ffd83dbSDimitry Andric             return false;
4833bdd1243dSDimitry Andric           int64_t TypeSize = TS.getFixedValue();
48345ffd83dbSDimitry Andric           if (ConstantInt *CI =
48355ffd83dbSDimitry Andric                   dyn_cast<ConstantInt>(AddrInst->getOperand(i))) {
48360b57cec5SDimitry Andric             const APInt &CVal = CI->getValue();
4837fe013be4SDimitry Andric             if (CVal.getSignificantBits() <= 64) {
48380b57cec5SDimitry Andric               ConstantOffset += CVal.getSExtValue() * TypeSize;
48390b57cec5SDimitry Andric               continue;
48400b57cec5SDimitry Andric             }
48410b57cec5SDimitry Andric           }
48420b57cec5SDimitry Andric           // We only allow one variable index at the moment.
48430b57cec5SDimitry Andric           if (VariableOperand != -1)
48440b57cec5SDimitry Andric             return false;
48450b57cec5SDimitry Andric 
48460b57cec5SDimitry Andric           // Remember the variable index.
48470b57cec5SDimitry Andric           VariableOperand = i;
48480b57cec5SDimitry Andric           VariableScale = TypeSize;
48490b57cec5SDimitry Andric         }
48500b57cec5SDimitry Andric       }
48510b57cec5SDimitry Andric     }
48520b57cec5SDimitry Andric 
48530b57cec5SDimitry Andric     // A common case is for the GEP to only do a constant offset.  In this case,
48540b57cec5SDimitry Andric     // just add it to the disp field and check validity.
48550b57cec5SDimitry Andric     if (VariableOperand == -1) {
48560b57cec5SDimitry Andric       AddrMode.BaseOffs += ConstantOffset;
48570b57cec5SDimitry Andric       if (matchAddr(AddrInst->getOperand(0), Depth + 1)) {
48580b57cec5SDimitry Andric           if (!cast<GEPOperator>(AddrInst)->isInBounds())
48590b57cec5SDimitry Andric             AddrMode.InBounds = false;
48600b57cec5SDimitry Andric           return true;
48610b57cec5SDimitry Andric       }
4862fe013be4SDimitry Andric       AddrMode.BaseOffs -= ConstantOffset;
4863fe013be4SDimitry Andric 
4864fe013be4SDimitry Andric       if (EnableGEPOffsetSplit && isa<GetElementPtrInst>(AddrInst) &&
48650b57cec5SDimitry Andric           TLI.shouldConsiderGEPOffsetSplit() && Depth == 0 &&
48660b57cec5SDimitry Andric           ConstantOffset > 0) {
4867fe013be4SDimitry Andric           // Record GEPs with non-zero offsets as candidates for splitting in
4868fe013be4SDimitry Andric           // the event that the offset cannot fit into the r+i addressing mode.
48690b57cec5SDimitry Andric           // Simple and common case that only one GEP is used in calculating the
48700b57cec5SDimitry Andric           // address for the memory access.
48710b57cec5SDimitry Andric           Value *Base = AddrInst->getOperand(0);
48720b57cec5SDimitry Andric           auto *BaseI = dyn_cast<Instruction>(Base);
48730b57cec5SDimitry Andric           auto *GEP = cast<GetElementPtrInst>(AddrInst);
48740b57cec5SDimitry Andric           if (isa<Argument>(Base) || isa<GlobalValue>(Base) ||
48750b57cec5SDimitry Andric               (BaseI && !isa<CastInst>(BaseI) &&
48760b57cec5SDimitry Andric                !isa<GetElementPtrInst>(BaseI))) {
48770b57cec5SDimitry Andric             // Make sure the parent block allows inserting non-PHI instructions
48780b57cec5SDimitry Andric             // before the terminator.
4879fe013be4SDimitry Andric             BasicBlock *Parent = BaseI ? BaseI->getParent()
4880fe013be4SDimitry Andric                                        : &GEP->getFunction()->getEntryBlock();
48810b57cec5SDimitry Andric             if (!Parent->getTerminator()->isEHPad())
48820b57cec5SDimitry Andric             LargeOffsetGEP = std::make_pair(GEP, ConstantOffset);
48830b57cec5SDimitry Andric           }
48840b57cec5SDimitry Andric       }
4885fe013be4SDimitry Andric 
48860b57cec5SDimitry Andric       return false;
48870b57cec5SDimitry Andric     }
48880b57cec5SDimitry Andric 
48890b57cec5SDimitry Andric     // Save the valid addressing mode in case we can't match.
48900b57cec5SDimitry Andric     ExtAddrMode BackupAddrMode = AddrMode;
48910b57cec5SDimitry Andric     unsigned OldSize = AddrModeInsts.size();
48920b57cec5SDimitry Andric 
48930b57cec5SDimitry Andric     // See if the scale and offset amount is valid for this target.
48940b57cec5SDimitry Andric     AddrMode.BaseOffs += ConstantOffset;
48950b57cec5SDimitry Andric     if (!cast<GEPOperator>(AddrInst)->isInBounds())
48960b57cec5SDimitry Andric       AddrMode.InBounds = false;
48970b57cec5SDimitry Andric 
48980b57cec5SDimitry Andric     // Match the base operand of the GEP.
48990b57cec5SDimitry Andric     if (!matchAddr(AddrInst->getOperand(0), Depth + 1)) {
49000b57cec5SDimitry Andric       // If it couldn't be matched, just stuff the value in a register.
49010b57cec5SDimitry Andric       if (AddrMode.HasBaseReg) {
49020b57cec5SDimitry Andric         AddrMode = BackupAddrMode;
49030b57cec5SDimitry Andric         AddrModeInsts.resize(OldSize);
49040b57cec5SDimitry Andric         return false;
49050b57cec5SDimitry Andric       }
49060b57cec5SDimitry Andric       AddrMode.HasBaseReg = true;
49070b57cec5SDimitry Andric       AddrMode.BaseReg = AddrInst->getOperand(0);
49080b57cec5SDimitry Andric     }
49090b57cec5SDimitry Andric 
49100b57cec5SDimitry Andric     // Match the remaining variable portion of the GEP.
49110b57cec5SDimitry Andric     if (!matchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale,
49120b57cec5SDimitry Andric                           Depth)) {
49130b57cec5SDimitry Andric       // If it couldn't be matched, try stuffing the base into a register
49140b57cec5SDimitry Andric       // instead of matching it, and retrying the match of the scale.
49150b57cec5SDimitry Andric       AddrMode = BackupAddrMode;
49160b57cec5SDimitry Andric       AddrModeInsts.resize(OldSize);
49170b57cec5SDimitry Andric       if (AddrMode.HasBaseReg)
49180b57cec5SDimitry Andric         return false;
49190b57cec5SDimitry Andric       AddrMode.HasBaseReg = true;
49200b57cec5SDimitry Andric       AddrMode.BaseReg = AddrInst->getOperand(0);
49210b57cec5SDimitry Andric       AddrMode.BaseOffs += ConstantOffset;
49220b57cec5SDimitry Andric       if (!matchScaledValue(AddrInst->getOperand(VariableOperand),
49230b57cec5SDimitry Andric                             VariableScale, Depth)) {
49240b57cec5SDimitry Andric         // If even that didn't work, bail.
49250b57cec5SDimitry Andric         AddrMode = BackupAddrMode;
49260b57cec5SDimitry Andric         AddrModeInsts.resize(OldSize);
49270b57cec5SDimitry Andric         return false;
49280b57cec5SDimitry Andric       }
49290b57cec5SDimitry Andric     }
49300b57cec5SDimitry Andric 
49310b57cec5SDimitry Andric     return true;
49320b57cec5SDimitry Andric   }
49330b57cec5SDimitry Andric   case Instruction::SExt:
49340b57cec5SDimitry Andric   case Instruction::ZExt: {
49350b57cec5SDimitry Andric     Instruction *Ext = dyn_cast<Instruction>(AddrInst);
49360b57cec5SDimitry Andric     if (!Ext)
49370b57cec5SDimitry Andric       return false;
49380b57cec5SDimitry Andric 
49390b57cec5SDimitry Andric     // Try to move this ext out of the way of the addressing mode.
49400b57cec5SDimitry Andric     // Ask for a method for doing so.
49410b57cec5SDimitry Andric     TypePromotionHelper::Action TPH =
49420b57cec5SDimitry Andric         TypePromotionHelper::getAction(Ext, InsertedInsts, TLI, PromotedInsts);
49430b57cec5SDimitry Andric     if (!TPH)
49440b57cec5SDimitry Andric       return false;
49450b57cec5SDimitry Andric 
49460b57cec5SDimitry Andric     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
49470b57cec5SDimitry Andric         TPT.getRestorationPoint();
49480b57cec5SDimitry Andric     unsigned CreatedInstsCost = 0;
49490b57cec5SDimitry Andric     unsigned ExtCost = !TLI.isExtFree(Ext);
49500b57cec5SDimitry Andric     Value *PromotedOperand =
49510b57cec5SDimitry Andric         TPH(Ext, TPT, PromotedInsts, CreatedInstsCost, nullptr, nullptr, TLI);
49520b57cec5SDimitry Andric     // SExt has been moved away.
49530b57cec5SDimitry Andric     // Thus either it will be rematched later in the recursive calls or it is
49540b57cec5SDimitry Andric     // gone. Anyway, we must not fold it into the addressing mode at this point.
49550b57cec5SDimitry Andric     // E.g.,
49560b57cec5SDimitry Andric     // op = add opnd, 1
49570b57cec5SDimitry Andric     // idx = ext op
49580b57cec5SDimitry Andric     // addr = gep base, idx
49590b57cec5SDimitry Andric     // is now:
49600b57cec5SDimitry Andric     // promotedOpnd = ext opnd            <- no match here
49610b57cec5SDimitry Andric     // op = promoted_add promotedOpnd, 1  <- match (later in recursive calls)
49620b57cec5SDimitry Andric     // addr = gep base, op                <- match
49630b57cec5SDimitry Andric     if (MovedAway)
49640b57cec5SDimitry Andric       *MovedAway = true;
49650b57cec5SDimitry Andric 
49660b57cec5SDimitry Andric     assert(PromotedOperand &&
49670b57cec5SDimitry Andric            "TypePromotionHelper should have filtered out those cases");
49680b57cec5SDimitry Andric 
49690b57cec5SDimitry Andric     ExtAddrMode BackupAddrMode = AddrMode;
49700b57cec5SDimitry Andric     unsigned OldSize = AddrModeInsts.size();
49710b57cec5SDimitry Andric 
49720b57cec5SDimitry Andric     if (!matchAddr(PromotedOperand, Depth) ||
49730b57cec5SDimitry Andric         // The total of the new cost is equal to the cost of the created
49740b57cec5SDimitry Andric         // instructions.
49750b57cec5SDimitry Andric         // The total of the old cost is equal to the cost of the extension plus
49760b57cec5SDimitry Andric         // what we have saved in the addressing mode.
49770b57cec5SDimitry Andric         !isPromotionProfitable(CreatedInstsCost,
49780b57cec5SDimitry Andric                                ExtCost + (AddrModeInsts.size() - OldSize),
49790b57cec5SDimitry Andric                                PromotedOperand)) {
49800b57cec5SDimitry Andric       AddrMode = BackupAddrMode;
49810b57cec5SDimitry Andric       AddrModeInsts.resize(OldSize);
49820b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Sign extension does not pay off: rollback\n");
49830b57cec5SDimitry Andric       TPT.rollback(LastKnownGood);
49840b57cec5SDimitry Andric       return false;
49850b57cec5SDimitry Andric     }
49860b57cec5SDimitry Andric     return true;
49870b57cec5SDimitry Andric   }
49880b57cec5SDimitry Andric   }
49890b57cec5SDimitry Andric   return false;
49900b57cec5SDimitry Andric }
49910b57cec5SDimitry Andric 
49920b57cec5SDimitry Andric /// If we can, try to add the value of 'Addr' into the current addressing mode.
49930b57cec5SDimitry Andric /// If Addr can't be added to AddrMode this returns false and leaves AddrMode
49940b57cec5SDimitry Andric /// unmodified. This assumes that Addr is either a pointer type or intptr_t
49950b57cec5SDimitry Andric /// for the target.
49960b57cec5SDimitry Andric ///
matchAddr(Value * Addr,unsigned Depth)49970b57cec5SDimitry Andric bool AddressingModeMatcher::matchAddr(Value *Addr, unsigned Depth) {
49980b57cec5SDimitry Andric   // Start a transaction at this point that we will rollback if the matching
49990b57cec5SDimitry Andric   // fails.
50000b57cec5SDimitry Andric   TypePromotionTransaction::ConstRestorationPt LastKnownGood =
50010b57cec5SDimitry Andric       TPT.getRestorationPoint();
50020b57cec5SDimitry Andric   if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
50035ffd83dbSDimitry Andric     if (CI->getValue().isSignedIntN(64)) {
50040b57cec5SDimitry Andric       // Fold in immediates if legal for the target.
50050b57cec5SDimitry Andric       AddrMode.BaseOffs += CI->getSExtValue();
50060b57cec5SDimitry Andric       if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
50070b57cec5SDimitry Andric         return true;
50080b57cec5SDimitry Andric       AddrMode.BaseOffs -= CI->getSExtValue();
50095ffd83dbSDimitry Andric     }
50100b57cec5SDimitry Andric   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
50110b57cec5SDimitry Andric     // If this is a global variable, try to fold it into the addressing mode.
50120b57cec5SDimitry Andric     if (!AddrMode.BaseGV) {
50130b57cec5SDimitry Andric       AddrMode.BaseGV = GV;
50140b57cec5SDimitry Andric       if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
50150b57cec5SDimitry Andric         return true;
50160b57cec5SDimitry Andric       AddrMode.BaseGV = nullptr;
50170b57cec5SDimitry Andric     }
50180b57cec5SDimitry Andric   } else if (Instruction *I = dyn_cast<Instruction>(Addr)) {
50190b57cec5SDimitry Andric     ExtAddrMode BackupAddrMode = AddrMode;
50200b57cec5SDimitry Andric     unsigned OldSize = AddrModeInsts.size();
50210b57cec5SDimitry Andric 
50220b57cec5SDimitry Andric     // Check to see if it is possible to fold this operation.
50230b57cec5SDimitry Andric     bool MovedAway = false;
50240b57cec5SDimitry Andric     if (matchOperationAddr(I, I->getOpcode(), Depth, &MovedAway)) {
50250b57cec5SDimitry Andric       // This instruction may have been moved away. If so, there is nothing
50260b57cec5SDimitry Andric       // to check here.
50270b57cec5SDimitry Andric       if (MovedAway)
50280b57cec5SDimitry Andric         return true;
50290b57cec5SDimitry Andric       // Okay, it's possible to fold this.  Check to see if it is actually
50300b57cec5SDimitry Andric       // *profitable* to do so.  We use a simple cost model to avoid increasing
50310b57cec5SDimitry Andric       // register pressure too much.
50320b57cec5SDimitry Andric       if (I->hasOneUse() ||
50330b57cec5SDimitry Andric           isProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) {
50340b57cec5SDimitry Andric         AddrModeInsts.push_back(I);
50350b57cec5SDimitry Andric         return true;
50360b57cec5SDimitry Andric       }
50370b57cec5SDimitry Andric 
50380b57cec5SDimitry Andric       // It isn't profitable to do this, roll back.
50390b57cec5SDimitry Andric       AddrMode = BackupAddrMode;
50400b57cec5SDimitry Andric       AddrModeInsts.resize(OldSize);
50410b57cec5SDimitry Andric       TPT.rollback(LastKnownGood);
50420b57cec5SDimitry Andric     }
50430b57cec5SDimitry Andric   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
50440b57cec5SDimitry Andric     if (matchOperationAddr(CE, CE->getOpcode(), Depth))
50450b57cec5SDimitry Andric       return true;
50460b57cec5SDimitry Andric     TPT.rollback(LastKnownGood);
50470b57cec5SDimitry Andric   } else if (isa<ConstantPointerNull>(Addr)) {
50480b57cec5SDimitry Andric     // Null pointer gets folded without affecting the addressing mode.
50490b57cec5SDimitry Andric     return true;
50500b57cec5SDimitry Andric   }
50510b57cec5SDimitry Andric 
50520b57cec5SDimitry Andric   // Worse case, the target should support [reg] addressing modes. :)
50530b57cec5SDimitry Andric   if (!AddrMode.HasBaseReg) {
50540b57cec5SDimitry Andric     AddrMode.HasBaseReg = true;
50550b57cec5SDimitry Andric     AddrMode.BaseReg = Addr;
50560b57cec5SDimitry Andric     // Still check for legality in case the target supports [imm] but not [i+r].
50570b57cec5SDimitry Andric     if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
50580b57cec5SDimitry Andric       return true;
50590b57cec5SDimitry Andric     AddrMode.HasBaseReg = false;
50600b57cec5SDimitry Andric     AddrMode.BaseReg = nullptr;
50610b57cec5SDimitry Andric   }
50620b57cec5SDimitry Andric 
50630b57cec5SDimitry Andric   // If the base register is already taken, see if we can do [r+r].
50640b57cec5SDimitry Andric   if (AddrMode.Scale == 0) {
50650b57cec5SDimitry Andric     AddrMode.Scale = 1;
50660b57cec5SDimitry Andric     AddrMode.ScaledReg = Addr;
50670b57cec5SDimitry Andric     if (TLI.isLegalAddressingMode(DL, AddrMode, AccessTy, AddrSpace))
50680b57cec5SDimitry Andric       return true;
50690b57cec5SDimitry Andric     AddrMode.Scale = 0;
50700b57cec5SDimitry Andric     AddrMode.ScaledReg = nullptr;
50710b57cec5SDimitry Andric   }
50720b57cec5SDimitry Andric   // Couldn't match.
50730b57cec5SDimitry Andric   TPT.rollback(LastKnownGood);
50740b57cec5SDimitry Andric   return false;
50750b57cec5SDimitry Andric }
50760b57cec5SDimitry Andric 
50770b57cec5SDimitry Andric /// Check to see if all uses of OpVal by the specified inline asm call are due
50780b57cec5SDimitry Andric /// to memory operands. If so, return true, otherwise return false.
IsOperandAMemoryOperand(CallInst * CI,InlineAsm * IA,Value * OpVal,const TargetLowering & TLI,const TargetRegisterInfo & TRI)50790b57cec5SDimitry Andric static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal,
50800b57cec5SDimitry Andric                                     const TargetLowering &TLI,
50810b57cec5SDimitry Andric                                     const TargetRegisterInfo &TRI) {
50820b57cec5SDimitry Andric   const Function *F = CI->getFunction();
50830b57cec5SDimitry Andric   TargetLowering::AsmOperandInfoVector TargetConstraints =
50845ffd83dbSDimitry Andric       TLI.ParseConstraints(F->getParent()->getDataLayout(), &TRI, *CI);
50850b57cec5SDimitry Andric 
50860eae32dcSDimitry Andric   for (TargetLowering::AsmOperandInfo &OpInfo : TargetConstraints) {
50870b57cec5SDimitry Andric     // Compute the constraint code and ConstraintType to use.
50880b57cec5SDimitry Andric     TLI.ComputeConstraintToUse(OpInfo, SDValue());
50890b57cec5SDimitry Andric 
50900b57cec5SDimitry Andric     // If this asm operand is our Value*, and if it isn't an indirect memory
509181ad6265SDimitry Andric     // operand, we can't fold it!  TODO: Also handle C_Address?
50920b57cec5SDimitry Andric     if (OpInfo.CallOperandVal == OpVal &&
50930b57cec5SDimitry Andric         (OpInfo.ConstraintType != TargetLowering::C_Memory ||
50940b57cec5SDimitry Andric          !OpInfo.isIndirect))
50950b57cec5SDimitry Andric       return false;
50960b57cec5SDimitry Andric   }
50970b57cec5SDimitry Andric 
50980b57cec5SDimitry Andric   return true;
50990b57cec5SDimitry Andric }
51000b57cec5SDimitry Andric 
51010b57cec5SDimitry Andric /// Recursively walk all the uses of I until we find a memory use.
51020b57cec5SDimitry Andric /// If we find an obviously non-foldable instruction, return true.
5103349cc55cSDimitry Andric /// Add accessed addresses and types to MemoryUses.
FindAllMemoryUses(Instruction * I,SmallVectorImpl<std::pair<Use *,Type * >> & MemoryUses,SmallPtrSetImpl<Instruction * > & ConsideredInsts,const TargetLowering & TLI,const TargetRegisterInfo & TRI,bool OptSize,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI,unsigned & SeenInsts)51040b57cec5SDimitry Andric static bool FindAllMemoryUses(
5105fe013be4SDimitry Andric     Instruction *I, SmallVectorImpl<std::pair<Use *, Type *>> &MemoryUses,
51060b57cec5SDimitry Andric     SmallPtrSetImpl<Instruction *> &ConsideredInsts, const TargetLowering &TLI,
5107480093f4SDimitry Andric     const TargetRegisterInfo &TRI, bool OptSize, ProfileSummaryInfo *PSI,
5108fe013be4SDimitry Andric     BlockFrequencyInfo *BFI, unsigned &SeenInsts) {
51090b57cec5SDimitry Andric   // If we already considered this instruction, we're done.
51100b57cec5SDimitry Andric   if (!ConsideredInsts.insert(I).second)
51110b57cec5SDimitry Andric     return false;
51120b57cec5SDimitry Andric 
51130b57cec5SDimitry Andric   // If this is an obviously unfoldable instruction, bail out.
51140b57cec5SDimitry Andric   if (!MightBeFoldableInst(I))
51150b57cec5SDimitry Andric     return true;
51160b57cec5SDimitry Andric 
51170b57cec5SDimitry Andric   // Loop over all the uses, recursively processing them.
51180b57cec5SDimitry Andric   for (Use &U : I->uses()) {
51190b57cec5SDimitry Andric     // Conservatively return true if we're seeing a large number or a deep chain
51200b57cec5SDimitry Andric     // of users. This avoids excessive compilation times in pathological cases.
5121fe013be4SDimitry Andric     if (SeenInsts++ >= MaxAddressUsersToScan)
51220b57cec5SDimitry Andric       return true;
51230b57cec5SDimitry Andric 
51240b57cec5SDimitry Andric     Instruction *UserI = cast<Instruction>(U.getUser());
51250b57cec5SDimitry Andric     if (LoadInst *LI = dyn_cast<LoadInst>(UserI)) {
5126fe013be4SDimitry Andric       MemoryUses.push_back({&U, LI->getType()});
51270b57cec5SDimitry Andric       continue;
51280b57cec5SDimitry Andric     }
51290b57cec5SDimitry Andric 
51300b57cec5SDimitry Andric     if (StoreInst *SI = dyn_cast<StoreInst>(UserI)) {
5131349cc55cSDimitry Andric       if (U.getOperandNo() != StoreInst::getPointerOperandIndex())
51320b57cec5SDimitry Andric         return true; // Storing addr, not into addr.
5133fe013be4SDimitry Andric       MemoryUses.push_back({&U, SI->getValueOperand()->getType()});
51340b57cec5SDimitry Andric       continue;
51350b57cec5SDimitry Andric     }
51360b57cec5SDimitry Andric 
51370b57cec5SDimitry Andric     if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UserI)) {
5138349cc55cSDimitry Andric       if (U.getOperandNo() != AtomicRMWInst::getPointerOperandIndex())
51390b57cec5SDimitry Andric         return true; // Storing addr, not into addr.
5140fe013be4SDimitry Andric       MemoryUses.push_back({&U, RMW->getValOperand()->getType()});
51410b57cec5SDimitry Andric       continue;
51420b57cec5SDimitry Andric     }
51430b57cec5SDimitry Andric 
51440b57cec5SDimitry Andric     if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(UserI)) {
5145349cc55cSDimitry Andric       if (U.getOperandNo() != AtomicCmpXchgInst::getPointerOperandIndex())
51460b57cec5SDimitry Andric         return true; // Storing addr, not into addr.
5147fe013be4SDimitry Andric       MemoryUses.push_back({&U, CmpX->getCompareOperand()->getType()});
51480b57cec5SDimitry Andric       continue;
51490b57cec5SDimitry Andric     }
51500b57cec5SDimitry Andric 
51510b57cec5SDimitry Andric     if (CallInst *CI = dyn_cast<CallInst>(UserI)) {
51525ffd83dbSDimitry Andric       if (CI->hasFnAttr(Attribute::Cold)) {
51530b57cec5SDimitry Andric         // If this is a cold call, we can sink the addressing calculation into
51540b57cec5SDimitry Andric         // the cold path.  See optimizeCallInst
5155bdd1243dSDimitry Andric         bool OptForSize =
5156bdd1243dSDimitry Andric             OptSize || llvm::shouldOptimizeForSize(CI->getParent(), PSI, BFI);
51575ffd83dbSDimitry Andric         if (!OptForSize)
51580b57cec5SDimitry Andric           continue;
51595ffd83dbSDimitry Andric       }
51600b57cec5SDimitry Andric 
51615ffd83dbSDimitry Andric       InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledOperand());
5162bdd1243dSDimitry Andric       if (!IA)
5163bdd1243dSDimitry Andric         return true;
51640b57cec5SDimitry Andric 
51650b57cec5SDimitry Andric       // If this is a memory operand, we're cool, otherwise bail out.
51660b57cec5SDimitry Andric       if (!IsOperandAMemoryOperand(CI, IA, I, TLI, TRI))
51670b57cec5SDimitry Andric         return true;
51680b57cec5SDimitry Andric       continue;
51690b57cec5SDimitry Andric     }
51700b57cec5SDimitry Andric 
5171480093f4SDimitry Andric     if (FindAllMemoryUses(UserI, MemoryUses, ConsideredInsts, TLI, TRI, OptSize,
5172480093f4SDimitry Andric                           PSI, BFI, SeenInsts))
51730b57cec5SDimitry Andric       return true;
51740b57cec5SDimitry Andric   }
51750b57cec5SDimitry Andric 
51760b57cec5SDimitry Andric   return false;
51770b57cec5SDimitry Andric }
51780b57cec5SDimitry Andric 
FindAllMemoryUses(Instruction * I,SmallVectorImpl<std::pair<Use *,Type * >> & MemoryUses,const TargetLowering & TLI,const TargetRegisterInfo & TRI,bool OptSize,ProfileSummaryInfo * PSI,BlockFrequencyInfo * BFI)5179fe013be4SDimitry Andric static bool FindAllMemoryUses(
5180fe013be4SDimitry Andric     Instruction *I, SmallVectorImpl<std::pair<Use *, Type *>> &MemoryUses,
5181fe013be4SDimitry Andric     const TargetLowering &TLI, const TargetRegisterInfo &TRI, bool OptSize,
5182fe013be4SDimitry Andric     ProfileSummaryInfo *PSI, BlockFrequencyInfo *BFI) {
5183fe013be4SDimitry Andric   unsigned SeenInsts = 0;
5184fe013be4SDimitry Andric   SmallPtrSet<Instruction *, 16> ConsideredInsts;
5185fe013be4SDimitry Andric   return FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI, TRI, OptSize,
5186fe013be4SDimitry Andric                            PSI, BFI, SeenInsts);
5187fe013be4SDimitry Andric }
5188fe013be4SDimitry Andric 
5189fe013be4SDimitry Andric 
51900b57cec5SDimitry Andric /// Return true if Val is already known to be live at the use site that we're
51910b57cec5SDimitry Andric /// folding it into. If so, there is no cost to include it in the addressing
51920b57cec5SDimitry Andric /// mode. KnownLive1 and KnownLive2 are two values that we know are live at the
51930b57cec5SDimitry Andric /// instruction already.
valueAlreadyLiveAtInst(Value * Val,Value * KnownLive1,Value * KnownLive2)5194bdd1243dSDimitry Andric bool AddressingModeMatcher::valueAlreadyLiveAtInst(Value *Val,
5195bdd1243dSDimitry Andric                                                    Value *KnownLive1,
51960b57cec5SDimitry Andric                                                    Value *KnownLive2) {
51970b57cec5SDimitry Andric   // If Val is either of the known-live values, we know it is live!
51980b57cec5SDimitry Andric   if (Val == nullptr || Val == KnownLive1 || Val == KnownLive2)
51990b57cec5SDimitry Andric     return true;
52000b57cec5SDimitry Andric 
52010b57cec5SDimitry Andric   // All values other than instructions and arguments (e.g. constants) are live.
5202bdd1243dSDimitry Andric   if (!isa<Instruction>(Val) && !isa<Argument>(Val))
5203bdd1243dSDimitry Andric     return true;
52040b57cec5SDimitry Andric 
52050b57cec5SDimitry Andric   // If Val is a constant sized alloca in the entry block, it is live, this is
52060b57cec5SDimitry Andric   // true because it is just a reference to the stack/frame pointer, which is
52070b57cec5SDimitry Andric   // live for the whole function.
52080b57cec5SDimitry Andric   if (AllocaInst *AI = dyn_cast<AllocaInst>(Val))
52090b57cec5SDimitry Andric     if (AI->isStaticAlloca())
52100b57cec5SDimitry Andric       return true;
52110b57cec5SDimitry Andric 
52120b57cec5SDimitry Andric   // Check to see if this value is already used in the memory instruction's
52130b57cec5SDimitry Andric   // block.  If so, it's already live into the block at the very least, so we
52140b57cec5SDimitry Andric   // can reasonably fold it.
52150b57cec5SDimitry Andric   return Val->isUsedInBasicBlock(MemoryInst->getParent());
52160b57cec5SDimitry Andric }
52170b57cec5SDimitry Andric 
52180b57cec5SDimitry Andric /// It is possible for the addressing mode of the machine to fold the specified
52190b57cec5SDimitry Andric /// instruction into a load or store that ultimately uses it.
52200b57cec5SDimitry Andric /// However, the specified instruction has multiple uses.
52210b57cec5SDimitry Andric /// Given this, it may actually increase register pressure to fold it
52220b57cec5SDimitry Andric /// into the load. For example, consider this code:
52230b57cec5SDimitry Andric ///
52240b57cec5SDimitry Andric ///     X = ...
52250b57cec5SDimitry Andric ///     Y = X+1
52260b57cec5SDimitry Andric ///     use(Y)   -> nonload/store
52270b57cec5SDimitry Andric ///     Z = Y+1
52280b57cec5SDimitry Andric ///     load Z
52290b57cec5SDimitry Andric ///
52300b57cec5SDimitry Andric /// In this case, Y has multiple uses, and can be folded into the load of Z
52310b57cec5SDimitry Andric /// (yielding load [X+2]).  However, doing this will cause both "X" and "X+1" to
52320b57cec5SDimitry Andric /// be live at the use(Y) line.  If we don't fold Y into load Z, we use one
52330b57cec5SDimitry Andric /// fewer register.  Since Y can't be folded into "use(Y)" we don't increase the
52340b57cec5SDimitry Andric /// number of computations either.
52350b57cec5SDimitry Andric ///
52360b57cec5SDimitry Andric /// Note that this (like most of CodeGenPrepare) is just a rough heuristic.  If
52370b57cec5SDimitry Andric /// X was live across 'load Z' for other reasons, we actually *would* want to
52380b57cec5SDimitry Andric /// fold the addressing mode in the Z case.  This would make Y die earlier.
isProfitableToFoldIntoAddressingMode(Instruction * I,ExtAddrMode & AMBefore,ExtAddrMode & AMAfter)5239bdd1243dSDimitry Andric bool AddressingModeMatcher::isProfitableToFoldIntoAddressingMode(
5240bdd1243dSDimitry Andric     Instruction *I, ExtAddrMode &AMBefore, ExtAddrMode &AMAfter) {
5241bdd1243dSDimitry Andric   if (IgnoreProfitability)
5242bdd1243dSDimitry Andric     return true;
52430b57cec5SDimitry Andric 
52440b57cec5SDimitry Andric   // AMBefore is the addressing mode before this instruction was folded into it,
52450b57cec5SDimitry Andric   // and AMAfter is the addressing mode after the instruction was folded.  Get
52460b57cec5SDimitry Andric   // the set of registers referenced by AMAfter and subtract out those
52470b57cec5SDimitry Andric   // referenced by AMBefore: this is the set of values which folding in this
52480b57cec5SDimitry Andric   // address extends the lifetime of.
52490b57cec5SDimitry Andric   //
52500b57cec5SDimitry Andric   // Note that there are only two potential values being referenced here,
52510b57cec5SDimitry Andric   // BaseReg and ScaleReg (global addresses are always available, as are any
52520b57cec5SDimitry Andric   // folded immediates).
52530b57cec5SDimitry Andric   Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
52540b57cec5SDimitry Andric 
52550b57cec5SDimitry Andric   // If the BaseReg or ScaledReg was referenced by the previous addrmode, their
52560b57cec5SDimitry Andric   // lifetime wasn't extended by adding this instruction.
52570b57cec5SDimitry Andric   if (valueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
52580b57cec5SDimitry Andric     BaseReg = nullptr;
52590b57cec5SDimitry Andric   if (valueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
52600b57cec5SDimitry Andric     ScaledReg = nullptr;
52610b57cec5SDimitry Andric 
52620b57cec5SDimitry Andric   // If folding this instruction (and it's subexprs) didn't extend any live
52630b57cec5SDimitry Andric   // ranges, we're ok with it.
52640b57cec5SDimitry Andric   if (!BaseReg && !ScaledReg)
52650b57cec5SDimitry Andric     return true;
52660b57cec5SDimitry Andric 
52670b57cec5SDimitry Andric   // If all uses of this instruction can have the address mode sunk into them,
52680b57cec5SDimitry Andric   // we can remove the addressing mode and effectively trade one live register
52690b57cec5SDimitry Andric   // for another (at worst.)  In this context, folding an addressing mode into
52700b57cec5SDimitry Andric   // the use is just a particularly nice way of sinking it.
5271fe013be4SDimitry Andric   SmallVector<std::pair<Use *, Type *>, 16> MemoryUses;
5272fe013be4SDimitry Andric   if (FindAllMemoryUses(I, MemoryUses, TLI, TRI, OptSize, PSI, BFI))
52730b57cec5SDimitry Andric     return false; // Has a non-memory, non-foldable use!
52740b57cec5SDimitry Andric 
52750b57cec5SDimitry Andric   // Now that we know that all uses of this instruction are part of a chain of
52760b57cec5SDimitry Andric   // computation involving only operations that could theoretically be folded
52770b57cec5SDimitry Andric   // into a memory use, loop over each of these memory operation uses and see
52780b57cec5SDimitry Andric   // if they could  *actually* fold the instruction.  The assumption is that
52790b57cec5SDimitry Andric   // addressing modes are cheap and that duplicating the computation involved
52800b57cec5SDimitry Andric   // many times is worthwhile, even on a fastpath. For sinking candidates
52810b57cec5SDimitry Andric   // (i.e. cold call sites), this serves as a way to prevent excessive code
52820b57cec5SDimitry Andric   // growth since most architectures have some reasonable small and fast way to
52830b57cec5SDimitry Andric   // compute an effective address.  (i.e LEA on x86)
52840b57cec5SDimitry Andric   SmallVector<Instruction *, 32> MatchedAddrModeInsts;
5285fe013be4SDimitry Andric   for (const std::pair<Use *, Type *> &Pair : MemoryUses) {
5286fe013be4SDimitry Andric     Value *Address = Pair.first->get();
5287fe013be4SDimitry Andric     Instruction *UserI = cast<Instruction>(Pair.first->getUser());
5288349cc55cSDimitry Andric     Type *AddressAccessTy = Pair.second;
5289349cc55cSDimitry Andric     unsigned AS = Address->getType()->getPointerAddressSpace();
52900b57cec5SDimitry Andric 
52910b57cec5SDimitry Andric     // Do a match against the root of this address, ignoring profitability. This
52920b57cec5SDimitry Andric     // will tell us if the addressing mode for the memory operation will
52930b57cec5SDimitry Andric     // *actually* cover the shared instruction.
52940b57cec5SDimitry Andric     ExtAddrMode Result;
52950b57cec5SDimitry Andric     std::pair<AssertingVH<GetElementPtrInst>, int64_t> LargeOffsetGEP(nullptr,
52960b57cec5SDimitry Andric                                                                       0);
52970b57cec5SDimitry Andric     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
52980b57cec5SDimitry Andric         TPT.getRestorationPoint();
5299fe6060f1SDimitry Andric     AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, TRI, LI, getDTFn,
5300fe013be4SDimitry Andric                                   AddressAccessTy, AS, UserI, Result,
5301fe6060f1SDimitry Andric                                   InsertedInsts, PromotedInsts, TPT,
5302fe6060f1SDimitry Andric                                   LargeOffsetGEP, OptSize, PSI, BFI);
53030b57cec5SDimitry Andric     Matcher.IgnoreProfitability = true;
53040b57cec5SDimitry Andric     bool Success = Matcher.matchAddr(Address, 0);
5305bdd1243dSDimitry Andric     (void)Success;
5306bdd1243dSDimitry Andric     assert(Success && "Couldn't select *anything*?");
53070b57cec5SDimitry Andric 
53080b57cec5SDimitry Andric     // The match was to check the profitability, the changes made are not
53090b57cec5SDimitry Andric     // part of the original matcher. Therefore, they should be dropped
53100b57cec5SDimitry Andric     // otherwise the original matcher will not present the right state.
53110b57cec5SDimitry Andric     TPT.rollback(LastKnownGood);
53120b57cec5SDimitry Andric 
53130b57cec5SDimitry Andric     // If the match didn't cover I, then it won't be shared by it.
53140b57cec5SDimitry Andric     if (!is_contained(MatchedAddrModeInsts, I))
53150b57cec5SDimitry Andric       return false;
53160b57cec5SDimitry Andric 
53170b57cec5SDimitry Andric     MatchedAddrModeInsts.clear();
53180b57cec5SDimitry Andric   }
53190b57cec5SDimitry Andric 
53200b57cec5SDimitry Andric   return true;
53210b57cec5SDimitry Andric }
53220b57cec5SDimitry Andric 
53230b57cec5SDimitry Andric /// Return true if the specified values are defined in a
53240b57cec5SDimitry Andric /// different basic block than BB.
IsNonLocalValue(Value * V,BasicBlock * BB)53250b57cec5SDimitry Andric static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
53260b57cec5SDimitry Andric   if (Instruction *I = dyn_cast<Instruction>(V))
53270b57cec5SDimitry Andric     return I->getParent() != BB;
53280b57cec5SDimitry Andric   return false;
53290b57cec5SDimitry Andric }
53300b57cec5SDimitry Andric 
53310b57cec5SDimitry Andric /// Sink addressing mode computation immediate before MemoryInst if doing so
53320b57cec5SDimitry Andric /// can be done without increasing register pressure.  The need for the
53330b57cec5SDimitry Andric /// register pressure constraint means this can end up being an all or nothing
53340b57cec5SDimitry Andric /// decision for all uses of the same addressing computation.
53350b57cec5SDimitry Andric ///
53360b57cec5SDimitry Andric /// Load and Store Instructions often have addressing modes that can do
53370b57cec5SDimitry Andric /// significant amounts of computation. As such, instruction selection will try
53380b57cec5SDimitry Andric /// to get the load or store to do as much computation as possible for the
53390b57cec5SDimitry Andric /// program. The problem is that isel can only see within a single block. As
53400b57cec5SDimitry Andric /// such, we sink as much legal addressing mode work into the block as possible.
53410b57cec5SDimitry Andric ///
53420b57cec5SDimitry Andric /// This method is used to optimize both load/store and inline asms with memory
53430b57cec5SDimitry Andric /// operands.  It's also used to sink addressing computations feeding into cold
53440b57cec5SDimitry Andric /// call sites into their (cold) basic block.
53450b57cec5SDimitry Andric ///
53460b57cec5SDimitry Andric /// The motivation for handling sinking into cold blocks is that doing so can
53470b57cec5SDimitry Andric /// both enable other address mode sinking (by satisfying the register pressure
53480b57cec5SDimitry Andric /// constraint above), and reduce register pressure globally (by removing the
53490b57cec5SDimitry Andric /// addressing mode computation from the fast path entirely.).
optimizeMemoryInst(Instruction * MemoryInst,Value * Addr,Type * AccessTy,unsigned AddrSpace)53500b57cec5SDimitry Andric bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
53510b57cec5SDimitry Andric                                         Type *AccessTy, unsigned AddrSpace) {
53520b57cec5SDimitry Andric   Value *Repl = Addr;
53530b57cec5SDimitry Andric 
53540b57cec5SDimitry Andric   // Try to collapse single-value PHI nodes.  This is necessary to undo
53550b57cec5SDimitry Andric   // unprofitable PRE transformations.
53560b57cec5SDimitry Andric   SmallVector<Value *, 8> worklist;
53570b57cec5SDimitry Andric   SmallPtrSet<Value *, 16> Visited;
53580b57cec5SDimitry Andric   worklist.push_back(Addr);
53590b57cec5SDimitry Andric 
53600b57cec5SDimitry Andric   // Use a worklist to iteratively look through PHI and select nodes, and
53610b57cec5SDimitry Andric   // ensure that the addressing mode obtained from the non-PHI/select roots of
53620b57cec5SDimitry Andric   // the graph are compatible.
53630b57cec5SDimitry Andric   bool PhiOrSelectSeen = false;
53640b57cec5SDimitry Andric   SmallVector<Instruction *, 16> AddrModeInsts;
53650b57cec5SDimitry Andric   const SimplifyQuery SQ(*DL, TLInfo);
53660b57cec5SDimitry Andric   AddressingModeCombiner AddrModes(SQ, Addr);
53670b57cec5SDimitry Andric   TypePromotionTransaction TPT(RemovedInsts);
53680b57cec5SDimitry Andric   TypePromotionTransaction::ConstRestorationPt LastKnownGood =
53690b57cec5SDimitry Andric       TPT.getRestorationPoint();
53700b57cec5SDimitry Andric   while (!worklist.empty()) {
5371349cc55cSDimitry Andric     Value *V = worklist.pop_back_val();
53720b57cec5SDimitry Andric 
53730b57cec5SDimitry Andric     // We allow traversing cyclic Phi nodes.
53740b57cec5SDimitry Andric     // In case of success after this loop we ensure that traversing through
53750b57cec5SDimitry Andric     // Phi nodes ends up with all cases to compute address of the form
53760b57cec5SDimitry Andric     //    BaseGV + Base + Scale * Index + Offset
53770b57cec5SDimitry Andric     // where Scale and Offset are constans and BaseGV, Base and Index
53780b57cec5SDimitry Andric     // are exactly the same Values in all cases.
53790b57cec5SDimitry Andric     // It means that BaseGV, Scale and Offset dominate our memory instruction
53800b57cec5SDimitry Andric     // and have the same value as they had in address computation represented
53810b57cec5SDimitry Andric     // as Phi. So we can safely sink address computation to memory instruction.
53820b57cec5SDimitry Andric     if (!Visited.insert(V).second)
53830b57cec5SDimitry Andric       continue;
53840b57cec5SDimitry Andric 
53850b57cec5SDimitry Andric     // For a PHI node, push all of its incoming values.
53860b57cec5SDimitry Andric     if (PHINode *P = dyn_cast<PHINode>(V)) {
5387e8d8bef9SDimitry Andric       append_range(worklist, P->incoming_values());
53880b57cec5SDimitry Andric       PhiOrSelectSeen = true;
53890b57cec5SDimitry Andric       continue;
53900b57cec5SDimitry Andric     }
53910b57cec5SDimitry Andric     // Similar for select.
53920b57cec5SDimitry Andric     if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
53930b57cec5SDimitry Andric       worklist.push_back(SI->getFalseValue());
53940b57cec5SDimitry Andric       worklist.push_back(SI->getTrueValue());
53950b57cec5SDimitry Andric       PhiOrSelectSeen = true;
53960b57cec5SDimitry Andric       continue;
53970b57cec5SDimitry Andric     }
53980b57cec5SDimitry Andric 
53990b57cec5SDimitry Andric     // For non-PHIs, determine the addressing mode being computed.  Note that
54000b57cec5SDimitry Andric     // the result may differ depending on what other uses our candidate
54010b57cec5SDimitry Andric     // addressing instructions might have.
54020b57cec5SDimitry Andric     AddrModeInsts.clear();
54030b57cec5SDimitry Andric     std::pair<AssertingVH<GetElementPtrInst>, int64_t> LargeOffsetGEP(nullptr,
54040b57cec5SDimitry Andric                                                                       0);
5405fe6060f1SDimitry Andric     // Defer the query (and possible computation of) the dom tree to point of
5406fe6060f1SDimitry Andric     // actual use.  It's expected that most address matches don't actually need
5407fe6060f1SDimitry Andric     // the domtree.
5408fe6060f1SDimitry Andric     auto getDTFn = [MemoryInst, this]() -> const DominatorTree & {
5409fe6060f1SDimitry Andric       Function *F = MemoryInst->getParent()->getParent();
5410fe6060f1SDimitry Andric       return this->getDT(*F);
5411fe6060f1SDimitry Andric     };
54120b57cec5SDimitry Andric     ExtAddrMode NewAddrMode = AddressingModeMatcher::Match(
5413fe6060f1SDimitry Andric         V, AccessTy, AddrSpace, MemoryInst, AddrModeInsts, *TLI, *LI, getDTFn,
5414fe6060f1SDimitry Andric         *TRI, InsertedInsts, PromotedInsts, TPT, LargeOffsetGEP, OptSize, PSI,
5415480093f4SDimitry Andric         BFI.get());
54160b57cec5SDimitry Andric 
54170b57cec5SDimitry Andric     GetElementPtrInst *GEP = LargeOffsetGEP.first;
54180b57cec5SDimitry Andric     if (GEP && !NewGEPBases.count(GEP)) {
54190b57cec5SDimitry Andric       // If splitting the underlying data structure can reduce the offset of a
54200b57cec5SDimitry Andric       // GEP, collect the GEP.  Skip the GEPs that are the new bases of
54210b57cec5SDimitry Andric       // previously split data structures.
54220b57cec5SDimitry Andric       LargeOffsetGEPMap[GEP->getPointerOperand()].push_back(LargeOffsetGEP);
542381ad6265SDimitry Andric       LargeOffsetGEPID.insert(std::make_pair(GEP, LargeOffsetGEPID.size()));
54240b57cec5SDimitry Andric     }
54250b57cec5SDimitry Andric 
54260b57cec5SDimitry Andric     NewAddrMode.OriginalValue = V;
54270b57cec5SDimitry Andric     if (!AddrModes.addNewAddrMode(NewAddrMode))
54280b57cec5SDimitry Andric       break;
54290b57cec5SDimitry Andric   }
54300b57cec5SDimitry Andric 
54310b57cec5SDimitry Andric   // Try to combine the AddrModes we've collected. If we couldn't collect any,
54320b57cec5SDimitry Andric   // or we have multiple but either couldn't combine them or combining them
54330b57cec5SDimitry Andric   // wouldn't do anything useful, bail out now.
54340b57cec5SDimitry Andric   if (!AddrModes.combineAddrModes()) {
54350b57cec5SDimitry Andric     TPT.rollback(LastKnownGood);
54360b57cec5SDimitry Andric     return false;
54370b57cec5SDimitry Andric   }
54385ffd83dbSDimitry Andric   bool Modified = TPT.commit();
54390b57cec5SDimitry Andric 
54400b57cec5SDimitry Andric   // Get the combined AddrMode (or the only AddrMode, if we only had one).
54410b57cec5SDimitry Andric   ExtAddrMode AddrMode = AddrModes.getAddrMode();
54420b57cec5SDimitry Andric 
54430b57cec5SDimitry Andric   // If all the instructions matched are already in this BB, don't do anything.
5444bdd1243dSDimitry Andric   // If we saw a Phi node then it is not local definitely, and if we saw a
5445bdd1243dSDimitry Andric   // select then we want to push the address calculation past it even if it's
5446bdd1243dSDimitry Andric   // already in this BB.
54470b57cec5SDimitry Andric   if (!PhiOrSelectSeen && none_of(AddrModeInsts, [&](Value *V) {
54480b57cec5SDimitry Andric         return IsNonLocalValue(V, MemoryInst->getParent());
54490b57cec5SDimitry Andric       })) {
54500b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "CGP: Found      local addrmode: " << AddrMode
54510b57cec5SDimitry Andric                       << "\n");
54525ffd83dbSDimitry Andric     return Modified;
54530b57cec5SDimitry Andric   }
54540b57cec5SDimitry Andric 
54550b57cec5SDimitry Andric   // Insert this computation right after this user.  Since our caller is
54560b57cec5SDimitry Andric   // scanning from the top of the BB to the bottom, reuse of the expr are
54570b57cec5SDimitry Andric   // guaranteed to happen later.
54580b57cec5SDimitry Andric   IRBuilder<> Builder(MemoryInst);
54590b57cec5SDimitry Andric 
54600b57cec5SDimitry Andric   // Now that we determined the addressing expression we want to use and know
54610b57cec5SDimitry Andric   // that we have to sink it into this block.  Check to see if we have already
54620b57cec5SDimitry Andric   // done this for some other load/store instr in this block.  If so, reuse
54630b57cec5SDimitry Andric   // the computation.  Before attempting reuse, check if the address is valid
54640b57cec5SDimitry Andric   // as it may have been erased.
54650b57cec5SDimitry Andric 
54660b57cec5SDimitry Andric   WeakTrackingVH SunkAddrVH = SunkAddrs[Addr];
54670b57cec5SDimitry Andric 
54680b57cec5SDimitry Andric   Value *SunkAddr = SunkAddrVH.pointsToAliveValue() ? SunkAddrVH : nullptr;
5469fcaf7f86SDimitry Andric   Type *IntPtrTy = DL->getIntPtrType(Addr->getType());
54700b57cec5SDimitry Andric   if (SunkAddr) {
54710b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode
54720b57cec5SDimitry Andric                       << " for " << *MemoryInst << "\n");
5473fcaf7f86SDimitry Andric     if (SunkAddr->getType() != Addr->getType()) {
5474fcaf7f86SDimitry Andric       if (SunkAddr->getType()->getPointerAddressSpace() !=
5475fcaf7f86SDimitry Andric               Addr->getType()->getPointerAddressSpace() &&
5476fcaf7f86SDimitry Andric           !DL->isNonIntegralPointerType(Addr->getType())) {
5477fcaf7f86SDimitry Andric         // There are two reasons the address spaces might not match: a no-op
5478fcaf7f86SDimitry Andric         // addrspacecast, or a ptrtoint/inttoptr pair. Either way, we emit a
5479fcaf7f86SDimitry Andric         // ptrtoint/inttoptr pair to ensure we match the original semantics.
5480fcaf7f86SDimitry Andric         // TODO: allow bitcast between different address space pointers with the
5481fcaf7f86SDimitry Andric         // same size.
5482fcaf7f86SDimitry Andric         SunkAddr = Builder.CreatePtrToInt(SunkAddr, IntPtrTy, "sunkaddr");
5483fcaf7f86SDimitry Andric         SunkAddr =
5484fcaf7f86SDimitry Andric             Builder.CreateIntToPtr(SunkAddr, Addr->getType(), "sunkaddr");
5485fcaf7f86SDimitry Andric       } else
54860b57cec5SDimitry Andric         SunkAddr = Builder.CreatePointerCast(SunkAddr, Addr->getType());
5487fcaf7f86SDimitry Andric     }
54888bcb0991SDimitry Andric   } else if (AddrSinkUsingGEPs || (!AddrSinkUsingGEPs.getNumOccurrences() &&
54895ffd83dbSDimitry Andric                                    SubtargetInfo->addrSinkUsingGEPs())) {
54900b57cec5SDimitry Andric     // By default, we use the GEP-based method when AA is used later. This
54910b57cec5SDimitry Andric     // prevents new inttoptr/ptrtoint pairs from degrading AA capabilities.
54920b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode
54930b57cec5SDimitry Andric                       << " for " << *MemoryInst << "\n");
54940b57cec5SDimitry Andric     Value *ResultPtr = nullptr, *ResultIndex = nullptr;
54950b57cec5SDimitry Andric 
54960b57cec5SDimitry Andric     // First, find the pointer.
54970b57cec5SDimitry Andric     if (AddrMode.BaseReg && AddrMode.BaseReg->getType()->isPointerTy()) {
54980b57cec5SDimitry Andric       ResultPtr = AddrMode.BaseReg;
54990b57cec5SDimitry Andric       AddrMode.BaseReg = nullptr;
55000b57cec5SDimitry Andric     }
55010b57cec5SDimitry Andric 
55020b57cec5SDimitry Andric     if (AddrMode.Scale && AddrMode.ScaledReg->getType()->isPointerTy()) {
55030b57cec5SDimitry Andric       // We can't add more than one pointer together, nor can we scale a
55040b57cec5SDimitry Andric       // pointer (both of which seem meaningless).
55050b57cec5SDimitry Andric       if (ResultPtr || AddrMode.Scale != 1)
55065ffd83dbSDimitry Andric         return Modified;
55070b57cec5SDimitry Andric 
55080b57cec5SDimitry Andric       ResultPtr = AddrMode.ScaledReg;
55090b57cec5SDimitry Andric       AddrMode.Scale = 0;
55100b57cec5SDimitry Andric     }
55110b57cec5SDimitry Andric 
55120b57cec5SDimitry Andric     // It is only safe to sign extend the BaseReg if we know that the math
55130b57cec5SDimitry Andric     // required to create it did not overflow before we extend it. Since
55140b57cec5SDimitry Andric     // the original IR value was tossed in favor of a constant back when
55150b57cec5SDimitry Andric     // the AddrMode was created we need to bail out gracefully if widths
55160b57cec5SDimitry Andric     // do not match instead of extending it.
55170b57cec5SDimitry Andric     //
55180b57cec5SDimitry Andric     // (See below for code to add the scale.)
55190b57cec5SDimitry Andric     if (AddrMode.Scale) {
55200b57cec5SDimitry Andric       Type *ScaledRegTy = AddrMode.ScaledReg->getType();
55210b57cec5SDimitry Andric       if (cast<IntegerType>(IntPtrTy)->getBitWidth() >
55220b57cec5SDimitry Andric           cast<IntegerType>(ScaledRegTy)->getBitWidth())
55235ffd83dbSDimitry Andric         return Modified;
55240b57cec5SDimitry Andric     }
55250b57cec5SDimitry Andric 
55260b57cec5SDimitry Andric     if (AddrMode.BaseGV) {
55270b57cec5SDimitry Andric       if (ResultPtr)
55285ffd83dbSDimitry Andric         return Modified;
55290b57cec5SDimitry Andric 
55300b57cec5SDimitry Andric       ResultPtr = AddrMode.BaseGV;
55310b57cec5SDimitry Andric     }
55320b57cec5SDimitry Andric 
55330b57cec5SDimitry Andric     // If the real base value actually came from an inttoptr, then the matcher
55340b57cec5SDimitry Andric     // will look through it and provide only the integer value. In that case,
55350b57cec5SDimitry Andric     // use it here.
55360b57cec5SDimitry Andric     if (!DL->isNonIntegralPointerType(Addr->getType())) {
55370b57cec5SDimitry Andric       if (!ResultPtr && AddrMode.BaseReg) {
55380b57cec5SDimitry Andric         ResultPtr = Builder.CreateIntToPtr(AddrMode.BaseReg, Addr->getType(),
55390b57cec5SDimitry Andric                                            "sunkaddr");
55400b57cec5SDimitry Andric         AddrMode.BaseReg = nullptr;
55410b57cec5SDimitry Andric       } else if (!ResultPtr && AddrMode.Scale == 1) {
55420b57cec5SDimitry Andric         ResultPtr = Builder.CreateIntToPtr(AddrMode.ScaledReg, Addr->getType(),
55430b57cec5SDimitry Andric                                            "sunkaddr");
55440b57cec5SDimitry Andric         AddrMode.Scale = 0;
55450b57cec5SDimitry Andric       }
55460b57cec5SDimitry Andric     }
55470b57cec5SDimitry Andric 
5548bdd1243dSDimitry Andric     if (!ResultPtr && !AddrMode.BaseReg && !AddrMode.Scale &&
5549bdd1243dSDimitry Andric         !AddrMode.BaseOffs) {
55500b57cec5SDimitry Andric       SunkAddr = Constant::getNullValue(Addr->getType());
55510b57cec5SDimitry Andric     } else if (!ResultPtr) {
55525ffd83dbSDimitry Andric       return Modified;
55530b57cec5SDimitry Andric     } else {
55540b57cec5SDimitry Andric       Type *I8PtrTy =
5555c9157d92SDimitry Andric           Builder.getPtrTy(Addr->getType()->getPointerAddressSpace());
55560b57cec5SDimitry Andric 
55570b57cec5SDimitry Andric       // Start with the base register. Do this first so that subsequent address
55580b57cec5SDimitry Andric       // matching finds it last, which will prevent it from trying to match it
55590b57cec5SDimitry Andric       // as the scaled value in case it happens to be a mul. That would be
55600b57cec5SDimitry Andric       // problematic if we've sunk a different mul for the scale, because then
55610b57cec5SDimitry Andric       // we'd end up sinking both muls.
55620b57cec5SDimitry Andric       if (AddrMode.BaseReg) {
55630b57cec5SDimitry Andric         Value *V = AddrMode.BaseReg;
55640b57cec5SDimitry Andric         if (V->getType() != IntPtrTy)
55650b57cec5SDimitry Andric           V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
55660b57cec5SDimitry Andric 
55670b57cec5SDimitry Andric         ResultIndex = V;
55680b57cec5SDimitry Andric       }
55690b57cec5SDimitry Andric 
55700b57cec5SDimitry Andric       // Add the scale value.
55710b57cec5SDimitry Andric       if (AddrMode.Scale) {
55720b57cec5SDimitry Andric         Value *V = AddrMode.ScaledReg;
55730b57cec5SDimitry Andric         if (V->getType() == IntPtrTy) {
55740b57cec5SDimitry Andric           // done.
55750b57cec5SDimitry Andric         } else {
55760b57cec5SDimitry Andric           assert(cast<IntegerType>(IntPtrTy)->getBitWidth() <
55770b57cec5SDimitry Andric                      cast<IntegerType>(V->getType())->getBitWidth() &&
55780b57cec5SDimitry Andric                  "We can't transform if ScaledReg is too narrow");
55790b57cec5SDimitry Andric           V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
55800b57cec5SDimitry Andric         }
55810b57cec5SDimitry Andric 
55820b57cec5SDimitry Andric         if (AddrMode.Scale != 1)
55830b57cec5SDimitry Andric           V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
55840b57cec5SDimitry Andric                                 "sunkaddr");
55850b57cec5SDimitry Andric         if (ResultIndex)
55860b57cec5SDimitry Andric           ResultIndex = Builder.CreateAdd(ResultIndex, V, "sunkaddr");
55870b57cec5SDimitry Andric         else
55880b57cec5SDimitry Andric           ResultIndex = V;
55890b57cec5SDimitry Andric       }
55900b57cec5SDimitry Andric 
55910b57cec5SDimitry Andric       // Add in the Base Offset if present.
55920b57cec5SDimitry Andric       if (AddrMode.BaseOffs) {
55930b57cec5SDimitry Andric         Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
55940b57cec5SDimitry Andric         if (ResultIndex) {
55950b57cec5SDimitry Andric           // We need to add this separately from the scale above to help with
55960b57cec5SDimitry Andric           // SDAG consecutive load/store merging.
55970b57cec5SDimitry Andric           if (ResultPtr->getType() != I8PtrTy)
55980b57cec5SDimitry Andric             ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
5599a58f00eaSDimitry Andric           ResultPtr = Builder.CreatePtrAdd(ResultPtr, ResultIndex, "sunkaddr",
5600a58f00eaSDimitry Andric                                            AddrMode.InBounds);
56010b57cec5SDimitry Andric         }
56020b57cec5SDimitry Andric 
56030b57cec5SDimitry Andric         ResultIndex = V;
56040b57cec5SDimitry Andric       }
56050b57cec5SDimitry Andric 
56060b57cec5SDimitry Andric       if (!ResultIndex) {
56070b57cec5SDimitry Andric         SunkAddr = ResultPtr;
56080b57cec5SDimitry Andric       } else {
56090b57cec5SDimitry Andric         if (ResultPtr->getType() != I8PtrTy)
56100b57cec5SDimitry Andric           ResultPtr = Builder.CreatePointerCast(ResultPtr, I8PtrTy);
5611a58f00eaSDimitry Andric         SunkAddr = Builder.CreatePtrAdd(ResultPtr, ResultIndex, "sunkaddr",
561281ad6265SDimitry Andric                                         AddrMode.InBounds);
56130b57cec5SDimitry Andric       }
56140b57cec5SDimitry Andric 
5615fcaf7f86SDimitry Andric       if (SunkAddr->getType() != Addr->getType()) {
5616fcaf7f86SDimitry Andric         if (SunkAddr->getType()->getPointerAddressSpace() !=
5617fcaf7f86SDimitry Andric                 Addr->getType()->getPointerAddressSpace() &&
5618fcaf7f86SDimitry Andric             !DL->isNonIntegralPointerType(Addr->getType())) {
5619fcaf7f86SDimitry Andric           // There are two reasons the address spaces might not match: a no-op
5620fcaf7f86SDimitry Andric           // addrspacecast, or a ptrtoint/inttoptr pair. Either way, we emit a
5621fcaf7f86SDimitry Andric           // ptrtoint/inttoptr pair to ensure we match the original semantics.
5622fcaf7f86SDimitry Andric           // TODO: allow bitcast between different address space pointers with
5623fcaf7f86SDimitry Andric           // the same size.
5624fcaf7f86SDimitry Andric           SunkAddr = Builder.CreatePtrToInt(SunkAddr, IntPtrTy, "sunkaddr");
5625fcaf7f86SDimitry Andric           SunkAddr =
5626fcaf7f86SDimitry Andric               Builder.CreateIntToPtr(SunkAddr, Addr->getType(), "sunkaddr");
5627fcaf7f86SDimitry Andric         } else
56280b57cec5SDimitry Andric           SunkAddr = Builder.CreatePointerCast(SunkAddr, Addr->getType());
56290b57cec5SDimitry Andric       }
5630fcaf7f86SDimitry Andric     }
56310b57cec5SDimitry Andric   } else {
56320b57cec5SDimitry Andric     // We'd require a ptrtoint/inttoptr down the line, which we can't do for
56330b57cec5SDimitry Andric     // non-integral pointers, so in that case bail out now.
56340b57cec5SDimitry Andric     Type *BaseTy = AddrMode.BaseReg ? AddrMode.BaseReg->getType() : nullptr;
56350b57cec5SDimitry Andric     Type *ScaleTy = AddrMode.Scale ? AddrMode.ScaledReg->getType() : nullptr;
56360b57cec5SDimitry Andric     PointerType *BasePtrTy = dyn_cast_or_null<PointerType>(BaseTy);
56370b57cec5SDimitry Andric     PointerType *ScalePtrTy = dyn_cast_or_null<PointerType>(ScaleTy);
56380b57cec5SDimitry Andric     if (DL->isNonIntegralPointerType(Addr->getType()) ||
56390b57cec5SDimitry Andric         (BasePtrTy && DL->isNonIntegralPointerType(BasePtrTy)) ||
56400b57cec5SDimitry Andric         (ScalePtrTy && DL->isNonIntegralPointerType(ScalePtrTy)) ||
56410b57cec5SDimitry Andric         (AddrMode.BaseGV &&
56420b57cec5SDimitry Andric          DL->isNonIntegralPointerType(AddrMode.BaseGV->getType())))
56435ffd83dbSDimitry Andric       return Modified;
56440b57cec5SDimitry Andric 
56450b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode
56460b57cec5SDimitry Andric                       << " for " << *MemoryInst << "\n");
56470b57cec5SDimitry Andric     Type *IntPtrTy = DL->getIntPtrType(Addr->getType());
56480b57cec5SDimitry Andric     Value *Result = nullptr;
56490b57cec5SDimitry Andric 
56500b57cec5SDimitry Andric     // Start with the base register. Do this first so that subsequent address
56510b57cec5SDimitry Andric     // matching finds it last, which will prevent it from trying to match it
56520b57cec5SDimitry Andric     // as the scaled value in case it happens to be a mul. That would be
56530b57cec5SDimitry Andric     // problematic if we've sunk a different mul for the scale, because then
56540b57cec5SDimitry Andric     // we'd end up sinking both muls.
56550b57cec5SDimitry Andric     if (AddrMode.BaseReg) {
56560b57cec5SDimitry Andric       Value *V = AddrMode.BaseReg;
56570b57cec5SDimitry Andric       if (V->getType()->isPointerTy())
56580b57cec5SDimitry Andric         V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
56590b57cec5SDimitry Andric       if (V->getType() != IntPtrTy)
56600b57cec5SDimitry Andric         V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
56610b57cec5SDimitry Andric       Result = V;
56620b57cec5SDimitry Andric     }
56630b57cec5SDimitry Andric 
56640b57cec5SDimitry Andric     // Add the scale value.
56650b57cec5SDimitry Andric     if (AddrMode.Scale) {
56660b57cec5SDimitry Andric       Value *V = AddrMode.ScaledReg;
56670b57cec5SDimitry Andric       if (V->getType() == IntPtrTy) {
56680b57cec5SDimitry Andric         // done.
56690b57cec5SDimitry Andric       } else if (V->getType()->isPointerTy()) {
56700b57cec5SDimitry Andric         V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
56710b57cec5SDimitry Andric       } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
56720b57cec5SDimitry Andric                  cast<IntegerType>(V->getType())->getBitWidth()) {
56730b57cec5SDimitry Andric         V = Builder.CreateTrunc(V, IntPtrTy, "sunkaddr");
56740b57cec5SDimitry Andric       } else {
56750b57cec5SDimitry Andric         // It is only safe to sign extend the BaseReg if we know that the math
56760b57cec5SDimitry Andric         // required to create it did not overflow before we extend it. Since
56770b57cec5SDimitry Andric         // the original IR value was tossed in favor of a constant back when
56780b57cec5SDimitry Andric         // the AddrMode was created we need to bail out gracefully if widths
56790b57cec5SDimitry Andric         // do not match instead of extending it.
56800b57cec5SDimitry Andric         Instruction *I = dyn_cast_or_null<Instruction>(Result);
56810b57cec5SDimitry Andric         if (I && (Result != AddrMode.BaseReg))
56820b57cec5SDimitry Andric           I->eraseFromParent();
56835ffd83dbSDimitry Andric         return Modified;
56840b57cec5SDimitry Andric       }
56850b57cec5SDimitry Andric       if (AddrMode.Scale != 1)
56860b57cec5SDimitry Andric         V = Builder.CreateMul(V, ConstantInt::get(IntPtrTy, AddrMode.Scale),
56870b57cec5SDimitry Andric                               "sunkaddr");
56880b57cec5SDimitry Andric       if (Result)
56890b57cec5SDimitry Andric         Result = Builder.CreateAdd(Result, V, "sunkaddr");
56900b57cec5SDimitry Andric       else
56910b57cec5SDimitry Andric         Result = V;
56920b57cec5SDimitry Andric     }
56930b57cec5SDimitry Andric 
56940b57cec5SDimitry Andric     // Add in the BaseGV if present.
56950b57cec5SDimitry Andric     if (AddrMode.BaseGV) {
56960b57cec5SDimitry Andric       Value *V = Builder.CreatePtrToInt(AddrMode.BaseGV, IntPtrTy, "sunkaddr");
56970b57cec5SDimitry Andric       if (Result)
56980b57cec5SDimitry Andric         Result = Builder.CreateAdd(Result, V, "sunkaddr");
56990b57cec5SDimitry Andric       else
57000b57cec5SDimitry Andric         Result = V;
57010b57cec5SDimitry Andric     }
57020b57cec5SDimitry Andric 
57030b57cec5SDimitry Andric     // Add in the Base Offset if present.
57040b57cec5SDimitry Andric     if (AddrMode.BaseOffs) {
57050b57cec5SDimitry Andric       Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
57060b57cec5SDimitry Andric       if (Result)
57070b57cec5SDimitry Andric         Result = Builder.CreateAdd(Result, V, "sunkaddr");
57080b57cec5SDimitry Andric       else
57090b57cec5SDimitry Andric         Result = V;
57100b57cec5SDimitry Andric     }
57110b57cec5SDimitry Andric 
57120b57cec5SDimitry Andric     if (!Result)
57130b57cec5SDimitry Andric       SunkAddr = Constant::getNullValue(Addr->getType());
57140b57cec5SDimitry Andric     else
57150b57cec5SDimitry Andric       SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr");
57160b57cec5SDimitry Andric   }
57170b57cec5SDimitry Andric 
57180b57cec5SDimitry Andric   MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
57190b57cec5SDimitry Andric   // Store the newly computed address into the cache. In the case we reused a
57200b57cec5SDimitry Andric   // value, this should be idempotent.
57210b57cec5SDimitry Andric   SunkAddrs[Addr] = WeakTrackingVH(SunkAddr);
57220b57cec5SDimitry Andric 
57230b57cec5SDimitry Andric   // If we have no uses, recursively delete the value and all dead instructions
57240b57cec5SDimitry Andric   // using it.
57250b57cec5SDimitry Andric   if (Repl->use_empty()) {
5726e8d8bef9SDimitry Andric     resetIteratorIfInvalidatedWhileCalling(CurInstIterator->getParent(), [&]() {
5727e8d8bef9SDimitry Andric       RecursivelyDeleteTriviallyDeadInstructions(
5728e8d8bef9SDimitry Andric           Repl, TLInfo, nullptr,
5729e8d8bef9SDimitry Andric           [&](Value *V) { removeAllAssertingVHReferences(V); });
5730e8d8bef9SDimitry Andric     });
57310b57cec5SDimitry Andric   }
57320b57cec5SDimitry Andric   ++NumMemoryInsts;
57330b57cec5SDimitry Andric   return true;
57340b57cec5SDimitry Andric }
57350b57cec5SDimitry Andric 
57365ffd83dbSDimitry Andric /// Rewrite GEP input to gather/scatter to enable SelectionDAGBuilder to find
57375ffd83dbSDimitry Andric /// a uniform base to use for ISD::MGATHER/MSCATTER. SelectionDAGBuilder can
57385ffd83dbSDimitry Andric /// only handle a 2 operand GEP in the same basic block or a splat constant
57395ffd83dbSDimitry Andric /// vector. The 2 operands to the GEP must have a scalar pointer and a vector
57405ffd83dbSDimitry Andric /// index.
57415ffd83dbSDimitry Andric ///
57425ffd83dbSDimitry Andric /// If the existing GEP has a vector base pointer that is splat, we can look
57435ffd83dbSDimitry Andric /// through the splat to find the scalar pointer. If we can't find a scalar
57445ffd83dbSDimitry Andric /// pointer there's nothing we can do.
57455ffd83dbSDimitry Andric ///
57465ffd83dbSDimitry Andric /// If we have a GEP with more than 2 indices where the middle indices are all
57475ffd83dbSDimitry Andric /// zeroes, we can replace it with 2 GEPs where the second has 2 operands.
57485ffd83dbSDimitry Andric ///
57495ffd83dbSDimitry Andric /// If the final index isn't a vector or is a splat, we can emit a scalar GEP
57505ffd83dbSDimitry Andric /// followed by a GEP with an all zeroes vector index. This will enable
5751e8d8bef9SDimitry Andric /// SelectionDAGBuilder to use the scalar GEP as the uniform base and have a
57525ffd83dbSDimitry Andric /// zero index.
optimizeGatherScatterInst(Instruction * MemoryInst,Value * Ptr)57535ffd83dbSDimitry Andric bool CodeGenPrepare::optimizeGatherScatterInst(Instruction *MemoryInst,
57545ffd83dbSDimitry Andric                                                Value *Ptr) {
5755e8d8bef9SDimitry Andric   Value *NewAddr;
5756e8d8bef9SDimitry Andric 
5757e8d8bef9SDimitry Andric   if (const auto *GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
5758e8d8bef9SDimitry Andric     // Don't optimize GEPs that don't have indices.
5759e8d8bef9SDimitry Andric     if (!GEP->hasIndices())
57605ffd83dbSDimitry Andric       return false;
57615ffd83dbSDimitry Andric 
57625ffd83dbSDimitry Andric     // If the GEP and the gather/scatter aren't in the same BB, don't optimize.
57635ffd83dbSDimitry Andric     // FIXME: We should support this by sinking the GEP.
57645ffd83dbSDimitry Andric     if (MemoryInst->getParent() != GEP->getParent())
57655ffd83dbSDimitry Andric       return false;
57665ffd83dbSDimitry Andric 
5767e8d8bef9SDimitry Andric     SmallVector<Value *, 2> Ops(GEP->operands());
57685ffd83dbSDimitry Andric 
57695ffd83dbSDimitry Andric     bool RewriteGEP = false;
57705ffd83dbSDimitry Andric 
57715ffd83dbSDimitry Andric     if (Ops[0]->getType()->isVectorTy()) {
5772e8d8bef9SDimitry Andric       Ops[0] = getSplatValue(Ops[0]);
57735ffd83dbSDimitry Andric       if (!Ops[0])
57745ffd83dbSDimitry Andric         return false;
57755ffd83dbSDimitry Andric       RewriteGEP = true;
57765ffd83dbSDimitry Andric     }
57775ffd83dbSDimitry Andric 
57785ffd83dbSDimitry Andric     unsigned FinalIndex = Ops.size() - 1;
57795ffd83dbSDimitry Andric 
57805ffd83dbSDimitry Andric     // Ensure all but the last index is 0.
57815ffd83dbSDimitry Andric     // FIXME: This isn't strictly required. All that's required is that they are
57825ffd83dbSDimitry Andric     // all scalars or splats.
57835ffd83dbSDimitry Andric     for (unsigned i = 1; i < FinalIndex; ++i) {
57845ffd83dbSDimitry Andric       auto *C = dyn_cast<Constant>(Ops[i]);
57855ffd83dbSDimitry Andric       if (!C)
57865ffd83dbSDimitry Andric         return false;
57875ffd83dbSDimitry Andric       if (isa<VectorType>(C->getType()))
57885ffd83dbSDimitry Andric         C = C->getSplatValue();
57895ffd83dbSDimitry Andric       auto *CI = dyn_cast_or_null<ConstantInt>(C);
57905ffd83dbSDimitry Andric       if (!CI || !CI->isZero())
57915ffd83dbSDimitry Andric         return false;
57925ffd83dbSDimitry Andric       // Scalarize the index if needed.
57935ffd83dbSDimitry Andric       Ops[i] = CI;
57945ffd83dbSDimitry Andric     }
57955ffd83dbSDimitry Andric 
57965ffd83dbSDimitry Andric     // Try to scalarize the final index.
57975ffd83dbSDimitry Andric     if (Ops[FinalIndex]->getType()->isVectorTy()) {
5798e8d8bef9SDimitry Andric       if (Value *V = getSplatValue(Ops[FinalIndex])) {
57995ffd83dbSDimitry Andric         auto *C = dyn_cast<ConstantInt>(V);
58005ffd83dbSDimitry Andric         // Don't scalarize all zeros vector.
58015ffd83dbSDimitry Andric         if (!C || !C->isZero()) {
58025ffd83dbSDimitry Andric           Ops[FinalIndex] = V;
58035ffd83dbSDimitry Andric           RewriteGEP = true;
58045ffd83dbSDimitry Andric         }
58055ffd83dbSDimitry Andric       }
58065ffd83dbSDimitry Andric     }
58075ffd83dbSDimitry Andric 
58085ffd83dbSDimitry Andric     // If we made any changes or the we have extra operands, we need to generate
58095ffd83dbSDimitry Andric     // new instructions.
58105ffd83dbSDimitry Andric     if (!RewriteGEP && Ops.size() == 2)
58115ffd83dbSDimitry Andric       return false;
58125ffd83dbSDimitry Andric 
5813e8d8bef9SDimitry Andric     auto NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
58145ffd83dbSDimitry Andric 
58155ffd83dbSDimitry Andric     IRBuilder<> Builder(MemoryInst);
58165ffd83dbSDimitry Andric 
5817fe6060f1SDimitry Andric     Type *SourceTy = GEP->getSourceElementType();
58185ffd83dbSDimitry Andric     Type *ScalarIndexTy = DL->getIndexType(Ops[0]->getType()->getScalarType());
58195ffd83dbSDimitry Andric 
58205ffd83dbSDimitry Andric     // If the final index isn't a vector, emit a scalar GEP containing all ops
58215ffd83dbSDimitry Andric     // and a vector GEP with all zeroes final index.
58225ffd83dbSDimitry Andric     if (!Ops[FinalIndex]->getType()->isVectorTy()) {
5823bdd1243dSDimitry Andric       NewAddr = Builder.CreateGEP(SourceTy, Ops[0], ArrayRef(Ops).drop_front());
5824e8d8bef9SDimitry Andric       auto *IndexTy = VectorType::get(ScalarIndexTy, NumElts);
5825fe6060f1SDimitry Andric       auto *SecondTy = GetElementPtrInst::getIndexedType(
5826bdd1243dSDimitry Andric           SourceTy, ArrayRef(Ops).drop_front());
5827fe6060f1SDimitry Andric       NewAddr =
5828fe6060f1SDimitry Andric           Builder.CreateGEP(SecondTy, NewAddr, Constant::getNullValue(IndexTy));
58295ffd83dbSDimitry Andric     } else {
58305ffd83dbSDimitry Andric       Value *Base = Ops[0];
58315ffd83dbSDimitry Andric       Value *Index = Ops[FinalIndex];
58325ffd83dbSDimitry Andric 
58335ffd83dbSDimitry Andric       // Create a scalar GEP if there are more than 2 operands.
58345ffd83dbSDimitry Andric       if (Ops.size() != 2) {
58355ffd83dbSDimitry Andric         // Replace the last index with 0.
5836fe013be4SDimitry Andric         Ops[FinalIndex] =
5837fe013be4SDimitry Andric             Constant::getNullValue(Ops[FinalIndex]->getType()->getScalarType());
5838bdd1243dSDimitry Andric         Base = Builder.CreateGEP(SourceTy, Base, ArrayRef(Ops).drop_front());
5839fe6060f1SDimitry Andric         SourceTy = GetElementPtrInst::getIndexedType(
5840bdd1243dSDimitry Andric             SourceTy, ArrayRef(Ops).drop_front());
58415ffd83dbSDimitry Andric       }
58425ffd83dbSDimitry Andric 
58435ffd83dbSDimitry Andric       // Now create the GEP with scalar pointer and vector index.
5844fe6060f1SDimitry Andric       NewAddr = Builder.CreateGEP(SourceTy, Base, Index);
58455ffd83dbSDimitry Andric     }
5846e8d8bef9SDimitry Andric   } else if (!isa<Constant>(Ptr)) {
5847e8d8bef9SDimitry Andric     // Not a GEP, maybe its a splat and we can create a GEP to enable
5848e8d8bef9SDimitry Andric     // SelectionDAGBuilder to use it as a uniform base.
5849e8d8bef9SDimitry Andric     Value *V = getSplatValue(Ptr);
5850e8d8bef9SDimitry Andric     if (!V)
5851e8d8bef9SDimitry Andric       return false;
5852e8d8bef9SDimitry Andric 
5853e8d8bef9SDimitry Andric     auto NumElts = cast<VectorType>(Ptr->getType())->getElementCount();
5854e8d8bef9SDimitry Andric 
5855e8d8bef9SDimitry Andric     IRBuilder<> Builder(MemoryInst);
5856e8d8bef9SDimitry Andric 
5857e8d8bef9SDimitry Andric     // Emit a vector GEP with a scalar pointer and all 0s vector index.
5858e8d8bef9SDimitry Andric     Type *ScalarIndexTy = DL->getIndexType(V->getType()->getScalarType());
5859e8d8bef9SDimitry Andric     auto *IndexTy = VectorType::get(ScalarIndexTy, NumElts);
5860fe6060f1SDimitry Andric     Type *ScalarTy;
5861fe6060f1SDimitry Andric     if (cast<IntrinsicInst>(MemoryInst)->getIntrinsicID() ==
5862fe6060f1SDimitry Andric         Intrinsic::masked_gather) {
5863fe6060f1SDimitry Andric       ScalarTy = MemoryInst->getType()->getScalarType();
5864fe6060f1SDimitry Andric     } else {
5865fe6060f1SDimitry Andric       assert(cast<IntrinsicInst>(MemoryInst)->getIntrinsicID() ==
5866fe6060f1SDimitry Andric              Intrinsic::masked_scatter);
5867fe6060f1SDimitry Andric       ScalarTy = MemoryInst->getOperand(0)->getType()->getScalarType();
5868fe6060f1SDimitry Andric     }
5869fe6060f1SDimitry Andric     NewAddr = Builder.CreateGEP(ScalarTy, V, Constant::getNullValue(IndexTy));
5870e8d8bef9SDimitry Andric   } else {
5871e8d8bef9SDimitry Andric     // Constant, SelectionDAGBuilder knows to check if its a splat.
5872e8d8bef9SDimitry Andric     return false;
5873e8d8bef9SDimitry Andric   }
58745ffd83dbSDimitry Andric 
58755ffd83dbSDimitry Andric   MemoryInst->replaceUsesOfWith(Ptr, NewAddr);
58765ffd83dbSDimitry Andric 
58775ffd83dbSDimitry Andric   // If we have no uses, recursively delete the value and all dead instructions
58785ffd83dbSDimitry Andric   // using it.
58795ffd83dbSDimitry Andric   if (Ptr->use_empty())
5880e8d8bef9SDimitry Andric     RecursivelyDeleteTriviallyDeadInstructions(
5881e8d8bef9SDimitry Andric         Ptr, TLInfo, nullptr,
5882e8d8bef9SDimitry Andric         [&](Value *V) { removeAllAssertingVHReferences(V); });
58835ffd83dbSDimitry Andric 
58845ffd83dbSDimitry Andric   return true;
58855ffd83dbSDimitry Andric }
58865ffd83dbSDimitry Andric 
58870b57cec5SDimitry Andric /// If there are any memory operands, use OptimizeMemoryInst to sink their
58880b57cec5SDimitry Andric /// address computing into the block when possible / profitable.
optimizeInlineAsmInst(CallInst * CS)58890b57cec5SDimitry Andric bool CodeGenPrepare::optimizeInlineAsmInst(CallInst *CS) {
58900b57cec5SDimitry Andric   bool MadeChange = false;
58910b57cec5SDimitry Andric 
58920b57cec5SDimitry Andric   const TargetRegisterInfo *TRI =
58930b57cec5SDimitry Andric       TM->getSubtargetImpl(*CS->getFunction())->getRegisterInfo();
58940b57cec5SDimitry Andric   TargetLowering::AsmOperandInfoVector TargetConstraints =
58955ffd83dbSDimitry Andric       TLI->ParseConstraints(*DL, TRI, *CS);
58960b57cec5SDimitry Andric   unsigned ArgNo = 0;
58970eae32dcSDimitry Andric   for (TargetLowering::AsmOperandInfo &OpInfo : TargetConstraints) {
58980b57cec5SDimitry Andric     // Compute the constraint code and ConstraintType to use.
58990b57cec5SDimitry Andric     TLI->ComputeConstraintToUse(OpInfo, SDValue());
59000b57cec5SDimitry Andric 
590181ad6265SDimitry Andric     // TODO: Also handle C_Address?
59020b57cec5SDimitry Andric     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
59030b57cec5SDimitry Andric         OpInfo.isIndirect) {
59040b57cec5SDimitry Andric       Value *OpVal = CS->getArgOperand(ArgNo++);
59050b57cec5SDimitry Andric       MadeChange |= optimizeMemoryInst(CS, OpVal, OpVal->getType(), ~0u);
59060b57cec5SDimitry Andric     } else if (OpInfo.Type == InlineAsm::isInput)
59070b57cec5SDimitry Andric       ArgNo++;
59080b57cec5SDimitry Andric   }
59090b57cec5SDimitry Andric 
59100b57cec5SDimitry Andric   return MadeChange;
59110b57cec5SDimitry Andric }
59120b57cec5SDimitry Andric 
59130b57cec5SDimitry Andric /// Check if all the uses of \p Val are equivalent (or free) zero or
59140b57cec5SDimitry Andric /// sign extensions.
hasSameExtUse(Value * Val,const TargetLowering & TLI)59150b57cec5SDimitry Andric static bool hasSameExtUse(Value *Val, const TargetLowering &TLI) {
59160b57cec5SDimitry Andric   assert(!Val->use_empty() && "Input must have at least one use");
59170b57cec5SDimitry Andric   const Instruction *FirstUser = cast<Instruction>(*Val->user_begin());
59180b57cec5SDimitry Andric   bool IsSExt = isa<SExtInst>(FirstUser);
59190b57cec5SDimitry Andric   Type *ExtTy = FirstUser->getType();
59200b57cec5SDimitry Andric   for (const User *U : Val->users()) {
59210b57cec5SDimitry Andric     const Instruction *UI = cast<Instruction>(U);
59220b57cec5SDimitry Andric     if ((IsSExt && !isa<SExtInst>(UI)) || (!IsSExt && !isa<ZExtInst>(UI)))
59230b57cec5SDimitry Andric       return false;
59240b57cec5SDimitry Andric     Type *CurTy = UI->getType();
59250b57cec5SDimitry Andric     // Same input and output types: Same instruction after CSE.
59260b57cec5SDimitry Andric     if (CurTy == ExtTy)
59270b57cec5SDimitry Andric       continue;
59280b57cec5SDimitry Andric 
59290b57cec5SDimitry Andric     // If IsSExt is true, we are in this situation:
59300b57cec5SDimitry Andric     // a = Val
59310b57cec5SDimitry Andric     // b = sext ty1 a to ty2
59320b57cec5SDimitry Andric     // c = sext ty1 a to ty3
59330b57cec5SDimitry Andric     // Assuming ty2 is shorter than ty3, this could be turned into:
59340b57cec5SDimitry Andric     // a = Val
59350b57cec5SDimitry Andric     // b = sext ty1 a to ty2
59360b57cec5SDimitry Andric     // c = sext ty2 b to ty3
59370b57cec5SDimitry Andric     // However, the last sext is not free.
59380b57cec5SDimitry Andric     if (IsSExt)
59390b57cec5SDimitry Andric       return false;
59400b57cec5SDimitry Andric 
59410b57cec5SDimitry Andric     // This is a ZExt, maybe this is free to extend from one type to another.
59420b57cec5SDimitry Andric     // In that case, we would not account for a different use.
59430b57cec5SDimitry Andric     Type *NarrowTy;
59440b57cec5SDimitry Andric     Type *LargeTy;
59450b57cec5SDimitry Andric     if (ExtTy->getScalarType()->getIntegerBitWidth() >
59460b57cec5SDimitry Andric         CurTy->getScalarType()->getIntegerBitWidth()) {
59470b57cec5SDimitry Andric       NarrowTy = CurTy;
59480b57cec5SDimitry Andric       LargeTy = ExtTy;
59490b57cec5SDimitry Andric     } else {
59500b57cec5SDimitry Andric       NarrowTy = ExtTy;
59510b57cec5SDimitry Andric       LargeTy = CurTy;
59520b57cec5SDimitry Andric     }
59530b57cec5SDimitry Andric 
59540b57cec5SDimitry Andric     if (!TLI.isZExtFree(NarrowTy, LargeTy))
59550b57cec5SDimitry Andric       return false;
59560b57cec5SDimitry Andric   }
59570b57cec5SDimitry Andric   // All uses are the same or can be derived from one another for free.
59580b57cec5SDimitry Andric   return true;
59590b57cec5SDimitry Andric }
59600b57cec5SDimitry Andric 
59610b57cec5SDimitry Andric /// Try to speculatively promote extensions in \p Exts and continue
59620b57cec5SDimitry Andric /// promoting through newly promoted operands recursively as far as doing so is
59630b57cec5SDimitry Andric /// profitable. Save extensions profitably moved up, in \p ProfitablyMovedExts.
59640b57cec5SDimitry Andric /// When some promotion happened, \p TPT contains the proper state to revert
59650b57cec5SDimitry Andric /// them.
59660b57cec5SDimitry Andric ///
59670b57cec5SDimitry Andric /// \return true if some promotion happened, false otherwise.
tryToPromoteExts(TypePromotionTransaction & TPT,const SmallVectorImpl<Instruction * > & Exts,SmallVectorImpl<Instruction * > & ProfitablyMovedExts,unsigned CreatedInstsCost)59680b57cec5SDimitry Andric bool CodeGenPrepare::tryToPromoteExts(
59690b57cec5SDimitry Andric     TypePromotionTransaction &TPT, const SmallVectorImpl<Instruction *> &Exts,
59700b57cec5SDimitry Andric     SmallVectorImpl<Instruction *> &ProfitablyMovedExts,
59710b57cec5SDimitry Andric     unsigned CreatedInstsCost) {
59720b57cec5SDimitry Andric   bool Promoted = false;
59730b57cec5SDimitry Andric 
59740b57cec5SDimitry Andric   // Iterate over all the extensions to try to promote them.
59755ffd83dbSDimitry Andric   for (auto *I : Exts) {
59760b57cec5SDimitry Andric     // Early check if we directly have ext(load).
59770b57cec5SDimitry Andric     if (isa<LoadInst>(I->getOperand(0))) {
59780b57cec5SDimitry Andric       ProfitablyMovedExts.push_back(I);
59790b57cec5SDimitry Andric       continue;
59800b57cec5SDimitry Andric     }
59810b57cec5SDimitry Andric 
59820b57cec5SDimitry Andric     // Check whether or not we want to do any promotion.  The reason we have
59830b57cec5SDimitry Andric     // this check inside the for loop is to catch the case where an extension
59840b57cec5SDimitry Andric     // is directly fed by a load because in such case the extension can be moved
59850b57cec5SDimitry Andric     // up without any promotion on its operands.
59865ffd83dbSDimitry Andric     if (!TLI->enableExtLdPromotion() || DisableExtLdPromotion)
59870b57cec5SDimitry Andric       return false;
59880b57cec5SDimitry Andric 
59890b57cec5SDimitry Andric     // Get the action to perform the promotion.
59900b57cec5SDimitry Andric     TypePromotionHelper::Action TPH =
59910b57cec5SDimitry Andric         TypePromotionHelper::getAction(I, InsertedInsts, *TLI, PromotedInsts);
59920b57cec5SDimitry Andric     // Check if we can promote.
59930b57cec5SDimitry Andric     if (!TPH) {
59940b57cec5SDimitry Andric       // Save the current extension as we cannot move up through its operand.
59950b57cec5SDimitry Andric       ProfitablyMovedExts.push_back(I);
59960b57cec5SDimitry Andric       continue;
59970b57cec5SDimitry Andric     }
59980b57cec5SDimitry Andric 
59990b57cec5SDimitry Andric     // Save the current state.
60000b57cec5SDimitry Andric     TypePromotionTransaction::ConstRestorationPt LastKnownGood =
60010b57cec5SDimitry Andric         TPT.getRestorationPoint();
60020b57cec5SDimitry Andric     SmallVector<Instruction *, 4> NewExts;
60030b57cec5SDimitry Andric     unsigned NewCreatedInstsCost = 0;
60040b57cec5SDimitry Andric     unsigned ExtCost = !TLI->isExtFree(I);
60050b57cec5SDimitry Andric     // Promote.
60060b57cec5SDimitry Andric     Value *PromotedVal = TPH(I, TPT, PromotedInsts, NewCreatedInstsCost,
60070b57cec5SDimitry Andric                              &NewExts, nullptr, *TLI);
60080b57cec5SDimitry Andric     assert(PromotedVal &&
60090b57cec5SDimitry Andric            "TypePromotionHelper should have filtered out those cases");
60100b57cec5SDimitry Andric 
60110b57cec5SDimitry Andric     // We would be able to merge only one extension in a load.
60120b57cec5SDimitry Andric     // Therefore, if we have more than 1 new extension we heuristically
60130b57cec5SDimitry Andric     // cut this search path, because it means we degrade the code quality.
60140b57cec5SDimitry Andric     // With exactly 2, the transformation is neutral, because we will merge
60150b57cec5SDimitry Andric     // one extension but leave one. However, we optimistically keep going,
6016a58f00eaSDimitry Andric     // because the new extension may be removed too. Also avoid replacing a
6017a58f00eaSDimitry Andric     // single free extension with multiple extensions, as this increases the
6018a58f00eaSDimitry Andric     // number of IR instructions while not providing any savings.
60190b57cec5SDimitry Andric     long long TotalCreatedInstsCost = CreatedInstsCost + NewCreatedInstsCost;
60200b57cec5SDimitry Andric     // FIXME: It would be possible to propagate a negative value instead of
60210b57cec5SDimitry Andric     // conservatively ceiling it to 0.
60220b57cec5SDimitry Andric     TotalCreatedInstsCost =
60230b57cec5SDimitry Andric         std::max((long long)0, (TotalCreatedInstsCost - ExtCost));
60240b57cec5SDimitry Andric     if (!StressExtLdPromotion &&
60250b57cec5SDimitry Andric         (TotalCreatedInstsCost > 1 ||
6026a58f00eaSDimitry Andric          !isPromotedInstructionLegal(*TLI, *DL, PromotedVal) ||
6027a58f00eaSDimitry Andric          (ExtCost == 0 && NewExts.size() > 1))) {
60280b57cec5SDimitry Andric       // This promotion is not profitable, rollback to the previous state, and
60290b57cec5SDimitry Andric       // save the current extension in ProfitablyMovedExts as the latest
60300b57cec5SDimitry Andric       // speculative promotion turned out to be unprofitable.
60310b57cec5SDimitry Andric       TPT.rollback(LastKnownGood);
60320b57cec5SDimitry Andric       ProfitablyMovedExts.push_back(I);
60330b57cec5SDimitry Andric       continue;
60340b57cec5SDimitry Andric     }
60350b57cec5SDimitry Andric     // Continue promoting NewExts as far as doing so is profitable.
60360b57cec5SDimitry Andric     SmallVector<Instruction *, 2> NewlyMovedExts;
60370b57cec5SDimitry Andric     (void)tryToPromoteExts(TPT, NewExts, NewlyMovedExts, TotalCreatedInstsCost);
60380b57cec5SDimitry Andric     bool NewPromoted = false;
60395ffd83dbSDimitry Andric     for (auto *ExtInst : NewlyMovedExts) {
60400b57cec5SDimitry Andric       Instruction *MovedExt = cast<Instruction>(ExtInst);
60410b57cec5SDimitry Andric       Value *ExtOperand = MovedExt->getOperand(0);
60420b57cec5SDimitry Andric       // If we have reached to a load, we need this extra profitability check
60430b57cec5SDimitry Andric       // as it could potentially be merged into an ext(load).
60440b57cec5SDimitry Andric       if (isa<LoadInst>(ExtOperand) &&
60450b57cec5SDimitry Andric           !(StressExtLdPromotion || NewCreatedInstsCost <= ExtCost ||
60460b57cec5SDimitry Andric             (ExtOperand->hasOneUse() || hasSameExtUse(ExtOperand, *TLI))))
60470b57cec5SDimitry Andric         continue;
60480b57cec5SDimitry Andric 
60490b57cec5SDimitry Andric       ProfitablyMovedExts.push_back(MovedExt);
60500b57cec5SDimitry Andric       NewPromoted = true;
60510b57cec5SDimitry Andric     }
60520b57cec5SDimitry Andric 
60530b57cec5SDimitry Andric     // If none of speculative promotions for NewExts is profitable, rollback
60540b57cec5SDimitry Andric     // and save the current extension (I) as the last profitable extension.
60550b57cec5SDimitry Andric     if (!NewPromoted) {
60560b57cec5SDimitry Andric       TPT.rollback(LastKnownGood);
60570b57cec5SDimitry Andric       ProfitablyMovedExts.push_back(I);
60580b57cec5SDimitry Andric       continue;
60590b57cec5SDimitry Andric     }
60600b57cec5SDimitry Andric     // The promotion is profitable.
60610b57cec5SDimitry Andric     Promoted = true;
60620b57cec5SDimitry Andric   }
60630b57cec5SDimitry Andric   return Promoted;
60640b57cec5SDimitry Andric }
60650b57cec5SDimitry Andric 
60660b57cec5SDimitry Andric /// Merging redundant sexts when one is dominating the other.
mergeSExts(Function & F)60670b57cec5SDimitry Andric bool CodeGenPrepare::mergeSExts(Function &F) {
60680b57cec5SDimitry Andric   bool Changed = false;
60690b57cec5SDimitry Andric   for (auto &Entry : ValToSExtendedUses) {
60700b57cec5SDimitry Andric     SExts &Insts = Entry.second;
60710b57cec5SDimitry Andric     SExts CurPts;
60720b57cec5SDimitry Andric     for (Instruction *Inst : Insts) {
60730b57cec5SDimitry Andric       if (RemovedInsts.count(Inst) || !isa<SExtInst>(Inst) ||
60740b57cec5SDimitry Andric           Inst->getOperand(0) != Entry.first)
60750b57cec5SDimitry Andric         continue;
60760b57cec5SDimitry Andric       bool inserted = false;
60770b57cec5SDimitry Andric       for (auto &Pt : CurPts) {
60780b57cec5SDimitry Andric         if (getDT(F).dominates(Inst, Pt)) {
6079bdd1243dSDimitry Andric           replaceAllUsesWith(Pt, Inst, FreshBBs, IsHugeFunc);
60800b57cec5SDimitry Andric           RemovedInsts.insert(Pt);
60810b57cec5SDimitry Andric           Pt->removeFromParent();
60820b57cec5SDimitry Andric           Pt = Inst;
60830b57cec5SDimitry Andric           inserted = true;
60840b57cec5SDimitry Andric           Changed = true;
60850b57cec5SDimitry Andric           break;
60860b57cec5SDimitry Andric         }
60870b57cec5SDimitry Andric         if (!getDT(F).dominates(Pt, Inst))
60880b57cec5SDimitry Andric           // Give up if we need to merge in a common dominator as the
60890b57cec5SDimitry Andric           // experiments show it is not profitable.
60900b57cec5SDimitry Andric           continue;
6091bdd1243dSDimitry Andric         replaceAllUsesWith(Inst, Pt, FreshBBs, IsHugeFunc);
60920b57cec5SDimitry Andric         RemovedInsts.insert(Inst);
60930b57cec5SDimitry Andric         Inst->removeFromParent();
60940b57cec5SDimitry Andric         inserted = true;
60950b57cec5SDimitry Andric         Changed = true;
60960b57cec5SDimitry Andric         break;
60970b57cec5SDimitry Andric       }
60980b57cec5SDimitry Andric       if (!inserted)
60990b57cec5SDimitry Andric         CurPts.push_back(Inst);
61000b57cec5SDimitry Andric     }
61010b57cec5SDimitry Andric   }
61020b57cec5SDimitry Andric   return Changed;
61030b57cec5SDimitry Andric }
61040b57cec5SDimitry Andric 
61055ffd83dbSDimitry Andric // Splitting large data structures so that the GEPs accessing them can have
61060b57cec5SDimitry Andric // smaller offsets so that they can be sunk to the same blocks as their users.
61075ffd83dbSDimitry Andric // For example, a large struct starting from %base is split into two parts
61080b57cec5SDimitry Andric // where the second part starts from %new_base.
61090b57cec5SDimitry Andric //
61100b57cec5SDimitry Andric // Before:
61110b57cec5SDimitry Andric // BB0:
61120b57cec5SDimitry Andric //   %base     =
61130b57cec5SDimitry Andric //
61140b57cec5SDimitry Andric // BB1:
61150b57cec5SDimitry Andric //   %gep0     = gep %base, off0
61160b57cec5SDimitry Andric //   %gep1     = gep %base, off1
61170b57cec5SDimitry Andric //   %gep2     = gep %base, off2
61180b57cec5SDimitry Andric //
61190b57cec5SDimitry Andric // BB2:
61200b57cec5SDimitry Andric //   %load1    = load %gep0
61210b57cec5SDimitry Andric //   %load2    = load %gep1
61220b57cec5SDimitry Andric //   %load3    = load %gep2
61230b57cec5SDimitry Andric //
61240b57cec5SDimitry Andric // After:
61250b57cec5SDimitry Andric // BB0:
61260b57cec5SDimitry Andric //   %base     =
61270b57cec5SDimitry Andric //   %new_base = gep %base, off0
61280b57cec5SDimitry Andric //
61290b57cec5SDimitry Andric // BB1:
61300b57cec5SDimitry Andric //   %new_gep0 = %new_base
61310b57cec5SDimitry Andric //   %new_gep1 = gep %new_base, off1 - off0
61320b57cec5SDimitry Andric //   %new_gep2 = gep %new_base, off2 - off0
61330b57cec5SDimitry Andric //
61340b57cec5SDimitry Andric // BB2:
61350b57cec5SDimitry Andric //   %load1    = load i32, i32* %new_gep0
61360b57cec5SDimitry Andric //   %load2    = load i32, i32* %new_gep1
61370b57cec5SDimitry Andric //   %load3    = load i32, i32* %new_gep2
61380b57cec5SDimitry Andric //
61390b57cec5SDimitry Andric // %new_gep1 and %new_gep2 can be sunk to BB2 now after the splitting because
61400b57cec5SDimitry Andric // their offsets are smaller enough to fit into the addressing mode.
splitLargeGEPOffsets()61410b57cec5SDimitry Andric bool CodeGenPrepare::splitLargeGEPOffsets() {
61420b57cec5SDimitry Andric   bool Changed = false;
61430b57cec5SDimitry Andric   for (auto &Entry : LargeOffsetGEPMap) {
61440b57cec5SDimitry Andric     Value *OldBase = Entry.first;
61450b57cec5SDimitry Andric     SmallVectorImpl<std::pair<AssertingVH<GetElementPtrInst>, int64_t>>
61460b57cec5SDimitry Andric         &LargeOffsetGEPs = Entry.second;
61470b57cec5SDimitry Andric     auto compareGEPOffset =
61480b57cec5SDimitry Andric         [&](const std::pair<GetElementPtrInst *, int64_t> &LHS,
61490b57cec5SDimitry Andric             const std::pair<GetElementPtrInst *, int64_t> &RHS) {
61500b57cec5SDimitry Andric           if (LHS.first == RHS.first)
61510b57cec5SDimitry Andric             return false;
61520b57cec5SDimitry Andric           if (LHS.second != RHS.second)
61530b57cec5SDimitry Andric             return LHS.second < RHS.second;
61540b57cec5SDimitry Andric           return LargeOffsetGEPID[LHS.first] < LargeOffsetGEPID[RHS.first];
61550b57cec5SDimitry Andric         };
61560b57cec5SDimitry Andric     // Sorting all the GEPs of the same data structures based on the offsets.
61570b57cec5SDimitry Andric     llvm::sort(LargeOffsetGEPs, compareGEPOffset);
61580b57cec5SDimitry Andric     LargeOffsetGEPs.erase(
61590b57cec5SDimitry Andric         std::unique(LargeOffsetGEPs.begin(), LargeOffsetGEPs.end()),
61600b57cec5SDimitry Andric         LargeOffsetGEPs.end());
61610b57cec5SDimitry Andric     // Skip if all the GEPs have the same offsets.
61620b57cec5SDimitry Andric     if (LargeOffsetGEPs.front().second == LargeOffsetGEPs.back().second)
61630b57cec5SDimitry Andric       continue;
61640b57cec5SDimitry Andric     GetElementPtrInst *BaseGEP = LargeOffsetGEPs.begin()->first;
61650b57cec5SDimitry Andric     int64_t BaseOffset = LargeOffsetGEPs.begin()->second;
61660b57cec5SDimitry Andric     Value *NewBaseGEP = nullptr;
61670b57cec5SDimitry Andric 
6168c9157d92SDimitry Andric     auto createNewBase = [&](int64_t BaseOffset, Value *OldBase,
6169c9157d92SDimitry Andric                              GetElementPtrInst *GEP) {
61700b57cec5SDimitry Andric       LLVMContext &Ctx = GEP->getContext();
6171fe013be4SDimitry Andric       Type *PtrIdxTy = DL->getIndexType(GEP->getType());
61720b57cec5SDimitry Andric       Type *I8PtrTy =
6173c9157d92SDimitry Andric           PointerType::get(Ctx, GEP->getType()->getPointerAddressSpace());
61740b57cec5SDimitry Andric 
61750b57cec5SDimitry Andric       BasicBlock::iterator NewBaseInsertPt;
61760b57cec5SDimitry Andric       BasicBlock *NewBaseInsertBB;
61770b57cec5SDimitry Andric       if (auto *BaseI = dyn_cast<Instruction>(OldBase)) {
61780b57cec5SDimitry Andric         // If the base of the struct is an instruction, the new base will be
61790b57cec5SDimitry Andric         // inserted close to it.
61800b57cec5SDimitry Andric         NewBaseInsertBB = BaseI->getParent();
61810b57cec5SDimitry Andric         if (isa<PHINode>(BaseI))
61820b57cec5SDimitry Andric           NewBaseInsertPt = NewBaseInsertBB->getFirstInsertionPt();
61830b57cec5SDimitry Andric         else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(BaseI)) {
61840b57cec5SDimitry Andric           NewBaseInsertBB =
6185fe013be4SDimitry Andric               SplitEdge(NewBaseInsertBB, Invoke->getNormalDest(), DT.get(), LI);
61860b57cec5SDimitry Andric           NewBaseInsertPt = NewBaseInsertBB->getFirstInsertionPt();
61870b57cec5SDimitry Andric         } else
61880b57cec5SDimitry Andric           NewBaseInsertPt = std::next(BaseI->getIterator());
61890b57cec5SDimitry Andric       } else {
61900b57cec5SDimitry Andric         // If the current base is an argument or global value, the new base
61910b57cec5SDimitry Andric         // will be inserted to the entry block.
61920b57cec5SDimitry Andric         NewBaseInsertBB = &BaseGEP->getFunction()->getEntryBlock();
61930b57cec5SDimitry Andric         NewBaseInsertPt = NewBaseInsertBB->getFirstInsertionPt();
61940b57cec5SDimitry Andric       }
61950b57cec5SDimitry Andric       IRBuilder<> NewBaseBuilder(NewBaseInsertBB, NewBaseInsertPt);
61960b57cec5SDimitry Andric       // Create a new base.
6197fe013be4SDimitry Andric       Value *BaseIndex = ConstantInt::get(PtrIdxTy, BaseOffset);
61980b57cec5SDimitry Andric       NewBaseGEP = OldBase;
61990b57cec5SDimitry Andric       if (NewBaseGEP->getType() != I8PtrTy)
62000b57cec5SDimitry Andric         NewBaseGEP = NewBaseBuilder.CreatePointerCast(NewBaseGEP, I8PtrTy);
62010b57cec5SDimitry Andric       NewBaseGEP =
6202a58f00eaSDimitry Andric           NewBaseBuilder.CreatePtrAdd(NewBaseGEP, BaseIndex, "splitgep");
62030b57cec5SDimitry Andric       NewGEPBases.insert(NewBaseGEP);
6204c9157d92SDimitry Andric       return;
6205c9157d92SDimitry Andric     };
6206c9157d92SDimitry Andric 
6207c9157d92SDimitry Andric     // Check whether all the offsets can be encoded with prefered common base.
6208c9157d92SDimitry Andric     if (int64_t PreferBase = TLI->getPreferredLargeGEPBaseOffset(
6209c9157d92SDimitry Andric             LargeOffsetGEPs.front().second, LargeOffsetGEPs.back().second)) {
6210c9157d92SDimitry Andric       BaseOffset = PreferBase;
6211c9157d92SDimitry Andric       // Create a new base if the offset of the BaseGEP can be decoded with one
6212c9157d92SDimitry Andric       // instruction.
6213c9157d92SDimitry Andric       createNewBase(BaseOffset, OldBase, BaseGEP);
6214c9157d92SDimitry Andric     }
6215c9157d92SDimitry Andric 
6216c9157d92SDimitry Andric     auto *LargeOffsetGEP = LargeOffsetGEPs.begin();
6217c9157d92SDimitry Andric     while (LargeOffsetGEP != LargeOffsetGEPs.end()) {
6218c9157d92SDimitry Andric       GetElementPtrInst *GEP = LargeOffsetGEP->first;
6219c9157d92SDimitry Andric       int64_t Offset = LargeOffsetGEP->second;
6220c9157d92SDimitry Andric       if (Offset != BaseOffset) {
6221c9157d92SDimitry Andric         TargetLowering::AddrMode AddrMode;
6222c9157d92SDimitry Andric         AddrMode.HasBaseReg = true;
6223c9157d92SDimitry Andric         AddrMode.BaseOffs = Offset - BaseOffset;
6224c9157d92SDimitry Andric         // The result type of the GEP might not be the type of the memory
6225c9157d92SDimitry Andric         // access.
6226c9157d92SDimitry Andric         if (!TLI->isLegalAddressingMode(*DL, AddrMode,
6227c9157d92SDimitry Andric                                         GEP->getResultElementType(),
6228c9157d92SDimitry Andric                                         GEP->getAddressSpace())) {
6229c9157d92SDimitry Andric           // We need to create a new base if the offset to the current base is
6230c9157d92SDimitry Andric           // too large to fit into the addressing mode. So, a very large struct
6231c9157d92SDimitry Andric           // may be split into several parts.
6232c9157d92SDimitry Andric           BaseGEP = GEP;
6233c9157d92SDimitry Andric           BaseOffset = Offset;
6234c9157d92SDimitry Andric           NewBaseGEP = nullptr;
6235c9157d92SDimitry Andric         }
6236c9157d92SDimitry Andric       }
6237c9157d92SDimitry Andric 
6238c9157d92SDimitry Andric       // Generate a new GEP to replace the current one.
6239c9157d92SDimitry Andric       Type *PtrIdxTy = DL->getIndexType(GEP->getType());
6240c9157d92SDimitry Andric 
6241c9157d92SDimitry Andric       if (!NewBaseGEP) {
6242c9157d92SDimitry Andric         // Create a new base if we don't have one yet.  Find the insertion
6243c9157d92SDimitry Andric         // pointer for the new base first.
6244c9157d92SDimitry Andric         createNewBase(BaseOffset, OldBase, GEP);
62450b57cec5SDimitry Andric       }
62460b57cec5SDimitry Andric 
62470b57cec5SDimitry Andric       IRBuilder<> Builder(GEP);
62480b57cec5SDimitry Andric       Value *NewGEP = NewBaseGEP;
6249c9157d92SDimitry Andric       if (Offset != BaseOffset) {
62500b57cec5SDimitry Andric         // Calculate the new offset for the new GEP.
6251fe013be4SDimitry Andric         Value *Index = ConstantInt::get(PtrIdxTy, Offset - BaseOffset);
6252a58f00eaSDimitry Andric         NewGEP = Builder.CreatePtrAdd(NewBaseGEP, Index);
62530b57cec5SDimitry Andric       }
6254bdd1243dSDimitry Andric       replaceAllUsesWith(GEP, NewGEP, FreshBBs, IsHugeFunc);
62550b57cec5SDimitry Andric       LargeOffsetGEPID.erase(GEP);
62560b57cec5SDimitry Andric       LargeOffsetGEP = LargeOffsetGEPs.erase(LargeOffsetGEP);
62570b57cec5SDimitry Andric       GEP->eraseFromParent();
62580b57cec5SDimitry Andric       Changed = true;
62590b57cec5SDimitry Andric     }
62600b57cec5SDimitry Andric   }
62610b57cec5SDimitry Andric   return Changed;
62620b57cec5SDimitry Andric }
62630b57cec5SDimitry Andric 
optimizePhiType(PHINode * I,SmallPtrSetImpl<PHINode * > & Visited,SmallPtrSetImpl<Instruction * > & DeletedInstrs)62645ffd83dbSDimitry Andric bool CodeGenPrepare::optimizePhiType(
62655ffd83dbSDimitry Andric     PHINode *I, SmallPtrSetImpl<PHINode *> &Visited,
62665ffd83dbSDimitry Andric     SmallPtrSetImpl<Instruction *> &DeletedInstrs) {
62675ffd83dbSDimitry Andric   // We are looking for a collection on interconnected phi nodes that together
62685ffd83dbSDimitry Andric   // only use loads/bitcasts and are used by stores/bitcasts, and the bitcasts
62695ffd83dbSDimitry Andric   // are of the same type. Convert the whole set of nodes to the type of the
62705ffd83dbSDimitry Andric   // bitcast.
62715ffd83dbSDimitry Andric   Type *PhiTy = I->getType();
62725ffd83dbSDimitry Andric   Type *ConvertTy = nullptr;
62735ffd83dbSDimitry Andric   if (Visited.count(I) ||
62745ffd83dbSDimitry Andric       (!I->getType()->isIntegerTy() && !I->getType()->isFloatingPointTy()))
62755ffd83dbSDimitry Andric     return false;
62765ffd83dbSDimitry Andric 
62775ffd83dbSDimitry Andric   SmallVector<Instruction *, 4> Worklist;
62785ffd83dbSDimitry Andric   Worklist.push_back(cast<Instruction>(I));
62795ffd83dbSDimitry Andric   SmallPtrSet<PHINode *, 4> PhiNodes;
6280bdd1243dSDimitry Andric   SmallPtrSet<ConstantData *, 4> Constants;
62815ffd83dbSDimitry Andric   PhiNodes.insert(I);
62825ffd83dbSDimitry Andric   Visited.insert(I);
62835ffd83dbSDimitry Andric   SmallPtrSet<Instruction *, 4> Defs;
62845ffd83dbSDimitry Andric   SmallPtrSet<Instruction *, 4> Uses;
6285e8d8bef9SDimitry Andric   // This works by adding extra bitcasts between load/stores and removing
6286e8d8bef9SDimitry Andric   // existing bicasts. If we have a phi(bitcast(load)) or a store(bitcast(phi))
6287e8d8bef9SDimitry Andric   // we can get in the situation where we remove a bitcast in one iteration
6288e8d8bef9SDimitry Andric   // just to add it again in the next. We need to ensure that at least one
6289e8d8bef9SDimitry Andric   // bitcast we remove are anchored to something that will not change back.
6290e8d8bef9SDimitry Andric   bool AnyAnchored = false;
62915ffd83dbSDimitry Andric 
62925ffd83dbSDimitry Andric   while (!Worklist.empty()) {
62935ffd83dbSDimitry Andric     Instruction *II = Worklist.pop_back_val();
62945ffd83dbSDimitry Andric 
62955ffd83dbSDimitry Andric     if (auto *Phi = dyn_cast<PHINode>(II)) {
62965ffd83dbSDimitry Andric       // Handle Defs, which might also be PHI's
62975ffd83dbSDimitry Andric       for (Value *V : Phi->incoming_values()) {
62985ffd83dbSDimitry Andric         if (auto *OpPhi = dyn_cast<PHINode>(V)) {
62995ffd83dbSDimitry Andric           if (!PhiNodes.count(OpPhi)) {
630081ad6265SDimitry Andric             if (!Visited.insert(OpPhi).second)
63015ffd83dbSDimitry Andric               return false;
63025ffd83dbSDimitry Andric             PhiNodes.insert(OpPhi);
63035ffd83dbSDimitry Andric             Worklist.push_back(OpPhi);
63045ffd83dbSDimitry Andric           }
63055ffd83dbSDimitry Andric         } else if (auto *OpLoad = dyn_cast<LoadInst>(V)) {
6306e8d8bef9SDimitry Andric           if (!OpLoad->isSimple())
6307e8d8bef9SDimitry Andric             return false;
630881ad6265SDimitry Andric           if (Defs.insert(OpLoad).second)
63095ffd83dbSDimitry Andric             Worklist.push_back(OpLoad);
63105ffd83dbSDimitry Andric         } else if (auto *OpEx = dyn_cast<ExtractElementInst>(V)) {
631181ad6265SDimitry Andric           if (Defs.insert(OpEx).second)
63125ffd83dbSDimitry Andric             Worklist.push_back(OpEx);
63135ffd83dbSDimitry Andric         } else if (auto *OpBC = dyn_cast<BitCastInst>(V)) {
63145ffd83dbSDimitry Andric           if (!ConvertTy)
63155ffd83dbSDimitry Andric             ConvertTy = OpBC->getOperand(0)->getType();
63165ffd83dbSDimitry Andric           if (OpBC->getOperand(0)->getType() != ConvertTy)
63175ffd83dbSDimitry Andric             return false;
631881ad6265SDimitry Andric           if (Defs.insert(OpBC).second) {
63195ffd83dbSDimitry Andric             Worklist.push_back(OpBC);
6320e8d8bef9SDimitry Andric             AnyAnchored |= !isa<LoadInst>(OpBC->getOperand(0)) &&
6321e8d8bef9SDimitry Andric                            !isa<ExtractElementInst>(OpBC->getOperand(0));
63225ffd83dbSDimitry Andric           }
6323bdd1243dSDimitry Andric         } else if (auto *OpC = dyn_cast<ConstantData>(V))
6324bdd1243dSDimitry Andric           Constants.insert(OpC);
6325bdd1243dSDimitry Andric         else
63265ffd83dbSDimitry Andric           return false;
63275ffd83dbSDimitry Andric       }
63285ffd83dbSDimitry Andric     }
63295ffd83dbSDimitry Andric 
63305ffd83dbSDimitry Andric     // Handle uses which might also be phi's
63315ffd83dbSDimitry Andric     for (User *V : II->users()) {
63325ffd83dbSDimitry Andric       if (auto *OpPhi = dyn_cast<PHINode>(V)) {
63335ffd83dbSDimitry Andric         if (!PhiNodes.count(OpPhi)) {
63345ffd83dbSDimitry Andric           if (Visited.count(OpPhi))
63355ffd83dbSDimitry Andric             return false;
63365ffd83dbSDimitry Andric           PhiNodes.insert(OpPhi);
63375ffd83dbSDimitry Andric           Visited.insert(OpPhi);
63385ffd83dbSDimitry Andric           Worklist.push_back(OpPhi);
63395ffd83dbSDimitry Andric         }
63405ffd83dbSDimitry Andric       } else if (auto *OpStore = dyn_cast<StoreInst>(V)) {
6341e8d8bef9SDimitry Andric         if (!OpStore->isSimple() || OpStore->getOperand(0) != II)
63425ffd83dbSDimitry Andric           return false;
63435ffd83dbSDimitry Andric         Uses.insert(OpStore);
63445ffd83dbSDimitry Andric       } else if (auto *OpBC = dyn_cast<BitCastInst>(V)) {
63455ffd83dbSDimitry Andric         if (!ConvertTy)
63465ffd83dbSDimitry Andric           ConvertTy = OpBC->getType();
63475ffd83dbSDimitry Andric         if (OpBC->getType() != ConvertTy)
63485ffd83dbSDimitry Andric           return false;
63495ffd83dbSDimitry Andric         Uses.insert(OpBC);
6350e8d8bef9SDimitry Andric         AnyAnchored |=
6351e8d8bef9SDimitry Andric             any_of(OpBC->users(), [](User *U) { return !isa<StoreInst>(U); });
6352e8d8bef9SDimitry Andric       } else {
63535ffd83dbSDimitry Andric         return false;
63545ffd83dbSDimitry Andric       }
63555ffd83dbSDimitry Andric     }
6356e8d8bef9SDimitry Andric   }
63575ffd83dbSDimitry Andric 
6358bdd1243dSDimitry Andric   if (!ConvertTy || !AnyAnchored ||
6359bdd1243dSDimitry Andric       !TLI->shouldConvertPhiType(PhiTy, ConvertTy))
63605ffd83dbSDimitry Andric     return false;
63615ffd83dbSDimitry Andric 
63625ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "Converting " << *I << "\n  and connected nodes to "
63635ffd83dbSDimitry Andric                     << *ConvertTy << "\n");
63645ffd83dbSDimitry Andric 
63655ffd83dbSDimitry Andric   // Create all the new phi nodes of the new type, and bitcast any loads to the
63665ffd83dbSDimitry Andric   // correct type.
63675ffd83dbSDimitry Andric   ValueToValueMap ValMap;
6368bdd1243dSDimitry Andric   for (ConstantData *C : Constants)
6369c9157d92SDimitry Andric     ValMap[C] = ConstantExpr::getBitCast(C, ConvertTy);
63705ffd83dbSDimitry Andric   for (Instruction *D : Defs) {
6371e8d8bef9SDimitry Andric     if (isa<BitCastInst>(D)) {
63725ffd83dbSDimitry Andric       ValMap[D] = D->getOperand(0);
6373e8d8bef9SDimitry Andric       DeletedInstrs.insert(D);
6374e8d8bef9SDimitry Andric     } else {
63755ffd83dbSDimitry Andric       ValMap[D] =
63765ffd83dbSDimitry Andric           new BitCastInst(D, ConvertTy, D->getName() + ".bc", D->getNextNode());
63775ffd83dbSDimitry Andric     }
6378e8d8bef9SDimitry Andric   }
63795ffd83dbSDimitry Andric   for (PHINode *Phi : PhiNodes)
63805ffd83dbSDimitry Andric     ValMap[Phi] = PHINode::Create(ConvertTy, Phi->getNumIncomingValues(),
63815ffd83dbSDimitry Andric                                   Phi->getName() + ".tc", Phi);
63825ffd83dbSDimitry Andric   // Pipe together all the PhiNodes.
63835ffd83dbSDimitry Andric   for (PHINode *Phi : PhiNodes) {
63845ffd83dbSDimitry Andric     PHINode *NewPhi = cast<PHINode>(ValMap[Phi]);
63855ffd83dbSDimitry Andric     for (int i = 0, e = Phi->getNumIncomingValues(); i < e; i++)
63865ffd83dbSDimitry Andric       NewPhi->addIncoming(ValMap[Phi->getIncomingValue(i)],
63875ffd83dbSDimitry Andric                           Phi->getIncomingBlock(i));
6388e8d8bef9SDimitry Andric     Visited.insert(NewPhi);
63895ffd83dbSDimitry Andric   }
63905ffd83dbSDimitry Andric   // And finally pipe up the stores and bitcasts
63915ffd83dbSDimitry Andric   for (Instruction *U : Uses) {
63925ffd83dbSDimitry Andric     if (isa<BitCastInst>(U)) {
63935ffd83dbSDimitry Andric       DeletedInstrs.insert(U);
6394bdd1243dSDimitry Andric       replaceAllUsesWith(U, ValMap[U->getOperand(0)], FreshBBs, IsHugeFunc);
6395e8d8bef9SDimitry Andric     } else {
63965ffd83dbSDimitry Andric       U->setOperand(0,
63975ffd83dbSDimitry Andric                     new BitCastInst(ValMap[U->getOperand(0)], PhiTy, "bc", U));
63985ffd83dbSDimitry Andric     }
6399e8d8bef9SDimitry Andric   }
64005ffd83dbSDimitry Andric 
64015ffd83dbSDimitry Andric   // Save the removed phis to be deleted later.
64025ffd83dbSDimitry Andric   for (PHINode *Phi : PhiNodes)
64035ffd83dbSDimitry Andric     DeletedInstrs.insert(Phi);
64045ffd83dbSDimitry Andric   return true;
64055ffd83dbSDimitry Andric }
64065ffd83dbSDimitry Andric 
optimizePhiTypes(Function & F)64075ffd83dbSDimitry Andric bool CodeGenPrepare::optimizePhiTypes(Function &F) {
64085ffd83dbSDimitry Andric   if (!OptimizePhiTypes)
64095ffd83dbSDimitry Andric     return false;
64105ffd83dbSDimitry Andric 
64115ffd83dbSDimitry Andric   bool Changed = false;
64125ffd83dbSDimitry Andric   SmallPtrSet<PHINode *, 4> Visited;
64135ffd83dbSDimitry Andric   SmallPtrSet<Instruction *, 4> DeletedInstrs;
64145ffd83dbSDimitry Andric 
64155ffd83dbSDimitry Andric   // Attempt to optimize all the phis in the functions to the correct type.
64165ffd83dbSDimitry Andric   for (auto &BB : F)
64175ffd83dbSDimitry Andric     for (auto &Phi : BB.phis())
64185ffd83dbSDimitry Andric       Changed |= optimizePhiType(&Phi, Visited, DeletedInstrs);
64195ffd83dbSDimitry Andric 
64205ffd83dbSDimitry Andric   // Remove any old phi's that have been converted.
64215ffd83dbSDimitry Andric   for (auto *I : DeletedInstrs) {
6422bdd1243dSDimitry Andric     replaceAllUsesWith(I, PoisonValue::get(I->getType()), FreshBBs, IsHugeFunc);
64235ffd83dbSDimitry Andric     I->eraseFromParent();
64245ffd83dbSDimitry Andric   }
64255ffd83dbSDimitry Andric 
64265ffd83dbSDimitry Andric   return Changed;
64275ffd83dbSDimitry Andric }
64285ffd83dbSDimitry Andric 
64290b57cec5SDimitry Andric /// Return true, if an ext(load) can be formed from an extension in
64300b57cec5SDimitry Andric /// \p MovedExts.
canFormExtLd(const SmallVectorImpl<Instruction * > & MovedExts,LoadInst * & LI,Instruction * & Inst,bool HasPromoted)64310b57cec5SDimitry Andric bool CodeGenPrepare::canFormExtLd(
64320b57cec5SDimitry Andric     const SmallVectorImpl<Instruction *> &MovedExts, LoadInst *&LI,
64330b57cec5SDimitry Andric     Instruction *&Inst, bool HasPromoted) {
64340b57cec5SDimitry Andric   for (auto *MovedExtInst : MovedExts) {
64350b57cec5SDimitry Andric     if (isa<LoadInst>(MovedExtInst->getOperand(0))) {
64360b57cec5SDimitry Andric       LI = cast<LoadInst>(MovedExtInst->getOperand(0));
64370b57cec5SDimitry Andric       Inst = MovedExtInst;
64380b57cec5SDimitry Andric       break;
64390b57cec5SDimitry Andric     }
64400b57cec5SDimitry Andric   }
64410b57cec5SDimitry Andric   if (!LI)
64420b57cec5SDimitry Andric     return false;
64430b57cec5SDimitry Andric 
64440b57cec5SDimitry Andric   // If they're already in the same block, there's nothing to do.
64450b57cec5SDimitry Andric   // Make the cheap checks first if we did not promote.
64460b57cec5SDimitry Andric   // If we promoted, we need to check if it is indeed profitable.
64470b57cec5SDimitry Andric   if (!HasPromoted && LI->getParent() == Inst->getParent())
64480b57cec5SDimitry Andric     return false;
64490b57cec5SDimitry Andric 
64500b57cec5SDimitry Andric   return TLI->isExtLoad(LI, Inst, *DL);
64510b57cec5SDimitry Andric }
64520b57cec5SDimitry Andric 
64530b57cec5SDimitry Andric /// Move a zext or sext fed by a load into the same basic block as the load,
64540b57cec5SDimitry Andric /// unless conditions are unfavorable. This allows SelectionDAG to fold the
64550b57cec5SDimitry Andric /// extend into the load.
64560b57cec5SDimitry Andric ///
64570b57cec5SDimitry Andric /// E.g.,
64580b57cec5SDimitry Andric /// \code
64590b57cec5SDimitry Andric /// %ld = load i32* %addr
64600b57cec5SDimitry Andric /// %add = add nuw i32 %ld, 4
64610b57cec5SDimitry Andric /// %zext = zext i32 %add to i64
64620b57cec5SDimitry Andric // \endcode
64630b57cec5SDimitry Andric /// =>
64640b57cec5SDimitry Andric /// \code
64650b57cec5SDimitry Andric /// %ld = load i32* %addr
64660b57cec5SDimitry Andric /// %zext = zext i32 %ld to i64
64670b57cec5SDimitry Andric /// %add = add nuw i64 %zext, 4
64680b57cec5SDimitry Andric /// \encode
64690b57cec5SDimitry Andric /// Note that the promotion in %add to i64 is done in tryToPromoteExts(), which
64700b57cec5SDimitry Andric /// allow us to match zext(load i32*) to i64.
64710b57cec5SDimitry Andric ///
64720b57cec5SDimitry Andric /// Also, try to promote the computations used to obtain a sign extended
64730b57cec5SDimitry Andric /// value used into memory accesses.
64740b57cec5SDimitry Andric /// E.g.,
64750b57cec5SDimitry Andric /// \code
64760b57cec5SDimitry Andric /// a = add nsw i32 b, 3
64770b57cec5SDimitry Andric /// d = sext i32 a to i64
64780b57cec5SDimitry Andric /// e = getelementptr ..., i64 d
64790b57cec5SDimitry Andric /// \endcode
64800b57cec5SDimitry Andric /// =>
64810b57cec5SDimitry Andric /// \code
64820b57cec5SDimitry Andric /// f = sext i32 b to i64
64830b57cec5SDimitry Andric /// a = add nsw i64 f, 3
64840b57cec5SDimitry Andric /// e = getelementptr ..., i64 a
64850b57cec5SDimitry Andric /// \endcode
64860b57cec5SDimitry Andric ///
64870b57cec5SDimitry Andric /// \p Inst[in/out] the extension may be modified during the process if some
64880b57cec5SDimitry Andric /// promotions apply.
optimizeExt(Instruction * & Inst)64890b57cec5SDimitry Andric bool CodeGenPrepare::optimizeExt(Instruction *&Inst) {
64900b57cec5SDimitry Andric   bool AllowPromotionWithoutCommonHeader = false;
64910b57cec5SDimitry Andric   /// See if it is an interesting sext operations for the address type
64920b57cec5SDimitry Andric   /// promotion before trying to promote it, e.g., the ones with the right
64930b57cec5SDimitry Andric   /// type and used in memory accesses.
64940b57cec5SDimitry Andric   bool ATPConsiderable = TTI->shouldConsiderAddressTypePromotion(
64950b57cec5SDimitry Andric       *Inst, AllowPromotionWithoutCommonHeader);
64960b57cec5SDimitry Andric   TypePromotionTransaction TPT(RemovedInsts);
64970b57cec5SDimitry Andric   TypePromotionTransaction::ConstRestorationPt LastKnownGood =
64980b57cec5SDimitry Andric       TPT.getRestorationPoint();
64990b57cec5SDimitry Andric   SmallVector<Instruction *, 1> Exts;
65000b57cec5SDimitry Andric   SmallVector<Instruction *, 2> SpeculativelyMovedExts;
65010b57cec5SDimitry Andric   Exts.push_back(Inst);
65020b57cec5SDimitry Andric 
65030b57cec5SDimitry Andric   bool HasPromoted = tryToPromoteExts(TPT, Exts, SpeculativelyMovedExts);
65040b57cec5SDimitry Andric 
65050b57cec5SDimitry Andric   // Look for a load being extended.
65060b57cec5SDimitry Andric   LoadInst *LI = nullptr;
65070b57cec5SDimitry Andric   Instruction *ExtFedByLoad;
65080b57cec5SDimitry Andric 
65090b57cec5SDimitry Andric   // Try to promote a chain of computation if it allows to form an extended
65100b57cec5SDimitry Andric   // load.
65110b57cec5SDimitry Andric   if (canFormExtLd(SpeculativelyMovedExts, LI, ExtFedByLoad, HasPromoted)) {
65120b57cec5SDimitry Andric     assert(LI && ExtFedByLoad && "Expect a valid load and extension");
65130b57cec5SDimitry Andric     TPT.commit();
65145ffd83dbSDimitry Andric     // Move the extend into the same block as the load.
65150b57cec5SDimitry Andric     ExtFedByLoad->moveAfter(LI);
65160b57cec5SDimitry Andric     ++NumExtsMoved;
65170b57cec5SDimitry Andric     Inst = ExtFedByLoad;
65180b57cec5SDimitry Andric     return true;
65190b57cec5SDimitry Andric   }
65200b57cec5SDimitry Andric 
65210b57cec5SDimitry Andric   // Continue promoting SExts if known as considerable depending on targets.
65220b57cec5SDimitry Andric   if (ATPConsiderable &&
65230b57cec5SDimitry Andric       performAddressTypePromotion(Inst, AllowPromotionWithoutCommonHeader,
65240b57cec5SDimitry Andric                                   HasPromoted, TPT, SpeculativelyMovedExts))
65250b57cec5SDimitry Andric     return true;
65260b57cec5SDimitry Andric 
65270b57cec5SDimitry Andric   TPT.rollback(LastKnownGood);
65280b57cec5SDimitry Andric   return false;
65290b57cec5SDimitry Andric }
65300b57cec5SDimitry Andric 
65310b57cec5SDimitry Andric // Perform address type promotion if doing so is profitable.
65320b57cec5SDimitry Andric // If AllowPromotionWithoutCommonHeader == false, we should find other sext
65330b57cec5SDimitry Andric // instructions that sign extended the same initial value. However, if
65340b57cec5SDimitry Andric // AllowPromotionWithoutCommonHeader == true, we expect promoting the
65350b57cec5SDimitry Andric // extension is just profitable.
performAddressTypePromotion(Instruction * & Inst,bool AllowPromotionWithoutCommonHeader,bool HasPromoted,TypePromotionTransaction & TPT,SmallVectorImpl<Instruction * > & SpeculativelyMovedExts)65360b57cec5SDimitry Andric bool CodeGenPrepare::performAddressTypePromotion(
65370b57cec5SDimitry Andric     Instruction *&Inst, bool AllowPromotionWithoutCommonHeader,
65380b57cec5SDimitry Andric     bool HasPromoted, TypePromotionTransaction &TPT,
65390b57cec5SDimitry Andric     SmallVectorImpl<Instruction *> &SpeculativelyMovedExts) {
65400b57cec5SDimitry Andric   bool Promoted = false;
65410b57cec5SDimitry Andric   SmallPtrSet<Instruction *, 1> UnhandledExts;
65420b57cec5SDimitry Andric   bool AllSeenFirst = true;
65435ffd83dbSDimitry Andric   for (auto *I : SpeculativelyMovedExts) {
65440b57cec5SDimitry Andric     Value *HeadOfChain = I->getOperand(0);
65450b57cec5SDimitry Andric     DenseMap<Value *, Instruction *>::iterator AlreadySeen =
65460b57cec5SDimitry Andric         SeenChainsForSExt.find(HeadOfChain);
65470b57cec5SDimitry Andric     // If there is an unhandled SExt which has the same header, try to promote
65480b57cec5SDimitry Andric     // it as well.
65490b57cec5SDimitry Andric     if (AlreadySeen != SeenChainsForSExt.end()) {
65500b57cec5SDimitry Andric       if (AlreadySeen->second != nullptr)
65510b57cec5SDimitry Andric         UnhandledExts.insert(AlreadySeen->second);
65520b57cec5SDimitry Andric       AllSeenFirst = false;
65530b57cec5SDimitry Andric     }
65540b57cec5SDimitry Andric   }
65550b57cec5SDimitry Andric 
65560b57cec5SDimitry Andric   if (!AllSeenFirst || (AllowPromotionWithoutCommonHeader &&
65570b57cec5SDimitry Andric                         SpeculativelyMovedExts.size() == 1)) {
65580b57cec5SDimitry Andric     TPT.commit();
65590b57cec5SDimitry Andric     if (HasPromoted)
65600b57cec5SDimitry Andric       Promoted = true;
65615ffd83dbSDimitry Andric     for (auto *I : SpeculativelyMovedExts) {
65620b57cec5SDimitry Andric       Value *HeadOfChain = I->getOperand(0);
65630b57cec5SDimitry Andric       SeenChainsForSExt[HeadOfChain] = nullptr;
65640b57cec5SDimitry Andric       ValToSExtendedUses[HeadOfChain].push_back(I);
65650b57cec5SDimitry Andric     }
65660b57cec5SDimitry Andric     // Update Inst as promotion happen.
65670b57cec5SDimitry Andric     Inst = SpeculativelyMovedExts.pop_back_val();
65680b57cec5SDimitry Andric   } else {
65690b57cec5SDimitry Andric     // This is the first chain visited from the header, keep the current chain
65700b57cec5SDimitry Andric     // as unhandled. Defer to promote this until we encounter another SExt
65710b57cec5SDimitry Andric     // chain derived from the same header.
65725ffd83dbSDimitry Andric     for (auto *I : SpeculativelyMovedExts) {
65730b57cec5SDimitry Andric       Value *HeadOfChain = I->getOperand(0);
65740b57cec5SDimitry Andric       SeenChainsForSExt[HeadOfChain] = Inst;
65750b57cec5SDimitry Andric     }
65760b57cec5SDimitry Andric     return false;
65770b57cec5SDimitry Andric   }
65780b57cec5SDimitry Andric 
65790b57cec5SDimitry Andric   if (!AllSeenFirst && !UnhandledExts.empty())
65805ffd83dbSDimitry Andric     for (auto *VisitedSExt : UnhandledExts) {
65810b57cec5SDimitry Andric       if (RemovedInsts.count(VisitedSExt))
65820b57cec5SDimitry Andric         continue;
65830b57cec5SDimitry Andric       TypePromotionTransaction TPT(RemovedInsts);
65840b57cec5SDimitry Andric       SmallVector<Instruction *, 1> Exts;
65850b57cec5SDimitry Andric       SmallVector<Instruction *, 2> Chains;
65860b57cec5SDimitry Andric       Exts.push_back(VisitedSExt);
65870b57cec5SDimitry Andric       bool HasPromoted = tryToPromoteExts(TPT, Exts, Chains);
65880b57cec5SDimitry Andric       TPT.commit();
65890b57cec5SDimitry Andric       if (HasPromoted)
65900b57cec5SDimitry Andric         Promoted = true;
65915ffd83dbSDimitry Andric       for (auto *I : Chains) {
65920b57cec5SDimitry Andric         Value *HeadOfChain = I->getOperand(0);
65930b57cec5SDimitry Andric         // Mark this as handled.
65940b57cec5SDimitry Andric         SeenChainsForSExt[HeadOfChain] = nullptr;
65950b57cec5SDimitry Andric         ValToSExtendedUses[HeadOfChain].push_back(I);
65960b57cec5SDimitry Andric       }
65970b57cec5SDimitry Andric     }
65980b57cec5SDimitry Andric   return Promoted;
65990b57cec5SDimitry Andric }
66000b57cec5SDimitry Andric 
optimizeExtUses(Instruction * I)66010b57cec5SDimitry Andric bool CodeGenPrepare::optimizeExtUses(Instruction *I) {
66020b57cec5SDimitry Andric   BasicBlock *DefBB = I->getParent();
66030b57cec5SDimitry Andric 
66040b57cec5SDimitry Andric   // If the result of a {s|z}ext and its source are both live out, rewrite all
66050b57cec5SDimitry Andric   // other uses of the source with result of extension.
66060b57cec5SDimitry Andric   Value *Src = I->getOperand(0);
66070b57cec5SDimitry Andric   if (Src->hasOneUse())
66080b57cec5SDimitry Andric     return false;
66090b57cec5SDimitry Andric 
66100b57cec5SDimitry Andric   // Only do this xform if truncating is free.
66115ffd83dbSDimitry Andric   if (!TLI->isTruncateFree(I->getType(), Src->getType()))
66120b57cec5SDimitry Andric     return false;
66130b57cec5SDimitry Andric 
66140b57cec5SDimitry Andric   // Only safe to perform the optimization if the source is also defined in
66150b57cec5SDimitry Andric   // this block.
66160b57cec5SDimitry Andric   if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
66170b57cec5SDimitry Andric     return false;
66180b57cec5SDimitry Andric 
66190b57cec5SDimitry Andric   bool DefIsLiveOut = false;
66200b57cec5SDimitry Andric   for (User *U : I->users()) {
66210b57cec5SDimitry Andric     Instruction *UI = cast<Instruction>(U);
66220b57cec5SDimitry Andric 
66230b57cec5SDimitry Andric     // Figure out which BB this ext is used in.
66240b57cec5SDimitry Andric     BasicBlock *UserBB = UI->getParent();
6625bdd1243dSDimitry Andric     if (UserBB == DefBB)
6626bdd1243dSDimitry Andric       continue;
66270b57cec5SDimitry Andric     DefIsLiveOut = true;
66280b57cec5SDimitry Andric     break;
66290b57cec5SDimitry Andric   }
66300b57cec5SDimitry Andric   if (!DefIsLiveOut)
66310b57cec5SDimitry Andric     return false;
66320b57cec5SDimitry Andric 
66330b57cec5SDimitry Andric   // Make sure none of the uses are PHI nodes.
66340b57cec5SDimitry Andric   for (User *U : Src->users()) {
66350b57cec5SDimitry Andric     Instruction *UI = cast<Instruction>(U);
66360b57cec5SDimitry Andric     BasicBlock *UserBB = UI->getParent();
6637bdd1243dSDimitry Andric     if (UserBB == DefBB)
6638bdd1243dSDimitry Andric       continue;
66390b57cec5SDimitry Andric     // Be conservative. We don't want this xform to end up introducing
66400b57cec5SDimitry Andric     // reloads just before load / store instructions.
66410b57cec5SDimitry Andric     if (isa<PHINode>(UI) || isa<LoadInst>(UI) || isa<StoreInst>(UI))
66420b57cec5SDimitry Andric       return false;
66430b57cec5SDimitry Andric   }
66440b57cec5SDimitry Andric 
66450b57cec5SDimitry Andric   // InsertedTruncs - Only insert one trunc in each block once.
66460b57cec5SDimitry Andric   DenseMap<BasicBlock *, Instruction *> InsertedTruncs;
66470b57cec5SDimitry Andric 
66480b57cec5SDimitry Andric   bool MadeChange = false;
66490b57cec5SDimitry Andric   for (Use &U : Src->uses()) {
66500b57cec5SDimitry Andric     Instruction *User = cast<Instruction>(U.getUser());
66510b57cec5SDimitry Andric 
66520b57cec5SDimitry Andric     // Figure out which BB this ext is used in.
66530b57cec5SDimitry Andric     BasicBlock *UserBB = User->getParent();
6654bdd1243dSDimitry Andric     if (UserBB == DefBB)
6655bdd1243dSDimitry Andric       continue;
66560b57cec5SDimitry Andric 
66570b57cec5SDimitry Andric     // Both src and def are live in this block. Rewrite the use.
66580b57cec5SDimitry Andric     Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
66590b57cec5SDimitry Andric 
66600b57cec5SDimitry Andric     if (!InsertedTrunc) {
66610b57cec5SDimitry Andric       BasicBlock::iterator InsertPt = UserBB->getFirstInsertionPt();
66620b57cec5SDimitry Andric       assert(InsertPt != UserBB->end());
6663c9157d92SDimitry Andric       InsertedTrunc = new TruncInst(I, Src->getType(), "");
6664c9157d92SDimitry Andric       InsertedTrunc->insertBefore(*UserBB, InsertPt);
66650b57cec5SDimitry Andric       InsertedInsts.insert(InsertedTrunc);
66660b57cec5SDimitry Andric     }
66670b57cec5SDimitry Andric 
66680b57cec5SDimitry Andric     // Replace a use of the {s|z}ext source with a use of the result.
66690b57cec5SDimitry Andric     U = InsertedTrunc;
66700b57cec5SDimitry Andric     ++NumExtUses;
66710b57cec5SDimitry Andric     MadeChange = true;
66720b57cec5SDimitry Andric   }
66730b57cec5SDimitry Andric 
66740b57cec5SDimitry Andric   return MadeChange;
66750b57cec5SDimitry Andric }
66760b57cec5SDimitry Andric 
66770b57cec5SDimitry Andric // Find loads whose uses only use some of the loaded value's bits.  Add an "and"
66780b57cec5SDimitry Andric // just after the load if the target can fold this into one extload instruction,
66790b57cec5SDimitry Andric // with the hope of eliminating some of the other later "and" instructions using
66800b57cec5SDimitry Andric // the loaded value.  "and"s that are made trivially redundant by the insertion
66810b57cec5SDimitry Andric // of the new "and" are removed by this function, while others (e.g. those whose
66820b57cec5SDimitry Andric // path from the load goes through a phi) are left for isel to potentially
66830b57cec5SDimitry Andric // remove.
66840b57cec5SDimitry Andric //
66850b57cec5SDimitry Andric // For example:
66860b57cec5SDimitry Andric //
66870b57cec5SDimitry Andric // b0:
66880b57cec5SDimitry Andric //   x = load i32
66890b57cec5SDimitry Andric //   ...
66900b57cec5SDimitry Andric // b1:
66910b57cec5SDimitry Andric //   y = and x, 0xff
66920b57cec5SDimitry Andric //   z = use y
66930b57cec5SDimitry Andric //
66940b57cec5SDimitry Andric // becomes:
66950b57cec5SDimitry Andric //
66960b57cec5SDimitry Andric // b0:
66970b57cec5SDimitry Andric //   x = load i32
66980b57cec5SDimitry Andric //   x' = and x, 0xff
66990b57cec5SDimitry Andric //   ...
67000b57cec5SDimitry Andric // b1:
67010b57cec5SDimitry Andric //   z = use x'
67020b57cec5SDimitry Andric //
67030b57cec5SDimitry Andric // whereas:
67040b57cec5SDimitry Andric //
67050b57cec5SDimitry Andric // b0:
67060b57cec5SDimitry Andric //   x1 = load i32
67070b57cec5SDimitry Andric //   ...
67080b57cec5SDimitry Andric // b1:
67090b57cec5SDimitry Andric //   x2 = load i32
67100b57cec5SDimitry Andric //   ...
67110b57cec5SDimitry Andric // b2:
67120b57cec5SDimitry Andric //   x = phi x1, x2
67130b57cec5SDimitry Andric //   y = and x, 0xff
67140b57cec5SDimitry Andric //
67150b57cec5SDimitry Andric // becomes (after a call to optimizeLoadExt for each load):
67160b57cec5SDimitry Andric //
67170b57cec5SDimitry Andric // b0:
67180b57cec5SDimitry Andric //   x1 = load i32
67190b57cec5SDimitry Andric //   x1' = and x1, 0xff
67200b57cec5SDimitry Andric //   ...
67210b57cec5SDimitry Andric // b1:
67220b57cec5SDimitry Andric //   x2 = load i32
67230b57cec5SDimitry Andric //   x2' = and x2, 0xff
67240b57cec5SDimitry Andric //   ...
67250b57cec5SDimitry Andric // b2:
67260b57cec5SDimitry Andric //   x = phi x1', x2'
67270b57cec5SDimitry Andric //   y = and x, 0xff
optimizeLoadExt(LoadInst * Load)67280b57cec5SDimitry Andric bool CodeGenPrepare::optimizeLoadExt(LoadInst *Load) {
67290b57cec5SDimitry Andric   if (!Load->isSimple() || !Load->getType()->isIntOrPtrTy())
67300b57cec5SDimitry Andric     return false;
67310b57cec5SDimitry Andric 
67320b57cec5SDimitry Andric   // Skip loads we've already transformed.
67330b57cec5SDimitry Andric   if (Load->hasOneUse() &&
67340b57cec5SDimitry Andric       InsertedInsts.count(cast<Instruction>(*Load->user_begin())))
67350b57cec5SDimitry Andric     return false;
67360b57cec5SDimitry Andric 
67370b57cec5SDimitry Andric   // Look at all uses of Load, looking through phis, to determine how many bits
67380b57cec5SDimitry Andric   // of the loaded value are needed.
67390b57cec5SDimitry Andric   SmallVector<Instruction *, 8> WorkList;
67400b57cec5SDimitry Andric   SmallPtrSet<Instruction *, 16> Visited;
67410b57cec5SDimitry Andric   SmallVector<Instruction *, 8> AndsToMaybeRemove;
67420b57cec5SDimitry Andric   for (auto *U : Load->users())
67430b57cec5SDimitry Andric     WorkList.push_back(cast<Instruction>(U));
67440b57cec5SDimitry Andric 
67450b57cec5SDimitry Andric   EVT LoadResultVT = TLI->getValueType(*DL, Load->getType());
67460b57cec5SDimitry Andric   unsigned BitWidth = LoadResultVT.getSizeInBits();
6747fe6060f1SDimitry Andric   // If the BitWidth is 0, do not try to optimize the type
6748fe6060f1SDimitry Andric   if (BitWidth == 0)
6749fe6060f1SDimitry Andric     return false;
6750fe6060f1SDimitry Andric 
67510b57cec5SDimitry Andric   APInt DemandBits(BitWidth, 0);
67520b57cec5SDimitry Andric   APInt WidestAndBits(BitWidth, 0);
67530b57cec5SDimitry Andric 
67540b57cec5SDimitry Andric   while (!WorkList.empty()) {
6755349cc55cSDimitry Andric     Instruction *I = WorkList.pop_back_val();
67560b57cec5SDimitry Andric 
67570b57cec5SDimitry Andric     // Break use-def graph loops.
67580b57cec5SDimitry Andric     if (!Visited.insert(I).second)
67590b57cec5SDimitry Andric       continue;
67600b57cec5SDimitry Andric 
67610b57cec5SDimitry Andric     // For a PHI node, push all of its users.
67620b57cec5SDimitry Andric     if (auto *Phi = dyn_cast<PHINode>(I)) {
67630b57cec5SDimitry Andric       for (auto *U : Phi->users())
67640b57cec5SDimitry Andric         WorkList.push_back(cast<Instruction>(U));
67650b57cec5SDimitry Andric       continue;
67660b57cec5SDimitry Andric     }
67670b57cec5SDimitry Andric 
67680b57cec5SDimitry Andric     switch (I->getOpcode()) {
67690b57cec5SDimitry Andric     case Instruction::And: {
67700b57cec5SDimitry Andric       auto *AndC = dyn_cast<ConstantInt>(I->getOperand(1));
67710b57cec5SDimitry Andric       if (!AndC)
67720b57cec5SDimitry Andric         return false;
67730b57cec5SDimitry Andric       APInt AndBits = AndC->getValue();
67740b57cec5SDimitry Andric       DemandBits |= AndBits;
67750b57cec5SDimitry Andric       // Keep track of the widest and mask we see.
67760b57cec5SDimitry Andric       if (AndBits.ugt(WidestAndBits))
67770b57cec5SDimitry Andric         WidestAndBits = AndBits;
67780b57cec5SDimitry Andric       if (AndBits == WidestAndBits && I->getOperand(0) == Load)
67790b57cec5SDimitry Andric         AndsToMaybeRemove.push_back(I);
67800b57cec5SDimitry Andric       break;
67810b57cec5SDimitry Andric     }
67820b57cec5SDimitry Andric 
67830b57cec5SDimitry Andric     case Instruction::Shl: {
67840b57cec5SDimitry Andric       auto *ShlC = dyn_cast<ConstantInt>(I->getOperand(1));
67850b57cec5SDimitry Andric       if (!ShlC)
67860b57cec5SDimitry Andric         return false;
67870b57cec5SDimitry Andric       uint64_t ShiftAmt = ShlC->getLimitedValue(BitWidth - 1);
67880b57cec5SDimitry Andric       DemandBits.setLowBits(BitWidth - ShiftAmt);
67890b57cec5SDimitry Andric       break;
67900b57cec5SDimitry Andric     }
67910b57cec5SDimitry Andric 
67920b57cec5SDimitry Andric     case Instruction::Trunc: {
67930b57cec5SDimitry Andric       EVT TruncVT = TLI->getValueType(*DL, I->getType());
67940b57cec5SDimitry Andric       unsigned TruncBitWidth = TruncVT.getSizeInBits();
67950b57cec5SDimitry Andric       DemandBits.setLowBits(TruncBitWidth);
67960b57cec5SDimitry Andric       break;
67970b57cec5SDimitry Andric     }
67980b57cec5SDimitry Andric 
67990b57cec5SDimitry Andric     default:
68000b57cec5SDimitry Andric       return false;
68010b57cec5SDimitry Andric     }
68020b57cec5SDimitry Andric   }
68030b57cec5SDimitry Andric 
68040b57cec5SDimitry Andric   uint32_t ActiveBits = DemandBits.getActiveBits();
68050b57cec5SDimitry Andric   // Avoid hoisting (and (load x) 1) since it is unlikely to be folded by the
68060b57cec5SDimitry Andric   // target even if isLoadExtLegal says an i1 EXTLOAD is valid.  For example,
68070b57cec5SDimitry Andric   // for the AArch64 target isLoadExtLegal(ZEXTLOAD, i32, i1) returns true, but
68080b57cec5SDimitry Andric   // (and (load x) 1) is not matched as a single instruction, rather as a LDR
68090b57cec5SDimitry Andric   // followed by an AND.
68100b57cec5SDimitry Andric   // TODO: Look into removing this restriction by fixing backends to either
68110b57cec5SDimitry Andric   // return false for isLoadExtLegal for i1 or have them select this pattern to
68120b57cec5SDimitry Andric   // a single instruction.
68130b57cec5SDimitry Andric   //
68140b57cec5SDimitry Andric   // Also avoid hoisting if we didn't see any ands with the exact DemandBits
68150b57cec5SDimitry Andric   // mask, since these are the only ands that will be removed by isel.
68160b57cec5SDimitry Andric   if (ActiveBits <= 1 || !DemandBits.isMask(ActiveBits) ||
68170b57cec5SDimitry Andric       WidestAndBits != DemandBits)
68180b57cec5SDimitry Andric     return false;
68190b57cec5SDimitry Andric 
68200b57cec5SDimitry Andric   LLVMContext &Ctx = Load->getType()->getContext();
68210b57cec5SDimitry Andric   Type *TruncTy = Type::getIntNTy(Ctx, ActiveBits);
68220b57cec5SDimitry Andric   EVT TruncVT = TLI->getValueType(*DL, TruncTy);
68230b57cec5SDimitry Andric 
68240b57cec5SDimitry Andric   // Reject cases that won't be matched as extloads.
68250b57cec5SDimitry Andric   if (!LoadResultVT.bitsGT(TruncVT) || !TruncVT.isRound() ||
68260b57cec5SDimitry Andric       !TLI->isLoadExtLegal(ISD::ZEXTLOAD, LoadResultVT, TruncVT))
68270b57cec5SDimitry Andric     return false;
68280b57cec5SDimitry Andric 
6829c9157d92SDimitry Andric   IRBuilder<> Builder(Load->getNextNonDebugInstruction());
68308bcb0991SDimitry Andric   auto *NewAnd = cast<Instruction>(
68310b57cec5SDimitry Andric       Builder.CreateAnd(Load, ConstantInt::get(Ctx, DemandBits)));
68320b57cec5SDimitry Andric   // Mark this instruction as "inserted by CGP", so that other
68330b57cec5SDimitry Andric   // optimizations don't touch it.
68340b57cec5SDimitry Andric   InsertedInsts.insert(NewAnd);
68350b57cec5SDimitry Andric 
68360b57cec5SDimitry Andric   // Replace all uses of load with new and (except for the use of load in the
68370b57cec5SDimitry Andric   // new and itself).
6838bdd1243dSDimitry Andric   replaceAllUsesWith(Load, NewAnd, FreshBBs, IsHugeFunc);
68390b57cec5SDimitry Andric   NewAnd->setOperand(0, Load);
68400b57cec5SDimitry Andric 
68410b57cec5SDimitry Andric   // Remove any and instructions that are now redundant.
68420b57cec5SDimitry Andric   for (auto *And : AndsToMaybeRemove)
68430b57cec5SDimitry Andric     // Check that the and mask is the same as the one we decided to put on the
68440b57cec5SDimitry Andric     // new and.
68450b57cec5SDimitry Andric     if (cast<ConstantInt>(And->getOperand(1))->getValue() == DemandBits) {
6846bdd1243dSDimitry Andric       replaceAllUsesWith(And, NewAnd, FreshBBs, IsHugeFunc);
68470b57cec5SDimitry Andric       if (&*CurInstIterator == And)
68480b57cec5SDimitry Andric         CurInstIterator = std::next(And->getIterator());
68490b57cec5SDimitry Andric       And->eraseFromParent();
68500b57cec5SDimitry Andric       ++NumAndUses;
68510b57cec5SDimitry Andric     }
68520b57cec5SDimitry Andric 
68530b57cec5SDimitry Andric   ++NumAndsAdded;
68540b57cec5SDimitry Andric   return true;
68550b57cec5SDimitry Andric }
68560b57cec5SDimitry Andric 
68570b57cec5SDimitry Andric /// Check if V (an operand of a select instruction) is an expensive instruction
68580b57cec5SDimitry Andric /// that is only used once.
sinkSelectOperand(const TargetTransformInfo * TTI,Value * V)68590b57cec5SDimitry Andric static bool sinkSelectOperand(const TargetTransformInfo *TTI, Value *V) {
68600b57cec5SDimitry Andric   auto *I = dyn_cast<Instruction>(V);
68610b57cec5SDimitry Andric   // If it's safe to speculatively execute, then it should not have side
68620b57cec5SDimitry Andric   // effects; therefore, it's safe to sink and possibly *not* execute.
68630b57cec5SDimitry Andric   return I && I->hasOneUse() && isSafeToSpeculativelyExecute(I) &&
6864bdd1243dSDimitry Andric          TTI->isExpensiveToSpeculativelyExecute(I);
68650b57cec5SDimitry Andric }
68660b57cec5SDimitry Andric 
68670b57cec5SDimitry Andric /// Returns true if a SelectInst should be turned into an explicit branch.
isFormingBranchFromSelectProfitable(const TargetTransformInfo * TTI,const TargetLowering * TLI,SelectInst * SI)68680b57cec5SDimitry Andric static bool isFormingBranchFromSelectProfitable(const TargetTransformInfo *TTI,
68690b57cec5SDimitry Andric                                                 const TargetLowering *TLI,
68700b57cec5SDimitry Andric                                                 SelectInst *SI) {
68710b57cec5SDimitry Andric   // If even a predictable select is cheap, then a branch can't be cheaper.
68720b57cec5SDimitry Andric   if (!TLI->isPredictableSelectExpensive())
68730b57cec5SDimitry Andric     return false;
68740b57cec5SDimitry Andric 
68750b57cec5SDimitry Andric   // FIXME: This should use the same heuristics as IfConversion to determine
68760b57cec5SDimitry Andric   // whether a select is better represented as a branch.
68770b57cec5SDimitry Andric 
68780b57cec5SDimitry Andric   // If metadata tells us that the select condition is obviously predictable,
68790b57cec5SDimitry Andric   // then we want to replace the select with a branch.
68800b57cec5SDimitry Andric   uint64_t TrueWeight, FalseWeight;
6881bdd1243dSDimitry Andric   if (extractBranchWeights(*SI, TrueWeight, FalseWeight)) {
68820b57cec5SDimitry Andric     uint64_t Max = std::max(TrueWeight, FalseWeight);
68830b57cec5SDimitry Andric     uint64_t Sum = TrueWeight + FalseWeight;
68840b57cec5SDimitry Andric     if (Sum != 0) {
68850b57cec5SDimitry Andric       auto Probability = BranchProbability::getBranchProbability(Max, Sum);
6886fe6060f1SDimitry Andric       if (Probability > TTI->getPredictableBranchThreshold())
68870b57cec5SDimitry Andric         return true;
68880b57cec5SDimitry Andric     }
68890b57cec5SDimitry Andric   }
68900b57cec5SDimitry Andric 
68910b57cec5SDimitry Andric   CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
68920b57cec5SDimitry Andric 
68930b57cec5SDimitry Andric   // If a branch is predictable, an out-of-order CPU can avoid blocking on its
68940b57cec5SDimitry Andric   // comparison condition. If the compare has more than one use, there's
68950b57cec5SDimitry Andric   // probably another cmov or setcc around, so it's not worth emitting a branch.
68960b57cec5SDimitry Andric   if (!Cmp || !Cmp->hasOneUse())
68970b57cec5SDimitry Andric     return false;
68980b57cec5SDimitry Andric 
68990b57cec5SDimitry Andric   // If either operand of the select is expensive and only needed on one side
69000b57cec5SDimitry Andric   // of the select, we should form a branch.
69010b57cec5SDimitry Andric   if (sinkSelectOperand(TTI, SI->getTrueValue()) ||
69020b57cec5SDimitry Andric       sinkSelectOperand(TTI, SI->getFalseValue()))
69030b57cec5SDimitry Andric     return true;
69040b57cec5SDimitry Andric 
69050b57cec5SDimitry Andric   return false;
69060b57cec5SDimitry Andric }
69070b57cec5SDimitry Andric 
69080b57cec5SDimitry Andric /// If \p isTrue is true, return the true value of \p SI, otherwise return
69090b57cec5SDimitry Andric /// false value of \p SI. If the true/false value of \p SI is defined by any
69100b57cec5SDimitry Andric /// select instructions in \p Selects, look through the defining select
69110b57cec5SDimitry Andric /// instruction until the true/false value is not defined in \p Selects.
6912bdd1243dSDimitry Andric static Value *
getTrueOrFalseValue(SelectInst * SI,bool isTrue,const SmallPtrSet<const Instruction *,2> & Selects)6913bdd1243dSDimitry Andric getTrueOrFalseValue(SelectInst *SI, bool isTrue,
69140b57cec5SDimitry Andric                     const SmallPtrSet<const Instruction *, 2> &Selects) {
69150b57cec5SDimitry Andric   Value *V = nullptr;
69160b57cec5SDimitry Andric 
69170b57cec5SDimitry Andric   for (SelectInst *DefSI = SI; DefSI != nullptr && Selects.count(DefSI);
69180b57cec5SDimitry Andric        DefSI = dyn_cast<SelectInst>(V)) {
69190b57cec5SDimitry Andric     assert(DefSI->getCondition() == SI->getCondition() &&
69200b57cec5SDimitry Andric            "The condition of DefSI does not match with SI");
69210b57cec5SDimitry Andric     V = (isTrue ? DefSI->getTrueValue() : DefSI->getFalseValue());
69220b57cec5SDimitry Andric   }
69230b57cec5SDimitry Andric 
69240b57cec5SDimitry Andric   assert(V && "Failed to get select true/false value");
69250b57cec5SDimitry Andric   return V;
69260b57cec5SDimitry Andric }
69270b57cec5SDimitry Andric 
optimizeShiftInst(BinaryOperator * Shift)69280b57cec5SDimitry Andric bool CodeGenPrepare::optimizeShiftInst(BinaryOperator *Shift) {
69290b57cec5SDimitry Andric   assert(Shift->isShift() && "Expected a shift");
69300b57cec5SDimitry Andric 
69310b57cec5SDimitry Andric   // If this is (1) a vector shift, (2) shifts by scalars are cheaper than
69320b57cec5SDimitry Andric   // general vector shifts, and (3) the shift amount is a select-of-splatted
69330b57cec5SDimitry Andric   // values, hoist the shifts before the select:
69340b57cec5SDimitry Andric   //   shift Op0, (select Cond, TVal, FVal) -->
69350b57cec5SDimitry Andric   //   select Cond, (shift Op0, TVal), (shift Op0, FVal)
69360b57cec5SDimitry Andric   //
69370b57cec5SDimitry Andric   // This is inverting a generic IR transform when we know that the cost of a
69380b57cec5SDimitry Andric   // general vector shift is more than the cost of 2 shift-by-scalars.
69390b57cec5SDimitry Andric   // We can't do this effectively in SDAG because we may not be able to
69400b57cec5SDimitry Andric   // determine if the select operands are splats from within a basic block.
69410b57cec5SDimitry Andric   Type *Ty = Shift->getType();
69420b57cec5SDimitry Andric   if (!Ty->isVectorTy() || !TLI->isVectorShiftByScalarCheap(Ty))
69430b57cec5SDimitry Andric     return false;
69440b57cec5SDimitry Andric   Value *Cond, *TVal, *FVal;
69450b57cec5SDimitry Andric   if (!match(Shift->getOperand(1),
69460b57cec5SDimitry Andric              m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))))
69470b57cec5SDimitry Andric     return false;
69480b57cec5SDimitry Andric   if (!isSplatValue(TVal) || !isSplatValue(FVal))
69490b57cec5SDimitry Andric     return false;
69500b57cec5SDimitry Andric 
69510b57cec5SDimitry Andric   IRBuilder<> Builder(Shift);
69520b57cec5SDimitry Andric   BinaryOperator::BinaryOps Opcode = Shift->getOpcode();
69530b57cec5SDimitry Andric   Value *NewTVal = Builder.CreateBinOp(Opcode, Shift->getOperand(0), TVal);
69540b57cec5SDimitry Andric   Value *NewFVal = Builder.CreateBinOp(Opcode, Shift->getOperand(0), FVal);
69550b57cec5SDimitry Andric   Value *NewSel = Builder.CreateSelect(Cond, NewTVal, NewFVal);
6956bdd1243dSDimitry Andric   replaceAllUsesWith(Shift, NewSel, FreshBBs, IsHugeFunc);
69570b57cec5SDimitry Andric   Shift->eraseFromParent();
69580b57cec5SDimitry Andric   return true;
69590b57cec5SDimitry Andric }
69600b57cec5SDimitry Andric 
optimizeFunnelShift(IntrinsicInst * Fsh)69615ffd83dbSDimitry Andric bool CodeGenPrepare::optimizeFunnelShift(IntrinsicInst *Fsh) {
69625ffd83dbSDimitry Andric   Intrinsic::ID Opcode = Fsh->getIntrinsicID();
69635ffd83dbSDimitry Andric   assert((Opcode == Intrinsic::fshl || Opcode == Intrinsic::fshr) &&
69645ffd83dbSDimitry Andric          "Expected a funnel shift");
69655ffd83dbSDimitry Andric 
69665ffd83dbSDimitry Andric   // If this is (1) a vector funnel shift, (2) shifts by scalars are cheaper
69675ffd83dbSDimitry Andric   // than general vector shifts, and (3) the shift amount is select-of-splatted
69685ffd83dbSDimitry Andric   // values, hoist the funnel shifts before the select:
69695ffd83dbSDimitry Andric   //   fsh Op0, Op1, (select Cond, TVal, FVal) -->
69705ffd83dbSDimitry Andric   //   select Cond, (fsh Op0, Op1, TVal), (fsh Op0, Op1, FVal)
69715ffd83dbSDimitry Andric   //
69725ffd83dbSDimitry Andric   // This is inverting a generic IR transform when we know that the cost of a
69735ffd83dbSDimitry Andric   // general vector shift is more than the cost of 2 shift-by-scalars.
69745ffd83dbSDimitry Andric   // We can't do this effectively in SDAG because we may not be able to
69755ffd83dbSDimitry Andric   // determine if the select operands are splats from within a basic block.
69765ffd83dbSDimitry Andric   Type *Ty = Fsh->getType();
69775ffd83dbSDimitry Andric   if (!Ty->isVectorTy() || !TLI->isVectorShiftByScalarCheap(Ty))
69785ffd83dbSDimitry Andric     return false;
69795ffd83dbSDimitry Andric   Value *Cond, *TVal, *FVal;
69805ffd83dbSDimitry Andric   if (!match(Fsh->getOperand(2),
69815ffd83dbSDimitry Andric              m_OneUse(m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))))
69825ffd83dbSDimitry Andric     return false;
69835ffd83dbSDimitry Andric   if (!isSplatValue(TVal) || !isSplatValue(FVal))
69845ffd83dbSDimitry Andric     return false;
69855ffd83dbSDimitry Andric 
69865ffd83dbSDimitry Andric   IRBuilder<> Builder(Fsh);
69875ffd83dbSDimitry Andric   Value *X = Fsh->getOperand(0), *Y = Fsh->getOperand(1);
69885ffd83dbSDimitry Andric   Value *NewTVal = Builder.CreateIntrinsic(Opcode, Ty, {X, Y, TVal});
69895ffd83dbSDimitry Andric   Value *NewFVal = Builder.CreateIntrinsic(Opcode, Ty, {X, Y, FVal});
69905ffd83dbSDimitry Andric   Value *NewSel = Builder.CreateSelect(Cond, NewTVal, NewFVal);
6991bdd1243dSDimitry Andric   replaceAllUsesWith(Fsh, NewSel, FreshBBs, IsHugeFunc);
69925ffd83dbSDimitry Andric   Fsh->eraseFromParent();
69935ffd83dbSDimitry Andric   return true;
69945ffd83dbSDimitry Andric }
69955ffd83dbSDimitry Andric 
69960b57cec5SDimitry Andric /// If we have a SelectInst that will likely profit from branch prediction,
69970b57cec5SDimitry Andric /// turn it into a branch.
optimizeSelectInst(SelectInst * SI)69980b57cec5SDimitry Andric bool CodeGenPrepare::optimizeSelectInst(SelectInst *SI) {
6999e8d8bef9SDimitry Andric   if (DisableSelectToBranch)
70000b57cec5SDimitry Andric     return false;
70010b57cec5SDimitry Andric 
7002bdd1243dSDimitry Andric   // If the SelectOptimize pass is enabled, selects have already been optimized.
7003bdd1243dSDimitry Andric   if (!getCGPassBuilderOption().DisableSelectOptimize)
7004bdd1243dSDimitry Andric     return false;
7005bdd1243dSDimitry Andric 
70060b57cec5SDimitry Andric   // Find all consecutive select instructions that share the same condition.
70070b57cec5SDimitry Andric   SmallVector<SelectInst *, 2> ASI;
70080b57cec5SDimitry Andric   ASI.push_back(SI);
70090b57cec5SDimitry Andric   for (BasicBlock::iterator It = ++BasicBlock::iterator(SI);
70100b57cec5SDimitry Andric        It != SI->getParent()->end(); ++It) {
70110b57cec5SDimitry Andric     SelectInst *I = dyn_cast<SelectInst>(&*It);
70120b57cec5SDimitry Andric     if (I && SI->getCondition() == I->getCondition()) {
70130b57cec5SDimitry Andric       ASI.push_back(I);
70140b57cec5SDimitry Andric     } else {
70150b57cec5SDimitry Andric       break;
70160b57cec5SDimitry Andric     }
70170b57cec5SDimitry Andric   }
70180b57cec5SDimitry Andric 
70190b57cec5SDimitry Andric   SelectInst *LastSI = ASI.back();
70200b57cec5SDimitry Andric   // Increment the current iterator to skip all the rest of select instructions
70210b57cec5SDimitry Andric   // because they will be either "not lowered" or "all lowered" to branch.
70220b57cec5SDimitry Andric   CurInstIterator = std::next(LastSI->getIterator());
7023c9157d92SDimitry Andric   // Examine debug-info attached to the consecutive select instructions. They
7024c9157d92SDimitry Andric   // won't be individually optimised by optimizeInst, so we need to perform
7025c9157d92SDimitry Andric   // DPValue maintenence here instead.
7026c9157d92SDimitry Andric   for (SelectInst *SI : ArrayRef(ASI).drop_front())
7027c9157d92SDimitry Andric     fixupDPValuesOnInst(*SI);
70280b57cec5SDimitry Andric 
70290b57cec5SDimitry Andric   bool VectorCond = !SI->getCondition()->getType()->isIntegerTy(1);
70300b57cec5SDimitry Andric 
70310b57cec5SDimitry Andric   // Can we convert the 'select' to CF ?
70320b57cec5SDimitry Andric   if (VectorCond || SI->getMetadata(LLVMContext::MD_unpredictable))
70330b57cec5SDimitry Andric     return false;
70340b57cec5SDimitry Andric 
70350b57cec5SDimitry Andric   TargetLowering::SelectSupportKind SelectKind;
7036fe013be4SDimitry Andric   if (SI->getType()->isVectorTy())
70370b57cec5SDimitry Andric     SelectKind = TargetLowering::ScalarCondVectorVal;
70380b57cec5SDimitry Andric   else
70390b57cec5SDimitry Andric     SelectKind = TargetLowering::ScalarValSelect;
70400b57cec5SDimitry Andric 
70410b57cec5SDimitry Andric   if (TLI->isSelectSupported(SelectKind) &&
7042e8d8bef9SDimitry Andric       (!isFormingBranchFromSelectProfitable(TTI, TLI, SI) || OptSize ||
7043e8d8bef9SDimitry Andric        llvm::shouldOptimizeForSize(SI->getParent(), PSI, BFI.get())))
70440b57cec5SDimitry Andric     return false;
70450b57cec5SDimitry Andric 
70460b57cec5SDimitry Andric   // The DominatorTree needs to be rebuilt by any consumers after this
70470b57cec5SDimitry Andric   // transformation. We simply reset here rather than setting the ModifiedDT
70480b57cec5SDimitry Andric   // flag to avoid restarting the function walk in runOnFunction for each
70490b57cec5SDimitry Andric   // select optimized.
70500b57cec5SDimitry Andric   DT.reset();
70510b57cec5SDimitry Andric 
70520b57cec5SDimitry Andric   // Transform a sequence like this:
70530b57cec5SDimitry Andric   //    start:
70540b57cec5SDimitry Andric   //       %cmp = cmp uge i32 %a, %b
70550b57cec5SDimitry Andric   //       %sel = select i1 %cmp, i32 %c, i32 %d
70560b57cec5SDimitry Andric   //
70570b57cec5SDimitry Andric   // Into:
70580b57cec5SDimitry Andric   //    start:
70590b57cec5SDimitry Andric   //       %cmp = cmp uge i32 %a, %b
70605ffd83dbSDimitry Andric   //       %cmp.frozen = freeze %cmp
70615ffd83dbSDimitry Andric   //       br i1 %cmp.frozen, label %select.true, label %select.false
70620b57cec5SDimitry Andric   //    select.true:
70630b57cec5SDimitry Andric   //       br label %select.end
70640b57cec5SDimitry Andric   //    select.false:
70650b57cec5SDimitry Andric   //       br label %select.end
70660b57cec5SDimitry Andric   //    select.end:
70670b57cec5SDimitry Andric   //       %sel = phi i32 [ %c, %select.true ], [ %d, %select.false ]
70680b57cec5SDimitry Andric   //
70695ffd83dbSDimitry Andric   // %cmp should be frozen, otherwise it may introduce undefined behavior.
70700b57cec5SDimitry Andric   // In addition, we may sink instructions that produce %c or %d from
70710b57cec5SDimitry Andric   // the entry block into the destination(s) of the new branch.
70720b57cec5SDimitry Andric   // If the true or false blocks do not contain a sunken instruction, that
70730b57cec5SDimitry Andric   // block and its branch may be optimized away. In that case, one side of the
70740b57cec5SDimitry Andric   // first branch will point directly to select.end, and the corresponding PHI
70750b57cec5SDimitry Andric   // predecessor block will be the start block.
70760b57cec5SDimitry Andric 
7077fe013be4SDimitry Andric   // Collect values that go on the true side and the values that go on the false
7078fe013be4SDimitry Andric   // side.
7079fe013be4SDimitry Andric   SmallVector<Instruction *> TrueInstrs, FalseInstrs;
7080fe013be4SDimitry Andric   for (SelectInst *SI : ASI) {
7081fe013be4SDimitry Andric     if (Value *V = SI->getTrueValue(); sinkSelectOperand(TTI, V))
7082fe013be4SDimitry Andric       TrueInstrs.push_back(cast<Instruction>(V));
7083fe013be4SDimitry Andric     if (Value *V = SI->getFalseValue(); sinkSelectOperand(TTI, V))
7084fe013be4SDimitry Andric       FalseInstrs.push_back(cast<Instruction>(V));
7085fe013be4SDimitry Andric   }
7086fe013be4SDimitry Andric 
7087fe013be4SDimitry Andric   // Split the select block, according to how many (if any) values go on each
7088fe013be4SDimitry Andric   // side.
70890b57cec5SDimitry Andric   BasicBlock *StartBlock = SI->getParent();
7090c9157d92SDimitry Andric   BasicBlock::iterator SplitPt = std::next(BasicBlock::iterator(LastSI));
7091c9157d92SDimitry Andric   // We should split before any debug-info.
7092c9157d92SDimitry Andric   SplitPt.setHeadBit(true);
70930b57cec5SDimitry Andric 
7094fe013be4SDimitry Andric   IRBuilder<> IB(SI);
7095fe013be4SDimitry Andric   auto *CondFr = IB.CreateFreeze(SI->getCondition(), SI->getName() + ".frozen");
70960b57cec5SDimitry Andric 
70970b57cec5SDimitry Andric   BasicBlock *TrueBlock = nullptr;
70980b57cec5SDimitry Andric   BasicBlock *FalseBlock = nullptr;
7099fe013be4SDimitry Andric   BasicBlock *EndBlock = nullptr;
71000b57cec5SDimitry Andric   BranchInst *TrueBranch = nullptr;
71010b57cec5SDimitry Andric   BranchInst *FalseBranch = nullptr;
7102fe013be4SDimitry Andric   if (TrueInstrs.size() == 0) {
7103fe013be4SDimitry Andric     FalseBranch = cast<BranchInst>(SplitBlockAndInsertIfElse(
7104c9157d92SDimitry Andric         CondFr, SplitPt, false, nullptr, nullptr, LI));
7105fe013be4SDimitry Andric     FalseBlock = FalseBranch->getParent();
7106fe013be4SDimitry Andric     EndBlock = cast<BasicBlock>(FalseBranch->getOperand(0));
7107fe013be4SDimitry Andric   } else if (FalseInstrs.size() == 0) {
7108fe013be4SDimitry Andric     TrueBranch = cast<BranchInst>(SplitBlockAndInsertIfThen(
7109c9157d92SDimitry Andric         CondFr, SplitPt, false, nullptr, nullptr, LI));
7110fe013be4SDimitry Andric     TrueBlock = TrueBranch->getParent();
7111fe013be4SDimitry Andric     EndBlock = cast<BasicBlock>(TrueBranch->getOperand(0));
7112fe013be4SDimitry Andric   } else {
7113fe013be4SDimitry Andric     Instruction *ThenTerm = nullptr;
7114fe013be4SDimitry Andric     Instruction *ElseTerm = nullptr;
7115c9157d92SDimitry Andric     SplitBlockAndInsertIfThenElse(CondFr, SplitPt, &ThenTerm, &ElseTerm,
7116fe013be4SDimitry Andric                                   nullptr, nullptr, LI);
7117fe013be4SDimitry Andric     TrueBranch = cast<BranchInst>(ThenTerm);
7118fe013be4SDimitry Andric     FalseBranch = cast<BranchInst>(ElseTerm);
7119fe013be4SDimitry Andric     TrueBlock = TrueBranch->getParent();
7120fe013be4SDimitry Andric     FalseBlock = FalseBranch->getParent();
7121fe013be4SDimitry Andric     EndBlock = cast<BasicBlock>(TrueBranch->getOperand(0));
7122fe013be4SDimitry Andric   }
7123fe013be4SDimitry Andric 
7124fe013be4SDimitry Andric   EndBlock->setName("select.end");
7125fe013be4SDimitry Andric   if (TrueBlock)
7126fe013be4SDimitry Andric     TrueBlock->setName("select.true.sink");
7127fe013be4SDimitry Andric   if (FalseBlock)
7128fe013be4SDimitry Andric     FalseBlock->setName(FalseInstrs.size() == 0 ? "select.false"
7129fe013be4SDimitry Andric                                                 : "select.false.sink");
7130fe013be4SDimitry Andric 
7131fe013be4SDimitry Andric   if (IsHugeFunc) {
7132fe013be4SDimitry Andric     if (TrueBlock)
7133fe013be4SDimitry Andric       FreshBBs.insert(TrueBlock);
7134fe013be4SDimitry Andric     if (FalseBlock)
7135fe013be4SDimitry Andric       FreshBBs.insert(FalseBlock);
7136fe013be4SDimitry Andric     FreshBBs.insert(EndBlock);
7137fe013be4SDimitry Andric   }
7138fe013be4SDimitry Andric 
7139c9157d92SDimitry Andric   BFI->setBlockFreq(EndBlock, BFI->getBlockFreq(StartBlock));
7140fe013be4SDimitry Andric 
7141fe013be4SDimitry Andric   static const unsigned MD[] = {
7142fe013be4SDimitry Andric       LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
7143fe013be4SDimitry Andric       LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
7144fe013be4SDimitry Andric   StartBlock->getTerminator()->copyMetadata(*SI, MD);
71450b57cec5SDimitry Andric 
71460b57cec5SDimitry Andric   // Sink expensive instructions into the conditional blocks to avoid executing
71470b57cec5SDimitry Andric   // them speculatively.
7148fe013be4SDimitry Andric   for (Instruction *I : TrueInstrs)
7149fe013be4SDimitry Andric     I->moveBefore(TrueBranch);
7150fe013be4SDimitry Andric   for (Instruction *I : FalseInstrs)
7151fe013be4SDimitry Andric     I->moveBefore(FalseBranch);
71520b57cec5SDimitry Andric 
71530b57cec5SDimitry Andric   // If we did not create a new block for one of the 'true' or 'false' paths
71540b57cec5SDimitry Andric   // of the condition, it means that side of the branch goes to the end block
71550b57cec5SDimitry Andric   // directly and the path originates from the start block from the point of
71560b57cec5SDimitry Andric   // view of the new PHI.
7157fe013be4SDimitry Andric   if (TrueBlock == nullptr)
71580b57cec5SDimitry Andric     TrueBlock = StartBlock;
7159fe013be4SDimitry Andric   else if (FalseBlock == nullptr)
71600b57cec5SDimitry Andric     FalseBlock = StartBlock;
71610b57cec5SDimitry Andric 
71620b57cec5SDimitry Andric   SmallPtrSet<const Instruction *, 2> INS;
71630b57cec5SDimitry Andric   INS.insert(ASI.begin(), ASI.end());
71640b57cec5SDimitry Andric   // Use reverse iterator because later select may use the value of the
71650b57cec5SDimitry Andric   // earlier select, and we need to propagate value through earlier select
71660b57cec5SDimitry Andric   // to get the PHI operand.
71670eae32dcSDimitry Andric   for (SelectInst *SI : llvm::reverse(ASI)) {
71680b57cec5SDimitry Andric     // The select itself is replaced with a PHI Node.
7169c9157d92SDimitry Andric     PHINode *PN = PHINode::Create(SI->getType(), 2, "");
7170c9157d92SDimitry Andric     PN->insertBefore(EndBlock->begin());
71710b57cec5SDimitry Andric     PN->takeName(SI);
71720b57cec5SDimitry Andric     PN->addIncoming(getTrueOrFalseValue(SI, true, INS), TrueBlock);
71730b57cec5SDimitry Andric     PN->addIncoming(getTrueOrFalseValue(SI, false, INS), FalseBlock);
71740b57cec5SDimitry Andric     PN->setDebugLoc(SI->getDebugLoc());
71750b57cec5SDimitry Andric 
7176bdd1243dSDimitry Andric     replaceAllUsesWith(SI, PN, FreshBBs, IsHugeFunc);
71770b57cec5SDimitry Andric     SI->eraseFromParent();
71780b57cec5SDimitry Andric     INS.erase(SI);
71790b57cec5SDimitry Andric     ++NumSelectsExpanded;
71800b57cec5SDimitry Andric   }
71810b57cec5SDimitry Andric 
71820b57cec5SDimitry Andric   // Instruct OptimizeBlock to skip to the next block.
71830b57cec5SDimitry Andric   CurInstIterator = StartBlock->end();
71840b57cec5SDimitry Andric   return true;
71850b57cec5SDimitry Andric }
71860b57cec5SDimitry Andric 
71875ffd83dbSDimitry Andric /// Some targets only accept certain types for splat inputs. For example a VDUP
71885ffd83dbSDimitry Andric /// in MVE takes a GPR (integer) register, and the instruction that incorporate
71895ffd83dbSDimitry Andric /// a VDUP (such as a VADD qd, qm, rm) also require a gpr register.
optimizeShuffleVectorInst(ShuffleVectorInst * SVI)71905ffd83dbSDimitry Andric bool CodeGenPrepare::optimizeShuffleVectorInst(ShuffleVectorInst *SVI) {
7191e8d8bef9SDimitry Andric   // Accept shuf(insertelem(undef/poison, val, 0), undef/poison, <0,0,..>) only
71925ffd83dbSDimitry Andric   if (!match(SVI, m_Shuffle(m_InsertElt(m_Undef(), m_Value(), m_ZeroInt()),
71935ffd83dbSDimitry Andric                             m_Undef(), m_ZeroMask())))
71940b57cec5SDimitry Andric     return false;
71955ffd83dbSDimitry Andric   Type *NewType = TLI->shouldConvertSplatType(SVI);
71965ffd83dbSDimitry Andric   if (!NewType)
71975ffd83dbSDimitry Andric     return false;
71985ffd83dbSDimitry Andric 
71995ffd83dbSDimitry Andric   auto *SVIVecType = cast<FixedVectorType>(SVI->getType());
72005ffd83dbSDimitry Andric   assert(!NewType->isVectorTy() && "Expected a scalar type!");
72015ffd83dbSDimitry Andric   assert(NewType->getScalarSizeInBits() == SVIVecType->getScalarSizeInBits() &&
72025ffd83dbSDimitry Andric          "Expected a type of the same size!");
72035ffd83dbSDimitry Andric   auto *NewVecType =
72045ffd83dbSDimitry Andric       FixedVectorType::get(NewType, SVIVecType->getNumElements());
72055ffd83dbSDimitry Andric 
72065ffd83dbSDimitry Andric   // Create a bitcast (shuffle (insert (bitcast(..))))
72075ffd83dbSDimitry Andric   IRBuilder<> Builder(SVI->getContext());
72085ffd83dbSDimitry Andric   Builder.SetInsertPoint(SVI);
72095ffd83dbSDimitry Andric   Value *BC1 = Builder.CreateBitCast(
72105ffd83dbSDimitry Andric       cast<Instruction>(SVI->getOperand(0))->getOperand(1), NewType);
7211e8d8bef9SDimitry Andric   Value *Shuffle = Builder.CreateVectorSplat(NewVecType->getNumElements(), BC1);
72125ffd83dbSDimitry Andric   Value *BC2 = Builder.CreateBitCast(Shuffle, SVIVecType);
72135ffd83dbSDimitry Andric 
7214bdd1243dSDimitry Andric   replaceAllUsesWith(SVI, BC2, FreshBBs, IsHugeFunc);
7215e8d8bef9SDimitry Andric   RecursivelyDeleteTriviallyDeadInstructions(
7216bdd1243dSDimitry Andric       SVI, TLInfo, nullptr,
7217bdd1243dSDimitry Andric       [&](Value *V) { removeAllAssertingVHReferences(V); });
72185ffd83dbSDimitry Andric 
72195ffd83dbSDimitry Andric   // Also hoist the bitcast up to its operand if it they are not in the same
72205ffd83dbSDimitry Andric   // block.
72215ffd83dbSDimitry Andric   if (auto *BCI = dyn_cast<Instruction>(BC1))
72225ffd83dbSDimitry Andric     if (auto *Op = dyn_cast<Instruction>(BCI->getOperand(0)))
72235ffd83dbSDimitry Andric       if (BCI->getParent() != Op->getParent() && !isa<PHINode>(Op) &&
72245ffd83dbSDimitry Andric           !Op->isTerminator() && !Op->isEHPad())
72255ffd83dbSDimitry Andric         BCI->moveAfter(Op);
72260b57cec5SDimitry Andric 
72270b57cec5SDimitry Andric   return true;
72280b57cec5SDimitry Andric }
72290b57cec5SDimitry Andric 
tryToSinkFreeOperands(Instruction * I)72300b57cec5SDimitry Andric bool CodeGenPrepare::tryToSinkFreeOperands(Instruction *I) {
72310b57cec5SDimitry Andric   // If the operands of I can be folded into a target instruction together with
72320b57cec5SDimitry Andric   // I, duplicate and sink them.
72330b57cec5SDimitry Andric   SmallVector<Use *, 4> OpsToSink;
72345ffd83dbSDimitry Andric   if (!TLI->shouldSinkOperands(I, OpsToSink))
72350b57cec5SDimitry Andric     return false;
72360b57cec5SDimitry Andric 
72370b57cec5SDimitry Andric   // OpsToSink can contain multiple uses in a use chain (e.g.
72380b57cec5SDimitry Andric   // (%u1 with %u1 = shufflevector), (%u2 with %u2 = zext %u1)). The dominating
72398bcb0991SDimitry Andric   // uses must come first, so we process the ops in reverse order so as to not
72408bcb0991SDimitry Andric   // create invalid IR.
72410b57cec5SDimitry Andric   BasicBlock *TargetBB = I->getParent();
72420b57cec5SDimitry Andric   bool Changed = false;
72430b57cec5SDimitry Andric   SmallVector<Use *, 4> ToReplace;
7244349cc55cSDimitry Andric   Instruction *InsertPoint = I;
7245349cc55cSDimitry Andric   DenseMap<const Instruction *, unsigned long> InstOrdering;
7246349cc55cSDimitry Andric   unsigned long InstNumber = 0;
7247349cc55cSDimitry Andric   for (const auto &I : *TargetBB)
7248349cc55cSDimitry Andric     InstOrdering[&I] = InstNumber++;
7249349cc55cSDimitry Andric 
72508bcb0991SDimitry Andric   for (Use *U : reverse(OpsToSink)) {
72510b57cec5SDimitry Andric     auto *UI = cast<Instruction>(U->get());
7252349cc55cSDimitry Andric     if (isa<PHINode>(UI))
72530b57cec5SDimitry Andric       continue;
7254349cc55cSDimitry Andric     if (UI->getParent() == TargetBB) {
7255349cc55cSDimitry Andric       if (InstOrdering[UI] < InstOrdering[InsertPoint])
7256349cc55cSDimitry Andric         InsertPoint = UI;
7257349cc55cSDimitry Andric       continue;
7258349cc55cSDimitry Andric     }
72590b57cec5SDimitry Andric     ToReplace.push_back(U);
72600b57cec5SDimitry Andric   }
72610b57cec5SDimitry Andric 
72628bcb0991SDimitry Andric   SetVector<Instruction *> MaybeDead;
72638bcb0991SDimitry Andric   DenseMap<Instruction *, Instruction *> NewInstructions;
72640b57cec5SDimitry Andric   for (Use *U : ToReplace) {
72650b57cec5SDimitry Andric     auto *UI = cast<Instruction>(U->get());
72660b57cec5SDimitry Andric     Instruction *NI = UI->clone();
7267bdd1243dSDimitry Andric 
7268bdd1243dSDimitry Andric     if (IsHugeFunc) {
7269bdd1243dSDimitry Andric       // Now we clone an instruction, its operands' defs may sink to this BB
7270fe013be4SDimitry Andric       // now. So we put the operands defs' BBs into FreshBBs to do optimization.
7271bdd1243dSDimitry Andric       for (unsigned I = 0; I < NI->getNumOperands(); ++I) {
7272bdd1243dSDimitry Andric         auto *OpDef = dyn_cast<Instruction>(NI->getOperand(I));
7273bdd1243dSDimitry Andric         if (!OpDef)
7274bdd1243dSDimitry Andric           continue;
7275bdd1243dSDimitry Andric         FreshBBs.insert(OpDef->getParent());
7276bdd1243dSDimitry Andric       }
7277bdd1243dSDimitry Andric     }
7278bdd1243dSDimitry Andric 
72798bcb0991SDimitry Andric     NewInstructions[UI] = NI;
72800b57cec5SDimitry Andric     MaybeDead.insert(UI);
72810b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Sinking " << *UI << " to user " << *I << "\n");
72828bcb0991SDimitry Andric     NI->insertBefore(InsertPoint);
72838bcb0991SDimitry Andric     InsertPoint = NI;
72840b57cec5SDimitry Andric     InsertedInsts.insert(NI);
72858bcb0991SDimitry Andric 
72868bcb0991SDimitry Andric     // Update the use for the new instruction, making sure that we update the
72878bcb0991SDimitry Andric     // sunk instruction uses, if it is part of a chain that has already been
72888bcb0991SDimitry Andric     // sunk.
72898bcb0991SDimitry Andric     Instruction *OldI = cast<Instruction>(U->getUser());
72908bcb0991SDimitry Andric     if (NewInstructions.count(OldI))
72918bcb0991SDimitry Andric       NewInstructions[OldI]->setOperand(U->getOperandNo(), NI);
72928bcb0991SDimitry Andric     else
72930b57cec5SDimitry Andric       U->set(NI);
72940b57cec5SDimitry Andric     Changed = true;
72950b57cec5SDimitry Andric   }
72960b57cec5SDimitry Andric 
72970b57cec5SDimitry Andric   // Remove instructions that are dead after sinking.
72988bcb0991SDimitry Andric   for (auto *I : MaybeDead) {
72998bcb0991SDimitry Andric     if (!I->hasNUsesOrMore(1)) {
73008bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Removing dead instruction: " << *I << "\n");
73010b57cec5SDimitry Andric       I->eraseFromParent();
73028bcb0991SDimitry Andric     }
73038bcb0991SDimitry Andric   }
73040b57cec5SDimitry Andric 
73050b57cec5SDimitry Andric   return Changed;
73060b57cec5SDimitry Andric }
73070b57cec5SDimitry Andric 
optimizeSwitchType(SwitchInst * SI)730881ad6265SDimitry Andric bool CodeGenPrepare::optimizeSwitchType(SwitchInst *SI) {
73090b57cec5SDimitry Andric   Value *Cond = SI->getCondition();
73100b57cec5SDimitry Andric   Type *OldType = Cond->getType();
73110b57cec5SDimitry Andric   LLVMContext &Context = Cond->getContext();
7312fe6060f1SDimitry Andric   EVT OldVT = TLI->getValueType(*DL, OldType);
731381ad6265SDimitry Andric   MVT RegType = TLI->getPreferredSwitchConditionType(Context, OldVT);
73140b57cec5SDimitry Andric   unsigned RegWidth = RegType.getSizeInBits();
73150b57cec5SDimitry Andric 
73160b57cec5SDimitry Andric   if (RegWidth <= cast<IntegerType>(OldType)->getBitWidth())
73170b57cec5SDimitry Andric     return false;
73180b57cec5SDimitry Andric 
73190b57cec5SDimitry Andric   // If the register width is greater than the type width, expand the condition
73200b57cec5SDimitry Andric   // of the switch instruction and each case constant to the width of the
73210b57cec5SDimitry Andric   // register. By widening the type of the switch condition, subsequent
73220b57cec5SDimitry Andric   // comparisons (for case comparisons) will not need to be extended to the
73230b57cec5SDimitry Andric   // preferred register width, so we will potentially eliminate N-1 extends,
73240b57cec5SDimitry Andric   // where N is the number of cases in the switch.
73250b57cec5SDimitry Andric   auto *NewType = Type::getIntNTy(Context, RegWidth);
73260b57cec5SDimitry Andric 
7327fe6060f1SDimitry Andric   // Extend the switch condition and case constants using the target preferred
7328fe6060f1SDimitry Andric   // extend unless the switch condition is a function argument with an extend
7329fe6060f1SDimitry Andric   // attribute. In that case, we can avoid an unnecessary mask/extension by
7330fe6060f1SDimitry Andric   // matching the argument extension instead.
73310b57cec5SDimitry Andric   Instruction::CastOps ExtType = Instruction::ZExt;
7332fe6060f1SDimitry Andric   // Some targets prefer SExt over ZExt.
7333fe6060f1SDimitry Andric   if (TLI->isSExtCheaperThanZExt(OldVT, RegType))
7334fe6060f1SDimitry Andric     ExtType = Instruction::SExt;
7335fe6060f1SDimitry Andric 
7336fe6060f1SDimitry Andric   if (auto *Arg = dyn_cast<Argument>(Cond)) {
73370b57cec5SDimitry Andric     if (Arg->hasSExtAttr())
73380b57cec5SDimitry Andric       ExtType = Instruction::SExt;
7339fe6060f1SDimitry Andric     if (Arg->hasZExtAttr())
7340fe6060f1SDimitry Andric       ExtType = Instruction::ZExt;
7341fe6060f1SDimitry Andric   }
73420b57cec5SDimitry Andric 
73430b57cec5SDimitry Andric   auto *ExtInst = CastInst::Create(ExtType, Cond, NewType);
73440b57cec5SDimitry Andric   ExtInst->insertBefore(SI);
73450b57cec5SDimitry Andric   ExtInst->setDebugLoc(SI->getDebugLoc());
73460b57cec5SDimitry Andric   SI->setCondition(ExtInst);
73470b57cec5SDimitry Andric   for (auto Case : SI->cases()) {
734881ad6265SDimitry Andric     const APInt &NarrowConst = Case.getCaseValue()->getValue();
7349bdd1243dSDimitry Andric     APInt WideConst = (ExtType == Instruction::ZExt)
7350bdd1243dSDimitry Andric                           ? NarrowConst.zext(RegWidth)
7351bdd1243dSDimitry Andric                           : NarrowConst.sext(RegWidth);
73520b57cec5SDimitry Andric     Case.setValue(ConstantInt::get(Context, WideConst));
73530b57cec5SDimitry Andric   }
73540b57cec5SDimitry Andric 
73550b57cec5SDimitry Andric   return true;
73560b57cec5SDimitry Andric }
73570b57cec5SDimitry Andric 
optimizeSwitchPhiConstants(SwitchInst * SI)735881ad6265SDimitry Andric bool CodeGenPrepare::optimizeSwitchPhiConstants(SwitchInst *SI) {
735981ad6265SDimitry Andric   // The SCCP optimization tends to produce code like this:
736081ad6265SDimitry Andric   //   switch(x) { case 42: phi(42, ...) }
736181ad6265SDimitry Andric   // Materializing the constant for the phi-argument needs instructions; So we
736281ad6265SDimitry Andric   // change the code to:
736381ad6265SDimitry Andric   //   switch(x) { case 42: phi(x, ...) }
736481ad6265SDimitry Andric 
736581ad6265SDimitry Andric   Value *Condition = SI->getCondition();
736681ad6265SDimitry Andric   // Avoid endless loop in degenerate case.
736781ad6265SDimitry Andric   if (isa<ConstantInt>(*Condition))
736881ad6265SDimitry Andric     return false;
736981ad6265SDimitry Andric 
737081ad6265SDimitry Andric   bool Changed = false;
737181ad6265SDimitry Andric   BasicBlock *SwitchBB = SI->getParent();
737281ad6265SDimitry Andric   Type *ConditionType = Condition->getType();
737381ad6265SDimitry Andric 
737481ad6265SDimitry Andric   for (const SwitchInst::CaseHandle &Case : SI->cases()) {
737581ad6265SDimitry Andric     ConstantInt *CaseValue = Case.getCaseValue();
737681ad6265SDimitry Andric     BasicBlock *CaseBB = Case.getCaseSuccessor();
737781ad6265SDimitry Andric     // Set to true if we previously checked that `CaseBB` is only reached by
737881ad6265SDimitry Andric     // a single case from this switch.
737981ad6265SDimitry Andric     bool CheckedForSinglePred = false;
738081ad6265SDimitry Andric     for (PHINode &PHI : CaseBB->phis()) {
738181ad6265SDimitry Andric       Type *PHIType = PHI.getType();
738281ad6265SDimitry Andric       // If ZExt is free then we can also catch patterns like this:
738381ad6265SDimitry Andric       //   switch((i32)x) { case 42: phi((i64)42, ...); }
738481ad6265SDimitry Andric       // and replace `(i64)42` with `zext i32 %x to i64`.
738581ad6265SDimitry Andric       bool TryZExt =
738681ad6265SDimitry Andric           PHIType->isIntegerTy() &&
738781ad6265SDimitry Andric           PHIType->getIntegerBitWidth() > ConditionType->getIntegerBitWidth() &&
738881ad6265SDimitry Andric           TLI->isZExtFree(ConditionType, PHIType);
738981ad6265SDimitry Andric       if (PHIType == ConditionType || TryZExt) {
739081ad6265SDimitry Andric         // Set to true to skip this case because of multiple preds.
739181ad6265SDimitry Andric         bool SkipCase = false;
739281ad6265SDimitry Andric         Value *Replacement = nullptr;
739381ad6265SDimitry Andric         for (unsigned I = 0, E = PHI.getNumIncomingValues(); I != E; I++) {
739481ad6265SDimitry Andric           Value *PHIValue = PHI.getIncomingValue(I);
739581ad6265SDimitry Andric           if (PHIValue != CaseValue) {
739681ad6265SDimitry Andric             if (!TryZExt)
739781ad6265SDimitry Andric               continue;
739881ad6265SDimitry Andric             ConstantInt *PHIValueInt = dyn_cast<ConstantInt>(PHIValue);
739981ad6265SDimitry Andric             if (!PHIValueInt ||
740081ad6265SDimitry Andric                 PHIValueInt->getValue() !=
740181ad6265SDimitry Andric                     CaseValue->getValue().zext(PHIType->getIntegerBitWidth()))
740281ad6265SDimitry Andric               continue;
740381ad6265SDimitry Andric           }
740481ad6265SDimitry Andric           if (PHI.getIncomingBlock(I) != SwitchBB)
740581ad6265SDimitry Andric             continue;
740681ad6265SDimitry Andric           // We cannot optimize if there are multiple case labels jumping to
740781ad6265SDimitry Andric           // this block.  This check may get expensive when there are many
740881ad6265SDimitry Andric           // case labels so we test for it last.
740981ad6265SDimitry Andric           if (!CheckedForSinglePred) {
741081ad6265SDimitry Andric             CheckedForSinglePred = true;
741181ad6265SDimitry Andric             if (SI->findCaseDest(CaseBB) == nullptr) {
741281ad6265SDimitry Andric               SkipCase = true;
741381ad6265SDimitry Andric               break;
741481ad6265SDimitry Andric             }
741581ad6265SDimitry Andric           }
741681ad6265SDimitry Andric 
741781ad6265SDimitry Andric           if (Replacement == nullptr) {
741881ad6265SDimitry Andric             if (PHIValue == CaseValue) {
741981ad6265SDimitry Andric               Replacement = Condition;
742081ad6265SDimitry Andric             } else {
742181ad6265SDimitry Andric               IRBuilder<> Builder(SI);
742281ad6265SDimitry Andric               Replacement = Builder.CreateZExt(Condition, PHIType);
742381ad6265SDimitry Andric             }
742481ad6265SDimitry Andric           }
742581ad6265SDimitry Andric           PHI.setIncomingValue(I, Replacement);
742681ad6265SDimitry Andric           Changed = true;
742781ad6265SDimitry Andric         }
742881ad6265SDimitry Andric         if (SkipCase)
742981ad6265SDimitry Andric           break;
743081ad6265SDimitry Andric       }
743181ad6265SDimitry Andric     }
743281ad6265SDimitry Andric   }
743381ad6265SDimitry Andric   return Changed;
743481ad6265SDimitry Andric }
743581ad6265SDimitry Andric 
optimizeSwitchInst(SwitchInst * SI)743681ad6265SDimitry Andric bool CodeGenPrepare::optimizeSwitchInst(SwitchInst *SI) {
743781ad6265SDimitry Andric   bool Changed = optimizeSwitchType(SI);
743881ad6265SDimitry Andric   Changed |= optimizeSwitchPhiConstants(SI);
743981ad6265SDimitry Andric   return Changed;
744081ad6265SDimitry Andric }
74410b57cec5SDimitry Andric 
74420b57cec5SDimitry Andric namespace {
74430b57cec5SDimitry Andric 
74440b57cec5SDimitry Andric /// Helper class to promote a scalar operation to a vector one.
74450b57cec5SDimitry Andric /// This class is used to move downward extractelement transition.
74460b57cec5SDimitry Andric /// E.g.,
74470b57cec5SDimitry Andric /// a = vector_op <2 x i32>
74480b57cec5SDimitry Andric /// b = extractelement <2 x i32> a, i32 0
74490b57cec5SDimitry Andric /// c = scalar_op b
74500b57cec5SDimitry Andric /// store c
74510b57cec5SDimitry Andric ///
74520b57cec5SDimitry Andric /// =>
74530b57cec5SDimitry Andric /// a = vector_op <2 x i32>
74540b57cec5SDimitry Andric /// c = vector_op a (equivalent to scalar_op on the related lane)
74550b57cec5SDimitry Andric /// * d = extractelement <2 x i32> c, i32 0
74560b57cec5SDimitry Andric /// * store d
74570b57cec5SDimitry Andric /// Assuming both extractelement and store can be combine, we get rid of the
74580b57cec5SDimitry Andric /// transition.
74590b57cec5SDimitry Andric class VectorPromoteHelper {
74600b57cec5SDimitry Andric   /// DataLayout associated with the current module.
74610b57cec5SDimitry Andric   const DataLayout &DL;
74620b57cec5SDimitry Andric 
74630b57cec5SDimitry Andric   /// Used to perform some checks on the legality of vector operations.
74640b57cec5SDimitry Andric   const TargetLowering &TLI;
74650b57cec5SDimitry Andric 
74660b57cec5SDimitry Andric   /// Used to estimated the cost of the promoted chain.
74670b57cec5SDimitry Andric   const TargetTransformInfo &TTI;
74680b57cec5SDimitry Andric 
74690b57cec5SDimitry Andric   /// The transition being moved downwards.
74700b57cec5SDimitry Andric   Instruction *Transition;
74710b57cec5SDimitry Andric 
74720b57cec5SDimitry Andric   /// The sequence of instructions to be promoted.
74730b57cec5SDimitry Andric   SmallVector<Instruction *, 4> InstsToBePromoted;
74740b57cec5SDimitry Andric 
74750b57cec5SDimitry Andric   /// Cost of combining a store and an extract.
74760b57cec5SDimitry Andric   unsigned StoreExtractCombineCost;
74770b57cec5SDimitry Andric 
74780b57cec5SDimitry Andric   /// Instruction that will be combined with the transition.
74790b57cec5SDimitry Andric   Instruction *CombineInst = nullptr;
74800b57cec5SDimitry Andric 
74810b57cec5SDimitry Andric   /// The instruction that represents the current end of the transition.
74820b57cec5SDimitry Andric   /// Since we are faking the promotion until we reach the end of the chain
74830b57cec5SDimitry Andric   /// of computation, we need a way to get the current end of the transition.
getEndOfTransition() const74840b57cec5SDimitry Andric   Instruction *getEndOfTransition() const {
74850b57cec5SDimitry Andric     if (InstsToBePromoted.empty())
74860b57cec5SDimitry Andric       return Transition;
74870b57cec5SDimitry Andric     return InstsToBePromoted.back();
74880b57cec5SDimitry Andric   }
74890b57cec5SDimitry Andric 
74900b57cec5SDimitry Andric   /// Return the index of the original value in the transition.
74910b57cec5SDimitry Andric   /// E.g., for "extractelement <2 x i32> c, i32 1" the original value,
74920b57cec5SDimitry Andric   /// c, is at index 0.
getTransitionOriginalValueIdx() const74930b57cec5SDimitry Andric   unsigned getTransitionOriginalValueIdx() const {
74940b57cec5SDimitry Andric     assert(isa<ExtractElementInst>(Transition) &&
74950b57cec5SDimitry Andric            "Other kind of transitions are not supported yet");
74960b57cec5SDimitry Andric     return 0;
74970b57cec5SDimitry Andric   }
74980b57cec5SDimitry Andric 
74990b57cec5SDimitry Andric   /// Return the index of the index in the transition.
75000b57cec5SDimitry Andric   /// E.g., for "extractelement <2 x i32> c, i32 0" the index
75010b57cec5SDimitry Andric   /// is at index 1.
getTransitionIdx() const75020b57cec5SDimitry Andric   unsigned getTransitionIdx() const {
75030b57cec5SDimitry Andric     assert(isa<ExtractElementInst>(Transition) &&
75040b57cec5SDimitry Andric            "Other kind of transitions are not supported yet");
75050b57cec5SDimitry Andric     return 1;
75060b57cec5SDimitry Andric   }
75070b57cec5SDimitry Andric 
75080b57cec5SDimitry Andric   /// Get the type of the transition.
75090b57cec5SDimitry Andric   /// This is the type of the original value.
75100b57cec5SDimitry Andric   /// E.g., for "extractelement <2 x i32> c, i32 1" the type of the
75110b57cec5SDimitry Andric   /// transition is <2 x i32>.
getTransitionType() const75120b57cec5SDimitry Andric   Type *getTransitionType() const {
75130b57cec5SDimitry Andric     return Transition->getOperand(getTransitionOriginalValueIdx())->getType();
75140b57cec5SDimitry Andric   }
75150b57cec5SDimitry Andric 
75160b57cec5SDimitry Andric   /// Promote \p ToBePromoted by moving \p Def downward through.
75170b57cec5SDimitry Andric   /// I.e., we have the following sequence:
75180b57cec5SDimitry Andric   /// Def = Transition <ty1> a to <ty2>
75190b57cec5SDimitry Andric   /// b = ToBePromoted <ty2> Def, ...
75200b57cec5SDimitry Andric   /// =>
75210b57cec5SDimitry Andric   /// b = ToBePromoted <ty1> a, ...
75220b57cec5SDimitry Andric   /// Def = Transition <ty1> ToBePromoted to <ty2>
75230b57cec5SDimitry Andric   void promoteImpl(Instruction *ToBePromoted);
75240b57cec5SDimitry Andric 
75250b57cec5SDimitry Andric   /// Check whether or not it is profitable to promote all the
75260b57cec5SDimitry Andric   /// instructions enqueued to be promoted.
isProfitableToPromote()75270b57cec5SDimitry Andric   bool isProfitableToPromote() {
75280b57cec5SDimitry Andric     Value *ValIdx = Transition->getOperand(getTransitionOriginalValueIdx());
75290b57cec5SDimitry Andric     unsigned Index = isa<ConstantInt>(ValIdx)
75300b57cec5SDimitry Andric                          ? cast<ConstantInt>(ValIdx)->getZExtValue()
75310b57cec5SDimitry Andric                          : -1;
75320b57cec5SDimitry Andric     Type *PromotedType = getTransitionType();
75330b57cec5SDimitry Andric 
75340b57cec5SDimitry Andric     StoreInst *ST = cast<StoreInst>(CombineInst);
75350b57cec5SDimitry Andric     unsigned AS = ST->getPointerAddressSpace();
75360b57cec5SDimitry Andric     // Check if this store is supported.
75370b57cec5SDimitry Andric     if (!TLI.allowsMisalignedMemoryAccesses(
75380b57cec5SDimitry Andric             TLI.getValueType(DL, ST->getValueOperand()->getType()), AS,
7539fe6060f1SDimitry Andric             ST->getAlign())) {
75400b57cec5SDimitry Andric       // If this is not supported, there is no way we can combine
75410b57cec5SDimitry Andric       // the extract with the store.
75420b57cec5SDimitry Andric       return false;
75430b57cec5SDimitry Andric     }
75440b57cec5SDimitry Andric 
75450b57cec5SDimitry Andric     // The scalar chain of computation has to pay for the transition
75460b57cec5SDimitry Andric     // scalar to vector.
75470b57cec5SDimitry Andric     // The vector chain has to account for the combining cost.
75485ffd83dbSDimitry Andric     enum TargetTransformInfo::TargetCostKind CostKind =
75495ffd83dbSDimitry Andric         TargetTransformInfo::TCK_RecipThroughput;
7550bdd1243dSDimitry Andric     InstructionCost ScalarCost =
7551bdd1243dSDimitry Andric         TTI.getVectorInstrCost(*Transition, PromotedType, CostKind, Index);
7552bdd1243dSDimitry Andric     InstructionCost VectorCost = StoreExtractCombineCost;
75530b57cec5SDimitry Andric     for (const auto &Inst : InstsToBePromoted) {
75540b57cec5SDimitry Andric       // Compute the cost.
75550b57cec5SDimitry Andric       // By construction, all instructions being promoted are arithmetic ones.
75560b57cec5SDimitry Andric       // Moreover, one argument is a constant that can be viewed as a splat
75570b57cec5SDimitry Andric       // constant.
75580b57cec5SDimitry Andric       Value *Arg0 = Inst->getOperand(0);
75590b57cec5SDimitry Andric       bool IsArg0Constant = isa<UndefValue>(Arg0) || isa<ConstantInt>(Arg0) ||
75600b57cec5SDimitry Andric                             isa<ConstantFP>(Arg0);
7561bdd1243dSDimitry Andric       TargetTransformInfo::OperandValueInfo Arg0Info, Arg1Info;
7562bdd1243dSDimitry Andric       if (IsArg0Constant)
7563bdd1243dSDimitry Andric         Arg0Info.Kind = TargetTransformInfo::OK_UniformConstantValue;
7564bdd1243dSDimitry Andric       else
7565bdd1243dSDimitry Andric         Arg1Info.Kind = TargetTransformInfo::OK_UniformConstantValue;
7566bdd1243dSDimitry Andric 
75670b57cec5SDimitry Andric       ScalarCost += TTI.getArithmeticInstrCost(
7568bdd1243dSDimitry Andric           Inst->getOpcode(), Inst->getType(), CostKind, Arg0Info, Arg1Info);
75690b57cec5SDimitry Andric       VectorCost += TTI.getArithmeticInstrCost(Inst->getOpcode(), PromotedType,
7570bdd1243dSDimitry Andric                                                CostKind, Arg0Info, Arg1Info);
75710b57cec5SDimitry Andric     }
75720b57cec5SDimitry Andric     LLVM_DEBUG(
75730b57cec5SDimitry Andric         dbgs() << "Estimated cost of computation to be promoted:\nScalar: "
75740b57cec5SDimitry Andric                << ScalarCost << "\nVector: " << VectorCost << '\n');
75750b57cec5SDimitry Andric     return ScalarCost > VectorCost;
75760b57cec5SDimitry Andric   }
75770b57cec5SDimitry Andric 
75780b57cec5SDimitry Andric   /// Generate a constant vector with \p Val with the same
75790b57cec5SDimitry Andric   /// number of elements as the transition.
75800b57cec5SDimitry Andric   /// \p UseSplat defines whether or not \p Val should be replicated
75810b57cec5SDimitry Andric   /// across the whole vector.
75820b57cec5SDimitry Andric   /// In other words, if UseSplat == true, we generate <Val, Val, ..., Val>,
75830b57cec5SDimitry Andric   /// otherwise we generate a vector with as many undef as possible:
75840b57cec5SDimitry Andric   /// <undef, ..., undef, Val, undef, ..., undef> where \p Val is only
75850b57cec5SDimitry Andric   /// used at the index of the extract.
getConstantVector(Constant * Val,bool UseSplat) const75860b57cec5SDimitry Andric   Value *getConstantVector(Constant *Val, bool UseSplat) const {
75870b57cec5SDimitry Andric     unsigned ExtractIdx = std::numeric_limits<unsigned>::max();
75880b57cec5SDimitry Andric     if (!UseSplat) {
75890b57cec5SDimitry Andric       // If we cannot determine where the constant must be, we have to
75900b57cec5SDimitry Andric       // use a splat constant.
75910b57cec5SDimitry Andric       Value *ValExtractIdx = Transition->getOperand(getTransitionIdx());
75920b57cec5SDimitry Andric       if (ConstantInt *CstVal = dyn_cast<ConstantInt>(ValExtractIdx))
75930b57cec5SDimitry Andric         ExtractIdx = CstVal->getSExtValue();
75940b57cec5SDimitry Andric       else
75950b57cec5SDimitry Andric         UseSplat = true;
75960b57cec5SDimitry Andric     }
75970b57cec5SDimitry Andric 
75985ffd83dbSDimitry Andric     ElementCount EC = cast<VectorType>(getTransitionType())->getElementCount();
75990b57cec5SDimitry Andric     if (UseSplat)
76005ffd83dbSDimitry Andric       return ConstantVector::getSplat(EC, Val);
76010b57cec5SDimitry Andric 
7602e8d8bef9SDimitry Andric     if (!EC.isScalable()) {
76030b57cec5SDimitry Andric       SmallVector<Constant *, 4> ConstVec;
76040b57cec5SDimitry Andric       UndefValue *UndefVal = UndefValue::get(Val->getType());
7605e8d8bef9SDimitry Andric       for (unsigned Idx = 0; Idx != EC.getKnownMinValue(); ++Idx) {
76060b57cec5SDimitry Andric         if (Idx == ExtractIdx)
76070b57cec5SDimitry Andric           ConstVec.push_back(Val);
76080b57cec5SDimitry Andric         else
76090b57cec5SDimitry Andric           ConstVec.push_back(UndefVal);
76100b57cec5SDimitry Andric       }
76110b57cec5SDimitry Andric       return ConstantVector::get(ConstVec);
76125ffd83dbSDimitry Andric     } else
76135ffd83dbSDimitry Andric       llvm_unreachable(
76145ffd83dbSDimitry Andric           "Generate scalable vector for non-splat is unimplemented");
76150b57cec5SDimitry Andric   }
76160b57cec5SDimitry Andric 
76170b57cec5SDimitry Andric   /// Check if promoting to a vector type an operand at \p OperandIdx
76180b57cec5SDimitry Andric   /// in \p Use can trigger undefined behavior.
canCauseUndefinedBehavior(const Instruction * Use,unsigned OperandIdx)76190b57cec5SDimitry Andric   static bool canCauseUndefinedBehavior(const Instruction *Use,
76200b57cec5SDimitry Andric                                         unsigned OperandIdx) {
76210b57cec5SDimitry Andric     // This is not safe to introduce undef when the operand is on
76220b57cec5SDimitry Andric     // the right hand side of a division-like instruction.
76230b57cec5SDimitry Andric     if (OperandIdx != 1)
76240b57cec5SDimitry Andric       return false;
76250b57cec5SDimitry Andric     switch (Use->getOpcode()) {
76260b57cec5SDimitry Andric     default:
76270b57cec5SDimitry Andric       return false;
76280b57cec5SDimitry Andric     case Instruction::SDiv:
76290b57cec5SDimitry Andric     case Instruction::UDiv:
76300b57cec5SDimitry Andric     case Instruction::SRem:
76310b57cec5SDimitry Andric     case Instruction::URem:
76320b57cec5SDimitry Andric       return true;
76330b57cec5SDimitry Andric     case Instruction::FDiv:
76340b57cec5SDimitry Andric     case Instruction::FRem:
76350b57cec5SDimitry Andric       return !Use->hasNoNaNs();
76360b57cec5SDimitry Andric     }
76370b57cec5SDimitry Andric     llvm_unreachable(nullptr);
76380b57cec5SDimitry Andric   }
76390b57cec5SDimitry Andric 
76400b57cec5SDimitry Andric public:
VectorPromoteHelper(const DataLayout & DL,const TargetLowering & TLI,const TargetTransformInfo & TTI,Instruction * Transition,unsigned CombineCost)76410b57cec5SDimitry Andric   VectorPromoteHelper(const DataLayout &DL, const TargetLowering &TLI,
76420b57cec5SDimitry Andric                       const TargetTransformInfo &TTI, Instruction *Transition,
76430b57cec5SDimitry Andric                       unsigned CombineCost)
76440b57cec5SDimitry Andric       : DL(DL), TLI(TLI), TTI(TTI), Transition(Transition),
76450b57cec5SDimitry Andric         StoreExtractCombineCost(CombineCost) {
76460b57cec5SDimitry Andric     assert(Transition && "Do not know how to promote null");
76470b57cec5SDimitry Andric   }
76480b57cec5SDimitry Andric 
76490b57cec5SDimitry Andric   /// Check if we can promote \p ToBePromoted to \p Type.
canPromote(const Instruction * ToBePromoted) const76500b57cec5SDimitry Andric   bool canPromote(const Instruction *ToBePromoted) const {
76510b57cec5SDimitry Andric     // We could support CastInst too.
76520b57cec5SDimitry Andric     return isa<BinaryOperator>(ToBePromoted);
76530b57cec5SDimitry Andric   }
76540b57cec5SDimitry Andric 
76550b57cec5SDimitry Andric   /// Check if it is profitable to promote \p ToBePromoted
76560b57cec5SDimitry Andric   /// by moving downward the transition through.
shouldPromote(const Instruction * ToBePromoted) const76570b57cec5SDimitry Andric   bool shouldPromote(const Instruction *ToBePromoted) const {
76580b57cec5SDimitry Andric     // Promote only if all the operands can be statically expanded.
76590b57cec5SDimitry Andric     // Indeed, we do not want to introduce any new kind of transitions.
76600b57cec5SDimitry Andric     for (const Use &U : ToBePromoted->operands()) {
76610b57cec5SDimitry Andric       const Value *Val = U.get();
76620b57cec5SDimitry Andric       if (Val == getEndOfTransition()) {
76630b57cec5SDimitry Andric         // If the use is a division and the transition is on the rhs,
76640b57cec5SDimitry Andric         // we cannot promote the operation, otherwise we may create a
76650b57cec5SDimitry Andric         // division by zero.
76660b57cec5SDimitry Andric         if (canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()))
76670b57cec5SDimitry Andric           return false;
76680b57cec5SDimitry Andric         continue;
76690b57cec5SDimitry Andric       }
76700b57cec5SDimitry Andric       if (!isa<ConstantInt>(Val) && !isa<UndefValue>(Val) &&
76710b57cec5SDimitry Andric           !isa<ConstantFP>(Val))
76720b57cec5SDimitry Andric         return false;
76730b57cec5SDimitry Andric     }
76740b57cec5SDimitry Andric     // Check that the resulting operation is legal.
76750b57cec5SDimitry Andric     int ISDOpcode = TLI.InstructionOpcodeToISD(ToBePromoted->getOpcode());
76760b57cec5SDimitry Andric     if (!ISDOpcode)
76770b57cec5SDimitry Andric       return false;
76780b57cec5SDimitry Andric     return StressStoreExtract ||
76790b57cec5SDimitry Andric            TLI.isOperationLegalOrCustom(
76800b57cec5SDimitry Andric                ISDOpcode, TLI.getValueType(DL, getTransitionType(), true));
76810b57cec5SDimitry Andric   }
76820b57cec5SDimitry Andric 
76830b57cec5SDimitry Andric   /// Check whether or not \p Use can be combined
76840b57cec5SDimitry Andric   /// with the transition.
76850b57cec5SDimitry Andric   /// I.e., is it possible to do Use(Transition) => AnotherUse?
canCombine(const Instruction * Use)76860b57cec5SDimitry Andric   bool canCombine(const Instruction *Use) { return isa<StoreInst>(Use); }
76870b57cec5SDimitry Andric 
76880b57cec5SDimitry Andric   /// Record \p ToBePromoted as part of the chain to be promoted.
enqueueForPromotion(Instruction * ToBePromoted)76890b57cec5SDimitry Andric   void enqueueForPromotion(Instruction *ToBePromoted) {
76900b57cec5SDimitry Andric     InstsToBePromoted.push_back(ToBePromoted);
76910b57cec5SDimitry Andric   }
76920b57cec5SDimitry Andric 
76930b57cec5SDimitry Andric   /// Set the instruction that will be combined with the transition.
recordCombineInstruction(Instruction * ToBeCombined)76940b57cec5SDimitry Andric   void recordCombineInstruction(Instruction *ToBeCombined) {
76950b57cec5SDimitry Andric     assert(canCombine(ToBeCombined) && "Unsupported instruction to combine");
76960b57cec5SDimitry Andric     CombineInst = ToBeCombined;
76970b57cec5SDimitry Andric   }
76980b57cec5SDimitry Andric 
76990b57cec5SDimitry Andric   /// Promote all the instructions enqueued for promotion if it is
77000b57cec5SDimitry Andric   /// is profitable.
77010b57cec5SDimitry Andric   /// \return True if the promotion happened, false otherwise.
promote()77020b57cec5SDimitry Andric   bool promote() {
77030b57cec5SDimitry Andric     // Check if there is something to promote.
77040b57cec5SDimitry Andric     // Right now, if we do not have anything to combine with,
77050b57cec5SDimitry Andric     // we assume the promotion is not profitable.
77060b57cec5SDimitry Andric     if (InstsToBePromoted.empty() || !CombineInst)
77070b57cec5SDimitry Andric       return false;
77080b57cec5SDimitry Andric 
77090b57cec5SDimitry Andric     // Check cost.
77100b57cec5SDimitry Andric     if (!StressStoreExtract && !isProfitableToPromote())
77110b57cec5SDimitry Andric       return false;
77120b57cec5SDimitry Andric 
77130b57cec5SDimitry Andric     // Promote.
77140b57cec5SDimitry Andric     for (auto &ToBePromoted : InstsToBePromoted)
77150b57cec5SDimitry Andric       promoteImpl(ToBePromoted);
77160b57cec5SDimitry Andric     InstsToBePromoted.clear();
77170b57cec5SDimitry Andric     return true;
77180b57cec5SDimitry Andric   }
77190b57cec5SDimitry Andric };
77200b57cec5SDimitry Andric 
77210b57cec5SDimitry Andric } // end anonymous namespace
77220b57cec5SDimitry Andric 
promoteImpl(Instruction * ToBePromoted)77230b57cec5SDimitry Andric void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) {
77240b57cec5SDimitry Andric   // At this point, we know that all the operands of ToBePromoted but Def
77250b57cec5SDimitry Andric   // can be statically promoted.
77260b57cec5SDimitry Andric   // For Def, we need to use its parameter in ToBePromoted:
77270b57cec5SDimitry Andric   // b = ToBePromoted ty1 a
77280b57cec5SDimitry Andric   // Def = Transition ty1 b to ty2
77290b57cec5SDimitry Andric   // Move the transition down.
77300b57cec5SDimitry Andric   // 1. Replace all uses of the promoted operation by the transition.
77310b57cec5SDimitry Andric   // = ... b => = ... Def.
77320b57cec5SDimitry Andric   assert(ToBePromoted->getType() == Transition->getType() &&
77330b57cec5SDimitry Andric          "The type of the result of the transition does not match "
77340b57cec5SDimitry Andric          "the final type");
77350b57cec5SDimitry Andric   ToBePromoted->replaceAllUsesWith(Transition);
77360b57cec5SDimitry Andric   // 2. Update the type of the uses.
77370b57cec5SDimitry Andric   // b = ToBePromoted ty2 Def => b = ToBePromoted ty1 Def.
77380b57cec5SDimitry Andric   Type *TransitionTy = getTransitionType();
77390b57cec5SDimitry Andric   ToBePromoted->mutateType(TransitionTy);
77400b57cec5SDimitry Andric   // 3. Update all the operands of the promoted operation with promoted
77410b57cec5SDimitry Andric   // operands.
77420b57cec5SDimitry Andric   // b = ToBePromoted ty1 Def => b = ToBePromoted ty1 a.
77430b57cec5SDimitry Andric   for (Use &U : ToBePromoted->operands()) {
77440b57cec5SDimitry Andric     Value *Val = U.get();
77450b57cec5SDimitry Andric     Value *NewVal = nullptr;
77460b57cec5SDimitry Andric     if (Val == Transition)
77470b57cec5SDimitry Andric       NewVal = Transition->getOperand(getTransitionOriginalValueIdx());
77480b57cec5SDimitry Andric     else if (isa<UndefValue>(Val) || isa<ConstantInt>(Val) ||
77490b57cec5SDimitry Andric              isa<ConstantFP>(Val)) {
77500b57cec5SDimitry Andric       // Use a splat constant if it is not safe to use undef.
77510b57cec5SDimitry Andric       NewVal = getConstantVector(
77520b57cec5SDimitry Andric           cast<Constant>(Val),
77530b57cec5SDimitry Andric           isa<UndefValue>(Val) ||
77540b57cec5SDimitry Andric               canCauseUndefinedBehavior(ToBePromoted, U.getOperandNo()));
77550b57cec5SDimitry Andric     } else
77560b57cec5SDimitry Andric       llvm_unreachable("Did you modified shouldPromote and forgot to update "
77570b57cec5SDimitry Andric                        "this?");
77580b57cec5SDimitry Andric     ToBePromoted->setOperand(U.getOperandNo(), NewVal);
77590b57cec5SDimitry Andric   }
77600b57cec5SDimitry Andric   Transition->moveAfter(ToBePromoted);
77610b57cec5SDimitry Andric   Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted);
77620b57cec5SDimitry Andric }
77630b57cec5SDimitry Andric 
77640b57cec5SDimitry Andric /// Some targets can do store(extractelement) with one instruction.
77650b57cec5SDimitry Andric /// Try to push the extractelement towards the stores when the target
77660b57cec5SDimitry Andric /// has this feature and this is profitable.
optimizeExtractElementInst(Instruction * Inst)77670b57cec5SDimitry Andric bool CodeGenPrepare::optimizeExtractElementInst(Instruction *Inst) {
77680b57cec5SDimitry Andric   unsigned CombineCost = std::numeric_limits<unsigned>::max();
77695ffd83dbSDimitry Andric   if (DisableStoreExtract ||
77700b57cec5SDimitry Andric       (!StressStoreExtract &&
77710b57cec5SDimitry Andric        !TLI->canCombineStoreAndExtract(Inst->getOperand(0)->getType(),
77720b57cec5SDimitry Andric                                        Inst->getOperand(1), CombineCost)))
77730b57cec5SDimitry Andric     return false;
77740b57cec5SDimitry Andric 
77750b57cec5SDimitry Andric   // At this point we know that Inst is a vector to scalar transition.
77760b57cec5SDimitry Andric   // Try to move it down the def-use chain, until:
77770b57cec5SDimitry Andric   // - We can combine the transition with its single use
77780b57cec5SDimitry Andric   //   => we got rid of the transition.
77790b57cec5SDimitry Andric   // - We escape the current basic block
77800b57cec5SDimitry Andric   //   => we would need to check that we are moving it at a cheaper place and
77810b57cec5SDimitry Andric   //      we do not do that for now.
77820b57cec5SDimitry Andric   BasicBlock *Parent = Inst->getParent();
77830b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Found an interesting transition: " << *Inst << '\n');
77840b57cec5SDimitry Andric   VectorPromoteHelper VPH(*DL, *TLI, *TTI, Inst, CombineCost);
77850b57cec5SDimitry Andric   // If the transition has more than one use, assume this is not going to be
77860b57cec5SDimitry Andric   // beneficial.
77870b57cec5SDimitry Andric   while (Inst->hasOneUse()) {
77880b57cec5SDimitry Andric     Instruction *ToBePromoted = cast<Instruction>(*Inst->user_begin());
77890b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Use: " << *ToBePromoted << '\n');
77900b57cec5SDimitry Andric 
77910b57cec5SDimitry Andric     if (ToBePromoted->getParent() != Parent) {
77920b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Instruction to promote is in a different block ("
77930b57cec5SDimitry Andric                         << ToBePromoted->getParent()->getName()
77940b57cec5SDimitry Andric                         << ") than the transition (" << Parent->getName()
77950b57cec5SDimitry Andric                         << ").\n");
77960b57cec5SDimitry Andric       return false;
77970b57cec5SDimitry Andric     }
77980b57cec5SDimitry Andric 
77990b57cec5SDimitry Andric     if (VPH.canCombine(ToBePromoted)) {
78000b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Assume " << *Inst << '\n'
78010b57cec5SDimitry Andric                         << "will be combined with: " << *ToBePromoted << '\n');
78020b57cec5SDimitry Andric       VPH.recordCombineInstruction(ToBePromoted);
78030b57cec5SDimitry Andric       bool Changed = VPH.promote();
78040b57cec5SDimitry Andric       NumStoreExtractExposed += Changed;
78050b57cec5SDimitry Andric       return Changed;
78060b57cec5SDimitry Andric     }
78070b57cec5SDimitry Andric 
78080b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Try promoting.\n");
78090b57cec5SDimitry Andric     if (!VPH.canPromote(ToBePromoted) || !VPH.shouldPromote(ToBePromoted))
78100b57cec5SDimitry Andric       return false;
78110b57cec5SDimitry Andric 
78120b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Promoting is possible... Enqueue for promotion!\n");
78130b57cec5SDimitry Andric 
78140b57cec5SDimitry Andric     VPH.enqueueForPromotion(ToBePromoted);
78150b57cec5SDimitry Andric     Inst = ToBePromoted;
78160b57cec5SDimitry Andric   }
78170b57cec5SDimitry Andric   return false;
78180b57cec5SDimitry Andric }
78190b57cec5SDimitry Andric 
78200b57cec5SDimitry Andric /// For the instruction sequence of store below, F and I values
78210b57cec5SDimitry Andric /// are bundled together as an i64 value before being stored into memory.
78220b57cec5SDimitry Andric /// Sometimes it is more efficient to generate separate stores for F and I,
78230b57cec5SDimitry Andric /// which can remove the bitwise instructions or sink them to colder places.
78240b57cec5SDimitry Andric ///
78250b57cec5SDimitry Andric ///   (store (or (zext (bitcast F to i32) to i64),
78260b57cec5SDimitry Andric ///              (shl (zext I to i64), 32)), addr)  -->
78270b57cec5SDimitry Andric ///   (store F, addr) and (store I, addr+4)
78280b57cec5SDimitry Andric ///
78290b57cec5SDimitry Andric /// Similarly, splitting for other merged store can also be beneficial, like:
78300b57cec5SDimitry Andric /// For pair of {i32, i32}, i64 store --> two i32 stores.
78310b57cec5SDimitry Andric /// For pair of {i32, i16}, i64 store --> two i32 stores.
78320b57cec5SDimitry Andric /// For pair of {i16, i16}, i32 store --> two i16 stores.
78330b57cec5SDimitry Andric /// For pair of {i16, i8},  i32 store --> two i16 stores.
78340b57cec5SDimitry Andric /// For pair of {i8, i8},   i16 store --> two i8 stores.
78350b57cec5SDimitry Andric ///
78360b57cec5SDimitry Andric /// We allow each target to determine specifically which kind of splitting is
78370b57cec5SDimitry Andric /// supported.
78380b57cec5SDimitry Andric ///
78390b57cec5SDimitry Andric /// The store patterns are commonly seen from the simple code snippet below
78400b57cec5SDimitry Andric /// if only std::make_pair(...) is sroa transformed before inlined into hoo.
78410b57cec5SDimitry Andric ///   void goo(const std::pair<int, float> &);
78420b57cec5SDimitry Andric ///   hoo() {
78430b57cec5SDimitry Andric ///     ...
78440b57cec5SDimitry Andric ///     goo(std::make_pair(tmp, ftmp));
78450b57cec5SDimitry Andric ///     ...
78460b57cec5SDimitry Andric ///   }
78470b57cec5SDimitry Andric ///
78480b57cec5SDimitry Andric /// Although we already have similar splitting in DAG Combine, we duplicate
78490b57cec5SDimitry Andric /// it in CodeGenPrepare to catch the case in which pattern is across
78500b57cec5SDimitry Andric /// multiple BBs. The logic in DAG Combine is kept to catch case generated
78510b57cec5SDimitry Andric /// during code expansion.
splitMergedValStore(StoreInst & SI,const DataLayout & DL,const TargetLowering & TLI)78520b57cec5SDimitry Andric static bool splitMergedValStore(StoreInst &SI, const DataLayout &DL,
78530b57cec5SDimitry Andric                                 const TargetLowering &TLI) {
78540b57cec5SDimitry Andric   // Handle simple but common cases only.
78550b57cec5SDimitry Andric   Type *StoreType = SI.getValueOperand()->getType();
78565ffd83dbSDimitry Andric 
78575ffd83dbSDimitry Andric   // The code below assumes shifting a value by <number of bits>,
78585ffd83dbSDimitry Andric   // whereas scalable vectors would have to be shifted by
78595ffd83dbSDimitry Andric   // <2log(vscale) + number of bits> in order to store the
78605ffd83dbSDimitry Andric   // low/high parts. Bailing out for now.
7861fe013be4SDimitry Andric   if (StoreType->isScalableTy())
78625ffd83dbSDimitry Andric     return false;
78635ffd83dbSDimitry Andric 
78640b57cec5SDimitry Andric   if (!DL.typeSizeEqualsStoreSize(StoreType) ||
78650b57cec5SDimitry Andric       DL.getTypeSizeInBits(StoreType) == 0)
78660b57cec5SDimitry Andric     return false;
78670b57cec5SDimitry Andric 
78680b57cec5SDimitry Andric   unsigned HalfValBitSize = DL.getTypeSizeInBits(StoreType) / 2;
78690b57cec5SDimitry Andric   Type *SplitStoreType = Type::getIntNTy(SI.getContext(), HalfValBitSize);
78700b57cec5SDimitry Andric   if (!DL.typeSizeEqualsStoreSize(SplitStoreType))
78710b57cec5SDimitry Andric     return false;
78720b57cec5SDimitry Andric 
78730b57cec5SDimitry Andric   // Don't split the store if it is volatile.
78740b57cec5SDimitry Andric   if (SI.isVolatile())
78750b57cec5SDimitry Andric     return false;
78760b57cec5SDimitry Andric 
78770b57cec5SDimitry Andric   // Match the following patterns:
78780b57cec5SDimitry Andric   // (store (or (zext LValue to i64),
78790b57cec5SDimitry Andric   //            (shl (zext HValue to i64), 32)), HalfValBitSize)
78800b57cec5SDimitry Andric   //  or
78810b57cec5SDimitry Andric   // (store (or (shl (zext HValue to i64), 32)), HalfValBitSize)
78820b57cec5SDimitry Andric   //            (zext LValue to i64),
78830b57cec5SDimitry Andric   // Expect both operands of OR and the first operand of SHL have only
78840b57cec5SDimitry Andric   // one use.
78850b57cec5SDimitry Andric   Value *LValue, *HValue;
78860b57cec5SDimitry Andric   if (!match(SI.getValueOperand(),
78870b57cec5SDimitry Andric              m_c_Or(m_OneUse(m_ZExt(m_Value(LValue))),
78880b57cec5SDimitry Andric                     m_OneUse(m_Shl(m_OneUse(m_ZExt(m_Value(HValue))),
78890b57cec5SDimitry Andric                                    m_SpecificInt(HalfValBitSize))))))
78900b57cec5SDimitry Andric     return false;
78910b57cec5SDimitry Andric 
78920b57cec5SDimitry Andric   // Check LValue and HValue are int with size less or equal than 32.
78930b57cec5SDimitry Andric   if (!LValue->getType()->isIntegerTy() ||
78940b57cec5SDimitry Andric       DL.getTypeSizeInBits(LValue->getType()) > HalfValBitSize ||
78950b57cec5SDimitry Andric       !HValue->getType()->isIntegerTy() ||
78960b57cec5SDimitry Andric       DL.getTypeSizeInBits(HValue->getType()) > HalfValBitSize)
78970b57cec5SDimitry Andric     return false;
78980b57cec5SDimitry Andric 
78990b57cec5SDimitry Andric   // If LValue/HValue is a bitcast instruction, use the EVT before bitcast
79000b57cec5SDimitry Andric   // as the input of target query.
79010b57cec5SDimitry Andric   auto *LBC = dyn_cast<BitCastInst>(LValue);
79020b57cec5SDimitry Andric   auto *HBC = dyn_cast<BitCastInst>(HValue);
79030b57cec5SDimitry Andric   EVT LowTy = LBC ? EVT::getEVT(LBC->getOperand(0)->getType())
79040b57cec5SDimitry Andric                   : EVT::getEVT(LValue->getType());
79050b57cec5SDimitry Andric   EVT HighTy = HBC ? EVT::getEVT(HBC->getOperand(0)->getType())
79060b57cec5SDimitry Andric                    : EVT::getEVT(HValue->getType());
79070b57cec5SDimitry Andric   if (!ForceSplitStore && !TLI.isMultiStoresCheaperThanBitsMerge(LowTy, HighTy))
79080b57cec5SDimitry Andric     return false;
79090b57cec5SDimitry Andric 
79100b57cec5SDimitry Andric   // Start to split store.
79110b57cec5SDimitry Andric   IRBuilder<> Builder(SI.getContext());
79120b57cec5SDimitry Andric   Builder.SetInsertPoint(&SI);
79130b57cec5SDimitry Andric 
79140b57cec5SDimitry Andric   // If LValue/HValue is a bitcast in another BB, create a new one in current
79150b57cec5SDimitry Andric   // BB so it may be merged with the splitted stores by dag combiner.
79160b57cec5SDimitry Andric   if (LBC && LBC->getParent() != SI.getParent())
79170b57cec5SDimitry Andric     LValue = Builder.CreateBitCast(LBC->getOperand(0), LBC->getType());
79180b57cec5SDimitry Andric   if (HBC && HBC->getParent() != SI.getParent())
79190b57cec5SDimitry Andric     HValue = Builder.CreateBitCast(HBC->getOperand(0), HBC->getType());
79200b57cec5SDimitry Andric 
79210b57cec5SDimitry Andric   bool IsLE = SI.getModule()->getDataLayout().isLittleEndian();
79220b57cec5SDimitry Andric   auto CreateSplitStore = [&](Value *V, bool Upper) {
79230b57cec5SDimitry Andric     V = Builder.CreateZExtOrBitCast(V, SplitStoreType);
7924c9157d92SDimitry Andric     Value *Addr = SI.getPointerOperand();
79255ffd83dbSDimitry Andric     Align Alignment = SI.getAlign();
792613138422SDimitry Andric     const bool IsOffsetStore = (IsLE && Upper) || (!IsLE && !Upper);
79275ffd83dbSDimitry Andric     if (IsOffsetStore) {
79280b57cec5SDimitry Andric       Addr = Builder.CreateGEP(
79290b57cec5SDimitry Andric           SplitStoreType, Addr,
79300b57cec5SDimitry Andric           ConstantInt::get(Type::getInt32Ty(SI.getContext()), 1));
79315ffd83dbSDimitry Andric 
793213138422SDimitry Andric       // When splitting the store in half, naturally one half will retain the
793313138422SDimitry Andric       // alignment of the original wider store, regardless of whether it was
793413138422SDimitry Andric       // over-aligned or not, while the other will require adjustment.
793513138422SDimitry Andric       Alignment = commonAlignment(Alignment, HalfValBitSize / 8);
793613138422SDimitry Andric     }
79375ffd83dbSDimitry Andric     Builder.CreateAlignedStore(V, Addr, Alignment);
79380b57cec5SDimitry Andric   };
79390b57cec5SDimitry Andric 
79400b57cec5SDimitry Andric   CreateSplitStore(LValue, false);
79410b57cec5SDimitry Andric   CreateSplitStore(HValue, true);
79420b57cec5SDimitry Andric 
79430b57cec5SDimitry Andric   // Delete the old store.
79440b57cec5SDimitry Andric   SI.eraseFromParent();
79450b57cec5SDimitry Andric   return true;
79460b57cec5SDimitry Andric }
79470b57cec5SDimitry Andric 
79480b57cec5SDimitry Andric // Return true if the GEP has two operands, the first operand is of a sequential
79490b57cec5SDimitry Andric // type, and the second operand is a constant.
GEPSequentialConstIndexed(GetElementPtrInst * GEP)79500b57cec5SDimitry Andric static bool GEPSequentialConstIndexed(GetElementPtrInst *GEP) {
79510b57cec5SDimitry Andric   gep_type_iterator I = gep_type_begin(*GEP);
7952bdd1243dSDimitry Andric   return GEP->getNumOperands() == 2 && I.isSequential() &&
79530b57cec5SDimitry Andric          isa<ConstantInt>(GEP->getOperand(1));
79540b57cec5SDimitry Andric }
79550b57cec5SDimitry Andric 
79560b57cec5SDimitry Andric // Try unmerging GEPs to reduce liveness interference (register pressure) across
79570b57cec5SDimitry Andric // IndirectBr edges. Since IndirectBr edges tend to touch on many blocks,
79580b57cec5SDimitry Andric // reducing liveness interference across those edges benefits global register
79590b57cec5SDimitry Andric // allocation. Currently handles only certain cases.
79600b57cec5SDimitry Andric //
79610b57cec5SDimitry Andric // For example, unmerge %GEPI and %UGEPI as below.
79620b57cec5SDimitry Andric //
79630b57cec5SDimitry Andric // ---------- BEFORE ----------
79640b57cec5SDimitry Andric // SrcBlock:
79650b57cec5SDimitry Andric //   ...
79660b57cec5SDimitry Andric //   %GEPIOp = ...
79670b57cec5SDimitry Andric //   ...
79680b57cec5SDimitry Andric //   %GEPI = gep %GEPIOp, Idx
79690b57cec5SDimitry Andric //   ...
79700b57cec5SDimitry Andric //   indirectbr ... [ label %DstB0, label %DstB1, ... label %DstBi ... ]
79710b57cec5SDimitry Andric //   (* %GEPI is alive on the indirectbr edges due to other uses ahead)
79720b57cec5SDimitry Andric //   (* %GEPIOp is alive on the indirectbr edges only because of it's used by
79730b57cec5SDimitry Andric //   %UGEPI)
79740b57cec5SDimitry Andric //
79750b57cec5SDimitry Andric // DstB0: ... (there may be a gep similar to %UGEPI to be unmerged)
79760b57cec5SDimitry Andric // DstB1: ... (there may be a gep similar to %UGEPI to be unmerged)
79770b57cec5SDimitry Andric // ...
79780b57cec5SDimitry Andric //
79790b57cec5SDimitry Andric // DstBi:
79800b57cec5SDimitry Andric //   ...
79810b57cec5SDimitry Andric //   %UGEPI = gep %GEPIOp, UIdx
79820b57cec5SDimitry Andric // ...
79830b57cec5SDimitry Andric // ---------------------------
79840b57cec5SDimitry Andric //
79850b57cec5SDimitry Andric // ---------- AFTER ----------
79860b57cec5SDimitry Andric // SrcBlock:
79870b57cec5SDimitry Andric //   ... (same as above)
79880b57cec5SDimitry Andric //    (* %GEPI is still alive on the indirectbr edges)
79890b57cec5SDimitry Andric //    (* %GEPIOp is no longer alive on the indirectbr edges as a result of the
79900b57cec5SDimitry Andric //    unmerging)
79910b57cec5SDimitry Andric // ...
79920b57cec5SDimitry Andric //
79930b57cec5SDimitry Andric // DstBi:
79940b57cec5SDimitry Andric //   ...
79950b57cec5SDimitry Andric //   %UGEPI = gep %GEPI, (UIdx-Idx)
79960b57cec5SDimitry Andric //   ...
79970b57cec5SDimitry Andric // ---------------------------
79980b57cec5SDimitry Andric //
79990b57cec5SDimitry Andric // The register pressure on the IndirectBr edges is reduced because %GEPIOp is
80000b57cec5SDimitry Andric // no longer alive on them.
80010b57cec5SDimitry Andric //
80020b57cec5SDimitry Andric // We try to unmerge GEPs here in CodGenPrepare, as opposed to limiting merging
80030b57cec5SDimitry Andric // of GEPs in the first place in InstCombiner::visitGetElementPtrInst() so as
80040b57cec5SDimitry Andric // not to disable further simplications and optimizations as a result of GEP
80050b57cec5SDimitry Andric // merging.
80060b57cec5SDimitry Andric //
80070b57cec5SDimitry Andric // Note this unmerging may increase the length of the data flow critical path
80080b57cec5SDimitry Andric // (the path from %GEPIOp to %UGEPI would go through %GEPI), which is a tradeoff
80090b57cec5SDimitry Andric // between the register pressure and the length of data-flow critical
80100b57cec5SDimitry Andric // path. Restricting this to the uncommon IndirectBr case would minimize the
80110b57cec5SDimitry Andric // impact of potentially longer critical path, if any, and the impact on compile
80120b57cec5SDimitry Andric // time.
tryUnmergingGEPsAcrossIndirectBr(GetElementPtrInst * GEPI,const TargetTransformInfo * TTI)80130b57cec5SDimitry Andric static bool tryUnmergingGEPsAcrossIndirectBr(GetElementPtrInst *GEPI,
80140b57cec5SDimitry Andric                                              const TargetTransformInfo *TTI) {
80150b57cec5SDimitry Andric   BasicBlock *SrcBlock = GEPI->getParent();
80160b57cec5SDimitry Andric   // Check that SrcBlock ends with an IndirectBr. If not, give up. The common
80170b57cec5SDimitry Andric   // (non-IndirectBr) cases exit early here.
80180b57cec5SDimitry Andric   if (!isa<IndirectBrInst>(SrcBlock->getTerminator()))
80190b57cec5SDimitry Andric     return false;
80200b57cec5SDimitry Andric   // Check that GEPI is a simple gep with a single constant index.
80210b57cec5SDimitry Andric   if (!GEPSequentialConstIndexed(GEPI))
80220b57cec5SDimitry Andric     return false;
80230b57cec5SDimitry Andric   ConstantInt *GEPIIdx = cast<ConstantInt>(GEPI->getOperand(1));
80240b57cec5SDimitry Andric   // Check that GEPI is a cheap one.
80255ffd83dbSDimitry Andric   if (TTI->getIntImmCost(GEPIIdx->getValue(), GEPIIdx->getType(),
8026bdd1243dSDimitry Andric                          TargetTransformInfo::TCK_SizeAndLatency) >
8027bdd1243dSDimitry Andric       TargetTransformInfo::TCC_Basic)
80280b57cec5SDimitry Andric     return false;
80290b57cec5SDimitry Andric   Value *GEPIOp = GEPI->getOperand(0);
80300b57cec5SDimitry Andric   // Check that GEPIOp is an instruction that's also defined in SrcBlock.
80310b57cec5SDimitry Andric   if (!isa<Instruction>(GEPIOp))
80320b57cec5SDimitry Andric     return false;
80330b57cec5SDimitry Andric   auto *GEPIOpI = cast<Instruction>(GEPIOp);
80340b57cec5SDimitry Andric   if (GEPIOpI->getParent() != SrcBlock)
80350b57cec5SDimitry Andric     return false;
80360b57cec5SDimitry Andric   // Check that GEP is used outside the block, meaning it's alive on the
80370b57cec5SDimitry Andric   // IndirectBr edge(s).
8038bdd1243dSDimitry Andric   if (llvm::none_of(GEPI->users(), [&](User *Usr) {
80390b57cec5SDimitry Andric         if (auto *I = dyn_cast<Instruction>(Usr)) {
80400b57cec5SDimitry Andric           if (I->getParent() != SrcBlock) {
80410b57cec5SDimitry Andric             return true;
80420b57cec5SDimitry Andric           }
80430b57cec5SDimitry Andric         }
80440b57cec5SDimitry Andric         return false;
8045bdd1243dSDimitry Andric       }))
80460b57cec5SDimitry Andric     return false;
80470b57cec5SDimitry Andric   // The second elements of the GEP chains to be unmerged.
80480b57cec5SDimitry Andric   std::vector<GetElementPtrInst *> UGEPIs;
80490b57cec5SDimitry Andric   // Check each user of GEPIOp to check if unmerging would make GEPIOp not alive
80500b57cec5SDimitry Andric   // on IndirectBr edges.
80510b57cec5SDimitry Andric   for (User *Usr : GEPIOp->users()) {
8052bdd1243dSDimitry Andric     if (Usr == GEPI)
8053bdd1243dSDimitry Andric       continue;
80540b57cec5SDimitry Andric     // Check if Usr is an Instruction. If not, give up.
80550b57cec5SDimitry Andric     if (!isa<Instruction>(Usr))
80560b57cec5SDimitry Andric       return false;
80570b57cec5SDimitry Andric     auto *UI = cast<Instruction>(Usr);
80580b57cec5SDimitry Andric     // Check if Usr in the same block as GEPIOp, which is fine, skip.
80590b57cec5SDimitry Andric     if (UI->getParent() == SrcBlock)
80600b57cec5SDimitry Andric       continue;
80610b57cec5SDimitry Andric     // Check if Usr is a GEP. If not, give up.
80620b57cec5SDimitry Andric     if (!isa<GetElementPtrInst>(Usr))
80630b57cec5SDimitry Andric       return false;
80640b57cec5SDimitry Andric     auto *UGEPI = cast<GetElementPtrInst>(Usr);
80650b57cec5SDimitry Andric     // Check if UGEPI is a simple gep with a single constant index and GEPIOp is
80660b57cec5SDimitry Andric     // the pointer operand to it. If so, record it in the vector. If not, give
80670b57cec5SDimitry Andric     // up.
80680b57cec5SDimitry Andric     if (!GEPSequentialConstIndexed(UGEPI))
80690b57cec5SDimitry Andric       return false;
80700b57cec5SDimitry Andric     if (UGEPI->getOperand(0) != GEPIOp)
80710b57cec5SDimitry Andric       return false;
8072c9157d92SDimitry Andric     if (UGEPI->getSourceElementType() != GEPI->getSourceElementType())
8073c9157d92SDimitry Andric       return false;
80740b57cec5SDimitry Andric     if (GEPIIdx->getType() !=
80750b57cec5SDimitry Andric         cast<ConstantInt>(UGEPI->getOperand(1))->getType())
80760b57cec5SDimitry Andric       return false;
80770b57cec5SDimitry Andric     ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
80785ffd83dbSDimitry Andric     if (TTI->getIntImmCost(UGEPIIdx->getValue(), UGEPIIdx->getType(),
8079bdd1243dSDimitry Andric                            TargetTransformInfo::TCK_SizeAndLatency) >
8080bdd1243dSDimitry Andric         TargetTransformInfo::TCC_Basic)
80810b57cec5SDimitry Andric       return false;
80820b57cec5SDimitry Andric     UGEPIs.push_back(UGEPI);
80830b57cec5SDimitry Andric   }
80840b57cec5SDimitry Andric   if (UGEPIs.size() == 0)
80850b57cec5SDimitry Andric     return false;
80860b57cec5SDimitry Andric   // Check the materializing cost of (Uidx-Idx).
80870b57cec5SDimitry Andric   for (GetElementPtrInst *UGEPI : UGEPIs) {
80880b57cec5SDimitry Andric     ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
80890b57cec5SDimitry Andric     APInt NewIdx = UGEPIIdx->getValue() - GEPIIdx->getValue();
8090fe6060f1SDimitry Andric     InstructionCost ImmCost = TTI->getIntImmCost(
8091fe6060f1SDimitry Andric         NewIdx, GEPIIdx->getType(), TargetTransformInfo::TCK_SizeAndLatency);
80920b57cec5SDimitry Andric     if (ImmCost > TargetTransformInfo::TCC_Basic)
80930b57cec5SDimitry Andric       return false;
80940b57cec5SDimitry Andric   }
80950b57cec5SDimitry Andric   // Now unmerge between GEPI and UGEPIs.
80960b57cec5SDimitry Andric   for (GetElementPtrInst *UGEPI : UGEPIs) {
80970b57cec5SDimitry Andric     UGEPI->setOperand(0, GEPI);
80980b57cec5SDimitry Andric     ConstantInt *UGEPIIdx = cast<ConstantInt>(UGEPI->getOperand(1));
8099bdd1243dSDimitry Andric     Constant *NewUGEPIIdx = ConstantInt::get(
8100bdd1243dSDimitry Andric         GEPIIdx->getType(), UGEPIIdx->getValue() - GEPIIdx->getValue());
81010b57cec5SDimitry Andric     UGEPI->setOperand(1, NewUGEPIIdx);
81020b57cec5SDimitry Andric     // If GEPI is not inbounds but UGEPI is inbounds, change UGEPI to not
81030b57cec5SDimitry Andric     // inbounds to avoid UB.
81040b57cec5SDimitry Andric     if (!GEPI->isInBounds()) {
81050b57cec5SDimitry Andric       UGEPI->setIsInBounds(false);
81060b57cec5SDimitry Andric     }
81070b57cec5SDimitry Andric   }
81080b57cec5SDimitry Andric   // After unmerging, verify that GEPIOp is actually only used in SrcBlock (not
81090b57cec5SDimitry Andric   // alive on IndirectBr edges).
8110fcaf7f86SDimitry Andric   assert(llvm::none_of(GEPIOp->users(),
8111fcaf7f86SDimitry Andric                        [&](User *Usr) {
81120b57cec5SDimitry Andric                          return cast<Instruction>(Usr)->getParent() != SrcBlock;
8113fcaf7f86SDimitry Andric                        }) &&
8114fcaf7f86SDimitry Andric          "GEPIOp is used outside SrcBlock");
81150b57cec5SDimitry Andric   return true;
81160b57cec5SDimitry Andric }
81170b57cec5SDimitry Andric 
optimizeBranch(BranchInst * Branch,const TargetLowering & TLI,SmallSet<BasicBlock *,32> & FreshBBs,bool IsHugeFunc)8118bdd1243dSDimitry Andric static bool optimizeBranch(BranchInst *Branch, const TargetLowering &TLI,
8119bdd1243dSDimitry Andric                            SmallSet<BasicBlock *, 32> &FreshBBs,
8120bdd1243dSDimitry Andric                            bool IsHugeFunc) {
8121fe6060f1SDimitry Andric   // Try and convert
8122fe6060f1SDimitry Andric   //  %c = icmp ult %x, 8
8123fe6060f1SDimitry Andric   //  br %c, bla, blb
8124fe6060f1SDimitry Andric   //  %tc = lshr %x, 3
8125fe6060f1SDimitry Andric   // to
8126fe6060f1SDimitry Andric   //  %tc = lshr %x, 3
8127fe6060f1SDimitry Andric   //  %c = icmp eq %tc, 0
8128fe6060f1SDimitry Andric   //  br %c, bla, blb
8129fe6060f1SDimitry Andric   // Creating the cmp to zero can be better for the backend, especially if the
8130fe6060f1SDimitry Andric   // lshr produces flags that can be used automatically.
8131fe6060f1SDimitry Andric   if (!TLI.preferZeroCompareBranch() || !Branch->isConditional())
8132fe6060f1SDimitry Andric     return false;
8133fe6060f1SDimitry Andric 
8134fe6060f1SDimitry Andric   ICmpInst *Cmp = dyn_cast<ICmpInst>(Branch->getCondition());
8135fe6060f1SDimitry Andric   if (!Cmp || !isa<ConstantInt>(Cmp->getOperand(1)) || !Cmp->hasOneUse())
8136fe6060f1SDimitry Andric     return false;
8137fe6060f1SDimitry Andric 
8138fe6060f1SDimitry Andric   Value *X = Cmp->getOperand(0);
8139fe6060f1SDimitry Andric   APInt CmpC = cast<ConstantInt>(Cmp->getOperand(1))->getValue();
8140fe6060f1SDimitry Andric 
8141fe6060f1SDimitry Andric   for (auto *U : X->users()) {
8142fe6060f1SDimitry Andric     Instruction *UI = dyn_cast<Instruction>(U);
8143fe6060f1SDimitry Andric     // A quick dominance check
8144fe6060f1SDimitry Andric     if (!UI ||
8145fe6060f1SDimitry Andric         (UI->getParent() != Branch->getParent() &&
8146fe6060f1SDimitry Andric          UI->getParent() != Branch->getSuccessor(0) &&
8147fe6060f1SDimitry Andric          UI->getParent() != Branch->getSuccessor(1)) ||
8148fe6060f1SDimitry Andric         (UI->getParent() != Branch->getParent() &&
8149fe6060f1SDimitry Andric          !UI->getParent()->getSinglePredecessor()))
8150fe6060f1SDimitry Andric       continue;
8151fe6060f1SDimitry Andric 
8152fe6060f1SDimitry Andric     if (CmpC.isPowerOf2() && Cmp->getPredicate() == ICmpInst::ICMP_ULT &&
8153fe6060f1SDimitry Andric         match(UI, m_Shr(m_Specific(X), m_SpecificInt(CmpC.logBase2())))) {
8154fe6060f1SDimitry Andric       IRBuilder<> Builder(Branch);
8155fe6060f1SDimitry Andric       if (UI->getParent() != Branch->getParent())
8156fe6060f1SDimitry Andric         UI->moveBefore(Branch);
8157*fe72d8ecSDimitry Andric       UI->dropPoisonGeneratingFlags();
8158fe6060f1SDimitry Andric       Value *NewCmp = Builder.CreateCmp(ICmpInst::ICMP_EQ, UI,
8159fe6060f1SDimitry Andric                                         ConstantInt::get(UI->getType(), 0));
8160fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "Converting " << *Cmp << "\n");
8161fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << " to compare on zero: " << *NewCmp << "\n");
8162bdd1243dSDimitry Andric       replaceAllUsesWith(Cmp, NewCmp, FreshBBs, IsHugeFunc);
8163fe6060f1SDimitry Andric       return true;
8164fe6060f1SDimitry Andric     }
8165fe6060f1SDimitry Andric     if (Cmp->isEquality() &&
8166fe6060f1SDimitry Andric         (match(UI, m_Add(m_Specific(X), m_SpecificInt(-CmpC))) ||
8167fe6060f1SDimitry Andric          match(UI, m_Sub(m_Specific(X), m_SpecificInt(CmpC))))) {
8168fe6060f1SDimitry Andric       IRBuilder<> Builder(Branch);
8169fe6060f1SDimitry Andric       if (UI->getParent() != Branch->getParent())
8170fe6060f1SDimitry Andric         UI->moveBefore(Branch);
8171*fe72d8ecSDimitry Andric       UI->dropPoisonGeneratingFlags();
8172fe6060f1SDimitry Andric       Value *NewCmp = Builder.CreateCmp(Cmp->getPredicate(), UI,
8173fe6060f1SDimitry Andric                                         ConstantInt::get(UI->getType(), 0));
8174fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << "Converting " << *Cmp << "\n");
8175fe6060f1SDimitry Andric       LLVM_DEBUG(dbgs() << " to compare on zero: " << *NewCmp << "\n");
8176bdd1243dSDimitry Andric       replaceAllUsesWith(Cmp, NewCmp, FreshBBs, IsHugeFunc);
8177fe6060f1SDimitry Andric       return true;
8178fe6060f1SDimitry Andric     }
8179fe6060f1SDimitry Andric   }
8180fe6060f1SDimitry Andric   return false;
8181fe6060f1SDimitry Andric }
8182fe6060f1SDimitry Andric 
optimizeInst(Instruction * I,ModifyDT & ModifiedDT)8183bdd1243dSDimitry Andric bool CodeGenPrepare::optimizeInst(Instruction *I, ModifyDT &ModifiedDT) {
8184c9157d92SDimitry Andric   bool AnyChange = false;
8185c9157d92SDimitry Andric   AnyChange = fixupDPValuesOnInst(*I);
8186c9157d92SDimitry Andric 
81870b57cec5SDimitry Andric   // Bail out if we inserted the instruction to prevent optimizations from
81880b57cec5SDimitry Andric   // stepping on each other's toes.
81890b57cec5SDimitry Andric   if (InsertedInsts.count(I))
8190c9157d92SDimitry Andric     return AnyChange;
81910b57cec5SDimitry Andric 
81920b57cec5SDimitry Andric   // TODO: Move into the switch on opcode below here.
81930b57cec5SDimitry Andric   if (PHINode *P = dyn_cast<PHINode>(I)) {
81940b57cec5SDimitry Andric     // It is possible for very late stage optimizations (such as SimplifyCFG)
81950b57cec5SDimitry Andric     // to introduce PHI nodes too late to be cleaned up.  If we detect such a
81960b57cec5SDimitry Andric     // trivial PHI, go ahead and zap it here.
819781ad6265SDimitry Andric     if (Value *V = simplifyInstruction(P, {*DL, TLInfo})) {
81980b57cec5SDimitry Andric       LargeOffsetGEPMap.erase(P);
8199bdd1243dSDimitry Andric       replaceAllUsesWith(P, V, FreshBBs, IsHugeFunc);
82000b57cec5SDimitry Andric       P->eraseFromParent();
82010b57cec5SDimitry Andric       ++NumPHIsElim;
82020b57cec5SDimitry Andric       return true;
82030b57cec5SDimitry Andric     }
8204c9157d92SDimitry Andric     return AnyChange;
82050b57cec5SDimitry Andric   }
82060b57cec5SDimitry Andric 
82070b57cec5SDimitry Andric   if (CastInst *CI = dyn_cast<CastInst>(I)) {
82080b57cec5SDimitry Andric     // If the source of the cast is a constant, then this should have
82090b57cec5SDimitry Andric     // already been constant folded.  The only reason NOT to constant fold
82100b57cec5SDimitry Andric     // it is if something (e.g. LSR) was careful to place the constant
82110b57cec5SDimitry Andric     // evaluation in a block other than then one that uses it (e.g. to hoist
82120b57cec5SDimitry Andric     // the address of globals out of a loop).  If this is the case, we don't
82130b57cec5SDimitry Andric     // want to forward-subst the cast.
82140b57cec5SDimitry Andric     if (isa<Constant>(CI->getOperand(0)))
8215c9157d92SDimitry Andric       return AnyChange;
82160b57cec5SDimitry Andric 
82175ffd83dbSDimitry Andric     if (OptimizeNoopCopyExpression(CI, *TLI, *DL))
82180b57cec5SDimitry Andric       return true;
82190b57cec5SDimitry Andric 
8220bdd1243dSDimitry Andric     if ((isa<UIToFPInst>(I) || isa<FPToUIInst>(I) || isa<TruncInst>(I)) &&
8221fe013be4SDimitry Andric         TLI->optimizeExtendOrTruncateConversion(
8222fe013be4SDimitry Andric             I, LI->getLoopFor(I->getParent()), *TTI))
8223bdd1243dSDimitry Andric       return true;
8224bdd1243dSDimitry Andric 
82250b57cec5SDimitry Andric     if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
82260b57cec5SDimitry Andric       /// Sink a zext or sext into its user blocks if the target type doesn't
82270b57cec5SDimitry Andric       /// fit in one register
82285ffd83dbSDimitry Andric       if (TLI->getTypeAction(CI->getContext(),
82290b57cec5SDimitry Andric                              TLI->getValueType(*DL, CI->getType())) ==
82300b57cec5SDimitry Andric           TargetLowering::TypeExpandInteger) {
82310b57cec5SDimitry Andric         return SinkCast(CI);
82320b57cec5SDimitry Andric       } else {
8233bdd1243dSDimitry Andric         if (TLI->optimizeExtendOrTruncateConversion(
8234fe013be4SDimitry Andric                 I, LI->getLoopFor(I->getParent()), *TTI))
8235bdd1243dSDimitry Andric           return true;
8236bdd1243dSDimitry Andric 
82370b57cec5SDimitry Andric         bool MadeChange = optimizeExt(I);
82380b57cec5SDimitry Andric         return MadeChange | optimizeExtUses(I);
82390b57cec5SDimitry Andric       }
82400b57cec5SDimitry Andric     }
8241c9157d92SDimitry Andric     return AnyChange;
82420b57cec5SDimitry Andric   }
82430b57cec5SDimitry Andric 
82440b57cec5SDimitry Andric   if (auto *Cmp = dyn_cast<CmpInst>(I))
82455ffd83dbSDimitry Andric     if (optimizeCmp(Cmp, ModifiedDT))
82460b57cec5SDimitry Andric       return true;
82470b57cec5SDimitry Andric 
82480b57cec5SDimitry Andric   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
82490b57cec5SDimitry Andric     LI->setMetadata(LLVMContext::MD_invariant_group, nullptr);
82500b57cec5SDimitry Andric     bool Modified = optimizeLoadExt(LI);
82510b57cec5SDimitry Andric     unsigned AS = LI->getPointerAddressSpace();
82520b57cec5SDimitry Andric     Modified |= optimizeMemoryInst(I, I->getOperand(0), LI->getType(), AS);
82530b57cec5SDimitry Andric     return Modified;
82540b57cec5SDimitry Andric   }
82550b57cec5SDimitry Andric 
82560b57cec5SDimitry Andric   if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
82575ffd83dbSDimitry Andric     if (splitMergedValStore(*SI, *DL, *TLI))
82580b57cec5SDimitry Andric       return true;
82590b57cec5SDimitry Andric     SI->setMetadata(LLVMContext::MD_invariant_group, nullptr);
82600b57cec5SDimitry Andric     unsigned AS = SI->getPointerAddressSpace();
82610b57cec5SDimitry Andric     return optimizeMemoryInst(I, SI->getOperand(1),
82620b57cec5SDimitry Andric                               SI->getOperand(0)->getType(), AS);
82630b57cec5SDimitry Andric   }
82640b57cec5SDimitry Andric 
82650b57cec5SDimitry Andric   if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(I)) {
82660b57cec5SDimitry Andric     unsigned AS = RMW->getPointerAddressSpace();
8267bdd1243dSDimitry Andric     return optimizeMemoryInst(I, RMW->getPointerOperand(), RMW->getType(), AS);
82680b57cec5SDimitry Andric   }
82690b57cec5SDimitry Andric 
82700b57cec5SDimitry Andric   if (AtomicCmpXchgInst *CmpX = dyn_cast<AtomicCmpXchgInst>(I)) {
82710b57cec5SDimitry Andric     unsigned AS = CmpX->getPointerAddressSpace();
82720b57cec5SDimitry Andric     return optimizeMemoryInst(I, CmpX->getPointerOperand(),
82730b57cec5SDimitry Andric                               CmpX->getCompareOperand()->getType(), AS);
82740b57cec5SDimitry Andric   }
82750b57cec5SDimitry Andric 
82760b57cec5SDimitry Andric   BinaryOperator *BinOp = dyn_cast<BinaryOperator>(I);
82770b57cec5SDimitry Andric 
8278349cc55cSDimitry Andric   if (BinOp && BinOp->getOpcode() == Instruction::And && EnableAndCmpSinking &&
8279349cc55cSDimitry Andric       sinkAndCmp0Expression(BinOp, *TLI, InsertedInsts))
8280349cc55cSDimitry Andric     return true;
82810b57cec5SDimitry Andric 
82820b57cec5SDimitry Andric   // TODO: Move this into the switch on opcode - it handles shifts already.
82830b57cec5SDimitry Andric   if (BinOp && (BinOp->getOpcode() == Instruction::AShr ||
82840b57cec5SDimitry Andric                 BinOp->getOpcode() == Instruction::LShr)) {
82850b57cec5SDimitry Andric     ConstantInt *CI = dyn_cast<ConstantInt>(BinOp->getOperand(1));
82865ffd83dbSDimitry Andric     if (CI && TLI->hasExtractBitsInsn())
82870b57cec5SDimitry Andric       if (OptimizeExtractBits(BinOp, CI, *TLI, *DL))
82880b57cec5SDimitry Andric         return true;
82890b57cec5SDimitry Andric   }
82900b57cec5SDimitry Andric 
82910b57cec5SDimitry Andric   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
82920b57cec5SDimitry Andric     if (GEPI->hasAllZeroIndices()) {
82930b57cec5SDimitry Andric       /// The GEP operand must be a pointer, so must its result -> BitCast
82940b57cec5SDimitry Andric       Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
82950b57cec5SDimitry Andric                                         GEPI->getName(), GEPI);
82960b57cec5SDimitry Andric       NC->setDebugLoc(GEPI->getDebugLoc());
8297bdd1243dSDimitry Andric       replaceAllUsesWith(GEPI, NC, FreshBBs, IsHugeFunc);
8298fe013be4SDimitry Andric       RecursivelyDeleteTriviallyDeadInstructions(
8299fe013be4SDimitry Andric           GEPI, TLInfo, nullptr,
8300fe013be4SDimitry Andric           [&](Value *V) { removeAllAssertingVHReferences(V); });
83010b57cec5SDimitry Andric       ++NumGEPsElim;
83020b57cec5SDimitry Andric       optimizeInst(NC, ModifiedDT);
83030b57cec5SDimitry Andric       return true;
83040b57cec5SDimitry Andric     }
83050b57cec5SDimitry Andric     if (tryUnmergingGEPsAcrossIndirectBr(GEPI, TTI)) {
83060b57cec5SDimitry Andric       return true;
83070b57cec5SDimitry Andric     }
83080b57cec5SDimitry Andric   }
83090b57cec5SDimitry Andric 
83105ffd83dbSDimitry Andric   if (FreezeInst *FI = dyn_cast<FreezeInst>(I)) {
83115ffd83dbSDimitry Andric     // freeze(icmp a, const)) -> icmp (freeze a), const
83125ffd83dbSDimitry Andric     // This helps generate efficient conditional jumps.
83135ffd83dbSDimitry Andric     Instruction *CmpI = nullptr;
83145ffd83dbSDimitry Andric     if (ICmpInst *II = dyn_cast<ICmpInst>(FI->getOperand(0)))
83155ffd83dbSDimitry Andric       CmpI = II;
83165ffd83dbSDimitry Andric     else if (FCmpInst *F = dyn_cast<FCmpInst>(FI->getOperand(0)))
83175ffd83dbSDimitry Andric       CmpI = F->getFastMathFlags().none() ? F : nullptr;
83185ffd83dbSDimitry Andric 
83195ffd83dbSDimitry Andric     if (CmpI && CmpI->hasOneUse()) {
83205ffd83dbSDimitry Andric       auto Op0 = CmpI->getOperand(0), Op1 = CmpI->getOperand(1);
83215ffd83dbSDimitry Andric       bool Const0 = isa<ConstantInt>(Op0) || isa<ConstantFP>(Op0) ||
83225ffd83dbSDimitry Andric                     isa<ConstantPointerNull>(Op0);
83235ffd83dbSDimitry Andric       bool Const1 = isa<ConstantInt>(Op1) || isa<ConstantFP>(Op1) ||
83245ffd83dbSDimitry Andric                     isa<ConstantPointerNull>(Op1);
83255ffd83dbSDimitry Andric       if (Const0 || Const1) {
83265ffd83dbSDimitry Andric         if (!Const0 || !Const1) {
83275ffd83dbSDimitry Andric           auto *F = new FreezeInst(Const0 ? Op1 : Op0, "", CmpI);
83285ffd83dbSDimitry Andric           F->takeName(FI);
83295ffd83dbSDimitry Andric           CmpI->setOperand(Const0 ? 1 : 0, F);
83305ffd83dbSDimitry Andric         }
8331bdd1243dSDimitry Andric         replaceAllUsesWith(FI, CmpI, FreshBBs, IsHugeFunc);
83325ffd83dbSDimitry Andric         FI->eraseFromParent();
83335ffd83dbSDimitry Andric         return true;
83345ffd83dbSDimitry Andric       }
83355ffd83dbSDimitry Andric     }
8336c9157d92SDimitry Andric     return AnyChange;
83375ffd83dbSDimitry Andric   }
83385ffd83dbSDimitry Andric 
83390b57cec5SDimitry Andric   if (tryToSinkFreeOperands(I))
83400b57cec5SDimitry Andric     return true;
83410b57cec5SDimitry Andric 
83420b57cec5SDimitry Andric   switch (I->getOpcode()) {
83430b57cec5SDimitry Andric   case Instruction::Shl:
83440b57cec5SDimitry Andric   case Instruction::LShr:
83450b57cec5SDimitry Andric   case Instruction::AShr:
83460b57cec5SDimitry Andric     return optimizeShiftInst(cast<BinaryOperator>(I));
83470b57cec5SDimitry Andric   case Instruction::Call:
83480b57cec5SDimitry Andric     return optimizeCallInst(cast<CallInst>(I), ModifiedDT);
83490b57cec5SDimitry Andric   case Instruction::Select:
83500b57cec5SDimitry Andric     return optimizeSelectInst(cast<SelectInst>(I));
83510b57cec5SDimitry Andric   case Instruction::ShuffleVector:
83520b57cec5SDimitry Andric     return optimizeShuffleVectorInst(cast<ShuffleVectorInst>(I));
83530b57cec5SDimitry Andric   case Instruction::Switch:
83540b57cec5SDimitry Andric     return optimizeSwitchInst(cast<SwitchInst>(I));
83550b57cec5SDimitry Andric   case Instruction::ExtractElement:
83560b57cec5SDimitry Andric     return optimizeExtractElementInst(cast<ExtractElementInst>(I));
8357fe6060f1SDimitry Andric   case Instruction::Br:
8358bdd1243dSDimitry Andric     return optimizeBranch(cast<BranchInst>(I), *TLI, FreshBBs, IsHugeFunc);
83590b57cec5SDimitry Andric   }
83600b57cec5SDimitry Andric 
8361c9157d92SDimitry Andric   return AnyChange;
83620b57cec5SDimitry Andric }
83630b57cec5SDimitry Andric 
83640b57cec5SDimitry Andric /// Given an OR instruction, check to see if this is a bitreverse
83650b57cec5SDimitry Andric /// idiom. If so, insert the new intrinsic and return true.
makeBitReverse(Instruction & I)8366e8d8bef9SDimitry Andric bool CodeGenPrepare::makeBitReverse(Instruction &I) {
83670b57cec5SDimitry Andric   if (!I.getType()->isIntegerTy() ||
8368e8d8bef9SDimitry Andric       !TLI->isOperationLegalOrCustom(ISD::BITREVERSE,
8369e8d8bef9SDimitry Andric                                      TLI->getValueType(*DL, I.getType(), true)))
83700b57cec5SDimitry Andric     return false;
83710b57cec5SDimitry Andric 
83720b57cec5SDimitry Andric   SmallVector<Instruction *, 4> Insts;
83730b57cec5SDimitry Andric   if (!recognizeBSwapOrBitReverseIdiom(&I, false, true, Insts))
83740b57cec5SDimitry Andric     return false;
83750b57cec5SDimitry Andric   Instruction *LastInst = Insts.back();
8376bdd1243dSDimitry Andric   replaceAllUsesWith(&I, LastInst, FreshBBs, IsHugeFunc);
8377e8d8bef9SDimitry Andric   RecursivelyDeleteTriviallyDeadInstructions(
8378bdd1243dSDimitry Andric       &I, TLInfo, nullptr,
8379bdd1243dSDimitry Andric       [&](Value *V) { removeAllAssertingVHReferences(V); });
83800b57cec5SDimitry Andric   return true;
83810b57cec5SDimitry Andric }
83820b57cec5SDimitry Andric 
83830b57cec5SDimitry Andric // In this pass we look for GEP and cast instructions that are used
83840b57cec5SDimitry Andric // across basic blocks and rewrite them to improve basic-block-at-a-time
83850b57cec5SDimitry Andric // selection.
optimizeBlock(BasicBlock & BB,ModifyDT & ModifiedDT)8386bdd1243dSDimitry Andric bool CodeGenPrepare::optimizeBlock(BasicBlock &BB, ModifyDT &ModifiedDT) {
83870b57cec5SDimitry Andric   SunkAddrs.clear();
83880b57cec5SDimitry Andric   bool MadeChange = false;
83890b57cec5SDimitry Andric 
8390bdd1243dSDimitry Andric   do {
83910b57cec5SDimitry Andric     CurInstIterator = BB.begin();
8392bdd1243dSDimitry Andric     ModifiedDT = ModifyDT::NotModifyDT;
83930b57cec5SDimitry Andric     while (CurInstIterator != BB.end()) {
83940b57cec5SDimitry Andric       MadeChange |= optimizeInst(&*CurInstIterator++, ModifiedDT);
8395bdd1243dSDimitry Andric       if (ModifiedDT != ModifyDT::NotModifyDT) {
8396bdd1243dSDimitry Andric         // For huge function we tend to quickly go though the inner optmization
8397bdd1243dSDimitry Andric         // opportunities in the BB. So we go back to the BB head to re-optimize
8398bdd1243dSDimitry Andric         // each instruction instead of go back to the function head.
8399bdd1243dSDimitry Andric         if (IsHugeFunc) {
8400bdd1243dSDimitry Andric           DT.reset();
8401bdd1243dSDimitry Andric           getDT(*BB.getParent());
8402bdd1243dSDimitry Andric           break;
8403bdd1243dSDimitry Andric         } else {
84040b57cec5SDimitry Andric           return true;
84050b57cec5SDimitry Andric         }
8406bdd1243dSDimitry Andric       }
8407bdd1243dSDimitry Andric     }
8408bdd1243dSDimitry Andric   } while (ModifiedDT == ModifyDT::ModifyInstDT);
84090b57cec5SDimitry Andric 
84100b57cec5SDimitry Andric   bool MadeBitReverse = true;
84115ffd83dbSDimitry Andric   while (MadeBitReverse) {
84120b57cec5SDimitry Andric     MadeBitReverse = false;
84130b57cec5SDimitry Andric     for (auto &I : reverse(BB)) {
8414e8d8bef9SDimitry Andric       if (makeBitReverse(I)) {
84150b57cec5SDimitry Andric         MadeBitReverse = MadeChange = true;
84160b57cec5SDimitry Andric         break;
84170b57cec5SDimitry Andric       }
84180b57cec5SDimitry Andric     }
84190b57cec5SDimitry Andric   }
84200b57cec5SDimitry Andric   MadeChange |= dupRetToEnableTailCallOpts(&BB, ModifiedDT);
84210b57cec5SDimitry Andric 
84220b57cec5SDimitry Andric   return MadeChange;
84230b57cec5SDimitry Andric }
84240b57cec5SDimitry Andric 
8425480093f4SDimitry Andric // Some CGP optimizations may move or alter what's computed in a block. Check
8426480093f4SDimitry Andric // whether a dbg.value intrinsic could be pointed at a more appropriate operand.
fixupDbgValue(Instruction * I)8427480093f4SDimitry Andric bool CodeGenPrepare::fixupDbgValue(Instruction *I) {
8428480093f4SDimitry Andric   assert(isa<DbgValueInst>(I));
8429480093f4SDimitry Andric   DbgValueInst &DVI = *cast<DbgValueInst>(I);
8430480093f4SDimitry Andric 
8431480093f4SDimitry Andric   // Does this dbg.value refer to a sunk address calculation?
8432fe6060f1SDimitry Andric   bool AnyChange = false;
8433fe6060f1SDimitry Andric   SmallDenseSet<Value *> LocationOps(DVI.location_ops().begin(),
8434fe6060f1SDimitry Andric                                      DVI.location_ops().end());
8435fe6060f1SDimitry Andric   for (Value *Location : LocationOps) {
8436480093f4SDimitry Andric     WeakTrackingVH SunkAddrVH = SunkAddrs[Location];
8437480093f4SDimitry Andric     Value *SunkAddr = SunkAddrVH.pointsToAliveValue() ? SunkAddrVH : nullptr;
8438480093f4SDimitry Andric     if (SunkAddr) {
8439480093f4SDimitry Andric       // Point dbg.value at locally computed address, which should give the best
8440fe6060f1SDimitry Andric       // opportunity to be accurately lowered. This update may change the type
8441fe6060f1SDimitry Andric       // of pointer being referred to; however this makes no difference to
8442fe6060f1SDimitry Andric       // debugging information, and we can't generate bitcasts that may affect
8443fe6060f1SDimitry Andric       // codegen.
8444fe6060f1SDimitry Andric       DVI.replaceVariableLocationOp(Location, SunkAddr);
8445fe6060f1SDimitry Andric       AnyChange = true;
8446480093f4SDimitry Andric     }
8447fe6060f1SDimitry Andric   }
8448fe6060f1SDimitry Andric   return AnyChange;
8449480093f4SDimitry Andric }
8450480093f4SDimitry Andric 
fixupDPValuesOnInst(Instruction & I)8451c9157d92SDimitry Andric bool CodeGenPrepare::fixupDPValuesOnInst(Instruction &I) {
8452c9157d92SDimitry Andric   bool AnyChange = false;
8453c9157d92SDimitry Andric   for (DPValue &DPV : I.getDbgValueRange())
8454c9157d92SDimitry Andric     AnyChange |= fixupDPValue(DPV);
8455c9157d92SDimitry Andric   return AnyChange;
8456c9157d92SDimitry Andric }
8457c9157d92SDimitry Andric 
8458c9157d92SDimitry Andric // FIXME: should updating debug-info really cause the "changed" flag to fire,
8459c9157d92SDimitry Andric // which can cause a function to be reprocessed?
fixupDPValue(DPValue & DPV)8460c9157d92SDimitry Andric bool CodeGenPrepare::fixupDPValue(DPValue &DPV) {
8461c9157d92SDimitry Andric   if (DPV.Type != DPValue::LocationType::Value)
8462c9157d92SDimitry Andric     return false;
8463c9157d92SDimitry Andric 
8464c9157d92SDimitry Andric   // Does this DPValue refer to a sunk address calculation?
8465c9157d92SDimitry Andric   bool AnyChange = false;
8466c9157d92SDimitry Andric   SmallDenseSet<Value *> LocationOps(DPV.location_ops().begin(),
8467c9157d92SDimitry Andric                                      DPV.location_ops().end());
8468c9157d92SDimitry Andric   for (Value *Location : LocationOps) {
8469c9157d92SDimitry Andric     WeakTrackingVH SunkAddrVH = SunkAddrs[Location];
8470c9157d92SDimitry Andric     Value *SunkAddr = SunkAddrVH.pointsToAliveValue() ? SunkAddrVH : nullptr;
8471c9157d92SDimitry Andric     if (SunkAddr) {
8472c9157d92SDimitry Andric       // Point dbg.value at locally computed address, which should give the best
8473c9157d92SDimitry Andric       // opportunity to be accurately lowered. This update may change the type
8474c9157d92SDimitry Andric       // of pointer being referred to; however this makes no difference to
8475c9157d92SDimitry Andric       // debugging information, and we can't generate bitcasts that may affect
8476c9157d92SDimitry Andric       // codegen.
8477c9157d92SDimitry Andric       DPV.replaceVariableLocationOp(Location, SunkAddr);
8478c9157d92SDimitry Andric       AnyChange = true;
8479c9157d92SDimitry Andric     }
8480c9157d92SDimitry Andric   }
8481c9157d92SDimitry Andric   return AnyChange;
8482c9157d92SDimitry Andric }
8483c9157d92SDimitry Andric 
DbgInserterHelper(DbgValueInst * DVI,Instruction * VI)8484c9157d92SDimitry Andric static void DbgInserterHelper(DbgValueInst *DVI, Instruction *VI) {
8485c9157d92SDimitry Andric   DVI->removeFromParent();
8486c9157d92SDimitry Andric   if (isa<PHINode>(VI))
8487c9157d92SDimitry Andric     DVI->insertBefore(&*VI->getParent()->getFirstInsertionPt());
8488c9157d92SDimitry Andric   else
8489c9157d92SDimitry Andric     DVI->insertAfter(VI);
8490c9157d92SDimitry Andric }
8491c9157d92SDimitry Andric 
DbgInserterHelper(DPValue * DPV,Instruction * VI)8492c9157d92SDimitry Andric static void DbgInserterHelper(DPValue *DPV, Instruction *VI) {
8493c9157d92SDimitry Andric   DPV->removeFromParent();
8494c9157d92SDimitry Andric   BasicBlock *VIBB = VI->getParent();
8495c9157d92SDimitry Andric   if (isa<PHINode>(VI))
8496c9157d92SDimitry Andric     VIBB->insertDPValueBefore(DPV, VIBB->getFirstInsertionPt());
8497c9157d92SDimitry Andric   else
8498c9157d92SDimitry Andric     VIBB->insertDPValueAfter(DPV, VI);
8499c9157d92SDimitry Andric }
8500c9157d92SDimitry Andric 
8501480093f4SDimitry Andric // A llvm.dbg.value may be using a value before its definition, due to
8502480093f4SDimitry Andric // optimizations in this pass and others. Scan for such dbg.values, and rescue
8503480093f4SDimitry Andric // them by moving the dbg.value to immediately after the value definition.
8504480093f4SDimitry Andric // FIXME: Ideally this should never be necessary, and this has the potential
8505480093f4SDimitry Andric // to re-order dbg.value intrinsics.
placeDbgValues(Function & F)85060b57cec5SDimitry Andric bool CodeGenPrepare::placeDbgValues(Function &F) {
85070b57cec5SDimitry Andric   bool MadeChange = false;
8508480093f4SDimitry Andric   DominatorTree DT(F);
8509480093f4SDimitry Andric 
8510c9157d92SDimitry Andric   auto DbgProcessor = [&](auto *DbgItem, Instruction *Position) {
8511fe6060f1SDimitry Andric     SmallVector<Instruction *, 4> VIs;
8512c9157d92SDimitry Andric     for (Value *V : DbgItem->location_ops())
8513fe6060f1SDimitry Andric       if (Instruction *VI = dyn_cast_or_null<Instruction>(V))
8514fe6060f1SDimitry Andric         VIs.push_back(VI);
8515480093f4SDimitry Andric 
8516c9157d92SDimitry Andric     // This item may depend on multiple instructions, complicating any
8517fe6060f1SDimitry Andric     // potential sink. This block takes the defensive approach, opting to
8518c9157d92SDimitry Andric     // "undef" the item if it has more than one instruction and any of them do
8519c9157d92SDimitry Andric     // not dominate iem.
8520fe6060f1SDimitry Andric     for (Instruction *VI : VIs) {
8521fe6060f1SDimitry Andric       if (VI->isTerminator())
8522480093f4SDimitry Andric         continue;
8523480093f4SDimitry Andric 
85240b57cec5SDimitry Andric       // If VI is a phi in a block with an EHPad terminator, we can't insert
85250b57cec5SDimitry Andric       // after it.
85260b57cec5SDimitry Andric       if (isa<PHINode>(VI) && VI->getParent()->getTerminator()->isEHPad())
85270b57cec5SDimitry Andric         continue;
8528480093f4SDimitry Andric 
8529480093f4SDimitry Andric       // If the defining instruction dominates the dbg.value, we do not need
8530480093f4SDimitry Andric       // to move the dbg.value.
8531c9157d92SDimitry Andric       if (DT.dominates(VI, Position))
8532480093f4SDimitry Andric         continue;
8533480093f4SDimitry Andric 
8534fe6060f1SDimitry Andric       // If we depend on multiple instructions and any of them doesn't
8535fe6060f1SDimitry Andric       // dominate this DVI, we probably can't salvage it: moving it to
8536fe6060f1SDimitry Andric       // after any of the instructions could cause us to lose the others.
8537fe6060f1SDimitry Andric       if (VIs.size() > 1) {
8538fe6060f1SDimitry Andric         LLVM_DEBUG(
8539fe6060f1SDimitry Andric             dbgs()
8540fe6060f1SDimitry Andric             << "Unable to find valid location for Debug Value, undefing:\n"
8541c9157d92SDimitry Andric             << *DbgItem);
8542c9157d92SDimitry Andric         DbgItem->setKillLocation();
8543fe6060f1SDimitry Andric         break;
8544fe6060f1SDimitry Andric       }
8545fe6060f1SDimitry Andric 
85460b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Moving Debug Value before :\n"
8547c9157d92SDimitry Andric                         << *DbgItem << ' ' << *VI);
8548c9157d92SDimitry Andric       DbgInserterHelper(DbgItem, VI);
85490b57cec5SDimitry Andric       MadeChange = true;
85500b57cec5SDimitry Andric       ++NumDbgValueMoved;
85510b57cec5SDimitry Andric     }
8552c9157d92SDimitry Andric   };
8553c9157d92SDimitry Andric 
8554c9157d92SDimitry Andric   for (BasicBlock &BB : F) {
8555c9157d92SDimitry Andric     for (Instruction &Insn : llvm::make_early_inc_range(BB)) {
8556c9157d92SDimitry Andric       // Process dbg.value intrinsics.
8557c9157d92SDimitry Andric       DbgValueInst *DVI = dyn_cast<DbgValueInst>(&Insn);
8558c9157d92SDimitry Andric       if (DVI) {
8559c9157d92SDimitry Andric         DbgProcessor(DVI, DVI);
8560c9157d92SDimitry Andric         continue;
8561c9157d92SDimitry Andric       }
8562c9157d92SDimitry Andric 
8563c9157d92SDimitry Andric       // If this isn't a dbg.value, process any attached DPValue records
8564c9157d92SDimitry Andric       // attached to this instruction.
8565c9157d92SDimitry Andric       for (DPValue &DPV : llvm::make_early_inc_range(Insn.getDbgValueRange())) {
8566c9157d92SDimitry Andric         if (DPV.Type != DPValue::LocationType::Value)
8567c9157d92SDimitry Andric           continue;
8568c9157d92SDimitry Andric         DbgProcessor(&DPV, &Insn);
85690b57cec5SDimitry Andric       }
8570fe6060f1SDimitry Andric     }
8571c9157d92SDimitry Andric   }
8572c9157d92SDimitry Andric 
8573fe6060f1SDimitry Andric   return MadeChange;
8574fe6060f1SDimitry Andric }
8575fe6060f1SDimitry Andric 
8576fe6060f1SDimitry Andric // Group scattered pseudo probes in a block to favor SelectionDAG. Scattered
8577fe6060f1SDimitry Andric // probes can be chained dependencies of other regular DAG nodes and block DAG
8578fe6060f1SDimitry Andric // combine optimizations.
placePseudoProbes(Function & F)8579fe6060f1SDimitry Andric bool CodeGenPrepare::placePseudoProbes(Function &F) {
8580fe6060f1SDimitry Andric   bool MadeChange = false;
8581fe6060f1SDimitry Andric   for (auto &Block : F) {
8582fe6060f1SDimitry Andric     // Move the rest probes to the beginning of the block.
8583fe6060f1SDimitry Andric     auto FirstInst = Block.getFirstInsertionPt();
8584fe6060f1SDimitry Andric     while (FirstInst != Block.end() && FirstInst->isDebugOrPseudoInst())
8585fe6060f1SDimitry Andric       ++FirstInst;
8586fe6060f1SDimitry Andric     BasicBlock::iterator I(FirstInst);
8587fe6060f1SDimitry Andric     I++;
8588fe6060f1SDimitry Andric     while (I != Block.end()) {
8589fe6060f1SDimitry Andric       if (auto *II = dyn_cast<PseudoProbeInst>(I++)) {
8590fe6060f1SDimitry Andric         II->moveBefore(&*FirstInst);
8591fe6060f1SDimitry Andric         MadeChange = true;
8592fe6060f1SDimitry Andric       }
8593fe6060f1SDimitry Andric     }
8594fe6060f1SDimitry Andric   }
85950b57cec5SDimitry Andric   return MadeChange;
85960b57cec5SDimitry Andric }
85970b57cec5SDimitry Andric 
85980b57cec5SDimitry Andric /// Scale down both weights to fit into uint32_t.
scaleWeights(uint64_t & NewTrue,uint64_t & NewFalse)85990b57cec5SDimitry Andric static void scaleWeights(uint64_t &NewTrue, uint64_t &NewFalse) {
86000b57cec5SDimitry Andric   uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
86010b57cec5SDimitry Andric   uint32_t Scale = (NewMax / std::numeric_limits<uint32_t>::max()) + 1;
86020b57cec5SDimitry Andric   NewTrue = NewTrue / Scale;
86030b57cec5SDimitry Andric   NewFalse = NewFalse / Scale;
86040b57cec5SDimitry Andric }
86050b57cec5SDimitry Andric 
86060b57cec5SDimitry Andric /// Some targets prefer to split a conditional branch like:
86070b57cec5SDimitry Andric /// \code
86080b57cec5SDimitry Andric ///   %0 = icmp ne i32 %a, 0
86090b57cec5SDimitry Andric ///   %1 = icmp ne i32 %b, 0
86100b57cec5SDimitry Andric ///   %or.cond = or i1 %0, %1
86110b57cec5SDimitry Andric ///   br i1 %or.cond, label %TrueBB, label %FalseBB
86120b57cec5SDimitry Andric /// \endcode
86130b57cec5SDimitry Andric /// into multiple branch instructions like:
86140b57cec5SDimitry Andric /// \code
86150b57cec5SDimitry Andric ///   bb1:
86160b57cec5SDimitry Andric ///     %0 = icmp ne i32 %a, 0
86170b57cec5SDimitry Andric ///     br i1 %0, label %TrueBB, label %bb2
86180b57cec5SDimitry Andric ///   bb2:
86190b57cec5SDimitry Andric ///     %1 = icmp ne i32 %b, 0
86200b57cec5SDimitry Andric ///     br i1 %1, label %TrueBB, label %FalseBB
86210b57cec5SDimitry Andric /// \endcode
86220b57cec5SDimitry Andric /// This usually allows instruction selection to do even further optimizations
86230b57cec5SDimitry Andric /// and combine the compare with the branch instruction. Currently this is
86240b57cec5SDimitry Andric /// applied for targets which have "cheap" jump instructions.
86250b57cec5SDimitry Andric ///
86260b57cec5SDimitry Andric /// FIXME: Remove the (equivalent?) implementation in SelectionDAG.
86270b57cec5SDimitry Andric ///
splitBranchCondition(Function & F,ModifyDT & ModifiedDT)8628bdd1243dSDimitry Andric bool CodeGenPrepare::splitBranchCondition(Function &F, ModifyDT &ModifiedDT) {
86295ffd83dbSDimitry Andric   if (!TM->Options.EnableFastISel || TLI->isJumpExpensive())
86300b57cec5SDimitry Andric     return false;
86310b57cec5SDimitry Andric 
86320b57cec5SDimitry Andric   bool MadeChange = false;
86330b57cec5SDimitry Andric   for (auto &BB : F) {
86340b57cec5SDimitry Andric     // Does this BB end with the following?
86350b57cec5SDimitry Andric     //   %cond1 = icmp|fcmp|binary instruction ...
86360b57cec5SDimitry Andric     //   %cond2 = icmp|fcmp|binary instruction ...
86370b57cec5SDimitry Andric     //   %cond.or = or|and i1 %cond1, cond2
86380b57cec5SDimitry Andric     //   br i1 %cond.or label %dest1, label %dest2"
8639e8d8bef9SDimitry Andric     Instruction *LogicOp;
86400b57cec5SDimitry Andric     BasicBlock *TBB, *FBB;
8641e8d8bef9SDimitry Andric     if (!match(BB.getTerminator(),
8642e8d8bef9SDimitry Andric                m_Br(m_OneUse(m_Instruction(LogicOp)), TBB, FBB)))
86430b57cec5SDimitry Andric       continue;
86440b57cec5SDimitry Andric 
86450b57cec5SDimitry Andric     auto *Br1 = cast<BranchInst>(BB.getTerminator());
86460b57cec5SDimitry Andric     if (Br1->getMetadata(LLVMContext::MD_unpredictable))
86470b57cec5SDimitry Andric       continue;
86480b57cec5SDimitry Andric 
8649480093f4SDimitry Andric     // The merging of mostly empty BB can cause a degenerate branch.
8650480093f4SDimitry Andric     if (TBB == FBB)
8651480093f4SDimitry Andric       continue;
8652480093f4SDimitry Andric 
86530b57cec5SDimitry Andric     unsigned Opc;
86540b57cec5SDimitry Andric     Value *Cond1, *Cond2;
8655e8d8bef9SDimitry Andric     if (match(LogicOp,
8656e8d8bef9SDimitry Andric               m_LogicalAnd(m_OneUse(m_Value(Cond1)), m_OneUse(m_Value(Cond2)))))
86570b57cec5SDimitry Andric       Opc = Instruction::And;
8658e8d8bef9SDimitry Andric     else if (match(LogicOp, m_LogicalOr(m_OneUse(m_Value(Cond1)),
86590b57cec5SDimitry Andric                                         m_OneUse(m_Value(Cond2)))))
86600b57cec5SDimitry Andric       Opc = Instruction::Or;
86610b57cec5SDimitry Andric     else
86620b57cec5SDimitry Andric       continue;
86630b57cec5SDimitry Andric 
8664e8d8bef9SDimitry Andric     auto IsGoodCond = [](Value *Cond) {
8665e8d8bef9SDimitry Andric       return match(
8666e8d8bef9SDimitry Andric           Cond,
8667e8d8bef9SDimitry Andric           m_CombineOr(m_Cmp(), m_CombineOr(m_LogicalAnd(m_Value(), m_Value()),
8668e8d8bef9SDimitry Andric                                            m_LogicalOr(m_Value(), m_Value()))));
8669e8d8bef9SDimitry Andric     };
8670e8d8bef9SDimitry Andric     if (!IsGoodCond(Cond1) || !IsGoodCond(Cond2))
86710b57cec5SDimitry Andric       continue;
86720b57cec5SDimitry Andric 
86730b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Before branch condition splitting\n"; BB.dump());
86740b57cec5SDimitry Andric 
86750b57cec5SDimitry Andric     // Create a new BB.
86765ffd83dbSDimitry Andric     auto *TmpBB =
86770b57cec5SDimitry Andric         BasicBlock::Create(BB.getContext(), BB.getName() + ".cond.split",
86780b57cec5SDimitry Andric                            BB.getParent(), BB.getNextNode());
8679bdd1243dSDimitry Andric     if (IsHugeFunc)
8680bdd1243dSDimitry Andric       FreshBBs.insert(TmpBB);
86810b57cec5SDimitry Andric 
86820b57cec5SDimitry Andric     // Update original basic block by using the first condition directly by the
86830b57cec5SDimitry Andric     // branch instruction and removing the no longer needed and/or instruction.
86840b57cec5SDimitry Andric     Br1->setCondition(Cond1);
86850b57cec5SDimitry Andric     LogicOp->eraseFromParent();
86860b57cec5SDimitry Andric 
86870b57cec5SDimitry Andric     // Depending on the condition we have to either replace the true or the
86880b57cec5SDimitry Andric     // false successor of the original branch instruction.
86890b57cec5SDimitry Andric     if (Opc == Instruction::And)
86900b57cec5SDimitry Andric       Br1->setSuccessor(0, TmpBB);
86910b57cec5SDimitry Andric     else
86920b57cec5SDimitry Andric       Br1->setSuccessor(1, TmpBB);
86930b57cec5SDimitry Andric 
86940b57cec5SDimitry Andric     // Fill in the new basic block.
86950b57cec5SDimitry Andric     auto *Br2 = IRBuilder<>(TmpBB).CreateCondBr(Cond2, TBB, FBB);
86960b57cec5SDimitry Andric     if (auto *I = dyn_cast<Instruction>(Cond2)) {
86970b57cec5SDimitry Andric       I->removeFromParent();
86980b57cec5SDimitry Andric       I->insertBefore(Br2);
86990b57cec5SDimitry Andric     }
87000b57cec5SDimitry Andric 
87010b57cec5SDimitry Andric     // Update PHI nodes in both successors. The original BB needs to be
87020b57cec5SDimitry Andric     // replaced in one successor's PHI nodes, because the branch comes now from
87030b57cec5SDimitry Andric     // the newly generated BB (NewBB). In the other successor we need to add one
87040b57cec5SDimitry Andric     // incoming edge to the PHI nodes, because both branch instructions target
87050b57cec5SDimitry Andric     // now the same successor. Depending on the original branch condition
87060b57cec5SDimitry Andric     // (and/or) we have to swap the successors (TrueDest, FalseDest), so that
87070b57cec5SDimitry Andric     // we perform the correct update for the PHI nodes.
87080b57cec5SDimitry Andric     // This doesn't change the successor order of the just created branch
87090b57cec5SDimitry Andric     // instruction (or any other instruction).
87100b57cec5SDimitry Andric     if (Opc == Instruction::Or)
87110b57cec5SDimitry Andric       std::swap(TBB, FBB);
87120b57cec5SDimitry Andric 
87130b57cec5SDimitry Andric     // Replace the old BB with the new BB.
87140b57cec5SDimitry Andric     TBB->replacePhiUsesWith(&BB, TmpBB);
87150b57cec5SDimitry Andric 
8716bdd1243dSDimitry Andric     // Add another incoming edge from the new BB.
87170b57cec5SDimitry Andric     for (PHINode &PN : FBB->phis()) {
87180b57cec5SDimitry Andric       auto *Val = PN.getIncomingValueForBlock(&BB);
87190b57cec5SDimitry Andric       PN.addIncoming(Val, TmpBB);
87200b57cec5SDimitry Andric     }
87210b57cec5SDimitry Andric 
87220b57cec5SDimitry Andric     // Update the branch weights (from SelectionDAGBuilder::
87230b57cec5SDimitry Andric     // FindMergedConditions).
87240b57cec5SDimitry Andric     if (Opc == Instruction::Or) {
87250b57cec5SDimitry Andric       // Codegen X | Y as:
87260b57cec5SDimitry Andric       // BB1:
87270b57cec5SDimitry Andric       //   jmp_if_X TBB
87280b57cec5SDimitry Andric       //   jmp TmpBB
87290b57cec5SDimitry Andric       // TmpBB:
87300b57cec5SDimitry Andric       //   jmp_if_Y TBB
87310b57cec5SDimitry Andric       //   jmp FBB
87320b57cec5SDimitry Andric       //
87330b57cec5SDimitry Andric 
87340b57cec5SDimitry Andric       // We have flexibility in setting Prob for BB1 and Prob for NewBB.
87350b57cec5SDimitry Andric       // The requirement is that
87360b57cec5SDimitry Andric       //   TrueProb for BB1 + (FalseProb for BB1 * TrueProb for TmpBB)
87370b57cec5SDimitry Andric       //     = TrueProb for original BB.
87380b57cec5SDimitry Andric       // Assuming the original weights are A and B, one choice is to set BB1's
87390b57cec5SDimitry Andric       // weights to A and A+2B, and set TmpBB's weights to A and 2B. This choice
87400b57cec5SDimitry Andric       // assumes that
87410b57cec5SDimitry Andric       //   TrueProb for BB1 == FalseProb for BB1 * TrueProb for TmpBB.
87420b57cec5SDimitry Andric       // Another choice is to assume TrueProb for BB1 equals to TrueProb for
87430b57cec5SDimitry Andric       // TmpBB, but the math is more complicated.
87440b57cec5SDimitry Andric       uint64_t TrueWeight, FalseWeight;
8745bdd1243dSDimitry Andric       if (extractBranchWeights(*Br1, TrueWeight, FalseWeight)) {
87460b57cec5SDimitry Andric         uint64_t NewTrueWeight = TrueWeight;
87470b57cec5SDimitry Andric         uint64_t NewFalseWeight = TrueWeight + 2 * FalseWeight;
87480b57cec5SDimitry Andric         scaleWeights(NewTrueWeight, NewFalseWeight);
8749bdd1243dSDimitry Andric         Br1->setMetadata(LLVMContext::MD_prof,
8750bdd1243dSDimitry Andric                          MDBuilder(Br1->getContext())
87510b57cec5SDimitry Andric                              .createBranchWeights(TrueWeight, FalseWeight));
87520b57cec5SDimitry Andric 
87530b57cec5SDimitry Andric         NewTrueWeight = TrueWeight;
87540b57cec5SDimitry Andric         NewFalseWeight = 2 * FalseWeight;
87550b57cec5SDimitry Andric         scaleWeights(NewTrueWeight, NewFalseWeight);
8756bdd1243dSDimitry Andric         Br2->setMetadata(LLVMContext::MD_prof,
8757bdd1243dSDimitry Andric                          MDBuilder(Br2->getContext())
87580b57cec5SDimitry Andric                              .createBranchWeights(TrueWeight, FalseWeight));
87590b57cec5SDimitry Andric       }
87600b57cec5SDimitry Andric     } else {
87610b57cec5SDimitry Andric       // Codegen X & Y as:
87620b57cec5SDimitry Andric       // BB1:
87630b57cec5SDimitry Andric       //   jmp_if_X TmpBB
87640b57cec5SDimitry Andric       //   jmp FBB
87650b57cec5SDimitry Andric       // TmpBB:
87660b57cec5SDimitry Andric       //   jmp_if_Y TBB
87670b57cec5SDimitry Andric       //   jmp FBB
87680b57cec5SDimitry Andric       //
87690b57cec5SDimitry Andric       //  This requires creation of TmpBB after CurBB.
87700b57cec5SDimitry Andric 
87710b57cec5SDimitry Andric       // We have flexibility in setting Prob for BB1 and Prob for TmpBB.
87720b57cec5SDimitry Andric       // The requirement is that
87730b57cec5SDimitry Andric       //   FalseProb for BB1 + (TrueProb for BB1 * FalseProb for TmpBB)
87740b57cec5SDimitry Andric       //     = FalseProb for original BB.
87750b57cec5SDimitry Andric       // Assuming the original weights are A and B, one choice is to set BB1's
87760b57cec5SDimitry Andric       // weights to 2A+B and B, and set TmpBB's weights to 2A and B. This choice
87770b57cec5SDimitry Andric       // assumes that
87780b57cec5SDimitry Andric       //   FalseProb for BB1 == TrueProb for BB1 * FalseProb for TmpBB.
87790b57cec5SDimitry Andric       uint64_t TrueWeight, FalseWeight;
8780bdd1243dSDimitry Andric       if (extractBranchWeights(*Br1, TrueWeight, FalseWeight)) {
87810b57cec5SDimitry Andric         uint64_t NewTrueWeight = 2 * TrueWeight + FalseWeight;
87820b57cec5SDimitry Andric         uint64_t NewFalseWeight = FalseWeight;
87830b57cec5SDimitry Andric         scaleWeights(NewTrueWeight, NewFalseWeight);
8784bdd1243dSDimitry Andric         Br1->setMetadata(LLVMContext::MD_prof,
8785bdd1243dSDimitry Andric                          MDBuilder(Br1->getContext())
87860b57cec5SDimitry Andric                              .createBranchWeights(TrueWeight, FalseWeight));
87870b57cec5SDimitry Andric 
87880b57cec5SDimitry Andric         NewTrueWeight = 2 * TrueWeight;
87890b57cec5SDimitry Andric         NewFalseWeight = FalseWeight;
87900b57cec5SDimitry Andric         scaleWeights(NewTrueWeight, NewFalseWeight);
8791bdd1243dSDimitry Andric         Br2->setMetadata(LLVMContext::MD_prof,
8792bdd1243dSDimitry Andric                          MDBuilder(Br2->getContext())
87930b57cec5SDimitry Andric                              .createBranchWeights(TrueWeight, FalseWeight));
87940b57cec5SDimitry Andric       }
87950b57cec5SDimitry Andric     }
87960b57cec5SDimitry Andric 
8797bdd1243dSDimitry Andric     ModifiedDT = ModifyDT::ModifyBBDT;
87980b57cec5SDimitry Andric     MadeChange = true;
87990b57cec5SDimitry Andric 
88000b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "After branch condition splitting\n"; BB.dump();
88010b57cec5SDimitry Andric                TmpBB->dump());
88020b57cec5SDimitry Andric   }
88030b57cec5SDimitry Andric   return MadeChange;
88040b57cec5SDimitry Andric }
8805